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 126f7220c48SRichard Scheffenegger #include <netinet/tcp_ecn.h> 1279e644c23SMichael Tuexen #include <netinet/udp.h> 128fb59c426SYoshinobu Inoue 129fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 130b9234fafSSam Leffler 131db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 132db4f9cc7SJonathan Lemon 133aed55708SRobert Watson #include <security/mac/mac_framework.h> 134aed55708SRobert Watson 135dbc42409SLawrence Stewart const int tcprexmtthresh = 3; 1360312fbe9SPoul-Henning Kamp 137334fc582SBjoern A. Zeeb VNET_DEFINE(int, tcp_log_in_vain) = 0; 138334fc582SBjoern A. Zeeb SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_VNET | CTLFLAG_RW, 139334fc582SBjoern A. Zeeb &VNET_NAME(tcp_log_in_vain), 0, 140eddfbb76SRobert Watson "Log all incoming TCP segments to closed ports"); 141816a3d83SPoul-Henning Kamp 14282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0; 14382cea7e6SBjoern A. Zeeb #define V_blackhole VNET(blackhole) 1446df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW, 145eddfbb76SRobert Watson &VNET_NAME(blackhole), 0, 146eddfbb76SRobert Watson "Do not send RST on segments to closed ports"); 14716f7f31fSGeoff Rehmet 1483ea9a7cfSGleb Smirnoff VNET_DEFINE(bool, blackhole_local) = false; 1493ea9a7cfSGleb Smirnoff #define V_blackhole_local VNET(blackhole_local) 1503ea9a7cfSGleb Smirnoff SYSCTL_BOOL(_net_inet_tcp, OID_AUTO, blackhole_local, CTLFLAG_VNET | 1513ea9a7cfSGleb Smirnoff CTLFLAG_RW, &VNET_NAME(blackhole_local), false, 1523ea9a7cfSGleb Smirnoff "Enforce net.inet.tcp.blackhole for locally originated packets"); 1533ea9a7cfSGleb Smirnoff 15482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1; 1556df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW, 156eddfbb76SRobert Watson &VNET_NAME(tcp_delack_enabled), 0, 1573d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 158f498eeeeSDavid Greenman 15982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0; 1606df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW, 161eddfbb76SRobert Watson &VNET_NAME(drop_synfin), 0, 162eddfbb76SRobert Watson "Drop TCP packets with SYN+FIN set"); 163f8613305SDag-Erling Smørgrav 1640e1d7c25SRichard Scheffenegger VNET_DEFINE(int, tcp_do_prr_conservative) = 0; 1650e1d7c25SRichard Scheffenegger SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_prr_conservative, CTLFLAG_VNET | CTLFLAG_RW, 1660e1d7c25SRichard Scheffenegger &VNET_NAME(tcp_do_prr_conservative), 0, 1670e1d7c25SRichard Scheffenegger "Do conservative Proportional Rate Reduction"); 1680e1d7c25SRichard Scheffenegger 1690e1d7c25SRichard Scheffenegger VNET_DEFINE(int, tcp_do_prr) = 1; 1700e1d7c25SRichard Scheffenegger SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_prr, CTLFLAG_VNET | CTLFLAG_RW, 1710e1d7c25SRichard Scheffenegger &VNET_NAME(tcp_do_prr), 1, 1720e1d7c25SRichard Scheffenegger "Enable Proportional Rate Reduction per RFC 6937"); 1730e1d7c25SRichard Scheffenegger 1740471a8c7SRichard Scheffenegger VNET_DEFINE(int, tcp_do_lrd) = 0; 1750471a8c7SRichard Scheffenegger SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_lrd, CTLFLAG_VNET | CTLFLAG_RW, 1760471a8c7SRichard Scheffenegger &VNET_NAME(tcp_do_lrd), 1, 1770471a8c7SRichard Scheffenegger "Perform Lost Retransmission Detection"); 1780471a8c7SRichard Scheffenegger 179b72e56e7SMichael Tuexen VNET_DEFINE(int, tcp_do_newcwv) = 0; 180b72e56e7SMichael Tuexen SYSCTL_INT(_net_inet_tcp, OID_AUTO, newcwv, CTLFLAG_VNET | CTLFLAG_RW, 181b72e56e7SMichael Tuexen &VNET_NAME(tcp_do_newcwv), 0, 182b72e56e7SMichael Tuexen "Enable New Congestion Window Validation per RFC7661"); 183b72e56e7SMichael Tuexen 18482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1; 1856df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW, 186eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3042), 0, 187eddfbb76SRobert Watson "Enable RFC 3042 (Limited Transmit)"); 188582a954bSJeffrey Hsu 18982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1; 1906df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW, 191eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3390), 0, 192da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 193da3a8a1aSJeffrey Hsu 194356c7958SHiren Panchasara VNET_DEFINE(int, tcp_initcwnd_segments) = 10; 195356c7958SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments, 196356c7958SHiren Panchasara CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0, 197356c7958SHiren Panchasara "Slow-start flight size (initial congestion window) in number of segments"); 19809440655SAndre Oppermann 19982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1; 2006df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW, 201eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3465), 0, 20224cb0f22SLawrence Stewart "Enable RFC 3465 (Appropriate Byte Counting)"); 203eddfbb76SRobert Watson 20482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2; 2056df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW, 206eddfbb76SRobert Watson &VNET_NAME(tcp_abc_l_var), 2, 20724cb0f22SLawrence Stewart "Cap the max cwnd increment during slow-start to this number of segments"); 20824cb0f22SLawrence Stewart 2097029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, 2107029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 2117029da5cSPawel Biernacki "TCP ECN"); 212f2512ba1SRui Paulo 213883054b4SDon Lewis VNET_DEFINE(int, tcp_do_ecn) = 2; 2146df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW, 215eddfbb76SRobert Watson &VNET_NAME(tcp_do_ecn), 0, 216eddfbb76SRobert Watson "TCP ECN support"); 217eddfbb76SRobert Watson 21882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1; 2196df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_VNET | CTLFLAG_RW, 220eddfbb76SRobert Watson &VNET_NAME(tcp_ecn_maxretries), 0, 221eddfbb76SRobert Watson "Max retries before giving up on ECN"); 222eddfbb76SRobert Watson 2233220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0; 2246df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW, 2253220a212SGleb Smirnoff &VNET_NAME(tcp_insecure_syn), 0, 2263220a212SGleb Smirnoff "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets"); 2273220a212SGleb Smirnoff 22882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0; 2296df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW, 230eddfbb76SRobert Watson &VNET_NAME(tcp_insecure_rst), 0, 2313220a212SGleb Smirnoff "Follow RFC793 instead of RFC5961 criteria for accepting RST packets"); 232a69968eeSMike Silbersack 233fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64; 234873789cbSAndre Oppermann #define V_tcp_recvspace VNET(tcp_recvspace) 2356df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW, 236873789cbSAndre Oppermann &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size"); 237873789cbSAndre Oppermann 23882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1; 2396df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW, 240eddfbb76SRobert Watson &VNET_NAME(tcp_do_autorcvbuf), 0, 2418b615593SMarko Zec "Enable automatic receive buffer sizing"); 2426741ecf5SAndre Oppermann 243b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024; 2446df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW, 245eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_max), 0, 2468b615593SMarko Zec "Max size of automatic receive buffer"); 2476741ecf5SAndre Oppermann 24882cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo); 249df8bae1dSRodney W. Grimes 250315e3e38SRobert Watson /* 251bf840a17SGleb Smirnoff * TCP statistics are stored in an array of counter(9)s, which size matches 252bf840a17SGleb Smirnoff * size of struct tcpstat. TCP running connection count is a regular array. 2535923c293SGleb Smirnoff */ 2545da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat); 2555da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat, 2565da0521fSAndrey V. Elsukov tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 257bf840a17SGleb Smirnoff VNET_DEFINE(counter_u64_t, tcps_states[TCP_NSTATES]); 258bf840a17SGleb Smirnoff SYSCTL_COUNTER_U64_ARRAY(_net_inet_tcp, TCPCTL_STATES, states, CTLFLAG_RD | 259d4d32b9fSHans Petter Selasky CTLFLAG_VNET, &VNET_NAME(tcps_states)[0], TCP_NSTATES, 260bf840a17SGleb Smirnoff "TCP connection counts by TCP state"); 261bf840a17SGleb Smirnoff 2625923c293SGleb Smirnoff /* 2637ca6e296SMichael Tuexen * Kernel module interface for updating tcpstat. The first argument is an index 2645923c293SGleb Smirnoff * into tcpstat treated as an array. 265315e3e38SRobert Watson */ 266315e3e38SRobert Watson void 2677ca6e296SMichael Tuexen kmod_tcpstat_add(int statnum, int val) 268315e3e38SRobert Watson { 269315e3e38SRobert Watson 2707ca6e296SMichael Tuexen counter_u64_add(VNET(tcpstat)[statnum], val); 271315e3e38SRobert Watson } 272315e3e38SRobert Watson 273bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 274dbc42409SLawrence Stewart /* 27539bc9de5SLawrence Stewart * Wrapper for the TCP established input helper hook. 27639bc9de5SLawrence Stewart */ 27755bceb1eSRandall Stewart void 27839bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to) 27939bc9de5SLawrence Stewart { 28039bc9de5SLawrence Stewart struct tcp_hhook_data hhook_data; 28139bc9de5SLawrence Stewart 28239bc9de5SLawrence Stewart if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) { 28339bc9de5SLawrence Stewart hhook_data.tp = tp; 28439bc9de5SLawrence Stewart hhook_data.th = th; 28539bc9de5SLawrence Stewart hhook_data.to = to; 28639bc9de5SLawrence Stewart 28739bc9de5SLawrence Stewart hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data, 28839bc9de5SLawrence Stewart tp->osd); 28939bc9de5SLawrence Stewart } 29039bc9de5SLawrence Stewart } 291bd79708dSJonathan T. Looney #endif 29239bc9de5SLawrence Stewart 29339bc9de5SLawrence Stewart /* 294dbc42409SLawrence Stewart * CC wrapper hook functions 295dbc42409SLawrence Stewart */ 29655bceb1eSRandall Stewart void 2974b7b743cSLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t nsegs, 2984b7b743cSLawrence Stewart uint16_t type) 299f2512ba1SRui Paulo { 300adc56f5aSEdward Tomasz Napierala #ifdef STATS 301adc56f5aSEdward Tomasz Napierala int32_t gput; 302adc56f5aSEdward Tomasz Napierala #endif 303adc56f5aSEdward Tomasz Napierala 304dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 305f2512ba1SRui Paulo 3064b7b743cSLawrence Stewart tp->ccv->nsegs = nsegs; 307dbc42409SLawrence Stewart tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th); 308b72e56e7SMichael Tuexen if ((!V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd)) || 309b72e56e7SMichael Tuexen (V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd) && 310b72e56e7SMichael Tuexen (tp->snd_cwnd < (tcp_compute_pipe(tp) * 2)))) 311dbc42409SLawrence Stewart tp->ccv->flags |= CCF_CWND_LIMITED; 312dbc42409SLawrence Stewart else 313dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_CWND_LIMITED; 314dbc42409SLawrence Stewart 315dbc42409SLawrence Stewart if (type == CC_ACK) { 316adc56f5aSEdward Tomasz Napierala #ifdef STATS 317adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF, 318adc56f5aSEdward Tomasz Napierala ((int32_t)tp->snd_cwnd) - tp->snd_wnd); 319adc56f5aSEdward Tomasz Napierala if (!IN_RECOVERY(tp->t_flags)) 320adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_ACKLEN, 321adc56f5aSEdward Tomasz Napierala tp->ccv->bytes_this_ack / (tcp_maxseg(tp) * nsegs)); 322adc56f5aSEdward Tomasz Napierala if ((tp->t_flags & TF_GPUTINPROG) && 323adc56f5aSEdward Tomasz Napierala SEQ_GEQ(th->th_ack, tp->gput_ack)) { 324adc56f5aSEdward Tomasz Napierala /* 325adc56f5aSEdward Tomasz Napierala * Compute goodput in bits per millisecond. 326adc56f5aSEdward Tomasz Napierala */ 327*f5766992SHans Petter Selasky gput = (((int64_t)SEQ_SUB(th->th_ack, tp->gput_seq)) << 3) / 328adc56f5aSEdward Tomasz Napierala max(1, tcp_ts_getticks() - tp->gput_ts); 329adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT, 330adc56f5aSEdward Tomasz Napierala gput); 331adc56f5aSEdward Tomasz Napierala /* 332adc56f5aSEdward Tomasz Napierala * XXXLAS: This is a temporary hack, and should be 333adc56f5aSEdward Tomasz Napierala * chained off VOI_TCP_GPUT when stats(9) grows an API 334adc56f5aSEdward Tomasz Napierala * to deal with chained VOIs. 335adc56f5aSEdward Tomasz Napierala */ 336adc56f5aSEdward Tomasz Napierala if (tp->t_stats_gput_prev > 0) 337adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_s32(tp->t_stats, 338adc56f5aSEdward Tomasz Napierala VOI_TCP_GPUT_ND, 339adc56f5aSEdward Tomasz Napierala ((gput - tp->t_stats_gput_prev) * 100) / 340adc56f5aSEdward Tomasz Napierala tp->t_stats_gput_prev); 341adc56f5aSEdward Tomasz Napierala tp->t_flags &= ~TF_GPUTINPROG; 342adc56f5aSEdward Tomasz Napierala tp->t_stats_gput_prev = gput; 343adc56f5aSEdward Tomasz Napierala } 344adc56f5aSEdward Tomasz Napierala #endif /* STATS */ 345dbc42409SLawrence Stewart if (tp->snd_cwnd > tp->snd_ssthresh) { 3461951fa79SMichael Tuexen tp->t_bytes_acked += tp->ccv->bytes_this_ack; 347dbc42409SLawrence Stewart if (tp->t_bytes_acked >= tp->snd_cwnd) { 348dbc42409SLawrence Stewart tp->t_bytes_acked -= tp->snd_cwnd; 349dbc42409SLawrence Stewart tp->ccv->flags |= CCF_ABC_SENTAWND; 350dbc42409SLawrence Stewart } 351dbc42409SLawrence Stewart } else { 352dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_ABC_SENTAWND; 353dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 354dbc42409SLawrence Stewart } 355dbc42409SLawrence Stewart } 356dbc42409SLawrence Stewart 357dbc42409SLawrence Stewart if (CC_ALGO(tp)->ack_received != NULL) { 358dbc42409SLawrence Stewart /* XXXLAS: Find a way to live without this */ 359dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 360dbc42409SLawrence Stewart CC_ALGO(tp)->ack_received(tp->ccv, type); 361dbc42409SLawrence Stewart } 362adc56f5aSEdward Tomasz Napierala #ifdef STATS 363adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, tp->snd_cwnd); 364adc56f5aSEdward Tomasz Napierala #endif 365dbc42409SLawrence Stewart } 366dbc42409SLawrence Stewart 36755bceb1eSRandall Stewart void 368dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp) 369dbc42409SLawrence Stewart { 370dbc42409SLawrence Stewart struct hc_metrics_lite metrics; 371dbc42409SLawrence Stewart struct inpcb *inp = tp->t_inpcb; 3720c39d38dSGleb Smirnoff u_int maxseg; 373dbc42409SLawrence Stewart int rtt; 374dbc42409SLawrence Stewart 375dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 376dbc42409SLawrence Stewart 377dbc42409SLawrence Stewart tcp_hc_get(&inp->inp_inc, &metrics); 3780c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 379dbc42409SLawrence Stewart 380dbc42409SLawrence Stewart if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 381dbc42409SLawrence Stewart tp->t_srtt = rtt; 382dbc42409SLawrence Stewart tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 383dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrtt); 384dbc42409SLawrence Stewart if (metrics.rmx_rttvar) { 385dbc42409SLawrence Stewart tp->t_rttvar = metrics.rmx_rttvar; 386dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrttvar); 387dbc42409SLawrence Stewart } else { 388dbc42409SLawrence Stewart /* default variation is +- 1 rtt */ 389dbc42409SLawrence Stewart tp->t_rttvar = 390dbc42409SLawrence Stewart tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 391dbc42409SLawrence Stewart } 392dbc42409SLawrence Stewart TCPT_RANGESET(tp->t_rxtcur, 393dbc42409SLawrence Stewart ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 394dbc42409SLawrence Stewart tp->t_rttmin, TCPTV_REXMTMAX); 395dbc42409SLawrence Stewart } 396dbc42409SLawrence Stewart if (metrics.rmx_ssthresh) { 397dbc42409SLawrence Stewart /* 398dbc42409SLawrence Stewart * There's some sort of gateway or interface 399dbc42409SLawrence Stewart * buffer limit on the path. Use this to set 400a4641f4eSPedro F. Giffuni * the slow start threshold, but set the 401dbc42409SLawrence Stewart * threshold to no less than 2*mss. 402dbc42409SLawrence Stewart */ 4030c39d38dSGleb Smirnoff tp->snd_ssthresh = max(2 * maxseg, metrics.rmx_ssthresh); 404dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedssthresh); 405dbc42409SLawrence Stewart } 406dbc42409SLawrence Stewart 407dbc42409SLawrence Stewart /* 4089ec4a4ccSAndre Oppermann * Set the initial slow-start flight size. 409dbc42409SLawrence Stewart * 410cf8f04f4SAndre Oppermann * If a SYN or SYN/ACK was lost and retransmitted, we have to 411cf8f04f4SAndre Oppermann * reduce the initial CWND to one segment as congestion is likely 412cf8f04f4SAndre Oppermann * requiring us to be cautious. 413dbc42409SLawrence Stewart */ 414cf8f04f4SAndre Oppermann if (tp->snd_cwnd == 1) 4150c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; /* SYN(-ACK) lost */ 416dbc42409SLawrence Stewart else 4177dc90a1dSMichael Tuexen tp->snd_cwnd = tcp_compute_initwnd(maxseg); 418dbc42409SLawrence Stewart 419dbc42409SLawrence Stewart if (CC_ALGO(tp)->conn_init != NULL) 420dbc42409SLawrence Stewart CC_ALGO(tp)->conn_init(tp->ccv); 421dbc42409SLawrence Stewart } 422dbc42409SLawrence Stewart 423dbc42409SLawrence Stewart void inline 424dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type) 425dbc42409SLawrence Stewart { 426dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 427dbc42409SLawrence Stewart 428adc56f5aSEdward Tomasz Napierala #ifdef STATS 429adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type); 430adc56f5aSEdward Tomasz Napierala #endif 431adc56f5aSEdward Tomasz Napierala 432dbc42409SLawrence Stewart switch(type) { 433dbc42409SLawrence Stewart case CC_NDUPACK: 434dbc42409SLawrence Stewart if (!IN_FASTRECOVERY(tp->t_flags)) { 435f2512ba1SRui Paulo tp->snd_recover = tp->snd_max; 4363cf38784SMichael Tuexen if (tp->t_flags2 & TF2_ECN_PERMIT) 4373cf38784SMichael Tuexen tp->t_flags2 |= TF2_ECN_SND_CWR; 438f2512ba1SRui Paulo } 439dbc42409SLawrence Stewart break; 440dbc42409SLawrence Stewart case CC_ECN: 441af2fb894SRichard Scheffenegger if (!IN_CONGRECOVERY(tp->t_flags) || 442af2fb894SRichard Scheffenegger /* 443af2fb894SRichard Scheffenegger * Allow ECN reaction on ACK to CWR, if 444af2fb894SRichard Scheffenegger * that data segment was also CE marked. 445af2fb894SRichard Scheffenegger */ 446af2fb894SRichard Scheffenegger SEQ_GEQ(th->th_ack, tp->snd_recover)) { 447af2fb894SRichard Scheffenegger EXIT_CONGRECOVERY(tp->t_flags); 448dbc42409SLawrence Stewart TCPSTAT_INC(tcps_ecn_rcwnd); 449af2fb894SRichard Scheffenegger tp->snd_recover = tp->snd_max + 1; 4503cf38784SMichael Tuexen if (tp->t_flags2 & TF2_ECN_PERMIT) 4513cf38784SMichael Tuexen tp->t_flags2 |= TF2_ECN_SND_CWR; 452dbc42409SLawrence Stewart } 453dbc42409SLawrence Stewart break; 454dbc42409SLawrence Stewart case CC_RTO: 455dbc42409SLawrence Stewart tp->t_dupacks = 0; 456dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 457dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 4589cc711c9SMichael Tuexen if (tp->t_flags2 & TF2_ECN_PERMIT) 4599cc711c9SMichael Tuexen tp->t_flags2 |= TF2_ECN_SND_CWR; 460dbc42409SLawrence Stewart break; 461dbc42409SLawrence Stewart case CC_RTO_ERR: 462dbc42409SLawrence Stewart TCPSTAT_INC(tcps_sndrexmitbad); 463dbc42409SLawrence Stewart /* RTO was unnecessary, so reset everything. */ 464dbc42409SLawrence Stewart tp->snd_cwnd = tp->snd_cwnd_prev; 465dbc42409SLawrence Stewart tp->snd_ssthresh = tp->snd_ssthresh_prev; 466dbc42409SLawrence Stewart tp->snd_recover = tp->snd_recover_prev; 467dbc42409SLawrence Stewart if (tp->t_flags & TF_WASFRECOVERY) 468dbc42409SLawrence Stewart ENTER_FASTRECOVERY(tp->t_flags); 469dbc42409SLawrence Stewart if (tp->t_flags & TF_WASCRECOVERY) 470dbc42409SLawrence Stewart ENTER_CONGRECOVERY(tp->t_flags); 471dbc42409SLawrence Stewart tp->snd_nxt = tp->snd_max; 472672dc4aeSJohn Baldwin tp->t_flags &= ~TF_PREVVALID; 473dbc42409SLawrence Stewart tp->t_badrxtwin = 0; 474dbc42409SLawrence Stewart break; 475dbc42409SLawrence Stewart } 476dbc42409SLawrence Stewart 477dbc42409SLawrence Stewart if (CC_ALGO(tp)->cong_signal != NULL) { 478dbc42409SLawrence Stewart if (th != NULL) 479dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 480dbc42409SLawrence Stewart CC_ALGO(tp)->cong_signal(tp->ccv, type); 481dbc42409SLawrence Stewart } 482dbc42409SLawrence Stewart } 483dbc42409SLawrence Stewart 48455bceb1eSRandall Stewart void inline 485dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th) 486dbc42409SLawrence Stewart { 487dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 488dbc42409SLawrence Stewart 489dbc42409SLawrence Stewart /* XXXLAS: KASSERT that we're in recovery? */ 490dbc42409SLawrence Stewart 491dbc42409SLawrence Stewart if (CC_ALGO(tp)->post_recovery != NULL) { 492dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 493dbc42409SLawrence Stewart CC_ALGO(tp)->post_recovery(tp->ccv); 494dbc42409SLawrence Stewart } 495dbc42409SLawrence Stewart /* XXXLAS: EXIT_RECOVERY ? */ 496dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 49748be5b97SRichard Scheffenegger tp->sackhint.delivered_data = 0; 498e5313869SRichard Scheffenegger tp->sackhint.prr_out = 0; 499dbc42409SLawrence Stewart } 5000312fbe9SPoul-Henning Kamp 501df8bae1dSRodney W. Grimes /* 502262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 50385536381SHiren Panchasara * following conditions are met: 50485536381SHiren Panchasara * - There is no delayed ack timer in progress. 50585536381SHiren Panchasara * - Our last ack wasn't a 0-sized window. We never want to delay 50685536381SHiren Panchasara * the ack that opens up a 0-sized window. 50785536381SHiren Panchasara * - LRO wasn't used for this segment. We make sure by checking that the 50885536381SHiren Panchasara * segment size is not larger than the MSS. 509d8c85a26SJonathan Lemon */ 510c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen) \ 511b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 512a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 5130c39d38dSGleb Smirnoff (tlen <= tp->t_maxseg) && \ 514603724d3SBjoern A. Zeeb (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 515d8c85a26SJonathan Lemon 5164ad24737SRandall Stewart void inline 5175d8fd932SRandall Stewart cc_ecnpkt_handler_flags(struct tcpcb *tp, uint16_t flags, uint8_t iptos) 51864807b30SHiren Panchasara { 51964807b30SHiren Panchasara INP_WLOCK_ASSERT(tp->t_inpcb); 52064807b30SHiren Panchasara 52164807b30SHiren Panchasara if (CC_ALGO(tp)->ecnpkt_handler != NULL) { 52264807b30SHiren Panchasara switch (iptos & IPTOS_ECN_MASK) { 52364807b30SHiren Panchasara case IPTOS_ECN_CE: 52464807b30SHiren Panchasara tp->ccv->flags |= CCF_IPHDR_CE; 52564807b30SHiren Panchasara break; 52664807b30SHiren Panchasara case IPTOS_ECN_ECT0: 52783a2839fSMichael Tuexen /* FALLTHROUGH */ 52864807b30SHiren Panchasara case IPTOS_ECN_ECT1: 52983a2839fSMichael Tuexen /* FALLTHROUGH */ 53083a2839fSMichael Tuexen case IPTOS_ECN_NOTECT: 53164807b30SHiren Panchasara tp->ccv->flags &= ~CCF_IPHDR_CE; 53264807b30SHiren Panchasara break; 53364807b30SHiren Panchasara } 53464807b30SHiren Panchasara 5355d8fd932SRandall Stewart if (flags & TH_CWR) 53664807b30SHiren Panchasara tp->ccv->flags |= CCF_TCPHDR_CWR; 53764807b30SHiren Panchasara else 53864807b30SHiren Panchasara tp->ccv->flags &= ~CCF_TCPHDR_CWR; 53964807b30SHiren Panchasara 54064807b30SHiren Panchasara CC_ALGO(tp)->ecnpkt_handler(tp->ccv); 54164807b30SHiren Panchasara 542e11c9783SMichael Tuexen if (tp->ccv->flags & CCF_ACKNOW) { 54364807b30SHiren Panchasara tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 544e11c9783SMichael Tuexen tp->t_flags |= TF_ACKNOW; 545e11c9783SMichael Tuexen } 54664807b30SHiren Panchasara } 54764807b30SHiren Panchasara } 54864807b30SHiren Panchasara 5495d8fd932SRandall Stewart void inline 5505d8fd932SRandall Stewart cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos) 5515d8fd932SRandall Stewart { 5521ebf4607SRichard Scheffenegger cc_ecnpkt_handler_flags(tp, tcp_get_flags(th), iptos); 5535d8fd932SRandall Stewart } 5545d8fd932SRandall Stewart 555df8bae1dSRodney W. Grimes /* 5568d573cc1SAndre Oppermann * TCP input handling is split into multiple parts: 5578d573cc1SAndre Oppermann * tcp6_input is a thin wrapper around tcp_input for the extended 5588d573cc1SAndre Oppermann * ip6_protox[] call format in ip6_input 5598d573cc1SAndre Oppermann * tcp_input handles primary segment validation, inpcb lookup and 5608d573cc1SAndre Oppermann * SYN processing on listen sockets 5618d573cc1SAndre Oppermann * tcp_do_segment processes the ACK and text of the segment for 5628d573cc1SAndre Oppermann * establishing, established and closing connections 563df8bae1dSRodney W. Grimes */ 564fb59c426SYoshinobu Inoue #ifdef INET6 565fb59c426SYoshinobu Inoue int 5669e644c23SMichael Tuexen tcp6_input_with_port(struct mbuf **mp, int *offp, int proto, uint16_t port) 567fb59c426SYoshinobu Inoue { 568503f4e47SBjoern A. Zeeb struct mbuf *m; 56933841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 5703e88eb90SAndrey V. Elsukov struct ip6_hdr *ip6; 571fb59c426SYoshinobu Inoue 572503f4e47SBjoern A. Zeeb m = *mp; 573a4adf6ccSBjoern A. Zeeb if (m->m_len < *offp + sizeof(struct tcphdr)) { 5744e619b17SBjoern A. Zeeb m = m_pullup(m, *offp + sizeof(struct tcphdr)); 5754e619b17SBjoern A. Zeeb if (m == NULL) { 5764e619b17SBjoern A. Zeeb *mp = m; 5774e619b17SBjoern A. Zeeb TCPSTAT_INC(tcps_rcvshort); 5784e619b17SBjoern A. Zeeb return (IPPROTO_DONE); 5794e619b17SBjoern A. Zeeb } 580a4adf6ccSBjoern A. Zeeb } 581fb59c426SYoshinobu Inoue 582fb59c426SYoshinobu Inoue /* 583fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 584fb59c426SYoshinobu Inoue * better place to put this in? 585fb59c426SYoshinobu Inoue */ 5863e88eb90SAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 5878268d82cSAlexander V. Chernikov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false); 58833841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 589fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 590fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 591a8fe77d8SBjoern A. Zeeb *mp = NULL; 5928f5a8818SKevin Lo return (IPPROTO_DONE); 593fb59c426SYoshinobu Inoue } 594fb59c426SYoshinobu Inoue 595a8fe77d8SBjoern A. Zeeb *mp = m; 5969e644c23SMichael Tuexen return (tcp_input_with_port(mp, offp, proto, port)); 5979e644c23SMichael Tuexen } 5989e644c23SMichael Tuexen 5999e644c23SMichael Tuexen int 6009e644c23SMichael Tuexen tcp6_input(struct mbuf **mp, int *offp, int proto) 6019e644c23SMichael Tuexen { 6029e644c23SMichael Tuexen 6039e644c23SMichael Tuexen return(tcp6_input_with_port(mp, offp, proto, 0)); 604fb59c426SYoshinobu Inoue } 605b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 606fb59c426SYoshinobu Inoue 6078f5a8818SKevin Lo int 6089e644c23SMichael Tuexen tcp_input_with_port(struct mbuf **mp, int *offp, int proto, uint16_t port) 609df8bae1dSRodney W. Grimes { 6108f5a8818SKevin Lo struct mbuf *m = *mp; 611b287c6c7SBjoern A. Zeeb struct tcphdr *th = NULL; 612ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 613ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 614302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 615302ce8d6SAndre Oppermann struct socket *so = NULL; 616e79adb8eSGarrett Wollman u_char *optp = NULL; 6178f5a8818SKevin Lo int off0; 61826f9a767SRodney W. Grimes int optlen = 0; 619b287c6c7SBjoern A. Zeeb #ifdef INET 620b287c6c7SBjoern A. Zeeb int len; 621c4556b2fSAndrey V. Elsukov uint8_t ipttl; 622b287c6c7SBjoern A. Zeeb #endif 623b287c6c7SBjoern A. Zeeb int tlen = 0, off; 624fb59c426SYoshinobu Inoue int drop_hdrlen; 625ad3f9ab3SAndre Oppermann int thflags; 626302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 62708d9c920SGleb Smirnoff int lookupflag; 6281d7ee746SKurt Lidl uint8_t iptos; 629c1de64a4SAndrey V. Elsukov struct m_tag *fwd_tag = NULL; 630c068736aSJeffrey Hsu #ifdef INET6 631302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 632c068736aSJeffrey Hsu int isipv6; 633c068736aSJeffrey Hsu #else 634a160e630SAndre Oppermann const void *ip6 = NULL; 635b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 636302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 637a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 638610ee2f9SDavid Greenman #ifdef TCPDEBUG 639c068736aSJeffrey Hsu /* 640c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 641c068736aSJeffrey Hsu * now IPv6. 642c068736aSJeffrey Hsu */ 64314739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 644410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 645610ee2f9SDavid Greenman short ostate = 0; 646610ee2f9SDavid Greenman #endif 647c068736aSJeffrey Hsu 648d40c0d47SGleb Smirnoff NET_EPOCH_ASSERT(); 649d40c0d47SGleb Smirnoff 650fb59c426SYoshinobu Inoue #ifdef INET6 651fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 652fb59c426SYoshinobu Inoue #endif 653a0292f23SGarrett Wollman 6548f5a8818SKevin Lo off0 = *offp; 6558f5a8818SKevin Lo m = *mp; 6568f5a8818SKevin Lo *mp = NULL; 657302ce8d6SAndre Oppermann to.to_flags = 0; 65878b50714SRobert Watson TCPSTAT_INC(tcps_rcvtotal); 659fb59c426SYoshinobu Inoue 66004d3a452SHajimu UMEMOTO #ifdef INET6 661b287c6c7SBjoern A. Zeeb if (isipv6) { 662fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 66345747ba5SBjoern A. Zeeb th = (struct tcphdr *)((caddr_t)ip6 + off0); 664fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 6659e644c23SMichael Tuexen if (port) 6669e644c23SMichael Tuexen goto skip6_csum; 667356ab07eSBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) { 66845747ba5SBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 66945747ba5SBjoern A. Zeeb th->th_sum = m->m_pkthdr.csum_data; 67045747ba5SBjoern A. Zeeb else 67145747ba5SBjoern A. Zeeb th->th_sum = in6_cksum_pseudo(ip6, tlen, 67245747ba5SBjoern A. Zeeb IPPROTO_TCP, m->m_pkthdr.csum_data); 67345747ba5SBjoern A. Zeeb th->th_sum ^= 0xffff; 67445747ba5SBjoern A. Zeeb } else 67545747ba5SBjoern A. Zeeb th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen); 67645747ba5SBjoern A. Zeeb if (th->th_sum) { 67778b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 678fb59c426SYoshinobu Inoue goto drop; 679fb59c426SYoshinobu Inoue } 6809e644c23SMichael Tuexen skip6_csum: 68133841545SHajimu UMEMOTO /* 68233841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 68333841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 68433841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 68533841545SHajimu UMEMOTO * 68633841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 68733841545SHajimu UMEMOTO * already dropped in ip6_input. 68833841545SHajimu UMEMOTO */ 68933841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 69033841545SHajimu UMEMOTO /* XXX stat */ 69133841545SHajimu UMEMOTO goto drop; 69233841545SHajimu UMEMOTO } 693bb4a7d94SKristof Provost iptos = IPV6_TRAFFIC_CLASS(ip6); 694b287c6c7SBjoern A. Zeeb } 69504d3a452SHajimu UMEMOTO #endif 696b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 697b287c6c7SBjoern A. Zeeb else 698b287c6c7SBjoern A. Zeeb #endif 699b287c6c7SBjoern A. Zeeb #ifdef INET 700b287c6c7SBjoern A. Zeeb { 701df8bae1dSRodney W. Grimes /* 702df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 703df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 704df8bae1dSRodney W. Grimes */ 705fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 706105bd211SGleb Smirnoff ip_stripoptions(m); 707fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 708fb59c426SYoshinobu Inoue } 709df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 7104dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 7114dfdffe9SAndre Oppermann == NULL) { 71278b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 7138f5a8818SKevin Lo return (IPPROTO_DONE); 714df8bae1dSRodney W. Grimes } 715df8bae1dSRodney W. Grimes } 716fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 717db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 7188ad458a4SGleb Smirnoff tlen = ntohs(ip->ip_len) - off0; 719df8bae1dSRodney W. Grimes 7201d7ee746SKurt Lidl iptos = ip->ip_tos; 7219e644c23SMichael Tuexen if (port) 7229e644c23SMichael Tuexen goto skip_csum; 723db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 724db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 725db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 726db4f9cc7SJonathan Lemon else 727db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 728c068736aSJeffrey Hsu ip->ip_dst.s_addr, 7298f134647SGleb Smirnoff htonl(m->m_pkthdr.csum_data + tlen + 730c068736aSJeffrey Hsu IPPROTO_TCP)); 731db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 732db4f9cc7SJonathan Lemon } else { 7338f134647SGleb Smirnoff struct ipovly *ipov = (struct ipovly *)ip; 7348f134647SGleb Smirnoff 735df8bae1dSRodney W. Grimes /* 736df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 737df8bae1dSRodney W. Grimes */ 7388f134647SGleb Smirnoff len = off0 + tlen; 739c4556b2fSAndrey V. Elsukov ipttl = ip->ip_ttl; 740fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 7418f134647SGleb Smirnoff ipov->ih_len = htons(tlen); 742fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 74357f60867SMark Johnston /* Reset length for SDT probes. */ 7441d7ee746SKurt Lidl ip->ip_len = htons(len); 7451d7ee746SKurt Lidl /* Reset TOS bits */ 7461d7ee746SKurt Lidl ip->ip_tos = iptos; 7471d7ee746SKurt Lidl /* Re-initialization for later version check */ 748c4556b2fSAndrey V. Elsukov ip->ip_ttl = ipttl; 7491d7ee746SKurt Lidl ip->ip_v = IPVERSION; 750b2fe54bcSAndrey V. Elsukov ip->ip_hl = off0 >> 2; 751db4f9cc7SJonathan Lemon } 7529e644c23SMichael Tuexen skip_csum: 7539e644c23SMichael Tuexen if (th->th_sum && (port == 0)) { 75478b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 755df8bae1dSRodney W. Grimes goto drop; 756df8bae1dSRodney W. Grimes } 757fb59c426SYoshinobu Inoue } 758b287c6c7SBjoern A. Zeeb #endif /* INET */ 759df8bae1dSRodney W. Grimes 760df8bae1dSRodney W. Grimes /* 761df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 762df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 763df8bae1dSRodney W. Grimes */ 764fb59c426SYoshinobu Inoue off = th->th_off << 2; 765df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 76678b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadoff); 767df8bae1dSRodney W. Grimes goto drop; 768df8bae1dSRodney W. Grimes } 769fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 770df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 77104d3a452SHajimu UMEMOTO #ifdef INET6 772b287c6c7SBjoern A. Zeeb if (isipv6) { 773a4adf6ccSBjoern A. Zeeb if (m->m_len < off0 + off) { 7744e619b17SBjoern A. Zeeb m = m_pullup(m, off0 + off); 7754e619b17SBjoern A. Zeeb if (m == NULL) { 7764e619b17SBjoern A. Zeeb TCPSTAT_INC(tcps_rcvshort); 7774e619b17SBjoern A. Zeeb return (IPPROTO_DONE); 7784e619b17SBjoern A. Zeeb } 779a4adf6ccSBjoern A. Zeeb } 780fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 781fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 782b287c6c7SBjoern A. Zeeb } 78304d3a452SHajimu UMEMOTO #endif 784b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 785b287c6c7SBjoern A. Zeeb else 786b287c6c7SBjoern A. Zeeb #endif 787b287c6c7SBjoern A. Zeeb #ifdef INET 788b287c6c7SBjoern A. Zeeb { 789df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 790c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 7914dfdffe9SAndre Oppermann == NULL) { 79278b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 7938f5a8818SKevin Lo return (IPPROTO_DONE); 794df8bae1dSRodney W. Grimes } 795fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 796fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 797fb59c426SYoshinobu Inoue } 798df8bae1dSRodney W. Grimes } 799b287c6c7SBjoern A. Zeeb #endif 800df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 801fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 802df8bae1dSRodney W. Grimes } 8031ebf4607SRichard Scheffenegger thflags = tcp_get_flags(th); 804df8bae1dSRodney W. Grimes 805e46cd3d4SDag-Erling Smørgrav /* 806df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 807df8bae1dSRodney W. Grimes */ 8082903309aSAttilio Rao tcp_fields_to_host(th); 809df8bae1dSRodney W. Grimes 810df8bae1dSRodney W. Grimes /* 811794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 812755c1f07SAndras Olah */ 813fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 814755c1f07SAndras Olah 815755c1f07SAndras Olah /* 8168d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 8178d573cc1SAndre Oppermann */ 818b8056faeSGleb Smirnoff if ( 819b8056faeSGleb Smirnoff #ifdef INET6 820b8056faeSGleb Smirnoff (isipv6 && (m->m_flags & M_IP6_NEXTHOP)) 821b8056faeSGleb Smirnoff #ifdef INET 822b8056faeSGleb Smirnoff || (!isipv6 && (m->m_flags & M_IP_NEXTHOP)) 823b8056faeSGleb Smirnoff #endif 824b8056faeSGleb Smirnoff #endif 825b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6) 826b8056faeSGleb Smirnoff (m->m_flags & M_IP_NEXTHOP) 827b8056faeSGleb Smirnoff #endif 828b8056faeSGleb Smirnoff ) 8299b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 8309b932e9eSAndre Oppermann 83108d9c920SGleb Smirnoff /* 8321db08fbeSGleb Smirnoff * For initial SYN packets we don't need write lock on matching 8331db08fbeSGleb Smirnoff * PCB, be it a listening one or a synchronized one. The packet 8341db08fbeSGleb Smirnoff * shall not modify its state. 83508d9c920SGleb Smirnoff */ 83608d9c920SGleb Smirnoff lookupflag = (thflags & (TH_ACK|TH_SYN)) == TH_SYN ? 8371db08fbeSGleb Smirnoff INPLOOKUP_RLOCKPCB : INPLOOKUP_WLOCKPCB; 838ce7ad664SEd Maste findpcb: 8398a006adbSBjoern A. Zeeb #ifdef INET6 8408a006adbSBjoern A. Zeeb if (isipv6 && fwd_tag != NULL) { 8418a006adbSBjoern A. Zeeb struct sockaddr_in6 *next_hop6; 8428a006adbSBjoern A. Zeeb 8438a006adbSBjoern A. Zeeb next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1); 8448a006adbSBjoern A. Zeeb /* 8458a006adbSBjoern A. Zeeb * Transparently forwarded. Pretend to be the destination. 8468a006adbSBjoern A. Zeeb * Already got one like this? 8478a006adbSBjoern A. Zeeb */ 8488a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, 8498a006adbSBjoern A. Zeeb &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport, 85008d9c920SGleb Smirnoff lookupflag, m->m_pkthdr.rcvif, m); 8518a006adbSBjoern A. Zeeb if (!inp) { 8528a006adbSBjoern A. Zeeb /* 8538a006adbSBjoern A. Zeeb * It's new. Try to find the ambushing socket. 8548a006adbSBjoern A. Zeeb * Because we've rewritten the destination address, 8558a006adbSBjoern A. Zeeb * any hardware-generated hash is ignored. 8568a006adbSBjoern A. Zeeb */ 8578a006adbSBjoern A. Zeeb inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src, 8588a006adbSBjoern A. Zeeb th->th_sport, &next_hop6->sin6_addr, 8598a006adbSBjoern A. Zeeb next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) : 86008d9c920SGleb Smirnoff th->th_dport, INPLOOKUP_WILDCARD | lookupflag, 86108d9c920SGleb Smirnoff m->m_pkthdr.rcvif); 8628a006adbSBjoern A. Zeeb } 863c1de64a4SAndrey V. Elsukov } else if (isipv6) { 8648a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src, 8658a006adbSBjoern A. Zeeb th->th_sport, &ip6->ip6_dst, th->th_dport, 86608d9c920SGleb Smirnoff INPLOOKUP_WILDCARD | lookupflag, m->m_pkthdr.rcvif, m); 8678a006adbSBjoern A. Zeeb } 8688a006adbSBjoern A. Zeeb #endif /* INET6 */ 8698a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET) 8708a006adbSBjoern A. Zeeb else 8718a006adbSBjoern A. Zeeb #endif 8728a006adbSBjoern A. Zeeb #ifdef INET 8738a006adbSBjoern A. Zeeb if (fwd_tag != NULL) { 8749b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 8759b932e9eSAndre Oppermann 8769b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 877f9e354dfSJulian Elischer /* 8782b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 879f9e354dfSJulian Elischer * already got one like this? 880f9e354dfSJulian Elischer */ 881d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport, 88208d9c920SGleb Smirnoff ip->ip_dst, th->th_dport, lookupflag, m->m_pkthdr.rcvif, m); 883fa046d87SRobert Watson if (!inp) { 884fa046d87SRobert Watson /* 885fa046d87SRobert Watson * It's new. Try to find the ambushing socket. 886d3c1f003SRobert Watson * Because we've rewritten the destination address, 887d3c1f003SRobert Watson * any hardware-generated hash is ignored. 888fa046d87SRobert Watson */ 889fa046d87SRobert Watson inp = in_pcblookup(&V_tcbinfo, ip->ip_src, 890fa046d87SRobert Watson th->th_sport, next_hop->sin_addr, 891fa046d87SRobert Watson next_hop->sin_port ? ntohs(next_hop->sin_port) : 89208d9c920SGleb Smirnoff th->th_dport, INPLOOKUP_WILDCARD | lookupflag, 89308d9c920SGleb Smirnoff m->m_pkthdr.rcvif); 894f9e354dfSJulian Elischer } 895b10fbdeaSAndre Oppermann } else 896d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, 897fa046d87SRobert Watson th->th_sport, ip->ip_dst, th->th_dport, 89808d9c920SGleb Smirnoff INPLOOKUP_WILDCARD | lookupflag, m->m_pkthdr.rcvif, m); 8998a006adbSBjoern A. Zeeb #endif /* INET */ 900f9e354dfSJulian Elischer 901df8bae1dSRodney W. Grimes /* 902574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 903574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 9048b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 905df8bae1dSRodney W. Grimes */ 906816a3d83SPoul-Henning Kamp if (inp == NULL) { 907574b6964SAndre Oppermann /* 908574b6964SAndre Oppermann * Log communication attempts to ports that are not 909574b6964SAndre Oppermann * in use. 910574b6964SAndre Oppermann */ 911334fc582SBjoern A. Zeeb if ((V_tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 912334fc582SBjoern A. Zeeb V_tcp_log_in_vain == 2) { 913b7d747ecSAndre Oppermann if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6))) 914df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 915df541e5fSAndre Oppermann "to closed port\n", s, __func__); 9162e4e1b4cSGeoff Rehmet } 917574b6964SAndre Oppermann /* 918574b6964SAndre Oppermann * When blackholing do not respond with a RST but 919574b6964SAndre Oppermann * completely ignore the segment and drop it. 920574b6964SAndre Oppermann */ 9213ea9a7cfSGleb Smirnoff if (((V_blackhole == 1 && (thflags & TH_SYN)) || 9223ea9a7cfSGleb Smirnoff V_blackhole == 2) && (V_blackhole_local || 9233ea9a7cfSGleb Smirnoff #ifdef INET6 9243ea9a7cfSGleb Smirnoff isipv6 ? !in6_localaddr(&ip6->ip6_src) : 9253ea9a7cfSGleb Smirnoff #endif 9263ea9a7cfSGleb Smirnoff #ifdef INET 9273ea9a7cfSGleb Smirnoff !in_localip(ip->ip_src) 9283ea9a7cfSGleb Smirnoff #else 9293ea9a7cfSGleb Smirnoff true 9303ea9a7cfSGleb Smirnoff #endif 9313ea9a7cfSGleb Smirnoff )) 9321929eae1SAndre Oppermann goto dropunlock; 933574b6964SAndre Oppermann 934a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 935a57815efSBosko Milekic goto dropwithreset; 936816a3d83SPoul-Henning Kamp } 93708d9c920SGleb Smirnoff INP_LOCK_ASSERT(inp); 938f5cf1e5fSJulien Charbon /* 939f5cf1e5fSJulien Charbon * While waiting for inp lock during the lookup, another thread 940f5cf1e5fSJulien Charbon * can have dropped the inpcb, in which case we need to loop back 941f5cf1e5fSJulien Charbon * and try to find a new inpcb to deliver to. 942f5cf1e5fSJulien Charbon */ 943f5cf1e5fSJulien Charbon if (inp->inp_flags & INP_DROPPED) { 94408d9c920SGleb Smirnoff INP_UNLOCK(inp); 945f5cf1e5fSJulien Charbon inp = NULL; 946f5cf1e5fSJulien Charbon goto findpcb; 947f5cf1e5fSJulien Charbon } 948c2529042SHans Petter Selasky if ((inp->inp_flowtype == M_HASHTYPE_NONE) && 949c2529042SHans Petter Selasky (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) && 950f4bb1869SMark Johnston ((inp->inp_socket == NULL) || !SOLISTENING(inp->inp_socket))) { 95180cb9f21SKip Macy inp->inp_flowid = m->m_pkthdr.flowid; 9522f719932SAdrian Chadd inp->inp_flowtype = M_HASHTYPE_GET(m); 95380cb9f21SKip Macy } 954fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 955a2589465SKen Smith #ifdef INET6 956fcf59617SAndrey V. Elsukov if (isipv6 && IPSEC_ENABLED(ipv6) && 957fcf59617SAndrey V. Elsukov IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) { 958a2589465SKen Smith goto dropunlock; 959a2589465SKen Smith } 960fcf59617SAndrey V. Elsukov #ifdef INET 961fcf59617SAndrey V. Elsukov else 962fcf59617SAndrey V. Elsukov #endif 963fcf59617SAndrey V. Elsukov #endif /* INET6 */ 964fcf59617SAndrey V. Elsukov #ifdef INET 965fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4) && 966fcf59617SAndrey V. Elsukov IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) { 967fcf59617SAndrey V. Elsukov goto dropunlock; 968fcf59617SAndrey V. Elsukov } 969fcf59617SAndrey V. Elsukov #endif /* INET */ 970a2589465SKen Smith #endif /* IPSEC */ 971a2589465SKen Smith 9728d573cc1SAndre Oppermann /* 9738d573cc1SAndre Oppermann * Check the minimum TTL for socket. 9748d573cc1SAndre Oppermann */ 97534f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 97634f83c52SGeorge V. Neville-Neil #ifdef INET6 9772d8868dbSJonathan T. Looney if (isipv6) { 9782d8868dbSJonathan T. Looney if (inp->inp_ip_minttl > ip6->ip6_hlim) 979302ce8d6SAndre Oppermann goto dropunlock; 9802d8868dbSJonathan T. Looney } else 98134f83c52SGeorge V. Neville-Neil #endif 98234f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 983302ce8d6SAndre Oppermann goto dropunlock; 98434f83c52SGeorge V. Neville-Neil } 985936cd18dSAndre Oppermann 986340c35deSJonathan Lemon /* 987252ca428SRobert Watson * A previous connection in TIMEWAIT state is supposed to catch stray 988252ca428SRobert Watson * or duplicate segments arriving late. If this segment was a 9890989f56cSRobert Watson * legitimate new connection attempt, the old INPCB gets removed and 990252ca428SRobert Watson * we can try again to find a listening socket. 991340c35deSJonathan Lemon */ 992ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 993283c76c7SMichael Tuexen tcp_dooptions(&to, optp, optlen, 994283c76c7SMichael Tuexen (thflags & TH_SYN) ? TO_SYN : 0); 9958d573cc1SAndre Oppermann /* 9968d573cc1SAndre Oppermann * NB: tcp_twcheck unlocks the INP and frees the mbuf. 9978d573cc1SAndre Oppermann */ 9982104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 999340c35deSJonathan Lemon goto findpcb; 10008f5a8818SKevin Lo return (IPPROTO_DONE); 1001340c35deSJonathan Lemon } 1002794235b7SAndre Oppermann /* 1003794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 1004794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 1005794235b7SAndre Oppermann * segment and send an appropriate response. 1006794235b7SAndre Oppermann */ 1007df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 1008a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 1009a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 1010a57815efSBosko Milekic goto dropwithreset; 101109f81a46SBosko Milekic } 1012df8bae1dSRodney W. Grimes 10139e644c23SMichael Tuexen if ((tp->t_port != port) && (tp->t_state > TCPS_LISTEN)) { 10149e644c23SMichael Tuexen rstreason = BANDLIM_RST_CLOSEDPORT; 10159e644c23SMichael Tuexen goto dropwithreset; 10169e644c23SMichael Tuexen } 10179e644c23SMichael Tuexen 101809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 101909fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 102009fe6320SNavdeep Parhar tcp_offload_input(tp, m); 102109fe6320SNavdeep Parhar m = NULL; /* consumed by the TOE driver */ 102209fe6320SNavdeep Parhar goto dropunlock; 102309fe6320SNavdeep Parhar } 102409fe6320SNavdeep Parhar #endif 102509fe6320SNavdeep Parhar 1026c488362eSRobert Watson #ifdef MAC 102730d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 1028302ce8d6SAndre Oppermann goto dropunlock; 1029c488362eSRobert Watson #endif 1030a557af22SRobert Watson so = inp->inp_socket; 1031302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 1032610ee2f9SDavid Greenman #ifdef TCPDEBUG 1033df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 1034df8bae1dSRodney W. Grimes ostate = tp->t_state; 103510fe523eSMaxim Konovalov #ifdef INET6 10366f697424SBjoern A. Zeeb if (isipv6) { 1037540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 1038d30d90dcSMaxim Konovalov } else 10396f697424SBjoern A. Zeeb #endif 1040540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 1041fb59c426SYoshinobu Inoue tcp_savetcp = *th; 1042df8bae1dSRodney W. Grimes } 1043b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */ 1044db33b3e6SAndre Oppermann /* 1045db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 1046db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 1047a7c7f2a7SJohn Baldwin * attempt or the completion of a previous one. 1048db33b3e6SAndre Oppermann */ 1049f4bb1869SMark Johnston KASSERT(tp->t_state == TCPS_LISTEN || !SOLISTENING(so), 105045274760SJonathan T. Looney ("%s: so accepting but tp %p not listening", __func__, tp)); 1051f4bb1869SMark Johnston if (tp->t_state == TCPS_LISTEN && SOLISTENING(so)) { 1052540e8b7eSJeffrey Hsu struct in_conninfo inc; 1053540e8b7eSJeffrey Hsu 10548bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 1055302ce8d6SAndre Oppermann #ifdef INET6 1056be2ac88cSJonathan Lemon if (isipv6) { 1057dcdb4371SBjoern A. Zeeb inc.inc_flags |= INC_ISIPV6; 10585dff1c38SMichael Tuexen if (inp->inp_inc.inc_flags & INC_IPV6MINMTU) 10595dff1c38SMichael Tuexen inc.inc_flags |= INC_IPV6MINMTU; 1060be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 1061be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 1062302ce8d6SAndre Oppermann } else 1063302ce8d6SAndre Oppermann #endif 1064302ce8d6SAndre Oppermann { 1065be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 1066be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 1067be2ac88cSJonathan Lemon } 1068be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 1069be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 10707973fba3SJulian Elischer inc.inc_fibnum = so->so_fibnum; 1071be2ac88cSJonathan Lemon 1072fb59c426SYoshinobu Inoue /* 10738d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 10748d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 10758d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 1076fb59c426SYoshinobu Inoue */ 1077be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 1078bf6d304aSAndre Oppermann /* 1079bf6d304aSAndre Oppermann * Parse the TCP options here because 1080bf6d304aSAndre Oppermann * syncookies need access to the reflected 1081bf6d304aSAndre Oppermann * timestamp. 1082bf6d304aSAndre Oppermann */ 1083bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 10849fa198beSAndre Oppermann /* 10859fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 10869fa198beSAndre Oppermann * inp and tcpinfo locks. 10879fa198beSAndre Oppermann */ 10889e644c23SMichael Tuexen rstreason = syncache_expand(&inc, &to, th, &so, m, port); 1089fcf59617SAndrey V. Elsukov if (rstreason < 0) { 1090fcf59617SAndrey V. Elsukov /* 1091fcf59617SAndrey V. Elsukov * A failing TCP MD5 signature comparison 1092fcf59617SAndrey V. Elsukov * must result in the segment being dropped 1093fcf59617SAndrey V. Elsukov * and must not produce any response back 1094fcf59617SAndrey V. Elsukov * to the sender. 1095fcf59617SAndrey V. Elsukov */ 1096fcf59617SAndrey V. Elsukov goto dropunlock; 1097fcf59617SAndrey V. Elsukov } else if (rstreason == 0) { 10984195b4afSPaul Traina /* 1099e207f800SAndre Oppermann * No syncache entry or ACK was not 1100be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 11018d573cc1SAndre Oppermann * NB: syncache did its own logging 11028d573cc1SAndre Oppermann * of the failure cause. 11034195b4afSPaul Traina */ 1104be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 1105be2ac88cSJonathan Lemon goto dropwithreset; 1106be2ac88cSJonathan Lemon } 110709c305ebSPatrick Kelsey tfo_socket_result: 1108f76fcf6dSJeffrey Hsu if (so == NULL) { 1109be2ac88cSJonathan Lemon /* 1110e207f800SAndre Oppermann * We completed the 3-way handshake 1111e207f800SAndre Oppermann * but could not allocate a socket 1112e207f800SAndre Oppermann * either due to memory shortage, 1113e207f800SAndre Oppermann * listen queue length limits or 1114a160e630SAndre Oppermann * global socket limits. Send RST 1115a160e630SAndre Oppermann * or wait and have the remote end 1116a160e630SAndre Oppermann * retransmit the ACK for another 1117a160e630SAndre Oppermann * try. 1118be2ac88cSJonathan Lemon */ 1119a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1120a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11218d573cc1SAndre Oppermann "Socket allocation failed due to " 11228d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 1123ac957cd2SJulian Elischer s, __func__, 1124ac957cd2SJulian Elischer V_tcp_sc_rst_sock_fail ? 1125ac957cd2SJulian Elischer "sending RST" : "try again"); 1126603724d3SBjoern A. Zeeb if (V_tcp_sc_rst_sock_fail) { 1127e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 1128e207f800SAndre Oppermann goto dropwithreset; 1129a160e630SAndre Oppermann } else 1130a160e630SAndre Oppermann goto dropunlock; 1131f76fcf6dSJeffrey Hsu } 1132be2ac88cSJonathan Lemon /* 1133be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 11348d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 11358d573cc1SAndre Oppermann * created socket and update the tp variable. 113608d9c920SGleb Smirnoff * If we came here via jump to tfo_socket_result, 113708d9c920SGleb Smirnoff * then listening socket is read-locked. 1138be2ac88cSJonathan Lemon */ 113908d9c920SGleb Smirnoff INP_UNLOCK(inp); /* listen socket */ 1140be2ac88cSJonathan Lemon inp = sotoinpcb(so); 1141ff9b006dSJulien Charbon /* 1142ff9b006dSJulien Charbon * New connection inpcb is already locked by 1143ff9b006dSJulien Charbon * syncache_expand(). 1144ff9b006dSJulien Charbon */ 1145ff9b006dSJulien Charbon INP_WLOCK_ASSERT(inp); 1146be2ac88cSJonathan Lemon tp = intotcpcb(inp); 11478d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 11488d573cc1SAndre Oppermann ("%s: ", __func__)); 1149be2ac88cSJonathan Lemon /* 1150302ce8d6SAndre Oppermann * Process the segment and the data it 1151302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 1152302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 1153302ce8d6SAndre Oppermann */ 11546138da62SMichael Tuexen TCP_PROBE5(receive, NULL, tp, m, tp, th); 115555bceb1eSRandall Stewart tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 11566573d758SMatt Macy iptos); 11578f5a8818SKevin Lo return (IPPROTO_DONE); 1158be2ac88cSJonathan Lemon } 1159a160e630SAndre Oppermann /* 11608d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 11618d573cc1SAndre Oppermann * 1162a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 1163a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 1164a160e630SAndre Oppermann * retransmits. 116519bc77c5SAndre Oppermann * 116619bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 116719bc77c5SAndre Oppermann * causes. 1168a160e630SAndre Oppermann */ 1169a160e630SAndre Oppermann if (thflags & TH_RST) { 11709e644c23SMichael Tuexen syncache_chkrst(&inc, th, m, port); 1171a160e630SAndre Oppermann goto dropunlock; 1172a160e630SAndre Oppermann } 1173a160e630SAndre Oppermann /* 1174a160e630SAndre Oppermann * We can't do anything without SYN. 1175a160e630SAndre Oppermann */ 1176a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 1177a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1178a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1179773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 11808d573cc1SAndre Oppermann s, __func__); 118178b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1182a160e630SAndre Oppermann goto dropunlock; 1183a160e630SAndre Oppermann } 1184a160e630SAndre Oppermann /* 1185a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 1186a160e630SAndre Oppermann */ 1187fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1188a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1189a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11908d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 11918d573cc1SAndre Oppermann s, __func__); 11929e644c23SMichael Tuexen syncache_badack(&inc, port); /* XXX: Not needed! */ 119378b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1194a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1195a57815efSBosko Milekic goto dropwithreset; 11964195b4afSPaul Traina } 1197a160e630SAndre Oppermann /* 1198a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 1199a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 1200a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 1201a160e630SAndre Oppermann * TCP/IP stack. 1202a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 1203a160e630SAndre Oppermann * and is constantly refining its stack detection 1204a160e630SAndre Oppermann * strategies. 12058d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 1206a160e630SAndre Oppermann * and was used by RFC1644. 1207a160e630SAndre Oppermann */ 1208603724d3SBjoern A. Zeeb if ((thflags & TH_FIN) && V_drop_synfin) { 1209a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1210a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1211773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 12128d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 121378b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1214302ce8d6SAndre Oppermann goto dropunlock; 1215ebb0cbeaSPaul Traina } 1216be2ac88cSJonathan Lemon /* 1217be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 1218a160e630SAndre Oppermann * 1219a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 1220a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 1221a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 1222be2ac88cSJonathan Lemon */ 1223a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 1224a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 1225a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 1226a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 122708d9c920SGleb Smirnoff INP_RLOCK_ASSERT(inp); 122833841545SHajimu UMEMOTO #ifdef INET6 122933841545SHajimu UMEMOTO /* 123033841545SHajimu UMEMOTO * If deprecated address is forbidden, 123133841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 123233841545SHajimu UMEMOTO * address to prevent any new inbound connection from 123333841545SHajimu UMEMOTO * getting established. 123433841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 123533841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 123633841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 123733841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 123833841545SHajimu UMEMOTO * for the exchange. 123933841545SHajimu UMEMOTO * 124033841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 124133841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 124233841545SHajimu UMEMOTO * SYN in this case. 124333841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 124433841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 124533841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 124633841545SHajimu UMEMOTO * used" 124733841545SHajimu UMEMOTO * 2. use of it with new communication: 124833841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 124933841545SHajimu UMEMOTO * with sufficient scope is available" 125033841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 125133841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 125233841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 125333841545SHajimu UMEMOTO * 125433841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 125533841545SHajimu UMEMOTO * multiple description text for deprecated address 125633841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 125733841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 125833841545SHajimu UMEMOTO */ 1259603724d3SBjoern A. Zeeb if (isipv6 && !V_ip6_use_deprecated) { 126033841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 126133841545SHajimu UMEMOTO 12628268d82cSAlexander V. Chernikov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false); 12638c0fec80SRobert Watson if (ia6 != NULL && 126433841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 1265a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1266a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12678d573cc1SAndre Oppermann "Connection attempt to deprecated " 12688d573cc1SAndre Oppermann "IPv6 address rejected\n", 1269a160e630SAndre Oppermann s, __func__); 127033841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 127133841545SHajimu UMEMOTO goto dropwithreset; 127233841545SHajimu UMEMOTO } 127333841545SHajimu UMEMOTO } 1274b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 127565f28919SJesper Skriver /* 1276db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 12778d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 1278db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 12798d573cc1SAndre Oppermann * If it is from this socket it must be forged. 12808d573cc1SAndre Oppermann * Don't respond if the source or destination is a 12818d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 128293ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 128393ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 128493ec91baSCrist J. Clark * in_broadcast() to find them. 1285be2ac88cSJonathan Lemon */ 12868d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 12878d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 12888d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12898d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 1290773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 1291302ce8d6SAndre Oppermann goto dropunlock; 12928d573cc1SAndre Oppermann } 1293db33b3e6SAndre Oppermann #ifdef INET6 1294b287c6c7SBjoern A. Zeeb if (isipv6) { 1295db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1296a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 1297a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1298a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12998d573cc1SAndre Oppermann "Connection attempt to/from self " 1300773673c1SAndre Oppermann "ignored\n", s, __func__); 1301302ce8d6SAndre Oppermann goto dropunlock; 1302a160e630SAndre Oppermann } 1303be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1304a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 1305a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1306a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13078d573cc1SAndre Oppermann "Connection attempt from/to multicast " 1308773673c1SAndre Oppermann "address ignored\n", s, __func__); 1309302ce8d6SAndre Oppermann goto dropunlock; 1310a160e630SAndre Oppermann } 1311b287c6c7SBjoern A. Zeeb } 1312db33b3e6SAndre Oppermann #endif 1313b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 1314b287c6c7SBjoern A. Zeeb else 1315b287c6c7SBjoern A. Zeeb #endif 1316b287c6c7SBjoern A. Zeeb #ifdef INET 1317b287c6c7SBjoern A. Zeeb { 1318db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1319a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 1320a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1321a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13228d573cc1SAndre Oppermann "Connection attempt from/to self " 1323773673c1SAndre Oppermann "ignored\n", s, __func__); 1324302ce8d6SAndre Oppermann goto dropunlock; 1325a160e630SAndre Oppermann } 1326be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1327be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 13282ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 1329a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 1330a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1331a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13328d573cc1SAndre Oppermann "Connection attempt from/to broad- " 1333773673c1SAndre Oppermann "or multicast address ignored\n", 1334a160e630SAndre Oppermann s, __func__); 1335302ce8d6SAndre Oppermann goto dropunlock; 1336c068736aSJeffrey Hsu } 1337a160e630SAndre Oppermann } 1338b287c6c7SBjoern A. Zeeb #endif 1339be2ac88cSJonathan Lemon /* 1340db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 1341db33b3e6SAndre Oppermann * for syncache. 1342be2ac88cSJonathan Lemon */ 13433c653157SHartmut Brandt #ifdef TCPDEBUG 13443c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 13453c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 13463c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 13473c653157SHartmut Brandt #endif 13482b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 1349f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 13508d5719aaSGleb Smirnoff if ((so = syncache_add(&inc, &to, th, inp, so, m, NULL, NULL, 13519e644c23SMichael Tuexen iptos, port)) != NULL) 135209c305ebSPatrick Kelsey goto tfo_socket_result; 135318a75309SPatrick Kelsey 1354be2ac88cSJonathan Lemon /* 13554d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 1356a7c7f2a7SJohn Baldwin * Only the listen socket is unlocked by syncache_add(). 1357be2ac88cSJonathan Lemon */ 13588f5a8818SKevin Lo return (IPPROTO_DONE); 1359982c1675SAndre Oppermann } else if (tp->t_state == TCPS_LISTEN) { 1360982c1675SAndre Oppermann /* 1361982c1675SAndre Oppermann * When a listen socket is torn down the SO_ACCEPTCONN 1362982c1675SAndre Oppermann * flag is removed first while connections are drained 1363982c1675SAndre Oppermann * from the accept queue in a unlock/lock cycle of the 1364982c1675SAndre Oppermann * ACCEPT_LOCK, opening a race condition allowing a SYN 1365982c1675SAndre Oppermann * attempt go through unhandled. 1366982c1675SAndre Oppermann */ 1367982c1675SAndre Oppermann goto dropunlock; 1368f76fcf6dSJeffrey Hsu } 1369fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1370fcf59617SAndrey V. Elsukov if (tp->t_flags & TF_SIGNATURE) { 1371fcf59617SAndrey V. Elsukov tcp_dooptions(&to, optp, optlen, thflags); 1372fcf59617SAndrey V. Elsukov if ((to.to_flags & TOF_SIGNATURE) == 0) { 1373fcf59617SAndrey V. Elsukov TCPSTAT_INC(tcps_sig_err_nosigopt); 13742903309aSAttilio Rao goto dropunlock; 13752903309aSAttilio Rao } 1376fcf59617SAndrey V. Elsukov if (!TCPMD5_ENABLED() || 1377fcf59617SAndrey V. Elsukov TCPMD5_INPUT(m, th, to.to_signature) != 0) 1378fcf59617SAndrey V. Elsukov goto dropunlock; 13792903309aSAttilio Rao } 13802903309aSAttilio Rao #endif 13812b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 138257f60867SMark Johnston 1383302ce8d6SAndre Oppermann /* 13848d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 13858d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 13868d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 13871db08fbeSGleb Smirnoff * 13881db08fbeSGleb Smirnoff * XXXGL: in case of a pure SYN arriving on existing connection 13891db08fbeSGleb Smirnoff * TCP stacks won't need to modify the PCB, they would either drop 13901db08fbeSGleb Smirnoff * the segment silently, or send a challenge ACK. However, we try 13911db08fbeSGleb Smirnoff * to upgrade the lock, because calling convention for stacks is 13921db08fbeSGleb Smirnoff * write-lock on PCB. If upgrade fails, drop the SYN. 1393302ce8d6SAndre Oppermann */ 13941db08fbeSGleb Smirnoff if (lookupflag == INPLOOKUP_RLOCKPCB && INP_TRY_UPGRADE(inp) == 0) 13951db08fbeSGleb Smirnoff goto dropunlock; 13961db08fbeSGleb Smirnoff 13976573d758SMatt Macy tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos); 13988f5a8818SKevin Lo return (IPPROTO_DONE); 1399be2ac88cSJonathan Lemon 1400302ce8d6SAndre Oppermann dropwithreset: 14012b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 140257f60867SMark Johnston 1403014ea782SRobert Watson if (inp != NULL) { 1404302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 140508d9c920SGleb Smirnoff INP_UNLOCK(inp); 1406dd8ac7f9SRobert Watson } else 1407014ea782SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1408302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 1409014ea782SRobert Watson goto drop; 1410014ea782SRobert Watson 1411302ce8d6SAndre Oppermann dropunlock: 141257f60867SMark Johnston if (m != NULL) 14132b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 141457f60867SMark Johnston 14159fa198beSAndre Oppermann if (inp != NULL) 141608d9c920SGleb Smirnoff INP_UNLOCK(inp); 1417014ea782SRobert Watson 1418302ce8d6SAndre Oppermann drop: 1419a160e630SAndre Oppermann if (s != NULL) 1420a160e630SAndre Oppermann free(s, M_TCPLOG); 1421302ce8d6SAndre Oppermann if (m != NULL) 1422302ce8d6SAndre Oppermann m_freem(m); 14238f5a8818SKevin Lo return (IPPROTO_DONE); 1424302ce8d6SAndre Oppermann } 1425302ce8d6SAndre Oppermann 1426e44c1887SSteven Hartland /* 1427e44c1887SSteven Hartland * Automatic sizing of receive socket buffer. Often the send 1428e44c1887SSteven Hartland * buffer size is not optimally adjusted to the actual network 1429e44c1887SSteven Hartland * conditions at hand (delay bandwidth product). Setting the 1430e44c1887SSteven Hartland * buffer size too small limits throughput on links with high 1431e44c1887SSteven Hartland * bandwidth and high delay (eg. trans-continental/oceanic links). 1432e44c1887SSteven Hartland * 1433e44c1887SSteven Hartland * On the receive side the socket buffer memory is only rarely 1434e44c1887SSteven Hartland * used to any significant extent. This allows us to be much 1435e44c1887SSteven Hartland * more aggressive in scaling the receive socket buffer. For 1436e44c1887SSteven Hartland * the case that the buffer space is actually used to a large 1437e44c1887SSteven Hartland * extent and we run out of kernel memory we can simply drop 1438e44c1887SSteven Hartland * the new segments; TCP on the sender will just retransmit it 1439e44c1887SSteven Hartland * later. Setting the buffer size too big may only consume too 1440e44c1887SSteven Hartland * much kernel memory if the application doesn't read() from 1441e44c1887SSteven Hartland * the socket or packet loss or reordering makes use of the 1442e44c1887SSteven Hartland * reassembly queue. 1443e44c1887SSteven Hartland * 1444e44c1887SSteven Hartland * The criteria to step up the receive buffer one notch are: 1445e44c1887SSteven Hartland * 1. Application has not set receive buffer size with 1446e44c1887SSteven Hartland * SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE. 1447560c0586SMichael Tuexen * 2. the number of bytes received during 1/2 of an sRTT 1448560c0586SMichael Tuexen * is at least 3/8 of the current socket buffer size. 1449560c0586SMichael Tuexen * 3. receive buffer size has not hit maximal automatic size; 1450e44c1887SSteven Hartland * 1451560c0586SMichael Tuexen * If all of the criteria are met we increaset the socket buffer 1452560c0586SMichael Tuexen * by a 1/2 (bounded by the max). This allows us to keep ahead 1453560c0586SMichael Tuexen * of slow-start but also makes it so our peer never gets limited 1454560c0586SMichael Tuexen * by our rwnd which we then open up causing a burst. 1455560c0586SMichael Tuexen * 1456560c0586SMichael Tuexen * This algorithm does two steps per RTT at most and only if 1457e44c1887SSteven Hartland * we receive a bulk stream w/o packet losses or reorderings. 1458e44c1887SSteven Hartland * Shrinking the buffer during idle times is not necessary as 1459e44c1887SSteven Hartland * it doesn't consume any memory when idle. 1460e44c1887SSteven Hartland * 1461e44c1887SSteven Hartland * TODO: Only step up if the application is actually serving 1462e44c1887SSteven Hartland * the buffer to better manage the socket buffer resources. 1463e44c1887SSteven Hartland */ 1464e44c1887SSteven Hartland int 1465e44c1887SSteven Hartland tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so, 1466e44c1887SSteven Hartland struct tcpcb *tp, int tlen) 1467e44c1887SSteven Hartland { 1468e44c1887SSteven Hartland int newsize = 0; 1469e44c1887SSteven Hartland 1470e44c1887SSteven Hartland if (V_tcp_do_autorcvbuf && (so->so_rcv.sb_flags & SB_AUTOSIZE) && 1471e44c1887SSteven Hartland tp->t_srtt != 0 && tp->rfbuf_ts != 0 && 1472e44c1887SSteven Hartland TCP_TS_TO_TICKS(tcp_ts_getticks() - tp->rfbuf_ts) > 1473560c0586SMichael Tuexen ((tp->t_srtt >> TCP_RTT_SHIFT)/2)) { 1474560c0586SMichael Tuexen if (tp->rfbuf_cnt > ((so->so_rcv.sb_hiwat / 2)/ 4 * 3) && 1475e44c1887SSteven Hartland so->so_rcv.sb_hiwat < V_tcp_autorcvbuf_max) { 1476560c0586SMichael Tuexen newsize = min((so->so_rcv.sb_hiwat + (so->so_rcv.sb_hiwat/2)), V_tcp_autorcvbuf_max); 1477e44c1887SSteven Hartland } 1478e44c1887SSteven Hartland TCP_PROBE6(receive__autoresize, NULL, tp, m, tp, th, newsize); 1479e44c1887SSteven Hartland 1480e44c1887SSteven Hartland /* Start over with next RTT. */ 1481e44c1887SSteven Hartland tp->rfbuf_ts = 0; 1482e44c1887SSteven Hartland tp->rfbuf_cnt = 0; 1483e44c1887SSteven Hartland } else { 1484e44c1887SSteven Hartland tp->rfbuf_cnt += tlen; /* add up */ 1485e44c1887SSteven Hartland } 1486e44c1887SSteven Hartland return (newsize); 1487e44c1887SSteven Hartland } 1488e44c1887SSteven Hartland 14899e644c23SMichael Tuexen int 14909e644c23SMichael Tuexen tcp_input(struct mbuf **mp, int *offp, int proto) 14919e644c23SMichael Tuexen { 14929e644c23SMichael Tuexen return(tcp_input_with_port(mp, offp, proto, 0)); 14939e644c23SMichael Tuexen } 14949e644c23SMichael Tuexen 149555bceb1eSRandall Stewart void 14964d0770f1SRichard Scheffenegger tcp_handle_wakeup(struct tcpcb *tp, struct socket *so) 14974d0770f1SRichard Scheffenegger { 14984d0770f1SRichard Scheffenegger /* 14994d0770f1SRichard Scheffenegger * Since tp might be gone if the session entered 15004d0770f1SRichard Scheffenegger * the TIME_WAIT state before coming here, we need 15014d0770f1SRichard Scheffenegger * to check if the socket is still connected. 15024d0770f1SRichard Scheffenegger */ 1503032bf749SRichard Scheffenegger if (tp == NULL) { 15044d0770f1SRichard Scheffenegger return; 1505032bf749SRichard Scheffenegger } 1506032bf749SRichard Scheffenegger if (so == NULL) { 1507032bf749SRichard Scheffenegger return; 1508032bf749SRichard Scheffenegger } 15094d0770f1SRichard Scheffenegger INP_LOCK_ASSERT(tp->t_inpcb); 15104d0770f1SRichard Scheffenegger if (tp->t_flags & TF_WAKESOR) { 15114d0770f1SRichard Scheffenegger tp->t_flags &= ~TF_WAKESOR; 1512032bf749SRichard Scheffenegger SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1513032bf749SRichard Scheffenegger sorwakeup_locked(so); 15144d0770f1SRichard Scheffenegger } 15154d0770f1SRichard Scheffenegger } 15164d0770f1SRichard Scheffenegger 15174d0770f1SRichard Scheffenegger void 1518302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 15196573d758SMatt Macy struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos) 1520302ce8d6SAndre Oppermann { 1521f7220c48SRichard Scheffenegger uint16_t thflags; 1522f7220c48SRichard Scheffenegger int acked, ourfinisacked, needoutput = 0, sack_changed; 1523b2ade6b1SRichard Scheffenegger int rstreason, todrop, win, incforsyn = 0; 15243ac12506SJonathan T. Looney uint32_t tiwin; 15254b7b743cSLawrence Stewart uint16_t nsegs; 152607dacf03SAndre Oppermann char *s; 152707dacf03SAndre Oppermann struct in_conninfo *inc; 1528c11a15bfSGleb Smirnoff struct mbuf *mfree; 1529302ce8d6SAndre Oppermann struct tcpopt to; 1530281a0fd4SPatrick Kelsey int tfo_syn; 15313c40e1d5SRichard Scheffenegger u_int maxseg; 1532302ce8d6SAndre Oppermann 153314739780SMaxim Konovalov #ifdef TCPDEBUG 153414739780SMaxim Konovalov /* 153514739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 153614739780SMaxim Konovalov * now IPv6. 153714739780SMaxim Konovalov */ 153814739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 153914739780SMaxim Konovalov struct tcphdr tcp_savetcp; 154014739780SMaxim Konovalov short ostate = 0; 154114739780SMaxim Konovalov #endif 15421ebf4607SRichard Scheffenegger thflags = tcp_get_flags(th); 154307dacf03SAndre Oppermann inc = &tp->t_inpcb->inp_inc; 1544d64a46eaSLawrence Stewart tp->sackhint.last_sack_ack = 0; 1545021eaf79SHiren Panchasara sack_changed = 0; 15464b7b743cSLawrence Stewart nsegs = max(1, m->m_pkthdr.lro_nsegs); 1547d40c0d47SGleb Smirnoff 1548d40c0d47SGleb Smirnoff NET_EPOCH_ASSERT(); 15498501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1550679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1551679d9708SAndre Oppermann __func__)); 1552679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1553679d9708SAndre Oppermann __func__)); 1554df8bae1dSRodney W. Grimes 155586a996e6SHiren Panchasara #ifdef TCPPCAP 155686a996e6SHiren Panchasara /* Save segment, if requested. */ 155786a996e6SHiren Panchasara tcp_pcap_add(th, m, &(tp->t_inpkts)); 155886a996e6SHiren Panchasara #endif 15592529f56eSJonathan T. Looney TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0, 15602529f56eSJonathan T. Looney tlen, NULL, true); 156186a996e6SHiren Panchasara 1562013f4df6SMichael Tuexen if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) { 1563013f4df6SMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 1564013f4df6SMichael Tuexen log(LOG_DEBUG, "%s; %s: " 1565013f4df6SMichael Tuexen "SYN|FIN segment ignored (based on " 1566013f4df6SMichael Tuexen "sysctl setting)\n", s, __func__); 1567013f4df6SMichael Tuexen free(s, M_TCPLOG); 1568013f4df6SMichael Tuexen } 1569013f4df6SMichael Tuexen goto drop; 1570013f4df6SMichael Tuexen } 1571013f4df6SMichael Tuexen 1572df8bae1dSRodney W. Grimes /* 1573ebfd7534SMichael Tuexen * If a segment with the ACK-bit set arrives in the SYN-SENT state 1574ebfd7534SMichael Tuexen * check SEQ.ACK first. 1575ebfd7534SMichael Tuexen */ 1576ebfd7534SMichael Tuexen if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) && 1577ebfd7534SMichael Tuexen (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) { 1578ebfd7534SMichael Tuexen rstreason = BANDLIM_UNLIMITED; 1579ebfd7534SMichael Tuexen goto dropwithreset; 1580ebfd7534SMichael Tuexen } 1581ebfd7534SMichael Tuexen 1582ebfd7534SMichael Tuexen /* 1583df8bae1dSRodney W. Grimes * Segment received on connection. 1584df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1585e8949f74SAndre Oppermann * XXX: This should be done after segment 1586e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 1587df8bae1dSRodney W. Grimes */ 15889b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 1589df8bae1dSRodney W. Grimes 1590df8bae1dSRodney W. Grimes /* 1591c52102ddSHiren Panchasara * Scale up the window into a 32-bit value. 1592e8949f74SAndre Oppermann * For the SYN_SENT state the scale is zero. 1593464fcfbcSAndre Oppermann */ 1594464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 1595adc56f5aSEdward Tomasz Napierala #ifdef STATS 1596adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin); 1597adc56f5aSEdward Tomasz Napierala #endif 1598464fcfbcSAndre Oppermann 1599464fcfbcSAndre Oppermann /* 1600f2512ba1SRui Paulo * TCP ECN processing. 1601f2512ba1SRui Paulo */ 1602f7220c48SRichard Scheffenegger if (tcp_ecn_input_segment(tp, thflags, iptos)) 1603dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_ECN); 1604f2512ba1SRui Paulo 1605f2512ba1SRui Paulo /* 1606f72167f4SAndre Oppermann * Parse options on any incoming segment. 1607f72167f4SAndre Oppermann */ 1608302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1609302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1610302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1611f72167f4SAndre Oppermann 1612fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1613fcf59617SAndrey V. Elsukov if ((tp->t_flags & TF_SIGNATURE) != 0 && 1614fcf59617SAndrey V. Elsukov (to.to_flags & TOF_SIGNATURE) == 0) { 1615fcf59617SAndrey V. Elsukov TCPSTAT_INC(tcps_sig_err_sigopt); 1616fcf59617SAndrey V. Elsukov /* XXX: should drop? */ 1617fcf59617SAndrey V. Elsukov } 1618fcf59617SAndrey V. Elsukov #endif 1619f72167f4SAndre Oppermann /* 1620f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1621bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1622bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1623bf6d304aSAndre Oppermann * was established. 1624f72167f4SAndre Oppermann */ 1625bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1626e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 1627d8951c8aSBjoern A. Zeeb if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) 1628f72167f4SAndre Oppermann to.to_tsecr = 0; 16294531b345SRichard Scheffenegger else if (tp->t_rxtshift == 1 && 16304531b345SRichard Scheffenegger tp->t_flags & TF_PREVVALID && 16314531b345SRichard Scheffenegger tp->t_badrxtwin != 0 && 16324531b345SRichard Scheffenegger TSTMP_LT(to.to_tsecr, tp->t_badrxtwin)) 163310d20c84SMatt Macy cc_cong_signal(tp, th, CC_RTO_ERR); 1634bf6d304aSAndre Oppermann } 163507dacf03SAndre Oppermann /* 163697d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 163797d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 16385396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 16395396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 164097d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1641df8bae1dSRodney W. Grimes */ 16420a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 16436e16d877SRichard Scheffenegger /* Handle parallel SYN for ECN */ 1644f7220c48SRichard Scheffenegger tcp_ecn_input_parallel_syn(tp, thflags, iptos); 1645464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 16462593f858SRichard Scheffenegger (tp->t_flags & TF_REQ_SCALE) && 16472593f858SRichard Scheffenegger !(tp->t_flags & TF_NOOPT)) { 1648be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 164902a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 16508e051165SRichard Scheffenegger } else 16518e051165SRichard Scheffenegger tp->t_flags &= ~TF_REQ_SCALE; 16525396d0f8SAndre Oppermann /* 16535396d0f8SAndre Oppermann * Initial send window. It will be updated with 16545396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 16555396d0f8SAndre Oppermann */ 16565396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 16578e051165SRichard Scheffenegger if ((to.to_flags & TOF_TS) && 16582593f858SRichard Scheffenegger (tp->t_flags & TF_REQ_TSTMP) && 16592593f858SRichard Scheffenegger !(tp->t_flags & TF_NOOPT)) { 1660be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1661be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1662d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 16638e051165SRichard Scheffenegger } else 16648e051165SRichard Scheffenegger tp->t_flags &= ~TF_REQ_TSTMP; 1665be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1666be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 16673529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 16682593f858SRichard Scheffenegger (!(to.to_flags & TOF_SACKPERM) || 16692593f858SRichard Scheffenegger (tp->t_flags & TF_NOOPT))) 16703529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 1671c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) { 16722593f858SRichard Scheffenegger if ((to.to_flags & TOF_FASTOPEN) && 16732593f858SRichard Scheffenegger !(tp->t_flags & TF_NOOPT)) { 1674a026a53aSMichael Tuexen uint16_t mss; 1675a026a53aSMichael Tuexen 1676a026a53aSMichael Tuexen if (to.to_flags & TOF_MSS) 1677a026a53aSMichael Tuexen mss = to.to_mss; 1678c560df6fSPatrick Kelsey else 1679a026a53aSMichael Tuexen if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) 1680a026a53aSMichael Tuexen mss = TCP6_MSS; 1681a026a53aSMichael Tuexen else 1682a026a53aSMichael Tuexen mss = TCP_MSS; 1683a026a53aSMichael Tuexen tcp_fastopen_update_cache(tp, mss, 1684a026a53aSMichael Tuexen to.to_tfo_len, to.to_tfo_cookie); 1685a026a53aSMichael Tuexen } else 1686c560df6fSPatrick Kelsey tcp_fastopen_disable_path(tp); 1687c560df6fSPatrick Kelsey } 16886d90faf3SPaul Saab } 16896d90faf3SPaul Saab 1690df8bae1dSRodney W. Grimes /* 1691283c76c7SMichael Tuexen * If timestamps were negotiated during SYN/ACK and a 1692283c76c7SMichael Tuexen * segment without a timestamp is received, silently drop 1693d2b3ceddSMichael Tuexen * the segment, unless it is a RST segment or missing timestamps are 1694d2b3ceddSMichael Tuexen * tolerated. 1695283c76c7SMichael Tuexen * See section 3.2 of RFC 7323. 16964da9052aSMichael Tuexen */ 16974da9052aSMichael Tuexen if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) { 1698d2b3ceddSMichael Tuexen if (((thflags & TH_RST) != 0) || V_tcp_tolerate_missing_ts) { 1699cc3c3485SMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 1700cc3c3485SMichael Tuexen log(LOG_DEBUG, "%s; %s: Timestamp missing, " 1701cc3c3485SMichael Tuexen "segment processed normally\n", 1702cc3c3485SMichael Tuexen s, __func__); 1703cc3c3485SMichael Tuexen free(s, M_TCPLOG); 1704cc3c3485SMichael Tuexen } 1705cc3c3485SMichael Tuexen } else { 17064da9052aSMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 17074da9052aSMichael Tuexen log(LOG_DEBUG, "%s; %s: Timestamp missing, " 1708283c76c7SMichael Tuexen "segment silently dropped\n", s, __func__); 17094da9052aSMichael Tuexen free(s, M_TCPLOG); 17104da9052aSMichael Tuexen } 1711283c76c7SMichael Tuexen goto drop; 17124da9052aSMichael Tuexen } 1713cc3c3485SMichael Tuexen } 1714283c76c7SMichael Tuexen /* 1715283c76c7SMichael Tuexen * If timestamps were not negotiated during SYN/ACK and a 171675fcd27aSMichael Tuexen * segment with a timestamp is received, ignore the 1717283c76c7SMichael Tuexen * timestamp and process the packet normally. 1718283c76c7SMichael Tuexen * See section 3.2 of RFC 7323. 1719283c76c7SMichael Tuexen */ 17204da9052aSMichael Tuexen if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) { 17214da9052aSMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 17224da9052aSMichael Tuexen log(LOG_DEBUG, "%s; %s: Timestamp not expected, " 1723283c76c7SMichael Tuexen "segment processed normally\n", s, __func__); 17244da9052aSMichael Tuexen free(s, M_TCPLOG); 17254da9052aSMichael Tuexen } 17264da9052aSMichael Tuexen } 17274da9052aSMichael Tuexen 17284da9052aSMichael Tuexen /* 1729df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1730df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1731df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1732df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1733df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1734df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1735df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1736df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1737df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1738df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1739df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1740df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1741a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1742c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 1743a0292f23SGarrett Wollman * be TH_NEEDSYN. 1744df8bae1dSRodney W. Grimes */ 1745df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1746c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 1747fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1748c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 1749c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 1750a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1751c28440dbSRandall Stewart SEGQ_EMPTY(tp) && 17524741bfcbSPatrick Kelsey ((to.to_flags & TOF_TS) == 0 || 1753c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1754df8bae1dSRodney W. Grimes /* 1755df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1756df8bae1dSRodney W. Grimes * record the timestamp. 1757a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1758a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1759df8bae1dSRodney W. Grimes */ 1760be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1761fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1762d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1763a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1764df8bae1dSRodney W. Grimes } 1765df8bae1dSRodney W. Grimes 1766fb59c426SYoshinobu Inoue if (tlen == 0) { 1767fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1768fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1769dbc42409SLawrence Stewart !IN_RECOVERY(tp->t_flags) && 1770fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1771dbc42409SLawrence Stewart TAILQ_EMPTY(&tp->snd_holes)) { 1772df8bae1dSRodney W. Grimes /* 1773e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1774df8bae1dSRodney W. Grimes */ 177578b50714SRobert Watson TCPSTAT_INC(tcps_predack); 1776252ca428SRobert Watson 17779b8b58e0SJonathan Lemon /* 177810d20c84SMatt Macy * "bad retransmit" recovery without timestamps. 17799b8b58e0SJonathan Lemon */ 178010d20c84SMatt Macy if ((to.to_flags & TOF_TS) == 0 && 178110d20c84SMatt Macy tp->t_rxtshift == 1 && 1782672dc4aeSJohn Baldwin tp->t_flags & TF_PREVVALID && 17834531b345SRichard Scheffenegger tp->t_badrxtwin != 0 && 17844531b345SRichard Scheffenegger TSTMP_LT(ticks, tp->t_badrxtwin)) { 1785dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 17869b8b58e0SJonathan Lemon } 1787fa55172bSMatthew Dillon 1788fa55172bSMatthew Dillon /* 1789fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1790fa55172bSMatthew Dillon * 1791fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1792fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1793fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1794fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1795fa55172bSMatthew Dillon */ 1796fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1797fa55172bSMatthew Dillon to.to_tsecr) { 17983ac12506SJonathan T. Looney uint32_t t; 1799d8951c8aSBjoern A. Zeeb 1800d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 1801d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 1802d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 1803a0292f23SGarrett Wollman tcp_xmit_timer(tp, 1804d8951c8aSBjoern A. Zeeb TCP_TS_TO_TICKS(t) + 1); 1805fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1806fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1807eaf80179SAndre Oppermann if (!tp->t_rttlow || 1808eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1809eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1810c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1811c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1812fa55172bSMatthew Dillon } 1813dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 181439bc9de5SLawrence Stewart 1815bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 181639bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 181739bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 1818bd79708dSJonathan T. Looney #endif 181939bc9de5SLawrence Stewart 18204b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvackpack, nsegs); 182178b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 1822df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 18239d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 18249d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 18259d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 1826dbc42409SLawrence Stewart 1827dbc42409SLawrence Stewart /* 1828dbc42409SLawrence Stewart * Let the congestion control algorithm update 1829dbc42409SLawrence Stewart * congestion control related information. This 1830dbc42409SLawrence Stewart * typically means increasing the congestion 1831dbc42409SLawrence Stewart * window. 1832dbc42409SLawrence Stewart */ 18334b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, CC_ACK); 1834dbc42409SLawrence Stewart 18359d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 18361645d090SJeff Roberson /* 1837e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 18381645d090SJeff Roberson * to th_ack. 18391645d090SJeff Roberson */ 1840cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1841b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1842df8bae1dSRodney W. Grimes m_freem(m); 1843df8bae1dSRodney W. Grimes 1844df8bae1dSRodney W. Grimes /* 1845df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1846df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1847df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1848df8bae1dSRodney W. Grimes * If process is waiting for space, 1849df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1850df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1851df8bae1dSRodney W. Grimes * decide between more output or persist. 1852e8949f74SAndre Oppermann */ 18533c653157SHartmut Brandt #ifdef TCPDEBUG 18543c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 18553c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 18563c653157SHartmut Brandt (void *)tcp_saveipgen, 18573c653157SHartmut Brandt &tcp_savetcp, 0); 18583c653157SHartmut Brandt #endif 18592b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 1860df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1861b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1862b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1863b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1864b8152ba7SAndre Oppermann tp->t_rxtcur); 1865032bf749SRichard Scheffenegger sowwakeup(so); 1866cfa6009eSGleb Smirnoff if (sbavail(&so->so_snd)) 186740fa3e40SGleb Smirnoff (void) tcp_output(tp); 1868a14c749fSJonathan Lemon goto check_delack; 1869df8bae1dSRodney W. Grimes } 1870fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1871fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 18726741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 18736741ecf5SAndre Oppermann 1874df8bae1dSRodney W. Grimes /* 1875252ca428SRobert Watson * This is a pure, in-sequence data packet with 1876252ca428SRobert Watson * nothing on the reassembly queue and we have enough 1877252ca428SRobert Watson * buffer space to take it. 1878df8bae1dSRodney W. Grimes */ 18796d90faf3SPaul Saab /* Clean receiver SACK report if present */ 18803529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 18816d90faf3SPaul Saab tcp_clean_sackreport(tp); 188278b50714SRobert Watson TCPSTAT_INC(tcps_preddat); 1883fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 1884e854dd38SRandall Stewart if (tlen && 1885e854dd38SRandall Stewart ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 1886e854dd38SRandall Stewart (tp->t_fbyte_in == 0)) { 1887e854dd38SRandall Stewart tp->t_fbyte_in = ticks; 1888e854dd38SRandall Stewart if (tp->t_fbyte_in == 0) 1889e854dd38SRandall Stewart tp->t_fbyte_in = 1; 1890e854dd38SRandall Stewart if (tp->t_fbyte_out && tp->t_fbyte_in) 1891e854dd38SRandall Stewart tp->t_flags2 |= TF2_FBYTES_COMPLETE; 1892e854dd38SRandall Stewart } 18931645d090SJeff Roberson /* 18941645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 18951645d090SJeff Roberson * th_seq. 18961645d090SJeff Roberson */ 189760ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 18981645d090SJeff Roberson /* 18991645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 19001645d090SJeff Roberson * rcv_nxt. 19011645d090SJeff Roberson */ 19021645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 19034b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvpack, nsegs); 190478b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 19053c653157SHartmut Brandt #ifdef TCPDEBUG 19063c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 19073c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 19083c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 19093c653157SHartmut Brandt #endif 19102b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 19115d06879aSGeorge V. Neville-Neil 1912e44c1887SSteven Hartland newsize = tcp_autorcvbuf(m, th, so, tp, tlen); 19136741ecf5SAndre Oppermann 19146741ecf5SAndre Oppermann /* Add data to socket buffer. */ 19151e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1916c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1917c1c36a2cSMike Silbersack m_freem(m); 1918c1c36a2cSMike Silbersack } else { 19196741ecf5SAndre Oppermann /* 19206741ecf5SAndre Oppermann * Set new socket buffer size. 19216741ecf5SAndre Oppermann * Give up when limit is reached. 19226741ecf5SAndre Oppermann */ 19236741ecf5SAndre Oppermann if (newsize) 192443283184SGleb Smirnoff if (!sbreserve_locked(so, SO_RCV, 192543283184SGleb Smirnoff newsize, NULL)) 19266741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1927fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 1928651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 1929c1c36a2cSMike Silbersack } 1930032bf749SRichard Scheffenegger /* NB: sorwakeup_locked() does an implicit unlock. */ 1931032bf749SRichard Scheffenegger sorwakeup_locked(so); 1932c1e5a6e5SAndre Oppermann if (DELAY_ACK(tp, tlen)) { 19333bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1934f498eeeeSDavid Greenman } else { 1935e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 193640fa3e40SGleb Smirnoff tcp_output(tp); 1937e612a582SDavid Greenman } 1938a14c749fSJonathan Lemon goto check_delack; 1939df8bae1dSRodney W. Grimes } 1940df8bae1dSRodney W. Grimes } 1941df8bae1dSRodney W. Grimes 1942df8bae1dSRodney W. Grimes /* 1943df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1944df8bae1dSRodney W. Grimes * and then do TCP input processing. 1945df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1946df8bae1dSRodney W. Grimes * but not less than advertised window. 1947df8bae1dSRodney W. Grimes */ 1948df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1949df8bae1dSRodney W. Grimes if (win < 0) 1950df8bae1dSRodney W. Grimes win = 0; 195166e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1952df8bae1dSRodney W. Grimes 1953df8bae1dSRodney W. Grimes switch (tp->t_state) { 1954df8bae1dSRodney W. Grimes /* 1955764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1956764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1957764d8cefSBill Fenner */ 1958764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1959fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1960fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1961a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1962a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1963a57815efSBosko Milekic goto dropwithreset; 1964a57815efSBosko Milekic } 196568bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) { 1966281a0fd4SPatrick Kelsey /* 1967281a0fd4SPatrick Kelsey * When a TFO connection is in SYN_RECEIVED, the 1968281a0fd4SPatrick Kelsey * only valid packets are the initial SYN, a 1969281a0fd4SPatrick Kelsey * retransmit/copy of the initial SYN (possibly with 1970281a0fd4SPatrick Kelsey * a subset of the original data), a valid ACK, a 1971281a0fd4SPatrick Kelsey * FIN, or a RST. 1972281a0fd4SPatrick Kelsey */ 1973281a0fd4SPatrick Kelsey if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) { 1974281a0fd4SPatrick Kelsey rstreason = BANDLIM_RST_OPENPORT; 1975281a0fd4SPatrick Kelsey goto dropwithreset; 1976281a0fd4SPatrick Kelsey } else if (thflags & TH_SYN) { 1977281a0fd4SPatrick Kelsey /* non-initial SYN is ignored */ 1978281a0fd4SPatrick Kelsey if ((tcp_timer_active(tp, TT_DELACK) || 1979281a0fd4SPatrick Kelsey tcp_timer_active(tp, TT_REXMT))) 1980281a0fd4SPatrick Kelsey goto drop; 1981281a0fd4SPatrick Kelsey } else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) { 1982281a0fd4SPatrick Kelsey goto drop; 1983281a0fd4SPatrick Kelsey } 1984281a0fd4SPatrick Kelsey } 1985764d8cefSBill Fenner break; 1986764d8cefSBill Fenner 1987764d8cefSBill Fenner /* 1988df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 198998732609SMichael Tuexen * if seg contains a RST with valid ACK (SEQ.ACK has already 199098732609SMichael Tuexen * been verified), then drop the connection. 199198732609SMichael Tuexen * if seg contains a RST without an ACK, drop the seg. 199298732609SMichael Tuexen * if seg does not contain SYN, then drop the seg. 1993df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1994df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1995df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1996f2512ba1SRui Paulo * if seg contains an ECE and ECN support is enabled, the stream 1997f2512ba1SRui Paulo * is ECN capable. 1998df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1999df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 2000df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 2001df8bae1dSRodney W. Grimes */ 2002df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 200357f60867SMark Johnston if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) { 2004d9fae5abSAndriy Gapon TCP_PROBE5(connect__refused, NULL, tp, 20052b9c9984SGeorge V. Neville-Neil m, tp, th); 2006df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 200757f60867SMark Johnston } 20080ca3f933SAndre Oppermann if (thflags & TH_RST) 2009df8bae1dSRodney W. Grimes goto drop; 20100ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 2011df8bae1dSRodney W. Grimes goto drop; 2012464fcfbcSAndre Oppermann 2013fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 2014df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 2015fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 2016c560df6fSPatrick Kelsey int tfo_partial_ack = 0; 2017c560df6fSPatrick Kelsey 201878b50714SRobert Watson TCPSTAT_INC(tcps_connects); 2019845799c1SAndras Olah soisconnected(so); 2020c488362eSRobert Watson #ifdef MAC 202130d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 2022c488362eSRobert Watson #endif 2023845799c1SAndras Olah /* Do window scaling on this connection? */ 2024845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2025845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2026845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 2027845799c1SAndras Olah } 20283ac12506SJonathan T. Looney tp->rcv_adv += min(tp->rcv_wnd, 2029766282cbSJohn Baldwin TCP_MAXWIN << tp->rcv_scale); 2030a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 2031a0292f23SGarrett Wollman /* 2032c560df6fSPatrick Kelsey * If not all the data that was sent in the TFO SYN 2033c560df6fSPatrick Kelsey * has been acked, resend the remainder right away. 2034c560df6fSPatrick Kelsey */ 2035c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags) && 2036c560df6fSPatrick Kelsey (tp->snd_una != tp->snd_max)) { 2037c560df6fSPatrick Kelsey tp->snd_nxt = th->th_ack; 2038c560df6fSPatrick Kelsey tfo_partial_ack = 1; 2039c560df6fSPatrick Kelsey } 2040c560df6fSPatrick Kelsey /* 2041a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 2042a0292f23SGarrett Wollman * ACKNOW will be turned on later. 2043a0292f23SGarrett Wollman */ 2044c560df6fSPatrick Kelsey if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial_ack) 2045b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 2046b8152ba7SAndre Oppermann tcp_delacktime); 2047a0292f23SGarrett Wollman else 2048a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2049f2512ba1SRui Paulo 2050f7220c48SRichard Scheffenegger tcp_ecn_input_syn_sent(tp, thflags, iptos); 2051f2512ba1SRui Paulo 2052a0292f23SGarrett Wollman /* 2053a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 2054a0292f23SGarrett Wollman * Transitions: 2055a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 2056a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 2057a0292f23SGarrett Wollman */ 20589b8b58e0SJonathan Lemon tp->t_starttime = ticks; 2059a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 206057f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2061a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 2062fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 20637ff19458SPaul Traina } else { 206457f60867SMark Johnston tcp_state_change(tp, TCPS_ESTABLISHED); 2065d9fae5abSAndriy Gapon TCP_PROBE5(connect__established, NULL, tp, 20662b9c9984SGeorge V. Neville-Neil m, tp, th); 2067dbc42409SLawrence Stewart cc_conn_init(tp); 20689077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 20699077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 20707ff19458SPaul Traina } 2071a0292f23SGarrett Wollman } else { 2072a0292f23SGarrett Wollman /* 2073c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 2074153edc50SHiren Panchasara * simultaneous open. 2075c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 2076c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 2077a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 2078a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 2079a0292f23SGarrett Wollman */ 20804b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 2081b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 208257f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_RECEIVED); 2083a0292f23SGarrett Wollman } 2084df8bae1dSRodney W. Grimes 20858501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 20867cfc6904SRobert Watson 2087df8bae1dSRodney W. Grimes /* 2088fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 2089df8bae1dSRodney W. Grimes * If data, trim to stay within window, 2090df8bae1dSRodney W. Grimes * dropping FIN if necessary. 2091df8bae1dSRodney W. Grimes */ 2092fb59c426SYoshinobu Inoue th->th_seq++; 2093fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 2094fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 2095df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2096fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 2097fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 209878b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 209978b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2100df8bae1dSRodney W. Grimes } 2101fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 2102fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 2103a0292f23SGarrett Wollman /* 2104a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 2105a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 2106a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 2107a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 2108a0292f23SGarrett Wollman * Otherwise, goto step 6. 2109a0292f23SGarrett Wollman */ 2110fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 2111a0292f23SGarrett Wollman goto process_ACK; 2112c068736aSJeffrey Hsu 2113df8bae1dSRodney W. Grimes goto step6; 2114c068736aSJeffrey Hsu 2115a0292f23SGarrett Wollman /* 2116a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 2117c94c54e4SAndre Oppermann * do normal processing. 2118a0292f23SGarrett Wollman * 2119c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 2120a0292f23SGarrett Wollman */ 2121a0292f23SGarrett Wollman case TCPS_LAST_ACK: 2122a0292f23SGarrett Wollman case TCPS_CLOSING: 2123a0292f23SGarrett Wollman break; /* continue normal processing */ 2124df8bae1dSRodney W. Grimes } 2125df8bae1dSRodney W. Grimes 2126df8bae1dSRodney W. Grimes /* 2127df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 212880ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 212980ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 213080ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 213180ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 213280ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 213380ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 213480ab7c0eSGarrett Wollman * killing a connection). 213580ab7c0eSGarrett Wollman * Then check timestamp, if present. 2136a0292f23SGarrett Wollman * Then check the connection count, if present. 2137df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 2138df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 2139df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 214080ab7c0eSGarrett Wollman */ 2141fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 21423220a212SGleb Smirnoff /* 21433220a212SGleb Smirnoff * RFC5961 Section 3.2 21443220a212SGleb Smirnoff * 21453220a212SGleb Smirnoff * - RST drops connection only if SEG.SEQ == RCV.NXT. 21463220a212SGleb Smirnoff * - If RST is in window, we send challenge ACK. 21473220a212SGleb Smirnoff * 21483220a212SGleb Smirnoff * Note: to take into account delayed ACKs, we should 21493220a212SGleb Smirnoff * test against last_ack_sent instead of rcv_nxt. 21503220a212SGleb Smirnoff * Note 2: we handle special case of closed window, not 21513220a212SGleb Smirnoff * covered by the RFC. 21523220a212SGleb Smirnoff */ 21533220a212SGleb Smirnoff if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 21543220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) || 21553220a212SGleb Smirnoff (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) { 21563220a212SGleb Smirnoff KASSERT(tp->t_state != TCPS_SYN_SENT, 21573220a212SGleb Smirnoff ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p", 21583220a212SGleb Smirnoff __func__, th, tp)); 21593220a212SGleb Smirnoff 21603220a212SGleb Smirnoff if (V_tcp_insecure_rst || 21613220a212SGleb Smirnoff tp->last_ack_sent == th->th_seq) { 21623220a212SGleb Smirnoff TCPSTAT_INC(tcps_drops); 21633220a212SGleb Smirnoff /* Drop the connection. */ 21643220a212SGleb Smirnoff switch (tp->t_state) { 216580ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 216680ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 216780ab7c0eSGarrett Wollman goto close; 216880ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 216980ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 217080ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 217180ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 21726779a1a1SMichael Tuexen case TCPS_CLOSING: 21736779a1a1SMichael Tuexen case TCPS_LAST_ACK: 217480ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 217580ab7c0eSGarrett Wollman close: 21763220a212SGleb Smirnoff /* FALLTHROUGH */ 21773220a212SGleb Smirnoff default: 217880ab7c0eSGarrett Wollman tp = tcp_close(tp); 21793220a212SGleb Smirnoff } 21803220a212SGleb Smirnoff } else { 21813220a212SGleb Smirnoff TCPSTAT_INC(tcps_badrst); 21823220a212SGleb Smirnoff /* Send challenge ACK. */ 21833220a212SGleb Smirnoff tcp_respond(tp, mtod(m, void *), th, m, 21843220a212SGleb Smirnoff tp->rcv_nxt, tp->snd_nxt, TH_ACK); 21853220a212SGleb Smirnoff tp->last_ack_sent = tp->rcv_nxt; 21863220a212SGleb Smirnoff m = NULL; 21873220a212SGleb Smirnoff } 21883220a212SGleb Smirnoff } 21893220a212SGleb Smirnoff goto drop; 21903220a212SGleb Smirnoff } 219180ab7c0eSGarrett Wollman 21923220a212SGleb Smirnoff /* 21933220a212SGleb Smirnoff * RFC5961 Section 4.2 21943220a212SGleb Smirnoff * Send challenge ACK for any SYN in synchronized state. 21953220a212SGleb Smirnoff */ 2196281a0fd4SPatrick Kelsey if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT && 2197281a0fd4SPatrick Kelsey tp->t_state != TCPS_SYN_RECEIVED) { 21983220a212SGleb Smirnoff TCPSTAT_INC(tcps_badsyn); 21993220a212SGleb Smirnoff if (V_tcp_insecure_syn && 22003220a212SGleb Smirnoff SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 22013220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 22023220a212SGleb Smirnoff tp = tcp_drop(tp, ECONNRESET); 22033220a212SGleb Smirnoff rstreason = BANDLIM_UNLIMITED; 22043220a212SGleb Smirnoff } else { 22053220a212SGleb Smirnoff /* Send challenge ACK. */ 22063220a212SGleb Smirnoff tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt, 22073220a212SGleb Smirnoff tp->snd_nxt, TH_ACK); 22083220a212SGleb Smirnoff tp->last_ack_sent = tp->rcv_nxt; 22093220a212SGleb Smirnoff m = NULL; 221080ab7c0eSGarrett Wollman } 221180ab7c0eSGarrett Wollman goto drop; 221280ab7c0eSGarrett Wollman } 221380ab7c0eSGarrett Wollman 221480ab7c0eSGarrett Wollman /* 2215df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 2216df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 2217df8bae1dSRodney W. Grimes */ 2218be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 221980ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 2220df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 2221d8951c8aSBjoern A. Zeeb if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 2222df8bae1dSRodney W. Grimes /* 2223df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 2224df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 2225df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 2226df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 2227df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 2228df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 2229df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 2230df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 2231df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 2232df8bae1dSRodney W. Grimes */ 2233df8bae1dSRodney W. Grimes tp->ts_recent = 0; 2234df8bae1dSRodney W. Grimes } else { 223578b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 223678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 223778b50714SRobert Watson TCPSTAT_INC(tcps_pawsdrop); 223807fd333dSMatthew Dillon if (tlen) 2239df8bae1dSRodney W. Grimes goto dropafterack; 22401ab4789dSMatthew Dillon goto drop; 22411ab4789dSMatthew Dillon } 2242df8bae1dSRodney W. Grimes } 2243df8bae1dSRodney W. Grimes 2244a0292f23SGarrett Wollman /* 224580ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 224680ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 224780ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 224880ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 224980ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 225080ab7c0eSGarrett Wollman */ 2251a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 2252a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2253a57815efSBosko Milekic goto dropwithreset; 2254a57815efSBosko Milekic } 225580ab7c0eSGarrett Wollman 2256fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 2257df8bae1dSRodney W. Grimes if (todrop > 0) { 2258831ad37eSXin LI if (thflags & TH_SYN) { 2259fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 2260fb59c426SYoshinobu Inoue th->th_seq++; 2261fb59c426SYoshinobu Inoue if (th->th_urp > 1) 2262fb59c426SYoshinobu Inoue th->th_urp--; 2263df8bae1dSRodney W. Grimes else 2264fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2265df8bae1dSRodney W. Grimes todrop--; 2266df8bae1dSRodney W. Grimes } 2267df8bae1dSRodney W. Grimes /* 2268dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 2269df8bae1dSRodney W. Grimes */ 2270fb59c426SYoshinobu Inoue if (todrop > tlen 2271fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 2272dac20301SGarrett Wollman /* 2273dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 2274dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 2275dac20301SGarrett Wollman * of sequence; drop it. 2276dac20301SGarrett Wollman */ 2277fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2278dac20301SGarrett Wollman 2279df8bae1dSRodney W. Grimes /* 2280dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 2281dac20301SGarrett Wollman * But keep on processing for RST or ACK. 2282df8bae1dSRodney W. Grimes */ 2283dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2284fb59c426SYoshinobu Inoue todrop = tlen; 228578b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 228678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 2287df8bae1dSRodney W. Grimes } else { 228878b50714SRobert Watson TCPSTAT_INC(tcps_rcvpartduppack); 228978b50714SRobert Watson TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 2290df8bae1dSRodney W. Grimes } 22915acfd95cSMichael Tuexen /* 22925acfd95cSMichael Tuexen * DSACK - add SACK block for dropped range 22935acfd95cSMichael Tuexen */ 22948f63a52bSMichael Tuexen if ((todrop > 0) && (tp->t_flags & TF_SACK_PERMIT)) { 2295ecc5b1d1SMichael Tuexen tcp_update_sack_list(tp, th->th_seq, 2296ecc5b1d1SMichael Tuexen th->th_seq + todrop); 22975acfd95cSMichael Tuexen /* 22985acfd95cSMichael Tuexen * ACK now, as the next in-sequence segment 22995acfd95cSMichael Tuexen * will clear the DSACK block again 23005acfd95cSMichael Tuexen */ 23015acfd95cSMichael Tuexen tp->t_flags |= TF_ACKNOW; 23025acfd95cSMichael Tuexen } 2303fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 2304fb59c426SYoshinobu Inoue th->th_seq += todrop; 2305fb59c426SYoshinobu Inoue tlen -= todrop; 2306fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 2307fb59c426SYoshinobu Inoue th->th_urp -= todrop; 2308df8bae1dSRodney W. Grimes else { 2309fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2310fb59c426SYoshinobu Inoue th->th_urp = 0; 2311df8bae1dSRodney W. Grimes } 2312df8bae1dSRodney W. Grimes } 2313df8bae1dSRodney W. Grimes 2314df8bae1dSRodney W. Grimes /* 2315df8bae1dSRodney W. Grimes * If new data are received on a connection after the 2316df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 2317df8bae1dSRodney W. Grimes */ 2318df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 2319fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 232007dacf03SAndre Oppermann if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 232107dacf03SAndre Oppermann log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data " 232207dacf03SAndre Oppermann "after socket was closed, " 232307dacf03SAndre Oppermann "sending RST and removing tcpcb\n", 2324e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 2325773673c1SAndre Oppermann free(s, M_TCPLOG); 2326773673c1SAndre Oppermann } 2327df8bae1dSRodney W. Grimes tp = tcp_close(tp); 232878b50714SRobert Watson TCPSTAT_INC(tcps_rcvafterclose); 2329a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2330df8bae1dSRodney W. Grimes goto dropwithreset; 2331df8bae1dSRodney W. Grimes } 2332df8bae1dSRodney W. Grimes 2333df8bae1dSRodney W. Grimes /* 2334df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 2335df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 2336df8bae1dSRodney W. Grimes */ 2337fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 2338df8bae1dSRodney W. Grimes if (todrop > 0) { 233978b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 2340fb59c426SYoshinobu Inoue if (todrop >= tlen) { 234178b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 2342df8bae1dSRodney W. Grimes /* 2343df8bae1dSRodney W. Grimes * If window is closed can only take segments at 2344df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 2345df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 2346df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 2347df8bae1dSRodney W. Grimes * and ack. 2348df8bae1dSRodney W. Grimes */ 2349fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2350df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 235178b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinprobe); 2352df8bae1dSRodney W. Grimes } else 2353df8bae1dSRodney W. Grimes goto dropafterack; 2354df8bae1dSRodney W. Grimes } else 235578b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2356df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2357fb59c426SYoshinobu Inoue tlen -= todrop; 2358fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 2359df8bae1dSRodney W. Grimes } 2360df8bae1dSRodney W. Grimes 2361df8bae1dSRodney W. Grimes /* 2362df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 2363df8bae1dSRodney W. Grimes * record its timestamp. 2364cf09195bSPaul Saab * NOTE: 2365cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 2366a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2367cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 2368cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 2369cf09195bSPaul Saab * predicated on the sequence space of this segment. 2370cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 2371cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 2372cf09195bSPaul Saab * instead of RFC1323's 2373cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 2374cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 2375cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 2376cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 2377cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 2378df8bae1dSRodney W. Grimes */ 2379be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 2380cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2381cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 2382cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 2383d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 2384a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 2385df8bae1dSRodney W. Grimes } 2386df8bae1dSRodney W. Grimes 2387df8bae1dSRodney W. Grimes /* 2388a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 2389a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 2390a0292f23SGarrett Wollman * later processing; else drop segment and return. 2391a0292f23SGarrett Wollman */ 2392fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 2393a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 2394281a0fd4SPatrick Kelsey (tp->t_flags & TF_NEEDSYN)) { 2395281a0fd4SPatrick Kelsey if (tp->t_state == TCPS_SYN_RECEIVED && 239668bd7ed1SJonathan T. Looney IS_FASTOPEN(tp->t_flags)) { 2397281a0fd4SPatrick Kelsey tp->snd_wnd = tiwin; 2398281a0fd4SPatrick Kelsey cc_conn_init(tp); 2399281a0fd4SPatrick Kelsey } 2400a0292f23SGarrett Wollman goto step6; 2401281a0fd4SPatrick Kelsey } else if (tp->t_flags & TF_ACKNOW) 24025e1aa279SDavid Malone goto dropafterack; 2403a0292f23SGarrett Wollman else 2404a0292f23SGarrett Wollman goto drop; 2405a0292f23SGarrett Wollman } 2406df8bae1dSRodney W. Grimes 2407df8bae1dSRodney W. Grimes /* 2408df8bae1dSRodney W. Grimes * Ack processing. 2409df8bae1dSRodney W. Grimes */ 2410df8bae1dSRodney W. Grimes switch (tp->t_state) { 2411df8bae1dSRodney W. Grimes /* 2412764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 2413764d8cefSBill Fenner * ESTABLISHED state and continue processing. 2414764d8cefSBill Fenner * The ACK was checked above. 2415df8bae1dSRodney W. Grimes */ 2416df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2417a0292f23SGarrett Wollman 241878b50714SRobert Watson TCPSTAT_INC(tcps_connects); 2419df8bae1dSRodney W. Grimes soisconnected(so); 2420df8bae1dSRodney W. Grimes /* Do window scaling? */ 2421df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2422df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2423df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 2424df8bae1dSRodney W. Grimes } 2425116ef4d6SMichael Tuexen tp->snd_wnd = tiwin; 2426a0292f23SGarrett Wollman /* 2427a0292f23SGarrett Wollman * Make transitions: 2428a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 2429a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 2430a0292f23SGarrett Wollman */ 24319b8b58e0SJonathan Lemon tp->t_starttime = ticks; 243218a75309SPatrick Kelsey if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) { 2433281a0fd4SPatrick Kelsey tcp_fastopen_decrement_counter(tp->t_tfo_pending); 2434281a0fd4SPatrick Kelsey tp->t_tfo_pending = NULL; 2435281a0fd4SPatrick Kelsey } 24368db239dcSMichael Tuexen if (tp->t_flags & TF_NEEDFIN) { 24378db239dcSMichael Tuexen tcp_state_change(tp, TCPS_FIN_WAIT_1); 24388db239dcSMichael Tuexen tp->t_flags &= ~TF_NEEDFIN; 24398db239dcSMichael Tuexen } else { 24408db239dcSMichael Tuexen tcp_state_change(tp, TCPS_ESTABLISHED); 24418db239dcSMichael Tuexen TCP_PROBE5(accept__established, NULL, tp, 24428db239dcSMichael Tuexen m, tp, th); 2443281a0fd4SPatrick Kelsey /* 2444281a0fd4SPatrick Kelsey * TFO connections call cc_conn_init() during SYN 2445281a0fd4SPatrick Kelsey * processing. Calling it again here for such 2446281a0fd4SPatrick Kelsey * connections is not harmless as it would undo the 2447281a0fd4SPatrick Kelsey * snd_cwnd reduction that occurs when a TFO SYN|ACK 2448281a0fd4SPatrick Kelsey * is retransmitted. 2449281a0fd4SPatrick Kelsey */ 245068bd7ed1SJonathan T. Looney if (!IS_FASTOPEN(tp->t_flags)) 2451dbc42409SLawrence Stewart cc_conn_init(tp); 24529077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 24537ff19458SPaul Traina } 2454a0292f23SGarrett Wollman /* 2455b2ade6b1SRichard Scheffenegger * Account for the ACK of our SYN prior to 2456b2ade6b1SRichard Scheffenegger * regular ACK processing below, except for 2457b2ade6b1SRichard Scheffenegger * simultaneous SYN, which is handled later. 2458b2ade6b1SRichard Scheffenegger */ 2459b2ade6b1SRichard Scheffenegger if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN)) 2460b2ade6b1SRichard Scheffenegger incforsyn = 1; 2461b2ade6b1SRichard Scheffenegger /* 2462a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 2463a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 2464a0292f23SGarrett Wollman */ 2465032bf749SRichard Scheffenegger if (tlen == 0 && (thflags & TH_FIN) == 0) { 2466c28440dbSRandall Stewart (void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0, 2467a0292f23SGarrett Wollman (struct mbuf *)0); 2468032bf749SRichard Scheffenegger tcp_handle_wakeup(tp, so); 2469032bf749SRichard Scheffenegger } 247060ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq - 1; 247193b0017fSPhilippe Charnier /* FALLTHROUGH */ 2472df8bae1dSRodney W. Grimes 2473df8bae1dSRodney W. Grimes /* 2474df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2475df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 2476fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 2477fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 2478df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 2479df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 2480df8bae1dSRodney W. Grimes */ 2481df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2482df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2483df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2484df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 2485df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2486df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 24875a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 248878b50714SRobert Watson TCPSTAT_INC(tcps_rcvacktoomuch); 24895a53ca16SPaul Saab goto dropafterack; 24905a53ca16SPaul Saab } 24913529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2492fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 24930471a8c7SRichard Scheffenegger !TAILQ_EMPTY(&tp->snd_holes))) { 24940471a8c7SRichard Scheffenegger if (((sack_changed = tcp_sack_doack(tp, &to, th->th_ack)) != 0) && 24950471a8c7SRichard Scheffenegger (tp->t_flags & TF_LRD)) { 24960471a8c7SRichard Scheffenegger tcp_sack_lost_retransmission(tp, th); 24970471a8c7SRichard Scheffenegger } 24980471a8c7SRichard Scheffenegger } else 249912eeb81fSHiren Panchasara /* 250012eeb81fSHiren Panchasara * Reset the value so that previous (valid) value 250112eeb81fSHiren Panchasara * from the last ack with SACK doesn't get used. 250212eeb81fSHiren Panchasara */ 250312eeb81fSHiren Panchasara tp->sackhint.sacked_bytes = 0; 250439bc9de5SLawrence Stewart 2505bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 250639bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 250739bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 2508bd79708dSJonathan T. Looney #endif 250939bc9de5SLawrence Stewart 2510fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 25110c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 2512021eaf79SHiren Panchasara if (tlen == 0 && 2513021eaf79SHiren Panchasara (tiwin == tp->snd_wnd || 2514021eaf79SHiren Panchasara (tp->t_flags & TF_SACK_PERMIT))) { 251562b90589SPeter Wemm /* 251662b90589SPeter Wemm * If this is the first time we've seen a 251762b90589SPeter Wemm * FIN from the remote, this is not a 251862b90589SPeter Wemm * duplicate and it needs to be processed 251962b90589SPeter Wemm * normally. This happens during a 252062b90589SPeter Wemm * simultaneous close. 252162b90589SPeter Wemm */ 252262b90589SPeter Wemm if ((thflags & TH_FIN) && 252362b90589SPeter Wemm (TCPS_HAVERCVDFIN(tp->t_state) == 0)) { 252462b90589SPeter Wemm tp->t_dupacks = 0; 252562b90589SPeter Wemm break; 252662b90589SPeter Wemm } 252778b50714SRobert Watson TCPSTAT_INC(tcps_rcvdupack); 2528df8bae1dSRodney W. Grimes /* 2529df8bae1dSRodney W. Grimes * If we have outstanding data (other than 2530df8bae1dSRodney W. Grimes * a window probe), this is a completely 2531df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 25325f30ec9bSEitan Adler * change and FIN isn't set), 25335f30ec9bSEitan Adler * the ack is the biggest we've 2534df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 2535a4641f4eSPedro F. Giffuni * threshold of them, assume a packet 2536df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 2537df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 2538df8bae1dSRodney W. Grimes * window so we send only this one 2539df8bae1dSRodney W. Grimes * packet. 2540df8bae1dSRodney W. Grimes * 2541df8bae1dSRodney W. Grimes * We know we're losing at the current 2542df8bae1dSRodney W. Grimes * window size so do congestion avoidance 2543df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 2544df8bae1dSRodney W. Grimes * and pull our congestion window back to 2545df8bae1dSRodney W. Grimes * the new ssthresh). 2546df8bae1dSRodney W. Grimes * 2547df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 2548df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 2549df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 2550df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 2551df8bae1dSRodney W. Grimes * network. 2552f2512ba1SRui Paulo * 2553f2512ba1SRui Paulo * When using TCP ECN, notify the peer that 2554f2512ba1SRui Paulo * we reduced the cwnd. 2555df8bae1dSRodney W. Grimes */ 2556021eaf79SHiren Panchasara /* 2557021eaf79SHiren Panchasara * Following 2 kinds of acks should not affect 2558021eaf79SHiren Panchasara * dupack counting: 2559021eaf79SHiren Panchasara * 1) Old acks 2560021eaf79SHiren Panchasara * 2) Acks with SACK but without any new SACK 2561021eaf79SHiren Panchasara * information in them. These could result from 2562021eaf79SHiren Panchasara * any anomaly in the network like a switch 2563021eaf79SHiren Panchasara * duplicating packets or a possible DoS attack. 2564021eaf79SHiren Panchasara */ 2565021eaf79SHiren Panchasara if (th->th_ack != tp->snd_una || 2566021eaf79SHiren Panchasara ((tp->t_flags & TF_SACK_PERMIT) && 25670533fab8SRichard Scheffenegger (to.to_flags & TOF_SACK) && 2568021eaf79SHiren Panchasara !sack_changed)) 2569021eaf79SHiren Panchasara break; 2570021eaf79SHiren Panchasara else if (!tcp_timer_active(tp, TT_REXMT)) 2571df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2572cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 2573dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 25744b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 25754b7b743cSLawrence Stewart CC_DUPACK); 25760e1d7c25SRichard Scheffenegger if (V_tcp_do_prr && 257774d7fc87SRichard Scheffenegger IN_FASTRECOVERY(tp->t_flags)) { 257890cca08eSRichard Scheffenegger tcp_do_prr_ack(tp, th, &to); 25790e1d7c25SRichard Scheffenegger } else if ((tp->t_flags & TF_SACK_PERMIT) && 25800533fab8SRichard Scheffenegger (to.to_flags & TOF_SACK) && 2581dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2582808f11b7SPaul Saab int awnd; 258325e6f9edSPaul Saab 258425e6f9edSPaul Saab /* 258525e6f9edSPaul Saab * Compute the amount of data in flight first. 25864c3972f0SHiren Panchasara * We can inject new data into the pipe iff 258725e6f9edSPaul Saab * we have less than 1/2 the original window's 258825e6f9edSPaul Saab * worth of data in flight. 258925e6f9edSPaul Saab */ 2590d1de2b05SRichard Scheffenegger if (V_tcp_do_newsack) 259112eeb81fSHiren Panchasara awnd = tcp_compute_pipe(tp); 259212eeb81fSHiren Panchasara else 2593808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 25940077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 259512eeb81fSHiren Panchasara 2596808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 25970c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 259825e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 259925e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 260025e6f9edSPaul Saab } 260125e6f9edSPaul Saab } else 26020c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 260340fa3e40SGleb Smirnoff (void) tcp_output(tp); 260446f58482SJonathan Lemon goto drop; 26053c40e1d5SRichard Scheffenegger } else if (tp->t_dupacks == tcprexmtthresh || 26063c40e1d5SRichard Scheffenegger (tp->t_flags & TF_SACK_PERMIT && 2607d1de2b05SRichard Scheffenegger V_tcp_do_newsack && 26083c40e1d5SRichard Scheffenegger tp->sackhint.sacked_bytes > 26093c40e1d5SRichard Scheffenegger (tcprexmtthresh - 1) * maxseg)) { 26103c40e1d5SRichard Scheffenegger enter_recovery: 26113c40e1d5SRichard Scheffenegger /* 26123c40e1d5SRichard Scheffenegger * Above is the RFC6675 trigger condition of 26133c40e1d5SRichard Scheffenegger * more than (dupthresh-1)*maxseg sacked data. 26143c40e1d5SRichard Scheffenegger * If the count of holes in the 26153c40e1d5SRichard Scheffenegger * scoreboard is >= dupthresh, we could 26163c40e1d5SRichard Scheffenegger * also enter loss recovery, but don't 26173c40e1d5SRichard Scheffenegger * have that value readily available. 26183c40e1d5SRichard Scheffenegger */ 26193c40e1d5SRichard Scheffenegger tp->t_dupacks = tcprexmtthresh; 2620cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 2621a0445c2eSJayanth Vijayaraghavan 2622a0445c2eSJayanth Vijayaraghavan /* 26230e1d7c25SRichard Scheffenegger * If we're doing sack, or prr, check 26240e1d7c25SRichard Scheffenegger * to see if we're already in sack 2625a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 2626a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 2627a0445c2eSJayanth Vijayaraghavan * recovery. 2628a0445c2eSJayanth Vijayaraghavan */ 26290e1d7c25SRichard Scheffenegger if (V_tcp_do_prr || 26300e1d7c25SRichard Scheffenegger (tp->t_flags & TF_SACK_PERMIT)) { 2631dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2632a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 2633a0445c2eSJayanth Vijayaraghavan break; 2634a0445c2eSJayanth Vijayaraghavan } 2635dbc42409SLawrence Stewart } else { 2636a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 26379d11646dSJeffrey Hsu tp->snd_recover)) { 2638cb942153SJeffrey Hsu tp->t_dupacks = 0; 2639cb942153SJeffrey Hsu break; 264046f58482SJonathan Lemon } 2641a0445c2eSJayanth Vijayaraghavan } 2642dbc42409SLawrence Stewart /* Congestion signal before ack. */ 2643dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_NDUPACK); 26444b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 26454b7b743cSLawrence Stewart CC_DUPACK); 2646b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 26479b8b58e0SJonathan Lemon tp->t_rtttime = 0; 26480e1d7c25SRichard Scheffenegger if (V_tcp_do_prr) { 26490e1d7c25SRichard Scheffenegger /* 26500e1d7c25SRichard Scheffenegger * snd_ssthresh is already updated by 26510e1d7c25SRichard Scheffenegger * cc_cong_signal. 26520e1d7c25SRichard Scheffenegger */ 265374d7fc87SRichard Scheffenegger if ((tp->t_flags & TF_SACK_PERMIT) && 265474d7fc87SRichard Scheffenegger (to.to_flags & TOF_SACK)) { 2655e9071000SRichard Scheffenegger tp->sackhint.prr_delivered = 2656e9071000SRichard Scheffenegger tp->sackhint.sacked_bytes; 265774d7fc87SRichard Scheffenegger } else { 265874d7fc87SRichard Scheffenegger tp->sackhint.prr_delivered = 265974d7fc87SRichard Scheffenegger imin(tp->snd_max - tp->snd_una, 266074d7fc87SRichard Scheffenegger imin(INT_MAX / 65536, 266174d7fc87SRichard Scheffenegger tp->t_dupacks) * maxseg); 266274d7fc87SRichard Scheffenegger } 2663bc7ee8e5SRichard Scheffenegger tp->sackhint.recover_fs = max(1, 2664bc7ee8e5SRichard Scheffenegger tp->snd_nxt - tp->snd_una); 26650e1d7c25SRichard Scheffenegger } 26660533fab8SRichard Scheffenegger if ((tp->t_flags & TF_SACK_PERMIT) && 26670533fab8SRichard Scheffenegger (to.to_flags & TOF_SACK)) { 266878b50714SRobert Watson TCPSTAT_INC( 266978b50714SRobert Watson tcps_sack_recovery_episode); 2670a3574665SMichael Tuexen tp->snd_recover = tp->snd_nxt; 26710c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 267240fa3e40SGleb Smirnoff (void) tcp_output(tp); 26733c40e1d5SRichard Scheffenegger if (SEQ_GT(th->th_ack, tp->snd_una)) 26743c40e1d5SRichard Scheffenegger goto resume_partialack; 26756d90faf3SPaul Saab goto drop; 26766d90faf3SPaul Saab } 2677fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 26780c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 267940fa3e40SGleb Smirnoff (void) tcp_output(tp); 268048d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 2681302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 2682302ce8d6SAndre Oppermann __func__)); 2683df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 26840c39d38dSGleb Smirnoff maxseg * 268548d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 2686df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 2687df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 2688df8bae1dSRodney W. Grimes goto drop; 2689603724d3SBjoern A. Zeeb } else if (V_tcp_do_rfc3042) { 269062d4443fSHiren Panchasara /* 269162d4443fSHiren Panchasara * Process first and second duplicate 269262d4443fSHiren Panchasara * ACKs. Each indicates a segment 269362d4443fSHiren Panchasara * leaving the network, creating room 269462d4443fSHiren Panchasara * for more. Make sure we can send a 269562d4443fSHiren Panchasara * packet on reception of each duplicate 269662d4443fSHiren Panchasara * ACK by increasing snd_cwnd by one 269762d4443fSHiren Panchasara * segment. Restore the original 269862d4443fSHiren Panchasara * snd_cwnd after packet transmission. 269962d4443fSHiren Panchasara */ 27004b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 27014b7b743cSLawrence Stewart CC_DUPACK); 27023ac12506SJonathan T. Looney uint32_t oldcwnd = tp->snd_cwnd; 270348d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 270448d2549cSJeffrey Hsu u_int sent; 27055628dd08SAndre Oppermann int avail; 270689c02376SJeffrey Hsu 2707582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 2708582a954bSJeffrey Hsu tp->t_dupacks == 2, 2709302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 2710302ce8d6SAndre Oppermann __func__)); 271161a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 271248d2549cSJeffrey Hsu tp->snd_limited = 0; 271361a36e3dSJeffrey Hsu tp->snd_cwnd = 271461a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 271561a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 27160c39d38dSGleb Smirnoff maxseg; 27175628dd08SAndre Oppermann /* 27185628dd08SAndre Oppermann * Only call tcp_output when there 27195628dd08SAndre Oppermann * is new data available to be sent. 27205628dd08SAndre Oppermann * Otherwise we would send pure ACKs. 27215628dd08SAndre Oppermann */ 27225628dd08SAndre Oppermann SOCKBUF_LOCK(&so->so_snd); 2723cfa6009eSGleb Smirnoff avail = sbavail(&so->so_snd) - 27245628dd08SAndre Oppermann (tp->snd_nxt - tp->snd_una); 27255628dd08SAndre Oppermann SOCKBUF_UNLOCK(&so->so_snd); 27265628dd08SAndre Oppermann if (avail > 0) 272740fa3e40SGleb Smirnoff (void) tcp_output(tp); 272848d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 27290c39d38dSGleb Smirnoff if (sent > maxseg) { 273089c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 273189c02376SJeffrey Hsu tp->snd_limited == 0) || 27320c39d38dSGleb Smirnoff (sent == maxseg + 1 && 273389c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 2734302ce8d6SAndre Oppermann ("%s: sent too much", 2735302ce8d6SAndre Oppermann __func__)); 273648d2549cSJeffrey Hsu tp->snd_limited = 2; 273748d2549cSJeffrey Hsu } else if (sent > 0) 273848d2549cSJeffrey Hsu ++tp->snd_limited; 2739582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 2740582a954bSJeffrey Hsu goto drop; 2741df8bae1dSRodney W. Grimes } 2742021eaf79SHiren Panchasara } 2743df8bae1dSRodney W. Grimes break; 2744021eaf79SHiren Panchasara } else { 2745021eaf79SHiren Panchasara /* 2746021eaf79SHiren Panchasara * This ack is advancing the left edge, reset the 2747021eaf79SHiren Panchasara * counter. 2748021eaf79SHiren Panchasara */ 2749021eaf79SHiren Panchasara tp->t_dupacks = 0; 2750021eaf79SHiren Panchasara /* 2751021eaf79SHiren Panchasara * If this ack also has new SACK info, increment the 2752f359d6ebSRichard Scheffenegger * counter as per rfc6675. The variable 2753f359d6ebSRichard Scheffenegger * sack_changed tracks all changes to the SACK 2754f359d6ebSRichard Scheffenegger * scoreboard, including when partial ACKs without 2755f359d6ebSRichard Scheffenegger * SACK options are received, and clear the scoreboard 2756f359d6ebSRichard Scheffenegger * from the left side. Such partial ACKs should not be 2757f359d6ebSRichard Scheffenegger * counted as dupacks here. 2758021eaf79SHiren Panchasara */ 2759f359d6ebSRichard Scheffenegger if ((tp->t_flags & TF_SACK_PERMIT) && 2760f359d6ebSRichard Scheffenegger (to.to_flags & TOF_SACK) && 27613c40e1d5SRichard Scheffenegger sack_changed) { 2762021eaf79SHiren Panchasara tp->t_dupacks++; 27633c40e1d5SRichard Scheffenegger /* limit overhead by setting maxseg last */ 27643c40e1d5SRichard Scheffenegger if (!IN_FASTRECOVERY(tp->t_flags) && 27653c40e1d5SRichard Scheffenegger (tp->sackhint.sacked_bytes > 27663c40e1d5SRichard Scheffenegger ((tcprexmtthresh - 1) * 27673c40e1d5SRichard Scheffenegger (maxseg = tcp_maxseg(tp))))) { 27683c40e1d5SRichard Scheffenegger goto enter_recovery; 27693c40e1d5SRichard Scheffenegger } 27703c40e1d5SRichard Scheffenegger } 2771df8bae1dSRodney W. Grimes } 2772cb942153SJeffrey Hsu 27733c40e1d5SRichard Scheffenegger resume_partialack: 2774302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2775302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 2776cb942153SJeffrey Hsu 2777df8bae1dSRodney W. Grimes /* 2778df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2779df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2780df8bae1dSRodney W. Grimes */ 2781dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2782cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 27833529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 2784eb3a59a8SRichard Scheffenegger if (V_tcp_do_prr && to.to_flags & TOF_SACK) { 2785eb3a59a8SRichard Scheffenegger tcp_timer_activate(tp, TT_REXMT, 0); 2786eb3a59a8SRichard Scheffenegger tp->t_rtttime = 0; 278790cca08eSRichard Scheffenegger tcp_do_prr_ack(tp, th, &to); 2788eb3a59a8SRichard Scheffenegger tp->t_flags |= TF_ACKNOW; 2789eb3a59a8SRichard Scheffenegger (void) tcp_output(tp); 2790eb3a59a8SRichard Scheffenegger } else 27916d90faf3SPaul Saab tcp_sack_partialack(tp, th); 27926d90faf3SPaul Saab else 2793c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2794dbc42409SLawrence Stewart } else 2795dbc42409SLawrence Stewart cc_post_recovery(tp, th); 2796b9f803b7SRichard Scheffenegger } else if (IN_CONGRECOVERY(tp->t_flags)) { 2797b9f803b7SRichard Scheffenegger if (SEQ_LT(th->th_ack, tp->snd_recover)) { 2798b9f803b7SRichard Scheffenegger if (V_tcp_do_prr) { 2799b9f803b7SRichard Scheffenegger tp->sackhint.delivered_data = BYTES_THIS_ACK(tp, th); 2800b9f803b7SRichard Scheffenegger tp->snd_fack = th->th_ack; 280190cca08eSRichard Scheffenegger tcp_do_prr_ack(tp, th, &to); 2802b9f803b7SRichard Scheffenegger (void) tcp_output(tp); 2803b9f803b7SRichard Scheffenegger } 2804b9f803b7SRichard Scheffenegger } else 2805b9f803b7SRichard Scheffenegger cc_post_recovery(tp, th); 280646f58482SJonathan Lemon } 2807a0292f23SGarrett Wollman /* 2808a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2809a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2810a0292f23SGarrett Wollman */ 2811a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2812a0292f23SGarrett Wollman /* 2813a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2814a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 281507e43e10SAndras Olah * synchronized). Go to non-starred state, 281607e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 281707e43e10SAndras Olah * we can do window scaling. 2818a0292f23SGarrett Wollman */ 2819a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2820a0292f23SGarrett Wollman tp->snd_una++; 282107e43e10SAndras Olah /* Do window scaling? */ 282207e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 282307e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 282407e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2825464fcfbcSAndre Oppermann /* Send window already scaled. */ 282607e43e10SAndras Olah } 2827a0292f23SGarrett Wollman } 2828a0292f23SGarrett Wollman 2829a0292f23SGarrett Wollman process_ACK: 28308501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 28317cfc6904SRobert Watson 2832b2ade6b1SRichard Scheffenegger /* 2833b2ade6b1SRichard Scheffenegger * Adjust for the SYN bit in sequence space, 2834b2ade6b1SRichard Scheffenegger * but don't account for it in cwnd calculations. 2835b2ade6b1SRichard Scheffenegger * This is for the SYN_RECEIVED, non-simultaneous 2836b2ade6b1SRichard Scheffenegger * SYN case. SYN_SENT and simultaneous SYN are 2837b2ade6b1SRichard Scheffenegger * treated elsewhere. 2838b2ade6b1SRichard Scheffenegger */ 2839b2ade6b1SRichard Scheffenegger if (incforsyn) 2840b2ade6b1SRichard Scheffenegger tp->snd_una++; 2841dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 2842b8c2cd15SJonathan T. Looney KASSERT(acked >= 0, ("%s: acked unexepectedly negative " 2843b8c2cd15SJonathan T. Looney "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__, 2844b8c2cd15SJonathan T. Looney tp->snd_una, th->th_ack, tp, m)); 28454b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvackpack, nsegs); 284678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 2847df8bae1dSRodney W. Grimes 2848df8bae1dSRodney W. Grimes /* 28499b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 28509b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 28519b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 28529b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 28539b8b58e0SJonathan Lemon * we left off. 28549b8b58e0SJonathan Lemon */ 285510d20c84SMatt Macy if (tp->t_rxtshift == 1 && 285610d20c84SMatt Macy tp->t_flags & TF_PREVVALID && 28574531b345SRichard Scheffenegger tp->t_badrxtwin != 0 && 28584531b345SRichard Scheffenegger to.to_flags & TOF_TS && 28594531b345SRichard Scheffenegger to.to_tsecr != 0 && 28604531b345SRichard Scheffenegger TSTMP_LT(to.to_tsecr, tp->t_badrxtwin)) 2861dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 28629b8b58e0SJonathan Lemon 28639b8b58e0SJonathan Lemon /* 2864df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2865df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2866df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2867df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2868df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2869df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2870df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2871fa55172bSMatthew Dillon * 2872fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2873fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2874fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2875fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2876df8bae1dSRodney W. Grimes */ 2877d8951c8aSBjoern A. Zeeb if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) { 28783ac12506SJonathan T. Looney uint32_t t; 2879d8951c8aSBjoern A. Zeeb 2880d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 2881d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 2882d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 2883d8951c8aSBjoern A. Zeeb tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1); 2884fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2885eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2886eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 28879b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2888fa55172bSMatthew Dillon } 2889df8bae1dSRodney W. Grimes 2890df8bae1dSRodney W. Grimes /* 2891df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2892df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2893df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2894df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2895df8bae1dSRodney W. Grimes */ 2896fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 2897b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2898df8bae1dSRodney W. Grimes needoutput = 1; 2899b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2900b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2901a0292f23SGarrett Wollman 2902a0292f23SGarrett Wollman /* 2903a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2904a0292f23SGarrett Wollman * skip rest of ACK processing. 2905a0292f23SGarrett Wollman */ 2906a0292f23SGarrett Wollman if (acked == 0) 2907a0292f23SGarrett Wollman goto step6; 2908a0292f23SGarrett Wollman 2909df8bae1dSRodney W. Grimes /* 2910dbc42409SLawrence Stewart * Let the congestion control algorithm update congestion 2911dbc42409SLawrence Stewart * control related information. This typically means increasing 2912dbc42409SLawrence Stewart * the congestion window. 2913df8bae1dSRodney W. Grimes */ 29144b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, CC_ACK); 2915dbc42409SLawrence Stewart 29165905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2917cfa6009eSGleb Smirnoff if (acked > sbavail(&so->so_snd)) { 2918b8c2cd15SJonathan T. Looney if (tp->snd_wnd >= sbavail(&so->so_snd)) 2919cfa6009eSGleb Smirnoff tp->snd_wnd -= sbavail(&so->so_snd); 2920b8c2cd15SJonathan T. Looney else 2921b8c2cd15SJonathan T. Looney tp->snd_wnd = 0; 2922c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, 2923cfa6009eSGleb Smirnoff (int)sbavail(&so->so_snd)); 2924df8bae1dSRodney W. Grimes ourfinisacked = 1; 2925df8bae1dSRodney W. Grimes } else { 2926c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, acked); 29273ac12506SJonathan T. Looney if (tp->snd_wnd >= (uint32_t) acked) 292860ee3bb2SAndre Oppermann tp->snd_wnd -= acked; 2929b8c2cd15SJonathan T. Looney else 2930b8c2cd15SJonathan T. Looney tp->snd_wnd = 0; 2931df8bae1dSRodney W. Grimes ourfinisacked = 0; 2932df8bae1dSRodney W. Grimes } 2933032bf749SRichard Scheffenegger /* NB: sowwakeup_locked() does an implicit unlock. */ 2934032bf749SRichard Scheffenegger sowwakeup_locked(so); 2935c11a15bfSGleb Smirnoff m_freem(mfree); 2936e8949f74SAndre Oppermann /* Detect una wraparound. */ 2937dbc42409SLawrence Stewart if (!IN_RECOVERY(tp->t_flags) && 29389d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 29399d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 29409d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 2941dbc42409SLawrence Stewart /* XXXLAS: Can this be moved up into cc_post_recovery? */ 2942dbc42409SLawrence Stewart if (IN_RECOVERY(tp->t_flags) && 294324cb0f22SLawrence Stewart SEQ_GEQ(th->th_ack, tp->snd_recover)) { 2944dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 294524cb0f22SLawrence Stewart } 2946fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 29473529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 29486d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 29496d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 29506d90faf3SPaul Saab } 2951df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2952df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2953df8bae1dSRodney W. Grimes 2954df8bae1dSRodney W. Grimes switch (tp->t_state) { 2955df8bae1dSRodney W. Grimes /* 2956df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2957df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2958df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2959df8bae1dSRodney W. Grimes */ 2960df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2961df8bae1dSRodney W. Grimes if (ourfinisacked) { 2962df8bae1dSRodney W. Grimes /* 2963df8bae1dSRodney W. Grimes * If we can't receive any more 2964df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2965df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2966df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2967df8bae1dSRodney W. Grimes * we'll hang forever. 2968e8949f74SAndre Oppermann * 2969e8949f74SAndre Oppermann * XXXjl: 2970340c35deSJonathan Lemon * we should release the tp also, and use a 2971340c35deSJonathan Lemon * compressed state. 2972340c35deSJonathan Lemon */ 2973c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 297403e49181SSeigo Tanimura soisdisconnected(so); 29759077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 29769077f387SGleb Smirnoff (tcp_fast_finwait2_recycle ? 29779077f387SGleb Smirnoff tcp_finwait2_timeout : 29789077f387SGleb Smirnoff TP_MAXIDLE(tp))); 29794cc20ab1SSeigo Tanimura } 298057f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_2); 2981df8bae1dSRodney W. Grimes } 2982df8bae1dSRodney W. Grimes break; 2983df8bae1dSRodney W. Grimes 2984df8bae1dSRodney W. Grimes /* 2985df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2986df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2987df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2988df8bae1dSRodney W. Grimes * the segment. 2989df8bae1dSRodney W. Grimes */ 2990df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2991df8bae1dSRodney W. Grimes if (ourfinisacked) { 299211a20fb8SJeffrey Hsu tcp_twstart(tp); 2993340c35deSJonathan Lemon m_freem(m); 2994679d9708SAndre Oppermann return; 2995df8bae1dSRodney W. Grimes } 2996df8bae1dSRodney W. Grimes break; 2997df8bae1dSRodney W. Grimes 2998df8bae1dSRodney W. Grimes /* 2999df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 3000df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 3001df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 3002df8bae1dSRodney W. Grimes * enter the closed state and return. 3003df8bae1dSRodney W. Grimes */ 3004df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 3005df8bae1dSRodney W. Grimes if (ourfinisacked) { 3006df8bae1dSRodney W. Grimes tp = tcp_close(tp); 3007df8bae1dSRodney W. Grimes goto drop; 3008df8bae1dSRodney W. Grimes } 3009df8bae1dSRodney W. Grimes break; 3010df8bae1dSRodney W. Grimes } 3011df8bae1dSRodney W. Grimes } 3012df8bae1dSRodney W. Grimes 3013df8bae1dSRodney W. Grimes step6: 30148501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 30157cfc6904SRobert Watson 3016df8bae1dSRodney W. Grimes /* 301760ee3bb2SAndre Oppermann * Update window information. 301860ee3bb2SAndre Oppermann * Don't look at window if no ACK: TAC's send garbage on first SYN. 3019df8bae1dSRodney W. Grimes */ 302060ee3bb2SAndre Oppermann if ((thflags & TH_ACK) && 302160ee3bb2SAndre Oppermann (SEQ_LT(tp->snd_wl1, th->th_seq) || 302260ee3bb2SAndre Oppermann (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 302360ee3bb2SAndre Oppermann (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 302460ee3bb2SAndre Oppermann /* keep track of pure window updates */ 302560ee3bb2SAndre Oppermann if (tlen == 0 && 302660ee3bb2SAndre Oppermann tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 302778b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinupd); 3028df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 302960ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 303060ee3bb2SAndre Oppermann tp->snd_wl2 = th->th_ack; 3031df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 3032df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 303360ee3bb2SAndre Oppermann needoutput = 1; 3034df8bae1dSRodney W. Grimes } 3035df8bae1dSRodney W. Grimes 3036df8bae1dSRodney W. Grimes /* 3037df8bae1dSRodney W. Grimes * Process segments with URG. 3038df8bae1dSRodney W. Grimes */ 3039fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 3040df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 3041df8bae1dSRodney W. Grimes /* 3042df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 3043df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 3044df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 3045df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 3046df8bae1dSRodney W. Grimes */ 304718ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3048cfa6009eSGleb Smirnoff if (th->th_urp + sbavail(&so->so_rcv) > sb_max) { 3049fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 3050fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 305118ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 3052df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 3053df8bae1dSRodney W. Grimes } 3054df8bae1dSRodney W. Grimes /* 3055df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 3056df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 3057df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 3058df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 3059df8bae1dSRodney W. Grimes * In these states we ignore the URG. 3060df8bae1dSRodney W. Grimes * 3061df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 3062df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 3063df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 3064df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 3065df8bae1dSRodney W. Grimes * of data past the urgent section as the original 3066df8bae1dSRodney W. Grimes * spec states (in one of two places). 3067df8bae1dSRodney W. Grimes */ 3068fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 3069fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 3070cfa6009eSGleb Smirnoff so->so_oobmark = sbavail(&so->so_rcv) + 3071df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 3072927c5ceaSRobert Watson if (so->so_oobmark == 0) 3073c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 3074df8bae1dSRodney W. Grimes sohasoutofband(so); 3075df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 3076df8bae1dSRodney W. Grimes } 307718ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 3078df8bae1dSRodney W. Grimes /* 3079df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 3080df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 3081df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 3082df8bae1dSRodney W. Grimes * data may creep in... ick. 3083df8bae1dSRodney W. Grimes */ 30843ac12506SJonathan T. Looney if (th->th_urp <= (uint32_t)tlen && 308530613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 308630613f56SJeffrey Hsu /* hdr drop is delayed */ 308730613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 308830613f56SJeffrey Hsu } 3089c068736aSJeffrey Hsu } else { 3090df8bae1dSRodney W. Grimes /* 3091df8bae1dSRodney W. Grimes * If no out of band data is expected, 3092df8bae1dSRodney W. Grimes * pull receive urgent pointer along 3093df8bae1dSRodney W. Grimes * with the receive window. 3094df8bae1dSRodney W. Grimes */ 3095df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 3096df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 3097c068736aSJeffrey Hsu } 3098df8bae1dSRodney W. Grimes dodata: /* XXX */ 30998501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 31007cfc6904SRobert Watson 3101df8bae1dSRodney W. Grimes /* 3102df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 3103df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 3104df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 3105df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 3106df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 3107df8bae1dSRodney W. Grimes * connection then we just ignore the text. 3108df8bae1dSRodney W. Grimes */ 3109281a0fd4SPatrick Kelsey tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) && 311068bd7ed1SJonathan T. Looney IS_FASTOPEN(tp->t_flags)); 3111f1ea4e41SRandall Stewart if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) && 3112df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 311369e03620SPaul Saab tcp_seq save_start = th->th_seq; 31145acfd95cSMichael Tuexen tcp_seq save_rnxt = tp->rcv_nxt; 31155acfd95cSMichael Tuexen int save_tlen = tlen; 3116fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 3117e4b64281SJesper Skriver /* 3118c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 3119c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 3120c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 3121c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 3122c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 3123c068736aSJeffrey Hsu * and removal from the queue and repetition of various 3124c068736aSJeffrey Hsu * conversions. 3125c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 3126c068736aSJeffrey Hsu * immediately when segments are out of order (so 3127c068736aSJeffrey Hsu * fast retransmit can work). 3128e4b64281SJesper Skriver */ 31294741bfcbSPatrick Kelsey if (th->th_seq == tp->rcv_nxt && 3130c28440dbSRandall Stewart SEGQ_EMPTY(tp) && 3131281a0fd4SPatrick Kelsey (TCPS_HAVEESTABLISHED(tp->t_state) || 3132281a0fd4SPatrick Kelsey tfo_syn)) { 3133281a0fd4SPatrick Kelsey if (DELAY_ACK(tp, tlen) || tfo_syn) 31343bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 3135e4b64281SJesper Skriver else 3136e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 3137e4b64281SJesper Skriver tp->rcv_nxt += tlen; 3138e854dd38SRandall Stewart if (tlen && 3139e854dd38SRandall Stewart ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 3140e854dd38SRandall Stewart (tp->t_fbyte_in == 0)) { 3141e854dd38SRandall Stewart tp->t_fbyte_in = ticks; 3142e854dd38SRandall Stewart if (tp->t_fbyte_in == 0) 3143e854dd38SRandall Stewart tp->t_fbyte_in = 1; 3144e854dd38SRandall Stewart if (tp->t_fbyte_out && tp->t_fbyte_in) 3145e854dd38SRandall Stewart tp->t_flags2 |= TF2_FBYTES_COMPLETE; 3146e854dd38SRandall Stewart } 31471ebf4607SRichard Scheffenegger thflags = tcp_get_flags(th) & TH_FIN; 314878b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 314978b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 31501e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3151c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 3152c1c36a2cSMike Silbersack m_freem(m); 3153c1c36a2cSMike Silbersack else 3154651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 31554d0770f1SRichard Scheffenegger tp->t_flags |= TF_WAKESOR; 3156e4b64281SJesper Skriver } else { 3157e8949f74SAndre Oppermann /* 3158e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 3159e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 3160e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 3161e8949f74SAndre Oppermann * when trimming from the head. 3162e8949f74SAndre Oppermann */ 31635acfd95cSMichael Tuexen tcp_seq temp = save_start; 31644747500dSRandall Stewart 31655acfd95cSMichael Tuexen thflags = tcp_reass(tp, th, &temp, &tlen, m); 3166e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 3167e4b64281SJesper Skriver } 316840f41eceSMichael Tuexen if ((tp->t_flags & TF_SACK_PERMIT) && 316940f41eceSMichael Tuexen (save_tlen > 0) && 317040f41eceSMichael Tuexen TCPS_HAVEESTABLISHED(tp->t_state)) { 31716d261981SMichael Tuexen if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) { 3172b5a154d8SMichael Tuexen /* 3173b5a154d8SMichael Tuexen * DSACK actually handled in the fastpath 3174b5a154d8SMichael Tuexen * above. 3175b5a154d8SMichael Tuexen */ 3176ecc5b1d1SMichael Tuexen tcp_update_sack_list(tp, save_start, 3177ecc5b1d1SMichael Tuexen save_start + save_tlen); 3178ecc5b1d1SMichael Tuexen } else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) { 3179ecc5b1d1SMichael Tuexen if ((tp->rcv_numsacks >= 1) && 3180ecc5b1d1SMichael Tuexen (tp->sackblks[0].end == save_start)) { 31816d261981SMichael Tuexen /* 31826d261981SMichael Tuexen * Partial overlap, recorded at todrop 31836d261981SMichael Tuexen * above. 31846d261981SMichael Tuexen */ 31856d261981SMichael Tuexen tcp_update_sack_list(tp, 31866d261981SMichael Tuexen tp->sackblks[0].start, 3187ecc5b1d1SMichael Tuexen tp->sackblks[0].end); 3188ecc5b1d1SMichael Tuexen } else { 3189ecc5b1d1SMichael Tuexen tcp_update_dsack_list(tp, save_start, 3190ecc5b1d1SMichael Tuexen save_start + save_tlen); 3191ecc5b1d1SMichael Tuexen } 31926d261981SMichael Tuexen } else if (tlen >= save_tlen) { 3193b5a154d8SMichael Tuexen /* Update of sackblks. */ 3194ecc5b1d1SMichael Tuexen tcp_update_dsack_list(tp, save_start, 3195ecc5b1d1SMichael Tuexen save_start + save_tlen); 3196ecc5b1d1SMichael Tuexen } else if (tlen > 0) { 3197ecc5b1d1SMichael Tuexen tcp_update_dsack_list(tp, save_start, 3198ecc5b1d1SMichael Tuexen save_start + tlen); 31995acfd95cSMichael Tuexen } 32005acfd95cSMichael Tuexen } 3201032bf749SRichard Scheffenegger tcp_handle_wakeup(tp, so); 3202302ce8d6SAndre Oppermann #if 0 3203df8bae1dSRodney W. Grimes /* 3204df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 3205df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 3206df8bae1dSRodney W. Grimes * buffer size. 3207302ce8d6SAndre Oppermann * XXX: Unused. 3208df8bae1dSRodney W. Grimes */ 3209f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) 3210df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 3211f701e30dSJohn Baldwin else 3212f701e30dSJohn Baldwin len = so->so_rcv.sb_hiwat; 3213302ce8d6SAndre Oppermann #endif 3214df8bae1dSRodney W. Grimes } else { 3215df8bae1dSRodney W. Grimes m_freem(m); 3216fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 3217df8bae1dSRodney W. Grimes } 3218df8bae1dSRodney W. Grimes 3219df8bae1dSRodney W. Grimes /* 3220df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 3221df8bae1dSRodney W. Grimes * that the connection is closing. 3222df8bae1dSRodney W. Grimes */ 3223fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 3224df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 32254d0770f1SRichard Scheffenegger /* The socket upcall is handled by socantrcvmore. */ 3226032bf749SRichard Scheffenegger socantrcvmore(so); 3227a0292f23SGarrett Wollman /* 3228a0292f23SGarrett Wollman * If connection is half-synchronized 3229d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 3230a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 3231a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 3232a0292f23SGarrett Wollman * more input can be expected, send ACK now. 3233a0292f23SGarrett Wollman */ 32343bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 32353bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 3236a0292f23SGarrett Wollman else 3237df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 3238df8bae1dSRodney W. Grimes tp->rcv_nxt++; 3239df8bae1dSRodney W. Grimes } 3240df8bae1dSRodney W. Grimes switch (tp->t_state) { 3241df8bae1dSRodney W. Grimes /* 3242df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 3243df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 3244df8bae1dSRodney W. Grimes */ 3245df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 32469b8b58e0SJonathan Lemon tp->t_starttime = ticks; 32479b8b58e0SJonathan Lemon /* FALLTHROUGH */ 3248df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 324957f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSE_WAIT); 3250df8bae1dSRodney W. Grimes break; 3251df8bae1dSRodney W. Grimes 3252df8bae1dSRodney W. Grimes /* 3253df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 3254df8bae1dSRodney W. Grimes * enter the CLOSING state. 3255df8bae1dSRodney W. Grimes */ 3256df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 325757f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSING); 3258df8bae1dSRodney W. Grimes break; 3259df8bae1dSRodney W. Grimes 3260df8bae1dSRodney W. Grimes /* 3261df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 3262df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 3263df8bae1dSRodney W. Grimes * standard timers. 3264df8bae1dSRodney W. Grimes */ 3265df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 3266340c35deSJonathan Lemon tcp_twstart(tp); 3267679d9708SAndre Oppermann return; 3268df8bae1dSRodney W. Grimes } 3269df8bae1dSRodney W. Grimes } 3270610ee2f9SDavid Greenman #ifdef TCPDEBUG 32714cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3272fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 3273fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3274610ee2f9SDavid Greenman #endif 32752b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 3276df8bae1dSRodney W. Grimes 3277df8bae1dSRodney W. Grimes /* 3278df8bae1dSRodney W. Grimes * Return any desired output. 3279df8bae1dSRodney W. Grimes */ 3280df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 328140fa3e40SGleb Smirnoff (void) tcp_output(tp); 32827792ea27SJeffrey Hsu 3283a14c749fSJonathan Lemon check_delack: 32848501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 3285252ca428SRobert Watson 3286a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 32873bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 3288b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 32893bfd6421SJonathan Lemon } 32908501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3291679d9708SAndre Oppermann return; 3292df8bae1dSRodney W. Grimes 3293df8bae1dSRodney W. Grimes dropafterack: 3294df8bae1dSRodney W. Grimes /* 3295df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 3296df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 329780ab7c0eSGarrett Wollman * 329880ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 329980ab7c0eSGarrett Wollman * paths to this code happen after packets containing 330080ab7c0eSGarrett Wollman * RST have been dropped. 330180ab7c0eSGarrett Wollman * 330280ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 330380ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 330480ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 330580ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 330680ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 330780ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 3308df8bae1dSRodney W. Grimes */ 3309fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 3310fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 3311a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 3312a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 3313a57815efSBosko Milekic goto dropwithreset; 3314a57815efSBosko Milekic } 3315a0292f23SGarrett Wollman #ifdef TCPDEBUG 33164cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3317fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3318fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3319a0292f23SGarrett Wollman #endif 33202b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 3321df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 332240fa3e40SGleb Smirnoff (void) tcp_output(tp); 33238501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3324d6915262SRobert Watson m_freem(m); 3325679d9708SAndre Oppermann return; 3326df8bae1dSRodney W. Grimes 3327df8bae1dSRodney W. Grimes dropwithreset: 3328a0ca0871SRobert Watson if (tp != NULL) { 3329302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 33308501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3331dd8ac7f9SRobert Watson } else 3332a0ca0871SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 3333679d9708SAndre Oppermann return; 3334df8bae1dSRodney W. Grimes 3335df8bae1dSRodney W. Grimes drop: 3336df8bae1dSRodney W. Grimes /* 3337df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 3338df8bae1dSRodney W. Grimes */ 3339610ee2f9SDavid Greenman #ifdef TCPDEBUG 33401c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 3341fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3342fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3343a0292f23SGarrett Wollman #endif 33442b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 33454d0770f1SRichard Scheffenegger if (tp != NULL) { 33468501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 33474d0770f1SRichard Scheffenegger } 3348d6915262SRobert Watson m_freem(m); 3349302ce8d6SAndre Oppermann } 3350302ce8d6SAndre Oppermann 3351302ce8d6SAndre Oppermann /* 33520ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 33530ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 33540ca3f933SAndre Oppermann * tp may be NULL. 3355302ce8d6SAndre Oppermann */ 335655bceb1eSRandall Stewart void 3357302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 3358302ce8d6SAndre Oppermann int tlen, int rstreason) 3359302ce8d6SAndre Oppermann { 3360b287c6c7SBjoern A. Zeeb #ifdef INET 3361302ce8d6SAndre Oppermann struct ip *ip; 3362b287c6c7SBjoern A. Zeeb #endif 3363302ce8d6SAndre Oppermann #ifdef INET6 3364302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 3365302ce8d6SAndre Oppermann #endif 33667a3244ccSRobert Watson 33677a3244ccSRobert Watson if (tp != NULL) { 336808d9c920SGleb Smirnoff INP_LOCK_ASSERT(tp->t_inpcb); 33697a3244ccSRobert Watson } 33707a3244ccSRobert Watson 33710ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 33721ebf4607SRichard Scheffenegger if ((tcp_get_flags(th) & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 3373302ce8d6SAndre Oppermann goto drop; 3374302ce8d6SAndre Oppermann #ifdef INET6 3375302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 3376302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 3377302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3378302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3379302ce8d6SAndre Oppermann goto drop; 3380302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 3381b287c6c7SBjoern A. Zeeb } 3382302ce8d6SAndre Oppermann #endif 3383b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3384b287c6c7SBjoern A. Zeeb else 3385b287c6c7SBjoern A. Zeeb #endif 3386b287c6c7SBjoern A. Zeeb #ifdef INET 3387302ce8d6SAndre Oppermann { 3388302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 3389302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3390302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3391302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3392302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3393302ce8d6SAndre Oppermann goto drop; 3394302ce8d6SAndre Oppermann } 3395b287c6c7SBjoern A. Zeeb #endif 3396302ce8d6SAndre Oppermann 3397302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 3398302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 3399302ce8d6SAndre Oppermann goto drop; 3400302ce8d6SAndre Oppermann 3401302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 34021ebf4607SRichard Scheffenegger if (tcp_get_flags(th) & TH_ACK) { 3403302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 3404302ce8d6SAndre Oppermann th->th_ack, TH_RST); 3405302ce8d6SAndre Oppermann } else { 34061ebf4607SRichard Scheffenegger if (tcp_get_flags(th) & TH_SYN) 3407302ce8d6SAndre Oppermann tlen++; 34081ebf4607SRichard Scheffenegger if (tcp_get_flags(th) & TH_FIN) 34094ddd5aadSMichael Tuexen tlen++; 3410302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 3411302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 3412302ce8d6SAndre Oppermann } 3413302ce8d6SAndre Oppermann return; 3414302ce8d6SAndre Oppermann drop: 3415302ce8d6SAndre Oppermann m_freem(m); 3416df8bae1dSRodney W. Grimes } 3417df8bae1dSRodney W. Grimes 3418be2ac88cSJonathan Lemon /* 3419be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 3420be2ac88cSJonathan Lemon */ 342155bceb1eSRandall Stewart void 3422ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 3423df8bae1dSRodney W. Grimes { 3424df8bae1dSRodney W. Grimes int opt, optlen; 3425df8bae1dSRodney W. Grimes 3426be2ac88cSJonathan Lemon to->to_flags = 0; 3427df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 3428df8bae1dSRodney W. Grimes opt = cp[0]; 3429df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 3430df8bae1dSRodney W. Grimes break; 3431df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 3432df8bae1dSRodney W. Grimes optlen = 1; 3433df8bae1dSRodney W. Grimes else { 3434b474779fSJun-ichiro itojun Hagino if (cnt < 2) 3435b474779fSJun-ichiro itojun Hagino break; 3436df8bae1dSRodney W. Grimes optlen = cp[1]; 3437b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 3438df8bae1dSRodney W. Grimes break; 3439df8bae1dSRodney W. Grimes } 3440df8bae1dSRodney W. Grimes switch (opt) { 3441df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 3442df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 3443df8bae1dSRodney W. Grimes continue; 3444f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3445df8bae1dSRodney W. Grimes continue; 3446be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 3447be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 3448be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 3449fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 3450df8bae1dSRodney W. Grimes break; 3451df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 3452df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 3453df8bae1dSRodney W. Grimes continue; 3454f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3455df8bae1dSRodney W. Grimes continue; 3456be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 345702a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 3458df8bae1dSRodney W. Grimes break; 3459df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 3460df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 3461df8bae1dSRodney W. Grimes continue; 3462be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 3463a0292f23SGarrett Wollman bcopy((char *)cp + 2, 3464a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 3465fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 3466a0292f23SGarrett Wollman bcopy((char *)cp + 6, 3467a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 3468fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 3469df8bae1dSRodney W. Grimes break; 34701cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 3471fcf59617SAndrey V. Elsukov /* 3472fcf59617SAndrey V. Elsukov * In order to reply to a host which has set the 3473fcf59617SAndrey V. Elsukov * TCP_SIGNATURE option in its initial SYN, we have 3474fcf59617SAndrey V. Elsukov * to record the fact that the option was observed 3475fcf59617SAndrey V. Elsukov * here for the syncache code to perform the correct 3476fcf59617SAndrey V. Elsukov * response. 3477fcf59617SAndrey V. Elsukov */ 34781cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 34791cfd4b53SBruce M Simpson continue; 3480df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 3481df47e437SAndre Oppermann to->to_signature = cp + 2; 34821cfd4b53SBruce M Simpson break; 34836d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 3484f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 34856d90faf3SPaul Saab continue; 3486f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3487f72167f4SAndre Oppermann continue; 3488603724d3SBjoern A. Zeeb if (!V_tcp_do_sack) 3489f72167f4SAndre Oppermann continue; 3490fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 34916d90faf3SPaul Saab break; 34926d90faf3SPaul Saab case TCPOPT_SACK: 34935a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 34946d90faf3SPaul Saab continue; 3495b728e902SAndre Oppermann if (flags & TO_SYN) 3496b728e902SAndre Oppermann continue; 3497fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 34985a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 34995a53ca16SPaul Saab to->to_sacks = cp + 2; 350078b50714SRobert Watson TCPSTAT_INC(tcps_sack_rcv_blocks); 35016d90faf3SPaul Saab break; 3502281a0fd4SPatrick Kelsey case TCPOPT_FAST_OPEN: 3503c560df6fSPatrick Kelsey /* 3504c560df6fSPatrick Kelsey * Cookie length validation is performed by the 3505c560df6fSPatrick Kelsey * server side cookie checking code or the client 3506c560df6fSPatrick Kelsey * side cookie cache update code. 3507c560df6fSPatrick Kelsey */ 3508281a0fd4SPatrick Kelsey if (!(flags & TO_SYN)) 3509281a0fd4SPatrick Kelsey continue; 3510c560df6fSPatrick Kelsey if (!V_tcp_fastopen_client_enable && 3511c560df6fSPatrick Kelsey !V_tcp_fastopen_server_enable) 3512281a0fd4SPatrick Kelsey continue; 3513281a0fd4SPatrick Kelsey to->to_flags |= TOF_FASTOPEN; 3514281a0fd4SPatrick Kelsey to->to_tfo_len = optlen - 2; 3515281a0fd4SPatrick Kelsey to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL; 3516281a0fd4SPatrick Kelsey break; 3517be2ac88cSJonathan Lemon default: 3518be2ac88cSJonathan Lemon continue; 3519df8bae1dSRodney W. Grimes } 3520df8bae1dSRodney W. Grimes } 3521df8bae1dSRodney W. Grimes } 3522df8bae1dSRodney W. Grimes 3523df8bae1dSRodney W. Grimes /* 3524df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 3525df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 3526df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 3527df8bae1dSRodney W. Grimes * sequencing purposes. 3528df8bae1dSRodney W. Grimes */ 352955bceb1eSRandall Stewart void 3530ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 3531ad3f9ab3SAndre Oppermann int off) 3532df8bae1dSRodney W. Grimes { 3533fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 3534df8bae1dSRodney W. Grimes 3535df8bae1dSRodney W. Grimes while (cnt >= 0) { 3536df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 3537df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 3538df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 3539df8bae1dSRodney W. Grimes 35408501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 35417a3244ccSRobert Watson 3542df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 3543df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 3544df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 3545df8bae1dSRodney W. Grimes m->m_len--; 354669a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 354769a34685SYoshinobu Inoue m->m_pkthdr.len--; 3548df8bae1dSRodney W. Grimes return; 3549df8bae1dSRodney W. Grimes } 3550df8bae1dSRodney W. Grimes cnt -= m->m_len; 3551df8bae1dSRodney W. Grimes m = m->m_next; 35524dfdffe9SAndre Oppermann if (m == NULL) 3553df8bae1dSRodney W. Grimes break; 3554df8bae1dSRodney W. Grimes } 3555df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 3556df8bae1dSRodney W. Grimes } 3557df8bae1dSRodney W. Grimes 3558df8bae1dSRodney W. Grimes /* 3559df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 3560df8bae1dSRodney W. Grimes * and update averages and current timeout. 3561df8bae1dSRodney W. Grimes */ 356255bceb1eSRandall Stewart void 3563ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 3564df8bae1dSRodney W. Grimes { 3565ad3f9ab3SAndre Oppermann int delta; 3566233e8c18SGarrett Wollman 35678501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 35682be3bf22SRobert Watson 356978b50714SRobert Watson TCPSTAT_INC(tcps_rttupdated); 3570233e8c18SGarrett Wollman tp->t_rttupdated++; 3571adc56f5aSEdward Tomasz Napierala #ifdef STATS 3572adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, 3573adc56f5aSEdward Tomasz Napierala imax(0, rtt * 1000 / hz)); 3574adc56f5aSEdward Tomasz Napierala #endif 35755ede40dcSRyan Stone if ((tp->t_srtt != 0) && (tp->t_rxtshift <= TCP_RTT_INVALIDATE)) { 3576233e8c18SGarrett Wollman /* 3577233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 3578233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 3579233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 3580233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3581233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 3582233e8c18SGarrett Wollman */ 3583233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3584233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3585233e8c18SGarrett Wollman 3586233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 3587233e8c18SGarrett Wollman tp->t_srtt = 1; 3588233e8c18SGarrett Wollman 3589233e8c18SGarrett Wollman /* 3590233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 3591233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 3592233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 3593233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 3594233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 3595233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 3596233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3597233e8c18SGarrett Wollman * rfc793's wired-in beta. 3598233e8c18SGarrett Wollman */ 3599233e8c18SGarrett Wollman if (delta < 0) 3600233e8c18SGarrett Wollman delta = -delta; 3601233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3602233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 3603233e8c18SGarrett Wollman tp->t_rttvar = 1; 36041fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 36051fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3606233e8c18SGarrett Wollman } else { 3607233e8c18SGarrett Wollman /* 3608233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 3609233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 3610233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 3611233e8c18SGarrett Wollman */ 3612233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 3613233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 36141fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3615233e8c18SGarrett Wollman } 36169b8b58e0SJonathan Lemon tp->t_rtttime = 0; 3617df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 3618df8bae1dSRodney W. Grimes 3619df8bae1dSRodney W. Grimes /* 3620df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 3621df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 3622df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 3623df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 3624df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 3625df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 3626df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 3627df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 3628df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 3629df8bae1dSRodney W. Grimes */ 3630233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 36319e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3632df8bae1dSRodney W. Grimes 3633df8bae1dSRodney W. Grimes /* 3634df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 3635df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 3636df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 3637df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 3638df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 3639df8bae1dSRodney W. Grimes */ 3640df8bae1dSRodney W. Grimes tp->t_softerror = 0; 3641df8bae1dSRodney W. Grimes } 3642df8bae1dSRodney W. Grimes 3643df8bae1dSRodney W. Grimes /* 3644df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 3645df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 36464faaea55SAndre Oppermann * If none, use an mss that can be handled on the outgoing interface 36474faaea55SAndre Oppermann * without forcing IP to fragment. If no route is found, route has no mtu, 3648df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 3649df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 3650df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 3651df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 3652df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 3653df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 3654df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 3655a0292f23SGarrett Wollman * 36560c39d38dSGleb Smirnoff * NOTE that resulting t_maxseg doesn't include space for TCP options or 36570c39d38dSGleb Smirnoff * IP options, e.g. IPSEC data, since length of this data may vary, and 36580c39d38dSGleb Smirnoff * thus it is calculated for every segment separately in tcp_output(). 3659a0292f23SGarrett Wollman * 366097d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 3661ef341ee1SGleb Smirnoff * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS 3662ef341ee1SGleb Smirnoff * settings are handled in tcp_mssopt(). 3663df8bae1dSRodney W. Grimes */ 3664a0292f23SGarrett Wollman void 3665ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer, 36663c914c54SAndre Oppermann struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap) 3667df8bae1dSRodney W. Grimes { 3668b287c6c7SBjoern A. Zeeb int mss = 0; 36693ac12506SJonathan T. Looney uint32_t maxmtu = 0; 3670c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 367197d8d152SAndre Oppermann struct hc_metrics_lite metrics; 3672fb59c426SYoshinobu Inoue #ifdef INET6 3673c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3674c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 3675c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3676c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 3677c068736aSJeffrey Hsu #else 36789e644c23SMichael Tuexen size_t min_protoh = sizeof(struct tcpiphdr); 3679fb59c426SYoshinobu Inoue #endif 3680df8bae1dSRodney W. Grimes 36818501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 36827a3244ccSRobert Watson 36839e644c23SMichael Tuexen if (tp->t_port) 36849e644c23SMichael Tuexen min_protoh += V_tcp_udp_tunneling_overhead; 3685ef341ee1SGleb Smirnoff if (mtuoffer != -1) { 3686ef341ee1SGleb Smirnoff KASSERT(offer == -1, ("%s: conflict", __func__)); 3687ef341ee1SGleb Smirnoff offer = mtuoffer - min_protoh; 3688ef341ee1SGleb Smirnoff } 3689ef341ee1SGleb Smirnoff 3690e8949f74SAndre Oppermann /* Initialize. */ 369197d8d152SAndre Oppermann #ifdef INET6 369297d8d152SAndre Oppermann if (isipv6) { 36933c914c54SAndre Oppermann maxmtu = tcp_maxmtu6(&inp->inp_inc, cap); 36940c39d38dSGleb Smirnoff tp->t_maxseg = V_tcp_v6mssdflt; 3695b287c6c7SBjoern A. Zeeb } 369697d8d152SAndre Oppermann #endif 3697b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3698b287c6c7SBjoern A. Zeeb else 3699b287c6c7SBjoern A. Zeeb #endif 3700b287c6c7SBjoern A. Zeeb #ifdef INET 370197d8d152SAndre Oppermann { 37023c914c54SAndre Oppermann maxmtu = tcp_maxmtu(&inp->inp_inc, cap); 37030c39d38dSGleb Smirnoff tp->t_maxseg = V_tcp_mssdflt; 3704df8bae1dSRodney W. Grimes } 3705b287c6c7SBjoern A. Zeeb #endif 3706df8bae1dSRodney W. Grimes 370797d8d152SAndre Oppermann /* 3708e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 370997d8d152SAndre Oppermann */ 37106f01cac6SBjoern A. Zeeb if (maxmtu == 0) { 37116f01cac6SBjoern A. Zeeb /* 37128e5c87f4SBjoern A. Zeeb * In case we return early we need to initialize metrics 37136f01cac6SBjoern A. Zeeb * to a defined state as tcp_hc_get() would do for us 37146f01cac6SBjoern A. Zeeb * if there was no cache hit. 37156f01cac6SBjoern A. Zeeb */ 37166f01cac6SBjoern A. Zeeb if (metricptr != NULL) 37176f01cac6SBjoern A. Zeeb bzero(metricptr, sizeof(struct hc_metrics_lite)); 371897d8d152SAndre Oppermann return; 37196f01cac6SBjoern A. Zeeb } 372097d8d152SAndre Oppermann 3721e8949f74SAndre Oppermann /* What have we got? */ 372297d8d152SAndre Oppermann switch (offer) { 372397d8d152SAndre Oppermann case 0: 372497d8d152SAndre Oppermann /* 372597d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 3726c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 37270c39d38dSGleb Smirnoff * already assigned to t_maxseg above. 372897d8d152SAndre Oppermann */ 37290c39d38dSGleb Smirnoff offer = tp->t_maxseg; 373097d8d152SAndre Oppermann break; 373197d8d152SAndre Oppermann 373297d8d152SAndre Oppermann case -1: 3733a0292f23SGarrett Wollman /* 3734c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 3735a0292f23SGarrett Wollman */ 373697d8d152SAndre Oppermann /* FALLTHROUGH */ 373797d8d152SAndre Oppermann 373897d8d152SAndre Oppermann default: 3739a0292f23SGarrett Wollman /* 374053369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 374153369ac9SAndre Oppermann * to at least minmss. 374253369ac9SAndre Oppermann */ 3743603724d3SBjoern A. Zeeb offer = max(offer, V_tcp_minmss); 374497d8d152SAndre Oppermann } 3745a0292f23SGarrett Wollman 3746df8bae1dSRodney W. Grimes /* 3747e8949f74SAndre Oppermann * rmx information is now retrieved from tcp_hostcache. 3748df8bae1dSRodney W. Grimes */ 374997d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 37503cee92e0SBjoern A. Zeeb if (metricptr != NULL) 37513cee92e0SBjoern A. Zeeb bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 375297d8d152SAndre Oppermann 3753df8bae1dSRodney W. Grimes /* 3754cc412412SHiren Panchasara * If there's a discovered mtu in tcp hostcache, use it. 3755cc412412SHiren Panchasara * Else, use the link mtu. 3756df8bae1dSRodney W. Grimes */ 375797d8d152SAndre Oppermann if (metrics.rmx_mtu) 37582d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3759c068736aSJeffrey Hsu else { 376031b3783cSHajimu UMEMOTO #ifdef INET6 3761fb59c426SYoshinobu Inoue if (isipv6) { 376297d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3763603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 376497d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 3765603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_v6mssdflt); 3766b287c6c7SBjoern A. Zeeb } 3767b3399803SHajimu UMEMOTO #endif 3768b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3769b287c6c7SBjoern A. Zeeb else 3770b287c6c7SBjoern A. Zeeb #endif 3771b287c6c7SBjoern A. Zeeb #ifdef INET 377297d8d152SAndre Oppermann { 377397d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3774603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 377597d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 3776603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_mssdflt); 3777a0292f23SGarrett Wollman } 3778b287c6c7SBjoern A. Zeeb #endif 37793cee92e0SBjoern A. Zeeb /* 37803cee92e0SBjoern A. Zeeb * XXX - The above conditional (mss = maxmtu - min_protoh) 37813cee92e0SBjoern A. Zeeb * probably violates the TCP spec. 37823cee92e0SBjoern A. Zeeb * The problem is that, since we don't know the 37833cee92e0SBjoern A. Zeeb * other end's MSS, we are supposed to use a conservative 37843cee92e0SBjoern A. Zeeb * default. But, if we do that, then MTU discovery will 37853cee92e0SBjoern A. Zeeb * never actually take place, because the conservative 37863cee92e0SBjoern A. Zeeb * default is much less than the MTUs typically seen 37873cee92e0SBjoern A. Zeeb * on the Internet today. For the moment, we'll sweep 37883cee92e0SBjoern A. Zeeb * this under the carpet. 37893cee92e0SBjoern A. Zeeb * 37903cee92e0SBjoern A. Zeeb * The conservative default might not actually be a problem 37913cee92e0SBjoern A. Zeeb * if the only case this occurs is when sending an initial 37923cee92e0SBjoern A. Zeeb * SYN with options and data to a host we've never talked 37933cee92e0SBjoern A. Zeeb * to before. Then, they will reply with an MSS value which 37943cee92e0SBjoern A. Zeeb * will get recorded and the new parameters should get 37953cee92e0SBjoern A. Zeeb * recomputed. For Further Study. 37963cee92e0SBjoern A. Zeeb */ 379797d8d152SAndre Oppermann } 3798a0292f23SGarrett Wollman mss = min(mss, offer); 379997d8d152SAndre Oppermann 3800a0292f23SGarrett Wollman /* 38010c39d38dSGleb Smirnoff * Sanity check: make sure that maxseg will be large 38023cee92e0SBjoern A. Zeeb * enough to allow some data on segments even if the 38033cee92e0SBjoern A. Zeeb * all the option space is used (40bytes). Otherwise 38043cee92e0SBjoern A. Zeeb * funny things may happen in tcp_output. 38050c39d38dSGleb Smirnoff * 38060c39d38dSGleb Smirnoff * XXXGL: shouldn't we reserve space for IP/IPv6 options? 38073cee92e0SBjoern A. Zeeb */ 38083cee92e0SBjoern A. Zeeb mss = max(mss, 64); 38093cee92e0SBjoern A. Zeeb 381097d8d152SAndre Oppermann tp->t_maxseg = mss; 38113cee92e0SBjoern A. Zeeb } 38123cee92e0SBjoern A. Zeeb 38133cee92e0SBjoern A. Zeeb void 38143cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer) 38153cee92e0SBjoern A. Zeeb { 3816dbc42409SLawrence Stewart int mss; 38173ac12506SJonathan T. Looney uint32_t bufsize; 38183cee92e0SBjoern A. Zeeb struct inpcb *inp; 38193cee92e0SBjoern A. Zeeb struct socket *so; 38203cee92e0SBjoern A. Zeeb struct hc_metrics_lite metrics; 38213c914c54SAndre Oppermann struct tcp_ifcap cap; 3822dbc42409SLawrence Stewart 38233cee92e0SBjoern A. Zeeb KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 38243cee92e0SBjoern A. Zeeb 38253c914c54SAndre Oppermann bzero(&cap, sizeof(cap)); 38263c914c54SAndre Oppermann tcp_mss_update(tp, offer, -1, &metrics, &cap); 38273cee92e0SBjoern A. Zeeb 38283cee92e0SBjoern A. Zeeb mss = tp->t_maxseg; 38293cee92e0SBjoern A. Zeeb inp = tp->t_inpcb; 383097d8d152SAndre Oppermann 3831df8bae1dSRodney W. Grimes /* 383297d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 383397d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 383497d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 383597d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 383697d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3837df8bae1dSRodney W. Grimes */ 3838c3b02504SBjoern A. Zeeb so = inp->inp_socket; 38393f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 3840e233e2acSAndre Oppermann if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe) 384197d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 384297d8d152SAndre Oppermann else 3843df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3844df8bae1dSRodney W. Grimes if (bufsize < mss) 3845df8bae1dSRodney W. Grimes mss = bufsize; 3846df8bae1dSRodney W. Grimes else { 3847df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3848df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3849df8bae1dSRodney W. Grimes bufsize = sb_max; 385088c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 385143283184SGleb Smirnoff (void)sbreserve_locked(so, SO_SND, bufsize, NULL); 3852df8bae1dSRodney W. Grimes } 38533f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 3854784ce8faSHiren Panchasara /* 3855784ce8faSHiren Panchasara * Sanity check: make sure that maxseg will be large 3856784ce8faSHiren Panchasara * enough to allow some data on segments even if the 3857784ce8faSHiren Panchasara * all the option space is used (40bytes). Otherwise 3858784ce8faSHiren Panchasara * funny things may happen in tcp_output. 3859784ce8faSHiren Panchasara * 3860784ce8faSHiren Panchasara * XXXGL: shouldn't we reserve space for IP/IPv6 options? 3861784ce8faSHiren Panchasara */ 3862784ce8faSHiren Panchasara tp->t_maxseg = max(mss, 64); 3863df8bae1dSRodney W. Grimes 38643f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3865e233e2acSAndre Oppermann if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe) 386697d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 386797d8d152SAndre Oppermann else 3868df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3869df8bae1dSRodney W. Grimes if (bufsize > mss) { 3870df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3871df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3872df8bae1dSRodney W. Grimes bufsize = sb_max; 387388c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 387443283184SGleb Smirnoff (void)sbreserve_locked(so, SO_RCV, bufsize, NULL); 3875df8bae1dSRodney W. Grimes } 38763f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 387791d6cfa6SBjoern A. Zeeb 387891d6cfa6SBjoern A. Zeeb /* Check the interface for TSO capabilities. */ 38793c914c54SAndre Oppermann if (cap.ifcap & CSUM_TSO) { 388091d6cfa6SBjoern A. Zeeb tp->t_flags |= TF_TSO; 38813c914c54SAndre Oppermann tp->t_tsomax = cap.tsomax; 38829fd573c3SHans Petter Selasky tp->t_tsomaxsegcount = cap.tsomaxsegcount; 38839fd573c3SHans Petter Selasky tp->t_tsomaxsegsize = cap.tsomaxsegsize; 38843c914c54SAndre Oppermann } 3885a0292f23SGarrett Wollman } 3886a0292f23SGarrett Wollman 3887a0292f23SGarrett Wollman /* 3888a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3889a0292f23SGarrett Wollman */ 3890a0292f23SGarrett Wollman int 3891ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 3892a0292f23SGarrett Wollman { 389397d8d152SAndre Oppermann int mss = 0; 38943ac12506SJonathan T. Looney uint32_t thcmtu = 0; 38953ac12506SJonathan T. Looney uint32_t maxmtu = 0; 389697d8d152SAndre Oppermann size_t min_protoh; 3897a0292f23SGarrett Wollman 389897d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3899a0292f23SGarrett Wollman 390031b3783cSHajimu UMEMOTO #ifdef INET6 3901dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3902603724d3SBjoern A. Zeeb mss = V_tcp_v6mssdflt; 3903233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 390497d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 3905b287c6c7SBjoern A. Zeeb } 390631b3783cSHajimu UMEMOTO #endif 3907b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3908b287c6c7SBjoern A. Zeeb else 3909b287c6c7SBjoern A. Zeeb #endif 3910b287c6c7SBjoern A. Zeeb #ifdef INET 391197d8d152SAndre Oppermann { 3912603724d3SBjoern A. Zeeb mss = V_tcp_mssdflt; 3913233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 391497d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 391597d8d152SAndre Oppermann } 3916b287c6c7SBjoern A. Zeeb #endif 39173a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET) 39183a9391deSBjoern A. Zeeb thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 39193a9391deSBjoern A. Zeeb #endif 39203a9391deSBjoern A. Zeeb 392197d8d152SAndre Oppermann if (maxmtu && thcmtu) 392297d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 392397d8d152SAndre Oppermann else if (maxmtu || thcmtu) 392497d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 392597d8d152SAndre Oppermann 392697d8d152SAndre Oppermann return (mss); 3927df8bae1dSRodney W. Grimes } 392846f58482SJonathan Lemon 39290e1d7c25SRichard Scheffenegger void 393090cca08eSRichard Scheffenegger tcp_do_prr_ack(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to) 39310e1d7c25SRichard Scheffenegger { 393248396dc7SRichard Scheffenegger int snd_cnt = 0, limit = 0, del_data = 0, pipe = 0; 39330e1d7c25SRichard Scheffenegger int maxseg = tcp_maxseg(tp); 39340e1d7c25SRichard Scheffenegger 39350e1d7c25SRichard Scheffenegger INP_WLOCK_ASSERT(tp->t_inpcb); 39360e1d7c25SRichard Scheffenegger 39370e1d7c25SRichard Scheffenegger /* 39380e1d7c25SRichard Scheffenegger * Compute the amount of data that this ACK is indicating 39390e1d7c25SRichard Scheffenegger * (del_data) and an estimate of how many bytes are in the 39400e1d7c25SRichard Scheffenegger * network. 39410e1d7c25SRichard Scheffenegger */ 394274d7fc87SRichard Scheffenegger if (((tp->t_flags & TF_SACK_PERMIT) && 394374d7fc87SRichard Scheffenegger (to->to_flags & TOF_SACK)) || 394474d7fc87SRichard Scheffenegger (IN_CONGRECOVERY(tp->t_flags) && 394574d7fc87SRichard Scheffenegger !IN_FASTRECOVERY(tp->t_flags))) { 394631d7a27cSRichard Scheffenegger del_data = tp->sackhint.delivered_data; 3947d1de2b05SRichard Scheffenegger if (V_tcp_do_newsack) 3948a8e431e1SRichard Scheffenegger pipe = tcp_compute_pipe(tp); 3949a8e431e1SRichard Scheffenegger else 395074d7fc87SRichard Scheffenegger pipe = (tp->snd_nxt - tp->snd_fack) + 395174d7fc87SRichard Scheffenegger tp->sackhint.sack_bytes_rexmit; 395274d7fc87SRichard Scheffenegger } else { 395374d7fc87SRichard Scheffenegger if (tp->sackhint.prr_delivered < (tcprexmtthresh * maxseg + 395474d7fc87SRichard Scheffenegger tp->snd_recover - tp->snd_una)) 395574d7fc87SRichard Scheffenegger del_data = maxseg; 395674d7fc87SRichard Scheffenegger pipe = imax(0, tp->snd_max - tp->snd_una - 395774d7fc87SRichard Scheffenegger imin(INT_MAX / 65536, tp->t_dupacks) * maxseg); 395874d7fc87SRichard Scheffenegger } 39590e1d7c25SRichard Scheffenegger tp->sackhint.prr_delivered += del_data; 39600e1d7c25SRichard Scheffenegger /* 39610e1d7c25SRichard Scheffenegger * Proportional Rate Reduction 39620e1d7c25SRichard Scheffenegger */ 3963e9071000SRichard Scheffenegger if (pipe >= tp->snd_ssthresh) { 39646a376af0SRichard Scheffenegger if (tp->sackhint.recover_fs == 0) 39656a376af0SRichard Scheffenegger tp->sackhint.recover_fs = 396648396dc7SRichard Scheffenegger imax(1, tp->snd_nxt - tp->snd_una); 396748396dc7SRichard Scheffenegger snd_cnt = howmany((long)tp->sackhint.prr_delivered * 396848396dc7SRichard Scheffenegger tp->snd_ssthresh, tp->sackhint.recover_fs) - 3969e5313869SRichard Scheffenegger tp->sackhint.prr_out; 39706a376af0SRichard Scheffenegger } else { 397174d7fc87SRichard Scheffenegger if (V_tcp_do_prr_conservative || (del_data == 0)) 397284761f3dSRichard Scheffenegger limit = tp->sackhint.prr_delivered - 3973e5313869SRichard Scheffenegger tp->sackhint.prr_out; 39740e1d7c25SRichard Scheffenegger else 397548396dc7SRichard Scheffenegger limit = imax(tp->sackhint.prr_delivered - 3976e5313869SRichard Scheffenegger tp->sackhint.prr_out, del_data) + 3977e5313869SRichard Scheffenegger maxseg; 397848396dc7SRichard Scheffenegger snd_cnt = imin((tp->snd_ssthresh - pipe), limit); 39790e1d7c25SRichard Scheffenegger } 398048396dc7SRichard Scheffenegger snd_cnt = imax(snd_cnt, 0) / maxseg; 39810e1d7c25SRichard Scheffenegger /* 39820e1d7c25SRichard Scheffenegger * Send snd_cnt new data into the network in response to this ack. 39830e1d7c25SRichard Scheffenegger * If there is going to be a SACK retransmission, adjust snd_cwnd 39840e1d7c25SRichard Scheffenegger * accordingly. 39850e1d7c25SRichard Scheffenegger */ 3986b9f803b7SRichard Scheffenegger if (IN_FASTRECOVERY(tp->t_flags)) { 398774d7fc87SRichard Scheffenegger if ((tp->t_flags & TF_SACK_PERMIT) && 398874d7fc87SRichard Scheffenegger (to->to_flags & TOF_SACK)) { 398974d7fc87SRichard Scheffenegger tp->snd_cwnd = tp->snd_nxt - tp->snd_recover + 399074d7fc87SRichard Scheffenegger tp->sackhint.sack_bytes_rexmit + 399174d7fc87SRichard Scheffenegger (snd_cnt * maxseg); 399274d7fc87SRichard Scheffenegger } else { 399374d7fc87SRichard Scheffenegger tp->snd_cwnd = (tp->snd_max - tp->snd_una) + 399474d7fc87SRichard Scheffenegger (snd_cnt * maxseg); 399574d7fc87SRichard Scheffenegger } 3996b9f803b7SRichard Scheffenegger } else if (IN_CONGRECOVERY(tp->t_flags)) 399774d7fc87SRichard Scheffenegger tp->snd_cwnd = pipe - del_data + (snd_cnt * maxseg); 399874d7fc87SRichard Scheffenegger tp->snd_cwnd = imax(maxseg, tp->snd_cwnd); 39990e1d7c25SRichard Scheffenegger } 40000e1d7c25SRichard Scheffenegger 400146f58482SJonathan Lemon /* 4002c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 4003c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 4004c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 4005c068736aSJeffrey Hsu * be started again. 400646f58482SJonathan Lemon */ 400755bceb1eSRandall Stewart void 4008ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 400946f58482SJonathan Lemon { 401046f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 40113ac12506SJonathan T. Looney uint32_t ocwnd = tp->snd_cwnd; 40120c39d38dSGleb Smirnoff u_int maxseg = tcp_maxseg(tp); 401346f58482SJonathan Lemon 40148501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 40157a3244ccSRobert Watson 4016b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 401746f58482SJonathan Lemon tp->t_rtttime = 0; 401846f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 40196b2a5f92SJayanth Vijayaraghavan /* 4020c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 4021c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 40226b2a5f92SJayanth Vijayaraghavan */ 40230c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th); 4024a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 402540fa3e40SGleb Smirnoff (void) tcp_output(tp); 402646f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 402746f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 402846f58482SJonathan Lemon tp->snd_nxt = onxt; 402946f58482SJonathan Lemon /* 403046f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 403146f58482SJonathan Lemon * not updated yet. 403246f58482SJonathan Lemon */ 4033dbc42409SLawrence Stewart if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th)) 4034dbc42409SLawrence Stewart tp->snd_cwnd -= BYTES_THIS_ACK(tp, th); 4035d7587117SPaul Saab else 4036d7587117SPaul Saab tp->snd_cwnd = 0; 40370c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 403846f58482SJonathan Lemon } 403912eeb81fSHiren Panchasara 404012eeb81fSHiren Panchasara int 404112eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp) 404212eeb81fSHiren Panchasara { 404312eeb81fSHiren Panchasara return (tp->snd_max - tp->snd_una + 404412eeb81fSHiren Panchasara tp->sackhint.sack_bytes_rexmit - 404512eeb81fSHiren Panchasara tp->sackhint.sacked_bytes); 404612eeb81fSHiren Panchasara } 40477dc90a1dSMichael Tuexen 40487dc90a1dSMichael Tuexen uint32_t 40497dc90a1dSMichael Tuexen tcp_compute_initwnd(uint32_t maxseg) 40507dc90a1dSMichael Tuexen { 40517dc90a1dSMichael Tuexen /* 40527dc90a1dSMichael Tuexen * Calculate the Initial Window, also used as Restart Window 40537dc90a1dSMichael Tuexen * 40547dc90a1dSMichael Tuexen * RFC5681 Section 3.1 specifies the default conservative values. 40557dc90a1dSMichael Tuexen * RFC3390 specifies slightly more aggressive values. 40567dc90a1dSMichael Tuexen * RFC6928 increases it to ten segments. 40577dc90a1dSMichael Tuexen * Support for user specified value for initial flight size. 40587dc90a1dSMichael Tuexen */ 40597dc90a1dSMichael Tuexen if (V_tcp_initcwnd_segments) 40607dc90a1dSMichael Tuexen return min(V_tcp_initcwnd_segments * maxseg, 40617dc90a1dSMichael Tuexen max(2 * maxseg, V_tcp_initcwnd_segments * 1460)); 40627dc90a1dSMichael Tuexen else if (V_tcp_do_rfc3390) 40637dc90a1dSMichael Tuexen return min(4 * maxseg, max(2 * maxseg, 4380)); 40647dc90a1dSMichael Tuexen else { 40657dc90a1dSMichael Tuexen /* Per RFC5681 Section 3.1 */ 40667dc90a1dSMichael Tuexen if (maxseg > 2190) 40677dc90a1dSMichael Tuexen return (2 * maxseg); 40687dc90a1dSMichael Tuexen else if (maxseg > 1095) 40697dc90a1dSMichael Tuexen return (3 * maxseg); 40707dc90a1dSMichael Tuexen else 40717dc90a1dSMichael Tuexen return (4 * maxseg); 40727dc90a1dSMichael Tuexen } 40737dc90a1dSMichael Tuexen } 4074