xref: /openbsd-src/sys/netinet/tcp_debug.c (revision 1fc27e414118cd8922c6b93fbaeb7a5246bfd593)
1 /*	$OpenBSD: tcp_debug.c,v 1.7 2000/02/07 06:09:09 itojun 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  *	@(#)tcp_debug.c	8.1 (Berkeley) 6/10/93
37  */
38 
39 /*
40 %%% portions-copyright-nrl-95
41 Portions of this software are Copyright 1995-1998 by Randall Atkinson,
42 Ronald Lee, Daniel McDonald, Bao Phan, and Chris Winters. All Rights
43 Reserved. All rights under this copyright have been assigned to the US
44 Naval Research Laboratory (NRL). The NRL Copyright Notice and License
45 Agreement Version 1.1 (January 17, 1995) applies to these portions of the
46 software.
47 You should have received a copy of the license with this software. If you
48 didn't get a copy, you may request one from <license@ipv6.nrl.navy.mil>.
49 */
50 
51 #ifdef TCPDEBUG
52 /* load symbolic names */
53 #define	PRUREQUESTS
54 #define	TCPSTATES
55 #define	TCPTIMERS
56 #define	TANAMES
57 #endif
58 
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/mbuf.h>
62 #include <sys/socket.h>
63 #include <sys/socketvar.h>
64 #include <sys/protosw.h>
65 #include <sys/errno.h>
66 
67 #include <net/route.h>
68 #include <net/if.h>
69 
70 #include <netinet/in.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #include <netinet/in_pcb.h>
74 #include <netinet/ip_var.h>
75 #include <netinet/tcp.h>
76 #include <netinet/tcp_fsm.h>
77 #include <netinet/tcp_seq.h>
78 #include <netinet/tcp_timer.h>
79 #include <netinet/tcp_var.h>
80 #include <netinet/tcpip.h>
81 #include <netinet/tcp_debug.h>
82 
83 #ifdef INET6
84 #ifndef INET
85 #include <netinet/in.h>
86 #endif
87 #include <netinet/ip6.h>
88 #endif /* INET6 */
89 
90 #ifdef TCPDEBUG
91 int	tcpconsdebug = 0;
92 #endif
93 /*
94  * Tcp debug routines
95  */
96 void
97 tcp_trace(act, ostate, tp, headers, req, len)
98 	short act, ostate;
99 	struct tcpcb *tp;
100 	caddr_t headers;
101 	int req;
102 	int len;
103 {
104 #ifdef TCPDEBUG
105 	tcp_seq seq, ack;
106 	int flags;
107 #endif
108 	struct tcp_debug *td = &tcp_debug[tcp_debx++];
109 	struct tcpiphdr *ti = (struct tcpiphdr *)headers;
110 #ifdef INET6
111 	struct tcphdr *th;
112 	struct tcpipv6hdr *ti6 = (struct tcpipv6hdr *)ti;
113 #endif
114 
115 	if (tcp_debx == TCP_NDEBUG)
116 		tcp_debx = 0;
117 	td->td_time = iptime();
118 	td->td_act = act;
119 	td->td_ostate = ostate;
120 	td->td_tcb = (caddr_t)tp;
121 	if (tp)
122 		td->td_cb = *tp;
123 	else
124 		bzero((caddr_t)&td->td_cb, sizeof (*tp));
125 #ifdef INET6
126 	if (tp->pf == PF_INET6) {
127 		if (ti) {
128 			th = &ti6->ti6_t;
129 			td->td_ti6 = *ti6;
130 		} else {
131 			bzero(&td->td_ti6, sizeof(struct tcpipv6hdr));
132 		}
133 	} else {
134 		if (ti) {
135 			th = &ti->ti_t;
136 			td->td_ti = *ti;
137 		} else {
138 			bzero(&td->td_ti, sizeof(struct tcpiphdr));
139 		}
140 	}
141 #else /* INET6 */
142 	if (ti)
143 		td->td_ti = *ti;
144 	else
145 		bzero((caddr_t)&td->td_ti, sizeof (*ti));
146 #endif /* INET6 */
147 
148 	td->td_req = req;
149 #ifdef TCPDEBUG
150 	if (tcpconsdebug == 0)
151 		return;
152 	if (tp)
153 		printf("%x %s:", tp, tcpstates[ostate]);
154 	else
155 		printf("???????? ");
156 	printf("%s ", tanames[act]);
157 	switch (act) {
158 
159 	case TA_INPUT:
160 	case TA_OUTPUT:
161 	case TA_DROP:
162 		if (ti == 0)
163 			break;
164 		seq = th->th_seq;
165 		ack = th->th_ack;
166 		if (act == TA_OUTPUT) {
167 			seq = ntohl(seq);
168 			ack = ntohl(ack);
169 		}
170 		if (len)
171 			printf("[%x..%x)", seq, seq+len);
172 		else
173 			printf("%x", seq);
174 		printf("@%x, urp=%x", ack, th->th_urp);
175 		flags = th->th_flags;
176 		if (flags) {
177 #ifndef lint
178 			char *cp = "<";
179 #define pf(f) { if (th->th_flags&TH_/**/f) { printf("%s%s", cp, "f"); cp = ","; } }
180 			pf(SYN); pf(ACK); pf(FIN); pf(RST); pf(PUSH); pf(URG);
181 #endif
182 			printf(">");
183 		}
184 		break;
185 
186 	case TA_USER:
187 		printf("%s", prurequests[req&0xff]);
188 		if ((req & 0xff) == PRU_SLOWTIMO)
189 			printf("<%s>", tcptimers[req>>8]);
190 		break;
191 	}
192 	if (tp)
193 		printf(" -> %s", tcpstates[tp->t_state]);
194 	/* print out internal state of tp !?! */
195 	printf("\n");
196 	if (tp == 0)
197 		return;
198 	printf("\trcv_(nxt,wnd,up) (%x,%x,%x) snd_(una,nxt,max) (%x,%x,%x)\n",
199 	    tp->rcv_nxt, tp->rcv_wnd, tp->rcv_up, tp->snd_una, tp->snd_nxt,
200 	    tp->snd_max);
201 	printf("\tsnd_(wl1,wl2,wnd) (%x,%x,%x)\n",
202 	    tp->snd_wl1, tp->snd_wl2, tp->snd_wnd);
203 #endif /* TCPDEBUG */
204 }
205