1c398230bSWarner Losh /*- 2*51369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*51369649SPedro 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> 103281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 104281a0fd4SPatrick Kelsey #include <netinet/tcp_fastopen.h> 105281a0fd4SPatrick Kelsey #endif 1062de3e790SGleb Smirnoff #include <netinet/tcp.h> 107df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 108df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 109df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 110df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 111fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 112df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 1134644fda3SGleb Smirnoff #include <netinet/cc/cc.h> 11486a996e6SHiren Panchasara #ifdef TCPPCAP 11586a996e6SHiren Panchasara #include <netinet/tcp_pcap.h> 11686a996e6SHiren Panchasara #endif 117c325962bSMike Silbersack #include <netinet/tcp_syncache.h> 118610ee2f9SDavid Greenman #ifdef TCPDEBUG 119df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 120fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */ 12109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 12209fe6320SNavdeep Parhar #include <netinet/tcp_offload.h> 12309fe6320SNavdeep Parhar #endif 124fb59c426SYoshinobu Inoue 125fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 126b9234fafSSam Leffler 127db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 128db4f9cc7SJonathan Lemon 129aed55708SRobert Watson #include <security/mac/mac_framework.h> 130aed55708SRobert Watson 131dbc42409SLawrence Stewart const int tcprexmtthresh = 3; 1320312fbe9SPoul-Henning Kamp 133773673c1SAndre Oppermann int tcp_log_in_vain = 0; 134816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 135eddfbb76SRobert Watson &tcp_log_in_vain, 0, 136eddfbb76SRobert Watson "Log all incoming TCP segments to closed ports"); 137816a3d83SPoul-Henning Kamp 13882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0; 13982cea7e6SBjoern A. Zeeb #define V_blackhole VNET(blackhole) 1406df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW, 141eddfbb76SRobert Watson &VNET_NAME(blackhole), 0, 142eddfbb76SRobert Watson "Do not send RST on segments to closed ports"); 14316f7f31fSGeoff Rehmet 14482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1; 1456df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW, 146eddfbb76SRobert Watson &VNET_NAME(tcp_delack_enabled), 0, 1473d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 148f498eeeeSDavid Greenman 14982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0; 1506df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW, 151eddfbb76SRobert Watson &VNET_NAME(drop_synfin), 0, 152eddfbb76SRobert Watson "Drop TCP packets with SYN+FIN set"); 153f8613305SDag-Erling Smørgrav 15412eeb81fSHiren Panchasara VNET_DEFINE(int, tcp_do_rfc6675_pipe) = 0; 155054d38e3SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc6675_pipe, CTLFLAG_VNET | CTLFLAG_RW, 15612eeb81fSHiren Panchasara &VNET_NAME(tcp_do_rfc6675_pipe), 0, 15712eeb81fSHiren Panchasara "Use calculated pipe/in-flight bytes per RFC 6675"); 15812eeb81fSHiren Panchasara 15982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1; 1606df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW, 161eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3042), 0, 162eddfbb76SRobert Watson "Enable RFC 3042 (Limited Transmit)"); 163582a954bSJeffrey Hsu 16482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1; 1656df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW, 166eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3390), 0, 167da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 168da3a8a1aSJeffrey Hsu 169356c7958SHiren Panchasara VNET_DEFINE(int, tcp_initcwnd_segments) = 10; 170356c7958SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments, 171356c7958SHiren Panchasara CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0, 172356c7958SHiren Panchasara "Slow-start flight size (initial congestion window) in number of segments"); 17309440655SAndre Oppermann 17482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1; 1756df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW, 176eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3465), 0, 17724cb0f22SLawrence Stewart "Enable RFC 3465 (Appropriate Byte Counting)"); 178eddfbb76SRobert Watson 17982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2; 1806df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW, 181eddfbb76SRobert Watson &VNET_NAME(tcp_abc_l_var), 2, 18224cb0f22SLawrence Stewart "Cap the max cwnd increment during slow-start to this number of segments"); 18324cb0f22SLawrence Stewart 1846472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN"); 185f2512ba1SRui Paulo 186883054b4SDon Lewis VNET_DEFINE(int, tcp_do_ecn) = 2; 1876df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW, 188eddfbb76SRobert Watson &VNET_NAME(tcp_do_ecn), 0, 189eddfbb76SRobert Watson "TCP ECN support"); 190eddfbb76SRobert Watson 19182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1; 1926df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_VNET | CTLFLAG_RW, 193eddfbb76SRobert Watson &VNET_NAME(tcp_ecn_maxretries), 0, 194eddfbb76SRobert Watson "Max retries before giving up on ECN"); 195eddfbb76SRobert Watson 1963220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0; 1976df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW, 1983220a212SGleb Smirnoff &VNET_NAME(tcp_insecure_syn), 0, 1993220a212SGleb Smirnoff "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets"); 2003220a212SGleb Smirnoff 20182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0; 2026df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW, 203eddfbb76SRobert Watson &VNET_NAME(tcp_insecure_rst), 0, 2043220a212SGleb Smirnoff "Follow RFC793 instead of RFC5961 criteria for accepting RST packets"); 205a69968eeSMike Silbersack 206fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64; 207873789cbSAndre Oppermann #define V_tcp_recvspace VNET(tcp_recvspace) 2086df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW, 209873789cbSAndre Oppermann &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size"); 210873789cbSAndre Oppermann 21182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1; 2126df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW, 213eddfbb76SRobert Watson &VNET_NAME(tcp_do_autorcvbuf), 0, 2148b615593SMarko Zec "Enable automatic receive buffer sizing"); 2156741ecf5SAndre Oppermann 21682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024; 2176df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_VNET | CTLFLAG_RW, 218eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_inc), 0, 2196489fe65SAndre Oppermann "Incrementor step size of automatic receive buffer"); 2206741ecf5SAndre Oppermann 221b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024; 2226df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW, 223eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_max), 0, 2248b615593SMarko Zec "Max size of automatic receive buffer"); 2256741ecf5SAndre Oppermann 226eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb); 22744e33a07SMarko Zec #define tcb6 tcb /* for KAME src sync over BSD*'s */ 22882cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo); 229df8bae1dSRodney W. Grimes 230315e3e38SRobert Watson /* 231bf840a17SGleb Smirnoff * TCP statistics are stored in an array of counter(9)s, which size matches 232bf840a17SGleb Smirnoff * size of struct tcpstat. TCP running connection count is a regular array. 2335923c293SGleb Smirnoff */ 2345da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat); 2355da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat, 2365da0521fSAndrey V. Elsukov tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 237bf840a17SGleb Smirnoff VNET_DEFINE(counter_u64_t, tcps_states[TCP_NSTATES]); 238bf840a17SGleb Smirnoff SYSCTL_COUNTER_U64_ARRAY(_net_inet_tcp, TCPCTL_STATES, states, CTLFLAG_RD | 239d4d32b9fSHans Petter Selasky CTLFLAG_VNET, &VNET_NAME(tcps_states)[0], TCP_NSTATES, 240bf840a17SGleb Smirnoff "TCP connection counts by TCP state"); 241bf840a17SGleb Smirnoff 242bf840a17SGleb Smirnoff static void 243bf840a17SGleb Smirnoff tcp_vnet_init(const void *unused) 244bf840a17SGleb Smirnoff { 245bf840a17SGleb Smirnoff 246f59d975eSGleb Smirnoff COUNTER_ARRAY_ALLOC(V_tcps_states, TCP_NSTATES, M_WAITOK); 247bf840a17SGleb Smirnoff VNET_PCPUSTAT_ALLOC(tcpstat, M_WAITOK); 248bf840a17SGleb Smirnoff } 249bf840a17SGleb Smirnoff VNET_SYSINIT(tcp_vnet_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 250bf840a17SGleb Smirnoff tcp_vnet_init, NULL); 2515923c293SGleb Smirnoff 2525923c293SGleb Smirnoff #ifdef VIMAGE 253bf840a17SGleb Smirnoff static void 254bf840a17SGleb Smirnoff tcp_vnet_uninit(const void *unused) 255bf840a17SGleb Smirnoff { 256bf840a17SGleb Smirnoff 257f59d975eSGleb Smirnoff COUNTER_ARRAY_FREE(V_tcps_states, TCP_NSTATES); 258bf840a17SGleb Smirnoff VNET_PCPUSTAT_FREE(tcpstat); 259bf840a17SGleb Smirnoff } 260bf840a17SGleb Smirnoff VNET_SYSUNINIT(tcp_vnet_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 261bf840a17SGleb Smirnoff tcp_vnet_uninit, NULL); 2625923c293SGleb Smirnoff #endif /* VIMAGE */ 263bf840a17SGleb Smirnoff 2645923c293SGleb Smirnoff /* 265315e3e38SRobert Watson * Kernel module interface for updating tcpstat. The argument is an index 2665923c293SGleb Smirnoff * into tcpstat treated as an array. 267315e3e38SRobert Watson */ 268315e3e38SRobert Watson void 269315e3e38SRobert Watson kmod_tcpstat_inc(int statnum) 270315e3e38SRobert Watson { 271315e3e38SRobert Watson 2725da0521fSAndrey V. Elsukov counter_u64_add(VNET(tcpstat)[statnum], 1); 273315e3e38SRobert Watson } 274315e3e38SRobert Watson 275bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 276dbc42409SLawrence Stewart /* 27739bc9de5SLawrence Stewart * Wrapper for the TCP established input helper hook. 27839bc9de5SLawrence Stewart */ 27955bceb1eSRandall Stewart void 28039bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to) 28139bc9de5SLawrence Stewart { 28239bc9de5SLawrence Stewart struct tcp_hhook_data hhook_data; 28339bc9de5SLawrence Stewart 28439bc9de5SLawrence Stewart if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) { 28539bc9de5SLawrence Stewart hhook_data.tp = tp; 28639bc9de5SLawrence Stewart hhook_data.th = th; 28739bc9de5SLawrence Stewart hhook_data.to = to; 28839bc9de5SLawrence Stewart 28939bc9de5SLawrence Stewart hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data, 29039bc9de5SLawrence Stewart tp->osd); 29139bc9de5SLawrence Stewart } 29239bc9de5SLawrence Stewart } 293bd79708dSJonathan T. Looney #endif 29439bc9de5SLawrence Stewart 29539bc9de5SLawrence Stewart /* 296dbc42409SLawrence Stewart * CC wrapper hook functions 297dbc42409SLawrence Stewart */ 29855bceb1eSRandall Stewart void 2994b7b743cSLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t nsegs, 3004b7b743cSLawrence Stewart uint16_t type) 301f2512ba1SRui Paulo { 302dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 303f2512ba1SRui Paulo 3044b7b743cSLawrence Stewart tp->ccv->nsegs = nsegs; 305dbc42409SLawrence Stewart tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th); 3065b648e79SLawrence Stewart if (tp->snd_cwnd <= tp->snd_wnd) 307dbc42409SLawrence Stewart tp->ccv->flags |= CCF_CWND_LIMITED; 308dbc42409SLawrence Stewart else 309dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_CWND_LIMITED; 310dbc42409SLawrence Stewart 311dbc42409SLawrence Stewart if (type == CC_ACK) { 312dbc42409SLawrence Stewart if (tp->snd_cwnd > tp->snd_ssthresh) { 313dbc42409SLawrence Stewart tp->t_bytes_acked += min(tp->ccv->bytes_this_ack, 3144b7b743cSLawrence Stewart nsegs * V_tcp_abc_l_var * tcp_maxseg(tp)); 315dbc42409SLawrence Stewart if (tp->t_bytes_acked >= tp->snd_cwnd) { 316dbc42409SLawrence Stewart tp->t_bytes_acked -= tp->snd_cwnd; 317dbc42409SLawrence Stewart tp->ccv->flags |= CCF_ABC_SENTAWND; 318dbc42409SLawrence Stewart } 319dbc42409SLawrence Stewart } else { 320dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_ABC_SENTAWND; 321dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 322dbc42409SLawrence Stewart } 323dbc42409SLawrence Stewart } 324dbc42409SLawrence Stewart 325dbc42409SLawrence Stewart if (CC_ALGO(tp)->ack_received != NULL) { 326dbc42409SLawrence Stewart /* XXXLAS: Find a way to live without this */ 327dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 328dbc42409SLawrence Stewart CC_ALGO(tp)->ack_received(tp->ccv, type); 329dbc42409SLawrence Stewart } 330dbc42409SLawrence Stewart } 331dbc42409SLawrence Stewart 33255bceb1eSRandall Stewart void 333dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp) 334dbc42409SLawrence Stewart { 335dbc42409SLawrence Stewart struct hc_metrics_lite metrics; 336dbc42409SLawrence Stewart struct inpcb *inp = tp->t_inpcb; 3370c39d38dSGleb Smirnoff u_int maxseg; 338dbc42409SLawrence Stewart int rtt; 339dbc42409SLawrence Stewart 340dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 341dbc42409SLawrence Stewart 342dbc42409SLawrence Stewart tcp_hc_get(&inp->inp_inc, &metrics); 3430c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 344dbc42409SLawrence Stewart 345dbc42409SLawrence Stewart if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 346dbc42409SLawrence Stewart tp->t_srtt = rtt; 347dbc42409SLawrence Stewart tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 348dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrtt); 349dbc42409SLawrence Stewart if (metrics.rmx_rttvar) { 350dbc42409SLawrence Stewart tp->t_rttvar = metrics.rmx_rttvar; 351dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrttvar); 352dbc42409SLawrence Stewart } else { 353dbc42409SLawrence Stewart /* default variation is +- 1 rtt */ 354dbc42409SLawrence Stewart tp->t_rttvar = 355dbc42409SLawrence Stewart tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 356dbc42409SLawrence Stewart } 357dbc42409SLawrence Stewart TCPT_RANGESET(tp->t_rxtcur, 358dbc42409SLawrence Stewart ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 359dbc42409SLawrence Stewart tp->t_rttmin, TCPTV_REXMTMAX); 360dbc42409SLawrence Stewart } 361dbc42409SLawrence Stewart if (metrics.rmx_ssthresh) { 362dbc42409SLawrence Stewart /* 363dbc42409SLawrence Stewart * There's some sort of gateway or interface 364dbc42409SLawrence Stewart * buffer limit on the path. Use this to set 365a4641f4eSPedro F. Giffuni * the slow start threshold, but set the 366dbc42409SLawrence Stewart * threshold to no less than 2*mss. 367dbc42409SLawrence Stewart */ 3680c39d38dSGleb Smirnoff tp->snd_ssthresh = max(2 * maxseg, metrics.rmx_ssthresh); 369dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedssthresh); 370dbc42409SLawrence Stewart } 371dbc42409SLawrence Stewart 372dbc42409SLawrence Stewart /* 3739ec4a4ccSAndre Oppermann * Set the initial slow-start flight size. 374dbc42409SLawrence Stewart * 375cf8f04f4SAndre Oppermann * RFC5681 Section 3.1 specifies the default conservative values. 376cf8f04f4SAndre Oppermann * RFC3390 specifies slightly more aggressive values. 377b8b4cfcdSSergey Kandaurov * RFC6928 increases it to ten segments. 378356c7958SHiren Panchasara * Support for user specified value for initial flight size. 379cf8f04f4SAndre Oppermann * 380cf8f04f4SAndre Oppermann * If a SYN or SYN/ACK was lost and retransmitted, we have to 381cf8f04f4SAndre Oppermann * reduce the initial CWND to one segment as congestion is likely 382cf8f04f4SAndre Oppermann * requiring us to be cautious. 383dbc42409SLawrence Stewart */ 384cf8f04f4SAndre Oppermann if (tp->snd_cwnd == 1) 3850c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; /* SYN(-ACK) lost */ 386356c7958SHiren Panchasara else if (V_tcp_initcwnd_segments) 3870c39d38dSGleb Smirnoff tp->snd_cwnd = min(V_tcp_initcwnd_segments * maxseg, 3880c39d38dSGleb Smirnoff max(2 * maxseg, V_tcp_initcwnd_segments * 1460)); 389cf8f04f4SAndre Oppermann else if (V_tcp_do_rfc3390) 3900c39d38dSGleb Smirnoff tp->snd_cwnd = min(4 * maxseg, max(2 * maxseg, 4380)); 39122efabd4SAndre Oppermann else { 39222efabd4SAndre Oppermann /* Per RFC5681 Section 3.1 */ 3930c39d38dSGleb Smirnoff if (maxseg > 2190) 3940c39d38dSGleb Smirnoff tp->snd_cwnd = 2 * maxseg; 3950c39d38dSGleb Smirnoff else if (maxseg > 1095) 3960c39d38dSGleb Smirnoff tp->snd_cwnd = 3 * maxseg; 397dbc42409SLawrence Stewart else 3980c39d38dSGleb Smirnoff tp->snd_cwnd = 4 * maxseg; 39922efabd4SAndre Oppermann } 400dbc42409SLawrence Stewart 401dbc42409SLawrence Stewart if (CC_ALGO(tp)->conn_init != NULL) 402dbc42409SLawrence Stewart CC_ALGO(tp)->conn_init(tp->ccv); 403dbc42409SLawrence Stewart } 404dbc42409SLawrence Stewart 405dbc42409SLawrence Stewart void inline 406dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type) 407dbc42409SLawrence Stewart { 4080c39d38dSGleb Smirnoff u_int maxseg; 4090c39d38dSGleb Smirnoff 410dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 411dbc42409SLawrence Stewart 412dbc42409SLawrence Stewart switch(type) { 413dbc42409SLawrence Stewart case CC_NDUPACK: 414dbc42409SLawrence Stewart if (!IN_FASTRECOVERY(tp->t_flags)) { 415f2512ba1SRui Paulo tp->snd_recover = tp->snd_max; 416f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) 417f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_CWR; 418f2512ba1SRui Paulo } 419dbc42409SLawrence Stewart break; 420dbc42409SLawrence Stewart case CC_ECN: 421dbc42409SLawrence Stewart if (!IN_CONGRECOVERY(tp->t_flags)) { 422dbc42409SLawrence Stewart TCPSTAT_INC(tcps_ecn_rcwnd); 423dbc42409SLawrence Stewart tp->snd_recover = tp->snd_max; 424dbc42409SLawrence Stewart if (tp->t_flags & TF_ECN_PERMIT) 425dbc42409SLawrence Stewart tp->t_flags |= TF_ECN_SND_CWR; 426dbc42409SLawrence Stewart } 427dbc42409SLawrence Stewart break; 428dbc42409SLawrence Stewart case CC_RTO: 4290c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 430dbc42409SLawrence Stewart tp->t_dupacks = 0; 431dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 432dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 43343053c12SSean Bruno tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 / 43443053c12SSean Bruno maxseg) * maxseg; 4350c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 436dbc42409SLawrence Stewart break; 437dbc42409SLawrence Stewart case CC_RTO_ERR: 438dbc42409SLawrence Stewart TCPSTAT_INC(tcps_sndrexmitbad); 439dbc42409SLawrence Stewart /* RTO was unnecessary, so reset everything. */ 440dbc42409SLawrence Stewart tp->snd_cwnd = tp->snd_cwnd_prev; 441dbc42409SLawrence Stewart tp->snd_ssthresh = tp->snd_ssthresh_prev; 442dbc42409SLawrence Stewart tp->snd_recover = tp->snd_recover_prev; 443dbc42409SLawrence Stewart if (tp->t_flags & TF_WASFRECOVERY) 444dbc42409SLawrence Stewart ENTER_FASTRECOVERY(tp->t_flags); 445dbc42409SLawrence Stewart if (tp->t_flags & TF_WASCRECOVERY) 446dbc42409SLawrence Stewart ENTER_CONGRECOVERY(tp->t_flags); 447dbc42409SLawrence Stewart tp->snd_nxt = tp->snd_max; 448672dc4aeSJohn Baldwin tp->t_flags &= ~TF_PREVVALID; 449dbc42409SLawrence Stewart tp->t_badrxtwin = 0; 450dbc42409SLawrence Stewart break; 451dbc42409SLawrence Stewart } 452dbc42409SLawrence Stewart 453dbc42409SLawrence Stewart if (CC_ALGO(tp)->cong_signal != NULL) { 454dbc42409SLawrence Stewart if (th != NULL) 455dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 456dbc42409SLawrence Stewart CC_ALGO(tp)->cong_signal(tp->ccv, type); 457dbc42409SLawrence Stewart } 458dbc42409SLawrence Stewart } 459dbc42409SLawrence Stewart 46055bceb1eSRandall Stewart void inline 461dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th) 462dbc42409SLawrence Stewart { 463dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 464dbc42409SLawrence Stewart 465dbc42409SLawrence Stewart /* XXXLAS: KASSERT that we're in recovery? */ 466dbc42409SLawrence Stewart 467dbc42409SLawrence Stewart if (CC_ALGO(tp)->post_recovery != NULL) { 468dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 469dbc42409SLawrence Stewart CC_ALGO(tp)->post_recovery(tp->ccv); 470dbc42409SLawrence Stewart } 471dbc42409SLawrence Stewart /* XXXLAS: EXIT_RECOVERY ? */ 472dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 473dbc42409SLawrence Stewart } 4740312fbe9SPoul-Henning Kamp 475df8bae1dSRodney W. Grimes /* 476262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 47785536381SHiren Panchasara * following conditions are met: 47885536381SHiren Panchasara * - There is no delayed ack timer in progress. 47985536381SHiren Panchasara * - Our last ack wasn't a 0-sized window. We never want to delay 48085536381SHiren Panchasara * the ack that opens up a 0-sized window. 48185536381SHiren Panchasara * - LRO wasn't used for this segment. We make sure by checking that the 48285536381SHiren Panchasara * segment size is not larger than the MSS. 483d8c85a26SJonathan Lemon */ 484c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen) \ 485b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 486a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 4870c39d38dSGleb Smirnoff (tlen <= tp->t_maxseg) && \ 488603724d3SBjoern A. Zeeb (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 489d8c85a26SJonathan Lemon 49064807b30SHiren Panchasara static void inline 49164807b30SHiren Panchasara cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos) 49264807b30SHiren Panchasara { 49364807b30SHiren Panchasara INP_WLOCK_ASSERT(tp->t_inpcb); 49464807b30SHiren Panchasara 49564807b30SHiren Panchasara if (CC_ALGO(tp)->ecnpkt_handler != NULL) { 49664807b30SHiren Panchasara switch (iptos & IPTOS_ECN_MASK) { 49764807b30SHiren Panchasara case IPTOS_ECN_CE: 49864807b30SHiren Panchasara tp->ccv->flags |= CCF_IPHDR_CE; 49964807b30SHiren Panchasara break; 50064807b30SHiren Panchasara case IPTOS_ECN_ECT0: 50164807b30SHiren Panchasara tp->ccv->flags &= ~CCF_IPHDR_CE; 50264807b30SHiren Panchasara break; 50364807b30SHiren Panchasara case IPTOS_ECN_ECT1: 50464807b30SHiren Panchasara tp->ccv->flags &= ~CCF_IPHDR_CE; 50564807b30SHiren Panchasara break; 50664807b30SHiren Panchasara } 50764807b30SHiren Panchasara 50864807b30SHiren Panchasara if (th->th_flags & TH_CWR) 50964807b30SHiren Panchasara tp->ccv->flags |= CCF_TCPHDR_CWR; 51064807b30SHiren Panchasara else 51164807b30SHiren Panchasara tp->ccv->flags &= ~CCF_TCPHDR_CWR; 51264807b30SHiren Panchasara 51364807b30SHiren Panchasara if (tp->t_flags & TF_DELACK) 51464807b30SHiren Panchasara tp->ccv->flags |= CCF_DELACK; 51564807b30SHiren Panchasara else 51664807b30SHiren Panchasara tp->ccv->flags &= ~CCF_DELACK; 51764807b30SHiren Panchasara 51864807b30SHiren Panchasara CC_ALGO(tp)->ecnpkt_handler(tp->ccv); 51964807b30SHiren Panchasara 52064807b30SHiren Panchasara if (tp->ccv->flags & CCF_ACKNOW) 52164807b30SHiren Panchasara tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 52264807b30SHiren Panchasara } 52364807b30SHiren Panchasara } 52464807b30SHiren Panchasara 525df8bae1dSRodney W. Grimes /* 5268d573cc1SAndre Oppermann * TCP input handling is split into multiple parts: 5278d573cc1SAndre Oppermann * tcp6_input is a thin wrapper around tcp_input for the extended 5288d573cc1SAndre Oppermann * ip6_protox[] call format in ip6_input 5298d573cc1SAndre Oppermann * tcp_input handles primary segment validation, inpcb lookup and 5308d573cc1SAndre Oppermann * SYN processing on listen sockets 5318d573cc1SAndre Oppermann * tcp_do_segment processes the ACK and text of the segment for 5328d573cc1SAndre Oppermann * establishing, established and closing connections 533df8bae1dSRodney W. Grimes */ 534fb59c426SYoshinobu Inoue #ifdef INET6 535fb59c426SYoshinobu Inoue int 536ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto) 537fb59c426SYoshinobu Inoue { 538ad3f9ab3SAndre Oppermann struct mbuf *m = *mp; 53933841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 5403e88eb90SAndrey V. Elsukov struct ip6_hdr *ip6; 541fb59c426SYoshinobu Inoue 542fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 543fb59c426SYoshinobu Inoue 544fb59c426SYoshinobu Inoue /* 545fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 546fb59c426SYoshinobu Inoue * better place to put this in? 547fb59c426SYoshinobu Inoue */ 5483e88eb90SAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 5493e88eb90SAndrey V. Elsukov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */); 55033841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 551fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 552fb59c426SYoshinobu Inoue 5538c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 554fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 555fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 556fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 5578f5a8818SKevin Lo return (IPPROTO_DONE); 558fb59c426SYoshinobu Inoue } 55977d396fdSMaksim Yevmenkin if (ia6) 56077d396fdSMaksim Yevmenkin ifa_free(&ia6->ia_ifa); 561fb59c426SYoshinobu Inoue 5628f5a8818SKevin Lo return (tcp_input(mp, offp, proto)); 563fb59c426SYoshinobu Inoue } 564b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 565fb59c426SYoshinobu Inoue 5668f5a8818SKevin Lo int 5678f5a8818SKevin Lo tcp_input(struct mbuf **mp, int *offp, int proto) 568df8bae1dSRodney W. Grimes { 5698f5a8818SKevin Lo struct mbuf *m = *mp; 570b287c6c7SBjoern A. Zeeb struct tcphdr *th = NULL; 571ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 572ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 573302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 574302ce8d6SAndre Oppermann struct socket *so = NULL; 575e79adb8eSGarrett Wollman u_char *optp = NULL; 5768f5a8818SKevin Lo int off0; 57726f9a767SRodney W. Grimes int optlen = 0; 578b287c6c7SBjoern A. Zeeb #ifdef INET 579b287c6c7SBjoern A. Zeeb int len; 580b287c6c7SBjoern A. Zeeb #endif 581b287c6c7SBjoern A. Zeeb int tlen = 0, off; 582fb59c426SYoshinobu Inoue int drop_hdrlen; 583ad3f9ab3SAndre Oppermann int thflags; 584302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 5851d7ee746SKurt Lidl uint8_t iptos; 586c1de64a4SAndrey V. Elsukov struct m_tag *fwd_tag = NULL; 587c068736aSJeffrey Hsu #ifdef INET6 588302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 589c068736aSJeffrey Hsu int isipv6; 590c068736aSJeffrey Hsu #else 591a160e630SAndre Oppermann const void *ip6 = NULL; 592b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 593302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 594a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 595252ca428SRobert Watson int ti_locked; 596610ee2f9SDavid Greenman #ifdef TCPDEBUG 597c068736aSJeffrey Hsu /* 598c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 599c068736aSJeffrey Hsu * now IPv6. 600c068736aSJeffrey Hsu */ 60114739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 602410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 603610ee2f9SDavid Greenman short ostate = 0; 604610ee2f9SDavid Greenman #endif 605c068736aSJeffrey Hsu 606fb59c426SYoshinobu Inoue #ifdef INET6 607fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 608fb59c426SYoshinobu Inoue #endif 609a0292f23SGarrett Wollman 6108f5a8818SKevin Lo off0 = *offp; 6118f5a8818SKevin Lo m = *mp; 6128f5a8818SKevin Lo *mp = NULL; 613302ce8d6SAndre Oppermann to.to_flags = 0; 61478b50714SRobert Watson TCPSTAT_INC(tcps_rcvtotal); 615fb59c426SYoshinobu Inoue 61604d3a452SHajimu UMEMOTO #ifdef INET6 617b287c6c7SBjoern A. Zeeb if (isipv6) { 6188d573cc1SAndre Oppermann /* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */ 61945747ba5SBjoern A. Zeeb 62045747ba5SBjoern A. Zeeb if (m->m_len < (sizeof(*ip6) + sizeof(*th))) { 62145747ba5SBjoern A. Zeeb m = m_pullup(m, sizeof(*ip6) + sizeof(*th)); 62245747ba5SBjoern A. Zeeb if (m == NULL) { 62345747ba5SBjoern A. Zeeb TCPSTAT_INC(tcps_rcvshort); 6248f5a8818SKevin Lo return (IPPROTO_DONE); 62545747ba5SBjoern A. Zeeb } 62645747ba5SBjoern A. Zeeb } 62745747ba5SBjoern A. Zeeb 628fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 62945747ba5SBjoern A. Zeeb th = (struct tcphdr *)((caddr_t)ip6 + off0); 630fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 631356ab07eSBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) { 63245747ba5SBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 63345747ba5SBjoern A. Zeeb th->th_sum = m->m_pkthdr.csum_data; 63445747ba5SBjoern A. Zeeb else 63545747ba5SBjoern A. Zeeb th->th_sum = in6_cksum_pseudo(ip6, tlen, 63645747ba5SBjoern A. Zeeb IPPROTO_TCP, m->m_pkthdr.csum_data); 63745747ba5SBjoern A. Zeeb th->th_sum ^= 0xffff; 63845747ba5SBjoern A. Zeeb } else 63945747ba5SBjoern A. Zeeb th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen); 64045747ba5SBjoern A. Zeeb if (th->th_sum) { 64178b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 642fb59c426SYoshinobu Inoue goto drop; 643fb59c426SYoshinobu Inoue } 64433841545SHajimu UMEMOTO 64533841545SHajimu UMEMOTO /* 64633841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 64733841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 64833841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 64933841545SHajimu UMEMOTO * 65033841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 65133841545SHajimu UMEMOTO * already dropped in ip6_input. 65233841545SHajimu UMEMOTO */ 65333841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 65433841545SHajimu UMEMOTO /* XXX stat */ 65533841545SHajimu UMEMOTO goto drop; 65633841545SHajimu UMEMOTO } 6571d7ee746SKurt Lidl iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 658b287c6c7SBjoern A. Zeeb } 65904d3a452SHajimu UMEMOTO #endif 660b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 661b287c6c7SBjoern A. Zeeb else 662b287c6c7SBjoern A. Zeeb #endif 663b287c6c7SBjoern A. Zeeb #ifdef INET 664b287c6c7SBjoern A. Zeeb { 665df8bae1dSRodney W. Grimes /* 666df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 667df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 668df8bae1dSRodney W. Grimes */ 669fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 670105bd211SGleb Smirnoff ip_stripoptions(m); 671fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 672fb59c426SYoshinobu Inoue } 673df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 6744dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 6754dfdffe9SAndre Oppermann == NULL) { 67678b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 6778f5a8818SKevin Lo return (IPPROTO_DONE); 678df8bae1dSRodney W. Grimes } 679df8bae1dSRodney W. Grimes } 680fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 681db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 6828ad458a4SGleb Smirnoff tlen = ntohs(ip->ip_len) - off0; 683df8bae1dSRodney W. Grimes 6841d7ee746SKurt Lidl iptos = ip->ip_tos; 685db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 686db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 687db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 688db4f9cc7SJonathan Lemon else 689db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 690c068736aSJeffrey Hsu ip->ip_dst.s_addr, 6918f134647SGleb Smirnoff htonl(m->m_pkthdr.csum_data + tlen + 692c068736aSJeffrey Hsu IPPROTO_TCP)); 693db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 694db4f9cc7SJonathan Lemon } else { 6958f134647SGleb Smirnoff struct ipovly *ipov = (struct ipovly *)ip; 6968f134647SGleb Smirnoff 697df8bae1dSRodney W. Grimes /* 698df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 699df8bae1dSRodney W. Grimes */ 7008f134647SGleb Smirnoff len = off0 + tlen; 701fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 7028f134647SGleb Smirnoff ipov->ih_len = htons(tlen); 703fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 70457f60867SMark Johnston /* Reset length for SDT probes. */ 7051d7ee746SKurt Lidl ip->ip_len = htons(len); 7061d7ee746SKurt Lidl /* Reset TOS bits */ 7071d7ee746SKurt Lidl ip->ip_tos = iptos; 7081d7ee746SKurt Lidl /* Re-initialization for later version check */ 7091d7ee746SKurt Lidl ip->ip_v = IPVERSION; 710db4f9cc7SJonathan Lemon } 71157f60867SMark Johnston 712fb59c426SYoshinobu Inoue if (th->th_sum) { 71378b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 714df8bae1dSRodney W. Grimes goto drop; 715df8bae1dSRodney W. Grimes } 716fb59c426SYoshinobu Inoue } 717b287c6c7SBjoern A. Zeeb #endif /* INET */ 718df8bae1dSRodney W. Grimes 719df8bae1dSRodney W. Grimes /* 720df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 721df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 722df8bae1dSRodney W. Grimes */ 723fb59c426SYoshinobu Inoue off = th->th_off << 2; 724df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 72578b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadoff); 726df8bae1dSRodney W. Grimes goto drop; 727df8bae1dSRodney W. Grimes } 728fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 729df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 73004d3a452SHajimu UMEMOTO #ifdef INET6 731b287c6c7SBjoern A. Zeeb if (isipv6) { 7328f5a8818SKevin Lo IP6_EXTHDR_CHECK(m, off0, off, IPPROTO_DONE); 733fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 734fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 735b287c6c7SBjoern A. Zeeb } 73604d3a452SHajimu UMEMOTO #endif 737b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 738b287c6c7SBjoern A. Zeeb else 739b287c6c7SBjoern A. Zeeb #endif 740b287c6c7SBjoern A. Zeeb #ifdef INET 741b287c6c7SBjoern A. Zeeb { 742df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 743c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 7444dfdffe9SAndre Oppermann == NULL) { 74578b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 7468f5a8818SKevin Lo return (IPPROTO_DONE); 747df8bae1dSRodney W. Grimes } 748fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 749fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 750fb59c426SYoshinobu Inoue } 751df8bae1dSRodney W. Grimes } 752b287c6c7SBjoern A. Zeeb #endif 753df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 754fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 755df8bae1dSRodney W. Grimes } 756fb59c426SYoshinobu Inoue thflags = th->th_flags; 757df8bae1dSRodney W. Grimes 758e46cd3d4SDag-Erling Smørgrav /* 759df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 760df8bae1dSRodney W. Grimes */ 7612903309aSAttilio Rao tcp_fields_to_host(th); 762df8bae1dSRodney W. Grimes 763df8bae1dSRodney W. Grimes /* 764794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 765755c1f07SAndras Olah */ 766fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 767755c1f07SAndras Olah 768755c1f07SAndras Olah /* 769fa046d87SRobert Watson * Locate pcb for segment; if we're likely to add or remove a 770a7c7f2a7SJohn Baldwin * connection then first acquire pcbinfo lock. There are three cases 771fa046d87SRobert Watson * where we might discover later we need a write lock despite the 772a7c7f2a7SJohn Baldwin * flags: ACKs moving a connection out of the syncache, ACKs for a 773a7c7f2a7SJohn Baldwin * connection in TIMEWAIT and SYNs not targeting a listening socket. 774df8bae1dSRodney W. Grimes */ 775a7c7f2a7SJohn Baldwin if ((thflags & (TH_FIN | TH_RST)) != 0) { 776ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 777ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 778fa046d87SRobert Watson } else 779fa046d87SRobert Watson ti_locked = TI_UNLOCKED; 780252ca428SRobert Watson 7818d573cc1SAndre Oppermann /* 7828d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 7838d573cc1SAndre Oppermann */ 784b8056faeSGleb Smirnoff if ( 785b8056faeSGleb Smirnoff #ifdef INET6 786b8056faeSGleb Smirnoff (isipv6 && (m->m_flags & M_IP6_NEXTHOP)) 787b8056faeSGleb Smirnoff #ifdef INET 788b8056faeSGleb Smirnoff || (!isipv6 && (m->m_flags & M_IP_NEXTHOP)) 789b8056faeSGleb Smirnoff #endif 790b8056faeSGleb Smirnoff #endif 791b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6) 792b8056faeSGleb Smirnoff (m->m_flags & M_IP_NEXTHOP) 793b8056faeSGleb Smirnoff #endif 794b8056faeSGleb Smirnoff ) 7959b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 7969b932e9eSAndre Oppermann 797ce7ad664SEd Maste findpcb: 798ce7ad664SEd Maste #ifdef INVARIANTS 799ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 800ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 801ce7ad664SEd Maste } else { 802ce7ad664SEd Maste INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 803ce7ad664SEd Maste } 804ce7ad664SEd Maste #endif 8058a006adbSBjoern A. Zeeb #ifdef INET6 8068a006adbSBjoern A. Zeeb if (isipv6 && fwd_tag != NULL) { 8078a006adbSBjoern A. Zeeb struct sockaddr_in6 *next_hop6; 8088a006adbSBjoern A. Zeeb 8098a006adbSBjoern A. Zeeb next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1); 8108a006adbSBjoern A. Zeeb /* 8118a006adbSBjoern A. Zeeb * Transparently forwarded. Pretend to be the destination. 8128a006adbSBjoern A. Zeeb * Already got one like this? 8138a006adbSBjoern A. Zeeb */ 8148a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, 8158a006adbSBjoern A. Zeeb &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport, 8168a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m); 8178a006adbSBjoern A. Zeeb if (!inp) { 8188a006adbSBjoern A. Zeeb /* 8198a006adbSBjoern A. Zeeb * It's new. Try to find the ambushing socket. 8208a006adbSBjoern A. Zeeb * Because we've rewritten the destination address, 8218a006adbSBjoern A. Zeeb * any hardware-generated hash is ignored. 8228a006adbSBjoern A. Zeeb */ 8238a006adbSBjoern A. Zeeb inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src, 8248a006adbSBjoern A. Zeeb th->th_sport, &next_hop6->sin6_addr, 8258a006adbSBjoern A. Zeeb next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) : 8268a006adbSBjoern A. Zeeb th->th_dport, INPLOOKUP_WILDCARD | 8278a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 8288a006adbSBjoern A. Zeeb } 829c1de64a4SAndrey V. Elsukov } else if (isipv6) { 8308a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src, 8318a006adbSBjoern A. Zeeb th->th_sport, &ip6->ip6_dst, th->th_dport, 8328a006adbSBjoern A. Zeeb INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 8338a006adbSBjoern A. Zeeb m->m_pkthdr.rcvif, m); 8348a006adbSBjoern A. Zeeb } 8358a006adbSBjoern A. Zeeb #endif /* INET6 */ 8368a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET) 8378a006adbSBjoern A. Zeeb else 8388a006adbSBjoern A. Zeeb #endif 8398a006adbSBjoern A. Zeeb #ifdef INET 8408a006adbSBjoern A. Zeeb if (fwd_tag != NULL) { 8419b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 8429b932e9eSAndre Oppermann 8439b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 844f9e354dfSJulian Elischer /* 8452b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 846f9e354dfSJulian Elischer * already got one like this? 847f9e354dfSJulian Elischer */ 848d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport, 849fa046d87SRobert Watson ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB, 850d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 851fa046d87SRobert Watson if (!inp) { 852fa046d87SRobert Watson /* 853fa046d87SRobert Watson * It's new. Try to find the ambushing socket. 854d3c1f003SRobert Watson * Because we've rewritten the destination address, 855d3c1f003SRobert Watson * any hardware-generated hash is ignored. 856fa046d87SRobert Watson */ 857fa046d87SRobert Watson inp = in_pcblookup(&V_tcbinfo, ip->ip_src, 858fa046d87SRobert Watson th->th_sport, next_hop->sin_addr, 859fa046d87SRobert Watson next_hop->sin_port ? ntohs(next_hop->sin_port) : 860fa046d87SRobert Watson th->th_dport, INPLOOKUP_WILDCARD | 861fa046d87SRobert Watson INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 862f9e354dfSJulian Elischer } 863b10fbdeaSAndre Oppermann } else 864d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, 865fa046d87SRobert Watson th->th_sport, ip->ip_dst, th->th_dport, 866fa046d87SRobert Watson INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 867d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 8688a006adbSBjoern A. Zeeb #endif /* INET */ 869f9e354dfSJulian Elischer 870df8bae1dSRodney W. Grimes /* 871574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 872574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 8738b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 874df8bae1dSRodney W. Grimes */ 875816a3d83SPoul-Henning Kamp if (inp == NULL) { 876574b6964SAndre Oppermann /* 877574b6964SAndre Oppermann * Log communication attempts to ports that are not 878574b6964SAndre Oppermann * in use. 879574b6964SAndre Oppermann */ 880574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 881574b6964SAndre Oppermann tcp_log_in_vain == 2) { 882b7d747ecSAndre Oppermann if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6))) 883df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 884df541e5fSAndre Oppermann "to closed port\n", s, __func__); 8852e4e1b4cSGeoff Rehmet } 886574b6964SAndre Oppermann /* 887574b6964SAndre Oppermann * When blackholing do not respond with a RST but 888574b6964SAndre Oppermann * completely ignore the segment and drop it. 889574b6964SAndre Oppermann */ 890603724d3SBjoern A. Zeeb if ((V_blackhole == 1 && (thflags & TH_SYN)) || 891603724d3SBjoern A. Zeeb V_blackhole == 2) 8921929eae1SAndre Oppermann goto dropunlock; 893574b6964SAndre Oppermann 894a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 895a57815efSBosko Milekic goto dropwithreset; 896816a3d83SPoul-Henning Kamp } 897fa046d87SRobert Watson INP_WLOCK_ASSERT(inp); 898f5cf1e5fSJulien Charbon /* 899f5cf1e5fSJulien Charbon * While waiting for inp lock during the lookup, another thread 900f5cf1e5fSJulien Charbon * can have dropped the inpcb, in which case we need to loop back 901f5cf1e5fSJulien Charbon * and try to find a new inpcb to deliver to. 902f5cf1e5fSJulien Charbon */ 903f5cf1e5fSJulien Charbon if (inp->inp_flags & INP_DROPPED) { 904f5cf1e5fSJulien Charbon INP_WUNLOCK(inp); 905f5cf1e5fSJulien Charbon inp = NULL; 906f5cf1e5fSJulien Charbon goto findpcb; 907f5cf1e5fSJulien Charbon } 908c2529042SHans Petter Selasky if ((inp->inp_flowtype == M_HASHTYPE_NONE) && 909c2529042SHans Petter Selasky (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) && 910c2529042SHans Petter Selasky ((inp->inp_socket == NULL) || 911c2529042SHans Petter Selasky (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0)) { 91280cb9f21SKip Macy inp->inp_flowid = m->m_pkthdr.flowid; 9132f719932SAdrian Chadd inp->inp_flowtype = M_HASHTYPE_GET(m); 91480cb9f21SKip Macy } 915fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 916a2589465SKen Smith #ifdef INET6 917fcf59617SAndrey V. Elsukov if (isipv6 && IPSEC_ENABLED(ipv6) && 918fcf59617SAndrey V. Elsukov IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) { 919a2589465SKen Smith goto dropunlock; 920a2589465SKen Smith } 921fcf59617SAndrey V. Elsukov #ifdef INET 922fcf59617SAndrey V. Elsukov else 923fcf59617SAndrey V. Elsukov #endif 924fcf59617SAndrey V. Elsukov #endif /* INET6 */ 925fcf59617SAndrey V. Elsukov #ifdef INET 926fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4) && 927fcf59617SAndrey V. Elsukov IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) { 928fcf59617SAndrey V. Elsukov goto dropunlock; 929fcf59617SAndrey V. Elsukov } 930fcf59617SAndrey V. Elsukov #endif /* INET */ 931a2589465SKen Smith #endif /* IPSEC */ 932a2589465SKen Smith 9338d573cc1SAndre Oppermann /* 9348d573cc1SAndre Oppermann * Check the minimum TTL for socket. 9358d573cc1SAndre Oppermann */ 93634f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 93734f83c52SGeorge V. Neville-Neil #ifdef INET6 9382d8868dbSJonathan T. Looney if (isipv6) { 9392d8868dbSJonathan T. Looney if (inp->inp_ip_minttl > ip6->ip6_hlim) 940302ce8d6SAndre Oppermann goto dropunlock; 9412d8868dbSJonathan T. Looney } else 94234f83c52SGeorge V. Neville-Neil #endif 94334f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 944302ce8d6SAndre Oppermann goto dropunlock; 94534f83c52SGeorge V. Neville-Neil } 946936cd18dSAndre Oppermann 947340c35deSJonathan Lemon /* 948252ca428SRobert Watson * A previous connection in TIMEWAIT state is supposed to catch stray 949252ca428SRobert Watson * or duplicate segments arriving late. If this segment was a 9500989f56cSRobert Watson * legitimate new connection attempt, the old INPCB gets removed and 951252ca428SRobert Watson * we can try again to find a listening socket. 952252ca428SRobert Watson * 953fa046d87SRobert Watson * At this point, due to earlier optimism, we may hold only an inpcb 954fa046d87SRobert Watson * lock, and not the inpcbinfo write lock. If so, we need to try to 955fa046d87SRobert Watson * acquire it, or if that fails, acquire a reference on the inpcb, 956fa046d87SRobert Watson * drop all locks, acquire a global write lock, and then re-acquire 957fa046d87SRobert Watson * the inpcb lock. We may at that point discover that another thread 958fa046d87SRobert Watson * has tried to free the inpcb, in which case we need to loop back 959fa046d87SRobert Watson * and try to find a new inpcb to deliver to. 960fa046d87SRobert Watson * 961fa046d87SRobert Watson * XXXRW: It may be time to rethink timewait locking. 962340c35deSJonathan Lemon */ 963883e9bc4SRobert Watson relocked: 964ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 965fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 966ff9b006dSJulien Charbon if (INP_INFO_TRY_RLOCK(&V_tcbinfo) == 0) { 967252ca428SRobert Watson in_pcbref(inp); 968252ca428SRobert Watson INP_WUNLOCK(inp); 969ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 970ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 971252ca428SRobert Watson INP_WLOCK(inp); 972fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) { 973252ca428SRobert Watson inp = NULL; 974252ca428SRobert Watson goto findpcb; 975f5cf1e5fSJulien Charbon } else if (inp->inp_flags & INP_DROPPED) { 976f5cf1e5fSJulien Charbon INP_WUNLOCK(inp); 977f5cf1e5fSJulien Charbon inp = NULL; 978f5cf1e5fSJulien Charbon goto findpcb; 979252ca428SRobert Watson } 980f681a5fdSRobert Watson } else 981ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 982252ca428SRobert Watson } 983ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 984252ca428SRobert Watson 985340c35deSJonathan Lemon if (thflags & TH_SYN) 986f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 9878d573cc1SAndre Oppermann /* 9888d573cc1SAndre Oppermann * NB: tcp_twcheck unlocks the INP and frees the mbuf. 9898d573cc1SAndre Oppermann */ 9902104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 991340c35deSJonathan Lemon goto findpcb; 992ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 9938f5a8818SKevin Lo return (IPPROTO_DONE); 994340c35deSJonathan Lemon } 995794235b7SAndre Oppermann /* 996794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 997794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 998794235b7SAndre Oppermann * segment and send an appropriate response. 999794235b7SAndre Oppermann */ 1000df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 1001a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 1002a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 1003a57815efSBosko Milekic goto dropwithreset; 100409f81a46SBosko Milekic } 1005df8bae1dSRodney W. Grimes 100609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 100709fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 100809fe6320SNavdeep Parhar tcp_offload_input(tp, m); 100909fe6320SNavdeep Parhar m = NULL; /* consumed by the TOE driver */ 101009fe6320SNavdeep Parhar goto dropunlock; 101109fe6320SNavdeep Parhar } 101209fe6320SNavdeep Parhar #endif 101309fe6320SNavdeep Parhar 1014252ca428SRobert Watson /* 1015252ca428SRobert Watson * We've identified a valid inpcb, but it could be that we need an 1016fa046d87SRobert Watson * inpcbinfo write lock but don't hold it. In this case, attempt to 1017fa046d87SRobert Watson * acquire using the same strategy as the TIMEWAIT case above. If we 1018fa046d87SRobert Watson * relock, we have to jump back to 'relocked' as the connection might 1019fa046d87SRobert Watson * now be in TIMEWAIT. 1020252ca428SRobert Watson */ 1021fa046d87SRobert Watson #ifdef INVARIANTS 1022a7c7f2a7SJohn Baldwin if ((thflags & (TH_FIN | TH_RST)) != 0) 1023ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1024fa046d87SRobert Watson #endif 1025a7c7f2a7SJohn Baldwin if (!((tp->t_state == TCPS_ESTABLISHED && (thflags & TH_SYN) == 0) || 1026281a0fd4SPatrick Kelsey (tp->t_state == TCPS_LISTEN && (thflags & TH_SYN) && 102768bd7ed1SJonathan T. Looney !IS_FASTOPEN(tp->t_flags)))) { 1028fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 1029ff9b006dSJulien Charbon if (INP_INFO_TRY_RLOCK(&V_tcbinfo) == 0) { 1030252ca428SRobert Watson in_pcbref(inp); 1031252ca428SRobert Watson INP_WUNLOCK(inp); 1032ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 1033ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 1034252ca428SRobert Watson INP_WLOCK(inp); 1035fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) { 1036252ca428SRobert Watson inp = NULL; 1037252ca428SRobert Watson goto findpcb; 1038f5cf1e5fSJulien Charbon } else if (inp->inp_flags & INP_DROPPED) { 1039f5cf1e5fSJulien Charbon INP_WUNLOCK(inp); 1040f5cf1e5fSJulien Charbon inp = NULL; 1041f5cf1e5fSJulien Charbon goto findpcb; 1042252ca428SRobert Watson } 1043883e9bc4SRobert Watson goto relocked; 1044f681a5fdSRobert Watson } else 1045ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 1046252ca428SRobert Watson } 1047ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1048252ca428SRobert Watson } 1049252ca428SRobert Watson 1050c488362eSRobert Watson #ifdef MAC 10518501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 105230d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 1053302ce8d6SAndre Oppermann goto dropunlock; 1054c488362eSRobert Watson #endif 1055a557af22SRobert Watson so = inp->inp_socket; 1056302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 1057610ee2f9SDavid Greenman #ifdef TCPDEBUG 1058df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 1059df8bae1dSRodney W. Grimes ostate = tp->t_state; 106010fe523eSMaxim Konovalov #ifdef INET6 10616f697424SBjoern A. Zeeb if (isipv6) { 1062540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 1063d30d90dcSMaxim Konovalov } else 10646f697424SBjoern A. Zeeb #endif 1065540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 1066fb59c426SYoshinobu Inoue tcp_savetcp = *th; 1067df8bae1dSRodney W. Grimes } 1068b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */ 1069db33b3e6SAndre Oppermann /* 1070db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 1071db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 1072a7c7f2a7SJohn Baldwin * attempt or the completion of a previous one. 1073db33b3e6SAndre Oppermann */ 107445274760SJonathan T. Looney KASSERT(tp->t_state == TCPS_LISTEN || !(so->so_options & SO_ACCEPTCONN), 107545274760SJonathan T. Looney ("%s: so accepting but tp %p not listening", __func__, tp)); 107645274760SJonathan T. Looney if (tp->t_state == TCPS_LISTEN && (so->so_options & SO_ACCEPTCONN)) { 1077540e8b7eSJeffrey Hsu struct in_conninfo inc; 1078540e8b7eSJeffrey Hsu 10798bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 1080302ce8d6SAndre Oppermann #ifdef INET6 1081be2ac88cSJonathan Lemon if (isipv6) { 1082dcdb4371SBjoern A. Zeeb inc.inc_flags |= INC_ISIPV6; 1083be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 1084be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 1085302ce8d6SAndre Oppermann } else 1086302ce8d6SAndre Oppermann #endif 1087302ce8d6SAndre Oppermann { 1088be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 1089be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 1090be2ac88cSJonathan Lemon } 1091be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 1092be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 10937973fba3SJulian Elischer inc.inc_fibnum = so->so_fibnum; 1094be2ac88cSJonathan Lemon 1095fb59c426SYoshinobu Inoue /* 10968d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 10978d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 10988d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 1099fb59c426SYoshinobu Inoue */ 1100be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 1101a7c7f2a7SJohn Baldwin 1102ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1103bf6d304aSAndre Oppermann /* 1104bf6d304aSAndre Oppermann * Parse the TCP options here because 1105bf6d304aSAndre Oppermann * syncookies need access to the reflected 1106bf6d304aSAndre Oppermann * timestamp. 1107bf6d304aSAndre Oppermann */ 1108bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 11099fa198beSAndre Oppermann /* 11109fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 11119fa198beSAndre Oppermann * inp and tcpinfo locks. 11129fa198beSAndre Oppermann */ 1113fcf59617SAndrey V. Elsukov rstreason = syncache_expand(&inc, &to, th, &so, m); 1114fcf59617SAndrey V. Elsukov if (rstreason < 0) { 1115fcf59617SAndrey V. Elsukov /* 1116fcf59617SAndrey V. Elsukov * A failing TCP MD5 signature comparison 1117fcf59617SAndrey V. Elsukov * must result in the segment being dropped 1118fcf59617SAndrey V. Elsukov * and must not produce any response back 1119fcf59617SAndrey V. Elsukov * to the sender. 1120fcf59617SAndrey V. Elsukov */ 1121fcf59617SAndrey V. Elsukov goto dropunlock; 1122fcf59617SAndrey V. Elsukov } else if (rstreason == 0) { 11234195b4afSPaul Traina /* 1124e207f800SAndre Oppermann * No syncache entry or ACK was not 1125be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 11268d573cc1SAndre Oppermann * NB: syncache did its own logging 11278d573cc1SAndre Oppermann * of the failure cause. 11284195b4afSPaul Traina */ 1129be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 1130be2ac88cSJonathan Lemon goto dropwithreset; 1131be2ac88cSJonathan Lemon } 1132281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 113309c305ebSPatrick Kelsey tfo_socket_result: 1134281a0fd4SPatrick Kelsey #endif 1135f76fcf6dSJeffrey Hsu if (so == NULL) { 1136be2ac88cSJonathan Lemon /* 1137e207f800SAndre Oppermann * We completed the 3-way handshake 1138e207f800SAndre Oppermann * but could not allocate a socket 1139e207f800SAndre Oppermann * either due to memory shortage, 1140e207f800SAndre Oppermann * listen queue length limits or 1141a160e630SAndre Oppermann * global socket limits. Send RST 1142a160e630SAndre Oppermann * or wait and have the remote end 1143a160e630SAndre Oppermann * retransmit the ACK for another 1144a160e630SAndre Oppermann * try. 1145be2ac88cSJonathan Lemon */ 1146a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1147a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11488d573cc1SAndre Oppermann "Socket allocation failed due to " 11498d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 1150ac957cd2SJulian Elischer s, __func__, 1151ac957cd2SJulian Elischer V_tcp_sc_rst_sock_fail ? 1152ac957cd2SJulian Elischer "sending RST" : "try again"); 1153603724d3SBjoern A. Zeeb if (V_tcp_sc_rst_sock_fail) { 1154e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 1155e207f800SAndre Oppermann goto dropwithreset; 1156a160e630SAndre Oppermann } else 1157a160e630SAndre Oppermann goto dropunlock; 1158f76fcf6dSJeffrey Hsu } 1159be2ac88cSJonathan Lemon /* 1160be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 11618d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 11628d573cc1SAndre Oppermann * created socket and update the tp variable. 1163be2ac88cSJonathan Lemon */ 11648501a69cSRobert Watson INP_WUNLOCK(inp); /* listen socket */ 1165be2ac88cSJonathan Lemon inp = sotoinpcb(so); 1166ff9b006dSJulien Charbon /* 1167ff9b006dSJulien Charbon * New connection inpcb is already locked by 1168ff9b006dSJulien Charbon * syncache_expand(). 1169ff9b006dSJulien Charbon */ 1170ff9b006dSJulien Charbon INP_WLOCK_ASSERT(inp); 1171be2ac88cSJonathan Lemon tp = intotcpcb(inp); 11728d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 11738d573cc1SAndre Oppermann ("%s: ", __func__)); 1174be2ac88cSJonathan Lemon /* 1175302ce8d6SAndre Oppermann * Process the segment and the data it 1176302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 1177302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 1178302ce8d6SAndre Oppermann */ 117955bceb1eSRandall Stewart tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 1180252ca428SRobert Watson iptos, ti_locked); 1181603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 11828f5a8818SKevin Lo return (IPPROTO_DONE); 1183be2ac88cSJonathan Lemon } 1184a160e630SAndre Oppermann /* 11858d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 11868d573cc1SAndre Oppermann * 1187a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 1188a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 1189a160e630SAndre Oppermann * retransmits. 119019bc77c5SAndre Oppermann * 119119bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 119219bc77c5SAndre Oppermann * causes. 1193a160e630SAndre Oppermann */ 1194a160e630SAndre Oppermann if (thflags & TH_RST) { 119519bc77c5SAndre Oppermann syncache_chkrst(&inc, th); 1196a160e630SAndre Oppermann goto dropunlock; 1197a160e630SAndre Oppermann } 1198a160e630SAndre Oppermann /* 1199a160e630SAndre Oppermann * We can't do anything without SYN. 1200a160e630SAndre Oppermann */ 1201a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 1202a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1203a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1204773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 12058d573cc1SAndre Oppermann s, __func__); 120678b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1207a160e630SAndre Oppermann goto dropunlock; 1208a160e630SAndre Oppermann } 1209a160e630SAndre Oppermann /* 1210a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 1211a160e630SAndre Oppermann */ 1212fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1213a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1214a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12158d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 12168d573cc1SAndre Oppermann s, __func__); 1217a160e630SAndre Oppermann syncache_badack(&inc); /* XXX: Not needed! */ 121878b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1219a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1220a57815efSBosko Milekic goto dropwithreset; 12214195b4afSPaul Traina } 1222a160e630SAndre Oppermann /* 1223a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 1224a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 1225a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 1226a160e630SAndre Oppermann * TCP/IP stack. 1227a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 1228a160e630SAndre Oppermann * and is constantly refining its stack detection 1229a160e630SAndre Oppermann * strategies. 12308d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 1231a160e630SAndre Oppermann * and was used by RFC1644. 1232a160e630SAndre Oppermann */ 1233603724d3SBjoern A. Zeeb if ((thflags & TH_FIN) && V_drop_synfin) { 1234a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1235a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1236773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 12378d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 123878b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1239302ce8d6SAndre Oppermann goto dropunlock; 1240ebb0cbeaSPaul Traina } 1241be2ac88cSJonathan Lemon /* 1242be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 1243a160e630SAndre Oppermann * 1244a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 1245a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 1246a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 1247be2ac88cSJonathan Lemon */ 1248a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 1249a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 1250a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 1251a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 125233841545SHajimu UMEMOTO #ifdef INET6 125333841545SHajimu UMEMOTO /* 125433841545SHajimu UMEMOTO * If deprecated address is forbidden, 125533841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 125633841545SHajimu UMEMOTO * address to prevent any new inbound connection from 125733841545SHajimu UMEMOTO * getting established. 125833841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 125933841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 126033841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 126133841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 126233841545SHajimu UMEMOTO * for the exchange. 126333841545SHajimu UMEMOTO * 126433841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 126533841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 126633841545SHajimu UMEMOTO * SYN in this case. 126733841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 126833841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 126933841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 127033841545SHajimu UMEMOTO * used" 127133841545SHajimu UMEMOTO * 2. use of it with new communication: 127233841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 127333841545SHajimu UMEMOTO * with sufficient scope is available" 127433841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 127533841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 127633841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 127733841545SHajimu UMEMOTO * 127833841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 127933841545SHajimu UMEMOTO * multiple description text for deprecated address 128033841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 128133841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 128233841545SHajimu UMEMOTO */ 1283603724d3SBjoern A. Zeeb if (isipv6 && !V_ip6_use_deprecated) { 128433841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 128533841545SHajimu UMEMOTO 12863e88eb90SAndrey V. Elsukov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */); 12878c0fec80SRobert Watson if (ia6 != NULL && 128833841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 12898c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 1290a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1291a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12928d573cc1SAndre Oppermann "Connection attempt to deprecated " 12938d573cc1SAndre Oppermann "IPv6 address rejected\n", 1294a160e630SAndre Oppermann s, __func__); 129533841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 129633841545SHajimu UMEMOTO goto dropwithreset; 129733841545SHajimu UMEMOTO } 129877d396fdSMaksim Yevmenkin if (ia6) 12998c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 130033841545SHajimu UMEMOTO } 1301b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 130265f28919SJesper Skriver /* 1303db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 13048d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 1305db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 13068d573cc1SAndre Oppermann * If it is from this socket it must be forged. 13078d573cc1SAndre Oppermann * Don't respond if the source or destination is a 13088d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 130993ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 131093ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 131193ec91baSCrist J. Clark * in_broadcast() to find them. 1312be2ac88cSJonathan Lemon */ 13138d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 13148d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 13158d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13168d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 1317773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 1318302ce8d6SAndre Oppermann goto dropunlock; 13198d573cc1SAndre Oppermann } 1320db33b3e6SAndre Oppermann #ifdef INET6 1321b287c6c7SBjoern A. Zeeb if (isipv6) { 1322db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1323a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 1324a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1325a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13268d573cc1SAndre Oppermann "Connection attempt to/from self " 1327773673c1SAndre Oppermann "ignored\n", s, __func__); 1328302ce8d6SAndre Oppermann goto dropunlock; 1329a160e630SAndre Oppermann } 1330be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1331a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 1332a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1333a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13348d573cc1SAndre Oppermann "Connection attempt from/to multicast " 1335773673c1SAndre Oppermann "address ignored\n", s, __func__); 1336302ce8d6SAndre Oppermann goto dropunlock; 1337a160e630SAndre Oppermann } 1338b287c6c7SBjoern A. Zeeb } 1339db33b3e6SAndre Oppermann #endif 1340b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 1341b287c6c7SBjoern A. Zeeb else 1342b287c6c7SBjoern A. Zeeb #endif 1343b287c6c7SBjoern A. Zeeb #ifdef INET 1344b287c6c7SBjoern A. Zeeb { 1345db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1346a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 1347a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1348a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13498d573cc1SAndre Oppermann "Connection attempt from/to self " 1350773673c1SAndre Oppermann "ignored\n", s, __func__); 1351302ce8d6SAndre Oppermann goto dropunlock; 1352a160e630SAndre Oppermann } 1353be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1354be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 13552ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 1356a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 1357a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1358a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13598d573cc1SAndre Oppermann "Connection attempt from/to broad- " 1360773673c1SAndre Oppermann "or multicast address ignored\n", 1361a160e630SAndre Oppermann s, __func__); 1362302ce8d6SAndre Oppermann goto dropunlock; 1363c068736aSJeffrey Hsu } 1364a160e630SAndre Oppermann } 1365b287c6c7SBjoern A. Zeeb #endif 1366be2ac88cSJonathan Lemon /* 1367db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 1368db33b3e6SAndre Oppermann * for syncache. 1369be2ac88cSJonathan Lemon */ 13703c653157SHartmut Brandt #ifdef TCPDEBUG 13713c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 13723c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 13733c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 13743c653157SHartmut Brandt #endif 13752b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 1376f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 1377281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 1378281a0fd4SPatrick Kelsey if (syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL)) 137909c305ebSPatrick Kelsey goto tfo_socket_result; 1380281a0fd4SPatrick Kelsey #else 138109fe6320SNavdeep Parhar syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL); 1382281a0fd4SPatrick Kelsey #endif 1383be2ac88cSJonathan Lemon /* 13844d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 1385a7c7f2a7SJohn Baldwin * Only the listen socket is unlocked by syncache_add(). 1386be2ac88cSJonathan Lemon */ 1387ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 1388ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1389a7c7f2a7SJohn Baldwin ti_locked = TI_UNLOCKED; 1390a7c7f2a7SJohn Baldwin } 1391603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 13928f5a8818SKevin Lo return (IPPROTO_DONE); 1393982c1675SAndre Oppermann } else if (tp->t_state == TCPS_LISTEN) { 1394982c1675SAndre Oppermann /* 1395982c1675SAndre Oppermann * When a listen socket is torn down the SO_ACCEPTCONN 1396982c1675SAndre Oppermann * flag is removed first while connections are drained 1397982c1675SAndre Oppermann * from the accept queue in a unlock/lock cycle of the 1398982c1675SAndre Oppermann * ACCEPT_LOCK, opening a race condition allowing a SYN 1399982c1675SAndre Oppermann * attempt go through unhandled. 1400982c1675SAndre Oppermann */ 1401982c1675SAndre Oppermann goto dropunlock; 1402f76fcf6dSJeffrey Hsu } 1403fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1404fcf59617SAndrey V. Elsukov if (tp->t_flags & TF_SIGNATURE) { 1405fcf59617SAndrey V. Elsukov tcp_dooptions(&to, optp, optlen, thflags); 1406fcf59617SAndrey V. Elsukov if ((to.to_flags & TOF_SIGNATURE) == 0) { 1407fcf59617SAndrey V. Elsukov TCPSTAT_INC(tcps_sig_err_nosigopt); 14082903309aSAttilio Rao goto dropunlock; 14092903309aSAttilio Rao } 1410fcf59617SAndrey V. Elsukov if (!TCPMD5_ENABLED() || 1411fcf59617SAndrey V. Elsukov TCPMD5_INPUT(m, th, to.to_signature) != 0) 1412fcf59617SAndrey V. Elsukov goto dropunlock; 14132903309aSAttilio Rao } 14142903309aSAttilio Rao #endif 14152b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 141657f60867SMark Johnston 1417302ce8d6SAndre Oppermann /* 14188d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 14198d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 14208d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 1421302ce8d6SAndre Oppermann */ 142255bceb1eSRandall Stewart tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked); 1423603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 14248f5a8818SKevin Lo return (IPPROTO_DONE); 1425be2ac88cSJonathan Lemon 1426302ce8d6SAndre Oppermann dropwithreset: 14272b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 142857f60867SMark Johnston 1429ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 1430ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1431252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1432fa046d87SRobert Watson } 1433fa046d87SRobert Watson #ifdef INVARIANTS 1434fa046d87SRobert Watson else { 1435fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset " 1436fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1437fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1438fa046d87SRobert Watson } 1439fa046d87SRobert Watson #endif 1440014ea782SRobert Watson 1441014ea782SRobert Watson if (inp != NULL) { 1442302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 1443014ea782SRobert Watson INP_WUNLOCK(inp); 1444dd8ac7f9SRobert Watson } else 1445014ea782SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1446302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 1447014ea782SRobert Watson goto drop; 1448014ea782SRobert Watson 1449302ce8d6SAndre Oppermann dropunlock: 145057f60867SMark Johnston if (m != NULL) 14512b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 145257f60867SMark Johnston 1453ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 1454ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1455252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1456fa046d87SRobert Watson } 1457fa046d87SRobert Watson #ifdef INVARIANTS 1458fa046d87SRobert Watson else { 1459fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock " 1460fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1461fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1462fa046d87SRobert Watson } 1463fa046d87SRobert Watson #endif 1464252ca428SRobert Watson 14659fa198beSAndre Oppermann if (inp != NULL) 14668501a69cSRobert Watson INP_WUNLOCK(inp); 1467014ea782SRobert Watson 1468302ce8d6SAndre Oppermann drop: 1469603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1470a160e630SAndre Oppermann if (s != NULL) 1471a160e630SAndre Oppermann free(s, M_TCPLOG); 1472302ce8d6SAndre Oppermann if (m != NULL) 1473302ce8d6SAndre Oppermann m_freem(m); 14748f5a8818SKevin Lo return (IPPROTO_DONE); 1475302ce8d6SAndre Oppermann } 1476302ce8d6SAndre Oppermann 1477e44c1887SSteven Hartland /* 1478e44c1887SSteven Hartland * Automatic sizing of receive socket buffer. Often the send 1479e44c1887SSteven Hartland * buffer size is not optimally adjusted to the actual network 1480e44c1887SSteven Hartland * conditions at hand (delay bandwidth product). Setting the 1481e44c1887SSteven Hartland * buffer size too small limits throughput on links with high 1482e44c1887SSteven Hartland * bandwidth and high delay (eg. trans-continental/oceanic links). 1483e44c1887SSteven Hartland * 1484e44c1887SSteven Hartland * On the receive side the socket buffer memory is only rarely 1485e44c1887SSteven Hartland * used to any significant extent. This allows us to be much 1486e44c1887SSteven Hartland * more aggressive in scaling the receive socket buffer. For 1487e44c1887SSteven Hartland * the case that the buffer space is actually used to a large 1488e44c1887SSteven Hartland * extent and we run out of kernel memory we can simply drop 1489e44c1887SSteven Hartland * the new segments; TCP on the sender will just retransmit it 1490e44c1887SSteven Hartland * later. Setting the buffer size too big may only consume too 1491e44c1887SSteven Hartland * much kernel memory if the application doesn't read() from 1492e44c1887SSteven Hartland * the socket or packet loss or reordering makes use of the 1493e44c1887SSteven Hartland * reassembly queue. 1494e44c1887SSteven Hartland * 1495e44c1887SSteven Hartland * The criteria to step up the receive buffer one notch are: 1496e44c1887SSteven Hartland * 1. Application has not set receive buffer size with 1497e44c1887SSteven Hartland * SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE. 1498e44c1887SSteven Hartland * 2. the number of bytes received during the time it takes 1499e44c1887SSteven Hartland * one timestamp to be reflected back to us (the RTT); 1500e44c1887SSteven Hartland * 3. received bytes per RTT is within seven eighth of the 1501e44c1887SSteven Hartland * current socket buffer size; 1502e44c1887SSteven Hartland * 4. receive buffer size has not hit maximal automatic size; 1503e44c1887SSteven Hartland * 1504e44c1887SSteven Hartland * This algorithm does one step per RTT at most and only if 1505e44c1887SSteven Hartland * we receive a bulk stream w/o packet losses or reorderings. 1506e44c1887SSteven Hartland * Shrinking the buffer during idle times is not necessary as 1507e44c1887SSteven Hartland * it doesn't consume any memory when idle. 1508e44c1887SSteven Hartland * 1509e44c1887SSteven Hartland * TODO: Only step up if the application is actually serving 1510e44c1887SSteven Hartland * the buffer to better manage the socket buffer resources. 1511e44c1887SSteven Hartland */ 1512e44c1887SSteven Hartland int 1513e44c1887SSteven Hartland tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so, 1514e44c1887SSteven Hartland struct tcpcb *tp, int tlen) 1515e44c1887SSteven Hartland { 1516e44c1887SSteven Hartland int newsize = 0; 1517e44c1887SSteven Hartland 1518e44c1887SSteven Hartland if (V_tcp_do_autorcvbuf && (so->so_rcv.sb_flags & SB_AUTOSIZE) && 1519e44c1887SSteven Hartland tp->t_srtt != 0 && tp->rfbuf_ts != 0 && 1520e44c1887SSteven Hartland TCP_TS_TO_TICKS(tcp_ts_getticks() - tp->rfbuf_ts) > 1521e44c1887SSteven Hartland (tp->t_srtt >> TCP_RTT_SHIFT)) { 1522e44c1887SSteven Hartland if (tp->rfbuf_cnt > (so->so_rcv.sb_hiwat / 8 * 7) && 1523e44c1887SSteven Hartland so->so_rcv.sb_hiwat < V_tcp_autorcvbuf_max) { 1524e44c1887SSteven Hartland newsize = min(so->so_rcv.sb_hiwat + 1525e44c1887SSteven Hartland V_tcp_autorcvbuf_inc, V_tcp_autorcvbuf_max); 1526e44c1887SSteven Hartland } 1527e44c1887SSteven Hartland TCP_PROBE6(receive__autoresize, NULL, tp, m, tp, th, newsize); 1528e44c1887SSteven Hartland 1529e44c1887SSteven Hartland /* Start over with next RTT. */ 1530e44c1887SSteven Hartland tp->rfbuf_ts = 0; 1531e44c1887SSteven Hartland tp->rfbuf_cnt = 0; 1532e44c1887SSteven Hartland } else { 1533e44c1887SSteven Hartland tp->rfbuf_cnt += tlen; /* add up */ 1534e44c1887SSteven Hartland } 1535e44c1887SSteven Hartland 1536e44c1887SSteven Hartland return (newsize); 1537e44c1887SSteven Hartland } 1538e44c1887SSteven Hartland 153955bceb1eSRandall Stewart void 1540302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 1541252ca428SRobert Watson struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos, 1542252ca428SRobert Watson int ti_locked) 1543302ce8d6SAndre Oppermann { 1544021eaf79SHiren Panchasara int thflags, acked, ourfinisacked, needoutput = 0, sack_changed; 1545302ce8d6SAndre Oppermann int rstreason, todrop, win; 15463ac12506SJonathan T. Looney uint32_t tiwin; 15474b7b743cSLawrence Stewart uint16_t nsegs; 154807dacf03SAndre Oppermann char *s; 154907dacf03SAndre Oppermann struct in_conninfo *inc; 1550c11a15bfSGleb Smirnoff struct mbuf *mfree; 1551302ce8d6SAndre Oppermann struct tcpopt to; 155268bd7ed1SJonathan T. Looney #ifdef TCP_RFC7413 1553281a0fd4SPatrick Kelsey int tfo_syn; 155468bd7ed1SJonathan T. Looney #endif 1555302ce8d6SAndre Oppermann 155614739780SMaxim Konovalov #ifdef TCPDEBUG 155714739780SMaxim Konovalov /* 155814739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 155914739780SMaxim Konovalov * now IPv6. 156014739780SMaxim Konovalov */ 156114739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 156214739780SMaxim Konovalov struct tcphdr tcp_savetcp; 156314739780SMaxim Konovalov short ostate = 0; 156414739780SMaxim Konovalov #endif 1565302ce8d6SAndre Oppermann thflags = th->th_flags; 156607dacf03SAndre Oppermann inc = &tp->t_inpcb->inp_inc; 1567d64a46eaSLawrence Stewart tp->sackhint.last_sack_ack = 0; 1568021eaf79SHiren Panchasara sack_changed = 0; 15694b7b743cSLawrence Stewart nsegs = max(1, m->m_pkthdr.lro_nsegs); 1570302ce8d6SAndre Oppermann 1571252ca428SRobert Watson /* 1572252ca428SRobert Watson * If this is either a state-changing packet or current state isn't 1573252ca428SRobert Watson * established, we require a write lock on tcbinfo. Otherwise, we 15740989f56cSRobert Watson * allow the tcbinfo to be in either alocked or unlocked, as the 15750989f56cSRobert Watson * caller may have unnecessarily acquired a write lock due to a race. 1576252ca428SRobert Watson */ 1577252ca428SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 1578252ca428SRobert Watson tp->t_state != TCPS_ESTABLISHED) { 1579ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, ("%s ti_locked %d for " 1580252ca428SRobert Watson "SYN/FIN/RST/!EST", __func__, ti_locked)); 1581ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1582252ca428SRobert Watson } else { 1583252ca428SRobert Watson #ifdef INVARIANTS 1584ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 1585ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1586fa046d87SRobert Watson else { 1587fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST " 1588fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1589fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1590fa046d87SRobert Watson } 1591252ca428SRobert Watson #endif 1592252ca428SRobert Watson } 15938501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1594679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1595679d9708SAndre Oppermann __func__)); 1596679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1597679d9708SAndre Oppermann __func__)); 1598df8bae1dSRodney W. Grimes 159986a996e6SHiren Panchasara #ifdef TCPPCAP 160086a996e6SHiren Panchasara /* Save segment, if requested. */ 160186a996e6SHiren Panchasara tcp_pcap_add(th, m, &(tp->t_inpkts)); 160286a996e6SHiren Panchasara #endif 160386a996e6SHiren Panchasara 1604013f4df6SMichael Tuexen if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) { 1605013f4df6SMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 1606013f4df6SMichael Tuexen log(LOG_DEBUG, "%s; %s: " 1607013f4df6SMichael Tuexen "SYN|FIN segment ignored (based on " 1608013f4df6SMichael Tuexen "sysctl setting)\n", s, __func__); 1609013f4df6SMichael Tuexen free(s, M_TCPLOG); 1610013f4df6SMichael Tuexen } 1611013f4df6SMichael Tuexen goto drop; 1612013f4df6SMichael Tuexen } 1613013f4df6SMichael Tuexen 1614df8bae1dSRodney W. Grimes /* 1615ebfd7534SMichael Tuexen * If a segment with the ACK-bit set arrives in the SYN-SENT state 1616ebfd7534SMichael Tuexen * check SEQ.ACK first. 1617ebfd7534SMichael Tuexen */ 1618ebfd7534SMichael Tuexen if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) && 1619ebfd7534SMichael Tuexen (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) { 1620ebfd7534SMichael Tuexen rstreason = BANDLIM_UNLIMITED; 1621ebfd7534SMichael Tuexen goto dropwithreset; 1622ebfd7534SMichael Tuexen } 1623ebfd7534SMichael Tuexen 1624ebfd7534SMichael Tuexen /* 1625df8bae1dSRodney W. Grimes * Segment received on connection. 1626df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1627e8949f74SAndre Oppermann * XXX: This should be done after segment 1628e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 1629df8bae1dSRodney W. Grimes */ 16309b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 1631df8bae1dSRodney W. Grimes 1632df8bae1dSRodney W. Grimes /* 1633c52102ddSHiren Panchasara * Scale up the window into a 32-bit value. 1634e8949f74SAndre Oppermann * For the SYN_SENT state the scale is zero. 1635464fcfbcSAndre Oppermann */ 1636464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 1637464fcfbcSAndre Oppermann 1638464fcfbcSAndre Oppermann /* 1639f2512ba1SRui Paulo * TCP ECN processing. 1640f2512ba1SRui Paulo */ 1641f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) { 16429c251892SRui Paulo if (thflags & TH_CWR) 16439c251892SRui Paulo tp->t_flags &= ~TF_ECN_SND_ECE; 1644f2512ba1SRui Paulo switch (iptos & IPTOS_ECN_MASK) { 1645f2512ba1SRui Paulo case IPTOS_ECN_CE: 1646f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_ECE; 164778b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ce); 1648f2512ba1SRui Paulo break; 1649f2512ba1SRui Paulo case IPTOS_ECN_ECT0: 165078b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect0); 1651f2512ba1SRui Paulo break; 1652f2512ba1SRui Paulo case IPTOS_ECN_ECT1: 165378b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect1); 1654f2512ba1SRui Paulo break; 1655f2512ba1SRui Paulo } 165664807b30SHiren Panchasara 165764807b30SHiren Panchasara /* Process a packet differently from RFC3168. */ 165864807b30SHiren Panchasara cc_ecnpkt_handler(tp, th, iptos); 165964807b30SHiren Panchasara 1660dbc42409SLawrence Stewart /* Congestion experienced. */ 1661dbc42409SLawrence Stewart if (thflags & TH_ECE) { 1662dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_ECN); 1663f2512ba1SRui Paulo } 1664f2512ba1SRui Paulo } 1665f2512ba1SRui Paulo 1666f2512ba1SRui Paulo /* 1667f72167f4SAndre Oppermann * Parse options on any incoming segment. 1668f72167f4SAndre Oppermann */ 1669302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1670302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1671302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1672f72167f4SAndre Oppermann 1673fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1674fcf59617SAndrey V. Elsukov if ((tp->t_flags & TF_SIGNATURE) != 0 && 1675fcf59617SAndrey V. Elsukov (to.to_flags & TOF_SIGNATURE) == 0) { 1676fcf59617SAndrey V. Elsukov TCPSTAT_INC(tcps_sig_err_sigopt); 1677fcf59617SAndrey V. Elsukov /* XXX: should drop? */ 1678fcf59617SAndrey V. Elsukov } 1679fcf59617SAndrey V. Elsukov #endif 1680f72167f4SAndre Oppermann /* 1681f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1682bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1683bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1684bf6d304aSAndre Oppermann * was established. 1685f72167f4SAndre Oppermann */ 1686bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1687e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 1688d8951c8aSBjoern A. Zeeb if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) 1689f72167f4SAndre Oppermann to.to_tsecr = 0; 1690bf6d304aSAndre Oppermann } 169107dacf03SAndre Oppermann /* 169297d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 169397d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 16945396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 16955396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 169697d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1697df8bae1dSRodney W. Grimes */ 16980a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1699464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 1700464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 1701be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 170202a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 1703be2ac88cSJonathan Lemon } 17045396d0f8SAndre Oppermann /* 17055396d0f8SAndre Oppermann * Initial send window. It will be updated with 17065396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 17075396d0f8SAndre Oppermann */ 17085396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 1709be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1710be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1711be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1712d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1713be2ac88cSJonathan Lemon } 1714be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1715be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 17163529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 17173529149eSAndre Oppermann (to.to_flags & TOF_SACKPERM) == 0) 17183529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 17196d90faf3SPaul Saab } 17206d90faf3SPaul Saab 1721df8bae1dSRodney W. Grimes /* 17224da9052aSMichael Tuexen * If timestamps were negotiated during SYN/ACK they should 17234da9052aSMichael Tuexen * appear on every segment during this session and vice versa. 17244da9052aSMichael Tuexen */ 17254da9052aSMichael Tuexen if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) { 17264da9052aSMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 17274da9052aSMichael Tuexen log(LOG_DEBUG, "%s; %s: Timestamp missing, " 17284da9052aSMichael Tuexen "no action\n", s, __func__); 17294da9052aSMichael Tuexen free(s, M_TCPLOG); 17304da9052aSMichael Tuexen } 17314da9052aSMichael Tuexen } 17324da9052aSMichael Tuexen if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) { 17334da9052aSMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 17344da9052aSMichael Tuexen log(LOG_DEBUG, "%s; %s: Timestamp not expected, " 17354da9052aSMichael Tuexen "no action\n", s, __func__); 17364da9052aSMichael Tuexen free(s, M_TCPLOG); 17374da9052aSMichael Tuexen } 17384da9052aSMichael Tuexen } 17394da9052aSMichael Tuexen 17404da9052aSMichael Tuexen /* 1741df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1742df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1743df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1744df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1745df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1746df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1747df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1748df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1749df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1750df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1751df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1752df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1753a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1754c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 1755a0292f23SGarrett Wollman * be TH_NEEDSYN. 1756df8bae1dSRodney W. Grimes */ 1757df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1758c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 1759fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1760c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 1761c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 1762a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 17634741bfcbSPatrick Kelsey LIST_EMPTY(&tp->t_segq) && 17644741bfcbSPatrick Kelsey ((to.to_flags & TOF_TS) == 0 || 1765c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1766df8bae1dSRodney W. Grimes 1767df8bae1dSRodney W. Grimes /* 1768df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1769df8bae1dSRodney W. Grimes * record the timestamp. 1770a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1771a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1772df8bae1dSRodney W. Grimes */ 1773be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1774fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1775d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1776a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1777df8bae1dSRodney W. Grimes } 1778df8bae1dSRodney W. Grimes 1779fb59c426SYoshinobu Inoue if (tlen == 0) { 1780fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1781fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1782dbc42409SLawrence Stewart !IN_RECOVERY(tp->t_flags) && 1783fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1784dbc42409SLawrence Stewart TAILQ_EMPTY(&tp->snd_holes)) { 1785df8bae1dSRodney W. Grimes /* 1786e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1787df8bae1dSRodney W. Grimes */ 1788ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 1789ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1790252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1791252ca428SRobert Watson 179278b50714SRobert Watson TCPSTAT_INC(tcps_predack); 1793252ca428SRobert Watson 17949b8b58e0SJonathan Lemon /* 1795e8949f74SAndre Oppermann * "bad retransmit" recovery. 17969b8b58e0SJonathan Lemon */ 17979b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 1798672dc4aeSJohn Baldwin tp->t_flags & TF_PREVVALID && 17996dfb8b31SJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) { 1800dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 18019b8b58e0SJonathan Lemon } 1802fa55172bSMatthew Dillon 1803fa55172bSMatthew Dillon /* 1804fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1805fa55172bSMatthew Dillon * 1806fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1807fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1808fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1809fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1810fa55172bSMatthew Dillon */ 1811fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1812fa55172bSMatthew Dillon to.to_tsecr) { 18133ac12506SJonathan T. Looney uint32_t t; 1814d8951c8aSBjoern A. Zeeb 1815d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 1816d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 1817d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 1818a0292f23SGarrett Wollman tcp_xmit_timer(tp, 1819d8951c8aSBjoern A. Zeeb TCP_TS_TO_TICKS(t) + 1); 1820fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1821fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1822eaf80179SAndre Oppermann if (!tp->t_rttlow || 1823eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1824eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1825c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1826c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1827fa55172bSMatthew Dillon } 1828dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 182939bc9de5SLawrence Stewart 1830bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 183139bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 183239bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 1833bd79708dSJonathan T. Looney #endif 183439bc9de5SLawrence Stewart 18354b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvackpack, nsegs); 183678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 1837df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 18389d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 18399d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 18409d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 1841dbc42409SLawrence Stewart 1842dbc42409SLawrence Stewart /* 1843dbc42409SLawrence Stewart * Let the congestion control algorithm update 1844dbc42409SLawrence Stewart * congestion control related information. This 1845dbc42409SLawrence Stewart * typically means increasing the congestion 1846dbc42409SLawrence Stewart * window. 1847dbc42409SLawrence Stewart */ 18484b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, CC_ACK); 1849dbc42409SLawrence Stewart 18509d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 18511645d090SJeff Roberson /* 1852e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 18531645d090SJeff Roberson * to th_ack. 18541645d090SJeff Roberson */ 1855cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1856b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1857df8bae1dSRodney W. Grimes m_freem(m); 1858df8bae1dSRodney W. Grimes 1859df8bae1dSRodney W. Grimes /* 1860df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1861df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1862df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1863df8bae1dSRodney W. Grimes * If process is waiting for space, 1864df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1865df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1866df8bae1dSRodney W. Grimes * decide between more output or persist. 1867e8949f74SAndre Oppermann */ 18683c653157SHartmut Brandt #ifdef TCPDEBUG 18693c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 18703c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 18713c653157SHartmut Brandt (void *)tcp_saveipgen, 18723c653157SHartmut Brandt &tcp_savetcp, 0); 18733c653157SHartmut Brandt #endif 18742b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 1875df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1876b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1877b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1878b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1879b8152ba7SAndre Oppermann tp->t_rxtcur); 1880df8bae1dSRodney W. Grimes sowwakeup(so); 1881cfa6009eSGleb Smirnoff if (sbavail(&so->so_snd)) 188255bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 1883a14c749fSJonathan Lemon goto check_delack; 1884df8bae1dSRodney W. Grimes } 1885fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1886fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 18876741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 18886741ecf5SAndre Oppermann 1889df8bae1dSRodney W. Grimes /* 1890252ca428SRobert Watson * This is a pure, in-sequence data packet with 1891252ca428SRobert Watson * nothing on the reassembly queue and we have enough 1892252ca428SRobert Watson * buffer space to take it. 1893df8bae1dSRodney W. Grimes */ 1894ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 1895ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1896252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1897252ca428SRobert Watson 18986d90faf3SPaul Saab /* Clean receiver SACK report if present */ 18993529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 19006d90faf3SPaul Saab tcp_clean_sackreport(tp); 190178b50714SRobert Watson TCPSTAT_INC(tcps_preddat); 1902fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 19031645d090SJeff Roberson /* 19041645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 19051645d090SJeff Roberson * th_seq. 19061645d090SJeff Roberson */ 190760ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 19081645d090SJeff Roberson /* 19091645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 19101645d090SJeff Roberson * rcv_nxt. 19111645d090SJeff Roberson */ 19121645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 19134b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvpack, nsegs); 191478b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 19153c653157SHartmut Brandt #ifdef TCPDEBUG 19163c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 19173c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 19183c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 19193c653157SHartmut Brandt #endif 19202b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 19215d06879aSGeorge V. Neville-Neil 1922e44c1887SSteven Hartland newsize = tcp_autorcvbuf(m, th, so, tp, tlen); 19236741ecf5SAndre Oppermann 19246741ecf5SAndre Oppermann /* Add data to socket buffer. */ 19251e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1926c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1927c1c36a2cSMike Silbersack m_freem(m); 1928c1c36a2cSMike Silbersack } else { 19296741ecf5SAndre Oppermann /* 19306741ecf5SAndre Oppermann * Set new socket buffer size. 19316741ecf5SAndre Oppermann * Give up when limit is reached. 19326741ecf5SAndre Oppermann */ 19336741ecf5SAndre Oppermann if (newsize) 19346741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 19356c8286e4SRobert Watson newsize, so, NULL)) 19366741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1937fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 1938651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 1939c1c36a2cSMike Silbersack } 1940e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 19411e4d7da7SRobert Watson sorwakeup_locked(so); 1942c1e5a6e5SAndre Oppermann if (DELAY_ACK(tp, tlen)) { 19433bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1944f498eeeeSDavid Greenman } else { 1945e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 194655bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 1947e612a582SDavid Greenman } 1948a14c749fSJonathan Lemon goto check_delack; 1949df8bae1dSRodney W. Grimes } 1950df8bae1dSRodney W. Grimes } 1951df8bae1dSRodney W. Grimes 1952df8bae1dSRodney W. Grimes /* 1953df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1954df8bae1dSRodney W. Grimes * and then do TCP input processing. 1955df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1956df8bae1dSRodney W. Grimes * but not less than advertised window. 1957df8bae1dSRodney W. Grimes */ 1958df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1959df8bae1dSRodney W. Grimes if (win < 0) 1960df8bae1dSRodney W. Grimes win = 0; 196166e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1962df8bae1dSRodney W. Grimes 1963df8bae1dSRodney W. Grimes switch (tp->t_state) { 1964df8bae1dSRodney W. Grimes 1965df8bae1dSRodney W. Grimes /* 1966764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1967764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1968764d8cefSBill Fenner */ 1969764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1970fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1971fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1972a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1973a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1974a57815efSBosko Milekic goto dropwithreset; 1975a57815efSBosko Milekic } 1976281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 197768bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) { 1978281a0fd4SPatrick Kelsey /* 1979281a0fd4SPatrick Kelsey * When a TFO connection is in SYN_RECEIVED, the 1980281a0fd4SPatrick Kelsey * only valid packets are the initial SYN, a 1981281a0fd4SPatrick Kelsey * retransmit/copy of the initial SYN (possibly with 1982281a0fd4SPatrick Kelsey * a subset of the original data), a valid ACK, a 1983281a0fd4SPatrick Kelsey * FIN, or a RST. 1984281a0fd4SPatrick Kelsey */ 1985281a0fd4SPatrick Kelsey if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) { 1986281a0fd4SPatrick Kelsey rstreason = BANDLIM_RST_OPENPORT; 1987281a0fd4SPatrick Kelsey goto dropwithreset; 1988281a0fd4SPatrick Kelsey } else if (thflags & TH_SYN) { 1989281a0fd4SPatrick Kelsey /* non-initial SYN is ignored */ 1990281a0fd4SPatrick Kelsey if ((tcp_timer_active(tp, TT_DELACK) || 1991281a0fd4SPatrick Kelsey tcp_timer_active(tp, TT_REXMT))) 1992281a0fd4SPatrick Kelsey goto drop; 1993281a0fd4SPatrick Kelsey } else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) { 1994281a0fd4SPatrick Kelsey goto drop; 1995281a0fd4SPatrick Kelsey } 1996281a0fd4SPatrick Kelsey } 1997281a0fd4SPatrick Kelsey #endif 1998764d8cefSBill Fenner break; 1999764d8cefSBill Fenner 2000764d8cefSBill Fenner /* 2001df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 200298732609SMichael Tuexen * if seg contains a RST with valid ACK (SEQ.ACK has already 200398732609SMichael Tuexen * been verified), then drop the connection. 200498732609SMichael Tuexen * if seg contains a RST without an ACK, drop the seg. 200598732609SMichael Tuexen * if seg does not contain SYN, then drop the seg. 2006df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 2007df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 2008df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 2009f2512ba1SRui Paulo * if seg contains an ECE and ECN support is enabled, the stream 2010f2512ba1SRui Paulo * is ECN capable. 2011df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 2012df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 2013df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 2014df8bae1dSRodney W. Grimes */ 2015df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 201657f60867SMark Johnston if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) { 2017d9fae5abSAndriy Gapon TCP_PROBE5(connect__refused, NULL, tp, 20182b9c9984SGeorge V. Neville-Neil m, tp, th); 2019df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 202057f60867SMark Johnston } 20210ca3f933SAndre Oppermann if (thflags & TH_RST) 2022df8bae1dSRodney W. Grimes goto drop; 20230ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 2024df8bae1dSRodney W. Grimes goto drop; 2025464fcfbcSAndre Oppermann 2026fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 2027df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 2028fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 202978b50714SRobert Watson TCPSTAT_INC(tcps_connects); 2030845799c1SAndras Olah soisconnected(so); 2031c488362eSRobert Watson #ifdef MAC 203230d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 2033c488362eSRobert Watson #endif 2034845799c1SAndras Olah /* Do window scaling on this connection? */ 2035845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2036845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2037845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 2038845799c1SAndras Olah } 20393ac12506SJonathan T. Looney tp->rcv_adv += min(tp->rcv_wnd, 2040766282cbSJohn Baldwin TCP_MAXWIN << tp->rcv_scale); 2041a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 2042a0292f23SGarrett Wollman /* 2043a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 2044a0292f23SGarrett Wollman * ACKNOW will be turned on later. 2045a0292f23SGarrett Wollman */ 2046c1e5a6e5SAndre Oppermann if (DELAY_ACK(tp, tlen) && tlen != 0) 2047b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 2048b8152ba7SAndre Oppermann tcp_delacktime); 2049a0292f23SGarrett Wollman else 2050a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2051f2512ba1SRui Paulo 2052603724d3SBjoern A. Zeeb if ((thflags & TH_ECE) && V_tcp_do_ecn) { 2053f2512ba1SRui Paulo tp->t_flags |= TF_ECN_PERMIT; 205478b50714SRobert Watson TCPSTAT_INC(tcps_ecn_shs); 2055f2512ba1SRui Paulo } 2056f2512ba1SRui Paulo 2057a0292f23SGarrett Wollman /* 2058a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 2059a0292f23SGarrett Wollman * Transitions: 2060a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 2061a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 2062a0292f23SGarrett Wollman */ 20639b8b58e0SJonathan Lemon tp->t_starttime = ticks; 2064a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 206557f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2066a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 2067fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 20687ff19458SPaul Traina } else { 206957f60867SMark Johnston tcp_state_change(tp, TCPS_ESTABLISHED); 2070d9fae5abSAndriy Gapon TCP_PROBE5(connect__established, NULL, tp, 20712b9c9984SGeorge V. Neville-Neil m, tp, th); 2072dbc42409SLawrence Stewart cc_conn_init(tp); 20739077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 20749077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 20757ff19458SPaul Traina } 2076a0292f23SGarrett Wollman } else { 2077a0292f23SGarrett Wollman /* 2078c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 2079153edc50SHiren Panchasara * simultaneous open. 2080c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 2081c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 2082a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 2083a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 2084a0292f23SGarrett Wollman */ 20854b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 2086b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 208757f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_RECEIVED); 2088a0292f23SGarrett Wollman } 2089df8bae1dSRodney W. Grimes 2090ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, ("%s: trimthenstep6: " 2091252ca428SRobert Watson "ti_locked %d", __func__, ti_locked)); 2092ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 20938501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 20947cfc6904SRobert Watson 2095df8bae1dSRodney W. Grimes /* 2096fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 2097df8bae1dSRodney W. Grimes * If data, trim to stay within window, 2098df8bae1dSRodney W. Grimes * dropping FIN if necessary. 2099df8bae1dSRodney W. Grimes */ 2100fb59c426SYoshinobu Inoue th->th_seq++; 2101fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 2102fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 2103df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2104fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 2105fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 210678b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 210778b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2108df8bae1dSRodney W. Grimes } 2109fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 2110fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 2111a0292f23SGarrett Wollman /* 2112a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 2113a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 2114a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 2115a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 2116a0292f23SGarrett Wollman * Otherwise, goto step 6. 2117a0292f23SGarrett Wollman */ 2118fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 2119a0292f23SGarrett Wollman goto process_ACK; 2120c068736aSJeffrey Hsu 2121df8bae1dSRodney W. Grimes goto step6; 2122c068736aSJeffrey Hsu 2123a0292f23SGarrett Wollman /* 2124a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 2125c94c54e4SAndre Oppermann * do normal processing. 2126a0292f23SGarrett Wollman * 2127c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 2128a0292f23SGarrett Wollman */ 2129a0292f23SGarrett Wollman case TCPS_LAST_ACK: 2130a0292f23SGarrett Wollman case TCPS_CLOSING: 2131a0292f23SGarrett Wollman break; /* continue normal processing */ 2132df8bae1dSRodney W. Grimes } 2133df8bae1dSRodney W. Grimes 2134df8bae1dSRodney W. Grimes /* 2135df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 213680ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 213780ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 213880ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 213980ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 214080ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 214180ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 214280ab7c0eSGarrett Wollman * killing a connection). 214380ab7c0eSGarrett Wollman * Then check timestamp, if present. 2144a0292f23SGarrett Wollman * Then check the connection count, if present. 2145df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 2146df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 2147df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 214880ab7c0eSGarrett Wollman */ 2149fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 21503220a212SGleb Smirnoff /* 21513220a212SGleb Smirnoff * RFC5961 Section 3.2 21523220a212SGleb Smirnoff * 21533220a212SGleb Smirnoff * - RST drops connection only if SEG.SEQ == RCV.NXT. 21543220a212SGleb Smirnoff * - If RST is in window, we send challenge ACK. 21553220a212SGleb Smirnoff * 21563220a212SGleb Smirnoff * Note: to take into account delayed ACKs, we should 21573220a212SGleb Smirnoff * test against last_ack_sent instead of rcv_nxt. 21583220a212SGleb Smirnoff * Note 2: we handle special case of closed window, not 21593220a212SGleb Smirnoff * covered by the RFC. 21603220a212SGleb Smirnoff */ 21613220a212SGleb Smirnoff if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 21623220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) || 21633220a212SGleb Smirnoff (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) { 216480ab7c0eSGarrett Wollman 2165ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2166ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, 21673220a212SGleb Smirnoff ("%s: TH_RST ti_locked %d, th %p tp %p", 21683220a212SGleb Smirnoff __func__, ti_locked, th, tp)); 21693220a212SGleb Smirnoff KASSERT(tp->t_state != TCPS_SYN_SENT, 21703220a212SGleb Smirnoff ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p", 21713220a212SGleb Smirnoff __func__, th, tp)); 21723220a212SGleb Smirnoff 21733220a212SGleb Smirnoff if (V_tcp_insecure_rst || 21743220a212SGleb Smirnoff tp->last_ack_sent == th->th_seq) { 21753220a212SGleb Smirnoff TCPSTAT_INC(tcps_drops); 21763220a212SGleb Smirnoff /* Drop the connection. */ 21773220a212SGleb Smirnoff switch (tp->t_state) { 217880ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 217980ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 218080ab7c0eSGarrett Wollman goto close; 218180ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 218280ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 218380ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 218480ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 21856779a1a1SMichael Tuexen case TCPS_CLOSING: 21866779a1a1SMichael Tuexen case TCPS_LAST_ACK: 218780ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 218880ab7c0eSGarrett Wollman close: 21893220a212SGleb Smirnoff /* FALLTHROUGH */ 21903220a212SGleb Smirnoff default: 219180ab7c0eSGarrett Wollman tp = tcp_close(tp); 21923220a212SGleb Smirnoff } 21933220a212SGleb Smirnoff } else { 21943220a212SGleb Smirnoff TCPSTAT_INC(tcps_badrst); 21953220a212SGleb Smirnoff /* Send challenge ACK. */ 21963220a212SGleb Smirnoff tcp_respond(tp, mtod(m, void *), th, m, 21973220a212SGleb Smirnoff tp->rcv_nxt, tp->snd_nxt, TH_ACK); 21983220a212SGleb Smirnoff tp->last_ack_sent = tp->rcv_nxt; 21993220a212SGleb Smirnoff m = NULL; 22003220a212SGleb Smirnoff } 22013220a212SGleb Smirnoff } 22023220a212SGleb Smirnoff goto drop; 22033220a212SGleb Smirnoff } 220480ab7c0eSGarrett Wollman 22053220a212SGleb Smirnoff /* 22063220a212SGleb Smirnoff * RFC5961 Section 4.2 22073220a212SGleb Smirnoff * Send challenge ACK for any SYN in synchronized state. 22083220a212SGleb Smirnoff */ 2209281a0fd4SPatrick Kelsey if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT && 2210281a0fd4SPatrick Kelsey tp->t_state != TCPS_SYN_RECEIVED) { 2211ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, 22123220a212SGleb Smirnoff ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked)); 2213ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2214252ca428SRobert Watson 22153220a212SGleb Smirnoff TCPSTAT_INC(tcps_badsyn); 22163220a212SGleb Smirnoff if (V_tcp_insecure_syn && 22173220a212SGleb Smirnoff SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 22183220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 22193220a212SGleb Smirnoff tp = tcp_drop(tp, ECONNRESET); 22203220a212SGleb Smirnoff rstreason = BANDLIM_UNLIMITED; 22213220a212SGleb Smirnoff } else { 22223220a212SGleb Smirnoff /* Send challenge ACK. */ 22233220a212SGleb Smirnoff tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt, 22243220a212SGleb Smirnoff tp->snd_nxt, TH_ACK); 22253220a212SGleb Smirnoff tp->last_ack_sent = tp->rcv_nxt; 22263220a212SGleb Smirnoff m = NULL; 222780ab7c0eSGarrett Wollman } 222880ab7c0eSGarrett Wollman goto drop; 222980ab7c0eSGarrett Wollman } 223080ab7c0eSGarrett Wollman 223180ab7c0eSGarrett Wollman /* 2232df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 2233df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 2234df8bae1dSRodney W. Grimes */ 2235be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 223680ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 2237df8bae1dSRodney W. Grimes 2238df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 2239d8951c8aSBjoern A. Zeeb if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 2240df8bae1dSRodney W. Grimes /* 2241df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 2242df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 2243df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 2244df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 2245df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 2246df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 2247df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 2248df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 2249df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 2250df8bae1dSRodney W. Grimes */ 2251df8bae1dSRodney W. Grimes tp->ts_recent = 0; 2252df8bae1dSRodney W. Grimes } else { 225378b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 225478b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 225578b50714SRobert Watson TCPSTAT_INC(tcps_pawsdrop); 225607fd333dSMatthew Dillon if (tlen) 2257df8bae1dSRodney W. Grimes goto dropafterack; 22581ab4789dSMatthew Dillon goto drop; 22591ab4789dSMatthew Dillon } 2260df8bae1dSRodney W. Grimes } 2261df8bae1dSRodney W. Grimes 2262a0292f23SGarrett Wollman /* 226380ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 226480ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 226580ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 226680ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 226780ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 226880ab7c0eSGarrett Wollman */ 2269a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 2270a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2271a57815efSBosko Milekic goto dropwithreset; 2272a57815efSBosko Milekic } 227380ab7c0eSGarrett Wollman 2274fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 2275df8bae1dSRodney W. Grimes if (todrop > 0) { 2276831ad37eSXin LI if (thflags & TH_SYN) { 2277fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 2278fb59c426SYoshinobu Inoue th->th_seq++; 2279fb59c426SYoshinobu Inoue if (th->th_urp > 1) 2280fb59c426SYoshinobu Inoue th->th_urp--; 2281df8bae1dSRodney W. Grimes else 2282fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2283df8bae1dSRodney W. Grimes todrop--; 2284df8bae1dSRodney W. Grimes } 2285df8bae1dSRodney W. Grimes /* 2286dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 2287df8bae1dSRodney W. Grimes */ 2288fb59c426SYoshinobu Inoue if (todrop > tlen 2289fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 2290dac20301SGarrett Wollman /* 2291dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 2292dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 2293dac20301SGarrett Wollman * of sequence; drop it. 2294dac20301SGarrett Wollman */ 2295fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2296dac20301SGarrett Wollman 2297df8bae1dSRodney W. Grimes /* 2298dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 2299dac20301SGarrett Wollman * But keep on processing for RST or ACK. 2300df8bae1dSRodney W. Grimes */ 2301dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2302fb59c426SYoshinobu Inoue todrop = tlen; 230378b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 230478b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 2305df8bae1dSRodney W. Grimes } else { 230678b50714SRobert Watson TCPSTAT_INC(tcps_rcvpartduppack); 230778b50714SRobert Watson TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 2308df8bae1dSRodney W. Grimes } 2309fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 2310fb59c426SYoshinobu Inoue th->th_seq += todrop; 2311fb59c426SYoshinobu Inoue tlen -= todrop; 2312fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 2313fb59c426SYoshinobu Inoue th->th_urp -= todrop; 2314df8bae1dSRodney W. Grimes else { 2315fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2316fb59c426SYoshinobu Inoue th->th_urp = 0; 2317df8bae1dSRodney W. Grimes } 2318df8bae1dSRodney W. Grimes } 2319df8bae1dSRodney W. Grimes 2320df8bae1dSRodney W. Grimes /* 2321df8bae1dSRodney W. Grimes * If new data are received on a connection after the 2322df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 2323df8bae1dSRodney W. Grimes */ 2324df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 2325fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 2326ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, ("%s: SS_NOFDEREF && " 2327252ca428SRobert Watson "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked)); 2328ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2329252ca428SRobert Watson 233007dacf03SAndre Oppermann if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 233107dacf03SAndre Oppermann log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data " 233207dacf03SAndre Oppermann "after socket was closed, " 233307dacf03SAndre Oppermann "sending RST and removing tcpcb\n", 2334e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 2335773673c1SAndre Oppermann free(s, M_TCPLOG); 2336773673c1SAndre Oppermann } 2337df8bae1dSRodney W. Grimes tp = tcp_close(tp); 233878b50714SRobert Watson TCPSTAT_INC(tcps_rcvafterclose); 2339a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2340df8bae1dSRodney W. Grimes goto dropwithreset; 2341df8bae1dSRodney W. Grimes } 2342df8bae1dSRodney W. Grimes 2343df8bae1dSRodney W. Grimes /* 2344df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 2345df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 2346df8bae1dSRodney W. Grimes */ 2347fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 2348df8bae1dSRodney W. Grimes if (todrop > 0) { 234978b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 2350fb59c426SYoshinobu Inoue if (todrop >= tlen) { 235178b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 2352df8bae1dSRodney W. Grimes /* 2353df8bae1dSRodney W. Grimes * If window is closed can only take segments at 2354df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 2355df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 2356df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 2357df8bae1dSRodney W. Grimes * and ack. 2358df8bae1dSRodney W. Grimes */ 2359fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2360df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 236178b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinprobe); 2362df8bae1dSRodney W. Grimes } else 2363df8bae1dSRodney W. Grimes goto dropafterack; 2364df8bae1dSRodney W. Grimes } else 236578b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2366df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2367fb59c426SYoshinobu Inoue tlen -= todrop; 2368fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 2369df8bae1dSRodney W. Grimes } 2370df8bae1dSRodney W. Grimes 2371df8bae1dSRodney W. Grimes /* 2372df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 2373df8bae1dSRodney W. Grimes * record its timestamp. 2374cf09195bSPaul Saab * NOTE: 2375cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 2376a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2377cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 2378cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 2379cf09195bSPaul Saab * predicated on the sequence space of this segment. 2380cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 2381cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 2382cf09195bSPaul Saab * instead of RFC1323's 2383cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 2384cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 2385cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 2386cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 2387cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 2388df8bae1dSRodney W. Grimes */ 2389be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 2390cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2391cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 2392cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 2393d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 2394a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 2395df8bae1dSRodney W. Grimes } 2396df8bae1dSRodney W. Grimes 2397df8bae1dSRodney W. Grimes /* 2398a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 2399a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 2400a0292f23SGarrett Wollman * later processing; else drop segment and return. 2401a0292f23SGarrett Wollman */ 2402fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 2403a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 2404281a0fd4SPatrick Kelsey (tp->t_flags & TF_NEEDSYN)) { 2405281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 2406281a0fd4SPatrick Kelsey if (tp->t_state == TCPS_SYN_RECEIVED && 240768bd7ed1SJonathan T. Looney IS_FASTOPEN(tp->t_flags)) { 2408281a0fd4SPatrick Kelsey tp->snd_wnd = tiwin; 2409281a0fd4SPatrick Kelsey cc_conn_init(tp); 2410281a0fd4SPatrick Kelsey } 2411281a0fd4SPatrick Kelsey #endif 2412a0292f23SGarrett Wollman goto step6; 2413281a0fd4SPatrick Kelsey } else if (tp->t_flags & TF_ACKNOW) 24145e1aa279SDavid Malone goto dropafterack; 2415a0292f23SGarrett Wollman else 2416a0292f23SGarrett Wollman goto drop; 2417a0292f23SGarrett Wollman } 2418df8bae1dSRodney W. Grimes 2419df8bae1dSRodney W. Grimes /* 2420df8bae1dSRodney W. Grimes * Ack processing. 2421df8bae1dSRodney W. Grimes */ 2422df8bae1dSRodney W. Grimes switch (tp->t_state) { 2423df8bae1dSRodney W. Grimes 2424df8bae1dSRodney W. Grimes /* 2425764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 2426764d8cefSBill Fenner * ESTABLISHED state and continue processing. 2427764d8cefSBill Fenner * The ACK was checked above. 2428df8bae1dSRodney W. Grimes */ 2429df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2430a0292f23SGarrett Wollman 243178b50714SRobert Watson TCPSTAT_INC(tcps_connects); 2432df8bae1dSRodney W. Grimes soisconnected(so); 2433df8bae1dSRodney W. Grimes /* Do window scaling? */ 2434df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2435df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2436df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 2437464fcfbcSAndre Oppermann tp->snd_wnd = tiwin; 2438df8bae1dSRodney W. Grimes } 2439a0292f23SGarrett Wollman /* 2440a0292f23SGarrett Wollman * Make transitions: 2441a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 2442a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 2443a0292f23SGarrett Wollman */ 24449b8b58e0SJonathan Lemon tp->t_starttime = ticks; 2445a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 244657f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2447a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 24487ff19458SPaul Traina } else { 244957f60867SMark Johnston tcp_state_change(tp, TCPS_ESTABLISHED); 2450d9fae5abSAndriy Gapon TCP_PROBE5(accept__established, NULL, tp, 24512b9c9984SGeorge V. Neville-Neil m, tp, th); 2452281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 2453281a0fd4SPatrick Kelsey if (tp->t_tfo_pending) { 2454281a0fd4SPatrick Kelsey tcp_fastopen_decrement_counter(tp->t_tfo_pending); 2455281a0fd4SPatrick Kelsey tp->t_tfo_pending = NULL; 2456281a0fd4SPatrick Kelsey 2457281a0fd4SPatrick Kelsey /* 2458281a0fd4SPatrick Kelsey * Account for the ACK of our SYN prior to 2459281a0fd4SPatrick Kelsey * regular ACK processing below. 2460281a0fd4SPatrick Kelsey */ 2461281a0fd4SPatrick Kelsey tp->snd_una++; 2462281a0fd4SPatrick Kelsey } 2463281a0fd4SPatrick Kelsey /* 2464281a0fd4SPatrick Kelsey * TFO connections call cc_conn_init() during SYN 2465281a0fd4SPatrick Kelsey * processing. Calling it again here for such 2466281a0fd4SPatrick Kelsey * connections is not harmless as it would undo the 2467281a0fd4SPatrick Kelsey * snd_cwnd reduction that occurs when a TFO SYN|ACK 2468281a0fd4SPatrick Kelsey * is retransmitted. 2469281a0fd4SPatrick Kelsey */ 247068bd7ed1SJonathan T. Looney if (!IS_FASTOPEN(tp->t_flags)) 2471281a0fd4SPatrick Kelsey #endif 2472dbc42409SLawrence Stewart cc_conn_init(tp); 24739077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 24747ff19458SPaul Traina } 2475a0292f23SGarrett Wollman /* 2476a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 2477a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 2478a0292f23SGarrett Wollman */ 2479fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 2480fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 2481a0292f23SGarrett Wollman (struct mbuf *)0); 248260ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq - 1; 248393b0017fSPhilippe Charnier /* FALLTHROUGH */ 2484df8bae1dSRodney W. Grimes 2485df8bae1dSRodney W. Grimes /* 2486df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2487df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 2488fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 2489fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 2490df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 2491df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 2492df8bae1dSRodney W. Grimes */ 2493df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2494df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2495df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2496df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 2497df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2498df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 24995a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 250078b50714SRobert Watson TCPSTAT_INC(tcps_rcvacktoomuch); 25015a53ca16SPaul Saab goto dropafterack; 25025a53ca16SPaul Saab } 25033529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2504fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 2505fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 2506021eaf79SHiren Panchasara sack_changed = tcp_sack_doack(tp, &to, th->th_ack); 250712eeb81fSHiren Panchasara else 250812eeb81fSHiren Panchasara /* 250912eeb81fSHiren Panchasara * Reset the value so that previous (valid) value 251012eeb81fSHiren Panchasara * from the last ack with SACK doesn't get used. 251112eeb81fSHiren Panchasara */ 251212eeb81fSHiren Panchasara tp->sackhint.sacked_bytes = 0; 251339bc9de5SLawrence Stewart 2514bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 251539bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 251639bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 2517bd79708dSJonathan T. Looney #endif 251839bc9de5SLawrence Stewart 2519fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 25200c39d38dSGleb Smirnoff u_int maxseg; 25210c39d38dSGleb Smirnoff 25220c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 2523021eaf79SHiren Panchasara if (tlen == 0 && 2524021eaf79SHiren Panchasara (tiwin == tp->snd_wnd || 2525021eaf79SHiren Panchasara (tp->t_flags & TF_SACK_PERMIT))) { 252662b90589SPeter Wemm /* 252762b90589SPeter Wemm * If this is the first time we've seen a 252862b90589SPeter Wemm * FIN from the remote, this is not a 252962b90589SPeter Wemm * duplicate and it needs to be processed 253062b90589SPeter Wemm * normally. This happens during a 253162b90589SPeter Wemm * simultaneous close. 253262b90589SPeter Wemm */ 253362b90589SPeter Wemm if ((thflags & TH_FIN) && 253462b90589SPeter Wemm (TCPS_HAVERCVDFIN(tp->t_state) == 0)) { 253562b90589SPeter Wemm tp->t_dupacks = 0; 253662b90589SPeter Wemm break; 253762b90589SPeter Wemm } 253878b50714SRobert Watson TCPSTAT_INC(tcps_rcvdupack); 2539df8bae1dSRodney W. Grimes /* 2540df8bae1dSRodney W. Grimes * If we have outstanding data (other than 2541df8bae1dSRodney W. Grimes * a window probe), this is a completely 2542df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 25435f30ec9bSEitan Adler * change and FIN isn't set), 25445f30ec9bSEitan Adler * the ack is the biggest we've 2545df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 2546a4641f4eSPedro F. Giffuni * threshold of them, assume a packet 2547df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 2548df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 2549df8bae1dSRodney W. Grimes * window so we send only this one 2550df8bae1dSRodney W. Grimes * packet. 2551df8bae1dSRodney W. Grimes * 2552df8bae1dSRodney W. Grimes * We know we're losing at the current 2553df8bae1dSRodney W. Grimes * window size so do congestion avoidance 2554df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 2555df8bae1dSRodney W. Grimes * and pull our congestion window back to 2556df8bae1dSRodney W. Grimes * the new ssthresh). 2557df8bae1dSRodney W. Grimes * 2558df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 2559df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 2560df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 2561df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 2562df8bae1dSRodney W. Grimes * network. 2563f2512ba1SRui Paulo * 2564f2512ba1SRui Paulo * When using TCP ECN, notify the peer that 2565f2512ba1SRui Paulo * we reduced the cwnd. 2566df8bae1dSRodney W. Grimes */ 2567021eaf79SHiren Panchasara /* 2568021eaf79SHiren Panchasara * Following 2 kinds of acks should not affect 2569021eaf79SHiren Panchasara * dupack counting: 2570021eaf79SHiren Panchasara * 1) Old acks 2571021eaf79SHiren Panchasara * 2) Acks with SACK but without any new SACK 2572021eaf79SHiren Panchasara * information in them. These could result from 2573021eaf79SHiren Panchasara * any anomaly in the network like a switch 2574021eaf79SHiren Panchasara * duplicating packets or a possible DoS attack. 2575021eaf79SHiren Panchasara */ 2576021eaf79SHiren Panchasara if (th->th_ack != tp->snd_una || 2577021eaf79SHiren Panchasara ((tp->t_flags & TF_SACK_PERMIT) && 2578021eaf79SHiren Panchasara !sack_changed)) 2579021eaf79SHiren Panchasara break; 2580021eaf79SHiren Panchasara else if (!tcp_timer_active(tp, TT_REXMT)) 2581df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2582cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 2583dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 25844b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 25854b7b743cSLawrence Stewart CC_DUPACK); 25863529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2587dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2588808f11b7SPaul Saab int awnd; 258925e6f9edSPaul Saab 259025e6f9edSPaul Saab /* 259125e6f9edSPaul Saab * Compute the amount of data in flight first. 25924c3972f0SHiren Panchasara * We can inject new data into the pipe iff 259325e6f9edSPaul Saab * we have less than 1/2 the original window's 259425e6f9edSPaul Saab * worth of data in flight. 259525e6f9edSPaul Saab */ 259612eeb81fSHiren Panchasara if (V_tcp_do_rfc6675_pipe) 259712eeb81fSHiren Panchasara awnd = tcp_compute_pipe(tp); 259812eeb81fSHiren Panchasara else 2599808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 26000077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 260112eeb81fSHiren Panchasara 2602808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 26030c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 260425e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 260525e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 260625e6f9edSPaul Saab } 260725e6f9edSPaul Saab } else 26080c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 260955bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 261046f58482SJonathan Lemon goto drop; 2611cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 2612cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 2613a0445c2eSJayanth Vijayaraghavan 2614a0445c2eSJayanth Vijayaraghavan /* 2615a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 2616a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 2617a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 2618a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 2619a0445c2eSJayanth Vijayaraghavan * recovery. 2620a0445c2eSJayanth Vijayaraghavan */ 26213529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 2622dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2623a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 2624a0445c2eSJayanth Vijayaraghavan break; 2625a0445c2eSJayanth Vijayaraghavan } 2626dbc42409SLawrence Stewart } else { 2627a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 26289d11646dSJeffrey Hsu tp->snd_recover)) { 2629cb942153SJeffrey Hsu tp->t_dupacks = 0; 2630cb942153SJeffrey Hsu break; 263146f58482SJonathan Lemon } 2632a0445c2eSJayanth Vijayaraghavan } 2633dbc42409SLawrence Stewart /* Congestion signal before ack. */ 2634dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_NDUPACK); 26354b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 26364b7b743cSLawrence Stewart CC_DUPACK); 2637b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 26389b8b58e0SJonathan Lemon tp->t_rtttime = 0; 26393529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 264078b50714SRobert Watson TCPSTAT_INC( 264178b50714SRobert Watson tcps_sack_recovery_episode); 2642a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 26430c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 264455bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 26456d90faf3SPaul Saab goto drop; 26466d90faf3SPaul Saab } 2647fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 26480c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 264955bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 265048d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 2651302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 2652302ce8d6SAndre Oppermann __func__)); 2653df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 26540c39d38dSGleb Smirnoff maxseg * 265548d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 2656df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 2657df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 2658df8bae1dSRodney W. Grimes goto drop; 2659603724d3SBjoern A. Zeeb } else if (V_tcp_do_rfc3042) { 266062d4443fSHiren Panchasara /* 266162d4443fSHiren Panchasara * Process first and second duplicate 266262d4443fSHiren Panchasara * ACKs. Each indicates a segment 266362d4443fSHiren Panchasara * leaving the network, creating room 266462d4443fSHiren Panchasara * for more. Make sure we can send a 266562d4443fSHiren Panchasara * packet on reception of each duplicate 266662d4443fSHiren Panchasara * ACK by increasing snd_cwnd by one 266762d4443fSHiren Panchasara * segment. Restore the original 266862d4443fSHiren Panchasara * snd_cwnd after packet transmission. 266962d4443fSHiren Panchasara */ 26704b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 26714b7b743cSLawrence Stewart CC_DUPACK); 26723ac12506SJonathan T. Looney uint32_t oldcwnd = tp->snd_cwnd; 267348d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 267448d2549cSJeffrey Hsu u_int sent; 26755628dd08SAndre Oppermann int avail; 267689c02376SJeffrey Hsu 2677582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 2678582a954bSJeffrey Hsu tp->t_dupacks == 2, 2679302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 2680302ce8d6SAndre Oppermann __func__)); 268161a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 268248d2549cSJeffrey Hsu tp->snd_limited = 0; 268361a36e3dSJeffrey Hsu tp->snd_cwnd = 268461a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 268561a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 26860c39d38dSGleb Smirnoff maxseg; 26875628dd08SAndre Oppermann /* 26885628dd08SAndre Oppermann * Only call tcp_output when there 26895628dd08SAndre Oppermann * is new data available to be sent. 26905628dd08SAndre Oppermann * Otherwise we would send pure ACKs. 26915628dd08SAndre Oppermann */ 26925628dd08SAndre Oppermann SOCKBUF_LOCK(&so->so_snd); 2693cfa6009eSGleb Smirnoff avail = sbavail(&so->so_snd) - 26945628dd08SAndre Oppermann (tp->snd_nxt - tp->snd_una); 26955628dd08SAndre Oppermann SOCKBUF_UNLOCK(&so->so_snd); 26965628dd08SAndre Oppermann if (avail > 0) 269755bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 269848d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 26990c39d38dSGleb Smirnoff if (sent > maxseg) { 270089c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 270189c02376SJeffrey Hsu tp->snd_limited == 0) || 27020c39d38dSGleb Smirnoff (sent == maxseg + 1 && 270389c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 2704302ce8d6SAndre Oppermann ("%s: sent too much", 2705302ce8d6SAndre Oppermann __func__)); 270648d2549cSJeffrey Hsu tp->snd_limited = 2; 270748d2549cSJeffrey Hsu } else if (sent > 0) 270848d2549cSJeffrey Hsu ++tp->snd_limited; 2709582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 2710582a954bSJeffrey Hsu goto drop; 2711df8bae1dSRodney W. Grimes } 2712021eaf79SHiren Panchasara } 2713df8bae1dSRodney W. Grimes break; 2714021eaf79SHiren Panchasara } else { 2715021eaf79SHiren Panchasara /* 2716021eaf79SHiren Panchasara * This ack is advancing the left edge, reset the 2717021eaf79SHiren Panchasara * counter. 2718021eaf79SHiren Panchasara */ 2719021eaf79SHiren Panchasara tp->t_dupacks = 0; 2720021eaf79SHiren Panchasara /* 2721021eaf79SHiren Panchasara * If this ack also has new SACK info, increment the 2722021eaf79SHiren Panchasara * counter as per rfc6675. 2723021eaf79SHiren Panchasara */ 2724021eaf79SHiren Panchasara if ((tp->t_flags & TF_SACK_PERMIT) && sack_changed) 2725021eaf79SHiren Panchasara tp->t_dupacks++; 2726df8bae1dSRodney W. Grimes } 2727cb942153SJeffrey Hsu 2728302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2729302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 2730cb942153SJeffrey Hsu 2731df8bae1dSRodney W. Grimes /* 2732df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2733df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2734df8bae1dSRodney W. Grimes */ 2735dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2736cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 27373529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 27386d90faf3SPaul Saab tcp_sack_partialack(tp, th); 27396d90faf3SPaul Saab else 2740c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2741dbc42409SLawrence Stewart } else 2742dbc42409SLawrence Stewart cc_post_recovery(tp, th); 274346f58482SJonathan Lemon } 2744a0292f23SGarrett Wollman /* 2745a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2746a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2747a0292f23SGarrett Wollman */ 2748a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2749a0292f23SGarrett Wollman /* 2750a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2751a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 275207e43e10SAndras Olah * synchronized). Go to non-starred state, 275307e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 275407e43e10SAndras Olah * we can do window scaling. 2755a0292f23SGarrett Wollman */ 2756a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2757a0292f23SGarrett Wollman tp->snd_una++; 275807e43e10SAndras Olah /* Do window scaling? */ 275907e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 276007e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 276107e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2762464fcfbcSAndre Oppermann /* Send window already scaled. */ 276307e43e10SAndras Olah } 2764a0292f23SGarrett Wollman } 2765a0292f23SGarrett Wollman 2766a0292f23SGarrett Wollman process_ACK: 27678501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 27687cfc6904SRobert Watson 2769dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 2770b8c2cd15SJonathan T. Looney KASSERT(acked >= 0, ("%s: acked unexepectedly negative " 2771b8c2cd15SJonathan T. Looney "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__, 2772b8c2cd15SJonathan T. Looney tp->snd_una, th->th_ack, tp, m)); 27734b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvackpack, nsegs); 277478b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 2775df8bae1dSRodney W. Grimes 2776df8bae1dSRodney W. Grimes /* 27779b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 27789b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 27799b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 27809b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 27819b8b58e0SJonathan Lemon * we left off. 27829b8b58e0SJonathan Lemon */ 2783672dc4aeSJohn Baldwin if (tp->t_rxtshift == 1 && tp->t_flags & TF_PREVVALID && 2784672dc4aeSJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) 2785dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 27869b8b58e0SJonathan Lemon 27879b8b58e0SJonathan Lemon /* 2788df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2789df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2790df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2791df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2792df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2793df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2794df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2795fa55172bSMatthew Dillon * 2796fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2797fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2798fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2799fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2800df8bae1dSRodney W. Grimes */ 2801d8951c8aSBjoern A. Zeeb if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) { 28023ac12506SJonathan T. Looney uint32_t t; 2803d8951c8aSBjoern A. Zeeb 2804d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 2805d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 2806d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 2807d8951c8aSBjoern A. Zeeb tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1); 2808fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2809eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2810eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 28119b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2812fa55172bSMatthew Dillon } 2813df8bae1dSRodney W. Grimes 2814df8bae1dSRodney W. Grimes /* 2815df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2816df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2817df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2818df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2819df8bae1dSRodney W. Grimes */ 2820fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 2821b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2822df8bae1dSRodney W. Grimes needoutput = 1; 2823b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2824b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2825a0292f23SGarrett Wollman 2826a0292f23SGarrett Wollman /* 2827a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2828a0292f23SGarrett Wollman * skip rest of ACK processing. 2829a0292f23SGarrett Wollman */ 2830a0292f23SGarrett Wollman if (acked == 0) 2831a0292f23SGarrett Wollman goto step6; 2832a0292f23SGarrett Wollman 2833df8bae1dSRodney W. Grimes /* 2834dbc42409SLawrence Stewart * Let the congestion control algorithm update congestion 2835dbc42409SLawrence Stewart * control related information. This typically means increasing 2836dbc42409SLawrence Stewart * the congestion window. 2837df8bae1dSRodney W. Grimes */ 28384b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, CC_ACK); 2839dbc42409SLawrence Stewart 28405905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2841cfa6009eSGleb Smirnoff if (acked > sbavail(&so->so_snd)) { 2842b8c2cd15SJonathan T. Looney if (tp->snd_wnd >= sbavail(&so->so_snd)) 2843cfa6009eSGleb Smirnoff tp->snd_wnd -= sbavail(&so->so_snd); 2844b8c2cd15SJonathan T. Looney else 2845b8c2cd15SJonathan T. Looney tp->snd_wnd = 0; 2846c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, 2847cfa6009eSGleb Smirnoff (int)sbavail(&so->so_snd)); 2848df8bae1dSRodney W. Grimes ourfinisacked = 1; 2849df8bae1dSRodney W. Grimes } else { 2850c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, acked); 28513ac12506SJonathan T. Looney if (tp->snd_wnd >= (uint32_t) acked) 285260ee3bb2SAndre Oppermann tp->snd_wnd -= acked; 2853b8c2cd15SJonathan T. Looney else 2854b8c2cd15SJonathan T. Looney tp->snd_wnd = 0; 2855df8bae1dSRodney W. Grimes ourfinisacked = 0; 2856df8bae1dSRodney W. Grimes } 2857564aab1fSAndre Oppermann /* NB: sowwakeup_locked() does an implicit unlock. */ 28581e4d7da7SRobert Watson sowwakeup_locked(so); 2859c11a15bfSGleb Smirnoff m_freem(mfree); 2860e8949f74SAndre Oppermann /* Detect una wraparound. */ 2861dbc42409SLawrence Stewart if (!IN_RECOVERY(tp->t_flags) && 28629d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 28639d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 28649d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 2865dbc42409SLawrence Stewart /* XXXLAS: Can this be moved up into cc_post_recovery? */ 2866dbc42409SLawrence Stewart if (IN_RECOVERY(tp->t_flags) && 286724cb0f22SLawrence Stewart SEQ_GEQ(th->th_ack, tp->snd_recover)) { 2868dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 286924cb0f22SLawrence Stewart } 2870fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 28713529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 28726d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 28736d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 28746d90faf3SPaul Saab } 2875df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2876df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2877df8bae1dSRodney W. Grimes 2878df8bae1dSRodney W. Grimes switch (tp->t_state) { 2879df8bae1dSRodney W. Grimes 2880df8bae1dSRodney W. Grimes /* 2881df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2882df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2883df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2884df8bae1dSRodney W. Grimes */ 2885df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2886df8bae1dSRodney W. Grimes if (ourfinisacked) { 2887df8bae1dSRodney W. Grimes /* 2888df8bae1dSRodney W. Grimes * If we can't receive any more 2889df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2890df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2891df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2892df8bae1dSRodney W. Grimes * we'll hang forever. 2893e8949f74SAndre Oppermann * 2894e8949f74SAndre Oppermann * XXXjl: 2895340c35deSJonathan Lemon * we should release the tp also, and use a 2896340c35deSJonathan Lemon * compressed state. 2897340c35deSJonathan Lemon */ 2898c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 289903e49181SSeigo Tanimura soisdisconnected(so); 29009077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 29019077f387SGleb Smirnoff (tcp_fast_finwait2_recycle ? 29029077f387SGleb Smirnoff tcp_finwait2_timeout : 29039077f387SGleb Smirnoff TP_MAXIDLE(tp))); 29044cc20ab1SSeigo Tanimura } 290557f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_2); 2906df8bae1dSRodney W. Grimes } 2907df8bae1dSRodney W. Grimes break; 2908df8bae1dSRodney W. Grimes 2909df8bae1dSRodney W. Grimes /* 2910df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2911df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2912df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2913df8bae1dSRodney W. Grimes * the segment. 2914df8bae1dSRodney W. Grimes */ 2915df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2916df8bae1dSRodney W. Grimes if (ourfinisacked) { 2917ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 291811a20fb8SJeffrey Hsu tcp_twstart(tp); 2919ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 2920340c35deSJonathan Lemon m_freem(m); 2921679d9708SAndre Oppermann return; 2922df8bae1dSRodney W. Grimes } 2923df8bae1dSRodney W. Grimes break; 2924df8bae1dSRodney W. Grimes 2925df8bae1dSRodney W. Grimes /* 2926df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2927df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2928df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2929df8bae1dSRodney W. Grimes * enter the closed state and return. 2930df8bae1dSRodney W. Grimes */ 2931df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2932df8bae1dSRodney W. Grimes if (ourfinisacked) { 2933ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2934df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2935df8bae1dSRodney W. Grimes goto drop; 2936df8bae1dSRodney W. Grimes } 2937df8bae1dSRodney W. Grimes break; 2938df8bae1dSRodney W. Grimes } 2939df8bae1dSRodney W. Grimes } 2940df8bae1dSRodney W. Grimes 2941df8bae1dSRodney W. Grimes step6: 29428501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 29437cfc6904SRobert Watson 2944df8bae1dSRodney W. Grimes /* 294560ee3bb2SAndre Oppermann * Update window information. 294660ee3bb2SAndre Oppermann * Don't look at window if no ACK: TAC's send garbage on first SYN. 2947df8bae1dSRodney W. Grimes */ 294860ee3bb2SAndre Oppermann if ((thflags & TH_ACK) && 294960ee3bb2SAndre Oppermann (SEQ_LT(tp->snd_wl1, th->th_seq) || 295060ee3bb2SAndre Oppermann (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 295160ee3bb2SAndre Oppermann (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 295260ee3bb2SAndre Oppermann /* keep track of pure window updates */ 295360ee3bb2SAndre Oppermann if (tlen == 0 && 295460ee3bb2SAndre Oppermann tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 295578b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinupd); 2956df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 295760ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 295860ee3bb2SAndre Oppermann tp->snd_wl2 = th->th_ack; 2959df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2960df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 296160ee3bb2SAndre Oppermann needoutput = 1; 2962df8bae1dSRodney W. Grimes } 2963df8bae1dSRodney W. Grimes 2964df8bae1dSRodney W. Grimes /* 2965df8bae1dSRodney W. Grimes * Process segments with URG. 2966df8bae1dSRodney W. Grimes */ 2967fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2968df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2969df8bae1dSRodney W. Grimes /* 2970df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2971df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2972df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2973df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2974df8bae1dSRodney W. Grimes */ 297518ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2976cfa6009eSGleb Smirnoff if (th->th_urp + sbavail(&so->so_rcv) > sb_max) { 2977fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2978fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 297918ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2980df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2981df8bae1dSRodney W. Grimes } 2982df8bae1dSRodney W. Grimes /* 2983df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2984df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2985df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2986df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2987df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2988df8bae1dSRodney W. Grimes * 2989df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2990df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2991df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2992df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2993df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2994df8bae1dSRodney W. Grimes * spec states (in one of two places). 2995df8bae1dSRodney W. Grimes */ 2996fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2997fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2998cfa6009eSGleb Smirnoff so->so_oobmark = sbavail(&so->so_rcv) + 2999df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 3000927c5ceaSRobert Watson if (so->so_oobmark == 0) 3001c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 3002df8bae1dSRodney W. Grimes sohasoutofband(so); 3003df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 3004df8bae1dSRodney W. Grimes } 300518ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 3006df8bae1dSRodney W. Grimes /* 3007df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 3008df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 3009df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 3010df8bae1dSRodney W. Grimes * data may creep in... ick. 3011df8bae1dSRodney W. Grimes */ 30123ac12506SJonathan T. Looney if (th->th_urp <= (uint32_t)tlen && 301330613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 301430613f56SJeffrey Hsu /* hdr drop is delayed */ 301530613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 301630613f56SJeffrey Hsu } 3017c068736aSJeffrey Hsu } else { 3018df8bae1dSRodney W. Grimes /* 3019df8bae1dSRodney W. Grimes * If no out of band data is expected, 3020df8bae1dSRodney W. Grimes * pull receive urgent pointer along 3021df8bae1dSRodney W. Grimes * with the receive window. 3022df8bae1dSRodney W. Grimes */ 3023df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 3024df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 3025c068736aSJeffrey Hsu } 3026df8bae1dSRodney W. Grimes dodata: /* XXX */ 30278501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 30287cfc6904SRobert Watson 3029df8bae1dSRodney W. Grimes /* 3030df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 3031df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 3032df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 3033df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 3034df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 3035df8bae1dSRodney W. Grimes * connection then we just ignore the text. 3036df8bae1dSRodney W. Grimes */ 303768bd7ed1SJonathan T. Looney #ifdef TCP_RFC7413 3038281a0fd4SPatrick Kelsey tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) && 303968bd7ed1SJonathan T. Looney IS_FASTOPEN(tp->t_flags)); 304068bd7ed1SJonathan T. Looney #else 304168bd7ed1SJonathan T. Looney #define tfo_syn (false) 304268bd7ed1SJonathan T. Looney #endif 3043281a0fd4SPatrick Kelsey if ((tlen || (thflags & TH_FIN) || tfo_syn) && 3044df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 304569e03620SPaul Saab tcp_seq save_start = th->th_seq; 3046fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 3047e4b64281SJesper Skriver /* 3048c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 3049c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 3050c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 3051c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 3052c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 3053c068736aSJeffrey Hsu * and removal from the queue and repetition of various 3054c068736aSJeffrey Hsu * conversions. 3055c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 3056c068736aSJeffrey Hsu * immediately when segments are out of order (so 3057c068736aSJeffrey Hsu * fast retransmit can work). 3058e4b64281SJesper Skriver */ 30594741bfcbSPatrick Kelsey if (th->th_seq == tp->rcv_nxt && 30604741bfcbSPatrick Kelsey LIST_EMPTY(&tp->t_segq) && 3061281a0fd4SPatrick Kelsey (TCPS_HAVEESTABLISHED(tp->t_state) || 3062281a0fd4SPatrick Kelsey tfo_syn)) { 3063281a0fd4SPatrick Kelsey if (DELAY_ACK(tp, tlen) || tfo_syn) 30643bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 3065e4b64281SJesper Skriver else 3066e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 3067e4b64281SJesper Skriver tp->rcv_nxt += tlen; 3068e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 306978b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 307078b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 30711e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3072c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 3073c1c36a2cSMike Silbersack m_freem(m); 3074c1c36a2cSMike Silbersack else 3075651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 3076e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 30771e4d7da7SRobert Watson sorwakeup_locked(so); 3078e4b64281SJesper Skriver } else { 3079e8949f74SAndre Oppermann /* 3080e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 3081e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 3082e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 3083e8949f74SAndre Oppermann * when trimming from the head. 3084e8949f74SAndre Oppermann */ 3085e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 3086e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 3087e4b64281SJesper Skriver } 30883529149eSAndre Oppermann if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 3089f194524fSAndre Oppermann tcp_update_sack_list(tp, save_start, save_start + tlen); 3090302ce8d6SAndre Oppermann #if 0 3091df8bae1dSRodney W. Grimes /* 3092df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 3093df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 3094df8bae1dSRodney W. Grimes * buffer size. 3095302ce8d6SAndre Oppermann * XXX: Unused. 3096df8bae1dSRodney W. Grimes */ 3097f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) 3098df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 3099f701e30dSJohn Baldwin else 3100f701e30dSJohn Baldwin len = so->so_rcv.sb_hiwat; 3101302ce8d6SAndre Oppermann #endif 3102df8bae1dSRodney W. Grimes } else { 3103df8bae1dSRodney W. Grimes m_freem(m); 3104fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 3105df8bae1dSRodney W. Grimes } 3106df8bae1dSRodney W. Grimes 3107df8bae1dSRodney W. Grimes /* 3108df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 3109df8bae1dSRodney W. Grimes * that the connection is closing. 3110df8bae1dSRodney W. Grimes */ 3111fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 3112df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 3113df8bae1dSRodney W. Grimes socantrcvmore(so); 3114a0292f23SGarrett Wollman /* 3115a0292f23SGarrett Wollman * If connection is half-synchronized 3116d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 3117a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 3118a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 3119a0292f23SGarrett Wollman * more input can be expected, send ACK now. 3120a0292f23SGarrett Wollman */ 31213bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 31223bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 3123a0292f23SGarrett Wollman else 3124df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 3125df8bae1dSRodney W. Grimes tp->rcv_nxt++; 3126df8bae1dSRodney W. Grimes } 3127df8bae1dSRodney W. Grimes switch (tp->t_state) { 3128df8bae1dSRodney W. Grimes 3129df8bae1dSRodney W. Grimes /* 3130df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 3131df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 3132df8bae1dSRodney W. Grimes */ 3133df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 31349b8b58e0SJonathan Lemon tp->t_starttime = ticks; 31359b8b58e0SJonathan Lemon /* FALLTHROUGH */ 3136df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 313757f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSE_WAIT); 3138df8bae1dSRodney W. Grimes break; 3139df8bae1dSRodney W. Grimes 3140df8bae1dSRodney W. Grimes /* 3141df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 3142df8bae1dSRodney W. Grimes * enter the CLOSING state. 3143df8bae1dSRodney W. Grimes */ 3144df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 314557f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSING); 3146df8bae1dSRodney W. Grimes break; 3147df8bae1dSRodney W. Grimes 3148df8bae1dSRodney W. Grimes /* 3149df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 3150df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 3151df8bae1dSRodney W. Grimes * standard timers. 3152df8bae1dSRodney W. Grimes */ 3153df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 3154ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 3155ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, ("%s: dodata " 3156252ca428SRobert Watson "TCP_FIN_WAIT_2 ti_locked: %d", __func__, 3157252ca428SRobert Watson ti_locked)); 3158252ca428SRobert Watson 3159340c35deSJonathan Lemon tcp_twstart(tp); 3160ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 3161679d9708SAndre Oppermann return; 3162df8bae1dSRodney W. Grimes } 3163df8bae1dSRodney W. Grimes } 3164ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 3165ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 3166252ca428SRobert Watson ti_locked = TI_UNLOCKED; 3167252ca428SRobert Watson 3168610ee2f9SDavid Greenman #ifdef TCPDEBUG 31694cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3170fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 3171fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3172610ee2f9SDavid Greenman #endif 31732b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 3174df8bae1dSRodney W. Grimes 3175df8bae1dSRodney W. Grimes /* 3176df8bae1dSRodney W. Grimes * Return any desired output. 3177df8bae1dSRodney W. Grimes */ 3178df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 317955bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 31807792ea27SJeffrey Hsu 3181a14c749fSJonathan Lemon check_delack: 3182252ca428SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 3183252ca428SRobert Watson __func__, ti_locked)); 3184603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 31858501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 3186252ca428SRobert Watson 3187a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 31883bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 3189b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 31903bfd6421SJonathan Lemon } 31918501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3192679d9708SAndre Oppermann return; 3193df8bae1dSRodney W. Grimes 3194df8bae1dSRodney W. Grimes dropafterack: 3195df8bae1dSRodney W. Grimes /* 3196df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 3197df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 319880ab7c0eSGarrett Wollman * 319980ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 320080ab7c0eSGarrett Wollman * paths to this code happen after packets containing 320180ab7c0eSGarrett Wollman * RST have been dropped. 320280ab7c0eSGarrett Wollman * 320380ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 320480ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 320580ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 320680ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 320780ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 320880ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 3209df8bae1dSRodney W. Grimes */ 3210fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 3211fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 3212a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 3213a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 3214a57815efSBosko Milekic goto dropwithreset; 3215a57815efSBosko Milekic } 3216a0292f23SGarrett Wollman #ifdef TCPDEBUG 32174cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3218fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3219fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3220a0292f23SGarrett Wollman #endif 32212b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 3222ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 3223ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 3224252ca428SRobert Watson ti_locked = TI_UNLOCKED; 3225252ca428SRobert Watson 3226df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 322755bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 32288501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3229d6915262SRobert Watson m_freem(m); 3230679d9708SAndre Oppermann return; 3231df8bae1dSRodney W. Grimes 3232df8bae1dSRodney W. Grimes dropwithreset: 3233ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 3234ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 3235252ca428SRobert Watson ti_locked = TI_UNLOCKED; 3236a57815efSBosko Milekic 3237a0ca0871SRobert Watson if (tp != NULL) { 3238302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 32398501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3240dd8ac7f9SRobert Watson } else 3241a0ca0871SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 3242679d9708SAndre Oppermann return; 3243df8bae1dSRodney W. Grimes 3244df8bae1dSRodney W. Grimes drop: 3245ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 3246ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 3247fa046d87SRobert Watson ti_locked = TI_UNLOCKED; 3248fa046d87SRobert Watson } 3249252ca428SRobert Watson #ifdef INVARIANTS 3250252ca428SRobert Watson else 3251252ca428SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 3252252ca428SRobert Watson #endif 3253252ca428SRobert Watson 3254df8bae1dSRodney W. Grimes /* 3255df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 3256df8bae1dSRodney W. Grimes */ 3257610ee2f9SDavid Greenman #ifdef TCPDEBUG 32581c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 3259fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3260fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3261a0292f23SGarrett Wollman #endif 32622b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 32631c53f806SRobert Watson if (tp != NULL) 32648501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3265d6915262SRobert Watson m_freem(m); 326668bd7ed1SJonathan T. Looney #ifndef TCP_RFC7413 326768bd7ed1SJonathan T. Looney #undef tfo_syn 326868bd7ed1SJonathan T. Looney #endif 3269302ce8d6SAndre Oppermann } 3270302ce8d6SAndre Oppermann 3271302ce8d6SAndre Oppermann /* 32720ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 32730ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 32740ca3f933SAndre Oppermann * tp may be NULL. 3275302ce8d6SAndre Oppermann */ 327655bceb1eSRandall Stewart void 3277302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 3278302ce8d6SAndre Oppermann int tlen, int rstreason) 3279302ce8d6SAndre Oppermann { 3280b287c6c7SBjoern A. Zeeb #ifdef INET 3281302ce8d6SAndre Oppermann struct ip *ip; 3282b287c6c7SBjoern A. Zeeb #endif 3283302ce8d6SAndre Oppermann #ifdef INET6 3284302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 3285302ce8d6SAndre Oppermann #endif 32867a3244ccSRobert Watson 32877a3244ccSRobert Watson if (tp != NULL) { 32888501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 32897a3244ccSRobert Watson } 32907a3244ccSRobert Watson 32910ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 3292302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 3293302ce8d6SAndre Oppermann goto drop; 3294302ce8d6SAndre Oppermann #ifdef INET6 3295302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 3296302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 3297302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3298302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3299302ce8d6SAndre Oppermann goto drop; 3300302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 3301b287c6c7SBjoern A. Zeeb } 3302302ce8d6SAndre Oppermann #endif 3303b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3304b287c6c7SBjoern A. Zeeb else 3305b287c6c7SBjoern A. Zeeb #endif 3306b287c6c7SBjoern A. Zeeb #ifdef INET 3307302ce8d6SAndre Oppermann { 3308302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 3309302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3310302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3311302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3312302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3313302ce8d6SAndre Oppermann goto drop; 3314302ce8d6SAndre Oppermann } 3315b287c6c7SBjoern A. Zeeb #endif 3316302ce8d6SAndre Oppermann 3317302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 3318302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 3319302ce8d6SAndre Oppermann goto drop; 3320302ce8d6SAndre Oppermann 3321302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 3322302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 3323302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 3324302ce8d6SAndre Oppermann th->th_ack, TH_RST); 3325302ce8d6SAndre Oppermann } else { 3326302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 3327302ce8d6SAndre Oppermann tlen++; 33284ddd5aadSMichael Tuexen if (th->th_flags & TH_FIN) 33294ddd5aadSMichael Tuexen tlen++; 3330302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 3331302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 3332302ce8d6SAndre Oppermann } 3333302ce8d6SAndre Oppermann return; 3334302ce8d6SAndre Oppermann drop: 3335302ce8d6SAndre Oppermann m_freem(m); 3336df8bae1dSRodney W. Grimes } 3337df8bae1dSRodney W. Grimes 3338be2ac88cSJonathan Lemon /* 3339be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 3340be2ac88cSJonathan Lemon */ 334155bceb1eSRandall Stewart void 3342ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 3343df8bae1dSRodney W. Grimes { 3344df8bae1dSRodney W. Grimes int opt, optlen; 3345df8bae1dSRodney W. Grimes 3346be2ac88cSJonathan Lemon to->to_flags = 0; 3347df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 3348df8bae1dSRodney W. Grimes opt = cp[0]; 3349df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 3350df8bae1dSRodney W. Grimes break; 3351df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 3352df8bae1dSRodney W. Grimes optlen = 1; 3353df8bae1dSRodney W. Grimes else { 3354b474779fSJun-ichiro itojun Hagino if (cnt < 2) 3355b474779fSJun-ichiro itojun Hagino break; 3356df8bae1dSRodney W. Grimes optlen = cp[1]; 3357b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 3358df8bae1dSRodney W. Grimes break; 3359df8bae1dSRodney W. Grimes } 3360df8bae1dSRodney W. Grimes switch (opt) { 3361df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 3362df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 3363df8bae1dSRodney W. Grimes continue; 3364f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3365df8bae1dSRodney W. Grimes continue; 3366be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 3367be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 3368be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 3369fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 3370df8bae1dSRodney W. Grimes break; 3371df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 3372df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 3373df8bae1dSRodney W. Grimes continue; 3374f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3375df8bae1dSRodney W. Grimes continue; 3376be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 337702a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 3378df8bae1dSRodney W. Grimes break; 3379df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 3380df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 3381df8bae1dSRodney W. Grimes continue; 3382be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 3383a0292f23SGarrett Wollman bcopy((char *)cp + 2, 3384a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 3385fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 3386a0292f23SGarrett Wollman bcopy((char *)cp + 6, 3387a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 3388fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 3389df8bae1dSRodney W. Grimes break; 33901cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 3391fcf59617SAndrey V. Elsukov /* 3392fcf59617SAndrey V. Elsukov * In order to reply to a host which has set the 3393fcf59617SAndrey V. Elsukov * TCP_SIGNATURE option in its initial SYN, we have 3394fcf59617SAndrey V. Elsukov * to record the fact that the option was observed 3395fcf59617SAndrey V. Elsukov * here for the syncache code to perform the correct 3396fcf59617SAndrey V. Elsukov * response. 3397fcf59617SAndrey V. Elsukov */ 33981cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 33991cfd4b53SBruce M Simpson continue; 3400df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 3401df47e437SAndre Oppermann to->to_signature = cp + 2; 34021cfd4b53SBruce M Simpson break; 34036d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 3404f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 34056d90faf3SPaul Saab continue; 3406f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3407f72167f4SAndre Oppermann continue; 3408603724d3SBjoern A. Zeeb if (!V_tcp_do_sack) 3409f72167f4SAndre Oppermann continue; 3410fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 34116d90faf3SPaul Saab break; 34126d90faf3SPaul Saab case TCPOPT_SACK: 34135a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 34146d90faf3SPaul Saab continue; 3415b728e902SAndre Oppermann if (flags & TO_SYN) 3416b728e902SAndre Oppermann continue; 3417fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 34185a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 34195a53ca16SPaul Saab to->to_sacks = cp + 2; 342078b50714SRobert Watson TCPSTAT_INC(tcps_sack_rcv_blocks); 34216d90faf3SPaul Saab break; 3422281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 3423281a0fd4SPatrick Kelsey case TCPOPT_FAST_OPEN: 3424281a0fd4SPatrick Kelsey if ((optlen != TCPOLEN_FAST_OPEN_EMPTY) && 3425281a0fd4SPatrick Kelsey (optlen < TCPOLEN_FAST_OPEN_MIN) && 3426281a0fd4SPatrick Kelsey (optlen > TCPOLEN_FAST_OPEN_MAX)) 3427281a0fd4SPatrick Kelsey continue; 3428281a0fd4SPatrick Kelsey if (!(flags & TO_SYN)) 3429281a0fd4SPatrick Kelsey continue; 3430281a0fd4SPatrick Kelsey if (!V_tcp_fastopen_enabled) 3431281a0fd4SPatrick Kelsey continue; 3432281a0fd4SPatrick Kelsey to->to_flags |= TOF_FASTOPEN; 3433281a0fd4SPatrick Kelsey to->to_tfo_len = optlen - 2; 3434281a0fd4SPatrick Kelsey to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL; 3435281a0fd4SPatrick Kelsey break; 3436281a0fd4SPatrick Kelsey #endif 3437be2ac88cSJonathan Lemon default: 3438be2ac88cSJonathan Lemon continue; 3439df8bae1dSRodney W. Grimes } 3440df8bae1dSRodney W. Grimes } 3441df8bae1dSRodney W. Grimes } 3442df8bae1dSRodney W. Grimes 3443df8bae1dSRodney W. Grimes /* 3444df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 3445df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 3446df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 3447df8bae1dSRodney W. Grimes * sequencing purposes. 3448df8bae1dSRodney W. Grimes */ 344955bceb1eSRandall Stewart void 3450ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 3451ad3f9ab3SAndre Oppermann int off) 3452df8bae1dSRodney W. Grimes { 3453fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 3454df8bae1dSRodney W. Grimes 3455df8bae1dSRodney W. Grimes while (cnt >= 0) { 3456df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 3457df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 3458df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 3459df8bae1dSRodney W. Grimes 34608501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 34617a3244ccSRobert Watson 3462df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 3463df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 3464df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 3465df8bae1dSRodney W. Grimes m->m_len--; 346669a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 346769a34685SYoshinobu Inoue m->m_pkthdr.len--; 3468df8bae1dSRodney W. Grimes return; 3469df8bae1dSRodney W. Grimes } 3470df8bae1dSRodney W. Grimes cnt -= m->m_len; 3471df8bae1dSRodney W. Grimes m = m->m_next; 34724dfdffe9SAndre Oppermann if (m == NULL) 3473df8bae1dSRodney W. Grimes break; 3474df8bae1dSRodney W. Grimes } 3475df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 3476df8bae1dSRodney W. Grimes } 3477df8bae1dSRodney W. Grimes 3478df8bae1dSRodney W. Grimes /* 3479df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 3480df8bae1dSRodney W. Grimes * and update averages and current timeout. 3481df8bae1dSRodney W. Grimes */ 348255bceb1eSRandall Stewart void 3483ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 3484df8bae1dSRodney W. Grimes { 3485ad3f9ab3SAndre Oppermann int delta; 3486233e8c18SGarrett Wollman 34878501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 34882be3bf22SRobert Watson 348978b50714SRobert Watson TCPSTAT_INC(tcps_rttupdated); 3490233e8c18SGarrett Wollman tp->t_rttupdated++; 34915ede40dcSRyan Stone if ((tp->t_srtt != 0) && (tp->t_rxtshift <= TCP_RTT_INVALIDATE)) { 3492233e8c18SGarrett Wollman /* 3493233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 3494233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 3495233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 3496233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3497233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 3498233e8c18SGarrett Wollman */ 3499233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3500233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3501233e8c18SGarrett Wollman 3502233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 3503233e8c18SGarrett Wollman tp->t_srtt = 1; 3504233e8c18SGarrett Wollman 3505233e8c18SGarrett Wollman /* 3506233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 3507233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 3508233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 3509233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 3510233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 3511233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 3512233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3513233e8c18SGarrett Wollman * rfc793's wired-in beta. 3514233e8c18SGarrett Wollman */ 3515233e8c18SGarrett Wollman if (delta < 0) 3516233e8c18SGarrett Wollman delta = -delta; 3517233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3518233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 3519233e8c18SGarrett Wollman tp->t_rttvar = 1; 35201fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 35211fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3522233e8c18SGarrett Wollman } else { 3523233e8c18SGarrett Wollman /* 3524233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 3525233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 3526233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 3527233e8c18SGarrett Wollman */ 3528233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 3529233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 35301fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3531233e8c18SGarrett Wollman } 35329b8b58e0SJonathan Lemon tp->t_rtttime = 0; 3533df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 3534df8bae1dSRodney W. Grimes 3535df8bae1dSRodney W. Grimes /* 3536df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 3537df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 3538df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 3539df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 3540df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 3541df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 3542df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 3543df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 3544df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 3545df8bae1dSRodney W. Grimes */ 3546233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 35479e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3548df8bae1dSRodney W. Grimes 3549df8bae1dSRodney W. Grimes /* 3550df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 3551df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 3552df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 3553df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 3554df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 3555df8bae1dSRodney W. Grimes */ 3556df8bae1dSRodney W. Grimes tp->t_softerror = 0; 3557df8bae1dSRodney W. Grimes } 3558df8bae1dSRodney W. Grimes 3559df8bae1dSRodney W. Grimes /* 3560df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 3561df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 35624faaea55SAndre Oppermann * If none, use an mss that can be handled on the outgoing interface 35634faaea55SAndre Oppermann * without forcing IP to fragment. If no route is found, route has no mtu, 3564df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 3565df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 3566df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 3567df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 3568df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 3569df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 3570df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 3571a0292f23SGarrett Wollman * 35720c39d38dSGleb Smirnoff * NOTE that resulting t_maxseg doesn't include space for TCP options or 35730c39d38dSGleb Smirnoff * IP options, e.g. IPSEC data, since length of this data may vary, and 35740c39d38dSGleb Smirnoff * thus it is calculated for every segment separately in tcp_output(). 3575a0292f23SGarrett Wollman * 357697d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 3577ef341ee1SGleb Smirnoff * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS 3578ef341ee1SGleb Smirnoff * settings are handled in tcp_mssopt(). 3579df8bae1dSRodney W. Grimes */ 3580a0292f23SGarrett Wollman void 3581ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer, 35823c914c54SAndre Oppermann struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap) 3583df8bae1dSRodney W. Grimes { 3584b287c6c7SBjoern A. Zeeb int mss = 0; 35853ac12506SJonathan T. Looney uint32_t maxmtu = 0; 3586c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 358797d8d152SAndre Oppermann struct hc_metrics_lite metrics; 3588fb59c426SYoshinobu Inoue #ifdef INET6 3589c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3590c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 3591c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3592c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 3593c068736aSJeffrey Hsu #else 3594c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 3595fb59c426SYoshinobu Inoue #endif 3596df8bae1dSRodney W. Grimes 35978501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 35987a3244ccSRobert Watson 3599ef341ee1SGleb Smirnoff if (mtuoffer != -1) { 3600ef341ee1SGleb Smirnoff KASSERT(offer == -1, ("%s: conflict", __func__)); 3601ef341ee1SGleb Smirnoff offer = mtuoffer - min_protoh; 3602ef341ee1SGleb Smirnoff } 3603ef341ee1SGleb Smirnoff 3604e8949f74SAndre Oppermann /* Initialize. */ 360597d8d152SAndre Oppermann #ifdef INET6 360697d8d152SAndre Oppermann if (isipv6) { 36073c914c54SAndre Oppermann maxmtu = tcp_maxmtu6(&inp->inp_inc, cap); 36080c39d38dSGleb Smirnoff tp->t_maxseg = V_tcp_v6mssdflt; 3609b287c6c7SBjoern A. Zeeb } 361097d8d152SAndre Oppermann #endif 3611b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3612b287c6c7SBjoern A. Zeeb else 3613b287c6c7SBjoern A. Zeeb #endif 3614b287c6c7SBjoern A. Zeeb #ifdef INET 361597d8d152SAndre Oppermann { 36163c914c54SAndre Oppermann maxmtu = tcp_maxmtu(&inp->inp_inc, cap); 36170c39d38dSGleb Smirnoff tp->t_maxseg = V_tcp_mssdflt; 3618df8bae1dSRodney W. Grimes } 3619b287c6c7SBjoern A. Zeeb #endif 3620df8bae1dSRodney W. Grimes 362197d8d152SAndre Oppermann /* 3622e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 362397d8d152SAndre Oppermann */ 36246f01cac6SBjoern A. Zeeb if (maxmtu == 0) { 36256f01cac6SBjoern A. Zeeb /* 36268e5c87f4SBjoern A. Zeeb * In case we return early we need to initialize metrics 36276f01cac6SBjoern A. Zeeb * to a defined state as tcp_hc_get() would do for us 36286f01cac6SBjoern A. Zeeb * if there was no cache hit. 36296f01cac6SBjoern A. Zeeb */ 36306f01cac6SBjoern A. Zeeb if (metricptr != NULL) 36316f01cac6SBjoern A. Zeeb bzero(metricptr, sizeof(struct hc_metrics_lite)); 363297d8d152SAndre Oppermann return; 36336f01cac6SBjoern A. Zeeb } 363497d8d152SAndre Oppermann 3635e8949f74SAndre Oppermann /* What have we got? */ 363697d8d152SAndre Oppermann switch (offer) { 363797d8d152SAndre Oppermann case 0: 363897d8d152SAndre Oppermann /* 363997d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 3640c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 36410c39d38dSGleb Smirnoff * already assigned to t_maxseg above. 364297d8d152SAndre Oppermann */ 36430c39d38dSGleb Smirnoff offer = tp->t_maxseg; 364497d8d152SAndre Oppermann break; 364597d8d152SAndre Oppermann 364697d8d152SAndre Oppermann case -1: 3647a0292f23SGarrett Wollman /* 3648c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 3649a0292f23SGarrett Wollman */ 365097d8d152SAndre Oppermann /* FALLTHROUGH */ 365197d8d152SAndre Oppermann 365297d8d152SAndre Oppermann default: 3653a0292f23SGarrett Wollman /* 365453369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 365553369ac9SAndre Oppermann * to at least minmss. 365653369ac9SAndre Oppermann */ 3657603724d3SBjoern A. Zeeb offer = max(offer, V_tcp_minmss); 365897d8d152SAndre Oppermann } 3659a0292f23SGarrett Wollman 3660df8bae1dSRodney W. Grimes /* 3661e8949f74SAndre Oppermann * rmx information is now retrieved from tcp_hostcache. 3662df8bae1dSRodney W. Grimes */ 366397d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 36643cee92e0SBjoern A. Zeeb if (metricptr != NULL) 36653cee92e0SBjoern A. Zeeb bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 366697d8d152SAndre Oppermann 3667df8bae1dSRodney W. Grimes /* 3668cc412412SHiren Panchasara * If there's a discovered mtu in tcp hostcache, use it. 3669cc412412SHiren Panchasara * Else, use the link mtu. 3670df8bae1dSRodney W. Grimes */ 367197d8d152SAndre Oppermann if (metrics.rmx_mtu) 36722d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3673c068736aSJeffrey Hsu else { 367431b3783cSHajimu UMEMOTO #ifdef INET6 3675fb59c426SYoshinobu Inoue if (isipv6) { 367697d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3677603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 367897d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 3679603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_v6mssdflt); 3680b287c6c7SBjoern A. Zeeb } 3681b3399803SHajimu UMEMOTO #endif 3682b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3683b287c6c7SBjoern A. Zeeb else 3684b287c6c7SBjoern A. Zeeb #endif 3685b287c6c7SBjoern A. Zeeb #ifdef INET 368697d8d152SAndre Oppermann { 368797d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3688603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 368997d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 3690603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_mssdflt); 3691a0292f23SGarrett Wollman } 3692b287c6c7SBjoern A. Zeeb #endif 36933cee92e0SBjoern A. Zeeb /* 36943cee92e0SBjoern A. Zeeb * XXX - The above conditional (mss = maxmtu - min_protoh) 36953cee92e0SBjoern A. Zeeb * probably violates the TCP spec. 36963cee92e0SBjoern A. Zeeb * The problem is that, since we don't know the 36973cee92e0SBjoern A. Zeeb * other end's MSS, we are supposed to use a conservative 36983cee92e0SBjoern A. Zeeb * default. But, if we do that, then MTU discovery will 36993cee92e0SBjoern A. Zeeb * never actually take place, because the conservative 37003cee92e0SBjoern A. Zeeb * default is much less than the MTUs typically seen 37013cee92e0SBjoern A. Zeeb * on the Internet today. For the moment, we'll sweep 37023cee92e0SBjoern A. Zeeb * this under the carpet. 37033cee92e0SBjoern A. Zeeb * 37043cee92e0SBjoern A. Zeeb * The conservative default might not actually be a problem 37053cee92e0SBjoern A. Zeeb * if the only case this occurs is when sending an initial 37063cee92e0SBjoern A. Zeeb * SYN with options and data to a host we've never talked 37073cee92e0SBjoern A. Zeeb * to before. Then, they will reply with an MSS value which 37083cee92e0SBjoern A. Zeeb * will get recorded and the new parameters should get 37093cee92e0SBjoern A. Zeeb * recomputed. For Further Study. 37103cee92e0SBjoern A. Zeeb */ 371197d8d152SAndre Oppermann } 3712a0292f23SGarrett Wollman mss = min(mss, offer); 371397d8d152SAndre Oppermann 3714a0292f23SGarrett Wollman /* 37150c39d38dSGleb Smirnoff * Sanity check: make sure that maxseg will be large 37163cee92e0SBjoern A. Zeeb * enough to allow some data on segments even if the 37173cee92e0SBjoern A. Zeeb * all the option space is used (40bytes). Otherwise 37183cee92e0SBjoern A. Zeeb * funny things may happen in tcp_output. 37190c39d38dSGleb Smirnoff * 37200c39d38dSGleb Smirnoff * XXXGL: shouldn't we reserve space for IP/IPv6 options? 37213cee92e0SBjoern A. Zeeb */ 37223cee92e0SBjoern A. Zeeb mss = max(mss, 64); 37233cee92e0SBjoern A. Zeeb 372497d8d152SAndre Oppermann tp->t_maxseg = mss; 37253cee92e0SBjoern A. Zeeb } 37263cee92e0SBjoern A. Zeeb 37273cee92e0SBjoern A. Zeeb void 37283cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer) 37293cee92e0SBjoern A. Zeeb { 3730dbc42409SLawrence Stewart int mss; 37313ac12506SJonathan T. Looney uint32_t bufsize; 37323cee92e0SBjoern A. Zeeb struct inpcb *inp; 37333cee92e0SBjoern A. Zeeb struct socket *so; 37343cee92e0SBjoern A. Zeeb struct hc_metrics_lite metrics; 37353c914c54SAndre Oppermann struct tcp_ifcap cap; 3736dbc42409SLawrence Stewart 37373cee92e0SBjoern A. Zeeb KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 37383cee92e0SBjoern A. Zeeb 37393c914c54SAndre Oppermann bzero(&cap, sizeof(cap)); 37403c914c54SAndre Oppermann tcp_mss_update(tp, offer, -1, &metrics, &cap); 37413cee92e0SBjoern A. Zeeb 37423cee92e0SBjoern A. Zeeb mss = tp->t_maxseg; 37433cee92e0SBjoern A. Zeeb inp = tp->t_inpcb; 374497d8d152SAndre Oppermann 3745df8bae1dSRodney W. Grimes /* 374697d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 374797d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 374897d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 374997d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 375097d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3751df8bae1dSRodney W. Grimes */ 3752c3b02504SBjoern A. Zeeb so = inp->inp_socket; 37533f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 3754e233e2acSAndre Oppermann if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe) 375597d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 375697d8d152SAndre Oppermann else 3757df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3758df8bae1dSRodney W. Grimes if (bufsize < mss) 3759df8bae1dSRodney W. Grimes mss = bufsize; 3760df8bae1dSRodney W. Grimes else { 3761df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3762df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3763df8bae1dSRodney W. Grimes bufsize = sb_max; 376488c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 37653f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3766df8bae1dSRodney W. Grimes } 37673f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 3768784ce8faSHiren Panchasara /* 3769784ce8faSHiren Panchasara * Sanity check: make sure that maxseg will be large 3770784ce8faSHiren Panchasara * enough to allow some data on segments even if the 3771784ce8faSHiren Panchasara * all the option space is used (40bytes). Otherwise 3772784ce8faSHiren Panchasara * funny things may happen in tcp_output. 3773784ce8faSHiren Panchasara * 3774784ce8faSHiren Panchasara * XXXGL: shouldn't we reserve space for IP/IPv6 options? 3775784ce8faSHiren Panchasara */ 3776784ce8faSHiren Panchasara tp->t_maxseg = max(mss, 64); 3777df8bae1dSRodney W. Grimes 37783f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3779e233e2acSAndre Oppermann if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe) 378097d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 378197d8d152SAndre Oppermann else 3782df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3783df8bae1dSRodney W. Grimes if (bufsize > mss) { 3784df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3785df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3786df8bae1dSRodney W. Grimes bufsize = sb_max; 378788c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 37883f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3789df8bae1dSRodney W. Grimes } 37903f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 379191d6cfa6SBjoern A. Zeeb 379291d6cfa6SBjoern A. Zeeb /* Check the interface for TSO capabilities. */ 37933c914c54SAndre Oppermann if (cap.ifcap & CSUM_TSO) { 379491d6cfa6SBjoern A. Zeeb tp->t_flags |= TF_TSO; 37953c914c54SAndre Oppermann tp->t_tsomax = cap.tsomax; 37969fd573c3SHans Petter Selasky tp->t_tsomaxsegcount = cap.tsomaxsegcount; 37979fd573c3SHans Petter Selasky tp->t_tsomaxsegsize = cap.tsomaxsegsize; 37983c914c54SAndre Oppermann } 3799a0292f23SGarrett Wollman } 3800a0292f23SGarrett Wollman 3801a0292f23SGarrett Wollman /* 3802a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3803a0292f23SGarrett Wollman */ 3804a0292f23SGarrett Wollman int 3805ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 3806a0292f23SGarrett Wollman { 380797d8d152SAndre Oppermann int mss = 0; 38083ac12506SJonathan T. Looney uint32_t thcmtu = 0; 38093ac12506SJonathan T. Looney uint32_t maxmtu = 0; 381097d8d152SAndre Oppermann size_t min_protoh; 3811a0292f23SGarrett Wollman 381297d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3813a0292f23SGarrett Wollman 381431b3783cSHajimu UMEMOTO #ifdef INET6 3815dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3816603724d3SBjoern A. Zeeb mss = V_tcp_v6mssdflt; 3817233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 381897d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 3819b287c6c7SBjoern A. Zeeb } 382031b3783cSHajimu UMEMOTO #endif 3821b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3822b287c6c7SBjoern A. Zeeb else 3823b287c6c7SBjoern A. Zeeb #endif 3824b287c6c7SBjoern A. Zeeb #ifdef INET 382597d8d152SAndre Oppermann { 3826603724d3SBjoern A. Zeeb mss = V_tcp_mssdflt; 3827233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 382897d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 382997d8d152SAndre Oppermann } 3830b287c6c7SBjoern A. Zeeb #endif 38313a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET) 38323a9391deSBjoern A. Zeeb thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 38333a9391deSBjoern A. Zeeb #endif 38343a9391deSBjoern A. Zeeb 383597d8d152SAndre Oppermann if (maxmtu && thcmtu) 383697d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 383797d8d152SAndre Oppermann else if (maxmtu || thcmtu) 383897d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 383997d8d152SAndre Oppermann 384097d8d152SAndre Oppermann return (mss); 3841df8bae1dSRodney W. Grimes } 384246f58482SJonathan Lemon 384346f58482SJonathan Lemon 384446f58482SJonathan Lemon /* 3845c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3846c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3847c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3848c068736aSJeffrey Hsu * be started again. 384946f58482SJonathan Lemon */ 385055bceb1eSRandall Stewart void 3851ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 385246f58482SJonathan Lemon { 385346f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 38543ac12506SJonathan T. Looney uint32_t ocwnd = tp->snd_cwnd; 38550c39d38dSGleb Smirnoff u_int maxseg = tcp_maxseg(tp); 385646f58482SJonathan Lemon 38578501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 38587a3244ccSRobert Watson 3859b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 386046f58482SJonathan Lemon tp->t_rtttime = 0; 386146f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 38626b2a5f92SJayanth Vijayaraghavan /* 3863c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3864c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 38656b2a5f92SJayanth Vijayaraghavan */ 38660c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th); 3867a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 386855bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 386946f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 387046f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 387146f58482SJonathan Lemon tp->snd_nxt = onxt; 387246f58482SJonathan Lemon /* 387346f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 387446f58482SJonathan Lemon * not updated yet. 387546f58482SJonathan Lemon */ 3876dbc42409SLawrence Stewart if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th)) 3877dbc42409SLawrence Stewart tp->snd_cwnd -= BYTES_THIS_ACK(tp, th); 3878d7587117SPaul Saab else 3879d7587117SPaul Saab tp->snd_cwnd = 0; 38800c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 388146f58482SJonathan Lemon } 388212eeb81fSHiren Panchasara 388312eeb81fSHiren Panchasara int 388412eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp) 388512eeb81fSHiren Panchasara { 388612eeb81fSHiren Panchasara return (tp->snd_max - tp->snd_una + 388712eeb81fSHiren Panchasara tp->sackhint.sack_bytes_rexmit - 388812eeb81fSHiren Panchasara tp->sackhint.sacked_bytes); 388912eeb81fSHiren Panchasara } 3890