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