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