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> 61adc56f5aSEdward Tomasz Napierala #include <sys/arb.h> 6298163b98SPoul-Henning Kamp #include <sys/kernel.h> 63bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 6439bc9de5SLawrence Stewart #include <sys/hhook.h> 65bd79708dSJonathan T. Looney #endif 66df8bae1dSRodney W. Grimes #include <sys/malloc.h> 67df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 68a29f300eSGarrett Wollman #include <sys/proc.h> /* for proc0 declaration */ 69df8bae1dSRodney W. Grimes #include <sys/protosw.h> 70adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h> 7157f60867SMark Johnston #include <sys/sdt.h> 72960ed29cSSeigo Tanimura #include <sys/signalvar.h> 73df8bae1dSRodney W. Grimes #include <sys/socket.h> 74df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 75960ed29cSSeigo Tanimura #include <sys/sysctl.h> 76816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 77960ed29cSSeigo Tanimura #include <sys/systm.h> 78adc56f5aSEdward Tomasz Napierala #include <sys/stats.h> 79df8bae1dSRodney W. Grimes 80e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 81e79adb8eSGarrett Wollman 8212e2e970SAndre Oppermann #include <vm/uma.h> 8312e2e970SAndre Oppermann 84df8bae1dSRodney W. Grimes #include <net/if.h> 8576039bc8SGleb Smirnoff #include <net/if_var.h> 86df8bae1dSRodney W. Grimes #include <net/route.h> 87530c0060SRobert Watson #include <net/vnet.h> 88df8bae1dSRodney W. Grimes 89773673c1SAndre Oppermann #define TCPSTATES /* for logging */ 90773673c1SAndre Oppermann 91df8bae1dSRodney W. Grimes #include <netinet/in.h> 9257f60867SMark Johnston #include <netinet/in_kdtrace.h> 93960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 94df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 95df8bae1dSRodney W. Grimes #include <netinet/ip.h> 968e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 97686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 98df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 99ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 100686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 101686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h> 102686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 103b66d74c1SGleb Smirnoff #include <netinet6/in6_var.h> 104960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h> 105960ed29cSSeigo Tanimura #include <netinet6/nd6.h> 1062de3e790SGleb Smirnoff #include <netinet/tcp.h> 107df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 1082529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h> 109df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 110df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 111df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 112fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 113df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 1144644fda3SGleb Smirnoff #include <netinet/cc/cc.h> 115c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h> 11686a996e6SHiren Panchasara #ifdef TCPPCAP 11786a996e6SHiren Panchasara #include <netinet/tcp_pcap.h> 11886a996e6SHiren Panchasara #endif 119c325962bSMike Silbersack #include <netinet/tcp_syncache.h> 120610ee2f9SDavid Greenman #ifdef TCPDEBUG 121df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 122fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */ 12309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 12409fe6320SNavdeep Parhar #include <netinet/tcp_offload.h> 12509fe6320SNavdeep Parhar #endif 1269e644c23SMichael Tuexen #include <netinet/udp.h> 127fb59c426SYoshinobu Inoue 128fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 129b9234fafSSam Leffler 130db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 131db4f9cc7SJonathan Lemon 132aed55708SRobert Watson #include <security/mac/mac_framework.h> 133aed55708SRobert Watson 134dbc42409SLawrence Stewart const int tcprexmtthresh = 3; 1350312fbe9SPoul-Henning Kamp 136334fc582SBjoern A. Zeeb VNET_DEFINE(int, tcp_log_in_vain) = 0; 137334fc582SBjoern A. Zeeb SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_VNET | CTLFLAG_RW, 138334fc582SBjoern A. Zeeb &VNET_NAME(tcp_log_in_vain), 0, 139eddfbb76SRobert Watson "Log all incoming TCP segments to closed ports"); 140816a3d83SPoul-Henning Kamp 14182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0; 14282cea7e6SBjoern A. Zeeb #define V_blackhole VNET(blackhole) 1436df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW, 144eddfbb76SRobert Watson &VNET_NAME(blackhole), 0, 145eddfbb76SRobert Watson "Do not send RST on segments to closed ports"); 14616f7f31fSGeoff Rehmet 14782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1; 1486df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW, 149eddfbb76SRobert Watson &VNET_NAME(tcp_delack_enabled), 0, 1503d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 151f498eeeeSDavid Greenman 15282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0; 1536df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW, 154eddfbb76SRobert Watson &VNET_NAME(drop_synfin), 0, 155eddfbb76SRobert Watson "Drop TCP packets with SYN+FIN set"); 156f8613305SDag-Erling Smørgrav 1570e1d7c25SRichard Scheffenegger VNET_DEFINE(int, tcp_do_prr_conservative) = 0; 1580e1d7c25SRichard Scheffenegger SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_prr_conservative, CTLFLAG_VNET | CTLFLAG_RW, 1590e1d7c25SRichard Scheffenegger &VNET_NAME(tcp_do_prr_conservative), 0, 1600e1d7c25SRichard Scheffenegger "Do conservative Proportional Rate Reduction"); 1610e1d7c25SRichard Scheffenegger 1620e1d7c25SRichard Scheffenegger VNET_DEFINE(int, tcp_do_prr) = 1; 1630e1d7c25SRichard Scheffenegger SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_prr, CTLFLAG_VNET | CTLFLAG_RW, 1640e1d7c25SRichard Scheffenegger &VNET_NAME(tcp_do_prr), 1, 1650e1d7c25SRichard Scheffenegger "Enable Proportional Rate Reduction per RFC 6937"); 1660e1d7c25SRichard Scheffenegger 1670471a8c7SRichard Scheffenegger VNET_DEFINE(int, tcp_do_lrd) = 0; 1680471a8c7SRichard Scheffenegger SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_lrd, CTLFLAG_VNET | CTLFLAG_RW, 1690471a8c7SRichard Scheffenegger &VNET_NAME(tcp_do_lrd), 1, 1700471a8c7SRichard Scheffenegger "Perform Lost Retransmission Detection"); 1710471a8c7SRichard Scheffenegger 172b72e56e7SMichael Tuexen VNET_DEFINE(int, tcp_do_newcwv) = 0; 173b72e56e7SMichael Tuexen SYSCTL_INT(_net_inet_tcp, OID_AUTO, newcwv, CTLFLAG_VNET | CTLFLAG_RW, 174b72e56e7SMichael Tuexen &VNET_NAME(tcp_do_newcwv), 0, 175b72e56e7SMichael Tuexen "Enable New Congestion Window Validation per RFC7661"); 176b72e56e7SMichael Tuexen 17782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1; 1786df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW, 179eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3042), 0, 180eddfbb76SRobert Watson "Enable RFC 3042 (Limited Transmit)"); 181582a954bSJeffrey Hsu 18282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1; 1836df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW, 184eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3390), 0, 185da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 186da3a8a1aSJeffrey Hsu 187356c7958SHiren Panchasara VNET_DEFINE(int, tcp_initcwnd_segments) = 10; 188356c7958SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments, 189356c7958SHiren Panchasara CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0, 190356c7958SHiren Panchasara "Slow-start flight size (initial congestion window) in number of segments"); 19109440655SAndre Oppermann 19282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1; 1936df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW, 194eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3465), 0, 19524cb0f22SLawrence Stewart "Enable RFC 3465 (Appropriate Byte Counting)"); 196eddfbb76SRobert Watson 19782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2; 1986df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW, 199eddfbb76SRobert Watson &VNET_NAME(tcp_abc_l_var), 2, 20024cb0f22SLawrence Stewart "Cap the max cwnd increment during slow-start to this number of segments"); 20124cb0f22SLawrence Stewart 2027029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, 2037029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 2047029da5cSPawel Biernacki "TCP ECN"); 205f2512ba1SRui Paulo 206883054b4SDon Lewis VNET_DEFINE(int, tcp_do_ecn) = 2; 2076df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW, 208eddfbb76SRobert Watson &VNET_NAME(tcp_do_ecn), 0, 209eddfbb76SRobert Watson "TCP ECN support"); 210eddfbb76SRobert Watson 21182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1; 2126df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_VNET | CTLFLAG_RW, 213eddfbb76SRobert Watson &VNET_NAME(tcp_ecn_maxretries), 0, 214eddfbb76SRobert Watson "Max retries before giving up on ECN"); 215eddfbb76SRobert Watson 2163220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0; 2176df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW, 2183220a212SGleb Smirnoff &VNET_NAME(tcp_insecure_syn), 0, 2193220a212SGleb Smirnoff "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets"); 2203220a212SGleb Smirnoff 22182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0; 2226df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW, 223eddfbb76SRobert Watson &VNET_NAME(tcp_insecure_rst), 0, 2243220a212SGleb Smirnoff "Follow RFC793 instead of RFC5961 criteria for accepting RST packets"); 225a69968eeSMike Silbersack 226fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64; 227873789cbSAndre Oppermann #define V_tcp_recvspace VNET(tcp_recvspace) 2286df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW, 229873789cbSAndre Oppermann &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size"); 230873789cbSAndre Oppermann 23182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1; 2326df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW, 233eddfbb76SRobert Watson &VNET_NAME(tcp_do_autorcvbuf), 0, 2348b615593SMarko Zec "Enable automatic receive buffer sizing"); 2356741ecf5SAndre Oppermann 236b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024; 2376df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW, 238eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_max), 0, 2398b615593SMarko Zec "Max size of automatic receive buffer"); 2406741ecf5SAndre Oppermann 241eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb); 24244e33a07SMarko Zec #define tcb6 tcb /* for KAME src sync over BSD*'s */ 24382cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo); 244df8bae1dSRodney W. Grimes 245315e3e38SRobert Watson /* 246bf840a17SGleb Smirnoff * TCP statistics are stored in an array of counter(9)s, which size matches 247bf840a17SGleb Smirnoff * size of struct tcpstat. TCP running connection count is a regular array. 2485923c293SGleb Smirnoff */ 2495da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat); 2505da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat, 2515da0521fSAndrey V. Elsukov tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 252bf840a17SGleb Smirnoff VNET_DEFINE(counter_u64_t, tcps_states[TCP_NSTATES]); 253bf840a17SGleb Smirnoff SYSCTL_COUNTER_U64_ARRAY(_net_inet_tcp, TCPCTL_STATES, states, CTLFLAG_RD | 254d4d32b9fSHans Petter Selasky CTLFLAG_VNET, &VNET_NAME(tcps_states)[0], TCP_NSTATES, 255bf840a17SGleb Smirnoff "TCP connection counts by TCP state"); 256bf840a17SGleb Smirnoff 257bf840a17SGleb Smirnoff static void 258bf840a17SGleb Smirnoff tcp_vnet_init(const void *unused) 259bf840a17SGleb Smirnoff { 260bf840a17SGleb Smirnoff 261f59d975eSGleb Smirnoff COUNTER_ARRAY_ALLOC(V_tcps_states, TCP_NSTATES, M_WAITOK); 262bf840a17SGleb Smirnoff VNET_PCPUSTAT_ALLOC(tcpstat, M_WAITOK); 263bf840a17SGleb Smirnoff } 264bf840a17SGleb Smirnoff VNET_SYSINIT(tcp_vnet_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 265bf840a17SGleb Smirnoff tcp_vnet_init, NULL); 2665923c293SGleb Smirnoff 2675923c293SGleb Smirnoff #ifdef VIMAGE 268bf840a17SGleb Smirnoff static void 269bf840a17SGleb Smirnoff tcp_vnet_uninit(const void *unused) 270bf840a17SGleb Smirnoff { 271bf840a17SGleb Smirnoff 272f59d975eSGleb Smirnoff COUNTER_ARRAY_FREE(V_tcps_states, TCP_NSTATES); 273bf840a17SGleb Smirnoff VNET_PCPUSTAT_FREE(tcpstat); 274bf840a17SGleb Smirnoff } 275bf840a17SGleb Smirnoff VNET_SYSUNINIT(tcp_vnet_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 276bf840a17SGleb Smirnoff tcp_vnet_uninit, NULL); 2775923c293SGleb Smirnoff #endif /* VIMAGE */ 278bf840a17SGleb Smirnoff 2795923c293SGleb Smirnoff /* 2807ca6e296SMichael Tuexen * Kernel module interface for updating tcpstat. The first argument is an index 2815923c293SGleb Smirnoff * into tcpstat treated as an array. 282315e3e38SRobert Watson */ 283315e3e38SRobert Watson void 2847ca6e296SMichael Tuexen kmod_tcpstat_add(int statnum, int val) 285315e3e38SRobert Watson { 286315e3e38SRobert Watson 2877ca6e296SMichael Tuexen counter_u64_add(VNET(tcpstat)[statnum], val); 288315e3e38SRobert Watson } 289315e3e38SRobert Watson 290bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 291dbc42409SLawrence Stewart /* 29239bc9de5SLawrence Stewart * Wrapper for the TCP established input helper hook. 29339bc9de5SLawrence Stewart */ 29455bceb1eSRandall Stewart void 29539bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to) 29639bc9de5SLawrence Stewart { 29739bc9de5SLawrence Stewart struct tcp_hhook_data hhook_data; 29839bc9de5SLawrence Stewart 29939bc9de5SLawrence Stewart if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) { 30039bc9de5SLawrence Stewart hhook_data.tp = tp; 30139bc9de5SLawrence Stewart hhook_data.th = th; 30239bc9de5SLawrence Stewart hhook_data.to = to; 30339bc9de5SLawrence Stewart 30439bc9de5SLawrence Stewart hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data, 30539bc9de5SLawrence Stewart tp->osd); 30639bc9de5SLawrence Stewart } 30739bc9de5SLawrence Stewart } 308bd79708dSJonathan T. Looney #endif 30939bc9de5SLawrence Stewart 31039bc9de5SLawrence Stewart /* 311dbc42409SLawrence Stewart * CC wrapper hook functions 312dbc42409SLawrence Stewart */ 31355bceb1eSRandall Stewart void 3144b7b743cSLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t nsegs, 3154b7b743cSLawrence Stewart uint16_t type) 316f2512ba1SRui Paulo { 317adc56f5aSEdward Tomasz Napierala #ifdef STATS 318adc56f5aSEdward Tomasz Napierala int32_t gput; 319adc56f5aSEdward Tomasz Napierala #endif 320adc56f5aSEdward Tomasz Napierala 321dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 322f2512ba1SRui Paulo 3234b7b743cSLawrence Stewart tp->ccv->nsegs = nsegs; 324dbc42409SLawrence Stewart tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th); 325b72e56e7SMichael Tuexen if ((!V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd)) || 326b72e56e7SMichael Tuexen (V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd) && 327b72e56e7SMichael Tuexen (tp->snd_cwnd < (tcp_compute_pipe(tp) * 2)))) 328dbc42409SLawrence Stewart tp->ccv->flags |= CCF_CWND_LIMITED; 329dbc42409SLawrence Stewart else 330dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_CWND_LIMITED; 331dbc42409SLawrence Stewart 332dbc42409SLawrence Stewart if (type == CC_ACK) { 333adc56f5aSEdward Tomasz Napierala #ifdef STATS 334adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF, 335adc56f5aSEdward Tomasz Napierala ((int32_t)tp->snd_cwnd) - tp->snd_wnd); 336adc56f5aSEdward Tomasz Napierala if (!IN_RECOVERY(tp->t_flags)) 337adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_ACKLEN, 338adc56f5aSEdward Tomasz Napierala tp->ccv->bytes_this_ack / (tcp_maxseg(tp) * nsegs)); 339adc56f5aSEdward Tomasz Napierala if ((tp->t_flags & TF_GPUTINPROG) && 340adc56f5aSEdward Tomasz Napierala SEQ_GEQ(th->th_ack, tp->gput_ack)) { 341adc56f5aSEdward Tomasz Napierala /* 342adc56f5aSEdward Tomasz Napierala * Compute goodput in bits per millisecond. 343adc56f5aSEdward Tomasz Napierala */ 344adc56f5aSEdward Tomasz Napierala gput = (((int64_t)(th->th_ack - tp->gput_seq)) << 3) / 345adc56f5aSEdward Tomasz Napierala max(1, tcp_ts_getticks() - tp->gput_ts); 346adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT, 347adc56f5aSEdward Tomasz Napierala gput); 348adc56f5aSEdward Tomasz Napierala /* 349adc56f5aSEdward Tomasz Napierala * XXXLAS: This is a temporary hack, and should be 350adc56f5aSEdward Tomasz Napierala * chained off VOI_TCP_GPUT when stats(9) grows an API 351adc56f5aSEdward Tomasz Napierala * to deal with chained VOIs. 352adc56f5aSEdward Tomasz Napierala */ 353adc56f5aSEdward Tomasz Napierala if (tp->t_stats_gput_prev > 0) 354adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_s32(tp->t_stats, 355adc56f5aSEdward Tomasz Napierala VOI_TCP_GPUT_ND, 356adc56f5aSEdward Tomasz Napierala ((gput - tp->t_stats_gput_prev) * 100) / 357adc56f5aSEdward Tomasz Napierala tp->t_stats_gput_prev); 358adc56f5aSEdward Tomasz Napierala tp->t_flags &= ~TF_GPUTINPROG; 359adc56f5aSEdward Tomasz Napierala tp->t_stats_gput_prev = gput; 360adc56f5aSEdward Tomasz Napierala } 361adc56f5aSEdward Tomasz Napierala #endif /* STATS */ 362dbc42409SLawrence Stewart if (tp->snd_cwnd > tp->snd_ssthresh) { 3631951fa79SMichael Tuexen tp->t_bytes_acked += tp->ccv->bytes_this_ack; 364dbc42409SLawrence Stewart if (tp->t_bytes_acked >= tp->snd_cwnd) { 365dbc42409SLawrence Stewart tp->t_bytes_acked -= tp->snd_cwnd; 366dbc42409SLawrence Stewart tp->ccv->flags |= CCF_ABC_SENTAWND; 367dbc42409SLawrence Stewart } 368dbc42409SLawrence Stewart } else { 369dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_ABC_SENTAWND; 370dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 371dbc42409SLawrence Stewart } 372dbc42409SLawrence Stewart } 373dbc42409SLawrence Stewart 374dbc42409SLawrence Stewart if (CC_ALGO(tp)->ack_received != NULL) { 375dbc42409SLawrence Stewart /* XXXLAS: Find a way to live without this */ 376dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 377dbc42409SLawrence Stewart CC_ALGO(tp)->ack_received(tp->ccv, type); 378dbc42409SLawrence Stewart } 379adc56f5aSEdward Tomasz Napierala #ifdef STATS 380adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, tp->snd_cwnd); 381adc56f5aSEdward Tomasz Napierala #endif 382dbc42409SLawrence Stewart } 383dbc42409SLawrence Stewart 38455bceb1eSRandall Stewart void 385dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp) 386dbc42409SLawrence Stewart { 387dbc42409SLawrence Stewart struct hc_metrics_lite metrics; 388dbc42409SLawrence Stewart struct inpcb *inp = tp->t_inpcb; 3890c39d38dSGleb Smirnoff u_int maxseg; 390dbc42409SLawrence Stewart int rtt; 391dbc42409SLawrence Stewart 392dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 393dbc42409SLawrence Stewart 394dbc42409SLawrence Stewart tcp_hc_get(&inp->inp_inc, &metrics); 3950c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 396dbc42409SLawrence Stewart 397dbc42409SLawrence Stewart if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 398dbc42409SLawrence Stewart tp->t_srtt = rtt; 399dbc42409SLawrence Stewart tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 400dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrtt); 401dbc42409SLawrence Stewart if (metrics.rmx_rttvar) { 402dbc42409SLawrence Stewart tp->t_rttvar = metrics.rmx_rttvar; 403dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrttvar); 404dbc42409SLawrence Stewart } else { 405dbc42409SLawrence Stewart /* default variation is +- 1 rtt */ 406dbc42409SLawrence Stewart tp->t_rttvar = 407dbc42409SLawrence Stewart tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 408dbc42409SLawrence Stewart } 409dbc42409SLawrence Stewart TCPT_RANGESET(tp->t_rxtcur, 410dbc42409SLawrence Stewart ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 411dbc42409SLawrence Stewart tp->t_rttmin, TCPTV_REXMTMAX); 412dbc42409SLawrence Stewart } 413dbc42409SLawrence Stewart if (metrics.rmx_ssthresh) { 414dbc42409SLawrence Stewart /* 415dbc42409SLawrence Stewart * There's some sort of gateway or interface 416dbc42409SLawrence Stewart * buffer limit on the path. Use this to set 417a4641f4eSPedro F. Giffuni * the slow start threshold, but set the 418dbc42409SLawrence Stewart * threshold to no less than 2*mss. 419dbc42409SLawrence Stewart */ 4200c39d38dSGleb Smirnoff tp->snd_ssthresh = max(2 * maxseg, metrics.rmx_ssthresh); 421dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedssthresh); 422dbc42409SLawrence Stewart } 423dbc42409SLawrence Stewart 424dbc42409SLawrence Stewart /* 4259ec4a4ccSAndre Oppermann * Set the initial slow-start flight size. 426dbc42409SLawrence Stewart * 427cf8f04f4SAndre Oppermann * If a SYN or SYN/ACK was lost and retransmitted, we have to 428cf8f04f4SAndre Oppermann * reduce the initial CWND to one segment as congestion is likely 429cf8f04f4SAndre Oppermann * requiring us to be cautious. 430dbc42409SLawrence Stewart */ 431cf8f04f4SAndre Oppermann if (tp->snd_cwnd == 1) 4320c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; /* SYN(-ACK) lost */ 433dbc42409SLawrence Stewart else 4347dc90a1dSMichael Tuexen tp->snd_cwnd = tcp_compute_initwnd(maxseg); 435dbc42409SLawrence Stewart 436dbc42409SLawrence Stewart if (CC_ALGO(tp)->conn_init != NULL) 437dbc42409SLawrence Stewart CC_ALGO(tp)->conn_init(tp->ccv); 438dbc42409SLawrence Stewart } 439dbc42409SLawrence Stewart 440dbc42409SLawrence Stewart void inline 441dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type) 442dbc42409SLawrence Stewart { 443dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 444dbc42409SLawrence Stewart 445adc56f5aSEdward Tomasz Napierala #ifdef STATS 446adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type); 447adc56f5aSEdward Tomasz Napierala #endif 448adc56f5aSEdward Tomasz Napierala 449dbc42409SLawrence Stewart switch(type) { 450dbc42409SLawrence Stewart case CC_NDUPACK: 451dbc42409SLawrence Stewart if (!IN_FASTRECOVERY(tp->t_flags)) { 452f2512ba1SRui Paulo tp->snd_recover = tp->snd_max; 4533cf38784SMichael Tuexen if (tp->t_flags2 & TF2_ECN_PERMIT) 4543cf38784SMichael Tuexen tp->t_flags2 |= TF2_ECN_SND_CWR; 455f2512ba1SRui Paulo } 456dbc42409SLawrence Stewart break; 457dbc42409SLawrence Stewart case CC_ECN: 458af2fb894SRichard Scheffenegger if (!IN_CONGRECOVERY(tp->t_flags) || 459af2fb894SRichard Scheffenegger /* 460af2fb894SRichard Scheffenegger * Allow ECN reaction on ACK to CWR, if 461af2fb894SRichard Scheffenegger * that data segment was also CE marked. 462af2fb894SRichard Scheffenegger */ 463af2fb894SRichard Scheffenegger SEQ_GEQ(th->th_ack, tp->snd_recover)) { 464af2fb894SRichard Scheffenegger EXIT_CONGRECOVERY(tp->t_flags); 465dbc42409SLawrence Stewart TCPSTAT_INC(tcps_ecn_rcwnd); 466af2fb894SRichard Scheffenegger tp->snd_recover = tp->snd_max + 1; 4673cf38784SMichael Tuexen if (tp->t_flags2 & TF2_ECN_PERMIT) 4683cf38784SMichael Tuexen tp->t_flags2 |= TF2_ECN_SND_CWR; 469dbc42409SLawrence Stewart } 470dbc42409SLawrence Stewart break; 471dbc42409SLawrence Stewart case CC_RTO: 472dbc42409SLawrence Stewart tp->t_dupacks = 0; 473dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 474dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 4759cc711c9SMichael Tuexen if (tp->t_flags2 & TF2_ECN_PERMIT) 4769cc711c9SMichael Tuexen tp->t_flags2 |= TF2_ECN_SND_CWR; 477dbc42409SLawrence Stewart break; 478dbc42409SLawrence Stewart case CC_RTO_ERR: 479dbc42409SLawrence Stewart TCPSTAT_INC(tcps_sndrexmitbad); 480dbc42409SLawrence Stewart /* RTO was unnecessary, so reset everything. */ 481dbc42409SLawrence Stewart tp->snd_cwnd = tp->snd_cwnd_prev; 482dbc42409SLawrence Stewart tp->snd_ssthresh = tp->snd_ssthresh_prev; 483dbc42409SLawrence Stewart tp->snd_recover = tp->snd_recover_prev; 484dbc42409SLawrence Stewart if (tp->t_flags & TF_WASFRECOVERY) 485dbc42409SLawrence Stewart ENTER_FASTRECOVERY(tp->t_flags); 486dbc42409SLawrence Stewart if (tp->t_flags & TF_WASCRECOVERY) 487dbc42409SLawrence Stewart ENTER_CONGRECOVERY(tp->t_flags); 488dbc42409SLawrence Stewart tp->snd_nxt = tp->snd_max; 489672dc4aeSJohn Baldwin tp->t_flags &= ~TF_PREVVALID; 490dbc42409SLawrence Stewart tp->t_badrxtwin = 0; 491dbc42409SLawrence Stewart break; 492dbc42409SLawrence Stewart } 493dbc42409SLawrence Stewart 494dbc42409SLawrence Stewart if (CC_ALGO(tp)->cong_signal != NULL) { 495dbc42409SLawrence Stewart if (th != NULL) 496dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 497dbc42409SLawrence Stewart CC_ALGO(tp)->cong_signal(tp->ccv, type); 498dbc42409SLawrence Stewart } 499dbc42409SLawrence Stewart } 500dbc42409SLawrence Stewart 50155bceb1eSRandall Stewart void inline 502dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th) 503dbc42409SLawrence Stewart { 504dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 505dbc42409SLawrence Stewart 506dbc42409SLawrence Stewart /* XXXLAS: KASSERT that we're in recovery? */ 507dbc42409SLawrence Stewart 508dbc42409SLawrence Stewart if (CC_ALGO(tp)->post_recovery != NULL) { 509dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 510dbc42409SLawrence Stewart CC_ALGO(tp)->post_recovery(tp->ccv); 511dbc42409SLawrence Stewart } 512dbc42409SLawrence Stewart /* XXXLAS: EXIT_RECOVERY ? */ 513dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 51448be5b97SRichard Scheffenegger tp->sackhint.delivered_data = 0; 515e5313869SRichard Scheffenegger tp->sackhint.prr_out = 0; 516dbc42409SLawrence Stewart } 5170312fbe9SPoul-Henning Kamp 518df8bae1dSRodney W. Grimes /* 519262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 52085536381SHiren Panchasara * following conditions are met: 52185536381SHiren Panchasara * - There is no delayed ack timer in progress. 52285536381SHiren Panchasara * - Our last ack wasn't a 0-sized window. We never want to delay 52385536381SHiren Panchasara * the ack that opens up a 0-sized window. 52485536381SHiren Panchasara * - LRO wasn't used for this segment. We make sure by checking that the 52585536381SHiren Panchasara * segment size is not larger than the MSS. 526d8c85a26SJonathan Lemon */ 527c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen) \ 528b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 529a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 5300c39d38dSGleb Smirnoff (tlen <= tp->t_maxseg) && \ 531603724d3SBjoern A. Zeeb (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 532d8c85a26SJonathan Lemon 5334ad24737SRandall Stewart void inline 5345d8fd932SRandall Stewart cc_ecnpkt_handler_flags(struct tcpcb *tp, uint16_t flags, uint8_t iptos) 53564807b30SHiren Panchasara { 53664807b30SHiren Panchasara INP_WLOCK_ASSERT(tp->t_inpcb); 53764807b30SHiren Panchasara 53864807b30SHiren Panchasara if (CC_ALGO(tp)->ecnpkt_handler != NULL) { 53964807b30SHiren Panchasara switch (iptos & IPTOS_ECN_MASK) { 54064807b30SHiren Panchasara case IPTOS_ECN_CE: 54164807b30SHiren Panchasara tp->ccv->flags |= CCF_IPHDR_CE; 54264807b30SHiren Panchasara break; 54364807b30SHiren Panchasara case IPTOS_ECN_ECT0: 54483a2839fSMichael Tuexen /* FALLTHROUGH */ 54564807b30SHiren Panchasara case IPTOS_ECN_ECT1: 54683a2839fSMichael Tuexen /* FALLTHROUGH */ 54783a2839fSMichael Tuexen case IPTOS_ECN_NOTECT: 54864807b30SHiren Panchasara tp->ccv->flags &= ~CCF_IPHDR_CE; 54964807b30SHiren Panchasara break; 55064807b30SHiren Panchasara } 55164807b30SHiren Panchasara 5525d8fd932SRandall Stewart if (flags & TH_CWR) 55364807b30SHiren Panchasara tp->ccv->flags |= CCF_TCPHDR_CWR; 55464807b30SHiren Panchasara else 55564807b30SHiren Panchasara tp->ccv->flags &= ~CCF_TCPHDR_CWR; 55664807b30SHiren Panchasara 55764807b30SHiren Panchasara CC_ALGO(tp)->ecnpkt_handler(tp->ccv); 55864807b30SHiren Panchasara 559e11c9783SMichael Tuexen if (tp->ccv->flags & CCF_ACKNOW) { 56064807b30SHiren Panchasara tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 561e11c9783SMichael Tuexen tp->t_flags |= TF_ACKNOW; 562e11c9783SMichael Tuexen } 56364807b30SHiren Panchasara } 56464807b30SHiren Panchasara } 56564807b30SHiren Panchasara 5665d8fd932SRandall Stewart void inline 5675d8fd932SRandall Stewart cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos) 5685d8fd932SRandall Stewart { 5695d8fd932SRandall Stewart cc_ecnpkt_handler_flags(tp, th->th_flags, iptos); 5705d8fd932SRandall Stewart } 5715d8fd932SRandall Stewart 572df8bae1dSRodney W. Grimes /* 5738d573cc1SAndre Oppermann * TCP input handling is split into multiple parts: 5748d573cc1SAndre Oppermann * tcp6_input is a thin wrapper around tcp_input for the extended 5758d573cc1SAndre Oppermann * ip6_protox[] call format in ip6_input 5768d573cc1SAndre Oppermann * tcp_input handles primary segment validation, inpcb lookup and 5778d573cc1SAndre Oppermann * SYN processing on listen sockets 5788d573cc1SAndre Oppermann * tcp_do_segment processes the ACK and text of the segment for 5798d573cc1SAndre Oppermann * establishing, established and closing connections 580df8bae1dSRodney W. Grimes */ 581fb59c426SYoshinobu Inoue #ifdef INET6 582fb59c426SYoshinobu Inoue int 5839e644c23SMichael Tuexen tcp6_input_with_port(struct mbuf **mp, int *offp, int proto, uint16_t port) 584fb59c426SYoshinobu Inoue { 585503f4e47SBjoern A. Zeeb struct mbuf *m; 58633841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 5873e88eb90SAndrey V. Elsukov struct ip6_hdr *ip6; 588fb59c426SYoshinobu Inoue 589503f4e47SBjoern A. Zeeb m = *mp; 590a4adf6ccSBjoern A. Zeeb if (m->m_len < *offp + sizeof(struct tcphdr)) { 5914e619b17SBjoern A. Zeeb m = m_pullup(m, *offp + sizeof(struct tcphdr)); 5924e619b17SBjoern A. Zeeb if (m == NULL) { 5934e619b17SBjoern A. Zeeb *mp = m; 5944e619b17SBjoern A. Zeeb TCPSTAT_INC(tcps_rcvshort); 5954e619b17SBjoern A. Zeeb return (IPPROTO_DONE); 5964e619b17SBjoern A. Zeeb } 597a4adf6ccSBjoern A. Zeeb } 598fb59c426SYoshinobu Inoue 599fb59c426SYoshinobu Inoue /* 600fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 601fb59c426SYoshinobu Inoue * better place to put this in? 602fb59c426SYoshinobu Inoue */ 6033e88eb90SAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 6048268d82cSAlexander V. Chernikov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false); 60533841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 606fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 607fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 608a8fe77d8SBjoern A. Zeeb *mp = NULL; 6098f5a8818SKevin Lo return (IPPROTO_DONE); 610fb59c426SYoshinobu Inoue } 611fb59c426SYoshinobu Inoue 612a8fe77d8SBjoern A. Zeeb *mp = m; 6139e644c23SMichael Tuexen return (tcp_input_with_port(mp, offp, proto, port)); 6149e644c23SMichael Tuexen } 6159e644c23SMichael Tuexen 6169e644c23SMichael Tuexen int 6179e644c23SMichael Tuexen tcp6_input(struct mbuf **mp, int *offp, int proto) 6189e644c23SMichael Tuexen { 6199e644c23SMichael Tuexen 6209e644c23SMichael Tuexen return(tcp6_input_with_port(mp, offp, proto, 0)); 621fb59c426SYoshinobu Inoue } 622b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 623fb59c426SYoshinobu Inoue 6248f5a8818SKevin Lo int 6259e644c23SMichael Tuexen tcp_input_with_port(struct mbuf **mp, int *offp, int proto, uint16_t port) 626df8bae1dSRodney W. Grimes { 6278f5a8818SKevin Lo struct mbuf *m = *mp; 628b287c6c7SBjoern A. Zeeb struct tcphdr *th = NULL; 629ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 630ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 631302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 632302ce8d6SAndre Oppermann struct socket *so = NULL; 633e79adb8eSGarrett Wollman u_char *optp = NULL; 6348f5a8818SKevin Lo int off0; 63526f9a767SRodney W. Grimes int optlen = 0; 636b287c6c7SBjoern A. Zeeb #ifdef INET 637b287c6c7SBjoern A. Zeeb int len; 638c4556b2fSAndrey V. Elsukov uint8_t ipttl; 639b287c6c7SBjoern A. Zeeb #endif 640b287c6c7SBjoern A. Zeeb int tlen = 0, off; 641fb59c426SYoshinobu Inoue int drop_hdrlen; 642ad3f9ab3SAndre Oppermann int thflags; 643302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 64408d9c920SGleb Smirnoff int lookupflag; 6451d7ee746SKurt Lidl uint8_t iptos; 646c1de64a4SAndrey V. Elsukov struct m_tag *fwd_tag = NULL; 647c068736aSJeffrey Hsu #ifdef INET6 648302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 649c068736aSJeffrey Hsu int isipv6; 650c068736aSJeffrey Hsu #else 651a160e630SAndre Oppermann const void *ip6 = NULL; 652b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 653302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 654a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 655610ee2f9SDavid Greenman #ifdef TCPDEBUG 656c068736aSJeffrey Hsu /* 657c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 658c068736aSJeffrey Hsu * now IPv6. 659c068736aSJeffrey Hsu */ 66014739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 661410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 662610ee2f9SDavid Greenman short ostate = 0; 663610ee2f9SDavid Greenman #endif 664c068736aSJeffrey Hsu 665d40c0d47SGleb Smirnoff NET_EPOCH_ASSERT(); 666d40c0d47SGleb Smirnoff 667fb59c426SYoshinobu Inoue #ifdef INET6 668fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 669fb59c426SYoshinobu Inoue #endif 670a0292f23SGarrett Wollman 6718f5a8818SKevin Lo off0 = *offp; 6728f5a8818SKevin Lo m = *mp; 6738f5a8818SKevin Lo *mp = NULL; 674302ce8d6SAndre Oppermann to.to_flags = 0; 67578b50714SRobert Watson TCPSTAT_INC(tcps_rcvtotal); 676fb59c426SYoshinobu Inoue 67704d3a452SHajimu UMEMOTO #ifdef INET6 678b287c6c7SBjoern A. Zeeb if (isipv6) { 679fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 68045747ba5SBjoern A. Zeeb th = (struct tcphdr *)((caddr_t)ip6 + off0); 681fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 6829e644c23SMichael Tuexen if (port) 6839e644c23SMichael Tuexen goto skip6_csum; 684356ab07eSBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) { 68545747ba5SBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 68645747ba5SBjoern A. Zeeb th->th_sum = m->m_pkthdr.csum_data; 68745747ba5SBjoern A. Zeeb else 68845747ba5SBjoern A. Zeeb th->th_sum = in6_cksum_pseudo(ip6, tlen, 68945747ba5SBjoern A. Zeeb IPPROTO_TCP, m->m_pkthdr.csum_data); 69045747ba5SBjoern A. Zeeb th->th_sum ^= 0xffff; 69145747ba5SBjoern A. Zeeb } else 69245747ba5SBjoern A. Zeeb th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen); 69345747ba5SBjoern A. Zeeb if (th->th_sum) { 69478b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 695fb59c426SYoshinobu Inoue goto drop; 696fb59c426SYoshinobu Inoue } 6979e644c23SMichael Tuexen skip6_csum: 69833841545SHajimu UMEMOTO /* 69933841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 70033841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 70133841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 70233841545SHajimu UMEMOTO * 70333841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 70433841545SHajimu UMEMOTO * already dropped in ip6_input. 70533841545SHajimu UMEMOTO */ 70633841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 70733841545SHajimu UMEMOTO /* XXX stat */ 70833841545SHajimu UMEMOTO goto drop; 70933841545SHajimu UMEMOTO } 710bb4a7d94SKristof Provost iptos = IPV6_TRAFFIC_CLASS(ip6); 711b287c6c7SBjoern A. Zeeb } 71204d3a452SHajimu UMEMOTO #endif 713b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 714b287c6c7SBjoern A. Zeeb else 715b287c6c7SBjoern A. Zeeb #endif 716b287c6c7SBjoern A. Zeeb #ifdef INET 717b287c6c7SBjoern A. Zeeb { 718df8bae1dSRodney W. Grimes /* 719df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 720df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 721df8bae1dSRodney W. Grimes */ 722fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 723105bd211SGleb Smirnoff ip_stripoptions(m); 724fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 725fb59c426SYoshinobu Inoue } 726df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 7274dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 7284dfdffe9SAndre Oppermann == NULL) { 72978b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 7308f5a8818SKevin Lo return (IPPROTO_DONE); 731df8bae1dSRodney W. Grimes } 732df8bae1dSRodney W. Grimes } 733fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 734db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 7358ad458a4SGleb Smirnoff tlen = ntohs(ip->ip_len) - off0; 736df8bae1dSRodney W. Grimes 7371d7ee746SKurt Lidl iptos = ip->ip_tos; 7389e644c23SMichael Tuexen if (port) 7399e644c23SMichael Tuexen goto skip_csum; 740db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 741db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 742db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 743db4f9cc7SJonathan Lemon else 744db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 745c068736aSJeffrey Hsu ip->ip_dst.s_addr, 7468f134647SGleb Smirnoff htonl(m->m_pkthdr.csum_data + tlen + 747c068736aSJeffrey Hsu IPPROTO_TCP)); 748db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 749db4f9cc7SJonathan Lemon } else { 7508f134647SGleb Smirnoff struct ipovly *ipov = (struct ipovly *)ip; 7518f134647SGleb Smirnoff 752df8bae1dSRodney W. Grimes /* 753df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 754df8bae1dSRodney W. Grimes */ 7558f134647SGleb Smirnoff len = off0 + tlen; 756c4556b2fSAndrey V. Elsukov ipttl = ip->ip_ttl; 757fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 7588f134647SGleb Smirnoff ipov->ih_len = htons(tlen); 759fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 76057f60867SMark Johnston /* Reset length for SDT probes. */ 7611d7ee746SKurt Lidl ip->ip_len = htons(len); 7621d7ee746SKurt Lidl /* Reset TOS bits */ 7631d7ee746SKurt Lidl ip->ip_tos = iptos; 7641d7ee746SKurt Lidl /* Re-initialization for later version check */ 765c4556b2fSAndrey V. Elsukov ip->ip_ttl = ipttl; 7661d7ee746SKurt Lidl ip->ip_v = IPVERSION; 767b2fe54bcSAndrey V. Elsukov ip->ip_hl = off0 >> 2; 768db4f9cc7SJonathan Lemon } 7699e644c23SMichael Tuexen skip_csum: 7709e644c23SMichael Tuexen if (th->th_sum && (port == 0)) { 77178b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 772df8bae1dSRodney W. Grimes goto drop; 773df8bae1dSRodney W. Grimes } 774fb59c426SYoshinobu Inoue } 775b287c6c7SBjoern A. Zeeb #endif /* INET */ 776df8bae1dSRodney W. Grimes 777df8bae1dSRodney W. Grimes /* 778df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 779df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 780df8bae1dSRodney W. Grimes */ 781fb59c426SYoshinobu Inoue off = th->th_off << 2; 782df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 78378b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadoff); 784df8bae1dSRodney W. Grimes goto drop; 785df8bae1dSRodney W. Grimes } 786fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 787df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 78804d3a452SHajimu UMEMOTO #ifdef INET6 789b287c6c7SBjoern A. Zeeb if (isipv6) { 790a4adf6ccSBjoern A. Zeeb if (m->m_len < off0 + off) { 7914e619b17SBjoern A. Zeeb m = m_pullup(m, off0 + off); 7924e619b17SBjoern A. Zeeb if (m == NULL) { 7934e619b17SBjoern A. Zeeb TCPSTAT_INC(tcps_rcvshort); 7944e619b17SBjoern A. Zeeb return (IPPROTO_DONE); 7954e619b17SBjoern A. Zeeb } 796a4adf6ccSBjoern A. Zeeb } 797fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 798fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 799b287c6c7SBjoern A. Zeeb } 80004d3a452SHajimu UMEMOTO #endif 801b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 802b287c6c7SBjoern A. Zeeb else 803b287c6c7SBjoern A. Zeeb #endif 804b287c6c7SBjoern A. Zeeb #ifdef INET 805b287c6c7SBjoern A. Zeeb { 806df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 807c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 8084dfdffe9SAndre Oppermann == NULL) { 80978b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 8108f5a8818SKevin Lo return (IPPROTO_DONE); 811df8bae1dSRodney W. Grimes } 812fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 813fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 814fb59c426SYoshinobu Inoue } 815df8bae1dSRodney W. Grimes } 816b287c6c7SBjoern A. Zeeb #endif 817df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 818fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 819df8bae1dSRodney W. Grimes } 820fb59c426SYoshinobu Inoue thflags = th->th_flags; 821df8bae1dSRodney W. Grimes 822e46cd3d4SDag-Erling Smørgrav /* 823df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 824df8bae1dSRodney W. Grimes */ 8252903309aSAttilio Rao tcp_fields_to_host(th); 826df8bae1dSRodney W. Grimes 827df8bae1dSRodney W. Grimes /* 828794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 829755c1f07SAndras Olah */ 830fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 831755c1f07SAndras Olah 832755c1f07SAndras Olah /* 8338d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 8348d573cc1SAndre Oppermann */ 835b8056faeSGleb Smirnoff if ( 836b8056faeSGleb Smirnoff #ifdef INET6 837b8056faeSGleb Smirnoff (isipv6 && (m->m_flags & M_IP6_NEXTHOP)) 838b8056faeSGleb Smirnoff #ifdef INET 839b8056faeSGleb Smirnoff || (!isipv6 && (m->m_flags & M_IP_NEXTHOP)) 840b8056faeSGleb Smirnoff #endif 841b8056faeSGleb Smirnoff #endif 842b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6) 843b8056faeSGleb Smirnoff (m->m_flags & M_IP_NEXTHOP) 844b8056faeSGleb Smirnoff #endif 845b8056faeSGleb Smirnoff ) 8469b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 8479b932e9eSAndre Oppermann 84808d9c920SGleb Smirnoff /* 8491db08fbeSGleb Smirnoff * For initial SYN packets we don't need write lock on matching 8501db08fbeSGleb Smirnoff * PCB, be it a listening one or a synchronized one. The packet 8511db08fbeSGleb Smirnoff * shall not modify its state. 85208d9c920SGleb Smirnoff */ 85308d9c920SGleb Smirnoff lookupflag = (thflags & (TH_ACK|TH_SYN)) == TH_SYN ? 8541db08fbeSGleb Smirnoff INPLOOKUP_RLOCKPCB : INPLOOKUP_WLOCKPCB; 855ce7ad664SEd Maste findpcb: 8568a006adbSBjoern A. Zeeb #ifdef INET6 8578a006adbSBjoern A. Zeeb if (isipv6 && fwd_tag != NULL) { 8588a006adbSBjoern A. Zeeb struct sockaddr_in6 *next_hop6; 8598a006adbSBjoern A. Zeeb 8608a006adbSBjoern A. Zeeb next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1); 8618a006adbSBjoern A. Zeeb /* 8628a006adbSBjoern A. Zeeb * Transparently forwarded. Pretend to be the destination. 8638a006adbSBjoern A. Zeeb * Already got one like this? 8648a006adbSBjoern A. Zeeb */ 8658a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, 8668a006adbSBjoern A. Zeeb &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport, 86708d9c920SGleb Smirnoff lookupflag, m->m_pkthdr.rcvif, m); 8688a006adbSBjoern A. Zeeb if (!inp) { 8698a006adbSBjoern A. Zeeb /* 8708a006adbSBjoern A. Zeeb * It's new. Try to find the ambushing socket. 8718a006adbSBjoern A. Zeeb * Because we've rewritten the destination address, 8728a006adbSBjoern A. Zeeb * any hardware-generated hash is ignored. 8738a006adbSBjoern A. Zeeb */ 8748a006adbSBjoern A. Zeeb inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src, 8758a006adbSBjoern A. Zeeb th->th_sport, &next_hop6->sin6_addr, 8768a006adbSBjoern A. Zeeb next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) : 87708d9c920SGleb Smirnoff th->th_dport, INPLOOKUP_WILDCARD | lookupflag, 87808d9c920SGleb Smirnoff m->m_pkthdr.rcvif); 8798a006adbSBjoern A. Zeeb } 880c1de64a4SAndrey V. Elsukov } else if (isipv6) { 8818a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src, 8828a006adbSBjoern A. Zeeb th->th_sport, &ip6->ip6_dst, th->th_dport, 88308d9c920SGleb Smirnoff INPLOOKUP_WILDCARD | lookupflag, m->m_pkthdr.rcvif, m); 8848a006adbSBjoern A. Zeeb } 8858a006adbSBjoern A. Zeeb #endif /* INET6 */ 8868a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET) 8878a006adbSBjoern A. Zeeb else 8888a006adbSBjoern A. Zeeb #endif 8898a006adbSBjoern A. Zeeb #ifdef INET 8908a006adbSBjoern A. Zeeb if (fwd_tag != NULL) { 8919b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 8929b932e9eSAndre Oppermann 8939b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 894f9e354dfSJulian Elischer /* 8952b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 896f9e354dfSJulian Elischer * already got one like this? 897f9e354dfSJulian Elischer */ 898d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport, 89908d9c920SGleb Smirnoff ip->ip_dst, th->th_dport, lookupflag, m->m_pkthdr.rcvif, m); 900fa046d87SRobert Watson if (!inp) { 901fa046d87SRobert Watson /* 902fa046d87SRobert Watson * It's new. Try to find the ambushing socket. 903d3c1f003SRobert Watson * Because we've rewritten the destination address, 904d3c1f003SRobert Watson * any hardware-generated hash is ignored. 905fa046d87SRobert Watson */ 906fa046d87SRobert Watson inp = in_pcblookup(&V_tcbinfo, ip->ip_src, 907fa046d87SRobert Watson th->th_sport, next_hop->sin_addr, 908fa046d87SRobert Watson next_hop->sin_port ? ntohs(next_hop->sin_port) : 90908d9c920SGleb Smirnoff th->th_dport, INPLOOKUP_WILDCARD | lookupflag, 91008d9c920SGleb Smirnoff m->m_pkthdr.rcvif); 911f9e354dfSJulian Elischer } 912b10fbdeaSAndre Oppermann } else 913d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, 914fa046d87SRobert Watson th->th_sport, ip->ip_dst, th->th_dport, 91508d9c920SGleb Smirnoff INPLOOKUP_WILDCARD | lookupflag, m->m_pkthdr.rcvif, m); 9168a006adbSBjoern A. Zeeb #endif /* INET */ 917f9e354dfSJulian Elischer 918df8bae1dSRodney W. Grimes /* 919574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 920574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 9218b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 922df8bae1dSRodney W. Grimes */ 923816a3d83SPoul-Henning Kamp if (inp == NULL) { 924574b6964SAndre Oppermann /* 925574b6964SAndre Oppermann * Log communication attempts to ports that are not 926574b6964SAndre Oppermann * in use. 927574b6964SAndre Oppermann */ 928334fc582SBjoern A. Zeeb if ((V_tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 929334fc582SBjoern A. Zeeb V_tcp_log_in_vain == 2) { 930b7d747ecSAndre Oppermann if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6))) 931df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 932df541e5fSAndre Oppermann "to closed port\n", s, __func__); 9332e4e1b4cSGeoff Rehmet } 934574b6964SAndre Oppermann /* 935574b6964SAndre Oppermann * When blackholing do not respond with a RST but 936574b6964SAndre Oppermann * completely ignore the segment and drop it. 937574b6964SAndre Oppermann */ 938603724d3SBjoern A. Zeeb if ((V_blackhole == 1 && (thflags & TH_SYN)) || 939603724d3SBjoern A. Zeeb V_blackhole == 2) 9401929eae1SAndre Oppermann goto dropunlock; 941574b6964SAndre Oppermann 942a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 943a57815efSBosko Milekic goto dropwithreset; 944816a3d83SPoul-Henning Kamp } 94508d9c920SGleb Smirnoff INP_LOCK_ASSERT(inp); 946f5cf1e5fSJulien Charbon /* 947f5cf1e5fSJulien Charbon * While waiting for inp lock during the lookup, another thread 948f5cf1e5fSJulien Charbon * can have dropped the inpcb, in which case we need to loop back 949f5cf1e5fSJulien Charbon * and try to find a new inpcb to deliver to. 950f5cf1e5fSJulien Charbon */ 951f5cf1e5fSJulien Charbon if (inp->inp_flags & INP_DROPPED) { 95208d9c920SGleb Smirnoff INP_UNLOCK(inp); 953f5cf1e5fSJulien Charbon inp = NULL; 954f5cf1e5fSJulien Charbon goto findpcb; 955f5cf1e5fSJulien Charbon } 956c2529042SHans Petter Selasky if ((inp->inp_flowtype == M_HASHTYPE_NONE) && 957c2529042SHans Petter Selasky (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) && 958c2529042SHans Petter Selasky ((inp->inp_socket == NULL) || 959c2529042SHans Petter Selasky (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0)) { 96080cb9f21SKip Macy inp->inp_flowid = m->m_pkthdr.flowid; 9612f719932SAdrian Chadd inp->inp_flowtype = M_HASHTYPE_GET(m); 96280cb9f21SKip Macy } 963fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 964a2589465SKen Smith #ifdef INET6 965fcf59617SAndrey V. Elsukov if (isipv6 && IPSEC_ENABLED(ipv6) && 966fcf59617SAndrey V. Elsukov IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) { 967a2589465SKen Smith goto dropunlock; 968a2589465SKen Smith } 969fcf59617SAndrey V. Elsukov #ifdef INET 970fcf59617SAndrey V. Elsukov else 971fcf59617SAndrey V. Elsukov #endif 972fcf59617SAndrey V. Elsukov #endif /* INET6 */ 973fcf59617SAndrey V. Elsukov #ifdef INET 974fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4) && 975fcf59617SAndrey V. Elsukov IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) { 976fcf59617SAndrey V. Elsukov goto dropunlock; 977fcf59617SAndrey V. Elsukov } 978fcf59617SAndrey V. Elsukov #endif /* INET */ 979a2589465SKen Smith #endif /* IPSEC */ 980a2589465SKen Smith 9818d573cc1SAndre Oppermann /* 9828d573cc1SAndre Oppermann * Check the minimum TTL for socket. 9838d573cc1SAndre Oppermann */ 98434f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 98534f83c52SGeorge V. Neville-Neil #ifdef INET6 9862d8868dbSJonathan T. Looney if (isipv6) { 9872d8868dbSJonathan T. Looney if (inp->inp_ip_minttl > ip6->ip6_hlim) 988302ce8d6SAndre Oppermann goto dropunlock; 9892d8868dbSJonathan T. Looney } else 99034f83c52SGeorge V. Neville-Neil #endif 99134f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 992302ce8d6SAndre Oppermann goto dropunlock; 99334f83c52SGeorge V. Neville-Neil } 994936cd18dSAndre Oppermann 995340c35deSJonathan Lemon /* 996252ca428SRobert Watson * A previous connection in TIMEWAIT state is supposed to catch stray 997252ca428SRobert Watson * or duplicate segments arriving late. If this segment was a 9980989f56cSRobert Watson * legitimate new connection attempt, the old INPCB gets removed and 999252ca428SRobert Watson * we can try again to find a listening socket. 1000340c35deSJonathan Lemon */ 1001ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 1002283c76c7SMichael Tuexen tcp_dooptions(&to, optp, optlen, 1003283c76c7SMichael Tuexen (thflags & TH_SYN) ? TO_SYN : 0); 10048d573cc1SAndre Oppermann /* 10058d573cc1SAndre Oppermann * NB: tcp_twcheck unlocks the INP and frees the mbuf. 10068d573cc1SAndre Oppermann */ 10072104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 1008340c35deSJonathan Lemon goto findpcb; 10098f5a8818SKevin Lo return (IPPROTO_DONE); 1010340c35deSJonathan Lemon } 1011794235b7SAndre Oppermann /* 1012794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 1013794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 1014794235b7SAndre Oppermann * segment and send an appropriate response. 1015794235b7SAndre Oppermann */ 1016df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 1017a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 1018a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 1019a57815efSBosko Milekic goto dropwithreset; 102009f81a46SBosko Milekic } 1021df8bae1dSRodney W. Grimes 10229e644c23SMichael Tuexen if ((tp->t_port != port) && (tp->t_state > TCPS_LISTEN)) { 10239e644c23SMichael Tuexen rstreason = BANDLIM_RST_CLOSEDPORT; 10249e644c23SMichael Tuexen goto dropwithreset; 10259e644c23SMichael Tuexen } 10269e644c23SMichael Tuexen 102709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 102809fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 102909fe6320SNavdeep Parhar tcp_offload_input(tp, m); 103009fe6320SNavdeep Parhar m = NULL; /* consumed by the TOE driver */ 103109fe6320SNavdeep Parhar goto dropunlock; 103209fe6320SNavdeep Parhar } 103309fe6320SNavdeep Parhar #endif 103409fe6320SNavdeep Parhar 1035c488362eSRobert Watson #ifdef MAC 103630d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 1037302ce8d6SAndre Oppermann goto dropunlock; 1038c488362eSRobert Watson #endif 1039a557af22SRobert Watson so = inp->inp_socket; 1040302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 1041610ee2f9SDavid Greenman #ifdef TCPDEBUG 1042df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 1043df8bae1dSRodney W. Grimes ostate = tp->t_state; 104410fe523eSMaxim Konovalov #ifdef INET6 10456f697424SBjoern A. Zeeb if (isipv6) { 1046540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 1047d30d90dcSMaxim Konovalov } else 10486f697424SBjoern A. Zeeb #endif 1049540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 1050fb59c426SYoshinobu Inoue tcp_savetcp = *th; 1051df8bae1dSRodney W. Grimes } 1052b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */ 1053db33b3e6SAndre Oppermann /* 1054db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 1055db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 1056a7c7f2a7SJohn Baldwin * attempt or the completion of a previous one. 1057db33b3e6SAndre Oppermann */ 105845274760SJonathan T. Looney KASSERT(tp->t_state == TCPS_LISTEN || !(so->so_options & SO_ACCEPTCONN), 105945274760SJonathan T. Looney ("%s: so accepting but tp %p not listening", __func__, tp)); 106045274760SJonathan T. Looney if (tp->t_state == TCPS_LISTEN && (so->so_options & SO_ACCEPTCONN)) { 1061540e8b7eSJeffrey Hsu struct in_conninfo inc; 1062540e8b7eSJeffrey Hsu 10638bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 1064302ce8d6SAndre Oppermann #ifdef INET6 1065be2ac88cSJonathan Lemon if (isipv6) { 1066dcdb4371SBjoern A. Zeeb inc.inc_flags |= INC_ISIPV6; 10675dff1c38SMichael Tuexen if (inp->inp_inc.inc_flags & INC_IPV6MINMTU) 10685dff1c38SMichael Tuexen inc.inc_flags |= INC_IPV6MINMTU; 1069be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 1070be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 1071302ce8d6SAndre Oppermann } else 1072302ce8d6SAndre Oppermann #endif 1073302ce8d6SAndre Oppermann { 1074be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 1075be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 1076be2ac88cSJonathan Lemon } 1077be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 1078be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 10797973fba3SJulian Elischer inc.inc_fibnum = so->so_fibnum; 1080be2ac88cSJonathan Lemon 1081fb59c426SYoshinobu Inoue /* 10828d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 10838d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 10848d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 1085fb59c426SYoshinobu Inoue */ 1086be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 1087bf6d304aSAndre Oppermann /* 1088bf6d304aSAndre Oppermann * Parse the TCP options here because 1089bf6d304aSAndre Oppermann * syncookies need access to the reflected 1090bf6d304aSAndre Oppermann * timestamp. 1091bf6d304aSAndre Oppermann */ 1092bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 10939fa198beSAndre Oppermann /* 10949fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 10959fa198beSAndre Oppermann * inp and tcpinfo locks. 10969fa198beSAndre Oppermann */ 10979e644c23SMichael Tuexen rstreason = syncache_expand(&inc, &to, th, &so, m, port); 1098fcf59617SAndrey V. Elsukov if (rstreason < 0) { 1099fcf59617SAndrey V. Elsukov /* 1100fcf59617SAndrey V. Elsukov * A failing TCP MD5 signature comparison 1101fcf59617SAndrey V. Elsukov * must result in the segment being dropped 1102fcf59617SAndrey V. Elsukov * and must not produce any response back 1103fcf59617SAndrey V. Elsukov * to the sender. 1104fcf59617SAndrey V. Elsukov */ 1105fcf59617SAndrey V. Elsukov goto dropunlock; 1106fcf59617SAndrey V. Elsukov } else if (rstreason == 0) { 11074195b4afSPaul Traina /* 1108e207f800SAndre Oppermann * No syncache entry or ACK was not 1109be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 11108d573cc1SAndre Oppermann * NB: syncache did its own logging 11118d573cc1SAndre Oppermann * of the failure cause. 11124195b4afSPaul Traina */ 1113be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 1114be2ac88cSJonathan Lemon goto dropwithreset; 1115be2ac88cSJonathan Lemon } 111609c305ebSPatrick Kelsey tfo_socket_result: 1117f76fcf6dSJeffrey Hsu if (so == NULL) { 1118be2ac88cSJonathan Lemon /* 1119e207f800SAndre Oppermann * We completed the 3-way handshake 1120e207f800SAndre Oppermann * but could not allocate a socket 1121e207f800SAndre Oppermann * either due to memory shortage, 1122e207f800SAndre Oppermann * listen queue length limits or 1123a160e630SAndre Oppermann * global socket limits. Send RST 1124a160e630SAndre Oppermann * or wait and have the remote end 1125a160e630SAndre Oppermann * retransmit the ACK for another 1126a160e630SAndre Oppermann * try. 1127be2ac88cSJonathan Lemon */ 1128a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1129a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11308d573cc1SAndre Oppermann "Socket allocation failed due to " 11318d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 1132ac957cd2SJulian Elischer s, __func__, 1133ac957cd2SJulian Elischer V_tcp_sc_rst_sock_fail ? 1134ac957cd2SJulian Elischer "sending RST" : "try again"); 1135603724d3SBjoern A. Zeeb if (V_tcp_sc_rst_sock_fail) { 1136e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 1137e207f800SAndre Oppermann goto dropwithreset; 1138a160e630SAndre Oppermann } else 1139a160e630SAndre Oppermann goto dropunlock; 1140f76fcf6dSJeffrey Hsu } 1141be2ac88cSJonathan Lemon /* 1142be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 11438d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 11448d573cc1SAndre Oppermann * created socket and update the tp variable. 114508d9c920SGleb Smirnoff * If we came here via jump to tfo_socket_result, 114608d9c920SGleb Smirnoff * then listening socket is read-locked. 1147be2ac88cSJonathan Lemon */ 114808d9c920SGleb Smirnoff INP_UNLOCK(inp); /* listen socket */ 1149be2ac88cSJonathan Lemon inp = sotoinpcb(so); 1150ff9b006dSJulien Charbon /* 1151ff9b006dSJulien Charbon * New connection inpcb is already locked by 1152ff9b006dSJulien Charbon * syncache_expand(). 1153ff9b006dSJulien Charbon */ 1154ff9b006dSJulien Charbon INP_WLOCK_ASSERT(inp); 1155be2ac88cSJonathan Lemon tp = intotcpcb(inp); 11568d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 11578d573cc1SAndre Oppermann ("%s: ", __func__)); 1158be2ac88cSJonathan Lemon /* 1159302ce8d6SAndre Oppermann * Process the segment and the data it 1160302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 1161302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 1162302ce8d6SAndre Oppermann */ 11636138da62SMichael Tuexen TCP_PROBE5(receive, NULL, tp, m, tp, th); 116455bceb1eSRandall Stewart tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 11656573d758SMatt Macy iptos); 11668f5a8818SKevin Lo return (IPPROTO_DONE); 1167be2ac88cSJonathan Lemon } 1168a160e630SAndre Oppermann /* 11698d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 11708d573cc1SAndre Oppermann * 1171a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 1172a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 1173a160e630SAndre Oppermann * retransmits. 117419bc77c5SAndre Oppermann * 117519bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 117619bc77c5SAndre Oppermann * causes. 1177a160e630SAndre Oppermann */ 1178a160e630SAndre Oppermann if (thflags & TH_RST) { 11799e644c23SMichael Tuexen syncache_chkrst(&inc, th, m, port); 1180a160e630SAndre Oppermann goto dropunlock; 1181a160e630SAndre Oppermann } 1182a160e630SAndre Oppermann /* 1183a160e630SAndre Oppermann * We can't do anything without SYN. 1184a160e630SAndre Oppermann */ 1185a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 1186a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1187a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1188773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 11898d573cc1SAndre Oppermann s, __func__); 119078b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1191a160e630SAndre Oppermann goto dropunlock; 1192a160e630SAndre Oppermann } 1193a160e630SAndre Oppermann /* 1194a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 1195a160e630SAndre Oppermann */ 1196fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1197a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1198a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11998d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 12008d573cc1SAndre Oppermann s, __func__); 12019e644c23SMichael Tuexen syncache_badack(&inc, port); /* XXX: Not needed! */ 120278b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1203a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1204a57815efSBosko Milekic goto dropwithreset; 12054195b4afSPaul Traina } 1206a160e630SAndre Oppermann /* 1207a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 1208a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 1209a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 1210a160e630SAndre Oppermann * TCP/IP stack. 1211a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 1212a160e630SAndre Oppermann * and is constantly refining its stack detection 1213a160e630SAndre Oppermann * strategies. 12148d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 1215a160e630SAndre Oppermann * and was used by RFC1644. 1216a160e630SAndre Oppermann */ 1217603724d3SBjoern A. Zeeb if ((thflags & TH_FIN) && V_drop_synfin) { 1218a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1219a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1220773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 12218d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 122278b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1223302ce8d6SAndre Oppermann goto dropunlock; 1224ebb0cbeaSPaul Traina } 1225be2ac88cSJonathan Lemon /* 1226be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 1227a160e630SAndre Oppermann * 1228a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 1229a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 1230a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 1231be2ac88cSJonathan Lemon */ 1232a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 1233a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 1234a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 1235a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 123608d9c920SGleb Smirnoff INP_RLOCK_ASSERT(inp); 123733841545SHajimu UMEMOTO #ifdef INET6 123833841545SHajimu UMEMOTO /* 123933841545SHajimu UMEMOTO * If deprecated address is forbidden, 124033841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 124133841545SHajimu UMEMOTO * address to prevent any new inbound connection from 124233841545SHajimu UMEMOTO * getting established. 124333841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 124433841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 124533841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 124633841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 124733841545SHajimu UMEMOTO * for the exchange. 124833841545SHajimu UMEMOTO * 124933841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 125033841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 125133841545SHajimu UMEMOTO * SYN in this case. 125233841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 125333841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 125433841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 125533841545SHajimu UMEMOTO * used" 125633841545SHajimu UMEMOTO * 2. use of it with new communication: 125733841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 125833841545SHajimu UMEMOTO * with sufficient scope is available" 125933841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 126033841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 126133841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 126233841545SHajimu UMEMOTO * 126333841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 126433841545SHajimu UMEMOTO * multiple description text for deprecated address 126533841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 126633841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 126733841545SHajimu UMEMOTO */ 1268603724d3SBjoern A. Zeeb if (isipv6 && !V_ip6_use_deprecated) { 126933841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 127033841545SHajimu UMEMOTO 12718268d82cSAlexander V. Chernikov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false); 12728c0fec80SRobert Watson if (ia6 != NULL && 127333841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 1274a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1275a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12768d573cc1SAndre Oppermann "Connection attempt to deprecated " 12778d573cc1SAndre Oppermann "IPv6 address rejected\n", 1278a160e630SAndre Oppermann s, __func__); 127933841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 128033841545SHajimu UMEMOTO goto dropwithreset; 128133841545SHajimu UMEMOTO } 128233841545SHajimu UMEMOTO } 1283b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 128465f28919SJesper Skriver /* 1285db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 12868d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 1287db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 12888d573cc1SAndre Oppermann * If it is from this socket it must be forged. 12898d573cc1SAndre Oppermann * Don't respond if the source or destination is a 12908d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 129193ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 129293ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 129393ec91baSCrist J. Clark * in_broadcast() to find them. 1294be2ac88cSJonathan Lemon */ 12958d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 12968d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 12978d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12988d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 1299773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 1300302ce8d6SAndre Oppermann goto dropunlock; 13018d573cc1SAndre Oppermann } 1302db33b3e6SAndre Oppermann #ifdef INET6 1303b287c6c7SBjoern A. Zeeb if (isipv6) { 1304db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1305a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 1306a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1307a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13088d573cc1SAndre Oppermann "Connection attempt to/from self " 1309773673c1SAndre Oppermann "ignored\n", s, __func__); 1310302ce8d6SAndre Oppermann goto dropunlock; 1311a160e630SAndre Oppermann } 1312be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1313a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 1314a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1315a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13168d573cc1SAndre Oppermann "Connection attempt from/to multicast " 1317773673c1SAndre Oppermann "address ignored\n", s, __func__); 1318302ce8d6SAndre Oppermann goto dropunlock; 1319a160e630SAndre Oppermann } 1320b287c6c7SBjoern A. Zeeb } 1321db33b3e6SAndre Oppermann #endif 1322b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 1323b287c6c7SBjoern A. Zeeb else 1324b287c6c7SBjoern A. Zeeb #endif 1325b287c6c7SBjoern A. Zeeb #ifdef INET 1326b287c6c7SBjoern A. Zeeb { 1327db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1328a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 1329a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1330a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13318d573cc1SAndre Oppermann "Connection attempt from/to self " 1332773673c1SAndre Oppermann "ignored\n", s, __func__); 1333302ce8d6SAndre Oppermann goto dropunlock; 1334a160e630SAndre Oppermann } 1335be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1336be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 13372ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 1338a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 1339a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1340a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13418d573cc1SAndre Oppermann "Connection attempt from/to broad- " 1342773673c1SAndre Oppermann "or multicast address ignored\n", 1343a160e630SAndre Oppermann s, __func__); 1344302ce8d6SAndre Oppermann goto dropunlock; 1345c068736aSJeffrey Hsu } 1346a160e630SAndre Oppermann } 1347b287c6c7SBjoern A. Zeeb #endif 1348be2ac88cSJonathan Lemon /* 1349db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 1350db33b3e6SAndre Oppermann * for syncache. 1351be2ac88cSJonathan Lemon */ 13523c653157SHartmut Brandt #ifdef TCPDEBUG 13533c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 13543c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 13553c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 13563c653157SHartmut Brandt #endif 13572b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 1358f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 13598d5719aaSGleb Smirnoff if ((so = syncache_add(&inc, &to, th, inp, so, m, NULL, NULL, 13609e644c23SMichael Tuexen iptos, port)) != NULL) 136109c305ebSPatrick Kelsey goto tfo_socket_result; 136218a75309SPatrick Kelsey 1363be2ac88cSJonathan Lemon /* 13644d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 1365a7c7f2a7SJohn Baldwin * Only the listen socket is unlocked by syncache_add(). 1366be2ac88cSJonathan Lemon */ 13678f5a8818SKevin Lo return (IPPROTO_DONE); 1368982c1675SAndre Oppermann } else if (tp->t_state == TCPS_LISTEN) { 1369982c1675SAndre Oppermann /* 1370982c1675SAndre Oppermann * When a listen socket is torn down the SO_ACCEPTCONN 1371982c1675SAndre Oppermann * flag is removed first while connections are drained 1372982c1675SAndre Oppermann * from the accept queue in a unlock/lock cycle of the 1373982c1675SAndre Oppermann * ACCEPT_LOCK, opening a race condition allowing a SYN 1374982c1675SAndre Oppermann * attempt go through unhandled. 1375982c1675SAndre Oppermann */ 1376982c1675SAndre Oppermann goto dropunlock; 1377f76fcf6dSJeffrey Hsu } 1378fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1379fcf59617SAndrey V. Elsukov if (tp->t_flags & TF_SIGNATURE) { 1380fcf59617SAndrey V. Elsukov tcp_dooptions(&to, optp, optlen, thflags); 1381fcf59617SAndrey V. Elsukov if ((to.to_flags & TOF_SIGNATURE) == 0) { 1382fcf59617SAndrey V. Elsukov TCPSTAT_INC(tcps_sig_err_nosigopt); 13832903309aSAttilio Rao goto dropunlock; 13842903309aSAttilio Rao } 1385fcf59617SAndrey V. Elsukov if (!TCPMD5_ENABLED() || 1386fcf59617SAndrey V. Elsukov TCPMD5_INPUT(m, th, to.to_signature) != 0) 1387fcf59617SAndrey V. Elsukov goto dropunlock; 13882903309aSAttilio Rao } 13892903309aSAttilio Rao #endif 13902b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 139157f60867SMark Johnston 1392302ce8d6SAndre Oppermann /* 13938d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 13948d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 13958d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 13961db08fbeSGleb Smirnoff * 13971db08fbeSGleb Smirnoff * XXXGL: in case of a pure SYN arriving on existing connection 13981db08fbeSGleb Smirnoff * TCP stacks won't need to modify the PCB, they would either drop 13991db08fbeSGleb Smirnoff * the segment silently, or send a challenge ACK. However, we try 14001db08fbeSGleb Smirnoff * to upgrade the lock, because calling convention for stacks is 14011db08fbeSGleb Smirnoff * write-lock on PCB. If upgrade fails, drop the SYN. 1402302ce8d6SAndre Oppermann */ 14031db08fbeSGleb Smirnoff if (lookupflag == INPLOOKUP_RLOCKPCB && INP_TRY_UPGRADE(inp) == 0) 14041db08fbeSGleb Smirnoff goto dropunlock; 14051db08fbeSGleb Smirnoff 14066573d758SMatt Macy tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos); 14078f5a8818SKevin Lo return (IPPROTO_DONE); 1408be2ac88cSJonathan Lemon 1409302ce8d6SAndre Oppermann dropwithreset: 14102b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 141157f60867SMark Johnston 1412014ea782SRobert Watson if (inp != NULL) { 1413302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 141408d9c920SGleb Smirnoff INP_UNLOCK(inp); 1415dd8ac7f9SRobert Watson } else 1416014ea782SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1417302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 1418014ea782SRobert Watson goto drop; 1419014ea782SRobert Watson 1420302ce8d6SAndre Oppermann dropunlock: 142157f60867SMark Johnston if (m != NULL) 14222b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 142357f60867SMark Johnston 14249fa198beSAndre Oppermann if (inp != NULL) 142508d9c920SGleb Smirnoff INP_UNLOCK(inp); 1426014ea782SRobert Watson 1427302ce8d6SAndre Oppermann drop: 1428a160e630SAndre Oppermann if (s != NULL) 1429a160e630SAndre Oppermann free(s, M_TCPLOG); 1430302ce8d6SAndre Oppermann if (m != NULL) 1431302ce8d6SAndre Oppermann m_freem(m); 14328f5a8818SKevin Lo return (IPPROTO_DONE); 1433302ce8d6SAndre Oppermann } 1434302ce8d6SAndre Oppermann 1435e44c1887SSteven Hartland /* 1436e44c1887SSteven Hartland * Automatic sizing of receive socket buffer. Often the send 1437e44c1887SSteven Hartland * buffer size is not optimally adjusted to the actual network 1438e44c1887SSteven Hartland * conditions at hand (delay bandwidth product). Setting the 1439e44c1887SSteven Hartland * buffer size too small limits throughput on links with high 1440e44c1887SSteven Hartland * bandwidth and high delay (eg. trans-continental/oceanic links). 1441e44c1887SSteven Hartland * 1442e44c1887SSteven Hartland * On the receive side the socket buffer memory is only rarely 1443e44c1887SSteven Hartland * used to any significant extent. This allows us to be much 1444e44c1887SSteven Hartland * more aggressive in scaling the receive socket buffer. For 1445e44c1887SSteven Hartland * the case that the buffer space is actually used to a large 1446e44c1887SSteven Hartland * extent and we run out of kernel memory we can simply drop 1447e44c1887SSteven Hartland * the new segments; TCP on the sender will just retransmit it 1448e44c1887SSteven Hartland * later. Setting the buffer size too big may only consume too 1449e44c1887SSteven Hartland * much kernel memory if the application doesn't read() from 1450e44c1887SSteven Hartland * the socket or packet loss or reordering makes use of the 1451e44c1887SSteven Hartland * reassembly queue. 1452e44c1887SSteven Hartland * 1453e44c1887SSteven Hartland * The criteria to step up the receive buffer one notch are: 1454e44c1887SSteven Hartland * 1. Application has not set receive buffer size with 1455e44c1887SSteven Hartland * SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE. 1456560c0586SMichael Tuexen * 2. the number of bytes received during 1/2 of an sRTT 1457560c0586SMichael Tuexen * is at least 3/8 of the current socket buffer size. 1458560c0586SMichael Tuexen * 3. receive buffer size has not hit maximal automatic size; 1459e44c1887SSteven Hartland * 1460560c0586SMichael Tuexen * If all of the criteria are met we increaset the socket buffer 1461560c0586SMichael Tuexen * by a 1/2 (bounded by the max). This allows us to keep ahead 1462560c0586SMichael Tuexen * of slow-start but also makes it so our peer never gets limited 1463560c0586SMichael Tuexen * by our rwnd which we then open up causing a burst. 1464560c0586SMichael Tuexen * 1465560c0586SMichael Tuexen * This algorithm does two steps per RTT at most and only if 1466e44c1887SSteven Hartland * we receive a bulk stream w/o packet losses or reorderings. 1467e44c1887SSteven Hartland * Shrinking the buffer during idle times is not necessary as 1468e44c1887SSteven Hartland * it doesn't consume any memory when idle. 1469e44c1887SSteven Hartland * 1470e44c1887SSteven Hartland * TODO: Only step up if the application is actually serving 1471e44c1887SSteven Hartland * the buffer to better manage the socket buffer resources. 1472e44c1887SSteven Hartland */ 1473e44c1887SSteven Hartland int 1474e44c1887SSteven Hartland tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so, 1475e44c1887SSteven Hartland struct tcpcb *tp, int tlen) 1476e44c1887SSteven Hartland { 1477e44c1887SSteven Hartland int newsize = 0; 1478e44c1887SSteven Hartland 1479e44c1887SSteven Hartland if (V_tcp_do_autorcvbuf && (so->so_rcv.sb_flags & SB_AUTOSIZE) && 1480e44c1887SSteven Hartland tp->t_srtt != 0 && tp->rfbuf_ts != 0 && 1481e44c1887SSteven Hartland TCP_TS_TO_TICKS(tcp_ts_getticks() - tp->rfbuf_ts) > 1482560c0586SMichael Tuexen ((tp->t_srtt >> TCP_RTT_SHIFT)/2)) { 1483560c0586SMichael Tuexen if (tp->rfbuf_cnt > ((so->so_rcv.sb_hiwat / 2)/ 4 * 3) && 1484e44c1887SSteven Hartland so->so_rcv.sb_hiwat < V_tcp_autorcvbuf_max) { 1485560c0586SMichael Tuexen newsize = min((so->so_rcv.sb_hiwat + (so->so_rcv.sb_hiwat/2)), V_tcp_autorcvbuf_max); 1486e44c1887SSteven Hartland } 1487e44c1887SSteven Hartland TCP_PROBE6(receive__autoresize, NULL, tp, m, tp, th, newsize); 1488e44c1887SSteven Hartland 1489e44c1887SSteven Hartland /* Start over with next RTT. */ 1490e44c1887SSteven Hartland tp->rfbuf_ts = 0; 1491e44c1887SSteven Hartland tp->rfbuf_cnt = 0; 1492e44c1887SSteven Hartland } else { 1493e44c1887SSteven Hartland tp->rfbuf_cnt += tlen; /* add up */ 1494e44c1887SSteven Hartland } 1495e44c1887SSteven Hartland return (newsize); 1496e44c1887SSteven Hartland } 1497e44c1887SSteven Hartland 14989e644c23SMichael Tuexen int 14999e644c23SMichael Tuexen tcp_input(struct mbuf **mp, int *offp, int proto) 15009e644c23SMichael Tuexen { 15019e644c23SMichael Tuexen return(tcp_input_with_port(mp, offp, proto, 0)); 15029e644c23SMichael Tuexen } 15039e644c23SMichael Tuexen 150455bceb1eSRandall Stewart void 15054d0770f1SRichard Scheffenegger tcp_handle_wakeup(struct tcpcb *tp, struct socket *so) 15064d0770f1SRichard Scheffenegger { 15074d0770f1SRichard Scheffenegger /* 15084d0770f1SRichard Scheffenegger * Since tp might be gone if the session entered 15094d0770f1SRichard Scheffenegger * the TIME_WAIT state before coming here, we need 15104d0770f1SRichard Scheffenegger * to check if the socket is still connected. 15114d0770f1SRichard Scheffenegger */ 1512*032bf749SRichard Scheffenegger if (tp == NULL) { 15134d0770f1SRichard Scheffenegger return; 1514*032bf749SRichard Scheffenegger } 1515*032bf749SRichard Scheffenegger if (so == NULL) { 1516*032bf749SRichard Scheffenegger return; 1517*032bf749SRichard Scheffenegger } 15184d0770f1SRichard Scheffenegger INP_LOCK_ASSERT(tp->t_inpcb); 15194d0770f1SRichard Scheffenegger if (tp->t_flags & TF_WAKESOR) { 15204d0770f1SRichard Scheffenegger tp->t_flags &= ~TF_WAKESOR; 1521*032bf749SRichard Scheffenegger SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1522*032bf749SRichard Scheffenegger sorwakeup_locked(so); 15234d0770f1SRichard Scheffenegger } 15244d0770f1SRichard Scheffenegger } 15254d0770f1SRichard Scheffenegger 15264d0770f1SRichard Scheffenegger void 1527302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 15286573d758SMatt Macy struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos) 1529302ce8d6SAndre Oppermann { 1530021eaf79SHiren Panchasara int thflags, acked, ourfinisacked, needoutput = 0, sack_changed; 1531b2ade6b1SRichard Scheffenegger int rstreason, todrop, win, incforsyn = 0; 15323ac12506SJonathan T. Looney uint32_t tiwin; 15334b7b743cSLawrence Stewart uint16_t nsegs; 153407dacf03SAndre Oppermann char *s; 153507dacf03SAndre Oppermann struct in_conninfo *inc; 1536c11a15bfSGleb Smirnoff struct mbuf *mfree; 1537302ce8d6SAndre Oppermann struct tcpopt to; 1538281a0fd4SPatrick Kelsey int tfo_syn; 15393c40e1d5SRichard Scheffenegger u_int maxseg; 1540302ce8d6SAndre Oppermann 154114739780SMaxim Konovalov #ifdef TCPDEBUG 154214739780SMaxim Konovalov /* 154314739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 154414739780SMaxim Konovalov * now IPv6. 154514739780SMaxim Konovalov */ 154614739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 154714739780SMaxim Konovalov struct tcphdr tcp_savetcp; 154814739780SMaxim Konovalov short ostate = 0; 154914739780SMaxim Konovalov #endif 1550302ce8d6SAndre Oppermann thflags = th->th_flags; 155107dacf03SAndre Oppermann inc = &tp->t_inpcb->inp_inc; 1552d64a46eaSLawrence Stewart tp->sackhint.last_sack_ack = 0; 1553021eaf79SHiren Panchasara sack_changed = 0; 15544b7b743cSLawrence Stewart nsegs = max(1, m->m_pkthdr.lro_nsegs); 1555d40c0d47SGleb Smirnoff 1556d40c0d47SGleb Smirnoff NET_EPOCH_ASSERT(); 15578501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1558679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1559679d9708SAndre Oppermann __func__)); 1560679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1561679d9708SAndre Oppermann __func__)); 1562df8bae1dSRodney W. Grimes 156386a996e6SHiren Panchasara #ifdef TCPPCAP 156486a996e6SHiren Panchasara /* Save segment, if requested. */ 156586a996e6SHiren Panchasara tcp_pcap_add(th, m, &(tp->t_inpkts)); 156686a996e6SHiren Panchasara #endif 15672529f56eSJonathan T. Looney TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0, 15682529f56eSJonathan T. Looney tlen, NULL, true); 156986a996e6SHiren Panchasara 1570013f4df6SMichael Tuexen if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) { 1571013f4df6SMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 1572013f4df6SMichael Tuexen log(LOG_DEBUG, "%s; %s: " 1573013f4df6SMichael Tuexen "SYN|FIN segment ignored (based on " 1574013f4df6SMichael Tuexen "sysctl setting)\n", s, __func__); 1575013f4df6SMichael Tuexen free(s, M_TCPLOG); 1576013f4df6SMichael Tuexen } 1577013f4df6SMichael Tuexen goto drop; 1578013f4df6SMichael Tuexen } 1579013f4df6SMichael Tuexen 1580df8bae1dSRodney W. Grimes /* 1581ebfd7534SMichael Tuexen * If a segment with the ACK-bit set arrives in the SYN-SENT state 1582ebfd7534SMichael Tuexen * check SEQ.ACK first. 1583ebfd7534SMichael Tuexen */ 1584ebfd7534SMichael Tuexen if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) && 1585ebfd7534SMichael Tuexen (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) { 1586ebfd7534SMichael Tuexen rstreason = BANDLIM_UNLIMITED; 1587ebfd7534SMichael Tuexen goto dropwithreset; 1588ebfd7534SMichael Tuexen } 1589ebfd7534SMichael Tuexen 1590ebfd7534SMichael Tuexen /* 1591df8bae1dSRodney W. Grimes * Segment received on connection. 1592df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1593e8949f74SAndre Oppermann * XXX: This should be done after segment 1594e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 1595df8bae1dSRodney W. Grimes */ 15969b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 1597df8bae1dSRodney W. Grimes 1598df8bae1dSRodney W. Grimes /* 1599c52102ddSHiren Panchasara * Scale up the window into a 32-bit value. 1600e8949f74SAndre Oppermann * For the SYN_SENT state the scale is zero. 1601464fcfbcSAndre Oppermann */ 1602464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 1603adc56f5aSEdward Tomasz Napierala #ifdef STATS 1604adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin); 1605adc56f5aSEdward Tomasz Napierala #endif 1606464fcfbcSAndre Oppermann 1607464fcfbcSAndre Oppermann /* 1608f2512ba1SRui Paulo * TCP ECN processing. 1609f2512ba1SRui Paulo */ 16103cf38784SMichael Tuexen if (tp->t_flags2 & TF2_ECN_PERMIT) { 1611ee97681eSMichael Tuexen if (thflags & TH_CWR) { 16123cf38784SMichael Tuexen tp->t_flags2 &= ~TF2_ECN_SND_ECE; 1613ee97681eSMichael Tuexen tp->t_flags |= TF_ACKNOW; 1614ee97681eSMichael Tuexen } 1615f2512ba1SRui Paulo switch (iptos & IPTOS_ECN_MASK) { 1616f2512ba1SRui Paulo case IPTOS_ECN_CE: 16173cf38784SMichael Tuexen tp->t_flags2 |= TF2_ECN_SND_ECE; 161878b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ce); 1619f2512ba1SRui Paulo break; 1620f2512ba1SRui Paulo case IPTOS_ECN_ECT0: 162178b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect0); 1622f2512ba1SRui Paulo break; 1623f2512ba1SRui Paulo case IPTOS_ECN_ECT1: 162478b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect1); 1625f2512ba1SRui Paulo break; 1626f2512ba1SRui Paulo } 162764807b30SHiren Panchasara 162864807b30SHiren Panchasara /* Process a packet differently from RFC3168. */ 162964807b30SHiren Panchasara cc_ecnpkt_handler(tp, th, iptos); 163064807b30SHiren Panchasara 1631dbc42409SLawrence Stewart /* Congestion experienced. */ 1632dbc42409SLawrence Stewart if (thflags & TH_ECE) { 1633dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_ECN); 1634f2512ba1SRui Paulo } 1635f2512ba1SRui Paulo } 1636f2512ba1SRui Paulo 1637f2512ba1SRui Paulo /* 1638f72167f4SAndre Oppermann * Parse options on any incoming segment. 1639f72167f4SAndre Oppermann */ 1640302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1641302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1642302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1643f72167f4SAndre Oppermann 1644fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1645fcf59617SAndrey V. Elsukov if ((tp->t_flags & TF_SIGNATURE) != 0 && 1646fcf59617SAndrey V. Elsukov (to.to_flags & TOF_SIGNATURE) == 0) { 1647fcf59617SAndrey V. Elsukov TCPSTAT_INC(tcps_sig_err_sigopt); 1648fcf59617SAndrey V. Elsukov /* XXX: should drop? */ 1649fcf59617SAndrey V. Elsukov } 1650fcf59617SAndrey V. Elsukov #endif 1651f72167f4SAndre Oppermann /* 1652f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1653bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1654bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1655bf6d304aSAndre Oppermann * was established. 1656f72167f4SAndre Oppermann */ 1657bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1658e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 1659d8951c8aSBjoern A. Zeeb if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) 1660f72167f4SAndre Oppermann to.to_tsecr = 0; 166110d20c84SMatt Macy else if (tp->t_flags & TF_PREVVALID && 166210d20c84SMatt Macy tp->t_badrxtwin != 0 && SEQ_LT(to.to_tsecr, tp->t_badrxtwin)) 166310d20c84SMatt Macy cc_cong_signal(tp, th, CC_RTO_ERR); 1664bf6d304aSAndre Oppermann } 166507dacf03SAndre Oppermann /* 166697d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 166797d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 16685396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 16695396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 167097d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1671df8bae1dSRodney W. Grimes */ 16720a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 16736e16d877SRichard Scheffenegger /* Handle parallel SYN for ECN */ 16746e16d877SRichard Scheffenegger if (!(thflags & TH_ACK) && 16756e16d877SRichard Scheffenegger ((thflags & (TH_CWR | TH_ECE)) == (TH_CWR | TH_ECE)) && 16766e16d877SRichard Scheffenegger ((V_tcp_do_ecn == 1) || (V_tcp_do_ecn == 2))) { 16776e16d877SRichard Scheffenegger tp->t_flags2 |= TF2_ECN_PERMIT; 16786e16d877SRichard Scheffenegger tp->t_flags2 |= TF2_ECN_SND_ECE; 16796e16d877SRichard Scheffenegger TCPSTAT_INC(tcps_ecn_shs); 16806e16d877SRichard Scheffenegger } 1681464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 16822593f858SRichard Scheffenegger (tp->t_flags & TF_REQ_SCALE) && 16832593f858SRichard Scheffenegger !(tp->t_flags & TF_NOOPT)) { 1684be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 168502a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 16868e051165SRichard Scheffenegger } else 16878e051165SRichard Scheffenegger tp->t_flags &= ~TF_REQ_SCALE; 16885396d0f8SAndre Oppermann /* 16895396d0f8SAndre Oppermann * Initial send window. It will be updated with 16905396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 16915396d0f8SAndre Oppermann */ 16925396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 16938e051165SRichard Scheffenegger if ((to.to_flags & TOF_TS) && 16942593f858SRichard Scheffenegger (tp->t_flags & TF_REQ_TSTMP) && 16952593f858SRichard Scheffenegger !(tp->t_flags & TF_NOOPT)) { 1696be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1697be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1698d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 16998e051165SRichard Scheffenegger } else 17008e051165SRichard Scheffenegger tp->t_flags &= ~TF_REQ_TSTMP; 1701be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1702be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 17033529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 17042593f858SRichard Scheffenegger (!(to.to_flags & TOF_SACKPERM) || 17052593f858SRichard Scheffenegger (tp->t_flags & TF_NOOPT))) 17063529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 1707c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) { 17082593f858SRichard Scheffenegger if ((to.to_flags & TOF_FASTOPEN) && 17092593f858SRichard Scheffenegger !(tp->t_flags & TF_NOOPT)) { 1710a026a53aSMichael Tuexen uint16_t mss; 1711a026a53aSMichael Tuexen 1712a026a53aSMichael Tuexen if (to.to_flags & TOF_MSS) 1713a026a53aSMichael Tuexen mss = to.to_mss; 1714c560df6fSPatrick Kelsey else 1715a026a53aSMichael Tuexen if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) 1716a026a53aSMichael Tuexen mss = TCP6_MSS; 1717a026a53aSMichael Tuexen else 1718a026a53aSMichael Tuexen mss = TCP_MSS; 1719a026a53aSMichael Tuexen tcp_fastopen_update_cache(tp, mss, 1720a026a53aSMichael Tuexen to.to_tfo_len, to.to_tfo_cookie); 1721a026a53aSMichael Tuexen } else 1722c560df6fSPatrick Kelsey tcp_fastopen_disable_path(tp); 1723c560df6fSPatrick Kelsey } 17246d90faf3SPaul Saab } 17256d90faf3SPaul Saab 1726df8bae1dSRodney W. Grimes /* 1727283c76c7SMichael Tuexen * If timestamps were negotiated during SYN/ACK and a 1728283c76c7SMichael Tuexen * segment without a timestamp is received, silently drop 1729d2b3ceddSMichael Tuexen * the segment, unless it is a RST segment or missing timestamps are 1730d2b3ceddSMichael Tuexen * tolerated. 1731283c76c7SMichael Tuexen * See section 3.2 of RFC 7323. 17324da9052aSMichael Tuexen */ 17334da9052aSMichael Tuexen if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) { 1734d2b3ceddSMichael Tuexen if (((thflags & TH_RST) != 0) || V_tcp_tolerate_missing_ts) { 1735cc3c3485SMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 1736cc3c3485SMichael Tuexen log(LOG_DEBUG, "%s; %s: Timestamp missing, " 1737cc3c3485SMichael Tuexen "segment processed normally\n", 1738cc3c3485SMichael Tuexen s, __func__); 1739cc3c3485SMichael Tuexen free(s, M_TCPLOG); 1740cc3c3485SMichael Tuexen } 1741cc3c3485SMichael Tuexen } else { 17424da9052aSMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 17434da9052aSMichael Tuexen log(LOG_DEBUG, "%s; %s: Timestamp missing, " 1744283c76c7SMichael Tuexen "segment silently dropped\n", s, __func__); 17454da9052aSMichael Tuexen free(s, M_TCPLOG); 17464da9052aSMichael Tuexen } 1747283c76c7SMichael Tuexen goto drop; 17484da9052aSMichael Tuexen } 1749cc3c3485SMichael Tuexen } 1750283c76c7SMichael Tuexen /* 1751283c76c7SMichael Tuexen * If timestamps were not negotiated during SYN/ACK and a 175275fcd27aSMichael Tuexen * segment with a timestamp is received, ignore the 1753283c76c7SMichael Tuexen * timestamp and process the packet normally. 1754283c76c7SMichael Tuexen * See section 3.2 of RFC 7323. 1755283c76c7SMichael Tuexen */ 17564da9052aSMichael Tuexen if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) { 17574da9052aSMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 17584da9052aSMichael Tuexen log(LOG_DEBUG, "%s; %s: Timestamp not expected, " 1759283c76c7SMichael Tuexen "segment processed normally\n", s, __func__); 17604da9052aSMichael Tuexen free(s, M_TCPLOG); 17614da9052aSMichael Tuexen } 17624da9052aSMichael Tuexen } 17634da9052aSMichael Tuexen 17644da9052aSMichael Tuexen /* 1765df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1766df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1767df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1768df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1769df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1770df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1771df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1772df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1773df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1774df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1775df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1776df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1777a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1778c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 1779a0292f23SGarrett Wollman * be TH_NEEDSYN. 1780df8bae1dSRodney W. Grimes */ 1781df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1782c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 1783fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1784c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 1785c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 1786a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1787c28440dbSRandall Stewart SEGQ_EMPTY(tp) && 17884741bfcbSPatrick Kelsey ((to.to_flags & TOF_TS) == 0 || 1789c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1790df8bae1dSRodney W. Grimes /* 1791df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1792df8bae1dSRodney W. Grimes * record the timestamp. 1793a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1794a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1795df8bae1dSRodney W. Grimes */ 1796be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1797fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1798d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1799a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1800df8bae1dSRodney W. Grimes } 1801df8bae1dSRodney W. Grimes 1802fb59c426SYoshinobu Inoue if (tlen == 0) { 1803fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1804fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1805dbc42409SLawrence Stewart !IN_RECOVERY(tp->t_flags) && 1806fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1807dbc42409SLawrence Stewart TAILQ_EMPTY(&tp->snd_holes)) { 1808df8bae1dSRodney W. Grimes /* 1809e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1810df8bae1dSRodney W. Grimes */ 181178b50714SRobert Watson TCPSTAT_INC(tcps_predack); 1812252ca428SRobert Watson 18139b8b58e0SJonathan Lemon /* 181410d20c84SMatt Macy * "bad retransmit" recovery without timestamps. 18159b8b58e0SJonathan Lemon */ 181610d20c84SMatt Macy if ((to.to_flags & TOF_TS) == 0 && 181710d20c84SMatt Macy tp->t_rxtshift == 1 && 1818672dc4aeSJohn Baldwin tp->t_flags & TF_PREVVALID && 18196dfb8b31SJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) { 1820dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 18219b8b58e0SJonathan Lemon } 1822fa55172bSMatthew Dillon 1823fa55172bSMatthew Dillon /* 1824fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1825fa55172bSMatthew Dillon * 1826fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1827fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1828fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1829fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1830fa55172bSMatthew Dillon */ 1831fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1832fa55172bSMatthew Dillon to.to_tsecr) { 18333ac12506SJonathan T. Looney uint32_t t; 1834d8951c8aSBjoern A. Zeeb 1835d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 1836d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 1837d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 1838a0292f23SGarrett Wollman tcp_xmit_timer(tp, 1839d8951c8aSBjoern A. Zeeb TCP_TS_TO_TICKS(t) + 1); 1840fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1841fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1842eaf80179SAndre Oppermann if (!tp->t_rttlow || 1843eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1844eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1845c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1846c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1847fa55172bSMatthew Dillon } 1848dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 184939bc9de5SLawrence Stewart 1850bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 185139bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 185239bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 1853bd79708dSJonathan T. Looney #endif 185439bc9de5SLawrence Stewart 18554b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvackpack, nsegs); 185678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 1857df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 18589d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 18599d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 18609d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 1861dbc42409SLawrence Stewart 1862dbc42409SLawrence Stewart /* 1863dbc42409SLawrence Stewart * Let the congestion control algorithm update 1864dbc42409SLawrence Stewart * congestion control related information. This 1865dbc42409SLawrence Stewart * typically means increasing the congestion 1866dbc42409SLawrence Stewart * window. 1867dbc42409SLawrence Stewart */ 18684b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, CC_ACK); 1869dbc42409SLawrence Stewart 18709d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 18711645d090SJeff Roberson /* 1872e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 18731645d090SJeff Roberson * to th_ack. 18741645d090SJeff Roberson */ 1875cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1876b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1877df8bae1dSRodney W. Grimes m_freem(m); 1878df8bae1dSRodney W. Grimes 1879df8bae1dSRodney W. Grimes /* 1880df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1881df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1882df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1883df8bae1dSRodney W. Grimes * If process is waiting for space, 1884df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1885df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1886df8bae1dSRodney W. Grimes * decide between more output or persist. 1887e8949f74SAndre Oppermann */ 18883c653157SHartmut Brandt #ifdef TCPDEBUG 18893c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 18903c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 18913c653157SHartmut Brandt (void *)tcp_saveipgen, 18923c653157SHartmut Brandt &tcp_savetcp, 0); 18933c653157SHartmut Brandt #endif 18942b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 1895df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1896b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1897b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1898b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1899b8152ba7SAndre Oppermann tp->t_rxtcur); 1900*032bf749SRichard Scheffenegger sowwakeup(so); 1901cfa6009eSGleb Smirnoff if (sbavail(&so->so_snd)) 190255bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 1903a14c749fSJonathan Lemon goto check_delack; 1904df8bae1dSRodney W. Grimes } 1905fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1906fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 19076741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 19086741ecf5SAndre Oppermann 1909df8bae1dSRodney W. Grimes /* 1910252ca428SRobert Watson * This is a pure, in-sequence data packet with 1911252ca428SRobert Watson * nothing on the reassembly queue and we have enough 1912252ca428SRobert Watson * buffer space to take it. 1913df8bae1dSRodney W. Grimes */ 19146d90faf3SPaul Saab /* Clean receiver SACK report if present */ 19153529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 19166d90faf3SPaul Saab tcp_clean_sackreport(tp); 191778b50714SRobert Watson TCPSTAT_INC(tcps_preddat); 1918fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 1919e854dd38SRandall Stewart if (tlen && 1920e854dd38SRandall Stewart ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 1921e854dd38SRandall Stewart (tp->t_fbyte_in == 0)) { 1922e854dd38SRandall Stewart tp->t_fbyte_in = ticks; 1923e854dd38SRandall Stewart if (tp->t_fbyte_in == 0) 1924e854dd38SRandall Stewart tp->t_fbyte_in = 1; 1925e854dd38SRandall Stewart if (tp->t_fbyte_out && tp->t_fbyte_in) 1926e854dd38SRandall Stewart tp->t_flags2 |= TF2_FBYTES_COMPLETE; 1927e854dd38SRandall Stewart } 19281645d090SJeff Roberson /* 19291645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 19301645d090SJeff Roberson * th_seq. 19311645d090SJeff Roberson */ 193260ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 19331645d090SJeff Roberson /* 19341645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 19351645d090SJeff Roberson * rcv_nxt. 19361645d090SJeff Roberson */ 19371645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 19384b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvpack, nsegs); 193978b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 19403c653157SHartmut Brandt #ifdef TCPDEBUG 19413c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 19423c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 19433c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 19443c653157SHartmut Brandt #endif 19452b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 19465d06879aSGeorge V. Neville-Neil 1947e44c1887SSteven Hartland newsize = tcp_autorcvbuf(m, th, so, tp, tlen); 19486741ecf5SAndre Oppermann 19496741ecf5SAndre Oppermann /* Add data to socket buffer. */ 19501e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1951c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1952c1c36a2cSMike Silbersack m_freem(m); 1953c1c36a2cSMike Silbersack } else { 19546741ecf5SAndre Oppermann /* 19556741ecf5SAndre Oppermann * Set new socket buffer size. 19566741ecf5SAndre Oppermann * Give up when limit is reached. 19576741ecf5SAndre Oppermann */ 19586741ecf5SAndre Oppermann if (newsize) 19596741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 19606c8286e4SRobert Watson newsize, so, NULL)) 19616741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1962fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 1963651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 1964c1c36a2cSMike Silbersack } 1965*032bf749SRichard Scheffenegger /* NB: sorwakeup_locked() does an implicit unlock. */ 1966*032bf749SRichard Scheffenegger sorwakeup_locked(so); 1967c1e5a6e5SAndre Oppermann if (DELAY_ACK(tp, tlen)) { 19683bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1969f498eeeeSDavid Greenman } else { 1970e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 197155bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 1972e612a582SDavid Greenman } 1973a14c749fSJonathan Lemon goto check_delack; 1974df8bae1dSRodney W. Grimes } 1975df8bae1dSRodney W. Grimes } 1976df8bae1dSRodney W. Grimes 1977df8bae1dSRodney W. Grimes /* 1978df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1979df8bae1dSRodney W. Grimes * and then do TCP input processing. 1980df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1981df8bae1dSRodney W. Grimes * but not less than advertised window. 1982df8bae1dSRodney W. Grimes */ 1983df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1984df8bae1dSRodney W. Grimes if (win < 0) 1985df8bae1dSRodney W. Grimes win = 0; 198666e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1987df8bae1dSRodney W. Grimes 1988df8bae1dSRodney W. Grimes switch (tp->t_state) { 1989df8bae1dSRodney W. Grimes /* 1990764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1991764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1992764d8cefSBill Fenner */ 1993764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1994fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1995fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1996a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1997a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1998a57815efSBosko Milekic goto dropwithreset; 1999a57815efSBosko Milekic } 200068bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) { 2001281a0fd4SPatrick Kelsey /* 2002281a0fd4SPatrick Kelsey * When a TFO connection is in SYN_RECEIVED, the 2003281a0fd4SPatrick Kelsey * only valid packets are the initial SYN, a 2004281a0fd4SPatrick Kelsey * retransmit/copy of the initial SYN (possibly with 2005281a0fd4SPatrick Kelsey * a subset of the original data), a valid ACK, a 2006281a0fd4SPatrick Kelsey * FIN, or a RST. 2007281a0fd4SPatrick Kelsey */ 2008281a0fd4SPatrick Kelsey if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) { 2009281a0fd4SPatrick Kelsey rstreason = BANDLIM_RST_OPENPORT; 2010281a0fd4SPatrick Kelsey goto dropwithreset; 2011281a0fd4SPatrick Kelsey } else if (thflags & TH_SYN) { 2012281a0fd4SPatrick Kelsey /* non-initial SYN is ignored */ 2013281a0fd4SPatrick Kelsey if ((tcp_timer_active(tp, TT_DELACK) || 2014281a0fd4SPatrick Kelsey tcp_timer_active(tp, TT_REXMT))) 2015281a0fd4SPatrick Kelsey goto drop; 2016281a0fd4SPatrick Kelsey } else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) { 2017281a0fd4SPatrick Kelsey goto drop; 2018281a0fd4SPatrick Kelsey } 2019281a0fd4SPatrick Kelsey } 2020764d8cefSBill Fenner break; 2021764d8cefSBill Fenner 2022764d8cefSBill Fenner /* 2023df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 202498732609SMichael Tuexen * if seg contains a RST with valid ACK (SEQ.ACK has already 202598732609SMichael Tuexen * been verified), then drop the connection. 202698732609SMichael Tuexen * if seg contains a RST without an ACK, drop the seg. 202798732609SMichael Tuexen * if seg does not contain SYN, then drop the seg. 2028df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 2029df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 2030df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 2031f2512ba1SRui Paulo * if seg contains an ECE and ECN support is enabled, the stream 2032f2512ba1SRui Paulo * is ECN capable. 2033df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 2034df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 2035df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 2036df8bae1dSRodney W. Grimes */ 2037df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 203857f60867SMark Johnston if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) { 2039d9fae5abSAndriy Gapon TCP_PROBE5(connect__refused, NULL, tp, 20402b9c9984SGeorge V. Neville-Neil m, tp, th); 2041df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 204257f60867SMark Johnston } 20430ca3f933SAndre Oppermann if (thflags & TH_RST) 2044df8bae1dSRodney W. Grimes goto drop; 20450ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 2046df8bae1dSRodney W. Grimes goto drop; 2047464fcfbcSAndre Oppermann 2048fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 2049df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 2050fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 2051c560df6fSPatrick Kelsey int tfo_partial_ack = 0; 2052c560df6fSPatrick Kelsey 205378b50714SRobert Watson TCPSTAT_INC(tcps_connects); 2054845799c1SAndras Olah soisconnected(so); 2055c488362eSRobert Watson #ifdef MAC 205630d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 2057c488362eSRobert Watson #endif 2058845799c1SAndras Olah /* Do window scaling on this connection? */ 2059845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2060845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2061845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 2062845799c1SAndras Olah } 20633ac12506SJonathan T. Looney tp->rcv_adv += min(tp->rcv_wnd, 2064766282cbSJohn Baldwin TCP_MAXWIN << tp->rcv_scale); 2065a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 2066a0292f23SGarrett Wollman /* 2067c560df6fSPatrick Kelsey * If not all the data that was sent in the TFO SYN 2068c560df6fSPatrick Kelsey * has been acked, resend the remainder right away. 2069c560df6fSPatrick Kelsey */ 2070c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags) && 2071c560df6fSPatrick Kelsey (tp->snd_una != tp->snd_max)) { 2072c560df6fSPatrick Kelsey tp->snd_nxt = th->th_ack; 2073c560df6fSPatrick Kelsey tfo_partial_ack = 1; 2074c560df6fSPatrick Kelsey } 2075c560df6fSPatrick Kelsey /* 2076a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 2077a0292f23SGarrett Wollman * ACKNOW will be turned on later. 2078a0292f23SGarrett Wollman */ 2079c560df6fSPatrick Kelsey if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial_ack) 2080b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 2081b8152ba7SAndre Oppermann tcp_delacktime); 2082a0292f23SGarrett Wollman else 2083a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2084f2512ba1SRui Paulo 2085bf7fcdb1SMichael Tuexen if (((thflags & (TH_CWR | TH_ECE)) == TH_ECE) && 2086a2d59694SMichael Tuexen (V_tcp_do_ecn == 1)) { 20873cf38784SMichael Tuexen tp->t_flags2 |= TF2_ECN_PERMIT; 208878b50714SRobert Watson TCPSTAT_INC(tcps_ecn_shs); 2089f2512ba1SRui Paulo } 2090f2512ba1SRui Paulo 2091a0292f23SGarrett Wollman /* 2092a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 2093a0292f23SGarrett Wollman * Transitions: 2094a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 2095a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 2096a0292f23SGarrett Wollman */ 20979b8b58e0SJonathan Lemon tp->t_starttime = ticks; 2098a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 209957f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2100a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 2101fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 21027ff19458SPaul Traina } else { 210357f60867SMark Johnston tcp_state_change(tp, TCPS_ESTABLISHED); 2104d9fae5abSAndriy Gapon TCP_PROBE5(connect__established, NULL, tp, 21052b9c9984SGeorge V. Neville-Neil m, tp, th); 2106dbc42409SLawrence Stewart cc_conn_init(tp); 21079077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 21089077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 21097ff19458SPaul Traina } 2110a0292f23SGarrett Wollman } else { 2111a0292f23SGarrett Wollman /* 2112c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 2113153edc50SHiren Panchasara * simultaneous open. 2114c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 2115c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 2116a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 2117a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 2118a0292f23SGarrett Wollman */ 21194b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 2120b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 212157f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_RECEIVED); 2122a0292f23SGarrett Wollman } 2123df8bae1dSRodney W. Grimes 21248501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 21257cfc6904SRobert Watson 2126df8bae1dSRodney W. Grimes /* 2127fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 2128df8bae1dSRodney W. Grimes * If data, trim to stay within window, 2129df8bae1dSRodney W. Grimes * dropping FIN if necessary. 2130df8bae1dSRodney W. Grimes */ 2131fb59c426SYoshinobu Inoue th->th_seq++; 2132fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 2133fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 2134df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2135fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 2136fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 213778b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 213878b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2139df8bae1dSRodney W. Grimes } 2140fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 2141fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 2142a0292f23SGarrett Wollman /* 2143a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 2144a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 2145a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 2146a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 2147a0292f23SGarrett Wollman * Otherwise, goto step 6. 2148a0292f23SGarrett Wollman */ 2149fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 2150a0292f23SGarrett Wollman goto process_ACK; 2151c068736aSJeffrey Hsu 2152df8bae1dSRodney W. Grimes goto step6; 2153c068736aSJeffrey Hsu 2154a0292f23SGarrett Wollman /* 2155a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 2156c94c54e4SAndre Oppermann * do normal processing. 2157a0292f23SGarrett Wollman * 2158c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 2159a0292f23SGarrett Wollman */ 2160a0292f23SGarrett Wollman case TCPS_LAST_ACK: 2161a0292f23SGarrett Wollman case TCPS_CLOSING: 2162a0292f23SGarrett Wollman break; /* continue normal processing */ 2163df8bae1dSRodney W. Grimes } 2164df8bae1dSRodney W. Grimes 2165df8bae1dSRodney W. Grimes /* 2166df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 216780ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 216880ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 216980ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 217080ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 217180ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 217280ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 217380ab7c0eSGarrett Wollman * killing a connection). 217480ab7c0eSGarrett Wollman * Then check timestamp, if present. 2175a0292f23SGarrett Wollman * Then check the connection count, if present. 2176df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 2177df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 2178df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 217980ab7c0eSGarrett Wollman */ 2180fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 21813220a212SGleb Smirnoff /* 21823220a212SGleb Smirnoff * RFC5961 Section 3.2 21833220a212SGleb Smirnoff * 21843220a212SGleb Smirnoff * - RST drops connection only if SEG.SEQ == RCV.NXT. 21853220a212SGleb Smirnoff * - If RST is in window, we send challenge ACK. 21863220a212SGleb Smirnoff * 21873220a212SGleb Smirnoff * Note: to take into account delayed ACKs, we should 21883220a212SGleb Smirnoff * test against last_ack_sent instead of rcv_nxt. 21893220a212SGleb Smirnoff * Note 2: we handle special case of closed window, not 21903220a212SGleb Smirnoff * covered by the RFC. 21913220a212SGleb Smirnoff */ 21923220a212SGleb Smirnoff if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 21933220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) || 21943220a212SGleb Smirnoff (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) { 21953220a212SGleb Smirnoff KASSERT(tp->t_state != TCPS_SYN_SENT, 21963220a212SGleb Smirnoff ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p", 21973220a212SGleb Smirnoff __func__, th, tp)); 21983220a212SGleb Smirnoff 21993220a212SGleb Smirnoff if (V_tcp_insecure_rst || 22003220a212SGleb Smirnoff tp->last_ack_sent == th->th_seq) { 22013220a212SGleb Smirnoff TCPSTAT_INC(tcps_drops); 22023220a212SGleb Smirnoff /* Drop the connection. */ 22033220a212SGleb Smirnoff switch (tp->t_state) { 220480ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 220580ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 220680ab7c0eSGarrett Wollman goto close; 220780ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 220880ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 220980ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 221080ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 22116779a1a1SMichael Tuexen case TCPS_CLOSING: 22126779a1a1SMichael Tuexen case TCPS_LAST_ACK: 221380ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 221480ab7c0eSGarrett Wollman close: 22153220a212SGleb Smirnoff /* FALLTHROUGH */ 22163220a212SGleb Smirnoff default: 221780ab7c0eSGarrett Wollman tp = tcp_close(tp); 22183220a212SGleb Smirnoff } 22193220a212SGleb Smirnoff } else { 22203220a212SGleb Smirnoff TCPSTAT_INC(tcps_badrst); 22213220a212SGleb Smirnoff /* Send challenge ACK. */ 22223220a212SGleb Smirnoff tcp_respond(tp, mtod(m, void *), th, m, 22233220a212SGleb Smirnoff tp->rcv_nxt, tp->snd_nxt, TH_ACK); 22243220a212SGleb Smirnoff tp->last_ack_sent = tp->rcv_nxt; 22253220a212SGleb Smirnoff m = NULL; 22263220a212SGleb Smirnoff } 22273220a212SGleb Smirnoff } 22283220a212SGleb Smirnoff goto drop; 22293220a212SGleb Smirnoff } 223080ab7c0eSGarrett Wollman 22313220a212SGleb Smirnoff /* 22323220a212SGleb Smirnoff * RFC5961 Section 4.2 22333220a212SGleb Smirnoff * Send challenge ACK for any SYN in synchronized state. 22343220a212SGleb Smirnoff */ 2235281a0fd4SPatrick Kelsey if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT && 2236281a0fd4SPatrick Kelsey tp->t_state != TCPS_SYN_RECEIVED) { 22373220a212SGleb Smirnoff TCPSTAT_INC(tcps_badsyn); 22383220a212SGleb Smirnoff if (V_tcp_insecure_syn && 22393220a212SGleb Smirnoff SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 22403220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 22413220a212SGleb Smirnoff tp = tcp_drop(tp, ECONNRESET); 22423220a212SGleb Smirnoff rstreason = BANDLIM_UNLIMITED; 22433220a212SGleb Smirnoff } else { 22443220a212SGleb Smirnoff /* Send challenge ACK. */ 22453220a212SGleb Smirnoff tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt, 22463220a212SGleb Smirnoff tp->snd_nxt, TH_ACK); 22473220a212SGleb Smirnoff tp->last_ack_sent = tp->rcv_nxt; 22483220a212SGleb Smirnoff m = NULL; 224980ab7c0eSGarrett Wollman } 225080ab7c0eSGarrett Wollman goto drop; 225180ab7c0eSGarrett Wollman } 225280ab7c0eSGarrett Wollman 225380ab7c0eSGarrett Wollman /* 2254df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 2255df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 2256df8bae1dSRodney W. Grimes */ 2257be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 225880ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 2259df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 2260d8951c8aSBjoern A. Zeeb if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 2261df8bae1dSRodney W. Grimes /* 2262df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 2263df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 2264df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 2265df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 2266df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 2267df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 2268df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 2269df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 2270df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 2271df8bae1dSRodney W. Grimes */ 2272df8bae1dSRodney W. Grimes tp->ts_recent = 0; 2273df8bae1dSRodney W. Grimes } else { 227478b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 227578b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 227678b50714SRobert Watson TCPSTAT_INC(tcps_pawsdrop); 227707fd333dSMatthew Dillon if (tlen) 2278df8bae1dSRodney W. Grimes goto dropafterack; 22791ab4789dSMatthew Dillon goto drop; 22801ab4789dSMatthew Dillon } 2281df8bae1dSRodney W. Grimes } 2282df8bae1dSRodney W. Grimes 2283a0292f23SGarrett Wollman /* 228480ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 228580ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 228680ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 228780ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 228880ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 228980ab7c0eSGarrett Wollman */ 2290a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 2291a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2292a57815efSBosko Milekic goto dropwithreset; 2293a57815efSBosko Milekic } 229480ab7c0eSGarrett Wollman 2295fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 2296df8bae1dSRodney W. Grimes if (todrop > 0) { 2297831ad37eSXin LI if (thflags & TH_SYN) { 2298fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 2299fb59c426SYoshinobu Inoue th->th_seq++; 2300fb59c426SYoshinobu Inoue if (th->th_urp > 1) 2301fb59c426SYoshinobu Inoue th->th_urp--; 2302df8bae1dSRodney W. Grimes else 2303fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2304df8bae1dSRodney W. Grimes todrop--; 2305df8bae1dSRodney W. Grimes } 2306df8bae1dSRodney W. Grimes /* 2307dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 2308df8bae1dSRodney W. Grimes */ 2309fb59c426SYoshinobu Inoue if (todrop > tlen 2310fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 2311dac20301SGarrett Wollman /* 2312dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 2313dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 2314dac20301SGarrett Wollman * of sequence; drop it. 2315dac20301SGarrett Wollman */ 2316fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2317dac20301SGarrett Wollman 2318df8bae1dSRodney W. Grimes /* 2319dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 2320dac20301SGarrett Wollman * But keep on processing for RST or ACK. 2321df8bae1dSRodney W. Grimes */ 2322dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2323fb59c426SYoshinobu Inoue todrop = tlen; 232478b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 232578b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 2326df8bae1dSRodney W. Grimes } else { 232778b50714SRobert Watson TCPSTAT_INC(tcps_rcvpartduppack); 232878b50714SRobert Watson TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 2329df8bae1dSRodney W. Grimes } 23305acfd95cSMichael Tuexen /* 23315acfd95cSMichael Tuexen * DSACK - add SACK block for dropped range 23325acfd95cSMichael Tuexen */ 23338f63a52bSMichael Tuexen if ((todrop > 0) && (tp->t_flags & TF_SACK_PERMIT)) { 2334ecc5b1d1SMichael Tuexen tcp_update_sack_list(tp, th->th_seq, 2335ecc5b1d1SMichael Tuexen th->th_seq + todrop); 23365acfd95cSMichael Tuexen /* 23375acfd95cSMichael Tuexen * ACK now, as the next in-sequence segment 23385acfd95cSMichael Tuexen * will clear the DSACK block again 23395acfd95cSMichael Tuexen */ 23405acfd95cSMichael Tuexen tp->t_flags |= TF_ACKNOW; 23415acfd95cSMichael Tuexen } 2342fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 2343fb59c426SYoshinobu Inoue th->th_seq += todrop; 2344fb59c426SYoshinobu Inoue tlen -= todrop; 2345fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 2346fb59c426SYoshinobu Inoue th->th_urp -= todrop; 2347df8bae1dSRodney W. Grimes else { 2348fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2349fb59c426SYoshinobu Inoue th->th_urp = 0; 2350df8bae1dSRodney W. Grimes } 2351df8bae1dSRodney W. Grimes } 2352df8bae1dSRodney W. Grimes 2353df8bae1dSRodney W. Grimes /* 2354df8bae1dSRodney W. Grimes * If new data are received on a connection after the 2355df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 2356df8bae1dSRodney W. Grimes */ 2357df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 2358fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 235907dacf03SAndre Oppermann if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 236007dacf03SAndre Oppermann log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data " 236107dacf03SAndre Oppermann "after socket was closed, " 236207dacf03SAndre Oppermann "sending RST and removing tcpcb\n", 2363e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 2364773673c1SAndre Oppermann free(s, M_TCPLOG); 2365773673c1SAndre Oppermann } 2366df8bae1dSRodney W. Grimes tp = tcp_close(tp); 236778b50714SRobert Watson TCPSTAT_INC(tcps_rcvafterclose); 2368a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2369df8bae1dSRodney W. Grimes goto dropwithreset; 2370df8bae1dSRodney W. Grimes } 2371df8bae1dSRodney W. Grimes 2372df8bae1dSRodney W. Grimes /* 2373df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 2374df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 2375df8bae1dSRodney W. Grimes */ 2376fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 2377df8bae1dSRodney W. Grimes if (todrop > 0) { 237878b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 2379fb59c426SYoshinobu Inoue if (todrop >= tlen) { 238078b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 2381df8bae1dSRodney W. Grimes /* 2382df8bae1dSRodney W. Grimes * If window is closed can only take segments at 2383df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 2384df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 2385df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 2386df8bae1dSRodney W. Grimes * and ack. 2387df8bae1dSRodney W. Grimes */ 2388fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2389df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 239078b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinprobe); 2391df8bae1dSRodney W. Grimes } else 2392df8bae1dSRodney W. Grimes goto dropafterack; 2393df8bae1dSRodney W. Grimes } else 239478b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2395df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2396fb59c426SYoshinobu Inoue tlen -= todrop; 2397fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 2398df8bae1dSRodney W. Grimes } 2399df8bae1dSRodney W. Grimes 2400df8bae1dSRodney W. Grimes /* 2401df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 2402df8bae1dSRodney W. Grimes * record its timestamp. 2403cf09195bSPaul Saab * NOTE: 2404cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 2405a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2406cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 2407cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 2408cf09195bSPaul Saab * predicated on the sequence space of this segment. 2409cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 2410cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 2411cf09195bSPaul Saab * instead of RFC1323's 2412cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 2413cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 2414cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 2415cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 2416cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 2417df8bae1dSRodney W. Grimes */ 2418be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 2419cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2420cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 2421cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 2422d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 2423a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 2424df8bae1dSRodney W. Grimes } 2425df8bae1dSRodney W. Grimes 2426df8bae1dSRodney W. Grimes /* 2427a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 2428a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 2429a0292f23SGarrett Wollman * later processing; else drop segment and return. 2430a0292f23SGarrett Wollman */ 2431fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 2432a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 2433281a0fd4SPatrick Kelsey (tp->t_flags & TF_NEEDSYN)) { 2434281a0fd4SPatrick Kelsey if (tp->t_state == TCPS_SYN_RECEIVED && 243568bd7ed1SJonathan T. Looney IS_FASTOPEN(tp->t_flags)) { 2436281a0fd4SPatrick Kelsey tp->snd_wnd = tiwin; 2437281a0fd4SPatrick Kelsey cc_conn_init(tp); 2438281a0fd4SPatrick Kelsey } 2439a0292f23SGarrett Wollman goto step6; 2440281a0fd4SPatrick Kelsey } else if (tp->t_flags & TF_ACKNOW) 24415e1aa279SDavid Malone goto dropafterack; 2442a0292f23SGarrett Wollman else 2443a0292f23SGarrett Wollman goto drop; 2444a0292f23SGarrett Wollman } 2445df8bae1dSRodney W. Grimes 2446df8bae1dSRodney W. Grimes /* 2447df8bae1dSRodney W. Grimes * Ack processing. 2448df8bae1dSRodney W. Grimes */ 2449df8bae1dSRodney W. Grimes switch (tp->t_state) { 2450df8bae1dSRodney W. Grimes /* 2451764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 2452764d8cefSBill Fenner * ESTABLISHED state and continue processing. 2453764d8cefSBill Fenner * The ACK was checked above. 2454df8bae1dSRodney W. Grimes */ 2455df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2456a0292f23SGarrett Wollman 245778b50714SRobert Watson TCPSTAT_INC(tcps_connects); 2458df8bae1dSRodney W. Grimes soisconnected(so); 2459df8bae1dSRodney W. Grimes /* Do window scaling? */ 2460df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2461df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2462df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 2463df8bae1dSRodney W. Grimes } 2464116ef4d6SMichael Tuexen tp->snd_wnd = tiwin; 2465a0292f23SGarrett Wollman /* 2466a0292f23SGarrett Wollman * Make transitions: 2467a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 2468a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 2469a0292f23SGarrett Wollman */ 24709b8b58e0SJonathan Lemon tp->t_starttime = ticks; 247118a75309SPatrick Kelsey if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) { 2472281a0fd4SPatrick Kelsey tcp_fastopen_decrement_counter(tp->t_tfo_pending); 2473281a0fd4SPatrick Kelsey tp->t_tfo_pending = NULL; 2474281a0fd4SPatrick Kelsey } 24758db239dcSMichael Tuexen if (tp->t_flags & TF_NEEDFIN) { 24768db239dcSMichael Tuexen tcp_state_change(tp, TCPS_FIN_WAIT_1); 24778db239dcSMichael Tuexen tp->t_flags &= ~TF_NEEDFIN; 24788db239dcSMichael Tuexen } else { 24798db239dcSMichael Tuexen tcp_state_change(tp, TCPS_ESTABLISHED); 24808db239dcSMichael Tuexen TCP_PROBE5(accept__established, NULL, tp, 24818db239dcSMichael Tuexen m, tp, th); 2482281a0fd4SPatrick Kelsey /* 2483281a0fd4SPatrick Kelsey * TFO connections call cc_conn_init() during SYN 2484281a0fd4SPatrick Kelsey * processing. Calling it again here for such 2485281a0fd4SPatrick Kelsey * connections is not harmless as it would undo the 2486281a0fd4SPatrick Kelsey * snd_cwnd reduction that occurs when a TFO SYN|ACK 2487281a0fd4SPatrick Kelsey * is retransmitted. 2488281a0fd4SPatrick Kelsey */ 248968bd7ed1SJonathan T. Looney if (!IS_FASTOPEN(tp->t_flags)) 2490dbc42409SLawrence Stewart cc_conn_init(tp); 24919077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 24927ff19458SPaul Traina } 2493a0292f23SGarrett Wollman /* 2494b2ade6b1SRichard Scheffenegger * Account for the ACK of our SYN prior to 2495b2ade6b1SRichard Scheffenegger * regular ACK processing below, except for 2496b2ade6b1SRichard Scheffenegger * simultaneous SYN, which is handled later. 2497b2ade6b1SRichard Scheffenegger */ 2498b2ade6b1SRichard Scheffenegger if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN)) 2499b2ade6b1SRichard Scheffenegger incforsyn = 1; 2500b2ade6b1SRichard Scheffenegger /* 2501a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 2502a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 2503a0292f23SGarrett Wollman */ 2504*032bf749SRichard Scheffenegger if (tlen == 0 && (thflags & TH_FIN) == 0) { 2505c28440dbSRandall Stewart (void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0, 2506a0292f23SGarrett Wollman (struct mbuf *)0); 2507*032bf749SRichard Scheffenegger tcp_handle_wakeup(tp, so); 2508*032bf749SRichard Scheffenegger } 250960ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq - 1; 251093b0017fSPhilippe Charnier /* FALLTHROUGH */ 2511df8bae1dSRodney W. Grimes 2512df8bae1dSRodney W. Grimes /* 2513df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2514df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 2515fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 2516fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 2517df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 2518df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 2519df8bae1dSRodney W. Grimes */ 2520df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2521df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2522df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2523df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 2524df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2525df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 25265a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 252778b50714SRobert Watson TCPSTAT_INC(tcps_rcvacktoomuch); 25285a53ca16SPaul Saab goto dropafterack; 25295a53ca16SPaul Saab } 25303529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2531fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 25320471a8c7SRichard Scheffenegger !TAILQ_EMPTY(&tp->snd_holes))) { 25330471a8c7SRichard Scheffenegger if (((sack_changed = tcp_sack_doack(tp, &to, th->th_ack)) != 0) && 25340471a8c7SRichard Scheffenegger (tp->t_flags & TF_LRD)) { 25350471a8c7SRichard Scheffenegger tcp_sack_lost_retransmission(tp, th); 25360471a8c7SRichard Scheffenegger } 25370471a8c7SRichard Scheffenegger } else 253812eeb81fSHiren Panchasara /* 253912eeb81fSHiren Panchasara * Reset the value so that previous (valid) value 254012eeb81fSHiren Panchasara * from the last ack with SACK doesn't get used. 254112eeb81fSHiren Panchasara */ 254212eeb81fSHiren Panchasara tp->sackhint.sacked_bytes = 0; 254339bc9de5SLawrence Stewart 2544bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 254539bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 254639bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 2547bd79708dSJonathan T. Looney #endif 254839bc9de5SLawrence Stewart 2549fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 25500c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 2551021eaf79SHiren Panchasara if (tlen == 0 && 2552021eaf79SHiren Panchasara (tiwin == tp->snd_wnd || 2553021eaf79SHiren Panchasara (tp->t_flags & TF_SACK_PERMIT))) { 255462b90589SPeter Wemm /* 255562b90589SPeter Wemm * If this is the first time we've seen a 255662b90589SPeter Wemm * FIN from the remote, this is not a 255762b90589SPeter Wemm * duplicate and it needs to be processed 255862b90589SPeter Wemm * normally. This happens during a 255962b90589SPeter Wemm * simultaneous close. 256062b90589SPeter Wemm */ 256162b90589SPeter Wemm if ((thflags & TH_FIN) && 256262b90589SPeter Wemm (TCPS_HAVERCVDFIN(tp->t_state) == 0)) { 256362b90589SPeter Wemm tp->t_dupacks = 0; 256462b90589SPeter Wemm break; 256562b90589SPeter Wemm } 256678b50714SRobert Watson TCPSTAT_INC(tcps_rcvdupack); 2567df8bae1dSRodney W. Grimes /* 2568df8bae1dSRodney W. Grimes * If we have outstanding data (other than 2569df8bae1dSRodney W. Grimes * a window probe), this is a completely 2570df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 25715f30ec9bSEitan Adler * change and FIN isn't set), 25725f30ec9bSEitan Adler * the ack is the biggest we've 2573df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 2574a4641f4eSPedro F. Giffuni * threshold of them, assume a packet 2575df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 2576df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 2577df8bae1dSRodney W. Grimes * window so we send only this one 2578df8bae1dSRodney W. Grimes * packet. 2579df8bae1dSRodney W. Grimes * 2580df8bae1dSRodney W. Grimes * We know we're losing at the current 2581df8bae1dSRodney W. Grimes * window size so do congestion avoidance 2582df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 2583df8bae1dSRodney W. Grimes * and pull our congestion window back to 2584df8bae1dSRodney W. Grimes * the new ssthresh). 2585df8bae1dSRodney W. Grimes * 2586df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 2587df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 2588df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 2589df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 2590df8bae1dSRodney W. Grimes * network. 2591f2512ba1SRui Paulo * 2592f2512ba1SRui Paulo * When using TCP ECN, notify the peer that 2593f2512ba1SRui Paulo * we reduced the cwnd. 2594df8bae1dSRodney W. Grimes */ 2595021eaf79SHiren Panchasara /* 2596021eaf79SHiren Panchasara * Following 2 kinds of acks should not affect 2597021eaf79SHiren Panchasara * dupack counting: 2598021eaf79SHiren Panchasara * 1) Old acks 2599021eaf79SHiren Panchasara * 2) Acks with SACK but without any new SACK 2600021eaf79SHiren Panchasara * information in them. These could result from 2601021eaf79SHiren Panchasara * any anomaly in the network like a switch 2602021eaf79SHiren Panchasara * duplicating packets or a possible DoS attack. 2603021eaf79SHiren Panchasara */ 2604021eaf79SHiren Panchasara if (th->th_ack != tp->snd_una || 2605021eaf79SHiren Panchasara ((tp->t_flags & TF_SACK_PERMIT) && 26060533fab8SRichard Scheffenegger (to.to_flags & TOF_SACK) && 2607021eaf79SHiren Panchasara !sack_changed)) 2608021eaf79SHiren Panchasara break; 2609021eaf79SHiren Panchasara else if (!tcp_timer_active(tp, TT_REXMT)) 2610df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2611cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 2612dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 26134b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 26144b7b743cSLawrence Stewart CC_DUPACK); 26150e1d7c25SRichard Scheffenegger if (V_tcp_do_prr && 26160e1d7c25SRichard Scheffenegger IN_FASTRECOVERY(tp->t_flags) && 26170e1d7c25SRichard Scheffenegger (tp->t_flags & TF_SACK_PERMIT)) { 261890cca08eSRichard Scheffenegger tcp_do_prr_ack(tp, th, &to); 26190e1d7c25SRichard Scheffenegger } else if ((tp->t_flags & TF_SACK_PERMIT) && 26200533fab8SRichard Scheffenegger (to.to_flags & TOF_SACK) && 2621dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2622808f11b7SPaul Saab int awnd; 262325e6f9edSPaul Saab 262425e6f9edSPaul Saab /* 262525e6f9edSPaul Saab * Compute the amount of data in flight first. 26264c3972f0SHiren Panchasara * We can inject new data into the pipe iff 262725e6f9edSPaul Saab * we have less than 1/2 the original window's 262825e6f9edSPaul Saab * worth of data in flight. 262925e6f9edSPaul Saab */ 2630d1de2b05SRichard Scheffenegger if (V_tcp_do_newsack) 263112eeb81fSHiren Panchasara awnd = tcp_compute_pipe(tp); 263212eeb81fSHiren Panchasara else 2633808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 26340077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 263512eeb81fSHiren Panchasara 2636808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 26370c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 263825e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 263925e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 264025e6f9edSPaul Saab } 264125e6f9edSPaul Saab } else 26420c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 264355bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 264446f58482SJonathan Lemon goto drop; 26453c40e1d5SRichard Scheffenegger } else if (tp->t_dupacks == tcprexmtthresh || 26463c40e1d5SRichard Scheffenegger (tp->t_flags & TF_SACK_PERMIT && 2647d1de2b05SRichard Scheffenegger V_tcp_do_newsack && 26483c40e1d5SRichard Scheffenegger tp->sackhint.sacked_bytes > 26493c40e1d5SRichard Scheffenegger (tcprexmtthresh - 1) * maxseg)) { 26503c40e1d5SRichard Scheffenegger enter_recovery: 26513c40e1d5SRichard Scheffenegger /* 26523c40e1d5SRichard Scheffenegger * Above is the RFC6675 trigger condition of 26533c40e1d5SRichard Scheffenegger * more than (dupthresh-1)*maxseg sacked data. 26543c40e1d5SRichard Scheffenegger * If the count of holes in the 26553c40e1d5SRichard Scheffenegger * scoreboard is >= dupthresh, we could 26563c40e1d5SRichard Scheffenegger * also enter loss recovery, but don't 26573c40e1d5SRichard Scheffenegger * have that value readily available. 26583c40e1d5SRichard Scheffenegger */ 26593c40e1d5SRichard Scheffenegger tp->t_dupacks = tcprexmtthresh; 2660cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 2661a0445c2eSJayanth Vijayaraghavan 2662a0445c2eSJayanth Vijayaraghavan /* 26630e1d7c25SRichard Scheffenegger * If we're doing sack, or prr, check 26640e1d7c25SRichard Scheffenegger * to see if we're already in sack 2665a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 2666a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 2667a0445c2eSJayanth Vijayaraghavan * recovery. 2668a0445c2eSJayanth Vijayaraghavan */ 26690e1d7c25SRichard Scheffenegger if (V_tcp_do_prr || 26700e1d7c25SRichard Scheffenegger (tp->t_flags & TF_SACK_PERMIT)) { 2671dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2672a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 2673a0445c2eSJayanth Vijayaraghavan break; 2674a0445c2eSJayanth Vijayaraghavan } 2675dbc42409SLawrence Stewart } else { 2676a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 26779d11646dSJeffrey Hsu tp->snd_recover)) { 2678cb942153SJeffrey Hsu tp->t_dupacks = 0; 2679cb942153SJeffrey Hsu break; 268046f58482SJonathan Lemon } 2681a0445c2eSJayanth Vijayaraghavan } 2682dbc42409SLawrence Stewart /* Congestion signal before ack. */ 2683dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_NDUPACK); 26844b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 26854b7b743cSLawrence Stewart CC_DUPACK); 2686b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 26879b8b58e0SJonathan Lemon tp->t_rtttime = 0; 26880e1d7c25SRichard Scheffenegger if (V_tcp_do_prr) { 26890e1d7c25SRichard Scheffenegger /* 26900e1d7c25SRichard Scheffenegger * snd_ssthresh is already updated by 26910e1d7c25SRichard Scheffenegger * cc_cong_signal. 26920e1d7c25SRichard Scheffenegger */ 2693e9071000SRichard Scheffenegger tp->sackhint.prr_delivered = 2694e9071000SRichard Scheffenegger tp->sackhint.sacked_bytes; 2695bc7ee8e5SRichard Scheffenegger tp->sackhint.recover_fs = max(1, 2696bc7ee8e5SRichard Scheffenegger tp->snd_nxt - tp->snd_una); 26970e1d7c25SRichard Scheffenegger } 26980533fab8SRichard Scheffenegger if ((tp->t_flags & TF_SACK_PERMIT) && 26990533fab8SRichard Scheffenegger (to.to_flags & TOF_SACK)) { 270078b50714SRobert Watson TCPSTAT_INC( 270178b50714SRobert Watson tcps_sack_recovery_episode); 2702a3574665SMichael Tuexen tp->snd_recover = tp->snd_nxt; 27030c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 270455bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 27053c40e1d5SRichard Scheffenegger if (SEQ_GT(th->th_ack, tp->snd_una)) 27063c40e1d5SRichard Scheffenegger goto resume_partialack; 27076d90faf3SPaul Saab goto drop; 27086d90faf3SPaul Saab } 2709fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 27100c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 271155bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 271248d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 2713302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 2714302ce8d6SAndre Oppermann __func__)); 2715df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 27160c39d38dSGleb Smirnoff maxseg * 271748d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 2718df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 2719df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 2720df8bae1dSRodney W. Grimes goto drop; 2721603724d3SBjoern A. Zeeb } else if (V_tcp_do_rfc3042) { 272262d4443fSHiren Panchasara /* 272362d4443fSHiren Panchasara * Process first and second duplicate 272462d4443fSHiren Panchasara * ACKs. Each indicates a segment 272562d4443fSHiren Panchasara * leaving the network, creating room 272662d4443fSHiren Panchasara * for more. Make sure we can send a 272762d4443fSHiren Panchasara * packet on reception of each duplicate 272862d4443fSHiren Panchasara * ACK by increasing snd_cwnd by one 272962d4443fSHiren Panchasara * segment. Restore the original 273062d4443fSHiren Panchasara * snd_cwnd after packet transmission. 273162d4443fSHiren Panchasara */ 27324b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 27334b7b743cSLawrence Stewart CC_DUPACK); 27343ac12506SJonathan T. Looney uint32_t oldcwnd = tp->snd_cwnd; 273548d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 273648d2549cSJeffrey Hsu u_int sent; 27375628dd08SAndre Oppermann int avail; 273889c02376SJeffrey Hsu 2739582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 2740582a954bSJeffrey Hsu tp->t_dupacks == 2, 2741302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 2742302ce8d6SAndre Oppermann __func__)); 274361a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 274448d2549cSJeffrey Hsu tp->snd_limited = 0; 274561a36e3dSJeffrey Hsu tp->snd_cwnd = 274661a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 274761a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 27480c39d38dSGleb Smirnoff maxseg; 27495628dd08SAndre Oppermann /* 27505628dd08SAndre Oppermann * Only call tcp_output when there 27515628dd08SAndre Oppermann * is new data available to be sent. 27525628dd08SAndre Oppermann * Otherwise we would send pure ACKs. 27535628dd08SAndre Oppermann */ 27545628dd08SAndre Oppermann SOCKBUF_LOCK(&so->so_snd); 2755cfa6009eSGleb Smirnoff avail = sbavail(&so->so_snd) - 27565628dd08SAndre Oppermann (tp->snd_nxt - tp->snd_una); 27575628dd08SAndre Oppermann SOCKBUF_UNLOCK(&so->so_snd); 27585628dd08SAndre Oppermann if (avail > 0) 275955bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 276048d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 27610c39d38dSGleb Smirnoff if (sent > maxseg) { 276289c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 276389c02376SJeffrey Hsu tp->snd_limited == 0) || 27640c39d38dSGleb Smirnoff (sent == maxseg + 1 && 276589c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 2766302ce8d6SAndre Oppermann ("%s: sent too much", 2767302ce8d6SAndre Oppermann __func__)); 276848d2549cSJeffrey Hsu tp->snd_limited = 2; 276948d2549cSJeffrey Hsu } else if (sent > 0) 277048d2549cSJeffrey Hsu ++tp->snd_limited; 2771582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 2772582a954bSJeffrey Hsu goto drop; 2773df8bae1dSRodney W. Grimes } 2774021eaf79SHiren Panchasara } 2775df8bae1dSRodney W. Grimes break; 2776021eaf79SHiren Panchasara } else { 2777021eaf79SHiren Panchasara /* 2778021eaf79SHiren Panchasara * This ack is advancing the left edge, reset the 2779021eaf79SHiren Panchasara * counter. 2780021eaf79SHiren Panchasara */ 2781021eaf79SHiren Panchasara tp->t_dupacks = 0; 2782021eaf79SHiren Panchasara /* 2783021eaf79SHiren Panchasara * If this ack also has new SACK info, increment the 2784f359d6ebSRichard Scheffenegger * counter as per rfc6675. The variable 2785f359d6ebSRichard Scheffenegger * sack_changed tracks all changes to the SACK 2786f359d6ebSRichard Scheffenegger * scoreboard, including when partial ACKs without 2787f359d6ebSRichard Scheffenegger * SACK options are received, and clear the scoreboard 2788f359d6ebSRichard Scheffenegger * from the left side. Such partial ACKs should not be 2789f359d6ebSRichard Scheffenegger * counted as dupacks here. 2790021eaf79SHiren Panchasara */ 2791f359d6ebSRichard Scheffenegger if ((tp->t_flags & TF_SACK_PERMIT) && 2792f359d6ebSRichard Scheffenegger (to.to_flags & TOF_SACK) && 27933c40e1d5SRichard Scheffenegger sack_changed) { 2794021eaf79SHiren Panchasara tp->t_dupacks++; 27953c40e1d5SRichard Scheffenegger /* limit overhead by setting maxseg last */ 27963c40e1d5SRichard Scheffenegger if (!IN_FASTRECOVERY(tp->t_flags) && 27973c40e1d5SRichard Scheffenegger (tp->sackhint.sacked_bytes > 27983c40e1d5SRichard Scheffenegger ((tcprexmtthresh - 1) * 27993c40e1d5SRichard Scheffenegger (maxseg = tcp_maxseg(tp))))) { 28003c40e1d5SRichard Scheffenegger goto enter_recovery; 28013c40e1d5SRichard Scheffenegger } 28023c40e1d5SRichard Scheffenegger } 2803df8bae1dSRodney W. Grimes } 2804cb942153SJeffrey Hsu 28053c40e1d5SRichard Scheffenegger resume_partialack: 2806302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2807302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 2808cb942153SJeffrey Hsu 2809df8bae1dSRodney W. Grimes /* 2810df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2811df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2812df8bae1dSRodney W. Grimes */ 2813dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2814cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 28153529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 2816eb3a59a8SRichard Scheffenegger if (V_tcp_do_prr && to.to_flags & TOF_SACK) { 2817eb3a59a8SRichard Scheffenegger tcp_timer_activate(tp, TT_REXMT, 0); 2818eb3a59a8SRichard Scheffenegger tp->t_rtttime = 0; 281990cca08eSRichard Scheffenegger tcp_do_prr_ack(tp, th, &to); 2820eb3a59a8SRichard Scheffenegger tp->t_flags |= TF_ACKNOW; 2821eb3a59a8SRichard Scheffenegger (void) tcp_output(tp); 2822eb3a59a8SRichard Scheffenegger } else 28236d90faf3SPaul Saab tcp_sack_partialack(tp, th); 28246d90faf3SPaul Saab else 2825c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2826dbc42409SLawrence Stewart } else 2827dbc42409SLawrence Stewart cc_post_recovery(tp, th); 2828b9f803b7SRichard Scheffenegger } else if (IN_CONGRECOVERY(tp->t_flags)) { 2829b9f803b7SRichard Scheffenegger if (SEQ_LT(th->th_ack, tp->snd_recover)) { 2830b9f803b7SRichard Scheffenegger if (V_tcp_do_prr) { 2831b9f803b7SRichard Scheffenegger tp->sackhint.delivered_data = BYTES_THIS_ACK(tp, th); 2832b9f803b7SRichard Scheffenegger tp->snd_fack = th->th_ack; 283390cca08eSRichard Scheffenegger tcp_do_prr_ack(tp, th, &to); 2834b9f803b7SRichard Scheffenegger (void) tcp_output(tp); 2835b9f803b7SRichard Scheffenegger } 2836b9f803b7SRichard Scheffenegger } else 2837b9f803b7SRichard Scheffenegger cc_post_recovery(tp, th); 283846f58482SJonathan Lemon } 2839a0292f23SGarrett Wollman /* 2840a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2841a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2842a0292f23SGarrett Wollman */ 2843a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2844a0292f23SGarrett Wollman /* 2845a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2846a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 284707e43e10SAndras Olah * synchronized). Go to non-starred state, 284807e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 284907e43e10SAndras Olah * we can do window scaling. 2850a0292f23SGarrett Wollman */ 2851a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2852a0292f23SGarrett Wollman tp->snd_una++; 285307e43e10SAndras Olah /* Do window scaling? */ 285407e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 285507e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 285607e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2857464fcfbcSAndre Oppermann /* Send window already scaled. */ 285807e43e10SAndras Olah } 2859a0292f23SGarrett Wollman } 2860a0292f23SGarrett Wollman 2861a0292f23SGarrett Wollman process_ACK: 28628501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 28637cfc6904SRobert Watson 2864b2ade6b1SRichard Scheffenegger /* 2865b2ade6b1SRichard Scheffenegger * Adjust for the SYN bit in sequence space, 2866b2ade6b1SRichard Scheffenegger * but don't account for it in cwnd calculations. 2867b2ade6b1SRichard Scheffenegger * This is for the SYN_RECEIVED, non-simultaneous 2868b2ade6b1SRichard Scheffenegger * SYN case. SYN_SENT and simultaneous SYN are 2869b2ade6b1SRichard Scheffenegger * treated elsewhere. 2870b2ade6b1SRichard Scheffenegger */ 2871b2ade6b1SRichard Scheffenegger if (incforsyn) 2872b2ade6b1SRichard Scheffenegger tp->snd_una++; 2873dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 2874b8c2cd15SJonathan T. Looney KASSERT(acked >= 0, ("%s: acked unexepectedly negative " 2875b8c2cd15SJonathan T. Looney "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__, 2876b8c2cd15SJonathan T. Looney tp->snd_una, th->th_ack, tp, m)); 28774b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvackpack, nsegs); 287878b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 2879df8bae1dSRodney W. Grimes 2880df8bae1dSRodney W. Grimes /* 28819b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 28829b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 28839b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 28849b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 28859b8b58e0SJonathan Lemon * we left off. 28869b8b58e0SJonathan Lemon */ 288710d20c84SMatt Macy if (tp->t_rxtshift == 1 && 288810d20c84SMatt Macy tp->t_flags & TF_PREVVALID && 288910d20c84SMatt Macy tp->t_badrxtwin && 289010d20c84SMatt Macy SEQ_LT(to.to_tsecr, tp->t_badrxtwin)) 2891dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 28929b8b58e0SJonathan Lemon 28939b8b58e0SJonathan Lemon /* 2894df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2895df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2896df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2897df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2898df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2899df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2900df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2901fa55172bSMatthew Dillon * 2902fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2903fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2904fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2905fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2906df8bae1dSRodney W. Grimes */ 2907d8951c8aSBjoern A. Zeeb if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) { 29083ac12506SJonathan T. Looney uint32_t t; 2909d8951c8aSBjoern A. Zeeb 2910d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 2911d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 2912d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 2913d8951c8aSBjoern A. Zeeb tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1); 2914fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2915eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2916eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 29179b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2918fa55172bSMatthew Dillon } 2919df8bae1dSRodney W. Grimes 2920df8bae1dSRodney W. Grimes /* 2921df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2922df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2923df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2924df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2925df8bae1dSRodney W. Grimes */ 2926fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 2927b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2928df8bae1dSRodney W. Grimes needoutput = 1; 2929b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2930b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2931a0292f23SGarrett Wollman 2932a0292f23SGarrett Wollman /* 2933a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2934a0292f23SGarrett Wollman * skip rest of ACK processing. 2935a0292f23SGarrett Wollman */ 2936a0292f23SGarrett Wollman if (acked == 0) 2937a0292f23SGarrett Wollman goto step6; 2938a0292f23SGarrett Wollman 2939df8bae1dSRodney W. Grimes /* 2940dbc42409SLawrence Stewart * Let the congestion control algorithm update congestion 2941dbc42409SLawrence Stewart * control related information. This typically means increasing 2942dbc42409SLawrence Stewart * the congestion window. 2943df8bae1dSRodney W. Grimes */ 29444b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, CC_ACK); 2945dbc42409SLawrence Stewart 29465905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2947cfa6009eSGleb Smirnoff if (acked > sbavail(&so->so_snd)) { 2948b8c2cd15SJonathan T. Looney if (tp->snd_wnd >= sbavail(&so->so_snd)) 2949cfa6009eSGleb Smirnoff tp->snd_wnd -= sbavail(&so->so_snd); 2950b8c2cd15SJonathan T. Looney else 2951b8c2cd15SJonathan T. Looney tp->snd_wnd = 0; 2952c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, 2953cfa6009eSGleb Smirnoff (int)sbavail(&so->so_snd)); 2954df8bae1dSRodney W. Grimes ourfinisacked = 1; 2955df8bae1dSRodney W. Grimes } else { 2956c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, acked); 29573ac12506SJonathan T. Looney if (tp->snd_wnd >= (uint32_t) acked) 295860ee3bb2SAndre Oppermann tp->snd_wnd -= acked; 2959b8c2cd15SJonathan T. Looney else 2960b8c2cd15SJonathan T. Looney tp->snd_wnd = 0; 2961df8bae1dSRodney W. Grimes ourfinisacked = 0; 2962df8bae1dSRodney W. Grimes } 2963*032bf749SRichard Scheffenegger /* NB: sowwakeup_locked() does an implicit unlock. */ 2964*032bf749SRichard Scheffenegger sowwakeup_locked(so); 2965c11a15bfSGleb Smirnoff m_freem(mfree); 2966e8949f74SAndre Oppermann /* Detect una wraparound. */ 2967dbc42409SLawrence Stewart if (!IN_RECOVERY(tp->t_flags) && 29689d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 29699d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 29709d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 2971dbc42409SLawrence Stewart /* XXXLAS: Can this be moved up into cc_post_recovery? */ 2972dbc42409SLawrence Stewart if (IN_RECOVERY(tp->t_flags) && 297324cb0f22SLawrence Stewart SEQ_GEQ(th->th_ack, tp->snd_recover)) { 2974dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 297524cb0f22SLawrence Stewart } 2976fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 29773529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 29786d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 29796d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 29806d90faf3SPaul Saab } 2981df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2982df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2983df8bae1dSRodney W. Grimes 2984df8bae1dSRodney W. Grimes switch (tp->t_state) { 2985df8bae1dSRodney W. Grimes /* 2986df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2987df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2988df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2989df8bae1dSRodney W. Grimes */ 2990df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2991df8bae1dSRodney W. Grimes if (ourfinisacked) { 2992df8bae1dSRodney W. Grimes /* 2993df8bae1dSRodney W. Grimes * If we can't receive any more 2994df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2995df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2996df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2997df8bae1dSRodney W. Grimes * we'll hang forever. 2998e8949f74SAndre Oppermann * 2999e8949f74SAndre Oppermann * XXXjl: 3000340c35deSJonathan Lemon * we should release the tp also, and use a 3001340c35deSJonathan Lemon * compressed state. 3002340c35deSJonathan Lemon */ 3003c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 300403e49181SSeigo Tanimura soisdisconnected(so); 30059077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 30069077f387SGleb Smirnoff (tcp_fast_finwait2_recycle ? 30079077f387SGleb Smirnoff tcp_finwait2_timeout : 30089077f387SGleb Smirnoff TP_MAXIDLE(tp))); 30094cc20ab1SSeigo Tanimura } 301057f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_2); 3011df8bae1dSRodney W. Grimes } 3012df8bae1dSRodney W. Grimes break; 3013df8bae1dSRodney W. Grimes 3014df8bae1dSRodney W. Grimes /* 3015df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 3016df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 3017df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 3018df8bae1dSRodney W. Grimes * the segment. 3019df8bae1dSRodney W. Grimes */ 3020df8bae1dSRodney W. Grimes case TCPS_CLOSING: 3021df8bae1dSRodney W. Grimes if (ourfinisacked) { 302211a20fb8SJeffrey Hsu tcp_twstart(tp); 3023340c35deSJonathan Lemon m_freem(m); 3024679d9708SAndre Oppermann return; 3025df8bae1dSRodney W. Grimes } 3026df8bae1dSRodney W. Grimes break; 3027df8bae1dSRodney W. Grimes 3028df8bae1dSRodney W. Grimes /* 3029df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 3030df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 3031df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 3032df8bae1dSRodney W. Grimes * enter the closed state and return. 3033df8bae1dSRodney W. Grimes */ 3034df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 3035df8bae1dSRodney W. Grimes if (ourfinisacked) { 3036df8bae1dSRodney W. Grimes tp = tcp_close(tp); 3037df8bae1dSRodney W. Grimes goto drop; 3038df8bae1dSRodney W. Grimes } 3039df8bae1dSRodney W. Grimes break; 3040df8bae1dSRodney W. Grimes } 3041df8bae1dSRodney W. Grimes } 3042df8bae1dSRodney W. Grimes 3043df8bae1dSRodney W. Grimes step6: 30448501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 30457cfc6904SRobert Watson 3046df8bae1dSRodney W. Grimes /* 304760ee3bb2SAndre Oppermann * Update window information. 304860ee3bb2SAndre Oppermann * Don't look at window if no ACK: TAC's send garbage on first SYN. 3049df8bae1dSRodney W. Grimes */ 305060ee3bb2SAndre Oppermann if ((thflags & TH_ACK) && 305160ee3bb2SAndre Oppermann (SEQ_LT(tp->snd_wl1, th->th_seq) || 305260ee3bb2SAndre Oppermann (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 305360ee3bb2SAndre Oppermann (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 305460ee3bb2SAndre Oppermann /* keep track of pure window updates */ 305560ee3bb2SAndre Oppermann if (tlen == 0 && 305660ee3bb2SAndre Oppermann tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 305778b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinupd); 3058df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 305960ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 306060ee3bb2SAndre Oppermann tp->snd_wl2 = th->th_ack; 3061df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 3062df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 306360ee3bb2SAndre Oppermann needoutput = 1; 3064df8bae1dSRodney W. Grimes } 3065df8bae1dSRodney W. Grimes 3066df8bae1dSRodney W. Grimes /* 3067df8bae1dSRodney W. Grimes * Process segments with URG. 3068df8bae1dSRodney W. Grimes */ 3069fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 3070df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 3071df8bae1dSRodney W. Grimes /* 3072df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 3073df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 3074df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 3075df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 3076df8bae1dSRodney W. Grimes */ 307718ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3078cfa6009eSGleb Smirnoff if (th->th_urp + sbavail(&so->so_rcv) > sb_max) { 3079fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 3080fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 308118ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 3082df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 3083df8bae1dSRodney W. Grimes } 3084df8bae1dSRodney W. Grimes /* 3085df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 3086df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 3087df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 3088df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 3089df8bae1dSRodney W. Grimes * In these states we ignore the URG. 3090df8bae1dSRodney W. Grimes * 3091df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 3092df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 3093df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 3094df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 3095df8bae1dSRodney W. Grimes * of data past the urgent section as the original 3096df8bae1dSRodney W. Grimes * spec states (in one of two places). 3097df8bae1dSRodney W. Grimes */ 3098fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 3099fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 3100cfa6009eSGleb Smirnoff so->so_oobmark = sbavail(&so->so_rcv) + 3101df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 3102927c5ceaSRobert Watson if (so->so_oobmark == 0) 3103c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 3104df8bae1dSRodney W. Grimes sohasoutofband(so); 3105df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 3106df8bae1dSRodney W. Grimes } 310718ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 3108df8bae1dSRodney W. Grimes /* 3109df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 3110df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 3111df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 3112df8bae1dSRodney W. Grimes * data may creep in... ick. 3113df8bae1dSRodney W. Grimes */ 31143ac12506SJonathan T. Looney if (th->th_urp <= (uint32_t)tlen && 311530613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 311630613f56SJeffrey Hsu /* hdr drop is delayed */ 311730613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 311830613f56SJeffrey Hsu } 3119c068736aSJeffrey Hsu } else { 3120df8bae1dSRodney W. Grimes /* 3121df8bae1dSRodney W. Grimes * If no out of band data is expected, 3122df8bae1dSRodney W. Grimes * pull receive urgent pointer along 3123df8bae1dSRodney W. Grimes * with the receive window. 3124df8bae1dSRodney W. Grimes */ 3125df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 3126df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 3127c068736aSJeffrey Hsu } 3128df8bae1dSRodney W. Grimes dodata: /* XXX */ 31298501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 31307cfc6904SRobert Watson 3131df8bae1dSRodney W. Grimes /* 3132df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 3133df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 3134df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 3135df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 3136df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 3137df8bae1dSRodney W. Grimes * connection then we just ignore the text. 3138df8bae1dSRodney W. Grimes */ 3139281a0fd4SPatrick Kelsey tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) && 314068bd7ed1SJonathan T. Looney IS_FASTOPEN(tp->t_flags)); 3141f1ea4e41SRandall Stewart if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) && 3142df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 314369e03620SPaul Saab tcp_seq save_start = th->th_seq; 31445acfd95cSMichael Tuexen tcp_seq save_rnxt = tp->rcv_nxt; 31455acfd95cSMichael Tuexen int save_tlen = tlen; 3146fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 3147e4b64281SJesper Skriver /* 3148c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 3149c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 3150c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 3151c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 3152c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 3153c068736aSJeffrey Hsu * and removal from the queue and repetition of various 3154c068736aSJeffrey Hsu * conversions. 3155c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 3156c068736aSJeffrey Hsu * immediately when segments are out of order (so 3157c068736aSJeffrey Hsu * fast retransmit can work). 3158e4b64281SJesper Skriver */ 31594741bfcbSPatrick Kelsey if (th->th_seq == tp->rcv_nxt && 3160c28440dbSRandall Stewart SEGQ_EMPTY(tp) && 3161281a0fd4SPatrick Kelsey (TCPS_HAVEESTABLISHED(tp->t_state) || 3162281a0fd4SPatrick Kelsey tfo_syn)) { 3163281a0fd4SPatrick Kelsey if (DELAY_ACK(tp, tlen) || tfo_syn) 31643bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 3165e4b64281SJesper Skriver else 3166e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 3167e4b64281SJesper Skriver tp->rcv_nxt += tlen; 3168e854dd38SRandall Stewart if (tlen && 3169e854dd38SRandall Stewart ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 3170e854dd38SRandall Stewart (tp->t_fbyte_in == 0)) { 3171e854dd38SRandall Stewart tp->t_fbyte_in = ticks; 3172e854dd38SRandall Stewart if (tp->t_fbyte_in == 0) 3173e854dd38SRandall Stewart tp->t_fbyte_in = 1; 3174e854dd38SRandall Stewart if (tp->t_fbyte_out && tp->t_fbyte_in) 3175e854dd38SRandall Stewart tp->t_flags2 |= TF2_FBYTES_COMPLETE; 3176e854dd38SRandall Stewart } 3177e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 317878b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 317978b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 31801e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3181c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 3182c1c36a2cSMike Silbersack m_freem(m); 3183c1c36a2cSMike Silbersack else 3184651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 31854d0770f1SRichard Scheffenegger tp->t_flags |= TF_WAKESOR; 3186e4b64281SJesper Skriver } else { 3187e8949f74SAndre Oppermann /* 3188e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 3189e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 3190e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 3191e8949f74SAndre Oppermann * when trimming from the head. 3192e8949f74SAndre Oppermann */ 31935acfd95cSMichael Tuexen tcp_seq temp = save_start; 31945acfd95cSMichael Tuexen thflags = tcp_reass(tp, th, &temp, &tlen, m); 3195e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 3196e4b64281SJesper Skriver } 319740f41eceSMichael Tuexen if ((tp->t_flags & TF_SACK_PERMIT) && 319840f41eceSMichael Tuexen (save_tlen > 0) && 319940f41eceSMichael Tuexen TCPS_HAVEESTABLISHED(tp->t_state)) { 32006d261981SMichael Tuexen if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) { 3201b5a154d8SMichael Tuexen /* 3202b5a154d8SMichael Tuexen * DSACK actually handled in the fastpath 3203b5a154d8SMichael Tuexen * above. 3204b5a154d8SMichael Tuexen */ 3205ecc5b1d1SMichael Tuexen tcp_update_sack_list(tp, save_start, 3206ecc5b1d1SMichael Tuexen save_start + save_tlen); 3207ecc5b1d1SMichael Tuexen } else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) { 3208ecc5b1d1SMichael Tuexen if ((tp->rcv_numsacks >= 1) && 3209ecc5b1d1SMichael Tuexen (tp->sackblks[0].end == save_start)) { 32106d261981SMichael Tuexen /* 32116d261981SMichael Tuexen * Partial overlap, recorded at todrop 32126d261981SMichael Tuexen * above. 32136d261981SMichael Tuexen */ 32146d261981SMichael Tuexen tcp_update_sack_list(tp, 32156d261981SMichael Tuexen tp->sackblks[0].start, 3216ecc5b1d1SMichael Tuexen tp->sackblks[0].end); 3217ecc5b1d1SMichael Tuexen } else { 3218ecc5b1d1SMichael Tuexen tcp_update_dsack_list(tp, save_start, 3219ecc5b1d1SMichael Tuexen save_start + save_tlen); 3220ecc5b1d1SMichael Tuexen } 32216d261981SMichael Tuexen } else if (tlen >= save_tlen) { 3222b5a154d8SMichael Tuexen /* Update of sackblks. */ 3223ecc5b1d1SMichael Tuexen tcp_update_dsack_list(tp, save_start, 3224ecc5b1d1SMichael Tuexen save_start + save_tlen); 3225ecc5b1d1SMichael Tuexen } else if (tlen > 0) { 3226ecc5b1d1SMichael Tuexen tcp_update_dsack_list(tp, save_start, 3227ecc5b1d1SMichael Tuexen save_start + tlen); 32285acfd95cSMichael Tuexen } 32295acfd95cSMichael Tuexen } 3230*032bf749SRichard Scheffenegger tcp_handle_wakeup(tp, so); 3231302ce8d6SAndre Oppermann #if 0 3232df8bae1dSRodney W. Grimes /* 3233df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 3234df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 3235df8bae1dSRodney W. Grimes * buffer size. 3236302ce8d6SAndre Oppermann * XXX: Unused. 3237df8bae1dSRodney W. Grimes */ 3238f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) 3239df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 3240f701e30dSJohn Baldwin else 3241f701e30dSJohn Baldwin len = so->so_rcv.sb_hiwat; 3242302ce8d6SAndre Oppermann #endif 3243df8bae1dSRodney W. Grimes } else { 3244df8bae1dSRodney W. Grimes m_freem(m); 3245fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 3246df8bae1dSRodney W. Grimes } 3247df8bae1dSRodney W. Grimes 3248df8bae1dSRodney W. Grimes /* 3249df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 3250df8bae1dSRodney W. Grimes * that the connection is closing. 3251df8bae1dSRodney W. Grimes */ 3252fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 3253df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 32544d0770f1SRichard Scheffenegger /* The socket upcall is handled by socantrcvmore. */ 3255*032bf749SRichard Scheffenegger socantrcvmore(so); 3256a0292f23SGarrett Wollman /* 3257a0292f23SGarrett Wollman * If connection is half-synchronized 3258d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 3259a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 3260a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 3261a0292f23SGarrett Wollman * more input can be expected, send ACK now. 3262a0292f23SGarrett Wollman */ 32633bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 32643bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 3265a0292f23SGarrett Wollman else 3266df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 3267df8bae1dSRodney W. Grimes tp->rcv_nxt++; 3268df8bae1dSRodney W. Grimes } 3269df8bae1dSRodney W. Grimes switch (tp->t_state) { 3270df8bae1dSRodney W. Grimes /* 3271df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 3272df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 3273df8bae1dSRodney W. Grimes */ 3274df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 32759b8b58e0SJonathan Lemon tp->t_starttime = ticks; 32769b8b58e0SJonathan Lemon /* FALLTHROUGH */ 3277df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 327857f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSE_WAIT); 3279df8bae1dSRodney W. Grimes break; 3280df8bae1dSRodney W. Grimes 3281df8bae1dSRodney W. Grimes /* 3282df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 3283df8bae1dSRodney W. Grimes * enter the CLOSING state. 3284df8bae1dSRodney W. Grimes */ 3285df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 328657f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSING); 3287df8bae1dSRodney W. Grimes break; 3288df8bae1dSRodney W. Grimes 3289df8bae1dSRodney W. Grimes /* 3290df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 3291df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 3292df8bae1dSRodney W. Grimes * standard timers. 3293df8bae1dSRodney W. Grimes */ 3294df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 3295340c35deSJonathan Lemon tcp_twstart(tp); 3296679d9708SAndre Oppermann return; 3297df8bae1dSRodney W. Grimes } 3298df8bae1dSRodney W. Grimes } 3299610ee2f9SDavid Greenman #ifdef TCPDEBUG 33004cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3301fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 3302fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3303610ee2f9SDavid Greenman #endif 33042b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 3305df8bae1dSRodney W. Grimes 3306df8bae1dSRodney W. Grimes /* 3307df8bae1dSRodney W. Grimes * Return any desired output. 3308df8bae1dSRodney W. Grimes */ 3309df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 331055bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 33117792ea27SJeffrey Hsu 3312a14c749fSJonathan Lemon check_delack: 33138501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 3314252ca428SRobert Watson 3315a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 33163bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 3317b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 33183bfd6421SJonathan Lemon } 33198501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3320679d9708SAndre Oppermann return; 3321df8bae1dSRodney W. Grimes 3322df8bae1dSRodney W. Grimes dropafterack: 3323df8bae1dSRodney W. Grimes /* 3324df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 3325df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 332680ab7c0eSGarrett Wollman * 332780ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 332880ab7c0eSGarrett Wollman * paths to this code happen after packets containing 332980ab7c0eSGarrett Wollman * RST have been dropped. 333080ab7c0eSGarrett Wollman * 333180ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 333280ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 333380ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 333480ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 333580ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 333680ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 3337df8bae1dSRodney W. Grimes */ 3338fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 3339fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 3340a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 3341a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 3342a57815efSBosko Milekic goto dropwithreset; 3343a57815efSBosko Milekic } 3344a0292f23SGarrett Wollman #ifdef TCPDEBUG 33454cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3346fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3347fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3348a0292f23SGarrett Wollman #endif 33492b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 3350df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 335155bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 33528501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3353d6915262SRobert Watson m_freem(m); 3354679d9708SAndre Oppermann return; 3355df8bae1dSRodney W. Grimes 3356df8bae1dSRodney W. Grimes dropwithreset: 3357a0ca0871SRobert Watson if (tp != NULL) { 3358302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 33598501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3360dd8ac7f9SRobert Watson } else 3361a0ca0871SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 3362679d9708SAndre Oppermann return; 3363df8bae1dSRodney W. Grimes 3364df8bae1dSRodney W. Grimes drop: 3365df8bae1dSRodney W. Grimes /* 3366df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 3367df8bae1dSRodney W. Grimes */ 3368610ee2f9SDavid Greenman #ifdef TCPDEBUG 33691c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 3370fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3371fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3372a0292f23SGarrett Wollman #endif 33732b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 33744d0770f1SRichard Scheffenegger if (tp != NULL) { 33758501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 33764d0770f1SRichard Scheffenegger } 3377d6915262SRobert Watson m_freem(m); 3378302ce8d6SAndre Oppermann } 3379302ce8d6SAndre Oppermann 3380302ce8d6SAndre Oppermann /* 33810ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 33820ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 33830ca3f933SAndre Oppermann * tp may be NULL. 3384302ce8d6SAndre Oppermann */ 338555bceb1eSRandall Stewart void 3386302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 3387302ce8d6SAndre Oppermann int tlen, int rstreason) 3388302ce8d6SAndre Oppermann { 3389b287c6c7SBjoern A. Zeeb #ifdef INET 3390302ce8d6SAndre Oppermann struct ip *ip; 3391b287c6c7SBjoern A. Zeeb #endif 3392302ce8d6SAndre Oppermann #ifdef INET6 3393302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 3394302ce8d6SAndre Oppermann #endif 33957a3244ccSRobert Watson 33967a3244ccSRobert Watson if (tp != NULL) { 339708d9c920SGleb Smirnoff INP_LOCK_ASSERT(tp->t_inpcb); 33987a3244ccSRobert Watson } 33997a3244ccSRobert Watson 34000ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 3401302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 3402302ce8d6SAndre Oppermann goto drop; 3403302ce8d6SAndre Oppermann #ifdef INET6 3404302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 3405302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 3406302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3407302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3408302ce8d6SAndre Oppermann goto drop; 3409302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 3410b287c6c7SBjoern A. Zeeb } 3411302ce8d6SAndre Oppermann #endif 3412b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3413b287c6c7SBjoern A. Zeeb else 3414b287c6c7SBjoern A. Zeeb #endif 3415b287c6c7SBjoern A. Zeeb #ifdef INET 3416302ce8d6SAndre Oppermann { 3417302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 3418302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3419302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3420302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3421302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3422302ce8d6SAndre Oppermann goto drop; 3423302ce8d6SAndre Oppermann } 3424b287c6c7SBjoern A. Zeeb #endif 3425302ce8d6SAndre Oppermann 3426302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 3427302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 3428302ce8d6SAndre Oppermann goto drop; 3429302ce8d6SAndre Oppermann 3430302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 3431302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 3432302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 3433302ce8d6SAndre Oppermann th->th_ack, TH_RST); 3434302ce8d6SAndre Oppermann } else { 3435302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 3436302ce8d6SAndre Oppermann tlen++; 34374ddd5aadSMichael Tuexen if (th->th_flags & TH_FIN) 34384ddd5aadSMichael Tuexen tlen++; 3439302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 3440302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 3441302ce8d6SAndre Oppermann } 3442302ce8d6SAndre Oppermann return; 3443302ce8d6SAndre Oppermann drop: 3444302ce8d6SAndre Oppermann m_freem(m); 3445df8bae1dSRodney W. Grimes } 3446df8bae1dSRodney W. Grimes 3447be2ac88cSJonathan Lemon /* 3448be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 3449be2ac88cSJonathan Lemon */ 345055bceb1eSRandall Stewart void 3451ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 3452df8bae1dSRodney W. Grimes { 3453df8bae1dSRodney W. Grimes int opt, optlen; 3454df8bae1dSRodney W. Grimes 3455be2ac88cSJonathan Lemon to->to_flags = 0; 3456df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 3457df8bae1dSRodney W. Grimes opt = cp[0]; 3458df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 3459df8bae1dSRodney W. Grimes break; 3460df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 3461df8bae1dSRodney W. Grimes optlen = 1; 3462df8bae1dSRodney W. Grimes else { 3463b474779fSJun-ichiro itojun Hagino if (cnt < 2) 3464b474779fSJun-ichiro itojun Hagino break; 3465df8bae1dSRodney W. Grimes optlen = cp[1]; 3466b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 3467df8bae1dSRodney W. Grimes break; 3468df8bae1dSRodney W. Grimes } 3469df8bae1dSRodney W. Grimes switch (opt) { 3470df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 3471df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 3472df8bae1dSRodney W. Grimes continue; 3473f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3474df8bae1dSRodney W. Grimes continue; 3475be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 3476be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 3477be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 3478fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 3479df8bae1dSRodney W. Grimes break; 3480df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 3481df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 3482df8bae1dSRodney W. Grimes continue; 3483f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3484df8bae1dSRodney W. Grimes continue; 3485be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 348602a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 3487df8bae1dSRodney W. Grimes break; 3488df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 3489df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 3490df8bae1dSRodney W. Grimes continue; 3491be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 3492a0292f23SGarrett Wollman bcopy((char *)cp + 2, 3493a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 3494fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 3495a0292f23SGarrett Wollman bcopy((char *)cp + 6, 3496a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 3497fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 3498df8bae1dSRodney W. Grimes break; 34991cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 3500fcf59617SAndrey V. Elsukov /* 3501fcf59617SAndrey V. Elsukov * In order to reply to a host which has set the 3502fcf59617SAndrey V. Elsukov * TCP_SIGNATURE option in its initial SYN, we have 3503fcf59617SAndrey V. Elsukov * to record the fact that the option was observed 3504fcf59617SAndrey V. Elsukov * here for the syncache code to perform the correct 3505fcf59617SAndrey V. Elsukov * response. 3506fcf59617SAndrey V. Elsukov */ 35071cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 35081cfd4b53SBruce M Simpson continue; 3509df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 3510df47e437SAndre Oppermann to->to_signature = cp + 2; 35111cfd4b53SBruce M Simpson break; 35126d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 3513f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 35146d90faf3SPaul Saab continue; 3515f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3516f72167f4SAndre Oppermann continue; 3517603724d3SBjoern A. Zeeb if (!V_tcp_do_sack) 3518f72167f4SAndre Oppermann continue; 3519fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 35206d90faf3SPaul Saab break; 35216d90faf3SPaul Saab case TCPOPT_SACK: 35225a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 35236d90faf3SPaul Saab continue; 3524b728e902SAndre Oppermann if (flags & TO_SYN) 3525b728e902SAndre Oppermann continue; 3526fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 35275a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 35285a53ca16SPaul Saab to->to_sacks = cp + 2; 352978b50714SRobert Watson TCPSTAT_INC(tcps_sack_rcv_blocks); 35306d90faf3SPaul Saab break; 3531281a0fd4SPatrick Kelsey case TCPOPT_FAST_OPEN: 3532c560df6fSPatrick Kelsey /* 3533c560df6fSPatrick Kelsey * Cookie length validation is performed by the 3534c560df6fSPatrick Kelsey * server side cookie checking code or the client 3535c560df6fSPatrick Kelsey * side cookie cache update code. 3536c560df6fSPatrick Kelsey */ 3537281a0fd4SPatrick Kelsey if (!(flags & TO_SYN)) 3538281a0fd4SPatrick Kelsey continue; 3539c560df6fSPatrick Kelsey if (!V_tcp_fastopen_client_enable && 3540c560df6fSPatrick Kelsey !V_tcp_fastopen_server_enable) 3541281a0fd4SPatrick Kelsey continue; 3542281a0fd4SPatrick Kelsey to->to_flags |= TOF_FASTOPEN; 3543281a0fd4SPatrick Kelsey to->to_tfo_len = optlen - 2; 3544281a0fd4SPatrick Kelsey to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL; 3545281a0fd4SPatrick Kelsey break; 3546be2ac88cSJonathan Lemon default: 3547be2ac88cSJonathan Lemon continue; 3548df8bae1dSRodney W. Grimes } 3549df8bae1dSRodney W. Grimes } 3550df8bae1dSRodney W. Grimes } 3551df8bae1dSRodney W. Grimes 3552df8bae1dSRodney W. Grimes /* 3553df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 3554df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 3555df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 3556df8bae1dSRodney W. Grimes * sequencing purposes. 3557df8bae1dSRodney W. Grimes */ 355855bceb1eSRandall Stewart void 3559ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 3560ad3f9ab3SAndre Oppermann int off) 3561df8bae1dSRodney W. Grimes { 3562fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 3563df8bae1dSRodney W. Grimes 3564df8bae1dSRodney W. Grimes while (cnt >= 0) { 3565df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 3566df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 3567df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 3568df8bae1dSRodney W. Grimes 35698501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 35707a3244ccSRobert Watson 3571df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 3572df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 3573df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 3574df8bae1dSRodney W. Grimes m->m_len--; 357569a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 357669a34685SYoshinobu Inoue m->m_pkthdr.len--; 3577df8bae1dSRodney W. Grimes return; 3578df8bae1dSRodney W. Grimes } 3579df8bae1dSRodney W. Grimes cnt -= m->m_len; 3580df8bae1dSRodney W. Grimes m = m->m_next; 35814dfdffe9SAndre Oppermann if (m == NULL) 3582df8bae1dSRodney W. Grimes break; 3583df8bae1dSRodney W. Grimes } 3584df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 3585df8bae1dSRodney W. Grimes } 3586df8bae1dSRodney W. Grimes 3587df8bae1dSRodney W. Grimes /* 3588df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 3589df8bae1dSRodney W. Grimes * and update averages and current timeout. 3590df8bae1dSRodney W. Grimes */ 359155bceb1eSRandall Stewart void 3592ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 3593df8bae1dSRodney W. Grimes { 3594ad3f9ab3SAndre Oppermann int delta; 3595233e8c18SGarrett Wollman 35968501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 35972be3bf22SRobert Watson 359878b50714SRobert Watson TCPSTAT_INC(tcps_rttupdated); 3599233e8c18SGarrett Wollman tp->t_rttupdated++; 3600adc56f5aSEdward Tomasz Napierala #ifdef STATS 3601adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, 3602adc56f5aSEdward Tomasz Napierala imax(0, rtt * 1000 / hz)); 3603adc56f5aSEdward Tomasz Napierala #endif 36045ede40dcSRyan Stone if ((tp->t_srtt != 0) && (tp->t_rxtshift <= TCP_RTT_INVALIDATE)) { 3605233e8c18SGarrett Wollman /* 3606233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 3607233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 3608233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 3609233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3610233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 3611233e8c18SGarrett Wollman */ 3612233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3613233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3614233e8c18SGarrett Wollman 3615233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 3616233e8c18SGarrett Wollman tp->t_srtt = 1; 3617233e8c18SGarrett Wollman 3618233e8c18SGarrett Wollman /* 3619233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 3620233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 3621233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 3622233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 3623233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 3624233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 3625233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3626233e8c18SGarrett Wollman * rfc793's wired-in beta. 3627233e8c18SGarrett Wollman */ 3628233e8c18SGarrett Wollman if (delta < 0) 3629233e8c18SGarrett Wollman delta = -delta; 3630233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3631233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 3632233e8c18SGarrett Wollman tp->t_rttvar = 1; 36331fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 36341fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3635233e8c18SGarrett Wollman } else { 3636233e8c18SGarrett Wollman /* 3637233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 3638233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 3639233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 3640233e8c18SGarrett Wollman */ 3641233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 3642233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 36431fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3644233e8c18SGarrett Wollman } 36459b8b58e0SJonathan Lemon tp->t_rtttime = 0; 3646df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 3647df8bae1dSRodney W. Grimes 3648df8bae1dSRodney W. Grimes /* 3649df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 3650df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 3651df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 3652df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 3653df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 3654df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 3655df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 3656df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 3657df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 3658df8bae1dSRodney W. Grimes */ 3659233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 36609e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3661df8bae1dSRodney W. Grimes 3662df8bae1dSRodney W. Grimes /* 3663df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 3664df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 3665df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 3666df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 3667df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 3668df8bae1dSRodney W. Grimes */ 3669df8bae1dSRodney W. Grimes tp->t_softerror = 0; 3670df8bae1dSRodney W. Grimes } 3671df8bae1dSRodney W. Grimes 3672df8bae1dSRodney W. Grimes /* 3673df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 3674df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 36754faaea55SAndre Oppermann * If none, use an mss that can be handled on the outgoing interface 36764faaea55SAndre Oppermann * without forcing IP to fragment. If no route is found, route has no mtu, 3677df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 3678df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 3679df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 3680df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 3681df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 3682df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 3683df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 3684a0292f23SGarrett Wollman * 36850c39d38dSGleb Smirnoff * NOTE that resulting t_maxseg doesn't include space for TCP options or 36860c39d38dSGleb Smirnoff * IP options, e.g. IPSEC data, since length of this data may vary, and 36870c39d38dSGleb Smirnoff * thus it is calculated for every segment separately in tcp_output(). 3688a0292f23SGarrett Wollman * 368997d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 3690ef341ee1SGleb Smirnoff * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS 3691ef341ee1SGleb Smirnoff * settings are handled in tcp_mssopt(). 3692df8bae1dSRodney W. Grimes */ 3693a0292f23SGarrett Wollman void 3694ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer, 36953c914c54SAndre Oppermann struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap) 3696df8bae1dSRodney W. Grimes { 3697b287c6c7SBjoern A. Zeeb int mss = 0; 36983ac12506SJonathan T. Looney uint32_t maxmtu = 0; 3699c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 370097d8d152SAndre Oppermann struct hc_metrics_lite metrics; 3701fb59c426SYoshinobu Inoue #ifdef INET6 3702c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3703c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 3704c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3705c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 3706c068736aSJeffrey Hsu #else 37079e644c23SMichael Tuexen size_t min_protoh = sizeof(struct tcpiphdr); 3708fb59c426SYoshinobu Inoue #endif 3709df8bae1dSRodney W. Grimes 37108501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 37117a3244ccSRobert Watson 37129e644c23SMichael Tuexen if (tp->t_port) 37139e644c23SMichael Tuexen min_protoh += V_tcp_udp_tunneling_overhead; 3714ef341ee1SGleb Smirnoff if (mtuoffer != -1) { 3715ef341ee1SGleb Smirnoff KASSERT(offer == -1, ("%s: conflict", __func__)); 3716ef341ee1SGleb Smirnoff offer = mtuoffer - min_protoh; 3717ef341ee1SGleb Smirnoff } 3718ef341ee1SGleb Smirnoff 3719e8949f74SAndre Oppermann /* Initialize. */ 372097d8d152SAndre Oppermann #ifdef INET6 372197d8d152SAndre Oppermann if (isipv6) { 37223c914c54SAndre Oppermann maxmtu = tcp_maxmtu6(&inp->inp_inc, cap); 37230c39d38dSGleb Smirnoff tp->t_maxseg = V_tcp_v6mssdflt; 3724b287c6c7SBjoern A. Zeeb } 372597d8d152SAndre Oppermann #endif 3726b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3727b287c6c7SBjoern A. Zeeb else 3728b287c6c7SBjoern A. Zeeb #endif 3729b287c6c7SBjoern A. Zeeb #ifdef INET 373097d8d152SAndre Oppermann { 37313c914c54SAndre Oppermann maxmtu = tcp_maxmtu(&inp->inp_inc, cap); 37320c39d38dSGleb Smirnoff tp->t_maxseg = V_tcp_mssdflt; 3733df8bae1dSRodney W. Grimes } 3734b287c6c7SBjoern A. Zeeb #endif 3735df8bae1dSRodney W. Grimes 373697d8d152SAndre Oppermann /* 3737e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 373897d8d152SAndre Oppermann */ 37396f01cac6SBjoern A. Zeeb if (maxmtu == 0) { 37406f01cac6SBjoern A. Zeeb /* 37418e5c87f4SBjoern A. Zeeb * In case we return early we need to initialize metrics 37426f01cac6SBjoern A. Zeeb * to a defined state as tcp_hc_get() would do for us 37436f01cac6SBjoern A. Zeeb * if there was no cache hit. 37446f01cac6SBjoern A. Zeeb */ 37456f01cac6SBjoern A. Zeeb if (metricptr != NULL) 37466f01cac6SBjoern A. Zeeb bzero(metricptr, sizeof(struct hc_metrics_lite)); 374797d8d152SAndre Oppermann return; 37486f01cac6SBjoern A. Zeeb } 374997d8d152SAndre Oppermann 3750e8949f74SAndre Oppermann /* What have we got? */ 375197d8d152SAndre Oppermann switch (offer) { 375297d8d152SAndre Oppermann case 0: 375397d8d152SAndre Oppermann /* 375497d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 3755c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 37560c39d38dSGleb Smirnoff * already assigned to t_maxseg above. 375797d8d152SAndre Oppermann */ 37580c39d38dSGleb Smirnoff offer = tp->t_maxseg; 375997d8d152SAndre Oppermann break; 376097d8d152SAndre Oppermann 376197d8d152SAndre Oppermann case -1: 3762a0292f23SGarrett Wollman /* 3763c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 3764a0292f23SGarrett Wollman */ 376597d8d152SAndre Oppermann /* FALLTHROUGH */ 376697d8d152SAndre Oppermann 376797d8d152SAndre Oppermann default: 3768a0292f23SGarrett Wollman /* 376953369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 377053369ac9SAndre Oppermann * to at least minmss. 377153369ac9SAndre Oppermann */ 3772603724d3SBjoern A. Zeeb offer = max(offer, V_tcp_minmss); 377397d8d152SAndre Oppermann } 3774a0292f23SGarrett Wollman 3775df8bae1dSRodney W. Grimes /* 3776e8949f74SAndre Oppermann * rmx information is now retrieved from tcp_hostcache. 3777df8bae1dSRodney W. Grimes */ 377897d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 37793cee92e0SBjoern A. Zeeb if (metricptr != NULL) 37803cee92e0SBjoern A. Zeeb bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 378197d8d152SAndre Oppermann 3782df8bae1dSRodney W. Grimes /* 3783cc412412SHiren Panchasara * If there's a discovered mtu in tcp hostcache, use it. 3784cc412412SHiren Panchasara * Else, use the link mtu. 3785df8bae1dSRodney W. Grimes */ 378697d8d152SAndre Oppermann if (metrics.rmx_mtu) 37872d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3788c068736aSJeffrey Hsu else { 378931b3783cSHajimu UMEMOTO #ifdef INET6 3790fb59c426SYoshinobu Inoue if (isipv6) { 379197d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3792603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 379397d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 3794603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_v6mssdflt); 3795b287c6c7SBjoern A. Zeeb } 3796b3399803SHajimu UMEMOTO #endif 3797b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3798b287c6c7SBjoern A. Zeeb else 3799b287c6c7SBjoern A. Zeeb #endif 3800b287c6c7SBjoern A. Zeeb #ifdef INET 380197d8d152SAndre Oppermann { 380297d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3803603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 380497d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 3805603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_mssdflt); 3806a0292f23SGarrett Wollman } 3807b287c6c7SBjoern A. Zeeb #endif 38083cee92e0SBjoern A. Zeeb /* 38093cee92e0SBjoern A. Zeeb * XXX - The above conditional (mss = maxmtu - min_protoh) 38103cee92e0SBjoern A. Zeeb * probably violates the TCP spec. 38113cee92e0SBjoern A. Zeeb * The problem is that, since we don't know the 38123cee92e0SBjoern A. Zeeb * other end's MSS, we are supposed to use a conservative 38133cee92e0SBjoern A. Zeeb * default. But, if we do that, then MTU discovery will 38143cee92e0SBjoern A. Zeeb * never actually take place, because the conservative 38153cee92e0SBjoern A. Zeeb * default is much less than the MTUs typically seen 38163cee92e0SBjoern A. Zeeb * on the Internet today. For the moment, we'll sweep 38173cee92e0SBjoern A. Zeeb * this under the carpet. 38183cee92e0SBjoern A. Zeeb * 38193cee92e0SBjoern A. Zeeb * The conservative default might not actually be a problem 38203cee92e0SBjoern A. Zeeb * if the only case this occurs is when sending an initial 38213cee92e0SBjoern A. Zeeb * SYN with options and data to a host we've never talked 38223cee92e0SBjoern A. Zeeb * to before. Then, they will reply with an MSS value which 38233cee92e0SBjoern A. Zeeb * will get recorded and the new parameters should get 38243cee92e0SBjoern A. Zeeb * recomputed. For Further Study. 38253cee92e0SBjoern A. Zeeb */ 382697d8d152SAndre Oppermann } 3827a0292f23SGarrett Wollman mss = min(mss, offer); 382897d8d152SAndre Oppermann 3829a0292f23SGarrett Wollman /* 38300c39d38dSGleb Smirnoff * Sanity check: make sure that maxseg will be large 38313cee92e0SBjoern A. Zeeb * enough to allow some data on segments even if the 38323cee92e0SBjoern A. Zeeb * all the option space is used (40bytes). Otherwise 38333cee92e0SBjoern A. Zeeb * funny things may happen in tcp_output. 38340c39d38dSGleb Smirnoff * 38350c39d38dSGleb Smirnoff * XXXGL: shouldn't we reserve space for IP/IPv6 options? 38363cee92e0SBjoern A. Zeeb */ 38373cee92e0SBjoern A. Zeeb mss = max(mss, 64); 38383cee92e0SBjoern A. Zeeb 383997d8d152SAndre Oppermann tp->t_maxseg = mss; 38403cee92e0SBjoern A. Zeeb } 38413cee92e0SBjoern A. Zeeb 38423cee92e0SBjoern A. Zeeb void 38433cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer) 38443cee92e0SBjoern A. Zeeb { 3845dbc42409SLawrence Stewart int mss; 38463ac12506SJonathan T. Looney uint32_t bufsize; 38473cee92e0SBjoern A. Zeeb struct inpcb *inp; 38483cee92e0SBjoern A. Zeeb struct socket *so; 38493cee92e0SBjoern A. Zeeb struct hc_metrics_lite metrics; 38503c914c54SAndre Oppermann struct tcp_ifcap cap; 3851dbc42409SLawrence Stewart 38523cee92e0SBjoern A. Zeeb KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 38533cee92e0SBjoern A. Zeeb 38543c914c54SAndre Oppermann bzero(&cap, sizeof(cap)); 38553c914c54SAndre Oppermann tcp_mss_update(tp, offer, -1, &metrics, &cap); 38563cee92e0SBjoern A. Zeeb 38573cee92e0SBjoern A. Zeeb mss = tp->t_maxseg; 38583cee92e0SBjoern A. Zeeb inp = tp->t_inpcb; 385997d8d152SAndre Oppermann 3860df8bae1dSRodney W. Grimes /* 386197d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 386297d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 386397d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 386497d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 386597d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3866df8bae1dSRodney W. Grimes */ 3867c3b02504SBjoern A. Zeeb so = inp->inp_socket; 38683f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 3869e233e2acSAndre Oppermann if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe) 387097d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 387197d8d152SAndre Oppermann else 3872df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3873df8bae1dSRodney W. Grimes if (bufsize < mss) 3874df8bae1dSRodney W. Grimes mss = bufsize; 3875df8bae1dSRodney W. Grimes else { 3876df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3877df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3878df8bae1dSRodney W. Grimes bufsize = sb_max; 387988c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 38803f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3881df8bae1dSRodney W. Grimes } 38823f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 3883784ce8faSHiren Panchasara /* 3884784ce8faSHiren Panchasara * Sanity check: make sure that maxseg will be large 3885784ce8faSHiren Panchasara * enough to allow some data on segments even if the 3886784ce8faSHiren Panchasara * all the option space is used (40bytes). Otherwise 3887784ce8faSHiren Panchasara * funny things may happen in tcp_output. 3888784ce8faSHiren Panchasara * 3889784ce8faSHiren Panchasara * XXXGL: shouldn't we reserve space for IP/IPv6 options? 3890784ce8faSHiren Panchasara */ 3891784ce8faSHiren Panchasara tp->t_maxseg = max(mss, 64); 3892df8bae1dSRodney W. Grimes 38933f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3894e233e2acSAndre Oppermann if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe) 389597d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 389697d8d152SAndre Oppermann else 3897df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3898df8bae1dSRodney W. Grimes if (bufsize > mss) { 3899df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3900df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3901df8bae1dSRodney W. Grimes bufsize = sb_max; 390288c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 39033f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3904df8bae1dSRodney W. Grimes } 39053f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 390691d6cfa6SBjoern A. Zeeb 390791d6cfa6SBjoern A. Zeeb /* Check the interface for TSO capabilities. */ 39083c914c54SAndre Oppermann if (cap.ifcap & CSUM_TSO) { 390991d6cfa6SBjoern A. Zeeb tp->t_flags |= TF_TSO; 39103c914c54SAndre Oppermann tp->t_tsomax = cap.tsomax; 39119fd573c3SHans Petter Selasky tp->t_tsomaxsegcount = cap.tsomaxsegcount; 39129fd573c3SHans Petter Selasky tp->t_tsomaxsegsize = cap.tsomaxsegsize; 39133c914c54SAndre Oppermann } 3914a0292f23SGarrett Wollman } 3915a0292f23SGarrett Wollman 3916a0292f23SGarrett Wollman /* 3917a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3918a0292f23SGarrett Wollman */ 3919a0292f23SGarrett Wollman int 3920ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 3921a0292f23SGarrett Wollman { 392297d8d152SAndre Oppermann int mss = 0; 39233ac12506SJonathan T. Looney uint32_t thcmtu = 0; 39243ac12506SJonathan T. Looney uint32_t maxmtu = 0; 392597d8d152SAndre Oppermann size_t min_protoh; 3926a0292f23SGarrett Wollman 392797d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3928a0292f23SGarrett Wollman 392931b3783cSHajimu UMEMOTO #ifdef INET6 3930dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3931603724d3SBjoern A. Zeeb mss = V_tcp_v6mssdflt; 3932233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 393397d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 3934b287c6c7SBjoern A. Zeeb } 393531b3783cSHajimu UMEMOTO #endif 3936b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3937b287c6c7SBjoern A. Zeeb else 3938b287c6c7SBjoern A. Zeeb #endif 3939b287c6c7SBjoern A. Zeeb #ifdef INET 394097d8d152SAndre Oppermann { 3941603724d3SBjoern A. Zeeb mss = V_tcp_mssdflt; 3942233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 394397d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 394497d8d152SAndre Oppermann } 3945b287c6c7SBjoern A. Zeeb #endif 39463a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET) 39473a9391deSBjoern A. Zeeb thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 39483a9391deSBjoern A. Zeeb #endif 39493a9391deSBjoern A. Zeeb 395097d8d152SAndre Oppermann if (maxmtu && thcmtu) 395197d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 395297d8d152SAndre Oppermann else if (maxmtu || thcmtu) 395397d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 395497d8d152SAndre Oppermann 395597d8d152SAndre Oppermann return (mss); 3956df8bae1dSRodney W. Grimes } 395746f58482SJonathan Lemon 39580e1d7c25SRichard Scheffenegger void 395990cca08eSRichard Scheffenegger tcp_do_prr_ack(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to) 39600e1d7c25SRichard Scheffenegger { 396148396dc7SRichard Scheffenegger int snd_cnt = 0, limit = 0, del_data = 0, pipe = 0; 39620e1d7c25SRichard Scheffenegger int maxseg = tcp_maxseg(tp); 39630e1d7c25SRichard Scheffenegger 39640e1d7c25SRichard Scheffenegger INP_WLOCK_ASSERT(tp->t_inpcb); 39650e1d7c25SRichard Scheffenegger 39660e1d7c25SRichard Scheffenegger /* 39670e1d7c25SRichard Scheffenegger * Compute the amount of data that this ACK is indicating 39680e1d7c25SRichard Scheffenegger * (del_data) and an estimate of how many bytes are in the 39690e1d7c25SRichard Scheffenegger * network. 39700e1d7c25SRichard Scheffenegger */ 397131d7a27cSRichard Scheffenegger del_data = tp->sackhint.delivered_data; 3972d1de2b05SRichard Scheffenegger if (V_tcp_do_newsack) 3973a8e431e1SRichard Scheffenegger pipe = tcp_compute_pipe(tp); 3974a8e431e1SRichard Scheffenegger else 39750e1d7c25SRichard Scheffenegger pipe = (tp->snd_nxt - tp->snd_fack) + tp->sackhint.sack_bytes_rexmit; 39760e1d7c25SRichard Scheffenegger tp->sackhint.prr_delivered += del_data; 39770e1d7c25SRichard Scheffenegger /* 39780e1d7c25SRichard Scheffenegger * Proportional Rate Reduction 39790e1d7c25SRichard Scheffenegger */ 3980e9071000SRichard Scheffenegger if (pipe >= tp->snd_ssthresh) { 39816a376af0SRichard Scheffenegger if (tp->sackhint.recover_fs == 0) 39826a376af0SRichard Scheffenegger tp->sackhint.recover_fs = 398348396dc7SRichard Scheffenegger imax(1, tp->snd_nxt - tp->snd_una); 398448396dc7SRichard Scheffenegger snd_cnt = howmany((long)tp->sackhint.prr_delivered * 398548396dc7SRichard Scheffenegger tp->snd_ssthresh, tp->sackhint.recover_fs) - 3986e5313869SRichard Scheffenegger tp->sackhint.prr_out; 39876a376af0SRichard Scheffenegger } else { 39880e1d7c25SRichard Scheffenegger if (V_tcp_do_prr_conservative) 398984761f3dSRichard Scheffenegger limit = tp->sackhint.prr_delivered - 3990e5313869SRichard Scheffenegger tp->sackhint.prr_out; 39910e1d7c25SRichard Scheffenegger else 399248396dc7SRichard Scheffenegger limit = imax(tp->sackhint.prr_delivered - 3993e5313869SRichard Scheffenegger tp->sackhint.prr_out, del_data) + 3994e5313869SRichard Scheffenegger maxseg; 399548396dc7SRichard Scheffenegger snd_cnt = imin((tp->snd_ssthresh - pipe), limit); 39960e1d7c25SRichard Scheffenegger } 399748396dc7SRichard Scheffenegger snd_cnt = imax(snd_cnt, 0) / maxseg; 39980e1d7c25SRichard Scheffenegger /* 39990e1d7c25SRichard Scheffenegger * Send snd_cnt new data into the network in response to this ack. 40000e1d7c25SRichard Scheffenegger * If there is going to be a SACK retransmission, adjust snd_cwnd 40010e1d7c25SRichard Scheffenegger * accordingly. 40020e1d7c25SRichard Scheffenegger */ 4003b9f803b7SRichard Scheffenegger if (IN_FASTRECOVERY(tp->t_flags)) { 400448396dc7SRichard Scheffenegger tp->snd_cwnd = imax(maxseg, tp->snd_nxt - tp->snd_recover + 4005853fd7a2SRichard Scheffenegger tp->sackhint.sack_bytes_rexmit + (snd_cnt * maxseg)); 4006b9f803b7SRichard Scheffenegger } else if (IN_CONGRECOVERY(tp->t_flags)) 4007b9f803b7SRichard Scheffenegger tp->snd_cwnd = imax(maxseg, pipe - del_data + 4008b9f803b7SRichard Scheffenegger (snd_cnt * maxseg)); 40090e1d7c25SRichard Scheffenegger } 40100e1d7c25SRichard Scheffenegger 401146f58482SJonathan Lemon /* 4012c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 4013c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 4014c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 4015c068736aSJeffrey Hsu * be started again. 401646f58482SJonathan Lemon */ 401755bceb1eSRandall Stewart void 4018ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 401946f58482SJonathan Lemon { 402046f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 40213ac12506SJonathan T. Looney uint32_t ocwnd = tp->snd_cwnd; 40220c39d38dSGleb Smirnoff u_int maxseg = tcp_maxseg(tp); 402346f58482SJonathan Lemon 40248501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 40257a3244ccSRobert Watson 4026b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 402746f58482SJonathan Lemon tp->t_rtttime = 0; 402846f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 40296b2a5f92SJayanth Vijayaraghavan /* 4030c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 4031c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 40326b2a5f92SJayanth Vijayaraghavan */ 40330c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th); 4034a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 403555bceb1eSRandall Stewart (void) tp->t_fb->tfb_tcp_output(tp); 403646f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 403746f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 403846f58482SJonathan Lemon tp->snd_nxt = onxt; 403946f58482SJonathan Lemon /* 404046f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 404146f58482SJonathan Lemon * not updated yet. 404246f58482SJonathan Lemon */ 4043dbc42409SLawrence Stewart if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th)) 4044dbc42409SLawrence Stewart tp->snd_cwnd -= BYTES_THIS_ACK(tp, th); 4045d7587117SPaul Saab else 4046d7587117SPaul Saab tp->snd_cwnd = 0; 40470c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 404846f58482SJonathan Lemon } 404912eeb81fSHiren Panchasara 405012eeb81fSHiren Panchasara int 405112eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp) 405212eeb81fSHiren Panchasara { 405312eeb81fSHiren Panchasara return (tp->snd_max - tp->snd_una + 405412eeb81fSHiren Panchasara tp->sackhint.sack_bytes_rexmit - 405512eeb81fSHiren Panchasara tp->sackhint.sacked_bytes); 405612eeb81fSHiren Panchasara } 40577dc90a1dSMichael Tuexen 40587dc90a1dSMichael Tuexen uint32_t 40597dc90a1dSMichael Tuexen tcp_compute_initwnd(uint32_t maxseg) 40607dc90a1dSMichael Tuexen { 40617dc90a1dSMichael Tuexen /* 40627dc90a1dSMichael Tuexen * Calculate the Initial Window, also used as Restart Window 40637dc90a1dSMichael Tuexen * 40647dc90a1dSMichael Tuexen * RFC5681 Section 3.1 specifies the default conservative values. 40657dc90a1dSMichael Tuexen * RFC3390 specifies slightly more aggressive values. 40667dc90a1dSMichael Tuexen * RFC6928 increases it to ten segments. 40677dc90a1dSMichael Tuexen * Support for user specified value for initial flight size. 40687dc90a1dSMichael Tuexen */ 40697dc90a1dSMichael Tuexen if (V_tcp_initcwnd_segments) 40707dc90a1dSMichael Tuexen return min(V_tcp_initcwnd_segments * maxseg, 40717dc90a1dSMichael Tuexen max(2 * maxseg, V_tcp_initcwnd_segments * 1460)); 40727dc90a1dSMichael Tuexen else if (V_tcp_do_rfc3390) 40737dc90a1dSMichael Tuexen return min(4 * maxseg, max(2 * maxseg, 4380)); 40747dc90a1dSMichael Tuexen else { 40757dc90a1dSMichael Tuexen /* Per RFC5681 Section 3.1 */ 40767dc90a1dSMichael Tuexen if (maxseg > 2190) 40777dc90a1dSMichael Tuexen return (2 * maxseg); 40787dc90a1dSMichael Tuexen else if (maxseg > 1095) 40797dc90a1dSMichael Tuexen return (3 * maxseg); 40807dc90a1dSMichael Tuexen else 40817dc90a1dSMichael Tuexen return (4 * maxseg); 40827dc90a1dSMichael Tuexen } 40837dc90a1dSMichael Tuexen } 4084