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