1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. 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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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
61 * $FreeBSD: src/sys/netinet/tcp_debug.c,v 1.16.2.1 2000/07/15 07:14:31 kris Exp $
62 */
63
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
73#define	TCPTIMERS
74#define	TANAMES
75#endif
76
77#include <sys/param.h>
78#include <sys/systm.h>
79#include <sys/protosw.h>
80#include <sys/sysctl.h>
81#include <sys/socket.h>
82
83#include <netinet/in.h>
84#include <netinet/in_systm.h>
85#include <netinet/ip.h>
86#if INET6
87#include <netinet/ip6.h>
88#endif
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
98__private_extern__ int	tcpconsdebug = 0;
99SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcpconsdebug, CTLFLAG_RW | CTLFLAG_LOCKED,
100    &tcpconsdebug, 0, "Turn tcp debugging on or off");
101#endif
102
103static struct tcp_debug tcp_debug[TCP_NDEBUG];
104static int	tcp_debx;
105
106/*
107 * Tcp debug routines
108 */
109void
110tcp_trace(act, ostate, tp, ipgen, th, req)
111	short act, ostate;
112	struct tcpcb *tp;
113	void *ipgen;
114	struct tcphdr *th;
115	int req;
116{
117#if INET6
118	int isipv6;
119#endif /* INET6 */
120	tcp_seq seq, ack;
121	int len, flags;
122	struct tcp_debug *td = &tcp_debug[tcp_debx++];
123
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
129		(isipv6 != 0) ? AF_INET6 :
130#endif
131		AF_INET;
132	if (tcp_debx == TCP_NDEBUG)
133		tcp_debx = 0;
134	td->td_time = iptime();
135	td->td_act = act;
136	td->td_ostate = ostate;
137	td->td_tcb = (caddr_t)tp;
138	if (tp)
139		td->td_cb = *tp;
140	else
141		bzero((caddr_t)&td->td_cb, sizeof (*tp));
142	if (ipgen) {
143		switch (td->td_family) {
144		case AF_INET:
145			bcopy((caddr_t)ipgen, (caddr_t)&td->td_ti.ti_i,
146			      sizeof(td->td_ti.ti_i));
147			bzero((caddr_t)td->td_ip6buf, sizeof(td->td_ip6buf));
148			break;
149#if INET6
150		case AF_INET6:
151			bcopy((caddr_t)ipgen, (caddr_t)td->td_ip6buf,
152			      sizeof(td->td_ip6buf));
153			bzero((caddr_t)&td->td_ti.ti_i,
154			      sizeof(td->td_ti.ti_i));
155			break;
156#endif
157		default:
158			bzero((caddr_t)td->td_ip6buf, sizeof(td->td_ip6buf));
159			bzero((caddr_t)&td->td_ti.ti_i,
160			      sizeof(td->td_ti.ti_i));
161			break;
162		}
163	} else {
164		bzero((caddr_t)&td->td_ti.ti_i, sizeof(td->td_ti.ti_i));
165		bzero((caddr_t)td->td_ip6buf, sizeof(td->td_ip6buf));
166	}
167	if (th) {
168		switch (td->td_family) {
169		case AF_INET:
170			td->td_ti.ti_t = *th;
171			bzero((caddr_t)&td->td_ti6.th, sizeof(td->td_ti6.th));
172			break;
173#if INET6
174		case AF_INET6:
175			td->td_ti6.th = *th;
176			bzero((caddr_t)&td->td_ti.ti_t,
177			      sizeof(td->td_ti.ti_t));
178			break;
179#endif
180		default:
181			bzero((caddr_t)&td->td_ti.ti_t,
182			      sizeof(td->td_ti.ti_t));
183			bzero((caddr_t)&td->td_ti6.th, sizeof(td->td_ti6.th));
184			break;
185		}
186	} else {
187		bzero((caddr_t)&td->td_ti.ti_t, sizeof(td->td_ti.ti_t));
188		bzero((caddr_t)&td->td_ti6.th, sizeof(td->td_ti6.th));
189	}
190	td->td_req = req;
191#if TCPDEBUG
192	if (tcpconsdebug == 0)
193		return;
194	if (tp)
195		printf("%x %s:", tp, tcpstates[ostate]);
196	else
197		printf("???????? ");
198	printf("%s ", tanames[act]);
199	switch (act) {
200
201	case TA_INPUT:
202	case TA_OUTPUT:
203	case TA_DROP:
204		if (ipgen == NULL || th == NULL)
205			break;
206		seq = th->th_seq;
207		ack = th->th_ack;
208		len =
209#if INET6
210			isipv6 ? ((struct ip6_hdr *)ipgen)->ip6_plen :
211#endif
212			((struct ip *)ipgen)->ip_len;
213		if (act == TA_OUTPUT) {
214			seq = ntohl(seq);
215			ack = ntohl(ack);
216			len = ntohs((u_short)len);
217		}
218		if (act == TA_OUTPUT)
219			len -= sizeof (struct tcphdr);
220		if (len)
221			printf("[%x..%x)", seq, seq+len);
222		else
223			printf("%x", seq);
224		printf("@%x, urp=%x", ack, th->th_urp);
225		flags = th->th_flags;
226		if (flags) {
227			char *cp = "<";
228#define pf(f) {					\
229	if (th->th_flags & TH_##f) {		\
230		printf("%s%s", cp, #f);		\
231		cp = ",";			\
232	}					\
233}
234			pf(SYN); pf(ACK); pf(FIN); pf(RST); pf(PUSH); pf(URG);
235			printf(">");
236		}
237		break;
238
239	case TA_USER:
240		printf("%s", prurequests[req&0xff]);
241		if ((req & 0xff) == PRU_SLOWTIMO)
242			printf("<%s>", tcptimers[req>>8]);
243		break;
244	}
245	if (tp)
246		printf(" -> %s", tcpstates[tp->t_state]);
247	/* print out internal state of tp !?! */
248	printf("\n");
249	if (tp == 0)
250		return;
251	printf(
252	"\trcv_(nxt,wnd,up) (%lx,%lx,%lx) snd_(una,nxt,max) (%lx,%lx,%lx)\n",
253	    (uint32_t)tp->rcv_nxt, tp->rcv_wnd, (uint32_t)tp->rcv_up,
254	    (uint32_t)tp->snd_una, (uint32_t)tp->snd_nxt, (uint32_t)tp->snd_max);
255	printf("\tsnd_(wl1,wl2,wnd) (%lx,%lx,%lx)\n",
256	    (uint32_t)tp->snd_wl1, (uint32_t)tp->snd_wl2, tp->snd_wnd);
257#endif /* TCPDEBUG */
258}
259