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