Home | History | Annotate | Line # | Download | only in trpt
trpt.c revision 1.9
      1 /*	$NetBSD: trpt.c,v 1.9 1999/07/01 19:15:03 itojun 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.9 1999/07/01 19:15:03 itojun 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 extern	char *__progname;
    148 
    149 int	main __P((int, char *[]));
    150 void	dotrace __P((caddr_t));
    151 void	tcp_trace __P((short, short, struct tcpcb *, struct tcpcb *,
    152 	    int, void *, int));
    153 int	numeric __P((const void *, const void *));
    154 void	usage __P((void));
    155 
    156 kvm_t	*kd;
    157 
    158 int
    159 main(argc, argv)
    160 	int argc;
    161 	char *argv[];
    162 {
    163 	int ch, i, jflag, npcbs;
    164 	char *system, *core, *cp, errbuf[_POSIX2_LINE_MAX];
    165 	gid_t egid = getegid();
    166 
    167 	(void)setegid(getgid());
    168 	system = core = NULL;
    169 
    170 	jflag = npcbs = 0;
    171 	while ((ch = getopt(argc, argv, "afjp:st")) != -1) {
    172 		switch (ch) {
    173 		case 'a':
    174 			++aflag;
    175 			break;
    176 		case 'f':
    177 			++follow;
    178 			setlinebuf(stdout);
    179 			break;
    180 		case 'j':
    181 			++jflag;
    182 			break;
    183 		case 'p':
    184 			if (npcbs >= TCP_NDEBUG)
    185 				errx(1, "too many pcbs specified");
    186 			errno = 0;
    187 			tcp_pcbs[npcbs++] = (caddr_t)strtoul(optarg, &cp, 16);
    188 			if (*cp != '\0' || errno == ERANGE)
    189 				errx(1, "invalid address: %s", optarg);
    190 			break;
    191 		case 's':
    192 			++sflag;
    193 			break;
    194 		case 't':
    195 			++tflag;
    196 			break;
    197 		case 'N':
    198 			system = optarg;
    199 			break;
    200 		case 'M':
    201 			core = optarg;
    202 			break;
    203 		case '?':
    204 		default:
    205 			usage();
    206 			/* NOTREACHED */
    207 		}
    208 	}
    209 	argc -= optind;
    210 	argv += optind;
    211 
    212 	if (argc)
    213 		usage();
    214 
    215 	/*
    216 	 * Discard setgid privileges.  If not the running kernel, we toss
    217 	 * them away totally so that bad guys can't print interesting stuff
    218 	 * from kernel memory, otherwise switch back to kmem for the
    219 	 * duration of the kvm_openfiles() call.
    220 	 */
    221 	if (core != NULL || system != NULL)
    222 		setgid(getgid());
    223 	else
    224 		setegid(egid);
    225 
    226 	kd = kvm_openfiles(system, core, NULL, O_RDONLY, errbuf);
    227 	if (kd == NULL)
    228 		errx(1, "can't open kmem: %s", errbuf);
    229 
    230 	/* get rid of it now anyway */
    231 	if (core == NULL && system == NULL)
    232 		setgid(getgid());
    233 
    234 	if (kvm_nlist(kd, nl))
    235 		errx(2, "%s: no namelist", system ? system : _PATH_UNIX);
    236 
    237 	if (kvm_read(kd, nl[N_TCP_DEBX].n_value, (char *)&tcp_debx,
    238 	    sizeof(tcp_debx)) != sizeof(tcp_debx))
    239 		errx(3, "tcp_debx: %s", kvm_geterr(kd));
    240 
    241 	if (kvm_read(kd, nl[N_TCP_DEBUG].n_value, (char *)tcp_debug,
    242 	    sizeof(tcp_debug)) != sizeof(tcp_debug))
    243 		errx(3, "tcp_debug: %s", kvm_geterr(kd));
    244 
    245 	/*
    246 	 * If no control blocks have been specified, figure
    247 	 * out how many distinct one we have and summarize
    248 	 * them in tcp_pcbs for sorting the trace records
    249 	 * below.
    250 	 */
    251 	if (npcbs == 0) {
    252 		for (i = 0; i < TCP_NDEBUG; i++) {
    253 			struct tcp_debug *td = &tcp_debug[i];
    254 			int j;
    255 
    256 			if (td->td_tcb == 0)
    257 				continue;
    258 			for (j = 0; j < npcbs; j++)
    259 				if (tcp_pcbs[j] == td->td_tcb)
    260 					break;
    261 			if (j >= npcbs)
    262 				tcp_pcbs[npcbs++] = td->td_tcb;
    263 		}
    264 		if (npcbs == 0)
    265 			exit(0);
    266 	}
    267 	qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric);
    268 	if (jflag) {
    269 		for (i = 0;;) {
    270 			printf("%lx", (long)tcp_pcbs[i]);
    271 			if (++i == npcbs)
    272 				break;
    273 			fputs(", ", stdout);
    274 		}
    275 		putchar('\n');
    276 	} else {
    277 		for (i = 0; i < npcbs; i++) {
    278 			printf("\n%lx:\n", (long)tcp_pcbs[i]);
    279 			dotrace(tcp_pcbs[i]);
    280 		}
    281 	}
    282 	exit(0);
    283 }
    284 
    285 void
    286 dotrace(tcpcb)
    287 	caddr_t tcpcb;
    288 {
    289 	struct tcp_debug *td;
    290 	int prev_debx = tcp_debx;
    291 	int i;
    292 
    293  again:
    294 	if (--tcp_debx < 0)
    295 		tcp_debx = TCP_NDEBUG - 1;
    296 	for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
    297 		td = &tcp_debug[i];
    298 		if (tcpcb && td->td_tcb != tcpcb)
    299 			continue;
    300 		ntime = ntohl(td->td_time);
    301 		switch (td->td_family) {
    302 		case AF_INET:
    303 			tcp_trace(td->td_act, td->td_ostate,
    304 			    (struct tcpcb *)td->td_tcb, &td->td_cb,
    305 			    td->td_family, &td->td_ti, td->td_req);
    306 			break;
    307 #ifdef INET6
    308 		case AF_INET6:
    309 			tcp_trace(td->td_act, td->td_ostate,
    310 			    (struct tcpcb *)td->td_tcb, &td->td_cb,
    311 			    td->td_family, &td->td_ti6, td->td_req);
    312 			break;
    313 #endif
    314 		default:
    315 			tcp_trace(td->td_act, td->td_ostate,
    316 			    (struct tcpcb *)td->td_tcb, &td->td_cb,
    317 			    td->td_family, NULL, td->td_req);
    318 			break;
    319 		}
    320 		if (i == tcp_debx)
    321 			goto done;
    322 	}
    323 	for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) {
    324 		td = &tcp_debug[i];
    325 		if (tcpcb && td->td_tcb != tcpcb)
    326 			continue;
    327 		ntime = ntohl(td->td_time);
    328 		switch (td->td_family) {
    329 		case AF_INET:
    330 			tcp_trace(td->td_act, td->td_ostate,
    331 			    (struct tcpcb *)td->td_tcb, &td->td_cb,
    332 			    td->td_family, &td->td_ti, td->td_req);
    333 			break;
    334 #ifdef INET6
    335 		case AF_INET6:
    336 			tcp_trace(td->td_act, td->td_ostate,
    337 			    (struct tcpcb *)td->td_tcb, &td->td_cb,
    338 			    td->td_family, &td->td_ti6, td->td_req);
    339 			break;
    340 #endif
    341 		default:
    342 			tcp_trace(td->td_act, td->td_ostate,
    343 			    (struct tcpcb *)td->td_tcb, &td->td_cb,
    344 			    td->td_family, NULL, td->td_req);
    345 			break;
    346 		}
    347 	}
    348  done:
    349 	if (follow) {
    350 		prev_debx = tcp_debx + 1;
    351 		if (prev_debx >= TCP_NDEBUG)
    352 			prev_debx = 0;
    353 		do {
    354 			sleep(1);
    355 			if (kvm_read(kd, nl[N_TCP_DEBX].n_value,
    356 			    (char *)&tcp_debx, sizeof(tcp_debx)) !=
    357 			    sizeof(tcp_debx))
    358 				errx(3, "tcp_debx: %s", kvm_geterr(kd));
    359 		} while (tcp_debx == prev_debx);
    360 
    361 		if (kvm_read(kd, nl[N_TCP_DEBUG].n_value, (char *)tcp_debug,
    362 		    sizeof(tcp_debug)) != sizeof(tcp_debug))
    363 			errx(3, "tcp_debug: %s", kvm_geterr(kd));
    364 
    365 		goto again;
    366 	}
    367 }
    368 
    369 /*
    370  * Tcp debug routines
    371  */
    372 /*ARGSUSED*/
    373 void
    374 tcp_trace(act, ostate, atp, tp, family, packet, req)
    375 	short act, ostate;
    376 	struct tcpcb *atp, *tp;
    377 	int family;
    378 	void *packet;
    379 	int req;
    380 {
    381 	tcp_seq seq, ack;
    382 	int flags, len, win, timer;
    383 	struct tcphdr *th = NULL;
    384 	struct ip *ip = NULL;
    385 #ifdef INET6
    386 	struct ip6_hdr *ip6 = NULL;
    387 #endif
    388 	char hbuf[MAXHOSTNAMELEN];
    389 
    390 	switch (family) {
    391 	case AF_INET:
    392 		if (packet) {
    393 			ip = (struct ip *)packet;
    394 			th = (struct tcphdr *)(ip + 1);
    395 		}
    396 		break;
    397 #ifdef INET6
    398 	case AF_INET6:
    399 		if (packet) {
    400 			ip6 = (struct ip6_hdr *)packet;
    401 			th = (struct tcphdr *)(ip6 + 1);
    402 		}
    403 		break;
    404 #endif
    405 	default:
    406 		return;
    407 	}
    408 
    409 	printf("%03d %s:%s ", (ntime/10) % 1000, tcpstates[ostate],
    410 	    tanames[act]);
    411 
    412 #ifndef INET6
    413 	if (!ip)
    414 #else
    415 	if (!(ip || ip6))
    416 #endif
    417 		goto skipact;
    418 
    419 	switch (act) {
    420 	case TA_INPUT:
    421 	case TA_OUTPUT:
    422 	case TA_DROP:
    423 		if (aflag) {
    424 			inet_ntop(family,
    425 #ifndef INET6
    426 				(void *)&ip->ip_src,
    427 #else
    428 				family == AF_INET ? (void *)&ip->ip_src
    429 						  : (void *)&ip6->ip6_src,
    430 #endif
    431 				hbuf, sizeof(hbuf));
    432 			printf("(src=%s,%u, ",
    433 			    hbuf, ntohs(th->th_sport));
    434 			inet_ntop(family,
    435 #ifndef INET6
    436 				(void *)&ip->ip_dst,
    437 #else
    438 				family == AF_INET ? (void *)&ip->ip_dst
    439 						  : (void *)&ip6->ip6_dst,
    440 #endif
    441 				hbuf, sizeof(hbuf));
    442 			printf("dst=%s,%u)",
    443 			    hbuf, ntohs(th->th_dport));
    444 		}
    445 		seq = th->th_seq;
    446 		ack = th->th_ack;
    447 		if (ip)
    448 			len = ip->ip_len;
    449 #ifdef INET6
    450 		else if (ip6)
    451 			len = ip6->ip6_plen;
    452 #endif
    453 		win = th->th_win;
    454 		if (act == TA_OUTPUT) {
    455 			NTOHL(seq);
    456 			NTOHL(ack);
    457 			NTOHS(len);
    458 			NTOHS(win);
    459 		}
    460 		if (act == TA_OUTPUT)
    461 			len -= sizeof(struct tcphdr);
    462 		if (len)
    463 			printf("[%x..%x)", seq, seq + len);
    464 		else
    465 			printf("%x", seq);
    466 		printf("@%x", ack);
    467 		if (win)
    468 			printf("(win=%x)", win);
    469 		flags = th->th_flags;
    470 		if (flags) {
    471 			register char *cp = "<";
    472 #define	pf(flag, string) { \
    473 	if (th->th_flags&flag) { \
    474 		(void)printf("%s%s", cp, string); \
    475 		cp = ","; \
    476 	} \
    477 }
    478 			pf(TH_SYN, "SYN");
    479 			pf(TH_ACK, "ACK");
    480 			pf(TH_FIN, "FIN");
    481 			pf(TH_RST, "RST");
    482 			pf(TH_PUSH, "PUSH");
    483 			pf(TH_URG, "URG");
    484 			printf(">");
    485 		}
    486 		break;
    487 	case TA_USER:
    488 		timer = req >> 8;
    489 		req &= 0xff;
    490 		printf("%s", prurequests[req]);
    491 		if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
    492 			printf("<%s>", tcptimers[timer]);
    493 		break;
    494 	}
    495 
    496 skipact:
    497 	printf(" -> %s", tcpstates[tp->t_state]);
    498 	/* print out internal state of tp !?! */
    499 	printf("\n");
    500 	if (sflag) {
    501 		printf("\trcv_nxt %x rcv_wnd %lx snd_una %x snd_nxt %x snd_max %x\n",
    502 		    tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt,
    503 		    tp->snd_max);
    504 		printf("\tsnd_wl1 %x snd_wl2 %x snd_wnd %lx\n", tp->snd_wl1,
    505 		    tp->snd_wl2, tp->snd_wnd);
    506 	}
    507 	/* print out timers? */
    508 	if (tflag) {
    509 		register char *cp = "\t";
    510 		register int i;
    511 
    512 		for (i = 0; i < TCPT_NTIMERS; i++) {
    513 			if (tp->t_timer[i] == 0)
    514 				continue;
    515 			printf("%s%s=%d", cp, tcptimers[i], tp->t_timer[i]);
    516 			if (i == TCPT_REXMT)
    517 				printf(" (t_rxtshft=%d)", tp->t_rxtshift);
    518 			cp = ", ";
    519 		}
    520 		if (*cp != '\t')
    521 			putchar('\n');
    522 	}
    523 }
    524 
    525 int
    526 numeric(v1, v2)
    527 	const void *v1, *v2;
    528 {
    529 	const caddr_t *c1 = v1;
    530 	const caddr_t *c2 = v2;
    531 	int rv;
    532 
    533 	if (*c1 < *c2)
    534 		rv = -1;
    535 	else if (*c1 > *c2)
    536 		rv = 1;
    537 	else
    538 		rv = 0;
    539 
    540 	return (rv);
    541 }
    542 
    543 void
    544 usage()
    545 {
    546 
    547 	(void) fprintf(stderr, "usage: %s [-afjst] [-p hex-address]"
    548 	    " [-N system] [-M core]\n", __progname);
    549 	exit(1);
    550 }
    551