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