Home | History | Annotate | Line # | Download | only in netinet
tcp_debug.c revision 1.13.26.3
      1 /*	$NetBSD: tcp_debug.c,v 1.13.26.3 1999/11/30 13:35:34 itojun 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. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *	This product includes software developed by the University of
     47  *	California, Berkeley and its contributors.
     48  * 4. Neither the name of the University nor the names of its contributors
     49  *    may be used to endorse or promote products derived from this software
     50  *    without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  *
     64  *	@(#)tcp_debug.c	8.1 (Berkeley) 6/10/93
     65  */
     66 
     67 #include "opt_inet.h"
     68 
     69 #ifdef TCPDEBUG
     70 /* load symbolic names */
     71 #define	PRUREQUESTS
     72 #define	TCPSTATES
     73 #define	TCPTIMERS
     74 #define	TANAMES
     75 #endif
     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 #ifdef TCPDEBUG
    110 int	tcpconsdebug = 0;
    111 #endif
    112 /*
    113  * Tcp debug routines
    114  */
    115 void
    116 tcp_trace(act, ostate, tp, m, req)
    117 	short act, ostate;
    118 	struct tcpcb *tp;
    119 	struct mbuf *m;
    120 	int req;
    121 {
    122 #ifdef TCPDEBUG
    123 	tcp_seq seq, ack;
    124 	int len, flags;
    125 	struct tcphdr *th;
    126 #endif
    127 	struct tcp_debug *td = &tcp_debug[tcp_debx++];
    128 
    129 	if (tcp_debx == TCP_NDEBUG)
    130 		tcp_debx = 0;
    131 	td->td_time = iptime();
    132 	td->td_act = act;
    133 	td->td_ostate = ostate;
    134 	td->td_tcb = (caddr_t)tp;
    135 	if (tp)
    136 		td->td_cb = *tp;
    137 	else
    138 		bzero((caddr_t)&td->td_cb, sizeof (*tp));
    139 	td->td_family = tp->t_family;
    140 	bzero((caddr_t)&td->td_ti, sizeof (td->td_ti));
    141 #ifdef INET6
    142 	bzero((caddr_t)&td->td_ti6, sizeof (td->td_ti6));
    143 #endif
    144 #ifdef TCPDEBUG
    145 	th = NULL;
    146 #endif
    147 	if (m) {
    148 		struct ip *ip;
    149 		ip = mtod(m, struct ip *);
    150 		switch (ip->ip_v) {
    151 		case 4:
    152 			if (m->m_len < sizeof(td->td_ti))
    153 				break;
    154 			bcopy(mtod(m, caddr_t), &td->td_ti, sizeof(td->td_ti));
    155 #ifdef TCPDEBUG
    156 			th = (struct tcphdr *)((caddr_t)td->td_ti + sizeof(struct ip));
    157 #endif
    158 			break;
    159 #ifdef INET6
    160 		case 6:
    161 			if (m->m_len < sizeof(td->td_ti6))
    162 				break;
    163 			bcopy(mtod(m, caddr_t), &td->td_ti6,
    164 				sizeof(td->td_ti6));
    165 #ifdef TCPDEBUG
    166 			th = (struct tcphdr *)((caddr_t)td->td_ti6 + sizeof(struct ip6_hdr));
    167 #endif
    168 			break;
    169 #endif
    170 		}
    171 	}
    172 	td->td_req = req;
    173 #ifdef TCPDEBUG
    174 	if (tcpconsdebug == 0)
    175 		return;
    176 	if (tp)
    177 		printf("%x %s:", tp, tcpstates[ostate]);
    178 	else
    179 		printf("???????? ");
    180 	printf("%s ", tanames[act]);
    181 	switch (act) {
    182 
    183 	case TA_INPUT:
    184 	case TA_OUTPUT:
    185 	case TA_DROP:
    186 		if (th == 0)
    187 			break;
    188 		seq = th->th_seq;
    189 		ack = th->th_ack;
    190 		len = th->th_len;
    191 		if (act == TA_OUTPUT) {
    192 			seq = ntohl(seq);
    193 			ack = ntohl(ack);
    194 			len = ntohs((u_int16_t)len);
    195 		}
    196 		if (act == TA_OUTPUT)
    197 			len -= sizeof (struct tcphdr);
    198 		if (len)
    199 			printf("[%x..%x)", seq, seq+len);
    200 		else
    201 			printf("%x", seq);
    202 		printf("@%x, urp=%x", ack, th->th_urp);
    203 		flags = th->th_flags;
    204 		if (flags) {
    205 #ifndef lint
    206 			char *cp = "<";
    207 #define pf(f) { if (th->th_flags&TH_/**/f) { printf("%s%s", cp, "f"); cp = ","; } }
    208 			pf(SYN); pf(ACK); pf(FIN); pf(RST); pf(PUSH); pf(URG);
    209 #endif
    210 			printf(">");
    211 		}
    212 		break;
    213 
    214 	case TA_USER:
    215 		printf("%s", prurequests[req&0xff]);
    216 		if ((req & 0xff) == PRU_SLOWTIMO)
    217 			printf("<%s>", tcptimers[req>>8]);
    218 		break;
    219 	}
    220 	if (tp)
    221 		printf(" -> %s", tcpstates[tp->t_state]);
    222 	/* print out internal state of tp !?! */
    223 	printf("\n");
    224 	if (tp == 0)
    225 		return;
    226 	printf("\trcv_(nxt,wnd,up) (%x,%x,%x) snd_(una,nxt,max) (%x,%x,%x)\n",
    227 	    tp->rcv_nxt, tp->rcv_wnd, tp->rcv_up, tp->snd_una, tp->snd_nxt,
    228 	    tp->snd_max);
    229 	printf("\tsnd_(wl1,wl2,wnd) (%x,%x,%x)\n",
    230 	    tp->snd_wl1, tp->snd_wl2, tp->snd_wnd);
    231 #endif /* TCPDEBUG */
    232 }
    233