xref: /openbsd-src/sys/netinet/tcp_debug.c (revision ce7e0fc6a9d74d25b78fb6ad846387717f5172b6)
1 /*	$OpenBSD: tcp_debug.c,v 1.10 2001/06/08 03:53:46 angelos Exp $	*/
2 /*	$NetBSD: tcp_debug.c,v 1.10 1996/02/13 23:43:36 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)COPYRIGHT	1.1 (NRL) 17 January 1995
37  *
38  * NRL grants permission for redistribution and use in source and binary
39  * forms, with or without modification, of the software and documentation
40  * created at NRL provided that the following conditions are met:
41  *
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  *    must display the following acknowledgements:
49  * 	This product includes software developed by the University of
50  * 	California, Berkeley and its contributors.
51  * 	This product includes software developed at the Information
52  * 	Technology Division, US Naval Research Laboratory.
53  * 4. Neither the name of the NRL nor the names of its contributors
54  *    may be used to endorse or promote products derived from this software
55  *    without specific prior written permission.
56  *
57  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
58  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
59  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
60  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
61  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
62  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
63  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
64  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
65  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
66  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
67  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68  *
69  * The views and conclusions contained in the software and documentation
70  * are those of the authors and should not be interpreted as representing
71  * official policies, either expressed or implied, of the US Naval
72  * Research Laboratory (NRL).
73  */
74 
75 #ifdef TCPDEBUG
76 /* load symbolic names */
77 #define	PRUREQUESTS
78 #define	TCPSTATES
79 #define	TCPTIMERS
80 #define	TANAMES
81 #endif
82 
83 #include <sys/param.h>
84 #include <sys/systm.h>
85 #include <sys/mbuf.h>
86 #include <sys/socket.h>
87 
88 #include <net/route.h>
89 #include <net/if.h>
90 
91 #include <netinet/in.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/ip.h>
94 #include <netinet/in_pcb.h>
95 #include <netinet/ip_var.h>
96 #include <netinet/tcp.h>
97 #include <netinet/tcp_timer.h>
98 #include <netinet/tcp_var.h>
99 #include <netinet/tcpip.h>
100 #include <netinet/tcp_debug.h>
101 
102 #ifdef INET6
103 #ifndef INET
104 #include <netinet/in.h>
105 #endif
106 #include <netinet/ip6.h>
107 #endif /* INET6 */
108 
109 #ifdef TCPDEBUG
110 int	tcpconsdebug = 0;
111 #endif
112 /*
113  * Tcp debug routines
114  */
115 void
116 tcp_trace(act, ostate, tp, headers, req, len)
117 	short act, ostate;
118 	struct tcpcb *tp;
119 	caddr_t headers;
120 	int req;
121 	int len;
122 {
123 #ifdef TCPDEBUG
124 	tcp_seq seq, ack;
125 	int flags;
126 #endif
127 	struct tcp_debug *td = &tcp_debug[tcp_debx++];
128 	struct tcpiphdr *ti = (struct tcpiphdr *)headers;
129 	struct tcphdr *th;
130 #ifdef INET6
131 	struct tcpipv6hdr *ti6 = (struct tcpipv6hdr *)ti;
132 #endif
133 
134 	if (tcp_debx == TCP_NDEBUG)
135 		tcp_debx = 0;
136 	td->td_time = iptime();
137 	td->td_act = act;
138 	td->td_ostate = ostate;
139 	td->td_tcb = (caddr_t)tp;
140 	if (tp)
141 		td->td_cb = *tp;
142 	else
143 		bzero((caddr_t)&td->td_cb, sizeof (*tp));
144 #ifdef INET6
145 	if (tp->pf == PF_INET6) {
146 		if (ti) {
147 			th = &ti6->ti6_t;
148 			td->td_ti6 = *ti6;
149 		} else {
150 			bzero(&td->td_ti6, sizeof(struct tcpipv6hdr));
151 		}
152 	} else
153 #endif /* INET6 */
154 	{
155 		if (ti) {
156 			th = &ti->ti_t;
157 			td->td_ti = *ti;
158 		} else {
159 			bzero(&td->td_ti, sizeof(struct tcpiphdr));
160 		}
161 	}
162 
163 	td->td_req = req;
164 #ifdef TCPDEBUG
165 	if (tcpconsdebug == 0)
166 		return;
167 	if (tp)
168 		printf("%x %s:", tp, tcpstates[ostate]);
169 	else
170 		printf("???????? ");
171 	printf("%s ", tanames[act]);
172 	switch (act) {
173 
174 	case TA_INPUT:
175 	case TA_OUTPUT:
176 	case TA_DROP:
177 		if (ti == 0)
178 			break;
179 		seq = th->th_seq;
180 		ack = th->th_ack;
181 		if (act == TA_OUTPUT) {
182 			seq = ntohl(seq);
183 			ack = ntohl(ack);
184 		}
185 		if (len)
186 			printf("[%x..%x)", seq, seq+len);
187 		else
188 			printf("%x", seq);
189 		printf("@%x, urp=%x", ack, th->th_urp);
190 		flags = th->th_flags;
191 		if (flags) {
192 #ifndef lint
193 			char *cp = "<";
194 #define pf(f) { if (th->th_flags&TH_##f) { printf("%s%s", cp, "f"); cp = ","; } }
195 			pf(SYN); pf(ACK); pf(FIN); pf(RST); pf(PUSH); pf(URG);
196 #endif
197 			printf(">");
198 		}
199 		break;
200 
201 	case TA_USER:
202 		printf("%s", prurequests[req&0xff]);
203 		if ((req & 0xff) == PRU_SLOWTIMO)
204 			printf("<%s>", tcptimers[req>>8]);
205 		break;
206 	}
207 	if (tp)
208 		printf(" -> %s", tcpstates[tp->t_state]);
209 	/* print out internal state of tp !?! */
210 	printf("\n");
211 	if (tp == 0)
212 		return;
213 	printf("\trcv_(nxt,wnd,up) (%x,%x,%x) snd_(una,nxt,max) (%x,%x,%x)\n",
214 	    tp->rcv_nxt, tp->rcv_wnd, tp->rcv_up, tp->snd_una, tp->snd_nxt,
215 	    tp->snd_max);
216 	printf("\tsnd_(wl1,wl2,wnd) (%x,%x,%x)\n",
217 	    tp->snd_wl1, tp->snd_wl2, tp->snd_wnd);
218 #endif /* TCPDEBUG */
219 }
220