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