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