]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/trpt/trpt.c
This commit was generated by cvs2svn to compensate for changes in r102521,
[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. 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.
20  *
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
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1988, 1993\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)trpt.c      8.1 (Berkeley) 6/6/93";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47
48 #include <sys/param.h>
49 #include <sys/queue.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #define PRUREQUESTS
53 #include <sys/protosw.h>
54 #include <sys/file.h>
55 #include <sys/time.h>
56
57 #include <net/route.h>
58 #include <net/if.h>
59
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #ifdef INET6
64 #include <netinet/ip6.h>
65 #endif
66 #include <netinet/ip_var.h>
67 #include <netinet/tcp.h>
68 #define TCPSTATES
69 #include <netinet/tcp_fsm.h>
70 #include <netinet/tcp_seq.h>
71 #define TCPTIMERS
72 #include <netinet/tcp_timer.h>
73 #include <netinet/tcp_var.h>
74 #include <netinet/tcpip.h>
75 #define TANAMES
76 #include <netinet/tcp_debug.h>
77
78 #include <arpa/inet.h>
79
80 #include <err.h>
81 #include <nlist.h>
82 #include <paths.h>
83 #include <stdio.h>
84 #include <stdlib.h>
85 #include <string.h>
86 #include <unistd.h>
87
88 struct nlist nl[3];
89 #define N_TCP_DEBUG     0
90 #define N_TCP_DEBX      1
91
92 static caddr_t tcp_pcbs[TCP_NDEBUG];
93 static n_time ntime;
94 static int aflag, kflag, memf, follow, sflag, tflag;
95
96 void dotrace(caddr_t);
97 void klseek(int, off_t, int);
98 int numeric(const void *, const void *);
99 void tcp_trace(short, short, struct tcpcb *, int, void *, struct tcphdr *, int);
100 static void usage(void);
101
102 int
103 main(int argc, char **argv)
104 {
105         int ch, i, jflag, npcbs;
106         const char *core, *syst;
107
108         nl[0].n_name = strdup("_tcp_debug");
109         nl[1].n_name = strdup("_tcp_debx");
110
111         jflag = npcbs = 0;
112         while ((ch = getopt(argc, argv, "afjp:st")) != -1)
113                 switch (ch) {
114                 case 'a':
115                         ++aflag;
116                         break;
117                 case 'f':
118                         ++follow;
119                         setlinebuf(stdout);
120                         break;
121                 case 'j':
122                         ++jflag;
123                         break;
124                 case 'p':
125                         if (npcbs >= TCP_NDEBUG)
126                                 errx(1, "too many pcb's specified");
127                         (void)sscanf(optarg, "%x", (int *)&tcp_pcbs[npcbs++]);
128                         break;
129                 case 's':
130                         ++sflag;
131                         break;
132                 case 't':
133                         ++tflag;
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                 setgid(getgid());
156         }
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         setgid(getgid());
165         if (kflag)
166                 errx(1, "can't do core files yet");
167         (void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET);
168         if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) !=
169             sizeof(tcp_debx))
170                 err(3, "tcp_debx");
171         (void)klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET);
172         if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) !=
173             sizeof(tcp_debug))
174                 err(3, "tcp_debug");
175         /*
176          * If no control blocks have been specified, figure
177          * out how many distinct one we have and summarize
178          * them in tcp_pcbs for sorting the trace records
179          * below.
180          */
181         if (!npcbs) {
182                 for (i = 0; i < TCP_NDEBUG; i++) {
183                         register struct tcp_debug *td = &tcp_debug[i];
184                         register int j;
185
186                         if (td->td_tcb == 0)
187                                 continue;
188                         for (j = 0; j < npcbs; j++)
189                                 if (tcp_pcbs[j] == td->td_tcb)
190                                         break;
191                         if (j >= npcbs)
192                                 tcp_pcbs[npcbs++] = td->td_tcb;
193                 }
194                 if (!npcbs)
195                         exit(0);
196         }
197         qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric);
198         if (jflag) {
199                 for (i = 0;;) {
200                         printf("%p", (void *)tcp_pcbs[i]);
201                         if (++i == npcbs)
202                                 break;
203                         fputs(", ", stdout);
204                 }
205                 putchar('\n');
206         }
207         else for (i = 0; i < npcbs; i++) {
208                 printf("\n%p:\n", tcp_pcbs[i]);
209                 dotrace(tcp_pcbs[i]);
210         }
211         exit(0);
212 }
213
214 static void
215 usage()
216 {
217         (void)fprintf(stderr,
218                 "usage: trpt [-afjst] [-p hex-address] [system [core]]\n");
219         exit(1);
220 }
221
222 void
223 dotrace(tcpcb)
224         register caddr_t tcpcb;
225 {
226         register struct tcp_debug *td;
227         register int i;
228         int prev_debx = tcp_debx, family;
229
230 again:  if (--tcp_debx < 0)
231                 tcp_debx = TCP_NDEBUG - 1;
232         for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
233                 td = &tcp_debug[i];
234                 if (tcpcb && td->td_tcb != tcpcb)
235                         continue;
236                 ntime = ntohl(td->td_time);
237 #ifdef INET6
238                 family = td->td_family;
239 #else
240                 family = AF_INET;
241 #endif
242                 switch(family) {
243                 case AF_INET:
244                         tcp_trace(td->td_act, td->td_ostate,
245                                   &td->td_cb, td->td_family, &td->td_ti.ti_i,
246                                   &td->td_ti.ti_t, td->td_req);
247                         break;
248 #ifdef INET6
249                 case AF_INET6:
250                         tcp_trace(td->td_act, td->td_ostate,
251                                   &td->td_cb, td->td_family, &td->td_ti6.ip6,
252                                   &td->td_ti6.th, td->td_req);
253                         break;
254 #endif
255                 }
256                 if (i == tcp_debx)
257                         goto done;
258         }
259         for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) {
260                 td = &tcp_debug[i];
261                 if (tcpcb && td->td_tcb != tcpcb)
262                         continue;
263                 ntime = ntohl(td->td_time);
264 #ifdef INET6
265                 family = td->td_family;
266 #else
267                 family = AF_INET;
268 #endif
269                 switch(family) {
270                 case AF_INET:
271                         tcp_trace(td->td_act, td->td_ostate,
272                                   &td->td_cb, td->td_family, &td->td_ti.ti_i,
273                                   &td->td_ti.ti_t, td->td_req);
274                         break;
275 #ifdef INET6
276                 case AF_INET6:
277                         tcp_trace(td->td_act, td->td_ostate,
278                                   &td->td_cb, td->td_family, &td->td_ti6.ip6,
279                                   &td->td_ti6.th, td->td_req);
280                         break;
281 #endif
282                 }
283         }
284 done:   if (follow) {
285                 prev_debx = tcp_debx + 1;
286                 if (prev_debx >= TCP_NDEBUG)
287                         prev_debx = 0;
288                 do {
289                         sleep(1);
290                         (void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET);
291                         if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) !=
292                             sizeof(tcp_debx))
293                                 err(3, "tcp_debx");
294                 } while (tcp_debx == prev_debx);
295                 (void)klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET);
296                 if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) !=
297                     sizeof(tcp_debug))
298                         err(3, "tcp_debug");
299                 goto again;
300         }
301 }
302
303 /*
304  * Tcp debug routines
305  */
306 /*ARGSUSED*/
307 void
308 tcp_trace(act, ostate, tp, family, ip, th, req)
309         short act, ostate;
310         struct tcpcb *tp;
311         int family;
312         void *ip;
313         struct tcphdr *th;
314         int req;
315 {
316         tcp_seq seq, ack;
317         int flags, len, win, timer;
318         struct ip *ip4;
319 #ifdef INET6
320         int isipv6, nopkt = 1;
321         struct ip6_hdr *ip6;
322         char ntop_buf[INET6_ADDRSTRLEN];
323 #endif
324
325 #ifdef INET6
326         switch (family) {
327         case AF_INET:
328                 nopkt = 0;
329                 isipv6 = 0;
330                 ip4 = (struct ip *)ip;
331                 break;
332         case AF_INET6:
333                 nopkt = 0;
334                 isipv6 = 1;
335                 ip6 = (struct ip6_hdr *)ip;
336         case 0:
337         default:
338                 break;
339         }
340 #else
341         ip4 = (struct ip *)ip;
342 #endif
343         printf("%03ld %s:%s ", (long)((ntime/10) % 1000), tcpstates[ostate],
344             tanames[act]);
345         switch (act) {
346         case TA_INPUT:
347         case TA_OUTPUT:
348         case TA_DROP:
349 #ifdef INET6
350                 if (nopkt != 0)
351                         break;
352 #endif
353                 if (aflag) {
354                         printf("(src=%s,%u, ",
355
356 #ifdef INET6
357                                isipv6
358                                ? inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf,
359                                            sizeof(ntop_buf)) :
360 #endif
361                                inet_ntoa(ip4->ip_src),
362                                ntohs(th->th_sport));
363                         printf("dst=%s,%u)",
364 #ifdef INET6
365                                isipv6
366                                ? inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf,
367                                            sizeof(ntop_buf)) :
368 #endif
369                                inet_ntoa(ip4->ip_dst),
370                                ntohs(th->th_dport));
371                 }
372                 seq = th->th_seq;
373                 ack = th->th_ack;
374
375                 len =
376 #ifdef INET6
377                         isipv6 ? ip6->ip6_plen :
378 #endif
379                         ip4->ip_len;
380                 win = th->th_win;
381                 if (act == TA_OUTPUT) {
382                         seq = ntohl(seq);
383                         ack = ntohl(ack);
384                         len = ntohs(len);
385                         win = ntohs(win);
386                 }
387                 if (act == TA_OUTPUT)
388                         len -= sizeof(struct tcphdr);
389                 if (len)
390                         printf("[%lx..%lx)", (u_long)seq, (u_long)(seq + len));
391                 else
392                         printf("%lx", (u_long)seq);
393                 printf("@%lx", (u_long)ack);
394                 if (win)
395                         printf("(win=%x)", win);
396                 flags = th->th_flags;
397                 if (flags) {
398                         const char *cp = "<";
399 #define pf(flag, string) { \
400         if (th->th_flags&flag) { \
401                 (void)printf("%s%s", cp, string); \
402                 cp = ","; \
403         } \
404 }
405                         pf(TH_SYN, "SYN");
406                         pf(TH_ACK, "ACK");
407                         pf(TH_FIN, "FIN");
408                         pf(TH_RST, "RST");
409                         pf(TH_PUSH, "PUSH");
410                         pf(TH_URG, "URG");
411                         printf(">");
412                 }
413                 break;
414         case TA_USER:
415                 timer = req >> 8;
416                 req &= 0xff;
417                 printf("%s", prurequests[req]);
418                 if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
419                         printf("<%s>", tcptimers[timer]);
420                 break;
421         }
422         printf(" -> %s", tcpstates[tp->t_state]);
423         /* print out internal state of tp !?! */
424         printf("\n");
425         if (sflag) {
426                 printf("\trcv_nxt %lx rcv_wnd %lx snd_una %lx snd_nxt %lx snd_max %lx\n",
427                     (u_long)tp->rcv_nxt, tp->rcv_wnd,
428                     (u_long)tp->snd_una, (u_long)tp->snd_nxt,
429                     (u_long)tp->snd_max);
430                 printf("\tsnd_wl1 %lx snd_wl2 %lx snd_wnd %lx\n",
431                     (u_long)tp->snd_wl1,
432                     (u_long)tp->snd_wl2, tp->snd_wnd);
433         }
434         /* print out timers? */
435 #if 0
436         /*
437          * XXX 
438          * kernel now uses callouts, not integer time values.
439          */
440         if (tflag) {
441                 register char *cp = "\t";
442                 register int i;
443
444                 for (i = 0; i < TCPT_NTIMERS; i++) {
445                         if (tp->t_timer[i] == 0)
446                                 continue;
447                         printf("%s%s=%d", cp, tcptimers[i], tp->t_timer[i]);
448                         if (i == TCPT_REXMT)
449                                 printf(" (t_rxtshft=%d)", tp->t_rxtshift);
450                         cp = ", ";
451                 }
452                 if (*cp != '\t')
453                         putchar('\n');
454         }
455 #endif
456 }
457
458 int
459 numeric(v1, v2)
460         const void *v1, *v2;
461 {
462         const caddr_t *c1 = v1, *c2 = v2;
463         return(*c1 - *c2);
464 }
465
466 void
467 klseek(fd, base, off)
468         int fd, off;
469         off_t base;
470 {
471         (void)lseek(fd, base, off);
472 }