1 /* $OpenBSD: trpt.c,v 1.18 2003/06/02 23:36:55 millert 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 #ifndef lint 70 char copyright[] = 71 "@(#) Copyright (c) 1983, 1988, 1993\n\ 72 The Regents of the University of California. All rights reserved.\n"; 73 #endif /* not lint */ 74 75 #ifndef lint 76 static char sccsid[] = "@(#)trpt.c 8.1 (Berkeley) 6/6/93"; 77 #endif /* not lint */ 78 79 #include <sys/param.h> 80 #include <sys/queue.h> 81 #include <sys/socket.h> 82 #include <sys/socketvar.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 #include <netinet/tcp.h> 96 #define TCPSTATES 97 #include <netinet/tcp_fsm.h> 98 #include <netinet/tcp_seq.h> 99 #define TCPTIMERS 100 #include <netinet/tcp_timer.h> 101 #include <netinet/tcp_var.h> 102 #include <netinet/tcpip.h> 103 #define TANAMES 104 #include <netinet/tcp_debug.h> 105 106 #include <arpa/inet.h> 107 108 #include <err.h> 109 #include <stdio.h> 110 #include <errno.h> 111 #include <kvm.h> 112 #include <nlist.h> 113 #include <paths.h> 114 #include <limits.h> 115 #include <stdlib.h> 116 #include <unistd.h> 117 118 struct nlist nl[] = { 119 #define N_TCP_DEBUG 0 /* no sysctl */ 120 { "_tcp_debug" }, 121 #define N_TCP_DEBX 1 /* no sysctl */ 122 { "_tcp_debx" }, 123 { NULL }, 124 }; 125 126 int tcp_debx; 127 struct tcp_debug tcp_debug[TCP_NDEBUG]; 128 129 static caddr_t tcp_pcbs[TCP_NDEBUG]; 130 static n_time ntime; 131 static int aflag, follow, sflag, tflag; 132 133 extern char *__progname; 134 135 void dotrace(caddr_t); 136 void tcp_trace(short, short, struct tcpcb *, struct tcpcb *, 137 struct tcpiphdr *, struct tcpipv6hdr *, int); 138 int numeric(const void *, const void *); 139 void usage(void); 140 141 kvm_t *kd; 142 143 int 144 main(int argc, char *argv[]) 145 { 146 char *system = NULL, *core = NULL, *cp, errbuf[_POSIX2_LINE_MAX]; 147 int ch, i, jflag = 0, npcbs = 0; 148 unsigned long l; 149 150 while ((ch = getopt(argc, argv, "afjM:N:p:st")) != -1) { 151 switch (ch) { 152 case 'a': 153 ++aflag; 154 break; 155 case 'f': 156 ++follow; 157 setlinebuf(stdout); 158 break; 159 case 'j': 160 ++jflag; 161 break; 162 case 'p': 163 if (npcbs >= TCP_NDEBUG) 164 errx(1, "too many pcbs specified"); 165 errno = 0; 166 l = strtoul(optarg, &cp, 16); 167 tcp_pcbs[npcbs] = (caddr_t)l; 168 if (*optarg == '\0' || *cp != '\0' || errno || 169 (unsigned long)tcp_pcbs[npcbs] != l) 170 errx(1, "invalid address: %s", optarg); 171 npcbs++; 172 break; 173 case 's': 174 ++sflag; 175 break; 176 case 't': 177 ++tflag; 178 break; 179 case 'N': 180 system = optarg; 181 break; 182 case 'M': 183 core = optarg; 184 break; 185 case '?': 186 default: 187 usage(); 188 /* NOTREACHED */ 189 } 190 } 191 argc -= optind; 192 argv += optind; 193 194 if (argc) 195 usage(); 196 197 /* 198 * Discard setgid privileged if not the running kernel so that bad 199 * guys can't print interesting stuff from kernel memory. 200 */ 201 if (core != NULL || system != NULL) { 202 setegid(getgid()); 203 setgid(getgid()); 204 } 205 206 kd = kvm_openfiles(system, core, NULL, O_RDONLY, errbuf); 207 if (kd == NULL) 208 errx(1, "can't open kmem: %s", errbuf); 209 210 setegid(getgid()); 211 setgid(getgid()); 212 213 if (kvm_nlist(kd, nl)) 214 errx(2, "%s: no namelist", system ? system : _PATH_UNIX); 215 216 if (kvm_read(kd, nl[N_TCP_DEBX].n_value, (char *)&tcp_debx, 217 sizeof(tcp_debx)) != sizeof(tcp_debx)) 218 errx(3, "tcp_debx: %s", kvm_geterr(kd)); 219 220 if (kvm_read(kd, nl[N_TCP_DEBUG].n_value, (char *)tcp_debug, 221 sizeof(tcp_debug)) != sizeof(tcp_debug)) 222 errx(3, "tcp_debug: %s", kvm_geterr(kd)); 223 224 /* 225 * If no control blocks have been specified, figure 226 * out how many distinct one we have and summarize 227 * them in tcp_pcbs for sorting the trace records 228 * below. 229 */ 230 if (npcbs == 0) { 231 for (i = 0; i < TCP_NDEBUG; i++) { 232 struct tcp_debug *td = &tcp_debug[i]; 233 int j; 234 235 if (td->td_tcb == 0) 236 continue; 237 for (j = 0; j < npcbs; j++) 238 if (tcp_pcbs[j] == td->td_tcb) 239 break; 240 if (j >= npcbs) 241 tcp_pcbs[npcbs++] = td->td_tcb; 242 } 243 if (npcbs == 0) 244 exit(0); 245 } 246 qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric); 247 if (jflag) { 248 for (i = 0;;) { 249 printf("%lx", (long)tcp_pcbs[i]); 250 if (++i == npcbs) 251 break; 252 fputs(", ", stdout); 253 } 254 putchar('\n'); 255 } else { 256 for (i = 0; i < npcbs; i++) { 257 printf("\n%lx:\n", (long)tcp_pcbs[i]); 258 dotrace(tcp_pcbs[i]); 259 } 260 } 261 exit(0); 262 } 263 264 void 265 dotrace(caddr_t tcpcb) 266 { 267 struct tcp_debug *td; 268 int prev_debx = tcp_debx; 269 int i; 270 271 again: 272 if (--tcp_debx < 0) 273 tcp_debx = TCP_NDEBUG - 1; 274 for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) { 275 td = &tcp_debug[i]; 276 if (tcpcb && td->td_tcb != tcpcb) 277 continue; 278 ntime = ntohl(td->td_time); 279 tcp_trace(td->td_act, td->td_ostate, 280 (struct tcpcb *)td->td_tcb, &td->td_cb, &td->td_ti, 281 &td->td_ti6, td->td_req); 282 if (i == tcp_debx) 283 goto done; 284 } 285 for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) { 286 td = &tcp_debug[i]; 287 if (tcpcb && td->td_tcb != tcpcb) 288 continue; 289 ntime = ntohl(td->td_time); 290 tcp_trace(td->td_act, td->td_ostate, 291 (struct tcpcb *)td->td_tcb, &td->td_cb, &td->td_ti, 292 &td->td_ti6, td->td_req); 293 } 294 done: 295 if (follow) { 296 prev_debx = tcp_debx + 1; 297 if (prev_debx >= TCP_NDEBUG) 298 prev_debx = 0; 299 do { 300 sleep(1); 301 if (kvm_read(kd, nl[N_TCP_DEBX].n_value, 302 (char *)&tcp_debx, sizeof(tcp_debx)) != 303 sizeof(tcp_debx)) 304 errx(3, "tcp_debx: %s", kvm_geterr(kd)); 305 } while (tcp_debx == prev_debx); 306 307 if (kvm_read(kd, nl[N_TCP_DEBUG].n_value, (char *)tcp_debug, 308 sizeof(tcp_debug)) != sizeof(tcp_debug)) 309 errx(3, "tcp_debug: %s", kvm_geterr(kd)); 310 311 goto again; 312 } 313 } 314 315 /* 316 * Tcp debug routines 317 */ 318 /*ARGSUSED*/ 319 void 320 tcp_trace(short act, short ostate, struct tcpcb *atp, 321 struct tcpcb *tp, struct tcpiphdr *ti, struct tcpipv6hdr *ti6, int req) 322 { 323 tcp_seq seq, ack; 324 int flags, len, win, timer; 325 struct tcphdr *th; 326 char hbuf[INET6_ADDRSTRLEN]; 327 328 if (ti->ti_src.s_addr) 329 th = &ti->ti_t; 330 else 331 th = &ti6->ti6_t; 332 333 printf("%03d %s:%s ", (ntime/10) % 1000, tcpstates[ostate], 334 tanames[act]); 335 switch (act) { 336 case TA_INPUT: 337 case TA_OUTPUT: 338 case TA_DROP: 339 if (aflag) { 340 if (ti->ti_src.s_addr) { 341 printf("(src=%s,%u, ", 342 inet_ntoa(ti->ti_src), ntohs(ti->ti_sport)); 343 printf("dst=%s,%u)", 344 inet_ntoa(ti->ti_dst), ntohs(ti->ti_dport)); 345 } else { 346 printf("(src=%s,%u, ", 347 inet_ntop(AF_INET6, &ti6->ti6_src, 348 hbuf, sizeof(hbuf)), ntohs(ti->ti_sport)); 349 printf("dst=%s,%u)", 350 inet_ntop(AF_INET6, &ti6->ti6_dst, 351 hbuf, sizeof(hbuf)), ntohs(ti->ti_dport)); 352 } 353 } 354 seq = th->th_seq; 355 ack = th->th_ack; 356 if (ti->ti_src.s_addr) 357 len = ti->ti_len; 358 else 359 len = ti6->ti6_plen; /*XXX intermediate header*/ 360 win = th->th_win; 361 if (act == TA_OUTPUT) { 362 NTOHL(seq); 363 NTOHL(ack); 364 NTOHS(len); 365 NTOHS(win); 366 } 367 if (act == TA_OUTPUT) 368 len -= sizeof(struct tcphdr); 369 if (len) 370 printf("[%x..%x)", seq, seq + len); 371 else 372 printf("%x", seq); 373 printf("@%x", ack); 374 if (win) 375 printf("(win=%x)", win); 376 flags = th->th_flags; 377 if (flags) { 378 char *cp = "<"; 379 #define pf(flag, string) { \ 380 if (th->th_flags & flag) { \ 381 (void)printf("%s%s", cp, string); \ 382 cp = ","; \ 383 } \ 384 } 385 pf(TH_SYN, "SYN"); 386 pf(TH_ACK, "ACK"); 387 pf(TH_FIN, "FIN"); 388 pf(TH_RST, "RST"); 389 pf(TH_PUSH, "PUSH"); 390 pf(TH_URG, "URG"); 391 printf(">"); 392 } 393 break; 394 case TA_USER: 395 timer = req >> 8; 396 req &= 0xff; 397 printf("%s", prurequests[req]); 398 if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO) 399 printf("<%s>", tcptimers[timer]); 400 break; 401 } 402 printf(" -> %s", tcpstates[tp->t_state]); 403 /* print out internal state of tp !?! */ 404 printf("\n"); 405 if (sflag) { 406 printf("\trcv_nxt %x rcv_wnd %lx snd_una %x snd_nxt %x snd_max %x\n", 407 tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt, 408 tp->snd_max); 409 printf("\tsnd_wl1 %x snd_wl2 %x snd_wnd %lx\n", tp->snd_wl1, 410 tp->snd_wl2, tp->snd_wnd); 411 } 412 /* print out timers? */ 413 if (tflag) { 414 char *cp = "\t"; 415 int i; 416 417 for (i = 0; i < TCPT_NTIMERS; i++) { 418 if (timeout_pending(&tp->t_timer[i])) 419 continue; 420 printf("%s%s=%d", cp, tcptimers[i], 421 tp->t_timer[i].to_time); 422 if (i == TCPT_REXMT) 423 printf(" (t_rxtshft=%d)", tp->t_rxtshift); 424 cp = ", "; 425 } 426 if (*cp != '\t') 427 putchar('\n'); 428 } 429 } 430 431 int 432 numeric(const void *v1, const void *v2) 433 { 434 const caddr_t *c1 = v1; 435 const caddr_t *c2 = v2; 436 int rv; 437 438 if (*c1 < *c2) 439 rv = -1; 440 else if (*c1 > *c2) 441 rv = 1; 442 else 443 rv = 0; 444 445 return (rv); 446 } 447 448 void 449 usage(void) 450 { 451 452 (void) fprintf(stderr, "usage: %s [-afjst] [-p hex-address]" 453 " [-N system] [-M core]\n", __progname); 454 exit(1); 455 } 456