tcp_debug.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* $OpenBSD: tcp_debug.c,v 1.23 2015/03/14 03:38:52 jsg Exp $ */
  2. /* $NetBSD: tcp_debug.c,v 1.10 1996/02/13 23:43:36 christos Exp $ */
  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. Neither the name of the University 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 REGENTS 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 REGENTS 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. * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995
  32. *
  33. * NRL grants permission for redistribution and use in source and binary
  34. * forms, with or without modification, of the software and documentation
  35. * created at NRL provided that the following conditions are met:
  36. *
  37. * 1. Redistributions of source code must retain the above copyright
  38. * notice, this list of conditions and the following disclaimer.
  39. * 2. Redistributions in binary form must reproduce the above copyright
  40. * notice, this list of conditions and the following disclaimer in the
  41. * documentation and/or other materials provided with the distribution.
  42. * 3. All advertising materials mentioning features or use of this software
  43. * must display the following acknowledgements:
  44. * This product includes software developed by the University of
  45. * California, Berkeley and its contributors.
  46. * This product includes software developed at the Information
  47. * Technology Division, US Naval Research Laboratory.
  48. * 4. Neither the name of the NRL 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. * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
  53. * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  54. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  55. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR
  56. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  57. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  58. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  59. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  60. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  61. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  62. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *
  64. * The views and conclusions contained in the software and documentation
  65. * are those of the authors and should not be interpreted as representing
  66. * official policies, either expressed or implied, of the US Naval
  67. * Research Laboratory (NRL).
  68. */
  69. #ifdef TCPDEBUG
  70. /* load symbolic names */
  71. #define PRUREQUESTS
  72. #define TCPSTATES
  73. #define TCPTIMERS
  74. #define TANAMES
  75. #endif
  76. #include <sys/param.h>
  77. #include <sys/systm.h>
  78. #include <sys/mbuf.h>
  79. #include <sys/socket.h>
  80. #include <sys/protosw.h>
  81. #include <net/route.h>
  82. #include <netinet/in.h>
  83. #include <netinet/in_systm.h>
  84. #include <netinet/ip.h>
  85. #include <netinet/in_pcb.h>
  86. #include <netinet/ip_var.h>
  87. #include <netinet/tcp.h>
  88. #include <netinet/tcp_timer.h>
  89. #include <netinet/tcp_var.h>
  90. #include <netinet/tcpip.h>
  91. #include <netinet/tcp_debug.h>
  92. #include <netinet/tcp_fsm.h>
  93. #ifdef INET6
  94. #include <netinet/ip6.h>
  95. #endif /* INET6 */
  96. #ifdef TCPDEBUG
  97. int tcpconsdebug = 0;
  98. #endif
  99. struct tcp_debug tcp_debug[TCP_NDEBUG];
  100. int tcp_debx;
  101. /*
  102. * Tcp debug routines
  103. */
  104. void
  105. tcp_trace(short act, short ostate, struct tcpcb *tp, caddr_t headers,
  106. int req, int len)
  107. {
  108. #ifdef TCPDEBUG
  109. tcp_seq seq, ack;
  110. int flags;
  111. #endif
  112. struct tcp_debug *td = &tcp_debug[tcp_debx++];
  113. struct tcpiphdr *ti = (struct tcpiphdr *)headers;
  114. struct tcphdr *th;
  115. #ifdef INET6
  116. struct tcpipv6hdr *ti6 = (struct tcpipv6hdr *)ti;
  117. #endif
  118. if (tcp_debx == TCP_NDEBUG)
  119. tcp_debx = 0;
  120. td->td_time = iptime();
  121. td->td_act = act;
  122. td->td_ostate = ostate;
  123. td->td_tcb = (caddr_t)tp;
  124. if (tp)
  125. td->td_cb = *tp;
  126. else
  127. bzero((caddr_t)&td->td_cb, sizeof (*tp));
  128. switch (tp->pf) {
  129. #ifdef INET6
  130. case PF_INET6:
  131. if (ti6) {
  132. th = &ti6->ti6_t;
  133. td->td_ti6 = *ti6;
  134. td->td_ti6.ti6_plen = len;
  135. } else
  136. bzero(&td->td_ti6, sizeof(struct tcpipv6hdr));
  137. break;
  138. #endif /* INET6 */
  139. case PF_INET:
  140. if (ti) {
  141. th = &ti->ti_t;
  142. td->td_ti = *ti;
  143. td->td_ti.ti_len = len;
  144. } else
  145. bzero(&td->td_ti, sizeof(struct tcpiphdr));
  146. break;
  147. default:
  148. return;
  149. }
  150. td->td_req = req;
  151. #ifdef TCPDEBUG
  152. if (tcpconsdebug == 0)
  153. return;
  154. if (tp)
  155. printf("%x %s:", tp, tcpstates[ostate]);
  156. else
  157. printf("???????? ");
  158. printf("%s ", tanames[act]);
  159. switch (act) {
  160. case TA_INPUT:
  161. case TA_OUTPUT:
  162. case TA_DROP:
  163. if (ti == 0)
  164. break;
  165. seq = th->th_seq;
  166. ack = th->th_ack;
  167. if (act == TA_OUTPUT) {
  168. seq = ntohl(seq);
  169. ack = ntohl(ack);
  170. }
  171. if (len)
  172. printf("[%x..%x)", seq, seq+len);
  173. else
  174. printf("%x", seq);
  175. printf("@%x, urp=%x", ack, th->th_urp);
  176. flags = th->th_flags;
  177. if (flags) {
  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. printf(">");
  182. }
  183. break;
  184. case TA_USER:
  185. printf("%s", prurequests[req&0xff]);
  186. if ((req & 0xff) == PRU_SLOWTIMO)
  187. printf("<%s>", tcptimers[req>>8]);
  188. break;
  189. }
  190. if (tp)
  191. printf(" -> %s", tcpstates[tp->t_state]);
  192. /* print out internal state of tp !?! */
  193. printf("\n");
  194. if (tp == 0)
  195. return;
  196. printf("\trcv_(nxt,wnd,up) (%x,%x,%x) snd_(una,nxt,max) (%x,%x,%x)\n",
  197. tp->rcv_nxt, tp->rcv_wnd, tp->rcv_up, tp->snd_una, tp->snd_nxt,
  198. tp->snd_max);
  199. printf("\tsnd_(wl1,wl2,wnd) (%x,%x,%x)\n",
  200. tp->snd_wl1, tp->snd_wl2, tp->snd_wnd);
  201. #endif /* TCPDEBUG */
  202. }