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