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