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; 557*c4556b2fSAndrey V. Elsukov uint8_t ipttl; 558b287c6c7SBjoern A. Zeeb #endif 559b287c6c7SBjoern A. Zeeb int tlen = 0, off; 560fb59c426SYoshinobu Inoue int drop_hdrlen; 561ad3f9ab3SAndre Oppermann int thflags; 562302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 5631d7ee746SKurt Lidl uint8_t iptos; 564c1de64a4SAndrey V. Elsukov struct m_tag *fwd_tag = NULL; 5656573d758SMatt Macy struct epoch_tracker et; 566c068736aSJeffrey Hsu #ifdef INET6 567302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 568c068736aSJeffrey Hsu int isipv6; 569c068736aSJeffrey Hsu #else 570a160e630SAndre Oppermann const void *ip6 = NULL; 571b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 572302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 573a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 574252ca428SRobert Watson int ti_locked; 575610ee2f9SDavid Greenman #ifdef TCPDEBUG 576c068736aSJeffrey Hsu /* 577c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 578c068736aSJeffrey Hsu * now IPv6. 579c068736aSJeffrey Hsu */ 58014739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 581410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 582610ee2f9SDavid Greenman short ostate = 0; 583610ee2f9SDavid Greenman #endif 584c068736aSJeffrey Hsu 585fb59c426SYoshinobu Inoue #ifdef INET6 586fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 587fb59c426SYoshinobu Inoue #endif 588a0292f23SGarrett Wollman 5898f5a8818SKevin Lo off0 = *offp; 5908f5a8818SKevin Lo m = *mp; 5918f5a8818SKevin Lo *mp = NULL; 592302ce8d6SAndre Oppermann to.to_flags = 0; 59378b50714SRobert Watson TCPSTAT_INC(tcps_rcvtotal); 594fb59c426SYoshinobu Inoue 59504d3a452SHajimu UMEMOTO #ifdef INET6 596b287c6c7SBjoern A. Zeeb if (isipv6) { 5978d573cc1SAndre Oppermann /* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */ 59845747ba5SBjoern A. Zeeb 59945747ba5SBjoern A. Zeeb if (m->m_len < (sizeof(*ip6) + sizeof(*th))) { 60045747ba5SBjoern A. Zeeb m = m_pullup(m, sizeof(*ip6) + sizeof(*th)); 60145747ba5SBjoern A. Zeeb if (m == NULL) { 60245747ba5SBjoern A. Zeeb TCPSTAT_INC(tcps_rcvshort); 6038f5a8818SKevin Lo return (IPPROTO_DONE); 60445747ba5SBjoern A. Zeeb } 60545747ba5SBjoern A. Zeeb } 60645747ba5SBjoern A. Zeeb 607fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 60845747ba5SBjoern A. Zeeb th = (struct tcphdr *)((caddr_t)ip6 + off0); 609fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 610356ab07eSBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) { 61145747ba5SBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 61245747ba5SBjoern A. Zeeb th->th_sum = m->m_pkthdr.csum_data; 61345747ba5SBjoern A. Zeeb else 61445747ba5SBjoern A. Zeeb th->th_sum = in6_cksum_pseudo(ip6, tlen, 61545747ba5SBjoern A. Zeeb IPPROTO_TCP, m->m_pkthdr.csum_data); 61645747ba5SBjoern A. Zeeb th->th_sum ^= 0xffff; 61745747ba5SBjoern A. Zeeb } else 61845747ba5SBjoern A. Zeeb th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen); 61945747ba5SBjoern A. Zeeb if (th->th_sum) { 62078b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 621fb59c426SYoshinobu Inoue goto drop; 622fb59c426SYoshinobu Inoue } 62333841545SHajimu UMEMOTO 62433841545SHajimu UMEMOTO /* 62533841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 62633841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 62733841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 62833841545SHajimu UMEMOTO * 62933841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 63033841545SHajimu UMEMOTO * already dropped in ip6_input. 63133841545SHajimu UMEMOTO */ 63233841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 63333841545SHajimu UMEMOTO /* XXX stat */ 63433841545SHajimu UMEMOTO goto drop; 63533841545SHajimu UMEMOTO } 6361d7ee746SKurt Lidl iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 637b287c6c7SBjoern A. Zeeb } 63804d3a452SHajimu UMEMOTO #endif 639b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 640b287c6c7SBjoern A. Zeeb else 641b287c6c7SBjoern A. Zeeb #endif 642b287c6c7SBjoern A. Zeeb #ifdef INET 643b287c6c7SBjoern A. Zeeb { 644df8bae1dSRodney W. Grimes /* 645df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 646df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 647df8bae1dSRodney W. Grimes */ 648fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 649105bd211SGleb Smirnoff ip_stripoptions(m); 650fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 651fb59c426SYoshinobu Inoue } 652df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 6534dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 6544dfdffe9SAndre Oppermann == NULL) { 65578b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 6568f5a8818SKevin Lo return (IPPROTO_DONE); 657df8bae1dSRodney W. Grimes } 658df8bae1dSRodney W. Grimes } 659fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 660db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 6618ad458a4SGleb Smirnoff tlen = ntohs(ip->ip_len) - off0; 662df8bae1dSRodney W. Grimes 6631d7ee746SKurt Lidl iptos = ip->ip_tos; 664db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 665db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 666db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 667db4f9cc7SJonathan Lemon else 668db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 669c068736aSJeffrey Hsu ip->ip_dst.s_addr, 6708f134647SGleb Smirnoff htonl(m->m_pkthdr.csum_data + tlen + 671c068736aSJeffrey Hsu IPPROTO_TCP)); 672db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 673db4f9cc7SJonathan Lemon } else { 6748f134647SGleb Smirnoff struct ipovly *ipov = (struct ipovly *)ip; 6758f134647SGleb Smirnoff 676df8bae1dSRodney W. Grimes /* 677df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 678df8bae1dSRodney W. Grimes */ 6798f134647SGleb Smirnoff len = off0 + tlen; 680*c4556b2fSAndrey V. Elsukov ipttl = ip->ip_ttl; 681fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 6828f134647SGleb Smirnoff ipov->ih_len = htons(tlen); 683fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 68457f60867SMark Johnston /* Reset length for SDT probes. */ 6851d7ee746SKurt Lidl ip->ip_len = htons(len); 6861d7ee746SKurt Lidl /* Reset TOS bits */ 6871d7ee746SKurt Lidl ip->ip_tos = iptos; 6881d7ee746SKurt Lidl /* Re-initialization for later version check */ 689*c4556b2fSAndrey V. Elsukov ip->ip_ttl = ipttl; 6901d7ee746SKurt Lidl ip->ip_v = IPVERSION; 691b2fe54bcSAndrey V. Elsukov ip->ip_hl = off0 >> 2; 692db4f9cc7SJonathan Lemon } 69357f60867SMark Johnston 694fb59c426SYoshinobu Inoue if (th->th_sum) { 69578b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 696df8bae1dSRodney W. Grimes goto drop; 697df8bae1dSRodney W. Grimes } 698fb59c426SYoshinobu Inoue } 699b287c6c7SBjoern A. Zeeb #endif /* INET */ 700df8bae1dSRodney W. Grimes 701df8bae1dSRodney W. Grimes /* 702df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 703df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 704df8bae1dSRodney W. Grimes */ 705fb59c426SYoshinobu Inoue off = th->th_off << 2; 706df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 70778b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadoff); 708df8bae1dSRodney W. Grimes goto drop; 709df8bae1dSRodney W. Grimes } 710fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 711df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 71204d3a452SHajimu UMEMOTO #ifdef INET6 713b287c6c7SBjoern A. Zeeb if (isipv6) { 7148f5a8818SKevin Lo IP6_EXTHDR_CHECK(m, off0, off, IPPROTO_DONE); 715fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 716fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 717b287c6c7SBjoern A. Zeeb } 71804d3a452SHajimu UMEMOTO #endif 719b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 720b287c6c7SBjoern A. Zeeb else 721b287c6c7SBjoern A. Zeeb #endif 722b287c6c7SBjoern A. Zeeb #ifdef INET 723b287c6c7SBjoern A. Zeeb { 724df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 725c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 7264dfdffe9SAndre Oppermann == NULL) { 72778b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 7288f5a8818SKevin Lo return (IPPROTO_DONE); 729df8bae1dSRodney W. Grimes } 730fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 731fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 732fb59c426SYoshinobu Inoue } 733df8bae1dSRodney W. Grimes } 734b287c6c7SBjoern A. Zeeb #endif 735df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 736fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 737df8bae1dSRodney W. Grimes } 738fb59c426SYoshinobu Inoue thflags = th->th_flags; 739df8bae1dSRodney W. Grimes 740e46cd3d4SDag-Erling Smørgrav /* 741df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 742df8bae1dSRodney W. Grimes */ 7432903309aSAttilio Rao tcp_fields_to_host(th); 744df8bae1dSRodney W. Grimes 745df8bae1dSRodney W. Grimes /* 746794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 747755c1f07SAndras Olah */ 748fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 749755c1f07SAndras Olah 750755c1f07SAndras Olah /* 751fa046d87SRobert Watson * Locate pcb for segment; if we're likely to add or remove a 752a7c7f2a7SJohn Baldwin * connection then first acquire pcbinfo lock. There are three cases 753fa046d87SRobert Watson * where we might discover later we need a write lock despite the 754a7c7f2a7SJohn Baldwin * flags: ACKs moving a connection out of the syncache, ACKs for a 755a7c7f2a7SJohn Baldwin * connection in TIMEWAIT and SYNs not targeting a listening socket. 756df8bae1dSRodney W. Grimes */ 757a7c7f2a7SJohn Baldwin if ((thflags & (TH_FIN | TH_RST)) != 0) { 7586573d758SMatt Macy INP_INFO_RLOCK_ET(&V_tcbinfo, et); 759ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 760fa046d87SRobert Watson } else 761fa046d87SRobert Watson ti_locked = TI_UNLOCKED; 762252ca428SRobert Watson 7638d573cc1SAndre Oppermann /* 7648d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 7658d573cc1SAndre Oppermann */ 766b8056faeSGleb Smirnoff if ( 767b8056faeSGleb Smirnoff #ifdef INET6 768b8056faeSGleb Smirnoff (isipv6 && (m->m_flags & M_IP6_NEXTHOP)) 769b8056faeSGleb Smirnoff #ifdef INET 770b8056faeSGleb Smirnoff || (!isipv6 && (m->m_flags & M_IP_NEXTHOP)) 771b8056faeSGleb Smirnoff #endif 772b8056faeSGleb Smirnoff #endif 773b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6) 774b8056faeSGleb Smirnoff (m->m_flags & M_IP_NEXTHOP) 775b8056faeSGleb Smirnoff #endif 776b8056faeSGleb Smirnoff ) 7779b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 7789b932e9eSAndre Oppermann 779ce7ad664SEd Maste findpcb: 780ce7ad664SEd Maste #ifdef INVARIANTS 781ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 782ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 783ce7ad664SEd Maste } else { 784384a5c3cSAndrey V. Elsukov INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo); 785ce7ad664SEd Maste } 786ce7ad664SEd Maste #endif 7878a006adbSBjoern A. Zeeb #ifdef INET6 7888a006adbSBjoern A. Zeeb if (isipv6 && fwd_tag != NULL) { 7898a006adbSBjoern A. Zeeb struct sockaddr_in6 *next_hop6; 7908a006adbSBjoern A. Zeeb 7918a006adbSBjoern A. Zeeb next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1); 7928a006adbSBjoern A. Zeeb /* 7938a006adbSBjoern A. Zeeb * Transparently forwarded. Pretend to be the destination. 7948a006adbSBjoern A. Zeeb * Already got one like this? 7958a006adbSBjoern A. Zeeb */ 7968a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, 7978a006adbSBjoern A. Zeeb &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport, 7988a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m); 7998a006adbSBjoern A. Zeeb if (!inp) { 8008a006adbSBjoern A. Zeeb /* 8018a006adbSBjoern A. Zeeb * It's new. Try to find the ambushing socket. 8028a006adbSBjoern A. Zeeb * Because we've rewritten the destination address, 8038a006adbSBjoern A. Zeeb * any hardware-generated hash is ignored. 8048a006adbSBjoern A. Zeeb */ 8058a006adbSBjoern A. Zeeb inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src, 8068a006adbSBjoern A. Zeeb th->th_sport, &next_hop6->sin6_addr, 8078a006adbSBjoern A. Zeeb next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) : 8088a006adbSBjoern A. Zeeb th->th_dport, INPLOOKUP_WILDCARD | 8098a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 8108a006adbSBjoern A. Zeeb } 811c1de64a4SAndrey V. Elsukov } else if (isipv6) { 8128a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src, 8138a006adbSBjoern A. Zeeb th->th_sport, &ip6->ip6_dst, th->th_dport, 8148a006adbSBjoern A. Zeeb INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 8158a006adbSBjoern A. Zeeb m->m_pkthdr.rcvif, m); 8168a006adbSBjoern A. Zeeb } 8178a006adbSBjoern A. Zeeb #endif /* INET6 */ 8188a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET) 8198a006adbSBjoern A. Zeeb else 8208a006adbSBjoern A. Zeeb #endif 8218a006adbSBjoern A. Zeeb #ifdef INET 8228a006adbSBjoern A. Zeeb if (fwd_tag != NULL) { 8239b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 8249b932e9eSAndre Oppermann 8259b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 826f9e354dfSJulian Elischer /* 8272b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 828f9e354dfSJulian Elischer * already got one like this? 829f9e354dfSJulian Elischer */ 830d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport, 831fa046d87SRobert Watson ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB, 832d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 833fa046d87SRobert Watson if (!inp) { 834fa046d87SRobert Watson /* 835fa046d87SRobert Watson * It's new. Try to find the ambushing socket. 836d3c1f003SRobert Watson * Because we've rewritten the destination address, 837d3c1f003SRobert Watson * any hardware-generated hash is ignored. 838fa046d87SRobert Watson */ 839fa046d87SRobert Watson inp = in_pcblookup(&V_tcbinfo, ip->ip_src, 840fa046d87SRobert Watson th->th_sport, next_hop->sin_addr, 841fa046d87SRobert Watson next_hop->sin_port ? ntohs(next_hop->sin_port) : 842fa046d87SRobert Watson th->th_dport, INPLOOKUP_WILDCARD | 843fa046d87SRobert Watson INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 844f9e354dfSJulian Elischer } 845b10fbdeaSAndre Oppermann } else 846d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, 847fa046d87SRobert Watson th->th_sport, ip->ip_dst, th->th_dport, 848fa046d87SRobert Watson INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 849d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 8508a006adbSBjoern A. Zeeb #endif /* INET */ 851f9e354dfSJulian Elischer 852df8bae1dSRodney W. Grimes /* 853574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 854574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 8558b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 856df8bae1dSRodney W. Grimes */ 857816a3d83SPoul-Henning Kamp if (inp == NULL) { 858574b6964SAndre Oppermann /* 859574b6964SAndre Oppermann * Log communication attempts to ports that are not 860574b6964SAndre Oppermann * in use. 861574b6964SAndre Oppermann */ 862574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 863574b6964SAndre Oppermann tcp_log_in_vain == 2) { 864b7d747ecSAndre Oppermann if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6))) 865df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 866df541e5fSAndre Oppermann "to closed port\n", s, __func__); 8672e4e1b4cSGeoff Rehmet } 868574b6964SAndre Oppermann /* 869574b6964SAndre Oppermann * When blackholing do not respond with a RST but 870574b6964SAndre Oppermann * completely ignore the segment and drop it. 871574b6964SAndre Oppermann */ 872603724d3SBjoern A. Zeeb if ((V_blackhole == 1 && (thflags & TH_SYN)) || 873603724d3SBjoern A. Zeeb V_blackhole == 2) 8741929eae1SAndre Oppermann goto dropunlock; 875574b6964SAndre Oppermann 876a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 877a57815efSBosko Milekic goto dropwithreset; 878816a3d83SPoul-Henning Kamp } 879fa046d87SRobert Watson INP_WLOCK_ASSERT(inp); 880f5cf1e5fSJulien Charbon /* 881f5cf1e5fSJulien Charbon * While waiting for inp lock during the lookup, another thread 882f5cf1e5fSJulien Charbon * can have dropped the inpcb, in which case we need to loop back 883f5cf1e5fSJulien Charbon * and try to find a new inpcb to deliver to. 884f5cf1e5fSJulien Charbon */ 885f5cf1e5fSJulien Charbon if (inp->inp_flags & INP_DROPPED) { 886f5cf1e5fSJulien Charbon INP_WUNLOCK(inp); 887f5cf1e5fSJulien Charbon inp = NULL; 888f5cf1e5fSJulien Charbon goto findpcb; 889f5cf1e5fSJulien Charbon } 890c2529042SHans Petter Selasky if ((inp->inp_flowtype == M_HASHTYPE_NONE) && 891c2529042SHans Petter Selasky (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) && 892c2529042SHans Petter Selasky ((inp->inp_socket == NULL) || 893c2529042SHans Petter Selasky (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0)) { 89480cb9f21SKip Macy inp->inp_flowid = m->m_pkthdr.flowid; 8952f719932SAdrian Chadd inp->inp_flowtype = M_HASHTYPE_GET(m); 89680cb9f21SKip Macy } 897fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 898a2589465SKen Smith #ifdef INET6 899fcf59617SAndrey V. Elsukov if (isipv6 && IPSEC_ENABLED(ipv6) && 900fcf59617SAndrey V. Elsukov IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) { 901a2589465SKen Smith goto dropunlock; 902a2589465SKen Smith } 903fcf59617SAndrey V. Elsukov #ifdef INET 904fcf59617SAndrey V. Elsukov else 905fcf59617SAndrey V. Elsukov #endif 906fcf59617SAndrey V. Elsukov #endif /* INET6 */ 907fcf59617SAndrey V. Elsukov #ifdef INET 908fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4) && 909fcf59617SAndrey V. Elsukov IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) { 910fcf59617SAndrey V. Elsukov goto dropunlock; 911fcf59617SAndrey V. Elsukov } 912fcf59617SAndrey V. Elsukov #endif /* INET */ 913a2589465SKen Smith #endif /* IPSEC */ 914a2589465SKen Smith 9158d573cc1SAndre Oppermann /* 9168d573cc1SAndre Oppermann * Check the minimum TTL for socket. 9178d573cc1SAndre Oppermann */ 91834f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 91934f83c52SGeorge V. Neville-Neil #ifdef INET6 9202d8868dbSJonathan T. Looney if (isipv6) { 9212d8868dbSJonathan T. Looney if (inp->inp_ip_minttl > ip6->ip6_hlim) 922302ce8d6SAndre Oppermann goto dropunlock; 9232d8868dbSJonathan T. Looney } else 92434f83c52SGeorge V. Neville-Neil #endif 92534f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 926302ce8d6SAndre Oppermann goto dropunlock; 92734f83c52SGeorge V. Neville-Neil } 928936cd18dSAndre Oppermann 929340c35deSJonathan Lemon /* 930252ca428SRobert Watson * A previous connection in TIMEWAIT state is supposed to catch stray 931252ca428SRobert Watson * or duplicate segments arriving late. If this segment was a 9320989f56cSRobert Watson * legitimate new connection attempt, the old INPCB gets removed and 933252ca428SRobert Watson * we can try again to find a listening socket. 934252ca428SRobert Watson * 935fa046d87SRobert Watson * At this point, due to earlier optimism, we may hold only an inpcb 936fa046d87SRobert Watson * lock, and not the inpcbinfo write lock. If so, we need to try to 937fa046d87SRobert Watson * acquire it, or if that fails, acquire a reference on the inpcb, 938fa046d87SRobert Watson * drop all locks, acquire a global write lock, and then re-acquire 939fa046d87SRobert Watson * the inpcb lock. We may at that point discover that another thread 940fa046d87SRobert Watson * has tried to free the inpcb, in which case we need to loop back 941fa046d87SRobert Watson * and try to find a new inpcb to deliver to. 942fa046d87SRobert Watson * 943fa046d87SRobert Watson * XXXRW: It may be time to rethink timewait locking. 944340c35deSJonathan Lemon */ 945ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 946fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 9476573d758SMatt Macy INP_INFO_RLOCK_ET(&V_tcbinfo, et); 948ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 949252ca428SRobert Watson } 950ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 951252ca428SRobert Watson 952340c35deSJonathan Lemon if (thflags & TH_SYN) 953f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 9548d573cc1SAndre Oppermann /* 9558d573cc1SAndre Oppermann * NB: tcp_twcheck unlocks the INP and frees the mbuf. 9568d573cc1SAndre Oppermann */ 9572104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 958340c35deSJonathan Lemon goto findpcb; 9596573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 9608f5a8818SKevin Lo return (IPPROTO_DONE); 961340c35deSJonathan Lemon } 962794235b7SAndre Oppermann /* 963794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 964794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 965794235b7SAndre Oppermann * segment and send an appropriate response. 966794235b7SAndre Oppermann */ 967df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 968a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 969a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 970a57815efSBosko Milekic goto dropwithreset; 97109f81a46SBosko Milekic } 972df8bae1dSRodney W. Grimes 97309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 97409fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 97509fe6320SNavdeep Parhar tcp_offload_input(tp, m); 97609fe6320SNavdeep Parhar m = NULL; /* consumed by the TOE driver */ 97709fe6320SNavdeep Parhar goto dropunlock; 97809fe6320SNavdeep Parhar } 97909fe6320SNavdeep Parhar #endif 98009fe6320SNavdeep Parhar 981252ca428SRobert Watson /* 982252ca428SRobert Watson * We've identified a valid inpcb, but it could be that we need an 983fa046d87SRobert Watson * inpcbinfo write lock but don't hold it. In this case, attempt to 984fa046d87SRobert Watson * acquire using the same strategy as the TIMEWAIT case above. If we 985fa046d87SRobert Watson * relock, we have to jump back to 'relocked' as the connection might 986fa046d87SRobert Watson * now be in TIMEWAIT. 987252ca428SRobert Watson */ 988fa046d87SRobert Watson #ifdef INVARIANTS 989a7c7f2a7SJohn Baldwin if ((thflags & (TH_FIN | TH_RST)) != 0) 990ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 991fa046d87SRobert Watson #endif 992a7c7f2a7SJohn Baldwin if (!((tp->t_state == TCPS_ESTABLISHED && (thflags & TH_SYN) == 0) || 993281a0fd4SPatrick Kelsey (tp->t_state == TCPS_LISTEN && (thflags & TH_SYN) && 99468bd7ed1SJonathan T. Looney !IS_FASTOPEN(tp->t_flags)))) { 995fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 9966573d758SMatt Macy INP_INFO_RLOCK_ET(&V_tcbinfo, et); 997ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 998252ca428SRobert Watson } 999ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1000252ca428SRobert Watson } 1001252ca428SRobert Watson 1002c488362eSRobert Watson #ifdef MAC 10038501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 100430d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 1005302ce8d6SAndre Oppermann goto dropunlock; 1006c488362eSRobert Watson #endif 1007a557af22SRobert Watson so = inp->inp_socket; 1008302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 1009610ee2f9SDavid Greenman #ifdef TCPDEBUG 1010df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 1011df8bae1dSRodney W. Grimes ostate = tp->t_state; 101210fe523eSMaxim Konovalov #ifdef INET6 10136f697424SBjoern A. Zeeb if (isipv6) { 1014540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 1015d30d90dcSMaxim Konovalov } else 10166f697424SBjoern A. Zeeb #endif 1017540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 1018fb59c426SYoshinobu Inoue tcp_savetcp = *th; 1019df8bae1dSRodney W. Grimes } 1020b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */ 1021db33b3e6SAndre Oppermann /* 1022db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 1023db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 1024a7c7f2a7SJohn Baldwin * attempt or the completion of a previous one. 1025db33b3e6SAndre Oppermann */ 102645274760SJonathan T. Looney KASSERT(tp->t_state == TCPS_LISTEN || !(so->so_options & SO_ACCEPTCONN), 102745274760SJonathan T. Looney ("%s: so accepting but tp %p not listening", __func__, tp)); 102845274760SJonathan T. Looney if (tp->t_state == TCPS_LISTEN && (so->so_options & SO_ACCEPTCONN)) { 1029540e8b7eSJeffrey Hsu struct in_conninfo inc; 1030540e8b7eSJeffrey Hsu 10318bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 1032302ce8d6SAndre Oppermann #ifdef INET6 1033be2ac88cSJonathan Lemon if (isipv6) { 1034dcdb4371SBjoern A. Zeeb inc.inc_flags |= INC_ISIPV6; 10355dff1c38SMichael Tuexen if (inp->inp_inc.inc_flags & INC_IPV6MINMTU) 10365dff1c38SMichael Tuexen inc.inc_flags |= INC_IPV6MINMTU; 1037be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 1038be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 1039302ce8d6SAndre Oppermann } else 1040302ce8d6SAndre Oppermann #endif 1041302ce8d6SAndre Oppermann { 1042be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 1043be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 1044be2ac88cSJonathan Lemon } 1045be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 1046be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 10477973fba3SJulian Elischer inc.inc_fibnum = so->so_fibnum; 1048be2ac88cSJonathan Lemon 1049fb59c426SYoshinobu Inoue /* 10508d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 10518d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 10528d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 1053fb59c426SYoshinobu Inoue */ 1054be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 1055a7c7f2a7SJohn Baldwin 1056ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1057bf6d304aSAndre Oppermann /* 1058bf6d304aSAndre Oppermann * Parse the TCP options here because 1059bf6d304aSAndre Oppermann * syncookies need access to the reflected 1060bf6d304aSAndre Oppermann * timestamp. 1061bf6d304aSAndre Oppermann */ 1062bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 10639fa198beSAndre Oppermann /* 10649fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 10659fa198beSAndre Oppermann * inp and tcpinfo locks. 10669fa198beSAndre Oppermann */ 1067fcf59617SAndrey V. Elsukov rstreason = syncache_expand(&inc, &to, th, &so, m); 1068fcf59617SAndrey V. Elsukov if (rstreason < 0) { 1069fcf59617SAndrey V. Elsukov /* 1070fcf59617SAndrey V. Elsukov * A failing TCP MD5 signature comparison 1071fcf59617SAndrey V. Elsukov * must result in the segment being dropped 1072fcf59617SAndrey V. Elsukov * and must not produce any response back 1073fcf59617SAndrey V. Elsukov * to the sender. 1074fcf59617SAndrey V. Elsukov */ 1075fcf59617SAndrey V. Elsukov goto dropunlock; 1076fcf59617SAndrey V. Elsukov } else if (rstreason == 0) { 10774195b4afSPaul Traina /* 1078e207f800SAndre Oppermann * No syncache entry or ACK was not 1079be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 10808d573cc1SAndre Oppermann * NB: syncache did its own logging 10818d573cc1SAndre Oppermann * of the failure cause. 10824195b4afSPaul Traina */ 1083be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 1084be2ac88cSJonathan Lemon goto dropwithreset; 1085be2ac88cSJonathan Lemon } 108609c305ebSPatrick Kelsey tfo_socket_result: 1087f76fcf6dSJeffrey Hsu if (so == NULL) { 1088be2ac88cSJonathan Lemon /* 1089e207f800SAndre Oppermann * We completed the 3-way handshake 1090e207f800SAndre Oppermann * but could not allocate a socket 1091e207f800SAndre Oppermann * either due to memory shortage, 1092e207f800SAndre Oppermann * listen queue length limits or 1093a160e630SAndre Oppermann * global socket limits. Send RST 1094a160e630SAndre Oppermann * or wait and have the remote end 1095a160e630SAndre Oppermann * retransmit the ACK for another 1096a160e630SAndre Oppermann * try. 1097be2ac88cSJonathan Lemon */ 1098a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1099a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11008d573cc1SAndre Oppermann "Socket allocation failed due to " 11018d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 1102ac957cd2SJulian Elischer s, __func__, 1103ac957cd2SJulian Elischer V_tcp_sc_rst_sock_fail ? 1104ac957cd2SJulian Elischer "sending RST" : "try again"); 1105603724d3SBjoern A. Zeeb if (V_tcp_sc_rst_sock_fail) { 1106e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 1107e207f800SAndre Oppermann goto dropwithreset; 1108a160e630SAndre Oppermann } else 1109a160e630SAndre Oppermann goto dropunlock; 1110f76fcf6dSJeffrey Hsu } 1111be2ac88cSJonathan Lemon /* 1112be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 11138d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 11148d573cc1SAndre Oppermann * created socket and update the tp variable. 1115be2ac88cSJonathan Lemon */ 11168501a69cSRobert Watson INP_WUNLOCK(inp); /* listen socket */ 1117be2ac88cSJonathan Lemon inp = sotoinpcb(so); 1118ff9b006dSJulien Charbon /* 1119ff9b006dSJulien Charbon * New connection inpcb is already locked by 1120ff9b006dSJulien Charbon * syncache_expand(). 1121ff9b006dSJulien Charbon */ 1122ff9b006dSJulien Charbon INP_WLOCK_ASSERT(inp); 1123be2ac88cSJonathan Lemon tp = intotcpcb(inp); 11248d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 11258d573cc1SAndre Oppermann ("%s: ", __func__)); 1126be2ac88cSJonathan Lemon /* 1127302ce8d6SAndre Oppermann * Process the segment and the data it 1128302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 1129302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 1130302ce8d6SAndre Oppermann */ 11316138da62SMichael Tuexen TCP_PROBE5(receive, NULL, tp, m, tp, th); 113255bceb1eSRandall Stewart tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 11336573d758SMatt Macy iptos); 11346573d758SMatt Macy if (ti_locked == TI_RLOCKED) 11356573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 11368f5a8818SKevin Lo return (IPPROTO_DONE); 1137be2ac88cSJonathan Lemon } 1138a160e630SAndre Oppermann /* 11398d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 11408d573cc1SAndre Oppermann * 1141a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 1142a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 1143a160e630SAndre Oppermann * retransmits. 114419bc77c5SAndre Oppermann * 114519bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 114619bc77c5SAndre Oppermann * causes. 1147a160e630SAndre Oppermann */ 1148a160e630SAndre Oppermann if (thflags & TH_RST) { 114993899d10SMichael Tuexen syncache_chkrst(&inc, th, m); 1150a160e630SAndre Oppermann goto dropunlock; 1151a160e630SAndre Oppermann } 1152a160e630SAndre Oppermann /* 1153a160e630SAndre Oppermann * We can't do anything without SYN. 1154a160e630SAndre Oppermann */ 1155a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 1156a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1157a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1158773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 11598d573cc1SAndre Oppermann s, __func__); 116078b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1161a160e630SAndre Oppermann goto dropunlock; 1162a160e630SAndre Oppermann } 1163a160e630SAndre Oppermann /* 1164a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 1165a160e630SAndre Oppermann */ 1166fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1167a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1168a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11698d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 11708d573cc1SAndre Oppermann s, __func__); 1171a160e630SAndre Oppermann syncache_badack(&inc); /* XXX: Not needed! */ 117278b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1173a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1174a57815efSBosko Milekic goto dropwithreset; 11754195b4afSPaul Traina } 1176a160e630SAndre Oppermann /* 1177a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 1178a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 1179a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 1180a160e630SAndre Oppermann * TCP/IP stack. 1181a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 1182a160e630SAndre Oppermann * and is constantly refining its stack detection 1183a160e630SAndre Oppermann * strategies. 11848d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 1185a160e630SAndre Oppermann * and was used by RFC1644. 1186a160e630SAndre Oppermann */ 1187603724d3SBjoern A. Zeeb if ((thflags & TH_FIN) && V_drop_synfin) { 1188a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1189a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1190773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 11918d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 119278b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1193302ce8d6SAndre Oppermann goto dropunlock; 1194ebb0cbeaSPaul Traina } 1195be2ac88cSJonathan Lemon /* 1196be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 1197a160e630SAndre Oppermann * 1198a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 1199a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 1200a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 1201be2ac88cSJonathan Lemon */ 1202a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 1203a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 1204a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 1205a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 120633841545SHajimu UMEMOTO #ifdef INET6 120733841545SHajimu UMEMOTO /* 120833841545SHajimu UMEMOTO * If deprecated address is forbidden, 120933841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 121033841545SHajimu UMEMOTO * address to prevent any new inbound connection from 121133841545SHajimu UMEMOTO * getting established. 121233841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 121333841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 121433841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 121533841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 121633841545SHajimu UMEMOTO * for the exchange. 121733841545SHajimu UMEMOTO * 121833841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 121933841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 122033841545SHajimu UMEMOTO * SYN in this case. 122133841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 122233841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 122333841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 122433841545SHajimu UMEMOTO * used" 122533841545SHajimu UMEMOTO * 2. use of it with new communication: 122633841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 122733841545SHajimu UMEMOTO * with sufficient scope is available" 122833841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 122933841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 123033841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 123133841545SHajimu UMEMOTO * 123233841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 123333841545SHajimu UMEMOTO * multiple description text for deprecated address 123433841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 123533841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 123633841545SHajimu UMEMOTO */ 1237603724d3SBjoern A. Zeeb if (isipv6 && !V_ip6_use_deprecated) { 123833841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 123933841545SHajimu UMEMOTO 12403e88eb90SAndrey V. Elsukov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */); 12418c0fec80SRobert Watson if (ia6 != NULL && 124233841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 12438c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 1244a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1245a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12468d573cc1SAndre Oppermann "Connection attempt to deprecated " 12478d573cc1SAndre Oppermann "IPv6 address rejected\n", 1248a160e630SAndre Oppermann s, __func__); 124933841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 125033841545SHajimu UMEMOTO goto dropwithreset; 125133841545SHajimu UMEMOTO } 125277d396fdSMaksim Yevmenkin if (ia6) 12538c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 125433841545SHajimu UMEMOTO } 1255b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 125665f28919SJesper Skriver /* 1257db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 12588d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 1259db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 12608d573cc1SAndre Oppermann * If it is from this socket it must be forged. 12618d573cc1SAndre Oppermann * Don't respond if the source or destination is a 12628d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 126393ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 126493ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 126593ec91baSCrist J. Clark * in_broadcast() to find them. 1266be2ac88cSJonathan Lemon */ 12678d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 12688d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 12698d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12708d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 1271773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 1272302ce8d6SAndre Oppermann goto dropunlock; 12738d573cc1SAndre Oppermann } 1274db33b3e6SAndre Oppermann #ifdef INET6 1275b287c6c7SBjoern A. Zeeb if (isipv6) { 1276db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1277a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 1278a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1279a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12808d573cc1SAndre Oppermann "Connection attempt to/from self " 1281773673c1SAndre Oppermann "ignored\n", s, __func__); 1282302ce8d6SAndre Oppermann goto dropunlock; 1283a160e630SAndre Oppermann } 1284be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1285a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 1286a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1287a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12888d573cc1SAndre Oppermann "Connection attempt from/to multicast " 1289773673c1SAndre Oppermann "address ignored\n", s, __func__); 1290302ce8d6SAndre Oppermann goto dropunlock; 1291a160e630SAndre Oppermann } 1292b287c6c7SBjoern A. Zeeb } 1293db33b3e6SAndre Oppermann #endif 1294b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 1295b287c6c7SBjoern A. Zeeb else 1296b287c6c7SBjoern A. Zeeb #endif 1297b287c6c7SBjoern A. Zeeb #ifdef INET 1298b287c6c7SBjoern A. Zeeb { 1299db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1300a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 1301a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1302a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13038d573cc1SAndre Oppermann "Connection attempt from/to self " 1304773673c1SAndre Oppermann "ignored\n", s, __func__); 1305302ce8d6SAndre Oppermann goto dropunlock; 1306a160e630SAndre Oppermann } 1307be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1308be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 13092ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 1310a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 1311a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1312a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13138d573cc1SAndre Oppermann "Connection attempt from/to broad- " 1314773673c1SAndre Oppermann "or multicast address ignored\n", 1315a160e630SAndre Oppermann s, __func__); 1316302ce8d6SAndre Oppermann goto dropunlock; 1317c068736aSJeffrey Hsu } 1318a160e630SAndre Oppermann } 1319b287c6c7SBjoern A. Zeeb #endif 1320be2ac88cSJonathan Lemon /* 1321db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 1322db33b3e6SAndre Oppermann * for syncache. 1323be2ac88cSJonathan Lemon */ 13243c653157SHartmut Brandt #ifdef TCPDEBUG 13253c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 13263c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 13273c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 13283c653157SHartmut Brandt #endif 13292b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 1330f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 1331281a0fd4SPatrick Kelsey if (syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL)) 133209c305ebSPatrick Kelsey goto tfo_socket_result; 133318a75309SPatrick Kelsey 1334be2ac88cSJonathan Lemon /* 13354d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 1336a7c7f2a7SJohn Baldwin * Only the listen socket is unlocked by syncache_add(). 1337be2ac88cSJonathan Lemon */ 1338ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 13396573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 1340a7c7f2a7SJohn Baldwin ti_locked = TI_UNLOCKED; 1341a7c7f2a7SJohn Baldwin } 1342384a5c3cSAndrey V. Elsukov INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo); 13438f5a8818SKevin Lo return (IPPROTO_DONE); 1344982c1675SAndre Oppermann } else if (tp->t_state == TCPS_LISTEN) { 1345982c1675SAndre Oppermann /* 1346982c1675SAndre Oppermann * When a listen socket is torn down the SO_ACCEPTCONN 1347982c1675SAndre Oppermann * flag is removed first while connections are drained 1348982c1675SAndre Oppermann * from the accept queue in a unlock/lock cycle of the 1349982c1675SAndre Oppermann * ACCEPT_LOCK, opening a race condition allowing a SYN 1350982c1675SAndre Oppermann * attempt go through unhandled. 1351982c1675SAndre Oppermann */ 1352982c1675SAndre Oppermann goto dropunlock; 1353f76fcf6dSJeffrey Hsu } 1354fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1355fcf59617SAndrey V. Elsukov if (tp->t_flags & TF_SIGNATURE) { 1356fcf59617SAndrey V. Elsukov tcp_dooptions(&to, optp, optlen, thflags); 1357fcf59617SAndrey V. Elsukov if ((to.to_flags & TOF_SIGNATURE) == 0) { 1358fcf59617SAndrey V. Elsukov TCPSTAT_INC(tcps_sig_err_nosigopt); 13592903309aSAttilio Rao goto dropunlock; 13602903309aSAttilio Rao } 1361fcf59617SAndrey V. Elsukov if (!TCPMD5_ENABLED() || 1362fcf59617SAndrey V. Elsukov TCPMD5_INPUT(m, th, to.to_signature) != 0) 1363fcf59617SAndrey V. Elsukov goto dropunlock; 13642903309aSAttilio Rao } 13652903309aSAttilio Rao #endif 13662b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 136757f60867SMark Johnston 1368302ce8d6SAndre Oppermann /* 13698d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 13708d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 13718d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 1372302ce8d6SAndre Oppermann */ 13736573d758SMatt Macy tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos); 13746573d758SMatt Macy if (ti_locked == TI_RLOCKED) 13756573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 13768f5a8818SKevin Lo return (IPPROTO_DONE); 1377be2ac88cSJonathan Lemon 1378302ce8d6SAndre Oppermann dropwithreset: 13792b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 138057f60867SMark Johnston 1381ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 13826573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 1383252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1384fa046d87SRobert Watson } 1385fa046d87SRobert Watson #ifdef INVARIANTS 1386fa046d87SRobert Watson else { 1387fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset " 1388fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1389384a5c3cSAndrey V. Elsukov INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo); 1390fa046d87SRobert Watson } 1391fa046d87SRobert Watson #endif 1392014ea782SRobert Watson 1393014ea782SRobert Watson if (inp != NULL) { 1394302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 1395014ea782SRobert Watson INP_WUNLOCK(inp); 1396dd8ac7f9SRobert Watson } else 1397014ea782SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1398302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 1399014ea782SRobert Watson goto drop; 1400014ea782SRobert Watson 1401302ce8d6SAndre Oppermann dropunlock: 140257f60867SMark Johnston if (m != NULL) 14032b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 140457f60867SMark Johnston 1405ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 14066573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 1407252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1408fa046d87SRobert Watson } 1409fa046d87SRobert Watson #ifdef INVARIANTS 1410fa046d87SRobert Watson else { 1411fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock " 1412fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1413384a5c3cSAndrey V. Elsukov INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo); 1414fa046d87SRobert Watson } 1415fa046d87SRobert Watson #endif 1416252ca428SRobert Watson 14179fa198beSAndre Oppermann if (inp != NULL) 14188501a69cSRobert Watson INP_WUNLOCK(inp); 1419014ea782SRobert Watson 1420302ce8d6SAndre Oppermann drop: 1421384a5c3cSAndrey V. Elsukov INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo); 1422a160e630SAndre Oppermann if (s != NULL) 1423a160e630SAndre Oppermann free(s, M_TCPLOG); 1424302ce8d6SAndre Oppermann if (m != NULL) 1425302ce8d6SAndre Oppermann m_freem(m); 14268f5a8818SKevin Lo return (IPPROTO_DONE); 1427302ce8d6SAndre Oppermann } 1428302ce8d6SAndre Oppermann 1429e44c1887SSteven Hartland /* 1430e44c1887SSteven Hartland * Automatic sizing of receive socket buffer. Often the send 1431e44c1887SSteven Hartland * buffer size is not optimally adjusted to the actual network 1432e44c1887SSteven Hartland * conditions at hand (delay bandwidth product). Setting the 1433e44c1887SSteven Hartland * buffer size too small limits throughput on links with high 1434e44c1887SSteven Hartland * bandwidth and high delay (eg. trans-continental/oceanic links). 1435e44c1887SSteven Hartland * 1436e44c1887SSteven Hartland * On the receive side the socket buffer memory is only rarely 1437e44c1887SSteven Hartland * used to any significant extent. This allows us to be much 1438e44c1887SSteven Hartland * more aggressive in scaling the receive socket buffer. For 1439e44c1887SSteven Hartland * the case that the buffer space is actually used to a large 1440e44c1887SSteven Hartland * extent and we run out of kernel memory we can simply drop 1441e44c1887SSteven Hartland * the new segments; TCP on the sender will just retransmit it 1442e44c1887SSteven Hartland * later. Setting the buffer size too big may only consume too 1443e44c1887SSteven Hartland * much kernel memory if the application doesn't read() from 1444e44c1887SSteven Hartland * the socket or packet loss or reordering makes use of the 1445e44c1887SSteven Hartland * reassembly queue. 1446e44c1887SSteven Hartland * 1447e44c1887SSteven Hartland * The criteria to step up the receive buffer one notch are: 1448e44c1887SSteven Hartland * 1. Application has not set receive buffer size with 1449e44c1887SSteven Hartland * SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE. 1450560c0586SMichael Tuexen * 2. the number of bytes received during 1/2 of an sRTT 1451560c0586SMichael Tuexen * is at least 3/8 of the current socket buffer size. 1452560c0586SMichael Tuexen * 3. receive buffer size has not hit maximal automatic size; 1453e44c1887SSteven Hartland * 1454560c0586SMichael Tuexen * If all of the criteria are met we increaset the socket buffer 1455560c0586SMichael Tuexen * by a 1/2 (bounded by the max). This allows us to keep ahead 1456560c0586SMichael Tuexen * of slow-start but also makes it so our peer never gets limited 1457560c0586SMichael Tuexen * by our rwnd which we then open up causing a burst. 1458560c0586SMichael Tuexen * 1459560c0586SMichael Tuexen * This algorithm does two steps per RTT at most and only if 1460e44c1887SSteven Hartland * we receive a bulk stream w/o packet losses or reorderings. 1461e44c1887SSteven Hartland * Shrinking the buffer during idle times is not necessary as 1462e44c1887SSteven Hartland * it doesn't consume any memory when idle. 1463e44c1887SSteven Hartland * 1464e44c1887SSteven Hartland * TODO: Only step up if the application is actually serving 1465e44c1887SSteven Hartland * the buffer to better manage the socket buffer resources. 1466e44c1887SSteven Hartland */ 1467e44c1887SSteven Hartland int 1468e44c1887SSteven Hartland tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so, 1469e44c1887SSteven Hartland struct tcpcb *tp, int tlen) 1470e44c1887SSteven Hartland { 1471e44c1887SSteven Hartland int newsize = 0; 1472e44c1887SSteven Hartland 1473e44c1887SSteven Hartland if (V_tcp_do_autorcvbuf && (so->so_rcv.sb_flags & SB_AUTOSIZE) && 1474e44c1887SSteven Hartland tp->t_srtt != 0 && tp->rfbuf_ts != 0 && 1475e44c1887SSteven Hartland TCP_TS_TO_TICKS(tcp_ts_getticks() - tp->rfbuf_ts) > 1476560c0586SMichael Tuexen ((tp->t_srtt >> TCP_RTT_SHIFT)/2)) { 1477560c0586SMichael Tuexen if (tp->rfbuf_cnt > ((so->so_rcv.sb_hiwat / 2)/ 4 * 3) && 1478e44c1887SSteven Hartland so->so_rcv.sb_hiwat < V_tcp_autorcvbuf_max) { 1479560c0586SMichael Tuexen newsize = min((so->so_rcv.sb_hiwat + (so->so_rcv.sb_hiwat/2)), V_tcp_autorcvbuf_max); 1480e44c1887SSteven Hartland } 1481e44c1887SSteven Hartland TCP_PROBE6(receive__autoresize, NULL, tp, m, tp, th, newsize); 1482e44c1887SSteven Hartland 1483e44c1887SSteven Hartland /* Start over with next RTT. */ 1484e44c1887SSteven Hartland tp->rfbuf_ts = 0; 1485e44c1887SSteven Hartland tp->rfbuf_cnt = 0; 1486e44c1887SSteven Hartland } else { 1487e44c1887SSteven Hartland tp->rfbuf_cnt += tlen; /* add up */ 1488e44c1887SSteven Hartland } 1489e44c1887SSteven Hartland 1490e44c1887SSteven Hartland return (newsize); 1491e44c1887SSteven Hartland } 1492e44c1887SSteven Hartland 149355bceb1eSRandall Stewart void 1494302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 14956573d758SMatt Macy struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos) 1496302ce8d6SAndre Oppermann { 1497021eaf79SHiren Panchasara int thflags, acked, ourfinisacked, needoutput = 0, sack_changed; 1498302ce8d6SAndre Oppermann int rstreason, todrop, win; 14993ac12506SJonathan T. Looney uint32_t tiwin; 15004b7b743cSLawrence Stewart uint16_t nsegs; 150107dacf03SAndre Oppermann char *s; 150207dacf03SAndre Oppermann struct in_conninfo *inc; 1503c11a15bfSGleb Smirnoff struct mbuf *mfree; 1504302ce8d6SAndre Oppermann struct tcpopt to; 1505281a0fd4SPatrick Kelsey int tfo_syn; 1506302ce8d6SAndre Oppermann 150714739780SMaxim Konovalov #ifdef TCPDEBUG 150814739780SMaxim Konovalov /* 150914739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 151014739780SMaxim Konovalov * now IPv6. 151114739780SMaxim Konovalov */ 151214739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 151314739780SMaxim Konovalov struct tcphdr tcp_savetcp; 151414739780SMaxim Konovalov short ostate = 0; 151514739780SMaxim Konovalov #endif 1516302ce8d6SAndre Oppermann thflags = th->th_flags; 151707dacf03SAndre Oppermann inc = &tp->t_inpcb->inp_inc; 1518d64a46eaSLawrence Stewart tp->sackhint.last_sack_ack = 0; 1519021eaf79SHiren Panchasara sack_changed = 0; 15204b7b743cSLawrence Stewart nsegs = max(1, m->m_pkthdr.lro_nsegs); 1521252ca428SRobert Watson /* 1522252ca428SRobert Watson * If this is either a state-changing packet or current state isn't 1523252ca428SRobert Watson * established, we require a write lock on tcbinfo. Otherwise, we 15240989f56cSRobert Watson * allow the tcbinfo to be in either alocked or unlocked, as the 15250989f56cSRobert Watson * caller may have unnecessarily acquired a write lock due to a race. 1526252ca428SRobert Watson */ 1527252ca428SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 1528252ca428SRobert Watson tp->t_state != TCPS_ESTABLISHED) { 1529ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1530252ca428SRobert Watson } 15318501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1532679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1533679d9708SAndre Oppermann __func__)); 1534679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1535679d9708SAndre Oppermann __func__)); 1536df8bae1dSRodney W. Grimes 153786a996e6SHiren Panchasara #ifdef TCPPCAP 153886a996e6SHiren Panchasara /* Save segment, if requested. */ 153986a996e6SHiren Panchasara tcp_pcap_add(th, m, &(tp->t_inpkts)); 154086a996e6SHiren Panchasara #endif 15412529f56eSJonathan T. Looney TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0, 15422529f56eSJonathan T. Looney tlen, NULL, true); 154386a996e6SHiren Panchasara 1544013f4df6SMichael Tuexen if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) { 1545013f4df6SMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 1546013f4df6SMichael Tuexen log(LOG_DEBUG, "%s; %s: " 1547013f4df6SMichael Tuexen "SYN|FIN segment ignored (based on " 1548013f4df6SMichael Tuexen "sysctl setting)\n", s, __func__); 1549013f4df6SMichael Tuexen free(s, M_TCPLOG); 1550013f4df6SMichael Tuexen } 1551013f4df6SMichael Tuexen goto drop; 1552013f4df6SMichael Tuexen } 1553013f4df6SMichael Tuexen 1554df8bae1dSRodney W. Grimes /* 1555ebfd7534SMichael Tuexen * If a segment with the ACK-bit set arrives in the SYN-SENT state 1556ebfd7534SMichael Tuexen * check SEQ.ACK first. 1557ebfd7534SMichael Tuexen */ 1558ebfd7534SMichael Tuexen if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) && 1559ebfd7534SMichael Tuexen (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) { 1560ebfd7534SMichael Tuexen rstreason = BANDLIM_UNLIMITED; 1561ebfd7534SMichael Tuexen goto dropwithreset; 1562ebfd7534SMichael Tuexen } 1563ebfd7534SMichael Tuexen 1564ebfd7534SMichael Tuexen /* 1565df8bae1dSRodney W. Grimes * Segment received on connection. 1566df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1567e8949f74SAndre Oppermann * XXX: This should be done after segment 1568e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 1569df8bae1dSRodney W. Grimes */ 15709b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 1571df8bae1dSRodney W. Grimes 1572df8bae1dSRodney W. Grimes /* 1573c52102ddSHiren Panchasara * Scale up the window into a 32-bit value. 1574e8949f74SAndre Oppermann * For the SYN_SENT state the scale is zero. 1575464fcfbcSAndre Oppermann */ 1576464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 1577464fcfbcSAndre Oppermann 1578464fcfbcSAndre Oppermann /* 1579f2512ba1SRui Paulo * TCP ECN processing. 1580f2512ba1SRui Paulo */ 1581f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) { 15829c251892SRui Paulo if (thflags & TH_CWR) 15839c251892SRui Paulo tp->t_flags &= ~TF_ECN_SND_ECE; 1584f2512ba1SRui Paulo switch (iptos & IPTOS_ECN_MASK) { 1585f2512ba1SRui Paulo case IPTOS_ECN_CE: 1586f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_ECE; 158778b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ce); 1588f2512ba1SRui Paulo break; 1589f2512ba1SRui Paulo case IPTOS_ECN_ECT0: 159078b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect0); 1591f2512ba1SRui Paulo break; 1592f2512ba1SRui Paulo case IPTOS_ECN_ECT1: 159378b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect1); 1594f2512ba1SRui Paulo break; 1595f2512ba1SRui Paulo } 159664807b30SHiren Panchasara 159764807b30SHiren Panchasara /* Process a packet differently from RFC3168. */ 159864807b30SHiren Panchasara cc_ecnpkt_handler(tp, th, iptos); 159964807b30SHiren Panchasara 1600dbc42409SLawrence Stewart /* Congestion experienced. */ 1601dbc42409SLawrence Stewart if (thflags & TH_ECE) { 1602dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_ECN); 1603f2512ba1SRui Paulo } 1604f2512ba1SRui Paulo } 1605f2512ba1SRui Paulo 1606f2512ba1SRui Paulo /* 1607f72167f4SAndre Oppermann * Parse options on any incoming segment. 1608f72167f4SAndre Oppermann */ 1609302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1610302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1611302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1612f72167f4SAndre Oppermann 1613fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1614fcf59617SAndrey V. Elsukov if ((tp->t_flags & TF_SIGNATURE) != 0 && 1615fcf59617SAndrey V. Elsukov (to.to_flags & TOF_SIGNATURE) == 0) { 1616fcf59617SAndrey V. Elsukov TCPSTAT_INC(tcps_sig_err_sigopt); 1617fcf59617SAndrey V. Elsukov /* XXX: should drop? */ 1618fcf59617SAndrey V. Elsukov } 1619fcf59617SAndrey V. Elsukov #endif 1620f72167f4SAndre Oppermann /* 1621f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1622bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1623bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1624bf6d304aSAndre Oppermann * was established. 1625f72167f4SAndre Oppermann */ 1626bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1627e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 1628d8951c8aSBjoern A. Zeeb if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) 1629f72167f4SAndre Oppermann to.to_tsecr = 0; 163010d20c84SMatt Macy else if (tp->t_flags & TF_PREVVALID && 163110d20c84SMatt Macy tp->t_badrxtwin != 0 && SEQ_LT(to.to_tsecr, tp->t_badrxtwin)) 163210d20c84SMatt Macy cc_cong_signal(tp, th, CC_RTO_ERR); 1633bf6d304aSAndre Oppermann } 163407dacf03SAndre Oppermann /* 163597d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 163697d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 16375396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 16385396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 163997d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1640df8bae1dSRodney W. Grimes */ 16410a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1642464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 1643464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 1644be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 164502a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 1646be2ac88cSJonathan Lemon } 16475396d0f8SAndre Oppermann /* 16485396d0f8SAndre Oppermann * Initial send window. It will be updated with 16495396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 16505396d0f8SAndre Oppermann */ 16515396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 1652be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1653be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1654be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1655d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1656be2ac88cSJonathan Lemon } 1657be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1658be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 16593529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 16603529149eSAndre Oppermann (to.to_flags & TOF_SACKPERM) == 0) 16613529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 1662c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) { 1663a026a53aSMichael Tuexen if (to.to_flags & TOF_FASTOPEN) { 1664a026a53aSMichael Tuexen uint16_t mss; 1665a026a53aSMichael Tuexen 1666a026a53aSMichael Tuexen if (to.to_flags & TOF_MSS) 1667a026a53aSMichael Tuexen mss = to.to_mss; 1668c560df6fSPatrick Kelsey else 1669a026a53aSMichael Tuexen if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) 1670a026a53aSMichael Tuexen mss = TCP6_MSS; 1671a026a53aSMichael Tuexen else 1672a026a53aSMichael Tuexen mss = TCP_MSS; 1673a026a53aSMichael Tuexen tcp_fastopen_update_cache(tp, mss, 1674a026a53aSMichael Tuexen to.to_tfo_len, to.to_tfo_cookie); 1675a026a53aSMichael Tuexen } else 1676c560df6fSPatrick Kelsey tcp_fastopen_disable_path(tp); 1677c560df6fSPatrick Kelsey } 16786d90faf3SPaul Saab } 16796d90faf3SPaul Saab 1680df8bae1dSRodney W. Grimes /* 16814da9052aSMichael Tuexen * If timestamps were negotiated during SYN/ACK they should 16824da9052aSMichael Tuexen * appear on every segment during this session and vice versa. 16834da9052aSMichael Tuexen */ 16844da9052aSMichael Tuexen if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) { 16854da9052aSMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 16864da9052aSMichael Tuexen log(LOG_DEBUG, "%s; %s: Timestamp missing, " 16874da9052aSMichael Tuexen "no action\n", s, __func__); 16884da9052aSMichael Tuexen free(s, M_TCPLOG); 16894da9052aSMichael Tuexen } 16904da9052aSMichael Tuexen } 16914da9052aSMichael Tuexen if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) { 16924da9052aSMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 16934da9052aSMichael Tuexen log(LOG_DEBUG, "%s; %s: Timestamp not expected, " 16944da9052aSMichael Tuexen "no action\n", s, __func__); 16954da9052aSMichael Tuexen free(s, M_TCPLOG); 16964da9052aSMichael Tuexen } 16974da9052aSMichael Tuexen } 16984da9052aSMichael Tuexen 16994da9052aSMichael Tuexen /* 1700df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1701df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1702df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1703df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1704df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1705df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1706df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1707df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1708df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1709df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1710df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1711df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1712a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1713c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 1714a0292f23SGarrett Wollman * be TH_NEEDSYN. 1715df8bae1dSRodney W. Grimes */ 1716df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1717c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 1718fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1719c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 1720c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 1721a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1722c28440dbSRandall Stewart SEGQ_EMPTY(tp) && 17234741bfcbSPatrick Kelsey ((to.to_flags & TOF_TS) == 0 || 1724c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1725df8bae1dSRodney W. Grimes 1726df8bae1dSRodney W. Grimes /* 1727df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1728df8bae1dSRodney W. Grimes * record the timestamp. 1729a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1730a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1731df8bae1dSRodney W. Grimes */ 1732be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1733fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1734d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1735a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1736df8bae1dSRodney W. Grimes } 1737df8bae1dSRodney W. Grimes 1738fb59c426SYoshinobu Inoue if (tlen == 0) { 1739fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1740fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1741dbc42409SLawrence Stewart !IN_RECOVERY(tp->t_flags) && 1742fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1743dbc42409SLawrence Stewart TAILQ_EMPTY(&tp->snd_holes)) { 1744df8bae1dSRodney W. Grimes /* 1745e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1746df8bae1dSRodney W. Grimes */ 174778b50714SRobert Watson TCPSTAT_INC(tcps_predack); 1748252ca428SRobert Watson 17499b8b58e0SJonathan Lemon /* 175010d20c84SMatt Macy * "bad retransmit" recovery without timestamps. 17519b8b58e0SJonathan Lemon */ 175210d20c84SMatt Macy if ((to.to_flags & TOF_TS) == 0 && 175310d20c84SMatt Macy tp->t_rxtshift == 1 && 1754672dc4aeSJohn Baldwin tp->t_flags & TF_PREVVALID && 17556dfb8b31SJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) { 1756dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 17579b8b58e0SJonathan Lemon } 1758fa55172bSMatthew Dillon 1759fa55172bSMatthew Dillon /* 1760fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1761fa55172bSMatthew Dillon * 1762fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1763fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1764fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1765fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1766fa55172bSMatthew Dillon */ 1767fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1768fa55172bSMatthew Dillon to.to_tsecr) { 17693ac12506SJonathan T. Looney uint32_t t; 1770d8951c8aSBjoern A. Zeeb 1771d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 1772d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 1773d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 1774a0292f23SGarrett Wollman tcp_xmit_timer(tp, 1775d8951c8aSBjoern A. Zeeb TCP_TS_TO_TICKS(t) + 1); 1776fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1777fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1778eaf80179SAndre Oppermann if (!tp->t_rttlow || 1779eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1780eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1781c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1782c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1783fa55172bSMatthew Dillon } 1784dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 178539bc9de5SLawrence Stewart 1786bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 178739bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 178839bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 1789bd79708dSJonathan T. Looney #endif 179039bc9de5SLawrence Stewart 17914b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvackpack, nsegs); 179278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 1793df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 17949d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 17959d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 17969d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 1797dbc42409SLawrence Stewart 1798dbc42409SLawrence Stewart /* 1799dbc42409SLawrence Stewart * Let the congestion control algorithm update 1800dbc42409SLawrence Stewart * congestion control related information. This 1801dbc42409SLawrence Stewart * typically means increasing the congestion 1802dbc42409SLawrence Stewart * window. 1803dbc42409SLawrence Stewart */ 18044b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, CC_ACK); 1805dbc42409SLawrence Stewart 18069d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 18071645d090SJeff Roberson /* 1808e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 18091645d090SJeff Roberson * to th_ack. 18101645d090SJeff Roberson */ 1811cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1812b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1813df8bae1dSRodney W. Grimes m_freem(m); 1814df8bae1dSRodney W. Grimes 1815df8bae1dSRodney W. Grimes /* 1816df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1817df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1818df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1819df8bae1dSRodney W. Grimes * If process is waiting for space, 1820df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1821df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1822df8bae1dSRodney W. Grimes * decide between more output or persist. 1823e8949f74SAndre Oppermann */ 18243c653157SHartmut Brandt #ifdef TCPDEBUG 18253c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 18263c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 18273c653157SHartmut Brandt (void *)tcp_saveipgen, 18283c653157SHartmut Brandt &tcp_savetcp, 0); 18293c653157SHartmut Brandt #endif 18302b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 1831df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1832b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1833b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1834b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1835b8152ba7SAndre Oppermann tp->t_rxtcur); 1836df8bae1dSRodney W. Grimes sowwakeup(so); 1837cfa6009eSGleb Smirnoff if (sbavail(&so->so_snd)) 183855bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 1839a14c749fSJonathan Lemon goto check_delack; 1840df8bae1dSRodney W. Grimes } 1841fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1842fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 18436741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 18446741ecf5SAndre Oppermann 1845df8bae1dSRodney W. Grimes /* 1846252ca428SRobert Watson * This is a pure, in-sequence data packet with 1847252ca428SRobert Watson * nothing on the reassembly queue and we have enough 1848252ca428SRobert Watson * buffer space to take it. 1849df8bae1dSRodney W. Grimes */ 18506d90faf3SPaul Saab /* Clean receiver SACK report if present */ 18513529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 18526d90faf3SPaul Saab tcp_clean_sackreport(tp); 185378b50714SRobert Watson TCPSTAT_INC(tcps_preddat); 1854fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 18551645d090SJeff Roberson /* 18561645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 18571645d090SJeff Roberson * th_seq. 18581645d090SJeff Roberson */ 185960ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 18601645d090SJeff Roberson /* 18611645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 18621645d090SJeff Roberson * rcv_nxt. 18631645d090SJeff Roberson */ 18641645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 18654b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvpack, nsegs); 186678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 18673c653157SHartmut Brandt #ifdef TCPDEBUG 18683c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 18693c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 18703c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 18713c653157SHartmut Brandt #endif 18722b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 18735d06879aSGeorge V. Neville-Neil 1874e44c1887SSteven Hartland newsize = tcp_autorcvbuf(m, th, so, tp, tlen); 18756741ecf5SAndre Oppermann 18766741ecf5SAndre Oppermann /* Add data to socket buffer. */ 18771e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1878c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1879c1c36a2cSMike Silbersack m_freem(m); 1880c1c36a2cSMike Silbersack } else { 18816741ecf5SAndre Oppermann /* 18826741ecf5SAndre Oppermann * Set new socket buffer size. 18836741ecf5SAndre Oppermann * Give up when limit is reached. 18846741ecf5SAndre Oppermann */ 18856741ecf5SAndre Oppermann if (newsize) 18866741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 18876c8286e4SRobert Watson newsize, so, NULL)) 18886741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1889fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 1890651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 1891c1c36a2cSMike Silbersack } 1892e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 18931e4d7da7SRobert Watson sorwakeup_locked(so); 1894c1e5a6e5SAndre Oppermann if (DELAY_ACK(tp, tlen)) { 18953bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1896f498eeeeSDavid Greenman } else { 1897e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 189855bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 1899e612a582SDavid Greenman } 1900a14c749fSJonathan Lemon goto check_delack; 1901df8bae1dSRodney W. Grimes } 1902df8bae1dSRodney W. Grimes } 1903df8bae1dSRodney W. Grimes 1904df8bae1dSRodney W. Grimes /* 1905df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1906df8bae1dSRodney W. Grimes * and then do TCP input processing. 1907df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1908df8bae1dSRodney W. Grimes * but not less than advertised window. 1909df8bae1dSRodney W. Grimes */ 1910df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1911df8bae1dSRodney W. Grimes if (win < 0) 1912df8bae1dSRodney W. Grimes win = 0; 191366e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1914df8bae1dSRodney W. Grimes 1915df8bae1dSRodney W. Grimes switch (tp->t_state) { 1916df8bae1dSRodney W. Grimes 1917df8bae1dSRodney W. Grimes /* 1918764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1919764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1920764d8cefSBill Fenner */ 1921764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1922fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1923fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1924a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1925a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1926a57815efSBosko Milekic goto dropwithreset; 1927a57815efSBosko Milekic } 192868bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) { 1929281a0fd4SPatrick Kelsey /* 1930281a0fd4SPatrick Kelsey * When a TFO connection is in SYN_RECEIVED, the 1931281a0fd4SPatrick Kelsey * only valid packets are the initial SYN, a 1932281a0fd4SPatrick Kelsey * retransmit/copy of the initial SYN (possibly with 1933281a0fd4SPatrick Kelsey * a subset of the original data), a valid ACK, a 1934281a0fd4SPatrick Kelsey * FIN, or a RST. 1935281a0fd4SPatrick Kelsey */ 1936281a0fd4SPatrick Kelsey if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) { 1937281a0fd4SPatrick Kelsey rstreason = BANDLIM_RST_OPENPORT; 1938281a0fd4SPatrick Kelsey goto dropwithreset; 1939281a0fd4SPatrick Kelsey } else if (thflags & TH_SYN) { 1940281a0fd4SPatrick Kelsey /* non-initial SYN is ignored */ 1941281a0fd4SPatrick Kelsey if ((tcp_timer_active(tp, TT_DELACK) || 1942281a0fd4SPatrick Kelsey tcp_timer_active(tp, TT_REXMT))) 1943281a0fd4SPatrick Kelsey goto drop; 1944281a0fd4SPatrick Kelsey } else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) { 1945281a0fd4SPatrick Kelsey goto drop; 1946281a0fd4SPatrick Kelsey } 1947281a0fd4SPatrick Kelsey } 1948764d8cefSBill Fenner break; 1949764d8cefSBill Fenner 1950764d8cefSBill Fenner /* 1951df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 195298732609SMichael Tuexen * if seg contains a RST with valid ACK (SEQ.ACK has already 195398732609SMichael Tuexen * been verified), then drop the connection. 195498732609SMichael Tuexen * if seg contains a RST without an ACK, drop the seg. 195598732609SMichael Tuexen * if seg does not contain SYN, then drop the seg. 1956df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1957df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1958df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1959f2512ba1SRui Paulo * if seg contains an ECE and ECN support is enabled, the stream 1960f2512ba1SRui Paulo * is ECN capable. 1961df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1962df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1963df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1964df8bae1dSRodney W. Grimes */ 1965df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 196657f60867SMark Johnston if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) { 1967d9fae5abSAndriy Gapon TCP_PROBE5(connect__refused, NULL, tp, 19682b9c9984SGeorge V. Neville-Neil m, tp, th); 1969df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 197057f60867SMark Johnston } 19710ca3f933SAndre Oppermann if (thflags & TH_RST) 1972df8bae1dSRodney W. Grimes goto drop; 19730ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 1974df8bae1dSRodney W. Grimes goto drop; 1975464fcfbcSAndre Oppermann 1976fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1977df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1978fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1979c560df6fSPatrick Kelsey int tfo_partial_ack = 0; 1980c560df6fSPatrick Kelsey 198178b50714SRobert Watson TCPSTAT_INC(tcps_connects); 1982845799c1SAndras Olah soisconnected(so); 1983c488362eSRobert Watson #ifdef MAC 198430d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 1985c488362eSRobert Watson #endif 1986845799c1SAndras Olah /* Do window scaling on this connection? */ 1987845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1988845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1989845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1990845799c1SAndras Olah } 19913ac12506SJonathan T. Looney tp->rcv_adv += min(tp->rcv_wnd, 1992766282cbSJohn Baldwin TCP_MAXWIN << tp->rcv_scale); 1993a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1994a0292f23SGarrett Wollman /* 1995c560df6fSPatrick Kelsey * If not all the data that was sent in the TFO SYN 1996c560df6fSPatrick Kelsey * has been acked, resend the remainder right away. 1997c560df6fSPatrick Kelsey */ 1998c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags) && 1999c560df6fSPatrick Kelsey (tp->snd_una != tp->snd_max)) { 2000c560df6fSPatrick Kelsey tp->snd_nxt = th->th_ack; 2001c560df6fSPatrick Kelsey tfo_partial_ack = 1; 2002c560df6fSPatrick Kelsey } 2003c560df6fSPatrick Kelsey /* 2004a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 2005a0292f23SGarrett Wollman * ACKNOW will be turned on later. 2006a0292f23SGarrett Wollman */ 2007c560df6fSPatrick Kelsey if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial_ack) 2008b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 2009b8152ba7SAndre Oppermann tcp_delacktime); 2010a0292f23SGarrett Wollman else 2011a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2012f2512ba1SRui Paulo 2013bf7fcdb1SMichael Tuexen if (((thflags & (TH_CWR | TH_ECE)) == TH_ECE) && 2014bf7fcdb1SMichael Tuexen V_tcp_do_ecn) { 2015f2512ba1SRui Paulo tp->t_flags |= TF_ECN_PERMIT; 201678b50714SRobert Watson TCPSTAT_INC(tcps_ecn_shs); 2017f2512ba1SRui Paulo } 2018f2512ba1SRui Paulo 2019a0292f23SGarrett Wollman /* 2020a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 2021a0292f23SGarrett Wollman * Transitions: 2022a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 2023a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 2024a0292f23SGarrett Wollman */ 20259b8b58e0SJonathan Lemon tp->t_starttime = ticks; 2026a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 202757f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2028a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 2029fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 20307ff19458SPaul Traina } else { 203157f60867SMark Johnston tcp_state_change(tp, TCPS_ESTABLISHED); 2032d9fae5abSAndriy Gapon TCP_PROBE5(connect__established, NULL, tp, 20332b9c9984SGeorge V. Neville-Neil m, tp, th); 2034dbc42409SLawrence Stewart cc_conn_init(tp); 20359077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 20369077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 20377ff19458SPaul Traina } 2038a0292f23SGarrett Wollman } else { 2039a0292f23SGarrett Wollman /* 2040c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 2041153edc50SHiren Panchasara * simultaneous open. 2042c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 2043c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 2044a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 2045a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 2046a0292f23SGarrett Wollman */ 20474b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 2048b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 204957f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_RECEIVED); 2050a0292f23SGarrett Wollman } 2051df8bae1dSRodney W. Grimes 2052ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 20538501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 20547cfc6904SRobert Watson 2055df8bae1dSRodney W. Grimes /* 2056fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 2057df8bae1dSRodney W. Grimes * If data, trim to stay within window, 2058df8bae1dSRodney W. Grimes * dropping FIN if necessary. 2059df8bae1dSRodney W. Grimes */ 2060fb59c426SYoshinobu Inoue th->th_seq++; 2061fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 2062fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 2063df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2064fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 2065fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 206678b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 206778b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2068df8bae1dSRodney W. Grimes } 2069fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 2070fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 2071a0292f23SGarrett Wollman /* 2072a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 2073a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 2074a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 2075a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 2076a0292f23SGarrett Wollman * Otherwise, goto step 6. 2077a0292f23SGarrett Wollman */ 2078fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 2079a0292f23SGarrett Wollman goto process_ACK; 2080c068736aSJeffrey Hsu 2081df8bae1dSRodney W. Grimes goto step6; 2082c068736aSJeffrey Hsu 2083a0292f23SGarrett Wollman /* 2084a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 2085c94c54e4SAndre Oppermann * do normal processing. 2086a0292f23SGarrett Wollman * 2087c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 2088a0292f23SGarrett Wollman */ 2089a0292f23SGarrett Wollman case TCPS_LAST_ACK: 2090a0292f23SGarrett Wollman case TCPS_CLOSING: 2091a0292f23SGarrett Wollman break; /* continue normal processing */ 2092df8bae1dSRodney W. Grimes } 2093df8bae1dSRodney W. Grimes 2094df8bae1dSRodney W. Grimes /* 2095df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 209680ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 209780ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 209880ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 209980ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 210080ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 210180ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 210280ab7c0eSGarrett Wollman * killing a connection). 210380ab7c0eSGarrett Wollman * Then check timestamp, if present. 2104a0292f23SGarrett Wollman * Then check the connection count, if present. 2105df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 2106df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 2107df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 210880ab7c0eSGarrett Wollman */ 2109fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 21103220a212SGleb Smirnoff /* 21113220a212SGleb Smirnoff * RFC5961 Section 3.2 21123220a212SGleb Smirnoff * 21133220a212SGleb Smirnoff * - RST drops connection only if SEG.SEQ == RCV.NXT. 21143220a212SGleb Smirnoff * - If RST is in window, we send challenge ACK. 21153220a212SGleb Smirnoff * 21163220a212SGleb Smirnoff * Note: to take into account delayed ACKs, we should 21173220a212SGleb Smirnoff * test against last_ack_sent instead of rcv_nxt. 21183220a212SGleb Smirnoff * Note 2: we handle special case of closed window, not 21193220a212SGleb Smirnoff * covered by the RFC. 21203220a212SGleb Smirnoff */ 21213220a212SGleb Smirnoff if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 21223220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) || 21233220a212SGleb Smirnoff (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) { 212480ab7c0eSGarrett Wollman 2125ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 21263220a212SGleb Smirnoff KASSERT(tp->t_state != TCPS_SYN_SENT, 21273220a212SGleb Smirnoff ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p", 21283220a212SGleb Smirnoff __func__, th, tp)); 21293220a212SGleb Smirnoff 21303220a212SGleb Smirnoff if (V_tcp_insecure_rst || 21313220a212SGleb Smirnoff tp->last_ack_sent == th->th_seq) { 21323220a212SGleb Smirnoff TCPSTAT_INC(tcps_drops); 21333220a212SGleb Smirnoff /* Drop the connection. */ 21343220a212SGleb Smirnoff switch (tp->t_state) { 213580ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 213680ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 213780ab7c0eSGarrett Wollman goto close; 213880ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 213980ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 214080ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 214180ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 21426779a1a1SMichael Tuexen case TCPS_CLOSING: 21436779a1a1SMichael Tuexen case TCPS_LAST_ACK: 214480ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 214580ab7c0eSGarrett Wollman close: 21463220a212SGleb Smirnoff /* FALLTHROUGH */ 21473220a212SGleb Smirnoff default: 214880ab7c0eSGarrett Wollman tp = tcp_close(tp); 21493220a212SGleb Smirnoff } 21503220a212SGleb Smirnoff } else { 21513220a212SGleb Smirnoff TCPSTAT_INC(tcps_badrst); 21523220a212SGleb Smirnoff /* Send challenge ACK. */ 21533220a212SGleb Smirnoff tcp_respond(tp, mtod(m, void *), th, m, 21543220a212SGleb Smirnoff tp->rcv_nxt, tp->snd_nxt, TH_ACK); 21553220a212SGleb Smirnoff tp->last_ack_sent = tp->rcv_nxt; 21563220a212SGleb Smirnoff m = NULL; 21573220a212SGleb Smirnoff } 21583220a212SGleb Smirnoff } 21593220a212SGleb Smirnoff goto drop; 21603220a212SGleb Smirnoff } 216180ab7c0eSGarrett Wollman 21623220a212SGleb Smirnoff /* 21633220a212SGleb Smirnoff * RFC5961 Section 4.2 21643220a212SGleb Smirnoff * Send challenge ACK for any SYN in synchronized state. 21653220a212SGleb Smirnoff */ 2166281a0fd4SPatrick Kelsey if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT && 2167281a0fd4SPatrick Kelsey tp->t_state != TCPS_SYN_RECEIVED) { 2168ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2169252ca428SRobert Watson 21703220a212SGleb Smirnoff TCPSTAT_INC(tcps_badsyn); 21713220a212SGleb Smirnoff if (V_tcp_insecure_syn && 21723220a212SGleb Smirnoff SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 21733220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 21743220a212SGleb Smirnoff tp = tcp_drop(tp, ECONNRESET); 21753220a212SGleb Smirnoff rstreason = BANDLIM_UNLIMITED; 21763220a212SGleb Smirnoff } else { 21773220a212SGleb Smirnoff /* Send challenge ACK. */ 21783220a212SGleb Smirnoff tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt, 21793220a212SGleb Smirnoff tp->snd_nxt, TH_ACK); 21803220a212SGleb Smirnoff tp->last_ack_sent = tp->rcv_nxt; 21813220a212SGleb Smirnoff m = NULL; 218280ab7c0eSGarrett Wollman } 218380ab7c0eSGarrett Wollman goto drop; 218480ab7c0eSGarrett Wollman } 218580ab7c0eSGarrett Wollman 218680ab7c0eSGarrett Wollman /* 2187df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 2188df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 2189df8bae1dSRodney W. Grimes */ 2190be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 219180ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 2192df8bae1dSRodney W. Grimes 2193df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 2194d8951c8aSBjoern A. Zeeb if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 2195df8bae1dSRodney W. Grimes /* 2196df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 2197df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 2198df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 2199df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 2200df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 2201df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 2202df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 2203df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 2204df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 2205df8bae1dSRodney W. Grimes */ 2206df8bae1dSRodney W. Grimes tp->ts_recent = 0; 2207df8bae1dSRodney W. Grimes } else { 220878b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 220978b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 221078b50714SRobert Watson TCPSTAT_INC(tcps_pawsdrop); 221107fd333dSMatthew Dillon if (tlen) 2212df8bae1dSRodney W. Grimes goto dropafterack; 22131ab4789dSMatthew Dillon goto drop; 22141ab4789dSMatthew Dillon } 2215df8bae1dSRodney W. Grimes } 2216df8bae1dSRodney W. Grimes 2217a0292f23SGarrett Wollman /* 221880ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 221980ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 222080ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 222180ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 222280ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 222380ab7c0eSGarrett Wollman */ 2224a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 2225a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2226a57815efSBosko Milekic goto dropwithreset; 2227a57815efSBosko Milekic } 222880ab7c0eSGarrett Wollman 2229fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 2230df8bae1dSRodney W. Grimes if (todrop > 0) { 2231831ad37eSXin LI if (thflags & TH_SYN) { 2232fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 2233fb59c426SYoshinobu Inoue th->th_seq++; 2234fb59c426SYoshinobu Inoue if (th->th_urp > 1) 2235fb59c426SYoshinobu Inoue th->th_urp--; 2236df8bae1dSRodney W. Grimes else 2237fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2238df8bae1dSRodney W. Grimes todrop--; 2239df8bae1dSRodney W. Grimes } 2240df8bae1dSRodney W. Grimes /* 2241dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 2242df8bae1dSRodney W. Grimes */ 2243fb59c426SYoshinobu Inoue if (todrop > tlen 2244fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 2245dac20301SGarrett Wollman /* 2246dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 2247dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 2248dac20301SGarrett Wollman * of sequence; drop it. 2249dac20301SGarrett Wollman */ 2250fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2251dac20301SGarrett Wollman 2252df8bae1dSRodney W. Grimes /* 2253dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 2254dac20301SGarrett Wollman * But keep on processing for RST or ACK. 2255df8bae1dSRodney W. Grimes */ 2256dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2257fb59c426SYoshinobu Inoue todrop = tlen; 225878b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 225978b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 2260df8bae1dSRodney W. Grimes } else { 226178b50714SRobert Watson TCPSTAT_INC(tcps_rcvpartduppack); 226278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 2263df8bae1dSRodney W. Grimes } 22645acfd95cSMichael Tuexen /* 22655acfd95cSMichael Tuexen * DSACK - add SACK block for dropped range 22665acfd95cSMichael Tuexen */ 22675acfd95cSMichael Tuexen if (tp->t_flags & TF_SACK_PERMIT) { 22685acfd95cSMichael Tuexen tcp_update_sack_list(tp, th->th_seq, th->th_seq+tlen); 22695acfd95cSMichael Tuexen /* 22705acfd95cSMichael Tuexen * ACK now, as the next in-sequence segment 22715acfd95cSMichael Tuexen * will clear the DSACK block again 22725acfd95cSMichael Tuexen */ 22735acfd95cSMichael Tuexen tp->t_flags |= TF_ACKNOW; 22745acfd95cSMichael Tuexen } 2275fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 2276fb59c426SYoshinobu Inoue th->th_seq += todrop; 2277fb59c426SYoshinobu Inoue tlen -= todrop; 2278fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 2279fb59c426SYoshinobu Inoue th->th_urp -= todrop; 2280df8bae1dSRodney W. Grimes else { 2281fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2282fb59c426SYoshinobu Inoue th->th_urp = 0; 2283df8bae1dSRodney W. Grimes } 2284df8bae1dSRodney W. Grimes } 2285df8bae1dSRodney W. Grimes 2286df8bae1dSRodney W. Grimes /* 2287df8bae1dSRodney W. Grimes * If new data are received on a connection after the 2288df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 2289df8bae1dSRodney W. Grimes */ 2290df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 2291fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 2292ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2293252ca428SRobert Watson 229407dacf03SAndre Oppermann if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 229507dacf03SAndre Oppermann log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data " 229607dacf03SAndre Oppermann "after socket was closed, " 229707dacf03SAndre Oppermann "sending RST and removing tcpcb\n", 2298e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 2299773673c1SAndre Oppermann free(s, M_TCPLOG); 2300773673c1SAndre Oppermann } 2301df8bae1dSRodney W. Grimes tp = tcp_close(tp); 230278b50714SRobert Watson TCPSTAT_INC(tcps_rcvafterclose); 2303a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2304df8bae1dSRodney W. Grimes goto dropwithreset; 2305df8bae1dSRodney W. Grimes } 2306df8bae1dSRodney W. Grimes 2307df8bae1dSRodney W. Grimes /* 2308df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 2309df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 2310df8bae1dSRodney W. Grimes */ 2311fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 2312df8bae1dSRodney W. Grimes if (todrop > 0) { 231378b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 2314fb59c426SYoshinobu Inoue if (todrop >= tlen) { 231578b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 2316df8bae1dSRodney W. Grimes /* 2317df8bae1dSRodney W. Grimes * If window is closed can only take segments at 2318df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 2319df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 2320df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 2321df8bae1dSRodney W. Grimes * and ack. 2322df8bae1dSRodney W. Grimes */ 2323fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2324df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 232578b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinprobe); 2326df8bae1dSRodney W. Grimes } else 2327df8bae1dSRodney W. Grimes goto dropafterack; 2328df8bae1dSRodney W. Grimes } else 232978b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2330df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2331fb59c426SYoshinobu Inoue tlen -= todrop; 2332fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 2333df8bae1dSRodney W. Grimes } 2334df8bae1dSRodney W. Grimes 2335df8bae1dSRodney W. Grimes /* 2336df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 2337df8bae1dSRodney W. Grimes * record its timestamp. 2338cf09195bSPaul Saab * NOTE: 2339cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 2340a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2341cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 2342cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 2343cf09195bSPaul Saab * predicated on the sequence space of this segment. 2344cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 2345cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 2346cf09195bSPaul Saab * instead of RFC1323's 2347cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 2348cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 2349cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 2350cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 2351cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 2352df8bae1dSRodney W. Grimes */ 2353be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 2354cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2355cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 2356cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 2357d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 2358a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 2359df8bae1dSRodney W. Grimes } 2360df8bae1dSRodney W. Grimes 2361df8bae1dSRodney W. Grimes /* 2362a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 2363a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 2364a0292f23SGarrett Wollman * later processing; else drop segment and return. 2365a0292f23SGarrett Wollman */ 2366fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 2367a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 2368281a0fd4SPatrick Kelsey (tp->t_flags & TF_NEEDSYN)) { 2369281a0fd4SPatrick Kelsey if (tp->t_state == TCPS_SYN_RECEIVED && 237068bd7ed1SJonathan T. Looney IS_FASTOPEN(tp->t_flags)) { 2371281a0fd4SPatrick Kelsey tp->snd_wnd = tiwin; 2372281a0fd4SPatrick Kelsey cc_conn_init(tp); 2373281a0fd4SPatrick Kelsey } 2374a0292f23SGarrett Wollman goto step6; 2375281a0fd4SPatrick Kelsey } else if (tp->t_flags & TF_ACKNOW) 23765e1aa279SDavid Malone goto dropafterack; 2377a0292f23SGarrett Wollman else 2378a0292f23SGarrett Wollman goto drop; 2379a0292f23SGarrett Wollman } 2380df8bae1dSRodney W. Grimes 2381df8bae1dSRodney W. Grimes /* 2382df8bae1dSRodney W. Grimes * Ack processing. 2383df8bae1dSRodney W. Grimes */ 2384df8bae1dSRodney W. Grimes switch (tp->t_state) { 2385df8bae1dSRodney W. Grimes 2386df8bae1dSRodney W. Grimes /* 2387764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 2388764d8cefSBill Fenner * ESTABLISHED state and continue processing. 2389764d8cefSBill Fenner * The ACK was checked above. 2390df8bae1dSRodney W. Grimes */ 2391df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2392a0292f23SGarrett Wollman 239378b50714SRobert Watson TCPSTAT_INC(tcps_connects); 2394df8bae1dSRodney W. Grimes soisconnected(so); 2395df8bae1dSRodney W. Grimes /* Do window scaling? */ 2396df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2397df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2398df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 2399df8bae1dSRodney W. Grimes } 2400116ef4d6SMichael Tuexen tp->snd_wnd = tiwin; 2401a0292f23SGarrett Wollman /* 2402a0292f23SGarrett Wollman * Make transitions: 2403a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 2404a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 2405a0292f23SGarrett Wollman */ 24069b8b58e0SJonathan Lemon tp->t_starttime = ticks; 240718a75309SPatrick Kelsey if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) { 2408281a0fd4SPatrick Kelsey tcp_fastopen_decrement_counter(tp->t_tfo_pending); 2409281a0fd4SPatrick Kelsey tp->t_tfo_pending = NULL; 2410281a0fd4SPatrick Kelsey 2411281a0fd4SPatrick Kelsey /* 2412281a0fd4SPatrick Kelsey * Account for the ACK of our SYN prior to 2413281a0fd4SPatrick Kelsey * regular ACK processing below. 2414281a0fd4SPatrick Kelsey */ 2415281a0fd4SPatrick Kelsey tp->snd_una++; 2416281a0fd4SPatrick Kelsey } 24178db239dcSMichael Tuexen if (tp->t_flags & TF_NEEDFIN) { 24188db239dcSMichael Tuexen tcp_state_change(tp, TCPS_FIN_WAIT_1); 24198db239dcSMichael Tuexen tp->t_flags &= ~TF_NEEDFIN; 24208db239dcSMichael Tuexen } else { 24218db239dcSMichael Tuexen tcp_state_change(tp, TCPS_ESTABLISHED); 24228db239dcSMichael Tuexen TCP_PROBE5(accept__established, NULL, tp, 24238db239dcSMichael Tuexen m, tp, th); 2424281a0fd4SPatrick Kelsey /* 2425281a0fd4SPatrick Kelsey * TFO connections call cc_conn_init() during SYN 2426281a0fd4SPatrick Kelsey * processing. Calling it again here for such 2427281a0fd4SPatrick Kelsey * connections is not harmless as it would undo the 2428281a0fd4SPatrick Kelsey * snd_cwnd reduction that occurs when a TFO SYN|ACK 2429281a0fd4SPatrick Kelsey * is retransmitted. 2430281a0fd4SPatrick Kelsey */ 243168bd7ed1SJonathan T. Looney if (!IS_FASTOPEN(tp->t_flags)) 2432dbc42409SLawrence Stewart cc_conn_init(tp); 24339077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 24347ff19458SPaul Traina } 2435a0292f23SGarrett Wollman /* 2436a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 2437a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 2438a0292f23SGarrett Wollman */ 2439fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 2440c28440dbSRandall Stewart (void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0, 2441a0292f23SGarrett Wollman (struct mbuf *)0); 244260ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq - 1; 244393b0017fSPhilippe Charnier /* FALLTHROUGH */ 2444df8bae1dSRodney W. Grimes 2445df8bae1dSRodney W. Grimes /* 2446df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2447df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 2448fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 2449fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 2450df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 2451df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 2452df8bae1dSRodney W. Grimes */ 2453df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2454df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2455df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2456df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 2457df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2458df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 24595a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 246078b50714SRobert Watson TCPSTAT_INC(tcps_rcvacktoomuch); 24615a53ca16SPaul Saab goto dropafterack; 24625a53ca16SPaul Saab } 24633529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2464fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 2465fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 2466021eaf79SHiren Panchasara sack_changed = tcp_sack_doack(tp, &to, th->th_ack); 246712eeb81fSHiren Panchasara else 246812eeb81fSHiren Panchasara /* 246912eeb81fSHiren Panchasara * Reset the value so that previous (valid) value 247012eeb81fSHiren Panchasara * from the last ack with SACK doesn't get used. 247112eeb81fSHiren Panchasara */ 247212eeb81fSHiren Panchasara tp->sackhint.sacked_bytes = 0; 247339bc9de5SLawrence Stewart 2474bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 247539bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 247639bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 2477bd79708dSJonathan T. Looney #endif 247839bc9de5SLawrence Stewart 2479fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 24800c39d38dSGleb Smirnoff u_int maxseg; 24810c39d38dSGleb Smirnoff 24820c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 2483021eaf79SHiren Panchasara if (tlen == 0 && 2484021eaf79SHiren Panchasara (tiwin == tp->snd_wnd || 2485021eaf79SHiren Panchasara (tp->t_flags & TF_SACK_PERMIT))) { 248662b90589SPeter Wemm /* 248762b90589SPeter Wemm * If this is the first time we've seen a 248862b90589SPeter Wemm * FIN from the remote, this is not a 248962b90589SPeter Wemm * duplicate and it needs to be processed 249062b90589SPeter Wemm * normally. This happens during a 249162b90589SPeter Wemm * simultaneous close. 249262b90589SPeter Wemm */ 249362b90589SPeter Wemm if ((thflags & TH_FIN) && 249462b90589SPeter Wemm (TCPS_HAVERCVDFIN(tp->t_state) == 0)) { 249562b90589SPeter Wemm tp->t_dupacks = 0; 249662b90589SPeter Wemm break; 249762b90589SPeter Wemm } 249878b50714SRobert Watson TCPSTAT_INC(tcps_rcvdupack); 2499df8bae1dSRodney W. Grimes /* 2500df8bae1dSRodney W. Grimes * If we have outstanding data (other than 2501df8bae1dSRodney W. Grimes * a window probe), this is a completely 2502df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 25035f30ec9bSEitan Adler * change and FIN isn't set), 25045f30ec9bSEitan Adler * the ack is the biggest we've 2505df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 2506a4641f4eSPedro F. Giffuni * threshold of them, assume a packet 2507df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 2508df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 2509df8bae1dSRodney W. Grimes * window so we send only this one 2510df8bae1dSRodney W. Grimes * packet. 2511df8bae1dSRodney W. Grimes * 2512df8bae1dSRodney W. Grimes * We know we're losing at the current 2513df8bae1dSRodney W. Grimes * window size so do congestion avoidance 2514df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 2515df8bae1dSRodney W. Grimes * and pull our congestion window back to 2516df8bae1dSRodney W. Grimes * the new ssthresh). 2517df8bae1dSRodney W. Grimes * 2518df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 2519df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 2520df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 2521df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 2522df8bae1dSRodney W. Grimes * network. 2523f2512ba1SRui Paulo * 2524f2512ba1SRui Paulo * When using TCP ECN, notify the peer that 2525f2512ba1SRui Paulo * we reduced the cwnd. 2526df8bae1dSRodney W. Grimes */ 2527021eaf79SHiren Panchasara /* 2528021eaf79SHiren Panchasara * Following 2 kinds of acks should not affect 2529021eaf79SHiren Panchasara * dupack counting: 2530021eaf79SHiren Panchasara * 1) Old acks 2531021eaf79SHiren Panchasara * 2) Acks with SACK but without any new SACK 2532021eaf79SHiren Panchasara * information in them. These could result from 2533021eaf79SHiren Panchasara * any anomaly in the network like a switch 2534021eaf79SHiren Panchasara * duplicating packets or a possible DoS attack. 2535021eaf79SHiren Panchasara */ 2536021eaf79SHiren Panchasara if (th->th_ack != tp->snd_una || 2537021eaf79SHiren Panchasara ((tp->t_flags & TF_SACK_PERMIT) && 2538021eaf79SHiren Panchasara !sack_changed)) 2539021eaf79SHiren Panchasara break; 2540021eaf79SHiren Panchasara else if (!tcp_timer_active(tp, TT_REXMT)) 2541df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2542cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 2543dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 25444b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 25454b7b743cSLawrence Stewart CC_DUPACK); 25463529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2547dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2548808f11b7SPaul Saab int awnd; 254925e6f9edSPaul Saab 255025e6f9edSPaul Saab /* 255125e6f9edSPaul Saab * Compute the amount of data in flight first. 25524c3972f0SHiren Panchasara * We can inject new data into the pipe iff 255325e6f9edSPaul Saab * we have less than 1/2 the original window's 255425e6f9edSPaul Saab * worth of data in flight. 255525e6f9edSPaul Saab */ 255612eeb81fSHiren Panchasara if (V_tcp_do_rfc6675_pipe) 255712eeb81fSHiren Panchasara awnd = tcp_compute_pipe(tp); 255812eeb81fSHiren Panchasara else 2559808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 25600077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 256112eeb81fSHiren Panchasara 2562808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 25630c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 256425e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 256525e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 256625e6f9edSPaul Saab } 256725e6f9edSPaul Saab } else 25680c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 256955bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 257046f58482SJonathan Lemon goto drop; 2571cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 2572cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 2573a0445c2eSJayanth Vijayaraghavan 2574a0445c2eSJayanth Vijayaraghavan /* 2575a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 2576a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 2577a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 2578a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 2579a0445c2eSJayanth Vijayaraghavan * recovery. 2580a0445c2eSJayanth Vijayaraghavan */ 25813529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 2582dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2583a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 2584a0445c2eSJayanth Vijayaraghavan break; 2585a0445c2eSJayanth Vijayaraghavan } 2586dbc42409SLawrence Stewart } else { 2587a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 25889d11646dSJeffrey Hsu tp->snd_recover)) { 2589cb942153SJeffrey Hsu tp->t_dupacks = 0; 2590cb942153SJeffrey Hsu break; 259146f58482SJonathan Lemon } 2592a0445c2eSJayanth Vijayaraghavan } 2593dbc42409SLawrence Stewart /* Congestion signal before ack. */ 2594dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_NDUPACK); 25954b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 25964b7b743cSLawrence Stewart CC_DUPACK); 2597b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 25989b8b58e0SJonathan Lemon tp->t_rtttime = 0; 25993529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 260078b50714SRobert Watson TCPSTAT_INC( 260178b50714SRobert Watson tcps_sack_recovery_episode); 2602a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 26030c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 260455bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 26056d90faf3SPaul Saab goto drop; 26066d90faf3SPaul Saab } 2607fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 26080c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 260955bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 261048d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 2611302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 2612302ce8d6SAndre Oppermann __func__)); 2613df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 26140c39d38dSGleb Smirnoff maxseg * 261548d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 2616df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 2617df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 2618df8bae1dSRodney W. Grimes goto drop; 2619603724d3SBjoern A. Zeeb } else if (V_tcp_do_rfc3042) { 262062d4443fSHiren Panchasara /* 262162d4443fSHiren Panchasara * Process first and second duplicate 262262d4443fSHiren Panchasara * ACKs. Each indicates a segment 262362d4443fSHiren Panchasara * leaving the network, creating room 262462d4443fSHiren Panchasara * for more. Make sure we can send a 262562d4443fSHiren Panchasara * packet on reception of each duplicate 262662d4443fSHiren Panchasara * ACK by increasing snd_cwnd by one 262762d4443fSHiren Panchasara * segment. Restore the original 262862d4443fSHiren Panchasara * snd_cwnd after packet transmission. 262962d4443fSHiren Panchasara */ 26304b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 26314b7b743cSLawrence Stewart CC_DUPACK); 26323ac12506SJonathan T. Looney uint32_t oldcwnd = tp->snd_cwnd; 263348d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 263448d2549cSJeffrey Hsu u_int sent; 26355628dd08SAndre Oppermann int avail; 263689c02376SJeffrey Hsu 2637582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 2638582a954bSJeffrey Hsu tp->t_dupacks == 2, 2639302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 2640302ce8d6SAndre Oppermann __func__)); 264161a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 264248d2549cSJeffrey Hsu tp->snd_limited = 0; 264361a36e3dSJeffrey Hsu tp->snd_cwnd = 264461a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 264561a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 26460c39d38dSGleb Smirnoff maxseg; 26475628dd08SAndre Oppermann /* 26485628dd08SAndre Oppermann * Only call tcp_output when there 26495628dd08SAndre Oppermann * is new data available to be sent. 26505628dd08SAndre Oppermann * Otherwise we would send pure ACKs. 26515628dd08SAndre Oppermann */ 26525628dd08SAndre Oppermann SOCKBUF_LOCK(&so->so_snd); 2653cfa6009eSGleb Smirnoff avail = sbavail(&so->so_snd) - 26545628dd08SAndre Oppermann (tp->snd_nxt - tp->snd_una); 26555628dd08SAndre Oppermann SOCKBUF_UNLOCK(&so->so_snd); 26565628dd08SAndre Oppermann if (avail > 0) 265755bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 265848d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 26590c39d38dSGleb Smirnoff if (sent > maxseg) { 266089c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 266189c02376SJeffrey Hsu tp->snd_limited == 0) || 26620c39d38dSGleb Smirnoff (sent == maxseg + 1 && 266389c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 2664302ce8d6SAndre Oppermann ("%s: sent too much", 2665302ce8d6SAndre Oppermann __func__)); 266648d2549cSJeffrey Hsu tp->snd_limited = 2; 266748d2549cSJeffrey Hsu } else if (sent > 0) 266848d2549cSJeffrey Hsu ++tp->snd_limited; 2669582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 2670582a954bSJeffrey Hsu goto drop; 2671df8bae1dSRodney W. Grimes } 2672021eaf79SHiren Panchasara } 2673df8bae1dSRodney W. Grimes break; 2674021eaf79SHiren Panchasara } else { 2675021eaf79SHiren Panchasara /* 2676021eaf79SHiren Panchasara * This ack is advancing the left edge, reset the 2677021eaf79SHiren Panchasara * counter. 2678021eaf79SHiren Panchasara */ 2679021eaf79SHiren Panchasara tp->t_dupacks = 0; 2680021eaf79SHiren Panchasara /* 2681021eaf79SHiren Panchasara * If this ack also has new SACK info, increment the 2682021eaf79SHiren Panchasara * counter as per rfc6675. 2683021eaf79SHiren Panchasara */ 2684021eaf79SHiren Panchasara if ((tp->t_flags & TF_SACK_PERMIT) && sack_changed) 2685021eaf79SHiren Panchasara tp->t_dupacks++; 2686df8bae1dSRodney W. Grimes } 2687cb942153SJeffrey Hsu 2688302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2689302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 2690cb942153SJeffrey Hsu 2691df8bae1dSRodney W. Grimes /* 2692df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2693df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2694df8bae1dSRodney W. Grimes */ 2695dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2696cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 26973529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 26986d90faf3SPaul Saab tcp_sack_partialack(tp, th); 26996d90faf3SPaul Saab else 2700c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2701dbc42409SLawrence Stewart } else 2702dbc42409SLawrence Stewart cc_post_recovery(tp, th); 270346f58482SJonathan Lemon } 2704a0292f23SGarrett Wollman /* 2705a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2706a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2707a0292f23SGarrett Wollman */ 2708a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2709a0292f23SGarrett Wollman /* 2710a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2711a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 271207e43e10SAndras Olah * synchronized). Go to non-starred state, 271307e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 271407e43e10SAndras Olah * we can do window scaling. 2715a0292f23SGarrett Wollman */ 2716a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2717a0292f23SGarrett Wollman tp->snd_una++; 271807e43e10SAndras Olah /* Do window scaling? */ 271907e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 272007e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 272107e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2722464fcfbcSAndre Oppermann /* Send window already scaled. */ 272307e43e10SAndras Olah } 2724a0292f23SGarrett Wollman } 2725a0292f23SGarrett Wollman 2726a0292f23SGarrett Wollman process_ACK: 27278501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 27287cfc6904SRobert Watson 2729dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 2730b8c2cd15SJonathan T. Looney KASSERT(acked >= 0, ("%s: acked unexepectedly negative " 2731b8c2cd15SJonathan T. Looney "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__, 2732b8c2cd15SJonathan T. Looney tp->snd_una, th->th_ack, tp, m)); 27334b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvackpack, nsegs); 273478b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 2735df8bae1dSRodney W. Grimes 2736df8bae1dSRodney W. Grimes /* 27379b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 27389b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 27399b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 27409b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 27419b8b58e0SJonathan Lemon * we left off. 27429b8b58e0SJonathan Lemon */ 274310d20c84SMatt Macy if (tp->t_rxtshift == 1 && 274410d20c84SMatt Macy tp->t_flags & TF_PREVVALID && 274510d20c84SMatt Macy tp->t_badrxtwin && 274610d20c84SMatt Macy SEQ_LT(to.to_tsecr, tp->t_badrxtwin)) 2747dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 27489b8b58e0SJonathan Lemon 27499b8b58e0SJonathan Lemon /* 2750df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2751df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2752df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2753df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2754df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2755df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2756df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2757fa55172bSMatthew Dillon * 2758fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2759fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2760fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2761fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2762df8bae1dSRodney W. Grimes */ 2763d8951c8aSBjoern A. Zeeb if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) { 27643ac12506SJonathan T. Looney uint32_t t; 2765d8951c8aSBjoern A. Zeeb 2766d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 2767d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 2768d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 2769d8951c8aSBjoern A. Zeeb tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1); 2770fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2771eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2772eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 27739b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2774fa55172bSMatthew Dillon } 2775df8bae1dSRodney W. Grimes 2776df8bae1dSRodney W. Grimes /* 2777df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2778df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2779df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2780df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2781df8bae1dSRodney W. Grimes */ 2782fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 2783b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2784df8bae1dSRodney W. Grimes needoutput = 1; 2785b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2786b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2787a0292f23SGarrett Wollman 2788a0292f23SGarrett Wollman /* 2789a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2790a0292f23SGarrett Wollman * skip rest of ACK processing. 2791a0292f23SGarrett Wollman */ 2792a0292f23SGarrett Wollman if (acked == 0) 2793a0292f23SGarrett Wollman goto step6; 2794a0292f23SGarrett Wollman 2795df8bae1dSRodney W. Grimes /* 2796dbc42409SLawrence Stewart * Let the congestion control algorithm update congestion 2797dbc42409SLawrence Stewart * control related information. This typically means increasing 2798dbc42409SLawrence Stewart * the congestion window. 2799df8bae1dSRodney W. Grimes */ 28004b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, CC_ACK); 2801dbc42409SLawrence Stewart 28025905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2803cfa6009eSGleb Smirnoff if (acked > sbavail(&so->so_snd)) { 2804b8c2cd15SJonathan T. Looney if (tp->snd_wnd >= sbavail(&so->so_snd)) 2805cfa6009eSGleb Smirnoff tp->snd_wnd -= sbavail(&so->so_snd); 2806b8c2cd15SJonathan T. Looney else 2807b8c2cd15SJonathan T. Looney tp->snd_wnd = 0; 2808c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, 2809cfa6009eSGleb Smirnoff (int)sbavail(&so->so_snd)); 2810df8bae1dSRodney W. Grimes ourfinisacked = 1; 2811df8bae1dSRodney W. Grimes } else { 2812c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, acked); 28133ac12506SJonathan T. Looney if (tp->snd_wnd >= (uint32_t) acked) 281460ee3bb2SAndre Oppermann tp->snd_wnd -= acked; 2815b8c2cd15SJonathan T. Looney else 2816b8c2cd15SJonathan T. Looney tp->snd_wnd = 0; 2817df8bae1dSRodney W. Grimes ourfinisacked = 0; 2818df8bae1dSRodney W. Grimes } 2819564aab1fSAndre Oppermann /* NB: sowwakeup_locked() does an implicit unlock. */ 28201e4d7da7SRobert Watson sowwakeup_locked(so); 2821c11a15bfSGleb Smirnoff m_freem(mfree); 2822e8949f74SAndre Oppermann /* Detect una wraparound. */ 2823dbc42409SLawrence Stewart if (!IN_RECOVERY(tp->t_flags) && 28249d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 28259d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 28269d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 2827dbc42409SLawrence Stewart /* XXXLAS: Can this be moved up into cc_post_recovery? */ 2828dbc42409SLawrence Stewart if (IN_RECOVERY(tp->t_flags) && 282924cb0f22SLawrence Stewart SEQ_GEQ(th->th_ack, tp->snd_recover)) { 2830dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 283124cb0f22SLawrence Stewart } 2832fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 28333529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 28346d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 28356d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 28366d90faf3SPaul Saab } 2837df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2838df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2839df8bae1dSRodney W. Grimes 2840df8bae1dSRodney W. Grimes switch (tp->t_state) { 2841df8bae1dSRodney W. Grimes 2842df8bae1dSRodney W. Grimes /* 2843df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2844df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2845df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2846df8bae1dSRodney W. Grimes */ 2847df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2848df8bae1dSRodney W. Grimes if (ourfinisacked) { 2849df8bae1dSRodney W. Grimes /* 2850df8bae1dSRodney W. Grimes * If we can't receive any more 2851df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2852df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2853df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2854df8bae1dSRodney W. Grimes * we'll hang forever. 2855e8949f74SAndre Oppermann * 2856e8949f74SAndre Oppermann * XXXjl: 2857340c35deSJonathan Lemon * we should release the tp also, and use a 2858340c35deSJonathan Lemon * compressed state. 2859340c35deSJonathan Lemon */ 2860c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 286103e49181SSeigo Tanimura soisdisconnected(so); 28629077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 28639077f387SGleb Smirnoff (tcp_fast_finwait2_recycle ? 28649077f387SGleb Smirnoff tcp_finwait2_timeout : 28659077f387SGleb Smirnoff TP_MAXIDLE(tp))); 28664cc20ab1SSeigo Tanimura } 286757f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_2); 2868df8bae1dSRodney W. Grimes } 2869df8bae1dSRodney W. Grimes break; 2870df8bae1dSRodney W. Grimes 2871df8bae1dSRodney W. Grimes /* 2872df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2873df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2874df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2875df8bae1dSRodney W. Grimes * the segment. 2876df8bae1dSRodney W. Grimes */ 2877df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2878df8bae1dSRodney W. Grimes if (ourfinisacked) { 2879ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 288011a20fb8SJeffrey Hsu tcp_twstart(tp); 2881340c35deSJonathan Lemon m_freem(m); 2882679d9708SAndre Oppermann return; 2883df8bae1dSRodney W. Grimes } 2884df8bae1dSRodney W. Grimes break; 2885df8bae1dSRodney W. Grimes 2886df8bae1dSRodney W. Grimes /* 2887df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2888df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2889df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2890df8bae1dSRodney W. Grimes * enter the closed state and return. 2891df8bae1dSRodney W. Grimes */ 2892df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2893df8bae1dSRodney W. Grimes if (ourfinisacked) { 2894ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2895df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2896df8bae1dSRodney W. Grimes goto drop; 2897df8bae1dSRodney W. Grimes } 2898df8bae1dSRodney W. Grimes break; 2899df8bae1dSRodney W. Grimes } 2900df8bae1dSRodney W. Grimes } 2901df8bae1dSRodney W. Grimes 2902df8bae1dSRodney W. Grimes step6: 29038501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 29047cfc6904SRobert Watson 2905df8bae1dSRodney W. Grimes /* 290660ee3bb2SAndre Oppermann * Update window information. 290760ee3bb2SAndre Oppermann * Don't look at window if no ACK: TAC's send garbage on first SYN. 2908df8bae1dSRodney W. Grimes */ 290960ee3bb2SAndre Oppermann if ((thflags & TH_ACK) && 291060ee3bb2SAndre Oppermann (SEQ_LT(tp->snd_wl1, th->th_seq) || 291160ee3bb2SAndre Oppermann (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 291260ee3bb2SAndre Oppermann (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 291360ee3bb2SAndre Oppermann /* keep track of pure window updates */ 291460ee3bb2SAndre Oppermann if (tlen == 0 && 291560ee3bb2SAndre Oppermann tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 291678b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinupd); 2917df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 291860ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 291960ee3bb2SAndre Oppermann tp->snd_wl2 = th->th_ack; 2920df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2921df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 292260ee3bb2SAndre Oppermann needoutput = 1; 2923df8bae1dSRodney W. Grimes } 2924df8bae1dSRodney W. Grimes 2925df8bae1dSRodney W. Grimes /* 2926df8bae1dSRodney W. Grimes * Process segments with URG. 2927df8bae1dSRodney W. Grimes */ 2928fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2929df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2930df8bae1dSRodney W. Grimes /* 2931df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2932df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2933df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2934df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2935df8bae1dSRodney W. Grimes */ 293618ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2937cfa6009eSGleb Smirnoff if (th->th_urp + sbavail(&so->so_rcv) > sb_max) { 2938fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2939fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 294018ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2941df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2942df8bae1dSRodney W. Grimes } 2943df8bae1dSRodney W. Grimes /* 2944df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2945df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2946df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2947df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2948df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2949df8bae1dSRodney W. Grimes * 2950df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2951df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2952df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2953df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2954df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2955df8bae1dSRodney W. Grimes * spec states (in one of two places). 2956df8bae1dSRodney W. Grimes */ 2957fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2958fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2959cfa6009eSGleb Smirnoff so->so_oobmark = sbavail(&so->so_rcv) + 2960df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2961927c5ceaSRobert Watson if (so->so_oobmark == 0) 2962c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 2963df8bae1dSRodney W. Grimes sohasoutofband(so); 2964df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2965df8bae1dSRodney W. Grimes } 296618ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2967df8bae1dSRodney W. Grimes /* 2968df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2969df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2970df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2971df8bae1dSRodney W. Grimes * data may creep in... ick. 2972df8bae1dSRodney W. Grimes */ 29733ac12506SJonathan T. Looney if (th->th_urp <= (uint32_t)tlen && 297430613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 297530613f56SJeffrey Hsu /* hdr drop is delayed */ 297630613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 297730613f56SJeffrey Hsu } 2978c068736aSJeffrey Hsu } else { 2979df8bae1dSRodney W. Grimes /* 2980df8bae1dSRodney W. Grimes * If no out of band data is expected, 2981df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2982df8bae1dSRodney W. Grimes * with the receive window. 2983df8bae1dSRodney W. Grimes */ 2984df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2985df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2986c068736aSJeffrey Hsu } 2987df8bae1dSRodney W. Grimes dodata: /* XXX */ 29888501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 29897cfc6904SRobert Watson 2990df8bae1dSRodney W. Grimes /* 2991df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2992df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2993df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2994df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2995df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2996df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2997df8bae1dSRodney W. Grimes */ 2998281a0fd4SPatrick Kelsey tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) && 299968bd7ed1SJonathan T. Looney IS_FASTOPEN(tp->t_flags)); 3000281a0fd4SPatrick Kelsey if ((tlen || (thflags & TH_FIN) || tfo_syn) && 3001df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 300269e03620SPaul Saab tcp_seq save_start = th->th_seq; 30035acfd95cSMichael Tuexen tcp_seq save_rnxt = tp->rcv_nxt; 30045acfd95cSMichael Tuexen int save_tlen = tlen; 3005fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 3006e4b64281SJesper Skriver /* 3007c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 3008c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 3009c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 3010c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 3011c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 3012c068736aSJeffrey Hsu * and removal from the queue and repetition of various 3013c068736aSJeffrey Hsu * conversions. 3014c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 3015c068736aSJeffrey Hsu * immediately when segments are out of order (so 3016c068736aSJeffrey Hsu * fast retransmit can work). 3017e4b64281SJesper Skriver */ 30184741bfcbSPatrick Kelsey if (th->th_seq == tp->rcv_nxt && 3019c28440dbSRandall Stewart SEGQ_EMPTY(tp) && 3020281a0fd4SPatrick Kelsey (TCPS_HAVEESTABLISHED(tp->t_state) || 3021281a0fd4SPatrick Kelsey tfo_syn)) { 3022281a0fd4SPatrick Kelsey if (DELAY_ACK(tp, tlen) || tfo_syn) 30233bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 3024e4b64281SJesper Skriver else 3025e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 3026e4b64281SJesper Skriver tp->rcv_nxt += tlen; 3027e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 302878b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 302978b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 30301e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3031c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 3032c1c36a2cSMike Silbersack m_freem(m); 3033c1c36a2cSMike Silbersack else 3034651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 3035e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 30361e4d7da7SRobert Watson sorwakeup_locked(so); 3037e4b64281SJesper Skriver } else { 3038e8949f74SAndre Oppermann /* 3039e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 3040e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 3041e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 3042e8949f74SAndre Oppermann * when trimming from the head. 3043e8949f74SAndre Oppermann */ 30445acfd95cSMichael Tuexen tcp_seq temp = save_start; 30455acfd95cSMichael Tuexen thflags = tcp_reass(tp, th, &temp, &tlen, m); 3046e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 3047e4b64281SJesper Skriver } 30485acfd95cSMichael Tuexen if (tp->t_flags & TF_SACK_PERMIT) { 30495acfd95cSMichael Tuexen if (((tlen == 0) && (save_tlen > 0) && 30505acfd95cSMichael Tuexen (SEQ_LT(save_start, save_rnxt)))) { 3051b5a154d8SMichael Tuexen /* 3052b5a154d8SMichael Tuexen * DSACK actually handled in the fastpath 3053b5a154d8SMichael Tuexen * above. 3054b5a154d8SMichael Tuexen */ 30555acfd95cSMichael Tuexen tcp_update_sack_list(tp, save_start, save_start + save_tlen); 30565acfd95cSMichael Tuexen } else 30575acfd95cSMichael Tuexen if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) { 3058b5a154d8SMichael Tuexen /* 3059b5a154d8SMichael Tuexen * Cleaning sackblks by using zero length 3060b5a154d8SMichael Tuexen * update. 3061b5a154d8SMichael Tuexen */ 30625acfd95cSMichael Tuexen tcp_update_sack_list(tp, save_start, save_start); 30635acfd95cSMichael Tuexen } else 30645acfd95cSMichael Tuexen if ((tlen > 0) && (tlen >= save_tlen)) { 3065b5a154d8SMichael Tuexen /* Update of sackblks. */ 30665acfd95cSMichael Tuexen tcp_update_sack_list(tp, save_start, save_start + save_tlen); 30675acfd95cSMichael Tuexen } else 30685acfd95cSMichael Tuexen if (tlen > 0) { 3069f194524fSAndre Oppermann tcp_update_sack_list(tp, save_start, save_start+tlen); 30705acfd95cSMichael Tuexen } 30715acfd95cSMichael Tuexen } 3072302ce8d6SAndre Oppermann #if 0 3073df8bae1dSRodney W. Grimes /* 3074df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 3075df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 3076df8bae1dSRodney W. Grimes * buffer size. 3077302ce8d6SAndre Oppermann * XXX: Unused. 3078df8bae1dSRodney W. Grimes */ 3079f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) 3080df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 3081f701e30dSJohn Baldwin else 3082f701e30dSJohn Baldwin len = so->so_rcv.sb_hiwat; 3083302ce8d6SAndre Oppermann #endif 3084df8bae1dSRodney W. Grimes } else { 3085df8bae1dSRodney W. Grimes m_freem(m); 3086fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 3087df8bae1dSRodney W. Grimes } 3088df8bae1dSRodney W. Grimes 3089df8bae1dSRodney W. Grimes /* 3090df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 3091df8bae1dSRodney W. Grimes * that the connection is closing. 3092df8bae1dSRodney W. Grimes */ 3093fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 3094df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 3095df8bae1dSRodney W. Grimes socantrcvmore(so); 3096a0292f23SGarrett Wollman /* 3097a0292f23SGarrett Wollman * If connection is half-synchronized 3098d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 3099a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 3100a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 3101a0292f23SGarrett Wollman * more input can be expected, send ACK now. 3102a0292f23SGarrett Wollman */ 31033bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 31043bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 3105a0292f23SGarrett Wollman else 3106df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 3107df8bae1dSRodney W. Grimes tp->rcv_nxt++; 3108df8bae1dSRodney W. Grimes } 3109df8bae1dSRodney W. Grimes switch (tp->t_state) { 3110df8bae1dSRodney W. Grimes 3111df8bae1dSRodney W. Grimes /* 3112df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 3113df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 3114df8bae1dSRodney W. Grimes */ 3115df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 31169b8b58e0SJonathan Lemon tp->t_starttime = ticks; 31179b8b58e0SJonathan Lemon /* FALLTHROUGH */ 3118df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 311957f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSE_WAIT); 3120df8bae1dSRodney W. Grimes break; 3121df8bae1dSRodney W. Grimes 3122df8bae1dSRodney W. Grimes /* 3123df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 3124df8bae1dSRodney W. Grimes * enter the CLOSING state. 3125df8bae1dSRodney W. Grimes */ 3126df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 312757f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSING); 3128df8bae1dSRodney W. Grimes break; 3129df8bae1dSRodney W. Grimes 3130df8bae1dSRodney W. Grimes /* 3131df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 3132df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 3133df8bae1dSRodney W. Grimes * standard timers. 3134df8bae1dSRodney W. Grimes */ 3135df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 3136ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 3137252ca428SRobert Watson 3138340c35deSJonathan Lemon tcp_twstart(tp); 3139679d9708SAndre Oppermann return; 3140df8bae1dSRodney W. Grimes } 3141df8bae1dSRodney W. Grimes } 3142610ee2f9SDavid Greenman #ifdef TCPDEBUG 31434cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3144fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 3145fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3146610ee2f9SDavid Greenman #endif 31472b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 3148df8bae1dSRodney W. Grimes 3149df8bae1dSRodney W. Grimes /* 3150df8bae1dSRodney W. Grimes * Return any desired output. 3151df8bae1dSRodney W. Grimes */ 3152df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 315355bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 31547792ea27SJeffrey Hsu 3155a14c749fSJonathan Lemon check_delack: 31568501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 3157252ca428SRobert Watson 3158a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 31593bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 3160b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 31613bfd6421SJonathan Lemon } 31628501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3163679d9708SAndre Oppermann return; 3164df8bae1dSRodney W. Grimes 3165df8bae1dSRodney W. Grimes dropafterack: 3166df8bae1dSRodney W. Grimes /* 3167df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 3168df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 316980ab7c0eSGarrett Wollman * 317080ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 317180ab7c0eSGarrett Wollman * paths to this code happen after packets containing 317280ab7c0eSGarrett Wollman * RST have been dropped. 317380ab7c0eSGarrett Wollman * 317480ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 317580ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 317680ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 317780ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 317880ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 317980ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 3180df8bae1dSRodney W. Grimes */ 3181fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 3182fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 3183a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 3184a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 3185a57815efSBosko Milekic goto dropwithreset; 3186a57815efSBosko Milekic } 3187a0292f23SGarrett Wollman #ifdef TCPDEBUG 31884cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3189fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3190fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3191a0292f23SGarrett Wollman #endif 31922b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 3193df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 319455bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 31958501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3196d6915262SRobert Watson m_freem(m); 3197679d9708SAndre Oppermann return; 3198df8bae1dSRodney W. Grimes 3199df8bae1dSRodney W. Grimes dropwithreset: 3200a0ca0871SRobert Watson if (tp != NULL) { 3201302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 32028501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3203dd8ac7f9SRobert Watson } else 3204a0ca0871SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 3205679d9708SAndre Oppermann return; 3206df8bae1dSRodney W. Grimes 3207df8bae1dSRodney W. Grimes drop: 3208df8bae1dSRodney W. Grimes /* 3209df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 3210df8bae1dSRodney W. Grimes */ 3211610ee2f9SDavid Greenman #ifdef TCPDEBUG 32121c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 3213fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3214fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3215a0292f23SGarrett Wollman #endif 32162b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 32171c53f806SRobert Watson if (tp != NULL) 32188501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3219d6915262SRobert Watson m_freem(m); 3220302ce8d6SAndre Oppermann } 3221302ce8d6SAndre Oppermann 3222302ce8d6SAndre Oppermann /* 32230ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 32240ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 32250ca3f933SAndre Oppermann * tp may be NULL. 3226302ce8d6SAndre Oppermann */ 322755bceb1eSRandall Stewart void 3228302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 3229302ce8d6SAndre Oppermann int tlen, int rstreason) 3230302ce8d6SAndre Oppermann { 3231b287c6c7SBjoern A. Zeeb #ifdef INET 3232302ce8d6SAndre Oppermann struct ip *ip; 3233b287c6c7SBjoern A. Zeeb #endif 3234302ce8d6SAndre Oppermann #ifdef INET6 3235302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 3236302ce8d6SAndre Oppermann #endif 32377a3244ccSRobert Watson 32387a3244ccSRobert Watson if (tp != NULL) { 32398501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 32407a3244ccSRobert Watson } 32417a3244ccSRobert Watson 32420ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 3243302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 3244302ce8d6SAndre Oppermann goto drop; 3245302ce8d6SAndre Oppermann #ifdef INET6 3246302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 3247302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 3248302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3249302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3250302ce8d6SAndre Oppermann goto drop; 3251302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 3252b287c6c7SBjoern A. Zeeb } 3253302ce8d6SAndre Oppermann #endif 3254b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3255b287c6c7SBjoern A. Zeeb else 3256b287c6c7SBjoern A. Zeeb #endif 3257b287c6c7SBjoern A. Zeeb #ifdef INET 3258302ce8d6SAndre Oppermann { 3259302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 3260302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3261302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3262302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3263302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3264302ce8d6SAndre Oppermann goto drop; 3265302ce8d6SAndre Oppermann } 3266b287c6c7SBjoern A. Zeeb #endif 3267302ce8d6SAndre Oppermann 3268302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 3269302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 3270302ce8d6SAndre Oppermann goto drop; 3271302ce8d6SAndre Oppermann 3272302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 3273302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 3274302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 3275302ce8d6SAndre Oppermann th->th_ack, TH_RST); 3276302ce8d6SAndre Oppermann } else { 3277302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 3278302ce8d6SAndre Oppermann tlen++; 32794ddd5aadSMichael Tuexen if (th->th_flags & TH_FIN) 32804ddd5aadSMichael Tuexen tlen++; 3281302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 3282302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 3283302ce8d6SAndre Oppermann } 3284302ce8d6SAndre Oppermann return; 3285302ce8d6SAndre Oppermann drop: 3286302ce8d6SAndre Oppermann m_freem(m); 3287df8bae1dSRodney W. Grimes } 3288df8bae1dSRodney W. Grimes 3289be2ac88cSJonathan Lemon /* 3290be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 3291be2ac88cSJonathan Lemon */ 329255bceb1eSRandall Stewart void 3293ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 3294df8bae1dSRodney W. Grimes { 3295df8bae1dSRodney W. Grimes int opt, optlen; 3296df8bae1dSRodney W. Grimes 3297be2ac88cSJonathan Lemon to->to_flags = 0; 3298df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 3299df8bae1dSRodney W. Grimes opt = cp[0]; 3300df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 3301df8bae1dSRodney W. Grimes break; 3302df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 3303df8bae1dSRodney W. Grimes optlen = 1; 3304df8bae1dSRodney W. Grimes else { 3305b474779fSJun-ichiro itojun Hagino if (cnt < 2) 3306b474779fSJun-ichiro itojun Hagino break; 3307df8bae1dSRodney W. Grimes optlen = cp[1]; 3308b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 3309df8bae1dSRodney W. Grimes break; 3310df8bae1dSRodney W. Grimes } 3311df8bae1dSRodney W. Grimes switch (opt) { 3312df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 3313df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 3314df8bae1dSRodney W. Grimes continue; 3315f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3316df8bae1dSRodney W. Grimes continue; 3317be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 3318be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 3319be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 3320fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 3321df8bae1dSRodney W. Grimes break; 3322df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 3323df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 3324df8bae1dSRodney W. Grimes continue; 3325f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3326df8bae1dSRodney W. Grimes continue; 3327be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 332802a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 3329df8bae1dSRodney W. Grimes break; 3330df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 3331df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 3332df8bae1dSRodney W. Grimes continue; 3333be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 3334a0292f23SGarrett Wollman bcopy((char *)cp + 2, 3335a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 3336fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 3337a0292f23SGarrett Wollman bcopy((char *)cp + 6, 3338a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 3339fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 3340df8bae1dSRodney W. Grimes break; 33411cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 3342fcf59617SAndrey V. Elsukov /* 3343fcf59617SAndrey V. Elsukov * In order to reply to a host which has set the 3344fcf59617SAndrey V. Elsukov * TCP_SIGNATURE option in its initial SYN, we have 3345fcf59617SAndrey V. Elsukov * to record the fact that the option was observed 3346fcf59617SAndrey V. Elsukov * here for the syncache code to perform the correct 3347fcf59617SAndrey V. Elsukov * response. 3348fcf59617SAndrey V. Elsukov */ 33491cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 33501cfd4b53SBruce M Simpson continue; 3351df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 3352df47e437SAndre Oppermann to->to_signature = cp + 2; 33531cfd4b53SBruce M Simpson break; 33546d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 3355f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 33566d90faf3SPaul Saab continue; 3357f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3358f72167f4SAndre Oppermann continue; 3359603724d3SBjoern A. Zeeb if (!V_tcp_do_sack) 3360f72167f4SAndre Oppermann continue; 3361fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 33626d90faf3SPaul Saab break; 33636d90faf3SPaul Saab case TCPOPT_SACK: 33645a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 33656d90faf3SPaul Saab continue; 3366b728e902SAndre Oppermann if (flags & TO_SYN) 3367b728e902SAndre Oppermann continue; 3368fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 33695a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 33705a53ca16SPaul Saab to->to_sacks = cp + 2; 337178b50714SRobert Watson TCPSTAT_INC(tcps_sack_rcv_blocks); 33726d90faf3SPaul Saab break; 3373281a0fd4SPatrick Kelsey case TCPOPT_FAST_OPEN: 3374c560df6fSPatrick Kelsey /* 3375c560df6fSPatrick Kelsey * Cookie length validation is performed by the 3376c560df6fSPatrick Kelsey * server side cookie checking code or the client 3377c560df6fSPatrick Kelsey * side cookie cache update code. 3378c560df6fSPatrick Kelsey */ 3379281a0fd4SPatrick Kelsey if (!(flags & TO_SYN)) 3380281a0fd4SPatrick Kelsey continue; 3381c560df6fSPatrick Kelsey if (!V_tcp_fastopen_client_enable && 3382c560df6fSPatrick Kelsey !V_tcp_fastopen_server_enable) 3383281a0fd4SPatrick Kelsey continue; 3384281a0fd4SPatrick Kelsey to->to_flags |= TOF_FASTOPEN; 3385281a0fd4SPatrick Kelsey to->to_tfo_len = optlen - 2; 3386281a0fd4SPatrick Kelsey to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL; 3387281a0fd4SPatrick Kelsey break; 3388be2ac88cSJonathan Lemon default: 3389be2ac88cSJonathan Lemon continue; 3390df8bae1dSRodney W. Grimes } 3391df8bae1dSRodney W. Grimes } 3392df8bae1dSRodney W. Grimes } 3393df8bae1dSRodney W. Grimes 3394df8bae1dSRodney W. Grimes /* 3395df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 3396df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 3397df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 3398df8bae1dSRodney W. Grimes * sequencing purposes. 3399df8bae1dSRodney W. Grimes */ 340055bceb1eSRandall Stewart void 3401ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 3402ad3f9ab3SAndre Oppermann int off) 3403df8bae1dSRodney W. Grimes { 3404fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 3405df8bae1dSRodney W. Grimes 3406df8bae1dSRodney W. Grimes while (cnt >= 0) { 3407df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 3408df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 3409df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 3410df8bae1dSRodney W. Grimes 34118501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 34127a3244ccSRobert Watson 3413df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 3414df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 3415df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 3416df8bae1dSRodney W. Grimes m->m_len--; 341769a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 341869a34685SYoshinobu Inoue m->m_pkthdr.len--; 3419df8bae1dSRodney W. Grimes return; 3420df8bae1dSRodney W. Grimes } 3421df8bae1dSRodney W. Grimes cnt -= m->m_len; 3422df8bae1dSRodney W. Grimes m = m->m_next; 34234dfdffe9SAndre Oppermann if (m == NULL) 3424df8bae1dSRodney W. Grimes break; 3425df8bae1dSRodney W. Grimes } 3426df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 3427df8bae1dSRodney W. Grimes } 3428df8bae1dSRodney W. Grimes 3429df8bae1dSRodney W. Grimes /* 3430df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 3431df8bae1dSRodney W. Grimes * and update averages and current timeout. 3432df8bae1dSRodney W. Grimes */ 343355bceb1eSRandall Stewart void 3434ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 3435df8bae1dSRodney W. Grimes { 3436ad3f9ab3SAndre Oppermann int delta; 3437233e8c18SGarrett Wollman 34388501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 34392be3bf22SRobert Watson 344078b50714SRobert Watson TCPSTAT_INC(tcps_rttupdated); 3441233e8c18SGarrett Wollman tp->t_rttupdated++; 34425ede40dcSRyan Stone if ((tp->t_srtt != 0) && (tp->t_rxtshift <= TCP_RTT_INVALIDATE)) { 3443233e8c18SGarrett Wollman /* 3444233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 3445233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 3446233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 3447233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3448233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 3449233e8c18SGarrett Wollman */ 3450233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3451233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3452233e8c18SGarrett Wollman 3453233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 3454233e8c18SGarrett Wollman tp->t_srtt = 1; 3455233e8c18SGarrett Wollman 3456233e8c18SGarrett Wollman /* 3457233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 3458233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 3459233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 3460233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 3461233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 3462233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 3463233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3464233e8c18SGarrett Wollman * rfc793's wired-in beta. 3465233e8c18SGarrett Wollman */ 3466233e8c18SGarrett Wollman if (delta < 0) 3467233e8c18SGarrett Wollman delta = -delta; 3468233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3469233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 3470233e8c18SGarrett Wollman tp->t_rttvar = 1; 34711fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 34721fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3473233e8c18SGarrett Wollman } else { 3474233e8c18SGarrett Wollman /* 3475233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 3476233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 3477233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 3478233e8c18SGarrett Wollman */ 3479233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 3480233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 34811fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3482233e8c18SGarrett Wollman } 34839b8b58e0SJonathan Lemon tp->t_rtttime = 0; 3484df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 3485df8bae1dSRodney W. Grimes 3486df8bae1dSRodney W. Grimes /* 3487df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 3488df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 3489df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 3490df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 3491df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 3492df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 3493df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 3494df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 3495df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 3496df8bae1dSRodney W. Grimes */ 3497233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 34989e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3499df8bae1dSRodney W. Grimes 3500df8bae1dSRodney W. Grimes /* 3501df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 3502df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 3503df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 3504df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 3505df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 3506df8bae1dSRodney W. Grimes */ 3507df8bae1dSRodney W. Grimes tp->t_softerror = 0; 3508df8bae1dSRodney W. Grimes } 3509df8bae1dSRodney W. Grimes 3510df8bae1dSRodney W. Grimes /* 3511df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 3512df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 35134faaea55SAndre Oppermann * If none, use an mss that can be handled on the outgoing interface 35144faaea55SAndre Oppermann * without forcing IP to fragment. If no route is found, route has no mtu, 3515df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 3516df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 3517df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 3518df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 3519df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 3520df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 3521df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 3522a0292f23SGarrett Wollman * 35230c39d38dSGleb Smirnoff * NOTE that resulting t_maxseg doesn't include space for TCP options or 35240c39d38dSGleb Smirnoff * IP options, e.g. IPSEC data, since length of this data may vary, and 35250c39d38dSGleb Smirnoff * thus it is calculated for every segment separately in tcp_output(). 3526a0292f23SGarrett Wollman * 352797d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 3528ef341ee1SGleb Smirnoff * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS 3529ef341ee1SGleb Smirnoff * settings are handled in tcp_mssopt(). 3530df8bae1dSRodney W. Grimes */ 3531a0292f23SGarrett Wollman void 3532ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer, 35333c914c54SAndre Oppermann struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap) 3534df8bae1dSRodney W. Grimes { 3535b287c6c7SBjoern A. Zeeb int mss = 0; 35363ac12506SJonathan T. Looney uint32_t maxmtu = 0; 3537c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 353897d8d152SAndre Oppermann struct hc_metrics_lite metrics; 3539fb59c426SYoshinobu Inoue #ifdef INET6 3540c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3541c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 3542c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3543c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 3544c068736aSJeffrey Hsu #else 3545c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 3546fb59c426SYoshinobu Inoue #endif 3547df8bae1dSRodney W. Grimes 35488501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 35497a3244ccSRobert Watson 3550ef341ee1SGleb Smirnoff if (mtuoffer != -1) { 3551ef341ee1SGleb Smirnoff KASSERT(offer == -1, ("%s: conflict", __func__)); 3552ef341ee1SGleb Smirnoff offer = mtuoffer - min_protoh; 3553ef341ee1SGleb Smirnoff } 3554ef341ee1SGleb Smirnoff 3555e8949f74SAndre Oppermann /* Initialize. */ 355697d8d152SAndre Oppermann #ifdef INET6 355797d8d152SAndre Oppermann if (isipv6) { 35583c914c54SAndre Oppermann maxmtu = tcp_maxmtu6(&inp->inp_inc, cap); 35590c39d38dSGleb Smirnoff tp->t_maxseg = V_tcp_v6mssdflt; 3560b287c6c7SBjoern A. Zeeb } 356197d8d152SAndre Oppermann #endif 3562b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3563b287c6c7SBjoern A. Zeeb else 3564b287c6c7SBjoern A. Zeeb #endif 3565b287c6c7SBjoern A. Zeeb #ifdef INET 356697d8d152SAndre Oppermann { 35673c914c54SAndre Oppermann maxmtu = tcp_maxmtu(&inp->inp_inc, cap); 35680c39d38dSGleb Smirnoff tp->t_maxseg = V_tcp_mssdflt; 3569df8bae1dSRodney W. Grimes } 3570b287c6c7SBjoern A. Zeeb #endif 3571df8bae1dSRodney W. Grimes 357297d8d152SAndre Oppermann /* 3573e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 357497d8d152SAndre Oppermann */ 35756f01cac6SBjoern A. Zeeb if (maxmtu == 0) { 35766f01cac6SBjoern A. Zeeb /* 35778e5c87f4SBjoern A. Zeeb * In case we return early we need to initialize metrics 35786f01cac6SBjoern A. Zeeb * to a defined state as tcp_hc_get() would do for us 35796f01cac6SBjoern A. Zeeb * if there was no cache hit. 35806f01cac6SBjoern A. Zeeb */ 35816f01cac6SBjoern A. Zeeb if (metricptr != NULL) 35826f01cac6SBjoern A. Zeeb bzero(metricptr, sizeof(struct hc_metrics_lite)); 358397d8d152SAndre Oppermann return; 35846f01cac6SBjoern A. Zeeb } 358597d8d152SAndre Oppermann 3586e8949f74SAndre Oppermann /* What have we got? */ 358797d8d152SAndre Oppermann switch (offer) { 358897d8d152SAndre Oppermann case 0: 358997d8d152SAndre Oppermann /* 359097d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 3591c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 35920c39d38dSGleb Smirnoff * already assigned to t_maxseg above. 359397d8d152SAndre Oppermann */ 35940c39d38dSGleb Smirnoff offer = tp->t_maxseg; 359597d8d152SAndre Oppermann break; 359697d8d152SAndre Oppermann 359797d8d152SAndre Oppermann case -1: 3598a0292f23SGarrett Wollman /* 3599c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 3600a0292f23SGarrett Wollman */ 360197d8d152SAndre Oppermann /* FALLTHROUGH */ 360297d8d152SAndre Oppermann 360397d8d152SAndre Oppermann default: 3604a0292f23SGarrett Wollman /* 360553369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 360653369ac9SAndre Oppermann * to at least minmss. 360753369ac9SAndre Oppermann */ 3608603724d3SBjoern A. Zeeb offer = max(offer, V_tcp_minmss); 360997d8d152SAndre Oppermann } 3610a0292f23SGarrett Wollman 3611df8bae1dSRodney W. Grimes /* 3612e8949f74SAndre Oppermann * rmx information is now retrieved from tcp_hostcache. 3613df8bae1dSRodney W. Grimes */ 361497d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 36153cee92e0SBjoern A. Zeeb if (metricptr != NULL) 36163cee92e0SBjoern A. Zeeb bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 361797d8d152SAndre Oppermann 3618df8bae1dSRodney W. Grimes /* 3619cc412412SHiren Panchasara * If there's a discovered mtu in tcp hostcache, use it. 3620cc412412SHiren Panchasara * Else, use the link mtu. 3621df8bae1dSRodney W. Grimes */ 362297d8d152SAndre Oppermann if (metrics.rmx_mtu) 36232d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3624c068736aSJeffrey Hsu else { 362531b3783cSHajimu UMEMOTO #ifdef INET6 3626fb59c426SYoshinobu Inoue if (isipv6) { 362797d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3628603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 362997d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 3630603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_v6mssdflt); 3631b287c6c7SBjoern A. Zeeb } 3632b3399803SHajimu UMEMOTO #endif 3633b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3634b287c6c7SBjoern A. Zeeb else 3635b287c6c7SBjoern A. Zeeb #endif 3636b287c6c7SBjoern A. Zeeb #ifdef INET 363797d8d152SAndre Oppermann { 363897d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3639603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 364097d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 3641603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_mssdflt); 3642a0292f23SGarrett Wollman } 3643b287c6c7SBjoern A. Zeeb #endif 36443cee92e0SBjoern A. Zeeb /* 36453cee92e0SBjoern A. Zeeb * XXX - The above conditional (mss = maxmtu - min_protoh) 36463cee92e0SBjoern A. Zeeb * probably violates the TCP spec. 36473cee92e0SBjoern A. Zeeb * The problem is that, since we don't know the 36483cee92e0SBjoern A. Zeeb * other end's MSS, we are supposed to use a conservative 36493cee92e0SBjoern A. Zeeb * default. But, if we do that, then MTU discovery will 36503cee92e0SBjoern A. Zeeb * never actually take place, because the conservative 36513cee92e0SBjoern A. Zeeb * default is much less than the MTUs typically seen 36523cee92e0SBjoern A. Zeeb * on the Internet today. For the moment, we'll sweep 36533cee92e0SBjoern A. Zeeb * this under the carpet. 36543cee92e0SBjoern A. Zeeb * 36553cee92e0SBjoern A. Zeeb * The conservative default might not actually be a problem 36563cee92e0SBjoern A. Zeeb * if the only case this occurs is when sending an initial 36573cee92e0SBjoern A. Zeeb * SYN with options and data to a host we've never talked 36583cee92e0SBjoern A. Zeeb * to before. Then, they will reply with an MSS value which 36593cee92e0SBjoern A. Zeeb * will get recorded and the new parameters should get 36603cee92e0SBjoern A. Zeeb * recomputed. For Further Study. 36613cee92e0SBjoern A. Zeeb */ 366297d8d152SAndre Oppermann } 3663a0292f23SGarrett Wollman mss = min(mss, offer); 366497d8d152SAndre Oppermann 3665a0292f23SGarrett Wollman /* 36660c39d38dSGleb Smirnoff * Sanity check: make sure that maxseg will be large 36673cee92e0SBjoern A. Zeeb * enough to allow some data on segments even if the 36683cee92e0SBjoern A. Zeeb * all the option space is used (40bytes). Otherwise 36693cee92e0SBjoern A. Zeeb * funny things may happen in tcp_output. 36700c39d38dSGleb Smirnoff * 36710c39d38dSGleb Smirnoff * XXXGL: shouldn't we reserve space for IP/IPv6 options? 36723cee92e0SBjoern A. Zeeb */ 36733cee92e0SBjoern A. Zeeb mss = max(mss, 64); 36743cee92e0SBjoern A. Zeeb 367597d8d152SAndre Oppermann tp->t_maxseg = mss; 36763cee92e0SBjoern A. Zeeb } 36773cee92e0SBjoern A. Zeeb 36783cee92e0SBjoern A. Zeeb void 36793cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer) 36803cee92e0SBjoern A. Zeeb { 3681dbc42409SLawrence Stewart int mss; 36823ac12506SJonathan T. Looney uint32_t bufsize; 36833cee92e0SBjoern A. Zeeb struct inpcb *inp; 36843cee92e0SBjoern A. Zeeb struct socket *so; 36853cee92e0SBjoern A. Zeeb struct hc_metrics_lite metrics; 36863c914c54SAndre Oppermann struct tcp_ifcap cap; 3687dbc42409SLawrence Stewart 36883cee92e0SBjoern A. Zeeb KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 36893cee92e0SBjoern A. Zeeb 36903c914c54SAndre Oppermann bzero(&cap, sizeof(cap)); 36913c914c54SAndre Oppermann tcp_mss_update(tp, offer, -1, &metrics, &cap); 36923cee92e0SBjoern A. Zeeb 36933cee92e0SBjoern A. Zeeb mss = tp->t_maxseg; 36943cee92e0SBjoern A. Zeeb inp = tp->t_inpcb; 369597d8d152SAndre Oppermann 3696df8bae1dSRodney W. Grimes /* 369797d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 369897d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 369997d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 370097d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 370197d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3702df8bae1dSRodney W. Grimes */ 3703c3b02504SBjoern A. Zeeb so = inp->inp_socket; 37043f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 3705e233e2acSAndre Oppermann if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe) 370697d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 370797d8d152SAndre Oppermann else 3708df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3709df8bae1dSRodney W. Grimes if (bufsize < mss) 3710df8bae1dSRodney W. Grimes mss = bufsize; 3711df8bae1dSRodney W. Grimes else { 3712df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3713df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3714df8bae1dSRodney W. Grimes bufsize = sb_max; 371588c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 37163f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3717df8bae1dSRodney W. Grimes } 37183f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 3719784ce8faSHiren Panchasara /* 3720784ce8faSHiren Panchasara * Sanity check: make sure that maxseg will be large 3721784ce8faSHiren Panchasara * enough to allow some data on segments even if the 3722784ce8faSHiren Panchasara * all the option space is used (40bytes). Otherwise 3723784ce8faSHiren Panchasara * funny things may happen in tcp_output. 3724784ce8faSHiren Panchasara * 3725784ce8faSHiren Panchasara * XXXGL: shouldn't we reserve space for IP/IPv6 options? 3726784ce8faSHiren Panchasara */ 3727784ce8faSHiren Panchasara tp->t_maxseg = max(mss, 64); 3728df8bae1dSRodney W. Grimes 37293f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3730e233e2acSAndre Oppermann if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe) 373197d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 373297d8d152SAndre Oppermann else 3733df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3734df8bae1dSRodney W. Grimes if (bufsize > mss) { 3735df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3736df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3737df8bae1dSRodney W. Grimes bufsize = sb_max; 373888c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 37393f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3740df8bae1dSRodney W. Grimes } 37413f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 374291d6cfa6SBjoern A. Zeeb 374391d6cfa6SBjoern A. Zeeb /* Check the interface for TSO capabilities. */ 37443c914c54SAndre Oppermann if (cap.ifcap & CSUM_TSO) { 374591d6cfa6SBjoern A. Zeeb tp->t_flags |= TF_TSO; 37463c914c54SAndre Oppermann tp->t_tsomax = cap.tsomax; 37479fd573c3SHans Petter Selasky tp->t_tsomaxsegcount = cap.tsomaxsegcount; 37489fd573c3SHans Petter Selasky tp->t_tsomaxsegsize = cap.tsomaxsegsize; 37493c914c54SAndre Oppermann } 3750a0292f23SGarrett Wollman } 3751a0292f23SGarrett Wollman 3752a0292f23SGarrett Wollman /* 3753a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3754a0292f23SGarrett Wollman */ 3755a0292f23SGarrett Wollman int 3756ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 3757a0292f23SGarrett Wollman { 375897d8d152SAndre Oppermann int mss = 0; 37593ac12506SJonathan T. Looney uint32_t thcmtu = 0; 37603ac12506SJonathan T. Looney uint32_t maxmtu = 0; 376197d8d152SAndre Oppermann size_t min_protoh; 3762a0292f23SGarrett Wollman 376397d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3764a0292f23SGarrett Wollman 376531b3783cSHajimu UMEMOTO #ifdef INET6 3766dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3767603724d3SBjoern A. Zeeb mss = V_tcp_v6mssdflt; 3768233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 376997d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 3770b287c6c7SBjoern A. Zeeb } 377131b3783cSHajimu UMEMOTO #endif 3772b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3773b287c6c7SBjoern A. Zeeb else 3774b287c6c7SBjoern A. Zeeb #endif 3775b287c6c7SBjoern A. Zeeb #ifdef INET 377697d8d152SAndre Oppermann { 3777603724d3SBjoern A. Zeeb mss = V_tcp_mssdflt; 3778233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 377997d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 378097d8d152SAndre Oppermann } 3781b287c6c7SBjoern A. Zeeb #endif 37823a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET) 37833a9391deSBjoern A. Zeeb thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 37843a9391deSBjoern A. Zeeb #endif 37853a9391deSBjoern A. Zeeb 378697d8d152SAndre Oppermann if (maxmtu && thcmtu) 378797d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 378897d8d152SAndre Oppermann else if (maxmtu || thcmtu) 378997d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 379097d8d152SAndre Oppermann 379197d8d152SAndre Oppermann return (mss); 3792df8bae1dSRodney W. Grimes } 379346f58482SJonathan Lemon 379446f58482SJonathan Lemon 379546f58482SJonathan Lemon /* 3796c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3797c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3798c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3799c068736aSJeffrey Hsu * be started again. 380046f58482SJonathan Lemon */ 380155bceb1eSRandall Stewart void 3802ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 380346f58482SJonathan Lemon { 380446f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 38053ac12506SJonathan T. Looney uint32_t ocwnd = tp->snd_cwnd; 38060c39d38dSGleb Smirnoff u_int maxseg = tcp_maxseg(tp); 380746f58482SJonathan Lemon 38088501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 38097a3244ccSRobert Watson 3810b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 381146f58482SJonathan Lemon tp->t_rtttime = 0; 381246f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 38136b2a5f92SJayanth Vijayaraghavan /* 3814c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3815c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 38166b2a5f92SJayanth Vijayaraghavan */ 38170c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th); 3818a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 381955bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 382046f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 382146f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 382246f58482SJonathan Lemon tp->snd_nxt = onxt; 382346f58482SJonathan Lemon /* 382446f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 382546f58482SJonathan Lemon * not updated yet. 382646f58482SJonathan Lemon */ 3827dbc42409SLawrence Stewart if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th)) 3828dbc42409SLawrence Stewart tp->snd_cwnd -= BYTES_THIS_ACK(tp, th); 3829d7587117SPaul Saab else 3830d7587117SPaul Saab tp->snd_cwnd = 0; 38310c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 383246f58482SJonathan Lemon } 383312eeb81fSHiren Panchasara 383412eeb81fSHiren Panchasara int 383512eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp) 383612eeb81fSHiren Panchasara { 383712eeb81fSHiren Panchasara return (tp->snd_max - tp->snd_una + 383812eeb81fSHiren Panchasara tp->sackhint.sack_bytes_rexmit - 383912eeb81fSHiren Panchasara tp->sackhint.sacked_bytes); 384012eeb81fSHiren Panchasara } 38417dc90a1dSMichael Tuexen 38427dc90a1dSMichael Tuexen uint32_t 38437dc90a1dSMichael Tuexen tcp_compute_initwnd(uint32_t maxseg) 38447dc90a1dSMichael Tuexen { 38457dc90a1dSMichael Tuexen /* 38467dc90a1dSMichael Tuexen * Calculate the Initial Window, also used as Restart Window 38477dc90a1dSMichael Tuexen * 38487dc90a1dSMichael Tuexen * RFC5681 Section 3.1 specifies the default conservative values. 38497dc90a1dSMichael Tuexen * RFC3390 specifies slightly more aggressive values. 38507dc90a1dSMichael Tuexen * RFC6928 increases it to ten segments. 38517dc90a1dSMichael Tuexen * Support for user specified value for initial flight size. 38527dc90a1dSMichael Tuexen */ 38537dc90a1dSMichael Tuexen if (V_tcp_initcwnd_segments) 38547dc90a1dSMichael Tuexen return min(V_tcp_initcwnd_segments * maxseg, 38557dc90a1dSMichael Tuexen max(2 * maxseg, V_tcp_initcwnd_segments * 1460)); 38567dc90a1dSMichael Tuexen else if (V_tcp_do_rfc3390) 38577dc90a1dSMichael Tuexen return min(4 * maxseg, max(2 * maxseg, 4380)); 38587dc90a1dSMichael Tuexen else { 38597dc90a1dSMichael Tuexen /* Per RFC5681 Section 3.1 */ 38607dc90a1dSMichael Tuexen if (maxseg > 2190) 38617dc90a1dSMichael Tuexen return (2 * maxseg); 38627dc90a1dSMichael Tuexen else if (maxseg > 1095) 38637dc90a1dSMichael Tuexen return (3 * maxseg); 38647dc90a1dSMichael Tuexen else 38657dc90a1dSMichael Tuexen return (4 * maxseg); 38667dc90a1dSMichael Tuexen } 38677dc90a1dSMichael Tuexen } 3868