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