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. 4dbc42409SLawrence Stewart * Copyright (c) 2007-2008,2010 5dbc42409SLawrence Stewart * Swinburne University of Technology, Melbourne, Australia. 6dbc42409SLawrence Stewart * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org> 7dbc42409SLawrence Stewart * Copyright (c) 2010 The FreeBSD Foundation 8fa046d87SRobert Watson * Copyright (c) 2010-2011 Juniper Networks, Inc. 9dbc42409SLawrence Stewart * All rights reserved. 10dbc42409SLawrence Stewart * 11dbc42409SLawrence Stewart * Portions of this software were developed at the Centre for Advanced Internet 12891b8ed4SLawrence Stewart * Architectures, Swinburne University of Technology, by Lawrence Stewart, 13891b8ed4SLawrence Stewart * James Healy and David Hayes, made possible in part by a grant from the Cisco 14891b8ed4SLawrence Stewart * University Research Program Fund at Community Foundation Silicon Valley. 15dbc42409SLawrence Stewart * 16dbc42409SLawrence Stewart * Portions of this software were developed at the Centre for Advanced 17dbc42409SLawrence Stewart * Internet Architectures, Swinburne University of Technology, Melbourne, 18dbc42409SLawrence Stewart * Australia by David Hayes under sponsorship from the FreeBSD Foundation. 19df8bae1dSRodney W. Grimes * 20fa046d87SRobert Watson * Portions of this software were developed by Robert N. M. Watson under 21fa046d87SRobert Watson * contract to Juniper Networks, Inc. 22fa046d87SRobert Watson * 23df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 24df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 25df8bae1dSRodney W. Grimes * are met: 26df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 27df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 28df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 29df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 30df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 31df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 32df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 33df8bae1dSRodney W. Grimes * without specific prior written permission. 34df8bae1dSRodney W. Grimes * 35df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 36df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 38df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 39df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 40df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 41df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 42df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 43df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 44df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 45df8bae1dSRodney W. Grimes * SUCH DAMAGE. 46df8bae1dSRodney W. Grimes * 47e79adb8eSGarrett Wollman * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 48df8bae1dSRodney W. Grimes */ 49df8bae1dSRodney W. Grimes 504b421e2dSMike Silbersack #include <sys/cdefs.h> 514b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 524b421e2dSMike Silbersack 531cfd4b53SBruce M Simpson #include "opt_inet.h" 54fb59c426SYoshinobu Inoue #include "opt_inet6.h" 553a2a9f79SYoshinobu Inoue #include "opt_ipsec.h" 560cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 570cc12cc5SJoerg Wunsch 58df8bae1dSRodney W. Grimes #include <sys/param.h> 5998163b98SPoul-Henning Kamp #include <sys/kernel.h> 60bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 6139bc9de5SLawrence Stewart #include <sys/hhook.h> 62bd79708dSJonathan T. Looney #endif 63df8bae1dSRodney W. Grimes #include <sys/malloc.h> 64df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 65a29f300eSGarrett Wollman #include <sys/proc.h> /* for proc0 declaration */ 66df8bae1dSRodney W. Grimes #include <sys/protosw.h> 6757f60867SMark Johnston #include <sys/sdt.h> 68960ed29cSSeigo Tanimura #include <sys/signalvar.h> 69df8bae1dSRodney W. Grimes #include <sys/socket.h> 70df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 71960ed29cSSeigo Tanimura #include <sys/sysctl.h> 72816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 73960ed29cSSeigo Tanimura #include <sys/systm.h> 74df8bae1dSRodney W. Grimes 75e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 76e79adb8eSGarrett Wollman 7712e2e970SAndre Oppermann #include <vm/uma.h> 7812e2e970SAndre Oppermann 79df8bae1dSRodney W. Grimes #include <net/if.h> 8076039bc8SGleb Smirnoff #include <net/if_var.h> 81df8bae1dSRodney W. Grimes #include <net/route.h> 82530c0060SRobert Watson #include <net/vnet.h> 83df8bae1dSRodney W. Grimes 84773673c1SAndre Oppermann #define TCPSTATES /* for logging */ 85773673c1SAndre Oppermann 86df8bae1dSRodney W. Grimes #include <netinet/in.h> 8757f60867SMark Johnston #include <netinet/in_kdtrace.h> 88960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 89df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 90df8bae1dSRodney W. Grimes #include <netinet/ip.h> 918e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 92686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 93df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 94ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 95686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 96686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h> 97686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 98b66d74c1SGleb Smirnoff #include <netinet6/in6_var.h> 99960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h> 100960ed29cSSeigo Tanimura #include <netinet6/nd6.h> 101281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 102281a0fd4SPatrick Kelsey #include <netinet/tcp_fastopen.h> 103281a0fd4SPatrick Kelsey #endif 1042de3e790SGleb Smirnoff #include <netinet/tcp.h> 105df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 106df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 107df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 108df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 109fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 110df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 1114644fda3SGleb Smirnoff #include <netinet/cc/cc.h> 11286a996e6SHiren Panchasara #ifdef TCPPCAP 11386a996e6SHiren Panchasara #include <netinet/tcp_pcap.h> 11486a996e6SHiren Panchasara #endif 115c325962bSMike Silbersack #include <netinet/tcp_syncache.h> 116610ee2f9SDavid Greenman #ifdef TCPDEBUG 117df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 118fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */ 11909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 12009fe6320SNavdeep Parhar #include <netinet/tcp_offload.h> 12109fe6320SNavdeep Parhar #endif 122fb59c426SYoshinobu Inoue 123b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 124b9234fafSSam Leffler #include <netipsec/ipsec.h> 125b9234fafSSam Leffler #include <netipsec/ipsec6.h> 126b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 127b9234fafSSam Leffler 128db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 129db4f9cc7SJonathan Lemon 130aed55708SRobert Watson #include <security/mac/mac_framework.h> 131aed55708SRobert Watson 132dbc42409SLawrence Stewart const int tcprexmtthresh = 3; 1330312fbe9SPoul-Henning Kamp 134773673c1SAndre Oppermann int tcp_log_in_vain = 0; 135816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 136eddfbb76SRobert Watson &tcp_log_in_vain, 0, 137eddfbb76SRobert Watson "Log all incoming TCP segments to closed ports"); 138816a3d83SPoul-Henning Kamp 13982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0; 14082cea7e6SBjoern A. Zeeb #define V_blackhole VNET(blackhole) 1416df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW, 142eddfbb76SRobert Watson &VNET_NAME(blackhole), 0, 143eddfbb76SRobert Watson "Do not send RST on segments to closed ports"); 14416f7f31fSGeoff Rehmet 14582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1; 1466df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW, 147eddfbb76SRobert Watson &VNET_NAME(tcp_delack_enabled), 0, 1483d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 149f498eeeeSDavid Greenman 15082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0; 15182cea7e6SBjoern A. Zeeb #define V_drop_synfin VNET(drop_synfin) 1526df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW, 153eddfbb76SRobert Watson &VNET_NAME(drop_synfin), 0, 154eddfbb76SRobert Watson "Drop TCP packets with SYN+FIN set"); 155f8613305SDag-Erling Smørgrav 15612eeb81fSHiren Panchasara VNET_DEFINE(int, tcp_do_rfc6675_pipe) = 0; 157054d38e3SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc6675_pipe, CTLFLAG_VNET | CTLFLAG_RW, 15812eeb81fSHiren Panchasara &VNET_NAME(tcp_do_rfc6675_pipe), 0, 15912eeb81fSHiren Panchasara "Use calculated pipe/in-flight bytes per RFC 6675"); 16012eeb81fSHiren Panchasara 16182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1; 16282cea7e6SBjoern A. Zeeb #define V_tcp_do_rfc3042 VNET(tcp_do_rfc3042) 1636df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW, 164eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3042), 0, 165eddfbb76SRobert Watson "Enable RFC 3042 (Limited Transmit)"); 166582a954bSJeffrey Hsu 16782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1; 1686df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW, 169eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3390), 0, 170da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 171da3a8a1aSJeffrey Hsu 172356c7958SHiren Panchasara VNET_DEFINE(int, tcp_initcwnd_segments) = 10; 173356c7958SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments, 174356c7958SHiren Panchasara CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0, 175356c7958SHiren Panchasara "Slow-start flight size (initial congestion window) in number of segments"); 17609440655SAndre Oppermann 17782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1; 1786df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW, 179eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3465), 0, 18024cb0f22SLawrence Stewart "Enable RFC 3465 (Appropriate Byte Counting)"); 181eddfbb76SRobert Watson 18282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2; 1836df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW, 184eddfbb76SRobert Watson &VNET_NAME(tcp_abc_l_var), 2, 18524cb0f22SLawrence Stewart "Cap the max cwnd increment during slow-start to this number of segments"); 18624cb0f22SLawrence Stewart 1876472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN"); 188f2512ba1SRui Paulo 189883054b4SDon Lewis VNET_DEFINE(int, tcp_do_ecn) = 2; 1906df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW, 191eddfbb76SRobert Watson &VNET_NAME(tcp_do_ecn), 0, 192eddfbb76SRobert Watson "TCP ECN support"); 193eddfbb76SRobert Watson 19482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1; 1956df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_VNET | CTLFLAG_RW, 196eddfbb76SRobert Watson &VNET_NAME(tcp_ecn_maxretries), 0, 197eddfbb76SRobert Watson "Max retries before giving up on ECN"); 198eddfbb76SRobert Watson 1993220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0; 2003220a212SGleb Smirnoff #define V_tcp_insecure_syn VNET(tcp_insecure_syn) 2016df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW, 2023220a212SGleb Smirnoff &VNET_NAME(tcp_insecure_syn), 0, 2033220a212SGleb Smirnoff "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets"); 2043220a212SGleb Smirnoff 20582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0; 20682cea7e6SBjoern A. Zeeb #define V_tcp_insecure_rst VNET(tcp_insecure_rst) 2076df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW, 208eddfbb76SRobert Watson &VNET_NAME(tcp_insecure_rst), 0, 2093220a212SGleb Smirnoff "Follow RFC793 instead of RFC5961 criteria for accepting RST packets"); 210a69968eeSMike Silbersack 211fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64; 212873789cbSAndre Oppermann #define V_tcp_recvspace VNET(tcp_recvspace) 2136df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW, 214873789cbSAndre Oppermann &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size"); 215873789cbSAndre Oppermann 21682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1; 21782cea7e6SBjoern A. Zeeb #define V_tcp_do_autorcvbuf VNET(tcp_do_autorcvbuf) 2186df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW, 219eddfbb76SRobert Watson &VNET_NAME(tcp_do_autorcvbuf), 0, 2208b615593SMarko Zec "Enable automatic receive buffer sizing"); 2216741ecf5SAndre Oppermann 22282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024; 22382cea7e6SBjoern A. Zeeb #define V_tcp_autorcvbuf_inc VNET(tcp_autorcvbuf_inc) 2246df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_VNET | CTLFLAG_RW, 225eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_inc), 0, 2266489fe65SAndre Oppermann "Incrementor step size of automatic receive buffer"); 2276741ecf5SAndre Oppermann 228b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024; 22982cea7e6SBjoern A. Zeeb #define V_tcp_autorcvbuf_max VNET(tcp_autorcvbuf_max) 2306df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW, 231eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_max), 0, 2328b615593SMarko Zec "Max size of automatic receive buffer"); 2336741ecf5SAndre Oppermann 234eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb); 23544e33a07SMarko Zec #define tcb6 tcb /* for KAME src sync over BSD*'s */ 23682cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo); 237df8bae1dSRodney W. Grimes 238315e3e38SRobert Watson /* 239bf840a17SGleb Smirnoff * TCP statistics are stored in an array of counter(9)s, which size matches 240bf840a17SGleb Smirnoff * size of struct tcpstat. TCP running connection count is a regular array. 2415923c293SGleb Smirnoff */ 2425da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat); 2435da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat, 2445da0521fSAndrey V. Elsukov tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 245bf840a17SGleb Smirnoff VNET_DEFINE(counter_u64_t, tcps_states[TCP_NSTATES]); 246bf840a17SGleb Smirnoff SYSCTL_COUNTER_U64_ARRAY(_net_inet_tcp, TCPCTL_STATES, states, CTLFLAG_RD | 247d4d32b9fSHans Petter Selasky CTLFLAG_VNET, &VNET_NAME(tcps_states)[0], TCP_NSTATES, 248bf840a17SGleb Smirnoff "TCP connection counts by TCP state"); 249bf840a17SGleb Smirnoff 250bf840a17SGleb Smirnoff static void 251bf840a17SGleb Smirnoff tcp_vnet_init(const void *unused) 252bf840a17SGleb Smirnoff { 253bf840a17SGleb Smirnoff 254f59d975eSGleb Smirnoff COUNTER_ARRAY_ALLOC(V_tcps_states, TCP_NSTATES, M_WAITOK); 255bf840a17SGleb Smirnoff VNET_PCPUSTAT_ALLOC(tcpstat, M_WAITOK); 256bf840a17SGleb Smirnoff } 257bf840a17SGleb Smirnoff VNET_SYSINIT(tcp_vnet_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 258bf840a17SGleb Smirnoff tcp_vnet_init, NULL); 2595923c293SGleb Smirnoff 2605923c293SGleb Smirnoff #ifdef VIMAGE 261bf840a17SGleb Smirnoff static void 262bf840a17SGleb Smirnoff tcp_vnet_uninit(const void *unused) 263bf840a17SGleb Smirnoff { 264bf840a17SGleb Smirnoff 265f59d975eSGleb Smirnoff COUNTER_ARRAY_FREE(V_tcps_states, TCP_NSTATES); 266bf840a17SGleb Smirnoff VNET_PCPUSTAT_FREE(tcpstat); 267bf840a17SGleb Smirnoff } 268bf840a17SGleb Smirnoff VNET_SYSUNINIT(tcp_vnet_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 269bf840a17SGleb Smirnoff tcp_vnet_uninit, NULL); 2705923c293SGleb Smirnoff #endif /* VIMAGE */ 271bf840a17SGleb Smirnoff 2725923c293SGleb Smirnoff /* 273315e3e38SRobert Watson * Kernel module interface for updating tcpstat. The argument is an index 2745923c293SGleb Smirnoff * into tcpstat treated as an array. 275315e3e38SRobert Watson */ 276315e3e38SRobert Watson void 277315e3e38SRobert Watson kmod_tcpstat_inc(int statnum) 278315e3e38SRobert Watson { 279315e3e38SRobert Watson 2805da0521fSAndrey V. Elsukov counter_u64_add(VNET(tcpstat)[statnum], 1); 281315e3e38SRobert Watson } 282315e3e38SRobert Watson 283bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 284dbc42409SLawrence Stewart /* 28539bc9de5SLawrence Stewart * Wrapper for the TCP established input helper hook. 28639bc9de5SLawrence Stewart */ 28755bceb1eSRandall Stewart void 28839bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to) 28939bc9de5SLawrence Stewart { 29039bc9de5SLawrence Stewart struct tcp_hhook_data hhook_data; 29139bc9de5SLawrence Stewart 29239bc9de5SLawrence Stewart if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) { 29339bc9de5SLawrence Stewart hhook_data.tp = tp; 29439bc9de5SLawrence Stewart hhook_data.th = th; 29539bc9de5SLawrence Stewart hhook_data.to = to; 29639bc9de5SLawrence Stewart 29739bc9de5SLawrence Stewart hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data, 29839bc9de5SLawrence Stewart tp->osd); 29939bc9de5SLawrence Stewart } 30039bc9de5SLawrence Stewart } 301bd79708dSJonathan T. Looney #endif 30239bc9de5SLawrence Stewart 30339bc9de5SLawrence Stewart /* 304dbc42409SLawrence Stewart * CC wrapper hook functions 305dbc42409SLawrence Stewart */ 30655bceb1eSRandall Stewart void 3074b7b743cSLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t nsegs, 3084b7b743cSLawrence Stewart uint16_t type) 309f2512ba1SRui Paulo { 310dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 311f2512ba1SRui Paulo 3124b7b743cSLawrence Stewart tp->ccv->nsegs = nsegs; 313dbc42409SLawrence Stewart tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th); 3145b648e79SLawrence Stewart if (tp->snd_cwnd <= tp->snd_wnd) 315dbc42409SLawrence Stewart tp->ccv->flags |= CCF_CWND_LIMITED; 316dbc42409SLawrence Stewart else 317dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_CWND_LIMITED; 318dbc42409SLawrence Stewart 319dbc42409SLawrence Stewart if (type == CC_ACK) { 320dbc42409SLawrence Stewart if (tp->snd_cwnd > tp->snd_ssthresh) { 321dbc42409SLawrence Stewart tp->t_bytes_acked += min(tp->ccv->bytes_this_ack, 3224b7b743cSLawrence Stewart nsegs * V_tcp_abc_l_var * tcp_maxseg(tp)); 323dbc42409SLawrence Stewart if (tp->t_bytes_acked >= tp->snd_cwnd) { 324dbc42409SLawrence Stewart tp->t_bytes_acked -= tp->snd_cwnd; 325dbc42409SLawrence Stewart tp->ccv->flags |= CCF_ABC_SENTAWND; 326dbc42409SLawrence Stewart } 327dbc42409SLawrence Stewart } else { 328dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_ABC_SENTAWND; 329dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 330dbc42409SLawrence Stewart } 331dbc42409SLawrence Stewart } 332dbc42409SLawrence Stewart 333dbc42409SLawrence Stewart if (CC_ALGO(tp)->ack_received != NULL) { 334dbc42409SLawrence Stewart /* XXXLAS: Find a way to live without this */ 335dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 336dbc42409SLawrence Stewart CC_ALGO(tp)->ack_received(tp->ccv, type); 337dbc42409SLawrence Stewart } 338dbc42409SLawrence Stewart } 339dbc42409SLawrence Stewart 34055bceb1eSRandall Stewart void 341dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp) 342dbc42409SLawrence Stewart { 343dbc42409SLawrence Stewart struct hc_metrics_lite metrics; 344dbc42409SLawrence Stewart struct inpcb *inp = tp->t_inpcb; 3450c39d38dSGleb Smirnoff u_int maxseg; 346dbc42409SLawrence Stewart int rtt; 347dbc42409SLawrence Stewart 348dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 349dbc42409SLawrence Stewart 350dbc42409SLawrence Stewart tcp_hc_get(&inp->inp_inc, &metrics); 3510c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 352dbc42409SLawrence Stewart 353dbc42409SLawrence Stewart if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 354dbc42409SLawrence Stewart tp->t_srtt = rtt; 355dbc42409SLawrence Stewart tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 356dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrtt); 357dbc42409SLawrence Stewart if (metrics.rmx_rttvar) { 358dbc42409SLawrence Stewart tp->t_rttvar = metrics.rmx_rttvar; 359dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrttvar); 360dbc42409SLawrence Stewart } else { 361dbc42409SLawrence Stewart /* default variation is +- 1 rtt */ 362dbc42409SLawrence Stewart tp->t_rttvar = 363dbc42409SLawrence Stewart tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 364dbc42409SLawrence Stewart } 365dbc42409SLawrence Stewart TCPT_RANGESET(tp->t_rxtcur, 366dbc42409SLawrence Stewart ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 367dbc42409SLawrence Stewart tp->t_rttmin, TCPTV_REXMTMAX); 368dbc42409SLawrence Stewart } 369dbc42409SLawrence Stewart if (metrics.rmx_ssthresh) { 370dbc42409SLawrence Stewart /* 371dbc42409SLawrence Stewart * There's some sort of gateway or interface 372dbc42409SLawrence Stewart * buffer limit on the path. Use this to set 373a4641f4eSPedro F. Giffuni * the slow start threshold, but set the 374dbc42409SLawrence Stewart * threshold to no less than 2*mss. 375dbc42409SLawrence Stewart */ 3760c39d38dSGleb Smirnoff tp->snd_ssthresh = max(2 * maxseg, metrics.rmx_ssthresh); 377dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedssthresh); 378dbc42409SLawrence Stewart } 379dbc42409SLawrence Stewart 380dbc42409SLawrence Stewart /* 3819ec4a4ccSAndre Oppermann * Set the initial slow-start flight size. 382dbc42409SLawrence Stewart * 383cf8f04f4SAndre Oppermann * RFC5681 Section 3.1 specifies the default conservative values. 384cf8f04f4SAndre Oppermann * RFC3390 specifies slightly more aggressive values. 385b8b4cfcdSSergey Kandaurov * RFC6928 increases it to ten segments. 386356c7958SHiren Panchasara * Support for user specified value for initial flight size. 387cf8f04f4SAndre Oppermann * 388cf8f04f4SAndre Oppermann * If a SYN or SYN/ACK was lost and retransmitted, we have to 389cf8f04f4SAndre Oppermann * reduce the initial CWND to one segment as congestion is likely 390cf8f04f4SAndre Oppermann * requiring us to be cautious. 391dbc42409SLawrence Stewart */ 392cf8f04f4SAndre Oppermann if (tp->snd_cwnd == 1) 3930c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; /* SYN(-ACK) lost */ 394356c7958SHiren Panchasara else if (V_tcp_initcwnd_segments) 3950c39d38dSGleb Smirnoff tp->snd_cwnd = min(V_tcp_initcwnd_segments * maxseg, 3960c39d38dSGleb Smirnoff max(2 * maxseg, V_tcp_initcwnd_segments * 1460)); 397cf8f04f4SAndre Oppermann else if (V_tcp_do_rfc3390) 3980c39d38dSGleb Smirnoff tp->snd_cwnd = min(4 * maxseg, max(2 * maxseg, 4380)); 39922efabd4SAndre Oppermann else { 40022efabd4SAndre Oppermann /* Per RFC5681 Section 3.1 */ 4010c39d38dSGleb Smirnoff if (maxseg > 2190) 4020c39d38dSGleb Smirnoff tp->snd_cwnd = 2 * maxseg; 4030c39d38dSGleb Smirnoff else if (maxseg > 1095) 4040c39d38dSGleb Smirnoff tp->snd_cwnd = 3 * maxseg; 405dbc42409SLawrence Stewart else 4060c39d38dSGleb Smirnoff tp->snd_cwnd = 4 * maxseg; 40722efabd4SAndre Oppermann } 408dbc42409SLawrence Stewart 409dbc42409SLawrence Stewart if (CC_ALGO(tp)->conn_init != NULL) 410dbc42409SLawrence Stewart CC_ALGO(tp)->conn_init(tp->ccv); 411dbc42409SLawrence Stewart } 412dbc42409SLawrence Stewart 413dbc42409SLawrence Stewart void inline 414dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type) 415dbc42409SLawrence Stewart { 4160c39d38dSGleb Smirnoff u_int maxseg; 4170c39d38dSGleb Smirnoff 418dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 419dbc42409SLawrence Stewart 420dbc42409SLawrence Stewart switch(type) { 421dbc42409SLawrence Stewart case CC_NDUPACK: 422dbc42409SLawrence Stewart if (!IN_FASTRECOVERY(tp->t_flags)) { 423f2512ba1SRui Paulo tp->snd_recover = tp->snd_max; 424f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) 425f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_CWR; 426f2512ba1SRui Paulo } 427dbc42409SLawrence Stewart break; 428dbc42409SLawrence Stewart case CC_ECN: 429dbc42409SLawrence Stewart if (!IN_CONGRECOVERY(tp->t_flags)) { 430dbc42409SLawrence Stewart TCPSTAT_INC(tcps_ecn_rcwnd); 431dbc42409SLawrence Stewart tp->snd_recover = tp->snd_max; 432dbc42409SLawrence Stewart if (tp->t_flags & TF_ECN_PERMIT) 433dbc42409SLawrence Stewart tp->t_flags |= TF_ECN_SND_CWR; 434dbc42409SLawrence Stewart } 435dbc42409SLawrence Stewart break; 436dbc42409SLawrence Stewart case CC_RTO: 4370c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 438dbc42409SLawrence Stewart tp->t_dupacks = 0; 439dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 440dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 4414e7f7553SHiren Panchasara if (CC_ALGO(tp)->cong_signal == NULL) { 4424e7f7553SHiren Panchasara /* 4434e7f7553SHiren Panchasara * RFC5681 Section 3.1 4444e7f7553SHiren Panchasara * ssthresh = max (FlightSize / 2, 2*SMSS) eq (4) 4454e7f7553SHiren Panchasara */ 4464e7f7553SHiren Panchasara tp->snd_ssthresh = 447e04310d5SHiren Panchasara max((tp->snd_max - tp->snd_una) / 2 / maxseg, 2) 448e04310d5SHiren Panchasara * maxseg; 4490c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 4504e7f7553SHiren Panchasara } 451dbc42409SLawrence Stewart break; 452dbc42409SLawrence Stewart case CC_RTO_ERR: 453dbc42409SLawrence Stewart TCPSTAT_INC(tcps_sndrexmitbad); 454dbc42409SLawrence Stewart /* RTO was unnecessary, so reset everything. */ 455dbc42409SLawrence Stewart tp->snd_cwnd = tp->snd_cwnd_prev; 456dbc42409SLawrence Stewart tp->snd_ssthresh = tp->snd_ssthresh_prev; 457dbc42409SLawrence Stewart tp->snd_recover = tp->snd_recover_prev; 458dbc42409SLawrence Stewart if (tp->t_flags & TF_WASFRECOVERY) 459dbc42409SLawrence Stewart ENTER_FASTRECOVERY(tp->t_flags); 460dbc42409SLawrence Stewart if (tp->t_flags & TF_WASCRECOVERY) 461dbc42409SLawrence Stewart ENTER_CONGRECOVERY(tp->t_flags); 462dbc42409SLawrence Stewart tp->snd_nxt = tp->snd_max; 463672dc4aeSJohn Baldwin tp->t_flags &= ~TF_PREVVALID; 464dbc42409SLawrence Stewart tp->t_badrxtwin = 0; 465dbc42409SLawrence Stewart break; 466dbc42409SLawrence Stewart } 467dbc42409SLawrence Stewart 468dbc42409SLawrence Stewart if (CC_ALGO(tp)->cong_signal != NULL) { 469dbc42409SLawrence Stewart if (th != NULL) 470dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 471dbc42409SLawrence Stewart CC_ALGO(tp)->cong_signal(tp->ccv, type); 472dbc42409SLawrence Stewart } 473dbc42409SLawrence Stewart } 474dbc42409SLawrence Stewart 47555bceb1eSRandall Stewart void inline 476dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th) 477dbc42409SLawrence Stewart { 478dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 479dbc42409SLawrence Stewart 480dbc42409SLawrence Stewart /* XXXLAS: KASSERT that we're in recovery? */ 481dbc42409SLawrence Stewart 482dbc42409SLawrence Stewart if (CC_ALGO(tp)->post_recovery != NULL) { 483dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 484dbc42409SLawrence Stewart CC_ALGO(tp)->post_recovery(tp->ccv); 485dbc42409SLawrence Stewart } 486dbc42409SLawrence Stewart /* XXXLAS: EXIT_RECOVERY ? */ 487dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 488dbc42409SLawrence Stewart } 4890312fbe9SPoul-Henning Kamp 4902903309aSAttilio Rao #ifdef TCP_SIGNATURE 4912903309aSAttilio Rao static inline int 4922903309aSAttilio Rao tcp_signature_verify_input(struct mbuf *m, int off0, int tlen, int optlen, 4932903309aSAttilio Rao struct tcpopt *to, struct tcphdr *th, u_int tcpbflag) 4942903309aSAttilio Rao { 4952903309aSAttilio Rao int ret; 4962903309aSAttilio Rao 4972903309aSAttilio Rao tcp_fields_to_net(th); 4982903309aSAttilio Rao ret = tcp_signature_verify(m, off0, tlen, optlen, to, th, tcpbflag); 4992903309aSAttilio Rao tcp_fields_to_host(th); 5002903309aSAttilio Rao return (ret); 5012903309aSAttilio Rao } 5022903309aSAttilio Rao #endif 5032903309aSAttilio Rao 504df8bae1dSRodney W. Grimes /* 505262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 50685536381SHiren Panchasara * following conditions are met: 50785536381SHiren Panchasara * - There is no delayed ack timer in progress. 50885536381SHiren Panchasara * - Our last ack wasn't a 0-sized window. We never want to delay 50985536381SHiren Panchasara * the ack that opens up a 0-sized window. 51085536381SHiren Panchasara * - LRO wasn't used for this segment. We make sure by checking that the 51185536381SHiren Panchasara * segment size is not larger than the MSS. 512d8c85a26SJonathan Lemon */ 513c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen) \ 514b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 515a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 5160c39d38dSGleb Smirnoff (tlen <= tp->t_maxseg) && \ 517603724d3SBjoern A. Zeeb (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 518d8c85a26SJonathan Lemon 51964807b30SHiren Panchasara static void inline 52064807b30SHiren Panchasara cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos) 52164807b30SHiren Panchasara { 52264807b30SHiren Panchasara INP_WLOCK_ASSERT(tp->t_inpcb); 52364807b30SHiren Panchasara 52464807b30SHiren Panchasara if (CC_ALGO(tp)->ecnpkt_handler != NULL) { 52564807b30SHiren Panchasara switch (iptos & IPTOS_ECN_MASK) { 52664807b30SHiren Panchasara case IPTOS_ECN_CE: 52764807b30SHiren Panchasara tp->ccv->flags |= CCF_IPHDR_CE; 52864807b30SHiren Panchasara break; 52964807b30SHiren Panchasara case IPTOS_ECN_ECT0: 53064807b30SHiren Panchasara tp->ccv->flags &= ~CCF_IPHDR_CE; 53164807b30SHiren Panchasara break; 53264807b30SHiren Panchasara case IPTOS_ECN_ECT1: 53364807b30SHiren Panchasara tp->ccv->flags &= ~CCF_IPHDR_CE; 53464807b30SHiren Panchasara break; 53564807b30SHiren Panchasara } 53664807b30SHiren Panchasara 53764807b30SHiren Panchasara if (th->th_flags & TH_CWR) 53864807b30SHiren Panchasara tp->ccv->flags |= CCF_TCPHDR_CWR; 53964807b30SHiren Panchasara else 54064807b30SHiren Panchasara tp->ccv->flags &= ~CCF_TCPHDR_CWR; 54164807b30SHiren Panchasara 54264807b30SHiren Panchasara if (tp->t_flags & TF_DELACK) 54364807b30SHiren Panchasara tp->ccv->flags |= CCF_DELACK; 54464807b30SHiren Panchasara else 54564807b30SHiren Panchasara tp->ccv->flags &= ~CCF_DELACK; 54664807b30SHiren Panchasara 54764807b30SHiren Panchasara CC_ALGO(tp)->ecnpkt_handler(tp->ccv); 54864807b30SHiren Panchasara 54964807b30SHiren Panchasara if (tp->ccv->flags & CCF_ACKNOW) 55064807b30SHiren Panchasara tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 55164807b30SHiren Panchasara } 55264807b30SHiren Panchasara } 55364807b30SHiren Panchasara 554df8bae1dSRodney W. Grimes /* 5558d573cc1SAndre Oppermann * TCP input handling is split into multiple parts: 5568d573cc1SAndre Oppermann * tcp6_input is a thin wrapper around tcp_input for the extended 5578d573cc1SAndre Oppermann * ip6_protox[] call format in ip6_input 5588d573cc1SAndre Oppermann * tcp_input handles primary segment validation, inpcb lookup and 5598d573cc1SAndre Oppermann * SYN processing on listen sockets 5608d573cc1SAndre Oppermann * tcp_do_segment processes the ACK and text of the segment for 5618d573cc1SAndre Oppermann * establishing, established and closing connections 562df8bae1dSRodney W. Grimes */ 563fb59c426SYoshinobu Inoue #ifdef INET6 564fb59c426SYoshinobu Inoue int 565ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto) 566fb59c426SYoshinobu Inoue { 567ad3f9ab3SAndre Oppermann struct mbuf *m = *mp; 56833841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 5693e88eb90SAndrey V. Elsukov struct ip6_hdr *ip6; 570fb59c426SYoshinobu Inoue 571fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 572fb59c426SYoshinobu Inoue 573fb59c426SYoshinobu Inoue /* 574fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 575fb59c426SYoshinobu Inoue * better place to put this in? 576fb59c426SYoshinobu Inoue */ 5773e88eb90SAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 5783e88eb90SAndrey V. Elsukov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */); 57933841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 580fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 581fb59c426SYoshinobu Inoue 5828c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 583fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 584fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 585fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 5868f5a8818SKevin Lo return (IPPROTO_DONE); 587fb59c426SYoshinobu Inoue } 58877d396fdSMaksim Yevmenkin if (ia6) 58977d396fdSMaksim Yevmenkin ifa_free(&ia6->ia_ifa); 590fb59c426SYoshinobu Inoue 5918f5a8818SKevin Lo return (tcp_input(mp, offp, proto)); 592fb59c426SYoshinobu Inoue } 593b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 594fb59c426SYoshinobu Inoue 5958f5a8818SKevin Lo int 5968f5a8818SKevin Lo tcp_input(struct mbuf **mp, int *offp, int proto) 597df8bae1dSRodney W. Grimes { 5988f5a8818SKevin Lo struct mbuf *m = *mp; 599b287c6c7SBjoern A. Zeeb struct tcphdr *th = NULL; 600ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 601ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 602302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 603302ce8d6SAndre Oppermann struct socket *so = NULL; 604e79adb8eSGarrett Wollman u_char *optp = NULL; 6058f5a8818SKevin Lo int off0; 60626f9a767SRodney W. Grimes int optlen = 0; 607b287c6c7SBjoern A. Zeeb #ifdef INET 608b287c6c7SBjoern A. Zeeb int len; 609b287c6c7SBjoern A. Zeeb #endif 610b287c6c7SBjoern A. Zeeb int tlen = 0, off; 611fb59c426SYoshinobu Inoue int drop_hdrlen; 612ad3f9ab3SAndre Oppermann int thflags; 613302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 6142903309aSAttilio Rao #ifdef TCP_SIGNATURE 6152903309aSAttilio Rao uint8_t sig_checked = 0; 6162903309aSAttilio Rao #endif 6171d7ee746SKurt Lidl uint8_t iptos; 618c1de64a4SAndrey V. Elsukov struct m_tag *fwd_tag = NULL; 619c068736aSJeffrey Hsu #ifdef INET6 620302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 621c068736aSJeffrey Hsu int isipv6; 622c068736aSJeffrey Hsu #else 623a160e630SAndre Oppermann const void *ip6 = NULL; 624b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 625302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 626a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 627252ca428SRobert Watson int ti_locked; 628610ee2f9SDavid Greenman #ifdef TCPDEBUG 629c068736aSJeffrey Hsu /* 630c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 631c068736aSJeffrey Hsu * now IPv6. 632c068736aSJeffrey Hsu */ 63314739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 634410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 635610ee2f9SDavid Greenman short ostate = 0; 636610ee2f9SDavid Greenman #endif 637c068736aSJeffrey Hsu 638fb59c426SYoshinobu Inoue #ifdef INET6 639fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 640fb59c426SYoshinobu Inoue #endif 641a0292f23SGarrett Wollman 6428f5a8818SKevin Lo off0 = *offp; 6438f5a8818SKevin Lo m = *mp; 6448f5a8818SKevin Lo *mp = NULL; 645302ce8d6SAndre Oppermann to.to_flags = 0; 64678b50714SRobert Watson TCPSTAT_INC(tcps_rcvtotal); 647fb59c426SYoshinobu Inoue 64804d3a452SHajimu UMEMOTO #ifdef INET6 649b287c6c7SBjoern A. Zeeb if (isipv6) { 6508d573cc1SAndre Oppermann /* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */ 65145747ba5SBjoern A. Zeeb 65245747ba5SBjoern A. Zeeb if (m->m_len < (sizeof(*ip6) + sizeof(*th))) { 65345747ba5SBjoern A. Zeeb m = m_pullup(m, sizeof(*ip6) + sizeof(*th)); 65445747ba5SBjoern A. Zeeb if (m == NULL) { 65545747ba5SBjoern A. Zeeb TCPSTAT_INC(tcps_rcvshort); 6568f5a8818SKevin Lo return (IPPROTO_DONE); 65745747ba5SBjoern A. Zeeb } 65845747ba5SBjoern A. Zeeb } 65945747ba5SBjoern A. Zeeb 660fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 66145747ba5SBjoern A. Zeeb th = (struct tcphdr *)((caddr_t)ip6 + off0); 662fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 663356ab07eSBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) { 66445747ba5SBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 66545747ba5SBjoern A. Zeeb th->th_sum = m->m_pkthdr.csum_data; 66645747ba5SBjoern A. Zeeb else 66745747ba5SBjoern A. Zeeb th->th_sum = in6_cksum_pseudo(ip6, tlen, 66845747ba5SBjoern A. Zeeb IPPROTO_TCP, m->m_pkthdr.csum_data); 66945747ba5SBjoern A. Zeeb th->th_sum ^= 0xffff; 67045747ba5SBjoern A. Zeeb } else 67145747ba5SBjoern A. Zeeb th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen); 67245747ba5SBjoern A. Zeeb if (th->th_sum) { 67378b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 674fb59c426SYoshinobu Inoue goto drop; 675fb59c426SYoshinobu Inoue } 67633841545SHajimu UMEMOTO 67733841545SHajimu UMEMOTO /* 67833841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 67933841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 68033841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 68133841545SHajimu UMEMOTO * 68233841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 68333841545SHajimu UMEMOTO * already dropped in ip6_input. 68433841545SHajimu UMEMOTO */ 68533841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 68633841545SHajimu UMEMOTO /* XXX stat */ 68733841545SHajimu UMEMOTO goto drop; 68833841545SHajimu UMEMOTO } 6891d7ee746SKurt Lidl iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 690b287c6c7SBjoern A. Zeeb } 69104d3a452SHajimu UMEMOTO #endif 692b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 693b287c6c7SBjoern A. Zeeb else 694b287c6c7SBjoern A. Zeeb #endif 695b287c6c7SBjoern A. Zeeb #ifdef INET 696b287c6c7SBjoern A. Zeeb { 697df8bae1dSRodney W. Grimes /* 698df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 699df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 700df8bae1dSRodney W. Grimes */ 701fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 702105bd211SGleb Smirnoff ip_stripoptions(m); 703fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 704fb59c426SYoshinobu Inoue } 705df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 7064dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 7074dfdffe9SAndre Oppermann == NULL) { 70878b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 7098f5a8818SKevin Lo return (IPPROTO_DONE); 710df8bae1dSRodney W. Grimes } 711df8bae1dSRodney W. Grimes } 712fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 713db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 7148ad458a4SGleb Smirnoff tlen = ntohs(ip->ip_len) - off0; 715df8bae1dSRodney W. Grimes 7161d7ee746SKurt Lidl iptos = ip->ip_tos; 717db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 718db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 719db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 720db4f9cc7SJonathan Lemon else 721db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 722c068736aSJeffrey Hsu ip->ip_dst.s_addr, 7238f134647SGleb Smirnoff htonl(m->m_pkthdr.csum_data + tlen + 724c068736aSJeffrey Hsu IPPROTO_TCP)); 725db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 726db4f9cc7SJonathan Lemon } else { 7278f134647SGleb Smirnoff struct ipovly *ipov = (struct ipovly *)ip; 7288f134647SGleb Smirnoff 729df8bae1dSRodney W. Grimes /* 730df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 731df8bae1dSRodney W. Grimes */ 7328f134647SGleb Smirnoff len = off0 + tlen; 733fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 7348f134647SGleb Smirnoff ipov->ih_len = htons(tlen); 735fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 73657f60867SMark Johnston /* Reset length for SDT probes. */ 7371d7ee746SKurt Lidl ip->ip_len = htons(len); 7381d7ee746SKurt Lidl /* Reset TOS bits */ 7391d7ee746SKurt Lidl ip->ip_tos = iptos; 7401d7ee746SKurt Lidl /* Re-initialization for later version check */ 7411d7ee746SKurt Lidl ip->ip_v = IPVERSION; 742db4f9cc7SJonathan Lemon } 74357f60867SMark Johnston 744fb59c426SYoshinobu Inoue if (th->th_sum) { 74578b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 746df8bae1dSRodney W. Grimes goto drop; 747df8bae1dSRodney W. Grimes } 748fb59c426SYoshinobu Inoue } 749b287c6c7SBjoern A. Zeeb #endif /* INET */ 750df8bae1dSRodney W. Grimes 751df8bae1dSRodney W. Grimes /* 752df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 753df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 754df8bae1dSRodney W. Grimes */ 755fb59c426SYoshinobu Inoue off = th->th_off << 2; 756df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 75778b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadoff); 758df8bae1dSRodney W. Grimes goto drop; 759df8bae1dSRodney W. Grimes } 760fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 761df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 76204d3a452SHajimu UMEMOTO #ifdef INET6 763b287c6c7SBjoern A. Zeeb if (isipv6) { 7648f5a8818SKevin Lo IP6_EXTHDR_CHECK(m, off0, off, IPPROTO_DONE); 765fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 766fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 767b287c6c7SBjoern A. Zeeb } 76804d3a452SHajimu UMEMOTO #endif 769b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 770b287c6c7SBjoern A. Zeeb else 771b287c6c7SBjoern A. Zeeb #endif 772b287c6c7SBjoern A. Zeeb #ifdef INET 773b287c6c7SBjoern A. Zeeb { 774df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 775c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 7764dfdffe9SAndre Oppermann == NULL) { 77778b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 7788f5a8818SKevin Lo return (IPPROTO_DONE); 779df8bae1dSRodney W. Grimes } 780fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 781fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 782fb59c426SYoshinobu Inoue } 783df8bae1dSRodney W. Grimes } 784b287c6c7SBjoern A. Zeeb #endif 785df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 786fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 787df8bae1dSRodney W. Grimes } 788fb59c426SYoshinobu Inoue thflags = th->th_flags; 789df8bae1dSRodney W. Grimes 790e46cd3d4SDag-Erling Smørgrav /* 791df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 792df8bae1dSRodney W. Grimes */ 7932903309aSAttilio Rao tcp_fields_to_host(th); 794df8bae1dSRodney W. Grimes 795df8bae1dSRodney W. Grimes /* 796794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 797755c1f07SAndras Olah */ 798fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 799755c1f07SAndras Olah 800755c1f07SAndras Olah /* 801fa046d87SRobert Watson * Locate pcb for segment; if we're likely to add or remove a 802a7c7f2a7SJohn Baldwin * connection then first acquire pcbinfo lock. There are three cases 803fa046d87SRobert Watson * where we might discover later we need a write lock despite the 804a7c7f2a7SJohn Baldwin * flags: ACKs moving a connection out of the syncache, ACKs for a 805a7c7f2a7SJohn Baldwin * connection in TIMEWAIT and SYNs not targeting a listening socket. 806df8bae1dSRodney W. Grimes */ 807a7c7f2a7SJohn Baldwin if ((thflags & (TH_FIN | TH_RST)) != 0) { 808ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 809ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 810fa046d87SRobert Watson } else 811fa046d87SRobert Watson ti_locked = TI_UNLOCKED; 812252ca428SRobert Watson 8138d573cc1SAndre Oppermann /* 8148d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 8158d573cc1SAndre Oppermann */ 816b8056faeSGleb Smirnoff if ( 817b8056faeSGleb Smirnoff #ifdef INET6 818b8056faeSGleb Smirnoff (isipv6 && (m->m_flags & M_IP6_NEXTHOP)) 819b8056faeSGleb Smirnoff #ifdef INET 820b8056faeSGleb Smirnoff || (!isipv6 && (m->m_flags & M_IP_NEXTHOP)) 821b8056faeSGleb Smirnoff #endif 822b8056faeSGleb Smirnoff #endif 823b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6) 824b8056faeSGleb Smirnoff (m->m_flags & M_IP_NEXTHOP) 825b8056faeSGleb Smirnoff #endif 826b8056faeSGleb Smirnoff ) 8279b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 8289b932e9eSAndre Oppermann 829ce7ad664SEd Maste findpcb: 830ce7ad664SEd Maste #ifdef INVARIANTS 831ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 832ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 833ce7ad664SEd Maste } else { 834ce7ad664SEd Maste INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 835ce7ad664SEd Maste } 836ce7ad664SEd Maste #endif 8378a006adbSBjoern A. Zeeb #ifdef INET6 8388a006adbSBjoern A. Zeeb if (isipv6 && fwd_tag != NULL) { 8398a006adbSBjoern A. Zeeb struct sockaddr_in6 *next_hop6; 8408a006adbSBjoern A. Zeeb 8418a006adbSBjoern A. Zeeb next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1); 8428a006adbSBjoern A. Zeeb /* 8438a006adbSBjoern A. Zeeb * Transparently forwarded. Pretend to be the destination. 8448a006adbSBjoern A. Zeeb * Already got one like this? 8458a006adbSBjoern A. Zeeb */ 8468a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, 8478a006adbSBjoern A. Zeeb &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport, 8488a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m); 8498a006adbSBjoern A. Zeeb if (!inp) { 8508a006adbSBjoern A. Zeeb /* 8518a006adbSBjoern A. Zeeb * It's new. Try to find the ambushing socket. 8528a006adbSBjoern A. Zeeb * Because we've rewritten the destination address, 8538a006adbSBjoern A. Zeeb * any hardware-generated hash is ignored. 8548a006adbSBjoern A. Zeeb */ 8558a006adbSBjoern A. Zeeb inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src, 8568a006adbSBjoern A. Zeeb th->th_sport, &next_hop6->sin6_addr, 8578a006adbSBjoern A. Zeeb next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) : 8588a006adbSBjoern A. Zeeb th->th_dport, INPLOOKUP_WILDCARD | 8598a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 8608a006adbSBjoern A. Zeeb } 861c1de64a4SAndrey V. Elsukov } else if (isipv6) { 8628a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src, 8638a006adbSBjoern A. Zeeb th->th_sport, &ip6->ip6_dst, th->th_dport, 8648a006adbSBjoern A. Zeeb INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 8658a006adbSBjoern A. Zeeb m->m_pkthdr.rcvif, m); 8668a006adbSBjoern A. Zeeb } 8678a006adbSBjoern A. Zeeb #endif /* INET6 */ 8688a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET) 8698a006adbSBjoern A. Zeeb else 8708a006adbSBjoern A. Zeeb #endif 8718a006adbSBjoern A. Zeeb #ifdef INET 8728a006adbSBjoern A. Zeeb if (fwd_tag != NULL) { 8739b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 8749b932e9eSAndre Oppermann 8759b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 876f9e354dfSJulian Elischer /* 8772b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 878f9e354dfSJulian Elischer * already got one like this? 879f9e354dfSJulian Elischer */ 880d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport, 881fa046d87SRobert Watson ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB, 882d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 883fa046d87SRobert Watson if (!inp) { 884fa046d87SRobert Watson /* 885fa046d87SRobert Watson * It's new. Try to find the ambushing socket. 886d3c1f003SRobert Watson * Because we've rewritten the destination address, 887d3c1f003SRobert Watson * any hardware-generated hash is ignored. 888fa046d87SRobert Watson */ 889fa046d87SRobert Watson inp = in_pcblookup(&V_tcbinfo, ip->ip_src, 890fa046d87SRobert Watson th->th_sport, next_hop->sin_addr, 891fa046d87SRobert Watson next_hop->sin_port ? ntohs(next_hop->sin_port) : 892fa046d87SRobert Watson th->th_dport, INPLOOKUP_WILDCARD | 893fa046d87SRobert Watson INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 894f9e354dfSJulian Elischer } 895b10fbdeaSAndre Oppermann } else 896d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, 897fa046d87SRobert Watson th->th_sport, ip->ip_dst, th->th_dport, 898fa046d87SRobert Watson INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 899d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 9008a006adbSBjoern A. Zeeb #endif /* INET */ 901f9e354dfSJulian Elischer 902df8bae1dSRodney W. Grimes /* 903574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 904574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 9058b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 906df8bae1dSRodney W. Grimes */ 907816a3d83SPoul-Henning Kamp if (inp == NULL) { 908574b6964SAndre Oppermann /* 909574b6964SAndre Oppermann * Log communication attempts to ports that are not 910574b6964SAndre Oppermann * in use. 911574b6964SAndre Oppermann */ 912574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 913574b6964SAndre Oppermann tcp_log_in_vain == 2) { 914b7d747ecSAndre Oppermann if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6))) 915df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 916df541e5fSAndre Oppermann "to closed port\n", s, __func__); 9172e4e1b4cSGeoff Rehmet } 918574b6964SAndre Oppermann /* 919574b6964SAndre Oppermann * When blackholing do not respond with a RST but 920574b6964SAndre Oppermann * completely ignore the segment and drop it. 921574b6964SAndre Oppermann */ 922603724d3SBjoern A. Zeeb if ((V_blackhole == 1 && (thflags & TH_SYN)) || 923603724d3SBjoern A. Zeeb V_blackhole == 2) 9241929eae1SAndre Oppermann goto dropunlock; 925574b6964SAndre Oppermann 926a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 927a57815efSBosko Milekic goto dropwithreset; 928816a3d83SPoul-Henning Kamp } 929fa046d87SRobert Watson INP_WLOCK_ASSERT(inp); 930f5cf1e5fSJulien Charbon /* 931f5cf1e5fSJulien Charbon * While waiting for inp lock during the lookup, another thread 932f5cf1e5fSJulien Charbon * can have dropped the inpcb, in which case we need to loop back 933f5cf1e5fSJulien Charbon * and try to find a new inpcb to deliver to. 934f5cf1e5fSJulien Charbon */ 935f5cf1e5fSJulien Charbon if (inp->inp_flags & INP_DROPPED) { 936f5cf1e5fSJulien Charbon INP_WUNLOCK(inp); 937f5cf1e5fSJulien Charbon inp = NULL; 938f5cf1e5fSJulien Charbon goto findpcb; 939f5cf1e5fSJulien Charbon } 940c2529042SHans Petter Selasky if ((inp->inp_flowtype == M_HASHTYPE_NONE) && 941c2529042SHans Petter Selasky (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) && 942c2529042SHans Petter Selasky ((inp->inp_socket == NULL) || 943c2529042SHans Petter Selasky (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0)) { 94480cb9f21SKip Macy inp->inp_flowid = m->m_pkthdr.flowid; 9452f719932SAdrian Chadd inp->inp_flowtype = M_HASHTYPE_GET(m); 94680cb9f21SKip Macy } 947a2589465SKen Smith #ifdef IPSEC 948a2589465SKen Smith #ifdef INET6 949a2589465SKen Smith if (isipv6 && ipsec6_in_reject(m, inp)) { 950a2589465SKen Smith goto dropunlock; 951a2589465SKen Smith } else 952a2589465SKen Smith #endif /* INET6 */ 953a2589465SKen Smith if (ipsec4_in_reject(m, inp) != 0) { 954a2589465SKen Smith goto dropunlock; 955a2589465SKen Smith } 956a2589465SKen Smith #endif /* IPSEC */ 957a2589465SKen Smith 9588d573cc1SAndre Oppermann /* 9598d573cc1SAndre Oppermann * Check the minimum TTL for socket. 9608d573cc1SAndre Oppermann */ 96134f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 96234f83c52SGeorge V. Neville-Neil #ifdef INET6 9632d8868dbSJonathan T. Looney if (isipv6) { 9642d8868dbSJonathan T. Looney if (inp->inp_ip_minttl > ip6->ip6_hlim) 965302ce8d6SAndre Oppermann goto dropunlock; 9662d8868dbSJonathan T. Looney } else 96734f83c52SGeorge V. Neville-Neil #endif 96834f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 969302ce8d6SAndre Oppermann goto dropunlock; 97034f83c52SGeorge V. Neville-Neil } 971936cd18dSAndre Oppermann 972340c35deSJonathan Lemon /* 973252ca428SRobert Watson * A previous connection in TIMEWAIT state is supposed to catch stray 974252ca428SRobert Watson * or duplicate segments arriving late. If this segment was a 9750989f56cSRobert Watson * legitimate new connection attempt, the old INPCB gets removed and 976252ca428SRobert Watson * we can try again to find a listening socket. 977252ca428SRobert Watson * 978fa046d87SRobert Watson * At this point, due to earlier optimism, we may hold only an inpcb 979fa046d87SRobert Watson * lock, and not the inpcbinfo write lock. If so, we need to try to 980fa046d87SRobert Watson * acquire it, or if that fails, acquire a reference on the inpcb, 981fa046d87SRobert Watson * drop all locks, acquire a global write lock, and then re-acquire 982fa046d87SRobert Watson * the inpcb lock. We may at that point discover that another thread 983fa046d87SRobert Watson * has tried to free the inpcb, in which case we need to loop back 984fa046d87SRobert Watson * and try to find a new inpcb to deliver to. 985fa046d87SRobert Watson * 986fa046d87SRobert Watson * XXXRW: It may be time to rethink timewait locking. 987340c35deSJonathan Lemon */ 988883e9bc4SRobert Watson relocked: 989ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 990fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 991ff9b006dSJulien Charbon if (INP_INFO_TRY_RLOCK(&V_tcbinfo) == 0) { 992252ca428SRobert Watson in_pcbref(inp); 993252ca428SRobert Watson INP_WUNLOCK(inp); 994ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 995ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 996252ca428SRobert Watson INP_WLOCK(inp); 997fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) { 998252ca428SRobert Watson inp = NULL; 999252ca428SRobert Watson goto findpcb; 1000f5cf1e5fSJulien Charbon } else if (inp->inp_flags & INP_DROPPED) { 1001f5cf1e5fSJulien Charbon INP_WUNLOCK(inp); 1002f5cf1e5fSJulien Charbon inp = NULL; 1003f5cf1e5fSJulien Charbon goto findpcb; 1004252ca428SRobert Watson } 1005f681a5fdSRobert Watson } else 1006ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 1007252ca428SRobert Watson } 1008ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1009252ca428SRobert Watson 1010340c35deSJonathan Lemon if (thflags & TH_SYN) 1011f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 10128d573cc1SAndre Oppermann /* 10138d573cc1SAndre Oppermann * NB: tcp_twcheck unlocks the INP and frees the mbuf. 10148d573cc1SAndre Oppermann */ 10152104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 1016340c35deSJonathan Lemon goto findpcb; 1017ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 10188f5a8818SKevin Lo return (IPPROTO_DONE); 1019340c35deSJonathan Lemon } 1020794235b7SAndre Oppermann /* 1021794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 1022794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 1023794235b7SAndre Oppermann * segment and send an appropriate response. 1024794235b7SAndre Oppermann */ 1025df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 1026a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 1027a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 1028a57815efSBosko Milekic goto dropwithreset; 102909f81a46SBosko Milekic } 1030df8bae1dSRodney W. Grimes 103109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 103209fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 103309fe6320SNavdeep Parhar tcp_offload_input(tp, m); 103409fe6320SNavdeep Parhar m = NULL; /* consumed by the TOE driver */ 103509fe6320SNavdeep Parhar goto dropunlock; 103609fe6320SNavdeep Parhar } 103709fe6320SNavdeep Parhar #endif 103809fe6320SNavdeep Parhar 1039252ca428SRobert Watson /* 1040252ca428SRobert Watson * We've identified a valid inpcb, but it could be that we need an 1041fa046d87SRobert Watson * inpcbinfo write lock but don't hold it. In this case, attempt to 1042fa046d87SRobert Watson * acquire using the same strategy as the TIMEWAIT case above. If we 1043fa046d87SRobert Watson * relock, we have to jump back to 'relocked' as the connection might 1044fa046d87SRobert Watson * now be in TIMEWAIT. 1045252ca428SRobert Watson */ 1046fa046d87SRobert Watson #ifdef INVARIANTS 1047a7c7f2a7SJohn Baldwin if ((thflags & (TH_FIN | TH_RST)) != 0) 1048ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1049fa046d87SRobert Watson #endif 1050a7c7f2a7SJohn Baldwin if (!((tp->t_state == TCPS_ESTABLISHED && (thflags & TH_SYN) == 0) || 1051281a0fd4SPatrick Kelsey (tp->t_state == TCPS_LISTEN && (thflags & TH_SYN) && 105268bd7ed1SJonathan T. Looney !IS_FASTOPEN(tp->t_flags)))) { 1053fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 1054ff9b006dSJulien Charbon if (INP_INFO_TRY_RLOCK(&V_tcbinfo) == 0) { 1055252ca428SRobert Watson in_pcbref(inp); 1056252ca428SRobert Watson INP_WUNLOCK(inp); 1057ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 1058ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 1059252ca428SRobert Watson INP_WLOCK(inp); 1060fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) { 1061252ca428SRobert Watson inp = NULL; 1062252ca428SRobert Watson goto findpcb; 1063f5cf1e5fSJulien Charbon } else if (inp->inp_flags & INP_DROPPED) { 1064f5cf1e5fSJulien Charbon INP_WUNLOCK(inp); 1065f5cf1e5fSJulien Charbon inp = NULL; 1066f5cf1e5fSJulien Charbon goto findpcb; 1067252ca428SRobert Watson } 1068883e9bc4SRobert Watson goto relocked; 1069f681a5fdSRobert Watson } else 1070ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 1071252ca428SRobert Watson } 1072ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1073252ca428SRobert Watson } 1074252ca428SRobert Watson 1075c488362eSRobert Watson #ifdef MAC 10768501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 107730d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 1078302ce8d6SAndre Oppermann goto dropunlock; 1079c488362eSRobert Watson #endif 1080a557af22SRobert Watson so = inp->inp_socket; 1081302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 1082610ee2f9SDavid Greenman #ifdef TCPDEBUG 1083df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 1084df8bae1dSRodney W. Grimes ostate = tp->t_state; 108510fe523eSMaxim Konovalov #ifdef INET6 10866f697424SBjoern A. Zeeb if (isipv6) { 1087540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 1088d30d90dcSMaxim Konovalov } else 10896f697424SBjoern A. Zeeb #endif 1090540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 1091fb59c426SYoshinobu Inoue tcp_savetcp = *th; 1092df8bae1dSRodney W. Grimes } 1093b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */ 1094db33b3e6SAndre Oppermann /* 1095db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 1096db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 1097a7c7f2a7SJohn Baldwin * attempt or the completion of a previous one. 1098db33b3e6SAndre Oppermann */ 109945274760SJonathan T. Looney KASSERT(tp->t_state == TCPS_LISTEN || !(so->so_options & SO_ACCEPTCONN), 110045274760SJonathan T. Looney ("%s: so accepting but tp %p not listening", __func__, tp)); 110145274760SJonathan T. Looney if (tp->t_state == TCPS_LISTEN && (so->so_options & SO_ACCEPTCONN)) { 1102540e8b7eSJeffrey Hsu struct in_conninfo inc; 1103540e8b7eSJeffrey Hsu 11048bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 1105302ce8d6SAndre Oppermann #ifdef INET6 1106be2ac88cSJonathan Lemon if (isipv6) { 1107dcdb4371SBjoern A. Zeeb inc.inc_flags |= INC_ISIPV6; 1108be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 1109be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 1110302ce8d6SAndre Oppermann } else 1111302ce8d6SAndre Oppermann #endif 1112302ce8d6SAndre Oppermann { 1113be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 1114be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 1115be2ac88cSJonathan Lemon } 1116be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 1117be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 11187973fba3SJulian Elischer inc.inc_fibnum = so->so_fibnum; 1119be2ac88cSJonathan Lemon 1120fb59c426SYoshinobu Inoue /* 11218d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 11228d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 11238d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 1124fb59c426SYoshinobu Inoue */ 1125be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 1126a7c7f2a7SJohn Baldwin 1127ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1128bf6d304aSAndre Oppermann /* 1129bf6d304aSAndre Oppermann * Parse the TCP options here because 1130bf6d304aSAndre Oppermann * syncookies need access to the reflected 1131bf6d304aSAndre Oppermann * timestamp. 1132bf6d304aSAndre Oppermann */ 1133bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 11349fa198beSAndre Oppermann /* 11359fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 11369fa198beSAndre Oppermann * inp and tcpinfo locks. 11379fa198beSAndre Oppermann */ 1138bf6d304aSAndre Oppermann if (!syncache_expand(&inc, &to, th, &so, m)) { 11394195b4afSPaul Traina /* 1140e207f800SAndre Oppermann * No syncache entry or ACK was not 1141be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 11428d573cc1SAndre Oppermann * NB: syncache did its own logging 11438d573cc1SAndre Oppermann * of the failure cause. 11444195b4afSPaul Traina */ 1145be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 1146be2ac88cSJonathan Lemon goto dropwithreset; 1147be2ac88cSJonathan Lemon } 1148281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 114909c305ebSPatrick Kelsey tfo_socket_result: 1150281a0fd4SPatrick Kelsey #endif 1151f76fcf6dSJeffrey Hsu if (so == NULL) { 1152be2ac88cSJonathan Lemon /* 1153e207f800SAndre Oppermann * We completed the 3-way handshake 1154e207f800SAndre Oppermann * but could not allocate a socket 1155e207f800SAndre Oppermann * either due to memory shortage, 1156e207f800SAndre Oppermann * listen queue length limits or 1157a160e630SAndre Oppermann * global socket limits. Send RST 1158a160e630SAndre Oppermann * or wait and have the remote end 1159a160e630SAndre Oppermann * retransmit the ACK for another 1160a160e630SAndre Oppermann * try. 1161be2ac88cSJonathan Lemon */ 1162a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1163a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11648d573cc1SAndre Oppermann "Socket allocation failed due to " 11658d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 1166ac957cd2SJulian Elischer s, __func__, 1167ac957cd2SJulian Elischer V_tcp_sc_rst_sock_fail ? 1168ac957cd2SJulian Elischer "sending RST" : "try again"); 1169603724d3SBjoern A. Zeeb if (V_tcp_sc_rst_sock_fail) { 1170e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 1171e207f800SAndre Oppermann goto dropwithreset; 1172a160e630SAndre Oppermann } else 1173a160e630SAndre Oppermann goto dropunlock; 1174f76fcf6dSJeffrey Hsu } 1175be2ac88cSJonathan Lemon /* 1176be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 11778d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 11788d573cc1SAndre Oppermann * created socket and update the tp variable. 1179be2ac88cSJonathan Lemon */ 11808501a69cSRobert Watson INP_WUNLOCK(inp); /* listen socket */ 1181be2ac88cSJonathan Lemon inp = sotoinpcb(so); 1182ff9b006dSJulien Charbon /* 1183ff9b006dSJulien Charbon * New connection inpcb is already locked by 1184ff9b006dSJulien Charbon * syncache_expand(). 1185ff9b006dSJulien Charbon */ 1186ff9b006dSJulien Charbon INP_WLOCK_ASSERT(inp); 1187be2ac88cSJonathan Lemon tp = intotcpcb(inp); 11888d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 11898d573cc1SAndre Oppermann ("%s: ", __func__)); 11902903309aSAttilio Rao #ifdef TCP_SIGNATURE 11912903309aSAttilio Rao if (sig_checked == 0) { 11922903309aSAttilio Rao tcp_dooptions(&to, optp, optlen, 11932903309aSAttilio Rao (thflags & TH_SYN) ? TO_SYN : 0); 11942903309aSAttilio Rao if (!tcp_signature_verify_input(m, off0, tlen, 11952903309aSAttilio Rao optlen, &to, th, tp->t_flags)) { 11962903309aSAttilio Rao 11972903309aSAttilio Rao /* 11982903309aSAttilio Rao * In SYN_SENT state if it receives an 11992903309aSAttilio Rao * RST, it is allowed for further 12002903309aSAttilio Rao * processing. 12012903309aSAttilio Rao */ 12022903309aSAttilio Rao if ((thflags & TH_RST) == 0 || 12032903309aSAttilio Rao (tp->t_state == TCPS_SYN_SENT) == 0) 12042903309aSAttilio Rao goto dropunlock; 12052903309aSAttilio Rao } 12062903309aSAttilio Rao sig_checked = 1; 12072903309aSAttilio Rao } 12082903309aSAttilio Rao #endif 12092903309aSAttilio Rao 1210be2ac88cSJonathan Lemon /* 1211302ce8d6SAndre Oppermann * Process the segment and the data it 1212302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 1213302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 1214302ce8d6SAndre Oppermann */ 121555bceb1eSRandall Stewart tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 1216252ca428SRobert Watson iptos, ti_locked); 1217603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 12188f5a8818SKevin Lo return (IPPROTO_DONE); 1219be2ac88cSJonathan Lemon } 1220a160e630SAndre Oppermann /* 12218d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 12228d573cc1SAndre Oppermann * 1223a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 1224a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 1225a160e630SAndre Oppermann * retransmits. 122619bc77c5SAndre Oppermann * 122719bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 122819bc77c5SAndre Oppermann * causes. 1229a160e630SAndre Oppermann */ 1230a160e630SAndre Oppermann if (thflags & TH_RST) { 123119bc77c5SAndre Oppermann syncache_chkrst(&inc, th); 1232a160e630SAndre Oppermann goto dropunlock; 1233a160e630SAndre Oppermann } 1234a160e630SAndre Oppermann /* 1235a160e630SAndre Oppermann * We can't do anything without SYN. 1236a160e630SAndre Oppermann */ 1237a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 1238a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1239a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1240773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 12418d573cc1SAndre Oppermann s, __func__); 124278b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1243a160e630SAndre Oppermann goto dropunlock; 1244a160e630SAndre Oppermann } 1245a160e630SAndre Oppermann /* 1246a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 1247a160e630SAndre Oppermann */ 1248fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1249a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1250a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12518d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 12528d573cc1SAndre Oppermann s, __func__); 1253a160e630SAndre Oppermann syncache_badack(&inc); /* XXX: Not needed! */ 125478b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1255a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1256a57815efSBosko Milekic goto dropwithreset; 12574195b4afSPaul Traina } 1258a160e630SAndre Oppermann /* 1259a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 1260a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 1261a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 1262a160e630SAndre Oppermann * TCP/IP stack. 1263a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 1264a160e630SAndre Oppermann * and is constantly refining its stack detection 1265a160e630SAndre Oppermann * strategies. 12668d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 1267a160e630SAndre Oppermann * and was used by RFC1644. 1268a160e630SAndre Oppermann */ 1269603724d3SBjoern A. Zeeb if ((thflags & TH_FIN) && V_drop_synfin) { 1270a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1271a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1272773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 12738d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 127478b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1275302ce8d6SAndre Oppermann goto dropunlock; 1276ebb0cbeaSPaul Traina } 1277be2ac88cSJonathan Lemon /* 1278be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 1279a160e630SAndre Oppermann * 1280a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 1281a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 1282a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 1283be2ac88cSJonathan Lemon */ 1284a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 1285a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 1286a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 1287a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 128833841545SHajimu UMEMOTO #ifdef INET6 128933841545SHajimu UMEMOTO /* 129033841545SHajimu UMEMOTO * If deprecated address is forbidden, 129133841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 129233841545SHajimu UMEMOTO * address to prevent any new inbound connection from 129333841545SHajimu UMEMOTO * getting established. 129433841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 129533841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 129633841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 129733841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 129833841545SHajimu UMEMOTO * for the exchange. 129933841545SHajimu UMEMOTO * 130033841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 130133841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 130233841545SHajimu UMEMOTO * SYN in this case. 130333841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 130433841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 130533841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 130633841545SHajimu UMEMOTO * used" 130733841545SHajimu UMEMOTO * 2. use of it with new communication: 130833841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 130933841545SHajimu UMEMOTO * with sufficient scope is available" 131033841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 131133841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 131233841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 131333841545SHajimu UMEMOTO * 131433841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 131533841545SHajimu UMEMOTO * multiple description text for deprecated address 131633841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 131733841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 131833841545SHajimu UMEMOTO */ 1319603724d3SBjoern A. Zeeb if (isipv6 && !V_ip6_use_deprecated) { 132033841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 132133841545SHajimu UMEMOTO 13223e88eb90SAndrey V. Elsukov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */); 13238c0fec80SRobert Watson if (ia6 != NULL && 132433841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 13258c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 1326a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1327a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13288d573cc1SAndre Oppermann "Connection attempt to deprecated " 13298d573cc1SAndre Oppermann "IPv6 address rejected\n", 1330a160e630SAndre Oppermann s, __func__); 133133841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 133233841545SHajimu UMEMOTO goto dropwithreset; 133333841545SHajimu UMEMOTO } 133477d396fdSMaksim Yevmenkin if (ia6) 13358c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 133633841545SHajimu UMEMOTO } 1337b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 133865f28919SJesper Skriver /* 1339db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 13408d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 1341db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 13428d573cc1SAndre Oppermann * If it is from this socket it must be forged. 13438d573cc1SAndre Oppermann * Don't respond if the source or destination is a 13448d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 134593ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 134693ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 134793ec91baSCrist J. Clark * in_broadcast() to find them. 1348be2ac88cSJonathan Lemon */ 13498d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 13508d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 13518d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13528d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 1353773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 1354302ce8d6SAndre Oppermann goto dropunlock; 13558d573cc1SAndre Oppermann } 1356db33b3e6SAndre Oppermann #ifdef INET6 1357b287c6c7SBjoern A. Zeeb if (isipv6) { 1358db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1359a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 1360a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1361a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13628d573cc1SAndre Oppermann "Connection attempt to/from self " 1363773673c1SAndre Oppermann "ignored\n", s, __func__); 1364302ce8d6SAndre Oppermann goto dropunlock; 1365a160e630SAndre Oppermann } 1366be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1367a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 1368a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1369a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13708d573cc1SAndre Oppermann "Connection attempt from/to multicast " 1371773673c1SAndre Oppermann "address ignored\n", s, __func__); 1372302ce8d6SAndre Oppermann goto dropunlock; 1373a160e630SAndre Oppermann } 1374b287c6c7SBjoern A. Zeeb } 1375db33b3e6SAndre Oppermann #endif 1376b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 1377b287c6c7SBjoern A. Zeeb else 1378b287c6c7SBjoern A. Zeeb #endif 1379b287c6c7SBjoern A. Zeeb #ifdef INET 1380b287c6c7SBjoern A. Zeeb { 1381db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1382a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 1383a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1384a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13858d573cc1SAndre Oppermann "Connection attempt from/to self " 1386773673c1SAndre Oppermann "ignored\n", s, __func__); 1387302ce8d6SAndre Oppermann goto dropunlock; 1388a160e630SAndre Oppermann } 1389be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1390be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 13912ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 1392a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 1393a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1394a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13958d573cc1SAndre Oppermann "Connection attempt from/to broad- " 1396773673c1SAndre Oppermann "or multicast address ignored\n", 1397a160e630SAndre Oppermann s, __func__); 1398302ce8d6SAndre Oppermann goto dropunlock; 1399c068736aSJeffrey Hsu } 1400a160e630SAndre Oppermann } 1401b287c6c7SBjoern A. Zeeb #endif 1402be2ac88cSJonathan Lemon /* 1403db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 1404db33b3e6SAndre Oppermann * for syncache. 1405be2ac88cSJonathan Lemon */ 14063c653157SHartmut Brandt #ifdef TCPDEBUG 14073c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 14083c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 14093c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 14103c653157SHartmut Brandt #endif 1411*2b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 1412f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 1413281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 1414281a0fd4SPatrick Kelsey if (syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL)) 141509c305ebSPatrick Kelsey goto tfo_socket_result; 1416281a0fd4SPatrick Kelsey #else 141709fe6320SNavdeep Parhar syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL); 1418281a0fd4SPatrick Kelsey #endif 1419be2ac88cSJonathan Lemon /* 14204d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 1421a7c7f2a7SJohn Baldwin * Only the listen socket is unlocked by syncache_add(). 1422be2ac88cSJonathan Lemon */ 1423ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 1424ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1425a7c7f2a7SJohn Baldwin ti_locked = TI_UNLOCKED; 1426a7c7f2a7SJohn Baldwin } 1427603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 14288f5a8818SKevin Lo return (IPPROTO_DONE); 1429982c1675SAndre Oppermann } else if (tp->t_state == TCPS_LISTEN) { 1430982c1675SAndre Oppermann /* 1431982c1675SAndre Oppermann * When a listen socket is torn down the SO_ACCEPTCONN 1432982c1675SAndre Oppermann * flag is removed first while connections are drained 1433982c1675SAndre Oppermann * from the accept queue in a unlock/lock cycle of the 1434982c1675SAndre Oppermann * ACCEPT_LOCK, opening a race condition allowing a SYN 1435982c1675SAndre Oppermann * attempt go through unhandled. 1436982c1675SAndre Oppermann */ 1437982c1675SAndre Oppermann goto dropunlock; 1438f76fcf6dSJeffrey Hsu } 1439db33b3e6SAndre Oppermann 14402903309aSAttilio Rao #ifdef TCP_SIGNATURE 14412903309aSAttilio Rao if (sig_checked == 0) { 14422903309aSAttilio Rao tcp_dooptions(&to, optp, optlen, 14432903309aSAttilio Rao (thflags & TH_SYN) ? TO_SYN : 0); 14442903309aSAttilio Rao if (!tcp_signature_verify_input(m, off0, tlen, optlen, &to, 14452903309aSAttilio Rao th, tp->t_flags)) { 14462903309aSAttilio Rao 14472903309aSAttilio Rao /* 14482903309aSAttilio Rao * In SYN_SENT state if it receives an RST, it is 14492903309aSAttilio Rao * allowed for further processing. 14502903309aSAttilio Rao */ 14512903309aSAttilio Rao if ((thflags & TH_RST) == 0 || 14522903309aSAttilio Rao (tp->t_state == TCPS_SYN_SENT) == 0) 14532903309aSAttilio Rao goto dropunlock; 14542903309aSAttilio Rao } 14552903309aSAttilio Rao sig_checked = 1; 14562903309aSAttilio Rao } 14572903309aSAttilio Rao #endif 14582903309aSAttilio Rao 1459*2b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 146057f60867SMark Johnston 1461302ce8d6SAndre Oppermann /* 14628d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 14638d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 14648d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 1465302ce8d6SAndre Oppermann */ 146655bceb1eSRandall Stewart tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked); 1467603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 14688f5a8818SKevin Lo return (IPPROTO_DONE); 1469be2ac88cSJonathan Lemon 1470302ce8d6SAndre Oppermann dropwithreset: 1471*2b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 147257f60867SMark Johnston 1473ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 1474ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1475252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1476fa046d87SRobert Watson } 1477fa046d87SRobert Watson #ifdef INVARIANTS 1478fa046d87SRobert Watson else { 1479fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset " 1480fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1481fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1482fa046d87SRobert Watson } 1483fa046d87SRobert Watson #endif 1484014ea782SRobert Watson 1485014ea782SRobert Watson if (inp != NULL) { 1486302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 1487014ea782SRobert Watson INP_WUNLOCK(inp); 1488dd8ac7f9SRobert Watson } else 1489014ea782SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1490302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 1491014ea782SRobert Watson goto drop; 1492014ea782SRobert Watson 1493302ce8d6SAndre Oppermann dropunlock: 149457f60867SMark Johnston if (m != NULL) 1495*2b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 149657f60867SMark Johnston 1497ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 1498ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1499252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1500fa046d87SRobert Watson } 1501fa046d87SRobert Watson #ifdef INVARIANTS 1502fa046d87SRobert Watson else { 1503fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock " 1504fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1505fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1506fa046d87SRobert Watson } 1507fa046d87SRobert Watson #endif 1508252ca428SRobert Watson 15099fa198beSAndre Oppermann if (inp != NULL) 15108501a69cSRobert Watson INP_WUNLOCK(inp); 1511014ea782SRobert Watson 1512302ce8d6SAndre Oppermann drop: 1513603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1514a160e630SAndre Oppermann if (s != NULL) 1515a160e630SAndre Oppermann free(s, M_TCPLOG); 1516302ce8d6SAndre Oppermann if (m != NULL) 1517302ce8d6SAndre Oppermann m_freem(m); 15188f5a8818SKevin Lo return (IPPROTO_DONE); 1519302ce8d6SAndre Oppermann } 1520302ce8d6SAndre Oppermann 152155bceb1eSRandall Stewart void 1522302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 1523252ca428SRobert Watson struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos, 1524252ca428SRobert Watson int ti_locked) 1525302ce8d6SAndre Oppermann { 1526021eaf79SHiren Panchasara int thflags, acked, ourfinisacked, needoutput = 0, sack_changed; 1527302ce8d6SAndre Oppermann int rstreason, todrop, win; 15283ac12506SJonathan T. Looney uint32_t tiwin; 15294b7b743cSLawrence Stewart uint16_t nsegs; 153007dacf03SAndre Oppermann char *s; 153107dacf03SAndre Oppermann struct in_conninfo *inc; 1532c11a15bfSGleb Smirnoff struct mbuf *mfree; 1533302ce8d6SAndre Oppermann struct tcpopt to; 153468bd7ed1SJonathan T. Looney #ifdef TCP_RFC7413 1535281a0fd4SPatrick Kelsey int tfo_syn; 153668bd7ed1SJonathan T. Looney #endif 1537302ce8d6SAndre Oppermann 153814739780SMaxim Konovalov #ifdef TCPDEBUG 153914739780SMaxim Konovalov /* 154014739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 154114739780SMaxim Konovalov * now IPv6. 154214739780SMaxim Konovalov */ 154314739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 154414739780SMaxim Konovalov struct tcphdr tcp_savetcp; 154514739780SMaxim Konovalov short ostate = 0; 154614739780SMaxim Konovalov #endif 1547302ce8d6SAndre Oppermann thflags = th->th_flags; 154807dacf03SAndre Oppermann inc = &tp->t_inpcb->inp_inc; 1549d64a46eaSLawrence Stewart tp->sackhint.last_sack_ack = 0; 1550021eaf79SHiren Panchasara sack_changed = 0; 15514b7b743cSLawrence Stewart nsegs = max(1, m->m_pkthdr.lro_nsegs); 1552302ce8d6SAndre Oppermann 1553252ca428SRobert Watson /* 1554252ca428SRobert Watson * If this is either a state-changing packet or current state isn't 1555252ca428SRobert Watson * established, we require a write lock on tcbinfo. Otherwise, we 15560989f56cSRobert Watson * allow the tcbinfo to be in either alocked or unlocked, as the 15570989f56cSRobert Watson * caller may have unnecessarily acquired a write lock due to a race. 1558252ca428SRobert Watson */ 1559252ca428SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 1560252ca428SRobert Watson tp->t_state != TCPS_ESTABLISHED) { 1561ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, ("%s ti_locked %d for " 1562252ca428SRobert Watson "SYN/FIN/RST/!EST", __func__, ti_locked)); 1563ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1564252ca428SRobert Watson } else { 1565252ca428SRobert Watson #ifdef INVARIANTS 1566ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 1567ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1568fa046d87SRobert Watson else { 1569fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST " 1570fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1571fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1572fa046d87SRobert Watson } 1573252ca428SRobert Watson #endif 1574252ca428SRobert Watson } 15758501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1576679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1577679d9708SAndre Oppermann __func__)); 1578679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1579679d9708SAndre Oppermann __func__)); 1580df8bae1dSRodney W. Grimes 158186a996e6SHiren Panchasara #ifdef TCPPCAP 158286a996e6SHiren Panchasara /* Save segment, if requested. */ 158386a996e6SHiren Panchasara tcp_pcap_add(th, m, &(tp->t_inpkts)); 158486a996e6SHiren Panchasara #endif 158586a996e6SHiren Panchasara 1586df8bae1dSRodney W. Grimes /* 1587df8bae1dSRodney W. Grimes * Segment received on connection. 1588df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1589e8949f74SAndre Oppermann * XXX: This should be done after segment 1590e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 1591df8bae1dSRodney W. Grimes */ 15929b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 1593df8bae1dSRodney W. Grimes 1594df8bae1dSRodney W. Grimes /* 1595c52102ddSHiren Panchasara * Scale up the window into a 32-bit value. 1596e8949f74SAndre Oppermann * For the SYN_SENT state the scale is zero. 1597464fcfbcSAndre Oppermann */ 1598464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 1599464fcfbcSAndre Oppermann 1600464fcfbcSAndre Oppermann /* 1601f2512ba1SRui Paulo * TCP ECN processing. 1602f2512ba1SRui Paulo */ 1603f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) { 16049c251892SRui Paulo if (thflags & TH_CWR) 16059c251892SRui Paulo tp->t_flags &= ~TF_ECN_SND_ECE; 1606f2512ba1SRui Paulo switch (iptos & IPTOS_ECN_MASK) { 1607f2512ba1SRui Paulo case IPTOS_ECN_CE: 1608f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_ECE; 160978b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ce); 1610f2512ba1SRui Paulo break; 1611f2512ba1SRui Paulo case IPTOS_ECN_ECT0: 161278b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect0); 1613f2512ba1SRui Paulo break; 1614f2512ba1SRui Paulo case IPTOS_ECN_ECT1: 161578b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect1); 1616f2512ba1SRui Paulo break; 1617f2512ba1SRui Paulo } 161864807b30SHiren Panchasara 161964807b30SHiren Panchasara /* Process a packet differently from RFC3168. */ 162064807b30SHiren Panchasara cc_ecnpkt_handler(tp, th, iptos); 162164807b30SHiren Panchasara 1622dbc42409SLawrence Stewart /* Congestion experienced. */ 1623dbc42409SLawrence Stewart if (thflags & TH_ECE) { 1624dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_ECN); 1625f2512ba1SRui Paulo } 1626f2512ba1SRui Paulo } 1627f2512ba1SRui Paulo 1628f2512ba1SRui Paulo /* 1629f72167f4SAndre Oppermann * Parse options on any incoming segment. 1630f72167f4SAndre Oppermann */ 1631302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1632302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1633302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1634f72167f4SAndre Oppermann 1635f72167f4SAndre Oppermann /* 1636f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1637bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1638bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1639bf6d304aSAndre Oppermann * was established. 1640f72167f4SAndre Oppermann */ 1641bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1642e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 1643d8951c8aSBjoern A. Zeeb if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) 1644f72167f4SAndre Oppermann to.to_tsecr = 0; 1645bf6d304aSAndre Oppermann } 164607dacf03SAndre Oppermann /* 164707dacf03SAndre Oppermann * If timestamps were negotiated during SYN/ACK they should 164807dacf03SAndre Oppermann * appear on every segment during this session and vice versa. 164907dacf03SAndre Oppermann */ 165007dacf03SAndre Oppermann if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) { 165107dacf03SAndre Oppermann if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 165207dacf03SAndre Oppermann log(LOG_DEBUG, "%s; %s: Timestamp missing, " 165307dacf03SAndre Oppermann "no action\n", s, __func__); 165407dacf03SAndre Oppermann free(s, M_TCPLOG); 165507dacf03SAndre Oppermann } 165607dacf03SAndre Oppermann } 165707dacf03SAndre Oppermann if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) { 165807dacf03SAndre Oppermann if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 165907dacf03SAndre Oppermann log(LOG_DEBUG, "%s; %s: Timestamp not expected, " 166007dacf03SAndre Oppermann "no action\n", s, __func__); 166107dacf03SAndre Oppermann free(s, M_TCPLOG); 166207dacf03SAndre Oppermann } 166307dacf03SAndre Oppermann } 1664f72167f4SAndre Oppermann 1665f72167f4SAndre Oppermann /* 166697d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 166797d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 16685396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 16695396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 167097d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1671df8bae1dSRodney W. Grimes */ 16720a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1673464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 1674464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 1675be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 167602a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 1677be2ac88cSJonathan Lemon } 16785396d0f8SAndre Oppermann /* 16795396d0f8SAndre Oppermann * Initial send window. It will be updated with 16805396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 16815396d0f8SAndre Oppermann */ 16825396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 1683be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1684be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1685be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1686d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1687be2ac88cSJonathan Lemon } 1688be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1689be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 16903529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 16913529149eSAndre Oppermann (to.to_flags & TOF_SACKPERM) == 0) 16923529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 16936d90faf3SPaul Saab } 16946d90faf3SPaul Saab 1695df8bae1dSRodney W. Grimes /* 1696df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1697df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1698df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1699df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1700df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1701df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1702df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1703df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1704df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1705df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1706df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1707df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1708a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1709c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 1710a0292f23SGarrett Wollman * be TH_NEEDSYN. 1711df8bae1dSRodney W. Grimes */ 1712df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1713c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 1714fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1715c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 1716c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 1717a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 17184741bfcbSPatrick Kelsey LIST_EMPTY(&tp->t_segq) && 17194741bfcbSPatrick Kelsey ((to.to_flags & TOF_TS) == 0 || 1720c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1721df8bae1dSRodney W. Grimes 1722df8bae1dSRodney W. Grimes /* 1723df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1724df8bae1dSRodney W. Grimes * record the timestamp. 1725a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1726a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1727df8bae1dSRodney W. Grimes */ 1728be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1729fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1730d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1731a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1732df8bae1dSRodney W. Grimes } 1733df8bae1dSRodney W. Grimes 1734fb59c426SYoshinobu Inoue if (tlen == 0) { 1735fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1736fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1737dbc42409SLawrence Stewart !IN_RECOVERY(tp->t_flags) && 1738fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1739dbc42409SLawrence Stewart TAILQ_EMPTY(&tp->snd_holes)) { 1740df8bae1dSRodney W. Grimes /* 1741e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1742df8bae1dSRodney W. Grimes */ 1743ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 1744ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1745252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1746252ca428SRobert Watson 174778b50714SRobert Watson TCPSTAT_INC(tcps_predack); 1748252ca428SRobert Watson 17499b8b58e0SJonathan Lemon /* 1750e8949f74SAndre Oppermann * "bad retransmit" recovery. 17519b8b58e0SJonathan Lemon */ 17529b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 1753672dc4aeSJohn Baldwin tp->t_flags & TF_PREVVALID && 17546dfb8b31SJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) { 1755dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 17569b8b58e0SJonathan Lemon } 1757fa55172bSMatthew Dillon 1758fa55172bSMatthew Dillon /* 1759fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1760fa55172bSMatthew Dillon * 1761fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1762fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1763fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1764fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1765fa55172bSMatthew Dillon */ 1766fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1767fa55172bSMatthew Dillon to.to_tsecr) { 17683ac12506SJonathan T. Looney uint32_t t; 1769d8951c8aSBjoern A. Zeeb 1770d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 1771d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 1772d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 1773a0292f23SGarrett Wollman tcp_xmit_timer(tp, 1774d8951c8aSBjoern A. Zeeb TCP_TS_TO_TICKS(t) + 1); 1775fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1776fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1777eaf80179SAndre Oppermann if (!tp->t_rttlow || 1778eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1779eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1780c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1781c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1782fa55172bSMatthew Dillon } 1783dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 178439bc9de5SLawrence Stewart 1785bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 178639bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 178739bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 1788bd79708dSJonathan T. Looney #endif 178939bc9de5SLawrence Stewart 17904b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvackpack, nsegs); 179178b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 1792df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 17939d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 17949d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 17959d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 1796dbc42409SLawrence Stewart 1797dbc42409SLawrence Stewart /* 1798dbc42409SLawrence Stewart * Let the congestion control algorithm update 1799dbc42409SLawrence Stewart * congestion control related information. This 1800dbc42409SLawrence Stewart * typically means increasing the congestion 1801dbc42409SLawrence Stewart * window. 1802dbc42409SLawrence Stewart */ 18034b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, CC_ACK); 1804dbc42409SLawrence Stewart 18059d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 18061645d090SJeff Roberson /* 1807e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 18081645d090SJeff Roberson * to th_ack. 18091645d090SJeff Roberson */ 1810cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1811b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1812df8bae1dSRodney W. Grimes m_freem(m); 1813df8bae1dSRodney W. Grimes 1814df8bae1dSRodney W. Grimes /* 1815df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1816df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1817df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1818df8bae1dSRodney W. Grimes * If process is waiting for space, 1819df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1820df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1821df8bae1dSRodney W. Grimes * decide between more output or persist. 1822e8949f74SAndre Oppermann */ 18233c653157SHartmut Brandt #ifdef TCPDEBUG 18243c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 18253c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 18263c653157SHartmut Brandt (void *)tcp_saveipgen, 18273c653157SHartmut Brandt &tcp_savetcp, 0); 18283c653157SHartmut Brandt #endif 1829*2b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 1830df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1831b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1832b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1833b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1834b8152ba7SAndre Oppermann tp->t_rxtcur); 1835df8bae1dSRodney W. Grimes sowwakeup(so); 1836cfa6009eSGleb Smirnoff if (sbavail(&so->so_snd)) 183755bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 1838a14c749fSJonathan Lemon goto check_delack; 1839df8bae1dSRodney W. Grimes } 1840fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1841fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 18426741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 18436741ecf5SAndre Oppermann 1844df8bae1dSRodney W. Grimes /* 1845252ca428SRobert Watson * This is a pure, in-sequence data packet with 1846252ca428SRobert Watson * nothing on the reassembly queue and we have enough 1847252ca428SRobert Watson * buffer space to take it. 1848df8bae1dSRodney W. Grimes */ 1849ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 1850ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1851252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1852252ca428SRobert Watson 18536d90faf3SPaul Saab /* Clean receiver SACK report if present */ 18543529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 18556d90faf3SPaul Saab tcp_clean_sackreport(tp); 185678b50714SRobert Watson TCPSTAT_INC(tcps_preddat); 1857fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 18581645d090SJeff Roberson /* 18591645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 18601645d090SJeff Roberson * th_seq. 18611645d090SJeff Roberson */ 186260ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 18631645d090SJeff Roberson /* 18641645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 18651645d090SJeff Roberson * rcv_nxt. 18661645d090SJeff Roberson */ 18671645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 18684b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvpack, nsegs); 186978b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 18703c653157SHartmut Brandt #ifdef TCPDEBUG 18713c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 18723c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 18733c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 18743c653157SHartmut Brandt #endif 1875*2b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 18765d06879aSGeorge V. Neville-Neil 18776741ecf5SAndre Oppermann /* 18786741ecf5SAndre Oppermann * Automatic sizing of receive socket buffer. Often the send 18796741ecf5SAndre Oppermann * buffer size is not optimally adjusted to the actual network 18806741ecf5SAndre Oppermann * conditions at hand (delay bandwidth product). Setting the 18816741ecf5SAndre Oppermann * buffer size too small limits throughput on links with high 18826741ecf5SAndre Oppermann * bandwidth and high delay (eg. trans-continental/oceanic links). 18836741ecf5SAndre Oppermann * 18846741ecf5SAndre Oppermann * On the receive side the socket buffer memory is only rarely 18856741ecf5SAndre Oppermann * used to any significant extent. This allows us to be much 18866741ecf5SAndre Oppermann * more aggressive in scaling the receive socket buffer. For 18876741ecf5SAndre Oppermann * the case that the buffer space is actually used to a large 18886741ecf5SAndre Oppermann * extent and we run out of kernel memory we can simply drop 18896741ecf5SAndre Oppermann * the new segments; TCP on the sender will just retransmit it 18906741ecf5SAndre Oppermann * later. Setting the buffer size too big may only consume too 18916741ecf5SAndre Oppermann * much kernel memory if the application doesn't read() from 18926741ecf5SAndre Oppermann * the socket or packet loss or reordering makes use of the 18936741ecf5SAndre Oppermann * reassembly queue. 18946741ecf5SAndre Oppermann * 18956741ecf5SAndre Oppermann * The criteria to step up the receive buffer one notch are: 1896f7469d3eSHiren Panchasara * 1. Application has not set receive buffer size with 1897f7469d3eSHiren Panchasara * SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE. 1898f7469d3eSHiren Panchasara * 2. the number of bytes received during the time it takes 18996741ecf5SAndre Oppermann * one timestamp to be reflected back to us (the RTT); 1900f7469d3eSHiren Panchasara * 3. received bytes per RTT is within seven eighth of the 19016741ecf5SAndre Oppermann * current socket buffer size; 1902f7469d3eSHiren Panchasara * 4. receive buffer size has not hit maximal automatic size; 19036741ecf5SAndre Oppermann * 19046741ecf5SAndre Oppermann * This algorithm does one step per RTT at most and only if 19056741ecf5SAndre Oppermann * we receive a bulk stream w/o packet losses or reorderings. 19066741ecf5SAndre Oppermann * Shrinking the buffer during idle times is not necessary as 19076741ecf5SAndre Oppermann * it doesn't consume any memory when idle. 19086741ecf5SAndre Oppermann * 19096741ecf5SAndre Oppermann * TODO: Only step up if the application is actually serving 19106741ecf5SAndre Oppermann * the buffer to better manage the socket buffer resources. 1911df8bae1dSRodney W. Grimes */ 1912603724d3SBjoern A. Zeeb if (V_tcp_do_autorcvbuf && 1913d57724fdSPatrick Kelsey (to.to_flags & TOF_TS) && 19146741ecf5SAndre Oppermann to.to_tsecr && 19156741ecf5SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 19168502ec25SAndre Oppermann if (TSTMP_GT(to.to_tsecr, tp->rfbuf_ts) && 19176741ecf5SAndre Oppermann to.to_tsecr - tp->rfbuf_ts < hz) { 19186741ecf5SAndre Oppermann if (tp->rfbuf_cnt > 19196741ecf5SAndre Oppermann (so->so_rcv.sb_hiwat / 8 * 7) && 19206741ecf5SAndre Oppermann so->so_rcv.sb_hiwat < 1921603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max) { 19226741ecf5SAndre Oppermann newsize = 19236741ecf5SAndre Oppermann min(so->so_rcv.sb_hiwat + 1924603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_inc, 1925603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max); 19266741ecf5SAndre Oppermann } 19276741ecf5SAndre Oppermann /* Start over with next RTT. */ 19286741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 19296741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 19306741ecf5SAndre Oppermann } else 19316741ecf5SAndre Oppermann tp->rfbuf_cnt += tlen; /* add up */ 19326741ecf5SAndre Oppermann } 19336741ecf5SAndre Oppermann 19346741ecf5SAndre Oppermann /* Add data to socket buffer. */ 19351e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1936c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1937c1c36a2cSMike Silbersack m_freem(m); 1938c1c36a2cSMike Silbersack } else { 19396741ecf5SAndre Oppermann /* 19406741ecf5SAndre Oppermann * Set new socket buffer size. 19416741ecf5SAndre Oppermann * Give up when limit is reached. 19426741ecf5SAndre Oppermann */ 19436741ecf5SAndre Oppermann if (newsize) 19446741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 19456c8286e4SRobert Watson newsize, so, NULL)) 19466741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1947fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 1948651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 1949c1c36a2cSMike Silbersack } 1950e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 19511e4d7da7SRobert Watson sorwakeup_locked(so); 1952c1e5a6e5SAndre Oppermann if (DELAY_ACK(tp, tlen)) { 19533bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1954f498eeeeSDavid Greenman } else { 1955e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 195655bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 1957e612a582SDavid Greenman } 1958a14c749fSJonathan Lemon goto check_delack; 1959df8bae1dSRodney W. Grimes } 1960df8bae1dSRodney W. Grimes } 1961df8bae1dSRodney W. Grimes 1962df8bae1dSRodney W. Grimes /* 1963df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1964df8bae1dSRodney W. Grimes * and then do TCP input processing. 1965df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1966df8bae1dSRodney W. Grimes * but not less than advertised window. 1967df8bae1dSRodney W. Grimes */ 1968df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1969df8bae1dSRodney W. Grimes if (win < 0) 1970df8bae1dSRodney W. Grimes win = 0; 197166e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1972df8bae1dSRodney W. Grimes 19736741ecf5SAndre Oppermann /* Reset receive buffer auto scaling when not in bulk receive mode. */ 19746741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 19756741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 19766741ecf5SAndre Oppermann 1977df8bae1dSRodney W. Grimes switch (tp->t_state) { 1978df8bae1dSRodney W. Grimes 1979df8bae1dSRodney W. Grimes /* 1980764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1981764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1982764d8cefSBill Fenner */ 1983764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1984fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1985fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1986a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1987a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1988a57815efSBosko Milekic goto dropwithreset; 1989a57815efSBosko Milekic } 1990281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 199168bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) { 1992281a0fd4SPatrick Kelsey /* 1993281a0fd4SPatrick Kelsey * When a TFO connection is in SYN_RECEIVED, the 1994281a0fd4SPatrick Kelsey * only valid packets are the initial SYN, a 1995281a0fd4SPatrick Kelsey * retransmit/copy of the initial SYN (possibly with 1996281a0fd4SPatrick Kelsey * a subset of the original data), a valid ACK, a 1997281a0fd4SPatrick Kelsey * FIN, or a RST. 1998281a0fd4SPatrick Kelsey */ 1999281a0fd4SPatrick Kelsey if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) { 2000281a0fd4SPatrick Kelsey rstreason = BANDLIM_RST_OPENPORT; 2001281a0fd4SPatrick Kelsey goto dropwithreset; 2002281a0fd4SPatrick Kelsey } else if (thflags & TH_SYN) { 2003281a0fd4SPatrick Kelsey /* non-initial SYN is ignored */ 2004281a0fd4SPatrick Kelsey if ((tcp_timer_active(tp, TT_DELACK) || 2005281a0fd4SPatrick Kelsey tcp_timer_active(tp, TT_REXMT))) 2006281a0fd4SPatrick Kelsey goto drop; 2007281a0fd4SPatrick Kelsey } else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) { 2008281a0fd4SPatrick Kelsey goto drop; 2009281a0fd4SPatrick Kelsey } 2010281a0fd4SPatrick Kelsey } 2011281a0fd4SPatrick Kelsey #endif 2012764d8cefSBill Fenner break; 2013764d8cefSBill Fenner 2014764d8cefSBill Fenner /* 2015df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 2016df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 2017df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 2018df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 2019df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 2020df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 2021df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 2022f2512ba1SRui Paulo * if seg contains an ECE and ECN support is enabled, the stream 2023f2512ba1SRui Paulo * is ECN capable. 2024df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 2025df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 2026df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 2027df8bae1dSRodney W. Grimes */ 2028df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 2029fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 2030fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 2031fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 2032a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2033a0292f23SGarrett Wollman goto dropwithreset; 2034a0292f23SGarrett Wollman } 203557f60867SMark Johnston if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) { 2036d9fae5abSAndriy Gapon TCP_PROBE5(connect__refused, NULL, tp, 2037*2b9c9984SGeorge V. Neville-Neil m, tp, th); 2038df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 203957f60867SMark Johnston } 20400ca3f933SAndre Oppermann if (thflags & TH_RST) 2041df8bae1dSRodney W. Grimes goto drop; 20420ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 2043df8bae1dSRodney W. Grimes goto drop; 2044464fcfbcSAndre Oppermann 2045fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 2046df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 2047fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 204878b50714SRobert Watson TCPSTAT_INC(tcps_connects); 2049845799c1SAndras Olah soisconnected(so); 2050c488362eSRobert Watson #ifdef MAC 205130d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 2052c488362eSRobert Watson #endif 2053845799c1SAndras Olah /* Do window scaling on this connection? */ 2054845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2055845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2056845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 2057845799c1SAndras Olah } 20583ac12506SJonathan T. Looney tp->rcv_adv += min(tp->rcv_wnd, 2059766282cbSJohn Baldwin TCP_MAXWIN << tp->rcv_scale); 2060a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 2061a0292f23SGarrett Wollman /* 2062a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 2063a0292f23SGarrett Wollman * ACKNOW will be turned on later. 2064a0292f23SGarrett Wollman */ 2065c1e5a6e5SAndre Oppermann if (DELAY_ACK(tp, tlen) && tlen != 0) 2066b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 2067b8152ba7SAndre Oppermann tcp_delacktime); 2068a0292f23SGarrett Wollman else 2069a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2070f2512ba1SRui Paulo 2071603724d3SBjoern A. Zeeb if ((thflags & TH_ECE) && V_tcp_do_ecn) { 2072f2512ba1SRui Paulo tp->t_flags |= TF_ECN_PERMIT; 207378b50714SRobert Watson TCPSTAT_INC(tcps_ecn_shs); 2074f2512ba1SRui Paulo } 2075f2512ba1SRui Paulo 2076a0292f23SGarrett Wollman /* 2077a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 2078a0292f23SGarrett Wollman * Transitions: 2079a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 2080a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 2081a0292f23SGarrett Wollman */ 20829b8b58e0SJonathan Lemon tp->t_starttime = ticks; 2083a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 208457f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2085a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 2086fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 20877ff19458SPaul Traina } else { 208857f60867SMark Johnston tcp_state_change(tp, TCPS_ESTABLISHED); 2089d9fae5abSAndriy Gapon TCP_PROBE5(connect__established, NULL, tp, 2090*2b9c9984SGeorge V. Neville-Neil m, tp, th); 2091dbc42409SLawrence Stewart cc_conn_init(tp); 20929077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 20939077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 20947ff19458SPaul Traina } 2095a0292f23SGarrett Wollman } else { 2096a0292f23SGarrett Wollman /* 2097c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 2098153edc50SHiren Panchasara * simultaneous open. 2099c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 2100c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 2101a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 2102a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 2103a0292f23SGarrett Wollman */ 21044b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 2105b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 210657f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_RECEIVED); 2107a0292f23SGarrett Wollman } 2108df8bae1dSRodney W. Grimes 2109ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, ("%s: trimthenstep6: " 2110252ca428SRobert Watson "ti_locked %d", __func__, ti_locked)); 2111ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 21128501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 21137cfc6904SRobert Watson 2114df8bae1dSRodney W. Grimes /* 2115fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 2116df8bae1dSRodney W. Grimes * If data, trim to stay within window, 2117df8bae1dSRodney W. Grimes * dropping FIN if necessary. 2118df8bae1dSRodney W. Grimes */ 2119fb59c426SYoshinobu Inoue th->th_seq++; 2120fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 2121fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 2122df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2123fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 2124fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 212578b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 212678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2127df8bae1dSRodney W. Grimes } 2128fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 2129fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 2130a0292f23SGarrett Wollman /* 2131a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 2132a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 2133a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 2134a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 2135a0292f23SGarrett Wollman * Otherwise, goto step 6. 2136a0292f23SGarrett Wollman */ 2137fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 2138a0292f23SGarrett Wollman goto process_ACK; 2139c068736aSJeffrey Hsu 2140df8bae1dSRodney W. Grimes goto step6; 2141c068736aSJeffrey Hsu 2142a0292f23SGarrett Wollman /* 2143a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 2144c94c54e4SAndre Oppermann * do normal processing. 2145a0292f23SGarrett Wollman * 2146c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 2147a0292f23SGarrett Wollman */ 2148a0292f23SGarrett Wollman case TCPS_LAST_ACK: 2149a0292f23SGarrett Wollman case TCPS_CLOSING: 2150a0292f23SGarrett Wollman break; /* continue normal processing */ 2151df8bae1dSRodney W. Grimes } 2152df8bae1dSRodney W. Grimes 2153df8bae1dSRodney W. Grimes /* 2154df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 215580ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 215680ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 215780ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 215880ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 215980ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 216080ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 216180ab7c0eSGarrett Wollman * killing a connection). 216280ab7c0eSGarrett Wollman * Then check timestamp, if present. 2163a0292f23SGarrett Wollman * Then check the connection count, if present. 2164df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 2165df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 2166df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 216780ab7c0eSGarrett Wollman */ 2168fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 21693220a212SGleb Smirnoff /* 21703220a212SGleb Smirnoff * RFC5961 Section 3.2 21713220a212SGleb Smirnoff * 21723220a212SGleb Smirnoff * - RST drops connection only if SEG.SEQ == RCV.NXT. 21733220a212SGleb Smirnoff * - If RST is in window, we send challenge ACK. 21743220a212SGleb Smirnoff * 21753220a212SGleb Smirnoff * Note: to take into account delayed ACKs, we should 21763220a212SGleb Smirnoff * test against last_ack_sent instead of rcv_nxt. 21773220a212SGleb Smirnoff * Note 2: we handle special case of closed window, not 21783220a212SGleb Smirnoff * covered by the RFC. 21793220a212SGleb Smirnoff */ 21803220a212SGleb Smirnoff if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 21813220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) || 21823220a212SGleb Smirnoff (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) { 218380ab7c0eSGarrett Wollman 2184ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2185ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, 21863220a212SGleb Smirnoff ("%s: TH_RST ti_locked %d, th %p tp %p", 21873220a212SGleb Smirnoff __func__, ti_locked, th, tp)); 21883220a212SGleb Smirnoff KASSERT(tp->t_state != TCPS_SYN_SENT, 21893220a212SGleb Smirnoff ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p", 21903220a212SGleb Smirnoff __func__, th, tp)); 21913220a212SGleb Smirnoff 21923220a212SGleb Smirnoff if (V_tcp_insecure_rst || 21933220a212SGleb Smirnoff tp->last_ack_sent == th->th_seq) { 21943220a212SGleb Smirnoff TCPSTAT_INC(tcps_drops); 21953220a212SGleb Smirnoff /* Drop the connection. */ 21963220a212SGleb Smirnoff switch (tp->t_state) { 219780ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 219880ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 219980ab7c0eSGarrett Wollman goto close; 220080ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 220180ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 220280ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 220380ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 22046779a1a1SMichael Tuexen case TCPS_CLOSING: 22056779a1a1SMichael Tuexen case TCPS_LAST_ACK: 220680ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 220780ab7c0eSGarrett Wollman close: 22083220a212SGleb Smirnoff /* FALLTHROUGH */ 22093220a212SGleb Smirnoff default: 221080ab7c0eSGarrett Wollman tp = tcp_close(tp); 22113220a212SGleb Smirnoff } 22123220a212SGleb Smirnoff } else { 22133220a212SGleb Smirnoff TCPSTAT_INC(tcps_badrst); 22143220a212SGleb Smirnoff /* Send challenge ACK. */ 22153220a212SGleb Smirnoff tcp_respond(tp, mtod(m, void *), th, m, 22163220a212SGleb Smirnoff tp->rcv_nxt, tp->snd_nxt, TH_ACK); 22173220a212SGleb Smirnoff tp->last_ack_sent = tp->rcv_nxt; 22183220a212SGleb Smirnoff m = NULL; 22193220a212SGleb Smirnoff } 22203220a212SGleb Smirnoff } 22213220a212SGleb Smirnoff goto drop; 22223220a212SGleb Smirnoff } 222380ab7c0eSGarrett Wollman 22243220a212SGleb Smirnoff /* 22253220a212SGleb Smirnoff * RFC5961 Section 4.2 22263220a212SGleb Smirnoff * Send challenge ACK for any SYN in synchronized state. 22273220a212SGleb Smirnoff */ 2228281a0fd4SPatrick Kelsey if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT && 2229281a0fd4SPatrick Kelsey tp->t_state != TCPS_SYN_RECEIVED) { 2230ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, 22313220a212SGleb Smirnoff ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked)); 2232ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2233252ca428SRobert Watson 22343220a212SGleb Smirnoff TCPSTAT_INC(tcps_badsyn); 22353220a212SGleb Smirnoff if (V_tcp_insecure_syn && 22363220a212SGleb Smirnoff SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 22373220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 22383220a212SGleb Smirnoff tp = tcp_drop(tp, ECONNRESET); 22393220a212SGleb Smirnoff rstreason = BANDLIM_UNLIMITED; 22403220a212SGleb Smirnoff } else { 22413220a212SGleb Smirnoff /* Send challenge ACK. */ 22423220a212SGleb Smirnoff tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt, 22433220a212SGleb Smirnoff tp->snd_nxt, TH_ACK); 22443220a212SGleb Smirnoff tp->last_ack_sent = tp->rcv_nxt; 22453220a212SGleb Smirnoff m = NULL; 224680ab7c0eSGarrett Wollman } 224780ab7c0eSGarrett Wollman goto drop; 224880ab7c0eSGarrett Wollman } 224980ab7c0eSGarrett Wollman 225080ab7c0eSGarrett Wollman /* 2251df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 2252df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 2253df8bae1dSRodney W. Grimes */ 2254be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 225580ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 2256df8bae1dSRodney W. Grimes 2257df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 2258d8951c8aSBjoern A. Zeeb if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 2259df8bae1dSRodney W. Grimes /* 2260df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 2261df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 2262df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 2263df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 2264df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 2265df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 2266df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 2267df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 2268df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 2269df8bae1dSRodney W. Grimes */ 2270df8bae1dSRodney W. Grimes tp->ts_recent = 0; 2271df8bae1dSRodney W. Grimes } else { 227278b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 227378b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 227478b50714SRobert Watson TCPSTAT_INC(tcps_pawsdrop); 227507fd333dSMatthew Dillon if (tlen) 2276df8bae1dSRodney W. Grimes goto dropafterack; 22771ab4789dSMatthew Dillon goto drop; 22781ab4789dSMatthew Dillon } 2279df8bae1dSRodney W. Grimes } 2280df8bae1dSRodney W. Grimes 2281a0292f23SGarrett Wollman /* 228280ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 228380ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 228480ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 228580ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 228680ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 228780ab7c0eSGarrett Wollman */ 2288a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 2289a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2290a57815efSBosko Milekic goto dropwithreset; 2291a57815efSBosko Milekic } 229280ab7c0eSGarrett Wollman 2293fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 2294df8bae1dSRodney W. Grimes if (todrop > 0) { 2295831ad37eSXin LI if (thflags & TH_SYN) { 2296fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 2297fb59c426SYoshinobu Inoue th->th_seq++; 2298fb59c426SYoshinobu Inoue if (th->th_urp > 1) 2299fb59c426SYoshinobu Inoue th->th_urp--; 2300df8bae1dSRodney W. Grimes else 2301fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2302df8bae1dSRodney W. Grimes todrop--; 2303df8bae1dSRodney W. Grimes } 2304df8bae1dSRodney W. Grimes /* 2305dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 2306df8bae1dSRodney W. Grimes */ 2307fb59c426SYoshinobu Inoue if (todrop > tlen 2308fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 2309dac20301SGarrett Wollman /* 2310dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 2311dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 2312dac20301SGarrett Wollman * of sequence; drop it. 2313dac20301SGarrett Wollman */ 2314fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2315dac20301SGarrett Wollman 2316df8bae1dSRodney W. Grimes /* 2317dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 2318dac20301SGarrett Wollman * But keep on processing for RST or ACK. 2319df8bae1dSRodney W. Grimes */ 2320dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2321fb59c426SYoshinobu Inoue todrop = tlen; 232278b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 232378b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 2324df8bae1dSRodney W. Grimes } else { 232578b50714SRobert Watson TCPSTAT_INC(tcps_rcvpartduppack); 232678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 2327df8bae1dSRodney W. Grimes } 2328fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 2329fb59c426SYoshinobu Inoue th->th_seq += todrop; 2330fb59c426SYoshinobu Inoue tlen -= todrop; 2331fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 2332fb59c426SYoshinobu Inoue th->th_urp -= todrop; 2333df8bae1dSRodney W. Grimes else { 2334fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2335fb59c426SYoshinobu Inoue th->th_urp = 0; 2336df8bae1dSRodney W. Grimes } 2337df8bae1dSRodney W. Grimes } 2338df8bae1dSRodney W. Grimes 2339df8bae1dSRodney W. Grimes /* 2340df8bae1dSRodney W. Grimes * If new data are received on a connection after the 2341df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 2342df8bae1dSRodney W. Grimes */ 2343df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 2344fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 2345ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, ("%s: SS_NOFDEREF && " 2346252ca428SRobert Watson "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked)); 2347ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2348252ca428SRobert Watson 234907dacf03SAndre Oppermann if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 235007dacf03SAndre Oppermann log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data " 235107dacf03SAndre Oppermann "after socket was closed, " 235207dacf03SAndre Oppermann "sending RST and removing tcpcb\n", 2353e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 2354773673c1SAndre Oppermann free(s, M_TCPLOG); 2355773673c1SAndre Oppermann } 2356df8bae1dSRodney W. Grimes tp = tcp_close(tp); 235778b50714SRobert Watson TCPSTAT_INC(tcps_rcvafterclose); 2358a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2359df8bae1dSRodney W. Grimes goto dropwithreset; 2360df8bae1dSRodney W. Grimes } 2361df8bae1dSRodney W. Grimes 2362df8bae1dSRodney W. Grimes /* 2363df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 2364df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 2365df8bae1dSRodney W. Grimes */ 2366fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 2367df8bae1dSRodney W. Grimes if (todrop > 0) { 236878b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 2369fb59c426SYoshinobu Inoue if (todrop >= tlen) { 237078b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 2371df8bae1dSRodney W. Grimes /* 2372df8bae1dSRodney W. Grimes * If window is closed can only take segments at 2373df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 2374df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 2375df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 2376df8bae1dSRodney W. Grimes * and ack. 2377df8bae1dSRodney W. Grimes */ 2378fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2379df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 238078b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinprobe); 2381df8bae1dSRodney W. Grimes } else 2382df8bae1dSRodney W. Grimes goto dropafterack; 2383df8bae1dSRodney W. Grimes } else 238478b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2385df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2386fb59c426SYoshinobu Inoue tlen -= todrop; 2387fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 2388df8bae1dSRodney W. Grimes } 2389df8bae1dSRodney W. Grimes 2390df8bae1dSRodney W. Grimes /* 2391df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 2392df8bae1dSRodney W. Grimes * record its timestamp. 2393cf09195bSPaul Saab * NOTE: 2394cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 2395a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2396cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 2397cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 2398cf09195bSPaul Saab * predicated on the sequence space of this segment. 2399cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 2400cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 2401cf09195bSPaul Saab * instead of RFC1323's 2402cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 2403cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 2404cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 2405cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 2406cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 2407df8bae1dSRodney W. Grimes */ 2408be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 2409cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2410cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 2411cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 2412d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 2413a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 2414df8bae1dSRodney W. Grimes } 2415df8bae1dSRodney W. Grimes 2416df8bae1dSRodney W. Grimes /* 2417a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 2418a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 2419a0292f23SGarrett Wollman * later processing; else drop segment and return. 2420a0292f23SGarrett Wollman */ 2421fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 2422a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 2423281a0fd4SPatrick Kelsey (tp->t_flags & TF_NEEDSYN)) { 2424281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 2425281a0fd4SPatrick Kelsey if (tp->t_state == TCPS_SYN_RECEIVED && 242668bd7ed1SJonathan T. Looney IS_FASTOPEN(tp->t_flags)) { 2427281a0fd4SPatrick Kelsey tp->snd_wnd = tiwin; 2428281a0fd4SPatrick Kelsey cc_conn_init(tp); 2429281a0fd4SPatrick Kelsey } 2430281a0fd4SPatrick Kelsey #endif 2431a0292f23SGarrett Wollman goto step6; 2432281a0fd4SPatrick Kelsey } else if (tp->t_flags & TF_ACKNOW) 24335e1aa279SDavid Malone goto dropafterack; 2434a0292f23SGarrett Wollman else 2435a0292f23SGarrett Wollman goto drop; 2436a0292f23SGarrett Wollman } 2437df8bae1dSRodney W. Grimes 2438df8bae1dSRodney W. Grimes /* 2439df8bae1dSRodney W. Grimes * Ack processing. 2440df8bae1dSRodney W. Grimes */ 2441df8bae1dSRodney W. Grimes switch (tp->t_state) { 2442df8bae1dSRodney W. Grimes 2443df8bae1dSRodney W. Grimes /* 2444764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 2445764d8cefSBill Fenner * ESTABLISHED state and continue processing. 2446764d8cefSBill Fenner * The ACK was checked above. 2447df8bae1dSRodney W. Grimes */ 2448df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2449a0292f23SGarrett Wollman 245078b50714SRobert Watson TCPSTAT_INC(tcps_connects); 2451df8bae1dSRodney W. Grimes soisconnected(so); 2452df8bae1dSRodney W. Grimes /* Do window scaling? */ 2453df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2454df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2455df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 2456464fcfbcSAndre Oppermann tp->snd_wnd = tiwin; 2457df8bae1dSRodney W. Grimes } 2458a0292f23SGarrett Wollman /* 2459a0292f23SGarrett Wollman * Make transitions: 2460a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 2461a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 2462a0292f23SGarrett Wollman */ 24639b8b58e0SJonathan Lemon tp->t_starttime = ticks; 2464a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 246557f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2466a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 24677ff19458SPaul Traina } else { 246857f60867SMark Johnston tcp_state_change(tp, TCPS_ESTABLISHED); 2469d9fae5abSAndriy Gapon TCP_PROBE5(accept__established, NULL, tp, 2470*2b9c9984SGeorge V. Neville-Neil m, tp, th); 2471281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 2472281a0fd4SPatrick Kelsey if (tp->t_tfo_pending) { 2473281a0fd4SPatrick Kelsey tcp_fastopen_decrement_counter(tp->t_tfo_pending); 2474281a0fd4SPatrick Kelsey tp->t_tfo_pending = NULL; 2475281a0fd4SPatrick Kelsey 2476281a0fd4SPatrick Kelsey /* 2477281a0fd4SPatrick Kelsey * Account for the ACK of our SYN prior to 2478281a0fd4SPatrick Kelsey * regular ACK processing below. 2479281a0fd4SPatrick Kelsey */ 2480281a0fd4SPatrick Kelsey tp->snd_una++; 2481281a0fd4SPatrick Kelsey } 2482281a0fd4SPatrick Kelsey /* 2483281a0fd4SPatrick Kelsey * TFO connections call cc_conn_init() during SYN 2484281a0fd4SPatrick Kelsey * processing. Calling it again here for such 2485281a0fd4SPatrick Kelsey * connections is not harmless as it would undo the 2486281a0fd4SPatrick Kelsey * snd_cwnd reduction that occurs when a TFO SYN|ACK 2487281a0fd4SPatrick Kelsey * is retransmitted. 2488281a0fd4SPatrick Kelsey */ 248968bd7ed1SJonathan T. Looney if (!IS_FASTOPEN(tp->t_flags)) 2490281a0fd4SPatrick Kelsey #endif 2491dbc42409SLawrence Stewart cc_conn_init(tp); 24929077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 24937ff19458SPaul Traina } 2494a0292f23SGarrett Wollman /* 2495a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 2496a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 2497a0292f23SGarrett Wollman */ 2498fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 2499fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 2500a0292f23SGarrett Wollman (struct mbuf *)0); 250160ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq - 1; 250293b0017fSPhilippe Charnier /* FALLTHROUGH */ 2503df8bae1dSRodney W. Grimes 2504df8bae1dSRodney W. Grimes /* 2505df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2506df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 2507fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 2508fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 2509df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 2510df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 2511df8bae1dSRodney W. Grimes */ 2512df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2513df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2514df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2515df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 2516df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2517df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 25185a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 251978b50714SRobert Watson TCPSTAT_INC(tcps_rcvacktoomuch); 25205a53ca16SPaul Saab goto dropafterack; 25215a53ca16SPaul Saab } 25223529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2523fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 2524fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 2525021eaf79SHiren Panchasara sack_changed = tcp_sack_doack(tp, &to, th->th_ack); 252612eeb81fSHiren Panchasara else 252712eeb81fSHiren Panchasara /* 252812eeb81fSHiren Panchasara * Reset the value so that previous (valid) value 252912eeb81fSHiren Panchasara * from the last ack with SACK doesn't get used. 253012eeb81fSHiren Panchasara */ 253112eeb81fSHiren Panchasara tp->sackhint.sacked_bytes = 0; 253239bc9de5SLawrence Stewart 2533bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 253439bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 253539bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 2536bd79708dSJonathan T. Looney #endif 253739bc9de5SLawrence Stewart 2538fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 25390c39d38dSGleb Smirnoff u_int maxseg; 25400c39d38dSGleb Smirnoff 25410c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 2542021eaf79SHiren Panchasara if (tlen == 0 && 2543021eaf79SHiren Panchasara (tiwin == tp->snd_wnd || 2544021eaf79SHiren Panchasara (tp->t_flags & TF_SACK_PERMIT))) { 254562b90589SPeter Wemm /* 254662b90589SPeter Wemm * If this is the first time we've seen a 254762b90589SPeter Wemm * FIN from the remote, this is not a 254862b90589SPeter Wemm * duplicate and it needs to be processed 254962b90589SPeter Wemm * normally. This happens during a 255062b90589SPeter Wemm * simultaneous close. 255162b90589SPeter Wemm */ 255262b90589SPeter Wemm if ((thflags & TH_FIN) && 255362b90589SPeter Wemm (TCPS_HAVERCVDFIN(tp->t_state) == 0)) { 255462b90589SPeter Wemm tp->t_dupacks = 0; 255562b90589SPeter Wemm break; 255662b90589SPeter Wemm } 255778b50714SRobert Watson TCPSTAT_INC(tcps_rcvdupack); 2558df8bae1dSRodney W. Grimes /* 2559df8bae1dSRodney W. Grimes * If we have outstanding data (other than 2560df8bae1dSRodney W. Grimes * a window probe), this is a completely 2561df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 25625f30ec9bSEitan Adler * change and FIN isn't set), 25635f30ec9bSEitan Adler * the ack is the biggest we've 2564df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 2565a4641f4eSPedro F. Giffuni * threshold of them, assume a packet 2566df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 2567df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 2568df8bae1dSRodney W. Grimes * window so we send only this one 2569df8bae1dSRodney W. Grimes * packet. 2570df8bae1dSRodney W. Grimes * 2571df8bae1dSRodney W. Grimes * We know we're losing at the current 2572df8bae1dSRodney W. Grimes * window size so do congestion avoidance 2573df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 2574df8bae1dSRodney W. Grimes * and pull our congestion window back to 2575df8bae1dSRodney W. Grimes * the new ssthresh). 2576df8bae1dSRodney W. Grimes * 2577df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 2578df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 2579df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 2580df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 2581df8bae1dSRodney W. Grimes * network. 2582f2512ba1SRui Paulo * 2583f2512ba1SRui Paulo * When using TCP ECN, notify the peer that 2584f2512ba1SRui Paulo * we reduced the cwnd. 2585df8bae1dSRodney W. Grimes */ 2586021eaf79SHiren Panchasara /* 2587021eaf79SHiren Panchasara * Following 2 kinds of acks should not affect 2588021eaf79SHiren Panchasara * dupack counting: 2589021eaf79SHiren Panchasara * 1) Old acks 2590021eaf79SHiren Panchasara * 2) Acks with SACK but without any new SACK 2591021eaf79SHiren Panchasara * information in them. These could result from 2592021eaf79SHiren Panchasara * any anomaly in the network like a switch 2593021eaf79SHiren Panchasara * duplicating packets or a possible DoS attack. 2594021eaf79SHiren Panchasara */ 2595021eaf79SHiren Panchasara if (th->th_ack != tp->snd_una || 2596021eaf79SHiren Panchasara ((tp->t_flags & TF_SACK_PERMIT) && 2597021eaf79SHiren Panchasara !sack_changed)) 2598021eaf79SHiren Panchasara break; 2599021eaf79SHiren Panchasara else if (!tcp_timer_active(tp, TT_REXMT)) 2600df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2601cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 2602dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 26034b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 26044b7b743cSLawrence Stewart CC_DUPACK); 26053529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2606dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2607808f11b7SPaul Saab int awnd; 260825e6f9edSPaul Saab 260925e6f9edSPaul Saab /* 261025e6f9edSPaul Saab * Compute the amount of data in flight first. 26114c3972f0SHiren Panchasara * We can inject new data into the pipe iff 261225e6f9edSPaul Saab * we have less than 1/2 the original window's 261325e6f9edSPaul Saab * worth of data in flight. 261425e6f9edSPaul Saab */ 261512eeb81fSHiren Panchasara if (V_tcp_do_rfc6675_pipe) 261612eeb81fSHiren Panchasara awnd = tcp_compute_pipe(tp); 261712eeb81fSHiren Panchasara else 2618808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 26190077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 262012eeb81fSHiren Panchasara 2621808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 26220c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 26234e7f7553SHiren Panchasara /* 26244e7f7553SHiren Panchasara * RFC5681 Section 3.2 talks about cwnd 26254e7f7553SHiren Panchasara * inflation on additional dupacks and 26264e7f7553SHiren Panchasara * deflation on recovering from loss. 26274e7f7553SHiren Panchasara * 26284e7f7553SHiren Panchasara * We keep cwnd into check so that 26294e7f7553SHiren Panchasara * we don't have to 'deflate' it when we 26304e7f7553SHiren Panchasara * get out of recovery. 26314e7f7553SHiren Panchasara */ 263225e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 263325e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 263425e6f9edSPaul Saab } 263525e6f9edSPaul Saab } else 26360c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 263755bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 263846f58482SJonathan Lemon goto drop; 2639cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 2640cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 2641a0445c2eSJayanth Vijayaraghavan 2642a0445c2eSJayanth Vijayaraghavan /* 2643a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 2644a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 2645a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 2646a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 2647a0445c2eSJayanth Vijayaraghavan * recovery. 2648a0445c2eSJayanth Vijayaraghavan */ 26493529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 2650dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2651a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 2652a0445c2eSJayanth Vijayaraghavan break; 2653a0445c2eSJayanth Vijayaraghavan } 2654dbc42409SLawrence Stewart } else { 2655a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 26569d11646dSJeffrey Hsu tp->snd_recover)) { 2657cb942153SJeffrey Hsu tp->t_dupacks = 0; 2658cb942153SJeffrey Hsu break; 265946f58482SJonathan Lemon } 2660a0445c2eSJayanth Vijayaraghavan } 2661dbc42409SLawrence Stewart /* Congestion signal before ack. */ 2662dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_NDUPACK); 26634b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 26644b7b743cSLawrence Stewart CC_DUPACK); 2665b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 26669b8b58e0SJonathan Lemon tp->t_rtttime = 0; 26673529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 266878b50714SRobert Watson TCPSTAT_INC( 266978b50714SRobert Watson tcps_sack_recovery_episode); 2670a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 26714e7f7553SHiren Panchasara if (CC_ALGO(tp)->cong_signal == NULL) 26720c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 267355bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 26746d90faf3SPaul Saab goto drop; 26756d90faf3SPaul Saab } 2676fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 26774e7f7553SHiren Panchasara if (CC_ALGO(tp)->cong_signal == NULL) 26780c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 267955bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 268048d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 2681302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 2682302ce8d6SAndre Oppermann __func__)); 26834e7f7553SHiren Panchasara if (CC_ALGO(tp)->cong_signal == NULL) 2684df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 26850c39d38dSGleb Smirnoff maxseg * 268648d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 2687df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 2688df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 2689df8bae1dSRodney W. Grimes goto drop; 2690603724d3SBjoern A. Zeeb } else if (V_tcp_do_rfc3042) { 269162d4443fSHiren Panchasara /* 269262d4443fSHiren Panchasara * Process first and second duplicate 269362d4443fSHiren Panchasara * ACKs. Each indicates a segment 269462d4443fSHiren Panchasara * leaving the network, creating room 269562d4443fSHiren Panchasara * for more. Make sure we can send a 269662d4443fSHiren Panchasara * packet on reception of each duplicate 269762d4443fSHiren Panchasara * ACK by increasing snd_cwnd by one 269862d4443fSHiren Panchasara * segment. Restore the original 269962d4443fSHiren Panchasara * snd_cwnd after packet transmission. 270062d4443fSHiren Panchasara */ 27014b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 27024b7b743cSLawrence Stewart CC_DUPACK); 27033ac12506SJonathan T. Looney uint32_t oldcwnd = tp->snd_cwnd; 270448d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 270548d2549cSJeffrey Hsu u_int sent; 27065628dd08SAndre Oppermann int avail; 270789c02376SJeffrey Hsu 2708582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 2709582a954bSJeffrey Hsu tp->t_dupacks == 2, 2710302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 2711302ce8d6SAndre Oppermann __func__)); 271261a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 271348d2549cSJeffrey Hsu tp->snd_limited = 0; 271461a36e3dSJeffrey Hsu tp->snd_cwnd = 271561a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 271661a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 27170c39d38dSGleb Smirnoff maxseg; 27185628dd08SAndre Oppermann /* 27195628dd08SAndre Oppermann * Only call tcp_output when there 27205628dd08SAndre Oppermann * is new data available to be sent. 27215628dd08SAndre Oppermann * Otherwise we would send pure ACKs. 27225628dd08SAndre Oppermann */ 27235628dd08SAndre Oppermann SOCKBUF_LOCK(&so->so_snd); 2724cfa6009eSGleb Smirnoff avail = sbavail(&so->so_snd) - 27255628dd08SAndre Oppermann (tp->snd_nxt - tp->snd_una); 27265628dd08SAndre Oppermann SOCKBUF_UNLOCK(&so->so_snd); 27275628dd08SAndre Oppermann if (avail > 0) 272855bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 272948d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 27300c39d38dSGleb Smirnoff if (sent > maxseg) { 273189c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 273289c02376SJeffrey Hsu tp->snd_limited == 0) || 27330c39d38dSGleb Smirnoff (sent == maxseg + 1 && 273489c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 2735302ce8d6SAndre Oppermann ("%s: sent too much", 2736302ce8d6SAndre Oppermann __func__)); 273748d2549cSJeffrey Hsu tp->snd_limited = 2; 273848d2549cSJeffrey Hsu } else if (sent > 0) 273948d2549cSJeffrey Hsu ++tp->snd_limited; 2740582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 2741582a954bSJeffrey Hsu goto drop; 2742df8bae1dSRodney W. Grimes } 2743021eaf79SHiren Panchasara } 2744df8bae1dSRodney W. Grimes break; 2745021eaf79SHiren Panchasara } else { 2746021eaf79SHiren Panchasara /* 2747021eaf79SHiren Panchasara * This ack is advancing the left edge, reset the 2748021eaf79SHiren Panchasara * counter. 2749021eaf79SHiren Panchasara */ 2750021eaf79SHiren Panchasara tp->t_dupacks = 0; 2751021eaf79SHiren Panchasara /* 2752021eaf79SHiren Panchasara * If this ack also has new SACK info, increment the 2753021eaf79SHiren Panchasara * counter as per rfc6675. 2754021eaf79SHiren Panchasara */ 2755021eaf79SHiren Panchasara if ((tp->t_flags & TF_SACK_PERMIT) && sack_changed) 2756021eaf79SHiren Panchasara tp->t_dupacks++; 2757df8bae1dSRodney W. Grimes } 2758cb942153SJeffrey Hsu 2759302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2760302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 2761cb942153SJeffrey Hsu 2762df8bae1dSRodney W. Grimes /* 2763df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2764df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2765df8bae1dSRodney W. Grimes */ 2766dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2767cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 27683529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 27696d90faf3SPaul Saab tcp_sack_partialack(tp, th); 27706d90faf3SPaul Saab else 2771c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2772dbc42409SLawrence Stewart } else 2773dbc42409SLawrence Stewart cc_post_recovery(tp, th); 277446f58482SJonathan Lemon } 2775a0292f23SGarrett Wollman /* 2776a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2777a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2778a0292f23SGarrett Wollman */ 2779a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2780a0292f23SGarrett Wollman /* 2781a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2782a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 278307e43e10SAndras Olah * synchronized). Go to non-starred state, 278407e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 278507e43e10SAndras Olah * we can do window scaling. 2786a0292f23SGarrett Wollman */ 2787a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2788a0292f23SGarrett Wollman tp->snd_una++; 278907e43e10SAndras Olah /* Do window scaling? */ 279007e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 279107e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 279207e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2793464fcfbcSAndre Oppermann /* Send window already scaled. */ 279407e43e10SAndras Olah } 2795a0292f23SGarrett Wollman } 2796a0292f23SGarrett Wollman 2797a0292f23SGarrett Wollman process_ACK: 27988501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 27997cfc6904SRobert Watson 2800dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 2801b8c2cd15SJonathan T. Looney KASSERT(acked >= 0, ("%s: acked unexepectedly negative " 2802b8c2cd15SJonathan T. Looney "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__, 2803b8c2cd15SJonathan T. Looney tp->snd_una, th->th_ack, tp, m)); 28044b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvackpack, nsegs); 280578b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 2806df8bae1dSRodney W. Grimes 2807df8bae1dSRodney W. Grimes /* 28089b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 28099b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 28109b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 28119b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 28129b8b58e0SJonathan Lemon * we left off. 28139b8b58e0SJonathan Lemon */ 2814672dc4aeSJohn Baldwin if (tp->t_rxtshift == 1 && tp->t_flags & TF_PREVVALID && 2815672dc4aeSJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) 2816dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 28179b8b58e0SJonathan Lemon 28189b8b58e0SJonathan Lemon /* 2819df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2820df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2821df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2822df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2823df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2824df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2825df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2826fa55172bSMatthew Dillon * 2827fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2828fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2829fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2830fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2831df8bae1dSRodney W. Grimes */ 2832d8951c8aSBjoern A. Zeeb if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) { 28333ac12506SJonathan T. Looney uint32_t t; 2834d8951c8aSBjoern A. Zeeb 2835d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 2836d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 2837d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 2838d8951c8aSBjoern A. Zeeb tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1); 2839fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2840eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2841eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 28429b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2843fa55172bSMatthew Dillon } 2844df8bae1dSRodney W. Grimes 2845df8bae1dSRodney W. Grimes /* 2846df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2847df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2848df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2849df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2850df8bae1dSRodney W. Grimes */ 2851fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 2852b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2853df8bae1dSRodney W. Grimes needoutput = 1; 2854b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2855b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2856a0292f23SGarrett Wollman 2857a0292f23SGarrett Wollman /* 2858a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2859a0292f23SGarrett Wollman * skip rest of ACK processing. 2860a0292f23SGarrett Wollman */ 2861a0292f23SGarrett Wollman if (acked == 0) 2862a0292f23SGarrett Wollman goto step6; 2863a0292f23SGarrett Wollman 2864df8bae1dSRodney W. Grimes /* 2865dbc42409SLawrence Stewart * Let the congestion control algorithm update congestion 2866dbc42409SLawrence Stewart * control related information. This typically means increasing 2867dbc42409SLawrence Stewart * the congestion window. 2868df8bae1dSRodney W. Grimes */ 28694b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, CC_ACK); 2870dbc42409SLawrence Stewart 28715905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2872cfa6009eSGleb Smirnoff if (acked > sbavail(&so->so_snd)) { 2873b8c2cd15SJonathan T. Looney if (tp->snd_wnd >= sbavail(&so->so_snd)) 2874cfa6009eSGleb Smirnoff tp->snd_wnd -= sbavail(&so->so_snd); 2875b8c2cd15SJonathan T. Looney else 2876b8c2cd15SJonathan T. Looney tp->snd_wnd = 0; 2877c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, 2878cfa6009eSGleb Smirnoff (int)sbavail(&so->so_snd)); 2879df8bae1dSRodney W. Grimes ourfinisacked = 1; 2880df8bae1dSRodney W. Grimes } else { 2881c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, acked); 28823ac12506SJonathan T. Looney if (tp->snd_wnd >= (uint32_t) acked) 288360ee3bb2SAndre Oppermann tp->snd_wnd -= acked; 2884b8c2cd15SJonathan T. Looney else 2885b8c2cd15SJonathan T. Looney tp->snd_wnd = 0; 2886df8bae1dSRodney W. Grimes ourfinisacked = 0; 2887df8bae1dSRodney W. Grimes } 2888564aab1fSAndre Oppermann /* NB: sowwakeup_locked() does an implicit unlock. */ 28891e4d7da7SRobert Watson sowwakeup_locked(so); 2890c11a15bfSGleb Smirnoff m_freem(mfree); 2891e8949f74SAndre Oppermann /* Detect una wraparound. */ 2892dbc42409SLawrence Stewart if (!IN_RECOVERY(tp->t_flags) && 28939d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 28949d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 28959d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 2896dbc42409SLawrence Stewart /* XXXLAS: Can this be moved up into cc_post_recovery? */ 2897dbc42409SLawrence Stewart if (IN_RECOVERY(tp->t_flags) && 289824cb0f22SLawrence Stewart SEQ_GEQ(th->th_ack, tp->snd_recover)) { 2899dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 290024cb0f22SLawrence Stewart } 2901fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 29023529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 29036d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 29046d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 29056d90faf3SPaul Saab } 2906df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2907df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2908df8bae1dSRodney W. Grimes 2909df8bae1dSRodney W. Grimes switch (tp->t_state) { 2910df8bae1dSRodney W. Grimes 2911df8bae1dSRodney W. Grimes /* 2912df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2913df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2914df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2915df8bae1dSRodney W. Grimes */ 2916df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2917df8bae1dSRodney W. Grimes if (ourfinisacked) { 2918df8bae1dSRodney W. Grimes /* 2919df8bae1dSRodney W. Grimes * If we can't receive any more 2920df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2921df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2922df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2923df8bae1dSRodney W. Grimes * we'll hang forever. 2924e8949f74SAndre Oppermann * 2925e8949f74SAndre Oppermann * XXXjl: 2926340c35deSJonathan Lemon * we should release the tp also, and use a 2927340c35deSJonathan Lemon * compressed state. 2928340c35deSJonathan Lemon */ 2929c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 293003e49181SSeigo Tanimura soisdisconnected(so); 29319077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 29329077f387SGleb Smirnoff (tcp_fast_finwait2_recycle ? 29339077f387SGleb Smirnoff tcp_finwait2_timeout : 29349077f387SGleb Smirnoff TP_MAXIDLE(tp))); 29354cc20ab1SSeigo Tanimura } 293657f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_2); 2937df8bae1dSRodney W. Grimes } 2938df8bae1dSRodney W. Grimes break; 2939df8bae1dSRodney W. Grimes 2940df8bae1dSRodney W. Grimes /* 2941df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2942df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2943df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2944df8bae1dSRodney W. Grimes * the segment. 2945df8bae1dSRodney W. Grimes */ 2946df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2947df8bae1dSRodney W. Grimes if (ourfinisacked) { 2948ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 294911a20fb8SJeffrey Hsu tcp_twstart(tp); 2950ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 2951340c35deSJonathan Lemon m_freem(m); 2952679d9708SAndre Oppermann return; 2953df8bae1dSRodney W. Grimes } 2954df8bae1dSRodney W. Grimes break; 2955df8bae1dSRodney W. Grimes 2956df8bae1dSRodney W. Grimes /* 2957df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2958df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2959df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2960df8bae1dSRodney W. Grimes * enter the closed state and return. 2961df8bae1dSRodney W. Grimes */ 2962df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2963df8bae1dSRodney W. Grimes if (ourfinisacked) { 2964ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2965df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2966df8bae1dSRodney W. Grimes goto drop; 2967df8bae1dSRodney W. Grimes } 2968df8bae1dSRodney W. Grimes break; 2969df8bae1dSRodney W. Grimes } 2970df8bae1dSRodney W. Grimes } 2971df8bae1dSRodney W. Grimes 2972df8bae1dSRodney W. Grimes step6: 29738501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 29747cfc6904SRobert Watson 2975df8bae1dSRodney W. Grimes /* 297660ee3bb2SAndre Oppermann * Update window information. 297760ee3bb2SAndre Oppermann * Don't look at window if no ACK: TAC's send garbage on first SYN. 2978df8bae1dSRodney W. Grimes */ 297960ee3bb2SAndre Oppermann if ((thflags & TH_ACK) && 298060ee3bb2SAndre Oppermann (SEQ_LT(tp->snd_wl1, th->th_seq) || 298160ee3bb2SAndre Oppermann (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 298260ee3bb2SAndre Oppermann (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 298360ee3bb2SAndre Oppermann /* keep track of pure window updates */ 298460ee3bb2SAndre Oppermann if (tlen == 0 && 298560ee3bb2SAndre Oppermann tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 298678b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinupd); 2987df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 298860ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 298960ee3bb2SAndre Oppermann tp->snd_wl2 = th->th_ack; 2990df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2991df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 299260ee3bb2SAndre Oppermann needoutput = 1; 2993df8bae1dSRodney W. Grimes } 2994df8bae1dSRodney W. Grimes 2995df8bae1dSRodney W. Grimes /* 2996df8bae1dSRodney W. Grimes * Process segments with URG. 2997df8bae1dSRodney W. Grimes */ 2998fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2999df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 3000df8bae1dSRodney W. Grimes /* 3001df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 3002df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 3003df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 3004df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 3005df8bae1dSRodney W. Grimes */ 300618ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3007cfa6009eSGleb Smirnoff if (th->th_urp + sbavail(&so->so_rcv) > sb_max) { 3008fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 3009fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 301018ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 3011df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 3012df8bae1dSRodney W. Grimes } 3013df8bae1dSRodney W. Grimes /* 3014df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 3015df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 3016df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 3017df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 3018df8bae1dSRodney W. Grimes * In these states we ignore the URG. 3019df8bae1dSRodney W. Grimes * 3020df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 3021df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 3022df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 3023df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 3024df8bae1dSRodney W. Grimes * of data past the urgent section as the original 3025df8bae1dSRodney W. Grimes * spec states (in one of two places). 3026df8bae1dSRodney W. Grimes */ 3027fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 3028fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 3029cfa6009eSGleb Smirnoff so->so_oobmark = sbavail(&so->so_rcv) + 3030df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 3031927c5ceaSRobert Watson if (so->so_oobmark == 0) 3032c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 3033df8bae1dSRodney W. Grimes sohasoutofband(so); 3034df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 3035df8bae1dSRodney W. Grimes } 303618ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 3037df8bae1dSRodney W. Grimes /* 3038df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 3039df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 3040df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 3041df8bae1dSRodney W. Grimes * data may creep in... ick. 3042df8bae1dSRodney W. Grimes */ 30433ac12506SJonathan T. Looney if (th->th_urp <= (uint32_t)tlen && 304430613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 304530613f56SJeffrey Hsu /* hdr drop is delayed */ 304630613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 304730613f56SJeffrey Hsu } 3048c068736aSJeffrey Hsu } else { 3049df8bae1dSRodney W. Grimes /* 3050df8bae1dSRodney W. Grimes * If no out of band data is expected, 3051df8bae1dSRodney W. Grimes * pull receive urgent pointer along 3052df8bae1dSRodney W. Grimes * with the receive window. 3053df8bae1dSRodney W. Grimes */ 3054df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 3055df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 3056c068736aSJeffrey Hsu } 3057df8bae1dSRodney W. Grimes dodata: /* XXX */ 30588501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 30597cfc6904SRobert Watson 3060df8bae1dSRodney W. Grimes /* 3061df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 3062df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 3063df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 3064df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 3065df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 3066df8bae1dSRodney W. Grimes * connection then we just ignore the text. 3067df8bae1dSRodney W. Grimes */ 306868bd7ed1SJonathan T. Looney #ifdef TCP_RFC7413 3069281a0fd4SPatrick Kelsey tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) && 307068bd7ed1SJonathan T. Looney IS_FASTOPEN(tp->t_flags)); 307168bd7ed1SJonathan T. Looney #else 307268bd7ed1SJonathan T. Looney #define tfo_syn (false) 307368bd7ed1SJonathan T. Looney #endif 3074281a0fd4SPatrick Kelsey if ((tlen || (thflags & TH_FIN) || tfo_syn) && 3075df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 307669e03620SPaul Saab tcp_seq save_start = th->th_seq; 3077fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 3078e4b64281SJesper Skriver /* 3079c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 3080c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 3081c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 3082c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 3083c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 3084c068736aSJeffrey Hsu * and removal from the queue and repetition of various 3085c068736aSJeffrey Hsu * conversions. 3086c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 3087c068736aSJeffrey Hsu * immediately when segments are out of order (so 3088c068736aSJeffrey Hsu * fast retransmit can work). 3089e4b64281SJesper Skriver */ 30904741bfcbSPatrick Kelsey if (th->th_seq == tp->rcv_nxt && 30914741bfcbSPatrick Kelsey LIST_EMPTY(&tp->t_segq) && 3092281a0fd4SPatrick Kelsey (TCPS_HAVEESTABLISHED(tp->t_state) || 3093281a0fd4SPatrick Kelsey tfo_syn)) { 3094281a0fd4SPatrick Kelsey if (DELAY_ACK(tp, tlen) || tfo_syn) 30953bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 3096e4b64281SJesper Skriver else 3097e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 3098e4b64281SJesper Skriver tp->rcv_nxt += tlen; 3099e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 310078b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 310178b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 31021e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3103c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 3104c1c36a2cSMike Silbersack m_freem(m); 3105c1c36a2cSMike Silbersack else 3106651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 3107e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 31081e4d7da7SRobert Watson sorwakeup_locked(so); 3109e4b64281SJesper Skriver } else { 3110e8949f74SAndre Oppermann /* 3111e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 3112e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 3113e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 3114e8949f74SAndre Oppermann * when trimming from the head. 3115e8949f74SAndre Oppermann */ 3116e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 3117e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 3118e4b64281SJesper Skriver } 31193529149eSAndre Oppermann if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 3120f194524fSAndre Oppermann tcp_update_sack_list(tp, save_start, save_start + tlen); 3121302ce8d6SAndre Oppermann #if 0 3122df8bae1dSRodney W. Grimes /* 3123df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 3124df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 3125df8bae1dSRodney W. Grimes * buffer size. 3126302ce8d6SAndre Oppermann * XXX: Unused. 3127df8bae1dSRodney W. Grimes */ 3128f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) 3129df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 3130f701e30dSJohn Baldwin else 3131f701e30dSJohn Baldwin len = so->so_rcv.sb_hiwat; 3132302ce8d6SAndre Oppermann #endif 3133df8bae1dSRodney W. Grimes } else { 3134df8bae1dSRodney W. Grimes m_freem(m); 3135fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 3136df8bae1dSRodney W. Grimes } 3137df8bae1dSRodney W. Grimes 3138df8bae1dSRodney W. Grimes /* 3139df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 3140df8bae1dSRodney W. Grimes * that the connection is closing. 3141df8bae1dSRodney W. Grimes */ 3142fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 3143df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 3144df8bae1dSRodney W. Grimes socantrcvmore(so); 3145a0292f23SGarrett Wollman /* 3146a0292f23SGarrett Wollman * If connection is half-synchronized 3147d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 3148a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 3149a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 3150a0292f23SGarrett Wollman * more input can be expected, send ACK now. 3151a0292f23SGarrett Wollman */ 31523bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 31533bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 3154a0292f23SGarrett Wollman else 3155df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 3156df8bae1dSRodney W. Grimes tp->rcv_nxt++; 3157df8bae1dSRodney W. Grimes } 3158df8bae1dSRodney W. Grimes switch (tp->t_state) { 3159df8bae1dSRodney W. Grimes 3160df8bae1dSRodney W. Grimes /* 3161df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 3162df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 3163df8bae1dSRodney W. Grimes */ 3164df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 31659b8b58e0SJonathan Lemon tp->t_starttime = ticks; 31669b8b58e0SJonathan Lemon /* FALLTHROUGH */ 3167df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 316857f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSE_WAIT); 3169df8bae1dSRodney W. Grimes break; 3170df8bae1dSRodney W. Grimes 3171df8bae1dSRodney W. Grimes /* 3172df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 3173df8bae1dSRodney W. Grimes * enter the CLOSING state. 3174df8bae1dSRodney W. Grimes */ 3175df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 317657f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSING); 3177df8bae1dSRodney W. Grimes break; 3178df8bae1dSRodney W. Grimes 3179df8bae1dSRodney W. Grimes /* 3180df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 3181df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 3182df8bae1dSRodney W. Grimes * standard timers. 3183df8bae1dSRodney W. Grimes */ 3184df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 3185ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 3186ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, ("%s: dodata " 3187252ca428SRobert Watson "TCP_FIN_WAIT_2 ti_locked: %d", __func__, 3188252ca428SRobert Watson ti_locked)); 3189252ca428SRobert Watson 3190340c35deSJonathan Lemon tcp_twstart(tp); 3191ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 3192679d9708SAndre Oppermann return; 3193df8bae1dSRodney W. Grimes } 3194df8bae1dSRodney W. Grimes } 3195ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 3196ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 3197252ca428SRobert Watson ti_locked = TI_UNLOCKED; 3198252ca428SRobert Watson 3199610ee2f9SDavid Greenman #ifdef TCPDEBUG 32004cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3201fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 3202fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3203610ee2f9SDavid Greenman #endif 3204*2b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 3205df8bae1dSRodney W. Grimes 3206df8bae1dSRodney W. Grimes /* 3207df8bae1dSRodney W. Grimes * Return any desired output. 3208df8bae1dSRodney W. Grimes */ 3209df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 321055bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 32117792ea27SJeffrey Hsu 3212a14c749fSJonathan Lemon check_delack: 3213252ca428SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 3214252ca428SRobert Watson __func__, ti_locked)); 3215603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 32168501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 3217252ca428SRobert Watson 3218a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 32193bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 3220b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 32213bfd6421SJonathan Lemon } 32228501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3223679d9708SAndre Oppermann return; 3224df8bae1dSRodney W. Grimes 3225df8bae1dSRodney W. Grimes dropafterack: 3226df8bae1dSRodney W. Grimes /* 3227df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 3228df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 322980ab7c0eSGarrett Wollman * 323080ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 323180ab7c0eSGarrett Wollman * paths to this code happen after packets containing 323280ab7c0eSGarrett Wollman * RST have been dropped. 323380ab7c0eSGarrett Wollman * 323480ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 323580ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 323680ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 323780ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 323880ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 323980ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 3240df8bae1dSRodney W. Grimes */ 3241fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 3242fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 3243a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 3244a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 3245a57815efSBosko Milekic goto dropwithreset; 3246a57815efSBosko Milekic } 3247a0292f23SGarrett Wollman #ifdef TCPDEBUG 32484cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3249fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3250fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3251a0292f23SGarrett Wollman #endif 3252*2b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 3253ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 3254ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 3255252ca428SRobert Watson ti_locked = TI_UNLOCKED; 3256252ca428SRobert Watson 3257df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 325855bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 32598501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3260d6915262SRobert Watson m_freem(m); 3261679d9708SAndre Oppermann return; 3262df8bae1dSRodney W. Grimes 3263df8bae1dSRodney W. Grimes dropwithreset: 3264ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 3265ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 3266252ca428SRobert Watson ti_locked = TI_UNLOCKED; 3267a57815efSBosko Milekic 3268a0ca0871SRobert Watson if (tp != NULL) { 3269302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 32708501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3271dd8ac7f9SRobert Watson } else 3272a0ca0871SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 3273679d9708SAndre Oppermann return; 3274df8bae1dSRodney W. Grimes 3275df8bae1dSRodney W. Grimes drop: 3276ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 3277ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 3278fa046d87SRobert Watson ti_locked = TI_UNLOCKED; 3279fa046d87SRobert Watson } 3280252ca428SRobert Watson #ifdef INVARIANTS 3281252ca428SRobert Watson else 3282252ca428SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 3283252ca428SRobert Watson #endif 3284252ca428SRobert Watson 3285df8bae1dSRodney W. Grimes /* 3286df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 3287df8bae1dSRodney W. Grimes */ 3288610ee2f9SDavid Greenman #ifdef TCPDEBUG 32891c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 3290fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3291fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3292a0292f23SGarrett Wollman #endif 3293*2b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 32941c53f806SRobert Watson if (tp != NULL) 32958501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3296d6915262SRobert Watson m_freem(m); 329768bd7ed1SJonathan T. Looney #ifndef TCP_RFC7413 329868bd7ed1SJonathan T. Looney #undef tfo_syn 329968bd7ed1SJonathan T. Looney #endif 3300302ce8d6SAndre Oppermann } 3301302ce8d6SAndre Oppermann 3302302ce8d6SAndre Oppermann /* 33030ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 33040ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 33050ca3f933SAndre Oppermann * tp may be NULL. 3306302ce8d6SAndre Oppermann */ 330755bceb1eSRandall Stewart void 3308302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 3309302ce8d6SAndre Oppermann int tlen, int rstreason) 3310302ce8d6SAndre Oppermann { 3311b287c6c7SBjoern A. Zeeb #ifdef INET 3312302ce8d6SAndre Oppermann struct ip *ip; 3313b287c6c7SBjoern A. Zeeb #endif 3314302ce8d6SAndre Oppermann #ifdef INET6 3315302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 3316302ce8d6SAndre Oppermann #endif 33177a3244ccSRobert Watson 33187a3244ccSRobert Watson if (tp != NULL) { 33198501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 33207a3244ccSRobert Watson } 33217a3244ccSRobert Watson 33220ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 3323302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 3324302ce8d6SAndre Oppermann goto drop; 3325302ce8d6SAndre Oppermann #ifdef INET6 3326302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 3327302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 3328302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3329302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3330302ce8d6SAndre Oppermann goto drop; 3331302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 3332b287c6c7SBjoern A. Zeeb } 3333302ce8d6SAndre Oppermann #endif 3334b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3335b287c6c7SBjoern A. Zeeb else 3336b287c6c7SBjoern A. Zeeb #endif 3337b287c6c7SBjoern A. Zeeb #ifdef INET 3338302ce8d6SAndre Oppermann { 3339302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 3340302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3341302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3342302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3343302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3344302ce8d6SAndre Oppermann goto drop; 3345302ce8d6SAndre Oppermann } 3346b287c6c7SBjoern A. Zeeb #endif 3347302ce8d6SAndre Oppermann 3348302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 3349302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 3350302ce8d6SAndre Oppermann goto drop; 3351302ce8d6SAndre Oppermann 3352302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 3353302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 3354302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 3355302ce8d6SAndre Oppermann th->th_ack, TH_RST); 3356302ce8d6SAndre Oppermann } else { 3357302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 3358302ce8d6SAndre Oppermann tlen++; 33594ddd5aadSMichael Tuexen if (th->th_flags & TH_FIN) 33604ddd5aadSMichael Tuexen tlen++; 3361302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 3362302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 3363302ce8d6SAndre Oppermann } 3364302ce8d6SAndre Oppermann return; 3365302ce8d6SAndre Oppermann drop: 3366302ce8d6SAndre Oppermann m_freem(m); 3367df8bae1dSRodney W. Grimes } 3368df8bae1dSRodney W. Grimes 3369be2ac88cSJonathan Lemon /* 3370be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 3371be2ac88cSJonathan Lemon */ 337255bceb1eSRandall Stewart void 3373ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 3374df8bae1dSRodney W. Grimes { 3375df8bae1dSRodney W. Grimes int opt, optlen; 3376df8bae1dSRodney W. Grimes 3377be2ac88cSJonathan Lemon to->to_flags = 0; 3378df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 3379df8bae1dSRodney W. Grimes opt = cp[0]; 3380df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 3381df8bae1dSRodney W. Grimes break; 3382df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 3383df8bae1dSRodney W. Grimes optlen = 1; 3384df8bae1dSRodney W. Grimes else { 3385b474779fSJun-ichiro itojun Hagino if (cnt < 2) 3386b474779fSJun-ichiro itojun Hagino break; 3387df8bae1dSRodney W. Grimes optlen = cp[1]; 3388b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 3389df8bae1dSRodney W. Grimes break; 3390df8bae1dSRodney W. Grimes } 3391df8bae1dSRodney W. Grimes switch (opt) { 3392df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 3393df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 3394df8bae1dSRodney W. Grimes continue; 3395f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3396df8bae1dSRodney W. Grimes continue; 3397be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 3398be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 3399be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 3400fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 3401df8bae1dSRodney W. Grimes break; 3402df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 3403df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 3404df8bae1dSRodney W. Grimes continue; 3405f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3406df8bae1dSRodney W. Grimes continue; 3407be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 340802a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 3409df8bae1dSRodney W. Grimes break; 3410df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 3411df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 3412df8bae1dSRodney W. Grimes continue; 3413be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 3414a0292f23SGarrett Wollman bcopy((char *)cp + 2, 3415a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 3416fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 3417a0292f23SGarrett Wollman bcopy((char *)cp + 6, 3418a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 3419fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 3420df8bae1dSRodney W. Grimes break; 34211cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 34221cfd4b53SBruce M Simpson /* 34231cfd4b53SBruce M Simpson * XXX In order to reply to a host which has set the 34241cfd4b53SBruce M Simpson * TCP_SIGNATURE option in its initial SYN, we have to 34251cfd4b53SBruce M Simpson * record the fact that the option was observed here 34261cfd4b53SBruce M Simpson * for the syncache code to perform the correct response. 34271cfd4b53SBruce M Simpson */ 34281cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 34291cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 34301cfd4b53SBruce M Simpson continue; 3431df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 3432df47e437SAndre Oppermann to->to_signature = cp + 2; 34331cfd4b53SBruce M Simpson break; 3434265ed012SBruce M Simpson #endif 34356d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 3436f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 34376d90faf3SPaul Saab continue; 3438f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3439f72167f4SAndre Oppermann continue; 3440603724d3SBjoern A. Zeeb if (!V_tcp_do_sack) 3441f72167f4SAndre Oppermann continue; 3442fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 34436d90faf3SPaul Saab break; 34446d90faf3SPaul Saab case TCPOPT_SACK: 34455a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 34466d90faf3SPaul Saab continue; 3447b728e902SAndre Oppermann if (flags & TO_SYN) 3448b728e902SAndre Oppermann continue; 3449fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 34505a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 34515a53ca16SPaul Saab to->to_sacks = cp + 2; 345278b50714SRobert Watson TCPSTAT_INC(tcps_sack_rcv_blocks); 34536d90faf3SPaul Saab break; 3454281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 3455281a0fd4SPatrick Kelsey case TCPOPT_FAST_OPEN: 3456281a0fd4SPatrick Kelsey if ((optlen != TCPOLEN_FAST_OPEN_EMPTY) && 3457281a0fd4SPatrick Kelsey (optlen < TCPOLEN_FAST_OPEN_MIN) && 3458281a0fd4SPatrick Kelsey (optlen > TCPOLEN_FAST_OPEN_MAX)) 3459281a0fd4SPatrick Kelsey continue; 3460281a0fd4SPatrick Kelsey if (!(flags & TO_SYN)) 3461281a0fd4SPatrick Kelsey continue; 3462281a0fd4SPatrick Kelsey if (!V_tcp_fastopen_enabled) 3463281a0fd4SPatrick Kelsey continue; 3464281a0fd4SPatrick Kelsey to->to_flags |= TOF_FASTOPEN; 3465281a0fd4SPatrick Kelsey to->to_tfo_len = optlen - 2; 3466281a0fd4SPatrick Kelsey to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL; 3467281a0fd4SPatrick Kelsey break; 3468281a0fd4SPatrick Kelsey #endif 3469be2ac88cSJonathan Lemon default: 3470be2ac88cSJonathan Lemon continue; 3471df8bae1dSRodney W. Grimes } 3472df8bae1dSRodney W. Grimes } 3473df8bae1dSRodney W. Grimes } 3474df8bae1dSRodney W. Grimes 3475df8bae1dSRodney W. Grimes /* 3476df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 3477df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 3478df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 3479df8bae1dSRodney W. Grimes * sequencing purposes. 3480df8bae1dSRodney W. Grimes */ 348155bceb1eSRandall Stewart void 3482ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 3483ad3f9ab3SAndre Oppermann int off) 3484df8bae1dSRodney W. Grimes { 3485fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 3486df8bae1dSRodney W. Grimes 3487df8bae1dSRodney W. Grimes while (cnt >= 0) { 3488df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 3489df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 3490df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 3491df8bae1dSRodney W. Grimes 34928501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 34937a3244ccSRobert Watson 3494df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 3495df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 3496df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 3497df8bae1dSRodney W. Grimes m->m_len--; 349869a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 349969a34685SYoshinobu Inoue m->m_pkthdr.len--; 3500df8bae1dSRodney W. Grimes return; 3501df8bae1dSRodney W. Grimes } 3502df8bae1dSRodney W. Grimes cnt -= m->m_len; 3503df8bae1dSRodney W. Grimes m = m->m_next; 35044dfdffe9SAndre Oppermann if (m == NULL) 3505df8bae1dSRodney W. Grimes break; 3506df8bae1dSRodney W. Grimes } 3507df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 3508df8bae1dSRodney W. Grimes } 3509df8bae1dSRodney W. Grimes 3510df8bae1dSRodney W. Grimes /* 3511df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 3512df8bae1dSRodney W. Grimes * and update averages and current timeout. 3513df8bae1dSRodney W. Grimes */ 351455bceb1eSRandall Stewart void 3515ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 3516df8bae1dSRodney W. Grimes { 3517ad3f9ab3SAndre Oppermann int delta; 3518233e8c18SGarrett Wollman 35198501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 35202be3bf22SRobert Watson 352178b50714SRobert Watson TCPSTAT_INC(tcps_rttupdated); 3522233e8c18SGarrett Wollman tp->t_rttupdated++; 3523233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 3524233e8c18SGarrett Wollman /* 3525233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 3526233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 3527233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 3528233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3529233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 3530233e8c18SGarrett Wollman */ 3531233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3532233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3533233e8c18SGarrett Wollman 3534233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 3535233e8c18SGarrett Wollman tp->t_srtt = 1; 3536233e8c18SGarrett Wollman 3537233e8c18SGarrett Wollman /* 3538233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 3539233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 3540233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 3541233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 3542233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 3543233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 3544233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3545233e8c18SGarrett Wollman * rfc793's wired-in beta. 3546233e8c18SGarrett Wollman */ 3547233e8c18SGarrett Wollman if (delta < 0) 3548233e8c18SGarrett Wollman delta = -delta; 3549233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3550233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 3551233e8c18SGarrett Wollman tp->t_rttvar = 1; 35521fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 35531fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3554233e8c18SGarrett Wollman } else { 3555233e8c18SGarrett Wollman /* 3556233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 3557233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 3558233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 3559233e8c18SGarrett Wollman */ 3560233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 3561233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 35621fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3563233e8c18SGarrett Wollman } 35649b8b58e0SJonathan Lemon tp->t_rtttime = 0; 3565df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 3566df8bae1dSRodney W. Grimes 3567df8bae1dSRodney W. Grimes /* 3568df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 3569df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 3570df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 3571df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 3572df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 3573df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 3574df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 3575df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 3576df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 3577df8bae1dSRodney W. Grimes */ 3578233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 35799e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3580df8bae1dSRodney W. Grimes 3581df8bae1dSRodney W. Grimes /* 3582df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 3583df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 3584df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 3585df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 3586df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 3587df8bae1dSRodney W. Grimes */ 3588df8bae1dSRodney W. Grimes tp->t_softerror = 0; 3589df8bae1dSRodney W. Grimes } 3590df8bae1dSRodney W. Grimes 3591df8bae1dSRodney W. Grimes /* 3592df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 3593df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 35944faaea55SAndre Oppermann * If none, use an mss that can be handled on the outgoing interface 35954faaea55SAndre Oppermann * without forcing IP to fragment. If no route is found, route has no mtu, 3596df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 3597df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 3598df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 3599df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 3600df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 3601df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 3602df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 3603a0292f23SGarrett Wollman * 36040c39d38dSGleb Smirnoff * NOTE that resulting t_maxseg doesn't include space for TCP options or 36050c39d38dSGleb Smirnoff * IP options, e.g. IPSEC data, since length of this data may vary, and 36060c39d38dSGleb Smirnoff * thus it is calculated for every segment separately in tcp_output(). 3607a0292f23SGarrett Wollman * 360897d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 3609ef341ee1SGleb Smirnoff * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS 3610ef341ee1SGleb Smirnoff * settings are handled in tcp_mssopt(). 3611df8bae1dSRodney W. Grimes */ 3612a0292f23SGarrett Wollman void 3613ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer, 36143c914c54SAndre Oppermann struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap) 3615df8bae1dSRodney W. Grimes { 3616b287c6c7SBjoern A. Zeeb int mss = 0; 36173ac12506SJonathan T. Looney uint32_t maxmtu = 0; 3618c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 361997d8d152SAndre Oppermann struct hc_metrics_lite metrics; 3620fb59c426SYoshinobu Inoue #ifdef INET6 3621c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3622c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 3623c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3624c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 3625c068736aSJeffrey Hsu #else 3626c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 3627fb59c426SYoshinobu Inoue #endif 3628df8bae1dSRodney W. Grimes 36298501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 36307a3244ccSRobert Watson 3631ef341ee1SGleb Smirnoff if (mtuoffer != -1) { 3632ef341ee1SGleb Smirnoff KASSERT(offer == -1, ("%s: conflict", __func__)); 3633ef341ee1SGleb Smirnoff offer = mtuoffer - min_protoh; 3634ef341ee1SGleb Smirnoff } 3635ef341ee1SGleb Smirnoff 3636e8949f74SAndre Oppermann /* Initialize. */ 363797d8d152SAndre Oppermann #ifdef INET6 363897d8d152SAndre Oppermann if (isipv6) { 36393c914c54SAndre Oppermann maxmtu = tcp_maxmtu6(&inp->inp_inc, cap); 36400c39d38dSGleb Smirnoff tp->t_maxseg = V_tcp_v6mssdflt; 3641b287c6c7SBjoern A. Zeeb } 364297d8d152SAndre Oppermann #endif 3643b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3644b287c6c7SBjoern A. Zeeb else 3645b287c6c7SBjoern A. Zeeb #endif 3646b287c6c7SBjoern A. Zeeb #ifdef INET 364797d8d152SAndre Oppermann { 36483c914c54SAndre Oppermann maxmtu = tcp_maxmtu(&inp->inp_inc, cap); 36490c39d38dSGleb Smirnoff tp->t_maxseg = V_tcp_mssdflt; 3650df8bae1dSRodney W. Grimes } 3651b287c6c7SBjoern A. Zeeb #endif 3652df8bae1dSRodney W. Grimes 365397d8d152SAndre Oppermann /* 3654e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 365597d8d152SAndre Oppermann */ 36566f01cac6SBjoern A. Zeeb if (maxmtu == 0) { 36576f01cac6SBjoern A. Zeeb /* 36588e5c87f4SBjoern A. Zeeb * In case we return early we need to initialize metrics 36596f01cac6SBjoern A. Zeeb * to a defined state as tcp_hc_get() would do for us 36606f01cac6SBjoern A. Zeeb * if there was no cache hit. 36616f01cac6SBjoern A. Zeeb */ 36626f01cac6SBjoern A. Zeeb if (metricptr != NULL) 36636f01cac6SBjoern A. Zeeb bzero(metricptr, sizeof(struct hc_metrics_lite)); 366497d8d152SAndre Oppermann return; 36656f01cac6SBjoern A. Zeeb } 366697d8d152SAndre Oppermann 3667e8949f74SAndre Oppermann /* What have we got? */ 366897d8d152SAndre Oppermann switch (offer) { 366997d8d152SAndre Oppermann case 0: 367097d8d152SAndre Oppermann /* 367197d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 3672c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 36730c39d38dSGleb Smirnoff * already assigned to t_maxseg above. 367497d8d152SAndre Oppermann */ 36750c39d38dSGleb Smirnoff offer = tp->t_maxseg; 367697d8d152SAndre Oppermann break; 367797d8d152SAndre Oppermann 367897d8d152SAndre Oppermann case -1: 3679a0292f23SGarrett Wollman /* 3680c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 3681a0292f23SGarrett Wollman */ 368297d8d152SAndre Oppermann /* FALLTHROUGH */ 368397d8d152SAndre Oppermann 368497d8d152SAndre Oppermann default: 3685a0292f23SGarrett Wollman /* 368653369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 368753369ac9SAndre Oppermann * to at least minmss. 368853369ac9SAndre Oppermann */ 3689603724d3SBjoern A. Zeeb offer = max(offer, V_tcp_minmss); 369097d8d152SAndre Oppermann } 3691a0292f23SGarrett Wollman 3692df8bae1dSRodney W. Grimes /* 3693e8949f74SAndre Oppermann * rmx information is now retrieved from tcp_hostcache. 3694df8bae1dSRodney W. Grimes */ 369597d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 36963cee92e0SBjoern A. Zeeb if (metricptr != NULL) 36973cee92e0SBjoern A. Zeeb bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 369897d8d152SAndre Oppermann 3699df8bae1dSRodney W. Grimes /* 3700cc412412SHiren Panchasara * If there's a discovered mtu in tcp hostcache, use it. 3701cc412412SHiren Panchasara * Else, use the link mtu. 3702df8bae1dSRodney W. Grimes */ 370397d8d152SAndre Oppermann if (metrics.rmx_mtu) 37042d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3705c068736aSJeffrey Hsu else { 370631b3783cSHajimu UMEMOTO #ifdef INET6 3707fb59c426SYoshinobu Inoue if (isipv6) { 370897d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3709603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 371097d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 3711603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_v6mssdflt); 3712b287c6c7SBjoern A. Zeeb } 3713b3399803SHajimu UMEMOTO #endif 3714b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3715b287c6c7SBjoern A. Zeeb else 3716b287c6c7SBjoern A. Zeeb #endif 3717b287c6c7SBjoern A. Zeeb #ifdef INET 371897d8d152SAndre Oppermann { 371997d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3720603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 372197d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 3722603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_mssdflt); 3723a0292f23SGarrett Wollman } 3724b287c6c7SBjoern A. Zeeb #endif 37253cee92e0SBjoern A. Zeeb /* 37263cee92e0SBjoern A. Zeeb * XXX - The above conditional (mss = maxmtu - min_protoh) 37273cee92e0SBjoern A. Zeeb * probably violates the TCP spec. 37283cee92e0SBjoern A. Zeeb * The problem is that, since we don't know the 37293cee92e0SBjoern A. Zeeb * other end's MSS, we are supposed to use a conservative 37303cee92e0SBjoern A. Zeeb * default. But, if we do that, then MTU discovery will 37313cee92e0SBjoern A. Zeeb * never actually take place, because the conservative 37323cee92e0SBjoern A. Zeeb * default is much less than the MTUs typically seen 37333cee92e0SBjoern A. Zeeb * on the Internet today. For the moment, we'll sweep 37343cee92e0SBjoern A. Zeeb * this under the carpet. 37353cee92e0SBjoern A. Zeeb * 37363cee92e0SBjoern A. Zeeb * The conservative default might not actually be a problem 37373cee92e0SBjoern A. Zeeb * if the only case this occurs is when sending an initial 37383cee92e0SBjoern A. Zeeb * SYN with options and data to a host we've never talked 37393cee92e0SBjoern A. Zeeb * to before. Then, they will reply with an MSS value which 37403cee92e0SBjoern A. Zeeb * will get recorded and the new parameters should get 37413cee92e0SBjoern A. Zeeb * recomputed. For Further Study. 37423cee92e0SBjoern A. Zeeb */ 374397d8d152SAndre Oppermann } 3744a0292f23SGarrett Wollman mss = min(mss, offer); 374597d8d152SAndre Oppermann 3746a0292f23SGarrett Wollman /* 37470c39d38dSGleb Smirnoff * Sanity check: make sure that maxseg will be large 37483cee92e0SBjoern A. Zeeb * enough to allow some data on segments even if the 37493cee92e0SBjoern A. Zeeb * all the option space is used (40bytes). Otherwise 37503cee92e0SBjoern A. Zeeb * funny things may happen in tcp_output. 37510c39d38dSGleb Smirnoff * 37520c39d38dSGleb Smirnoff * XXXGL: shouldn't we reserve space for IP/IPv6 options? 37533cee92e0SBjoern A. Zeeb */ 37543cee92e0SBjoern A. Zeeb mss = max(mss, 64); 37553cee92e0SBjoern A. Zeeb 375697d8d152SAndre Oppermann tp->t_maxseg = mss; 37573cee92e0SBjoern A. Zeeb } 37583cee92e0SBjoern A. Zeeb 37593cee92e0SBjoern A. Zeeb void 37603cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer) 37613cee92e0SBjoern A. Zeeb { 3762dbc42409SLawrence Stewart int mss; 37633ac12506SJonathan T. Looney uint32_t bufsize; 37643cee92e0SBjoern A. Zeeb struct inpcb *inp; 37653cee92e0SBjoern A. Zeeb struct socket *so; 37663cee92e0SBjoern A. Zeeb struct hc_metrics_lite metrics; 37673c914c54SAndre Oppermann struct tcp_ifcap cap; 3768dbc42409SLawrence Stewart 37693cee92e0SBjoern A. Zeeb KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 37703cee92e0SBjoern A. Zeeb 37713c914c54SAndre Oppermann bzero(&cap, sizeof(cap)); 37723c914c54SAndre Oppermann tcp_mss_update(tp, offer, -1, &metrics, &cap); 37733cee92e0SBjoern A. Zeeb 37743cee92e0SBjoern A. Zeeb mss = tp->t_maxseg; 37753cee92e0SBjoern A. Zeeb inp = tp->t_inpcb; 377697d8d152SAndre Oppermann 3777df8bae1dSRodney W. Grimes /* 377897d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 377997d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 378097d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 378197d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 378297d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3783df8bae1dSRodney W. Grimes */ 3784c3b02504SBjoern A. Zeeb so = inp->inp_socket; 37853f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 3786e233e2acSAndre Oppermann if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe) 378797d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 378897d8d152SAndre Oppermann else 3789df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3790df8bae1dSRodney W. Grimes if (bufsize < mss) 3791df8bae1dSRodney W. Grimes mss = bufsize; 3792df8bae1dSRodney W. Grimes else { 3793df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3794df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3795df8bae1dSRodney W. Grimes bufsize = sb_max; 379688c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 37973f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3798df8bae1dSRodney W. Grimes } 37993f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 3800784ce8faSHiren Panchasara /* 3801784ce8faSHiren Panchasara * Sanity check: make sure that maxseg will be large 3802784ce8faSHiren Panchasara * enough to allow some data on segments even if the 3803784ce8faSHiren Panchasara * all the option space is used (40bytes). Otherwise 3804784ce8faSHiren Panchasara * funny things may happen in tcp_output. 3805784ce8faSHiren Panchasara * 3806784ce8faSHiren Panchasara * XXXGL: shouldn't we reserve space for IP/IPv6 options? 3807784ce8faSHiren Panchasara */ 3808784ce8faSHiren Panchasara tp->t_maxseg = max(mss, 64); 3809df8bae1dSRodney W. Grimes 38103f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3811e233e2acSAndre Oppermann if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe) 381297d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 381397d8d152SAndre Oppermann else 3814df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3815df8bae1dSRodney W. Grimes if (bufsize > mss) { 3816df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3817df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3818df8bae1dSRodney W. Grimes bufsize = sb_max; 381988c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 38203f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3821df8bae1dSRodney W. Grimes } 38223f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 382391d6cfa6SBjoern A. Zeeb 382491d6cfa6SBjoern A. Zeeb /* Check the interface for TSO capabilities. */ 38253c914c54SAndre Oppermann if (cap.ifcap & CSUM_TSO) { 382691d6cfa6SBjoern A. Zeeb tp->t_flags |= TF_TSO; 38273c914c54SAndre Oppermann tp->t_tsomax = cap.tsomax; 38289fd573c3SHans Petter Selasky tp->t_tsomaxsegcount = cap.tsomaxsegcount; 38299fd573c3SHans Petter Selasky tp->t_tsomaxsegsize = cap.tsomaxsegsize; 38303c914c54SAndre Oppermann } 3831a0292f23SGarrett Wollman } 3832a0292f23SGarrett Wollman 3833a0292f23SGarrett Wollman /* 3834a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3835a0292f23SGarrett Wollman */ 3836a0292f23SGarrett Wollman int 3837ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 3838a0292f23SGarrett Wollman { 383997d8d152SAndre Oppermann int mss = 0; 38403ac12506SJonathan T. Looney uint32_t thcmtu = 0; 38413ac12506SJonathan T. Looney uint32_t maxmtu = 0; 384297d8d152SAndre Oppermann size_t min_protoh; 3843a0292f23SGarrett Wollman 384497d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3845a0292f23SGarrett Wollman 384631b3783cSHajimu UMEMOTO #ifdef INET6 3847dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3848603724d3SBjoern A. Zeeb mss = V_tcp_v6mssdflt; 3849233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 385097d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 3851b287c6c7SBjoern A. Zeeb } 385231b3783cSHajimu UMEMOTO #endif 3853b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3854b287c6c7SBjoern A. Zeeb else 3855b287c6c7SBjoern A. Zeeb #endif 3856b287c6c7SBjoern A. Zeeb #ifdef INET 385797d8d152SAndre Oppermann { 3858603724d3SBjoern A. Zeeb mss = V_tcp_mssdflt; 3859233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 386097d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 386197d8d152SAndre Oppermann } 3862b287c6c7SBjoern A. Zeeb #endif 38633a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET) 38643a9391deSBjoern A. Zeeb thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 38653a9391deSBjoern A. Zeeb #endif 38663a9391deSBjoern A. Zeeb 386797d8d152SAndre Oppermann if (maxmtu && thcmtu) 386897d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 386997d8d152SAndre Oppermann else if (maxmtu || thcmtu) 387097d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 387197d8d152SAndre Oppermann 387297d8d152SAndre Oppermann return (mss); 3873df8bae1dSRodney W. Grimes } 387446f58482SJonathan Lemon 387546f58482SJonathan Lemon 387646f58482SJonathan Lemon /* 3877c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3878c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3879c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3880c068736aSJeffrey Hsu * be started again. 388146f58482SJonathan Lemon */ 388255bceb1eSRandall Stewart void 3883ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 388446f58482SJonathan Lemon { 388546f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 38863ac12506SJonathan T. Looney uint32_t ocwnd = tp->snd_cwnd; 38870c39d38dSGleb Smirnoff u_int maxseg = tcp_maxseg(tp); 388846f58482SJonathan Lemon 38898501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 38907a3244ccSRobert Watson 3891b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 389246f58482SJonathan Lemon tp->t_rtttime = 0; 389346f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 38946b2a5f92SJayanth Vijayaraghavan /* 3895c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3896c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 38976b2a5f92SJayanth Vijayaraghavan */ 38980c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th); 3899a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 390055bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 390146f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 390246f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 390346f58482SJonathan Lemon tp->snd_nxt = onxt; 390446f58482SJonathan Lemon /* 390546f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 390646f58482SJonathan Lemon * not updated yet. 390746f58482SJonathan Lemon */ 3908dbc42409SLawrence Stewart if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th)) 3909dbc42409SLawrence Stewart tp->snd_cwnd -= BYTES_THIS_ACK(tp, th); 3910d7587117SPaul Saab else 3911d7587117SPaul Saab tp->snd_cwnd = 0; 39120c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 391346f58482SJonathan Lemon } 391412eeb81fSHiren Panchasara 391512eeb81fSHiren Panchasara int 391612eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp) 391712eeb81fSHiren Panchasara { 391812eeb81fSHiren Panchasara return (tp->snd_max - tp->snd_una + 391912eeb81fSHiren Panchasara tp->sackhint.sack_bytes_rexmit - 392012eeb81fSHiren Panchasara tp->sackhint.sacked_bytes); 392112eeb81fSHiren Panchasara } 3922