1 /* 2 * Copyright (c) 1983, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char copyright[] = 10 "@(#) Copyright (c) 1983, 1988, 1993\n\ 11 The Regents of the University of California. All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)trpt.c 8.1 (Berkeley) 06/06/93"; 16 #endif /* not lint */ 17 18 #include <sys/param.h> 19 #if BSD >= 199103 20 #define NEWVM 21 #endif 22 #ifndef NEWVM 23 #include <machine/pte.h> 24 #include <sys/vmmac.h> 25 #endif 26 #include <sys/socket.h> 27 #include <sys/socketvar.h> 28 #define PRUREQUESTS 29 #include <sys/protosw.h> 30 #include <sys/file.h> 31 32 #include <net/route.h> 33 #include <net/if.h> 34 35 #include <netinet/in.h> 36 #include <netinet/in_systm.h> 37 #include <netinet/ip.h> 38 #include <netinet/in_pcb.h> 39 #include <netinet/ip_var.h> 40 #include <netinet/tcp.h> 41 #define TCPSTATES 42 #include <netinet/tcp_fsm.h> 43 #include <netinet/tcp_seq.h> 44 #define TCPTIMERS 45 #include <netinet/tcp_timer.h> 46 #include <netinet/tcp_var.h> 47 #include <netinet/tcpip.h> 48 #define TANAMES 49 #include <netinet/tcp_debug.h> 50 51 #include <arpa/inet.h> 52 53 #include <stdio.h> 54 #include <errno.h> 55 #include <nlist.h> 56 #include <paths.h> 57 58 struct nlist nl[] = { 59 #define N_TCP_DEBUG 0 60 { "_tcp_debug" }, 61 #define N_TCP_DEBX 1 62 { "_tcp_debx" }, 63 #ifndef NEWVM 64 #define N_SYSMAP 2 65 { "_Sysmap" }, 66 #define N_SYSSIZE 3 67 { "_Syssize" }, 68 #endif 69 { "" }, 70 }; 71 72 #ifndef NEWVM 73 static struct pte *Sysmap; 74 #endif 75 static caddr_t tcp_pcbs[TCP_NDEBUG]; 76 static n_time ntime; 77 static int aflag, kflag, memf, follow, sflag, tflag; 78 79 main(argc, argv) 80 int argc; 81 char **argv; 82 { 83 extern char *optarg; 84 extern int optind; 85 int ch, i, jflag, npcbs, numeric(); 86 char *system, *core, *malloc(); 87 off_t lseek(); 88 89 jflag = npcbs = 0; 90 while ((ch = getopt(argc, argv, "afjp:st")) != EOF) 91 switch (ch) { 92 case 'a': 93 ++aflag; 94 break; 95 case 'f': 96 ++follow; 97 setlinebuf(stdout); 98 break; 99 case 'j': 100 ++jflag; 101 break; 102 case 'p': 103 if (npcbs >= TCP_NDEBUG) { 104 fputs("trpt: too many pcb's specified\n", 105 stderr); 106 exit(1); 107 } 108 (void)sscanf(optarg, "%x", (int *)&tcp_pcbs[npcbs++]); 109 break; 110 case 's': 111 ++sflag; 112 break; 113 case 't': 114 ++tflag; 115 break; 116 case '?': 117 default: 118 (void)fprintf(stderr, 119 "usage: trpt [-afjst] [-p hex-address] [system [core]]\n"); 120 exit(1); 121 } 122 argc -= optind; 123 argv += optind; 124 125 core = _PATH_KMEM; 126 if (argc > 0) { 127 system = *argv; 128 argc--, argv++; 129 if (argc > 0) { 130 core = *argv; 131 argc--, argv++; 132 ++kflag; 133 } 134 /* 135 * Discard setgid privileges if not the running kernel so that 136 * bad guys can't print interesting stuff from kernel memory. 137 */ 138 setgid(getgid()); 139 } 140 else 141 system = _PATH_UNIX; 142 143 if (nlist(system, nl) < 0 || !nl[0].n_value) { 144 fprintf(stderr, "trpt: %s: no namelist\n", system); 145 exit(1); 146 } 147 if ((memf = open(core, O_RDONLY)) < 0) { 148 perror(core); 149 exit(2); 150 } 151 if (kflag) { 152 #ifdef NEWVM 153 fputs("trpt: can't do core files yet\n", stderr); 154 exit(1); 155 #else 156 off_t off; 157 158 Sysmap = (struct pte *) 159 malloc((u_int)(nl[N_SYSSIZE].n_value * sizeof(struct pte))); 160 if (!Sysmap) { 161 fputs("trpt: can't get memory for Sysmap.\n", stderr); 162 exit(1); 163 } 164 off = nl[N_SYSMAP].n_value & ~KERNBASE; 165 (void)lseek(memf, off, L_SET); 166 (void)read(memf, (char *)Sysmap, 167 (int)(nl[N_SYSSIZE].n_value * sizeof(struct pte))); 168 #endif 169 } 170 (void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET); 171 if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) != 172 sizeof(tcp_debx)) { 173 perror("trpt: tcp_debx"); 174 exit(3); 175 } 176 (void)klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET); 177 if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) != 178 sizeof(tcp_debug)) { 179 perror("trpt: tcp_debug"); 180 exit(3); 181 } 182 /* 183 * If no control blocks have been specified, figure 184 * out how many distinct one we have and summarize 185 * them in tcp_pcbs for sorting the trace records 186 * below. 187 */ 188 if (!npcbs) { 189 for (i = 0; i < TCP_NDEBUG; i++) { 190 register struct tcp_debug *td = &tcp_debug[i]; 191 register int j; 192 193 if (td->td_tcb == 0) 194 continue; 195 for (j = 0; j < npcbs; j++) 196 if (tcp_pcbs[j] == td->td_tcb) 197 break; 198 if (j >= npcbs) 199 tcp_pcbs[npcbs++] = td->td_tcb; 200 } 201 if (!npcbs) 202 exit(0); 203 } 204 qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric); 205 if (jflag) { 206 for (i = 0;;) { 207 printf("%x", (int)tcp_pcbs[i]); 208 if (++i == npcbs) 209 break; 210 fputs(", ", stdout); 211 } 212 putchar('\n'); 213 } 214 else for (i = 0; i < npcbs; i++) { 215 printf("\n%x:\n", (int)tcp_pcbs[i]); 216 dotrace(tcp_pcbs[i]); 217 } 218 exit(0); 219 } 220 221 dotrace(tcpcb) 222 register caddr_t tcpcb; 223 { 224 register struct tcp_debug *td; 225 register int i; 226 int prev_debx = tcp_debx; 227 228 again: if (--tcp_debx < 0) 229 tcp_debx = TCP_NDEBUG - 1; 230 for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) { 231 td = &tcp_debug[i]; 232 if (tcpcb && td->td_tcb != tcpcb) 233 continue; 234 ntime = ntohl(td->td_time); 235 tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb, 236 &td->td_ti, td->td_req); 237 if (i == tcp_debx) 238 goto done; 239 } 240 for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) { 241 td = &tcp_debug[i]; 242 if (tcpcb && td->td_tcb != tcpcb) 243 continue; 244 ntime = ntohl(td->td_time); 245 tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb, 246 &td->td_ti, td->td_req); 247 } 248 done: if (follow) { 249 prev_debx = tcp_debx + 1; 250 if (prev_debx >= TCP_NDEBUG) 251 prev_debx = 0; 252 do { 253 sleep(1); 254 (void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET); 255 if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) != 256 sizeof(tcp_debx)) { 257 perror("trpt: tcp_debx"); 258 exit(3); 259 } 260 } while (tcp_debx == prev_debx); 261 (void)klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET); 262 if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) != 263 sizeof(tcp_debug)) { 264 perror("trpt: tcp_debug"); 265 exit(3); 266 } 267 goto again; 268 } 269 } 270 271 /* 272 * Tcp debug routines 273 */ 274 /*ARGSUSED*/ 275 tcp_trace(act, ostate, atp, tp, ti, req) 276 short act, ostate; 277 struct tcpcb *atp, *tp; 278 struct tcpiphdr *ti; 279 int req; 280 { 281 tcp_seq seq, ack; 282 int flags, len, win, timer; 283 284 printf("%03ld %s:%s ",(ntime/10) % 1000, tcpstates[ostate], 285 tanames[act]); 286 switch (act) { 287 case TA_INPUT: 288 case TA_OUTPUT: 289 case TA_DROP: 290 if (aflag) { 291 printf("(src=%s,%u, ", 292 inet_ntoa(ti->ti_src), ntohs(ti->ti_sport)); 293 printf("dst=%s,%u)", 294 inet_ntoa(ti->ti_dst), ntohs(ti->ti_dport)); 295 } 296 seq = ti->ti_seq; 297 ack = ti->ti_ack; 298 len = ti->ti_len; 299 win = ti->ti_win; 300 if (act == TA_OUTPUT) { 301 seq = ntohl(seq); 302 ack = ntohl(ack); 303 len = ntohs(len); 304 win = ntohs(win); 305 } 306 if (act == TA_OUTPUT) 307 len -= sizeof(struct tcphdr); 308 if (len) 309 printf("[%lx..%lx)", seq, seq + len); 310 else 311 printf("%lx", seq); 312 printf("@%lx", ack); 313 if (win) 314 printf("(win=%x)", win); 315 flags = ti->ti_flags; 316 if (flags) { 317 register char *cp = "<"; 318 #define pf(flag, string) { \ 319 if (ti->ti_flags&flag) { \ 320 (void)printf("%s%s", cp, string); \ 321 cp = ","; \ 322 } \ 323 } 324 pf(TH_SYN, "SYN"); 325 pf(TH_ACK, "ACK"); 326 pf(TH_FIN, "FIN"); 327 pf(TH_RST, "RST"); 328 pf(TH_PUSH, "PUSH"); 329 pf(TH_URG, "URG"); 330 printf(">"); 331 } 332 break; 333 case TA_USER: 334 timer = req >> 8; 335 req &= 0xff; 336 printf("%s", prurequests[req]); 337 if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO) 338 printf("<%s>", tcptimers[timer]); 339 break; 340 } 341 printf(" -> %s", tcpstates[tp->t_state]); 342 /* print out internal state of tp !?! */ 343 printf("\n"); 344 if (sflag) { 345 printf("\trcv_nxt %lx rcv_wnd %x snd_una %lx snd_nxt %lx snd_max %lx\n", 346 tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt, 347 tp->snd_max); 348 printf("\tsnd_wl1 %lx snd_wl2 %lx snd_wnd %x\n", tp->snd_wl1, 349 tp->snd_wl2, tp->snd_wnd); 350 } 351 /* print out timers? */ 352 if (tflag) { 353 register char *cp = "\t"; 354 register int i; 355 356 for (i = 0; i < TCPT_NTIMERS; i++) { 357 if (tp->t_timer[i] == 0) 358 continue; 359 printf("%s%s=%d", cp, tcptimers[i], tp->t_timer[i]); 360 if (i == TCPT_REXMT) 361 printf(" (t_rxtshft=%d)", tp->t_rxtshift); 362 cp = ", "; 363 } 364 if (*cp != '\t') 365 putchar('\n'); 366 } 367 } 368 369 numeric(c1, c2) 370 caddr_t *c1, *c2; 371 { 372 return(*c1 - *c2); 373 } 374 375 klseek(fd, base, off) 376 int fd, off; 377 off_t base; 378 { 379 off_t lseek(); 380 381 #ifndef NEWVM 382 if (kflag) { /* get kernel pte */ 383 base &= ~KERNBASE; 384 base = ctob(Sysmap[btop(base)].pg_pfnum) + (base & PGOFSET); 385 } 386 #endif 387 (void)lseek(fd, base, off); 388 } 389