1 /* $OpenBSD: tcp_debug.c,v 1.4 1999/01/11 02:01:35 deraadt 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 #include <netinet6/ipv6.h> 85 #include <netinet6/in6.h> 86 #endif /* INET6 */ 87 88 #ifdef TCPDEBUG 89 int tcpconsdebug = 0; 90 #endif 91 /* 92 * Tcp debug routines 93 */ 94 void 95 tcp_trace(act, ostate, tp, ti, req, len) 96 short act, ostate; 97 struct tcpcb *tp; 98 struct tcpiphdr *ti; 99 int req; 100 int len; 101 { 102 #ifdef TCPDEBUG 103 tcp_seq seq, ack; 104 int flags; 105 #endif 106 struct tcp_debug *td = &tcp_debug[tcp_debx++]; 107 #ifdef INET6 108 struct tcphdr *th; 109 struct tcpipv6hdr *ti6 = (struct tcpipv6hdr *)ti; 110 #endif 111 112 if (tcp_debx == TCP_NDEBUG) 113 tcp_debx = 0; 114 td->td_time = iptime(); 115 td->td_act = act; 116 td->td_ostate = ostate; 117 td->td_tcb = (caddr_t)tp; 118 if (tp) 119 td->td_cb = *tp; 120 else 121 bzero((caddr_t)&td->td_cb, sizeof (*tp)); 122 #ifdef INET6 123 if (tp->pf == PF_INET6) { 124 if (ti) { 125 th = &ti6->ti6_t; 126 td->td_ti6 = *ti6; 127 } else { 128 bzero(&td->td_ti6, sizeof(struct tcpipv6hdr)); 129 } 130 } else { 131 if (ti) { 132 th = &ti->ti_t; 133 td->td_ti = *ti; 134 } else { 135 bzero(&td->td_ti, sizeof(struct tcpiphdr)); 136 } 137 } 138 #else /* INET6 */ 139 if (ti) 140 td->td_ti = *ti; 141 else 142 bzero((caddr_t)&td->td_ti, sizeof (*ti)); 143 #endif /* INET6 */ 144 145 td->td_req = req; 146 #ifdef TCPDEBUG 147 if (tcpconsdebug == 0) 148 return; 149 if (tp) 150 printf("%x %s:", tp, tcpstates[ostate]); 151 else 152 printf("???????? "); 153 printf("%s ", tanames[act]); 154 switch (act) { 155 156 case TA_INPUT: 157 case TA_OUTPUT: 158 case TA_DROP: 159 if (ti == 0) 160 break; 161 seq = th->th_seq; 162 ack = th->th_ack; 163 if (act == TA_OUTPUT) { 164 seq = ntohl(seq); 165 ack = ntohl(ack); 166 } 167 if (len) 168 printf("[%x..%x)", seq, seq+len); 169 else 170 printf("%x", seq); 171 printf("@%x, urp=%x", ack, th->th_urp); 172 flags = th->th_flags; 173 if (flags) { 174 #ifndef lint 175 char *cp = "<"; 176 #define pf(f) { if (th->th_flags&TH_/**/f) { printf("%s%s", cp, "f"); cp = ","; } } 177 pf(SYN); pf(ACK); pf(FIN); pf(RST); pf(PUSH); pf(URG); 178 #endif 179 printf(">"); 180 } 181 break; 182 183 case TA_USER: 184 printf("%s", prurequests[req&0xff]); 185 if ((req & 0xff) == PRU_SLOWTIMO) 186 printf("<%s>", tcptimers[req>>8]); 187 break; 188 } 189 if (tp) 190 printf(" -> %s", tcpstates[tp->t_state]); 191 /* print out internal state of tp !?! */ 192 printf("\n"); 193 if (tp == 0) 194 return; 195 printf("\trcv_(nxt,wnd,up) (%x,%x,%x) snd_(una,nxt,max) (%x,%x,%x)\n", 196 tp->rcv_nxt, tp->rcv_wnd, tp->rcv_up, tp->snd_una, tp->snd_nxt, 197 tp->snd_max); 198 printf("\tsnd_(wl1,wl2,wnd) (%x,%x,%x)\n", 199 tp->snd_wl1, tp->snd_wl2, tp->snd_wnd); 200 #endif /* TCPDEBUG */ 201 } 202