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 215b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024; 2166df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW, 217eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_max), 0, 2188b615593SMarko Zec "Max size of automatic receive buffer"); 2196741ecf5SAndre Oppermann 220eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb); 22144e33a07SMarko Zec #define tcb6 tcb /* for KAME src sync over BSD*'s */ 22282cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo); 223df8bae1dSRodney W. Grimes 224315e3e38SRobert Watson /* 225bf840a17SGleb Smirnoff * TCP statistics are stored in an array of counter(9)s, which size matches 226bf840a17SGleb Smirnoff * size of struct tcpstat. TCP running connection count is a regular array. 2275923c293SGleb Smirnoff */ 2285da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat); 2295da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat, 2305da0521fSAndrey V. Elsukov tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 231bf840a17SGleb Smirnoff VNET_DEFINE(counter_u64_t, tcps_states[TCP_NSTATES]); 232bf840a17SGleb Smirnoff SYSCTL_COUNTER_U64_ARRAY(_net_inet_tcp, TCPCTL_STATES, states, CTLFLAG_RD | 233d4d32b9fSHans Petter Selasky CTLFLAG_VNET, &VNET_NAME(tcps_states)[0], TCP_NSTATES, 234bf840a17SGleb Smirnoff "TCP connection counts by TCP state"); 235bf840a17SGleb Smirnoff 236bf840a17SGleb Smirnoff static void 237bf840a17SGleb Smirnoff tcp_vnet_init(const void *unused) 238bf840a17SGleb Smirnoff { 239bf840a17SGleb Smirnoff 240f59d975eSGleb Smirnoff COUNTER_ARRAY_ALLOC(V_tcps_states, TCP_NSTATES, M_WAITOK); 241bf840a17SGleb Smirnoff VNET_PCPUSTAT_ALLOC(tcpstat, M_WAITOK); 242bf840a17SGleb Smirnoff } 243bf840a17SGleb Smirnoff VNET_SYSINIT(tcp_vnet_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 244bf840a17SGleb Smirnoff tcp_vnet_init, NULL); 2455923c293SGleb Smirnoff 2465923c293SGleb Smirnoff #ifdef VIMAGE 247bf840a17SGleb Smirnoff static void 248bf840a17SGleb Smirnoff tcp_vnet_uninit(const void *unused) 249bf840a17SGleb Smirnoff { 250bf840a17SGleb Smirnoff 251f59d975eSGleb Smirnoff COUNTER_ARRAY_FREE(V_tcps_states, TCP_NSTATES); 252bf840a17SGleb Smirnoff VNET_PCPUSTAT_FREE(tcpstat); 253bf840a17SGleb Smirnoff } 254bf840a17SGleb Smirnoff VNET_SYSUNINIT(tcp_vnet_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 255bf840a17SGleb Smirnoff tcp_vnet_uninit, NULL); 2565923c293SGleb Smirnoff #endif /* VIMAGE */ 257bf840a17SGleb Smirnoff 2585923c293SGleb Smirnoff /* 259315e3e38SRobert Watson * Kernel module interface for updating tcpstat. The argument is an index 2605923c293SGleb Smirnoff * into tcpstat treated as an array. 261315e3e38SRobert Watson */ 262315e3e38SRobert Watson void 263315e3e38SRobert Watson kmod_tcpstat_inc(int statnum) 264315e3e38SRobert Watson { 265315e3e38SRobert Watson 2665da0521fSAndrey V. Elsukov counter_u64_add(VNET(tcpstat)[statnum], 1); 267315e3e38SRobert Watson } 268315e3e38SRobert Watson 269bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 270dbc42409SLawrence Stewart /* 27139bc9de5SLawrence Stewart * Wrapper for the TCP established input helper hook. 27239bc9de5SLawrence Stewart */ 27355bceb1eSRandall Stewart void 27439bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to) 27539bc9de5SLawrence Stewart { 27639bc9de5SLawrence Stewart struct tcp_hhook_data hhook_data; 27739bc9de5SLawrence Stewart 27839bc9de5SLawrence Stewart if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) { 27939bc9de5SLawrence Stewart hhook_data.tp = tp; 28039bc9de5SLawrence Stewart hhook_data.th = th; 28139bc9de5SLawrence Stewart hhook_data.to = to; 28239bc9de5SLawrence Stewart 28339bc9de5SLawrence Stewart hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data, 28439bc9de5SLawrence Stewart tp->osd); 28539bc9de5SLawrence Stewart } 28639bc9de5SLawrence Stewart } 287bd79708dSJonathan T. Looney #endif 28839bc9de5SLawrence Stewart 28939bc9de5SLawrence Stewart /* 290dbc42409SLawrence Stewart * CC wrapper hook functions 291dbc42409SLawrence Stewart */ 29255bceb1eSRandall Stewart void 2934b7b743cSLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t nsegs, 2944b7b743cSLawrence Stewart uint16_t type) 295f2512ba1SRui Paulo { 296dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 297f2512ba1SRui Paulo 2984b7b743cSLawrence Stewart tp->ccv->nsegs = nsegs; 299dbc42409SLawrence Stewart tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th); 3005b648e79SLawrence Stewart if (tp->snd_cwnd <= tp->snd_wnd) 301dbc42409SLawrence Stewart tp->ccv->flags |= CCF_CWND_LIMITED; 302dbc42409SLawrence Stewart else 303dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_CWND_LIMITED; 304dbc42409SLawrence Stewart 305dbc42409SLawrence Stewart if (type == CC_ACK) { 306dbc42409SLawrence Stewart if (tp->snd_cwnd > tp->snd_ssthresh) { 307dbc42409SLawrence Stewart tp->t_bytes_acked += min(tp->ccv->bytes_this_ack, 3084b7b743cSLawrence Stewart nsegs * V_tcp_abc_l_var * tcp_maxseg(tp)); 309dbc42409SLawrence Stewart if (tp->t_bytes_acked >= tp->snd_cwnd) { 310dbc42409SLawrence Stewart tp->t_bytes_acked -= tp->snd_cwnd; 311dbc42409SLawrence Stewart tp->ccv->flags |= CCF_ABC_SENTAWND; 312dbc42409SLawrence Stewart } 313dbc42409SLawrence Stewart } else { 314dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_ABC_SENTAWND; 315dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 316dbc42409SLawrence Stewart } 317dbc42409SLawrence Stewart } 318dbc42409SLawrence Stewart 319dbc42409SLawrence Stewart if (CC_ALGO(tp)->ack_received != NULL) { 320dbc42409SLawrence Stewart /* XXXLAS: Find a way to live without this */ 321dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 322dbc42409SLawrence Stewart CC_ALGO(tp)->ack_received(tp->ccv, type); 323dbc42409SLawrence Stewart } 324dbc42409SLawrence Stewart } 325dbc42409SLawrence Stewart 32655bceb1eSRandall Stewart void 327dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp) 328dbc42409SLawrence Stewart { 329dbc42409SLawrence Stewart struct hc_metrics_lite metrics; 330dbc42409SLawrence Stewart struct inpcb *inp = tp->t_inpcb; 3310c39d38dSGleb Smirnoff u_int maxseg; 332dbc42409SLawrence Stewart int rtt; 333dbc42409SLawrence Stewart 334dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 335dbc42409SLawrence Stewart 336dbc42409SLawrence Stewart tcp_hc_get(&inp->inp_inc, &metrics); 3370c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 338dbc42409SLawrence Stewart 339dbc42409SLawrence Stewart if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 340dbc42409SLawrence Stewart tp->t_srtt = rtt; 341dbc42409SLawrence Stewart tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 342dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrtt); 343dbc42409SLawrence Stewart if (metrics.rmx_rttvar) { 344dbc42409SLawrence Stewart tp->t_rttvar = metrics.rmx_rttvar; 345dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrttvar); 346dbc42409SLawrence Stewart } else { 347dbc42409SLawrence Stewart /* default variation is +- 1 rtt */ 348dbc42409SLawrence Stewart tp->t_rttvar = 349dbc42409SLawrence Stewart tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 350dbc42409SLawrence Stewart } 351dbc42409SLawrence Stewart TCPT_RANGESET(tp->t_rxtcur, 352dbc42409SLawrence Stewart ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 353dbc42409SLawrence Stewart tp->t_rttmin, TCPTV_REXMTMAX); 354dbc42409SLawrence Stewart } 355dbc42409SLawrence Stewart if (metrics.rmx_ssthresh) { 356dbc42409SLawrence Stewart /* 357dbc42409SLawrence Stewart * There's some sort of gateway or interface 358dbc42409SLawrence Stewart * buffer limit on the path. Use this to set 359a4641f4eSPedro F. Giffuni * the slow start threshold, but set the 360dbc42409SLawrence Stewart * threshold to no less than 2*mss. 361dbc42409SLawrence Stewart */ 3620c39d38dSGleb Smirnoff tp->snd_ssthresh = max(2 * maxseg, metrics.rmx_ssthresh); 363dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedssthresh); 364dbc42409SLawrence Stewart } 365dbc42409SLawrence Stewart 366dbc42409SLawrence Stewart /* 3679ec4a4ccSAndre Oppermann * Set the initial slow-start flight size. 368dbc42409SLawrence Stewart * 369cf8f04f4SAndre Oppermann * If a SYN or SYN/ACK was lost and retransmitted, we have to 370cf8f04f4SAndre Oppermann * reduce the initial CWND to one segment as congestion is likely 371cf8f04f4SAndre Oppermann * requiring us to be cautious. 372dbc42409SLawrence Stewart */ 373cf8f04f4SAndre Oppermann if (tp->snd_cwnd == 1) 3740c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; /* SYN(-ACK) lost */ 375dbc42409SLawrence Stewart else 3767dc90a1dSMichael Tuexen tp->snd_cwnd = tcp_compute_initwnd(maxseg); 377dbc42409SLawrence Stewart 378dbc42409SLawrence Stewart if (CC_ALGO(tp)->conn_init != NULL) 379dbc42409SLawrence Stewart CC_ALGO(tp)->conn_init(tp->ccv); 380dbc42409SLawrence Stewart } 381dbc42409SLawrence Stewart 382dbc42409SLawrence Stewart void inline 383dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type) 384dbc42409SLawrence Stewart { 3850c39d38dSGleb Smirnoff u_int maxseg; 3860c39d38dSGleb Smirnoff 387dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 388dbc42409SLawrence Stewart 389dbc42409SLawrence Stewart switch(type) { 390dbc42409SLawrence Stewart case CC_NDUPACK: 391dbc42409SLawrence Stewart if (!IN_FASTRECOVERY(tp->t_flags)) { 392f2512ba1SRui Paulo tp->snd_recover = tp->snd_max; 393f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) 394f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_CWR; 395f2512ba1SRui Paulo } 396dbc42409SLawrence Stewart break; 397dbc42409SLawrence Stewart case CC_ECN: 398dbc42409SLawrence Stewart if (!IN_CONGRECOVERY(tp->t_flags)) { 399dbc42409SLawrence Stewart TCPSTAT_INC(tcps_ecn_rcwnd); 400dbc42409SLawrence Stewart tp->snd_recover = tp->snd_max; 401dbc42409SLawrence Stewart if (tp->t_flags & TF_ECN_PERMIT) 402dbc42409SLawrence Stewart tp->t_flags |= TF_ECN_SND_CWR; 403dbc42409SLawrence Stewart } 404dbc42409SLawrence Stewart break; 405dbc42409SLawrence Stewart case CC_RTO: 4060c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 407dbc42409SLawrence Stewart tp->t_dupacks = 0; 408dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 409dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 41043053c12SSean Bruno tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 / 41143053c12SSean Bruno maxseg) * maxseg; 4120c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 413dbc42409SLawrence Stewart break; 414dbc42409SLawrence Stewart case CC_RTO_ERR: 415dbc42409SLawrence Stewart TCPSTAT_INC(tcps_sndrexmitbad); 416dbc42409SLawrence Stewart /* RTO was unnecessary, so reset everything. */ 417dbc42409SLawrence Stewart tp->snd_cwnd = tp->snd_cwnd_prev; 418dbc42409SLawrence Stewart tp->snd_ssthresh = tp->snd_ssthresh_prev; 419dbc42409SLawrence Stewart tp->snd_recover = tp->snd_recover_prev; 420dbc42409SLawrence Stewart if (tp->t_flags & TF_WASFRECOVERY) 421dbc42409SLawrence Stewart ENTER_FASTRECOVERY(tp->t_flags); 422dbc42409SLawrence Stewart if (tp->t_flags & TF_WASCRECOVERY) 423dbc42409SLawrence Stewart ENTER_CONGRECOVERY(tp->t_flags); 424dbc42409SLawrence Stewart tp->snd_nxt = tp->snd_max; 425672dc4aeSJohn Baldwin tp->t_flags &= ~TF_PREVVALID; 426dbc42409SLawrence Stewart tp->t_badrxtwin = 0; 427dbc42409SLawrence Stewart break; 428dbc42409SLawrence Stewart } 429dbc42409SLawrence Stewart 430dbc42409SLawrence Stewart if (CC_ALGO(tp)->cong_signal != NULL) { 431dbc42409SLawrence Stewart if (th != NULL) 432dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 433dbc42409SLawrence Stewart CC_ALGO(tp)->cong_signal(tp->ccv, type); 434dbc42409SLawrence Stewart } 435dbc42409SLawrence Stewart } 436dbc42409SLawrence Stewart 43755bceb1eSRandall Stewart void inline 438dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th) 439dbc42409SLawrence Stewart { 440dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 441dbc42409SLawrence Stewart 442dbc42409SLawrence Stewart /* XXXLAS: KASSERT that we're in recovery? */ 443dbc42409SLawrence Stewart 444dbc42409SLawrence Stewart if (CC_ALGO(tp)->post_recovery != NULL) { 445dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 446dbc42409SLawrence Stewart CC_ALGO(tp)->post_recovery(tp->ccv); 447dbc42409SLawrence Stewart } 448dbc42409SLawrence Stewart /* XXXLAS: EXIT_RECOVERY ? */ 449dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 450dbc42409SLawrence Stewart } 4510312fbe9SPoul-Henning Kamp 452df8bae1dSRodney W. Grimes /* 453262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 45485536381SHiren Panchasara * following conditions are met: 45585536381SHiren Panchasara * - There is no delayed ack timer in progress. 45685536381SHiren Panchasara * - Our last ack wasn't a 0-sized window. We never want to delay 45785536381SHiren Panchasara * the ack that opens up a 0-sized window. 45885536381SHiren Panchasara * - LRO wasn't used for this segment. We make sure by checking that the 45985536381SHiren Panchasara * segment size is not larger than the MSS. 460d8c85a26SJonathan Lemon */ 461c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen) \ 462b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 463a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 4640c39d38dSGleb Smirnoff (tlen <= tp->t_maxseg) && \ 465603724d3SBjoern A. Zeeb (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 466d8c85a26SJonathan Lemon 46764807b30SHiren Panchasara static void inline 46864807b30SHiren Panchasara cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos) 46964807b30SHiren Panchasara { 47064807b30SHiren Panchasara INP_WLOCK_ASSERT(tp->t_inpcb); 47164807b30SHiren Panchasara 47264807b30SHiren Panchasara if (CC_ALGO(tp)->ecnpkt_handler != NULL) { 47364807b30SHiren Panchasara switch (iptos & IPTOS_ECN_MASK) { 47464807b30SHiren Panchasara case IPTOS_ECN_CE: 47564807b30SHiren Panchasara tp->ccv->flags |= CCF_IPHDR_CE; 47664807b30SHiren Panchasara break; 47764807b30SHiren Panchasara case IPTOS_ECN_ECT0: 47864807b30SHiren Panchasara tp->ccv->flags &= ~CCF_IPHDR_CE; 47964807b30SHiren Panchasara break; 48064807b30SHiren Panchasara case IPTOS_ECN_ECT1: 48164807b30SHiren Panchasara tp->ccv->flags &= ~CCF_IPHDR_CE; 48264807b30SHiren Panchasara break; 48364807b30SHiren Panchasara } 48464807b30SHiren Panchasara 48564807b30SHiren Panchasara if (th->th_flags & TH_CWR) 48664807b30SHiren Panchasara tp->ccv->flags |= CCF_TCPHDR_CWR; 48764807b30SHiren Panchasara else 48864807b30SHiren Panchasara tp->ccv->flags &= ~CCF_TCPHDR_CWR; 48964807b30SHiren Panchasara 49064807b30SHiren Panchasara if (tp->t_flags & TF_DELACK) 49164807b30SHiren Panchasara tp->ccv->flags |= CCF_DELACK; 49264807b30SHiren Panchasara else 49364807b30SHiren Panchasara tp->ccv->flags &= ~CCF_DELACK; 49464807b30SHiren Panchasara 49564807b30SHiren Panchasara CC_ALGO(tp)->ecnpkt_handler(tp->ccv); 49664807b30SHiren Panchasara 49764807b30SHiren Panchasara if (tp->ccv->flags & CCF_ACKNOW) 49864807b30SHiren Panchasara tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 49964807b30SHiren Panchasara } 50064807b30SHiren Panchasara } 50164807b30SHiren Panchasara 502df8bae1dSRodney W. Grimes /* 5038d573cc1SAndre Oppermann * TCP input handling is split into multiple parts: 5048d573cc1SAndre Oppermann * tcp6_input is a thin wrapper around tcp_input for the extended 5058d573cc1SAndre Oppermann * ip6_protox[] call format in ip6_input 5068d573cc1SAndre Oppermann * tcp_input handles primary segment validation, inpcb lookup and 5078d573cc1SAndre Oppermann * SYN processing on listen sockets 5088d573cc1SAndre Oppermann * tcp_do_segment processes the ACK and text of the segment for 5098d573cc1SAndre Oppermann * establishing, established and closing connections 510df8bae1dSRodney W. Grimes */ 511fb59c426SYoshinobu Inoue #ifdef INET6 512fb59c426SYoshinobu Inoue int 513ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto) 514fb59c426SYoshinobu Inoue { 515ad3f9ab3SAndre Oppermann struct mbuf *m = *mp; 51633841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 5173e88eb90SAndrey V. Elsukov struct ip6_hdr *ip6; 518fb59c426SYoshinobu Inoue 519fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 520fb59c426SYoshinobu Inoue 521fb59c426SYoshinobu Inoue /* 522fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 523fb59c426SYoshinobu Inoue * better place to put this in? 524fb59c426SYoshinobu Inoue */ 5253e88eb90SAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 5263e88eb90SAndrey V. Elsukov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */); 52733841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 528fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 529fb59c426SYoshinobu Inoue 5308c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 531fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 532fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 533fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 5348f5a8818SKevin Lo return (IPPROTO_DONE); 535fb59c426SYoshinobu Inoue } 53677d396fdSMaksim Yevmenkin if (ia6) 53777d396fdSMaksim Yevmenkin ifa_free(&ia6->ia_ifa); 538fb59c426SYoshinobu Inoue 5398f5a8818SKevin Lo return (tcp_input(mp, offp, proto)); 540fb59c426SYoshinobu Inoue } 541b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 542fb59c426SYoshinobu Inoue 5438f5a8818SKevin Lo int 5448f5a8818SKevin Lo tcp_input(struct mbuf **mp, int *offp, int proto) 545df8bae1dSRodney W. Grimes { 5468f5a8818SKevin Lo struct mbuf *m = *mp; 547b287c6c7SBjoern A. Zeeb struct tcphdr *th = NULL; 548ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 549ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 550302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 551302ce8d6SAndre Oppermann struct socket *so = NULL; 552e79adb8eSGarrett Wollman u_char *optp = NULL; 5538f5a8818SKevin Lo int off0; 55426f9a767SRodney W. Grimes int optlen = 0; 555b287c6c7SBjoern A. Zeeb #ifdef INET 556b287c6c7SBjoern A. Zeeb int len; 557b287c6c7SBjoern A. Zeeb #endif 558b287c6c7SBjoern A. Zeeb int tlen = 0, off; 559fb59c426SYoshinobu Inoue int drop_hdrlen; 560ad3f9ab3SAndre Oppermann int thflags; 561302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 5621d7ee746SKurt Lidl uint8_t iptos; 563c1de64a4SAndrey V. Elsukov struct m_tag *fwd_tag = NULL; 5646573d758SMatt Macy struct epoch_tracker et; 565c068736aSJeffrey Hsu #ifdef INET6 566302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 567c068736aSJeffrey Hsu int isipv6; 568c068736aSJeffrey Hsu #else 569a160e630SAndre Oppermann const void *ip6 = NULL; 570b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 571302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 572a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 573252ca428SRobert Watson int ti_locked; 574610ee2f9SDavid Greenman #ifdef TCPDEBUG 575c068736aSJeffrey Hsu /* 576c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 577c068736aSJeffrey Hsu * now IPv6. 578c068736aSJeffrey Hsu */ 57914739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 580410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 581610ee2f9SDavid Greenman short ostate = 0; 582610ee2f9SDavid Greenman #endif 583c068736aSJeffrey Hsu 584fb59c426SYoshinobu Inoue #ifdef INET6 585fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 586fb59c426SYoshinobu Inoue #endif 587a0292f23SGarrett Wollman 5888f5a8818SKevin Lo off0 = *offp; 5898f5a8818SKevin Lo m = *mp; 5908f5a8818SKevin Lo *mp = NULL; 591302ce8d6SAndre Oppermann to.to_flags = 0; 59278b50714SRobert Watson TCPSTAT_INC(tcps_rcvtotal); 593fb59c426SYoshinobu Inoue 59404d3a452SHajimu UMEMOTO #ifdef INET6 595b287c6c7SBjoern A. Zeeb if (isipv6) { 5968d573cc1SAndre Oppermann /* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */ 59745747ba5SBjoern A. Zeeb 59845747ba5SBjoern A. Zeeb if (m->m_len < (sizeof(*ip6) + sizeof(*th))) { 59945747ba5SBjoern A. Zeeb m = m_pullup(m, sizeof(*ip6) + sizeof(*th)); 60045747ba5SBjoern A. Zeeb if (m == NULL) { 60145747ba5SBjoern A. Zeeb TCPSTAT_INC(tcps_rcvshort); 6028f5a8818SKevin Lo return (IPPROTO_DONE); 60345747ba5SBjoern A. Zeeb } 60445747ba5SBjoern A. Zeeb } 60545747ba5SBjoern A. Zeeb 606fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 60745747ba5SBjoern A. Zeeb th = (struct tcphdr *)((caddr_t)ip6 + off0); 608fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 609356ab07eSBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) { 61045747ba5SBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 61145747ba5SBjoern A. Zeeb th->th_sum = m->m_pkthdr.csum_data; 61245747ba5SBjoern A. Zeeb else 61345747ba5SBjoern A. Zeeb th->th_sum = in6_cksum_pseudo(ip6, tlen, 61445747ba5SBjoern A. Zeeb IPPROTO_TCP, m->m_pkthdr.csum_data); 61545747ba5SBjoern A. Zeeb th->th_sum ^= 0xffff; 61645747ba5SBjoern A. Zeeb } else 61745747ba5SBjoern A. Zeeb th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen); 61845747ba5SBjoern A. Zeeb if (th->th_sum) { 61978b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 620fb59c426SYoshinobu Inoue goto drop; 621fb59c426SYoshinobu Inoue } 62233841545SHajimu UMEMOTO 62333841545SHajimu UMEMOTO /* 62433841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 62533841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 62633841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 62733841545SHajimu UMEMOTO * 62833841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 62933841545SHajimu UMEMOTO * already dropped in ip6_input. 63033841545SHajimu UMEMOTO */ 63133841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 63233841545SHajimu UMEMOTO /* XXX stat */ 63333841545SHajimu UMEMOTO goto drop; 63433841545SHajimu UMEMOTO } 6351d7ee746SKurt Lidl iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 636b287c6c7SBjoern A. Zeeb } 63704d3a452SHajimu UMEMOTO #endif 638b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 639b287c6c7SBjoern A. Zeeb else 640b287c6c7SBjoern A. Zeeb #endif 641b287c6c7SBjoern A. Zeeb #ifdef INET 642b287c6c7SBjoern A. Zeeb { 643df8bae1dSRodney W. Grimes /* 644df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 645df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 646df8bae1dSRodney W. Grimes */ 647fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 648105bd211SGleb Smirnoff ip_stripoptions(m); 649fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 650fb59c426SYoshinobu Inoue } 651df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 6524dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 6534dfdffe9SAndre Oppermann == NULL) { 65478b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 6558f5a8818SKevin Lo return (IPPROTO_DONE); 656df8bae1dSRodney W. Grimes } 657df8bae1dSRodney W. Grimes } 658fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 659db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 6608ad458a4SGleb Smirnoff tlen = ntohs(ip->ip_len) - off0; 661df8bae1dSRodney W. Grimes 6621d7ee746SKurt Lidl iptos = ip->ip_tos; 663db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 664db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 665db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 666db4f9cc7SJonathan Lemon else 667db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 668c068736aSJeffrey Hsu ip->ip_dst.s_addr, 6698f134647SGleb Smirnoff htonl(m->m_pkthdr.csum_data + tlen + 670c068736aSJeffrey Hsu IPPROTO_TCP)); 671db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 672db4f9cc7SJonathan Lemon } else { 6738f134647SGleb Smirnoff struct ipovly *ipov = (struct ipovly *)ip; 6748f134647SGleb Smirnoff 675df8bae1dSRodney W. Grimes /* 676df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 677df8bae1dSRodney W. Grimes */ 6788f134647SGleb Smirnoff len = off0 + tlen; 679fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 6808f134647SGleb Smirnoff ipov->ih_len = htons(tlen); 681fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 68257f60867SMark Johnston /* Reset length for SDT probes. */ 6831d7ee746SKurt Lidl ip->ip_len = htons(len); 6841d7ee746SKurt Lidl /* Reset TOS bits */ 6851d7ee746SKurt Lidl ip->ip_tos = iptos; 6861d7ee746SKurt Lidl /* Re-initialization for later version check */ 6871d7ee746SKurt Lidl ip->ip_v = IPVERSION; 688b2fe54bcSAndrey V. Elsukov ip->ip_hl = off0 >> 2; 689db4f9cc7SJonathan Lemon } 69057f60867SMark Johnston 691fb59c426SYoshinobu Inoue if (th->th_sum) { 69278b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 693df8bae1dSRodney W. Grimes goto drop; 694df8bae1dSRodney W. Grimes } 695fb59c426SYoshinobu Inoue } 696b287c6c7SBjoern A. Zeeb #endif /* INET */ 697df8bae1dSRodney W. Grimes 698df8bae1dSRodney W. Grimes /* 699df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 700df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 701df8bae1dSRodney W. Grimes */ 702fb59c426SYoshinobu Inoue off = th->th_off << 2; 703df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 70478b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadoff); 705df8bae1dSRodney W. Grimes goto drop; 706df8bae1dSRodney W. Grimes } 707fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 708df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 70904d3a452SHajimu UMEMOTO #ifdef INET6 710b287c6c7SBjoern A. Zeeb if (isipv6) { 7118f5a8818SKevin Lo IP6_EXTHDR_CHECK(m, off0, off, IPPROTO_DONE); 712fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 713fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 714b287c6c7SBjoern A. Zeeb } 71504d3a452SHajimu UMEMOTO #endif 716b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 717b287c6c7SBjoern A. Zeeb else 718b287c6c7SBjoern A. Zeeb #endif 719b287c6c7SBjoern A. Zeeb #ifdef INET 720b287c6c7SBjoern A. Zeeb { 721df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 722c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 7234dfdffe9SAndre Oppermann == NULL) { 72478b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 7258f5a8818SKevin Lo return (IPPROTO_DONE); 726df8bae1dSRodney W. Grimes } 727fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 728fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 729fb59c426SYoshinobu Inoue } 730df8bae1dSRodney W. Grimes } 731b287c6c7SBjoern A. Zeeb #endif 732df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 733fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 734df8bae1dSRodney W. Grimes } 735fb59c426SYoshinobu Inoue thflags = th->th_flags; 736df8bae1dSRodney W. Grimes 737e46cd3d4SDag-Erling Smørgrav /* 738df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 739df8bae1dSRodney W. Grimes */ 7402903309aSAttilio Rao tcp_fields_to_host(th); 741df8bae1dSRodney W. Grimes 742df8bae1dSRodney W. Grimes /* 743794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 744755c1f07SAndras Olah */ 745fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 746755c1f07SAndras Olah 747755c1f07SAndras Olah /* 748fa046d87SRobert Watson * Locate pcb for segment; if we're likely to add or remove a 749a7c7f2a7SJohn Baldwin * connection then first acquire pcbinfo lock. There are three cases 750fa046d87SRobert Watson * where we might discover later we need a write lock despite the 751a7c7f2a7SJohn Baldwin * flags: ACKs moving a connection out of the syncache, ACKs for a 752a7c7f2a7SJohn Baldwin * connection in TIMEWAIT and SYNs not targeting a listening socket. 753df8bae1dSRodney W. Grimes */ 754a7c7f2a7SJohn Baldwin if ((thflags & (TH_FIN | TH_RST)) != 0) { 7556573d758SMatt Macy INP_INFO_RLOCK_ET(&V_tcbinfo, et); 756ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 757fa046d87SRobert Watson } else 758fa046d87SRobert Watson ti_locked = TI_UNLOCKED; 759252ca428SRobert Watson 7608d573cc1SAndre Oppermann /* 7618d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 7628d573cc1SAndre Oppermann */ 763b8056faeSGleb Smirnoff if ( 764b8056faeSGleb Smirnoff #ifdef INET6 765b8056faeSGleb Smirnoff (isipv6 && (m->m_flags & M_IP6_NEXTHOP)) 766b8056faeSGleb Smirnoff #ifdef INET 767b8056faeSGleb Smirnoff || (!isipv6 && (m->m_flags & M_IP_NEXTHOP)) 768b8056faeSGleb Smirnoff #endif 769b8056faeSGleb Smirnoff #endif 770b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6) 771b8056faeSGleb Smirnoff (m->m_flags & M_IP_NEXTHOP) 772b8056faeSGleb Smirnoff #endif 773b8056faeSGleb Smirnoff ) 7749b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 7759b932e9eSAndre Oppermann 776ce7ad664SEd Maste findpcb: 777ce7ad664SEd Maste #ifdef INVARIANTS 778ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 779ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 780ce7ad664SEd Maste } else { 781384a5c3cSAndrey V. Elsukov INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo); 782ce7ad664SEd Maste } 783ce7ad664SEd Maste #endif 7848a006adbSBjoern A. Zeeb #ifdef INET6 7858a006adbSBjoern A. Zeeb if (isipv6 && fwd_tag != NULL) { 7868a006adbSBjoern A. Zeeb struct sockaddr_in6 *next_hop6; 7878a006adbSBjoern A. Zeeb 7888a006adbSBjoern A. Zeeb next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1); 7898a006adbSBjoern A. Zeeb /* 7908a006adbSBjoern A. Zeeb * Transparently forwarded. Pretend to be the destination. 7918a006adbSBjoern A. Zeeb * Already got one like this? 7928a006adbSBjoern A. Zeeb */ 7938a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, 7948a006adbSBjoern A. Zeeb &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport, 7958a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m); 7968a006adbSBjoern A. Zeeb if (!inp) { 7978a006adbSBjoern A. Zeeb /* 7988a006adbSBjoern A. Zeeb * It's new. Try to find the ambushing socket. 7998a006adbSBjoern A. Zeeb * Because we've rewritten the destination address, 8008a006adbSBjoern A. Zeeb * any hardware-generated hash is ignored. 8018a006adbSBjoern A. Zeeb */ 8028a006adbSBjoern A. Zeeb inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src, 8038a006adbSBjoern A. Zeeb th->th_sport, &next_hop6->sin6_addr, 8048a006adbSBjoern A. Zeeb next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) : 8058a006adbSBjoern A. Zeeb th->th_dport, INPLOOKUP_WILDCARD | 8068a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 8078a006adbSBjoern A. Zeeb } 808c1de64a4SAndrey V. Elsukov } else if (isipv6) { 8098a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src, 8108a006adbSBjoern A. Zeeb th->th_sport, &ip6->ip6_dst, th->th_dport, 8118a006adbSBjoern A. Zeeb INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 8128a006adbSBjoern A. Zeeb m->m_pkthdr.rcvif, m); 8138a006adbSBjoern A. Zeeb } 8148a006adbSBjoern A. Zeeb #endif /* INET6 */ 8158a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET) 8168a006adbSBjoern A. Zeeb else 8178a006adbSBjoern A. Zeeb #endif 8188a006adbSBjoern A. Zeeb #ifdef INET 8198a006adbSBjoern A. Zeeb if (fwd_tag != NULL) { 8209b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 8219b932e9eSAndre Oppermann 8229b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 823f9e354dfSJulian Elischer /* 8242b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 825f9e354dfSJulian Elischer * already got one like this? 826f9e354dfSJulian Elischer */ 827d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport, 828fa046d87SRobert Watson ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB, 829d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 830fa046d87SRobert Watson if (!inp) { 831fa046d87SRobert Watson /* 832fa046d87SRobert Watson * It's new. Try to find the ambushing socket. 833d3c1f003SRobert Watson * Because we've rewritten the destination address, 834d3c1f003SRobert Watson * any hardware-generated hash is ignored. 835fa046d87SRobert Watson */ 836fa046d87SRobert Watson inp = in_pcblookup(&V_tcbinfo, ip->ip_src, 837fa046d87SRobert Watson th->th_sport, next_hop->sin_addr, 838fa046d87SRobert Watson next_hop->sin_port ? ntohs(next_hop->sin_port) : 839fa046d87SRobert Watson th->th_dport, INPLOOKUP_WILDCARD | 840fa046d87SRobert Watson INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 841f9e354dfSJulian Elischer } 842b10fbdeaSAndre Oppermann } else 843d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, 844fa046d87SRobert Watson th->th_sport, ip->ip_dst, th->th_dport, 845fa046d87SRobert Watson INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 846d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 8478a006adbSBjoern A. Zeeb #endif /* INET */ 848f9e354dfSJulian Elischer 849df8bae1dSRodney W. Grimes /* 850574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 851574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 8528b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 853df8bae1dSRodney W. Grimes */ 854816a3d83SPoul-Henning Kamp if (inp == NULL) { 855574b6964SAndre Oppermann /* 856574b6964SAndre Oppermann * Log communication attempts to ports that are not 857574b6964SAndre Oppermann * in use. 858574b6964SAndre Oppermann */ 859574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 860574b6964SAndre Oppermann tcp_log_in_vain == 2) { 861b7d747ecSAndre Oppermann if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6))) 862df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 863df541e5fSAndre Oppermann "to closed port\n", s, __func__); 8642e4e1b4cSGeoff Rehmet } 865574b6964SAndre Oppermann /* 866574b6964SAndre Oppermann * When blackholing do not respond with a RST but 867574b6964SAndre Oppermann * completely ignore the segment and drop it. 868574b6964SAndre Oppermann */ 869603724d3SBjoern A. Zeeb if ((V_blackhole == 1 && (thflags & TH_SYN)) || 870603724d3SBjoern A. Zeeb V_blackhole == 2) 8711929eae1SAndre Oppermann goto dropunlock; 872574b6964SAndre Oppermann 873a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 874a57815efSBosko Milekic goto dropwithreset; 875816a3d83SPoul-Henning Kamp } 876fa046d87SRobert Watson INP_WLOCK_ASSERT(inp); 877f5cf1e5fSJulien Charbon /* 878f5cf1e5fSJulien Charbon * While waiting for inp lock during the lookup, another thread 879f5cf1e5fSJulien Charbon * can have dropped the inpcb, in which case we need to loop back 880f5cf1e5fSJulien Charbon * and try to find a new inpcb to deliver to. 881f5cf1e5fSJulien Charbon */ 882f5cf1e5fSJulien Charbon if (inp->inp_flags & INP_DROPPED) { 883f5cf1e5fSJulien Charbon INP_WUNLOCK(inp); 884f5cf1e5fSJulien Charbon inp = NULL; 885f5cf1e5fSJulien Charbon goto findpcb; 886f5cf1e5fSJulien Charbon } 887c2529042SHans Petter Selasky if ((inp->inp_flowtype == M_HASHTYPE_NONE) && 888c2529042SHans Petter Selasky (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) && 889c2529042SHans Petter Selasky ((inp->inp_socket == NULL) || 890c2529042SHans Petter Selasky (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0)) { 89180cb9f21SKip Macy inp->inp_flowid = m->m_pkthdr.flowid; 8922f719932SAdrian Chadd inp->inp_flowtype = M_HASHTYPE_GET(m); 89380cb9f21SKip Macy } 894fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 895a2589465SKen Smith #ifdef INET6 896fcf59617SAndrey V. Elsukov if (isipv6 && IPSEC_ENABLED(ipv6) && 897fcf59617SAndrey V. Elsukov IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) { 898a2589465SKen Smith goto dropunlock; 899a2589465SKen Smith } 900fcf59617SAndrey V. Elsukov #ifdef INET 901fcf59617SAndrey V. Elsukov else 902fcf59617SAndrey V. Elsukov #endif 903fcf59617SAndrey V. Elsukov #endif /* INET6 */ 904fcf59617SAndrey V. Elsukov #ifdef INET 905fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4) && 906fcf59617SAndrey V. Elsukov IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) { 907fcf59617SAndrey V. Elsukov goto dropunlock; 908fcf59617SAndrey V. Elsukov } 909fcf59617SAndrey V. Elsukov #endif /* INET */ 910a2589465SKen Smith #endif /* IPSEC */ 911a2589465SKen Smith 9128d573cc1SAndre Oppermann /* 9138d573cc1SAndre Oppermann * Check the minimum TTL for socket. 9148d573cc1SAndre Oppermann */ 91534f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 91634f83c52SGeorge V. Neville-Neil #ifdef INET6 9172d8868dbSJonathan T. Looney if (isipv6) { 9182d8868dbSJonathan T. Looney if (inp->inp_ip_minttl > ip6->ip6_hlim) 919302ce8d6SAndre Oppermann goto dropunlock; 9202d8868dbSJonathan T. Looney } else 92134f83c52SGeorge V. Neville-Neil #endif 92234f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 923302ce8d6SAndre Oppermann goto dropunlock; 92434f83c52SGeorge V. Neville-Neil } 925936cd18dSAndre Oppermann 926340c35deSJonathan Lemon /* 927252ca428SRobert Watson * A previous connection in TIMEWAIT state is supposed to catch stray 928252ca428SRobert Watson * or duplicate segments arriving late. If this segment was a 9290989f56cSRobert Watson * legitimate new connection attempt, the old INPCB gets removed and 930252ca428SRobert Watson * we can try again to find a listening socket. 931252ca428SRobert Watson * 932fa046d87SRobert Watson * At this point, due to earlier optimism, we may hold only an inpcb 933fa046d87SRobert Watson * lock, and not the inpcbinfo write lock. If so, we need to try to 934fa046d87SRobert Watson * acquire it, or if that fails, acquire a reference on the inpcb, 935fa046d87SRobert Watson * drop all locks, acquire a global write lock, and then re-acquire 936fa046d87SRobert Watson * the inpcb lock. We may at that point discover that another thread 937fa046d87SRobert Watson * has tried to free the inpcb, in which case we need to loop back 938fa046d87SRobert Watson * and try to find a new inpcb to deliver to. 939fa046d87SRobert Watson * 940fa046d87SRobert Watson * XXXRW: It may be time to rethink timewait locking. 941340c35deSJonathan Lemon */ 942ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 943fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 9446573d758SMatt Macy INP_INFO_RLOCK_ET(&V_tcbinfo, et); 945ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 946252ca428SRobert Watson } 947ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 948252ca428SRobert Watson 949340c35deSJonathan Lemon if (thflags & TH_SYN) 950f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 9518d573cc1SAndre Oppermann /* 9528d573cc1SAndre Oppermann * NB: tcp_twcheck unlocks the INP and frees the mbuf. 9538d573cc1SAndre Oppermann */ 9542104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 955340c35deSJonathan Lemon goto findpcb; 9566573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 9578f5a8818SKevin Lo return (IPPROTO_DONE); 958340c35deSJonathan Lemon } 959794235b7SAndre Oppermann /* 960794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 961794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 962794235b7SAndre Oppermann * segment and send an appropriate response. 963794235b7SAndre Oppermann */ 964df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 965a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 966a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 967a57815efSBosko Milekic goto dropwithreset; 96809f81a46SBosko Milekic } 969df8bae1dSRodney W. Grimes 97009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 97109fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 97209fe6320SNavdeep Parhar tcp_offload_input(tp, m); 97309fe6320SNavdeep Parhar m = NULL; /* consumed by the TOE driver */ 97409fe6320SNavdeep Parhar goto dropunlock; 97509fe6320SNavdeep Parhar } 97609fe6320SNavdeep Parhar #endif 97709fe6320SNavdeep Parhar 978252ca428SRobert Watson /* 979252ca428SRobert Watson * We've identified a valid inpcb, but it could be that we need an 980fa046d87SRobert Watson * inpcbinfo write lock but don't hold it. In this case, attempt to 981fa046d87SRobert Watson * acquire using the same strategy as the TIMEWAIT case above. If we 982fa046d87SRobert Watson * relock, we have to jump back to 'relocked' as the connection might 983fa046d87SRobert Watson * now be in TIMEWAIT. 984252ca428SRobert Watson */ 985fa046d87SRobert Watson #ifdef INVARIANTS 986a7c7f2a7SJohn Baldwin if ((thflags & (TH_FIN | TH_RST)) != 0) 987ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 988fa046d87SRobert Watson #endif 989a7c7f2a7SJohn Baldwin if (!((tp->t_state == TCPS_ESTABLISHED && (thflags & TH_SYN) == 0) || 990281a0fd4SPatrick Kelsey (tp->t_state == TCPS_LISTEN && (thflags & TH_SYN) && 99168bd7ed1SJonathan T. Looney !IS_FASTOPEN(tp->t_flags)))) { 992fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 9936573d758SMatt Macy INP_INFO_RLOCK_ET(&V_tcbinfo, et); 994ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 995252ca428SRobert Watson } 996ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 997252ca428SRobert Watson } 998252ca428SRobert Watson 999c488362eSRobert Watson #ifdef MAC 10008501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 100130d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 1002302ce8d6SAndre Oppermann goto dropunlock; 1003c488362eSRobert Watson #endif 1004a557af22SRobert Watson so = inp->inp_socket; 1005302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 1006610ee2f9SDavid Greenman #ifdef TCPDEBUG 1007df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 1008df8bae1dSRodney W. Grimes ostate = tp->t_state; 100910fe523eSMaxim Konovalov #ifdef INET6 10106f697424SBjoern A. Zeeb if (isipv6) { 1011540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 1012d30d90dcSMaxim Konovalov } else 10136f697424SBjoern A. Zeeb #endif 1014540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 1015fb59c426SYoshinobu Inoue tcp_savetcp = *th; 1016df8bae1dSRodney W. Grimes } 1017b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */ 1018db33b3e6SAndre Oppermann /* 1019db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 1020db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 1021a7c7f2a7SJohn Baldwin * attempt or the completion of a previous one. 1022db33b3e6SAndre Oppermann */ 102345274760SJonathan T. Looney KASSERT(tp->t_state == TCPS_LISTEN || !(so->so_options & SO_ACCEPTCONN), 102445274760SJonathan T. Looney ("%s: so accepting but tp %p not listening", __func__, tp)); 102545274760SJonathan T. Looney if (tp->t_state == TCPS_LISTEN && (so->so_options & SO_ACCEPTCONN)) { 1026540e8b7eSJeffrey Hsu struct in_conninfo inc; 1027540e8b7eSJeffrey Hsu 10288bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 1029302ce8d6SAndre Oppermann #ifdef INET6 1030be2ac88cSJonathan Lemon if (isipv6) { 1031dcdb4371SBjoern A. Zeeb inc.inc_flags |= INC_ISIPV6; 10325dff1c38SMichael Tuexen if (inp->inp_inc.inc_flags & INC_IPV6MINMTU) 10335dff1c38SMichael Tuexen inc.inc_flags |= INC_IPV6MINMTU; 1034be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 1035be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 1036302ce8d6SAndre Oppermann } else 1037302ce8d6SAndre Oppermann #endif 1038302ce8d6SAndre Oppermann { 1039be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 1040be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 1041be2ac88cSJonathan Lemon } 1042be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 1043be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 10447973fba3SJulian Elischer inc.inc_fibnum = so->so_fibnum; 1045be2ac88cSJonathan Lemon 1046fb59c426SYoshinobu Inoue /* 10478d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 10488d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 10498d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 1050fb59c426SYoshinobu Inoue */ 1051be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 1052a7c7f2a7SJohn Baldwin 1053ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1054bf6d304aSAndre Oppermann /* 1055bf6d304aSAndre Oppermann * Parse the TCP options here because 1056bf6d304aSAndre Oppermann * syncookies need access to the reflected 1057bf6d304aSAndre Oppermann * timestamp. 1058bf6d304aSAndre Oppermann */ 1059bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 10609fa198beSAndre Oppermann /* 10619fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 10629fa198beSAndre Oppermann * inp and tcpinfo locks. 10639fa198beSAndre Oppermann */ 1064fcf59617SAndrey V. Elsukov rstreason = syncache_expand(&inc, &to, th, &so, m); 1065fcf59617SAndrey V. Elsukov if (rstreason < 0) { 1066fcf59617SAndrey V. Elsukov /* 1067fcf59617SAndrey V. Elsukov * A failing TCP MD5 signature comparison 1068fcf59617SAndrey V. Elsukov * must result in the segment being dropped 1069fcf59617SAndrey V. Elsukov * and must not produce any response back 1070fcf59617SAndrey V. Elsukov * to the sender. 1071fcf59617SAndrey V. Elsukov */ 1072fcf59617SAndrey V. Elsukov goto dropunlock; 1073fcf59617SAndrey V. Elsukov } else if (rstreason == 0) { 10744195b4afSPaul Traina /* 1075e207f800SAndre Oppermann * No syncache entry or ACK was not 1076be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 10778d573cc1SAndre Oppermann * NB: syncache did its own logging 10788d573cc1SAndre Oppermann * of the failure cause. 10794195b4afSPaul Traina */ 1080be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 1081be2ac88cSJonathan Lemon goto dropwithreset; 1082be2ac88cSJonathan Lemon } 108309c305ebSPatrick Kelsey tfo_socket_result: 1084f76fcf6dSJeffrey Hsu if (so == NULL) { 1085be2ac88cSJonathan Lemon /* 1086e207f800SAndre Oppermann * We completed the 3-way handshake 1087e207f800SAndre Oppermann * but could not allocate a socket 1088e207f800SAndre Oppermann * either due to memory shortage, 1089e207f800SAndre Oppermann * listen queue length limits or 1090a160e630SAndre Oppermann * global socket limits. Send RST 1091a160e630SAndre Oppermann * or wait and have the remote end 1092a160e630SAndre Oppermann * retransmit the ACK for another 1093a160e630SAndre Oppermann * try. 1094be2ac88cSJonathan Lemon */ 1095a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1096a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 10978d573cc1SAndre Oppermann "Socket allocation failed due to " 10988d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 1099ac957cd2SJulian Elischer s, __func__, 1100ac957cd2SJulian Elischer V_tcp_sc_rst_sock_fail ? 1101ac957cd2SJulian Elischer "sending RST" : "try again"); 1102603724d3SBjoern A. Zeeb if (V_tcp_sc_rst_sock_fail) { 1103e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 1104e207f800SAndre Oppermann goto dropwithreset; 1105a160e630SAndre Oppermann } else 1106a160e630SAndre Oppermann goto dropunlock; 1107f76fcf6dSJeffrey Hsu } 1108be2ac88cSJonathan Lemon /* 1109be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 11108d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 11118d573cc1SAndre Oppermann * created socket and update the tp variable. 1112be2ac88cSJonathan Lemon */ 11138501a69cSRobert Watson INP_WUNLOCK(inp); /* listen socket */ 1114be2ac88cSJonathan Lemon inp = sotoinpcb(so); 1115ff9b006dSJulien Charbon /* 1116ff9b006dSJulien Charbon * New connection inpcb is already locked by 1117ff9b006dSJulien Charbon * syncache_expand(). 1118ff9b006dSJulien Charbon */ 1119ff9b006dSJulien Charbon INP_WLOCK_ASSERT(inp); 1120be2ac88cSJonathan Lemon tp = intotcpcb(inp); 11218d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 11228d573cc1SAndre Oppermann ("%s: ", __func__)); 1123be2ac88cSJonathan Lemon /* 1124302ce8d6SAndre Oppermann * Process the segment and the data it 1125302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 1126302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 1127302ce8d6SAndre Oppermann */ 11286138da62SMichael Tuexen TCP_PROBE5(receive, NULL, tp, m, tp, th); 112955bceb1eSRandall Stewart tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 11306573d758SMatt Macy iptos); 11316573d758SMatt Macy if (ti_locked == TI_RLOCKED) 11326573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 11338f5a8818SKevin Lo return (IPPROTO_DONE); 1134be2ac88cSJonathan Lemon } 1135a160e630SAndre Oppermann /* 11368d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 11378d573cc1SAndre Oppermann * 1138a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 1139a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 1140a160e630SAndre Oppermann * retransmits. 114119bc77c5SAndre Oppermann * 114219bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 114319bc77c5SAndre Oppermann * causes. 1144a160e630SAndre Oppermann */ 1145a160e630SAndre Oppermann if (thflags & TH_RST) { 114693899d10SMichael Tuexen syncache_chkrst(&inc, th, m); 1147a160e630SAndre Oppermann goto dropunlock; 1148a160e630SAndre Oppermann } 1149a160e630SAndre Oppermann /* 1150a160e630SAndre Oppermann * We can't do anything without SYN. 1151a160e630SAndre Oppermann */ 1152a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 1153a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1154a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1155773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 11568d573cc1SAndre Oppermann s, __func__); 115778b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1158a160e630SAndre Oppermann goto dropunlock; 1159a160e630SAndre Oppermann } 1160a160e630SAndre Oppermann /* 1161a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 1162a160e630SAndre Oppermann */ 1163fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1164a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1165a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11668d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 11678d573cc1SAndre Oppermann s, __func__); 1168a160e630SAndre Oppermann syncache_badack(&inc); /* XXX: Not needed! */ 116978b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1170a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1171a57815efSBosko Milekic goto dropwithreset; 11724195b4afSPaul Traina } 1173a160e630SAndre Oppermann /* 1174a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 1175a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 1176a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 1177a160e630SAndre Oppermann * TCP/IP stack. 1178a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 1179a160e630SAndre Oppermann * and is constantly refining its stack detection 1180a160e630SAndre Oppermann * strategies. 11818d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 1182a160e630SAndre Oppermann * and was used by RFC1644. 1183a160e630SAndre Oppermann */ 1184603724d3SBjoern A. Zeeb if ((thflags & TH_FIN) && V_drop_synfin) { 1185a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1186a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1187773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 11888d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 118978b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1190302ce8d6SAndre Oppermann goto dropunlock; 1191ebb0cbeaSPaul Traina } 1192be2ac88cSJonathan Lemon /* 1193be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 1194a160e630SAndre Oppermann * 1195a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 1196a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 1197a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 1198be2ac88cSJonathan Lemon */ 1199a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 1200a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 1201a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 1202a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 120333841545SHajimu UMEMOTO #ifdef INET6 120433841545SHajimu UMEMOTO /* 120533841545SHajimu UMEMOTO * If deprecated address is forbidden, 120633841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 120733841545SHajimu UMEMOTO * address to prevent any new inbound connection from 120833841545SHajimu UMEMOTO * getting established. 120933841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 121033841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 121133841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 121233841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 121333841545SHajimu UMEMOTO * for the exchange. 121433841545SHajimu UMEMOTO * 121533841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 121633841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 121733841545SHajimu UMEMOTO * SYN in this case. 121833841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 121933841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 122033841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 122133841545SHajimu UMEMOTO * used" 122233841545SHajimu UMEMOTO * 2. use of it with new communication: 122333841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 122433841545SHajimu UMEMOTO * with sufficient scope is available" 122533841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 122633841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 122733841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 122833841545SHajimu UMEMOTO * 122933841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 123033841545SHajimu UMEMOTO * multiple description text for deprecated address 123133841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 123233841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 123333841545SHajimu UMEMOTO */ 1234603724d3SBjoern A. Zeeb if (isipv6 && !V_ip6_use_deprecated) { 123533841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 123633841545SHajimu UMEMOTO 12373e88eb90SAndrey V. Elsukov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */); 12388c0fec80SRobert Watson if (ia6 != NULL && 123933841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 12408c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 1241a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1242a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12438d573cc1SAndre Oppermann "Connection attempt to deprecated " 12448d573cc1SAndre Oppermann "IPv6 address rejected\n", 1245a160e630SAndre Oppermann s, __func__); 124633841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 124733841545SHajimu UMEMOTO goto dropwithreset; 124833841545SHajimu UMEMOTO } 124977d396fdSMaksim Yevmenkin if (ia6) 12508c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 125133841545SHajimu UMEMOTO } 1252b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 125365f28919SJesper Skriver /* 1254db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 12558d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 1256db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 12578d573cc1SAndre Oppermann * If it is from this socket it must be forged. 12588d573cc1SAndre Oppermann * Don't respond if the source or destination is a 12598d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 126093ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 126193ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 126293ec91baSCrist J. Clark * in_broadcast() to find them. 1263be2ac88cSJonathan Lemon */ 12648d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 12658d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 12668d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12678d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 1268773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 1269302ce8d6SAndre Oppermann goto dropunlock; 12708d573cc1SAndre Oppermann } 1271db33b3e6SAndre Oppermann #ifdef INET6 1272b287c6c7SBjoern A. Zeeb if (isipv6) { 1273db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1274a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 1275a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1276a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12778d573cc1SAndre Oppermann "Connection attempt to/from self " 1278773673c1SAndre Oppermann "ignored\n", s, __func__); 1279302ce8d6SAndre Oppermann goto dropunlock; 1280a160e630SAndre Oppermann } 1281be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1282a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 1283a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1284a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12858d573cc1SAndre Oppermann "Connection attempt from/to multicast " 1286773673c1SAndre Oppermann "address ignored\n", s, __func__); 1287302ce8d6SAndre Oppermann goto dropunlock; 1288a160e630SAndre Oppermann } 1289b287c6c7SBjoern A. Zeeb } 1290db33b3e6SAndre Oppermann #endif 1291b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 1292b287c6c7SBjoern A. Zeeb else 1293b287c6c7SBjoern A. Zeeb #endif 1294b287c6c7SBjoern A. Zeeb #ifdef INET 1295b287c6c7SBjoern A. Zeeb { 1296db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1297a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 1298a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1299a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13008d573cc1SAndre Oppermann "Connection attempt from/to self " 1301773673c1SAndre Oppermann "ignored\n", s, __func__); 1302302ce8d6SAndre Oppermann goto dropunlock; 1303a160e630SAndre Oppermann } 1304be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1305be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 13062ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 1307a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 1308a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1309a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13108d573cc1SAndre Oppermann "Connection attempt from/to broad- " 1311773673c1SAndre Oppermann "or multicast address ignored\n", 1312a160e630SAndre Oppermann s, __func__); 1313302ce8d6SAndre Oppermann goto dropunlock; 1314c068736aSJeffrey Hsu } 1315a160e630SAndre Oppermann } 1316b287c6c7SBjoern A. Zeeb #endif 1317be2ac88cSJonathan Lemon /* 1318db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 1319db33b3e6SAndre Oppermann * for syncache. 1320be2ac88cSJonathan Lemon */ 13213c653157SHartmut Brandt #ifdef TCPDEBUG 13223c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 13233c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 13243c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 13253c653157SHartmut Brandt #endif 13262b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 1327f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 1328281a0fd4SPatrick Kelsey if (syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL)) 132909c305ebSPatrick Kelsey goto tfo_socket_result; 133018a75309SPatrick Kelsey 1331be2ac88cSJonathan Lemon /* 13324d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 1333a7c7f2a7SJohn Baldwin * Only the listen socket is unlocked by syncache_add(). 1334be2ac88cSJonathan Lemon */ 1335ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 13366573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 1337a7c7f2a7SJohn Baldwin ti_locked = TI_UNLOCKED; 1338a7c7f2a7SJohn Baldwin } 1339384a5c3cSAndrey V. Elsukov INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo); 13408f5a8818SKevin Lo return (IPPROTO_DONE); 1341982c1675SAndre Oppermann } else if (tp->t_state == TCPS_LISTEN) { 1342982c1675SAndre Oppermann /* 1343982c1675SAndre Oppermann * When a listen socket is torn down the SO_ACCEPTCONN 1344982c1675SAndre Oppermann * flag is removed first while connections are drained 1345982c1675SAndre Oppermann * from the accept queue in a unlock/lock cycle of the 1346982c1675SAndre Oppermann * ACCEPT_LOCK, opening a race condition allowing a SYN 1347982c1675SAndre Oppermann * attempt go through unhandled. 1348982c1675SAndre Oppermann */ 1349982c1675SAndre Oppermann goto dropunlock; 1350f76fcf6dSJeffrey Hsu } 1351fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1352fcf59617SAndrey V. Elsukov if (tp->t_flags & TF_SIGNATURE) { 1353fcf59617SAndrey V. Elsukov tcp_dooptions(&to, optp, optlen, thflags); 1354fcf59617SAndrey V. Elsukov if ((to.to_flags & TOF_SIGNATURE) == 0) { 1355fcf59617SAndrey V. Elsukov TCPSTAT_INC(tcps_sig_err_nosigopt); 13562903309aSAttilio Rao goto dropunlock; 13572903309aSAttilio Rao } 1358fcf59617SAndrey V. Elsukov if (!TCPMD5_ENABLED() || 1359fcf59617SAndrey V. Elsukov TCPMD5_INPUT(m, th, to.to_signature) != 0) 1360fcf59617SAndrey V. Elsukov goto dropunlock; 13612903309aSAttilio Rao } 13622903309aSAttilio Rao #endif 13632b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 136457f60867SMark Johnston 1365302ce8d6SAndre Oppermann /* 13668d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 13678d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 13688d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 1369302ce8d6SAndre Oppermann */ 13706573d758SMatt Macy tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos); 13716573d758SMatt Macy if (ti_locked == TI_RLOCKED) 13726573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 13738f5a8818SKevin Lo return (IPPROTO_DONE); 1374be2ac88cSJonathan Lemon 1375302ce8d6SAndre Oppermann dropwithreset: 13762b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 137757f60867SMark Johnston 1378ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 13796573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 1380252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1381fa046d87SRobert Watson } 1382fa046d87SRobert Watson #ifdef INVARIANTS 1383fa046d87SRobert Watson else { 1384fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset " 1385fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1386384a5c3cSAndrey V. Elsukov INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo); 1387fa046d87SRobert Watson } 1388fa046d87SRobert Watson #endif 1389014ea782SRobert Watson 1390014ea782SRobert Watson if (inp != NULL) { 1391302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 1392014ea782SRobert Watson INP_WUNLOCK(inp); 1393dd8ac7f9SRobert Watson } else 1394014ea782SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1395302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 1396014ea782SRobert Watson goto drop; 1397014ea782SRobert Watson 1398302ce8d6SAndre Oppermann dropunlock: 139957f60867SMark Johnston if (m != NULL) 14002b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 140157f60867SMark Johnston 1402ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 14036573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 1404252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1405fa046d87SRobert Watson } 1406fa046d87SRobert Watson #ifdef INVARIANTS 1407fa046d87SRobert Watson else { 1408fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock " 1409fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1410384a5c3cSAndrey V. Elsukov INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo); 1411fa046d87SRobert Watson } 1412fa046d87SRobert Watson #endif 1413252ca428SRobert Watson 14149fa198beSAndre Oppermann if (inp != NULL) 14158501a69cSRobert Watson INP_WUNLOCK(inp); 1416014ea782SRobert Watson 1417302ce8d6SAndre Oppermann drop: 1418384a5c3cSAndrey V. Elsukov INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo); 1419a160e630SAndre Oppermann if (s != NULL) 1420a160e630SAndre Oppermann free(s, M_TCPLOG); 1421302ce8d6SAndre Oppermann if (m != NULL) 1422302ce8d6SAndre Oppermann m_freem(m); 14238f5a8818SKevin Lo return (IPPROTO_DONE); 1424302ce8d6SAndre Oppermann } 1425302ce8d6SAndre Oppermann 1426e44c1887SSteven Hartland /* 1427e44c1887SSteven Hartland * Automatic sizing of receive socket buffer. Often the send 1428e44c1887SSteven Hartland * buffer size is not optimally adjusted to the actual network 1429e44c1887SSteven Hartland * conditions at hand (delay bandwidth product). Setting the 1430e44c1887SSteven Hartland * buffer size too small limits throughput on links with high 1431e44c1887SSteven Hartland * bandwidth and high delay (eg. trans-continental/oceanic links). 1432e44c1887SSteven Hartland * 1433e44c1887SSteven Hartland * On the receive side the socket buffer memory is only rarely 1434e44c1887SSteven Hartland * used to any significant extent. This allows us to be much 1435e44c1887SSteven Hartland * more aggressive in scaling the receive socket buffer. For 1436e44c1887SSteven Hartland * the case that the buffer space is actually used to a large 1437e44c1887SSteven Hartland * extent and we run out of kernel memory we can simply drop 1438e44c1887SSteven Hartland * the new segments; TCP on the sender will just retransmit it 1439e44c1887SSteven Hartland * later. Setting the buffer size too big may only consume too 1440e44c1887SSteven Hartland * much kernel memory if the application doesn't read() from 1441e44c1887SSteven Hartland * the socket or packet loss or reordering makes use of the 1442e44c1887SSteven Hartland * reassembly queue. 1443e44c1887SSteven Hartland * 1444e44c1887SSteven Hartland * The criteria to step up the receive buffer one notch are: 1445e44c1887SSteven Hartland * 1. Application has not set receive buffer size with 1446e44c1887SSteven Hartland * SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE. 1447560c0586SMichael Tuexen * 2. the number of bytes received during 1/2 of an sRTT 1448560c0586SMichael Tuexen * is at least 3/8 of the current socket buffer size. 1449560c0586SMichael Tuexen * 3. receive buffer size has not hit maximal automatic size; 1450e44c1887SSteven Hartland * 1451560c0586SMichael Tuexen * If all of the criteria are met we increaset the socket buffer 1452560c0586SMichael Tuexen * by a 1/2 (bounded by the max). This allows us to keep ahead 1453560c0586SMichael Tuexen * of slow-start but also makes it so our peer never gets limited 1454560c0586SMichael Tuexen * by our rwnd which we then open up causing a burst. 1455560c0586SMichael Tuexen * 1456560c0586SMichael Tuexen * This algorithm does two steps per RTT at most and only if 1457e44c1887SSteven Hartland * we receive a bulk stream w/o packet losses or reorderings. 1458e44c1887SSteven Hartland * Shrinking the buffer during idle times is not necessary as 1459e44c1887SSteven Hartland * it doesn't consume any memory when idle. 1460e44c1887SSteven Hartland * 1461e44c1887SSteven Hartland * TODO: Only step up if the application is actually serving 1462e44c1887SSteven Hartland * the buffer to better manage the socket buffer resources. 1463e44c1887SSteven Hartland */ 1464e44c1887SSteven Hartland int 1465e44c1887SSteven Hartland tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so, 1466e44c1887SSteven Hartland struct tcpcb *tp, int tlen) 1467e44c1887SSteven Hartland { 1468e44c1887SSteven Hartland int newsize = 0; 1469e44c1887SSteven Hartland 1470e44c1887SSteven Hartland if (V_tcp_do_autorcvbuf && (so->so_rcv.sb_flags & SB_AUTOSIZE) && 1471e44c1887SSteven Hartland tp->t_srtt != 0 && tp->rfbuf_ts != 0 && 1472e44c1887SSteven Hartland TCP_TS_TO_TICKS(tcp_ts_getticks() - tp->rfbuf_ts) > 1473560c0586SMichael Tuexen ((tp->t_srtt >> TCP_RTT_SHIFT)/2)) { 1474560c0586SMichael Tuexen if (tp->rfbuf_cnt > ((so->so_rcv.sb_hiwat / 2)/ 4 * 3) && 1475e44c1887SSteven Hartland so->so_rcv.sb_hiwat < V_tcp_autorcvbuf_max) { 1476560c0586SMichael Tuexen newsize = min((so->so_rcv.sb_hiwat + (so->so_rcv.sb_hiwat/2)), V_tcp_autorcvbuf_max); 1477e44c1887SSteven Hartland } 1478e44c1887SSteven Hartland TCP_PROBE6(receive__autoresize, NULL, tp, m, tp, th, newsize); 1479e44c1887SSteven Hartland 1480e44c1887SSteven Hartland /* Start over with next RTT. */ 1481e44c1887SSteven Hartland tp->rfbuf_ts = 0; 1482e44c1887SSteven Hartland tp->rfbuf_cnt = 0; 1483e44c1887SSteven Hartland } else { 1484e44c1887SSteven Hartland tp->rfbuf_cnt += tlen; /* add up */ 1485e44c1887SSteven Hartland } 1486e44c1887SSteven Hartland 1487e44c1887SSteven Hartland return (newsize); 1488e44c1887SSteven Hartland } 1489e44c1887SSteven Hartland 149055bceb1eSRandall Stewart void 1491302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 14926573d758SMatt Macy struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos) 1493302ce8d6SAndre Oppermann { 1494021eaf79SHiren Panchasara int thflags, acked, ourfinisacked, needoutput = 0, sack_changed; 1495302ce8d6SAndre Oppermann int rstreason, todrop, win; 14963ac12506SJonathan T. Looney uint32_t tiwin; 14974b7b743cSLawrence Stewart uint16_t nsegs; 149807dacf03SAndre Oppermann char *s; 149907dacf03SAndre Oppermann struct in_conninfo *inc; 1500c11a15bfSGleb Smirnoff struct mbuf *mfree; 1501302ce8d6SAndre Oppermann struct tcpopt to; 1502281a0fd4SPatrick Kelsey int tfo_syn; 1503302ce8d6SAndre Oppermann 150414739780SMaxim Konovalov #ifdef TCPDEBUG 150514739780SMaxim Konovalov /* 150614739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 150714739780SMaxim Konovalov * now IPv6. 150814739780SMaxim Konovalov */ 150914739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 151014739780SMaxim Konovalov struct tcphdr tcp_savetcp; 151114739780SMaxim Konovalov short ostate = 0; 151214739780SMaxim Konovalov #endif 1513302ce8d6SAndre Oppermann thflags = th->th_flags; 151407dacf03SAndre Oppermann inc = &tp->t_inpcb->inp_inc; 1515d64a46eaSLawrence Stewart tp->sackhint.last_sack_ack = 0; 1516021eaf79SHiren Panchasara sack_changed = 0; 15174b7b743cSLawrence Stewart nsegs = max(1, m->m_pkthdr.lro_nsegs); 1518252ca428SRobert Watson /* 1519252ca428SRobert Watson * If this is either a state-changing packet or current state isn't 1520252ca428SRobert Watson * established, we require a write lock on tcbinfo. Otherwise, we 15210989f56cSRobert Watson * allow the tcbinfo to be in either alocked or unlocked, as the 15220989f56cSRobert Watson * caller may have unnecessarily acquired a write lock due to a race. 1523252ca428SRobert Watson */ 1524252ca428SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 1525252ca428SRobert Watson tp->t_state != TCPS_ESTABLISHED) { 1526ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1527252ca428SRobert Watson } 15288501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1529679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1530679d9708SAndre Oppermann __func__)); 1531679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1532679d9708SAndre Oppermann __func__)); 1533df8bae1dSRodney W. Grimes 153486a996e6SHiren Panchasara #ifdef TCPPCAP 153586a996e6SHiren Panchasara /* Save segment, if requested. */ 153686a996e6SHiren Panchasara tcp_pcap_add(th, m, &(tp->t_inpkts)); 153786a996e6SHiren Panchasara #endif 15382529f56eSJonathan T. Looney TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0, 15392529f56eSJonathan T. Looney tlen, NULL, true); 154086a996e6SHiren Panchasara 1541013f4df6SMichael Tuexen if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) { 1542013f4df6SMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 1543013f4df6SMichael Tuexen log(LOG_DEBUG, "%s; %s: " 1544013f4df6SMichael Tuexen "SYN|FIN segment ignored (based on " 1545013f4df6SMichael Tuexen "sysctl setting)\n", s, __func__); 1546013f4df6SMichael Tuexen free(s, M_TCPLOG); 1547013f4df6SMichael Tuexen } 1548013f4df6SMichael Tuexen goto drop; 1549013f4df6SMichael Tuexen } 1550013f4df6SMichael Tuexen 1551df8bae1dSRodney W. Grimes /* 1552ebfd7534SMichael Tuexen * If a segment with the ACK-bit set arrives in the SYN-SENT state 1553ebfd7534SMichael Tuexen * check SEQ.ACK first. 1554ebfd7534SMichael Tuexen */ 1555ebfd7534SMichael Tuexen if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) && 1556ebfd7534SMichael Tuexen (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) { 1557ebfd7534SMichael Tuexen rstreason = BANDLIM_UNLIMITED; 1558ebfd7534SMichael Tuexen goto dropwithreset; 1559ebfd7534SMichael Tuexen } 1560ebfd7534SMichael Tuexen 1561ebfd7534SMichael Tuexen /* 1562df8bae1dSRodney W. Grimes * Segment received on connection. 1563df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1564e8949f74SAndre Oppermann * XXX: This should be done after segment 1565e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 1566df8bae1dSRodney W. Grimes */ 15679b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 1568df8bae1dSRodney W. Grimes 1569df8bae1dSRodney W. Grimes /* 1570c52102ddSHiren Panchasara * Scale up the window into a 32-bit value. 1571e8949f74SAndre Oppermann * For the SYN_SENT state the scale is zero. 1572464fcfbcSAndre Oppermann */ 1573464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 1574464fcfbcSAndre Oppermann 1575464fcfbcSAndre Oppermann /* 1576f2512ba1SRui Paulo * TCP ECN processing. 1577f2512ba1SRui Paulo */ 1578f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) { 15799c251892SRui Paulo if (thflags & TH_CWR) 15809c251892SRui Paulo tp->t_flags &= ~TF_ECN_SND_ECE; 1581f2512ba1SRui Paulo switch (iptos & IPTOS_ECN_MASK) { 1582f2512ba1SRui Paulo case IPTOS_ECN_CE: 1583f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_ECE; 158478b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ce); 1585f2512ba1SRui Paulo break; 1586f2512ba1SRui Paulo case IPTOS_ECN_ECT0: 158778b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect0); 1588f2512ba1SRui Paulo break; 1589f2512ba1SRui Paulo case IPTOS_ECN_ECT1: 159078b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect1); 1591f2512ba1SRui Paulo break; 1592f2512ba1SRui Paulo } 159364807b30SHiren Panchasara 159464807b30SHiren Panchasara /* Process a packet differently from RFC3168. */ 159564807b30SHiren Panchasara cc_ecnpkt_handler(tp, th, iptos); 159664807b30SHiren Panchasara 1597dbc42409SLawrence Stewart /* Congestion experienced. */ 1598dbc42409SLawrence Stewart if (thflags & TH_ECE) { 1599dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_ECN); 1600f2512ba1SRui Paulo } 1601f2512ba1SRui Paulo } 1602f2512ba1SRui Paulo 1603f2512ba1SRui Paulo /* 1604f72167f4SAndre Oppermann * Parse options on any incoming segment. 1605f72167f4SAndre Oppermann */ 1606302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1607302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1608302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1609f72167f4SAndre Oppermann 1610fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1611fcf59617SAndrey V. Elsukov if ((tp->t_flags & TF_SIGNATURE) != 0 && 1612fcf59617SAndrey V. Elsukov (to.to_flags & TOF_SIGNATURE) == 0) { 1613fcf59617SAndrey V. Elsukov TCPSTAT_INC(tcps_sig_err_sigopt); 1614fcf59617SAndrey V. Elsukov /* XXX: should drop? */ 1615fcf59617SAndrey V. Elsukov } 1616fcf59617SAndrey V. Elsukov #endif 1617f72167f4SAndre Oppermann /* 1618f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1619bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1620bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1621bf6d304aSAndre Oppermann * was established. 1622f72167f4SAndre Oppermann */ 1623bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1624e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 1625d8951c8aSBjoern A. Zeeb if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) 1626f72167f4SAndre Oppermann to.to_tsecr = 0; 162710d20c84SMatt Macy else if (tp->t_flags & TF_PREVVALID && 162810d20c84SMatt Macy tp->t_badrxtwin != 0 && SEQ_LT(to.to_tsecr, tp->t_badrxtwin)) 162910d20c84SMatt Macy cc_cong_signal(tp, th, CC_RTO_ERR); 1630bf6d304aSAndre Oppermann } 163107dacf03SAndre Oppermann /* 163297d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 163397d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 16345396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 16355396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 163697d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1637df8bae1dSRodney W. Grimes */ 16380a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1639464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 1640464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 1641be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 164202a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 1643be2ac88cSJonathan Lemon } 16445396d0f8SAndre Oppermann /* 16455396d0f8SAndre Oppermann * Initial send window. It will be updated with 16465396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 16475396d0f8SAndre Oppermann */ 16485396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 1649be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1650be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1651be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1652d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1653be2ac88cSJonathan Lemon } 1654be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1655be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 16563529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 16573529149eSAndre Oppermann (to.to_flags & TOF_SACKPERM) == 0) 16583529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 1659c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) { 1660a026a53aSMichael Tuexen if (to.to_flags & TOF_FASTOPEN) { 1661a026a53aSMichael Tuexen uint16_t mss; 1662a026a53aSMichael Tuexen 1663a026a53aSMichael Tuexen if (to.to_flags & TOF_MSS) 1664a026a53aSMichael Tuexen mss = to.to_mss; 1665c560df6fSPatrick Kelsey else 1666a026a53aSMichael Tuexen if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) 1667a026a53aSMichael Tuexen mss = TCP6_MSS; 1668a026a53aSMichael Tuexen else 1669a026a53aSMichael Tuexen mss = TCP_MSS; 1670a026a53aSMichael Tuexen tcp_fastopen_update_cache(tp, mss, 1671a026a53aSMichael Tuexen to.to_tfo_len, to.to_tfo_cookie); 1672a026a53aSMichael Tuexen } else 1673c560df6fSPatrick Kelsey tcp_fastopen_disable_path(tp); 1674c560df6fSPatrick Kelsey } 16756d90faf3SPaul Saab } 16766d90faf3SPaul Saab 1677df8bae1dSRodney W. Grimes /* 16784da9052aSMichael Tuexen * If timestamps were negotiated during SYN/ACK they should 16794da9052aSMichael Tuexen * appear on every segment during this session and vice versa. 16804da9052aSMichael Tuexen */ 16814da9052aSMichael Tuexen if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) { 16824da9052aSMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 16834da9052aSMichael Tuexen log(LOG_DEBUG, "%s; %s: Timestamp missing, " 16844da9052aSMichael Tuexen "no action\n", s, __func__); 16854da9052aSMichael Tuexen free(s, M_TCPLOG); 16864da9052aSMichael Tuexen } 16874da9052aSMichael Tuexen } 16884da9052aSMichael Tuexen if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) { 16894da9052aSMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 16904da9052aSMichael Tuexen log(LOG_DEBUG, "%s; %s: Timestamp not expected, " 16914da9052aSMichael Tuexen "no action\n", s, __func__); 16924da9052aSMichael Tuexen free(s, M_TCPLOG); 16934da9052aSMichael Tuexen } 16944da9052aSMichael Tuexen } 16954da9052aSMichael Tuexen 16964da9052aSMichael Tuexen /* 1697df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1698df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1699df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1700df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1701df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1702df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1703df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1704df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1705df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1706df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1707df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1708df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1709a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1710c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 1711a0292f23SGarrett Wollman * be TH_NEEDSYN. 1712df8bae1dSRodney W. Grimes */ 1713df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1714c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 1715fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1716c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 1717c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 1718a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1719c28440dbSRandall Stewart SEGQ_EMPTY(tp) && 17204741bfcbSPatrick Kelsey ((to.to_flags & TOF_TS) == 0 || 1721c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1722df8bae1dSRodney W. Grimes 1723df8bae1dSRodney W. Grimes /* 1724df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1725df8bae1dSRodney W. Grimes * record the timestamp. 1726a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1727a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1728df8bae1dSRodney W. Grimes */ 1729be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1730fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1731d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1732a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1733df8bae1dSRodney W. Grimes } 1734df8bae1dSRodney W. Grimes 1735fb59c426SYoshinobu Inoue if (tlen == 0) { 1736fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1737fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1738dbc42409SLawrence Stewart !IN_RECOVERY(tp->t_flags) && 1739fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1740dbc42409SLawrence Stewart TAILQ_EMPTY(&tp->snd_holes)) { 1741df8bae1dSRodney W. Grimes /* 1742e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1743df8bae1dSRodney W. Grimes */ 174478b50714SRobert Watson TCPSTAT_INC(tcps_predack); 1745252ca428SRobert Watson 17469b8b58e0SJonathan Lemon /* 174710d20c84SMatt Macy * "bad retransmit" recovery without timestamps. 17489b8b58e0SJonathan Lemon */ 174910d20c84SMatt Macy if ((to.to_flags & TOF_TS) == 0 && 175010d20c84SMatt Macy tp->t_rxtshift == 1 && 1751672dc4aeSJohn Baldwin tp->t_flags & TF_PREVVALID && 17526dfb8b31SJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) { 1753dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 17549b8b58e0SJonathan Lemon } 1755fa55172bSMatthew Dillon 1756fa55172bSMatthew Dillon /* 1757fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1758fa55172bSMatthew Dillon * 1759fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1760fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1761fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1762fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1763fa55172bSMatthew Dillon */ 1764fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1765fa55172bSMatthew Dillon to.to_tsecr) { 17663ac12506SJonathan T. Looney uint32_t t; 1767d8951c8aSBjoern A. Zeeb 1768d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 1769d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 1770d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 1771a0292f23SGarrett Wollman tcp_xmit_timer(tp, 1772d8951c8aSBjoern A. Zeeb TCP_TS_TO_TICKS(t) + 1); 1773fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1774fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1775eaf80179SAndre Oppermann if (!tp->t_rttlow || 1776eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1777eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1778c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1779c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1780fa55172bSMatthew Dillon } 1781dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 178239bc9de5SLawrence Stewart 1783bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 178439bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 178539bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 1786bd79708dSJonathan T. Looney #endif 178739bc9de5SLawrence Stewart 17884b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvackpack, nsegs); 178978b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 1790df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 17919d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 17929d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 17939d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 1794dbc42409SLawrence Stewart 1795dbc42409SLawrence Stewart /* 1796dbc42409SLawrence Stewart * Let the congestion control algorithm update 1797dbc42409SLawrence Stewart * congestion control related information. This 1798dbc42409SLawrence Stewart * typically means increasing the congestion 1799dbc42409SLawrence Stewart * window. 1800dbc42409SLawrence Stewart */ 18014b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, CC_ACK); 1802dbc42409SLawrence Stewart 18039d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 18041645d090SJeff Roberson /* 1805e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 18061645d090SJeff Roberson * to th_ack. 18071645d090SJeff Roberson */ 1808cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1809b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1810df8bae1dSRodney W. Grimes m_freem(m); 1811df8bae1dSRodney W. Grimes 1812df8bae1dSRodney W. Grimes /* 1813df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1814df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1815df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1816df8bae1dSRodney W. Grimes * If process is waiting for space, 1817df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1818df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1819df8bae1dSRodney W. Grimes * decide between more output or persist. 1820e8949f74SAndre Oppermann */ 18213c653157SHartmut Brandt #ifdef TCPDEBUG 18223c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 18233c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 18243c653157SHartmut Brandt (void *)tcp_saveipgen, 18253c653157SHartmut Brandt &tcp_savetcp, 0); 18263c653157SHartmut Brandt #endif 18272b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 1828df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1829b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1830b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1831b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1832b8152ba7SAndre Oppermann tp->t_rxtcur); 1833df8bae1dSRodney W. Grimes sowwakeup(so); 1834cfa6009eSGleb Smirnoff if (sbavail(&so->so_snd)) 183555bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 1836a14c749fSJonathan Lemon goto check_delack; 1837df8bae1dSRodney W. Grimes } 1838fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1839fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 18406741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 18416741ecf5SAndre Oppermann 1842df8bae1dSRodney W. Grimes /* 1843252ca428SRobert Watson * This is a pure, in-sequence data packet with 1844252ca428SRobert Watson * nothing on the reassembly queue and we have enough 1845252ca428SRobert Watson * buffer space to take it. 1846df8bae1dSRodney W. Grimes */ 18476d90faf3SPaul Saab /* Clean receiver SACK report if present */ 18483529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 18496d90faf3SPaul Saab tcp_clean_sackreport(tp); 185078b50714SRobert Watson TCPSTAT_INC(tcps_preddat); 1851fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 18521645d090SJeff Roberson /* 18531645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 18541645d090SJeff Roberson * th_seq. 18551645d090SJeff Roberson */ 185660ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 18571645d090SJeff Roberson /* 18581645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 18591645d090SJeff Roberson * rcv_nxt. 18601645d090SJeff Roberson */ 18611645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 18624b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvpack, nsegs); 186378b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 18643c653157SHartmut Brandt #ifdef TCPDEBUG 18653c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 18663c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 18673c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 18683c653157SHartmut Brandt #endif 18692b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 18705d06879aSGeorge V. Neville-Neil 1871e44c1887SSteven Hartland newsize = tcp_autorcvbuf(m, th, so, tp, tlen); 18726741ecf5SAndre Oppermann 18736741ecf5SAndre Oppermann /* Add data to socket buffer. */ 18741e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1875c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1876c1c36a2cSMike Silbersack m_freem(m); 1877c1c36a2cSMike Silbersack } else { 18786741ecf5SAndre Oppermann /* 18796741ecf5SAndre Oppermann * Set new socket buffer size. 18806741ecf5SAndre Oppermann * Give up when limit is reached. 18816741ecf5SAndre Oppermann */ 18826741ecf5SAndre Oppermann if (newsize) 18836741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 18846c8286e4SRobert Watson newsize, so, NULL)) 18856741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1886fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 1887651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 1888c1c36a2cSMike Silbersack } 1889e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 18901e4d7da7SRobert Watson sorwakeup_locked(so); 1891c1e5a6e5SAndre Oppermann if (DELAY_ACK(tp, tlen)) { 18923bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1893f498eeeeSDavid Greenman } else { 1894e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 189555bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 1896e612a582SDavid Greenman } 1897a14c749fSJonathan Lemon goto check_delack; 1898df8bae1dSRodney W. Grimes } 1899df8bae1dSRodney W. Grimes } 1900df8bae1dSRodney W. Grimes 1901df8bae1dSRodney W. Grimes /* 1902df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1903df8bae1dSRodney W. Grimes * and then do TCP input processing. 1904df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1905df8bae1dSRodney W. Grimes * but not less than advertised window. 1906df8bae1dSRodney W. Grimes */ 1907df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1908df8bae1dSRodney W. Grimes if (win < 0) 1909df8bae1dSRodney W. Grimes win = 0; 191066e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1911df8bae1dSRodney W. Grimes 1912df8bae1dSRodney W. Grimes switch (tp->t_state) { 1913df8bae1dSRodney W. Grimes 1914df8bae1dSRodney W. Grimes /* 1915764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1916764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1917764d8cefSBill Fenner */ 1918764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1919fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1920fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1921a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1922a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1923a57815efSBosko Milekic goto dropwithreset; 1924a57815efSBosko Milekic } 192568bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) { 1926281a0fd4SPatrick Kelsey /* 1927281a0fd4SPatrick Kelsey * When a TFO connection is in SYN_RECEIVED, the 1928281a0fd4SPatrick Kelsey * only valid packets are the initial SYN, a 1929281a0fd4SPatrick Kelsey * retransmit/copy of the initial SYN (possibly with 1930281a0fd4SPatrick Kelsey * a subset of the original data), a valid ACK, a 1931281a0fd4SPatrick Kelsey * FIN, or a RST. 1932281a0fd4SPatrick Kelsey */ 1933281a0fd4SPatrick Kelsey if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) { 1934281a0fd4SPatrick Kelsey rstreason = BANDLIM_RST_OPENPORT; 1935281a0fd4SPatrick Kelsey goto dropwithreset; 1936281a0fd4SPatrick Kelsey } else if (thflags & TH_SYN) { 1937281a0fd4SPatrick Kelsey /* non-initial SYN is ignored */ 1938281a0fd4SPatrick Kelsey if ((tcp_timer_active(tp, TT_DELACK) || 1939281a0fd4SPatrick Kelsey tcp_timer_active(tp, TT_REXMT))) 1940281a0fd4SPatrick Kelsey goto drop; 1941281a0fd4SPatrick Kelsey } else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) { 1942281a0fd4SPatrick Kelsey goto drop; 1943281a0fd4SPatrick Kelsey } 1944281a0fd4SPatrick Kelsey } 1945764d8cefSBill Fenner break; 1946764d8cefSBill Fenner 1947764d8cefSBill Fenner /* 1948df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 194998732609SMichael Tuexen * if seg contains a RST with valid ACK (SEQ.ACK has already 195098732609SMichael Tuexen * been verified), then drop the connection. 195198732609SMichael Tuexen * if seg contains a RST without an ACK, drop the seg. 195298732609SMichael Tuexen * if seg does not contain SYN, then drop the seg. 1953df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1954df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1955df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1956f2512ba1SRui Paulo * if seg contains an ECE and ECN support is enabled, the stream 1957f2512ba1SRui Paulo * is ECN capable. 1958df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1959df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1960df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1961df8bae1dSRodney W. Grimes */ 1962df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 196357f60867SMark Johnston if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) { 1964d9fae5abSAndriy Gapon TCP_PROBE5(connect__refused, NULL, tp, 19652b9c9984SGeorge V. Neville-Neil m, tp, th); 1966df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 196757f60867SMark Johnston } 19680ca3f933SAndre Oppermann if (thflags & TH_RST) 1969df8bae1dSRodney W. Grimes goto drop; 19700ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 1971df8bae1dSRodney W. Grimes goto drop; 1972464fcfbcSAndre Oppermann 1973fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1974df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1975fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1976c560df6fSPatrick Kelsey int tfo_partial_ack = 0; 1977c560df6fSPatrick Kelsey 197878b50714SRobert Watson TCPSTAT_INC(tcps_connects); 1979845799c1SAndras Olah soisconnected(so); 1980c488362eSRobert Watson #ifdef MAC 198130d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 1982c488362eSRobert Watson #endif 1983845799c1SAndras Olah /* Do window scaling on this connection? */ 1984845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1985845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1986845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1987845799c1SAndras Olah } 19883ac12506SJonathan T. Looney tp->rcv_adv += min(tp->rcv_wnd, 1989766282cbSJohn Baldwin TCP_MAXWIN << tp->rcv_scale); 1990a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1991a0292f23SGarrett Wollman /* 1992c560df6fSPatrick Kelsey * If not all the data that was sent in the TFO SYN 1993c560df6fSPatrick Kelsey * has been acked, resend the remainder right away. 1994c560df6fSPatrick Kelsey */ 1995c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags) && 1996c560df6fSPatrick Kelsey (tp->snd_una != tp->snd_max)) { 1997c560df6fSPatrick Kelsey tp->snd_nxt = th->th_ack; 1998c560df6fSPatrick Kelsey tfo_partial_ack = 1; 1999c560df6fSPatrick Kelsey } 2000c560df6fSPatrick Kelsey /* 2001a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 2002a0292f23SGarrett Wollman * ACKNOW will be turned on later. 2003a0292f23SGarrett Wollman */ 2004c560df6fSPatrick Kelsey if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial_ack) 2005b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 2006b8152ba7SAndre Oppermann tcp_delacktime); 2007a0292f23SGarrett Wollman else 2008a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2009f2512ba1SRui Paulo 2010bf7fcdb1SMichael Tuexen if (((thflags & (TH_CWR | TH_ECE)) == TH_ECE) && 2011bf7fcdb1SMichael Tuexen V_tcp_do_ecn) { 2012f2512ba1SRui Paulo tp->t_flags |= TF_ECN_PERMIT; 201378b50714SRobert Watson TCPSTAT_INC(tcps_ecn_shs); 2014f2512ba1SRui Paulo } 2015f2512ba1SRui Paulo 2016a0292f23SGarrett Wollman /* 2017a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 2018a0292f23SGarrett Wollman * Transitions: 2019a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 2020a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 2021a0292f23SGarrett Wollman */ 20229b8b58e0SJonathan Lemon tp->t_starttime = ticks; 2023a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 202457f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2025a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 2026fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 20277ff19458SPaul Traina } else { 202857f60867SMark Johnston tcp_state_change(tp, TCPS_ESTABLISHED); 2029d9fae5abSAndriy Gapon TCP_PROBE5(connect__established, NULL, tp, 20302b9c9984SGeorge V. Neville-Neil m, tp, th); 2031dbc42409SLawrence Stewart cc_conn_init(tp); 20329077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 20339077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 20347ff19458SPaul Traina } 2035a0292f23SGarrett Wollman } else { 2036a0292f23SGarrett Wollman /* 2037c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 2038153edc50SHiren Panchasara * simultaneous open. 2039c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 2040c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 2041a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 2042a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 2043a0292f23SGarrett Wollman */ 20444b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 2045b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 204657f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_RECEIVED); 2047a0292f23SGarrett Wollman } 2048df8bae1dSRodney W. Grimes 2049ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 20508501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 20517cfc6904SRobert Watson 2052df8bae1dSRodney W. Grimes /* 2053fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 2054df8bae1dSRodney W. Grimes * If data, trim to stay within window, 2055df8bae1dSRodney W. Grimes * dropping FIN if necessary. 2056df8bae1dSRodney W. Grimes */ 2057fb59c426SYoshinobu Inoue th->th_seq++; 2058fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 2059fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 2060df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2061fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 2062fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 206378b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 206478b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2065df8bae1dSRodney W. Grimes } 2066fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 2067fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 2068a0292f23SGarrett Wollman /* 2069a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 2070a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 2071a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 2072a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 2073a0292f23SGarrett Wollman * Otherwise, goto step 6. 2074a0292f23SGarrett Wollman */ 2075fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 2076a0292f23SGarrett Wollman goto process_ACK; 2077c068736aSJeffrey Hsu 2078df8bae1dSRodney W. Grimes goto step6; 2079c068736aSJeffrey Hsu 2080a0292f23SGarrett Wollman /* 2081a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 2082c94c54e4SAndre Oppermann * do normal processing. 2083a0292f23SGarrett Wollman * 2084c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 2085a0292f23SGarrett Wollman */ 2086a0292f23SGarrett Wollman case TCPS_LAST_ACK: 2087a0292f23SGarrett Wollman case TCPS_CLOSING: 2088a0292f23SGarrett Wollman break; /* continue normal processing */ 2089df8bae1dSRodney W. Grimes } 2090df8bae1dSRodney W. Grimes 2091df8bae1dSRodney W. Grimes /* 2092df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 209380ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 209480ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 209580ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 209680ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 209780ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 209880ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 209980ab7c0eSGarrett Wollman * killing a connection). 210080ab7c0eSGarrett Wollman * Then check timestamp, if present. 2101a0292f23SGarrett Wollman * Then check the connection count, if present. 2102df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 2103df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 2104df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 210580ab7c0eSGarrett Wollman */ 2106fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 21073220a212SGleb Smirnoff /* 21083220a212SGleb Smirnoff * RFC5961 Section 3.2 21093220a212SGleb Smirnoff * 21103220a212SGleb Smirnoff * - RST drops connection only if SEG.SEQ == RCV.NXT. 21113220a212SGleb Smirnoff * - If RST is in window, we send challenge ACK. 21123220a212SGleb Smirnoff * 21133220a212SGleb Smirnoff * Note: to take into account delayed ACKs, we should 21143220a212SGleb Smirnoff * test against last_ack_sent instead of rcv_nxt. 21153220a212SGleb Smirnoff * Note 2: we handle special case of closed window, not 21163220a212SGleb Smirnoff * covered by the RFC. 21173220a212SGleb Smirnoff */ 21183220a212SGleb Smirnoff if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 21193220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) || 21203220a212SGleb Smirnoff (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) { 212180ab7c0eSGarrett Wollman 2122ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 21233220a212SGleb Smirnoff KASSERT(tp->t_state != TCPS_SYN_SENT, 21243220a212SGleb Smirnoff ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p", 21253220a212SGleb Smirnoff __func__, th, tp)); 21263220a212SGleb Smirnoff 21273220a212SGleb Smirnoff if (V_tcp_insecure_rst || 21283220a212SGleb Smirnoff tp->last_ack_sent == th->th_seq) { 21293220a212SGleb Smirnoff TCPSTAT_INC(tcps_drops); 21303220a212SGleb Smirnoff /* Drop the connection. */ 21313220a212SGleb Smirnoff switch (tp->t_state) { 213280ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 213380ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 213480ab7c0eSGarrett Wollman goto close; 213580ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 213680ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 213780ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 213880ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 21396779a1a1SMichael Tuexen case TCPS_CLOSING: 21406779a1a1SMichael Tuexen case TCPS_LAST_ACK: 214180ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 214280ab7c0eSGarrett Wollman close: 21433220a212SGleb Smirnoff /* FALLTHROUGH */ 21443220a212SGleb Smirnoff default: 214580ab7c0eSGarrett Wollman tp = tcp_close(tp); 21463220a212SGleb Smirnoff } 21473220a212SGleb Smirnoff } else { 21483220a212SGleb Smirnoff TCPSTAT_INC(tcps_badrst); 21493220a212SGleb Smirnoff /* Send challenge ACK. */ 21503220a212SGleb Smirnoff tcp_respond(tp, mtod(m, void *), th, m, 21513220a212SGleb Smirnoff tp->rcv_nxt, tp->snd_nxt, TH_ACK); 21523220a212SGleb Smirnoff tp->last_ack_sent = tp->rcv_nxt; 21533220a212SGleb Smirnoff m = NULL; 21543220a212SGleb Smirnoff } 21553220a212SGleb Smirnoff } 21563220a212SGleb Smirnoff goto drop; 21573220a212SGleb Smirnoff } 215880ab7c0eSGarrett Wollman 21593220a212SGleb Smirnoff /* 21603220a212SGleb Smirnoff * RFC5961 Section 4.2 21613220a212SGleb Smirnoff * Send challenge ACK for any SYN in synchronized state. 21623220a212SGleb Smirnoff */ 2163281a0fd4SPatrick Kelsey if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT && 2164281a0fd4SPatrick Kelsey tp->t_state != TCPS_SYN_RECEIVED) { 2165ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2166252ca428SRobert Watson 21673220a212SGleb Smirnoff TCPSTAT_INC(tcps_badsyn); 21683220a212SGleb Smirnoff if (V_tcp_insecure_syn && 21693220a212SGleb Smirnoff SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 21703220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 21713220a212SGleb Smirnoff tp = tcp_drop(tp, ECONNRESET); 21723220a212SGleb Smirnoff rstreason = BANDLIM_UNLIMITED; 21733220a212SGleb Smirnoff } else { 21743220a212SGleb Smirnoff /* Send challenge ACK. */ 21753220a212SGleb Smirnoff tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt, 21763220a212SGleb Smirnoff tp->snd_nxt, TH_ACK); 21773220a212SGleb Smirnoff tp->last_ack_sent = tp->rcv_nxt; 21783220a212SGleb Smirnoff m = NULL; 217980ab7c0eSGarrett Wollman } 218080ab7c0eSGarrett Wollman goto drop; 218180ab7c0eSGarrett Wollman } 218280ab7c0eSGarrett Wollman 218380ab7c0eSGarrett Wollman /* 2184df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 2185df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 2186df8bae1dSRodney W. Grimes */ 2187be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 218880ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 2189df8bae1dSRodney W. Grimes 2190df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 2191d8951c8aSBjoern A. Zeeb if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 2192df8bae1dSRodney W. Grimes /* 2193df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 2194df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 2195df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 2196df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 2197df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 2198df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 2199df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 2200df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 2201df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 2202df8bae1dSRodney W. Grimes */ 2203df8bae1dSRodney W. Grimes tp->ts_recent = 0; 2204df8bae1dSRodney W. Grimes } else { 220578b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 220678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 220778b50714SRobert Watson TCPSTAT_INC(tcps_pawsdrop); 220807fd333dSMatthew Dillon if (tlen) 2209df8bae1dSRodney W. Grimes goto dropafterack; 22101ab4789dSMatthew Dillon goto drop; 22111ab4789dSMatthew Dillon } 2212df8bae1dSRodney W. Grimes } 2213df8bae1dSRodney W. Grimes 2214a0292f23SGarrett Wollman /* 221580ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 221680ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 221780ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 221880ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 221980ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 222080ab7c0eSGarrett Wollman */ 2221a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 2222a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2223a57815efSBosko Milekic goto dropwithreset; 2224a57815efSBosko Milekic } 222580ab7c0eSGarrett Wollman 2226fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 2227df8bae1dSRodney W. Grimes if (todrop > 0) { 2228831ad37eSXin LI if (thflags & TH_SYN) { 2229fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 2230fb59c426SYoshinobu Inoue th->th_seq++; 2231fb59c426SYoshinobu Inoue if (th->th_urp > 1) 2232fb59c426SYoshinobu Inoue th->th_urp--; 2233df8bae1dSRodney W. Grimes else 2234fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2235df8bae1dSRodney W. Grimes todrop--; 2236df8bae1dSRodney W. Grimes } 2237df8bae1dSRodney W. Grimes /* 2238dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 2239df8bae1dSRodney W. Grimes */ 2240fb59c426SYoshinobu Inoue if (todrop > tlen 2241fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 2242dac20301SGarrett Wollman /* 2243dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 2244dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 2245dac20301SGarrett Wollman * of sequence; drop it. 2246dac20301SGarrett Wollman */ 2247fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2248dac20301SGarrett Wollman 2249df8bae1dSRodney W. Grimes /* 2250dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 2251dac20301SGarrett Wollman * But keep on processing for RST or ACK. 2252df8bae1dSRodney W. Grimes */ 2253dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2254fb59c426SYoshinobu Inoue todrop = tlen; 225578b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 225678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 2257df8bae1dSRodney W. Grimes } else { 225878b50714SRobert Watson TCPSTAT_INC(tcps_rcvpartduppack); 225978b50714SRobert Watson TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 2260df8bae1dSRodney W. Grimes } 22615acfd95cSMichael Tuexen /* 22625acfd95cSMichael Tuexen * DSACK - add SACK block for dropped range 22635acfd95cSMichael Tuexen */ 22645acfd95cSMichael Tuexen if (tp->t_flags & TF_SACK_PERMIT) { 22655acfd95cSMichael Tuexen tcp_update_sack_list(tp, th->th_seq, th->th_seq+tlen); 22665acfd95cSMichael Tuexen /* 22675acfd95cSMichael Tuexen * ACK now, as the next in-sequence segment 22685acfd95cSMichael Tuexen * will clear the DSACK block again 22695acfd95cSMichael Tuexen */ 22705acfd95cSMichael Tuexen tp->t_flags |= TF_ACKNOW; 22715acfd95cSMichael Tuexen } 2272fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 2273fb59c426SYoshinobu Inoue th->th_seq += todrop; 2274fb59c426SYoshinobu Inoue tlen -= todrop; 2275fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 2276fb59c426SYoshinobu Inoue th->th_urp -= todrop; 2277df8bae1dSRodney W. Grimes else { 2278fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2279fb59c426SYoshinobu Inoue th->th_urp = 0; 2280df8bae1dSRodney W. Grimes } 2281df8bae1dSRodney W. Grimes } 2282df8bae1dSRodney W. Grimes 2283df8bae1dSRodney W. Grimes /* 2284df8bae1dSRodney W. Grimes * If new data are received on a connection after the 2285df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 2286df8bae1dSRodney W. Grimes */ 2287df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 2288fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 2289ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2290252ca428SRobert Watson 229107dacf03SAndre Oppermann if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 229207dacf03SAndre Oppermann log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data " 229307dacf03SAndre Oppermann "after socket was closed, " 229407dacf03SAndre Oppermann "sending RST and removing tcpcb\n", 2295e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 2296773673c1SAndre Oppermann free(s, M_TCPLOG); 2297773673c1SAndre Oppermann } 2298df8bae1dSRodney W. Grimes tp = tcp_close(tp); 229978b50714SRobert Watson TCPSTAT_INC(tcps_rcvafterclose); 2300a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2301df8bae1dSRodney W. Grimes goto dropwithreset; 2302df8bae1dSRodney W. Grimes } 2303df8bae1dSRodney W. Grimes 2304df8bae1dSRodney W. Grimes /* 2305df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 2306df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 2307df8bae1dSRodney W. Grimes */ 2308fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 2309df8bae1dSRodney W. Grimes if (todrop > 0) { 231078b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 2311fb59c426SYoshinobu Inoue if (todrop >= tlen) { 231278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 2313df8bae1dSRodney W. Grimes /* 2314df8bae1dSRodney W. Grimes * If window is closed can only take segments at 2315df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 2316df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 2317df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 2318df8bae1dSRodney W. Grimes * and ack. 2319df8bae1dSRodney W. Grimes */ 2320fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2321df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 232278b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinprobe); 2323df8bae1dSRodney W. Grimes } else 2324df8bae1dSRodney W. Grimes goto dropafterack; 2325df8bae1dSRodney W. Grimes } else 232678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2327df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2328fb59c426SYoshinobu Inoue tlen -= todrop; 2329fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 2330df8bae1dSRodney W. Grimes } 2331df8bae1dSRodney W. Grimes 2332df8bae1dSRodney W. Grimes /* 2333df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 2334df8bae1dSRodney W. Grimes * record its timestamp. 2335cf09195bSPaul Saab * NOTE: 2336cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 2337a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2338cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 2339cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 2340cf09195bSPaul Saab * predicated on the sequence space of this segment. 2341cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 2342cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 2343cf09195bSPaul Saab * instead of RFC1323's 2344cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 2345cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 2346cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 2347cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 2348cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 2349df8bae1dSRodney W. Grimes */ 2350be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 2351cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2352cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 2353cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 2354d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 2355a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 2356df8bae1dSRodney W. Grimes } 2357df8bae1dSRodney W. Grimes 2358df8bae1dSRodney W. Grimes /* 2359a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 2360a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 2361a0292f23SGarrett Wollman * later processing; else drop segment and return. 2362a0292f23SGarrett Wollman */ 2363fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 2364a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 2365281a0fd4SPatrick Kelsey (tp->t_flags & TF_NEEDSYN)) { 2366281a0fd4SPatrick Kelsey if (tp->t_state == TCPS_SYN_RECEIVED && 236768bd7ed1SJonathan T. Looney IS_FASTOPEN(tp->t_flags)) { 2368281a0fd4SPatrick Kelsey tp->snd_wnd = tiwin; 2369281a0fd4SPatrick Kelsey cc_conn_init(tp); 2370281a0fd4SPatrick Kelsey } 2371a0292f23SGarrett Wollman goto step6; 2372281a0fd4SPatrick Kelsey } else if (tp->t_flags & TF_ACKNOW) 23735e1aa279SDavid Malone goto dropafterack; 2374a0292f23SGarrett Wollman else 2375a0292f23SGarrett Wollman goto drop; 2376a0292f23SGarrett Wollman } 2377df8bae1dSRodney W. Grimes 2378df8bae1dSRodney W. Grimes /* 2379df8bae1dSRodney W. Grimes * Ack processing. 2380df8bae1dSRodney W. Grimes */ 2381df8bae1dSRodney W. Grimes switch (tp->t_state) { 2382df8bae1dSRodney W. Grimes 2383df8bae1dSRodney W. Grimes /* 2384764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 2385764d8cefSBill Fenner * ESTABLISHED state and continue processing. 2386764d8cefSBill Fenner * The ACK was checked above. 2387df8bae1dSRodney W. Grimes */ 2388df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2389a0292f23SGarrett Wollman 239078b50714SRobert Watson TCPSTAT_INC(tcps_connects); 2391df8bae1dSRodney W. Grimes soisconnected(so); 2392df8bae1dSRodney W. Grimes /* Do window scaling? */ 2393df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2394df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2395df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 2396df8bae1dSRodney W. Grimes } 2397116ef4d6SMichael Tuexen tp->snd_wnd = tiwin; 2398a0292f23SGarrett Wollman /* 2399a0292f23SGarrett Wollman * Make transitions: 2400a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 2401a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 2402a0292f23SGarrett Wollman */ 24039b8b58e0SJonathan Lemon tp->t_starttime = ticks; 240418a75309SPatrick Kelsey if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) { 2405281a0fd4SPatrick Kelsey tcp_fastopen_decrement_counter(tp->t_tfo_pending); 2406281a0fd4SPatrick Kelsey tp->t_tfo_pending = NULL; 2407281a0fd4SPatrick Kelsey 2408281a0fd4SPatrick Kelsey /* 2409281a0fd4SPatrick Kelsey * Account for the ACK of our SYN prior to 2410281a0fd4SPatrick Kelsey * regular ACK processing below. 2411281a0fd4SPatrick Kelsey */ 2412281a0fd4SPatrick Kelsey tp->snd_una++; 2413281a0fd4SPatrick Kelsey } 24148db239dcSMichael Tuexen if (tp->t_flags & TF_NEEDFIN) { 24158db239dcSMichael Tuexen tcp_state_change(tp, TCPS_FIN_WAIT_1); 24168db239dcSMichael Tuexen tp->t_flags &= ~TF_NEEDFIN; 24178db239dcSMichael Tuexen } else { 24188db239dcSMichael Tuexen tcp_state_change(tp, TCPS_ESTABLISHED); 24198db239dcSMichael Tuexen TCP_PROBE5(accept__established, NULL, tp, 24208db239dcSMichael Tuexen m, tp, th); 2421281a0fd4SPatrick Kelsey /* 2422281a0fd4SPatrick Kelsey * TFO connections call cc_conn_init() during SYN 2423281a0fd4SPatrick Kelsey * processing. Calling it again here for such 2424281a0fd4SPatrick Kelsey * connections is not harmless as it would undo the 2425281a0fd4SPatrick Kelsey * snd_cwnd reduction that occurs when a TFO SYN|ACK 2426281a0fd4SPatrick Kelsey * is retransmitted. 2427281a0fd4SPatrick Kelsey */ 242868bd7ed1SJonathan T. Looney if (!IS_FASTOPEN(tp->t_flags)) 2429dbc42409SLawrence Stewart cc_conn_init(tp); 24309077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 24317ff19458SPaul Traina } 2432a0292f23SGarrett Wollman /* 2433a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 2434a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 2435a0292f23SGarrett Wollman */ 2436fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 2437c28440dbSRandall Stewart (void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0, 2438a0292f23SGarrett Wollman (struct mbuf *)0); 243960ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq - 1; 244093b0017fSPhilippe Charnier /* FALLTHROUGH */ 2441df8bae1dSRodney W. Grimes 2442df8bae1dSRodney W. Grimes /* 2443df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2444df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 2445fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 2446fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 2447df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 2448df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 2449df8bae1dSRodney W. Grimes */ 2450df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2451df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2452df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2453df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 2454df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2455df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 24565a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 245778b50714SRobert Watson TCPSTAT_INC(tcps_rcvacktoomuch); 24585a53ca16SPaul Saab goto dropafterack; 24595a53ca16SPaul Saab } 24603529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2461fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 2462fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 2463021eaf79SHiren Panchasara sack_changed = tcp_sack_doack(tp, &to, th->th_ack); 246412eeb81fSHiren Panchasara else 246512eeb81fSHiren Panchasara /* 246612eeb81fSHiren Panchasara * Reset the value so that previous (valid) value 246712eeb81fSHiren Panchasara * from the last ack with SACK doesn't get used. 246812eeb81fSHiren Panchasara */ 246912eeb81fSHiren Panchasara tp->sackhint.sacked_bytes = 0; 247039bc9de5SLawrence Stewart 2471bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 247239bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 247339bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 2474bd79708dSJonathan T. Looney #endif 247539bc9de5SLawrence Stewart 2476fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 24770c39d38dSGleb Smirnoff u_int maxseg; 24780c39d38dSGleb Smirnoff 24790c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 2480021eaf79SHiren Panchasara if (tlen == 0 && 2481021eaf79SHiren Panchasara (tiwin == tp->snd_wnd || 2482021eaf79SHiren Panchasara (tp->t_flags & TF_SACK_PERMIT))) { 248362b90589SPeter Wemm /* 248462b90589SPeter Wemm * If this is the first time we've seen a 248562b90589SPeter Wemm * FIN from the remote, this is not a 248662b90589SPeter Wemm * duplicate and it needs to be processed 248762b90589SPeter Wemm * normally. This happens during a 248862b90589SPeter Wemm * simultaneous close. 248962b90589SPeter Wemm */ 249062b90589SPeter Wemm if ((thflags & TH_FIN) && 249162b90589SPeter Wemm (TCPS_HAVERCVDFIN(tp->t_state) == 0)) { 249262b90589SPeter Wemm tp->t_dupacks = 0; 249362b90589SPeter Wemm break; 249462b90589SPeter Wemm } 249578b50714SRobert Watson TCPSTAT_INC(tcps_rcvdupack); 2496df8bae1dSRodney W. Grimes /* 2497df8bae1dSRodney W. Grimes * If we have outstanding data (other than 2498df8bae1dSRodney W. Grimes * a window probe), this is a completely 2499df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 25005f30ec9bSEitan Adler * change and FIN isn't set), 25015f30ec9bSEitan Adler * the ack is the biggest we've 2502df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 2503a4641f4eSPedro F. Giffuni * threshold of them, assume a packet 2504df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 2505df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 2506df8bae1dSRodney W. Grimes * window so we send only this one 2507df8bae1dSRodney W. Grimes * packet. 2508df8bae1dSRodney W. Grimes * 2509df8bae1dSRodney W. Grimes * We know we're losing at the current 2510df8bae1dSRodney W. Grimes * window size so do congestion avoidance 2511df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 2512df8bae1dSRodney W. Grimes * and pull our congestion window back to 2513df8bae1dSRodney W. Grimes * the new ssthresh). 2514df8bae1dSRodney W. Grimes * 2515df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 2516df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 2517df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 2518df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 2519df8bae1dSRodney W. Grimes * network. 2520f2512ba1SRui Paulo * 2521f2512ba1SRui Paulo * When using TCP ECN, notify the peer that 2522f2512ba1SRui Paulo * we reduced the cwnd. 2523df8bae1dSRodney W. Grimes */ 2524021eaf79SHiren Panchasara /* 2525021eaf79SHiren Panchasara * Following 2 kinds of acks should not affect 2526021eaf79SHiren Panchasara * dupack counting: 2527021eaf79SHiren Panchasara * 1) Old acks 2528021eaf79SHiren Panchasara * 2) Acks with SACK but without any new SACK 2529021eaf79SHiren Panchasara * information in them. These could result from 2530021eaf79SHiren Panchasara * any anomaly in the network like a switch 2531021eaf79SHiren Panchasara * duplicating packets or a possible DoS attack. 2532021eaf79SHiren Panchasara */ 2533021eaf79SHiren Panchasara if (th->th_ack != tp->snd_una || 2534021eaf79SHiren Panchasara ((tp->t_flags & TF_SACK_PERMIT) && 2535021eaf79SHiren Panchasara !sack_changed)) 2536021eaf79SHiren Panchasara break; 2537021eaf79SHiren Panchasara else if (!tcp_timer_active(tp, TT_REXMT)) 2538df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2539cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 2540dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 25414b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 25424b7b743cSLawrence Stewart CC_DUPACK); 25433529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2544dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2545808f11b7SPaul Saab int awnd; 254625e6f9edSPaul Saab 254725e6f9edSPaul Saab /* 254825e6f9edSPaul Saab * Compute the amount of data in flight first. 25494c3972f0SHiren Panchasara * We can inject new data into the pipe iff 255025e6f9edSPaul Saab * we have less than 1/2 the original window's 255125e6f9edSPaul Saab * worth of data in flight. 255225e6f9edSPaul Saab */ 255312eeb81fSHiren Panchasara if (V_tcp_do_rfc6675_pipe) 255412eeb81fSHiren Panchasara awnd = tcp_compute_pipe(tp); 255512eeb81fSHiren Panchasara else 2556808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 25570077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 255812eeb81fSHiren Panchasara 2559808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 25600c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 256125e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 256225e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 256325e6f9edSPaul Saab } 256425e6f9edSPaul Saab } else 25650c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 256655bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 256746f58482SJonathan Lemon goto drop; 2568cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 2569cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 2570a0445c2eSJayanth Vijayaraghavan 2571a0445c2eSJayanth Vijayaraghavan /* 2572a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 2573a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 2574a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 2575a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 2576a0445c2eSJayanth Vijayaraghavan * recovery. 2577a0445c2eSJayanth Vijayaraghavan */ 25783529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 2579dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2580a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 2581a0445c2eSJayanth Vijayaraghavan break; 2582a0445c2eSJayanth Vijayaraghavan } 2583dbc42409SLawrence Stewart } else { 2584a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 25859d11646dSJeffrey Hsu tp->snd_recover)) { 2586cb942153SJeffrey Hsu tp->t_dupacks = 0; 2587cb942153SJeffrey Hsu break; 258846f58482SJonathan Lemon } 2589a0445c2eSJayanth Vijayaraghavan } 2590dbc42409SLawrence Stewart /* Congestion signal before ack. */ 2591dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_NDUPACK); 25924b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 25934b7b743cSLawrence Stewart CC_DUPACK); 2594b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 25959b8b58e0SJonathan Lemon tp->t_rtttime = 0; 25963529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 259778b50714SRobert Watson TCPSTAT_INC( 259878b50714SRobert Watson tcps_sack_recovery_episode); 2599a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 26000c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 260155bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 26026d90faf3SPaul Saab goto drop; 26036d90faf3SPaul Saab } 2604fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 26050c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 260655bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 260748d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 2608302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 2609302ce8d6SAndre Oppermann __func__)); 2610df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 26110c39d38dSGleb Smirnoff maxseg * 261248d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 2613df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 2614df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 2615df8bae1dSRodney W. Grimes goto drop; 2616603724d3SBjoern A. Zeeb } else if (V_tcp_do_rfc3042) { 261762d4443fSHiren Panchasara /* 261862d4443fSHiren Panchasara * Process first and second duplicate 261962d4443fSHiren Panchasara * ACKs. Each indicates a segment 262062d4443fSHiren Panchasara * leaving the network, creating room 262162d4443fSHiren Panchasara * for more. Make sure we can send a 262262d4443fSHiren Panchasara * packet on reception of each duplicate 262362d4443fSHiren Panchasara * ACK by increasing snd_cwnd by one 262462d4443fSHiren Panchasara * segment. Restore the original 262562d4443fSHiren Panchasara * snd_cwnd after packet transmission. 262662d4443fSHiren Panchasara */ 26274b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 26284b7b743cSLawrence Stewart CC_DUPACK); 26293ac12506SJonathan T. Looney uint32_t oldcwnd = tp->snd_cwnd; 263048d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 263148d2549cSJeffrey Hsu u_int sent; 26325628dd08SAndre Oppermann int avail; 263389c02376SJeffrey Hsu 2634582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 2635582a954bSJeffrey Hsu tp->t_dupacks == 2, 2636302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 2637302ce8d6SAndre Oppermann __func__)); 263861a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 263948d2549cSJeffrey Hsu tp->snd_limited = 0; 264061a36e3dSJeffrey Hsu tp->snd_cwnd = 264161a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 264261a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 26430c39d38dSGleb Smirnoff maxseg; 26445628dd08SAndre Oppermann /* 26455628dd08SAndre Oppermann * Only call tcp_output when there 26465628dd08SAndre Oppermann * is new data available to be sent. 26475628dd08SAndre Oppermann * Otherwise we would send pure ACKs. 26485628dd08SAndre Oppermann */ 26495628dd08SAndre Oppermann SOCKBUF_LOCK(&so->so_snd); 2650cfa6009eSGleb Smirnoff avail = sbavail(&so->so_snd) - 26515628dd08SAndre Oppermann (tp->snd_nxt - tp->snd_una); 26525628dd08SAndre Oppermann SOCKBUF_UNLOCK(&so->so_snd); 26535628dd08SAndre Oppermann if (avail > 0) 265455bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 265548d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 26560c39d38dSGleb Smirnoff if (sent > maxseg) { 265789c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 265889c02376SJeffrey Hsu tp->snd_limited == 0) || 26590c39d38dSGleb Smirnoff (sent == maxseg + 1 && 266089c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 2661302ce8d6SAndre Oppermann ("%s: sent too much", 2662302ce8d6SAndre Oppermann __func__)); 266348d2549cSJeffrey Hsu tp->snd_limited = 2; 266448d2549cSJeffrey Hsu } else if (sent > 0) 266548d2549cSJeffrey Hsu ++tp->snd_limited; 2666582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 2667582a954bSJeffrey Hsu goto drop; 2668df8bae1dSRodney W. Grimes } 2669021eaf79SHiren Panchasara } 2670df8bae1dSRodney W. Grimes break; 2671021eaf79SHiren Panchasara } else { 2672021eaf79SHiren Panchasara /* 2673021eaf79SHiren Panchasara * This ack is advancing the left edge, reset the 2674021eaf79SHiren Panchasara * counter. 2675021eaf79SHiren Panchasara */ 2676021eaf79SHiren Panchasara tp->t_dupacks = 0; 2677021eaf79SHiren Panchasara /* 2678021eaf79SHiren Panchasara * If this ack also has new SACK info, increment the 2679021eaf79SHiren Panchasara * counter as per rfc6675. 2680021eaf79SHiren Panchasara */ 2681021eaf79SHiren Panchasara if ((tp->t_flags & TF_SACK_PERMIT) && sack_changed) 2682021eaf79SHiren Panchasara tp->t_dupacks++; 2683df8bae1dSRodney W. Grimes } 2684cb942153SJeffrey Hsu 2685302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2686302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 2687cb942153SJeffrey Hsu 2688df8bae1dSRodney W. Grimes /* 2689df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2690df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2691df8bae1dSRodney W. Grimes */ 2692dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2693cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 26943529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 26956d90faf3SPaul Saab tcp_sack_partialack(tp, th); 26966d90faf3SPaul Saab else 2697c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2698dbc42409SLawrence Stewart } else 2699dbc42409SLawrence Stewart cc_post_recovery(tp, th); 270046f58482SJonathan Lemon } 2701a0292f23SGarrett Wollman /* 2702a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2703a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2704a0292f23SGarrett Wollman */ 2705a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2706a0292f23SGarrett Wollman /* 2707a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2708a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 270907e43e10SAndras Olah * synchronized). Go to non-starred state, 271007e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 271107e43e10SAndras Olah * we can do window scaling. 2712a0292f23SGarrett Wollman */ 2713a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2714a0292f23SGarrett Wollman tp->snd_una++; 271507e43e10SAndras Olah /* Do window scaling? */ 271607e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 271707e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 271807e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2719464fcfbcSAndre Oppermann /* Send window already scaled. */ 272007e43e10SAndras Olah } 2721a0292f23SGarrett Wollman } 2722a0292f23SGarrett Wollman 2723a0292f23SGarrett Wollman process_ACK: 27248501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 27257cfc6904SRobert Watson 2726dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 2727b8c2cd15SJonathan T. Looney KASSERT(acked >= 0, ("%s: acked unexepectedly negative " 2728b8c2cd15SJonathan T. Looney "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__, 2729b8c2cd15SJonathan T. Looney tp->snd_una, th->th_ack, tp, m)); 27304b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvackpack, nsegs); 273178b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 2732df8bae1dSRodney W. Grimes 2733df8bae1dSRodney W. Grimes /* 27349b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 27359b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 27369b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 27379b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 27389b8b58e0SJonathan Lemon * we left off. 27399b8b58e0SJonathan Lemon */ 274010d20c84SMatt Macy if (tp->t_rxtshift == 1 && 274110d20c84SMatt Macy tp->t_flags & TF_PREVVALID && 274210d20c84SMatt Macy tp->t_badrxtwin && 274310d20c84SMatt Macy SEQ_LT(to.to_tsecr, tp->t_badrxtwin)) 2744dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 27459b8b58e0SJonathan Lemon 27469b8b58e0SJonathan Lemon /* 2747df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2748df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2749df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2750df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2751df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2752df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2753df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2754fa55172bSMatthew Dillon * 2755fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2756fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2757fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2758fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2759df8bae1dSRodney W. Grimes */ 2760d8951c8aSBjoern A. Zeeb if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) { 27613ac12506SJonathan T. Looney uint32_t t; 2762d8951c8aSBjoern A. Zeeb 2763d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 2764d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 2765d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 2766d8951c8aSBjoern A. Zeeb tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1); 2767fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2768eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2769eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 27709b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2771fa55172bSMatthew Dillon } 2772df8bae1dSRodney W. Grimes 2773df8bae1dSRodney W. Grimes /* 2774df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2775df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2776df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2777df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2778df8bae1dSRodney W. Grimes */ 2779fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 2780b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2781df8bae1dSRodney W. Grimes needoutput = 1; 2782b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2783b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2784a0292f23SGarrett Wollman 2785a0292f23SGarrett Wollman /* 2786a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2787a0292f23SGarrett Wollman * skip rest of ACK processing. 2788a0292f23SGarrett Wollman */ 2789a0292f23SGarrett Wollman if (acked == 0) 2790a0292f23SGarrett Wollman goto step6; 2791a0292f23SGarrett Wollman 2792df8bae1dSRodney W. Grimes /* 2793dbc42409SLawrence Stewart * Let the congestion control algorithm update congestion 2794dbc42409SLawrence Stewart * control related information. This typically means increasing 2795dbc42409SLawrence Stewart * the congestion window. 2796df8bae1dSRodney W. Grimes */ 27974b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, CC_ACK); 2798dbc42409SLawrence Stewart 27995905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2800cfa6009eSGleb Smirnoff if (acked > sbavail(&so->so_snd)) { 2801b8c2cd15SJonathan T. Looney if (tp->snd_wnd >= sbavail(&so->so_snd)) 2802cfa6009eSGleb Smirnoff tp->snd_wnd -= sbavail(&so->so_snd); 2803b8c2cd15SJonathan T. Looney else 2804b8c2cd15SJonathan T. Looney tp->snd_wnd = 0; 2805c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, 2806cfa6009eSGleb Smirnoff (int)sbavail(&so->so_snd)); 2807df8bae1dSRodney W. Grimes ourfinisacked = 1; 2808df8bae1dSRodney W. Grimes } else { 2809c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, acked); 28103ac12506SJonathan T. Looney if (tp->snd_wnd >= (uint32_t) acked) 281160ee3bb2SAndre Oppermann tp->snd_wnd -= acked; 2812b8c2cd15SJonathan T. Looney else 2813b8c2cd15SJonathan T. Looney tp->snd_wnd = 0; 2814df8bae1dSRodney W. Grimes ourfinisacked = 0; 2815df8bae1dSRodney W. Grimes } 2816564aab1fSAndre Oppermann /* NB: sowwakeup_locked() does an implicit unlock. */ 28171e4d7da7SRobert Watson sowwakeup_locked(so); 2818c11a15bfSGleb Smirnoff m_freem(mfree); 2819e8949f74SAndre Oppermann /* Detect una wraparound. */ 2820dbc42409SLawrence Stewart if (!IN_RECOVERY(tp->t_flags) && 28219d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 28229d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 28239d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 2824dbc42409SLawrence Stewart /* XXXLAS: Can this be moved up into cc_post_recovery? */ 2825dbc42409SLawrence Stewart if (IN_RECOVERY(tp->t_flags) && 282624cb0f22SLawrence Stewart SEQ_GEQ(th->th_ack, tp->snd_recover)) { 2827dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 282824cb0f22SLawrence Stewart } 2829fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 28303529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 28316d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 28326d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 28336d90faf3SPaul Saab } 2834df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2835df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2836df8bae1dSRodney W. Grimes 2837df8bae1dSRodney W. Grimes switch (tp->t_state) { 2838df8bae1dSRodney W. Grimes 2839df8bae1dSRodney W. Grimes /* 2840df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2841df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2842df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2843df8bae1dSRodney W. Grimes */ 2844df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2845df8bae1dSRodney W. Grimes if (ourfinisacked) { 2846df8bae1dSRodney W. Grimes /* 2847df8bae1dSRodney W. Grimes * If we can't receive any more 2848df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2849df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2850df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2851df8bae1dSRodney W. Grimes * we'll hang forever. 2852e8949f74SAndre Oppermann * 2853e8949f74SAndre Oppermann * XXXjl: 2854340c35deSJonathan Lemon * we should release the tp also, and use a 2855340c35deSJonathan Lemon * compressed state. 2856340c35deSJonathan Lemon */ 2857c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 285803e49181SSeigo Tanimura soisdisconnected(so); 28599077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 28609077f387SGleb Smirnoff (tcp_fast_finwait2_recycle ? 28619077f387SGleb Smirnoff tcp_finwait2_timeout : 28629077f387SGleb Smirnoff TP_MAXIDLE(tp))); 28634cc20ab1SSeigo Tanimura } 286457f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_2); 2865df8bae1dSRodney W. Grimes } 2866df8bae1dSRodney W. Grimes break; 2867df8bae1dSRodney W. Grimes 2868df8bae1dSRodney W. Grimes /* 2869df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2870df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2871df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2872df8bae1dSRodney W. Grimes * the segment. 2873df8bae1dSRodney W. Grimes */ 2874df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2875df8bae1dSRodney W. Grimes if (ourfinisacked) { 2876ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 287711a20fb8SJeffrey Hsu tcp_twstart(tp); 2878340c35deSJonathan Lemon m_freem(m); 2879679d9708SAndre Oppermann return; 2880df8bae1dSRodney W. Grimes } 2881df8bae1dSRodney W. Grimes break; 2882df8bae1dSRodney W. Grimes 2883df8bae1dSRodney W. Grimes /* 2884df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2885df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2886df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2887df8bae1dSRodney W. Grimes * enter the closed state and return. 2888df8bae1dSRodney W. Grimes */ 2889df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2890df8bae1dSRodney W. Grimes if (ourfinisacked) { 2891ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2892df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2893df8bae1dSRodney W. Grimes goto drop; 2894df8bae1dSRodney W. Grimes } 2895df8bae1dSRodney W. Grimes break; 2896df8bae1dSRodney W. Grimes } 2897df8bae1dSRodney W. Grimes } 2898df8bae1dSRodney W. Grimes 2899df8bae1dSRodney W. Grimes step6: 29008501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 29017cfc6904SRobert Watson 2902df8bae1dSRodney W. Grimes /* 290360ee3bb2SAndre Oppermann * Update window information. 290460ee3bb2SAndre Oppermann * Don't look at window if no ACK: TAC's send garbage on first SYN. 2905df8bae1dSRodney W. Grimes */ 290660ee3bb2SAndre Oppermann if ((thflags & TH_ACK) && 290760ee3bb2SAndre Oppermann (SEQ_LT(tp->snd_wl1, th->th_seq) || 290860ee3bb2SAndre Oppermann (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 290960ee3bb2SAndre Oppermann (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 291060ee3bb2SAndre Oppermann /* keep track of pure window updates */ 291160ee3bb2SAndre Oppermann if (tlen == 0 && 291260ee3bb2SAndre Oppermann tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 291378b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinupd); 2914df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 291560ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 291660ee3bb2SAndre Oppermann tp->snd_wl2 = th->th_ack; 2917df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2918df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 291960ee3bb2SAndre Oppermann needoutput = 1; 2920df8bae1dSRodney W. Grimes } 2921df8bae1dSRodney W. Grimes 2922df8bae1dSRodney W. Grimes /* 2923df8bae1dSRodney W. Grimes * Process segments with URG. 2924df8bae1dSRodney W. Grimes */ 2925fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2926df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2927df8bae1dSRodney W. Grimes /* 2928df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2929df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2930df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2931df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2932df8bae1dSRodney W. Grimes */ 293318ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2934cfa6009eSGleb Smirnoff if (th->th_urp + sbavail(&so->so_rcv) > sb_max) { 2935fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2936fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 293718ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2938df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2939df8bae1dSRodney W. Grimes } 2940df8bae1dSRodney W. Grimes /* 2941df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2942df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2943df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2944df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2945df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2946df8bae1dSRodney W. Grimes * 2947df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2948df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2949df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2950df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2951df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2952df8bae1dSRodney W. Grimes * spec states (in one of two places). 2953df8bae1dSRodney W. Grimes */ 2954fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2955fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2956cfa6009eSGleb Smirnoff so->so_oobmark = sbavail(&so->so_rcv) + 2957df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2958927c5ceaSRobert Watson if (so->so_oobmark == 0) 2959c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 2960df8bae1dSRodney W. Grimes sohasoutofband(so); 2961df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2962df8bae1dSRodney W. Grimes } 296318ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2964df8bae1dSRodney W. Grimes /* 2965df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2966df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2967df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2968df8bae1dSRodney W. Grimes * data may creep in... ick. 2969df8bae1dSRodney W. Grimes */ 29703ac12506SJonathan T. Looney if (th->th_urp <= (uint32_t)tlen && 297130613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 297230613f56SJeffrey Hsu /* hdr drop is delayed */ 297330613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 297430613f56SJeffrey Hsu } 2975c068736aSJeffrey Hsu } else { 2976df8bae1dSRodney W. Grimes /* 2977df8bae1dSRodney W. Grimes * If no out of band data is expected, 2978df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2979df8bae1dSRodney W. Grimes * with the receive window. 2980df8bae1dSRodney W. Grimes */ 2981df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2982df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2983c068736aSJeffrey Hsu } 2984df8bae1dSRodney W. Grimes dodata: /* XXX */ 29858501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 29867cfc6904SRobert Watson 2987df8bae1dSRodney W. Grimes /* 2988df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2989df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2990df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2991df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2992df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2993df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2994df8bae1dSRodney W. Grimes */ 2995281a0fd4SPatrick Kelsey tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) && 299668bd7ed1SJonathan T. Looney IS_FASTOPEN(tp->t_flags)); 2997281a0fd4SPatrick Kelsey if ((tlen || (thflags & TH_FIN) || tfo_syn) && 2998df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 299969e03620SPaul Saab tcp_seq save_start = th->th_seq; 30005acfd95cSMichael Tuexen tcp_seq save_rnxt = tp->rcv_nxt; 30015acfd95cSMichael Tuexen int save_tlen = tlen; 3002fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 3003e4b64281SJesper Skriver /* 3004c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 3005c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 3006c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 3007c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 3008c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 3009c068736aSJeffrey Hsu * and removal from the queue and repetition of various 3010c068736aSJeffrey Hsu * conversions. 3011c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 3012c068736aSJeffrey Hsu * immediately when segments are out of order (so 3013c068736aSJeffrey Hsu * fast retransmit can work). 3014e4b64281SJesper Skriver */ 30154741bfcbSPatrick Kelsey if (th->th_seq == tp->rcv_nxt && 3016c28440dbSRandall Stewart SEGQ_EMPTY(tp) && 3017281a0fd4SPatrick Kelsey (TCPS_HAVEESTABLISHED(tp->t_state) || 3018281a0fd4SPatrick Kelsey tfo_syn)) { 3019281a0fd4SPatrick Kelsey if (DELAY_ACK(tp, tlen) || tfo_syn) 30203bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 3021e4b64281SJesper Skriver else 3022e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 3023e4b64281SJesper Skriver tp->rcv_nxt += tlen; 3024e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 302578b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 302678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 30271e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3028c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 3029c1c36a2cSMike Silbersack m_freem(m); 3030c1c36a2cSMike Silbersack else 3031651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 3032e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 30331e4d7da7SRobert Watson sorwakeup_locked(so); 3034e4b64281SJesper Skriver } else { 3035e8949f74SAndre Oppermann /* 3036e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 3037e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 3038e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 3039e8949f74SAndre Oppermann * when trimming from the head. 3040e8949f74SAndre Oppermann */ 30415acfd95cSMichael Tuexen tcp_seq temp = save_start; 30425acfd95cSMichael Tuexen thflags = tcp_reass(tp, th, &temp, &tlen, m); 3043e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 3044e4b64281SJesper Skriver } 30455acfd95cSMichael Tuexen if (tp->t_flags & TF_SACK_PERMIT) { 30465acfd95cSMichael Tuexen if (((tlen == 0) && (save_tlen > 0) && 30475acfd95cSMichael Tuexen (SEQ_LT(save_start, save_rnxt)))) { 3048*b5a154d8SMichael Tuexen /* 3049*b5a154d8SMichael Tuexen * DSACK actually handled in the fastpath 3050*b5a154d8SMichael Tuexen * above. 3051*b5a154d8SMichael Tuexen */ 30525acfd95cSMichael Tuexen tcp_update_sack_list(tp, save_start, save_start + save_tlen); 30535acfd95cSMichael Tuexen } else 30545acfd95cSMichael Tuexen if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) { 3055*b5a154d8SMichael Tuexen /* 3056*b5a154d8SMichael Tuexen * Cleaning sackblks by using zero length 3057*b5a154d8SMichael Tuexen * update. 3058*b5a154d8SMichael Tuexen */ 30595acfd95cSMichael Tuexen tcp_update_sack_list(tp, save_start, save_start); 30605acfd95cSMichael Tuexen } else 30615acfd95cSMichael Tuexen if ((tlen > 0) && (tlen >= save_tlen)) { 3062*b5a154d8SMichael Tuexen /* Update of sackblks. */ 30635acfd95cSMichael Tuexen tcp_update_sack_list(tp, save_start, save_start + save_tlen); 30645acfd95cSMichael Tuexen } else 30655acfd95cSMichael Tuexen if (tlen > 0) { 3066f194524fSAndre Oppermann tcp_update_sack_list(tp, save_start, save_start+tlen); 30675acfd95cSMichael Tuexen } 30685acfd95cSMichael Tuexen } 3069302ce8d6SAndre Oppermann #if 0 3070df8bae1dSRodney W. Grimes /* 3071df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 3072df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 3073df8bae1dSRodney W. Grimes * buffer size. 3074302ce8d6SAndre Oppermann * XXX: Unused. 3075df8bae1dSRodney W. Grimes */ 3076f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) 3077df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 3078f701e30dSJohn Baldwin else 3079f701e30dSJohn Baldwin len = so->so_rcv.sb_hiwat; 3080302ce8d6SAndre Oppermann #endif 3081df8bae1dSRodney W. Grimes } else { 3082df8bae1dSRodney W. Grimes m_freem(m); 3083fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 3084df8bae1dSRodney W. Grimes } 3085df8bae1dSRodney W. Grimes 3086df8bae1dSRodney W. Grimes /* 3087df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 3088df8bae1dSRodney W. Grimes * that the connection is closing. 3089df8bae1dSRodney W. Grimes */ 3090fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 3091df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 3092df8bae1dSRodney W. Grimes socantrcvmore(so); 3093a0292f23SGarrett Wollman /* 3094a0292f23SGarrett Wollman * If connection is half-synchronized 3095d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 3096a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 3097a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 3098a0292f23SGarrett Wollman * more input can be expected, send ACK now. 3099a0292f23SGarrett Wollman */ 31003bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 31013bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 3102a0292f23SGarrett Wollman else 3103df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 3104df8bae1dSRodney W. Grimes tp->rcv_nxt++; 3105df8bae1dSRodney W. Grimes } 3106df8bae1dSRodney W. Grimes switch (tp->t_state) { 3107df8bae1dSRodney W. Grimes 3108df8bae1dSRodney W. Grimes /* 3109df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 3110df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 3111df8bae1dSRodney W. Grimes */ 3112df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 31139b8b58e0SJonathan Lemon tp->t_starttime = ticks; 31149b8b58e0SJonathan Lemon /* FALLTHROUGH */ 3115df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 311657f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSE_WAIT); 3117df8bae1dSRodney W. Grimes break; 3118df8bae1dSRodney W. Grimes 3119df8bae1dSRodney W. Grimes /* 3120df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 3121df8bae1dSRodney W. Grimes * enter the CLOSING state. 3122df8bae1dSRodney W. Grimes */ 3123df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 312457f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSING); 3125df8bae1dSRodney W. Grimes break; 3126df8bae1dSRodney W. Grimes 3127df8bae1dSRodney W. Grimes /* 3128df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 3129df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 3130df8bae1dSRodney W. Grimes * standard timers. 3131df8bae1dSRodney W. Grimes */ 3132df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 3133ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 3134252ca428SRobert Watson 3135340c35deSJonathan Lemon tcp_twstart(tp); 3136679d9708SAndre Oppermann return; 3137df8bae1dSRodney W. Grimes } 3138df8bae1dSRodney W. Grimes } 3139610ee2f9SDavid Greenman #ifdef TCPDEBUG 31404cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3141fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 3142fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3143610ee2f9SDavid Greenman #endif 31442b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 3145df8bae1dSRodney W. Grimes 3146df8bae1dSRodney W. Grimes /* 3147df8bae1dSRodney W. Grimes * Return any desired output. 3148df8bae1dSRodney W. Grimes */ 3149df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 315055bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 31517792ea27SJeffrey Hsu 3152a14c749fSJonathan Lemon check_delack: 31538501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 3154252ca428SRobert Watson 3155a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 31563bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 3157b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 31583bfd6421SJonathan Lemon } 31598501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3160679d9708SAndre Oppermann return; 3161df8bae1dSRodney W. Grimes 3162df8bae1dSRodney W. Grimes dropafterack: 3163df8bae1dSRodney W. Grimes /* 3164df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 3165df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 316680ab7c0eSGarrett Wollman * 316780ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 316880ab7c0eSGarrett Wollman * paths to this code happen after packets containing 316980ab7c0eSGarrett Wollman * RST have been dropped. 317080ab7c0eSGarrett Wollman * 317180ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 317280ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 317380ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 317480ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 317580ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 317680ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 3177df8bae1dSRodney W. Grimes */ 3178fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 3179fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 3180a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 3181a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 3182a57815efSBosko Milekic goto dropwithreset; 3183a57815efSBosko Milekic } 3184a0292f23SGarrett Wollman #ifdef TCPDEBUG 31854cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3186fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3187fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3188a0292f23SGarrett Wollman #endif 31892b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 3190df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 319155bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 31928501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3193d6915262SRobert Watson m_freem(m); 3194679d9708SAndre Oppermann return; 3195df8bae1dSRodney W. Grimes 3196df8bae1dSRodney W. Grimes dropwithreset: 3197a0ca0871SRobert Watson if (tp != NULL) { 3198302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 31998501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3200dd8ac7f9SRobert Watson } else 3201a0ca0871SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 3202679d9708SAndre Oppermann return; 3203df8bae1dSRodney W. Grimes 3204df8bae1dSRodney W. Grimes drop: 3205df8bae1dSRodney W. Grimes /* 3206df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 3207df8bae1dSRodney W. Grimes */ 3208610ee2f9SDavid Greenman #ifdef TCPDEBUG 32091c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 3210fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3211fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3212a0292f23SGarrett Wollman #endif 32132b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 32141c53f806SRobert Watson if (tp != NULL) 32158501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3216d6915262SRobert Watson m_freem(m); 3217302ce8d6SAndre Oppermann } 3218302ce8d6SAndre Oppermann 3219302ce8d6SAndre Oppermann /* 32200ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 32210ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 32220ca3f933SAndre Oppermann * tp may be NULL. 3223302ce8d6SAndre Oppermann */ 322455bceb1eSRandall Stewart void 3225302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 3226302ce8d6SAndre Oppermann int tlen, int rstreason) 3227302ce8d6SAndre Oppermann { 3228b287c6c7SBjoern A. Zeeb #ifdef INET 3229302ce8d6SAndre Oppermann struct ip *ip; 3230b287c6c7SBjoern A. Zeeb #endif 3231302ce8d6SAndre Oppermann #ifdef INET6 3232302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 3233302ce8d6SAndre Oppermann #endif 32347a3244ccSRobert Watson 32357a3244ccSRobert Watson if (tp != NULL) { 32368501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 32377a3244ccSRobert Watson } 32387a3244ccSRobert Watson 32390ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 3240302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 3241302ce8d6SAndre Oppermann goto drop; 3242302ce8d6SAndre Oppermann #ifdef INET6 3243302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 3244302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 3245302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3246302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3247302ce8d6SAndre Oppermann goto drop; 3248302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 3249b287c6c7SBjoern A. Zeeb } 3250302ce8d6SAndre Oppermann #endif 3251b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3252b287c6c7SBjoern A. Zeeb else 3253b287c6c7SBjoern A. Zeeb #endif 3254b287c6c7SBjoern A. Zeeb #ifdef INET 3255302ce8d6SAndre Oppermann { 3256302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 3257302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3258302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3259302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3260302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3261302ce8d6SAndre Oppermann goto drop; 3262302ce8d6SAndre Oppermann } 3263b287c6c7SBjoern A. Zeeb #endif 3264302ce8d6SAndre Oppermann 3265302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 3266302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 3267302ce8d6SAndre Oppermann goto drop; 3268302ce8d6SAndre Oppermann 3269302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 3270302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 3271302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 3272302ce8d6SAndre Oppermann th->th_ack, TH_RST); 3273302ce8d6SAndre Oppermann } else { 3274302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 3275302ce8d6SAndre Oppermann tlen++; 32764ddd5aadSMichael Tuexen if (th->th_flags & TH_FIN) 32774ddd5aadSMichael Tuexen tlen++; 3278302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 3279302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 3280302ce8d6SAndre Oppermann } 3281302ce8d6SAndre Oppermann return; 3282302ce8d6SAndre Oppermann drop: 3283302ce8d6SAndre Oppermann m_freem(m); 3284df8bae1dSRodney W. Grimes } 3285df8bae1dSRodney W. Grimes 3286be2ac88cSJonathan Lemon /* 3287be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 3288be2ac88cSJonathan Lemon */ 328955bceb1eSRandall Stewart void 3290ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 3291df8bae1dSRodney W. Grimes { 3292df8bae1dSRodney W. Grimes int opt, optlen; 3293df8bae1dSRodney W. Grimes 3294be2ac88cSJonathan Lemon to->to_flags = 0; 3295df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 3296df8bae1dSRodney W. Grimes opt = cp[0]; 3297df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 3298df8bae1dSRodney W. Grimes break; 3299df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 3300df8bae1dSRodney W. Grimes optlen = 1; 3301df8bae1dSRodney W. Grimes else { 3302b474779fSJun-ichiro itojun Hagino if (cnt < 2) 3303b474779fSJun-ichiro itojun Hagino break; 3304df8bae1dSRodney W. Grimes optlen = cp[1]; 3305b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 3306df8bae1dSRodney W. Grimes break; 3307df8bae1dSRodney W. Grimes } 3308df8bae1dSRodney W. Grimes switch (opt) { 3309df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 3310df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 3311df8bae1dSRodney W. Grimes continue; 3312f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3313df8bae1dSRodney W. Grimes continue; 3314be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 3315be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 3316be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 3317fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 3318df8bae1dSRodney W. Grimes break; 3319df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 3320df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 3321df8bae1dSRodney W. Grimes continue; 3322f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3323df8bae1dSRodney W. Grimes continue; 3324be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 332502a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 3326df8bae1dSRodney W. Grimes break; 3327df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 3328df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 3329df8bae1dSRodney W. Grimes continue; 3330be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 3331a0292f23SGarrett Wollman bcopy((char *)cp + 2, 3332a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 3333fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 3334a0292f23SGarrett Wollman bcopy((char *)cp + 6, 3335a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 3336fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 3337df8bae1dSRodney W. Grimes break; 33381cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 3339fcf59617SAndrey V. Elsukov /* 3340fcf59617SAndrey V. Elsukov * In order to reply to a host which has set the 3341fcf59617SAndrey V. Elsukov * TCP_SIGNATURE option in its initial SYN, we have 3342fcf59617SAndrey V. Elsukov * to record the fact that the option was observed 3343fcf59617SAndrey V. Elsukov * here for the syncache code to perform the correct 3344fcf59617SAndrey V. Elsukov * response. 3345fcf59617SAndrey V. Elsukov */ 33461cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 33471cfd4b53SBruce M Simpson continue; 3348df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 3349df47e437SAndre Oppermann to->to_signature = cp + 2; 33501cfd4b53SBruce M Simpson break; 33516d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 3352f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 33536d90faf3SPaul Saab continue; 3354f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3355f72167f4SAndre Oppermann continue; 3356603724d3SBjoern A. Zeeb if (!V_tcp_do_sack) 3357f72167f4SAndre Oppermann continue; 3358fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 33596d90faf3SPaul Saab break; 33606d90faf3SPaul Saab case TCPOPT_SACK: 33615a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 33626d90faf3SPaul Saab continue; 3363b728e902SAndre Oppermann if (flags & TO_SYN) 3364b728e902SAndre Oppermann continue; 3365fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 33665a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 33675a53ca16SPaul Saab to->to_sacks = cp + 2; 336878b50714SRobert Watson TCPSTAT_INC(tcps_sack_rcv_blocks); 33696d90faf3SPaul Saab break; 3370281a0fd4SPatrick Kelsey case TCPOPT_FAST_OPEN: 3371c560df6fSPatrick Kelsey /* 3372c560df6fSPatrick Kelsey * Cookie length validation is performed by the 3373c560df6fSPatrick Kelsey * server side cookie checking code or the client 3374c560df6fSPatrick Kelsey * side cookie cache update code. 3375c560df6fSPatrick Kelsey */ 3376281a0fd4SPatrick Kelsey if (!(flags & TO_SYN)) 3377281a0fd4SPatrick Kelsey continue; 3378c560df6fSPatrick Kelsey if (!V_tcp_fastopen_client_enable && 3379c560df6fSPatrick Kelsey !V_tcp_fastopen_server_enable) 3380281a0fd4SPatrick Kelsey continue; 3381281a0fd4SPatrick Kelsey to->to_flags |= TOF_FASTOPEN; 3382281a0fd4SPatrick Kelsey to->to_tfo_len = optlen - 2; 3383281a0fd4SPatrick Kelsey to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL; 3384281a0fd4SPatrick Kelsey break; 3385be2ac88cSJonathan Lemon default: 3386be2ac88cSJonathan Lemon continue; 3387df8bae1dSRodney W. Grimes } 3388df8bae1dSRodney W. Grimes } 3389df8bae1dSRodney W. Grimes } 3390df8bae1dSRodney W. Grimes 3391df8bae1dSRodney W. Grimes /* 3392df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 3393df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 3394df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 3395df8bae1dSRodney W. Grimes * sequencing purposes. 3396df8bae1dSRodney W. Grimes */ 339755bceb1eSRandall Stewart void 3398ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 3399ad3f9ab3SAndre Oppermann int off) 3400df8bae1dSRodney W. Grimes { 3401fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 3402df8bae1dSRodney W. Grimes 3403df8bae1dSRodney W. Grimes while (cnt >= 0) { 3404df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 3405df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 3406df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 3407df8bae1dSRodney W. Grimes 34088501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 34097a3244ccSRobert Watson 3410df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 3411df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 3412df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 3413df8bae1dSRodney W. Grimes m->m_len--; 341469a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 341569a34685SYoshinobu Inoue m->m_pkthdr.len--; 3416df8bae1dSRodney W. Grimes return; 3417df8bae1dSRodney W. Grimes } 3418df8bae1dSRodney W. Grimes cnt -= m->m_len; 3419df8bae1dSRodney W. Grimes m = m->m_next; 34204dfdffe9SAndre Oppermann if (m == NULL) 3421df8bae1dSRodney W. Grimes break; 3422df8bae1dSRodney W. Grimes } 3423df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 3424df8bae1dSRodney W. Grimes } 3425df8bae1dSRodney W. Grimes 3426df8bae1dSRodney W. Grimes /* 3427df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 3428df8bae1dSRodney W. Grimes * and update averages and current timeout. 3429df8bae1dSRodney W. Grimes */ 343055bceb1eSRandall Stewart void 3431ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 3432df8bae1dSRodney W. Grimes { 3433ad3f9ab3SAndre Oppermann int delta; 3434233e8c18SGarrett Wollman 34358501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 34362be3bf22SRobert Watson 343778b50714SRobert Watson TCPSTAT_INC(tcps_rttupdated); 3438233e8c18SGarrett Wollman tp->t_rttupdated++; 34395ede40dcSRyan Stone if ((tp->t_srtt != 0) && (tp->t_rxtshift <= TCP_RTT_INVALIDATE)) { 3440233e8c18SGarrett Wollman /* 3441233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 3442233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 3443233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 3444233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3445233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 3446233e8c18SGarrett Wollman */ 3447233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3448233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3449233e8c18SGarrett Wollman 3450233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 3451233e8c18SGarrett Wollman tp->t_srtt = 1; 3452233e8c18SGarrett Wollman 3453233e8c18SGarrett Wollman /* 3454233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 3455233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 3456233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 3457233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 3458233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 3459233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 3460233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3461233e8c18SGarrett Wollman * rfc793's wired-in beta. 3462233e8c18SGarrett Wollman */ 3463233e8c18SGarrett Wollman if (delta < 0) 3464233e8c18SGarrett Wollman delta = -delta; 3465233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3466233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 3467233e8c18SGarrett Wollman tp->t_rttvar = 1; 34681fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 34691fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3470233e8c18SGarrett Wollman } else { 3471233e8c18SGarrett Wollman /* 3472233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 3473233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 3474233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 3475233e8c18SGarrett Wollman */ 3476233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 3477233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 34781fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3479233e8c18SGarrett Wollman } 34809b8b58e0SJonathan Lemon tp->t_rtttime = 0; 3481df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 3482df8bae1dSRodney W. Grimes 3483df8bae1dSRodney W. Grimes /* 3484df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 3485df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 3486df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 3487df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 3488df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 3489df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 3490df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 3491df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 3492df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 3493df8bae1dSRodney W. Grimes */ 3494233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 34959e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3496df8bae1dSRodney W. Grimes 3497df8bae1dSRodney W. Grimes /* 3498df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 3499df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 3500df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 3501df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 3502df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 3503df8bae1dSRodney W. Grimes */ 3504df8bae1dSRodney W. Grimes tp->t_softerror = 0; 3505df8bae1dSRodney W. Grimes } 3506df8bae1dSRodney W. Grimes 3507df8bae1dSRodney W. Grimes /* 3508df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 3509df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 35104faaea55SAndre Oppermann * If none, use an mss that can be handled on the outgoing interface 35114faaea55SAndre Oppermann * without forcing IP to fragment. If no route is found, route has no mtu, 3512df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 3513df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 3514df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 3515df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 3516df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 3517df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 3518df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 3519a0292f23SGarrett Wollman * 35200c39d38dSGleb Smirnoff * NOTE that resulting t_maxseg doesn't include space for TCP options or 35210c39d38dSGleb Smirnoff * IP options, e.g. IPSEC data, since length of this data may vary, and 35220c39d38dSGleb Smirnoff * thus it is calculated for every segment separately in tcp_output(). 3523a0292f23SGarrett Wollman * 352497d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 3525ef341ee1SGleb Smirnoff * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS 3526ef341ee1SGleb Smirnoff * settings are handled in tcp_mssopt(). 3527df8bae1dSRodney W. Grimes */ 3528a0292f23SGarrett Wollman void 3529ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer, 35303c914c54SAndre Oppermann struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap) 3531df8bae1dSRodney W. Grimes { 3532b287c6c7SBjoern A. Zeeb int mss = 0; 35333ac12506SJonathan T. Looney uint32_t maxmtu = 0; 3534c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 353597d8d152SAndre Oppermann struct hc_metrics_lite metrics; 3536fb59c426SYoshinobu Inoue #ifdef INET6 3537c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3538c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 3539c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3540c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 3541c068736aSJeffrey Hsu #else 3542c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 3543fb59c426SYoshinobu Inoue #endif 3544df8bae1dSRodney W. Grimes 35458501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 35467a3244ccSRobert Watson 3547ef341ee1SGleb Smirnoff if (mtuoffer != -1) { 3548ef341ee1SGleb Smirnoff KASSERT(offer == -1, ("%s: conflict", __func__)); 3549ef341ee1SGleb Smirnoff offer = mtuoffer - min_protoh; 3550ef341ee1SGleb Smirnoff } 3551ef341ee1SGleb Smirnoff 3552e8949f74SAndre Oppermann /* Initialize. */ 355397d8d152SAndre Oppermann #ifdef INET6 355497d8d152SAndre Oppermann if (isipv6) { 35553c914c54SAndre Oppermann maxmtu = tcp_maxmtu6(&inp->inp_inc, cap); 35560c39d38dSGleb Smirnoff tp->t_maxseg = V_tcp_v6mssdflt; 3557b287c6c7SBjoern A. Zeeb } 355897d8d152SAndre Oppermann #endif 3559b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3560b287c6c7SBjoern A. Zeeb else 3561b287c6c7SBjoern A. Zeeb #endif 3562b287c6c7SBjoern A. Zeeb #ifdef INET 356397d8d152SAndre Oppermann { 35643c914c54SAndre Oppermann maxmtu = tcp_maxmtu(&inp->inp_inc, cap); 35650c39d38dSGleb Smirnoff tp->t_maxseg = V_tcp_mssdflt; 3566df8bae1dSRodney W. Grimes } 3567b287c6c7SBjoern A. Zeeb #endif 3568df8bae1dSRodney W. Grimes 356997d8d152SAndre Oppermann /* 3570e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 357197d8d152SAndre Oppermann */ 35726f01cac6SBjoern A. Zeeb if (maxmtu == 0) { 35736f01cac6SBjoern A. Zeeb /* 35748e5c87f4SBjoern A. Zeeb * In case we return early we need to initialize metrics 35756f01cac6SBjoern A. Zeeb * to a defined state as tcp_hc_get() would do for us 35766f01cac6SBjoern A. Zeeb * if there was no cache hit. 35776f01cac6SBjoern A. Zeeb */ 35786f01cac6SBjoern A. Zeeb if (metricptr != NULL) 35796f01cac6SBjoern A. Zeeb bzero(metricptr, sizeof(struct hc_metrics_lite)); 358097d8d152SAndre Oppermann return; 35816f01cac6SBjoern A. Zeeb } 358297d8d152SAndre Oppermann 3583e8949f74SAndre Oppermann /* What have we got? */ 358497d8d152SAndre Oppermann switch (offer) { 358597d8d152SAndre Oppermann case 0: 358697d8d152SAndre Oppermann /* 358797d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 3588c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 35890c39d38dSGleb Smirnoff * already assigned to t_maxseg above. 359097d8d152SAndre Oppermann */ 35910c39d38dSGleb Smirnoff offer = tp->t_maxseg; 359297d8d152SAndre Oppermann break; 359397d8d152SAndre Oppermann 359497d8d152SAndre Oppermann case -1: 3595a0292f23SGarrett Wollman /* 3596c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 3597a0292f23SGarrett Wollman */ 359897d8d152SAndre Oppermann /* FALLTHROUGH */ 359997d8d152SAndre Oppermann 360097d8d152SAndre Oppermann default: 3601a0292f23SGarrett Wollman /* 360253369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 360353369ac9SAndre Oppermann * to at least minmss. 360453369ac9SAndre Oppermann */ 3605603724d3SBjoern A. Zeeb offer = max(offer, V_tcp_minmss); 360697d8d152SAndre Oppermann } 3607a0292f23SGarrett Wollman 3608df8bae1dSRodney W. Grimes /* 3609e8949f74SAndre Oppermann * rmx information is now retrieved from tcp_hostcache. 3610df8bae1dSRodney W. Grimes */ 361197d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 36123cee92e0SBjoern A. Zeeb if (metricptr != NULL) 36133cee92e0SBjoern A. Zeeb bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 361497d8d152SAndre Oppermann 3615df8bae1dSRodney W. Grimes /* 3616cc412412SHiren Panchasara * If there's a discovered mtu in tcp hostcache, use it. 3617cc412412SHiren Panchasara * Else, use the link mtu. 3618df8bae1dSRodney W. Grimes */ 361997d8d152SAndre Oppermann if (metrics.rmx_mtu) 36202d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3621c068736aSJeffrey Hsu else { 362231b3783cSHajimu UMEMOTO #ifdef INET6 3623fb59c426SYoshinobu Inoue if (isipv6) { 362497d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3625603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 362697d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 3627603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_v6mssdflt); 3628b287c6c7SBjoern A. Zeeb } 3629b3399803SHajimu UMEMOTO #endif 3630b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3631b287c6c7SBjoern A. Zeeb else 3632b287c6c7SBjoern A. Zeeb #endif 3633b287c6c7SBjoern A. Zeeb #ifdef INET 363497d8d152SAndre Oppermann { 363597d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3636603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 363797d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 3638603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_mssdflt); 3639a0292f23SGarrett Wollman } 3640b287c6c7SBjoern A. Zeeb #endif 36413cee92e0SBjoern A. Zeeb /* 36423cee92e0SBjoern A. Zeeb * XXX - The above conditional (mss = maxmtu - min_protoh) 36433cee92e0SBjoern A. Zeeb * probably violates the TCP spec. 36443cee92e0SBjoern A. Zeeb * The problem is that, since we don't know the 36453cee92e0SBjoern A. Zeeb * other end's MSS, we are supposed to use a conservative 36463cee92e0SBjoern A. Zeeb * default. But, if we do that, then MTU discovery will 36473cee92e0SBjoern A. Zeeb * never actually take place, because the conservative 36483cee92e0SBjoern A. Zeeb * default is much less than the MTUs typically seen 36493cee92e0SBjoern A. Zeeb * on the Internet today. For the moment, we'll sweep 36503cee92e0SBjoern A. Zeeb * this under the carpet. 36513cee92e0SBjoern A. Zeeb * 36523cee92e0SBjoern A. Zeeb * The conservative default might not actually be a problem 36533cee92e0SBjoern A. Zeeb * if the only case this occurs is when sending an initial 36543cee92e0SBjoern A. Zeeb * SYN with options and data to a host we've never talked 36553cee92e0SBjoern A. Zeeb * to before. Then, they will reply with an MSS value which 36563cee92e0SBjoern A. Zeeb * will get recorded and the new parameters should get 36573cee92e0SBjoern A. Zeeb * recomputed. For Further Study. 36583cee92e0SBjoern A. Zeeb */ 365997d8d152SAndre Oppermann } 3660a0292f23SGarrett Wollman mss = min(mss, offer); 366197d8d152SAndre Oppermann 3662a0292f23SGarrett Wollman /* 36630c39d38dSGleb Smirnoff * Sanity check: make sure that maxseg will be large 36643cee92e0SBjoern A. Zeeb * enough to allow some data on segments even if the 36653cee92e0SBjoern A. Zeeb * all the option space is used (40bytes). Otherwise 36663cee92e0SBjoern A. Zeeb * funny things may happen in tcp_output. 36670c39d38dSGleb Smirnoff * 36680c39d38dSGleb Smirnoff * XXXGL: shouldn't we reserve space for IP/IPv6 options? 36693cee92e0SBjoern A. Zeeb */ 36703cee92e0SBjoern A. Zeeb mss = max(mss, 64); 36713cee92e0SBjoern A. Zeeb 367297d8d152SAndre Oppermann tp->t_maxseg = mss; 36733cee92e0SBjoern A. Zeeb } 36743cee92e0SBjoern A. Zeeb 36753cee92e0SBjoern A. Zeeb void 36763cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer) 36773cee92e0SBjoern A. Zeeb { 3678dbc42409SLawrence Stewart int mss; 36793ac12506SJonathan T. Looney uint32_t bufsize; 36803cee92e0SBjoern A. Zeeb struct inpcb *inp; 36813cee92e0SBjoern A. Zeeb struct socket *so; 36823cee92e0SBjoern A. Zeeb struct hc_metrics_lite metrics; 36833c914c54SAndre Oppermann struct tcp_ifcap cap; 3684dbc42409SLawrence Stewart 36853cee92e0SBjoern A. Zeeb KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 36863cee92e0SBjoern A. Zeeb 36873c914c54SAndre Oppermann bzero(&cap, sizeof(cap)); 36883c914c54SAndre Oppermann tcp_mss_update(tp, offer, -1, &metrics, &cap); 36893cee92e0SBjoern A. Zeeb 36903cee92e0SBjoern A. Zeeb mss = tp->t_maxseg; 36913cee92e0SBjoern A. Zeeb inp = tp->t_inpcb; 369297d8d152SAndre Oppermann 3693df8bae1dSRodney W. Grimes /* 369497d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 369597d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 369697d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 369797d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 369897d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3699df8bae1dSRodney W. Grimes */ 3700c3b02504SBjoern A. Zeeb so = inp->inp_socket; 37013f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 3702e233e2acSAndre Oppermann if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe) 370397d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 370497d8d152SAndre Oppermann else 3705df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3706df8bae1dSRodney W. Grimes if (bufsize < mss) 3707df8bae1dSRodney W. Grimes mss = bufsize; 3708df8bae1dSRodney W. Grimes else { 3709df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3710df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3711df8bae1dSRodney W. Grimes bufsize = sb_max; 371288c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 37133f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3714df8bae1dSRodney W. Grimes } 37153f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 3716784ce8faSHiren Panchasara /* 3717784ce8faSHiren Panchasara * Sanity check: make sure that maxseg will be large 3718784ce8faSHiren Panchasara * enough to allow some data on segments even if the 3719784ce8faSHiren Panchasara * all the option space is used (40bytes). Otherwise 3720784ce8faSHiren Panchasara * funny things may happen in tcp_output. 3721784ce8faSHiren Panchasara * 3722784ce8faSHiren Panchasara * XXXGL: shouldn't we reserve space for IP/IPv6 options? 3723784ce8faSHiren Panchasara */ 3724784ce8faSHiren Panchasara tp->t_maxseg = max(mss, 64); 3725df8bae1dSRodney W. Grimes 37263f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3727e233e2acSAndre Oppermann if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe) 372897d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 372997d8d152SAndre Oppermann else 3730df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3731df8bae1dSRodney W. Grimes if (bufsize > mss) { 3732df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3733df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3734df8bae1dSRodney W. Grimes bufsize = sb_max; 373588c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 37363f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3737df8bae1dSRodney W. Grimes } 37383f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 373991d6cfa6SBjoern A. Zeeb 374091d6cfa6SBjoern A. Zeeb /* Check the interface for TSO capabilities. */ 37413c914c54SAndre Oppermann if (cap.ifcap & CSUM_TSO) { 374291d6cfa6SBjoern A. Zeeb tp->t_flags |= TF_TSO; 37433c914c54SAndre Oppermann tp->t_tsomax = cap.tsomax; 37449fd573c3SHans Petter Selasky tp->t_tsomaxsegcount = cap.tsomaxsegcount; 37459fd573c3SHans Petter Selasky tp->t_tsomaxsegsize = cap.tsomaxsegsize; 37463c914c54SAndre Oppermann } 3747a0292f23SGarrett Wollman } 3748a0292f23SGarrett Wollman 3749a0292f23SGarrett Wollman /* 3750a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3751a0292f23SGarrett Wollman */ 3752a0292f23SGarrett Wollman int 3753ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 3754a0292f23SGarrett Wollman { 375597d8d152SAndre Oppermann int mss = 0; 37563ac12506SJonathan T. Looney uint32_t thcmtu = 0; 37573ac12506SJonathan T. Looney uint32_t maxmtu = 0; 375897d8d152SAndre Oppermann size_t min_protoh; 3759a0292f23SGarrett Wollman 376097d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3761a0292f23SGarrett Wollman 376231b3783cSHajimu UMEMOTO #ifdef INET6 3763dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3764603724d3SBjoern A. Zeeb mss = V_tcp_v6mssdflt; 3765233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 376697d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 3767b287c6c7SBjoern A. Zeeb } 376831b3783cSHajimu UMEMOTO #endif 3769b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3770b287c6c7SBjoern A. Zeeb else 3771b287c6c7SBjoern A. Zeeb #endif 3772b287c6c7SBjoern A. Zeeb #ifdef INET 377397d8d152SAndre Oppermann { 3774603724d3SBjoern A. Zeeb mss = V_tcp_mssdflt; 3775233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 377697d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 377797d8d152SAndre Oppermann } 3778b287c6c7SBjoern A. Zeeb #endif 37793a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET) 37803a9391deSBjoern A. Zeeb thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 37813a9391deSBjoern A. Zeeb #endif 37823a9391deSBjoern A. Zeeb 378397d8d152SAndre Oppermann if (maxmtu && thcmtu) 378497d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 378597d8d152SAndre Oppermann else if (maxmtu || thcmtu) 378697d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 378797d8d152SAndre Oppermann 378897d8d152SAndre Oppermann return (mss); 3789df8bae1dSRodney W. Grimes } 379046f58482SJonathan Lemon 379146f58482SJonathan Lemon 379246f58482SJonathan Lemon /* 3793c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3794c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3795c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3796c068736aSJeffrey Hsu * be started again. 379746f58482SJonathan Lemon */ 379855bceb1eSRandall Stewart void 3799ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 380046f58482SJonathan Lemon { 380146f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 38023ac12506SJonathan T. Looney uint32_t ocwnd = tp->snd_cwnd; 38030c39d38dSGleb Smirnoff u_int maxseg = tcp_maxseg(tp); 380446f58482SJonathan Lemon 38058501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 38067a3244ccSRobert Watson 3807b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 380846f58482SJonathan Lemon tp->t_rtttime = 0; 380946f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 38106b2a5f92SJayanth Vijayaraghavan /* 3811c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3812c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 38136b2a5f92SJayanth Vijayaraghavan */ 38140c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th); 3815a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 381655bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 381746f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 381846f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 381946f58482SJonathan Lemon tp->snd_nxt = onxt; 382046f58482SJonathan Lemon /* 382146f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 382246f58482SJonathan Lemon * not updated yet. 382346f58482SJonathan Lemon */ 3824dbc42409SLawrence Stewart if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th)) 3825dbc42409SLawrence Stewart tp->snd_cwnd -= BYTES_THIS_ACK(tp, th); 3826d7587117SPaul Saab else 3827d7587117SPaul Saab tp->snd_cwnd = 0; 38280c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 382946f58482SJonathan Lemon } 383012eeb81fSHiren Panchasara 383112eeb81fSHiren Panchasara int 383212eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp) 383312eeb81fSHiren Panchasara { 383412eeb81fSHiren Panchasara return (tp->snd_max - tp->snd_una + 383512eeb81fSHiren Panchasara tp->sackhint.sack_bytes_rexmit - 383612eeb81fSHiren Panchasara tp->sackhint.sacked_bytes); 383712eeb81fSHiren Panchasara } 38387dc90a1dSMichael Tuexen 38397dc90a1dSMichael Tuexen uint32_t 38407dc90a1dSMichael Tuexen tcp_compute_initwnd(uint32_t maxseg) 38417dc90a1dSMichael Tuexen { 38427dc90a1dSMichael Tuexen /* 38437dc90a1dSMichael Tuexen * Calculate the Initial Window, also used as Restart Window 38447dc90a1dSMichael Tuexen * 38457dc90a1dSMichael Tuexen * RFC5681 Section 3.1 specifies the default conservative values. 38467dc90a1dSMichael Tuexen * RFC3390 specifies slightly more aggressive values. 38477dc90a1dSMichael Tuexen * RFC6928 increases it to ten segments. 38487dc90a1dSMichael Tuexen * Support for user specified value for initial flight size. 38497dc90a1dSMichael Tuexen */ 38507dc90a1dSMichael Tuexen if (V_tcp_initcwnd_segments) 38517dc90a1dSMichael Tuexen return min(V_tcp_initcwnd_segments * maxseg, 38527dc90a1dSMichael Tuexen max(2 * maxseg, V_tcp_initcwnd_segments * 1460)); 38537dc90a1dSMichael Tuexen else if (V_tcp_do_rfc3390) 38547dc90a1dSMichael Tuexen return min(4 * maxseg, max(2 * maxseg, 4380)); 38557dc90a1dSMichael Tuexen else { 38567dc90a1dSMichael Tuexen /* Per RFC5681 Section 3.1 */ 38577dc90a1dSMichael Tuexen if (maxseg > 2190) 38587dc90a1dSMichael Tuexen return (2 * maxseg); 38597dc90a1dSMichael Tuexen else if (maxseg > 1095) 38607dc90a1dSMichael Tuexen return (3 * maxseg); 38617dc90a1dSMichael Tuexen else 38627dc90a1dSMichael Tuexen return (4 * maxseg); 38637dc90a1dSMichael Tuexen } 38647dc90a1dSMichael Tuexen } 3865