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