Home | History | Annotate | Line # | Download | only in trpt
trpt.c revision 1.10
      1 /*	$NetBSD: trpt.c,v 1.10 2001/02/19 23:22:48 cgd 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  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1983, 1988, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by the University of
     55  *	California, Berkeley and its contributors.
     56  * 4. Neither the name of the University nor the names of its contributors
     57  *    may be used to endorse or promote products derived from this software
     58  *    without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 #ifndef lint
     75 __COPYRIGHT(
     76 "@(#) Copyright (c) 1983, 1988, 1993\n\
     77 	The Regents of the University of California.  All rights reserved.\n");
     78 #endif /* not lint */
     79 
     80 #ifndef lint
     81 #if 0
     82 static char sccsid[] = "@(#)trpt.c	8.1 (Berkeley) 6/6/93";
     83 #else
     84 __RCSID("$NetBSD: trpt.c,v 1.10 2001/02/19 23:22:48 cgd Exp $");
     85 #endif
     86 #endif /* not lint */
     87 
     88 #include <sys/param.h>
     89 #include <sys/queue.h>
     90 #include <sys/socket.h>
     91 #include <sys/socketvar.h>
     92 #define PRUREQUESTS
     93 #include <sys/protosw.h>
     94 #include <sys/file.h>
     95 
     96 #include <net/route.h>
     97 #include <net/if.h>
     98 
     99 #include <netinet/in.h>
    100 #include <netinet/in_systm.h>
    101 #include <netinet/ip.h>
    102 #include <netinet/in_pcb.h>
    103 #include <netinet/ip_var.h>
    104 
    105 #ifdef INET6
    106 #ifndef INET
    107 #include <netinet/in.h>
    108 #endif
    109 #include <netinet/ip6.h>
    110 #endif
    111 
    112 #include <netinet/tcp.h>
    113 #define TCPSTATES
    114 #include <netinet/tcp_fsm.h>
    115 #include <netinet/tcp_seq.h>
    116 #define	TCPTIMERS
    117 #include <netinet/tcp_timer.h>
    118 #include <netinet/tcp_var.h>
    119 #include <netinet/tcpip.h>
    120 #define	TANAMES
    121 #include <netinet/tcp_debug.h>
    122 
    123 #include <arpa/inet.h>
    124 
    125 #include <err.h>
    126 #include <stdio.h>
    127 #include <errno.h>
    128 #include <kvm.h>
    129 #include <nlist.h>
    130 #include <paths.h>
    131 #include <limits.h>
    132 #include <stdlib.h>
    133 #include <unistd.h>
    134 
    135 struct nlist nl[] = {
    136 #define	N_TCP_DEBUG	0
    137 	{ "_tcp_debug" },
    138 #define	N_TCP_DEBX	1
    139 	{ "_tcp_debx" },
    140 	{ NULL },
    141 };
    142 
    143 static caddr_t tcp_pcbs[TCP_NDEBUG];
    144 static n_time ntime;
    145 static int aflag, follow, sflag, tflag;
    146 
    147 int	main __P((int, char *[]));
    148 void	dotrace __P((caddr_t));
    149 void	tcp_trace __P((short, short, struct tcpcb *, struct tcpcb *,
    150 	    int, void *, int));
    151 int	numeric __P((const void *, const void *));
    152 void	usage __P((void));
    153 
    154 kvm_t	*kd;
    155 
    156 int
    157 main(argc, argv)
    158 	int argc;
    159 	char *argv[];
    160 {
    161 	int ch, i, jflag, npcbs;
    162 	char *system, *core, *cp, errbuf[_POSIX2_LINE_MAX];
    163 	gid_t egid = getegid();
    164 
    165 	(void)setegid(getgid());
    166 	system = core = NULL;
    167 
    168 	jflag = npcbs = 0;
    169 	while ((ch = getopt(argc, argv, "afjp:st")) != -1) {
    170 		switch (ch) {
    171 		case 'a':
    172 			++aflag;
    173 			break;
    174 		case 'f':
    175 			++follow;
    176 			setlinebuf(stdout);
    177 			break;
    178 		case 'j':
    179 			++jflag;
    180 			break;
    181 		case 'p':
    182 			if (npcbs >= TCP_NDEBUG)
    183 				errx(1, "too many pcbs specified");
    184 			errno = 0;
    185 			tcp_pcbs[npcbs++] = (caddr_t)strtoul(optarg, &cp, 16);
    186 			if (*cp != '\0' || errno == ERANGE)
    187 				errx(1, "invalid address: %s", optarg);
    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 		case '?':
    202 		default:
    203 			usage();
    204 			/* NOTREACHED */
    205 		}
    206 	}
    207 	argc -= optind;
    208 	argv += optind;
    209 
    210 	if (argc)
    211 		usage();
    212 
    213 	/*
    214 	 * Discard setgid privileges.  If not the running kernel, we toss
    215 	 * them away totally so that bad guys can't print interesting stuff
    216 	 * from kernel memory, otherwise switch back to kmem for the
    217 	 * duration of the kvm_openfiles() call.
    218 	 */
    219 	if (core != NULL || system != NULL)
    220 		setgid(getgid());
    221 	else
    222 		setegid(egid);
    223 
    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 	/* get rid of it now anyway */
    229 	if (core == NULL && system == NULL)
    230 		setgid(getgid());
    231 
    232 	if (kvm_nlist(kd, nl))
    233 		errx(2, "%s: no namelist", system ? system : _PATH_UNIX);
    234 
    235 	if (kvm_read(kd, nl[N_TCP_DEBX].n_value, (char *)&tcp_debx,
    236 	    sizeof(tcp_debx)) != sizeof(tcp_debx))
    237 		errx(3, "tcp_debx: %s", kvm_geterr(kd));
    238 
    239 	if (kvm_read(kd, nl[N_TCP_DEBUG].n_value, (char *)tcp_debug,
    240 	    sizeof(tcp_debug)) != sizeof(tcp_debug))
    241 		errx(3, "tcp_debug: %s", kvm_geterr(kd));
    242 
    243 	/*
    244 	 * If no control blocks have been specified, figure
    245 	 * out how many distinct one we have and summarize
    246 	 * them in tcp_pcbs for sorting the trace records
    247 	 * below.
    248 	 */
    249 	if (npcbs == 0) {
    250 		for (i = 0; i < TCP_NDEBUG; i++) {
    251 			struct tcp_debug *td = &tcp_debug[i];
    252 			int j;
    253 
    254 			if (td->td_tcb == 0)
    255 				continue;
    256 			for (j = 0; j < npcbs; j++)
    257 				if (tcp_pcbs[j] == td->td_tcb)
    258 					break;
    259 			if (j >= npcbs)
    260 				tcp_pcbs[npcbs++] = td->td_tcb;
    261 		}
    262 		if (npcbs == 0)
    263 			exit(0);
    264 	}
    265 	qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric);
    266 	if (jflag) {
    267 		for (i = 0;;) {
    268 			printf("%lx", (long)tcp_pcbs[i]);
    269 			if (++i == npcbs)
    270 				break;
    271 			fputs(", ", stdout);
    272 		}
    273 		putchar('\n');
    274 	} else {
    275 		for (i = 0; i < npcbs; i++) {
    276 			printf("\n%lx:\n", (long)tcp_pcbs[i]);
    277 			dotrace(tcp_pcbs[i]);
    278 		}
    279 	}
    280 	exit(0);
    281 }
    282 
    283 void
    284 dotrace(tcpcb)
    285 	caddr_t tcpcb;
    286 {
    287 	struct tcp_debug *td;
    288 	int prev_debx = tcp_debx;
    289 	int i;
    290 
    291  again:
    292 	if (--tcp_debx < 0)
    293 		tcp_debx = TCP_NDEBUG - 1;
    294 	for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
    295 		td = &tcp_debug[i];
    296 		if (tcpcb && td->td_tcb != tcpcb)
    297 			continue;
    298 		ntime = ntohl(td->td_time);
    299 		switch (td->td_family) {
    300 		case AF_INET:
    301 			tcp_trace(td->td_act, td->td_ostate,
    302 			    (struct tcpcb *)td->td_tcb, &td->td_cb,
    303 			    td->td_family, &td->td_ti, td->td_req);
    304 			break;
    305 #ifdef INET6
    306 		case AF_INET6:
    307 			tcp_trace(td->td_act, td->td_ostate,
    308 			    (struct tcpcb *)td->td_tcb, &td->td_cb,
    309 			    td->td_family, &td->td_ti6, td->td_req);
    310 			break;
    311 #endif
    312 		default:
    313 			tcp_trace(td->td_act, td->td_ostate,
    314 			    (struct tcpcb *)td->td_tcb, &td->td_cb,
    315 			    td->td_family, NULL, td->td_req);
    316 			break;
    317 		}
    318 		if (i == tcp_debx)
    319 			goto done;
    320 	}
    321 	for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) {
    322 		td = &tcp_debug[i];
    323 		if (tcpcb && td->td_tcb != tcpcb)
    324 			continue;
    325 		ntime = ntohl(td->td_time);
    326 		switch (td->td_family) {
    327 		case AF_INET:
    328 			tcp_trace(td->td_act, td->td_ostate,
    329 			    (struct tcpcb *)td->td_tcb, &td->td_cb,
    330 			    td->td_family, &td->td_ti, td->td_req);
    331 			break;
    332 #ifdef INET6
    333 		case AF_INET6:
    334 			tcp_trace(td->td_act, td->td_ostate,
    335 			    (struct tcpcb *)td->td_tcb, &td->td_cb,
    336 			    td->td_family, &td->td_ti6, td->td_req);
    337 			break;
    338 #endif
    339 		default:
    340 			tcp_trace(td->td_act, td->td_ostate,
    341 			    (struct tcpcb *)td->td_tcb, &td->td_cb,
    342 			    td->td_family, NULL, td->td_req);
    343 			break;
    344 		}
    345 	}
    346  done:
    347 	if (follow) {
    348 		prev_debx = tcp_debx + 1;
    349 		if (prev_debx >= TCP_NDEBUG)
    350 			prev_debx = 0;
    351 		do {
    352 			sleep(1);
    353 			if (kvm_read(kd, nl[N_TCP_DEBX].n_value,
    354 			    (char *)&tcp_debx, sizeof(tcp_debx)) !=
    355 			    sizeof(tcp_debx))
    356 				errx(3, "tcp_debx: %s", kvm_geterr(kd));
    357 		} while (tcp_debx == prev_debx);
    358 
    359 		if (kvm_read(kd, nl[N_TCP_DEBUG].n_value, (char *)tcp_debug,
    360 		    sizeof(tcp_debug)) != sizeof(tcp_debug))
    361 			errx(3, "tcp_debug: %s", kvm_geterr(kd));
    362 
    363 		goto again;
    364 	}
    365 }
    366 
    367 /*
    368  * Tcp debug routines
    369  */
    370 /*ARGSUSED*/
    371 void
    372 tcp_trace(act, ostate, atp, tp, family, packet, req)
    373 	short act, ostate;
    374 	struct tcpcb *atp, *tp;
    375 	int family;
    376 	void *packet;
    377 	int req;
    378 {
    379 	tcp_seq seq, ack;
    380 	int flags, len, win, timer;
    381 	struct tcphdr *th = NULL;
    382 	struct ip *ip = NULL;
    383 #ifdef INET6
    384 	struct ip6_hdr *ip6 = NULL;
    385 #endif
    386 	char hbuf[MAXHOSTNAMELEN];
    387 
    388 	switch (family) {
    389 	case AF_INET:
    390 		if (packet) {
    391 			ip = (struct ip *)packet;
    392 			th = (struct tcphdr *)(ip + 1);
    393 		}
    394 		break;
    395 #ifdef INET6
    396 	case AF_INET6:
    397 		if (packet) {
    398 			ip6 = (struct ip6_hdr *)packet;
    399 			th = (struct tcphdr *)(ip6 + 1);
    400 		}
    401 		break;
    402 #endif
    403 	default:
    404 		return;
    405 	}
    406 
    407 	printf("%03d %s:%s ", (ntime/10) % 1000, tcpstates[ostate],
    408 	    tanames[act]);
    409 
    410 #ifndef INET6
    411 	if (!ip)
    412 #else
    413 	if (!(ip || ip6))
    414 #endif
    415 		goto skipact;
    416 
    417 	switch (act) {
    418 	case TA_INPUT:
    419 	case TA_OUTPUT:
    420 	case TA_DROP:
    421 		if (aflag) {
    422 			inet_ntop(family,
    423 #ifndef INET6
    424 				(void *)&ip->ip_src,
    425 #else
    426 				family == AF_INET ? (void *)&ip->ip_src
    427 						  : (void *)&ip6->ip6_src,
    428 #endif
    429 				hbuf, sizeof(hbuf));
    430 			printf("(src=%s,%u, ",
    431 			    hbuf, ntohs(th->th_sport));
    432 			inet_ntop(family,
    433 #ifndef INET6
    434 				(void *)&ip->ip_dst,
    435 #else
    436 				family == AF_INET ? (void *)&ip->ip_dst
    437 						  : (void *)&ip6->ip6_dst,
    438 #endif
    439 				hbuf, sizeof(hbuf));
    440 			printf("dst=%s,%u)",
    441 			    hbuf, ntohs(th->th_dport));
    442 		}
    443 		seq = th->th_seq;
    444 		ack = th->th_ack;
    445 		if (ip)
    446 			len = ip->ip_len;
    447 #ifdef INET6
    448 		else if (ip6)
    449 			len = ip6->ip6_plen;
    450 #endif
    451 		win = th->th_win;
    452 		if (act == TA_OUTPUT) {
    453 			NTOHL(seq);
    454 			NTOHL(ack);
    455 			NTOHS(len);
    456 			NTOHS(win);
    457 		}
    458 		if (act == TA_OUTPUT)
    459 			len -= sizeof(struct tcphdr);
    460 		if (len)
    461 			printf("[%x..%x)", seq, seq + len);
    462 		else
    463 			printf("%x", seq);
    464 		printf("@%x", ack);
    465 		if (win)
    466 			printf("(win=%x)", win);
    467 		flags = th->th_flags;
    468 		if (flags) {
    469 			register char *cp = "<";
    470 #define	pf(flag, string) { \
    471 	if (th->th_flags&flag) { \
    472 		(void)printf("%s%s", cp, string); \
    473 		cp = ","; \
    474 	} \
    475 }
    476 			pf(TH_SYN, "SYN");
    477 			pf(TH_ACK, "ACK");
    478 			pf(TH_FIN, "FIN");
    479 			pf(TH_RST, "RST");
    480 			pf(TH_PUSH, "PUSH");
    481 			pf(TH_URG, "URG");
    482 			printf(">");
    483 		}
    484 		break;
    485 	case TA_USER:
    486 		timer = req >> 8;
    487 		req &= 0xff;
    488 		printf("%s", prurequests[req]);
    489 		if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
    490 			printf("<%s>", tcptimers[timer]);
    491 		break;
    492 	}
    493 
    494 skipact:
    495 	printf(" -> %s", tcpstates[tp->t_state]);
    496 	/* print out internal state of tp !?! */
    497 	printf("\n");
    498 	if (sflag) {
    499 		printf("\trcv_nxt %x rcv_wnd %lx snd_una %x snd_nxt %x snd_max %x\n",
    500 		    tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt,
    501 		    tp->snd_max);
    502 		printf("\tsnd_wl1 %x snd_wl2 %x snd_wnd %lx\n", tp->snd_wl1,
    503 		    tp->snd_wl2, tp->snd_wnd);
    504 	}
    505 	/* print out timers? */
    506 	if (tflag) {
    507 		register char *cp = "\t";
    508 		register int i;
    509 
    510 		for (i = 0; i < TCPT_NTIMERS; i++) {
    511 			if (tp->t_timer[i] == 0)
    512 				continue;
    513 			printf("%s%s=%d", cp, tcptimers[i], tp->t_timer[i]);
    514 			if (i == TCPT_REXMT)
    515 				printf(" (t_rxtshft=%d)", tp->t_rxtshift);
    516 			cp = ", ";
    517 		}
    518 		if (*cp != '\t')
    519 			putchar('\n');
    520 	}
    521 }
    522 
    523 int
    524 numeric(v1, v2)
    525 	const void *v1, *v2;
    526 {
    527 	const caddr_t *c1 = v1;
    528 	const caddr_t *c2 = v2;
    529 	int rv;
    530 
    531 	if (*c1 < *c2)
    532 		rv = -1;
    533 	else if (*c1 > *c2)
    534 		rv = 1;
    535 	else
    536 		rv = 0;
    537 
    538 	return (rv);
    539 }
    540 
    541 void
    542 usage()
    543 {
    544 
    545 	(void) fprintf(stderr, "usage: %s [-afjst] [-p hex-address]"
    546 	    " [-N system] [-M core]\n", getprogname());
    547 	exit(1);
    548 }
    549