xref: /openbsd/sys/netinet/tcp_debug.c (revision fc5d5711)
1*fc5d5711Sbluhm /*	$OpenBSD: tcp_debug.c,v 1.32 2024/04/10 22:24:07 bluhm Exp $	*/
2b400e158Sniklas /*	$NetBSD: tcp_debug.c,v 1.10 1996/02/13 23:43:36 christos Exp $	*/
3df930be7Sderaadt 
4df930be7Sderaadt /*
5df930be7Sderaadt  * Copyright (c) 1982, 1986, 1993
6df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
7df930be7Sderaadt  *
8df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
9df930be7Sderaadt  * modification, are permitted provided that the following conditions
10df930be7Sderaadt  * are met:
11df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
12df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
13df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
14df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
15df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
1629295d1cSmillert  * 3. Neither the name of the University nor the names of its contributors
17df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
18df930be7Sderaadt  *    without specific prior written permission.
19df930be7Sderaadt  *
20df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df930be7Sderaadt  * SUCH DAMAGE.
31df930be7Sderaadt  *
326cdd78adSderaadt  *	@(#)COPYRIGHT	1.1 (NRL) 17 January 1995
336cdd78adSderaadt  *
346cdd78adSderaadt  * NRL grants permission for redistribution and use in source and binary
356cdd78adSderaadt  * forms, with or without modification, of the software and documentation
366cdd78adSderaadt  * created at NRL provided that the following conditions are met:
376cdd78adSderaadt  *
386cdd78adSderaadt  * 1. Redistributions of source code must retain the above copyright
396cdd78adSderaadt  *    notice, this list of conditions and the following disclaimer.
406cdd78adSderaadt  * 2. Redistributions in binary form must reproduce the above copyright
416cdd78adSderaadt  *    notice, this list of conditions and the following disclaimer in the
426cdd78adSderaadt  *    documentation and/or other materials provided with the distribution.
436cdd78adSderaadt  * 3. All advertising materials mentioning features or use of this software
446cdd78adSderaadt  *    must display the following acknowledgements:
456cdd78adSderaadt  *	This product includes software developed by the University of
466cdd78adSderaadt  *	California, Berkeley and its contributors.
476cdd78adSderaadt  *	This product includes software developed at the Information
486cdd78adSderaadt  *	Technology Division, US Naval Research Laboratory.
496cdd78adSderaadt  * 4. Neither the name of the NRL nor the names of its contributors
506cdd78adSderaadt  *    may be used to endorse or promote products derived from this software
516cdd78adSderaadt  *    without specific prior written permission.
526cdd78adSderaadt  *
536cdd78adSderaadt  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
546cdd78adSderaadt  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
556cdd78adSderaadt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
566cdd78adSderaadt  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
576cdd78adSderaadt  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
586cdd78adSderaadt  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
596cdd78adSderaadt  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
606cdd78adSderaadt  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
616cdd78adSderaadt  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
626cdd78adSderaadt  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
636cdd78adSderaadt  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
646cdd78adSderaadt  *
656cdd78adSderaadt  * The views and conclusions contained in the software and documentation
666cdd78adSderaadt  * are those of the authors and should not be interpreted as representing
676cdd78adSderaadt  * official policies, either expressed or implied, of the US Naval
686cdd78adSderaadt  * Research Laboratory (NRL).
698c2ac9d7Sderaadt  */
708c2ac9d7Sderaadt 
71df930be7Sderaadt #ifdef TCPDEBUG
72df930be7Sderaadt /* load symbolic names */
73df930be7Sderaadt #define	PRUREQUESTS
74df930be7Sderaadt #define	TCPSTATES
75df930be7Sderaadt #define	TCPTIMERS
76df930be7Sderaadt #define	TANAMES
77df930be7Sderaadt #endif
78df930be7Sderaadt 
79df930be7Sderaadt #include <sys/param.h>
80df930be7Sderaadt #include <sys/systm.h>
81df930be7Sderaadt #include <sys/mbuf.h>
82*fc5d5711Sbluhm #include <sys/mutex.h>
83df930be7Sderaadt #include <sys/socket.h>
84df930be7Sderaadt 
85df930be7Sderaadt #include <net/route.h>
86df930be7Sderaadt 
87df930be7Sderaadt #include <netinet/in.h>
88df930be7Sderaadt #include <netinet/in_systm.h>
89df930be7Sderaadt #include <netinet/ip.h>
90df930be7Sderaadt #include <netinet/in_pcb.h>
91df930be7Sderaadt #include <netinet/ip_var.h>
92df930be7Sderaadt #include <netinet/tcp.h>
93df930be7Sderaadt #include <netinet/tcp_timer.h>
94df930be7Sderaadt #include <netinet/tcp_var.h>
95df930be7Sderaadt #include <netinet/tcp_debug.h>
969b1c5828Stedu #include <netinet/tcp_fsm.h>
97df930be7Sderaadt 
988c2ac9d7Sderaadt #ifdef INET6
99fa86ee14Sitojun #include <netinet/ip6.h>
1008c2ac9d7Sderaadt #endif /* INET6 */
1018c2ac9d7Sderaadt 
102df930be7Sderaadt #ifdef TCPDEBUG
103*fc5d5711Sbluhm #include <sys/protosw.h>
104*fc5d5711Sbluhm #endif
105*fc5d5711Sbluhm 
106*fc5d5711Sbluhm /*
107*fc5d5711Sbluhm  *  Locks used to protect struct members in this file:
108*fc5d5711Sbluhm  *	D	TCP debug global mutex
109*fc5d5711Sbluhm  */
110*fc5d5711Sbluhm 
111*fc5d5711Sbluhm struct mutex tcp_debug_mtx = MUTEX_INITIALIZER(IPL_SOFTNET);
112*fc5d5711Sbluhm 
113*fc5d5711Sbluhm #ifdef TCPDEBUG
114df930be7Sderaadt int	tcpconsdebug = 0;
115df930be7Sderaadt #endif
11696363099Sjason 
117*fc5d5711Sbluhm struct	tcp_debug tcp_debug[TCP_NDEBUG];	/* [D] */
118*fc5d5711Sbluhm int	tcp_debx;				/* [D] */
11996363099Sjason 
120df930be7Sderaadt /*
121df930be7Sderaadt  * Tcp debug routines
122df930be7Sderaadt  */
123df930be7Sderaadt void
tcp_trace(short act,short ostate,struct tcpcb * tp,struct tcpcb * otp,caddr_t headers,int req,int len)1244e64d49bSbluhm tcp_trace(short act, short ostate, struct tcpcb *tp, struct tcpcb *otp,
1254e64d49bSbluhm     caddr_t headers, int req, int len)
126df930be7Sderaadt {
127b400e158Sniklas #ifdef TCPDEBUG
128*fc5d5711Sbluhm 	struct tcphdr *th;
129df930be7Sderaadt 	tcp_seq seq, ack;
1308c2ac9d7Sderaadt 	int flags;
131b400e158Sniklas #endif
1324e64d49bSbluhm 	int pf = PF_UNSPEC;
133*fc5d5711Sbluhm 	struct tcp_debug *td;
134*fc5d5711Sbluhm 	struct tcpiphdr *ti;
135*fc5d5711Sbluhm 	struct tcpipv6hdr *ti6;
136*fc5d5711Sbluhm 
137*fc5d5711Sbluhm 	mtx_enter(&tcp_debug_mtx);
138*fc5d5711Sbluhm 
139*fc5d5711Sbluhm 	td = &tcp_debug[tcp_debx++];
140*fc5d5711Sbluhm 	ti = (struct tcpiphdr *)headers;
141*fc5d5711Sbluhm 	ti6 = (struct tcpipv6hdr *)headers;
142df930be7Sderaadt 
143df930be7Sderaadt 	if (tcp_debx == TCP_NDEBUG)
144df930be7Sderaadt 		tcp_debx = 0;
145df930be7Sderaadt 	td->td_time = iptime();
146df930be7Sderaadt 	td->td_act = act;
147df930be7Sderaadt 	td->td_ostate = ostate;
1484e64d49bSbluhm 	td->td_tcb = (caddr_t)otp;
1494e64d49bSbluhm 	if (tp) {
1504e64d49bSbluhm 		pf = tp->pf;
151df930be7Sderaadt 		td->td_cb = *tp;
1524e64d49bSbluhm 	} else
153df930be7Sderaadt 		bzero((caddr_t)&td->td_cb, sizeof (*tp));
1544e64d49bSbluhm 
1554e64d49bSbluhm 	bzero(&td->td_ti6, sizeof(struct tcpipv6hdr));
1564e64d49bSbluhm 	bzero(&td->td_ti, sizeof(struct tcpiphdr));
1574e64d49bSbluhm 	if (headers) {
1584e64d49bSbluhm 		/* The address family may be in tcpcb or ip header. */
1594e64d49bSbluhm 		if (pf == PF_UNSPEC) {
1604e64d49bSbluhm 			switch (ti6->ti6_i.ip6_vfc & IPV6_VERSION_MASK) {
1614e64d49bSbluhm #ifdef INET6
1624e64d49bSbluhm 			case IPV6_VERSION:
1634e64d49bSbluhm 				pf = PF_INET6;
1644e64d49bSbluhm 				break;
1654e64d49bSbluhm #endif /* INET6 */
1664e64d49bSbluhm 			case IPVERSION:
1674e64d49bSbluhm 				pf = PF_INET;
1684e64d49bSbluhm 				break;
1694e64d49bSbluhm 			}
1704e64d49bSbluhm 		}
1714e64d49bSbluhm 		switch (pf) {
1728c2ac9d7Sderaadt #ifdef INET6
17325a14d70Sitojun 		case PF_INET6:
174*fc5d5711Sbluhm #ifdef TCPDEBUG
1758c2ac9d7Sderaadt 			th = &ti6->ti6_t;
176*fc5d5711Sbluhm #endif
1778c2ac9d7Sderaadt 			td->td_ti6 = *ti6;
1780056b770Smarkus 			td->td_ti6.ti6_plen = len;
17925a14d70Sitojun 			break;
1800c47cb18Sitojun #endif /* INET6 */
18125a14d70Sitojun 		case PF_INET:
182*fc5d5711Sbluhm #ifdef TCPDEBUG
1838c2ac9d7Sderaadt 			th = &ti->ti_t;
184*fc5d5711Sbluhm #endif
1858c2ac9d7Sderaadt 			td->td_ti = *ti;
1860056b770Smarkus 			td->td_ti.ti_len = len;
18725a14d70Sitojun 			break;
18825a14d70Sitojun 		default:
1894e64d49bSbluhm 			headers = NULL;
1904e64d49bSbluhm 			break;
1914e64d49bSbluhm 		}
1928c2ac9d7Sderaadt 	}
1938c2ac9d7Sderaadt 
194df930be7Sderaadt 	td->td_req = req;
195df930be7Sderaadt #ifdef TCPDEBUG
196df930be7Sderaadt 	if (tcpconsdebug == 0)
197*fc5d5711Sbluhm 		goto done;
1984e64d49bSbluhm 	if (otp)
1994e64d49bSbluhm 		printf("%p %s:", otp, tcpstates[ostate]);
200df930be7Sderaadt 	else
201df930be7Sderaadt 		printf("???????? ");
202df930be7Sderaadt 	printf("%s ", tanames[act]);
203df930be7Sderaadt 	switch (act) {
204df930be7Sderaadt 
205df930be7Sderaadt 	case TA_INPUT:
206df930be7Sderaadt 	case TA_OUTPUT:
207df930be7Sderaadt 	case TA_DROP:
2084e64d49bSbluhm 		if (headers == NULL)
209df930be7Sderaadt 			break;
2108c2ac9d7Sderaadt 		seq = th->th_seq;
2118c2ac9d7Sderaadt 		ack = th->th_ack;
212df930be7Sderaadt 		if (act == TA_OUTPUT) {
213df930be7Sderaadt 			seq = ntohl(seq);
214df930be7Sderaadt 			ack = ntohl(ack);
215df930be7Sderaadt 		}
216df930be7Sderaadt 		if (len)
217df930be7Sderaadt 			printf("[%x..%x)", seq, seq+len);
218df930be7Sderaadt 		else
219df930be7Sderaadt 			printf("%x", seq);
2208c2ac9d7Sderaadt 		printf("@%x, urp=%x", ack, th->th_urp);
2218c2ac9d7Sderaadt 		flags = th->th_flags;
222df930be7Sderaadt 		if (flags) {
223df930be7Sderaadt 			char *cp = "<";
224d4718babSbluhm #define pf(f) { if (th->th_flags&TH_##f) { printf("%s%s", cp, #f); cp = ","; } }
225df930be7Sderaadt 			pf(SYN); pf(ACK); pf(FIN); pf(RST); pf(PUSH); pf(URG);
226df930be7Sderaadt 			printf(">");
227df930be7Sderaadt 		}
228df930be7Sderaadt 		break;
229df930be7Sderaadt 
230df930be7Sderaadt 	case TA_USER:
23136d7e348Sbluhm 		printf("%s", prurequests[req]);
23236d7e348Sbluhm 		break;
23336d7e348Sbluhm 
23436d7e348Sbluhm 	case TA_TIMER:
23536d7e348Sbluhm 		printf("%s", tcptimers[req]);
236df930be7Sderaadt 		break;
237df930be7Sderaadt 	}
238df930be7Sderaadt 	if (tp)
239df930be7Sderaadt 		printf(" -> %s", tcpstates[tp->t_state]);
240df930be7Sderaadt 	/* print out internal state of tp !?! */
241df930be7Sderaadt 	printf("\n");
2424e64d49bSbluhm 	if (tp == NULL)
243*fc5d5711Sbluhm 		goto done;
244f1bbc13fSbluhm 	printf("\trcv_(nxt,wnd,up) (%x,%lx,%x) snd_(una,nxt,max) (%x,%x,%x)\n",
245df930be7Sderaadt 	    tp->rcv_nxt, tp->rcv_wnd, tp->rcv_up, tp->snd_una, tp->snd_nxt,
246df930be7Sderaadt 	    tp->snd_max);
247f1bbc13fSbluhm 	printf("\tsnd_(wl1,wl2,wnd) (%x,%x,%lx)\n",
248df930be7Sderaadt 	    tp->snd_wl1, tp->snd_wl2, tp->snd_wnd);
249*fc5d5711Sbluhm 
250*fc5d5711Sbluhm  done:
251df930be7Sderaadt #endif /* TCPDEBUG */
252*fc5d5711Sbluhm 	mtx_leave(&tcp_debug_mtx);
253df930be7Sderaadt }
254