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