2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1983, 1988, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)trpt.c 8.1 (Berkeley) 6/6/93
35 * $FreeBSD: src/usr.sbin/trpt/trpt.c,v 1.12 2000/01/29 11:49:07 shin Exp $
36 * $DragonFly: src/usr.sbin/trpt/trpt.c,v 1.4 2004/03/21 22:41:24 cpressey Exp $
39 #include <sys/param.h>
40 #include <sys/queue.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
44 #include <sys/protosw.h>
48 #include <net/route.h>
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
55 #include <netinet/ip6.h>
57 #include <netinet/ip_var.h>
58 #include <netinet/tcp.h>
60 #include <netinet/tcp_fsm.h>
61 #include <netinet/tcp_seq.h>
63 #include <netinet/tcp_timer.h>
64 #include <netinet/tcp_var.h>
65 #include <netinet/tcpip.h>
67 #include <netinet/tcp_debug.h>
69 #include <arpa/inet.h>
86 static caddr_t tcp_pcbs[TCP_NDEBUG];
88 static int aflag, kflag, memf, follow, sflag, tflag;
90 void dotrace(caddr_t);
91 void klseek(int, off_t, int);
92 int numeric(caddr_t *, caddr_t *);
93 void tcp_trace(short, short, struct tcpcb *, struct tcpcb *,
94 int, void *, struct tcphdr *, int);
95 static void usage(void);
98 main(int argc, char **argv)
100 int ch, i, jflag, npcbs;
104 while ((ch = getopt(argc, argv, "afjp:st")) != -1)
117 if (npcbs >= TCP_NDEBUG)
118 errx(1, "too many pcb's specified");
119 sscanf(optarg, "%x", (int *)&tcp_pcbs[npcbs++]);
144 * Discard setgid privileges if not the running kernel so that
145 * bad guys can't print interesting stuff from kernel memory.
150 system = (char *)getbootfile();
152 if (nlist(system, nl) < 0 || !nl[0].n_value)
153 errx(1, "%s: no namelist", system);
154 if ((memf = open(core, O_RDONLY)) < 0)
157 errx(1, "can't do core files yet");
158 klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET);
159 if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) !=
162 klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET);
163 if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) !=
167 * If no control blocks have been specified, figure
168 * out how many distinct one we have and summarize
169 * them in tcp_pcbs for sorting the trace records
173 for (i = 0; i < TCP_NDEBUG; i++) {
174 struct tcp_debug *td = &tcp_debug[i];
179 for (j = 0; j < npcbs; j++)
180 if (tcp_pcbs[j] == td->td_tcb)
183 tcp_pcbs[npcbs++] = td->td_tcb;
188 qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric);
191 printf("%x", (int)tcp_pcbs[i]);
198 else for (i = 0; i < npcbs; i++) {
199 printf("\n%x:\n", (int)tcp_pcbs[i]);
200 dotrace(tcp_pcbs[i]);
209 "usage: trpt [-afjst] [-p hex-address] [system [core]]\n");
214 dotrace(caddr_t tcpcb)
216 struct tcp_debug *td;
218 int prev_debx, family;
220 prev_debx = tcp_debx;
221 again: if (--tcp_debx < 0)
222 tcp_debx = TCP_NDEBUG - 1;
223 for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
225 if (tcpcb && td->td_tcb != tcpcb)
227 ntime = ntohl(td->td_time);
229 family = td->td_family;
235 tcp_trace(td->td_act, td->td_ostate,
236 (struct tcpcb *)td->td_tcb,
237 &td->td_cb, td->td_family, &td->td_ti.ti_i,
238 &td->td_ti.ti_t, td->td_req);
242 tcp_trace(td->td_act, td->td_ostate,
243 (struct tcpcb *)td->td_tcb,
244 &td->td_cb, td->td_family, &td->td_ti6.ip6,
245 &td->td_ti6.th, td->td_req);
252 for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) {
254 if (tcpcb && td->td_tcb != tcpcb)
256 ntime = ntohl(td->td_time);
258 family = td->td_family;
264 tcp_trace(td->td_act, td->td_ostate,
265 (struct tcpcb *)td->td_tcb,
266 &td->td_cb, td->td_family, &td->td_ti.ti_i,
267 &td->td_ti.ti_t, td->td_req);
271 tcp_trace(td->td_act, td->td_ostate,
272 (struct tcpcb *)td->td_tcb,
273 &td->td_cb, td->td_family, &td->td_ti6.ip6,
274 &td->td_ti6.th, td->td_req);
280 prev_debx = tcp_debx + 1;
281 if (prev_debx >= TCP_NDEBUG)
285 klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET);
286 if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) !=
289 } while (tcp_debx == prev_debx);
290 klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET);
291 if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) !=
303 tcp_trace(short act, short ostate, struct tcpcb *atp, struct tcpcb *tp,
304 int family, void *ip, struct tcphdr *th, int req)
307 int flags, len, win, timer;
310 int isipv6, nopkt = 1;
312 char ntop_buf[INET6_ADDRSTRLEN];
320 ip4 = (struct ip *)ip;
325 ip6 = (struct ip6_hdr *)ip;
331 ip4 = (struct ip *)ip;
333 printf("%03ld %s:%s ",(ntime/10) % 1000, tcpstates[ostate],
344 printf("(src=%s,%u, ",
348 ? inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf,
351 inet_ntoa(ip4->ip_src),
352 ntohs(th->th_sport));
356 ? inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf,
359 inet_ntoa(ip4->ip_dst),
360 ntohs(th->th_dport));
367 isipv6 ? ip6->ip6_plen :
371 if (act == TA_OUTPUT) {
377 if (act == TA_OUTPUT)
378 len -= sizeof(struct tcphdr);
380 printf("[%lx..%lx)", seq, seq + len);
385 printf("(win=%x)", win);
386 flags = th->th_flags;
390 #define pf(flag, string) { \
391 if (th->th_flags & flag) { \
392 printf("%s%s", cp, string); \
408 printf("%s", prurequests[req]);
409 if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
410 printf("<%s>", tcptimers[timer]);
413 printf(" -> %s", tcpstates[tp->t_state]);
414 /* print out internal state of tp !?! */
417 printf("\trcv_nxt %lx rcv_wnd %x snd_una %lx snd_nxt %lx snd_max %lx\n",
418 tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt,
420 printf("\tsnd_wl1 %lx snd_wl2 %lx snd_wnd %x\n", tp->snd_wl1,
421 tp->snd_wl2, tp->snd_wnd);
423 /* print out timers? */
427 * kernel now uses callouts, not integer time values.
433 for (i = 0; i < TCPT_NTIMERS; i++) {
434 if (tp->t_timer[i] == 0)
436 printf("%s%s=%d", cp, tcptimers[i], tp->t_timer[i]);
438 printf(" (t_rxtshft=%d)", tp->t_rxtshift);
448 numeric(caddr_t *c1, caddr_t *c2)
454 klseek(int fd, off_t base, int off)
456 lseek(fd, base, off);