Home | History | Annotate | Line # | Download | only in netinet
tcp_debug.c revision 1.13.26.1
      1 /*	$NetBSD: tcp_debug.c,v 1.13.26.1 1999/06/28 06:37:01 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1993
      5  *	The Regents of the University of California.  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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)tcp_debug.c	8.1 (Berkeley) 6/10/93
     36  */
     37 
     38 #include "opt_inet.h"
     39 
     40 #ifdef TCPDEBUG
     41 /* load symbolic names */
     42 #define	PRUREQUESTS
     43 #define	TCPSTATES
     44 #define	TCPTIMERS
     45 #define	TANAMES
     46 #endif
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/mbuf.h>
     51 #include <sys/socket.h>
     52 #include <sys/socketvar.h>
     53 #include <sys/protosw.h>
     54 #include <sys/errno.h>
     55 
     56 #include <net/route.h>
     57 #include <net/if.h>
     58 
     59 #include <netinet/in.h>
     60 #include <netinet/in_systm.h>
     61 #include <netinet/ip.h>
     62 #include <netinet/in_pcb.h>
     63 #include <netinet/ip_var.h>
     64 
     65 #ifdef INET6
     66 #ifndef INET
     67 #include <netinet/in.h>
     68 #endif
     69 #include <netinet/ip6.h>
     70 #endif
     71 
     72 #include <netinet/tcp.h>
     73 #include <netinet/tcp_fsm.h>
     74 #include <netinet/tcp_seq.h>
     75 #include <netinet/tcp_timer.h>
     76 #include <netinet/tcp_var.h>
     77 #include <netinet/tcpip.h>
     78 #include <netinet/tcp_debug.h>
     79 
     80 #ifdef TCPDEBUG
     81 int	tcpconsdebug = 0;
     82 #endif
     83 /*
     84  * Tcp debug routines
     85  */
     86 void
     87 tcp_trace(act, ostate, tp, m, req)
     88 	short act, ostate;
     89 	struct tcpcb *tp;
     90 	struct mbuf *m;
     91 	int req;
     92 {
     93 #ifdef TCPDEBUG
     94 	tcp_seq seq, ack;
     95 	int len, flags;
     96 	struct tcphdr *th;
     97 #endif
     98 	struct tcp_debug *td = &tcp_debug[tcp_debx++];
     99 
    100 	if (tcp_debx == TCP_NDEBUG)
    101 		tcp_debx = 0;
    102 	td->td_time = iptime();
    103 	td->td_act = act;
    104 	td->td_ostate = ostate;
    105 	td->td_tcb = (caddr_t)tp;
    106 	if (tp)
    107 		td->td_cb = *tp;
    108 	else
    109 		bzero((caddr_t)&td->td_cb, sizeof (*tp));
    110 	td->td_family = tp->t_family;
    111 	bzero((caddr_t)&td->td_ti, sizeof (td->td_ti));
    112 #ifdef INET6
    113 	bzero((caddr_t)&td->td_ti6, sizeof (td->td_ti6));
    114 #endif
    115 #ifdef TCPDEBUG
    116 	th = NULL;
    117 #endif
    118 	if (m) {
    119 		struct ip *ip;
    120 		ip = mtod(m, struct ip *);
    121 		switch (ip->ip_v) {
    122 		case 4:
    123 			if (m->m_len < sizeof(td->td_ti))
    124 				break;
    125 			bcopy(mtod(m, caddr_t), &td->td_ti, sizeof(td->td_ti));
    126 #ifdef TCPDEBUG
    127 			th = (struct tcphdr *)((caddr_t)td->td_ti + sizeof(struct ip));
    128 #endif
    129 			break;
    130 #ifdef INET6
    131 		case 6:
    132 			if (m->m_len < sizeof(td->td_ti6))
    133 				break;
    134 			bcopy(mtod(m, caddr_t), &td->td_ti6,
    135 				sizeof(td->td_ti6));
    136 #ifdef TCPDEBUG
    137 			th = (struct tcphdr *)((caddr_t)td->td_ti6 + sizeof(struct ip6_hdr));
    138 #endif
    139 			break;
    140 #endif
    141 		}
    142 	}
    143 	td->td_req = req;
    144 #ifdef TCPDEBUG
    145 	if (tcpconsdebug == 0)
    146 		return;
    147 	if (tp)
    148 		printf("%x %s:", tp, tcpstates[ostate]);
    149 	else
    150 		printf("???????? ");
    151 	printf("%s ", tanames[act]);
    152 	switch (act) {
    153 
    154 	case TA_INPUT:
    155 	case TA_OUTPUT:
    156 	case TA_DROP:
    157 		if (th == 0)
    158 			break;
    159 		seq = th->th_seq;
    160 		ack = th->th_ack;
    161 		len = th->th_len;
    162 		if (act == TA_OUTPUT) {
    163 			seq = ntohl(seq);
    164 			ack = ntohl(ack);
    165 			len = ntohs((u_int16_t)len);
    166 		}
    167 		if (act == TA_OUTPUT)
    168 			len -= sizeof (struct tcphdr);
    169 		if (len)
    170 			printf("[%x..%x)", seq, seq+len);
    171 		else
    172 			printf("%x", seq);
    173 		printf("@%x, urp=%x", ack, th->th_urp);
    174 		flags = th->th_flags;
    175 		if (flags) {
    176 #ifndef lint
    177 			char *cp = "<";
    178 #define pf(f) { if (th->th_flags&TH_/**/f) { printf("%s%s", cp, "f"); cp = ","; } }
    179 			pf(SYN); pf(ACK); pf(FIN); pf(RST); pf(PUSH); pf(URG);
    180 #endif
    181 			printf(">");
    182 		}
    183 		break;
    184 
    185 	case TA_USER:
    186 		printf("%s", prurequests[req&0xff]);
    187 		if ((req & 0xff) == PRU_SLOWTIMO)
    188 			printf("<%s>", tcptimers[req>>8]);
    189 		break;
    190 	}
    191 	if (tp)
    192 		printf(" -> %s", tcpstates[tp->t_state]);
    193 	/* print out internal state of tp !?! */
    194 	printf("\n");
    195 	if (tp == 0)
    196 		return;
    197 	printf("\trcv_(nxt,wnd,up) (%x,%x,%x) snd_(una,nxt,max) (%x,%x,%x)\n",
    198 	    tp->rcv_nxt, tp->rcv_wnd, tp->rcv_up, tp->snd_una, tp->snd_nxt,
    199 	    tp->snd_max);
    200 	printf("\tsnd_(wl1,wl2,wnd) (%x,%x,%x)\n",
    201 	    tp->snd_wl1, tp->snd_wl2, tp->snd_wnd);
    202 #endif /* TCPDEBUG */
    203 }
    204