1c398230bSWarner Losh /*- 2e79adb8eSGarrett Wollman * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 29e79adb8eSGarrett Wollman * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 324b421e2dSMike Silbersack #include <sys/cdefs.h> 334b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 344b421e2dSMike Silbersack 35f9e354dfSJulian Elischer #include "opt_ipfw.h" /* for ipfw_fwd */ 361cfd4b53SBruce M Simpson #include "opt_inet.h" 37fb59c426SYoshinobu Inoue #include "opt_inet6.h" 383a2a9f79SYoshinobu Inoue #include "opt_ipsec.h" 39c488362eSRobert Watson #include "opt_mac.h" 400cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 410cc12cc5SJoerg Wunsch 42df8bae1dSRodney W. Grimes #include <sys/param.h> 4398163b98SPoul-Henning Kamp #include <sys/kernel.h> 44df8bae1dSRodney W. Grimes #include <sys/malloc.h> 45df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 46a29f300eSGarrett Wollman #include <sys/proc.h> /* for proc0 declaration */ 47df8bae1dSRodney W. Grimes #include <sys/protosw.h> 48960ed29cSSeigo Tanimura #include <sys/signalvar.h> 49df8bae1dSRodney W. Grimes #include <sys/socket.h> 50df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 51960ed29cSSeigo Tanimura #include <sys/sysctl.h> 52816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 53960ed29cSSeigo Tanimura #include <sys/systm.h> 54603724d3SBjoern A. Zeeb #include <sys/vimage.h> 55df8bae1dSRodney W. Grimes 56e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 57e79adb8eSGarrett Wollman 5812e2e970SAndre Oppermann #include <vm/uma.h> 5912e2e970SAndre Oppermann 60df8bae1dSRodney W. Grimes #include <net/if.h> 61df8bae1dSRodney W. Grimes #include <net/route.h> 62df8bae1dSRodney W. Grimes 63773673c1SAndre Oppermann #define TCPSTATES /* for logging */ 64773673c1SAndre Oppermann 65df8bae1dSRodney W. Grimes #include <netinet/in.h> 66960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 67df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 68960ed29cSSeigo Tanimura #include <netinet/in_var.h> 69df8bae1dSRodney W. Grimes #include <netinet/ip.h> 708e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 71686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 72df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 73ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 74686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 75686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h> 76686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 77960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h> 78960ed29cSSeigo Tanimura #include <netinet6/nd6.h> 79df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 80df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 81df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 82df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 83df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 84fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 85df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 86c325962bSMike Silbersack #include <netinet/tcp_syncache.h> 87610ee2f9SDavid Greenman #ifdef TCPDEBUG 88df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 89fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */ 904b79449eSBjoern A. Zeeb #include <netinet/vinet.h> 914b79449eSBjoern A. Zeeb 924b79449eSBjoern A. Zeeb #ifdef INET6 934b79449eSBjoern A. Zeeb #include <netinet6/vinet6.h> 944b79449eSBjoern A. Zeeb #endif 95fb59c426SYoshinobu Inoue 96b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 97b9234fafSSam Leffler #include <netipsec/ipsec.h> 98b9234fafSSam Leffler #include <netipsec/ipsec6.h> 99b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 100b9234fafSSam Leffler 101db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 102db4f9cc7SJonathan Lemon 103aed55708SRobert Watson #include <security/mac/mac_framework.h> 104aed55708SRobert Watson 105c068736aSJeffrey Hsu static const int tcprexmtthresh = 3; 1060312fbe9SPoul-Henning Kamp 10744e33a07SMarko Zec #ifdef VIMAGE_GLOBALS 1082f96f1f4SGarrett Wollman struct tcpstat tcpstat; 10944e33a07SMarko Zec int blackhole; 11044e33a07SMarko Zec int tcp_delack_enabled; 11144e33a07SMarko Zec int drop_synfin; 11244e33a07SMarko Zec int tcp_do_rfc3042; 11344e33a07SMarko Zec int tcp_do_rfc3390; 11444e33a07SMarko Zec int tcp_do_ecn; 11544e33a07SMarko Zec int tcp_ecn_maxretries; 11644e33a07SMarko Zec int tcp_insecure_rst; 11744e33a07SMarko Zec int tcp_do_autorcvbuf; 11844e33a07SMarko Zec int tcp_autorcvbuf_inc; 11944e33a07SMarko Zec int tcp_autorcvbuf_max; 12024cb0f22SLawrence Stewart int tcp_do_rfc3465; 12124cb0f22SLawrence Stewart int tcp_abc_l_var; 12244e33a07SMarko Zec #endif 12344e33a07SMarko Zec 1248b615593SMarko Zec SYSCTL_V_STRUCT(V_NET, vnet_inet, _net_inet_tcp, TCPCTL_STATS, stats, 1258b615593SMarko Zec CTLFLAG_RW, tcpstat , tcpstat, 1268b615593SMarko Zec "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 1270312fbe9SPoul-Henning Kamp 128773673c1SAndre Oppermann int tcp_log_in_vain = 0; 129816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 130574b6964SAndre Oppermann &tcp_log_in_vain, 0, "Log all incoming TCP segments to closed ports"); 131816a3d83SPoul-Henning Kamp 1328b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 1338b615593SMarko Zec blackhole, 0, "Do not send RST on segments to closed ports"); 13416f7f31fSGeoff Rehmet 1358b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, delayed_ack, 1368b615593SMarko Zec CTLFLAG_RW, tcp_delack_enabled, 0, 1373d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 138f498eeeeSDavid Greenman 1398b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, drop_synfin, 1408b615593SMarko Zec CTLFLAG_RW, drop_synfin, 0, "Drop TCP packets with SYN+FIN set"); 141f8613305SDag-Erling Smørgrav 1428b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW, 1438b615593SMarko Zec tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)"); 144582a954bSJeffrey Hsu 1458b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW, 1468b615593SMarko Zec tcp_do_rfc3390, 0, 147da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 148da3a8a1aSJeffrey Hsu 14924cb0f22SLawrence Stewart SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_RW, 15024cb0f22SLawrence Stewart tcp_do_rfc3465, 0, 15124cb0f22SLawrence Stewart "Enable RFC 3465 (Appropriate Byte Counting)"); 15224cb0f22SLawrence Stewart SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_RW, 15324cb0f22SLawrence Stewart tcp_abc_l_var, 2, 15424cb0f22SLawrence Stewart "Cap the max cwnd increment during slow-start to this number of segments"); 15524cb0f22SLawrence Stewart 156f2512ba1SRui Paulo SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN"); 1578b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_ecn, OID_AUTO, enable, 1588b615593SMarko Zec CTLFLAG_RW, tcp_do_ecn, 0, "TCP ECN support"); 1598b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_ecn, OID_AUTO, maxretries, 1608b615593SMarko Zec CTLFLAG_RW, tcp_ecn_maxretries, 0, "Max retries before giving up on ECN"); 161f2512ba1SRui Paulo 1628b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, insecure_rst, 1638b615593SMarko Zec CTLFLAG_RW, tcp_insecure_rst, 0, 1646489fe65SAndre Oppermann "Follow the old (insecure) criteria for accepting RST packets"); 165a69968eeSMike Silbersack 1668b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, recvbuf_auto, 1678b615593SMarko Zec CTLFLAG_RW, tcp_do_autorcvbuf, 0, 1688b615593SMarko Zec "Enable automatic receive buffer sizing"); 1696741ecf5SAndre Oppermann 1708b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, recvbuf_inc, 1718b615593SMarko Zec CTLFLAG_RW, tcp_autorcvbuf_inc, 0, 1726489fe65SAndre Oppermann "Incrementor step size of automatic receive buffer"); 1736741ecf5SAndre Oppermann 1748b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, recvbuf_max, 1758b615593SMarko Zec CTLFLAG_RW, tcp_autorcvbuf_max, 0, 1768b615593SMarko Zec "Max size of automatic receive buffer"); 1776741ecf5SAndre Oppermann 178252ca428SRobert Watson int tcp_read_locking = 1; 179252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, read_locking, CTLFLAG_RW, 180252ca428SRobert Watson &tcp_read_locking, 0, "Enable read locking strategy"); 181252ca428SRobert Watson 182252ca428SRobert Watson int tcp_rlock_atfirst; 183252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, rlock_atfirst, CTLFLAG_RD, 184252ca428SRobert Watson &tcp_rlock_atfirst, 0, ""); 185252ca428SRobert Watson 186252ca428SRobert Watson int tcp_wlock_atfirst; 187252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcp_wlock_atfirst, CTLFLAG_RD, 188252ca428SRobert Watson &tcp_wlock_atfirst, 0, ""); 189252ca428SRobert Watson 190252ca428SRobert Watson int tcp_wlock_upgraded; 191252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, wlock_upgraded, CTLFLAG_RD, 192252ca428SRobert Watson &tcp_wlock_upgraded, 0, ""); 193252ca428SRobert Watson 194252ca428SRobert Watson int tcp_wlock_relocked; 195252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, wlock_relocked, CTLFLAG_RD, 196252ca428SRobert Watson &tcp_wlock_relocked, 0, ""); 197252ca428SRobert Watson 198252ca428SRobert Watson int tcp_wlock_looped; 199252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, wlock_looped, CTLFLAG_RD, 200252ca428SRobert Watson &tcp_wlock_looped, 0, ""); 201252ca428SRobert Watson 20244e33a07SMarko Zec #ifdef VIMAGE_GLOBALS 20315bd2b43SDavid Greenman struct inpcbhead tcb; 20415bd2b43SDavid Greenman struct inpcbinfo tcbinfo; 20544e33a07SMarko Zec #endif 20644e33a07SMarko Zec #define tcb6 tcb /* for KAME src sync over BSD*'s */ 207df8bae1dSRodney W. Grimes 2085a53ca16SPaul Saab static void tcp_dooptions(struct tcpopt *, u_char *, int, int); 209679d9708SAndre Oppermann static void tcp_do_segment(struct mbuf *, struct tcphdr *, 210252ca428SRobert Watson struct socket *, struct tcpcb *, int, int, uint8_t, 211252ca428SRobert Watson int); 212302ce8d6SAndre Oppermann static void tcp_dropwithreset(struct mbuf *, struct tcphdr *, 213302ce8d6SAndre Oppermann struct tcpcb *, int, int); 2144d77a549SAlfred Perlstein static void tcp_pulloutofband(struct socket *, 2154d77a549SAlfred Perlstein struct tcphdr *, struct mbuf *, int); 2164d77a549SAlfred Perlstein static void tcp_xmit_timer(struct tcpcb *, int); 217c068736aSJeffrey Hsu static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 218f2512ba1SRui Paulo static void inline 219f2512ba1SRui Paulo tcp_congestion_exp(struct tcpcb *); 220f2512ba1SRui Paulo 221f2512ba1SRui Paulo static void inline 222f2512ba1SRui Paulo tcp_congestion_exp(struct tcpcb *tp) 223f2512ba1SRui Paulo { 224f2512ba1SRui Paulo u_int win; 225f2512ba1SRui Paulo 226f2512ba1SRui Paulo win = min(tp->snd_wnd, tp->snd_cwnd) / 227f2512ba1SRui Paulo 2 / tp->t_maxseg; 228f2512ba1SRui Paulo if (win < 2) 229f2512ba1SRui Paulo win = 2; 230f2512ba1SRui Paulo tp->snd_ssthresh = win * tp->t_maxseg; 231f2512ba1SRui Paulo ENTER_FASTRECOVERY(tp); 232f2512ba1SRui Paulo tp->snd_recover = tp->snd_max; 233f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) 234f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_CWR; 235f2512ba1SRui Paulo } 2360312fbe9SPoul-Henning Kamp 237fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 238fb59c426SYoshinobu Inoue #ifdef INET6 239fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \ 240fb59c426SYoshinobu Inoue do { \ 241fb59c426SYoshinobu Inoue if ((tp) && (tp)->t_inpcb && \ 24297d8d152SAndre Oppermann ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \ 24397d8d152SAndre Oppermann nd6_nud_hint(NULL, NULL, 0); \ 244fb59c426SYoshinobu Inoue } while (0) 245fb59c426SYoshinobu Inoue #else 246fb59c426SYoshinobu Inoue #define ND6_HINT(tp) 247fb59c426SYoshinobu Inoue #endif 248df8bae1dSRodney W. Grimes 249df8bae1dSRodney W. Grimes /* 250262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 251262c1c1aSMatthew Dillon * - there is no delayed ack timer in progress and 252262c1c1aSMatthew Dillon * - our last ack wasn't a 0-sized window. We never want to delay 2533bfd6421SJonathan Lemon * the ack that opens up a 0-sized window and 2543bfd6421SJonathan Lemon * - delayed acks are enabled or 2553bfd6421SJonathan Lemon * - this is a half-synchronized T/TCP connection. 256d8c85a26SJonathan Lemon */ 257d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \ 258b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 259a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 260603724d3SBjoern A. Zeeb (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 261d8c85a26SJonathan Lemon 262df8bae1dSRodney W. Grimes /* 2638d573cc1SAndre Oppermann * TCP input handling is split into multiple parts: 2648d573cc1SAndre Oppermann * tcp6_input is a thin wrapper around tcp_input for the extended 2658d573cc1SAndre Oppermann * ip6_protox[] call format in ip6_input 2668d573cc1SAndre Oppermann * tcp_input handles primary segment validation, inpcb lookup and 2678d573cc1SAndre Oppermann * SYN processing on listen sockets 2688d573cc1SAndre Oppermann * tcp_do_segment processes the ACK and text of the segment for 2698d573cc1SAndre Oppermann * establishing, established and closing connections 270df8bae1dSRodney W. Grimes */ 271fb59c426SYoshinobu Inoue #ifdef INET6 272fb59c426SYoshinobu Inoue int 273ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto) 274fb59c426SYoshinobu Inoue { 2758b615593SMarko Zec INIT_VNET_INET6(curvnet); 276ad3f9ab3SAndre Oppermann struct mbuf *m = *mp; 27733841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 278fb59c426SYoshinobu Inoue 279fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 280fb59c426SYoshinobu Inoue 281fb59c426SYoshinobu Inoue /* 282fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 283fb59c426SYoshinobu Inoue * better place to put this in? 284fb59c426SYoshinobu Inoue */ 28533841545SHajimu UMEMOTO ia6 = ip6_getdstifaddr(m); 28633841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 287fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 288fb59c426SYoshinobu Inoue 289fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 290fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 291fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 292fb59c426SYoshinobu Inoue return IPPROTO_DONE; 293fb59c426SYoshinobu Inoue } 294fb59c426SYoshinobu Inoue 295f0ffb944SJulian Elischer tcp_input(m, *offp); 296fb59c426SYoshinobu Inoue return IPPROTO_DONE; 297fb59c426SYoshinobu Inoue } 298fb59c426SYoshinobu Inoue #endif 299fb59c426SYoshinobu Inoue 300df8bae1dSRodney W. Grimes void 301ad3f9ab3SAndre Oppermann tcp_input(struct mbuf *m, int off0) 302df8bae1dSRodney W. Grimes { 3038b615593SMarko Zec INIT_VNET_INET(curvnet); 3048b615593SMarko Zec #ifdef INET6 3058b615593SMarko Zec INIT_VNET_INET6(curvnet); 3068b615593SMarko Zec #endif 3078b615593SMarko Zec #ifdef IPSEC 3088b615593SMarko Zec INIT_VNET_IPSEC(curvnet); 3098b615593SMarko Zec #endif 310ad3f9ab3SAndre Oppermann struct tcphdr *th; 311ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 312ad3f9ab3SAndre Oppermann struct ipovly *ipov; 313ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 314302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 315302ce8d6SAndre Oppermann struct socket *so = NULL; 316e79adb8eSGarrett Wollman u_char *optp = NULL; 31726f9a767SRodney W. Grimes int optlen = 0; 318a0194ef1SBruce M Simpson int len, tlen, off; 319fb59c426SYoshinobu Inoue int drop_hdrlen; 320ad3f9ab3SAndre Oppermann int thflags; 321302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 322f2512ba1SRui Paulo uint8_t iptos; 3239b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 3249b932e9eSAndre Oppermann struct m_tag *fwd_tag; 3259b932e9eSAndre Oppermann #endif 326c068736aSJeffrey Hsu #ifdef INET6 327302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 328c068736aSJeffrey Hsu int isipv6; 329c068736aSJeffrey Hsu #else 330a160e630SAndre Oppermann const void *ip6 = NULL; 331c068736aSJeffrey Hsu const int isipv6 = 0; 332c068736aSJeffrey Hsu #endif 333302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 334a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 335252ca428SRobert Watson int ti_locked; 336252ca428SRobert Watson #define TI_UNLOCKED 1 337252ca428SRobert Watson #define TI_RLOCKED 2 338252ca428SRobert Watson #define TI_WLOCKED 3 339f76fcf6dSJeffrey Hsu 340610ee2f9SDavid Greenman #ifdef TCPDEBUG 341c068736aSJeffrey Hsu /* 342c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 343c068736aSJeffrey Hsu * now IPv6. 344c068736aSJeffrey Hsu */ 34514739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 346410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 347610ee2f9SDavid Greenman short ostate = 0; 348610ee2f9SDavid Greenman #endif 349c068736aSJeffrey Hsu 350fb59c426SYoshinobu Inoue #ifdef INET6 351fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 352fb59c426SYoshinobu Inoue #endif 353a0292f23SGarrett Wollman 354302ce8d6SAndre Oppermann to.to_flags = 0; 355603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvtotal++; 356fb59c426SYoshinobu Inoue 357fb59c426SYoshinobu Inoue if (isipv6) { 35804d3a452SHajimu UMEMOTO #ifdef INET6 3598d573cc1SAndre Oppermann /* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */ 360fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 361fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 362fb59c426SYoshinobu Inoue if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 363603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbadsum++; 364fb59c426SYoshinobu Inoue goto drop; 365fb59c426SYoshinobu Inoue } 366fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 36733841545SHajimu UMEMOTO 36833841545SHajimu UMEMOTO /* 36933841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 37033841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 37133841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 37233841545SHajimu UMEMOTO * 37333841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 37433841545SHajimu UMEMOTO * already dropped in ip6_input. 37533841545SHajimu UMEMOTO */ 37633841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 37733841545SHajimu UMEMOTO /* XXX stat */ 37833841545SHajimu UMEMOTO goto drop; 37933841545SHajimu UMEMOTO } 38004d3a452SHajimu UMEMOTO #else 3818d573cc1SAndre Oppermann th = NULL; /* XXX: Avoid compiler warning. */ 38204d3a452SHajimu UMEMOTO #endif 383c068736aSJeffrey Hsu } else { 384df8bae1dSRodney W. Grimes /* 385df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 386df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 387df8bae1dSRodney W. Grimes */ 388fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 389df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 390fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 391fb59c426SYoshinobu Inoue } 392df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 3934dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 3944dfdffe9SAndre Oppermann == NULL) { 395603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvshort++; 396df8bae1dSRodney W. Grimes return; 397df8bae1dSRodney W. Grimes } 398df8bae1dSRodney W. Grimes } 399fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 400fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 401db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 402db4f9cc7SJonathan Lemon tlen = ip->ip_len; 403df8bae1dSRodney W. Grimes 404db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 405db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 406db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 407db4f9cc7SJonathan Lemon else 408db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 409c068736aSJeffrey Hsu ip->ip_dst.s_addr, 410c068736aSJeffrey Hsu htonl(m->m_pkthdr.csum_data + 411c068736aSJeffrey Hsu ip->ip_len + 412c068736aSJeffrey Hsu IPPROTO_TCP)); 413db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 4143c653157SHartmut Brandt #ifdef TCPDEBUG 4153c653157SHartmut Brandt ipov->ih_len = (u_short)tlen; 4163c653157SHartmut Brandt ipov->ih_len = htons(ipov->ih_len); 4173c653157SHartmut Brandt #endif 418db4f9cc7SJonathan Lemon } else { 419df8bae1dSRodney W. Grimes /* 420df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 421df8bae1dSRodney W. Grimes */ 422df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 423fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 424fb59c426SYoshinobu Inoue ipov->ih_len = (u_short)tlen; 425fd8e4ebcSMike Barcroft ipov->ih_len = htons(ipov->ih_len); 426fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 427db4f9cc7SJonathan Lemon } 428fb59c426SYoshinobu Inoue if (th->th_sum) { 429603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbadsum++; 430df8bae1dSRodney W. Grimes goto drop; 431df8bae1dSRodney W. Grimes } 432fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 433fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 434fb59c426SYoshinobu Inoue } 435df8bae1dSRodney W. Grimes 436f2512ba1SRui Paulo #ifdef INET6 437f2512ba1SRui Paulo if (isipv6) 438f2512ba1SRui Paulo iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 439f2512ba1SRui Paulo else 440f2512ba1SRui Paulo #endif 441f2512ba1SRui Paulo iptos = ip->ip_tos; 442f2512ba1SRui Paulo 443df8bae1dSRodney W. Grimes /* 444df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 445df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 446df8bae1dSRodney W. Grimes */ 447fb59c426SYoshinobu Inoue off = th->th_off << 2; 448df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 449603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbadoff++; 450df8bae1dSRodney W. Grimes goto drop; 451df8bae1dSRodney W. Grimes } 452fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 453df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 454fb59c426SYoshinobu Inoue if (isipv6) { 45504d3a452SHajimu UMEMOTO #ifdef INET6 456fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 457fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 458fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 45904d3a452SHajimu UMEMOTO #endif 460c068736aSJeffrey Hsu } else { 461df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 462c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 4634dfdffe9SAndre Oppermann == NULL) { 464603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvshort++; 465df8bae1dSRodney W. Grimes return; 466df8bae1dSRodney W. Grimes } 467fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 468fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 469fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 470fb59c426SYoshinobu Inoue } 471df8bae1dSRodney W. Grimes } 472df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 473fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 474df8bae1dSRodney W. Grimes } 475fb59c426SYoshinobu Inoue thflags = th->th_flags; 476df8bae1dSRodney W. Grimes 477e46cd3d4SDag-Erling Smørgrav /* 478df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 479df8bae1dSRodney W. Grimes */ 480fd8e4ebcSMike Barcroft th->th_seq = ntohl(th->th_seq); 481fd8e4ebcSMike Barcroft th->th_ack = ntohl(th->th_ack); 482fd8e4ebcSMike Barcroft th->th_win = ntohs(th->th_win); 483fd8e4ebcSMike Barcroft th->th_urp = ntohs(th->th_urp); 484df8bae1dSRodney W. Grimes 485df8bae1dSRodney W. Grimes /* 486794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 487755c1f07SAndras Olah */ 488fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 489755c1f07SAndras Olah 490755c1f07SAndras Olah /* 491252ca428SRobert Watson * Locate pcb for segment, which requires a lock on tcbinfo. 492d15fb965SRobert Watson * Optimisticaly acquire a global read lock rather than a write lock 493d15fb965SRobert Watson * unless header flags necessarily imply a state change. There are 494d15fb965SRobert Watson * two cases where we might discover later we need a write lock 495d15fb965SRobert Watson * despite the flags: ACKs moving a connection out of the syncache, 496d15fb965SRobert Watson * and ACKs for a connection in TIMEWAIT. 497df8bae1dSRodney W. Grimes */ 498252ca428SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 499252ca428SRobert Watson tcp_read_locking == 0) { 500603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 501252ca428SRobert Watson ti_locked = TI_WLOCKED; 502252ca428SRobert Watson tcp_wlock_atfirst++; 503252ca428SRobert Watson } else { 504252ca428SRobert Watson INP_INFO_RLOCK(&V_tcbinfo); 505252ca428SRobert Watson ti_locked = TI_RLOCKED; 506252ca428SRobert Watson tcp_rlock_atfirst++; 507252ca428SRobert Watson } 508252ca428SRobert Watson 509df8bae1dSRodney W. Grimes findpcb: 510252ca428SRobert Watson #ifdef INVARIANTS 511252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 512252ca428SRobert Watson INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 513252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 514603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 515252ca428SRobert Watson else 516252ca428SRobert Watson panic("%s: findpcb ti_locked %d\n", __func__, ti_locked); 517252ca428SRobert Watson #endif 518252ca428SRobert Watson 5199b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 5208d573cc1SAndre Oppermann /* 5218d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 5228d573cc1SAndre Oppermann */ 5239b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 5249b932e9eSAndre Oppermann 5259b932e9eSAndre Oppermann if (fwd_tag != NULL && isipv6 == 0) { /* IPv6 support is not yet */ 5269b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 5279b932e9eSAndre Oppermann 5289b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 529f9e354dfSJulian Elischer /* 5302b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 531f9e354dfSJulian Elischer * already got one like this? 532f9e354dfSJulian Elischer */ 533603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_tcbinfo, 5349b932e9eSAndre Oppermann ip->ip_src, th->th_sport, 535c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 536c068736aSJeffrey Hsu 0, m->m_pkthdr.rcvif); 537f9e354dfSJulian Elischer if (!inp) { 5389b932e9eSAndre Oppermann /* It's new. Try to find the ambushing socket. */ 539603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_tcbinfo, 540fb59c426SYoshinobu Inoue ip->ip_src, th->th_sport, 5412b25acc1SLuigi Rizzo next_hop->sin_addr, 542c068736aSJeffrey Hsu next_hop->sin_port ? 543c068736aSJeffrey Hsu ntohs(next_hop->sin_port) : 544c068736aSJeffrey Hsu th->th_dport, 545421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 546421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 547f9e354dfSJulian Elischer } 5489b932e9eSAndre Oppermann /* Remove the tag from the packet. We don't need it anymore. */ 5499b932e9eSAndre Oppermann m_tag_delete(m, fwd_tag); 550b10fbdeaSAndre Oppermann } else 5519b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 552b10fbdeaSAndre Oppermann { 55304d3a452SHajimu UMEMOTO if (isipv6) { 55404d3a452SHajimu UMEMOTO #ifdef INET6 555603724d3SBjoern A. Zeeb inp = in6_pcblookup_hash(&V_tcbinfo, 556c068736aSJeffrey Hsu &ip6->ip6_src, th->th_sport, 557c068736aSJeffrey Hsu &ip6->ip6_dst, th->th_dport, 558421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 559421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 56004d3a452SHajimu UMEMOTO #endif 56104d3a452SHajimu UMEMOTO } else 562603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_tcbinfo, 563c068736aSJeffrey Hsu ip->ip_src, th->th_sport, 564c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 565421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 566421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 567fb59c426SYoshinobu Inoue } 568f9e354dfSJulian Elischer 569df8bae1dSRodney W. Grimes /* 570574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 571574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 5728b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 573df8bae1dSRodney W. Grimes */ 574816a3d83SPoul-Henning Kamp if (inp == NULL) { 575574b6964SAndre Oppermann /* 576574b6964SAndre Oppermann * Log communication attempts to ports that are not 577574b6964SAndre Oppermann * in use. 578574b6964SAndre Oppermann */ 579574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 580574b6964SAndre Oppermann tcp_log_in_vain == 2) { 5819fb5d4c0SPeter Wemm if ((s = tcp_log_addrs(NULL, th, (void *)ip, ip6))) 582df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 583df541e5fSAndre Oppermann "to closed port\n", s, __func__); 5842e4e1b4cSGeoff Rehmet } 585574b6964SAndre Oppermann /* 586574b6964SAndre Oppermann * When blackholing do not respond with a RST but 587574b6964SAndre Oppermann * completely ignore the segment and drop it. 588574b6964SAndre Oppermann */ 589603724d3SBjoern A. Zeeb if ((V_blackhole == 1 && (thflags & TH_SYN)) || 590603724d3SBjoern A. Zeeb V_blackhole == 2) 5911929eae1SAndre Oppermann goto dropunlock; 592574b6964SAndre Oppermann 593a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 594a57815efSBosko Milekic goto dropwithreset; 595816a3d83SPoul-Henning Kamp } 5968501a69cSRobert Watson INP_WLOCK(inp); 597936cd18dSAndre Oppermann 598a2589465SKen Smith #ifdef IPSEC 599a2589465SKen Smith #ifdef INET6 600a2589465SKen Smith if (isipv6 && ipsec6_in_reject(m, inp)) { 601603724d3SBjoern A. Zeeb V_ipsec6stat.in_polvio++; 602a2589465SKen Smith goto dropunlock; 603a2589465SKen Smith } else 604a2589465SKen Smith #endif /* INET6 */ 605a2589465SKen Smith if (ipsec4_in_reject(m, inp) != 0) { 606603724d3SBjoern A. Zeeb V_ipsec4stat.in_polvio++; 607a2589465SKen Smith goto dropunlock; 608a2589465SKen Smith } 609a2589465SKen Smith #endif /* IPSEC */ 610a2589465SKen Smith 6118d573cc1SAndre Oppermann /* 6128d573cc1SAndre Oppermann * Check the minimum TTL for socket. 6138d573cc1SAndre Oppermann */ 61434f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 61534f83c52SGeorge V. Neville-Neil #ifdef INET6 61634f83c52SGeorge V. Neville-Neil if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim) 617302ce8d6SAndre Oppermann goto dropunlock; 61802707462SAndre Oppermann else 61934f83c52SGeorge V. Neville-Neil #endif 62034f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 621302ce8d6SAndre Oppermann goto dropunlock; 62234f83c52SGeorge V. Neville-Neil } 623936cd18dSAndre Oppermann 624340c35deSJonathan Lemon /* 625252ca428SRobert Watson * A previous connection in TIMEWAIT state is supposed to catch stray 626252ca428SRobert Watson * or duplicate segments arriving late. If this segment was a 627252ca428SRobert Watson * legitimate new connection attempt the old INPCB gets removed and 628252ca428SRobert Watson * we can try again to find a listening socket. 629252ca428SRobert Watson * 630252ca428SRobert Watson * At this point, due to earlier optimism, we may hold a read lock on 631252ca428SRobert Watson * the inpcbinfo, rather than a write lock. If so, we need to 632252ca428SRobert Watson * upgrade, or if that fails, acquire a reference on the inpcb, drop 633252ca428SRobert Watson * all locks, acquire a global write lock, and then re-acquire the 634252ca428SRobert Watson * inpcb lock. We may at that point discover that another thread has 635252ca428SRobert Watson * tried to free the inpcb, in which case we need to loop back and 636252ca428SRobert Watson * try to find a new inpcb to deliver to. 637340c35deSJonathan Lemon */ 638794235b7SAndre Oppermann if (inp->inp_vflag & INP_TIMEWAIT) { 639252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 640252ca428SRobert Watson ("%s: INP_TIMEWAIT ti_locked %d", __func__, ti_locked)); 641252ca428SRobert Watson 642252ca428SRobert Watson if (ti_locked == TI_RLOCKED) { 643252ca428SRobert Watson if (rw_try_upgrade(&V_tcbinfo.ipi_lock) == 0) { 644252ca428SRobert Watson in_pcbref(inp); 645252ca428SRobert Watson INP_WUNLOCK(inp); 646252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 647252ca428SRobert Watson INP_INFO_WLOCK(&V_tcbinfo); 648252ca428SRobert Watson ti_locked = TI_WLOCKED; 649252ca428SRobert Watson INP_WLOCK(inp); 650252ca428SRobert Watson if (in_pcbrele(inp)) { 651252ca428SRobert Watson tcp_wlock_looped++; 652252ca428SRobert Watson inp = NULL; 653252ca428SRobert Watson goto findpcb; 654252ca428SRobert Watson } 655252ca428SRobert Watson tcp_wlock_relocked++; 656252ca428SRobert Watson } else { 657252ca428SRobert Watson ti_locked = TI_WLOCKED; 658252ca428SRobert Watson tcp_wlock_upgraded++; 659252ca428SRobert Watson } 660252ca428SRobert Watson } 661252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 662252ca428SRobert Watson 663340c35deSJonathan Lemon if (thflags & TH_SYN) 664f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 6658d573cc1SAndre Oppermann /* 6668d573cc1SAndre Oppermann * NB: tcp_twcheck unlocks the INP and frees the mbuf. 6678d573cc1SAndre Oppermann */ 6682104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 669340c35deSJonathan Lemon goto findpcb; 670603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 671340c35deSJonathan Lemon return; 672340c35deSJonathan Lemon } 673794235b7SAndre Oppermann /* 674794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 675794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 676794235b7SAndre Oppermann * segment and send an appropriate response. 677794235b7SAndre Oppermann */ 678df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 679a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 680a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 681a57815efSBosko Milekic goto dropwithreset; 68209f81a46SBosko Milekic } 683df8bae1dSRodney W. Grimes 684252ca428SRobert Watson /* 685252ca428SRobert Watson * We've identified a valid inpcb, but it could be that we need an 686252ca428SRobert Watson * inpcbinfo write lock and have only a read lock. In this case, 687252ca428SRobert Watson * attempt to upgrade/relock using the same strategy as the TIMEWAIT 688252ca428SRobert Watson * case above. 689252ca428SRobert Watson */ 690252ca428SRobert Watson if (tp->t_state != TCPS_ESTABLISHED || 691252ca428SRobert Watson (thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 692252ca428SRobert Watson tcp_read_locking == 0) { 693252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 694252ca428SRobert Watson ("%s: upgrade check ti_locked %d", __func__, ti_locked)); 695252ca428SRobert Watson 696252ca428SRobert Watson if (ti_locked == TI_RLOCKED) { 697252ca428SRobert Watson if (rw_try_upgrade(&V_tcbinfo.ipi_lock) == 0) { 698252ca428SRobert Watson in_pcbref(inp); 699252ca428SRobert Watson INP_WUNLOCK(inp); 700252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 701252ca428SRobert Watson INP_INFO_WLOCK(&V_tcbinfo); 702252ca428SRobert Watson ti_locked = TI_WLOCKED; 703252ca428SRobert Watson INP_WLOCK(inp); 704252ca428SRobert Watson if (in_pcbrele(inp)) { 705252ca428SRobert Watson tcp_wlock_looped++; 706252ca428SRobert Watson inp = NULL; 707252ca428SRobert Watson goto findpcb; 708252ca428SRobert Watson } 709252ca428SRobert Watson tcp_wlock_relocked++; 710252ca428SRobert Watson } else { 711252ca428SRobert Watson ti_locked = TI_WLOCKED; 712252ca428SRobert Watson tcp_wlock_upgraded++; 713252ca428SRobert Watson } 714252ca428SRobert Watson } 715252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 716252ca428SRobert Watson } 717252ca428SRobert Watson 718c488362eSRobert Watson #ifdef MAC 7198501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 72030d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 721302ce8d6SAndre Oppermann goto dropunlock; 722c488362eSRobert Watson #endif 723a557af22SRobert Watson so = inp->inp_socket; 724302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 725610ee2f9SDavid Greenman #ifdef TCPDEBUG 726df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 727df8bae1dSRodney W. Grimes ostate = tp->t_state; 728d30d90dcSMaxim Konovalov if (isipv6) { 72910fe523eSMaxim Konovalov #ifdef INET6 730540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 731d30d90dcSMaxim Konovalov #endif 732d30d90dcSMaxim Konovalov } else 733540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 734fb59c426SYoshinobu Inoue tcp_savetcp = *th; 735df8bae1dSRodney W. Grimes } 736610ee2f9SDavid Greenman #endif 737db33b3e6SAndre Oppermann /* 738db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 739db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 740db33b3e6SAndre Oppermann * attempt or the completion of a previous one. 741db33b3e6SAndre Oppermann */ 742540e8b7eSJeffrey Hsu if (so->so_options & SO_ACCEPTCONN) { 743540e8b7eSJeffrey Hsu struct in_conninfo inc; 744540e8b7eSJeffrey Hsu 7457824d002SAndre Oppermann KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but " 7467824d002SAndre Oppermann "tp not listening", __func__)); 7477824d002SAndre Oppermann 7488bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 749302ce8d6SAndre Oppermann #ifdef INET6 750be2ac88cSJonathan Lemon if (isipv6) { 751dcdb4371SBjoern A. Zeeb inc.inc_flags |= INC_ISIPV6; 752be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 753be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 754302ce8d6SAndre Oppermann } else 755302ce8d6SAndre Oppermann #endif 756302ce8d6SAndre Oppermann { 757be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 758be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 759be2ac88cSJonathan Lemon } 760be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 761be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 762be2ac88cSJonathan Lemon 763fb59c426SYoshinobu Inoue /* 7648d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 7658d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 7668d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 767fb59c426SYoshinobu Inoue */ 768be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 769bf6d304aSAndre Oppermann /* 770bf6d304aSAndre Oppermann * Parse the TCP options here because 771bf6d304aSAndre Oppermann * syncookies need access to the reflected 772bf6d304aSAndre Oppermann * timestamp. 773bf6d304aSAndre Oppermann */ 774bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 7759fa198beSAndre Oppermann /* 7769fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 7779fa198beSAndre Oppermann * inp and tcpinfo locks. 7789fa198beSAndre Oppermann */ 779bf6d304aSAndre Oppermann if (!syncache_expand(&inc, &to, th, &so, m)) { 7804195b4afSPaul Traina /* 781e207f800SAndre Oppermann * No syncache entry or ACK was not 782be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 7838d573cc1SAndre Oppermann * NB: syncache did its own logging 7848d573cc1SAndre Oppermann * of the failure cause. 7854195b4afSPaul Traina */ 786be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 787be2ac88cSJonathan Lemon goto dropwithreset; 788be2ac88cSJonathan Lemon } 789f76fcf6dSJeffrey Hsu if (so == NULL) { 790be2ac88cSJonathan Lemon /* 791e207f800SAndre Oppermann * We completed the 3-way handshake 792e207f800SAndre Oppermann * but could not allocate a socket 793e207f800SAndre Oppermann * either due to memory shortage, 794e207f800SAndre Oppermann * listen queue length limits or 795a160e630SAndre Oppermann * global socket limits. Send RST 796a160e630SAndre Oppermann * or wait and have the remote end 797a160e630SAndre Oppermann * retransmit the ACK for another 798a160e630SAndre Oppermann * try. 799be2ac88cSJonathan Lemon */ 800a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 801a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 8028d573cc1SAndre Oppermann "Socket allocation failed due to " 8038d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 804ac957cd2SJulian Elischer s, __func__, 805ac957cd2SJulian Elischer V_tcp_sc_rst_sock_fail ? 806ac957cd2SJulian Elischer "sending RST" : "try again"); 807603724d3SBjoern A. Zeeb if (V_tcp_sc_rst_sock_fail) { 808e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 809e207f800SAndre Oppermann goto dropwithreset; 810a160e630SAndre Oppermann } else 811a160e630SAndre Oppermann goto dropunlock; 812f76fcf6dSJeffrey Hsu } 813be2ac88cSJonathan Lemon /* 814be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 8158d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 8168d573cc1SAndre Oppermann * created socket and update the tp variable. 817be2ac88cSJonathan Lemon */ 8188501a69cSRobert Watson INP_WUNLOCK(inp); /* listen socket */ 819be2ac88cSJonathan Lemon inp = sotoinpcb(so); 8208501a69cSRobert Watson INP_WLOCK(inp); /* new connection */ 821be2ac88cSJonathan Lemon tp = intotcpcb(inp); 8228d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 8238d573cc1SAndre Oppermann ("%s: ", __func__)); 824be2ac88cSJonathan Lemon /* 825302ce8d6SAndre Oppermann * Process the segment and the data it 826302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 827302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 828302ce8d6SAndre Oppermann */ 829f2512ba1SRui Paulo tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 830252ca428SRobert Watson iptos, ti_locked); 831603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 832302ce8d6SAndre Oppermann return; 833be2ac88cSJonathan Lemon } 834a160e630SAndre Oppermann /* 8358d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 8368d573cc1SAndre Oppermann * 837a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 838a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 839a160e630SAndre Oppermann * retransmits. 84019bc77c5SAndre Oppermann * 84119bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 84219bc77c5SAndre Oppermann * causes. 843a160e630SAndre Oppermann */ 844a160e630SAndre Oppermann if (thflags & TH_RST) { 84519bc77c5SAndre Oppermann syncache_chkrst(&inc, th); 846a160e630SAndre Oppermann goto dropunlock; 847a160e630SAndre Oppermann } 848a160e630SAndre Oppermann /* 849a160e630SAndre Oppermann * We can't do anything without SYN. 850a160e630SAndre Oppermann */ 851a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 852a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 853a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 854773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 8558d573cc1SAndre Oppermann s, __func__); 856603724d3SBjoern A. Zeeb V_tcpstat.tcps_badsyn++; 857a160e630SAndre Oppermann goto dropunlock; 858a160e630SAndre Oppermann } 859a160e630SAndre Oppermann /* 860a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 861a160e630SAndre Oppermann */ 862fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 863a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 864a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 8658d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 8668d573cc1SAndre Oppermann s, __func__); 867a160e630SAndre Oppermann syncache_badack(&inc); /* XXX: Not needed! */ 868603724d3SBjoern A. Zeeb V_tcpstat.tcps_badsyn++; 869a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 870a57815efSBosko Milekic goto dropwithreset; 8714195b4afSPaul Traina } 872a160e630SAndre Oppermann /* 873a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 874a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 875a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 876a160e630SAndre Oppermann * TCP/IP stack. 877a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 878a160e630SAndre Oppermann * and is constantly refining its stack detection 879a160e630SAndre Oppermann * strategies. 8808d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 881a160e630SAndre Oppermann * and was used by RFC1644. 882a160e630SAndre Oppermann */ 883603724d3SBjoern A. Zeeb if ((thflags & TH_FIN) && V_drop_synfin) { 884a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 885a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 886773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 8878d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 888603724d3SBjoern A. Zeeb V_tcpstat.tcps_badsyn++; 889302ce8d6SAndre Oppermann goto dropunlock; 890ebb0cbeaSPaul Traina } 891be2ac88cSJonathan Lemon /* 892be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 893a160e630SAndre Oppermann * 894a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 895a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 896a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 897be2ac88cSJonathan Lemon */ 898a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 899a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 900a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 901a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 90233841545SHajimu UMEMOTO #ifdef INET6 90333841545SHajimu UMEMOTO /* 90433841545SHajimu UMEMOTO * If deprecated address is forbidden, 90533841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 90633841545SHajimu UMEMOTO * address to prevent any new inbound connection from 90733841545SHajimu UMEMOTO * getting established. 90833841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 90933841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 91033841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 91133841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 91233841545SHajimu UMEMOTO * for the exchange. 91333841545SHajimu UMEMOTO * 91433841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 91533841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 91633841545SHajimu UMEMOTO * SYN in this case. 91733841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 91833841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 91933841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 92033841545SHajimu UMEMOTO * used" 92133841545SHajimu UMEMOTO * 2. use of it with new communication: 92233841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 92333841545SHajimu UMEMOTO * with sufficient scope is available" 92433841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 92533841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 92633841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 92733841545SHajimu UMEMOTO * 92833841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 92933841545SHajimu UMEMOTO * multiple description text for deprecated address 93033841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 93133841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 93233841545SHajimu UMEMOTO */ 933603724d3SBjoern A. Zeeb if (isipv6 && !V_ip6_use_deprecated) { 93433841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 93533841545SHajimu UMEMOTO 93633841545SHajimu UMEMOTO if ((ia6 = ip6_getdstifaddr(m)) && 93733841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 938a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 939a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 9408d573cc1SAndre Oppermann "Connection attempt to deprecated " 9418d573cc1SAndre Oppermann "IPv6 address rejected\n", 942a160e630SAndre Oppermann s, __func__); 94333841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 94433841545SHajimu UMEMOTO goto dropwithreset; 94533841545SHajimu UMEMOTO } 94633841545SHajimu UMEMOTO } 94733841545SHajimu UMEMOTO #endif 94865f28919SJesper Skriver /* 949db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 9508d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 951db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 9528d573cc1SAndre Oppermann * If it is from this socket it must be forged. 9538d573cc1SAndre Oppermann * Don't respond if the source or destination is a 9548d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 95593ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 95693ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 95793ec91baSCrist J. Clark * in_broadcast() to find them. 958be2ac88cSJonathan Lemon */ 9598d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 9608d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 9618d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 9628d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 963773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 964302ce8d6SAndre Oppermann goto dropunlock; 9658d573cc1SAndre Oppermann } 966be2ac88cSJonathan Lemon if (isipv6) { 967db33b3e6SAndre Oppermann #ifdef INET6 968db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 969a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 970a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 971a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 9728d573cc1SAndre Oppermann "Connection attempt to/from self " 973773673c1SAndre Oppermann "ignored\n", s, __func__); 974302ce8d6SAndre Oppermann goto dropunlock; 975a160e630SAndre Oppermann } 976be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 977a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 978a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 979a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 9808d573cc1SAndre Oppermann "Connection attempt from/to multicast " 981773673c1SAndre Oppermann "address ignored\n", s, __func__); 982302ce8d6SAndre Oppermann goto dropunlock; 983a160e630SAndre Oppermann } 984db33b3e6SAndre Oppermann #endif 985c068736aSJeffrey Hsu } else { 986db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 987a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 988a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 989a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 9908d573cc1SAndre Oppermann "Connection attempt from/to self " 991773673c1SAndre Oppermann "ignored\n", s, __func__); 992302ce8d6SAndre Oppermann goto dropunlock; 993a160e630SAndre Oppermann } 994be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 995be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 9962ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 997a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 998a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 999a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 10008d573cc1SAndre Oppermann "Connection attempt from/to broad- " 1001773673c1SAndre Oppermann "or multicast address ignored\n", 1002a160e630SAndre Oppermann s, __func__); 1003302ce8d6SAndre Oppermann goto dropunlock; 1004c068736aSJeffrey Hsu } 1005a160e630SAndre Oppermann } 1006be2ac88cSJonathan Lemon /* 1007db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 1008db33b3e6SAndre Oppermann * for syncache. 1009be2ac88cSJonathan Lemon */ 10103c653157SHartmut Brandt #ifdef TCPDEBUG 10113c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 10123c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 10133c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 10143c653157SHartmut Brandt #endif 1015f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 10164d6e7130SAndre Oppermann syncache_add(&inc, &to, th, inp, &so, m); 1017be2ac88cSJonathan Lemon /* 10184d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 1019a160e630SAndre Oppermann * Everything already unlocked by syncache_add(). 1020be2ac88cSJonathan Lemon */ 1021603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1022be2ac88cSJonathan Lemon return; 1023f76fcf6dSJeffrey Hsu } 1024db33b3e6SAndre Oppermann 1025302ce8d6SAndre Oppermann /* 10268d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 10278d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 10288d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 1029302ce8d6SAndre Oppermann */ 1030252ca428SRobert Watson tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked); 1031603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1032302ce8d6SAndre Oppermann return; 1033be2ac88cSJonathan Lemon 1034302ce8d6SAndre Oppermann dropwithreset: 1035252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1036252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 1037252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1038dd8ac7f9SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1039252ca428SRobert Watson else 1040252ca428SRobert Watson panic("%s: dropwithreset ti_locked %d", __func__, ti_locked); 1041252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1042014ea782SRobert Watson 1043014ea782SRobert Watson if (inp != NULL) { 1044302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 1045014ea782SRobert Watson INP_WUNLOCK(inp); 1046dd8ac7f9SRobert Watson } else 1047014ea782SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1048302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 1049014ea782SRobert Watson goto drop; 1050014ea782SRobert Watson 1051302ce8d6SAndre Oppermann dropunlock: 1052252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1053252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 1054252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1055252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1056252ca428SRobert Watson else 1057252ca428SRobert Watson panic("%s: dropunlock ti_locked %d", __func__, ti_locked); 1058252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1059252ca428SRobert Watson 10609fa198beSAndre Oppermann if (inp != NULL) 10618501a69cSRobert Watson INP_WUNLOCK(inp); 1062014ea782SRobert Watson 1063302ce8d6SAndre Oppermann drop: 1064603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1065a160e630SAndre Oppermann if (s != NULL) 1066a160e630SAndre Oppermann free(s, M_TCPLOG); 1067302ce8d6SAndre Oppermann if (m != NULL) 1068302ce8d6SAndre Oppermann m_freem(m); 1069302ce8d6SAndre Oppermann } 1070302ce8d6SAndre Oppermann 1071679d9708SAndre Oppermann static void 1072302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 1073252ca428SRobert Watson struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos, 1074252ca428SRobert Watson int ti_locked) 1075302ce8d6SAndre Oppermann { 10768b615593SMarko Zec INIT_VNET_INET(tp->t_vnet); 1077302ce8d6SAndre Oppermann int thflags, acked, ourfinisacked, needoutput = 0; 1078302ce8d6SAndre Oppermann int rstreason, todrop, win; 1079302ce8d6SAndre Oppermann u_long tiwin; 1080302ce8d6SAndre Oppermann struct tcpopt to; 1081302ce8d6SAndre Oppermann 108214739780SMaxim Konovalov #ifdef TCPDEBUG 108314739780SMaxim Konovalov /* 108414739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 108514739780SMaxim Konovalov * now IPv6. 108614739780SMaxim Konovalov */ 108714739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 108814739780SMaxim Konovalov struct tcphdr tcp_savetcp; 108914739780SMaxim Konovalov short ostate = 0; 109014739780SMaxim Konovalov #endif 1091302ce8d6SAndre Oppermann thflags = th->th_flags; 1092302ce8d6SAndre Oppermann 1093252ca428SRobert Watson /* 1094252ca428SRobert Watson * If this is either a state-changing packet or current state isn't 1095252ca428SRobert Watson * established, we require a write lock on tcbinfo. Otherwise, we 1096252ca428SRobert Watson * allow either a read lock or a write lock, as we may have acquired 1097252ca428SRobert Watson * a write lock due to a race. 1098252ca428SRobert Watson * 1099d15fb965SRobert Watson * Require a global write lock for SYN/FIN/RST segments or 1100252ca428SRobert Watson * non-established connections; otherwise accept either a read or 1101252ca428SRobert Watson * write lock, as we may have conservatively acquired a write lock in 1102252ca428SRobert Watson * certain cases in tcp_input() (is this still true?). Currently we 1103252ca428SRobert Watson * will never enter with no lock, so we try to drop it quickly in the 1104252ca428SRobert Watson * common pure ack/pure data cases. 1105252ca428SRobert Watson */ 1106252ca428SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 1107252ca428SRobert Watson tp->t_state != TCPS_ESTABLISHED) { 1108252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s ti_locked %d for " 1109252ca428SRobert Watson "SYN/FIN/RST/!EST", __func__, ti_locked)); 1110603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1111252ca428SRobert Watson } else { 1112252ca428SRobert Watson #ifdef INVARIANTS 1113252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1114252ca428SRobert Watson INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1115252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1116252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1117252ca428SRobert Watson else 1118252ca428SRobert Watson panic("%s: ti_locked %d for EST", __func__, 1119252ca428SRobert Watson ti_locked); 1120252ca428SRobert Watson #endif 1121252ca428SRobert Watson } 11228501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1123679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1124679d9708SAndre Oppermann __func__)); 1125679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1126679d9708SAndre Oppermann __func__)); 1127df8bae1dSRodney W. Grimes 1128df8bae1dSRodney W. Grimes /* 1129df8bae1dSRodney W. Grimes * Segment received on connection. 1130df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1131e8949f74SAndre Oppermann * XXX: This should be done after segment 1132e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 1133df8bae1dSRodney W. Grimes */ 11349b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 11357ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 1136b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 1137df8bae1dSRodney W. Grimes 1138df8bae1dSRodney W. Grimes /* 1139464fcfbcSAndre Oppermann * Unscale the window into a 32-bit value. 1140e8949f74SAndre Oppermann * For the SYN_SENT state the scale is zero. 1141464fcfbcSAndre Oppermann */ 1142464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 1143464fcfbcSAndre Oppermann 1144464fcfbcSAndre Oppermann /* 1145f2512ba1SRui Paulo * TCP ECN processing. 1146f2512ba1SRui Paulo */ 1147f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) { 1148f2512ba1SRui Paulo switch (iptos & IPTOS_ECN_MASK) { 1149f2512ba1SRui Paulo case IPTOS_ECN_CE: 1150f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_ECE; 1151603724d3SBjoern A. Zeeb V_tcpstat.tcps_ecn_ce++; 1152f2512ba1SRui Paulo break; 1153f2512ba1SRui Paulo case IPTOS_ECN_ECT0: 1154603724d3SBjoern A. Zeeb V_tcpstat.tcps_ecn_ect0++; 1155f2512ba1SRui Paulo break; 1156f2512ba1SRui Paulo case IPTOS_ECN_ECT1: 1157603724d3SBjoern A. Zeeb V_tcpstat.tcps_ecn_ect1++; 1158f2512ba1SRui Paulo break; 1159f2512ba1SRui Paulo } 1160f2512ba1SRui Paulo 1161f2512ba1SRui Paulo if (thflags & TH_CWR) 1162f2512ba1SRui Paulo tp->t_flags &= ~TF_ECN_SND_ECE; 1163f2512ba1SRui Paulo 1164f2512ba1SRui Paulo /* 1165f2512ba1SRui Paulo * Congestion experienced. 1166f2512ba1SRui Paulo * Ignore if we are already trying to recover. 1167f2512ba1SRui Paulo */ 1168f2512ba1SRui Paulo if ((thflags & TH_ECE) && 1169f2512ba1SRui Paulo SEQ_LEQ(th->th_ack, tp->snd_recover)) { 1170603724d3SBjoern A. Zeeb V_tcpstat.tcps_ecn_rcwnd++; 1171f2512ba1SRui Paulo tcp_congestion_exp(tp); 1172f2512ba1SRui Paulo } 1173f2512ba1SRui Paulo } 1174f2512ba1SRui Paulo 1175f2512ba1SRui Paulo /* 1176f72167f4SAndre Oppermann * Parse options on any incoming segment. 1177f72167f4SAndre Oppermann */ 1178302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1179302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1180302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1181f72167f4SAndre Oppermann 1182f72167f4SAndre Oppermann /* 1183f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1184bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1185bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1186bf6d304aSAndre Oppermann * was established. 1187f72167f4SAndre Oppermann */ 1188bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1189e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 1190bf6d304aSAndre Oppermann if (TSTMP_GT(to.to_tsecr, ticks)) 1191f72167f4SAndre Oppermann to.to_tsecr = 0; 1192bf6d304aSAndre Oppermann } 1193f72167f4SAndre Oppermann 1194f72167f4SAndre Oppermann /* 119597d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 119697d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 11975396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 11985396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 119997d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1200df8bae1dSRodney W. Grimes */ 12010a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1202464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 1203464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 1204be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 120502a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 1206be2ac88cSJonathan Lemon } 12075396d0f8SAndre Oppermann /* 12085396d0f8SAndre Oppermann * Initial send window. It will be updated with 12095396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 12105396d0f8SAndre Oppermann */ 12115396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 1212be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1213be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1214be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1215be2ac88cSJonathan Lemon tp->ts_recent_age = ticks; 1216be2ac88cSJonathan Lemon } 1217be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1218be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 12193529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 12203529149eSAndre Oppermann (to.to_flags & TOF_SACKPERM) == 0) 12213529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 12226d90faf3SPaul Saab } 12236d90faf3SPaul Saab 1224df8bae1dSRodney W. Grimes /* 1225df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1226df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1227df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1228df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1229df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1230df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1231df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1232df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1233df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1234df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1235df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1236df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1237a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1238c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 1239a0292f23SGarrett Wollman * be TH_NEEDSYN. 1240df8bae1dSRodney W. Grimes */ 1241df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1242c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 1243fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1244c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 1245c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 1246a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1247c5ad39b9SAndre Oppermann LIST_EMPTY(&tp->t_segq) && 1248be2ac88cSJonathan Lemon ((to.to_flags & TOF_TS) == 0 || 1249c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1250df8bae1dSRodney W. Grimes 1251df8bae1dSRodney W. Grimes /* 1252df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1253df8bae1dSRodney W. Grimes * record the timestamp. 1254a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1255a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1256df8bae1dSRodney W. Grimes */ 1257be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1258fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 12599b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1260a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1261df8bae1dSRodney W. Grimes } 1262df8bae1dSRodney W. Grimes 1263fb59c426SYoshinobu Inoue if (tlen == 0) { 1264fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1265fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1266233e8c18SGarrett Wollman tp->snd_cwnd >= tp->snd_wnd && 1267603724d3SBjoern A. Zeeb ((!V_tcp_do_newreno && 12683529149eSAndre Oppermann !(tp->t_flags & TF_SACK_PERMIT) && 1269cb942153SJeffrey Hsu tp->t_dupacks < tcprexmtthresh) || 1270603724d3SBjoern A. Zeeb ((V_tcp_do_newreno || 12713529149eSAndre Oppermann (tp->t_flags & TF_SACK_PERMIT)) && 1272fc30a251SAndre Oppermann !IN_FASTRECOVERY(tp) && 1273fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1274482ac968SPaul Saab TAILQ_EMPTY(&tp->snd_holes)))) { 1275df8bae1dSRodney W. Grimes /* 1276e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1277df8bae1dSRodney W. Grimes */ 1278252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1279252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 1280252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1281252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1282252ca428SRobert Watson else 1283252ca428SRobert Watson panic("%s: ti_locked %d on pure ACK", 1284252ca428SRobert Watson __func__, ti_locked); 1285252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1286252ca428SRobert Watson 1287603724d3SBjoern A. Zeeb ++V_tcpstat.tcps_predack; 1288252ca428SRobert Watson 12899b8b58e0SJonathan Lemon /* 1290e8949f74SAndre Oppermann * "bad retransmit" recovery. 12919b8b58e0SJonathan Lemon */ 12929b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 12939b8b58e0SJonathan Lemon ticks < tp->t_badrxtwin) { 1294603724d3SBjoern A. Zeeb ++V_tcpstat.tcps_sndrexmitbad; 12959b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 12969b8b58e0SJonathan Lemon tp->snd_ssthresh = 12979b8b58e0SJonathan Lemon tp->snd_ssthresh_prev; 12989d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 12999d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 13009d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 13019b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 13029b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; 13039b8b58e0SJonathan Lemon } 1304fa55172bSMatthew Dillon 1305fa55172bSMatthew Dillon /* 1306fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1307fa55172bSMatthew Dillon * 1308fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1309fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1310fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1311fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1312fa55172bSMatthew Dillon */ 1313fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1314fa55172bSMatthew Dillon to.to_tsecr) { 1315eaf80179SAndre Oppermann if (!tp->t_rttlow || 1316eaf80179SAndre Oppermann tp->t_rttlow > ticks - to.to_tsecr) 1317eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 1318a0292f23SGarrett Wollman tcp_xmit_timer(tp, 13199b8b58e0SJonathan Lemon ticks - to.to_tsecr + 1); 1320fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1321fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1322eaf80179SAndre Oppermann if (!tp->t_rttlow || 1323eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1324eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1325c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1326c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1327fa55172bSMatthew Dillon } 13281fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 1329fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 1330603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvackpack++; 1331603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvackbyte += acked; 1332df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 13339d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 13349d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 13359d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 13369d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 13371645d090SJeff Roberson /* 1338e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 13391645d090SJeff Roberson * to th_ack. 13401645d090SJeff Roberson */ 1341cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1342b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1343df8bae1dSRodney W. Grimes m_freem(m); 1344e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made. */ 1345df8bae1dSRodney W. Grimes 1346df8bae1dSRodney W. Grimes /* 1347df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1348df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1349df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1350df8bae1dSRodney W. Grimes * If process is waiting for space, 1351df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1352df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1353df8bae1dSRodney W. Grimes * decide between more output or persist. 1354e8949f74SAndre Oppermann */ 13553c653157SHartmut Brandt #ifdef TCPDEBUG 13563c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 13573c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 13583c653157SHartmut Brandt (void *)tcp_saveipgen, 13593c653157SHartmut Brandt &tcp_savetcp, 0); 13603c653157SHartmut Brandt #endif 1361df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1362b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1363b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1364b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1365b8152ba7SAndre Oppermann tp->t_rxtcur); 1366df8bae1dSRodney W. Grimes sowwakeup(so); 1367df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 1368df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1369a14c749fSJonathan Lemon goto check_delack; 1370df8bae1dSRodney W. Grimes } 1371fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1372fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 13736741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 13746741ecf5SAndre Oppermann 1375df8bae1dSRodney W. Grimes /* 1376252ca428SRobert Watson * This is a pure, in-sequence data packet with 1377252ca428SRobert Watson * nothing on the reassembly queue and we have enough 1378252ca428SRobert Watson * buffer space to take it. 1379df8bae1dSRodney W. Grimes */ 1380252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1381252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 1382252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1383252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1384252ca428SRobert Watson else 1385252ca428SRobert Watson panic("%s: ti_locked %d on pure data " 1386252ca428SRobert Watson "segment", __func__, ti_locked); 1387252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1388252ca428SRobert Watson 13896d90faf3SPaul Saab /* Clean receiver SACK report if present */ 13903529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 13916d90faf3SPaul Saab tcp_clean_sackreport(tp); 1392603724d3SBjoern A. Zeeb ++V_tcpstat.tcps_preddat; 1393fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 13941645d090SJeff Roberson /* 13951645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 13961645d090SJeff Roberson * th_seq. 13971645d090SJeff Roberson */ 13981645d090SJeff Roberson tp->snd_wl1 = th->th_seq; 13991645d090SJeff Roberson /* 14001645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 14011645d090SJeff Roberson * rcv_nxt. 14021645d090SJeff Roberson */ 14031645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 1404603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvpack++; 1405603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbyte += tlen; 1406e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made */ 14073c653157SHartmut Brandt #ifdef TCPDEBUG 14083c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 14093c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 14103c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 14113c653157SHartmut Brandt #endif 14126741ecf5SAndre Oppermann /* 14136741ecf5SAndre Oppermann * Automatic sizing of receive socket buffer. Often the send 14146741ecf5SAndre Oppermann * buffer size is not optimally adjusted to the actual network 14156741ecf5SAndre Oppermann * conditions at hand (delay bandwidth product). Setting the 14166741ecf5SAndre Oppermann * buffer size too small limits throughput on links with high 14176741ecf5SAndre Oppermann * bandwidth and high delay (eg. trans-continental/oceanic links). 14186741ecf5SAndre Oppermann * 14196741ecf5SAndre Oppermann * On the receive side the socket buffer memory is only rarely 14206741ecf5SAndre Oppermann * used to any significant extent. This allows us to be much 14216741ecf5SAndre Oppermann * more aggressive in scaling the receive socket buffer. For 14226741ecf5SAndre Oppermann * the case that the buffer space is actually used to a large 14236741ecf5SAndre Oppermann * extent and we run out of kernel memory we can simply drop 14246741ecf5SAndre Oppermann * the new segments; TCP on the sender will just retransmit it 14256741ecf5SAndre Oppermann * later. Setting the buffer size too big may only consume too 14266741ecf5SAndre Oppermann * much kernel memory if the application doesn't read() from 14276741ecf5SAndre Oppermann * the socket or packet loss or reordering makes use of the 14286741ecf5SAndre Oppermann * reassembly queue. 14296741ecf5SAndre Oppermann * 14306741ecf5SAndre Oppermann * The criteria to step up the receive buffer one notch are: 14316741ecf5SAndre Oppermann * 1. the number of bytes received during the time it takes 14326741ecf5SAndre Oppermann * one timestamp to be reflected back to us (the RTT); 14336741ecf5SAndre Oppermann * 2. received bytes per RTT is within seven eighth of the 14346741ecf5SAndre Oppermann * current socket buffer size; 14356741ecf5SAndre Oppermann * 3. receive buffer size has not hit maximal automatic size; 14366741ecf5SAndre Oppermann * 14376741ecf5SAndre Oppermann * This algorithm does one step per RTT at most and only if 14386741ecf5SAndre Oppermann * we receive a bulk stream w/o packet losses or reorderings. 14396741ecf5SAndre Oppermann * Shrinking the buffer during idle times is not necessary as 14406741ecf5SAndre Oppermann * it doesn't consume any memory when idle. 14416741ecf5SAndre Oppermann * 14426741ecf5SAndre Oppermann * TODO: Only step up if the application is actually serving 14436741ecf5SAndre Oppermann * the buffer to better manage the socket buffer resources. 1444df8bae1dSRodney W. Grimes */ 1445603724d3SBjoern A. Zeeb if (V_tcp_do_autorcvbuf && 14466741ecf5SAndre Oppermann to.to_tsecr && 14476741ecf5SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 14486741ecf5SAndre Oppermann if (to.to_tsecr > tp->rfbuf_ts && 14496741ecf5SAndre Oppermann to.to_tsecr - tp->rfbuf_ts < hz) { 14506741ecf5SAndre Oppermann if (tp->rfbuf_cnt > 14516741ecf5SAndre Oppermann (so->so_rcv.sb_hiwat / 8 * 7) && 14526741ecf5SAndre Oppermann so->so_rcv.sb_hiwat < 1453603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max) { 14546741ecf5SAndre Oppermann newsize = 14556741ecf5SAndre Oppermann min(so->so_rcv.sb_hiwat + 1456603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_inc, 1457603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max); 14586741ecf5SAndre Oppermann } 14596741ecf5SAndre Oppermann /* Start over with next RTT. */ 14606741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 14616741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 14626741ecf5SAndre Oppermann } else 14636741ecf5SAndre Oppermann tp->rfbuf_cnt += tlen; /* add up */ 14646741ecf5SAndre Oppermann } 14656741ecf5SAndre Oppermann 14666741ecf5SAndre Oppermann /* Add data to socket buffer. */ 14671e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1468c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1469c1c36a2cSMike Silbersack m_freem(m); 1470c1c36a2cSMike Silbersack } else { 14716741ecf5SAndre Oppermann /* 14726741ecf5SAndre Oppermann * Set new socket buffer size. 14736741ecf5SAndre Oppermann * Give up when limit is reached. 14746741ecf5SAndre Oppermann */ 14756741ecf5SAndre Oppermann if (newsize) 14766741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 14776c8286e4SRobert Watson newsize, so, NULL)) 14786741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1479fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 14801e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 1481c1c36a2cSMike Silbersack } 1482e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 14831e4d7da7SRobert Watson sorwakeup_locked(so); 1484d8c85a26SJonathan Lemon if (DELAY_ACK(tp)) { 14853bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1486f498eeeeSDavid Greenman } else { 1487e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 1488e612a582SDavid Greenman tcp_output(tp); 1489e612a582SDavid Greenman } 1490a14c749fSJonathan Lemon goto check_delack; 1491df8bae1dSRodney W. Grimes } 1492df8bae1dSRodney W. Grimes } 1493df8bae1dSRodney W. Grimes 1494df8bae1dSRodney W. Grimes /* 1495df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1496df8bae1dSRodney W. Grimes * and then do TCP input processing. 1497df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1498df8bae1dSRodney W. Grimes * but not less than advertised window. 1499df8bae1dSRodney W. Grimes */ 1500df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1501df8bae1dSRodney W. Grimes if (win < 0) 1502df8bae1dSRodney W. Grimes win = 0; 150366e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1504df8bae1dSRodney W. Grimes 15056741ecf5SAndre Oppermann /* Reset receive buffer auto scaling when not in bulk receive mode. */ 15066741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 15076741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 15086741ecf5SAndre Oppermann 1509df8bae1dSRodney W. Grimes switch (tp->t_state) { 1510df8bae1dSRodney W. Grimes 1511df8bae1dSRodney W. Grimes /* 1512764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1513764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1514764d8cefSBill Fenner */ 1515764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1516fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1517fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1518a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1519a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1520a57815efSBosko Milekic goto dropwithreset; 1521a57815efSBosko Milekic } 1522764d8cefSBill Fenner break; 1523764d8cefSBill Fenner 1524764d8cefSBill Fenner /* 1525df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1526df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1527df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1528df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1529df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1530df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1531df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1532f2512ba1SRui Paulo * if seg contains an ECE and ECN support is enabled, the stream 1533f2512ba1SRui Paulo * is ECN capable. 1534df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1535df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1536df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1537df8bae1dSRodney W. Grimes */ 1538df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1539fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1540fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1541fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1542a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1543a0292f23SGarrett Wollman goto dropwithreset; 1544a0292f23SGarrett Wollman } 15450ca3f933SAndre Oppermann if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) 1546df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 15470ca3f933SAndre Oppermann if (thflags & TH_RST) 1548df8bae1dSRodney W. Grimes goto drop; 15490ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 1550df8bae1dSRodney W. Grimes goto drop; 1551464fcfbcSAndre Oppermann 1552fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1553df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1554fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1555603724d3SBjoern A. Zeeb V_tcpstat.tcps_connects++; 1556845799c1SAndras Olah soisconnected(so); 1557c488362eSRobert Watson #ifdef MAC 1558310e7cebSRobert Watson SOCK_LOCK(so); 155930d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 1560310e7cebSRobert Watson SOCK_UNLOCK(so); 1561c488362eSRobert Watson #endif 1562845799c1SAndras Olah /* Do window scaling on this connection? */ 1563845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1564845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1565845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1566845799c1SAndras Olah } 1567a0292f23SGarrett Wollman tp->rcv_adv += tp->rcv_wnd; 1568a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1569a0292f23SGarrett Wollman /* 1570a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1571a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1572a0292f23SGarrett Wollman */ 1573d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && tlen != 0) 1574b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 1575b8152ba7SAndre Oppermann tcp_delacktime); 1576a0292f23SGarrett Wollman else 1577a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1578f2512ba1SRui Paulo 1579603724d3SBjoern A. Zeeb if ((thflags & TH_ECE) && V_tcp_do_ecn) { 1580f2512ba1SRui Paulo tp->t_flags |= TF_ECN_PERMIT; 1581603724d3SBjoern A. Zeeb V_tcpstat.tcps_ecn_shs++; 1582f2512ba1SRui Paulo } 1583f2512ba1SRui Paulo 1584a0292f23SGarrett Wollman /* 1585a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1586a0292f23SGarrett Wollman * Transitions: 1587a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1588a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1589a0292f23SGarrett Wollman */ 15909b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1591a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1592a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1593a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1594fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 15957ff19458SPaul Traina } else { 1596a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1597b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 15987ff19458SPaul Traina } 1599a0292f23SGarrett Wollman } else { 1600a0292f23SGarrett Wollman /* 1601c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 1602c068736aSJeffrey Hsu * simultaneous open. If segment contains CC option 1603c068736aSJeffrey Hsu * and there is a cached CC, apply TAO test. 1604c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 1605c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 1606a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1607a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1608a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1609a0292f23SGarrett Wollman */ 16104b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 1611b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1612df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1613a0292f23SGarrett Wollman } 1614df8bae1dSRodney W. Grimes 1615252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: trimthenstep6: " 1616252ca428SRobert Watson "ti_locked %d", __func__, ti_locked)); 1617252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 16188501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 16197cfc6904SRobert Watson 1620df8bae1dSRodney W. Grimes /* 1621fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1622df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1623df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1624df8bae1dSRodney W. Grimes */ 1625fb59c426SYoshinobu Inoue th->th_seq++; 1626fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1627fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1628df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1629fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1630fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1631603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvpackafterwin++; 1632603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbyteafterwin += todrop; 1633df8bae1dSRodney W. Grimes } 1634fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1635fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 1636a0292f23SGarrett Wollman /* 1637a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1638a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1639a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1640a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1641a0292f23SGarrett Wollman * Otherwise, goto step 6. 1642a0292f23SGarrett Wollman */ 1643fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1644a0292f23SGarrett Wollman goto process_ACK; 1645c068736aSJeffrey Hsu 1646df8bae1dSRodney W. Grimes goto step6; 1647c068736aSJeffrey Hsu 1648a0292f23SGarrett Wollman /* 1649a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1650c94c54e4SAndre Oppermann * do normal processing. 1651a0292f23SGarrett Wollman * 1652c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 1653a0292f23SGarrett Wollman */ 1654a0292f23SGarrett Wollman case TCPS_LAST_ACK: 1655a0292f23SGarrett Wollman case TCPS_CLOSING: 1656a0292f23SGarrett Wollman break; /* continue normal processing */ 1657df8bae1dSRodney W. Grimes } 1658df8bae1dSRodney W. Grimes 1659df8bae1dSRodney W. Grimes /* 1660df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 166180ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 166280ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 166380ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 166480ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 166580ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 166680ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 166780ab7c0eSGarrett Wollman * killing a connection). 166880ab7c0eSGarrett Wollman * Then check timestamp, if present. 1669a0292f23SGarrett Wollman * Then check the connection count, if present. 1670df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 1671df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 1672df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 1673df8bae1dSRodney W. Grimes * 167480ab7c0eSGarrett Wollman * 167580ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 167680ab7c0eSGarrett Wollman * if this is a valid reset segment. 167780ab7c0eSGarrett Wollman * RFC 793 page 37: 167880ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 167980ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 168080ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 168180ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 168280ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 16831a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 16841a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 16851a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 16861a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 168780dd2a81SMike Silbersack * Note 2: Paul Watson's paper "Slipping in the Window" has shown 168880dd2a81SMike Silbersack * that brute force RST attacks are possible. To combat this, 168980dd2a81SMike Silbersack * we use a much stricter check while in the ESTABLISHED state, 169080dd2a81SMike Silbersack * only accepting RSTs where the sequence number is equal to 169180dd2a81SMike Silbersack * last_ack_sent. In all other states (the states in which a 169280dd2a81SMike Silbersack * RST is more likely), the more permissive check is used. 16938e5c87f4SBjoern A. Zeeb * If we have multiple segments in flight, the initial reset 169480ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 169580ab7c0eSGarrett Wollman * but they will eventually catch up. 169680ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 169780ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 169880ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 169980ab7c0eSGarrett Wollman * 170080ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 170180ab7c0eSGarrett Wollman * 170280ab7c0eSGarrett Wollman * DISCUSSION 170380ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 170480ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 170580ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 170680ab7c0eSGarrett Wollman * data. 170780ab7c0eSGarrett Wollman * 170880ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 170980ab7c0eSGarrett Wollman * the state: 171080ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 171180ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 171280ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 1713745bab7fSDima Dorfman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 171480ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 1715e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 171680ab7c0eSGarrett Wollman * Close the tcb. 1717e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 171880ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 171980ab7c0eSGarrett Wollman * RFC 1337. 172080ab7c0eSGarrett Wollman */ 1721fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 172295ad8418SQing Li if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 172395ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 172480ab7c0eSGarrett Wollman switch (tp->t_state) { 172580ab7c0eSGarrett Wollman 172680ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 172780ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 172880ab7c0eSGarrett Wollman goto close; 172980ab7c0eSGarrett Wollman 173080ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 1731603724d3SBjoern A. Zeeb if (V_tcp_insecure_rst == 0 && 173295ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) && 173395ad8418SQing Li SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) && 173495ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 173595ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) { 1736603724d3SBjoern A. Zeeb V_tcpstat.tcps_badrst++; 173780dd2a81SMike Silbersack goto drop; 173880dd2a81SMike Silbersack } 1739564aab1fSAndre Oppermann /* FALLTHROUGH */ 174080ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 174180ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 174280ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 174380ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 174480ab7c0eSGarrett Wollman close: 1745252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 1746252ca428SRobert Watson ("tcp_do_segment: TH_RST 1 ti_locked %d", 1747252ca428SRobert Watson ti_locked)); 1748252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1749252ca428SRobert Watson 175080ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 1751603724d3SBjoern A. Zeeb V_tcpstat.tcps_drops++; 175280ab7c0eSGarrett Wollman tp = tcp_close(tp); 175380ab7c0eSGarrett Wollman break; 175480ab7c0eSGarrett Wollman 175580ab7c0eSGarrett Wollman case TCPS_CLOSING: 175680ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 1757252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 1758252ca428SRobert Watson ("tcp_do_segment: TH_RST 2 ti_locked %d", 1759252ca428SRobert Watson ti_locked)); 1760252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1761252ca428SRobert Watson 176280ab7c0eSGarrett Wollman tp = tcp_close(tp); 176380ab7c0eSGarrett Wollman break; 176480ab7c0eSGarrett Wollman } 176580ab7c0eSGarrett Wollman } 176680ab7c0eSGarrett Wollman goto drop; 176780ab7c0eSGarrett Wollman } 176880ab7c0eSGarrett Wollman 176980ab7c0eSGarrett Wollman /* 1770df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 1771df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 1772df8bae1dSRodney W. Grimes */ 1773be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 177480ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1775df8bae1dSRodney W. Grimes 1776df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 17779b8b58e0SJonathan Lemon if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1778df8bae1dSRodney W. Grimes /* 1779df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 1780df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 1781df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 1782df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 1783df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 1784df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 1785df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 1786df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 1787df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 1788df8bae1dSRodney W. Grimes */ 1789df8bae1dSRodney W. Grimes tp->ts_recent = 0; 1790df8bae1dSRodney W. Grimes } else { 1791603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvduppack++; 1792603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvdupbyte += tlen; 1793603724d3SBjoern A. Zeeb V_tcpstat.tcps_pawsdrop++; 179407fd333dSMatthew Dillon if (tlen) 1795df8bae1dSRodney W. Grimes goto dropafterack; 17961ab4789dSMatthew Dillon goto drop; 17971ab4789dSMatthew Dillon } 1798df8bae1dSRodney W. Grimes } 1799df8bae1dSRodney W. Grimes 1800a0292f23SGarrett Wollman /* 180180ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 180280ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 180380ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 180480ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 180580ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 180680ab7c0eSGarrett Wollman */ 1807a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 1808a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1809a57815efSBosko Milekic goto dropwithreset; 1810a57815efSBosko Milekic } 181180ab7c0eSGarrett Wollman 1812fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 1813df8bae1dSRodney W. Grimes if (todrop > 0) { 1814fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1815fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 1816fb59c426SYoshinobu Inoue th->th_seq++; 1817fb59c426SYoshinobu Inoue if (th->th_urp > 1) 1818fb59c426SYoshinobu Inoue th->th_urp--; 1819df8bae1dSRodney W. Grimes else 1820fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1821df8bae1dSRodney W. Grimes todrop--; 1822df8bae1dSRodney W. Grimes } 1823df8bae1dSRodney W. Grimes /* 1824dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 1825df8bae1dSRodney W. Grimes */ 1826fb59c426SYoshinobu Inoue if (todrop > tlen 1827fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1828dac20301SGarrett Wollman /* 1829dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 1830dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 1831dac20301SGarrett Wollman * of sequence; drop it. 1832dac20301SGarrett Wollman */ 1833fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1834dac20301SGarrett Wollman 1835df8bae1dSRodney W. Grimes /* 1836dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 1837dac20301SGarrett Wollman * But keep on processing for RST or ACK. 1838df8bae1dSRodney W. Grimes */ 1839dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1840fb59c426SYoshinobu Inoue todrop = tlen; 1841603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvduppack++; 1842603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvdupbyte += todrop; 1843df8bae1dSRodney W. Grimes } else { 1844603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvpartduppack++; 1845603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvpartdupbyte += todrop; 1846df8bae1dSRodney W. Grimes } 1847fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 1848fb59c426SYoshinobu Inoue th->th_seq += todrop; 1849fb59c426SYoshinobu Inoue tlen -= todrop; 1850fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 1851fb59c426SYoshinobu Inoue th->th_urp -= todrop; 1852df8bae1dSRodney W. Grimes else { 1853fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1854fb59c426SYoshinobu Inoue th->th_urp = 0; 1855df8bae1dSRodney W. Grimes } 1856df8bae1dSRodney W. Grimes } 1857df8bae1dSRodney W. Grimes 1858df8bae1dSRodney W. Grimes /* 1859df8bae1dSRodney W. Grimes * If new data are received on a connection after the 1860df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 1861df8bae1dSRodney W. Grimes */ 1862df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 1863fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1864773673c1SAndre Oppermann char *s; 1865773673c1SAndre Oppermann 1866252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: SS_NOFDEREF && " 1867252ca428SRobert Watson "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked)); 1868252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1869252ca428SRobert Watson 1870773673c1SAndre Oppermann if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) { 1871e31d8aa3SMike Silbersack log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data after socket " 1872773673c1SAndre Oppermann "was closed, sending RST and removing tcpcb\n", 1873e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 1874773673c1SAndre Oppermann free(s, M_TCPLOG); 1875773673c1SAndre Oppermann } 1876df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1877603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvafterclose++; 1878a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1879df8bae1dSRodney W. Grimes goto dropwithreset; 1880df8bae1dSRodney W. Grimes } 1881df8bae1dSRodney W. Grimes 1882df8bae1dSRodney W. Grimes /* 1883df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 1884df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 1885df8bae1dSRodney W. Grimes */ 1886fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 1887df8bae1dSRodney W. Grimes if (todrop > 0) { 1888603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvpackafterwin++; 1889fb59c426SYoshinobu Inoue if (todrop >= tlen) { 1890603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbyteafterwin += tlen; 1891df8bae1dSRodney W. Grimes /* 1892df8bae1dSRodney W. Grimes * If window is closed can only take segments at 1893df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 1894df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 1895df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 1896df8bae1dSRodney W. Grimes * and ack. 1897df8bae1dSRodney W. Grimes */ 1898fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1899df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1900603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvwinprobe++; 1901df8bae1dSRodney W. Grimes } else 1902df8bae1dSRodney W. Grimes goto dropafterack; 1903df8bae1dSRodney W. Grimes } else 1904603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbyteafterwin += todrop; 1905df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1906fb59c426SYoshinobu Inoue tlen -= todrop; 1907fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 1908df8bae1dSRodney W. Grimes } 1909df8bae1dSRodney W. Grimes 1910df8bae1dSRodney W. Grimes /* 1911df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1912df8bae1dSRodney W. Grimes * record its timestamp. 1913cf09195bSPaul Saab * NOTE: 1914cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 1915a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1916cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 1917cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 1918cf09195bSPaul Saab * predicated on the sequence space of this segment. 1919cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 1920cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 1921cf09195bSPaul Saab * instead of RFC1323's 1922cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 1923cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 1924cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 1925cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 1926cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 1927df8bae1dSRodney W. Grimes */ 1928be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1929cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 1930cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 1931cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 19329b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1933a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1934df8bae1dSRodney W. Grimes } 1935df8bae1dSRodney W. Grimes 1936df8bae1dSRodney W. Grimes /* 1937df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 1938df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 1939df8bae1dSRodney W. Grimes */ 1940fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1941252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 1942252ca428SRobert Watson ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked)); 1943252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1944252ca428SRobert Watson 1945df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 1946a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1947122aad88SAndre Oppermann goto drop; 1948df8bae1dSRodney W. Grimes } 1949df8bae1dSRodney W. Grimes 1950a0292f23SGarrett Wollman /* 1951a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1952a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 1953a0292f23SGarrett Wollman * later processing; else drop segment and return. 1954a0292f23SGarrett Wollman */ 1955fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 1956a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 1957a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 1958a0292f23SGarrett Wollman goto step6; 19595e1aa279SDavid Malone else if (tp->t_flags & TF_ACKNOW) 19605e1aa279SDavid Malone goto dropafterack; 1961a0292f23SGarrett Wollman else 1962a0292f23SGarrett Wollman goto drop; 1963a0292f23SGarrett Wollman } 1964df8bae1dSRodney W. Grimes 1965df8bae1dSRodney W. Grimes /* 1966df8bae1dSRodney W. Grimes * Ack processing. 1967df8bae1dSRodney W. Grimes */ 1968df8bae1dSRodney W. Grimes switch (tp->t_state) { 1969df8bae1dSRodney W. Grimes 1970df8bae1dSRodney W. Grimes /* 1971764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1972764d8cefSBill Fenner * ESTABLISHED state and continue processing. 1973764d8cefSBill Fenner * The ACK was checked above. 1974df8bae1dSRodney W. Grimes */ 1975df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1976a0292f23SGarrett Wollman 1977603724d3SBjoern A. Zeeb V_tcpstat.tcps_connects++; 1978df8bae1dSRodney W. Grimes soisconnected(so); 1979df8bae1dSRodney W. Grimes /* Do window scaling? */ 1980df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1981df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1982df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 1983464fcfbcSAndre Oppermann tp->snd_wnd = tiwin; 1984df8bae1dSRodney W. Grimes } 1985a0292f23SGarrett Wollman /* 1986a0292f23SGarrett Wollman * Make transitions: 1987a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 1988a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 1989a0292f23SGarrett Wollman */ 19909b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1991a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1992a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1993a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 19947ff19458SPaul Traina } else { 1995a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1996b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 19977ff19458SPaul Traina } 1998a0292f23SGarrett Wollman /* 1999a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 2000a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 2001a0292f23SGarrett Wollman */ 2002fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 2003fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 2004a0292f23SGarrett Wollman (struct mbuf *)0); 2005fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 200693b0017fSPhilippe Charnier /* FALLTHROUGH */ 2007df8bae1dSRodney W. Grimes 2008df8bae1dSRodney W. Grimes /* 2009df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2010df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 2011fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 2012fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 2013df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 2014df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 2015df8bae1dSRodney W. Grimes */ 2016df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2017df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2018df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2019df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 2020df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2021df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 20225a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 2023603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvacktoomuch++; 20245a53ca16SPaul Saab goto dropafterack; 20255a53ca16SPaul Saab } 20263529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2027fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 2028fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 20295a53ca16SPaul Saab tcp_sack_doack(tp, &to, th->th_ack); 2030fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 2031fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 2032603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvdupack++; 2033df8bae1dSRodney W. Grimes /* 2034df8bae1dSRodney W. Grimes * If we have outstanding data (other than 2035df8bae1dSRodney W. Grimes * a window probe), this is a completely 2036df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 2037df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 2038df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 2039df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 2040df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 2041df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 2042df8bae1dSRodney W. Grimes * window so we send only this one 2043df8bae1dSRodney W. Grimes * packet. 2044df8bae1dSRodney W. Grimes * 2045df8bae1dSRodney W. Grimes * We know we're losing at the current 2046df8bae1dSRodney W. Grimes * window size so do congestion avoidance 2047df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 2048df8bae1dSRodney W. Grimes * and pull our congestion window back to 2049df8bae1dSRodney W. Grimes * the new ssthresh). 2050df8bae1dSRodney W. Grimes * 2051df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 2052df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 2053df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 2054df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 2055df8bae1dSRodney W. Grimes * network. 2056f2512ba1SRui Paulo * 2057f2512ba1SRui Paulo * When using TCP ECN, notify the peer that 2058f2512ba1SRui Paulo * we reduced the cwnd. 2059df8bae1dSRodney W. Grimes */ 2060b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_REXMT) || 2061fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 2062df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2063cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 2064603724d3SBjoern A. Zeeb ((V_tcp_do_newreno || 20653529149eSAndre Oppermann (tp->t_flags & TF_SACK_PERMIT)) && 20669d11646dSJeffrey Hsu IN_FASTRECOVERY(tp))) { 20673529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 20683529149eSAndre Oppermann IN_FASTRECOVERY(tp)) { 2069808f11b7SPaul Saab int awnd; 207025e6f9edSPaul Saab 207125e6f9edSPaul Saab /* 207225e6f9edSPaul Saab * Compute the amount of data in flight first. 207325e6f9edSPaul Saab * We can inject new data into the pipe iff 207425e6f9edSPaul Saab * we have less than 1/2 the original window's 207525e6f9edSPaul Saab * worth of data in flight. 207625e6f9edSPaul Saab */ 2077808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 20780077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 2079808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 208025e6f9edSPaul Saab tp->snd_cwnd += tp->t_maxseg; 208125e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 208225e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 208325e6f9edSPaul Saab } 208425e6f9edSPaul Saab } else 208546f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 208646f58482SJonathan Lemon (void) tcp_output(tp); 208746f58482SJonathan Lemon goto drop; 2088cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 2089cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 2090a0445c2eSJayanth Vijayaraghavan 2091a0445c2eSJayanth Vijayaraghavan /* 2092a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 2093a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 2094a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 2095a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 2096a0445c2eSJayanth Vijayaraghavan * recovery. 2097a0445c2eSJayanth Vijayaraghavan */ 20983529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 2099a0445c2eSJayanth Vijayaraghavan if (IN_FASTRECOVERY(tp)) { 2100a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 2101a0445c2eSJayanth Vijayaraghavan break; 2102a0445c2eSJayanth Vijayaraghavan } 2103603724d3SBjoern A. Zeeb } else if (V_tcp_do_newreno || 2104603724d3SBjoern A. Zeeb V_tcp_do_ecn) { 2105a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 21069d11646dSJeffrey Hsu tp->snd_recover)) { 2107cb942153SJeffrey Hsu tp->t_dupacks = 0; 2108cb942153SJeffrey Hsu break; 210946f58482SJonathan Lemon } 2110a0445c2eSJayanth Vijayaraghavan } 2111f2512ba1SRui Paulo tcp_congestion_exp(tp); 2112b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 21139b8b58e0SJonathan Lemon tp->t_rtttime = 0; 21143529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 2115603724d3SBjoern A. Zeeb V_tcpstat.tcps_sack_recovery_episode++; 2116a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 21178db456bfSPaul Saab tp->snd_cwnd = tp->t_maxseg; 21186d90faf3SPaul Saab (void) tcp_output(tp); 21196d90faf3SPaul Saab goto drop; 21206d90faf3SPaul Saab } 2121fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 2122df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 2123df8bae1dSRodney W. Grimes (void) tcp_output(tp); 212448d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 2125302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 2126302ce8d6SAndre Oppermann __func__)); 2127df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 212848d2549cSJeffrey Hsu tp->t_maxseg * 212948d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 2130df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 2131df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 2132df8bae1dSRodney W. Grimes goto drop; 2133603724d3SBjoern A. Zeeb } else if (V_tcp_do_rfc3042) { 2134582a954bSJeffrey Hsu u_long oldcwnd = tp->snd_cwnd; 213548d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 213648d2549cSJeffrey Hsu u_int sent; 213789c02376SJeffrey Hsu 2138582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 2139582a954bSJeffrey Hsu tp->t_dupacks == 2, 2140302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 2141302ce8d6SAndre Oppermann __func__)); 214261a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 214348d2549cSJeffrey Hsu tp->snd_limited = 0; 214461a36e3dSJeffrey Hsu tp->snd_cwnd = 214561a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 214661a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 214761a36e3dSJeffrey Hsu tp->t_maxseg; 2148582a954bSJeffrey Hsu (void) tcp_output(tp); 214948d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 215048d2549cSJeffrey Hsu if (sent > tp->t_maxseg) { 215189c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 215289c02376SJeffrey Hsu tp->snd_limited == 0) || 215389c02376SJeffrey Hsu (sent == tp->t_maxseg + 1 && 215489c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 2155302ce8d6SAndre Oppermann ("%s: sent too much", 2156302ce8d6SAndre Oppermann __func__)); 215748d2549cSJeffrey Hsu tp->snd_limited = 2; 215848d2549cSJeffrey Hsu } else if (sent > 0) 215948d2549cSJeffrey Hsu ++tp->snd_limited; 2160582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 2161582a954bSJeffrey Hsu goto drop; 2162df8bae1dSRodney W. Grimes } 2163df8bae1dSRodney W. Grimes } else 2164df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2165df8bae1dSRodney W. Grimes break; 2166df8bae1dSRodney W. Grimes } 2167cb942153SJeffrey Hsu 2168302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2169302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 2170cb942153SJeffrey Hsu 2171df8bae1dSRodney W. Grimes /* 2172df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2173df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2174df8bae1dSRodney W. Grimes */ 2175603724d3SBjoern A. Zeeb if (V_tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) { 21769d11646dSJeffrey Hsu if (IN_FASTRECOVERY(tp)) { 2177cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 21783529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 21796d90faf3SPaul Saab tcp_sack_partialack(tp, th); 21806d90faf3SPaul Saab else 2181c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2182c068736aSJeffrey Hsu } else { 2183c068736aSJeffrey Hsu /* 21846d90faf3SPaul Saab * Out of fast recovery. 2185c068736aSJeffrey Hsu * Window inflation should have left us 2186c068736aSJeffrey Hsu * with approximately snd_ssthresh 2187c068736aSJeffrey Hsu * outstanding data. 2188c068736aSJeffrey Hsu * But in case we would be inclined to 2189c068736aSJeffrey Hsu * send a burst, better to do it via 2190c068736aSJeffrey Hsu * the slow start mechanism. 2191c068736aSJeffrey Hsu */ 2192c068736aSJeffrey Hsu if (SEQ_GT(th->th_ack + 2193c068736aSJeffrey Hsu tp->snd_ssthresh, 2194c068736aSJeffrey Hsu tp->snd_max)) 2195c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_max - 2196c068736aSJeffrey Hsu th->th_ack + 2197c068736aSJeffrey Hsu tp->t_maxseg; 2198c068736aSJeffrey Hsu else 2199c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_ssthresh; 2200c068736aSJeffrey Hsu } 2201c068736aSJeffrey Hsu } 2202c068736aSJeffrey Hsu } else { 2203233e8c18SGarrett Wollman if (tp->t_dupacks >= tcprexmtthresh && 2204df8bae1dSRodney W. Grimes tp->snd_cwnd > tp->snd_ssthresh) 2205df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh; 220646f58482SJonathan Lemon } 2207cb942153SJeffrey Hsu tp->t_dupacks = 0; 2208a0292f23SGarrett Wollman /* 2209a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2210a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2211a0292f23SGarrett Wollman */ 2212a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2213a0292f23SGarrett Wollman /* 2214a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2215a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 221607e43e10SAndras Olah * synchronized). Go to non-starred state, 221707e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 221807e43e10SAndras Olah * we can do window scaling. 2219a0292f23SGarrett Wollman */ 2220a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2221a0292f23SGarrett Wollman tp->snd_una++; 222207e43e10SAndras Olah /* Do window scaling? */ 222307e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 222407e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 222507e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2226464fcfbcSAndre Oppermann /* Send window already scaled. */ 222707e43e10SAndras Olah } 2228a0292f23SGarrett Wollman } 2229a0292f23SGarrett Wollman 2230a0292f23SGarrett Wollman process_ACK: 2231252ca428SRobert Watson INP_INFO_LOCK_ASSERT(&V_tcbinfo); 2232252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2233252ca428SRobert Watson ("tcp_input: process_ACK ti_locked %d", ti_locked)); 22348501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 22357cfc6904SRobert Watson 2236fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 2237603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvackpack++; 2238603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvackbyte += acked; 2239df8bae1dSRodney W. Grimes 2240df8bae1dSRodney W. Grimes /* 22419b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 22429b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 22439b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 22449b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 22459b8b58e0SJonathan Lemon * we left off. 22469b8b58e0SJonathan Lemon */ 22479b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 2248603724d3SBjoern A. Zeeb ++V_tcpstat.tcps_sndrexmitbad; 22499b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 22509b8b58e0SJonathan Lemon tp->snd_ssthresh = tp->snd_ssthresh_prev; 22519d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 22529d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 22539d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 22549b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 22559b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; /* XXX probably not required */ 22569b8b58e0SJonathan Lemon } 22579b8b58e0SJonathan Lemon 22589b8b58e0SJonathan Lemon /* 2259df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2260df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2261df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2262df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2263df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2264df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2265df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2266fa55172bSMatthew Dillon * 2267fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2268fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2269fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2270fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2271df8bae1dSRodney W. Grimes */ 2272fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 2273fa55172bSMatthew Dillon to.to_tsecr) { 2274eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - to.to_tsecr) 2275eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 22769b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 2277fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2278eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2279eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 22809b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2281fa55172bSMatthew Dillon } 22821fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 2283df8bae1dSRodney W. Grimes 2284df8bae1dSRodney W. Grimes /* 2285df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2286df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2287df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2288df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2289df8bae1dSRodney W. Grimes */ 2290fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 2291b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2292df8bae1dSRodney W. Grimes needoutput = 1; 2293b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2294b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2295a0292f23SGarrett Wollman 2296a0292f23SGarrett Wollman /* 2297a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2298a0292f23SGarrett Wollman * skip rest of ACK processing. 2299a0292f23SGarrett Wollman */ 2300a0292f23SGarrett Wollman if (acked == 0) 2301a0292f23SGarrett Wollman goto step6; 2302a0292f23SGarrett Wollman 2303df8bae1dSRodney W. Grimes /* 2304df8bae1dSRodney W. Grimes * When new data is acked, open the congestion window. 230524cb0f22SLawrence Stewart * Method depends on which congestion control state we're 230624cb0f22SLawrence Stewart * in (slow start or cong avoid) and if ABC (RFC 3465) is 230724cb0f22SLawrence Stewart * enabled. 230824cb0f22SLawrence Stewart * 230924cb0f22SLawrence Stewart * slow start: cwnd <= ssthresh 231024cb0f22SLawrence Stewart * cong avoid: cwnd > ssthresh 231124cb0f22SLawrence Stewart * 231224cb0f22SLawrence Stewart * slow start and ABC (RFC 3465): 231324cb0f22SLawrence Stewart * Grow cwnd exponentially by the amount of data 231424cb0f22SLawrence Stewart * ACKed capping the max increment per ACK to 231524cb0f22SLawrence Stewart * (abc_l_var * maxseg) bytes. 231624cb0f22SLawrence Stewart * 231724cb0f22SLawrence Stewart * slow start without ABC (RFC 2581): 231824cb0f22SLawrence Stewart * Grow cwnd exponentially by maxseg per ACK. 231924cb0f22SLawrence Stewart * 232024cb0f22SLawrence Stewart * cong avoid and ABC (RFC 3465): 232124cb0f22SLawrence Stewart * Grow cwnd linearly by maxseg per RTT for each 232224cb0f22SLawrence Stewart * cwnd worth of ACKed data. 232324cb0f22SLawrence Stewart * 232424cb0f22SLawrence Stewart * cong avoid without ABC (RFC 2581): 232524cb0f22SLawrence Stewart * Grow cwnd linearly by approximately maxseg per RTT using 232624cb0f22SLawrence Stewart * maxseg^2 / cwnd per ACK as the increment. 232724cb0f22SLawrence Stewart * If cwnd > maxseg^2, fix the cwnd increment at 1 byte to 232824cb0f22SLawrence Stewart * avoid capping cwnd. 2329df8bae1dSRodney W. Grimes */ 2330603724d3SBjoern A. Zeeb if ((!V_tcp_do_newreno && !(tp->t_flags & TF_SACK_PERMIT)) || 23316d90faf3SPaul Saab !IN_FASTRECOVERY(tp)) { 2332ad3f9ab3SAndre Oppermann u_int cw = tp->snd_cwnd; 2333ad3f9ab3SAndre Oppermann u_int incr = tp->t_maxseg; 233424cb0f22SLawrence Stewart /* In congestion avoidance? */ 233524cb0f22SLawrence Stewart if (cw > tp->snd_ssthresh) { 233624cb0f22SLawrence Stewart if (V_tcp_do_rfc3465) { 233724cb0f22SLawrence Stewart tp->t_bytes_acked += acked; 233824cb0f22SLawrence Stewart if (tp->t_bytes_acked >= tp->snd_cwnd) 233924cb0f22SLawrence Stewart tp->t_bytes_acked -= cw; 234024cb0f22SLawrence Stewart else 234124cb0f22SLawrence Stewart incr = 0; 234224cb0f22SLawrence Stewart } 234324cb0f22SLawrence Stewart else 2344c10eb6d1SBjoern A. Zeeb incr = max((incr * incr / cw), 1); 234524cb0f22SLawrence Stewart /* 234624cb0f22SLawrence Stewart * In slow-start with ABC enabled and no RTO in sight? 234724cb0f22SLawrence Stewart * (Must not use abc_l_var > 1 if slow starting after an 234824cb0f22SLawrence Stewart * RTO. On RTO, snd_nxt = snd_una, so the snd_nxt == 234924cb0f22SLawrence Stewart * snd_max check is sufficient to handle this). 235024cb0f22SLawrence Stewart */ 235124cb0f22SLawrence Stewart } else if (V_tcp_do_rfc3465 && 235224cb0f22SLawrence Stewart tp->snd_nxt == tp->snd_max) 235324cb0f22SLawrence Stewart incr = min(acked, 235424cb0f22SLawrence Stewart V_tcp_abc_l_var * tp->t_maxseg); 235524cb0f22SLawrence Stewart /* ABC is on by default, so (incr == 0) frequently. */ 235624cb0f22SLawrence Stewart if (incr > 0) 2357df8bae1dSRodney W. Grimes tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale); 2358df8bae1dSRodney W. Grimes } 23595905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2360df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 2361df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 23625905999bSRobert Watson sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc); 2363df8bae1dSRodney W. Grimes ourfinisacked = 1; 2364df8bae1dSRodney W. Grimes } else { 23655905999bSRobert Watson sbdrop_locked(&so->so_snd, acked); 2366df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 2367df8bae1dSRodney W. Grimes ourfinisacked = 0; 2368df8bae1dSRodney W. Grimes } 2369564aab1fSAndre Oppermann /* NB: sowwakeup_locked() does an implicit unlock. */ 23701e4d7da7SRobert Watson sowwakeup_locked(so); 2371e8949f74SAndre Oppermann /* Detect una wraparound. */ 2372603724d3SBjoern A. Zeeb if ((V_tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) && 23736d90faf3SPaul Saab !IN_FASTRECOVERY(tp) && 23749d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 23759d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 23769d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 2377603724d3SBjoern A. Zeeb if ((V_tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) && 23786d90faf3SPaul Saab IN_FASTRECOVERY(tp) && 237924cb0f22SLawrence Stewart SEQ_GEQ(th->th_ack, tp->snd_recover)) { 23809d11646dSJeffrey Hsu EXIT_FASTRECOVERY(tp); 238124cb0f22SLawrence Stewart tp->t_bytes_acked = 0; 238224cb0f22SLawrence Stewart } 2383fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 23843529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 23856d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 23866d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 23876d90faf3SPaul Saab } 2388df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2389df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2390df8bae1dSRodney W. Grimes 2391df8bae1dSRodney W. Grimes switch (tp->t_state) { 2392df8bae1dSRodney W. Grimes 2393df8bae1dSRodney W. Grimes /* 2394df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2395df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2396df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2397df8bae1dSRodney W. Grimes */ 2398df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2399df8bae1dSRodney W. Grimes if (ourfinisacked) { 2400df8bae1dSRodney W. Grimes /* 2401df8bae1dSRodney W. Grimes * If we can't receive any more 2402df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2403df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2404df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2405df8bae1dSRodney W. Grimes * we'll hang forever. 2406e8949f74SAndre Oppermann * 2407e8949f74SAndre Oppermann * XXXjl: 2408340c35deSJonathan Lemon * we should release the tp also, and use a 2409340c35deSJonathan Lemon * compressed state. 2410340c35deSJonathan Lemon */ 2411c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 24127c72af87SMohan Srinivasan int timeout; 24137c72af87SMohan Srinivasan 241403e49181SSeigo Tanimura soisdisconnected(so); 24157c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 24167c72af87SMohan Srinivasan tcp_finwait2_timeout : tcp_maxidle; 2417b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 24184cc20ab1SSeigo Tanimura } 2419df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 2420df8bae1dSRodney W. Grimes } 2421df8bae1dSRodney W. Grimes break; 2422df8bae1dSRodney W. Grimes 2423df8bae1dSRodney W. Grimes /* 2424df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2425df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2426df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2427df8bae1dSRodney W. Grimes * the segment. 2428df8bae1dSRodney W. Grimes */ 2429df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2430df8bae1dSRodney W. Grimes if (ourfinisacked) { 2431252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 243211a20fb8SJeffrey Hsu tcp_twstart(tp); 2433603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2434340c35deSJonathan Lemon m_freem(m); 2435679d9708SAndre Oppermann return; 2436df8bae1dSRodney W. Grimes } 2437df8bae1dSRodney W. Grimes break; 2438df8bae1dSRodney W. Grimes 2439df8bae1dSRodney W. Grimes /* 2440df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2441df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2442df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2443df8bae1dSRodney W. Grimes * enter the closed state and return. 2444df8bae1dSRodney W. Grimes */ 2445df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2446df8bae1dSRodney W. Grimes if (ourfinisacked) { 2447252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2448df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2449df8bae1dSRodney W. Grimes goto drop; 2450df8bae1dSRodney W. Grimes } 2451df8bae1dSRodney W. Grimes break; 2452df8bae1dSRodney W. Grimes } 2453df8bae1dSRodney W. Grimes } 2454df8bae1dSRodney W. Grimes 2455df8bae1dSRodney W. Grimes step6: 2456252ca428SRobert Watson INP_INFO_LOCK_ASSERT(&V_tcbinfo); 2457252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2458252ca428SRobert Watson ("tcp_do_segment: step6 ti_locked %d", ti_locked)); 24598501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 24607cfc6904SRobert Watson 2461df8bae1dSRodney W. Grimes /* 2462df8bae1dSRodney W. Grimes * Update window information. 2463df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 2464df8bae1dSRodney W. Grimes */ 2465fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 2466fb59c426SYoshinobu Inoue (SEQ_LT(tp->snd_wl1, th->th_seq) || 2467fb59c426SYoshinobu Inoue (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2468fb59c426SYoshinobu Inoue (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2469df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 2470fb59c426SYoshinobu Inoue if (tlen == 0 && 2471fb59c426SYoshinobu Inoue tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2472603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvwinupd++; 2473df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 2474fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq; 2475fb59c426SYoshinobu Inoue tp->snd_wl2 = th->th_ack; 2476df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2477df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 2478df8bae1dSRodney W. Grimes needoutput = 1; 2479df8bae1dSRodney W. Grimes } 2480df8bae1dSRodney W. Grimes 2481df8bae1dSRodney W. Grimes /* 2482df8bae1dSRodney W. Grimes * Process segments with URG. 2483df8bae1dSRodney W. Grimes */ 2484fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2485df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2486df8bae1dSRodney W. Grimes /* 2487df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2488df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2489df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2490df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2491df8bae1dSRodney W. Grimes */ 249218ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2493fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2494fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2495fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 249618ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2497df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2498df8bae1dSRodney W. Grimes } 2499df8bae1dSRodney W. Grimes /* 2500df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2501df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2502df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2503df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2504df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2505df8bae1dSRodney W. Grimes * 2506df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2507df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2508df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2509df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2510df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2511df8bae1dSRodney W. Grimes * spec states (in one of two places). 2512df8bae1dSRodney W. Grimes */ 2513fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2514fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2515df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2516df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2517927c5ceaSRobert Watson if (so->so_oobmark == 0) 2518c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 2519df8bae1dSRodney W. Grimes sohasoutofband(so); 2520df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2521df8bae1dSRodney W. Grimes } 252218ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2523df8bae1dSRodney W. Grimes /* 2524df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2525df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2526df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2527df8bae1dSRodney W. Grimes * data may creep in... ick. 2528df8bae1dSRodney W. Grimes */ 252930613f56SJeffrey Hsu if (th->th_urp <= (u_long)tlen && 253030613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 253130613f56SJeffrey Hsu /* hdr drop is delayed */ 253230613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 253330613f56SJeffrey Hsu } 2534c068736aSJeffrey Hsu } else { 2535df8bae1dSRodney W. Grimes /* 2536df8bae1dSRodney W. Grimes * If no out of band data is expected, 2537df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2538df8bae1dSRodney W. Grimes * with the receive window. 2539df8bae1dSRodney W. Grimes */ 2540df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2541df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2542c068736aSJeffrey Hsu } 2543df8bae1dSRodney W. Grimes dodata: /* XXX */ 2544252ca428SRobert Watson INP_INFO_LOCK_ASSERT(&V_tcbinfo); 2545252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2546252ca428SRobert Watson ("tcp_do_segment: dodata ti_locked %d", ti_locked)); 25478501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 25487cfc6904SRobert Watson 2549df8bae1dSRodney W. Grimes /* 2550df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2551df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2552df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2553df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2554df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2555df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2556df8bae1dSRodney W. Grimes */ 2557fb59c426SYoshinobu Inoue if ((tlen || (thflags & TH_FIN)) && 2558df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 255969e03620SPaul Saab tcp_seq save_start = th->th_seq; 2560fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2561e4b64281SJesper Skriver /* 2562c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2563c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2564c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2565c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2566c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2567c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2568c068736aSJeffrey Hsu * conversions. 2569c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 2570c068736aSJeffrey Hsu * immediately when segments are out of order (so 2571c068736aSJeffrey Hsu * fast retransmit can work). 2572e4b64281SJesper Skriver */ 2573e4b64281SJesper Skriver if (th->th_seq == tp->rcv_nxt && 2574e4b64281SJesper Skriver LIST_EMPTY(&tp->t_segq) && 2575e4b64281SJesper Skriver TCPS_HAVEESTABLISHED(tp->t_state)) { 2576e4b64281SJesper Skriver if (DELAY_ACK(tp)) 25773bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2578e4b64281SJesper Skriver else 2579e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2580e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2581e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 2582603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvpack++; 2583603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbyte += tlen; 2584e4b64281SJesper Skriver ND6_HINT(tp); 25851e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2586c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2587c1c36a2cSMike Silbersack m_freem(m); 2588c1c36a2cSMike Silbersack else 25891e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 2590e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 25911e4d7da7SRobert Watson sorwakeup_locked(so); 2592e4b64281SJesper Skriver } else { 2593e8949f74SAndre Oppermann /* 2594e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 2595e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 2596e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 2597e8949f74SAndre Oppermann * when trimming from the head. 2598e8949f74SAndre Oppermann */ 2599e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 2600e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2601e4b64281SJesper Skriver } 26023529149eSAndre Oppermann if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 2603f194524fSAndre Oppermann tcp_update_sack_list(tp, save_start, save_start + tlen); 2604302ce8d6SAndre Oppermann #if 0 2605df8bae1dSRodney W. Grimes /* 2606df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2607df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2608df8bae1dSRodney W. Grimes * buffer size. 2609302ce8d6SAndre Oppermann * XXX: Unused. 2610df8bae1dSRodney W. Grimes */ 2611df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2612302ce8d6SAndre Oppermann #endif 2613df8bae1dSRodney W. Grimes } else { 2614df8bae1dSRodney W. Grimes m_freem(m); 2615fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2616df8bae1dSRodney W. Grimes } 2617df8bae1dSRodney W. Grimes 2618df8bae1dSRodney W. Grimes /* 2619df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2620df8bae1dSRodney W. Grimes * that the connection is closing. 2621df8bae1dSRodney W. Grimes */ 2622fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2623df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2624df8bae1dSRodney W. Grimes socantrcvmore(so); 2625a0292f23SGarrett Wollman /* 2626a0292f23SGarrett Wollman * If connection is half-synchronized 2627d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2628a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2629a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2630a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2631a0292f23SGarrett Wollman */ 26323bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 26333bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2634a0292f23SGarrett Wollman else 2635df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2636df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2637df8bae1dSRodney W. Grimes } 2638df8bae1dSRodney W. Grimes switch (tp->t_state) { 2639df8bae1dSRodney W. Grimes 2640df8bae1dSRodney W. Grimes /* 2641df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2642df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2643df8bae1dSRodney W. Grimes */ 2644df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 26459b8b58e0SJonathan Lemon tp->t_starttime = ticks; 26469b8b58e0SJonathan Lemon /* FALLTHROUGH */ 2647df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2648df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2649df8bae1dSRodney W. Grimes break; 2650df8bae1dSRodney W. Grimes 2651df8bae1dSRodney W. Grimes /* 2652df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2653df8bae1dSRodney W. Grimes * enter the CLOSING state. 2654df8bae1dSRodney W. Grimes */ 2655df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2656df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 2657df8bae1dSRodney W. Grimes break; 2658df8bae1dSRodney W. Grimes 2659df8bae1dSRodney W. Grimes /* 2660df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 2661df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 2662df8bae1dSRodney W. Grimes * standard timers. 2663df8bae1dSRodney W. Grimes */ 2664df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2665252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2666252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: dodata " 2667252ca428SRobert Watson "TCP_FIN_WAIT_2 ti_locked: %d", __func__, 2668252ca428SRobert Watson ti_locked)); 2669252ca428SRobert Watson 2670340c35deSJonathan Lemon tcp_twstart(tp); 2671603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2672679d9708SAndre Oppermann return; 2673df8bae1dSRodney W. Grimes } 2674df8bae1dSRodney W. Grimes } 2675252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 2676252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 2677252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 2678603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2679252ca428SRobert Watson else 2680252ca428SRobert Watson panic("%s: dodata epilogue ti_locked %d", __func__, 2681252ca428SRobert Watson ti_locked); 2682252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2683252ca428SRobert Watson 2684610ee2f9SDavid Greenman #ifdef TCPDEBUG 26854cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2686fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2687fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2688610ee2f9SDavid Greenman #endif 2689df8bae1dSRodney W. Grimes 2690df8bae1dSRodney W. Grimes /* 2691df8bae1dSRodney W. Grimes * Return any desired output. 2692df8bae1dSRodney W. Grimes */ 2693df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 2694df8bae1dSRodney W. Grimes (void) tcp_output(tp); 26957792ea27SJeffrey Hsu 2696a14c749fSJonathan Lemon check_delack: 2697252ca428SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 2698252ca428SRobert Watson __func__, ti_locked)); 2699603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 27008501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 2701252ca428SRobert Watson 2702a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 27033bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 2704b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 27053bfd6421SJonathan Lemon } 27068501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2707679d9708SAndre Oppermann return; 2708df8bae1dSRodney W. Grimes 2709df8bae1dSRodney W. Grimes dropafterack: 2710252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2711252ca428SRobert Watson ("tcp_do_segment: dropafterack ti_locked %d", ti_locked)); 2712252ca428SRobert Watson 2713df8bae1dSRodney W. Grimes /* 2714df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 2715df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 271680ab7c0eSGarrett Wollman * 271780ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 271880ab7c0eSGarrett Wollman * paths to this code happen after packets containing 271980ab7c0eSGarrett Wollman * RST have been dropped. 272080ab7c0eSGarrett Wollman * 272180ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 272280ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 272380ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 272480ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 272580ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 272680ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 2727df8bae1dSRodney W. Grimes */ 2728fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2729fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 2730a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 2731a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2732a57815efSBosko Milekic goto dropwithreset; 2733a57815efSBosko Milekic } 2734a0292f23SGarrett Wollman #ifdef TCPDEBUG 27354cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2736fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2737fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2738a0292f23SGarrett Wollman #endif 2739252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 2740252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 2741252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 2742603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2743252ca428SRobert Watson else 2744252ca428SRobert Watson panic("%s: dropafterack epilogue ti_locked %d", __func__, 2745252ca428SRobert Watson ti_locked); 2746252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2747252ca428SRobert Watson 2748df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2749df8bae1dSRodney W. Grimes (void) tcp_output(tp); 27508501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2751d6915262SRobert Watson m_freem(m); 2752679d9708SAndre Oppermann return; 2753df8bae1dSRodney W. Grimes 2754df8bae1dSRodney W. Grimes dropwithreset: 2755252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 2756252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 2757252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 2758dd8ac7f9SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 2759252ca428SRobert Watson else 2760252ca428SRobert Watson panic("%s: dropwithreset ti_locked %d", __func__, ti_locked); 2761252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2762a57815efSBosko Milekic 2763a0ca0871SRobert Watson if (tp != NULL) { 2764302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 27658501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2766dd8ac7f9SRobert Watson } else 2767a0ca0871SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 2768679d9708SAndre Oppermann return; 2769df8bae1dSRodney W. Grimes 2770df8bae1dSRodney W. Grimes drop: 2771252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 2772252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 2773252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 2774252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 2775252ca428SRobert Watson #ifdef INVARIANTS 2776252ca428SRobert Watson else 2777252ca428SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 2778252ca428SRobert Watson #endif 2779252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2780252ca428SRobert Watson 2781df8bae1dSRodney W. Grimes /* 2782df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 2783df8bae1dSRodney W. Grimes */ 2784610ee2f9SDavid Greenman #ifdef TCPDEBUG 27851c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2786fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2787fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2788a0292f23SGarrett Wollman #endif 27891c53f806SRobert Watson if (tp != NULL) 27908501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2791d6915262SRobert Watson m_freem(m); 2792302ce8d6SAndre Oppermann } 2793302ce8d6SAndre Oppermann 2794302ce8d6SAndre Oppermann /* 27950ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 27960ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 27970ca3f933SAndre Oppermann * tp may be NULL. 2798302ce8d6SAndre Oppermann */ 2799302ce8d6SAndre Oppermann static void 2800302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 2801302ce8d6SAndre Oppermann int tlen, int rstreason) 2802302ce8d6SAndre Oppermann { 2803302ce8d6SAndre Oppermann struct ip *ip; 2804302ce8d6SAndre Oppermann #ifdef INET6 2805302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 2806302ce8d6SAndre Oppermann #endif 28077a3244ccSRobert Watson 28087a3244ccSRobert Watson if (tp != NULL) { 28098501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 28107a3244ccSRobert Watson } 28117a3244ccSRobert Watson 28120ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 2813302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2814302ce8d6SAndre Oppermann goto drop; 2815302ce8d6SAndre Oppermann #ifdef INET6 2816302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 2817302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 2818302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2819302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2820302ce8d6SAndre Oppermann goto drop; 2821302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 2822302ce8d6SAndre Oppermann } else 2823302ce8d6SAndre Oppermann #endif 2824302ce8d6SAndre Oppermann { 2825302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 2826302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2827302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2828302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 2829302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2830302ce8d6SAndre Oppermann goto drop; 2831302ce8d6SAndre Oppermann } 2832302ce8d6SAndre Oppermann 2833302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 2834302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 2835302ce8d6SAndre Oppermann goto drop; 2836302ce8d6SAndre Oppermann 2837302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 2838302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 2839302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 2840302ce8d6SAndre Oppermann th->th_ack, TH_RST); 2841302ce8d6SAndre Oppermann } else { 2842302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 2843302ce8d6SAndre Oppermann tlen++; 2844302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2845302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 2846302ce8d6SAndre Oppermann } 2847302ce8d6SAndre Oppermann return; 2848302ce8d6SAndre Oppermann drop: 2849302ce8d6SAndre Oppermann m_freem(m); 2850df8bae1dSRodney W. Grimes } 2851df8bae1dSRodney W. Grimes 2852be2ac88cSJonathan Lemon /* 2853be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 2854be2ac88cSJonathan Lemon */ 28550312fbe9SPoul-Henning Kamp static void 2856ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 2857df8bae1dSRodney W. Grimes { 28588b615593SMarko Zec INIT_VNET_INET(curvnet); 2859df8bae1dSRodney W. Grimes int opt, optlen; 2860df8bae1dSRodney W. Grimes 2861be2ac88cSJonathan Lemon to->to_flags = 0; 2862df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 2863df8bae1dSRodney W. Grimes opt = cp[0]; 2864df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 2865df8bae1dSRodney W. Grimes break; 2866df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 2867df8bae1dSRodney W. Grimes optlen = 1; 2868df8bae1dSRodney W. Grimes else { 2869b474779fSJun-ichiro itojun Hagino if (cnt < 2) 2870b474779fSJun-ichiro itojun Hagino break; 2871df8bae1dSRodney W. Grimes optlen = cp[1]; 2872b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 2873df8bae1dSRodney W. Grimes break; 2874df8bae1dSRodney W. Grimes } 2875df8bae1dSRodney W. Grimes switch (opt) { 2876df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 2877df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 2878df8bae1dSRodney W. Grimes continue; 2879f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2880df8bae1dSRodney W. Grimes continue; 2881be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 2882be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 2883be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 2884fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 2885df8bae1dSRodney W. Grimes break; 2886df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 2887df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 2888df8bae1dSRodney W. Grimes continue; 2889f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2890df8bae1dSRodney W. Grimes continue; 2891be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 289202a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 2893df8bae1dSRodney W. Grimes break; 2894df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 2895df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 2896df8bae1dSRodney W. Grimes continue; 2897be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 2898a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2899a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 2900fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 2901a0292f23SGarrett Wollman bcopy((char *)cp + 6, 2902a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2903fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 2904df8bae1dSRodney W. Grimes break; 29051cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 29061cfd4b53SBruce M Simpson /* 29071cfd4b53SBruce M Simpson * XXX In order to reply to a host which has set the 29081cfd4b53SBruce M Simpson * TCP_SIGNATURE option in its initial SYN, we have to 29091cfd4b53SBruce M Simpson * record the fact that the option was observed here 29101cfd4b53SBruce M Simpson * for the syncache code to perform the correct response. 29111cfd4b53SBruce M Simpson */ 29121cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 29131cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 29141cfd4b53SBruce M Simpson continue; 2915df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 2916df47e437SAndre Oppermann to->to_signature = cp + 2; 29171cfd4b53SBruce M Simpson break; 2918265ed012SBruce M Simpson #endif 29196d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 2920f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 29216d90faf3SPaul Saab continue; 2922f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2923f72167f4SAndre Oppermann continue; 2924603724d3SBjoern A. Zeeb if (!V_tcp_do_sack) 2925f72167f4SAndre Oppermann continue; 2926fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 29276d90faf3SPaul Saab break; 29286d90faf3SPaul Saab case TCPOPT_SACK: 29295a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 29306d90faf3SPaul Saab continue; 2931b728e902SAndre Oppermann if (flags & TO_SYN) 2932b728e902SAndre Oppermann continue; 2933fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 29345a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 29355a53ca16SPaul Saab to->to_sacks = cp + 2; 2936603724d3SBjoern A. Zeeb V_tcpstat.tcps_sack_rcv_blocks++; 29376d90faf3SPaul Saab break; 2938be2ac88cSJonathan Lemon default: 2939be2ac88cSJonathan Lemon continue; 2940df8bae1dSRodney W. Grimes } 2941df8bae1dSRodney W. Grimes } 2942df8bae1dSRodney W. Grimes } 2943df8bae1dSRodney W. Grimes 2944df8bae1dSRodney W. Grimes /* 2945df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 2946df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 2947df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 2948df8bae1dSRodney W. Grimes * sequencing purposes. 2949df8bae1dSRodney W. Grimes */ 29500312fbe9SPoul-Henning Kamp static void 2951ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 2952ad3f9ab3SAndre Oppermann int off) 2953df8bae1dSRodney W. Grimes { 2954fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 2955df8bae1dSRodney W. Grimes 2956df8bae1dSRodney W. Grimes while (cnt >= 0) { 2957df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 2958df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 2959df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 2960df8bae1dSRodney W. Grimes 29618501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 29627a3244ccSRobert Watson 2963df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 2964df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 2965df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2966df8bae1dSRodney W. Grimes m->m_len--; 296769a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 296869a34685SYoshinobu Inoue m->m_pkthdr.len--; 2969df8bae1dSRodney W. Grimes return; 2970df8bae1dSRodney W. Grimes } 2971df8bae1dSRodney W. Grimes cnt -= m->m_len; 2972df8bae1dSRodney W. Grimes m = m->m_next; 29734dfdffe9SAndre Oppermann if (m == NULL) 2974df8bae1dSRodney W. Grimes break; 2975df8bae1dSRodney W. Grimes } 2976df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 2977df8bae1dSRodney W. Grimes } 2978df8bae1dSRodney W. Grimes 2979df8bae1dSRodney W. Grimes /* 2980df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 2981df8bae1dSRodney W. Grimes * and update averages and current timeout. 2982df8bae1dSRodney W. Grimes */ 29830312fbe9SPoul-Henning Kamp static void 2984ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 2985df8bae1dSRodney W. Grimes { 29868b615593SMarko Zec INIT_VNET_INET(tp->t_inpcb->inp_vnet); 2987ad3f9ab3SAndre Oppermann int delta; 2988233e8c18SGarrett Wollman 29898501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 29902be3bf22SRobert Watson 2991603724d3SBjoern A. Zeeb V_tcpstat.tcps_rttupdated++; 2992233e8c18SGarrett Wollman tp->t_rttupdated++; 2993233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 2994233e8c18SGarrett Wollman /* 2995233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 2996233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 2997233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 2998233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2999233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 3000233e8c18SGarrett Wollman */ 3001233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3002233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3003233e8c18SGarrett Wollman 3004233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 3005233e8c18SGarrett Wollman tp->t_srtt = 1; 3006233e8c18SGarrett Wollman 3007233e8c18SGarrett Wollman /* 3008233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 3009233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 3010233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 3011233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 3012233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 3013233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 3014233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3015233e8c18SGarrett Wollman * rfc793's wired-in beta. 3016233e8c18SGarrett Wollman */ 3017233e8c18SGarrett Wollman if (delta < 0) 3018233e8c18SGarrett Wollman delta = -delta; 3019233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3020233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 3021233e8c18SGarrett Wollman tp->t_rttvar = 1; 30221fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 30231fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3024233e8c18SGarrett Wollman } else { 3025233e8c18SGarrett Wollman /* 3026233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 3027233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 3028233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 3029233e8c18SGarrett Wollman */ 3030233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 3031233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 30321fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3033233e8c18SGarrett Wollman } 30349b8b58e0SJonathan Lemon tp->t_rtttime = 0; 3035df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 3036df8bae1dSRodney W. Grimes 3037df8bae1dSRodney W. Grimes /* 3038df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 3039df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 3040df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 3041df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 3042df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 3043df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 3044df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 3045df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 3046df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 3047df8bae1dSRodney W. Grimes */ 3048233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 30499e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3050df8bae1dSRodney W. Grimes 3051df8bae1dSRodney W. Grimes /* 3052df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 3053df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 3054df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 3055df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 3056df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 3057df8bae1dSRodney W. Grimes */ 3058df8bae1dSRodney W. Grimes tp->t_softerror = 0; 3059df8bae1dSRodney W. Grimes } 3060df8bae1dSRodney W. Grimes 3061df8bae1dSRodney W. Grimes /* 3062df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 3063df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 3064df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 3065df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 3066df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 3067df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 3068df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 3069df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 3070df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 3071df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 3072df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 3073df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 3074df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 3075a0292f23SGarrett Wollman * 3076a0292f23SGarrett Wollman * Also take into account the space needed for options that we 3077a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 3078a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 3079a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 3080a0292f23SGarrett Wollman * data in maxopd. 3081a0292f23SGarrett Wollman * 3082a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 3083a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 3084a0292f23SGarrett Wollman * MSS of our peer. 308597d8d152SAndre Oppermann * 308697d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 308797d8d152SAndre Oppermann * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt(). 3088df8bae1dSRodney W. Grimes */ 3089a0292f23SGarrett Wollman void 309091d6cfa6SBjoern A. Zeeb tcp_mss_update(struct tcpcb *tp, int offer, 309191d6cfa6SBjoern A. Zeeb struct hc_metrics_lite *metricptr, int *mtuflags) 3092df8bae1dSRodney W. Grimes { 30938b615593SMarko Zec INIT_VNET_INET(tp->t_inpcb->inp_vnet); 30943cee92e0SBjoern A. Zeeb int mss; 309597d8d152SAndre Oppermann u_long maxmtu; 3096c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 309797d8d152SAndre Oppermann struct hc_metrics_lite metrics; 3098a0292f23SGarrett Wollman int origoffer = offer; 3099fb59c426SYoshinobu Inoue #ifdef INET6 3100c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3101c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 3102c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3103c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 3104c068736aSJeffrey Hsu #else 3105c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 3106fb59c426SYoshinobu Inoue #endif 3107df8bae1dSRodney W. Grimes 31088501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 31097a3244ccSRobert Watson 3110e8949f74SAndre Oppermann /* Initialize. */ 311197d8d152SAndre Oppermann #ifdef INET6 311297d8d152SAndre Oppermann if (isipv6) { 311391d6cfa6SBjoern A. Zeeb maxmtu = tcp_maxmtu6(&inp->inp_inc, mtuflags); 3114603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_v6mssdflt; 311597d8d152SAndre Oppermann } else 311697d8d152SAndre Oppermann #endif 311797d8d152SAndre Oppermann { 311891d6cfa6SBjoern A. Zeeb maxmtu = tcp_maxmtu(&inp->inp_inc, mtuflags); 3119603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_mssdflt; 3120df8bae1dSRodney W. Grimes } 3121df8bae1dSRodney W. Grimes 312297d8d152SAndre Oppermann /* 3123e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 312497d8d152SAndre Oppermann */ 31256f01cac6SBjoern A. Zeeb if (maxmtu == 0) { 31266f01cac6SBjoern A. Zeeb /* 31278e5c87f4SBjoern A. Zeeb * In case we return early we need to initialize metrics 31286f01cac6SBjoern A. Zeeb * to a defined state as tcp_hc_get() would do for us 31296f01cac6SBjoern A. Zeeb * if there was no cache hit. 31306f01cac6SBjoern A. Zeeb */ 31316f01cac6SBjoern A. Zeeb if (metricptr != NULL) 31326f01cac6SBjoern A. Zeeb bzero(metricptr, sizeof(struct hc_metrics_lite)); 313397d8d152SAndre Oppermann return; 31346f01cac6SBjoern A. Zeeb } 313597d8d152SAndre Oppermann 3136e8949f74SAndre Oppermann /* What have we got? */ 313797d8d152SAndre Oppermann switch (offer) { 313897d8d152SAndre Oppermann case 0: 313997d8d152SAndre Oppermann /* 314097d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 3141c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 3142c3b02504SBjoern A. Zeeb * already assigned to t_maxopd above. 314397d8d152SAndre Oppermann */ 3144c3b02504SBjoern A. Zeeb offer = tp->t_maxopd; 314597d8d152SAndre Oppermann break; 314697d8d152SAndre Oppermann 314797d8d152SAndre Oppermann case -1: 3148a0292f23SGarrett Wollman /* 3149c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 3150a0292f23SGarrett Wollman */ 315197d8d152SAndre Oppermann /* FALLTHROUGH */ 315297d8d152SAndre Oppermann 315397d8d152SAndre Oppermann default: 3154a0292f23SGarrett Wollman /* 315553369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 315653369ac9SAndre Oppermann * to at least minmss. 315753369ac9SAndre Oppermann */ 3158603724d3SBjoern A. Zeeb offer = max(offer, V_tcp_minmss); 315997d8d152SAndre Oppermann } 3160a0292f23SGarrett Wollman 3161df8bae1dSRodney W. Grimes /* 3162e8949f74SAndre Oppermann * rmx information is now retrieved from tcp_hostcache. 3163df8bae1dSRodney W. Grimes */ 316497d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 31653cee92e0SBjoern A. Zeeb if (metricptr != NULL) 31663cee92e0SBjoern A. Zeeb bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 316797d8d152SAndre Oppermann 3168df8bae1dSRodney W. Grimes /* 3169e8949f74SAndre Oppermann * If there's a discovered mtu int tcp hostcache, use it 3170fb59c426SYoshinobu Inoue * else, use the link mtu. 3171df8bae1dSRodney W. Grimes */ 317297d8d152SAndre Oppermann if (metrics.rmx_mtu) 31732d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3174c068736aSJeffrey Hsu else { 317531b3783cSHajimu UMEMOTO #ifdef INET6 3176fb59c426SYoshinobu Inoue if (isipv6) { 317797d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3178603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 317997d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 3180603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_v6mssdflt); 3181b3399803SHajimu UMEMOTO } else 3182b3399803SHajimu UMEMOTO #endif 318397d8d152SAndre Oppermann { 318497d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3185603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 318697d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 3187603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_mssdflt); 3188a0292f23SGarrett Wollman } 31893cee92e0SBjoern A. Zeeb /* 31903cee92e0SBjoern A. Zeeb * XXX - The above conditional (mss = maxmtu - min_protoh) 31913cee92e0SBjoern A. Zeeb * probably violates the TCP spec. 31923cee92e0SBjoern A. Zeeb * The problem is that, since we don't know the 31933cee92e0SBjoern A. Zeeb * other end's MSS, we are supposed to use a conservative 31943cee92e0SBjoern A. Zeeb * default. But, if we do that, then MTU discovery will 31953cee92e0SBjoern A. Zeeb * never actually take place, because the conservative 31963cee92e0SBjoern A. Zeeb * default is much less than the MTUs typically seen 31973cee92e0SBjoern A. Zeeb * on the Internet today. For the moment, we'll sweep 31983cee92e0SBjoern A. Zeeb * this under the carpet. 31993cee92e0SBjoern A. Zeeb * 32003cee92e0SBjoern A. Zeeb * The conservative default might not actually be a problem 32013cee92e0SBjoern A. Zeeb * if the only case this occurs is when sending an initial 32023cee92e0SBjoern A. Zeeb * SYN with options and data to a host we've never talked 32033cee92e0SBjoern A. Zeeb * to before. Then, they will reply with an MSS value which 32043cee92e0SBjoern A. Zeeb * will get recorded and the new parameters should get 32053cee92e0SBjoern A. Zeeb * recomputed. For Further Study. 32063cee92e0SBjoern A. Zeeb */ 320797d8d152SAndre Oppermann } 3208a0292f23SGarrett Wollman mss = min(mss, offer); 320997d8d152SAndre Oppermann 3210a0292f23SGarrett Wollman /* 32113cee92e0SBjoern A. Zeeb * Sanity check: make sure that maxopd will be large 32123cee92e0SBjoern A. Zeeb * enough to allow some data on segments even if the 32133cee92e0SBjoern A. Zeeb * all the option space is used (40bytes). Otherwise 32143cee92e0SBjoern A. Zeeb * funny things may happen in tcp_output. 32153cee92e0SBjoern A. Zeeb */ 32163cee92e0SBjoern A. Zeeb mss = max(mss, 64); 32173cee92e0SBjoern A. Zeeb 32183cee92e0SBjoern A. Zeeb /* 3219a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 3220a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 3221a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 3222a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 3223a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 3224a0292f23SGarrett Wollman */ 3225a0292f23SGarrett Wollman tp->t_maxopd = mss; 3226a0292f23SGarrett Wollman 3227a0292f23SGarrett Wollman /* 3228e8949f74SAndre Oppermann * origoffer==-1 indicates that no segments were received yet. 3229c94c54e4SAndre Oppermann * In this case we just guess. 3230a0292f23SGarrett Wollman */ 3231a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 3232a0292f23SGarrett Wollman (origoffer == -1 || 3233a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 3234a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 3235a0292f23SGarrett Wollman 3236df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 3237df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 3238df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 3239df8bae1dSRodney W. Grimes #else 3240df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 3241df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 3242df8bae1dSRodney W. Grimes #endif 324397d8d152SAndre Oppermann tp->t_maxseg = mss; 32443cee92e0SBjoern A. Zeeb } 32453cee92e0SBjoern A. Zeeb 32463cee92e0SBjoern A. Zeeb void 32473cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer) 32483cee92e0SBjoern A. Zeeb { 32493cee92e0SBjoern A. Zeeb int rtt, mss; 32503cee92e0SBjoern A. Zeeb u_long bufsize; 32513cee92e0SBjoern A. Zeeb struct inpcb *inp; 32523cee92e0SBjoern A. Zeeb struct socket *so; 32533cee92e0SBjoern A. Zeeb struct hc_metrics_lite metrics; 325491d6cfa6SBjoern A. Zeeb int mtuflags = 0; 32553cee92e0SBjoern A. Zeeb #ifdef INET6 32563cee92e0SBjoern A. Zeeb int isipv6; 32573cee92e0SBjoern A. Zeeb #endif 32583cee92e0SBjoern A. Zeeb KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 325997021c24SMarko Zec INIT_VNET_INET(tp->t_vnet); 32603cee92e0SBjoern A. Zeeb 326191d6cfa6SBjoern A. Zeeb tcp_mss_update(tp, offer, &metrics, &mtuflags); 32623cee92e0SBjoern A. Zeeb 32633cee92e0SBjoern A. Zeeb mss = tp->t_maxseg; 32643cee92e0SBjoern A. Zeeb inp = tp->t_inpcb; 32653cee92e0SBjoern A. Zeeb #ifdef INET6 32663cee92e0SBjoern A. Zeeb isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 32673cee92e0SBjoern A. Zeeb #endif 326897d8d152SAndre Oppermann 3269df8bae1dSRodney W. Grimes /* 327097d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 327197d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 327297d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 327397d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 327497d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3275df8bae1dSRodney W. Grimes */ 3276c3b02504SBjoern A. Zeeb so = inp->inp_socket; 32773f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 327897d8d152SAndre Oppermann if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe) 327997d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 328097d8d152SAndre Oppermann else 3281df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3282df8bae1dSRodney W. Grimes if (bufsize < mss) 3283df8bae1dSRodney W. Grimes mss = bufsize; 3284df8bae1dSRodney W. Grimes else { 3285df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3286df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3287df8bae1dSRodney W. Grimes bufsize = sb_max; 328888c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 32893f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3290df8bae1dSRodney W. Grimes } 32913f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 3292df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 3293df8bae1dSRodney W. Grimes 32943f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 329597d8d152SAndre Oppermann if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe) 329697d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 329797d8d152SAndre Oppermann else 3298df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3299df8bae1dSRodney W. Grimes if (bufsize > mss) { 3300df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3301df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3302df8bae1dSRodney W. Grimes bufsize = sb_max; 330388c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 33043f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3305df8bae1dSRodney W. Grimes } 33063f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 3307a0292f23SGarrett Wollman /* 3308e8949f74SAndre Oppermann * While we're here, check the others too. 3309a0292f23SGarrett Wollman */ 331097d8d152SAndre Oppermann if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 331197d8d152SAndre Oppermann tp->t_srtt = rtt; 331297d8d152SAndre Oppermann tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 3313603724d3SBjoern A. Zeeb V_tcpstat.tcps_usedrtt++; 331497d8d152SAndre Oppermann if (metrics.rmx_rttvar) { 331597d8d152SAndre Oppermann tp->t_rttvar = metrics.rmx_rttvar; 3316603724d3SBjoern A. Zeeb V_tcpstat.tcps_usedrttvar++; 331797d8d152SAndre Oppermann } else { 331897d8d152SAndre Oppermann /* default variation is +- 1 rtt */ 331997d8d152SAndre Oppermann tp->t_rttvar = 332097d8d152SAndre Oppermann tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 332197d8d152SAndre Oppermann } 332297d8d152SAndre Oppermann TCPT_RANGESET(tp->t_rxtcur, 332397d8d152SAndre Oppermann ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 332497d8d152SAndre Oppermann tp->t_rttmin, TCPTV_REXMTMAX); 332597d8d152SAndre Oppermann } 332697d8d152SAndre Oppermann if (metrics.rmx_ssthresh) { 3327df8bae1dSRodney W. Grimes /* 3328df8bae1dSRodney W. Grimes * There's some sort of gateway or interface 3329df8bae1dSRodney W. Grimes * buffer limit on the path. Use this to set 3330df8bae1dSRodney W. Grimes * the slow start threshhold, but set the 3331df8bae1dSRodney W. Grimes * threshold to no less than 2*mss. 3332df8bae1dSRodney W. Grimes */ 333397d8d152SAndre Oppermann tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh); 3334603724d3SBjoern A. Zeeb V_tcpstat.tcps_usedssthresh++; 3335df8bae1dSRodney W. Grimes } 333697d8d152SAndre Oppermann if (metrics.rmx_bandwidth) 333797d8d152SAndre Oppermann tp->snd_bandwidth = metrics.rmx_bandwidth; 333897d8d152SAndre Oppermann 333997d8d152SAndre Oppermann /* 334097d8d152SAndre Oppermann * Set the slow-start flight size depending on whether this 334197d8d152SAndre Oppermann * is a local network or not. 334297d8d152SAndre Oppermann * 334397d8d152SAndre Oppermann * Extend this so we cache the cwnd too and retrieve it here. 334497d8d152SAndre Oppermann * Make cwnd even bigger than RFC3390 suggests but only if we 334597d8d152SAndre Oppermann * have previous experience with the remote host. Be careful 334697d8d152SAndre Oppermann * not make cwnd bigger than remote receive window or our own 334797d8d152SAndre Oppermann * send socket buffer. Maybe put some additional upper bound 334897d8d152SAndre Oppermann * on the retrieved cwnd. Should do incremental updates to 334997d8d152SAndre Oppermann * hostcache when cwnd collapses so next connection doesn't 335097d8d152SAndre Oppermann * overloads the path again. 335197d8d152SAndre Oppermann * 335297d8d152SAndre Oppermann * RFC3390 says only do this if SYN or SYN/ACK didn't got lost. 335397d8d152SAndre Oppermann * We currently check only in syncache_socket for that. 335497d8d152SAndre Oppermann */ 335597d8d152SAndre Oppermann #define TCP_METRICS_CWND 335697d8d152SAndre Oppermann #ifdef TCP_METRICS_CWND 335797d8d152SAndre Oppermann if (metrics.rmx_cwnd) 335897d8d152SAndre Oppermann tp->snd_cwnd = max(mss, 335997d8d152SAndre Oppermann min(metrics.rmx_cwnd / 2, 336097d8d152SAndre Oppermann min(tp->snd_wnd, so->so_snd.sb_hiwat))); 336197d8d152SAndre Oppermann else 336297d8d152SAndre Oppermann #endif 3363603724d3SBjoern A. Zeeb if (V_tcp_do_rfc3390) 336497d8d152SAndre Oppermann tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380)); 336597d8d152SAndre Oppermann #ifdef INET6 336697d8d152SAndre Oppermann else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) || 336797d8d152SAndre Oppermann (!isipv6 && in_localaddr(inp->inp_faddr))) 3368943ae302SAndre Oppermann #else 3369943ae302SAndre Oppermann else if (in_localaddr(inp->inp_faddr)) 337097d8d152SAndre Oppermann #endif 3371603724d3SBjoern A. Zeeb tp->snd_cwnd = mss * V_ss_fltsz_local; 337297d8d152SAndre Oppermann else 3373603724d3SBjoern A. Zeeb tp->snd_cwnd = mss * V_ss_fltsz; 337491d6cfa6SBjoern A. Zeeb 337591d6cfa6SBjoern A. Zeeb /* Check the interface for TSO capabilities. */ 337691d6cfa6SBjoern A. Zeeb if (mtuflags & CSUM_TSO) 337791d6cfa6SBjoern A. Zeeb tp->t_flags |= TF_TSO; 3378a0292f23SGarrett Wollman } 3379a0292f23SGarrett Wollman 3380a0292f23SGarrett Wollman /* 3381a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3382a0292f23SGarrett Wollman */ 3383a0292f23SGarrett Wollman int 3384ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 3385a0292f23SGarrett Wollman { 33868b615593SMarko Zec INIT_VNET_INET(curvnet); 338797d8d152SAndre Oppermann int mss = 0; 338897d8d152SAndre Oppermann u_long maxmtu = 0; 338997d8d152SAndre Oppermann u_long thcmtu = 0; 339097d8d152SAndre Oppermann size_t min_protoh; 3391a0292f23SGarrett Wollman 339297d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3393a0292f23SGarrett Wollman 339431b3783cSHajimu UMEMOTO #ifdef INET6 3395dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3396603724d3SBjoern A. Zeeb mss = V_tcp_v6mssdflt; 3397233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 339897d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 339997d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 340097d8d152SAndre Oppermann } else 340131b3783cSHajimu UMEMOTO #endif 340297d8d152SAndre Oppermann { 3403603724d3SBjoern A. Zeeb mss = V_tcp_mssdflt; 3404233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 340597d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 340697d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 340797d8d152SAndre Oppermann } 340897d8d152SAndre Oppermann if (maxmtu && thcmtu) 340997d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 341097d8d152SAndre Oppermann else if (maxmtu || thcmtu) 341197d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 341297d8d152SAndre Oppermann 341397d8d152SAndre Oppermann return (mss); 3414df8bae1dSRodney W. Grimes } 341546f58482SJonathan Lemon 341646f58482SJonathan Lemon 341746f58482SJonathan Lemon /* 3418c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3419c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3420c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3421c068736aSJeffrey Hsu * be started again. 342246f58482SJonathan Lemon */ 3423c068736aSJeffrey Hsu static void 3424ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 342546f58482SJonathan Lemon { 342646f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 342746f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 342846f58482SJonathan Lemon 34298501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 34307a3244ccSRobert Watson 3431b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 343246f58482SJonathan Lemon tp->t_rtttime = 0; 343346f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 34346b2a5f92SJayanth Vijayaraghavan /* 3435c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3436c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 34376b2a5f92SJayanth Vijayaraghavan */ 34386b2a5f92SJayanth Vijayaraghavan tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 3439a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 34406b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 344146f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 344246f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 344346f58482SJonathan Lemon tp->snd_nxt = onxt; 344446f58482SJonathan Lemon /* 344546f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 344646f58482SJonathan Lemon * not updated yet. 344746f58482SJonathan Lemon */ 3448d7587117SPaul Saab if (tp->snd_cwnd > th->th_ack - tp->snd_una) 3449d7587117SPaul Saab tp->snd_cwnd -= th->th_ack - tp->snd_una; 3450d7587117SPaul Saab else 3451d7587117SPaul Saab tp->snd_cwnd = 0; 3452d7587117SPaul Saab tp->snd_cwnd += tp->t_maxseg; 345346f58482SJonathan Lemon } 3454