xref: /openbsd/usr.sbin/trpt/trpt.c (revision 133306f0)
1 /*	$OpenBSD: trpt.c,v 1.8 2000/02/25 23:32:55 deraadt 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 #ifndef lint
74 char copyright[] =
75 "@(#) Copyright (c) 1983, 1988, 1993\n\
76 	The Regents of the University of California.  All rights reserved.\n";
77 #endif /* not lint */
78 
79 #ifndef lint
80 static char sccsid[] = "@(#)trpt.c	8.1 (Berkeley) 6/6/93";
81 #endif /* not lint */
82 
83 #include <sys/param.h>
84 #include <sys/queue.h>
85 #include <sys/socket.h>
86 #include <sys/socketvar.h>
87 #define PRUREQUESTS
88 #include <sys/protosw.h>
89 #include <sys/file.h>
90 
91 #include <net/route.h>
92 #include <net/if.h>
93 
94 #include <netinet/in.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/ip.h>
97 #include <netinet/in_pcb.h>
98 #include <netinet/ip_var.h>
99 #include <netinet/tcp.h>
100 #define TCPSTATES
101 #include <netinet/tcp_fsm.h>
102 #include <netinet/tcp_seq.h>
103 #define	TCPTIMERS
104 #include <netinet/tcp_timer.h>
105 #include <netinet/tcp_var.h>
106 #include <netinet/tcpip.h>
107 #define	TANAMES
108 #include <netinet/tcp_debug.h>
109 
110 #include <arpa/inet.h>
111 
112 #include <err.h>
113 #include <stdio.h>
114 #include <errno.h>
115 #include <kvm.h>
116 #include <nlist.h>
117 #include <paths.h>
118 #include <limits.h>
119 #include <stdlib.h>
120 #include <unistd.h>
121 
122 struct nlist nl[] = {
123 #define	N_TCP_DEBUG	0
124 	{ "_tcp_debug" },
125 #define	N_TCP_DEBX	1
126 	{ "_tcp_debx" },
127 	{ NULL },
128 };
129 
130 static caddr_t tcp_pcbs[TCP_NDEBUG];
131 static n_time ntime;
132 static int aflag, follow, sflag, tflag;
133 
134 extern	char *__progname;
135 
136 int	main __P((int, char *[]));
137 void	dotrace __P((caddr_t));
138 void	tcp_trace __P((short, short, struct tcpcb *, struct tcpcb *,
139 	    struct tcpiphdr *, int));
140 int	numeric __P((const void *, const void *));
141 void	usage __P((void));
142 
143 kvm_t	*kd;
144 
145 int
146 main(argc, argv)
147 	int argc;
148 	char *argv[];
149 {
150 	int ch, i, jflag, npcbs;
151 	char *system, *core, *cp, errbuf[_POSIX2_LINE_MAX];
152 
153 	system = core = NULL;
154 
155 	jflag = npcbs = 0;
156 	while ((ch = getopt(argc, argv, "afjM:N:p:st")) != -1) {
157 		switch (ch) {
158 		case 'a':
159 			++aflag;
160 			break;
161 		case 'f':
162 			++follow;
163 			setlinebuf(stdout);
164 			break;
165 		case 'j':
166 			++jflag;
167 			break;
168 		case 'p':
169 			if (npcbs >= TCP_NDEBUG)
170 				errx(1, "too many pcbs specified");
171 			errno = 0;
172 			tcp_pcbs[npcbs++] = (caddr_t)strtoul(optarg, &cp, 16);
173 			if (*cp != '\0' || errno == ERANGE)
174 				errx(1, "invalid address: %s", optarg);
175 			break;
176 		case 's':
177 			++sflag;
178 			break;
179 		case 't':
180 			++tflag;
181 			break;
182 		case 'N':
183 			system = optarg;
184 			break;
185 		case 'M':
186 			core = optarg;
187 			break;
188 		case '?':
189 		default:
190 			usage();
191 			/* NOTREACHED */
192 		}
193 	}
194 	argc -= optind;
195 	argv += optind;
196 
197 	if (argc)
198 		usage();
199 
200 	/*
201 	 * Discard setgid privileged if not the running kernel so that bad
202 	 * guys can't print interesting stuff from kernel memory.
203 	 */
204 	if (core != NULL || system != NULL) {
205 		setegid(getgid());
206 		setgid(getgid());
207 	}
208 
209 	kd = kvm_openfiles(system, core, NULL, O_RDONLY, errbuf);
210 	if (kd == NULL)
211 		errx(1, "can't open kmem: %s", errbuf);
212 
213 	setegid(getgid());
214 	setgid(getgid());
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 	const caddr_t *c1 = v1;
423 	const caddr_t *c2 = v2;
424 	int rv;
425 
426 	if (*c1 < *c2)
427 		rv = -1;
428 	else if (*c1 > *c2)
429 		rv = 1;
430 	else
431 		rv = 0;
432 
433 	return (rv);
434 }
435 
436 void
437 usage()
438 {
439 
440 	(void) fprintf(stderr, "usage: %s [-afjst] [-p hex-address]"
441 	    " [-N system] [-M core]\n", __progname);
442 	exit(1);
443 }
444