1 /* $NetBSD: trpt.c,v 1.20 2005/06/02 09:44:41 lukem 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.20 2005/06/02 09:44:41 lukem 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 #define PRUREQUESTS 89 #include <sys/protosw.h> 90 #include <sys/file.h> 91 92 #include <net/route.h> 93 #include <net/if.h> 94 95 #include <netinet/in.h> 96 #include <netinet/in_systm.h> 97 #include <netinet/ip.h> 98 #include <netinet/in_pcb.h> 99 #include <netinet/ip_var.h> 100 101 #ifdef INET6 102 #ifndef INET 103 #include <netinet/in.h> 104 #endif 105 #include <netinet/ip6.h> 106 #endif 107 108 #include <netinet/tcp.h> 109 #define TCPSTATES 110 #include <netinet/tcp_fsm.h> 111 #include <netinet/tcp_seq.h> 112 #define TCPTIMERS 113 #include <netinet/tcp_timer.h> 114 #include <netinet/tcp_var.h> 115 #include <netinet/tcpip.h> 116 #define TANAMES 117 #include <netinet/tcp_debug.h> 118 119 #include <arpa/inet.h> 120 121 #include <err.h> 122 #include <stdio.h> 123 #include <errno.h> 124 #include <kvm.h> 125 #include <nlist.h> 126 #include <paths.h> 127 #include <limits.h> 128 #include <stdlib.h> 129 #include <unistd.h> 130 131 struct nlist nl[] = { 132 #define N_HARDCLOCK_TICKS 0 133 { "_hardclock_ticks" }, 134 #define N_TCP_DEBUG 1 135 { "_tcp_debug" }, 136 #define N_TCP_DEBX 2 137 { "_tcp_debx" }, 138 { NULL }, 139 }; 140 141 static caddr_t tcp_pcbs[TCP_NDEBUG]; 142 static n_time ntime; 143 static int aflag, follow, sflag, tflag; 144 145 /* see sys/netinet/tcp_debug.c */ 146 struct tcp_debug tcp_debug[TCP_NDEBUG]; 147 int tcp_debx; 148 149 int main(int, char *[]); 150 void dotrace(caddr_t); 151 void tcp_trace(short, short, struct tcpcb *, struct tcpcb *, 152 int, void *, int); 153 int numeric(const void *, const void *); 154 void usage(void); 155 156 kvm_t *kd; 157 158 int 159 main(int argc, char *argv[]) 160 { 161 int ch, i, jflag, npcbs; 162 char *system, *core, *cp, errbuf[_POSIX2_LINE_MAX]; 163 gid_t egid = getegid(); 164 unsigned long l; 165 166 (void)setegid(getgid()); 167 system = core = NULL; 168 169 jflag = npcbs = 0; 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 /* 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(caddr_t tcpcb) 289 { 290 struct tcp_debug *td; 291 int prev_debx = tcp_debx; 292 int i; 293 294 again: 295 if (--tcp_debx < 0) 296 tcp_debx = TCP_NDEBUG - 1; 297 for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) { 298 td = &tcp_debug[i]; 299 if (tcpcb && td->td_tcb != tcpcb) 300 continue; 301 ntime = ntohl(td->td_time); 302 switch (td->td_family) { 303 case AF_INET: 304 tcp_trace(td->td_act, td->td_ostate, 305 (struct tcpcb *)td->td_tcb, &td->td_cb, 306 td->td_family, &td->td_ti, td->td_req); 307 break; 308 #ifdef INET6 309 case AF_INET6: 310 tcp_trace(td->td_act, td->td_ostate, 311 (struct tcpcb *)td->td_tcb, &td->td_cb, 312 td->td_family, &td->td_ti6, td->td_req); 313 break; 314 #endif 315 default: 316 tcp_trace(td->td_act, td->td_ostate, 317 (struct tcpcb *)td->td_tcb, &td->td_cb, 318 td->td_family, NULL, td->td_req); 319 break; 320 } 321 if (i == tcp_debx) 322 goto done; 323 } 324 for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) { 325 td = &tcp_debug[i]; 326 if (tcpcb && td->td_tcb != tcpcb) 327 continue; 328 ntime = ntohl(td->td_time); 329 switch (td->td_family) { 330 case AF_INET: 331 tcp_trace(td->td_act, td->td_ostate, 332 (struct tcpcb *)td->td_tcb, &td->td_cb, 333 td->td_family, &td->td_ti, td->td_req); 334 break; 335 #ifdef INET6 336 case AF_INET6: 337 tcp_trace(td->td_act, td->td_ostate, 338 (struct tcpcb *)td->td_tcb, &td->td_cb, 339 td->td_family, &td->td_ti6, td->td_req); 340 break; 341 #endif 342 default: 343 tcp_trace(td->td_act, td->td_ostate, 344 (struct tcpcb *)td->td_tcb, &td->td_cb, 345 td->td_family, NULL, td->td_req); 346 break; 347 } 348 } 349 done: 350 if (follow) { 351 prev_debx = tcp_debx + 1; 352 if (prev_debx >= TCP_NDEBUG) 353 prev_debx = 0; 354 do { 355 sleep(1); 356 if (kvm_read(kd, nl[N_TCP_DEBX].n_value, 357 (char *)&tcp_debx, sizeof(tcp_debx)) != 358 sizeof(tcp_debx)) 359 errx(3, "tcp_debx: %s", kvm_geterr(kd)); 360 } while (tcp_debx == prev_debx); 361 362 if (kvm_read(kd, nl[N_TCP_DEBUG].n_value, (char *)tcp_debug, 363 sizeof(tcp_debug)) != sizeof(tcp_debug)) 364 errx(3, "tcp_debug: %s", kvm_geterr(kd)); 365 366 goto again; 367 } 368 } 369 370 /* 371 * Tcp debug routines 372 */ 373 /*ARGSUSED*/ 374 void 375 tcp_trace(short act, short ostate, struct tcpcb *atp, struct tcpcb *tp, 376 int family, void *packet, int req) 377 { 378 tcp_seq seq, ack; 379 int flags, len, win, timer; 380 struct tcphdr *th = NULL; 381 struct ip *ip = NULL; 382 #ifdef INET6 383 struct ip6_hdr *ip6 = NULL; 384 #endif 385 char hbuf[MAXHOSTNAMELEN]; 386 387 len = 0; /* XXXGCC -Wuninitialized */ 388 389 switch (family) { 390 case AF_INET: 391 if (packet) { 392 ip = (struct ip *)packet; 393 th = (struct tcphdr *)(ip + 1); 394 } 395 break; 396 #ifdef INET6 397 case AF_INET6: 398 if (packet) { 399 ip6 = (struct ip6_hdr *)packet; 400 th = (struct tcphdr *)(ip6 + 1); 401 } 402 break; 403 #endif 404 default: 405 return; 406 } 407 408 printf("%03d %s:%s ", (ntime/10) % 1000, tcpstates[ostate], 409 tanames[act]); 410 411 #ifndef INET6 412 if (!ip) 413 #else 414 if (!(ip || ip6)) 415 #endif 416 goto skipact; 417 418 switch (act) { 419 case TA_INPUT: 420 case TA_OUTPUT: 421 case TA_DROP: 422 if (aflag) { 423 inet_ntop(family, 424 #ifndef INET6 425 (void *)&ip->ip_src, 426 #else 427 family == AF_INET ? (void *)&ip->ip_src 428 : (void *)&ip6->ip6_src, 429 #endif 430 hbuf, sizeof(hbuf)); 431 printf("(src=%s,%u, ", 432 hbuf, ntohs(th->th_sport)); 433 inet_ntop(family, 434 #ifndef INET6 435 (void *)&ip->ip_dst, 436 #else 437 family == AF_INET ? (void *)&ip->ip_dst 438 : (void *)&ip6->ip6_dst, 439 #endif 440 hbuf, sizeof(hbuf)); 441 printf("dst=%s,%u)", 442 hbuf, ntohs(th->th_dport)); 443 } 444 seq = th->th_seq; 445 ack = th->th_ack; 446 if (ip) 447 len = ip->ip_len; 448 #ifdef INET6 449 else if (ip6) 450 len = ip6->ip6_plen; 451 #endif 452 win = th->th_win; 453 if (act == TA_OUTPUT) { 454 NTOHL(seq); 455 NTOHL(ack); 456 NTOHS(len); 457 NTOHS(win); 458 } 459 if (act == TA_OUTPUT) 460 len -= sizeof(struct tcphdr); 461 if (len) 462 printf("[%x..%x)", seq, seq + len); 463 else 464 printf("%x", seq); 465 printf("@%x", ack); 466 if (win) 467 printf("(win=%x)", win); 468 flags = th->th_flags; 469 if (flags) { 470 char *cp = "<"; 471 #define pf(flag, string) { \ 472 if (th->th_flags&flag) { \ 473 (void)printf("%s%s", cp, string); \ 474 cp = ","; \ 475 } \ 476 } 477 pf(TH_SYN, "SYN"); 478 pf(TH_ACK, "ACK"); 479 pf(TH_FIN, "FIN"); 480 pf(TH_RST, "RST"); 481 pf(TH_PUSH, "PUSH"); 482 pf(TH_URG, "URG"); 483 printf(">"); 484 } 485 break; 486 case TA_USER: 487 timer = req >> 8; 488 req &= 0xff; 489 printf("%s", prurequests[req]); 490 if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO) 491 printf("<%s>", tcptimers[timer]); 492 break; 493 } 494 495 skipact: 496 printf(" -> %s", tcpstates[tp->t_state]); 497 /* print out internal state of tp !?! */ 498 printf("\n"); 499 if (sflag) { 500 printf("\trcv_nxt %x rcv_wnd %lx snd_una %x snd_nxt %x snd_max %x\n", 501 tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt, 502 tp->snd_max); 503 printf("\tsnd_wl1 %x snd_wl2 %x snd_wnd %lx\n", tp->snd_wl1, 504 tp->snd_wl2, tp->snd_wnd); 505 } 506 /* print out timers? */ 507 if (tflag) { 508 char *cp = "\t"; 509 int i; 510 int hardticks; 511 512 if (kvm_read(kd, nl[N_HARDCLOCK_TICKS].n_value, 513 (char *)&hardticks, sizeof(hardticks)) != sizeof(hardticks)) 514 errx(3, "hardclock_ticks: %s", kvm_geterr(kd)); 515 516 for (i = 0; i < TCPT_NTIMERS; i++) { 517 if ((tp->t_timer[i].c_flags & CALLOUT_PENDING) == 0) 518 continue; 519 printf("%s%s=%d", cp, tcptimers[i], 520 tp->t_timer[i].c_time - hardticks); 521 if (i == TCPT_REXMT) 522 printf(" (t_rxtshft=%d)", tp->t_rxtshift); 523 cp = ", "; 524 } 525 if (*cp != '\t') 526 putchar('\n'); 527 } 528 } 529 530 int 531 numeric(const void *v1, const void *v2) 532 { 533 const caddr_t *c1 = v1; 534 const caddr_t *c2 = v2; 535 int rv; 536 537 if (*c1 < *c2) 538 rv = -1; 539 else if (*c1 > *c2) 540 rv = 1; 541 else 542 rv = 0; 543 544 return (rv); 545 } 546 547 void 548 usage(void) 549 { 550 551 (void) fprintf(stderr, "usage: %s [-afjst] [-p hex-address]" 552 " [-N system] [-M core]\n", getprogname()); 553 exit(1); 554 } 555