xref: /netbsd-src/sys/netinet/tcp_debug.c (revision 5d73803e75686aae4aba84ec732ba931fa004138)
1*5d73803eSmaxv /*	$NetBSD: tcp_debug.c,v 1.32 2018/05/03 07:13:48 maxv Exp $	*/
2118d2b1dSitojun 
3118d2b1dSitojun /*
4118d2b1dSitojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5118d2b1dSitojun  * All rights reserved.
6118d2b1dSitojun  *
7118d2b1dSitojun  * Redistribution and use in source and binary forms, with or without
8118d2b1dSitojun  * modification, are permitted provided that the following conditions
9118d2b1dSitojun  * are met:
10118d2b1dSitojun  * 1. Redistributions of source code must retain the above copyright
11118d2b1dSitojun  *    notice, this list of conditions and the following disclaimer.
12118d2b1dSitojun  * 2. Redistributions in binary form must reproduce the above copyright
13118d2b1dSitojun  *    notice, this list of conditions and the following disclaimer in the
14118d2b1dSitojun  *    documentation and/or other materials provided with the distribution.
15118d2b1dSitojun  * 3. Neither the name of the project nor the names of its contributors
16118d2b1dSitojun  *    may be used to endorse or promote products derived from this software
17118d2b1dSitojun  *    without specific prior written permission.
18118d2b1dSitojun  *
19118d2b1dSitojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20118d2b1dSitojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21118d2b1dSitojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22118d2b1dSitojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23118d2b1dSitojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24118d2b1dSitojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25118d2b1dSitojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26118d2b1dSitojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27118d2b1dSitojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28118d2b1dSitojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29118d2b1dSitojun  * SUCH DAMAGE.
30118d2b1dSitojun  */
31cf92afd6Scgd 
3261f28255Scgd /*
3307b4f2abSmycroft  * Copyright (c) 1982, 1986, 1993
3407b4f2abSmycroft  *	The Regents of the University of California.  All rights reserved.
3561f28255Scgd  *
3661f28255Scgd  * Redistribution and use in source and binary forms, with or without
3761f28255Scgd  * modification, are permitted provided that the following conditions
3861f28255Scgd  * are met:
3961f28255Scgd  * 1. Redistributions of source code must retain the above copyright
4061f28255Scgd  *    notice, this list of conditions and the following disclaimer.
4161f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
4261f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
4361f28255Scgd  *    documentation and/or other materials provided with the distribution.
44aad01611Sagc  * 3. Neither the name of the University nor the names of its contributors
4561f28255Scgd  *    may be used to endorse or promote products derived from this software
4661f28255Scgd  *    without specific prior written permission.
4761f28255Scgd  *
4861f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4961f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5061f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5161f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5261f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5361f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5461f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5561f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5661f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5761f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5861f28255Scgd  * SUCH DAMAGE.
5961f28255Scgd  *
60cf92afd6Scgd  *	@(#)tcp_debug.c	8.1 (Berkeley) 6/10/93
6161f28255Scgd  */
6261f28255Scgd 
63ea1cd7ebSlukem #include <sys/cdefs.h>
64*5d73803eSmaxv __KERNEL_RCSID(0, "$NetBSD: tcp_debug.c,v 1.32 2018/05/03 07:13:48 maxv Exp $");
65ea1cd7ebSlukem 
661c4a50f1Spooka #ifdef _KERNEL_OPT
67118d2b1dSitojun #include "opt_inet.h"
6803aaf3d8Sabs #include "opt_tcp_debug.h"
691c4a50f1Spooka #endif
70118d2b1dSitojun 
7161f28255Scgd /* load symbolic names */
7261f28255Scgd #define	PRUREQUESTS
7361f28255Scgd #define	TCPSTATES
7461f28255Scgd #define	TCPTIMERS
7561f28255Scgd #define	TANAMES
7661f28255Scgd 
7795b048b5Smycroft #include <sys/param.h>
7895b048b5Smycroft #include <sys/systm.h>
7995b048b5Smycroft #include <sys/mbuf.h>
8095b048b5Smycroft #include <sys/socket.h>
8195b048b5Smycroft #include <sys/socketvar.h>
8295b048b5Smycroft #include <sys/protosw.h>
8395b048b5Smycroft #include <sys/errno.h>
8461f28255Scgd 
8595b048b5Smycroft #include <net/if.h>
8661f28255Scgd 
8795b048b5Smycroft #include <netinet/in.h>
8895b048b5Smycroft #include <netinet/in_systm.h>
8995b048b5Smycroft #include <netinet/ip.h>
9095b048b5Smycroft #include <netinet/in_pcb.h>
9195b048b5Smycroft #include <netinet/ip_var.h>
92118d2b1dSitojun 
93118d2b1dSitojun #ifdef INET6
94118d2b1dSitojun #include <netinet/ip6.h>
95118d2b1dSitojun #endif
96118d2b1dSitojun 
9795b048b5Smycroft #include <netinet/tcp.h>
9895b048b5Smycroft #include <netinet/tcp_fsm.h>
9995b048b5Smycroft #include <netinet/tcp_seq.h>
10095b048b5Smycroft #include <netinet/tcp_timer.h>
10195b048b5Smycroft #include <netinet/tcp_var.h>
10295b048b5Smycroft #include <netinet/tcp_debug.h>
10361f28255Scgd 
10403aaf3d8Sabs struct	tcp_debug tcp_debug[TCP_NDEBUG];
10503aaf3d8Sabs int	tcp_debx;
10661f28255Scgd int	tcpconsdebug = 0;
10761f28255Scgd /*
10861f28255Scgd  * Tcp debug routines
10961f28255Scgd  */
110e4311718Smycroft void
tcp_trace(short act,short ostate,struct tcpcb * tp,struct mbuf * m,int req)111dcf28860Sperry tcp_trace(short act, short ostate, struct tcpcb *tp, struct mbuf *m, int req)
11261f28255Scgd {
11361f28255Scgd 	tcp_seq seq, ack;
11461f28255Scgd 	int len, flags;
115118d2b1dSitojun 	struct tcphdr *th;
11661f28255Scgd 	struct tcp_debug *td = &tcp_debug[tcp_debx++];
11761f28255Scgd 
11861f28255Scgd 	if (tcp_debx == TCP_NDEBUG)
11961f28255Scgd 		tcp_debx = 0;
12061f28255Scgd 	td->td_time = iptime();
12161f28255Scgd 	td->td_act = act;
12261f28255Scgd 	td->td_ostate = ostate;
12353524e44Schristos 	td->td_tcb = (void *)tp;
12461f28255Scgd 	if (tp)
12561f28255Scgd 		td->td_cb = *tp;
12661f28255Scgd 	else
127c363a9cbScegger 		memset((void *)&td->td_cb, 0, sizeof (*tp));
128118d2b1dSitojun 	td->td_family = tp->t_family;
129c363a9cbScegger 	memset((void *)&td->td_ti, 0, sizeof (td->td_ti));
130118d2b1dSitojun #ifdef INET6
131c363a9cbScegger 	memset((void *)&td->td_ti6, 0, sizeof (td->td_ti6));
132118d2b1dSitojun #endif
133118d2b1dSitojun 	th = NULL;
134118d2b1dSitojun 	if (m) {
135118d2b1dSitojun 		struct ip *ip;
136118d2b1dSitojun 		ip = mtod(m, struct ip *);
137118d2b1dSitojun 		switch (ip->ip_v) {
138118d2b1dSitojun 		case 4:
139118d2b1dSitojun 			if (m->m_len < sizeof(td->td_ti))
140118d2b1dSitojun 				break;
141e2cb8590Scegger 			memcpy(&td->td_ti, mtod(m, void *), sizeof(td->td_ti));
14253524e44Schristos 			th = (struct tcphdr *)((char *)&td->td_ti +
143f8bcc940Srpaulo 			    sizeof(struct ip));
144118d2b1dSitojun 			break;
145118d2b1dSitojun #ifdef INET6
146118d2b1dSitojun 		case 6:
147118d2b1dSitojun 			if (m->m_len < sizeof(td->td_ti6))
148118d2b1dSitojun 				break;
149e2cb8590Scegger 			memcpy(&td->td_ti6, mtod(m, void *),
150118d2b1dSitojun 				sizeof(td->td_ti6));
15153524e44Schristos 			th = (struct tcphdr *)((char *)&td->td_ti6 +
152f8bcc940Srpaulo 			    sizeof(struct ip6_hdr));
153118d2b1dSitojun 			break;
154118d2b1dSitojun #endif
155118d2b1dSitojun 		}
156118d2b1dSitojun 	}
15761f28255Scgd 	td->td_req = req;
15861f28255Scgd 	if (tcpconsdebug == 0)
15961f28255Scgd 		return;
16061f28255Scgd 	if (tp)
1612f72fbeeSabs 		printf("%p %s:", tp, tcpstates[ostate]);
16261f28255Scgd 	else
1635545959dSchristos 		printf("???????? ");
1645545959dSchristos 	printf("%s ", tanames[act]);
16561f28255Scgd 	switch (act) {
16661f28255Scgd 
16761f28255Scgd 	case TA_INPUT:
16861f28255Scgd 	case TA_OUTPUT:
16961f28255Scgd 	case TA_DROP:
170118d2b1dSitojun 		if (th == 0)
17161f28255Scgd 			break;
172118d2b1dSitojun 		seq = th->th_seq;
173118d2b1dSitojun 		ack = th->th_ack;
1742f72fbeeSabs 		len = m->m_pkthdr.len;
17561f28255Scgd 		if (act == TA_OUTPUT) {
17661f28255Scgd 			seq = ntohl(seq);
17761f28255Scgd 			ack = ntohl(ack);
17880929f85Scgd 			len = ntohs((u_int16_t)len);
17961f28255Scgd 		}
18061f28255Scgd 		if (act == TA_OUTPUT)
18161f28255Scgd 			len -= sizeof (struct tcphdr);
18261f28255Scgd 		if (len)
1835545959dSchristos 			printf("[%x..%x)", seq, seq+len);
18461f28255Scgd 		else
1855545959dSchristos 			printf("%x", seq);
186118d2b1dSitojun 		printf("@%x, urp=%x", ack, th->th_urp);
187118d2b1dSitojun 		flags = th->th_flags;
18861f28255Scgd 		if (flags) {
18961f28255Scgd #ifndef lint
190c8636820Sriz 			const char *cp = "<";
191f8bcc940Srpaulo #define pf(f) { if (th->th_flags&__CONCAT(TH_,f)) { \
192f8bcc940Srpaulo 	printf("%s%s", cp, "f"); cp = ","; } }
19361f28255Scgd 			pf(SYN); pf(ACK); pf(FIN); pf(RST); pf(PUSH); pf(URG);
19461f28255Scgd #endif
195ad67e041Schristos 			printf(">");
19661f28255Scgd 		}
19761f28255Scgd 		break;
19861f28255Scgd 
19961f28255Scgd 	case TA_USER:
2005545959dSchristos 		printf("%s", prurequests[req&0xff]);
20161f28255Scgd 		if ((req & 0xff) == PRU_SLOWTIMO)
2025545959dSchristos 			printf("<%s>", tcptimers[req>>8]);
20361f28255Scgd 		break;
20461f28255Scgd 	}
20561f28255Scgd 	if (tp)
2065545959dSchristos 		printf(" -> %s", tcpstates[tp->t_state]);
20761f28255Scgd 	/* print out internal state of tp !?! */
2085545959dSchristos 	printf("\n");
20961f28255Scgd 	if (tp == 0)
21061f28255Scgd 		return;
2112f72fbeeSabs 	printf("\trcv_(nxt,wnd,up) (%x,%lx,%x) snd_(una,nxt,max) (%x,%x,%x)\n",
21261f28255Scgd 	    tp->rcv_nxt, tp->rcv_wnd, tp->rcv_up, tp->snd_una, tp->snd_nxt,
21361f28255Scgd 	    tp->snd_max);
2142f72fbeeSabs 	printf("\tsnd_(wl1,wl2,wnd) (%x,%x,%lx)\n",
21561f28255Scgd 	    tp->snd_wl1, tp->snd_wl2, tp->snd_wnd);
21661f28255Scgd }
217