File: | src/usr.sbin/trpt/trpt.c |
Warning: | line 175, column 2 Value stored to 'argv' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* $OpenBSD: trpt.c,v 1.39 2019/12/02 21:47:54 cheloha Exp $ */ |
2 | |
3 | /*- |
4 | * Copyright (c) 1997 The NetBSD Foundation, Inc. |
5 | * All rights reserved. |
6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, |
9 | * NASA Ames Research Center. |
10 | * |
11 | * Redistribution and use in source and binary forms, with or without |
12 | * modification, are permitted provided that the following conditions |
13 | * are met: |
14 | * 1. Redistributions of source code must retain the above copyright |
15 | * notice, this list of conditions and the following disclaimer. |
16 | * 2. Redistributions in binary form must reproduce the above copyright |
17 | * notice, this list of conditions and the following disclaimer in the |
18 | * documentation and/or other materials provided with the distribution. |
19 | * |
20 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
22 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
23 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
24 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
30 | * POSSIBILITY OF SUCH DAMAGE. |
31 | */ |
32 | |
33 | /* |
34 | * Copyright (c) 1983, 1988, 1993 |
35 | * The Regents of the University of California. All rights reserved. |
36 | * |
37 | * Redistribution and use in source and binary forms, with or without |
38 | * modification, are permitted provided that the following conditions |
39 | * are met: |
40 | * 1. Redistributions of source code must retain the above copyright |
41 | * notice, this list of conditions and the following disclaimer. |
42 | * 2. Redistributions in binary form must reproduce the above copyright |
43 | * notice, this list of conditions and the following disclaimer in the |
44 | * documentation and/or other materials provided with the distribution. |
45 | * 3. Neither the name of the University nor the names of its contributors |
46 | * may be used to endorse or promote products derived from this software |
47 | * without specific prior written permission. |
48 | * |
49 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
50 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
51 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
52 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
53 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
54 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
55 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
56 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
57 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
58 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
59 | * SUCH DAMAGE. |
60 | */ |
61 | |
62 | #include <sys/queue.h> |
63 | #include <sys/time.h> |
64 | #include <sys/socket.h> |
65 | #define PRUREQUESTS |
66 | #include <sys/protosw.h> |
67 | #define _KERNEL |
68 | #include <sys/timeout.h> /* to get timeout_pending() and such */ |
69 | #undef _KERNEL |
70 | |
71 | #include <net/route.h> |
72 | #include <net/if.h> |
73 | |
74 | #include <netinet/in.h> |
75 | #include <netinet/ip.h> |
76 | #include <netinet/in_pcb.h> |
77 | #include <netinet/ip_var.h> |
78 | #include <netinet/tcp.h> |
79 | #define TCPSTATES |
80 | #include <netinet/tcp_fsm.h> |
81 | #include <netinet/tcp_seq.h> |
82 | #define TCPTIMERS |
83 | #include <netinet/tcp_timer.h> |
84 | #include <netinet/tcp_var.h> |
85 | #define TANAMES |
86 | #include <netinet/tcp_debug.h> |
87 | |
88 | #include <arpa/inet.h> |
89 | |
90 | #include <err.h> |
91 | #include <stdio.h> |
92 | #include <errno(*__errno()).h> |
93 | #include <fcntl.h> |
94 | #include <kvm.h> |
95 | #include <nlist.h> |
96 | #include <paths.h> |
97 | #include <limits.h> |
98 | #include <stdlib.h> |
99 | #include <unistd.h> |
100 | |
101 | struct nlist nl[] = { |
102 | #define N_TCP_DEBUG0 0 /* no sysctl */ |
103 | { "_tcp_debug" }, |
104 | #define N_TCP_DEBX1 1 /* no sysctl */ |
105 | { "_tcp_debx" }, |
106 | { NULL((void *)0) }, |
107 | }; |
108 | |
109 | int tcp_debx; |
110 | struct tcp_debug tcp_debug[TCP_NDEBUG100]; |
111 | |
112 | static caddr_t tcp_pcbs[TCP_NDEBUG100]; |
113 | static u_int32_t ntime; |
114 | static int aflag, follow, sflag, tflag; |
115 | |
116 | extern char *__progname; |
117 | |
118 | void dotrace(caddr_t); |
119 | void tcp_trace(short, short, struct tcpcb *, struct tcpiphdr *, |
120 | struct tcpipv6hdr *, int); |
121 | int numeric(const void *, const void *); |
122 | void usage(void); |
123 | |
124 | kvm_t *kd; |
125 | |
126 | int |
127 | main(int argc, char *argv[]) |
128 | { |
129 | char *sys = NULL((void *)0), *core = NULL((void *)0), *cp, errbuf[_POSIX2_LINE_MAX2048]; |
130 | int ch, i, jflag = 0, npcbs = 0; |
131 | unsigned long l; |
132 | gid_t gid; |
133 | |
134 | while ((ch = getopt(argc, argv, "afjM:N:p:st")) != -1) { |
135 | switch (ch) { |
136 | case 'a': |
137 | aflag = 1; |
138 | break; |
139 | case 'f': |
140 | follow = 1; |
141 | setvbuf(stdout(&__sF[1]), NULL((void *)0), _IOLBF1, 0); |
142 | break; |
143 | case 'j': |
144 | jflag = 1; |
145 | break; |
146 | case 'p': |
147 | if (npcbs >= TCP_NDEBUG100) |
148 | errx(1, "too many pcbs specified"); |
149 | errno(*__errno()) = 0; |
150 | l = strtoul(optarg, &cp, 16); |
151 | tcp_pcbs[npcbs] = (caddr_t)l; |
152 | if (*optarg == '\0' || *cp != '\0' || errno(*__errno()) || |
153 | (unsigned long)tcp_pcbs[npcbs] != l) |
154 | errx(1, "invalid address: %s", optarg); |
155 | npcbs++; |
156 | break; |
157 | case 's': |
158 | sflag = 1; |
159 | break; |
160 | case 't': |
161 | tflag = 1; |
162 | break; |
163 | case 'N': |
164 | sys = optarg; |
165 | break; |
166 | case 'M': |
167 | core = optarg; |
168 | break; |
169 | default: |
170 | usage(); |
171 | /* NOTREACHED */ |
172 | } |
173 | } |
174 | argc -= optind; |
175 | argv += optind; |
Value stored to 'argv' is never read | |
176 | |
177 | if (argc) |
178 | usage(); |
179 | |
180 | /* |
181 | * Discard setgid privileged if not the running kernel so that bad |
182 | * guys can't print interesting stuff from kernel memory. |
183 | */ |
184 | gid = getgid(); |
185 | if (core != NULL((void *)0) || sys != NULL((void *)0)) |
186 | if (setresgid(gid, gid, gid) == -1) |
187 | err(1, "setresgid"); |
188 | |
189 | kd = kvm_openfiles(sys, core, NULL((void *)0), O_RDONLY0x0000, errbuf); |
190 | if (kd == NULL((void *)0)) |
191 | errx(1, "can't open kmem: %s", errbuf); |
192 | |
193 | if (core == NULL((void *)0) && sys == NULL((void *)0)) |
194 | if (setresgid(gid, gid, gid) == -1) |
195 | err(1, "setresgid"); |
196 | |
197 | if (kvm_nlist(kd, nl)) |
198 | errx(2, "%s: no namelist", sys ? sys : _PATH_UNIX"/bsd"); |
199 | |
200 | if (pledge("stdio", NULL((void *)0)) == -1) |
201 | err(1, "pledge"); |
202 | |
203 | if (kvm_read(kd, nl[N_TCP_DEBX1].n_value, (char *)&tcp_debx, |
204 | sizeof(tcp_debx)) != sizeof(tcp_debx)) |
205 | errx(3, "tcp_debx: %s", kvm_geterr(kd)); |
206 | |
207 | if (kvm_read(kd, nl[N_TCP_DEBUG0].n_value, (char *)tcp_debug, |
208 | sizeof(tcp_debug)) != sizeof(tcp_debug)) |
209 | errx(3, "tcp_debug: %s", kvm_geterr(kd)); |
210 | |
211 | /* |
212 | * If no control blocks have been specified, figure |
213 | * out how many distinct one we have and summarize |
214 | * them in tcp_pcbs for sorting the trace records |
215 | * below. |
216 | */ |
217 | if (npcbs == 0) { |
218 | for (i = 0; i < TCP_NDEBUG100; i++) { |
219 | struct tcp_debug *td = &tcp_debug[i]; |
220 | int j; |
221 | |
222 | if (td->td_tcb == 0) |
223 | continue; |
224 | for (j = 0; j < npcbs; j++) |
225 | if (tcp_pcbs[j] == td->td_tcb) |
226 | break; |
227 | if (j >= npcbs) |
228 | tcp_pcbs[npcbs++] = td->td_tcb; |
229 | } |
230 | if (npcbs == 0) |
231 | exit(0); |
232 | } |
233 | qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric); |
234 | if (jflag) { |
235 | for (i = 0;;) { |
236 | printf("%lx", (long)tcp_pcbs[i]); |
237 | if (++i == npcbs) |
238 | break; |
239 | fputs(", ", stdout(&__sF[1])); |
240 | } |
241 | putchar('\n')(!__isthreaded ? __sputc('\n', (&__sF[1])) : (putc)('\n', (&__sF[1]))); |
242 | } else { |
243 | for (i = 0; i < npcbs; i++) { |
244 | printf("\n%lx:\n", (long)tcp_pcbs[i]); |
245 | dotrace(tcp_pcbs[i]); |
246 | } |
247 | } |
248 | exit(0); |
249 | } |
250 | |
251 | void |
252 | dotrace(caddr_t tcpcb) |
253 | { |
254 | struct tcp_debug *td; |
255 | int prev_debx = tcp_debx; |
256 | int i; |
257 | |
258 | again: |
259 | if (--tcp_debx < 0) |
260 | tcp_debx = TCP_NDEBUG100 - 1; |
261 | for (i = prev_debx % TCP_NDEBUG100; i < TCP_NDEBUG100; i++) { |
262 | td = &tcp_debug[i]; |
263 | if (tcpcb && td->td_tcb != tcpcb) |
264 | continue; |
265 | ntime = ntohl(td->td_time)(__uint32_t)(__builtin_constant_p(td->td_time) ? (__uint32_t )(((__uint32_t)(td->td_time) & 0xff) << 24 | ((__uint32_t )(td->td_time) & 0xff00) << 8 | ((__uint32_t)(td ->td_time) & 0xff0000) >> 8 | ((__uint32_t)(td-> td_time) & 0xff000000) >> 24) : __swap32md(td->td_time )); |
266 | tcp_trace(td->td_act, td->td_ostate, |
267 | &td->td_cb, &td->td_ti, |
268 | &td->td_ti6, td->td_req); |
269 | if (i == tcp_debx) |
270 | goto done; |
271 | } |
272 | for (i = 0; i <= tcp_debx % TCP_NDEBUG100; i++) { |
273 | td = &tcp_debug[i]; |
274 | if (tcpcb && td->td_tcb != tcpcb) |
275 | continue; |
276 | ntime = ntohl(td->td_time)(__uint32_t)(__builtin_constant_p(td->td_time) ? (__uint32_t )(((__uint32_t)(td->td_time) & 0xff) << 24 | ((__uint32_t )(td->td_time) & 0xff00) << 8 | ((__uint32_t)(td ->td_time) & 0xff0000) >> 8 | ((__uint32_t)(td-> td_time) & 0xff000000) >> 24) : __swap32md(td->td_time )); |
277 | tcp_trace(td->td_act, td->td_ostate, |
278 | &td->td_cb, &td->td_ti, |
279 | &td->td_ti6, td->td_req); |
280 | } |
281 | done: |
282 | if (follow) { |
283 | prev_debx = tcp_debx + 1; |
284 | if (prev_debx >= TCP_NDEBUG100) |
285 | prev_debx = 0; |
286 | do { |
287 | sleep(1); |
288 | if (kvm_read(kd, nl[N_TCP_DEBX1].n_value, |
289 | (char *)&tcp_debx, sizeof(tcp_debx)) != |
290 | sizeof(tcp_debx)) |
291 | errx(3, "tcp_debx: %s", kvm_geterr(kd)); |
292 | } while (tcp_debx == prev_debx); |
293 | |
294 | if (kvm_read(kd, nl[N_TCP_DEBUG0].n_value, (char *)tcp_debug, |
295 | sizeof(tcp_debug)) != sizeof(tcp_debug)) |
296 | errx(3, "tcp_debug: %s", kvm_geterr(kd)); |
297 | |
298 | goto again; |
299 | } |
300 | } |
301 | |
302 | /* |
303 | * Tcp debug routines |
304 | */ |
305 | /*ARGSUSED*/ |
306 | void |
307 | tcp_trace(short act, short ostate, struct tcpcb *tp, |
308 | struct tcpiphdr *ti, struct tcpipv6hdr *ti6, int req) |
309 | { |
310 | tcp_seq seq, ack; |
311 | int flags, len, win; |
312 | struct tcphdr *th; |
313 | char hbuf[INET6_ADDRSTRLEN46]; |
314 | |
315 | if (ti->ti_srcti_i.ih_src.s_addr) |
316 | th = &ti->ti_t; |
317 | else |
318 | th = &ti6->ti6_t; |
319 | |
320 | printf("%03d %s:%s ", (ntime/10) % 1000, tcpstates[ostate], |
321 | tanames[act]); |
322 | switch (act) { |
323 | case TA_INPUT0: |
324 | case TA_OUTPUT1: |
325 | case TA_DROP4: |
326 | if (aflag) { |
327 | if (ti->ti_srcti_i.ih_src.s_addr) { |
328 | printf("(src=%s,%u, ", |
329 | inet_ntoa(ti->ti_srcti_i.ih_src), ntohs(ti->ti_sport)(__uint16_t)(__builtin_constant_p(ti->ti_t.th_sport) ? (__uint16_t )(((__uint16_t)(ti->ti_t.th_sport) & 0xffU) << 8 | ((__uint16_t)(ti->ti_t.th_sport) & 0xff00U) >> 8) : __swap16md(ti->ti_t.th_sport))); |
330 | printf("dst=%s,%u)", |
331 | inet_ntoa(ti->ti_dstti_i.ih_dst), ntohs(ti->ti_dport)(__uint16_t)(__builtin_constant_p(ti->ti_t.th_dport) ? (__uint16_t )(((__uint16_t)(ti->ti_t.th_dport) & 0xffU) << 8 | ((__uint16_t)(ti->ti_t.th_dport) & 0xff00U) >> 8) : __swap16md(ti->ti_t.th_dport))); |
332 | } else { |
333 | printf("(src=%s,%u, ", |
334 | inet_ntop(AF_INET624, &ti6->ti6_srcti6_i.ip6_src, |
335 | hbuf, sizeof(hbuf)), ntohs(ti->ti_sport)(__uint16_t)(__builtin_constant_p(ti->ti_t.th_sport) ? (__uint16_t )(((__uint16_t)(ti->ti_t.th_sport) & 0xffU) << 8 | ((__uint16_t)(ti->ti_t.th_sport) & 0xff00U) >> 8) : __swap16md(ti->ti_t.th_sport))); |
336 | printf("dst=%s,%u)", |
337 | inet_ntop(AF_INET624, &ti6->ti6_dstti6_i.ip6_dst, |
338 | hbuf, sizeof(hbuf)), ntohs(ti->ti_dport)(__uint16_t)(__builtin_constant_p(ti->ti_t.th_dport) ? (__uint16_t )(((__uint16_t)(ti->ti_t.th_dport) & 0xffU) << 8 | ((__uint16_t)(ti->ti_t.th_dport) & 0xff00U) >> 8) : __swap16md(ti->ti_t.th_dport))); |
339 | } |
340 | } |
341 | seq = th->th_seq; |
342 | ack = th->th_ack; |
343 | if (ti->ti_srcti_i.ih_src.s_addr) |
344 | len = ti->ti_lenti_i.ih_len; |
345 | else |
346 | len = ti6->ti6_plenti6_i.ip6_ctlun.ip6_un1.ip6_un1_plen; /*XXX intermediate header*/ |
347 | win = th->th_win; |
348 | if (act == TA_OUTPUT1) { |
349 | NTOHL(seq)(seq) = (__uint32_t)(__builtin_constant_p((u_int32_t)(seq)) ? (__uint32_t)(((__uint32_t)((u_int32_t)(seq)) & 0xff) << 24 | ((__uint32_t)((u_int32_t)(seq)) & 0xff00) << 8 | ((__uint32_t)((u_int32_t)(seq)) & 0xff0000) >> 8 | ((__uint32_t)((u_int32_t)(seq)) & 0xff000000) >> 24) : __swap32md((u_int32_t)(seq))); |
350 | NTOHL(ack)(ack) = (__uint32_t)(__builtin_constant_p((u_int32_t)(ack)) ? (__uint32_t)(((__uint32_t)((u_int32_t)(ack)) & 0xff) << 24 | ((__uint32_t)((u_int32_t)(ack)) & 0xff00) << 8 | ((__uint32_t)((u_int32_t)(ack)) & 0xff0000) >> 8 | ((__uint32_t)((u_int32_t)(ack)) & 0xff000000) >> 24) : __swap32md((u_int32_t)(ack))); |
351 | NTOHS(win)(win) = (__uint16_t)(__builtin_constant_p((u_int16_t)(win)) ? (__uint16_t)(((__uint16_t)((u_int16_t)(win)) & 0xffU) << 8 | ((__uint16_t)((u_int16_t)(win)) & 0xff00U) >> 8 ) : __swap16md((u_int16_t)(win))); |
352 | } |
353 | if (len) |
354 | printf("[%x..%x)", seq, seq + len); |
355 | else |
356 | printf("%x", seq); |
357 | printf("@%x", ack); |
358 | if (win) |
359 | printf("(win=%x)", win); |
360 | flags = th->th_flags; |
361 | if (flags) { |
362 | char *cp = "<"; |
363 | #define pf(flag, string){ if (th->th_flags & flag) { (void)printf("%s%s", cp, string ); cp = ","; } } { \ |
364 | if (th->th_flags & flag) { \ |
365 | (void)printf("%s%s", cp, string); \ |
366 | cp = ","; \ |
367 | } \ |
368 | } |
369 | pf(TH_SYN, "SYN"){ if (th->th_flags & 0x02) { (void)printf("%s%s", cp, "SYN" ); cp = ","; } }; |
370 | pf(TH_ACK, "ACK"){ if (th->th_flags & 0x10) { (void)printf("%s%s", cp, "ACK" ); cp = ","; } }; |
371 | pf(TH_FIN, "FIN"){ if (th->th_flags & 0x01) { (void)printf("%s%s", cp, "FIN" ); cp = ","; } }; |
372 | pf(TH_RST, "RST"){ if (th->th_flags & 0x04) { (void)printf("%s%s", cp, "RST" ); cp = ","; } }; |
373 | pf(TH_PUSH, "PUSH"){ if (th->th_flags & 0x08) { (void)printf("%s%s", cp, "PUSH" ); cp = ","; } }; |
374 | pf(TH_URG, "URG"){ if (th->th_flags & 0x20) { (void)printf("%s%s", cp, "URG" ); cp = ","; } }; |
375 | printf(">"); |
376 | } |
377 | break; |
378 | case TA_USER2: |
379 | printf("%s", prurequests[req]); |
380 | break; |
381 | case TA_TIMER5: |
382 | printf("%s", tcptimers[req]); |
383 | break; |
384 | } |
385 | printf(" -> %s", tcpstates[tp->t_state]); |
386 | /* print out internal state of tp !?! */ |
387 | printf("\n"); |
388 | if (sflag) { |
389 | printf("\trcv_nxt %x rcv_wnd %lx snd_una %x snd_nxt %x snd_max %x\n", |
390 | tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt, |
391 | tp->snd_max); |
392 | printf("\tsnd_wl1 %x snd_wl2 %x snd_wnd %lx\n", tp->snd_wl1, |
393 | tp->snd_wl2, tp->snd_wnd); |
394 | } |
395 | /* print out timers? */ |
396 | if (tflag) { |
397 | char *cp = "\t"; |
398 | int i; |
399 | |
400 | for (i = 0; i < TCPT_NTIMERS6; i++) { |
401 | if (timeout_pending(&tp->t_timer[i])((&tp->t_timer[i])->to_flags & 0x02)) |
402 | continue; |
403 | printf("%s%s=%d", cp, tcptimers[i], |
404 | tp->t_timer[i].to_time); |
405 | if (i == TCPT_REXMT0) |
406 | printf(" (t_rxtshft=%d)", tp->t_rxtshift); |
407 | cp = ", "; |
408 | } |
409 | if (*cp != '\t') |
410 | putchar('\n')(!__isthreaded ? __sputc('\n', (&__sF[1])) : (putc)('\n', (&__sF[1]))); |
411 | } |
412 | } |
413 | |
414 | int |
415 | numeric(const void *v1, const void *v2) |
416 | { |
417 | const caddr_t *c1 = v1; |
418 | const caddr_t *c2 = v2; |
419 | int rv; |
420 | |
421 | if (*c1 < *c2) |
422 | rv = -1; |
423 | else if (*c1 > *c2) |
424 | rv = 1; |
425 | else |
426 | rv = 0; |
427 | |
428 | return (rv); |
429 | } |
430 | |
431 | void |
432 | usage(void) |
433 | { |
434 | |
435 | (void) fprintf(stderr(&__sF[2]), "usage: %s [-afjst] [-M core]" |
436 | " [-N system] [-p hex-address]\n", __progname); |
437 | exit(1); |
438 | } |