trpt.c revision 1.23.10.1 1 /* $NetBSD: trpt.c,v 1.23.10.1 2008/05/18 12:36:23 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 2005, 2006 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/cdefs.h>
63 #ifndef lint
64 __COPYRIGHT(
65 "@(#) Copyright (c) 1983, 1988, 1993\n\
66 The Regents of the University of California. All rights reserved.\n");
67 #endif /* not lint */
68
69 #ifndef lint
70 #if 0
71 static char sccsid[] = "@(#)trpt.c 8.1 (Berkeley) 6/6/93";
72 #else
73 __RCSID("$NetBSD: trpt.c,v 1.23.10.1 2008/05/18 12:36:23 yamt Exp $");
74 #endif
75 #endif /* not lint */
76
77 #define _CALLOUT_PRIVATE /* for defs in sys/callout.h */
78
79 #include <sys/param.h>
80 #include <sys/queue.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/sysctl.h>
84 #define PRUREQUESTS
85 #include <sys/protosw.h>
86 #include <sys/file.h>
87
88 #include <net/route.h>
89 #include <net/if.h>
90
91 #include <netinet/in.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/ip.h>
94 #include <netinet/in_pcb.h>
95 #include <netinet/ip_var.h>
96
97 #ifdef INET6
98 #ifndef INET
99 #include <netinet/in.h>
100 #endif
101 #include <netinet/ip6.h>
102 #endif
103
104 #include <netinet/tcp.h>
105 #define TCPSTATES
106 #include <netinet/tcp_fsm.h>
107 #include <netinet/tcp_seq.h>
108 #define TCPTIMERS
109 #include <netinet/tcp_timer.h>
110 #include <netinet/tcp_var.h>
111 #include <netinet/tcpip.h>
112 #define TANAMES
113 #include <netinet/tcp_debug.h>
114
115 #include <arpa/inet.h>
116
117 #include <err.h>
118 #include <stdio.h>
119 #include <errno.h>
120 #include <kvm.h>
121 #include <nlist.h>
122 #include <paths.h>
123 #include <limits.h>
124 #include <stdlib.h>
125 #include <unistd.h>
126
127 struct nlist nl[] = {
128 #define N_HARDCLOCK_TICKS 0
129 { "_hardclock_ticks" },
130 #define N_TCP_DEBUG 1
131 { "_tcp_debug" },
132 #define N_TCP_DEBX 2
133 { "_tcp_debx" },
134 { NULL },
135 };
136
137 static caddr_t tcp_pcbs[TCP_NDEBUG];
138 static n_time ntime;
139 static int aflag, follow, sflag, tflag;
140
141 /* see sys/netinet/tcp_debug.c */
142 struct tcp_debug tcp_debug[TCP_NDEBUG];
143 int tcp_debx;
144
145 int main(int, char *[]);
146 void dotrace(caddr_t);
147 void tcp_trace(short, short, struct tcpcb *, struct tcpcb *,
148 int, void *, int);
149 int numeric(const void *, const void *);
150 void usage(void);
151
152 kvm_t *kd;
153 int use_sysctl;
154
155 int
156 main(int argc, char *argv[])
157 {
158 int ch, i, jflag, npcbs;
159 char *system, *core, *cp, errbuf[_POSIX2_LINE_MAX];
160 unsigned long l;
161
162 jflag = npcbs = 0;
163 system = core = NULL;
164
165 while ((ch = getopt(argc, argv, "afjp:stN:M:")) != -1) {
166 switch (ch) {
167 case 'a':
168 ++aflag;
169 break;
170 case 'f':
171 ++follow;
172 setlinebuf(stdout);
173 break;
174 case 'j':
175 ++jflag;
176 break;
177 case 'p':
178 if (npcbs >= TCP_NDEBUG)
179 errx(1, "too many pcbs specified");
180 errno = 0;
181 cp = NULL;
182 l = strtoul(optarg, &cp, 16);
183 tcp_pcbs[npcbs] = (caddr_t)l;
184 if (*optarg == '\0' || *cp != '\0' || errno ||
185 (unsigned long)tcp_pcbs[npcbs] != l)
186 errx(1, "invalid address: %s", optarg);
187 npcbs++;
188 break;
189 case 's':
190 ++sflag;
191 break;
192 case 't':
193 ++tflag;
194 break;
195 case 'N':
196 system = optarg;
197 break;
198 case 'M':
199 core = optarg;
200 break;
201 default:
202 usage();
203 /* NOTREACHED */
204 }
205 }
206 argc -= optind;
207 argv += optind;
208
209 if (argc)
210 usage();
211
212 use_sysctl = (system == NULL && core == NULL);
213
214 if (use_sysctl) {
215 size_t lenx = sizeof(tcp_debx);
216 size_t lend = sizeof(tcp_debug);
217
218 if (sysctlbyname("net.inet.tcp.debx", &tcp_debx, &lenx,
219 NULL, 0) == -1)
220 err(1, "net.inet.tcp.debx");
221 if (sysctlbyname("net.inet.tcp.debug", &tcp_debug, &lend,
222 NULL, 0) == -1)
223 err(1, "net.inet.tcp.debug");
224 } else {
225 kd = kvm_openfiles(system, core, NULL, O_RDONLY, errbuf);
226 if (kd == NULL)
227 errx(1, "can't open kmem: %s", errbuf);
228
229 if (kvm_nlist(kd, nl))
230 errx(2, "%s: no namelist", system);
231
232 if (kvm_read(kd, nl[N_TCP_DEBX].n_value, (char *)&tcp_debx,
233 sizeof(tcp_debx)) != sizeof(tcp_debx))
234 errx(3, "tcp_debx: %s", kvm_geterr(kd));
235
236 if (kvm_read(kd, nl[N_TCP_DEBUG].n_value, (char *)tcp_debug,
237 sizeof(tcp_debug)) != sizeof(tcp_debug))
238 errx(3, "tcp_debug: %s", kvm_geterr(kd));
239 }
240
241 /*
242 * If no control blocks have been specified, figure
243 * out how many distinct one we have and summarize
244 * them in tcp_pcbs for sorting the trace records
245 * below.
246 */
247 if (npcbs == 0) {
248 for (i = 0; i < TCP_NDEBUG; i++) {
249 struct tcp_debug *td = &tcp_debug[i];
250 int j;
251
252 if (td->td_tcb == 0)
253 continue;
254 for (j = 0; j < npcbs; j++)
255 if (tcp_pcbs[j] == td->td_tcb)
256 break;
257 if (j >= npcbs)
258 tcp_pcbs[npcbs++] = td->td_tcb;
259 }
260 if (npcbs == 0)
261 exit(0);
262 }
263 qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric);
264 if (jflag) {
265 for (i = 0;;) {
266 printf("%lx", (long)tcp_pcbs[i]);
267 if (++i == npcbs)
268 break;
269 fputs(", ", stdout);
270 }
271 putchar('\n');
272 } else {
273 for (i = 0; i < npcbs; i++) {
274 printf("\n%lx:\n", (long)tcp_pcbs[i]);
275 dotrace(tcp_pcbs[i]);
276 }
277 }
278 exit(0);
279 }
280
281 void
282 dotrace(caddr_t tcpcb)
283 {
284 struct tcp_debug *td;
285 int prev_debx = tcp_debx;
286 int i;
287
288 again:
289 if (--tcp_debx < 0)
290 tcp_debx = TCP_NDEBUG - 1;
291 for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
292 td = &tcp_debug[i];
293 if (tcpcb && td->td_tcb != tcpcb)
294 continue;
295 ntime = ntohl(td->td_time);
296 switch (td->td_family) {
297 case AF_INET:
298 tcp_trace(td->td_act, td->td_ostate,
299 (struct tcpcb *)td->td_tcb, &td->td_cb,
300 td->td_family, &td->td_ti, td->td_req);
301 break;
302 #ifdef INET6
303 case AF_INET6:
304 tcp_trace(td->td_act, td->td_ostate,
305 (struct tcpcb *)td->td_tcb, &td->td_cb,
306 td->td_family, &td->td_ti6, td->td_req);
307 break;
308 #endif
309 default:
310 tcp_trace(td->td_act, td->td_ostate,
311 (struct tcpcb *)td->td_tcb, &td->td_cb,
312 td->td_family, NULL, td->td_req);
313 break;
314 }
315 if (i == tcp_debx)
316 goto done;
317 }
318 for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) {
319 td = &tcp_debug[i];
320 if (tcpcb && td->td_tcb != tcpcb)
321 continue;
322 ntime = ntohl(td->td_time);
323 switch (td->td_family) {
324 case AF_INET:
325 tcp_trace(td->td_act, td->td_ostate,
326 (struct tcpcb *)td->td_tcb, &td->td_cb,
327 td->td_family, &td->td_ti, td->td_req);
328 break;
329 #ifdef INET6
330 case AF_INET6:
331 tcp_trace(td->td_act, td->td_ostate,
332 (struct tcpcb *)td->td_tcb, &td->td_cb,
333 td->td_family, &td->td_ti6, td->td_req);
334 break;
335 #endif
336 default:
337 tcp_trace(td->td_act, td->td_ostate,
338 (struct tcpcb *)td->td_tcb, &td->td_cb,
339 td->td_family, NULL, td->td_req);
340 break;
341 }
342 }
343 done:
344 if (follow) {
345 prev_debx = tcp_debx + 1;
346 if (prev_debx >= TCP_NDEBUG)
347 prev_debx = 0;
348 do {
349 sleep(1);
350 if (use_sysctl) {
351 size_t len = sizeof(tcp_debx);
352
353 if (sysctlbyname("net.inet.tcp.debx",
354 &tcp_debx, &len, NULL, 0) == -1)
355 err(1, "net.inet.tcp.debx");
356 } else
357 if (kvm_read(kd, nl[N_TCP_DEBX].n_value,
358 (char *)&tcp_debx, sizeof(tcp_debx)) !=
359 sizeof(tcp_debx))
360 errx(3, "tcp_debx: %s",
361 kvm_geterr(kd));
362 } while (tcp_debx == prev_debx);
363
364 if (use_sysctl) {
365 size_t len = sizeof(tcp_debug);
366
367 if (sysctlbyname("net.inet.tcp.debug", &tcp_debug,
368 &len, NULL, 0) == -1)
369 err(1, "net.inet.tcp.debug");
370 } else
371 if (kvm_read(kd, nl[N_TCP_DEBUG].n_value,
372 (char *)tcp_debug,
373 sizeof(tcp_debug)) != sizeof(tcp_debug))
374 errx(3, "tcp_debug: %s", kvm_geterr(kd));
375
376 goto again;
377 }
378 }
379
380 /*
381 * Tcp debug routines
382 */
383 /*ARGSUSED*/
384 void
385 tcp_trace(short act, short ostate, struct tcpcb *atp, struct tcpcb *tp,
386 int family, void *packet, int req)
387 {
388 tcp_seq seq, ack;
389 int flags, len, win, timer;
390 struct tcphdr *th = NULL;
391 struct ip *ip = NULL;
392 #ifdef INET6
393 struct ip6_hdr *ip6 = NULL;
394 #endif
395 callout_impl_t *ci;
396 char hbuf[MAXHOSTNAMELEN];
397
398 len = 0; /* XXXGCC -Wuninitialized */
399
400 switch (family) {
401 case AF_INET:
402 if (packet) {
403 ip = (struct ip *)packet;
404 th = (struct tcphdr *)(ip + 1);
405 }
406 break;
407 #ifdef INET6
408 case AF_INET6:
409 if (packet) {
410 ip6 = (struct ip6_hdr *)packet;
411 th = (struct tcphdr *)(ip6 + 1);
412 }
413 break;
414 #endif
415 default:
416 return;
417 }
418
419 printf("%03d %s:%s ", (ntime/10) % 1000, tcpstates[ostate],
420 tanames[act]);
421
422 #ifndef INET6
423 if (!ip)
424 #else
425 if (!(ip || ip6))
426 #endif
427 goto skipact;
428
429 switch (act) {
430 case TA_INPUT:
431 case TA_OUTPUT:
432 case TA_DROP:
433 if (aflag) {
434 inet_ntop(family,
435 #ifndef INET6
436 (void *)&ip->ip_src,
437 #else
438 family == AF_INET ? (void *)&ip->ip_src
439 : (void *)&ip6->ip6_src,
440 #endif
441 hbuf, sizeof(hbuf));
442 printf("(src=%s,%u, ",
443 hbuf, ntohs(th->th_sport));
444 inet_ntop(family,
445 #ifndef INET6
446 (void *)&ip->ip_dst,
447 #else
448 family == AF_INET ? (void *)&ip->ip_dst
449 : (void *)&ip6->ip6_dst,
450 #endif
451 hbuf, sizeof(hbuf));
452 printf("dst=%s,%u)",
453 hbuf, ntohs(th->th_dport));
454 }
455 seq = th->th_seq;
456 ack = th->th_ack;
457 if (ip)
458 len = ip->ip_len;
459 #ifdef INET6
460 else if (ip6)
461 len = ip6->ip6_plen;
462 #endif
463 win = th->th_win;
464 if (act == TA_OUTPUT) {
465 NTOHL(seq);
466 NTOHL(ack);
467 NTOHS(len);
468 NTOHS(win);
469 }
470 if (act == TA_OUTPUT)
471 len -= sizeof(struct tcphdr);
472 if (len)
473 printf("[%x..%x)", seq, seq + len);
474 else
475 printf("%x", seq);
476 printf("@%x", ack);
477 if (win)
478 printf("(win=%x)", win);
479 flags = th->th_flags;
480 if (flags) {
481 char *cp = "<";
482 #define pf(flag, string) { \
483 if (th->th_flags&flag) { \
484 (void)printf("%s%s", cp, string); \
485 cp = ","; \
486 } \
487 }
488 pf(TH_SYN, "SYN");
489 pf(TH_ACK, "ACK");
490 pf(TH_FIN, "FIN");
491 pf(TH_RST, "RST");
492 pf(TH_PUSH, "PUSH");
493 pf(TH_URG, "URG");
494 pf(TH_CWR, "CWR");
495 pf(TH_ECE, "ECE");
496 printf(">");
497 }
498 break;
499 case TA_USER:
500 timer = req >> 8;
501 req &= 0xff;
502 printf("%s", prurequests[req]);
503 if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
504 printf("<%s>", tcptimers[timer]);
505 break;
506 }
507
508 skipact:
509 printf(" -> %s", tcpstates[tp->t_state]);
510 /* print out internal state of tp !?! */
511 printf("\n");
512 if (sflag) {
513 printf("\trcv_nxt %x rcv_wnd %lx snd_una %x snd_nxt %x snd_max %x\n",
514 tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt,
515 tp->snd_max);
516 printf("\tsnd_wl1 %x snd_wl2 %x snd_wnd %lx\n", tp->snd_wl1,
517 tp->snd_wl2, tp->snd_wnd);
518 }
519 /* print out timers? */
520 if (tflag) {
521 char *cp = "\t";
522 int i;
523 int hardticks;
524
525 if (use_sysctl) {
526 size_t len = sizeof(hardticks);
527
528 if (sysctlbyname("kern.hardclock_ticks", &hardticks,
529 &len, NULL, 0) == -1)
530 err(1, "kern.hardclock_ticks");
531 } else {
532 if (kvm_read(kd, nl[N_HARDCLOCK_TICKS].n_value,
533 (char *)&hardticks,
534 sizeof(hardticks)) != sizeof(hardticks))
535 errx(3, "hardclock_ticks: %s", kvm_geterr(kd));
536
537 for (i = 0; i < TCPT_NTIMERS; i++) {
538 ci = (callout_impl_t *)&tp->t_timer[i];
539 if ((ci->c_flags & CALLOUT_PENDING) == 0)
540 continue;
541 printf("%s%s=%d", cp, tcptimers[i],
542 ci->c_time - hardticks);
543 if (i == TCPT_REXMT)
544 printf(" (t_rxtshft=%d)",
545 tp->t_rxtshift);
546 cp = ", ";
547 }
548 if (*cp != '\t')
549 putchar('\n');
550 }
551 }
552 }
553
554 int
555 numeric(const void *v1, const void *v2)
556 {
557 const caddr_t *c1 = v1;
558 const caddr_t *c2 = v2;
559 int rv;
560
561 if (*c1 < *c2)
562 rv = -1;
563 else if (*c1 > *c2)
564 rv = 1;
565 else
566 rv = 0;
567
568 return (rv);
569 }
570
571 void
572 usage(void)
573 {
574
575 (void) fprintf(stderr, "usage: %s [-afjst] [-p hex-address]"
576 " [-N system] [-M core]\n", getprogname());
577 exit(1);
578 }
579