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