Home | History | Annotate | Line # | Download | only in trpt
trpt.c revision 1.1.1.1
      1      1.1    cgd /*
      2  1.1.1.1  mikel  * Copyright (c) 1983, 1988, 1993
      3  1.1.1.1  mikel  *	The Regents of the University of California.  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.1.1  mikel static char copyright[] =
     36  1.1.1.1  mikel "@(#) Copyright (c) 1983, 1988, 1993\n\
     37  1.1.1.1  mikel 	The Regents of the University of California.  All rights reserved.\n";
     38      1.1    cgd #endif /* not lint */
     39      1.1    cgd 
     40      1.1    cgd #ifndef lint
     41  1.1.1.1  mikel static char sccsid[] = "@(#)trpt.c	8.1 (Berkeley) 6/6/93";
     42      1.1    cgd #endif /* not lint */
     43      1.1    cgd 
     44      1.1    cgd #include <sys/param.h>
     45      1.1    cgd #if BSD >= 199103
     46      1.1    cgd #define NEWVM
     47      1.1    cgd #endif
     48      1.1    cgd #ifndef NEWVM
     49      1.1    cgd #include <machine/pte.h>
     50      1.1    cgd #include <sys/vmmac.h>
     51      1.1    cgd #endif
     52      1.1    cgd #include <sys/socket.h>
     53      1.1    cgd #include <sys/socketvar.h>
     54      1.1    cgd #define PRUREQUESTS
     55      1.1    cgd #include <sys/protosw.h>
     56      1.1    cgd #include <sys/file.h>
     57      1.1    cgd 
     58      1.1    cgd #include <net/route.h>
     59      1.1    cgd #include <net/if.h>
     60      1.1    cgd 
     61      1.1    cgd #include <netinet/in.h>
     62      1.1    cgd #include <netinet/in_systm.h>
     63      1.1    cgd #include <netinet/ip.h>
     64      1.1    cgd #include <netinet/in_pcb.h>
     65      1.1    cgd #include <netinet/ip_var.h>
     66      1.1    cgd #include <netinet/tcp.h>
     67      1.1    cgd #define TCPSTATES
     68      1.1    cgd #include <netinet/tcp_fsm.h>
     69      1.1    cgd #include <netinet/tcp_seq.h>
     70      1.1    cgd #define	TCPTIMERS
     71      1.1    cgd #include <netinet/tcp_timer.h>
     72      1.1    cgd #include <netinet/tcp_var.h>
     73      1.1    cgd #include <netinet/tcpip.h>
     74      1.1    cgd #define	TANAMES
     75      1.1    cgd #include <netinet/tcp_debug.h>
     76      1.1    cgd 
     77      1.1    cgd #include <arpa/inet.h>
     78      1.1    cgd 
     79      1.1    cgd #include <stdio.h>
     80      1.1    cgd #include <errno.h>
     81      1.1    cgd #include <nlist.h>
     82      1.1    cgd #include <paths.h>
     83      1.1    cgd 
     84      1.1    cgd struct nlist nl[] = {
     85      1.1    cgd #define	N_TCP_DEBUG	0
     86      1.1    cgd 	{ "_tcp_debug" },
     87      1.1    cgd #define	N_TCP_DEBX	1
     88      1.1    cgd 	{ "_tcp_debx" },
     89      1.1    cgd #ifndef NEWVM
     90      1.1    cgd #define	N_SYSMAP	2
     91      1.1    cgd 	{ "_Sysmap" },
     92      1.1    cgd #define	N_SYSSIZE	3
     93      1.1    cgd 	{ "_Syssize" },
     94      1.1    cgd #endif
     95      1.1    cgd 	{ "" },
     96      1.1    cgd };
     97      1.1    cgd 
     98      1.1    cgd #ifndef NEWVM
     99      1.1    cgd static struct pte *Sysmap;
    100      1.1    cgd #endif
    101      1.1    cgd static caddr_t tcp_pcbs[TCP_NDEBUG];
    102      1.1    cgd static n_time ntime;
    103      1.1    cgd static int aflag, kflag, memf, follow, sflag, tflag;
    104      1.1    cgd 
    105      1.1    cgd main(argc, argv)
    106      1.1    cgd 	int argc;
    107      1.1    cgd 	char **argv;
    108      1.1    cgd {
    109      1.1    cgd 	extern char *optarg;
    110      1.1    cgd 	extern int optind;
    111      1.1    cgd 	int ch, i, jflag, npcbs, numeric();
    112      1.1    cgd 	char *system, *core, *malloc();
    113      1.1    cgd 	off_t lseek();
    114      1.1    cgd 
    115      1.1    cgd 	jflag = npcbs = 0;
    116      1.1    cgd 	while ((ch = getopt(argc, argv, "afjp:st")) != EOF)
    117      1.1    cgd 		switch (ch) {
    118      1.1    cgd 		case 'a':
    119      1.1    cgd 			++aflag;
    120      1.1    cgd 			break;
    121      1.1    cgd 		case 'f':
    122      1.1    cgd 			++follow;
    123      1.1    cgd 			setlinebuf(stdout);
    124      1.1    cgd 			break;
    125      1.1    cgd 		case 'j':
    126      1.1    cgd 			++jflag;
    127      1.1    cgd 			break;
    128      1.1    cgd 		case 'p':
    129      1.1    cgd 			if (npcbs >= TCP_NDEBUG) {
    130      1.1    cgd 				fputs("trpt: too many pcb's specified\n",
    131      1.1    cgd 				    stderr);
    132      1.1    cgd 				exit(1);
    133      1.1    cgd 			}
    134      1.1    cgd 			(void)sscanf(optarg, "%x", (int *)&tcp_pcbs[npcbs++]);
    135      1.1    cgd 			break;
    136      1.1    cgd 		case 's':
    137      1.1    cgd 			++sflag;
    138      1.1    cgd 			break;
    139      1.1    cgd 		case 't':
    140      1.1    cgd 			++tflag;
    141      1.1    cgd 			break;
    142      1.1    cgd 		case '?':
    143      1.1    cgd 		default:
    144      1.1    cgd 			(void)fprintf(stderr,
    145      1.1    cgd "usage: trpt [-afjst] [-p hex-address] [system [core]]\n");
    146      1.1    cgd 			exit(1);
    147      1.1    cgd 		}
    148      1.1    cgd 	argc -= optind;
    149      1.1    cgd 	argv += optind;
    150      1.1    cgd 
    151      1.1    cgd 	core = _PATH_KMEM;
    152      1.1    cgd 	if (argc > 0) {
    153      1.1    cgd 		system = *argv;
    154      1.1    cgd 		argc--, argv++;
    155      1.1    cgd 		if (argc > 0) {
    156      1.1    cgd 			core = *argv;
    157      1.1    cgd 			argc--, argv++;
    158      1.1    cgd 			++kflag;
    159      1.1    cgd 		}
    160  1.1.1.1  mikel 		/*
    161  1.1.1.1  mikel 		 * Discard setgid privileges if not the running kernel so that
    162  1.1.1.1  mikel 		 * bad guys can't print interesting stuff from kernel memory.
    163  1.1.1.1  mikel 		 */
    164  1.1.1.1  mikel 		setgid(getgid());
    165      1.1    cgd 	}
    166      1.1    cgd 	else
    167      1.1    cgd 		system = _PATH_UNIX;
    168      1.1    cgd 
    169      1.1    cgd 	if (nlist(system, nl) < 0 || !nl[0].n_value) {
    170      1.1    cgd 		fprintf(stderr, "trpt: %s: no namelist\n", system);
    171      1.1    cgd 		exit(1);
    172      1.1    cgd 	}
    173      1.1    cgd 	if ((memf = open(core, O_RDONLY)) < 0) {
    174      1.1    cgd 		perror(core);
    175      1.1    cgd 		exit(2);
    176      1.1    cgd 	}
    177      1.1    cgd 	if (kflag) {
    178      1.1    cgd #ifdef NEWVM
    179      1.1    cgd 		fputs("trpt: can't do core files yet\n", stderr);
    180      1.1    cgd 		exit(1);
    181      1.1    cgd #else
    182      1.1    cgd 		off_t off;
    183      1.1    cgd 
    184      1.1    cgd 		Sysmap = (struct pte *)
    185      1.1    cgd 		   malloc((u_int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
    186      1.1    cgd 		if (!Sysmap) {
    187      1.1    cgd 			fputs("trpt: can't get memory for Sysmap.\n", stderr);
    188      1.1    cgd 			exit(1);
    189      1.1    cgd 		}
    190      1.1    cgd 		off = nl[N_SYSMAP].n_value & ~KERNBASE;
    191      1.1    cgd 		(void)lseek(memf, off, L_SET);
    192      1.1    cgd 		(void)read(memf, (char *)Sysmap,
    193      1.1    cgd 		    (int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
    194      1.1    cgd #endif
    195      1.1    cgd 	}
    196      1.1    cgd 	(void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET);
    197      1.1    cgd 	if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) !=
    198      1.1    cgd 	    sizeof(tcp_debx)) {
    199      1.1    cgd 		perror("trpt: tcp_debx");
    200      1.1    cgd 		exit(3);
    201      1.1    cgd 	}
    202      1.1    cgd 	(void)klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET);
    203      1.1    cgd 	if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) !=
    204      1.1    cgd 	    sizeof(tcp_debug)) {
    205      1.1    cgd 		perror("trpt: tcp_debug");
    206      1.1    cgd 		exit(3);
    207      1.1    cgd 	}
    208      1.1    cgd 	/*
    209      1.1    cgd 	 * If no control blocks have been specified, figure
    210      1.1    cgd 	 * out how many distinct one we have and summarize
    211      1.1    cgd 	 * them in tcp_pcbs for sorting the trace records
    212      1.1    cgd 	 * below.
    213      1.1    cgd 	 */
    214      1.1    cgd 	if (!npcbs) {
    215      1.1    cgd 		for (i = 0; i < TCP_NDEBUG; i++) {
    216      1.1    cgd 			register struct tcp_debug *td = &tcp_debug[i];
    217      1.1    cgd 			register int j;
    218      1.1    cgd 
    219      1.1    cgd 			if (td->td_tcb == 0)
    220      1.1    cgd 				continue;
    221      1.1    cgd 			for (j = 0; j < npcbs; j++)
    222      1.1    cgd 				if (tcp_pcbs[j] == td->td_tcb)
    223      1.1    cgd 					break;
    224      1.1    cgd 			if (j >= npcbs)
    225      1.1    cgd 				tcp_pcbs[npcbs++] = td->td_tcb;
    226      1.1    cgd 		}
    227      1.1    cgd 		if (!npcbs)
    228      1.1    cgd 			exit(0);
    229      1.1    cgd 	}
    230      1.1    cgd 	qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric);
    231      1.1    cgd 	if (jflag) {
    232      1.1    cgd 		for (i = 0;;) {
    233      1.1    cgd 			printf("%x", (int)tcp_pcbs[i]);
    234      1.1    cgd 			if (++i == npcbs)
    235      1.1    cgd 				break;
    236      1.1    cgd 			fputs(", ", stdout);
    237      1.1    cgd 		}
    238      1.1    cgd 		putchar('\n');
    239      1.1    cgd 	}
    240      1.1    cgd 	else for (i = 0; i < npcbs; i++) {
    241      1.1    cgd 		printf("\n%x:\n", (int)tcp_pcbs[i]);
    242      1.1    cgd 		dotrace(tcp_pcbs[i]);
    243      1.1    cgd 	}
    244      1.1    cgd 	exit(0);
    245      1.1    cgd }
    246      1.1    cgd 
    247      1.1    cgd dotrace(tcpcb)
    248      1.1    cgd 	register caddr_t tcpcb;
    249      1.1    cgd {
    250      1.1    cgd 	register struct tcp_debug *td;
    251      1.1    cgd 	register int i;
    252      1.1    cgd 	int prev_debx = tcp_debx;
    253      1.1    cgd 
    254      1.1    cgd again:	if (--tcp_debx < 0)
    255      1.1    cgd 		tcp_debx = TCP_NDEBUG - 1;
    256      1.1    cgd 	for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
    257      1.1    cgd 		td = &tcp_debug[i];
    258      1.1    cgd 		if (tcpcb && td->td_tcb != tcpcb)
    259      1.1    cgd 			continue;
    260      1.1    cgd 		ntime = ntohl(td->td_time);
    261      1.1    cgd 		tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb,
    262      1.1    cgd 		    &td->td_ti, td->td_req);
    263      1.1    cgd 		if (i == tcp_debx)
    264      1.1    cgd 			goto done;
    265      1.1    cgd 	}
    266      1.1    cgd 	for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) {
    267      1.1    cgd 		td = &tcp_debug[i];
    268      1.1    cgd 		if (tcpcb && td->td_tcb != tcpcb)
    269      1.1    cgd 			continue;
    270      1.1    cgd 		ntime = ntohl(td->td_time);
    271      1.1    cgd 		tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb,
    272      1.1    cgd 		    &td->td_ti, td->td_req);
    273      1.1    cgd 	}
    274      1.1    cgd done:	if (follow) {
    275      1.1    cgd 		prev_debx = tcp_debx + 1;
    276      1.1    cgd 		if (prev_debx >= TCP_NDEBUG)
    277      1.1    cgd 			prev_debx = 0;
    278      1.1    cgd 		do {
    279      1.1    cgd 			sleep(1);
    280      1.1    cgd 			(void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET);
    281      1.1    cgd 			if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) !=
    282      1.1    cgd 			    sizeof(tcp_debx)) {
    283      1.1    cgd 				perror("trpt: tcp_debx");
    284      1.1    cgd 				exit(3);
    285      1.1    cgd 			}
    286      1.1    cgd 		} while (tcp_debx == prev_debx);
    287      1.1    cgd 		(void)klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET);
    288      1.1    cgd 		if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) !=
    289      1.1    cgd 		    sizeof(tcp_debug)) {
    290      1.1    cgd 			perror("trpt: tcp_debug");
    291      1.1    cgd 			exit(3);
    292      1.1    cgd 		}
    293      1.1    cgd 		goto again;
    294      1.1    cgd 	}
    295      1.1    cgd }
    296      1.1    cgd 
    297      1.1    cgd /*
    298      1.1    cgd  * Tcp debug routines
    299      1.1    cgd  */
    300      1.1    cgd /*ARGSUSED*/
    301      1.1    cgd tcp_trace(act, ostate, atp, tp, ti, req)
    302      1.1    cgd 	short act, ostate;
    303      1.1    cgd 	struct tcpcb *atp, *tp;
    304      1.1    cgd 	struct tcpiphdr *ti;
    305      1.1    cgd 	int req;
    306      1.1    cgd {
    307      1.1    cgd 	tcp_seq seq, ack;
    308      1.1    cgd 	int flags, len, win, timer;
    309      1.1    cgd 
    310      1.1    cgd 	printf("%03ld %s:%s ",(ntime/10) % 1000, tcpstates[ostate],
    311      1.1    cgd 	    tanames[act]);
    312      1.1    cgd 	switch (act) {
    313      1.1    cgd 	case TA_INPUT:
    314      1.1    cgd 	case TA_OUTPUT:
    315      1.1    cgd 	case TA_DROP:
    316      1.1    cgd 		if (aflag) {
    317      1.1    cgd 			printf("(src=%s,%u, ",
    318      1.1    cgd 			    inet_ntoa(ti->ti_src), ntohs(ti->ti_sport));
    319      1.1    cgd 			printf("dst=%s,%u)",
    320      1.1    cgd 			    inet_ntoa(ti->ti_dst), ntohs(ti->ti_dport));
    321      1.1    cgd 		}
    322      1.1    cgd 		seq = ti->ti_seq;
    323      1.1    cgd 		ack = ti->ti_ack;
    324      1.1    cgd 		len = ti->ti_len;
    325      1.1    cgd 		win = ti->ti_win;
    326      1.1    cgd 		if (act == TA_OUTPUT) {
    327      1.1    cgd 			seq = ntohl(seq);
    328      1.1    cgd 			ack = ntohl(ack);
    329      1.1    cgd 			len = ntohs(len);
    330      1.1    cgd 			win = ntohs(win);
    331      1.1    cgd 		}
    332      1.1    cgd 		if (act == TA_OUTPUT)
    333      1.1    cgd 			len -= sizeof(struct tcphdr);
    334      1.1    cgd 		if (len)
    335      1.1    cgd 			printf("[%lx..%lx)", seq, seq + len);
    336      1.1    cgd 		else
    337      1.1    cgd 			printf("%lx", seq);
    338      1.1    cgd 		printf("@%lx", ack);
    339      1.1    cgd 		if (win)
    340      1.1    cgd 			printf("(win=%x)", win);
    341      1.1    cgd 		flags = ti->ti_flags;
    342      1.1    cgd 		if (flags) {
    343      1.1    cgd 			register char *cp = "<";
    344      1.1    cgd #define	pf(flag, string) { \
    345      1.1    cgd 	if (ti->ti_flags&flag) { \
    346      1.1    cgd 		(void)printf("%s%s", cp, string); \
    347      1.1    cgd 		cp = ","; \
    348      1.1    cgd 	} \
    349      1.1    cgd }
    350      1.1    cgd 			pf(TH_SYN, "SYN");
    351      1.1    cgd 			pf(TH_ACK, "ACK");
    352      1.1    cgd 			pf(TH_FIN, "FIN");
    353      1.1    cgd 			pf(TH_RST, "RST");
    354      1.1    cgd 			pf(TH_PUSH, "PUSH");
    355      1.1    cgd 			pf(TH_URG, "URG");
    356      1.1    cgd 			printf(">");
    357      1.1    cgd 		}
    358      1.1    cgd 		break;
    359      1.1    cgd 	case TA_USER:
    360      1.1    cgd 		timer = req >> 8;
    361      1.1    cgd 		req &= 0xff;
    362      1.1    cgd 		printf("%s", prurequests[req]);
    363      1.1    cgd 		if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
    364      1.1    cgd 			printf("<%s>", tcptimers[timer]);
    365      1.1    cgd 		break;
    366      1.1    cgd 	}
    367      1.1    cgd 	printf(" -> %s", tcpstates[tp->t_state]);
    368      1.1    cgd 	/* print out internal state of tp !?! */
    369      1.1    cgd 	printf("\n");
    370      1.1    cgd 	if (sflag) {
    371      1.1    cgd 		printf("\trcv_nxt %lx rcv_wnd %x snd_una %lx snd_nxt %lx snd_max %lx\n",
    372      1.1    cgd 		    tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt,
    373      1.1    cgd 		    tp->snd_max);
    374      1.1    cgd 		printf("\tsnd_wl1 %lx snd_wl2 %lx snd_wnd %x\n", tp->snd_wl1,
    375      1.1    cgd 		    tp->snd_wl2, tp->snd_wnd);
    376      1.1    cgd 	}
    377      1.1    cgd 	/* print out timers? */
    378      1.1    cgd 	if (tflag) {
    379      1.1    cgd 		register char *cp = "\t";
    380      1.1    cgd 		register int i;
    381      1.1    cgd 
    382      1.1    cgd 		for (i = 0; i < TCPT_NTIMERS; i++) {
    383      1.1    cgd 			if (tp->t_timer[i] == 0)
    384      1.1    cgd 				continue;
    385      1.1    cgd 			printf("%s%s=%d", cp, tcptimers[i], tp->t_timer[i]);
    386      1.1    cgd 			if (i == TCPT_REXMT)
    387      1.1    cgd 				printf(" (t_rxtshft=%d)", tp->t_rxtshift);
    388      1.1    cgd 			cp = ", ";
    389      1.1    cgd 		}
    390      1.1    cgd 		if (*cp != '\t')
    391      1.1    cgd 			putchar('\n');
    392      1.1    cgd 	}
    393      1.1    cgd }
    394      1.1    cgd 
    395      1.1    cgd numeric(c1, c2)
    396      1.1    cgd 	caddr_t *c1, *c2;
    397      1.1    cgd {
    398      1.1    cgd 	return(*c1 - *c2);
    399      1.1    cgd }
    400      1.1    cgd 
    401      1.1    cgd klseek(fd, base, off)
    402      1.1    cgd 	int fd, off;
    403      1.1    cgd 	off_t base;
    404      1.1    cgd {
    405      1.1    cgd 	off_t lseek();
    406      1.1    cgd 
    407      1.1    cgd #ifndef NEWVM
    408      1.1    cgd 	if (kflag) {	/* get kernel pte */
    409      1.1    cgd 		base &= ~KERNBASE;
    410      1.1    cgd 		base = ctob(Sysmap[btop(base)].pg_pfnum) + (base & PGOFSET);
    411      1.1    cgd 	}
    412      1.1    cgd #endif
    413      1.1    cgd 	(void)lseek(fd, base, off);
    414      1.1    cgd }
    415