Home | History | Annotate | Line # | Download | only in trpt
trpt.c revision 1.5
      1 /*	$NetBSD: trpt.c,v 1.5 1997/07/23 08:20:02 thorpej 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.5 1997/07/23 08:20:02 thorpej 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 #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_TCP_DEBUG	0
    129 	{ "_tcp_debug" },
    130 #define	N_TCP_DEBX	1
    131 	{ "_tcp_debx" },
    132 	{ NULL },
    133 };
    134 
    135 static caddr_t tcp_pcbs[TCP_NDEBUG];
    136 static n_time ntime;
    137 static int aflag, follow, sflag, tflag;
    138 
    139 extern	char *__progname;
    140 
    141 int	main __P((int, char *[]));
    142 void	dotrace __P((caddr_t));
    143 void	tcp_trace __P((short, short, struct tcpcb *, struct tcpcb *,
    144 	    struct tcpiphdr *, int));
    145 int	numeric __P((const void *, const void *));
    146 void	usage __P((void));
    147 
    148 kvm_t	*kd;
    149 
    150 int
    151 main(argc, argv)
    152 	int argc;
    153 	char *argv[];
    154 {
    155 	int ch, i, jflag, npcbs;
    156 	char *system, *core, *cp, errbuf[_POSIX2_LINE_MAX];
    157 
    158 	system = core = NULL;
    159 
    160 	jflag = npcbs = 0;
    161 	while ((ch = getopt(argc, argv, "afjp:st")) != -1) {
    162 		switch (ch) {
    163 		case 'a':
    164 			++aflag;
    165 			break;
    166 		case 'f':
    167 			++follow;
    168 			setlinebuf(stdout);
    169 			break;
    170 		case 'j':
    171 			++jflag;
    172 			break;
    173 		case 'p':
    174 			if (npcbs >= TCP_NDEBUG)
    175 				errx(1, "too many pcbs specified");
    176 			errno = 0;
    177 			tcp_pcbs[npcbs++] = (caddr_t)strtoul(optarg, &cp, 16);
    178 			if (*cp != '\0' || errno == ERANGE)
    179 				errx(1, "invalid address: %s", optarg);
    180 			break;
    181 		case 's':
    182 			++sflag;
    183 			break;
    184 		case 't':
    185 			++tflag;
    186 			break;
    187 		case 'N':
    188 			system = optarg;
    189 			break;
    190 		case 'M':
    191 			core = optarg;
    192 			break;
    193 		case '?':
    194 		default:
    195 			usage();
    196 			/* NOTREACHED */
    197 		}
    198 	}
    199 	argc -= optind;
    200 	argv += optind;
    201 
    202 	if (argc)
    203 		usage();
    204 
    205 	/*
    206 	 * Discard setgid privileged if not the running kernel so that bad
    207 	 * guys can't print interesting stuff from kernel memory.
    208 	 */
    209 	if (core != NULL || system != NULL)
    210 		setgid(getgid());
    211 
    212 	kd = kvm_openfiles(system, core, NULL, O_RDONLY, errbuf);
    213 	if (kd == NULL)
    214 		errx(1, "can't open kmem: %s", errbuf);
    215 
    216 	if (kvm_nlist(kd, nl))
    217 		errx(2, "%s: no namelist", system ? system : _PATH_UNIX);
    218 
    219 	if (kvm_read(kd, nl[N_TCP_DEBX].n_value, (char *)&tcp_debx,
    220 	    sizeof(tcp_debx)) != sizeof(tcp_debx))
    221 		errx(3, "tcp_debx: %s", kvm_geterr(kd));
    222 
    223 	if (kvm_read(kd, nl[N_TCP_DEBUG].n_value, (char *)tcp_debug,
    224 	    sizeof(tcp_debug)) != sizeof(tcp_debug))
    225 		errx(3, "tcp_debug: %s", kvm_geterr(kd));
    226 
    227 	/*
    228 	 * If no control blocks have been specified, figure
    229 	 * out how many distinct one we have and summarize
    230 	 * them in tcp_pcbs for sorting the trace records
    231 	 * below.
    232 	 */
    233 	if (npcbs == 0) {
    234 		for (i = 0; i < TCP_NDEBUG; i++) {
    235 			struct tcp_debug *td = &tcp_debug[i];
    236 			int j;
    237 
    238 			if (td->td_tcb == 0)
    239 				continue;
    240 			for (j = 0; j < npcbs; j++)
    241 				if (tcp_pcbs[j] == td->td_tcb)
    242 					break;
    243 			if (j >= npcbs)
    244 				tcp_pcbs[npcbs++] = td->td_tcb;
    245 		}
    246 		if (npcbs == 0)
    247 			exit(0);
    248 	}
    249 	qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric);
    250 	if (jflag) {
    251 		for (i = 0;;) {
    252 			printf("%lx", (long)tcp_pcbs[i]);
    253 			if (++i == npcbs)
    254 				break;
    255 			fputs(", ", stdout);
    256 		}
    257 		putchar('\n');
    258 	} else {
    259 		for (i = 0; i < npcbs; i++) {
    260 			printf("\n%lx:\n", (long)tcp_pcbs[i]);
    261 			dotrace(tcp_pcbs[i]);
    262 		}
    263 	}
    264 	exit(0);
    265 }
    266 
    267 void
    268 dotrace(tcpcb)
    269 	caddr_t tcpcb;
    270 {
    271 	struct tcp_debug *td;
    272 	int prev_debx = tcp_debx;
    273 	int i;
    274 
    275  again:
    276 	if (--tcp_debx < 0)
    277 		tcp_debx = TCP_NDEBUG - 1;
    278 	for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
    279 		td = &tcp_debug[i];
    280 		if (tcpcb && td->td_tcb != tcpcb)
    281 			continue;
    282 		ntime = ntohl(td->td_time);
    283 		tcp_trace(td->td_act, td->td_ostate,
    284 		    (struct tcpcb *)td->td_tcb, &td->td_cb, &td->td_ti,
    285 		    td->td_req);
    286 		if (i == tcp_debx)
    287 			goto done;
    288 	}
    289 	for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) {
    290 		td = &tcp_debug[i];
    291 		if (tcpcb && td->td_tcb != tcpcb)
    292 			continue;
    293 		ntime = ntohl(td->td_time);
    294 		tcp_trace(td->td_act, td->td_ostate,
    295 		    (struct tcpcb *)td->td_tcb, &td->td_cb, &td->td_ti,
    296 		    td->td_req);
    297 	}
    298  done:
    299 	if (follow) {
    300 		prev_debx = tcp_debx + 1;
    301 		if (prev_debx >= TCP_NDEBUG)
    302 			prev_debx = 0;
    303 		do {
    304 			sleep(1);
    305 			if (kvm_read(kd, nl[N_TCP_DEBX].n_value,
    306 			    (char *)&tcp_debx, sizeof(tcp_debx)) !=
    307 			    sizeof(tcp_debx))
    308 				errx(3, "tcp_debx: %s", kvm_geterr(kd));
    309 		} while (tcp_debx == prev_debx);
    310 
    311 		if (kvm_read(kd, nl[N_TCP_DEBUG].n_value, (char *)tcp_debug,
    312 		    sizeof(tcp_debug)) != sizeof(tcp_debug))
    313 			errx(3, "tcp_debug: %s", kvm_geterr(kd));
    314 
    315 		goto again;
    316 	}
    317 }
    318 
    319 /*
    320  * Tcp debug routines
    321  */
    322 /*ARGSUSED*/
    323 void
    324 tcp_trace(act, ostate, atp, tp, ti, req)
    325 	short act, ostate;
    326 	struct tcpcb *atp, *tp;
    327 	struct tcpiphdr *ti;
    328 	int req;
    329 {
    330 	tcp_seq seq, ack;
    331 	int flags, len, win, timer;
    332 
    333 	printf("%03d %s:%s ", (ntime/10) % 1000, tcpstates[ostate],
    334 	    tanames[act]);
    335 	switch (act) {
    336 	case TA_INPUT:
    337 	case TA_OUTPUT:
    338 	case TA_DROP:
    339 		if (aflag) {
    340 			printf("(src=%s,%u, ",
    341 			    inet_ntoa(ti->ti_src), ntohs(ti->ti_sport));
    342 			printf("dst=%s,%u)",
    343 			    inet_ntoa(ti->ti_dst), ntohs(ti->ti_dport));
    344 		}
    345 		seq = ti->ti_seq;
    346 		ack = ti->ti_ack;
    347 		len = ti->ti_len;
    348 		win = ti->ti_win;
    349 		if (act == TA_OUTPUT) {
    350 			NTOHL(seq);
    351 			NTOHL(ack);
    352 			NTOHS(len);
    353 			NTOHS(win);
    354 		}
    355 		if (act == TA_OUTPUT)
    356 			len -= sizeof(struct tcphdr);
    357 		if (len)
    358 			printf("[%x..%x)", seq, seq + len);
    359 		else
    360 			printf("%x", seq);
    361 		printf("@%x", ack);
    362 		if (win)
    363 			printf("(win=%x)", win);
    364 		flags = ti->ti_flags;
    365 		if (flags) {
    366 			register char *cp = "<";
    367 #define	pf(flag, string) { \
    368 	if (ti->ti_flags&flag) { \
    369 		(void)printf("%s%s", cp, string); \
    370 		cp = ","; \
    371 	} \
    372 }
    373 			pf(TH_SYN, "SYN");
    374 			pf(TH_ACK, "ACK");
    375 			pf(TH_FIN, "FIN");
    376 			pf(TH_RST, "RST");
    377 			pf(TH_PUSH, "PUSH");
    378 			pf(TH_URG, "URG");
    379 			printf(">");
    380 		}
    381 		break;
    382 	case TA_USER:
    383 		timer = req >> 8;
    384 		req &= 0xff;
    385 		printf("%s", prurequests[req]);
    386 		if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
    387 			printf("<%s>", tcptimers[timer]);
    388 		break;
    389 	}
    390 	printf(" -> %s", tcpstates[tp->t_state]);
    391 	/* print out internal state of tp !?! */
    392 	printf("\n");
    393 	if (sflag) {
    394 		printf("\trcv_nxt %x rcv_wnd %lx snd_una %x snd_nxt %x snd_max %x\n",
    395 		    tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt,
    396 		    tp->snd_max);
    397 		printf("\tsnd_wl1 %x snd_wl2 %x snd_wnd %lx\n", tp->snd_wl1,
    398 		    tp->snd_wl2, tp->snd_wnd);
    399 	}
    400 	/* print out timers? */
    401 	if (tflag) {
    402 		register char *cp = "\t";
    403 		register int i;
    404 
    405 		for (i = 0; i < TCPT_NTIMERS; i++) {
    406 			if (tp->t_timer[i] == 0)
    407 				continue;
    408 			printf("%s%s=%d", cp, tcptimers[i], tp->t_timer[i]);
    409 			if (i == TCPT_REXMT)
    410 				printf(" (t_rxtshft=%d)", tp->t_rxtshift);
    411 			cp = ", ";
    412 		}
    413 		if (*cp != '\t')
    414 			putchar('\n');
    415 	}
    416 }
    417 
    418 int
    419 numeric(v1, v2)
    420 	const void *v1, *v2;
    421 {
    422 
    423 	return(*(const char *)v1 - *(const char *)v2);
    424 }
    425 
    426 void
    427 usage()
    428 {
    429 
    430 	(void) fprintf(stderr, "usage: %s [-afjst] [-p hex-address]"
    431 	    " [-N system] [-M core]\n", __progname);
    432 	exit(1);
    433 }
    434