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> 66960ed29cSSeigo Tanimura #include <sys/signalvar.h> 67df8bae1dSRodney W. Grimes #include <sys/socket.h> 68df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 69960ed29cSSeigo Tanimura #include <sys/sysctl.h> 70816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 71960ed29cSSeigo Tanimura #include <sys/systm.h> 72df8bae1dSRodney W. Grimes 73e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 74e79adb8eSGarrett Wollman 7512e2e970SAndre Oppermann #include <vm/uma.h> 7612e2e970SAndre Oppermann 77df8bae1dSRodney W. Grimes #include <net/if.h> 78df8bae1dSRodney W. Grimes #include <net/route.h> 79530c0060SRobert Watson #include <net/vnet.h> 80df8bae1dSRodney W. Grimes 81773673c1SAndre Oppermann #define TCPSTATES /* for logging */ 82773673c1SAndre Oppermann 83dbc42409SLawrence Stewart #include <netinet/cc.h> 84df8bae1dSRodney W. Grimes #include <netinet/in.h> 85960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 86df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 87960ed29cSSeigo Tanimura #include <netinet/in_var.h> 88df8bae1dSRodney W. Grimes #include <netinet/ip.h> 898e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 90686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 91df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 92ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 93686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 94686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h> 95686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 96960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h> 97960ed29cSSeigo Tanimura #include <netinet6/nd6.h> 98df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 99df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 100df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 101df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 102fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 103df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 104c325962bSMike Silbersack #include <netinet/tcp_syncache.h> 105610ee2f9SDavid Greenman #ifdef TCPDEBUG 106df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 107fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */ 10809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 10909fe6320SNavdeep Parhar #include <netinet/tcp_offload.h> 11009fe6320SNavdeep Parhar #endif 111fb59c426SYoshinobu Inoue 112b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 113b9234fafSSam Leffler #include <netipsec/ipsec.h> 114b9234fafSSam Leffler #include <netipsec/ipsec6.h> 115b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 116b9234fafSSam Leffler 117db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 118db4f9cc7SJonathan Lemon 119aed55708SRobert Watson #include <security/mac/mac_framework.h> 120aed55708SRobert Watson 121dbc42409SLawrence Stewart const int tcprexmtthresh = 3; 1220312fbe9SPoul-Henning Kamp 123773673c1SAndre Oppermann int tcp_log_in_vain = 0; 124816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 125eddfbb76SRobert Watson &tcp_log_in_vain, 0, 126eddfbb76SRobert Watson "Log all incoming TCP segments to closed ports"); 127816a3d83SPoul-Henning Kamp 12882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0; 12982cea7e6SBjoern A. Zeeb #define V_blackhole VNET(blackhole) 130eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 131eddfbb76SRobert Watson &VNET_NAME(blackhole), 0, 132eddfbb76SRobert Watson "Do not send RST on segments to closed ports"); 13316f7f31fSGeoff Rehmet 13482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1; 135eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 136eddfbb76SRobert Watson &VNET_NAME(tcp_delack_enabled), 0, 1373d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 138f498eeeeSDavid Greenman 13982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0; 14082cea7e6SBjoern A. Zeeb #define V_drop_synfin VNET(drop_synfin) 141eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, 142eddfbb76SRobert Watson &VNET_NAME(drop_synfin), 0, 143eddfbb76SRobert Watson "Drop TCP packets with SYN+FIN set"); 144f8613305SDag-Erling Smørgrav 14582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1; 14682cea7e6SBjoern A. Zeeb #define V_tcp_do_rfc3042 VNET(tcp_do_rfc3042) 147eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW, 148eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3042), 0, 149eddfbb76SRobert Watson "Enable RFC 3042 (Limited Transmit)"); 150582a954bSJeffrey Hsu 15182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1; 152eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW, 153eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3390), 0, 154da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 155da3a8a1aSJeffrey Hsu 15609440655SAndre Oppermann SYSCTL_NODE(_net_inet_tcp, OID_AUTO, experimental, CTLFLAG_RW, 0, 15709440655SAndre Oppermann "Experimental TCP extensions"); 15809440655SAndre Oppermann 15909440655SAndre Oppermann VNET_DEFINE(int, tcp_do_initcwnd10) = 1; 16009440655SAndre Oppermann SYSCTL_VNET_INT(_net_inet_tcp_experimental, OID_AUTO, initcwnd10, CTLFLAG_RW, 16109440655SAndre Oppermann &VNET_NAME(tcp_do_initcwnd10), 0, 16209440655SAndre Oppermann "Enable draft-ietf-tcpm-initcwnd-05 (Increasing initial CWND to 10)"); 16309440655SAndre Oppermann 16482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1; 165eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_RW, 166eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3465), 0, 16724cb0f22SLawrence Stewart "Enable RFC 3465 (Appropriate Byte Counting)"); 168eddfbb76SRobert Watson 16982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2; 170eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_RW, 171eddfbb76SRobert Watson &VNET_NAME(tcp_abc_l_var), 2, 17224cb0f22SLawrence Stewart "Cap the max cwnd increment during slow-start to this number of segments"); 17324cb0f22SLawrence Stewart 1746472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN"); 175f2512ba1SRui Paulo 17682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_ecn) = 0; 177eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_RW, 178eddfbb76SRobert Watson &VNET_NAME(tcp_do_ecn), 0, 179eddfbb76SRobert Watson "TCP ECN support"); 180eddfbb76SRobert Watson 18182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1; 182eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_RW, 183eddfbb76SRobert Watson &VNET_NAME(tcp_ecn_maxretries), 0, 184eddfbb76SRobert Watson "Max retries before giving up on ECN"); 185eddfbb76SRobert Watson 18682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0; 18782cea7e6SBjoern A. Zeeb #define V_tcp_insecure_rst VNET(tcp_insecure_rst) 188eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_RW, 189eddfbb76SRobert Watson &VNET_NAME(tcp_insecure_rst), 0, 1906489fe65SAndre Oppermann "Follow the old (insecure) criteria for accepting RST packets"); 191a69968eeSMike Silbersack 192fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64; 193873789cbSAndre Oppermann #define V_tcp_recvspace VNET(tcp_recvspace) 194ddd0c4a9SSergey Kandaurov SYSCTL_VNET_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 195873789cbSAndre Oppermann &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size"); 196873789cbSAndre Oppermann 19782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1; 19882cea7e6SBjoern A. Zeeb #define V_tcp_do_autorcvbuf VNET(tcp_do_autorcvbuf) 199eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_RW, 200eddfbb76SRobert Watson &VNET_NAME(tcp_do_autorcvbuf), 0, 2018b615593SMarko Zec "Enable automatic receive buffer sizing"); 2026741ecf5SAndre Oppermann 20382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024; 20482cea7e6SBjoern A. Zeeb #define V_tcp_autorcvbuf_inc VNET(tcp_autorcvbuf_inc) 205eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_RW, 206eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_inc), 0, 2076489fe65SAndre Oppermann "Incrementor step size of automatic receive buffer"); 2086741ecf5SAndre Oppermann 209b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024; 21082cea7e6SBjoern A. Zeeb #define V_tcp_autorcvbuf_max VNET(tcp_autorcvbuf_max) 211eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_RW, 212eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_max), 0, 2138b615593SMarko Zec "Max size of automatic receive buffer"); 2146741ecf5SAndre Oppermann 215eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb); 21644e33a07SMarko Zec #define tcb6 tcb /* for KAME src sync over BSD*'s */ 21782cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo); 218df8bae1dSRodney W. Grimes 2195a53ca16SPaul Saab static void tcp_dooptions(struct tcpopt *, u_char *, int, int); 220679d9708SAndre Oppermann static void tcp_do_segment(struct mbuf *, struct tcphdr *, 221252ca428SRobert Watson struct socket *, struct tcpcb *, int, int, uint8_t, 222252ca428SRobert Watson int); 223302ce8d6SAndre Oppermann static void tcp_dropwithreset(struct mbuf *, struct tcphdr *, 224302ce8d6SAndre Oppermann struct tcpcb *, int, int); 2254d77a549SAlfred Perlstein static void tcp_pulloutofband(struct socket *, 2264d77a549SAlfred Perlstein struct tcphdr *, struct mbuf *, int); 2274d77a549SAlfred Perlstein static void tcp_xmit_timer(struct tcpcb *, int); 228c068736aSJeffrey Hsu static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 2292903309aSAttilio Rao static void inline tcp_fields_to_host(struct tcphdr *); 2302903309aSAttilio Rao #ifdef TCP_SIGNATURE 2312903309aSAttilio Rao static void inline tcp_fields_to_net(struct tcphdr *); 2322903309aSAttilio Rao static int inline tcp_signature_verify_input(struct mbuf *, int, int, 2332903309aSAttilio Rao int, struct tcpopt *, struct tcphdr *, u_int); 2342903309aSAttilio Rao #endif 235dbc42409SLawrence Stewart static void inline cc_ack_received(struct tcpcb *tp, struct tcphdr *th, 236dbc42409SLawrence Stewart uint16_t type); 237dbc42409SLawrence Stewart static void inline cc_conn_init(struct tcpcb *tp); 238dbc42409SLawrence Stewart static void inline cc_post_recovery(struct tcpcb *tp, struct tcphdr *th); 23939bc9de5SLawrence Stewart static void inline hhook_run_tcp_est_in(struct tcpcb *tp, 24039bc9de5SLawrence Stewart struct tcphdr *th, struct tcpopt *to); 241f2512ba1SRui Paulo 242315e3e38SRobert Watson /* 2435da0521fSAndrey V. Elsukov * TCP statistics are stored in an "array" of counter(9)s. 2445923c293SGleb Smirnoff */ 2455da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat); 2465da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(tcpstat); 2475da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat, 2485da0521fSAndrey V. Elsukov tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 2495923c293SGleb Smirnoff 2505923c293SGleb Smirnoff #ifdef VIMAGE 2515da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(tcpstat); 2525923c293SGleb Smirnoff #endif /* VIMAGE */ 2535923c293SGleb Smirnoff /* 254315e3e38SRobert Watson * Kernel module interface for updating tcpstat. The argument is an index 2555923c293SGleb Smirnoff * into tcpstat treated as an array. 256315e3e38SRobert Watson */ 257315e3e38SRobert Watson void 258315e3e38SRobert Watson kmod_tcpstat_inc(int statnum) 259315e3e38SRobert Watson { 260315e3e38SRobert Watson 2615da0521fSAndrey V. Elsukov counter_u64_add(VNET(tcpstat)[statnum], 1); 262315e3e38SRobert Watson } 263315e3e38SRobert Watson 264dbc42409SLawrence Stewart /* 26539bc9de5SLawrence Stewart * Wrapper for the TCP established input helper hook. 26639bc9de5SLawrence Stewart */ 26739bc9de5SLawrence Stewart static void inline 26839bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to) 26939bc9de5SLawrence Stewart { 27039bc9de5SLawrence Stewart struct tcp_hhook_data hhook_data; 27139bc9de5SLawrence Stewart 27239bc9de5SLawrence Stewart if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) { 27339bc9de5SLawrence Stewart hhook_data.tp = tp; 27439bc9de5SLawrence Stewart hhook_data.th = th; 27539bc9de5SLawrence Stewart hhook_data.to = to; 27639bc9de5SLawrence Stewart 27739bc9de5SLawrence Stewart hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data, 27839bc9de5SLawrence Stewart tp->osd); 27939bc9de5SLawrence Stewart } 28039bc9de5SLawrence Stewart } 28139bc9de5SLawrence Stewart 28239bc9de5SLawrence Stewart /* 283dbc42409SLawrence Stewart * CC wrapper hook functions 284dbc42409SLawrence Stewart */ 285f2512ba1SRui Paulo static void inline 286dbc42409SLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t type) 287f2512ba1SRui Paulo { 288dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 289f2512ba1SRui Paulo 290dbc42409SLawrence Stewart tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th); 2915b648e79SLawrence Stewart if (tp->snd_cwnd <= tp->snd_wnd) 292dbc42409SLawrence Stewart tp->ccv->flags |= CCF_CWND_LIMITED; 293dbc42409SLawrence Stewart else 294dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_CWND_LIMITED; 295dbc42409SLawrence Stewart 296dbc42409SLawrence Stewart if (type == CC_ACK) { 297dbc42409SLawrence Stewart if (tp->snd_cwnd > tp->snd_ssthresh) { 298dbc42409SLawrence Stewart tp->t_bytes_acked += min(tp->ccv->bytes_this_ack, 299dbc42409SLawrence Stewart V_tcp_abc_l_var * tp->t_maxseg); 300dbc42409SLawrence Stewart if (tp->t_bytes_acked >= tp->snd_cwnd) { 301dbc42409SLawrence Stewart tp->t_bytes_acked -= tp->snd_cwnd; 302dbc42409SLawrence Stewart tp->ccv->flags |= CCF_ABC_SENTAWND; 303dbc42409SLawrence Stewart } 304dbc42409SLawrence Stewart } else { 305dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_ABC_SENTAWND; 306dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 307dbc42409SLawrence Stewart } 308dbc42409SLawrence Stewart } 309dbc42409SLawrence Stewart 310dbc42409SLawrence Stewart if (CC_ALGO(tp)->ack_received != NULL) { 311dbc42409SLawrence Stewart /* XXXLAS: Find a way to live without this */ 312dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 313dbc42409SLawrence Stewart CC_ALGO(tp)->ack_received(tp->ccv, type); 314dbc42409SLawrence Stewart } 315dbc42409SLawrence Stewart } 316dbc42409SLawrence Stewart 317dbc42409SLawrence Stewart static void inline 318dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp) 319dbc42409SLawrence Stewart { 320dbc42409SLawrence Stewart struct hc_metrics_lite metrics; 321dbc42409SLawrence Stewart struct inpcb *inp = tp->t_inpcb; 322dbc42409SLawrence Stewart int rtt; 323dbc42409SLawrence Stewart 324dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 325dbc42409SLawrence Stewart 326dbc42409SLawrence Stewart tcp_hc_get(&inp->inp_inc, &metrics); 327dbc42409SLawrence Stewart 328dbc42409SLawrence Stewart if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 329dbc42409SLawrence Stewart tp->t_srtt = rtt; 330dbc42409SLawrence Stewart tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 331dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrtt); 332dbc42409SLawrence Stewart if (metrics.rmx_rttvar) { 333dbc42409SLawrence Stewart tp->t_rttvar = metrics.rmx_rttvar; 334dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrttvar); 335dbc42409SLawrence Stewart } else { 336dbc42409SLawrence Stewart /* default variation is +- 1 rtt */ 337dbc42409SLawrence Stewart tp->t_rttvar = 338dbc42409SLawrence Stewart tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 339dbc42409SLawrence Stewart } 340dbc42409SLawrence Stewart TCPT_RANGESET(tp->t_rxtcur, 341dbc42409SLawrence Stewart ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 342dbc42409SLawrence Stewart tp->t_rttmin, TCPTV_REXMTMAX); 343dbc42409SLawrence Stewart } 344dbc42409SLawrence Stewart if (metrics.rmx_ssthresh) { 345dbc42409SLawrence Stewart /* 346dbc42409SLawrence Stewart * There's some sort of gateway or interface 347dbc42409SLawrence Stewart * buffer limit on the path. Use this to set 348dbc42409SLawrence Stewart * the slow start threshhold, but set the 349dbc42409SLawrence Stewart * threshold to no less than 2*mss. 350dbc42409SLawrence Stewart */ 351dbc42409SLawrence Stewart tp->snd_ssthresh = max(2 * tp->t_maxseg, metrics.rmx_ssthresh); 352dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedssthresh); 353dbc42409SLawrence Stewart } 354dbc42409SLawrence Stewart 355dbc42409SLawrence Stewart /* 3569ec4a4ccSAndre Oppermann * Set the initial slow-start flight size. 357dbc42409SLawrence Stewart * 358cf8f04f4SAndre Oppermann * RFC5681 Section 3.1 specifies the default conservative values. 359cf8f04f4SAndre Oppermann * RFC3390 specifies slightly more aggressive values. 36009440655SAndre Oppermann * Draft-ietf-tcpm-initcwnd-05 increases it to ten segments. 361cf8f04f4SAndre Oppermann * 362cf8f04f4SAndre Oppermann * If a SYN or SYN/ACK was lost and retransmitted, we have to 363cf8f04f4SAndre Oppermann * reduce the initial CWND to one segment as congestion is likely 364cf8f04f4SAndre Oppermann * requiring us to be cautious. 365dbc42409SLawrence Stewart */ 366cf8f04f4SAndre Oppermann if (tp->snd_cwnd == 1) 367cf8f04f4SAndre Oppermann tp->snd_cwnd = tp->t_maxseg; /* SYN(-ACK) lost */ 36809440655SAndre Oppermann else if (V_tcp_do_initcwnd10) 36909440655SAndre Oppermann tp->snd_cwnd = min(10 * tp->t_maxseg, 37009440655SAndre Oppermann max(2 * tp->t_maxseg, 14600)); 371cf8f04f4SAndre Oppermann else if (V_tcp_do_rfc3390) 372dbc42409SLawrence Stewart tp->snd_cwnd = min(4 * tp->t_maxseg, 373dbc42409SLawrence Stewart max(2 * tp->t_maxseg, 4380)); 37422efabd4SAndre Oppermann else { 37522efabd4SAndre Oppermann /* Per RFC5681 Section 3.1 */ 37622efabd4SAndre Oppermann if (tp->t_maxseg > 2190) 37722efabd4SAndre Oppermann tp->snd_cwnd = 2 * tp->t_maxseg; 37822efabd4SAndre Oppermann else if (tp->t_maxseg > 1095) 37922efabd4SAndre Oppermann tp->snd_cwnd = 3 * tp->t_maxseg; 380dbc42409SLawrence Stewart else 38122efabd4SAndre Oppermann tp->snd_cwnd = 4 * tp->t_maxseg; 38222efabd4SAndre Oppermann } 383dbc42409SLawrence Stewart 384dbc42409SLawrence Stewart if (CC_ALGO(tp)->conn_init != NULL) 385dbc42409SLawrence Stewart CC_ALGO(tp)->conn_init(tp->ccv); 386dbc42409SLawrence Stewart } 387dbc42409SLawrence Stewart 388dbc42409SLawrence Stewart void inline 389dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type) 390dbc42409SLawrence Stewart { 391dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 392dbc42409SLawrence Stewart 393dbc42409SLawrence Stewart switch(type) { 394dbc42409SLawrence Stewart case CC_NDUPACK: 395dbc42409SLawrence Stewart if (!IN_FASTRECOVERY(tp->t_flags)) { 396f2512ba1SRui Paulo tp->snd_recover = tp->snd_max; 397f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) 398f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_CWR; 399f2512ba1SRui Paulo } 400dbc42409SLawrence Stewart break; 401dbc42409SLawrence Stewart case CC_ECN: 402dbc42409SLawrence Stewart if (!IN_CONGRECOVERY(tp->t_flags)) { 403dbc42409SLawrence Stewart TCPSTAT_INC(tcps_ecn_rcwnd); 404dbc42409SLawrence Stewart tp->snd_recover = tp->snd_max; 405dbc42409SLawrence Stewart if (tp->t_flags & TF_ECN_PERMIT) 406dbc42409SLawrence Stewart tp->t_flags |= TF_ECN_SND_CWR; 407dbc42409SLawrence Stewart } 408dbc42409SLawrence Stewart break; 409dbc42409SLawrence Stewart case CC_RTO: 410dbc42409SLawrence Stewart tp->t_dupacks = 0; 411dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 412dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 4136157935fSLawrence Stewart tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 / 4146157935fSLawrence Stewart tp->t_maxseg) * tp->t_maxseg; 415dbc42409SLawrence Stewart tp->snd_cwnd = tp->t_maxseg; 416dbc42409SLawrence Stewart break; 417dbc42409SLawrence Stewart case CC_RTO_ERR: 418dbc42409SLawrence Stewart TCPSTAT_INC(tcps_sndrexmitbad); 419dbc42409SLawrence Stewart /* RTO was unnecessary, so reset everything. */ 420dbc42409SLawrence Stewart tp->snd_cwnd = tp->snd_cwnd_prev; 421dbc42409SLawrence Stewart tp->snd_ssthresh = tp->snd_ssthresh_prev; 422dbc42409SLawrence Stewart tp->snd_recover = tp->snd_recover_prev; 423dbc42409SLawrence Stewart if (tp->t_flags & TF_WASFRECOVERY) 424dbc42409SLawrence Stewart ENTER_FASTRECOVERY(tp->t_flags); 425dbc42409SLawrence Stewart if (tp->t_flags & TF_WASCRECOVERY) 426dbc42409SLawrence Stewart ENTER_CONGRECOVERY(tp->t_flags); 427dbc42409SLawrence Stewart tp->snd_nxt = tp->snd_max; 428672dc4aeSJohn Baldwin tp->t_flags &= ~TF_PREVVALID; 429dbc42409SLawrence Stewart tp->t_badrxtwin = 0; 430dbc42409SLawrence Stewart break; 431dbc42409SLawrence Stewart } 432dbc42409SLawrence Stewart 433dbc42409SLawrence Stewart if (CC_ALGO(tp)->cong_signal != NULL) { 434dbc42409SLawrence Stewart if (th != NULL) 435dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 436dbc42409SLawrence Stewart CC_ALGO(tp)->cong_signal(tp->ccv, type); 437dbc42409SLawrence Stewart } 438dbc42409SLawrence Stewart } 439dbc42409SLawrence Stewart 440dbc42409SLawrence Stewart static void inline 441dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th) 442dbc42409SLawrence Stewart { 443dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 444dbc42409SLawrence Stewart 445dbc42409SLawrence Stewart /* XXXLAS: KASSERT that we're in recovery? */ 446dbc42409SLawrence Stewart 447dbc42409SLawrence Stewart if (CC_ALGO(tp)->post_recovery != NULL) { 448dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 449dbc42409SLawrence Stewart CC_ALGO(tp)->post_recovery(tp->ccv); 450dbc42409SLawrence Stewart } 451dbc42409SLawrence Stewart /* XXXLAS: EXIT_RECOVERY ? */ 452dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 453dbc42409SLawrence Stewart } 4540312fbe9SPoul-Henning Kamp 4552903309aSAttilio Rao static inline void 4562903309aSAttilio Rao tcp_fields_to_host(struct tcphdr *th) 4572903309aSAttilio Rao { 4582903309aSAttilio Rao 4592903309aSAttilio Rao th->th_seq = ntohl(th->th_seq); 4602903309aSAttilio Rao th->th_ack = ntohl(th->th_ack); 4612903309aSAttilio Rao th->th_win = ntohs(th->th_win); 4622903309aSAttilio Rao th->th_urp = ntohs(th->th_urp); 4632903309aSAttilio Rao } 4642903309aSAttilio Rao 4652903309aSAttilio Rao #ifdef TCP_SIGNATURE 4662903309aSAttilio Rao static inline void 4672903309aSAttilio Rao tcp_fields_to_net(struct tcphdr *th) 4682903309aSAttilio Rao { 4692903309aSAttilio Rao 4702903309aSAttilio Rao th->th_seq = htonl(th->th_seq); 4712903309aSAttilio Rao th->th_ack = htonl(th->th_ack); 4722903309aSAttilio Rao th->th_win = htons(th->th_win); 4732903309aSAttilio Rao th->th_urp = htons(th->th_urp); 4742903309aSAttilio Rao } 4752903309aSAttilio Rao 4762903309aSAttilio Rao static inline int 4772903309aSAttilio Rao tcp_signature_verify_input(struct mbuf *m, int off0, int tlen, int optlen, 4782903309aSAttilio Rao struct tcpopt *to, struct tcphdr *th, u_int tcpbflag) 4792903309aSAttilio Rao { 4802903309aSAttilio Rao int ret; 4812903309aSAttilio Rao 4822903309aSAttilio Rao tcp_fields_to_net(th); 4832903309aSAttilio Rao ret = tcp_signature_verify(m, off0, tlen, optlen, to, th, tcpbflag); 4842903309aSAttilio Rao tcp_fields_to_host(th); 4852903309aSAttilio Rao return (ret); 4862903309aSAttilio Rao } 4872903309aSAttilio Rao #endif 4882903309aSAttilio Rao 489fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 490fb59c426SYoshinobu Inoue #ifdef INET6 491fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \ 492fb59c426SYoshinobu Inoue do { \ 493fb59c426SYoshinobu Inoue if ((tp) && (tp)->t_inpcb && \ 49497d8d152SAndre Oppermann ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \ 49597d8d152SAndre Oppermann nd6_nud_hint(NULL, NULL, 0); \ 496fb59c426SYoshinobu Inoue } while (0) 497fb59c426SYoshinobu Inoue #else 498fb59c426SYoshinobu Inoue #define ND6_HINT(tp) 499fb59c426SYoshinobu Inoue #endif 500df8bae1dSRodney W. Grimes 501df8bae1dSRodney W. Grimes /* 502262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 503262c1c1aSMatthew Dillon * - there is no delayed ack timer in progress and 504262c1c1aSMatthew Dillon * - our last ack wasn't a 0-sized window. We never want to delay 5053bfd6421SJonathan Lemon * the ack that opens up a 0-sized window and 5063bfd6421SJonathan Lemon * - delayed acks are enabled or 5073bfd6421SJonathan Lemon * - this is a half-synchronized T/TCP connection. 508d8c85a26SJonathan Lemon */ 509d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \ 510b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 511a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 512603724d3SBjoern A. Zeeb (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 513d8c85a26SJonathan Lemon 514df8bae1dSRodney W. Grimes /* 5158d573cc1SAndre Oppermann * TCP input handling is split into multiple parts: 5168d573cc1SAndre Oppermann * tcp6_input is a thin wrapper around tcp_input for the extended 5178d573cc1SAndre Oppermann * ip6_protox[] call format in ip6_input 5188d573cc1SAndre Oppermann * tcp_input handles primary segment validation, inpcb lookup and 5198d573cc1SAndre Oppermann * SYN processing on listen sockets 5208d573cc1SAndre Oppermann * tcp_do_segment processes the ACK and text of the segment for 5218d573cc1SAndre Oppermann * establishing, established and closing connections 522df8bae1dSRodney W. Grimes */ 523fb59c426SYoshinobu Inoue #ifdef INET6 524fb59c426SYoshinobu Inoue int 525ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto) 526fb59c426SYoshinobu Inoue { 527ad3f9ab3SAndre Oppermann struct mbuf *m = *mp; 52833841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 529fb59c426SYoshinobu Inoue 530fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 531fb59c426SYoshinobu Inoue 532fb59c426SYoshinobu Inoue /* 533fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 534fb59c426SYoshinobu Inoue * better place to put this in? 535fb59c426SYoshinobu Inoue */ 53633841545SHajimu UMEMOTO ia6 = ip6_getdstifaddr(m); 53733841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 538fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 539fb59c426SYoshinobu Inoue 5408c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 541fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 542fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 543fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 544fb59c426SYoshinobu Inoue return IPPROTO_DONE; 545fb59c426SYoshinobu Inoue } 54677d396fdSMaksim Yevmenkin if (ia6) 54777d396fdSMaksim Yevmenkin ifa_free(&ia6->ia_ifa); 548fb59c426SYoshinobu Inoue 549f0ffb944SJulian Elischer tcp_input(m, *offp); 550fb59c426SYoshinobu Inoue return IPPROTO_DONE; 551fb59c426SYoshinobu Inoue } 552b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 553fb59c426SYoshinobu Inoue 554df8bae1dSRodney W. Grimes void 555ad3f9ab3SAndre Oppermann tcp_input(struct mbuf *m, int off0) 556df8bae1dSRodney W. Grimes { 557b287c6c7SBjoern A. Zeeb struct tcphdr *th = NULL; 558ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 559ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 560302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 561302ce8d6SAndre Oppermann struct socket *so = NULL; 562e79adb8eSGarrett Wollman u_char *optp = NULL; 56326f9a767SRodney W. Grimes int optlen = 0; 564b287c6c7SBjoern A. Zeeb #ifdef INET 565b287c6c7SBjoern A. Zeeb int len; 566b287c6c7SBjoern A. Zeeb #endif 567b287c6c7SBjoern A. Zeeb int tlen = 0, off; 568fb59c426SYoshinobu Inoue int drop_hdrlen; 569ad3f9ab3SAndre Oppermann int thflags; 570302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 5712903309aSAttilio Rao #ifdef TCP_SIGNATURE 5722903309aSAttilio Rao uint8_t sig_checked = 0; 5732903309aSAttilio Rao #endif 574b287c6c7SBjoern A. Zeeb uint8_t iptos = 0; 575c1de64a4SAndrey V. Elsukov struct m_tag *fwd_tag = NULL; 576c068736aSJeffrey Hsu #ifdef INET6 577302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 578c068736aSJeffrey Hsu int isipv6; 579c068736aSJeffrey Hsu #else 580a160e630SAndre Oppermann const void *ip6 = NULL; 581b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 582302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 583a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 584252ca428SRobert Watson int ti_locked; 585252ca428SRobert Watson #define TI_UNLOCKED 1 586fa046d87SRobert Watson #define TI_WLOCKED 2 587f76fcf6dSJeffrey Hsu 588610ee2f9SDavid Greenman #ifdef TCPDEBUG 589c068736aSJeffrey Hsu /* 590c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 591c068736aSJeffrey Hsu * now IPv6. 592c068736aSJeffrey Hsu */ 59314739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 594410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 595610ee2f9SDavid Greenman short ostate = 0; 596610ee2f9SDavid Greenman #endif 597c068736aSJeffrey Hsu 598fb59c426SYoshinobu Inoue #ifdef INET6 599fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 600fb59c426SYoshinobu Inoue #endif 601a0292f23SGarrett Wollman 602302ce8d6SAndre Oppermann to.to_flags = 0; 60378b50714SRobert Watson TCPSTAT_INC(tcps_rcvtotal); 604fb59c426SYoshinobu Inoue 60504d3a452SHajimu UMEMOTO #ifdef INET6 606b287c6c7SBjoern A. Zeeb if (isipv6) { 6078d573cc1SAndre Oppermann /* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */ 60845747ba5SBjoern A. Zeeb 60945747ba5SBjoern A. Zeeb if (m->m_len < (sizeof(*ip6) + sizeof(*th))) { 61045747ba5SBjoern A. Zeeb m = m_pullup(m, sizeof(*ip6) + sizeof(*th)); 61145747ba5SBjoern A. Zeeb if (m == NULL) { 61245747ba5SBjoern A. Zeeb TCPSTAT_INC(tcps_rcvshort); 61345747ba5SBjoern A. Zeeb return; 61445747ba5SBjoern A. Zeeb } 61545747ba5SBjoern A. Zeeb } 61645747ba5SBjoern A. Zeeb 617fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 61845747ba5SBjoern A. Zeeb th = (struct tcphdr *)((caddr_t)ip6 + off0); 619fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 620356ab07eSBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) { 62145747ba5SBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 62245747ba5SBjoern A. Zeeb th->th_sum = m->m_pkthdr.csum_data; 62345747ba5SBjoern A. Zeeb else 62445747ba5SBjoern A. Zeeb th->th_sum = in6_cksum_pseudo(ip6, tlen, 62545747ba5SBjoern A. Zeeb IPPROTO_TCP, m->m_pkthdr.csum_data); 62645747ba5SBjoern A. Zeeb th->th_sum ^= 0xffff; 62745747ba5SBjoern A. Zeeb } else 62845747ba5SBjoern A. Zeeb th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen); 62945747ba5SBjoern A. Zeeb if (th->th_sum) { 63078b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 631fb59c426SYoshinobu Inoue goto drop; 632fb59c426SYoshinobu Inoue } 63333841545SHajimu UMEMOTO 63433841545SHajimu UMEMOTO /* 63533841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 63633841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 63733841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 63833841545SHajimu UMEMOTO * 63933841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 64033841545SHajimu UMEMOTO * already dropped in ip6_input. 64133841545SHajimu UMEMOTO */ 64233841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 64333841545SHajimu UMEMOTO /* XXX stat */ 64433841545SHajimu UMEMOTO goto drop; 64533841545SHajimu UMEMOTO } 646b287c6c7SBjoern A. Zeeb } 64704d3a452SHajimu UMEMOTO #endif 648b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 649b287c6c7SBjoern A. Zeeb else 650b287c6c7SBjoern A. Zeeb #endif 651b287c6c7SBjoern A. Zeeb #ifdef INET 652b287c6c7SBjoern A. Zeeb { 653df8bae1dSRodney W. Grimes /* 654df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 655df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 656df8bae1dSRodney W. Grimes */ 657fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 658105bd211SGleb Smirnoff ip_stripoptions(m); 659fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 660fb59c426SYoshinobu Inoue } 661df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 6624dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 6634dfdffe9SAndre Oppermann == NULL) { 66478b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 665df8bae1dSRodney W. Grimes return; 666df8bae1dSRodney W. Grimes } 667df8bae1dSRodney W. Grimes } 668fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 669db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 6708ad458a4SGleb Smirnoff tlen = ntohs(ip->ip_len) - off0; 671df8bae1dSRodney W. Grimes 672db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 673db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 674db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 675db4f9cc7SJonathan Lemon else 676db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 677c068736aSJeffrey Hsu ip->ip_dst.s_addr, 6788f134647SGleb Smirnoff htonl(m->m_pkthdr.csum_data + tlen + 679c068736aSJeffrey Hsu IPPROTO_TCP)); 680db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 681db4f9cc7SJonathan Lemon } else { 6828f134647SGleb Smirnoff struct ipovly *ipov = (struct ipovly *)ip; 6838f134647SGleb Smirnoff 684df8bae1dSRodney W. Grimes /* 685df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 686df8bae1dSRodney W. Grimes */ 6878f134647SGleb Smirnoff len = off0 + tlen; 688fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 6898f134647SGleb Smirnoff ipov->ih_len = htons(tlen); 690fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 691db4f9cc7SJonathan Lemon } 692fb59c426SYoshinobu Inoue if (th->th_sum) { 69378b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 694df8bae1dSRodney W. Grimes goto drop; 695df8bae1dSRodney W. Grimes } 696fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 697fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 698fb59c426SYoshinobu Inoue } 699b287c6c7SBjoern A. Zeeb #endif /* INET */ 700df8bae1dSRodney W. Grimes 701f2512ba1SRui Paulo #ifdef INET6 702f2512ba1SRui Paulo if (isipv6) 703f2512ba1SRui Paulo iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 704b287c6c7SBjoern A. Zeeb #endif 705b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 706f2512ba1SRui Paulo else 707f2512ba1SRui Paulo #endif 708b287c6c7SBjoern A. Zeeb #ifdef INET 709f2512ba1SRui Paulo iptos = ip->ip_tos; 710b287c6c7SBjoern A. Zeeb #endif 711f2512ba1SRui Paulo 712df8bae1dSRodney W. Grimes /* 713df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 714df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 715df8bae1dSRodney W. Grimes */ 716fb59c426SYoshinobu Inoue off = th->th_off << 2; 717df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 71878b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadoff); 719df8bae1dSRodney W. Grimes goto drop; 720df8bae1dSRodney W. Grimes } 721fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 722df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 72304d3a452SHajimu UMEMOTO #ifdef INET6 724b287c6c7SBjoern A. Zeeb if (isipv6) { 725fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 726fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 727fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 728b287c6c7SBjoern A. Zeeb } 72904d3a452SHajimu UMEMOTO #endif 730b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 731b287c6c7SBjoern A. Zeeb else 732b287c6c7SBjoern A. Zeeb #endif 733b287c6c7SBjoern A. Zeeb #ifdef INET 734b287c6c7SBjoern A. Zeeb { 735df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 736c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 7374dfdffe9SAndre Oppermann == NULL) { 73878b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 739df8bae1dSRodney W. Grimes return; 740df8bae1dSRodney W. Grimes } 741fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 742fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 743fb59c426SYoshinobu Inoue } 744df8bae1dSRodney W. Grimes } 745b287c6c7SBjoern A. Zeeb #endif 746df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 747fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 748df8bae1dSRodney W. Grimes } 749fb59c426SYoshinobu Inoue thflags = th->th_flags; 750df8bae1dSRodney W. Grimes 751e46cd3d4SDag-Erling Smørgrav /* 752df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 753df8bae1dSRodney W. Grimes */ 7542903309aSAttilio Rao tcp_fields_to_host(th); 755df8bae1dSRodney W. Grimes 756df8bae1dSRodney W. Grimes /* 757794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 758755c1f07SAndras Olah */ 759fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 760755c1f07SAndras Olah 761755c1f07SAndras Olah /* 762fa046d87SRobert Watson * Locate pcb for segment; if we're likely to add or remove a 763fa046d87SRobert Watson * connection then first acquire pcbinfo lock. There are two cases 764fa046d87SRobert Watson * where we might discover later we need a write lock despite the 765fa046d87SRobert Watson * flags: ACKs moving a connection out of the syncache, and ACKs for 766fa046d87SRobert Watson * a connection in TIMEWAIT. 767df8bae1dSRodney W. Grimes */ 768fa046d87SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0) { 769603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 770252ca428SRobert Watson ti_locked = TI_WLOCKED; 771fa046d87SRobert Watson } else 772fa046d87SRobert Watson ti_locked = TI_UNLOCKED; 773252ca428SRobert Watson 7748d573cc1SAndre Oppermann /* 7758d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 7768d573cc1SAndre Oppermann */ 777b8056faeSGleb Smirnoff if ( 778b8056faeSGleb Smirnoff #ifdef INET6 779b8056faeSGleb Smirnoff (isipv6 && (m->m_flags & M_IP6_NEXTHOP)) 780b8056faeSGleb Smirnoff #ifdef INET 781b8056faeSGleb Smirnoff || (!isipv6 && (m->m_flags & M_IP_NEXTHOP)) 782b8056faeSGleb Smirnoff #endif 783b8056faeSGleb Smirnoff #endif 784b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6) 785b8056faeSGleb Smirnoff (m->m_flags & M_IP_NEXTHOP) 786b8056faeSGleb Smirnoff #endif 787b8056faeSGleb Smirnoff ) 7889b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 7899b932e9eSAndre Oppermann 790ce7ad664SEd Maste findpcb: 791ce7ad664SEd Maste #ifdef INVARIANTS 792ce7ad664SEd Maste if (ti_locked == TI_WLOCKED) { 793ce7ad664SEd Maste INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 794ce7ad664SEd Maste } else { 795ce7ad664SEd Maste INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 796ce7ad664SEd Maste } 797ce7ad664SEd Maste #endif 7988a006adbSBjoern A. Zeeb #ifdef INET6 7998a006adbSBjoern A. Zeeb if (isipv6 && fwd_tag != NULL) { 8008a006adbSBjoern A. Zeeb struct sockaddr_in6 *next_hop6; 8018a006adbSBjoern A. Zeeb 8028a006adbSBjoern A. Zeeb next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1); 8038a006adbSBjoern A. Zeeb /* 8048a006adbSBjoern A. Zeeb * Transparently forwarded. Pretend to be the destination. 8058a006adbSBjoern A. Zeeb * Already got one like this? 8068a006adbSBjoern A. Zeeb */ 8078a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, 8088a006adbSBjoern A. Zeeb &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport, 8098a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m); 8108a006adbSBjoern A. Zeeb if (!inp) { 8118a006adbSBjoern A. Zeeb /* 8128a006adbSBjoern A. Zeeb * It's new. Try to find the ambushing socket. 8138a006adbSBjoern A. Zeeb * Because we've rewritten the destination address, 8148a006adbSBjoern A. Zeeb * any hardware-generated hash is ignored. 8158a006adbSBjoern A. Zeeb */ 8168a006adbSBjoern A. Zeeb inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src, 8178a006adbSBjoern A. Zeeb th->th_sport, &next_hop6->sin6_addr, 8188a006adbSBjoern A. Zeeb next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) : 8198a006adbSBjoern A. Zeeb th->th_dport, INPLOOKUP_WILDCARD | 8208a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 8218a006adbSBjoern A. Zeeb } 822c1de64a4SAndrey V. Elsukov } else if (isipv6) { 8238a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src, 8248a006adbSBjoern A. Zeeb th->th_sport, &ip6->ip6_dst, th->th_dport, 8258a006adbSBjoern A. Zeeb INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 8268a006adbSBjoern A. Zeeb m->m_pkthdr.rcvif, m); 8278a006adbSBjoern A. Zeeb } 8288a006adbSBjoern A. Zeeb #endif /* INET6 */ 8298a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET) 8308a006adbSBjoern A. Zeeb else 8318a006adbSBjoern A. Zeeb #endif 8328a006adbSBjoern A. Zeeb #ifdef INET 8338a006adbSBjoern A. Zeeb if (fwd_tag != NULL) { 8349b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 8359b932e9eSAndre Oppermann 8369b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 837f9e354dfSJulian Elischer /* 8382b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 839f9e354dfSJulian Elischer * already got one like this? 840f9e354dfSJulian Elischer */ 841d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport, 842fa046d87SRobert Watson ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB, 843d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 844fa046d87SRobert Watson if (!inp) { 845fa046d87SRobert Watson /* 846fa046d87SRobert Watson * It's new. Try to find the ambushing socket. 847d3c1f003SRobert Watson * Because we've rewritten the destination address, 848d3c1f003SRobert Watson * any hardware-generated hash is ignored. 849fa046d87SRobert Watson */ 850fa046d87SRobert Watson inp = in_pcblookup(&V_tcbinfo, ip->ip_src, 851fa046d87SRobert Watson th->th_sport, next_hop->sin_addr, 852fa046d87SRobert Watson next_hop->sin_port ? ntohs(next_hop->sin_port) : 853fa046d87SRobert Watson th->th_dport, INPLOOKUP_WILDCARD | 854fa046d87SRobert Watson INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 855f9e354dfSJulian Elischer } 856b10fbdeaSAndre Oppermann } else 857d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, 858fa046d87SRobert Watson th->th_sport, ip->ip_dst, th->th_dport, 859fa046d87SRobert Watson INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 860d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 8618a006adbSBjoern A. Zeeb #endif /* INET */ 862f9e354dfSJulian Elischer 863df8bae1dSRodney W. Grimes /* 864574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 865574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 8668b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 867df8bae1dSRodney W. Grimes */ 868816a3d83SPoul-Henning Kamp if (inp == NULL) { 869574b6964SAndre Oppermann /* 870574b6964SAndre Oppermann * Log communication attempts to ports that are not 871574b6964SAndre Oppermann * in use. 872574b6964SAndre Oppermann */ 873574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 874574b6964SAndre Oppermann tcp_log_in_vain == 2) { 875b7d747ecSAndre Oppermann if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6))) 876df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 877df541e5fSAndre Oppermann "to closed port\n", s, __func__); 8782e4e1b4cSGeoff Rehmet } 879574b6964SAndre Oppermann /* 880574b6964SAndre Oppermann * When blackholing do not respond with a RST but 881574b6964SAndre Oppermann * completely ignore the segment and drop it. 882574b6964SAndre Oppermann */ 883603724d3SBjoern A. Zeeb if ((V_blackhole == 1 && (thflags & TH_SYN)) || 884603724d3SBjoern A. Zeeb V_blackhole == 2) 8851929eae1SAndre Oppermann goto dropunlock; 886574b6964SAndre Oppermann 887a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 888a57815efSBosko Milekic goto dropwithreset; 889816a3d83SPoul-Henning Kamp } 890fa046d87SRobert Watson INP_WLOCK_ASSERT(inp); 89180cb9f21SKip Macy if (!(inp->inp_flags & INP_HW_FLOWID) 89280cb9f21SKip Macy && (m->m_flags & M_FLOWID) 89380cb9f21SKip Macy && ((inp->inp_socket == NULL) 89480cb9f21SKip Macy || !(inp->inp_socket->so_options & SO_ACCEPTCONN))) { 89580cb9f21SKip Macy inp->inp_flags |= INP_HW_FLOWID; 89680cb9f21SKip Macy inp->inp_flags &= ~INP_SW_FLOWID; 89780cb9f21SKip Macy inp->inp_flowid = m->m_pkthdr.flowid; 89880cb9f21SKip Macy } 899a2589465SKen Smith #ifdef IPSEC 900a2589465SKen Smith #ifdef INET6 901a2589465SKen Smith if (isipv6 && ipsec6_in_reject(m, inp)) { 9026659296cSAndrey V. Elsukov IPSEC6STAT_INC(in_polvio); 903a2589465SKen Smith goto dropunlock; 904a2589465SKen Smith } else 905a2589465SKen Smith #endif /* INET6 */ 906a2589465SKen Smith if (ipsec4_in_reject(m, inp) != 0) { 9076659296cSAndrey V. Elsukov IPSECSTAT_INC(in_polvio); 908a2589465SKen Smith goto dropunlock; 909a2589465SKen Smith } 910a2589465SKen Smith #endif /* IPSEC */ 911a2589465SKen Smith 9128d573cc1SAndre Oppermann /* 9138d573cc1SAndre Oppermann * Check the minimum TTL for socket. 9148d573cc1SAndre Oppermann */ 91534f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 91634f83c52SGeorge V. Neville-Neil #ifdef INET6 91734f83c52SGeorge V. Neville-Neil if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim) 918302ce8d6SAndre Oppermann goto dropunlock; 91902707462SAndre Oppermann else 92034f83c52SGeorge V. Neville-Neil #endif 92134f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 922302ce8d6SAndre Oppermann goto dropunlock; 92334f83c52SGeorge V. Neville-Neil } 924936cd18dSAndre Oppermann 925340c35deSJonathan Lemon /* 926252ca428SRobert Watson * A previous connection in TIMEWAIT state is supposed to catch stray 927252ca428SRobert Watson * or duplicate segments arriving late. If this segment was a 9280989f56cSRobert Watson * legitimate new connection attempt, the old INPCB gets removed and 929252ca428SRobert Watson * we can try again to find a listening socket. 930252ca428SRobert Watson * 931fa046d87SRobert Watson * At this point, due to earlier optimism, we may hold only an inpcb 932fa046d87SRobert Watson * lock, and not the inpcbinfo write lock. If so, we need to try to 933fa046d87SRobert Watson * acquire it, or if that fails, acquire a reference on the inpcb, 934fa046d87SRobert Watson * drop all locks, acquire a global write lock, and then re-acquire 935fa046d87SRobert Watson * the inpcb lock. We may at that point discover that another thread 936fa046d87SRobert Watson * has tried to free the inpcb, in which case we need to loop back 937fa046d87SRobert Watson * and try to find a new inpcb to deliver to. 938fa046d87SRobert Watson * 939fa046d87SRobert Watson * XXXRW: It may be time to rethink timewait locking. 940340c35deSJonathan Lemon */ 941883e9bc4SRobert Watson relocked: 942ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 943fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 944fa046d87SRobert Watson if (INP_INFO_TRY_WLOCK(&V_tcbinfo) == 0) { 945252ca428SRobert Watson in_pcbref(inp); 946252ca428SRobert Watson INP_WUNLOCK(inp); 947252ca428SRobert Watson INP_INFO_WLOCK(&V_tcbinfo); 948252ca428SRobert Watson ti_locked = TI_WLOCKED; 949252ca428SRobert Watson INP_WLOCK(inp); 950fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) { 951252ca428SRobert Watson inp = NULL; 952252ca428SRobert Watson goto findpcb; 953252ca428SRobert Watson } 954f681a5fdSRobert Watson } else 955252ca428SRobert Watson ti_locked = TI_WLOCKED; 956252ca428SRobert Watson } 957252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 958252ca428SRobert Watson 959340c35deSJonathan Lemon if (thflags & TH_SYN) 960f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 9618d573cc1SAndre Oppermann /* 9628d573cc1SAndre Oppermann * NB: tcp_twcheck unlocks the INP and frees the mbuf. 9638d573cc1SAndre Oppermann */ 9642104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 965340c35deSJonathan Lemon goto findpcb; 966603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 967340c35deSJonathan Lemon return; 968340c35deSJonathan Lemon } 969794235b7SAndre Oppermann /* 970794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 971794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 972794235b7SAndre Oppermann * segment and send an appropriate response. 973794235b7SAndre Oppermann */ 974df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 975a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 976a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 977a57815efSBosko Milekic goto dropwithreset; 97809f81a46SBosko Milekic } 979df8bae1dSRodney W. Grimes 98009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 98109fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 98209fe6320SNavdeep Parhar tcp_offload_input(tp, m); 98309fe6320SNavdeep Parhar m = NULL; /* consumed by the TOE driver */ 98409fe6320SNavdeep Parhar goto dropunlock; 98509fe6320SNavdeep Parhar } 98609fe6320SNavdeep Parhar #endif 98709fe6320SNavdeep Parhar 988252ca428SRobert Watson /* 989252ca428SRobert Watson * We've identified a valid inpcb, but it could be that we need an 990fa046d87SRobert Watson * inpcbinfo write lock but don't hold it. In this case, attempt to 991fa046d87SRobert Watson * acquire using the same strategy as the TIMEWAIT case above. If we 992fa046d87SRobert Watson * relock, we have to jump back to 'relocked' as the connection might 993fa046d87SRobert Watson * now be in TIMEWAIT. 994252ca428SRobert Watson */ 995fa046d87SRobert Watson #ifdef INVARIANTS 996fa046d87SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0) 997fa046d87SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 998fa046d87SRobert Watson #endif 999fa046d87SRobert Watson if (tp->t_state != TCPS_ESTABLISHED) { 1000fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 1001fa046d87SRobert Watson if (INP_INFO_TRY_WLOCK(&V_tcbinfo) == 0) { 1002252ca428SRobert Watson in_pcbref(inp); 1003252ca428SRobert Watson INP_WUNLOCK(inp); 1004252ca428SRobert Watson INP_INFO_WLOCK(&V_tcbinfo); 1005252ca428SRobert Watson ti_locked = TI_WLOCKED; 1006252ca428SRobert Watson INP_WLOCK(inp); 1007fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) { 1008252ca428SRobert Watson inp = NULL; 1009252ca428SRobert Watson goto findpcb; 1010252ca428SRobert Watson } 1011883e9bc4SRobert Watson goto relocked; 1012f681a5fdSRobert Watson } else 1013252ca428SRobert Watson ti_locked = TI_WLOCKED; 1014252ca428SRobert Watson } 1015252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1016252ca428SRobert Watson } 1017252ca428SRobert Watson 1018c488362eSRobert Watson #ifdef MAC 10198501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 102030d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 1021302ce8d6SAndre Oppermann goto dropunlock; 1022c488362eSRobert Watson #endif 1023a557af22SRobert Watson so = inp->inp_socket; 1024302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 1025610ee2f9SDavid Greenman #ifdef TCPDEBUG 1026df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 1027df8bae1dSRodney W. Grimes ostate = tp->t_state; 102810fe523eSMaxim Konovalov #ifdef INET6 10296f697424SBjoern A. Zeeb if (isipv6) { 1030540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 1031d30d90dcSMaxim Konovalov } else 10326f697424SBjoern A. Zeeb #endif 1033540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 1034fb59c426SYoshinobu Inoue tcp_savetcp = *th; 1035df8bae1dSRodney W. Grimes } 1036b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */ 1037db33b3e6SAndre Oppermann /* 1038db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 1039db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 1040fa046d87SRobert Watson * attempt or the completion of a previous one. Because listen 1041fa046d87SRobert Watson * sockets are never in TCPS_ESTABLISHED, the V_tcbinfo lock will be 1042fa046d87SRobert Watson * held in this case. 1043db33b3e6SAndre Oppermann */ 1044540e8b7eSJeffrey Hsu if (so->so_options & SO_ACCEPTCONN) { 1045540e8b7eSJeffrey Hsu struct in_conninfo inc; 1046540e8b7eSJeffrey Hsu 10477824d002SAndre Oppermann KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but " 10487824d002SAndre Oppermann "tp not listening", __func__)); 1049fa046d87SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 10507824d002SAndre Oppermann 10518bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 1052302ce8d6SAndre Oppermann #ifdef INET6 1053be2ac88cSJonathan Lemon if (isipv6) { 1054dcdb4371SBjoern A. Zeeb inc.inc_flags |= INC_ISIPV6; 1055be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 1056be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 1057302ce8d6SAndre Oppermann } else 1058302ce8d6SAndre Oppermann #endif 1059302ce8d6SAndre Oppermann { 1060be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 1061be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 1062be2ac88cSJonathan Lemon } 1063be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 1064be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 10657973fba3SJulian Elischer inc.inc_fibnum = so->so_fibnum; 1066be2ac88cSJonathan Lemon 1067fb59c426SYoshinobu Inoue /* 10688d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 10698d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 10708d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 1071fb59c426SYoshinobu Inoue */ 1072be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 1073bf6d304aSAndre Oppermann /* 1074bf6d304aSAndre Oppermann * Parse the TCP options here because 1075bf6d304aSAndre Oppermann * syncookies need access to the reflected 1076bf6d304aSAndre Oppermann * timestamp. 1077bf6d304aSAndre Oppermann */ 1078bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 10799fa198beSAndre Oppermann /* 10809fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 10819fa198beSAndre Oppermann * inp and tcpinfo locks. 10829fa198beSAndre Oppermann */ 1083bf6d304aSAndre Oppermann if (!syncache_expand(&inc, &to, th, &so, m)) { 10844195b4afSPaul Traina /* 1085e207f800SAndre Oppermann * No syncache entry or ACK was not 1086be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 10878d573cc1SAndre Oppermann * NB: syncache did its own logging 10888d573cc1SAndre Oppermann * of the failure cause. 10894195b4afSPaul Traina */ 1090be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 1091be2ac88cSJonathan Lemon goto dropwithreset; 1092be2ac88cSJonathan Lemon } 1093f76fcf6dSJeffrey Hsu if (so == NULL) { 1094be2ac88cSJonathan Lemon /* 1095e207f800SAndre Oppermann * We completed the 3-way handshake 1096e207f800SAndre Oppermann * but could not allocate a socket 1097e207f800SAndre Oppermann * either due to memory shortage, 1098e207f800SAndre Oppermann * listen queue length limits or 1099a160e630SAndre Oppermann * global socket limits. Send RST 1100a160e630SAndre Oppermann * or wait and have the remote end 1101a160e630SAndre Oppermann * retransmit the ACK for another 1102a160e630SAndre Oppermann * try. 1103be2ac88cSJonathan Lemon */ 1104a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1105a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11068d573cc1SAndre Oppermann "Socket allocation failed due to " 11078d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 1108ac957cd2SJulian Elischer s, __func__, 1109ac957cd2SJulian Elischer V_tcp_sc_rst_sock_fail ? 1110ac957cd2SJulian Elischer "sending RST" : "try again"); 1111603724d3SBjoern A. Zeeb if (V_tcp_sc_rst_sock_fail) { 1112e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 1113e207f800SAndre Oppermann goto dropwithreset; 1114a160e630SAndre Oppermann } else 1115a160e630SAndre Oppermann goto dropunlock; 1116f76fcf6dSJeffrey Hsu } 1117be2ac88cSJonathan Lemon /* 1118be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 11198d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 11208d573cc1SAndre Oppermann * created socket and update the tp variable. 1121be2ac88cSJonathan Lemon */ 11228501a69cSRobert Watson INP_WUNLOCK(inp); /* listen socket */ 1123be2ac88cSJonathan Lemon inp = sotoinpcb(so); 11248501a69cSRobert Watson INP_WLOCK(inp); /* new connection */ 1125be2ac88cSJonathan Lemon tp = intotcpcb(inp); 11268d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 11278d573cc1SAndre Oppermann ("%s: ", __func__)); 11282903309aSAttilio Rao #ifdef TCP_SIGNATURE 11292903309aSAttilio Rao if (sig_checked == 0) { 11302903309aSAttilio Rao tcp_dooptions(&to, optp, optlen, 11312903309aSAttilio Rao (thflags & TH_SYN) ? TO_SYN : 0); 11322903309aSAttilio Rao if (!tcp_signature_verify_input(m, off0, tlen, 11332903309aSAttilio Rao optlen, &to, th, tp->t_flags)) { 11342903309aSAttilio Rao 11352903309aSAttilio Rao /* 11362903309aSAttilio Rao * In SYN_SENT state if it receives an 11372903309aSAttilio Rao * RST, it is allowed for further 11382903309aSAttilio Rao * processing. 11392903309aSAttilio Rao */ 11402903309aSAttilio Rao if ((thflags & TH_RST) == 0 || 11412903309aSAttilio Rao (tp->t_state == TCPS_SYN_SENT) == 0) 11422903309aSAttilio Rao goto dropunlock; 11432903309aSAttilio Rao } 11442903309aSAttilio Rao sig_checked = 1; 11452903309aSAttilio Rao } 11462903309aSAttilio Rao #endif 11472903309aSAttilio Rao 1148be2ac88cSJonathan Lemon /* 1149302ce8d6SAndre Oppermann * Process the segment and the data it 1150302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 1151302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 1152302ce8d6SAndre Oppermann */ 1153f2512ba1SRui Paulo tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 1154252ca428SRobert Watson iptos, ti_locked); 1155603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1156302ce8d6SAndre Oppermann return; 1157be2ac88cSJonathan Lemon } 1158a160e630SAndre Oppermann /* 11598d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 11608d573cc1SAndre Oppermann * 1161a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 1162a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 1163a160e630SAndre Oppermann * retransmits. 116419bc77c5SAndre Oppermann * 116519bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 116619bc77c5SAndre Oppermann * causes. 1167a160e630SAndre Oppermann */ 1168a160e630SAndre Oppermann if (thflags & TH_RST) { 116919bc77c5SAndre Oppermann syncache_chkrst(&inc, th); 1170a160e630SAndre Oppermann goto dropunlock; 1171a160e630SAndre Oppermann } 1172a160e630SAndre Oppermann /* 1173a160e630SAndre Oppermann * We can't do anything without SYN. 1174a160e630SAndre Oppermann */ 1175a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 1176a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1177a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1178773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 11798d573cc1SAndre Oppermann s, __func__); 118078b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1181a160e630SAndre Oppermann goto dropunlock; 1182a160e630SAndre Oppermann } 1183a160e630SAndre Oppermann /* 1184a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 1185a160e630SAndre Oppermann */ 1186fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1187a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1188a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11898d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 11908d573cc1SAndre Oppermann s, __func__); 1191a160e630SAndre Oppermann syncache_badack(&inc); /* XXX: Not needed! */ 119278b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1193a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1194a57815efSBosko Milekic goto dropwithreset; 11954195b4afSPaul Traina } 1196a160e630SAndre Oppermann /* 1197a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 1198a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 1199a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 1200a160e630SAndre Oppermann * TCP/IP stack. 1201a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 1202a160e630SAndre Oppermann * and is constantly refining its stack detection 1203a160e630SAndre Oppermann * strategies. 12048d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 1205a160e630SAndre Oppermann * and was used by RFC1644. 1206a160e630SAndre Oppermann */ 1207603724d3SBjoern A. Zeeb if ((thflags & TH_FIN) && V_drop_synfin) { 1208a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1209a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1210773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 12118d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 121278b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1213302ce8d6SAndre Oppermann goto dropunlock; 1214ebb0cbeaSPaul Traina } 1215be2ac88cSJonathan Lemon /* 1216be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 1217a160e630SAndre Oppermann * 1218a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 1219a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 1220a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 1221be2ac88cSJonathan Lemon */ 1222a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 1223a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 1224a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 1225a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 122633841545SHajimu UMEMOTO #ifdef INET6 122733841545SHajimu UMEMOTO /* 122833841545SHajimu UMEMOTO * If deprecated address is forbidden, 122933841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 123033841545SHajimu UMEMOTO * address to prevent any new inbound connection from 123133841545SHajimu UMEMOTO * getting established. 123233841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 123333841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 123433841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 123533841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 123633841545SHajimu UMEMOTO * for the exchange. 123733841545SHajimu UMEMOTO * 123833841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 123933841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 124033841545SHajimu UMEMOTO * SYN in this case. 124133841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 124233841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 124333841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 124433841545SHajimu UMEMOTO * used" 124533841545SHajimu UMEMOTO * 2. use of it with new communication: 124633841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 124733841545SHajimu UMEMOTO * with sufficient scope is available" 124833841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 124933841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 125033841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 125133841545SHajimu UMEMOTO * 125233841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 125333841545SHajimu UMEMOTO * multiple description text for deprecated address 125433841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 125533841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 125633841545SHajimu UMEMOTO */ 1257603724d3SBjoern A. Zeeb if (isipv6 && !V_ip6_use_deprecated) { 125833841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 125933841545SHajimu UMEMOTO 12608c0fec80SRobert Watson ia6 = ip6_getdstifaddr(m); 12618c0fec80SRobert Watson if (ia6 != NULL && 126233841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 12638c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 1264a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1265a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12668d573cc1SAndre Oppermann "Connection attempt to deprecated " 12678d573cc1SAndre Oppermann "IPv6 address rejected\n", 1268a160e630SAndre Oppermann s, __func__); 126933841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 127033841545SHajimu UMEMOTO goto dropwithreset; 127133841545SHajimu UMEMOTO } 127277d396fdSMaksim Yevmenkin if (ia6) 12738c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 127433841545SHajimu UMEMOTO } 1275b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 127665f28919SJesper Skriver /* 1277db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 12788d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 1279db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 12808d573cc1SAndre Oppermann * If it is from this socket it must be forged. 12818d573cc1SAndre Oppermann * Don't respond if the source or destination is a 12828d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 128393ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 128493ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 128593ec91baSCrist J. Clark * in_broadcast() to find them. 1286be2ac88cSJonathan Lemon */ 12878d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 12888d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 12898d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12908d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 1291773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 1292302ce8d6SAndre Oppermann goto dropunlock; 12938d573cc1SAndre Oppermann } 1294db33b3e6SAndre Oppermann #ifdef INET6 1295b287c6c7SBjoern A. Zeeb if (isipv6) { 1296db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1297a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 1298a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1299a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13008d573cc1SAndre Oppermann "Connection attempt to/from self " 1301773673c1SAndre Oppermann "ignored\n", s, __func__); 1302302ce8d6SAndre Oppermann goto dropunlock; 1303a160e630SAndre Oppermann } 1304be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1305a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 1306a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1307a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13088d573cc1SAndre Oppermann "Connection attempt from/to multicast " 1309773673c1SAndre Oppermann "address ignored\n", s, __func__); 1310302ce8d6SAndre Oppermann goto dropunlock; 1311a160e630SAndre Oppermann } 1312b287c6c7SBjoern A. Zeeb } 1313db33b3e6SAndre Oppermann #endif 1314b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 1315b287c6c7SBjoern A. Zeeb else 1316b287c6c7SBjoern A. Zeeb #endif 1317b287c6c7SBjoern A. Zeeb #ifdef INET 1318b287c6c7SBjoern A. Zeeb { 1319db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1320a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 1321a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1322a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13238d573cc1SAndre Oppermann "Connection attempt from/to self " 1324773673c1SAndre Oppermann "ignored\n", s, __func__); 1325302ce8d6SAndre Oppermann goto dropunlock; 1326a160e630SAndre Oppermann } 1327be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1328be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 13292ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 1330a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 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 broad- " 1334773673c1SAndre Oppermann "or multicast address ignored\n", 1335a160e630SAndre Oppermann s, __func__); 1336302ce8d6SAndre Oppermann goto dropunlock; 1337c068736aSJeffrey Hsu } 1338a160e630SAndre Oppermann } 1339b287c6c7SBjoern A. Zeeb #endif 1340be2ac88cSJonathan Lemon /* 1341db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 1342db33b3e6SAndre Oppermann * for syncache. 1343be2ac88cSJonathan Lemon */ 13443c653157SHartmut Brandt #ifdef TCPDEBUG 13453c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 13463c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 13473c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 13483c653157SHartmut Brandt #endif 1349f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 135009fe6320SNavdeep Parhar syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL); 1351be2ac88cSJonathan Lemon /* 13524d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 1353a160e630SAndre Oppermann * Everything already unlocked by syncache_add(). 1354be2ac88cSJonathan Lemon */ 1355603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1356be2ac88cSJonathan Lemon return; 1357982c1675SAndre Oppermann } else if (tp->t_state == TCPS_LISTEN) { 1358982c1675SAndre Oppermann /* 1359982c1675SAndre Oppermann * When a listen socket is torn down the SO_ACCEPTCONN 1360982c1675SAndre Oppermann * flag is removed first while connections are drained 1361982c1675SAndre Oppermann * from the accept queue in a unlock/lock cycle of the 1362982c1675SAndre Oppermann * ACCEPT_LOCK, opening a race condition allowing a SYN 1363982c1675SAndre Oppermann * attempt go through unhandled. 1364982c1675SAndre Oppermann */ 1365982c1675SAndre Oppermann goto dropunlock; 1366f76fcf6dSJeffrey Hsu } 1367db33b3e6SAndre Oppermann 13682903309aSAttilio Rao #ifdef TCP_SIGNATURE 13692903309aSAttilio Rao if (sig_checked == 0) { 13702903309aSAttilio Rao tcp_dooptions(&to, optp, optlen, 13712903309aSAttilio Rao (thflags & TH_SYN) ? TO_SYN : 0); 13722903309aSAttilio Rao if (!tcp_signature_verify_input(m, off0, tlen, optlen, &to, 13732903309aSAttilio Rao th, tp->t_flags)) { 13742903309aSAttilio Rao 13752903309aSAttilio Rao /* 13762903309aSAttilio Rao * In SYN_SENT state if it receives an RST, it is 13772903309aSAttilio Rao * allowed for further processing. 13782903309aSAttilio Rao */ 13792903309aSAttilio Rao if ((thflags & TH_RST) == 0 || 13802903309aSAttilio Rao (tp->t_state == TCPS_SYN_SENT) == 0) 13812903309aSAttilio Rao goto dropunlock; 13822903309aSAttilio Rao } 13832903309aSAttilio Rao sig_checked = 1; 13842903309aSAttilio Rao } 13852903309aSAttilio Rao #endif 13862903309aSAttilio Rao 1387302ce8d6SAndre Oppermann /* 13888d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 13898d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 13908d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 1391302ce8d6SAndre Oppermann */ 1392252ca428SRobert Watson tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked); 1393603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1394302ce8d6SAndre Oppermann return; 1395be2ac88cSJonathan Lemon 1396302ce8d6SAndre Oppermann dropwithreset: 1397fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) { 1398dd8ac7f9SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1399252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1400fa046d87SRobert Watson } 1401fa046d87SRobert Watson #ifdef INVARIANTS 1402fa046d87SRobert Watson else { 1403fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset " 1404fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1405fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1406fa046d87SRobert Watson } 1407fa046d87SRobert Watson #endif 1408014ea782SRobert Watson 1409014ea782SRobert Watson if (inp != NULL) { 1410302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 1411014ea782SRobert Watson INP_WUNLOCK(inp); 1412dd8ac7f9SRobert Watson } else 1413014ea782SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1414302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 1415014ea782SRobert Watson goto drop; 1416014ea782SRobert Watson 1417302ce8d6SAndre Oppermann dropunlock: 1418fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) { 1419252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1420252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1421fa046d87SRobert Watson } 1422fa046d87SRobert Watson #ifdef INVARIANTS 1423fa046d87SRobert Watson else { 1424fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock " 1425fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1426fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1427fa046d87SRobert Watson } 1428fa046d87SRobert Watson #endif 1429252ca428SRobert Watson 14309fa198beSAndre Oppermann if (inp != NULL) 14318501a69cSRobert Watson INP_WUNLOCK(inp); 1432014ea782SRobert Watson 1433302ce8d6SAndre Oppermann drop: 1434603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1435a160e630SAndre Oppermann if (s != NULL) 1436a160e630SAndre Oppermann free(s, M_TCPLOG); 1437302ce8d6SAndre Oppermann if (m != NULL) 1438302ce8d6SAndre Oppermann m_freem(m); 1439302ce8d6SAndre Oppermann } 1440302ce8d6SAndre Oppermann 1441679d9708SAndre Oppermann static void 1442302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 1443252ca428SRobert Watson struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos, 1444252ca428SRobert Watson int ti_locked) 1445302ce8d6SAndre Oppermann { 1446302ce8d6SAndre Oppermann int thflags, acked, ourfinisacked, needoutput = 0; 1447302ce8d6SAndre Oppermann int rstreason, todrop, win; 1448302ce8d6SAndre Oppermann u_long tiwin; 1449*07dacf03SAndre Oppermann char *s; 1450*07dacf03SAndre Oppermann struct in_conninfo *inc; 1451302ce8d6SAndre Oppermann struct tcpopt to; 1452302ce8d6SAndre Oppermann 145314739780SMaxim Konovalov #ifdef TCPDEBUG 145414739780SMaxim Konovalov /* 145514739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 145614739780SMaxim Konovalov * now IPv6. 145714739780SMaxim Konovalov */ 145814739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 145914739780SMaxim Konovalov struct tcphdr tcp_savetcp; 146014739780SMaxim Konovalov short ostate = 0; 146114739780SMaxim Konovalov #endif 1462302ce8d6SAndre Oppermann thflags = th->th_flags; 1463*07dacf03SAndre Oppermann inc = &tp->t_inpcb->inp_inc; 1464d64a46eaSLawrence Stewart tp->sackhint.last_sack_ack = 0; 1465302ce8d6SAndre Oppermann 1466252ca428SRobert Watson /* 1467252ca428SRobert Watson * If this is either a state-changing packet or current state isn't 1468252ca428SRobert Watson * established, we require a write lock on tcbinfo. Otherwise, we 14690989f56cSRobert Watson * allow the tcbinfo to be in either alocked or unlocked, as the 14700989f56cSRobert Watson * caller may have unnecessarily acquired a write lock due to a race. 1471252ca428SRobert Watson */ 1472252ca428SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 1473252ca428SRobert Watson tp->t_state != TCPS_ESTABLISHED) { 1474252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s ti_locked %d for " 1475252ca428SRobert Watson "SYN/FIN/RST/!EST", __func__, ti_locked)); 1476603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1477252ca428SRobert Watson } else { 1478252ca428SRobert Watson #ifdef INVARIANTS 1479fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 1480252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1481fa046d87SRobert Watson else { 1482fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST " 1483fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1484fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1485fa046d87SRobert Watson } 1486252ca428SRobert Watson #endif 1487252ca428SRobert Watson } 14888501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1489679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1490679d9708SAndre Oppermann __func__)); 1491679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1492679d9708SAndre Oppermann __func__)); 1493df8bae1dSRodney W. Grimes 1494df8bae1dSRodney W. Grimes /* 1495df8bae1dSRodney W. Grimes * Segment received on connection. 1496df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1497e8949f74SAndre Oppermann * XXX: This should be done after segment 1498e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 1499df8bae1dSRodney W. Grimes */ 15009b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 15017ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 15029077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 1503df8bae1dSRodney W. Grimes 1504df8bae1dSRodney W. Grimes /* 1505464fcfbcSAndre Oppermann * Unscale the window into a 32-bit value. 1506e8949f74SAndre Oppermann * For the SYN_SENT state the scale is zero. 1507464fcfbcSAndre Oppermann */ 1508464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 1509464fcfbcSAndre Oppermann 1510464fcfbcSAndre Oppermann /* 1511f2512ba1SRui Paulo * TCP ECN processing. 1512f2512ba1SRui Paulo */ 1513f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) { 15149c251892SRui Paulo if (thflags & TH_CWR) 15159c251892SRui Paulo tp->t_flags &= ~TF_ECN_SND_ECE; 1516f2512ba1SRui Paulo switch (iptos & IPTOS_ECN_MASK) { 1517f2512ba1SRui Paulo case IPTOS_ECN_CE: 1518f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_ECE; 151978b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ce); 1520f2512ba1SRui Paulo break; 1521f2512ba1SRui Paulo case IPTOS_ECN_ECT0: 152278b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect0); 1523f2512ba1SRui Paulo break; 1524f2512ba1SRui Paulo case IPTOS_ECN_ECT1: 152578b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect1); 1526f2512ba1SRui Paulo break; 1527f2512ba1SRui Paulo } 1528dbc42409SLawrence Stewart /* Congestion experienced. */ 1529dbc42409SLawrence Stewart if (thflags & TH_ECE) { 1530dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_ECN); 1531f2512ba1SRui Paulo } 1532f2512ba1SRui Paulo } 1533f2512ba1SRui Paulo 1534f2512ba1SRui Paulo /* 1535f72167f4SAndre Oppermann * Parse options on any incoming segment. 1536f72167f4SAndre Oppermann */ 1537302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1538302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1539302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1540f72167f4SAndre Oppermann 1541f72167f4SAndre Oppermann /* 1542f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1543bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1544bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1545bf6d304aSAndre Oppermann * was established. 1546f72167f4SAndre Oppermann */ 1547bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1548e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 1549d8951c8aSBjoern A. Zeeb if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) 1550f72167f4SAndre Oppermann to.to_tsecr = 0; 1551bf6d304aSAndre Oppermann } 1552*07dacf03SAndre Oppermann /* 1553*07dacf03SAndre Oppermann * If timestamps were negotiated during SYN/ACK they should 1554*07dacf03SAndre Oppermann * appear on every segment during this session and vice versa. 1555*07dacf03SAndre Oppermann */ 1556*07dacf03SAndre Oppermann if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) { 1557*07dacf03SAndre Oppermann if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 1558*07dacf03SAndre Oppermann log(LOG_DEBUG, "%s; %s: Timestamp missing, " 1559*07dacf03SAndre Oppermann "no action\n", s, __func__); 1560*07dacf03SAndre Oppermann free(s, M_TCPLOG); 1561*07dacf03SAndre Oppermann } 1562*07dacf03SAndre Oppermann } 1563*07dacf03SAndre Oppermann if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) { 1564*07dacf03SAndre Oppermann if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 1565*07dacf03SAndre Oppermann log(LOG_DEBUG, "%s; %s: Timestamp not expected, " 1566*07dacf03SAndre Oppermann "no action\n", s, __func__); 1567*07dacf03SAndre Oppermann free(s, M_TCPLOG); 1568*07dacf03SAndre Oppermann } 1569*07dacf03SAndre Oppermann } 1570f72167f4SAndre Oppermann 1571f72167f4SAndre Oppermann /* 157297d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 157397d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 15745396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 15755396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 157697d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1577df8bae1dSRodney W. Grimes */ 15780a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1579464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 1580464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 1581be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 158202a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 1583be2ac88cSJonathan Lemon } 15845396d0f8SAndre Oppermann /* 15855396d0f8SAndre Oppermann * Initial send window. It will be updated with 15865396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 15875396d0f8SAndre Oppermann */ 15885396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 1589be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1590be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1591be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1592d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1593be2ac88cSJonathan Lemon } 1594be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1595be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 15963529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 15973529149eSAndre Oppermann (to.to_flags & TOF_SACKPERM) == 0) 15983529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 15996d90faf3SPaul Saab } 16006d90faf3SPaul Saab 1601df8bae1dSRodney W. Grimes /* 1602df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1603df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1604df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1605df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1606df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1607df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1608df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1609df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1610df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1611df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1612df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1613df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1614a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1615c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 1616a0292f23SGarrett Wollman * be TH_NEEDSYN. 1617df8bae1dSRodney W. Grimes */ 1618df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1619c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 1620fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1621c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 1622c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 1623a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1624c5ad39b9SAndre Oppermann LIST_EMPTY(&tp->t_segq) && 1625be2ac88cSJonathan Lemon ((to.to_flags & TOF_TS) == 0 || 1626c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1627df8bae1dSRodney W. Grimes 1628df8bae1dSRodney W. Grimes /* 1629df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1630df8bae1dSRodney W. Grimes * record the timestamp. 1631a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1632a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1633df8bae1dSRodney W. Grimes */ 1634be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1635fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1636d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1637a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1638df8bae1dSRodney W. Grimes } 1639df8bae1dSRodney W. Grimes 1640fb59c426SYoshinobu Inoue if (tlen == 0) { 1641fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1642fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1643dbc42409SLawrence Stewart !IN_RECOVERY(tp->t_flags) && 1644fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1645dbc42409SLawrence Stewart TAILQ_EMPTY(&tp->snd_holes)) { 1646df8bae1dSRodney W. Grimes /* 1647e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1648df8bae1dSRodney W. Grimes */ 1649fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 1650252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1651252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1652252ca428SRobert Watson 165378b50714SRobert Watson TCPSTAT_INC(tcps_predack); 1654252ca428SRobert Watson 16559b8b58e0SJonathan Lemon /* 1656e8949f74SAndre Oppermann * "bad retransmit" recovery. 16579b8b58e0SJonathan Lemon */ 16589b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 1659672dc4aeSJohn Baldwin tp->t_flags & TF_PREVVALID && 16606dfb8b31SJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) { 1661dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 16629b8b58e0SJonathan Lemon } 1663fa55172bSMatthew Dillon 1664fa55172bSMatthew Dillon /* 1665fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1666fa55172bSMatthew Dillon * 1667fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1668fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1669fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1670fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1671fa55172bSMatthew Dillon */ 1672fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1673fa55172bSMatthew Dillon to.to_tsecr) { 1674d8951c8aSBjoern A. Zeeb u_int t; 1675d8951c8aSBjoern A. Zeeb 1676d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 1677d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 1678d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 1679a0292f23SGarrett Wollman tcp_xmit_timer(tp, 1680d8951c8aSBjoern A. Zeeb TCP_TS_TO_TICKS(t) + 1); 1681fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1682fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1683eaf80179SAndre Oppermann if (!tp->t_rttlow || 1684eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1685eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1686c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1687c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1688fa55172bSMatthew Dillon } 1689dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 169039bc9de5SLawrence Stewart 169139bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 169239bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 169339bc9de5SLawrence Stewart 169478b50714SRobert Watson TCPSTAT_INC(tcps_rcvackpack); 169578b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 1696df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 16979d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 16989d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 16999d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 1700dbc42409SLawrence Stewart 1701dbc42409SLawrence Stewart /* 1702dbc42409SLawrence Stewart * Let the congestion control algorithm update 1703dbc42409SLawrence Stewart * congestion control related information. This 1704dbc42409SLawrence Stewart * typically means increasing the congestion 1705dbc42409SLawrence Stewart * window. 1706dbc42409SLawrence Stewart */ 1707dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_ACK); 1708dbc42409SLawrence Stewart 17099d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 17101645d090SJeff Roberson /* 1711e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 17121645d090SJeff Roberson * to th_ack. 17131645d090SJeff Roberson */ 1714cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1715b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1716df8bae1dSRodney W. Grimes m_freem(m); 1717e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made. */ 1718df8bae1dSRodney W. Grimes 1719df8bae1dSRodney W. Grimes /* 1720df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1721df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1722df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1723df8bae1dSRodney W. Grimes * If process is waiting for space, 1724df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1725df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1726df8bae1dSRodney W. Grimes * decide between more output or persist. 1727e8949f74SAndre Oppermann */ 17283c653157SHartmut Brandt #ifdef TCPDEBUG 17293c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 17303c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 17313c653157SHartmut Brandt (void *)tcp_saveipgen, 17323c653157SHartmut Brandt &tcp_savetcp, 0); 17333c653157SHartmut Brandt #endif 1734df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1735b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1736b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1737b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1738b8152ba7SAndre Oppermann tp->t_rxtcur); 1739df8bae1dSRodney W. Grimes sowwakeup(so); 1740df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 1741df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1742a14c749fSJonathan Lemon goto check_delack; 1743df8bae1dSRodney W. Grimes } 1744fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1745fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 17466741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 17476741ecf5SAndre Oppermann 1748df8bae1dSRodney W. Grimes /* 1749252ca428SRobert Watson * This is a pure, in-sequence data packet with 1750252ca428SRobert Watson * nothing on the reassembly queue and we have enough 1751252ca428SRobert Watson * buffer space to take it. 1752df8bae1dSRodney W. Grimes */ 1753fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 1754252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1755252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1756252ca428SRobert Watson 17576d90faf3SPaul Saab /* Clean receiver SACK report if present */ 17583529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 17596d90faf3SPaul Saab tcp_clean_sackreport(tp); 176078b50714SRobert Watson TCPSTAT_INC(tcps_preddat); 1761fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 17621645d090SJeff Roberson /* 17631645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 17641645d090SJeff Roberson * th_seq. 17651645d090SJeff Roberson */ 176660ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 17671645d090SJeff Roberson /* 17681645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 17691645d090SJeff Roberson * rcv_nxt. 17701645d090SJeff Roberson */ 17711645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 177278b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 177378b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 1774e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made */ 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, &tcp_savetcp, 0); 17793c653157SHartmut Brandt #endif 17806741ecf5SAndre Oppermann /* 17816741ecf5SAndre Oppermann * Automatic sizing of receive socket buffer. Often the send 17826741ecf5SAndre Oppermann * buffer size is not optimally adjusted to the actual network 17836741ecf5SAndre Oppermann * conditions at hand (delay bandwidth product). Setting the 17846741ecf5SAndre Oppermann * buffer size too small limits throughput on links with high 17856741ecf5SAndre Oppermann * bandwidth and high delay (eg. trans-continental/oceanic links). 17866741ecf5SAndre Oppermann * 17876741ecf5SAndre Oppermann * On the receive side the socket buffer memory is only rarely 17886741ecf5SAndre Oppermann * used to any significant extent. This allows us to be much 17896741ecf5SAndre Oppermann * more aggressive in scaling the receive socket buffer. For 17906741ecf5SAndre Oppermann * the case that the buffer space is actually used to a large 17916741ecf5SAndre Oppermann * extent and we run out of kernel memory we can simply drop 17926741ecf5SAndre Oppermann * the new segments; TCP on the sender will just retransmit it 17936741ecf5SAndre Oppermann * later. Setting the buffer size too big may only consume too 17946741ecf5SAndre Oppermann * much kernel memory if the application doesn't read() from 17956741ecf5SAndre Oppermann * the socket or packet loss or reordering makes use of the 17966741ecf5SAndre Oppermann * reassembly queue. 17976741ecf5SAndre Oppermann * 17986741ecf5SAndre Oppermann * The criteria to step up the receive buffer one notch are: 17996741ecf5SAndre Oppermann * 1. the number of bytes received during the time it takes 18006741ecf5SAndre Oppermann * one timestamp to be reflected back to us (the RTT); 18016741ecf5SAndre Oppermann * 2. received bytes per RTT is within seven eighth of the 18026741ecf5SAndre Oppermann * current socket buffer size; 18036741ecf5SAndre Oppermann * 3. receive buffer size has not hit maximal automatic size; 18046741ecf5SAndre Oppermann * 18056741ecf5SAndre Oppermann * This algorithm does one step per RTT at most and only if 18066741ecf5SAndre Oppermann * we receive a bulk stream w/o packet losses or reorderings. 18076741ecf5SAndre Oppermann * Shrinking the buffer during idle times is not necessary as 18086741ecf5SAndre Oppermann * it doesn't consume any memory when idle. 18096741ecf5SAndre Oppermann * 18106741ecf5SAndre Oppermann * TODO: Only step up if the application is actually serving 18116741ecf5SAndre Oppermann * the buffer to better manage the socket buffer resources. 1812df8bae1dSRodney W. Grimes */ 1813603724d3SBjoern A. Zeeb if (V_tcp_do_autorcvbuf && 18146741ecf5SAndre Oppermann to.to_tsecr && 18156741ecf5SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 18168502ec25SAndre Oppermann if (TSTMP_GT(to.to_tsecr, tp->rfbuf_ts) && 18176741ecf5SAndre Oppermann to.to_tsecr - tp->rfbuf_ts < hz) { 18186741ecf5SAndre Oppermann if (tp->rfbuf_cnt > 18196741ecf5SAndre Oppermann (so->so_rcv.sb_hiwat / 8 * 7) && 18206741ecf5SAndre Oppermann so->so_rcv.sb_hiwat < 1821603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max) { 18226741ecf5SAndre Oppermann newsize = 18236741ecf5SAndre Oppermann min(so->so_rcv.sb_hiwat + 1824603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_inc, 1825603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max); 18266741ecf5SAndre Oppermann } 18276741ecf5SAndre Oppermann /* Start over with next RTT. */ 18286741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 18296741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 18306741ecf5SAndre Oppermann } else 18316741ecf5SAndre Oppermann tp->rfbuf_cnt += tlen; /* add up */ 18326741ecf5SAndre Oppermann } 18336741ecf5SAndre Oppermann 18346741ecf5SAndre Oppermann /* Add data to socket buffer. */ 18351e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1836c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1837c1c36a2cSMike Silbersack m_freem(m); 1838c1c36a2cSMike Silbersack } else { 18396741ecf5SAndre Oppermann /* 18406741ecf5SAndre Oppermann * Set new socket buffer size. 18416741ecf5SAndre Oppermann * Give up when limit is reached. 18426741ecf5SAndre Oppermann */ 18436741ecf5SAndre Oppermann if (newsize) 18446741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 18456c8286e4SRobert Watson newsize, so, NULL)) 18466741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1847fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 18481e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 1849c1c36a2cSMike Silbersack } 1850e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 18511e4d7da7SRobert Watson sorwakeup_locked(so); 1852d8c85a26SJonathan Lemon if (DELAY_ACK(tp)) { 18533bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1854f498eeeeSDavid Greenman } else { 1855e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 1856e612a582SDavid Greenman tcp_output(tp); 1857e612a582SDavid Greenman } 1858a14c749fSJonathan Lemon goto check_delack; 1859df8bae1dSRodney W. Grimes } 1860df8bae1dSRodney W. Grimes } 1861df8bae1dSRodney W. Grimes 1862df8bae1dSRodney W. Grimes /* 1863df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1864df8bae1dSRodney W. Grimes * and then do TCP input processing. 1865df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1866df8bae1dSRodney W. Grimes * but not less than advertised window. 1867df8bae1dSRodney W. Grimes */ 1868df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1869df8bae1dSRodney W. Grimes if (win < 0) 1870df8bae1dSRodney W. Grimes win = 0; 187166e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1872df8bae1dSRodney W. Grimes 18736741ecf5SAndre Oppermann /* Reset receive buffer auto scaling when not in bulk receive mode. */ 18746741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 18756741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 18766741ecf5SAndre Oppermann 1877df8bae1dSRodney W. Grimes switch (tp->t_state) { 1878df8bae1dSRodney W. Grimes 1879df8bae1dSRodney W. Grimes /* 1880764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1881764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1882764d8cefSBill Fenner */ 1883764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1884fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1885fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1886a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1887a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1888a57815efSBosko Milekic goto dropwithreset; 1889a57815efSBosko Milekic } 1890764d8cefSBill Fenner break; 1891764d8cefSBill Fenner 1892764d8cefSBill Fenner /* 1893df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1894df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1895df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1896df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1897df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1898df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1899df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1900f2512ba1SRui Paulo * if seg contains an ECE and ECN support is enabled, the stream 1901f2512ba1SRui Paulo * is ECN capable. 1902df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1903df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1904df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1905df8bae1dSRodney W. Grimes */ 1906df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1907fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1908fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1909fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1910a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1911a0292f23SGarrett Wollman goto dropwithreset; 1912a0292f23SGarrett Wollman } 19130ca3f933SAndre Oppermann if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) 1914df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 19150ca3f933SAndre Oppermann if (thflags & TH_RST) 1916df8bae1dSRodney W. Grimes goto drop; 19170ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 1918df8bae1dSRodney W. Grimes goto drop; 1919464fcfbcSAndre Oppermann 1920fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1921df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1922fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 192378b50714SRobert Watson TCPSTAT_INC(tcps_connects); 1924845799c1SAndras Olah soisconnected(so); 1925c488362eSRobert Watson #ifdef MAC 192630d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 1927c488362eSRobert Watson #endif 1928845799c1SAndras Olah /* Do window scaling on this connection? */ 1929845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1930845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1931845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1932845799c1SAndras Olah } 1933766282cbSJohn Baldwin tp->rcv_adv += imin(tp->rcv_wnd, 1934766282cbSJohn Baldwin TCP_MAXWIN << tp->rcv_scale); 1935a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1936a0292f23SGarrett Wollman /* 1937a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1938a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1939a0292f23SGarrett Wollman */ 1940d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && tlen != 0) 1941b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 1942b8152ba7SAndre Oppermann tcp_delacktime); 1943a0292f23SGarrett Wollman else 1944a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1945f2512ba1SRui Paulo 1946603724d3SBjoern A. Zeeb if ((thflags & TH_ECE) && V_tcp_do_ecn) { 1947f2512ba1SRui Paulo tp->t_flags |= TF_ECN_PERMIT; 194878b50714SRobert Watson TCPSTAT_INC(tcps_ecn_shs); 1949f2512ba1SRui Paulo } 1950f2512ba1SRui Paulo 1951a0292f23SGarrett Wollman /* 1952a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1953a0292f23SGarrett Wollman * Transitions: 1954a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1955a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1956a0292f23SGarrett Wollman */ 19579b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1958a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1959a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1960a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1961fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 19627ff19458SPaul Traina } else { 1963a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1964dbc42409SLawrence Stewart cc_conn_init(tp); 19659077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 19669077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 19677ff19458SPaul Traina } 1968a0292f23SGarrett Wollman } else { 1969a0292f23SGarrett Wollman /* 1970c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 1971c068736aSJeffrey Hsu * simultaneous open. If segment contains CC option 1972c068736aSJeffrey Hsu * and there is a cached CC, apply TAO test. 1973c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 1974c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 1975a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1976a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1977a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1978a0292f23SGarrett Wollman */ 19794b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 1980b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1981df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1982a0292f23SGarrett Wollman } 1983df8bae1dSRodney W. Grimes 1984252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: trimthenstep6: " 1985252ca428SRobert Watson "ti_locked %d", __func__, ti_locked)); 1986252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 19878501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 19887cfc6904SRobert Watson 1989df8bae1dSRodney W. Grimes /* 1990fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1991df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1992df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1993df8bae1dSRodney W. Grimes */ 1994fb59c426SYoshinobu Inoue th->th_seq++; 1995fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1996fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1997df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1998fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1999fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 200078b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 200178b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2002df8bae1dSRodney W. Grimes } 2003fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 2004fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 2005a0292f23SGarrett Wollman /* 2006a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 2007a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 2008a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 2009a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 2010a0292f23SGarrett Wollman * Otherwise, goto step 6. 2011a0292f23SGarrett Wollman */ 2012fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 2013a0292f23SGarrett Wollman goto process_ACK; 2014c068736aSJeffrey Hsu 2015df8bae1dSRodney W. Grimes goto step6; 2016c068736aSJeffrey Hsu 2017a0292f23SGarrett Wollman /* 2018a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 2019c94c54e4SAndre Oppermann * do normal processing. 2020a0292f23SGarrett Wollman * 2021c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 2022a0292f23SGarrett Wollman */ 2023a0292f23SGarrett Wollman case TCPS_LAST_ACK: 2024a0292f23SGarrett Wollman case TCPS_CLOSING: 2025a0292f23SGarrett Wollman break; /* continue normal processing */ 2026df8bae1dSRodney W. Grimes } 2027df8bae1dSRodney W. Grimes 2028df8bae1dSRodney W. Grimes /* 2029df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 203080ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 203180ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 203280ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 203380ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 203480ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 203580ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 203680ab7c0eSGarrett Wollman * killing a connection). 203780ab7c0eSGarrett Wollman * Then check timestamp, if present. 2038a0292f23SGarrett Wollman * Then check the connection count, if present. 2039df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 2040df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 2041df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 2042df8bae1dSRodney W. Grimes * 204380ab7c0eSGarrett Wollman * 204480ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 204580ab7c0eSGarrett Wollman * if this is a valid reset segment. 204680ab7c0eSGarrett Wollman * RFC 793 page 37: 204780ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 204880ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 204980ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 205080ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 205180ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 20521a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 20531a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 20541a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 20551a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 205680dd2a81SMike Silbersack * Note 2: Paul Watson's paper "Slipping in the Window" has shown 205780dd2a81SMike Silbersack * that brute force RST attacks are possible. To combat this, 205880dd2a81SMike Silbersack * we use a much stricter check while in the ESTABLISHED state, 205980dd2a81SMike Silbersack * only accepting RSTs where the sequence number is equal to 206080dd2a81SMike Silbersack * last_ack_sent. In all other states (the states in which a 206180dd2a81SMike Silbersack * RST is more likely), the more permissive check is used. 20628e5c87f4SBjoern A. Zeeb * If we have multiple segments in flight, the initial reset 206380ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 206480ab7c0eSGarrett Wollman * but they will eventually catch up. 206580ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 206680ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 206780ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 206880ab7c0eSGarrett Wollman * 206980ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 207080ab7c0eSGarrett Wollman * 207180ab7c0eSGarrett Wollman * DISCUSSION 207280ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 207380ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 207480ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 207580ab7c0eSGarrett Wollman * data. 207680ab7c0eSGarrett Wollman * 207780ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 207880ab7c0eSGarrett Wollman * the state: 207980ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 208080ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 208180ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 2082745bab7fSDima Dorfman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 208380ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 2084e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 208580ab7c0eSGarrett Wollman * Close the tcb. 2086e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 208780ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 208880ab7c0eSGarrett Wollman * RFC 1337. 208980ab7c0eSGarrett Wollman */ 2090fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 209195ad8418SQing Li if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 209295ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 209380ab7c0eSGarrett Wollman switch (tp->t_state) { 209480ab7c0eSGarrett Wollman 209580ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 209680ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 209780ab7c0eSGarrett Wollman goto close; 209880ab7c0eSGarrett Wollman 209980ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 2100603724d3SBjoern A. Zeeb if (V_tcp_insecure_rst == 0 && 210195ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) && 210295ad8418SQing Li SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) && 210395ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 210495ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) { 210578b50714SRobert Watson TCPSTAT_INC(tcps_badrst); 210680dd2a81SMike Silbersack goto drop; 210780dd2a81SMike Silbersack } 2108564aab1fSAndre Oppermann /* FALLTHROUGH */ 210980ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 211080ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 211180ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 211280ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 211380ab7c0eSGarrett Wollman close: 2114252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 2115252ca428SRobert Watson ("tcp_do_segment: TH_RST 1 ti_locked %d", 2116252ca428SRobert Watson ti_locked)); 2117252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2118252ca428SRobert Watson 211980ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 212078b50714SRobert Watson TCPSTAT_INC(tcps_drops); 212180ab7c0eSGarrett Wollman tp = tcp_close(tp); 212280ab7c0eSGarrett Wollman break; 212380ab7c0eSGarrett Wollman 212480ab7c0eSGarrett Wollman case TCPS_CLOSING: 212580ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 2126252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 2127252ca428SRobert Watson ("tcp_do_segment: TH_RST 2 ti_locked %d", 2128252ca428SRobert Watson ti_locked)); 2129252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2130252ca428SRobert Watson 213180ab7c0eSGarrett Wollman tp = tcp_close(tp); 213280ab7c0eSGarrett Wollman break; 213380ab7c0eSGarrett Wollman } 213480ab7c0eSGarrett Wollman } 213580ab7c0eSGarrett Wollman goto drop; 213680ab7c0eSGarrett Wollman } 213780ab7c0eSGarrett Wollman 213880ab7c0eSGarrett Wollman /* 2139df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 2140df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 2141df8bae1dSRodney W. Grimes */ 2142be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 214380ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 2144df8bae1dSRodney W. Grimes 2145df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 2146d8951c8aSBjoern A. Zeeb if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 2147df8bae1dSRodney W. Grimes /* 2148df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 2149df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 2150df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 2151df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 2152df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 2153df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 2154df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 2155df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 2156df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 2157df8bae1dSRodney W. Grimes */ 2158df8bae1dSRodney W. Grimes tp->ts_recent = 0; 2159df8bae1dSRodney W. Grimes } else { 216078b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 216178b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 216278b50714SRobert Watson TCPSTAT_INC(tcps_pawsdrop); 216307fd333dSMatthew Dillon if (tlen) 2164df8bae1dSRodney W. Grimes goto dropafterack; 21651ab4789dSMatthew Dillon goto drop; 21661ab4789dSMatthew Dillon } 2167df8bae1dSRodney W. Grimes } 2168df8bae1dSRodney W. Grimes 2169a0292f23SGarrett Wollman /* 217080ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 217180ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 217280ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 217380ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 217480ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 217580ab7c0eSGarrett Wollman */ 2176a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 2177a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2178a57815efSBosko Milekic goto dropwithreset; 2179a57815efSBosko Milekic } 218080ab7c0eSGarrett Wollman 2181fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 2182df8bae1dSRodney W. Grimes if (todrop > 0) { 218381ad7eb0SZachary Loafman /* 218481ad7eb0SZachary Loafman * If this is a duplicate SYN for our current connection, 218581ad7eb0SZachary Loafman * advance over it and pretend and it's not a SYN. 218681ad7eb0SZachary Loafman */ 218781ad7eb0SZachary Loafman if (thflags & TH_SYN && th->th_seq == tp->irs) { 2188fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 2189fb59c426SYoshinobu Inoue th->th_seq++; 2190fb59c426SYoshinobu Inoue if (th->th_urp > 1) 2191fb59c426SYoshinobu Inoue th->th_urp--; 2192df8bae1dSRodney W. Grimes else 2193fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2194df8bae1dSRodney W. Grimes todrop--; 2195df8bae1dSRodney W. Grimes } 2196df8bae1dSRodney W. Grimes /* 2197dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 2198df8bae1dSRodney W. Grimes */ 2199fb59c426SYoshinobu Inoue if (todrop > tlen 2200fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 2201dac20301SGarrett Wollman /* 2202dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 2203dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 2204dac20301SGarrett Wollman * of sequence; drop it. 2205dac20301SGarrett Wollman */ 2206fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2207dac20301SGarrett Wollman 2208df8bae1dSRodney W. Grimes /* 2209dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 2210dac20301SGarrett Wollman * But keep on processing for RST or ACK. 2211df8bae1dSRodney W. Grimes */ 2212dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2213fb59c426SYoshinobu Inoue todrop = tlen; 221478b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 221578b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 2216df8bae1dSRodney W. Grimes } else { 221778b50714SRobert Watson TCPSTAT_INC(tcps_rcvpartduppack); 221878b50714SRobert Watson TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 2219df8bae1dSRodney W. Grimes } 2220fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 2221fb59c426SYoshinobu Inoue th->th_seq += todrop; 2222fb59c426SYoshinobu Inoue tlen -= todrop; 2223fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 2224fb59c426SYoshinobu Inoue th->th_urp -= todrop; 2225df8bae1dSRodney W. Grimes else { 2226fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2227fb59c426SYoshinobu Inoue th->th_urp = 0; 2228df8bae1dSRodney W. Grimes } 2229df8bae1dSRodney W. Grimes } 2230df8bae1dSRodney W. Grimes 2231df8bae1dSRodney W. Grimes /* 2232df8bae1dSRodney W. Grimes * If new data are received on a connection after the 2233df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 2234df8bae1dSRodney W. Grimes */ 2235df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 2236fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 2237252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: SS_NOFDEREF && " 2238252ca428SRobert Watson "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked)); 2239252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2240252ca428SRobert Watson 2241*07dacf03SAndre Oppermann if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 2242*07dacf03SAndre Oppermann log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data " 2243*07dacf03SAndre Oppermann "after socket was closed, " 2244*07dacf03SAndre Oppermann "sending RST and removing tcpcb\n", 2245e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 2246773673c1SAndre Oppermann free(s, M_TCPLOG); 2247773673c1SAndre Oppermann } 2248df8bae1dSRodney W. Grimes tp = tcp_close(tp); 224978b50714SRobert Watson TCPSTAT_INC(tcps_rcvafterclose); 2250a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2251df8bae1dSRodney W. Grimes goto dropwithreset; 2252df8bae1dSRodney W. Grimes } 2253df8bae1dSRodney W. Grimes 2254df8bae1dSRodney W. Grimes /* 2255df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 2256df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 2257df8bae1dSRodney W. Grimes */ 2258fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 2259df8bae1dSRodney W. Grimes if (todrop > 0) { 226078b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 2261fb59c426SYoshinobu Inoue if (todrop >= tlen) { 226278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 2263df8bae1dSRodney W. Grimes /* 2264df8bae1dSRodney W. Grimes * If window is closed can only take segments at 2265df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 2266df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 2267df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 2268df8bae1dSRodney W. Grimes * and ack. 2269df8bae1dSRodney W. Grimes */ 2270fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2271df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 227278b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinprobe); 2273df8bae1dSRodney W. Grimes } else 2274df8bae1dSRodney W. Grimes goto dropafterack; 2275df8bae1dSRodney W. Grimes } else 227678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2277df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2278fb59c426SYoshinobu Inoue tlen -= todrop; 2279fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 2280df8bae1dSRodney W. Grimes } 2281df8bae1dSRodney W. Grimes 2282df8bae1dSRodney W. Grimes /* 2283df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 2284df8bae1dSRodney W. Grimes * record its timestamp. 2285cf09195bSPaul Saab * NOTE: 2286cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 2287a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2288cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 2289cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 2290cf09195bSPaul Saab * predicated on the sequence space of this segment. 2291cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 2292cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 2293cf09195bSPaul Saab * instead of RFC1323's 2294cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 2295cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 2296cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 2297cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 2298cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 2299df8bae1dSRodney W. Grimes */ 2300be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 2301cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2302cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 2303cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 2304d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 2305a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 2306df8bae1dSRodney W. Grimes } 2307df8bae1dSRodney W. Grimes 2308df8bae1dSRodney W. Grimes /* 2309df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 2310df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 2311df8bae1dSRodney W. Grimes */ 2312fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 2313252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 2314252ca428SRobert Watson ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked)); 2315252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2316252ca428SRobert Watson 2317df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 2318a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2319122aad88SAndre Oppermann goto drop; 2320df8bae1dSRodney W. Grimes } 2321df8bae1dSRodney W. Grimes 2322a0292f23SGarrett Wollman /* 2323a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 2324a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 2325a0292f23SGarrett Wollman * later processing; else drop segment and return. 2326a0292f23SGarrett Wollman */ 2327fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 2328a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 2329a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 2330a0292f23SGarrett Wollman goto step6; 23315e1aa279SDavid Malone else if (tp->t_flags & TF_ACKNOW) 23325e1aa279SDavid Malone goto dropafterack; 2333a0292f23SGarrett Wollman else 2334a0292f23SGarrett Wollman goto drop; 2335a0292f23SGarrett Wollman } 2336df8bae1dSRodney W. Grimes 2337df8bae1dSRodney W. Grimes /* 2338df8bae1dSRodney W. Grimes * Ack processing. 2339df8bae1dSRodney W. Grimes */ 2340df8bae1dSRodney W. Grimes switch (tp->t_state) { 2341df8bae1dSRodney W. Grimes 2342df8bae1dSRodney W. Grimes /* 2343764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 2344764d8cefSBill Fenner * ESTABLISHED state and continue processing. 2345764d8cefSBill Fenner * The ACK was checked above. 2346df8bae1dSRodney W. Grimes */ 2347df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2348a0292f23SGarrett Wollman 234978b50714SRobert Watson TCPSTAT_INC(tcps_connects); 2350df8bae1dSRodney W. Grimes soisconnected(so); 2351df8bae1dSRodney W. Grimes /* Do window scaling? */ 2352df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2353df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2354df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 2355464fcfbcSAndre Oppermann tp->snd_wnd = tiwin; 2356df8bae1dSRodney W. Grimes } 2357a0292f23SGarrett Wollman /* 2358a0292f23SGarrett Wollman * Make transitions: 2359a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 2360a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 2361a0292f23SGarrett Wollman */ 23629b8b58e0SJonathan Lemon tp->t_starttime = ticks; 2363a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 2364a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 2365a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 23667ff19458SPaul Traina } else { 2367a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 2368dbc42409SLawrence Stewart cc_conn_init(tp); 23699077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 23707ff19458SPaul Traina } 2371a0292f23SGarrett Wollman /* 2372a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 2373a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 2374a0292f23SGarrett Wollman */ 2375fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 2376fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 2377a0292f23SGarrett Wollman (struct mbuf *)0); 237860ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq - 1; 237993b0017fSPhilippe Charnier /* FALLTHROUGH */ 2380df8bae1dSRodney W. Grimes 2381df8bae1dSRodney W. Grimes /* 2382df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2383df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 2384fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 2385fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 2386df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 2387df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 2388df8bae1dSRodney W. Grimes */ 2389df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2390df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2391df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2392df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 2393df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2394df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 23955a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 239678b50714SRobert Watson TCPSTAT_INC(tcps_rcvacktoomuch); 23975a53ca16SPaul Saab goto dropafterack; 23985a53ca16SPaul Saab } 23993529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2400fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 2401fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 24025a53ca16SPaul Saab tcp_sack_doack(tp, &to, th->th_ack); 240339bc9de5SLawrence Stewart 240439bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 240539bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 240639bc9de5SLawrence Stewart 2407fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 2408fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 240978b50714SRobert Watson TCPSTAT_INC(tcps_rcvdupack); 2410df8bae1dSRodney W. Grimes /* 2411df8bae1dSRodney W. Grimes * If we have outstanding data (other than 2412df8bae1dSRodney W. Grimes * a window probe), this is a completely 2413df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 2414df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 2415df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 2416df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 2417df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 2418df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 2419df8bae1dSRodney W. Grimes * window so we send only this one 2420df8bae1dSRodney W. Grimes * packet. 2421df8bae1dSRodney W. Grimes * 2422df8bae1dSRodney W. Grimes * We know we're losing at the current 2423df8bae1dSRodney W. Grimes * window size so do congestion avoidance 2424df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 2425df8bae1dSRodney W. Grimes * and pull our congestion window back to 2426df8bae1dSRodney W. Grimes * the new ssthresh). 2427df8bae1dSRodney W. Grimes * 2428df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 2429df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 2430df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 2431df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 2432df8bae1dSRodney W. Grimes * network. 2433f2512ba1SRui Paulo * 2434f2512ba1SRui Paulo * When using TCP ECN, notify the peer that 2435f2512ba1SRui Paulo * we reduced the cwnd. 2436df8bae1dSRodney W. Grimes */ 2437b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_REXMT) || 2438fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 2439df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2440cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 2441dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2442dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_DUPACK); 24433529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2444dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2445808f11b7SPaul Saab int awnd; 244625e6f9edSPaul Saab 244725e6f9edSPaul Saab /* 244825e6f9edSPaul Saab * Compute the amount of data in flight first. 244925e6f9edSPaul Saab * We can inject new data into the pipe iff 245025e6f9edSPaul Saab * we have less than 1/2 the original window's 245125e6f9edSPaul Saab * worth of data in flight. 245225e6f9edSPaul Saab */ 2453808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 24540077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 2455808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 245625e6f9edSPaul Saab tp->snd_cwnd += tp->t_maxseg; 245725e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 245825e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 245925e6f9edSPaul Saab } 246025e6f9edSPaul Saab } else 246146f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 2462ec03d543SRandall Stewart if ((thflags & TH_FIN) && 2463ec03d543SRandall Stewart (TCPS_HAVERCVDFIN(tp->t_state) == 0)) { 2464ec03d543SRandall Stewart /* 2465ec03d543SRandall Stewart * If its a fin we need to process 2466ec03d543SRandall Stewart * it to avoid a race where both 2467ec03d543SRandall Stewart * sides enter FIN-WAIT and send FIN|ACK 2468ec03d543SRandall Stewart * at the same time. 2469ec03d543SRandall Stewart */ 2470ec03d543SRandall Stewart break; 2471ec03d543SRandall Stewart } 247246f58482SJonathan Lemon (void) tcp_output(tp); 247346f58482SJonathan Lemon goto drop; 2474cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 2475cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 2476a0445c2eSJayanth Vijayaraghavan 2477a0445c2eSJayanth Vijayaraghavan /* 2478a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 2479a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 2480a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 2481a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 2482a0445c2eSJayanth Vijayaraghavan * recovery. 2483a0445c2eSJayanth Vijayaraghavan */ 24843529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 2485dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2486a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 2487a0445c2eSJayanth Vijayaraghavan break; 2488a0445c2eSJayanth Vijayaraghavan } 2489dbc42409SLawrence Stewart } else { 2490a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 24919d11646dSJeffrey Hsu tp->snd_recover)) { 2492cb942153SJeffrey Hsu tp->t_dupacks = 0; 2493cb942153SJeffrey Hsu break; 249446f58482SJonathan Lemon } 2495a0445c2eSJayanth Vijayaraghavan } 2496dbc42409SLawrence Stewart /* Congestion signal before ack. */ 2497dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_NDUPACK); 2498dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_DUPACK); 2499b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 25009b8b58e0SJonathan Lemon tp->t_rtttime = 0; 25013529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 250278b50714SRobert Watson TCPSTAT_INC( 250378b50714SRobert Watson tcps_sack_recovery_episode); 2504a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 25058db456bfSPaul Saab tp->snd_cwnd = tp->t_maxseg; 25066d90faf3SPaul Saab (void) tcp_output(tp); 25076d90faf3SPaul Saab goto drop; 25086d90faf3SPaul Saab } 2509fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 2510df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 2511ec03d543SRandall Stewart if ((thflags & TH_FIN) && 2512ec03d543SRandall Stewart (TCPS_HAVERCVDFIN(tp->t_state) == 0)) { 2513ec03d543SRandall Stewart /* 2514ec03d543SRandall Stewart * If its a fin we need to process 2515ec03d543SRandall Stewart * it to avoid a race where both 2516ec03d543SRandall Stewart * sides enter FIN-WAIT and send FIN|ACK 2517ec03d543SRandall Stewart * at the same time. 2518ec03d543SRandall Stewart */ 2519ec03d543SRandall Stewart break; 2520ec03d543SRandall Stewart } 2521df8bae1dSRodney W. Grimes (void) tcp_output(tp); 252248d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 2523302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 2524302ce8d6SAndre Oppermann __func__)); 2525df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 252648d2549cSJeffrey Hsu tp->t_maxseg * 252748d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 2528df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 2529df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 2530df8bae1dSRodney W. Grimes goto drop; 2531603724d3SBjoern A. Zeeb } else if (V_tcp_do_rfc3042) { 2532dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_DUPACK); 2533582a954bSJeffrey Hsu u_long oldcwnd = tp->snd_cwnd; 253448d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 253548d2549cSJeffrey Hsu u_int sent; 25365628dd08SAndre Oppermann int avail; 253789c02376SJeffrey Hsu 2538582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 2539582a954bSJeffrey Hsu tp->t_dupacks == 2, 2540302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 2541302ce8d6SAndre Oppermann __func__)); 254261a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 254348d2549cSJeffrey Hsu tp->snd_limited = 0; 254461a36e3dSJeffrey Hsu tp->snd_cwnd = 254561a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 254661a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 254761a36e3dSJeffrey Hsu tp->t_maxseg; 2548ec03d543SRandall Stewart if ((thflags & TH_FIN) && 2549ec03d543SRandall Stewart (TCPS_HAVERCVDFIN(tp->t_state) == 0)) { 2550ec03d543SRandall Stewart /* 2551ec03d543SRandall Stewart * If its a fin we need to process 2552ec03d543SRandall Stewart * it to avoid a race where both 2553ec03d543SRandall Stewart * sides enter FIN-WAIT and send FIN|ACK 2554ec03d543SRandall Stewart * at the same time. 2555ec03d543SRandall Stewart */ 2556ec03d543SRandall Stewart break; 2557ec03d543SRandall Stewart } 25585628dd08SAndre Oppermann /* 25595628dd08SAndre Oppermann * Only call tcp_output when there 25605628dd08SAndre Oppermann * is new data available to be sent. 25615628dd08SAndre Oppermann * Otherwise we would send pure ACKs. 25625628dd08SAndre Oppermann */ 25635628dd08SAndre Oppermann SOCKBUF_LOCK(&so->so_snd); 25645628dd08SAndre Oppermann avail = so->so_snd.sb_cc - 25655628dd08SAndre Oppermann (tp->snd_nxt - tp->snd_una); 25665628dd08SAndre Oppermann SOCKBUF_UNLOCK(&so->so_snd); 25675628dd08SAndre Oppermann if (avail > 0) 2568582a954bSJeffrey Hsu (void) tcp_output(tp); 256948d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 257048d2549cSJeffrey Hsu if (sent > tp->t_maxseg) { 257189c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 257289c02376SJeffrey Hsu tp->snd_limited == 0) || 257389c02376SJeffrey Hsu (sent == tp->t_maxseg + 1 && 257489c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 2575302ce8d6SAndre Oppermann ("%s: sent too much", 2576302ce8d6SAndre Oppermann __func__)); 257748d2549cSJeffrey Hsu tp->snd_limited = 2; 257848d2549cSJeffrey Hsu } else if (sent > 0) 257948d2549cSJeffrey Hsu ++tp->snd_limited; 2580582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 2581582a954bSJeffrey Hsu goto drop; 2582df8bae1dSRodney W. Grimes } 2583df8bae1dSRodney W. Grimes } else 2584df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2585df8bae1dSRodney W. Grimes break; 2586df8bae1dSRodney W. Grimes } 2587cb942153SJeffrey Hsu 2588302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2589302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 2590cb942153SJeffrey Hsu 2591df8bae1dSRodney W. Grimes /* 2592df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2593df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2594df8bae1dSRodney W. Grimes */ 2595dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2596cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 25973529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 25986d90faf3SPaul Saab tcp_sack_partialack(tp, th); 25996d90faf3SPaul Saab else 2600c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2601dbc42409SLawrence Stewart } else 2602dbc42409SLawrence Stewart cc_post_recovery(tp, th); 260346f58482SJonathan Lemon } 2604cb942153SJeffrey Hsu tp->t_dupacks = 0; 2605a0292f23SGarrett Wollman /* 2606a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2607a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2608a0292f23SGarrett Wollman */ 2609a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2610a0292f23SGarrett Wollman /* 2611a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2612a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 261307e43e10SAndras Olah * synchronized). Go to non-starred state, 261407e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 261507e43e10SAndras Olah * we can do window scaling. 2616a0292f23SGarrett Wollman */ 2617a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2618a0292f23SGarrett Wollman tp->snd_una++; 261907e43e10SAndras Olah /* Do window scaling? */ 262007e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 262107e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 262207e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2623464fcfbcSAndre Oppermann /* Send window already scaled. */ 262407e43e10SAndras Olah } 2625a0292f23SGarrett Wollman } 2626a0292f23SGarrett Wollman 2627a0292f23SGarrett Wollman process_ACK: 26288501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 26297cfc6904SRobert Watson 2630dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 263178b50714SRobert Watson TCPSTAT_INC(tcps_rcvackpack); 263278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 2633df8bae1dSRodney W. Grimes 2634df8bae1dSRodney W. Grimes /* 26359b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 26369b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 26379b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 26389b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 26399b8b58e0SJonathan Lemon * we left off. 26409b8b58e0SJonathan Lemon */ 2641672dc4aeSJohn Baldwin if (tp->t_rxtshift == 1 && tp->t_flags & TF_PREVVALID && 2642672dc4aeSJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) 2643dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 26449b8b58e0SJonathan Lemon 26459b8b58e0SJonathan Lemon /* 2646df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2647df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2648df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2649df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2650df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2651df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2652df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2653fa55172bSMatthew Dillon * 2654fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2655fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2656fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2657fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2658df8bae1dSRodney W. Grimes */ 2659d8951c8aSBjoern A. Zeeb if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) { 2660d8951c8aSBjoern A. Zeeb u_int t; 2661d8951c8aSBjoern A. Zeeb 2662d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 2663d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 2664d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 2665d8951c8aSBjoern A. Zeeb tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1); 2666fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2667eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2668eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 26699b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2670fa55172bSMatthew Dillon } 2671df8bae1dSRodney W. Grimes 2672df8bae1dSRodney W. Grimes /* 2673df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2674df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2675df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2676df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2677df8bae1dSRodney W. Grimes */ 2678fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 2679b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2680df8bae1dSRodney W. Grimes needoutput = 1; 2681b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2682b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2683a0292f23SGarrett Wollman 2684a0292f23SGarrett Wollman /* 2685a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2686a0292f23SGarrett Wollman * skip rest of ACK processing. 2687a0292f23SGarrett Wollman */ 2688a0292f23SGarrett Wollman if (acked == 0) 2689a0292f23SGarrett Wollman goto step6; 2690a0292f23SGarrett Wollman 2691df8bae1dSRodney W. Grimes /* 2692dbc42409SLawrence Stewart * Let the congestion control algorithm update congestion 2693dbc42409SLawrence Stewart * control related information. This typically means increasing 2694dbc42409SLawrence Stewart * the congestion window. 2695df8bae1dSRodney W. Grimes */ 2696dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_ACK); 2697dbc42409SLawrence Stewart 26985905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2699df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 270060ee3bb2SAndre Oppermann tp->snd_wnd -= so->so_snd.sb_cc; 27015905999bSRobert Watson sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc); 2702df8bae1dSRodney W. Grimes ourfinisacked = 1; 2703df8bae1dSRodney W. Grimes } else { 27045905999bSRobert Watson sbdrop_locked(&so->so_snd, acked); 270560ee3bb2SAndre Oppermann tp->snd_wnd -= acked; 2706df8bae1dSRodney W. Grimes ourfinisacked = 0; 2707df8bae1dSRodney W. Grimes } 2708564aab1fSAndre Oppermann /* NB: sowwakeup_locked() does an implicit unlock. */ 27091e4d7da7SRobert Watson sowwakeup_locked(so); 2710e8949f74SAndre Oppermann /* Detect una wraparound. */ 2711dbc42409SLawrence Stewart if (!IN_RECOVERY(tp->t_flags) && 27129d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 27139d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 27149d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 2715dbc42409SLawrence Stewart /* XXXLAS: Can this be moved up into cc_post_recovery? */ 2716dbc42409SLawrence Stewart if (IN_RECOVERY(tp->t_flags) && 271724cb0f22SLawrence Stewart SEQ_GEQ(th->th_ack, tp->snd_recover)) { 2718dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 271924cb0f22SLawrence Stewart } 2720fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 27213529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 27226d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 27236d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 27246d90faf3SPaul Saab } 2725df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2726df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2727df8bae1dSRodney W. Grimes 2728df8bae1dSRodney W. Grimes switch (tp->t_state) { 2729df8bae1dSRodney W. Grimes 2730df8bae1dSRodney W. Grimes /* 2731df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2732df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2733df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2734df8bae1dSRodney W. Grimes */ 2735df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2736df8bae1dSRodney W. Grimes if (ourfinisacked) { 2737df8bae1dSRodney W. Grimes /* 2738df8bae1dSRodney W. Grimes * If we can't receive any more 2739df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2740df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2741df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2742df8bae1dSRodney W. Grimes * we'll hang forever. 2743e8949f74SAndre Oppermann * 2744e8949f74SAndre Oppermann * XXXjl: 2745340c35deSJonathan Lemon * we should release the tp also, and use a 2746340c35deSJonathan Lemon * compressed state. 2747340c35deSJonathan Lemon */ 2748c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 274903e49181SSeigo Tanimura soisdisconnected(so); 27509077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 27519077f387SGleb Smirnoff (tcp_fast_finwait2_recycle ? 27529077f387SGleb Smirnoff tcp_finwait2_timeout : 27539077f387SGleb Smirnoff TP_MAXIDLE(tp))); 27544cc20ab1SSeigo Tanimura } 2755df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 2756df8bae1dSRodney W. Grimes } 2757df8bae1dSRodney W. Grimes break; 2758df8bae1dSRodney W. Grimes 2759df8bae1dSRodney W. Grimes /* 2760df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2761df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2762df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2763df8bae1dSRodney W. Grimes * the segment. 2764df8bae1dSRodney W. Grimes */ 2765df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2766df8bae1dSRodney W. Grimes if (ourfinisacked) { 2767252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 276811a20fb8SJeffrey Hsu tcp_twstart(tp); 2769603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2770340c35deSJonathan Lemon m_freem(m); 2771679d9708SAndre Oppermann return; 2772df8bae1dSRodney W. Grimes } 2773df8bae1dSRodney W. Grimes break; 2774df8bae1dSRodney W. Grimes 2775df8bae1dSRodney W. Grimes /* 2776df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2777df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2778df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2779df8bae1dSRodney W. Grimes * enter the closed state and return. 2780df8bae1dSRodney W. Grimes */ 2781df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2782df8bae1dSRodney W. Grimes if (ourfinisacked) { 2783252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2784df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2785df8bae1dSRodney W. Grimes goto drop; 2786df8bae1dSRodney W. Grimes } 2787df8bae1dSRodney W. Grimes break; 2788df8bae1dSRodney W. Grimes } 2789df8bae1dSRodney W. Grimes } 2790df8bae1dSRodney W. Grimes 2791df8bae1dSRodney W. Grimes step6: 27928501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 27937cfc6904SRobert Watson 2794df8bae1dSRodney W. Grimes /* 279560ee3bb2SAndre Oppermann * Update window information. 279660ee3bb2SAndre Oppermann * Don't look at window if no ACK: TAC's send garbage on first SYN. 2797df8bae1dSRodney W. Grimes */ 279860ee3bb2SAndre Oppermann if ((thflags & TH_ACK) && 279960ee3bb2SAndre Oppermann (SEQ_LT(tp->snd_wl1, th->th_seq) || 280060ee3bb2SAndre Oppermann (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 280160ee3bb2SAndre Oppermann (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 280260ee3bb2SAndre Oppermann /* keep track of pure window updates */ 280360ee3bb2SAndre Oppermann if (tlen == 0 && 280460ee3bb2SAndre Oppermann tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 280578b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinupd); 2806df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 280760ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 280860ee3bb2SAndre Oppermann tp->snd_wl2 = th->th_ack; 2809df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2810df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 281160ee3bb2SAndre Oppermann needoutput = 1; 2812df8bae1dSRodney W. Grimes } 2813df8bae1dSRodney W. Grimes 2814df8bae1dSRodney W. Grimes /* 2815df8bae1dSRodney W. Grimes * Process segments with URG. 2816df8bae1dSRodney W. Grimes */ 2817fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2818df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2819df8bae1dSRodney W. Grimes /* 2820df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2821df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2822df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2823df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2824df8bae1dSRodney W. Grimes */ 282518ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2826fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2827fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2828fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 282918ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2830df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2831df8bae1dSRodney W. Grimes } 2832df8bae1dSRodney W. Grimes /* 2833df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2834df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2835df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2836df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2837df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2838df8bae1dSRodney W. Grimes * 2839df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2840df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2841df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2842df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2843df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2844df8bae1dSRodney W. Grimes * spec states (in one of two places). 2845df8bae1dSRodney W. Grimes */ 2846fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2847fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2848df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2849df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2850927c5ceaSRobert Watson if (so->so_oobmark == 0) 2851c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 2852df8bae1dSRodney W. Grimes sohasoutofband(so); 2853df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2854df8bae1dSRodney W. Grimes } 285518ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2856df8bae1dSRodney W. Grimes /* 2857df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2858df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2859df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2860df8bae1dSRodney W. Grimes * data may creep in... ick. 2861df8bae1dSRodney W. Grimes */ 286230613f56SJeffrey Hsu if (th->th_urp <= (u_long)tlen && 286330613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 286430613f56SJeffrey Hsu /* hdr drop is delayed */ 286530613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 286630613f56SJeffrey Hsu } 2867c068736aSJeffrey Hsu } else { 2868df8bae1dSRodney W. Grimes /* 2869df8bae1dSRodney W. Grimes * If no out of band data is expected, 2870df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2871df8bae1dSRodney W. Grimes * with the receive window. 2872df8bae1dSRodney W. Grimes */ 2873df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2874df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2875c068736aSJeffrey Hsu } 2876df8bae1dSRodney W. Grimes dodata: /* XXX */ 28778501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 28787cfc6904SRobert Watson 2879df8bae1dSRodney W. Grimes /* 2880df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2881df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2882df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2883df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2884df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2885df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2886df8bae1dSRodney W. Grimes */ 2887fb59c426SYoshinobu Inoue if ((tlen || (thflags & TH_FIN)) && 2888df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 288969e03620SPaul Saab tcp_seq save_start = th->th_seq; 2890fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2891e4b64281SJesper Skriver /* 2892c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2893c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2894c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2895c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2896c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2897c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2898c068736aSJeffrey Hsu * conversions. 2899c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 2900c068736aSJeffrey Hsu * immediately when segments are out of order (so 2901c068736aSJeffrey Hsu * fast retransmit can work). 2902e4b64281SJesper Skriver */ 2903e4b64281SJesper Skriver if (th->th_seq == tp->rcv_nxt && 2904e4b64281SJesper Skriver LIST_EMPTY(&tp->t_segq) && 2905e4b64281SJesper Skriver TCPS_HAVEESTABLISHED(tp->t_state)) { 2906e4b64281SJesper Skriver if (DELAY_ACK(tp)) 29073bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2908e4b64281SJesper Skriver else 2909e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2910e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2911e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 291278b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 291378b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 2914e4b64281SJesper Skriver ND6_HINT(tp); 29151e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2916c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2917c1c36a2cSMike Silbersack m_freem(m); 2918c1c36a2cSMike Silbersack else 29191e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 2920e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 29211e4d7da7SRobert Watson sorwakeup_locked(so); 2922e4b64281SJesper Skriver } else { 2923e8949f74SAndre Oppermann /* 2924e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 2925e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 2926e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 2927e8949f74SAndre Oppermann * when trimming from the head. 2928e8949f74SAndre Oppermann */ 2929e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 2930e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2931e4b64281SJesper Skriver } 29323529149eSAndre Oppermann if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 2933f194524fSAndre Oppermann tcp_update_sack_list(tp, save_start, save_start + tlen); 2934302ce8d6SAndre Oppermann #if 0 2935df8bae1dSRodney W. Grimes /* 2936df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2937df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2938df8bae1dSRodney W. Grimes * buffer size. 2939302ce8d6SAndre Oppermann * XXX: Unused. 2940df8bae1dSRodney W. Grimes */ 2941f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) 2942df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2943f701e30dSJohn Baldwin else 2944f701e30dSJohn Baldwin len = so->so_rcv.sb_hiwat; 2945302ce8d6SAndre Oppermann #endif 2946df8bae1dSRodney W. Grimes } else { 2947df8bae1dSRodney W. Grimes m_freem(m); 2948fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2949df8bae1dSRodney W. Grimes } 2950df8bae1dSRodney W. Grimes 2951df8bae1dSRodney W. Grimes /* 2952df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2953df8bae1dSRodney W. Grimes * that the connection is closing. 2954df8bae1dSRodney W. Grimes */ 2955fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2956df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2957df8bae1dSRodney W. Grimes socantrcvmore(so); 2958a0292f23SGarrett Wollman /* 2959a0292f23SGarrett Wollman * If connection is half-synchronized 2960d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2961a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2962a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2963a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2964a0292f23SGarrett Wollman */ 29653bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 29663bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2967a0292f23SGarrett Wollman else 2968df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2969df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2970df8bae1dSRodney W. Grimes } 2971df8bae1dSRodney W. Grimes switch (tp->t_state) { 2972df8bae1dSRodney W. Grimes 2973df8bae1dSRodney W. Grimes /* 2974df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2975df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2976df8bae1dSRodney W. Grimes */ 2977df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 29789b8b58e0SJonathan Lemon tp->t_starttime = ticks; 29799b8b58e0SJonathan Lemon /* FALLTHROUGH */ 2980df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2981df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2982df8bae1dSRodney W. Grimes break; 2983df8bae1dSRodney W. Grimes 2984df8bae1dSRodney W. Grimes /* 2985df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2986df8bae1dSRodney W. Grimes * enter the CLOSING state. 2987df8bae1dSRodney W. Grimes */ 2988df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2989df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 2990df8bae1dSRodney W. Grimes break; 2991df8bae1dSRodney W. Grimes 2992df8bae1dSRodney W. Grimes /* 2993df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 2994df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 2995df8bae1dSRodney W. Grimes * standard timers. 2996df8bae1dSRodney W. Grimes */ 2997df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2998252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2999252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: dodata " 3000252ca428SRobert Watson "TCP_FIN_WAIT_2 ti_locked: %d", __func__, 3001252ca428SRobert Watson ti_locked)); 3002252ca428SRobert Watson 3003340c35deSJonathan Lemon tcp_twstart(tp); 3004603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 3005679d9708SAndre Oppermann return; 3006df8bae1dSRodney W. Grimes } 3007df8bae1dSRodney W. Grimes } 3008fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 3009603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 3010252ca428SRobert Watson ti_locked = TI_UNLOCKED; 3011252ca428SRobert Watson 3012610ee2f9SDavid Greenman #ifdef TCPDEBUG 30134cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3014fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 3015fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3016610ee2f9SDavid Greenman #endif 3017df8bae1dSRodney W. Grimes 3018df8bae1dSRodney W. Grimes /* 3019df8bae1dSRodney W. Grimes * Return any desired output. 3020df8bae1dSRodney W. Grimes */ 3021df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 3022df8bae1dSRodney W. Grimes (void) tcp_output(tp); 30237792ea27SJeffrey Hsu 3024a14c749fSJonathan Lemon check_delack: 3025252ca428SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 3026252ca428SRobert Watson __func__, ti_locked)); 3027603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 30288501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 3029252ca428SRobert Watson 3030a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 30313bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 3032b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 30333bfd6421SJonathan Lemon } 30348501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3035679d9708SAndre Oppermann return; 3036df8bae1dSRodney W. Grimes 3037df8bae1dSRodney W. Grimes dropafterack: 3038df8bae1dSRodney W. Grimes /* 3039df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 3040df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 304180ab7c0eSGarrett Wollman * 304280ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 304380ab7c0eSGarrett Wollman * paths to this code happen after packets containing 304480ab7c0eSGarrett Wollman * RST have been dropped. 304580ab7c0eSGarrett Wollman * 304680ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 304780ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 304880ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 304980ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 305080ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 305180ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 3052df8bae1dSRodney W. Grimes */ 3053fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 3054fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 3055a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 3056a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 3057a57815efSBosko Milekic goto dropwithreset; 3058a57815efSBosko Milekic } 3059a0292f23SGarrett Wollman #ifdef TCPDEBUG 30604cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3061fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3062fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3063a0292f23SGarrett Wollman #endif 3064fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 3065603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 3066252ca428SRobert Watson ti_locked = TI_UNLOCKED; 3067252ca428SRobert Watson 3068df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 3069df8bae1dSRodney W. Grimes (void) tcp_output(tp); 30708501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3071d6915262SRobert Watson m_freem(m); 3072679d9708SAndre Oppermann return; 3073df8bae1dSRodney W. Grimes 3074df8bae1dSRodney W. Grimes dropwithreset: 3075fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 3076dd8ac7f9SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 3077252ca428SRobert Watson ti_locked = TI_UNLOCKED; 3078a57815efSBosko Milekic 3079a0ca0871SRobert Watson if (tp != NULL) { 3080302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 30818501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3082dd8ac7f9SRobert Watson } else 3083a0ca0871SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 3084679d9708SAndre Oppermann return; 3085df8bae1dSRodney W. Grimes 3086df8bae1dSRodney W. Grimes drop: 3087fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) { 3088252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 3089fa046d87SRobert Watson ti_locked = TI_UNLOCKED; 3090fa046d87SRobert Watson } 3091252ca428SRobert Watson #ifdef INVARIANTS 3092252ca428SRobert Watson else 3093252ca428SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 3094252ca428SRobert Watson #endif 3095252ca428SRobert Watson 3096df8bae1dSRodney W. Grimes /* 3097df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 3098df8bae1dSRodney W. Grimes */ 3099610ee2f9SDavid Greenman #ifdef TCPDEBUG 31001c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 3101fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3102fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3103a0292f23SGarrett Wollman #endif 31041c53f806SRobert Watson if (tp != NULL) 31058501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3106d6915262SRobert Watson m_freem(m); 3107302ce8d6SAndre Oppermann } 3108302ce8d6SAndre Oppermann 3109302ce8d6SAndre Oppermann /* 31100ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 31110ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 31120ca3f933SAndre Oppermann * tp may be NULL. 3113302ce8d6SAndre Oppermann */ 3114302ce8d6SAndre Oppermann static void 3115302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 3116302ce8d6SAndre Oppermann int tlen, int rstreason) 3117302ce8d6SAndre Oppermann { 3118b287c6c7SBjoern A. Zeeb #ifdef INET 3119302ce8d6SAndre Oppermann struct ip *ip; 3120b287c6c7SBjoern A. Zeeb #endif 3121302ce8d6SAndre Oppermann #ifdef INET6 3122302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 3123302ce8d6SAndre Oppermann #endif 31247a3244ccSRobert Watson 31257a3244ccSRobert Watson if (tp != NULL) { 31268501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 31277a3244ccSRobert Watson } 31287a3244ccSRobert Watson 31290ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 3130302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 3131302ce8d6SAndre Oppermann goto drop; 3132302ce8d6SAndre Oppermann #ifdef INET6 3133302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 3134302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 3135302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3136302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3137302ce8d6SAndre Oppermann goto drop; 3138302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 3139b287c6c7SBjoern A. Zeeb } 3140302ce8d6SAndre Oppermann #endif 3141b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3142b287c6c7SBjoern A. Zeeb else 3143b287c6c7SBjoern A. Zeeb #endif 3144b287c6c7SBjoern A. Zeeb #ifdef INET 3145302ce8d6SAndre Oppermann { 3146302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 3147302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3148302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3149302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3150302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3151302ce8d6SAndre Oppermann goto drop; 3152302ce8d6SAndre Oppermann } 3153b287c6c7SBjoern A. Zeeb #endif 3154302ce8d6SAndre Oppermann 3155302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 3156302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 3157302ce8d6SAndre Oppermann goto drop; 3158302ce8d6SAndre Oppermann 3159302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 3160302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 3161302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 3162302ce8d6SAndre Oppermann th->th_ack, TH_RST); 3163302ce8d6SAndre Oppermann } else { 3164302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 3165302ce8d6SAndre Oppermann tlen++; 3166302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 3167302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 3168302ce8d6SAndre Oppermann } 3169302ce8d6SAndre Oppermann return; 3170302ce8d6SAndre Oppermann drop: 3171302ce8d6SAndre Oppermann m_freem(m); 3172df8bae1dSRodney W. Grimes } 3173df8bae1dSRodney W. Grimes 3174be2ac88cSJonathan Lemon /* 3175be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 3176be2ac88cSJonathan Lemon */ 31770312fbe9SPoul-Henning Kamp static void 3178ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 3179df8bae1dSRodney W. Grimes { 3180df8bae1dSRodney W. Grimes int opt, optlen; 3181df8bae1dSRodney W. Grimes 3182be2ac88cSJonathan Lemon to->to_flags = 0; 3183df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 3184df8bae1dSRodney W. Grimes opt = cp[0]; 3185df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 3186df8bae1dSRodney W. Grimes break; 3187df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 3188df8bae1dSRodney W. Grimes optlen = 1; 3189df8bae1dSRodney W. Grimes else { 3190b474779fSJun-ichiro itojun Hagino if (cnt < 2) 3191b474779fSJun-ichiro itojun Hagino break; 3192df8bae1dSRodney W. Grimes optlen = cp[1]; 3193b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 3194df8bae1dSRodney W. Grimes break; 3195df8bae1dSRodney W. Grimes } 3196df8bae1dSRodney W. Grimes switch (opt) { 3197df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 3198df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 3199df8bae1dSRodney W. Grimes continue; 3200f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3201df8bae1dSRodney W. Grimes continue; 3202be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 3203be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 3204be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 3205fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 3206df8bae1dSRodney W. Grimes break; 3207df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 3208df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 3209df8bae1dSRodney W. Grimes continue; 3210f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3211df8bae1dSRodney W. Grimes continue; 3212be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 321302a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 3214df8bae1dSRodney W. Grimes break; 3215df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 3216df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 3217df8bae1dSRodney W. Grimes continue; 3218be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 3219a0292f23SGarrett Wollman bcopy((char *)cp + 2, 3220a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 3221fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 3222a0292f23SGarrett Wollman bcopy((char *)cp + 6, 3223a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 3224fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 3225df8bae1dSRodney W. Grimes break; 32261cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 32271cfd4b53SBruce M Simpson /* 32281cfd4b53SBruce M Simpson * XXX In order to reply to a host which has set the 32291cfd4b53SBruce M Simpson * TCP_SIGNATURE option in its initial SYN, we have to 32301cfd4b53SBruce M Simpson * record the fact that the option was observed here 32311cfd4b53SBruce M Simpson * for the syncache code to perform the correct response. 32321cfd4b53SBruce M Simpson */ 32331cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 32341cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 32351cfd4b53SBruce M Simpson continue; 3236df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 3237df47e437SAndre Oppermann to->to_signature = cp + 2; 32381cfd4b53SBruce M Simpson break; 3239265ed012SBruce M Simpson #endif 32406d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 3241f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 32426d90faf3SPaul Saab continue; 3243f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3244f72167f4SAndre Oppermann continue; 3245603724d3SBjoern A. Zeeb if (!V_tcp_do_sack) 3246f72167f4SAndre Oppermann continue; 3247fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 32486d90faf3SPaul Saab break; 32496d90faf3SPaul Saab case TCPOPT_SACK: 32505a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 32516d90faf3SPaul Saab continue; 3252b728e902SAndre Oppermann if (flags & TO_SYN) 3253b728e902SAndre Oppermann continue; 3254fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 32555a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 32565a53ca16SPaul Saab to->to_sacks = cp + 2; 325778b50714SRobert Watson TCPSTAT_INC(tcps_sack_rcv_blocks); 32586d90faf3SPaul Saab break; 3259be2ac88cSJonathan Lemon default: 3260be2ac88cSJonathan Lemon continue; 3261df8bae1dSRodney W. Grimes } 3262df8bae1dSRodney W. Grimes } 3263df8bae1dSRodney W. Grimes } 3264df8bae1dSRodney W. Grimes 3265df8bae1dSRodney W. Grimes /* 3266df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 3267df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 3268df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 3269df8bae1dSRodney W. Grimes * sequencing purposes. 3270df8bae1dSRodney W. Grimes */ 32710312fbe9SPoul-Henning Kamp static void 3272ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 3273ad3f9ab3SAndre Oppermann int off) 3274df8bae1dSRodney W. Grimes { 3275fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 3276df8bae1dSRodney W. Grimes 3277df8bae1dSRodney W. Grimes while (cnt >= 0) { 3278df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 3279df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 3280df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 3281df8bae1dSRodney W. Grimes 32828501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 32837a3244ccSRobert Watson 3284df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 3285df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 3286df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 3287df8bae1dSRodney W. Grimes m->m_len--; 328869a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 328969a34685SYoshinobu Inoue m->m_pkthdr.len--; 3290df8bae1dSRodney W. Grimes return; 3291df8bae1dSRodney W. Grimes } 3292df8bae1dSRodney W. Grimes cnt -= m->m_len; 3293df8bae1dSRodney W. Grimes m = m->m_next; 32944dfdffe9SAndre Oppermann if (m == NULL) 3295df8bae1dSRodney W. Grimes break; 3296df8bae1dSRodney W. Grimes } 3297df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 3298df8bae1dSRodney W. Grimes } 3299df8bae1dSRodney W. Grimes 3300df8bae1dSRodney W. Grimes /* 3301df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 3302df8bae1dSRodney W. Grimes * and update averages and current timeout. 3303df8bae1dSRodney W. Grimes */ 33040312fbe9SPoul-Henning Kamp static void 3305ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 3306df8bae1dSRodney W. Grimes { 3307ad3f9ab3SAndre Oppermann int delta; 3308233e8c18SGarrett Wollman 33098501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 33102be3bf22SRobert Watson 331178b50714SRobert Watson TCPSTAT_INC(tcps_rttupdated); 3312233e8c18SGarrett Wollman tp->t_rttupdated++; 3313233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 3314233e8c18SGarrett Wollman /* 3315233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 3316233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 3317233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 3318233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3319233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 3320233e8c18SGarrett Wollman */ 3321233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3322233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3323233e8c18SGarrett Wollman 3324233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 3325233e8c18SGarrett Wollman tp->t_srtt = 1; 3326233e8c18SGarrett Wollman 3327233e8c18SGarrett Wollman /* 3328233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 3329233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 3330233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 3331233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 3332233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 3333233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 3334233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3335233e8c18SGarrett Wollman * rfc793's wired-in beta. 3336233e8c18SGarrett Wollman */ 3337233e8c18SGarrett Wollman if (delta < 0) 3338233e8c18SGarrett Wollman delta = -delta; 3339233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3340233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 3341233e8c18SGarrett Wollman tp->t_rttvar = 1; 33421fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 33431fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3344233e8c18SGarrett Wollman } else { 3345233e8c18SGarrett Wollman /* 3346233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 3347233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 3348233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 3349233e8c18SGarrett Wollman */ 3350233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 3351233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 33521fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3353233e8c18SGarrett Wollman } 33549b8b58e0SJonathan Lemon tp->t_rtttime = 0; 3355df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 3356df8bae1dSRodney W. Grimes 3357df8bae1dSRodney W. Grimes /* 3358df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 3359df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 3360df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 3361df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 3362df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 3363df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 3364df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 3365df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 3366df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 3367df8bae1dSRodney W. Grimes */ 3368233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 33699e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3370df8bae1dSRodney W. Grimes 3371df8bae1dSRodney W. Grimes /* 3372df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 3373df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 3374df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 3375df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 3376df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 3377df8bae1dSRodney W. Grimes */ 3378df8bae1dSRodney W. Grimes tp->t_softerror = 0; 3379df8bae1dSRodney W. Grimes } 3380df8bae1dSRodney W. Grimes 3381df8bae1dSRodney W. Grimes /* 3382df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 3383df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 33844faaea55SAndre Oppermann * If none, use an mss that can be handled on the outgoing interface 33854faaea55SAndre Oppermann * without forcing IP to fragment. If no route is found, route has no mtu, 3386df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 3387df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 3388df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 3389df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 3390df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 3391df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 3392df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 3393a0292f23SGarrett Wollman * 3394a0292f23SGarrett Wollman * Also take into account the space needed for options that we 3395a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 3396a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 3397a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 3398a0292f23SGarrett Wollman * data in maxopd. 3399a0292f23SGarrett Wollman * 340097d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 3401ef341ee1SGleb Smirnoff * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS 3402ef341ee1SGleb Smirnoff * settings are handled in tcp_mssopt(). 3403df8bae1dSRodney W. Grimes */ 3404a0292f23SGarrett Wollman void 3405ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer, 34063c914c54SAndre Oppermann struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap) 3407df8bae1dSRodney W. Grimes { 3408b287c6c7SBjoern A. Zeeb int mss = 0; 3409b287c6c7SBjoern A. Zeeb u_long maxmtu = 0; 3410c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 341197d8d152SAndre Oppermann struct hc_metrics_lite metrics; 3412ef341ee1SGleb Smirnoff int origoffer; 3413fb59c426SYoshinobu Inoue #ifdef INET6 3414c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3415c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 3416c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3417c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 3418c068736aSJeffrey Hsu #else 3419c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 3420fb59c426SYoshinobu Inoue #endif 3421df8bae1dSRodney W. Grimes 34228501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 34237a3244ccSRobert Watson 3424ef341ee1SGleb Smirnoff if (mtuoffer != -1) { 3425ef341ee1SGleb Smirnoff KASSERT(offer == -1, ("%s: conflict", __func__)); 3426ef341ee1SGleb Smirnoff offer = mtuoffer - min_protoh; 3427ef341ee1SGleb Smirnoff } 3428ef341ee1SGleb Smirnoff origoffer = offer; 3429ef341ee1SGleb Smirnoff 3430e8949f74SAndre Oppermann /* Initialize. */ 343197d8d152SAndre Oppermann #ifdef INET6 343297d8d152SAndre Oppermann if (isipv6) { 34333c914c54SAndre Oppermann maxmtu = tcp_maxmtu6(&inp->inp_inc, cap); 3434603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_v6mssdflt; 3435b287c6c7SBjoern A. Zeeb } 343697d8d152SAndre Oppermann #endif 3437b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3438b287c6c7SBjoern A. Zeeb else 3439b287c6c7SBjoern A. Zeeb #endif 3440b287c6c7SBjoern A. Zeeb #ifdef INET 344197d8d152SAndre Oppermann { 34423c914c54SAndre Oppermann maxmtu = tcp_maxmtu(&inp->inp_inc, cap); 3443603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_mssdflt; 3444df8bae1dSRodney W. Grimes } 3445b287c6c7SBjoern A. Zeeb #endif 3446df8bae1dSRodney W. Grimes 344797d8d152SAndre Oppermann /* 3448e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 344997d8d152SAndre Oppermann */ 34506f01cac6SBjoern A. Zeeb if (maxmtu == 0) { 34516f01cac6SBjoern A. Zeeb /* 34528e5c87f4SBjoern A. Zeeb * In case we return early we need to initialize metrics 34536f01cac6SBjoern A. Zeeb * to a defined state as tcp_hc_get() would do for us 34546f01cac6SBjoern A. Zeeb * if there was no cache hit. 34556f01cac6SBjoern A. Zeeb */ 34566f01cac6SBjoern A. Zeeb if (metricptr != NULL) 34576f01cac6SBjoern A. Zeeb bzero(metricptr, sizeof(struct hc_metrics_lite)); 345897d8d152SAndre Oppermann return; 34596f01cac6SBjoern A. Zeeb } 346097d8d152SAndre Oppermann 3461e8949f74SAndre Oppermann /* What have we got? */ 346297d8d152SAndre Oppermann switch (offer) { 346397d8d152SAndre Oppermann case 0: 346497d8d152SAndre Oppermann /* 346597d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 3466c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 3467c3b02504SBjoern A. Zeeb * already assigned to t_maxopd above. 346897d8d152SAndre Oppermann */ 3469c3b02504SBjoern A. Zeeb offer = tp->t_maxopd; 347097d8d152SAndre Oppermann break; 347197d8d152SAndre Oppermann 347297d8d152SAndre Oppermann case -1: 3473a0292f23SGarrett Wollman /* 3474c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 3475a0292f23SGarrett Wollman */ 347697d8d152SAndre Oppermann /* FALLTHROUGH */ 347797d8d152SAndre Oppermann 347897d8d152SAndre Oppermann default: 3479a0292f23SGarrett Wollman /* 348053369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 348153369ac9SAndre Oppermann * to at least minmss. 348253369ac9SAndre Oppermann */ 3483603724d3SBjoern A. Zeeb offer = max(offer, V_tcp_minmss); 348497d8d152SAndre Oppermann } 3485a0292f23SGarrett Wollman 3486df8bae1dSRodney W. Grimes /* 3487e8949f74SAndre Oppermann * rmx information is now retrieved from tcp_hostcache. 3488df8bae1dSRodney W. Grimes */ 348997d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 34903cee92e0SBjoern A. Zeeb if (metricptr != NULL) 34913cee92e0SBjoern A. Zeeb bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 349297d8d152SAndre Oppermann 3493df8bae1dSRodney W. Grimes /* 3494e8949f74SAndre Oppermann * If there's a discovered mtu int tcp hostcache, use it 3495fb59c426SYoshinobu Inoue * else, use the link mtu. 3496df8bae1dSRodney W. Grimes */ 349797d8d152SAndre Oppermann if (metrics.rmx_mtu) 34982d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3499c068736aSJeffrey Hsu else { 350031b3783cSHajimu UMEMOTO #ifdef INET6 3501fb59c426SYoshinobu Inoue if (isipv6) { 350297d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3503603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 350497d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 3505603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_v6mssdflt); 3506b287c6c7SBjoern A. Zeeb } 3507b3399803SHajimu UMEMOTO #endif 3508b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3509b287c6c7SBjoern A. Zeeb else 3510b287c6c7SBjoern A. Zeeb #endif 3511b287c6c7SBjoern A. Zeeb #ifdef INET 351297d8d152SAndre Oppermann { 351397d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3514603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 351597d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 3516603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_mssdflt); 3517a0292f23SGarrett Wollman } 3518b287c6c7SBjoern A. Zeeb #endif 35193cee92e0SBjoern A. Zeeb /* 35203cee92e0SBjoern A. Zeeb * XXX - The above conditional (mss = maxmtu - min_protoh) 35213cee92e0SBjoern A. Zeeb * probably violates the TCP spec. 35223cee92e0SBjoern A. Zeeb * The problem is that, since we don't know the 35233cee92e0SBjoern A. Zeeb * other end's MSS, we are supposed to use a conservative 35243cee92e0SBjoern A. Zeeb * default. But, if we do that, then MTU discovery will 35253cee92e0SBjoern A. Zeeb * never actually take place, because the conservative 35263cee92e0SBjoern A. Zeeb * default is much less than the MTUs typically seen 35273cee92e0SBjoern A. Zeeb * on the Internet today. For the moment, we'll sweep 35283cee92e0SBjoern A. Zeeb * this under the carpet. 35293cee92e0SBjoern A. Zeeb * 35303cee92e0SBjoern A. Zeeb * The conservative default might not actually be a problem 35313cee92e0SBjoern A. Zeeb * if the only case this occurs is when sending an initial 35323cee92e0SBjoern A. Zeeb * SYN with options and data to a host we've never talked 35333cee92e0SBjoern A. Zeeb * to before. Then, they will reply with an MSS value which 35343cee92e0SBjoern A. Zeeb * will get recorded and the new parameters should get 35353cee92e0SBjoern A. Zeeb * recomputed. For Further Study. 35363cee92e0SBjoern A. Zeeb */ 353797d8d152SAndre Oppermann } 3538a0292f23SGarrett Wollman mss = min(mss, offer); 353997d8d152SAndre Oppermann 3540a0292f23SGarrett Wollman /* 35413cee92e0SBjoern A. Zeeb * Sanity check: make sure that maxopd will be large 35423cee92e0SBjoern A. Zeeb * enough to allow some data on segments even if the 35433cee92e0SBjoern A. Zeeb * all the option space is used (40bytes). Otherwise 35443cee92e0SBjoern A. Zeeb * funny things may happen in tcp_output. 35453cee92e0SBjoern A. Zeeb */ 35463cee92e0SBjoern A. Zeeb mss = max(mss, 64); 35473cee92e0SBjoern A. Zeeb 35483cee92e0SBjoern A. Zeeb /* 3549a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 3550a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 3551a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 3552a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 3553a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 3554a0292f23SGarrett Wollman */ 3555a0292f23SGarrett Wollman tp->t_maxopd = mss; 3556a0292f23SGarrett Wollman 3557a0292f23SGarrett Wollman /* 3558e8949f74SAndre Oppermann * origoffer==-1 indicates that no segments were received yet. 3559c94c54e4SAndre Oppermann * In this case we just guess. 3560a0292f23SGarrett Wollman */ 3561a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 3562a0292f23SGarrett Wollman (origoffer == -1 || 3563a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 3564a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 3565a0292f23SGarrett Wollman 356697d8d152SAndre Oppermann tp->t_maxseg = mss; 35673cee92e0SBjoern A. Zeeb } 35683cee92e0SBjoern A. Zeeb 35693cee92e0SBjoern A. Zeeb void 35703cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer) 35713cee92e0SBjoern A. Zeeb { 3572dbc42409SLawrence Stewart int mss; 35733cee92e0SBjoern A. Zeeb u_long bufsize; 35743cee92e0SBjoern A. Zeeb struct inpcb *inp; 35753cee92e0SBjoern A. Zeeb struct socket *so; 35763cee92e0SBjoern A. Zeeb struct hc_metrics_lite metrics; 35773c914c54SAndre Oppermann struct tcp_ifcap cap; 3578dbc42409SLawrence Stewart 35793cee92e0SBjoern A. Zeeb KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 35803cee92e0SBjoern A. Zeeb 35813c914c54SAndre Oppermann bzero(&cap, sizeof(cap)); 35823c914c54SAndre Oppermann tcp_mss_update(tp, offer, -1, &metrics, &cap); 35833cee92e0SBjoern A. Zeeb 35843cee92e0SBjoern A. Zeeb mss = tp->t_maxseg; 35853cee92e0SBjoern A. Zeeb inp = tp->t_inpcb; 358697d8d152SAndre Oppermann 3587df8bae1dSRodney W. Grimes /* 358897d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 358997d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 359097d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 359197d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 359297d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3593df8bae1dSRodney W. Grimes */ 3594c3b02504SBjoern A. Zeeb so = inp->inp_socket; 35953f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 3596e233e2acSAndre Oppermann if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe) 359797d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 359897d8d152SAndre Oppermann else 3599df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3600df8bae1dSRodney W. Grimes if (bufsize < mss) 3601df8bae1dSRodney W. Grimes mss = bufsize; 3602df8bae1dSRodney W. Grimes else { 3603df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3604df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3605df8bae1dSRodney W. Grimes bufsize = sb_max; 360688c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 36073f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3608df8bae1dSRodney W. Grimes } 36093f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 3610df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 3611df8bae1dSRodney W. Grimes 36123f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3613e233e2acSAndre Oppermann if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe) 361497d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 361597d8d152SAndre Oppermann else 3616df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3617df8bae1dSRodney W. Grimes if (bufsize > mss) { 3618df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3619df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3620df8bae1dSRodney W. Grimes bufsize = sb_max; 362188c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 36223f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3623df8bae1dSRodney W. Grimes } 36243f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 362591d6cfa6SBjoern A. Zeeb 362691d6cfa6SBjoern A. Zeeb /* Check the interface for TSO capabilities. */ 36273c914c54SAndre Oppermann if (cap.ifcap & CSUM_TSO) { 362891d6cfa6SBjoern A. Zeeb tp->t_flags |= TF_TSO; 36293c914c54SAndre Oppermann tp->t_tsomax = cap.tsomax; 36303c914c54SAndre Oppermann } 3631a0292f23SGarrett Wollman } 3632a0292f23SGarrett Wollman 3633a0292f23SGarrett Wollman /* 3634a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3635a0292f23SGarrett Wollman */ 3636a0292f23SGarrett Wollman int 3637ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 3638a0292f23SGarrett Wollman { 363997d8d152SAndre Oppermann int mss = 0; 364097d8d152SAndre Oppermann u_long maxmtu = 0; 364197d8d152SAndre Oppermann u_long thcmtu = 0; 364297d8d152SAndre Oppermann size_t min_protoh; 3643a0292f23SGarrett Wollman 364497d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3645a0292f23SGarrett Wollman 364631b3783cSHajimu UMEMOTO #ifdef INET6 3647dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3648603724d3SBjoern A. Zeeb mss = V_tcp_v6mssdflt; 3649233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 365097d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 3651b287c6c7SBjoern A. Zeeb } 365231b3783cSHajimu UMEMOTO #endif 3653b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3654b287c6c7SBjoern A. Zeeb else 3655b287c6c7SBjoern A. Zeeb #endif 3656b287c6c7SBjoern A. Zeeb #ifdef INET 365797d8d152SAndre Oppermann { 3658603724d3SBjoern A. Zeeb mss = V_tcp_mssdflt; 3659233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 366097d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 366197d8d152SAndre Oppermann } 3662b287c6c7SBjoern A. Zeeb #endif 36633a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET) 36643a9391deSBjoern A. Zeeb thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 36653a9391deSBjoern A. Zeeb #endif 36663a9391deSBjoern A. Zeeb 366797d8d152SAndre Oppermann if (maxmtu && thcmtu) 366897d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 366997d8d152SAndre Oppermann else if (maxmtu || thcmtu) 367097d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 367197d8d152SAndre Oppermann 367297d8d152SAndre Oppermann return (mss); 3673df8bae1dSRodney W. Grimes } 367446f58482SJonathan Lemon 367546f58482SJonathan Lemon 367646f58482SJonathan Lemon /* 3677c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3678c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3679c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3680c068736aSJeffrey Hsu * be started again. 368146f58482SJonathan Lemon */ 3682c068736aSJeffrey Hsu static void 3683ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 368446f58482SJonathan Lemon { 368546f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 368646f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 368746f58482SJonathan Lemon 36888501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 36897a3244ccSRobert Watson 3690b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 369146f58482SJonathan Lemon tp->t_rtttime = 0; 369246f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 36936b2a5f92SJayanth Vijayaraghavan /* 3694c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3695c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 36966b2a5f92SJayanth Vijayaraghavan */ 3697dbc42409SLawrence Stewart tp->snd_cwnd = tp->t_maxseg + BYTES_THIS_ACK(tp, th); 3698a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 36996b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 370046f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 370146f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 370246f58482SJonathan Lemon tp->snd_nxt = onxt; 370346f58482SJonathan Lemon /* 370446f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 370546f58482SJonathan Lemon * not updated yet. 370646f58482SJonathan Lemon */ 3707dbc42409SLawrence Stewart if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th)) 3708dbc42409SLawrence Stewart tp->snd_cwnd -= BYTES_THIS_ACK(tp, th); 3709d7587117SPaul Saab else 3710d7587117SPaul Saab tp->snd_cwnd = 0; 3711d7587117SPaul Saab tp->snd_cwnd += tp->t_maxseg; 371246f58482SJonathan Lemon } 3713