]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/trpt/trpt.c
Merge bmake-20220204
[FreeBSD/FreeBSD.git] / usr.sbin / trpt / trpt.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1988, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31
32 #if 0
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1983, 1988, 1993\n\
36         The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38
39 #ifndef lint
40 static char sccsid[] = "@(#)trpt.c      8.1 (Berkeley) 6/6/93";
41 #endif /* not lint */
42 #endif
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45
46 #include <sys/param.h>
47 #include <sys/queue.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #define PRUREQUESTS
51 #include <sys/protosw.h>
52 #include <sys/file.h>
53 #include <sys/time.h>
54
55 #include <net/route.h>
56 #include <net/if.h>
57
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/ip.h>
61 #ifdef INET6
62 #include <netinet/ip6.h>
63 #endif
64 #include <netinet/ip_var.h>
65 #include <netinet/tcp.h>
66 #define TCPSTATES
67 #include <netinet/tcp_fsm.h>
68 #include <netinet/tcp_seq.h>
69 #define TCPTIMERS
70 #include <netinet/tcp_timer.h>
71 #define _WANT_TCPCB
72 #include <netinet/tcp_var.h>
73 #include <netinet/tcpip.h>
74 #define TANAMES
75 #include <netinet/tcp_debug.h>
76
77 #include <arpa/inet.h>
78
79 #include <err.h>
80 #include <nlist.h>
81 #include <paths.h>
82 #include <stdbool.h>
83 #include <stdio.h>
84 #include <stdlib.h>
85 #include <string.h>
86 #include <unistd.h>
87
88 static struct  tcp_debug tcp_debug[TCP_NDEBUG];
89 static int     tcp_debx;
90
91 static struct nlist nl[3];
92 #define N_TCP_DEBUG     0
93 #define N_TCP_DEBX      1
94
95 static caddr_t tcp_pcbs[TCP_NDEBUG];
96 static n_time ntime;
97 static int aflag, kflag, memf, follow, sflag;
98
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);
104
105 int
106 main(int argc, char **argv)
107 {
108         int ch, i, jflag, npcbs;
109         const char *core, *syst;
110
111         nl[0].n_name = strdup("_tcp_debug");
112         nl[1].n_name = strdup("_tcp_debx");
113
114         jflag = npcbs = 0;
115         while ((ch = getopt(argc, argv, "afjp:s")) != -1)
116                 switch (ch) {
117                 case 'a':
118                         ++aflag;
119                         break;
120                 case 'f':
121                         ++follow;
122                         setlinebuf(stdout);
123                         break;
124                 case 'j':
125                         ++jflag;
126                         break;
127                 case 'p':
128                         if (npcbs >= TCP_NDEBUG)
129                                 errx(1, "too many pcb's specified");
130                         (void)sscanf(optarg, "%x", (int *)&tcp_pcbs[npcbs++]);
131                         break;
132                 case 's':
133                         ++sflag;
134                         break;
135                 case '?':
136                 default:
137                         usage();
138                 }
139         argc -= optind;
140         argv += optind;
141
142         core = _PATH_KMEM;
143         if (argc > 0) {
144                 syst = *argv;
145                 argc--, argv++;
146                 if (argc > 0) {
147                         core = *argv;
148                         argc--, argv++;
149                         ++kflag;
150                 }
151                 /*
152                  * Discard setgid privileges if not the running kernel so that
153                  * bad guys can't print interesting stuff from kernel memory.
154                  */
155                 if (setgid(getgid()) != 0)
156                         err(1, "setgid");
157         } else
158                 syst = getbootfile();
159
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)
163                 err(2, "%s", core);
164         if (setgid(getgid()) != 0)
165                 err(1, "setgid");
166         if (kflag)
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)) !=
170             sizeof(tcp_debx))
171                 err(3, "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)) !=
174             sizeof(tcp_debug))
175                 err(3, "tcp_debug");
176         /*
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
180          * below.
181          */
182         if (!npcbs) {
183                 for (i = 0; i < TCP_NDEBUG; i++) {
184                         struct tcp_debug *td = &tcp_debug[i];
185                         int j;
186
187                         if (td->td_tcb == 0)
188                                 continue;
189                         for (j = 0; j < npcbs; j++)
190                                 if (tcp_pcbs[j] == td->td_tcb)
191                                         break;
192                         if (j >= npcbs)
193                                 tcp_pcbs[npcbs++] = td->td_tcb;
194                 }
195                 if (!npcbs)
196                         exit(0);
197         }
198         qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric);
199         if (jflag) {
200                 for (i = 0;;) {
201                         printf("%p", (void *)tcp_pcbs[i]);
202                         if (++i == npcbs)
203                                 break;
204                         fputs(", ", stdout);
205                 }
206                 putchar('\n');
207         } else
208                 for (i = 0; i < npcbs; i++) {
209                         printf("\n%p:\n", tcp_pcbs[i]);
210                         dotrace(tcp_pcbs[i]);
211                 }
212         exit(0);
213 }
214
215 static void
216 usage(void)
217 {
218         (void)fprintf(stderr,
219             "usage: trpt [-afjs] [-p hex-address] [system [core]]\n");
220         exit(1);
221 }
222
223 static void
224 dotrace(caddr_t tcpcb)
225 {
226         struct tcp_debug *td;
227         int i;
228         int prev_debx = tcp_debx, family;
229
230 again:
231         if (--tcp_debx < 0)
232                 tcp_debx = TCP_NDEBUG - 1;
233         for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
234                 td = &tcp_debug[i];
235                 if (tcpcb && td->td_tcb != tcpcb)
236                         continue;
237                 ntime = ntohl(td->td_time);
238 #ifdef INET6
239                 family = td->td_family;
240 #else
241                 family = AF_INET;
242 #endif
243                 switch (family) {
244                 case AF_INET:
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,
247                             td->td_req);
248                         break;
249 #ifdef INET6
250                 case AF_INET6:
251                         tcp_trace(td->td_act, td->td_ostate, &td->td_cb,
252                             td->td_family, &td->td_ti6.ip6, &td->td_ti6.th,
253                             td->td_req);
254                         break;
255 #endif
256                 }
257                 if (i == tcp_debx)
258                         goto done;
259         }
260         for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) {
261                 td = &tcp_debug[i];
262                 if (tcpcb && td->td_tcb != tcpcb)
263                         continue;
264                 ntime = ntohl(td->td_time);
265 #ifdef INET6
266                 family = td->td_family;
267 #else
268                 family = AF_INET;
269 #endif
270                 switch (family) {
271                 case AF_INET:
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,
274                             td->td_req);
275                         break;
276 #ifdef INET6
277                 case AF_INET6:
278                         tcp_trace(td->td_act, td->td_ostate, &td->td_cb,
279                             td->td_family, &td->td_ti6.ip6, &td->td_ti6.th,
280                             td->td_req);
281                         break;
282 #endif
283                 }
284         }
285 done:
286         if (follow) {
287                 prev_debx = tcp_debx + 1;
288                 if (prev_debx >= TCP_NDEBUG)
289                         prev_debx = 0;
290                 do {
291                         sleep(1);
292                         (void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET);
293                         if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) !=
294                             sizeof(tcp_debx))
295                                 err(3, "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)) !=
299                     sizeof(tcp_debug))
300                         err(3, "tcp_debug");
301                 goto again;
302         }
303 }
304
305 /*
306  * Tcp debug routines
307  */
308 /*ARGSUSED*/
309 static void
310 tcp_trace(short act, short ostate, struct tcpcb *tp, int family __unused,
311     void *ip, struct tcphdr *th, int req)
312 {
313         tcp_seq seq, ack;
314         int flags, len, win, timer;
315         struct ip *ip4;
316 #ifdef INET6
317         bool isipv6, nopkt = true;
318         struct ip6_hdr *ip6;
319         char ntop_buf[INET6_ADDRSTRLEN];
320 #endif
321
322 #ifdef INET6
323         /* Appease GCC -Wmaybe-uninitialized */
324         ip4 = NULL;
325         ip6 = NULL;
326         isipv6 = false;
327
328         switch (family) {
329         case AF_INET:
330                 nopkt = false;
331                 isipv6 = false;
332                 ip4 = (struct ip *)ip;
333                 break;
334         case AF_INET6:
335                 nopkt = false;
336                 isipv6 = true;
337                 ip6 = (struct ip6_hdr *)ip;
338         case 0:
339         default:
340                 break;
341         }
342 #else
343         ip4 = (struct ip *)ip;
344 #endif
345         printf("%03ld %s:%s ", (long)((ntime / 10) % 1000), tcpstates[ostate],
346             tanames[act]);
347         switch (act) {
348         case TA_INPUT:
349         case TA_OUTPUT:
350         case TA_DROP:
351 #ifdef INET6
352                 if (nopkt)
353                         break;
354 #endif
355                 if (aflag) {
356                         printf("(src=%s,%u, ",
357
358 #ifdef INET6
359                             isipv6 ? inet_ntop(AF_INET6, &ip6->ip6_src,
360                                 ntop_buf, sizeof(ntop_buf)) :
361 #endif
362                             inet_ntoa(ip4->ip_src), ntohs(th->th_sport));
363                         printf("dst=%s,%u)",
364 #ifdef INET6
365                             isipv6 ? inet_ntop(AF_INET6, &ip6->ip6_dst,
366                                 ntop_buf, sizeof(ntop_buf)) :
367 #endif
368                             inet_ntoa(ip4->ip_dst), ntohs(th->th_dport));
369                 }
370                 seq = th->th_seq;
371                 ack = th->th_ack;
372
373                 len =
374 #ifdef INET6
375                     isipv6 ? ip6->ip6_plen :
376 #endif
377                     ip4->ip_len;
378                 win = th->th_win;
379                 if (act == TA_OUTPUT) {
380                         seq = ntohl(seq);
381                         ack = ntohl(ack);
382                         len = ntohs(len);
383                         win = ntohs(win);
384                 }
385                 if (act == TA_OUTPUT)
386                         len -= sizeof(struct tcphdr);
387                 if (len)
388                         printf("[%lx..%lx)", (u_long)seq, (u_long)(seq + len));
389                 else
390                         printf("%lx", (u_long)seq);
391                 printf("@%lx", (u_long)ack);
392                 if (win)
393                         printf("(win=%x)", win);
394                 flags = th->th_flags;
395                 if (flags) {
396                         const char *cp = "<";
397 #define pf(flag, string) { \
398         if (th->th_flags&flag) { \
399                 (void)printf("%s%s", cp, string); \
400                 cp = ","; \
401         } \
402 }
403                         pf(TH_SYN, "SYN");
404                         pf(TH_ACK, "ACK");
405                         pf(TH_FIN, "FIN");
406                         pf(TH_RST, "RST");
407                         pf(TH_PUSH, "PUSH");
408                         pf(TH_URG, "URG");
409                         printf(">");
410                 }
411                 break;
412         case TA_USER:
413                 timer = req >> 8;
414                 req &= 0xff;
415                 printf("%s", prurequests[req]);
416                 if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
417                         printf("<%s>", tcptimers[timer]);
418                 break;
419         }
420         printf(" -> %s", tcpstates[tp->t_state]);
421         /* print out internal state of tp !?! */
422         printf("\n");
423         if (sflag) {
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);
431         }
432 }
433
434 static int
435 numeric(const void *v1, const void *v2)
436 {
437         const caddr_t *c1 = v1, *c2 = v2;
438
439         return (*c1 - *c2);
440 }
441
442 static void
443 klseek(int fd, off_t base, int off)
444 {
445         (void)lseek(fd, base, off);
446 }