Commit | Line | Data |
---|---|---|
5ff67f98 | 1 | /* |
ad787160 C |
2 | * Copyright (c) 1983, 1988, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
f157d52e | 4 | * |
ad787160 C |
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. | |
5ff67f98 DF |
32 | */ |
33 | ||
34 | #ifndef lint | |
ad787160 C |
35 | static char copyright[] = |
36 | "@(#) Copyright (c) 1983, 1988, 1993\n\ | |
37 | The Regents of the University of California. All rights reserved.\n"; | |
7c5704a5 | 38 | #endif /* not lint */ |
5ff67f98 | 39 | |
9a3a7863 | 40 | #ifndef lint |
ad787160 | 41 | static char sccsid[] = "@(#)trpt.c 8.1 (Berkeley) 6/6/93"; |
7c5704a5 KB |
42 | #endif /* not lint */ |
43 | ||
9a3a7863 | 44 | #include <sys/param.h> |
eff65f87 MK |
45 | #if BSD >= 199103 |
46 | #define NEWVM | |
47 | #endif | |
48 | #ifndef NEWVM | |
49 | #include <machine/pte.h> | |
7c5704a5 | 50 | #include <sys/vmmac.h> |
eff65f87 | 51 | #endif |
9a3a7863 BJ |
52 | #include <sys/socket.h> |
53 | #include <sys/socketvar.h> | |
54 | #define PRUREQUESTS | |
55 | #include <sys/protosw.h> | |
7c5704a5 | 56 | #include <sys/file.h> |
94a2d2a7 | 57 | |
9a3a7863 | 58 | #include <net/route.h> |
9a3a7863 | 59 | #include <net/if.h> |
94a2d2a7 SL |
60 | |
61 | #include <netinet/in.h> | |
94a2d2a7 SL |
62 | #include <netinet/in_systm.h> |
63 | #include <netinet/ip.h> | |
04ac9cf7 | 64 | #include <netinet/in_pcb.h> |
94a2d2a7 SL |
65 | #include <netinet/ip_var.h> |
66 | #include <netinet/tcp.h> | |
9a3a7863 | 67 | #define TCPSTATES |
94a2d2a7 SL |
68 | #include <netinet/tcp_fsm.h> |
69 | #include <netinet/tcp_seq.h> | |
9a3a7863 | 70 | #define TCPTIMERS |
94a2d2a7 SL |
71 | #include <netinet/tcp_timer.h> |
72 | #include <netinet/tcp_var.h> | |
73 | #include <netinet/tcpip.h> | |
9a3a7863 | 74 | #define TANAMES |
94a2d2a7 | 75 | #include <netinet/tcp_debug.h> |
9a3a7863 | 76 | |
7d3cb675 SL |
77 | #include <arpa/inet.h> |
78 | ||
eb0fbeab | 79 | #include <stdio.h> |
94a2d2a7 | 80 | #include <errno.h> |
9a3a7863 | 81 | #include <nlist.h> |
7abf8d65 | 82 | #include <paths.h> |
9a3a7863 | 83 | |
7c5704a5 KB |
84 | struct nlist nl[] = { |
85 | #define N_TCP_DEBUG 0 | |
9a3a7863 | 86 | { "_tcp_debug" }, |
7c5704a5 | 87 | #define N_TCP_DEBX 1 |
9a3a7863 | 88 | { "_tcp_debx" }, |
eff65f87 | 89 | #ifndef NEWVM |
7c5704a5 KB |
90 | #define N_SYSMAP 2 |
91 | { "_Sysmap" }, | |
92 | #define N_SYSSIZE 3 | |
93 | { "_Syssize" }, | |
eff65f87 | 94 | #endif |
7c5704a5 | 95 | { "" }, |
9a3a7863 | 96 | }; |
7c5704a5 | 97 | |
eff65f87 | 98 | #ifndef NEWVM |
bb1dac50 | 99 | static struct pte *Sysmap; |
eff65f87 | 100 | #endif |
bb1dac50 KB |
101 | static caddr_t tcp_pcbs[TCP_NDEBUG]; |
102 | static n_time ntime; | |
103 | static int aflag, kflag, memf, follow, sflag, tflag; | |
9a3a7863 BJ |
104 | |
105 | main(argc, argv) | |
106 | int argc; | |
107 | char **argv; | |
108 | { | |
7c5704a5 KB |
109 | extern char *optarg; |
110 | extern int optind; | |
111 | int ch, i, jflag, npcbs, numeric(); | |
112 | char *system, *core, *malloc(); | |
113 | off_t lseek(); | |
9a3a7863 | 114 | |
7c5704a5 KB |
115 | jflag = npcbs = 0; |
116 | while ((ch = getopt(argc, argv, "afjp:st")) != EOF) | |
eff65f87 | 117 | switch (ch) { |
7c5704a5 KB |
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: | |
383be14d KB |
144 | (void)fprintf(stderr, |
145 | "usage: trpt [-afjst] [-p hex-address] [system [core]]\n"); | |
175d35a6 SL |
146 | exit(1); |
147 | } | |
7c5704a5 KB |
148 | argc -= optind; |
149 | argv += optind; | |
150 | ||
383be14d | 151 | core = _PATH_KMEM; |
eb0fbeab SL |
152 | if (argc > 0) { |
153 | system = *argv; | |
154 | argc--, argv++; | |
7c5704a5 KB |
155 | if (argc > 0) { |
156 | core = *argv; | |
157 | argc--, argv++; | |
158 | ++kflag; | |
159 | } | |
5c5d1b50 KB |
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()); | |
eb0fbeab | 165 | } |
7c5704a5 | 166 | else |
383be14d | 167 | system = _PATH_UNIX; |
7c5704a5 KB |
168 | |
169 | if (nlist(system, nl) < 0 || !nl[0].n_value) { | |
eb0fbeab | 170 | fprintf(stderr, "trpt: %s: no namelist\n", system); |
9a3a7863 BJ |
171 | exit(1); |
172 | } | |
7c5704a5 KB |
173 | if ((memf = open(core, O_RDONLY)) < 0) { |
174 | perror(core); | |
eb0fbeab SL |
175 | exit(2); |
176 | } | |
7c5704a5 | 177 | if (kflag) { |
eff65f87 MK |
178 | #ifdef NEWVM |
179 | fputs("trpt: can't do core files yet\n", stderr); | |
180 | exit(1); | |
181 | #else | |
7c5704a5 KB |
182 | off_t off; |
183 | ||
184 | Sysmap = (struct pte *) | |
185 | malloc((u_int)(nl[N_SYSSIZE].n_value * sizeof(struct pte))); | |
186 | if (!Sysmap) { | |
eff65f87 | 187 | fputs("trpt: can't get memory for Sysmap.\n", stderr); |
7c5704a5 KB |
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))); | |
eff65f87 | 194 | #endif |
9a3a7863 | 195 | } |
7c5704a5 KB |
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"); | |
eb0fbeab SL |
200 | exit(3); |
201 | } | |
7c5704a5 KB |
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"); | |
eb0fbeab SL |
206 | exit(3); |
207 | } | |
175d35a6 SL |
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 | */ | |
7c5704a5 | 214 | if (!npcbs) { |
175d35a6 | 215 | for (i = 0; i < TCP_NDEBUG; i++) { |
175d35a6 | 216 | register struct tcp_debug *td = &tcp_debug[i]; |
7c5704a5 | 217 | register int j; |
175d35a6 SL |
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 | } | |
7c5704a5 KB |
227 | if (!npcbs) |
228 | exit(0); | |
175d35a6 | 229 | } |
7c5704a5 | 230 | qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric); |
175d35a6 | 231 | if (jflag) { |
7c5704a5 KB |
232 | for (i = 0;;) { |
233 | printf("%x", (int)tcp_pcbs[i]); | |
234 | if (++i == npcbs) | |
235 | break; | |
236 | fputs(", ", stdout); | |
175d35a6 | 237 | } |
7c5704a5 | 238 | putchar('\n'); |
175d35a6 | 239 | } |
7c5704a5 KB |
240 | else for (i = 0; i < npcbs; i++) { |
241 | printf("\n%x:\n", (int)tcp_pcbs[i]); | |
175d35a6 SL |
242 | dotrace(tcp_pcbs[i]); |
243 | } | |
244 | exit(0); | |
245 | } | |
246 | ||
247 | dotrace(tcpcb) | |
248 | register caddr_t tcpcb; | |
249 | { | |
175d35a6 | 250 | register struct tcp_debug *td; |
7c5704a5 | 251 | register int i; |
89586647 | 252 | int prev_debx = tcp_debx; |
175d35a6 | 253 | |
7c5704a5 | 254 | again: if (--tcp_debx < 0) |
89586647 MK |
255 | tcp_debx = TCP_NDEBUG - 1; |
256 | for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) { | |
175d35a6 | 257 | td = &tcp_debug[i]; |
eb0fbeab SL |
258 | if (tcpcb && td->td_tcb != tcpcb) |
259 | continue; | |
10ce0f6a | 260 | ntime = ntohl(td->td_time); |
9a3a7863 BJ |
261 | tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb, |
262 | &td->td_ti, td->td_req); | |
89586647 MK |
263 | if (i == tcp_debx) |
264 | goto done; | |
9a3a7863 | 265 | } |
89586647 | 266 | for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) { |
175d35a6 | 267 | td = &tcp_debug[i]; |
eb0fbeab SL |
268 | if (tcpcb && td->td_tcb != tcpcb) |
269 | continue; | |
10ce0f6a | 270 | ntime = ntohl(td->td_time); |
9a3a7863 BJ |
271 | tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb, |
272 | &td->td_ti, td->td_req); | |
273 | } | |
7c5704a5 KB |
274 | done: 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"); | |
89586647 MK |
291 | exit(3); |
292 | } | |
7c5704a5 | 293 | goto again; |
89586647 | 294 | } |
9a3a7863 BJ |
295 | } |
296 | ||
297 | /* | |
298 | * Tcp debug routines | |
299 | */ | |
7c5704a5 | 300 | /*ARGSUSED*/ |
9a3a7863 BJ |
301 | tcp_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; | |
7c5704a5 | 308 | int flags, len, win, timer; |
9a3a7863 | 309 | |
7c5704a5 KB |
310 | printf("%03ld %s:%s ",(ntime/10) % 1000, tcpstates[ostate], |
311 | tanames[act]); | |
9a3a7863 | 312 | switch (act) { |
9a3a7863 BJ |
313 | case TA_INPUT: |
314 | case TA_OUTPUT: | |
3cd6f35b | 315 | case TA_DROP: |
368686ad SL |
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 | } | |
9a3a7863 BJ |
322 | seq = ti->ti_seq; |
323 | ack = ti->ti_ack; | |
324 | len = ti->ti_len; | |
325 | win = ti->ti_win; | |
9a3a7863 BJ |
326 | if (act == TA_OUTPUT) { |
327 | seq = ntohl(seq); | |
328 | ack = ntohl(ack); | |
329 | len = ntohs(len); | |
330 | win = ntohs(win); | |
331 | } | |
9a3a7863 | 332 | if (act == TA_OUTPUT) |
7c5704a5 | 333 | len -= sizeof(struct tcphdr); |
9a3a7863 | 334 | if (len) |
7c5704a5 | 335 | printf("[%lx..%lx)", seq, seq + len); |
9a3a7863 | 336 | else |
7c5704a5 KB |
337 | printf("%lx", seq); |
338 | printf("@%lx", ack); | |
9a3a7863 | 339 | if (win) |
23c416d7 | 340 | printf("(win=%x)", win); |
9a3a7863 | 341 | flags = ti->ti_flags; |
4657a7f6 SL |
342 | if (flags) { |
343 | register char *cp = "<"; | |
3ea8f991 KB |
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"); | |
9a3a7863 BJ |
356 | printf(">"); |
357 | } | |
358 | break; | |
9a3a7863 | 359 | case TA_USER: |
10ce0f6a SL |
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]); | |
9a3a7863 BJ |
365 | break; |
366 | } | |
367 | printf(" -> %s", tcpstates[tp->t_state]); | |
368 | /* print out internal state of tp !?! */ | |
369 | printf("\n"); | |
370 | if (sflag) { | |
7c5704a5 | 371 | printf("\trcv_nxt %lx rcv_wnd %x snd_una %lx snd_nxt %lx snd_max %lx\n", |
eb0fbeab SL |
372 | tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt, |
373 | tp->snd_max); | |
7c5704a5 | 374 | printf("\tsnd_wl1 %lx snd_wl2 %lx snd_wnd %x\n", tp->snd_wl1, |
eb0fbeab | 375 | tp->snd_wl2, tp->snd_wnd); |
9a3a7863 | 376 | } |
175d35a6 SL |
377 | /* print out timers? */ |
378 | if (tflag) { | |
7c5704a5 | 379 | register char *cp = "\t"; |
175d35a6 SL |
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 | } | |
9a3a7863 BJ |
393 | } |
394 | ||
7c5704a5 KB |
395 | numeric(c1, c2) |
396 | caddr_t *c1, *c2; | |
9a3a7863 | 397 | { |
7c5704a5 | 398 | return(*c1 - *c2); |
9a3a7863 | 399 | } |
175d35a6 | 400 | |
7c5704a5 KB |
401 | klseek(fd, base, off) |
402 | int fd, off; | |
403 | off_t base; | |
175d35a6 | 404 | { |
7c5704a5 KB |
405 | off_t lseek(); |
406 | ||
eff65f87 | 407 | #ifndef NEWVM |
7c5704a5 KB |
408 | if (kflag) { /* get kernel pte */ |
409 | base &= ~KERNBASE; | |
410 | base = ctob(Sysmap[btop(base)].pg_pfnum) + (base & PGOFSET); | |
411 | } | |
eff65f87 | 412 | #endif |
7c5704a5 | 413 | (void)lseek(fd, base, off); |
175d35a6 | 414 | } |