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 53f9e354dfSJulian Elischer #include "opt_ipfw.h" /* for ipfw_fwd */ 541cfd4b53SBruce M Simpson #include "opt_inet.h" 55fb59c426SYoshinobu Inoue #include "opt_inet6.h" 563a2a9f79SYoshinobu Inoue #include "opt_ipsec.h" 570cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 580cc12cc5SJoerg Wunsch 59df8bae1dSRodney W. Grimes #include <sys/param.h> 6098163b98SPoul-Henning Kamp #include <sys/kernel.h> 6139bc9de5SLawrence Stewart #include <sys/hhook.h> 62df8bae1dSRodney W. Grimes #include <sys/malloc.h> 63df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 64a29f300eSGarrett Wollman #include <sys/proc.h> /* for proc0 declaration */ 65df8bae1dSRodney W. Grimes #include <sys/protosw.h> 6657f60867SMark Johnston #include <sys/sdt.h> 67960ed29cSSeigo Tanimura #include <sys/signalvar.h> 68df8bae1dSRodney W. Grimes #include <sys/socket.h> 69df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 70960ed29cSSeigo Tanimura #include <sys/sysctl.h> 71816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 72960ed29cSSeigo Tanimura #include <sys/systm.h> 73df8bae1dSRodney W. Grimes 74e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 75e79adb8eSGarrett Wollman 7612e2e970SAndre Oppermann #include <vm/uma.h> 7712e2e970SAndre Oppermann 78df8bae1dSRodney W. Grimes #include <net/if.h> 7976039bc8SGleb Smirnoff #include <net/if_var.h> 80df8bae1dSRodney W. Grimes #include <net/route.h> 81530c0060SRobert Watson #include <net/vnet.h> 82df8bae1dSRodney W. Grimes 83773673c1SAndre Oppermann #define TCPSTATES /* for logging */ 84773673c1SAndre Oppermann 85dbc42409SLawrence Stewart #include <netinet/cc.h> 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> 90960ed29cSSeigo Tanimura #include <netinet/in_var.h> 91df8bae1dSRodney W. Grimes #include <netinet/ip.h> 928e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 93686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 94df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 95ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 96686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 97686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h> 98686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 99960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h> 100960ed29cSSeigo Tanimura #include <netinet6/nd6.h> 101*281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 102*281a0fd4SPatrick Kelsey #include <netinet/tcp_fastopen.h> 103*281a0fd4SPatrick Kelsey #endif 104df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 105df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 106df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 107df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 108fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 109df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 11086a996e6SHiren Panchasara #ifdef TCPPCAP 11186a996e6SHiren Panchasara #include <netinet/tcp_pcap.h> 11286a996e6SHiren Panchasara #endif 113c325962bSMike Silbersack #include <netinet/tcp_syncache.h> 114610ee2f9SDavid Greenman #ifdef TCPDEBUG 115df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 116fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */ 11709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 11809fe6320SNavdeep Parhar #include <netinet/tcp_offload.h> 11909fe6320SNavdeep Parhar #endif 120fb59c426SYoshinobu Inoue 121b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 122b9234fafSSam Leffler #include <netipsec/ipsec.h> 123b9234fafSSam Leffler #include <netipsec/ipsec6.h> 124b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 125b9234fafSSam Leffler 126db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 127db4f9cc7SJonathan Lemon 128aed55708SRobert Watson #include <security/mac/mac_framework.h> 129aed55708SRobert Watson 130dbc42409SLawrence Stewart const int tcprexmtthresh = 3; 1310312fbe9SPoul-Henning Kamp 132773673c1SAndre Oppermann int tcp_log_in_vain = 0; 133816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 134eddfbb76SRobert Watson &tcp_log_in_vain, 0, 135eddfbb76SRobert Watson "Log all incoming TCP segments to closed ports"); 136816a3d83SPoul-Henning Kamp 13782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0; 13882cea7e6SBjoern A. Zeeb #define V_blackhole VNET(blackhole) 1396df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW, 140eddfbb76SRobert Watson &VNET_NAME(blackhole), 0, 141eddfbb76SRobert Watson "Do not send RST on segments to closed ports"); 14216f7f31fSGeoff Rehmet 14382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1; 1446df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW, 145eddfbb76SRobert Watson &VNET_NAME(tcp_delack_enabled), 0, 1463d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 147f498eeeeSDavid Greenman 14882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0; 14982cea7e6SBjoern A. Zeeb #define V_drop_synfin VNET(drop_synfin) 1506df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW, 151eddfbb76SRobert Watson &VNET_NAME(drop_synfin), 0, 152eddfbb76SRobert Watson "Drop TCP packets with SYN+FIN set"); 153f8613305SDag-Erling Smørgrav 15412eeb81fSHiren Panchasara VNET_DEFINE(int, tcp_do_rfc6675_pipe) = 0; 155054d38e3SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc6675_pipe, CTLFLAG_VNET | CTLFLAG_RW, 15612eeb81fSHiren Panchasara &VNET_NAME(tcp_do_rfc6675_pipe), 0, 15712eeb81fSHiren Panchasara "Use calculated pipe/in-flight bytes per RFC 6675"); 15812eeb81fSHiren Panchasara 15982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1; 16082cea7e6SBjoern A. Zeeb #define V_tcp_do_rfc3042 VNET(tcp_do_rfc3042) 1616df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW, 162eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3042), 0, 163eddfbb76SRobert Watson "Enable RFC 3042 (Limited Transmit)"); 164582a954bSJeffrey Hsu 16582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1; 1666df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW, 167eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3390), 0, 168da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 169da3a8a1aSJeffrey Hsu 170356c7958SHiren Panchasara VNET_DEFINE(int, tcp_initcwnd_segments) = 10; 171356c7958SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments, 172356c7958SHiren Panchasara CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0, 173356c7958SHiren Panchasara "Slow-start flight size (initial congestion window) in number of segments"); 17409440655SAndre Oppermann 17582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1; 1766df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW, 177eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3465), 0, 17824cb0f22SLawrence Stewart "Enable RFC 3465 (Appropriate Byte Counting)"); 179eddfbb76SRobert Watson 18082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2; 1816df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW, 182eddfbb76SRobert Watson &VNET_NAME(tcp_abc_l_var), 2, 18324cb0f22SLawrence Stewart "Cap the max cwnd increment during slow-start to this number of segments"); 18424cb0f22SLawrence Stewart 1856472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN"); 186f2512ba1SRui Paulo 18782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_ecn) = 0; 1886df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW, 189eddfbb76SRobert Watson &VNET_NAME(tcp_do_ecn), 0, 190eddfbb76SRobert Watson "TCP ECN support"); 191eddfbb76SRobert Watson 19282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1; 1936df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_VNET | CTLFLAG_RW, 194eddfbb76SRobert Watson &VNET_NAME(tcp_ecn_maxretries), 0, 195eddfbb76SRobert Watson "Max retries before giving up on ECN"); 196eddfbb76SRobert Watson 1973220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0; 1983220a212SGleb Smirnoff #define V_tcp_insecure_syn VNET(tcp_insecure_syn) 1996df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW, 2003220a212SGleb Smirnoff &VNET_NAME(tcp_insecure_syn), 0, 2013220a212SGleb Smirnoff "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets"); 2023220a212SGleb Smirnoff 20382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0; 20482cea7e6SBjoern A. Zeeb #define V_tcp_insecure_rst VNET(tcp_insecure_rst) 2056df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW, 206eddfbb76SRobert Watson &VNET_NAME(tcp_insecure_rst), 0, 2073220a212SGleb Smirnoff "Follow RFC793 instead of RFC5961 criteria for accepting RST packets"); 208a69968eeSMike Silbersack 209fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64; 210873789cbSAndre Oppermann #define V_tcp_recvspace VNET(tcp_recvspace) 2116df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW, 212873789cbSAndre Oppermann &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size"); 213873789cbSAndre Oppermann 21482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1; 21582cea7e6SBjoern A. Zeeb #define V_tcp_do_autorcvbuf VNET(tcp_do_autorcvbuf) 2166df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW, 217eddfbb76SRobert Watson &VNET_NAME(tcp_do_autorcvbuf), 0, 2188b615593SMarko Zec "Enable automatic receive buffer sizing"); 2196741ecf5SAndre Oppermann 22082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024; 22182cea7e6SBjoern A. Zeeb #define V_tcp_autorcvbuf_inc VNET(tcp_autorcvbuf_inc) 2226df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_VNET | CTLFLAG_RW, 223eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_inc), 0, 2246489fe65SAndre Oppermann "Incrementor step size of automatic receive buffer"); 2256741ecf5SAndre Oppermann 226b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024; 22782cea7e6SBjoern A. Zeeb #define V_tcp_autorcvbuf_max VNET(tcp_autorcvbuf_max) 2286df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW, 229eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_max), 0, 2308b615593SMarko Zec "Max size of automatic receive buffer"); 2316741ecf5SAndre Oppermann 232eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb); 23344e33a07SMarko Zec #define tcb6 tcb /* for KAME src sync over BSD*'s */ 23482cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo); 235df8bae1dSRodney W. Grimes 236315e3e38SRobert Watson /* 2375da0521fSAndrey V. Elsukov * TCP statistics are stored in an "array" of counter(9)s. 2385923c293SGleb Smirnoff */ 2395da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat); 2405da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(tcpstat); 2415da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat, 2425da0521fSAndrey V. Elsukov tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 2435923c293SGleb Smirnoff 2445923c293SGleb Smirnoff #ifdef VIMAGE 2455da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(tcpstat); 2465923c293SGleb Smirnoff #endif /* VIMAGE */ 2475923c293SGleb Smirnoff /* 248315e3e38SRobert Watson * Kernel module interface for updating tcpstat. The argument is an index 2495923c293SGleb Smirnoff * into tcpstat treated as an array. 250315e3e38SRobert Watson */ 251315e3e38SRobert Watson void 252315e3e38SRobert Watson kmod_tcpstat_inc(int statnum) 253315e3e38SRobert Watson { 254315e3e38SRobert Watson 2555da0521fSAndrey V. Elsukov counter_u64_add(VNET(tcpstat)[statnum], 1); 256315e3e38SRobert Watson } 257315e3e38SRobert Watson 258dbc42409SLawrence Stewart /* 25939bc9de5SLawrence Stewart * Wrapper for the TCP established input helper hook. 26039bc9de5SLawrence Stewart */ 26155bceb1eSRandall Stewart void 26239bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to) 26339bc9de5SLawrence Stewart { 26439bc9de5SLawrence Stewart struct tcp_hhook_data hhook_data; 26539bc9de5SLawrence Stewart 26639bc9de5SLawrence Stewart if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) { 26739bc9de5SLawrence Stewart hhook_data.tp = tp; 26839bc9de5SLawrence Stewart hhook_data.th = th; 26939bc9de5SLawrence Stewart hhook_data.to = to; 27039bc9de5SLawrence Stewart 27139bc9de5SLawrence Stewart hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data, 27239bc9de5SLawrence Stewart tp->osd); 27339bc9de5SLawrence Stewart } 27439bc9de5SLawrence Stewart } 27539bc9de5SLawrence Stewart 27639bc9de5SLawrence Stewart /* 277dbc42409SLawrence Stewart * CC wrapper hook functions 278dbc42409SLawrence Stewart */ 27955bceb1eSRandall Stewart void 280dbc42409SLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t type) 281f2512ba1SRui Paulo { 282dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 283f2512ba1SRui Paulo 284dbc42409SLawrence Stewart tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th); 2855b648e79SLawrence Stewart if (tp->snd_cwnd <= tp->snd_wnd) 286dbc42409SLawrence Stewart tp->ccv->flags |= CCF_CWND_LIMITED; 287dbc42409SLawrence Stewart else 288dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_CWND_LIMITED; 289dbc42409SLawrence Stewart 290dbc42409SLawrence Stewart if (type == CC_ACK) { 291dbc42409SLawrence Stewart if (tp->snd_cwnd > tp->snd_ssthresh) { 292dbc42409SLawrence Stewart tp->t_bytes_acked += min(tp->ccv->bytes_this_ack, 293dbc42409SLawrence Stewart V_tcp_abc_l_var * tp->t_maxseg); 294dbc42409SLawrence Stewart if (tp->t_bytes_acked >= tp->snd_cwnd) { 295dbc42409SLawrence Stewart tp->t_bytes_acked -= tp->snd_cwnd; 296dbc42409SLawrence Stewart tp->ccv->flags |= CCF_ABC_SENTAWND; 297dbc42409SLawrence Stewart } 298dbc42409SLawrence Stewart } else { 299dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_ABC_SENTAWND; 300dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 301dbc42409SLawrence Stewart } 302dbc42409SLawrence Stewart } 303dbc42409SLawrence Stewart 304dbc42409SLawrence Stewart if (CC_ALGO(tp)->ack_received != NULL) { 305dbc42409SLawrence Stewart /* XXXLAS: Find a way to live without this */ 306dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 307dbc42409SLawrence Stewart CC_ALGO(tp)->ack_received(tp->ccv, type); 308dbc42409SLawrence Stewart } 309dbc42409SLawrence Stewart } 310dbc42409SLawrence Stewart 31155bceb1eSRandall Stewart void 312dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp) 313dbc42409SLawrence Stewart { 314dbc42409SLawrence Stewart struct hc_metrics_lite metrics; 315dbc42409SLawrence Stewart struct inpcb *inp = tp->t_inpcb; 316dbc42409SLawrence Stewart int rtt; 317dbc42409SLawrence Stewart 318dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 319dbc42409SLawrence Stewart 320dbc42409SLawrence Stewart tcp_hc_get(&inp->inp_inc, &metrics); 321dbc42409SLawrence Stewart 322dbc42409SLawrence Stewart if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 323dbc42409SLawrence Stewart tp->t_srtt = rtt; 324dbc42409SLawrence Stewart tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 325dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrtt); 326dbc42409SLawrence Stewart if (metrics.rmx_rttvar) { 327dbc42409SLawrence Stewart tp->t_rttvar = metrics.rmx_rttvar; 328dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrttvar); 329dbc42409SLawrence Stewart } else { 330dbc42409SLawrence Stewart /* default variation is +- 1 rtt */ 331dbc42409SLawrence Stewart tp->t_rttvar = 332dbc42409SLawrence Stewart tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 333dbc42409SLawrence Stewart } 334dbc42409SLawrence Stewart TCPT_RANGESET(tp->t_rxtcur, 335dbc42409SLawrence Stewart ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 336dbc42409SLawrence Stewart tp->t_rttmin, TCPTV_REXMTMAX); 337dbc42409SLawrence Stewart } 338dbc42409SLawrence Stewart if (metrics.rmx_ssthresh) { 339dbc42409SLawrence Stewart /* 340dbc42409SLawrence Stewart * There's some sort of gateway or interface 341dbc42409SLawrence Stewart * buffer limit on the path. Use this to set 342dbc42409SLawrence Stewart * the slow start threshhold, but set the 343dbc42409SLawrence Stewart * threshold to no less than 2*mss. 344dbc42409SLawrence Stewart */ 345dbc42409SLawrence Stewart tp->snd_ssthresh = max(2 * tp->t_maxseg, metrics.rmx_ssthresh); 346dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedssthresh); 347dbc42409SLawrence Stewart } 348dbc42409SLawrence Stewart 349dbc42409SLawrence Stewart /* 3509ec4a4ccSAndre Oppermann * Set the initial slow-start flight size. 351dbc42409SLawrence Stewart * 352cf8f04f4SAndre Oppermann * RFC5681 Section 3.1 specifies the default conservative values. 353cf8f04f4SAndre Oppermann * RFC3390 specifies slightly more aggressive values. 354b8b4cfcdSSergey Kandaurov * RFC6928 increases it to ten segments. 355356c7958SHiren Panchasara * Support for user specified value for initial flight size. 356cf8f04f4SAndre Oppermann * 357cf8f04f4SAndre Oppermann * If a SYN or SYN/ACK was lost and retransmitted, we have to 358cf8f04f4SAndre Oppermann * reduce the initial CWND to one segment as congestion is likely 359cf8f04f4SAndre Oppermann * requiring us to be cautious. 360dbc42409SLawrence Stewart */ 361cf8f04f4SAndre Oppermann if (tp->snd_cwnd == 1) 362cf8f04f4SAndre Oppermann tp->snd_cwnd = tp->t_maxseg; /* SYN(-ACK) lost */ 363356c7958SHiren Panchasara else if (V_tcp_initcwnd_segments) 364356c7958SHiren Panchasara tp->snd_cwnd = min(V_tcp_initcwnd_segments * tp->t_maxseg, 365356c7958SHiren Panchasara max(2 * tp->t_maxseg, V_tcp_initcwnd_segments * 1460)); 366cf8f04f4SAndre Oppermann else if (V_tcp_do_rfc3390) 367dbc42409SLawrence Stewart tp->snd_cwnd = min(4 * tp->t_maxseg, 368dbc42409SLawrence Stewart max(2 * tp->t_maxseg, 4380)); 36922efabd4SAndre Oppermann else { 37022efabd4SAndre Oppermann /* Per RFC5681 Section 3.1 */ 37122efabd4SAndre Oppermann if (tp->t_maxseg > 2190) 37222efabd4SAndre Oppermann tp->snd_cwnd = 2 * tp->t_maxseg; 37322efabd4SAndre Oppermann else if (tp->t_maxseg > 1095) 37422efabd4SAndre Oppermann tp->snd_cwnd = 3 * tp->t_maxseg; 375dbc42409SLawrence Stewart else 37622efabd4SAndre Oppermann tp->snd_cwnd = 4 * tp->t_maxseg; 37722efabd4SAndre Oppermann } 378dbc42409SLawrence Stewart 379dbc42409SLawrence Stewart if (CC_ALGO(tp)->conn_init != NULL) 380dbc42409SLawrence Stewart CC_ALGO(tp)->conn_init(tp->ccv); 381dbc42409SLawrence Stewart } 382dbc42409SLawrence Stewart 383dbc42409SLawrence Stewart void inline 384dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type) 385dbc42409SLawrence Stewart { 386dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 387dbc42409SLawrence Stewart 388dbc42409SLawrence Stewart switch(type) { 389dbc42409SLawrence Stewart case CC_NDUPACK: 390dbc42409SLawrence Stewart if (!IN_FASTRECOVERY(tp->t_flags)) { 391f2512ba1SRui Paulo tp->snd_recover = tp->snd_max; 392f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) 393f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_CWR; 394f2512ba1SRui Paulo } 395dbc42409SLawrence Stewart break; 396dbc42409SLawrence Stewart case CC_ECN: 397dbc42409SLawrence Stewart if (!IN_CONGRECOVERY(tp->t_flags)) { 398dbc42409SLawrence Stewart TCPSTAT_INC(tcps_ecn_rcwnd); 399dbc42409SLawrence Stewart tp->snd_recover = tp->snd_max; 400dbc42409SLawrence Stewart if (tp->t_flags & TF_ECN_PERMIT) 401dbc42409SLawrence Stewart tp->t_flags |= TF_ECN_SND_CWR; 402dbc42409SLawrence Stewart } 403dbc42409SLawrence Stewart break; 404dbc42409SLawrence Stewart case CC_RTO: 405dbc42409SLawrence Stewart tp->t_dupacks = 0; 406dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 407dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 4086157935fSLawrence Stewart tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 / 4096157935fSLawrence Stewart tp->t_maxseg) * tp->t_maxseg; 410dbc42409SLawrence Stewart tp->snd_cwnd = tp->t_maxseg; 411dbc42409SLawrence Stewart break; 412dbc42409SLawrence Stewart case CC_RTO_ERR: 413dbc42409SLawrence Stewart TCPSTAT_INC(tcps_sndrexmitbad); 414dbc42409SLawrence Stewart /* RTO was unnecessary, so reset everything. */ 415dbc42409SLawrence Stewart tp->snd_cwnd = tp->snd_cwnd_prev; 416dbc42409SLawrence Stewart tp->snd_ssthresh = tp->snd_ssthresh_prev; 417dbc42409SLawrence Stewart tp->snd_recover = tp->snd_recover_prev; 418dbc42409SLawrence Stewart if (tp->t_flags & TF_WASFRECOVERY) 419dbc42409SLawrence Stewart ENTER_FASTRECOVERY(tp->t_flags); 420dbc42409SLawrence Stewart if (tp->t_flags & TF_WASCRECOVERY) 421dbc42409SLawrence Stewart ENTER_CONGRECOVERY(tp->t_flags); 422dbc42409SLawrence Stewart tp->snd_nxt = tp->snd_max; 423672dc4aeSJohn Baldwin tp->t_flags &= ~TF_PREVVALID; 424dbc42409SLawrence Stewart tp->t_badrxtwin = 0; 425dbc42409SLawrence Stewart break; 426dbc42409SLawrence Stewart } 427dbc42409SLawrence Stewart 428dbc42409SLawrence Stewart if (CC_ALGO(tp)->cong_signal != NULL) { 429dbc42409SLawrence Stewart if (th != NULL) 430dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 431dbc42409SLawrence Stewart CC_ALGO(tp)->cong_signal(tp->ccv, type); 432dbc42409SLawrence Stewart } 433dbc42409SLawrence Stewart } 434dbc42409SLawrence Stewart 43555bceb1eSRandall Stewart void inline 436dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th) 437dbc42409SLawrence Stewart { 438dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 439dbc42409SLawrence Stewart 440dbc42409SLawrence Stewart /* XXXLAS: KASSERT that we're in recovery? */ 441dbc42409SLawrence Stewart 442dbc42409SLawrence Stewart if (CC_ALGO(tp)->post_recovery != NULL) { 443dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 444dbc42409SLawrence Stewart CC_ALGO(tp)->post_recovery(tp->ccv); 445dbc42409SLawrence Stewart } 446dbc42409SLawrence Stewart /* XXXLAS: EXIT_RECOVERY ? */ 447dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 448dbc42409SLawrence Stewart } 4490312fbe9SPoul-Henning Kamp 4502903309aSAttilio Rao #ifdef TCP_SIGNATURE 4512903309aSAttilio Rao static inline int 4522903309aSAttilio Rao tcp_signature_verify_input(struct mbuf *m, int off0, int tlen, int optlen, 4532903309aSAttilio Rao struct tcpopt *to, struct tcphdr *th, u_int tcpbflag) 4542903309aSAttilio Rao { 4552903309aSAttilio Rao int ret; 4562903309aSAttilio Rao 4572903309aSAttilio Rao tcp_fields_to_net(th); 4582903309aSAttilio Rao ret = tcp_signature_verify(m, off0, tlen, optlen, to, th, tcpbflag); 4592903309aSAttilio Rao tcp_fields_to_host(th); 4602903309aSAttilio Rao return (ret); 4612903309aSAttilio Rao } 4622903309aSAttilio Rao #endif 4632903309aSAttilio Rao 464df8bae1dSRodney W. Grimes /* 465262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 46685536381SHiren Panchasara * following conditions are met: 46785536381SHiren Panchasara * - There is no delayed ack timer in progress. 46885536381SHiren Panchasara * - Our last ack wasn't a 0-sized window. We never want to delay 46985536381SHiren Panchasara * the ack that opens up a 0-sized window. 47085536381SHiren Panchasara * - LRO wasn't used for this segment. We make sure by checking that the 47185536381SHiren Panchasara * segment size is not larger than the MSS. 47285536381SHiren Panchasara * - Delayed acks are enabled or this is a half-synchronized T/TCP 47385536381SHiren Panchasara * connection. 474d8c85a26SJonathan Lemon */ 475c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen) \ 476b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 477a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 478c1e5a6e5SAndre Oppermann (tlen <= tp->t_maxopd) && \ 479603724d3SBjoern A. Zeeb (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 480d8c85a26SJonathan Lemon 48164807b30SHiren Panchasara static void inline 48264807b30SHiren Panchasara cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos) 48364807b30SHiren Panchasara { 48464807b30SHiren Panchasara INP_WLOCK_ASSERT(tp->t_inpcb); 48564807b30SHiren Panchasara 48664807b30SHiren Panchasara if (CC_ALGO(tp)->ecnpkt_handler != NULL) { 48764807b30SHiren Panchasara switch (iptos & IPTOS_ECN_MASK) { 48864807b30SHiren Panchasara case IPTOS_ECN_CE: 48964807b30SHiren Panchasara tp->ccv->flags |= CCF_IPHDR_CE; 49064807b30SHiren Panchasara break; 49164807b30SHiren Panchasara case IPTOS_ECN_ECT0: 49264807b30SHiren Panchasara tp->ccv->flags &= ~CCF_IPHDR_CE; 49364807b30SHiren Panchasara break; 49464807b30SHiren Panchasara case IPTOS_ECN_ECT1: 49564807b30SHiren Panchasara tp->ccv->flags &= ~CCF_IPHDR_CE; 49664807b30SHiren Panchasara break; 49764807b30SHiren Panchasara } 49864807b30SHiren Panchasara 49964807b30SHiren Panchasara if (th->th_flags & TH_CWR) 50064807b30SHiren Panchasara tp->ccv->flags |= CCF_TCPHDR_CWR; 50164807b30SHiren Panchasara else 50264807b30SHiren Panchasara tp->ccv->flags &= ~CCF_TCPHDR_CWR; 50364807b30SHiren Panchasara 50464807b30SHiren Panchasara if (tp->t_flags & TF_DELACK) 50564807b30SHiren Panchasara tp->ccv->flags |= CCF_DELACK; 50664807b30SHiren Panchasara else 50764807b30SHiren Panchasara tp->ccv->flags &= ~CCF_DELACK; 50864807b30SHiren Panchasara 50964807b30SHiren Panchasara CC_ALGO(tp)->ecnpkt_handler(tp->ccv); 51064807b30SHiren Panchasara 51164807b30SHiren Panchasara if (tp->ccv->flags & CCF_ACKNOW) 51264807b30SHiren Panchasara tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 51364807b30SHiren Panchasara } 51464807b30SHiren Panchasara } 51564807b30SHiren Panchasara 516df8bae1dSRodney W. Grimes /* 5178d573cc1SAndre Oppermann * TCP input handling is split into multiple parts: 5188d573cc1SAndre Oppermann * tcp6_input is a thin wrapper around tcp_input for the extended 5198d573cc1SAndre Oppermann * ip6_protox[] call format in ip6_input 5208d573cc1SAndre Oppermann * tcp_input handles primary segment validation, inpcb lookup and 5218d573cc1SAndre Oppermann * SYN processing on listen sockets 5228d573cc1SAndre Oppermann * tcp_do_segment processes the ACK and text of the segment for 5238d573cc1SAndre Oppermann * establishing, established and closing connections 524df8bae1dSRodney W. Grimes */ 525fb59c426SYoshinobu Inoue #ifdef INET6 526fb59c426SYoshinobu Inoue int 527ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto) 528fb59c426SYoshinobu Inoue { 529ad3f9ab3SAndre Oppermann struct mbuf *m = *mp; 53033841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 5313e88eb90SAndrey V. Elsukov struct ip6_hdr *ip6; 532fb59c426SYoshinobu Inoue 533fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 534fb59c426SYoshinobu Inoue 535fb59c426SYoshinobu Inoue /* 536fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 537fb59c426SYoshinobu Inoue * better place to put this in? 538fb59c426SYoshinobu Inoue */ 5393e88eb90SAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 5403e88eb90SAndrey V. Elsukov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */); 54133841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 542fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 543fb59c426SYoshinobu Inoue 5448c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 545fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 546fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 547fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 5488f5a8818SKevin Lo return (IPPROTO_DONE); 549fb59c426SYoshinobu Inoue } 55077d396fdSMaksim Yevmenkin if (ia6) 55177d396fdSMaksim Yevmenkin ifa_free(&ia6->ia_ifa); 552fb59c426SYoshinobu Inoue 5538f5a8818SKevin Lo return (tcp_input(mp, offp, proto)); 554fb59c426SYoshinobu Inoue } 555b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 556fb59c426SYoshinobu Inoue 5578f5a8818SKevin Lo int 5588f5a8818SKevin Lo tcp_input(struct mbuf **mp, int *offp, int proto) 559df8bae1dSRodney W. Grimes { 5608f5a8818SKevin Lo struct mbuf *m = *mp; 561b287c6c7SBjoern A. Zeeb struct tcphdr *th = NULL; 562ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 563ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 564302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 565302ce8d6SAndre Oppermann struct socket *so = NULL; 566e79adb8eSGarrett Wollman u_char *optp = NULL; 5678f5a8818SKevin Lo int off0; 56826f9a767SRodney W. Grimes int optlen = 0; 569b287c6c7SBjoern A. Zeeb #ifdef INET 570b287c6c7SBjoern A. Zeeb int len; 571b287c6c7SBjoern A. Zeeb #endif 572b287c6c7SBjoern A. Zeeb int tlen = 0, off; 573fb59c426SYoshinobu Inoue int drop_hdrlen; 574ad3f9ab3SAndre Oppermann int thflags; 575302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 5762903309aSAttilio Rao #ifdef TCP_SIGNATURE 5772903309aSAttilio Rao uint8_t sig_checked = 0; 5782903309aSAttilio Rao #endif 579b287c6c7SBjoern A. Zeeb uint8_t iptos = 0; 580c1de64a4SAndrey V. Elsukov struct m_tag *fwd_tag = NULL; 581c068736aSJeffrey Hsu #ifdef INET6 582302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 583c068736aSJeffrey Hsu int isipv6; 584c068736aSJeffrey Hsu #else 585a160e630SAndre Oppermann const void *ip6 = NULL; 586b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 587302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 588a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 589252ca428SRobert Watson int ti_locked; 590610ee2f9SDavid Greenman #ifdef TCPDEBUG 591c068736aSJeffrey Hsu /* 592c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 593c068736aSJeffrey Hsu * now IPv6. 594c068736aSJeffrey Hsu */ 59514739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 596410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 597610ee2f9SDavid Greenman short ostate = 0; 598610ee2f9SDavid Greenman #endif 599c068736aSJeffrey Hsu 600fb59c426SYoshinobu Inoue #ifdef INET6 601fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 602fb59c426SYoshinobu Inoue #endif 603a0292f23SGarrett Wollman 6048f5a8818SKevin Lo off0 = *offp; 6058f5a8818SKevin Lo m = *mp; 6068f5a8818SKevin Lo *mp = NULL; 607302ce8d6SAndre Oppermann to.to_flags = 0; 60878b50714SRobert Watson TCPSTAT_INC(tcps_rcvtotal); 609fb59c426SYoshinobu Inoue 61004d3a452SHajimu UMEMOTO #ifdef INET6 611b287c6c7SBjoern A. Zeeb if (isipv6) { 6128d573cc1SAndre Oppermann /* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */ 61345747ba5SBjoern A. Zeeb 61445747ba5SBjoern A. Zeeb if (m->m_len < (sizeof(*ip6) + sizeof(*th))) { 61545747ba5SBjoern A. Zeeb m = m_pullup(m, sizeof(*ip6) + sizeof(*th)); 61645747ba5SBjoern A. Zeeb if (m == NULL) { 61745747ba5SBjoern A. Zeeb TCPSTAT_INC(tcps_rcvshort); 6188f5a8818SKevin Lo return (IPPROTO_DONE); 61945747ba5SBjoern A. Zeeb } 62045747ba5SBjoern A. Zeeb } 62145747ba5SBjoern A. Zeeb 622fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 62345747ba5SBjoern A. Zeeb th = (struct tcphdr *)((caddr_t)ip6 + off0); 624fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 625356ab07eSBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) { 62645747ba5SBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 62745747ba5SBjoern A. Zeeb th->th_sum = m->m_pkthdr.csum_data; 62845747ba5SBjoern A. Zeeb else 62945747ba5SBjoern A. Zeeb th->th_sum = in6_cksum_pseudo(ip6, tlen, 63045747ba5SBjoern A. Zeeb IPPROTO_TCP, m->m_pkthdr.csum_data); 63145747ba5SBjoern A. Zeeb th->th_sum ^= 0xffff; 63245747ba5SBjoern A. Zeeb } else 63345747ba5SBjoern A. Zeeb th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen); 63445747ba5SBjoern A. Zeeb if (th->th_sum) { 63578b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 636fb59c426SYoshinobu Inoue goto drop; 637fb59c426SYoshinobu Inoue } 63833841545SHajimu UMEMOTO 63933841545SHajimu UMEMOTO /* 64033841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 64133841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 64233841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 64333841545SHajimu UMEMOTO * 64433841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 64533841545SHajimu UMEMOTO * already dropped in ip6_input. 64633841545SHajimu UMEMOTO */ 64733841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 64833841545SHajimu UMEMOTO /* XXX stat */ 64933841545SHajimu UMEMOTO goto drop; 65033841545SHajimu UMEMOTO } 651b287c6c7SBjoern A. Zeeb } 65204d3a452SHajimu UMEMOTO #endif 653b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 654b287c6c7SBjoern A. Zeeb else 655b287c6c7SBjoern A. Zeeb #endif 656b287c6c7SBjoern A. Zeeb #ifdef INET 657b287c6c7SBjoern A. Zeeb { 658df8bae1dSRodney W. Grimes /* 659df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 660df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 661df8bae1dSRodney W. Grimes */ 662fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 663105bd211SGleb Smirnoff ip_stripoptions(m); 664fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 665fb59c426SYoshinobu Inoue } 666df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 6674dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 6684dfdffe9SAndre Oppermann == NULL) { 66978b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 6708f5a8818SKevin Lo return (IPPROTO_DONE); 671df8bae1dSRodney W. Grimes } 672df8bae1dSRodney W. Grimes } 673fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 674db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 6758ad458a4SGleb Smirnoff tlen = ntohs(ip->ip_len) - off0; 676df8bae1dSRodney W. Grimes 677db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 678db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 679db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 680db4f9cc7SJonathan Lemon else 681db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 682c068736aSJeffrey Hsu ip->ip_dst.s_addr, 6838f134647SGleb Smirnoff htonl(m->m_pkthdr.csum_data + tlen + 684c068736aSJeffrey Hsu IPPROTO_TCP)); 685db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 686db4f9cc7SJonathan Lemon } else { 6878f134647SGleb Smirnoff struct ipovly *ipov = (struct ipovly *)ip; 6888f134647SGleb Smirnoff 689df8bae1dSRodney W. Grimes /* 690df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 691df8bae1dSRodney W. Grimes */ 6928f134647SGleb Smirnoff len = off0 + tlen; 693fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 6948f134647SGleb Smirnoff ipov->ih_len = htons(tlen); 695fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 69657f60867SMark Johnston /* Reset length for SDT probes. */ 69757f60867SMark Johnston ip->ip_len = htons(tlen + off0); 698db4f9cc7SJonathan Lemon } 69957f60867SMark Johnston 700fb59c426SYoshinobu Inoue if (th->th_sum) { 70178b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 702df8bae1dSRodney W. Grimes goto drop; 703df8bae1dSRodney W. Grimes } 704fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 705fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 706fb59c426SYoshinobu Inoue } 707b287c6c7SBjoern A. Zeeb #endif /* INET */ 708df8bae1dSRodney W. Grimes 709f2512ba1SRui Paulo #ifdef INET6 710f2512ba1SRui Paulo if (isipv6) 711f2512ba1SRui Paulo iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 712b287c6c7SBjoern A. Zeeb #endif 713b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 714f2512ba1SRui Paulo else 715f2512ba1SRui Paulo #endif 716b287c6c7SBjoern A. Zeeb #ifdef INET 717f2512ba1SRui Paulo iptos = ip->ip_tos; 718b287c6c7SBjoern A. Zeeb #endif 719f2512ba1SRui Paulo 720df8bae1dSRodney W. Grimes /* 721df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 722df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 723df8bae1dSRodney W. Grimes */ 724fb59c426SYoshinobu Inoue off = th->th_off << 2; 725df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 72678b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadoff); 727df8bae1dSRodney W. Grimes goto drop; 728df8bae1dSRodney W. Grimes } 729fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 730df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 73104d3a452SHajimu UMEMOTO #ifdef INET6 732b287c6c7SBjoern A. Zeeb if (isipv6) { 7338f5a8818SKevin Lo IP6_EXTHDR_CHECK(m, off0, off, IPPROTO_DONE); 734fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 735fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 736b287c6c7SBjoern A. Zeeb } 73704d3a452SHajimu UMEMOTO #endif 738b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 739b287c6c7SBjoern A. Zeeb else 740b287c6c7SBjoern A. Zeeb #endif 741b287c6c7SBjoern A. Zeeb #ifdef INET 742b287c6c7SBjoern A. Zeeb { 743df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 744c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 7454dfdffe9SAndre Oppermann == NULL) { 74678b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 7478f5a8818SKevin Lo return (IPPROTO_DONE); 748df8bae1dSRodney W. Grimes } 749fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 750fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 751fb59c426SYoshinobu Inoue } 752df8bae1dSRodney W. Grimes } 753b287c6c7SBjoern A. Zeeb #endif 754df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 755fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 756df8bae1dSRodney W. Grimes } 757fb59c426SYoshinobu Inoue thflags = th->th_flags; 758df8bae1dSRodney W. Grimes 759e46cd3d4SDag-Erling Smørgrav /* 760df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 761df8bae1dSRodney W. Grimes */ 7622903309aSAttilio Rao tcp_fields_to_host(th); 763df8bae1dSRodney W. Grimes 764df8bae1dSRodney W. Grimes /* 765794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 766755c1f07SAndras Olah */ 767fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 768755c1f07SAndras Olah 769755c1f07SAndras Olah /* 770fa046d87SRobert Watson * Locate pcb for segment; if we're likely to add or remove a 771a7c7f2a7SJohn Baldwin * connection then first acquire pcbinfo lock. There are three cases 772fa046d87SRobert Watson * where we might discover later we need a write lock despite the 773a7c7f2a7SJohn Baldwin * flags: ACKs moving a connection out of the syncache, ACKs for a 774a7c7f2a7SJohn Baldwin * connection in TIMEWAIT and SYNs not targeting a listening socket. 775df8bae1dSRodney W. Grimes */ 776a7c7f2a7SJohn Baldwin if ((thflags & (TH_FIN | TH_RST)) != 0) { 777ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 778ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 779fa046d87SRobert Watson } else 780fa046d87SRobert Watson ti_locked = TI_UNLOCKED; 781252ca428SRobert Watson 7828d573cc1SAndre Oppermann /* 7838d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 7848d573cc1SAndre Oppermann */ 785b8056faeSGleb Smirnoff if ( 786b8056faeSGleb Smirnoff #ifdef INET6 787b8056faeSGleb Smirnoff (isipv6 && (m->m_flags & M_IP6_NEXTHOP)) 788b8056faeSGleb Smirnoff #ifdef INET 789b8056faeSGleb Smirnoff || (!isipv6 && (m->m_flags & M_IP_NEXTHOP)) 790b8056faeSGleb Smirnoff #endif 791b8056faeSGleb Smirnoff #endif 792b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6) 793b8056faeSGleb Smirnoff (m->m_flags & M_IP_NEXTHOP) 794b8056faeSGleb Smirnoff #endif 795b8056faeSGleb Smirnoff ) 7969b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 7979b932e9eSAndre Oppermann 798ce7ad664SEd Maste findpcb: 799ce7ad664SEd Maste #ifdef INVARIANTS 800ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 801ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 802ce7ad664SEd Maste } else { 803ce7ad664SEd Maste INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 804ce7ad664SEd Maste } 805ce7ad664SEd Maste #endif 8068a006adbSBjoern A. Zeeb #ifdef INET6 8078a006adbSBjoern A. Zeeb if (isipv6 && fwd_tag != NULL) { 8088a006adbSBjoern A. Zeeb struct sockaddr_in6 *next_hop6; 8098a006adbSBjoern A. Zeeb 8108a006adbSBjoern A. Zeeb next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1); 8118a006adbSBjoern A. Zeeb /* 8128a006adbSBjoern A. Zeeb * Transparently forwarded. Pretend to be the destination. 8138a006adbSBjoern A. Zeeb * Already got one like this? 8148a006adbSBjoern A. Zeeb */ 8158a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, 8168a006adbSBjoern A. Zeeb &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport, 8178a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m); 8188a006adbSBjoern A. Zeeb if (!inp) { 8198a006adbSBjoern A. Zeeb /* 8208a006adbSBjoern A. Zeeb * It's new. Try to find the ambushing socket. 8218a006adbSBjoern A. Zeeb * Because we've rewritten the destination address, 8228a006adbSBjoern A. Zeeb * any hardware-generated hash is ignored. 8238a006adbSBjoern A. Zeeb */ 8248a006adbSBjoern A. Zeeb inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src, 8258a006adbSBjoern A. Zeeb th->th_sport, &next_hop6->sin6_addr, 8268a006adbSBjoern A. Zeeb next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) : 8278a006adbSBjoern A. Zeeb th->th_dport, INPLOOKUP_WILDCARD | 8288a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 8298a006adbSBjoern A. Zeeb } 830c1de64a4SAndrey V. Elsukov } else if (isipv6) { 8318a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src, 8328a006adbSBjoern A. Zeeb th->th_sport, &ip6->ip6_dst, th->th_dport, 8338a006adbSBjoern A. Zeeb INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 8348a006adbSBjoern A. Zeeb m->m_pkthdr.rcvif, m); 8358a006adbSBjoern A. Zeeb } 8368a006adbSBjoern A. Zeeb #endif /* INET6 */ 8378a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET) 8388a006adbSBjoern A. Zeeb else 8398a006adbSBjoern A. Zeeb #endif 8408a006adbSBjoern A. Zeeb #ifdef INET 8418a006adbSBjoern A. Zeeb if (fwd_tag != NULL) { 8429b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 8439b932e9eSAndre Oppermann 8449b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 845f9e354dfSJulian Elischer /* 8462b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 847f9e354dfSJulian Elischer * already got one like this? 848f9e354dfSJulian Elischer */ 849d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport, 850fa046d87SRobert Watson ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB, 851d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 852fa046d87SRobert Watson if (!inp) { 853fa046d87SRobert Watson /* 854fa046d87SRobert Watson * It's new. Try to find the ambushing socket. 855d3c1f003SRobert Watson * Because we've rewritten the destination address, 856d3c1f003SRobert Watson * any hardware-generated hash is ignored. 857fa046d87SRobert Watson */ 858fa046d87SRobert Watson inp = in_pcblookup(&V_tcbinfo, ip->ip_src, 859fa046d87SRobert Watson th->th_sport, next_hop->sin_addr, 860fa046d87SRobert Watson next_hop->sin_port ? ntohs(next_hop->sin_port) : 861fa046d87SRobert Watson th->th_dport, INPLOOKUP_WILDCARD | 862fa046d87SRobert Watson INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 863f9e354dfSJulian Elischer } 864b10fbdeaSAndre Oppermann } else 865d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, 866fa046d87SRobert Watson th->th_sport, ip->ip_dst, th->th_dport, 867fa046d87SRobert Watson INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 868d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 8698a006adbSBjoern A. Zeeb #endif /* INET */ 870f9e354dfSJulian Elischer 871df8bae1dSRodney W. Grimes /* 872574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 873574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 8748b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 875df8bae1dSRodney W. Grimes */ 876816a3d83SPoul-Henning Kamp if (inp == NULL) { 877574b6964SAndre Oppermann /* 878574b6964SAndre Oppermann * Log communication attempts to ports that are not 879574b6964SAndre Oppermann * in use. 880574b6964SAndre Oppermann */ 881574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 882574b6964SAndre Oppermann tcp_log_in_vain == 2) { 883b7d747ecSAndre Oppermann if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6))) 884df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 885df541e5fSAndre Oppermann "to closed port\n", s, __func__); 8862e4e1b4cSGeoff Rehmet } 887574b6964SAndre Oppermann /* 888574b6964SAndre Oppermann * When blackholing do not respond with a RST but 889574b6964SAndre Oppermann * completely ignore the segment and drop it. 890574b6964SAndre Oppermann */ 891603724d3SBjoern A. Zeeb if ((V_blackhole == 1 && (thflags & TH_SYN)) || 892603724d3SBjoern A. Zeeb V_blackhole == 2) 8931929eae1SAndre Oppermann goto dropunlock; 894574b6964SAndre Oppermann 895a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 896a57815efSBosko Milekic goto dropwithreset; 897816a3d83SPoul-Henning Kamp } 898fa046d87SRobert Watson INP_WLOCK_ASSERT(inp); 899c2529042SHans Petter Selasky if ((inp->inp_flowtype == M_HASHTYPE_NONE) && 900c2529042SHans Petter Selasky (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) && 901c2529042SHans Petter Selasky ((inp->inp_socket == NULL) || 902c2529042SHans Petter Selasky (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0)) { 90380cb9f21SKip Macy inp->inp_flowid = m->m_pkthdr.flowid; 9042f719932SAdrian Chadd inp->inp_flowtype = M_HASHTYPE_GET(m); 90580cb9f21SKip Macy } 906a2589465SKen Smith #ifdef IPSEC 907a2589465SKen Smith #ifdef INET6 908a2589465SKen Smith if (isipv6 && ipsec6_in_reject(m, inp)) { 909a2589465SKen Smith goto dropunlock; 910a2589465SKen Smith } else 911a2589465SKen Smith #endif /* INET6 */ 912a2589465SKen Smith if (ipsec4_in_reject(m, inp) != 0) { 913a2589465SKen Smith goto dropunlock; 914a2589465SKen Smith } 915a2589465SKen Smith #endif /* IPSEC */ 916a2589465SKen Smith 9178d573cc1SAndre Oppermann /* 9188d573cc1SAndre Oppermann * Check the minimum TTL for socket. 9198d573cc1SAndre Oppermann */ 92034f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 92134f83c52SGeorge V. Neville-Neil #ifdef INET6 92234f83c52SGeorge V. Neville-Neil if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim) 923302ce8d6SAndre Oppermann goto dropunlock; 92402707462SAndre Oppermann else 92534f83c52SGeorge V. Neville-Neil #endif 92634f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 927302ce8d6SAndre Oppermann goto dropunlock; 92834f83c52SGeorge V. Neville-Neil } 929936cd18dSAndre Oppermann 930340c35deSJonathan Lemon /* 931252ca428SRobert Watson * A previous connection in TIMEWAIT state is supposed to catch stray 932252ca428SRobert Watson * or duplicate segments arriving late. If this segment was a 9330989f56cSRobert Watson * legitimate new connection attempt, the old INPCB gets removed and 934252ca428SRobert Watson * we can try again to find a listening socket. 935252ca428SRobert Watson * 936fa046d87SRobert Watson * At this point, due to earlier optimism, we may hold only an inpcb 937fa046d87SRobert Watson * lock, and not the inpcbinfo write lock. If so, we need to try to 938fa046d87SRobert Watson * acquire it, or if that fails, acquire a reference on the inpcb, 939fa046d87SRobert Watson * drop all locks, acquire a global write lock, and then re-acquire 940fa046d87SRobert Watson * the inpcb lock. We may at that point discover that another thread 941fa046d87SRobert Watson * has tried to free the inpcb, in which case we need to loop back 942fa046d87SRobert Watson * and try to find a new inpcb to deliver to. 943fa046d87SRobert Watson * 944fa046d87SRobert Watson * XXXRW: It may be time to rethink timewait locking. 945340c35deSJonathan Lemon */ 946883e9bc4SRobert Watson relocked: 947ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 948fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 949ff9b006dSJulien Charbon if (INP_INFO_TRY_RLOCK(&V_tcbinfo) == 0) { 950252ca428SRobert Watson in_pcbref(inp); 951252ca428SRobert Watson INP_WUNLOCK(inp); 952ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 953ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 954252ca428SRobert Watson INP_WLOCK(inp); 955fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) { 956252ca428SRobert Watson inp = NULL; 957252ca428SRobert Watson goto findpcb; 958252ca428SRobert Watson } 959f681a5fdSRobert Watson } else 960ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 961252ca428SRobert Watson } 962ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 963252ca428SRobert Watson 964340c35deSJonathan Lemon if (thflags & TH_SYN) 965f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 9668d573cc1SAndre Oppermann /* 9678d573cc1SAndre Oppermann * NB: tcp_twcheck unlocks the INP and frees the mbuf. 9688d573cc1SAndre Oppermann */ 9692104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 970340c35deSJonathan Lemon goto findpcb; 971ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 9728f5a8818SKevin Lo return (IPPROTO_DONE); 973340c35deSJonathan Lemon } 974794235b7SAndre Oppermann /* 975794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 976794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 977794235b7SAndre Oppermann * segment and send an appropriate response. 978794235b7SAndre Oppermann */ 979df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 980a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 981a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 982a57815efSBosko Milekic goto dropwithreset; 98309f81a46SBosko Milekic } 984df8bae1dSRodney W. Grimes 98509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 98609fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 98709fe6320SNavdeep Parhar tcp_offload_input(tp, m); 98809fe6320SNavdeep Parhar m = NULL; /* consumed by the TOE driver */ 98909fe6320SNavdeep Parhar goto dropunlock; 99009fe6320SNavdeep Parhar } 99109fe6320SNavdeep Parhar #endif 99209fe6320SNavdeep Parhar 993252ca428SRobert Watson /* 994252ca428SRobert Watson * We've identified a valid inpcb, but it could be that we need an 995fa046d87SRobert Watson * inpcbinfo write lock but don't hold it. In this case, attempt to 996fa046d87SRobert Watson * acquire using the same strategy as the TIMEWAIT case above. If we 997fa046d87SRobert Watson * relock, we have to jump back to 'relocked' as the connection might 998fa046d87SRobert Watson * now be in TIMEWAIT. 999252ca428SRobert Watson */ 1000fa046d87SRobert Watson #ifdef INVARIANTS 1001a7c7f2a7SJohn Baldwin if ((thflags & (TH_FIN | TH_RST)) != 0) 1002ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1003fa046d87SRobert Watson #endif 1004a7c7f2a7SJohn Baldwin if (!((tp->t_state == TCPS_ESTABLISHED && (thflags & TH_SYN) == 0) || 1005*281a0fd4SPatrick Kelsey (tp->t_state == TCPS_LISTEN && (thflags & TH_SYN) && 1006*281a0fd4SPatrick Kelsey !(tp->t_flags & TF_FASTOPEN)))) { 1007fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 1008ff9b006dSJulien Charbon if (INP_INFO_TRY_RLOCK(&V_tcbinfo) == 0) { 1009252ca428SRobert Watson in_pcbref(inp); 1010252ca428SRobert Watson INP_WUNLOCK(inp); 1011ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 1012ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 1013252ca428SRobert Watson INP_WLOCK(inp); 1014fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) { 1015252ca428SRobert Watson inp = NULL; 1016252ca428SRobert Watson goto findpcb; 1017252ca428SRobert Watson } 1018883e9bc4SRobert Watson goto relocked; 1019f681a5fdSRobert Watson } else 1020ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 1021252ca428SRobert Watson } 1022ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1023252ca428SRobert Watson } 1024252ca428SRobert Watson 1025c488362eSRobert Watson #ifdef MAC 10268501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 102730d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 1028302ce8d6SAndre Oppermann goto dropunlock; 1029c488362eSRobert Watson #endif 1030a557af22SRobert Watson so = inp->inp_socket; 1031302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 1032610ee2f9SDavid Greenman #ifdef TCPDEBUG 1033df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 1034df8bae1dSRodney W. Grimes ostate = tp->t_state; 103510fe523eSMaxim Konovalov #ifdef INET6 10366f697424SBjoern A. Zeeb if (isipv6) { 1037540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 1038d30d90dcSMaxim Konovalov } else 10396f697424SBjoern A. Zeeb #endif 1040540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 1041fb59c426SYoshinobu Inoue tcp_savetcp = *th; 1042df8bae1dSRodney W. Grimes } 1043b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */ 1044db33b3e6SAndre Oppermann /* 1045db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 1046db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 1047a7c7f2a7SJohn Baldwin * attempt or the completion of a previous one. 1048db33b3e6SAndre Oppermann */ 1049540e8b7eSJeffrey Hsu if (so->so_options & SO_ACCEPTCONN) { 1050540e8b7eSJeffrey Hsu struct in_conninfo inc; 1051540e8b7eSJeffrey Hsu 10527824d002SAndre Oppermann KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but " 10537824d002SAndre Oppermann "tp not listening", __func__)); 10548bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 1055302ce8d6SAndre Oppermann #ifdef INET6 1056be2ac88cSJonathan Lemon if (isipv6) { 1057dcdb4371SBjoern A. Zeeb inc.inc_flags |= INC_ISIPV6; 1058be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 1059be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 1060302ce8d6SAndre Oppermann } else 1061302ce8d6SAndre Oppermann #endif 1062302ce8d6SAndre Oppermann { 1063be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 1064be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 1065be2ac88cSJonathan Lemon } 1066be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 1067be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 10687973fba3SJulian Elischer inc.inc_fibnum = so->so_fibnum; 1069be2ac88cSJonathan Lemon 1070fb59c426SYoshinobu Inoue /* 10718d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 10728d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 10738d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 1074fb59c426SYoshinobu Inoue */ 1075be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 1076a7c7f2a7SJohn Baldwin 1077ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1078bf6d304aSAndre Oppermann /* 1079bf6d304aSAndre Oppermann * Parse the TCP options here because 1080bf6d304aSAndre Oppermann * syncookies need access to the reflected 1081bf6d304aSAndre Oppermann * timestamp. 1082bf6d304aSAndre Oppermann */ 1083bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 10849fa198beSAndre Oppermann /* 10859fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 10869fa198beSAndre Oppermann * inp and tcpinfo locks. 10879fa198beSAndre Oppermann */ 1088bf6d304aSAndre Oppermann if (!syncache_expand(&inc, &to, th, &so, m)) { 10894195b4afSPaul Traina /* 1090e207f800SAndre Oppermann * No syncache entry or ACK was not 1091be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 10928d573cc1SAndre Oppermann * NB: syncache did its own logging 10938d573cc1SAndre Oppermann * of the failure cause. 10944195b4afSPaul Traina */ 1095be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 1096be2ac88cSJonathan Lemon goto dropwithreset; 1097be2ac88cSJonathan Lemon } 1098*281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 1099*281a0fd4SPatrick Kelsey new_tfo_socket: 1100*281a0fd4SPatrick Kelsey #endif 1101f76fcf6dSJeffrey Hsu if (so == NULL) { 1102be2ac88cSJonathan Lemon /* 1103e207f800SAndre Oppermann * We completed the 3-way handshake 1104e207f800SAndre Oppermann * but could not allocate a socket 1105e207f800SAndre Oppermann * either due to memory shortage, 1106e207f800SAndre Oppermann * listen queue length limits or 1107a160e630SAndre Oppermann * global socket limits. Send RST 1108a160e630SAndre Oppermann * or wait and have the remote end 1109a160e630SAndre Oppermann * retransmit the ACK for another 1110a160e630SAndre Oppermann * try. 1111be2ac88cSJonathan Lemon */ 1112a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1113a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11148d573cc1SAndre Oppermann "Socket allocation failed due to " 11158d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 1116ac957cd2SJulian Elischer s, __func__, 1117ac957cd2SJulian Elischer V_tcp_sc_rst_sock_fail ? 1118ac957cd2SJulian Elischer "sending RST" : "try again"); 1119603724d3SBjoern A. Zeeb if (V_tcp_sc_rst_sock_fail) { 1120e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 1121e207f800SAndre Oppermann goto dropwithreset; 1122a160e630SAndre Oppermann } else 1123a160e630SAndre Oppermann goto dropunlock; 1124f76fcf6dSJeffrey Hsu } 1125be2ac88cSJonathan Lemon /* 1126be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 11278d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 11288d573cc1SAndre Oppermann * created socket and update the tp variable. 1129be2ac88cSJonathan Lemon */ 11308501a69cSRobert Watson INP_WUNLOCK(inp); /* listen socket */ 1131be2ac88cSJonathan Lemon inp = sotoinpcb(so); 1132ff9b006dSJulien Charbon /* 1133ff9b006dSJulien Charbon * New connection inpcb is already locked by 1134ff9b006dSJulien Charbon * syncache_expand(). 1135ff9b006dSJulien Charbon */ 1136ff9b006dSJulien Charbon INP_WLOCK_ASSERT(inp); 1137be2ac88cSJonathan Lemon tp = intotcpcb(inp); 11388d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 11398d573cc1SAndre Oppermann ("%s: ", __func__)); 11402903309aSAttilio Rao #ifdef TCP_SIGNATURE 11412903309aSAttilio Rao if (sig_checked == 0) { 11422903309aSAttilio Rao tcp_dooptions(&to, optp, optlen, 11432903309aSAttilio Rao (thflags & TH_SYN) ? TO_SYN : 0); 11442903309aSAttilio Rao if (!tcp_signature_verify_input(m, off0, tlen, 11452903309aSAttilio Rao optlen, &to, th, tp->t_flags)) { 11462903309aSAttilio Rao 11472903309aSAttilio Rao /* 11482903309aSAttilio Rao * In SYN_SENT state if it receives an 11492903309aSAttilio Rao * RST, it is allowed for further 11502903309aSAttilio Rao * processing. 11512903309aSAttilio Rao */ 11522903309aSAttilio Rao if ((thflags & TH_RST) == 0 || 11532903309aSAttilio Rao (tp->t_state == TCPS_SYN_SENT) == 0) 11542903309aSAttilio Rao goto dropunlock; 11552903309aSAttilio Rao } 11562903309aSAttilio Rao sig_checked = 1; 11572903309aSAttilio Rao } 11582903309aSAttilio Rao #endif 11592903309aSAttilio Rao 1160be2ac88cSJonathan Lemon /* 1161302ce8d6SAndre Oppermann * Process the segment and the data it 1162302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 1163302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 1164302ce8d6SAndre Oppermann */ 116555bceb1eSRandall Stewart tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 1166252ca428SRobert Watson iptos, ti_locked); 1167603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 11688f5a8818SKevin Lo return (IPPROTO_DONE); 1169be2ac88cSJonathan Lemon } 1170a160e630SAndre Oppermann /* 11718d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 11728d573cc1SAndre Oppermann * 1173a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 1174a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 1175a160e630SAndre Oppermann * retransmits. 117619bc77c5SAndre Oppermann * 117719bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 117819bc77c5SAndre Oppermann * causes. 1179a160e630SAndre Oppermann */ 1180a160e630SAndre Oppermann if (thflags & TH_RST) { 118119bc77c5SAndre Oppermann syncache_chkrst(&inc, th); 1182a160e630SAndre Oppermann goto dropunlock; 1183a160e630SAndre Oppermann } 1184a160e630SAndre Oppermann /* 1185a160e630SAndre Oppermann * We can't do anything without SYN. 1186a160e630SAndre Oppermann */ 1187a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 1188a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1189a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1190773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 11918d573cc1SAndre Oppermann s, __func__); 119278b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1193a160e630SAndre Oppermann goto dropunlock; 1194a160e630SAndre Oppermann } 1195a160e630SAndre Oppermann /* 1196a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 1197a160e630SAndre Oppermann */ 1198fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1199a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1200a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12018d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 12028d573cc1SAndre Oppermann s, __func__); 1203a160e630SAndre Oppermann syncache_badack(&inc); /* XXX: Not needed! */ 120478b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1205a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1206a57815efSBosko Milekic goto dropwithreset; 12074195b4afSPaul Traina } 1208a160e630SAndre Oppermann /* 1209a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 1210a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 1211a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 1212a160e630SAndre Oppermann * TCP/IP stack. 1213a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 1214a160e630SAndre Oppermann * and is constantly refining its stack detection 1215a160e630SAndre Oppermann * strategies. 12168d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 1217a160e630SAndre Oppermann * and was used by RFC1644. 1218a160e630SAndre Oppermann */ 1219603724d3SBjoern A. Zeeb if ((thflags & TH_FIN) && V_drop_synfin) { 1220a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1221a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1222773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 12238d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 122478b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1225302ce8d6SAndre Oppermann goto dropunlock; 1226ebb0cbeaSPaul Traina } 1227be2ac88cSJonathan Lemon /* 1228be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 1229a160e630SAndre Oppermann * 1230a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 1231a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 1232a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 1233be2ac88cSJonathan Lemon */ 1234a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 1235a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 1236a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 1237a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 123833841545SHajimu UMEMOTO #ifdef INET6 123933841545SHajimu UMEMOTO /* 124033841545SHajimu UMEMOTO * If deprecated address is forbidden, 124133841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 124233841545SHajimu UMEMOTO * address to prevent any new inbound connection from 124333841545SHajimu UMEMOTO * getting established. 124433841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 124533841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 124633841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 124733841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 124833841545SHajimu UMEMOTO * for the exchange. 124933841545SHajimu UMEMOTO * 125033841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 125133841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 125233841545SHajimu UMEMOTO * SYN in this case. 125333841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 125433841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 125533841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 125633841545SHajimu UMEMOTO * used" 125733841545SHajimu UMEMOTO * 2. use of it with new communication: 125833841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 125933841545SHajimu UMEMOTO * with sufficient scope is available" 126033841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 126133841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 126233841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 126333841545SHajimu UMEMOTO * 126433841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 126533841545SHajimu UMEMOTO * multiple description text for deprecated address 126633841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 126733841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 126833841545SHajimu UMEMOTO */ 1269603724d3SBjoern A. Zeeb if (isipv6 && !V_ip6_use_deprecated) { 127033841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 127133841545SHajimu UMEMOTO 12723e88eb90SAndrey V. Elsukov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */); 12738c0fec80SRobert Watson if (ia6 != NULL && 127433841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 12758c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 1276a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1277a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12788d573cc1SAndre Oppermann "Connection attempt to deprecated " 12798d573cc1SAndre Oppermann "IPv6 address rejected\n", 1280a160e630SAndre Oppermann s, __func__); 128133841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 128233841545SHajimu UMEMOTO goto dropwithreset; 128333841545SHajimu UMEMOTO } 128477d396fdSMaksim Yevmenkin if (ia6) 12858c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 128633841545SHajimu UMEMOTO } 1287b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 128865f28919SJesper Skriver /* 1289db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 12908d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 1291db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 12928d573cc1SAndre Oppermann * If it is from this socket it must be forged. 12938d573cc1SAndre Oppermann * Don't respond if the source or destination is a 12948d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 129593ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 129693ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 129793ec91baSCrist J. Clark * in_broadcast() to find them. 1298be2ac88cSJonathan Lemon */ 12998d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 13008d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 13018d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13028d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 1303773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 1304302ce8d6SAndre Oppermann goto dropunlock; 13058d573cc1SAndre Oppermann } 1306db33b3e6SAndre Oppermann #ifdef INET6 1307b287c6c7SBjoern A. Zeeb if (isipv6) { 1308db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1309a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 1310a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1311a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13128d573cc1SAndre Oppermann "Connection attempt to/from self " 1313773673c1SAndre Oppermann "ignored\n", s, __func__); 1314302ce8d6SAndre Oppermann goto dropunlock; 1315a160e630SAndre Oppermann } 1316be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1317a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 1318a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1319a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13208d573cc1SAndre Oppermann "Connection attempt from/to multicast " 1321773673c1SAndre Oppermann "address ignored\n", s, __func__); 1322302ce8d6SAndre Oppermann goto dropunlock; 1323a160e630SAndre Oppermann } 1324b287c6c7SBjoern A. Zeeb } 1325db33b3e6SAndre Oppermann #endif 1326b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 1327b287c6c7SBjoern A. Zeeb else 1328b287c6c7SBjoern A. Zeeb #endif 1329b287c6c7SBjoern A. Zeeb #ifdef INET 1330b287c6c7SBjoern A. Zeeb { 1331db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1332a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 1333a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1334a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13358d573cc1SAndre Oppermann "Connection attempt from/to self " 1336773673c1SAndre Oppermann "ignored\n", s, __func__); 1337302ce8d6SAndre Oppermann goto dropunlock; 1338a160e630SAndre Oppermann } 1339be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1340be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 13412ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 1342a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 1343a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1344a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13458d573cc1SAndre Oppermann "Connection attempt from/to broad- " 1346773673c1SAndre Oppermann "or multicast address ignored\n", 1347a160e630SAndre Oppermann s, __func__); 1348302ce8d6SAndre Oppermann goto dropunlock; 1349c068736aSJeffrey Hsu } 1350a160e630SAndre Oppermann } 1351b287c6c7SBjoern A. Zeeb #endif 1352be2ac88cSJonathan Lemon /* 1353db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 1354db33b3e6SAndre Oppermann * for syncache. 1355be2ac88cSJonathan Lemon */ 13563c653157SHartmut Brandt #ifdef TCPDEBUG 13573c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 13583c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 13593c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 13603c653157SHartmut Brandt #endif 13615d06879aSGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, mtod(m, const char *)); 1362f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 1363*281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 1364*281a0fd4SPatrick Kelsey if (syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL)) 1365*281a0fd4SPatrick Kelsey goto new_tfo_socket; 1366*281a0fd4SPatrick Kelsey #else 136709fe6320SNavdeep Parhar syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL); 1368*281a0fd4SPatrick Kelsey #endif 1369be2ac88cSJonathan Lemon /* 13704d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 1371a7c7f2a7SJohn Baldwin * Only the listen socket is unlocked by syncache_add(). 1372be2ac88cSJonathan Lemon */ 1373ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 1374ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1375a7c7f2a7SJohn Baldwin ti_locked = TI_UNLOCKED; 1376a7c7f2a7SJohn Baldwin } 1377603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 13788f5a8818SKevin Lo return (IPPROTO_DONE); 1379982c1675SAndre Oppermann } else if (tp->t_state == TCPS_LISTEN) { 1380982c1675SAndre Oppermann /* 1381982c1675SAndre Oppermann * When a listen socket is torn down the SO_ACCEPTCONN 1382982c1675SAndre Oppermann * flag is removed first while connections are drained 1383982c1675SAndre Oppermann * from the accept queue in a unlock/lock cycle of the 1384982c1675SAndre Oppermann * ACCEPT_LOCK, opening a race condition allowing a SYN 1385982c1675SAndre Oppermann * attempt go through unhandled. 1386982c1675SAndre Oppermann */ 1387982c1675SAndre Oppermann goto dropunlock; 1388f76fcf6dSJeffrey Hsu } 1389db33b3e6SAndre Oppermann 13902903309aSAttilio Rao #ifdef TCP_SIGNATURE 13912903309aSAttilio Rao if (sig_checked == 0) { 13922903309aSAttilio Rao tcp_dooptions(&to, optp, optlen, 13932903309aSAttilio Rao (thflags & TH_SYN) ? TO_SYN : 0); 13942903309aSAttilio Rao if (!tcp_signature_verify_input(m, off0, tlen, optlen, &to, 13952903309aSAttilio Rao th, tp->t_flags)) { 13962903309aSAttilio Rao 13972903309aSAttilio Rao /* 13982903309aSAttilio Rao * In SYN_SENT state if it receives an RST, it is 13992903309aSAttilio Rao * allowed for further processing. 14002903309aSAttilio Rao */ 14012903309aSAttilio Rao if ((thflags & TH_RST) == 0 || 14022903309aSAttilio Rao (tp->t_state == TCPS_SYN_SENT) == 0) 14032903309aSAttilio Rao goto dropunlock; 14042903309aSAttilio Rao } 14052903309aSAttilio Rao sig_checked = 1; 14062903309aSAttilio Rao } 14072903309aSAttilio Rao #endif 14082903309aSAttilio Rao 1409fa22ce15SAdrian Chadd TCP_PROBE5(receive, NULL, tp, mtod(m, const char *), tp, th); 141057f60867SMark Johnston 1411302ce8d6SAndre Oppermann /* 14128d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 14138d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 14148d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 1415302ce8d6SAndre Oppermann */ 141655bceb1eSRandall Stewart tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked); 1417603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 14188f5a8818SKevin Lo return (IPPROTO_DONE); 1419be2ac88cSJonathan Lemon 1420302ce8d6SAndre Oppermann dropwithreset: 1421fa22ce15SAdrian Chadd TCP_PROBE5(receive, NULL, tp, mtod(m, const char *), tp, th); 142257f60867SMark Johnston 1423ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 1424ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1425252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1426fa046d87SRobert Watson } 1427fa046d87SRobert Watson #ifdef INVARIANTS 1428fa046d87SRobert Watson else { 1429fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset " 1430fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1431fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1432fa046d87SRobert Watson } 1433fa046d87SRobert Watson #endif 1434014ea782SRobert Watson 1435014ea782SRobert Watson if (inp != NULL) { 1436302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 1437014ea782SRobert Watson INP_WUNLOCK(inp); 1438dd8ac7f9SRobert Watson } else 1439014ea782SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1440302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 1441014ea782SRobert Watson goto drop; 1442014ea782SRobert Watson 1443302ce8d6SAndre Oppermann dropunlock: 144457f60867SMark Johnston if (m != NULL) 1445fa22ce15SAdrian Chadd TCP_PROBE5(receive, NULL, tp, mtod(m, const char *), tp, th); 144657f60867SMark Johnston 1447ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 1448ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1449252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1450fa046d87SRobert Watson } 1451fa046d87SRobert Watson #ifdef INVARIANTS 1452fa046d87SRobert Watson else { 1453fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock " 1454fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1455fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1456fa046d87SRobert Watson } 1457fa046d87SRobert Watson #endif 1458252ca428SRobert Watson 14599fa198beSAndre Oppermann if (inp != NULL) 14608501a69cSRobert Watson INP_WUNLOCK(inp); 1461014ea782SRobert Watson 1462302ce8d6SAndre Oppermann drop: 1463603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1464a160e630SAndre Oppermann if (s != NULL) 1465a160e630SAndre Oppermann free(s, M_TCPLOG); 1466302ce8d6SAndre Oppermann if (m != NULL) 1467302ce8d6SAndre Oppermann m_freem(m); 14688f5a8818SKevin Lo return (IPPROTO_DONE); 1469302ce8d6SAndre Oppermann } 1470302ce8d6SAndre Oppermann 147155bceb1eSRandall Stewart void 1472302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 1473252ca428SRobert Watson struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos, 1474252ca428SRobert Watson int ti_locked) 1475302ce8d6SAndre Oppermann { 1476021eaf79SHiren Panchasara int thflags, acked, ourfinisacked, needoutput = 0, sack_changed; 1477302ce8d6SAndre Oppermann int rstreason, todrop, win; 1478302ce8d6SAndre Oppermann u_long tiwin; 147907dacf03SAndre Oppermann char *s; 148007dacf03SAndre Oppermann struct in_conninfo *inc; 1481c11a15bfSGleb Smirnoff struct mbuf *mfree; 1482302ce8d6SAndre Oppermann struct tcpopt to; 1483*281a0fd4SPatrick Kelsey int tfo_syn; 1484302ce8d6SAndre Oppermann 148514739780SMaxim Konovalov #ifdef TCPDEBUG 148614739780SMaxim Konovalov /* 148714739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 148814739780SMaxim Konovalov * now IPv6. 148914739780SMaxim Konovalov */ 149014739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 149114739780SMaxim Konovalov struct tcphdr tcp_savetcp; 149214739780SMaxim Konovalov short ostate = 0; 149314739780SMaxim Konovalov #endif 1494302ce8d6SAndre Oppermann thflags = th->th_flags; 149507dacf03SAndre Oppermann inc = &tp->t_inpcb->inp_inc; 1496d64a46eaSLawrence Stewart tp->sackhint.last_sack_ack = 0; 1497021eaf79SHiren Panchasara sack_changed = 0; 1498302ce8d6SAndre Oppermann 1499252ca428SRobert Watson /* 1500252ca428SRobert Watson * If this is either a state-changing packet or current state isn't 1501252ca428SRobert Watson * established, we require a write lock on tcbinfo. Otherwise, we 15020989f56cSRobert Watson * allow the tcbinfo to be in either alocked or unlocked, as the 15030989f56cSRobert Watson * caller may have unnecessarily acquired a write lock due to a race. 1504252ca428SRobert Watson */ 1505252ca428SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 1506252ca428SRobert Watson tp->t_state != TCPS_ESTABLISHED) { 1507ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, ("%s ti_locked %d for " 1508252ca428SRobert Watson "SYN/FIN/RST/!EST", __func__, ti_locked)); 1509ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1510252ca428SRobert Watson } else { 1511252ca428SRobert Watson #ifdef INVARIANTS 1512ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 1513ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1514fa046d87SRobert Watson else { 1515fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST " 1516fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1517fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1518fa046d87SRobert Watson } 1519252ca428SRobert Watson #endif 1520252ca428SRobert Watson } 15218501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1522679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1523679d9708SAndre Oppermann __func__)); 1524679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1525679d9708SAndre Oppermann __func__)); 1526df8bae1dSRodney W. Grimes 152786a996e6SHiren Panchasara #ifdef TCPPCAP 152886a996e6SHiren Panchasara /* Save segment, if requested. */ 152986a996e6SHiren Panchasara tcp_pcap_add(th, m, &(tp->t_inpkts)); 153086a996e6SHiren Panchasara #endif 153186a996e6SHiren Panchasara 1532df8bae1dSRodney W. Grimes /* 1533df8bae1dSRodney W. Grimes * Segment received on connection. 1534df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1535e8949f74SAndre Oppermann * XXX: This should be done after segment 1536e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 1537df8bae1dSRodney W. Grimes */ 15389b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 15397ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 15409077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 1541df8bae1dSRodney W. Grimes 1542df8bae1dSRodney W. Grimes /* 1543c52102ddSHiren Panchasara * Scale up the window into a 32-bit value. 1544e8949f74SAndre Oppermann * For the SYN_SENT state the scale is zero. 1545464fcfbcSAndre Oppermann */ 1546464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 1547464fcfbcSAndre Oppermann 1548464fcfbcSAndre Oppermann /* 1549f2512ba1SRui Paulo * TCP ECN processing. 1550f2512ba1SRui Paulo */ 1551f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) { 15529c251892SRui Paulo if (thflags & TH_CWR) 15539c251892SRui Paulo tp->t_flags &= ~TF_ECN_SND_ECE; 1554f2512ba1SRui Paulo switch (iptos & IPTOS_ECN_MASK) { 1555f2512ba1SRui Paulo case IPTOS_ECN_CE: 1556f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_ECE; 155778b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ce); 1558f2512ba1SRui Paulo break; 1559f2512ba1SRui Paulo case IPTOS_ECN_ECT0: 156078b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect0); 1561f2512ba1SRui Paulo break; 1562f2512ba1SRui Paulo case IPTOS_ECN_ECT1: 156378b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect1); 1564f2512ba1SRui Paulo break; 1565f2512ba1SRui Paulo } 156664807b30SHiren Panchasara 156764807b30SHiren Panchasara /* Process a packet differently from RFC3168. */ 156864807b30SHiren Panchasara cc_ecnpkt_handler(tp, th, iptos); 156964807b30SHiren Panchasara 1570dbc42409SLawrence Stewart /* Congestion experienced. */ 1571dbc42409SLawrence Stewart if (thflags & TH_ECE) { 1572dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_ECN); 1573f2512ba1SRui Paulo } 1574f2512ba1SRui Paulo } 1575f2512ba1SRui Paulo 1576f2512ba1SRui Paulo /* 1577f72167f4SAndre Oppermann * Parse options on any incoming segment. 1578f72167f4SAndre Oppermann */ 1579302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1580302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1581302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1582f72167f4SAndre Oppermann 1583f72167f4SAndre Oppermann /* 1584f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1585bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1586bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1587bf6d304aSAndre Oppermann * was established. 1588f72167f4SAndre Oppermann */ 1589bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1590e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 1591d8951c8aSBjoern A. Zeeb if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) 1592f72167f4SAndre Oppermann to.to_tsecr = 0; 1593bf6d304aSAndre Oppermann } 159407dacf03SAndre Oppermann /* 159507dacf03SAndre Oppermann * If timestamps were negotiated during SYN/ACK they should 159607dacf03SAndre Oppermann * appear on every segment during this session and vice versa. 159707dacf03SAndre Oppermann */ 159807dacf03SAndre Oppermann if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) { 159907dacf03SAndre Oppermann if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 160007dacf03SAndre Oppermann log(LOG_DEBUG, "%s; %s: Timestamp missing, " 160107dacf03SAndre Oppermann "no action\n", s, __func__); 160207dacf03SAndre Oppermann free(s, M_TCPLOG); 160307dacf03SAndre Oppermann } 160407dacf03SAndre Oppermann } 160507dacf03SAndre Oppermann if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) { 160607dacf03SAndre Oppermann if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 160707dacf03SAndre Oppermann log(LOG_DEBUG, "%s; %s: Timestamp not expected, " 160807dacf03SAndre Oppermann "no action\n", s, __func__); 160907dacf03SAndre Oppermann free(s, M_TCPLOG); 161007dacf03SAndre Oppermann } 161107dacf03SAndre Oppermann } 1612f72167f4SAndre Oppermann 1613f72167f4SAndre Oppermann /* 161497d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 161597d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 16165396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 16175396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 161897d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1619df8bae1dSRodney W. Grimes */ 16200a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1621464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 1622464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 1623be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 162402a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 1625be2ac88cSJonathan Lemon } 16265396d0f8SAndre Oppermann /* 16275396d0f8SAndre Oppermann * Initial send window. It will be updated with 16285396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 16295396d0f8SAndre Oppermann */ 16305396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 1631be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1632be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1633be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1634d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1635be2ac88cSJonathan Lemon } 1636be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1637be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 16383529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 16393529149eSAndre Oppermann (to.to_flags & TOF_SACKPERM) == 0) 16403529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 16416d90faf3SPaul Saab } 16426d90faf3SPaul Saab 1643df8bae1dSRodney W. Grimes /* 1644df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1645df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1646df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1647df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1648df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1649df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1650df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1651df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1652df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1653df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1654df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1655df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1656a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1657c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 1658a0292f23SGarrett Wollman * be TH_NEEDSYN. 1659df8bae1dSRodney W. Grimes */ 1660df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1661c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 1662fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1663c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 1664c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 1665a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 16664741bfcbSPatrick Kelsey LIST_EMPTY(&tp->t_segq) && 16674741bfcbSPatrick Kelsey ((to.to_flags & TOF_TS) == 0 || 1668c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1669df8bae1dSRodney W. Grimes 1670df8bae1dSRodney W. Grimes /* 1671df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1672df8bae1dSRodney W. Grimes * record the timestamp. 1673a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1674a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1675df8bae1dSRodney W. Grimes */ 1676be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1677fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1678d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1679a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1680df8bae1dSRodney W. Grimes } 1681df8bae1dSRodney W. Grimes 1682fb59c426SYoshinobu Inoue if (tlen == 0) { 1683fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1684fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1685dbc42409SLawrence Stewart !IN_RECOVERY(tp->t_flags) && 1686fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1687dbc42409SLawrence Stewart TAILQ_EMPTY(&tp->snd_holes)) { 1688df8bae1dSRodney W. Grimes /* 1689e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1690df8bae1dSRodney W. Grimes */ 1691ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 1692ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1693252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1694252ca428SRobert Watson 169578b50714SRobert Watson TCPSTAT_INC(tcps_predack); 1696252ca428SRobert Watson 16979b8b58e0SJonathan Lemon /* 1698e8949f74SAndre Oppermann * "bad retransmit" recovery. 16999b8b58e0SJonathan Lemon */ 17009b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 1701672dc4aeSJohn Baldwin tp->t_flags & TF_PREVVALID && 17026dfb8b31SJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) { 1703dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 17049b8b58e0SJonathan Lemon } 1705fa55172bSMatthew Dillon 1706fa55172bSMatthew Dillon /* 1707fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1708fa55172bSMatthew Dillon * 1709fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1710fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1711fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1712fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1713fa55172bSMatthew Dillon */ 1714fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1715fa55172bSMatthew Dillon to.to_tsecr) { 1716d8951c8aSBjoern A. Zeeb u_int t; 1717d8951c8aSBjoern A. Zeeb 1718d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 1719d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 1720d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 1721a0292f23SGarrett Wollman tcp_xmit_timer(tp, 1722d8951c8aSBjoern A. Zeeb TCP_TS_TO_TICKS(t) + 1); 1723fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1724fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1725eaf80179SAndre Oppermann if (!tp->t_rttlow || 1726eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1727eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1728c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1729c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1730fa55172bSMatthew Dillon } 1731dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 173239bc9de5SLawrence Stewart 173339bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 173439bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 173539bc9de5SLawrence Stewart 173678b50714SRobert Watson TCPSTAT_INC(tcps_rcvackpack); 173778b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 1738df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 17399d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 17409d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 17419d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 1742dbc42409SLawrence Stewart 1743dbc42409SLawrence Stewart /* 1744dbc42409SLawrence Stewart * Let the congestion control algorithm update 1745dbc42409SLawrence Stewart * congestion control related information. This 1746dbc42409SLawrence Stewart * typically means increasing the congestion 1747dbc42409SLawrence Stewart * window. 1748dbc42409SLawrence Stewart */ 1749dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_ACK); 1750dbc42409SLawrence Stewart 17519d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 17521645d090SJeff Roberson /* 1753e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 17541645d090SJeff Roberson * to th_ack. 17551645d090SJeff Roberson */ 1756cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1757b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1758df8bae1dSRodney W. Grimes m_freem(m); 1759df8bae1dSRodney W. Grimes 1760df8bae1dSRodney W. Grimes /* 1761df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1762df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1763df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1764df8bae1dSRodney W. Grimes * If process is waiting for space, 1765df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1766df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1767df8bae1dSRodney W. Grimes * decide between more output or persist. 1768e8949f74SAndre Oppermann */ 17693c653157SHartmut Brandt #ifdef TCPDEBUG 17703c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 17713c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 17723c653157SHartmut Brandt (void *)tcp_saveipgen, 17733c653157SHartmut Brandt &tcp_savetcp, 0); 17743c653157SHartmut Brandt #endif 17755d06879aSGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, 17765d06879aSGeorge V. Neville-Neil mtod(m, const char *)); 1777df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1778b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1779b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1780b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1781b8152ba7SAndre Oppermann tp->t_rxtcur); 1782df8bae1dSRodney W. Grimes sowwakeup(so); 1783cfa6009eSGleb Smirnoff if (sbavail(&so->so_snd)) 178455bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 1785a14c749fSJonathan Lemon goto check_delack; 1786df8bae1dSRodney W. Grimes } 1787fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1788fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 17896741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 17906741ecf5SAndre Oppermann 1791df8bae1dSRodney W. Grimes /* 1792252ca428SRobert Watson * This is a pure, in-sequence data packet with 1793252ca428SRobert Watson * nothing on the reassembly queue and we have enough 1794252ca428SRobert Watson * buffer space to take it. 1795df8bae1dSRodney W. Grimes */ 1796ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 1797ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1798252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1799252ca428SRobert Watson 18006d90faf3SPaul Saab /* Clean receiver SACK report if present */ 18013529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 18026d90faf3SPaul Saab tcp_clean_sackreport(tp); 180378b50714SRobert Watson TCPSTAT_INC(tcps_preddat); 1804fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 18051645d090SJeff Roberson /* 18061645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 18071645d090SJeff Roberson * th_seq. 18081645d090SJeff Roberson */ 180960ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 18101645d090SJeff Roberson /* 18111645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 18121645d090SJeff Roberson * rcv_nxt. 18131645d090SJeff Roberson */ 18141645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 181578b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 181678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 18173c653157SHartmut Brandt #ifdef TCPDEBUG 18183c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 18193c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 18203c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 18213c653157SHartmut Brandt #endif 18225d06879aSGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, mtod(m, const char *)); 18235d06879aSGeorge V. Neville-Neil 18246741ecf5SAndre Oppermann /* 18256741ecf5SAndre Oppermann * Automatic sizing of receive socket buffer. Often the send 18266741ecf5SAndre Oppermann * buffer size is not optimally adjusted to the actual network 18276741ecf5SAndre Oppermann * conditions at hand (delay bandwidth product). Setting the 18286741ecf5SAndre Oppermann * buffer size too small limits throughput on links with high 18296741ecf5SAndre Oppermann * bandwidth and high delay (eg. trans-continental/oceanic links). 18306741ecf5SAndre Oppermann * 18316741ecf5SAndre Oppermann * On the receive side the socket buffer memory is only rarely 18326741ecf5SAndre Oppermann * used to any significant extent. This allows us to be much 18336741ecf5SAndre Oppermann * more aggressive in scaling the receive socket buffer. For 18346741ecf5SAndre Oppermann * the case that the buffer space is actually used to a large 18356741ecf5SAndre Oppermann * extent and we run out of kernel memory we can simply drop 18366741ecf5SAndre Oppermann * the new segments; TCP on the sender will just retransmit it 18376741ecf5SAndre Oppermann * later. Setting the buffer size too big may only consume too 18386741ecf5SAndre Oppermann * much kernel memory if the application doesn't read() from 18396741ecf5SAndre Oppermann * the socket or packet loss or reordering makes use of the 18406741ecf5SAndre Oppermann * reassembly queue. 18416741ecf5SAndre Oppermann * 18426741ecf5SAndre Oppermann * The criteria to step up the receive buffer one notch are: 1843f7469d3eSHiren Panchasara * 1. Application has not set receive buffer size with 1844f7469d3eSHiren Panchasara * SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE. 1845f7469d3eSHiren Panchasara * 2. the number of bytes received during the time it takes 18466741ecf5SAndre Oppermann * one timestamp to be reflected back to us (the RTT); 1847f7469d3eSHiren Panchasara * 3. received bytes per RTT is within seven eighth of the 18486741ecf5SAndre Oppermann * current socket buffer size; 1849f7469d3eSHiren Panchasara * 4. receive buffer size has not hit maximal automatic size; 18506741ecf5SAndre Oppermann * 18516741ecf5SAndre Oppermann * This algorithm does one step per RTT at most and only if 18526741ecf5SAndre Oppermann * we receive a bulk stream w/o packet losses or reorderings. 18536741ecf5SAndre Oppermann * Shrinking the buffer during idle times is not necessary as 18546741ecf5SAndre Oppermann * it doesn't consume any memory when idle. 18556741ecf5SAndre Oppermann * 18566741ecf5SAndre Oppermann * TODO: Only step up if the application is actually serving 18576741ecf5SAndre Oppermann * the buffer to better manage the socket buffer resources. 1858df8bae1dSRodney W. Grimes */ 1859603724d3SBjoern A. Zeeb if (V_tcp_do_autorcvbuf && 1860d57724fdSPatrick Kelsey (to.to_flags & TOF_TS) && 18616741ecf5SAndre Oppermann to.to_tsecr && 18626741ecf5SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 18638502ec25SAndre Oppermann if (TSTMP_GT(to.to_tsecr, tp->rfbuf_ts) && 18646741ecf5SAndre Oppermann to.to_tsecr - tp->rfbuf_ts < hz) { 18656741ecf5SAndre Oppermann if (tp->rfbuf_cnt > 18666741ecf5SAndre Oppermann (so->so_rcv.sb_hiwat / 8 * 7) && 18676741ecf5SAndre Oppermann so->so_rcv.sb_hiwat < 1868603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max) { 18696741ecf5SAndre Oppermann newsize = 18706741ecf5SAndre Oppermann min(so->so_rcv.sb_hiwat + 1871603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_inc, 1872603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max); 18736741ecf5SAndre Oppermann } 18746741ecf5SAndre Oppermann /* Start over with next RTT. */ 18756741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 18766741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 18776741ecf5SAndre Oppermann } else 18786741ecf5SAndre Oppermann tp->rfbuf_cnt += tlen; /* add up */ 18796741ecf5SAndre Oppermann } 18806741ecf5SAndre Oppermann 18816741ecf5SAndre Oppermann /* Add data to socket buffer. */ 18821e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1883c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1884c1c36a2cSMike Silbersack m_freem(m); 1885c1c36a2cSMike Silbersack } else { 18866741ecf5SAndre Oppermann /* 18876741ecf5SAndre Oppermann * Set new socket buffer size. 18886741ecf5SAndre Oppermann * Give up when limit is reached. 18896741ecf5SAndre Oppermann */ 18906741ecf5SAndre Oppermann if (newsize) 18916741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 18926c8286e4SRobert Watson newsize, so, NULL)) 18936741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1894fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 1895651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 1896c1c36a2cSMike Silbersack } 1897e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 18981e4d7da7SRobert Watson sorwakeup_locked(so); 1899c1e5a6e5SAndre Oppermann if (DELAY_ACK(tp, tlen)) { 19003bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1901f498eeeeSDavid Greenman } else { 1902e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 190355bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 1904e612a582SDavid Greenman } 1905a14c749fSJonathan Lemon goto check_delack; 1906df8bae1dSRodney W. Grimes } 1907df8bae1dSRodney W. Grimes } 1908df8bae1dSRodney W. Grimes 1909df8bae1dSRodney W. Grimes /* 1910df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1911df8bae1dSRodney W. Grimes * and then do TCP input processing. 1912df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1913df8bae1dSRodney W. Grimes * but not less than advertised window. 1914df8bae1dSRodney W. Grimes */ 1915df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1916df8bae1dSRodney W. Grimes if (win < 0) 1917df8bae1dSRodney W. Grimes win = 0; 191866e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1919df8bae1dSRodney W. Grimes 19206741ecf5SAndre Oppermann /* Reset receive buffer auto scaling when not in bulk receive mode. */ 19216741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 19226741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 19236741ecf5SAndre Oppermann 1924df8bae1dSRodney W. Grimes switch (tp->t_state) { 1925df8bae1dSRodney W. Grimes 1926df8bae1dSRodney W. Grimes /* 1927764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1928764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1929764d8cefSBill Fenner */ 1930764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1931fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1932fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1933a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1934a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1935a57815efSBosko Milekic goto dropwithreset; 1936a57815efSBosko Milekic } 1937*281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 1938*281a0fd4SPatrick Kelsey if (tp->t_flags & TF_FASTOPEN) { 1939*281a0fd4SPatrick Kelsey /* 1940*281a0fd4SPatrick Kelsey * When a TFO connection is in SYN_RECEIVED, the 1941*281a0fd4SPatrick Kelsey * only valid packets are the initial SYN, a 1942*281a0fd4SPatrick Kelsey * retransmit/copy of the initial SYN (possibly with 1943*281a0fd4SPatrick Kelsey * a subset of the original data), a valid ACK, a 1944*281a0fd4SPatrick Kelsey * FIN, or a RST. 1945*281a0fd4SPatrick Kelsey */ 1946*281a0fd4SPatrick Kelsey if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) { 1947*281a0fd4SPatrick Kelsey rstreason = BANDLIM_RST_OPENPORT; 1948*281a0fd4SPatrick Kelsey goto dropwithreset; 1949*281a0fd4SPatrick Kelsey } else if (thflags & TH_SYN) { 1950*281a0fd4SPatrick Kelsey /* non-initial SYN is ignored */ 1951*281a0fd4SPatrick Kelsey if ((tcp_timer_active(tp, TT_DELACK) || 1952*281a0fd4SPatrick Kelsey tcp_timer_active(tp, TT_REXMT))) 1953*281a0fd4SPatrick Kelsey goto drop; 1954*281a0fd4SPatrick Kelsey } else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) { 1955*281a0fd4SPatrick Kelsey goto drop; 1956*281a0fd4SPatrick Kelsey } 1957*281a0fd4SPatrick Kelsey } 1958*281a0fd4SPatrick Kelsey #endif 1959764d8cefSBill Fenner break; 1960764d8cefSBill Fenner 1961764d8cefSBill Fenner /* 1962df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1963df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1964df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1965df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1966df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1967df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1968df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1969f2512ba1SRui Paulo * if seg contains an ECE and ECN support is enabled, the stream 1970f2512ba1SRui Paulo * is ECN capable. 1971df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1972df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1973df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1974df8bae1dSRodney W. Grimes */ 1975df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1976fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1977fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1978fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1979a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1980a0292f23SGarrett Wollman goto dropwithreset; 1981a0292f23SGarrett Wollman } 198257f60867SMark Johnston if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) { 1983d9fae5abSAndriy Gapon TCP_PROBE5(connect__refused, NULL, tp, 1984fa22ce15SAdrian Chadd mtod(m, const char *), tp, th); 1985df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 198657f60867SMark Johnston } 19870ca3f933SAndre Oppermann if (thflags & TH_RST) 1988df8bae1dSRodney W. Grimes goto drop; 19890ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 1990df8bae1dSRodney W. Grimes goto drop; 1991464fcfbcSAndre Oppermann 1992fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1993df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1994fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 199578b50714SRobert Watson TCPSTAT_INC(tcps_connects); 1996845799c1SAndras Olah soisconnected(so); 1997c488362eSRobert Watson #ifdef MAC 199830d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 1999c488362eSRobert Watson #endif 2000845799c1SAndras Olah /* Do window scaling on this connection? */ 2001845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2002845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2003845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 2004845799c1SAndras Olah } 2005766282cbSJohn Baldwin tp->rcv_adv += imin(tp->rcv_wnd, 2006766282cbSJohn Baldwin TCP_MAXWIN << tp->rcv_scale); 2007a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 2008a0292f23SGarrett Wollman /* 2009a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 2010a0292f23SGarrett Wollman * ACKNOW will be turned on later. 2011a0292f23SGarrett Wollman */ 2012c1e5a6e5SAndre Oppermann if (DELAY_ACK(tp, tlen) && tlen != 0) 2013b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 2014b8152ba7SAndre Oppermann tcp_delacktime); 2015a0292f23SGarrett Wollman else 2016a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2017f2512ba1SRui Paulo 2018603724d3SBjoern A. Zeeb if ((thflags & TH_ECE) && V_tcp_do_ecn) { 2019f2512ba1SRui Paulo tp->t_flags |= TF_ECN_PERMIT; 202078b50714SRobert Watson TCPSTAT_INC(tcps_ecn_shs); 2021f2512ba1SRui Paulo } 2022f2512ba1SRui Paulo 2023a0292f23SGarrett Wollman /* 2024a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 2025a0292f23SGarrett Wollman * Transitions: 2026a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 2027a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 2028a0292f23SGarrett Wollman */ 20299b8b58e0SJonathan Lemon tp->t_starttime = ticks; 2030a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 203157f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2032a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 2033fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 20347ff19458SPaul Traina } else { 203557f60867SMark Johnston tcp_state_change(tp, TCPS_ESTABLISHED); 2036d9fae5abSAndriy Gapon TCP_PROBE5(connect__established, NULL, tp, 2037fa22ce15SAdrian Chadd mtod(m, const char *), tp, th); 2038dbc42409SLawrence Stewart cc_conn_init(tp); 20399077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 20409077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 20417ff19458SPaul Traina } 2042a0292f23SGarrett Wollman } else { 2043a0292f23SGarrett Wollman /* 2044c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 2045153edc50SHiren Panchasara * simultaneous open. 2046c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 2047c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 2048a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 2049a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 2050a0292f23SGarrett Wollman */ 20514b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 2052b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 205357f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_RECEIVED); 2054a0292f23SGarrett Wollman } 2055df8bae1dSRodney W. Grimes 2056ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, ("%s: trimthenstep6: " 2057252ca428SRobert Watson "ti_locked %d", __func__, ti_locked)); 2058ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 20598501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 20607cfc6904SRobert Watson 2061df8bae1dSRodney W. Grimes /* 2062fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 2063df8bae1dSRodney W. Grimes * If data, trim to stay within window, 2064df8bae1dSRodney W. Grimes * dropping FIN if necessary. 2065df8bae1dSRodney W. Grimes */ 2066fb59c426SYoshinobu Inoue th->th_seq++; 2067fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 2068fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 2069df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2070fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 2071fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 207278b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 207378b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2074df8bae1dSRodney W. Grimes } 2075fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 2076fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 2077a0292f23SGarrett Wollman /* 2078a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 2079a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 2080a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 2081a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 2082a0292f23SGarrett Wollman * Otherwise, goto step 6. 2083a0292f23SGarrett Wollman */ 2084fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 2085a0292f23SGarrett Wollman goto process_ACK; 2086c068736aSJeffrey Hsu 2087df8bae1dSRodney W. Grimes goto step6; 2088c068736aSJeffrey Hsu 2089a0292f23SGarrett Wollman /* 2090a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 2091c94c54e4SAndre Oppermann * do normal processing. 2092a0292f23SGarrett Wollman * 2093c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 2094a0292f23SGarrett Wollman */ 2095a0292f23SGarrett Wollman case TCPS_LAST_ACK: 2096a0292f23SGarrett Wollman case TCPS_CLOSING: 2097a0292f23SGarrett Wollman break; /* continue normal processing */ 2098df8bae1dSRodney W. Grimes } 2099df8bae1dSRodney W. Grimes 2100df8bae1dSRodney W. Grimes /* 2101df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 210280ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 210380ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 210480ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 210580ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 210680ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 210780ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 210880ab7c0eSGarrett Wollman * killing a connection). 210980ab7c0eSGarrett Wollman * Then check timestamp, if present. 2110a0292f23SGarrett Wollman * Then check the connection count, if present. 2111df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 2112df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 2113df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 211480ab7c0eSGarrett Wollman */ 2115fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 21163220a212SGleb Smirnoff /* 21173220a212SGleb Smirnoff * RFC5961 Section 3.2 21183220a212SGleb Smirnoff * 21193220a212SGleb Smirnoff * - RST drops connection only if SEG.SEQ == RCV.NXT. 21203220a212SGleb Smirnoff * - If RST is in window, we send challenge ACK. 21213220a212SGleb Smirnoff * 21223220a212SGleb Smirnoff * Note: to take into account delayed ACKs, we should 21233220a212SGleb Smirnoff * test against last_ack_sent instead of rcv_nxt. 21243220a212SGleb Smirnoff * Note 2: we handle special case of closed window, not 21253220a212SGleb Smirnoff * covered by the RFC. 21263220a212SGleb Smirnoff */ 21273220a212SGleb Smirnoff if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 21283220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) || 21293220a212SGleb Smirnoff (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) { 213080ab7c0eSGarrett Wollman 2131ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2132ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, 21333220a212SGleb Smirnoff ("%s: TH_RST ti_locked %d, th %p tp %p", 21343220a212SGleb Smirnoff __func__, ti_locked, th, tp)); 21353220a212SGleb Smirnoff KASSERT(tp->t_state != TCPS_SYN_SENT, 21363220a212SGleb Smirnoff ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p", 21373220a212SGleb Smirnoff __func__, th, tp)); 21383220a212SGleb Smirnoff 21393220a212SGleb Smirnoff if (V_tcp_insecure_rst || 21403220a212SGleb Smirnoff tp->last_ack_sent == th->th_seq) { 21413220a212SGleb Smirnoff TCPSTAT_INC(tcps_drops); 21423220a212SGleb Smirnoff /* Drop the connection. */ 21433220a212SGleb Smirnoff switch (tp->t_state) { 214480ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 214580ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 214680ab7c0eSGarrett Wollman goto close; 214780ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 214880ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 214980ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 215080ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 215180ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 215280ab7c0eSGarrett Wollman close: 215357f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSED); 21543220a212SGleb Smirnoff /* FALLTHROUGH */ 21553220a212SGleb Smirnoff default: 215680ab7c0eSGarrett Wollman tp = tcp_close(tp); 21573220a212SGleb Smirnoff } 21583220a212SGleb Smirnoff } else { 21593220a212SGleb Smirnoff TCPSTAT_INC(tcps_badrst); 21603220a212SGleb Smirnoff /* Send challenge ACK. */ 21613220a212SGleb Smirnoff tcp_respond(tp, mtod(m, void *), th, m, 21623220a212SGleb Smirnoff tp->rcv_nxt, tp->snd_nxt, TH_ACK); 21633220a212SGleb Smirnoff tp->last_ack_sent = tp->rcv_nxt; 21643220a212SGleb Smirnoff m = NULL; 21653220a212SGleb Smirnoff } 21663220a212SGleb Smirnoff } 21673220a212SGleb Smirnoff goto drop; 21683220a212SGleb Smirnoff } 216980ab7c0eSGarrett Wollman 21703220a212SGleb Smirnoff /* 21713220a212SGleb Smirnoff * RFC5961 Section 4.2 21723220a212SGleb Smirnoff * Send challenge ACK for any SYN in synchronized state. 21733220a212SGleb Smirnoff */ 2174*281a0fd4SPatrick Kelsey if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT && 2175*281a0fd4SPatrick Kelsey tp->t_state != TCPS_SYN_RECEIVED) { 2176ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, 21773220a212SGleb Smirnoff ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked)); 2178ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2179252ca428SRobert Watson 21803220a212SGleb Smirnoff TCPSTAT_INC(tcps_badsyn); 21813220a212SGleb Smirnoff if (V_tcp_insecure_syn && 21823220a212SGleb Smirnoff SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 21833220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 21843220a212SGleb Smirnoff tp = tcp_drop(tp, ECONNRESET); 21853220a212SGleb Smirnoff rstreason = BANDLIM_UNLIMITED; 21863220a212SGleb Smirnoff } else { 21873220a212SGleb Smirnoff /* Send challenge ACK. */ 21883220a212SGleb Smirnoff tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt, 21893220a212SGleb Smirnoff tp->snd_nxt, TH_ACK); 21903220a212SGleb Smirnoff tp->last_ack_sent = tp->rcv_nxt; 21913220a212SGleb Smirnoff m = NULL; 219280ab7c0eSGarrett Wollman } 219380ab7c0eSGarrett Wollman goto drop; 219480ab7c0eSGarrett Wollman } 219580ab7c0eSGarrett Wollman 219680ab7c0eSGarrett Wollman /* 2197df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 2198df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 2199df8bae1dSRodney W. Grimes */ 2200be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 220180ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 2202df8bae1dSRodney W. Grimes 2203df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 2204d8951c8aSBjoern A. Zeeb if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 2205df8bae1dSRodney W. Grimes /* 2206df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 2207df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 2208df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 2209df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 2210df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 2211df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 2212df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 2213df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 2214df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 2215df8bae1dSRodney W. Grimes */ 2216df8bae1dSRodney W. Grimes tp->ts_recent = 0; 2217df8bae1dSRodney W. Grimes } else { 221878b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 221978b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 222078b50714SRobert Watson TCPSTAT_INC(tcps_pawsdrop); 222107fd333dSMatthew Dillon if (tlen) 2222df8bae1dSRodney W. Grimes goto dropafterack; 22231ab4789dSMatthew Dillon goto drop; 22241ab4789dSMatthew Dillon } 2225df8bae1dSRodney W. Grimes } 2226df8bae1dSRodney W. Grimes 2227a0292f23SGarrett Wollman /* 222880ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 222980ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 223080ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 223180ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 223280ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 223380ab7c0eSGarrett Wollman */ 2234a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 2235a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2236a57815efSBosko Milekic goto dropwithreset; 2237a57815efSBosko Milekic } 223880ab7c0eSGarrett Wollman 2239fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 2240df8bae1dSRodney W. Grimes if (todrop > 0) { 2241831ad37eSXin LI if (thflags & TH_SYN) { 2242fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 2243fb59c426SYoshinobu Inoue th->th_seq++; 2244fb59c426SYoshinobu Inoue if (th->th_urp > 1) 2245fb59c426SYoshinobu Inoue th->th_urp--; 2246df8bae1dSRodney W. Grimes else 2247fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2248df8bae1dSRodney W. Grimes todrop--; 2249df8bae1dSRodney W. Grimes } 2250df8bae1dSRodney W. Grimes /* 2251dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 2252df8bae1dSRodney W. Grimes */ 2253fb59c426SYoshinobu Inoue if (todrop > tlen 2254fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 2255dac20301SGarrett Wollman /* 2256dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 2257dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 2258dac20301SGarrett Wollman * of sequence; drop it. 2259dac20301SGarrett Wollman */ 2260fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2261dac20301SGarrett Wollman 2262df8bae1dSRodney W. Grimes /* 2263dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 2264dac20301SGarrett Wollman * But keep on processing for RST or ACK. 2265df8bae1dSRodney W. Grimes */ 2266dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2267fb59c426SYoshinobu Inoue todrop = tlen; 226878b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 226978b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 2270df8bae1dSRodney W. Grimes } else { 227178b50714SRobert Watson TCPSTAT_INC(tcps_rcvpartduppack); 227278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 2273df8bae1dSRodney W. Grimes } 2274fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 2275fb59c426SYoshinobu Inoue th->th_seq += todrop; 2276fb59c426SYoshinobu Inoue tlen -= todrop; 2277fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 2278fb59c426SYoshinobu Inoue th->th_urp -= todrop; 2279df8bae1dSRodney W. Grimes else { 2280fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2281fb59c426SYoshinobu Inoue th->th_urp = 0; 2282df8bae1dSRodney W. Grimes } 2283df8bae1dSRodney W. Grimes } 2284df8bae1dSRodney W. Grimes 2285df8bae1dSRodney W. Grimes /* 2286df8bae1dSRodney W. Grimes * If new data are received on a connection after the 2287df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 2288df8bae1dSRodney W. Grimes */ 2289df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 2290fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 2291ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, ("%s: SS_NOFDEREF && " 2292252ca428SRobert Watson "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked)); 2293ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2294252ca428SRobert Watson 229507dacf03SAndre Oppermann if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 229607dacf03SAndre Oppermann log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data " 229707dacf03SAndre Oppermann "after socket was closed, " 229807dacf03SAndre Oppermann "sending RST and removing tcpcb\n", 2299e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 2300773673c1SAndre Oppermann free(s, M_TCPLOG); 2301773673c1SAndre Oppermann } 2302df8bae1dSRodney W. Grimes tp = tcp_close(tp); 230378b50714SRobert Watson TCPSTAT_INC(tcps_rcvafterclose); 2304a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2305df8bae1dSRodney W. Grimes goto dropwithreset; 2306df8bae1dSRodney W. Grimes } 2307df8bae1dSRodney W. Grimes 2308df8bae1dSRodney W. Grimes /* 2309df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 2310df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 2311df8bae1dSRodney W. Grimes */ 2312fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 2313df8bae1dSRodney W. Grimes if (todrop > 0) { 231478b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 2315fb59c426SYoshinobu Inoue if (todrop >= tlen) { 231678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 2317df8bae1dSRodney W. Grimes /* 2318df8bae1dSRodney W. Grimes * If window is closed can only take segments at 2319df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 2320df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 2321df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 2322df8bae1dSRodney W. Grimes * and ack. 2323df8bae1dSRodney W. Grimes */ 2324fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2325df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 232678b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinprobe); 2327df8bae1dSRodney W. Grimes } else 2328df8bae1dSRodney W. Grimes goto dropafterack; 2329df8bae1dSRodney W. Grimes } else 233078b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2331df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2332fb59c426SYoshinobu Inoue tlen -= todrop; 2333fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 2334df8bae1dSRodney W. Grimes } 2335df8bae1dSRodney W. Grimes 2336df8bae1dSRodney W. Grimes /* 2337df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 2338df8bae1dSRodney W. Grimes * record its timestamp. 2339cf09195bSPaul Saab * NOTE: 2340cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 2341a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2342cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 2343cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 2344cf09195bSPaul Saab * predicated on the sequence space of this segment. 2345cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 2346cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 2347cf09195bSPaul Saab * instead of RFC1323's 2348cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 2349cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 2350cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 2351cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 2352cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 2353df8bae1dSRodney W. Grimes */ 2354be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 2355cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2356cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 2357cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 2358d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 2359a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 2360df8bae1dSRodney W. Grimes } 2361df8bae1dSRodney W. Grimes 2362df8bae1dSRodney W. Grimes /* 2363a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 2364a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 2365a0292f23SGarrett Wollman * later processing; else drop segment and return. 2366a0292f23SGarrett Wollman */ 2367fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 2368a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 2369*281a0fd4SPatrick Kelsey (tp->t_flags & TF_NEEDSYN)) { 2370*281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 2371*281a0fd4SPatrick Kelsey if (tp->t_state == TCPS_SYN_RECEIVED && 2372*281a0fd4SPatrick Kelsey tp->t_flags & TF_FASTOPEN) { 2373*281a0fd4SPatrick Kelsey tp->snd_wnd = tiwin; 2374*281a0fd4SPatrick Kelsey cc_conn_init(tp); 2375*281a0fd4SPatrick Kelsey } 2376*281a0fd4SPatrick Kelsey #endif 2377a0292f23SGarrett Wollman goto step6; 2378*281a0fd4SPatrick Kelsey } else if (tp->t_flags & TF_ACKNOW) 23795e1aa279SDavid Malone goto dropafterack; 2380a0292f23SGarrett Wollman else 2381a0292f23SGarrett Wollman goto drop; 2382a0292f23SGarrett Wollman } 2383df8bae1dSRodney W. Grimes 2384df8bae1dSRodney W. Grimes /* 2385df8bae1dSRodney W. Grimes * Ack processing. 2386df8bae1dSRodney W. Grimes */ 2387df8bae1dSRodney W. Grimes switch (tp->t_state) { 2388df8bae1dSRodney W. Grimes 2389df8bae1dSRodney W. Grimes /* 2390764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 2391764d8cefSBill Fenner * ESTABLISHED state and continue processing. 2392764d8cefSBill Fenner * The ACK was checked above. 2393df8bae1dSRodney W. Grimes */ 2394df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2395a0292f23SGarrett Wollman 239678b50714SRobert Watson TCPSTAT_INC(tcps_connects); 2397df8bae1dSRodney W. Grimes soisconnected(so); 2398df8bae1dSRodney W. Grimes /* Do window scaling? */ 2399df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2400df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2401df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 2402464fcfbcSAndre Oppermann tp->snd_wnd = tiwin; 2403df8bae1dSRodney W. Grimes } 2404a0292f23SGarrett Wollman /* 2405a0292f23SGarrett Wollman * Make transitions: 2406a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 2407a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 2408a0292f23SGarrett Wollman */ 24099b8b58e0SJonathan Lemon tp->t_starttime = ticks; 2410a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 241157f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2412a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 24137ff19458SPaul Traina } else { 241457f60867SMark Johnston tcp_state_change(tp, TCPS_ESTABLISHED); 2415d9fae5abSAndriy Gapon TCP_PROBE5(accept__established, NULL, tp, 2416fa22ce15SAdrian Chadd mtod(m, const char *), tp, th); 2417*281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 2418*281a0fd4SPatrick Kelsey if (tp->t_tfo_pending) { 2419*281a0fd4SPatrick Kelsey tcp_fastopen_decrement_counter(tp->t_tfo_pending); 2420*281a0fd4SPatrick Kelsey tp->t_tfo_pending = NULL; 2421*281a0fd4SPatrick Kelsey 2422*281a0fd4SPatrick Kelsey /* 2423*281a0fd4SPatrick Kelsey * Account for the ACK of our SYN prior to 2424*281a0fd4SPatrick Kelsey * regular ACK processing below. 2425*281a0fd4SPatrick Kelsey */ 2426*281a0fd4SPatrick Kelsey tp->snd_una++; 2427*281a0fd4SPatrick Kelsey } 2428*281a0fd4SPatrick Kelsey /* 2429*281a0fd4SPatrick Kelsey * TFO connections call cc_conn_init() during SYN 2430*281a0fd4SPatrick Kelsey * processing. Calling it again here for such 2431*281a0fd4SPatrick Kelsey * connections is not harmless as it would undo the 2432*281a0fd4SPatrick Kelsey * snd_cwnd reduction that occurs when a TFO SYN|ACK 2433*281a0fd4SPatrick Kelsey * is retransmitted. 2434*281a0fd4SPatrick Kelsey */ 2435*281a0fd4SPatrick Kelsey if (!(tp->t_flags & TF_FASTOPEN)) 2436*281a0fd4SPatrick Kelsey #endif 2437dbc42409SLawrence Stewart cc_conn_init(tp); 24389077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 24397ff19458SPaul Traina } 2440a0292f23SGarrett Wollman /* 2441a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 2442a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 2443a0292f23SGarrett Wollman */ 2444fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 2445fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 2446a0292f23SGarrett Wollman (struct mbuf *)0); 244760ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq - 1; 244893b0017fSPhilippe Charnier /* FALLTHROUGH */ 2449df8bae1dSRodney W. Grimes 2450df8bae1dSRodney W. Grimes /* 2451df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2452df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 2453fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 2454fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 2455df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 2456df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 2457df8bae1dSRodney W. Grimes */ 2458df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2459df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2460df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2461df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 2462df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2463df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 24645a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 246578b50714SRobert Watson TCPSTAT_INC(tcps_rcvacktoomuch); 24665a53ca16SPaul Saab goto dropafterack; 24675a53ca16SPaul Saab } 24683529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2469fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 2470fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 2471021eaf79SHiren Panchasara sack_changed = tcp_sack_doack(tp, &to, th->th_ack); 247212eeb81fSHiren Panchasara else 247312eeb81fSHiren Panchasara /* 247412eeb81fSHiren Panchasara * Reset the value so that previous (valid) value 247512eeb81fSHiren Panchasara * from the last ack with SACK doesn't get used. 247612eeb81fSHiren Panchasara */ 247712eeb81fSHiren Panchasara tp->sackhint.sacked_bytes = 0; 247839bc9de5SLawrence Stewart 247939bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 248039bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 248139bc9de5SLawrence Stewart 2482fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 2483021eaf79SHiren Panchasara if (tlen == 0 && 2484021eaf79SHiren Panchasara (tiwin == tp->snd_wnd || 2485021eaf79SHiren Panchasara (tp->t_flags & TF_SACK_PERMIT))) { 248662b90589SPeter Wemm /* 248762b90589SPeter Wemm * If this is the first time we've seen a 248862b90589SPeter Wemm * FIN from the remote, this is not a 248962b90589SPeter Wemm * duplicate and it needs to be processed 249062b90589SPeter Wemm * normally. This happens during a 249162b90589SPeter Wemm * simultaneous close. 249262b90589SPeter Wemm */ 249362b90589SPeter Wemm if ((thflags & TH_FIN) && 249462b90589SPeter Wemm (TCPS_HAVERCVDFIN(tp->t_state) == 0)) { 249562b90589SPeter Wemm tp->t_dupacks = 0; 249662b90589SPeter Wemm break; 249762b90589SPeter Wemm } 249878b50714SRobert Watson TCPSTAT_INC(tcps_rcvdupack); 2499df8bae1dSRodney W. Grimes /* 2500df8bae1dSRodney W. Grimes * If we have outstanding data (other than 2501df8bae1dSRodney W. Grimes * a window probe), this is a completely 2502df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 25035f30ec9bSEitan Adler * change and FIN isn't set), 25045f30ec9bSEitan Adler * the ack is the biggest we've 2505df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 2506df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 2507df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 2508df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 2509df8bae1dSRodney W. Grimes * window so we send only this one 2510df8bae1dSRodney W. Grimes * packet. 2511df8bae1dSRodney W. Grimes * 2512df8bae1dSRodney W. Grimes * We know we're losing at the current 2513df8bae1dSRodney W. Grimes * window size so do congestion avoidance 2514df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 2515df8bae1dSRodney W. Grimes * and pull our congestion window back to 2516df8bae1dSRodney W. Grimes * the new ssthresh). 2517df8bae1dSRodney W. Grimes * 2518df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 2519df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 2520df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 2521df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 2522df8bae1dSRodney W. Grimes * network. 2523f2512ba1SRui Paulo * 2524f2512ba1SRui Paulo * When using TCP ECN, notify the peer that 2525f2512ba1SRui Paulo * we reduced the cwnd. 2526df8bae1dSRodney W. Grimes */ 2527021eaf79SHiren Panchasara /* 2528021eaf79SHiren Panchasara * Following 2 kinds of acks should not affect 2529021eaf79SHiren Panchasara * dupack counting: 2530021eaf79SHiren Panchasara * 1) Old acks 2531021eaf79SHiren Panchasara * 2) Acks with SACK but without any new SACK 2532021eaf79SHiren Panchasara * information in them. These could result from 2533021eaf79SHiren Panchasara * any anomaly in the network like a switch 2534021eaf79SHiren Panchasara * duplicating packets or a possible DoS attack. 2535021eaf79SHiren Panchasara */ 2536021eaf79SHiren Panchasara if (th->th_ack != tp->snd_una || 2537021eaf79SHiren Panchasara ((tp->t_flags & TF_SACK_PERMIT) && 2538021eaf79SHiren Panchasara !sack_changed)) 2539021eaf79SHiren Panchasara break; 2540021eaf79SHiren Panchasara else if (!tcp_timer_active(tp, TT_REXMT)) 2541df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2542cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 2543dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2544dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_DUPACK); 25453529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2546dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2547808f11b7SPaul Saab int awnd; 254825e6f9edSPaul Saab 254925e6f9edSPaul Saab /* 255025e6f9edSPaul Saab * Compute the amount of data in flight first. 25514c3972f0SHiren Panchasara * We can inject new data into the pipe iff 255225e6f9edSPaul Saab * we have less than 1/2 the original window's 255325e6f9edSPaul Saab * worth of data in flight. 255425e6f9edSPaul Saab */ 255512eeb81fSHiren Panchasara if (V_tcp_do_rfc6675_pipe) 255612eeb81fSHiren Panchasara awnd = tcp_compute_pipe(tp); 255712eeb81fSHiren Panchasara else 2558808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 25590077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 256012eeb81fSHiren Panchasara 2561808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 256225e6f9edSPaul Saab tp->snd_cwnd += tp->t_maxseg; 256325e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 256425e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 256525e6f9edSPaul Saab } 256625e6f9edSPaul Saab } else 256746f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 256855bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 256946f58482SJonathan Lemon goto drop; 2570cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 2571cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 2572a0445c2eSJayanth Vijayaraghavan 2573a0445c2eSJayanth Vijayaraghavan /* 2574a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 2575a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 2576a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 2577a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 2578a0445c2eSJayanth Vijayaraghavan * recovery. 2579a0445c2eSJayanth Vijayaraghavan */ 25803529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 2581dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2582a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 2583a0445c2eSJayanth Vijayaraghavan break; 2584a0445c2eSJayanth Vijayaraghavan } 2585dbc42409SLawrence Stewart } else { 2586a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 25879d11646dSJeffrey Hsu tp->snd_recover)) { 2588cb942153SJeffrey Hsu tp->t_dupacks = 0; 2589cb942153SJeffrey Hsu break; 259046f58482SJonathan Lemon } 2591a0445c2eSJayanth Vijayaraghavan } 2592dbc42409SLawrence Stewart /* Congestion signal before ack. */ 2593dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_NDUPACK); 2594dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_DUPACK); 2595b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 25969b8b58e0SJonathan Lemon tp->t_rtttime = 0; 25973529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 259878b50714SRobert Watson TCPSTAT_INC( 259978b50714SRobert Watson tcps_sack_recovery_episode); 2600a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 26018db456bfSPaul Saab tp->snd_cwnd = tp->t_maxseg; 260255bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 26036d90faf3SPaul Saab goto drop; 26046d90faf3SPaul Saab } 2605fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 2606df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 260755bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 260848d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 2609302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 2610302ce8d6SAndre Oppermann __func__)); 2611df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 261248d2549cSJeffrey Hsu tp->t_maxseg * 261348d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 2614df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 2615df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 2616df8bae1dSRodney W. Grimes goto drop; 2617603724d3SBjoern A. Zeeb } else if (V_tcp_do_rfc3042) { 261862d4443fSHiren Panchasara /* 261962d4443fSHiren Panchasara * Process first and second duplicate 262062d4443fSHiren Panchasara * ACKs. Each indicates a segment 262162d4443fSHiren Panchasara * leaving the network, creating room 262262d4443fSHiren Panchasara * for more. Make sure we can send a 262362d4443fSHiren Panchasara * packet on reception of each duplicate 262462d4443fSHiren Panchasara * ACK by increasing snd_cwnd by one 262562d4443fSHiren Panchasara * segment. Restore the original 262662d4443fSHiren Panchasara * snd_cwnd after packet transmission. 262762d4443fSHiren Panchasara */ 2628dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_DUPACK); 2629582a954bSJeffrey Hsu u_long oldcwnd = tp->snd_cwnd; 263048d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 263148d2549cSJeffrey Hsu u_int sent; 26325628dd08SAndre Oppermann int avail; 263389c02376SJeffrey Hsu 2634582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 2635582a954bSJeffrey Hsu tp->t_dupacks == 2, 2636302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 2637302ce8d6SAndre Oppermann __func__)); 263861a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 263948d2549cSJeffrey Hsu tp->snd_limited = 0; 264061a36e3dSJeffrey Hsu tp->snd_cwnd = 264161a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 264261a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 264361a36e3dSJeffrey Hsu tp->t_maxseg; 26445628dd08SAndre Oppermann /* 26455628dd08SAndre Oppermann * Only call tcp_output when there 26465628dd08SAndre Oppermann * is new data available to be sent. 26475628dd08SAndre Oppermann * Otherwise we would send pure ACKs. 26485628dd08SAndre Oppermann */ 26495628dd08SAndre Oppermann SOCKBUF_LOCK(&so->so_snd); 2650cfa6009eSGleb Smirnoff avail = sbavail(&so->so_snd) - 26515628dd08SAndre Oppermann (tp->snd_nxt - tp->snd_una); 26525628dd08SAndre Oppermann SOCKBUF_UNLOCK(&so->so_snd); 26535628dd08SAndre Oppermann if (avail > 0) 265455bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 265548d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 265648d2549cSJeffrey Hsu if (sent > tp->t_maxseg) { 265789c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 265889c02376SJeffrey Hsu tp->snd_limited == 0) || 265989c02376SJeffrey Hsu (sent == tp->t_maxseg + 1 && 266089c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 2661302ce8d6SAndre Oppermann ("%s: sent too much", 2662302ce8d6SAndre Oppermann __func__)); 266348d2549cSJeffrey Hsu tp->snd_limited = 2; 266448d2549cSJeffrey Hsu } else if (sent > 0) 266548d2549cSJeffrey Hsu ++tp->snd_limited; 2666582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 2667582a954bSJeffrey Hsu goto drop; 2668df8bae1dSRodney W. Grimes } 2669021eaf79SHiren Panchasara } 2670df8bae1dSRodney W. Grimes break; 2671021eaf79SHiren Panchasara } else { 2672021eaf79SHiren Panchasara /* 2673021eaf79SHiren Panchasara * This ack is advancing the left edge, reset the 2674021eaf79SHiren Panchasara * counter. 2675021eaf79SHiren Panchasara */ 2676021eaf79SHiren Panchasara tp->t_dupacks = 0; 2677021eaf79SHiren Panchasara /* 2678021eaf79SHiren Panchasara * If this ack also has new SACK info, increment the 2679021eaf79SHiren Panchasara * counter as per rfc6675. 2680021eaf79SHiren Panchasara */ 2681021eaf79SHiren Panchasara if ((tp->t_flags & TF_SACK_PERMIT) && sack_changed) 2682021eaf79SHiren Panchasara tp->t_dupacks++; 2683df8bae1dSRodney W. Grimes } 2684cb942153SJeffrey Hsu 2685302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2686302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 2687cb942153SJeffrey Hsu 2688df8bae1dSRodney W. Grimes /* 2689df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2690df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2691df8bae1dSRodney W. Grimes */ 2692dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2693cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 26943529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 26956d90faf3SPaul Saab tcp_sack_partialack(tp, th); 26966d90faf3SPaul Saab else 2697c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2698dbc42409SLawrence Stewart } else 2699dbc42409SLawrence Stewart cc_post_recovery(tp, th); 270046f58482SJonathan Lemon } 2701a0292f23SGarrett Wollman /* 2702a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2703a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2704a0292f23SGarrett Wollman */ 2705a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2706a0292f23SGarrett Wollman /* 2707a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2708a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 270907e43e10SAndras Olah * synchronized). Go to non-starred state, 271007e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 271107e43e10SAndras Olah * we can do window scaling. 2712a0292f23SGarrett Wollman */ 2713a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2714a0292f23SGarrett Wollman tp->snd_una++; 271507e43e10SAndras Olah /* Do window scaling? */ 271607e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 271707e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 271807e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2719464fcfbcSAndre Oppermann /* Send window already scaled. */ 272007e43e10SAndras Olah } 2721a0292f23SGarrett Wollman } 2722a0292f23SGarrett Wollman 2723a0292f23SGarrett Wollman process_ACK: 27248501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 27257cfc6904SRobert Watson 2726dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 272778b50714SRobert Watson TCPSTAT_INC(tcps_rcvackpack); 272878b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 2729df8bae1dSRodney W. Grimes 2730df8bae1dSRodney W. Grimes /* 27319b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 27329b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 27339b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 27349b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 27359b8b58e0SJonathan Lemon * we left off. 27369b8b58e0SJonathan Lemon */ 2737672dc4aeSJohn Baldwin if (tp->t_rxtshift == 1 && tp->t_flags & TF_PREVVALID && 2738672dc4aeSJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) 2739dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 27409b8b58e0SJonathan Lemon 27419b8b58e0SJonathan Lemon /* 2742df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2743df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2744df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2745df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2746df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2747df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2748df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2749fa55172bSMatthew Dillon * 2750fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2751fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2752fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2753fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2754df8bae1dSRodney W. Grimes */ 2755d8951c8aSBjoern A. Zeeb if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) { 2756d8951c8aSBjoern A. Zeeb u_int t; 2757d8951c8aSBjoern A. Zeeb 2758d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 2759d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 2760d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 2761d8951c8aSBjoern A. Zeeb tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1); 2762fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2763eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2764eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 27659b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2766fa55172bSMatthew Dillon } 2767df8bae1dSRodney W. Grimes 2768df8bae1dSRodney W. Grimes /* 2769df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2770df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2771df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2772df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2773df8bae1dSRodney W. Grimes */ 2774fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 2775b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2776df8bae1dSRodney W. Grimes needoutput = 1; 2777b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2778b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2779a0292f23SGarrett Wollman 2780a0292f23SGarrett Wollman /* 2781a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2782a0292f23SGarrett Wollman * skip rest of ACK processing. 2783a0292f23SGarrett Wollman */ 2784a0292f23SGarrett Wollman if (acked == 0) 2785a0292f23SGarrett Wollman goto step6; 2786a0292f23SGarrett Wollman 2787df8bae1dSRodney W. Grimes /* 2788dbc42409SLawrence Stewart * Let the congestion control algorithm update congestion 2789dbc42409SLawrence Stewart * control related information. This typically means increasing 2790dbc42409SLawrence Stewart * the congestion window. 2791df8bae1dSRodney W. Grimes */ 2792dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_ACK); 2793dbc42409SLawrence Stewart 27945905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2795cfa6009eSGleb Smirnoff if (acked > sbavail(&so->so_snd)) { 2796cfa6009eSGleb Smirnoff tp->snd_wnd -= sbavail(&so->so_snd); 2797c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, 2798cfa6009eSGleb Smirnoff (int)sbavail(&so->so_snd)); 2799df8bae1dSRodney W. Grimes ourfinisacked = 1; 2800df8bae1dSRodney W. Grimes } else { 2801c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, acked); 280260ee3bb2SAndre Oppermann tp->snd_wnd -= acked; 2803df8bae1dSRodney W. Grimes ourfinisacked = 0; 2804df8bae1dSRodney W. Grimes } 2805564aab1fSAndre Oppermann /* NB: sowwakeup_locked() does an implicit unlock. */ 28061e4d7da7SRobert Watson sowwakeup_locked(so); 2807c11a15bfSGleb Smirnoff m_freem(mfree); 2808e8949f74SAndre Oppermann /* Detect una wraparound. */ 2809dbc42409SLawrence Stewart if (!IN_RECOVERY(tp->t_flags) && 28109d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 28119d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 28129d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 2813dbc42409SLawrence Stewart /* XXXLAS: Can this be moved up into cc_post_recovery? */ 2814dbc42409SLawrence Stewart if (IN_RECOVERY(tp->t_flags) && 281524cb0f22SLawrence Stewart SEQ_GEQ(th->th_ack, tp->snd_recover)) { 2816dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 281724cb0f22SLawrence Stewart } 2818fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 28193529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 28206d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 28216d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 28226d90faf3SPaul Saab } 2823df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2824df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2825df8bae1dSRodney W. Grimes 2826df8bae1dSRodney W. Grimes switch (tp->t_state) { 2827df8bae1dSRodney W. Grimes 2828df8bae1dSRodney W. Grimes /* 2829df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2830df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2831df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2832df8bae1dSRodney W. Grimes */ 2833df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2834df8bae1dSRodney W. Grimes if (ourfinisacked) { 2835df8bae1dSRodney W. Grimes /* 2836df8bae1dSRodney W. Grimes * If we can't receive any more 2837df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2838df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2839df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2840df8bae1dSRodney W. Grimes * we'll hang forever. 2841e8949f74SAndre Oppermann * 2842e8949f74SAndre Oppermann * XXXjl: 2843340c35deSJonathan Lemon * we should release the tp also, and use a 2844340c35deSJonathan Lemon * compressed state. 2845340c35deSJonathan Lemon */ 2846c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 284703e49181SSeigo Tanimura soisdisconnected(so); 28489077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 28499077f387SGleb Smirnoff (tcp_fast_finwait2_recycle ? 28509077f387SGleb Smirnoff tcp_finwait2_timeout : 28519077f387SGleb Smirnoff TP_MAXIDLE(tp))); 28524cc20ab1SSeigo Tanimura } 285357f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_2); 2854df8bae1dSRodney W. Grimes } 2855df8bae1dSRodney W. Grimes break; 2856df8bae1dSRodney W. Grimes 2857df8bae1dSRodney W. Grimes /* 2858df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2859df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2860df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2861df8bae1dSRodney W. Grimes * the segment. 2862df8bae1dSRodney W. Grimes */ 2863df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2864df8bae1dSRodney W. Grimes if (ourfinisacked) { 2865ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 286611a20fb8SJeffrey Hsu tcp_twstart(tp); 2867ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 2868340c35deSJonathan Lemon m_freem(m); 2869679d9708SAndre Oppermann return; 2870df8bae1dSRodney W. Grimes } 2871df8bae1dSRodney W. Grimes break; 2872df8bae1dSRodney W. Grimes 2873df8bae1dSRodney W. Grimes /* 2874df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2875df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2876df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2877df8bae1dSRodney W. Grimes * enter the closed state and return. 2878df8bae1dSRodney W. Grimes */ 2879df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2880df8bae1dSRodney W. Grimes if (ourfinisacked) { 2881ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2882df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2883df8bae1dSRodney W. Grimes goto drop; 2884df8bae1dSRodney W. Grimes } 2885df8bae1dSRodney W. Grimes break; 2886df8bae1dSRodney W. Grimes } 2887df8bae1dSRodney W. Grimes } 2888df8bae1dSRodney W. Grimes 2889df8bae1dSRodney W. Grimes step6: 28908501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 28917cfc6904SRobert Watson 2892df8bae1dSRodney W. Grimes /* 289360ee3bb2SAndre Oppermann * Update window information. 289460ee3bb2SAndre Oppermann * Don't look at window if no ACK: TAC's send garbage on first SYN. 2895df8bae1dSRodney W. Grimes */ 289660ee3bb2SAndre Oppermann if ((thflags & TH_ACK) && 289760ee3bb2SAndre Oppermann (SEQ_LT(tp->snd_wl1, th->th_seq) || 289860ee3bb2SAndre Oppermann (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 289960ee3bb2SAndre Oppermann (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 290060ee3bb2SAndre Oppermann /* keep track of pure window updates */ 290160ee3bb2SAndre Oppermann if (tlen == 0 && 290260ee3bb2SAndre Oppermann tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 290378b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinupd); 2904df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 290560ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 290660ee3bb2SAndre Oppermann tp->snd_wl2 = th->th_ack; 2907df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2908df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 290960ee3bb2SAndre Oppermann needoutput = 1; 2910df8bae1dSRodney W. Grimes } 2911df8bae1dSRodney W. Grimes 2912df8bae1dSRodney W. Grimes /* 2913df8bae1dSRodney W. Grimes * Process segments with URG. 2914df8bae1dSRodney W. Grimes */ 2915fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2916df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2917df8bae1dSRodney W. Grimes /* 2918df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2919df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2920df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2921df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2922df8bae1dSRodney W. Grimes */ 292318ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2924cfa6009eSGleb Smirnoff if (th->th_urp + sbavail(&so->so_rcv) > sb_max) { 2925fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2926fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 292718ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2928df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2929df8bae1dSRodney W. Grimes } 2930df8bae1dSRodney W. Grimes /* 2931df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2932df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2933df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2934df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2935df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2936df8bae1dSRodney W. Grimes * 2937df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2938df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2939df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2940df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2941df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2942df8bae1dSRodney W. Grimes * spec states (in one of two places). 2943df8bae1dSRodney W. Grimes */ 2944fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2945fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2946cfa6009eSGleb Smirnoff so->so_oobmark = sbavail(&so->so_rcv) + 2947df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2948927c5ceaSRobert Watson if (so->so_oobmark == 0) 2949c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 2950df8bae1dSRodney W. Grimes sohasoutofband(so); 2951df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2952df8bae1dSRodney W. Grimes } 295318ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2954df8bae1dSRodney W. Grimes /* 2955df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2956df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2957df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2958df8bae1dSRodney W. Grimes * data may creep in... ick. 2959df8bae1dSRodney W. Grimes */ 296030613f56SJeffrey Hsu if (th->th_urp <= (u_long)tlen && 296130613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 296230613f56SJeffrey Hsu /* hdr drop is delayed */ 296330613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 296430613f56SJeffrey Hsu } 2965c068736aSJeffrey Hsu } else { 2966df8bae1dSRodney W. Grimes /* 2967df8bae1dSRodney W. Grimes * If no out of band data is expected, 2968df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2969df8bae1dSRodney W. Grimes * with the receive window. 2970df8bae1dSRodney W. Grimes */ 2971df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2972df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2973c068736aSJeffrey Hsu } 2974df8bae1dSRodney W. Grimes dodata: /* XXX */ 29758501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 29767cfc6904SRobert Watson 2977df8bae1dSRodney W. Grimes /* 2978df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2979df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2980df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2981df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2982df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2983df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2984df8bae1dSRodney W. Grimes */ 2985*281a0fd4SPatrick Kelsey tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) && 2986*281a0fd4SPatrick Kelsey (tp->t_flags & TF_FASTOPEN)); 2987*281a0fd4SPatrick Kelsey if ((tlen || (thflags & TH_FIN) || tfo_syn) && 2988df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 298969e03620SPaul Saab tcp_seq save_start = th->th_seq; 2990fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2991e4b64281SJesper Skriver /* 2992c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2993c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2994c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2995c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2996c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2997c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2998c068736aSJeffrey Hsu * conversions. 2999c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 3000c068736aSJeffrey Hsu * immediately when segments are out of order (so 3001c068736aSJeffrey Hsu * fast retransmit can work). 3002e4b64281SJesper Skriver */ 30034741bfcbSPatrick Kelsey if (th->th_seq == tp->rcv_nxt && 30044741bfcbSPatrick Kelsey LIST_EMPTY(&tp->t_segq) && 3005*281a0fd4SPatrick Kelsey (TCPS_HAVEESTABLISHED(tp->t_state) || 3006*281a0fd4SPatrick Kelsey tfo_syn)) { 3007*281a0fd4SPatrick Kelsey if (DELAY_ACK(tp, tlen) || tfo_syn) 30083bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 3009e4b64281SJesper Skriver else 3010e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 3011e4b64281SJesper Skriver tp->rcv_nxt += tlen; 3012e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 301378b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 301478b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 30151e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3016c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 3017c1c36a2cSMike Silbersack m_freem(m); 3018c1c36a2cSMike Silbersack else 3019651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 3020e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 30211e4d7da7SRobert Watson sorwakeup_locked(so); 3022e4b64281SJesper Skriver } else { 3023e8949f74SAndre Oppermann /* 3024e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 3025e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 3026e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 3027e8949f74SAndre Oppermann * when trimming from the head. 3028e8949f74SAndre Oppermann */ 3029e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 3030e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 3031e4b64281SJesper Skriver } 30323529149eSAndre Oppermann if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 3033f194524fSAndre Oppermann tcp_update_sack_list(tp, save_start, save_start + tlen); 3034302ce8d6SAndre Oppermann #if 0 3035df8bae1dSRodney W. Grimes /* 3036df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 3037df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 3038df8bae1dSRodney W. Grimes * buffer size. 3039302ce8d6SAndre Oppermann * XXX: Unused. 3040df8bae1dSRodney W. Grimes */ 3041f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) 3042df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 3043f701e30dSJohn Baldwin else 3044f701e30dSJohn Baldwin len = so->so_rcv.sb_hiwat; 3045302ce8d6SAndre Oppermann #endif 3046df8bae1dSRodney W. Grimes } else { 3047df8bae1dSRodney W. Grimes m_freem(m); 3048fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 3049df8bae1dSRodney W. Grimes } 3050df8bae1dSRodney W. Grimes 3051df8bae1dSRodney W. Grimes /* 3052df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 3053df8bae1dSRodney W. Grimes * that the connection is closing. 3054df8bae1dSRodney W. Grimes */ 3055fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 3056df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 3057df8bae1dSRodney W. Grimes socantrcvmore(so); 3058a0292f23SGarrett Wollman /* 3059a0292f23SGarrett Wollman * If connection is half-synchronized 3060d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 3061a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 3062a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 3063a0292f23SGarrett Wollman * more input can be expected, send ACK now. 3064a0292f23SGarrett Wollman */ 30653bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 30663bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 3067a0292f23SGarrett Wollman else 3068df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 3069df8bae1dSRodney W. Grimes tp->rcv_nxt++; 3070df8bae1dSRodney W. Grimes } 3071df8bae1dSRodney W. Grimes switch (tp->t_state) { 3072df8bae1dSRodney W. Grimes 3073df8bae1dSRodney W. Grimes /* 3074df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 3075df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 3076df8bae1dSRodney W. Grimes */ 3077df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 30789b8b58e0SJonathan Lemon tp->t_starttime = ticks; 30799b8b58e0SJonathan Lemon /* FALLTHROUGH */ 3080df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 308157f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSE_WAIT); 3082df8bae1dSRodney W. Grimes break; 3083df8bae1dSRodney W. Grimes 3084df8bae1dSRodney W. Grimes /* 3085df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 3086df8bae1dSRodney W. Grimes * enter the CLOSING state. 3087df8bae1dSRodney W. Grimes */ 3088df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 308957f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSING); 3090df8bae1dSRodney W. Grimes break; 3091df8bae1dSRodney W. Grimes 3092df8bae1dSRodney W. Grimes /* 3093df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 3094df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 3095df8bae1dSRodney W. Grimes * standard timers. 3096df8bae1dSRodney W. Grimes */ 3097df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 3098ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 3099ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, ("%s: dodata " 3100252ca428SRobert Watson "TCP_FIN_WAIT_2 ti_locked: %d", __func__, 3101252ca428SRobert Watson ti_locked)); 3102252ca428SRobert Watson 3103340c35deSJonathan Lemon tcp_twstart(tp); 3104ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 3105679d9708SAndre Oppermann return; 3106df8bae1dSRodney W. Grimes } 3107df8bae1dSRodney W. Grimes } 3108ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 3109ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 3110252ca428SRobert Watson ti_locked = TI_UNLOCKED; 3111252ca428SRobert Watson 3112610ee2f9SDavid Greenman #ifdef TCPDEBUG 31134cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3114fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 3115fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3116610ee2f9SDavid Greenman #endif 31175d06879aSGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, mtod(m, const char *)); 3118df8bae1dSRodney W. Grimes 3119df8bae1dSRodney W. Grimes /* 3120df8bae1dSRodney W. Grimes * Return any desired output. 3121df8bae1dSRodney W. Grimes */ 3122df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 312355bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 31247792ea27SJeffrey Hsu 3125a14c749fSJonathan Lemon check_delack: 3126252ca428SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 3127252ca428SRobert Watson __func__, ti_locked)); 3128603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 31298501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 3130252ca428SRobert Watson 3131a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 31323bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 3133b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 31343bfd6421SJonathan Lemon } 31358501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3136679d9708SAndre Oppermann return; 3137df8bae1dSRodney W. Grimes 3138df8bae1dSRodney W. Grimes dropafterack: 3139df8bae1dSRodney W. Grimes /* 3140df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 3141df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 314280ab7c0eSGarrett Wollman * 314380ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 314480ab7c0eSGarrett Wollman * paths to this code happen after packets containing 314580ab7c0eSGarrett Wollman * RST have been dropped. 314680ab7c0eSGarrett Wollman * 314780ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 314880ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 314980ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 315080ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 315180ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 315280ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 3153df8bae1dSRodney W. Grimes */ 3154fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 3155fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 3156a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 3157a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 3158a57815efSBosko Milekic goto dropwithreset; 3159a57815efSBosko Milekic } 3160a0292f23SGarrett Wollman #ifdef TCPDEBUG 31614cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3162fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3163fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3164a0292f23SGarrett Wollman #endif 31655d06879aSGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, mtod(m, const char *)); 3166ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 3167ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 3168252ca428SRobert Watson ti_locked = TI_UNLOCKED; 3169252ca428SRobert Watson 3170df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 317155bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 31728501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3173d6915262SRobert Watson m_freem(m); 3174679d9708SAndre Oppermann return; 3175df8bae1dSRodney W. Grimes 3176df8bae1dSRodney W. Grimes dropwithreset: 3177ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 3178ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 3179252ca428SRobert Watson ti_locked = TI_UNLOCKED; 3180a57815efSBosko Milekic 3181a0ca0871SRobert Watson if (tp != NULL) { 3182302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 31838501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3184dd8ac7f9SRobert Watson } else 3185a0ca0871SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 3186679d9708SAndre Oppermann return; 3187df8bae1dSRodney W. Grimes 3188df8bae1dSRodney W. Grimes drop: 3189ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 3190ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 3191fa046d87SRobert Watson ti_locked = TI_UNLOCKED; 3192fa046d87SRobert Watson } 3193252ca428SRobert Watson #ifdef INVARIANTS 3194252ca428SRobert Watson else 3195252ca428SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 3196252ca428SRobert Watson #endif 3197252ca428SRobert Watson 3198df8bae1dSRodney W. Grimes /* 3199df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 3200df8bae1dSRodney W. Grimes */ 3201610ee2f9SDavid Greenman #ifdef TCPDEBUG 32021c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 3203fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3204fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3205a0292f23SGarrett Wollman #endif 32065d06879aSGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, mtod(m, const char *)); 32071c53f806SRobert Watson if (tp != NULL) 32088501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3209d6915262SRobert Watson m_freem(m); 3210302ce8d6SAndre Oppermann } 3211302ce8d6SAndre Oppermann 3212302ce8d6SAndre Oppermann /* 32130ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 32140ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 32150ca3f933SAndre Oppermann * tp may be NULL. 3216302ce8d6SAndre Oppermann */ 321755bceb1eSRandall Stewart void 3218302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 3219302ce8d6SAndre Oppermann int tlen, int rstreason) 3220302ce8d6SAndre Oppermann { 3221b287c6c7SBjoern A. Zeeb #ifdef INET 3222302ce8d6SAndre Oppermann struct ip *ip; 3223b287c6c7SBjoern A. Zeeb #endif 3224302ce8d6SAndre Oppermann #ifdef INET6 3225302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 3226302ce8d6SAndre Oppermann #endif 32277a3244ccSRobert Watson 32287a3244ccSRobert Watson if (tp != NULL) { 32298501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 32307a3244ccSRobert Watson } 32317a3244ccSRobert Watson 32320ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 3233302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 3234302ce8d6SAndre Oppermann goto drop; 3235302ce8d6SAndre Oppermann #ifdef INET6 3236302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 3237302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 3238302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3239302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3240302ce8d6SAndre Oppermann goto drop; 3241302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 3242b287c6c7SBjoern A. Zeeb } 3243302ce8d6SAndre Oppermann #endif 3244b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3245b287c6c7SBjoern A. Zeeb else 3246b287c6c7SBjoern A. Zeeb #endif 3247b287c6c7SBjoern A. Zeeb #ifdef INET 3248302ce8d6SAndre Oppermann { 3249302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 3250302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3251302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3252302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3253302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3254302ce8d6SAndre Oppermann goto drop; 3255302ce8d6SAndre Oppermann } 3256b287c6c7SBjoern A. Zeeb #endif 3257302ce8d6SAndre Oppermann 3258302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 3259302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 3260302ce8d6SAndre Oppermann goto drop; 3261302ce8d6SAndre Oppermann 3262302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 3263302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 3264302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 3265302ce8d6SAndre Oppermann th->th_ack, TH_RST); 3266302ce8d6SAndre Oppermann } else { 3267302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 3268302ce8d6SAndre Oppermann tlen++; 3269302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 3270302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 3271302ce8d6SAndre Oppermann } 3272302ce8d6SAndre Oppermann return; 3273302ce8d6SAndre Oppermann drop: 3274302ce8d6SAndre Oppermann m_freem(m); 3275df8bae1dSRodney W. Grimes } 3276df8bae1dSRodney W. Grimes 3277be2ac88cSJonathan Lemon /* 3278be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 3279be2ac88cSJonathan Lemon */ 328055bceb1eSRandall Stewart void 3281ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 3282df8bae1dSRodney W. Grimes { 3283df8bae1dSRodney W. Grimes int opt, optlen; 3284df8bae1dSRodney W. Grimes 3285be2ac88cSJonathan Lemon to->to_flags = 0; 3286df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 3287df8bae1dSRodney W. Grimes opt = cp[0]; 3288df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 3289df8bae1dSRodney W. Grimes break; 3290df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 3291df8bae1dSRodney W. Grimes optlen = 1; 3292df8bae1dSRodney W. Grimes else { 3293b474779fSJun-ichiro itojun Hagino if (cnt < 2) 3294b474779fSJun-ichiro itojun Hagino break; 3295df8bae1dSRodney W. Grimes optlen = cp[1]; 3296b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 3297df8bae1dSRodney W. Grimes break; 3298df8bae1dSRodney W. Grimes } 3299df8bae1dSRodney W. Grimes switch (opt) { 3300df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 3301df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 3302df8bae1dSRodney W. Grimes continue; 3303f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3304df8bae1dSRodney W. Grimes continue; 3305be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 3306be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 3307be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 3308fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 3309df8bae1dSRodney W. Grimes break; 3310df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 3311df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 3312df8bae1dSRodney W. Grimes continue; 3313f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3314df8bae1dSRodney W. Grimes continue; 3315be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 331602a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 3317df8bae1dSRodney W. Grimes break; 3318df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 3319df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 3320df8bae1dSRodney W. Grimes continue; 3321be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 3322a0292f23SGarrett Wollman bcopy((char *)cp + 2, 3323a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 3324fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 3325a0292f23SGarrett Wollman bcopy((char *)cp + 6, 3326a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 3327fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 3328df8bae1dSRodney W. Grimes break; 33291cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 33301cfd4b53SBruce M Simpson /* 33311cfd4b53SBruce M Simpson * XXX In order to reply to a host which has set the 33321cfd4b53SBruce M Simpson * TCP_SIGNATURE option in its initial SYN, we have to 33331cfd4b53SBruce M Simpson * record the fact that the option was observed here 33341cfd4b53SBruce M Simpson * for the syncache code to perform the correct response. 33351cfd4b53SBruce M Simpson */ 33361cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 33371cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 33381cfd4b53SBruce M Simpson continue; 3339df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 3340df47e437SAndre Oppermann to->to_signature = cp + 2; 33411cfd4b53SBruce M Simpson break; 3342265ed012SBruce M Simpson #endif 33436d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 3344f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 33456d90faf3SPaul Saab continue; 3346f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3347f72167f4SAndre Oppermann continue; 3348603724d3SBjoern A. Zeeb if (!V_tcp_do_sack) 3349f72167f4SAndre Oppermann continue; 3350fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 33516d90faf3SPaul Saab break; 33526d90faf3SPaul Saab case TCPOPT_SACK: 33535a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 33546d90faf3SPaul Saab continue; 3355b728e902SAndre Oppermann if (flags & TO_SYN) 3356b728e902SAndre Oppermann continue; 3357fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 33585a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 33595a53ca16SPaul Saab to->to_sacks = cp + 2; 336078b50714SRobert Watson TCPSTAT_INC(tcps_sack_rcv_blocks); 33616d90faf3SPaul Saab break; 3362*281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 3363*281a0fd4SPatrick Kelsey case TCPOPT_FAST_OPEN: 3364*281a0fd4SPatrick Kelsey if ((optlen != TCPOLEN_FAST_OPEN_EMPTY) && 3365*281a0fd4SPatrick Kelsey (optlen < TCPOLEN_FAST_OPEN_MIN) && 3366*281a0fd4SPatrick Kelsey (optlen > TCPOLEN_FAST_OPEN_MAX)) 3367*281a0fd4SPatrick Kelsey continue; 3368*281a0fd4SPatrick Kelsey if (!(flags & TO_SYN)) 3369*281a0fd4SPatrick Kelsey continue; 3370*281a0fd4SPatrick Kelsey if (!V_tcp_fastopen_enabled) 3371*281a0fd4SPatrick Kelsey continue; 3372*281a0fd4SPatrick Kelsey to->to_flags |= TOF_FASTOPEN; 3373*281a0fd4SPatrick Kelsey to->to_tfo_len = optlen - 2; 3374*281a0fd4SPatrick Kelsey to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL; 3375*281a0fd4SPatrick Kelsey break; 3376*281a0fd4SPatrick Kelsey #endif 3377be2ac88cSJonathan Lemon default: 3378be2ac88cSJonathan Lemon continue; 3379df8bae1dSRodney W. Grimes } 3380df8bae1dSRodney W. Grimes } 3381df8bae1dSRodney W. Grimes } 3382df8bae1dSRodney W. Grimes 3383df8bae1dSRodney W. Grimes /* 3384df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 3385df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 3386df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 3387df8bae1dSRodney W. Grimes * sequencing purposes. 3388df8bae1dSRodney W. Grimes */ 338955bceb1eSRandall Stewart void 3390ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 3391ad3f9ab3SAndre Oppermann int off) 3392df8bae1dSRodney W. Grimes { 3393fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 3394df8bae1dSRodney W. Grimes 3395df8bae1dSRodney W. Grimes while (cnt >= 0) { 3396df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 3397df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 3398df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 3399df8bae1dSRodney W. Grimes 34008501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 34017a3244ccSRobert Watson 3402df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 3403df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 3404df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 3405df8bae1dSRodney W. Grimes m->m_len--; 340669a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 340769a34685SYoshinobu Inoue m->m_pkthdr.len--; 3408df8bae1dSRodney W. Grimes return; 3409df8bae1dSRodney W. Grimes } 3410df8bae1dSRodney W. Grimes cnt -= m->m_len; 3411df8bae1dSRodney W. Grimes m = m->m_next; 34124dfdffe9SAndre Oppermann if (m == NULL) 3413df8bae1dSRodney W. Grimes break; 3414df8bae1dSRodney W. Grimes } 3415df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 3416df8bae1dSRodney W. Grimes } 3417df8bae1dSRodney W. Grimes 3418df8bae1dSRodney W. Grimes /* 3419df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 3420df8bae1dSRodney W. Grimes * and update averages and current timeout. 3421df8bae1dSRodney W. Grimes */ 342255bceb1eSRandall Stewart void 3423ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 3424df8bae1dSRodney W. Grimes { 3425ad3f9ab3SAndre Oppermann int delta; 3426233e8c18SGarrett Wollman 34278501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 34282be3bf22SRobert Watson 342978b50714SRobert Watson TCPSTAT_INC(tcps_rttupdated); 3430233e8c18SGarrett Wollman tp->t_rttupdated++; 3431233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 3432233e8c18SGarrett Wollman /* 3433233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 3434233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 3435233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 3436233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3437233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 3438233e8c18SGarrett Wollman */ 3439233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3440233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3441233e8c18SGarrett Wollman 3442233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 3443233e8c18SGarrett Wollman tp->t_srtt = 1; 3444233e8c18SGarrett Wollman 3445233e8c18SGarrett Wollman /* 3446233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 3447233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 3448233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 3449233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 3450233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 3451233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 3452233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3453233e8c18SGarrett Wollman * rfc793's wired-in beta. 3454233e8c18SGarrett Wollman */ 3455233e8c18SGarrett Wollman if (delta < 0) 3456233e8c18SGarrett Wollman delta = -delta; 3457233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3458233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 3459233e8c18SGarrett Wollman tp->t_rttvar = 1; 34601fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 34611fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3462233e8c18SGarrett Wollman } else { 3463233e8c18SGarrett Wollman /* 3464233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 3465233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 3466233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 3467233e8c18SGarrett Wollman */ 3468233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 3469233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 34701fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3471233e8c18SGarrett Wollman } 34729b8b58e0SJonathan Lemon tp->t_rtttime = 0; 3473df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 3474df8bae1dSRodney W. Grimes 3475df8bae1dSRodney W. Grimes /* 3476df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 3477df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 3478df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 3479df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 3480df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 3481df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 3482df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 3483df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 3484df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 3485df8bae1dSRodney W. Grimes */ 3486233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 34879e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3488df8bae1dSRodney W. Grimes 3489df8bae1dSRodney W. Grimes /* 3490df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 3491df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 3492df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 3493df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 3494df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 3495df8bae1dSRodney W. Grimes */ 3496df8bae1dSRodney W. Grimes tp->t_softerror = 0; 3497df8bae1dSRodney W. Grimes } 3498df8bae1dSRodney W. Grimes 3499df8bae1dSRodney W. Grimes /* 3500df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 3501df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 35024faaea55SAndre Oppermann * If none, use an mss that can be handled on the outgoing interface 35034faaea55SAndre Oppermann * without forcing IP to fragment. If no route is found, route has no mtu, 3504df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 3505df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 3506df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 3507df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 3508df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 3509df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 3510df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 3511a0292f23SGarrett Wollman * 3512a0292f23SGarrett Wollman * Also take into account the space needed for options that we 3513a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 3514a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 3515a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 3516a0292f23SGarrett Wollman * data in maxopd. 3517a0292f23SGarrett Wollman * 351897d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 3519ef341ee1SGleb Smirnoff * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS 3520ef341ee1SGleb Smirnoff * settings are handled in tcp_mssopt(). 3521df8bae1dSRodney W. Grimes */ 3522a0292f23SGarrett Wollman void 3523ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer, 35243c914c54SAndre Oppermann struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap) 3525df8bae1dSRodney W. Grimes { 3526b287c6c7SBjoern A. Zeeb int mss = 0; 3527b287c6c7SBjoern A. Zeeb u_long maxmtu = 0; 3528c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 352997d8d152SAndre Oppermann struct hc_metrics_lite metrics; 3530ef341ee1SGleb Smirnoff int origoffer; 3531fb59c426SYoshinobu Inoue #ifdef INET6 3532c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3533c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 3534c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3535c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 3536c068736aSJeffrey Hsu #else 3537c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 3538fb59c426SYoshinobu Inoue #endif 3539df8bae1dSRodney W. Grimes 35408501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 35417a3244ccSRobert Watson 3542ef341ee1SGleb Smirnoff if (mtuoffer != -1) { 3543ef341ee1SGleb Smirnoff KASSERT(offer == -1, ("%s: conflict", __func__)); 3544ef341ee1SGleb Smirnoff offer = mtuoffer - min_protoh; 3545ef341ee1SGleb Smirnoff } 3546ef341ee1SGleb Smirnoff origoffer = offer; 3547ef341ee1SGleb Smirnoff 3548e8949f74SAndre Oppermann /* Initialize. */ 354997d8d152SAndre Oppermann #ifdef INET6 355097d8d152SAndre Oppermann if (isipv6) { 35513c914c54SAndre Oppermann maxmtu = tcp_maxmtu6(&inp->inp_inc, cap); 3552603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_v6mssdflt; 3553b287c6c7SBjoern A. Zeeb } 355497d8d152SAndre Oppermann #endif 3555b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3556b287c6c7SBjoern A. Zeeb else 3557b287c6c7SBjoern A. Zeeb #endif 3558b287c6c7SBjoern A. Zeeb #ifdef INET 355997d8d152SAndre Oppermann { 35603c914c54SAndre Oppermann maxmtu = tcp_maxmtu(&inp->inp_inc, cap); 3561603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_mssdflt; 3562df8bae1dSRodney W. Grimes } 3563b287c6c7SBjoern A. Zeeb #endif 3564df8bae1dSRodney W. Grimes 356597d8d152SAndre Oppermann /* 3566e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 356797d8d152SAndre Oppermann */ 35686f01cac6SBjoern A. Zeeb if (maxmtu == 0) { 35696f01cac6SBjoern A. Zeeb /* 35708e5c87f4SBjoern A. Zeeb * In case we return early we need to initialize metrics 35716f01cac6SBjoern A. Zeeb * to a defined state as tcp_hc_get() would do for us 35726f01cac6SBjoern A. Zeeb * if there was no cache hit. 35736f01cac6SBjoern A. Zeeb */ 35746f01cac6SBjoern A. Zeeb if (metricptr != NULL) 35756f01cac6SBjoern A. Zeeb bzero(metricptr, sizeof(struct hc_metrics_lite)); 357697d8d152SAndre Oppermann return; 35776f01cac6SBjoern A. Zeeb } 357897d8d152SAndre Oppermann 3579e8949f74SAndre Oppermann /* What have we got? */ 358097d8d152SAndre Oppermann switch (offer) { 358197d8d152SAndre Oppermann case 0: 358297d8d152SAndre Oppermann /* 358397d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 3584c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 3585c3b02504SBjoern A. Zeeb * already assigned to t_maxopd above. 358697d8d152SAndre Oppermann */ 3587c3b02504SBjoern A. Zeeb offer = tp->t_maxopd; 358897d8d152SAndre Oppermann break; 358997d8d152SAndre Oppermann 359097d8d152SAndre Oppermann case -1: 3591a0292f23SGarrett Wollman /* 3592c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 3593a0292f23SGarrett Wollman */ 359497d8d152SAndre Oppermann /* FALLTHROUGH */ 359597d8d152SAndre Oppermann 359697d8d152SAndre Oppermann default: 3597a0292f23SGarrett Wollman /* 359853369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 359953369ac9SAndre Oppermann * to at least minmss. 360053369ac9SAndre Oppermann */ 3601603724d3SBjoern A. Zeeb offer = max(offer, V_tcp_minmss); 360297d8d152SAndre Oppermann } 3603a0292f23SGarrett Wollman 3604df8bae1dSRodney W. Grimes /* 3605e8949f74SAndre Oppermann * rmx information is now retrieved from tcp_hostcache. 3606df8bae1dSRodney W. Grimes */ 360797d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 36083cee92e0SBjoern A. Zeeb if (metricptr != NULL) 36093cee92e0SBjoern A. Zeeb bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 361097d8d152SAndre Oppermann 3611df8bae1dSRodney W. Grimes /* 3612cc412412SHiren Panchasara * If there's a discovered mtu in tcp hostcache, use it. 3613cc412412SHiren Panchasara * Else, use the link mtu. 3614df8bae1dSRodney W. Grimes */ 361597d8d152SAndre Oppermann if (metrics.rmx_mtu) 36162d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3617c068736aSJeffrey Hsu else { 361831b3783cSHajimu UMEMOTO #ifdef INET6 3619fb59c426SYoshinobu Inoue if (isipv6) { 362097d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3621603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 362297d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 3623603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_v6mssdflt); 3624b287c6c7SBjoern A. Zeeb } 3625b3399803SHajimu UMEMOTO #endif 3626b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3627b287c6c7SBjoern A. Zeeb else 3628b287c6c7SBjoern A. Zeeb #endif 3629b287c6c7SBjoern A. Zeeb #ifdef INET 363097d8d152SAndre Oppermann { 363197d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3632603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 363397d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 3634603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_mssdflt); 3635a0292f23SGarrett Wollman } 3636b287c6c7SBjoern A. Zeeb #endif 36373cee92e0SBjoern A. Zeeb /* 36383cee92e0SBjoern A. Zeeb * XXX - The above conditional (mss = maxmtu - min_protoh) 36393cee92e0SBjoern A. Zeeb * probably violates the TCP spec. 36403cee92e0SBjoern A. Zeeb * The problem is that, since we don't know the 36413cee92e0SBjoern A. Zeeb * other end's MSS, we are supposed to use a conservative 36423cee92e0SBjoern A. Zeeb * default. But, if we do that, then MTU discovery will 36433cee92e0SBjoern A. Zeeb * never actually take place, because the conservative 36443cee92e0SBjoern A. Zeeb * default is much less than the MTUs typically seen 36453cee92e0SBjoern A. Zeeb * on the Internet today. For the moment, we'll sweep 36463cee92e0SBjoern A. Zeeb * this under the carpet. 36473cee92e0SBjoern A. Zeeb * 36483cee92e0SBjoern A. Zeeb * The conservative default might not actually be a problem 36493cee92e0SBjoern A. Zeeb * if the only case this occurs is when sending an initial 36503cee92e0SBjoern A. Zeeb * SYN with options and data to a host we've never talked 36513cee92e0SBjoern A. Zeeb * to before. Then, they will reply with an MSS value which 36523cee92e0SBjoern A. Zeeb * will get recorded and the new parameters should get 36533cee92e0SBjoern A. Zeeb * recomputed. For Further Study. 36543cee92e0SBjoern A. Zeeb */ 365597d8d152SAndre Oppermann } 3656a0292f23SGarrett Wollman mss = min(mss, offer); 365797d8d152SAndre Oppermann 3658a0292f23SGarrett Wollman /* 36593cee92e0SBjoern A. Zeeb * Sanity check: make sure that maxopd will be large 36603cee92e0SBjoern A. Zeeb * enough to allow some data on segments even if the 36613cee92e0SBjoern A. Zeeb * all the option space is used (40bytes). Otherwise 36623cee92e0SBjoern A. Zeeb * funny things may happen in tcp_output. 36633cee92e0SBjoern A. Zeeb */ 36643cee92e0SBjoern A. Zeeb mss = max(mss, 64); 36653cee92e0SBjoern A. Zeeb 36663cee92e0SBjoern A. Zeeb /* 3667a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 3668a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 3669a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 3670a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 3671a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 3672a0292f23SGarrett Wollman */ 3673a0292f23SGarrett Wollman tp->t_maxopd = mss; 3674a0292f23SGarrett Wollman 3675a0292f23SGarrett Wollman /* 3676e8949f74SAndre Oppermann * origoffer==-1 indicates that no segments were received yet. 3677c94c54e4SAndre Oppermann * In this case we just guess. 3678a0292f23SGarrett Wollman */ 3679a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 3680a0292f23SGarrett Wollman (origoffer == -1 || 3681a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 3682a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 3683a0292f23SGarrett Wollman 368497d8d152SAndre Oppermann tp->t_maxseg = mss; 36853cee92e0SBjoern A. Zeeb } 36863cee92e0SBjoern A. Zeeb 36873cee92e0SBjoern A. Zeeb void 36883cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer) 36893cee92e0SBjoern A. Zeeb { 3690dbc42409SLawrence Stewart int mss; 36913cee92e0SBjoern A. Zeeb u_long bufsize; 36923cee92e0SBjoern A. Zeeb struct inpcb *inp; 36933cee92e0SBjoern A. Zeeb struct socket *so; 36943cee92e0SBjoern A. Zeeb struct hc_metrics_lite metrics; 36953c914c54SAndre Oppermann struct tcp_ifcap cap; 3696dbc42409SLawrence Stewart 36973cee92e0SBjoern A. Zeeb KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 36983cee92e0SBjoern A. Zeeb 36993c914c54SAndre Oppermann bzero(&cap, sizeof(cap)); 37003c914c54SAndre Oppermann tcp_mss_update(tp, offer, -1, &metrics, &cap); 37013cee92e0SBjoern A. Zeeb 37023cee92e0SBjoern A. Zeeb mss = tp->t_maxseg; 37033cee92e0SBjoern A. Zeeb inp = tp->t_inpcb; 370497d8d152SAndre Oppermann 3705df8bae1dSRodney W. Grimes /* 370697d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 370797d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 370897d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 370997d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 371097d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3711df8bae1dSRodney W. Grimes */ 3712c3b02504SBjoern A. Zeeb so = inp->inp_socket; 37133f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 3714e233e2acSAndre Oppermann if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe) 371597d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 371697d8d152SAndre Oppermann else 3717df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3718df8bae1dSRodney W. Grimes if (bufsize < mss) 3719df8bae1dSRodney W. Grimes mss = bufsize; 3720df8bae1dSRodney W. Grimes else { 3721df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3722df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3723df8bae1dSRodney W. Grimes bufsize = sb_max; 372488c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 37253f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3726df8bae1dSRodney W. Grimes } 37273f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 3728df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 3729df8bae1dSRodney W. Grimes 37303f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3731e233e2acSAndre Oppermann if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe) 373297d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 373397d8d152SAndre Oppermann else 3734df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3735df8bae1dSRodney W. Grimes if (bufsize > mss) { 3736df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3737df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3738df8bae1dSRodney W. Grimes bufsize = sb_max; 373988c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 37403f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3741df8bae1dSRodney W. Grimes } 37423f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 374391d6cfa6SBjoern A. Zeeb 374491d6cfa6SBjoern A. Zeeb /* Check the interface for TSO capabilities. */ 37453c914c54SAndre Oppermann if (cap.ifcap & CSUM_TSO) { 374691d6cfa6SBjoern A. Zeeb tp->t_flags |= TF_TSO; 37473c914c54SAndre Oppermann tp->t_tsomax = cap.tsomax; 37489fd573c3SHans Petter Selasky tp->t_tsomaxsegcount = cap.tsomaxsegcount; 37499fd573c3SHans Petter Selasky tp->t_tsomaxsegsize = cap.tsomaxsegsize; 37503c914c54SAndre Oppermann } 3751a0292f23SGarrett Wollman } 3752a0292f23SGarrett Wollman 3753a0292f23SGarrett Wollman /* 3754a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3755a0292f23SGarrett Wollman */ 3756a0292f23SGarrett Wollman int 3757ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 3758a0292f23SGarrett Wollman { 375997d8d152SAndre Oppermann int mss = 0; 376097d8d152SAndre Oppermann u_long maxmtu = 0; 376197d8d152SAndre Oppermann u_long thcmtu = 0; 376297d8d152SAndre Oppermann size_t min_protoh; 3763a0292f23SGarrett Wollman 376497d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3765a0292f23SGarrett Wollman 376631b3783cSHajimu UMEMOTO #ifdef INET6 3767dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3768603724d3SBjoern A. Zeeb mss = V_tcp_v6mssdflt; 3769233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 377097d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 3771b287c6c7SBjoern A. Zeeb } 377231b3783cSHajimu UMEMOTO #endif 3773b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3774b287c6c7SBjoern A. Zeeb else 3775b287c6c7SBjoern A. Zeeb #endif 3776b287c6c7SBjoern A. Zeeb #ifdef INET 377797d8d152SAndre Oppermann { 3778603724d3SBjoern A. Zeeb mss = V_tcp_mssdflt; 3779233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 378097d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 378197d8d152SAndre Oppermann } 3782b287c6c7SBjoern A. Zeeb #endif 37833a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET) 37843a9391deSBjoern A. Zeeb thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 37853a9391deSBjoern A. Zeeb #endif 37863a9391deSBjoern A. Zeeb 378797d8d152SAndre Oppermann if (maxmtu && thcmtu) 378897d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 378997d8d152SAndre Oppermann else if (maxmtu || thcmtu) 379097d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 379197d8d152SAndre Oppermann 379297d8d152SAndre Oppermann return (mss); 3793df8bae1dSRodney W. Grimes } 379446f58482SJonathan Lemon 379546f58482SJonathan Lemon 379646f58482SJonathan Lemon /* 3797c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3798c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3799c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3800c068736aSJeffrey Hsu * be started again. 380146f58482SJonathan Lemon */ 380255bceb1eSRandall Stewart void 3803ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 380446f58482SJonathan Lemon { 380546f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 380646f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 380746f58482SJonathan Lemon 38088501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 38097a3244ccSRobert Watson 3810b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 381146f58482SJonathan Lemon tp->t_rtttime = 0; 381246f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 38136b2a5f92SJayanth Vijayaraghavan /* 3814c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3815c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 38166b2a5f92SJayanth Vijayaraghavan */ 3817dbc42409SLawrence Stewart tp->snd_cwnd = tp->t_maxseg + BYTES_THIS_ACK(tp, th); 3818a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 381955bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 382046f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 382146f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 382246f58482SJonathan Lemon tp->snd_nxt = onxt; 382346f58482SJonathan Lemon /* 382446f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 382546f58482SJonathan Lemon * not updated yet. 382646f58482SJonathan Lemon */ 3827dbc42409SLawrence Stewart if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th)) 3828dbc42409SLawrence Stewart tp->snd_cwnd -= BYTES_THIS_ACK(tp, th); 3829d7587117SPaul Saab else 3830d7587117SPaul Saab tp->snd_cwnd = 0; 3831d7587117SPaul Saab tp->snd_cwnd += tp->t_maxseg; 383246f58482SJonathan Lemon } 383312eeb81fSHiren Panchasara 383412eeb81fSHiren Panchasara int 383512eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp) 383612eeb81fSHiren Panchasara { 383712eeb81fSHiren Panchasara return (tp->snd_max - tp->snd_una + 383812eeb81fSHiren Panchasara tp->sackhint.sack_bytes_rexmit - 383912eeb81fSHiren Panchasara tp->sackhint.sacked_bytes); 384012eeb81fSHiren Panchasara } 3841