1 /* $OpenBSD: tcp_debug.c,v 1.32 2024/04/10 22:24:07 bluhm 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. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995 33 * 34 * NRL grants permission for redistribution and use in source and binary 35 * forms, with or without modification, of the software and documentation 36 * created at NRL provided that the following conditions are met: 37 * 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. All advertising materials mentioning features or use of this software 44 * must display the following acknowledgements: 45 * This product includes software developed by the University of 46 * California, Berkeley and its contributors. 47 * This product includes software developed at the Information 48 * Technology Division, US Naval Research Laboratory. 49 * 4. Neither the name of the NRL nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 54 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 56 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 57 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 58 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 59 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 60 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 61 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 62 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 63 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 64 * 65 * The views and conclusions contained in the software and documentation 66 * are those of the authors and should not be interpreted as representing 67 * official policies, either expressed or implied, of the US Naval 68 * Research Laboratory (NRL). 69 */ 70 71 #ifdef TCPDEBUG 72 /* load symbolic names */ 73 #define PRUREQUESTS 74 #define TCPSTATES 75 #define TCPTIMERS 76 #define TANAMES 77 #endif 78 79 #include <sys/param.h> 80 #include <sys/systm.h> 81 #include <sys/mbuf.h> 82 #include <sys/mutex.h> 83 #include <sys/socket.h> 84 85 #include <net/route.h> 86 87 #include <netinet/in.h> 88 #include <netinet/in_systm.h> 89 #include <netinet/ip.h> 90 #include <netinet/in_pcb.h> 91 #include <netinet/ip_var.h> 92 #include <netinet/tcp.h> 93 #include <netinet/tcp_timer.h> 94 #include <netinet/tcp_var.h> 95 #include <netinet/tcp_debug.h> 96 #include <netinet/tcp_fsm.h> 97 98 #ifdef INET6 99 #include <netinet/ip6.h> 100 #endif /* INET6 */ 101 102 #ifdef TCPDEBUG 103 #include <sys/protosw.h> 104 #endif 105 106 /* 107 * Locks used to protect struct members in this file: 108 * D TCP debug global mutex 109 */ 110 111 struct mutex tcp_debug_mtx = MUTEX_INITIALIZER(IPL_SOFTNET); 112 113 #ifdef TCPDEBUG 114 int tcpconsdebug = 0; 115 #endif 116 117 struct tcp_debug tcp_debug[TCP_NDEBUG]; /* [D] */ 118 int tcp_debx; /* [D] */ 119 120 /* 121 * Tcp debug routines 122 */ 123 void 124 tcp_trace(short act, short ostate, struct tcpcb *tp, struct tcpcb *otp, 125 caddr_t headers, int req, int len) 126 { 127 #ifdef TCPDEBUG 128 struct tcphdr *th; 129 tcp_seq seq, ack; 130 int flags; 131 #endif 132 int pf = PF_UNSPEC; 133 struct tcp_debug *td; 134 struct tcpiphdr *ti; 135 struct tcpipv6hdr *ti6; 136 137 mtx_enter(&tcp_debug_mtx); 138 139 td = &tcp_debug[tcp_debx++]; 140 ti = (struct tcpiphdr *)headers; 141 ti6 = (struct tcpipv6hdr *)headers; 142 143 if (tcp_debx == TCP_NDEBUG) 144 tcp_debx = 0; 145 td->td_time = iptime(); 146 td->td_act = act; 147 td->td_ostate = ostate; 148 td->td_tcb = (caddr_t)otp; 149 if (tp) { 150 pf = tp->pf; 151 td->td_cb = *tp; 152 } else 153 bzero((caddr_t)&td->td_cb, sizeof (*tp)); 154 155 bzero(&td->td_ti6, sizeof(struct tcpipv6hdr)); 156 bzero(&td->td_ti, sizeof(struct tcpiphdr)); 157 if (headers) { 158 /* The address family may be in tcpcb or ip header. */ 159 if (pf == PF_UNSPEC) { 160 switch (ti6->ti6_i.ip6_vfc & IPV6_VERSION_MASK) { 161 #ifdef INET6 162 case IPV6_VERSION: 163 pf = PF_INET6; 164 break; 165 #endif /* INET6 */ 166 case IPVERSION: 167 pf = PF_INET; 168 break; 169 } 170 } 171 switch (pf) { 172 #ifdef INET6 173 case PF_INET6: 174 #ifdef TCPDEBUG 175 th = &ti6->ti6_t; 176 #endif 177 td->td_ti6 = *ti6; 178 td->td_ti6.ti6_plen = len; 179 break; 180 #endif /* INET6 */ 181 case PF_INET: 182 #ifdef TCPDEBUG 183 th = &ti->ti_t; 184 #endif 185 td->td_ti = *ti; 186 td->td_ti.ti_len = len; 187 break; 188 default: 189 headers = NULL; 190 break; 191 } 192 } 193 194 td->td_req = req; 195 #ifdef TCPDEBUG 196 if (tcpconsdebug == 0) 197 goto done; 198 if (otp) 199 printf("%p %s:", otp, tcpstates[ostate]); 200 else 201 printf("???????? "); 202 printf("%s ", tanames[act]); 203 switch (act) { 204 205 case TA_INPUT: 206 case TA_OUTPUT: 207 case TA_DROP: 208 if (headers == NULL) 209 break; 210 seq = th->th_seq; 211 ack = th->th_ack; 212 if (act == TA_OUTPUT) { 213 seq = ntohl(seq); 214 ack = ntohl(ack); 215 } 216 if (len) 217 printf("[%x..%x)", seq, seq+len); 218 else 219 printf("%x", seq); 220 printf("@%x, urp=%x", ack, th->th_urp); 221 flags = th->th_flags; 222 if (flags) { 223 char *cp = "<"; 224 #define pf(f) { if (th->th_flags&TH_##f) { printf("%s%s", cp, #f); cp = ","; } } 225 pf(SYN); pf(ACK); pf(FIN); pf(RST); pf(PUSH); pf(URG); 226 printf(">"); 227 } 228 break; 229 230 case TA_USER: 231 printf("%s", prurequests[req]); 232 break; 233 234 case TA_TIMER: 235 printf("%s", tcptimers[req]); 236 break; 237 } 238 if (tp) 239 printf(" -> %s", tcpstates[tp->t_state]); 240 /* print out internal state of tp !?! */ 241 printf("\n"); 242 if (tp == NULL) 243 goto done; 244 printf("\trcv_(nxt,wnd,up) (%x,%lx,%x) snd_(una,nxt,max) (%x,%x,%x)\n", 245 tp->rcv_nxt, tp->rcv_wnd, tp->rcv_up, tp->snd_una, tp->snd_nxt, 246 tp->snd_max); 247 printf("\tsnd_(wl1,wl2,wnd) (%x,%x,%lx)\n", 248 tp->snd_wl1, tp->snd_wl2, tp->snd_wnd); 249 250 done: 251 #endif /* TCPDEBUG */ 252 mtx_leave(&tcp_debug_mtx); 253 } 254