trpt.c revision 1.21 1 1.21 rpaulo /* $NetBSD: trpt.c,v 1.21 2005/09/06 03:04:15 rpaulo Exp $ */
2 1.5 thorpej
3 1.5 thorpej /*-
4 1.5 thorpej * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 1.5 thorpej * All rights reserved.
6 1.5 thorpej *
7 1.5 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.5 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.5 thorpej * NASA Ames Research Center.
10 1.5 thorpej *
11 1.5 thorpej * Redistribution and use in source and binary forms, with or without
12 1.5 thorpej * modification, are permitted provided that the following conditions
13 1.5 thorpej * are met:
14 1.5 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.5 thorpej * notice, this list of conditions and the following disclaimer.
16 1.5 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.5 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.5 thorpej * documentation and/or other materials provided with the distribution.
19 1.5 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.5 thorpej * must display the following acknowledgement:
21 1.5 thorpej * This product includes software developed by the NetBSD
22 1.5 thorpej * Foundation, Inc. and its contributors.
23 1.5 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.5 thorpej * contributors may be used to endorse or promote products derived
25 1.5 thorpej * from this software without specific prior written permission.
26 1.5 thorpej *
27 1.5 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.5 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.5 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.5 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.5 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.5 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.5 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.5 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.5 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.5 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.5 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.5 thorpej */
39 1.4 mikel
40 1.1 cgd /*
41 1.4 mikel * Copyright (c) 1983, 1988, 1993
42 1.4 mikel * The Regents of the University of California. All rights reserved.
43 1.1 cgd *
44 1.1 cgd * Redistribution and use in source and binary forms, with or without
45 1.1 cgd * modification, are permitted provided that the following conditions
46 1.1 cgd * are met:
47 1.1 cgd * 1. Redistributions of source code must retain the above copyright
48 1.1 cgd * notice, this list of conditions and the following disclaimer.
49 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
50 1.1 cgd * notice, this list of conditions and the following disclaimer in the
51 1.1 cgd * documentation and/or other materials provided with the distribution.
52 1.17 agc * 3. Neither the name of the University nor the names of its contributors
53 1.1 cgd * may be used to endorse or promote products derived from this software
54 1.1 cgd * without specific prior written permission.
55 1.1 cgd *
56 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 1.1 cgd * SUCH DAMAGE.
67 1.1 cgd */
68 1.1 cgd
69 1.5 thorpej #include <sys/cdefs.h>
70 1.1 cgd #ifndef lint
71 1.5 thorpej __COPYRIGHT(
72 1.4 mikel "@(#) Copyright (c) 1983, 1988, 1993\n\
73 1.5 thorpej The Regents of the University of California. All rights reserved.\n");
74 1.1 cgd #endif /* not lint */
75 1.1 cgd
76 1.1 cgd #ifndef lint
77 1.4 mikel #if 0
78 1.4 mikel static char sccsid[] = "@(#)trpt.c 8.1 (Berkeley) 6/6/93";
79 1.4 mikel #else
80 1.21 rpaulo __RCSID("$NetBSD: trpt.c,v 1.21 2005/09/06 03:04:15 rpaulo Exp $");
81 1.4 mikel #endif
82 1.1 cgd #endif /* not lint */
83 1.1 cgd
84 1.1 cgd #include <sys/param.h>
85 1.3 cgd #include <sys/queue.h>
86 1.1 cgd #include <sys/socket.h>
87 1.1 cgd #include <sys/socketvar.h>
88 1.21 rpaulo #include <sys/sysctl.h>
89 1.1 cgd #define PRUREQUESTS
90 1.1 cgd #include <sys/protosw.h>
91 1.1 cgd #include <sys/file.h>
92 1.1 cgd
93 1.1 cgd #include <net/route.h>
94 1.1 cgd #include <net/if.h>
95 1.1 cgd
96 1.1 cgd #include <netinet/in.h>
97 1.1 cgd #include <netinet/in_systm.h>
98 1.1 cgd #include <netinet/ip.h>
99 1.1 cgd #include <netinet/in_pcb.h>
100 1.1 cgd #include <netinet/ip_var.h>
101 1.9 itojun
102 1.9 itojun #ifdef INET6
103 1.9 itojun #ifndef INET
104 1.9 itojun #include <netinet/in.h>
105 1.9 itojun #endif
106 1.9 itojun #include <netinet/ip6.h>
107 1.9 itojun #endif
108 1.9 itojun
109 1.1 cgd #include <netinet/tcp.h>
110 1.1 cgd #define TCPSTATES
111 1.1 cgd #include <netinet/tcp_fsm.h>
112 1.1 cgd #include <netinet/tcp_seq.h>
113 1.1 cgd #define TCPTIMERS
114 1.1 cgd #include <netinet/tcp_timer.h>
115 1.1 cgd #include <netinet/tcp_var.h>
116 1.1 cgd #include <netinet/tcpip.h>
117 1.1 cgd #define TANAMES
118 1.1 cgd #include <netinet/tcp_debug.h>
119 1.1 cgd
120 1.1 cgd #include <arpa/inet.h>
121 1.1 cgd
122 1.5 thorpej #include <err.h>
123 1.1 cgd #include <stdio.h>
124 1.1 cgd #include <errno.h>
125 1.5 thorpej #include <kvm.h>
126 1.1 cgd #include <nlist.h>
127 1.1 cgd #include <paths.h>
128 1.5 thorpej #include <limits.h>
129 1.5 thorpej #include <stdlib.h>
130 1.5 thorpej #include <unistd.h>
131 1.1 cgd
132 1.1 cgd struct nlist nl[] = {
133 1.16 thorpej #define N_HARDCLOCK_TICKS 0
134 1.16 thorpej { "_hardclock_ticks" },
135 1.16 thorpej #define N_TCP_DEBUG 1
136 1.1 cgd { "_tcp_debug" },
137 1.16 thorpej #define N_TCP_DEBX 2
138 1.1 cgd { "_tcp_debx" },
139 1.5 thorpej { NULL },
140 1.1 cgd };
141 1.1 cgd
142 1.1 cgd static caddr_t tcp_pcbs[TCP_NDEBUG];
143 1.1 cgd static n_time ntime;
144 1.5 thorpej static int aflag, follow, sflag, tflag;
145 1.11 itojun
146 1.11 itojun /* see sys/netinet/tcp_debug.c */
147 1.11 itojun struct tcp_debug tcp_debug[TCP_NDEBUG];
148 1.11 itojun int tcp_debx;
149 1.5 thorpej
150 1.19 perry int main(int, char *[]);
151 1.19 perry void dotrace(caddr_t);
152 1.19 perry void tcp_trace(short, short, struct tcpcb *, struct tcpcb *,
153 1.19 perry int, void *, int);
154 1.19 perry int numeric(const void *, const void *);
155 1.19 perry void usage(void);
156 1.1 cgd
157 1.5 thorpej kvm_t *kd;
158 1.21 rpaulo int use_sysctl;
159 1.5 thorpej
160 1.5 thorpej int
161 1.19 perry main(int argc, char *argv[])
162 1.1 cgd {
163 1.5 thorpej int ch, i, jflag, npcbs;
164 1.5 thorpej char *system, *core, *cp, errbuf[_POSIX2_LINE_MAX];
165 1.14 itojun unsigned long l;
166 1.5 thorpej
167 1.21 rpaulo jflag = npcbs = 0;
168 1.5 thorpej system = core = NULL;
169 1.1 cgd
170 1.13 soda while ((ch = getopt(argc, argv, "afjp:stN:M:")) != -1) {
171 1.1 cgd switch (ch) {
172 1.1 cgd case 'a':
173 1.1 cgd ++aflag;
174 1.1 cgd break;
175 1.1 cgd case 'f':
176 1.1 cgd ++follow;
177 1.1 cgd setlinebuf(stdout);
178 1.1 cgd break;
179 1.1 cgd case 'j':
180 1.1 cgd ++jflag;
181 1.1 cgd break;
182 1.1 cgd case 'p':
183 1.5 thorpej if (npcbs >= TCP_NDEBUG)
184 1.5 thorpej errx(1, "too many pcbs specified");
185 1.5 thorpej errno = 0;
186 1.14 itojun cp = NULL;
187 1.14 itojun l = strtoul(optarg, &cp, 16);
188 1.14 itojun tcp_pcbs[npcbs] = (caddr_t)l;
189 1.14 itojun if (*optarg == '\0' || *cp != '\0' || errno ||
190 1.14 itojun (unsigned long)tcp_pcbs[npcbs] != l)
191 1.5 thorpej errx(1, "invalid address: %s", optarg);
192 1.14 itojun npcbs++;
193 1.1 cgd break;
194 1.1 cgd case 's':
195 1.1 cgd ++sflag;
196 1.1 cgd break;
197 1.1 cgd case 't':
198 1.1 cgd ++tflag;
199 1.1 cgd break;
200 1.5 thorpej case 'N':
201 1.5 thorpej system = optarg;
202 1.5 thorpej break;
203 1.5 thorpej case 'M':
204 1.5 thorpej core = optarg;
205 1.5 thorpej break;
206 1.1 cgd default:
207 1.5 thorpej usage();
208 1.5 thorpej /* NOTREACHED */
209 1.1 cgd }
210 1.5 thorpej }
211 1.1 cgd argc -= optind;
212 1.1 cgd argv += optind;
213 1.1 cgd
214 1.5 thorpej if (argc)
215 1.5 thorpej usage();
216 1.5 thorpej
217 1.21 rpaulo use_sysctl = (system == NULL && core == NULL);
218 1.1 cgd
219 1.21 rpaulo if (use_sysctl) {
220 1.21 rpaulo size_t lenx = sizeof(tcp_debx);
221 1.21 rpaulo size_t lend = sizeof(tcp_debug);
222 1.21 rpaulo
223 1.21 rpaulo if (sysctlbyname("net.inet.tcp.debx", &tcp_debx, &lenx,
224 1.21 rpaulo NULL, 0) == -1)
225 1.21 rpaulo err(1, "net.inet.tcp.debx");
226 1.21 rpaulo if (sysctlbyname("net.inet.tcp.debug", &tcp_debug, &lend,
227 1.21 rpaulo NULL, 0) == -1)
228 1.21 rpaulo err(1, "net.inet.tcp.debug");
229 1.21 rpaulo } else {
230 1.21 rpaulo kd = kvm_openfiles(system, core, NULL, O_RDONLY, errbuf);
231 1.21 rpaulo if (kd == NULL)
232 1.21 rpaulo errx(1, "can't open kmem: %s", errbuf);
233 1.21 rpaulo
234 1.21 rpaulo if (kvm_nlist(kd, nl))
235 1.21 rpaulo errx(2, "%s: no namelist", system);
236 1.21 rpaulo
237 1.21 rpaulo if (kvm_read(kd, nl[N_TCP_DEBX].n_value, (char *)&tcp_debx,
238 1.21 rpaulo sizeof(tcp_debx)) != sizeof(tcp_debx))
239 1.21 rpaulo errx(3, "tcp_debx: %s", kvm_geterr(kd));
240 1.21 rpaulo
241 1.21 rpaulo if (kvm_read(kd, nl[N_TCP_DEBUG].n_value, (char *)tcp_debug,
242 1.21 rpaulo sizeof(tcp_debug)) != sizeof(tcp_debug))
243 1.21 rpaulo errx(3, "tcp_debug: %s", kvm_geterr(kd));
244 1.21 rpaulo }
245 1.1 cgd
246 1.1 cgd /*
247 1.1 cgd * If no control blocks have been specified, figure
248 1.1 cgd * out how many distinct one we have and summarize
249 1.1 cgd * them in tcp_pcbs for sorting the trace records
250 1.1 cgd * below.
251 1.1 cgd */
252 1.5 thorpej if (npcbs == 0) {
253 1.1 cgd for (i = 0; i < TCP_NDEBUG; i++) {
254 1.5 thorpej struct tcp_debug *td = &tcp_debug[i];
255 1.5 thorpej int j;
256 1.1 cgd
257 1.1 cgd if (td->td_tcb == 0)
258 1.1 cgd continue;
259 1.1 cgd for (j = 0; j < npcbs; j++)
260 1.1 cgd if (tcp_pcbs[j] == td->td_tcb)
261 1.1 cgd break;
262 1.1 cgd if (j >= npcbs)
263 1.1 cgd tcp_pcbs[npcbs++] = td->td_tcb;
264 1.1 cgd }
265 1.5 thorpej if (npcbs == 0)
266 1.1 cgd exit(0);
267 1.1 cgd }
268 1.1 cgd qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric);
269 1.1 cgd if (jflag) {
270 1.1 cgd for (i = 0;;) {
271 1.3 cgd printf("%lx", (long)tcp_pcbs[i]);
272 1.1 cgd if (++i == npcbs)
273 1.1 cgd break;
274 1.1 cgd fputs(", ", stdout);
275 1.1 cgd }
276 1.1 cgd putchar('\n');
277 1.5 thorpej } else {
278 1.5 thorpej for (i = 0; i < npcbs; i++) {
279 1.5 thorpej printf("\n%lx:\n", (long)tcp_pcbs[i]);
280 1.5 thorpej dotrace(tcp_pcbs[i]);
281 1.5 thorpej }
282 1.1 cgd }
283 1.1 cgd exit(0);
284 1.1 cgd }
285 1.1 cgd
286 1.5 thorpej void
287 1.19 perry dotrace(caddr_t tcpcb)
288 1.1 cgd {
289 1.5 thorpej struct tcp_debug *td;
290 1.1 cgd int prev_debx = tcp_debx;
291 1.5 thorpej int i;
292 1.1 cgd
293 1.5 thorpej again:
294 1.5 thorpej if (--tcp_debx < 0)
295 1.1 cgd tcp_debx = TCP_NDEBUG - 1;
296 1.1 cgd for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
297 1.1 cgd td = &tcp_debug[i];
298 1.1 cgd if (tcpcb && td->td_tcb != tcpcb)
299 1.1 cgd continue;
300 1.1 cgd ntime = ntohl(td->td_time);
301 1.9 itojun switch (td->td_family) {
302 1.9 itojun case AF_INET:
303 1.9 itojun tcp_trace(td->td_act, td->td_ostate,
304 1.9 itojun (struct tcpcb *)td->td_tcb, &td->td_cb,
305 1.9 itojun td->td_family, &td->td_ti, td->td_req);
306 1.9 itojun break;
307 1.9 itojun #ifdef INET6
308 1.9 itojun case AF_INET6:
309 1.9 itojun tcp_trace(td->td_act, td->td_ostate,
310 1.9 itojun (struct tcpcb *)td->td_tcb, &td->td_cb,
311 1.9 itojun td->td_family, &td->td_ti6, td->td_req);
312 1.9 itojun break;
313 1.9 itojun #endif
314 1.9 itojun default:
315 1.9 itojun tcp_trace(td->td_act, td->td_ostate,
316 1.9 itojun (struct tcpcb *)td->td_tcb, &td->td_cb,
317 1.9 itojun td->td_family, NULL, td->td_req);
318 1.9 itojun break;
319 1.9 itojun }
320 1.1 cgd if (i == tcp_debx)
321 1.1 cgd goto done;
322 1.1 cgd }
323 1.1 cgd for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) {
324 1.1 cgd td = &tcp_debug[i];
325 1.1 cgd if (tcpcb && td->td_tcb != tcpcb)
326 1.1 cgd continue;
327 1.1 cgd ntime = ntohl(td->td_time);
328 1.9 itojun switch (td->td_family) {
329 1.9 itojun case AF_INET:
330 1.9 itojun tcp_trace(td->td_act, td->td_ostate,
331 1.9 itojun (struct tcpcb *)td->td_tcb, &td->td_cb,
332 1.9 itojun td->td_family, &td->td_ti, td->td_req);
333 1.9 itojun break;
334 1.9 itojun #ifdef INET6
335 1.9 itojun case AF_INET6:
336 1.9 itojun tcp_trace(td->td_act, td->td_ostate,
337 1.9 itojun (struct tcpcb *)td->td_tcb, &td->td_cb,
338 1.9 itojun td->td_family, &td->td_ti6, td->td_req);
339 1.9 itojun break;
340 1.9 itojun #endif
341 1.9 itojun default:
342 1.9 itojun tcp_trace(td->td_act, td->td_ostate,
343 1.9 itojun (struct tcpcb *)td->td_tcb, &td->td_cb,
344 1.9 itojun td->td_family, NULL, td->td_req);
345 1.9 itojun break;
346 1.9 itojun }
347 1.1 cgd }
348 1.5 thorpej done:
349 1.5 thorpej if (follow) {
350 1.1 cgd prev_debx = tcp_debx + 1;
351 1.1 cgd if (prev_debx >= TCP_NDEBUG)
352 1.1 cgd prev_debx = 0;
353 1.1 cgd do {
354 1.1 cgd sleep(1);
355 1.21 rpaulo if (use_sysctl) {
356 1.21 rpaulo size_t len = sizeof(tcp_debx);
357 1.21 rpaulo
358 1.21 rpaulo if (sysctlbyname("net.inet.tcp.debx",
359 1.21 rpaulo &tcp_debx, &len, NULL, 0) == -1)
360 1.21 rpaulo err(1, "net.inet.tcp.debx");
361 1.21 rpaulo } else
362 1.21 rpaulo if (kvm_read(kd, nl[N_TCP_DEBX].n_value,
363 1.21 rpaulo (char *)&tcp_debx, sizeof(tcp_debx)) !=
364 1.21 rpaulo sizeof(tcp_debx))
365 1.21 rpaulo errx(3, "tcp_debx: %s",
366 1.21 rpaulo kvm_geterr(kd));
367 1.1 cgd } while (tcp_debx == prev_debx);
368 1.5 thorpej
369 1.21 rpaulo if (use_sysctl) {
370 1.21 rpaulo size_t len = sizeof(tcp_debug);
371 1.21 rpaulo
372 1.21 rpaulo if (sysctlbyname("net.inet.tcp.debug", &tcp_debug,
373 1.21 rpaulo &len, NULL, 0) == -1)
374 1.21 rpaulo err(1, "net.inet.tcp.debug");
375 1.21 rpaulo } else
376 1.21 rpaulo if (kvm_read(kd, nl[N_TCP_DEBUG].n_value,
377 1.21 rpaulo (char *)tcp_debug,
378 1.21 rpaulo sizeof(tcp_debug)) != sizeof(tcp_debug))
379 1.21 rpaulo errx(3, "tcp_debug: %s", kvm_geterr(kd));
380 1.5 thorpej
381 1.1 cgd goto again;
382 1.1 cgd }
383 1.1 cgd }
384 1.1 cgd
385 1.1 cgd /*
386 1.1 cgd * Tcp debug routines
387 1.1 cgd */
388 1.1 cgd /*ARGSUSED*/
389 1.5 thorpej void
390 1.19 perry tcp_trace(short act, short ostate, struct tcpcb *atp, struct tcpcb *tp,
391 1.19 perry int family, void *packet, int req)
392 1.1 cgd {
393 1.1 cgd tcp_seq seq, ack;
394 1.1 cgd int flags, len, win, timer;
395 1.9 itojun struct tcphdr *th = NULL;
396 1.9 itojun struct ip *ip = NULL;
397 1.9 itojun #ifdef INET6
398 1.9 itojun struct ip6_hdr *ip6 = NULL;
399 1.9 itojun #endif
400 1.9 itojun char hbuf[MAXHOSTNAMELEN];
401 1.9 itojun
402 1.20 lukem len = 0; /* XXXGCC -Wuninitialized */
403 1.20 lukem
404 1.9 itojun switch (family) {
405 1.9 itojun case AF_INET:
406 1.9 itojun if (packet) {
407 1.9 itojun ip = (struct ip *)packet;
408 1.9 itojun th = (struct tcphdr *)(ip + 1);
409 1.9 itojun }
410 1.9 itojun break;
411 1.9 itojun #ifdef INET6
412 1.9 itojun case AF_INET6:
413 1.9 itojun if (packet) {
414 1.9 itojun ip6 = (struct ip6_hdr *)packet;
415 1.9 itojun th = (struct tcphdr *)(ip6 + 1);
416 1.9 itojun }
417 1.9 itojun break;
418 1.9 itojun #endif
419 1.9 itojun default:
420 1.9 itojun return;
421 1.9 itojun }
422 1.1 cgd
423 1.5 thorpej printf("%03d %s:%s ", (ntime/10) % 1000, tcpstates[ostate],
424 1.1 cgd tanames[act]);
425 1.9 itojun
426 1.9 itojun #ifndef INET6
427 1.9 itojun if (!ip)
428 1.9 itojun #else
429 1.9 itojun if (!(ip || ip6))
430 1.9 itojun #endif
431 1.9 itojun goto skipact;
432 1.9 itojun
433 1.1 cgd switch (act) {
434 1.1 cgd case TA_INPUT:
435 1.1 cgd case TA_OUTPUT:
436 1.1 cgd case TA_DROP:
437 1.1 cgd if (aflag) {
438 1.9 itojun inet_ntop(family,
439 1.9 itojun #ifndef INET6
440 1.9 itojun (void *)&ip->ip_src,
441 1.9 itojun #else
442 1.9 itojun family == AF_INET ? (void *)&ip->ip_src
443 1.9 itojun : (void *)&ip6->ip6_src,
444 1.9 itojun #endif
445 1.9 itojun hbuf, sizeof(hbuf));
446 1.1 cgd printf("(src=%s,%u, ",
447 1.9 itojun hbuf, ntohs(th->th_sport));
448 1.9 itojun inet_ntop(family,
449 1.9 itojun #ifndef INET6
450 1.9 itojun (void *)&ip->ip_dst,
451 1.9 itojun #else
452 1.9 itojun family == AF_INET ? (void *)&ip->ip_dst
453 1.9 itojun : (void *)&ip6->ip6_dst,
454 1.9 itojun #endif
455 1.9 itojun hbuf, sizeof(hbuf));
456 1.1 cgd printf("dst=%s,%u)",
457 1.9 itojun hbuf, ntohs(th->th_dport));
458 1.1 cgd }
459 1.9 itojun seq = th->th_seq;
460 1.9 itojun ack = th->th_ack;
461 1.9 itojun if (ip)
462 1.9 itojun len = ip->ip_len;
463 1.9 itojun #ifdef INET6
464 1.9 itojun else if (ip6)
465 1.9 itojun len = ip6->ip6_plen;
466 1.9 itojun #endif
467 1.9 itojun win = th->th_win;
468 1.1 cgd if (act == TA_OUTPUT) {
469 1.5 thorpej NTOHL(seq);
470 1.5 thorpej NTOHL(ack);
471 1.5 thorpej NTOHS(len);
472 1.5 thorpej NTOHS(win);
473 1.1 cgd }
474 1.1 cgd if (act == TA_OUTPUT)
475 1.1 cgd len -= sizeof(struct tcphdr);
476 1.1 cgd if (len)
477 1.5 thorpej printf("[%x..%x)", seq, seq + len);
478 1.1 cgd else
479 1.5 thorpej printf("%x", seq);
480 1.5 thorpej printf("@%x", ack);
481 1.1 cgd if (win)
482 1.1 cgd printf("(win=%x)", win);
483 1.9 itojun flags = th->th_flags;
484 1.1 cgd if (flags) {
485 1.19 perry char *cp = "<";
486 1.1 cgd #define pf(flag, string) { \
487 1.9 itojun if (th->th_flags&flag) { \
488 1.1 cgd (void)printf("%s%s", cp, string); \
489 1.1 cgd cp = ","; \
490 1.1 cgd } \
491 1.1 cgd }
492 1.1 cgd pf(TH_SYN, "SYN");
493 1.1 cgd pf(TH_ACK, "ACK");
494 1.1 cgd pf(TH_FIN, "FIN");
495 1.1 cgd pf(TH_RST, "RST");
496 1.1 cgd pf(TH_PUSH, "PUSH");
497 1.1 cgd pf(TH_URG, "URG");
498 1.1 cgd printf(">");
499 1.1 cgd }
500 1.1 cgd break;
501 1.1 cgd case TA_USER:
502 1.1 cgd timer = req >> 8;
503 1.1 cgd req &= 0xff;
504 1.1 cgd printf("%s", prurequests[req]);
505 1.1 cgd if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
506 1.1 cgd printf("<%s>", tcptimers[timer]);
507 1.1 cgd break;
508 1.1 cgd }
509 1.9 itojun
510 1.9 itojun skipact:
511 1.1 cgd printf(" -> %s", tcpstates[tp->t_state]);
512 1.1 cgd /* print out internal state of tp !?! */
513 1.1 cgd printf("\n");
514 1.1 cgd if (sflag) {
515 1.5 thorpej printf("\trcv_nxt %x rcv_wnd %lx snd_una %x snd_nxt %x snd_max %x\n",
516 1.1 cgd tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt,
517 1.1 cgd tp->snd_max);
518 1.5 thorpej printf("\tsnd_wl1 %x snd_wl2 %x snd_wnd %lx\n", tp->snd_wl1,
519 1.1 cgd tp->snd_wl2, tp->snd_wnd);
520 1.1 cgd }
521 1.1 cgd /* print out timers? */
522 1.1 cgd if (tflag) {
523 1.19 perry char *cp = "\t";
524 1.19 perry int i;
525 1.16 thorpej int hardticks;
526 1.16 thorpej
527 1.21 rpaulo if (use_sysctl) {
528 1.21 rpaulo size_t len = sizeof(hardticks);
529 1.1 cgd
530 1.21 rpaulo if (sysctlbyname("kern.hardclock_ticks", &hardticks,
531 1.21 rpaulo &len, NULL, 0) == -1)
532 1.21 rpaulo err(1, "kern.hardclock_ticks");
533 1.21 rpaulo } else {
534 1.21 rpaulo if (kvm_read(kd, nl[N_HARDCLOCK_TICKS].n_value,
535 1.21 rpaulo (char *)&hardticks,
536 1.21 rpaulo sizeof(hardticks)) != sizeof(hardticks))
537 1.21 rpaulo errx(3, "hardclock_ticks: %s", kvm_geterr(kd));
538 1.21 rpaulo
539 1.21 rpaulo for (i = 0; i < TCPT_NTIMERS; i++) {
540 1.21 rpaulo if ((tp->t_timer[i].c_flags
541 1.21 rpaulo & CALLOUT_PENDING) == 0)
542 1.21 rpaulo continue;
543 1.21 rpaulo printf("%s%s=%d", cp, tcptimers[i],
544 1.21 rpaulo tp->t_timer[i].c_time - hardticks);
545 1.21 rpaulo if (i == TCPT_REXMT)
546 1.21 rpaulo printf(" (t_rxtshft=%d)",
547 1.21 rpaulo tp->t_rxtshift);
548 1.21 rpaulo cp = ", ";
549 1.21 rpaulo }
550 1.21 rpaulo if (*cp != '\t')
551 1.21 rpaulo putchar('\n');
552 1.1 cgd }
553 1.1 cgd }
554 1.1 cgd }
555 1.1 cgd
556 1.5 thorpej int
557 1.19 perry numeric(const void *v1, const void *v2)
558 1.1 cgd {
559 1.6 thorpej const caddr_t *c1 = v1;
560 1.6 thorpej const caddr_t *c2 = v2;
561 1.7 thorpej int rv;
562 1.5 thorpej
563 1.7 thorpej if (*c1 < *c2)
564 1.7 thorpej rv = -1;
565 1.7 thorpej else if (*c1 > *c2)
566 1.7 thorpej rv = 1;
567 1.7 thorpej else
568 1.7 thorpej rv = 0;
569 1.7 thorpej
570 1.7 thorpej return (rv);
571 1.1 cgd }
572 1.1 cgd
573 1.5 thorpej void
574 1.19 perry usage(void)
575 1.1 cgd {
576 1.1 cgd
577 1.5 thorpej (void) fprintf(stderr, "usage: %s [-afjst] [-p hex-address]"
578 1.10 cgd " [-N system] [-M core]\n", getprogname());
579 1.5 thorpej exit(1);
580 1.1 cgd }
581