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