xref: /netbsd/usr.sbin/trpt/trpt.c (revision bf9ec67e)
1 /*	$NetBSD: trpt.c,v 1.12 2001/09/11 15:45:01 thorpej 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 #include <sys/cdefs.h>
74 #ifndef lint
75 __COPYRIGHT(
76 "@(#) Copyright (c) 1983, 1988, 1993\n\
77 	The Regents of the University of California.  All rights reserved.\n");
78 #endif /* not lint */
79 
80 #ifndef lint
81 #if 0
82 static char sccsid[] = "@(#)trpt.c	8.1 (Berkeley) 6/6/93";
83 #else
84 __RCSID("$NetBSD: trpt.c,v 1.12 2001/09/11 15:45:01 thorpej Exp $");
85 #endif
86 #endif /* not lint */
87 
88 #include <sys/param.h>
89 #include <sys/queue.h>
90 #include <sys/socket.h>
91 #include <sys/socketvar.h>
92 #define PRUREQUESTS
93 #include <sys/protosw.h>
94 #include <sys/file.h>
95 
96 #include <net/route.h>
97 #include <net/if.h>
98 
99 #include <netinet/in.h>
100 #include <netinet/in_systm.h>
101 #include <netinet/ip.h>
102 #include <netinet/in_pcb.h>
103 #include <netinet/ip_var.h>
104 
105 #ifdef INET6
106 #ifndef INET
107 #include <netinet/in.h>
108 #endif
109 #include <netinet/ip6.h>
110 #endif
111 
112 #include <netinet/tcp.h>
113 #define TCPSTATES
114 #include <netinet/tcp_fsm.h>
115 #include <netinet/tcp_seq.h>
116 #define	TCPTIMERS
117 #include <netinet/tcp_timer.h>
118 #include <netinet/tcp_var.h>
119 #include <netinet/tcpip.h>
120 #define	TANAMES
121 #include <netinet/tcp_debug.h>
122 
123 #include <arpa/inet.h>
124 
125 #include <err.h>
126 #include <stdio.h>
127 #include <errno.h>
128 #include <kvm.h>
129 #include <nlist.h>
130 #include <paths.h>
131 #include <limits.h>
132 #include <stdlib.h>
133 #include <unistd.h>
134 
135 struct nlist nl[] = {
136 #define	N_TCP_DEBUG	0
137 	{ "_tcp_debug" },
138 #define	N_TCP_DEBX	1
139 	{ "_tcp_debx" },
140 	{ NULL },
141 };
142 
143 static caddr_t tcp_pcbs[TCP_NDEBUG];
144 static n_time ntime;
145 static int aflag, follow, sflag, tflag;
146 
147 /* see sys/netinet/tcp_debug.c */
148 struct  tcp_debug tcp_debug[TCP_NDEBUG];
149 int tcp_debx;
150 
151 int	main __P((int, char *[]));
152 void	dotrace __P((caddr_t));
153 void	tcp_trace __P((short, short, struct tcpcb *, struct tcpcb *,
154 	    int, void *, int));
155 int	numeric __P((const void *, const void *));
156 void	usage __P((void));
157 
158 kvm_t	*kd;
159 
160 int
161 main(argc, argv)
162 	int argc;
163 	char *argv[];
164 {
165 	int ch, i, jflag, npcbs;
166 	char *system, *core, *cp, errbuf[_POSIX2_LINE_MAX];
167 	gid_t egid = getegid();
168 
169 	(void)setegid(getgid());
170 	system = core = NULL;
171 
172 	jflag = npcbs = 0;
173 	while ((ch = getopt(argc, argv, "afjp:st")) != -1) {
174 		switch (ch) {
175 		case 'a':
176 			++aflag;
177 			break;
178 		case 'f':
179 			++follow;
180 			setlinebuf(stdout);
181 			break;
182 		case 'j':
183 			++jflag;
184 			break;
185 		case 'p':
186 			if (npcbs >= TCP_NDEBUG)
187 				errx(1, "too many pcbs specified");
188 			errno = 0;
189 			tcp_pcbs[npcbs++] = (caddr_t)strtoul(optarg, &cp, 16);
190 			if (*cp != '\0' || errno == ERANGE)
191 				errx(1, "invalid address: %s", optarg);
192 			break;
193 		case 's':
194 			++sflag;
195 			break;
196 		case 't':
197 			++tflag;
198 			break;
199 		case 'N':
200 			system = optarg;
201 			break;
202 		case 'M':
203 			core = optarg;
204 			break;
205 		case '?':
206 		default:
207 			usage();
208 			/* NOTREACHED */
209 		}
210 	}
211 	argc -= optind;
212 	argv += optind;
213 
214 	if (argc)
215 		usage();
216 
217 	/*
218 	 * Discard setgid privileges.  If not the running kernel, we toss
219 	 * them away totally so that bad guys can't print interesting stuff
220 	 * from kernel memory, otherwise switch back to kmem for the
221 	 * duration of the kvm_openfiles() call.
222 	 */
223 	if (core != NULL || system != NULL)
224 		setgid(getgid());
225 	else
226 		setegid(egid);
227 
228 	kd = kvm_openfiles(system, core, NULL, O_RDONLY, errbuf);
229 	if (kd == NULL)
230 		errx(1, "can't open kmem: %s", errbuf);
231 
232 	/* get rid of it now anyway */
233 	if (core == NULL && system == NULL)
234 		setgid(getgid());
235 
236 	if (kvm_nlist(kd, nl))
237 		errx(2, "%s: no namelist", system ? system : _PATH_UNIX);
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 	 * If no control blocks have been specified, figure
249 	 * out how many distinct one we have and summarize
250 	 * them in tcp_pcbs for sorting the trace records
251 	 * below.
252 	 */
253 	if (npcbs == 0) {
254 		for (i = 0; i < TCP_NDEBUG; i++) {
255 			struct tcp_debug *td = &tcp_debug[i];
256 			int j;
257 
258 			if (td->td_tcb == 0)
259 				continue;
260 			for (j = 0; j < npcbs; j++)
261 				if (tcp_pcbs[j] == td->td_tcb)
262 					break;
263 			if (j >= npcbs)
264 				tcp_pcbs[npcbs++] = td->td_tcb;
265 		}
266 		if (npcbs == 0)
267 			exit(0);
268 	}
269 	qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric);
270 	if (jflag) {
271 		for (i = 0;;) {
272 			printf("%lx", (long)tcp_pcbs[i]);
273 			if (++i == npcbs)
274 				break;
275 			fputs(", ", stdout);
276 		}
277 		putchar('\n');
278 	} else {
279 		for (i = 0; i < npcbs; i++) {
280 			printf("\n%lx:\n", (long)tcp_pcbs[i]);
281 			dotrace(tcp_pcbs[i]);
282 		}
283 	}
284 	exit(0);
285 }
286 
287 void
288 dotrace(tcpcb)
289 	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 (kvm_read(kd, nl[N_TCP_DEBX].n_value,
358 			    (char *)&tcp_debx, sizeof(tcp_debx)) !=
359 			    sizeof(tcp_debx))
360 				errx(3, "tcp_debx: %s", kvm_geterr(kd));
361 		} while (tcp_debx == prev_debx);
362 
363 		if (kvm_read(kd, nl[N_TCP_DEBUG].n_value, (char *)tcp_debug,
364 		    sizeof(tcp_debug)) != sizeof(tcp_debug))
365 			errx(3, "tcp_debug: %s", kvm_geterr(kd));
366 
367 		goto again;
368 	}
369 }
370 
371 /*
372  * Tcp debug routines
373  */
374 /*ARGSUSED*/
375 void
376 tcp_trace(act, ostate, atp, tp, family, packet, req)
377 	short act, ostate;
378 	struct tcpcb *atp, *tp;
379 	int family;
380 	void *packet;
381 	int req;
382 {
383 	tcp_seq seq, ack;
384 	int flags, len, win, timer;
385 	struct tcphdr *th = NULL;
386 	struct ip *ip = NULL;
387 #ifdef INET6
388 	struct ip6_hdr *ip6 = NULL;
389 #endif
390 	char hbuf[MAXHOSTNAMELEN];
391 
392 	switch (family) {
393 	case AF_INET:
394 		if (packet) {
395 			ip = (struct ip *)packet;
396 			th = (struct tcphdr *)(ip + 1);
397 		}
398 		break;
399 #ifdef INET6
400 	case AF_INET6:
401 		if (packet) {
402 			ip6 = (struct ip6_hdr *)packet;
403 			th = (struct tcphdr *)(ip6 + 1);
404 		}
405 		break;
406 #endif
407 	default:
408 		return;
409 	}
410 
411 	printf("%03d %s:%s ", (ntime/10) % 1000, tcpstates[ostate],
412 	    tanames[act]);
413 
414 #ifndef INET6
415 	if (!ip)
416 #else
417 	if (!(ip || ip6))
418 #endif
419 		goto skipact;
420 
421 	switch (act) {
422 	case TA_INPUT:
423 	case TA_OUTPUT:
424 	case TA_DROP:
425 		if (aflag) {
426 			inet_ntop(family,
427 #ifndef INET6
428 				(void *)&ip->ip_src,
429 #else
430 				family == AF_INET ? (void *)&ip->ip_src
431 						  : (void *)&ip6->ip6_src,
432 #endif
433 				hbuf, sizeof(hbuf));
434 			printf("(src=%s,%u, ",
435 			    hbuf, ntohs(th->th_sport));
436 			inet_ntop(family,
437 #ifndef INET6
438 				(void *)&ip->ip_dst,
439 #else
440 				family == AF_INET ? (void *)&ip->ip_dst
441 						  : (void *)&ip6->ip6_dst,
442 #endif
443 				hbuf, sizeof(hbuf));
444 			printf("dst=%s,%u)",
445 			    hbuf, ntohs(th->th_dport));
446 		}
447 		seq = th->th_seq;
448 		ack = th->th_ack;
449 		if (ip)
450 			len = ip->ip_len;
451 #ifdef INET6
452 		else if (ip6)
453 			len = ip6->ip6_plen;
454 #endif
455 		win = th->th_win;
456 		if (act == TA_OUTPUT) {
457 			NTOHL(seq);
458 			NTOHL(ack);
459 			NTOHS(len);
460 			NTOHS(win);
461 		}
462 		if (act == TA_OUTPUT)
463 			len -= sizeof(struct tcphdr);
464 		if (len)
465 			printf("[%x..%x)", seq, seq + len);
466 		else
467 			printf("%x", seq);
468 		printf("@%x", ack);
469 		if (win)
470 			printf("(win=%x)", win);
471 		flags = th->th_flags;
472 		if (flags) {
473 			register char *cp = "<";
474 #define	pf(flag, string) { \
475 	if (th->th_flags&flag) { \
476 		(void)printf("%s%s", cp, string); \
477 		cp = ","; \
478 	} \
479 }
480 			pf(TH_SYN, "SYN");
481 			pf(TH_ACK, "ACK");
482 			pf(TH_FIN, "FIN");
483 			pf(TH_RST, "RST");
484 			pf(TH_PUSH, "PUSH");
485 			pf(TH_URG, "URG");
486 			printf(">");
487 		}
488 		break;
489 	case TA_USER:
490 		timer = req >> 8;
491 		req &= 0xff;
492 		printf("%s", prurequests[req]);
493 		if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
494 			printf("<%s>", tcptimers[timer]);
495 		break;
496 	}
497 
498 skipact:
499 	printf(" -> %s", tcpstates[tp->t_state]);
500 	/* print out internal state of tp !?! */
501 	printf("\n");
502 	if (sflag) {
503 		printf("\trcv_nxt %x rcv_wnd %lx snd_una %x snd_nxt %x snd_max %x\n",
504 		    tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt,
505 		    tp->snd_max);
506 		printf("\tsnd_wl1 %x snd_wl2 %x snd_wnd %lx\n", tp->snd_wl1,
507 		    tp->snd_wl2, tp->snd_wnd);
508 	}
509 	/* print out timers? */
510 	if (tflag) {
511 		register char *cp = "\t";
512 		register int i;
513 
514 		for (i = 0; i < TCPT_NTIMERS; i++) {
515 			if ((tp->t_timer[i].c_flags & CALLOUT_ACTIVE) == 0)
516 				continue;
517 			printf("%s%s=%llu", cp, tcptimers[i],
518 			    (unsigned long long) tp->t_timer[i].c_time);
519 			if (i == TCPT_REXMT)
520 				printf(" (t_rxtshft=%d)", tp->t_rxtshift);
521 			cp = ", ";
522 		}
523 		if (*cp != '\t')
524 			putchar('\n');
525 	}
526 }
527 
528 int
529 numeric(v1, v2)
530 	const void *v1, *v2;
531 {
532 	const caddr_t *c1 = v1;
533 	const caddr_t *c2 = v2;
534 	int rv;
535 
536 	if (*c1 < *c2)
537 		rv = -1;
538 	else if (*c1 > *c2)
539 		rv = 1;
540 	else
541 		rv = 0;
542 
543 	return (rv);
544 }
545 
546 void
547 usage()
548 {
549 
550 	(void) fprintf(stderr, "usage: %s [-afjst] [-p hex-address]"
551 	    " [-N system] [-M core]\n", getprogname());
552 	exit(1);
553 }
554