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