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 /* 243*5923c293SGleb Smirnoff * TCP statistics are stored in struct tcpstat_p, which is 244*5923c293SGleb Smirnoff * an "array" of counter(9)s. Although it isn't a real 245*5923c293SGleb Smirnoff * array, we treat it as array to reduce code bloat. 246*5923c293SGleb Smirnoff */ 247*5923c293SGleb Smirnoff VNET_DEFINE(struct tcpstat_p, tcpstatp); 248*5923c293SGleb Smirnoff 249*5923c293SGleb Smirnoff static void 250*5923c293SGleb Smirnoff vnet_tcpstatp_init(const void *unused) 251*5923c293SGleb Smirnoff { 252*5923c293SGleb Smirnoff counter_u64_t *c; 253*5923c293SGleb Smirnoff int i; 254*5923c293SGleb Smirnoff 255*5923c293SGleb Smirnoff for (i = 0, c = (counter_u64_t *)&V_tcpstatp; 256*5923c293SGleb Smirnoff i < sizeof(V_tcpstatp) / sizeof(counter_u64_t); 257*5923c293SGleb Smirnoff i++, c++) { 258*5923c293SGleb Smirnoff *c = counter_u64_alloc(M_WAITOK); 259*5923c293SGleb Smirnoff counter_u64_zero(*c); 260*5923c293SGleb Smirnoff } 261*5923c293SGleb Smirnoff } 262*5923c293SGleb Smirnoff VNET_SYSINIT(vnet_tcpstatp_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 263*5923c293SGleb Smirnoff vnet_tcpstatp_init, NULL); 264*5923c293SGleb Smirnoff 265*5923c293SGleb Smirnoff #ifdef VIMAGE 266*5923c293SGleb Smirnoff static void 267*5923c293SGleb Smirnoff vnet_tcpstatp_uninit(const void *unused) 268*5923c293SGleb Smirnoff { 269*5923c293SGleb Smirnoff counter_u64_t *c; 270*5923c293SGleb Smirnoff int i; 271*5923c293SGleb Smirnoff 272*5923c293SGleb Smirnoff for (i = 0, c = (counter_u64_t *)&V_tcpstatp; 273*5923c293SGleb Smirnoff i < sizeof(V_tcpstatp) / sizeof(counter_u64_t); 274*5923c293SGleb Smirnoff i++, c++) 275*5923c293SGleb Smirnoff counter_u64_free(*c); 276*5923c293SGleb Smirnoff } 277*5923c293SGleb Smirnoff VNET_SYSUNINIT(vnet_tcpstatp_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 278*5923c293SGleb Smirnoff vnet_ipstatp_uninit, NULL); 279*5923c293SGleb Smirnoff #endif /* VIMAGE */ 280*5923c293SGleb Smirnoff 281*5923c293SGleb Smirnoff static int 282*5923c293SGleb Smirnoff tcpstat_sysctl(SYSCTL_HANDLER_ARGS) 283*5923c293SGleb Smirnoff { 284*5923c293SGleb Smirnoff struct tcpstat tcpstat; 285*5923c293SGleb Smirnoff counter_u64_t *c; 286*5923c293SGleb Smirnoff uint64_t *v; 287*5923c293SGleb Smirnoff int i; 288*5923c293SGleb Smirnoff 289*5923c293SGleb Smirnoff for (i = 0, c = (counter_u64_t *)&V_tcpstatp, v = (uint64_t *)&tcpstat; 290*5923c293SGleb Smirnoff i < sizeof(V_tcpstatp) / sizeof(counter_u64_t); 291*5923c293SGleb Smirnoff i++, c++, v++) { 292*5923c293SGleb Smirnoff *v = counter_u64_fetch(*c); 293*5923c293SGleb Smirnoff if (req->newptr) 294*5923c293SGleb Smirnoff counter_u64_zero(*c); 295*5923c293SGleb Smirnoff } 296*5923c293SGleb Smirnoff 297*5923c293SGleb Smirnoff return (SYSCTL_OUT(req, &tcpstat, sizeof(tcpstat))); 298*5923c293SGleb Smirnoff } 299*5923c293SGleb Smirnoff 300*5923c293SGleb Smirnoff SYSCTL_VNET_PROC(_net_inet_tcp, TCPCTL_STATS, stats, CTLTYPE_OPAQUE | 301*5923c293SGleb Smirnoff CTLFLAG_RW, NULL, 0, tcpstat_sysctl, "I", 302*5923c293SGleb Smirnoff "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 303*5923c293SGleb Smirnoff 304*5923c293SGleb Smirnoff /* 305315e3e38SRobert Watson * Kernel module interface for updating tcpstat. The argument is an index 306*5923c293SGleb Smirnoff * into tcpstat treated as an array. 307315e3e38SRobert Watson */ 308315e3e38SRobert Watson void 309315e3e38SRobert Watson kmod_tcpstat_inc(int statnum) 310315e3e38SRobert Watson { 311315e3e38SRobert Watson 312*5923c293SGleb Smirnoff counter_u64_add((counter_u64_t )&V_tcpstatp + statnum, 1); 313315e3e38SRobert Watson } 314315e3e38SRobert Watson 315dbc42409SLawrence Stewart /* 31639bc9de5SLawrence Stewart * Wrapper for the TCP established input helper hook. 31739bc9de5SLawrence Stewart */ 31839bc9de5SLawrence Stewart static void inline 31939bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to) 32039bc9de5SLawrence Stewart { 32139bc9de5SLawrence Stewart struct tcp_hhook_data hhook_data; 32239bc9de5SLawrence Stewart 32339bc9de5SLawrence Stewart if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) { 32439bc9de5SLawrence Stewart hhook_data.tp = tp; 32539bc9de5SLawrence Stewart hhook_data.th = th; 32639bc9de5SLawrence Stewart hhook_data.to = to; 32739bc9de5SLawrence Stewart 32839bc9de5SLawrence Stewart hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data, 32939bc9de5SLawrence Stewart tp->osd); 33039bc9de5SLawrence Stewart } 33139bc9de5SLawrence Stewart } 33239bc9de5SLawrence Stewart 33339bc9de5SLawrence Stewart /* 334dbc42409SLawrence Stewart * CC wrapper hook functions 335dbc42409SLawrence Stewart */ 336f2512ba1SRui Paulo static void inline 337dbc42409SLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t type) 338f2512ba1SRui Paulo { 339dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 340f2512ba1SRui Paulo 341dbc42409SLawrence Stewart tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th); 3425b648e79SLawrence Stewart if (tp->snd_cwnd <= tp->snd_wnd) 343dbc42409SLawrence Stewart tp->ccv->flags |= CCF_CWND_LIMITED; 344dbc42409SLawrence Stewart else 345dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_CWND_LIMITED; 346dbc42409SLawrence Stewart 347dbc42409SLawrence Stewart if (type == CC_ACK) { 348dbc42409SLawrence Stewart if (tp->snd_cwnd > tp->snd_ssthresh) { 349dbc42409SLawrence Stewart tp->t_bytes_acked += min(tp->ccv->bytes_this_ack, 350dbc42409SLawrence Stewart V_tcp_abc_l_var * tp->t_maxseg); 351dbc42409SLawrence Stewart if (tp->t_bytes_acked >= tp->snd_cwnd) { 352dbc42409SLawrence Stewart tp->t_bytes_acked -= tp->snd_cwnd; 353dbc42409SLawrence Stewart tp->ccv->flags |= CCF_ABC_SENTAWND; 354dbc42409SLawrence Stewart } 355dbc42409SLawrence Stewart } else { 356dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_ABC_SENTAWND; 357dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 358dbc42409SLawrence Stewart } 359dbc42409SLawrence Stewart } 360dbc42409SLawrence Stewart 361dbc42409SLawrence Stewart if (CC_ALGO(tp)->ack_received != NULL) { 362dbc42409SLawrence Stewart /* XXXLAS: Find a way to live without this */ 363dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 364dbc42409SLawrence Stewart CC_ALGO(tp)->ack_received(tp->ccv, type); 365dbc42409SLawrence Stewart } 366dbc42409SLawrence Stewart } 367dbc42409SLawrence Stewart 368dbc42409SLawrence Stewart static void inline 369dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp) 370dbc42409SLawrence Stewart { 371dbc42409SLawrence Stewart struct hc_metrics_lite metrics; 372dbc42409SLawrence Stewart struct inpcb *inp = tp->t_inpcb; 373dbc42409SLawrence Stewart int rtt; 374dbc42409SLawrence Stewart 375dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 376dbc42409SLawrence Stewart 377dbc42409SLawrence Stewart tcp_hc_get(&inp->inp_inc, &metrics); 378dbc42409SLawrence Stewart 379dbc42409SLawrence Stewart if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 380dbc42409SLawrence Stewart tp->t_srtt = rtt; 381dbc42409SLawrence Stewart tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 382dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrtt); 383dbc42409SLawrence Stewart if (metrics.rmx_rttvar) { 384dbc42409SLawrence Stewart tp->t_rttvar = metrics.rmx_rttvar; 385dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrttvar); 386dbc42409SLawrence Stewart } else { 387dbc42409SLawrence Stewart /* default variation is +- 1 rtt */ 388dbc42409SLawrence Stewart tp->t_rttvar = 389dbc42409SLawrence Stewart tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 390dbc42409SLawrence Stewart } 391dbc42409SLawrence Stewart TCPT_RANGESET(tp->t_rxtcur, 392dbc42409SLawrence Stewart ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 393dbc42409SLawrence Stewart tp->t_rttmin, TCPTV_REXMTMAX); 394dbc42409SLawrence Stewart } 395dbc42409SLawrence Stewart if (metrics.rmx_ssthresh) { 396dbc42409SLawrence Stewart /* 397dbc42409SLawrence Stewart * There's some sort of gateway or interface 398dbc42409SLawrence Stewart * buffer limit on the path. Use this to set 399dbc42409SLawrence Stewart * the slow start threshhold, but set the 400dbc42409SLawrence Stewart * threshold to no less than 2*mss. 401dbc42409SLawrence Stewart */ 402dbc42409SLawrence Stewart tp->snd_ssthresh = max(2 * tp->t_maxseg, metrics.rmx_ssthresh); 403dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedssthresh); 404dbc42409SLawrence Stewart } 405dbc42409SLawrence Stewart 406dbc42409SLawrence Stewart /* 4079ec4a4ccSAndre Oppermann * Set the initial slow-start flight size. 408dbc42409SLawrence Stewart * 409cf8f04f4SAndre Oppermann * RFC5681 Section 3.1 specifies the default conservative values. 410cf8f04f4SAndre Oppermann * RFC3390 specifies slightly more aggressive values. 41109440655SAndre Oppermann * Draft-ietf-tcpm-initcwnd-05 increases it to ten segments. 412cf8f04f4SAndre Oppermann * 413cf8f04f4SAndre Oppermann * If a SYN or SYN/ACK was lost and retransmitted, we have to 414cf8f04f4SAndre Oppermann * reduce the initial CWND to one segment as congestion is likely 415cf8f04f4SAndre Oppermann * requiring us to be cautious. 416dbc42409SLawrence Stewart */ 417cf8f04f4SAndre Oppermann if (tp->snd_cwnd == 1) 418cf8f04f4SAndre Oppermann tp->snd_cwnd = tp->t_maxseg; /* SYN(-ACK) lost */ 41909440655SAndre Oppermann else if (V_tcp_do_initcwnd10) 42009440655SAndre Oppermann tp->snd_cwnd = min(10 * tp->t_maxseg, 42109440655SAndre Oppermann max(2 * tp->t_maxseg, 14600)); 422cf8f04f4SAndre Oppermann else if (V_tcp_do_rfc3390) 423dbc42409SLawrence Stewart tp->snd_cwnd = min(4 * tp->t_maxseg, 424dbc42409SLawrence Stewart max(2 * tp->t_maxseg, 4380)); 42522efabd4SAndre Oppermann else { 42622efabd4SAndre Oppermann /* Per RFC5681 Section 3.1 */ 42722efabd4SAndre Oppermann if (tp->t_maxseg > 2190) 42822efabd4SAndre Oppermann tp->snd_cwnd = 2 * tp->t_maxseg; 42922efabd4SAndre Oppermann else if (tp->t_maxseg > 1095) 43022efabd4SAndre Oppermann tp->snd_cwnd = 3 * tp->t_maxseg; 431dbc42409SLawrence Stewart else 43222efabd4SAndre Oppermann tp->snd_cwnd = 4 * tp->t_maxseg; 43322efabd4SAndre Oppermann } 434dbc42409SLawrence Stewart 435dbc42409SLawrence Stewart if (CC_ALGO(tp)->conn_init != NULL) 436dbc42409SLawrence Stewart CC_ALGO(tp)->conn_init(tp->ccv); 437dbc42409SLawrence Stewart } 438dbc42409SLawrence Stewart 439dbc42409SLawrence Stewart void inline 440dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type) 441dbc42409SLawrence Stewart { 442dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 443dbc42409SLawrence Stewart 444dbc42409SLawrence Stewart switch(type) { 445dbc42409SLawrence Stewart case CC_NDUPACK: 446dbc42409SLawrence Stewart if (!IN_FASTRECOVERY(tp->t_flags)) { 447f2512ba1SRui Paulo tp->snd_recover = tp->snd_max; 448f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) 449f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_CWR; 450f2512ba1SRui Paulo } 451dbc42409SLawrence Stewart break; 452dbc42409SLawrence Stewart case CC_ECN: 453dbc42409SLawrence Stewart if (!IN_CONGRECOVERY(tp->t_flags)) { 454dbc42409SLawrence Stewart TCPSTAT_INC(tcps_ecn_rcwnd); 455dbc42409SLawrence Stewart tp->snd_recover = tp->snd_max; 456dbc42409SLawrence Stewart if (tp->t_flags & TF_ECN_PERMIT) 457dbc42409SLawrence Stewart tp->t_flags |= TF_ECN_SND_CWR; 458dbc42409SLawrence Stewart } 459dbc42409SLawrence Stewart break; 460dbc42409SLawrence Stewart case CC_RTO: 461dbc42409SLawrence Stewart tp->t_dupacks = 0; 462dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 463dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 4646157935fSLawrence Stewart tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 / 4656157935fSLawrence Stewart tp->t_maxseg) * tp->t_maxseg; 466dbc42409SLawrence Stewart tp->snd_cwnd = tp->t_maxseg; 467dbc42409SLawrence Stewart break; 468dbc42409SLawrence Stewart case CC_RTO_ERR: 469dbc42409SLawrence Stewart TCPSTAT_INC(tcps_sndrexmitbad); 470dbc42409SLawrence Stewart /* RTO was unnecessary, so reset everything. */ 471dbc42409SLawrence Stewart tp->snd_cwnd = tp->snd_cwnd_prev; 472dbc42409SLawrence Stewart tp->snd_ssthresh = tp->snd_ssthresh_prev; 473dbc42409SLawrence Stewart tp->snd_recover = tp->snd_recover_prev; 474dbc42409SLawrence Stewart if (tp->t_flags & TF_WASFRECOVERY) 475dbc42409SLawrence Stewart ENTER_FASTRECOVERY(tp->t_flags); 476dbc42409SLawrence Stewart if (tp->t_flags & TF_WASCRECOVERY) 477dbc42409SLawrence Stewart ENTER_CONGRECOVERY(tp->t_flags); 478dbc42409SLawrence Stewart tp->snd_nxt = tp->snd_max; 479672dc4aeSJohn Baldwin tp->t_flags &= ~TF_PREVVALID; 480dbc42409SLawrence Stewart tp->t_badrxtwin = 0; 481dbc42409SLawrence Stewart break; 482dbc42409SLawrence Stewart } 483dbc42409SLawrence Stewart 484dbc42409SLawrence Stewart if (CC_ALGO(tp)->cong_signal != NULL) { 485dbc42409SLawrence Stewart if (th != NULL) 486dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 487dbc42409SLawrence Stewart CC_ALGO(tp)->cong_signal(tp->ccv, type); 488dbc42409SLawrence Stewart } 489dbc42409SLawrence Stewart } 490dbc42409SLawrence Stewart 491dbc42409SLawrence Stewart static void inline 492dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th) 493dbc42409SLawrence Stewart { 494dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 495dbc42409SLawrence Stewart 496dbc42409SLawrence Stewart /* XXXLAS: KASSERT that we're in recovery? */ 497dbc42409SLawrence Stewart 498dbc42409SLawrence Stewart if (CC_ALGO(tp)->post_recovery != NULL) { 499dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 500dbc42409SLawrence Stewart CC_ALGO(tp)->post_recovery(tp->ccv); 501dbc42409SLawrence Stewart } 502dbc42409SLawrence Stewart /* XXXLAS: EXIT_RECOVERY ? */ 503dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 504dbc42409SLawrence Stewart } 5050312fbe9SPoul-Henning Kamp 5062903309aSAttilio Rao static inline void 5072903309aSAttilio Rao tcp_fields_to_host(struct tcphdr *th) 5082903309aSAttilio Rao { 5092903309aSAttilio Rao 5102903309aSAttilio Rao th->th_seq = ntohl(th->th_seq); 5112903309aSAttilio Rao th->th_ack = ntohl(th->th_ack); 5122903309aSAttilio Rao th->th_win = ntohs(th->th_win); 5132903309aSAttilio Rao th->th_urp = ntohs(th->th_urp); 5142903309aSAttilio Rao } 5152903309aSAttilio Rao 5162903309aSAttilio Rao #ifdef TCP_SIGNATURE 5172903309aSAttilio Rao static inline void 5182903309aSAttilio Rao tcp_fields_to_net(struct tcphdr *th) 5192903309aSAttilio Rao { 5202903309aSAttilio Rao 5212903309aSAttilio Rao th->th_seq = htonl(th->th_seq); 5222903309aSAttilio Rao th->th_ack = htonl(th->th_ack); 5232903309aSAttilio Rao th->th_win = htons(th->th_win); 5242903309aSAttilio Rao th->th_urp = htons(th->th_urp); 5252903309aSAttilio Rao } 5262903309aSAttilio Rao 5272903309aSAttilio Rao static inline int 5282903309aSAttilio Rao tcp_signature_verify_input(struct mbuf *m, int off0, int tlen, int optlen, 5292903309aSAttilio Rao struct tcpopt *to, struct tcphdr *th, u_int tcpbflag) 5302903309aSAttilio Rao { 5312903309aSAttilio Rao int ret; 5322903309aSAttilio Rao 5332903309aSAttilio Rao tcp_fields_to_net(th); 5342903309aSAttilio Rao ret = tcp_signature_verify(m, off0, tlen, optlen, to, th, tcpbflag); 5352903309aSAttilio Rao tcp_fields_to_host(th); 5362903309aSAttilio Rao return (ret); 5372903309aSAttilio Rao } 5382903309aSAttilio Rao #endif 5392903309aSAttilio Rao 540fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 541fb59c426SYoshinobu Inoue #ifdef INET6 542fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \ 543fb59c426SYoshinobu Inoue do { \ 544fb59c426SYoshinobu Inoue if ((tp) && (tp)->t_inpcb && \ 54597d8d152SAndre Oppermann ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \ 54697d8d152SAndre Oppermann nd6_nud_hint(NULL, NULL, 0); \ 547fb59c426SYoshinobu Inoue } while (0) 548fb59c426SYoshinobu Inoue #else 549fb59c426SYoshinobu Inoue #define ND6_HINT(tp) 550fb59c426SYoshinobu Inoue #endif 551df8bae1dSRodney W. Grimes 552df8bae1dSRodney W. Grimes /* 553262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 554262c1c1aSMatthew Dillon * - there is no delayed ack timer in progress and 555262c1c1aSMatthew Dillon * - our last ack wasn't a 0-sized window. We never want to delay 5563bfd6421SJonathan Lemon * the ack that opens up a 0-sized window and 5573bfd6421SJonathan Lemon * - delayed acks are enabled or 5583bfd6421SJonathan Lemon * - this is a half-synchronized T/TCP connection. 559d8c85a26SJonathan Lemon */ 560d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \ 561b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 562a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 563603724d3SBjoern A. Zeeb (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 564d8c85a26SJonathan Lemon 565df8bae1dSRodney W. Grimes /* 5668d573cc1SAndre Oppermann * TCP input handling is split into multiple parts: 5678d573cc1SAndre Oppermann * tcp6_input is a thin wrapper around tcp_input for the extended 5688d573cc1SAndre Oppermann * ip6_protox[] call format in ip6_input 5698d573cc1SAndre Oppermann * tcp_input handles primary segment validation, inpcb lookup and 5708d573cc1SAndre Oppermann * SYN processing on listen sockets 5718d573cc1SAndre Oppermann * tcp_do_segment processes the ACK and text of the segment for 5728d573cc1SAndre Oppermann * establishing, established and closing connections 573df8bae1dSRodney W. Grimes */ 574fb59c426SYoshinobu Inoue #ifdef INET6 575fb59c426SYoshinobu Inoue int 576ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto) 577fb59c426SYoshinobu Inoue { 578ad3f9ab3SAndre Oppermann struct mbuf *m = *mp; 57933841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 580fb59c426SYoshinobu Inoue 581fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 582fb59c426SYoshinobu Inoue 583fb59c426SYoshinobu Inoue /* 584fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 585fb59c426SYoshinobu Inoue * better place to put this in? 586fb59c426SYoshinobu Inoue */ 58733841545SHajimu UMEMOTO ia6 = ip6_getdstifaddr(m); 58833841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 589fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 590fb59c426SYoshinobu Inoue 5918c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 592fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 593fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 594fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 595fb59c426SYoshinobu Inoue return IPPROTO_DONE; 596fb59c426SYoshinobu Inoue } 59777d396fdSMaksim Yevmenkin if (ia6) 59877d396fdSMaksim Yevmenkin ifa_free(&ia6->ia_ifa); 599fb59c426SYoshinobu Inoue 600f0ffb944SJulian Elischer tcp_input(m, *offp); 601fb59c426SYoshinobu Inoue return IPPROTO_DONE; 602fb59c426SYoshinobu Inoue } 603b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 604fb59c426SYoshinobu Inoue 605df8bae1dSRodney W. Grimes void 606ad3f9ab3SAndre Oppermann tcp_input(struct mbuf *m, int off0) 607df8bae1dSRodney W. Grimes { 608b287c6c7SBjoern A. Zeeb struct tcphdr *th = NULL; 609ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 610ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 611302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 612302ce8d6SAndre Oppermann struct socket *so = NULL; 613e79adb8eSGarrett Wollman u_char *optp = NULL; 61426f9a767SRodney W. Grimes int optlen = 0; 615b287c6c7SBjoern A. Zeeb #ifdef INET 616b287c6c7SBjoern A. Zeeb int len; 617b287c6c7SBjoern A. Zeeb #endif 618b287c6c7SBjoern A. Zeeb int tlen = 0, off; 619fb59c426SYoshinobu Inoue int drop_hdrlen; 620ad3f9ab3SAndre Oppermann int thflags; 621302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 6222903309aSAttilio Rao #ifdef TCP_SIGNATURE 6232903309aSAttilio Rao uint8_t sig_checked = 0; 6242903309aSAttilio Rao #endif 625b287c6c7SBjoern A. Zeeb uint8_t iptos = 0; 626c1de64a4SAndrey V. Elsukov struct m_tag *fwd_tag = NULL; 627c068736aSJeffrey Hsu #ifdef INET6 628302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 629c068736aSJeffrey Hsu int isipv6; 630c068736aSJeffrey Hsu #else 631a160e630SAndre Oppermann const void *ip6 = NULL; 632b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 633302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 634a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 635252ca428SRobert Watson int ti_locked; 636252ca428SRobert Watson #define TI_UNLOCKED 1 637fa046d87SRobert Watson #define TI_WLOCKED 2 638f76fcf6dSJeffrey Hsu 639610ee2f9SDavid Greenman #ifdef TCPDEBUG 640c068736aSJeffrey Hsu /* 641c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 642c068736aSJeffrey Hsu * now IPv6. 643c068736aSJeffrey Hsu */ 64414739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 645410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 646610ee2f9SDavid Greenman short ostate = 0; 647610ee2f9SDavid Greenman #endif 648c068736aSJeffrey Hsu 649fb59c426SYoshinobu Inoue #ifdef INET6 650fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 651fb59c426SYoshinobu Inoue #endif 652a0292f23SGarrett Wollman 653302ce8d6SAndre Oppermann to.to_flags = 0; 65478b50714SRobert Watson TCPSTAT_INC(tcps_rcvtotal); 655fb59c426SYoshinobu Inoue 65604d3a452SHajimu UMEMOTO #ifdef INET6 657b287c6c7SBjoern A. Zeeb if (isipv6) { 6588d573cc1SAndre Oppermann /* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */ 65945747ba5SBjoern A. Zeeb 66045747ba5SBjoern A. Zeeb if (m->m_len < (sizeof(*ip6) + sizeof(*th))) { 66145747ba5SBjoern A. Zeeb m = m_pullup(m, sizeof(*ip6) + sizeof(*th)); 66245747ba5SBjoern A. Zeeb if (m == NULL) { 66345747ba5SBjoern A. Zeeb TCPSTAT_INC(tcps_rcvshort); 66445747ba5SBjoern A. Zeeb return; 66545747ba5SBjoern A. Zeeb } 66645747ba5SBjoern A. Zeeb } 66745747ba5SBjoern A. Zeeb 668fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 66945747ba5SBjoern A. Zeeb th = (struct tcphdr *)((caddr_t)ip6 + off0); 670fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 671356ab07eSBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) { 67245747ba5SBjoern A. Zeeb if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 67345747ba5SBjoern A. Zeeb th->th_sum = m->m_pkthdr.csum_data; 67445747ba5SBjoern A. Zeeb else 67545747ba5SBjoern A. Zeeb th->th_sum = in6_cksum_pseudo(ip6, tlen, 67645747ba5SBjoern A. Zeeb IPPROTO_TCP, m->m_pkthdr.csum_data); 67745747ba5SBjoern A. Zeeb th->th_sum ^= 0xffff; 67845747ba5SBjoern A. Zeeb } else 67945747ba5SBjoern A. Zeeb th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen); 68045747ba5SBjoern A. Zeeb if (th->th_sum) { 68178b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 682fb59c426SYoshinobu Inoue goto drop; 683fb59c426SYoshinobu Inoue } 68433841545SHajimu UMEMOTO 68533841545SHajimu UMEMOTO /* 68633841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 68733841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 68833841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 68933841545SHajimu UMEMOTO * 69033841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 69133841545SHajimu UMEMOTO * already dropped in ip6_input. 69233841545SHajimu UMEMOTO */ 69333841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 69433841545SHajimu UMEMOTO /* XXX stat */ 69533841545SHajimu UMEMOTO goto drop; 69633841545SHajimu UMEMOTO } 697b287c6c7SBjoern A. Zeeb } 69804d3a452SHajimu UMEMOTO #endif 699b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 700b287c6c7SBjoern A. Zeeb else 701b287c6c7SBjoern A. Zeeb #endif 702b287c6c7SBjoern A. Zeeb #ifdef INET 703b287c6c7SBjoern A. Zeeb { 704df8bae1dSRodney W. Grimes /* 705df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 706df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 707df8bae1dSRodney W. Grimes */ 708fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 709105bd211SGleb Smirnoff ip_stripoptions(m); 710fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 711fb59c426SYoshinobu Inoue } 712df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 7134dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 7144dfdffe9SAndre Oppermann == NULL) { 71578b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 716df8bae1dSRodney W. Grimes return; 717df8bae1dSRodney W. Grimes } 718df8bae1dSRodney W. Grimes } 719fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 720db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 7218ad458a4SGleb Smirnoff tlen = ntohs(ip->ip_len) - off0; 722df8bae1dSRodney W. Grimes 723db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 724db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 725db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 726db4f9cc7SJonathan Lemon else 727db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 728c068736aSJeffrey Hsu ip->ip_dst.s_addr, 7298f134647SGleb Smirnoff htonl(m->m_pkthdr.csum_data + tlen + 730c068736aSJeffrey Hsu IPPROTO_TCP)); 731db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 732db4f9cc7SJonathan Lemon } else { 7338f134647SGleb Smirnoff struct ipovly *ipov = (struct ipovly *)ip; 7348f134647SGleb Smirnoff 735df8bae1dSRodney W. Grimes /* 736df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 737df8bae1dSRodney W. Grimes */ 7388f134647SGleb Smirnoff len = off0 + tlen; 739fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 7408f134647SGleb Smirnoff ipov->ih_len = htons(tlen); 741fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 742db4f9cc7SJonathan Lemon } 743fb59c426SYoshinobu Inoue if (th->th_sum) { 74478b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 745df8bae1dSRodney W. Grimes goto drop; 746df8bae1dSRodney W. Grimes } 747fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 748fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 749fb59c426SYoshinobu Inoue } 750b287c6c7SBjoern A. Zeeb #endif /* INET */ 751df8bae1dSRodney W. Grimes 752f2512ba1SRui Paulo #ifdef INET6 753f2512ba1SRui Paulo if (isipv6) 754f2512ba1SRui Paulo iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 755b287c6c7SBjoern A. Zeeb #endif 756b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 757f2512ba1SRui Paulo else 758f2512ba1SRui Paulo #endif 759b287c6c7SBjoern A. Zeeb #ifdef INET 760f2512ba1SRui Paulo iptos = ip->ip_tos; 761b287c6c7SBjoern A. Zeeb #endif 762f2512ba1SRui Paulo 763df8bae1dSRodney W. Grimes /* 764df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 765df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 766df8bae1dSRodney W. Grimes */ 767fb59c426SYoshinobu Inoue off = th->th_off << 2; 768df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 76978b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadoff); 770df8bae1dSRodney W. Grimes goto drop; 771df8bae1dSRodney W. Grimes } 772fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 773df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 77404d3a452SHajimu UMEMOTO #ifdef INET6 775b287c6c7SBjoern A. Zeeb if (isipv6) { 776fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 777fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 778fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 779b287c6c7SBjoern A. Zeeb } 78004d3a452SHajimu UMEMOTO #endif 781b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 782b287c6c7SBjoern A. Zeeb else 783b287c6c7SBjoern A. Zeeb #endif 784b287c6c7SBjoern A. Zeeb #ifdef INET 785b287c6c7SBjoern A. Zeeb { 786df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 787c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 7884dfdffe9SAndre Oppermann == NULL) { 78978b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 790df8bae1dSRodney W. Grimes return; 791df8bae1dSRodney W. Grimes } 792fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 793fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 794fb59c426SYoshinobu Inoue } 795df8bae1dSRodney W. Grimes } 796b287c6c7SBjoern A. Zeeb #endif 797df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 798fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 799df8bae1dSRodney W. Grimes } 800fb59c426SYoshinobu Inoue thflags = th->th_flags; 801df8bae1dSRodney W. Grimes 802e46cd3d4SDag-Erling Smørgrav /* 803df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 804df8bae1dSRodney W. Grimes */ 8052903309aSAttilio Rao tcp_fields_to_host(th); 806df8bae1dSRodney W. Grimes 807df8bae1dSRodney W. Grimes /* 808794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 809755c1f07SAndras Olah */ 810fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 811755c1f07SAndras Olah 812755c1f07SAndras Olah /* 813fa046d87SRobert Watson * Locate pcb for segment; if we're likely to add or remove a 814fa046d87SRobert Watson * connection then first acquire pcbinfo lock. There are two cases 815fa046d87SRobert Watson * where we might discover later we need a write lock despite the 816fa046d87SRobert Watson * flags: ACKs moving a connection out of the syncache, and ACKs for 817fa046d87SRobert Watson * a connection in TIMEWAIT. 818df8bae1dSRodney W. Grimes */ 819fa046d87SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0) { 820603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 821252ca428SRobert Watson ti_locked = TI_WLOCKED; 822fa046d87SRobert Watson } else 823fa046d87SRobert Watson ti_locked = TI_UNLOCKED; 824252ca428SRobert Watson 8258d573cc1SAndre Oppermann /* 8268d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 8278d573cc1SAndre Oppermann */ 828b8056faeSGleb Smirnoff if ( 829b8056faeSGleb Smirnoff #ifdef INET6 830b8056faeSGleb Smirnoff (isipv6 && (m->m_flags & M_IP6_NEXTHOP)) 831b8056faeSGleb Smirnoff #ifdef INET 832b8056faeSGleb Smirnoff || (!isipv6 && (m->m_flags & M_IP_NEXTHOP)) 833b8056faeSGleb Smirnoff #endif 834b8056faeSGleb Smirnoff #endif 835b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6) 836b8056faeSGleb Smirnoff (m->m_flags & M_IP_NEXTHOP) 837b8056faeSGleb Smirnoff #endif 838b8056faeSGleb Smirnoff ) 8399b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 8409b932e9eSAndre Oppermann 841ce7ad664SEd Maste findpcb: 842ce7ad664SEd Maste #ifdef INVARIANTS 843ce7ad664SEd Maste if (ti_locked == TI_WLOCKED) { 844ce7ad664SEd Maste INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 845ce7ad664SEd Maste } else { 846ce7ad664SEd Maste INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 847ce7ad664SEd Maste } 848ce7ad664SEd Maste #endif 8498a006adbSBjoern A. Zeeb #ifdef INET6 8508a006adbSBjoern A. Zeeb if (isipv6 && fwd_tag != NULL) { 8518a006adbSBjoern A. Zeeb struct sockaddr_in6 *next_hop6; 8528a006adbSBjoern A. Zeeb 8538a006adbSBjoern A. Zeeb next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1); 8548a006adbSBjoern A. Zeeb /* 8558a006adbSBjoern A. Zeeb * Transparently forwarded. Pretend to be the destination. 8568a006adbSBjoern A. Zeeb * Already got one like this? 8578a006adbSBjoern A. Zeeb */ 8588a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, 8598a006adbSBjoern A. Zeeb &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport, 8608a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m); 8618a006adbSBjoern A. Zeeb if (!inp) { 8628a006adbSBjoern A. Zeeb /* 8638a006adbSBjoern A. Zeeb * It's new. Try to find the ambushing socket. 8648a006adbSBjoern A. Zeeb * Because we've rewritten the destination address, 8658a006adbSBjoern A. Zeeb * any hardware-generated hash is ignored. 8668a006adbSBjoern A. Zeeb */ 8678a006adbSBjoern A. Zeeb inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src, 8688a006adbSBjoern A. Zeeb th->th_sport, &next_hop6->sin6_addr, 8698a006adbSBjoern A. Zeeb next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) : 8708a006adbSBjoern A. Zeeb th->th_dport, INPLOOKUP_WILDCARD | 8718a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 8728a006adbSBjoern A. Zeeb } 873c1de64a4SAndrey V. Elsukov } else if (isipv6) { 8748a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src, 8758a006adbSBjoern A. Zeeb th->th_sport, &ip6->ip6_dst, th->th_dport, 8768a006adbSBjoern A. Zeeb INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 8778a006adbSBjoern A. Zeeb m->m_pkthdr.rcvif, m); 8788a006adbSBjoern A. Zeeb } 8798a006adbSBjoern A. Zeeb #endif /* INET6 */ 8808a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET) 8818a006adbSBjoern A. Zeeb else 8828a006adbSBjoern A. Zeeb #endif 8838a006adbSBjoern A. Zeeb #ifdef INET 8848a006adbSBjoern A. Zeeb if (fwd_tag != NULL) { 8859b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 8869b932e9eSAndre Oppermann 8879b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 888f9e354dfSJulian Elischer /* 8892b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 890f9e354dfSJulian Elischer * already got one like this? 891f9e354dfSJulian Elischer */ 892d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport, 893fa046d87SRobert Watson ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB, 894d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 895fa046d87SRobert Watson if (!inp) { 896fa046d87SRobert Watson /* 897fa046d87SRobert Watson * It's new. Try to find the ambushing socket. 898d3c1f003SRobert Watson * Because we've rewritten the destination address, 899d3c1f003SRobert Watson * any hardware-generated hash is ignored. 900fa046d87SRobert Watson */ 901fa046d87SRobert Watson inp = in_pcblookup(&V_tcbinfo, ip->ip_src, 902fa046d87SRobert Watson th->th_sport, next_hop->sin_addr, 903fa046d87SRobert Watson next_hop->sin_port ? ntohs(next_hop->sin_port) : 904fa046d87SRobert Watson th->th_dport, INPLOOKUP_WILDCARD | 905fa046d87SRobert Watson INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 906f9e354dfSJulian Elischer } 907b10fbdeaSAndre Oppermann } else 908d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, 909fa046d87SRobert Watson th->th_sport, ip->ip_dst, th->th_dport, 910fa046d87SRobert Watson INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 911d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 9128a006adbSBjoern A. Zeeb #endif /* INET */ 913f9e354dfSJulian Elischer 914df8bae1dSRodney W. Grimes /* 915574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 916574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 9178b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 918df8bae1dSRodney W. Grimes */ 919816a3d83SPoul-Henning Kamp if (inp == NULL) { 920574b6964SAndre Oppermann /* 921574b6964SAndre Oppermann * Log communication attempts to ports that are not 922574b6964SAndre Oppermann * in use. 923574b6964SAndre Oppermann */ 924574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 925574b6964SAndre Oppermann tcp_log_in_vain == 2) { 926b7d747ecSAndre Oppermann if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6))) 927df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 928df541e5fSAndre Oppermann "to closed port\n", s, __func__); 9292e4e1b4cSGeoff Rehmet } 930574b6964SAndre Oppermann /* 931574b6964SAndre Oppermann * When blackholing do not respond with a RST but 932574b6964SAndre Oppermann * completely ignore the segment and drop it. 933574b6964SAndre Oppermann */ 934603724d3SBjoern A. Zeeb if ((V_blackhole == 1 && (thflags & TH_SYN)) || 935603724d3SBjoern A. Zeeb V_blackhole == 2) 9361929eae1SAndre Oppermann goto dropunlock; 937574b6964SAndre Oppermann 938a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 939a57815efSBosko Milekic goto dropwithreset; 940816a3d83SPoul-Henning Kamp } 941fa046d87SRobert Watson INP_WLOCK_ASSERT(inp); 94280cb9f21SKip Macy if (!(inp->inp_flags & INP_HW_FLOWID) 94380cb9f21SKip Macy && (m->m_flags & M_FLOWID) 94480cb9f21SKip Macy && ((inp->inp_socket == NULL) 94580cb9f21SKip Macy || !(inp->inp_socket->so_options & SO_ACCEPTCONN))) { 94680cb9f21SKip Macy inp->inp_flags |= INP_HW_FLOWID; 94780cb9f21SKip Macy inp->inp_flags &= ~INP_SW_FLOWID; 94880cb9f21SKip Macy inp->inp_flowid = m->m_pkthdr.flowid; 94980cb9f21SKip Macy } 950a2589465SKen Smith #ifdef IPSEC 951a2589465SKen Smith #ifdef INET6 952a2589465SKen Smith if (isipv6 && ipsec6_in_reject(m, inp)) { 953603724d3SBjoern A. Zeeb V_ipsec6stat.in_polvio++; 954a2589465SKen Smith goto dropunlock; 955a2589465SKen Smith } else 956a2589465SKen Smith #endif /* INET6 */ 957a2589465SKen Smith if (ipsec4_in_reject(m, inp) != 0) { 958603724d3SBjoern A. Zeeb V_ipsec4stat.in_polvio++; 959a2589465SKen Smith goto dropunlock; 960a2589465SKen Smith } 961a2589465SKen Smith #endif /* IPSEC */ 962a2589465SKen Smith 9638d573cc1SAndre Oppermann /* 9648d573cc1SAndre Oppermann * Check the minimum TTL for socket. 9658d573cc1SAndre Oppermann */ 96634f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 96734f83c52SGeorge V. Neville-Neil #ifdef INET6 96834f83c52SGeorge V. Neville-Neil if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim) 969302ce8d6SAndre Oppermann goto dropunlock; 97002707462SAndre Oppermann else 97134f83c52SGeorge V. Neville-Neil #endif 97234f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 973302ce8d6SAndre Oppermann goto dropunlock; 97434f83c52SGeorge V. Neville-Neil } 975936cd18dSAndre Oppermann 976340c35deSJonathan Lemon /* 977252ca428SRobert Watson * A previous connection in TIMEWAIT state is supposed to catch stray 978252ca428SRobert Watson * or duplicate segments arriving late. If this segment was a 9790989f56cSRobert Watson * legitimate new connection attempt, the old INPCB gets removed and 980252ca428SRobert Watson * we can try again to find a listening socket. 981252ca428SRobert Watson * 982fa046d87SRobert Watson * At this point, due to earlier optimism, we may hold only an inpcb 983fa046d87SRobert Watson * lock, and not the inpcbinfo write lock. If so, we need to try to 984fa046d87SRobert Watson * acquire it, or if that fails, acquire a reference on the inpcb, 985fa046d87SRobert Watson * drop all locks, acquire a global write lock, and then re-acquire 986fa046d87SRobert Watson * the inpcb lock. We may at that point discover that another thread 987fa046d87SRobert Watson * has tried to free the inpcb, in which case we need to loop back 988fa046d87SRobert Watson * and try to find a new inpcb to deliver to. 989fa046d87SRobert Watson * 990fa046d87SRobert Watson * XXXRW: It may be time to rethink timewait locking. 991340c35deSJonathan Lemon */ 992883e9bc4SRobert Watson relocked: 993ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 994fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 995fa046d87SRobert Watson if (INP_INFO_TRY_WLOCK(&V_tcbinfo) == 0) { 996252ca428SRobert Watson in_pcbref(inp); 997252ca428SRobert Watson INP_WUNLOCK(inp); 998252ca428SRobert Watson INP_INFO_WLOCK(&V_tcbinfo); 999252ca428SRobert Watson ti_locked = TI_WLOCKED; 1000252ca428SRobert Watson INP_WLOCK(inp); 1001fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) { 1002252ca428SRobert Watson inp = NULL; 1003252ca428SRobert Watson goto findpcb; 1004252ca428SRobert Watson } 1005f681a5fdSRobert Watson } else 1006252ca428SRobert Watson ti_locked = TI_WLOCKED; 1007252ca428SRobert Watson } 1008252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1009252ca428SRobert Watson 1010340c35deSJonathan Lemon if (thflags & TH_SYN) 1011f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 10128d573cc1SAndre Oppermann /* 10138d573cc1SAndre Oppermann * NB: tcp_twcheck unlocks the INP and frees the mbuf. 10148d573cc1SAndre Oppermann */ 10152104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 1016340c35deSJonathan Lemon goto findpcb; 1017603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 1018340c35deSJonathan Lemon return; 1019340c35deSJonathan Lemon } 1020794235b7SAndre Oppermann /* 1021794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 1022794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 1023794235b7SAndre Oppermann * segment and send an appropriate response. 1024794235b7SAndre Oppermann */ 1025df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 1026a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 1027a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 1028a57815efSBosko Milekic goto dropwithreset; 102909f81a46SBosko Milekic } 1030df8bae1dSRodney W. Grimes 103109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 103209fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 103309fe6320SNavdeep Parhar tcp_offload_input(tp, m); 103409fe6320SNavdeep Parhar m = NULL; /* consumed by the TOE driver */ 103509fe6320SNavdeep Parhar goto dropunlock; 103609fe6320SNavdeep Parhar } 103709fe6320SNavdeep Parhar #endif 103809fe6320SNavdeep Parhar 1039252ca428SRobert Watson /* 1040252ca428SRobert Watson * We've identified a valid inpcb, but it could be that we need an 1041fa046d87SRobert Watson * inpcbinfo write lock but don't hold it. In this case, attempt to 1042fa046d87SRobert Watson * acquire using the same strategy as the TIMEWAIT case above. If we 1043fa046d87SRobert Watson * relock, we have to jump back to 'relocked' as the connection might 1044fa046d87SRobert Watson * now be in TIMEWAIT. 1045252ca428SRobert Watson */ 1046fa046d87SRobert Watson #ifdef INVARIANTS 1047fa046d87SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0) 1048fa046d87SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1049fa046d87SRobert Watson #endif 1050fa046d87SRobert Watson if (tp->t_state != TCPS_ESTABLISHED) { 1051fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 1052fa046d87SRobert Watson if (INP_INFO_TRY_WLOCK(&V_tcbinfo) == 0) { 1053252ca428SRobert Watson in_pcbref(inp); 1054252ca428SRobert Watson INP_WUNLOCK(inp); 1055252ca428SRobert Watson INP_INFO_WLOCK(&V_tcbinfo); 1056252ca428SRobert Watson ti_locked = TI_WLOCKED; 1057252ca428SRobert Watson INP_WLOCK(inp); 1058fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) { 1059252ca428SRobert Watson inp = NULL; 1060252ca428SRobert Watson goto findpcb; 1061252ca428SRobert Watson } 1062883e9bc4SRobert Watson goto relocked; 1063f681a5fdSRobert Watson } else 1064252ca428SRobert Watson ti_locked = TI_WLOCKED; 1065252ca428SRobert Watson } 1066252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1067252ca428SRobert Watson } 1068252ca428SRobert Watson 1069c488362eSRobert Watson #ifdef MAC 10708501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 107130d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 1072302ce8d6SAndre Oppermann goto dropunlock; 1073c488362eSRobert Watson #endif 1074a557af22SRobert Watson so = inp->inp_socket; 1075302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 1076610ee2f9SDavid Greenman #ifdef TCPDEBUG 1077df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 1078df8bae1dSRodney W. Grimes ostate = tp->t_state; 107910fe523eSMaxim Konovalov #ifdef INET6 10806f697424SBjoern A. Zeeb if (isipv6) { 1081540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 1082d30d90dcSMaxim Konovalov } else 10836f697424SBjoern A. Zeeb #endif 1084540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 1085fb59c426SYoshinobu Inoue tcp_savetcp = *th; 1086df8bae1dSRodney W. Grimes } 1087b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */ 1088db33b3e6SAndre Oppermann /* 1089db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 1090db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 1091fa046d87SRobert Watson * attempt or the completion of a previous one. Because listen 1092fa046d87SRobert Watson * sockets are never in TCPS_ESTABLISHED, the V_tcbinfo lock will be 1093fa046d87SRobert Watson * held in this case. 1094db33b3e6SAndre Oppermann */ 1095540e8b7eSJeffrey Hsu if (so->so_options & SO_ACCEPTCONN) { 1096540e8b7eSJeffrey Hsu struct in_conninfo inc; 1097540e8b7eSJeffrey Hsu 10987824d002SAndre Oppermann KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but " 10997824d002SAndre Oppermann "tp not listening", __func__)); 1100fa046d87SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 11017824d002SAndre Oppermann 11028bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 1103302ce8d6SAndre Oppermann #ifdef INET6 1104be2ac88cSJonathan Lemon if (isipv6) { 1105dcdb4371SBjoern A. Zeeb inc.inc_flags |= INC_ISIPV6; 1106be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 1107be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 1108302ce8d6SAndre Oppermann } else 1109302ce8d6SAndre Oppermann #endif 1110302ce8d6SAndre Oppermann { 1111be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 1112be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 1113be2ac88cSJonathan Lemon } 1114be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 1115be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 11167973fba3SJulian Elischer inc.inc_fibnum = so->so_fibnum; 1117be2ac88cSJonathan Lemon 1118fb59c426SYoshinobu Inoue /* 11198d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 11208d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 11218d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 1122fb59c426SYoshinobu Inoue */ 1123be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 1124bf6d304aSAndre Oppermann /* 1125bf6d304aSAndre Oppermann * Parse the TCP options here because 1126bf6d304aSAndre Oppermann * syncookies need access to the reflected 1127bf6d304aSAndre Oppermann * timestamp. 1128bf6d304aSAndre Oppermann */ 1129bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 11309fa198beSAndre Oppermann /* 11319fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 11329fa198beSAndre Oppermann * inp and tcpinfo locks. 11339fa198beSAndre Oppermann */ 1134bf6d304aSAndre Oppermann if (!syncache_expand(&inc, &to, th, &so, m)) { 11354195b4afSPaul Traina /* 1136e207f800SAndre Oppermann * No syncache entry or ACK was not 1137be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 11388d573cc1SAndre Oppermann * NB: syncache did its own logging 11398d573cc1SAndre Oppermann * of the failure cause. 11404195b4afSPaul Traina */ 1141be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 1142be2ac88cSJonathan Lemon goto dropwithreset; 1143be2ac88cSJonathan Lemon } 1144f76fcf6dSJeffrey Hsu if (so == NULL) { 1145be2ac88cSJonathan Lemon /* 1146e207f800SAndre Oppermann * We completed the 3-way handshake 1147e207f800SAndre Oppermann * but could not allocate a socket 1148e207f800SAndre Oppermann * either due to memory shortage, 1149e207f800SAndre Oppermann * listen queue length limits or 1150a160e630SAndre Oppermann * global socket limits. Send RST 1151a160e630SAndre Oppermann * or wait and have the remote end 1152a160e630SAndre Oppermann * retransmit the ACK for another 1153a160e630SAndre Oppermann * try. 1154be2ac88cSJonathan Lemon */ 1155a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1156a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11578d573cc1SAndre Oppermann "Socket allocation failed due to " 11588d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 1159ac957cd2SJulian Elischer s, __func__, 1160ac957cd2SJulian Elischer V_tcp_sc_rst_sock_fail ? 1161ac957cd2SJulian Elischer "sending RST" : "try again"); 1162603724d3SBjoern A. Zeeb if (V_tcp_sc_rst_sock_fail) { 1163e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 1164e207f800SAndre Oppermann goto dropwithreset; 1165a160e630SAndre Oppermann } else 1166a160e630SAndre Oppermann goto dropunlock; 1167f76fcf6dSJeffrey Hsu } 1168be2ac88cSJonathan Lemon /* 1169be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 11708d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 11718d573cc1SAndre Oppermann * created socket and update the tp variable. 1172be2ac88cSJonathan Lemon */ 11738501a69cSRobert Watson INP_WUNLOCK(inp); /* listen socket */ 1174be2ac88cSJonathan Lemon inp = sotoinpcb(so); 11758501a69cSRobert Watson INP_WLOCK(inp); /* new connection */ 1176be2ac88cSJonathan Lemon tp = intotcpcb(inp); 11778d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 11788d573cc1SAndre Oppermann ("%s: ", __func__)); 11792903309aSAttilio Rao #ifdef TCP_SIGNATURE 11802903309aSAttilio Rao if (sig_checked == 0) { 11812903309aSAttilio Rao tcp_dooptions(&to, optp, optlen, 11822903309aSAttilio Rao (thflags & TH_SYN) ? TO_SYN : 0); 11832903309aSAttilio Rao if (!tcp_signature_verify_input(m, off0, tlen, 11842903309aSAttilio Rao optlen, &to, th, tp->t_flags)) { 11852903309aSAttilio Rao 11862903309aSAttilio Rao /* 11872903309aSAttilio Rao * In SYN_SENT state if it receives an 11882903309aSAttilio Rao * RST, it is allowed for further 11892903309aSAttilio Rao * processing. 11902903309aSAttilio Rao */ 11912903309aSAttilio Rao if ((thflags & TH_RST) == 0 || 11922903309aSAttilio Rao (tp->t_state == TCPS_SYN_SENT) == 0) 11932903309aSAttilio Rao goto dropunlock; 11942903309aSAttilio Rao } 11952903309aSAttilio Rao sig_checked = 1; 11962903309aSAttilio Rao } 11972903309aSAttilio Rao #endif 11982903309aSAttilio Rao 1199be2ac88cSJonathan Lemon /* 1200302ce8d6SAndre Oppermann * Process the segment and the data it 1201302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 1202302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 1203302ce8d6SAndre Oppermann */ 1204f2512ba1SRui Paulo tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 1205252ca428SRobert Watson iptos, ti_locked); 1206603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1207302ce8d6SAndre Oppermann return; 1208be2ac88cSJonathan Lemon } 1209a160e630SAndre Oppermann /* 12108d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 12118d573cc1SAndre Oppermann * 1212a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 1213a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 1214a160e630SAndre Oppermann * retransmits. 121519bc77c5SAndre Oppermann * 121619bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 121719bc77c5SAndre Oppermann * causes. 1218a160e630SAndre Oppermann */ 1219a160e630SAndre Oppermann if (thflags & TH_RST) { 122019bc77c5SAndre Oppermann syncache_chkrst(&inc, th); 1221a160e630SAndre Oppermann goto dropunlock; 1222a160e630SAndre Oppermann } 1223a160e630SAndre Oppermann /* 1224a160e630SAndre Oppermann * We can't do anything without SYN. 1225a160e630SAndre Oppermann */ 1226a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 1227a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1228a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1229773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 12308d573cc1SAndre Oppermann s, __func__); 123178b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1232a160e630SAndre Oppermann goto dropunlock; 1233a160e630SAndre Oppermann } 1234a160e630SAndre Oppermann /* 1235a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 1236a160e630SAndre Oppermann */ 1237fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1238a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1239a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12408d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 12418d573cc1SAndre Oppermann s, __func__); 1242a160e630SAndre Oppermann syncache_badack(&inc); /* XXX: Not needed! */ 124378b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1244a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1245a57815efSBosko Milekic goto dropwithreset; 12464195b4afSPaul Traina } 1247a160e630SAndre Oppermann /* 1248a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 1249a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 1250a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 1251a160e630SAndre Oppermann * TCP/IP stack. 1252a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 1253a160e630SAndre Oppermann * and is constantly refining its stack detection 1254a160e630SAndre Oppermann * strategies. 12558d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 1256a160e630SAndre Oppermann * and was used by RFC1644. 1257a160e630SAndre Oppermann */ 1258603724d3SBjoern A. Zeeb if ((thflags & TH_FIN) && V_drop_synfin) { 1259a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1260a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1261773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 12628d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 126378b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1264302ce8d6SAndre Oppermann goto dropunlock; 1265ebb0cbeaSPaul Traina } 1266be2ac88cSJonathan Lemon /* 1267be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 1268a160e630SAndre Oppermann * 1269a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 1270a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 1271a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 1272be2ac88cSJonathan Lemon */ 1273a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 1274a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 1275a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 1276a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 127733841545SHajimu UMEMOTO #ifdef INET6 127833841545SHajimu UMEMOTO /* 127933841545SHajimu UMEMOTO * If deprecated address is forbidden, 128033841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 128133841545SHajimu UMEMOTO * address to prevent any new inbound connection from 128233841545SHajimu UMEMOTO * getting established. 128333841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 128433841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 128533841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 128633841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 128733841545SHajimu UMEMOTO * for the exchange. 128833841545SHajimu UMEMOTO * 128933841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 129033841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 129133841545SHajimu UMEMOTO * SYN in this case. 129233841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 129333841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 129433841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 129533841545SHajimu UMEMOTO * used" 129633841545SHajimu UMEMOTO * 2. use of it with new communication: 129733841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 129833841545SHajimu UMEMOTO * with sufficient scope is available" 129933841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 130033841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 130133841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 130233841545SHajimu UMEMOTO * 130333841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 130433841545SHajimu UMEMOTO * multiple description text for deprecated address 130533841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 130633841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 130733841545SHajimu UMEMOTO */ 1308603724d3SBjoern A. Zeeb if (isipv6 && !V_ip6_use_deprecated) { 130933841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 131033841545SHajimu UMEMOTO 13118c0fec80SRobert Watson ia6 = ip6_getdstifaddr(m); 13128c0fec80SRobert Watson if (ia6 != NULL && 131333841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 13148c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 1315a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1316a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13178d573cc1SAndre Oppermann "Connection attempt to deprecated " 13188d573cc1SAndre Oppermann "IPv6 address rejected\n", 1319a160e630SAndre Oppermann s, __func__); 132033841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 132133841545SHajimu UMEMOTO goto dropwithreset; 132233841545SHajimu UMEMOTO } 132377d396fdSMaksim Yevmenkin if (ia6) 13248c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 132533841545SHajimu UMEMOTO } 1326b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 132765f28919SJesper Skriver /* 1328db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 13298d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 1330db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 13318d573cc1SAndre Oppermann * If it is from this socket it must be forged. 13328d573cc1SAndre Oppermann * Don't respond if the source or destination is a 13338d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 133493ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 133593ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 133693ec91baSCrist J. Clark * in_broadcast() to find them. 1337be2ac88cSJonathan Lemon */ 13388d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 13398d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 13408d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13418d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 1342773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 1343302ce8d6SAndre Oppermann goto dropunlock; 13448d573cc1SAndre Oppermann } 1345db33b3e6SAndre Oppermann #ifdef INET6 1346b287c6c7SBjoern A. Zeeb if (isipv6) { 1347db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1348a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 1349a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1350a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13518d573cc1SAndre Oppermann "Connection attempt to/from self " 1352773673c1SAndre Oppermann "ignored\n", s, __func__); 1353302ce8d6SAndre Oppermann goto dropunlock; 1354a160e630SAndre Oppermann } 1355be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1356a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 1357a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1358a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13598d573cc1SAndre Oppermann "Connection attempt from/to multicast " 1360773673c1SAndre Oppermann "address ignored\n", s, __func__); 1361302ce8d6SAndre Oppermann goto dropunlock; 1362a160e630SAndre Oppermann } 1363b287c6c7SBjoern A. Zeeb } 1364db33b3e6SAndre Oppermann #endif 1365b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 1366b287c6c7SBjoern A. Zeeb else 1367b287c6c7SBjoern A. Zeeb #endif 1368b287c6c7SBjoern A. Zeeb #ifdef INET 1369b287c6c7SBjoern A. Zeeb { 1370db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1371a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 1372a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1373a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13748d573cc1SAndre Oppermann "Connection attempt from/to self " 1375773673c1SAndre Oppermann "ignored\n", s, __func__); 1376302ce8d6SAndre Oppermann goto dropunlock; 1377a160e630SAndre Oppermann } 1378be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1379be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 13802ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 1381a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 1382a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1383a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13848d573cc1SAndre Oppermann "Connection attempt from/to broad- " 1385773673c1SAndre Oppermann "or multicast address ignored\n", 1386a160e630SAndre Oppermann s, __func__); 1387302ce8d6SAndre Oppermann goto dropunlock; 1388c068736aSJeffrey Hsu } 1389a160e630SAndre Oppermann } 1390b287c6c7SBjoern A. Zeeb #endif 1391be2ac88cSJonathan Lemon /* 1392db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 1393db33b3e6SAndre Oppermann * for syncache. 1394be2ac88cSJonathan Lemon */ 13953c653157SHartmut Brandt #ifdef TCPDEBUG 13963c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 13973c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 13983c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 13993c653157SHartmut Brandt #endif 1400f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 140109fe6320SNavdeep Parhar syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL); 1402be2ac88cSJonathan Lemon /* 14034d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 1404a160e630SAndre Oppermann * Everything already unlocked by syncache_add(). 1405be2ac88cSJonathan Lemon */ 1406603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1407be2ac88cSJonathan Lemon return; 1408f76fcf6dSJeffrey Hsu } 1409db33b3e6SAndre Oppermann 14102903309aSAttilio Rao #ifdef TCP_SIGNATURE 14112903309aSAttilio Rao if (sig_checked == 0) { 14122903309aSAttilio Rao tcp_dooptions(&to, optp, optlen, 14132903309aSAttilio Rao (thflags & TH_SYN) ? TO_SYN : 0); 14142903309aSAttilio Rao if (!tcp_signature_verify_input(m, off0, tlen, optlen, &to, 14152903309aSAttilio Rao th, tp->t_flags)) { 14162903309aSAttilio Rao 14172903309aSAttilio Rao /* 14182903309aSAttilio Rao * In SYN_SENT state if it receives an RST, it is 14192903309aSAttilio Rao * allowed for further processing. 14202903309aSAttilio Rao */ 14212903309aSAttilio Rao if ((thflags & TH_RST) == 0 || 14222903309aSAttilio Rao (tp->t_state == TCPS_SYN_SENT) == 0) 14232903309aSAttilio Rao goto dropunlock; 14242903309aSAttilio Rao } 14252903309aSAttilio Rao sig_checked = 1; 14262903309aSAttilio Rao } 14272903309aSAttilio Rao #endif 14282903309aSAttilio Rao 1429302ce8d6SAndre Oppermann /* 14308d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 14318d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 14328d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 1433302ce8d6SAndre Oppermann */ 1434252ca428SRobert Watson tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked); 1435603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1436302ce8d6SAndre Oppermann return; 1437be2ac88cSJonathan Lemon 1438302ce8d6SAndre Oppermann dropwithreset: 1439fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) { 1440dd8ac7f9SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1441252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1442fa046d87SRobert Watson } 1443fa046d87SRobert Watson #ifdef INVARIANTS 1444fa046d87SRobert Watson else { 1445fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset " 1446fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1447fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1448fa046d87SRobert Watson } 1449fa046d87SRobert Watson #endif 1450014ea782SRobert Watson 1451014ea782SRobert Watson if (inp != NULL) { 1452302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 1453014ea782SRobert Watson INP_WUNLOCK(inp); 1454dd8ac7f9SRobert Watson } else 1455014ea782SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1456302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 1457014ea782SRobert Watson goto drop; 1458014ea782SRobert Watson 1459302ce8d6SAndre Oppermann dropunlock: 1460fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) { 1461252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1462252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1463fa046d87SRobert Watson } 1464fa046d87SRobert Watson #ifdef INVARIANTS 1465fa046d87SRobert Watson else { 1466fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock " 1467fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1468fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1469fa046d87SRobert Watson } 1470fa046d87SRobert Watson #endif 1471252ca428SRobert Watson 14729fa198beSAndre Oppermann if (inp != NULL) 14738501a69cSRobert Watson INP_WUNLOCK(inp); 1474014ea782SRobert Watson 1475302ce8d6SAndre Oppermann drop: 1476603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1477a160e630SAndre Oppermann if (s != NULL) 1478a160e630SAndre Oppermann free(s, M_TCPLOG); 1479302ce8d6SAndre Oppermann if (m != NULL) 1480302ce8d6SAndre Oppermann m_freem(m); 1481302ce8d6SAndre Oppermann } 1482302ce8d6SAndre Oppermann 1483679d9708SAndre Oppermann static void 1484302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 1485252ca428SRobert Watson struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos, 1486252ca428SRobert Watson int ti_locked) 1487302ce8d6SAndre Oppermann { 1488302ce8d6SAndre Oppermann int thflags, acked, ourfinisacked, needoutput = 0; 1489302ce8d6SAndre Oppermann int rstreason, todrop, win; 1490302ce8d6SAndre Oppermann u_long tiwin; 1491302ce8d6SAndre Oppermann struct tcpopt to; 1492302ce8d6SAndre Oppermann 149314739780SMaxim Konovalov #ifdef TCPDEBUG 149414739780SMaxim Konovalov /* 149514739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 149614739780SMaxim Konovalov * now IPv6. 149714739780SMaxim Konovalov */ 149814739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 149914739780SMaxim Konovalov struct tcphdr tcp_savetcp; 150014739780SMaxim Konovalov short ostate = 0; 150114739780SMaxim Konovalov #endif 1502302ce8d6SAndre Oppermann thflags = th->th_flags; 1503d64a46eaSLawrence Stewart tp->sackhint.last_sack_ack = 0; 1504302ce8d6SAndre Oppermann 1505252ca428SRobert Watson /* 1506252ca428SRobert Watson * If this is either a state-changing packet or current state isn't 1507252ca428SRobert Watson * established, we require a write lock on tcbinfo. Otherwise, we 15080989f56cSRobert Watson * allow the tcbinfo to be in either alocked or unlocked, as the 15090989f56cSRobert Watson * caller may have unnecessarily acquired a write lock due to a race. 1510252ca428SRobert Watson */ 1511252ca428SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 1512252ca428SRobert Watson tp->t_state != TCPS_ESTABLISHED) { 1513252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s ti_locked %d for " 1514252ca428SRobert Watson "SYN/FIN/RST/!EST", __func__, ti_locked)); 1515603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1516252ca428SRobert Watson } else { 1517252ca428SRobert Watson #ifdef INVARIANTS 1518fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 1519252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1520fa046d87SRobert Watson else { 1521fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST " 1522fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1523fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1524fa046d87SRobert Watson } 1525252ca428SRobert Watson #endif 1526252ca428SRobert Watson } 15278501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1528679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1529679d9708SAndre Oppermann __func__)); 1530679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1531679d9708SAndre Oppermann __func__)); 1532df8bae1dSRodney W. Grimes 1533df8bae1dSRodney W. Grimes /* 1534df8bae1dSRodney W. Grimes * Segment received on connection. 1535df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1536e8949f74SAndre Oppermann * XXX: This should be done after segment 1537e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 1538df8bae1dSRodney W. Grimes */ 15399b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 15407ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 15419077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 1542df8bae1dSRodney W. Grimes 1543df8bae1dSRodney W. Grimes /* 1544464fcfbcSAndre Oppermann * Unscale the window into a 32-bit value. 1545e8949f74SAndre Oppermann * For the SYN_SENT state the scale is zero. 1546464fcfbcSAndre Oppermann */ 1547464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 1548464fcfbcSAndre Oppermann 1549464fcfbcSAndre Oppermann /* 1550f2512ba1SRui Paulo * TCP ECN processing. 1551f2512ba1SRui Paulo */ 1552f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) { 15539c251892SRui Paulo if (thflags & TH_CWR) 15549c251892SRui Paulo tp->t_flags &= ~TF_ECN_SND_ECE; 1555f2512ba1SRui Paulo switch (iptos & IPTOS_ECN_MASK) { 1556f2512ba1SRui Paulo case IPTOS_ECN_CE: 1557f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_ECE; 155878b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ce); 1559f2512ba1SRui Paulo break; 1560f2512ba1SRui Paulo case IPTOS_ECN_ECT0: 156178b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect0); 1562f2512ba1SRui Paulo break; 1563f2512ba1SRui Paulo case IPTOS_ECN_ECT1: 156478b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect1); 1565f2512ba1SRui Paulo break; 1566f2512ba1SRui Paulo } 1567dbc42409SLawrence Stewart /* Congestion experienced. */ 1568dbc42409SLawrence Stewart if (thflags & TH_ECE) { 1569dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_ECN); 1570f2512ba1SRui Paulo } 1571f2512ba1SRui Paulo } 1572f2512ba1SRui Paulo 1573f2512ba1SRui Paulo /* 1574f72167f4SAndre Oppermann * Parse options on any incoming segment. 1575f72167f4SAndre Oppermann */ 1576302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1577302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1578302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1579f72167f4SAndre Oppermann 1580f72167f4SAndre Oppermann /* 1581f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1582bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1583bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1584bf6d304aSAndre Oppermann * was established. 1585f72167f4SAndre Oppermann */ 1586bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1587e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 1588d8951c8aSBjoern A. Zeeb if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) 1589f72167f4SAndre Oppermann to.to_tsecr = 0; 1590bf6d304aSAndre Oppermann } 1591f72167f4SAndre Oppermann 1592f72167f4SAndre Oppermann /* 159397d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 159497d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 15955396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 15965396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 159797d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1598df8bae1dSRodney W. Grimes */ 15990a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1600464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 1601464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 1602be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 160302a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 1604be2ac88cSJonathan Lemon } 16055396d0f8SAndre Oppermann /* 16065396d0f8SAndre Oppermann * Initial send window. It will be updated with 16075396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 16085396d0f8SAndre Oppermann */ 16095396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 1610be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1611be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1612be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1613d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1614be2ac88cSJonathan Lemon } 1615be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1616be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 16173529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 16183529149eSAndre Oppermann (to.to_flags & TOF_SACKPERM) == 0) 16193529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 16206d90faf3SPaul Saab } 16216d90faf3SPaul Saab 1622df8bae1dSRodney W. Grimes /* 1623df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1624df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1625df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1626df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1627df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1628df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1629df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1630df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1631df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1632df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1633df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1634df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1635a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1636c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 1637a0292f23SGarrett Wollman * be TH_NEEDSYN. 1638df8bae1dSRodney W. Grimes */ 1639df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1640c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 1641fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1642c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 1643c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 1644a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1645c5ad39b9SAndre Oppermann LIST_EMPTY(&tp->t_segq) && 1646be2ac88cSJonathan Lemon ((to.to_flags & TOF_TS) == 0 || 1647c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1648df8bae1dSRodney W. Grimes 1649df8bae1dSRodney W. Grimes /* 1650df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1651df8bae1dSRodney W. Grimes * record the timestamp. 1652a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1653a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1654df8bae1dSRodney W. Grimes */ 1655be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1656fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 1657d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 1658a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1659df8bae1dSRodney W. Grimes } 1660df8bae1dSRodney W. Grimes 1661fb59c426SYoshinobu Inoue if (tlen == 0) { 1662fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1663fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1664dbc42409SLawrence Stewart !IN_RECOVERY(tp->t_flags) && 1665fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1666dbc42409SLawrence Stewart TAILQ_EMPTY(&tp->snd_holes)) { 1667df8bae1dSRodney W. Grimes /* 1668e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1669df8bae1dSRodney W. Grimes */ 1670fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 1671252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1672252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1673252ca428SRobert Watson 167478b50714SRobert Watson TCPSTAT_INC(tcps_predack); 1675252ca428SRobert Watson 16769b8b58e0SJonathan Lemon /* 1677e8949f74SAndre Oppermann * "bad retransmit" recovery. 16789b8b58e0SJonathan Lemon */ 16799b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 1680672dc4aeSJohn Baldwin tp->t_flags & TF_PREVVALID && 16816dfb8b31SJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) { 1682dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 16839b8b58e0SJonathan Lemon } 1684fa55172bSMatthew Dillon 1685fa55172bSMatthew Dillon /* 1686fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1687fa55172bSMatthew Dillon * 1688fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1689fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1690fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1691fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1692fa55172bSMatthew Dillon */ 1693fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1694fa55172bSMatthew Dillon to.to_tsecr) { 1695d8951c8aSBjoern A. Zeeb u_int t; 1696d8951c8aSBjoern A. Zeeb 1697d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 1698d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 1699d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 1700a0292f23SGarrett Wollman tcp_xmit_timer(tp, 1701d8951c8aSBjoern A. Zeeb TCP_TS_TO_TICKS(t) + 1); 1702fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1703fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1704eaf80179SAndre Oppermann if (!tp->t_rttlow || 1705eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1706eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1707c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1708c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1709fa55172bSMatthew Dillon } 1710dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 171139bc9de5SLawrence Stewart 171239bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 171339bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 171439bc9de5SLawrence Stewart 171578b50714SRobert Watson TCPSTAT_INC(tcps_rcvackpack); 171678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 1717df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 17189d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 17199d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 17209d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 1721dbc42409SLawrence Stewart 1722dbc42409SLawrence Stewart /* 1723dbc42409SLawrence Stewart * Let the congestion control algorithm update 1724dbc42409SLawrence Stewart * congestion control related information. This 1725dbc42409SLawrence Stewart * typically means increasing the congestion 1726dbc42409SLawrence Stewart * window. 1727dbc42409SLawrence Stewart */ 1728dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_ACK); 1729dbc42409SLawrence Stewart 17309d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 17311645d090SJeff Roberson /* 1732e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 17331645d090SJeff Roberson * to th_ack. 17341645d090SJeff Roberson */ 1735cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1736b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1737df8bae1dSRodney W. Grimes m_freem(m); 1738e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made. */ 1739df8bae1dSRodney W. Grimes 1740df8bae1dSRodney W. Grimes /* 1741df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1742df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1743df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1744df8bae1dSRodney W. Grimes * If process is waiting for space, 1745df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1746df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1747df8bae1dSRodney W. Grimes * decide between more output or persist. 1748e8949f74SAndre Oppermann */ 17493c653157SHartmut Brandt #ifdef TCPDEBUG 17503c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 17513c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 17523c653157SHartmut Brandt (void *)tcp_saveipgen, 17533c653157SHartmut Brandt &tcp_savetcp, 0); 17543c653157SHartmut Brandt #endif 1755df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1756b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1757b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1758b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1759b8152ba7SAndre Oppermann tp->t_rxtcur); 1760df8bae1dSRodney W. Grimes sowwakeup(so); 1761df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 1762df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1763a14c749fSJonathan Lemon goto check_delack; 1764df8bae1dSRodney W. Grimes } 1765fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1766fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 17676741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 17686741ecf5SAndre Oppermann 1769df8bae1dSRodney W. Grimes /* 1770252ca428SRobert Watson * This is a pure, in-sequence data packet with 1771252ca428SRobert Watson * nothing on the reassembly queue and we have enough 1772252ca428SRobert Watson * buffer space to take it. 1773df8bae1dSRodney W. Grimes */ 1774fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 1775252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1776252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1777252ca428SRobert Watson 17786d90faf3SPaul Saab /* Clean receiver SACK report if present */ 17793529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 17806d90faf3SPaul Saab tcp_clean_sackreport(tp); 178178b50714SRobert Watson TCPSTAT_INC(tcps_preddat); 1782fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 17831645d090SJeff Roberson /* 17841645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 17851645d090SJeff Roberson * th_seq. 17861645d090SJeff Roberson */ 178760ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 17881645d090SJeff Roberson /* 17891645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 17901645d090SJeff Roberson * rcv_nxt. 17911645d090SJeff Roberson */ 17921645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 179378b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 179478b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 1795e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made */ 17963c653157SHartmut Brandt #ifdef TCPDEBUG 17973c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 17983c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 17993c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 18003c653157SHartmut Brandt #endif 18016741ecf5SAndre Oppermann /* 18026741ecf5SAndre Oppermann * Automatic sizing of receive socket buffer. Often the send 18036741ecf5SAndre Oppermann * buffer size is not optimally adjusted to the actual network 18046741ecf5SAndre Oppermann * conditions at hand (delay bandwidth product). Setting the 18056741ecf5SAndre Oppermann * buffer size too small limits throughput on links with high 18066741ecf5SAndre Oppermann * bandwidth and high delay (eg. trans-continental/oceanic links). 18076741ecf5SAndre Oppermann * 18086741ecf5SAndre Oppermann * On the receive side the socket buffer memory is only rarely 18096741ecf5SAndre Oppermann * used to any significant extent. This allows us to be much 18106741ecf5SAndre Oppermann * more aggressive in scaling the receive socket buffer. For 18116741ecf5SAndre Oppermann * the case that the buffer space is actually used to a large 18126741ecf5SAndre Oppermann * extent and we run out of kernel memory we can simply drop 18136741ecf5SAndre Oppermann * the new segments; TCP on the sender will just retransmit it 18146741ecf5SAndre Oppermann * later. Setting the buffer size too big may only consume too 18156741ecf5SAndre Oppermann * much kernel memory if the application doesn't read() from 18166741ecf5SAndre Oppermann * the socket or packet loss or reordering makes use of the 18176741ecf5SAndre Oppermann * reassembly queue. 18186741ecf5SAndre Oppermann * 18196741ecf5SAndre Oppermann * The criteria to step up the receive buffer one notch are: 18206741ecf5SAndre Oppermann * 1. the number of bytes received during the time it takes 18216741ecf5SAndre Oppermann * one timestamp to be reflected back to us (the RTT); 18226741ecf5SAndre Oppermann * 2. received bytes per RTT is within seven eighth of the 18236741ecf5SAndre Oppermann * current socket buffer size; 18246741ecf5SAndre Oppermann * 3. receive buffer size has not hit maximal automatic size; 18256741ecf5SAndre Oppermann * 18266741ecf5SAndre Oppermann * This algorithm does one step per RTT at most and only if 18276741ecf5SAndre Oppermann * we receive a bulk stream w/o packet losses or reorderings. 18286741ecf5SAndre Oppermann * Shrinking the buffer during idle times is not necessary as 18296741ecf5SAndre Oppermann * it doesn't consume any memory when idle. 18306741ecf5SAndre Oppermann * 18316741ecf5SAndre Oppermann * TODO: Only step up if the application is actually serving 18326741ecf5SAndre Oppermann * the buffer to better manage the socket buffer resources. 1833df8bae1dSRodney W. Grimes */ 1834603724d3SBjoern A. Zeeb if (V_tcp_do_autorcvbuf && 18356741ecf5SAndre Oppermann to.to_tsecr && 18366741ecf5SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 18378502ec25SAndre Oppermann if (TSTMP_GT(to.to_tsecr, tp->rfbuf_ts) && 18386741ecf5SAndre Oppermann to.to_tsecr - tp->rfbuf_ts < hz) { 18396741ecf5SAndre Oppermann if (tp->rfbuf_cnt > 18406741ecf5SAndre Oppermann (so->so_rcv.sb_hiwat / 8 * 7) && 18416741ecf5SAndre Oppermann so->so_rcv.sb_hiwat < 1842603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max) { 18436741ecf5SAndre Oppermann newsize = 18446741ecf5SAndre Oppermann min(so->so_rcv.sb_hiwat + 1845603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_inc, 1846603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max); 18476741ecf5SAndre Oppermann } 18486741ecf5SAndre Oppermann /* Start over with next RTT. */ 18496741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 18506741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 18516741ecf5SAndre Oppermann } else 18526741ecf5SAndre Oppermann tp->rfbuf_cnt += tlen; /* add up */ 18536741ecf5SAndre Oppermann } 18546741ecf5SAndre Oppermann 18556741ecf5SAndre Oppermann /* Add data to socket buffer. */ 18561e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1857c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1858c1c36a2cSMike Silbersack m_freem(m); 1859c1c36a2cSMike Silbersack } else { 18606741ecf5SAndre Oppermann /* 18616741ecf5SAndre Oppermann * Set new socket buffer size. 18626741ecf5SAndre Oppermann * Give up when limit is reached. 18636741ecf5SAndre Oppermann */ 18646741ecf5SAndre Oppermann if (newsize) 18656741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 18666c8286e4SRobert Watson newsize, so, NULL)) 18676741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1868fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 18691e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 1870c1c36a2cSMike Silbersack } 1871e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 18721e4d7da7SRobert Watson sorwakeup_locked(so); 1873d8c85a26SJonathan Lemon if (DELAY_ACK(tp)) { 18743bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1875f498eeeeSDavid Greenman } else { 1876e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 1877e612a582SDavid Greenman tcp_output(tp); 1878e612a582SDavid Greenman } 1879a14c749fSJonathan Lemon goto check_delack; 1880df8bae1dSRodney W. Grimes } 1881df8bae1dSRodney W. Grimes } 1882df8bae1dSRodney W. Grimes 1883df8bae1dSRodney W. Grimes /* 1884df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1885df8bae1dSRodney W. Grimes * and then do TCP input processing. 1886df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1887df8bae1dSRodney W. Grimes * but not less than advertised window. 1888df8bae1dSRodney W. Grimes */ 1889df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1890df8bae1dSRodney W. Grimes if (win < 0) 1891df8bae1dSRodney W. Grimes win = 0; 189266e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1893df8bae1dSRodney W. Grimes 18946741ecf5SAndre Oppermann /* Reset receive buffer auto scaling when not in bulk receive mode. */ 18956741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 18966741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 18976741ecf5SAndre Oppermann 1898df8bae1dSRodney W. Grimes switch (tp->t_state) { 1899df8bae1dSRodney W. Grimes 1900df8bae1dSRodney W. Grimes /* 1901764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1902764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1903764d8cefSBill Fenner */ 1904764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1905fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1906fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1907a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1908a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1909a57815efSBosko Milekic goto dropwithreset; 1910a57815efSBosko Milekic } 1911764d8cefSBill Fenner break; 1912764d8cefSBill Fenner 1913764d8cefSBill Fenner /* 1914df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1915df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1916df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1917df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1918df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1919df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1920df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1921f2512ba1SRui Paulo * if seg contains an ECE and ECN support is enabled, the stream 1922f2512ba1SRui Paulo * is ECN capable. 1923df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1924df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1925df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1926df8bae1dSRodney W. Grimes */ 1927df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1928fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1929fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1930fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1931a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1932a0292f23SGarrett Wollman goto dropwithreset; 1933a0292f23SGarrett Wollman } 19340ca3f933SAndre Oppermann if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) 1935df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 19360ca3f933SAndre Oppermann if (thflags & TH_RST) 1937df8bae1dSRodney W. Grimes goto drop; 19380ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 1939df8bae1dSRodney W. Grimes goto drop; 1940464fcfbcSAndre Oppermann 1941fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1942df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1943fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 194478b50714SRobert Watson TCPSTAT_INC(tcps_connects); 1945845799c1SAndras Olah soisconnected(so); 1946c488362eSRobert Watson #ifdef MAC 194730d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 1948c488362eSRobert Watson #endif 1949845799c1SAndras Olah /* Do window scaling on this connection? */ 1950845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1951845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1952845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1953845799c1SAndras Olah } 1954766282cbSJohn Baldwin tp->rcv_adv += imin(tp->rcv_wnd, 1955766282cbSJohn Baldwin TCP_MAXWIN << tp->rcv_scale); 1956a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1957a0292f23SGarrett Wollman /* 1958a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1959a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1960a0292f23SGarrett Wollman */ 1961d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && tlen != 0) 1962b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 1963b8152ba7SAndre Oppermann tcp_delacktime); 1964a0292f23SGarrett Wollman else 1965a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1966f2512ba1SRui Paulo 1967603724d3SBjoern A. Zeeb if ((thflags & TH_ECE) && V_tcp_do_ecn) { 1968f2512ba1SRui Paulo tp->t_flags |= TF_ECN_PERMIT; 196978b50714SRobert Watson TCPSTAT_INC(tcps_ecn_shs); 1970f2512ba1SRui Paulo } 1971f2512ba1SRui Paulo 1972a0292f23SGarrett Wollman /* 1973a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1974a0292f23SGarrett Wollman * Transitions: 1975a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1976a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1977a0292f23SGarrett Wollman */ 19789b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1979a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1980a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1981a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1982fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 19837ff19458SPaul Traina } else { 1984a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1985dbc42409SLawrence Stewart cc_conn_init(tp); 19869077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 19879077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 19887ff19458SPaul Traina } 1989a0292f23SGarrett Wollman } else { 1990a0292f23SGarrett Wollman /* 1991c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 1992c068736aSJeffrey Hsu * simultaneous open. If segment contains CC option 1993c068736aSJeffrey Hsu * and there is a cached CC, apply TAO test. 1994c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 1995c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 1996a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1997a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1998a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1999a0292f23SGarrett Wollman */ 20004b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 2001b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2002df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 2003a0292f23SGarrett Wollman } 2004df8bae1dSRodney W. Grimes 2005252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: trimthenstep6: " 2006252ca428SRobert Watson "ti_locked %d", __func__, ti_locked)); 2007252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 20088501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 20097cfc6904SRobert Watson 2010df8bae1dSRodney W. Grimes /* 2011fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 2012df8bae1dSRodney W. Grimes * If data, trim to stay within window, 2013df8bae1dSRodney W. Grimes * dropping FIN if necessary. 2014df8bae1dSRodney W. Grimes */ 2015fb59c426SYoshinobu Inoue th->th_seq++; 2016fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 2017fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 2018df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2019fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 2020fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 202178b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 202278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2023df8bae1dSRodney W. Grimes } 2024fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 2025fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 2026a0292f23SGarrett Wollman /* 2027a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 2028a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 2029a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 2030a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 2031a0292f23SGarrett Wollman * Otherwise, goto step 6. 2032a0292f23SGarrett Wollman */ 2033fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 2034a0292f23SGarrett Wollman goto process_ACK; 2035c068736aSJeffrey Hsu 2036df8bae1dSRodney W. Grimes goto step6; 2037c068736aSJeffrey Hsu 2038a0292f23SGarrett Wollman /* 2039a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 2040c94c54e4SAndre Oppermann * do normal processing. 2041a0292f23SGarrett Wollman * 2042c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 2043a0292f23SGarrett Wollman */ 2044a0292f23SGarrett Wollman case TCPS_LAST_ACK: 2045a0292f23SGarrett Wollman case TCPS_CLOSING: 2046a0292f23SGarrett Wollman break; /* continue normal processing */ 2047df8bae1dSRodney W. Grimes } 2048df8bae1dSRodney W. Grimes 2049df8bae1dSRodney W. Grimes /* 2050df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 205180ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 205280ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 205380ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 205480ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 205580ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 205680ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 205780ab7c0eSGarrett Wollman * killing a connection). 205880ab7c0eSGarrett Wollman * Then check timestamp, if present. 2059a0292f23SGarrett Wollman * Then check the connection count, if present. 2060df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 2061df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 2062df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 2063df8bae1dSRodney W. Grimes * 206480ab7c0eSGarrett Wollman * 206580ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 206680ab7c0eSGarrett Wollman * if this is a valid reset segment. 206780ab7c0eSGarrett Wollman * RFC 793 page 37: 206880ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 206980ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 207080ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 207180ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 207280ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 20731a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 20741a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 20751a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 20761a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 207780dd2a81SMike Silbersack * Note 2: Paul Watson's paper "Slipping in the Window" has shown 207880dd2a81SMike Silbersack * that brute force RST attacks are possible. To combat this, 207980dd2a81SMike Silbersack * we use a much stricter check while in the ESTABLISHED state, 208080dd2a81SMike Silbersack * only accepting RSTs where the sequence number is equal to 208180dd2a81SMike Silbersack * last_ack_sent. In all other states (the states in which a 208280dd2a81SMike Silbersack * RST is more likely), the more permissive check is used. 20838e5c87f4SBjoern A. Zeeb * If we have multiple segments in flight, the initial reset 208480ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 208580ab7c0eSGarrett Wollman * but they will eventually catch up. 208680ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 208780ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 208880ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 208980ab7c0eSGarrett Wollman * 209080ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 209180ab7c0eSGarrett Wollman * 209280ab7c0eSGarrett Wollman * DISCUSSION 209380ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 209480ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 209580ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 209680ab7c0eSGarrett Wollman * data. 209780ab7c0eSGarrett Wollman * 209880ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 209980ab7c0eSGarrett Wollman * the state: 210080ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 210180ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 210280ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 2103745bab7fSDima Dorfman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 210480ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 2105e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 210680ab7c0eSGarrett Wollman * Close the tcb. 2107e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 210880ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 210980ab7c0eSGarrett Wollman * RFC 1337. 211080ab7c0eSGarrett Wollman */ 2111fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 211295ad8418SQing Li if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 211395ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 211480ab7c0eSGarrett Wollman switch (tp->t_state) { 211580ab7c0eSGarrett Wollman 211680ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 211780ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 211880ab7c0eSGarrett Wollman goto close; 211980ab7c0eSGarrett Wollman 212080ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 2121603724d3SBjoern A. Zeeb if (V_tcp_insecure_rst == 0 && 212295ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) && 212395ad8418SQing Li SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) && 212495ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 212595ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) { 212678b50714SRobert Watson TCPSTAT_INC(tcps_badrst); 212780dd2a81SMike Silbersack goto drop; 212880dd2a81SMike Silbersack } 2129564aab1fSAndre Oppermann /* FALLTHROUGH */ 213080ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 213180ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 213280ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 213380ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 213480ab7c0eSGarrett Wollman close: 2135252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 2136252ca428SRobert Watson ("tcp_do_segment: TH_RST 1 ti_locked %d", 2137252ca428SRobert Watson ti_locked)); 2138252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2139252ca428SRobert Watson 214080ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 214178b50714SRobert Watson TCPSTAT_INC(tcps_drops); 214280ab7c0eSGarrett Wollman tp = tcp_close(tp); 214380ab7c0eSGarrett Wollman break; 214480ab7c0eSGarrett Wollman 214580ab7c0eSGarrett Wollman case TCPS_CLOSING: 214680ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 2147252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 2148252ca428SRobert Watson ("tcp_do_segment: TH_RST 2 ti_locked %d", 2149252ca428SRobert Watson ti_locked)); 2150252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2151252ca428SRobert Watson 215280ab7c0eSGarrett Wollman tp = tcp_close(tp); 215380ab7c0eSGarrett Wollman break; 215480ab7c0eSGarrett Wollman } 215580ab7c0eSGarrett Wollman } 215680ab7c0eSGarrett Wollman goto drop; 215780ab7c0eSGarrett Wollman } 215880ab7c0eSGarrett Wollman 215980ab7c0eSGarrett Wollman /* 2160df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 2161df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 2162df8bae1dSRodney W. Grimes */ 2163be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 216480ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 2165df8bae1dSRodney W. Grimes 2166df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 2167d8951c8aSBjoern A. Zeeb if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) { 2168df8bae1dSRodney W. Grimes /* 2169df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 2170df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 2171df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 2172df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 2173df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 2174df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 2175df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 2176df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 2177df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 2178df8bae1dSRodney W. Grimes */ 2179df8bae1dSRodney W. Grimes tp->ts_recent = 0; 2180df8bae1dSRodney W. Grimes } else { 218178b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 218278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 218378b50714SRobert Watson TCPSTAT_INC(tcps_pawsdrop); 218407fd333dSMatthew Dillon if (tlen) 2185df8bae1dSRodney W. Grimes goto dropafterack; 21861ab4789dSMatthew Dillon goto drop; 21871ab4789dSMatthew Dillon } 2188df8bae1dSRodney W. Grimes } 2189df8bae1dSRodney W. Grimes 2190a0292f23SGarrett Wollman /* 219180ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 219280ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 219380ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 219480ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 219580ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 219680ab7c0eSGarrett Wollman */ 2197a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 2198a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2199a57815efSBosko Milekic goto dropwithreset; 2200a57815efSBosko Milekic } 220180ab7c0eSGarrett Wollman 2202fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 2203df8bae1dSRodney W. Grimes if (todrop > 0) { 220481ad7eb0SZachary Loafman /* 220581ad7eb0SZachary Loafman * If this is a duplicate SYN for our current connection, 220681ad7eb0SZachary Loafman * advance over it and pretend and it's not a SYN. 220781ad7eb0SZachary Loafman */ 220881ad7eb0SZachary Loafman if (thflags & TH_SYN && th->th_seq == tp->irs) { 2209fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 2210fb59c426SYoshinobu Inoue th->th_seq++; 2211fb59c426SYoshinobu Inoue if (th->th_urp > 1) 2212fb59c426SYoshinobu Inoue th->th_urp--; 2213df8bae1dSRodney W. Grimes else 2214fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2215df8bae1dSRodney W. Grimes todrop--; 2216df8bae1dSRodney W. Grimes } 2217df8bae1dSRodney W. Grimes /* 2218dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 2219df8bae1dSRodney W. Grimes */ 2220fb59c426SYoshinobu Inoue if (todrop > tlen 2221fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 2222dac20301SGarrett Wollman /* 2223dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 2224dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 2225dac20301SGarrett Wollman * of sequence; drop it. 2226dac20301SGarrett Wollman */ 2227fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2228dac20301SGarrett Wollman 2229df8bae1dSRodney W. Grimes /* 2230dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 2231dac20301SGarrett Wollman * But keep on processing for RST or ACK. 2232df8bae1dSRodney W. Grimes */ 2233dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2234fb59c426SYoshinobu Inoue todrop = tlen; 223578b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 223678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 2237df8bae1dSRodney W. Grimes } else { 223878b50714SRobert Watson TCPSTAT_INC(tcps_rcvpartduppack); 223978b50714SRobert Watson TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 2240df8bae1dSRodney W. Grimes } 2241fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 2242fb59c426SYoshinobu Inoue th->th_seq += todrop; 2243fb59c426SYoshinobu Inoue tlen -= todrop; 2244fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 2245fb59c426SYoshinobu Inoue th->th_urp -= todrop; 2246df8bae1dSRodney W. Grimes else { 2247fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2248fb59c426SYoshinobu Inoue th->th_urp = 0; 2249df8bae1dSRodney W. Grimes } 2250df8bae1dSRodney W. Grimes } 2251df8bae1dSRodney W. Grimes 2252df8bae1dSRodney W. Grimes /* 2253df8bae1dSRodney W. Grimes * If new data are received on a connection after the 2254df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 2255df8bae1dSRodney W. Grimes */ 2256df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 2257fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 2258773673c1SAndre Oppermann char *s; 2259773673c1SAndre Oppermann 2260252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: SS_NOFDEREF && " 2261252ca428SRobert Watson "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked)); 2262252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2263252ca428SRobert Watson 2264773673c1SAndre Oppermann if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) { 2265e31d8aa3SMike Silbersack log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data after socket " 2266773673c1SAndre Oppermann "was closed, sending RST and removing tcpcb\n", 2267e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 2268773673c1SAndre Oppermann free(s, M_TCPLOG); 2269773673c1SAndre Oppermann } 2270df8bae1dSRodney W. Grimes tp = tcp_close(tp); 227178b50714SRobert Watson TCPSTAT_INC(tcps_rcvafterclose); 2272a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2273df8bae1dSRodney W. Grimes goto dropwithreset; 2274df8bae1dSRodney W. Grimes } 2275df8bae1dSRodney W. Grimes 2276df8bae1dSRodney W. Grimes /* 2277df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 2278df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 2279df8bae1dSRodney W. Grimes */ 2280fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 2281df8bae1dSRodney W. Grimes if (todrop > 0) { 228278b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 2283fb59c426SYoshinobu Inoue if (todrop >= tlen) { 228478b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 2285df8bae1dSRodney W. Grimes /* 2286df8bae1dSRodney W. Grimes * If window is closed can only take segments at 2287df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 2288df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 2289df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 2290df8bae1dSRodney W. Grimes * and ack. 2291df8bae1dSRodney W. Grimes */ 2292fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2293df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 229478b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinprobe); 2295df8bae1dSRodney W. Grimes } else 2296df8bae1dSRodney W. Grimes goto dropafterack; 2297df8bae1dSRodney W. Grimes } else 229878b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2299df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2300fb59c426SYoshinobu Inoue tlen -= todrop; 2301fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 2302df8bae1dSRodney W. Grimes } 2303df8bae1dSRodney W. Grimes 2304df8bae1dSRodney W. Grimes /* 2305df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 2306df8bae1dSRodney W. Grimes * record its timestamp. 2307cf09195bSPaul Saab * NOTE: 2308cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 2309a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2310cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 2311cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 2312cf09195bSPaul Saab * predicated on the sequence space of this segment. 2313cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 2314cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 2315cf09195bSPaul Saab * instead of RFC1323's 2316cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 2317cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 2318cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 2319cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 2320cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 2321df8bae1dSRodney W. Grimes */ 2322be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 2323cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2324cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 2325cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 2326d8951c8aSBjoern A. Zeeb tp->ts_recent_age = tcp_ts_getticks(); 2327a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 2328df8bae1dSRodney W. Grimes } 2329df8bae1dSRodney W. Grimes 2330df8bae1dSRodney W. Grimes /* 2331df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 2332df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 2333df8bae1dSRodney W. Grimes */ 2334fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 2335252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 2336252ca428SRobert Watson ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked)); 2337252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2338252ca428SRobert Watson 2339df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 2340a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2341122aad88SAndre Oppermann goto drop; 2342df8bae1dSRodney W. Grimes } 2343df8bae1dSRodney W. Grimes 2344a0292f23SGarrett Wollman /* 2345a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 2346a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 2347a0292f23SGarrett Wollman * later processing; else drop segment and return. 2348a0292f23SGarrett Wollman */ 2349fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 2350a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 2351a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 2352a0292f23SGarrett Wollman goto step6; 23535e1aa279SDavid Malone else if (tp->t_flags & TF_ACKNOW) 23545e1aa279SDavid Malone goto dropafterack; 2355a0292f23SGarrett Wollman else 2356a0292f23SGarrett Wollman goto drop; 2357a0292f23SGarrett Wollman } 2358df8bae1dSRodney W. Grimes 2359df8bae1dSRodney W. Grimes /* 2360df8bae1dSRodney W. Grimes * Ack processing. 2361df8bae1dSRodney W. Grimes */ 2362df8bae1dSRodney W. Grimes switch (tp->t_state) { 2363df8bae1dSRodney W. Grimes 2364df8bae1dSRodney W. Grimes /* 2365764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 2366764d8cefSBill Fenner * ESTABLISHED state and continue processing. 2367764d8cefSBill Fenner * The ACK was checked above. 2368df8bae1dSRodney W. Grimes */ 2369df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2370a0292f23SGarrett Wollman 237178b50714SRobert Watson TCPSTAT_INC(tcps_connects); 2372df8bae1dSRodney W. Grimes soisconnected(so); 2373df8bae1dSRodney W. Grimes /* Do window scaling? */ 2374df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2375df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2376df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 2377464fcfbcSAndre Oppermann tp->snd_wnd = tiwin; 2378df8bae1dSRodney W. Grimes } 2379a0292f23SGarrett Wollman /* 2380a0292f23SGarrett Wollman * Make transitions: 2381a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 2382a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 2383a0292f23SGarrett Wollman */ 23849b8b58e0SJonathan Lemon tp->t_starttime = ticks; 2385a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 2386a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 2387a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 23887ff19458SPaul Traina } else { 2389a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 2390dbc42409SLawrence Stewart cc_conn_init(tp); 23919077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp)); 23927ff19458SPaul Traina } 2393a0292f23SGarrett Wollman /* 2394a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 2395a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 2396a0292f23SGarrett Wollman */ 2397fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 2398fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 2399a0292f23SGarrett Wollman (struct mbuf *)0); 240060ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq - 1; 240193b0017fSPhilippe Charnier /* FALLTHROUGH */ 2402df8bae1dSRodney W. Grimes 2403df8bae1dSRodney W. Grimes /* 2404df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2405df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 2406fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 2407fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 2408df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 2409df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 2410df8bae1dSRodney W. Grimes */ 2411df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2412df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2413df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2414df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 2415df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2416df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 24175a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 241878b50714SRobert Watson TCPSTAT_INC(tcps_rcvacktoomuch); 24195a53ca16SPaul Saab goto dropafterack; 24205a53ca16SPaul Saab } 24213529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2422fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 2423fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 24245a53ca16SPaul Saab tcp_sack_doack(tp, &to, th->th_ack); 242539bc9de5SLawrence Stewart 242639bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 242739bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 242839bc9de5SLawrence Stewart 2429fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 2430fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 243178b50714SRobert Watson TCPSTAT_INC(tcps_rcvdupack); 2432df8bae1dSRodney W. Grimes /* 2433df8bae1dSRodney W. Grimes * If we have outstanding data (other than 2434df8bae1dSRodney W. Grimes * a window probe), this is a completely 2435df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 2436df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 2437df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 2438df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 2439df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 2440df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 2441df8bae1dSRodney W. Grimes * window so we send only this one 2442df8bae1dSRodney W. Grimes * packet. 2443df8bae1dSRodney W. Grimes * 2444df8bae1dSRodney W. Grimes * We know we're losing at the current 2445df8bae1dSRodney W. Grimes * window size so do congestion avoidance 2446df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 2447df8bae1dSRodney W. Grimes * and pull our congestion window back to 2448df8bae1dSRodney W. Grimes * the new ssthresh). 2449df8bae1dSRodney W. Grimes * 2450df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 2451df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 2452df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 2453df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 2454df8bae1dSRodney W. Grimes * network. 2455f2512ba1SRui Paulo * 2456f2512ba1SRui Paulo * When using TCP ECN, notify the peer that 2457f2512ba1SRui Paulo * we reduced the cwnd. 2458df8bae1dSRodney W. Grimes */ 2459b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_REXMT) || 2460fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 2461df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2462cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 2463dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2464dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_DUPACK); 24653529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2466dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2467808f11b7SPaul Saab int awnd; 246825e6f9edSPaul Saab 246925e6f9edSPaul Saab /* 247025e6f9edSPaul Saab * Compute the amount of data in flight first. 247125e6f9edSPaul Saab * We can inject new data into the pipe iff 247225e6f9edSPaul Saab * we have less than 1/2 the original window's 247325e6f9edSPaul Saab * worth of data in flight. 247425e6f9edSPaul Saab */ 2475808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 24760077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 2477808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 247825e6f9edSPaul Saab tp->snd_cwnd += tp->t_maxseg; 247925e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 248025e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 248125e6f9edSPaul Saab } 248225e6f9edSPaul Saab } else 248346f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 2484ec03d543SRandall Stewart if ((thflags & TH_FIN) && 2485ec03d543SRandall Stewart (TCPS_HAVERCVDFIN(tp->t_state) == 0)) { 2486ec03d543SRandall Stewart /* 2487ec03d543SRandall Stewart * If its a fin we need to process 2488ec03d543SRandall Stewart * it to avoid a race where both 2489ec03d543SRandall Stewart * sides enter FIN-WAIT and send FIN|ACK 2490ec03d543SRandall Stewart * at the same time. 2491ec03d543SRandall Stewart */ 2492ec03d543SRandall Stewart break; 2493ec03d543SRandall Stewart } 249446f58482SJonathan Lemon (void) tcp_output(tp); 249546f58482SJonathan Lemon goto drop; 2496cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 2497cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 2498a0445c2eSJayanth Vijayaraghavan 2499a0445c2eSJayanth Vijayaraghavan /* 2500a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 2501a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 2502a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 2503a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 2504a0445c2eSJayanth Vijayaraghavan * recovery. 2505a0445c2eSJayanth Vijayaraghavan */ 25063529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 2507dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2508a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 2509a0445c2eSJayanth Vijayaraghavan break; 2510a0445c2eSJayanth Vijayaraghavan } 2511dbc42409SLawrence Stewart } else { 2512a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 25139d11646dSJeffrey Hsu tp->snd_recover)) { 2514cb942153SJeffrey Hsu tp->t_dupacks = 0; 2515cb942153SJeffrey Hsu break; 251646f58482SJonathan Lemon } 2517a0445c2eSJayanth Vijayaraghavan } 2518dbc42409SLawrence Stewart /* Congestion signal before ack. */ 2519dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_NDUPACK); 2520dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_DUPACK); 2521b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 25229b8b58e0SJonathan Lemon tp->t_rtttime = 0; 25233529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 252478b50714SRobert Watson TCPSTAT_INC( 252578b50714SRobert Watson tcps_sack_recovery_episode); 2526a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 25278db456bfSPaul Saab tp->snd_cwnd = tp->t_maxseg; 25286d90faf3SPaul Saab (void) tcp_output(tp); 25296d90faf3SPaul Saab goto drop; 25306d90faf3SPaul Saab } 2531fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 2532df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 2533ec03d543SRandall Stewart if ((thflags & TH_FIN) && 2534ec03d543SRandall Stewart (TCPS_HAVERCVDFIN(tp->t_state) == 0)) { 2535ec03d543SRandall Stewart /* 2536ec03d543SRandall Stewart * If its a fin we need to process 2537ec03d543SRandall Stewart * it to avoid a race where both 2538ec03d543SRandall Stewart * sides enter FIN-WAIT and send FIN|ACK 2539ec03d543SRandall Stewart * at the same time. 2540ec03d543SRandall Stewart */ 2541ec03d543SRandall Stewart break; 2542ec03d543SRandall Stewart } 2543df8bae1dSRodney W. Grimes (void) tcp_output(tp); 254448d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 2545302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 2546302ce8d6SAndre Oppermann __func__)); 2547df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 254848d2549cSJeffrey Hsu tp->t_maxseg * 254948d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 2550df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 2551df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 2552df8bae1dSRodney W. Grimes goto drop; 2553603724d3SBjoern A. Zeeb } else if (V_tcp_do_rfc3042) { 2554dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_DUPACK); 2555582a954bSJeffrey Hsu u_long oldcwnd = tp->snd_cwnd; 255648d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 255748d2549cSJeffrey Hsu u_int sent; 255889c02376SJeffrey Hsu 2559582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 2560582a954bSJeffrey Hsu tp->t_dupacks == 2, 2561302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 2562302ce8d6SAndre Oppermann __func__)); 256361a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 256448d2549cSJeffrey Hsu tp->snd_limited = 0; 256561a36e3dSJeffrey Hsu tp->snd_cwnd = 256661a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 256761a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 256861a36e3dSJeffrey Hsu tp->t_maxseg; 2569ec03d543SRandall Stewart if ((thflags & TH_FIN) && 2570ec03d543SRandall Stewart (TCPS_HAVERCVDFIN(tp->t_state) == 0)) { 2571ec03d543SRandall Stewart /* 2572ec03d543SRandall Stewart * If its a fin we need to process 2573ec03d543SRandall Stewart * it to avoid a race where both 2574ec03d543SRandall Stewart * sides enter FIN-WAIT and send FIN|ACK 2575ec03d543SRandall Stewart * at the same time. 2576ec03d543SRandall Stewart */ 2577ec03d543SRandall Stewart break; 2578ec03d543SRandall Stewart } 2579582a954bSJeffrey Hsu (void) tcp_output(tp); 258048d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 258148d2549cSJeffrey Hsu if (sent > tp->t_maxseg) { 258289c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 258389c02376SJeffrey Hsu tp->snd_limited == 0) || 258489c02376SJeffrey Hsu (sent == tp->t_maxseg + 1 && 258589c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 2586302ce8d6SAndre Oppermann ("%s: sent too much", 2587302ce8d6SAndre Oppermann __func__)); 258848d2549cSJeffrey Hsu tp->snd_limited = 2; 258948d2549cSJeffrey Hsu } else if (sent > 0) 259048d2549cSJeffrey Hsu ++tp->snd_limited; 2591582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 2592582a954bSJeffrey Hsu goto drop; 2593df8bae1dSRodney W. Grimes } 2594df8bae1dSRodney W. Grimes } else 2595df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2596df8bae1dSRodney W. Grimes break; 2597df8bae1dSRodney W. Grimes } 2598cb942153SJeffrey Hsu 2599302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2600302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 2601cb942153SJeffrey Hsu 2602df8bae1dSRodney W. Grimes /* 2603df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2604df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2605df8bae1dSRodney W. Grimes */ 2606dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2607cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 26083529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 26096d90faf3SPaul Saab tcp_sack_partialack(tp, th); 26106d90faf3SPaul Saab else 2611c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2612dbc42409SLawrence Stewart } else 2613dbc42409SLawrence Stewart cc_post_recovery(tp, th); 261446f58482SJonathan Lemon } 2615cb942153SJeffrey Hsu tp->t_dupacks = 0; 2616a0292f23SGarrett Wollman /* 2617a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2618a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2619a0292f23SGarrett Wollman */ 2620a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2621a0292f23SGarrett Wollman /* 2622a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2623a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 262407e43e10SAndras Olah * synchronized). Go to non-starred state, 262507e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 262607e43e10SAndras Olah * we can do window scaling. 2627a0292f23SGarrett Wollman */ 2628a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2629a0292f23SGarrett Wollman tp->snd_una++; 263007e43e10SAndras Olah /* Do window scaling? */ 263107e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 263207e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 263307e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2634464fcfbcSAndre Oppermann /* Send window already scaled. */ 263507e43e10SAndras Olah } 2636a0292f23SGarrett Wollman } 2637a0292f23SGarrett Wollman 2638a0292f23SGarrett Wollman process_ACK: 26398501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 26407cfc6904SRobert Watson 2641dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 264278b50714SRobert Watson TCPSTAT_INC(tcps_rcvackpack); 264378b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 2644df8bae1dSRodney W. Grimes 2645df8bae1dSRodney W. Grimes /* 26469b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 26479b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 26489b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 26499b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 26509b8b58e0SJonathan Lemon * we left off. 26519b8b58e0SJonathan Lemon */ 2652672dc4aeSJohn Baldwin if (tp->t_rxtshift == 1 && tp->t_flags & TF_PREVVALID && 2653672dc4aeSJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) 2654dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 26559b8b58e0SJonathan Lemon 26569b8b58e0SJonathan Lemon /* 2657df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2658df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2659df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2660df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2661df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2662df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2663df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2664fa55172bSMatthew Dillon * 2665fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2666fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2667fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2668fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2669df8bae1dSRodney W. Grimes */ 2670d8951c8aSBjoern A. Zeeb if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) { 2671d8951c8aSBjoern A. Zeeb u_int t; 2672d8951c8aSBjoern A. Zeeb 2673d8951c8aSBjoern A. Zeeb t = tcp_ts_getticks() - to.to_tsecr; 2674d8951c8aSBjoern A. Zeeb if (!tp->t_rttlow || tp->t_rttlow > t) 2675d8951c8aSBjoern A. Zeeb tp->t_rttlow = t; 2676d8951c8aSBjoern A. Zeeb tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1); 2677fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2678eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2679eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 26809b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2681fa55172bSMatthew Dillon } 2682df8bae1dSRodney W. Grimes 2683df8bae1dSRodney W. Grimes /* 2684df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2685df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2686df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2687df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2688df8bae1dSRodney W. Grimes */ 2689fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 2690b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2691df8bae1dSRodney W. Grimes needoutput = 1; 2692b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2693b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2694a0292f23SGarrett Wollman 2695a0292f23SGarrett Wollman /* 2696a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2697a0292f23SGarrett Wollman * skip rest of ACK processing. 2698a0292f23SGarrett Wollman */ 2699a0292f23SGarrett Wollman if (acked == 0) 2700a0292f23SGarrett Wollman goto step6; 2701a0292f23SGarrett Wollman 2702df8bae1dSRodney W. Grimes /* 2703dbc42409SLawrence Stewart * Let the congestion control algorithm update congestion 2704dbc42409SLawrence Stewart * control related information. This typically means increasing 2705dbc42409SLawrence Stewart * the congestion window. 2706df8bae1dSRodney W. Grimes */ 2707dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_ACK); 2708dbc42409SLawrence Stewart 27095905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2710df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 271160ee3bb2SAndre Oppermann tp->snd_wnd -= so->so_snd.sb_cc; 27125905999bSRobert Watson sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc); 2713df8bae1dSRodney W. Grimes ourfinisacked = 1; 2714df8bae1dSRodney W. Grimes } else { 27155905999bSRobert Watson sbdrop_locked(&so->so_snd, acked); 271660ee3bb2SAndre Oppermann tp->snd_wnd -= acked; 2717df8bae1dSRodney W. Grimes ourfinisacked = 0; 2718df8bae1dSRodney W. Grimes } 2719564aab1fSAndre Oppermann /* NB: sowwakeup_locked() does an implicit unlock. */ 27201e4d7da7SRobert Watson sowwakeup_locked(so); 2721e8949f74SAndre Oppermann /* Detect una wraparound. */ 2722dbc42409SLawrence Stewart if (!IN_RECOVERY(tp->t_flags) && 27239d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 27249d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 27259d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 2726dbc42409SLawrence Stewart /* XXXLAS: Can this be moved up into cc_post_recovery? */ 2727dbc42409SLawrence Stewart if (IN_RECOVERY(tp->t_flags) && 272824cb0f22SLawrence Stewart SEQ_GEQ(th->th_ack, tp->snd_recover)) { 2729dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 273024cb0f22SLawrence Stewart } 2731fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 27323529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 27336d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 27346d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 27356d90faf3SPaul Saab } 2736df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2737df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2738df8bae1dSRodney W. Grimes 2739df8bae1dSRodney W. Grimes switch (tp->t_state) { 2740df8bae1dSRodney W. Grimes 2741df8bae1dSRodney W. Grimes /* 2742df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2743df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2744df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2745df8bae1dSRodney W. Grimes */ 2746df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2747df8bae1dSRodney W. Grimes if (ourfinisacked) { 2748df8bae1dSRodney W. Grimes /* 2749df8bae1dSRodney W. Grimes * If we can't receive any more 2750df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2751df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2752df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2753df8bae1dSRodney W. Grimes * we'll hang forever. 2754e8949f74SAndre Oppermann * 2755e8949f74SAndre Oppermann * XXXjl: 2756340c35deSJonathan Lemon * we should release the tp also, and use a 2757340c35deSJonathan Lemon * compressed state. 2758340c35deSJonathan Lemon */ 2759c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 276003e49181SSeigo Tanimura soisdisconnected(so); 27619077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 27629077f387SGleb Smirnoff (tcp_fast_finwait2_recycle ? 27639077f387SGleb Smirnoff tcp_finwait2_timeout : 27649077f387SGleb Smirnoff TP_MAXIDLE(tp))); 27654cc20ab1SSeigo Tanimura } 2766df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 2767df8bae1dSRodney W. Grimes } 2768df8bae1dSRodney W. Grimes break; 2769df8bae1dSRodney W. Grimes 2770df8bae1dSRodney W. Grimes /* 2771df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2772df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2773df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2774df8bae1dSRodney W. Grimes * the segment. 2775df8bae1dSRodney W. Grimes */ 2776df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2777df8bae1dSRodney W. Grimes if (ourfinisacked) { 2778252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 277911a20fb8SJeffrey Hsu tcp_twstart(tp); 2780603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2781340c35deSJonathan Lemon m_freem(m); 2782679d9708SAndre Oppermann return; 2783df8bae1dSRodney W. Grimes } 2784df8bae1dSRodney W. Grimes break; 2785df8bae1dSRodney W. Grimes 2786df8bae1dSRodney W. Grimes /* 2787df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2788df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2789df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2790df8bae1dSRodney W. Grimes * enter the closed state and return. 2791df8bae1dSRodney W. Grimes */ 2792df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2793df8bae1dSRodney W. Grimes if (ourfinisacked) { 2794252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2795df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2796df8bae1dSRodney W. Grimes goto drop; 2797df8bae1dSRodney W. Grimes } 2798df8bae1dSRodney W. Grimes break; 2799df8bae1dSRodney W. Grimes } 2800df8bae1dSRodney W. Grimes } 2801df8bae1dSRodney W. Grimes 2802df8bae1dSRodney W. Grimes step6: 28038501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 28047cfc6904SRobert Watson 2805df8bae1dSRodney W. Grimes /* 280660ee3bb2SAndre Oppermann * Update window information. 280760ee3bb2SAndre Oppermann * Don't look at window if no ACK: TAC's send garbage on first SYN. 2808df8bae1dSRodney W. Grimes */ 280960ee3bb2SAndre Oppermann if ((thflags & TH_ACK) && 281060ee3bb2SAndre Oppermann (SEQ_LT(tp->snd_wl1, th->th_seq) || 281160ee3bb2SAndre Oppermann (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 281260ee3bb2SAndre Oppermann (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 281360ee3bb2SAndre Oppermann /* keep track of pure window updates */ 281460ee3bb2SAndre Oppermann if (tlen == 0 && 281560ee3bb2SAndre Oppermann tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 281678b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinupd); 2817df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 281860ee3bb2SAndre Oppermann tp->snd_wl1 = th->th_seq; 281960ee3bb2SAndre Oppermann tp->snd_wl2 = th->th_ack; 2820df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2821df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 282260ee3bb2SAndre Oppermann needoutput = 1; 2823df8bae1dSRodney W. Grimes } 2824df8bae1dSRodney W. Grimes 2825df8bae1dSRodney W. Grimes /* 2826df8bae1dSRodney W. Grimes * Process segments with URG. 2827df8bae1dSRodney W. Grimes */ 2828fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2829df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2830df8bae1dSRodney W. Grimes /* 2831df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2832df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2833df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2834df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2835df8bae1dSRodney W. Grimes */ 283618ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2837fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2838fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2839fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 284018ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2841df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2842df8bae1dSRodney W. Grimes } 2843df8bae1dSRodney W. Grimes /* 2844df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2845df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2846df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2847df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2848df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2849df8bae1dSRodney W. Grimes * 2850df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2851df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2852df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2853df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2854df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2855df8bae1dSRodney W. Grimes * spec states (in one of two places). 2856df8bae1dSRodney W. Grimes */ 2857fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2858fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2859df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2860df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2861927c5ceaSRobert Watson if (so->so_oobmark == 0) 2862c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 2863df8bae1dSRodney W. Grimes sohasoutofband(so); 2864df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2865df8bae1dSRodney W. Grimes } 286618ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2867df8bae1dSRodney W. Grimes /* 2868df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2869df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2870df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2871df8bae1dSRodney W. Grimes * data may creep in... ick. 2872df8bae1dSRodney W. Grimes */ 287330613f56SJeffrey Hsu if (th->th_urp <= (u_long)tlen && 287430613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 287530613f56SJeffrey Hsu /* hdr drop is delayed */ 287630613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 287730613f56SJeffrey Hsu } 2878c068736aSJeffrey Hsu } else { 2879df8bae1dSRodney W. Grimes /* 2880df8bae1dSRodney W. Grimes * If no out of band data is expected, 2881df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2882df8bae1dSRodney W. Grimes * with the receive window. 2883df8bae1dSRodney W. Grimes */ 2884df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2885df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2886c068736aSJeffrey Hsu } 2887df8bae1dSRodney W. Grimes dodata: /* XXX */ 28888501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 28897cfc6904SRobert Watson 2890df8bae1dSRodney W. Grimes /* 2891df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2892df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2893df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2894df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2895df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2896df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2897df8bae1dSRodney W. Grimes */ 2898fb59c426SYoshinobu Inoue if ((tlen || (thflags & TH_FIN)) && 2899df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 290069e03620SPaul Saab tcp_seq save_start = th->th_seq; 2901fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2902e4b64281SJesper Skriver /* 2903c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2904c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2905c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2906c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2907c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2908c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2909c068736aSJeffrey Hsu * conversions. 2910c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 2911c068736aSJeffrey Hsu * immediately when segments are out of order (so 2912c068736aSJeffrey Hsu * fast retransmit can work). 2913e4b64281SJesper Skriver */ 2914e4b64281SJesper Skriver if (th->th_seq == tp->rcv_nxt && 2915e4b64281SJesper Skriver LIST_EMPTY(&tp->t_segq) && 2916e4b64281SJesper Skriver TCPS_HAVEESTABLISHED(tp->t_state)) { 2917e4b64281SJesper Skriver if (DELAY_ACK(tp)) 29183bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2919e4b64281SJesper Skriver else 2920e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2921e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2922e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 292378b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 292478b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 2925e4b64281SJesper Skriver ND6_HINT(tp); 29261e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2927c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2928c1c36a2cSMike Silbersack m_freem(m); 2929c1c36a2cSMike Silbersack else 29301e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 2931e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 29321e4d7da7SRobert Watson sorwakeup_locked(so); 2933e4b64281SJesper Skriver } else { 2934e8949f74SAndre Oppermann /* 2935e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 2936e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 2937e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 2938e8949f74SAndre Oppermann * when trimming from the head. 2939e8949f74SAndre Oppermann */ 2940e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 2941e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2942e4b64281SJesper Skriver } 29433529149eSAndre Oppermann if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 2944f194524fSAndre Oppermann tcp_update_sack_list(tp, save_start, save_start + tlen); 2945302ce8d6SAndre Oppermann #if 0 2946df8bae1dSRodney W. Grimes /* 2947df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2948df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2949df8bae1dSRodney W. Grimes * buffer size. 2950302ce8d6SAndre Oppermann * XXX: Unused. 2951df8bae1dSRodney W. Grimes */ 2952f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) 2953df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2954f701e30dSJohn Baldwin else 2955f701e30dSJohn Baldwin len = so->so_rcv.sb_hiwat; 2956302ce8d6SAndre Oppermann #endif 2957df8bae1dSRodney W. Grimes } else { 2958df8bae1dSRodney W. Grimes m_freem(m); 2959fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2960df8bae1dSRodney W. Grimes } 2961df8bae1dSRodney W. Grimes 2962df8bae1dSRodney W. Grimes /* 2963df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2964df8bae1dSRodney W. Grimes * that the connection is closing. 2965df8bae1dSRodney W. Grimes */ 2966fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2967df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2968df8bae1dSRodney W. Grimes socantrcvmore(so); 2969a0292f23SGarrett Wollman /* 2970a0292f23SGarrett Wollman * If connection is half-synchronized 2971d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2972a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2973a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2974a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2975a0292f23SGarrett Wollman */ 29763bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 29773bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2978a0292f23SGarrett Wollman else 2979df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2980df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2981df8bae1dSRodney W. Grimes } 2982df8bae1dSRodney W. Grimes switch (tp->t_state) { 2983df8bae1dSRodney W. Grimes 2984df8bae1dSRodney W. Grimes /* 2985df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2986df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2987df8bae1dSRodney W. Grimes */ 2988df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 29899b8b58e0SJonathan Lemon tp->t_starttime = ticks; 29909b8b58e0SJonathan Lemon /* FALLTHROUGH */ 2991df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2992df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2993df8bae1dSRodney W. Grimes break; 2994df8bae1dSRodney W. Grimes 2995df8bae1dSRodney W. Grimes /* 2996df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2997df8bae1dSRodney W. Grimes * enter the CLOSING state. 2998df8bae1dSRodney W. Grimes */ 2999df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 3000df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 3001df8bae1dSRodney W. Grimes break; 3002df8bae1dSRodney W. Grimes 3003df8bae1dSRodney W. Grimes /* 3004df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 3005df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 3006df8bae1dSRodney W. Grimes * standard timers. 3007df8bae1dSRodney W. Grimes */ 3008df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 3009252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 3010252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: dodata " 3011252ca428SRobert Watson "TCP_FIN_WAIT_2 ti_locked: %d", __func__, 3012252ca428SRobert Watson ti_locked)); 3013252ca428SRobert Watson 3014340c35deSJonathan Lemon tcp_twstart(tp); 3015603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 3016679d9708SAndre Oppermann return; 3017df8bae1dSRodney W. Grimes } 3018df8bae1dSRodney W. Grimes } 3019fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 3020603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 3021252ca428SRobert Watson ti_locked = TI_UNLOCKED; 3022252ca428SRobert Watson 3023610ee2f9SDavid Greenman #ifdef TCPDEBUG 30244cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3025fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 3026fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3027610ee2f9SDavid Greenman #endif 3028df8bae1dSRodney W. Grimes 3029df8bae1dSRodney W. Grimes /* 3030df8bae1dSRodney W. Grimes * Return any desired output. 3031df8bae1dSRodney W. Grimes */ 3032df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 3033df8bae1dSRodney W. Grimes (void) tcp_output(tp); 30347792ea27SJeffrey Hsu 3035a14c749fSJonathan Lemon check_delack: 3036252ca428SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 3037252ca428SRobert Watson __func__, ti_locked)); 3038603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 30398501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 3040252ca428SRobert Watson 3041a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 30423bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 3043b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 30443bfd6421SJonathan Lemon } 30458501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3046679d9708SAndre Oppermann return; 3047df8bae1dSRodney W. Grimes 3048df8bae1dSRodney W. Grimes dropafterack: 3049df8bae1dSRodney W. Grimes /* 3050df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 3051df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 305280ab7c0eSGarrett Wollman * 305380ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 305480ab7c0eSGarrett Wollman * paths to this code happen after packets containing 305580ab7c0eSGarrett Wollman * RST have been dropped. 305680ab7c0eSGarrett Wollman * 305780ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 305880ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 305980ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 306080ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 306180ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 306280ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 3063df8bae1dSRodney W. Grimes */ 3064fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 3065fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 3066a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 3067a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 3068a57815efSBosko Milekic goto dropwithreset; 3069a57815efSBosko Milekic } 3070a0292f23SGarrett Wollman #ifdef TCPDEBUG 30714cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 3072fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3073fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3074a0292f23SGarrett Wollman #endif 3075fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 3076603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 3077252ca428SRobert Watson ti_locked = TI_UNLOCKED; 3078252ca428SRobert Watson 3079df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 3080df8bae1dSRodney W. Grimes (void) tcp_output(tp); 30818501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3082d6915262SRobert Watson m_freem(m); 3083679d9708SAndre Oppermann return; 3084df8bae1dSRodney W. Grimes 3085df8bae1dSRodney W. Grimes dropwithreset: 3086fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 3087dd8ac7f9SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 3088252ca428SRobert Watson ti_locked = TI_UNLOCKED; 3089a57815efSBosko Milekic 3090a0ca0871SRobert Watson if (tp != NULL) { 3091302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 30928501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3093dd8ac7f9SRobert Watson } else 3094a0ca0871SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 3095679d9708SAndre Oppermann return; 3096df8bae1dSRodney W. Grimes 3097df8bae1dSRodney W. Grimes drop: 3098fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) { 3099252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 3100fa046d87SRobert Watson ti_locked = TI_UNLOCKED; 3101fa046d87SRobert Watson } 3102252ca428SRobert Watson #ifdef INVARIANTS 3103252ca428SRobert Watson else 3104252ca428SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 3105252ca428SRobert Watson #endif 3106252ca428SRobert Watson 3107df8bae1dSRodney W. Grimes /* 3108df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 3109df8bae1dSRodney W. Grimes */ 3110610ee2f9SDavid Greenman #ifdef TCPDEBUG 31111c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 3112fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3113fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3114a0292f23SGarrett Wollman #endif 31151c53f806SRobert Watson if (tp != NULL) 31168501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3117d6915262SRobert Watson m_freem(m); 3118302ce8d6SAndre Oppermann } 3119302ce8d6SAndre Oppermann 3120302ce8d6SAndre Oppermann /* 31210ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 31220ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 31230ca3f933SAndre Oppermann * tp may be NULL. 3124302ce8d6SAndre Oppermann */ 3125302ce8d6SAndre Oppermann static void 3126302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 3127302ce8d6SAndre Oppermann int tlen, int rstreason) 3128302ce8d6SAndre Oppermann { 3129b287c6c7SBjoern A. Zeeb #ifdef INET 3130302ce8d6SAndre Oppermann struct ip *ip; 3131b287c6c7SBjoern A. Zeeb #endif 3132302ce8d6SAndre Oppermann #ifdef INET6 3133302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 3134302ce8d6SAndre Oppermann #endif 31357a3244ccSRobert Watson 31367a3244ccSRobert Watson if (tp != NULL) { 31378501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 31387a3244ccSRobert Watson } 31397a3244ccSRobert Watson 31400ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 3141302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 3142302ce8d6SAndre Oppermann goto drop; 3143302ce8d6SAndre Oppermann #ifdef INET6 3144302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 3145302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 3146302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3147302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3148302ce8d6SAndre Oppermann goto drop; 3149302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 3150b287c6c7SBjoern A. Zeeb } 3151302ce8d6SAndre Oppermann #endif 3152b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3153b287c6c7SBjoern A. Zeeb else 3154b287c6c7SBjoern A. Zeeb #endif 3155b287c6c7SBjoern A. Zeeb #ifdef INET 3156302ce8d6SAndre Oppermann { 3157302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 3158302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3159302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3160302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3161302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3162302ce8d6SAndre Oppermann goto drop; 3163302ce8d6SAndre Oppermann } 3164b287c6c7SBjoern A. Zeeb #endif 3165302ce8d6SAndre Oppermann 3166302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 3167302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 3168302ce8d6SAndre Oppermann goto drop; 3169302ce8d6SAndre Oppermann 3170302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 3171302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 3172302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 3173302ce8d6SAndre Oppermann th->th_ack, TH_RST); 3174302ce8d6SAndre Oppermann } else { 3175302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 3176302ce8d6SAndre Oppermann tlen++; 3177302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 3178302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 3179302ce8d6SAndre Oppermann } 3180302ce8d6SAndre Oppermann return; 3181302ce8d6SAndre Oppermann drop: 3182302ce8d6SAndre Oppermann m_freem(m); 3183df8bae1dSRodney W. Grimes } 3184df8bae1dSRodney W. Grimes 3185be2ac88cSJonathan Lemon /* 3186be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 3187be2ac88cSJonathan Lemon */ 31880312fbe9SPoul-Henning Kamp static void 3189ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 3190df8bae1dSRodney W. Grimes { 3191df8bae1dSRodney W. Grimes int opt, optlen; 3192df8bae1dSRodney W. Grimes 3193be2ac88cSJonathan Lemon to->to_flags = 0; 3194df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 3195df8bae1dSRodney W. Grimes opt = cp[0]; 3196df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 3197df8bae1dSRodney W. Grimes break; 3198df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 3199df8bae1dSRodney W. Grimes optlen = 1; 3200df8bae1dSRodney W. Grimes else { 3201b474779fSJun-ichiro itojun Hagino if (cnt < 2) 3202b474779fSJun-ichiro itojun Hagino break; 3203df8bae1dSRodney W. Grimes optlen = cp[1]; 3204b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 3205df8bae1dSRodney W. Grimes break; 3206df8bae1dSRodney W. Grimes } 3207df8bae1dSRodney W. Grimes switch (opt) { 3208df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 3209df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 3210df8bae1dSRodney W. Grimes continue; 3211f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3212df8bae1dSRodney W. Grimes continue; 3213be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 3214be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 3215be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 3216fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 3217df8bae1dSRodney W. Grimes break; 3218df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 3219df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 3220df8bae1dSRodney W. Grimes continue; 3221f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3222df8bae1dSRodney W. Grimes continue; 3223be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 322402a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 3225df8bae1dSRodney W. Grimes break; 3226df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 3227df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 3228df8bae1dSRodney W. Grimes continue; 3229be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 3230a0292f23SGarrett Wollman bcopy((char *)cp + 2, 3231a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 3232fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 3233a0292f23SGarrett Wollman bcopy((char *)cp + 6, 3234a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 3235fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 3236df8bae1dSRodney W. Grimes break; 32371cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 32381cfd4b53SBruce M Simpson /* 32391cfd4b53SBruce M Simpson * XXX In order to reply to a host which has set the 32401cfd4b53SBruce M Simpson * TCP_SIGNATURE option in its initial SYN, we have to 32411cfd4b53SBruce M Simpson * record the fact that the option was observed here 32421cfd4b53SBruce M Simpson * for the syncache code to perform the correct response. 32431cfd4b53SBruce M Simpson */ 32441cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 32451cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 32461cfd4b53SBruce M Simpson continue; 3247df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 3248df47e437SAndre Oppermann to->to_signature = cp + 2; 32491cfd4b53SBruce M Simpson break; 3250265ed012SBruce M Simpson #endif 32516d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 3252f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 32536d90faf3SPaul Saab continue; 3254f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3255f72167f4SAndre Oppermann continue; 3256603724d3SBjoern A. Zeeb if (!V_tcp_do_sack) 3257f72167f4SAndre Oppermann continue; 3258fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 32596d90faf3SPaul Saab break; 32606d90faf3SPaul Saab case TCPOPT_SACK: 32615a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 32626d90faf3SPaul Saab continue; 3263b728e902SAndre Oppermann if (flags & TO_SYN) 3264b728e902SAndre Oppermann continue; 3265fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 32665a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 32675a53ca16SPaul Saab to->to_sacks = cp + 2; 326878b50714SRobert Watson TCPSTAT_INC(tcps_sack_rcv_blocks); 32696d90faf3SPaul Saab break; 3270be2ac88cSJonathan Lemon default: 3271be2ac88cSJonathan Lemon continue; 3272df8bae1dSRodney W. Grimes } 3273df8bae1dSRodney W. Grimes } 3274df8bae1dSRodney W. Grimes } 3275df8bae1dSRodney W. Grimes 3276df8bae1dSRodney W. Grimes /* 3277df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 3278df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 3279df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 3280df8bae1dSRodney W. Grimes * sequencing purposes. 3281df8bae1dSRodney W. Grimes */ 32820312fbe9SPoul-Henning Kamp static void 3283ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 3284ad3f9ab3SAndre Oppermann int off) 3285df8bae1dSRodney W. Grimes { 3286fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 3287df8bae1dSRodney W. Grimes 3288df8bae1dSRodney W. Grimes while (cnt >= 0) { 3289df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 3290df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 3291df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 3292df8bae1dSRodney W. Grimes 32938501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 32947a3244ccSRobert Watson 3295df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 3296df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 3297df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 3298df8bae1dSRodney W. Grimes m->m_len--; 329969a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 330069a34685SYoshinobu Inoue m->m_pkthdr.len--; 3301df8bae1dSRodney W. Grimes return; 3302df8bae1dSRodney W. Grimes } 3303df8bae1dSRodney W. Grimes cnt -= m->m_len; 3304df8bae1dSRodney W. Grimes m = m->m_next; 33054dfdffe9SAndre Oppermann if (m == NULL) 3306df8bae1dSRodney W. Grimes break; 3307df8bae1dSRodney W. Grimes } 3308df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 3309df8bae1dSRodney W. Grimes } 3310df8bae1dSRodney W. Grimes 3311df8bae1dSRodney W. Grimes /* 3312df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 3313df8bae1dSRodney W. Grimes * and update averages and current timeout. 3314df8bae1dSRodney W. Grimes */ 33150312fbe9SPoul-Henning Kamp static void 3316ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 3317df8bae1dSRodney W. Grimes { 3318ad3f9ab3SAndre Oppermann int delta; 3319233e8c18SGarrett Wollman 33208501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 33212be3bf22SRobert Watson 332278b50714SRobert Watson TCPSTAT_INC(tcps_rttupdated); 3323233e8c18SGarrett Wollman tp->t_rttupdated++; 3324233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 3325233e8c18SGarrett Wollman /* 3326233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 3327233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 3328233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 3329233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3330233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 3331233e8c18SGarrett Wollman */ 3332233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3333233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3334233e8c18SGarrett Wollman 3335233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 3336233e8c18SGarrett Wollman tp->t_srtt = 1; 3337233e8c18SGarrett Wollman 3338233e8c18SGarrett Wollman /* 3339233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 3340233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 3341233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 3342233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 3343233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 3344233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 3345233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3346233e8c18SGarrett Wollman * rfc793's wired-in beta. 3347233e8c18SGarrett Wollman */ 3348233e8c18SGarrett Wollman if (delta < 0) 3349233e8c18SGarrett Wollman delta = -delta; 3350233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3351233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 3352233e8c18SGarrett Wollman tp->t_rttvar = 1; 33531fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 33541fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3355233e8c18SGarrett Wollman } else { 3356233e8c18SGarrett Wollman /* 3357233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 3358233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 3359233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 3360233e8c18SGarrett Wollman */ 3361233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 3362233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 33631fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3364233e8c18SGarrett Wollman } 33659b8b58e0SJonathan Lemon tp->t_rtttime = 0; 3366df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 3367df8bae1dSRodney W. Grimes 3368df8bae1dSRodney W. Grimes /* 3369df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 3370df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 3371df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 3372df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 3373df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 3374df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 3375df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 3376df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 3377df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 3378df8bae1dSRodney W. Grimes */ 3379233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 33809e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3381df8bae1dSRodney W. Grimes 3382df8bae1dSRodney W. Grimes /* 3383df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 3384df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 3385df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 3386df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 3387df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 3388df8bae1dSRodney W. Grimes */ 3389df8bae1dSRodney W. Grimes tp->t_softerror = 0; 3390df8bae1dSRodney W. Grimes } 3391df8bae1dSRodney W. Grimes 3392df8bae1dSRodney W. Grimes /* 3393df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 3394df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 33954faaea55SAndre Oppermann * If none, use an mss that can be handled on the outgoing interface 33964faaea55SAndre Oppermann * without forcing IP to fragment. If no route is found, route has no mtu, 3397df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 3398df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 3399df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 3400df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 3401df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 3402df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 3403df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 3404a0292f23SGarrett Wollman * 3405a0292f23SGarrett Wollman * Also take into account the space needed for options that we 3406a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 3407a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 3408a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 3409a0292f23SGarrett Wollman * data in maxopd. 3410a0292f23SGarrett Wollman * 341197d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 3412ef341ee1SGleb Smirnoff * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS 3413ef341ee1SGleb Smirnoff * settings are handled in tcp_mssopt(). 3414df8bae1dSRodney W. Grimes */ 3415a0292f23SGarrett Wollman void 3416ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer, 341791d6cfa6SBjoern A. Zeeb struct hc_metrics_lite *metricptr, int *mtuflags) 3418df8bae1dSRodney W. Grimes { 3419b287c6c7SBjoern A. Zeeb int mss = 0; 3420b287c6c7SBjoern A. Zeeb u_long maxmtu = 0; 3421c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 342297d8d152SAndre Oppermann struct hc_metrics_lite metrics; 3423ef341ee1SGleb Smirnoff int origoffer; 3424fb59c426SYoshinobu Inoue #ifdef INET6 3425c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3426c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 3427c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3428c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 3429c068736aSJeffrey Hsu #else 3430c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 3431fb59c426SYoshinobu Inoue #endif 3432df8bae1dSRodney W. Grimes 34338501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 34347a3244ccSRobert Watson 3435ef341ee1SGleb Smirnoff if (mtuoffer != -1) { 3436ef341ee1SGleb Smirnoff KASSERT(offer == -1, ("%s: conflict", __func__)); 3437ef341ee1SGleb Smirnoff offer = mtuoffer - min_protoh; 3438ef341ee1SGleb Smirnoff } 3439ef341ee1SGleb Smirnoff origoffer = offer; 3440ef341ee1SGleb Smirnoff 3441e8949f74SAndre Oppermann /* Initialize. */ 344297d8d152SAndre Oppermann #ifdef INET6 344397d8d152SAndre Oppermann if (isipv6) { 344491d6cfa6SBjoern A. Zeeb maxmtu = tcp_maxmtu6(&inp->inp_inc, mtuflags); 3445603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_v6mssdflt; 3446b287c6c7SBjoern A. Zeeb } 344797d8d152SAndre Oppermann #endif 3448b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3449b287c6c7SBjoern A. Zeeb else 3450b287c6c7SBjoern A. Zeeb #endif 3451b287c6c7SBjoern A. Zeeb #ifdef INET 345297d8d152SAndre Oppermann { 345391d6cfa6SBjoern A. Zeeb maxmtu = tcp_maxmtu(&inp->inp_inc, mtuflags); 3454603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_mssdflt; 3455df8bae1dSRodney W. Grimes } 3456b287c6c7SBjoern A. Zeeb #endif 3457df8bae1dSRodney W. Grimes 345897d8d152SAndre Oppermann /* 3459e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 346097d8d152SAndre Oppermann */ 34616f01cac6SBjoern A. Zeeb if (maxmtu == 0) { 34626f01cac6SBjoern A. Zeeb /* 34638e5c87f4SBjoern A. Zeeb * In case we return early we need to initialize metrics 34646f01cac6SBjoern A. Zeeb * to a defined state as tcp_hc_get() would do for us 34656f01cac6SBjoern A. Zeeb * if there was no cache hit. 34666f01cac6SBjoern A. Zeeb */ 34676f01cac6SBjoern A. Zeeb if (metricptr != NULL) 34686f01cac6SBjoern A. Zeeb bzero(metricptr, sizeof(struct hc_metrics_lite)); 346997d8d152SAndre Oppermann return; 34706f01cac6SBjoern A. Zeeb } 347197d8d152SAndre Oppermann 3472e8949f74SAndre Oppermann /* What have we got? */ 347397d8d152SAndre Oppermann switch (offer) { 347497d8d152SAndre Oppermann case 0: 347597d8d152SAndre Oppermann /* 347697d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 3477c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 3478c3b02504SBjoern A. Zeeb * already assigned to t_maxopd above. 347997d8d152SAndre Oppermann */ 3480c3b02504SBjoern A. Zeeb offer = tp->t_maxopd; 348197d8d152SAndre Oppermann break; 348297d8d152SAndre Oppermann 348397d8d152SAndre Oppermann case -1: 3484a0292f23SGarrett Wollman /* 3485c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 3486a0292f23SGarrett Wollman */ 348797d8d152SAndre Oppermann /* FALLTHROUGH */ 348897d8d152SAndre Oppermann 348997d8d152SAndre Oppermann default: 3490a0292f23SGarrett Wollman /* 349153369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 349253369ac9SAndre Oppermann * to at least minmss. 349353369ac9SAndre Oppermann */ 3494603724d3SBjoern A. Zeeb offer = max(offer, V_tcp_minmss); 349597d8d152SAndre Oppermann } 3496a0292f23SGarrett Wollman 3497df8bae1dSRodney W. Grimes /* 3498e8949f74SAndre Oppermann * rmx information is now retrieved from tcp_hostcache. 3499df8bae1dSRodney W. Grimes */ 350097d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 35013cee92e0SBjoern A. Zeeb if (metricptr != NULL) 35023cee92e0SBjoern A. Zeeb bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 350397d8d152SAndre Oppermann 3504df8bae1dSRodney W. Grimes /* 3505e8949f74SAndre Oppermann * If there's a discovered mtu int tcp hostcache, use it 3506fb59c426SYoshinobu Inoue * else, use the link mtu. 3507df8bae1dSRodney W. Grimes */ 350897d8d152SAndre Oppermann if (metrics.rmx_mtu) 35092d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3510c068736aSJeffrey Hsu else { 351131b3783cSHajimu UMEMOTO #ifdef INET6 3512fb59c426SYoshinobu Inoue if (isipv6) { 351397d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3514603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 351597d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 3516603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_v6mssdflt); 3517b287c6c7SBjoern A. Zeeb } 3518b3399803SHajimu UMEMOTO #endif 3519b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3520b287c6c7SBjoern A. Zeeb else 3521b287c6c7SBjoern A. Zeeb #endif 3522b287c6c7SBjoern A. Zeeb #ifdef INET 352397d8d152SAndre Oppermann { 352497d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3525603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 352697d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 3527603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_mssdflt); 3528a0292f23SGarrett Wollman } 3529b287c6c7SBjoern A. Zeeb #endif 35303cee92e0SBjoern A. Zeeb /* 35313cee92e0SBjoern A. Zeeb * XXX - The above conditional (mss = maxmtu - min_protoh) 35323cee92e0SBjoern A. Zeeb * probably violates the TCP spec. 35333cee92e0SBjoern A. Zeeb * The problem is that, since we don't know the 35343cee92e0SBjoern A. Zeeb * other end's MSS, we are supposed to use a conservative 35353cee92e0SBjoern A. Zeeb * default. But, if we do that, then MTU discovery will 35363cee92e0SBjoern A. Zeeb * never actually take place, because the conservative 35373cee92e0SBjoern A. Zeeb * default is much less than the MTUs typically seen 35383cee92e0SBjoern A. Zeeb * on the Internet today. For the moment, we'll sweep 35393cee92e0SBjoern A. Zeeb * this under the carpet. 35403cee92e0SBjoern A. Zeeb * 35413cee92e0SBjoern A. Zeeb * The conservative default might not actually be a problem 35423cee92e0SBjoern A. Zeeb * if the only case this occurs is when sending an initial 35433cee92e0SBjoern A. Zeeb * SYN with options and data to a host we've never talked 35443cee92e0SBjoern A. Zeeb * to before. Then, they will reply with an MSS value which 35453cee92e0SBjoern A. Zeeb * will get recorded and the new parameters should get 35463cee92e0SBjoern A. Zeeb * recomputed. For Further Study. 35473cee92e0SBjoern A. Zeeb */ 354897d8d152SAndre Oppermann } 3549a0292f23SGarrett Wollman mss = min(mss, offer); 355097d8d152SAndre Oppermann 3551a0292f23SGarrett Wollman /* 35523cee92e0SBjoern A. Zeeb * Sanity check: make sure that maxopd will be large 35533cee92e0SBjoern A. Zeeb * enough to allow some data on segments even if the 35543cee92e0SBjoern A. Zeeb * all the option space is used (40bytes). Otherwise 35553cee92e0SBjoern A. Zeeb * funny things may happen in tcp_output. 35563cee92e0SBjoern A. Zeeb */ 35573cee92e0SBjoern A. Zeeb mss = max(mss, 64); 35583cee92e0SBjoern A. Zeeb 35593cee92e0SBjoern A. Zeeb /* 3560a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 3561a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 3562a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 3563a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 3564a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 3565a0292f23SGarrett Wollman */ 3566a0292f23SGarrett Wollman tp->t_maxopd = mss; 3567a0292f23SGarrett Wollman 3568a0292f23SGarrett Wollman /* 3569e8949f74SAndre Oppermann * origoffer==-1 indicates that no segments were received yet. 3570c94c54e4SAndre Oppermann * In this case we just guess. 3571a0292f23SGarrett Wollman */ 3572a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 3573a0292f23SGarrett Wollman (origoffer == -1 || 3574a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 3575a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 3576a0292f23SGarrett Wollman 357797d8d152SAndre Oppermann tp->t_maxseg = mss; 35783cee92e0SBjoern A. Zeeb } 35793cee92e0SBjoern A. Zeeb 35803cee92e0SBjoern A. Zeeb void 35813cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer) 35823cee92e0SBjoern A. Zeeb { 3583dbc42409SLawrence Stewart int mss; 35843cee92e0SBjoern A. Zeeb u_long bufsize; 35853cee92e0SBjoern A. Zeeb struct inpcb *inp; 35863cee92e0SBjoern A. Zeeb struct socket *so; 35873cee92e0SBjoern A. Zeeb struct hc_metrics_lite metrics; 358891d6cfa6SBjoern A. Zeeb int mtuflags = 0; 3589dbc42409SLawrence Stewart 35903cee92e0SBjoern A. Zeeb KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 35913cee92e0SBjoern A. Zeeb 3592ef341ee1SGleb Smirnoff tcp_mss_update(tp, offer, -1, &metrics, &mtuflags); 35933cee92e0SBjoern A. Zeeb 35943cee92e0SBjoern A. Zeeb mss = tp->t_maxseg; 35953cee92e0SBjoern A. Zeeb inp = tp->t_inpcb; 359697d8d152SAndre Oppermann 3597df8bae1dSRodney W. Grimes /* 359897d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 359997d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 360097d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 360197d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 360297d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3603df8bae1dSRodney W. Grimes */ 3604c3b02504SBjoern A. Zeeb so = inp->inp_socket; 36053f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 3606e233e2acSAndre Oppermann if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe) 360797d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 360897d8d152SAndre Oppermann else 3609df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3610df8bae1dSRodney W. Grimes if (bufsize < mss) 3611df8bae1dSRodney W. Grimes mss = bufsize; 3612df8bae1dSRodney W. Grimes else { 3613df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3614df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3615df8bae1dSRodney W. Grimes bufsize = sb_max; 361688c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 36173f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3618df8bae1dSRodney W. Grimes } 36193f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 3620df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 3621df8bae1dSRodney W. Grimes 36223f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3623e233e2acSAndre Oppermann if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe) 362497d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 362597d8d152SAndre Oppermann else 3626df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3627df8bae1dSRodney W. Grimes if (bufsize > mss) { 3628df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3629df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3630df8bae1dSRodney W. Grimes bufsize = sb_max; 363188c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 36323f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3633df8bae1dSRodney W. Grimes } 36343f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 363591d6cfa6SBjoern A. Zeeb 363691d6cfa6SBjoern A. Zeeb /* Check the interface for TSO capabilities. */ 363791d6cfa6SBjoern A. Zeeb if (mtuflags & CSUM_TSO) 363891d6cfa6SBjoern A. Zeeb tp->t_flags |= TF_TSO; 3639a0292f23SGarrett Wollman } 3640a0292f23SGarrett Wollman 3641a0292f23SGarrett Wollman /* 3642a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3643a0292f23SGarrett Wollman */ 3644a0292f23SGarrett Wollman int 3645ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 3646a0292f23SGarrett Wollman { 364797d8d152SAndre Oppermann int mss = 0; 364897d8d152SAndre Oppermann u_long maxmtu = 0; 364997d8d152SAndre Oppermann u_long thcmtu = 0; 365097d8d152SAndre Oppermann size_t min_protoh; 3651a0292f23SGarrett Wollman 365297d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3653a0292f23SGarrett Wollman 365431b3783cSHajimu UMEMOTO #ifdef INET6 3655dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3656603724d3SBjoern A. Zeeb mss = V_tcp_v6mssdflt; 3657233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 365897d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 3659b287c6c7SBjoern A. Zeeb } 366031b3783cSHajimu UMEMOTO #endif 3661b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3662b287c6c7SBjoern A. Zeeb else 3663b287c6c7SBjoern A. Zeeb #endif 3664b287c6c7SBjoern A. Zeeb #ifdef INET 366597d8d152SAndre Oppermann { 3666603724d3SBjoern A. Zeeb mss = V_tcp_mssdflt; 3667233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 366897d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 366997d8d152SAndre Oppermann } 3670b287c6c7SBjoern A. Zeeb #endif 36713a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET) 36723a9391deSBjoern A. Zeeb thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 36733a9391deSBjoern A. Zeeb #endif 36743a9391deSBjoern A. Zeeb 367597d8d152SAndre Oppermann if (maxmtu && thcmtu) 367697d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 367797d8d152SAndre Oppermann else if (maxmtu || thcmtu) 367897d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 367997d8d152SAndre Oppermann 368097d8d152SAndre Oppermann return (mss); 3681df8bae1dSRodney W. Grimes } 368246f58482SJonathan Lemon 368346f58482SJonathan Lemon 368446f58482SJonathan Lemon /* 3685c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3686c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3687c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3688c068736aSJeffrey Hsu * be started again. 368946f58482SJonathan Lemon */ 3690c068736aSJeffrey Hsu static void 3691ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 369246f58482SJonathan Lemon { 369346f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 369446f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 369546f58482SJonathan Lemon 36968501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 36977a3244ccSRobert Watson 3698b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 369946f58482SJonathan Lemon tp->t_rtttime = 0; 370046f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 37016b2a5f92SJayanth Vijayaraghavan /* 3702c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3703c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 37046b2a5f92SJayanth Vijayaraghavan */ 3705dbc42409SLawrence Stewart tp->snd_cwnd = tp->t_maxseg + BYTES_THIS_ACK(tp, th); 3706a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 37076b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 370846f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 370946f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 371046f58482SJonathan Lemon tp->snd_nxt = onxt; 371146f58482SJonathan Lemon /* 371246f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 371346f58482SJonathan Lemon * not updated yet. 371446f58482SJonathan Lemon */ 3715dbc42409SLawrence Stewart if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th)) 3716dbc42409SLawrence Stewart tp->snd_cwnd -= BYTES_THIS_ACK(tp, th); 3717d7587117SPaul Saab else 3718d7587117SPaul Saab tp->snd_cwnd = 0; 3719d7587117SPaul Saab tp->snd_cwnd += tp->t_maxseg; 372046f58482SJonathan Lemon } 3721