2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1983, 1988, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)trpt.c 8.1 (Berkeley) 6/6/93";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/queue.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
51 #include <sys/protosw.h>
55 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/ip.h>
62 #include <netinet/ip6.h>
64 #include <netinet/ip_var.h>
65 #include <netinet/tcp.h>
67 #include <netinet/tcp_fsm.h>
68 #include <netinet/tcp_seq.h>
70 #include <netinet/tcp_timer.h>
72 #include <netinet/tcp_var.h>
73 #include <netinet/tcpip.h>
75 #include <netinet/tcp_debug.h>
77 #include <arpa/inet.h>
88 static struct tcp_debug tcp_debug[TCP_NDEBUG];
91 static struct nlist nl[3];
95 static caddr_t tcp_pcbs[TCP_NDEBUG];
97 static int aflag, kflag, memf, follow, sflag;
99 static void dotrace(caddr_t);
100 static void klseek(int, off_t, int);
101 static int numeric(const void *, const void *);
102 static void tcp_trace(short, short, struct tcpcb *, int, void *, struct tcphdr *, int);
103 static void usage(void);
106 main(int argc, char **argv)
108 int ch, i, jflag, npcbs;
109 const char *core, *syst;
111 nl[0].n_name = strdup("_tcp_debug");
112 nl[1].n_name = strdup("_tcp_debx");
115 while ((ch = getopt(argc, argv, "afjp:s")) != -1)
128 if (npcbs >= TCP_NDEBUG)
129 errx(1, "too many pcb's specified");
130 (void)sscanf(optarg, "%x", (int *)&tcp_pcbs[npcbs++]);
152 * Discard setgid privileges if not the running kernel so that
153 * bad guys can't print interesting stuff from kernel memory.
155 if (setgid(getgid()) != 0)
158 syst = getbootfile();
160 if (nlist(syst, nl) < 0 || !nl[0].n_value)
161 errx(1, "%s: no namelist", syst);
162 if ((memf = open(core, O_RDONLY)) < 0)
164 if (setgid(getgid()) != 0)
167 errx(1, "can't do core files yet");
168 (void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET);
169 if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) !=
172 (void)klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET);
173 if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) !=
177 * If no control blocks have been specified, figure
178 * out how many distinct one we have and summarize
179 * them in tcp_pcbs for sorting the trace records
183 for (i = 0; i < TCP_NDEBUG; i++) {
184 struct tcp_debug *td = &tcp_debug[i];
189 for (j = 0; j < npcbs; j++)
190 if (tcp_pcbs[j] == td->td_tcb)
193 tcp_pcbs[npcbs++] = td->td_tcb;
198 qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric);
201 printf("%p", (void *)tcp_pcbs[i]);
208 for (i = 0; i < npcbs; i++) {
209 printf("\n%p:\n", tcp_pcbs[i]);
210 dotrace(tcp_pcbs[i]);
218 (void)fprintf(stderr,
219 "usage: trpt [-afjs] [-p hex-address] [system [core]]\n");
224 dotrace(caddr_t tcpcb)
226 struct tcp_debug *td;
228 int prev_debx = tcp_debx, family;
232 tcp_debx = TCP_NDEBUG - 1;
233 for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
235 if (tcpcb && td->td_tcb != tcpcb)
237 ntime = ntohl(td->td_time);
239 family = td->td_family;
245 tcp_trace(td->td_act, td->td_ostate, &td->td_cb,
246 td->td_family, &td->td_ti.ti_i, &td->td_ti.ti_t,
251 tcp_trace(td->td_act, td->td_ostate, &td->td_cb,
252 td->td_family, &td->td_ti6.ip6, &td->td_ti6.th,
260 for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) {
262 if (tcpcb && td->td_tcb != tcpcb)
264 ntime = ntohl(td->td_time);
266 family = td->td_family;
272 tcp_trace(td->td_act, td->td_ostate, &td->td_cb,
273 td->td_family, &td->td_ti.ti_i, &td->td_ti.ti_t,
278 tcp_trace(td->td_act, td->td_ostate, &td->td_cb,
279 td->td_family, &td->td_ti6.ip6, &td->td_ti6.th,
287 prev_debx = tcp_debx + 1;
288 if (prev_debx >= TCP_NDEBUG)
292 (void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET);
293 if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) !=
296 } while (tcp_debx == prev_debx);
297 (void)klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET);
298 if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) !=
310 tcp_trace(short act, short ostate, struct tcpcb *tp, int family __unused,
311 void *ip, struct tcphdr *th, int req)
314 int flags, len, win, timer;
317 bool isipv6, nopkt = true;
319 char ntop_buf[INET6_ADDRSTRLEN];
323 /* Appease GCC -Wmaybe-uninitialized */
332 ip4 = (struct ip *)ip;
337 ip6 = (struct ip6_hdr *)ip;
343 ip4 = (struct ip *)ip;
345 printf("%03ld %s:%s ", (long)((ntime / 10) % 1000), tcpstates[ostate],
356 printf("(src=%s,%u, ",
359 isipv6 ? inet_ntop(AF_INET6, &ip6->ip6_src,
360 ntop_buf, sizeof(ntop_buf)) :
362 inet_ntoa(ip4->ip_src), ntohs(th->th_sport));
365 isipv6 ? inet_ntop(AF_INET6, &ip6->ip6_dst,
366 ntop_buf, sizeof(ntop_buf)) :
368 inet_ntoa(ip4->ip_dst), ntohs(th->th_dport));
375 isipv6 ? ip6->ip6_plen :
379 if (act == TA_OUTPUT) {
385 if (act == TA_OUTPUT)
386 len -= sizeof(struct tcphdr);
388 printf("[%lx..%lx)", (u_long)seq, (u_long)(seq + len));
390 printf("%lx", (u_long)seq);
391 printf("@%lx", (u_long)ack);
393 printf("(win=%x)", win);
394 flags = th->th_flags;
396 const char *cp = "<";
397 #define pf(flag, string) { \
398 if (th->th_flags&flag) { \
399 (void)printf("%s%s", cp, string); \
415 printf("%s", prurequests[req]);
416 if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
417 printf("<%s>", tcptimers[timer]);
420 printf(" -> %s", tcpstates[tp->t_state]);
421 /* print out internal state of tp !?! */
424 printf("\trcv_nxt %lx rcv_wnd %lx snd_una %lx snd_nxt %lx snd_max %lx\n",
425 (u_long)tp->rcv_nxt, (u_long)tp->rcv_wnd,
426 (u_long)tp->snd_una, (u_long)tp->snd_nxt,
427 (u_long)tp->snd_max);
428 printf("\tsnd_wl1 %lx snd_wl2 %lx snd_wnd %lx\n",
429 (u_long)tp->snd_wl1, (u_long)tp->snd_wl2,
430 (u_long)tp->snd_wnd);
435 numeric(const void *v1, const void *v2)
437 const caddr_t *c1 = v1, *c2 = v2;
443 klseek(int fd, off_t base, int off)
445 (void)lseek(fd, base, off);