Home | History | Annotate | Line # | Download | only in trpt
trpt.c revision 1.2
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1983, 1988 Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  */
     33  1.1      cgd 
     34  1.1      cgd #ifndef lint
     35  1.1      cgd char copyright[] =
     36  1.1      cgd "@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
     37  1.1      cgd  All rights reserved.\n";
     38  1.1      cgd #endif /* not lint */
     39  1.1      cgd 
     40  1.1      cgd #ifndef lint
     41  1.2  mycroft /*static char sccsid[] = "from: @(#)trpt.c	5.14 (Berkeley) 7/1/91";*/
     42  1.2  mycroft static char rcsid[] = "$Id: trpt.c,v 1.2 1993/08/01 17:54:59 mycroft Exp $";
     43  1.1      cgd #endif /* not lint */
     44  1.1      cgd 
     45  1.1      cgd #include <sys/param.h>
     46  1.1      cgd #if BSD >= 199103
     47  1.1      cgd #define NEWVM
     48  1.1      cgd #endif
     49  1.1      cgd #ifndef NEWVM
     50  1.1      cgd #include <machine/pte.h>
     51  1.1      cgd #include <sys/vmmac.h>
     52  1.1      cgd #endif
     53  1.1      cgd #include <sys/socket.h>
     54  1.1      cgd #include <sys/socketvar.h>
     55  1.1      cgd #define PRUREQUESTS
     56  1.1      cgd #include <sys/protosw.h>
     57  1.1      cgd #include <sys/file.h>
     58  1.1      cgd 
     59  1.1      cgd #include <net/route.h>
     60  1.1      cgd #include <net/if.h>
     61  1.1      cgd 
     62  1.1      cgd #include <netinet/in.h>
     63  1.1      cgd #include <netinet/in_systm.h>
     64  1.1      cgd #include <netinet/ip.h>
     65  1.1      cgd #include <netinet/in_pcb.h>
     66  1.1      cgd #include <netinet/ip_var.h>
     67  1.1      cgd #include <netinet/tcp.h>
     68  1.1      cgd #define TCPSTATES
     69  1.1      cgd #include <netinet/tcp_fsm.h>
     70  1.1      cgd #include <netinet/tcp_seq.h>
     71  1.1      cgd #define	TCPTIMERS
     72  1.1      cgd #include <netinet/tcp_timer.h>
     73  1.1      cgd #include <netinet/tcp_var.h>
     74  1.1      cgd #include <netinet/tcpip.h>
     75  1.1      cgd #define	TANAMES
     76  1.1      cgd #include <netinet/tcp_debug.h>
     77  1.1      cgd 
     78  1.1      cgd #include <arpa/inet.h>
     79  1.1      cgd 
     80  1.1      cgd #include <stdio.h>
     81  1.1      cgd #include <errno.h>
     82  1.1      cgd #include <nlist.h>
     83  1.1      cgd #include <paths.h>
     84  1.1      cgd 
     85  1.1      cgd struct nlist nl[] = {
     86  1.1      cgd #define	N_TCP_DEBUG	0
     87  1.1      cgd 	{ "_tcp_debug" },
     88  1.1      cgd #define	N_TCP_DEBX	1
     89  1.1      cgd 	{ "_tcp_debx" },
     90  1.1      cgd #ifndef NEWVM
     91  1.1      cgd #define	N_SYSMAP	2
     92  1.1      cgd 	{ "_Sysmap" },
     93  1.1      cgd #define	N_SYSSIZE	3
     94  1.1      cgd 	{ "_Syssize" },
     95  1.1      cgd #endif
     96  1.1      cgd 	{ "" },
     97  1.1      cgd };
     98  1.1      cgd 
     99  1.1      cgd #ifndef NEWVM
    100  1.1      cgd static struct pte *Sysmap;
    101  1.1      cgd #endif
    102  1.1      cgd static caddr_t tcp_pcbs[TCP_NDEBUG];
    103  1.1      cgd static n_time ntime;
    104  1.1      cgd static int aflag, kflag, memf, follow, sflag, tflag;
    105  1.1      cgd 
    106  1.1      cgd main(argc, argv)
    107  1.1      cgd 	int argc;
    108  1.1      cgd 	char **argv;
    109  1.1      cgd {
    110  1.1      cgd 	extern char *optarg;
    111  1.1      cgd 	extern int optind;
    112  1.1      cgd 	int ch, i, jflag, npcbs, numeric();
    113  1.1      cgd 	char *system, *core, *malloc();
    114  1.1      cgd 	off_t lseek();
    115  1.1      cgd 
    116  1.1      cgd 	jflag = npcbs = 0;
    117  1.1      cgd 	while ((ch = getopt(argc, argv, "afjp:st")) != EOF)
    118  1.1      cgd 		switch (ch) {
    119  1.1      cgd 		case 'a':
    120  1.1      cgd 			++aflag;
    121  1.1      cgd 			break;
    122  1.1      cgd 		case 'f':
    123  1.1      cgd 			++follow;
    124  1.1      cgd 			setlinebuf(stdout);
    125  1.1      cgd 			break;
    126  1.1      cgd 		case 'j':
    127  1.1      cgd 			++jflag;
    128  1.1      cgd 			break;
    129  1.1      cgd 		case 'p':
    130  1.1      cgd 			if (npcbs >= TCP_NDEBUG) {
    131  1.1      cgd 				fputs("trpt: too many pcb's specified\n",
    132  1.1      cgd 				    stderr);
    133  1.1      cgd 				exit(1);
    134  1.1      cgd 			}
    135  1.1      cgd 			(void)sscanf(optarg, "%x", (int *)&tcp_pcbs[npcbs++]);
    136  1.1      cgd 			break;
    137  1.1      cgd 		case 's':
    138  1.1      cgd 			++sflag;
    139  1.1      cgd 			break;
    140  1.1      cgd 		case 't':
    141  1.1      cgd 			++tflag;
    142  1.1      cgd 			break;
    143  1.1      cgd 		case '?':
    144  1.1      cgd 		default:
    145  1.1      cgd 			(void)fprintf(stderr,
    146  1.1      cgd "usage: trpt [-afjst] [-p hex-address] [system [core]]\n");
    147  1.1      cgd 			exit(1);
    148  1.1      cgd 		}
    149  1.1      cgd 	argc -= optind;
    150  1.1      cgd 	argv += optind;
    151  1.1      cgd 
    152  1.1      cgd 	core = _PATH_KMEM;
    153  1.1      cgd 	if (argc > 0) {
    154  1.1      cgd 		system = *argv;
    155  1.1      cgd 		argc--, argv++;
    156  1.1      cgd 		if (argc > 0) {
    157  1.1      cgd 			core = *argv;
    158  1.1      cgd 			argc--, argv++;
    159  1.1      cgd 			++kflag;
    160  1.1      cgd 		}
    161  1.1      cgd 	}
    162  1.1      cgd 	else
    163  1.1      cgd 		system = _PATH_UNIX;
    164  1.1      cgd 
    165  1.1      cgd 	if (nlist(system, nl) < 0 || !nl[0].n_value) {
    166  1.1      cgd 		fprintf(stderr, "trpt: %s: no namelist\n", system);
    167  1.1      cgd 		exit(1);
    168  1.1      cgd 	}
    169  1.1      cgd 	if ((memf = open(core, O_RDONLY)) < 0) {
    170  1.1      cgd 		perror(core);
    171  1.1      cgd 		exit(2);
    172  1.1      cgd 	}
    173  1.1      cgd 	if (kflag) {
    174  1.1      cgd #ifdef NEWVM
    175  1.1      cgd 		fputs("trpt: can't do core files yet\n", stderr);
    176  1.1      cgd 		exit(1);
    177  1.1      cgd #else
    178  1.1      cgd 		off_t off;
    179  1.1      cgd 
    180  1.1      cgd 		Sysmap = (struct pte *)
    181  1.1      cgd 		   malloc((u_int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
    182  1.1      cgd 		if (!Sysmap) {
    183  1.1      cgd 			fputs("trpt: can't get memory for Sysmap.\n", stderr);
    184  1.1      cgd 			exit(1);
    185  1.1      cgd 		}
    186  1.1      cgd 		off = nl[N_SYSMAP].n_value & ~KERNBASE;
    187  1.1      cgd 		(void)lseek(memf, off, L_SET);
    188  1.1      cgd 		(void)read(memf, (char *)Sysmap,
    189  1.1      cgd 		    (int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
    190  1.1      cgd #endif
    191  1.1      cgd 	}
    192  1.1      cgd 	(void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET);
    193  1.1      cgd 	if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) !=
    194  1.1      cgd 	    sizeof(tcp_debx)) {
    195  1.1      cgd 		perror("trpt: tcp_debx");
    196  1.1      cgd 		exit(3);
    197  1.1      cgd 	}
    198  1.1      cgd 	(void)klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET);
    199  1.1      cgd 	if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) !=
    200  1.1      cgd 	    sizeof(tcp_debug)) {
    201  1.1      cgd 		perror("trpt: tcp_debug");
    202  1.1      cgd 		exit(3);
    203  1.1      cgd 	}
    204  1.1      cgd 	/*
    205  1.1      cgd 	 * If no control blocks have been specified, figure
    206  1.1      cgd 	 * out how many distinct one we have and summarize
    207  1.1      cgd 	 * them in tcp_pcbs for sorting the trace records
    208  1.1      cgd 	 * below.
    209  1.1      cgd 	 */
    210  1.1      cgd 	if (!npcbs) {
    211  1.1      cgd 		for (i = 0; i < TCP_NDEBUG; i++) {
    212  1.1      cgd 			register struct tcp_debug *td = &tcp_debug[i];
    213  1.1      cgd 			register int j;
    214  1.1      cgd 
    215  1.1      cgd 			if (td->td_tcb == 0)
    216  1.1      cgd 				continue;
    217  1.1      cgd 			for (j = 0; j < npcbs; j++)
    218  1.1      cgd 				if (tcp_pcbs[j] == td->td_tcb)
    219  1.1      cgd 					break;
    220  1.1      cgd 			if (j >= npcbs)
    221  1.1      cgd 				tcp_pcbs[npcbs++] = td->td_tcb;
    222  1.1      cgd 		}
    223  1.1      cgd 		if (!npcbs)
    224  1.1      cgd 			exit(0);
    225  1.1      cgd 	}
    226  1.1      cgd 	qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric);
    227  1.1      cgd 	if (jflag) {
    228  1.1      cgd 		for (i = 0;;) {
    229  1.1      cgd 			printf("%x", (int)tcp_pcbs[i]);
    230  1.1      cgd 			if (++i == npcbs)
    231  1.1      cgd 				break;
    232  1.1      cgd 			fputs(", ", stdout);
    233  1.1      cgd 		}
    234  1.1      cgd 		putchar('\n');
    235  1.1      cgd 	}
    236  1.1      cgd 	else for (i = 0; i < npcbs; i++) {
    237  1.1      cgd 		printf("\n%x:\n", (int)tcp_pcbs[i]);
    238  1.1      cgd 		dotrace(tcp_pcbs[i]);
    239  1.1      cgd 	}
    240  1.1      cgd 	exit(0);
    241  1.1      cgd }
    242  1.1      cgd 
    243  1.1      cgd dotrace(tcpcb)
    244  1.1      cgd 	register caddr_t tcpcb;
    245  1.1      cgd {
    246  1.1      cgd 	register struct tcp_debug *td;
    247  1.1      cgd 	register int i;
    248  1.1      cgd 	int prev_debx = tcp_debx;
    249  1.1      cgd 
    250  1.1      cgd again:	if (--tcp_debx < 0)
    251  1.1      cgd 		tcp_debx = TCP_NDEBUG - 1;
    252  1.1      cgd 	for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
    253  1.1      cgd 		td = &tcp_debug[i];
    254  1.1      cgd 		if (tcpcb && td->td_tcb != tcpcb)
    255  1.1      cgd 			continue;
    256  1.1      cgd 		ntime = ntohl(td->td_time);
    257  1.1      cgd 		tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb,
    258  1.1      cgd 		    &td->td_ti, td->td_req);
    259  1.1      cgd 		if (i == tcp_debx)
    260  1.1      cgd 			goto done;
    261  1.1      cgd 	}
    262  1.1      cgd 	for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) {
    263  1.1      cgd 		td = &tcp_debug[i];
    264  1.1      cgd 		if (tcpcb && td->td_tcb != tcpcb)
    265  1.1      cgd 			continue;
    266  1.1      cgd 		ntime = ntohl(td->td_time);
    267  1.1      cgd 		tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb,
    268  1.1      cgd 		    &td->td_ti, td->td_req);
    269  1.1      cgd 	}
    270  1.1      cgd done:	if (follow) {
    271  1.1      cgd 		prev_debx = tcp_debx + 1;
    272  1.1      cgd 		if (prev_debx >= TCP_NDEBUG)
    273  1.1      cgd 			prev_debx = 0;
    274  1.1      cgd 		do {
    275  1.1      cgd 			sleep(1);
    276  1.1      cgd 			(void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET);
    277  1.1      cgd 			if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) !=
    278  1.1      cgd 			    sizeof(tcp_debx)) {
    279  1.1      cgd 				perror("trpt: tcp_debx");
    280  1.1      cgd 				exit(3);
    281  1.1      cgd 			}
    282  1.1      cgd 		} while (tcp_debx == prev_debx);
    283  1.1      cgd 		(void)klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET);
    284  1.1      cgd 		if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) !=
    285  1.1      cgd 		    sizeof(tcp_debug)) {
    286  1.1      cgd 			perror("trpt: tcp_debug");
    287  1.1      cgd 			exit(3);
    288  1.1      cgd 		}
    289  1.1      cgd 		goto again;
    290  1.1      cgd 	}
    291  1.1      cgd }
    292  1.1      cgd 
    293  1.1      cgd /*
    294  1.1      cgd  * Tcp debug routines
    295  1.1      cgd  */
    296  1.1      cgd /*ARGSUSED*/
    297  1.1      cgd tcp_trace(act, ostate, atp, tp, ti, req)
    298  1.1      cgd 	short act, ostate;
    299  1.1      cgd 	struct tcpcb *atp, *tp;
    300  1.1      cgd 	struct tcpiphdr *ti;
    301  1.1      cgd 	int req;
    302  1.1      cgd {
    303  1.1      cgd 	tcp_seq seq, ack;
    304  1.1      cgd 	int flags, len, win, timer;
    305  1.1      cgd 
    306  1.1      cgd 	printf("%03ld %s:%s ",(ntime/10) % 1000, tcpstates[ostate],
    307  1.1      cgd 	    tanames[act]);
    308  1.1      cgd 	switch (act) {
    309  1.1      cgd 	case TA_INPUT:
    310  1.1      cgd 	case TA_OUTPUT:
    311  1.1      cgd 	case TA_DROP:
    312  1.1      cgd 		if (aflag) {
    313  1.1      cgd 			printf("(src=%s,%u, ",
    314  1.1      cgd 			    inet_ntoa(ti->ti_src), ntohs(ti->ti_sport));
    315  1.1      cgd 			printf("dst=%s,%u)",
    316  1.1      cgd 			    inet_ntoa(ti->ti_dst), ntohs(ti->ti_dport));
    317  1.1      cgd 		}
    318  1.1      cgd 		seq = ti->ti_seq;
    319  1.1      cgd 		ack = ti->ti_ack;
    320  1.1      cgd 		len = ti->ti_len;
    321  1.1      cgd 		win = ti->ti_win;
    322  1.1      cgd 		if (act == TA_OUTPUT) {
    323  1.1      cgd 			seq = ntohl(seq);
    324  1.1      cgd 			ack = ntohl(ack);
    325  1.1      cgd 			len = ntohs(len);
    326  1.1      cgd 			win = ntohs(win);
    327  1.1      cgd 		}
    328  1.1      cgd 		if (act == TA_OUTPUT)
    329  1.1      cgd 			len -= sizeof(struct tcphdr);
    330  1.1      cgd 		if (len)
    331  1.1      cgd 			printf("[%lx..%lx)", seq, seq + len);
    332  1.1      cgd 		else
    333  1.1      cgd 			printf("%lx", seq);
    334  1.1      cgd 		printf("@%lx", ack);
    335  1.1      cgd 		if (win)
    336  1.1      cgd 			printf("(win=%x)", win);
    337  1.1      cgd 		flags = ti->ti_flags;
    338  1.1      cgd 		if (flags) {
    339  1.1      cgd 			register char *cp = "<";
    340  1.1      cgd #define	pf(flag, string) { \
    341  1.1      cgd 	if (ti->ti_flags&flag) { \
    342  1.1      cgd 		(void)printf("%s%s", cp, string); \
    343  1.1      cgd 		cp = ","; \
    344  1.1      cgd 	} \
    345  1.1      cgd }
    346  1.1      cgd 			pf(TH_SYN, "SYN");
    347  1.1      cgd 			pf(TH_ACK, "ACK");
    348  1.1      cgd 			pf(TH_FIN, "FIN");
    349  1.1      cgd 			pf(TH_RST, "RST");
    350  1.1      cgd 			pf(TH_PUSH, "PUSH");
    351  1.1      cgd 			pf(TH_URG, "URG");
    352  1.1      cgd 			printf(">");
    353  1.1      cgd 		}
    354  1.1      cgd 		break;
    355  1.1      cgd 	case TA_USER:
    356  1.1      cgd 		timer = req >> 8;
    357  1.1      cgd 		req &= 0xff;
    358  1.1      cgd 		printf("%s", prurequests[req]);
    359  1.1      cgd 		if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
    360  1.1      cgd 			printf("<%s>", tcptimers[timer]);
    361  1.1      cgd 		break;
    362  1.1      cgd 	}
    363  1.1      cgd 	printf(" -> %s", tcpstates[tp->t_state]);
    364  1.1      cgd 	/* print out internal state of tp !?! */
    365  1.1      cgd 	printf("\n");
    366  1.1      cgd 	if (sflag) {
    367  1.1      cgd 		printf("\trcv_nxt %lx rcv_wnd %x snd_una %lx snd_nxt %lx snd_max %lx\n",
    368  1.1      cgd 		    tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt,
    369  1.1      cgd 		    tp->snd_max);
    370  1.1      cgd 		printf("\tsnd_wl1 %lx snd_wl2 %lx snd_wnd %x\n", tp->snd_wl1,
    371  1.1      cgd 		    tp->snd_wl2, tp->snd_wnd);
    372  1.1      cgd 	}
    373  1.1      cgd 	/* print out timers? */
    374  1.1      cgd 	if (tflag) {
    375  1.1      cgd 		register char *cp = "\t";
    376  1.1      cgd 		register int i;
    377  1.1      cgd 
    378  1.1      cgd 		for (i = 0; i < TCPT_NTIMERS; i++) {
    379  1.1      cgd 			if (tp->t_timer[i] == 0)
    380  1.1      cgd 				continue;
    381  1.1      cgd 			printf("%s%s=%d", cp, tcptimers[i], tp->t_timer[i]);
    382  1.1      cgd 			if (i == TCPT_REXMT)
    383  1.1      cgd 				printf(" (t_rxtshft=%d)", tp->t_rxtshift);
    384  1.1      cgd 			cp = ", ";
    385  1.1      cgd 		}
    386  1.1      cgd 		if (*cp != '\t')
    387  1.1      cgd 			putchar('\n');
    388  1.1      cgd 	}
    389  1.1      cgd }
    390  1.1      cgd 
    391  1.1      cgd numeric(c1, c2)
    392  1.1      cgd 	caddr_t *c1, *c2;
    393  1.1      cgd {
    394  1.1      cgd 	return(*c1 - *c2);
    395  1.1      cgd }
    396  1.1      cgd 
    397  1.1      cgd klseek(fd, base, off)
    398  1.1      cgd 	int fd, off;
    399  1.1      cgd 	off_t base;
    400  1.1      cgd {
    401  1.1      cgd 	off_t lseek();
    402  1.1      cgd 
    403  1.1      cgd #ifndef NEWVM
    404  1.1      cgd 	if (kflag) {	/* get kernel pte */
    405  1.1      cgd 		base &= ~KERNBASE;
    406  1.1      cgd 		base = ctob(Sysmap[btop(base)].pg_pfnum) + (base & PGOFSET);
    407  1.1      cgd 	}
    408  1.1      cgd #endif
    409  1.1      cgd 	(void)lseek(fd, base, off);
    410  1.1      cgd }
    411