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 */ 49df8bae1dSRodney W. Grimes 504b421e2dSMike Silbersack #include <sys/cdefs.h> 511cfd4b53SBruce M Simpson #include "opt_inet.h" 52fb59c426SYoshinobu Inoue #include "opt_inet6.h" 533a2a9f79SYoshinobu Inoue #include "opt_ipsec.h" 54e5738ee0SCheng Cui #include "opt_rss.h" 550cc12cc5SJoerg Wunsch 56df8bae1dSRodney W. Grimes #include <sys/param.h> 57adc56f5aSEdward Tomasz Napierala #include <sys/arb.h> 5898163b98SPoul-Henning Kamp #include <sys/kernel.h> 59bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 6039bc9de5SLawrence Stewart #include <sys/hhook.h> 61bd79708dSJonathan T. Looney #endif 62df8bae1dSRodney W. Grimes #include <sys/malloc.h> 63df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 64a29f300eSGarrett Wollman #include <sys/proc.h> /* for proc0 declaration */ 65df8bae1dSRodney W. Grimes #include <sys/protosw.h> 66adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h> 6757f60867SMark Johnston #include <sys/sdt.h> 68960ed29cSSeigo Tanimura #include <sys/signalvar.h> 69df8bae1dSRodney W. Grimes #include <sys/socket.h> 70df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 71960ed29cSSeigo Tanimura #include <sys/sysctl.h> 72816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 73960ed29cSSeigo Tanimura #include <sys/systm.h> 74adc56f5aSEdward Tomasz Napierala #include <sys/stats.h> 75df8bae1dSRodney W. Grimes 76e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 77e79adb8eSGarrett Wollman 7812e2e970SAndre Oppermann #include <vm/uma.h> 7912e2e970SAndre Oppermann 80df8bae1dSRodney W. Grimes #include <net/if.h> 8176039bc8SGleb Smirnoff #include <net/if_var.h> 82df8bae1dSRodney W. Grimes #include <net/route.h> 83e5738ee0SCheng Cui #include <net/rss_config.h> 84530c0060SRobert Watson #include <net/vnet.h> 85df8bae1dSRodney W. Grimes 86773673c1SAndre Oppermann #define TCPSTATES /* for logging */ 87773673c1SAndre Oppermann 88df8bae1dSRodney W. Grimes #include <netinet/in.h> 8957f60867SMark Johnston #include <netinet/in_kdtrace.h> 90960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 91e5738ee0SCheng Cui #include <netinet/in_rss.h> 92df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 93df8bae1dSRodney W. Grimes #include <netinet/ip.h> 948e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 95686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 96df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 97ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 98686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 99686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h> 100686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 101e5738ee0SCheng Cui #include <netinet6/in6_rss.h> 102b66d74c1SGleb Smirnoff #include <netinet6/in6_var.h> 103960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h> 104960ed29cSSeigo Tanimura #include <netinet6/nd6.h> 1052de3e790SGleb Smirnoff #include <netinet/tcp.h> 106df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 107df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 108df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 109df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 11069c7c811SRandall Stewart #include <netinet/tcp_log_buf.h> 111fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 112df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 1134644fda3SGleb Smirnoff #include <netinet/cc/cc.h> 114c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h> 11586a996e6SHiren Panchasara #ifdef TCPPCAP 11686a996e6SHiren Panchasara #include <netinet/tcp_pcap.h> 11786a996e6SHiren Panchasara #endif 118c325962bSMike Silbersack #include <netinet/tcp_syncache.h> 11909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 12009fe6320SNavdeep Parhar #include <netinet/tcp_offload.h> 12109fe6320SNavdeep Parhar #endif 122f7220c48SRichard Scheffenegger #include <netinet/tcp_ecn.h> 1239e644c23SMichael Tuexen #include <netinet/udp.h> 124fb59c426SYoshinobu Inoue 125fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 126b9234fafSSam Leffler 127db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 128db4f9cc7SJonathan Lemon 129aed55708SRobert Watson #include <security/mac/mac_framework.h> 130aed55708SRobert Watson 131dbc42409SLawrence Stewart const int tcprexmtthresh = 3; 1320312fbe9SPoul-Henning Kamp 133334fc582SBjoern A. Zeeb VNET_DEFINE(int, tcp_log_in_vain) = 0; 134334fc582SBjoern A. Zeeb SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_VNET | CTLFLAG_RW, 135334fc582SBjoern A. Zeeb &VNET_NAME(tcp_log_in_vain), 0, 136eddfbb76SRobert Watson "Log all incoming TCP segments to closed ports"); 137816a3d83SPoul-Henning Kamp 138*5dc99e9bSMark Johnston VNET_DEFINE(int, tcp_bind_all_fibs) = 1; 139*5dc99e9bSMark Johnston SYSCTL_INT(_net_inet_tcp, OID_AUTO, bind_all_fibs, CTLFLAG_VNET | CTLFLAG_RDTUN, 140*5dc99e9bSMark Johnston &VNET_NAME(tcp_bind_all_fibs), 0, 141*5dc99e9bSMark Johnston "Bound sockets receive traffic from all FIBs"); 142*5dc99e9bSMark Johnston 14382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0; 14482cea7e6SBjoern A. Zeeb #define V_blackhole VNET(blackhole) 1456df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW, 146eddfbb76SRobert Watson &VNET_NAME(blackhole), 0, 147eddfbb76SRobert Watson "Do not send RST on segments to closed ports"); 14816f7f31fSGeoff Rehmet 1493ea9a7cfSGleb Smirnoff VNET_DEFINE(bool, blackhole_local) = false; 1503ea9a7cfSGleb Smirnoff #define V_blackhole_local VNET(blackhole_local) 1513ea9a7cfSGleb Smirnoff SYSCTL_BOOL(_net_inet_tcp, OID_AUTO, blackhole_local, CTLFLAG_VNET | 1523ea9a7cfSGleb Smirnoff CTLFLAG_RW, &VNET_NAME(blackhole_local), false, 1533ea9a7cfSGleb Smirnoff "Enforce net.inet.tcp.blackhole for locally originated packets"); 1543ea9a7cfSGleb Smirnoff 15582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1; 1566df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW, 157eddfbb76SRobert Watson &VNET_NAME(tcp_delack_enabled), 0, 1583d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 159f498eeeeSDavid Greenman 16082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0; 1616df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW, 162eddfbb76SRobert Watson &VNET_NAME(drop_synfin), 0, 163eddfbb76SRobert Watson "Drop TCP packets with SYN+FIN set"); 164f8613305SDag-Erling Smørgrav 1650e1d7c25SRichard Scheffenegger VNET_DEFINE(int, tcp_do_prr) = 1; 1660e1d7c25SRichard Scheffenegger SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_prr, CTLFLAG_VNET | CTLFLAG_RW, 1670e1d7c25SRichard Scheffenegger &VNET_NAME(tcp_do_prr), 1, 1680e1d7c25SRichard Scheffenegger "Enable Proportional Rate Reduction per RFC 6937"); 1690e1d7c25SRichard Scheffenegger 170b72e56e7SMichael Tuexen VNET_DEFINE(int, tcp_do_newcwv) = 0; 171b72e56e7SMichael Tuexen SYSCTL_INT(_net_inet_tcp, OID_AUTO, newcwv, CTLFLAG_VNET | CTLFLAG_RW, 172b72e56e7SMichael Tuexen &VNET_NAME(tcp_do_newcwv), 0, 173b72e56e7SMichael Tuexen "Enable New Congestion Window Validation per RFC7661"); 174b72e56e7SMichael Tuexen 17582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1; 1766df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW, 177eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3042), 0, 178eddfbb76SRobert Watson "Enable RFC 3042 (Limited Transmit)"); 179582a954bSJeffrey Hsu 18082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1; 1816df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW, 182eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3390), 0, 183da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 184da3a8a1aSJeffrey Hsu 185356c7958SHiren Panchasara VNET_DEFINE(int, tcp_initcwnd_segments) = 10; 186356c7958SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments, 187356c7958SHiren Panchasara CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0, 188356c7958SHiren Panchasara "Slow-start flight size (initial congestion window) in number of segments"); 18909440655SAndre Oppermann 19082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1; 1916df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW, 192eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3465), 0, 19324cb0f22SLawrence Stewart "Enable RFC 3465 (Appropriate Byte Counting)"); 194eddfbb76SRobert Watson 19582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2; 1966df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW, 197eddfbb76SRobert Watson &VNET_NAME(tcp_abc_l_var), 2, 19824cb0f22SLawrence Stewart "Cap the max cwnd increment during slow-start to this number of segments"); 19924cb0f22SLawrence Stewart 2003220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0; 2016df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW, 2023220a212SGleb Smirnoff &VNET_NAME(tcp_insecure_syn), 0, 2033220a212SGleb Smirnoff "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets"); 2043220a212SGleb Smirnoff 20582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0; 2066df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW, 207eddfbb76SRobert Watson &VNET_NAME(tcp_insecure_rst), 0, 2083220a212SGleb Smirnoff "Follow RFC793 instead of RFC5961 criteria for accepting RST packets"); 209a69968eeSMike Silbersack 210646c28eaSMichael Tuexen VNET_DEFINE(int, tcp_insecure_ack) = 0; 211646c28eaSMichael Tuexen SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_ack, CTLFLAG_VNET | CTLFLAG_RW, 212646c28eaSMichael Tuexen &VNET_NAME(tcp_insecure_ack), 0, 213646c28eaSMichael Tuexen "Follow RFC793 criteria for validating SEG.ACK"); 214646c28eaSMichael Tuexen 215fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64; 216873789cbSAndre Oppermann #define V_tcp_recvspace VNET(tcp_recvspace) 2176df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW, 218873789cbSAndre Oppermann &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size"); 219873789cbSAndre Oppermann 22082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1; 2216df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW, 222eddfbb76SRobert Watson &VNET_NAME(tcp_do_autorcvbuf), 0, 2238b615593SMarko Zec "Enable automatic receive buffer sizing"); 2246741ecf5SAndre Oppermann 225b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024; 2266df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW, 227eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_max), 0, 2288b615593SMarko Zec "Max size of automatic receive buffer"); 2296741ecf5SAndre Oppermann 23082cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo); 231df8bae1dSRodney W. Grimes 232315e3e38SRobert Watson /* 233bf840a17SGleb Smirnoff * TCP statistics are stored in an array of counter(9)s, which size matches 234bf840a17SGleb Smirnoff * size of struct tcpstat. TCP running connection count is a regular array. 2355923c293SGleb Smirnoff */ 2365da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat); 2375da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat, 2385da0521fSAndrey V. Elsukov tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 239bf840a17SGleb Smirnoff VNET_DEFINE(counter_u64_t, tcps_states[TCP_NSTATES]); 240bf840a17SGleb Smirnoff SYSCTL_COUNTER_U64_ARRAY(_net_inet_tcp, TCPCTL_STATES, states, CTLFLAG_RD | 241d4d32b9fSHans Petter Selasky CTLFLAG_VNET, &VNET_NAME(tcps_states)[0], TCP_NSTATES, 242bf840a17SGleb Smirnoff "TCP connection counts by TCP state"); 243bf840a17SGleb Smirnoff 2445923c293SGleb Smirnoff /* 2457ca6e296SMichael Tuexen * Kernel module interface for updating tcpstat. The first argument is an index 2465923c293SGleb Smirnoff * into tcpstat treated as an array. 247315e3e38SRobert Watson */ 248315e3e38SRobert Watson void 2497ca6e296SMichael Tuexen kmod_tcpstat_add(int statnum, int val) 250315e3e38SRobert Watson { 251315e3e38SRobert Watson 2527ca6e296SMichael Tuexen counter_u64_add(VNET(tcpstat)[statnum], val); 253315e3e38SRobert Watson } 254315e3e38SRobert Watson 255c21b7b55SRichard Scheffenegger /* 256c21b7b55SRichard Scheffenegger * Make sure that we only start a SACK loss recovery when 257c21b7b55SRichard Scheffenegger * receiving a duplicate ACK with a SACK block, and also 258c21b7b55SRichard Scheffenegger * complete SACK loss recovery in case the other end 259c21b7b55SRichard Scheffenegger * reneges. 260c21b7b55SRichard Scheffenegger */ 261c21b7b55SRichard Scheffenegger static bool inline 262c21b7b55SRichard Scheffenegger tcp_is_sack_recovery(struct tcpcb *tp, struct tcpopt *to) 263c21b7b55SRichard Scheffenegger { 264c21b7b55SRichard Scheffenegger return ((tp->t_flags & TF_SACK_PERMIT) && 265c21b7b55SRichard Scheffenegger ((to->to_flags & TOF_SACK) || 266c21b7b55SRichard Scheffenegger (!TAILQ_EMPTY(&tp->snd_holes)))); 267c21b7b55SRichard Scheffenegger } 268c21b7b55SRichard Scheffenegger 269bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 270dbc42409SLawrence Stewart /* 27139bc9de5SLawrence Stewart * Wrapper for the TCP established input helper hook. 27239bc9de5SLawrence Stewart */ 27355bceb1eSRandall Stewart void 27439bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to) 27539bc9de5SLawrence Stewart { 27639bc9de5SLawrence Stewart struct tcp_hhook_data hhook_data; 27739bc9de5SLawrence Stewart 27839bc9de5SLawrence Stewart if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) { 27939bc9de5SLawrence Stewart hhook_data.tp = tp; 28039bc9de5SLawrence Stewart hhook_data.th = th; 28139bc9de5SLawrence Stewart hhook_data.to = to; 28239bc9de5SLawrence Stewart 28339bc9de5SLawrence Stewart hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data, 284e68b3792SGleb Smirnoff &tp->t_osd); 28539bc9de5SLawrence Stewart } 28639bc9de5SLawrence Stewart } 287bd79708dSJonathan T. Looney #endif 28839bc9de5SLawrence Stewart 28939bc9de5SLawrence Stewart /* 290dbc42409SLawrence Stewart * CC wrapper hook functions 291dbc42409SLawrence Stewart */ 29255bceb1eSRandall Stewart void 2934b7b743cSLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t nsegs, 2944b7b743cSLawrence Stewart uint16_t type) 295f2512ba1SRui Paulo { 296adc56f5aSEdward Tomasz Napierala #ifdef STATS 297adc56f5aSEdward Tomasz Napierala int32_t gput; 298adc56f5aSEdward Tomasz Napierala #endif 299adc56f5aSEdward Tomasz Napierala 3009eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(tptoinpcb(tp)); 301f2512ba1SRui Paulo 302e68b3792SGleb Smirnoff tp->t_ccv.nsegs = nsegs; 303e68b3792SGleb Smirnoff tp->t_ccv.bytes_this_ack = BYTES_THIS_ACK(tp, th); 304b72e56e7SMichael Tuexen if ((!V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd)) || 305b72e56e7SMichael Tuexen (V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd) && 306b72e56e7SMichael Tuexen (tp->snd_cwnd < (tcp_compute_pipe(tp) * 2)))) 307e68b3792SGleb Smirnoff tp->t_ccv.flags |= CCF_CWND_LIMITED; 308dbc42409SLawrence Stewart else 309e68b3792SGleb Smirnoff tp->t_ccv.flags &= ~CCF_CWND_LIMITED; 310dbc42409SLawrence Stewart 311dbc42409SLawrence Stewart if (type == CC_ACK) { 312adc56f5aSEdward Tomasz Napierala #ifdef STATS 313adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF, 314adc56f5aSEdward Tomasz Napierala ((int32_t)tp->snd_cwnd) - tp->snd_wnd); 315adc56f5aSEdward Tomasz Napierala if (!IN_RECOVERY(tp->t_flags)) 316adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_ACKLEN, 317e68b3792SGleb Smirnoff tp->t_ccv.bytes_this_ack / (tcp_maxseg(tp) * nsegs)); 318adc56f5aSEdward Tomasz Napierala if ((tp->t_flags & TF_GPUTINPROG) && 319adc56f5aSEdward Tomasz Napierala SEQ_GEQ(th->th_ack, tp->gput_ack)) { 320adc56f5aSEdward Tomasz Napierala /* 321adc56f5aSEdward Tomasz Napierala * Compute goodput in bits per millisecond. 322adc56f5aSEdward Tomasz Napierala */ 323f5766992SHans Petter Selasky gput = (((int64_t)SEQ_SUB(th->th_ack, tp->gput_seq)) << 3) / 324adc56f5aSEdward Tomasz Napierala max(1, tcp_ts_getticks() - tp->gput_ts); 325adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT, 326adc56f5aSEdward Tomasz Napierala gput); 327adc56f5aSEdward Tomasz Napierala /* 328adc56f5aSEdward Tomasz Napierala * XXXLAS: This is a temporary hack, and should be 329adc56f5aSEdward Tomasz Napierala * chained off VOI_TCP_GPUT when stats(9) grows an API 330adc56f5aSEdward Tomasz Napierala * to deal with chained VOIs. 331adc56f5aSEdward Tomasz Napierala */ 332adc56f5aSEdward Tomasz Napierala if (tp->t_stats_gput_prev > 0) 333adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_s32(tp->t_stats, 334adc56f5aSEdward Tomasz Napierala VOI_TCP_GPUT_ND, 335adc56f5aSEdward Tomasz Napierala ((gput - tp->t_stats_gput_prev) * 100) / 336adc56f5aSEdward Tomasz Napierala tp->t_stats_gput_prev); 337adc56f5aSEdward Tomasz Napierala tp->t_flags &= ~TF_GPUTINPROG; 338adc56f5aSEdward Tomasz Napierala tp->t_stats_gput_prev = gput; 339adc56f5aSEdward Tomasz Napierala } 340adc56f5aSEdward Tomasz Napierala #endif /* STATS */ 341dbc42409SLawrence Stewart if (tp->snd_cwnd > tp->snd_ssthresh) { 342e68b3792SGleb Smirnoff tp->t_bytes_acked += tp->t_ccv.bytes_this_ack; 343dbc42409SLawrence Stewart if (tp->t_bytes_acked >= tp->snd_cwnd) { 344dbc42409SLawrence Stewart tp->t_bytes_acked -= tp->snd_cwnd; 345e68b3792SGleb Smirnoff tp->t_ccv.flags |= CCF_ABC_SENTAWND; 346dbc42409SLawrence Stewart } 347dbc42409SLawrence Stewart } else { 348e68b3792SGleb Smirnoff tp->t_ccv.flags &= ~CCF_ABC_SENTAWND; 349dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 350dbc42409SLawrence Stewart } 351dbc42409SLawrence Stewart } 352dbc42409SLawrence Stewart 353dbc42409SLawrence Stewart if (CC_ALGO(tp)->ack_received != NULL) { 354dbc42409SLawrence Stewart /* XXXLAS: Find a way to live without this */ 355e68b3792SGleb Smirnoff tp->t_ccv.curack = th->th_ack; 356e68b3792SGleb Smirnoff CC_ALGO(tp)->ack_received(&tp->t_ccv, type); 357dbc42409SLawrence Stewart } 358adc56f5aSEdward Tomasz Napierala #ifdef STATS 359adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, tp->snd_cwnd); 360adc56f5aSEdward Tomasz Napierala #endif 361dbc42409SLawrence Stewart } 362dbc42409SLawrence Stewart 36355bceb1eSRandall Stewart void 364dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp) 365dbc42409SLawrence Stewart { 366dbc42409SLawrence Stewart struct hc_metrics_lite metrics; 3679eb0e832SGleb Smirnoff struct inpcb *inp = tptoinpcb(tp); 3680c39d38dSGleb Smirnoff u_int maxseg; 369dbc42409SLawrence Stewart int rtt; 370dbc42409SLawrence Stewart 3719eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(inp); 372dbc42409SLawrence Stewart 373dbc42409SLawrence Stewart tcp_hc_get(&inp->inp_inc, &metrics); 3740c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 375dbc42409SLawrence Stewart 37609000cc1SGleb Smirnoff if (tp->t_srtt == 0 && (rtt = metrics.hc_rtt)) { 377dbc42409SLawrence Stewart tp->t_srtt = rtt; 378dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrtt); 37909000cc1SGleb Smirnoff if (metrics.hc_rttvar) { 38009000cc1SGleb Smirnoff tp->t_rttvar = metrics.hc_rttvar; 381dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrttvar); 382dbc42409SLawrence Stewart } else { 383dbc42409SLawrence Stewart /* default variation is +- 1 rtt */ 384dbc42409SLawrence Stewart tp->t_rttvar = 385dbc42409SLawrence Stewart tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 386dbc42409SLawrence Stewart } 387dbc42409SLawrence Stewart TCPT_RANGESET(tp->t_rxtcur, 388dbc42409SLawrence Stewart ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 389dbc42409SLawrence Stewart tp->t_rttmin, TCPTV_REXMTMAX); 390dbc42409SLawrence Stewart } 39109000cc1SGleb Smirnoff if (metrics.hc_ssthresh) { 392dbc42409SLawrence Stewart /* 393dbc42409SLawrence Stewart * There's some sort of gateway or interface 394dbc42409SLawrence Stewart * buffer limit on the path. Use this to set 395a4641f4eSPedro F. Giffuni * the slow start threshold, but set the 396dbc42409SLawrence Stewart * threshold to no less than 2*mss. 397dbc42409SLawrence Stewart */ 39809000cc1SGleb Smirnoff tp->snd_ssthresh = max(2 * maxseg, metrics.hc_ssthresh); 399dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedssthresh); 400dbc42409SLawrence Stewart } 401dbc42409SLawrence Stewart 402dbc42409SLawrence Stewart /* 4039ec4a4ccSAndre Oppermann * Set the initial slow-start flight size. 404dbc42409SLawrence Stewart * 405cf8f04f4SAndre Oppermann * If a SYN or SYN/ACK was lost and retransmitted, we have to 406cf8f04f4SAndre Oppermann * reduce the initial CWND to one segment as congestion is likely 407cf8f04f4SAndre Oppermann * requiring us to be cautious. 408dbc42409SLawrence Stewart */ 409cf8f04f4SAndre Oppermann if (tp->snd_cwnd == 1) 4100c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; /* SYN(-ACK) lost */ 411dbc42409SLawrence Stewart else 4127dc90a1dSMichael Tuexen tp->snd_cwnd = tcp_compute_initwnd(maxseg); 413dbc42409SLawrence Stewart 414dbc42409SLawrence Stewart if (CC_ALGO(tp)->conn_init != NULL) 415e68b3792SGleb Smirnoff CC_ALGO(tp)->conn_init(&tp->t_ccv); 416dbc42409SLawrence Stewart } 417dbc42409SLawrence Stewart 418dbc42409SLawrence Stewart void inline 419dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type) 420dbc42409SLawrence Stewart { 4219eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(tptoinpcb(tp)); 422dbc42409SLawrence Stewart 423adc56f5aSEdward Tomasz Napierala #ifdef STATS 424adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type); 425adc56f5aSEdward Tomasz Napierala #endif 426adc56f5aSEdward Tomasz Napierala 427dbc42409SLawrence Stewart switch(type) { 428dbc42409SLawrence Stewart case CC_NDUPACK: 429dbc42409SLawrence Stewart if (!IN_FASTRECOVERY(tp->t_flags)) { 430f2512ba1SRui Paulo tp->snd_recover = tp->snd_max; 4313cf38784SMichael Tuexen if (tp->t_flags2 & TF2_ECN_PERMIT) 4323cf38784SMichael Tuexen tp->t_flags2 |= TF2_ECN_SND_CWR; 433f2512ba1SRui Paulo } 434dbc42409SLawrence Stewart break; 435dbc42409SLawrence Stewart case CC_ECN: 436af2fb894SRichard Scheffenegger if (!IN_CONGRECOVERY(tp->t_flags) || 437af2fb894SRichard Scheffenegger /* 438af2fb894SRichard Scheffenegger * Allow ECN reaction on ACK to CWR, if 439af2fb894SRichard Scheffenegger * that data segment was also CE marked. 440af2fb894SRichard Scheffenegger */ 441af2fb894SRichard Scheffenegger SEQ_GEQ(th->th_ack, tp->snd_recover)) { 442af2fb894SRichard Scheffenegger EXIT_CONGRECOVERY(tp->t_flags); 443dbc42409SLawrence Stewart TCPSTAT_INC(tcps_ecn_rcwnd); 444af2fb894SRichard Scheffenegger tp->snd_recover = tp->snd_max + 1; 4453cf38784SMichael Tuexen if (tp->t_flags2 & TF2_ECN_PERMIT) 4463cf38784SMichael Tuexen tp->t_flags2 |= TF2_ECN_SND_CWR; 447dbc42409SLawrence Stewart } 448dbc42409SLawrence Stewart break; 449dbc42409SLawrence Stewart case CC_RTO: 450dbc42409SLawrence Stewart tp->t_dupacks = 0; 451dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 452dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 4539cc711c9SMichael Tuexen if (tp->t_flags2 & TF2_ECN_PERMIT) 4549cc711c9SMichael Tuexen tp->t_flags2 |= TF2_ECN_SND_CWR; 455dbc42409SLawrence Stewart break; 456dbc42409SLawrence Stewart case CC_RTO_ERR: 457dbc42409SLawrence Stewart TCPSTAT_INC(tcps_sndrexmitbad); 458dbc42409SLawrence Stewart /* RTO was unnecessary, so reset everything. */ 459dbc42409SLawrence Stewart tp->snd_cwnd = tp->snd_cwnd_prev; 460dbc42409SLawrence Stewart tp->snd_ssthresh = tp->snd_ssthresh_prev; 461dbc42409SLawrence Stewart tp->snd_recover = tp->snd_recover_prev; 462dbc42409SLawrence Stewart if (tp->t_flags & TF_WASFRECOVERY) 463dbc42409SLawrence Stewart ENTER_FASTRECOVERY(tp->t_flags); 464dbc42409SLawrence Stewart if (tp->t_flags & TF_WASCRECOVERY) 465dbc42409SLawrence Stewart ENTER_CONGRECOVERY(tp->t_flags); 466dbc42409SLawrence Stewart tp->snd_nxt = tp->snd_max; 467672dc4aeSJohn Baldwin tp->t_flags &= ~TF_PREVVALID; 468dbc42409SLawrence Stewart tp->t_badrxtwin = 0; 469dbc42409SLawrence Stewart break; 470dbc42409SLawrence Stewart } 471fcea1cc9SRichard Scheffenegger if (SEQ_LT(tp->snd_fack, tp->snd_una) || 472fcea1cc9SRichard Scheffenegger SEQ_GT(tp->snd_fack, tp->snd_max)) { 473fcea1cc9SRichard Scheffenegger tp->snd_fack = tp->snd_una; 474fcea1cc9SRichard Scheffenegger } 475dbc42409SLawrence Stewart 476dbc42409SLawrence Stewart if (CC_ALGO(tp)->cong_signal != NULL) { 477dbc42409SLawrence Stewart if (th != NULL) 478e68b3792SGleb Smirnoff tp->t_ccv.curack = th->th_ack; 479e68b3792SGleb Smirnoff CC_ALGO(tp)->cong_signal(&tp->t_ccv, type); 480dbc42409SLawrence Stewart } 481dbc42409SLawrence Stewart } 482dbc42409SLawrence Stewart 48355bceb1eSRandall Stewart void inline 484dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th) 485dbc42409SLawrence Stewart { 4869eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(tptoinpcb(tp)); 487dbc42409SLawrence Stewart 488dbc42409SLawrence Stewart if (CC_ALGO(tp)->post_recovery != NULL) { 48940fdc6d2SRichard Scheffenegger if (SEQ_LT(tp->snd_fack, th->th_ack) || 49040fdc6d2SRichard Scheffenegger SEQ_GT(tp->snd_fack, tp->snd_max)) { 49140fdc6d2SRichard Scheffenegger tp->snd_fack = th->th_ack; 49240fdc6d2SRichard Scheffenegger } 493e68b3792SGleb Smirnoff tp->t_ccv.curack = th->th_ack; 494e68b3792SGleb Smirnoff CC_ALGO(tp)->post_recovery(&tp->t_ccv); 495dbc42409SLawrence Stewart } 4960b3f9e43SRichard Scheffenegger EXIT_RECOVERY(tp->t_flags); 4970b3f9e43SRichard Scheffenegger 498dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 49948be5b97SRichard Scheffenegger tp->sackhint.delivered_data = 0; 500429f14f8SRichard Scheffenegger tp->sackhint.prr_delivered = 0; 501e5313869SRichard Scheffenegger tp->sackhint.prr_out = 0; 502dbc42409SLawrence Stewart } 5030312fbe9SPoul-Henning Kamp 504df8bae1dSRodney W. Grimes /* 505262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 50685536381SHiren Panchasara * following conditions are met: 50785536381SHiren Panchasara * - There is no delayed ack timer in progress. 50885536381SHiren Panchasara * - Our last ack wasn't a 0-sized window. We never want to delay 50985536381SHiren Panchasara * the ack that opens up a 0-sized window. 51085536381SHiren Panchasara * - LRO wasn't used for this segment. We make sure by checking that the 51185536381SHiren Panchasara * segment size is not larger than the MSS. 512d8c85a26SJonathan Lemon */ 513c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen) \ 514b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 515a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 5160c39d38dSGleb Smirnoff (tlen <= tp->t_maxseg) && \ 517603724d3SBjoern A. Zeeb (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 518d8c85a26SJonathan Lemon 5194ad24737SRandall Stewart void inline 5205d8fd932SRandall Stewart cc_ecnpkt_handler_flags(struct tcpcb *tp, uint16_t flags, uint8_t iptos) 52164807b30SHiren Panchasara { 5229eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(tptoinpcb(tp)); 52364807b30SHiren Panchasara 52464807b30SHiren Panchasara if (CC_ALGO(tp)->ecnpkt_handler != NULL) { 52564807b30SHiren Panchasara switch (iptos & IPTOS_ECN_MASK) { 52664807b30SHiren Panchasara case IPTOS_ECN_CE: 527e68b3792SGleb Smirnoff tp->t_ccv.flags |= CCF_IPHDR_CE; 52864807b30SHiren Panchasara break; 52964807b30SHiren Panchasara case IPTOS_ECN_ECT0: 53083a2839fSMichael Tuexen /* FALLTHROUGH */ 53164807b30SHiren Panchasara case IPTOS_ECN_ECT1: 53283a2839fSMichael Tuexen /* FALLTHROUGH */ 53383a2839fSMichael Tuexen case IPTOS_ECN_NOTECT: 534e68b3792SGleb Smirnoff tp->t_ccv.flags &= ~CCF_IPHDR_CE; 53564807b30SHiren Panchasara break; 53664807b30SHiren Panchasara } 53764807b30SHiren Panchasara 5385d8fd932SRandall Stewart if (flags & TH_CWR) 539e68b3792SGleb Smirnoff tp->t_ccv.flags |= CCF_TCPHDR_CWR; 54064807b30SHiren Panchasara else 541e68b3792SGleb Smirnoff tp->t_ccv.flags &= ~CCF_TCPHDR_CWR; 54264807b30SHiren Panchasara 543e68b3792SGleb Smirnoff CC_ALGO(tp)->ecnpkt_handler(&tp->t_ccv); 54464807b30SHiren Panchasara 545e68b3792SGleb Smirnoff if (tp->t_ccv.flags & CCF_ACKNOW) { 54664807b30SHiren Panchasara tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 547e11c9783SMichael Tuexen tp->t_flags |= TF_ACKNOW; 548e11c9783SMichael Tuexen } 54964807b30SHiren Panchasara } 55064807b30SHiren Panchasara } 55164807b30SHiren Panchasara 5525d8fd932SRandall Stewart void inline 5535d8fd932SRandall Stewart cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos) 5545d8fd932SRandall Stewart { 5551ebf4607SRichard Scheffenegger cc_ecnpkt_handler_flags(tp, tcp_get_flags(th), iptos); 5565d8fd932SRandall Stewart } 5575d8fd932SRandall Stewart 558df8bae1dSRodney W. Grimes /* 5598d573cc1SAndre Oppermann * TCP input handling is split into multiple parts: 5608d573cc1SAndre Oppermann * tcp6_input is a thin wrapper around tcp_input for the extended 5618d573cc1SAndre Oppermann * ip6_protox[] call format in ip6_input 5628d573cc1SAndre Oppermann * tcp_input handles primary segment validation, inpcb lookup and 5638d573cc1SAndre Oppermann * SYN processing on listen sockets 5648d573cc1SAndre Oppermann * tcp_do_segment processes the ACK and text of the segment for 5658d573cc1SAndre Oppermann * establishing, established and closing connections 566df8bae1dSRodney W. Grimes */ 567fb59c426SYoshinobu Inoue #ifdef INET6 568fb59c426SYoshinobu Inoue int 5699e644c23SMichael Tuexen tcp6_input_with_port(struct mbuf **mp, int *offp, int proto, uint16_t port) 570fb59c426SYoshinobu Inoue { 571503f4e47SBjoern A. Zeeb struct mbuf *m; 57233841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 5733e88eb90SAndrey V. Elsukov struct ip6_hdr *ip6; 574fb59c426SYoshinobu Inoue 575503f4e47SBjoern A. Zeeb m = *mp; 576a4adf6ccSBjoern A. Zeeb if (m->m_len < *offp + sizeof(struct tcphdr)) { 5774e619b17SBjoern A. Zeeb m = m_pullup(m, *offp + sizeof(struct tcphdr)); 5784e619b17SBjoern A. Zeeb if (m == NULL) { 5794e619b17SBjoern A. Zeeb *mp = m; 5804e619b17SBjoern A. Zeeb TCPSTAT_INC(tcps_rcvshort); 5814e619b17SBjoern A. Zeeb return (IPPROTO_DONE); 5824e619b17SBjoern A. Zeeb } 583a4adf6ccSBjoern A. Zeeb } 584fb59c426SYoshinobu Inoue 585fb59c426SYoshinobu Inoue /* 586fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 587fb59c426SYoshinobu Inoue * better place to put this in? 588fb59c426SYoshinobu Inoue */ 5893e88eb90SAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 5908268d82cSAlexander V. Chernikov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false); 59133841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 592fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 593fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 594a8fe77d8SBjoern A. Zeeb *mp = NULL; 5958f5a8818SKevin Lo return (IPPROTO_DONE); 596fb59c426SYoshinobu Inoue } 597fb59c426SYoshinobu Inoue 598a8fe77d8SBjoern A. Zeeb *mp = m; 5999e644c23SMichael Tuexen return (tcp_input_with_port(mp, offp, proto, port)); 6009e644c23SMichael Tuexen } 6019e644c23SMichael Tuexen 6029e644c23SMichael Tuexen int 6039e644c23SMichael Tuexen tcp6_input(struct mbuf **mp, int *offp, int proto) 6049e644c23SMichael Tuexen { 6059e644c23SMichael Tuexen 6069e644c23SMichael Tuexen return(tcp6_input_with_port(mp, offp, proto, 0)); 607fb59c426SYoshinobu Inoue } 608b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 609fb59c426SYoshinobu Inoue 6108f5a8818SKevin Lo int 6119e644c23SMichael Tuexen tcp_input_with_port(struct mbuf **mp, int *offp, int proto, uint16_t port) 612df8bae1dSRodney W. Grimes { 6138f5a8818SKevin Lo struct mbuf *m = *mp; 614b287c6c7SBjoern A. Zeeb struct tcphdr *th = NULL; 615ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 616ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 617302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 618302ce8d6SAndre Oppermann struct socket *so = NULL; 619e79adb8eSGarrett Wollman u_char *optp = NULL; 6208f5a8818SKevin Lo int off0; 62126f9a767SRodney W. Grimes int optlen = 0; 622b287c6c7SBjoern A. Zeeb #ifdef INET 623b287c6c7SBjoern A. Zeeb int len; 624c4556b2fSAndrey V. Elsukov uint8_t ipttl; 625b287c6c7SBjoern A. Zeeb #endif 626b287c6c7SBjoern A. Zeeb int tlen = 0, off; 627fb59c426SYoshinobu Inoue int drop_hdrlen; 628ad3f9ab3SAndre Oppermann int thflags; 629302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 63008d9c920SGleb Smirnoff int lookupflag; 6311d7ee746SKurt Lidl uint8_t iptos; 632c1de64a4SAndrey V. Elsukov struct m_tag *fwd_tag = NULL; 633c068736aSJeffrey Hsu #ifdef INET6 634302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 635c068736aSJeffrey Hsu int isipv6; 636c068736aSJeffrey Hsu #else 637a160e630SAndre Oppermann const void *ip6 = NULL; 638b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 639302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 640a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 641c068736aSJeffrey Hsu 642d40c0d47SGleb Smirnoff NET_EPOCH_ASSERT(); 643d40c0d47SGleb Smirnoff 644fb59c426SYoshinobu Inoue #ifdef INET6 645fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 646fb59c426SYoshinobu Inoue #endif 647a0292f23SGarrett Wollman 6488f5a8818SKevin Lo off0 = *offp; 6498f5a8818SKevin Lo m = *mp; 6508f5a8818SKevin Lo *mp = NULL; 651302ce8d6SAndre Oppermann to.to_flags = 0; 65278b50714SRobert Watson TCPSTAT_INC(tcps_rcvtotal); 653fb59c426SYoshinobu Inoue 654513f2e2eSGleb Smirnoff m->m_pkthdr.tcp_tun_port = port; 65504d3a452SHajimu UMEMOTO #ifdef INET6 656b287c6c7SBjoern A. Zeeb if (isipv6) { 657fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 65845747ba5SBjoern A. Zeeb th = (struct tcphdr *)((caddr_t)ip6 + off0); 659fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 6609e644c23SMichael Tuexen if (port) 6619e644c23SMichael Tuexen goto skip6_csum; 662356ab07eSBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) { 66345747ba5SBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 66445747ba5SBjoern A. Zeeb th->th_sum = m->m_pkthdr.csum_data; 66545747ba5SBjoern A. Zeeb else 66645747ba5SBjoern A. Zeeb th->th_sum = in6_cksum_pseudo(ip6, tlen, 66745747ba5SBjoern A. Zeeb IPPROTO_TCP, m->m_pkthdr.csum_data); 66845747ba5SBjoern A. Zeeb th->th_sum ^= 0xffff; 66945747ba5SBjoern A. Zeeb } else 67045747ba5SBjoern A. Zeeb th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen); 67145747ba5SBjoern A. Zeeb if (th->th_sum) { 67278b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 673fb59c426SYoshinobu Inoue goto drop; 674fb59c426SYoshinobu Inoue } 6759e644c23SMichael Tuexen skip6_csum: 67633841545SHajimu UMEMOTO /* 67733841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 67833841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 67933841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 68033841545SHajimu UMEMOTO * 68133841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 68233841545SHajimu UMEMOTO * already dropped in ip6_input. 68333841545SHajimu UMEMOTO */ 684713264f6SMark Johnston KASSERT(!IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst), 685713264f6SMark Johnston ("%s: unspecified destination v6 address", __func__)); 68633841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 68778e6c3aaSGleb Smirnoff IP6STAT_INC(ip6s_badscope); /* XXX */ 68833841545SHajimu UMEMOTO goto drop; 68933841545SHajimu UMEMOTO } 690bb4a7d94SKristof Provost iptos = IPV6_TRAFFIC_CLASS(ip6); 691b287c6c7SBjoern A. Zeeb } 69204d3a452SHajimu UMEMOTO #endif 693b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 694b287c6c7SBjoern A. Zeeb else 695b287c6c7SBjoern A. Zeeb #endif 696b287c6c7SBjoern A. Zeeb #ifdef INET 697b287c6c7SBjoern A. Zeeb { 698df8bae1dSRodney W. Grimes /* 699df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 700df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 701df8bae1dSRodney W. Grimes */ 702fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 703105bd211SGleb Smirnoff ip_stripoptions(m); 704fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 705fb59c426SYoshinobu Inoue } 706df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 7074dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 7084dfdffe9SAndre Oppermann == NULL) { 70978b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 7108f5a8818SKevin Lo return (IPPROTO_DONE); 711df8bae1dSRodney W. Grimes } 712df8bae1dSRodney W. Grimes } 713fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 714db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 7158ad458a4SGleb Smirnoff tlen = ntohs(ip->ip_len) - off0; 716df8bae1dSRodney W. Grimes 7171d7ee746SKurt Lidl iptos = ip->ip_tos; 7189e644c23SMichael Tuexen if (port) 7199e644c23SMichael Tuexen goto skip_csum; 720db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 721db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 722db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 723db4f9cc7SJonathan Lemon else 724db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 725c068736aSJeffrey Hsu ip->ip_dst.s_addr, 7268f134647SGleb Smirnoff htonl(m->m_pkthdr.csum_data + tlen + 727c068736aSJeffrey Hsu IPPROTO_TCP)); 728db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 729db4f9cc7SJonathan Lemon } else { 7308f134647SGleb Smirnoff struct ipovly *ipov = (struct ipovly *)ip; 7318f134647SGleb Smirnoff 732df8bae1dSRodney W. Grimes /* 733df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 734df8bae1dSRodney W. Grimes */ 7358f134647SGleb Smirnoff len = off0 + tlen; 736c4556b2fSAndrey V. Elsukov ipttl = ip->ip_ttl; 737fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 7388f134647SGleb Smirnoff ipov->ih_len = htons(tlen); 739fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 74057f60867SMark Johnston /* Reset length for SDT probes. */ 7411d7ee746SKurt Lidl ip->ip_len = htons(len); 7421d7ee746SKurt Lidl /* Reset TOS bits */ 7431d7ee746SKurt Lidl ip->ip_tos = iptos; 7441d7ee746SKurt Lidl /* Re-initialization for later version check */ 745c4556b2fSAndrey V. Elsukov ip->ip_ttl = ipttl; 7461d7ee746SKurt Lidl ip->ip_v = IPVERSION; 747b2fe54bcSAndrey V. Elsukov ip->ip_hl = off0 >> 2; 748db4f9cc7SJonathan Lemon } 7499e644c23SMichael Tuexen skip_csum: 7509e644c23SMichael Tuexen if (th->th_sum && (port == 0)) { 75178b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 752df8bae1dSRodney W. Grimes goto drop; 753df8bae1dSRodney W. Grimes } 754713264f6SMark Johnston KASSERT(ip->ip_dst.s_addr != INADDR_ANY, 755713264f6SMark Johnston ("%s: unspecified destination v4 address", __func__)); 756713264f6SMark Johnston if (__predict_false(ip->ip_src.s_addr == INADDR_ANY)) { 75778e6c3aaSGleb Smirnoff IPSTAT_INC(ips_badaddr); 758713264f6SMark Johnston goto drop; 759713264f6SMark Johnston } 760fb59c426SYoshinobu Inoue } 761b287c6c7SBjoern A. Zeeb #endif /* INET */ 762df8bae1dSRodney W. Grimes 763df8bae1dSRodney W. Grimes /* 764df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 765df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 766df8bae1dSRodney W. Grimes */ 767fb59c426SYoshinobu Inoue off = th->th_off << 2; 768df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 76978b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadoff); 770df8bae1dSRodney W. Grimes goto drop; 771df8bae1dSRodney W. Grimes } 772fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 773df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 77404d3a452SHajimu UMEMOTO #ifdef INET6 775b287c6c7SBjoern A. Zeeb if (isipv6) { 776a4adf6ccSBjoern A. Zeeb if (m->m_len < off0 + off) { 7774e619b17SBjoern A. Zeeb m = m_pullup(m, off0 + off); 7784e619b17SBjoern A. Zeeb if (m == NULL) { 7794e619b17SBjoern A. Zeeb TCPSTAT_INC(tcps_rcvshort); 7804e619b17SBjoern A. Zeeb return (IPPROTO_DONE); 7814e619b17SBjoern A. Zeeb } 782a4adf6ccSBjoern A. Zeeb } 783fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 784fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 785b287c6c7SBjoern A. Zeeb } 78604d3a452SHajimu UMEMOTO #endif 787b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 788b287c6c7SBjoern A. Zeeb else 789b287c6c7SBjoern A. Zeeb #endif 790b287c6c7SBjoern A. Zeeb #ifdef INET 791b287c6c7SBjoern A. Zeeb { 792df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 793c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 7944dfdffe9SAndre Oppermann == NULL) { 79578b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 7968f5a8818SKevin Lo return (IPPROTO_DONE); 797df8bae1dSRodney W. Grimes } 798fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 799fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 800fb59c426SYoshinobu Inoue } 801df8bae1dSRodney W. Grimes } 802b287c6c7SBjoern A. Zeeb #endif 803df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 804fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 805df8bae1dSRodney W. Grimes } 8061ebf4607SRichard Scheffenegger thflags = tcp_get_flags(th); 807df8bae1dSRodney W. Grimes 808e46cd3d4SDag-Erling Smørgrav /* 809df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 810df8bae1dSRodney W. Grimes */ 8112903309aSAttilio Rao tcp_fields_to_host(th); 812df8bae1dSRodney W. Grimes 813df8bae1dSRodney W. Grimes /* 814794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 815755c1f07SAndras Olah */ 816fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 817755c1f07SAndras Olah 818755c1f07SAndras Olah /* 8198d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 8208d573cc1SAndre Oppermann */ 821b8056faeSGleb Smirnoff if ( 822b8056faeSGleb Smirnoff #ifdef INET6 823b8056faeSGleb Smirnoff (isipv6 && (m->m_flags & M_IP6_NEXTHOP)) 824b8056faeSGleb Smirnoff #ifdef INET 825b8056faeSGleb Smirnoff || (!isipv6 && (m->m_flags & M_IP_NEXTHOP)) 826b8056faeSGleb Smirnoff #endif 827b8056faeSGleb Smirnoff #endif 828b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6) 829b8056faeSGleb Smirnoff (m->m_flags & M_IP_NEXTHOP) 830b8056faeSGleb Smirnoff #endif 831b8056faeSGleb Smirnoff ) 8329b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 8339b932e9eSAndre Oppermann 83408d9c920SGleb Smirnoff /* 8351db08fbeSGleb Smirnoff * For initial SYN packets we don't need write lock on matching 8361db08fbeSGleb Smirnoff * PCB, be it a listening one or a synchronized one. The packet 8371db08fbeSGleb Smirnoff * shall not modify its state. 83808d9c920SGleb Smirnoff */ 839d88eb465SGleb Smirnoff lookupflag = INPLOOKUP_WILDCARD | 840d88eb465SGleb Smirnoff ((thflags & (TH_ACK|TH_SYN)) == TH_SYN ? 841*5dc99e9bSMark Johnston INPLOOKUP_RLOCKPCB : INPLOOKUP_WLOCKPCB) | 842*5dc99e9bSMark Johnston (V_tcp_bind_all_fibs ? 0 : INPLOOKUP_FIB); 843ce7ad664SEd Maste findpcb: 844df9de82fSMichael Tuexen tp = NULL; 8458a006adbSBjoern A. Zeeb #ifdef INET6 8468a006adbSBjoern A. Zeeb if (isipv6 && fwd_tag != NULL) { 8478a006adbSBjoern A. Zeeb struct sockaddr_in6 *next_hop6; 8488a006adbSBjoern A. Zeeb 8498a006adbSBjoern A. Zeeb next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1); 8508a006adbSBjoern A. Zeeb /* 8518a006adbSBjoern A. Zeeb * Transparently forwarded. Pretend to be the destination. 8528a006adbSBjoern A. Zeeb * Already got one like this? 8538a006adbSBjoern A. Zeeb */ 8548a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, 8558a006adbSBjoern A. Zeeb &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport, 856aab8c844SGleb Smirnoff lookupflag & ~INPLOOKUP_WILDCARD, m->m_pkthdr.rcvif, m); 8578a006adbSBjoern A. Zeeb if (!inp) { 8588a006adbSBjoern A. Zeeb /* 8598a006adbSBjoern A. Zeeb * It's new. Try to find the ambushing socket. 8608a006adbSBjoern A. Zeeb * Because we've rewritten the destination address, 8618a006adbSBjoern A. Zeeb * any hardware-generated hash is ignored. 8628a006adbSBjoern A. Zeeb */ 8638a006adbSBjoern A. Zeeb inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src, 8648a006adbSBjoern A. Zeeb th->th_sport, &next_hop6->sin6_addr, 8658a006adbSBjoern A. Zeeb next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) : 866d88eb465SGleb Smirnoff th->th_dport, lookupflag, m->m_pkthdr.rcvif); 8678a006adbSBjoern A. Zeeb } 868c1de64a4SAndrey V. Elsukov } else if (isipv6) { 8698a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src, 870d88eb465SGleb Smirnoff th->th_sport, &ip6->ip6_dst, th->th_dport, lookupflag, 871d88eb465SGleb Smirnoff m->m_pkthdr.rcvif, m); 8728a006adbSBjoern A. Zeeb } 8738a006adbSBjoern A. Zeeb #endif /* INET6 */ 8748a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET) 8758a006adbSBjoern A. Zeeb else 8768a006adbSBjoern A. Zeeb #endif 8778a006adbSBjoern A. Zeeb #ifdef INET 8788a006adbSBjoern A. Zeeb if (fwd_tag != NULL) { 8799b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 8809b932e9eSAndre Oppermann 8819b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 882f9e354dfSJulian Elischer /* 8832b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 884f9e354dfSJulian Elischer * already got one like this? 885f9e354dfSJulian Elischer */ 886d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport, 887aab8c844SGleb Smirnoff ip->ip_dst, th->th_dport, lookupflag & ~INPLOOKUP_WILDCARD, 888aab8c844SGleb Smirnoff m->m_pkthdr.rcvif, m); 889fa046d87SRobert Watson if (!inp) { 890fa046d87SRobert Watson /* 891fa046d87SRobert Watson * It's new. Try to find the ambushing socket. 892d3c1f003SRobert Watson * Because we've rewritten the destination address, 893d3c1f003SRobert Watson * any hardware-generated hash is ignored. 894fa046d87SRobert Watson */ 895fa046d87SRobert Watson inp = in_pcblookup(&V_tcbinfo, ip->ip_src, 896fa046d87SRobert Watson th->th_sport, next_hop->sin_addr, 897fa046d87SRobert Watson next_hop->sin_port ? ntohs(next_hop->sin_port) : 898d88eb465SGleb Smirnoff th->th_dport, lookupflag, m->m_pkthdr.rcvif); 899f9e354dfSJulian Elischer } 900b10fbdeaSAndre Oppermann } else 901d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, 902d88eb465SGleb Smirnoff th->th_sport, ip->ip_dst, th->th_dport, lookupflag, 903d88eb465SGleb Smirnoff m->m_pkthdr.rcvif, m); 9048a006adbSBjoern A. Zeeb #endif /* INET */ 905f9e354dfSJulian Elischer 906df8bae1dSRodney W. Grimes /* 907574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 908574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 9098b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 910df8bae1dSRodney W. Grimes */ 911816a3d83SPoul-Henning Kamp if (inp == NULL) { 912d88eb465SGleb Smirnoff if (rstreason != 0) { 913d88eb465SGleb Smirnoff /* We came here after second (safety) lookup. */ 914d88eb465SGleb Smirnoff MPASS((lookupflag & INPLOOKUP_WILDCARD) == 0); 915d88eb465SGleb Smirnoff goto dropwithreset; 916d88eb465SGleb Smirnoff } 917574b6964SAndre Oppermann /* 918574b6964SAndre Oppermann * Log communication attempts to ports that are not 919574b6964SAndre Oppermann * in use. 920574b6964SAndre Oppermann */ 921334fc582SBjoern A. Zeeb if ((V_tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 922334fc582SBjoern A. Zeeb V_tcp_log_in_vain == 2) { 923b7d747ecSAndre Oppermann if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6))) 924df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 925df541e5fSAndre Oppermann "to closed port\n", s, __func__); 9262e4e1b4cSGeoff Rehmet } 927a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 928a57815efSBosko Milekic goto dropwithreset; 929816a3d83SPoul-Henning Kamp } 93008d9c920SGleb Smirnoff INP_LOCK_ASSERT(inp); 931f567d55fSGleb Smirnoff 932c2529042SHans Petter Selasky if ((inp->inp_flowtype == M_HASHTYPE_NONE) && 933f71cb9f7SGleb Smirnoff !SOLISTENING(inp->inp_socket)) { 934e5738ee0SCheng Cui if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) { 93580cb9f21SKip Macy inp->inp_flowid = m->m_pkthdr.flowid; 9362f719932SAdrian Chadd inp->inp_flowtype = M_HASHTYPE_GET(m); 937e5738ee0SCheng Cui #ifdef RSS 938e5738ee0SCheng Cui } else { 939e5738ee0SCheng Cui /* assign flowid by software RSS hash */ 940e5738ee0SCheng Cui #ifdef INET6 941e5738ee0SCheng Cui if (isipv6) { 942e5738ee0SCheng Cui rss_proto_software_hash_v6(&inp->in6p_faddr, 943e5738ee0SCheng Cui &inp->in6p_laddr, 944e5738ee0SCheng Cui inp->inp_fport, 945e5738ee0SCheng Cui inp->inp_lport, 946e5738ee0SCheng Cui IPPROTO_TCP, 947e5738ee0SCheng Cui &inp->inp_flowid, 948e5738ee0SCheng Cui &inp->inp_flowtype); 949e5738ee0SCheng Cui } else 950e5738ee0SCheng Cui #endif /* INET6 */ 951e5738ee0SCheng Cui { 952e5738ee0SCheng Cui rss_proto_software_hash_v4(inp->inp_faddr, 953e5738ee0SCheng Cui inp->inp_laddr, 954e5738ee0SCheng Cui inp->inp_fport, 955e5738ee0SCheng Cui inp->inp_lport, 956e5738ee0SCheng Cui IPPROTO_TCP, 957e5738ee0SCheng Cui &inp->inp_flowid, 958e5738ee0SCheng Cui &inp->inp_flowtype); 959e5738ee0SCheng Cui } 960e5738ee0SCheng Cui #endif /* RSS */ 961e5738ee0SCheng Cui } 96280cb9f21SKip Macy } 963fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 964a2589465SKen Smith #ifdef INET6 965fcf59617SAndrey V. Elsukov if (isipv6 && IPSEC_ENABLED(ipv6) && 966fcf59617SAndrey V. Elsukov IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) { 967a2589465SKen Smith goto dropunlock; 968a2589465SKen Smith } 969fcf59617SAndrey V. Elsukov #ifdef INET 970fcf59617SAndrey V. Elsukov else 971fcf59617SAndrey V. Elsukov #endif 972fcf59617SAndrey V. Elsukov #endif /* INET6 */ 973fcf59617SAndrey V. Elsukov #ifdef INET 974fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4) && 975fcf59617SAndrey V. Elsukov IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) { 976fcf59617SAndrey V. Elsukov goto dropunlock; 977fcf59617SAndrey V. Elsukov } 978fcf59617SAndrey V. Elsukov #endif /* INET */ 979a2589465SKen Smith #endif /* IPSEC */ 980a2589465SKen Smith 9818d573cc1SAndre Oppermann /* 9828d573cc1SAndre Oppermann * Check the minimum TTL for socket. 9838d573cc1SAndre Oppermann */ 98434f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 98534f83c52SGeorge V. Neville-Neil #ifdef INET6 9862d8868dbSJonathan T. Looney if (isipv6) { 9872d8868dbSJonathan T. Looney if (inp->inp_ip_minttl > ip6->ip6_hlim) 988302ce8d6SAndre Oppermann goto dropunlock; 9892d8868dbSJonathan T. Looney } else 99034f83c52SGeorge V. Neville-Neil #endif 99134f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 992302ce8d6SAndre Oppermann goto dropunlock; 99334f83c52SGeorge V. Neville-Neil } 994936cd18dSAndre Oppermann 9950d744519SGleb Smirnoff tp = intotcpcb(inp); 9960d744519SGleb Smirnoff switch (tp->t_state) { 9970d744519SGleb Smirnoff case TCPS_TIME_WAIT: 998340c35deSJonathan Lemon /* 9990d744519SGleb Smirnoff * A previous connection in TIMEWAIT state is supposed to catch 10000d744519SGleb Smirnoff * stray or duplicate segments arriving late. If this segment 10010d744519SGleb Smirnoff * was a legitimate new connection attempt, the old INPCB gets 10020d744519SGleb Smirnoff * removed and we can try again to find a listening socket. 1003340c35deSJonathan Lemon */ 1004283c76c7SMichael Tuexen tcp_dooptions(&to, optp, optlen, 1005283c76c7SMichael Tuexen (thflags & TH_SYN) ? TO_SYN : 0); 10068d573cc1SAndre Oppermann /* 10070d744519SGleb Smirnoff * tcp_twcheck unlocks the inp always, and frees the m if fails. 10088d573cc1SAndre Oppermann */ 10092104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 1010340c35deSJonathan Lemon goto findpcb; 10118f5a8818SKevin Lo return (IPPROTO_DONE); 10120d744519SGleb Smirnoff case TCPS_CLOSED: 1013794235b7SAndre Oppermann /* 1014794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 1015794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 1016794235b7SAndre Oppermann * segment and send an appropriate response. 1017794235b7SAndre Oppermann */ 1018a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 1019a57815efSBosko Milekic goto dropwithreset; 102009f81a46SBosko Milekic } 1021df8bae1dSRodney W. Grimes 10229e644c23SMichael Tuexen if ((tp->t_port != port) && (tp->t_state > TCPS_LISTEN)) { 10239e644c23SMichael Tuexen rstreason = BANDLIM_RST_CLOSEDPORT; 10249e644c23SMichael Tuexen goto dropwithreset; 10259e644c23SMichael Tuexen } 10269e644c23SMichael Tuexen 102709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 102809fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 102909fe6320SNavdeep Parhar tcp_offload_input(tp, m); 103009fe6320SNavdeep Parhar m = NULL; /* consumed by the TOE driver */ 103109fe6320SNavdeep Parhar goto dropunlock; 103209fe6320SNavdeep Parhar } 103309fe6320SNavdeep Parhar #endif 103409fe6320SNavdeep Parhar 1035c488362eSRobert Watson #ifdef MAC 103630d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 1037302ce8d6SAndre Oppermann goto dropunlock; 1038c488362eSRobert Watson #endif 1039a557af22SRobert Watson so = inp->inp_socket; 1040302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 1041db33b3e6SAndre Oppermann /* 1042db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 1043db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 1044a7c7f2a7SJohn Baldwin * attempt or the completion of a previous one. 1045db33b3e6SAndre Oppermann */ 1046f4bb1869SMark Johnston KASSERT(tp->t_state == TCPS_LISTEN || !SOLISTENING(so), 104745274760SJonathan T. Looney ("%s: so accepting but tp %p not listening", __func__, tp)); 1048f4bb1869SMark Johnston if (tp->t_state == TCPS_LISTEN && SOLISTENING(so)) { 1049540e8b7eSJeffrey Hsu struct in_conninfo inc; 1050540e8b7eSJeffrey Hsu 10518bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 1052302ce8d6SAndre Oppermann #ifdef INET6 1053be2ac88cSJonathan Lemon if (isipv6) { 1054dcdb4371SBjoern A. Zeeb inc.inc_flags |= INC_ISIPV6; 10555dff1c38SMichael Tuexen if (inp->inp_inc.inc_flags & INC_IPV6MINMTU) 10565dff1c38SMichael Tuexen inc.inc_flags |= INC_IPV6MINMTU; 1057be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 1058be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 1059302ce8d6SAndre Oppermann } else 1060302ce8d6SAndre Oppermann #endif 1061302ce8d6SAndre Oppermann { 1062be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 1063be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 1064be2ac88cSJonathan Lemon } 1065be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 1066be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 10677973fba3SJulian Elischer inc.inc_fibnum = so->so_fibnum; 1068be2ac88cSJonathan Lemon 1069fb59c426SYoshinobu Inoue /* 10708d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 10718d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 10728d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 1073fb59c426SYoshinobu Inoue */ 1074be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 1075bf6d304aSAndre Oppermann /* 1076bf6d304aSAndre Oppermann * Parse the TCP options here because 1077bf6d304aSAndre Oppermann * syncookies need access to the reflected 1078bf6d304aSAndre Oppermann * timestamp. 1079bf6d304aSAndre Oppermann */ 1080bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 10819fa198beSAndre Oppermann /* 1082e7231d07SGleb Smirnoff * NB: syncache_expand() doesn't unlock inp. 10839fa198beSAndre Oppermann */ 10849e644c23SMichael Tuexen rstreason = syncache_expand(&inc, &to, th, &so, m, port); 1085fcf59617SAndrey V. Elsukov if (rstreason < 0) { 1086fcf59617SAndrey V. Elsukov /* 1087fcf59617SAndrey V. Elsukov * A failing TCP MD5 signature comparison 1088fcf59617SAndrey V. Elsukov * must result in the segment being dropped 1089fcf59617SAndrey V. Elsukov * and must not produce any response back 1090fcf59617SAndrey V. Elsukov * to the sender. 1091fcf59617SAndrey V. Elsukov */ 1092fcf59617SAndrey V. Elsukov goto dropunlock; 1093fcf59617SAndrey V. Elsukov } else if (rstreason == 0) { 10944195b4afSPaul Traina /* 1095d88eb465SGleb Smirnoff * No syncache entry, or ACK was not for our 1096d88eb465SGleb Smirnoff * SYN/ACK. Do our protection against double 1097d88eb465SGleb Smirnoff * ACK. If peer sent us 2 ACKs, then for the 1098d88eb465SGleb Smirnoff * first one syncache_expand() successfully 1099d88eb465SGleb Smirnoff * converted syncache entry into a socket, 1100d88eb465SGleb Smirnoff * while we were waiting on the inpcb lock. We 1101d88eb465SGleb Smirnoff * don't want to sent RST for the second ACK, 1102d88eb465SGleb Smirnoff * so we perform second lookup without wildcard 1103d88eb465SGleb Smirnoff * match, hoping to find the new socket. If 1104d88eb465SGleb Smirnoff * the ACK is stray indeed, rstreason would 1105d88eb465SGleb Smirnoff * hint the above code that the lookup was a 1106d88eb465SGleb Smirnoff * second attempt. 1107d88eb465SGleb Smirnoff * 11088d573cc1SAndre Oppermann * NB: syncache did its own logging 11098d573cc1SAndre Oppermann * of the failure cause. 11104195b4afSPaul Traina */ 1111d88eb465SGleb Smirnoff INP_WUNLOCK(inp); 1112be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 1113d88eb465SGleb Smirnoff lookupflag &= ~INPLOOKUP_WILDCARD; 1114d88eb465SGleb Smirnoff goto findpcb; 1115be2ac88cSJonathan Lemon } 111609c305ebSPatrick Kelsey tfo_socket_result: 1117f76fcf6dSJeffrey Hsu if (so == NULL) { 1118be2ac88cSJonathan Lemon /* 1119e207f800SAndre Oppermann * We completed the 3-way handshake 1120e207f800SAndre Oppermann * but could not allocate a socket 1121e207f800SAndre Oppermann * either due to memory shortage, 1122e207f800SAndre Oppermann * listen queue length limits or 1123a160e630SAndre Oppermann * global socket limits. Send RST 1124a160e630SAndre Oppermann * or wait and have the remote end 1125a160e630SAndre Oppermann * retransmit the ACK for another 1126a160e630SAndre Oppermann * try. 1127be2ac88cSJonathan Lemon */ 1128a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1129a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11308d573cc1SAndre Oppermann "Socket allocation failed due to " 11318d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 1132ac957cd2SJulian Elischer s, __func__, 1133ac957cd2SJulian Elischer V_tcp_sc_rst_sock_fail ? 1134ac957cd2SJulian Elischer "sending RST" : "try again"); 1135603724d3SBjoern A. Zeeb if (V_tcp_sc_rst_sock_fail) { 1136e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 1137e207f800SAndre Oppermann goto dropwithreset; 1138a160e630SAndre Oppermann } else 1139a160e630SAndre Oppermann goto dropunlock; 1140f76fcf6dSJeffrey Hsu } 1141be2ac88cSJonathan Lemon /* 1142be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 11438d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 11448d573cc1SAndre Oppermann * created socket and update the tp variable. 114508d9c920SGleb Smirnoff * If we came here via jump to tfo_socket_result, 114608d9c920SGleb Smirnoff * then listening socket is read-locked. 1147be2ac88cSJonathan Lemon */ 114808d9c920SGleb Smirnoff INP_UNLOCK(inp); /* listen socket */ 1149be2ac88cSJonathan Lemon inp = sotoinpcb(so); 1150ff9b006dSJulien Charbon /* 1151ff9b006dSJulien Charbon * New connection inpcb is already locked by 1152ff9b006dSJulien Charbon * syncache_expand(). 1153ff9b006dSJulien Charbon */ 1154ff9b006dSJulien Charbon INP_WLOCK_ASSERT(inp); 1155be2ac88cSJonathan Lemon tp = intotcpcb(inp); 11568d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 11578d573cc1SAndre Oppermann ("%s: ", __func__)); 1158be2ac88cSJonathan Lemon /* 1159302ce8d6SAndre Oppermann * Process the segment and the data it 1160302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 1161302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 1162302ce8d6SAndre Oppermann */ 11636138da62SMichael Tuexen TCP_PROBE5(receive, NULL, tp, m, tp, th); 116435bc0bccSGleb Smirnoff tp->t_fb->tfb_tcp_do_segment(tp, m, th, drop_hdrlen, 116535bc0bccSGleb Smirnoff tlen, iptos); 11668f5a8818SKevin Lo return (IPPROTO_DONE); 1167be2ac88cSJonathan Lemon } 1168a160e630SAndre Oppermann /* 11698d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 11708d573cc1SAndre Oppermann * 1171a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 1172a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 1173a160e630SAndre Oppermann * retransmits. 117419bc77c5SAndre Oppermann * 117519bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 117619bc77c5SAndre Oppermann * causes. 1177a160e630SAndre Oppermann */ 1178a160e630SAndre Oppermann if (thflags & TH_RST) { 11799e644c23SMichael Tuexen syncache_chkrst(&inc, th, m, port); 1180a160e630SAndre Oppermann goto dropunlock; 1181a160e630SAndre Oppermann } 1182a160e630SAndre Oppermann /* 1183a160e630SAndre Oppermann * We can't do anything without SYN. 1184a160e630SAndre Oppermann */ 1185a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 1186a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1187a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1188773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 11898d573cc1SAndre Oppermann s, __func__); 119078b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1191a160e630SAndre Oppermann goto dropunlock; 1192a160e630SAndre Oppermann } 1193a160e630SAndre Oppermann /* 1194a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 1195a160e630SAndre Oppermann */ 1196fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1197a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1198a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11998d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 12008d573cc1SAndre Oppermann s, __func__); 12019e644c23SMichael Tuexen syncache_badack(&inc, port); /* XXX: Not needed! */ 120278b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1203a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1204a57815efSBosko Milekic goto dropwithreset; 12054195b4afSPaul Traina } 1206a160e630SAndre Oppermann /* 1207a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 1208a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 1209a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 1210a160e630SAndre Oppermann * TCP/IP stack. 1211a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 1212a160e630SAndre Oppermann * and is constantly refining its stack detection 1213a160e630SAndre Oppermann * strategies. 12148d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 1215a160e630SAndre Oppermann * and was used by RFC1644. 1216a160e630SAndre Oppermann */ 1217603724d3SBjoern A. Zeeb if ((thflags & TH_FIN) && V_drop_synfin) { 1218a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1219a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1220773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 12218d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 122278b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1223302ce8d6SAndre Oppermann goto dropunlock; 1224ebb0cbeaSPaul Traina } 1225be2ac88cSJonathan Lemon /* 1226be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 1227a160e630SAndre Oppermann * 1228a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 1229a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 1230a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 1231be2ac88cSJonathan Lemon */ 1232a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 1233a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 1234a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 1235a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 123608d9c920SGleb Smirnoff INP_RLOCK_ASSERT(inp); 123733841545SHajimu UMEMOTO #ifdef INET6 123833841545SHajimu UMEMOTO /* 123933841545SHajimu UMEMOTO * If deprecated address is forbidden, 124033841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 124133841545SHajimu UMEMOTO * address to prevent any new inbound connection from 124233841545SHajimu UMEMOTO * getting established. 124333841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 124433841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 124533841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 124633841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 124733841545SHajimu UMEMOTO * for the exchange. 124833841545SHajimu UMEMOTO * 124933841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 125033841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 125133841545SHajimu UMEMOTO * SYN in this case. 125233841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 125333841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 125433841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 125533841545SHajimu UMEMOTO * used" 125633841545SHajimu UMEMOTO * 2. use of it with new communication: 125733841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 125833841545SHajimu UMEMOTO * with sufficient scope is available" 125933841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 126033841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 126133841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 126233841545SHajimu UMEMOTO * 126333841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 126433841545SHajimu UMEMOTO * multiple description text for deprecated address 126533841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 126633841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 126733841545SHajimu UMEMOTO */ 1268603724d3SBjoern A. Zeeb if (isipv6 && !V_ip6_use_deprecated) { 126933841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 127033841545SHajimu UMEMOTO 12718268d82cSAlexander V. Chernikov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false); 12728c0fec80SRobert Watson if (ia6 != NULL && 127333841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 1274a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1275a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12768d573cc1SAndre Oppermann "Connection attempt to deprecated " 12778d573cc1SAndre Oppermann "IPv6 address rejected\n", 1278a160e630SAndre Oppermann s, __func__); 127933841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 128033841545SHajimu UMEMOTO goto dropwithreset; 128133841545SHajimu UMEMOTO } 128233841545SHajimu UMEMOTO } 1283b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 128465f28919SJesper Skriver /* 1285db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 12868d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 1287db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 12888d573cc1SAndre Oppermann * If it is from this socket it must be forged. 12898d573cc1SAndre Oppermann * Don't respond if the source or destination is a 12908d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 129193ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 129293ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 129393ec91baSCrist J. Clark * in_broadcast() to find them. 1294be2ac88cSJonathan Lemon */ 12958d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 12968d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 12978d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12988d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 1299773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 1300302ce8d6SAndre Oppermann goto dropunlock; 13018d573cc1SAndre Oppermann } 1302db33b3e6SAndre Oppermann #ifdef INET6 1303b287c6c7SBjoern A. Zeeb if (isipv6) { 1304db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1305a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 1306a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1307a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13088d573cc1SAndre Oppermann "Connection attempt to/from self " 1309773673c1SAndre Oppermann "ignored\n", s, __func__); 1310302ce8d6SAndre Oppermann goto dropunlock; 1311a160e630SAndre Oppermann } 1312be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1313a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 1314a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1315a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13168d573cc1SAndre Oppermann "Connection attempt from/to multicast " 1317773673c1SAndre Oppermann "address ignored\n", s, __func__); 1318302ce8d6SAndre Oppermann goto dropunlock; 1319a160e630SAndre Oppermann } 1320b287c6c7SBjoern A. Zeeb } 1321db33b3e6SAndre Oppermann #endif 1322b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 1323b287c6c7SBjoern A. Zeeb else 1324b287c6c7SBjoern A. Zeeb #endif 1325b287c6c7SBjoern A. Zeeb #ifdef INET 1326b287c6c7SBjoern A. Zeeb { 1327db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1328a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 1329a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1330a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13318d573cc1SAndre Oppermann "Connection attempt from/to self " 1332773673c1SAndre Oppermann "ignored\n", s, __func__); 1333302ce8d6SAndre Oppermann goto dropunlock; 1334a160e630SAndre Oppermann } 1335be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1336be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 13372ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 1338a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 1339a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1340a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13418d573cc1SAndre Oppermann "Connection attempt from/to broad- " 1342773673c1SAndre Oppermann "or multicast address ignored\n", 1343a160e630SAndre Oppermann s, __func__); 1344302ce8d6SAndre Oppermann goto dropunlock; 1345c068736aSJeffrey Hsu } 1346a160e630SAndre Oppermann } 1347b287c6c7SBjoern A. Zeeb #endif 1348be2ac88cSJonathan Lemon /* 1349db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 1350db33b3e6SAndre Oppermann * for syncache. 1351be2ac88cSJonathan Lemon */ 13522b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 1353f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 13548d5719aaSGleb Smirnoff if ((so = syncache_add(&inc, &to, th, inp, so, m, NULL, NULL, 13559e644c23SMichael Tuexen iptos, port)) != NULL) 135609c305ebSPatrick Kelsey goto tfo_socket_result; 135718a75309SPatrick Kelsey 1358be2ac88cSJonathan Lemon /* 13594d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 1360a7c7f2a7SJohn Baldwin * Only the listen socket is unlocked by syncache_add(). 1361be2ac88cSJonathan Lemon */ 13628f5a8818SKevin Lo return (IPPROTO_DONE); 1363f76fcf6dSJeffrey Hsu } 1364fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1365fcf59617SAndrey V. Elsukov if (tp->t_flags & TF_SIGNATURE) { 1366fcf59617SAndrey V. Elsukov tcp_dooptions(&to, optp, optlen, thflags); 1367fcf59617SAndrey V. Elsukov if ((to.to_flags & TOF_SIGNATURE) == 0) { 1368fcf59617SAndrey V. Elsukov TCPSTAT_INC(tcps_sig_err_nosigopt); 13692903309aSAttilio Rao goto dropunlock; 13702903309aSAttilio Rao } 1371fcf59617SAndrey V. Elsukov if (!TCPMD5_ENABLED() || 1372fcf59617SAndrey V. Elsukov TCPMD5_INPUT(m, th, to.to_signature) != 0) 1373fcf59617SAndrey V. Elsukov goto dropunlock; 13742903309aSAttilio Rao } 13752903309aSAttilio Rao #endif 13762b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 137757f60867SMark Johnston 1378302ce8d6SAndre Oppermann /* 13798d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 13808d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 13818d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 13821db08fbeSGleb Smirnoff * 13831db08fbeSGleb Smirnoff * XXXGL: in case of a pure SYN arriving on existing connection 13841db08fbeSGleb Smirnoff * TCP stacks won't need to modify the PCB, they would either drop 13851db08fbeSGleb Smirnoff * the segment silently, or send a challenge ACK. However, we try 13861db08fbeSGleb Smirnoff * to upgrade the lock, because calling convention for stacks is 13871db08fbeSGleb Smirnoff * write-lock on PCB. If upgrade fails, drop the SYN. 1388302ce8d6SAndre Oppermann */ 1389d88eb465SGleb Smirnoff if ((lookupflag & INPLOOKUP_RLOCKPCB) && INP_TRY_UPGRADE(inp) == 0) 13901db08fbeSGleb Smirnoff goto dropunlock; 13911db08fbeSGleb Smirnoff 139235bc0bccSGleb Smirnoff tp->t_fb->tfb_tcp_do_segment(tp, m, th, drop_hdrlen, tlen, iptos); 13938f5a8818SKevin Lo return (IPPROTO_DONE); 1394be2ac88cSJonathan Lemon 1395302ce8d6SAndre Oppermann dropwithreset: 139602d15215SMichael Tuexen /* 139702d15215SMichael Tuexen * When blackholing do not respond with a RST but 139802d15215SMichael Tuexen * completely ignore the segment and drop it. 139902d15215SMichael Tuexen */ 140002d15215SMichael Tuexen if (((rstreason == BANDLIM_RST_OPENPORT && V_blackhole == 3) || 140102d15215SMichael Tuexen (rstreason == BANDLIM_RST_CLOSEDPORT && 140202d15215SMichael Tuexen ((V_blackhole == 1 && (thflags & TH_SYN)) || V_blackhole > 1))) && 140302d15215SMichael Tuexen (V_blackhole_local || ( 140402d15215SMichael Tuexen #ifdef INET6 140502d15215SMichael Tuexen isipv6 ? !in6_localaddr(&ip6->ip6_src) : 140602d15215SMichael Tuexen #endif 140702d15215SMichael Tuexen #ifdef INET 140802d15215SMichael Tuexen !in_localip(ip->ip_src) 140902d15215SMichael Tuexen #else 141002d15215SMichael Tuexen true 141102d15215SMichael Tuexen #endif 141202d15215SMichael Tuexen ))) 141302d15215SMichael Tuexen goto dropunlock; 14142b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 1415302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 1416302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 1417014ea782SRobert Watson 1418302ce8d6SAndre Oppermann dropunlock: 141957f60867SMark Johnston if (m != NULL) 14202b9c9984SGeorge V. Neville-Neil TCP_PROBE5(receive, NULL, tp, m, tp, th); 142157f60867SMark Johnston 14229fa198beSAndre Oppermann if (inp != NULL) 142308d9c920SGleb Smirnoff INP_UNLOCK(inp); 1424014ea782SRobert Watson 1425302ce8d6SAndre Oppermann drop: 1426a160e630SAndre Oppermann if (s != NULL) 1427a160e630SAndre Oppermann free(s, M_TCPLOG); 1428302ce8d6SAndre Oppermann if (m != NULL) 1429302ce8d6SAndre Oppermann m_freem(m); 14308f5a8818SKevin Lo return (IPPROTO_DONE); 1431302ce8d6SAndre Oppermann } 1432302ce8d6SAndre Oppermann 1433e44c1887SSteven Hartland /* 1434e44c1887SSteven Hartland * Automatic sizing of receive socket buffer. Often the send 1435e44c1887SSteven Hartland * buffer size is not optimally adjusted to the actual network 1436e44c1887SSteven Hartland * conditions at hand (delay bandwidth product). Setting the 1437e44c1887SSteven Hartland * buffer size too small limits throughput on links with high 1438e44c1887SSteven Hartland * bandwidth and high delay (eg. trans-continental/oceanic links). 1439e44c1887SSteven Hartland * 1440e44c1887SSteven Hartland * On the receive side the socket buffer memory is only rarely 1441e44c1887SSteven Hartland * used to any significant extent. This allows us to be much 1442e44c1887SSteven Hartland * more aggressive in scaling the receive socket buffer. For 1443e44c1887SSteven Hartland * the case that the buffer space is actually used to a large 1444e44c1887SSteven Hartland * extent and we run out of kernel memory we can simply drop 1445e44c1887SSteven Hartland * the new segments; TCP on the sender will just retransmit it 1446e44c1887SSteven Hartland * later. Setting the buffer size too big may only consume too 1447e44c1887SSteven Hartland * much kernel memory if the application doesn't read() from 1448e44c1887SSteven Hartland * the socket or packet loss or reordering makes use of the 1449e44c1887SSteven Hartland * reassembly queue. 1450e44c1887SSteven Hartland * 1451e44c1887SSteven Hartland * The criteria to step up the receive buffer one notch are: 1452e44c1887SSteven Hartland * 1. Application has not set receive buffer size with 1453e44c1887SSteven Hartland * SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE. 1454560c0586SMichael Tuexen * 2. the number of bytes received during 1/2 of an sRTT 1455560c0586SMichael Tuexen * is at least 3/8 of the current socket buffer size. 1456560c0586SMichael Tuexen * 3. receive buffer size has not hit maximal automatic size; 1457e44c1887SSteven Hartland * 1458560c0586SMichael Tuexen * If all of the criteria are met we increaset the socket buffer 1459560c0586SMichael Tuexen * by a 1/2 (bounded by the max). This allows us to keep ahead 1460560c0586SMichael Tuexen * of slow-start but also makes it so our peer never gets limited 1461560c0586SMichael Tuexen * by our rwnd which we then open up causing a burst. 1462560c0586SMichael Tuexen * 1463560c0586SMichael Tuexen * This algorithm does two steps per RTT at most and only if 1464e44c1887SSteven Hartland * we receive a bulk stream w/o packet losses or reorderings. 1465e44c1887SSteven Hartland * Shrinking the buffer during idle times is not necessary as 1466e44c1887SSteven Hartland * it doesn't consume any memory when idle. 1467e44c1887SSteven Hartland * 1468e44c1887SSteven Hartland * TODO: Only step up if the application is actually serving 1469e44c1887SSteven Hartland * the buffer to better manage the socket buffer resources. 1470e44c1887SSteven Hartland */ 1471e44c1887SSteven Hartland int 1472e44c1887SSteven Hartland tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so, 1473e44c1887SSteven Hartland struct tcpcb *tp, int tlen) 1474e44c1887SSteven Hartland { 1475e44c1887SSteven Hartland int newsize = 0; 1476e44c1887SSteven Hartland 1477e44c1887SSteven Hartland if (V_tcp_do_autorcvbuf && (so->so_rcv.sb_flags & SB_AUTOSIZE) && 1478e44c1887SSteven Hartland tp->t_srtt != 0 && tp->rfbuf_ts != 0 && 1479e44c1887SSteven Hartland TCP_TS_TO_TICKS(tcp_ts_getticks() - tp->rfbuf_ts) > 1480560c0586SMichael Tuexen ((tp->t_srtt >> TCP_RTT_SHIFT)/2)) { 1481560c0586SMichael Tuexen if (tp->rfbuf_cnt > ((so->so_rcv.sb_hiwat / 2)/ 4 * 3) && 1482e44c1887SSteven Hartland so->so_rcv.sb_hiwat < V_tcp_autorcvbuf_max) { 1483560c0586SMichael Tuexen newsize = min((so->so_rcv.sb_hiwat + (so->so_rcv.sb_hiwat/2)), V_tcp_autorcvbuf_max); 1484e44c1887SSteven Hartland } 1485e44c1887SSteven Hartland TCP_PROBE6(receive__autoresize, NULL, tp, m, tp, th, newsize); 1486e44c1887SSteven Hartland 1487e44c1887SSteven Hartland /* Start over with next RTT. */ 1488e44c1887SSteven Hartland tp->rfbuf_ts = 0; 1489e44c1887SSteven Hartland tp->rfbuf_cnt = 0; 1490e44c1887SSteven Hartland } else { 1491e44c1887SSteven Hartland tp->rfbuf_cnt += tlen; /* add up */ 1492e44c1887SSteven Hartland } 1493e44c1887SSteven Hartland return (newsize); 1494e44c1887SSteven Hartland } 1495e44c1887SSteven Hartland 14969e644c23SMichael Tuexen int 14979e644c23SMichael Tuexen tcp_input(struct mbuf **mp, int *offp, int proto) 14989e644c23SMichael Tuexen { 14999e644c23SMichael Tuexen return(tcp_input_with_port(mp, offp, proto, 0)); 15009e644c23SMichael Tuexen } 15019e644c23SMichael Tuexen 1502c348e880SGleb Smirnoff static void 1503c348e880SGleb Smirnoff tcp_handle_wakeup(struct tcpcb *tp) 15044d0770f1SRichard Scheffenegger { 1505c348e880SGleb Smirnoff 15069eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(tptoinpcb(tp)); 1507c348e880SGleb Smirnoff 15084d0770f1SRichard Scheffenegger if (tp->t_flags & TF_WAKESOR) { 15099eb0e832SGleb Smirnoff struct socket *so = tptosocket(tp); 1510c348e880SGleb Smirnoff 15114d0770f1SRichard Scheffenegger tp->t_flags &= ~TF_WAKESOR; 1512dded4e9eSRichard Scheffenegger SOCK_RECVBUF_LOCK_ASSERT(so); 1513032bf749SRichard Scheffenegger sorwakeup_locked(so); 15144d0770f1SRichard Scheffenegger } 15154d0770f1SRichard Scheffenegger } 15164d0770f1SRichard Scheffenegger 15174d0770f1SRichard Scheffenegger void 151835bc0bccSGleb Smirnoff tcp_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th, 151935bc0bccSGleb Smirnoff int drop_hdrlen, int tlen, uint8_t iptos) 1520302ce8d6SAndre Oppermann { 1521f7220c48SRichard Scheffenegger uint16_t thflags; 152249a6fbe3SRichard Scheffenegger int acked, ourfinisacked, needoutput = 0; 152349a6fbe3SRichard Scheffenegger sackstatus_t sack_changed; 1524b2ade6b1SRichard Scheffenegger int rstreason, todrop, win, incforsyn = 0; 15253ac12506SJonathan T. Looney uint32_t tiwin; 15264b7b743cSLawrence Stewart uint16_t nsegs; 152707dacf03SAndre Oppermann char *s; 15289eb0e832SGleb Smirnoff struct inpcb *inp = tptoinpcb(tp); 152935bc0bccSGleb Smirnoff struct socket *so = tptosocket(tp); 15309eb0e832SGleb Smirnoff struct in_conninfo *inc = &inp->inp_inc; 1531c11a15bfSGleb Smirnoff struct mbuf *mfree; 1532302ce8d6SAndre Oppermann struct tcpopt to; 1533281a0fd4SPatrick Kelsey int tfo_syn; 1534c7c325d0SRichard Scheffenegger u_int maxseg = 0; 1535302ce8d6SAndre Oppermann 15361ebf4607SRichard Scheffenegger thflags = tcp_get_flags(th); 1537d64a46eaSLawrence Stewart tp->sackhint.last_sack_ack = 0; 153849a6fbe3SRichard Scheffenegger sack_changed = SACK_NOCHANGE; 15394b7b743cSLawrence Stewart nsegs = max(1, m->m_pkthdr.lro_nsegs); 1540d40c0d47SGleb Smirnoff 1541d40c0d47SGleb Smirnoff NET_EPOCH_ASSERT(); 15429eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(inp); 1543679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1544679d9708SAndre Oppermann __func__)); 1545679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1546679d9708SAndre Oppermann __func__)); 1547df8bae1dSRodney W. Grimes 154886a996e6SHiren Panchasara #ifdef TCPPCAP 154986a996e6SHiren Panchasara /* Save segment, if requested. */ 155086a996e6SHiren Panchasara tcp_pcap_add(th, m, &(tp->t_inpkts)); 155186a996e6SHiren Panchasara #endif 15522529f56eSJonathan T. Looney TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0, 15532529f56eSJonathan T. Looney tlen, NULL, true); 155486a996e6SHiren Panchasara 1555013f4df6SMichael Tuexen if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) { 1556013f4df6SMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 1557013f4df6SMichael Tuexen log(LOG_DEBUG, "%s; %s: " 1558013f4df6SMichael Tuexen "SYN|FIN segment ignored (based on " 1559013f4df6SMichael Tuexen "sysctl setting)\n", s, __func__); 1560013f4df6SMichael Tuexen free(s, M_TCPLOG); 1561013f4df6SMichael Tuexen } 1562013f4df6SMichael Tuexen goto drop; 1563013f4df6SMichael Tuexen } 1564013f4df6SMichael Tuexen 1565df8bae1dSRodney W. Grimes /* 1566ebfd7534SMichael Tuexen * If a segment with the ACK-bit set arrives in the SYN-SENT state 1567ebfd7534SMichael Tuexen * check SEQ.ACK first. 1568ebfd7534SMichael Tuexen */ 1569ebfd7534SMichael Tuexen if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) && 1570ebfd7534SMichael Tuexen (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) { 1571ebfd7534SMichael Tuexen rstreason = BANDLIM_UNLIMITED; 1572d1b07f36SRandall Stewart tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 1573ebfd7534SMichael Tuexen goto dropwithreset; 1574ebfd7534SMichael Tuexen } 1575ebfd7534SMichael Tuexen 1576ebfd7534SMichael Tuexen /* 1577df8bae1dSRodney W. Grimes * Segment received on connection. 1578df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1579e8949f74SAndre Oppermann * XXX: This should be done after segment 1580e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 1581df8bae1dSRodney W. Grimes */ 1582cd84e78fSRandall Stewart if (tp->t_idle_reduce && 1583cd84e78fSRandall Stewart (tp->snd_max == tp->snd_una) && 1584cd84e78fSRandall Stewart ((ticks - tp->t_rcvtime) >= tp->t_rxtcur)) 1585cd84e78fSRandall Stewart cc_after_idle(tp); 15869b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 1587df8bae1dSRodney W. Grimes 1588d1b07f36SRandall Stewart if (thflags & TH_FIN) 1589d1b07f36SRandall Stewart tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN); 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 */ 1602b1258b76SRichard Scheffenegger if (tcp_ecn_input_segment(tp, thflags, tlen, 1603b1258b76SRichard Scheffenegger tcp_packets_this_ack(tp, th->th_ack), 1604b1258b76SRichard Scheffenegger iptos)) 1605dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_ECN); 1606f2512ba1SRui Paulo 1607f2512ba1SRui Paulo /* 1608f72167f4SAndre Oppermann * Parse options on any incoming segment. 1609f72167f4SAndre Oppermann */ 1610302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1611302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1612302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1613fce03f85SRandall Stewart if (tp->t_flags2 & TF2_PROC_SACK_PROHIBIT) { 1614fce03f85SRandall Stewart /* 1615fce03f85SRandall Stewart * We don't look at sack's from the 1616fce03f85SRandall Stewart * peer because the MSS is too small which 1617fce03f85SRandall Stewart * can subject us to an attack. 1618fce03f85SRandall Stewart */ 1619fce03f85SRandall Stewart to.to_flags &= ~TOF_SACK; 1620fce03f85SRandall Stewart } 1621fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1622fcf59617SAndrey V. Elsukov if ((tp->t_flags & TF_SIGNATURE) != 0 && 1623fcf59617SAndrey V. Elsukov (to.to_flags & TOF_SIGNATURE) == 0) { 1624fcf59617SAndrey V. Elsukov TCPSTAT_INC(tcps_sig_err_sigopt); 1625fcf59617SAndrey V. Elsukov /* XXX: should drop? */ 1626fcf59617SAndrey V. Elsukov } 1627fcf59617SAndrey V. Elsukov #endif 1628f72167f4SAndre Oppermann /* 1629f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1630bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1631bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1632bf6d304aSAndre Oppermann * was established. 1633f72167f4SAndre Oppermann */ 1634bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1635e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 16362d05a1c8SRichard Scheffenegger if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) { 1637f72167f4SAndre Oppermann to.to_tsecr = 0; 16382d05a1c8SRichard Scheffenegger } else if (tp->t_rxtshift == 1 && 16394531b345SRichard Scheffenegger tp->t_flags & TF_PREVVALID && 16404531b345SRichard Scheffenegger tp->t_badrxtwin != 0 && 16412d05a1c8SRichard Scheffenegger TSTMP_LT(to.to_tsecr, tp->t_badrxtwin)) { 164210d20c84SMatt Macy cc_cong_signal(tp, th, CC_RTO_ERR); 1643bf6d304aSAndre Oppermann } 16442d05a1c8SRichard Scheffenegger } 164507dacf03SAndre Oppermann /* 164697d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 164797d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 16485396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 16495396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 165097d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1651df8bae1dSRodney W. Grimes */ 16520a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 16536e16d877SRichard Scheffenegger /* Handle parallel SYN for ECN */ 1654f7220c48SRichard Scheffenegger tcp_ecn_input_parallel_syn(tp, thflags, iptos); 1655464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 16562593f858SRichard Scheffenegger (tp->t_flags & TF_REQ_SCALE) && 16572593f858SRichard Scheffenegger !(tp->t_flags & TF_NOOPT)) { 1658be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 165902a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 16602d05a1c8SRichard Scheffenegger } else { 16618e051165SRichard Scheffenegger tp->t_flags &= ~TF_REQ_SCALE; 16622d05a1c8SRichard Scheffenegger } 16635396d0f8SAndre Oppermann /* 16645396d0f8SAndre Oppermann * Initial send window. It will be updated with 16655396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 16665396d0f8SAndre Oppermann */ 16675396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 16688e051165SRichard Scheffenegger if ((to.to_flags & TOF_TS) && 16692593f858SRichard Scheffenegger (tp->t_flags & TF_REQ_TSTMP) && 16702593f858SRichard Scheffenegger !(tp->t_flags & TF_NOOPT)) { 1671be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1672be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1673d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 16742d05a1c8SRichard Scheffenegger } else { 16758e051165SRichard Scheffenegger tp->t_flags &= ~TF_REQ_TSTMP; 16762d05a1c8SRichard Scheffenegger } 16772d05a1c8SRichard Scheffenegger if (to.to_flags & TOF_MSS) { 1678be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 16792d05a1c8SRichard Scheffenegger } 16803529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 16812593f858SRichard Scheffenegger (!(to.to_flags & TOF_SACKPERM) || 16822d05a1c8SRichard Scheffenegger (tp->t_flags & TF_NOOPT))) { 16833529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 16842d05a1c8SRichard Scheffenegger } 1685dd7b86e2SGleb Smirnoff if (tp->t_flags & TF_FASTOPEN) { 16862593f858SRichard Scheffenegger if ((to.to_flags & TOF_FASTOPEN) && 16872593f858SRichard Scheffenegger !(tp->t_flags & TF_NOOPT)) { 1688a026a53aSMichael Tuexen uint16_t mss; 1689a026a53aSMichael Tuexen 16902d05a1c8SRichard Scheffenegger if (to.to_flags & TOF_MSS) { 1691a026a53aSMichael Tuexen mss = to.to_mss; 16922d05a1c8SRichard Scheffenegger } else { 16932d05a1c8SRichard Scheffenegger if ((inp->inp_vflag & INP_IPV6) != 0) { 1694a026a53aSMichael Tuexen mss = TCP6_MSS; 16952d05a1c8SRichard Scheffenegger } else { 1696a026a53aSMichael Tuexen mss = TCP_MSS; 16972d05a1c8SRichard Scheffenegger } 16982d05a1c8SRichard Scheffenegger } 1699a026a53aSMichael Tuexen tcp_fastopen_update_cache(tp, mss, 1700a026a53aSMichael Tuexen to.to_tfo_len, to.to_tfo_cookie); 17012d05a1c8SRichard Scheffenegger } else { 1702c560df6fSPatrick Kelsey tcp_fastopen_disable_path(tp); 1703c560df6fSPatrick Kelsey } 17046d90faf3SPaul Saab } 17052d05a1c8SRichard Scheffenegger } 17066d90faf3SPaul Saab 1707df8bae1dSRodney W. Grimes /* 1708283c76c7SMichael Tuexen * If timestamps were negotiated during SYN/ACK and a 1709283c76c7SMichael Tuexen * segment without a timestamp is received, silently drop 1710d2b3ceddSMichael Tuexen * the segment, unless it is a RST segment or missing timestamps are 1711d2b3ceddSMichael Tuexen * tolerated. 1712283c76c7SMichael Tuexen * See section 3.2 of RFC 7323. 17134da9052aSMichael Tuexen */ 17144da9052aSMichael Tuexen if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) { 1715d2b3ceddSMichael Tuexen if (((thflags & TH_RST) != 0) || V_tcp_tolerate_missing_ts) { 1716cc3c3485SMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 1717cc3c3485SMichael Tuexen log(LOG_DEBUG, "%s; %s: Timestamp missing, " 1718cc3c3485SMichael Tuexen "segment processed normally\n", 1719cc3c3485SMichael Tuexen s, __func__); 1720cc3c3485SMichael Tuexen free(s, M_TCPLOG); 1721cc3c3485SMichael Tuexen } 1722cc3c3485SMichael Tuexen } else { 17234da9052aSMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 17244da9052aSMichael Tuexen log(LOG_DEBUG, "%s; %s: Timestamp missing, " 1725283c76c7SMichael Tuexen "segment silently dropped\n", s, __func__); 17264da9052aSMichael Tuexen free(s, M_TCPLOG); 17274da9052aSMichael Tuexen } 1728283c76c7SMichael Tuexen goto drop; 17294da9052aSMichael Tuexen } 1730cc3c3485SMichael Tuexen } 1731283c76c7SMichael Tuexen /* 1732283c76c7SMichael Tuexen * If timestamps were not negotiated during SYN/ACK and a 173375fcd27aSMichael Tuexen * segment with a timestamp is received, ignore the 1734283c76c7SMichael Tuexen * timestamp and process the packet normally. 1735283c76c7SMichael Tuexen * See section 3.2 of RFC 7323. 1736283c76c7SMichael Tuexen */ 17374da9052aSMichael Tuexen if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) { 17384da9052aSMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 17394da9052aSMichael Tuexen log(LOG_DEBUG, "%s; %s: Timestamp not expected, " 1740283c76c7SMichael Tuexen "segment processed normally\n", s, __func__); 17414da9052aSMichael Tuexen free(s, M_TCPLOG); 17424da9052aSMichael Tuexen } 17434da9052aSMichael Tuexen } 17444da9052aSMichael Tuexen 17454da9052aSMichael Tuexen /* 1746df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1747df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1748df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1749df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1750df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1751df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1752df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1753df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1754df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1755df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1756df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1757df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1758a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1759c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 1760a0292f23SGarrett Wollman * be TH_NEEDSYN. 1761df8bae1dSRodney W. Grimes */ 1762df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1763c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 1764fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1765c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 1766c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 1767a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1768c28440dbSRandall Stewart SEGQ_EMPTY(tp) && 17694741bfcbSPatrick Kelsey ((to.to_flags & TOF_TS) == 0 || 1770c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1771df8bae1dSRodney W. Grimes /* 1772df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1773df8bae1dSRodney W. Grimes * record the timestamp. 1774a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1775a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1776df8bae1dSRodney W. Grimes */ 1777be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1778fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1779d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1780a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1781df8bae1dSRodney W. Grimes } 1782df8bae1dSRodney W. Grimes 1783fb59c426SYoshinobu Inoue if (tlen == 0) { 1784fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1785fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1786dbc42409SLawrence Stewart !IN_RECOVERY(tp->t_flags) && 1787fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1788dbc42409SLawrence Stewart TAILQ_EMPTY(&tp->snd_holes)) { 1789df8bae1dSRodney W. Grimes /* 1790e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1791df8bae1dSRodney W. Grimes */ 179278b50714SRobert Watson TCPSTAT_INC(tcps_predack); 1793252ca428SRobert Watson 17949b8b58e0SJonathan Lemon /* 179510d20c84SMatt Macy * "bad retransmit" recovery without timestamps. 17969b8b58e0SJonathan Lemon */ 179710d20c84SMatt Macy if ((to.to_flags & TOF_TS) == 0 && 179810d20c84SMatt Macy tp->t_rxtshift == 1 && 1799672dc4aeSJohn Baldwin tp->t_flags & TF_PREVVALID && 18004531b345SRichard Scheffenegger tp->t_badrxtwin != 0 && 18014531b345SRichard Scheffenegger TSTMP_LT(ticks, tp->t_badrxtwin)) { 1802dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 18039b8b58e0SJonathan Lemon } 1804fa55172bSMatthew Dillon 1805fa55172bSMatthew Dillon /* 1806fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1807fa55172bSMatthew Dillon * 1808fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1809fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1810fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1811fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1812fa55172bSMatthew Dillon */ 1813fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1814fa55172bSMatthew Dillon to.to_tsecr) { 18153ac12506SJonathan T. Looney uint32_t t; 1816d8951c8aSBjoern A. Zeeb 1817d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 1818d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 1819d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 1820a0292f23SGarrett Wollman tcp_xmit_timer(tp, 1821d8951c8aSBjoern A. Zeeb TCP_TS_TO_TICKS(t) + 1); 1822fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1823fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1824eaf80179SAndre Oppermann if (!tp->t_rttlow || 1825eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1826eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1827c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1828c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1829fa55172bSMatthew Dillon } 1830dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 183139bc9de5SLawrence Stewart 1832bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 183339bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 183439bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 1835bd79708dSJonathan T. Looney #endif 183639bc9de5SLawrence Stewart 18374b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvackpack, nsegs); 183878b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 1839df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 18409d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 18419d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 18429d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 1843dbc42409SLawrence Stewart 1844dbc42409SLawrence Stewart /* 1845dbc42409SLawrence Stewart * Let the congestion control algorithm update 1846dbc42409SLawrence Stewart * congestion control related information. This 1847dbc42409SLawrence Stewart * typically means increasing the congestion 1848dbc42409SLawrence Stewart * window. 1849dbc42409SLawrence Stewart */ 18504b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, CC_ACK); 1851dbc42409SLawrence Stewart 18529d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 18531645d090SJeff Roberson /* 1854e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 18551645d090SJeff Roberson * to th_ack. 18561645d090SJeff Roberson */ 1857cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1858b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1859df8bae1dSRodney W. Grimes m_freem(m); 1860df8bae1dSRodney W. Grimes 1861df8bae1dSRodney W. Grimes /* 1862df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1863df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1864df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1865df8bae1dSRodney W. Grimes * If process is waiting for space, 1866df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1867df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1868df8bae1dSRodney W. Grimes * decide between more output or persist. 1869e8949f74SAndre Oppermann */ 18702b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 187108af8aacSRandall Stewart /* 187208af8aacSRandall Stewart * Clear t_acktime if remote side has ACKd 187308af8aacSRandall Stewart * all data in the socket buffer. 187408af8aacSRandall Stewart * Otherwise, update t_acktime if we received 187508af8aacSRandall Stewart * a sufficiently large ACK. 187608af8aacSRandall Stewart */ 187708af8aacSRandall Stewart if (sbavail(&so->so_snd) == 0) 187808af8aacSRandall Stewart tp->t_acktime = 0; 187908af8aacSRandall Stewart else if (acked > 1) 188008af8aacSRandall Stewart tp->t_acktime = ticks; 1881df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1882b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1883b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1884b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 188508af8aacSRandall Stewart TP_RXTCUR(tp)); 1886032bf749SRichard Scheffenegger sowwakeup(so); 1887f4574e2dSRichard Scheffenegger /* 1888f4574e2dSRichard Scheffenegger * Only call tcp_output when there 1889f4574e2dSRichard Scheffenegger * is new data available to be sent 1890f4574e2dSRichard Scheffenegger * or we need to send an ACK. 1891f4574e2dSRichard Scheffenegger */ 18922d05a1c8SRichard Scheffenegger if ((tp->t_flags & TF_ACKNOW) || 18932d05a1c8SRichard Scheffenegger (sbavail(&so->so_snd) >= 18942d05a1c8SRichard Scheffenegger SEQ_SUB(tp->snd_max, tp->snd_una))) { 189540fa3e40SGleb Smirnoff (void) tcp_output(tp); 18962d05a1c8SRichard Scheffenegger } 1897a14c749fSJonathan Lemon goto check_delack; 1898df8bae1dSRodney W. Grimes } 1899fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1900fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 19016741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 19026741ecf5SAndre Oppermann 1903df8bae1dSRodney W. Grimes /* 1904252ca428SRobert Watson * This is a pure, in-sequence data packet with 1905252ca428SRobert Watson * nothing on the reassembly queue and we have enough 1906252ca428SRobert Watson * buffer space to take it. 1907df8bae1dSRodney W. Grimes */ 19086d90faf3SPaul Saab /* Clean receiver SACK report if present */ 19093529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 19106d90faf3SPaul Saab tcp_clean_sackreport(tp); 191178b50714SRobert Watson TCPSTAT_INC(tcps_preddat); 1912fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 1913e854dd38SRandall Stewart if (tlen && 1914e854dd38SRandall Stewart ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 1915e854dd38SRandall Stewart (tp->t_fbyte_in == 0)) { 1916e854dd38SRandall Stewart tp->t_fbyte_in = ticks; 1917e854dd38SRandall Stewart if (tp->t_fbyte_in == 0) 1918e854dd38SRandall Stewart tp->t_fbyte_in = 1; 1919e854dd38SRandall Stewart if (tp->t_fbyte_out && tp->t_fbyte_in) 1920e854dd38SRandall Stewart tp->t_flags2 |= TF2_FBYTES_COMPLETE; 1921e854dd38SRandall Stewart } 19221645d090SJeff Roberson /* 19231645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 19241645d090SJeff Roberson * th_seq. 19251645d090SJeff Roberson */ 192660ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 19271645d090SJeff Roberson /* 19281645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 19291645d090SJeff Roberson * rcv_nxt. 19301645d090SJeff Roberson */ 19311645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 19324b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvpack, nsegs); 193378b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 19342b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 19355d06879aSGeorge V. Neville-Neil 1936e44c1887SSteven Hartland newsize = tcp_autorcvbuf(m, th, so, tp, tlen); 19376741ecf5SAndre Oppermann 19386741ecf5SAndre Oppermann /* Add data to socket buffer. */ 1939dded4e9eSRichard Scheffenegger SOCK_RECVBUF_LOCK(so); 1940c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1941c1c36a2cSMike Silbersack m_freem(m); 1942c1c36a2cSMike Silbersack } else { 19436741ecf5SAndre Oppermann /* 19446741ecf5SAndre Oppermann * Set new socket buffer size. 19456741ecf5SAndre Oppermann * Give up when limit is reached. 19466741ecf5SAndre Oppermann */ 19476741ecf5SAndre Oppermann if (newsize) 194843283184SGleb Smirnoff if (!sbreserve_locked(so, SO_RCV, 194943283184SGleb Smirnoff newsize, NULL)) 19506741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1951fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 1952651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 1953c1c36a2cSMike Silbersack } 1954032bf749SRichard Scheffenegger /* NB: sorwakeup_locked() does an implicit unlock. */ 1955032bf749SRichard Scheffenegger sorwakeup_locked(so); 1956c1e5a6e5SAndre Oppermann if (DELAY_ACK(tp, tlen)) { 19573bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1958f498eeeeSDavid Greenman } else { 1959e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 19603e1c8a35SMichael Tuexen (void) tcp_output(tp); 1961e612a582SDavid Greenman } 1962a14c749fSJonathan Lemon goto check_delack; 1963df8bae1dSRodney W. Grimes } 1964df8bae1dSRodney W. Grimes } 1965df8bae1dSRodney W. Grimes 1966df8bae1dSRodney W. Grimes /* 1967df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1968df8bae1dSRodney W. Grimes * and then do TCP input processing. 1969df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1970df8bae1dSRodney W. Grimes * but not less than advertised window. 1971df8bae1dSRodney W. Grimes */ 1972df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1973df8bae1dSRodney W. Grimes if (win < 0) 1974df8bae1dSRodney W. Grimes win = 0; 197566e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1976df8bae1dSRodney W. Grimes 1977df8bae1dSRodney W. Grimes switch (tp->t_state) { 1978df8bae1dSRodney W. Grimes /* 1979764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1980764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1981764d8cefSBill Fenner */ 1982764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1983b352ef58SRichard Scheffenegger if (thflags & TH_RST) { 1984b352ef58SRichard Scheffenegger /* Handle RST segments later. */ 1985b352ef58SRichard Scheffenegger break; 1986b352ef58SRichard Scheffenegger } 1987fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1988fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1989a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1990a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1991d1b07f36SRandall Stewart tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 1992a57815efSBosko Milekic goto dropwithreset; 1993a57815efSBosko Milekic } 1994dd7b86e2SGleb Smirnoff if (tp->t_flags & TF_FASTOPEN) { 1995281a0fd4SPatrick Kelsey /* 1996281a0fd4SPatrick Kelsey * When a TFO connection is in SYN_RECEIVED, the 1997281a0fd4SPatrick Kelsey * only valid packets are the initial SYN, a 1998281a0fd4SPatrick Kelsey * retransmit/copy of the initial SYN (possibly with 1999281a0fd4SPatrick Kelsey * a subset of the original data), a valid ACK, a 2000281a0fd4SPatrick Kelsey * FIN, or a RST. 2001281a0fd4SPatrick Kelsey */ 2002281a0fd4SPatrick Kelsey if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) { 2003281a0fd4SPatrick Kelsey rstreason = BANDLIM_RST_OPENPORT; 2004d1b07f36SRandall Stewart tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 2005281a0fd4SPatrick Kelsey goto dropwithreset; 2006281a0fd4SPatrick Kelsey } else if (thflags & TH_SYN) { 2007281a0fd4SPatrick Kelsey /* non-initial SYN is ignored */ 2008281a0fd4SPatrick Kelsey if ((tcp_timer_active(tp, TT_DELACK) || 2009281a0fd4SPatrick Kelsey tcp_timer_active(tp, TT_REXMT))) 2010281a0fd4SPatrick Kelsey goto drop; 2011281a0fd4SPatrick Kelsey } else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) { 2012281a0fd4SPatrick Kelsey goto drop; 2013281a0fd4SPatrick Kelsey } 2014281a0fd4SPatrick Kelsey } 2015764d8cefSBill Fenner break; 2016764d8cefSBill Fenner 2017764d8cefSBill Fenner /* 2018df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 201998732609SMichael Tuexen * if seg contains a RST with valid ACK (SEQ.ACK has already 202098732609SMichael Tuexen * been verified), then drop the connection. 202198732609SMichael Tuexen * if seg contains a RST without an ACK, drop the seg. 202298732609SMichael Tuexen * if seg does not contain SYN, then drop the seg. 2023df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 2024df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 2025df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 2026f2512ba1SRui Paulo * if seg contains an ECE and ECN support is enabled, the stream 2027f2512ba1SRui Paulo * is ECN capable. 2028df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 2029df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 2030df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 2031df8bae1dSRodney W. Grimes */ 2032df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 203357f60867SMark Johnston if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) { 2034d9fae5abSAndriy Gapon TCP_PROBE5(connect__refused, NULL, tp, 20352b9c9984SGeorge V. Neville-Neil m, tp, th); 2036d1b07f36SRandall Stewart tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 2037df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 203857f60867SMark Johnston } 20390ca3f933SAndre Oppermann if (thflags & TH_RST) 2040df8bae1dSRodney W. Grimes goto drop; 20410ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 2042df8bae1dSRodney W. Grimes goto drop; 2043464fcfbcSAndre Oppermann 2044fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 2045df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 2046fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 2047c560df6fSPatrick Kelsey int tfo_partial_ack = 0; 2048c560df6fSPatrick Kelsey 204978b50714SRobert Watson TCPSTAT_INC(tcps_connects); 2050845799c1SAndras Olah soisconnected(so); 2051c488362eSRobert Watson #ifdef MAC 205230d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 2053c488362eSRobert Watson #endif 2054845799c1SAndras Olah /* Do window scaling on this connection? */ 2055845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2056845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2057845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 2058845799c1SAndras Olah } 20593ac12506SJonathan T. Looney tp->rcv_adv += min(tp->rcv_wnd, 2060766282cbSJohn Baldwin TCP_MAXWIN << tp->rcv_scale); 2061a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 2062f4574e2dSRichard Scheffenegger if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2063f4574e2dSRichard Scheffenegger tp->snd_nxt = tp->snd_una; 2064a0292f23SGarrett Wollman /* 2065c560df6fSPatrick Kelsey * If not all the data that was sent in the TFO SYN 2066c560df6fSPatrick Kelsey * has been acked, resend the remainder right away. 2067c560df6fSPatrick Kelsey */ 2068dd7b86e2SGleb Smirnoff if ((tp->t_flags & TF_FASTOPEN) && 2069c560df6fSPatrick Kelsey (tp->snd_una != tp->snd_max)) { 2070c560df6fSPatrick Kelsey tp->snd_nxt = th->th_ack; 2071c560df6fSPatrick Kelsey tfo_partial_ack = 1; 2072c560df6fSPatrick Kelsey } 2073c560df6fSPatrick Kelsey /* 2074a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 2075a0292f23SGarrett Wollman * ACKNOW will be turned on later. 2076a0292f23SGarrett Wollman */ 2077c560df6fSPatrick Kelsey if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial_ack) 2078b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 2079b8152ba7SAndre Oppermann tcp_delacktime); 2080a0292f23SGarrett Wollman else 2081a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2082f2512ba1SRui Paulo 2083f7220c48SRichard Scheffenegger tcp_ecn_input_syn_sent(tp, thflags, iptos); 2084f2512ba1SRui Paulo 2085a0292f23SGarrett Wollman /* 2086a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 2087a0292f23SGarrett Wollman * Transitions: 2088a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 2089a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 2090a0292f23SGarrett Wollman */ 20919b8b58e0SJonathan Lemon tp->t_starttime = ticks; 2092a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 209308af8aacSRandall Stewart tp->t_acktime = ticks; 209457f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2095a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 2096fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 20977ff19458SPaul Traina } else { 209857f60867SMark Johnston tcp_state_change(tp, TCPS_ESTABLISHED); 2099d9fae5abSAndriy Gapon TCP_PROBE5(connect__established, NULL, tp, 21002b9c9984SGeorge V. Neville-Neil m, tp, th); 2101dbc42409SLawrence Stewart cc_conn_init(tp); 21029077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 21039077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 21047ff19458SPaul Traina } 2105a0292f23SGarrett Wollman } else { 2106a0292f23SGarrett Wollman /* 2107c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 2108153edc50SHiren Panchasara * simultaneous open. 2109c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 2110c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 2111a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 2112a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 2113a0292f23SGarrett Wollman */ 2114493105c2SGleb Smirnoff tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN | TF_SONOTCONN); 2115b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 211657f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_RECEIVED); 2117a0292f23SGarrett Wollman } 2118df8bae1dSRodney W. Grimes 2119df8bae1dSRodney W. Grimes /* 2120fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 2121df8bae1dSRodney W. Grimes * If data, trim to stay within window, 2122df8bae1dSRodney W. Grimes * dropping FIN if necessary. 2123df8bae1dSRodney W. Grimes */ 2124fb59c426SYoshinobu Inoue th->th_seq++; 2125fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 2126fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 2127df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2128fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 2129fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 213078b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 213178b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2132df8bae1dSRodney W. Grimes } 2133fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 2134fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 2135a0292f23SGarrett Wollman /* 2136a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 2137a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 2138a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 2139a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 2140a0292f23SGarrett Wollman * Otherwise, goto step 6. 2141a0292f23SGarrett Wollman */ 2142fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 2143a0292f23SGarrett Wollman goto process_ACK; 2144c068736aSJeffrey Hsu 2145df8bae1dSRodney W. Grimes goto step6; 2146df8bae1dSRodney W. Grimes } 2147df8bae1dSRodney W. Grimes 2148df8bae1dSRodney W. Grimes /* 2149df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 215080ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 215180ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 215280ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 215380ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 215480ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 215580ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 215680ab7c0eSGarrett Wollman * killing a connection). 215780ab7c0eSGarrett Wollman * Then check timestamp, if present. 2158a0292f23SGarrett Wollman * Then check the connection count, if present. 2159df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 2160df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 2161df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 216280ab7c0eSGarrett Wollman */ 2163fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 21643220a212SGleb Smirnoff /* 21653220a212SGleb Smirnoff * RFC5961 Section 3.2 21663220a212SGleb Smirnoff * 21673220a212SGleb Smirnoff * - RST drops connection only if SEG.SEQ == RCV.NXT. 21683220a212SGleb Smirnoff * - If RST is in window, we send challenge ACK. 21693220a212SGleb Smirnoff * 21703220a212SGleb Smirnoff * Note: to take into account delayed ACKs, we should 21713220a212SGleb Smirnoff * test against last_ack_sent instead of rcv_nxt. 21723220a212SGleb Smirnoff * Note 2: we handle special case of closed window, not 21733220a212SGleb Smirnoff * covered by the RFC. 21743220a212SGleb Smirnoff */ 21753220a212SGleb Smirnoff if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 21763220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) || 21773220a212SGleb Smirnoff (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) { 21783220a212SGleb Smirnoff KASSERT(tp->t_state != TCPS_SYN_SENT, 21793220a212SGleb Smirnoff ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p", 21803220a212SGleb Smirnoff __func__, th, tp)); 21813220a212SGleb Smirnoff 21823220a212SGleb Smirnoff if (V_tcp_insecure_rst || 21833220a212SGleb Smirnoff tp->last_ack_sent == th->th_seq) { 21843220a212SGleb Smirnoff TCPSTAT_INC(tcps_drops); 21853220a212SGleb Smirnoff /* Drop the connection. */ 21863220a212SGleb Smirnoff switch (tp->t_state) { 218780ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 218880ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 218980ab7c0eSGarrett Wollman goto close; 219080ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 219180ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 219280ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 219380ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 21946779a1a1SMichael Tuexen case TCPS_CLOSING: 21956779a1a1SMichael Tuexen case TCPS_LAST_ACK: 219680ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 219780ab7c0eSGarrett Wollman close: 21983220a212SGleb Smirnoff /* FALLTHROUGH */ 21993220a212SGleb Smirnoff default: 2200d1b07f36SRandall Stewart tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_RST); 220180ab7c0eSGarrett Wollman tp = tcp_close(tp); 22023220a212SGleb Smirnoff } 22033220a212SGleb Smirnoff } else { 22043220a212SGleb Smirnoff TCPSTAT_INC(tcps_badrst); 220540299c55SMichael Tuexen tcp_send_challenge_ack(tp, th, m); 22063220a212SGleb Smirnoff m = NULL; 22073220a212SGleb Smirnoff } 22083220a212SGleb Smirnoff } 22093220a212SGleb Smirnoff goto drop; 22103220a212SGleb Smirnoff } 221180ab7c0eSGarrett Wollman 22123220a212SGleb Smirnoff /* 22133220a212SGleb Smirnoff * RFC5961 Section 4.2 22143220a212SGleb Smirnoff * Send challenge ACK for any SYN in synchronized state. 22153220a212SGleb Smirnoff */ 2216281a0fd4SPatrick Kelsey if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT && 2217281a0fd4SPatrick Kelsey tp->t_state != TCPS_SYN_RECEIVED) { 22183220a212SGleb Smirnoff TCPSTAT_INC(tcps_badsyn); 22193220a212SGleb Smirnoff if (V_tcp_insecure_syn && 22203220a212SGleb Smirnoff SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 22213220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 2222d1b07f36SRandall Stewart tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 22233220a212SGleb Smirnoff tp = tcp_drop(tp, ECONNRESET); 22243220a212SGleb Smirnoff rstreason = BANDLIM_UNLIMITED; 22253220a212SGleb Smirnoff } else { 222683c1ec92SRichard Scheffenegger tcp_ecn_input_syn_sent(tp, thflags, iptos); 222740299c55SMichael Tuexen tcp_send_challenge_ack(tp, th, m); 22283220a212SGleb Smirnoff m = NULL; 222980ab7c0eSGarrett Wollman } 223080ab7c0eSGarrett Wollman goto drop; 223180ab7c0eSGarrett Wollman } 223280ab7c0eSGarrett Wollman 223380ab7c0eSGarrett Wollman /* 2234df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 2235df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 2236df8bae1dSRodney W. Grimes */ 2237be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 223880ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 2239df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 2240d8951c8aSBjoern A. Zeeb if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 2241df8bae1dSRodney W. Grimes /* 2242df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 2243df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 2244df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 2245df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 2246df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 2247df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 2248df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 2249df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 2250df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 2251df8bae1dSRodney W. Grimes */ 2252df8bae1dSRodney W. Grimes tp->ts_recent = 0; 2253df8bae1dSRodney W. Grimes } else { 225478b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 225578b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 225678b50714SRobert Watson TCPSTAT_INC(tcps_pawsdrop); 225707fd333dSMatthew Dillon if (tlen) 2258df8bae1dSRodney W. Grimes goto dropafterack; 22591ab4789dSMatthew Dillon goto drop; 22601ab4789dSMatthew Dillon } 2261df8bae1dSRodney W. Grimes } 2262df8bae1dSRodney W. Grimes 2263a0292f23SGarrett Wollman /* 226480ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 226580ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 226680ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 226780ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 226880ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 226980ab7c0eSGarrett Wollman */ 2270a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 2271a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2272d1b07f36SRandall Stewart tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 2273a57815efSBosko Milekic goto dropwithreset; 2274a57815efSBosko Milekic } 227580ab7c0eSGarrett Wollman 2276fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 2277df8bae1dSRodney W. Grimes if (todrop > 0) { 2278831ad37eSXin LI if (thflags & TH_SYN) { 2279fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 2280fb59c426SYoshinobu Inoue th->th_seq++; 2281fb59c426SYoshinobu Inoue if (th->th_urp > 1) 2282fb59c426SYoshinobu Inoue th->th_urp--; 2283df8bae1dSRodney W. Grimes else 2284fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2285df8bae1dSRodney W. Grimes todrop--; 2286df8bae1dSRodney W. Grimes } 2287df8bae1dSRodney W. Grimes /* 2288dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 2289df8bae1dSRodney W. Grimes */ 2290fb59c426SYoshinobu Inoue if (todrop > tlen 2291fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 2292dac20301SGarrett Wollman /* 2293dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 2294dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 2295dac20301SGarrett Wollman * of sequence; drop it. 2296dac20301SGarrett Wollman */ 2297fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2298dac20301SGarrett Wollman 2299df8bae1dSRodney W. Grimes /* 2300dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 2301dac20301SGarrett Wollman * But keep on processing for RST or ACK. 2302df8bae1dSRodney W. Grimes */ 2303dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2304fb59c426SYoshinobu Inoue todrop = tlen; 230578b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 230678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 2307df8bae1dSRodney W. Grimes } else { 230878b50714SRobert Watson TCPSTAT_INC(tcps_rcvpartduppack); 230978b50714SRobert Watson TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 2310df8bae1dSRodney W. Grimes } 23115acfd95cSMichael Tuexen /* 23125acfd95cSMichael Tuexen * DSACK - add SACK block for dropped range 23135acfd95cSMichael Tuexen */ 23148f63a52bSMichael Tuexen if ((todrop > 0) && (tp->t_flags & TF_SACK_PERMIT)) { 2315ecc5b1d1SMichael Tuexen tcp_update_sack_list(tp, th->th_seq, 2316ecc5b1d1SMichael Tuexen th->th_seq + todrop); 23175acfd95cSMichael Tuexen /* 23185acfd95cSMichael Tuexen * ACK now, as the next in-sequence segment 23195acfd95cSMichael Tuexen * will clear the DSACK block again 23205acfd95cSMichael Tuexen */ 23215acfd95cSMichael Tuexen tp->t_flags |= TF_ACKNOW; 23225acfd95cSMichael Tuexen } 2323fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 2324fb59c426SYoshinobu Inoue th->th_seq += todrop; 2325fb59c426SYoshinobu Inoue tlen -= todrop; 2326fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 2327fb59c426SYoshinobu Inoue th->th_urp -= todrop; 2328df8bae1dSRodney W. Grimes else { 2329fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2330fb59c426SYoshinobu Inoue th->th_urp = 0; 2331df8bae1dSRodney W. Grimes } 2332df8bae1dSRodney W. Grimes } 2333df8bae1dSRodney W. Grimes 2334df8bae1dSRodney W. Grimes /* 2335df8bae1dSRodney W. Grimes * If new data are received on a connection after the 2336c9cd686bSMichael Tuexen * user processes are gone, then RST the other end if 2337c9cd686bSMichael Tuexen * no FIN has been processed. 2338df8bae1dSRodney W. Grimes */ 2339c9cd686bSMichael Tuexen if ((tp->t_flags & TF_CLOSED) && tlen > 0 && 2340c9cd686bSMichael Tuexen TCPS_HAVERCVDFIN(tp->t_state) == 0) { 234107dacf03SAndre Oppermann if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 234207dacf03SAndre Oppermann log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data " 234307dacf03SAndre Oppermann "after socket was closed, " 234407dacf03SAndre Oppermann "sending RST and removing tcpcb\n", 2345e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 2346773673c1SAndre Oppermann free(s, M_TCPLOG); 2347773673c1SAndre Oppermann } 2348d1b07f36SRandall Stewart tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE); 2349d1b07f36SRandall Stewart /* tcp_close will kill the inp pre-log the Reset */ 2350d1b07f36SRandall Stewart tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 2351df8bae1dSRodney W. Grimes tp = tcp_close(tp); 235278b50714SRobert Watson TCPSTAT_INC(tcps_rcvafterclose); 2353a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2354df8bae1dSRodney W. Grimes goto dropwithreset; 2355df8bae1dSRodney W. Grimes } 2356df8bae1dSRodney W. Grimes 2357df8bae1dSRodney W. Grimes /* 2358df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 2359df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 2360df8bae1dSRodney W. Grimes */ 2361fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 2362df8bae1dSRodney W. Grimes if (todrop > 0) { 236378b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 2364fb59c426SYoshinobu Inoue if (todrop >= tlen) { 236578b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 2366df8bae1dSRodney W. Grimes /* 2367df8bae1dSRodney W. Grimes * If window is closed can only take segments at 2368df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 2369df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 2370df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 2371df8bae1dSRodney W. Grimes * and ack. 2372df8bae1dSRodney W. Grimes */ 2373fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2374df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 237578b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinprobe); 2376df8bae1dSRodney W. Grimes } else 2377df8bae1dSRodney W. Grimes goto dropafterack; 2378df8bae1dSRodney W. Grimes } else 237978b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2380df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2381fb59c426SYoshinobu Inoue tlen -= todrop; 2382fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 2383df8bae1dSRodney W. Grimes } 2384df8bae1dSRodney W. Grimes 2385df8bae1dSRodney W. Grimes /* 2386df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 2387df8bae1dSRodney W. Grimes * record its timestamp. 2388cf09195bSPaul Saab * NOTE: 2389cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 2390a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2391cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 2392cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 2393cf09195bSPaul Saab * predicated on the sequence space of this segment. 2394cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 2395cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 2396cf09195bSPaul Saab * instead of RFC1323's 2397cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 2398cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 2399cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 2400cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 2401cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 2402df8bae1dSRodney W. Grimes */ 2403be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 2404cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2405cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 2406cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 2407d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 2408a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 2409df8bae1dSRodney W. Grimes } 2410df8bae1dSRodney W. Grimes 2411df8bae1dSRodney W. Grimes /* 2412a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 2413a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 2414a0292f23SGarrett Wollman * later processing; else drop segment and return. 2415a0292f23SGarrett Wollman */ 2416fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 2417a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 2418281a0fd4SPatrick Kelsey (tp->t_flags & TF_NEEDSYN)) { 2419281a0fd4SPatrick Kelsey if (tp->t_state == TCPS_SYN_RECEIVED && 2420dd7b86e2SGleb Smirnoff (tp->t_flags & TF_FASTOPEN)) { 2421281a0fd4SPatrick Kelsey tp->snd_wnd = tiwin; 2422281a0fd4SPatrick Kelsey cc_conn_init(tp); 2423281a0fd4SPatrick Kelsey } 2424a0292f23SGarrett Wollman goto step6; 2425281a0fd4SPatrick Kelsey } else if (tp->t_flags & TF_ACKNOW) 24265e1aa279SDavid Malone goto dropafterack; 2427a0292f23SGarrett Wollman else 2428a0292f23SGarrett Wollman goto drop; 2429a0292f23SGarrett Wollman } 2430df8bae1dSRodney W. Grimes 2431df8bae1dSRodney W. Grimes /* 2432df8bae1dSRodney W. Grimes * Ack processing. 2433df8bae1dSRodney W. Grimes */ 243437b3e6a6SMichael Tuexen if (SEQ_GEQ(tp->snd_una, tp->iss + (TCP_MAXWIN << tp->snd_scale))) { 2435646c28eaSMichael Tuexen /* Checking SEG.ACK against ISS is definitely redundant. */ 2436646c28eaSMichael Tuexen tp->t_flags2 |= TF2_NO_ISS_CHECK; 2437646c28eaSMichael Tuexen } 2438646c28eaSMichael Tuexen if (!V_tcp_insecure_ack) { 2439646c28eaSMichael Tuexen tcp_seq seq_min; 2440646c28eaSMichael Tuexen bool ghost_ack_check; 2441646c28eaSMichael Tuexen 2442646c28eaSMichael Tuexen if (tp->t_flags2 & TF2_NO_ISS_CHECK) { 2443646c28eaSMichael Tuexen /* Check for too old ACKs (RFC 5961, Section 5.2). */ 2444646c28eaSMichael Tuexen seq_min = tp->snd_una - tp->max_sndwnd; 2445646c28eaSMichael Tuexen ghost_ack_check = false; 2446646c28eaSMichael Tuexen } else { 2447646c28eaSMichael Tuexen if (SEQ_GT(tp->iss + 1, tp->snd_una - tp->max_sndwnd)) { 2448646c28eaSMichael Tuexen /* Checking for ghost ACKs is stricter. */ 2449646c28eaSMichael Tuexen seq_min = tp->iss + 1; 2450646c28eaSMichael Tuexen ghost_ack_check = true; 2451646c28eaSMichael Tuexen } else { 2452646c28eaSMichael Tuexen /* 2453646c28eaSMichael Tuexen * Checking for too old ACKs (RFC 5961, 2454646c28eaSMichael Tuexen * Section 5.2) is stricter. 2455646c28eaSMichael Tuexen */ 2456646c28eaSMichael Tuexen seq_min = tp->snd_una - tp->max_sndwnd; 2457646c28eaSMichael Tuexen ghost_ack_check = false; 2458646c28eaSMichael Tuexen } 2459646c28eaSMichael Tuexen } 2460646c28eaSMichael Tuexen if (SEQ_LT(th->th_ack, seq_min)) { 2461646c28eaSMichael Tuexen if (ghost_ack_check) 2462646c28eaSMichael Tuexen TCPSTAT_INC(tcps_rcvghostack); 2463646c28eaSMichael Tuexen else 2464646c28eaSMichael Tuexen TCPSTAT_INC(tcps_rcvacktooold); 246540299c55SMichael Tuexen tcp_send_challenge_ack(tp, th, m); 2466646c28eaSMichael Tuexen m = NULL; 2467646c28eaSMichael Tuexen goto drop; 2468646c28eaSMichael Tuexen } 2469646c28eaSMichael Tuexen } 2470df8bae1dSRodney W. Grimes switch (tp->t_state) { 2471df8bae1dSRodney W. Grimes /* 2472764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 2473764d8cefSBill Fenner * ESTABLISHED state and continue processing. 2474764d8cefSBill Fenner * The ACK was checked above. 2475df8bae1dSRodney W. Grimes */ 2476df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2477a0292f23SGarrett Wollman 247878b50714SRobert Watson TCPSTAT_INC(tcps_connects); 2479493105c2SGleb Smirnoff if (tp->t_flags & TF_SONOTCONN) { 2480493105c2SGleb Smirnoff /* 2481493105c2SGleb Smirnoff * Usually SYN_RECEIVED had been created from a LISTEN, 2482493105c2SGleb Smirnoff * and solisten_enqueue() has already marked the socket 2483493105c2SGleb Smirnoff * layer as connected. If it didn't, which can happen 2484493105c2SGleb Smirnoff * only with an accept_filter(9), then the tp is marked 2485493105c2SGleb Smirnoff * with TF_SONOTCONN. The other reason for this mark 2486493105c2SGleb Smirnoff * to be set is a simultaneous open, a SYN_RECEIVED 2487493105c2SGleb Smirnoff * that had been created from SYN_SENT. 2488493105c2SGleb Smirnoff */ 2489493105c2SGleb Smirnoff tp->t_flags &= ~TF_SONOTCONN; 2490df8bae1dSRodney W. Grimes soisconnected(so); 2491e80062a2SGleb Smirnoff } 2492df8bae1dSRodney W. Grimes /* Do window scaling? */ 2493df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2494df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2495df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 2496df8bae1dSRodney W. Grimes } 2497116ef4d6SMichael Tuexen tp->snd_wnd = tiwin; 2498a0292f23SGarrett Wollman /* 2499a0292f23SGarrett Wollman * Make transitions: 2500a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 2501a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 2502a0292f23SGarrett Wollman */ 25039b8b58e0SJonathan Lemon tp->t_starttime = ticks; 2504dd7b86e2SGleb Smirnoff if ((tp->t_flags & TF_FASTOPEN) && tp->t_tfo_pending) { 2505281a0fd4SPatrick Kelsey tcp_fastopen_decrement_counter(tp->t_tfo_pending); 2506281a0fd4SPatrick Kelsey tp->t_tfo_pending = NULL; 2507281a0fd4SPatrick Kelsey } 25088db239dcSMichael Tuexen if (tp->t_flags & TF_NEEDFIN) { 250908af8aacSRandall Stewart tp->t_acktime = ticks; 25108db239dcSMichael Tuexen tcp_state_change(tp, TCPS_FIN_WAIT_1); 25118db239dcSMichael Tuexen tp->t_flags &= ~TF_NEEDFIN; 25128db239dcSMichael Tuexen } else { 25138db239dcSMichael Tuexen tcp_state_change(tp, TCPS_ESTABLISHED); 25148db239dcSMichael Tuexen TCP_PROBE5(accept__established, NULL, tp, 25158db239dcSMichael Tuexen m, tp, th); 2516281a0fd4SPatrick Kelsey /* 2517281a0fd4SPatrick Kelsey * TFO connections call cc_conn_init() during SYN 2518281a0fd4SPatrick Kelsey * processing. Calling it again here for such 2519281a0fd4SPatrick Kelsey * connections is not harmless as it would undo the 2520281a0fd4SPatrick Kelsey * snd_cwnd reduction that occurs when a TFO SYN|ACK 2521281a0fd4SPatrick Kelsey * is retransmitted. 2522281a0fd4SPatrick Kelsey */ 2523dd7b86e2SGleb Smirnoff if (!(tp->t_flags & TF_FASTOPEN)) 2524dbc42409SLawrence Stewart cc_conn_init(tp); 25259077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 25267ff19458SPaul Traina } 2527a0292f23SGarrett Wollman /* 2528b2ade6b1SRichard Scheffenegger * Account for the ACK of our SYN prior to 2529b2ade6b1SRichard Scheffenegger * regular ACK processing below, except for 2530b2ade6b1SRichard Scheffenegger * simultaneous SYN, which is handled later. 2531b2ade6b1SRichard Scheffenegger */ 2532b2ade6b1SRichard Scheffenegger if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN)) 2533b2ade6b1SRichard Scheffenegger incforsyn = 1; 2534b2ade6b1SRichard Scheffenegger /* 2535a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 2536a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 2537a0292f23SGarrett Wollman */ 2538032bf749SRichard Scheffenegger if (tlen == 0 && (thflags & TH_FIN) == 0) { 2539c28440dbSRandall Stewart (void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0, 2540a0292f23SGarrett Wollman (struct mbuf *)0); 2541c348e880SGleb Smirnoff tcp_handle_wakeup(tp); 2542032bf749SRichard Scheffenegger } 254360ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq - 1; 254493b0017fSPhilippe Charnier /* FALLTHROUGH */ 2545df8bae1dSRodney W. Grimes 2546df8bae1dSRodney W. Grimes /* 2547df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2548df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 2549fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 2550fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 2551df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 2552df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 2553df8bae1dSRodney W. Grimes */ 2554df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2555df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2556df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2557df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 2558df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2559df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 25605a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 256178b50714SRobert Watson TCPSTAT_INC(tcps_rcvacktoomuch); 25625a53ca16SPaul Saab goto dropafterack; 25635a53ca16SPaul Saab } 2564c21b7b55SRichard Scheffenegger if (tcp_is_sack_recovery(tp, &to)) { 2565893ed42eSRichard Scheffenegger sack_changed = tcp_sack_doack(tp, &to, th->th_ack); 2566893ed42eSRichard Scheffenegger if ((sack_changed != SACK_NOCHANGE) && 25670471a8c7SRichard Scheffenegger (tp->t_flags & TF_LRD)) { 25680471a8c7SRichard Scheffenegger tcp_sack_lost_retransmission(tp, th); 25690471a8c7SRichard Scheffenegger } 25700471a8c7SRichard Scheffenegger } else 257112eeb81fSHiren Panchasara /* 257212eeb81fSHiren Panchasara * Reset the value so that previous (valid) value 257312eeb81fSHiren Panchasara * from the last ack with SACK doesn't get used. 257412eeb81fSHiren Panchasara */ 257512eeb81fSHiren Panchasara tp->sackhint.sacked_bytes = 0; 257639bc9de5SLawrence Stewart 2577bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 257839bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 257939bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 2580bd79708dSJonathan T. Looney #endif 258139bc9de5SLawrence Stewart 2582fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 25830c39d38dSGleb Smirnoff maxseg = tcp_maxseg(tp); 2584021eaf79SHiren Panchasara if (tlen == 0 && 2585021eaf79SHiren Panchasara (tiwin == tp->snd_wnd || 2586021eaf79SHiren Panchasara (tp->t_flags & TF_SACK_PERMIT))) { 258762b90589SPeter Wemm /* 258862b90589SPeter Wemm * If this is the first time we've seen a 258962b90589SPeter Wemm * FIN from the remote, this is not a 259062b90589SPeter Wemm * duplicate and it needs to be processed 259162b90589SPeter Wemm * normally. This happens during a 259262b90589SPeter Wemm * simultaneous close. 259362b90589SPeter Wemm */ 259462b90589SPeter Wemm if ((thflags & TH_FIN) && 259562b90589SPeter Wemm (TCPS_HAVERCVDFIN(tp->t_state) == 0)) { 259662b90589SPeter Wemm tp->t_dupacks = 0; 259762b90589SPeter Wemm break; 259862b90589SPeter Wemm } 259978b50714SRobert Watson TCPSTAT_INC(tcps_rcvdupack); 2600df8bae1dSRodney W. Grimes /* 2601df8bae1dSRodney W. Grimes * If we have outstanding data (other than 2602df8bae1dSRodney W. Grimes * a window probe), this is a completely 2603df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 26045f30ec9bSEitan Adler * change and FIN isn't set), 26055f30ec9bSEitan Adler * the ack is the biggest we've 2606df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 2607a4641f4eSPedro F. Giffuni * threshold of them, assume a packet 2608df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 2609df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 2610df8bae1dSRodney W. Grimes * window so we send only this one 2611df8bae1dSRodney W. Grimes * packet. 2612df8bae1dSRodney W. Grimes * 2613df8bae1dSRodney W. Grimes * We know we're losing at the current 2614df8bae1dSRodney W. Grimes * window size so do congestion avoidance 2615df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 2616df8bae1dSRodney W. Grimes * and pull our congestion window back to 2617df8bae1dSRodney W. Grimes * the new ssthresh). 2618df8bae1dSRodney W. Grimes * 2619df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 2620df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 2621df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 2622df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 2623df8bae1dSRodney W. Grimes * network. 2624f2512ba1SRui Paulo * 2625f2512ba1SRui Paulo * When using TCP ECN, notify the peer that 2626f2512ba1SRui Paulo * we reduced the cwnd. 2627df8bae1dSRodney W. Grimes */ 2628021eaf79SHiren Panchasara /* 2629021eaf79SHiren Panchasara * Following 2 kinds of acks should not affect 2630021eaf79SHiren Panchasara * dupack counting: 2631021eaf79SHiren Panchasara * 1) Old acks 2632021eaf79SHiren Panchasara * 2) Acks with SACK but without any new SACK 2633021eaf79SHiren Panchasara * information in them. These could result from 2634021eaf79SHiren Panchasara * any anomaly in the network like a switch 2635021eaf79SHiren Panchasara * duplicating packets or a possible DoS attack. 2636021eaf79SHiren Panchasara */ 2637021eaf79SHiren Panchasara if (th->th_ack != tp->snd_una || 2638c21b7b55SRichard Scheffenegger (tcp_is_sack_recovery(tp, &to) && 26392d05a1c8SRichard Scheffenegger (sack_changed == SACK_NOCHANGE))) { 2640021eaf79SHiren Panchasara break; 26412d05a1c8SRichard Scheffenegger } else if (!tcp_timer_active(tp, TT_REXMT)) { 2642df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 26432d05a1c8SRichard Scheffenegger } else if (++tp->t_dupacks > tcprexmtthresh || 2644dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 26454b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 26464b7b743cSLawrence Stewart CC_DUPACK); 26470e1d7c25SRichard Scheffenegger if (V_tcp_do_prr && 264849a6fbe3SRichard Scheffenegger IN_FASTRECOVERY(tp->t_flags) && 264949a6fbe3SRichard Scheffenegger (tp->t_flags & TF_SACK_PERMIT)) { 2650c7c325d0SRichard Scheffenegger tcp_do_prr_ack(tp, th, &to, 2651c7c325d0SRichard Scheffenegger sack_changed, &maxseg); 2652c21b7b55SRichard Scheffenegger } else if (tcp_is_sack_recovery(tp, &to) && 26537dc78150SRichard Scheffenegger IN_FASTRECOVERY(tp->t_flags) && 26547dc78150SRichard Scheffenegger (tp->snd_nxt == tp->snd_max)) { 2655808f11b7SPaul Saab int awnd; 265625e6f9edSPaul Saab 265725e6f9edSPaul Saab /* 265825e6f9edSPaul Saab * Compute the amount of data in flight first. 26594c3972f0SHiren Panchasara * We can inject new data into the pipe iff 26607dc78150SRichard Scheffenegger * we have less than ssthresh 266125e6f9edSPaul Saab * worth of data in flight. 266225e6f9edSPaul Saab */ 26632d05a1c8SRichard Scheffenegger if (V_tcp_do_newsack) { 266412eeb81fSHiren Panchasara awnd = tcp_compute_pipe(tp); 26652d05a1c8SRichard Scheffenegger } else { 2666808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 26670077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 26682d05a1c8SRichard Scheffenegger } 2669808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 26707dc78150SRichard Scheffenegger tp->snd_cwnd += imax(maxseg, 26717dc78150SRichard Scheffenegger imin(2 * maxseg, 26727dc78150SRichard Scheffenegger tp->sackhint.delivered_data)); 267325e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 267425e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 267525e6f9edSPaul Saab } 26767dc78150SRichard Scheffenegger } else if (tcp_is_sack_recovery(tp, &to) && 26777dc78150SRichard Scheffenegger IN_FASTRECOVERY(tp->t_flags) && 26787dc78150SRichard Scheffenegger SEQ_LT(tp->snd_nxt, tp->snd_max)) { 26797dc78150SRichard Scheffenegger tp->snd_cwnd += imax(maxseg, 26807dc78150SRichard Scheffenegger imin(2 * maxseg, 26817dc78150SRichard Scheffenegger tp->sackhint.delivered_data)); 26822d05a1c8SRichard Scheffenegger } else { 26830c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 26842d05a1c8SRichard Scheffenegger } 268540fa3e40SGleb Smirnoff (void) tcp_output(tp); 268646f58482SJonathan Lemon goto drop; 26873c40e1d5SRichard Scheffenegger } else if (tp->t_dupacks == tcprexmtthresh || 26883c40e1d5SRichard Scheffenegger (tp->t_flags & TF_SACK_PERMIT && 2689d1de2b05SRichard Scheffenegger V_tcp_do_newsack && 26903c40e1d5SRichard Scheffenegger tp->sackhint.sacked_bytes > 26913c40e1d5SRichard Scheffenegger (tcprexmtthresh - 1) * maxseg)) { 26923c40e1d5SRichard Scheffenegger enter_recovery: 26933c40e1d5SRichard Scheffenegger /* 26943c40e1d5SRichard Scheffenegger * Above is the RFC6675 trigger condition of 26953c40e1d5SRichard Scheffenegger * more than (dupthresh-1)*maxseg sacked data. 26963c40e1d5SRichard Scheffenegger * If the count of holes in the 26973c40e1d5SRichard Scheffenegger * scoreboard is >= dupthresh, we could 26983c40e1d5SRichard Scheffenegger * also enter loss recovery, but don't 26993c40e1d5SRichard Scheffenegger * have that value readily available. 27003c40e1d5SRichard Scheffenegger */ 27013c40e1d5SRichard Scheffenegger tp->t_dupacks = tcprexmtthresh; 2702cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 2703a0445c2eSJayanth Vijayaraghavan 2704a0445c2eSJayanth Vijayaraghavan /* 27057dc78150SRichard Scheffenegger * If we're doing sack, check to 27067dc78150SRichard Scheffenegger * see if we're already in sack 2707a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 2708a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 2709a0445c2eSJayanth Vijayaraghavan * recovery. 2710a0445c2eSJayanth Vijayaraghavan */ 27117dc78150SRichard Scheffenegger if (tcp_is_sack_recovery(tp, &to)) { 2712dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2713a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 2714a0445c2eSJayanth Vijayaraghavan break; 2715a0445c2eSJayanth Vijayaraghavan } 2716dbc42409SLawrence Stewart } else { 2717a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 27189d11646dSJeffrey Hsu tp->snd_recover)) { 2719cb942153SJeffrey Hsu tp->t_dupacks = 0; 2720cb942153SJeffrey Hsu break; 272146f58482SJonathan Lemon } 2722a0445c2eSJayanth Vijayaraghavan } 2723dbc42409SLawrence Stewart /* Congestion signal before ack. */ 2724dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_NDUPACK); 27254b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, 27264b7b743cSLawrence Stewart CC_DUPACK); 2727b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 27289b8b58e0SJonathan Lemon tp->t_rtttime = 0; 27290e1d7c25SRichard Scheffenegger if (V_tcp_do_prr) { 27300e1d7c25SRichard Scheffenegger /* 27317dc78150SRichard Scheffenegger * snd_ssthresh and snd_recover are 27327dc78150SRichard Scheffenegger * already updated by cc_cong_signal. 27330e1d7c25SRichard Scheffenegger */ 2734c21b7b55SRichard Scheffenegger if (tcp_is_sack_recovery(tp, &to)) { 273549a6fbe3SRichard Scheffenegger /* 27367dc78150SRichard Scheffenegger * Include Limited Transmit 273749a6fbe3SRichard Scheffenegger * segments here 273849a6fbe3SRichard Scheffenegger */ 2739e9071000SRichard Scheffenegger tp->sackhint.prr_delivered = 27407dc78150SRichard Scheffenegger imin(tp->snd_max - th->th_ack, 27417dc78150SRichard Scheffenegger (tp->snd_limited + 1) * maxseg); 274274d7fc87SRichard Scheffenegger } else { 274374d7fc87SRichard Scheffenegger tp->sackhint.prr_delivered = 27447dc78150SRichard Scheffenegger maxseg; 274574d7fc87SRichard Scheffenegger } 2746bc7ee8e5SRichard Scheffenegger tp->sackhint.recover_fs = max(1, 2747bc7ee8e5SRichard Scheffenegger tp->snd_nxt - tp->snd_una); 27480e1d7c25SRichard Scheffenegger } 27497dc78150SRichard Scheffenegger tp->snd_limited = 0; 2750c21b7b55SRichard Scheffenegger if (tcp_is_sack_recovery(tp, &to)) { 27512d05a1c8SRichard Scheffenegger TCPSTAT_INC(tcps_sack_recovery_episode); 27527dc78150SRichard Scheffenegger /* 27537dc78150SRichard Scheffenegger * When entering LR after RTO due to 27547dc78150SRichard Scheffenegger * Duplicate ACKs, retransmit existing 27557dc78150SRichard Scheffenegger * holes from the scoreboard. 27567dc78150SRichard Scheffenegger */ 27577dc78150SRichard Scheffenegger tcp_resend_sackholes(tp); 27587dc78150SRichard Scheffenegger /* Avoid inflating cwnd in tcp_output */ 27597dc78150SRichard Scheffenegger tp->snd_nxt = tp->snd_max; 27607dc78150SRichard Scheffenegger tp->snd_cwnd = tcp_compute_pipe(tp) + 27617dc78150SRichard Scheffenegger maxseg; 276240fa3e40SGleb Smirnoff (void) tcp_output(tp); 27637dc78150SRichard Scheffenegger /* Set cwnd to the expected flightsize */ 27647dc78150SRichard Scheffenegger tp->snd_cwnd = tp->snd_ssthresh; 27652d05a1c8SRichard Scheffenegger if (SEQ_GT(th->th_ack, tp->snd_una)) { 27663c40e1d5SRichard Scheffenegger goto resume_partialack; 27672d05a1c8SRichard Scheffenegger } 27686d90faf3SPaul Saab goto drop; 27696d90faf3SPaul Saab } 2770fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 27710c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg; 277240fa3e40SGleb Smirnoff (void) tcp_output(tp); 277348d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 2774302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 2775302ce8d6SAndre Oppermann __func__)); 2776df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 27770c39d38dSGleb Smirnoff maxseg * 277848d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 2779df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 2780df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 2781df8bae1dSRodney W. Grimes goto drop; 2782603724d3SBjoern A. Zeeb } else if (V_tcp_do_rfc3042) { 278362d4443fSHiren Panchasara /* 278462d4443fSHiren Panchasara * Process first and second duplicate 278562d4443fSHiren Panchasara * ACKs. Each indicates a segment 278662d4443fSHiren Panchasara * leaving the network, creating room 278762d4443fSHiren Panchasara * for more. Make sure we can send a 278862d4443fSHiren Panchasara * packet on reception of each duplicate 278962d4443fSHiren Panchasara * ACK by increasing snd_cwnd by one 279062d4443fSHiren Panchasara * segment. Restore the original 279162d4443fSHiren Panchasara * snd_cwnd after packet transmission. 279262d4443fSHiren Panchasara */ 27932d05a1c8SRichard Scheffenegger cc_ack_received(tp, th, nsegs, CC_DUPACK); 27943ac12506SJonathan T. Looney uint32_t oldcwnd = tp->snd_cwnd; 279548d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 279648d2549cSJeffrey Hsu u_int sent; 27975628dd08SAndre Oppermann int avail; 279889c02376SJeffrey Hsu 2799582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 2800582a954bSJeffrey Hsu tp->t_dupacks == 2, 2801302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 2802302ce8d6SAndre Oppermann __func__)); 280361a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 280448d2549cSJeffrey Hsu tp->snd_limited = 0; 2805440f4ba1SRichard Scheffenegger if ((tp->snd_nxt == tp->snd_max) && 2806440f4ba1SRichard Scheffenegger (tp->t_rxtshift == 0)) 280761a36e3dSJeffrey Hsu tp->snd_cwnd = 2808440f4ba1SRichard Scheffenegger SEQ_SUB(tp->snd_nxt, 28097dc78150SRichard Scheffenegger tp->snd_una) - 28107dc78150SRichard Scheffenegger tcp_sack_adjust(tp); 2811440f4ba1SRichard Scheffenegger tp->snd_cwnd += 281261a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 28138f5a2e21SRichard Scheffenegger maxseg - tcp_sack_adjust(tp); 28145628dd08SAndre Oppermann /* 28155628dd08SAndre Oppermann * Only call tcp_output when there 28165ae83e0dSMichael Tuexen * is new data available to be sent 28175ae83e0dSMichael Tuexen * or we need to send an ACK. 28185628dd08SAndre Oppermann */ 2819dded4e9eSRichard Scheffenegger SOCK_SENDBUF_LOCK(so); 28202d05a1c8SRichard Scheffenegger avail = sbavail(&so->so_snd); 2821dded4e9eSRichard Scheffenegger SOCK_SENDBUF_UNLOCK(so); 28222d05a1c8SRichard Scheffenegger if (tp->t_flags & TF_ACKNOW || 28232d05a1c8SRichard Scheffenegger (avail >= 28242d05a1c8SRichard Scheffenegger SEQ_SUB(tp->snd_nxt, tp->snd_una))) { 282540fa3e40SGleb Smirnoff (void) tcp_output(tp); 28262d05a1c8SRichard Scheffenegger } 28272d05a1c8SRichard Scheffenegger sent = SEQ_SUB(tp->snd_max, oldsndmax); 28280c39d38dSGleb Smirnoff if (sent > maxseg) { 282989c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 283089c02376SJeffrey Hsu tp->snd_limited == 0) || 28310c39d38dSGleb Smirnoff (sent == maxseg + 1 && 283289c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 2833302ce8d6SAndre Oppermann ("%s: sent too much", 2834302ce8d6SAndre Oppermann __func__)); 283548d2549cSJeffrey Hsu tp->snd_limited = 2; 28362d05a1c8SRichard Scheffenegger } else if (sent > 0) { 283748d2549cSJeffrey Hsu ++tp->snd_limited; 28382d05a1c8SRichard Scheffenegger } 2839582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 2840582a954bSJeffrey Hsu goto drop; 2841df8bae1dSRodney W. Grimes } 2842021eaf79SHiren Panchasara } 2843df8bae1dSRodney W. Grimes break; 2844021eaf79SHiren Panchasara } else { 2845021eaf79SHiren Panchasara /* 2846021eaf79SHiren Panchasara * This ack is advancing the left edge, reset the 2847021eaf79SHiren Panchasara * counter. 2848021eaf79SHiren Panchasara */ 2849021eaf79SHiren Panchasara tp->t_dupacks = 0; 2850021eaf79SHiren Panchasara /* 2851021eaf79SHiren Panchasara * If this ack also has new SACK info, increment the 2852f359d6ebSRichard Scheffenegger * counter as per rfc6675. The variable 2853f359d6ebSRichard Scheffenegger * sack_changed tracks all changes to the SACK 2854f359d6ebSRichard Scheffenegger * scoreboard, including when partial ACKs without 2855f359d6ebSRichard Scheffenegger * SACK options are received, and clear the scoreboard 2856f359d6ebSRichard Scheffenegger * from the left side. Such partial ACKs should not be 2857f359d6ebSRichard Scheffenegger * counted as dupacks here. 2858021eaf79SHiren Panchasara */ 2859c21b7b55SRichard Scheffenegger if (tcp_is_sack_recovery(tp, &to) && 2860440f4ba1SRichard Scheffenegger (((tp->t_rxtshift == 0) && (sack_changed != SACK_NOCHANGE)) || 2861440f4ba1SRichard Scheffenegger ((tp->t_rxtshift > 0) && (sack_changed == SACK_NEWLOSS))) && 2862440f4ba1SRichard Scheffenegger (tp->snd_nxt == tp->snd_max)) { 2863021eaf79SHiren Panchasara tp->t_dupacks++; 28643c40e1d5SRichard Scheffenegger /* limit overhead by setting maxseg last */ 28653c40e1d5SRichard Scheffenegger if (!IN_FASTRECOVERY(tp->t_flags) && 28663c40e1d5SRichard Scheffenegger (tp->sackhint.sacked_bytes > 28673c40e1d5SRichard Scheffenegger ((tcprexmtthresh - 1) * 28683c40e1d5SRichard Scheffenegger (maxseg = tcp_maxseg(tp))))) { 28693c40e1d5SRichard Scheffenegger goto enter_recovery; 28703c40e1d5SRichard Scheffenegger } 28713c40e1d5SRichard Scheffenegger } 2872df8bae1dSRodney W. Grimes } 2873cb942153SJeffrey Hsu 28743c40e1d5SRichard Scheffenegger resume_partialack: 2875302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2876302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 2877cb942153SJeffrey Hsu 2878df8bae1dSRodney W. Grimes /* 2879df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2880df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2881df8bae1dSRodney W. Grimes */ 2882cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 28830b3f9e43SRichard Scheffenegger if (IN_FASTRECOVERY(tp->t_flags)) { 2884c7c325d0SRichard Scheffenegger if (tp->t_flags & TF_SACK_PERMIT) { 28850b3f9e43SRichard Scheffenegger if (V_tcp_do_prr && 28860b3f9e43SRichard Scheffenegger (to.to_flags & TOF_SACK)) { 28870b3f9e43SRichard Scheffenegger tcp_timer_activate(tp, 28880b3f9e43SRichard Scheffenegger TT_REXMT, 0); 2889eb3a59a8SRichard Scheffenegger tp->t_rtttime = 0; 2890c7c325d0SRichard Scheffenegger tcp_do_prr_ack(tp, th, &to, 2891c7c325d0SRichard Scheffenegger sack_changed, &maxseg); 2892eb3a59a8SRichard Scheffenegger tp->t_flags |= TF_ACKNOW; 2893eb3a59a8SRichard Scheffenegger (void) tcp_output(tp); 2894c7c325d0SRichard Scheffenegger } else { 2895c7c325d0SRichard Scheffenegger tcp_sack_partialack(tp, th, 2896c7c325d0SRichard Scheffenegger &maxseg); 2897c7c325d0SRichard Scheffenegger } 2898c7c325d0SRichard Scheffenegger } else { 2899c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2900c7c325d0SRichard Scheffenegger } 29010b3f9e43SRichard Scheffenegger } else if (IN_CONGRECOVERY(tp->t_flags) && 29020b3f9e43SRichard Scheffenegger (V_tcp_do_prr)) { 29030b3f9e43SRichard Scheffenegger tp->sackhint.delivered_data = 29040b3f9e43SRichard Scheffenegger BYTES_THIS_ACK(tp, th); 2905b9f803b7SRichard Scheffenegger tp->snd_fack = th->th_ack; 290649a6fbe3SRichard Scheffenegger /* 290749a6fbe3SRichard Scheffenegger * During ECN cwnd reduction 290849a6fbe3SRichard Scheffenegger * always use PRR-SSRB 290949a6fbe3SRichard Scheffenegger */ 2910c7c325d0SRichard Scheffenegger tcp_do_prr_ack(tp, th, &to, SACK_CHANGE, 2911c7c325d0SRichard Scheffenegger &maxseg); 2912b9f803b7SRichard Scheffenegger (void) tcp_output(tp); 2913b9f803b7SRichard Scheffenegger } 2914c7c325d0SRichard Scheffenegger } 2915a0292f23SGarrett Wollman /* 2916a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2917a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2918a0292f23SGarrett Wollman */ 2919a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2920a0292f23SGarrett Wollman /* 2921a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2922a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 292307e43e10SAndras Olah * synchronized). Go to non-starred state, 292407e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 292507e43e10SAndras Olah * we can do window scaling. 2926a0292f23SGarrett Wollman */ 2927a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2928a0292f23SGarrett Wollman tp->snd_una++; 292907e43e10SAndras Olah /* Do window scaling? */ 293007e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 293107e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 293207e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2933464fcfbcSAndre Oppermann /* Send window already scaled. */ 293407e43e10SAndras Olah } 2935a0292f23SGarrett Wollman } 2936a0292f23SGarrett Wollman 2937a0292f23SGarrett Wollman process_ACK: 29389eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(inp); 29397cfc6904SRobert Watson 2940b2ade6b1SRichard Scheffenegger /* 2941b2ade6b1SRichard Scheffenegger * Adjust for the SYN bit in sequence space, 2942b2ade6b1SRichard Scheffenegger * but don't account for it in cwnd calculations. 2943b2ade6b1SRichard Scheffenegger * This is for the SYN_RECEIVED, non-simultaneous 2944b2ade6b1SRichard Scheffenegger * SYN case. SYN_SENT and simultaneous SYN are 2945b2ade6b1SRichard Scheffenegger * treated elsewhere. 2946b2ade6b1SRichard Scheffenegger */ 2947b2ade6b1SRichard Scheffenegger if (incforsyn) 2948b2ade6b1SRichard Scheffenegger tp->snd_una++; 2949dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 2950b8c2cd15SJonathan T. Looney KASSERT(acked >= 0, ("%s: acked unexepectedly negative " 2951b8c2cd15SJonathan T. Looney "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__, 2952b8c2cd15SJonathan T. Looney tp->snd_una, th->th_ack, tp, m)); 29534b7b743cSLawrence Stewart TCPSTAT_ADD(tcps_rcvackpack, nsegs); 295478b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 2955df8bae1dSRodney W. Grimes 2956df8bae1dSRodney W. Grimes /* 29579b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 29589b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 29599b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 29609b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 29619b8b58e0SJonathan Lemon * we left off. 29629b8b58e0SJonathan Lemon */ 296310d20c84SMatt Macy if (tp->t_rxtshift == 1 && 296410d20c84SMatt Macy tp->t_flags & TF_PREVVALID && 29654531b345SRichard Scheffenegger tp->t_badrxtwin != 0 && 29664531b345SRichard Scheffenegger to.to_flags & TOF_TS && 29674531b345SRichard Scheffenegger to.to_tsecr != 0 && 29684531b345SRichard Scheffenegger TSTMP_LT(to.to_tsecr, tp->t_badrxtwin)) 2969dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 29709b8b58e0SJonathan Lemon 29719b8b58e0SJonathan Lemon /* 2972df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2973df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2974df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2975df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2976df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2977df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2978df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2979fa55172bSMatthew Dillon * 2980fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2981fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2982fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2983fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2984df8bae1dSRodney W. Grimes */ 2985d8951c8aSBjoern A. Zeeb if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) { 29863ac12506SJonathan T. Looney uint32_t t; 2987d8951c8aSBjoern A. Zeeb 2988d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 2989d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 2990d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 2991d8951c8aSBjoern A. Zeeb tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1); 2992fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2993eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2994eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 29959b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2996fa55172bSMatthew Dillon } 2997df8bae1dSRodney W. Grimes 2998dded4e9eSRichard Scheffenegger SOCK_SENDBUF_LOCK(so); 299908af8aacSRandall Stewart /* 300008af8aacSRandall Stewart * Clear t_acktime if remote side has ACKd all data in the 300108af8aacSRandall Stewart * socket buffer and FIN (if applicable). 300208af8aacSRandall Stewart * Otherwise, update t_acktime if we received a sufficiently 300308af8aacSRandall Stewart * large ACK. 300408af8aacSRandall Stewart */ 300508af8aacSRandall Stewart if ((tp->t_state <= TCPS_CLOSE_WAIT && 300608af8aacSRandall Stewart acked == sbavail(&so->so_snd)) || 300708af8aacSRandall Stewart acked > sbavail(&so->so_snd)) 300808af8aacSRandall Stewart tp->t_acktime = 0; 300908af8aacSRandall Stewart else if (acked > 1) 301008af8aacSRandall Stewart tp->t_acktime = ticks; 301108af8aacSRandall Stewart 3012df8bae1dSRodney W. Grimes /* 3013df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 3014df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 3015df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 3016df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 3017df8bae1dSRodney W. Grimes */ 3018fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 3019b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 3020df8bae1dSRodney W. Grimes needoutput = 1; 3021b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 302208af8aacSRandall Stewart tcp_timer_activate(tp, TT_REXMT, TP_RXTCUR(tp)); 3023a0292f23SGarrett Wollman 3024a0292f23SGarrett Wollman /* 3025a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 3026a0292f23SGarrett Wollman * skip rest of ACK processing. 3027a0292f23SGarrett Wollman */ 302808af8aacSRandall Stewart if (acked == 0) { 3029dded4e9eSRichard Scheffenegger SOCK_SENDBUF_UNLOCK(so); 3030a0292f23SGarrett Wollman goto step6; 303108af8aacSRandall Stewart } 3032a0292f23SGarrett Wollman 3033df8bae1dSRodney W. Grimes /* 3034dbc42409SLawrence Stewart * Let the congestion control algorithm update congestion 3035dbc42409SLawrence Stewart * control related information. This typically means increasing 3036dbc42409SLawrence Stewart * the congestion window. 3037df8bae1dSRodney W. Grimes */ 30384b7b743cSLawrence Stewart cc_ack_received(tp, th, nsegs, CC_ACK); 3039dbc42409SLawrence Stewart 3040cfa6009eSGleb Smirnoff if (acked > sbavail(&so->so_snd)) { 3041b8c2cd15SJonathan T. Looney if (tp->snd_wnd >= sbavail(&so->so_snd)) 3042cfa6009eSGleb Smirnoff tp->snd_wnd -= sbavail(&so->so_snd); 3043b8c2cd15SJonathan T. Looney else 3044b8c2cd15SJonathan T. Looney tp->snd_wnd = 0; 3045c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, 3046cfa6009eSGleb Smirnoff (int)sbavail(&so->so_snd)); 3047df8bae1dSRodney W. Grimes ourfinisacked = 1; 3048df8bae1dSRodney W. Grimes } else { 3049c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, acked); 30503ac12506SJonathan T. Looney if (tp->snd_wnd >= (uint32_t) acked) 305160ee3bb2SAndre Oppermann tp->snd_wnd -= acked; 3052b8c2cd15SJonathan T. Looney else 3053b8c2cd15SJonathan T. Looney tp->snd_wnd = 0; 3054df8bae1dSRodney W. Grimes ourfinisacked = 0; 3055df8bae1dSRodney W. Grimes } 3056032bf749SRichard Scheffenegger /* NB: sowwakeup_locked() does an implicit unlock. */ 3057032bf749SRichard Scheffenegger sowwakeup_locked(so); 3058c11a15bfSGleb Smirnoff m_freem(mfree); 3059e8949f74SAndre Oppermann /* Detect una wraparound. */ 3060dbc42409SLawrence Stewart if (!IN_RECOVERY(tp->t_flags) && 30619d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 30629d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 30639d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 30640b3f9e43SRichard Scheffenegger tp->snd_una = th->th_ack; 3065dbc42409SLawrence Stewart if (IN_RECOVERY(tp->t_flags) && 306624cb0f22SLawrence Stewart SEQ_GEQ(th->th_ack, tp->snd_recover)) { 30670b3f9e43SRichard Scheffenegger cc_post_recovery(tp, th); 306824cb0f22SLawrence Stewart } 30697dc78150SRichard Scheffenegger if (SEQ_GT(tp->snd_una, tp->snd_recover)) { 30706d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 30716d90faf3SPaul Saab } 3072df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 3073df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 3074df8bae1dSRodney W. Grimes 3075df8bae1dSRodney W. Grimes switch (tp->t_state) { 3076df8bae1dSRodney W. Grimes /* 3077df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 3078df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 3079df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 3080df8bae1dSRodney W. Grimes */ 3081df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 3082df8bae1dSRodney W. Grimes if (ourfinisacked) { 3083df8bae1dSRodney W. Grimes /* 3084df8bae1dSRodney W. Grimes * If we can't receive any more 3085df8bae1dSRodney W. Grimes * data, then closing user can proceed. 3086df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 3087df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 3088df8bae1dSRodney W. Grimes * we'll hang forever. 3089340c35deSJonathan Lemon */ 3090c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 30913eeb22cbSRichard Scheffenegger tcp_free_sackholes(tp); 309203e49181SSeigo Tanimura soisdisconnected(so); 30939077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 30949077f387SGleb Smirnoff (tcp_fast_finwait2_recycle ? 30959077f387SGleb Smirnoff tcp_finwait2_timeout : 30969077f387SGleb Smirnoff TP_MAXIDLE(tp))); 30974cc20ab1SSeigo Tanimura } 309857f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_2); 3099df8bae1dSRodney W. Grimes } 3100df8bae1dSRodney W. Grimes break; 3101df8bae1dSRodney W. Grimes 3102df8bae1dSRodney W. Grimes /* 3103df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 3104df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 3105df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 3106df8bae1dSRodney W. Grimes * the segment. 3107df8bae1dSRodney W. Grimes */ 3108df8bae1dSRodney W. Grimes case TCPS_CLOSING: 3109df8bae1dSRodney W. Grimes if (ourfinisacked) { 311011a20fb8SJeffrey Hsu tcp_twstart(tp); 3111340c35deSJonathan Lemon m_freem(m); 3112679d9708SAndre Oppermann return; 3113df8bae1dSRodney W. Grimes } 3114df8bae1dSRodney W. Grimes break; 3115df8bae1dSRodney W. Grimes 3116df8bae1dSRodney W. Grimes /* 3117df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 3118df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 3119df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 3120df8bae1dSRodney W. Grimes * enter the closed state and return. 3121df8bae1dSRodney W. Grimes */ 3122df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 3123df8bae1dSRodney W. Grimes if (ourfinisacked) { 3124df8bae1dSRodney W. Grimes tp = tcp_close(tp); 3125df8bae1dSRodney W. Grimes goto drop; 3126df8bae1dSRodney W. Grimes } 3127df8bae1dSRodney W. Grimes break; 3128df8bae1dSRodney W. Grimes } 3129df8bae1dSRodney W. Grimes } 3130df8bae1dSRodney W. Grimes 3131df8bae1dSRodney W. Grimes step6: 31329eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(inp); 31337cfc6904SRobert Watson 3134df8bae1dSRodney W. Grimes /* 313560ee3bb2SAndre Oppermann * Update window information. 313660ee3bb2SAndre Oppermann * Don't look at window if no ACK: TAC's send garbage on first SYN. 3137df8bae1dSRodney W. Grimes */ 313860ee3bb2SAndre Oppermann if ((thflags & TH_ACK) && 313960ee3bb2SAndre Oppermann (SEQ_LT(tp->snd_wl1, th->th_seq) || 314060ee3bb2SAndre Oppermann (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 314160ee3bb2SAndre Oppermann (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 314260ee3bb2SAndre Oppermann /* keep track of pure window updates */ 314360ee3bb2SAndre Oppermann if (tlen == 0 && 314460ee3bb2SAndre Oppermann tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 314578b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinupd); 3146df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 314760ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 314860ee3bb2SAndre Oppermann tp->snd_wl2 = th->th_ack; 3149df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 3150df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 315160ee3bb2SAndre Oppermann needoutput = 1; 3152df8bae1dSRodney W. Grimes } 3153df8bae1dSRodney W. Grimes 3154df8bae1dSRodney W. Grimes /* 3155df8bae1dSRodney W. Grimes * Process segments with URG. 3156df8bae1dSRodney W. Grimes */ 3157fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 3158df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 3159df8bae1dSRodney W. Grimes /* 3160df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 3161df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 3162df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 3163df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 3164df8bae1dSRodney W. Grimes */ 3165dded4e9eSRichard Scheffenegger SOCK_RECVBUF_LOCK(so); 3166cfa6009eSGleb Smirnoff if (th->th_urp + sbavail(&so->so_rcv) > sb_max) { 3167fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 3168fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 3169dded4e9eSRichard Scheffenegger SOCK_RECVBUF_UNLOCK(so); /* XXX */ 3170df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 3171df8bae1dSRodney W. Grimes } 3172df8bae1dSRodney W. Grimes /* 3173df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 3174df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 3175df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 3176df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 3177df8bae1dSRodney W. Grimes * In these states we ignore the URG. 3178df8bae1dSRodney W. Grimes * 3179df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 3180df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 3181df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 3182df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 3183df8bae1dSRodney W. Grimes * of data past the urgent section as the original 3184df8bae1dSRodney W. Grimes * spec states (in one of two places). 3185df8bae1dSRodney W. Grimes */ 3186fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 3187fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 3188cfa6009eSGleb Smirnoff so->so_oobmark = sbavail(&so->so_rcv) + 3189df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 3190927c5ceaSRobert Watson if (so->so_oobmark == 0) 3191c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 3192df8bae1dSRodney W. Grimes sohasoutofband(so); 3193df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 3194df8bae1dSRodney W. Grimes } 3195dded4e9eSRichard Scheffenegger SOCK_RECVBUF_UNLOCK(so); 3196df8bae1dSRodney W. Grimes /* 3197df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 3198df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 3199df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 3200df8bae1dSRodney W. Grimes * data may creep in... ick. 3201df8bae1dSRodney W. Grimes */ 32023ac12506SJonathan T. Looney if (th->th_urp <= (uint32_t)tlen && 320330613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 320430613f56SJeffrey Hsu /* hdr drop is delayed */ 320530613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 320630613f56SJeffrey Hsu } 3207c068736aSJeffrey Hsu } else { 3208df8bae1dSRodney W. Grimes /* 3209df8bae1dSRodney W. Grimes * If no out of band data is expected, 3210df8bae1dSRodney W. Grimes * pull receive urgent pointer along 3211df8bae1dSRodney W. Grimes * with the receive window. 3212df8bae1dSRodney W. Grimes */ 3213df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 3214df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 3215c068736aSJeffrey Hsu } 3216df8bae1dSRodney W. Grimes dodata: /* XXX */ 32179eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(inp); 32187cfc6904SRobert Watson 3219df8bae1dSRodney W. Grimes /* 3220df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 3221df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 3222df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 3223df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 3224df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 3225df8bae1dSRodney W. Grimes * connection then we just ignore the text. 3226df8bae1dSRodney W. Grimes */ 3227281a0fd4SPatrick Kelsey tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) && 3228dd7b86e2SGleb Smirnoff (tp->t_flags & TF_FASTOPEN)); 3229f1ea4e41SRandall Stewart if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) && 3230df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 323169e03620SPaul Saab tcp_seq save_start = th->th_seq; 32325acfd95cSMichael Tuexen tcp_seq save_rnxt = tp->rcv_nxt; 32335acfd95cSMichael Tuexen int save_tlen = tlen; 3234fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 3235e4b64281SJesper Skriver /* 3236c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 3237c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 3238c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 3239c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 3240c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 3241c068736aSJeffrey Hsu * and removal from the queue and repetition of various 3242c068736aSJeffrey Hsu * conversions. 3243c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 3244c068736aSJeffrey Hsu * immediately when segments are out of order (so 3245c068736aSJeffrey Hsu * fast retransmit can work). 3246e4b64281SJesper Skriver */ 32474741bfcbSPatrick Kelsey if (th->th_seq == tp->rcv_nxt && 3248c28440dbSRandall Stewart SEGQ_EMPTY(tp) && 3249281a0fd4SPatrick Kelsey (TCPS_HAVEESTABLISHED(tp->t_state) || 3250281a0fd4SPatrick Kelsey tfo_syn)) { 3251281a0fd4SPatrick Kelsey if (DELAY_ACK(tp, tlen) || tfo_syn) 32523bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 3253e4b64281SJesper Skriver else 3254e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 3255e4b64281SJesper Skriver tp->rcv_nxt += tlen; 3256e854dd38SRandall Stewart if (tlen && 3257e854dd38SRandall Stewart ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 3258e854dd38SRandall Stewart (tp->t_fbyte_in == 0)) { 3259e854dd38SRandall Stewart tp->t_fbyte_in = ticks; 3260e854dd38SRandall Stewart if (tp->t_fbyte_in == 0) 3261e854dd38SRandall Stewart tp->t_fbyte_in = 1; 3262e854dd38SRandall Stewart if (tp->t_fbyte_out && tp->t_fbyte_in) 3263e854dd38SRandall Stewart tp->t_flags2 |= TF2_FBYTES_COMPLETE; 3264e854dd38SRandall Stewart } 32651ebf4607SRichard Scheffenegger thflags = tcp_get_flags(th) & TH_FIN; 326678b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 326778b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 3268dded4e9eSRichard Scheffenegger SOCK_RECVBUF_LOCK(so); 3269c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 3270c1c36a2cSMike Silbersack m_freem(m); 3271c1c36a2cSMike Silbersack else 3272651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 32734d0770f1SRichard Scheffenegger tp->t_flags |= TF_WAKESOR; 3274e4b64281SJesper Skriver } else { 3275e8949f74SAndre Oppermann /* 3276e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 3277e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 3278e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 3279e8949f74SAndre Oppermann * when trimming from the head. 3280e8949f74SAndre Oppermann */ 32815acfd95cSMichael Tuexen tcp_seq temp = save_start; 32824747500dSRandall Stewart 32835acfd95cSMichael Tuexen thflags = tcp_reass(tp, th, &temp, &tlen, m); 3284e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 3285e4b64281SJesper Skriver } 328640f41eceSMichael Tuexen if ((tp->t_flags & TF_SACK_PERMIT) && 328740f41eceSMichael Tuexen (save_tlen > 0) && 328840f41eceSMichael Tuexen TCPS_HAVEESTABLISHED(tp->t_state)) { 32896d261981SMichael Tuexen if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) { 3290b5a154d8SMichael Tuexen /* 3291b5a154d8SMichael Tuexen * DSACK actually handled in the fastpath 3292b5a154d8SMichael Tuexen * above. 3293b5a154d8SMichael Tuexen */ 3294ecc5b1d1SMichael Tuexen tcp_update_sack_list(tp, save_start, 3295ecc5b1d1SMichael Tuexen save_start + save_tlen); 3296ecc5b1d1SMichael Tuexen } else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) { 3297ecc5b1d1SMichael Tuexen if ((tp->rcv_numsacks >= 1) && 3298ecc5b1d1SMichael Tuexen (tp->sackblks[0].end == save_start)) { 32996d261981SMichael Tuexen /* 33006d261981SMichael Tuexen * Partial overlap, recorded at todrop 33016d261981SMichael Tuexen * above. 33026d261981SMichael Tuexen */ 33036d261981SMichael Tuexen tcp_update_sack_list(tp, 33046d261981SMichael Tuexen tp->sackblks[0].start, 3305ecc5b1d1SMichael Tuexen tp->sackblks[0].end); 3306ecc5b1d1SMichael Tuexen } else { 3307ecc5b1d1SMichael Tuexen tcp_update_dsack_list(tp, save_start, 3308ecc5b1d1SMichael Tuexen save_start + save_tlen); 3309ecc5b1d1SMichael Tuexen } 33106d261981SMichael Tuexen } else if (tlen >= save_tlen) { 3311b5a154d8SMichael Tuexen /* Update of sackblks. */ 3312ecc5b1d1SMichael Tuexen tcp_update_dsack_list(tp, save_start, 3313ecc5b1d1SMichael Tuexen save_start + save_tlen); 3314ecc5b1d1SMichael Tuexen } else if (tlen > 0) { 3315ecc5b1d1SMichael Tuexen tcp_update_dsack_list(tp, save_start, 3316ecc5b1d1SMichael Tuexen save_start + tlen); 33175acfd95cSMichael Tuexen } 33185acfd95cSMichael Tuexen } 3319c348e880SGleb Smirnoff tcp_handle_wakeup(tp); 3320302ce8d6SAndre Oppermann #if 0 3321df8bae1dSRodney W. Grimes /* 3322df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 3323df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 3324df8bae1dSRodney W. Grimes * buffer size. 3325302ce8d6SAndre Oppermann * XXX: Unused. 3326df8bae1dSRodney W. Grimes */ 3327f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) 3328df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 3329f701e30dSJohn Baldwin else 3330f701e30dSJohn Baldwin len = so->so_rcv.sb_hiwat; 3331302ce8d6SAndre Oppermann #endif 3332df8bae1dSRodney W. Grimes } else { 3333e8c149abSMichael Tuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 3334e8c149abSMichael Tuexen if (tlen > 0) { 3335e8c149abSMichael Tuexen if ((thflags & TH_FIN) != 0) { 3336e8c149abSMichael Tuexen log(LOG_DEBUG, "%s; %s: %s: " 3337e8c149abSMichael Tuexen "Received %d bytes of data and FIN " 3338e8c149abSMichael Tuexen "after having received a FIN, " 3339e8c149abSMichael Tuexen "just dropping both\n", 3340e8c149abSMichael Tuexen s, __func__, 3341e8c149abSMichael Tuexen tcpstates[tp->t_state], tlen); 3342e8c149abSMichael Tuexen } else { 3343e8c149abSMichael Tuexen log(LOG_DEBUG, "%s; %s: %s: " 3344e8c149abSMichael Tuexen "Received %d bytes of data " 3345e8c149abSMichael Tuexen "after having received a FIN, " 3346e8c149abSMichael Tuexen "just dropping it\n", 3347e8c149abSMichael Tuexen s, __func__, 3348e8c149abSMichael Tuexen tcpstates[tp->t_state], tlen); 3349e8c149abSMichael Tuexen } 3350e8c149abSMichael Tuexen } else { 3351e8c149abSMichael Tuexen if ((thflags & TH_FIN) != 0) { 3352e8c149abSMichael Tuexen log(LOG_DEBUG, "%s; %s: %s: " 3353e8c149abSMichael Tuexen "Received FIN " 3354e8c149abSMichael Tuexen "after having received a FIN, " 3355e8c149abSMichael Tuexen "just dropping it\n", 3356e8c149abSMichael Tuexen s, __func__, 3357e8c149abSMichael Tuexen tcpstates[tp->t_state]); 3358e8c149abSMichael Tuexen } 3359e8c149abSMichael Tuexen } 3360e8c149abSMichael Tuexen free(s, M_TCPLOG); 3361e8c149abSMichael Tuexen } 3362df8bae1dSRodney W. Grimes m_freem(m); 3363fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 3364df8bae1dSRodney W. Grimes } 3365df8bae1dSRodney W. Grimes 3366df8bae1dSRodney W. Grimes /* 3367df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 3368df8bae1dSRodney W. Grimes * that the connection is closing. 3369df8bae1dSRodney W. Grimes */ 3370fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 3371df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 33724d0770f1SRichard Scheffenegger /* The socket upcall is handled by socantrcvmore. */ 3373032bf749SRichard Scheffenegger socantrcvmore(so); 3374a0292f23SGarrett Wollman /* 3375a0292f23SGarrett Wollman * If connection is half-synchronized 3376d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 3377a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 3378a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 3379a0292f23SGarrett Wollman * more input can be expected, send ACK now. 3380a0292f23SGarrett Wollman */ 33813bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 33823bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 3383a0292f23SGarrett Wollman else 3384df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 3385df8bae1dSRodney W. Grimes tp->rcv_nxt++; 3386df8bae1dSRodney W. Grimes } 3387df8bae1dSRodney W. Grimes switch (tp->t_state) { 3388df8bae1dSRodney W. Grimes /* 3389df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 3390df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 3391df8bae1dSRodney W. Grimes */ 3392df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 33939b8b58e0SJonathan Lemon tp->t_starttime = ticks; 33949b8b58e0SJonathan Lemon /* FALLTHROUGH */ 3395df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 339657f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSE_WAIT); 3397df8bae1dSRodney W. Grimes break; 3398df8bae1dSRodney W. Grimes 3399df8bae1dSRodney W. Grimes /* 3400df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 3401df8bae1dSRodney W. Grimes * enter the CLOSING state. 3402df8bae1dSRodney W. Grimes */ 3403df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 340457f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSING); 3405df8bae1dSRodney W. Grimes break; 3406df8bae1dSRodney W. Grimes 3407df8bae1dSRodney W. Grimes /* 3408df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 3409df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 3410df8bae1dSRodney W. Grimes * standard timers. 3411df8bae1dSRodney W. Grimes */ 3412df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 3413340c35deSJonathan Lemon tcp_twstart(tp); 3414679d9708SAndre Oppermann return; 3415df8bae1dSRodney W. Grimes } 3416df8bae1dSRodney W. Grimes } 34172b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 3418df8bae1dSRodney W. Grimes 3419df8bae1dSRodney W. Grimes /* 3420df8bae1dSRodney W. Grimes * Return any desired output. 3421df8bae1dSRodney W. Grimes */ 34222d05a1c8SRichard Scheffenegger if (needoutput || (tp->t_flags & TF_ACKNOW)) { 342340fa3e40SGleb Smirnoff (void) tcp_output(tp); 34242d05a1c8SRichard Scheffenegger } 3425a14c749fSJonathan Lemon check_delack: 34269eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(inp); 3427252ca428SRobert Watson 3428a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 34293bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 3430b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 34313bfd6421SJonathan Lemon } 34329eb0e832SGleb Smirnoff INP_WUNLOCK(inp); 3433679d9708SAndre Oppermann return; 3434df8bae1dSRodney W. Grimes 3435df8bae1dSRodney W. Grimes dropafterack: 3436df8bae1dSRodney W. Grimes /* 3437df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 3438df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 343980ab7c0eSGarrett Wollman * 344080ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 344180ab7c0eSGarrett Wollman * paths to this code happen after packets containing 344280ab7c0eSGarrett Wollman * RST have been dropped. 344380ab7c0eSGarrett Wollman * 344480ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 344580ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 344680ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 344780ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 344880ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 344980ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 3450df8bae1dSRodney W. Grimes */ 3451fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 3452fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 3453a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 3454a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 3455d1b07f36SRandall Stewart tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT); 3456a57815efSBosko Milekic goto dropwithreset; 3457a57815efSBosko Milekic } 34582b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 3459df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 346040fa3e40SGleb Smirnoff (void) tcp_output(tp); 34619eb0e832SGleb Smirnoff INP_WUNLOCK(inp); 3462d6915262SRobert Watson m_freem(m); 3463679d9708SAndre Oppermann return; 3464df8bae1dSRodney W. Grimes 3465df8bae1dSRodney W. Grimes dropwithreset: 3466a0ca0871SRobert Watson if (tp != NULL) { 3467302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 34689eb0e832SGleb Smirnoff INP_WUNLOCK(inp); 3469dd8ac7f9SRobert Watson } else 3470a0ca0871SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 3471679d9708SAndre Oppermann return; 3472df8bae1dSRodney W. Grimes 3473df8bae1dSRodney W. Grimes drop: 3474df8bae1dSRodney W. Grimes /* 3475df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 3476df8bae1dSRodney W. Grimes */ 34772b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, m); 34784d0770f1SRichard Scheffenegger if (tp != NULL) { 34799eb0e832SGleb Smirnoff INP_WUNLOCK(inp); 34804d0770f1SRichard Scheffenegger } 3481d6915262SRobert Watson m_freem(m); 3482302ce8d6SAndre Oppermann } 3483302ce8d6SAndre Oppermann 3484302ce8d6SAndre Oppermann /* 34850ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 34860ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 34870ca3f933SAndre Oppermann * tp may be NULL. 3488302ce8d6SAndre Oppermann */ 348955bceb1eSRandall Stewart void 3490302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 3491302ce8d6SAndre Oppermann int tlen, int rstreason) 3492302ce8d6SAndre Oppermann { 3493b287c6c7SBjoern A. Zeeb #ifdef INET 3494302ce8d6SAndre Oppermann struct ip *ip; 3495b287c6c7SBjoern A. Zeeb #endif 3496302ce8d6SAndre Oppermann #ifdef INET6 3497302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 3498302ce8d6SAndre Oppermann #endif 34997a3244ccSRobert Watson 35007a3244ccSRobert Watson if (tp != NULL) { 35019eb0e832SGleb Smirnoff INP_LOCK_ASSERT(tptoinpcb(tp)); 35027a3244ccSRobert Watson } 35037a3244ccSRobert Watson 35040ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 35051ebf4607SRichard Scheffenegger if ((tcp_get_flags(th) & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 3506302ce8d6SAndre Oppermann goto drop; 3507302ce8d6SAndre Oppermann #ifdef INET6 3508302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 3509302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 3510302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3511302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3512302ce8d6SAndre Oppermann goto drop; 3513302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 3514b287c6c7SBjoern A. Zeeb } 3515302ce8d6SAndre Oppermann #endif 3516b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3517b287c6c7SBjoern A. Zeeb else 3518b287c6c7SBjoern A. Zeeb #endif 3519b287c6c7SBjoern A. Zeeb #ifdef INET 3520302ce8d6SAndre Oppermann { 3521302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 3522302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3523302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3524302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3525302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3526302ce8d6SAndre Oppermann goto drop; 3527302ce8d6SAndre Oppermann } 3528b287c6c7SBjoern A. Zeeb #endif 3529302ce8d6SAndre Oppermann 3530302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 3531302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 3532302ce8d6SAndre Oppermann goto drop; 3533302ce8d6SAndre Oppermann 3534302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 35351ebf4607SRichard Scheffenegger if (tcp_get_flags(th) & TH_ACK) { 3536302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 3537302ce8d6SAndre Oppermann th->th_ack, TH_RST); 3538302ce8d6SAndre Oppermann } else { 35391ebf4607SRichard Scheffenegger if (tcp_get_flags(th) & TH_SYN) 3540302ce8d6SAndre Oppermann tlen++; 35411ebf4607SRichard Scheffenegger if (tcp_get_flags(th) & TH_FIN) 35424ddd5aadSMichael Tuexen tlen++; 3543302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 3544302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 3545302ce8d6SAndre Oppermann } 3546302ce8d6SAndre Oppermann return; 3547302ce8d6SAndre Oppermann drop: 3548302ce8d6SAndre Oppermann m_freem(m); 3549df8bae1dSRodney W. Grimes } 3550df8bae1dSRodney W. Grimes 3551be2ac88cSJonathan Lemon /* 3552be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 3553be2ac88cSJonathan Lemon */ 355455bceb1eSRandall Stewart void 3555ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 3556df8bae1dSRodney W. Grimes { 3557df8bae1dSRodney W. Grimes int opt, optlen; 3558df8bae1dSRodney W. Grimes 3559be2ac88cSJonathan Lemon to->to_flags = 0; 3560df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 3561df8bae1dSRodney W. Grimes opt = cp[0]; 3562df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 3563df8bae1dSRodney W. Grimes break; 3564df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 3565df8bae1dSRodney W. Grimes optlen = 1; 3566df8bae1dSRodney W. Grimes else { 3567b474779fSJun-ichiro itojun Hagino if (cnt < 2) 3568b474779fSJun-ichiro itojun Hagino break; 3569df8bae1dSRodney W. Grimes optlen = cp[1]; 3570b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 3571df8bae1dSRodney W. Grimes break; 3572df8bae1dSRodney W. Grimes } 3573df8bae1dSRodney W. Grimes switch (opt) { 3574df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 3575df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 3576df8bae1dSRodney W. Grimes continue; 3577f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3578df8bae1dSRodney W. Grimes continue; 3579be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 3580be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 3581be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 3582fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 3583df8bae1dSRodney W. Grimes break; 3584df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 3585df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 3586df8bae1dSRodney W. Grimes continue; 3587f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3588df8bae1dSRodney W. Grimes continue; 3589be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 359002a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 3591df8bae1dSRodney W. Grimes break; 3592df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 3593df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 3594df8bae1dSRodney W. Grimes continue; 3595be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 3596a0292f23SGarrett Wollman bcopy((char *)cp + 2, 3597a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 3598fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 3599a0292f23SGarrett Wollman bcopy((char *)cp + 6, 3600a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 3601fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 3602df8bae1dSRodney W. Grimes break; 36031cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 3604fcf59617SAndrey V. Elsukov /* 3605fcf59617SAndrey V. Elsukov * In order to reply to a host which has set the 3606fcf59617SAndrey V. Elsukov * TCP_SIGNATURE option in its initial SYN, we have 3607fcf59617SAndrey V. Elsukov * to record the fact that the option was observed 3608fcf59617SAndrey V. Elsukov * here for the syncache code to perform the correct 3609fcf59617SAndrey V. Elsukov * response. 3610fcf59617SAndrey V. Elsukov */ 36111cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 36121cfd4b53SBruce M Simpson continue; 3613df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 3614df47e437SAndre Oppermann to->to_signature = cp + 2; 36151cfd4b53SBruce M Simpson break; 36166d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 3617f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 36186d90faf3SPaul Saab continue; 3619f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3620f72167f4SAndre Oppermann continue; 3621603724d3SBjoern A. Zeeb if (!V_tcp_do_sack) 3622f72167f4SAndre Oppermann continue; 3623fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 36246d90faf3SPaul Saab break; 36256d90faf3SPaul Saab case TCPOPT_SACK: 36265a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 36276d90faf3SPaul Saab continue; 3628b728e902SAndre Oppermann if (flags & TO_SYN) 3629b728e902SAndre Oppermann continue; 3630fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 36315a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 36325a53ca16SPaul Saab to->to_sacks = cp + 2; 363378b50714SRobert Watson TCPSTAT_INC(tcps_sack_rcv_blocks); 36346d90faf3SPaul Saab break; 3635281a0fd4SPatrick Kelsey case TCPOPT_FAST_OPEN: 3636c560df6fSPatrick Kelsey /* 3637c560df6fSPatrick Kelsey * Cookie length validation is performed by the 3638c560df6fSPatrick Kelsey * server side cookie checking code or the client 3639c560df6fSPatrick Kelsey * side cookie cache update code. 3640c560df6fSPatrick Kelsey */ 3641281a0fd4SPatrick Kelsey if (!(flags & TO_SYN)) 3642281a0fd4SPatrick Kelsey continue; 3643c560df6fSPatrick Kelsey if (!V_tcp_fastopen_client_enable && 3644c560df6fSPatrick Kelsey !V_tcp_fastopen_server_enable) 3645281a0fd4SPatrick Kelsey continue; 3646281a0fd4SPatrick Kelsey to->to_flags |= TOF_FASTOPEN; 3647281a0fd4SPatrick Kelsey to->to_tfo_len = optlen - 2; 3648281a0fd4SPatrick Kelsey to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL; 3649281a0fd4SPatrick Kelsey break; 3650be2ac88cSJonathan Lemon default: 3651be2ac88cSJonathan Lemon continue; 3652df8bae1dSRodney W. Grimes } 3653df8bae1dSRodney W. Grimes } 3654df8bae1dSRodney W. Grimes } 3655df8bae1dSRodney W. Grimes 3656df8bae1dSRodney W. Grimes /* 3657df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 3658df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 3659df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 3660df8bae1dSRodney W. Grimes * sequencing purposes. 3661df8bae1dSRodney W. Grimes */ 366255bceb1eSRandall Stewart void 3663ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 3664ad3f9ab3SAndre Oppermann int off) 3665df8bae1dSRodney W. Grimes { 3666fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 3667df8bae1dSRodney W. Grimes 3668df8bae1dSRodney W. Grimes while (cnt >= 0) { 3669df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 3670df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 3671df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 3672df8bae1dSRodney W. Grimes 36739eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(tptoinpcb(tp)); 36747a3244ccSRobert Watson 3675df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 3676df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 3677df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 3678df8bae1dSRodney W. Grimes m->m_len--; 367969a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 368069a34685SYoshinobu Inoue m->m_pkthdr.len--; 3681df8bae1dSRodney W. Grimes return; 3682df8bae1dSRodney W. Grimes } 3683df8bae1dSRodney W. Grimes cnt -= m->m_len; 3684df8bae1dSRodney W. Grimes m = m->m_next; 36854dfdffe9SAndre Oppermann if (m == NULL) 3686df8bae1dSRodney W. Grimes break; 3687df8bae1dSRodney W. Grimes } 3688df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 3689df8bae1dSRodney W. Grimes } 3690df8bae1dSRodney W. Grimes 3691df8bae1dSRodney W. Grimes /* 3692df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 3693df8bae1dSRodney W. Grimes * and update averages and current timeout. 3694df8bae1dSRodney W. Grimes */ 369555bceb1eSRandall Stewart void 3696ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 3697df8bae1dSRodney W. Grimes { 3698ad3f9ab3SAndre Oppermann int delta; 3699233e8c18SGarrett Wollman 37009eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(tptoinpcb(tp)); 37012be3bf22SRobert Watson 370278b50714SRobert Watson TCPSTAT_INC(tcps_rttupdated); 370318b83b62SRichard Scheffenegger if (tp->t_rttupdated < UCHAR_MAX) 3704233e8c18SGarrett Wollman tp->t_rttupdated++; 3705adc56f5aSEdward Tomasz Napierala #ifdef STATS 3706adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT, 3707adc56f5aSEdward Tomasz Napierala imax(0, rtt * 1000 / hz)); 3708adc56f5aSEdward Tomasz Napierala #endif 37095ede40dcSRyan Stone if ((tp->t_srtt != 0) && (tp->t_rxtshift <= TCP_RTT_INVALIDATE)) { 3710233e8c18SGarrett Wollman /* 3711233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 3712233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 3713233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 3714233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3715233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 3716233e8c18SGarrett Wollman */ 3717233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3718233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3719233e8c18SGarrett Wollman 3720233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 3721233e8c18SGarrett Wollman tp->t_srtt = 1; 3722233e8c18SGarrett Wollman 3723233e8c18SGarrett Wollman /* 3724233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 3725233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 3726233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 3727233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 3728233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 3729233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 3730233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3731233e8c18SGarrett Wollman * rfc793's wired-in beta. 3732233e8c18SGarrett Wollman */ 3733233e8c18SGarrett Wollman if (delta < 0) 3734233e8c18SGarrett Wollman delta = -delta; 3735233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3736233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 3737233e8c18SGarrett Wollman tp->t_rttvar = 1; 3738233e8c18SGarrett Wollman } else { 3739233e8c18SGarrett Wollman /* 3740233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 3741233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 3742233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 3743233e8c18SGarrett Wollman */ 3744233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 3745233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 3746233e8c18SGarrett Wollman } 37479b8b58e0SJonathan Lemon tp->t_rtttime = 0; 3748df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 3749df8bae1dSRodney W. Grimes 3750df8bae1dSRodney W. Grimes /* 3751df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 3752df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 3753df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 3754df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 3755df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 3756df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 3757df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 3758df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 3759df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 3760df8bae1dSRodney W. Grimes */ 3761233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 37629e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3763df8bae1dSRodney W. Grimes 3764df8bae1dSRodney W. Grimes /* 3765df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 3766df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 3767df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 3768df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 3769df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 3770df8bae1dSRodney W. Grimes */ 3771df8bae1dSRodney W. Grimes tp->t_softerror = 0; 3772df8bae1dSRodney W. Grimes } 3773df8bae1dSRodney W. Grimes 3774df8bae1dSRodney W. Grimes /* 3775df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 3776df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 37774faaea55SAndre Oppermann * If none, use an mss that can be handled on the outgoing interface 37784faaea55SAndre Oppermann * without forcing IP to fragment. If no route is found, route has no mtu, 3779df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 3780df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 3781df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 3782df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 3783df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 3784df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 3785df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 3786a0292f23SGarrett Wollman * 37870c39d38dSGleb Smirnoff * NOTE that resulting t_maxseg doesn't include space for TCP options or 37880c39d38dSGleb Smirnoff * IP options, e.g. IPSEC data, since length of this data may vary, and 37890c39d38dSGleb Smirnoff * thus it is calculated for every segment separately in tcp_output(). 3790a0292f23SGarrett Wollman * 379197d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 3792ef341ee1SGleb Smirnoff * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS 3793ef341ee1SGleb Smirnoff * settings are handled in tcp_mssopt(). 3794df8bae1dSRodney W. Grimes */ 3795a0292f23SGarrett Wollman void 3796ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer, 37973c914c54SAndre Oppermann struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap) 3798df8bae1dSRodney W. Grimes { 3799b287c6c7SBjoern A. Zeeb int mss = 0; 38003ac12506SJonathan T. Looney uint32_t maxmtu = 0; 38019eb0e832SGleb Smirnoff struct inpcb *inp = tptoinpcb(tp); 380297d8d152SAndre Oppermann struct hc_metrics_lite metrics; 3803fb59c426SYoshinobu Inoue #ifdef INET6 3804c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3805c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 3806c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3807c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 3808c068736aSJeffrey Hsu #else 38099e644c23SMichael Tuexen size_t min_protoh = sizeof(struct tcpiphdr); 3810fb59c426SYoshinobu Inoue #endif 3811df8bae1dSRodney W. Grimes 38129eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(inp); 38137a3244ccSRobert Watson 38149e644c23SMichael Tuexen if (tp->t_port) 38159e644c23SMichael Tuexen min_protoh += V_tcp_udp_tunneling_overhead; 3816ef341ee1SGleb Smirnoff if (mtuoffer != -1) { 3817ef341ee1SGleb Smirnoff KASSERT(offer == -1, ("%s: conflict", __func__)); 3818ef341ee1SGleb Smirnoff offer = mtuoffer - min_protoh; 3819ef341ee1SGleb Smirnoff } 3820ef341ee1SGleb Smirnoff 3821e8949f74SAndre Oppermann /* Initialize. */ 382297d8d152SAndre Oppermann #ifdef INET6 382397d8d152SAndre Oppermann if (isipv6) { 38243c914c54SAndre Oppermann maxmtu = tcp_maxmtu6(&inp->inp_inc, cap); 38250c39d38dSGleb Smirnoff tp->t_maxseg = V_tcp_v6mssdflt; 3826b287c6c7SBjoern A. Zeeb } 382797d8d152SAndre Oppermann #endif 3828b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3829b287c6c7SBjoern A. Zeeb else 3830b287c6c7SBjoern A. Zeeb #endif 3831b287c6c7SBjoern A. Zeeb #ifdef INET 383297d8d152SAndre Oppermann { 38333c914c54SAndre Oppermann maxmtu = tcp_maxmtu(&inp->inp_inc, cap); 38340c39d38dSGleb Smirnoff tp->t_maxseg = V_tcp_mssdflt; 3835df8bae1dSRodney W. Grimes } 3836b287c6c7SBjoern A. Zeeb #endif 3837df8bae1dSRodney W. Grimes 383897d8d152SAndre Oppermann /* 3839e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 384097d8d152SAndre Oppermann */ 38416f01cac6SBjoern A. Zeeb if (maxmtu == 0) { 38426f01cac6SBjoern A. Zeeb /* 38438e5c87f4SBjoern A. Zeeb * In case we return early we need to initialize metrics 38446f01cac6SBjoern A. Zeeb * to a defined state as tcp_hc_get() would do for us 38456f01cac6SBjoern A. Zeeb * if there was no cache hit. 38466f01cac6SBjoern A. Zeeb */ 38476f01cac6SBjoern A. Zeeb if (metricptr != NULL) 38486f01cac6SBjoern A. Zeeb bzero(metricptr, sizeof(struct hc_metrics_lite)); 384997d8d152SAndre Oppermann return; 38506f01cac6SBjoern A. Zeeb } 385197d8d152SAndre Oppermann 3852e8949f74SAndre Oppermann /* What have we got? */ 385397d8d152SAndre Oppermann switch (offer) { 385497d8d152SAndre Oppermann case 0: 385597d8d152SAndre Oppermann /* 385697d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 3857c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 38580c39d38dSGleb Smirnoff * already assigned to t_maxseg above. 385997d8d152SAndre Oppermann */ 38600c39d38dSGleb Smirnoff offer = tp->t_maxseg; 386197d8d152SAndre Oppermann break; 386297d8d152SAndre Oppermann 386397d8d152SAndre Oppermann case -1: 3864a0292f23SGarrett Wollman /* 3865c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 3866a0292f23SGarrett Wollman */ 386797d8d152SAndre Oppermann /* FALLTHROUGH */ 386897d8d152SAndre Oppermann 386997d8d152SAndre Oppermann default: 3870a0292f23SGarrett Wollman /* 387153369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 387253369ac9SAndre Oppermann * to at least minmss. 387353369ac9SAndre Oppermann */ 3874603724d3SBjoern A. Zeeb offer = max(offer, V_tcp_minmss); 387597d8d152SAndre Oppermann } 3876a0292f23SGarrett Wollman 387737898108SGleb Smirnoff if (metricptr == NULL) 387837898108SGleb Smirnoff metricptr = &metrics; 387937898108SGleb Smirnoff tcp_hc_get(&inp->inp_inc, metricptr); 388097d8d152SAndre Oppermann 3881df8bae1dSRodney W. Grimes /* 3882cc412412SHiren Panchasara * If there's a discovered mtu in tcp hostcache, use it. 3883cc412412SHiren Panchasara * Else, use the link mtu. 3884df8bae1dSRodney W. Grimes */ 388537898108SGleb Smirnoff if (metricptr->hc_mtu) 388637898108SGleb Smirnoff mss = min(metricptr->hc_mtu, maxmtu) - min_protoh; 3887c068736aSJeffrey Hsu else { 388831b3783cSHajimu UMEMOTO #ifdef INET6 3889fb59c426SYoshinobu Inoue if (isipv6) { 389097d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3891603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 389297d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 3893603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_v6mssdflt); 3894b287c6c7SBjoern A. Zeeb } 3895b3399803SHajimu UMEMOTO #endif 3896b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3897b287c6c7SBjoern A. Zeeb else 3898b287c6c7SBjoern A. Zeeb #endif 3899b287c6c7SBjoern A. Zeeb #ifdef INET 390097d8d152SAndre Oppermann { 390197d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3902603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 390397d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 3904603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_mssdflt); 3905a0292f23SGarrett Wollman } 3906b287c6c7SBjoern A. Zeeb #endif 39073cee92e0SBjoern A. Zeeb /* 39083cee92e0SBjoern A. Zeeb * XXX - The above conditional (mss = maxmtu - min_protoh) 39093cee92e0SBjoern A. Zeeb * probably violates the TCP spec. 39103cee92e0SBjoern A. Zeeb * The problem is that, since we don't know the 39113cee92e0SBjoern A. Zeeb * other end's MSS, we are supposed to use a conservative 39123cee92e0SBjoern A. Zeeb * default. But, if we do that, then MTU discovery will 39133cee92e0SBjoern A. Zeeb * never actually take place, because the conservative 39143cee92e0SBjoern A. Zeeb * default is much less than the MTUs typically seen 39153cee92e0SBjoern A. Zeeb * on the Internet today. For the moment, we'll sweep 39163cee92e0SBjoern A. Zeeb * this under the carpet. 39173cee92e0SBjoern A. Zeeb * 39183cee92e0SBjoern A. Zeeb * The conservative default might not actually be a problem 39193cee92e0SBjoern A. Zeeb * if the only case this occurs is when sending an initial 39203cee92e0SBjoern A. Zeeb * SYN with options and data to a host we've never talked 39213cee92e0SBjoern A. Zeeb * to before. Then, they will reply with an MSS value which 39223cee92e0SBjoern A. Zeeb * will get recorded and the new parameters should get 39233cee92e0SBjoern A. Zeeb * recomputed. For Further Study. 39243cee92e0SBjoern A. Zeeb */ 392597d8d152SAndre Oppermann } 3926a0292f23SGarrett Wollman mss = min(mss, offer); 392797d8d152SAndre Oppermann 3928a0292f23SGarrett Wollman /* 39290c39d38dSGleb Smirnoff * Sanity check: make sure that maxseg will be large 39303cee92e0SBjoern A. Zeeb * enough to allow some data on segments even if the 39313cee92e0SBjoern A. Zeeb * all the option space is used (40bytes). Otherwise 39323cee92e0SBjoern A. Zeeb * funny things may happen in tcp_output. 39330c39d38dSGleb Smirnoff * 39340c39d38dSGleb Smirnoff * XXXGL: shouldn't we reserve space for IP/IPv6 options? 39353cee92e0SBjoern A. Zeeb */ 39363cee92e0SBjoern A. Zeeb mss = max(mss, 64); 39373cee92e0SBjoern A. Zeeb 393897d8d152SAndre Oppermann tp->t_maxseg = mss; 3939fce03f85SRandall Stewart if (tp->t_maxseg < V_tcp_mssdflt) { 3940fce03f85SRandall Stewart /* 3941fce03f85SRandall Stewart * The MSS is so small we should not process incoming 3942fce03f85SRandall Stewart * SACK's since we are subject to attack in such a 3943fce03f85SRandall Stewart * case. 3944fce03f85SRandall Stewart */ 3945fce03f85SRandall Stewart tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT; 3946fce03f85SRandall Stewart } else { 3947fce03f85SRandall Stewart tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT; 3948fce03f85SRandall Stewart } 3949fce03f85SRandall Stewart 39503cee92e0SBjoern A. Zeeb } 39513cee92e0SBjoern A. Zeeb 39523cee92e0SBjoern A. Zeeb void 39533cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer) 39543cee92e0SBjoern A. Zeeb { 3955dbc42409SLawrence Stewart int mss; 39563ac12506SJonathan T. Looney uint32_t bufsize; 39579eb0e832SGleb Smirnoff struct inpcb *inp = tptoinpcb(tp); 39583cee92e0SBjoern A. Zeeb struct socket *so; 39593cee92e0SBjoern A. Zeeb struct hc_metrics_lite metrics; 39603c914c54SAndre Oppermann struct tcp_ifcap cap; 3961dbc42409SLawrence Stewart 39623cee92e0SBjoern A. Zeeb KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 39633cee92e0SBjoern A. Zeeb 39643c914c54SAndre Oppermann bzero(&cap, sizeof(cap)); 39653c914c54SAndre Oppermann tcp_mss_update(tp, offer, -1, &metrics, &cap); 39663cee92e0SBjoern A. Zeeb 39673cee92e0SBjoern A. Zeeb mss = tp->t_maxseg; 396897d8d152SAndre Oppermann 3969df8bae1dSRodney W. Grimes /* 397097d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 397197d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 397297d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 397397d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 397497d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3975df8bae1dSRodney W. Grimes */ 3976c3b02504SBjoern A. Zeeb so = inp->inp_socket; 3977dded4e9eSRichard Scheffenegger SOCK_SENDBUF_LOCK(so); 397809000cc1SGleb Smirnoff if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.hc_sendpipe) 397909000cc1SGleb Smirnoff bufsize = metrics.hc_sendpipe; 398097d8d152SAndre Oppermann else 3981df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3982df8bae1dSRodney W. Grimes if (bufsize < mss) 3983df8bae1dSRodney W. Grimes mss = bufsize; 3984df8bae1dSRodney W. Grimes else { 3985df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3986df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3987df8bae1dSRodney W. Grimes bufsize = sb_max; 398888c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 398943283184SGleb Smirnoff (void)sbreserve_locked(so, SO_SND, bufsize, NULL); 3990df8bae1dSRodney W. Grimes } 3991dded4e9eSRichard Scheffenegger SOCK_SENDBUF_UNLOCK(so); 3992784ce8faSHiren Panchasara /* 3993784ce8faSHiren Panchasara * Sanity check: make sure that maxseg will be large 3994784ce8faSHiren Panchasara * enough to allow some data on segments even if the 3995784ce8faSHiren Panchasara * all the option space is used (40bytes). Otherwise 3996784ce8faSHiren Panchasara * funny things may happen in tcp_output. 3997784ce8faSHiren Panchasara * 3998784ce8faSHiren Panchasara * XXXGL: shouldn't we reserve space for IP/IPv6 options? 3999784ce8faSHiren Panchasara */ 4000784ce8faSHiren Panchasara tp->t_maxseg = max(mss, 64); 4001fce03f85SRandall Stewart if (tp->t_maxseg < V_tcp_mssdflt) { 4002fce03f85SRandall Stewart /* 4003fce03f85SRandall Stewart * The MSS is so small we should not process incoming 4004fce03f85SRandall Stewart * SACK's since we are subject to attack in such a 4005fce03f85SRandall Stewart * case. 4006fce03f85SRandall Stewart */ 4007fce03f85SRandall Stewart tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT; 4008fce03f85SRandall Stewart } else { 4009fce03f85SRandall Stewart tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT; 4010fce03f85SRandall Stewart } 4011df8bae1dSRodney W. Grimes 4012dded4e9eSRichard Scheffenegger SOCK_RECVBUF_LOCK(so); 401309000cc1SGleb Smirnoff if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.hc_recvpipe) 401409000cc1SGleb Smirnoff bufsize = metrics.hc_recvpipe; 401597d8d152SAndre Oppermann else 4016df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 4017df8bae1dSRodney W. Grimes if (bufsize > mss) { 4018df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 4019df8bae1dSRodney W. Grimes if (bufsize > sb_max) 4020df8bae1dSRodney W. Grimes bufsize = sb_max; 402188c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 402243283184SGleb Smirnoff (void)sbreserve_locked(so, SO_RCV, bufsize, NULL); 4023df8bae1dSRodney W. Grimes } 4024dded4e9eSRichard Scheffenegger SOCK_RECVBUF_UNLOCK(so); 402591d6cfa6SBjoern A. Zeeb 402691d6cfa6SBjoern A. Zeeb /* Check the interface for TSO capabilities. */ 40273c914c54SAndre Oppermann if (cap.ifcap & CSUM_TSO) { 402891d6cfa6SBjoern A. Zeeb tp->t_flags |= TF_TSO; 40293c914c54SAndre Oppermann tp->t_tsomax = cap.tsomax; 40309fd573c3SHans Petter Selasky tp->t_tsomaxsegcount = cap.tsomaxsegcount; 40319fd573c3SHans Petter Selasky tp->t_tsomaxsegsize = cap.tsomaxsegsize; 4032b6919741SKonstantin Belousov if (cap.ipsec_tso) 4033b6919741SKonstantin Belousov tp->t_flags2 |= TF2_IPSEC_TSO; 40343c914c54SAndre Oppermann } 4035a0292f23SGarrett Wollman } 4036a0292f23SGarrett Wollman 4037a0292f23SGarrett Wollman /* 4038a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 4039a0292f23SGarrett Wollman */ 4040a0292f23SGarrett Wollman int 4041ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 4042a0292f23SGarrett Wollman { 404397d8d152SAndre Oppermann int mss = 0; 40443ac12506SJonathan T. Looney uint32_t thcmtu = 0; 40453ac12506SJonathan T. Looney uint32_t maxmtu = 0; 404697d8d152SAndre Oppermann size_t min_protoh; 4047a0292f23SGarrett Wollman 404897d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 4049a0292f23SGarrett Wollman 405031b3783cSHajimu UMEMOTO #ifdef INET6 4051dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 4052603724d3SBjoern A. Zeeb mss = V_tcp_v6mssdflt; 4053233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 405497d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 4055b287c6c7SBjoern A. Zeeb } 405631b3783cSHajimu UMEMOTO #endif 4057b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 4058b287c6c7SBjoern A. Zeeb else 4059b287c6c7SBjoern A. Zeeb #endif 4060b287c6c7SBjoern A. Zeeb #ifdef INET 406197d8d152SAndre Oppermann { 4062603724d3SBjoern A. Zeeb mss = V_tcp_mssdflt; 4063233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 406497d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 406597d8d152SAndre Oppermann } 4066b287c6c7SBjoern A. Zeeb #endif 40673a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET) 40683a9391deSBjoern A. Zeeb thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 40693a9391deSBjoern A. Zeeb #endif 40703a9391deSBjoern A. Zeeb 407197d8d152SAndre Oppermann if (maxmtu && thcmtu) 407297d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 407397d8d152SAndre Oppermann else if (maxmtu || thcmtu) 407497d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 407597d8d152SAndre Oppermann 407697d8d152SAndre Oppermann return (mss); 4077df8bae1dSRodney W. Grimes } 407846f58482SJonathan Lemon 40790e1d7c25SRichard Scheffenegger void 4080c7c325d0SRichard Scheffenegger tcp_do_prr_ack(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to, 4081c7c325d0SRichard Scheffenegger sackstatus_t sack_changed, u_int *maxsegp) 40820e1d7c25SRichard Scheffenegger { 408348396dc7SRichard Scheffenegger int snd_cnt = 0, limit = 0, del_data = 0, pipe = 0; 4084c7c325d0SRichard Scheffenegger u_int maxseg; 40850e1d7c25SRichard Scheffenegger 40869eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(tptoinpcb(tp)); 40870e1d7c25SRichard Scheffenegger 4088c7c325d0SRichard Scheffenegger if (*maxsegp == 0) { 4089c7c325d0SRichard Scheffenegger *maxsegp = tcp_maxseg(tp); 4090c7c325d0SRichard Scheffenegger } 4091c7c325d0SRichard Scheffenegger maxseg = *maxsegp; 40920e1d7c25SRichard Scheffenegger /* 40930e1d7c25SRichard Scheffenegger * Compute the amount of data that this ACK is indicating 40940e1d7c25SRichard Scheffenegger * (del_data) and an estimate of how many bytes are in the 40950e1d7c25SRichard Scheffenegger * network. 40960e1d7c25SRichard Scheffenegger */ 4097c21b7b55SRichard Scheffenegger if (tcp_is_sack_recovery(tp, to) || 409874d7fc87SRichard Scheffenegger (IN_CONGRECOVERY(tp->t_flags) && 409974d7fc87SRichard Scheffenegger !IN_FASTRECOVERY(tp->t_flags))) { 410031d7a27cSRichard Scheffenegger del_data = tp->sackhint.delivered_data; 4101d1de2b05SRichard Scheffenegger if (V_tcp_do_newsack) 4102a8e431e1SRichard Scheffenegger pipe = tcp_compute_pipe(tp); 4103a8e431e1SRichard Scheffenegger else 410474d7fc87SRichard Scheffenegger pipe = (tp->snd_nxt - tp->snd_fack) + 410574d7fc87SRichard Scheffenegger tp->sackhint.sack_bytes_rexmit; 410674d7fc87SRichard Scheffenegger } else { 410774d7fc87SRichard Scheffenegger if (tp->sackhint.prr_delivered < (tcprexmtthresh * maxseg + 41082d05a1c8SRichard Scheffenegger tp->snd_recover - tp->snd_una)) { 410974d7fc87SRichard Scheffenegger del_data = maxseg; 41102d05a1c8SRichard Scheffenegger } 411174d7fc87SRichard Scheffenegger pipe = imax(0, tp->snd_max - tp->snd_una - 411274d7fc87SRichard Scheffenegger imin(INT_MAX / 65536, tp->t_dupacks) * maxseg); 411374d7fc87SRichard Scheffenegger } 41140e1d7c25SRichard Scheffenegger tp->sackhint.prr_delivered += del_data; 41150e1d7c25SRichard Scheffenegger /* 41160e1d7c25SRichard Scheffenegger * Proportional Rate Reduction 41170e1d7c25SRichard Scheffenegger */ 4118e9071000SRichard Scheffenegger if (pipe >= tp->snd_ssthresh) { 41196a376af0SRichard Scheffenegger if (tp->sackhint.recover_fs == 0) 41206a376af0SRichard Scheffenegger tp->sackhint.recover_fs = 412148396dc7SRichard Scheffenegger imax(1, tp->snd_nxt - tp->snd_una); 412248396dc7SRichard Scheffenegger snd_cnt = howmany((long)tp->sackhint.prr_delivered * 412348396dc7SRichard Scheffenegger tp->snd_ssthresh, tp->sackhint.recover_fs) - 41249276ad23SRichard Scheffenegger tp->sackhint.prr_out + maxseg - 1; 41256a376af0SRichard Scheffenegger } else { 412649a6fbe3SRichard Scheffenegger /* 412749a6fbe3SRichard Scheffenegger * PRR 6937bis heuristic: 412849a6fbe3SRichard Scheffenegger * - A partial ack without SACK block beneath snd_recover 412949a6fbe3SRichard Scheffenegger * indicates further loss. 413049a6fbe3SRichard Scheffenegger * - An SACK scoreboard update adding a new hole indicates 413149a6fbe3SRichard Scheffenegger * further loss, so be conservative and send at most one 413249a6fbe3SRichard Scheffenegger * segment. 413349a6fbe3SRichard Scheffenegger * - Prevent ACK splitting attacks, by being conservative 413449a6fbe3SRichard Scheffenegger * when no new data is acked. 413549a6fbe3SRichard Scheffenegger */ 41362d05a1c8SRichard Scheffenegger if ((sack_changed == SACK_NEWLOSS) || (del_data == 0)) { 413784761f3dSRichard Scheffenegger limit = tp->sackhint.prr_delivered - 4138e5313869SRichard Scheffenegger tp->sackhint.prr_out; 41392d05a1c8SRichard Scheffenegger } else { 414048396dc7SRichard Scheffenegger limit = imax(tp->sackhint.prr_delivered - 4141e5313869SRichard Scheffenegger tp->sackhint.prr_out, del_data) + 4142e5313869SRichard Scheffenegger maxseg; 41432d05a1c8SRichard Scheffenegger } 414448396dc7SRichard Scheffenegger snd_cnt = imin((tp->snd_ssthresh - pipe), limit); 41450e1d7c25SRichard Scheffenegger } 414648396dc7SRichard Scheffenegger snd_cnt = imax(snd_cnt, 0) / maxseg; 41470e1d7c25SRichard Scheffenegger /* 41480e1d7c25SRichard Scheffenegger * Send snd_cnt new data into the network in response to this ack. 41490e1d7c25SRichard Scheffenegger * If there is going to be a SACK retransmission, adjust snd_cwnd 41500e1d7c25SRichard Scheffenegger * accordingly. 41510e1d7c25SRichard Scheffenegger */ 4152b9f803b7SRichard Scheffenegger if (IN_FASTRECOVERY(tp->t_flags)) { 4153c21b7b55SRichard Scheffenegger if (tcp_is_sack_recovery(tp, to)) { 41547dc78150SRichard Scheffenegger tp->snd_cwnd = pipe - del_data + (snd_cnt * maxseg); 415574d7fc87SRichard Scheffenegger } else { 415674d7fc87SRichard Scheffenegger tp->snd_cwnd = (tp->snd_max - tp->snd_una) + 415774d7fc87SRichard Scheffenegger (snd_cnt * maxseg); 415874d7fc87SRichard Scheffenegger } 41592d05a1c8SRichard Scheffenegger } else if (IN_CONGRECOVERY(tp->t_flags)) { 416074d7fc87SRichard Scheffenegger tp->snd_cwnd = pipe - del_data + (snd_cnt * maxseg); 41612d05a1c8SRichard Scheffenegger } 416274d7fc87SRichard Scheffenegger tp->snd_cwnd = imax(maxseg, tp->snd_cwnd); 41630e1d7c25SRichard Scheffenegger } 41640e1d7c25SRichard Scheffenegger 416546f58482SJonathan Lemon /* 4166c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 4167c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 4168c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 4169c068736aSJeffrey Hsu * be started again. 417046f58482SJonathan Lemon */ 417155bceb1eSRandall Stewart void 4172ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 417346f58482SJonathan Lemon { 417446f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 41753ac12506SJonathan T. Looney uint32_t ocwnd = tp->snd_cwnd; 41760c39d38dSGleb Smirnoff u_int maxseg = tcp_maxseg(tp); 417746f58482SJonathan Lemon 41789eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(tptoinpcb(tp)); 41797a3244ccSRobert Watson 4180b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 418146f58482SJonathan Lemon tp->t_rtttime = 0; 41827dc78150SRichard Scheffenegger if (IN_FASTRECOVERY(tp->t_flags)) { 418346f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 41846b2a5f92SJayanth Vijayaraghavan /* 4185c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 4186c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 41876b2a5f92SJayanth Vijayaraghavan */ 41880c39d38dSGleb Smirnoff tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th); 4189a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 419040fa3e40SGleb Smirnoff (void) tcp_output(tp); 419146f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 419246f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 419346f58482SJonathan Lemon tp->snd_nxt = onxt; 41947dc78150SRichard Scheffenegger } 419546f58482SJonathan Lemon /* 419646f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 419746f58482SJonathan Lemon * not updated yet. 419846f58482SJonathan Lemon */ 4199dbc42409SLawrence Stewart if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th)) 4200dbc42409SLawrence Stewart tp->snd_cwnd -= BYTES_THIS_ACK(tp, th); 4201d7587117SPaul Saab else 4202d7587117SPaul Saab tp->snd_cwnd = 0; 42030c39d38dSGleb Smirnoff tp->snd_cwnd += maxseg; 420446f58482SJonathan Lemon } 420512eeb81fSHiren Panchasara 420612eeb81fSHiren Panchasara int 420712eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp) 420812eeb81fSHiren Panchasara { 4209e5049a17SRandall Stewart if (tp->t_fb->tfb_compute_pipe == NULL) { 421012eeb81fSHiren Panchasara return (tp->snd_max - tp->snd_una + 421112eeb81fSHiren Panchasara tp->sackhint.sack_bytes_rexmit - 4212e2c6a6d2SRichard Scheffenegger tp->sackhint.sacked_bytes - 4213e2c6a6d2SRichard Scheffenegger tp->sackhint.lost_bytes); 4214e5049a17SRandall Stewart } else { 4215e5049a17SRandall Stewart return((*tp->t_fb->tfb_compute_pipe)(tp)); 4216e5049a17SRandall Stewart } 421712eeb81fSHiren Panchasara } 42187dc90a1dSMichael Tuexen 42197dc90a1dSMichael Tuexen uint32_t 42207dc90a1dSMichael Tuexen tcp_compute_initwnd(uint32_t maxseg) 42217dc90a1dSMichael Tuexen { 42227dc90a1dSMichael Tuexen /* 42237dc90a1dSMichael Tuexen * Calculate the Initial Window, also used as Restart Window 42247dc90a1dSMichael Tuexen * 42257dc90a1dSMichael Tuexen * RFC5681 Section 3.1 specifies the default conservative values. 42267dc90a1dSMichael Tuexen * RFC3390 specifies slightly more aggressive values. 42277dc90a1dSMichael Tuexen * RFC6928 increases it to ten segments. 42287dc90a1dSMichael Tuexen * Support for user specified value for initial flight size. 42297dc90a1dSMichael Tuexen */ 42307dc90a1dSMichael Tuexen if (V_tcp_initcwnd_segments) 42317dc90a1dSMichael Tuexen return min(V_tcp_initcwnd_segments * maxseg, 42327dc90a1dSMichael Tuexen max(2 * maxseg, V_tcp_initcwnd_segments * 1460)); 42337dc90a1dSMichael Tuexen else if (V_tcp_do_rfc3390) 42347dc90a1dSMichael Tuexen return min(4 * maxseg, max(2 * maxseg, 4380)); 42357dc90a1dSMichael Tuexen else { 42367dc90a1dSMichael Tuexen /* Per RFC5681 Section 3.1 */ 42377dc90a1dSMichael Tuexen if (maxseg > 2190) 42387dc90a1dSMichael Tuexen return (2 * maxseg); 42397dc90a1dSMichael Tuexen else if (maxseg > 1095) 42407dc90a1dSMichael Tuexen return (3 * maxseg); 42417dc90a1dSMichael Tuexen else 42427dc90a1dSMichael Tuexen return (4 * maxseg); 42437dc90a1dSMichael Tuexen } 42447dc90a1dSMichael Tuexen } 4245