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