1c398230bSWarner Losh /*- 2e79adb8eSGarrett Wollman * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4dbc42409SLawrence Stewart * Copyright (c) 2007-2008,2010 5dbc42409SLawrence Stewart * Swinburne University of Technology, Melbourne, Australia. 6dbc42409SLawrence Stewart * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org> 7dbc42409SLawrence Stewart * Copyright (c) 2010 The FreeBSD Foundation 8fa046d87SRobert Watson * Copyright (c) 2010-2011 Juniper Networks, Inc. 9dbc42409SLawrence Stewart * All rights reserved. 10dbc42409SLawrence Stewart * 11dbc42409SLawrence Stewart * Portions of this software were developed at the Centre for Advanced Internet 12891b8ed4SLawrence Stewart * Architectures, Swinburne University of Technology, by Lawrence Stewart, 13891b8ed4SLawrence Stewart * James Healy and David Hayes, made possible in part by a grant from the Cisco 14891b8ed4SLawrence Stewart * University Research Program Fund at Community Foundation Silicon Valley. 15dbc42409SLawrence Stewart * 16dbc42409SLawrence Stewart * Portions of this software were developed at the Centre for Advanced 17dbc42409SLawrence Stewart * Internet Architectures, Swinburne University of Technology, Melbourne, 18dbc42409SLawrence Stewart * Australia by David Hayes under sponsorship from the FreeBSD Foundation. 19df8bae1dSRodney W. Grimes * 20fa046d87SRobert Watson * Portions of this software were developed by Robert N. M. Watson under 21fa046d87SRobert Watson * contract to Juniper Networks, Inc. 22fa046d87SRobert Watson * 23df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 24df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 25df8bae1dSRodney W. Grimes * are met: 26df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 27df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 28df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 29df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 30df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 31df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 32df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 33df8bae1dSRodney W. Grimes * without specific prior written permission. 34df8bae1dSRodney W. Grimes * 35df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 36df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 38df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 39df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 40df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 41df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 42df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 43df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 44df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 45df8bae1dSRodney W. Grimes * SUCH DAMAGE. 46df8bae1dSRodney W. Grimes * 47e79adb8eSGarrett Wollman * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 48df8bae1dSRodney W. Grimes */ 49df8bae1dSRodney W. Grimes 504b421e2dSMike Silbersack #include <sys/cdefs.h> 514b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 524b421e2dSMike Silbersack 53f9e354dfSJulian Elischer #include "opt_ipfw.h" /* for ipfw_fwd */ 541cfd4b53SBruce M Simpson #include "opt_inet.h" 55fb59c426SYoshinobu Inoue #include "opt_inet6.h" 563a2a9f79SYoshinobu Inoue #include "opt_ipsec.h" 570cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 580cc12cc5SJoerg Wunsch 59df8bae1dSRodney W. Grimes #include <sys/param.h> 6098163b98SPoul-Henning Kamp #include <sys/kernel.h> 6139bc9de5SLawrence Stewart #include <sys/hhook.h> 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> 6657f60867SMark Johnston #include <sys/sdt.h> 67960ed29cSSeigo Tanimura #include <sys/signalvar.h> 68df8bae1dSRodney W. Grimes #include <sys/socket.h> 69df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 70960ed29cSSeigo Tanimura #include <sys/sysctl.h> 71816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 72960ed29cSSeigo Tanimura #include <sys/systm.h> 73df8bae1dSRodney W. Grimes 74e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 75e79adb8eSGarrett Wollman 7612e2e970SAndre Oppermann #include <vm/uma.h> 7712e2e970SAndre Oppermann 78df8bae1dSRodney W. Grimes #include <net/if.h> 7976039bc8SGleb Smirnoff #include <net/if_var.h> 80df8bae1dSRodney W. Grimes #include <net/route.h> 81530c0060SRobert Watson #include <net/vnet.h> 82df8bae1dSRodney W. Grimes 83773673c1SAndre Oppermann #define TCPSTATES /* for logging */ 84773673c1SAndre Oppermann 85dbc42409SLawrence Stewart #include <netinet/cc.h> 86df8bae1dSRodney W. Grimes #include <netinet/in.h> 8757f60867SMark Johnston #include <netinet/in_kdtrace.h> 88960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 89df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 90960ed29cSSeigo Tanimura #include <netinet/in_var.h> 91df8bae1dSRodney W. Grimes #include <netinet/ip.h> 928e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 93686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 94df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 95ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 96686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 97686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h> 98686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 99960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h> 100960ed29cSSeigo Tanimura #include <netinet6/nd6.h> 101df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 102df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 103df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 104df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 105fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 106df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 10786a996e6SHiren Panchasara #ifdef TCPPCAP 10886a996e6SHiren Panchasara #include <netinet/tcp_pcap.h> 10986a996e6SHiren Panchasara #endif 110c325962bSMike Silbersack #include <netinet/tcp_syncache.h> 111610ee2f9SDavid Greenman #ifdef TCPDEBUG 112df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 113fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */ 11409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 11509fe6320SNavdeep Parhar #include <netinet/tcp_offload.h> 11609fe6320SNavdeep Parhar #endif 117fb59c426SYoshinobu Inoue 118b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 119b9234fafSSam Leffler #include <netipsec/ipsec.h> 120b9234fafSSam Leffler #include <netipsec/ipsec6.h> 121b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 122b9234fafSSam Leffler 123db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 124db4f9cc7SJonathan Lemon 125aed55708SRobert Watson #include <security/mac/mac_framework.h> 126aed55708SRobert Watson 127dbc42409SLawrence Stewart const int tcprexmtthresh = 3; 1280312fbe9SPoul-Henning Kamp 129773673c1SAndre Oppermann int tcp_log_in_vain = 0; 130816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 131eddfbb76SRobert Watson &tcp_log_in_vain, 0, 132eddfbb76SRobert Watson "Log all incoming TCP segments to closed ports"); 133816a3d83SPoul-Henning Kamp 13482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0; 13582cea7e6SBjoern A. Zeeb #define V_blackhole VNET(blackhole) 1366df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW, 137eddfbb76SRobert Watson &VNET_NAME(blackhole), 0, 138eddfbb76SRobert Watson "Do not send RST on segments to closed ports"); 13916f7f31fSGeoff Rehmet 14082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1; 1416df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW, 142eddfbb76SRobert Watson &VNET_NAME(tcp_delack_enabled), 0, 1433d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 144f498eeeeSDavid Greenman 14582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0; 14682cea7e6SBjoern A. Zeeb #define V_drop_synfin VNET(drop_synfin) 1476df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW, 148eddfbb76SRobert Watson &VNET_NAME(drop_synfin), 0, 149eddfbb76SRobert Watson "Drop TCP packets with SYN+FIN set"); 150f8613305SDag-Erling Smørgrav 151*12eeb81fSHiren Panchasara VNET_DEFINE(int, tcp_do_rfc6675_pipe) = 0; 152*12eeb81fSHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_pipe, CTLFLAG_VNET | CTLFLAG_RW, 153*12eeb81fSHiren Panchasara &VNET_NAME(tcp_do_rfc6675_pipe), 0, 154*12eeb81fSHiren Panchasara "Use calculated pipe/in-flight bytes per RFC 6675"); 155*12eeb81fSHiren Panchasara 15682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1; 15782cea7e6SBjoern A. Zeeb #define V_tcp_do_rfc3042 VNET(tcp_do_rfc3042) 1586df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW, 159eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3042), 0, 160eddfbb76SRobert Watson "Enable RFC 3042 (Limited Transmit)"); 161582a954bSJeffrey Hsu 16282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1; 1636df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW, 164eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3390), 0, 165da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 166da3a8a1aSJeffrey Hsu 167356c7958SHiren Panchasara VNET_DEFINE(int, tcp_initcwnd_segments) = 10; 168356c7958SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments, 169356c7958SHiren Panchasara CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0, 170356c7958SHiren Panchasara "Slow-start flight size (initial congestion window) in number of segments"); 17109440655SAndre Oppermann 17282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1; 1736df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW, 174eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3465), 0, 17524cb0f22SLawrence Stewart "Enable RFC 3465 (Appropriate Byte Counting)"); 176eddfbb76SRobert Watson 17782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2; 1786df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW, 179eddfbb76SRobert Watson &VNET_NAME(tcp_abc_l_var), 2, 18024cb0f22SLawrence Stewart "Cap the max cwnd increment during slow-start to this number of segments"); 18124cb0f22SLawrence Stewart 1826472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN"); 183f2512ba1SRui Paulo 18482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_ecn) = 0; 1856df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW, 186eddfbb76SRobert Watson &VNET_NAME(tcp_do_ecn), 0, 187eddfbb76SRobert Watson "TCP ECN support"); 188eddfbb76SRobert Watson 18982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1; 1906df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_VNET | CTLFLAG_RW, 191eddfbb76SRobert Watson &VNET_NAME(tcp_ecn_maxretries), 0, 192eddfbb76SRobert Watson "Max retries before giving up on ECN"); 193eddfbb76SRobert Watson 1943220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0; 1953220a212SGleb Smirnoff #define V_tcp_insecure_syn VNET(tcp_insecure_syn) 1966df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW, 1973220a212SGleb Smirnoff &VNET_NAME(tcp_insecure_syn), 0, 1983220a212SGleb Smirnoff "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets"); 1993220a212SGleb Smirnoff 20082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0; 20182cea7e6SBjoern A. Zeeb #define V_tcp_insecure_rst VNET(tcp_insecure_rst) 2026df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW, 203eddfbb76SRobert Watson &VNET_NAME(tcp_insecure_rst), 0, 2043220a212SGleb Smirnoff "Follow RFC793 instead of RFC5961 criteria for accepting RST packets"); 205a69968eeSMike Silbersack 206fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64; 207873789cbSAndre Oppermann #define V_tcp_recvspace VNET(tcp_recvspace) 2086df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW, 209873789cbSAndre Oppermann &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size"); 210873789cbSAndre Oppermann 21182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1; 21282cea7e6SBjoern A. Zeeb #define V_tcp_do_autorcvbuf VNET(tcp_do_autorcvbuf) 2136df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW, 214eddfbb76SRobert Watson &VNET_NAME(tcp_do_autorcvbuf), 0, 2158b615593SMarko Zec "Enable automatic receive buffer sizing"); 2166741ecf5SAndre Oppermann 21782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024; 21882cea7e6SBjoern A. Zeeb #define V_tcp_autorcvbuf_inc VNET(tcp_autorcvbuf_inc) 2196df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_VNET | CTLFLAG_RW, 220eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_inc), 0, 2216489fe65SAndre Oppermann "Incrementor step size of automatic receive buffer"); 2226741ecf5SAndre Oppermann 223b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024; 22482cea7e6SBjoern A. Zeeb #define V_tcp_autorcvbuf_max VNET(tcp_autorcvbuf_max) 2256df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW, 226eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_max), 0, 2278b615593SMarko Zec "Max size of automatic receive buffer"); 2286741ecf5SAndre Oppermann 229eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb); 23044e33a07SMarko Zec #define tcb6 tcb /* for KAME src sync over BSD*'s */ 23182cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo); 232df8bae1dSRodney W. Grimes 2335a53ca16SPaul Saab static void tcp_dooptions(struct tcpopt *, u_char *, int, int); 234679d9708SAndre Oppermann static void tcp_do_segment(struct mbuf *, struct tcphdr *, 235252ca428SRobert Watson struct socket *, struct tcpcb *, int, int, uint8_t, 236252ca428SRobert Watson int); 237302ce8d6SAndre Oppermann static void tcp_dropwithreset(struct mbuf *, struct tcphdr *, 238302ce8d6SAndre Oppermann struct tcpcb *, int, int); 2394d77a549SAlfred Perlstein static void tcp_pulloutofband(struct socket *, 2404d77a549SAlfred Perlstein struct tcphdr *, struct mbuf *, int); 2414d77a549SAlfred Perlstein static void tcp_xmit_timer(struct tcpcb *, int); 242c068736aSJeffrey Hsu static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 243dbc42409SLawrence Stewart static void inline cc_ack_received(struct tcpcb *tp, struct tcphdr *th, 244dbc42409SLawrence Stewart uint16_t type); 245dbc42409SLawrence Stewart static void inline cc_conn_init(struct tcpcb *tp); 246dbc42409SLawrence Stewart static void inline cc_post_recovery(struct tcpcb *tp, struct tcphdr *th); 24739bc9de5SLawrence Stewart static void inline hhook_run_tcp_est_in(struct tcpcb *tp, 24839bc9de5SLawrence Stewart struct tcphdr *th, struct tcpopt *to); 249f2512ba1SRui Paulo 250315e3e38SRobert Watson /* 2515da0521fSAndrey V. Elsukov * TCP statistics are stored in an "array" of counter(9)s. 2525923c293SGleb Smirnoff */ 2535da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat); 2545da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(tcpstat); 2555da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat, 2565da0521fSAndrey V. Elsukov tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 2575923c293SGleb Smirnoff 2585923c293SGleb Smirnoff #ifdef VIMAGE 2595da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(tcpstat); 2605923c293SGleb Smirnoff #endif /* VIMAGE */ 2615923c293SGleb Smirnoff /* 262315e3e38SRobert Watson * Kernel module interface for updating tcpstat. The argument is an index 2635923c293SGleb Smirnoff * into tcpstat treated as an array. 264315e3e38SRobert Watson */ 265315e3e38SRobert Watson void 266315e3e38SRobert Watson kmod_tcpstat_inc(int statnum) 267315e3e38SRobert Watson { 268315e3e38SRobert Watson 2695da0521fSAndrey V. Elsukov counter_u64_add(VNET(tcpstat)[statnum], 1); 270315e3e38SRobert Watson } 271315e3e38SRobert Watson 272dbc42409SLawrence Stewart /* 27339bc9de5SLawrence Stewart * Wrapper for the TCP established input helper hook. 27439bc9de5SLawrence Stewart */ 27539bc9de5SLawrence Stewart static void inline 27639bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to) 27739bc9de5SLawrence Stewart { 27839bc9de5SLawrence Stewart struct tcp_hhook_data hhook_data; 27939bc9de5SLawrence Stewart 28039bc9de5SLawrence Stewart if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) { 28139bc9de5SLawrence Stewart hhook_data.tp = tp; 28239bc9de5SLawrence Stewart hhook_data.th = th; 28339bc9de5SLawrence Stewart hhook_data.to = to; 28439bc9de5SLawrence Stewart 28539bc9de5SLawrence Stewart hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data, 28639bc9de5SLawrence Stewart tp->osd); 28739bc9de5SLawrence Stewart } 28839bc9de5SLawrence Stewart } 28939bc9de5SLawrence Stewart 29039bc9de5SLawrence Stewart /* 291dbc42409SLawrence Stewart * CC wrapper hook functions 292dbc42409SLawrence Stewart */ 293f2512ba1SRui Paulo static void inline 294dbc42409SLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t type) 295f2512ba1SRui Paulo { 296dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 297f2512ba1SRui Paulo 298dbc42409SLawrence Stewart tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th); 2995b648e79SLawrence Stewart if (tp->snd_cwnd <= tp->snd_wnd) 300dbc42409SLawrence Stewart tp->ccv->flags |= CCF_CWND_LIMITED; 301dbc42409SLawrence Stewart else 302dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_CWND_LIMITED; 303dbc42409SLawrence Stewart 304dbc42409SLawrence Stewart if (type == CC_ACK) { 305dbc42409SLawrence Stewart if (tp->snd_cwnd > tp->snd_ssthresh) { 306dbc42409SLawrence Stewart tp->t_bytes_acked += min(tp->ccv->bytes_this_ack, 307dbc42409SLawrence Stewart V_tcp_abc_l_var * tp->t_maxseg); 308dbc42409SLawrence Stewart if (tp->t_bytes_acked >= tp->snd_cwnd) { 309dbc42409SLawrence Stewart tp->t_bytes_acked -= tp->snd_cwnd; 310dbc42409SLawrence Stewart tp->ccv->flags |= CCF_ABC_SENTAWND; 311dbc42409SLawrence Stewart } 312dbc42409SLawrence Stewart } else { 313dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_ABC_SENTAWND; 314dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 315dbc42409SLawrence Stewart } 316dbc42409SLawrence Stewart } 317dbc42409SLawrence Stewart 318dbc42409SLawrence Stewart if (CC_ALGO(tp)->ack_received != NULL) { 319dbc42409SLawrence Stewart /* XXXLAS: Find a way to live without this */ 320dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 321dbc42409SLawrence Stewart CC_ALGO(tp)->ack_received(tp->ccv, type); 322dbc42409SLawrence Stewart } 323dbc42409SLawrence Stewart } 324dbc42409SLawrence Stewart 325dbc42409SLawrence Stewart static void inline 326dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp) 327dbc42409SLawrence Stewart { 328dbc42409SLawrence Stewart struct hc_metrics_lite metrics; 329dbc42409SLawrence Stewart struct inpcb *inp = tp->t_inpcb; 330dbc42409SLawrence Stewart int rtt; 331dbc42409SLawrence Stewart 332dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 333dbc42409SLawrence Stewart 334dbc42409SLawrence Stewart tcp_hc_get(&inp->inp_inc, &metrics); 335dbc42409SLawrence Stewart 336dbc42409SLawrence Stewart if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 337dbc42409SLawrence Stewart tp->t_srtt = rtt; 338dbc42409SLawrence Stewart tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 339dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrtt); 340dbc42409SLawrence Stewart if (metrics.rmx_rttvar) { 341dbc42409SLawrence Stewart tp->t_rttvar = metrics.rmx_rttvar; 342dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrttvar); 343dbc42409SLawrence Stewart } else { 344dbc42409SLawrence Stewart /* default variation is +- 1 rtt */ 345dbc42409SLawrence Stewart tp->t_rttvar = 346dbc42409SLawrence Stewart tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 347dbc42409SLawrence Stewart } 348dbc42409SLawrence Stewart TCPT_RANGESET(tp->t_rxtcur, 349dbc42409SLawrence Stewart ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 350dbc42409SLawrence Stewart tp->t_rttmin, TCPTV_REXMTMAX); 351dbc42409SLawrence Stewart } 352dbc42409SLawrence Stewart if (metrics.rmx_ssthresh) { 353dbc42409SLawrence Stewart /* 354dbc42409SLawrence Stewart * There's some sort of gateway or interface 355dbc42409SLawrence Stewart * buffer limit on the path. Use this to set 356dbc42409SLawrence Stewart * the slow start threshhold, but set the 357dbc42409SLawrence Stewart * threshold to no less than 2*mss. 358dbc42409SLawrence Stewart */ 359dbc42409SLawrence Stewart tp->snd_ssthresh = max(2 * tp->t_maxseg, metrics.rmx_ssthresh); 360dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedssthresh); 361dbc42409SLawrence Stewart } 362dbc42409SLawrence Stewart 363dbc42409SLawrence Stewart /* 3649ec4a4ccSAndre Oppermann * Set the initial slow-start flight size. 365dbc42409SLawrence Stewart * 366cf8f04f4SAndre Oppermann * RFC5681 Section 3.1 specifies the default conservative values. 367cf8f04f4SAndre Oppermann * RFC3390 specifies slightly more aggressive values. 368b8b4cfcdSSergey Kandaurov * RFC6928 increases it to ten segments. 369356c7958SHiren Panchasara * Support for user specified value for initial flight size. 370cf8f04f4SAndre Oppermann * 371cf8f04f4SAndre Oppermann * If a SYN or SYN/ACK was lost and retransmitted, we have to 372cf8f04f4SAndre Oppermann * reduce the initial CWND to one segment as congestion is likely 373cf8f04f4SAndre Oppermann * requiring us to be cautious. 374dbc42409SLawrence Stewart */ 375cf8f04f4SAndre Oppermann if (tp->snd_cwnd == 1) 376cf8f04f4SAndre Oppermann tp->snd_cwnd = tp->t_maxseg; /* SYN(-ACK) lost */ 377356c7958SHiren Panchasara else if (V_tcp_initcwnd_segments) 378356c7958SHiren Panchasara tp->snd_cwnd = min(V_tcp_initcwnd_segments * tp->t_maxseg, 379356c7958SHiren Panchasara max(2 * tp->t_maxseg, V_tcp_initcwnd_segments * 1460)); 380cf8f04f4SAndre Oppermann else if (V_tcp_do_rfc3390) 381dbc42409SLawrence Stewart tp->snd_cwnd = min(4 * tp->t_maxseg, 382dbc42409SLawrence Stewart max(2 * tp->t_maxseg, 4380)); 38322efabd4SAndre Oppermann else { 38422efabd4SAndre Oppermann /* Per RFC5681 Section 3.1 */ 38522efabd4SAndre Oppermann if (tp->t_maxseg > 2190) 38622efabd4SAndre Oppermann tp->snd_cwnd = 2 * tp->t_maxseg; 38722efabd4SAndre Oppermann else if (tp->t_maxseg > 1095) 38822efabd4SAndre Oppermann tp->snd_cwnd = 3 * tp->t_maxseg; 389dbc42409SLawrence Stewart else 39022efabd4SAndre Oppermann tp->snd_cwnd = 4 * tp->t_maxseg; 39122efabd4SAndre Oppermann } 392dbc42409SLawrence Stewart 393dbc42409SLawrence Stewart if (CC_ALGO(tp)->conn_init != NULL) 394dbc42409SLawrence Stewart CC_ALGO(tp)->conn_init(tp->ccv); 395dbc42409SLawrence Stewart } 396dbc42409SLawrence Stewart 397dbc42409SLawrence Stewart void inline 398dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type) 399dbc42409SLawrence Stewart { 400dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 401dbc42409SLawrence Stewart 402dbc42409SLawrence Stewart switch(type) { 403dbc42409SLawrence Stewart case CC_NDUPACK: 404dbc42409SLawrence Stewart if (!IN_FASTRECOVERY(tp->t_flags)) { 405f2512ba1SRui Paulo tp->snd_recover = tp->snd_max; 406f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) 407f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_CWR; 408f2512ba1SRui Paulo } 409dbc42409SLawrence Stewart break; 410dbc42409SLawrence Stewart case CC_ECN: 411dbc42409SLawrence Stewart if (!IN_CONGRECOVERY(tp->t_flags)) { 412dbc42409SLawrence Stewart TCPSTAT_INC(tcps_ecn_rcwnd); 413dbc42409SLawrence Stewart tp->snd_recover = tp->snd_max; 414dbc42409SLawrence Stewart if (tp->t_flags & TF_ECN_PERMIT) 415dbc42409SLawrence Stewart tp->t_flags |= TF_ECN_SND_CWR; 416dbc42409SLawrence Stewart } 417dbc42409SLawrence Stewart break; 418dbc42409SLawrence Stewart case CC_RTO: 419dbc42409SLawrence Stewart tp->t_dupacks = 0; 420dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 421dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 4226157935fSLawrence Stewart tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 / 4236157935fSLawrence Stewart tp->t_maxseg) * tp->t_maxseg; 424dbc42409SLawrence Stewart tp->snd_cwnd = tp->t_maxseg; 425dbc42409SLawrence Stewart break; 426dbc42409SLawrence Stewart case CC_RTO_ERR: 427dbc42409SLawrence Stewart TCPSTAT_INC(tcps_sndrexmitbad); 428dbc42409SLawrence Stewart /* RTO was unnecessary, so reset everything. */ 429dbc42409SLawrence Stewart tp->snd_cwnd = tp->snd_cwnd_prev; 430dbc42409SLawrence Stewart tp->snd_ssthresh = tp->snd_ssthresh_prev; 431dbc42409SLawrence Stewart tp->snd_recover = tp->snd_recover_prev; 432dbc42409SLawrence Stewart if (tp->t_flags & TF_WASFRECOVERY) 433dbc42409SLawrence Stewart ENTER_FASTRECOVERY(tp->t_flags); 434dbc42409SLawrence Stewart if (tp->t_flags & TF_WASCRECOVERY) 435dbc42409SLawrence Stewart ENTER_CONGRECOVERY(tp->t_flags); 436dbc42409SLawrence Stewart tp->snd_nxt = tp->snd_max; 437672dc4aeSJohn Baldwin tp->t_flags &= ~TF_PREVVALID; 438dbc42409SLawrence Stewart tp->t_badrxtwin = 0; 439dbc42409SLawrence Stewart break; 440dbc42409SLawrence Stewart } 441dbc42409SLawrence Stewart 442dbc42409SLawrence Stewart if (CC_ALGO(tp)->cong_signal != NULL) { 443dbc42409SLawrence Stewart if (th != NULL) 444dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 445dbc42409SLawrence Stewart CC_ALGO(tp)->cong_signal(tp->ccv, type); 446dbc42409SLawrence Stewart } 447dbc42409SLawrence Stewart } 448dbc42409SLawrence Stewart 449dbc42409SLawrence Stewart static void inline 450dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th) 451dbc42409SLawrence Stewart { 452dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 453dbc42409SLawrence Stewart 454dbc42409SLawrence Stewart /* XXXLAS: KASSERT that we're in recovery? */ 455dbc42409SLawrence Stewart 456dbc42409SLawrence Stewart if (CC_ALGO(tp)->post_recovery != NULL) { 457dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 458dbc42409SLawrence Stewart CC_ALGO(tp)->post_recovery(tp->ccv); 459dbc42409SLawrence Stewart } 460dbc42409SLawrence Stewart /* XXXLAS: EXIT_RECOVERY ? */ 461dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 462dbc42409SLawrence Stewart } 4630312fbe9SPoul-Henning Kamp 4642903309aSAttilio Rao #ifdef TCP_SIGNATURE 4652903309aSAttilio Rao static inline int 4662903309aSAttilio Rao tcp_signature_verify_input(struct mbuf *m, int off0, int tlen, int optlen, 4672903309aSAttilio Rao struct tcpopt *to, struct tcphdr *th, u_int tcpbflag) 4682903309aSAttilio Rao { 4692903309aSAttilio Rao int ret; 4702903309aSAttilio Rao 4712903309aSAttilio Rao tcp_fields_to_net(th); 4722903309aSAttilio Rao ret = tcp_signature_verify(m, off0, tlen, optlen, to, th, tcpbflag); 4732903309aSAttilio Rao tcp_fields_to_host(th); 4742903309aSAttilio Rao return (ret); 4752903309aSAttilio Rao } 4762903309aSAttilio Rao #endif 4772903309aSAttilio Rao 478df8bae1dSRodney W. Grimes /* 479262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 48085536381SHiren Panchasara * following conditions are met: 48185536381SHiren Panchasara * - There is no delayed ack timer in progress. 48285536381SHiren Panchasara * - Our last ack wasn't a 0-sized window. We never want to delay 48385536381SHiren Panchasara * the ack that opens up a 0-sized window. 48485536381SHiren Panchasara * - LRO wasn't used for this segment. We make sure by checking that the 48585536381SHiren Panchasara * segment size is not larger than the MSS. 48685536381SHiren Panchasara * - Delayed acks are enabled or this is a half-synchronized T/TCP 48785536381SHiren Panchasara * connection. 488d8c85a26SJonathan Lemon */ 489c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen) \ 490b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 491a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 492c1e5a6e5SAndre Oppermann (tlen <= tp->t_maxopd) && \ 493603724d3SBjoern A. Zeeb (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 494d8c85a26SJonathan Lemon 49564807b30SHiren Panchasara static void inline 49664807b30SHiren Panchasara cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos) 49764807b30SHiren Panchasara { 49864807b30SHiren Panchasara INP_WLOCK_ASSERT(tp->t_inpcb); 49964807b30SHiren Panchasara 50064807b30SHiren Panchasara if (CC_ALGO(tp)->ecnpkt_handler != NULL) { 50164807b30SHiren Panchasara switch (iptos & IPTOS_ECN_MASK) { 50264807b30SHiren Panchasara case IPTOS_ECN_CE: 50364807b30SHiren Panchasara tp->ccv->flags |= CCF_IPHDR_CE; 50464807b30SHiren Panchasara break; 50564807b30SHiren Panchasara case IPTOS_ECN_ECT0: 50664807b30SHiren Panchasara tp->ccv->flags &= ~CCF_IPHDR_CE; 50764807b30SHiren Panchasara break; 50864807b30SHiren Panchasara case IPTOS_ECN_ECT1: 50964807b30SHiren Panchasara tp->ccv->flags &= ~CCF_IPHDR_CE; 51064807b30SHiren Panchasara break; 51164807b30SHiren Panchasara } 51264807b30SHiren Panchasara 51364807b30SHiren Panchasara if (th->th_flags & TH_CWR) 51464807b30SHiren Panchasara tp->ccv->flags |= CCF_TCPHDR_CWR; 51564807b30SHiren Panchasara else 51664807b30SHiren Panchasara tp->ccv->flags &= ~CCF_TCPHDR_CWR; 51764807b30SHiren Panchasara 51864807b30SHiren Panchasara if (tp->t_flags & TF_DELACK) 51964807b30SHiren Panchasara tp->ccv->flags |= CCF_DELACK; 52064807b30SHiren Panchasara else 52164807b30SHiren Panchasara tp->ccv->flags &= ~CCF_DELACK; 52264807b30SHiren Panchasara 52364807b30SHiren Panchasara CC_ALGO(tp)->ecnpkt_handler(tp->ccv); 52464807b30SHiren Panchasara 52564807b30SHiren Panchasara if (tp->ccv->flags & CCF_ACKNOW) 52664807b30SHiren Panchasara tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 52764807b30SHiren Panchasara } 52864807b30SHiren Panchasara } 52964807b30SHiren Panchasara 530df8bae1dSRodney W. Grimes /* 5318d573cc1SAndre Oppermann * TCP input handling is split into multiple parts: 5328d573cc1SAndre Oppermann * tcp6_input is a thin wrapper around tcp_input for the extended 5338d573cc1SAndre Oppermann * ip6_protox[] call format in ip6_input 5348d573cc1SAndre Oppermann * tcp_input handles primary segment validation, inpcb lookup and 5358d573cc1SAndre Oppermann * SYN processing on listen sockets 5368d573cc1SAndre Oppermann * tcp_do_segment processes the ACK and text of the segment for 5378d573cc1SAndre Oppermann * establishing, established and closing connections 538df8bae1dSRodney W. Grimes */ 539fb59c426SYoshinobu Inoue #ifdef INET6 540fb59c426SYoshinobu Inoue int 541ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto) 542fb59c426SYoshinobu Inoue { 543ad3f9ab3SAndre Oppermann struct mbuf *m = *mp; 54433841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 5453e88eb90SAndrey V. Elsukov struct ip6_hdr *ip6; 546fb59c426SYoshinobu Inoue 547fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 548fb59c426SYoshinobu Inoue 549fb59c426SYoshinobu Inoue /* 550fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 551fb59c426SYoshinobu Inoue * better place to put this in? 552fb59c426SYoshinobu Inoue */ 5533e88eb90SAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 5543e88eb90SAndrey V. Elsukov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */); 55533841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 556fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 557fb59c426SYoshinobu Inoue 5588c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 559fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 560fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 561fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 5628f5a8818SKevin Lo return (IPPROTO_DONE); 563fb59c426SYoshinobu Inoue } 56477d396fdSMaksim Yevmenkin if (ia6) 56577d396fdSMaksim Yevmenkin ifa_free(&ia6->ia_ifa); 566fb59c426SYoshinobu Inoue 5678f5a8818SKevin Lo return (tcp_input(mp, offp, proto)); 568fb59c426SYoshinobu Inoue } 569b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 570fb59c426SYoshinobu Inoue 5718f5a8818SKevin Lo int 5728f5a8818SKevin Lo tcp_input(struct mbuf **mp, int *offp, int proto) 573df8bae1dSRodney W. Grimes { 5748f5a8818SKevin Lo struct mbuf *m = *mp; 575b287c6c7SBjoern A. Zeeb struct tcphdr *th = NULL; 576ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 577ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 578302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 579302ce8d6SAndre Oppermann struct socket *so = NULL; 580e79adb8eSGarrett Wollman u_char *optp = NULL; 5818f5a8818SKevin Lo int off0; 58226f9a767SRodney W. Grimes int optlen = 0; 583b287c6c7SBjoern A. Zeeb #ifdef INET 584b287c6c7SBjoern A. Zeeb int len; 585b287c6c7SBjoern A. Zeeb #endif 586b287c6c7SBjoern A. Zeeb int tlen = 0, off; 587fb59c426SYoshinobu Inoue int drop_hdrlen; 588ad3f9ab3SAndre Oppermann int thflags; 589302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 5902903309aSAttilio Rao #ifdef TCP_SIGNATURE 5912903309aSAttilio Rao uint8_t sig_checked = 0; 5922903309aSAttilio Rao #endif 593b287c6c7SBjoern A. Zeeb uint8_t iptos = 0; 594c1de64a4SAndrey V. Elsukov struct m_tag *fwd_tag = NULL; 595c068736aSJeffrey Hsu #ifdef INET6 596302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 597c068736aSJeffrey Hsu int isipv6; 598c068736aSJeffrey Hsu #else 599a160e630SAndre Oppermann const void *ip6 = NULL; 600b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 601302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 602a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 603252ca428SRobert Watson int ti_locked; 604252ca428SRobert Watson #define TI_UNLOCKED 1 605ff9b006dSJulien Charbon #define TI_RLOCKED 2 606f76fcf6dSJeffrey Hsu 607610ee2f9SDavid Greenman #ifdef TCPDEBUG 608c068736aSJeffrey Hsu /* 609c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 610c068736aSJeffrey Hsu * now IPv6. 611c068736aSJeffrey Hsu */ 61214739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 613410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 614610ee2f9SDavid Greenman short ostate = 0; 615610ee2f9SDavid Greenman #endif 616c068736aSJeffrey Hsu 617fb59c426SYoshinobu Inoue #ifdef INET6 618fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 619fb59c426SYoshinobu Inoue #endif 620a0292f23SGarrett Wollman 6218f5a8818SKevin Lo off0 = *offp; 6228f5a8818SKevin Lo m = *mp; 6238f5a8818SKevin Lo *mp = NULL; 624302ce8d6SAndre Oppermann to.to_flags = 0; 62578b50714SRobert Watson TCPSTAT_INC(tcps_rcvtotal); 626fb59c426SYoshinobu Inoue 62704d3a452SHajimu UMEMOTO #ifdef INET6 628b287c6c7SBjoern A. Zeeb if (isipv6) { 6298d573cc1SAndre Oppermann /* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */ 63045747ba5SBjoern A. Zeeb 63145747ba5SBjoern A. Zeeb if (m->m_len < (sizeof(*ip6) + sizeof(*th))) { 63245747ba5SBjoern A. Zeeb m = m_pullup(m, sizeof(*ip6) + sizeof(*th)); 63345747ba5SBjoern A. Zeeb if (m == NULL) { 63445747ba5SBjoern A. Zeeb TCPSTAT_INC(tcps_rcvshort); 6358f5a8818SKevin Lo return (IPPROTO_DONE); 63645747ba5SBjoern A. Zeeb } 63745747ba5SBjoern A. Zeeb } 63845747ba5SBjoern A. Zeeb 639fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 64045747ba5SBjoern A. Zeeb th = (struct tcphdr *)((caddr_t)ip6 + off0); 641fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 642356ab07eSBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) { 64345747ba5SBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 64445747ba5SBjoern A. Zeeb th->th_sum = m->m_pkthdr.csum_data; 64545747ba5SBjoern A. Zeeb else 64645747ba5SBjoern A. Zeeb th->th_sum = in6_cksum_pseudo(ip6, tlen, 64745747ba5SBjoern A. Zeeb IPPROTO_TCP, m->m_pkthdr.csum_data); 64845747ba5SBjoern A. Zeeb th->th_sum ^= 0xffff; 64945747ba5SBjoern A. Zeeb } else 65045747ba5SBjoern A. Zeeb th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen); 65145747ba5SBjoern A. Zeeb if (th->th_sum) { 65278b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 653fb59c426SYoshinobu Inoue goto drop; 654fb59c426SYoshinobu Inoue } 65533841545SHajimu UMEMOTO 65633841545SHajimu UMEMOTO /* 65733841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 65833841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 65933841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 66033841545SHajimu UMEMOTO * 66133841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 66233841545SHajimu UMEMOTO * already dropped in ip6_input. 66333841545SHajimu UMEMOTO */ 66433841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 66533841545SHajimu UMEMOTO /* XXX stat */ 66633841545SHajimu UMEMOTO goto drop; 66733841545SHajimu UMEMOTO } 668b287c6c7SBjoern A. Zeeb } 66904d3a452SHajimu UMEMOTO #endif 670b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 671b287c6c7SBjoern A. Zeeb else 672b287c6c7SBjoern A. Zeeb #endif 673b287c6c7SBjoern A. Zeeb #ifdef INET 674b287c6c7SBjoern A. Zeeb { 675df8bae1dSRodney W. Grimes /* 676df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 677df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 678df8bae1dSRodney W. Grimes */ 679fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 680105bd211SGleb Smirnoff ip_stripoptions(m); 681fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 682fb59c426SYoshinobu Inoue } 683df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 6844dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 6854dfdffe9SAndre Oppermann == NULL) { 68678b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 6878f5a8818SKevin Lo return (IPPROTO_DONE); 688df8bae1dSRodney W. Grimes } 689df8bae1dSRodney W. Grimes } 690fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 691db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 6928ad458a4SGleb Smirnoff tlen = ntohs(ip->ip_len) - off0; 693df8bae1dSRodney W. Grimes 694db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 695db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 696db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 697db4f9cc7SJonathan Lemon else 698db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 699c068736aSJeffrey Hsu ip->ip_dst.s_addr, 7008f134647SGleb Smirnoff htonl(m->m_pkthdr.csum_data + tlen + 701c068736aSJeffrey Hsu IPPROTO_TCP)); 702db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 703db4f9cc7SJonathan Lemon } else { 7048f134647SGleb Smirnoff struct ipovly *ipov = (struct ipovly *)ip; 7058f134647SGleb Smirnoff 706df8bae1dSRodney W. Grimes /* 707df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 708df8bae1dSRodney W. Grimes */ 7098f134647SGleb Smirnoff len = off0 + tlen; 710fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 7118f134647SGleb Smirnoff ipov->ih_len = htons(tlen); 712fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 71357f60867SMark Johnston /* Reset length for SDT probes. */ 71457f60867SMark Johnston ip->ip_len = htons(tlen + off0); 715db4f9cc7SJonathan Lemon } 71657f60867SMark Johnston 717fb59c426SYoshinobu Inoue if (th->th_sum) { 71878b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 719df8bae1dSRodney W. Grimes goto drop; 720df8bae1dSRodney W. Grimes } 721fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 722fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 723fb59c426SYoshinobu Inoue } 724b287c6c7SBjoern A. Zeeb #endif /* INET */ 725df8bae1dSRodney W. Grimes 726f2512ba1SRui Paulo #ifdef INET6 727f2512ba1SRui Paulo if (isipv6) 728f2512ba1SRui Paulo iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 729b287c6c7SBjoern A. Zeeb #endif 730b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 731f2512ba1SRui Paulo else 732f2512ba1SRui Paulo #endif 733b287c6c7SBjoern A. Zeeb #ifdef INET 734f2512ba1SRui Paulo iptos = ip->ip_tos; 735b287c6c7SBjoern A. Zeeb #endif 736f2512ba1SRui Paulo 737df8bae1dSRodney W. Grimes /* 738df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 739df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 740df8bae1dSRodney W. Grimes */ 741fb59c426SYoshinobu Inoue off = th->th_off << 2; 742df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 74378b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadoff); 744df8bae1dSRodney W. Grimes goto drop; 745df8bae1dSRodney W. Grimes } 746fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 747df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 74804d3a452SHajimu UMEMOTO #ifdef INET6 749b287c6c7SBjoern A. Zeeb if (isipv6) { 7508f5a8818SKevin Lo IP6_EXTHDR_CHECK(m, off0, off, IPPROTO_DONE); 751fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 752fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 753b287c6c7SBjoern A. Zeeb } 75404d3a452SHajimu UMEMOTO #endif 755b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 756b287c6c7SBjoern A. Zeeb else 757b287c6c7SBjoern A. Zeeb #endif 758b287c6c7SBjoern A. Zeeb #ifdef INET 759b287c6c7SBjoern A. Zeeb { 760df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 761c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 7624dfdffe9SAndre Oppermann == NULL) { 76378b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 7648f5a8818SKevin Lo return (IPPROTO_DONE); 765df8bae1dSRodney W. Grimes } 766fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 767fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 768fb59c426SYoshinobu Inoue } 769df8bae1dSRodney W. Grimes } 770b287c6c7SBjoern A. Zeeb #endif 771df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 772fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 773df8bae1dSRodney W. Grimes } 774fb59c426SYoshinobu Inoue thflags = th->th_flags; 775df8bae1dSRodney W. Grimes 776e46cd3d4SDag-Erling Smørgrav /* 777df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 778df8bae1dSRodney W. Grimes */ 7792903309aSAttilio Rao tcp_fields_to_host(th); 780df8bae1dSRodney W. Grimes 781df8bae1dSRodney W. Grimes /* 782794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 783755c1f07SAndras Olah */ 784fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 785755c1f07SAndras Olah 786755c1f07SAndras Olah /* 787fa046d87SRobert Watson * Locate pcb for segment; if we're likely to add or remove a 788a7c7f2a7SJohn Baldwin * connection then first acquire pcbinfo lock. There are three cases 789fa046d87SRobert Watson * where we might discover later we need a write lock despite the 790a7c7f2a7SJohn Baldwin * flags: ACKs moving a connection out of the syncache, ACKs for a 791a7c7f2a7SJohn Baldwin * connection in TIMEWAIT and SYNs not targeting a listening socket. 792df8bae1dSRodney W. Grimes */ 793a7c7f2a7SJohn Baldwin if ((thflags & (TH_FIN | TH_RST)) != 0) { 794ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 795ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 796fa046d87SRobert Watson } else 797fa046d87SRobert Watson ti_locked = TI_UNLOCKED; 798252ca428SRobert Watson 7998d573cc1SAndre Oppermann /* 8008d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 8018d573cc1SAndre Oppermann */ 802b8056faeSGleb Smirnoff if ( 803b8056faeSGleb Smirnoff #ifdef INET6 804b8056faeSGleb Smirnoff (isipv6 && (m->m_flags & M_IP6_NEXTHOP)) 805b8056faeSGleb Smirnoff #ifdef INET 806b8056faeSGleb Smirnoff || (!isipv6 && (m->m_flags & M_IP_NEXTHOP)) 807b8056faeSGleb Smirnoff #endif 808b8056faeSGleb Smirnoff #endif 809b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6) 810b8056faeSGleb Smirnoff (m->m_flags & M_IP_NEXTHOP) 811b8056faeSGleb Smirnoff #endif 812b8056faeSGleb Smirnoff ) 8139b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 8149b932e9eSAndre Oppermann 815ce7ad664SEd Maste findpcb: 816ce7ad664SEd Maste #ifdef INVARIANTS 817ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 818ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 819ce7ad664SEd Maste } else { 820ce7ad664SEd Maste INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 821ce7ad664SEd Maste } 822ce7ad664SEd Maste #endif 8238a006adbSBjoern A. Zeeb #ifdef INET6 8248a006adbSBjoern A. Zeeb if (isipv6 && fwd_tag != NULL) { 8258a006adbSBjoern A. Zeeb struct sockaddr_in6 *next_hop6; 8268a006adbSBjoern A. Zeeb 8278a006adbSBjoern A. Zeeb next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1); 8288a006adbSBjoern A. Zeeb /* 8298a006adbSBjoern A. Zeeb * Transparently forwarded. Pretend to be the destination. 8308a006adbSBjoern A. Zeeb * Already got one like this? 8318a006adbSBjoern A. Zeeb */ 8328a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, 8338a006adbSBjoern A. Zeeb &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport, 8348a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m); 8358a006adbSBjoern A. Zeeb if (!inp) { 8368a006adbSBjoern A. Zeeb /* 8378a006adbSBjoern A. Zeeb * It's new. Try to find the ambushing socket. 8388a006adbSBjoern A. Zeeb * Because we've rewritten the destination address, 8398a006adbSBjoern A. Zeeb * any hardware-generated hash is ignored. 8408a006adbSBjoern A. Zeeb */ 8418a006adbSBjoern A. Zeeb inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src, 8428a006adbSBjoern A. Zeeb th->th_sport, &next_hop6->sin6_addr, 8438a006adbSBjoern A. Zeeb next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) : 8448a006adbSBjoern A. Zeeb th->th_dport, INPLOOKUP_WILDCARD | 8458a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 8468a006adbSBjoern A. Zeeb } 847c1de64a4SAndrey V. Elsukov } else if (isipv6) { 8488a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src, 8498a006adbSBjoern A. Zeeb th->th_sport, &ip6->ip6_dst, th->th_dport, 8508a006adbSBjoern A. Zeeb INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 8518a006adbSBjoern A. Zeeb m->m_pkthdr.rcvif, m); 8528a006adbSBjoern A. Zeeb } 8538a006adbSBjoern A. Zeeb #endif /* INET6 */ 8548a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET) 8558a006adbSBjoern A. Zeeb else 8568a006adbSBjoern A. Zeeb #endif 8578a006adbSBjoern A. Zeeb #ifdef INET 8588a006adbSBjoern A. Zeeb if (fwd_tag != NULL) { 8599b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 8609b932e9eSAndre Oppermann 8619b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 862f9e354dfSJulian Elischer /* 8632b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 864f9e354dfSJulian Elischer * already got one like this? 865f9e354dfSJulian Elischer */ 866d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport, 867fa046d87SRobert Watson ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB, 868d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 869fa046d87SRobert Watson if (!inp) { 870fa046d87SRobert Watson /* 871fa046d87SRobert Watson * It's new. Try to find the ambushing socket. 872d3c1f003SRobert Watson * Because we've rewritten the destination address, 873d3c1f003SRobert Watson * any hardware-generated hash is ignored. 874fa046d87SRobert Watson */ 875fa046d87SRobert Watson inp = in_pcblookup(&V_tcbinfo, ip->ip_src, 876fa046d87SRobert Watson th->th_sport, next_hop->sin_addr, 877fa046d87SRobert Watson next_hop->sin_port ? ntohs(next_hop->sin_port) : 878fa046d87SRobert Watson th->th_dport, INPLOOKUP_WILDCARD | 879fa046d87SRobert Watson INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 880f9e354dfSJulian Elischer } 881b10fbdeaSAndre Oppermann } else 882d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, 883fa046d87SRobert Watson th->th_sport, ip->ip_dst, th->th_dport, 884fa046d87SRobert Watson INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 885d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 8868a006adbSBjoern A. Zeeb #endif /* INET */ 887f9e354dfSJulian Elischer 888df8bae1dSRodney W. Grimes /* 889574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 890574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 8918b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 892df8bae1dSRodney W. Grimes */ 893816a3d83SPoul-Henning Kamp if (inp == NULL) { 894574b6964SAndre Oppermann /* 895574b6964SAndre Oppermann * Log communication attempts to ports that are not 896574b6964SAndre Oppermann * in use. 897574b6964SAndre Oppermann */ 898574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 899574b6964SAndre Oppermann tcp_log_in_vain == 2) { 900b7d747ecSAndre Oppermann if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6))) 901df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 902df541e5fSAndre Oppermann "to closed port\n", s, __func__); 9032e4e1b4cSGeoff Rehmet } 904574b6964SAndre Oppermann /* 905574b6964SAndre Oppermann * When blackholing do not respond with a RST but 906574b6964SAndre Oppermann * completely ignore the segment and drop it. 907574b6964SAndre Oppermann */ 908603724d3SBjoern A. Zeeb if ((V_blackhole == 1 && (thflags & TH_SYN)) || 909603724d3SBjoern A. Zeeb V_blackhole == 2) 9101929eae1SAndre Oppermann goto dropunlock; 911574b6964SAndre Oppermann 912a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 913a57815efSBosko Milekic goto dropwithreset; 914816a3d83SPoul-Henning Kamp } 915fa046d87SRobert Watson INP_WLOCK_ASSERT(inp); 916c2529042SHans Petter Selasky if ((inp->inp_flowtype == M_HASHTYPE_NONE) && 917c2529042SHans Petter Selasky (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) && 918c2529042SHans Petter Selasky ((inp->inp_socket == NULL) || 919c2529042SHans Petter Selasky (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0)) { 92080cb9f21SKip Macy inp->inp_flowid = m->m_pkthdr.flowid; 9212f719932SAdrian Chadd inp->inp_flowtype = M_HASHTYPE_GET(m); 92280cb9f21SKip Macy } 923a2589465SKen Smith #ifdef IPSEC 924a2589465SKen Smith #ifdef INET6 925a2589465SKen Smith if (isipv6 && ipsec6_in_reject(m, inp)) { 926a2589465SKen Smith goto dropunlock; 927a2589465SKen Smith } else 928a2589465SKen Smith #endif /* INET6 */ 929a2589465SKen Smith if (ipsec4_in_reject(m, inp) != 0) { 930a2589465SKen Smith goto dropunlock; 931a2589465SKen Smith } 932a2589465SKen Smith #endif /* IPSEC */ 933a2589465SKen Smith 9348d573cc1SAndre Oppermann /* 9358d573cc1SAndre Oppermann * Check the minimum TTL for socket. 9368d573cc1SAndre Oppermann */ 93734f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 93834f83c52SGeorge V. Neville-Neil #ifdef INET6 93934f83c52SGeorge V. Neville-Neil if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim) 940302ce8d6SAndre Oppermann goto dropunlock; 94102707462SAndre Oppermann else 94234f83c52SGeorge V. Neville-Neil #endif 94334f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 944302ce8d6SAndre Oppermann goto dropunlock; 94534f83c52SGeorge V. Neville-Neil } 946936cd18dSAndre Oppermann 947340c35deSJonathan Lemon /* 948252ca428SRobert Watson * A previous connection in TIMEWAIT state is supposed to catch stray 949252ca428SRobert Watson * or duplicate segments arriving late. If this segment was a 9500989f56cSRobert Watson * legitimate new connection attempt, the old INPCB gets removed and 951252ca428SRobert Watson * we can try again to find a listening socket. 952252ca428SRobert Watson * 953fa046d87SRobert Watson * At this point, due to earlier optimism, we may hold only an inpcb 954fa046d87SRobert Watson * lock, and not the inpcbinfo write lock. If so, we need to try to 955fa046d87SRobert Watson * acquire it, or if that fails, acquire a reference on the inpcb, 956fa046d87SRobert Watson * drop all locks, acquire a global write lock, and then re-acquire 957fa046d87SRobert Watson * the inpcb lock. We may at that point discover that another thread 958fa046d87SRobert Watson * has tried to free the inpcb, in which case we need to loop back 959fa046d87SRobert Watson * and try to find a new inpcb to deliver to. 960fa046d87SRobert Watson * 961fa046d87SRobert Watson * XXXRW: It may be time to rethink timewait locking. 962340c35deSJonathan Lemon */ 963883e9bc4SRobert Watson relocked: 964ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 965fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 966ff9b006dSJulien Charbon if (INP_INFO_TRY_RLOCK(&V_tcbinfo) == 0) { 967252ca428SRobert Watson in_pcbref(inp); 968252ca428SRobert Watson INP_WUNLOCK(inp); 969ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 970ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 971252ca428SRobert Watson INP_WLOCK(inp); 972fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) { 973252ca428SRobert Watson inp = NULL; 974252ca428SRobert Watson goto findpcb; 975252ca428SRobert Watson } 976f681a5fdSRobert Watson } else 977ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 978252ca428SRobert Watson } 979ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 980252ca428SRobert Watson 981340c35deSJonathan Lemon if (thflags & TH_SYN) 982f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 9838d573cc1SAndre Oppermann /* 9848d573cc1SAndre Oppermann * NB: tcp_twcheck unlocks the INP and frees the mbuf. 9858d573cc1SAndre Oppermann */ 9862104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 987340c35deSJonathan Lemon goto findpcb; 988ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 9898f5a8818SKevin Lo return (IPPROTO_DONE); 990340c35deSJonathan Lemon } 991794235b7SAndre Oppermann /* 992794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 993794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 994794235b7SAndre Oppermann * segment and send an appropriate response. 995794235b7SAndre Oppermann */ 996df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 997a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 998a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 999a57815efSBosko Milekic goto dropwithreset; 100009f81a46SBosko Milekic } 1001df8bae1dSRodney W. Grimes 100209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 100309fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 100409fe6320SNavdeep Parhar tcp_offload_input(tp, m); 100509fe6320SNavdeep Parhar m = NULL; /* consumed by the TOE driver */ 100609fe6320SNavdeep Parhar goto dropunlock; 100709fe6320SNavdeep Parhar } 100809fe6320SNavdeep Parhar #endif 100909fe6320SNavdeep Parhar 1010252ca428SRobert Watson /* 1011252ca428SRobert Watson * We've identified a valid inpcb, but it could be that we need an 1012fa046d87SRobert Watson * inpcbinfo write lock but don't hold it. In this case, attempt to 1013fa046d87SRobert Watson * acquire using the same strategy as the TIMEWAIT case above. If we 1014fa046d87SRobert Watson * relock, we have to jump back to 'relocked' as the connection might 1015fa046d87SRobert Watson * now be in TIMEWAIT. 1016252ca428SRobert Watson */ 1017fa046d87SRobert Watson #ifdef INVARIANTS 1018a7c7f2a7SJohn Baldwin if ((thflags & (TH_FIN | TH_RST)) != 0) 1019ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1020fa046d87SRobert Watson #endif 1021a7c7f2a7SJohn Baldwin if (!((tp->t_state == TCPS_ESTABLISHED && (thflags & TH_SYN) == 0) || 1022a7c7f2a7SJohn Baldwin (tp->t_state == TCPS_LISTEN && (thflags & TH_SYN)))) { 1023fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 1024ff9b006dSJulien Charbon if (INP_INFO_TRY_RLOCK(&V_tcbinfo) == 0) { 1025252ca428SRobert Watson in_pcbref(inp); 1026252ca428SRobert Watson INP_WUNLOCK(inp); 1027ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 1028ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 1029252ca428SRobert Watson INP_WLOCK(inp); 1030fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) { 1031252ca428SRobert Watson inp = NULL; 1032252ca428SRobert Watson goto findpcb; 1033252ca428SRobert Watson } 1034883e9bc4SRobert Watson goto relocked; 1035f681a5fdSRobert Watson } else 1036ff9b006dSJulien Charbon ti_locked = TI_RLOCKED; 1037252ca428SRobert Watson } 1038ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1039252ca428SRobert Watson } 1040252ca428SRobert Watson 1041c488362eSRobert Watson #ifdef MAC 10428501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 104330d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 1044302ce8d6SAndre Oppermann goto dropunlock; 1045c488362eSRobert Watson #endif 1046a557af22SRobert Watson so = inp->inp_socket; 1047302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 1048610ee2f9SDavid Greenman #ifdef TCPDEBUG 1049df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 1050df8bae1dSRodney W. Grimes ostate = tp->t_state; 105110fe523eSMaxim Konovalov #ifdef INET6 10526f697424SBjoern A. Zeeb if (isipv6) { 1053540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 1054d30d90dcSMaxim Konovalov } else 10556f697424SBjoern A. Zeeb #endif 1056540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 1057fb59c426SYoshinobu Inoue tcp_savetcp = *th; 1058df8bae1dSRodney W. Grimes } 1059b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */ 1060db33b3e6SAndre Oppermann /* 1061db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 1062db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 1063a7c7f2a7SJohn Baldwin * attempt or the completion of a previous one. 1064db33b3e6SAndre Oppermann */ 1065540e8b7eSJeffrey Hsu if (so->so_options & SO_ACCEPTCONN) { 1066540e8b7eSJeffrey Hsu struct in_conninfo inc; 1067540e8b7eSJeffrey Hsu 10687824d002SAndre Oppermann KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but " 10697824d002SAndre Oppermann "tp not listening", __func__)); 10708bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 1071302ce8d6SAndre Oppermann #ifdef INET6 1072be2ac88cSJonathan Lemon if (isipv6) { 1073dcdb4371SBjoern A. Zeeb inc.inc_flags |= INC_ISIPV6; 1074be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 1075be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 1076302ce8d6SAndre Oppermann } else 1077302ce8d6SAndre Oppermann #endif 1078302ce8d6SAndre Oppermann { 1079be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 1080be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 1081be2ac88cSJonathan Lemon } 1082be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 1083be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 10847973fba3SJulian Elischer inc.inc_fibnum = so->so_fibnum; 1085be2ac88cSJonathan Lemon 1086fb59c426SYoshinobu Inoue /* 10878d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 10888d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 10898d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 1090fb59c426SYoshinobu Inoue */ 1091be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 1092a7c7f2a7SJohn Baldwin 1093ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1094bf6d304aSAndre Oppermann /* 1095bf6d304aSAndre Oppermann * Parse the TCP options here because 1096bf6d304aSAndre Oppermann * syncookies need access to the reflected 1097bf6d304aSAndre Oppermann * timestamp. 1098bf6d304aSAndre Oppermann */ 1099bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 11009fa198beSAndre Oppermann /* 11019fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 11029fa198beSAndre Oppermann * inp and tcpinfo locks. 11039fa198beSAndre Oppermann */ 1104bf6d304aSAndre Oppermann if (!syncache_expand(&inc, &to, th, &so, m)) { 11054195b4afSPaul Traina /* 1106e207f800SAndre Oppermann * No syncache entry or ACK was not 1107be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 11088d573cc1SAndre Oppermann * NB: syncache did its own logging 11098d573cc1SAndre Oppermann * of the failure cause. 11104195b4afSPaul Traina */ 1111be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 1112be2ac88cSJonathan Lemon goto dropwithreset; 1113be2ac88cSJonathan Lemon } 1114f76fcf6dSJeffrey Hsu if (so == NULL) { 1115be2ac88cSJonathan Lemon /* 1116e207f800SAndre Oppermann * We completed the 3-way handshake 1117e207f800SAndre Oppermann * but could not allocate a socket 1118e207f800SAndre Oppermann * either due to memory shortage, 1119e207f800SAndre Oppermann * listen queue length limits or 1120a160e630SAndre Oppermann * global socket limits. Send RST 1121a160e630SAndre Oppermann * or wait and have the remote end 1122a160e630SAndre Oppermann * retransmit the ACK for another 1123a160e630SAndre Oppermann * try. 1124be2ac88cSJonathan Lemon */ 1125a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1126a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11278d573cc1SAndre Oppermann "Socket allocation failed due to " 11288d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 1129ac957cd2SJulian Elischer s, __func__, 1130ac957cd2SJulian Elischer V_tcp_sc_rst_sock_fail ? 1131ac957cd2SJulian Elischer "sending RST" : "try again"); 1132603724d3SBjoern A. Zeeb if (V_tcp_sc_rst_sock_fail) { 1133e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 1134e207f800SAndre Oppermann goto dropwithreset; 1135a160e630SAndre Oppermann } else 1136a160e630SAndre Oppermann goto dropunlock; 1137f76fcf6dSJeffrey Hsu } 1138be2ac88cSJonathan Lemon /* 1139be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 11408d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 11418d573cc1SAndre Oppermann * created socket and update the tp variable. 1142be2ac88cSJonathan Lemon */ 11438501a69cSRobert Watson INP_WUNLOCK(inp); /* listen socket */ 1144be2ac88cSJonathan Lemon inp = sotoinpcb(so); 1145ff9b006dSJulien Charbon /* 1146ff9b006dSJulien Charbon * New connection inpcb is already locked by 1147ff9b006dSJulien Charbon * syncache_expand(). 1148ff9b006dSJulien Charbon */ 1149ff9b006dSJulien Charbon INP_WLOCK_ASSERT(inp); 1150be2ac88cSJonathan Lemon tp = intotcpcb(inp); 11518d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 11528d573cc1SAndre Oppermann ("%s: ", __func__)); 11532903309aSAttilio Rao #ifdef TCP_SIGNATURE 11542903309aSAttilio Rao if (sig_checked == 0) { 11552903309aSAttilio Rao tcp_dooptions(&to, optp, optlen, 11562903309aSAttilio Rao (thflags & TH_SYN) ? TO_SYN : 0); 11572903309aSAttilio Rao if (!tcp_signature_verify_input(m, off0, tlen, 11582903309aSAttilio Rao optlen, &to, th, tp->t_flags)) { 11592903309aSAttilio Rao 11602903309aSAttilio Rao /* 11612903309aSAttilio Rao * In SYN_SENT state if it receives an 11622903309aSAttilio Rao * RST, it is allowed for further 11632903309aSAttilio Rao * processing. 11642903309aSAttilio Rao */ 11652903309aSAttilio Rao if ((thflags & TH_RST) == 0 || 11662903309aSAttilio Rao (tp->t_state == TCPS_SYN_SENT) == 0) 11672903309aSAttilio Rao goto dropunlock; 11682903309aSAttilio Rao } 11692903309aSAttilio Rao sig_checked = 1; 11702903309aSAttilio Rao } 11712903309aSAttilio Rao #endif 11722903309aSAttilio Rao 1173be2ac88cSJonathan Lemon /* 1174302ce8d6SAndre Oppermann * Process the segment and the data it 1175302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 1176302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 1177302ce8d6SAndre Oppermann */ 1178f2512ba1SRui Paulo tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 1179252ca428SRobert Watson iptos, ti_locked); 1180603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 11818f5a8818SKevin Lo return (IPPROTO_DONE); 1182be2ac88cSJonathan Lemon } 1183a160e630SAndre Oppermann /* 11848d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 11858d573cc1SAndre Oppermann * 1186a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 1187a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 1188a160e630SAndre Oppermann * retransmits. 118919bc77c5SAndre Oppermann * 119019bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 119119bc77c5SAndre Oppermann * causes. 1192a160e630SAndre Oppermann */ 1193a160e630SAndre Oppermann if (thflags & TH_RST) { 119419bc77c5SAndre Oppermann syncache_chkrst(&inc, th); 1195a160e630SAndre Oppermann goto dropunlock; 1196a160e630SAndre Oppermann } 1197a160e630SAndre Oppermann /* 1198a160e630SAndre Oppermann * We can't do anything without SYN. 1199a160e630SAndre Oppermann */ 1200a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 1201a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1202a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1203773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 12048d573cc1SAndre Oppermann s, __func__); 120578b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1206a160e630SAndre Oppermann goto dropunlock; 1207a160e630SAndre Oppermann } 1208a160e630SAndre Oppermann /* 1209a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 1210a160e630SAndre Oppermann */ 1211fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1212a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1213a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12148d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 12158d573cc1SAndre Oppermann s, __func__); 1216a160e630SAndre Oppermann syncache_badack(&inc); /* XXX: Not needed! */ 121778b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1218a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1219a57815efSBosko Milekic goto dropwithreset; 12204195b4afSPaul Traina } 1221a160e630SAndre Oppermann /* 1222a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 1223a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 1224a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 1225a160e630SAndre Oppermann * TCP/IP stack. 1226a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 1227a160e630SAndre Oppermann * and is constantly refining its stack detection 1228a160e630SAndre Oppermann * strategies. 12298d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 1230a160e630SAndre Oppermann * and was used by RFC1644. 1231a160e630SAndre Oppermann */ 1232603724d3SBjoern A. Zeeb if ((thflags & TH_FIN) && V_drop_synfin) { 1233a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1234a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1235773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 12368d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 123778b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1238302ce8d6SAndre Oppermann goto dropunlock; 1239ebb0cbeaSPaul Traina } 1240be2ac88cSJonathan Lemon /* 1241be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 1242a160e630SAndre Oppermann * 1243a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 1244a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 1245a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 1246be2ac88cSJonathan Lemon */ 1247a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 1248a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 1249a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 1250a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 125133841545SHajimu UMEMOTO #ifdef INET6 125233841545SHajimu UMEMOTO /* 125333841545SHajimu UMEMOTO * If deprecated address is forbidden, 125433841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 125533841545SHajimu UMEMOTO * address to prevent any new inbound connection from 125633841545SHajimu UMEMOTO * getting established. 125733841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 125833841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 125933841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 126033841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 126133841545SHajimu UMEMOTO * for the exchange. 126233841545SHajimu UMEMOTO * 126333841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 126433841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 126533841545SHajimu UMEMOTO * SYN in this case. 126633841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 126733841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 126833841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 126933841545SHajimu UMEMOTO * used" 127033841545SHajimu UMEMOTO * 2. use of it with new communication: 127133841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 127233841545SHajimu UMEMOTO * with sufficient scope is available" 127333841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 127433841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 127533841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 127633841545SHajimu UMEMOTO * 127733841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 127833841545SHajimu UMEMOTO * multiple description text for deprecated address 127933841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 128033841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 128133841545SHajimu UMEMOTO */ 1282603724d3SBjoern A. Zeeb if (isipv6 && !V_ip6_use_deprecated) { 128333841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 128433841545SHajimu UMEMOTO 12853e88eb90SAndrey V. Elsukov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */); 12868c0fec80SRobert Watson if (ia6 != NULL && 128733841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 12888c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 1289a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1290a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12918d573cc1SAndre Oppermann "Connection attempt to deprecated " 12928d573cc1SAndre Oppermann "IPv6 address rejected\n", 1293a160e630SAndre Oppermann s, __func__); 129433841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 129533841545SHajimu UMEMOTO goto dropwithreset; 129633841545SHajimu UMEMOTO } 129777d396fdSMaksim Yevmenkin if (ia6) 12988c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 129933841545SHajimu UMEMOTO } 1300b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 130165f28919SJesper Skriver /* 1302db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 13038d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 1304db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 13058d573cc1SAndre Oppermann * If it is from this socket it must be forged. 13068d573cc1SAndre Oppermann * Don't respond if the source or destination is a 13078d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 130893ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 130993ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 131093ec91baSCrist J. Clark * in_broadcast() to find them. 1311be2ac88cSJonathan Lemon */ 13128d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 13138d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 13148d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13158d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 1316773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 1317302ce8d6SAndre Oppermann goto dropunlock; 13188d573cc1SAndre Oppermann } 1319db33b3e6SAndre Oppermann #ifdef INET6 1320b287c6c7SBjoern A. Zeeb if (isipv6) { 1321db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1322a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 1323a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1324a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13258d573cc1SAndre Oppermann "Connection attempt to/from self " 1326773673c1SAndre Oppermann "ignored\n", s, __func__); 1327302ce8d6SAndre Oppermann goto dropunlock; 1328a160e630SAndre Oppermann } 1329be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1330a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 1331a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1332a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13338d573cc1SAndre Oppermann "Connection attempt from/to multicast " 1334773673c1SAndre Oppermann "address ignored\n", s, __func__); 1335302ce8d6SAndre Oppermann goto dropunlock; 1336a160e630SAndre Oppermann } 1337b287c6c7SBjoern A. Zeeb } 1338db33b3e6SAndre Oppermann #endif 1339b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 1340b287c6c7SBjoern A. Zeeb else 1341b287c6c7SBjoern A. Zeeb #endif 1342b287c6c7SBjoern A. Zeeb #ifdef INET 1343b287c6c7SBjoern A. Zeeb { 1344db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1345a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 1346a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1347a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13488d573cc1SAndre Oppermann "Connection attempt from/to self " 1349773673c1SAndre Oppermann "ignored\n", s, __func__); 1350302ce8d6SAndre Oppermann goto dropunlock; 1351a160e630SAndre Oppermann } 1352be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1353be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 13542ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 1355a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 1356a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1357a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13588d573cc1SAndre Oppermann "Connection attempt from/to broad- " 1359773673c1SAndre Oppermann "or multicast address ignored\n", 1360a160e630SAndre Oppermann s, __func__); 1361302ce8d6SAndre Oppermann goto dropunlock; 1362c068736aSJeffrey Hsu } 1363a160e630SAndre Oppermann } 1364b287c6c7SBjoern A. Zeeb #endif 1365be2ac88cSJonathan Lemon /* 1366db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 1367db33b3e6SAndre Oppermann * for syncache. 1368be2ac88cSJonathan Lemon */ 13693c653157SHartmut Brandt #ifdef TCPDEBUG 13703c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 13713c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 13723c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 13733c653157SHartmut Brandt #endif 13745d06879aSGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, mtod(m, const char *)); 1375f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 137609fe6320SNavdeep Parhar syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL); 1377be2ac88cSJonathan Lemon /* 13784d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 1379a7c7f2a7SJohn Baldwin * Only the listen socket is unlocked by syncache_add(). 1380be2ac88cSJonathan Lemon */ 1381ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 1382ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1383a7c7f2a7SJohn Baldwin ti_locked = TI_UNLOCKED; 1384a7c7f2a7SJohn Baldwin } 1385603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 13868f5a8818SKevin Lo return (IPPROTO_DONE); 1387982c1675SAndre Oppermann } else if (tp->t_state == TCPS_LISTEN) { 1388982c1675SAndre Oppermann /* 1389982c1675SAndre Oppermann * When a listen socket is torn down the SO_ACCEPTCONN 1390982c1675SAndre Oppermann * flag is removed first while connections are drained 1391982c1675SAndre Oppermann * from the accept queue in a unlock/lock cycle of the 1392982c1675SAndre Oppermann * ACCEPT_LOCK, opening a race condition allowing a SYN 1393982c1675SAndre Oppermann * attempt go through unhandled. 1394982c1675SAndre Oppermann */ 1395982c1675SAndre Oppermann goto dropunlock; 1396f76fcf6dSJeffrey Hsu } 1397db33b3e6SAndre Oppermann 13982903309aSAttilio Rao #ifdef TCP_SIGNATURE 13992903309aSAttilio Rao if (sig_checked == 0) { 14002903309aSAttilio Rao tcp_dooptions(&to, optp, optlen, 14012903309aSAttilio Rao (thflags & TH_SYN) ? TO_SYN : 0); 14022903309aSAttilio Rao if (!tcp_signature_verify_input(m, off0, tlen, optlen, &to, 14032903309aSAttilio Rao th, tp->t_flags)) { 14042903309aSAttilio Rao 14052903309aSAttilio Rao /* 14062903309aSAttilio Rao * In SYN_SENT state if it receives an RST, it is 14072903309aSAttilio Rao * allowed for further processing. 14082903309aSAttilio Rao */ 14092903309aSAttilio Rao if ((thflags & TH_RST) == 0 || 14102903309aSAttilio Rao (tp->t_state == TCPS_SYN_SENT) == 0) 14112903309aSAttilio Rao goto dropunlock; 14122903309aSAttilio Rao } 14132903309aSAttilio Rao sig_checked = 1; 14142903309aSAttilio Rao } 14152903309aSAttilio Rao #endif 14162903309aSAttilio Rao 1417fa22ce15SAdrian Chadd TCP_PROBE5(receive, NULL, tp, mtod(m, const char *), tp, th); 141857f60867SMark Johnston 1419302ce8d6SAndre Oppermann /* 14208d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 14218d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 14228d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 1423302ce8d6SAndre Oppermann */ 1424252ca428SRobert Watson tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked); 1425603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 14268f5a8818SKevin Lo return (IPPROTO_DONE); 1427be2ac88cSJonathan Lemon 1428302ce8d6SAndre Oppermann dropwithreset: 1429fa22ce15SAdrian Chadd TCP_PROBE5(receive, NULL, tp, mtod(m, const char *), tp, th); 143057f60867SMark Johnston 1431ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 1432ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1433252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1434fa046d87SRobert Watson } 1435fa046d87SRobert Watson #ifdef INVARIANTS 1436fa046d87SRobert Watson else { 1437fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset " 1438fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1439fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1440fa046d87SRobert Watson } 1441fa046d87SRobert Watson #endif 1442014ea782SRobert Watson 1443014ea782SRobert Watson if (inp != NULL) { 1444302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 1445014ea782SRobert Watson INP_WUNLOCK(inp); 1446dd8ac7f9SRobert Watson } else 1447014ea782SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1448302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 1449014ea782SRobert Watson goto drop; 1450014ea782SRobert Watson 1451302ce8d6SAndre Oppermann dropunlock: 145257f60867SMark Johnston if (m != NULL) 1453fa22ce15SAdrian Chadd TCP_PROBE5(receive, NULL, tp, mtod(m, const char *), tp, th); 145457f60867SMark Johnston 1455ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 1456ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1457252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1458fa046d87SRobert Watson } 1459fa046d87SRobert Watson #ifdef INVARIANTS 1460fa046d87SRobert Watson else { 1461fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock " 1462fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1463fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1464fa046d87SRobert Watson } 1465fa046d87SRobert Watson #endif 1466252ca428SRobert Watson 14679fa198beSAndre Oppermann if (inp != NULL) 14688501a69cSRobert Watson INP_WUNLOCK(inp); 1469014ea782SRobert Watson 1470302ce8d6SAndre Oppermann drop: 1471603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1472a160e630SAndre Oppermann if (s != NULL) 1473a160e630SAndre Oppermann free(s, M_TCPLOG); 1474302ce8d6SAndre Oppermann if (m != NULL) 1475302ce8d6SAndre Oppermann m_freem(m); 14768f5a8818SKevin Lo return (IPPROTO_DONE); 1477302ce8d6SAndre Oppermann } 1478302ce8d6SAndre Oppermann 1479679d9708SAndre Oppermann static void 1480302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 1481252ca428SRobert Watson struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos, 1482252ca428SRobert Watson int ti_locked) 1483302ce8d6SAndre Oppermann { 1484302ce8d6SAndre Oppermann int thflags, acked, ourfinisacked, needoutput = 0; 1485302ce8d6SAndre Oppermann int rstreason, todrop, win; 1486302ce8d6SAndre Oppermann u_long tiwin; 148707dacf03SAndre Oppermann char *s; 148807dacf03SAndre Oppermann struct in_conninfo *inc; 1489c11a15bfSGleb Smirnoff struct mbuf *mfree; 1490302ce8d6SAndre Oppermann struct tcpopt to; 1491302ce8d6SAndre Oppermann 149214739780SMaxim Konovalov #ifdef TCPDEBUG 149314739780SMaxim Konovalov /* 149414739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 149514739780SMaxim Konovalov * now IPv6. 149614739780SMaxim Konovalov */ 149714739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 149814739780SMaxim Konovalov struct tcphdr tcp_savetcp; 149914739780SMaxim Konovalov short ostate = 0; 150014739780SMaxim Konovalov #endif 1501302ce8d6SAndre Oppermann thflags = th->th_flags; 150207dacf03SAndre Oppermann inc = &tp->t_inpcb->inp_inc; 1503d64a46eaSLawrence Stewart tp->sackhint.last_sack_ack = 0; 1504302ce8d6SAndre Oppermann 1505252ca428SRobert Watson /* 1506252ca428SRobert Watson * If this is either a state-changing packet or current state isn't 1507252ca428SRobert Watson * established, we require a write lock on tcbinfo. Otherwise, we 15080989f56cSRobert Watson * allow the tcbinfo to be in either alocked or unlocked, as the 15090989f56cSRobert Watson * caller may have unnecessarily acquired a write lock due to a race. 1510252ca428SRobert Watson */ 1511252ca428SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 1512252ca428SRobert Watson tp->t_state != TCPS_ESTABLISHED) { 1513ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, ("%s ti_locked %d for " 1514252ca428SRobert Watson "SYN/FIN/RST/!EST", __func__, ti_locked)); 1515ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1516252ca428SRobert Watson } else { 1517252ca428SRobert Watson #ifdef INVARIANTS 1518ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 1519ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1520fa046d87SRobert Watson else { 1521fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST " 1522fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1523fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1524fa046d87SRobert Watson } 1525252ca428SRobert Watson #endif 1526252ca428SRobert Watson } 15278501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1528679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1529679d9708SAndre Oppermann __func__)); 1530679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1531679d9708SAndre Oppermann __func__)); 1532df8bae1dSRodney W. Grimes 153386a996e6SHiren Panchasara #ifdef TCPPCAP 153486a996e6SHiren Panchasara /* Save segment, if requested. */ 153586a996e6SHiren Panchasara tcp_pcap_add(th, m, &(tp->t_inpkts)); 153686a996e6SHiren Panchasara #endif 153786a996e6SHiren Panchasara 1538df8bae1dSRodney W. Grimes /* 1539df8bae1dSRodney W. Grimes * Segment received on connection. 1540df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1541e8949f74SAndre Oppermann * XXX: This should be done after segment 1542e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 1543df8bae1dSRodney W. Grimes */ 15449b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 15457ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 15469077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 1547df8bae1dSRodney W. Grimes 1548df8bae1dSRodney W. Grimes /* 1549c52102ddSHiren Panchasara * Scale up the window into a 32-bit value. 1550e8949f74SAndre Oppermann * For the SYN_SENT state the scale is zero. 1551464fcfbcSAndre Oppermann */ 1552464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 1553464fcfbcSAndre Oppermann 1554464fcfbcSAndre Oppermann /* 1555f2512ba1SRui Paulo * TCP ECN processing. 1556f2512ba1SRui Paulo */ 1557f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) { 15589c251892SRui Paulo if (thflags & TH_CWR) 15599c251892SRui Paulo tp->t_flags &= ~TF_ECN_SND_ECE; 1560f2512ba1SRui Paulo switch (iptos & IPTOS_ECN_MASK) { 1561f2512ba1SRui Paulo case IPTOS_ECN_CE: 1562f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_ECE; 156378b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ce); 1564f2512ba1SRui Paulo break; 1565f2512ba1SRui Paulo case IPTOS_ECN_ECT0: 156678b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect0); 1567f2512ba1SRui Paulo break; 1568f2512ba1SRui Paulo case IPTOS_ECN_ECT1: 156978b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect1); 1570f2512ba1SRui Paulo break; 1571f2512ba1SRui Paulo } 157264807b30SHiren Panchasara 157364807b30SHiren Panchasara /* Process a packet differently from RFC3168. */ 157464807b30SHiren Panchasara cc_ecnpkt_handler(tp, th, iptos); 157564807b30SHiren Panchasara 1576dbc42409SLawrence Stewart /* Congestion experienced. */ 1577dbc42409SLawrence Stewart if (thflags & TH_ECE) { 1578dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_ECN); 1579f2512ba1SRui Paulo } 1580f2512ba1SRui Paulo } 1581f2512ba1SRui Paulo 1582f2512ba1SRui Paulo /* 1583f72167f4SAndre Oppermann * Parse options on any incoming segment. 1584f72167f4SAndre Oppermann */ 1585302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1586302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1587302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1588f72167f4SAndre Oppermann 1589f72167f4SAndre Oppermann /* 1590f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1591bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1592bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1593bf6d304aSAndre Oppermann * was established. 1594f72167f4SAndre Oppermann */ 1595bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1596e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 1597d8951c8aSBjoern A. Zeeb if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) 1598f72167f4SAndre Oppermann to.to_tsecr = 0; 1599bf6d304aSAndre Oppermann } 160007dacf03SAndre Oppermann /* 160107dacf03SAndre Oppermann * If timestamps were negotiated during SYN/ACK they should 160207dacf03SAndre Oppermann * appear on every segment during this session and vice versa. 160307dacf03SAndre Oppermann */ 160407dacf03SAndre Oppermann if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) { 160507dacf03SAndre Oppermann if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 160607dacf03SAndre Oppermann log(LOG_DEBUG, "%s; %s: Timestamp missing, " 160707dacf03SAndre Oppermann "no action\n", s, __func__); 160807dacf03SAndre Oppermann free(s, M_TCPLOG); 160907dacf03SAndre Oppermann } 161007dacf03SAndre Oppermann } 161107dacf03SAndre Oppermann if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) { 161207dacf03SAndre Oppermann if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 161307dacf03SAndre Oppermann log(LOG_DEBUG, "%s; %s: Timestamp not expected, " 161407dacf03SAndre Oppermann "no action\n", s, __func__); 161507dacf03SAndre Oppermann free(s, M_TCPLOG); 161607dacf03SAndre Oppermann } 161707dacf03SAndre Oppermann } 1618f72167f4SAndre Oppermann 1619f72167f4SAndre Oppermann /* 162097d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 162197d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 16225396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 16235396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 162497d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1625df8bae1dSRodney W. Grimes */ 16260a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1627464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 1628464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 1629be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 163002a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 1631be2ac88cSJonathan Lemon } 16325396d0f8SAndre Oppermann /* 16335396d0f8SAndre Oppermann * Initial send window. It will be updated with 16345396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 16355396d0f8SAndre Oppermann */ 16365396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 1637be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1638be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1639be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1640d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1641be2ac88cSJonathan Lemon } 1642be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1643be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 16443529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 16453529149eSAndre Oppermann (to.to_flags & TOF_SACKPERM) == 0) 16463529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 16476d90faf3SPaul Saab } 16486d90faf3SPaul Saab 1649df8bae1dSRodney W. Grimes /* 1650df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1651df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1652df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1653df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1654df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1655df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1656df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1657df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1658df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1659df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1660df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1661df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1662a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1663c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 1664a0292f23SGarrett Wollman * be TH_NEEDSYN. 1665df8bae1dSRodney W. Grimes */ 1666df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1667c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 1668fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1669c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 1670c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 1671a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 16724741bfcbSPatrick Kelsey LIST_EMPTY(&tp->t_segq) && 16734741bfcbSPatrick Kelsey ((to.to_flags & TOF_TS) == 0 || 1674c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1675df8bae1dSRodney W. Grimes 1676df8bae1dSRodney W. Grimes /* 1677df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1678df8bae1dSRodney W. Grimes * record the timestamp. 1679a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1680a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1681df8bae1dSRodney W. Grimes */ 1682be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1683fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1684d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1685a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1686df8bae1dSRodney W. Grimes } 1687df8bae1dSRodney W. Grimes 1688fb59c426SYoshinobu Inoue if (tlen == 0) { 1689fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1690fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1691dbc42409SLawrence Stewart !IN_RECOVERY(tp->t_flags) && 1692fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1693dbc42409SLawrence Stewart TAILQ_EMPTY(&tp->snd_holes)) { 1694df8bae1dSRodney W. Grimes /* 1695e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1696df8bae1dSRodney W. Grimes */ 1697ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 1698ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1699252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1700252ca428SRobert Watson 170178b50714SRobert Watson TCPSTAT_INC(tcps_predack); 1702252ca428SRobert Watson 17039b8b58e0SJonathan Lemon /* 1704e8949f74SAndre Oppermann * "bad retransmit" recovery. 17059b8b58e0SJonathan Lemon */ 17069b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 1707672dc4aeSJohn Baldwin tp->t_flags & TF_PREVVALID && 17086dfb8b31SJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) { 1709dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 17109b8b58e0SJonathan Lemon } 1711fa55172bSMatthew Dillon 1712fa55172bSMatthew Dillon /* 1713fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1714fa55172bSMatthew Dillon * 1715fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1716fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1717fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1718fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1719fa55172bSMatthew Dillon */ 1720fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1721fa55172bSMatthew Dillon to.to_tsecr) { 1722d8951c8aSBjoern A. Zeeb u_int t; 1723d8951c8aSBjoern A. Zeeb 1724d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 1725d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 1726d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 1727a0292f23SGarrett Wollman tcp_xmit_timer(tp, 1728d8951c8aSBjoern A. Zeeb TCP_TS_TO_TICKS(t) + 1); 1729fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1730fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1731eaf80179SAndre Oppermann if (!tp->t_rttlow || 1732eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1733eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1734c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1735c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1736fa55172bSMatthew Dillon } 1737dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 173839bc9de5SLawrence Stewart 173939bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 174039bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 174139bc9de5SLawrence Stewart 174278b50714SRobert Watson TCPSTAT_INC(tcps_rcvackpack); 174378b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 1744df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 17459d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 17469d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 17479d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 1748dbc42409SLawrence Stewart 1749dbc42409SLawrence Stewart /* 1750dbc42409SLawrence Stewart * Let the congestion control algorithm update 1751dbc42409SLawrence Stewart * congestion control related information. This 1752dbc42409SLawrence Stewart * typically means increasing the congestion 1753dbc42409SLawrence Stewart * window. 1754dbc42409SLawrence Stewart */ 1755dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_ACK); 1756dbc42409SLawrence Stewart 17579d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 17581645d090SJeff Roberson /* 1759e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 17601645d090SJeff Roberson * to th_ack. 17611645d090SJeff Roberson */ 1762cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1763b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1764df8bae1dSRodney W. Grimes m_freem(m); 1765df8bae1dSRodney W. Grimes 1766df8bae1dSRodney W. Grimes /* 1767df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1768df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1769df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1770df8bae1dSRodney W. Grimes * If process is waiting for space, 1771df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1772df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1773df8bae1dSRodney W. Grimes * decide between more output or persist. 1774e8949f74SAndre Oppermann */ 17753c653157SHartmut Brandt #ifdef TCPDEBUG 17763c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 17773c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 17783c653157SHartmut Brandt (void *)tcp_saveipgen, 17793c653157SHartmut Brandt &tcp_savetcp, 0); 17803c653157SHartmut Brandt #endif 17815d06879aSGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, 17825d06879aSGeorge V. Neville-Neil mtod(m, const char *)); 1783df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1784b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1785b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1786b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1787b8152ba7SAndre Oppermann tp->t_rxtcur); 1788df8bae1dSRodney W. Grimes sowwakeup(so); 1789cfa6009eSGleb Smirnoff if (sbavail(&so->so_snd)) 1790df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1791a14c749fSJonathan Lemon goto check_delack; 1792df8bae1dSRodney W. Grimes } 1793fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1794fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 17956741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 17966741ecf5SAndre Oppermann 1797df8bae1dSRodney W. Grimes /* 1798252ca428SRobert Watson * This is a pure, in-sequence data packet with 1799252ca428SRobert Watson * nothing on the reassembly queue and we have enough 1800252ca428SRobert Watson * buffer space to take it. 1801df8bae1dSRodney W. Grimes */ 1802ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 1803ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1804252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1805252ca428SRobert Watson 18066d90faf3SPaul Saab /* Clean receiver SACK report if present */ 18073529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 18086d90faf3SPaul Saab tcp_clean_sackreport(tp); 180978b50714SRobert Watson TCPSTAT_INC(tcps_preddat); 1810fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 18111645d090SJeff Roberson /* 18121645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 18131645d090SJeff Roberson * th_seq. 18141645d090SJeff Roberson */ 181560ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 18161645d090SJeff Roberson /* 18171645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 18181645d090SJeff Roberson * rcv_nxt. 18191645d090SJeff Roberson */ 18201645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 182178b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 182278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 18233c653157SHartmut Brandt #ifdef TCPDEBUG 18243c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 18253c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 18263c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 18273c653157SHartmut Brandt #endif 18285d06879aSGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, mtod(m, const char *)); 18295d06879aSGeorge V. Neville-Neil 18306741ecf5SAndre Oppermann /* 18316741ecf5SAndre Oppermann * Automatic sizing of receive socket buffer. Often the send 18326741ecf5SAndre Oppermann * buffer size is not optimally adjusted to the actual network 18336741ecf5SAndre Oppermann * conditions at hand (delay bandwidth product). Setting the 18346741ecf5SAndre Oppermann * buffer size too small limits throughput on links with high 18356741ecf5SAndre Oppermann * bandwidth and high delay (eg. trans-continental/oceanic links). 18366741ecf5SAndre Oppermann * 18376741ecf5SAndre Oppermann * On the receive side the socket buffer memory is only rarely 18386741ecf5SAndre Oppermann * used to any significant extent. This allows us to be much 18396741ecf5SAndre Oppermann * more aggressive in scaling the receive socket buffer. For 18406741ecf5SAndre Oppermann * the case that the buffer space is actually used to a large 18416741ecf5SAndre Oppermann * extent and we run out of kernel memory we can simply drop 18426741ecf5SAndre Oppermann * the new segments; TCP on the sender will just retransmit it 18436741ecf5SAndre Oppermann * later. Setting the buffer size too big may only consume too 18446741ecf5SAndre Oppermann * much kernel memory if the application doesn't read() from 18456741ecf5SAndre Oppermann * the socket or packet loss or reordering makes use of the 18466741ecf5SAndre Oppermann * reassembly queue. 18476741ecf5SAndre Oppermann * 18486741ecf5SAndre Oppermann * The criteria to step up the receive buffer one notch are: 1849f7469d3eSHiren Panchasara * 1. Application has not set receive buffer size with 1850f7469d3eSHiren Panchasara * SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE. 1851f7469d3eSHiren Panchasara * 2. the number of bytes received during the time it takes 18526741ecf5SAndre Oppermann * one timestamp to be reflected back to us (the RTT); 1853f7469d3eSHiren Panchasara * 3. received bytes per RTT is within seven eighth of the 18546741ecf5SAndre Oppermann * current socket buffer size; 1855f7469d3eSHiren Panchasara * 4. receive buffer size has not hit maximal automatic size; 18566741ecf5SAndre Oppermann * 18576741ecf5SAndre Oppermann * This algorithm does one step per RTT at most and only if 18586741ecf5SAndre Oppermann * we receive a bulk stream w/o packet losses or reorderings. 18596741ecf5SAndre Oppermann * Shrinking the buffer during idle times is not necessary as 18606741ecf5SAndre Oppermann * it doesn't consume any memory when idle. 18616741ecf5SAndre Oppermann * 18626741ecf5SAndre Oppermann * TODO: Only step up if the application is actually serving 18636741ecf5SAndre Oppermann * the buffer to better manage the socket buffer resources. 1864df8bae1dSRodney W. Grimes */ 1865603724d3SBjoern A. Zeeb if (V_tcp_do_autorcvbuf && 1866d57724fdSPatrick Kelsey (to.to_flags & TOF_TS) && 18676741ecf5SAndre Oppermann to.to_tsecr && 18686741ecf5SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 18698502ec25SAndre Oppermann if (TSTMP_GT(to.to_tsecr, tp->rfbuf_ts) && 18706741ecf5SAndre Oppermann to.to_tsecr - tp->rfbuf_ts < hz) { 18716741ecf5SAndre Oppermann if (tp->rfbuf_cnt > 18726741ecf5SAndre Oppermann (so->so_rcv.sb_hiwat / 8 * 7) && 18736741ecf5SAndre Oppermann so->so_rcv.sb_hiwat < 1874603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max) { 18756741ecf5SAndre Oppermann newsize = 18766741ecf5SAndre Oppermann min(so->so_rcv.sb_hiwat + 1877603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_inc, 1878603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max); 18796741ecf5SAndre Oppermann } 18806741ecf5SAndre Oppermann /* Start over with next RTT. */ 18816741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 18826741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 18836741ecf5SAndre Oppermann } else 18846741ecf5SAndre Oppermann tp->rfbuf_cnt += tlen; /* add up */ 18856741ecf5SAndre Oppermann } 18866741ecf5SAndre Oppermann 18876741ecf5SAndre Oppermann /* Add data to socket buffer. */ 18881e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1889c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1890c1c36a2cSMike Silbersack m_freem(m); 1891c1c36a2cSMike Silbersack } else { 18926741ecf5SAndre Oppermann /* 18936741ecf5SAndre Oppermann * Set new socket buffer size. 18946741ecf5SAndre Oppermann * Give up when limit is reached. 18956741ecf5SAndre Oppermann */ 18966741ecf5SAndre Oppermann if (newsize) 18976741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 18986c8286e4SRobert Watson newsize, so, NULL)) 18996741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1900fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 1901651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 1902c1c36a2cSMike Silbersack } 1903e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 19041e4d7da7SRobert Watson sorwakeup_locked(so); 1905c1e5a6e5SAndre Oppermann if (DELAY_ACK(tp, tlen)) { 19063bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1907f498eeeeSDavid Greenman } else { 1908e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 1909e612a582SDavid Greenman tcp_output(tp); 1910e612a582SDavid Greenman } 1911a14c749fSJonathan Lemon goto check_delack; 1912df8bae1dSRodney W. Grimes } 1913df8bae1dSRodney W. Grimes } 1914df8bae1dSRodney W. Grimes 1915df8bae1dSRodney W. Grimes /* 1916df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1917df8bae1dSRodney W. Grimes * and then do TCP input processing. 1918df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1919df8bae1dSRodney W. Grimes * but not less than advertised window. 1920df8bae1dSRodney W. Grimes */ 1921df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1922df8bae1dSRodney W. Grimes if (win < 0) 1923df8bae1dSRodney W. Grimes win = 0; 192466e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1925df8bae1dSRodney W. Grimes 19266741ecf5SAndre Oppermann /* Reset receive buffer auto scaling when not in bulk receive mode. */ 19276741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 19286741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 19296741ecf5SAndre Oppermann 1930df8bae1dSRodney W. Grimes switch (tp->t_state) { 1931df8bae1dSRodney W. Grimes 1932df8bae1dSRodney W. Grimes /* 1933764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1934764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1935764d8cefSBill Fenner */ 1936764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1937fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1938fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1939a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1940a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1941a57815efSBosko Milekic goto dropwithreset; 1942a57815efSBosko Milekic } 1943764d8cefSBill Fenner break; 1944764d8cefSBill Fenner 1945764d8cefSBill Fenner /* 1946df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1947df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1948df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1949df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1950df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1951df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1952df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1953f2512ba1SRui Paulo * if seg contains an ECE and ECN support is enabled, the stream 1954f2512ba1SRui Paulo * is ECN capable. 1955df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1956df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1957df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1958df8bae1dSRodney W. Grimes */ 1959df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1960fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1961fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1962fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1963a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1964a0292f23SGarrett Wollman goto dropwithreset; 1965a0292f23SGarrett Wollman } 196657f60867SMark Johnston if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) { 1967d9fae5abSAndriy Gapon TCP_PROBE5(connect__refused, NULL, tp, 1968fa22ce15SAdrian Chadd mtod(m, const char *), tp, th); 1969df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 197057f60867SMark Johnston } 19710ca3f933SAndre Oppermann if (thflags & TH_RST) 1972df8bae1dSRodney W. Grimes goto drop; 19730ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 1974df8bae1dSRodney W. Grimes goto drop; 1975464fcfbcSAndre Oppermann 1976fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1977df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1978fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 197978b50714SRobert Watson TCPSTAT_INC(tcps_connects); 1980845799c1SAndras Olah soisconnected(so); 1981c488362eSRobert Watson #ifdef MAC 198230d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 1983c488362eSRobert Watson #endif 1984845799c1SAndras Olah /* Do window scaling on this connection? */ 1985845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1986845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1987845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1988845799c1SAndras Olah } 1989766282cbSJohn Baldwin tp->rcv_adv += imin(tp->rcv_wnd, 1990766282cbSJohn Baldwin TCP_MAXWIN << tp->rcv_scale); 1991a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1992a0292f23SGarrett Wollman /* 1993a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1994a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1995a0292f23SGarrett Wollman */ 1996c1e5a6e5SAndre Oppermann if (DELAY_ACK(tp, tlen) && tlen != 0) 1997b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 1998b8152ba7SAndre Oppermann tcp_delacktime); 1999a0292f23SGarrett Wollman else 2000a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2001f2512ba1SRui Paulo 2002603724d3SBjoern A. Zeeb if ((thflags & TH_ECE) && V_tcp_do_ecn) { 2003f2512ba1SRui Paulo tp->t_flags |= TF_ECN_PERMIT; 200478b50714SRobert Watson TCPSTAT_INC(tcps_ecn_shs); 2005f2512ba1SRui Paulo } 2006f2512ba1SRui Paulo 2007a0292f23SGarrett Wollman /* 2008a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 2009a0292f23SGarrett Wollman * Transitions: 2010a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 2011a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 2012a0292f23SGarrett Wollman */ 20139b8b58e0SJonathan Lemon tp->t_starttime = ticks; 2014a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 201557f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2016a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 2017fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 20187ff19458SPaul Traina } else { 201957f60867SMark Johnston tcp_state_change(tp, TCPS_ESTABLISHED); 2020d9fae5abSAndriy Gapon TCP_PROBE5(connect__established, NULL, tp, 2021fa22ce15SAdrian Chadd mtod(m, const char *), tp, th); 2022dbc42409SLawrence Stewart cc_conn_init(tp); 20239077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 20249077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 20257ff19458SPaul Traina } 2026a0292f23SGarrett Wollman } else { 2027a0292f23SGarrett Wollman /* 2028c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 2029153edc50SHiren Panchasara * simultaneous open. 2030c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 2031c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 2032a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 2033a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 2034a0292f23SGarrett Wollman */ 20354b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 2036b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 203757f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_RECEIVED); 2038a0292f23SGarrett Wollman } 2039df8bae1dSRodney W. Grimes 2040ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, ("%s: trimthenstep6: " 2041252ca428SRobert Watson "ti_locked %d", __func__, ti_locked)); 2042ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 20438501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 20447cfc6904SRobert Watson 2045df8bae1dSRodney W. Grimes /* 2046fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 2047df8bae1dSRodney W. Grimes * If data, trim to stay within window, 2048df8bae1dSRodney W. Grimes * dropping FIN if necessary. 2049df8bae1dSRodney W. Grimes */ 2050fb59c426SYoshinobu Inoue th->th_seq++; 2051fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 2052fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 2053df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2054fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 2055fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 205678b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 205778b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2058df8bae1dSRodney W. Grimes } 2059fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 2060fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 2061a0292f23SGarrett Wollman /* 2062a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 2063a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 2064a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 2065a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 2066a0292f23SGarrett Wollman * Otherwise, goto step 6. 2067a0292f23SGarrett Wollman */ 2068fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 2069a0292f23SGarrett Wollman goto process_ACK; 2070c068736aSJeffrey Hsu 2071df8bae1dSRodney W. Grimes goto step6; 2072c068736aSJeffrey Hsu 2073a0292f23SGarrett Wollman /* 2074a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 2075c94c54e4SAndre Oppermann * do normal processing. 2076a0292f23SGarrett Wollman * 2077c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 2078a0292f23SGarrett Wollman */ 2079a0292f23SGarrett Wollman case TCPS_LAST_ACK: 2080a0292f23SGarrett Wollman case TCPS_CLOSING: 2081a0292f23SGarrett Wollman break; /* continue normal processing */ 2082df8bae1dSRodney W. Grimes } 2083df8bae1dSRodney W. Grimes 2084df8bae1dSRodney W. Grimes /* 2085df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 208680ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 208780ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 208880ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 208980ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 209080ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 209180ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 209280ab7c0eSGarrett Wollman * killing a connection). 209380ab7c0eSGarrett Wollman * Then check timestamp, if present. 2094a0292f23SGarrett Wollman * Then check the connection count, if present. 2095df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 2096df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 2097df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 209880ab7c0eSGarrett Wollman */ 2099fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 21003220a212SGleb Smirnoff /* 21013220a212SGleb Smirnoff * RFC5961 Section 3.2 21023220a212SGleb Smirnoff * 21033220a212SGleb Smirnoff * - RST drops connection only if SEG.SEQ == RCV.NXT. 21043220a212SGleb Smirnoff * - If RST is in window, we send challenge ACK. 21053220a212SGleb Smirnoff * 21063220a212SGleb Smirnoff * Note: to take into account delayed ACKs, we should 21073220a212SGleb Smirnoff * test against last_ack_sent instead of rcv_nxt. 21083220a212SGleb Smirnoff * Note 2: we handle special case of closed window, not 21093220a212SGleb Smirnoff * covered by the RFC. 21103220a212SGleb Smirnoff */ 21113220a212SGleb Smirnoff if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 21123220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) || 21133220a212SGleb Smirnoff (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) { 211480ab7c0eSGarrett Wollman 2115ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2116ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, 21173220a212SGleb Smirnoff ("%s: TH_RST ti_locked %d, th %p tp %p", 21183220a212SGleb Smirnoff __func__, ti_locked, th, tp)); 21193220a212SGleb Smirnoff KASSERT(tp->t_state != TCPS_SYN_SENT, 21203220a212SGleb Smirnoff ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p", 21213220a212SGleb Smirnoff __func__, th, tp)); 21223220a212SGleb Smirnoff 21233220a212SGleb Smirnoff if (V_tcp_insecure_rst || 21243220a212SGleb Smirnoff tp->last_ack_sent == th->th_seq) { 21253220a212SGleb Smirnoff TCPSTAT_INC(tcps_drops); 21263220a212SGleb Smirnoff /* Drop the connection. */ 21273220a212SGleb Smirnoff switch (tp->t_state) { 212880ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 212980ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 213080ab7c0eSGarrett Wollman goto close; 213180ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 213280ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 213380ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 213480ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 213580ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 213680ab7c0eSGarrett Wollman close: 213757f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSED); 21383220a212SGleb Smirnoff /* FALLTHROUGH */ 21393220a212SGleb Smirnoff default: 214080ab7c0eSGarrett Wollman tp = tcp_close(tp); 21413220a212SGleb Smirnoff } 21423220a212SGleb Smirnoff } else { 21433220a212SGleb Smirnoff TCPSTAT_INC(tcps_badrst); 21443220a212SGleb Smirnoff /* Send challenge ACK. */ 21453220a212SGleb Smirnoff tcp_respond(tp, mtod(m, void *), th, m, 21463220a212SGleb Smirnoff tp->rcv_nxt, tp->snd_nxt, TH_ACK); 21473220a212SGleb Smirnoff tp->last_ack_sent = tp->rcv_nxt; 21483220a212SGleb Smirnoff m = NULL; 21493220a212SGleb Smirnoff } 21503220a212SGleb Smirnoff } 21513220a212SGleb Smirnoff goto drop; 21523220a212SGleb Smirnoff } 215380ab7c0eSGarrett Wollman 21543220a212SGleb Smirnoff /* 21553220a212SGleb Smirnoff * RFC5961 Section 4.2 21563220a212SGleb Smirnoff * Send challenge ACK for any SYN in synchronized state. 21573220a212SGleb Smirnoff */ 21583220a212SGleb Smirnoff if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT) { 2159ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, 21603220a212SGleb Smirnoff ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked)); 2161ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2162252ca428SRobert Watson 21633220a212SGleb Smirnoff TCPSTAT_INC(tcps_badsyn); 21643220a212SGleb Smirnoff if (V_tcp_insecure_syn && 21653220a212SGleb Smirnoff SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 21663220a212SGleb Smirnoff SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 21673220a212SGleb Smirnoff tp = tcp_drop(tp, ECONNRESET); 21683220a212SGleb Smirnoff rstreason = BANDLIM_UNLIMITED; 21693220a212SGleb Smirnoff } else { 21703220a212SGleb Smirnoff /* Send challenge ACK. */ 21713220a212SGleb Smirnoff tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt, 21723220a212SGleb Smirnoff tp->snd_nxt, TH_ACK); 21733220a212SGleb Smirnoff tp->last_ack_sent = tp->rcv_nxt; 21743220a212SGleb Smirnoff m = NULL; 217580ab7c0eSGarrett Wollman } 217680ab7c0eSGarrett Wollman goto drop; 217780ab7c0eSGarrett Wollman } 217880ab7c0eSGarrett Wollman 217980ab7c0eSGarrett Wollman /* 2180df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 2181df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 2182df8bae1dSRodney W. Grimes */ 2183be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 218480ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 2185df8bae1dSRodney W. Grimes 2186df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 2187d8951c8aSBjoern A. Zeeb if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 2188df8bae1dSRodney W. Grimes /* 2189df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 2190df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 2191df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 2192df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 2193df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 2194df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 2195df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 2196df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 2197df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 2198df8bae1dSRodney W. Grimes */ 2199df8bae1dSRodney W. Grimes tp->ts_recent = 0; 2200df8bae1dSRodney W. Grimes } else { 220178b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 220278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 220378b50714SRobert Watson TCPSTAT_INC(tcps_pawsdrop); 220407fd333dSMatthew Dillon if (tlen) 2205df8bae1dSRodney W. Grimes goto dropafterack; 22061ab4789dSMatthew Dillon goto drop; 22071ab4789dSMatthew Dillon } 2208df8bae1dSRodney W. Grimes } 2209df8bae1dSRodney W. Grimes 2210a0292f23SGarrett Wollman /* 221180ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 221280ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 221380ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 221480ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 221580ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 221680ab7c0eSGarrett Wollman */ 2217a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 2218a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2219a57815efSBosko Milekic goto dropwithreset; 2220a57815efSBosko Milekic } 222180ab7c0eSGarrett Wollman 2222fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 2223df8bae1dSRodney W. Grimes if (todrop > 0) { 2224831ad37eSXin LI if (thflags & TH_SYN) { 2225fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 2226fb59c426SYoshinobu Inoue th->th_seq++; 2227fb59c426SYoshinobu Inoue if (th->th_urp > 1) 2228fb59c426SYoshinobu Inoue th->th_urp--; 2229df8bae1dSRodney W. Grimes else 2230fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2231df8bae1dSRodney W. Grimes todrop--; 2232df8bae1dSRodney W. Grimes } 2233df8bae1dSRodney W. Grimes /* 2234dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 2235df8bae1dSRodney W. Grimes */ 2236fb59c426SYoshinobu Inoue if (todrop > tlen 2237fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 2238dac20301SGarrett Wollman /* 2239dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 2240dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 2241dac20301SGarrett Wollman * of sequence; drop it. 2242dac20301SGarrett Wollman */ 2243fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2244dac20301SGarrett Wollman 2245df8bae1dSRodney W. Grimes /* 2246dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 2247dac20301SGarrett Wollman * But keep on processing for RST or ACK. 2248df8bae1dSRodney W. Grimes */ 2249dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2250fb59c426SYoshinobu Inoue todrop = tlen; 225178b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 225278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 2253df8bae1dSRodney W. Grimes } else { 225478b50714SRobert Watson TCPSTAT_INC(tcps_rcvpartduppack); 225578b50714SRobert Watson TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 2256df8bae1dSRodney W. Grimes } 2257fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 2258fb59c426SYoshinobu Inoue th->th_seq += todrop; 2259fb59c426SYoshinobu Inoue tlen -= todrop; 2260fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 2261fb59c426SYoshinobu Inoue th->th_urp -= todrop; 2262df8bae1dSRodney W. Grimes else { 2263fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2264fb59c426SYoshinobu Inoue th->th_urp = 0; 2265df8bae1dSRodney W. Grimes } 2266df8bae1dSRodney W. Grimes } 2267df8bae1dSRodney W. Grimes 2268df8bae1dSRodney W. Grimes /* 2269df8bae1dSRodney W. Grimes * If new data are received on a connection after the 2270df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 2271df8bae1dSRodney W. Grimes */ 2272df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 2273fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 2274ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, ("%s: SS_NOFDEREF && " 2275252ca428SRobert Watson "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked)); 2276ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2277252ca428SRobert Watson 227807dacf03SAndre Oppermann if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 227907dacf03SAndre Oppermann log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data " 228007dacf03SAndre Oppermann "after socket was closed, " 228107dacf03SAndre Oppermann "sending RST and removing tcpcb\n", 2282e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 2283773673c1SAndre Oppermann free(s, M_TCPLOG); 2284773673c1SAndre Oppermann } 2285df8bae1dSRodney W. Grimes tp = tcp_close(tp); 228678b50714SRobert Watson TCPSTAT_INC(tcps_rcvafterclose); 2287a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2288df8bae1dSRodney W. Grimes goto dropwithreset; 2289df8bae1dSRodney W. Grimes } 2290df8bae1dSRodney W. Grimes 2291df8bae1dSRodney W. Grimes /* 2292df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 2293df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 2294df8bae1dSRodney W. Grimes */ 2295fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 2296df8bae1dSRodney W. Grimes if (todrop > 0) { 229778b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 2298fb59c426SYoshinobu Inoue if (todrop >= tlen) { 229978b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 2300df8bae1dSRodney W. Grimes /* 2301df8bae1dSRodney W. Grimes * If window is closed can only take segments at 2302df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 2303df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 2304df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 2305df8bae1dSRodney W. Grimes * and ack. 2306df8bae1dSRodney W. Grimes */ 2307fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2308df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 230978b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinprobe); 2310df8bae1dSRodney W. Grimes } else 2311df8bae1dSRodney W. Grimes goto dropafterack; 2312df8bae1dSRodney W. Grimes } else 231378b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2314df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2315fb59c426SYoshinobu Inoue tlen -= todrop; 2316fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 2317df8bae1dSRodney W. Grimes } 2318df8bae1dSRodney W. Grimes 2319df8bae1dSRodney W. Grimes /* 2320df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 2321df8bae1dSRodney W. Grimes * record its timestamp. 2322cf09195bSPaul Saab * NOTE: 2323cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 2324a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2325cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 2326cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 2327cf09195bSPaul Saab * predicated on the sequence space of this segment. 2328cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 2329cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 2330cf09195bSPaul Saab * instead of RFC1323's 2331cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 2332cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 2333cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 2334cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 2335cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 2336df8bae1dSRodney W. Grimes */ 2337be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 2338cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2339cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 2340cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 2341d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 2342a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 2343df8bae1dSRodney W. Grimes } 2344df8bae1dSRodney W. Grimes 2345df8bae1dSRodney W. Grimes /* 2346a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 2347a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 2348a0292f23SGarrett Wollman * later processing; else drop segment and return. 2349a0292f23SGarrett Wollman */ 2350fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 2351a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 2352a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 2353a0292f23SGarrett Wollman goto step6; 23545e1aa279SDavid Malone else if (tp->t_flags & TF_ACKNOW) 23555e1aa279SDavid Malone goto dropafterack; 2356a0292f23SGarrett Wollman else 2357a0292f23SGarrett Wollman goto drop; 2358a0292f23SGarrett Wollman } 2359df8bae1dSRodney W. Grimes 2360df8bae1dSRodney W. Grimes /* 2361df8bae1dSRodney W. Grimes * Ack processing. 2362df8bae1dSRodney W. Grimes */ 2363df8bae1dSRodney W. Grimes switch (tp->t_state) { 2364df8bae1dSRodney W. Grimes 2365df8bae1dSRodney W. Grimes /* 2366764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 2367764d8cefSBill Fenner * ESTABLISHED state and continue processing. 2368764d8cefSBill Fenner * The ACK was checked above. 2369df8bae1dSRodney W. Grimes */ 2370df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2371a0292f23SGarrett Wollman 237278b50714SRobert Watson TCPSTAT_INC(tcps_connects); 2373df8bae1dSRodney W. Grimes soisconnected(so); 2374df8bae1dSRodney W. Grimes /* Do window scaling? */ 2375df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2376df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2377df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 2378464fcfbcSAndre Oppermann tp->snd_wnd = tiwin; 2379df8bae1dSRodney W. Grimes } 2380a0292f23SGarrett Wollman /* 2381a0292f23SGarrett Wollman * Make transitions: 2382a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 2383a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 2384a0292f23SGarrett Wollman */ 23859b8b58e0SJonathan Lemon tp->t_starttime = ticks; 2386a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 238757f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2388a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 23897ff19458SPaul Traina } else { 239057f60867SMark Johnston tcp_state_change(tp, TCPS_ESTABLISHED); 2391d9fae5abSAndriy Gapon TCP_PROBE5(accept__established, NULL, tp, 2392fa22ce15SAdrian Chadd mtod(m, const char *), tp, th); 2393dbc42409SLawrence Stewart cc_conn_init(tp); 23949077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 23957ff19458SPaul Traina } 2396a0292f23SGarrett Wollman /* 2397a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 2398a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 2399a0292f23SGarrett Wollman */ 2400fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 2401fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 2402a0292f23SGarrett Wollman (struct mbuf *)0); 240360ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq - 1; 240493b0017fSPhilippe Charnier /* FALLTHROUGH */ 2405df8bae1dSRodney W. Grimes 2406df8bae1dSRodney W. Grimes /* 2407df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2408df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 2409fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 2410fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 2411df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 2412df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 2413df8bae1dSRodney W. Grimes */ 2414df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2415df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2416df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2417df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 2418df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2419df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 24205a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 242178b50714SRobert Watson TCPSTAT_INC(tcps_rcvacktoomuch); 24225a53ca16SPaul Saab goto dropafterack; 24235a53ca16SPaul Saab } 24243529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2425fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 2426fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 24275a53ca16SPaul Saab tcp_sack_doack(tp, &to, th->th_ack); 2428*12eeb81fSHiren Panchasara else 2429*12eeb81fSHiren Panchasara /* 2430*12eeb81fSHiren Panchasara * Reset the value so that previous (valid) value 2431*12eeb81fSHiren Panchasara * from the last ack with SACK doesn't get used. 2432*12eeb81fSHiren Panchasara */ 2433*12eeb81fSHiren Panchasara tp->sackhint.sacked_bytes = 0; 243439bc9de5SLawrence Stewart 243539bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 243639bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 243739bc9de5SLawrence Stewart 2438fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 243962b90589SPeter Wemm if (tlen == 0 && tiwin == tp->snd_wnd) { 244062b90589SPeter Wemm /* 244162b90589SPeter Wemm * If this is the first time we've seen a 244262b90589SPeter Wemm * FIN from the remote, this is not a 244362b90589SPeter Wemm * duplicate and it needs to be processed 244462b90589SPeter Wemm * normally. This happens during a 244562b90589SPeter Wemm * simultaneous close. 244662b90589SPeter Wemm */ 244762b90589SPeter Wemm if ((thflags & TH_FIN) && 244862b90589SPeter Wemm (TCPS_HAVERCVDFIN(tp->t_state) == 0)) { 244962b90589SPeter Wemm tp->t_dupacks = 0; 245062b90589SPeter Wemm break; 245162b90589SPeter Wemm } 245278b50714SRobert Watson TCPSTAT_INC(tcps_rcvdupack); 2453df8bae1dSRodney W. Grimes /* 2454df8bae1dSRodney W. Grimes * If we have outstanding data (other than 2455df8bae1dSRodney W. Grimes * a window probe), this is a completely 2456df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 24575f30ec9bSEitan Adler * change and FIN isn't set), 24585f30ec9bSEitan Adler * the ack is the biggest we've 2459df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 2460df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 2461df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 2462df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 2463df8bae1dSRodney W. Grimes * window so we send only this one 2464df8bae1dSRodney W. Grimes * packet. 2465df8bae1dSRodney W. Grimes * 2466df8bae1dSRodney W. Grimes * We know we're losing at the current 2467df8bae1dSRodney W. Grimes * window size so do congestion avoidance 2468df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 2469df8bae1dSRodney W. Grimes * and pull our congestion window back to 2470df8bae1dSRodney W. Grimes * the new ssthresh). 2471df8bae1dSRodney W. Grimes * 2472df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 2473df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 2474df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 2475df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 2476df8bae1dSRodney W. Grimes * network. 2477f2512ba1SRui Paulo * 2478f2512ba1SRui Paulo * When using TCP ECN, notify the peer that 2479f2512ba1SRui Paulo * we reduced the cwnd. 2480df8bae1dSRodney W. Grimes */ 2481b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_REXMT) || 2482fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 2483df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2484cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 2485dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2486dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_DUPACK); 24873529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2488dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2489808f11b7SPaul Saab int awnd; 249025e6f9edSPaul Saab 249125e6f9edSPaul Saab /* 249225e6f9edSPaul Saab * Compute the amount of data in flight first. 24934c3972f0SHiren Panchasara * We can inject new data into the pipe iff 249425e6f9edSPaul Saab * we have less than 1/2 the original window's 249525e6f9edSPaul Saab * worth of data in flight. 249625e6f9edSPaul Saab */ 2497*12eeb81fSHiren Panchasara if (V_tcp_do_rfc6675_pipe) 2498*12eeb81fSHiren Panchasara awnd = tcp_compute_pipe(tp); 2499*12eeb81fSHiren Panchasara else 2500808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 25010077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 2502*12eeb81fSHiren Panchasara 2503808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 250425e6f9edSPaul Saab tp->snd_cwnd += tp->t_maxseg; 250525e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 250625e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 250725e6f9edSPaul Saab } 250825e6f9edSPaul Saab } else 250946f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 251046f58482SJonathan Lemon (void) tcp_output(tp); 251146f58482SJonathan Lemon goto drop; 2512cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 2513cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 2514a0445c2eSJayanth Vijayaraghavan 2515a0445c2eSJayanth Vijayaraghavan /* 2516a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 2517a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 2518a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 2519a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 2520a0445c2eSJayanth Vijayaraghavan * recovery. 2521a0445c2eSJayanth Vijayaraghavan */ 25223529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 2523dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2524a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 2525a0445c2eSJayanth Vijayaraghavan break; 2526a0445c2eSJayanth Vijayaraghavan } 2527dbc42409SLawrence Stewart } else { 2528a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 25299d11646dSJeffrey Hsu tp->snd_recover)) { 2530cb942153SJeffrey Hsu tp->t_dupacks = 0; 2531cb942153SJeffrey Hsu break; 253246f58482SJonathan Lemon } 2533a0445c2eSJayanth Vijayaraghavan } 2534dbc42409SLawrence Stewart /* Congestion signal before ack. */ 2535dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_NDUPACK); 2536dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_DUPACK); 2537b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 25389b8b58e0SJonathan Lemon tp->t_rtttime = 0; 25393529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 254078b50714SRobert Watson TCPSTAT_INC( 254178b50714SRobert Watson tcps_sack_recovery_episode); 2542a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 25438db456bfSPaul Saab tp->snd_cwnd = tp->t_maxseg; 25446d90faf3SPaul Saab (void) tcp_output(tp); 25456d90faf3SPaul Saab goto drop; 25466d90faf3SPaul Saab } 2547fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 2548df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 2549df8bae1dSRodney W. Grimes (void) tcp_output(tp); 255048d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 2551302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 2552302ce8d6SAndre Oppermann __func__)); 2553df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 255448d2549cSJeffrey Hsu tp->t_maxseg * 255548d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 2556df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 2557df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 2558df8bae1dSRodney W. Grimes goto drop; 2559603724d3SBjoern A. Zeeb } else if (V_tcp_do_rfc3042) { 256062d4443fSHiren Panchasara /* 256162d4443fSHiren Panchasara * Process first and second duplicate 256262d4443fSHiren Panchasara * ACKs. Each indicates a segment 256362d4443fSHiren Panchasara * leaving the network, creating room 256462d4443fSHiren Panchasara * for more. Make sure we can send a 256562d4443fSHiren Panchasara * packet on reception of each duplicate 256662d4443fSHiren Panchasara * ACK by increasing snd_cwnd by one 256762d4443fSHiren Panchasara * segment. Restore the original 256862d4443fSHiren Panchasara * snd_cwnd after packet transmission. 256962d4443fSHiren Panchasara */ 2570dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_DUPACK); 2571582a954bSJeffrey Hsu u_long oldcwnd = tp->snd_cwnd; 257248d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 257348d2549cSJeffrey Hsu u_int sent; 25745628dd08SAndre Oppermann int avail; 257589c02376SJeffrey Hsu 2576582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 2577582a954bSJeffrey Hsu tp->t_dupacks == 2, 2578302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 2579302ce8d6SAndre Oppermann __func__)); 258061a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 258148d2549cSJeffrey Hsu tp->snd_limited = 0; 258261a36e3dSJeffrey Hsu tp->snd_cwnd = 258361a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 258461a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 258561a36e3dSJeffrey Hsu tp->t_maxseg; 25865628dd08SAndre Oppermann /* 25875628dd08SAndre Oppermann * Only call tcp_output when there 25885628dd08SAndre Oppermann * is new data available to be sent. 25895628dd08SAndre Oppermann * Otherwise we would send pure ACKs. 25905628dd08SAndre Oppermann */ 25915628dd08SAndre Oppermann SOCKBUF_LOCK(&so->so_snd); 2592cfa6009eSGleb Smirnoff avail = sbavail(&so->so_snd) - 25935628dd08SAndre Oppermann (tp->snd_nxt - tp->snd_una); 25945628dd08SAndre Oppermann SOCKBUF_UNLOCK(&so->so_snd); 25955628dd08SAndre Oppermann if (avail > 0) 2596582a954bSJeffrey Hsu (void) tcp_output(tp); 259748d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 259848d2549cSJeffrey Hsu if (sent > tp->t_maxseg) { 259989c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 260089c02376SJeffrey Hsu tp->snd_limited == 0) || 260189c02376SJeffrey Hsu (sent == tp->t_maxseg + 1 && 260289c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 2603302ce8d6SAndre Oppermann ("%s: sent too much", 2604302ce8d6SAndre Oppermann __func__)); 260548d2549cSJeffrey Hsu tp->snd_limited = 2; 260648d2549cSJeffrey Hsu } else if (sent > 0) 260748d2549cSJeffrey Hsu ++tp->snd_limited; 2608582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 2609582a954bSJeffrey Hsu goto drop; 2610df8bae1dSRodney W. Grimes } 2611df8bae1dSRodney W. Grimes } else 2612df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2613df8bae1dSRodney W. Grimes break; 2614df8bae1dSRodney W. Grimes } 2615cb942153SJeffrey Hsu 2616302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2617302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 2618cb942153SJeffrey Hsu 2619df8bae1dSRodney W. Grimes /* 2620df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2621df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2622df8bae1dSRodney W. Grimes */ 2623dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2624cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 26253529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 26266d90faf3SPaul Saab tcp_sack_partialack(tp, th); 26276d90faf3SPaul Saab else 2628c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2629dbc42409SLawrence Stewart } else 2630dbc42409SLawrence Stewart cc_post_recovery(tp, th); 263146f58482SJonathan Lemon } 2632cb942153SJeffrey Hsu tp->t_dupacks = 0; 2633a0292f23SGarrett Wollman /* 2634a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2635a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2636a0292f23SGarrett Wollman */ 2637a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2638a0292f23SGarrett Wollman /* 2639a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2640a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 264107e43e10SAndras Olah * synchronized). Go to non-starred state, 264207e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 264307e43e10SAndras Olah * we can do window scaling. 2644a0292f23SGarrett Wollman */ 2645a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2646a0292f23SGarrett Wollman tp->snd_una++; 264707e43e10SAndras Olah /* Do window scaling? */ 264807e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 264907e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 265007e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2651464fcfbcSAndre Oppermann /* Send window already scaled. */ 265207e43e10SAndras Olah } 2653a0292f23SGarrett Wollman } 2654a0292f23SGarrett Wollman 2655a0292f23SGarrett Wollman process_ACK: 26568501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 26577cfc6904SRobert Watson 2658dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 265978b50714SRobert Watson TCPSTAT_INC(tcps_rcvackpack); 266078b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 2661df8bae1dSRodney W. Grimes 2662df8bae1dSRodney W. Grimes /* 26639b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 26649b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 26659b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 26669b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 26679b8b58e0SJonathan Lemon * we left off. 26689b8b58e0SJonathan Lemon */ 2669672dc4aeSJohn Baldwin if (tp->t_rxtshift == 1 && tp->t_flags & TF_PREVVALID && 2670672dc4aeSJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) 2671dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 26729b8b58e0SJonathan Lemon 26739b8b58e0SJonathan Lemon /* 2674df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2675df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2676df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2677df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2678df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2679df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2680df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2681fa55172bSMatthew Dillon * 2682fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2683fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2684fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2685fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2686df8bae1dSRodney W. Grimes */ 2687d8951c8aSBjoern A. Zeeb if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) { 2688d8951c8aSBjoern A. Zeeb u_int t; 2689d8951c8aSBjoern A. Zeeb 2690d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 2691d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 2692d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 2693d8951c8aSBjoern A. Zeeb tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1); 2694fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2695eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2696eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 26979b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2698fa55172bSMatthew Dillon } 2699df8bae1dSRodney W. Grimes 2700df8bae1dSRodney W. Grimes /* 2701df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2702df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2703df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2704df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2705df8bae1dSRodney W. Grimes */ 2706fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 2707b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2708df8bae1dSRodney W. Grimes needoutput = 1; 2709b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2710b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2711a0292f23SGarrett Wollman 2712a0292f23SGarrett Wollman /* 2713a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2714a0292f23SGarrett Wollman * skip rest of ACK processing. 2715a0292f23SGarrett Wollman */ 2716a0292f23SGarrett Wollman if (acked == 0) 2717a0292f23SGarrett Wollman goto step6; 2718a0292f23SGarrett Wollman 2719df8bae1dSRodney W. Grimes /* 2720dbc42409SLawrence Stewart * Let the congestion control algorithm update congestion 2721dbc42409SLawrence Stewart * control related information. This typically means increasing 2722dbc42409SLawrence Stewart * the congestion window. 2723df8bae1dSRodney W. Grimes */ 2724dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_ACK); 2725dbc42409SLawrence Stewart 27265905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2727cfa6009eSGleb Smirnoff if (acked > sbavail(&so->so_snd)) { 2728cfa6009eSGleb Smirnoff tp->snd_wnd -= sbavail(&so->so_snd); 2729c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, 2730cfa6009eSGleb Smirnoff (int)sbavail(&so->so_snd)); 2731df8bae1dSRodney W. Grimes ourfinisacked = 1; 2732df8bae1dSRodney W. Grimes } else { 2733c11a15bfSGleb Smirnoff mfree = sbcut_locked(&so->so_snd, acked); 273460ee3bb2SAndre Oppermann tp->snd_wnd -= acked; 2735df8bae1dSRodney W. Grimes ourfinisacked = 0; 2736df8bae1dSRodney W. Grimes } 2737564aab1fSAndre Oppermann /* NB: sowwakeup_locked() does an implicit unlock. */ 27381e4d7da7SRobert Watson sowwakeup_locked(so); 2739c11a15bfSGleb Smirnoff m_freem(mfree); 2740e8949f74SAndre Oppermann /* Detect una wraparound. */ 2741dbc42409SLawrence Stewart if (!IN_RECOVERY(tp->t_flags) && 27429d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 27439d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 27449d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 2745dbc42409SLawrence Stewart /* XXXLAS: Can this be moved up into cc_post_recovery? */ 2746dbc42409SLawrence Stewart if (IN_RECOVERY(tp->t_flags) && 274724cb0f22SLawrence Stewart SEQ_GEQ(th->th_ack, tp->snd_recover)) { 2748dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 274924cb0f22SLawrence Stewart } 2750fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 27513529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 27526d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 27536d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 27546d90faf3SPaul Saab } 2755df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2756df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2757df8bae1dSRodney W. Grimes 2758df8bae1dSRodney W. Grimes switch (tp->t_state) { 2759df8bae1dSRodney W. Grimes 2760df8bae1dSRodney W. Grimes /* 2761df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2762df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2763df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2764df8bae1dSRodney W. Grimes */ 2765df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2766df8bae1dSRodney W. Grimes if (ourfinisacked) { 2767df8bae1dSRodney W. Grimes /* 2768df8bae1dSRodney W. Grimes * If we can't receive any more 2769df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2770df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2771df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2772df8bae1dSRodney W. Grimes * we'll hang forever. 2773e8949f74SAndre Oppermann * 2774e8949f74SAndre Oppermann * XXXjl: 2775340c35deSJonathan Lemon * we should release the tp also, and use a 2776340c35deSJonathan Lemon * compressed state. 2777340c35deSJonathan Lemon */ 2778c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 277903e49181SSeigo Tanimura soisdisconnected(so); 27809077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 27819077f387SGleb Smirnoff (tcp_fast_finwait2_recycle ? 27829077f387SGleb Smirnoff tcp_finwait2_timeout : 27839077f387SGleb Smirnoff TP_MAXIDLE(tp))); 27844cc20ab1SSeigo Tanimura } 278557f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_2); 2786df8bae1dSRodney W. Grimes } 2787df8bae1dSRodney W. Grimes break; 2788df8bae1dSRodney W. Grimes 2789df8bae1dSRodney W. Grimes /* 2790df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2791df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2792df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2793df8bae1dSRodney W. Grimes * the segment. 2794df8bae1dSRodney W. Grimes */ 2795df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2796df8bae1dSRodney W. Grimes if (ourfinisacked) { 2797ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 279811a20fb8SJeffrey Hsu tcp_twstart(tp); 2799ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 2800340c35deSJonathan Lemon m_freem(m); 2801679d9708SAndre Oppermann return; 2802df8bae1dSRodney W. Grimes } 2803df8bae1dSRodney W. Grimes break; 2804df8bae1dSRodney W. Grimes 2805df8bae1dSRodney W. Grimes /* 2806df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2807df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2808df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2809df8bae1dSRodney W. Grimes * enter the closed state and return. 2810df8bae1dSRodney W. Grimes */ 2811df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2812df8bae1dSRodney W. Grimes if (ourfinisacked) { 2813ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2814df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2815df8bae1dSRodney W. Grimes goto drop; 2816df8bae1dSRodney W. Grimes } 2817df8bae1dSRodney W. Grimes break; 2818df8bae1dSRodney W. Grimes } 2819df8bae1dSRodney W. Grimes } 2820df8bae1dSRodney W. Grimes 2821df8bae1dSRodney W. Grimes step6: 28228501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 28237cfc6904SRobert Watson 2824df8bae1dSRodney W. Grimes /* 282560ee3bb2SAndre Oppermann * Update window information. 282660ee3bb2SAndre Oppermann * Don't look at window if no ACK: TAC's send garbage on first SYN. 2827df8bae1dSRodney W. Grimes */ 282860ee3bb2SAndre Oppermann if ((thflags & TH_ACK) && 282960ee3bb2SAndre Oppermann (SEQ_LT(tp->snd_wl1, th->th_seq) || 283060ee3bb2SAndre Oppermann (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 283160ee3bb2SAndre Oppermann (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 283260ee3bb2SAndre Oppermann /* keep track of pure window updates */ 283360ee3bb2SAndre Oppermann if (tlen == 0 && 283460ee3bb2SAndre Oppermann tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 283578b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinupd); 2836df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 283760ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 283860ee3bb2SAndre Oppermann tp->snd_wl2 = th->th_ack; 2839df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2840df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 284160ee3bb2SAndre Oppermann needoutput = 1; 2842df8bae1dSRodney W. Grimes } 2843df8bae1dSRodney W. Grimes 2844df8bae1dSRodney W. Grimes /* 2845df8bae1dSRodney W. Grimes * Process segments with URG. 2846df8bae1dSRodney W. Grimes */ 2847fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2848df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2849df8bae1dSRodney W. Grimes /* 2850df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2851df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2852df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2853df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2854df8bae1dSRodney W. Grimes */ 285518ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2856cfa6009eSGleb Smirnoff if (th->th_urp + sbavail(&so->so_rcv) > sb_max) { 2857fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2858fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 285918ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2860df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2861df8bae1dSRodney W. Grimes } 2862df8bae1dSRodney W. Grimes /* 2863df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2864df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2865df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2866df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2867df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2868df8bae1dSRodney W. Grimes * 2869df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2870df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2871df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2872df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2873df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2874df8bae1dSRodney W. Grimes * spec states (in one of two places). 2875df8bae1dSRodney W. Grimes */ 2876fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2877fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2878cfa6009eSGleb Smirnoff so->so_oobmark = sbavail(&so->so_rcv) + 2879df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2880927c5ceaSRobert Watson if (so->so_oobmark == 0) 2881c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 2882df8bae1dSRodney W. Grimes sohasoutofband(so); 2883df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2884df8bae1dSRodney W. Grimes } 288518ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2886df8bae1dSRodney W. Grimes /* 2887df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2888df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2889df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2890df8bae1dSRodney W. Grimes * data may creep in... ick. 2891df8bae1dSRodney W. Grimes */ 289230613f56SJeffrey Hsu if (th->th_urp <= (u_long)tlen && 289330613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 289430613f56SJeffrey Hsu /* hdr drop is delayed */ 289530613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 289630613f56SJeffrey Hsu } 2897c068736aSJeffrey Hsu } else { 2898df8bae1dSRodney W. Grimes /* 2899df8bae1dSRodney W. Grimes * If no out of band data is expected, 2900df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2901df8bae1dSRodney W. Grimes * with the receive window. 2902df8bae1dSRodney W. Grimes */ 2903df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2904df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2905c068736aSJeffrey Hsu } 2906df8bae1dSRodney W. Grimes dodata: /* XXX */ 29078501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 29087cfc6904SRobert Watson 2909df8bae1dSRodney W. Grimes /* 2910df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2911df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2912df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2913df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2914df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2915df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2916df8bae1dSRodney W. Grimes */ 2917fb59c426SYoshinobu Inoue if ((tlen || (thflags & TH_FIN)) && 2918df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 291969e03620SPaul Saab tcp_seq save_start = th->th_seq; 2920fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2921e4b64281SJesper Skriver /* 2922c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2923c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2924c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2925c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2926c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2927c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2928c068736aSJeffrey Hsu * conversions. 2929c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 2930c068736aSJeffrey Hsu * immediately when segments are out of order (so 2931c068736aSJeffrey Hsu * fast retransmit can work). 2932e4b64281SJesper Skriver */ 29334741bfcbSPatrick Kelsey if (th->th_seq == tp->rcv_nxt && 29344741bfcbSPatrick Kelsey LIST_EMPTY(&tp->t_segq) && 2935e4b64281SJesper Skriver TCPS_HAVEESTABLISHED(tp->t_state)) { 2936c1e5a6e5SAndre Oppermann if (DELAY_ACK(tp, tlen)) 29373bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2938e4b64281SJesper Skriver else 2939e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2940e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2941e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 294278b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 294378b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 29441e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2945c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2946c1c36a2cSMike Silbersack m_freem(m); 2947c1c36a2cSMike Silbersack else 2948651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_rcv, m, 0); 2949e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 29501e4d7da7SRobert Watson sorwakeup_locked(so); 2951e4b64281SJesper Skriver } else { 2952e8949f74SAndre Oppermann /* 2953e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 2954e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 2955e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 2956e8949f74SAndre Oppermann * when trimming from the head. 2957e8949f74SAndre Oppermann */ 2958e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 2959e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2960e4b64281SJesper Skriver } 29613529149eSAndre Oppermann if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 2962f194524fSAndre Oppermann tcp_update_sack_list(tp, save_start, save_start + tlen); 2963302ce8d6SAndre Oppermann #if 0 2964df8bae1dSRodney W. Grimes /* 2965df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2966df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2967df8bae1dSRodney W. Grimes * buffer size. 2968302ce8d6SAndre Oppermann * XXX: Unused. 2969df8bae1dSRodney W. Grimes */ 2970f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) 2971df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2972f701e30dSJohn Baldwin else 2973f701e30dSJohn Baldwin len = so->so_rcv.sb_hiwat; 2974302ce8d6SAndre Oppermann #endif 2975df8bae1dSRodney W. Grimes } else { 2976df8bae1dSRodney W. Grimes m_freem(m); 2977fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2978df8bae1dSRodney W. Grimes } 2979df8bae1dSRodney W. Grimes 2980df8bae1dSRodney W. Grimes /* 2981df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2982df8bae1dSRodney W. Grimes * that the connection is closing. 2983df8bae1dSRodney W. Grimes */ 2984fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2985df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2986df8bae1dSRodney W. Grimes socantrcvmore(so); 2987a0292f23SGarrett Wollman /* 2988a0292f23SGarrett Wollman * If connection is half-synchronized 2989d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2990a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2991a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2992a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2993a0292f23SGarrett Wollman */ 29943bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 29953bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2996a0292f23SGarrett Wollman else 2997df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2998df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2999df8bae1dSRodney W. Grimes } 3000df8bae1dSRodney W. Grimes switch (tp->t_state) { 3001df8bae1dSRodney W. Grimes 3002df8bae1dSRodney W. Grimes /* 3003df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 3004df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 3005df8bae1dSRodney W. Grimes */ 3006df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 30079b8b58e0SJonathan Lemon tp->t_starttime = ticks; 30089b8b58e0SJonathan Lemon /* FALLTHROUGH */ 3009df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 301057f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSE_WAIT); 3011df8bae1dSRodney W. Grimes break; 3012df8bae1dSRodney W. Grimes 3013df8bae1dSRodney W. Grimes /* 3014df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 3015df8bae1dSRodney W. Grimes * enter the CLOSING state. 3016df8bae1dSRodney W. Grimes */ 3017df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 301857f60867SMark Johnston tcp_state_change(tp, TCPS_CLOSING); 3019df8bae1dSRodney W. Grimes break; 3020df8bae1dSRodney W. Grimes 3021df8bae1dSRodney W. Grimes /* 3022df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 3023df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 3024df8bae1dSRodney W. Grimes * standard timers. 3025df8bae1dSRodney W. Grimes */ 3026df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 3027ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 3028ff9b006dSJulien Charbon KASSERT(ti_locked == TI_RLOCKED, ("%s: dodata " 3029252ca428SRobert Watson "TCP_FIN_WAIT_2 ti_locked: %d", __func__, 3030252ca428SRobert Watson ti_locked)); 3031252ca428SRobert Watson 3032340c35deSJonathan Lemon tcp_twstart(tp); 3033ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 3034679d9708SAndre Oppermann return; 3035df8bae1dSRodney W. Grimes } 3036df8bae1dSRodney W. Grimes } 3037ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 3038ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 3039252ca428SRobert Watson ti_locked = TI_UNLOCKED; 3040252ca428SRobert Watson 3041610ee2f9SDavid Greenman #ifdef TCPDEBUG 30424cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3043fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 3044fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3045610ee2f9SDavid Greenman #endif 30465d06879aSGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, mtod(m, const char *)); 3047df8bae1dSRodney W. Grimes 3048df8bae1dSRodney W. Grimes /* 3049df8bae1dSRodney W. Grimes * Return any desired output. 3050df8bae1dSRodney W. Grimes */ 3051df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 3052df8bae1dSRodney W. Grimes (void) tcp_output(tp); 30537792ea27SJeffrey Hsu 3054a14c749fSJonathan Lemon check_delack: 3055252ca428SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 3056252ca428SRobert Watson __func__, ti_locked)); 3057603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 30588501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 3059252ca428SRobert Watson 3060a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 30613bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 3062b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 30633bfd6421SJonathan Lemon } 30648501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3065679d9708SAndre Oppermann return; 3066df8bae1dSRodney W. Grimes 3067df8bae1dSRodney W. Grimes dropafterack: 3068df8bae1dSRodney W. Grimes /* 3069df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 3070df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 307180ab7c0eSGarrett Wollman * 307280ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 307380ab7c0eSGarrett Wollman * paths to this code happen after packets containing 307480ab7c0eSGarrett Wollman * RST have been dropped. 307580ab7c0eSGarrett Wollman * 307680ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 307780ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 307880ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 307980ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 308080ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 308180ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 3082df8bae1dSRodney W. Grimes */ 3083fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 3084fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 3085a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 3086a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 3087a57815efSBosko Milekic goto dropwithreset; 3088a57815efSBosko Milekic } 3089a0292f23SGarrett Wollman #ifdef TCPDEBUG 30904cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3091fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3092fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3093a0292f23SGarrett Wollman #endif 30945d06879aSGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, mtod(m, const char *)); 3095ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 3096ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 3097252ca428SRobert Watson ti_locked = TI_UNLOCKED; 3098252ca428SRobert Watson 3099df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 3100df8bae1dSRodney W. Grimes (void) tcp_output(tp); 31018501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3102d6915262SRobert Watson m_freem(m); 3103679d9708SAndre Oppermann return; 3104df8bae1dSRodney W. Grimes 3105df8bae1dSRodney W. Grimes dropwithreset: 3106ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) 3107ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 3108252ca428SRobert Watson ti_locked = TI_UNLOCKED; 3109a57815efSBosko Milekic 3110a0ca0871SRobert Watson if (tp != NULL) { 3111302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 31128501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3113dd8ac7f9SRobert Watson } else 3114a0ca0871SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 3115679d9708SAndre Oppermann return; 3116df8bae1dSRodney W. Grimes 3117df8bae1dSRodney W. Grimes drop: 3118ff9b006dSJulien Charbon if (ti_locked == TI_RLOCKED) { 3119ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 3120fa046d87SRobert Watson ti_locked = TI_UNLOCKED; 3121fa046d87SRobert Watson } 3122252ca428SRobert Watson #ifdef INVARIANTS 3123252ca428SRobert Watson else 3124252ca428SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 3125252ca428SRobert Watson #endif 3126252ca428SRobert Watson 3127df8bae1dSRodney W. Grimes /* 3128df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 3129df8bae1dSRodney W. Grimes */ 3130610ee2f9SDavid Greenman #ifdef TCPDEBUG 31311c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 3132fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3133fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3134a0292f23SGarrett Wollman #endif 31355d06879aSGeorge V. Neville-Neil TCP_PROBE3(debug__input, tp, th, mtod(m, const char *)); 31361c53f806SRobert Watson if (tp != NULL) 31378501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3138d6915262SRobert Watson m_freem(m); 3139302ce8d6SAndre Oppermann } 3140302ce8d6SAndre Oppermann 3141302ce8d6SAndre Oppermann /* 31420ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 31430ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 31440ca3f933SAndre Oppermann * tp may be NULL. 3145302ce8d6SAndre Oppermann */ 3146302ce8d6SAndre Oppermann static void 3147302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 3148302ce8d6SAndre Oppermann int tlen, int rstreason) 3149302ce8d6SAndre Oppermann { 3150b287c6c7SBjoern A. Zeeb #ifdef INET 3151302ce8d6SAndre Oppermann struct ip *ip; 3152b287c6c7SBjoern A. Zeeb #endif 3153302ce8d6SAndre Oppermann #ifdef INET6 3154302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 3155302ce8d6SAndre Oppermann #endif 31567a3244ccSRobert Watson 31577a3244ccSRobert Watson if (tp != NULL) { 31588501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 31597a3244ccSRobert Watson } 31607a3244ccSRobert Watson 31610ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 3162302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 3163302ce8d6SAndre Oppermann goto drop; 3164302ce8d6SAndre Oppermann #ifdef INET6 3165302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 3166302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 3167302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3168302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3169302ce8d6SAndre Oppermann goto drop; 3170302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 3171b287c6c7SBjoern A. Zeeb } 3172302ce8d6SAndre Oppermann #endif 3173b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3174b287c6c7SBjoern A. Zeeb else 3175b287c6c7SBjoern A. Zeeb #endif 3176b287c6c7SBjoern A. Zeeb #ifdef INET 3177302ce8d6SAndre Oppermann { 3178302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 3179302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3180302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3181302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3182302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3183302ce8d6SAndre Oppermann goto drop; 3184302ce8d6SAndre Oppermann } 3185b287c6c7SBjoern A. Zeeb #endif 3186302ce8d6SAndre Oppermann 3187302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 3188302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 3189302ce8d6SAndre Oppermann goto drop; 3190302ce8d6SAndre Oppermann 3191302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 3192302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 3193302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 3194302ce8d6SAndre Oppermann th->th_ack, TH_RST); 3195302ce8d6SAndre Oppermann } else { 3196302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 3197302ce8d6SAndre Oppermann tlen++; 3198302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 3199302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 3200302ce8d6SAndre Oppermann } 3201302ce8d6SAndre Oppermann return; 3202302ce8d6SAndre Oppermann drop: 3203302ce8d6SAndre Oppermann m_freem(m); 3204df8bae1dSRodney W. Grimes } 3205df8bae1dSRodney W. Grimes 3206be2ac88cSJonathan Lemon /* 3207be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 3208be2ac88cSJonathan Lemon */ 32090312fbe9SPoul-Henning Kamp static void 3210ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 3211df8bae1dSRodney W. Grimes { 3212df8bae1dSRodney W. Grimes int opt, optlen; 3213df8bae1dSRodney W. Grimes 3214be2ac88cSJonathan Lemon to->to_flags = 0; 3215df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 3216df8bae1dSRodney W. Grimes opt = cp[0]; 3217df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 3218df8bae1dSRodney W. Grimes break; 3219df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 3220df8bae1dSRodney W. Grimes optlen = 1; 3221df8bae1dSRodney W. Grimes else { 3222b474779fSJun-ichiro itojun Hagino if (cnt < 2) 3223b474779fSJun-ichiro itojun Hagino break; 3224df8bae1dSRodney W. Grimes optlen = cp[1]; 3225b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 3226df8bae1dSRodney W. Grimes break; 3227df8bae1dSRodney W. Grimes } 3228df8bae1dSRodney W. Grimes switch (opt) { 3229df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 3230df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 3231df8bae1dSRodney W. Grimes continue; 3232f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3233df8bae1dSRodney W. Grimes continue; 3234be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 3235be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 3236be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 3237fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 3238df8bae1dSRodney W. Grimes break; 3239df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 3240df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 3241df8bae1dSRodney W. Grimes continue; 3242f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3243df8bae1dSRodney W. Grimes continue; 3244be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 324502a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 3246df8bae1dSRodney W. Grimes break; 3247df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 3248df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 3249df8bae1dSRodney W. Grimes continue; 3250be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 3251a0292f23SGarrett Wollman bcopy((char *)cp + 2, 3252a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 3253fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 3254a0292f23SGarrett Wollman bcopy((char *)cp + 6, 3255a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 3256fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 3257df8bae1dSRodney W. Grimes break; 32581cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 32591cfd4b53SBruce M Simpson /* 32601cfd4b53SBruce M Simpson * XXX In order to reply to a host which has set the 32611cfd4b53SBruce M Simpson * TCP_SIGNATURE option in its initial SYN, we have to 32621cfd4b53SBruce M Simpson * record the fact that the option was observed here 32631cfd4b53SBruce M Simpson * for the syncache code to perform the correct response. 32641cfd4b53SBruce M Simpson */ 32651cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 32661cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 32671cfd4b53SBruce M Simpson continue; 3268df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 3269df47e437SAndre Oppermann to->to_signature = cp + 2; 32701cfd4b53SBruce M Simpson break; 3271265ed012SBruce M Simpson #endif 32726d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 3273f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 32746d90faf3SPaul Saab continue; 3275f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3276f72167f4SAndre Oppermann continue; 3277603724d3SBjoern A. Zeeb if (!V_tcp_do_sack) 3278f72167f4SAndre Oppermann continue; 3279fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 32806d90faf3SPaul Saab break; 32816d90faf3SPaul Saab case TCPOPT_SACK: 32825a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 32836d90faf3SPaul Saab continue; 3284b728e902SAndre Oppermann if (flags & TO_SYN) 3285b728e902SAndre Oppermann continue; 3286fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 32875a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 32885a53ca16SPaul Saab to->to_sacks = cp + 2; 328978b50714SRobert Watson TCPSTAT_INC(tcps_sack_rcv_blocks); 32906d90faf3SPaul Saab break; 3291be2ac88cSJonathan Lemon default: 3292be2ac88cSJonathan Lemon continue; 3293df8bae1dSRodney W. Grimes } 3294df8bae1dSRodney W. Grimes } 3295df8bae1dSRodney W. Grimes } 3296df8bae1dSRodney W. Grimes 3297df8bae1dSRodney W. Grimes /* 3298df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 3299df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 3300df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 3301df8bae1dSRodney W. Grimes * sequencing purposes. 3302df8bae1dSRodney W. Grimes */ 33030312fbe9SPoul-Henning Kamp static void 3304ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 3305ad3f9ab3SAndre Oppermann int off) 3306df8bae1dSRodney W. Grimes { 3307fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 3308df8bae1dSRodney W. Grimes 3309df8bae1dSRodney W. Grimes while (cnt >= 0) { 3310df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 3311df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 3312df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 3313df8bae1dSRodney W. Grimes 33148501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 33157a3244ccSRobert Watson 3316df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 3317df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 3318df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 3319df8bae1dSRodney W. Grimes m->m_len--; 332069a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 332169a34685SYoshinobu Inoue m->m_pkthdr.len--; 3322df8bae1dSRodney W. Grimes return; 3323df8bae1dSRodney W. Grimes } 3324df8bae1dSRodney W. Grimes cnt -= m->m_len; 3325df8bae1dSRodney W. Grimes m = m->m_next; 33264dfdffe9SAndre Oppermann if (m == NULL) 3327df8bae1dSRodney W. Grimes break; 3328df8bae1dSRodney W. Grimes } 3329df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 3330df8bae1dSRodney W. Grimes } 3331df8bae1dSRodney W. Grimes 3332df8bae1dSRodney W. Grimes /* 3333df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 3334df8bae1dSRodney W. Grimes * and update averages and current timeout. 3335df8bae1dSRodney W. Grimes */ 33360312fbe9SPoul-Henning Kamp static void 3337ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 3338df8bae1dSRodney W. Grimes { 3339ad3f9ab3SAndre Oppermann int delta; 3340233e8c18SGarrett Wollman 33418501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 33422be3bf22SRobert Watson 334378b50714SRobert Watson TCPSTAT_INC(tcps_rttupdated); 3344233e8c18SGarrett Wollman tp->t_rttupdated++; 3345233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 3346233e8c18SGarrett Wollman /* 3347233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 3348233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 3349233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 3350233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3351233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 3352233e8c18SGarrett Wollman */ 3353233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3354233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3355233e8c18SGarrett Wollman 3356233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 3357233e8c18SGarrett Wollman tp->t_srtt = 1; 3358233e8c18SGarrett Wollman 3359233e8c18SGarrett Wollman /* 3360233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 3361233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 3362233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 3363233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 3364233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 3365233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 3366233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3367233e8c18SGarrett Wollman * rfc793's wired-in beta. 3368233e8c18SGarrett Wollman */ 3369233e8c18SGarrett Wollman if (delta < 0) 3370233e8c18SGarrett Wollman delta = -delta; 3371233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3372233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 3373233e8c18SGarrett Wollman tp->t_rttvar = 1; 33741fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 33751fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3376233e8c18SGarrett Wollman } else { 3377233e8c18SGarrett Wollman /* 3378233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 3379233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 3380233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 3381233e8c18SGarrett Wollman */ 3382233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 3383233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 33841fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3385233e8c18SGarrett Wollman } 33869b8b58e0SJonathan Lemon tp->t_rtttime = 0; 3387df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 3388df8bae1dSRodney W. Grimes 3389df8bae1dSRodney W. Grimes /* 3390df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 3391df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 3392df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 3393df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 3394df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 3395df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 3396df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 3397df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 3398df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 3399df8bae1dSRodney W. Grimes */ 3400233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 34019e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3402df8bae1dSRodney W. Grimes 3403df8bae1dSRodney W. Grimes /* 3404df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 3405df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 3406df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 3407df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 3408df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 3409df8bae1dSRodney W. Grimes */ 3410df8bae1dSRodney W. Grimes tp->t_softerror = 0; 3411df8bae1dSRodney W. Grimes } 3412df8bae1dSRodney W. Grimes 3413df8bae1dSRodney W. Grimes /* 3414df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 3415df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 34164faaea55SAndre Oppermann * If none, use an mss that can be handled on the outgoing interface 34174faaea55SAndre Oppermann * without forcing IP to fragment. If no route is found, route has no mtu, 3418df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 3419df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 3420df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 3421df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 3422df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 3423df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 3424df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 3425a0292f23SGarrett Wollman * 3426a0292f23SGarrett Wollman * Also take into account the space needed for options that we 3427a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 3428a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 3429a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 3430a0292f23SGarrett Wollman * data in maxopd. 3431a0292f23SGarrett Wollman * 343297d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 3433ef341ee1SGleb Smirnoff * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS 3434ef341ee1SGleb Smirnoff * settings are handled in tcp_mssopt(). 3435df8bae1dSRodney W. Grimes */ 3436a0292f23SGarrett Wollman void 3437ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer, 34383c914c54SAndre Oppermann struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap) 3439df8bae1dSRodney W. Grimes { 3440b287c6c7SBjoern A. Zeeb int mss = 0; 3441b287c6c7SBjoern A. Zeeb u_long maxmtu = 0; 3442c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 344397d8d152SAndre Oppermann struct hc_metrics_lite metrics; 3444ef341ee1SGleb Smirnoff int origoffer; 3445fb59c426SYoshinobu Inoue #ifdef INET6 3446c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3447c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 3448c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3449c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 3450c068736aSJeffrey Hsu #else 3451c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 3452fb59c426SYoshinobu Inoue #endif 3453df8bae1dSRodney W. Grimes 34548501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 34557a3244ccSRobert Watson 3456ef341ee1SGleb Smirnoff if (mtuoffer != -1) { 3457ef341ee1SGleb Smirnoff KASSERT(offer == -1, ("%s: conflict", __func__)); 3458ef341ee1SGleb Smirnoff offer = mtuoffer - min_protoh; 3459ef341ee1SGleb Smirnoff } 3460ef341ee1SGleb Smirnoff origoffer = offer; 3461ef341ee1SGleb Smirnoff 3462e8949f74SAndre Oppermann /* Initialize. */ 346397d8d152SAndre Oppermann #ifdef INET6 346497d8d152SAndre Oppermann if (isipv6) { 34653c914c54SAndre Oppermann maxmtu = tcp_maxmtu6(&inp->inp_inc, cap); 3466603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_v6mssdflt; 3467b287c6c7SBjoern A. Zeeb } 346897d8d152SAndre Oppermann #endif 3469b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3470b287c6c7SBjoern A. Zeeb else 3471b287c6c7SBjoern A. Zeeb #endif 3472b287c6c7SBjoern A. Zeeb #ifdef INET 347397d8d152SAndre Oppermann { 34743c914c54SAndre Oppermann maxmtu = tcp_maxmtu(&inp->inp_inc, cap); 3475603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_mssdflt; 3476df8bae1dSRodney W. Grimes } 3477b287c6c7SBjoern A. Zeeb #endif 3478df8bae1dSRodney W. Grimes 347997d8d152SAndre Oppermann /* 3480e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 348197d8d152SAndre Oppermann */ 34826f01cac6SBjoern A. Zeeb if (maxmtu == 0) { 34836f01cac6SBjoern A. Zeeb /* 34848e5c87f4SBjoern A. Zeeb * In case we return early we need to initialize metrics 34856f01cac6SBjoern A. Zeeb * to a defined state as tcp_hc_get() would do for us 34866f01cac6SBjoern A. Zeeb * if there was no cache hit. 34876f01cac6SBjoern A. Zeeb */ 34886f01cac6SBjoern A. Zeeb if (metricptr != NULL) 34896f01cac6SBjoern A. Zeeb bzero(metricptr, sizeof(struct hc_metrics_lite)); 349097d8d152SAndre Oppermann return; 34916f01cac6SBjoern A. Zeeb } 349297d8d152SAndre Oppermann 3493e8949f74SAndre Oppermann /* What have we got? */ 349497d8d152SAndre Oppermann switch (offer) { 349597d8d152SAndre Oppermann case 0: 349697d8d152SAndre Oppermann /* 349797d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 3498c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 3499c3b02504SBjoern A. Zeeb * already assigned to t_maxopd above. 350097d8d152SAndre Oppermann */ 3501c3b02504SBjoern A. Zeeb offer = tp->t_maxopd; 350297d8d152SAndre Oppermann break; 350397d8d152SAndre Oppermann 350497d8d152SAndre Oppermann case -1: 3505a0292f23SGarrett Wollman /* 3506c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 3507a0292f23SGarrett Wollman */ 350897d8d152SAndre Oppermann /* FALLTHROUGH */ 350997d8d152SAndre Oppermann 351097d8d152SAndre Oppermann default: 3511a0292f23SGarrett Wollman /* 351253369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 351353369ac9SAndre Oppermann * to at least minmss. 351453369ac9SAndre Oppermann */ 3515603724d3SBjoern A. Zeeb offer = max(offer, V_tcp_minmss); 351697d8d152SAndre Oppermann } 3517a0292f23SGarrett Wollman 3518df8bae1dSRodney W. Grimes /* 3519e8949f74SAndre Oppermann * rmx information is now retrieved from tcp_hostcache. 3520df8bae1dSRodney W. Grimes */ 352197d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 35223cee92e0SBjoern A. Zeeb if (metricptr != NULL) 35233cee92e0SBjoern A. Zeeb bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 352497d8d152SAndre Oppermann 3525df8bae1dSRodney W. Grimes /* 3526cc412412SHiren Panchasara * If there's a discovered mtu in tcp hostcache, use it. 3527cc412412SHiren Panchasara * Else, use the link mtu. 3528df8bae1dSRodney W. Grimes */ 352997d8d152SAndre Oppermann if (metrics.rmx_mtu) 35302d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3531c068736aSJeffrey Hsu else { 353231b3783cSHajimu UMEMOTO #ifdef INET6 3533fb59c426SYoshinobu Inoue if (isipv6) { 353497d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3535603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 353697d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 3537603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_v6mssdflt); 3538b287c6c7SBjoern A. Zeeb } 3539b3399803SHajimu UMEMOTO #endif 3540b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3541b287c6c7SBjoern A. Zeeb else 3542b287c6c7SBjoern A. Zeeb #endif 3543b287c6c7SBjoern A. Zeeb #ifdef INET 354497d8d152SAndre Oppermann { 354597d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3546603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 354797d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 3548603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_mssdflt); 3549a0292f23SGarrett Wollman } 3550b287c6c7SBjoern A. Zeeb #endif 35513cee92e0SBjoern A. Zeeb /* 35523cee92e0SBjoern A. Zeeb * XXX - The above conditional (mss = maxmtu - min_protoh) 35533cee92e0SBjoern A. Zeeb * probably violates the TCP spec. 35543cee92e0SBjoern A. Zeeb * The problem is that, since we don't know the 35553cee92e0SBjoern A. Zeeb * other end's MSS, we are supposed to use a conservative 35563cee92e0SBjoern A. Zeeb * default. But, if we do that, then MTU discovery will 35573cee92e0SBjoern A. Zeeb * never actually take place, because the conservative 35583cee92e0SBjoern A. Zeeb * default is much less than the MTUs typically seen 35593cee92e0SBjoern A. Zeeb * on the Internet today. For the moment, we'll sweep 35603cee92e0SBjoern A. Zeeb * this under the carpet. 35613cee92e0SBjoern A. Zeeb * 35623cee92e0SBjoern A. Zeeb * The conservative default might not actually be a problem 35633cee92e0SBjoern A. Zeeb * if the only case this occurs is when sending an initial 35643cee92e0SBjoern A. Zeeb * SYN with options and data to a host we've never talked 35653cee92e0SBjoern A. Zeeb * to before. Then, they will reply with an MSS value which 35663cee92e0SBjoern A. Zeeb * will get recorded and the new parameters should get 35673cee92e0SBjoern A. Zeeb * recomputed. For Further Study. 35683cee92e0SBjoern A. Zeeb */ 356997d8d152SAndre Oppermann } 3570a0292f23SGarrett Wollman mss = min(mss, offer); 357197d8d152SAndre Oppermann 3572a0292f23SGarrett Wollman /* 35733cee92e0SBjoern A. Zeeb * Sanity check: make sure that maxopd will be large 35743cee92e0SBjoern A. Zeeb * enough to allow some data on segments even if the 35753cee92e0SBjoern A. Zeeb * all the option space is used (40bytes). Otherwise 35763cee92e0SBjoern A. Zeeb * funny things may happen in tcp_output. 35773cee92e0SBjoern A. Zeeb */ 35783cee92e0SBjoern A. Zeeb mss = max(mss, 64); 35793cee92e0SBjoern A. Zeeb 35803cee92e0SBjoern A. Zeeb /* 3581a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 3582a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 3583a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 3584a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 3585a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 3586a0292f23SGarrett Wollman */ 3587a0292f23SGarrett Wollman tp->t_maxopd = mss; 3588a0292f23SGarrett Wollman 3589a0292f23SGarrett Wollman /* 3590e8949f74SAndre Oppermann * origoffer==-1 indicates that no segments were received yet. 3591c94c54e4SAndre Oppermann * In this case we just guess. 3592a0292f23SGarrett Wollman */ 3593a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 3594a0292f23SGarrett Wollman (origoffer == -1 || 3595a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 3596a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 3597a0292f23SGarrett Wollman 359897d8d152SAndre Oppermann tp->t_maxseg = mss; 35993cee92e0SBjoern A. Zeeb } 36003cee92e0SBjoern A. Zeeb 36013cee92e0SBjoern A. Zeeb void 36023cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer) 36033cee92e0SBjoern A. Zeeb { 3604dbc42409SLawrence Stewart int mss; 36053cee92e0SBjoern A. Zeeb u_long bufsize; 36063cee92e0SBjoern A. Zeeb struct inpcb *inp; 36073cee92e0SBjoern A. Zeeb struct socket *so; 36083cee92e0SBjoern A. Zeeb struct hc_metrics_lite metrics; 36093c914c54SAndre Oppermann struct tcp_ifcap cap; 3610dbc42409SLawrence Stewart 36113cee92e0SBjoern A. Zeeb KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 36123cee92e0SBjoern A. Zeeb 36133c914c54SAndre Oppermann bzero(&cap, sizeof(cap)); 36143c914c54SAndre Oppermann tcp_mss_update(tp, offer, -1, &metrics, &cap); 36153cee92e0SBjoern A. Zeeb 36163cee92e0SBjoern A. Zeeb mss = tp->t_maxseg; 36173cee92e0SBjoern A. Zeeb inp = tp->t_inpcb; 361897d8d152SAndre Oppermann 3619df8bae1dSRodney W. Grimes /* 362097d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 362197d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 362297d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 362397d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 362497d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3625df8bae1dSRodney W. Grimes */ 3626c3b02504SBjoern A. Zeeb so = inp->inp_socket; 36273f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 3628e233e2acSAndre Oppermann if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe) 362997d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 363097d8d152SAndre Oppermann else 3631df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3632df8bae1dSRodney W. Grimes if (bufsize < mss) 3633df8bae1dSRodney W. Grimes mss = bufsize; 3634df8bae1dSRodney W. Grimes else { 3635df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3636df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3637df8bae1dSRodney W. Grimes bufsize = sb_max; 363888c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 36393f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3640df8bae1dSRodney W. Grimes } 36413f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 3642df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 3643df8bae1dSRodney W. Grimes 36443f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3645e233e2acSAndre Oppermann if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe) 364697d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 364797d8d152SAndre Oppermann else 3648df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3649df8bae1dSRodney W. Grimes if (bufsize > mss) { 3650df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3651df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3652df8bae1dSRodney W. Grimes bufsize = sb_max; 365388c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 36543f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3655df8bae1dSRodney W. Grimes } 36563f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 365791d6cfa6SBjoern A. Zeeb 365891d6cfa6SBjoern A. Zeeb /* Check the interface for TSO capabilities. */ 36593c914c54SAndre Oppermann if (cap.ifcap & CSUM_TSO) { 366091d6cfa6SBjoern A. Zeeb tp->t_flags |= TF_TSO; 36613c914c54SAndre Oppermann tp->t_tsomax = cap.tsomax; 36629fd573c3SHans Petter Selasky tp->t_tsomaxsegcount = cap.tsomaxsegcount; 36639fd573c3SHans Petter Selasky tp->t_tsomaxsegsize = cap.tsomaxsegsize; 36643c914c54SAndre Oppermann } 3665a0292f23SGarrett Wollman } 3666a0292f23SGarrett Wollman 3667a0292f23SGarrett Wollman /* 3668a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3669a0292f23SGarrett Wollman */ 3670a0292f23SGarrett Wollman int 3671ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 3672a0292f23SGarrett Wollman { 367397d8d152SAndre Oppermann int mss = 0; 367497d8d152SAndre Oppermann u_long maxmtu = 0; 367597d8d152SAndre Oppermann u_long thcmtu = 0; 367697d8d152SAndre Oppermann size_t min_protoh; 3677a0292f23SGarrett Wollman 367897d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3679a0292f23SGarrett Wollman 368031b3783cSHajimu UMEMOTO #ifdef INET6 3681dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3682603724d3SBjoern A. Zeeb mss = V_tcp_v6mssdflt; 3683233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 368497d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 3685b287c6c7SBjoern A. Zeeb } 368631b3783cSHajimu UMEMOTO #endif 3687b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3688b287c6c7SBjoern A. Zeeb else 3689b287c6c7SBjoern A. Zeeb #endif 3690b287c6c7SBjoern A. Zeeb #ifdef INET 369197d8d152SAndre Oppermann { 3692603724d3SBjoern A. Zeeb mss = V_tcp_mssdflt; 3693233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 369497d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 369597d8d152SAndre Oppermann } 3696b287c6c7SBjoern A. Zeeb #endif 36973a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET) 36983a9391deSBjoern A. Zeeb thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 36993a9391deSBjoern A. Zeeb #endif 37003a9391deSBjoern A. Zeeb 370197d8d152SAndre Oppermann if (maxmtu && thcmtu) 370297d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 370397d8d152SAndre Oppermann else if (maxmtu || thcmtu) 370497d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 370597d8d152SAndre Oppermann 370697d8d152SAndre Oppermann return (mss); 3707df8bae1dSRodney W. Grimes } 370846f58482SJonathan Lemon 370946f58482SJonathan Lemon 371046f58482SJonathan Lemon /* 3711c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3712c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3713c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3714c068736aSJeffrey Hsu * be started again. 371546f58482SJonathan Lemon */ 3716c068736aSJeffrey Hsu static void 3717ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 371846f58482SJonathan Lemon { 371946f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 372046f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 372146f58482SJonathan Lemon 37228501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 37237a3244ccSRobert Watson 3724b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 372546f58482SJonathan Lemon tp->t_rtttime = 0; 372646f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 37276b2a5f92SJayanth Vijayaraghavan /* 3728c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3729c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 37306b2a5f92SJayanth Vijayaraghavan */ 3731dbc42409SLawrence Stewart tp->snd_cwnd = tp->t_maxseg + BYTES_THIS_ACK(tp, th); 3732a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 37336b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 373446f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 373546f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 373646f58482SJonathan Lemon tp->snd_nxt = onxt; 373746f58482SJonathan Lemon /* 373846f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 373946f58482SJonathan Lemon * not updated yet. 374046f58482SJonathan Lemon */ 3741dbc42409SLawrence Stewart if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th)) 3742dbc42409SLawrence Stewart tp->snd_cwnd -= BYTES_THIS_ACK(tp, th); 3743d7587117SPaul Saab else 3744d7587117SPaul Saab tp->snd_cwnd = 0; 3745d7587117SPaul Saab tp->snd_cwnd += tp->t_maxseg; 374646f58482SJonathan Lemon } 3747*12eeb81fSHiren Panchasara 3748*12eeb81fSHiren Panchasara int 3749*12eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp) 3750*12eeb81fSHiren Panchasara { 3751*12eeb81fSHiren Panchasara return (tp->snd_max - tp->snd_una + 3752*12eeb81fSHiren Panchasara tp->sackhint.sack_bytes_rexmit - 3753*12eeb81fSHiren Panchasara tp->sackhint.sacked_bytes); 3754*12eeb81fSHiren Panchasara } 3755