1c398230bSWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4e79adb8eSGarrett Wollman * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6dbc42409SLawrence Stewart * Copyright (c) 2007-2008,2010 7dbc42409SLawrence Stewart * Swinburne University of Technology, Melbourne, Australia. 8dbc42409SLawrence Stewart * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org> 9dbc42409SLawrence Stewart * Copyright (c) 2010 The FreeBSD Foundation 10fa046d87SRobert Watson * Copyright (c) 2010-2011 Juniper Networks, Inc. 11dbc42409SLawrence Stewart * All rights reserved. 12dbc42409SLawrence Stewart * 13dbc42409SLawrence Stewart * Portions of this software were developed at the Centre for Advanced Internet 14891b8ed4SLawrence Stewart * Architectures, Swinburne University of Technology, by Lawrence Stewart, 15891b8ed4SLawrence Stewart * James Healy and David Hayes, made possible in part by a grant from the Cisco 16891b8ed4SLawrence Stewart * University Research Program Fund at Community Foundation Silicon Valley. 17dbc42409SLawrence Stewart * 18dbc42409SLawrence Stewart * Portions of this software were developed at the Centre for Advanced 19dbc42409SLawrence Stewart * Internet Architectures, Swinburne University of Technology, Melbourne, 20dbc42409SLawrence Stewart * Australia by David Hayes under sponsorship from the FreeBSD Foundation. 21df8bae1dSRodney W. Grimes * 22fa046d87SRobert Watson * Portions of this software were developed by Robert N. M. Watson under 23fa046d87SRobert Watson * contract to Juniper Networks, Inc. 24fa046d87SRobert Watson * 25df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 26df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 27df8bae1dSRodney W. Grimes * are met: 28df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 29df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 30df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 31df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 32df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 33fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 34df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 35df8bae1dSRodney W. Grimes * without specific prior written permission. 36df8bae1dSRodney W. Grimes * 37df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 38df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 39df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 40df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 41df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 42df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 43df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 44df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 45df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 46df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 47df8bae1dSRodney W. Grimes * SUCH DAMAGE. 48df8bae1dSRodney W. Grimes * 49e79adb8eSGarrett Wollman * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 50df8bae1dSRodney W. Grimes */ 51df8bae1dSRodney W. Grimes 524b421e2dSMike Silbersack #include <sys/cdefs.h> 534b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 544b421e2dSMike Silbersack 551cfd4b53SBruce M Simpson #include "opt_inet.h" 56fb59c426SYoshinobu Inoue #include "opt_inet6.h" 573a2a9f79SYoshinobu Inoue #include "opt_ipsec.h" 580cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 590cc12cc5SJoerg Wunsch 60df8bae1dSRodney W. Grimes #include <sys/param.h> 6198163b98SPoul-Henning Kamp #include <sys/kernel.h> 62bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 6339bc9de5SLawrence Stewart #include <sys/hhook.h> 64bd79708dSJonathan T. Looney #endif 65df8bae1dSRodney W. Grimes #include <sys/malloc.h> 66df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 67a29f300eSGarrett Wollman #include <sys/proc.h> /* for proc0 declaration */ 68df8bae1dSRodney W. Grimes #include <sys/protosw.h> 6957f60867SMark Johnston #include <sys/sdt.h> 70960ed29cSSeigo Tanimura #include <sys/signalvar.h> 71df8bae1dSRodney W. Grimes #include <sys/socket.h> 72df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 73960ed29cSSeigo Tanimura #include <sys/sysctl.h> 74816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 75960ed29cSSeigo Tanimura #include <sys/systm.h> 76df8bae1dSRodney W. Grimes 77e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 78e79adb8eSGarrett Wollman 7912e2e970SAndre Oppermann #include <vm/uma.h> 8012e2e970SAndre Oppermann 81df8bae1dSRodney W. Grimes #include <net/if.h> 8276039bc8SGleb Smirnoff #include <net/if_var.h> 83df8bae1dSRodney W. Grimes #include <net/route.h> 84530c0060SRobert Watson #include <net/vnet.h> 85df8bae1dSRodney W. Grimes 86773673c1SAndre Oppermann #define TCPSTATES /* for logging */ 87773673c1SAndre Oppermann 88df8bae1dSRodney W. Grimes #include <netinet/in.h> 8957f60867SMark Johnston #include <netinet/in_kdtrace.h> 90960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 91df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 92df8bae1dSRodney W. Grimes #include <netinet/ip.h> 938e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 94686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 95df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 96ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 97686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 98686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h> 99686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 100b66d74c1SGleb Smirnoff #include <netinet6/in6_var.h> 101960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h> 102960ed29cSSeigo Tanimura #include <netinet6/nd6.h> 1032de3e790SGleb Smirnoff #include <netinet/tcp.h> 104df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 1052529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h> 106df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 107df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 108df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 109fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 110df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 1114644fda3SGleb Smirnoff #include <netinet/cc/cc.h> 112c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h> 11386a996e6SHiren Panchasara #ifdef TCPPCAP 11486a996e6SHiren Panchasara #include <netinet/tcp_pcap.h> 11586a996e6SHiren Panchasara #endif 116c325962bSMike Silbersack #include <netinet/tcp_syncache.h> 117610ee2f9SDavid Greenman #ifdef TCPDEBUG 118df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 119fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */ 12009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 12109fe6320SNavdeep Parhar #include <netinet/tcp_offload.h> 12209fe6320SNavdeep Parhar #endif 123fb59c426SYoshinobu Inoue 124fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 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; 1496df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW, 150eddfbb76SRobert Watson &VNET_NAME(drop_synfin), 0, 151eddfbb76SRobert Watson "Drop TCP packets with SYN+FIN set"); 152f8613305SDag-Erling Smørgrav 153b72e56e7SMichael Tuexen VNET_DEFINE(int, tcp_do_newcwv) = 0; 154b72e56e7SMichael Tuexen SYSCTL_INT(_net_inet_tcp, OID_AUTO, newcwv, CTLFLAG_VNET | CTLFLAG_RW, 155b72e56e7SMichael Tuexen &VNET_NAME(tcp_do_newcwv), 0, 156b72e56e7SMichael Tuexen "Enable New Congestion Window Validation per RFC7661"); 157b72e56e7SMichael Tuexen 15812eeb81fSHiren Panchasara VNET_DEFINE(int, tcp_do_rfc6675_pipe) = 0; 159054d38e3SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc6675_pipe, CTLFLAG_VNET | CTLFLAG_RW, 16012eeb81fSHiren Panchasara &VNET_NAME(tcp_do_rfc6675_pipe), 0, 16112eeb81fSHiren Panchasara "Use calculated pipe/in-flight bytes per RFC 6675"); 16212eeb81fSHiren Panchasara 16382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1; 1646df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW, 165eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3042), 0, 166eddfbb76SRobert Watson "Enable RFC 3042 (Limited Transmit)"); 167582a954bSJeffrey Hsu 16882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1; 1696df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW, 170eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3390), 0, 171da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 172da3a8a1aSJeffrey Hsu 173356c7958SHiren Panchasara VNET_DEFINE(int, tcp_initcwnd_segments) = 10; 174356c7958SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments, 175356c7958SHiren Panchasara CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0, 176356c7958SHiren Panchasara "Slow-start flight size (initial congestion window) in number of segments"); 17709440655SAndre Oppermann 17882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1; 1796df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW, 180eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3465), 0, 18124cb0f22SLawrence Stewart "Enable RFC 3465 (Appropriate Byte Counting)"); 182eddfbb76SRobert Watson 18382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2; 1846df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW, 185eddfbb76SRobert Watson &VNET_NAME(tcp_abc_l_var), 2, 18624cb0f22SLawrence Stewart "Cap the max cwnd increment during slow-start to this number of segments"); 18724cb0f22SLawrence Stewart 1886472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN"); 189f2512ba1SRui Paulo 190883054b4SDon Lewis VNET_DEFINE(int, tcp_do_ecn) = 2; 1916df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW, 192eddfbb76SRobert Watson &VNET_NAME(tcp_do_ecn), 0, 193eddfbb76SRobert Watson "TCP ECN support"); 194eddfbb76SRobert Watson 19582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1; 1966df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_VNET | CTLFLAG_RW, 197eddfbb76SRobert Watson &VNET_NAME(tcp_ecn_maxretries), 0, 198eddfbb76SRobert Watson "Max retries before giving up on ECN"); 199eddfbb76SRobert Watson 2003220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0; 2016df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW, 2023220a212SGleb Smirnoff &VNET_NAME(tcp_insecure_syn), 0, 2033220a212SGleb Smirnoff "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets"); 2043220a212SGleb Smirnoff 20582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0; 2066df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW, 207eddfbb76SRobert Watson &VNET_NAME(tcp_insecure_rst), 0, 2083220a212SGleb Smirnoff "Follow RFC793 instead of RFC5961 criteria for accepting RST packets"); 209a69968eeSMike Silbersack 210fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64; 211873789cbSAndre Oppermann #define V_tcp_recvspace VNET(tcp_recvspace) 2126df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW, 213873789cbSAndre Oppermann &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size"); 214873789cbSAndre Oppermann 21582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1; 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 220b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024; 2216df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW, 222eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_max), 0, 2238b615593SMarko Zec "Max size of automatic receive buffer"); 2246741ecf5SAndre Oppermann 225eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb); 22644e33a07SMarko Zec #define tcb6 tcb /* for KAME src sync over BSD*'s */ 22782cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo); 228df8bae1dSRodney W. Grimes 229315e3e38SRobert Watson /* 230bf840a17SGleb Smirnoff * TCP statistics are stored in an array of counter(9)s, which size matches 231bf840a17SGleb Smirnoff * size of struct tcpstat. TCP running connection count is a regular array. 2325923c293SGleb Smirnoff */ 2335da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat); 2345da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat, 2355da0521fSAndrey V. Elsukov tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 236bf840a17SGleb Smirnoff VNET_DEFINE(counter_u64_t, tcps_states[TCP_NSTATES]); 237bf840a17SGleb Smirnoff SYSCTL_COUNTER_U64_ARRAY(_net_inet_tcp, TCPCTL_STATES, states, CTLFLAG_RD | 238d4d32b9fSHans Petter Selasky CTLFLAG_VNET, &VNET_NAME(tcps_states)[0], TCP_NSTATES, 239bf840a17SGleb Smirnoff "TCP connection counts by TCP state"); 240bf840a17SGleb Smirnoff 241bf840a17SGleb Smirnoff static void 242bf840a17SGleb Smirnoff tcp_vnet_init(const void *unused) 243bf840a17SGleb Smirnoff { 244bf840a17SGleb Smirnoff 245f59d975eSGleb Smirnoff COUNTER_ARRAY_ALLOC(V_tcps_states, TCP_NSTATES, M_WAITOK); 246bf840a17SGleb Smirnoff VNET_PCPUSTAT_ALLOC(tcpstat, M_WAITOK); 247bf840a17SGleb Smirnoff } 248bf840a17SGleb Smirnoff VNET_SYSINIT(tcp_vnet_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 249bf840a17SGleb Smirnoff tcp_vnet_init, NULL); 2505923c293SGleb Smirnoff 2515923c293SGleb Smirnoff #ifdef VIMAGE 252bf840a17SGleb Smirnoff static void 253bf840a17SGleb Smirnoff tcp_vnet_uninit(const void *unused) 254bf840a17SGleb Smirnoff { 255bf840a17SGleb Smirnoff 256f59d975eSGleb Smirnoff COUNTER_ARRAY_FREE(V_tcps_states, TCP_NSTATES); 257bf840a17SGleb Smirnoff VNET_PCPUSTAT_FREE(tcpstat); 258bf840a17SGleb Smirnoff } 259bf840a17SGleb Smirnoff VNET_SYSUNINIT(tcp_vnet_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 260bf840a17SGleb Smirnoff tcp_vnet_uninit, NULL); 2615923c293SGleb Smirnoff #endif /* VIMAGE */ 262bf840a17SGleb Smirnoff 2635923c293SGleb Smirnoff /* 264315e3e38SRobert Watson * Kernel module interface for updating tcpstat. The argument is an index 2655923c293SGleb Smirnoff * into tcpstat treated as an array. 266315e3e38SRobert Watson */ 267315e3e38SRobert Watson void 268315e3e38SRobert Watson kmod_tcpstat_inc(int statnum) 269315e3e38SRobert Watson { 270315e3e38SRobert Watson 2715da0521fSAndrey V. Elsukov counter_u64_add(VNET(tcpstat)[statnum], 1); 272315e3e38SRobert Watson } 273315e3e38SRobert Watson 274bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 275dbc42409SLawrence Stewart /* 27639bc9de5SLawrence Stewart * Wrapper for the TCP established input helper hook. 27739bc9de5SLawrence Stewart */ 27855bceb1eSRandall Stewart void 27939bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to) 28039bc9de5SLawrence Stewart { 28139bc9de5SLawrence Stewart struct tcp_hhook_data hhook_data; 28239bc9de5SLawrence Stewart 28339bc9de5SLawrence Stewart if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) { 28439bc9de5SLawrence Stewart hhook_data.tp = tp; 28539bc9de5SLawrence Stewart hhook_data.th = th; 28639bc9de5SLawrence Stewart hhook_data.to = to; 28739bc9de5SLawrence Stewart 28839bc9de5SLawrence Stewart hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data, 28939bc9de5SLawrence Stewart tp->osd); 29039bc9de5SLawrence Stewart } 29139bc9de5SLawrence Stewart } 292bd79708dSJonathan T. Looney #endif 29339bc9de5SLawrence Stewart 29439bc9de5SLawrence Stewart /* 295dbc42409SLawrence Stewart * CC wrapper hook functions 296dbc42409SLawrence Stewart */ 29755bceb1eSRandall Stewart void 2984b7b743cSLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t nsegs, 2994b7b743cSLawrence Stewart uint16_t type) 300f2512ba1SRui Paulo { 301dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 302f2512ba1SRui Paulo 3034b7b743cSLawrence Stewart tp->ccv->nsegs = nsegs; 304dbc42409SLawrence Stewart tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th); 305b72e56e7SMichael Tuexen if ((!V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd)) || 306b72e56e7SMichael Tuexen (V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd) && 307b72e56e7SMichael Tuexen (tp->snd_cwnd < (tcp_compute_pipe(tp) * 2)))) 308dbc42409SLawrence Stewart tp->ccv->flags |= CCF_CWND_LIMITED; 309dbc42409SLawrence Stewart else 310dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_CWND_LIMITED; 311dbc42409SLawrence Stewart 312dbc42409SLawrence Stewart if (type == CC_ACK) { 313dbc42409SLawrence Stewart if (tp->snd_cwnd > tp->snd_ssthresh) { 314dbc42409SLawrence Stewart tp->t_bytes_acked += min(tp->ccv->bytes_this_ack, 3154b7b743cSLawrence Stewart nsegs * V_tcp_abc_l_var * tcp_maxseg(tp)); 316dbc42409SLawrence Stewart if (tp->t_bytes_acked >= tp->snd_cwnd) { 317dbc42409SLawrence Stewart tp->t_bytes_acked -= tp->snd_cwnd; 318dbc42409SLawrence Stewart tp->ccv->flags |= CCF_ABC_SENTAWND; 319dbc42409SLawrence Stewart } 320dbc42409SLawrence Stewart } else { 321dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_ABC_SENTAWND; 322dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 323dbc42409SLawrence Stewart } 324dbc42409SLawrence Stewart } 325dbc42409SLawrence Stewart 326dbc42409SLawrence Stewart if (CC_ALGO(tp)->ack_received != NULL) { 327dbc42409SLawrence Stewart /* XXXLAS: Find a way to live without this */ 328dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 329dbc42409SLawrence Stewart CC_ALGO(tp)->ack_received(tp->ccv, type); 330dbc42409SLawrence Stewart } 331dbc42409SLawrence Stewart } 332dbc42409SLawrence Stewart 33355bceb1eSRandall Stewart void 334dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp) 335dbc42409SLawrence Stewart { 336dbc42409SLawrence Stewart struct hc_metrics_lite metrics; 337dbc42409SLawrence Stewart struct inpcb *inp = tp->t_inpcb; 3380c39d38dSGleb Smirnoff u_int maxseg; 339dbc42409SLawrence Stewart int rtt; 340dbc42409SLawrence Stewart 341dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 342dbc42409SLawrence Stewart 343dbc42409SLawrence Stewart tcp_hc_get(&inp->inp_inc, &metrics); 3440c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 345dbc42409SLawrence Stewart 346dbc42409SLawrence Stewart if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 347dbc42409SLawrence Stewart tp->t_srtt = rtt; 348dbc42409SLawrence Stewart tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 349dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrtt); 350dbc42409SLawrence Stewart if (metrics.rmx_rttvar) { 351dbc42409SLawrence Stewart tp->t_rttvar = metrics.rmx_rttvar; 352dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrttvar); 353dbc42409SLawrence Stewart } else { 354dbc42409SLawrence Stewart /* default variation is +- 1 rtt */ 355dbc42409SLawrence Stewart tp->t_rttvar = 356dbc42409SLawrence Stewart tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 357dbc42409SLawrence Stewart } 358dbc42409SLawrence Stewart TCPT_RANGESET(tp->t_rxtcur, 359dbc42409SLawrence Stewart ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 360dbc42409SLawrence Stewart tp->t_rttmin, TCPTV_REXMTMAX); 361dbc42409SLawrence Stewart } 362dbc42409SLawrence Stewart if (metrics.rmx_ssthresh) { 363dbc42409SLawrence Stewart /* 364dbc42409SLawrence Stewart * There's some sort of gateway or interface 365dbc42409SLawrence Stewart * buffer limit on the path. Use this to set 366a4641f4eSPedro F. Giffuni * the slow start threshold, but set the 367dbc42409SLawrence Stewart * threshold to no less than 2*mss. 368dbc42409SLawrence Stewart */ 3690c39d38dSGleb Smirnoff tp->snd_ssthresh = max(2 * maxseg, metrics.rmx_ssthresh); 370dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedssthresh); 371dbc42409SLawrence Stewart } 372dbc42409SLawrence Stewart 373dbc42409SLawrence Stewart /* 3749ec4a4ccSAndre Oppermann * Set the initial slow-start flight size. 375dbc42409SLawrence Stewart * 376cf8f04f4SAndre Oppermann * If a SYN or SYN/ACK was lost and retransmitted, we have to 377cf8f04f4SAndre Oppermann * reduce the initial CWND to one segment as congestion is likely 378cf8f04f4SAndre Oppermann * requiring us to be cautious. 379dbc42409SLawrence Stewart */ 380cf8f04f4SAndre Oppermann if (tp->snd_cwnd == 1) 3810c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; /* SYN(-ACK) lost */ 382dbc42409SLawrence Stewart else 3837dc90a1dSMichael Tuexen tp->snd_cwnd = tcp_compute_initwnd(maxseg); 384dbc42409SLawrence Stewart 385dbc42409SLawrence Stewart if (CC_ALGO(tp)->conn_init != NULL) 386dbc42409SLawrence Stewart CC_ALGO(tp)->conn_init(tp->ccv); 387dbc42409SLawrence Stewart } 388dbc42409SLawrence Stewart 389dbc42409SLawrence Stewart void inline 390dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type) 391dbc42409SLawrence Stewart { 3920c39d38dSGleb Smirnoff u_int maxseg; 3930c39d38dSGleb Smirnoff 394dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 395dbc42409SLawrence Stewart 396dbc42409SLawrence Stewart switch(type) { 397dbc42409SLawrence Stewart case CC_NDUPACK: 398dbc42409SLawrence Stewart if (!IN_FASTRECOVERY(tp->t_flags)) { 399f2512ba1SRui Paulo tp->snd_recover = tp->snd_max; 400*3cf38784SMichael Tuexen if (tp->t_flags2 & TF2_ECN_PERMIT) 401*3cf38784SMichael Tuexen tp->t_flags2 |= TF2_ECN_SND_CWR; 402f2512ba1SRui Paulo } 403dbc42409SLawrence Stewart break; 404dbc42409SLawrence Stewart case CC_ECN: 405dbc42409SLawrence Stewart if (!IN_CONGRECOVERY(tp->t_flags)) { 406dbc42409SLawrence Stewart TCPSTAT_INC(tcps_ecn_rcwnd); 407dbc42409SLawrence Stewart tp->snd_recover = tp->snd_max; 408*3cf38784SMichael Tuexen if (tp->t_flags2 & TF2_ECN_PERMIT) 409*3cf38784SMichael Tuexen tp->t_flags2 |= TF2_ECN_SND_CWR; 410dbc42409SLawrence Stewart } 411dbc42409SLawrence Stewart break; 412dbc42409SLawrence Stewart case CC_RTO: 4130c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 414dbc42409SLawrence Stewart tp->t_dupacks = 0; 415dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 416dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 41743053c12SSean Bruno tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 / 41843053c12SSean Bruno maxseg) * maxseg; 4190c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 420dbc42409SLawrence Stewart break; 421dbc42409SLawrence Stewart case CC_RTO_ERR: 422dbc42409SLawrence Stewart TCPSTAT_INC(tcps_sndrexmitbad); 423dbc42409SLawrence Stewart /* RTO was unnecessary, so reset everything. */ 424dbc42409SLawrence Stewart tp->snd_cwnd = tp->snd_cwnd_prev; 425dbc42409SLawrence Stewart tp->snd_ssthresh = tp->snd_ssthresh_prev; 426dbc42409SLawrence Stewart tp->snd_recover = tp->snd_recover_prev; 427dbc42409SLawrence Stewart if (tp->t_flags & TF_WASFRECOVERY) 428dbc42409SLawrence Stewart ENTER_FASTRECOVERY(tp->t_flags); 429dbc42409SLawrence Stewart if (tp->t_flags & TF_WASCRECOVERY) 430dbc42409SLawrence Stewart ENTER_CONGRECOVERY(tp->t_flags); 431dbc42409SLawrence Stewart tp->snd_nxt = tp->snd_max; 432672dc4aeSJohn Baldwin tp->t_flags &= ~TF_PREVVALID; 433dbc42409SLawrence Stewart tp->t_badrxtwin = 0; 434dbc42409SLawrence Stewart break; 435dbc42409SLawrence Stewart } 436dbc42409SLawrence Stewart 437dbc42409SLawrence Stewart if (CC_ALGO(tp)->cong_signal != NULL) { 438dbc42409SLawrence Stewart if (th != NULL) 439dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 440dbc42409SLawrence Stewart CC_ALGO(tp)->cong_signal(tp->ccv, type); 441dbc42409SLawrence Stewart } 442dbc42409SLawrence Stewart } 443dbc42409SLawrence Stewart 44455bceb1eSRandall Stewart void inline 445dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th) 446dbc42409SLawrence Stewart { 447dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 448dbc42409SLawrence Stewart 449dbc42409SLawrence Stewart /* XXXLAS: KASSERT that we're in recovery? */ 450dbc42409SLawrence Stewart 451dbc42409SLawrence Stewart if (CC_ALGO(tp)->post_recovery != NULL) { 452dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 453dbc42409SLawrence Stewart CC_ALGO(tp)->post_recovery(tp->ccv); 454dbc42409SLawrence Stewart } 455dbc42409SLawrence Stewart /* XXXLAS: EXIT_RECOVERY ? */ 456dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 457dbc42409SLawrence Stewart } 4580312fbe9SPoul-Henning Kamp 459df8bae1dSRodney W. Grimes /* 460262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 46185536381SHiren Panchasara * following conditions are met: 46285536381SHiren Panchasara * - There is no delayed ack timer in progress. 46385536381SHiren Panchasara * - Our last ack wasn't a 0-sized window. We never want to delay 46485536381SHiren Panchasara * the ack that opens up a 0-sized window. 46585536381SHiren Panchasara * - LRO wasn't used for this segment. We make sure by checking that the 46685536381SHiren Panchasara * segment size is not larger than the MSS. 467d8c85a26SJonathan Lemon */ 468c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen) \ 469b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 470a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 4710c39d38dSGleb Smirnoff (tlen <= tp->t_maxseg) && \ 472603724d3SBjoern A. Zeeb (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 473d8c85a26SJonathan Lemon 47464807b30SHiren Panchasara static void inline 47564807b30SHiren Panchasara cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos) 47664807b30SHiren Panchasara { 47764807b30SHiren Panchasara INP_WLOCK_ASSERT(tp->t_inpcb); 47864807b30SHiren Panchasara 47964807b30SHiren Panchasara if (CC_ALGO(tp)->ecnpkt_handler != NULL) { 48064807b30SHiren Panchasara switch (iptos & IPTOS_ECN_MASK) { 48164807b30SHiren Panchasara case IPTOS_ECN_CE: 48264807b30SHiren Panchasara tp->ccv->flags |= CCF_IPHDR_CE; 48364807b30SHiren Panchasara break; 48464807b30SHiren Panchasara case IPTOS_ECN_ECT0: 48564807b30SHiren Panchasara tp->ccv->flags &= ~CCF_IPHDR_CE; 48664807b30SHiren Panchasara break; 48764807b30SHiren Panchasara case IPTOS_ECN_ECT1: 48864807b30SHiren Panchasara tp->ccv->flags &= ~CCF_IPHDR_CE; 48964807b30SHiren Panchasara break; 49064807b30SHiren Panchasara } 49164807b30SHiren Panchasara 49264807b30SHiren Panchasara if (th->th_flags & TH_CWR) 49364807b30SHiren Panchasara tp->ccv->flags |= CCF_TCPHDR_CWR; 49464807b30SHiren Panchasara else 49564807b30SHiren Panchasara tp->ccv->flags &= ~CCF_TCPHDR_CWR; 49664807b30SHiren Panchasara 49764807b30SHiren Panchasara if (tp->t_flags & TF_DELACK) 49864807b30SHiren Panchasara tp->ccv->flags |= CCF_DELACK; 49964807b30SHiren Panchasara else 50064807b30SHiren Panchasara tp->ccv->flags &= ~CCF_DELACK; 50164807b30SHiren Panchasara 50264807b30SHiren Panchasara CC_ALGO(tp)->ecnpkt_handler(tp->ccv); 50364807b30SHiren Panchasara 50464807b30SHiren Panchasara if (tp->ccv->flags & CCF_ACKNOW) 50564807b30SHiren Panchasara tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 50664807b30SHiren Panchasara } 50764807b30SHiren Panchasara } 50864807b30SHiren Panchasara 509df8bae1dSRodney W. Grimes /* 5108d573cc1SAndre Oppermann * TCP input handling is split into multiple parts: 5118d573cc1SAndre Oppermann * tcp6_input is a thin wrapper around tcp_input for the extended 5128d573cc1SAndre Oppermann * ip6_protox[] call format in ip6_input 5138d573cc1SAndre Oppermann * tcp_input handles primary segment validation, inpcb lookup and 5148d573cc1SAndre Oppermann * SYN processing on listen sockets 5158d573cc1SAndre Oppermann * tcp_do_segment processes the ACK and text of the segment for 5168d573cc1SAndre Oppermann * establishing, established and closing connections 517df8bae1dSRodney W. Grimes */ 518fb59c426SYoshinobu Inoue #ifdef INET6 519fb59c426SYoshinobu Inoue int 520ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto) 521fb59c426SYoshinobu Inoue { 522503f4e47SBjoern A. Zeeb struct mbuf *m; 52333841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 5243e88eb90SAndrey V. Elsukov struct ip6_hdr *ip6; 525fb59c426SYoshinobu Inoue 526503f4e47SBjoern A. Zeeb m = *mp; 527a4adf6ccSBjoern A. Zeeb if (m->m_len < *offp + sizeof(struct tcphdr)) { 5284e619b17SBjoern A. Zeeb m = m_pullup(m, *offp + sizeof(struct tcphdr)); 5294e619b17SBjoern A. Zeeb if (m == NULL) { 5304e619b17SBjoern A. Zeeb *mp = m; 5314e619b17SBjoern A. Zeeb TCPSTAT_INC(tcps_rcvshort); 5324e619b17SBjoern A. Zeeb return (IPPROTO_DONE); 5334e619b17SBjoern A. Zeeb } 534a4adf6ccSBjoern A. Zeeb } 535fb59c426SYoshinobu Inoue 536fb59c426SYoshinobu Inoue /* 537fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 538fb59c426SYoshinobu Inoue * better place to put this in? 539fb59c426SYoshinobu Inoue */ 5403e88eb90SAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 5413e88eb90SAndrey V. Elsukov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */); 54233841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 543fb59c426SYoshinobu Inoue 5448c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 545fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 546fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 547a8fe77d8SBjoern A. Zeeb *mp = NULL; 5488f5a8818SKevin Lo return (IPPROTO_DONE); 549fb59c426SYoshinobu Inoue } 55077d396fdSMaksim Yevmenkin if (ia6) 55177d396fdSMaksim Yevmenkin ifa_free(&ia6->ia_ifa); 552fb59c426SYoshinobu Inoue 553a8fe77d8SBjoern A. Zeeb *mp = m; 5548f5a8818SKevin Lo return (tcp_input(mp, offp, proto)); 555fb59c426SYoshinobu Inoue } 556b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 557fb59c426SYoshinobu Inoue 5588f5a8818SKevin Lo int 5598f5a8818SKevin Lo tcp_input(struct mbuf **mp, int *offp, int proto) 560df8bae1dSRodney W. Grimes { 5618f5a8818SKevin Lo struct mbuf *m = *mp; 562b287c6c7SBjoern A. Zeeb struct tcphdr *th = NULL; 563ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 564ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 565302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 566302ce8d6SAndre Oppermann struct socket *so = NULL; 567e79adb8eSGarrett Wollman u_char *optp = NULL; 5688f5a8818SKevin Lo int off0; 56926f9a767SRodney W. Grimes int optlen = 0; 570b287c6c7SBjoern A. Zeeb #ifdef INET 571b287c6c7SBjoern A. Zeeb int len; 572c4556b2fSAndrey V. Elsukov uint8_t ipttl; 573b287c6c7SBjoern A. Zeeb #endif 574b287c6c7SBjoern A. Zeeb int tlen = 0, off; 575fb59c426SYoshinobu Inoue int drop_hdrlen; 576ad3f9ab3SAndre Oppermann int thflags; 577302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 5781d7ee746SKurt Lidl uint8_t iptos; 579c1de64a4SAndrey V. Elsukov struct m_tag *fwd_tag = NULL; 580c068736aSJeffrey Hsu #ifdef INET6 581302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 582c068736aSJeffrey Hsu int isipv6; 583c068736aSJeffrey Hsu #else 584a160e630SAndre Oppermann const void *ip6 = NULL; 585b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 586302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 587a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 588610ee2f9SDavid Greenman #ifdef TCPDEBUG 589c068736aSJeffrey Hsu /* 590c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 591c068736aSJeffrey Hsu * now IPv6. 592c068736aSJeffrey Hsu */ 59314739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 594410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 595610ee2f9SDavid Greenman short ostate = 0; 596610ee2f9SDavid Greenman #endif 597c068736aSJeffrey Hsu 598d40c0d47SGleb Smirnoff NET_EPOCH_ASSERT(); 599d40c0d47SGleb Smirnoff 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) { 61245747ba5SBjoern A. Zeeb 613fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 61445747ba5SBjoern A. Zeeb th = (struct tcphdr *)((caddr_t)ip6 + off0); 615fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 616356ab07eSBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) { 61745747ba5SBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 61845747ba5SBjoern A. Zeeb th->th_sum = m->m_pkthdr.csum_data; 61945747ba5SBjoern A. Zeeb else 62045747ba5SBjoern A. Zeeb th->th_sum = in6_cksum_pseudo(ip6, tlen, 62145747ba5SBjoern A. Zeeb IPPROTO_TCP, m->m_pkthdr.csum_data); 62245747ba5SBjoern A. Zeeb th->th_sum ^= 0xffff; 62345747ba5SBjoern A. Zeeb } else 62445747ba5SBjoern A. Zeeb th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen); 62545747ba5SBjoern A. Zeeb if (th->th_sum) { 62678b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 627fb59c426SYoshinobu Inoue goto drop; 628fb59c426SYoshinobu Inoue } 62933841545SHajimu UMEMOTO 63033841545SHajimu UMEMOTO /* 63133841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 63233841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 63333841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 63433841545SHajimu UMEMOTO * 63533841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 63633841545SHajimu UMEMOTO * already dropped in ip6_input. 63733841545SHajimu UMEMOTO */ 63833841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 63933841545SHajimu UMEMOTO /* XXX stat */ 64033841545SHajimu UMEMOTO goto drop; 64133841545SHajimu UMEMOTO } 6421d7ee746SKurt Lidl iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 643b287c6c7SBjoern A. Zeeb } 64404d3a452SHajimu UMEMOTO #endif 645b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 646b287c6c7SBjoern A. Zeeb else 647b287c6c7SBjoern A. Zeeb #endif 648b287c6c7SBjoern A. Zeeb #ifdef INET 649b287c6c7SBjoern A. Zeeb { 650df8bae1dSRodney W. Grimes /* 651df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 652df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 653df8bae1dSRodney W. Grimes */ 654fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 655105bd211SGleb Smirnoff ip_stripoptions(m); 656fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 657fb59c426SYoshinobu Inoue } 658df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 6594dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 6604dfdffe9SAndre Oppermann == NULL) { 66178b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 6628f5a8818SKevin Lo return (IPPROTO_DONE); 663df8bae1dSRodney W. Grimes } 664df8bae1dSRodney W. Grimes } 665fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 666db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 6678ad458a4SGleb Smirnoff tlen = ntohs(ip->ip_len) - off0; 668df8bae1dSRodney W. Grimes 6691d7ee746SKurt Lidl iptos = ip->ip_tos; 670db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 671db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 672db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 673db4f9cc7SJonathan Lemon else 674db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 675c068736aSJeffrey Hsu ip->ip_dst.s_addr, 6768f134647SGleb Smirnoff htonl(m->m_pkthdr.csum_data + tlen + 677c068736aSJeffrey Hsu IPPROTO_TCP)); 678db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 679db4f9cc7SJonathan Lemon } else { 6808f134647SGleb Smirnoff struct ipovly *ipov = (struct ipovly *)ip; 6818f134647SGleb Smirnoff 682df8bae1dSRodney W. Grimes /* 683df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 684df8bae1dSRodney W. Grimes */ 6858f134647SGleb Smirnoff len = off0 + tlen; 686c4556b2fSAndrey V. Elsukov ipttl = ip->ip_ttl; 687fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 6888f134647SGleb Smirnoff ipov->ih_len = htons(tlen); 689fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 69057f60867SMark Johnston /* Reset length for SDT probes. */ 6911d7ee746SKurt Lidl ip->ip_len = htons(len); 6921d7ee746SKurt Lidl /* Reset TOS bits */ 6931d7ee746SKurt Lidl ip->ip_tos = iptos; 6941d7ee746SKurt Lidl /* Re-initialization for later version check */ 695c4556b2fSAndrey V. Elsukov ip->ip_ttl = ipttl; 6961d7ee746SKurt Lidl ip->ip_v = IPVERSION; 697b2fe54bcSAndrey V. Elsukov ip->ip_hl = off0 >> 2; 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 } 705b287c6c7SBjoern A. Zeeb #endif /* INET */ 706df8bae1dSRodney W. Grimes 707df8bae1dSRodney W. Grimes /* 708df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 709df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 710df8bae1dSRodney W. Grimes */ 711fb59c426SYoshinobu Inoue off = th->th_off << 2; 712df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 71378b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadoff); 714df8bae1dSRodney W. Grimes goto drop; 715df8bae1dSRodney W. Grimes } 716fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 717df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 71804d3a452SHajimu UMEMOTO #ifdef INET6 719b287c6c7SBjoern A. Zeeb if (isipv6) { 720a4adf6ccSBjoern A. Zeeb if (m->m_len < off0 + off) { 7214e619b17SBjoern A. Zeeb m = m_pullup(m, off0 + off); 7224e619b17SBjoern A. Zeeb if (m == NULL) { 7234e619b17SBjoern A. Zeeb TCPSTAT_INC(tcps_rcvshort); 7244e619b17SBjoern A. Zeeb return (IPPROTO_DONE); 7254e619b17SBjoern A. Zeeb } 726a4adf6ccSBjoern A. Zeeb } 727fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 728fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 729b287c6c7SBjoern A. Zeeb } 73004d3a452SHajimu UMEMOTO #endif 731b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 732b287c6c7SBjoern A. Zeeb else 733b287c6c7SBjoern A. Zeeb #endif 734b287c6c7SBjoern A. Zeeb #ifdef INET 735b287c6c7SBjoern A. Zeeb { 736df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 737c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 7384dfdffe9SAndre Oppermann == NULL) { 73978b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 7408f5a8818SKevin Lo return (IPPROTO_DONE); 741df8bae1dSRodney W. Grimes } 742fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 743fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 744fb59c426SYoshinobu Inoue } 745df8bae1dSRodney W. Grimes } 746b287c6c7SBjoern A. Zeeb #endif 747df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 748fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 749df8bae1dSRodney W. Grimes } 750fb59c426SYoshinobu Inoue thflags = th->th_flags; 751df8bae1dSRodney W. Grimes 752e46cd3d4SDag-Erling Smørgrav /* 753df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 754df8bae1dSRodney W. Grimes */ 7552903309aSAttilio Rao tcp_fields_to_host(th); 756df8bae1dSRodney W. Grimes 757df8bae1dSRodney W. Grimes /* 758794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 759755c1f07SAndras Olah */ 760fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 761755c1f07SAndras Olah 762755c1f07SAndras Olah /* 7638d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 7648d573cc1SAndre Oppermann */ 765b8056faeSGleb Smirnoff if ( 766b8056faeSGleb Smirnoff #ifdef INET6 767b8056faeSGleb Smirnoff (isipv6 && (m->m_flags & M_IP6_NEXTHOP)) 768b8056faeSGleb Smirnoff #ifdef INET 769b8056faeSGleb Smirnoff || (!isipv6 && (m->m_flags & M_IP_NEXTHOP)) 770b8056faeSGleb Smirnoff #endif 771b8056faeSGleb Smirnoff #endif 772b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6) 773b8056faeSGleb Smirnoff (m->m_flags & M_IP_NEXTHOP) 774b8056faeSGleb Smirnoff #endif 775b8056faeSGleb Smirnoff ) 7769b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 7779b932e9eSAndre Oppermann 778ce7ad664SEd Maste findpcb: 7798a006adbSBjoern A. Zeeb #ifdef INET6 7808a006adbSBjoern A. Zeeb if (isipv6 && fwd_tag != NULL) { 7818a006adbSBjoern A. Zeeb struct sockaddr_in6 *next_hop6; 7828a006adbSBjoern A. Zeeb 7838a006adbSBjoern A. Zeeb next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1); 7848a006adbSBjoern A. Zeeb /* 7858a006adbSBjoern A. Zeeb * Transparently forwarded. Pretend to be the destination. 7868a006adbSBjoern A. Zeeb * Already got one like this? 7878a006adbSBjoern A. Zeeb */ 7888a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, 7898a006adbSBjoern A. Zeeb &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport, 7908a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m); 7918a006adbSBjoern A. Zeeb if (!inp) { 7928a006adbSBjoern A. Zeeb /* 7938a006adbSBjoern A. Zeeb * It's new. Try to find the ambushing socket. 7948a006adbSBjoern A. Zeeb * Because we've rewritten the destination address, 7958a006adbSBjoern A. Zeeb * any hardware-generated hash is ignored. 7968a006adbSBjoern A. Zeeb */ 7978a006adbSBjoern A. Zeeb inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src, 7988a006adbSBjoern A. Zeeb th->th_sport, &next_hop6->sin6_addr, 7998a006adbSBjoern A. Zeeb next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) : 8008a006adbSBjoern A. Zeeb th->th_dport, INPLOOKUP_WILDCARD | 8018a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 8028a006adbSBjoern A. Zeeb } 803c1de64a4SAndrey V. Elsukov } else if (isipv6) { 8048a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src, 8058a006adbSBjoern A. Zeeb th->th_sport, &ip6->ip6_dst, th->th_dport, 8068a006adbSBjoern A. Zeeb INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 8078a006adbSBjoern A. Zeeb m->m_pkthdr.rcvif, m); 8088a006adbSBjoern A. Zeeb } 8098a006adbSBjoern A. Zeeb #endif /* INET6 */ 8108a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET) 8118a006adbSBjoern A. Zeeb else 8128a006adbSBjoern A. Zeeb #endif 8138a006adbSBjoern A. Zeeb #ifdef INET 8148a006adbSBjoern A. Zeeb if (fwd_tag != NULL) { 8159b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 8169b932e9eSAndre Oppermann 8179b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 818f9e354dfSJulian Elischer /* 8192b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 820f9e354dfSJulian Elischer * already got one like this? 821f9e354dfSJulian Elischer */ 822d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport, 823fa046d87SRobert Watson ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB, 824d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 825fa046d87SRobert Watson if (!inp) { 826fa046d87SRobert Watson /* 827fa046d87SRobert Watson * It's new. Try to find the ambushing socket. 828d3c1f003SRobert Watson * Because we've rewritten the destination address, 829d3c1f003SRobert Watson * any hardware-generated hash is ignored. 830fa046d87SRobert Watson */ 831fa046d87SRobert Watson inp = in_pcblookup(&V_tcbinfo, ip->ip_src, 832fa046d87SRobert Watson th->th_sport, next_hop->sin_addr, 833fa046d87SRobert Watson next_hop->sin_port ? ntohs(next_hop->sin_port) : 834fa046d87SRobert Watson th->th_dport, INPLOOKUP_WILDCARD | 835fa046d87SRobert Watson INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 836f9e354dfSJulian Elischer } 837b10fbdeaSAndre Oppermann } else 838d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, 839fa046d87SRobert Watson th->th_sport, ip->ip_dst, th->th_dport, 840fa046d87SRobert Watson INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 841d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 8428a006adbSBjoern A. Zeeb #endif /* INET */ 843f9e354dfSJulian Elischer 844df8bae1dSRodney W. Grimes /* 845574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 846574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 8478b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 848df8bae1dSRodney W. Grimes */ 849816a3d83SPoul-Henning Kamp if (inp == NULL) { 850574b6964SAndre Oppermann /* 851574b6964SAndre Oppermann * Log communication attempts to ports that are not 852574b6964SAndre Oppermann * in use. 853574b6964SAndre Oppermann */ 854574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 855574b6964SAndre Oppermann tcp_log_in_vain == 2) { 856b7d747ecSAndre Oppermann if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6))) 857df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 858df541e5fSAndre Oppermann "to closed port\n", s, __func__); 8592e4e1b4cSGeoff Rehmet } 860574b6964SAndre Oppermann /* 861574b6964SAndre Oppermann * When blackholing do not respond with a RST but 862574b6964SAndre Oppermann * completely ignore the segment and drop it. 863574b6964SAndre Oppermann */ 864603724d3SBjoern A. Zeeb if ((V_blackhole == 1 && (thflags & TH_SYN)) || 865603724d3SBjoern A. Zeeb V_blackhole == 2) 8661929eae1SAndre Oppermann goto dropunlock; 867574b6964SAndre Oppermann 868a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 869a57815efSBosko Milekic goto dropwithreset; 870816a3d83SPoul-Henning Kamp } 871fa046d87SRobert Watson INP_WLOCK_ASSERT(inp); 872f5cf1e5fSJulien Charbon /* 873f5cf1e5fSJulien Charbon * While waiting for inp lock during the lookup, another thread 874f5cf1e5fSJulien Charbon * can have dropped the inpcb, in which case we need to loop back 875f5cf1e5fSJulien Charbon * and try to find a new inpcb to deliver to. 876f5cf1e5fSJulien Charbon */ 877f5cf1e5fSJulien Charbon if (inp->inp_flags & INP_DROPPED) { 878f5cf1e5fSJulien Charbon INP_WUNLOCK(inp); 879f5cf1e5fSJulien Charbon inp = NULL; 880f5cf1e5fSJulien Charbon goto findpcb; 881f5cf1e5fSJulien Charbon } 882c2529042SHans Petter Selasky if ((inp->inp_flowtype == M_HASHTYPE_NONE) && 883c2529042SHans Petter Selasky (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) && 884c2529042SHans Petter Selasky ((inp->inp_socket == NULL) || 885c2529042SHans Petter Selasky (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0)) { 88680cb9f21SKip Macy inp->inp_flowid = m->m_pkthdr.flowid; 8872f719932SAdrian Chadd inp->inp_flowtype = M_HASHTYPE_GET(m); 88880cb9f21SKip Macy } 889fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 890a2589465SKen Smith #ifdef INET6 891fcf59617SAndrey V. Elsukov if (isipv6 && IPSEC_ENABLED(ipv6) && 892fcf59617SAndrey V. Elsukov IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) { 893a2589465SKen Smith goto dropunlock; 894a2589465SKen Smith } 895fcf59617SAndrey V. Elsukov #ifdef INET 896fcf59617SAndrey V. Elsukov else 897fcf59617SAndrey V. Elsukov #endif 898fcf59617SAndrey V. Elsukov #endif /* INET6 */ 899fcf59617SAndrey V. Elsukov #ifdef INET 900fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4) && 901fcf59617SAndrey V. Elsukov IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) { 902fcf59617SAndrey V. Elsukov goto dropunlock; 903fcf59617SAndrey V. Elsukov } 904fcf59617SAndrey V. Elsukov #endif /* INET */ 905a2589465SKen Smith #endif /* IPSEC */ 906a2589465SKen Smith 9078d573cc1SAndre Oppermann /* 9088d573cc1SAndre Oppermann * Check the minimum TTL for socket. 9098d573cc1SAndre Oppermann */ 91034f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 91134f83c52SGeorge V. Neville-Neil #ifdef INET6 9122d8868dbSJonathan T. Looney if (isipv6) { 9132d8868dbSJonathan T. Looney if (inp->inp_ip_minttl > ip6->ip6_hlim) 914302ce8d6SAndre Oppermann goto dropunlock; 9152d8868dbSJonathan T. Looney } else 91634f83c52SGeorge V. Neville-Neil #endif 91734f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 918302ce8d6SAndre Oppermann goto dropunlock; 91934f83c52SGeorge V. Neville-Neil } 920936cd18dSAndre Oppermann 921340c35deSJonathan Lemon /* 922252ca428SRobert Watson * A previous connection in TIMEWAIT state is supposed to catch stray 923252ca428SRobert Watson * or duplicate segments arriving late. If this segment was a 9240989f56cSRobert Watson * legitimate new connection attempt, the old INPCB gets removed and 925252ca428SRobert Watson * we can try again to find a listening socket. 926252ca428SRobert Watson * 927fa046d87SRobert Watson * At this point, due to earlier optimism, we may hold only an inpcb 928fa046d87SRobert Watson * lock, and not the inpcbinfo write lock. If so, we need to try to 929fa046d87SRobert Watson * acquire it, or if that fails, acquire a reference on the inpcb, 930fa046d87SRobert Watson * drop all locks, acquire a global write lock, and then re-acquire 931fa046d87SRobert Watson * the inpcb lock. We may at that point discover that another thread 932fa046d87SRobert Watson * has tried to free the inpcb, in which case we need to loop back 933fa046d87SRobert Watson * and try to find a new inpcb to deliver to. 934fa046d87SRobert Watson * 935fa046d87SRobert Watson * XXXRW: It may be time to rethink timewait locking. 936340c35deSJonathan Lemon */ 937ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 938340c35deSJonathan Lemon if (thflags & TH_SYN) 939f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 9408d573cc1SAndre Oppermann /* 9418d573cc1SAndre Oppermann * NB: tcp_twcheck unlocks the INP and frees the mbuf. 9428d573cc1SAndre Oppermann */ 9432104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 944340c35deSJonathan Lemon goto findpcb; 9458f5a8818SKevin Lo return (IPPROTO_DONE); 946340c35deSJonathan Lemon } 947794235b7SAndre Oppermann /* 948794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 949794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 950794235b7SAndre Oppermann * segment and send an appropriate response. 951794235b7SAndre Oppermann */ 952df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 953a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 954a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 955a57815efSBosko Milekic goto dropwithreset; 95609f81a46SBosko Milekic } 957df8bae1dSRodney W. Grimes 95809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 95909fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 96009fe6320SNavdeep Parhar tcp_offload_input(tp, m); 96109fe6320SNavdeep Parhar m = NULL; /* consumed by the TOE driver */ 96209fe6320SNavdeep Parhar goto dropunlock; 96309fe6320SNavdeep Parhar } 96409fe6320SNavdeep Parhar #endif 96509fe6320SNavdeep Parhar 966c488362eSRobert Watson #ifdef MAC 9678501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 96830d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 969302ce8d6SAndre Oppermann goto dropunlock; 970c488362eSRobert Watson #endif 971a557af22SRobert Watson so = inp->inp_socket; 972302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 973610ee2f9SDavid Greenman #ifdef TCPDEBUG 974df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 975df8bae1dSRodney W. Grimes ostate = tp->t_state; 97610fe523eSMaxim Konovalov #ifdef INET6 9776f697424SBjoern A. Zeeb if (isipv6) { 978540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 979d30d90dcSMaxim Konovalov } else 9806f697424SBjoern A. Zeeb #endif 981540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 982fb59c426SYoshinobu Inoue tcp_savetcp = *th; 983df8bae1dSRodney W. Grimes } 984b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */ 985db33b3e6SAndre Oppermann /* 986db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 987db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 988a7c7f2a7SJohn Baldwin * attempt or the completion of a previous one. 989db33b3e6SAndre Oppermann */ 99045274760SJonathan T. Looney KASSERT(tp->t_state == TCPS_LISTEN || !(so->so_options & SO_ACCEPTCONN), 99145274760SJonathan T. Looney ("%s: so accepting but tp %p not listening", __func__, tp)); 99245274760SJonathan T. Looney if (tp->t_state == TCPS_LISTEN && (so->so_options & SO_ACCEPTCONN)) { 993540e8b7eSJeffrey Hsu struct in_conninfo inc; 994540e8b7eSJeffrey Hsu 9958bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 996302ce8d6SAndre Oppermann #ifdef INET6 997be2ac88cSJonathan Lemon if (isipv6) { 998dcdb4371SBjoern A. Zeeb inc.inc_flags |= INC_ISIPV6; 9995dff1c38SMichael Tuexen if (inp->inp_inc.inc_flags & INC_IPV6MINMTU) 10005dff1c38SMichael Tuexen inc.inc_flags |= INC_IPV6MINMTU; 1001be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 1002be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 1003302ce8d6SAndre Oppermann } else 1004302ce8d6SAndre Oppermann #endif 1005302ce8d6SAndre Oppermann { 1006be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 1007be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 1008be2ac88cSJonathan Lemon } 1009be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 1010be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 10117973fba3SJulian Elischer inc.inc_fibnum = so->so_fibnum; 1012be2ac88cSJonathan Lemon 1013fb59c426SYoshinobu Inoue /* 10148d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 10158d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 10168d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 1017fb59c426SYoshinobu Inoue */ 1018be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 1019a7c7f2a7SJohn Baldwin 1020bf6d304aSAndre Oppermann /* 1021bf6d304aSAndre Oppermann * Parse the TCP options here because 1022bf6d304aSAndre Oppermann * syncookies need access to the reflected 1023bf6d304aSAndre Oppermann * timestamp. 1024bf6d304aSAndre Oppermann */ 1025bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 10269fa198beSAndre Oppermann /* 10279fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 10289fa198beSAndre Oppermann * inp and tcpinfo locks. 10299fa198beSAndre Oppermann */ 1030fcf59617SAndrey V. Elsukov rstreason = syncache_expand(&inc, &to, th, &so, m); 1031fcf59617SAndrey V. Elsukov if (rstreason < 0) { 1032fcf59617SAndrey V. Elsukov /* 1033fcf59617SAndrey V. Elsukov * A failing TCP MD5 signature comparison 1034fcf59617SAndrey V. Elsukov * must result in the segment being dropped 1035fcf59617SAndrey V. Elsukov * and must not produce any response back 1036fcf59617SAndrey V. Elsukov * to the sender. 1037fcf59617SAndrey V. Elsukov */ 1038fcf59617SAndrey V. Elsukov goto dropunlock; 1039fcf59617SAndrey V. Elsukov } else if (rstreason == 0) { 10404195b4afSPaul Traina /* 1041e207f800SAndre Oppermann * No syncache entry or ACK was not 1042be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 10438d573cc1SAndre Oppermann * NB: syncache did its own logging 10448d573cc1SAndre Oppermann * of the failure cause. 10454195b4afSPaul Traina */ 1046be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 1047be2ac88cSJonathan Lemon goto dropwithreset; 1048be2ac88cSJonathan Lemon } 104909c305ebSPatrick Kelsey tfo_socket_result: 1050f76fcf6dSJeffrey Hsu if (so == NULL) { 1051be2ac88cSJonathan Lemon /* 1052e207f800SAndre Oppermann * We completed the 3-way handshake 1053e207f800SAndre Oppermann * but could not allocate a socket 1054e207f800SAndre Oppermann * either due to memory shortage, 1055e207f800SAndre Oppermann * listen queue length limits or 1056a160e630SAndre Oppermann * global socket limits. Send RST 1057a160e630SAndre Oppermann * or wait and have the remote end 1058a160e630SAndre Oppermann * retransmit the ACK for another 1059a160e630SAndre Oppermann * try. 1060be2ac88cSJonathan Lemon */ 1061a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1062a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 10638d573cc1SAndre Oppermann "Socket allocation failed due to " 10648d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 1065ac957cd2SJulian Elischer s, __func__, 1066ac957cd2SJulian Elischer V_tcp_sc_rst_sock_fail ? 1067ac957cd2SJulian Elischer "sending RST" : "try again"); 1068603724d3SBjoern A. Zeeb if (V_tcp_sc_rst_sock_fail) { 1069e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 1070e207f800SAndre Oppermann goto dropwithreset; 1071a160e630SAndre Oppermann } else 1072a160e630SAndre Oppermann goto dropunlock; 1073f76fcf6dSJeffrey Hsu } 1074be2ac88cSJonathan Lemon /* 1075be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 10768d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 10778d573cc1SAndre Oppermann * created socket and update the tp variable. 1078be2ac88cSJonathan Lemon */ 10798501a69cSRobert Watson INP_WUNLOCK(inp); /* listen socket */ 1080be2ac88cSJonathan Lemon inp = sotoinpcb(so); 1081ff9b006dSJulien Charbon /* 1082ff9b006dSJulien Charbon * New connection inpcb is already locked by 1083ff9b006dSJulien Charbon * syncache_expand(). 1084ff9b006dSJulien Charbon */ 1085ff9b006dSJulien Charbon INP_WLOCK_ASSERT(inp); 1086be2ac88cSJonathan Lemon tp = intotcpcb(inp); 10878d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 10888d573cc1SAndre Oppermann ("%s: ", __func__)); 1089be2ac88cSJonathan Lemon /* 1090302ce8d6SAndre Oppermann * Process the segment and the data it 1091302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 1092302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 1093302ce8d6SAndre Oppermann */ 10946138da62SMichael Tuexen TCP_PROBE5(receive, NULL, tp, m, tp, th); 109555bceb1eSRandall Stewart tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 10966573d758SMatt Macy iptos); 10978f5a8818SKevin Lo return (IPPROTO_DONE); 1098be2ac88cSJonathan Lemon } 1099a160e630SAndre Oppermann /* 11008d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 11018d573cc1SAndre Oppermann * 1102a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 1103a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 1104a160e630SAndre Oppermann * retransmits. 110519bc77c5SAndre Oppermann * 110619bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 110719bc77c5SAndre Oppermann * causes. 1108a160e630SAndre Oppermann */ 1109a160e630SAndre Oppermann if (thflags & TH_RST) { 111093899d10SMichael Tuexen syncache_chkrst(&inc, th, m); 1111a160e630SAndre Oppermann goto dropunlock; 1112a160e630SAndre Oppermann } 1113a160e630SAndre Oppermann /* 1114a160e630SAndre Oppermann * We can't do anything without SYN. 1115a160e630SAndre Oppermann */ 1116a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 1117a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1118a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1119773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 11208d573cc1SAndre Oppermann s, __func__); 112178b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1122a160e630SAndre Oppermann goto dropunlock; 1123a160e630SAndre Oppermann } 1124a160e630SAndre Oppermann /* 1125a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 1126a160e630SAndre Oppermann */ 1127fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1128a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1129a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11308d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 11318d573cc1SAndre Oppermann s, __func__); 1132a160e630SAndre Oppermann syncache_badack(&inc); /* XXX: Not needed! */ 113378b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1134a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1135a57815efSBosko Milekic goto dropwithreset; 11364195b4afSPaul Traina } 1137a160e630SAndre Oppermann /* 1138a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 1139a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 1140a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 1141a160e630SAndre Oppermann * TCP/IP stack. 1142a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 1143a160e630SAndre Oppermann * and is constantly refining its stack detection 1144a160e630SAndre Oppermann * strategies. 11458d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 1146a160e630SAndre Oppermann * and was used by RFC1644. 1147a160e630SAndre Oppermann */ 1148603724d3SBjoern A. Zeeb if ((thflags & TH_FIN) && V_drop_synfin) { 1149a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1150a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1151773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 11528d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 115378b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1154302ce8d6SAndre Oppermann goto dropunlock; 1155ebb0cbeaSPaul Traina } 1156be2ac88cSJonathan Lemon /* 1157be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 1158a160e630SAndre Oppermann * 1159a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 1160a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 1161a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 1162be2ac88cSJonathan Lemon */ 1163a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 1164a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 1165a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 1166a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 116733841545SHajimu UMEMOTO #ifdef INET6 116833841545SHajimu UMEMOTO /* 116933841545SHajimu UMEMOTO * If deprecated address is forbidden, 117033841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 117133841545SHajimu UMEMOTO * address to prevent any new inbound connection from 117233841545SHajimu UMEMOTO * getting established. 117333841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 117433841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 117533841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 117633841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 117733841545SHajimu UMEMOTO * for the exchange. 117833841545SHajimu UMEMOTO * 117933841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 118033841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 118133841545SHajimu UMEMOTO * SYN in this case. 118233841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 118333841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 118433841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 118533841545SHajimu UMEMOTO * used" 118633841545SHajimu UMEMOTO * 2. use of it with new communication: 118733841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 118833841545SHajimu UMEMOTO * with sufficient scope is available" 118933841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 119033841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 119133841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 119233841545SHajimu UMEMOTO * 119333841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 119433841545SHajimu UMEMOTO * multiple description text for deprecated address 119533841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 119633841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 119733841545SHajimu UMEMOTO */ 1198603724d3SBjoern A. Zeeb if (isipv6 && !V_ip6_use_deprecated) { 119933841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 120033841545SHajimu UMEMOTO 12013e88eb90SAndrey V. Elsukov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */); 12028c0fec80SRobert Watson if (ia6 != NULL && 120333841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 12048c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 1205a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1206a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12078d573cc1SAndre Oppermann "Connection attempt to deprecated " 12088d573cc1SAndre Oppermann "IPv6 address rejected\n", 1209a160e630SAndre Oppermann s, __func__); 121033841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 121133841545SHajimu UMEMOTO goto dropwithreset; 121233841545SHajimu UMEMOTO } 121377d396fdSMaksim Yevmenkin if (ia6) 12148c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 121533841545SHajimu UMEMOTO } 1216b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 121765f28919SJesper Skriver /* 1218db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 12198d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 1220db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 12218d573cc1SAndre Oppermann * If it is from this socket it must be forged. 12228d573cc1SAndre Oppermann * Don't respond if the source or destination is a 12238d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 122493ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 122593ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 122693ec91baSCrist J. Clark * in_broadcast() to find them. 1227be2ac88cSJonathan Lemon */ 12288d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 12298d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 12308d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12318d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 1232773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 1233302ce8d6SAndre Oppermann goto dropunlock; 12348d573cc1SAndre Oppermann } 1235db33b3e6SAndre Oppermann #ifdef INET6 1236b287c6c7SBjoern A. Zeeb if (isipv6) { 1237db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1238a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 1239a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1240a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12418d573cc1SAndre Oppermann "Connection attempt to/from self " 1242773673c1SAndre Oppermann "ignored\n", s, __func__); 1243302ce8d6SAndre Oppermann goto dropunlock; 1244a160e630SAndre Oppermann } 1245be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1246a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 1247a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1248a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12498d573cc1SAndre Oppermann "Connection attempt from/to multicast " 1250773673c1SAndre Oppermann "address ignored\n", s, __func__); 1251302ce8d6SAndre Oppermann goto dropunlock; 1252a160e630SAndre Oppermann } 1253b287c6c7SBjoern A. Zeeb } 1254db33b3e6SAndre Oppermann #endif 1255b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 1256b287c6c7SBjoern A. Zeeb else 1257b287c6c7SBjoern A. Zeeb #endif 1258b287c6c7SBjoern A. Zeeb #ifdef INET 1259b287c6c7SBjoern A. Zeeb { 1260db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1261a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 1262a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1263a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12648d573cc1SAndre Oppermann "Connection attempt from/to self " 1265773673c1SAndre Oppermann "ignored\n", s, __func__); 1266302ce8d6SAndre Oppermann goto dropunlock; 1267a160e630SAndre Oppermann } 1268be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1269be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 12702ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 1271a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 1272a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1273a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12748d573cc1SAndre Oppermann "Connection attempt from/to broad- " 1275773673c1SAndre Oppermann "or multicast address ignored\n", 1276a160e630SAndre Oppermann s, __func__); 1277302ce8d6SAndre Oppermann goto dropunlock; 1278c068736aSJeffrey Hsu } 1279a160e630SAndre Oppermann } 1280b287c6c7SBjoern A. Zeeb #endif 1281be2ac88cSJonathan Lemon /* 1282db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 1283db33b3e6SAndre Oppermann * for syncache. 1284be2ac88cSJonathan Lemon */ 12853c653157SHartmut Brandt #ifdef TCPDEBUG 12863c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 12873c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 12883c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 12893c653157SHartmut Brandt #endif 12902b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 1291f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 1292fa49a964SMichael Tuexen if (syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL, iptos)) 129309c305ebSPatrick Kelsey goto tfo_socket_result; 129418a75309SPatrick Kelsey 1295be2ac88cSJonathan Lemon /* 12964d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 1297a7c7f2a7SJohn Baldwin * Only the listen socket is unlocked by syncache_add(). 1298be2ac88cSJonathan Lemon */ 1299384a5c3cSAndrey V. Elsukov INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo); 13008f5a8818SKevin Lo return (IPPROTO_DONE); 1301982c1675SAndre Oppermann } else if (tp->t_state == TCPS_LISTEN) { 1302982c1675SAndre Oppermann /* 1303982c1675SAndre Oppermann * When a listen socket is torn down the SO_ACCEPTCONN 1304982c1675SAndre Oppermann * flag is removed first while connections are drained 1305982c1675SAndre Oppermann * from the accept queue in a unlock/lock cycle of the 1306982c1675SAndre Oppermann * ACCEPT_LOCK, opening a race condition allowing a SYN 1307982c1675SAndre Oppermann * attempt go through unhandled. 1308982c1675SAndre Oppermann */ 1309982c1675SAndre Oppermann goto dropunlock; 1310f76fcf6dSJeffrey Hsu } 1311fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1312fcf59617SAndrey V. Elsukov if (tp->t_flags & TF_SIGNATURE) { 1313fcf59617SAndrey V. Elsukov tcp_dooptions(&to, optp, optlen, thflags); 1314fcf59617SAndrey V. Elsukov if ((to.to_flags & TOF_SIGNATURE) == 0) { 1315fcf59617SAndrey V. Elsukov TCPSTAT_INC(tcps_sig_err_nosigopt); 13162903309aSAttilio Rao goto dropunlock; 13172903309aSAttilio Rao } 1318fcf59617SAndrey V. Elsukov if (!TCPMD5_ENABLED() || 1319fcf59617SAndrey V. Elsukov TCPMD5_INPUT(m, th, to.to_signature) != 0) 1320fcf59617SAndrey V. Elsukov goto dropunlock; 13212903309aSAttilio Rao } 13222903309aSAttilio Rao #endif 13232b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 132457f60867SMark Johnston 1325302ce8d6SAndre Oppermann /* 13268d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 13278d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 13288d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 1329302ce8d6SAndre Oppermann */ 13306573d758SMatt Macy tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos); 13318f5a8818SKevin Lo return (IPPROTO_DONE); 1332be2ac88cSJonathan Lemon 1333302ce8d6SAndre Oppermann dropwithreset: 13342b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 133557f60867SMark Johnston 1336014ea782SRobert Watson if (inp != NULL) { 1337302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 1338014ea782SRobert Watson INP_WUNLOCK(inp); 1339dd8ac7f9SRobert Watson } else 1340014ea782SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1341302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 1342014ea782SRobert Watson goto drop; 1343014ea782SRobert Watson 1344302ce8d6SAndre Oppermann dropunlock: 134557f60867SMark Johnston if (m != NULL) 13462b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 134757f60867SMark Johnston 13489fa198beSAndre Oppermann if (inp != NULL) 13498501a69cSRobert Watson INP_WUNLOCK(inp); 1350014ea782SRobert Watson 1351302ce8d6SAndre Oppermann drop: 1352384a5c3cSAndrey V. Elsukov INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo); 1353a160e630SAndre Oppermann if (s != NULL) 1354a160e630SAndre Oppermann free(s, M_TCPLOG); 1355302ce8d6SAndre Oppermann if (m != NULL) 1356302ce8d6SAndre Oppermann m_freem(m); 13578f5a8818SKevin Lo return (IPPROTO_DONE); 1358302ce8d6SAndre Oppermann } 1359302ce8d6SAndre Oppermann 1360e44c1887SSteven Hartland /* 1361e44c1887SSteven Hartland * Automatic sizing of receive socket buffer. Often the send 1362e44c1887SSteven Hartland * buffer size is not optimally adjusted to the actual network 1363e44c1887SSteven Hartland * conditions at hand (delay bandwidth product). Setting the 1364e44c1887SSteven Hartland * buffer size too small limits throughput on links with high 1365e44c1887SSteven Hartland * bandwidth and high delay (eg. trans-continental/oceanic links). 1366e44c1887SSteven Hartland * 1367e44c1887SSteven Hartland * On the receive side the socket buffer memory is only rarely 1368e44c1887SSteven Hartland * used to any significant extent. This allows us to be much 1369e44c1887SSteven Hartland * more aggressive in scaling the receive socket buffer. For 1370e44c1887SSteven Hartland * the case that the buffer space is actually used to a large 1371e44c1887SSteven Hartland * extent and we run out of kernel memory we can simply drop 1372e44c1887SSteven Hartland * the new segments; TCP on the sender will just retransmit it 1373e44c1887SSteven Hartland * later. Setting the buffer size too big may only consume too 1374e44c1887SSteven Hartland * much kernel memory if the application doesn't read() from 1375e44c1887SSteven Hartland * the socket or packet loss or reordering makes use of the 1376e44c1887SSteven Hartland * reassembly queue. 1377e44c1887SSteven Hartland * 1378e44c1887SSteven Hartland * The criteria to step up the receive buffer one notch are: 1379e44c1887SSteven Hartland * 1. Application has not set receive buffer size with 1380e44c1887SSteven Hartland * SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE. 1381560c0586SMichael Tuexen * 2. the number of bytes received during 1/2 of an sRTT 1382560c0586SMichael Tuexen * is at least 3/8 of the current socket buffer size. 1383560c0586SMichael Tuexen * 3. receive buffer size has not hit maximal automatic size; 1384e44c1887SSteven Hartland * 1385560c0586SMichael Tuexen * If all of the criteria are met we increaset the socket buffer 1386560c0586SMichael Tuexen * by a 1/2 (bounded by the max). This allows us to keep ahead 1387560c0586SMichael Tuexen * of slow-start but also makes it so our peer never gets limited 1388560c0586SMichael Tuexen * by our rwnd which we then open up causing a burst. 1389560c0586SMichael Tuexen * 1390560c0586SMichael Tuexen * This algorithm does two steps per RTT at most and only if 1391e44c1887SSteven Hartland * we receive a bulk stream w/o packet losses or reorderings. 1392e44c1887SSteven Hartland * Shrinking the buffer during idle times is not necessary as 1393e44c1887SSteven Hartland * it doesn't consume any memory when idle. 1394e44c1887SSteven Hartland * 1395e44c1887SSteven Hartland * TODO: Only step up if the application is actually serving 1396e44c1887SSteven Hartland * the buffer to better manage the socket buffer resources. 1397e44c1887SSteven Hartland */ 1398e44c1887SSteven Hartland int 1399e44c1887SSteven Hartland tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so, 1400e44c1887SSteven Hartland struct tcpcb *tp, int tlen) 1401e44c1887SSteven Hartland { 1402e44c1887SSteven Hartland int newsize = 0; 1403e44c1887SSteven Hartland 1404e44c1887SSteven Hartland if (V_tcp_do_autorcvbuf && (so->so_rcv.sb_flags & SB_AUTOSIZE) && 1405e44c1887SSteven Hartland tp->t_srtt != 0 && tp->rfbuf_ts != 0 && 1406e44c1887SSteven Hartland TCP_TS_TO_TICKS(tcp_ts_getticks() - tp->rfbuf_ts) > 1407560c0586SMichael Tuexen ((tp->t_srtt >> TCP_RTT_SHIFT)/2)) { 1408560c0586SMichael Tuexen if (tp->rfbuf_cnt > ((so->so_rcv.sb_hiwat / 2)/ 4 * 3) && 1409e44c1887SSteven Hartland so->so_rcv.sb_hiwat < V_tcp_autorcvbuf_max) { 1410560c0586SMichael Tuexen newsize = min((so->so_rcv.sb_hiwat + (so->so_rcv.sb_hiwat/2)), V_tcp_autorcvbuf_max); 1411e44c1887SSteven Hartland } 1412e44c1887SSteven Hartland TCP_PROBE6(receive__autoresize, NULL, tp, m, tp, th, newsize); 1413e44c1887SSteven Hartland 1414e44c1887SSteven Hartland /* Start over with next RTT. */ 1415e44c1887SSteven Hartland tp->rfbuf_ts = 0; 1416e44c1887SSteven Hartland tp->rfbuf_cnt = 0; 1417e44c1887SSteven Hartland } else { 1418e44c1887SSteven Hartland tp->rfbuf_cnt += tlen; /* add up */ 1419e44c1887SSteven Hartland } 1420e44c1887SSteven Hartland return (newsize); 1421e44c1887SSteven Hartland } 1422e44c1887SSteven Hartland 142355bceb1eSRandall Stewart void 1424302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 14256573d758SMatt Macy struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos) 1426302ce8d6SAndre Oppermann { 1427021eaf79SHiren Panchasara int thflags, acked, ourfinisacked, needoutput = 0, sack_changed; 1428302ce8d6SAndre Oppermann int rstreason, todrop, win; 14293ac12506SJonathan T. Looney uint32_t tiwin; 14304b7b743cSLawrence Stewart uint16_t nsegs; 143107dacf03SAndre Oppermann char *s; 143207dacf03SAndre Oppermann struct in_conninfo *inc; 1433c11a15bfSGleb Smirnoff struct mbuf *mfree; 1434302ce8d6SAndre Oppermann struct tcpopt to; 1435281a0fd4SPatrick Kelsey int tfo_syn; 1436302ce8d6SAndre Oppermann 143714739780SMaxim Konovalov #ifdef TCPDEBUG 143814739780SMaxim Konovalov /* 143914739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 144014739780SMaxim Konovalov * now IPv6. 144114739780SMaxim Konovalov */ 144214739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 144314739780SMaxim Konovalov struct tcphdr tcp_savetcp; 144414739780SMaxim Konovalov short ostate = 0; 144514739780SMaxim Konovalov #endif 1446302ce8d6SAndre Oppermann thflags = th->th_flags; 144707dacf03SAndre Oppermann inc = &tp->t_inpcb->inp_inc; 1448d64a46eaSLawrence Stewart tp->sackhint.last_sack_ack = 0; 1449021eaf79SHiren Panchasara sack_changed = 0; 14504b7b743cSLawrence Stewart nsegs = max(1, m->m_pkthdr.lro_nsegs); 1451d40c0d47SGleb Smirnoff 1452d40c0d47SGleb Smirnoff NET_EPOCH_ASSERT(); 14538501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1454679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1455679d9708SAndre Oppermann __func__)); 1456679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1457679d9708SAndre Oppermann __func__)); 1458df8bae1dSRodney W. Grimes 145986a996e6SHiren Panchasara #ifdef TCPPCAP 146086a996e6SHiren Panchasara /* Save segment, if requested. */ 146186a996e6SHiren Panchasara tcp_pcap_add(th, m, &(tp->t_inpkts)); 146286a996e6SHiren Panchasara #endif 14632529f56eSJonathan T. Looney TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0, 14642529f56eSJonathan T. Looney tlen, NULL, true); 146586a996e6SHiren Panchasara 1466013f4df6SMichael Tuexen if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) { 1467013f4df6SMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 1468013f4df6SMichael Tuexen log(LOG_DEBUG, "%s; %s: " 1469013f4df6SMichael Tuexen "SYN|FIN segment ignored (based on " 1470013f4df6SMichael Tuexen "sysctl setting)\n", s, __func__); 1471013f4df6SMichael Tuexen free(s, M_TCPLOG); 1472013f4df6SMichael Tuexen } 1473013f4df6SMichael Tuexen goto drop; 1474013f4df6SMichael Tuexen } 1475013f4df6SMichael Tuexen 1476df8bae1dSRodney W. Grimes /* 1477ebfd7534SMichael Tuexen * If a segment with the ACK-bit set arrives in the SYN-SENT state 1478ebfd7534SMichael Tuexen * check SEQ.ACK first. 1479ebfd7534SMichael Tuexen */ 1480ebfd7534SMichael Tuexen if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) && 1481ebfd7534SMichael Tuexen (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) { 1482ebfd7534SMichael Tuexen rstreason = BANDLIM_UNLIMITED; 1483ebfd7534SMichael Tuexen goto dropwithreset; 1484ebfd7534SMichael Tuexen } 1485ebfd7534SMichael Tuexen 1486ebfd7534SMichael Tuexen /* 1487df8bae1dSRodney W. Grimes * Segment received on connection. 1488df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1489e8949f74SAndre Oppermann * XXX: This should be done after segment 1490e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 1491df8bae1dSRodney W. Grimes */ 14929b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 1493df8bae1dSRodney W. Grimes 1494df8bae1dSRodney W. Grimes /* 1495c52102ddSHiren Panchasara * Scale up the window into a 32-bit value. 1496e8949f74SAndre Oppermann * For the SYN_SENT state the scale is zero. 1497464fcfbcSAndre Oppermann */ 1498464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 1499464fcfbcSAndre Oppermann 1500464fcfbcSAndre Oppermann /* 1501f2512ba1SRui Paulo * TCP ECN processing. 1502f2512ba1SRui Paulo */ 1503*3cf38784SMichael Tuexen if (tp->t_flags2 & TF2_ECN_PERMIT) { 15049c251892SRui Paulo if (thflags & TH_CWR) 1505*3cf38784SMichael Tuexen tp->t_flags2 &= ~TF2_ECN_SND_ECE; 1506f2512ba1SRui Paulo switch (iptos & IPTOS_ECN_MASK) { 1507f2512ba1SRui Paulo case IPTOS_ECN_CE: 1508*3cf38784SMichael Tuexen tp->t_flags2 |= TF2_ECN_SND_ECE; 150978b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ce); 1510f2512ba1SRui Paulo break; 1511f2512ba1SRui Paulo case IPTOS_ECN_ECT0: 151278b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect0); 1513f2512ba1SRui Paulo break; 1514f2512ba1SRui Paulo case IPTOS_ECN_ECT1: 151578b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect1); 1516f2512ba1SRui Paulo break; 1517f2512ba1SRui Paulo } 151864807b30SHiren Panchasara 151964807b30SHiren Panchasara /* Process a packet differently from RFC3168. */ 152064807b30SHiren Panchasara cc_ecnpkt_handler(tp, th, iptos); 152164807b30SHiren Panchasara 1522dbc42409SLawrence Stewart /* Congestion experienced. */ 1523dbc42409SLawrence Stewart if (thflags & TH_ECE) { 1524dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_ECN); 1525f2512ba1SRui Paulo } 1526f2512ba1SRui Paulo } 1527f2512ba1SRui Paulo 1528f2512ba1SRui Paulo /* 1529f72167f4SAndre Oppermann * Parse options on any incoming segment. 1530f72167f4SAndre Oppermann */ 1531302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1532302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1533302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1534f72167f4SAndre Oppermann 1535fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1536fcf59617SAndrey V. Elsukov if ((tp->t_flags & TF_SIGNATURE) != 0 && 1537fcf59617SAndrey V. Elsukov (to.to_flags & TOF_SIGNATURE) == 0) { 1538fcf59617SAndrey V. Elsukov TCPSTAT_INC(tcps_sig_err_sigopt); 1539fcf59617SAndrey V. Elsukov /* XXX: should drop? */ 1540fcf59617SAndrey V. Elsukov } 1541fcf59617SAndrey V. Elsukov #endif 1542f72167f4SAndre Oppermann /* 1543f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1544bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1545bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1546bf6d304aSAndre Oppermann * was established. 1547f72167f4SAndre Oppermann */ 1548bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1549e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 1550d8951c8aSBjoern A. Zeeb if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) 1551f72167f4SAndre Oppermann to.to_tsecr = 0; 155210d20c84SMatt Macy else if (tp->t_flags & TF_PREVVALID && 155310d20c84SMatt Macy tp->t_badrxtwin != 0 && SEQ_LT(to.to_tsecr, tp->t_badrxtwin)) 155410d20c84SMatt Macy cc_cong_signal(tp, th, CC_RTO_ERR); 1555bf6d304aSAndre Oppermann } 155607dacf03SAndre Oppermann /* 155797d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 155897d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 15595396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 15605396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 156197d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1562df8bae1dSRodney W. Grimes */ 15630a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1564464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 1565464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 1566be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 156702a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 1568be2ac88cSJonathan Lemon } 15695396d0f8SAndre Oppermann /* 15705396d0f8SAndre Oppermann * Initial send window. It will be updated with 15715396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 15725396d0f8SAndre Oppermann */ 15735396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 1574be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1575be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1576be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1577d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1578be2ac88cSJonathan Lemon } 1579be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1580be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 15813529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 15823529149eSAndre Oppermann (to.to_flags & TOF_SACKPERM) == 0) 15833529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 1584c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) { 1585a026a53aSMichael Tuexen if (to.to_flags & TOF_FASTOPEN) { 1586a026a53aSMichael Tuexen uint16_t mss; 1587a026a53aSMichael Tuexen 1588a026a53aSMichael Tuexen if (to.to_flags & TOF_MSS) 1589a026a53aSMichael Tuexen mss = to.to_mss; 1590c560df6fSPatrick Kelsey else 1591a026a53aSMichael Tuexen if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) 1592a026a53aSMichael Tuexen mss = TCP6_MSS; 1593a026a53aSMichael Tuexen else 1594a026a53aSMichael Tuexen mss = TCP_MSS; 1595a026a53aSMichael Tuexen tcp_fastopen_update_cache(tp, mss, 1596a026a53aSMichael Tuexen to.to_tfo_len, to.to_tfo_cookie); 1597a026a53aSMichael Tuexen } else 1598c560df6fSPatrick Kelsey tcp_fastopen_disable_path(tp); 1599c560df6fSPatrick Kelsey } 16006d90faf3SPaul Saab } 16016d90faf3SPaul Saab 1602df8bae1dSRodney W. Grimes /* 16034da9052aSMichael Tuexen * If timestamps were negotiated during SYN/ACK they should 16044da9052aSMichael Tuexen * appear on every segment during this session and vice versa. 16054da9052aSMichael Tuexen */ 16064da9052aSMichael Tuexen if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) { 16074da9052aSMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 16084da9052aSMichael Tuexen log(LOG_DEBUG, "%s; %s: Timestamp missing, " 16094da9052aSMichael Tuexen "no action\n", s, __func__); 16104da9052aSMichael Tuexen free(s, M_TCPLOG); 16114da9052aSMichael Tuexen } 16124da9052aSMichael Tuexen } 16134da9052aSMichael Tuexen if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) { 16144da9052aSMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 16154da9052aSMichael Tuexen log(LOG_DEBUG, "%s; %s: Timestamp not expected, " 16164da9052aSMichael Tuexen "no action\n", s, __func__); 16174da9052aSMichael Tuexen free(s, M_TCPLOG); 16184da9052aSMichael Tuexen } 16194da9052aSMichael Tuexen } 16204da9052aSMichael Tuexen 16214da9052aSMichael Tuexen /* 1622df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1623df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1624df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1625df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1626df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1627df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1628df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1629df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1630df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1631df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1632df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1633df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1634a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1635c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 1636a0292f23SGarrett Wollman * be TH_NEEDSYN. 1637df8bae1dSRodney W. Grimes */ 1638df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1639c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 1640fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1641c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 1642c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 1643a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1644c28440dbSRandall Stewart SEGQ_EMPTY(tp) && 16454741bfcbSPatrick Kelsey ((to.to_flags & TOF_TS) == 0 || 1646c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1647df8bae1dSRodney W. Grimes 1648df8bae1dSRodney W. Grimes /* 1649df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1650df8bae1dSRodney W. Grimes * record the timestamp. 1651a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1652a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1653df8bae1dSRodney W. Grimes */ 1654be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1655fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1656d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1657a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1658df8bae1dSRodney W. Grimes } 1659df8bae1dSRodney W. Grimes 1660fb59c426SYoshinobu Inoue if (tlen == 0) { 1661fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1662fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1663dbc42409SLawrence Stewart !IN_RECOVERY(tp->t_flags) && 1664fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1665dbc42409SLawrence Stewart TAILQ_EMPTY(&tp->snd_holes)) { 1666df8bae1dSRodney W. Grimes /* 1667e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1668df8bae1dSRodney W. Grimes */ 166978b50714SRobert Watson TCPSTAT_INC(tcps_predack); 1670252ca428SRobert Watson 16719b8b58e0SJonathan Lemon /* 167210d20c84SMatt Macy * "bad retransmit" recovery without timestamps. 16739b8b58e0SJonathan Lemon */ 167410d20c84SMatt Macy if ((to.to_flags & TOF_TS) == 0 && 167510d20c84SMatt Macy tp->t_rxtshift == 1 && 1676672dc4aeSJohn Baldwin tp->t_flags & TF_PREVVALID && 16776dfb8b31SJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) { 1678dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 16799b8b58e0SJonathan Lemon } 1680fa55172bSMatthew Dillon 1681fa55172bSMatthew Dillon /* 1682fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1683fa55172bSMatthew Dillon * 1684fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1685fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1686fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1687fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1688fa55172bSMatthew Dillon */ 1689fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1690fa55172bSMatthew Dillon to.to_tsecr) { 16913ac12506SJonathan T. Looney uint32_t t; 1692d8951c8aSBjoern A. Zeeb 1693d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 1694d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 1695d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 1696a0292f23SGarrett Wollman tcp_xmit_timer(tp, 1697d8951c8aSBjoern A. Zeeb TCP_TS_TO_TICKS(t) + 1); 1698fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1699fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1700eaf80179SAndre Oppermann if (!tp->t_rttlow || 1701eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1702eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1703c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1704c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1705fa55172bSMatthew Dillon } 1706dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 170739bc9de5SLawrence Stewart 1708bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 170939bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 171039bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 1711bd79708dSJonathan T. Looney #endif 171239bc9de5SLawrence Stewart 17134b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvackpack, nsegs); 171478b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 1715df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 17169d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 17179d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 17189d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 1719dbc42409SLawrence Stewart 1720dbc42409SLawrence Stewart /* 1721dbc42409SLawrence Stewart * Let the congestion control algorithm update 1722dbc42409SLawrence Stewart * congestion control related information. This 1723dbc42409SLawrence Stewart * typically means increasing the congestion 1724dbc42409SLawrence Stewart * window. 1725dbc42409SLawrence Stewart */ 17264b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, CC_ACK); 1727dbc42409SLawrence Stewart 17289d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 17291645d090SJeff Roberson /* 1730e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 17311645d090SJeff Roberson * to th_ack. 17321645d090SJeff Roberson */ 1733cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1734b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1735df8bae1dSRodney W. Grimes m_freem(m); 1736df8bae1dSRodney W. Grimes 1737df8bae1dSRodney W. Grimes /* 1738df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1739df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1740df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1741df8bae1dSRodney W. Grimes * If process is waiting for space, 1742df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1743df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1744df8bae1dSRodney W. Grimes * decide between more output or persist. 1745e8949f74SAndre Oppermann */ 17463c653157SHartmut Brandt #ifdef TCPDEBUG 17473c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 17483c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 17493c653157SHartmut Brandt (void *)tcp_saveipgen, 17503c653157SHartmut Brandt &tcp_savetcp, 0); 17513c653157SHartmut Brandt #endif 17522b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 1753df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1754b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1755b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1756b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1757b8152ba7SAndre Oppermann tp->t_rxtcur); 1758df8bae1dSRodney W. Grimes sowwakeup(so); 1759cfa6009eSGleb Smirnoff if (sbavail(&so->so_snd)) 176055bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 1761a14c749fSJonathan Lemon goto check_delack; 1762df8bae1dSRodney W. Grimes } 1763fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1764fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 17656741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 17666741ecf5SAndre Oppermann 1767df8bae1dSRodney W. Grimes /* 1768252ca428SRobert Watson * This is a pure, in-sequence data packet with 1769252ca428SRobert Watson * nothing on the reassembly queue and we have enough 1770252ca428SRobert Watson * buffer space to take it. 1771df8bae1dSRodney W. Grimes */ 17726d90faf3SPaul Saab /* Clean receiver SACK report if present */ 17733529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 17746d90faf3SPaul Saab tcp_clean_sackreport(tp); 177578b50714SRobert Watson TCPSTAT_INC(tcps_preddat); 1776fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 17771645d090SJeff Roberson /* 17781645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 17791645d090SJeff Roberson * th_seq. 17801645d090SJeff Roberson */ 178160ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 17821645d090SJeff Roberson /* 17831645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 17841645d090SJeff Roberson * rcv_nxt. 17851645d090SJeff Roberson */ 17861645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 17874b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvpack, nsegs); 178878b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 17893c653157SHartmut Brandt #ifdef TCPDEBUG 17903c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 17913c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 17923c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 17933c653157SHartmut Brandt #endif 17942b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 17955d06879aSGeorge V. Neville-Neil 1796e44c1887SSteven Hartland newsize = tcp_autorcvbuf(m, th, so, tp, tlen); 17976741ecf5SAndre Oppermann 17986741ecf5SAndre Oppermann /* Add data to socket buffer. */ 17991e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1800c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1801c1c36a2cSMike Silbersack m_freem(m); 1802c1c36a2cSMike Silbersack } else { 18036741ecf5SAndre Oppermann /* 18046741ecf5SAndre Oppermann * Set new socket buffer size. 18056741ecf5SAndre Oppermann * Give up when limit is reached. 18066741ecf5SAndre Oppermann */ 18076741ecf5SAndre Oppermann if (newsize) 18086741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 18096c8286e4SRobert Watson newsize, so, NULL)) 18106741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1811fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 1812651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 1813c1c36a2cSMike Silbersack } 1814e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 18151e4d7da7SRobert Watson sorwakeup_locked(so); 1816c1e5a6e5SAndre Oppermann if (DELAY_ACK(tp, tlen)) { 18173bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1818f498eeeeSDavid Greenman } else { 1819e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 182055bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 1821e612a582SDavid Greenman } 1822a14c749fSJonathan Lemon goto check_delack; 1823df8bae1dSRodney W. Grimes } 1824df8bae1dSRodney W. Grimes } 1825df8bae1dSRodney W. Grimes 1826df8bae1dSRodney W. Grimes /* 1827df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1828df8bae1dSRodney W. Grimes * and then do TCP input processing. 1829df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1830df8bae1dSRodney W. Grimes * but not less than advertised window. 1831df8bae1dSRodney W. Grimes */ 1832df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1833df8bae1dSRodney W. Grimes if (win < 0) 1834df8bae1dSRodney W. Grimes win = 0; 183566e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1836df8bae1dSRodney W. Grimes 1837df8bae1dSRodney W. Grimes switch (tp->t_state) { 1838df8bae1dSRodney W. Grimes 1839df8bae1dSRodney W. Grimes /* 1840764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1841764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1842764d8cefSBill Fenner */ 1843764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1844fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1845fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1846a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1847a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1848a57815efSBosko Milekic goto dropwithreset; 1849a57815efSBosko Milekic } 185068bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) { 1851281a0fd4SPatrick Kelsey /* 1852281a0fd4SPatrick Kelsey * When a TFO connection is in SYN_RECEIVED, the 1853281a0fd4SPatrick Kelsey * only valid packets are the initial SYN, a 1854281a0fd4SPatrick Kelsey * retransmit/copy of the initial SYN (possibly with 1855281a0fd4SPatrick Kelsey * a subset of the original data), a valid ACK, a 1856281a0fd4SPatrick Kelsey * FIN, or a RST. 1857281a0fd4SPatrick Kelsey */ 1858281a0fd4SPatrick Kelsey if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) { 1859281a0fd4SPatrick Kelsey rstreason = BANDLIM_RST_OPENPORT; 1860281a0fd4SPatrick Kelsey goto dropwithreset; 1861281a0fd4SPatrick Kelsey } else if (thflags & TH_SYN) { 1862281a0fd4SPatrick Kelsey /* non-initial SYN is ignored */ 1863281a0fd4SPatrick Kelsey if ((tcp_timer_active(tp, TT_DELACK) || 1864281a0fd4SPatrick Kelsey tcp_timer_active(tp, TT_REXMT))) 1865281a0fd4SPatrick Kelsey goto drop; 1866281a0fd4SPatrick Kelsey } else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) { 1867281a0fd4SPatrick Kelsey goto drop; 1868281a0fd4SPatrick Kelsey } 1869281a0fd4SPatrick Kelsey } 1870764d8cefSBill Fenner break; 1871764d8cefSBill Fenner 1872764d8cefSBill Fenner /* 1873df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 187498732609SMichael Tuexen * if seg contains a RST with valid ACK (SEQ.ACK has already 187598732609SMichael Tuexen * been verified), then drop the connection. 187698732609SMichael Tuexen * if seg contains a RST without an ACK, drop the seg. 187798732609SMichael Tuexen * if seg does not contain SYN, then drop the seg. 1878df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1879df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1880df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1881f2512ba1SRui Paulo * if seg contains an ECE and ECN support is enabled, the stream 1882f2512ba1SRui Paulo * is ECN capable. 1883df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1884df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1885df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1886df8bae1dSRodney W. Grimes */ 1887df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 188857f60867SMark Johnston if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) { 1889d9fae5abSAndriy Gapon TCP_PROBE5(connect__refused, NULL, tp, 18902b9c9984SGeorge V. Neville-Neil m, tp, th); 1891df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 189257f60867SMark Johnston } 18930ca3f933SAndre Oppermann if (thflags & TH_RST) 1894df8bae1dSRodney W. Grimes goto drop; 18950ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 1896df8bae1dSRodney W. Grimes goto drop; 1897464fcfbcSAndre Oppermann 1898fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1899df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1900fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1901c560df6fSPatrick Kelsey int tfo_partial_ack = 0; 1902c560df6fSPatrick Kelsey 190378b50714SRobert Watson TCPSTAT_INC(tcps_connects); 1904845799c1SAndras Olah soisconnected(so); 1905c488362eSRobert Watson #ifdef MAC 190630d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 1907c488362eSRobert Watson #endif 1908845799c1SAndras Olah /* Do window scaling on this connection? */ 1909845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1910845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1911845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1912845799c1SAndras Olah } 19133ac12506SJonathan T. Looney tp->rcv_adv += min(tp->rcv_wnd, 1914766282cbSJohn Baldwin TCP_MAXWIN << tp->rcv_scale); 1915a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1916a0292f23SGarrett Wollman /* 1917c560df6fSPatrick Kelsey * If not all the data that was sent in the TFO SYN 1918c560df6fSPatrick Kelsey * has been acked, resend the remainder right away. 1919c560df6fSPatrick Kelsey */ 1920c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags) && 1921c560df6fSPatrick Kelsey (tp->snd_una != tp->snd_max)) { 1922c560df6fSPatrick Kelsey tp->snd_nxt = th->th_ack; 1923c560df6fSPatrick Kelsey tfo_partial_ack = 1; 1924c560df6fSPatrick Kelsey } 1925c560df6fSPatrick Kelsey /* 1926a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1927a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1928a0292f23SGarrett Wollman */ 1929c560df6fSPatrick Kelsey if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial_ack) 1930b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 1931b8152ba7SAndre Oppermann tcp_delacktime); 1932a0292f23SGarrett Wollman else 1933a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1934f2512ba1SRui Paulo 1935bf7fcdb1SMichael Tuexen if (((thflags & (TH_CWR | TH_ECE)) == TH_ECE) && 1936bf7fcdb1SMichael Tuexen V_tcp_do_ecn) { 1937*3cf38784SMichael Tuexen tp->t_flags2 |= TF2_ECN_PERMIT; 193878b50714SRobert Watson TCPSTAT_INC(tcps_ecn_shs); 1939f2512ba1SRui Paulo } 1940f2512ba1SRui Paulo 1941a0292f23SGarrett Wollman /* 1942a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1943a0292f23SGarrett Wollman * Transitions: 1944a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1945a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1946a0292f23SGarrett Wollman */ 19479b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1948a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 194957f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 1950a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1951fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 19527ff19458SPaul Traina } else { 195357f60867SMark Johnston tcp_state_change(tp, TCPS_ESTABLISHED); 1954d9fae5abSAndriy Gapon TCP_PROBE5(connect__established, NULL, tp, 19552b9c9984SGeorge V. Neville-Neil m, tp, th); 1956dbc42409SLawrence Stewart cc_conn_init(tp); 19579077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 19589077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 19597ff19458SPaul Traina } 1960a0292f23SGarrett Wollman } else { 1961a0292f23SGarrett Wollman /* 1962c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 1963153edc50SHiren Panchasara * simultaneous open. 1964c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 1965c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 1966a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1967a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1968a0292f23SGarrett Wollman */ 19694b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 1970b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 197157f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_RECEIVED); 1972a0292f23SGarrett Wollman } 1973df8bae1dSRodney W. Grimes 19748501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 19757cfc6904SRobert Watson 1976df8bae1dSRodney W. Grimes /* 1977fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1978df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1979df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1980df8bae1dSRodney W. Grimes */ 1981fb59c426SYoshinobu Inoue th->th_seq++; 1982fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1983fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1984df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1985fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1986fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 198778b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 198878b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 1989df8bae1dSRodney W. Grimes } 1990fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1991fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 1992a0292f23SGarrett Wollman /* 1993a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1994a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1995a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1996a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1997a0292f23SGarrett Wollman * Otherwise, goto step 6. 1998a0292f23SGarrett Wollman */ 1999fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 2000a0292f23SGarrett Wollman goto process_ACK; 2001c068736aSJeffrey Hsu 2002df8bae1dSRodney W. Grimes goto step6; 2003c068736aSJeffrey Hsu 2004a0292f23SGarrett Wollman /* 2005a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 2006c94c54e4SAndre Oppermann * do normal processing. 2007a0292f23SGarrett Wollman * 2008c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 2009a0292f23SGarrett Wollman */ 2010a0292f23SGarrett Wollman case TCPS_LAST_ACK: 2011a0292f23SGarrett Wollman case TCPS_CLOSING: 2012a0292f23SGarrett Wollman break; /* continue normal processing */ 2013df8bae1dSRodney W. Grimes } 2014df8bae1dSRodney W. Grimes 2015df8bae1dSRodney W. Grimes /* 2016df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 201780ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 201880ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 201980ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 202080ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 202180ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 202280ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 202380ab7c0eSGarrett Wollman * killing a connection). 202480ab7c0eSGarrett Wollman * Then check timestamp, if present. 2025a0292f23SGarrett Wollman * Then check the connection count, if present. 2026df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 2027df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 2028df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 202980ab7c0eSGarrett Wollman */ 2030fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 20313220a212SGleb Smirnoff /* 20323220a212SGleb Smirnoff * RFC5961 Section 3.2 20333220a212SGleb Smirnoff * 20343220a212SGleb Smirnoff * - RST drops connection only if SEG.SEQ == RCV.NXT. 20353220a212SGleb Smirnoff * - If RST is in window, we send challenge ACK. 20363220a212SGleb Smirnoff * 20373220a212SGleb Smirnoff * Note: to take into account delayed ACKs, we should 20383220a212SGleb Smirnoff * test against last_ack_sent instead of rcv_nxt. 20393220a212SGleb Smirnoff * Note 2: we handle special case of closed window, not 20403220a212SGleb Smirnoff * covered by the RFC. 20413220a212SGleb Smirnoff */ 20423220a212SGleb Smirnoff if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 20433220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) || 20443220a212SGleb Smirnoff (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) { 204580ab7c0eSGarrett Wollman 20463220a212SGleb Smirnoff KASSERT(tp->t_state != TCPS_SYN_SENT, 20473220a212SGleb Smirnoff ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p", 20483220a212SGleb Smirnoff __func__, th, tp)); 20493220a212SGleb Smirnoff 20503220a212SGleb Smirnoff if (V_tcp_insecure_rst || 20513220a212SGleb Smirnoff tp->last_ack_sent == th->th_seq) { 20523220a212SGleb Smirnoff TCPSTAT_INC(tcps_drops); 20533220a212SGleb Smirnoff /* Drop the connection. */ 20543220a212SGleb Smirnoff switch (tp->t_state) { 205580ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 205680ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 205780ab7c0eSGarrett Wollman goto close; 205880ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 205980ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 206080ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 206180ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 20626779a1a1SMichael Tuexen case TCPS_CLOSING: 20636779a1a1SMichael Tuexen case TCPS_LAST_ACK: 206480ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 206580ab7c0eSGarrett Wollman close: 20663220a212SGleb Smirnoff /* FALLTHROUGH */ 20673220a212SGleb Smirnoff default: 206880ab7c0eSGarrett Wollman tp = tcp_close(tp); 20693220a212SGleb Smirnoff } 20703220a212SGleb Smirnoff } else { 20713220a212SGleb Smirnoff TCPSTAT_INC(tcps_badrst); 20723220a212SGleb Smirnoff /* Send challenge ACK. */ 20733220a212SGleb Smirnoff tcp_respond(tp, mtod(m, void *), th, m, 20743220a212SGleb Smirnoff tp->rcv_nxt, tp->snd_nxt, TH_ACK); 20753220a212SGleb Smirnoff tp->last_ack_sent = tp->rcv_nxt; 20763220a212SGleb Smirnoff m = NULL; 20773220a212SGleb Smirnoff } 20783220a212SGleb Smirnoff } 20793220a212SGleb Smirnoff goto drop; 20803220a212SGleb Smirnoff } 208180ab7c0eSGarrett Wollman 20823220a212SGleb Smirnoff /* 20833220a212SGleb Smirnoff * RFC5961 Section 4.2 20843220a212SGleb Smirnoff * Send challenge ACK for any SYN in synchronized state. 20853220a212SGleb Smirnoff */ 2086281a0fd4SPatrick Kelsey if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT && 2087281a0fd4SPatrick Kelsey tp->t_state != TCPS_SYN_RECEIVED) { 20883220a212SGleb Smirnoff TCPSTAT_INC(tcps_badsyn); 20893220a212SGleb Smirnoff if (V_tcp_insecure_syn && 20903220a212SGleb Smirnoff SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 20913220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 20923220a212SGleb Smirnoff tp = tcp_drop(tp, ECONNRESET); 20933220a212SGleb Smirnoff rstreason = BANDLIM_UNLIMITED; 20943220a212SGleb Smirnoff } else { 20953220a212SGleb Smirnoff /* Send challenge ACK. */ 20963220a212SGleb Smirnoff tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt, 20973220a212SGleb Smirnoff tp->snd_nxt, TH_ACK); 20983220a212SGleb Smirnoff tp->last_ack_sent = tp->rcv_nxt; 20993220a212SGleb Smirnoff m = NULL; 210080ab7c0eSGarrett Wollman } 210180ab7c0eSGarrett Wollman goto drop; 210280ab7c0eSGarrett Wollman } 210380ab7c0eSGarrett Wollman 210480ab7c0eSGarrett Wollman /* 2105df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 2106df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 2107df8bae1dSRodney W. Grimes */ 2108be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 210980ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 2110df8bae1dSRodney W. Grimes 2111df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 2112d8951c8aSBjoern A. Zeeb if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 2113df8bae1dSRodney W. Grimes /* 2114df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 2115df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 2116df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 2117df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 2118df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 2119df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 2120df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 2121df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 2122df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 2123df8bae1dSRodney W. Grimes */ 2124df8bae1dSRodney W. Grimes tp->ts_recent = 0; 2125df8bae1dSRodney W. Grimes } else { 212678b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 212778b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 212878b50714SRobert Watson TCPSTAT_INC(tcps_pawsdrop); 212907fd333dSMatthew Dillon if (tlen) 2130df8bae1dSRodney W. Grimes goto dropafterack; 21311ab4789dSMatthew Dillon goto drop; 21321ab4789dSMatthew Dillon } 2133df8bae1dSRodney W. Grimes } 2134df8bae1dSRodney W. Grimes 2135a0292f23SGarrett Wollman /* 213680ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 213780ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 213880ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 213980ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 214080ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 214180ab7c0eSGarrett Wollman */ 2142a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 2143a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2144a57815efSBosko Milekic goto dropwithreset; 2145a57815efSBosko Milekic } 214680ab7c0eSGarrett Wollman 2147fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 2148df8bae1dSRodney W. Grimes if (todrop > 0) { 2149831ad37eSXin LI if (thflags & TH_SYN) { 2150fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 2151fb59c426SYoshinobu Inoue th->th_seq++; 2152fb59c426SYoshinobu Inoue if (th->th_urp > 1) 2153fb59c426SYoshinobu Inoue th->th_urp--; 2154df8bae1dSRodney W. Grimes else 2155fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2156df8bae1dSRodney W. Grimes todrop--; 2157df8bae1dSRodney W. Grimes } 2158df8bae1dSRodney W. Grimes /* 2159dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 2160df8bae1dSRodney W. Grimes */ 2161fb59c426SYoshinobu Inoue if (todrop > tlen 2162fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 2163dac20301SGarrett Wollman /* 2164dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 2165dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 2166dac20301SGarrett Wollman * of sequence; drop it. 2167dac20301SGarrett Wollman */ 2168fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2169dac20301SGarrett Wollman 2170df8bae1dSRodney W. Grimes /* 2171dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 2172dac20301SGarrett Wollman * But keep on processing for RST or ACK. 2173df8bae1dSRodney W. Grimes */ 2174dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2175fb59c426SYoshinobu Inoue todrop = tlen; 217678b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 217778b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 2178df8bae1dSRodney W. Grimes } else { 217978b50714SRobert Watson TCPSTAT_INC(tcps_rcvpartduppack); 218078b50714SRobert Watson TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 2181df8bae1dSRodney W. Grimes } 21825acfd95cSMichael Tuexen /* 21835acfd95cSMichael Tuexen * DSACK - add SACK block for dropped range 21845acfd95cSMichael Tuexen */ 21855acfd95cSMichael Tuexen if (tp->t_flags & TF_SACK_PERMIT) { 2186ecc5b1d1SMichael Tuexen tcp_update_sack_list(tp, th->th_seq, 2187ecc5b1d1SMichael Tuexen th->th_seq + todrop); 21885acfd95cSMichael Tuexen /* 21895acfd95cSMichael Tuexen * ACK now, as the next in-sequence segment 21905acfd95cSMichael Tuexen * will clear the DSACK block again 21915acfd95cSMichael Tuexen */ 21925acfd95cSMichael Tuexen tp->t_flags |= TF_ACKNOW; 21935acfd95cSMichael Tuexen } 2194fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 2195fb59c426SYoshinobu Inoue th->th_seq += todrop; 2196fb59c426SYoshinobu Inoue tlen -= todrop; 2197fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 2198fb59c426SYoshinobu Inoue th->th_urp -= todrop; 2199df8bae1dSRodney W. Grimes else { 2200fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2201fb59c426SYoshinobu Inoue th->th_urp = 0; 2202df8bae1dSRodney W. Grimes } 2203df8bae1dSRodney W. Grimes } 2204df8bae1dSRodney W. Grimes 2205df8bae1dSRodney W. Grimes /* 2206df8bae1dSRodney W. Grimes * If new data are received on a connection after the 2207df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 2208df8bae1dSRodney W. Grimes */ 2209df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 2210fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 221107dacf03SAndre Oppermann if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 221207dacf03SAndre Oppermann log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data " 221307dacf03SAndre Oppermann "after socket was closed, " 221407dacf03SAndre Oppermann "sending RST and removing tcpcb\n", 2215e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 2216773673c1SAndre Oppermann free(s, M_TCPLOG); 2217773673c1SAndre Oppermann } 2218df8bae1dSRodney W. Grimes tp = tcp_close(tp); 221978b50714SRobert Watson TCPSTAT_INC(tcps_rcvafterclose); 2220a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2221df8bae1dSRodney W. Grimes goto dropwithreset; 2222df8bae1dSRodney W. Grimes } 2223df8bae1dSRodney W. Grimes 2224df8bae1dSRodney W. Grimes /* 2225df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 2226df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 2227df8bae1dSRodney W. Grimes */ 2228fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 2229df8bae1dSRodney W. Grimes if (todrop > 0) { 223078b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 2231fb59c426SYoshinobu Inoue if (todrop >= tlen) { 223278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 2233df8bae1dSRodney W. Grimes /* 2234df8bae1dSRodney W. Grimes * If window is closed can only take segments at 2235df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 2236df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 2237df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 2238df8bae1dSRodney W. Grimes * and ack. 2239df8bae1dSRodney W. Grimes */ 2240fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2241df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 224278b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinprobe); 2243df8bae1dSRodney W. Grimes } else 2244df8bae1dSRodney W. Grimes goto dropafterack; 2245df8bae1dSRodney W. Grimes } else 224678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2247df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2248fb59c426SYoshinobu Inoue tlen -= todrop; 2249fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 2250df8bae1dSRodney W. Grimes } 2251df8bae1dSRodney W. Grimes 2252df8bae1dSRodney W. Grimes /* 2253df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 2254df8bae1dSRodney W. Grimes * record its timestamp. 2255cf09195bSPaul Saab * NOTE: 2256cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 2257a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2258cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 2259cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 2260cf09195bSPaul Saab * predicated on the sequence space of this segment. 2261cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 2262cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 2263cf09195bSPaul Saab * instead of RFC1323's 2264cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 2265cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 2266cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 2267cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 2268cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 2269df8bae1dSRodney W. Grimes */ 2270be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 2271cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2272cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 2273cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 2274d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 2275a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 2276df8bae1dSRodney W. Grimes } 2277df8bae1dSRodney W. Grimes 2278df8bae1dSRodney W. Grimes /* 2279a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 2280a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 2281a0292f23SGarrett Wollman * later processing; else drop segment and return. 2282a0292f23SGarrett Wollman */ 2283fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 2284a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 2285281a0fd4SPatrick Kelsey (tp->t_flags & TF_NEEDSYN)) { 2286281a0fd4SPatrick Kelsey if (tp->t_state == TCPS_SYN_RECEIVED && 228768bd7ed1SJonathan T. Looney IS_FASTOPEN(tp->t_flags)) { 2288281a0fd4SPatrick Kelsey tp->snd_wnd = tiwin; 2289281a0fd4SPatrick Kelsey cc_conn_init(tp); 2290281a0fd4SPatrick Kelsey } 2291a0292f23SGarrett Wollman goto step6; 2292281a0fd4SPatrick Kelsey } else if (tp->t_flags & TF_ACKNOW) 22935e1aa279SDavid Malone goto dropafterack; 2294a0292f23SGarrett Wollman else 2295a0292f23SGarrett Wollman goto drop; 2296a0292f23SGarrett Wollman } 2297df8bae1dSRodney W. Grimes 2298df8bae1dSRodney W. Grimes /* 2299df8bae1dSRodney W. Grimes * Ack processing. 2300df8bae1dSRodney W. Grimes */ 2301df8bae1dSRodney W. Grimes switch (tp->t_state) { 2302df8bae1dSRodney W. Grimes 2303df8bae1dSRodney W. Grimes /* 2304764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 2305764d8cefSBill Fenner * ESTABLISHED state and continue processing. 2306764d8cefSBill Fenner * The ACK was checked above. 2307df8bae1dSRodney W. Grimes */ 2308df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2309a0292f23SGarrett Wollman 231078b50714SRobert Watson TCPSTAT_INC(tcps_connects); 2311df8bae1dSRodney W. Grimes soisconnected(so); 2312df8bae1dSRodney W. Grimes /* Do window scaling? */ 2313df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2314df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2315df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 2316df8bae1dSRodney W. Grimes } 2317116ef4d6SMichael Tuexen tp->snd_wnd = tiwin; 2318a0292f23SGarrett Wollman /* 2319a0292f23SGarrett Wollman * Make transitions: 2320a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 2321a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 2322a0292f23SGarrett Wollman */ 23239b8b58e0SJonathan Lemon tp->t_starttime = ticks; 232418a75309SPatrick Kelsey if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) { 2325281a0fd4SPatrick Kelsey tcp_fastopen_decrement_counter(tp->t_tfo_pending); 2326281a0fd4SPatrick Kelsey tp->t_tfo_pending = NULL; 2327281a0fd4SPatrick Kelsey 2328281a0fd4SPatrick Kelsey /* 2329281a0fd4SPatrick Kelsey * Account for the ACK of our SYN prior to 2330281a0fd4SPatrick Kelsey * regular ACK processing below. 2331281a0fd4SPatrick Kelsey */ 2332281a0fd4SPatrick Kelsey tp->snd_una++; 2333281a0fd4SPatrick Kelsey } 23348db239dcSMichael Tuexen if (tp->t_flags & TF_NEEDFIN) { 23358db239dcSMichael Tuexen tcp_state_change(tp, TCPS_FIN_WAIT_1); 23368db239dcSMichael Tuexen tp->t_flags &= ~TF_NEEDFIN; 23378db239dcSMichael Tuexen } else { 23388db239dcSMichael Tuexen tcp_state_change(tp, TCPS_ESTABLISHED); 23398db239dcSMichael Tuexen TCP_PROBE5(accept__established, NULL, tp, 23408db239dcSMichael Tuexen m, tp, th); 2341281a0fd4SPatrick Kelsey /* 2342281a0fd4SPatrick Kelsey * TFO connections call cc_conn_init() during SYN 2343281a0fd4SPatrick Kelsey * processing. Calling it again here for such 2344281a0fd4SPatrick Kelsey * connections is not harmless as it would undo the 2345281a0fd4SPatrick Kelsey * snd_cwnd reduction that occurs when a TFO SYN|ACK 2346281a0fd4SPatrick Kelsey * is retransmitted. 2347281a0fd4SPatrick Kelsey */ 234868bd7ed1SJonathan T. Looney if (!IS_FASTOPEN(tp->t_flags)) 2349dbc42409SLawrence Stewart cc_conn_init(tp); 23509077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 23517ff19458SPaul Traina } 2352a0292f23SGarrett Wollman /* 2353a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 2354a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 2355a0292f23SGarrett Wollman */ 2356fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 2357c28440dbSRandall Stewart (void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0, 2358a0292f23SGarrett Wollman (struct mbuf *)0); 235960ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq - 1; 236093b0017fSPhilippe Charnier /* FALLTHROUGH */ 2361df8bae1dSRodney W. Grimes 2362df8bae1dSRodney W. Grimes /* 2363df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2364df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 2365fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 2366fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 2367df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 2368df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 2369df8bae1dSRodney W. Grimes */ 2370df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2371df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2372df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2373df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 2374df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2375df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 23765a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 237778b50714SRobert Watson TCPSTAT_INC(tcps_rcvacktoomuch); 23785a53ca16SPaul Saab goto dropafterack; 23795a53ca16SPaul Saab } 23803529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2381fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 2382fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 2383021eaf79SHiren Panchasara sack_changed = tcp_sack_doack(tp, &to, th->th_ack); 238412eeb81fSHiren Panchasara else 238512eeb81fSHiren Panchasara /* 238612eeb81fSHiren Panchasara * Reset the value so that previous (valid) value 238712eeb81fSHiren Panchasara * from the last ack with SACK doesn't get used. 238812eeb81fSHiren Panchasara */ 238912eeb81fSHiren Panchasara tp->sackhint.sacked_bytes = 0; 239039bc9de5SLawrence Stewart 2391bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 239239bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 239339bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 2394bd79708dSJonathan T. Looney #endif 239539bc9de5SLawrence Stewart 2396fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 23970c39d38dSGleb Smirnoff u_int maxseg; 23980c39d38dSGleb Smirnoff 23990c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 2400021eaf79SHiren Panchasara if (tlen == 0 && 2401021eaf79SHiren Panchasara (tiwin == tp->snd_wnd || 2402021eaf79SHiren Panchasara (tp->t_flags & TF_SACK_PERMIT))) { 240362b90589SPeter Wemm /* 240462b90589SPeter Wemm * If this is the first time we've seen a 240562b90589SPeter Wemm * FIN from the remote, this is not a 240662b90589SPeter Wemm * duplicate and it needs to be processed 240762b90589SPeter Wemm * normally. This happens during a 240862b90589SPeter Wemm * simultaneous close. 240962b90589SPeter Wemm */ 241062b90589SPeter Wemm if ((thflags & TH_FIN) && 241162b90589SPeter Wemm (TCPS_HAVERCVDFIN(tp->t_state) == 0)) { 241262b90589SPeter Wemm tp->t_dupacks = 0; 241362b90589SPeter Wemm break; 241462b90589SPeter Wemm } 241578b50714SRobert Watson TCPSTAT_INC(tcps_rcvdupack); 2416df8bae1dSRodney W. Grimes /* 2417df8bae1dSRodney W. Grimes * If we have outstanding data (other than 2418df8bae1dSRodney W. Grimes * a window probe), this is a completely 2419df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 24205f30ec9bSEitan Adler * change and FIN isn't set), 24215f30ec9bSEitan Adler * the ack is the biggest we've 2422df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 2423a4641f4eSPedro F. Giffuni * threshold of them, assume a packet 2424df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 2425df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 2426df8bae1dSRodney W. Grimes * window so we send only this one 2427df8bae1dSRodney W. Grimes * packet. 2428df8bae1dSRodney W. Grimes * 2429df8bae1dSRodney W. Grimes * We know we're losing at the current 2430df8bae1dSRodney W. Grimes * window size so do congestion avoidance 2431df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 2432df8bae1dSRodney W. Grimes * and pull our congestion window back to 2433df8bae1dSRodney W. Grimes * the new ssthresh). 2434df8bae1dSRodney W. Grimes * 2435df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 2436df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 2437df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 2438df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 2439df8bae1dSRodney W. Grimes * network. 2440f2512ba1SRui Paulo * 2441f2512ba1SRui Paulo * When using TCP ECN, notify the peer that 2442f2512ba1SRui Paulo * we reduced the cwnd. 2443df8bae1dSRodney W. Grimes */ 2444021eaf79SHiren Panchasara /* 2445021eaf79SHiren Panchasara * Following 2 kinds of acks should not affect 2446021eaf79SHiren Panchasara * dupack counting: 2447021eaf79SHiren Panchasara * 1) Old acks 2448021eaf79SHiren Panchasara * 2) Acks with SACK but without any new SACK 2449021eaf79SHiren Panchasara * information in them. These could result from 2450021eaf79SHiren Panchasara * any anomaly in the network like a switch 2451021eaf79SHiren Panchasara * duplicating packets or a possible DoS attack. 2452021eaf79SHiren Panchasara */ 2453021eaf79SHiren Panchasara if (th->th_ack != tp->snd_una || 2454021eaf79SHiren Panchasara ((tp->t_flags & TF_SACK_PERMIT) && 2455021eaf79SHiren Panchasara !sack_changed)) 2456021eaf79SHiren Panchasara break; 2457021eaf79SHiren Panchasara else if (!tcp_timer_active(tp, TT_REXMT)) 2458df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2459cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 2460dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 24614b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 24624b7b743cSLawrence Stewart CC_DUPACK); 24633529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2464dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2465808f11b7SPaul Saab int awnd; 246625e6f9edSPaul Saab 246725e6f9edSPaul Saab /* 246825e6f9edSPaul Saab * Compute the amount of data in flight first. 24694c3972f0SHiren Panchasara * We can inject new data into the pipe iff 247025e6f9edSPaul Saab * we have less than 1/2 the original window's 247125e6f9edSPaul Saab * worth of data in flight. 247225e6f9edSPaul Saab */ 247312eeb81fSHiren Panchasara if (V_tcp_do_rfc6675_pipe) 247412eeb81fSHiren Panchasara awnd = tcp_compute_pipe(tp); 247512eeb81fSHiren Panchasara else 2476808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 24770077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 247812eeb81fSHiren Panchasara 2479808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 24800c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 248125e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 248225e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 248325e6f9edSPaul Saab } 248425e6f9edSPaul Saab } else 24850c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 248655bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 248746f58482SJonathan Lemon goto drop; 2488cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 2489cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 2490a0445c2eSJayanth Vijayaraghavan 2491a0445c2eSJayanth Vijayaraghavan /* 2492a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 2493a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 2494a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 2495a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 2496a0445c2eSJayanth Vijayaraghavan * recovery. 2497a0445c2eSJayanth Vijayaraghavan */ 24983529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 2499dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2500a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 2501a0445c2eSJayanth Vijayaraghavan break; 2502a0445c2eSJayanth Vijayaraghavan } 2503dbc42409SLawrence Stewart } else { 2504a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 25059d11646dSJeffrey Hsu tp->snd_recover)) { 2506cb942153SJeffrey Hsu tp->t_dupacks = 0; 2507cb942153SJeffrey Hsu break; 250846f58482SJonathan Lemon } 2509a0445c2eSJayanth Vijayaraghavan } 2510dbc42409SLawrence Stewart /* Congestion signal before ack. */ 2511dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_NDUPACK); 25124b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 25134b7b743cSLawrence Stewart CC_DUPACK); 2514b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 25159b8b58e0SJonathan Lemon tp->t_rtttime = 0; 25163529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 251778b50714SRobert Watson TCPSTAT_INC( 251878b50714SRobert Watson tcps_sack_recovery_episode); 2519a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 25200c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 252155bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 25226d90faf3SPaul Saab goto drop; 25236d90faf3SPaul Saab } 2524fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 25250c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 252655bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 252748d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 2528302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 2529302ce8d6SAndre Oppermann __func__)); 2530df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 25310c39d38dSGleb Smirnoff maxseg * 253248d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 2533df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 2534df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 2535df8bae1dSRodney W. Grimes goto drop; 2536603724d3SBjoern A. Zeeb } else if (V_tcp_do_rfc3042) { 253762d4443fSHiren Panchasara /* 253862d4443fSHiren Panchasara * Process first and second duplicate 253962d4443fSHiren Panchasara * ACKs. Each indicates a segment 254062d4443fSHiren Panchasara * leaving the network, creating room 254162d4443fSHiren Panchasara * for more. Make sure we can send a 254262d4443fSHiren Panchasara * packet on reception of each duplicate 254362d4443fSHiren Panchasara * ACK by increasing snd_cwnd by one 254462d4443fSHiren Panchasara * segment. Restore the original 254562d4443fSHiren Panchasara * snd_cwnd after packet transmission. 254662d4443fSHiren Panchasara */ 25474b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 25484b7b743cSLawrence Stewart CC_DUPACK); 25493ac12506SJonathan T. Looney uint32_t oldcwnd = tp->snd_cwnd; 255048d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 255148d2549cSJeffrey Hsu u_int sent; 25525628dd08SAndre Oppermann int avail; 255389c02376SJeffrey Hsu 2554582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 2555582a954bSJeffrey Hsu tp->t_dupacks == 2, 2556302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 2557302ce8d6SAndre Oppermann __func__)); 255861a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 255948d2549cSJeffrey Hsu tp->snd_limited = 0; 256061a36e3dSJeffrey Hsu tp->snd_cwnd = 256161a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 256261a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 25630c39d38dSGleb Smirnoff maxseg; 25645628dd08SAndre Oppermann /* 25655628dd08SAndre Oppermann * Only call tcp_output when there 25665628dd08SAndre Oppermann * is new data available to be sent. 25675628dd08SAndre Oppermann * Otherwise we would send pure ACKs. 25685628dd08SAndre Oppermann */ 25695628dd08SAndre Oppermann SOCKBUF_LOCK(&so->so_snd); 2570cfa6009eSGleb Smirnoff avail = sbavail(&so->so_snd) - 25715628dd08SAndre Oppermann (tp->snd_nxt - tp->snd_una); 25725628dd08SAndre Oppermann SOCKBUF_UNLOCK(&so->so_snd); 25735628dd08SAndre Oppermann if (avail > 0) 257455bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 257548d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 25760c39d38dSGleb Smirnoff if (sent > maxseg) { 257789c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 257889c02376SJeffrey Hsu tp->snd_limited == 0) || 25790c39d38dSGleb Smirnoff (sent == maxseg + 1 && 258089c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 2581302ce8d6SAndre Oppermann ("%s: sent too much", 2582302ce8d6SAndre Oppermann __func__)); 258348d2549cSJeffrey Hsu tp->snd_limited = 2; 258448d2549cSJeffrey Hsu } else if (sent > 0) 258548d2549cSJeffrey Hsu ++tp->snd_limited; 2586582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 2587582a954bSJeffrey Hsu goto drop; 2588df8bae1dSRodney W. Grimes } 2589021eaf79SHiren Panchasara } 2590df8bae1dSRodney W. Grimes break; 2591021eaf79SHiren Panchasara } else { 2592021eaf79SHiren Panchasara /* 2593021eaf79SHiren Panchasara * This ack is advancing the left edge, reset the 2594021eaf79SHiren Panchasara * counter. 2595021eaf79SHiren Panchasara */ 2596021eaf79SHiren Panchasara tp->t_dupacks = 0; 2597021eaf79SHiren Panchasara /* 2598021eaf79SHiren Panchasara * If this ack also has new SACK info, increment the 2599021eaf79SHiren Panchasara * counter as per rfc6675. 2600021eaf79SHiren Panchasara */ 2601021eaf79SHiren Panchasara if ((tp->t_flags & TF_SACK_PERMIT) && sack_changed) 2602021eaf79SHiren Panchasara tp->t_dupacks++; 2603df8bae1dSRodney W. Grimes } 2604cb942153SJeffrey Hsu 2605302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2606302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 2607cb942153SJeffrey Hsu 2608df8bae1dSRodney W. Grimes /* 2609df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2610df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2611df8bae1dSRodney W. Grimes */ 2612dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2613cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 26143529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 26156d90faf3SPaul Saab tcp_sack_partialack(tp, th); 26166d90faf3SPaul Saab else 2617c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2618dbc42409SLawrence Stewart } else 2619dbc42409SLawrence Stewart cc_post_recovery(tp, th); 262046f58482SJonathan Lemon } 2621a0292f23SGarrett Wollman /* 2622a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2623a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2624a0292f23SGarrett Wollman */ 2625a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2626a0292f23SGarrett Wollman /* 2627a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2628a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 262907e43e10SAndras Olah * synchronized). Go to non-starred state, 263007e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 263107e43e10SAndras Olah * we can do window scaling. 2632a0292f23SGarrett Wollman */ 2633a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2634a0292f23SGarrett Wollman tp->snd_una++; 263507e43e10SAndras Olah /* Do window scaling? */ 263607e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 263707e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 263807e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2639464fcfbcSAndre Oppermann /* Send window already scaled. */ 264007e43e10SAndras Olah } 2641a0292f23SGarrett Wollman } 2642a0292f23SGarrett Wollman 2643a0292f23SGarrett Wollman process_ACK: 26448501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 26457cfc6904SRobert Watson 2646dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 2647b8c2cd15SJonathan T. Looney KASSERT(acked >= 0, ("%s: acked unexepectedly negative " 2648b8c2cd15SJonathan T. Looney "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__, 2649b8c2cd15SJonathan T. Looney tp->snd_una, th->th_ack, tp, m)); 26504b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvackpack, nsegs); 265178b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 2652df8bae1dSRodney W. Grimes 2653df8bae1dSRodney W. Grimes /* 26549b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 26559b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 26569b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 26579b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 26589b8b58e0SJonathan Lemon * we left off. 26599b8b58e0SJonathan Lemon */ 266010d20c84SMatt Macy if (tp->t_rxtshift == 1 && 266110d20c84SMatt Macy tp->t_flags & TF_PREVVALID && 266210d20c84SMatt Macy tp->t_badrxtwin && 266310d20c84SMatt Macy SEQ_LT(to.to_tsecr, tp->t_badrxtwin)) 2664dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 26659b8b58e0SJonathan Lemon 26669b8b58e0SJonathan Lemon /* 2667df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2668df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2669df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2670df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2671df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2672df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2673df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2674fa55172bSMatthew Dillon * 2675fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2676fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2677fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2678fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2679df8bae1dSRodney W. Grimes */ 2680d8951c8aSBjoern A. Zeeb if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) { 26813ac12506SJonathan T. Looney uint32_t t; 2682d8951c8aSBjoern A. Zeeb 2683d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 2684d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 2685d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 2686d8951c8aSBjoern A. Zeeb tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1); 2687fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2688eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2689eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 26909b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2691fa55172bSMatthew Dillon } 2692df8bae1dSRodney W. Grimes 2693df8bae1dSRodney W. Grimes /* 2694df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2695df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2696df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2697df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2698df8bae1dSRodney W. Grimes */ 2699fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 2700b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2701df8bae1dSRodney W. Grimes needoutput = 1; 2702b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2703b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2704a0292f23SGarrett Wollman 2705a0292f23SGarrett Wollman /* 2706a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2707a0292f23SGarrett Wollman * skip rest of ACK processing. 2708a0292f23SGarrett Wollman */ 2709a0292f23SGarrett Wollman if (acked == 0) 2710a0292f23SGarrett Wollman goto step6; 2711a0292f23SGarrett Wollman 2712df8bae1dSRodney W. Grimes /* 2713dbc42409SLawrence Stewart * Let the congestion control algorithm update congestion 2714dbc42409SLawrence Stewart * control related information. This typically means increasing 2715dbc42409SLawrence Stewart * the congestion window. 2716df8bae1dSRodney W. Grimes */ 27174b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, CC_ACK); 2718dbc42409SLawrence Stewart 27195905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2720cfa6009eSGleb Smirnoff if (acked > sbavail(&so->so_snd)) { 2721b8c2cd15SJonathan T. Looney if (tp->snd_wnd >= sbavail(&so->so_snd)) 2722cfa6009eSGleb Smirnoff tp->snd_wnd -= sbavail(&so->so_snd); 2723b8c2cd15SJonathan T. Looney else 2724b8c2cd15SJonathan T. Looney tp->snd_wnd = 0; 2725c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, 2726cfa6009eSGleb Smirnoff (int)sbavail(&so->so_snd)); 2727df8bae1dSRodney W. Grimes ourfinisacked = 1; 2728df8bae1dSRodney W. Grimes } else { 2729c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, acked); 27303ac12506SJonathan T. Looney if (tp->snd_wnd >= (uint32_t) acked) 273160ee3bb2SAndre Oppermann tp->snd_wnd -= acked; 2732b8c2cd15SJonathan T. Looney else 2733b8c2cd15SJonathan T. Looney tp->snd_wnd = 0; 2734df8bae1dSRodney W. Grimes ourfinisacked = 0; 2735df8bae1dSRodney W. Grimes } 2736564aab1fSAndre Oppermann /* NB: sowwakeup_locked() does an implicit unlock. */ 27371e4d7da7SRobert Watson sowwakeup_locked(so); 2738c11a15bfSGleb Smirnoff m_freem(mfree); 2739e8949f74SAndre Oppermann /* Detect una wraparound. */ 2740dbc42409SLawrence Stewart if (!IN_RECOVERY(tp->t_flags) && 27419d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 27429d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 27439d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 2744dbc42409SLawrence Stewart /* XXXLAS: Can this be moved up into cc_post_recovery? */ 2745dbc42409SLawrence Stewart if (IN_RECOVERY(tp->t_flags) && 274624cb0f22SLawrence Stewart SEQ_GEQ(th->th_ack, tp->snd_recover)) { 2747dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 274824cb0f22SLawrence Stewart } 2749fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 27503529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 27516d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 27526d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 27536d90faf3SPaul Saab } 2754df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2755df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2756df8bae1dSRodney W. Grimes 2757df8bae1dSRodney W. Grimes switch (tp->t_state) { 2758df8bae1dSRodney W. Grimes 2759df8bae1dSRodney W. Grimes /* 2760df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2761df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2762df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2763df8bae1dSRodney W. Grimes */ 2764df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2765df8bae1dSRodney W. Grimes if (ourfinisacked) { 2766df8bae1dSRodney W. Grimes /* 2767df8bae1dSRodney W. Grimes * If we can't receive any more 2768df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2769df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2770df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2771df8bae1dSRodney W. Grimes * we'll hang forever. 2772e8949f74SAndre Oppermann * 2773e8949f74SAndre Oppermann * XXXjl: 2774340c35deSJonathan Lemon * we should release the tp also, and use a 2775340c35deSJonathan Lemon * compressed state. 2776340c35deSJonathan Lemon */ 2777c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 277803e49181SSeigo Tanimura soisdisconnected(so); 27799077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 27809077f387SGleb Smirnoff (tcp_fast_finwait2_recycle ? 27819077f387SGleb Smirnoff tcp_finwait2_timeout : 27829077f387SGleb Smirnoff TP_MAXIDLE(tp))); 27834cc20ab1SSeigo Tanimura } 278457f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_2); 2785df8bae1dSRodney W. Grimes } 2786df8bae1dSRodney W. Grimes break; 2787df8bae1dSRodney W. Grimes 2788df8bae1dSRodney W. Grimes /* 2789df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2790df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2791df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2792df8bae1dSRodney W. Grimes * the segment. 2793df8bae1dSRodney W. Grimes */ 2794df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2795df8bae1dSRodney W. Grimes if (ourfinisacked) { 279611a20fb8SJeffrey Hsu tcp_twstart(tp); 2797340c35deSJonathan Lemon m_freem(m); 2798679d9708SAndre Oppermann return; 2799df8bae1dSRodney W. Grimes } 2800df8bae1dSRodney W. Grimes break; 2801df8bae1dSRodney W. Grimes 2802df8bae1dSRodney W. Grimes /* 2803df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2804df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2805df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2806df8bae1dSRodney W. Grimes * enter the closed state and return. 2807df8bae1dSRodney W. Grimes */ 2808df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2809df8bae1dSRodney W. Grimes if (ourfinisacked) { 2810df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2811df8bae1dSRodney W. Grimes goto drop; 2812df8bae1dSRodney W. Grimes } 2813df8bae1dSRodney W. Grimes break; 2814df8bae1dSRodney W. Grimes } 2815df8bae1dSRodney W. Grimes } 2816df8bae1dSRodney W. Grimes 2817df8bae1dSRodney W. Grimes step6: 28188501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 28197cfc6904SRobert Watson 2820df8bae1dSRodney W. Grimes /* 282160ee3bb2SAndre Oppermann * Update window information. 282260ee3bb2SAndre Oppermann * Don't look at window if no ACK: TAC's send garbage on first SYN. 2823df8bae1dSRodney W. Grimes */ 282460ee3bb2SAndre Oppermann if ((thflags & TH_ACK) && 282560ee3bb2SAndre Oppermann (SEQ_LT(tp->snd_wl1, th->th_seq) || 282660ee3bb2SAndre Oppermann (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 282760ee3bb2SAndre Oppermann (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 282860ee3bb2SAndre Oppermann /* keep track of pure window updates */ 282960ee3bb2SAndre Oppermann if (tlen == 0 && 283060ee3bb2SAndre Oppermann tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 283178b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinupd); 2832df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 283360ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 283460ee3bb2SAndre Oppermann tp->snd_wl2 = th->th_ack; 2835df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2836df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 283760ee3bb2SAndre Oppermann needoutput = 1; 2838df8bae1dSRodney W. Grimes } 2839df8bae1dSRodney W. Grimes 2840df8bae1dSRodney W. Grimes /* 2841df8bae1dSRodney W. Grimes * Process segments with URG. 2842df8bae1dSRodney W. Grimes */ 2843fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2844df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2845df8bae1dSRodney W. Grimes /* 2846df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2847df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2848df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2849df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2850df8bae1dSRodney W. Grimes */ 285118ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2852cfa6009eSGleb Smirnoff if (th->th_urp + sbavail(&so->so_rcv) > sb_max) { 2853fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2854fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 285518ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2856df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2857df8bae1dSRodney W. Grimes } 2858df8bae1dSRodney W. Grimes /* 2859df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2860df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2861df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2862df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2863df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2864df8bae1dSRodney W. Grimes * 2865df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2866df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2867df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2868df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2869df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2870df8bae1dSRodney W. Grimes * spec states (in one of two places). 2871df8bae1dSRodney W. Grimes */ 2872fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2873fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2874cfa6009eSGleb Smirnoff so->so_oobmark = sbavail(&so->so_rcv) + 2875df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2876927c5ceaSRobert Watson if (so->so_oobmark == 0) 2877c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 2878df8bae1dSRodney W. Grimes sohasoutofband(so); 2879df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2880df8bae1dSRodney W. Grimes } 288118ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2882df8bae1dSRodney W. Grimes /* 2883df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2884df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2885df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2886df8bae1dSRodney W. Grimes * data may creep in... ick. 2887df8bae1dSRodney W. Grimes */ 28883ac12506SJonathan T. Looney if (th->th_urp <= (uint32_t)tlen && 288930613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 289030613f56SJeffrey Hsu /* hdr drop is delayed */ 289130613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 289230613f56SJeffrey Hsu } 2893c068736aSJeffrey Hsu } else { 2894df8bae1dSRodney W. Grimes /* 2895df8bae1dSRodney W. Grimes * If no out of band data is expected, 2896df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2897df8bae1dSRodney W. Grimes * with the receive window. 2898df8bae1dSRodney W. Grimes */ 2899df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2900df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2901c068736aSJeffrey Hsu } 2902df8bae1dSRodney W. Grimes dodata: /* XXX */ 29038501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 29047cfc6904SRobert Watson 2905df8bae1dSRodney W. Grimes /* 2906df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2907df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2908df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2909df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2910df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2911df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2912df8bae1dSRodney W. Grimes */ 2913281a0fd4SPatrick Kelsey tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) && 291468bd7ed1SJonathan T. Looney IS_FASTOPEN(tp->t_flags)); 2915281a0fd4SPatrick Kelsey if ((tlen || (thflags & TH_FIN) || tfo_syn) && 2916df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 291769e03620SPaul Saab tcp_seq save_start = th->th_seq; 29185acfd95cSMichael Tuexen tcp_seq save_rnxt = tp->rcv_nxt; 29195acfd95cSMichael Tuexen int save_tlen = tlen; 2920fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2921e4b64281SJesper Skriver /* 2922c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2923c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2924c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2925c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2926c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2927c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2928c068736aSJeffrey Hsu * conversions. 2929c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 2930c068736aSJeffrey Hsu * immediately when segments are out of order (so 2931c068736aSJeffrey Hsu * fast retransmit can work). 2932e4b64281SJesper Skriver */ 29334741bfcbSPatrick Kelsey if (th->th_seq == tp->rcv_nxt && 2934c28440dbSRandall Stewart SEGQ_EMPTY(tp) && 2935281a0fd4SPatrick Kelsey (TCPS_HAVEESTABLISHED(tp->t_state) || 2936281a0fd4SPatrick Kelsey tfo_syn)) { 2937281a0fd4SPatrick Kelsey if (DELAY_ACK(tp, tlen) || tfo_syn) 29383bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2939e4b64281SJesper Skriver else 2940e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2941e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2942e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 294378b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 294478b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 29451e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2946c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2947c1c36a2cSMike Silbersack m_freem(m); 2948c1c36a2cSMike Silbersack else 2949651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 2950e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 29511e4d7da7SRobert Watson sorwakeup_locked(so); 2952e4b64281SJesper Skriver } else { 2953e8949f74SAndre Oppermann /* 2954e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 2955e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 2956e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 2957e8949f74SAndre Oppermann * when trimming from the head. 2958e8949f74SAndre Oppermann */ 29595acfd95cSMichael Tuexen tcp_seq temp = save_start; 29605acfd95cSMichael Tuexen thflags = tcp_reass(tp, th, &temp, &tlen, m); 2961e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2962e4b64281SJesper Skriver } 29636d261981SMichael Tuexen if ((tp->t_flags & TF_SACK_PERMIT) && (save_tlen > 0)) { 29646d261981SMichael Tuexen if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) { 2965b5a154d8SMichael Tuexen /* 2966b5a154d8SMichael Tuexen * DSACK actually handled in the fastpath 2967b5a154d8SMichael Tuexen * above. 2968b5a154d8SMichael Tuexen */ 2969ecc5b1d1SMichael Tuexen tcp_update_sack_list(tp, save_start, 2970ecc5b1d1SMichael Tuexen save_start + save_tlen); 2971ecc5b1d1SMichael Tuexen } else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) { 2972ecc5b1d1SMichael Tuexen if ((tp->rcv_numsacks >= 1) && 2973ecc5b1d1SMichael Tuexen (tp->sackblks[0].end == save_start)) { 29746d261981SMichael Tuexen /* 29756d261981SMichael Tuexen * Partial overlap, recorded at todrop 29766d261981SMichael Tuexen * above. 29776d261981SMichael Tuexen */ 29786d261981SMichael Tuexen tcp_update_sack_list(tp, 29796d261981SMichael Tuexen tp->sackblks[0].start, 2980ecc5b1d1SMichael Tuexen tp->sackblks[0].end); 2981ecc5b1d1SMichael Tuexen } else { 2982ecc5b1d1SMichael Tuexen tcp_update_dsack_list(tp, save_start, 2983ecc5b1d1SMichael Tuexen save_start + save_tlen); 2984ecc5b1d1SMichael Tuexen } 29856d261981SMichael Tuexen } else if (tlen >= save_tlen) { 2986b5a154d8SMichael Tuexen /* Update of sackblks. */ 2987ecc5b1d1SMichael Tuexen tcp_update_dsack_list(tp, save_start, 2988ecc5b1d1SMichael Tuexen save_start + save_tlen); 2989ecc5b1d1SMichael Tuexen } else if (tlen > 0) { 2990ecc5b1d1SMichael Tuexen tcp_update_dsack_list(tp, save_start, 2991ecc5b1d1SMichael Tuexen save_start + tlen); 29925acfd95cSMichael Tuexen } 29935acfd95cSMichael Tuexen } 2994302ce8d6SAndre Oppermann #if 0 2995df8bae1dSRodney W. Grimes /* 2996df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2997df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2998df8bae1dSRodney W. Grimes * buffer size. 2999302ce8d6SAndre Oppermann * XXX: Unused. 3000df8bae1dSRodney W. Grimes */ 3001f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) 3002df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 3003f701e30dSJohn Baldwin else 3004f701e30dSJohn Baldwin len = so->so_rcv.sb_hiwat; 3005302ce8d6SAndre Oppermann #endif 3006df8bae1dSRodney W. Grimes } else { 3007df8bae1dSRodney W. Grimes m_freem(m); 3008fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 3009df8bae1dSRodney W. Grimes } 3010df8bae1dSRodney W. Grimes 3011df8bae1dSRodney W. Grimes /* 3012df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 3013df8bae1dSRodney W. Grimes * that the connection is closing. 3014df8bae1dSRodney W. Grimes */ 3015fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 3016df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 3017df8bae1dSRodney W. Grimes socantrcvmore(so); 3018a0292f23SGarrett Wollman /* 3019a0292f23SGarrett Wollman * If connection is half-synchronized 3020d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 3021a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 3022a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 3023a0292f23SGarrett Wollman * more input can be expected, send ACK now. 3024a0292f23SGarrett Wollman */ 30253bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 30263bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 3027a0292f23SGarrett Wollman else 3028df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 3029df8bae1dSRodney W. Grimes tp->rcv_nxt++; 3030df8bae1dSRodney W. Grimes } 3031df8bae1dSRodney W. Grimes switch (tp->t_state) { 3032df8bae1dSRodney W. Grimes 3033df8bae1dSRodney W. Grimes /* 3034df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 3035df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 3036df8bae1dSRodney W. Grimes */ 3037df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 30389b8b58e0SJonathan Lemon tp->t_starttime = ticks; 30399b8b58e0SJonathan Lemon /* FALLTHROUGH */ 3040df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 304157f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSE_WAIT); 3042df8bae1dSRodney W. Grimes break; 3043df8bae1dSRodney W. Grimes 3044df8bae1dSRodney W. Grimes /* 3045df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 3046df8bae1dSRodney W. Grimes * enter the CLOSING state. 3047df8bae1dSRodney W. Grimes */ 3048df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 304957f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSING); 3050df8bae1dSRodney W. Grimes break; 3051df8bae1dSRodney W. Grimes 3052df8bae1dSRodney W. Grimes /* 3053df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 3054df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 3055df8bae1dSRodney W. Grimes * standard timers. 3056df8bae1dSRodney W. Grimes */ 3057df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 3058340c35deSJonathan Lemon tcp_twstart(tp); 3059679d9708SAndre Oppermann return; 3060df8bae1dSRodney W. Grimes } 3061df8bae1dSRodney W. Grimes } 3062610ee2f9SDavid Greenman #ifdef TCPDEBUG 30634cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3064fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 3065fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3066610ee2f9SDavid Greenman #endif 30672b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 3068df8bae1dSRodney W. Grimes 3069df8bae1dSRodney W. Grimes /* 3070df8bae1dSRodney W. Grimes * Return any desired output. 3071df8bae1dSRodney W. Grimes */ 3072df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 307355bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 30747792ea27SJeffrey Hsu 3075a14c749fSJonathan Lemon check_delack: 30768501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 3077252ca428SRobert Watson 3078a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 30793bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 3080b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 30813bfd6421SJonathan Lemon } 30828501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3083679d9708SAndre Oppermann return; 3084df8bae1dSRodney W. Grimes 3085df8bae1dSRodney W. Grimes dropafterack: 3086df8bae1dSRodney W. Grimes /* 3087df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 3088df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 308980ab7c0eSGarrett Wollman * 309080ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 309180ab7c0eSGarrett Wollman * paths to this code happen after packets containing 309280ab7c0eSGarrett Wollman * RST have been dropped. 309380ab7c0eSGarrett Wollman * 309480ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 309580ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 309680ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 309780ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 309880ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 309980ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 3100df8bae1dSRodney W. Grimes */ 3101fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 3102fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 3103a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 3104a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 3105a57815efSBosko Milekic goto dropwithreset; 3106a57815efSBosko Milekic } 3107a0292f23SGarrett Wollman #ifdef TCPDEBUG 31084cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3109fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3110fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3111a0292f23SGarrett Wollman #endif 31122b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 3113df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 311455bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 31158501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3116d6915262SRobert Watson m_freem(m); 3117679d9708SAndre Oppermann return; 3118df8bae1dSRodney W. Grimes 3119df8bae1dSRodney W. Grimes dropwithreset: 3120a0ca0871SRobert Watson if (tp != NULL) { 3121302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 31228501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3123dd8ac7f9SRobert Watson } else 3124a0ca0871SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 3125679d9708SAndre Oppermann return; 3126df8bae1dSRodney W. Grimes 3127df8bae1dSRodney W. Grimes drop: 3128df8bae1dSRodney W. Grimes /* 3129df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 3130df8bae1dSRodney W. Grimes */ 3131610ee2f9SDavid Greenman #ifdef TCPDEBUG 31321c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 3133fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3134fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3135a0292f23SGarrett Wollman #endif 31362b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 31371c53f806SRobert Watson if (tp != NULL) 31388501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3139d6915262SRobert Watson m_freem(m); 3140302ce8d6SAndre Oppermann } 3141302ce8d6SAndre Oppermann 3142302ce8d6SAndre Oppermann /* 31430ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 31440ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 31450ca3f933SAndre Oppermann * tp may be NULL. 3146302ce8d6SAndre Oppermann */ 314755bceb1eSRandall Stewart void 3148302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 3149302ce8d6SAndre Oppermann int tlen, int rstreason) 3150302ce8d6SAndre Oppermann { 3151b287c6c7SBjoern A. Zeeb #ifdef INET 3152302ce8d6SAndre Oppermann struct ip *ip; 3153b287c6c7SBjoern A. Zeeb #endif 3154302ce8d6SAndre Oppermann #ifdef INET6 3155302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 3156302ce8d6SAndre Oppermann #endif 31577a3244ccSRobert Watson 31587a3244ccSRobert Watson if (tp != NULL) { 31598501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 31607a3244ccSRobert Watson } 31617a3244ccSRobert Watson 31620ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 3163302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 3164302ce8d6SAndre Oppermann goto drop; 3165302ce8d6SAndre Oppermann #ifdef INET6 3166302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 3167302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 3168302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3169302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3170302ce8d6SAndre Oppermann goto drop; 3171302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 3172b287c6c7SBjoern A. Zeeb } 3173302ce8d6SAndre Oppermann #endif 3174b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3175b287c6c7SBjoern A. Zeeb else 3176b287c6c7SBjoern A. Zeeb #endif 3177b287c6c7SBjoern A. Zeeb #ifdef INET 3178302ce8d6SAndre Oppermann { 3179302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 3180302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3181302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3182302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3183302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3184302ce8d6SAndre Oppermann goto drop; 3185302ce8d6SAndre Oppermann } 3186b287c6c7SBjoern A. Zeeb #endif 3187302ce8d6SAndre Oppermann 3188302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 3189302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 3190302ce8d6SAndre Oppermann goto drop; 3191302ce8d6SAndre Oppermann 3192302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 3193302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 3194302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 3195302ce8d6SAndre Oppermann th->th_ack, TH_RST); 3196302ce8d6SAndre Oppermann } else { 3197302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 3198302ce8d6SAndre Oppermann tlen++; 31994ddd5aadSMichael Tuexen if (th->th_flags & TH_FIN) 32004ddd5aadSMichael Tuexen tlen++; 3201302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 3202302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 3203302ce8d6SAndre Oppermann } 3204302ce8d6SAndre Oppermann return; 3205302ce8d6SAndre Oppermann drop: 3206302ce8d6SAndre Oppermann m_freem(m); 3207df8bae1dSRodney W. Grimes } 3208df8bae1dSRodney W. Grimes 3209be2ac88cSJonathan Lemon /* 3210be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 3211be2ac88cSJonathan Lemon */ 321255bceb1eSRandall Stewart void 3213ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 3214df8bae1dSRodney W. Grimes { 3215df8bae1dSRodney W. Grimes int opt, optlen; 3216df8bae1dSRodney W. Grimes 3217be2ac88cSJonathan Lemon to->to_flags = 0; 3218df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 3219df8bae1dSRodney W. Grimes opt = cp[0]; 3220df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 3221df8bae1dSRodney W. Grimes break; 3222df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 3223df8bae1dSRodney W. Grimes optlen = 1; 3224df8bae1dSRodney W. Grimes else { 3225b474779fSJun-ichiro itojun Hagino if (cnt < 2) 3226b474779fSJun-ichiro itojun Hagino break; 3227df8bae1dSRodney W. Grimes optlen = cp[1]; 3228b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 3229df8bae1dSRodney W. Grimes break; 3230df8bae1dSRodney W. Grimes } 3231df8bae1dSRodney W. Grimes switch (opt) { 3232df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 3233df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 3234df8bae1dSRodney W. Grimes continue; 3235f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3236df8bae1dSRodney W. Grimes continue; 3237be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 3238be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 3239be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 3240fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 3241df8bae1dSRodney W. Grimes break; 3242df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 3243df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 3244df8bae1dSRodney W. Grimes continue; 3245f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3246df8bae1dSRodney W. Grimes continue; 3247be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 324802a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 3249df8bae1dSRodney W. Grimes break; 3250df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 3251df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 3252df8bae1dSRodney W. Grimes continue; 3253be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 3254a0292f23SGarrett Wollman bcopy((char *)cp + 2, 3255a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 3256fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 3257a0292f23SGarrett Wollman bcopy((char *)cp + 6, 3258a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 3259fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 3260df8bae1dSRodney W. Grimes break; 32611cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 3262fcf59617SAndrey V. Elsukov /* 3263fcf59617SAndrey V. Elsukov * In order to reply to a host which has set the 3264fcf59617SAndrey V. Elsukov * TCP_SIGNATURE option in its initial SYN, we have 3265fcf59617SAndrey V. Elsukov * to record the fact that the option was observed 3266fcf59617SAndrey V. Elsukov * here for the syncache code to perform the correct 3267fcf59617SAndrey V. Elsukov * response. 3268fcf59617SAndrey V. Elsukov */ 32691cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 32701cfd4b53SBruce M Simpson continue; 3271df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 3272df47e437SAndre Oppermann to->to_signature = cp + 2; 32731cfd4b53SBruce M Simpson break; 32746d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 3275f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 32766d90faf3SPaul Saab continue; 3277f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3278f72167f4SAndre Oppermann continue; 3279603724d3SBjoern A. Zeeb if (!V_tcp_do_sack) 3280f72167f4SAndre Oppermann continue; 3281fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 32826d90faf3SPaul Saab break; 32836d90faf3SPaul Saab case TCPOPT_SACK: 32845a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 32856d90faf3SPaul Saab continue; 3286b728e902SAndre Oppermann if (flags & TO_SYN) 3287b728e902SAndre Oppermann continue; 3288fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 32895a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 32905a53ca16SPaul Saab to->to_sacks = cp + 2; 329178b50714SRobert Watson TCPSTAT_INC(tcps_sack_rcv_blocks); 32926d90faf3SPaul Saab break; 3293281a0fd4SPatrick Kelsey case TCPOPT_FAST_OPEN: 3294c560df6fSPatrick Kelsey /* 3295c560df6fSPatrick Kelsey * Cookie length validation is performed by the 3296c560df6fSPatrick Kelsey * server side cookie checking code or the client 3297c560df6fSPatrick Kelsey * side cookie cache update code. 3298c560df6fSPatrick Kelsey */ 3299281a0fd4SPatrick Kelsey if (!(flags & TO_SYN)) 3300281a0fd4SPatrick Kelsey continue; 3301c560df6fSPatrick Kelsey if (!V_tcp_fastopen_client_enable && 3302c560df6fSPatrick Kelsey !V_tcp_fastopen_server_enable) 3303281a0fd4SPatrick Kelsey continue; 3304281a0fd4SPatrick Kelsey to->to_flags |= TOF_FASTOPEN; 3305281a0fd4SPatrick Kelsey to->to_tfo_len = optlen - 2; 3306281a0fd4SPatrick Kelsey to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL; 3307281a0fd4SPatrick Kelsey break; 3308be2ac88cSJonathan Lemon default: 3309be2ac88cSJonathan Lemon continue; 3310df8bae1dSRodney W. Grimes } 3311df8bae1dSRodney W. Grimes } 3312df8bae1dSRodney W. Grimes } 3313df8bae1dSRodney W. Grimes 3314df8bae1dSRodney W. Grimes /* 3315df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 3316df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 3317df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 3318df8bae1dSRodney W. Grimes * sequencing purposes. 3319df8bae1dSRodney W. Grimes */ 332055bceb1eSRandall Stewart void 3321ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 3322ad3f9ab3SAndre Oppermann int off) 3323df8bae1dSRodney W. Grimes { 3324fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 3325df8bae1dSRodney W. Grimes 3326df8bae1dSRodney W. Grimes while (cnt >= 0) { 3327df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 3328df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 3329df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 3330df8bae1dSRodney W. Grimes 33318501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 33327a3244ccSRobert Watson 3333df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 3334df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 3335df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 3336df8bae1dSRodney W. Grimes m->m_len--; 333769a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 333869a34685SYoshinobu Inoue m->m_pkthdr.len--; 3339df8bae1dSRodney W. Grimes return; 3340df8bae1dSRodney W. Grimes } 3341df8bae1dSRodney W. Grimes cnt -= m->m_len; 3342df8bae1dSRodney W. Grimes m = m->m_next; 33434dfdffe9SAndre Oppermann if (m == NULL) 3344df8bae1dSRodney W. Grimes break; 3345df8bae1dSRodney W. Grimes } 3346df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 3347df8bae1dSRodney W. Grimes } 3348df8bae1dSRodney W. Grimes 3349df8bae1dSRodney W. Grimes /* 3350df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 3351df8bae1dSRodney W. Grimes * and update averages and current timeout. 3352df8bae1dSRodney W. Grimes */ 335355bceb1eSRandall Stewart void 3354ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 3355df8bae1dSRodney W. Grimes { 3356ad3f9ab3SAndre Oppermann int delta; 3357233e8c18SGarrett Wollman 33588501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 33592be3bf22SRobert Watson 336078b50714SRobert Watson TCPSTAT_INC(tcps_rttupdated); 3361233e8c18SGarrett Wollman tp->t_rttupdated++; 33625ede40dcSRyan Stone if ((tp->t_srtt != 0) && (tp->t_rxtshift <= TCP_RTT_INVALIDATE)) { 3363233e8c18SGarrett Wollman /* 3364233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 3365233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 3366233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 3367233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3368233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 3369233e8c18SGarrett Wollman */ 3370233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3371233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3372233e8c18SGarrett Wollman 3373233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 3374233e8c18SGarrett Wollman tp->t_srtt = 1; 3375233e8c18SGarrett Wollman 3376233e8c18SGarrett Wollman /* 3377233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 3378233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 3379233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 3380233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 3381233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 3382233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 3383233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3384233e8c18SGarrett Wollman * rfc793's wired-in beta. 3385233e8c18SGarrett Wollman */ 3386233e8c18SGarrett Wollman if (delta < 0) 3387233e8c18SGarrett Wollman delta = -delta; 3388233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3389233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 3390233e8c18SGarrett Wollman tp->t_rttvar = 1; 33911fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 33921fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3393233e8c18SGarrett Wollman } else { 3394233e8c18SGarrett Wollman /* 3395233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 3396233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 3397233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 3398233e8c18SGarrett Wollman */ 3399233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 3400233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 34011fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3402233e8c18SGarrett Wollman } 34039b8b58e0SJonathan Lemon tp->t_rtttime = 0; 3404df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 3405df8bae1dSRodney W. Grimes 3406df8bae1dSRodney W. Grimes /* 3407df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 3408df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 3409df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 3410df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 3411df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 3412df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 3413df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 3414df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 3415df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 3416df8bae1dSRodney W. Grimes */ 3417233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 34189e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3419df8bae1dSRodney W. Grimes 3420df8bae1dSRodney W. Grimes /* 3421df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 3422df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 3423df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 3424df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 3425df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 3426df8bae1dSRodney W. Grimes */ 3427df8bae1dSRodney W. Grimes tp->t_softerror = 0; 3428df8bae1dSRodney W. Grimes } 3429df8bae1dSRodney W. Grimes 3430df8bae1dSRodney W. Grimes /* 3431df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 3432df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 34334faaea55SAndre Oppermann * If none, use an mss that can be handled on the outgoing interface 34344faaea55SAndre Oppermann * without forcing IP to fragment. If no route is found, route has no mtu, 3435df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 3436df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 3437df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 3438df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 3439df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 3440df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 3441df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 3442a0292f23SGarrett Wollman * 34430c39d38dSGleb Smirnoff * NOTE that resulting t_maxseg doesn't include space for TCP options or 34440c39d38dSGleb Smirnoff * IP options, e.g. IPSEC data, since length of this data may vary, and 34450c39d38dSGleb Smirnoff * thus it is calculated for every segment separately in tcp_output(). 3446a0292f23SGarrett Wollman * 344797d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 3448ef341ee1SGleb Smirnoff * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS 3449ef341ee1SGleb Smirnoff * settings are handled in tcp_mssopt(). 3450df8bae1dSRodney W. Grimes */ 3451a0292f23SGarrett Wollman void 3452ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer, 34533c914c54SAndre Oppermann struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap) 3454df8bae1dSRodney W. Grimes { 3455b287c6c7SBjoern A. Zeeb int mss = 0; 34563ac12506SJonathan T. Looney uint32_t maxmtu = 0; 3457c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 345897d8d152SAndre Oppermann struct hc_metrics_lite metrics; 3459fb59c426SYoshinobu Inoue #ifdef INET6 3460c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3461c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 3462c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3463c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 3464c068736aSJeffrey Hsu #else 3465c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 3466fb59c426SYoshinobu Inoue #endif 3467df8bae1dSRodney W. Grimes 34688501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 34697a3244ccSRobert Watson 3470ef341ee1SGleb Smirnoff if (mtuoffer != -1) { 3471ef341ee1SGleb Smirnoff KASSERT(offer == -1, ("%s: conflict", __func__)); 3472ef341ee1SGleb Smirnoff offer = mtuoffer - min_protoh; 3473ef341ee1SGleb Smirnoff } 3474ef341ee1SGleb Smirnoff 3475e8949f74SAndre Oppermann /* Initialize. */ 347697d8d152SAndre Oppermann #ifdef INET6 347797d8d152SAndre Oppermann if (isipv6) { 34783c914c54SAndre Oppermann maxmtu = tcp_maxmtu6(&inp->inp_inc, cap); 34790c39d38dSGleb Smirnoff tp->t_maxseg = V_tcp_v6mssdflt; 3480b287c6c7SBjoern A. Zeeb } 348197d8d152SAndre Oppermann #endif 3482b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3483b287c6c7SBjoern A. Zeeb else 3484b287c6c7SBjoern A. Zeeb #endif 3485b287c6c7SBjoern A. Zeeb #ifdef INET 348697d8d152SAndre Oppermann { 34873c914c54SAndre Oppermann maxmtu = tcp_maxmtu(&inp->inp_inc, cap); 34880c39d38dSGleb Smirnoff tp->t_maxseg = V_tcp_mssdflt; 3489df8bae1dSRodney W. Grimes } 3490b287c6c7SBjoern A. Zeeb #endif 3491df8bae1dSRodney W. Grimes 349297d8d152SAndre Oppermann /* 3493e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 349497d8d152SAndre Oppermann */ 34956f01cac6SBjoern A. Zeeb if (maxmtu == 0) { 34966f01cac6SBjoern A. Zeeb /* 34978e5c87f4SBjoern A. Zeeb * In case we return early we need to initialize metrics 34986f01cac6SBjoern A. Zeeb * to a defined state as tcp_hc_get() would do for us 34996f01cac6SBjoern A. Zeeb * if there was no cache hit. 35006f01cac6SBjoern A. Zeeb */ 35016f01cac6SBjoern A. Zeeb if (metricptr != NULL) 35026f01cac6SBjoern A. Zeeb bzero(metricptr, sizeof(struct hc_metrics_lite)); 350397d8d152SAndre Oppermann return; 35046f01cac6SBjoern A. Zeeb } 350597d8d152SAndre Oppermann 3506e8949f74SAndre Oppermann /* What have we got? */ 350797d8d152SAndre Oppermann switch (offer) { 350897d8d152SAndre Oppermann case 0: 350997d8d152SAndre Oppermann /* 351097d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 3511c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 35120c39d38dSGleb Smirnoff * already assigned to t_maxseg above. 351397d8d152SAndre Oppermann */ 35140c39d38dSGleb Smirnoff offer = tp->t_maxseg; 351597d8d152SAndre Oppermann break; 351697d8d152SAndre Oppermann 351797d8d152SAndre Oppermann case -1: 3518a0292f23SGarrett Wollman /* 3519c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 3520a0292f23SGarrett Wollman */ 352197d8d152SAndre Oppermann /* FALLTHROUGH */ 352297d8d152SAndre Oppermann 352397d8d152SAndre Oppermann default: 3524a0292f23SGarrett Wollman /* 352553369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 352653369ac9SAndre Oppermann * to at least minmss. 352753369ac9SAndre Oppermann */ 3528603724d3SBjoern A. Zeeb offer = max(offer, V_tcp_minmss); 352997d8d152SAndre Oppermann } 3530a0292f23SGarrett Wollman 3531df8bae1dSRodney W. Grimes /* 3532e8949f74SAndre Oppermann * rmx information is now retrieved from tcp_hostcache. 3533df8bae1dSRodney W. Grimes */ 353497d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 35353cee92e0SBjoern A. Zeeb if (metricptr != NULL) 35363cee92e0SBjoern A. Zeeb bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 353797d8d152SAndre Oppermann 3538df8bae1dSRodney W. Grimes /* 3539cc412412SHiren Panchasara * If there's a discovered mtu in tcp hostcache, use it. 3540cc412412SHiren Panchasara * Else, use the link mtu. 3541df8bae1dSRodney W. Grimes */ 354297d8d152SAndre Oppermann if (metrics.rmx_mtu) 35432d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3544c068736aSJeffrey Hsu else { 354531b3783cSHajimu UMEMOTO #ifdef INET6 3546fb59c426SYoshinobu Inoue if (isipv6) { 354797d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3548603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 354997d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 3550603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_v6mssdflt); 3551b287c6c7SBjoern A. Zeeb } 3552b3399803SHajimu UMEMOTO #endif 3553b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3554b287c6c7SBjoern A. Zeeb else 3555b287c6c7SBjoern A. Zeeb #endif 3556b287c6c7SBjoern A. Zeeb #ifdef INET 355797d8d152SAndre Oppermann { 355897d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3559603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 356097d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 3561603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_mssdflt); 3562a0292f23SGarrett Wollman } 3563b287c6c7SBjoern A. Zeeb #endif 35643cee92e0SBjoern A. Zeeb /* 35653cee92e0SBjoern A. Zeeb * XXX - The above conditional (mss = maxmtu - min_protoh) 35663cee92e0SBjoern A. Zeeb * probably violates the TCP spec. 35673cee92e0SBjoern A. Zeeb * The problem is that, since we don't know the 35683cee92e0SBjoern A. Zeeb * other end's MSS, we are supposed to use a conservative 35693cee92e0SBjoern A. Zeeb * default. But, if we do that, then MTU discovery will 35703cee92e0SBjoern A. Zeeb * never actually take place, because the conservative 35713cee92e0SBjoern A. Zeeb * default is much less than the MTUs typically seen 35723cee92e0SBjoern A. Zeeb * on the Internet today. For the moment, we'll sweep 35733cee92e0SBjoern A. Zeeb * this under the carpet. 35743cee92e0SBjoern A. Zeeb * 35753cee92e0SBjoern A. Zeeb * The conservative default might not actually be a problem 35763cee92e0SBjoern A. Zeeb * if the only case this occurs is when sending an initial 35773cee92e0SBjoern A. Zeeb * SYN with options and data to a host we've never talked 35783cee92e0SBjoern A. Zeeb * to before. Then, they will reply with an MSS value which 35793cee92e0SBjoern A. Zeeb * will get recorded and the new parameters should get 35803cee92e0SBjoern A. Zeeb * recomputed. For Further Study. 35813cee92e0SBjoern A. Zeeb */ 358297d8d152SAndre Oppermann } 3583a0292f23SGarrett Wollman mss = min(mss, offer); 358497d8d152SAndre Oppermann 3585a0292f23SGarrett Wollman /* 35860c39d38dSGleb Smirnoff * Sanity check: make sure that maxseg will be large 35873cee92e0SBjoern A. Zeeb * enough to allow some data on segments even if the 35883cee92e0SBjoern A. Zeeb * all the option space is used (40bytes). Otherwise 35893cee92e0SBjoern A. Zeeb * funny things may happen in tcp_output. 35900c39d38dSGleb Smirnoff * 35910c39d38dSGleb Smirnoff * XXXGL: shouldn't we reserve space for IP/IPv6 options? 35923cee92e0SBjoern A. Zeeb */ 35933cee92e0SBjoern A. Zeeb mss = max(mss, 64); 35943cee92e0SBjoern A. Zeeb 359597d8d152SAndre Oppermann tp->t_maxseg = mss; 35963cee92e0SBjoern A. Zeeb } 35973cee92e0SBjoern A. Zeeb 35983cee92e0SBjoern A. Zeeb void 35993cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer) 36003cee92e0SBjoern A. Zeeb { 3601dbc42409SLawrence Stewart int mss; 36023ac12506SJonathan T. Looney uint32_t bufsize; 36033cee92e0SBjoern A. Zeeb struct inpcb *inp; 36043cee92e0SBjoern A. Zeeb struct socket *so; 36053cee92e0SBjoern A. Zeeb struct hc_metrics_lite metrics; 36063c914c54SAndre Oppermann struct tcp_ifcap cap; 3607dbc42409SLawrence Stewart 36083cee92e0SBjoern A. Zeeb KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 36093cee92e0SBjoern A. Zeeb 36103c914c54SAndre Oppermann bzero(&cap, sizeof(cap)); 36113c914c54SAndre Oppermann tcp_mss_update(tp, offer, -1, &metrics, &cap); 36123cee92e0SBjoern A. Zeeb 36133cee92e0SBjoern A. Zeeb mss = tp->t_maxseg; 36143cee92e0SBjoern A. Zeeb inp = tp->t_inpcb; 361597d8d152SAndre Oppermann 3616df8bae1dSRodney W. Grimes /* 361797d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 361897d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 361997d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 362097d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 362197d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3622df8bae1dSRodney W. Grimes */ 3623c3b02504SBjoern A. Zeeb so = inp->inp_socket; 36243f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 3625e233e2acSAndre Oppermann if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe) 362697d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 362797d8d152SAndre Oppermann else 3628df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3629df8bae1dSRodney W. Grimes if (bufsize < mss) 3630df8bae1dSRodney W. Grimes mss = bufsize; 3631df8bae1dSRodney W. Grimes else { 3632df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3633df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3634df8bae1dSRodney W. Grimes bufsize = sb_max; 363588c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 36363f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3637df8bae1dSRodney W. Grimes } 36383f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 3639784ce8faSHiren Panchasara /* 3640784ce8faSHiren Panchasara * Sanity check: make sure that maxseg will be large 3641784ce8faSHiren Panchasara * enough to allow some data on segments even if the 3642784ce8faSHiren Panchasara * all the option space is used (40bytes). Otherwise 3643784ce8faSHiren Panchasara * funny things may happen in tcp_output. 3644784ce8faSHiren Panchasara * 3645784ce8faSHiren Panchasara * XXXGL: shouldn't we reserve space for IP/IPv6 options? 3646784ce8faSHiren Panchasara */ 3647784ce8faSHiren Panchasara tp->t_maxseg = max(mss, 64); 3648df8bae1dSRodney W. Grimes 36493f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3650e233e2acSAndre Oppermann if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe) 365197d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 365297d8d152SAndre Oppermann else 3653df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3654df8bae1dSRodney W. Grimes if (bufsize > mss) { 3655df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3656df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3657df8bae1dSRodney W. Grimes bufsize = sb_max; 365888c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 36593f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3660df8bae1dSRodney W. Grimes } 36613f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 366291d6cfa6SBjoern A. Zeeb 366391d6cfa6SBjoern A. Zeeb /* Check the interface for TSO capabilities. */ 36643c914c54SAndre Oppermann if (cap.ifcap & CSUM_TSO) { 366591d6cfa6SBjoern A. Zeeb tp->t_flags |= TF_TSO; 36663c914c54SAndre Oppermann tp->t_tsomax = cap.tsomax; 36679fd573c3SHans Petter Selasky tp->t_tsomaxsegcount = cap.tsomaxsegcount; 36689fd573c3SHans Petter Selasky tp->t_tsomaxsegsize = cap.tsomaxsegsize; 36693c914c54SAndre Oppermann } 3670a0292f23SGarrett Wollman } 3671a0292f23SGarrett Wollman 3672a0292f23SGarrett Wollman /* 3673a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3674a0292f23SGarrett Wollman */ 3675a0292f23SGarrett Wollman int 3676ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 3677a0292f23SGarrett Wollman { 367897d8d152SAndre Oppermann int mss = 0; 36793ac12506SJonathan T. Looney uint32_t thcmtu = 0; 36803ac12506SJonathan T. Looney uint32_t maxmtu = 0; 368197d8d152SAndre Oppermann size_t min_protoh; 3682a0292f23SGarrett Wollman 368397d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3684a0292f23SGarrett Wollman 368531b3783cSHajimu UMEMOTO #ifdef INET6 3686dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3687603724d3SBjoern A. Zeeb mss = V_tcp_v6mssdflt; 3688233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 368997d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 3690b287c6c7SBjoern A. Zeeb } 369131b3783cSHajimu UMEMOTO #endif 3692b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3693b287c6c7SBjoern A. Zeeb else 3694b287c6c7SBjoern A. Zeeb #endif 3695b287c6c7SBjoern A. Zeeb #ifdef INET 369697d8d152SAndre Oppermann { 3697603724d3SBjoern A. Zeeb mss = V_tcp_mssdflt; 3698233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 369997d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 370097d8d152SAndre Oppermann } 3701b287c6c7SBjoern A. Zeeb #endif 37023a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET) 37033a9391deSBjoern A. Zeeb thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 37043a9391deSBjoern A. Zeeb #endif 37053a9391deSBjoern A. Zeeb 370697d8d152SAndre Oppermann if (maxmtu && thcmtu) 370797d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 370897d8d152SAndre Oppermann else if (maxmtu || thcmtu) 370997d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 371097d8d152SAndre Oppermann 371197d8d152SAndre Oppermann return (mss); 3712df8bae1dSRodney W. Grimes } 371346f58482SJonathan Lemon 371446f58482SJonathan Lemon 371546f58482SJonathan Lemon /* 3716c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3717c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3718c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3719c068736aSJeffrey Hsu * be started again. 372046f58482SJonathan Lemon */ 372155bceb1eSRandall Stewart void 3722ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 372346f58482SJonathan Lemon { 372446f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 37253ac12506SJonathan T. Looney uint32_t ocwnd = tp->snd_cwnd; 37260c39d38dSGleb Smirnoff u_int maxseg = tcp_maxseg(tp); 372746f58482SJonathan Lemon 37288501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 37297a3244ccSRobert Watson 3730b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 373146f58482SJonathan Lemon tp->t_rtttime = 0; 373246f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 37336b2a5f92SJayanth Vijayaraghavan /* 3734c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3735c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 37366b2a5f92SJayanth Vijayaraghavan */ 37370c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th); 3738a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 373955bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 374046f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 374146f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 374246f58482SJonathan Lemon tp->snd_nxt = onxt; 374346f58482SJonathan Lemon /* 374446f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 374546f58482SJonathan Lemon * not updated yet. 374646f58482SJonathan Lemon */ 3747dbc42409SLawrence Stewart if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th)) 3748dbc42409SLawrence Stewart tp->snd_cwnd -= BYTES_THIS_ACK(tp, th); 3749d7587117SPaul Saab else 3750d7587117SPaul Saab tp->snd_cwnd = 0; 37510c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 375246f58482SJonathan Lemon } 375312eeb81fSHiren Panchasara 375412eeb81fSHiren Panchasara int 375512eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp) 375612eeb81fSHiren Panchasara { 375712eeb81fSHiren Panchasara return (tp->snd_max - tp->snd_una + 375812eeb81fSHiren Panchasara tp->sackhint.sack_bytes_rexmit - 375912eeb81fSHiren Panchasara tp->sackhint.sacked_bytes); 376012eeb81fSHiren Panchasara } 37617dc90a1dSMichael Tuexen 37627dc90a1dSMichael Tuexen uint32_t 37637dc90a1dSMichael Tuexen tcp_compute_initwnd(uint32_t maxseg) 37647dc90a1dSMichael Tuexen { 37657dc90a1dSMichael Tuexen /* 37667dc90a1dSMichael Tuexen * Calculate the Initial Window, also used as Restart Window 37677dc90a1dSMichael Tuexen * 37687dc90a1dSMichael Tuexen * RFC5681 Section 3.1 specifies the default conservative values. 37697dc90a1dSMichael Tuexen * RFC3390 specifies slightly more aggressive values. 37707dc90a1dSMichael Tuexen * RFC6928 increases it to ten segments. 37717dc90a1dSMichael Tuexen * Support for user specified value for initial flight size. 37727dc90a1dSMichael Tuexen */ 37737dc90a1dSMichael Tuexen if (V_tcp_initcwnd_segments) 37747dc90a1dSMichael Tuexen return min(V_tcp_initcwnd_segments * maxseg, 37757dc90a1dSMichael Tuexen max(2 * maxseg, V_tcp_initcwnd_segments * 1460)); 37767dc90a1dSMichael Tuexen else if (V_tcp_do_rfc3390) 37777dc90a1dSMichael Tuexen return min(4 * maxseg, max(2 * maxseg, 4380)); 37787dc90a1dSMichael Tuexen else { 37797dc90a1dSMichael Tuexen /* Per RFC5681 Section 3.1 */ 37807dc90a1dSMichael Tuexen if (maxseg > 2190) 37817dc90a1dSMichael Tuexen return (2 * maxseg); 37827dc90a1dSMichael Tuexen else if (maxseg > 1095) 37837dc90a1dSMichael Tuexen return (3 * maxseg); 37847dc90a1dSMichael Tuexen else 37857dc90a1dSMichael Tuexen return (4 * maxseg); 37867dc90a1dSMichael Tuexen } 37877dc90a1dSMichael Tuexen } 3788