]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/tcp_debug.c
61f1ad53a3b9db6f38bc054116fbaff31af0ac29
[apple/xnu.git] / bsd / netinet / tcp_debug.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright (c) 1982, 1986, 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 *
55 * @(#)tcp_debug.c 8.1 (Berkeley) 6/10/93
56 * $FreeBSD: src/sys/netinet/tcp_debug.c,v 1.16.2.1 2000/07/15 07:14:31 kris Exp $
57 */
58
59
60 #ifndef INET
61 #error The option TCPDEBUG requires option INET.
62 #endif
63
64 #if TCPDEBUG
65 /* load symbolic names */
66 #define PRUREQUESTS
67 #define TCPSTATES
68 #define TCPTIMERS
69 #define TANAMES
70 #endif
71
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/protosw.h>
75 #include <sys/sysctl.h>
76 #include <sys/socket.h>
77
78 #include <netinet/in.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
81 #if INET6
82 #include <netinet/ip6.h>
83 #endif
84 #include <netinet/ip_var.h>
85 #include <netinet/tcp.h>
86 #include <netinet/tcp_fsm.h>
87 #include <netinet/tcp_timer.h>
88 #include <netinet/tcp_var.h>
89 #include <netinet/tcpip.h>
90 #include <netinet/tcp_debug.h>
91
92 #if TCPDEBUG
93 __private_extern__ int tcpconsdebug = 0;
94 SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcpconsdebug, CTLFLAG_RW,
95 &tcpconsdebug, 0, "Turn tcp debugging on or off");
96 #endif
97
98 static struct tcp_debug tcp_debug[TCP_NDEBUG];
99 static int tcp_debx;
100
101 /*
102 * Tcp debug routines
103 */
104 void
105 tcp_trace(act, ostate, tp, ipgen, th, req)
106 short act, ostate;
107 struct tcpcb *tp;
108 void *ipgen;
109 struct tcphdr *th;
110 int req;
111 {
112 #if INET6
113 int isipv6;
114 #endif /* INET6 */
115 tcp_seq seq, ack;
116 int len, flags;
117 struct tcp_debug *td = &tcp_debug[tcp_debx++];
118
119 #if INET6
120 isipv6 = (ipgen != NULL && ((struct ip *)ipgen)->ip_v == 6) ? 1 : 0;
121 #endif /* INET6 */
122 td->td_family =
123 #if INET6
124 (isipv6 != 0) ? AF_INET6 :
125 #endif
126 AF_INET;
127 if (tcp_debx == TCP_NDEBUG)
128 tcp_debx = 0;
129 td->td_time = iptime();
130 td->td_act = act;
131 td->td_ostate = ostate;
132 td->td_tcb = (caddr_t)tp;
133 if (tp)
134 td->td_cb = *tp;
135 else
136 bzero((caddr_t)&td->td_cb, sizeof (*tp));
137 if (ipgen) {
138 switch (td->td_family) {
139 case AF_INET:
140 bcopy((caddr_t)ipgen, (caddr_t)&td->td_ti.ti_i,
141 sizeof(td->td_ti.ti_i));
142 bzero((caddr_t)td->td_ip6buf, sizeof(td->td_ip6buf));
143 break;
144 #if INET6
145 case AF_INET6:
146 bcopy((caddr_t)ipgen, (caddr_t)td->td_ip6buf,
147 sizeof(td->td_ip6buf));
148 bzero((caddr_t)&td->td_ti.ti_i,
149 sizeof(td->td_ti.ti_i));
150 break;
151 #endif
152 default:
153 bzero((caddr_t)td->td_ip6buf, sizeof(td->td_ip6buf));
154 bzero((caddr_t)&td->td_ti.ti_i,
155 sizeof(td->td_ti.ti_i));
156 break;
157 }
158 } else {
159 bzero((caddr_t)&td->td_ti.ti_i, sizeof(td->td_ti.ti_i));
160 bzero((caddr_t)td->td_ip6buf, sizeof(td->td_ip6buf));
161 }
162 if (th) {
163 switch (td->td_family) {
164 case AF_INET:
165 td->td_ti.ti_t = *th;
166 bzero((caddr_t)&td->td_ti6.th, sizeof(td->td_ti6.th));
167 break;
168 #if INET6
169 case AF_INET6:
170 td->td_ti6.th = *th;
171 bzero((caddr_t)&td->td_ti.ti_t,
172 sizeof(td->td_ti.ti_t));
173 break;
174 #endif
175 default:
176 bzero((caddr_t)&td->td_ti.ti_t,
177 sizeof(td->td_ti.ti_t));
178 bzero((caddr_t)&td->td_ti6.th, sizeof(td->td_ti6.th));
179 break;
180 }
181 } else {
182 bzero((caddr_t)&td->td_ti.ti_t, sizeof(td->td_ti.ti_t));
183 bzero((caddr_t)&td->td_ti6.th, sizeof(td->td_ti6.th));
184 }
185 td->td_req = req;
186 #if TCPDEBUG
187 if (tcpconsdebug == 0)
188 return;
189 if (tp)
190 printf("%x %s:", tp, tcpstates[ostate]);
191 else
192 printf("???????? ");
193 printf("%s ", tanames[act]);
194 switch (act) {
195
196 case TA_INPUT:
197 case TA_OUTPUT:
198 case TA_DROP:
199 if (ipgen == NULL || th == NULL)
200 break;
201 seq = th->th_seq;
202 ack = th->th_ack;
203 len =
204 #if INET6
205 isipv6 ? ((struct ip6_hdr *)ipgen)->ip6_plen :
206 #endif
207 ((struct ip *)ipgen)->ip_len;
208 if (act == TA_OUTPUT) {
209 seq = ntohl(seq);
210 ack = ntohl(ack);
211 len = ntohs((u_short)len);
212 }
213 if (act == TA_OUTPUT)
214 len -= sizeof (struct tcphdr);
215 if (len)
216 printf("[%x..%x)", seq, seq+len);
217 else
218 printf("%x", seq);
219 printf("@%x, urp=%x", ack, th->th_urp);
220 flags = th->th_flags;
221 if (flags) {
222 char *cp = "<";
223 #define pf(f) { \
224 if (th->th_flags & TH_##f) { \
225 printf("%s%s", cp, #f); \
226 cp = ","; \
227 } \
228 }
229 pf(SYN); pf(ACK); pf(FIN); pf(RST); pf(PUSH); pf(URG);
230 printf(">");
231 }
232 break;
233
234 case TA_USER:
235 printf("%s", prurequests[req&0xff]);
236 if ((req & 0xff) == PRU_SLOWTIMO)
237 printf("<%s>", tcptimers[req>>8]);
238 break;
239 }
240 if (tp)
241 printf(" -> %s", tcpstates[tp->t_state]);
242 /* print out internal state of tp !?! */
243 printf("\n");
244 if (tp == 0)
245 return;
246 printf(
247 "\trcv_(nxt,wnd,up) (%lx,%lx,%lx) snd_(una,nxt,max) (%lx,%lx,%lx)\n",
248 (u_long)tp->rcv_nxt, tp->rcv_wnd, (u_long)tp->rcv_up,
249 (u_long)tp->snd_una, (u_long)tp->snd_nxt, (u_long)tp->snd_max);
250 printf("\tsnd_(wl1,wl2,wnd) (%lx,%lx,%lx)\n",
251 (u_long)tp->snd_wl1, (u_long)tp->snd_wl2, tp->snd_wnd);
252 #endif /* TCPDEBUG */
253 }