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