Merge branch 'vendor/TCPDUMP'
[dragonfly.git] / sys / netinet / tcp_debug.c
1 /*
2  * Copyright (c) 1982, 1986, 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  *      @(#)tcp_debug.c 8.1 (Berkeley) 6/10/93
34  * $FreeBSD: src/sys/netinet/tcp_debug.c,v 1.16.2.1 2000/07/15 07:14:31 kris Exp $
35  */
36
37 #include "opt_inet.h"
38 #include "opt_inet6.h"
39 #include "opt_tcpdebug.h"
40
41 #ifndef INET
42 #error The option TCPDEBUG requires option INET.
43 #endif
44
45 #ifdef TCPDEBUG
46 /* load symbolic names */
47 #define PRUREQUESTS
48 #define TCPSTATES
49 #define TCPTIMERS
50 #define TANAMES
51 #endif
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/protosw.h>
56 #include <sys/socket.h>
57
58 #include <net/netmsg.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 #include <netinet/tcp_fsm.h>
69 #include <netinet/tcp_timer.h>
70 #include <netinet/tcp_var.h>
71 #include <netinet/tcpip.h>
72 #include <netinet/tcp_debug.h>
73
74 #ifdef TCPDEBUG
75 static int      tcpconsdebug = 0;
76 #endif
77
78 static struct tcp_debug tcp_debug[TCP_NDEBUG];
79 static int      tcp_debx;
80
81 /*
82  * Tcp debug routines
83  */
84 void
85 tcp_trace(short act, short ostate, struct tcpcb *tp, void *ipgen,
86           struct tcphdr *th, int req)
87 {
88 #ifdef INET6
89         int isipv6;
90 #endif /* INET6 */
91         tcp_seq seq, ack;
92         int len, flags;
93         struct tcp_debug *td = &tcp_debug[tcp_debx++];
94
95 #ifdef INET6
96         isipv6 = (ipgen != NULL && ((struct ip *)ipgen)->ip_v == 6) ? 1 : 0;
97 #endif /* INET6 */
98         td->td_family =
99 #ifdef INET6
100                 (isipv6 != 0) ? AF_INET6 :
101 #endif
102                 AF_INET;
103         if (tcp_debx == TCP_NDEBUG)
104                 tcp_debx = 0;
105         td->td_time = iptime();
106         td->td_act = act;
107         td->td_ostate = ostate;
108         td->td_tcb = (caddr_t)tp;
109         if (tp)
110                 td->td_cb = *tp;
111         else
112                 bzero(&td->td_cb, sizeof *tp);
113         if (ipgen) {
114                 switch (td->td_family) {
115                 case AF_INET:
116                         bcopy(ipgen, &td->td_ti.ti_i, sizeof(td->td_ti.ti_i));
117                         bzero(td->td_ip6buf, sizeof td->td_ip6buf);
118                         break;
119 #ifdef INET6
120                 case AF_INET6:
121                         bcopy(ipgen, td->td_ip6buf, sizeof td->td_ip6buf);
122                         bzero(&td->td_ti.ti_i, sizeof td->td_ti.ti_i);
123                         break;
124 #endif
125                 default:
126                         bzero(td->td_ip6buf, sizeof td->td_ip6buf);
127                         bzero(&td->td_ti.ti_i, sizeof td->td_ti.ti_i);
128                         break;
129                 }
130         } else {
131                 bzero(&td->td_ti.ti_i, sizeof td->td_ti.ti_i);
132                 bzero(td->td_ip6buf, sizeof td->td_ip6buf);
133         }
134         if (th) {
135                 switch (td->td_family) {
136                 case AF_INET:
137                         td->td_ti.ti_t = *th;
138                         bzero(&td->td_ti6.th, sizeof td->td_ti6.th);
139                         break;
140 #ifdef INET6
141                 case AF_INET6:
142                         td->td_ti6.th = *th;
143                         bzero(&td->td_ti.ti_t, sizeof td->td_ti.ti_t);
144                         break;
145 #endif
146                 default:
147                         bzero(&td->td_ti.ti_t, sizeof td->td_ti.ti_t);
148                         bzero(&td->td_ti6.th, sizeof td->td_ti6.th);
149                         break;
150                 }
151         } else {
152                 bzero(&td->td_ti.ti_t, sizeof td->td_ti.ti_t);
153                 bzero(&td->td_ti6.th, sizeof td->td_ti6.th);
154         }
155         td->td_req = req;
156 #ifdef TCPDEBUG
157         if (tcpconsdebug == 0)
158                 return;
159         if (tp)
160                 kprintf("%p %s:", tp, tcpstates[ostate]);
161         else
162                 kprintf("???????? ");
163         kprintf("%s ", tanames[act]);
164         switch (act) {
165
166         case TA_INPUT:
167         case TA_OUTPUT:
168         case TA_DROP:
169                 if (ipgen == NULL || th == NULL)
170                         break;
171                 seq = th->th_seq;
172                 ack = th->th_ack;
173                 len =
174 #ifdef INET6
175                         isipv6 ? ((struct ip6_hdr *)ipgen)->ip6_plen :
176 #endif
177                         ((struct ip *)ipgen)->ip_len;
178                 if (act == TA_OUTPUT) {
179                         seq = ntohl(seq);
180                         ack = ntohl(ack);
181                         len = ntohs((u_short)len);
182                 }
183                 if (act == TA_OUTPUT)
184                         len -= sizeof(struct tcphdr);
185                 if (len)
186                         kprintf("[%x..%x)", seq, seq+len);
187                 else
188                         kprintf("%x", seq);
189                 kprintf("@%x, urp=%x", ack, th->th_urp);
190                 flags = th->th_flags;
191                 if (flags) {
192                         char *cp = "<";
193 #define pf(f) {                                 \
194         if (th->th_flags & TH_##f) {            \
195                 kprintf("%s%s", cp, #f);                \
196                 cp = ",";                       \
197         }                                       \
198 }
199                         pf(SYN); pf(ACK); pf(FIN); pf(RST); pf(PUSH); pf(URG);
200                         kprintf(">");
201                 }
202                 break;
203
204         case TA_USER:
205                 kprintf("%s", prurequests[req&0xff]);
206                 if ((req & 0xff) == PRU_SLOWTIMO)
207                         kprintf("<%s>", tcptimers[req>>8]);
208                 break;
209         }
210         if (tp)
211                 kprintf(" -> %s", tcpstates[tp->t_state]);
212         /* print out internal state of tp !?! */
213         kprintf("\n");
214         if (tp == NULL)
215                 return;
216         kprintf(
217         "\trcv_(nxt,wnd,up) (%lx,%lx,%lx) snd_(una,nxt,max) (%lx,%lx,%lx)\n",
218             (u_long)tp->rcv_nxt, tp->rcv_wnd, (u_long)tp->rcv_up,
219             (u_long)tp->snd_una, (u_long)tp->snd_nxt, (u_long)tp->snd_max);
220         kprintf("\tsnd_(wl1,wl2,wnd) (%lx,%lx,%lx)\n",
221             (u_long)tp->snd_wl1, (u_long)tp->snd_wl2, tp->snd_wnd);
222 #endif /* TCPDEBUG */
223 }