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