trpt.c revision 1.28
1257853Sjmmv/*	$OpenBSD: trpt.c,v 1.28 2011/07/04 07:06:49 guenther Exp $	*/
2257853Sjmmv
3257853Sjmmv/*-
4257853Sjmmv * Copyright (c) 1997 The NetBSD Foundation, Inc.
5257853Sjmmv * All rights reserved.
6257853Sjmmv *
7257853Sjmmv * This code is derived from software contributed to The NetBSD Foundation
8257853Sjmmv * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9257853Sjmmv * NASA Ames Research Center.
10257853Sjmmv *
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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * Copyright (c) 1983, 1988, 1993
35 *	The Regents of the University of California.  All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 *    notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 *    notice, this list of conditions and the following disclaimer in the
44 *    documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 *    may be used to endorse or promote products derived from this software
47 *    without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 */
61
62#include <sys/param.h>
63#include <sys/queue.h>
64#include <sys/socket.h>
65#define PRUREQUESTS
66#include <sys/protosw.h>
67#define _KERNEL
68#include <sys/timeout.h>		/* to get timeout_pending() and such */
69#undef _KERNEL
70#include <sys/file.h>
71
72#include <net/route.h>
73#include <net/if.h>
74
75#include <netinet/in.h>
76#include <netinet/in_systm.h>
77#include <netinet/ip.h>
78#include <netinet/in_pcb.h>
79#include <netinet/ip_var.h>
80#include <netinet/tcp.h>
81#define TCPSTATES
82#include <netinet/tcp_fsm.h>
83#include <netinet/tcp_seq.h>
84#define	TCPTIMERS
85#include <netinet/tcp_timer.h>
86#include <netinet/tcp_var.h>
87#include <netinet/tcpip.h>
88#define	TANAMES
89#include <netinet/tcp_debug.h>
90
91#include <arpa/inet.h>
92
93#include <err.h>
94#include <stdio.h>
95#include <errno.h>
96#include <kvm.h>
97#include <nlist.h>
98#include <paths.h>
99#include <limits.h>
100#include <stdlib.h>
101#include <unistd.h>
102
103struct nlist nl[] = {
104#define	N_TCP_DEBUG	0		/* no sysctl */
105	{ "_tcp_debug" },
106#define	N_TCP_DEBX	1		/* no sysctl */
107	{ "_tcp_debx" },
108	{ NULL },
109};
110
111int	tcp_debx;
112struct	tcp_debug tcp_debug[TCP_NDEBUG];
113
114static caddr_t tcp_pcbs[TCP_NDEBUG];
115static n_time ntime;
116static int aflag, follow, sflag, tflag;
117
118extern	char *__progname;
119
120void	dotrace(caddr_t);
121void	tcp_trace(short, short, struct tcpcb *, struct tcpiphdr *,
122	    struct tcpipv6hdr *, int);
123int	numeric(const void *, const void *);
124void	usage(void);
125
126kvm_t	*kd;
127
128int
129main(int argc, char *argv[])
130{
131	char *sys = NULL, *core = NULL, *cp, errbuf[_POSIX2_LINE_MAX];
132	int ch, i, jflag = 0, npcbs = 0;
133	unsigned long l;
134	gid_t gid;
135
136	while ((ch = getopt(argc, argv, "afjM:N:p:st")) != -1) {
137		switch (ch) {
138		case 'a':
139			++aflag;
140			break;
141		case 'f':
142			++follow;
143			setlinebuf(stdout);
144			break;
145		case 'j':
146			++jflag;
147			break;
148		case 'p':
149			if (npcbs >= TCP_NDEBUG)
150				errx(1, "too many pcbs specified");
151			errno = 0;
152			l = strtoul(optarg, &cp, 16);
153			tcp_pcbs[npcbs] = (caddr_t)l;
154			if (*optarg == '\0' || *cp != '\0' || errno ||
155			    (unsigned long)tcp_pcbs[npcbs] != l)
156				errx(1, "invalid address: %s", optarg);
157			npcbs++;
158			break;
159		case 's':
160			++sflag;
161			break;
162		case 't':
163			++tflag;
164			break;
165		case 'N':
166			sys = optarg;
167			break;
168		case 'M':
169			core = optarg;
170			break;
171		default:
172			usage();
173			/* NOTREACHED */
174		}
175	}
176	argc -= optind;
177	argv += optind;
178
179	if (argc)
180		usage();
181
182	/*
183	 * Discard setgid privileged if not the running kernel so that bad
184	 * guys can't print interesting stuff from kernel memory.
185	 */
186	gid = getgid();
187	if (core != NULL || sys != NULL)
188		if (setresgid(gid, gid, gid) == -1)
189			err(1, "setresgid");
190
191	kd = kvm_openfiles(sys, core, NULL, O_RDONLY, errbuf);
192	if (kd == NULL)
193		errx(1, "can't open kmem: %s", errbuf);
194
195	if (core == NULL && sys == NULL)
196		if (setresgid(gid, gid, gid) == -1)
197			err(1, "setresgid");
198
199	if (kvm_nlist(kd, nl))
200		errx(2, "%s: no namelist", sys ? sys : _PATH_UNIX);
201
202	if (kvm_read(kd, nl[N_TCP_DEBX].n_value, (char *)&tcp_debx,
203	    sizeof(tcp_debx)) != sizeof(tcp_debx))
204		errx(3, "tcp_debx: %s", kvm_geterr(kd));
205
206	if (kvm_read(kd, nl[N_TCP_DEBUG].n_value, (char *)tcp_debug,
207	    sizeof(tcp_debug)) != sizeof(tcp_debug))
208		errx(3, "tcp_debug: %s", kvm_geterr(kd));
209
210	/*
211	 * If no control blocks have been specified, figure
212	 * out how many distinct one we have and summarize
213	 * them in tcp_pcbs for sorting the trace records
214	 * below.
215	 */
216	if (npcbs == 0) {
217		for (i = 0; i < TCP_NDEBUG; i++) {
218			struct tcp_debug *td = &tcp_debug[i];
219			int j;
220
221			if (td->td_tcb == 0)
222				continue;
223			for (j = 0; j < npcbs; j++)
224				if (tcp_pcbs[j] == td->td_tcb)
225					break;
226			if (j >= npcbs)
227				tcp_pcbs[npcbs++] = td->td_tcb;
228		}
229		if (npcbs == 0)
230			exit(0);
231	}
232	qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric);
233	if (jflag) {
234		for (i = 0;;) {
235			printf("%lx", (long)tcp_pcbs[i]);
236			if (++i == npcbs)
237				break;
238			fputs(", ", stdout);
239		}
240		putchar('\n');
241	} else {
242		for (i = 0; i < npcbs; i++) {
243			printf("\n%lx:\n", (long)tcp_pcbs[i]);
244			dotrace(tcp_pcbs[i]);
245		}
246	}
247	exit(0);
248}
249
250void
251dotrace(caddr_t tcpcb)
252{
253	struct tcp_debug *td;
254	int prev_debx = tcp_debx;
255	int i;
256
257 again:
258	if (--tcp_debx < 0)
259		tcp_debx = TCP_NDEBUG - 1;
260	for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
261		td = &tcp_debug[i];
262		if (tcpcb && td->td_tcb != tcpcb)
263			continue;
264		ntime = ntohl(td->td_time);
265		tcp_trace(td->td_act, td->td_ostate,
266		    &td->td_cb, &td->td_ti,
267		    &td->td_ti6, td->td_req);
268		if (i == tcp_debx)
269			goto done;
270	}
271	for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) {
272		td = &tcp_debug[i];
273		if (tcpcb && td->td_tcb != tcpcb)
274			continue;
275		ntime = ntohl(td->td_time);
276		tcp_trace(td->td_act, td->td_ostate,
277		    &td->td_cb, &td->td_ti,
278		    &td->td_ti6, td->td_req);
279	}
280 done:
281	if (follow) {
282		prev_debx = tcp_debx + 1;
283		if (prev_debx >= TCP_NDEBUG)
284			prev_debx = 0;
285		do {
286			sleep(1);
287			if (kvm_read(kd, nl[N_TCP_DEBX].n_value,
288			    (char *)&tcp_debx, sizeof(tcp_debx)) !=
289			    sizeof(tcp_debx))
290				errx(3, "tcp_debx: %s", kvm_geterr(kd));
291		} while (tcp_debx == prev_debx);
292
293		if (kvm_read(kd, nl[N_TCP_DEBUG].n_value, (char *)tcp_debug,
294		    sizeof(tcp_debug)) != sizeof(tcp_debug))
295			errx(3, "tcp_debug: %s", kvm_geterr(kd));
296
297		goto again;
298	}
299}
300
301/*
302 * Tcp debug routines
303 */
304/*ARGSUSED*/
305void
306tcp_trace(short act, short ostate, struct tcpcb *tp,
307    struct tcpiphdr *ti, struct tcpipv6hdr *ti6, int req)
308{
309	tcp_seq seq, ack;
310	int flags, len, win, timer;
311	struct tcphdr *th;
312	char hbuf[INET6_ADDRSTRLEN];
313
314	if (ti->ti_src.s_addr)
315		th = &ti->ti_t;
316	else
317		th = &ti6->ti6_t;
318
319	printf("%03d %s:%s ", (ntime/10) % 1000, tcpstates[ostate],
320	    tanames[act]);
321	switch (act) {
322	case TA_INPUT:
323	case TA_OUTPUT:
324	case TA_DROP:
325		if (aflag) {
326			if (ti->ti_src.s_addr) {
327				printf("(src=%s,%u, ",
328				    inet_ntoa(ti->ti_src), ntohs(ti->ti_sport));
329				printf("dst=%s,%u)",
330				    inet_ntoa(ti->ti_dst), ntohs(ti->ti_dport));
331			} else {
332				printf("(src=%s,%u, ",
333				    inet_ntop(AF_INET6, &ti6->ti6_src,
334				    hbuf, sizeof(hbuf)), ntohs(ti->ti_sport));
335				printf("dst=%s,%u)",
336				    inet_ntop(AF_INET6, &ti6->ti6_dst,
337				    hbuf, sizeof(hbuf)), ntohs(ti->ti_dport));
338			}
339		}
340		seq = th->th_seq;
341		ack = th->th_ack;
342		if (ti->ti_src.s_addr)
343			len = ti->ti_len;
344		else
345			len = ti6->ti6_plen;	/*XXX intermediate header*/
346		win = th->th_win;
347		if (act == TA_OUTPUT) {
348			NTOHL(seq);
349			NTOHL(ack);
350			NTOHS(win);
351		}
352		if (len)
353			printf("[%x..%x)", seq, seq + len);
354		else
355			printf("%x", seq);
356		printf("@%x", ack);
357		if (win)
358			printf("(win=%x)", win);
359		flags = th->th_flags;
360		if (flags) {
361			char *cp = "<";
362#define	pf(flag, string) { \
363	if (th->th_flags & flag) { \
364		(void)printf("%s%s", cp, string); \
365		cp = ","; \
366	} \
367}
368			pf(TH_SYN, "SYN");
369			pf(TH_ACK, "ACK");
370			pf(TH_FIN, "FIN");
371			pf(TH_RST, "RST");
372			pf(TH_PUSH, "PUSH");
373			pf(TH_URG, "URG");
374			printf(">");
375		}
376		break;
377	case TA_USER:
378		timer = req >> 8;
379		req &= 0xff;
380		printf("%s", prurequests[req]);
381		if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
382			printf("<%s>", tcptimers[timer]);
383		break;
384	}
385	printf(" -> %s", tcpstates[tp->t_state]);
386	/* print out internal state of tp !?! */
387	printf("\n");
388	if (sflag) {
389		printf("\trcv_nxt %x rcv_wnd %lx snd_una %x snd_nxt %x snd_max %x\n",
390		    tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt,
391		    tp->snd_max);
392		printf("\tsnd_wl1 %x snd_wl2 %x snd_wnd %lx\n", tp->snd_wl1,
393		    tp->snd_wl2, tp->snd_wnd);
394	}
395	/* print out timers? */
396	if (tflag) {
397		char *cp = "\t";
398		int i;
399
400		for (i = 0; i < TCPT_NTIMERS; i++) {
401			if (timeout_pending(&tp->t_timer[i]))
402				continue;
403			printf("%s%s=%d", cp, tcptimers[i],
404			    tp->t_timer[i].to_time);
405			if (i == TCPT_REXMT)
406				printf(" (t_rxtshft=%d)", tp->t_rxtshift);
407			cp = ", ";
408		}
409		if (*cp != '\t')
410			putchar('\n');
411	}
412}
413
414int
415numeric(const void *v1, const void *v2)
416{
417	const caddr_t *c1 = v1;
418	const caddr_t *c2 = v2;
419	int rv;
420
421	if (*c1 < *c2)
422		rv = -1;
423	else if (*c1 > *c2)
424		rv = 1;
425	else
426		rv = 0;
427
428	return (rv);
429}
430
431void
432usage(void)
433{
434
435	(void) fprintf(stderr, "usage: %s [-afjst] [-M core]"
436	    " [-N system] [-p hex-address]\n", __progname);
437	exit(1);
438}
439