1c398230bSWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4e79adb8eSGarrett Wollman * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6dbc42409SLawrence Stewart * Copyright (c) 2007-2008,2010 7dbc42409SLawrence Stewart * Swinburne University of Technology, Melbourne, Australia. 8dbc42409SLawrence Stewart * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org> 9dbc42409SLawrence Stewart * Copyright (c) 2010 The FreeBSD Foundation 10fa046d87SRobert Watson * Copyright (c) 2010-2011 Juniper Networks, Inc. 11dbc42409SLawrence Stewart * All rights reserved. 12dbc42409SLawrence Stewart * 13dbc42409SLawrence Stewart * Portions of this software were developed at the Centre for Advanced Internet 14891b8ed4SLawrence Stewart * Architectures, Swinburne University of Technology, by Lawrence Stewart, 15891b8ed4SLawrence Stewart * James Healy and David Hayes, made possible in part by a grant from the Cisco 16891b8ed4SLawrence Stewart * University Research Program Fund at Community Foundation Silicon Valley. 17dbc42409SLawrence Stewart * 18dbc42409SLawrence Stewart * Portions of this software were developed at the Centre for Advanced 19dbc42409SLawrence Stewart * Internet Architectures, Swinburne University of Technology, Melbourne, 20dbc42409SLawrence Stewart * Australia by David Hayes under sponsorship from the FreeBSD Foundation. 21df8bae1dSRodney W. Grimes * 22fa046d87SRobert Watson * Portions of this software were developed by Robert N. M. Watson under 23fa046d87SRobert Watson * contract to Juniper Networks, Inc. 24fa046d87SRobert Watson * 25df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 26df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 27df8bae1dSRodney W. Grimes * are met: 28df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 29df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 30df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 31df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 32df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 33fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 34df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 35df8bae1dSRodney W. Grimes * without specific prior written permission. 36df8bae1dSRodney W. Grimes * 37df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 38df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 39df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 40df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 41df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 42df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 43df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 44df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 45df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 46df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 47df8bae1dSRodney W. Grimes * SUCH DAMAGE. 48df8bae1dSRodney W. Grimes * 49e79adb8eSGarrett Wollman * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 50df8bae1dSRodney W. Grimes */ 51df8bae1dSRodney W. Grimes 524b421e2dSMike Silbersack #include <sys/cdefs.h> 534b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 544b421e2dSMike Silbersack 551cfd4b53SBruce M Simpson #include "opt_inet.h" 56fb59c426SYoshinobu Inoue #include "opt_inet6.h" 573a2a9f79SYoshinobu Inoue #include "opt_ipsec.h" 580cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 590cc12cc5SJoerg Wunsch 60df8bae1dSRodney W. Grimes #include <sys/param.h> 6198163b98SPoul-Henning Kamp #include <sys/kernel.h> 62bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 6339bc9de5SLawrence Stewart #include <sys/hhook.h> 64bd79708dSJonathan T. Looney #endif 65df8bae1dSRodney W. Grimes #include <sys/malloc.h> 66df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 67a29f300eSGarrett Wollman #include <sys/proc.h> /* for proc0 declaration */ 68df8bae1dSRodney W. Grimes #include <sys/protosw.h> 6957f60867SMark Johnston #include <sys/sdt.h> 70960ed29cSSeigo Tanimura #include <sys/signalvar.h> 71df8bae1dSRodney W. Grimes #include <sys/socket.h> 72df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 73960ed29cSSeigo Tanimura #include <sys/sysctl.h> 74816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 75960ed29cSSeigo Tanimura #include <sys/systm.h> 76df8bae1dSRodney W. Grimes 77e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 78e79adb8eSGarrett Wollman 7912e2e970SAndre Oppermann #include <vm/uma.h> 8012e2e970SAndre Oppermann 81df8bae1dSRodney W. Grimes #include <net/if.h> 8276039bc8SGleb Smirnoff #include <net/if_var.h> 83df8bae1dSRodney W. Grimes #include <net/route.h> 84530c0060SRobert Watson #include <net/vnet.h> 85df8bae1dSRodney W. Grimes 86773673c1SAndre Oppermann #define TCPSTATES /* for logging */ 87773673c1SAndre Oppermann 88df8bae1dSRodney W. Grimes #include <netinet/in.h> 8957f60867SMark Johnston #include <netinet/in_kdtrace.h> 90960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 91df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 92df8bae1dSRodney W. Grimes #include <netinet/ip.h> 938e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 94686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 95df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 96ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 97686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 98686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h> 99686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 100b66d74c1SGleb Smirnoff #include <netinet6/in6_var.h> 101960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h> 102960ed29cSSeigo Tanimura #include <netinet6/nd6.h> 1032de3e790SGleb Smirnoff #include <netinet/tcp.h> 104df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 1052529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h> 106df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 107df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 108df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 109fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 110df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 1114644fda3SGleb Smirnoff #include <netinet/cc/cc.h> 112c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h> 11386a996e6SHiren Panchasara #ifdef TCPPCAP 11486a996e6SHiren Panchasara #include <netinet/tcp_pcap.h> 11586a996e6SHiren Panchasara #endif 116c325962bSMike Silbersack #include <netinet/tcp_syncache.h> 117610ee2f9SDavid Greenman #ifdef TCPDEBUG 118df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 119fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */ 12009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 12109fe6320SNavdeep Parhar #include <netinet/tcp_offload.h> 12209fe6320SNavdeep Parhar #endif 123fb59c426SYoshinobu Inoue 124fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 125b9234fafSSam Leffler 126db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 127db4f9cc7SJonathan Lemon 128aed55708SRobert Watson #include <security/mac/mac_framework.h> 129aed55708SRobert Watson 130dbc42409SLawrence Stewart const int tcprexmtthresh = 3; 1310312fbe9SPoul-Henning Kamp 132773673c1SAndre Oppermann int tcp_log_in_vain = 0; 133816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 134eddfbb76SRobert Watson &tcp_log_in_vain, 0, 135eddfbb76SRobert Watson "Log all incoming TCP segments to closed ports"); 136816a3d83SPoul-Henning Kamp 13782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0; 13882cea7e6SBjoern A. Zeeb #define V_blackhole VNET(blackhole) 1396df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW, 140eddfbb76SRobert Watson &VNET_NAME(blackhole), 0, 141eddfbb76SRobert Watson "Do not send RST on segments to closed ports"); 14216f7f31fSGeoff Rehmet 14382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1; 1446df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW, 145eddfbb76SRobert Watson &VNET_NAME(tcp_delack_enabled), 0, 1463d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 147f498eeeeSDavid Greenman 14882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0; 1496df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW, 150eddfbb76SRobert Watson &VNET_NAME(drop_synfin), 0, 151eddfbb76SRobert Watson "Drop TCP packets with SYN+FIN set"); 152f8613305SDag-Erling Smørgrav 15312eeb81fSHiren Panchasara VNET_DEFINE(int, tcp_do_rfc6675_pipe) = 0; 154054d38e3SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc6675_pipe, CTLFLAG_VNET | CTLFLAG_RW, 15512eeb81fSHiren Panchasara &VNET_NAME(tcp_do_rfc6675_pipe), 0, 15612eeb81fSHiren Panchasara "Use calculated pipe/in-flight bytes per RFC 6675"); 15712eeb81fSHiren Panchasara 15882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1; 1596df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW, 160eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3042), 0, 161eddfbb76SRobert Watson "Enable RFC 3042 (Limited Transmit)"); 162582a954bSJeffrey Hsu 16382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1; 1646df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW, 165eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3390), 0, 166da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 167da3a8a1aSJeffrey Hsu 168356c7958SHiren Panchasara VNET_DEFINE(int, tcp_initcwnd_segments) = 10; 169356c7958SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments, 170356c7958SHiren Panchasara CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0, 171356c7958SHiren Panchasara "Slow-start flight size (initial congestion window) in number of segments"); 17209440655SAndre Oppermann 17382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1; 1746df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW, 175eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3465), 0, 17624cb0f22SLawrence Stewart "Enable RFC 3465 (Appropriate Byte Counting)"); 177eddfbb76SRobert Watson 17882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2; 1796df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW, 180eddfbb76SRobert Watson &VNET_NAME(tcp_abc_l_var), 2, 18124cb0f22SLawrence Stewart "Cap the max cwnd increment during slow-start to this number of segments"); 18224cb0f22SLawrence Stewart 1836472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN"); 184f2512ba1SRui Paulo 185883054b4SDon Lewis VNET_DEFINE(int, tcp_do_ecn) = 2; 1866df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW, 187eddfbb76SRobert Watson &VNET_NAME(tcp_do_ecn), 0, 188eddfbb76SRobert Watson "TCP ECN support"); 189eddfbb76SRobert Watson 19082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1; 1916df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_VNET | CTLFLAG_RW, 192eddfbb76SRobert Watson &VNET_NAME(tcp_ecn_maxretries), 0, 193eddfbb76SRobert Watson "Max retries before giving up on ECN"); 194eddfbb76SRobert Watson 1953220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0; 1966df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW, 1973220a212SGleb Smirnoff &VNET_NAME(tcp_insecure_syn), 0, 1983220a212SGleb Smirnoff "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets"); 1993220a212SGleb Smirnoff 20082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0; 2016df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW, 202eddfbb76SRobert Watson &VNET_NAME(tcp_insecure_rst), 0, 2033220a212SGleb Smirnoff "Follow RFC793 instead of RFC5961 criteria for accepting RST packets"); 204a69968eeSMike Silbersack 205fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64; 206873789cbSAndre Oppermann #define V_tcp_recvspace VNET(tcp_recvspace) 2076df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW, 208873789cbSAndre Oppermann &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size"); 209873789cbSAndre Oppermann 21082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1; 2116df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW, 212eddfbb76SRobert Watson &VNET_NAME(tcp_do_autorcvbuf), 0, 2138b615593SMarko Zec "Enable automatic receive buffer sizing"); 2146741ecf5SAndre Oppermann 21582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024; 2166df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_VNET | CTLFLAG_RW, 217eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_inc), 0, 2186489fe65SAndre Oppermann "Incrementor step size of automatic receive buffer"); 2196741ecf5SAndre Oppermann 220b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024; 2216df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW, 222eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_max), 0, 2238b615593SMarko Zec "Max size of automatic receive buffer"); 2246741ecf5SAndre Oppermann 225eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb); 22644e33a07SMarko Zec #define tcb6 tcb /* for KAME src sync over BSD*'s */ 22782cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo); 228df8bae1dSRodney W. Grimes 229315e3e38SRobert Watson /* 230bf840a17SGleb Smirnoff * TCP statistics are stored in an array of counter(9)s, which size matches 231bf840a17SGleb Smirnoff * size of struct tcpstat. TCP running connection count is a regular array. 2325923c293SGleb Smirnoff */ 2335da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat); 2345da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat, 2355da0521fSAndrey V. Elsukov tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 236bf840a17SGleb Smirnoff VNET_DEFINE(counter_u64_t, tcps_states[TCP_NSTATES]); 237bf840a17SGleb Smirnoff SYSCTL_COUNTER_U64_ARRAY(_net_inet_tcp, TCPCTL_STATES, states, CTLFLAG_RD | 238d4d32b9fSHans Petter Selasky CTLFLAG_VNET, &VNET_NAME(tcps_states)[0], TCP_NSTATES, 239bf840a17SGleb Smirnoff "TCP connection counts by TCP state"); 240bf840a17SGleb Smirnoff 241bf840a17SGleb Smirnoff static void 242bf840a17SGleb Smirnoff tcp_vnet_init(const void *unused) 243bf840a17SGleb Smirnoff { 244bf840a17SGleb Smirnoff 245f59d975eSGleb Smirnoff COUNTER_ARRAY_ALLOC(V_tcps_states, TCP_NSTATES, M_WAITOK); 246bf840a17SGleb Smirnoff VNET_PCPUSTAT_ALLOC(tcpstat, M_WAITOK); 247bf840a17SGleb Smirnoff } 248bf840a17SGleb Smirnoff VNET_SYSINIT(tcp_vnet_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 249bf840a17SGleb Smirnoff tcp_vnet_init, NULL); 2505923c293SGleb Smirnoff 2515923c293SGleb Smirnoff #ifdef VIMAGE 252bf840a17SGleb Smirnoff static void 253bf840a17SGleb Smirnoff tcp_vnet_uninit(const void *unused) 254bf840a17SGleb Smirnoff { 255bf840a17SGleb Smirnoff 256f59d975eSGleb Smirnoff COUNTER_ARRAY_FREE(V_tcps_states, TCP_NSTATES); 257bf840a17SGleb Smirnoff VNET_PCPUSTAT_FREE(tcpstat); 258bf840a17SGleb Smirnoff } 259bf840a17SGleb Smirnoff VNET_SYSUNINIT(tcp_vnet_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 260bf840a17SGleb Smirnoff tcp_vnet_uninit, NULL); 2615923c293SGleb Smirnoff #endif /* VIMAGE */ 262bf840a17SGleb Smirnoff 2635923c293SGleb Smirnoff /* 264315e3e38SRobert Watson * Kernel module interface for updating tcpstat. The argument is an index 2655923c293SGleb Smirnoff * into tcpstat treated as an array. 266315e3e38SRobert Watson */ 267315e3e38SRobert Watson void 268315e3e38SRobert Watson kmod_tcpstat_inc(int statnum) 269315e3e38SRobert Watson { 270315e3e38SRobert Watson 2715da0521fSAndrey V. Elsukov counter_u64_add(VNET(tcpstat)[statnum], 1); 272315e3e38SRobert Watson } 273315e3e38SRobert Watson 274bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 275dbc42409SLawrence Stewart /* 27639bc9de5SLawrence Stewart * Wrapper for the TCP established input helper hook. 27739bc9de5SLawrence Stewart */ 27855bceb1eSRandall Stewart void 27939bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to) 28039bc9de5SLawrence Stewart { 28139bc9de5SLawrence Stewart struct tcp_hhook_data hhook_data; 28239bc9de5SLawrence Stewart 28339bc9de5SLawrence Stewart if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) { 28439bc9de5SLawrence Stewart hhook_data.tp = tp; 28539bc9de5SLawrence Stewart hhook_data.th = th; 28639bc9de5SLawrence Stewart hhook_data.to = to; 28739bc9de5SLawrence Stewart 28839bc9de5SLawrence Stewart hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data, 28939bc9de5SLawrence Stewart tp->osd); 29039bc9de5SLawrence Stewart } 29139bc9de5SLawrence Stewart } 292bd79708dSJonathan T. Looney #endif 29339bc9de5SLawrence Stewart 29439bc9de5SLawrence Stewart /* 295dbc42409SLawrence Stewart * CC wrapper hook functions 296dbc42409SLawrence Stewart */ 29755bceb1eSRandall Stewart void 2984b7b743cSLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t nsegs, 2994b7b743cSLawrence Stewart uint16_t type) 300f2512ba1SRui Paulo { 301dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 302f2512ba1SRui Paulo 3034b7b743cSLawrence Stewart tp->ccv->nsegs = nsegs; 304dbc42409SLawrence Stewart tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th); 3055b648e79SLawrence Stewart if (tp->snd_cwnd <= tp->snd_wnd) 306dbc42409SLawrence Stewart tp->ccv->flags |= CCF_CWND_LIMITED; 307dbc42409SLawrence Stewart else 308dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_CWND_LIMITED; 309dbc42409SLawrence Stewart 310dbc42409SLawrence Stewart if (type == CC_ACK) { 311dbc42409SLawrence Stewart if (tp->snd_cwnd > tp->snd_ssthresh) { 312dbc42409SLawrence Stewart tp->t_bytes_acked += min(tp->ccv->bytes_this_ack, 3134b7b743cSLawrence Stewart nsegs * V_tcp_abc_l_var * tcp_maxseg(tp)); 314dbc42409SLawrence Stewart if (tp->t_bytes_acked >= tp->snd_cwnd) { 315dbc42409SLawrence Stewart tp->t_bytes_acked -= tp->snd_cwnd; 316dbc42409SLawrence Stewart tp->ccv->flags |= CCF_ABC_SENTAWND; 317dbc42409SLawrence Stewart } 318dbc42409SLawrence Stewart } else { 319dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_ABC_SENTAWND; 320dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 321dbc42409SLawrence Stewart } 322dbc42409SLawrence Stewart } 323dbc42409SLawrence Stewart 324dbc42409SLawrence Stewart if (CC_ALGO(tp)->ack_received != NULL) { 325dbc42409SLawrence Stewart /* XXXLAS: Find a way to live without this */ 326dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 327dbc42409SLawrence Stewart CC_ALGO(tp)->ack_received(tp->ccv, type); 328dbc42409SLawrence Stewart } 329dbc42409SLawrence Stewart } 330dbc42409SLawrence Stewart 33155bceb1eSRandall Stewart void 332dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp) 333dbc42409SLawrence Stewart { 334dbc42409SLawrence Stewart struct hc_metrics_lite metrics; 335dbc42409SLawrence Stewart struct inpcb *inp = tp->t_inpcb; 3360c39d38dSGleb Smirnoff u_int maxseg; 337dbc42409SLawrence Stewart int rtt; 338dbc42409SLawrence Stewart 339dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 340dbc42409SLawrence Stewart 341dbc42409SLawrence Stewart tcp_hc_get(&inp->inp_inc, &metrics); 3420c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 343dbc42409SLawrence Stewart 344dbc42409SLawrence Stewart if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 345dbc42409SLawrence Stewart tp->t_srtt = rtt; 346dbc42409SLawrence Stewart tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 347dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrtt); 348dbc42409SLawrence Stewart if (metrics.rmx_rttvar) { 349dbc42409SLawrence Stewart tp->t_rttvar = metrics.rmx_rttvar; 350dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrttvar); 351dbc42409SLawrence Stewart } else { 352dbc42409SLawrence Stewart /* default variation is +- 1 rtt */ 353dbc42409SLawrence Stewart tp->t_rttvar = 354dbc42409SLawrence Stewart tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 355dbc42409SLawrence Stewart } 356dbc42409SLawrence Stewart TCPT_RANGESET(tp->t_rxtcur, 357dbc42409SLawrence Stewart ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 358dbc42409SLawrence Stewart tp->t_rttmin, TCPTV_REXMTMAX); 359dbc42409SLawrence Stewart } 360dbc42409SLawrence Stewart if (metrics.rmx_ssthresh) { 361dbc42409SLawrence Stewart /* 362dbc42409SLawrence Stewart * There's some sort of gateway or interface 363dbc42409SLawrence Stewart * buffer limit on the path. Use this to set 364a4641f4eSPedro F. Giffuni * the slow start threshold, but set the 365dbc42409SLawrence Stewart * threshold to no less than 2*mss. 366dbc42409SLawrence Stewart */ 3670c39d38dSGleb Smirnoff tp->snd_ssthresh = max(2 * maxseg, metrics.rmx_ssthresh); 368dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedssthresh); 369dbc42409SLawrence Stewart } 370dbc42409SLawrence Stewart 371dbc42409SLawrence Stewart /* 3729ec4a4ccSAndre Oppermann * Set the initial slow-start flight size. 373dbc42409SLawrence Stewart * 374cf8f04f4SAndre Oppermann * If a SYN or SYN/ACK was lost and retransmitted, we have to 375cf8f04f4SAndre Oppermann * reduce the initial CWND to one segment as congestion is likely 376cf8f04f4SAndre Oppermann * requiring us to be cautious. 377dbc42409SLawrence Stewart */ 378cf8f04f4SAndre Oppermann if (tp->snd_cwnd == 1) 3790c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; /* SYN(-ACK) lost */ 380dbc42409SLawrence Stewart else 381*7dc90a1dSMichael Tuexen tp->snd_cwnd = tcp_compute_initwnd(maxseg); 382dbc42409SLawrence Stewart 383dbc42409SLawrence Stewart if (CC_ALGO(tp)->conn_init != NULL) 384dbc42409SLawrence Stewart CC_ALGO(tp)->conn_init(tp->ccv); 385dbc42409SLawrence Stewart } 386dbc42409SLawrence Stewart 387dbc42409SLawrence Stewart void inline 388dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type) 389dbc42409SLawrence Stewart { 3900c39d38dSGleb Smirnoff u_int maxseg; 3910c39d38dSGleb Smirnoff 392dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 393dbc42409SLawrence Stewart 394dbc42409SLawrence Stewart switch(type) { 395dbc42409SLawrence Stewart case CC_NDUPACK: 396dbc42409SLawrence Stewart if (!IN_FASTRECOVERY(tp->t_flags)) { 397f2512ba1SRui Paulo tp->snd_recover = tp->snd_max; 398f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) 399f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_CWR; 400f2512ba1SRui Paulo } 401dbc42409SLawrence Stewart break; 402dbc42409SLawrence Stewart case CC_ECN: 403dbc42409SLawrence Stewart if (!IN_CONGRECOVERY(tp->t_flags)) { 404dbc42409SLawrence Stewart TCPSTAT_INC(tcps_ecn_rcwnd); 405dbc42409SLawrence Stewart tp->snd_recover = tp->snd_max; 406dbc42409SLawrence Stewart if (tp->t_flags & TF_ECN_PERMIT) 407dbc42409SLawrence Stewart tp->t_flags |= TF_ECN_SND_CWR; 408dbc42409SLawrence Stewart } 409dbc42409SLawrence Stewart break; 410dbc42409SLawrence Stewart case CC_RTO: 4110c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 412dbc42409SLawrence Stewart tp->t_dupacks = 0; 413dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 414dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 41543053c12SSean Bruno tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 / 41643053c12SSean Bruno maxseg) * maxseg; 4170c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 418dbc42409SLawrence Stewart break; 419dbc42409SLawrence Stewart case CC_RTO_ERR: 420dbc42409SLawrence Stewart TCPSTAT_INC(tcps_sndrexmitbad); 421dbc42409SLawrence Stewart /* RTO was unnecessary, so reset everything. */ 422dbc42409SLawrence Stewart tp->snd_cwnd = tp->snd_cwnd_prev; 423dbc42409SLawrence Stewart tp->snd_ssthresh = tp->snd_ssthresh_prev; 424dbc42409SLawrence Stewart tp->snd_recover = tp->snd_recover_prev; 425dbc42409SLawrence Stewart if (tp->t_flags & TF_WASFRECOVERY) 426dbc42409SLawrence Stewart ENTER_FASTRECOVERY(tp->t_flags); 427dbc42409SLawrence Stewart if (tp->t_flags & TF_WASCRECOVERY) 428dbc42409SLawrence Stewart ENTER_CONGRECOVERY(tp->t_flags); 429dbc42409SLawrence Stewart tp->snd_nxt = tp->snd_max; 430672dc4aeSJohn Baldwin tp->t_flags &= ~TF_PREVVALID; 431dbc42409SLawrence Stewart tp->t_badrxtwin = 0; 432dbc42409SLawrence Stewart break; 433dbc42409SLawrence Stewart } 434dbc42409SLawrence Stewart 435dbc42409SLawrence Stewart if (CC_ALGO(tp)->cong_signal != NULL) { 436dbc42409SLawrence Stewart if (th != NULL) 437dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 438dbc42409SLawrence Stewart CC_ALGO(tp)->cong_signal(tp->ccv, type); 439dbc42409SLawrence Stewart } 440dbc42409SLawrence Stewart } 441dbc42409SLawrence Stewart 44255bceb1eSRandall Stewart void inline 443dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th) 444dbc42409SLawrence Stewart { 445dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 446dbc42409SLawrence Stewart 447dbc42409SLawrence Stewart /* XXXLAS: KASSERT that we're in recovery? */ 448dbc42409SLawrence Stewart 449dbc42409SLawrence Stewart if (CC_ALGO(tp)->post_recovery != NULL) { 450dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 451dbc42409SLawrence Stewart CC_ALGO(tp)->post_recovery(tp->ccv); 452dbc42409SLawrence Stewart } 453dbc42409SLawrence Stewart /* XXXLAS: EXIT_RECOVERY ? */ 454dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 455dbc42409SLawrence Stewart } 4560312fbe9SPoul-Henning Kamp 457df8bae1dSRodney W. Grimes /* 458262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 45985536381SHiren Panchasara * following conditions are met: 46085536381SHiren Panchasara * - There is no delayed ack timer in progress. 46185536381SHiren Panchasara * - Our last ack wasn't a 0-sized window. We never want to delay 46285536381SHiren Panchasara * the ack that opens up a 0-sized window. 46385536381SHiren Panchasara * - LRO wasn't used for this segment. We make sure by checking that the 46485536381SHiren Panchasara * segment size is not larger than the MSS. 465d8c85a26SJonathan Lemon */ 466c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen) \ 467b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 468a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 4690c39d38dSGleb Smirnoff (tlen <= tp->t_maxseg) && \ 470603724d3SBjoern A. Zeeb (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 471d8c85a26SJonathan Lemon 47264807b30SHiren Panchasara static void inline 47364807b30SHiren Panchasara cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos) 47464807b30SHiren Panchasara { 47564807b30SHiren Panchasara INP_WLOCK_ASSERT(tp->t_inpcb); 47664807b30SHiren Panchasara 47764807b30SHiren Panchasara if (CC_ALGO(tp)->ecnpkt_handler != NULL) { 47864807b30SHiren Panchasara switch (iptos & IPTOS_ECN_MASK) { 47964807b30SHiren Panchasara case IPTOS_ECN_CE: 48064807b30SHiren Panchasara tp->ccv->flags |= CCF_IPHDR_CE; 48164807b30SHiren Panchasara break; 48264807b30SHiren Panchasara case IPTOS_ECN_ECT0: 48364807b30SHiren Panchasara tp->ccv->flags &= ~CCF_IPHDR_CE; 48464807b30SHiren Panchasara break; 48564807b30SHiren Panchasara case IPTOS_ECN_ECT1: 48664807b30SHiren Panchasara tp->ccv->flags &= ~CCF_IPHDR_CE; 48764807b30SHiren Panchasara break; 48864807b30SHiren Panchasara } 48964807b30SHiren Panchasara 49064807b30SHiren Panchasara if (th->th_flags & TH_CWR) 49164807b30SHiren Panchasara tp->ccv->flags |= CCF_TCPHDR_CWR; 49264807b30SHiren Panchasara else 49364807b30SHiren Panchasara tp->ccv->flags &= ~CCF_TCPHDR_CWR; 49464807b30SHiren Panchasara 49564807b30SHiren Panchasara if (tp->t_flags & TF_DELACK) 49664807b30SHiren Panchasara tp->ccv->flags |= CCF_DELACK; 49764807b30SHiren Panchasara else 49864807b30SHiren Panchasara tp->ccv->flags &= ~CCF_DELACK; 49964807b30SHiren Panchasara 50064807b30SHiren Panchasara CC_ALGO(tp)->ecnpkt_handler(tp->ccv); 50164807b30SHiren Panchasara 50264807b30SHiren Panchasara if (tp->ccv->flags & CCF_ACKNOW) 50364807b30SHiren Panchasara tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 50464807b30SHiren Panchasara } 50564807b30SHiren Panchasara } 50664807b30SHiren Panchasara 507df8bae1dSRodney W. Grimes /* 5088d573cc1SAndre Oppermann * TCP input handling is split into multiple parts: 5098d573cc1SAndre Oppermann * tcp6_input is a thin wrapper around tcp_input for the extended 5108d573cc1SAndre Oppermann * ip6_protox[] call format in ip6_input 5118d573cc1SAndre Oppermann * tcp_input handles primary segment validation, inpcb lookup and 5128d573cc1SAndre Oppermann * SYN processing on listen sockets 5138d573cc1SAndre Oppermann * tcp_do_segment processes the ACK and text of the segment for 5148d573cc1SAndre Oppermann * establishing, established and closing connections 515df8bae1dSRodney W. Grimes */ 516fb59c426SYoshinobu Inoue #ifdef INET6 517fb59c426SYoshinobu Inoue int 518ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto) 519fb59c426SYoshinobu Inoue { 520ad3f9ab3SAndre Oppermann struct mbuf *m = *mp; 52133841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 5223e88eb90SAndrey V. Elsukov struct ip6_hdr *ip6; 523fb59c426SYoshinobu Inoue 524fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 525fb59c426SYoshinobu Inoue 526fb59c426SYoshinobu Inoue /* 527fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 528fb59c426SYoshinobu Inoue * better place to put this in? 529fb59c426SYoshinobu Inoue */ 5303e88eb90SAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 5313e88eb90SAndrey V. Elsukov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */); 53233841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 533fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 534fb59c426SYoshinobu Inoue 5358c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 536fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 537fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 538fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 5398f5a8818SKevin Lo return (IPPROTO_DONE); 540fb59c426SYoshinobu Inoue } 54177d396fdSMaksim Yevmenkin if (ia6) 54277d396fdSMaksim Yevmenkin ifa_free(&ia6->ia_ifa); 543fb59c426SYoshinobu Inoue 5448f5a8818SKevin Lo return (tcp_input(mp, offp, proto)); 545fb59c426SYoshinobu Inoue } 546b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 547fb59c426SYoshinobu Inoue 5488f5a8818SKevin Lo int 5498f5a8818SKevin Lo tcp_input(struct mbuf **mp, int *offp, int proto) 550df8bae1dSRodney W. Grimes { 5518f5a8818SKevin Lo struct mbuf *m = *mp; 552b287c6c7SBjoern A. Zeeb struct tcphdr *th = NULL; 553ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 554ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 555302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 556302ce8d6SAndre Oppermann struct socket *so = NULL; 557e79adb8eSGarrett Wollman u_char *optp = NULL; 5588f5a8818SKevin Lo int off0; 55926f9a767SRodney W. Grimes int optlen = 0; 560b287c6c7SBjoern A. Zeeb #ifdef INET 561b287c6c7SBjoern A. Zeeb int len; 562b287c6c7SBjoern A. Zeeb #endif 563b287c6c7SBjoern A. Zeeb int tlen = 0, off; 564fb59c426SYoshinobu Inoue int drop_hdrlen; 565ad3f9ab3SAndre Oppermann int thflags; 566302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 5671d7ee746SKurt Lidl uint8_t iptos; 568c1de64a4SAndrey V. Elsukov struct m_tag *fwd_tag = NULL; 5696573d758SMatt Macy struct epoch_tracker et; 570c068736aSJeffrey Hsu #ifdef INET6 571302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 572c068736aSJeffrey Hsu int isipv6; 573c068736aSJeffrey Hsu #else 574a160e630SAndre Oppermann const void *ip6 = NULL; 575b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 576302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 577a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 578252ca428SRobert Watson int ti_locked; 579610ee2f9SDavid Greenman #ifdef TCPDEBUG 580c068736aSJeffrey Hsu /* 581c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 582c068736aSJeffrey Hsu * now IPv6. 583c068736aSJeffrey Hsu */ 58414739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 585410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 586610ee2f9SDavid Greenman short ostate = 0; 587610ee2f9SDavid Greenman #endif 588c068736aSJeffrey Hsu 589fb59c426SYoshinobu Inoue #ifdef INET6 590fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 591fb59c426SYoshinobu Inoue #endif 592a0292f23SGarrett Wollman 5938f5a8818SKevin Lo off0 = *offp; 5948f5a8818SKevin Lo m = *mp; 5958f5a8818SKevin Lo *mp = NULL; 596302ce8d6SAndre Oppermann to.to_flags = 0; 59778b50714SRobert Watson TCPSTAT_INC(tcps_rcvtotal); 598fb59c426SYoshinobu Inoue 59904d3a452SHajimu UMEMOTO #ifdef INET6 600b287c6c7SBjoern A. Zeeb if (isipv6) { 6018d573cc1SAndre Oppermann /* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */ 60245747ba5SBjoern A. Zeeb 60345747ba5SBjoern A. Zeeb if (m->m_len < (sizeof(*ip6) + sizeof(*th))) { 60445747ba5SBjoern A. Zeeb m = m_pullup(m, sizeof(*ip6) + sizeof(*th)); 60545747ba5SBjoern A. Zeeb if (m == NULL) { 60645747ba5SBjoern A. Zeeb TCPSTAT_INC(tcps_rcvshort); 6078f5a8818SKevin Lo return (IPPROTO_DONE); 60845747ba5SBjoern A. Zeeb } 60945747ba5SBjoern A. Zeeb } 61045747ba5SBjoern A. Zeeb 611fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 61245747ba5SBjoern A. Zeeb th = (struct tcphdr *)((caddr_t)ip6 + off0); 613fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 614356ab07eSBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) { 61545747ba5SBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 61645747ba5SBjoern A. Zeeb th->th_sum = m->m_pkthdr.csum_data; 61745747ba5SBjoern A. Zeeb else 61845747ba5SBjoern A. Zeeb th->th_sum = in6_cksum_pseudo(ip6, tlen, 61945747ba5SBjoern A. Zeeb IPPROTO_TCP, m->m_pkthdr.csum_data); 62045747ba5SBjoern A. Zeeb th->th_sum ^= 0xffff; 62145747ba5SBjoern A. Zeeb } else 62245747ba5SBjoern A. Zeeb th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen); 62345747ba5SBjoern A. Zeeb if (th->th_sum) { 62478b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 625fb59c426SYoshinobu Inoue goto drop; 626fb59c426SYoshinobu Inoue } 62733841545SHajimu UMEMOTO 62833841545SHajimu UMEMOTO /* 62933841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 63033841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 63133841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 63233841545SHajimu UMEMOTO * 63333841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 63433841545SHajimu UMEMOTO * already dropped in ip6_input. 63533841545SHajimu UMEMOTO */ 63633841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 63733841545SHajimu UMEMOTO /* XXX stat */ 63833841545SHajimu UMEMOTO goto drop; 63933841545SHajimu UMEMOTO } 6401d7ee746SKurt Lidl iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 641b287c6c7SBjoern A. Zeeb } 64204d3a452SHajimu UMEMOTO #endif 643b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 644b287c6c7SBjoern A. Zeeb else 645b287c6c7SBjoern A. Zeeb #endif 646b287c6c7SBjoern A. Zeeb #ifdef INET 647b287c6c7SBjoern A. Zeeb { 648df8bae1dSRodney W. Grimes /* 649df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 650df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 651df8bae1dSRodney W. Grimes */ 652fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 653105bd211SGleb Smirnoff ip_stripoptions(m); 654fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 655fb59c426SYoshinobu Inoue } 656df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 6574dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 6584dfdffe9SAndre Oppermann == NULL) { 65978b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 6608f5a8818SKevin Lo return (IPPROTO_DONE); 661df8bae1dSRodney W. Grimes } 662df8bae1dSRodney W. Grimes } 663fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 664db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 6658ad458a4SGleb Smirnoff tlen = ntohs(ip->ip_len) - off0; 666df8bae1dSRodney W. Grimes 6671d7ee746SKurt Lidl iptos = ip->ip_tos; 668db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 669db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 670db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 671db4f9cc7SJonathan Lemon else 672db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 673c068736aSJeffrey Hsu ip->ip_dst.s_addr, 6748f134647SGleb Smirnoff htonl(m->m_pkthdr.csum_data + tlen + 675c068736aSJeffrey Hsu IPPROTO_TCP)); 676db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 677db4f9cc7SJonathan Lemon } else { 6788f134647SGleb Smirnoff struct ipovly *ipov = (struct ipovly *)ip; 6798f134647SGleb Smirnoff 680df8bae1dSRodney W. Grimes /* 681df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 682df8bae1dSRodney W. Grimes */ 6838f134647SGleb Smirnoff len = off0 + tlen; 684fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 6858f134647SGleb Smirnoff ipov->ih_len = htons(tlen); 686fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 68757f60867SMark Johnston /* Reset length for SDT probes. */ 6881d7ee746SKurt Lidl ip->ip_len = htons(len); 6891d7ee746SKurt Lidl /* Reset TOS bits */ 6901d7ee746SKurt Lidl ip->ip_tos = iptos; 6911d7ee746SKurt Lidl /* Re-initialization for later version check */ 6921d7ee746SKurt Lidl ip->ip_v = IPVERSION; 693b2fe54bcSAndrey V. Elsukov ip->ip_hl = off0 >> 2; 694db4f9cc7SJonathan Lemon } 69557f60867SMark Johnston 696fb59c426SYoshinobu Inoue if (th->th_sum) { 69778b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 698df8bae1dSRodney W. Grimes goto drop; 699df8bae1dSRodney W. Grimes } 700fb59c426SYoshinobu Inoue } 701b287c6c7SBjoern A. Zeeb #endif /* INET */ 702df8bae1dSRodney W. Grimes 703df8bae1dSRodney W. Grimes /* 704df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 705df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 706df8bae1dSRodney W. Grimes */ 707fb59c426SYoshinobu Inoue off = th->th_off << 2; 708df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 70978b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadoff); 710df8bae1dSRodney W. Grimes goto drop; 711df8bae1dSRodney W. Grimes } 712fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 713df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 71404d3a452SHajimu UMEMOTO #ifdef INET6 715b287c6c7SBjoern A. Zeeb if (isipv6) { 7168f5a8818SKevin Lo IP6_EXTHDR_CHECK(m, off0, off, IPPROTO_DONE); 717fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 718fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 719b287c6c7SBjoern A. Zeeb } 72004d3a452SHajimu UMEMOTO #endif 721b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 722b287c6c7SBjoern A. Zeeb else 723b287c6c7SBjoern A. Zeeb #endif 724b287c6c7SBjoern A. Zeeb #ifdef INET 725b287c6c7SBjoern A. Zeeb { 726df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 727c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 7284dfdffe9SAndre Oppermann == NULL) { 72978b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 7308f5a8818SKevin Lo return (IPPROTO_DONE); 731df8bae1dSRodney W. Grimes } 732fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 733fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 734fb59c426SYoshinobu Inoue } 735df8bae1dSRodney W. Grimes } 736b287c6c7SBjoern A. Zeeb #endif 737df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 738fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 739df8bae1dSRodney W. Grimes } 740fb59c426SYoshinobu Inoue thflags = th->th_flags; 741df8bae1dSRodney W. Grimes 742e46cd3d4SDag-Erling Smørgrav /* 743df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 744df8bae1dSRodney W. Grimes */ 7452903309aSAttilio Rao tcp_fields_to_host(th); 746df8bae1dSRodney W. Grimes 747df8bae1dSRodney W. Grimes /* 748794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 749755c1f07SAndras Olah */ 750fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 751755c1f07SAndras Olah 752755c1f07SAndras Olah /* 753fa046d87SRobert Watson * Locate pcb for segment; if we're likely to add or remove a 754a7c7f2a7SJohn Baldwin * connection then first acquire pcbinfo lock. There are three cases 755fa046d87SRobert Watson * where we might discover later we need a write lock despite the 756a7c7f2a7SJohn Baldwin * flags: ACKs moving a connection out of the syncache, ACKs for a 757a7c7f2a7SJohn Baldwin * connection in TIMEWAIT and SYNs not targeting a listening socket. 758df8bae1dSRodney W. Grimes */ 759a7c7f2a7SJohn Baldwin if ((thflags & (TH_FIN | TH_RST)) != 0) { 7606573d758SMatt Macy INP_INFO_RLOCK_ET(&V_tcbinfo, et); 761ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 762fa046d87SRobert Watson } else 763fa046d87SRobert Watson ti_locked = TI_UNLOCKED; 764252ca428SRobert Watson 7658d573cc1SAndre Oppermann /* 7668d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 7678d573cc1SAndre Oppermann */ 768b8056faeSGleb Smirnoff if ( 769b8056faeSGleb Smirnoff #ifdef INET6 770b8056faeSGleb Smirnoff (isipv6 && (m->m_flags & M_IP6_NEXTHOP)) 771b8056faeSGleb Smirnoff #ifdef INET 772b8056faeSGleb Smirnoff || (!isipv6 && (m->m_flags & M_IP_NEXTHOP)) 773b8056faeSGleb Smirnoff #endif 774b8056faeSGleb Smirnoff #endif 775b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6) 776b8056faeSGleb Smirnoff (m->m_flags & M_IP_NEXTHOP) 777b8056faeSGleb Smirnoff #endif 778b8056faeSGleb Smirnoff ) 7799b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 7809b932e9eSAndre Oppermann 781ce7ad664SEd Maste findpcb: 782ce7ad664SEd Maste #ifdef INVARIANTS 783ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 784ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 785ce7ad664SEd Maste } else { 786384a5c3cSAndrey V. Elsukov INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo); 787ce7ad664SEd Maste } 788ce7ad664SEd Maste #endif 7898a006adbSBjoern A. Zeeb #ifdef INET6 7908a006adbSBjoern A. Zeeb if (isipv6 && fwd_tag != NULL) { 7918a006adbSBjoern A. Zeeb struct sockaddr_in6 *next_hop6; 7928a006adbSBjoern A. Zeeb 7938a006adbSBjoern A. Zeeb next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1); 7948a006adbSBjoern A. Zeeb /* 7958a006adbSBjoern A. Zeeb * Transparently forwarded. Pretend to be the destination. 7968a006adbSBjoern A. Zeeb * Already got one like this? 7978a006adbSBjoern A. Zeeb */ 7988a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, 7998a006adbSBjoern A. Zeeb &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport, 8008a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m); 8018a006adbSBjoern A. Zeeb if (!inp) { 8028a006adbSBjoern A. Zeeb /* 8038a006adbSBjoern A. Zeeb * It's new. Try to find the ambushing socket. 8048a006adbSBjoern A. Zeeb * Because we've rewritten the destination address, 8058a006adbSBjoern A. Zeeb * any hardware-generated hash is ignored. 8068a006adbSBjoern A. Zeeb */ 8078a006adbSBjoern A. Zeeb inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src, 8088a006adbSBjoern A. Zeeb th->th_sport, &next_hop6->sin6_addr, 8098a006adbSBjoern A. Zeeb next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) : 8108a006adbSBjoern A. Zeeb th->th_dport, INPLOOKUP_WILDCARD | 8118a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 8128a006adbSBjoern A. Zeeb } 813c1de64a4SAndrey V. Elsukov } else if (isipv6) { 8148a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src, 8158a006adbSBjoern A. Zeeb th->th_sport, &ip6->ip6_dst, th->th_dport, 8168a006adbSBjoern A. Zeeb INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 8178a006adbSBjoern A. Zeeb m->m_pkthdr.rcvif, m); 8188a006adbSBjoern A. Zeeb } 8198a006adbSBjoern A. Zeeb #endif /* INET6 */ 8208a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET) 8218a006adbSBjoern A. Zeeb else 8228a006adbSBjoern A. Zeeb #endif 8238a006adbSBjoern A. Zeeb #ifdef INET 8248a006adbSBjoern A. Zeeb if (fwd_tag != NULL) { 8259b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 8269b932e9eSAndre Oppermann 8279b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 828f9e354dfSJulian Elischer /* 8292b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 830f9e354dfSJulian Elischer * already got one like this? 831f9e354dfSJulian Elischer */ 832d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport, 833fa046d87SRobert Watson ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB, 834d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 835fa046d87SRobert Watson if (!inp) { 836fa046d87SRobert Watson /* 837fa046d87SRobert Watson * It's new. Try to find the ambushing socket. 838d3c1f003SRobert Watson * Because we've rewritten the destination address, 839d3c1f003SRobert Watson * any hardware-generated hash is ignored. 840fa046d87SRobert Watson */ 841fa046d87SRobert Watson inp = in_pcblookup(&V_tcbinfo, ip->ip_src, 842fa046d87SRobert Watson th->th_sport, next_hop->sin_addr, 843fa046d87SRobert Watson next_hop->sin_port ? ntohs(next_hop->sin_port) : 844fa046d87SRobert Watson th->th_dport, INPLOOKUP_WILDCARD | 845fa046d87SRobert Watson INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 846f9e354dfSJulian Elischer } 847b10fbdeaSAndre Oppermann } else 848d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, 849fa046d87SRobert Watson th->th_sport, ip->ip_dst, th->th_dport, 850fa046d87SRobert Watson INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 851d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 8528a006adbSBjoern A. Zeeb #endif /* INET */ 853f9e354dfSJulian Elischer 854df8bae1dSRodney W. Grimes /* 855574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 856574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 8578b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 858df8bae1dSRodney W. Grimes */ 859816a3d83SPoul-Henning Kamp if (inp == NULL) { 860574b6964SAndre Oppermann /* 861574b6964SAndre Oppermann * Log communication attempts to ports that are not 862574b6964SAndre Oppermann * in use. 863574b6964SAndre Oppermann */ 864574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 865574b6964SAndre Oppermann tcp_log_in_vain == 2) { 866b7d747ecSAndre Oppermann if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6))) 867df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 868df541e5fSAndre Oppermann "to closed port\n", s, __func__); 8692e4e1b4cSGeoff Rehmet } 870574b6964SAndre Oppermann /* 871574b6964SAndre Oppermann * When blackholing do not respond with a RST but 872574b6964SAndre Oppermann * completely ignore the segment and drop it. 873574b6964SAndre Oppermann */ 874603724d3SBjoern A. Zeeb if ((V_blackhole == 1 && (thflags & TH_SYN)) || 875603724d3SBjoern A. Zeeb V_blackhole == 2) 8761929eae1SAndre Oppermann goto dropunlock; 877574b6964SAndre Oppermann 878a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 879a57815efSBosko Milekic goto dropwithreset; 880816a3d83SPoul-Henning Kamp } 881fa046d87SRobert Watson INP_WLOCK_ASSERT(inp); 882f5cf1e5fSJulien Charbon /* 883f5cf1e5fSJulien Charbon * While waiting for inp lock during the lookup, another thread 884f5cf1e5fSJulien Charbon * can have dropped the inpcb, in which case we need to loop back 885f5cf1e5fSJulien Charbon * and try to find a new inpcb to deliver to. 886f5cf1e5fSJulien Charbon */ 887f5cf1e5fSJulien Charbon if (inp->inp_flags & INP_DROPPED) { 888f5cf1e5fSJulien Charbon INP_WUNLOCK(inp); 889f5cf1e5fSJulien Charbon inp = NULL; 890f5cf1e5fSJulien Charbon goto findpcb; 891f5cf1e5fSJulien Charbon } 892c2529042SHans Petter Selasky if ((inp->inp_flowtype == M_HASHTYPE_NONE) && 893c2529042SHans Petter Selasky (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) && 894c2529042SHans Petter Selasky ((inp->inp_socket == NULL) || 895c2529042SHans Petter Selasky (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0)) { 89680cb9f21SKip Macy inp->inp_flowid = m->m_pkthdr.flowid; 8972f719932SAdrian Chadd inp->inp_flowtype = M_HASHTYPE_GET(m); 89880cb9f21SKip Macy } 899fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 900a2589465SKen Smith #ifdef INET6 901fcf59617SAndrey V. Elsukov if (isipv6 && IPSEC_ENABLED(ipv6) && 902fcf59617SAndrey V. Elsukov IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) { 903a2589465SKen Smith goto dropunlock; 904a2589465SKen Smith } 905fcf59617SAndrey V. Elsukov #ifdef INET 906fcf59617SAndrey V. Elsukov else 907fcf59617SAndrey V. Elsukov #endif 908fcf59617SAndrey V. Elsukov #endif /* INET6 */ 909fcf59617SAndrey V. Elsukov #ifdef INET 910fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4) && 911fcf59617SAndrey V. Elsukov IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) { 912fcf59617SAndrey V. Elsukov goto dropunlock; 913fcf59617SAndrey V. Elsukov } 914fcf59617SAndrey V. Elsukov #endif /* INET */ 915a2589465SKen Smith #endif /* IPSEC */ 916a2589465SKen Smith 9178d573cc1SAndre Oppermann /* 9188d573cc1SAndre Oppermann * Check the minimum TTL for socket. 9198d573cc1SAndre Oppermann */ 92034f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 92134f83c52SGeorge V. Neville-Neil #ifdef INET6 9222d8868dbSJonathan T. Looney if (isipv6) { 9232d8868dbSJonathan T. Looney if (inp->inp_ip_minttl > ip6->ip6_hlim) 924302ce8d6SAndre Oppermann goto dropunlock; 9252d8868dbSJonathan T. Looney } else 92634f83c52SGeorge V. Neville-Neil #endif 92734f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 928302ce8d6SAndre Oppermann goto dropunlock; 92934f83c52SGeorge V. Neville-Neil } 930936cd18dSAndre Oppermann 931340c35deSJonathan Lemon /* 932252ca428SRobert Watson * A previous connection in TIMEWAIT state is supposed to catch stray 933252ca428SRobert Watson * or duplicate segments arriving late. If this segment was a 9340989f56cSRobert Watson * legitimate new connection attempt, the old INPCB gets removed and 935252ca428SRobert Watson * we can try again to find a listening socket. 936252ca428SRobert Watson * 937fa046d87SRobert Watson * At this point, due to earlier optimism, we may hold only an inpcb 938fa046d87SRobert Watson * lock, and not the inpcbinfo write lock. If so, we need to try to 939fa046d87SRobert Watson * acquire it, or if that fails, acquire a reference on the inpcb, 940fa046d87SRobert Watson * drop all locks, acquire a global write lock, and then re-acquire 941fa046d87SRobert Watson * the inpcb lock. We may at that point discover that another thread 942fa046d87SRobert Watson * has tried to free the inpcb, in which case we need to loop back 943fa046d87SRobert Watson * and try to find a new inpcb to deliver to. 944fa046d87SRobert Watson * 945fa046d87SRobert Watson * XXXRW: It may be time to rethink timewait locking. 946340c35deSJonathan Lemon */ 947ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 948fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 9496573d758SMatt Macy INP_INFO_RLOCK_ET(&V_tcbinfo, et); 950ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 951252ca428SRobert Watson } 952ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 953252ca428SRobert Watson 954340c35deSJonathan Lemon if (thflags & TH_SYN) 955f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 9568d573cc1SAndre Oppermann /* 9578d573cc1SAndre Oppermann * NB: tcp_twcheck unlocks the INP and frees the mbuf. 9588d573cc1SAndre Oppermann */ 9592104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 960340c35deSJonathan Lemon goto findpcb; 9616573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 9628f5a8818SKevin Lo return (IPPROTO_DONE); 963340c35deSJonathan Lemon } 964794235b7SAndre Oppermann /* 965794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 966794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 967794235b7SAndre Oppermann * segment and send an appropriate response. 968794235b7SAndre Oppermann */ 969df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 970a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 971a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 972a57815efSBosko Milekic goto dropwithreset; 97309f81a46SBosko Milekic } 974df8bae1dSRodney W. Grimes 97509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 97609fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 97709fe6320SNavdeep Parhar tcp_offload_input(tp, m); 97809fe6320SNavdeep Parhar m = NULL; /* consumed by the TOE driver */ 97909fe6320SNavdeep Parhar goto dropunlock; 98009fe6320SNavdeep Parhar } 98109fe6320SNavdeep Parhar #endif 98209fe6320SNavdeep Parhar 983252ca428SRobert Watson /* 984252ca428SRobert Watson * We've identified a valid inpcb, but it could be that we need an 985fa046d87SRobert Watson * inpcbinfo write lock but don't hold it. In this case, attempt to 986fa046d87SRobert Watson * acquire using the same strategy as the TIMEWAIT case above. If we 987fa046d87SRobert Watson * relock, we have to jump back to 'relocked' as the connection might 988fa046d87SRobert Watson * now be in TIMEWAIT. 989252ca428SRobert Watson */ 990fa046d87SRobert Watson #ifdef INVARIANTS 991a7c7f2a7SJohn Baldwin if ((thflags & (TH_FIN | TH_RST)) != 0) 992ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 993fa046d87SRobert Watson #endif 994a7c7f2a7SJohn Baldwin if (!((tp->t_state == TCPS_ESTABLISHED && (thflags & TH_SYN) == 0) || 995281a0fd4SPatrick Kelsey (tp->t_state == TCPS_LISTEN && (thflags & TH_SYN) && 99668bd7ed1SJonathan T. Looney !IS_FASTOPEN(tp->t_flags)))) { 997fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 9986573d758SMatt Macy INP_INFO_RLOCK_ET(&V_tcbinfo, et); 999ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 1000252ca428SRobert Watson } 1001ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1002252ca428SRobert Watson } 1003252ca428SRobert Watson 1004c488362eSRobert Watson #ifdef MAC 10058501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 100630d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 1007302ce8d6SAndre Oppermann goto dropunlock; 1008c488362eSRobert Watson #endif 1009a557af22SRobert Watson so = inp->inp_socket; 1010302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 1011610ee2f9SDavid Greenman #ifdef TCPDEBUG 1012df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 1013df8bae1dSRodney W. Grimes ostate = tp->t_state; 101410fe523eSMaxim Konovalov #ifdef INET6 10156f697424SBjoern A. Zeeb if (isipv6) { 1016540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 1017d30d90dcSMaxim Konovalov } else 10186f697424SBjoern A. Zeeb #endif 1019540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 1020fb59c426SYoshinobu Inoue tcp_savetcp = *th; 1021df8bae1dSRodney W. Grimes } 1022b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */ 1023db33b3e6SAndre Oppermann /* 1024db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 1025db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 1026a7c7f2a7SJohn Baldwin * attempt or the completion of a previous one. 1027db33b3e6SAndre Oppermann */ 102845274760SJonathan T. Looney KASSERT(tp->t_state == TCPS_LISTEN || !(so->so_options & SO_ACCEPTCONN), 102945274760SJonathan T. Looney ("%s: so accepting but tp %p not listening", __func__, tp)); 103045274760SJonathan T. Looney if (tp->t_state == TCPS_LISTEN && (so->so_options & SO_ACCEPTCONN)) { 1031540e8b7eSJeffrey Hsu struct in_conninfo inc; 1032540e8b7eSJeffrey Hsu 10338bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 1034302ce8d6SAndre Oppermann #ifdef INET6 1035be2ac88cSJonathan Lemon if (isipv6) { 1036dcdb4371SBjoern A. Zeeb inc.inc_flags |= INC_ISIPV6; 10375dff1c38SMichael Tuexen if (inp->inp_inc.inc_flags & INC_IPV6MINMTU) 10385dff1c38SMichael Tuexen inc.inc_flags |= INC_IPV6MINMTU; 1039be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 1040be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 1041302ce8d6SAndre Oppermann } else 1042302ce8d6SAndre Oppermann #endif 1043302ce8d6SAndre Oppermann { 1044be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 1045be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 1046be2ac88cSJonathan Lemon } 1047be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 1048be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 10497973fba3SJulian Elischer inc.inc_fibnum = so->so_fibnum; 1050be2ac88cSJonathan Lemon 1051fb59c426SYoshinobu Inoue /* 10528d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 10538d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 10548d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 1055fb59c426SYoshinobu Inoue */ 1056be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 1057a7c7f2a7SJohn Baldwin 1058ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1059bf6d304aSAndre Oppermann /* 1060bf6d304aSAndre Oppermann * Parse the TCP options here because 1061bf6d304aSAndre Oppermann * syncookies need access to the reflected 1062bf6d304aSAndre Oppermann * timestamp. 1063bf6d304aSAndre Oppermann */ 1064bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 10659fa198beSAndre Oppermann /* 10669fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 10679fa198beSAndre Oppermann * inp and tcpinfo locks. 10689fa198beSAndre Oppermann */ 1069fcf59617SAndrey V. Elsukov rstreason = syncache_expand(&inc, &to, th, &so, m); 1070fcf59617SAndrey V. Elsukov if (rstreason < 0) { 1071fcf59617SAndrey V. Elsukov /* 1072fcf59617SAndrey V. Elsukov * A failing TCP MD5 signature comparison 1073fcf59617SAndrey V. Elsukov * must result in the segment being dropped 1074fcf59617SAndrey V. Elsukov * and must not produce any response back 1075fcf59617SAndrey V. Elsukov * to the sender. 1076fcf59617SAndrey V. Elsukov */ 1077fcf59617SAndrey V. Elsukov goto dropunlock; 1078fcf59617SAndrey V. Elsukov } else if (rstreason == 0) { 10794195b4afSPaul Traina /* 1080e207f800SAndre Oppermann * No syncache entry or ACK was not 1081be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 10828d573cc1SAndre Oppermann * NB: syncache did its own logging 10838d573cc1SAndre Oppermann * of the failure cause. 10844195b4afSPaul Traina */ 1085be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 1086be2ac88cSJonathan Lemon goto dropwithreset; 1087be2ac88cSJonathan Lemon } 108809c305ebSPatrick Kelsey tfo_socket_result: 1089f76fcf6dSJeffrey Hsu if (so == NULL) { 1090be2ac88cSJonathan Lemon /* 1091e207f800SAndre Oppermann * We completed the 3-way handshake 1092e207f800SAndre Oppermann * but could not allocate a socket 1093e207f800SAndre Oppermann * either due to memory shortage, 1094e207f800SAndre Oppermann * listen queue length limits or 1095a160e630SAndre Oppermann * global socket limits. Send RST 1096a160e630SAndre Oppermann * or wait and have the remote end 1097a160e630SAndre Oppermann * retransmit the ACK for another 1098a160e630SAndre Oppermann * try. 1099be2ac88cSJonathan Lemon */ 1100a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1101a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11028d573cc1SAndre Oppermann "Socket allocation failed due to " 11038d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 1104ac957cd2SJulian Elischer s, __func__, 1105ac957cd2SJulian Elischer V_tcp_sc_rst_sock_fail ? 1106ac957cd2SJulian Elischer "sending RST" : "try again"); 1107603724d3SBjoern A. Zeeb if (V_tcp_sc_rst_sock_fail) { 1108e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 1109e207f800SAndre Oppermann goto dropwithreset; 1110a160e630SAndre Oppermann } else 1111a160e630SAndre Oppermann goto dropunlock; 1112f76fcf6dSJeffrey Hsu } 1113be2ac88cSJonathan Lemon /* 1114be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 11158d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 11168d573cc1SAndre Oppermann * created socket and update the tp variable. 1117be2ac88cSJonathan Lemon */ 11188501a69cSRobert Watson INP_WUNLOCK(inp); /* listen socket */ 1119be2ac88cSJonathan Lemon inp = sotoinpcb(so); 1120ff9b006dSJulien Charbon /* 1121ff9b006dSJulien Charbon * New connection inpcb is already locked by 1122ff9b006dSJulien Charbon * syncache_expand(). 1123ff9b006dSJulien Charbon */ 1124ff9b006dSJulien Charbon INP_WLOCK_ASSERT(inp); 1125be2ac88cSJonathan Lemon tp = intotcpcb(inp); 11268d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 11278d573cc1SAndre Oppermann ("%s: ", __func__)); 1128be2ac88cSJonathan Lemon /* 1129302ce8d6SAndre Oppermann * Process the segment and the data it 1130302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 1131302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 1132302ce8d6SAndre Oppermann */ 11336138da62SMichael Tuexen TCP_PROBE5(receive, NULL, tp, m, tp, th); 113455bceb1eSRandall Stewart tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 11356573d758SMatt Macy iptos); 11366573d758SMatt Macy if (ti_locked == TI_RLOCKED) 11376573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 11388f5a8818SKevin Lo return (IPPROTO_DONE); 1139be2ac88cSJonathan Lemon } 1140a160e630SAndre Oppermann /* 11418d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 11428d573cc1SAndre Oppermann * 1143a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 1144a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 1145a160e630SAndre Oppermann * retransmits. 114619bc77c5SAndre Oppermann * 114719bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 114819bc77c5SAndre Oppermann * causes. 1149a160e630SAndre Oppermann */ 1150a160e630SAndre Oppermann if (thflags & TH_RST) { 115193899d10SMichael Tuexen syncache_chkrst(&inc, th, m); 1152a160e630SAndre Oppermann goto dropunlock; 1153a160e630SAndre Oppermann } 1154a160e630SAndre Oppermann /* 1155a160e630SAndre Oppermann * We can't do anything without SYN. 1156a160e630SAndre Oppermann */ 1157a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 1158a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1159a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1160773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 11618d573cc1SAndre Oppermann s, __func__); 116278b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1163a160e630SAndre Oppermann goto dropunlock; 1164a160e630SAndre Oppermann } 1165a160e630SAndre Oppermann /* 1166a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 1167a160e630SAndre Oppermann */ 1168fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1169a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1170a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11718d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 11728d573cc1SAndre Oppermann s, __func__); 1173a160e630SAndre Oppermann syncache_badack(&inc); /* XXX: Not needed! */ 117478b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1175a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1176a57815efSBosko Milekic goto dropwithreset; 11774195b4afSPaul Traina } 1178a160e630SAndre Oppermann /* 1179a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 1180a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 1181a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 1182a160e630SAndre Oppermann * TCP/IP stack. 1183a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 1184a160e630SAndre Oppermann * and is constantly refining its stack detection 1185a160e630SAndre Oppermann * strategies. 11868d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 1187a160e630SAndre Oppermann * and was used by RFC1644. 1188a160e630SAndre Oppermann */ 1189603724d3SBjoern A. Zeeb if ((thflags & TH_FIN) && V_drop_synfin) { 1190a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1191a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1192773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 11938d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 119478b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1195302ce8d6SAndre Oppermann goto dropunlock; 1196ebb0cbeaSPaul Traina } 1197be2ac88cSJonathan Lemon /* 1198be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 1199a160e630SAndre Oppermann * 1200a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 1201a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 1202a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 1203be2ac88cSJonathan Lemon */ 1204a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 1205a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 1206a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 1207a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 120833841545SHajimu UMEMOTO #ifdef INET6 120933841545SHajimu UMEMOTO /* 121033841545SHajimu UMEMOTO * If deprecated address is forbidden, 121133841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 121233841545SHajimu UMEMOTO * address to prevent any new inbound connection from 121333841545SHajimu UMEMOTO * getting established. 121433841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 121533841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 121633841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 121733841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 121833841545SHajimu UMEMOTO * for the exchange. 121933841545SHajimu UMEMOTO * 122033841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 122133841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 122233841545SHajimu UMEMOTO * SYN in this case. 122333841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 122433841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 122533841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 122633841545SHajimu UMEMOTO * used" 122733841545SHajimu UMEMOTO * 2. use of it with new communication: 122833841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 122933841545SHajimu UMEMOTO * with sufficient scope is available" 123033841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 123133841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 123233841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 123333841545SHajimu UMEMOTO * 123433841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 123533841545SHajimu UMEMOTO * multiple description text for deprecated address 123633841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 123733841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 123833841545SHajimu UMEMOTO */ 1239603724d3SBjoern A. Zeeb if (isipv6 && !V_ip6_use_deprecated) { 124033841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 124133841545SHajimu UMEMOTO 12423e88eb90SAndrey V. Elsukov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */); 12438c0fec80SRobert Watson if (ia6 != NULL && 124433841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 12458c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 1246a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1247a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12488d573cc1SAndre Oppermann "Connection attempt to deprecated " 12498d573cc1SAndre Oppermann "IPv6 address rejected\n", 1250a160e630SAndre Oppermann s, __func__); 125133841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 125233841545SHajimu UMEMOTO goto dropwithreset; 125333841545SHajimu UMEMOTO } 125477d396fdSMaksim Yevmenkin if (ia6) 12558c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 125633841545SHajimu UMEMOTO } 1257b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 125865f28919SJesper Skriver /* 1259db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 12608d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 1261db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 12628d573cc1SAndre Oppermann * If it is from this socket it must be forged. 12638d573cc1SAndre Oppermann * Don't respond if the source or destination is a 12648d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 126593ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 126693ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 126793ec91baSCrist J. Clark * in_broadcast() to find them. 1268be2ac88cSJonathan Lemon */ 12698d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 12708d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 12718d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12728d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 1273773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 1274302ce8d6SAndre Oppermann goto dropunlock; 12758d573cc1SAndre Oppermann } 1276db33b3e6SAndre Oppermann #ifdef INET6 1277b287c6c7SBjoern A. Zeeb if (isipv6) { 1278db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1279a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 1280a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1281a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12828d573cc1SAndre Oppermann "Connection attempt to/from self " 1283773673c1SAndre Oppermann "ignored\n", s, __func__); 1284302ce8d6SAndre Oppermann goto dropunlock; 1285a160e630SAndre Oppermann } 1286be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1287a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 1288a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1289a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12908d573cc1SAndre Oppermann "Connection attempt from/to multicast " 1291773673c1SAndre Oppermann "address ignored\n", s, __func__); 1292302ce8d6SAndre Oppermann goto dropunlock; 1293a160e630SAndre Oppermann } 1294b287c6c7SBjoern A. Zeeb } 1295db33b3e6SAndre Oppermann #endif 1296b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 1297b287c6c7SBjoern A. Zeeb else 1298b287c6c7SBjoern A. Zeeb #endif 1299b287c6c7SBjoern A. Zeeb #ifdef INET 1300b287c6c7SBjoern A. Zeeb { 1301db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1302a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 1303a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1304a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13058d573cc1SAndre Oppermann "Connection attempt from/to self " 1306773673c1SAndre Oppermann "ignored\n", s, __func__); 1307302ce8d6SAndre Oppermann goto dropunlock; 1308a160e630SAndre Oppermann } 1309be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1310be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 13112ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 1312a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 1313a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1314a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13158d573cc1SAndre Oppermann "Connection attempt from/to broad- " 1316773673c1SAndre Oppermann "or multicast address ignored\n", 1317a160e630SAndre Oppermann s, __func__); 1318302ce8d6SAndre Oppermann goto dropunlock; 1319c068736aSJeffrey Hsu } 1320a160e630SAndre Oppermann } 1321b287c6c7SBjoern A. Zeeb #endif 1322be2ac88cSJonathan Lemon /* 1323db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 1324db33b3e6SAndre Oppermann * for syncache. 1325be2ac88cSJonathan Lemon */ 13263c653157SHartmut Brandt #ifdef TCPDEBUG 13273c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 13283c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 13293c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 13303c653157SHartmut Brandt #endif 13312b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 1332f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 1333281a0fd4SPatrick Kelsey if (syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL)) 133409c305ebSPatrick Kelsey goto tfo_socket_result; 133518a75309SPatrick Kelsey 1336be2ac88cSJonathan Lemon /* 13374d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 1338a7c7f2a7SJohn Baldwin * Only the listen socket is unlocked by syncache_add(). 1339be2ac88cSJonathan Lemon */ 1340ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 13416573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 1342a7c7f2a7SJohn Baldwin ti_locked = TI_UNLOCKED; 1343a7c7f2a7SJohn Baldwin } 1344384a5c3cSAndrey V. Elsukov INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo); 13458f5a8818SKevin Lo return (IPPROTO_DONE); 1346982c1675SAndre Oppermann } else if (tp->t_state == TCPS_LISTEN) { 1347982c1675SAndre Oppermann /* 1348982c1675SAndre Oppermann * When a listen socket is torn down the SO_ACCEPTCONN 1349982c1675SAndre Oppermann * flag is removed first while connections are drained 1350982c1675SAndre Oppermann * from the accept queue in a unlock/lock cycle of the 1351982c1675SAndre Oppermann * ACCEPT_LOCK, opening a race condition allowing a SYN 1352982c1675SAndre Oppermann * attempt go through unhandled. 1353982c1675SAndre Oppermann */ 1354982c1675SAndre Oppermann goto dropunlock; 1355f76fcf6dSJeffrey Hsu } 1356fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1357fcf59617SAndrey V. Elsukov if (tp->t_flags & TF_SIGNATURE) { 1358fcf59617SAndrey V. Elsukov tcp_dooptions(&to, optp, optlen, thflags); 1359fcf59617SAndrey V. Elsukov if ((to.to_flags & TOF_SIGNATURE) == 0) { 1360fcf59617SAndrey V. Elsukov TCPSTAT_INC(tcps_sig_err_nosigopt); 13612903309aSAttilio Rao goto dropunlock; 13622903309aSAttilio Rao } 1363fcf59617SAndrey V. Elsukov if (!TCPMD5_ENABLED() || 1364fcf59617SAndrey V. Elsukov TCPMD5_INPUT(m, th, to.to_signature) != 0) 1365fcf59617SAndrey V. Elsukov goto dropunlock; 13662903309aSAttilio Rao } 13672903309aSAttilio Rao #endif 13682b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 136957f60867SMark Johnston 1370302ce8d6SAndre Oppermann /* 13718d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 13728d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 13738d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 1374302ce8d6SAndre Oppermann */ 13756573d758SMatt Macy tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos); 13766573d758SMatt Macy if (ti_locked == TI_RLOCKED) 13776573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 13788f5a8818SKevin Lo return (IPPROTO_DONE); 1379be2ac88cSJonathan Lemon 1380302ce8d6SAndre Oppermann dropwithreset: 13812b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 138257f60867SMark Johnston 1383ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 13846573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 1385252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1386fa046d87SRobert Watson } 1387fa046d87SRobert Watson #ifdef INVARIANTS 1388fa046d87SRobert Watson else { 1389fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset " 1390fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1391384a5c3cSAndrey V. Elsukov INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo); 1392fa046d87SRobert Watson } 1393fa046d87SRobert Watson #endif 1394014ea782SRobert Watson 1395014ea782SRobert Watson if (inp != NULL) { 1396302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 1397014ea782SRobert Watson INP_WUNLOCK(inp); 1398dd8ac7f9SRobert Watson } else 1399014ea782SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1400302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 1401014ea782SRobert Watson goto drop; 1402014ea782SRobert Watson 1403302ce8d6SAndre Oppermann dropunlock: 140457f60867SMark Johnston if (m != NULL) 14052b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 140657f60867SMark Johnston 1407ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 14086573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 1409252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1410fa046d87SRobert Watson } 1411fa046d87SRobert Watson #ifdef INVARIANTS 1412fa046d87SRobert Watson else { 1413fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock " 1414fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1415384a5c3cSAndrey V. Elsukov INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo); 1416fa046d87SRobert Watson } 1417fa046d87SRobert Watson #endif 1418252ca428SRobert Watson 14199fa198beSAndre Oppermann if (inp != NULL) 14208501a69cSRobert Watson INP_WUNLOCK(inp); 1421014ea782SRobert Watson 1422302ce8d6SAndre Oppermann drop: 1423384a5c3cSAndrey V. Elsukov INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo); 1424a160e630SAndre Oppermann if (s != NULL) 1425a160e630SAndre Oppermann free(s, M_TCPLOG); 1426302ce8d6SAndre Oppermann if (m != NULL) 1427302ce8d6SAndre Oppermann m_freem(m); 14288f5a8818SKevin Lo return (IPPROTO_DONE); 1429302ce8d6SAndre Oppermann } 1430302ce8d6SAndre Oppermann 1431e44c1887SSteven Hartland /* 1432e44c1887SSteven Hartland * Automatic sizing of receive socket buffer. Often the send 1433e44c1887SSteven Hartland * buffer size is not optimally adjusted to the actual network 1434e44c1887SSteven Hartland * conditions at hand (delay bandwidth product). Setting the 1435e44c1887SSteven Hartland * buffer size too small limits throughput on links with high 1436e44c1887SSteven Hartland * bandwidth and high delay (eg. trans-continental/oceanic links). 1437e44c1887SSteven Hartland * 1438e44c1887SSteven Hartland * On the receive side the socket buffer memory is only rarely 1439e44c1887SSteven Hartland * used to any significant extent. This allows us to be much 1440e44c1887SSteven Hartland * more aggressive in scaling the receive socket buffer. For 1441e44c1887SSteven Hartland * the case that the buffer space is actually used to a large 1442e44c1887SSteven Hartland * extent and we run out of kernel memory we can simply drop 1443e44c1887SSteven Hartland * the new segments; TCP on the sender will just retransmit it 1444e44c1887SSteven Hartland * later. Setting the buffer size too big may only consume too 1445e44c1887SSteven Hartland * much kernel memory if the application doesn't read() from 1446e44c1887SSteven Hartland * the socket or packet loss or reordering makes use of the 1447e44c1887SSteven Hartland * reassembly queue. 1448e44c1887SSteven Hartland * 1449e44c1887SSteven Hartland * The criteria to step up the receive buffer one notch are: 1450e44c1887SSteven Hartland * 1. Application has not set receive buffer size with 1451e44c1887SSteven Hartland * SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE. 1452e44c1887SSteven Hartland * 2. the number of bytes received during the time it takes 1453e44c1887SSteven Hartland * one timestamp to be reflected back to us (the RTT); 1454e44c1887SSteven Hartland * 3. received bytes per RTT is within seven eighth of the 1455e44c1887SSteven Hartland * current socket buffer size; 1456e44c1887SSteven Hartland * 4. receive buffer size has not hit maximal automatic size; 1457e44c1887SSteven Hartland * 1458e44c1887SSteven Hartland * This algorithm does one step per RTT at most and only if 1459e44c1887SSteven Hartland * we receive a bulk stream w/o packet losses or reorderings. 1460e44c1887SSteven Hartland * Shrinking the buffer during idle times is not necessary as 1461e44c1887SSteven Hartland * it doesn't consume any memory when idle. 1462e44c1887SSteven Hartland * 1463e44c1887SSteven Hartland * TODO: Only step up if the application is actually serving 1464e44c1887SSteven Hartland * the buffer to better manage the socket buffer resources. 1465e44c1887SSteven Hartland */ 1466e44c1887SSteven Hartland int 1467e44c1887SSteven Hartland tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so, 1468e44c1887SSteven Hartland struct tcpcb *tp, int tlen) 1469e44c1887SSteven Hartland { 1470e44c1887SSteven Hartland int newsize = 0; 1471e44c1887SSteven Hartland 1472e44c1887SSteven Hartland if (V_tcp_do_autorcvbuf && (so->so_rcv.sb_flags & SB_AUTOSIZE) && 1473e44c1887SSteven Hartland tp->t_srtt != 0 && tp->rfbuf_ts != 0 && 1474e44c1887SSteven Hartland TCP_TS_TO_TICKS(tcp_ts_getticks() - tp->rfbuf_ts) > 1475e44c1887SSteven Hartland (tp->t_srtt >> TCP_RTT_SHIFT)) { 1476e44c1887SSteven Hartland if (tp->rfbuf_cnt > (so->so_rcv.sb_hiwat / 8 * 7) && 1477e44c1887SSteven Hartland so->so_rcv.sb_hiwat < V_tcp_autorcvbuf_max) { 1478e44c1887SSteven Hartland newsize = min(so->so_rcv.sb_hiwat + 1479e44c1887SSteven Hartland V_tcp_autorcvbuf_inc, V_tcp_autorcvbuf_max); 1480e44c1887SSteven Hartland } 1481e44c1887SSteven Hartland TCP_PROBE6(receive__autoresize, NULL, tp, m, tp, th, newsize); 1482e44c1887SSteven Hartland 1483e44c1887SSteven Hartland /* Start over with next RTT. */ 1484e44c1887SSteven Hartland tp->rfbuf_ts = 0; 1485e44c1887SSteven Hartland tp->rfbuf_cnt = 0; 1486e44c1887SSteven Hartland } else { 1487e44c1887SSteven Hartland tp->rfbuf_cnt += tlen; /* add up */ 1488e44c1887SSteven Hartland } 1489e44c1887SSteven Hartland 1490e44c1887SSteven Hartland return (newsize); 1491e44c1887SSteven Hartland } 1492e44c1887SSteven Hartland 149355bceb1eSRandall Stewart void 1494302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 14956573d758SMatt Macy struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos) 1496302ce8d6SAndre Oppermann { 1497021eaf79SHiren Panchasara int thflags, acked, ourfinisacked, needoutput = 0, sack_changed; 1498302ce8d6SAndre Oppermann int rstreason, todrop, win; 14993ac12506SJonathan T. Looney uint32_t tiwin; 15004b7b743cSLawrence Stewart uint16_t nsegs; 150107dacf03SAndre Oppermann char *s; 150207dacf03SAndre Oppermann struct in_conninfo *inc; 1503c11a15bfSGleb Smirnoff struct mbuf *mfree; 1504302ce8d6SAndre Oppermann struct tcpopt to; 1505281a0fd4SPatrick Kelsey int tfo_syn; 1506302ce8d6SAndre Oppermann 150714739780SMaxim Konovalov #ifdef TCPDEBUG 150814739780SMaxim Konovalov /* 150914739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 151014739780SMaxim Konovalov * now IPv6. 151114739780SMaxim Konovalov */ 151214739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 151314739780SMaxim Konovalov struct tcphdr tcp_savetcp; 151414739780SMaxim Konovalov short ostate = 0; 151514739780SMaxim Konovalov #endif 1516302ce8d6SAndre Oppermann thflags = th->th_flags; 151707dacf03SAndre Oppermann inc = &tp->t_inpcb->inp_inc; 1518d64a46eaSLawrence Stewart tp->sackhint.last_sack_ack = 0; 1519021eaf79SHiren Panchasara sack_changed = 0; 15204b7b743cSLawrence Stewart nsegs = max(1, m->m_pkthdr.lro_nsegs); 1521252ca428SRobert Watson /* 1522252ca428SRobert Watson * If this is either a state-changing packet or current state isn't 1523252ca428SRobert Watson * established, we require a write lock on tcbinfo. Otherwise, we 15240989f56cSRobert Watson * allow the tcbinfo to be in either alocked or unlocked, as the 15250989f56cSRobert Watson * caller may have unnecessarily acquired a write lock due to a race. 1526252ca428SRobert Watson */ 1527252ca428SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 1528252ca428SRobert Watson tp->t_state != TCPS_ESTABLISHED) { 1529ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1530252ca428SRobert Watson } 15318501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1532679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1533679d9708SAndre Oppermann __func__)); 1534679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1535679d9708SAndre Oppermann __func__)); 1536df8bae1dSRodney W. Grimes 153786a996e6SHiren Panchasara #ifdef TCPPCAP 153886a996e6SHiren Panchasara /* Save segment, if requested. */ 153986a996e6SHiren Panchasara tcp_pcap_add(th, m, &(tp->t_inpkts)); 154086a996e6SHiren Panchasara #endif 15412529f56eSJonathan T. Looney TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0, 15422529f56eSJonathan T. Looney tlen, NULL, true); 154386a996e6SHiren Panchasara 1544013f4df6SMichael Tuexen if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) { 1545013f4df6SMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 1546013f4df6SMichael Tuexen log(LOG_DEBUG, "%s; %s: " 1547013f4df6SMichael Tuexen "SYN|FIN segment ignored (based on " 1548013f4df6SMichael Tuexen "sysctl setting)\n", s, __func__); 1549013f4df6SMichael Tuexen free(s, M_TCPLOG); 1550013f4df6SMichael Tuexen } 1551013f4df6SMichael Tuexen goto drop; 1552013f4df6SMichael Tuexen } 1553013f4df6SMichael Tuexen 1554df8bae1dSRodney W. Grimes /* 1555ebfd7534SMichael Tuexen * If a segment with the ACK-bit set arrives in the SYN-SENT state 1556ebfd7534SMichael Tuexen * check SEQ.ACK first. 1557ebfd7534SMichael Tuexen */ 1558ebfd7534SMichael Tuexen if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) && 1559ebfd7534SMichael Tuexen (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) { 1560ebfd7534SMichael Tuexen rstreason = BANDLIM_UNLIMITED; 1561ebfd7534SMichael Tuexen goto dropwithreset; 1562ebfd7534SMichael Tuexen } 1563ebfd7534SMichael Tuexen 1564ebfd7534SMichael Tuexen /* 1565df8bae1dSRodney W. Grimes * Segment received on connection. 1566df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1567e8949f74SAndre Oppermann * XXX: This should be done after segment 1568e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 1569df8bae1dSRodney W. Grimes */ 15709b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 1571df8bae1dSRodney W. Grimes 1572df8bae1dSRodney W. Grimes /* 1573c52102ddSHiren Panchasara * Scale up the window into a 32-bit value. 1574e8949f74SAndre Oppermann * For the SYN_SENT state the scale is zero. 1575464fcfbcSAndre Oppermann */ 1576464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 1577464fcfbcSAndre Oppermann 1578464fcfbcSAndre Oppermann /* 1579f2512ba1SRui Paulo * TCP ECN processing. 1580f2512ba1SRui Paulo */ 1581f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) { 15829c251892SRui Paulo if (thflags & TH_CWR) 15839c251892SRui Paulo tp->t_flags &= ~TF_ECN_SND_ECE; 1584f2512ba1SRui Paulo switch (iptos & IPTOS_ECN_MASK) { 1585f2512ba1SRui Paulo case IPTOS_ECN_CE: 1586f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_ECE; 158778b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ce); 1588f2512ba1SRui Paulo break; 1589f2512ba1SRui Paulo case IPTOS_ECN_ECT0: 159078b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect0); 1591f2512ba1SRui Paulo break; 1592f2512ba1SRui Paulo case IPTOS_ECN_ECT1: 159378b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect1); 1594f2512ba1SRui Paulo break; 1595f2512ba1SRui Paulo } 159664807b30SHiren Panchasara 159764807b30SHiren Panchasara /* Process a packet differently from RFC3168. */ 159864807b30SHiren Panchasara cc_ecnpkt_handler(tp, th, iptos); 159964807b30SHiren Panchasara 1600dbc42409SLawrence Stewart /* Congestion experienced. */ 1601dbc42409SLawrence Stewart if (thflags & TH_ECE) { 1602dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_ECN); 1603f2512ba1SRui Paulo } 1604f2512ba1SRui Paulo } 1605f2512ba1SRui Paulo 1606f2512ba1SRui Paulo /* 1607f72167f4SAndre Oppermann * Parse options on any incoming segment. 1608f72167f4SAndre Oppermann */ 1609302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1610302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1611302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1612f72167f4SAndre Oppermann 1613fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1614fcf59617SAndrey V. Elsukov if ((tp->t_flags & TF_SIGNATURE) != 0 && 1615fcf59617SAndrey V. Elsukov (to.to_flags & TOF_SIGNATURE) == 0) { 1616fcf59617SAndrey V. Elsukov TCPSTAT_INC(tcps_sig_err_sigopt); 1617fcf59617SAndrey V. Elsukov /* XXX: should drop? */ 1618fcf59617SAndrey V. Elsukov } 1619fcf59617SAndrey V. Elsukov #endif 1620f72167f4SAndre Oppermann /* 1621f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1622bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1623bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1624bf6d304aSAndre Oppermann * was established. 1625f72167f4SAndre Oppermann */ 1626bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1627e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 1628d8951c8aSBjoern A. Zeeb if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) 1629f72167f4SAndre Oppermann to.to_tsecr = 0; 163010d20c84SMatt Macy else if (tp->t_flags & TF_PREVVALID && 163110d20c84SMatt Macy tp->t_badrxtwin != 0 && SEQ_LT(to.to_tsecr, tp->t_badrxtwin)) 163210d20c84SMatt Macy cc_cong_signal(tp, th, CC_RTO_ERR); 1633bf6d304aSAndre Oppermann } 163407dacf03SAndre Oppermann /* 163597d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 163697d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 16375396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 16385396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 163997d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1640df8bae1dSRodney W. Grimes */ 16410a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1642464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 1643464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 1644be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 164502a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 1646be2ac88cSJonathan Lemon } 16475396d0f8SAndre Oppermann /* 16485396d0f8SAndre Oppermann * Initial send window. It will be updated with 16495396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 16505396d0f8SAndre Oppermann */ 16515396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 1652be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1653be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1654be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1655d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1656be2ac88cSJonathan Lemon } 1657be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1658be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 16593529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 16603529149eSAndre Oppermann (to.to_flags & TOF_SACKPERM) == 0) 16613529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 1662c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) { 1663a026a53aSMichael Tuexen if (to.to_flags & TOF_FASTOPEN) { 1664a026a53aSMichael Tuexen uint16_t mss; 1665a026a53aSMichael Tuexen 1666a026a53aSMichael Tuexen if (to.to_flags & TOF_MSS) 1667a026a53aSMichael Tuexen mss = to.to_mss; 1668c560df6fSPatrick Kelsey else 1669a026a53aSMichael Tuexen if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) 1670a026a53aSMichael Tuexen mss = TCP6_MSS; 1671a026a53aSMichael Tuexen else 1672a026a53aSMichael Tuexen mss = TCP_MSS; 1673a026a53aSMichael Tuexen tcp_fastopen_update_cache(tp, mss, 1674a026a53aSMichael Tuexen to.to_tfo_len, to.to_tfo_cookie); 1675a026a53aSMichael Tuexen } else 1676c560df6fSPatrick Kelsey tcp_fastopen_disable_path(tp); 1677c560df6fSPatrick Kelsey } 16786d90faf3SPaul Saab } 16796d90faf3SPaul Saab 1680df8bae1dSRodney W. Grimes /* 16814da9052aSMichael Tuexen * If timestamps were negotiated during SYN/ACK they should 16824da9052aSMichael Tuexen * appear on every segment during this session and vice versa. 16834da9052aSMichael Tuexen */ 16844da9052aSMichael Tuexen if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) { 16854da9052aSMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 16864da9052aSMichael Tuexen log(LOG_DEBUG, "%s; %s: Timestamp missing, " 16874da9052aSMichael Tuexen "no action\n", s, __func__); 16884da9052aSMichael Tuexen free(s, M_TCPLOG); 16894da9052aSMichael Tuexen } 16904da9052aSMichael Tuexen } 16914da9052aSMichael Tuexen if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) { 16924da9052aSMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 16934da9052aSMichael Tuexen log(LOG_DEBUG, "%s; %s: Timestamp not expected, " 16944da9052aSMichael Tuexen "no action\n", s, __func__); 16954da9052aSMichael Tuexen free(s, M_TCPLOG); 16964da9052aSMichael Tuexen } 16974da9052aSMichael Tuexen } 16984da9052aSMichael Tuexen 16994da9052aSMichael Tuexen /* 1700df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1701df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1702df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1703df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1704df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1705df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1706df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1707df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1708df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1709df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1710df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1711df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1712a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1713c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 1714a0292f23SGarrett Wollman * be TH_NEEDSYN. 1715df8bae1dSRodney W. Grimes */ 1716df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1717c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 1718fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1719c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 1720c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 1721a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1722c28440dbSRandall Stewart SEGQ_EMPTY(tp) && 17234741bfcbSPatrick Kelsey ((to.to_flags & TOF_TS) == 0 || 1724c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1725df8bae1dSRodney W. Grimes 1726df8bae1dSRodney W. Grimes /* 1727df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1728df8bae1dSRodney W. Grimes * record the timestamp. 1729a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1730a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1731df8bae1dSRodney W. Grimes */ 1732be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1733fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1734d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1735a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1736df8bae1dSRodney W. Grimes } 1737df8bae1dSRodney W. Grimes 1738fb59c426SYoshinobu Inoue if (tlen == 0) { 1739fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1740fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1741dbc42409SLawrence Stewart !IN_RECOVERY(tp->t_flags) && 1742fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1743dbc42409SLawrence Stewart TAILQ_EMPTY(&tp->snd_holes)) { 1744df8bae1dSRodney W. Grimes /* 1745e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1746df8bae1dSRodney W. Grimes */ 174778b50714SRobert Watson TCPSTAT_INC(tcps_predack); 1748252ca428SRobert Watson 17499b8b58e0SJonathan Lemon /* 175010d20c84SMatt Macy * "bad retransmit" recovery without timestamps. 17519b8b58e0SJonathan Lemon */ 175210d20c84SMatt Macy if ((to.to_flags & TOF_TS) == 0 && 175310d20c84SMatt Macy tp->t_rxtshift == 1 && 1754672dc4aeSJohn Baldwin tp->t_flags & TF_PREVVALID && 17556dfb8b31SJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) { 1756dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 17579b8b58e0SJonathan Lemon } 1758fa55172bSMatthew Dillon 1759fa55172bSMatthew Dillon /* 1760fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1761fa55172bSMatthew Dillon * 1762fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1763fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1764fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1765fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1766fa55172bSMatthew Dillon */ 1767fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1768fa55172bSMatthew Dillon to.to_tsecr) { 17693ac12506SJonathan T. Looney uint32_t t; 1770d8951c8aSBjoern A. Zeeb 1771d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 1772d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 1773d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 1774a0292f23SGarrett Wollman tcp_xmit_timer(tp, 1775d8951c8aSBjoern A. Zeeb TCP_TS_TO_TICKS(t) + 1); 1776fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1777fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1778eaf80179SAndre Oppermann if (!tp->t_rttlow || 1779eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1780eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1781c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1782c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1783fa55172bSMatthew Dillon } 1784dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 178539bc9de5SLawrence Stewart 1786bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 178739bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 178839bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 1789bd79708dSJonathan T. Looney #endif 179039bc9de5SLawrence Stewart 17914b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvackpack, nsegs); 179278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 1793df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 17949d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 17959d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 17969d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 1797dbc42409SLawrence Stewart 1798dbc42409SLawrence Stewart /* 1799dbc42409SLawrence Stewart * Let the congestion control algorithm update 1800dbc42409SLawrence Stewart * congestion control related information. This 1801dbc42409SLawrence Stewart * typically means increasing the congestion 1802dbc42409SLawrence Stewart * window. 1803dbc42409SLawrence Stewart */ 18044b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, CC_ACK); 1805dbc42409SLawrence Stewart 18069d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 18071645d090SJeff Roberson /* 1808e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 18091645d090SJeff Roberson * to th_ack. 18101645d090SJeff Roberson */ 1811cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1812b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1813df8bae1dSRodney W. Grimes m_freem(m); 1814df8bae1dSRodney W. Grimes 1815df8bae1dSRodney W. Grimes /* 1816df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1817df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1818df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1819df8bae1dSRodney W. Grimes * If process is waiting for space, 1820df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1821df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1822df8bae1dSRodney W. Grimes * decide between more output or persist. 1823e8949f74SAndre Oppermann */ 18243c653157SHartmut Brandt #ifdef TCPDEBUG 18253c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 18263c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 18273c653157SHartmut Brandt (void *)tcp_saveipgen, 18283c653157SHartmut Brandt &tcp_savetcp, 0); 18293c653157SHartmut Brandt #endif 18302b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 1831df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1832b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1833b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1834b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1835b8152ba7SAndre Oppermann tp->t_rxtcur); 1836df8bae1dSRodney W. Grimes sowwakeup(so); 1837cfa6009eSGleb Smirnoff if (sbavail(&so->so_snd)) 183855bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 1839a14c749fSJonathan Lemon goto check_delack; 1840df8bae1dSRodney W. Grimes } 1841fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1842fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 18436741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 18446741ecf5SAndre Oppermann 1845df8bae1dSRodney W. Grimes /* 1846252ca428SRobert Watson * This is a pure, in-sequence data packet with 1847252ca428SRobert Watson * nothing on the reassembly queue and we have enough 1848252ca428SRobert Watson * buffer space to take it. 1849df8bae1dSRodney W. Grimes */ 18506d90faf3SPaul Saab /* Clean receiver SACK report if present */ 18513529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 18526d90faf3SPaul Saab tcp_clean_sackreport(tp); 185378b50714SRobert Watson TCPSTAT_INC(tcps_preddat); 1854fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 18551645d090SJeff Roberson /* 18561645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 18571645d090SJeff Roberson * th_seq. 18581645d090SJeff Roberson */ 185960ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 18601645d090SJeff Roberson /* 18611645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 18621645d090SJeff Roberson * rcv_nxt. 18631645d090SJeff Roberson */ 18641645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 18654b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvpack, nsegs); 186678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 18673c653157SHartmut Brandt #ifdef TCPDEBUG 18683c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 18693c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 18703c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 18713c653157SHartmut Brandt #endif 18722b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 18735d06879aSGeorge V. Neville-Neil 1874e44c1887SSteven Hartland newsize = tcp_autorcvbuf(m, th, so, tp, tlen); 18756741ecf5SAndre Oppermann 18766741ecf5SAndre Oppermann /* Add data to socket buffer. */ 18771e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1878c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1879c1c36a2cSMike Silbersack m_freem(m); 1880c1c36a2cSMike Silbersack } else { 18816741ecf5SAndre Oppermann /* 18826741ecf5SAndre Oppermann * Set new socket buffer size. 18836741ecf5SAndre Oppermann * Give up when limit is reached. 18846741ecf5SAndre Oppermann */ 18856741ecf5SAndre Oppermann if (newsize) 18866741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 18876c8286e4SRobert Watson newsize, so, NULL)) 18886741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1889fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 1890651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 1891c1c36a2cSMike Silbersack } 1892e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 18931e4d7da7SRobert Watson sorwakeup_locked(so); 1894c1e5a6e5SAndre Oppermann if (DELAY_ACK(tp, tlen)) { 18953bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1896f498eeeeSDavid Greenman } else { 1897e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 189855bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 1899e612a582SDavid Greenman } 1900a14c749fSJonathan Lemon goto check_delack; 1901df8bae1dSRodney W. Grimes } 1902df8bae1dSRodney W. Grimes } 1903df8bae1dSRodney W. Grimes 1904df8bae1dSRodney W. Grimes /* 1905df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1906df8bae1dSRodney W. Grimes * and then do TCP input processing. 1907df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1908df8bae1dSRodney W. Grimes * but not less than advertised window. 1909df8bae1dSRodney W. Grimes */ 1910df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1911df8bae1dSRodney W. Grimes if (win < 0) 1912df8bae1dSRodney W. Grimes win = 0; 191366e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1914df8bae1dSRodney W. Grimes 1915df8bae1dSRodney W. Grimes switch (tp->t_state) { 1916df8bae1dSRodney W. Grimes 1917df8bae1dSRodney W. Grimes /* 1918764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1919764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1920764d8cefSBill Fenner */ 1921764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1922fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1923fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1924a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1925a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1926a57815efSBosko Milekic goto dropwithreset; 1927a57815efSBosko Milekic } 192868bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) { 1929281a0fd4SPatrick Kelsey /* 1930281a0fd4SPatrick Kelsey * When a TFO connection is in SYN_RECEIVED, the 1931281a0fd4SPatrick Kelsey * only valid packets are the initial SYN, a 1932281a0fd4SPatrick Kelsey * retransmit/copy of the initial SYN (possibly with 1933281a0fd4SPatrick Kelsey * a subset of the original data), a valid ACK, a 1934281a0fd4SPatrick Kelsey * FIN, or a RST. 1935281a0fd4SPatrick Kelsey */ 1936281a0fd4SPatrick Kelsey if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) { 1937281a0fd4SPatrick Kelsey rstreason = BANDLIM_RST_OPENPORT; 1938281a0fd4SPatrick Kelsey goto dropwithreset; 1939281a0fd4SPatrick Kelsey } else if (thflags & TH_SYN) { 1940281a0fd4SPatrick Kelsey /* non-initial SYN is ignored */ 1941281a0fd4SPatrick Kelsey if ((tcp_timer_active(tp, TT_DELACK) || 1942281a0fd4SPatrick Kelsey tcp_timer_active(tp, TT_REXMT))) 1943281a0fd4SPatrick Kelsey goto drop; 1944281a0fd4SPatrick Kelsey } else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) { 1945281a0fd4SPatrick Kelsey goto drop; 1946281a0fd4SPatrick Kelsey } 1947281a0fd4SPatrick Kelsey } 1948764d8cefSBill Fenner break; 1949764d8cefSBill Fenner 1950764d8cefSBill Fenner /* 1951df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 195298732609SMichael Tuexen * if seg contains a RST with valid ACK (SEQ.ACK has already 195398732609SMichael Tuexen * been verified), then drop the connection. 195498732609SMichael Tuexen * if seg contains a RST without an ACK, drop the seg. 195598732609SMichael Tuexen * if seg does not contain SYN, then drop the seg. 1956df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1957df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1958df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1959f2512ba1SRui Paulo * if seg contains an ECE and ECN support is enabled, the stream 1960f2512ba1SRui Paulo * is ECN capable. 1961df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1962df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1963df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1964df8bae1dSRodney W. Grimes */ 1965df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 196657f60867SMark Johnston if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) { 1967d9fae5abSAndriy Gapon TCP_PROBE5(connect__refused, NULL, tp, 19682b9c9984SGeorge V. Neville-Neil m, tp, th); 1969df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 197057f60867SMark Johnston } 19710ca3f933SAndre Oppermann if (thflags & TH_RST) 1972df8bae1dSRodney W. Grimes goto drop; 19730ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 1974df8bae1dSRodney W. Grimes goto drop; 1975464fcfbcSAndre Oppermann 1976fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1977df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1978fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1979c560df6fSPatrick Kelsey int tfo_partial_ack = 0; 1980c560df6fSPatrick Kelsey 198178b50714SRobert Watson TCPSTAT_INC(tcps_connects); 1982845799c1SAndras Olah soisconnected(so); 1983c488362eSRobert Watson #ifdef MAC 198430d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 1985c488362eSRobert Watson #endif 1986845799c1SAndras Olah /* Do window scaling on this connection? */ 1987845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1988845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1989845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1990845799c1SAndras Olah } 19913ac12506SJonathan T. Looney tp->rcv_adv += min(tp->rcv_wnd, 1992766282cbSJohn Baldwin TCP_MAXWIN << tp->rcv_scale); 1993a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1994a0292f23SGarrett Wollman /* 1995c560df6fSPatrick Kelsey * If not all the data that was sent in the TFO SYN 1996c560df6fSPatrick Kelsey * has been acked, resend the remainder right away. 1997c560df6fSPatrick Kelsey */ 1998c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags) && 1999c560df6fSPatrick Kelsey (tp->snd_una != tp->snd_max)) { 2000c560df6fSPatrick Kelsey tp->snd_nxt = th->th_ack; 2001c560df6fSPatrick Kelsey tfo_partial_ack = 1; 2002c560df6fSPatrick Kelsey } 2003c560df6fSPatrick Kelsey /* 2004a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 2005a0292f23SGarrett Wollman * ACKNOW will be turned on later. 2006a0292f23SGarrett Wollman */ 2007c560df6fSPatrick Kelsey if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial_ack) 2008b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 2009b8152ba7SAndre Oppermann tcp_delacktime); 2010a0292f23SGarrett Wollman else 2011a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2012f2512ba1SRui Paulo 2013603724d3SBjoern A. Zeeb if ((thflags & TH_ECE) && V_tcp_do_ecn) { 2014f2512ba1SRui Paulo tp->t_flags |= TF_ECN_PERMIT; 201578b50714SRobert Watson TCPSTAT_INC(tcps_ecn_shs); 2016f2512ba1SRui Paulo } 2017f2512ba1SRui Paulo 2018a0292f23SGarrett Wollman /* 2019a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 2020a0292f23SGarrett Wollman * Transitions: 2021a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 2022a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 2023a0292f23SGarrett Wollman */ 20249b8b58e0SJonathan Lemon tp->t_starttime = ticks; 2025a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 202657f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2027a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 2028fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 20297ff19458SPaul Traina } else { 203057f60867SMark Johnston tcp_state_change(tp, TCPS_ESTABLISHED); 2031d9fae5abSAndriy Gapon TCP_PROBE5(connect__established, NULL, tp, 20322b9c9984SGeorge V. Neville-Neil m, tp, th); 2033dbc42409SLawrence Stewart cc_conn_init(tp); 20349077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 20359077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 20367ff19458SPaul Traina } 2037a0292f23SGarrett Wollman } else { 2038a0292f23SGarrett Wollman /* 2039c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 2040153edc50SHiren Panchasara * simultaneous open. 2041c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 2042c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 2043a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 2044a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 2045a0292f23SGarrett Wollman */ 20464b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 2047b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 204857f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_RECEIVED); 2049a0292f23SGarrett Wollman } 2050df8bae1dSRodney W. Grimes 2051ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 20528501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 20537cfc6904SRobert Watson 2054df8bae1dSRodney W. Grimes /* 2055fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 2056df8bae1dSRodney W. Grimes * If data, trim to stay within window, 2057df8bae1dSRodney W. Grimes * dropping FIN if necessary. 2058df8bae1dSRodney W. Grimes */ 2059fb59c426SYoshinobu Inoue th->th_seq++; 2060fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 2061fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 2062df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2063fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 2064fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 206578b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 206678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2067df8bae1dSRodney W. Grimes } 2068fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 2069fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 2070a0292f23SGarrett Wollman /* 2071a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 2072a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 2073a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 2074a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 2075a0292f23SGarrett Wollman * Otherwise, goto step 6. 2076a0292f23SGarrett Wollman */ 2077fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 2078a0292f23SGarrett Wollman goto process_ACK; 2079c068736aSJeffrey Hsu 2080df8bae1dSRodney W. Grimes goto step6; 2081c068736aSJeffrey Hsu 2082a0292f23SGarrett Wollman /* 2083a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 2084c94c54e4SAndre Oppermann * do normal processing. 2085a0292f23SGarrett Wollman * 2086c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 2087a0292f23SGarrett Wollman */ 2088a0292f23SGarrett Wollman case TCPS_LAST_ACK: 2089a0292f23SGarrett Wollman case TCPS_CLOSING: 2090a0292f23SGarrett Wollman break; /* continue normal processing */ 2091df8bae1dSRodney W. Grimes } 2092df8bae1dSRodney W. Grimes 2093df8bae1dSRodney W. Grimes /* 2094df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 209580ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 209680ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 209780ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 209880ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 209980ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 210080ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 210180ab7c0eSGarrett Wollman * killing a connection). 210280ab7c0eSGarrett Wollman * Then check timestamp, if present. 2103a0292f23SGarrett Wollman * Then check the connection count, if present. 2104df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 2105df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 2106df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 210780ab7c0eSGarrett Wollman */ 2108fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 21093220a212SGleb Smirnoff /* 21103220a212SGleb Smirnoff * RFC5961 Section 3.2 21113220a212SGleb Smirnoff * 21123220a212SGleb Smirnoff * - RST drops connection only if SEG.SEQ == RCV.NXT. 21133220a212SGleb Smirnoff * - If RST is in window, we send challenge ACK. 21143220a212SGleb Smirnoff * 21153220a212SGleb Smirnoff * Note: to take into account delayed ACKs, we should 21163220a212SGleb Smirnoff * test against last_ack_sent instead of rcv_nxt. 21173220a212SGleb Smirnoff * Note 2: we handle special case of closed window, not 21183220a212SGleb Smirnoff * covered by the RFC. 21193220a212SGleb Smirnoff */ 21203220a212SGleb Smirnoff if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 21213220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) || 21223220a212SGleb Smirnoff (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) { 212380ab7c0eSGarrett Wollman 2124ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 21253220a212SGleb Smirnoff KASSERT(tp->t_state != TCPS_SYN_SENT, 21263220a212SGleb Smirnoff ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p", 21273220a212SGleb Smirnoff __func__, th, tp)); 21283220a212SGleb Smirnoff 21293220a212SGleb Smirnoff if (V_tcp_insecure_rst || 21303220a212SGleb Smirnoff tp->last_ack_sent == th->th_seq) { 21313220a212SGleb Smirnoff TCPSTAT_INC(tcps_drops); 21323220a212SGleb Smirnoff /* Drop the connection. */ 21333220a212SGleb Smirnoff switch (tp->t_state) { 213480ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 213580ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 213680ab7c0eSGarrett Wollman goto close; 213780ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 213880ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 213980ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 214080ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 21416779a1a1SMichael Tuexen case TCPS_CLOSING: 21426779a1a1SMichael Tuexen case TCPS_LAST_ACK: 214380ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 214480ab7c0eSGarrett Wollman close: 21453220a212SGleb Smirnoff /* FALLTHROUGH */ 21463220a212SGleb Smirnoff default: 214780ab7c0eSGarrett Wollman tp = tcp_close(tp); 21483220a212SGleb Smirnoff } 21493220a212SGleb Smirnoff } else { 21503220a212SGleb Smirnoff TCPSTAT_INC(tcps_badrst); 21513220a212SGleb Smirnoff /* Send challenge ACK. */ 21523220a212SGleb Smirnoff tcp_respond(tp, mtod(m, void *), th, m, 21533220a212SGleb Smirnoff tp->rcv_nxt, tp->snd_nxt, TH_ACK); 21543220a212SGleb Smirnoff tp->last_ack_sent = tp->rcv_nxt; 21553220a212SGleb Smirnoff m = NULL; 21563220a212SGleb Smirnoff } 21573220a212SGleb Smirnoff } 21583220a212SGleb Smirnoff goto drop; 21593220a212SGleb Smirnoff } 216080ab7c0eSGarrett Wollman 21613220a212SGleb Smirnoff /* 21623220a212SGleb Smirnoff * RFC5961 Section 4.2 21633220a212SGleb Smirnoff * Send challenge ACK for any SYN in synchronized state. 21643220a212SGleb Smirnoff */ 2165281a0fd4SPatrick Kelsey if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT && 2166281a0fd4SPatrick Kelsey tp->t_state != TCPS_SYN_RECEIVED) { 2167ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2168252ca428SRobert Watson 21693220a212SGleb Smirnoff TCPSTAT_INC(tcps_badsyn); 21703220a212SGleb Smirnoff if (V_tcp_insecure_syn && 21713220a212SGleb Smirnoff SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 21723220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 21733220a212SGleb Smirnoff tp = tcp_drop(tp, ECONNRESET); 21743220a212SGleb Smirnoff rstreason = BANDLIM_UNLIMITED; 21753220a212SGleb Smirnoff } else { 21763220a212SGleb Smirnoff /* Send challenge ACK. */ 21773220a212SGleb Smirnoff tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt, 21783220a212SGleb Smirnoff tp->snd_nxt, TH_ACK); 21793220a212SGleb Smirnoff tp->last_ack_sent = tp->rcv_nxt; 21803220a212SGleb Smirnoff m = NULL; 218180ab7c0eSGarrett Wollman } 218280ab7c0eSGarrett Wollman goto drop; 218380ab7c0eSGarrett Wollman } 218480ab7c0eSGarrett Wollman 218580ab7c0eSGarrett Wollman /* 2186df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 2187df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 2188df8bae1dSRodney W. Grimes */ 2189be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 219080ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 2191df8bae1dSRodney W. Grimes 2192df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 2193d8951c8aSBjoern A. Zeeb if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 2194df8bae1dSRodney W. Grimes /* 2195df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 2196df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 2197df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 2198df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 2199df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 2200df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 2201df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 2202df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 2203df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 2204df8bae1dSRodney W. Grimes */ 2205df8bae1dSRodney W. Grimes tp->ts_recent = 0; 2206df8bae1dSRodney W. Grimes } else { 220778b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 220878b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 220978b50714SRobert Watson TCPSTAT_INC(tcps_pawsdrop); 221007fd333dSMatthew Dillon if (tlen) 2211df8bae1dSRodney W. Grimes goto dropafterack; 22121ab4789dSMatthew Dillon goto drop; 22131ab4789dSMatthew Dillon } 2214df8bae1dSRodney W. Grimes } 2215df8bae1dSRodney W. Grimes 2216a0292f23SGarrett Wollman /* 221780ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 221880ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 221980ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 222080ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 222180ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 222280ab7c0eSGarrett Wollman */ 2223a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 2224a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2225a57815efSBosko Milekic goto dropwithreset; 2226a57815efSBosko Milekic } 222780ab7c0eSGarrett Wollman 2228fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 2229df8bae1dSRodney W. Grimes if (todrop > 0) { 2230831ad37eSXin LI if (thflags & TH_SYN) { 2231fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 2232fb59c426SYoshinobu Inoue th->th_seq++; 2233fb59c426SYoshinobu Inoue if (th->th_urp > 1) 2234fb59c426SYoshinobu Inoue th->th_urp--; 2235df8bae1dSRodney W. Grimes else 2236fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2237df8bae1dSRodney W. Grimes todrop--; 2238df8bae1dSRodney W. Grimes } 2239df8bae1dSRodney W. Grimes /* 2240dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 2241df8bae1dSRodney W. Grimes */ 2242fb59c426SYoshinobu Inoue if (todrop > tlen 2243fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 2244dac20301SGarrett Wollman /* 2245dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 2246dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 2247dac20301SGarrett Wollman * of sequence; drop it. 2248dac20301SGarrett Wollman */ 2249fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2250dac20301SGarrett Wollman 2251df8bae1dSRodney W. Grimes /* 2252dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 2253dac20301SGarrett Wollman * But keep on processing for RST or ACK. 2254df8bae1dSRodney W. Grimes */ 2255dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2256fb59c426SYoshinobu Inoue todrop = tlen; 225778b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 225878b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 2259df8bae1dSRodney W. Grimes } else { 226078b50714SRobert Watson TCPSTAT_INC(tcps_rcvpartduppack); 226178b50714SRobert Watson TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 2262df8bae1dSRodney W. Grimes } 2263fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 2264fb59c426SYoshinobu Inoue th->th_seq += todrop; 2265fb59c426SYoshinobu Inoue tlen -= todrop; 2266fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 2267fb59c426SYoshinobu Inoue th->th_urp -= todrop; 2268df8bae1dSRodney W. Grimes else { 2269fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2270fb59c426SYoshinobu Inoue th->th_urp = 0; 2271df8bae1dSRodney W. Grimes } 2272df8bae1dSRodney W. Grimes } 2273df8bae1dSRodney W. Grimes 2274df8bae1dSRodney W. Grimes /* 2275df8bae1dSRodney W. Grimes * If new data are received on a connection after the 2276df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 2277df8bae1dSRodney W. Grimes */ 2278df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 2279fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 2280ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2281252ca428SRobert Watson 228207dacf03SAndre Oppermann if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 228307dacf03SAndre Oppermann log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data " 228407dacf03SAndre Oppermann "after socket was closed, " 228507dacf03SAndre Oppermann "sending RST and removing tcpcb\n", 2286e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 2287773673c1SAndre Oppermann free(s, M_TCPLOG); 2288773673c1SAndre Oppermann } 2289df8bae1dSRodney W. Grimes tp = tcp_close(tp); 229078b50714SRobert Watson TCPSTAT_INC(tcps_rcvafterclose); 2291a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2292df8bae1dSRodney W. Grimes goto dropwithreset; 2293df8bae1dSRodney W. Grimes } 2294df8bae1dSRodney W. Grimes 2295df8bae1dSRodney W. Grimes /* 2296df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 2297df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 2298df8bae1dSRodney W. Grimes */ 2299fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 2300df8bae1dSRodney W. Grimes if (todrop > 0) { 230178b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 2302fb59c426SYoshinobu Inoue if (todrop >= tlen) { 230378b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 2304df8bae1dSRodney W. Grimes /* 2305df8bae1dSRodney W. Grimes * If window is closed can only take segments at 2306df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 2307df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 2308df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 2309df8bae1dSRodney W. Grimes * and ack. 2310df8bae1dSRodney W. Grimes */ 2311fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2312df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 231378b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinprobe); 2314df8bae1dSRodney W. Grimes } else 2315df8bae1dSRodney W. Grimes goto dropafterack; 2316df8bae1dSRodney W. Grimes } else 231778b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2318df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2319fb59c426SYoshinobu Inoue tlen -= todrop; 2320fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 2321df8bae1dSRodney W. Grimes } 2322df8bae1dSRodney W. Grimes 2323df8bae1dSRodney W. Grimes /* 2324df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 2325df8bae1dSRodney W. Grimes * record its timestamp. 2326cf09195bSPaul Saab * NOTE: 2327cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 2328a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2329cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 2330cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 2331cf09195bSPaul Saab * predicated on the sequence space of this segment. 2332cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 2333cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 2334cf09195bSPaul Saab * instead of RFC1323's 2335cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 2336cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 2337cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 2338cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 2339cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 2340df8bae1dSRodney W. Grimes */ 2341be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 2342cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2343cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 2344cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 2345d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 2346a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 2347df8bae1dSRodney W. Grimes } 2348df8bae1dSRodney W. Grimes 2349df8bae1dSRodney W. Grimes /* 2350a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 2351a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 2352a0292f23SGarrett Wollman * later processing; else drop segment and return. 2353a0292f23SGarrett Wollman */ 2354fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 2355a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 2356281a0fd4SPatrick Kelsey (tp->t_flags & TF_NEEDSYN)) { 2357281a0fd4SPatrick Kelsey if (tp->t_state == TCPS_SYN_RECEIVED && 235868bd7ed1SJonathan T. Looney IS_FASTOPEN(tp->t_flags)) { 2359281a0fd4SPatrick Kelsey tp->snd_wnd = tiwin; 2360281a0fd4SPatrick Kelsey cc_conn_init(tp); 2361281a0fd4SPatrick Kelsey } 2362a0292f23SGarrett Wollman goto step6; 2363281a0fd4SPatrick Kelsey } else if (tp->t_flags & TF_ACKNOW) 23645e1aa279SDavid Malone goto dropafterack; 2365a0292f23SGarrett Wollman else 2366a0292f23SGarrett Wollman goto drop; 2367a0292f23SGarrett Wollman } 2368df8bae1dSRodney W. Grimes 2369df8bae1dSRodney W. Grimes /* 2370df8bae1dSRodney W. Grimes * Ack processing. 2371df8bae1dSRodney W. Grimes */ 2372df8bae1dSRodney W. Grimes switch (tp->t_state) { 2373df8bae1dSRodney W. Grimes 2374df8bae1dSRodney W. Grimes /* 2375764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 2376764d8cefSBill Fenner * ESTABLISHED state and continue processing. 2377764d8cefSBill Fenner * The ACK was checked above. 2378df8bae1dSRodney W. Grimes */ 2379df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2380a0292f23SGarrett Wollman 238178b50714SRobert Watson TCPSTAT_INC(tcps_connects); 2382df8bae1dSRodney W. Grimes soisconnected(so); 2383df8bae1dSRodney W. Grimes /* Do window scaling? */ 2384df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2385df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2386df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 2387464fcfbcSAndre Oppermann tp->snd_wnd = tiwin; 2388df8bae1dSRodney W. Grimes } 2389a0292f23SGarrett Wollman /* 2390a0292f23SGarrett Wollman * Make transitions: 2391a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 2392a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 2393a0292f23SGarrett Wollman */ 23949b8b58e0SJonathan Lemon tp->t_starttime = ticks; 239518a75309SPatrick Kelsey if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) { 2396281a0fd4SPatrick Kelsey tcp_fastopen_decrement_counter(tp->t_tfo_pending); 2397281a0fd4SPatrick Kelsey tp->t_tfo_pending = NULL; 2398281a0fd4SPatrick Kelsey 2399281a0fd4SPatrick Kelsey /* 2400281a0fd4SPatrick Kelsey * Account for the ACK of our SYN prior to 2401281a0fd4SPatrick Kelsey * regular ACK processing below. 2402281a0fd4SPatrick Kelsey */ 2403281a0fd4SPatrick Kelsey tp->snd_una++; 2404281a0fd4SPatrick Kelsey } 24058db239dcSMichael Tuexen if (tp->t_flags & TF_NEEDFIN) { 24068db239dcSMichael Tuexen tcp_state_change(tp, TCPS_FIN_WAIT_1); 24078db239dcSMichael Tuexen tp->t_flags &= ~TF_NEEDFIN; 24088db239dcSMichael Tuexen } else { 24098db239dcSMichael Tuexen tcp_state_change(tp, TCPS_ESTABLISHED); 24108db239dcSMichael Tuexen TCP_PROBE5(accept__established, NULL, tp, 24118db239dcSMichael Tuexen m, tp, th); 2412281a0fd4SPatrick Kelsey /* 2413281a0fd4SPatrick Kelsey * TFO connections call cc_conn_init() during SYN 2414281a0fd4SPatrick Kelsey * processing. Calling it again here for such 2415281a0fd4SPatrick Kelsey * connections is not harmless as it would undo the 2416281a0fd4SPatrick Kelsey * snd_cwnd reduction that occurs when a TFO SYN|ACK 2417281a0fd4SPatrick Kelsey * is retransmitted. 2418281a0fd4SPatrick Kelsey */ 241968bd7ed1SJonathan T. Looney if (!IS_FASTOPEN(tp->t_flags)) 2420dbc42409SLawrence Stewart cc_conn_init(tp); 24219077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 24227ff19458SPaul Traina } 2423a0292f23SGarrett Wollman /* 2424a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 2425a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 2426a0292f23SGarrett Wollman */ 2427fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 2428c28440dbSRandall Stewart (void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0, 2429a0292f23SGarrett Wollman (struct mbuf *)0); 243060ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq - 1; 243193b0017fSPhilippe Charnier /* FALLTHROUGH */ 2432df8bae1dSRodney W. Grimes 2433df8bae1dSRodney W. Grimes /* 2434df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2435df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 2436fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 2437fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 2438df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 2439df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 2440df8bae1dSRodney W. Grimes */ 2441df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2442df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2443df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2444df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 2445df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2446df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 24475a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 244878b50714SRobert Watson TCPSTAT_INC(tcps_rcvacktoomuch); 24495a53ca16SPaul Saab goto dropafterack; 24505a53ca16SPaul Saab } 24513529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2452fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 2453fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 2454021eaf79SHiren Panchasara sack_changed = tcp_sack_doack(tp, &to, th->th_ack); 245512eeb81fSHiren Panchasara else 245612eeb81fSHiren Panchasara /* 245712eeb81fSHiren Panchasara * Reset the value so that previous (valid) value 245812eeb81fSHiren Panchasara * from the last ack with SACK doesn't get used. 245912eeb81fSHiren Panchasara */ 246012eeb81fSHiren Panchasara tp->sackhint.sacked_bytes = 0; 246139bc9de5SLawrence Stewart 2462bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 246339bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 246439bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 2465bd79708dSJonathan T. Looney #endif 246639bc9de5SLawrence Stewart 2467fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 24680c39d38dSGleb Smirnoff u_int maxseg; 24690c39d38dSGleb Smirnoff 24700c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 2471021eaf79SHiren Panchasara if (tlen == 0 && 2472021eaf79SHiren Panchasara (tiwin == tp->snd_wnd || 2473021eaf79SHiren Panchasara (tp->t_flags & TF_SACK_PERMIT))) { 247462b90589SPeter Wemm /* 247562b90589SPeter Wemm * If this is the first time we've seen a 247662b90589SPeter Wemm * FIN from the remote, this is not a 247762b90589SPeter Wemm * duplicate and it needs to be processed 247862b90589SPeter Wemm * normally. This happens during a 247962b90589SPeter Wemm * simultaneous close. 248062b90589SPeter Wemm */ 248162b90589SPeter Wemm if ((thflags & TH_FIN) && 248262b90589SPeter Wemm (TCPS_HAVERCVDFIN(tp->t_state) == 0)) { 248362b90589SPeter Wemm tp->t_dupacks = 0; 248462b90589SPeter Wemm break; 248562b90589SPeter Wemm } 248678b50714SRobert Watson TCPSTAT_INC(tcps_rcvdupack); 2487df8bae1dSRodney W. Grimes /* 2488df8bae1dSRodney W. Grimes * If we have outstanding data (other than 2489df8bae1dSRodney W. Grimes * a window probe), this is a completely 2490df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 24915f30ec9bSEitan Adler * change and FIN isn't set), 24925f30ec9bSEitan Adler * the ack is the biggest we've 2493df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 2494a4641f4eSPedro F. Giffuni * threshold of them, assume a packet 2495df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 2496df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 2497df8bae1dSRodney W. Grimes * window so we send only this one 2498df8bae1dSRodney W. Grimes * packet. 2499df8bae1dSRodney W. Grimes * 2500df8bae1dSRodney W. Grimes * We know we're losing at the current 2501df8bae1dSRodney W. Grimes * window size so do congestion avoidance 2502df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 2503df8bae1dSRodney W. Grimes * and pull our congestion window back to 2504df8bae1dSRodney W. Grimes * the new ssthresh). 2505df8bae1dSRodney W. Grimes * 2506df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 2507df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 2508df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 2509df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 2510df8bae1dSRodney W. Grimes * network. 2511f2512ba1SRui Paulo * 2512f2512ba1SRui Paulo * When using TCP ECN, notify the peer that 2513f2512ba1SRui Paulo * we reduced the cwnd. 2514df8bae1dSRodney W. Grimes */ 2515021eaf79SHiren Panchasara /* 2516021eaf79SHiren Panchasara * Following 2 kinds of acks should not affect 2517021eaf79SHiren Panchasara * dupack counting: 2518021eaf79SHiren Panchasara * 1) Old acks 2519021eaf79SHiren Panchasara * 2) Acks with SACK but without any new SACK 2520021eaf79SHiren Panchasara * information in them. These could result from 2521021eaf79SHiren Panchasara * any anomaly in the network like a switch 2522021eaf79SHiren Panchasara * duplicating packets or a possible DoS attack. 2523021eaf79SHiren Panchasara */ 2524021eaf79SHiren Panchasara if (th->th_ack != tp->snd_una || 2525021eaf79SHiren Panchasara ((tp->t_flags & TF_SACK_PERMIT) && 2526021eaf79SHiren Panchasara !sack_changed)) 2527021eaf79SHiren Panchasara break; 2528021eaf79SHiren Panchasara else if (!tcp_timer_active(tp, TT_REXMT)) 2529df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2530cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 2531dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 25324b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 25334b7b743cSLawrence Stewart CC_DUPACK); 25343529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2535dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2536808f11b7SPaul Saab int awnd; 253725e6f9edSPaul Saab 253825e6f9edSPaul Saab /* 253925e6f9edSPaul Saab * Compute the amount of data in flight first. 25404c3972f0SHiren Panchasara * We can inject new data into the pipe iff 254125e6f9edSPaul Saab * we have less than 1/2 the original window's 254225e6f9edSPaul Saab * worth of data in flight. 254325e6f9edSPaul Saab */ 254412eeb81fSHiren Panchasara if (V_tcp_do_rfc6675_pipe) 254512eeb81fSHiren Panchasara awnd = tcp_compute_pipe(tp); 254612eeb81fSHiren Panchasara else 2547808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 25480077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 254912eeb81fSHiren Panchasara 2550808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 25510c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 255225e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 255325e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 255425e6f9edSPaul Saab } 255525e6f9edSPaul Saab } else 25560c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 255755bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 255846f58482SJonathan Lemon goto drop; 2559cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 2560cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 2561a0445c2eSJayanth Vijayaraghavan 2562a0445c2eSJayanth Vijayaraghavan /* 2563a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 2564a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 2565a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 2566a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 2567a0445c2eSJayanth Vijayaraghavan * recovery. 2568a0445c2eSJayanth Vijayaraghavan */ 25693529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 2570dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2571a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 2572a0445c2eSJayanth Vijayaraghavan break; 2573a0445c2eSJayanth Vijayaraghavan } 2574dbc42409SLawrence Stewart } else { 2575a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 25769d11646dSJeffrey Hsu tp->snd_recover)) { 2577cb942153SJeffrey Hsu tp->t_dupacks = 0; 2578cb942153SJeffrey Hsu break; 257946f58482SJonathan Lemon } 2580a0445c2eSJayanth Vijayaraghavan } 2581dbc42409SLawrence Stewart /* Congestion signal before ack. */ 2582dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_NDUPACK); 25834b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 25844b7b743cSLawrence Stewart CC_DUPACK); 2585b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 25869b8b58e0SJonathan Lemon tp->t_rtttime = 0; 25873529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 258878b50714SRobert Watson TCPSTAT_INC( 258978b50714SRobert Watson tcps_sack_recovery_episode); 2590a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 25910c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 259255bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 25936d90faf3SPaul Saab goto drop; 25946d90faf3SPaul Saab } 2595fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 25960c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 259755bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 259848d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 2599302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 2600302ce8d6SAndre Oppermann __func__)); 2601df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 26020c39d38dSGleb Smirnoff maxseg * 260348d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 2604df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 2605df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 2606df8bae1dSRodney W. Grimes goto drop; 2607603724d3SBjoern A. Zeeb } else if (V_tcp_do_rfc3042) { 260862d4443fSHiren Panchasara /* 260962d4443fSHiren Panchasara * Process first and second duplicate 261062d4443fSHiren Panchasara * ACKs. Each indicates a segment 261162d4443fSHiren Panchasara * leaving the network, creating room 261262d4443fSHiren Panchasara * for more. Make sure we can send a 261362d4443fSHiren Panchasara * packet on reception of each duplicate 261462d4443fSHiren Panchasara * ACK by increasing snd_cwnd by one 261562d4443fSHiren Panchasara * segment. Restore the original 261662d4443fSHiren Panchasara * snd_cwnd after packet transmission. 261762d4443fSHiren Panchasara */ 26184b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 26194b7b743cSLawrence Stewart CC_DUPACK); 26203ac12506SJonathan T. Looney uint32_t oldcwnd = tp->snd_cwnd; 262148d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 262248d2549cSJeffrey Hsu u_int sent; 26235628dd08SAndre Oppermann int avail; 262489c02376SJeffrey Hsu 2625582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 2626582a954bSJeffrey Hsu tp->t_dupacks == 2, 2627302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 2628302ce8d6SAndre Oppermann __func__)); 262961a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 263048d2549cSJeffrey Hsu tp->snd_limited = 0; 263161a36e3dSJeffrey Hsu tp->snd_cwnd = 263261a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 263361a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 26340c39d38dSGleb Smirnoff maxseg; 26355628dd08SAndre Oppermann /* 26365628dd08SAndre Oppermann * Only call tcp_output when there 26375628dd08SAndre Oppermann * is new data available to be sent. 26385628dd08SAndre Oppermann * Otherwise we would send pure ACKs. 26395628dd08SAndre Oppermann */ 26405628dd08SAndre Oppermann SOCKBUF_LOCK(&so->so_snd); 2641cfa6009eSGleb Smirnoff avail = sbavail(&so->so_snd) - 26425628dd08SAndre Oppermann (tp->snd_nxt - tp->snd_una); 26435628dd08SAndre Oppermann SOCKBUF_UNLOCK(&so->so_snd); 26445628dd08SAndre Oppermann if (avail > 0) 264555bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 264648d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 26470c39d38dSGleb Smirnoff if (sent > maxseg) { 264889c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 264989c02376SJeffrey Hsu tp->snd_limited == 0) || 26500c39d38dSGleb Smirnoff (sent == maxseg + 1 && 265189c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 2652302ce8d6SAndre Oppermann ("%s: sent too much", 2653302ce8d6SAndre Oppermann __func__)); 265448d2549cSJeffrey Hsu tp->snd_limited = 2; 265548d2549cSJeffrey Hsu } else if (sent > 0) 265648d2549cSJeffrey Hsu ++tp->snd_limited; 2657582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 2658582a954bSJeffrey Hsu goto drop; 2659df8bae1dSRodney W. Grimes } 2660021eaf79SHiren Panchasara } 2661df8bae1dSRodney W. Grimes break; 2662021eaf79SHiren Panchasara } else { 2663021eaf79SHiren Panchasara /* 2664021eaf79SHiren Panchasara * This ack is advancing the left edge, reset the 2665021eaf79SHiren Panchasara * counter. 2666021eaf79SHiren Panchasara */ 2667021eaf79SHiren Panchasara tp->t_dupacks = 0; 2668021eaf79SHiren Panchasara /* 2669021eaf79SHiren Panchasara * If this ack also has new SACK info, increment the 2670021eaf79SHiren Panchasara * counter as per rfc6675. 2671021eaf79SHiren Panchasara */ 2672021eaf79SHiren Panchasara if ((tp->t_flags & TF_SACK_PERMIT) && sack_changed) 2673021eaf79SHiren Panchasara tp->t_dupacks++; 2674df8bae1dSRodney W. Grimes } 2675cb942153SJeffrey Hsu 2676302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2677302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 2678cb942153SJeffrey Hsu 2679df8bae1dSRodney W. Grimes /* 2680df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2681df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2682df8bae1dSRodney W. Grimes */ 2683dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2684cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 26853529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 26866d90faf3SPaul Saab tcp_sack_partialack(tp, th); 26876d90faf3SPaul Saab else 2688c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2689dbc42409SLawrence Stewart } else 2690dbc42409SLawrence Stewart cc_post_recovery(tp, th); 269146f58482SJonathan Lemon } 2692a0292f23SGarrett Wollman /* 2693a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2694a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2695a0292f23SGarrett Wollman */ 2696a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2697a0292f23SGarrett Wollman /* 2698a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2699a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 270007e43e10SAndras Olah * synchronized). Go to non-starred state, 270107e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 270207e43e10SAndras Olah * we can do window scaling. 2703a0292f23SGarrett Wollman */ 2704a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2705a0292f23SGarrett Wollman tp->snd_una++; 270607e43e10SAndras Olah /* Do window scaling? */ 270707e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 270807e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 270907e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2710464fcfbcSAndre Oppermann /* Send window already scaled. */ 271107e43e10SAndras Olah } 2712a0292f23SGarrett Wollman } 2713a0292f23SGarrett Wollman 2714a0292f23SGarrett Wollman process_ACK: 27158501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 27167cfc6904SRobert Watson 2717dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 2718b8c2cd15SJonathan T. Looney KASSERT(acked >= 0, ("%s: acked unexepectedly negative " 2719b8c2cd15SJonathan T. Looney "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__, 2720b8c2cd15SJonathan T. Looney tp->snd_una, th->th_ack, tp, m)); 27214b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvackpack, nsegs); 272278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 2723df8bae1dSRodney W. Grimes 2724df8bae1dSRodney W. Grimes /* 27259b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 27269b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 27279b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 27289b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 27299b8b58e0SJonathan Lemon * we left off. 27309b8b58e0SJonathan Lemon */ 273110d20c84SMatt Macy if (tp->t_rxtshift == 1 && 273210d20c84SMatt Macy tp->t_flags & TF_PREVVALID && 273310d20c84SMatt Macy tp->t_badrxtwin && 273410d20c84SMatt Macy SEQ_LT(to.to_tsecr, tp->t_badrxtwin)) 2735dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 27369b8b58e0SJonathan Lemon 27379b8b58e0SJonathan Lemon /* 2738df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2739df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2740df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2741df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2742df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2743df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2744df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2745fa55172bSMatthew Dillon * 2746fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2747fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2748fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2749fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2750df8bae1dSRodney W. Grimes */ 2751d8951c8aSBjoern A. Zeeb if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) { 27523ac12506SJonathan T. Looney uint32_t t; 2753d8951c8aSBjoern A. Zeeb 2754d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 2755d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 2756d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 2757d8951c8aSBjoern A. Zeeb tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1); 2758fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2759eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2760eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 27619b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2762fa55172bSMatthew Dillon } 2763df8bae1dSRodney W. Grimes 2764df8bae1dSRodney W. Grimes /* 2765df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2766df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2767df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2768df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2769df8bae1dSRodney W. Grimes */ 2770fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 2771b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2772df8bae1dSRodney W. Grimes needoutput = 1; 2773b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2774b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2775a0292f23SGarrett Wollman 2776a0292f23SGarrett Wollman /* 2777a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2778a0292f23SGarrett Wollman * skip rest of ACK processing. 2779a0292f23SGarrett Wollman */ 2780a0292f23SGarrett Wollman if (acked == 0) 2781a0292f23SGarrett Wollman goto step6; 2782a0292f23SGarrett Wollman 2783df8bae1dSRodney W. Grimes /* 2784dbc42409SLawrence Stewart * Let the congestion control algorithm update congestion 2785dbc42409SLawrence Stewart * control related information. This typically means increasing 2786dbc42409SLawrence Stewart * the congestion window. 2787df8bae1dSRodney W. Grimes */ 27884b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, CC_ACK); 2789dbc42409SLawrence Stewart 27905905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2791cfa6009eSGleb Smirnoff if (acked > sbavail(&so->so_snd)) { 2792b8c2cd15SJonathan T. Looney if (tp->snd_wnd >= sbavail(&so->so_snd)) 2793cfa6009eSGleb Smirnoff tp->snd_wnd -= sbavail(&so->so_snd); 2794b8c2cd15SJonathan T. Looney else 2795b8c2cd15SJonathan T. Looney tp->snd_wnd = 0; 2796c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, 2797cfa6009eSGleb Smirnoff (int)sbavail(&so->so_snd)); 2798df8bae1dSRodney W. Grimes ourfinisacked = 1; 2799df8bae1dSRodney W. Grimes } else { 2800c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, acked); 28013ac12506SJonathan T. Looney if (tp->snd_wnd >= (uint32_t) acked) 280260ee3bb2SAndre Oppermann tp->snd_wnd -= acked; 2803b8c2cd15SJonathan T. Looney else 2804b8c2cd15SJonathan T. Looney tp->snd_wnd = 0; 2805df8bae1dSRodney W. Grimes ourfinisacked = 0; 2806df8bae1dSRodney W. Grimes } 2807564aab1fSAndre Oppermann /* NB: sowwakeup_locked() does an implicit unlock. */ 28081e4d7da7SRobert Watson sowwakeup_locked(so); 2809c11a15bfSGleb Smirnoff m_freem(mfree); 2810e8949f74SAndre Oppermann /* Detect una wraparound. */ 2811dbc42409SLawrence Stewart if (!IN_RECOVERY(tp->t_flags) && 28129d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 28139d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 28149d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 2815dbc42409SLawrence Stewart /* XXXLAS: Can this be moved up into cc_post_recovery? */ 2816dbc42409SLawrence Stewart if (IN_RECOVERY(tp->t_flags) && 281724cb0f22SLawrence Stewart SEQ_GEQ(th->th_ack, tp->snd_recover)) { 2818dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 281924cb0f22SLawrence Stewart } 2820fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 28213529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 28226d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 28236d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 28246d90faf3SPaul Saab } 2825df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2826df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2827df8bae1dSRodney W. Grimes 2828df8bae1dSRodney W. Grimes switch (tp->t_state) { 2829df8bae1dSRodney W. Grimes 2830df8bae1dSRodney W. Grimes /* 2831df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2832df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2833df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2834df8bae1dSRodney W. Grimes */ 2835df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2836df8bae1dSRodney W. Grimes if (ourfinisacked) { 2837df8bae1dSRodney W. Grimes /* 2838df8bae1dSRodney W. Grimes * If we can't receive any more 2839df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2840df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2841df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2842df8bae1dSRodney W. Grimes * we'll hang forever. 2843e8949f74SAndre Oppermann * 2844e8949f74SAndre Oppermann * XXXjl: 2845340c35deSJonathan Lemon * we should release the tp also, and use a 2846340c35deSJonathan Lemon * compressed state. 2847340c35deSJonathan Lemon */ 2848c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 284903e49181SSeigo Tanimura soisdisconnected(so); 28509077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 28519077f387SGleb Smirnoff (tcp_fast_finwait2_recycle ? 28529077f387SGleb Smirnoff tcp_finwait2_timeout : 28539077f387SGleb Smirnoff TP_MAXIDLE(tp))); 28544cc20ab1SSeigo Tanimura } 285557f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_2); 2856df8bae1dSRodney W. Grimes } 2857df8bae1dSRodney W. Grimes break; 2858df8bae1dSRodney W. Grimes 2859df8bae1dSRodney W. Grimes /* 2860df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2861df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2862df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2863df8bae1dSRodney W. Grimes * the segment. 2864df8bae1dSRodney W. Grimes */ 2865df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2866df8bae1dSRodney W. Grimes if (ourfinisacked) { 2867ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 286811a20fb8SJeffrey Hsu tcp_twstart(tp); 2869340c35deSJonathan Lemon m_freem(m); 2870679d9708SAndre Oppermann return; 2871df8bae1dSRodney W. Grimes } 2872df8bae1dSRodney W. Grimes break; 2873df8bae1dSRodney W. Grimes 2874df8bae1dSRodney W. Grimes /* 2875df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2876df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2877df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2878df8bae1dSRodney W. Grimes * enter the closed state and return. 2879df8bae1dSRodney W. Grimes */ 2880df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2881df8bae1dSRodney W. Grimes if (ourfinisacked) { 2882ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2883df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2884df8bae1dSRodney W. Grimes goto drop; 2885df8bae1dSRodney W. Grimes } 2886df8bae1dSRodney W. Grimes break; 2887df8bae1dSRodney W. Grimes } 2888df8bae1dSRodney W. Grimes } 2889df8bae1dSRodney W. Grimes 2890df8bae1dSRodney W. Grimes step6: 28918501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 28927cfc6904SRobert Watson 2893df8bae1dSRodney W. Grimes /* 289460ee3bb2SAndre Oppermann * Update window information. 289560ee3bb2SAndre Oppermann * Don't look at window if no ACK: TAC's send garbage on first SYN. 2896df8bae1dSRodney W. Grimes */ 289760ee3bb2SAndre Oppermann if ((thflags & TH_ACK) && 289860ee3bb2SAndre Oppermann (SEQ_LT(tp->snd_wl1, th->th_seq) || 289960ee3bb2SAndre Oppermann (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 290060ee3bb2SAndre Oppermann (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 290160ee3bb2SAndre Oppermann /* keep track of pure window updates */ 290260ee3bb2SAndre Oppermann if (tlen == 0 && 290360ee3bb2SAndre Oppermann tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 290478b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinupd); 2905df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 290660ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 290760ee3bb2SAndre Oppermann tp->snd_wl2 = th->th_ack; 2908df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2909df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 291060ee3bb2SAndre Oppermann needoutput = 1; 2911df8bae1dSRodney W. Grimes } 2912df8bae1dSRodney W. Grimes 2913df8bae1dSRodney W. Grimes /* 2914df8bae1dSRodney W. Grimes * Process segments with URG. 2915df8bae1dSRodney W. Grimes */ 2916fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2917df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2918df8bae1dSRodney W. Grimes /* 2919df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2920df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2921df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2922df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2923df8bae1dSRodney W. Grimes */ 292418ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2925cfa6009eSGleb Smirnoff if (th->th_urp + sbavail(&so->so_rcv) > sb_max) { 2926fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2927fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 292818ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2929df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2930df8bae1dSRodney W. Grimes } 2931df8bae1dSRodney W. Grimes /* 2932df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2933df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2934df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2935df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2936df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2937df8bae1dSRodney W. Grimes * 2938df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2939df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2940df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2941df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2942df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2943df8bae1dSRodney W. Grimes * spec states (in one of two places). 2944df8bae1dSRodney W. Grimes */ 2945fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2946fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2947cfa6009eSGleb Smirnoff so->so_oobmark = sbavail(&so->so_rcv) + 2948df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2949927c5ceaSRobert Watson if (so->so_oobmark == 0) 2950c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 2951df8bae1dSRodney W. Grimes sohasoutofband(so); 2952df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2953df8bae1dSRodney W. Grimes } 295418ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2955df8bae1dSRodney W. Grimes /* 2956df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2957df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2958df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2959df8bae1dSRodney W. Grimes * data may creep in... ick. 2960df8bae1dSRodney W. Grimes */ 29613ac12506SJonathan T. Looney if (th->th_urp <= (uint32_t)tlen && 296230613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 296330613f56SJeffrey Hsu /* hdr drop is delayed */ 296430613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 296530613f56SJeffrey Hsu } 2966c068736aSJeffrey Hsu } else { 2967df8bae1dSRodney W. Grimes /* 2968df8bae1dSRodney W. Grimes * If no out of band data is expected, 2969df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2970df8bae1dSRodney W. Grimes * with the receive window. 2971df8bae1dSRodney W. Grimes */ 2972df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2973df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2974c068736aSJeffrey Hsu } 2975df8bae1dSRodney W. Grimes dodata: /* XXX */ 29768501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 29777cfc6904SRobert Watson 2978df8bae1dSRodney W. Grimes /* 2979df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2980df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2981df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2982df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2983df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2984df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2985df8bae1dSRodney W. Grimes */ 2986281a0fd4SPatrick Kelsey tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) && 298768bd7ed1SJonathan T. Looney IS_FASTOPEN(tp->t_flags)); 2988281a0fd4SPatrick Kelsey if ((tlen || (thflags & TH_FIN) || tfo_syn) && 2989df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 299069e03620SPaul Saab tcp_seq save_start = th->th_seq; 2991fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2992e4b64281SJesper Skriver /* 2993c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2994c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2995c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2996c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2997c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2998c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2999c068736aSJeffrey Hsu * conversions. 3000c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 3001c068736aSJeffrey Hsu * immediately when segments are out of order (so 3002c068736aSJeffrey Hsu * fast retransmit can work). 3003e4b64281SJesper Skriver */ 30044741bfcbSPatrick Kelsey if (th->th_seq == tp->rcv_nxt && 3005c28440dbSRandall Stewart SEGQ_EMPTY(tp) && 3006281a0fd4SPatrick Kelsey (TCPS_HAVEESTABLISHED(tp->t_state) || 3007281a0fd4SPatrick Kelsey tfo_syn)) { 3008281a0fd4SPatrick Kelsey if (DELAY_ACK(tp, tlen) || tfo_syn) 30093bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 3010e4b64281SJesper Skriver else 3011e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 3012e4b64281SJesper Skriver tp->rcv_nxt += tlen; 3013e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 301478b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 301578b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 30161e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3017c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 3018c1c36a2cSMike Silbersack m_freem(m); 3019c1c36a2cSMike Silbersack else 3020651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 3021e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 30221e4d7da7SRobert Watson sorwakeup_locked(so); 3023e4b64281SJesper Skriver } else { 3024e8949f74SAndre Oppermann /* 3025e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 3026e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 3027e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 3028e8949f74SAndre Oppermann * when trimming from the head. 3029e8949f74SAndre Oppermann */ 3030c28440dbSRandall Stewart thflags = tcp_reass(tp, th, &save_start, &tlen, m); 3031e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 3032e4b64281SJesper Skriver } 30333529149eSAndre Oppermann if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 3034f194524fSAndre Oppermann tcp_update_sack_list(tp, save_start, save_start + tlen); 3035302ce8d6SAndre Oppermann #if 0 3036df8bae1dSRodney W. Grimes /* 3037df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 3038df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 3039df8bae1dSRodney W. Grimes * buffer size. 3040302ce8d6SAndre Oppermann * XXX: Unused. 3041df8bae1dSRodney W. Grimes */ 3042f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) 3043df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 3044f701e30dSJohn Baldwin else 3045f701e30dSJohn Baldwin len = so->so_rcv.sb_hiwat; 3046302ce8d6SAndre Oppermann #endif 3047df8bae1dSRodney W. Grimes } else { 3048df8bae1dSRodney W. Grimes m_freem(m); 3049fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 3050df8bae1dSRodney W. Grimes } 3051df8bae1dSRodney W. Grimes 3052df8bae1dSRodney W. Grimes /* 3053df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 3054df8bae1dSRodney W. Grimes * that the connection is closing. 3055df8bae1dSRodney W. Grimes */ 3056fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 3057df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 3058df8bae1dSRodney W. Grimes socantrcvmore(so); 3059a0292f23SGarrett Wollman /* 3060a0292f23SGarrett Wollman * If connection is half-synchronized 3061d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 3062a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 3063a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 3064a0292f23SGarrett Wollman * more input can be expected, send ACK now. 3065a0292f23SGarrett Wollman */ 30663bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 30673bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 3068a0292f23SGarrett Wollman else 3069df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 3070df8bae1dSRodney W. Grimes tp->rcv_nxt++; 3071df8bae1dSRodney W. Grimes } 3072df8bae1dSRodney W. Grimes switch (tp->t_state) { 3073df8bae1dSRodney W. Grimes 3074df8bae1dSRodney W. Grimes /* 3075df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 3076df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 3077df8bae1dSRodney W. Grimes */ 3078df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 30799b8b58e0SJonathan Lemon tp->t_starttime = ticks; 30809b8b58e0SJonathan Lemon /* FALLTHROUGH */ 3081df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 308257f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSE_WAIT); 3083df8bae1dSRodney W. Grimes break; 3084df8bae1dSRodney W. Grimes 3085df8bae1dSRodney W. Grimes /* 3086df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 3087df8bae1dSRodney W. Grimes * enter the CLOSING state. 3088df8bae1dSRodney W. Grimes */ 3089df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 309057f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSING); 3091df8bae1dSRodney W. Grimes break; 3092df8bae1dSRodney W. Grimes 3093df8bae1dSRodney W. Grimes /* 3094df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 3095df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 3096df8bae1dSRodney W. Grimes * standard timers. 3097df8bae1dSRodney W. Grimes */ 3098df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 3099ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 3100252ca428SRobert Watson 3101340c35deSJonathan Lemon tcp_twstart(tp); 3102679d9708SAndre Oppermann return; 3103df8bae1dSRodney W. Grimes } 3104df8bae1dSRodney W. Grimes } 3105610ee2f9SDavid Greenman #ifdef TCPDEBUG 31064cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3107fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 3108fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3109610ee2f9SDavid Greenman #endif 31102b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 3111df8bae1dSRodney W. Grimes 3112df8bae1dSRodney W. Grimes /* 3113df8bae1dSRodney W. Grimes * Return any desired output. 3114df8bae1dSRodney W. Grimes */ 3115df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 311655bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 31177792ea27SJeffrey Hsu 3118a14c749fSJonathan Lemon check_delack: 31198501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 3120252ca428SRobert Watson 3121a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 31223bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 3123b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 31243bfd6421SJonathan Lemon } 31258501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3126679d9708SAndre Oppermann return; 3127df8bae1dSRodney W. Grimes 3128df8bae1dSRodney W. Grimes dropafterack: 3129df8bae1dSRodney W. Grimes /* 3130df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 3131df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 313280ab7c0eSGarrett Wollman * 313380ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 313480ab7c0eSGarrett Wollman * paths to this code happen after packets containing 313580ab7c0eSGarrett Wollman * RST have been dropped. 313680ab7c0eSGarrett Wollman * 313780ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 313880ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 313980ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 314080ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 314180ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 314280ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 3143df8bae1dSRodney W. Grimes */ 3144fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 3145fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 3146a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 3147a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 3148a57815efSBosko Milekic goto dropwithreset; 3149a57815efSBosko Milekic } 3150a0292f23SGarrett Wollman #ifdef TCPDEBUG 31514cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3152fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3153fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3154a0292f23SGarrett Wollman #endif 31552b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 3156df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 315755bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 31588501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3159d6915262SRobert Watson m_freem(m); 3160679d9708SAndre Oppermann return; 3161df8bae1dSRodney W. Grimes 3162df8bae1dSRodney W. Grimes dropwithreset: 3163a0ca0871SRobert Watson if (tp != NULL) { 3164302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 31658501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3166dd8ac7f9SRobert Watson } else 3167a0ca0871SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 3168679d9708SAndre Oppermann return; 3169df8bae1dSRodney W. Grimes 3170df8bae1dSRodney W. Grimes drop: 3171df8bae1dSRodney W. Grimes /* 3172df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 3173df8bae1dSRodney W. Grimes */ 3174610ee2f9SDavid Greenman #ifdef TCPDEBUG 31751c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 3176fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3177fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3178a0292f23SGarrett Wollman #endif 31792b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 31801c53f806SRobert Watson if (tp != NULL) 31818501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3182d6915262SRobert Watson m_freem(m); 3183302ce8d6SAndre Oppermann } 3184302ce8d6SAndre Oppermann 3185302ce8d6SAndre Oppermann /* 31860ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 31870ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 31880ca3f933SAndre Oppermann * tp may be NULL. 3189302ce8d6SAndre Oppermann */ 319055bceb1eSRandall Stewart void 3191302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 3192302ce8d6SAndre Oppermann int tlen, int rstreason) 3193302ce8d6SAndre Oppermann { 3194b287c6c7SBjoern A. Zeeb #ifdef INET 3195302ce8d6SAndre Oppermann struct ip *ip; 3196b287c6c7SBjoern A. Zeeb #endif 3197302ce8d6SAndre Oppermann #ifdef INET6 3198302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 3199302ce8d6SAndre Oppermann #endif 32007a3244ccSRobert Watson 32017a3244ccSRobert Watson if (tp != NULL) { 32028501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 32037a3244ccSRobert Watson } 32047a3244ccSRobert Watson 32050ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 3206302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 3207302ce8d6SAndre Oppermann goto drop; 3208302ce8d6SAndre Oppermann #ifdef INET6 3209302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 3210302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 3211302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3212302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3213302ce8d6SAndre Oppermann goto drop; 3214302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 3215b287c6c7SBjoern A. Zeeb } 3216302ce8d6SAndre Oppermann #endif 3217b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3218b287c6c7SBjoern A. Zeeb else 3219b287c6c7SBjoern A. Zeeb #endif 3220b287c6c7SBjoern A. Zeeb #ifdef INET 3221302ce8d6SAndre Oppermann { 3222302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 3223302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3224302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3225302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3226302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3227302ce8d6SAndre Oppermann goto drop; 3228302ce8d6SAndre Oppermann } 3229b287c6c7SBjoern A. Zeeb #endif 3230302ce8d6SAndre Oppermann 3231302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 3232302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 3233302ce8d6SAndre Oppermann goto drop; 3234302ce8d6SAndre Oppermann 3235302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 3236302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 3237302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 3238302ce8d6SAndre Oppermann th->th_ack, TH_RST); 3239302ce8d6SAndre Oppermann } else { 3240302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 3241302ce8d6SAndre Oppermann tlen++; 32424ddd5aadSMichael Tuexen if (th->th_flags & TH_FIN) 32434ddd5aadSMichael Tuexen tlen++; 3244302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 3245302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 3246302ce8d6SAndre Oppermann } 3247302ce8d6SAndre Oppermann return; 3248302ce8d6SAndre Oppermann drop: 3249302ce8d6SAndre Oppermann m_freem(m); 3250df8bae1dSRodney W. Grimes } 3251df8bae1dSRodney W. Grimes 3252be2ac88cSJonathan Lemon /* 3253be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 3254be2ac88cSJonathan Lemon */ 325555bceb1eSRandall Stewart void 3256ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 3257df8bae1dSRodney W. Grimes { 3258df8bae1dSRodney W. Grimes int opt, optlen; 3259df8bae1dSRodney W. Grimes 3260be2ac88cSJonathan Lemon to->to_flags = 0; 3261df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 3262df8bae1dSRodney W. Grimes opt = cp[0]; 3263df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 3264df8bae1dSRodney W. Grimes break; 3265df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 3266df8bae1dSRodney W. Grimes optlen = 1; 3267df8bae1dSRodney W. Grimes else { 3268b474779fSJun-ichiro itojun Hagino if (cnt < 2) 3269b474779fSJun-ichiro itojun Hagino break; 3270df8bae1dSRodney W. Grimes optlen = cp[1]; 3271b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 3272df8bae1dSRodney W. Grimes break; 3273df8bae1dSRodney W. Grimes } 3274df8bae1dSRodney W. Grimes switch (opt) { 3275df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 3276df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 3277df8bae1dSRodney W. Grimes continue; 3278f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3279df8bae1dSRodney W. Grimes continue; 3280be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 3281be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 3282be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 3283fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 3284df8bae1dSRodney W. Grimes break; 3285df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 3286df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 3287df8bae1dSRodney W. Grimes continue; 3288f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3289df8bae1dSRodney W. Grimes continue; 3290be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 329102a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 3292df8bae1dSRodney W. Grimes break; 3293df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 3294df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 3295df8bae1dSRodney W. Grimes continue; 3296be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 3297a0292f23SGarrett Wollman bcopy((char *)cp + 2, 3298a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 3299fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 3300a0292f23SGarrett Wollman bcopy((char *)cp + 6, 3301a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 3302fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 3303df8bae1dSRodney W. Grimes break; 33041cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 3305fcf59617SAndrey V. Elsukov /* 3306fcf59617SAndrey V. Elsukov * In order to reply to a host which has set the 3307fcf59617SAndrey V. Elsukov * TCP_SIGNATURE option in its initial SYN, we have 3308fcf59617SAndrey V. Elsukov * to record the fact that the option was observed 3309fcf59617SAndrey V. Elsukov * here for the syncache code to perform the correct 3310fcf59617SAndrey V. Elsukov * response. 3311fcf59617SAndrey V. Elsukov */ 33121cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 33131cfd4b53SBruce M Simpson continue; 3314df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 3315df47e437SAndre Oppermann to->to_signature = cp + 2; 33161cfd4b53SBruce M Simpson break; 33176d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 3318f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 33196d90faf3SPaul Saab continue; 3320f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3321f72167f4SAndre Oppermann continue; 3322603724d3SBjoern A. Zeeb if (!V_tcp_do_sack) 3323f72167f4SAndre Oppermann continue; 3324fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 33256d90faf3SPaul Saab break; 33266d90faf3SPaul Saab case TCPOPT_SACK: 33275a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 33286d90faf3SPaul Saab continue; 3329b728e902SAndre Oppermann if (flags & TO_SYN) 3330b728e902SAndre Oppermann continue; 3331fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 33325a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 33335a53ca16SPaul Saab to->to_sacks = cp + 2; 333478b50714SRobert Watson TCPSTAT_INC(tcps_sack_rcv_blocks); 33356d90faf3SPaul Saab break; 3336281a0fd4SPatrick Kelsey case TCPOPT_FAST_OPEN: 3337c560df6fSPatrick Kelsey /* 3338c560df6fSPatrick Kelsey * Cookie length validation is performed by the 3339c560df6fSPatrick Kelsey * server side cookie checking code or the client 3340c560df6fSPatrick Kelsey * side cookie cache update code. 3341c560df6fSPatrick Kelsey */ 3342281a0fd4SPatrick Kelsey if (!(flags & TO_SYN)) 3343281a0fd4SPatrick Kelsey continue; 3344c560df6fSPatrick Kelsey if (!V_tcp_fastopen_client_enable && 3345c560df6fSPatrick Kelsey !V_tcp_fastopen_server_enable) 3346281a0fd4SPatrick Kelsey continue; 3347281a0fd4SPatrick Kelsey to->to_flags |= TOF_FASTOPEN; 3348281a0fd4SPatrick Kelsey to->to_tfo_len = optlen - 2; 3349281a0fd4SPatrick Kelsey to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL; 3350281a0fd4SPatrick Kelsey break; 3351be2ac88cSJonathan Lemon default: 3352be2ac88cSJonathan Lemon continue; 3353df8bae1dSRodney W. Grimes } 3354df8bae1dSRodney W. Grimes } 3355df8bae1dSRodney W. Grimes } 3356df8bae1dSRodney W. Grimes 3357df8bae1dSRodney W. Grimes /* 3358df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 3359df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 3360df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 3361df8bae1dSRodney W. Grimes * sequencing purposes. 3362df8bae1dSRodney W. Grimes */ 336355bceb1eSRandall Stewart void 3364ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 3365ad3f9ab3SAndre Oppermann int off) 3366df8bae1dSRodney W. Grimes { 3367fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 3368df8bae1dSRodney W. Grimes 3369df8bae1dSRodney W. Grimes while (cnt >= 0) { 3370df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 3371df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 3372df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 3373df8bae1dSRodney W. Grimes 33748501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 33757a3244ccSRobert Watson 3376df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 3377df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 3378df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 3379df8bae1dSRodney W. Grimes m->m_len--; 338069a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 338169a34685SYoshinobu Inoue m->m_pkthdr.len--; 3382df8bae1dSRodney W. Grimes return; 3383df8bae1dSRodney W. Grimes } 3384df8bae1dSRodney W. Grimes cnt -= m->m_len; 3385df8bae1dSRodney W. Grimes m = m->m_next; 33864dfdffe9SAndre Oppermann if (m == NULL) 3387df8bae1dSRodney W. Grimes break; 3388df8bae1dSRodney W. Grimes } 3389df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 3390df8bae1dSRodney W. Grimes } 3391df8bae1dSRodney W. Grimes 3392df8bae1dSRodney W. Grimes /* 3393df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 3394df8bae1dSRodney W. Grimes * and update averages and current timeout. 3395df8bae1dSRodney W. Grimes */ 339655bceb1eSRandall Stewart void 3397ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 3398df8bae1dSRodney W. Grimes { 3399ad3f9ab3SAndre Oppermann int delta; 3400233e8c18SGarrett Wollman 34018501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 34022be3bf22SRobert Watson 340378b50714SRobert Watson TCPSTAT_INC(tcps_rttupdated); 3404233e8c18SGarrett Wollman tp->t_rttupdated++; 34055ede40dcSRyan Stone if ((tp->t_srtt != 0) && (tp->t_rxtshift <= TCP_RTT_INVALIDATE)) { 3406233e8c18SGarrett Wollman /* 3407233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 3408233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 3409233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 3410233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3411233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 3412233e8c18SGarrett Wollman */ 3413233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3414233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3415233e8c18SGarrett Wollman 3416233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 3417233e8c18SGarrett Wollman tp->t_srtt = 1; 3418233e8c18SGarrett Wollman 3419233e8c18SGarrett Wollman /* 3420233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 3421233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 3422233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 3423233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 3424233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 3425233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 3426233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3427233e8c18SGarrett Wollman * rfc793's wired-in beta. 3428233e8c18SGarrett Wollman */ 3429233e8c18SGarrett Wollman if (delta < 0) 3430233e8c18SGarrett Wollman delta = -delta; 3431233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3432233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 3433233e8c18SGarrett Wollman tp->t_rttvar = 1; 34341fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 34351fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3436233e8c18SGarrett Wollman } else { 3437233e8c18SGarrett Wollman /* 3438233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 3439233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 3440233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 3441233e8c18SGarrett Wollman */ 3442233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 3443233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 34441fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3445233e8c18SGarrett Wollman } 34469b8b58e0SJonathan Lemon tp->t_rtttime = 0; 3447df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 3448df8bae1dSRodney W. Grimes 3449df8bae1dSRodney W. Grimes /* 3450df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 3451df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 3452df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 3453df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 3454df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 3455df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 3456df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 3457df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 3458df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 3459df8bae1dSRodney W. Grimes */ 3460233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 34619e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3462df8bae1dSRodney W. Grimes 3463df8bae1dSRodney W. Grimes /* 3464df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 3465df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 3466df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 3467df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 3468df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 3469df8bae1dSRodney W. Grimes */ 3470df8bae1dSRodney W. Grimes tp->t_softerror = 0; 3471df8bae1dSRodney W. Grimes } 3472df8bae1dSRodney W. Grimes 3473df8bae1dSRodney W. Grimes /* 3474df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 3475df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 34764faaea55SAndre Oppermann * If none, use an mss that can be handled on the outgoing interface 34774faaea55SAndre Oppermann * without forcing IP to fragment. If no route is found, route has no mtu, 3478df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 3479df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 3480df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 3481df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 3482df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 3483df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 3484df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 3485a0292f23SGarrett Wollman * 34860c39d38dSGleb Smirnoff * NOTE that resulting t_maxseg doesn't include space for TCP options or 34870c39d38dSGleb Smirnoff * IP options, e.g. IPSEC data, since length of this data may vary, and 34880c39d38dSGleb Smirnoff * thus it is calculated for every segment separately in tcp_output(). 3489a0292f23SGarrett Wollman * 349097d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 3491ef341ee1SGleb Smirnoff * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS 3492ef341ee1SGleb Smirnoff * settings are handled in tcp_mssopt(). 3493df8bae1dSRodney W. Grimes */ 3494a0292f23SGarrett Wollman void 3495ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer, 34963c914c54SAndre Oppermann struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap) 3497df8bae1dSRodney W. Grimes { 3498b287c6c7SBjoern A. Zeeb int mss = 0; 34993ac12506SJonathan T. Looney uint32_t maxmtu = 0; 3500c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 350197d8d152SAndre Oppermann struct hc_metrics_lite metrics; 3502fb59c426SYoshinobu Inoue #ifdef INET6 3503c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3504c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 3505c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3506c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 3507c068736aSJeffrey Hsu #else 3508c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 3509fb59c426SYoshinobu Inoue #endif 3510df8bae1dSRodney W. Grimes 35118501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 35127a3244ccSRobert Watson 3513ef341ee1SGleb Smirnoff if (mtuoffer != -1) { 3514ef341ee1SGleb Smirnoff KASSERT(offer == -1, ("%s: conflict", __func__)); 3515ef341ee1SGleb Smirnoff offer = mtuoffer - min_protoh; 3516ef341ee1SGleb Smirnoff } 3517ef341ee1SGleb Smirnoff 3518e8949f74SAndre Oppermann /* Initialize. */ 351997d8d152SAndre Oppermann #ifdef INET6 352097d8d152SAndre Oppermann if (isipv6) { 35213c914c54SAndre Oppermann maxmtu = tcp_maxmtu6(&inp->inp_inc, cap); 35220c39d38dSGleb Smirnoff tp->t_maxseg = V_tcp_v6mssdflt; 3523b287c6c7SBjoern A. Zeeb } 352497d8d152SAndre Oppermann #endif 3525b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3526b287c6c7SBjoern A. Zeeb else 3527b287c6c7SBjoern A. Zeeb #endif 3528b287c6c7SBjoern A. Zeeb #ifdef INET 352997d8d152SAndre Oppermann { 35303c914c54SAndre Oppermann maxmtu = tcp_maxmtu(&inp->inp_inc, cap); 35310c39d38dSGleb Smirnoff tp->t_maxseg = V_tcp_mssdflt; 3532df8bae1dSRodney W. Grimes } 3533b287c6c7SBjoern A. Zeeb #endif 3534df8bae1dSRodney W. Grimes 353597d8d152SAndre Oppermann /* 3536e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 353797d8d152SAndre Oppermann */ 35386f01cac6SBjoern A. Zeeb if (maxmtu == 0) { 35396f01cac6SBjoern A. Zeeb /* 35408e5c87f4SBjoern A. Zeeb * In case we return early we need to initialize metrics 35416f01cac6SBjoern A. Zeeb * to a defined state as tcp_hc_get() would do for us 35426f01cac6SBjoern A. Zeeb * if there was no cache hit. 35436f01cac6SBjoern A. Zeeb */ 35446f01cac6SBjoern A. Zeeb if (metricptr != NULL) 35456f01cac6SBjoern A. Zeeb bzero(metricptr, sizeof(struct hc_metrics_lite)); 354697d8d152SAndre Oppermann return; 35476f01cac6SBjoern A. Zeeb } 354897d8d152SAndre Oppermann 3549e8949f74SAndre Oppermann /* What have we got? */ 355097d8d152SAndre Oppermann switch (offer) { 355197d8d152SAndre Oppermann case 0: 355297d8d152SAndre Oppermann /* 355397d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 3554c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 35550c39d38dSGleb Smirnoff * already assigned to t_maxseg above. 355697d8d152SAndre Oppermann */ 35570c39d38dSGleb Smirnoff offer = tp->t_maxseg; 355897d8d152SAndre Oppermann break; 355997d8d152SAndre Oppermann 356097d8d152SAndre Oppermann case -1: 3561a0292f23SGarrett Wollman /* 3562c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 3563a0292f23SGarrett Wollman */ 356497d8d152SAndre Oppermann /* FALLTHROUGH */ 356597d8d152SAndre Oppermann 356697d8d152SAndre Oppermann default: 3567a0292f23SGarrett Wollman /* 356853369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 356953369ac9SAndre Oppermann * to at least minmss. 357053369ac9SAndre Oppermann */ 3571603724d3SBjoern A. Zeeb offer = max(offer, V_tcp_minmss); 357297d8d152SAndre Oppermann } 3573a0292f23SGarrett Wollman 3574df8bae1dSRodney W. Grimes /* 3575e8949f74SAndre Oppermann * rmx information is now retrieved from tcp_hostcache. 3576df8bae1dSRodney W. Grimes */ 357797d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 35783cee92e0SBjoern A. Zeeb if (metricptr != NULL) 35793cee92e0SBjoern A. Zeeb bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 358097d8d152SAndre Oppermann 3581df8bae1dSRodney W. Grimes /* 3582cc412412SHiren Panchasara * If there's a discovered mtu in tcp hostcache, use it. 3583cc412412SHiren Panchasara * Else, use the link mtu. 3584df8bae1dSRodney W. Grimes */ 358597d8d152SAndre Oppermann if (metrics.rmx_mtu) 35862d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3587c068736aSJeffrey Hsu else { 358831b3783cSHajimu UMEMOTO #ifdef INET6 3589fb59c426SYoshinobu Inoue if (isipv6) { 359097d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3591603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 359297d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 3593603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_v6mssdflt); 3594b287c6c7SBjoern A. Zeeb } 3595b3399803SHajimu UMEMOTO #endif 3596b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3597b287c6c7SBjoern A. Zeeb else 3598b287c6c7SBjoern A. Zeeb #endif 3599b287c6c7SBjoern A. Zeeb #ifdef INET 360097d8d152SAndre Oppermann { 360197d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3602603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 360397d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 3604603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_mssdflt); 3605a0292f23SGarrett Wollman } 3606b287c6c7SBjoern A. Zeeb #endif 36073cee92e0SBjoern A. Zeeb /* 36083cee92e0SBjoern A. Zeeb * XXX - The above conditional (mss = maxmtu - min_protoh) 36093cee92e0SBjoern A. Zeeb * probably violates the TCP spec. 36103cee92e0SBjoern A. Zeeb * The problem is that, since we don't know the 36113cee92e0SBjoern A. Zeeb * other end's MSS, we are supposed to use a conservative 36123cee92e0SBjoern A. Zeeb * default. But, if we do that, then MTU discovery will 36133cee92e0SBjoern A. Zeeb * never actually take place, because the conservative 36143cee92e0SBjoern A. Zeeb * default is much less than the MTUs typically seen 36153cee92e0SBjoern A. Zeeb * on the Internet today. For the moment, we'll sweep 36163cee92e0SBjoern A. Zeeb * this under the carpet. 36173cee92e0SBjoern A. Zeeb * 36183cee92e0SBjoern A. Zeeb * The conservative default might not actually be a problem 36193cee92e0SBjoern A. Zeeb * if the only case this occurs is when sending an initial 36203cee92e0SBjoern A. Zeeb * SYN with options and data to a host we've never talked 36213cee92e0SBjoern A. Zeeb * to before. Then, they will reply with an MSS value which 36223cee92e0SBjoern A. Zeeb * will get recorded and the new parameters should get 36233cee92e0SBjoern A. Zeeb * recomputed. For Further Study. 36243cee92e0SBjoern A. Zeeb */ 362597d8d152SAndre Oppermann } 3626a0292f23SGarrett Wollman mss = min(mss, offer); 362797d8d152SAndre Oppermann 3628a0292f23SGarrett Wollman /* 36290c39d38dSGleb Smirnoff * Sanity check: make sure that maxseg will be large 36303cee92e0SBjoern A. Zeeb * enough to allow some data on segments even if the 36313cee92e0SBjoern A. Zeeb * all the option space is used (40bytes). Otherwise 36323cee92e0SBjoern A. Zeeb * funny things may happen in tcp_output. 36330c39d38dSGleb Smirnoff * 36340c39d38dSGleb Smirnoff * XXXGL: shouldn't we reserve space for IP/IPv6 options? 36353cee92e0SBjoern A. Zeeb */ 36363cee92e0SBjoern A. Zeeb mss = max(mss, 64); 36373cee92e0SBjoern A. Zeeb 363897d8d152SAndre Oppermann tp->t_maxseg = mss; 36393cee92e0SBjoern A. Zeeb } 36403cee92e0SBjoern A. Zeeb 36413cee92e0SBjoern A. Zeeb void 36423cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer) 36433cee92e0SBjoern A. Zeeb { 3644dbc42409SLawrence Stewart int mss; 36453ac12506SJonathan T. Looney uint32_t bufsize; 36463cee92e0SBjoern A. Zeeb struct inpcb *inp; 36473cee92e0SBjoern A. Zeeb struct socket *so; 36483cee92e0SBjoern A. Zeeb struct hc_metrics_lite metrics; 36493c914c54SAndre Oppermann struct tcp_ifcap cap; 3650dbc42409SLawrence Stewart 36513cee92e0SBjoern A. Zeeb KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 36523cee92e0SBjoern A. Zeeb 36533c914c54SAndre Oppermann bzero(&cap, sizeof(cap)); 36543c914c54SAndre Oppermann tcp_mss_update(tp, offer, -1, &metrics, &cap); 36553cee92e0SBjoern A. Zeeb 36563cee92e0SBjoern A. Zeeb mss = tp->t_maxseg; 36573cee92e0SBjoern A. Zeeb inp = tp->t_inpcb; 365897d8d152SAndre Oppermann 3659df8bae1dSRodney W. Grimes /* 366097d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 366197d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 366297d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 366397d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 366497d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3665df8bae1dSRodney W. Grimes */ 3666c3b02504SBjoern A. Zeeb so = inp->inp_socket; 36673f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 3668e233e2acSAndre Oppermann if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe) 366997d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 367097d8d152SAndre Oppermann else 3671df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3672df8bae1dSRodney W. Grimes if (bufsize < mss) 3673df8bae1dSRodney W. Grimes mss = bufsize; 3674df8bae1dSRodney W. Grimes else { 3675df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3676df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3677df8bae1dSRodney W. Grimes bufsize = sb_max; 367888c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 36793f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3680df8bae1dSRodney W. Grimes } 36813f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 3682784ce8faSHiren Panchasara /* 3683784ce8faSHiren Panchasara * Sanity check: make sure that maxseg will be large 3684784ce8faSHiren Panchasara * enough to allow some data on segments even if the 3685784ce8faSHiren Panchasara * all the option space is used (40bytes). Otherwise 3686784ce8faSHiren Panchasara * funny things may happen in tcp_output. 3687784ce8faSHiren Panchasara * 3688784ce8faSHiren Panchasara * XXXGL: shouldn't we reserve space for IP/IPv6 options? 3689784ce8faSHiren Panchasara */ 3690784ce8faSHiren Panchasara tp->t_maxseg = max(mss, 64); 3691df8bae1dSRodney W. Grimes 36923f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3693e233e2acSAndre Oppermann if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe) 369497d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 369597d8d152SAndre Oppermann else 3696df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3697df8bae1dSRodney W. Grimes if (bufsize > mss) { 3698df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3699df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3700df8bae1dSRodney W. Grimes bufsize = sb_max; 370188c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 37023f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3703df8bae1dSRodney W. Grimes } 37043f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 370591d6cfa6SBjoern A. Zeeb 370691d6cfa6SBjoern A. Zeeb /* Check the interface for TSO capabilities. */ 37073c914c54SAndre Oppermann if (cap.ifcap & CSUM_TSO) { 370891d6cfa6SBjoern A. Zeeb tp->t_flags |= TF_TSO; 37093c914c54SAndre Oppermann tp->t_tsomax = cap.tsomax; 37109fd573c3SHans Petter Selasky tp->t_tsomaxsegcount = cap.tsomaxsegcount; 37119fd573c3SHans Petter Selasky tp->t_tsomaxsegsize = cap.tsomaxsegsize; 37123c914c54SAndre Oppermann } 3713a0292f23SGarrett Wollman } 3714a0292f23SGarrett Wollman 3715a0292f23SGarrett Wollman /* 3716a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3717a0292f23SGarrett Wollman */ 3718a0292f23SGarrett Wollman int 3719ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 3720a0292f23SGarrett Wollman { 372197d8d152SAndre Oppermann int mss = 0; 37223ac12506SJonathan T. Looney uint32_t thcmtu = 0; 37233ac12506SJonathan T. Looney uint32_t maxmtu = 0; 372497d8d152SAndre Oppermann size_t min_protoh; 3725a0292f23SGarrett Wollman 372697d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3727a0292f23SGarrett Wollman 372831b3783cSHajimu UMEMOTO #ifdef INET6 3729dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3730603724d3SBjoern A. Zeeb mss = V_tcp_v6mssdflt; 3731233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 373297d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 3733b287c6c7SBjoern A. Zeeb } 373431b3783cSHajimu UMEMOTO #endif 3735b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3736b287c6c7SBjoern A. Zeeb else 3737b287c6c7SBjoern A. Zeeb #endif 3738b287c6c7SBjoern A. Zeeb #ifdef INET 373997d8d152SAndre Oppermann { 3740603724d3SBjoern A. Zeeb mss = V_tcp_mssdflt; 3741233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 374297d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 374397d8d152SAndre Oppermann } 3744b287c6c7SBjoern A. Zeeb #endif 37453a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET) 37463a9391deSBjoern A. Zeeb thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 37473a9391deSBjoern A. Zeeb #endif 37483a9391deSBjoern A. Zeeb 374997d8d152SAndre Oppermann if (maxmtu && thcmtu) 375097d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 375197d8d152SAndre Oppermann else if (maxmtu || thcmtu) 375297d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 375397d8d152SAndre Oppermann 375497d8d152SAndre Oppermann return (mss); 3755df8bae1dSRodney W. Grimes } 375646f58482SJonathan Lemon 375746f58482SJonathan Lemon 375846f58482SJonathan Lemon /* 3759c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3760c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3761c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3762c068736aSJeffrey Hsu * be started again. 376346f58482SJonathan Lemon */ 376455bceb1eSRandall Stewart void 3765ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 376646f58482SJonathan Lemon { 376746f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 37683ac12506SJonathan T. Looney uint32_t ocwnd = tp->snd_cwnd; 37690c39d38dSGleb Smirnoff u_int maxseg = tcp_maxseg(tp); 377046f58482SJonathan Lemon 37718501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 37727a3244ccSRobert Watson 3773b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 377446f58482SJonathan Lemon tp->t_rtttime = 0; 377546f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 37766b2a5f92SJayanth Vijayaraghavan /* 3777c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3778c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 37796b2a5f92SJayanth Vijayaraghavan */ 37800c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th); 3781a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 378255bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 378346f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 378446f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 378546f58482SJonathan Lemon tp->snd_nxt = onxt; 378646f58482SJonathan Lemon /* 378746f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 378846f58482SJonathan Lemon * not updated yet. 378946f58482SJonathan Lemon */ 3790dbc42409SLawrence Stewart if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th)) 3791dbc42409SLawrence Stewart tp->snd_cwnd -= BYTES_THIS_ACK(tp, th); 3792d7587117SPaul Saab else 3793d7587117SPaul Saab tp->snd_cwnd = 0; 37940c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 379546f58482SJonathan Lemon } 379612eeb81fSHiren Panchasara 379712eeb81fSHiren Panchasara int 379812eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp) 379912eeb81fSHiren Panchasara { 380012eeb81fSHiren Panchasara return (tp->snd_max - tp->snd_una + 380112eeb81fSHiren Panchasara tp->sackhint.sack_bytes_rexmit - 380212eeb81fSHiren Panchasara tp->sackhint.sacked_bytes); 380312eeb81fSHiren Panchasara } 3804*7dc90a1dSMichael Tuexen 3805*7dc90a1dSMichael Tuexen uint32_t 3806*7dc90a1dSMichael Tuexen tcp_compute_initwnd(uint32_t maxseg) 3807*7dc90a1dSMichael Tuexen { 3808*7dc90a1dSMichael Tuexen /* 3809*7dc90a1dSMichael Tuexen * Calculate the Initial Window, also used as Restart Window 3810*7dc90a1dSMichael Tuexen * 3811*7dc90a1dSMichael Tuexen * RFC5681 Section 3.1 specifies the default conservative values. 3812*7dc90a1dSMichael Tuexen * RFC3390 specifies slightly more aggressive values. 3813*7dc90a1dSMichael Tuexen * RFC6928 increases it to ten segments. 3814*7dc90a1dSMichael Tuexen * Support for user specified value for initial flight size. 3815*7dc90a1dSMichael Tuexen */ 3816*7dc90a1dSMichael Tuexen if (V_tcp_initcwnd_segments) 3817*7dc90a1dSMichael Tuexen return min(V_tcp_initcwnd_segments * maxseg, 3818*7dc90a1dSMichael Tuexen max(2 * maxseg, V_tcp_initcwnd_segments * 1460)); 3819*7dc90a1dSMichael Tuexen else if (V_tcp_do_rfc3390) 3820*7dc90a1dSMichael Tuexen return min(4 * maxseg, max(2 * maxseg, 4380)); 3821*7dc90a1dSMichael Tuexen else { 3822*7dc90a1dSMichael Tuexen /* Per RFC5681 Section 3.1 */ 3823*7dc90a1dSMichael Tuexen if (maxseg > 2190) 3824*7dc90a1dSMichael Tuexen return (2 * maxseg); 3825*7dc90a1dSMichael Tuexen else if (maxseg > 1095) 3826*7dc90a1dSMichael Tuexen return (3 * maxseg); 3827*7dc90a1dSMichael Tuexen else 3828*7dc90a1dSMichael Tuexen return (4 * maxseg); 3829*7dc90a1dSMichael Tuexen } 3830*7dc90a1dSMichael Tuexen } 3831