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