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. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 29e79adb8eSGarrett Wollman * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 324b421e2dSMike Silbersack #include <sys/cdefs.h> 334b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 344b421e2dSMike Silbersack 35f9e354dfSJulian Elischer #include "opt_ipfw.h" /* for ipfw_fwd */ 361cfd4b53SBruce M Simpson #include "opt_inet.h" 37fb59c426SYoshinobu Inoue #include "opt_inet6.h" 383a2a9f79SYoshinobu Inoue #include "opt_ipsec.h" 39c488362eSRobert Watson #include "opt_mac.h" 400cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 410cc12cc5SJoerg Wunsch 42df8bae1dSRodney W. Grimes #include <sys/param.h> 4398163b98SPoul-Henning Kamp #include <sys/kernel.h> 44df8bae1dSRodney W. Grimes #include <sys/malloc.h> 45df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 46a29f300eSGarrett Wollman #include <sys/proc.h> /* for proc0 declaration */ 47df8bae1dSRodney W. Grimes #include <sys/protosw.h> 48960ed29cSSeigo Tanimura #include <sys/signalvar.h> 49df8bae1dSRodney W. Grimes #include <sys/socket.h> 50df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 51960ed29cSSeigo Tanimura #include <sys/sysctl.h> 52816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 53960ed29cSSeigo Tanimura #include <sys/systm.h> 54603724d3SBjoern A. Zeeb #include <sys/vimage.h> 55df8bae1dSRodney W. Grimes 56e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 57e79adb8eSGarrett Wollman 5812e2e970SAndre Oppermann #include <vm/uma.h> 5912e2e970SAndre Oppermann 60df8bae1dSRodney W. Grimes #include <net/if.h> 61df8bae1dSRodney W. Grimes #include <net/route.h> 62df8bae1dSRodney W. Grimes 63773673c1SAndre Oppermann #define TCPSTATES /* for logging */ 64773673c1SAndre Oppermann 65df8bae1dSRodney W. Grimes #include <netinet/in.h> 66960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 67df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 68960ed29cSSeigo Tanimura #include <netinet/in_var.h> 69df8bae1dSRodney W. Grimes #include <netinet/ip.h> 708e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 71686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 72df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 73ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 74686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 75686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h> 76686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 77960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h> 78960ed29cSSeigo Tanimura #include <netinet6/nd6.h> 79df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 80df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 81df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 82df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 83df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 84fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 85df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 86c325962bSMike Silbersack #include <netinet/tcp_syncache.h> 87610ee2f9SDavid Greenman #ifdef TCPDEBUG 88df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 89fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */ 904b79449eSBjoern A. Zeeb #include <netinet/vinet.h> 914b79449eSBjoern A. Zeeb 924b79449eSBjoern A. Zeeb #ifdef INET6 934b79449eSBjoern A. Zeeb #include <netinet6/vinet6.h> 944b79449eSBjoern A. Zeeb #endif 95fb59c426SYoshinobu Inoue 96b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 97b9234fafSSam Leffler #include <netipsec/ipsec.h> 98b9234fafSSam Leffler #include <netipsec/ipsec6.h> 99b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 100b9234fafSSam Leffler 101db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 102db4f9cc7SJonathan Lemon 103aed55708SRobert Watson #include <security/mac/mac_framework.h> 104aed55708SRobert Watson 105c068736aSJeffrey Hsu static const int tcprexmtthresh = 3; 1060312fbe9SPoul-Henning Kamp 10744e33a07SMarko Zec #ifdef VIMAGE_GLOBALS 1082f96f1f4SGarrett Wollman struct tcpstat tcpstat; 10944e33a07SMarko Zec int blackhole; 11044e33a07SMarko Zec int tcp_delack_enabled; 11144e33a07SMarko Zec int drop_synfin; 11244e33a07SMarko Zec int tcp_do_rfc3042; 11344e33a07SMarko Zec int tcp_do_rfc3390; 11444e33a07SMarko Zec int tcp_do_ecn; 11544e33a07SMarko Zec int tcp_ecn_maxretries; 11644e33a07SMarko Zec int tcp_insecure_rst; 11744e33a07SMarko Zec int tcp_do_autorcvbuf; 11844e33a07SMarko Zec int tcp_autorcvbuf_inc; 11944e33a07SMarko Zec int tcp_autorcvbuf_max; 12024cb0f22SLawrence Stewart int tcp_do_rfc3465; 12124cb0f22SLawrence Stewart int tcp_abc_l_var; 12244e33a07SMarko Zec #endif 12344e33a07SMarko Zec 1248b615593SMarko Zec SYSCTL_V_STRUCT(V_NET, vnet_inet, _net_inet_tcp, TCPCTL_STATS, stats, 1258b615593SMarko Zec CTLFLAG_RW, tcpstat , tcpstat, 1268b615593SMarko Zec "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 1270312fbe9SPoul-Henning Kamp 128773673c1SAndre Oppermann int tcp_log_in_vain = 0; 129816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 130574b6964SAndre Oppermann &tcp_log_in_vain, 0, "Log all incoming TCP segments to closed ports"); 131816a3d83SPoul-Henning Kamp 1328b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 1338b615593SMarko Zec blackhole, 0, "Do not send RST on segments to closed ports"); 13416f7f31fSGeoff Rehmet 1358b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, delayed_ack, 1368b615593SMarko Zec CTLFLAG_RW, tcp_delack_enabled, 0, 1373d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 138f498eeeeSDavid Greenman 1398b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, drop_synfin, 1408b615593SMarko Zec CTLFLAG_RW, drop_synfin, 0, "Drop TCP packets with SYN+FIN set"); 141f8613305SDag-Erling Smørgrav 1428b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW, 1438b615593SMarko Zec tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)"); 144582a954bSJeffrey Hsu 1458b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW, 1468b615593SMarko Zec tcp_do_rfc3390, 0, 147da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 148da3a8a1aSJeffrey Hsu 14924cb0f22SLawrence Stewart SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_RW, 15024cb0f22SLawrence Stewart tcp_do_rfc3465, 0, 15124cb0f22SLawrence Stewart "Enable RFC 3465 (Appropriate Byte Counting)"); 15224cb0f22SLawrence Stewart SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_RW, 15324cb0f22SLawrence Stewart tcp_abc_l_var, 2, 15424cb0f22SLawrence Stewart "Cap the max cwnd increment during slow-start to this number of segments"); 15524cb0f22SLawrence Stewart 156f2512ba1SRui Paulo SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN"); 1578b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_ecn, OID_AUTO, enable, 1588b615593SMarko Zec CTLFLAG_RW, tcp_do_ecn, 0, "TCP ECN support"); 1598b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_ecn, OID_AUTO, maxretries, 1608b615593SMarko Zec CTLFLAG_RW, tcp_ecn_maxretries, 0, "Max retries before giving up on ECN"); 161f2512ba1SRui Paulo 1628b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, insecure_rst, 1638b615593SMarko Zec CTLFLAG_RW, tcp_insecure_rst, 0, 1646489fe65SAndre Oppermann "Follow the old (insecure) criteria for accepting RST packets"); 165a69968eeSMike Silbersack 1668b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, recvbuf_auto, 1678b615593SMarko Zec CTLFLAG_RW, tcp_do_autorcvbuf, 0, 1688b615593SMarko Zec "Enable automatic receive buffer sizing"); 1696741ecf5SAndre Oppermann 1708b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, recvbuf_inc, 1718b615593SMarko Zec CTLFLAG_RW, tcp_autorcvbuf_inc, 0, 1726489fe65SAndre Oppermann "Incrementor step size of automatic receive buffer"); 1736741ecf5SAndre Oppermann 1748b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, recvbuf_max, 1758b615593SMarko Zec CTLFLAG_RW, tcp_autorcvbuf_max, 0, 1768b615593SMarko Zec "Max size of automatic receive buffer"); 1776741ecf5SAndre Oppermann 178252ca428SRobert Watson int tcp_read_locking = 1; 179252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, read_locking, CTLFLAG_RW, 180252ca428SRobert Watson &tcp_read_locking, 0, "Enable read locking strategy"); 181252ca428SRobert Watson 182252ca428SRobert Watson int tcp_rlock_atfirst; 183252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, rlock_atfirst, CTLFLAG_RD, 184252ca428SRobert Watson &tcp_rlock_atfirst, 0, ""); 185252ca428SRobert Watson 186252ca428SRobert Watson int tcp_wlock_atfirst; 187252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcp_wlock_atfirst, CTLFLAG_RD, 188252ca428SRobert Watson &tcp_wlock_atfirst, 0, ""); 189252ca428SRobert Watson 190252ca428SRobert Watson int tcp_wlock_upgraded; 191252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, wlock_upgraded, CTLFLAG_RD, 192252ca428SRobert Watson &tcp_wlock_upgraded, 0, ""); 193252ca428SRobert Watson 194252ca428SRobert Watson int tcp_wlock_relocked; 195252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, wlock_relocked, CTLFLAG_RD, 196252ca428SRobert Watson &tcp_wlock_relocked, 0, ""); 197252ca428SRobert Watson 198252ca428SRobert Watson int tcp_wlock_looped; 199252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, wlock_looped, CTLFLAG_RD, 200252ca428SRobert Watson &tcp_wlock_looped, 0, ""); 201252ca428SRobert Watson 20244e33a07SMarko Zec #ifdef VIMAGE_GLOBALS 20315bd2b43SDavid Greenman struct inpcbhead tcb; 20415bd2b43SDavid Greenman struct inpcbinfo tcbinfo; 20544e33a07SMarko Zec #endif 20644e33a07SMarko Zec #define tcb6 tcb /* for KAME src sync over BSD*'s */ 207df8bae1dSRodney W. Grimes 2085a53ca16SPaul Saab static void tcp_dooptions(struct tcpopt *, u_char *, int, int); 209679d9708SAndre Oppermann static void tcp_do_segment(struct mbuf *, struct tcphdr *, 210252ca428SRobert Watson struct socket *, struct tcpcb *, int, int, uint8_t, 211252ca428SRobert Watson int); 212302ce8d6SAndre Oppermann static void tcp_dropwithreset(struct mbuf *, struct tcphdr *, 213302ce8d6SAndre Oppermann struct tcpcb *, int, int); 2144d77a549SAlfred Perlstein static void tcp_pulloutofband(struct socket *, 2154d77a549SAlfred Perlstein struct tcphdr *, struct mbuf *, int); 2164d77a549SAlfred Perlstein static void tcp_xmit_timer(struct tcpcb *, int); 217c068736aSJeffrey Hsu static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 218f2512ba1SRui Paulo static void inline 219f2512ba1SRui Paulo tcp_congestion_exp(struct tcpcb *); 220f2512ba1SRui Paulo 221f2512ba1SRui Paulo static void inline 222f2512ba1SRui Paulo tcp_congestion_exp(struct tcpcb *tp) 223f2512ba1SRui Paulo { 224f2512ba1SRui Paulo u_int win; 225f2512ba1SRui Paulo 226f2512ba1SRui Paulo win = min(tp->snd_wnd, tp->snd_cwnd) / 227f2512ba1SRui Paulo 2 / tp->t_maxseg; 228f2512ba1SRui Paulo if (win < 2) 229f2512ba1SRui Paulo win = 2; 230f2512ba1SRui Paulo tp->snd_ssthresh = win * tp->t_maxseg; 231f2512ba1SRui Paulo ENTER_FASTRECOVERY(tp); 232f2512ba1SRui Paulo tp->snd_recover = tp->snd_max; 233f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) 234f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_CWR; 235f2512ba1SRui Paulo } 2360312fbe9SPoul-Henning Kamp 237fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 238fb59c426SYoshinobu Inoue #ifdef INET6 239fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \ 240fb59c426SYoshinobu Inoue do { \ 241fb59c426SYoshinobu Inoue if ((tp) && (tp)->t_inpcb && \ 24297d8d152SAndre Oppermann ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \ 24397d8d152SAndre Oppermann nd6_nud_hint(NULL, NULL, 0); \ 244fb59c426SYoshinobu Inoue } while (0) 245fb59c426SYoshinobu Inoue #else 246fb59c426SYoshinobu Inoue #define ND6_HINT(tp) 247fb59c426SYoshinobu Inoue #endif 248df8bae1dSRodney W. Grimes 249df8bae1dSRodney W. Grimes /* 250262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 251262c1c1aSMatthew Dillon * - there is no delayed ack timer in progress and 252262c1c1aSMatthew Dillon * - our last ack wasn't a 0-sized window. We never want to delay 2533bfd6421SJonathan Lemon * the ack that opens up a 0-sized window and 2543bfd6421SJonathan Lemon * - delayed acks are enabled or 2553bfd6421SJonathan Lemon * - this is a half-synchronized T/TCP connection. 256d8c85a26SJonathan Lemon */ 257d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \ 258b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 259a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 260603724d3SBjoern A. Zeeb (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 261d8c85a26SJonathan Lemon 262df8bae1dSRodney W. Grimes /* 2638d573cc1SAndre Oppermann * TCP input handling is split into multiple parts: 2648d573cc1SAndre Oppermann * tcp6_input is a thin wrapper around tcp_input for the extended 2658d573cc1SAndre Oppermann * ip6_protox[] call format in ip6_input 2668d573cc1SAndre Oppermann * tcp_input handles primary segment validation, inpcb lookup and 2678d573cc1SAndre Oppermann * SYN processing on listen sockets 2688d573cc1SAndre Oppermann * tcp_do_segment processes the ACK and text of the segment for 2698d573cc1SAndre Oppermann * establishing, established and closing connections 270df8bae1dSRodney W. Grimes */ 271fb59c426SYoshinobu Inoue #ifdef INET6 272fb59c426SYoshinobu Inoue int 273ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto) 274fb59c426SYoshinobu Inoue { 2758b615593SMarko Zec INIT_VNET_INET6(curvnet); 276ad3f9ab3SAndre Oppermann struct mbuf *m = *mp; 27733841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 278fb59c426SYoshinobu Inoue 279fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 280fb59c426SYoshinobu Inoue 281fb59c426SYoshinobu Inoue /* 282fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 283fb59c426SYoshinobu Inoue * better place to put this in? 284fb59c426SYoshinobu Inoue */ 28533841545SHajimu UMEMOTO ia6 = ip6_getdstifaddr(m); 28633841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 287fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 288fb59c426SYoshinobu Inoue 289fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 290fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 291fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 292fb59c426SYoshinobu Inoue return IPPROTO_DONE; 293fb59c426SYoshinobu Inoue } 294fb59c426SYoshinobu Inoue 295f0ffb944SJulian Elischer tcp_input(m, *offp); 296fb59c426SYoshinobu Inoue return IPPROTO_DONE; 297fb59c426SYoshinobu Inoue } 298fb59c426SYoshinobu Inoue #endif 299fb59c426SYoshinobu Inoue 300df8bae1dSRodney W. Grimes void 301ad3f9ab3SAndre Oppermann tcp_input(struct mbuf *m, int off0) 302df8bae1dSRodney W. Grimes { 3038b615593SMarko Zec INIT_VNET_INET(curvnet); 3048b615593SMarko Zec #ifdef INET6 3058b615593SMarko Zec INIT_VNET_INET6(curvnet); 3068b615593SMarko Zec #endif 3078b615593SMarko Zec #ifdef IPSEC 3088b615593SMarko Zec INIT_VNET_IPSEC(curvnet); 3098b615593SMarko Zec #endif 310ad3f9ab3SAndre Oppermann struct tcphdr *th; 311ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 312ad3f9ab3SAndre Oppermann struct ipovly *ipov; 313ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 314302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 315302ce8d6SAndre Oppermann struct socket *so = NULL; 316e79adb8eSGarrett Wollman u_char *optp = NULL; 31726f9a767SRodney W. Grimes int optlen = 0; 318a0194ef1SBruce M Simpson int len, tlen, off; 319fb59c426SYoshinobu Inoue int drop_hdrlen; 320ad3f9ab3SAndre Oppermann int thflags; 321302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 322f2512ba1SRui Paulo uint8_t iptos; 3239b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 3249b932e9eSAndre Oppermann struct m_tag *fwd_tag; 3259b932e9eSAndre Oppermann #endif 326c068736aSJeffrey Hsu #ifdef INET6 327302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 328c068736aSJeffrey Hsu int isipv6; 329c068736aSJeffrey Hsu #else 330a160e630SAndre Oppermann const void *ip6 = NULL; 331c068736aSJeffrey Hsu const int isipv6 = 0; 332c068736aSJeffrey Hsu #endif 333302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 334a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 335252ca428SRobert Watson int ti_locked; 336252ca428SRobert Watson #define TI_UNLOCKED 1 337252ca428SRobert Watson #define TI_RLOCKED 2 338252ca428SRobert Watson #define TI_WLOCKED 3 339f76fcf6dSJeffrey Hsu 340610ee2f9SDavid Greenman #ifdef TCPDEBUG 341c068736aSJeffrey Hsu /* 342c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 343c068736aSJeffrey Hsu * now IPv6. 344c068736aSJeffrey Hsu */ 34514739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 346410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 347610ee2f9SDavid Greenman short ostate = 0; 348610ee2f9SDavid Greenman #endif 349c068736aSJeffrey Hsu 350fb59c426SYoshinobu Inoue #ifdef INET6 351fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 352fb59c426SYoshinobu Inoue #endif 353a0292f23SGarrett Wollman 354302ce8d6SAndre Oppermann to.to_flags = 0; 355603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvtotal++; 356fb59c426SYoshinobu Inoue 357fb59c426SYoshinobu Inoue if (isipv6) { 35804d3a452SHajimu UMEMOTO #ifdef INET6 3598d573cc1SAndre Oppermann /* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */ 360fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 361fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 362fb59c426SYoshinobu Inoue if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 363603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbadsum++; 364fb59c426SYoshinobu Inoue goto drop; 365fb59c426SYoshinobu Inoue } 366fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 36733841545SHajimu UMEMOTO 36833841545SHajimu UMEMOTO /* 36933841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 37033841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 37133841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 37233841545SHajimu UMEMOTO * 37333841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 37433841545SHajimu UMEMOTO * already dropped in ip6_input. 37533841545SHajimu UMEMOTO */ 37633841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 37733841545SHajimu UMEMOTO /* XXX stat */ 37833841545SHajimu UMEMOTO goto drop; 37933841545SHajimu UMEMOTO } 38004d3a452SHajimu UMEMOTO #else 3818d573cc1SAndre Oppermann th = NULL; /* XXX: Avoid compiler warning. */ 38204d3a452SHajimu UMEMOTO #endif 383c068736aSJeffrey Hsu } else { 384df8bae1dSRodney W. Grimes /* 385df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 386df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 387df8bae1dSRodney W. Grimes */ 388fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 389df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 390fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 391fb59c426SYoshinobu Inoue } 392df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 3934dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 3944dfdffe9SAndre Oppermann == NULL) { 395603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvshort++; 396df8bae1dSRodney W. Grimes return; 397df8bae1dSRodney W. Grimes } 398df8bae1dSRodney W. Grimes } 399fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 400fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 401db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 402db4f9cc7SJonathan Lemon tlen = ip->ip_len; 403df8bae1dSRodney W. Grimes 404db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 405db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 406db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 407db4f9cc7SJonathan Lemon else 408db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 409c068736aSJeffrey Hsu ip->ip_dst.s_addr, 410c068736aSJeffrey Hsu htonl(m->m_pkthdr.csum_data + 411c068736aSJeffrey Hsu ip->ip_len + 412c068736aSJeffrey Hsu IPPROTO_TCP)); 413db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 4143c653157SHartmut Brandt #ifdef TCPDEBUG 4153c653157SHartmut Brandt ipov->ih_len = (u_short)tlen; 4163c653157SHartmut Brandt ipov->ih_len = htons(ipov->ih_len); 4173c653157SHartmut Brandt #endif 418db4f9cc7SJonathan Lemon } else { 419df8bae1dSRodney W. Grimes /* 420df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 421df8bae1dSRodney W. Grimes */ 422df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 423fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 424fb59c426SYoshinobu Inoue ipov->ih_len = (u_short)tlen; 425fd8e4ebcSMike Barcroft ipov->ih_len = htons(ipov->ih_len); 426fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 427db4f9cc7SJonathan Lemon } 428fb59c426SYoshinobu Inoue if (th->th_sum) { 429603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbadsum++; 430df8bae1dSRodney W. Grimes goto drop; 431df8bae1dSRodney W. Grimes } 432fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 433fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 434fb59c426SYoshinobu Inoue } 435df8bae1dSRodney W. Grimes 436f2512ba1SRui Paulo #ifdef INET6 437f2512ba1SRui Paulo if (isipv6) 438f2512ba1SRui Paulo iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 439f2512ba1SRui Paulo else 440f2512ba1SRui Paulo #endif 441f2512ba1SRui Paulo iptos = ip->ip_tos; 442f2512ba1SRui Paulo 443df8bae1dSRodney W. Grimes /* 444df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 445df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 446df8bae1dSRodney W. Grimes */ 447fb59c426SYoshinobu Inoue off = th->th_off << 2; 448df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 449603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbadoff++; 450df8bae1dSRodney W. Grimes goto drop; 451df8bae1dSRodney W. Grimes } 452fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 453df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 454fb59c426SYoshinobu Inoue if (isipv6) { 45504d3a452SHajimu UMEMOTO #ifdef INET6 456fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 457fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 458fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 45904d3a452SHajimu UMEMOTO #endif 460c068736aSJeffrey Hsu } else { 461df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 462c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 4634dfdffe9SAndre Oppermann == NULL) { 464603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvshort++; 465df8bae1dSRodney W. Grimes return; 466df8bae1dSRodney W. Grimes } 467fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 468fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 469fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 470fb59c426SYoshinobu Inoue } 471df8bae1dSRodney W. Grimes } 472df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 473fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 474df8bae1dSRodney W. Grimes } 475fb59c426SYoshinobu Inoue thflags = th->th_flags; 476df8bae1dSRodney W. Grimes 477e46cd3d4SDag-Erling Smørgrav /* 478df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 479df8bae1dSRodney W. Grimes */ 480fd8e4ebcSMike Barcroft th->th_seq = ntohl(th->th_seq); 481fd8e4ebcSMike Barcroft th->th_ack = ntohl(th->th_ack); 482fd8e4ebcSMike Barcroft th->th_win = ntohs(th->th_win); 483fd8e4ebcSMike Barcroft th->th_urp = ntohs(th->th_urp); 484df8bae1dSRodney W. Grimes 485df8bae1dSRodney W. Grimes /* 486794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 487755c1f07SAndras Olah */ 488fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 489755c1f07SAndras Olah 490755c1f07SAndras Olah /* 491252ca428SRobert Watson * Locate pcb for segment, which requires a lock on tcbinfo. 492d15fb965SRobert Watson * Optimisticaly acquire a global read lock rather than a write lock 493d15fb965SRobert Watson * unless header flags necessarily imply a state change. There are 494d15fb965SRobert Watson * two cases where we might discover later we need a write lock 495d15fb965SRobert Watson * despite the flags: ACKs moving a connection out of the syncache, 496d15fb965SRobert Watson * and ACKs for a connection in TIMEWAIT. 497df8bae1dSRodney W. Grimes */ 498252ca428SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 499252ca428SRobert Watson tcp_read_locking == 0) { 500603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 501252ca428SRobert Watson ti_locked = TI_WLOCKED; 502252ca428SRobert Watson tcp_wlock_atfirst++; 503252ca428SRobert Watson } else { 504252ca428SRobert Watson INP_INFO_RLOCK(&V_tcbinfo); 505252ca428SRobert Watson ti_locked = TI_RLOCKED; 506252ca428SRobert Watson tcp_rlock_atfirst++; 507252ca428SRobert Watson } 508252ca428SRobert Watson 509df8bae1dSRodney W. Grimes findpcb: 510252ca428SRobert Watson #ifdef INVARIANTS 511252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 512252ca428SRobert Watson INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 513252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 514603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 515252ca428SRobert Watson else 516252ca428SRobert Watson panic("%s: findpcb ti_locked %d\n", __func__, ti_locked); 517252ca428SRobert Watson #endif 518252ca428SRobert Watson 5199b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 5208d573cc1SAndre Oppermann /* 5218d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 5228d573cc1SAndre Oppermann */ 5239b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 5249b932e9eSAndre Oppermann 5259b932e9eSAndre Oppermann if (fwd_tag != NULL && isipv6 == 0) { /* IPv6 support is not yet */ 5269b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 5279b932e9eSAndre Oppermann 5289b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 529f9e354dfSJulian Elischer /* 5302b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 531f9e354dfSJulian Elischer * already got one like this? 532f9e354dfSJulian Elischer */ 533603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_tcbinfo, 5349b932e9eSAndre Oppermann ip->ip_src, th->th_sport, 535c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 536c068736aSJeffrey Hsu 0, m->m_pkthdr.rcvif); 537f9e354dfSJulian Elischer if (!inp) { 5389b932e9eSAndre Oppermann /* It's new. Try to find the ambushing socket. */ 539603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_tcbinfo, 540fb59c426SYoshinobu Inoue ip->ip_src, th->th_sport, 5412b25acc1SLuigi Rizzo next_hop->sin_addr, 542c068736aSJeffrey Hsu next_hop->sin_port ? 543c068736aSJeffrey Hsu ntohs(next_hop->sin_port) : 544c068736aSJeffrey Hsu th->th_dport, 545421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 546421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 547f9e354dfSJulian Elischer } 5489b932e9eSAndre Oppermann /* Remove the tag from the packet. We don't need it anymore. */ 5499b932e9eSAndre Oppermann m_tag_delete(m, fwd_tag); 550b10fbdeaSAndre Oppermann } else 5519b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 552b10fbdeaSAndre Oppermann { 55304d3a452SHajimu UMEMOTO if (isipv6) { 55404d3a452SHajimu UMEMOTO #ifdef INET6 555603724d3SBjoern A. Zeeb inp = in6_pcblookup_hash(&V_tcbinfo, 556c068736aSJeffrey Hsu &ip6->ip6_src, th->th_sport, 557c068736aSJeffrey Hsu &ip6->ip6_dst, th->th_dport, 558421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 559421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 56004d3a452SHajimu UMEMOTO #endif 56104d3a452SHajimu UMEMOTO } else 562603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_tcbinfo, 563c068736aSJeffrey Hsu ip->ip_src, th->th_sport, 564c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 565421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 566421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 567fb59c426SYoshinobu Inoue } 568f9e354dfSJulian Elischer 569df8bae1dSRodney W. Grimes /* 570574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 571574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 5728b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 573df8bae1dSRodney W. Grimes */ 574816a3d83SPoul-Henning Kamp if (inp == NULL) { 575574b6964SAndre Oppermann /* 576574b6964SAndre Oppermann * Log communication attempts to ports that are not 577574b6964SAndre Oppermann * in use. 578574b6964SAndre Oppermann */ 579574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 580574b6964SAndre Oppermann tcp_log_in_vain == 2) { 5819fb5d4c0SPeter Wemm if ((s = tcp_log_addrs(NULL, th, (void *)ip, ip6))) 582df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 583df541e5fSAndre Oppermann "to closed port\n", s, __func__); 5842e4e1b4cSGeoff Rehmet } 585574b6964SAndre Oppermann /* 586574b6964SAndre Oppermann * When blackholing do not respond with a RST but 587574b6964SAndre Oppermann * completely ignore the segment and drop it. 588574b6964SAndre Oppermann */ 589603724d3SBjoern A. Zeeb if ((V_blackhole == 1 && (thflags & TH_SYN)) || 590603724d3SBjoern A. Zeeb V_blackhole == 2) 5911929eae1SAndre Oppermann goto dropunlock; 592574b6964SAndre Oppermann 593a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 594a57815efSBosko Milekic goto dropwithreset; 595816a3d83SPoul-Henning Kamp } 5968501a69cSRobert Watson INP_WLOCK(inp); 59780cb9f21SKip Macy if (!(inp->inp_flags & INP_HW_FLOWID) 59880cb9f21SKip Macy && (m->m_flags & M_FLOWID) 59980cb9f21SKip Macy && ((inp->inp_socket == NULL) 60080cb9f21SKip Macy || !(inp->inp_socket->so_options & SO_ACCEPTCONN))) { 60180cb9f21SKip Macy inp->inp_flags |= INP_HW_FLOWID; 60280cb9f21SKip Macy inp->inp_flags &= ~INP_SW_FLOWID; 60380cb9f21SKip Macy inp->inp_flowid = m->m_pkthdr.flowid; 60480cb9f21SKip Macy } 605a2589465SKen Smith #ifdef IPSEC 606a2589465SKen Smith #ifdef INET6 607a2589465SKen Smith if (isipv6 && ipsec6_in_reject(m, inp)) { 608603724d3SBjoern A. Zeeb V_ipsec6stat.in_polvio++; 609a2589465SKen Smith goto dropunlock; 610a2589465SKen Smith } else 611a2589465SKen Smith #endif /* INET6 */ 612a2589465SKen Smith if (ipsec4_in_reject(m, inp) != 0) { 613603724d3SBjoern A. Zeeb V_ipsec4stat.in_polvio++; 614a2589465SKen Smith goto dropunlock; 615a2589465SKen Smith } 616a2589465SKen Smith #endif /* IPSEC */ 617a2589465SKen Smith 6188d573cc1SAndre Oppermann /* 6198d573cc1SAndre Oppermann * Check the minimum TTL for socket. 6208d573cc1SAndre Oppermann */ 62134f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 62234f83c52SGeorge V. Neville-Neil #ifdef INET6 62334f83c52SGeorge V. Neville-Neil if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim) 624302ce8d6SAndre Oppermann goto dropunlock; 62502707462SAndre Oppermann else 62634f83c52SGeorge V. Neville-Neil #endif 62734f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 628302ce8d6SAndre Oppermann goto dropunlock; 62934f83c52SGeorge V. Neville-Neil } 630936cd18dSAndre Oppermann 631340c35deSJonathan Lemon /* 632252ca428SRobert Watson * A previous connection in TIMEWAIT state is supposed to catch stray 633252ca428SRobert Watson * or duplicate segments arriving late. If this segment was a 634252ca428SRobert Watson * legitimate new connection attempt the old INPCB gets removed and 635252ca428SRobert Watson * we can try again to find a listening socket. 636252ca428SRobert Watson * 637252ca428SRobert Watson * At this point, due to earlier optimism, we may hold a read lock on 638252ca428SRobert Watson * the inpcbinfo, rather than a write lock. If so, we need to 639252ca428SRobert Watson * upgrade, or if that fails, acquire a reference on the inpcb, drop 640252ca428SRobert Watson * all locks, acquire a global write lock, and then re-acquire the 641252ca428SRobert Watson * inpcb lock. We may at that point discover that another thread has 642252ca428SRobert Watson * tried to free the inpcb, in which case we need to loop back and 643252ca428SRobert Watson * try to find a new inpcb to deliver to. 644340c35deSJonathan Lemon */ 645ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 646252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 647252ca428SRobert Watson ("%s: INP_TIMEWAIT ti_locked %d", __func__, ti_locked)); 648252ca428SRobert Watson 649252ca428SRobert Watson if (ti_locked == TI_RLOCKED) { 650252ca428SRobert Watson if (rw_try_upgrade(&V_tcbinfo.ipi_lock) == 0) { 651252ca428SRobert Watson in_pcbref(inp); 652252ca428SRobert Watson INP_WUNLOCK(inp); 653252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 654252ca428SRobert Watson INP_INFO_WLOCK(&V_tcbinfo); 655252ca428SRobert Watson ti_locked = TI_WLOCKED; 656252ca428SRobert Watson INP_WLOCK(inp); 657252ca428SRobert Watson if (in_pcbrele(inp)) { 658252ca428SRobert Watson tcp_wlock_looped++; 659252ca428SRobert Watson inp = NULL; 660252ca428SRobert Watson goto findpcb; 661252ca428SRobert Watson } 662252ca428SRobert Watson tcp_wlock_relocked++; 663252ca428SRobert Watson } else { 664252ca428SRobert Watson ti_locked = TI_WLOCKED; 665252ca428SRobert Watson tcp_wlock_upgraded++; 666252ca428SRobert Watson } 667252ca428SRobert Watson } 668252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 669252ca428SRobert Watson 670340c35deSJonathan Lemon if (thflags & TH_SYN) 671f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 6728d573cc1SAndre Oppermann /* 6738d573cc1SAndre Oppermann * NB: tcp_twcheck unlocks the INP and frees the mbuf. 6748d573cc1SAndre Oppermann */ 6752104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 676340c35deSJonathan Lemon goto findpcb; 677603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 678340c35deSJonathan Lemon return; 679340c35deSJonathan Lemon } 680794235b7SAndre Oppermann /* 681794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 682794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 683794235b7SAndre Oppermann * segment and send an appropriate response. 684794235b7SAndre Oppermann */ 685df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 686a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 687a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 688a57815efSBosko Milekic goto dropwithreset; 68909f81a46SBosko Milekic } 690df8bae1dSRodney W. Grimes 691252ca428SRobert Watson /* 692252ca428SRobert Watson * We've identified a valid inpcb, but it could be that we need an 693252ca428SRobert Watson * inpcbinfo write lock and have only a read lock. In this case, 694252ca428SRobert Watson * attempt to upgrade/relock using the same strategy as the TIMEWAIT 695252ca428SRobert Watson * case above. 696252ca428SRobert Watson */ 697252ca428SRobert Watson if (tp->t_state != TCPS_ESTABLISHED || 698252ca428SRobert Watson (thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 699252ca428SRobert Watson tcp_read_locking == 0) { 700252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 701252ca428SRobert Watson ("%s: upgrade check ti_locked %d", __func__, ti_locked)); 702252ca428SRobert Watson 703252ca428SRobert Watson if (ti_locked == TI_RLOCKED) { 704252ca428SRobert Watson if (rw_try_upgrade(&V_tcbinfo.ipi_lock) == 0) { 705252ca428SRobert Watson in_pcbref(inp); 706252ca428SRobert Watson INP_WUNLOCK(inp); 707252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 708252ca428SRobert Watson INP_INFO_WLOCK(&V_tcbinfo); 709252ca428SRobert Watson ti_locked = TI_WLOCKED; 710252ca428SRobert Watson INP_WLOCK(inp); 711252ca428SRobert Watson if (in_pcbrele(inp)) { 712252ca428SRobert Watson tcp_wlock_looped++; 713252ca428SRobert Watson inp = NULL; 714252ca428SRobert Watson goto findpcb; 715252ca428SRobert Watson } 716252ca428SRobert Watson tcp_wlock_relocked++; 717252ca428SRobert Watson } else { 718252ca428SRobert Watson ti_locked = TI_WLOCKED; 719252ca428SRobert Watson tcp_wlock_upgraded++; 720252ca428SRobert Watson } 721252ca428SRobert Watson } 722252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 723252ca428SRobert Watson } 724252ca428SRobert Watson 725c488362eSRobert Watson #ifdef MAC 7268501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 72730d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 728302ce8d6SAndre Oppermann goto dropunlock; 729c488362eSRobert Watson #endif 730a557af22SRobert Watson so = inp->inp_socket; 731302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 732610ee2f9SDavid Greenman #ifdef TCPDEBUG 733df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 734df8bae1dSRodney W. Grimes ostate = tp->t_state; 735d30d90dcSMaxim Konovalov if (isipv6) { 73610fe523eSMaxim Konovalov #ifdef INET6 737540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 738d30d90dcSMaxim Konovalov #endif 739d30d90dcSMaxim Konovalov } else 740540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 741fb59c426SYoshinobu Inoue tcp_savetcp = *th; 742df8bae1dSRodney W. Grimes } 743610ee2f9SDavid Greenman #endif 744db33b3e6SAndre Oppermann /* 745db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 746db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 747db33b3e6SAndre Oppermann * attempt or the completion of a previous one. 748db33b3e6SAndre Oppermann */ 749540e8b7eSJeffrey Hsu if (so->so_options & SO_ACCEPTCONN) { 750540e8b7eSJeffrey Hsu struct in_conninfo inc; 751540e8b7eSJeffrey Hsu 7527824d002SAndre Oppermann KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but " 7537824d002SAndre Oppermann "tp not listening", __func__)); 7547824d002SAndre Oppermann 7558bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 756302ce8d6SAndre Oppermann #ifdef INET6 757be2ac88cSJonathan Lemon if (isipv6) { 758dcdb4371SBjoern A. Zeeb inc.inc_flags |= INC_ISIPV6; 759be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 760be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 761302ce8d6SAndre Oppermann } else 762302ce8d6SAndre Oppermann #endif 763302ce8d6SAndre Oppermann { 764be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 765be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 766be2ac88cSJonathan Lemon } 767be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 768be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 769be2ac88cSJonathan Lemon 770fb59c426SYoshinobu Inoue /* 7718d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 7728d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 7738d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 774fb59c426SYoshinobu Inoue */ 775be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 776bf6d304aSAndre Oppermann /* 777bf6d304aSAndre Oppermann * Parse the TCP options here because 778bf6d304aSAndre Oppermann * syncookies need access to the reflected 779bf6d304aSAndre Oppermann * timestamp. 780bf6d304aSAndre Oppermann */ 781bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 7829fa198beSAndre Oppermann /* 7839fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 7849fa198beSAndre Oppermann * inp and tcpinfo locks. 7859fa198beSAndre Oppermann */ 786bf6d304aSAndre Oppermann if (!syncache_expand(&inc, &to, th, &so, m)) { 7874195b4afSPaul Traina /* 788e207f800SAndre Oppermann * No syncache entry or ACK was not 789be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 7908d573cc1SAndre Oppermann * NB: syncache did its own logging 7918d573cc1SAndre Oppermann * of the failure cause. 7924195b4afSPaul Traina */ 793be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 794be2ac88cSJonathan Lemon goto dropwithreset; 795be2ac88cSJonathan Lemon } 796f76fcf6dSJeffrey Hsu if (so == NULL) { 797be2ac88cSJonathan Lemon /* 798e207f800SAndre Oppermann * We completed the 3-way handshake 799e207f800SAndre Oppermann * but could not allocate a socket 800e207f800SAndre Oppermann * either due to memory shortage, 801e207f800SAndre Oppermann * listen queue length limits or 802a160e630SAndre Oppermann * global socket limits. Send RST 803a160e630SAndre Oppermann * or wait and have the remote end 804a160e630SAndre Oppermann * retransmit the ACK for another 805a160e630SAndre Oppermann * try. 806be2ac88cSJonathan Lemon */ 807a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 808a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 8098d573cc1SAndre Oppermann "Socket allocation failed due to " 8108d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 811ac957cd2SJulian Elischer s, __func__, 812ac957cd2SJulian Elischer V_tcp_sc_rst_sock_fail ? 813ac957cd2SJulian Elischer "sending RST" : "try again"); 814603724d3SBjoern A. Zeeb if (V_tcp_sc_rst_sock_fail) { 815e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 816e207f800SAndre Oppermann goto dropwithreset; 817a160e630SAndre Oppermann } else 818a160e630SAndre Oppermann goto dropunlock; 819f76fcf6dSJeffrey Hsu } 820be2ac88cSJonathan Lemon /* 821be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 8228d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 8238d573cc1SAndre Oppermann * created socket and update the tp variable. 824be2ac88cSJonathan Lemon */ 8258501a69cSRobert Watson INP_WUNLOCK(inp); /* listen socket */ 826be2ac88cSJonathan Lemon inp = sotoinpcb(so); 8278501a69cSRobert Watson INP_WLOCK(inp); /* new connection */ 828be2ac88cSJonathan Lemon tp = intotcpcb(inp); 8298d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 8308d573cc1SAndre Oppermann ("%s: ", __func__)); 831be2ac88cSJonathan Lemon /* 832302ce8d6SAndre Oppermann * Process the segment and the data it 833302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 834302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 835302ce8d6SAndre Oppermann */ 836f2512ba1SRui Paulo tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 837252ca428SRobert Watson iptos, ti_locked); 838603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 839302ce8d6SAndre Oppermann return; 840be2ac88cSJonathan Lemon } 841a160e630SAndre Oppermann /* 8428d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 8438d573cc1SAndre Oppermann * 844a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 845a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 846a160e630SAndre Oppermann * retransmits. 84719bc77c5SAndre Oppermann * 84819bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 84919bc77c5SAndre Oppermann * causes. 850a160e630SAndre Oppermann */ 851a160e630SAndre Oppermann if (thflags & TH_RST) { 85219bc77c5SAndre Oppermann syncache_chkrst(&inc, th); 853a160e630SAndre Oppermann goto dropunlock; 854a160e630SAndre Oppermann } 855a160e630SAndre Oppermann /* 856a160e630SAndre Oppermann * We can't do anything without SYN. 857a160e630SAndre Oppermann */ 858a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 859a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 860a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 861773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 8628d573cc1SAndre Oppermann s, __func__); 863603724d3SBjoern A. Zeeb V_tcpstat.tcps_badsyn++; 864a160e630SAndre Oppermann goto dropunlock; 865a160e630SAndre Oppermann } 866a160e630SAndre Oppermann /* 867a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 868a160e630SAndre Oppermann */ 869fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 870a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 871a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 8728d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 8738d573cc1SAndre Oppermann s, __func__); 874a160e630SAndre Oppermann syncache_badack(&inc); /* XXX: Not needed! */ 875603724d3SBjoern A. Zeeb V_tcpstat.tcps_badsyn++; 876a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 877a57815efSBosko Milekic goto dropwithreset; 8784195b4afSPaul Traina } 879a160e630SAndre Oppermann /* 880a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 881a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 882a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 883a160e630SAndre Oppermann * TCP/IP stack. 884a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 885a160e630SAndre Oppermann * and is constantly refining its stack detection 886a160e630SAndre Oppermann * strategies. 8878d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 888a160e630SAndre Oppermann * and was used by RFC1644. 889a160e630SAndre Oppermann */ 890603724d3SBjoern A. Zeeb if ((thflags & TH_FIN) && V_drop_synfin) { 891a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 892a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 893773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 8948d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 895603724d3SBjoern A. Zeeb V_tcpstat.tcps_badsyn++; 896302ce8d6SAndre Oppermann goto dropunlock; 897ebb0cbeaSPaul Traina } 898be2ac88cSJonathan Lemon /* 899be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 900a160e630SAndre Oppermann * 901a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 902a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 903a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 904be2ac88cSJonathan Lemon */ 905a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 906a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 907a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 908a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 90933841545SHajimu UMEMOTO #ifdef INET6 91033841545SHajimu UMEMOTO /* 91133841545SHajimu UMEMOTO * If deprecated address is forbidden, 91233841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 91333841545SHajimu UMEMOTO * address to prevent any new inbound connection from 91433841545SHajimu UMEMOTO * getting established. 91533841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 91633841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 91733841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 91833841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 91933841545SHajimu UMEMOTO * for the exchange. 92033841545SHajimu UMEMOTO * 92133841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 92233841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 92333841545SHajimu UMEMOTO * SYN in this case. 92433841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 92533841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 92633841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 92733841545SHajimu UMEMOTO * used" 92833841545SHajimu UMEMOTO * 2. use of it with new communication: 92933841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 93033841545SHajimu UMEMOTO * with sufficient scope is available" 93133841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 93233841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 93333841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 93433841545SHajimu UMEMOTO * 93533841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 93633841545SHajimu UMEMOTO * multiple description text for deprecated address 93733841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 93833841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 93933841545SHajimu UMEMOTO */ 940603724d3SBjoern A. Zeeb if (isipv6 && !V_ip6_use_deprecated) { 94133841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 94233841545SHajimu UMEMOTO 94333841545SHajimu UMEMOTO if ((ia6 = ip6_getdstifaddr(m)) && 94433841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 945a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 946a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 9478d573cc1SAndre Oppermann "Connection attempt to deprecated " 9488d573cc1SAndre Oppermann "IPv6 address rejected\n", 949a160e630SAndre Oppermann s, __func__); 95033841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 95133841545SHajimu UMEMOTO goto dropwithreset; 95233841545SHajimu UMEMOTO } 95333841545SHajimu UMEMOTO } 95433841545SHajimu UMEMOTO #endif 95565f28919SJesper Skriver /* 956db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 9578d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 958db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 9598d573cc1SAndre Oppermann * If it is from this socket it must be forged. 9608d573cc1SAndre Oppermann * Don't respond if the source or destination is a 9618d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 96293ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 96393ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 96493ec91baSCrist J. Clark * in_broadcast() to find them. 965be2ac88cSJonathan Lemon */ 9668d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 9678d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 9688d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 9698d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 970773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 971302ce8d6SAndre Oppermann goto dropunlock; 9728d573cc1SAndre Oppermann } 973be2ac88cSJonathan Lemon if (isipv6) { 974db33b3e6SAndre Oppermann #ifdef INET6 975db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 976a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 977a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 978a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 9798d573cc1SAndre Oppermann "Connection attempt to/from self " 980773673c1SAndre Oppermann "ignored\n", s, __func__); 981302ce8d6SAndre Oppermann goto dropunlock; 982a160e630SAndre Oppermann } 983be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 984a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 985a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 986a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 9878d573cc1SAndre Oppermann "Connection attempt from/to multicast " 988773673c1SAndre Oppermann "address ignored\n", s, __func__); 989302ce8d6SAndre Oppermann goto dropunlock; 990a160e630SAndre Oppermann } 991db33b3e6SAndre Oppermann #endif 992c068736aSJeffrey Hsu } else { 993db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 994a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 995a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 996a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 9978d573cc1SAndre Oppermann "Connection attempt from/to self " 998773673c1SAndre Oppermann "ignored\n", s, __func__); 999302ce8d6SAndre Oppermann goto dropunlock; 1000a160e630SAndre Oppermann } 1001be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1002be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 10032ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 1004a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 1005a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1006a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 10078d573cc1SAndre Oppermann "Connection attempt from/to broad- " 1008773673c1SAndre Oppermann "or multicast address ignored\n", 1009a160e630SAndre Oppermann s, __func__); 1010302ce8d6SAndre Oppermann goto dropunlock; 1011c068736aSJeffrey Hsu } 1012a160e630SAndre Oppermann } 1013be2ac88cSJonathan Lemon /* 1014db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 1015db33b3e6SAndre Oppermann * for syncache. 1016be2ac88cSJonathan Lemon */ 10173c653157SHartmut Brandt #ifdef TCPDEBUG 10183c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 10193c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 10203c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 10213c653157SHartmut Brandt #endif 1022f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 10234d6e7130SAndre Oppermann syncache_add(&inc, &to, th, inp, &so, m); 1024be2ac88cSJonathan Lemon /* 10254d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 1026a160e630SAndre Oppermann * Everything already unlocked by syncache_add(). 1027be2ac88cSJonathan Lemon */ 1028603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1029be2ac88cSJonathan Lemon return; 1030f76fcf6dSJeffrey Hsu } 1031db33b3e6SAndre Oppermann 1032302ce8d6SAndre Oppermann /* 10338d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 10348d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 10358d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 1036302ce8d6SAndre Oppermann */ 1037252ca428SRobert Watson tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked); 1038603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1039302ce8d6SAndre Oppermann return; 1040be2ac88cSJonathan Lemon 1041302ce8d6SAndre Oppermann dropwithreset: 1042252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1043252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 1044252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1045dd8ac7f9SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1046252ca428SRobert Watson else 1047252ca428SRobert Watson panic("%s: dropwithreset ti_locked %d", __func__, ti_locked); 1048252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1049014ea782SRobert Watson 1050014ea782SRobert Watson if (inp != NULL) { 1051302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 1052014ea782SRobert Watson INP_WUNLOCK(inp); 1053dd8ac7f9SRobert Watson } else 1054014ea782SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1055302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 1056014ea782SRobert Watson goto drop; 1057014ea782SRobert Watson 1058302ce8d6SAndre Oppermann dropunlock: 1059252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1060252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 1061252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1062252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1063252ca428SRobert Watson else 1064252ca428SRobert Watson panic("%s: dropunlock ti_locked %d", __func__, ti_locked); 1065252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1066252ca428SRobert Watson 10679fa198beSAndre Oppermann if (inp != NULL) 10688501a69cSRobert Watson INP_WUNLOCK(inp); 1069014ea782SRobert Watson 1070302ce8d6SAndre Oppermann drop: 1071603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1072a160e630SAndre Oppermann if (s != NULL) 1073a160e630SAndre Oppermann free(s, M_TCPLOG); 1074302ce8d6SAndre Oppermann if (m != NULL) 1075302ce8d6SAndre Oppermann m_freem(m); 1076302ce8d6SAndre Oppermann } 1077302ce8d6SAndre Oppermann 1078679d9708SAndre Oppermann static void 1079302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 1080252ca428SRobert Watson struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos, 1081252ca428SRobert Watson int ti_locked) 1082302ce8d6SAndre Oppermann { 10838b615593SMarko Zec INIT_VNET_INET(tp->t_vnet); 1084302ce8d6SAndre Oppermann int thflags, acked, ourfinisacked, needoutput = 0; 1085302ce8d6SAndre Oppermann int rstreason, todrop, win; 1086302ce8d6SAndre Oppermann u_long tiwin; 1087302ce8d6SAndre Oppermann struct tcpopt to; 1088302ce8d6SAndre Oppermann 108914739780SMaxim Konovalov #ifdef TCPDEBUG 109014739780SMaxim Konovalov /* 109114739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 109214739780SMaxim Konovalov * now IPv6. 109314739780SMaxim Konovalov */ 109414739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 109514739780SMaxim Konovalov struct tcphdr tcp_savetcp; 109614739780SMaxim Konovalov short ostate = 0; 109714739780SMaxim Konovalov #endif 1098302ce8d6SAndre Oppermann thflags = th->th_flags; 1099302ce8d6SAndre Oppermann 1100252ca428SRobert Watson /* 1101252ca428SRobert Watson * If this is either a state-changing packet or current state isn't 1102252ca428SRobert Watson * established, we require a write lock on tcbinfo. Otherwise, we 1103252ca428SRobert Watson * allow either a read lock or a write lock, as we may have acquired 1104252ca428SRobert Watson * a write lock due to a race. 1105252ca428SRobert Watson * 1106d15fb965SRobert Watson * Require a global write lock for SYN/FIN/RST segments or 1107252ca428SRobert Watson * non-established connections; otherwise accept either a read or 1108252ca428SRobert Watson * write lock, as we may have conservatively acquired a write lock in 1109252ca428SRobert Watson * certain cases in tcp_input() (is this still true?). Currently we 1110252ca428SRobert Watson * will never enter with no lock, so we try to drop it quickly in the 1111252ca428SRobert Watson * common pure ack/pure data cases. 1112252ca428SRobert Watson */ 1113252ca428SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 1114252ca428SRobert Watson tp->t_state != TCPS_ESTABLISHED) { 1115252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s ti_locked %d for " 1116252ca428SRobert Watson "SYN/FIN/RST/!EST", __func__, ti_locked)); 1117603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1118252ca428SRobert Watson } else { 1119252ca428SRobert Watson #ifdef INVARIANTS 1120252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1121252ca428SRobert Watson INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1122252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1123252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1124252ca428SRobert Watson else 1125252ca428SRobert Watson panic("%s: ti_locked %d for EST", __func__, 1126252ca428SRobert Watson ti_locked); 1127252ca428SRobert Watson #endif 1128252ca428SRobert Watson } 11298501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1130679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1131679d9708SAndre Oppermann __func__)); 1132679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1133679d9708SAndre Oppermann __func__)); 1134df8bae1dSRodney W. Grimes 1135df8bae1dSRodney W. Grimes /* 1136df8bae1dSRodney W. Grimes * Segment received on connection. 1137df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1138e8949f74SAndre Oppermann * XXX: This should be done after segment 1139e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 1140df8bae1dSRodney W. Grimes */ 11419b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 11427ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 1143b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 1144df8bae1dSRodney W. Grimes 1145df8bae1dSRodney W. Grimes /* 1146464fcfbcSAndre Oppermann * Unscale the window into a 32-bit value. 1147e8949f74SAndre Oppermann * For the SYN_SENT state the scale is zero. 1148464fcfbcSAndre Oppermann */ 1149464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 1150464fcfbcSAndre Oppermann 1151464fcfbcSAndre Oppermann /* 1152f2512ba1SRui Paulo * TCP ECN processing. 1153f2512ba1SRui Paulo */ 1154f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) { 1155f2512ba1SRui Paulo switch (iptos & IPTOS_ECN_MASK) { 1156f2512ba1SRui Paulo case IPTOS_ECN_CE: 1157f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_ECE; 1158603724d3SBjoern A. Zeeb V_tcpstat.tcps_ecn_ce++; 1159f2512ba1SRui Paulo break; 1160f2512ba1SRui Paulo case IPTOS_ECN_ECT0: 1161603724d3SBjoern A. Zeeb V_tcpstat.tcps_ecn_ect0++; 1162f2512ba1SRui Paulo break; 1163f2512ba1SRui Paulo case IPTOS_ECN_ECT1: 1164603724d3SBjoern A. Zeeb V_tcpstat.tcps_ecn_ect1++; 1165f2512ba1SRui Paulo break; 1166f2512ba1SRui Paulo } 1167f2512ba1SRui Paulo 1168f2512ba1SRui Paulo if (thflags & TH_CWR) 1169f2512ba1SRui Paulo tp->t_flags &= ~TF_ECN_SND_ECE; 1170f2512ba1SRui Paulo 1171f2512ba1SRui Paulo /* 1172f2512ba1SRui Paulo * Congestion experienced. 1173f2512ba1SRui Paulo * Ignore if we are already trying to recover. 1174f2512ba1SRui Paulo */ 1175f2512ba1SRui Paulo if ((thflags & TH_ECE) && 1176f2512ba1SRui Paulo SEQ_LEQ(th->th_ack, tp->snd_recover)) { 1177603724d3SBjoern A. Zeeb V_tcpstat.tcps_ecn_rcwnd++; 1178f2512ba1SRui Paulo tcp_congestion_exp(tp); 1179f2512ba1SRui Paulo } 1180f2512ba1SRui Paulo } 1181f2512ba1SRui Paulo 1182f2512ba1SRui Paulo /* 1183f72167f4SAndre Oppermann * Parse options on any incoming segment. 1184f72167f4SAndre Oppermann */ 1185302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1186302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1187302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1188f72167f4SAndre Oppermann 1189f72167f4SAndre Oppermann /* 1190f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1191bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1192bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1193bf6d304aSAndre Oppermann * was established. 1194f72167f4SAndre Oppermann */ 1195bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1196e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 1197bf6d304aSAndre Oppermann if (TSTMP_GT(to.to_tsecr, ticks)) 1198f72167f4SAndre Oppermann to.to_tsecr = 0; 1199bf6d304aSAndre Oppermann } 1200f72167f4SAndre Oppermann 1201f72167f4SAndre Oppermann /* 120297d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 120397d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 12045396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 12055396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 120697d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1207df8bae1dSRodney W. Grimes */ 12080a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1209464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 1210464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 1211be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 121202a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 1213be2ac88cSJonathan Lemon } 12145396d0f8SAndre Oppermann /* 12155396d0f8SAndre Oppermann * Initial send window. It will be updated with 12165396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 12175396d0f8SAndre Oppermann */ 12185396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 1219be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1220be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1221be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1222be2ac88cSJonathan Lemon tp->ts_recent_age = ticks; 1223be2ac88cSJonathan Lemon } 1224be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1225be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 12263529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 12273529149eSAndre Oppermann (to.to_flags & TOF_SACKPERM) == 0) 12283529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 12296d90faf3SPaul Saab } 12306d90faf3SPaul Saab 1231df8bae1dSRodney W. Grimes /* 1232df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1233df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1234df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1235df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1236df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1237df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1238df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1239df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1240df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1241df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1242df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1243df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1244a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1245c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 1246a0292f23SGarrett Wollman * be TH_NEEDSYN. 1247df8bae1dSRodney W. Grimes */ 1248df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1249c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 1250fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1251c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 1252c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 1253a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1254c5ad39b9SAndre Oppermann LIST_EMPTY(&tp->t_segq) && 1255be2ac88cSJonathan Lemon ((to.to_flags & TOF_TS) == 0 || 1256c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1257df8bae1dSRodney W. Grimes 1258df8bae1dSRodney W. Grimes /* 1259df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1260df8bae1dSRodney W. Grimes * record the timestamp. 1261a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1262a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1263df8bae1dSRodney W. Grimes */ 1264be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1265fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 12669b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1267a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1268df8bae1dSRodney W. Grimes } 1269df8bae1dSRodney W. Grimes 1270fb59c426SYoshinobu Inoue if (tlen == 0) { 1271fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1272fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1273233e8c18SGarrett Wollman tp->snd_cwnd >= tp->snd_wnd && 1274603724d3SBjoern A. Zeeb ((!V_tcp_do_newreno && 12753529149eSAndre Oppermann !(tp->t_flags & TF_SACK_PERMIT) && 1276cb942153SJeffrey Hsu tp->t_dupacks < tcprexmtthresh) || 1277603724d3SBjoern A. Zeeb ((V_tcp_do_newreno || 12783529149eSAndre Oppermann (tp->t_flags & TF_SACK_PERMIT)) && 1279fc30a251SAndre Oppermann !IN_FASTRECOVERY(tp) && 1280fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1281482ac968SPaul Saab TAILQ_EMPTY(&tp->snd_holes)))) { 1282df8bae1dSRodney W. Grimes /* 1283e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1284df8bae1dSRodney W. Grimes */ 1285252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1286252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 1287252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1288252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1289252ca428SRobert Watson else 1290252ca428SRobert Watson panic("%s: ti_locked %d on pure ACK", 1291252ca428SRobert Watson __func__, ti_locked); 1292252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1293252ca428SRobert Watson 1294603724d3SBjoern A. Zeeb ++V_tcpstat.tcps_predack; 1295252ca428SRobert Watson 12969b8b58e0SJonathan Lemon /* 1297e8949f74SAndre Oppermann * "bad retransmit" recovery. 12989b8b58e0SJonathan Lemon */ 12999b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 13009b8b58e0SJonathan Lemon ticks < tp->t_badrxtwin) { 1301603724d3SBjoern A. Zeeb ++V_tcpstat.tcps_sndrexmitbad; 13029b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 13039b8b58e0SJonathan Lemon tp->snd_ssthresh = 13049b8b58e0SJonathan Lemon tp->snd_ssthresh_prev; 13059d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 13069d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 13079d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 13089b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 13099b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; 13109b8b58e0SJonathan Lemon } 1311fa55172bSMatthew Dillon 1312fa55172bSMatthew Dillon /* 1313fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1314fa55172bSMatthew Dillon * 1315fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1316fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1317fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1318fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1319fa55172bSMatthew Dillon */ 1320fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1321fa55172bSMatthew Dillon to.to_tsecr) { 1322eaf80179SAndre Oppermann if (!tp->t_rttlow || 1323eaf80179SAndre Oppermann tp->t_rttlow > ticks - to.to_tsecr) 1324eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 1325a0292f23SGarrett Wollman tcp_xmit_timer(tp, 13269b8b58e0SJonathan Lemon ticks - to.to_tsecr + 1); 1327fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1328fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1329eaf80179SAndre Oppermann if (!tp->t_rttlow || 1330eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1331eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1332c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1333c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1334fa55172bSMatthew Dillon } 13351fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 1336fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 1337603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvackpack++; 1338603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvackbyte += acked; 1339df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 13409d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 13419d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 13429d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 13439d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 13441645d090SJeff Roberson /* 1345e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 13461645d090SJeff Roberson * to th_ack. 13471645d090SJeff Roberson */ 1348cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1349b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1350df8bae1dSRodney W. Grimes m_freem(m); 1351e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made. */ 1352df8bae1dSRodney W. Grimes 1353df8bae1dSRodney W. Grimes /* 1354df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1355df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1356df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1357df8bae1dSRodney W. Grimes * If process is waiting for space, 1358df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1359df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1360df8bae1dSRodney W. Grimes * decide between more output or persist. 1361e8949f74SAndre Oppermann */ 13623c653157SHartmut Brandt #ifdef TCPDEBUG 13633c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 13643c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 13653c653157SHartmut Brandt (void *)tcp_saveipgen, 13663c653157SHartmut Brandt &tcp_savetcp, 0); 13673c653157SHartmut Brandt #endif 1368df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1369b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1370b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1371b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1372b8152ba7SAndre Oppermann tp->t_rxtcur); 1373df8bae1dSRodney W. Grimes sowwakeup(so); 1374df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 1375df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1376a14c749fSJonathan Lemon goto check_delack; 1377df8bae1dSRodney W. Grimes } 1378fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1379fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 13806741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 13816741ecf5SAndre Oppermann 1382df8bae1dSRodney W. Grimes /* 1383252ca428SRobert Watson * This is a pure, in-sequence data packet with 1384252ca428SRobert Watson * nothing on the reassembly queue and we have enough 1385252ca428SRobert Watson * buffer space to take it. 1386df8bae1dSRodney W. Grimes */ 1387252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1388252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 1389252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1390252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1391252ca428SRobert Watson else 1392252ca428SRobert Watson panic("%s: ti_locked %d on pure data " 1393252ca428SRobert Watson "segment", __func__, ti_locked); 1394252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1395252ca428SRobert Watson 13966d90faf3SPaul Saab /* Clean receiver SACK report if present */ 13973529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 13986d90faf3SPaul Saab tcp_clean_sackreport(tp); 1399603724d3SBjoern A. Zeeb ++V_tcpstat.tcps_preddat; 1400fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 14011645d090SJeff Roberson /* 14021645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 14031645d090SJeff Roberson * th_seq. 14041645d090SJeff Roberson */ 14051645d090SJeff Roberson tp->snd_wl1 = th->th_seq; 14061645d090SJeff Roberson /* 14071645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 14081645d090SJeff Roberson * rcv_nxt. 14091645d090SJeff Roberson */ 14101645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 1411603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvpack++; 1412603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbyte += tlen; 1413e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made */ 14143c653157SHartmut Brandt #ifdef TCPDEBUG 14153c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 14163c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 14173c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 14183c653157SHartmut Brandt #endif 14196741ecf5SAndre Oppermann /* 14206741ecf5SAndre Oppermann * Automatic sizing of receive socket buffer. Often the send 14216741ecf5SAndre Oppermann * buffer size is not optimally adjusted to the actual network 14226741ecf5SAndre Oppermann * conditions at hand (delay bandwidth product). Setting the 14236741ecf5SAndre Oppermann * buffer size too small limits throughput on links with high 14246741ecf5SAndre Oppermann * bandwidth and high delay (eg. trans-continental/oceanic links). 14256741ecf5SAndre Oppermann * 14266741ecf5SAndre Oppermann * On the receive side the socket buffer memory is only rarely 14276741ecf5SAndre Oppermann * used to any significant extent. This allows us to be much 14286741ecf5SAndre Oppermann * more aggressive in scaling the receive socket buffer. For 14296741ecf5SAndre Oppermann * the case that the buffer space is actually used to a large 14306741ecf5SAndre Oppermann * extent and we run out of kernel memory we can simply drop 14316741ecf5SAndre Oppermann * the new segments; TCP on the sender will just retransmit it 14326741ecf5SAndre Oppermann * later. Setting the buffer size too big may only consume too 14336741ecf5SAndre Oppermann * much kernel memory if the application doesn't read() from 14346741ecf5SAndre Oppermann * the socket or packet loss or reordering makes use of the 14356741ecf5SAndre Oppermann * reassembly queue. 14366741ecf5SAndre Oppermann * 14376741ecf5SAndre Oppermann * The criteria to step up the receive buffer one notch are: 14386741ecf5SAndre Oppermann * 1. the number of bytes received during the time it takes 14396741ecf5SAndre Oppermann * one timestamp to be reflected back to us (the RTT); 14406741ecf5SAndre Oppermann * 2. received bytes per RTT is within seven eighth of the 14416741ecf5SAndre Oppermann * current socket buffer size; 14426741ecf5SAndre Oppermann * 3. receive buffer size has not hit maximal automatic size; 14436741ecf5SAndre Oppermann * 14446741ecf5SAndre Oppermann * This algorithm does one step per RTT at most and only if 14456741ecf5SAndre Oppermann * we receive a bulk stream w/o packet losses or reorderings. 14466741ecf5SAndre Oppermann * Shrinking the buffer during idle times is not necessary as 14476741ecf5SAndre Oppermann * it doesn't consume any memory when idle. 14486741ecf5SAndre Oppermann * 14496741ecf5SAndre Oppermann * TODO: Only step up if the application is actually serving 14506741ecf5SAndre Oppermann * the buffer to better manage the socket buffer resources. 1451df8bae1dSRodney W. Grimes */ 1452603724d3SBjoern A. Zeeb if (V_tcp_do_autorcvbuf && 14536741ecf5SAndre Oppermann to.to_tsecr && 14546741ecf5SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 14556741ecf5SAndre Oppermann if (to.to_tsecr > tp->rfbuf_ts && 14566741ecf5SAndre Oppermann to.to_tsecr - tp->rfbuf_ts < hz) { 14576741ecf5SAndre Oppermann if (tp->rfbuf_cnt > 14586741ecf5SAndre Oppermann (so->so_rcv.sb_hiwat / 8 * 7) && 14596741ecf5SAndre Oppermann so->so_rcv.sb_hiwat < 1460603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max) { 14616741ecf5SAndre Oppermann newsize = 14626741ecf5SAndre Oppermann min(so->so_rcv.sb_hiwat + 1463603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_inc, 1464603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max); 14656741ecf5SAndre Oppermann } 14666741ecf5SAndre Oppermann /* Start over with next RTT. */ 14676741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 14686741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 14696741ecf5SAndre Oppermann } else 14706741ecf5SAndre Oppermann tp->rfbuf_cnt += tlen; /* add up */ 14716741ecf5SAndre Oppermann } 14726741ecf5SAndre Oppermann 14736741ecf5SAndre Oppermann /* Add data to socket buffer. */ 14741e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1475c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1476c1c36a2cSMike Silbersack m_freem(m); 1477c1c36a2cSMike Silbersack } else { 14786741ecf5SAndre Oppermann /* 14796741ecf5SAndre Oppermann * Set new socket buffer size. 14806741ecf5SAndre Oppermann * Give up when limit is reached. 14816741ecf5SAndre Oppermann */ 14826741ecf5SAndre Oppermann if (newsize) 14836741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 14846c8286e4SRobert Watson newsize, so, NULL)) 14856741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1486fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 14871e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 1488c1c36a2cSMike Silbersack } 1489e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 14901e4d7da7SRobert Watson sorwakeup_locked(so); 1491d8c85a26SJonathan Lemon if (DELAY_ACK(tp)) { 14923bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1493f498eeeeSDavid Greenman } else { 1494e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 1495e612a582SDavid Greenman tcp_output(tp); 1496e612a582SDavid Greenman } 1497a14c749fSJonathan Lemon goto check_delack; 1498df8bae1dSRodney W. Grimes } 1499df8bae1dSRodney W. Grimes } 1500df8bae1dSRodney W. Grimes 1501df8bae1dSRodney W. Grimes /* 1502df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1503df8bae1dSRodney W. Grimes * and then do TCP input processing. 1504df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1505df8bae1dSRodney W. Grimes * but not less than advertised window. 1506df8bae1dSRodney W. Grimes */ 1507df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1508df8bae1dSRodney W. Grimes if (win < 0) 1509df8bae1dSRodney W. Grimes win = 0; 151066e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1511df8bae1dSRodney W. Grimes 15126741ecf5SAndre Oppermann /* Reset receive buffer auto scaling when not in bulk receive mode. */ 15136741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 15146741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 15156741ecf5SAndre Oppermann 1516df8bae1dSRodney W. Grimes switch (tp->t_state) { 1517df8bae1dSRodney W. Grimes 1518df8bae1dSRodney W. Grimes /* 1519764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1520764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1521764d8cefSBill Fenner */ 1522764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1523fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1524fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1525a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1526a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1527a57815efSBosko Milekic goto dropwithreset; 1528a57815efSBosko Milekic } 1529764d8cefSBill Fenner break; 1530764d8cefSBill Fenner 1531764d8cefSBill Fenner /* 1532df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1533df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1534df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1535df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1536df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1537df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1538df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1539f2512ba1SRui Paulo * if seg contains an ECE and ECN support is enabled, the stream 1540f2512ba1SRui Paulo * is ECN capable. 1541df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1542df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1543df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1544df8bae1dSRodney W. Grimes */ 1545df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1546fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1547fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1548fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1549a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1550a0292f23SGarrett Wollman goto dropwithreset; 1551a0292f23SGarrett Wollman } 15520ca3f933SAndre Oppermann if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) 1553df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 15540ca3f933SAndre Oppermann if (thflags & TH_RST) 1555df8bae1dSRodney W. Grimes goto drop; 15560ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 1557df8bae1dSRodney W. Grimes goto drop; 1558464fcfbcSAndre Oppermann 1559fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1560df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1561fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1562603724d3SBjoern A. Zeeb V_tcpstat.tcps_connects++; 1563845799c1SAndras Olah soisconnected(so); 1564c488362eSRobert Watson #ifdef MAC 1565310e7cebSRobert Watson SOCK_LOCK(so); 156630d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 1567310e7cebSRobert Watson SOCK_UNLOCK(so); 1568c488362eSRobert Watson #endif 1569845799c1SAndras Olah /* Do window scaling on this connection? */ 1570845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1571845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1572845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1573845799c1SAndras Olah } 1574a0292f23SGarrett Wollman tp->rcv_adv += tp->rcv_wnd; 1575a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1576a0292f23SGarrett Wollman /* 1577a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1578a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1579a0292f23SGarrett Wollman */ 1580d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && tlen != 0) 1581b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 1582b8152ba7SAndre Oppermann tcp_delacktime); 1583a0292f23SGarrett Wollman else 1584a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1585f2512ba1SRui Paulo 1586603724d3SBjoern A. Zeeb if ((thflags & TH_ECE) && V_tcp_do_ecn) { 1587f2512ba1SRui Paulo tp->t_flags |= TF_ECN_PERMIT; 1588603724d3SBjoern A. Zeeb V_tcpstat.tcps_ecn_shs++; 1589f2512ba1SRui Paulo } 1590f2512ba1SRui Paulo 1591a0292f23SGarrett Wollman /* 1592a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1593a0292f23SGarrett Wollman * Transitions: 1594a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1595a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1596a0292f23SGarrett Wollman */ 15979b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1598a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1599a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1600a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1601fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 16027ff19458SPaul Traina } else { 1603a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1604b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 16057ff19458SPaul Traina } 1606a0292f23SGarrett Wollman } else { 1607a0292f23SGarrett Wollman /* 1608c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 1609c068736aSJeffrey Hsu * simultaneous open. If segment contains CC option 1610c068736aSJeffrey Hsu * and there is a cached CC, apply TAO test. 1611c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 1612c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 1613a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1614a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1615a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1616a0292f23SGarrett Wollman */ 16174b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 1618b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1619df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1620a0292f23SGarrett Wollman } 1621df8bae1dSRodney W. Grimes 1622252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: trimthenstep6: " 1623252ca428SRobert Watson "ti_locked %d", __func__, ti_locked)); 1624252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 16258501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 16267cfc6904SRobert Watson 1627df8bae1dSRodney W. Grimes /* 1628fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1629df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1630df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1631df8bae1dSRodney W. Grimes */ 1632fb59c426SYoshinobu Inoue th->th_seq++; 1633fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1634fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1635df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1636fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1637fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1638603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvpackafterwin++; 1639603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbyteafterwin += todrop; 1640df8bae1dSRodney W. Grimes } 1641fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1642fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 1643a0292f23SGarrett Wollman /* 1644a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1645a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1646a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1647a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1648a0292f23SGarrett Wollman * Otherwise, goto step 6. 1649a0292f23SGarrett Wollman */ 1650fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1651a0292f23SGarrett Wollman goto process_ACK; 1652c068736aSJeffrey Hsu 1653df8bae1dSRodney W. Grimes goto step6; 1654c068736aSJeffrey Hsu 1655a0292f23SGarrett Wollman /* 1656a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1657c94c54e4SAndre Oppermann * do normal processing. 1658a0292f23SGarrett Wollman * 1659c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 1660a0292f23SGarrett Wollman */ 1661a0292f23SGarrett Wollman case TCPS_LAST_ACK: 1662a0292f23SGarrett Wollman case TCPS_CLOSING: 1663a0292f23SGarrett Wollman break; /* continue normal processing */ 1664df8bae1dSRodney W. Grimes } 1665df8bae1dSRodney W. Grimes 1666df8bae1dSRodney W. Grimes /* 1667df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 166880ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 166980ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 167080ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 167180ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 167280ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 167380ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 167480ab7c0eSGarrett Wollman * killing a connection). 167580ab7c0eSGarrett Wollman * Then check timestamp, if present. 1676a0292f23SGarrett Wollman * Then check the connection count, if present. 1677df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 1678df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 1679df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 1680df8bae1dSRodney W. Grimes * 168180ab7c0eSGarrett Wollman * 168280ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 168380ab7c0eSGarrett Wollman * if this is a valid reset segment. 168480ab7c0eSGarrett Wollman * RFC 793 page 37: 168580ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 168680ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 168780ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 168880ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 168980ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 16901a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 16911a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 16921a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 16931a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 169480dd2a81SMike Silbersack * Note 2: Paul Watson's paper "Slipping in the Window" has shown 169580dd2a81SMike Silbersack * that brute force RST attacks are possible. To combat this, 169680dd2a81SMike Silbersack * we use a much stricter check while in the ESTABLISHED state, 169780dd2a81SMike Silbersack * only accepting RSTs where the sequence number is equal to 169880dd2a81SMike Silbersack * last_ack_sent. In all other states (the states in which a 169980dd2a81SMike Silbersack * RST is more likely), the more permissive check is used. 17008e5c87f4SBjoern A. Zeeb * If we have multiple segments in flight, the initial reset 170180ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 170280ab7c0eSGarrett Wollman * but they will eventually catch up. 170380ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 170480ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 170580ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 170680ab7c0eSGarrett Wollman * 170780ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 170880ab7c0eSGarrett Wollman * 170980ab7c0eSGarrett Wollman * DISCUSSION 171080ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 171180ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 171280ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 171380ab7c0eSGarrett Wollman * data. 171480ab7c0eSGarrett Wollman * 171580ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 171680ab7c0eSGarrett Wollman * the state: 171780ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 171880ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 171980ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 1720745bab7fSDima Dorfman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 172180ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 1722e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 172380ab7c0eSGarrett Wollman * Close the tcb. 1724e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 172580ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 172680ab7c0eSGarrett Wollman * RFC 1337. 172780ab7c0eSGarrett Wollman */ 1728fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 172995ad8418SQing Li if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 173095ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 173180ab7c0eSGarrett Wollman switch (tp->t_state) { 173280ab7c0eSGarrett Wollman 173380ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 173480ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 173580ab7c0eSGarrett Wollman goto close; 173680ab7c0eSGarrett Wollman 173780ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 1738603724d3SBjoern A. Zeeb if (V_tcp_insecure_rst == 0 && 173995ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) && 174095ad8418SQing Li SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) && 174195ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 174295ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) { 1743603724d3SBjoern A. Zeeb V_tcpstat.tcps_badrst++; 174480dd2a81SMike Silbersack goto drop; 174580dd2a81SMike Silbersack } 1746564aab1fSAndre Oppermann /* FALLTHROUGH */ 174780ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 174880ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 174980ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 175080ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 175180ab7c0eSGarrett Wollman close: 1752252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 1753252ca428SRobert Watson ("tcp_do_segment: TH_RST 1 ti_locked %d", 1754252ca428SRobert Watson ti_locked)); 1755252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1756252ca428SRobert Watson 175780ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 1758603724d3SBjoern A. Zeeb V_tcpstat.tcps_drops++; 175980ab7c0eSGarrett Wollman tp = tcp_close(tp); 176080ab7c0eSGarrett Wollman break; 176180ab7c0eSGarrett Wollman 176280ab7c0eSGarrett Wollman case TCPS_CLOSING: 176380ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 1764252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 1765252ca428SRobert Watson ("tcp_do_segment: TH_RST 2 ti_locked %d", 1766252ca428SRobert Watson ti_locked)); 1767252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1768252ca428SRobert Watson 176980ab7c0eSGarrett Wollman tp = tcp_close(tp); 177080ab7c0eSGarrett Wollman break; 177180ab7c0eSGarrett Wollman } 177280ab7c0eSGarrett Wollman } 177380ab7c0eSGarrett Wollman goto drop; 177480ab7c0eSGarrett Wollman } 177580ab7c0eSGarrett Wollman 177680ab7c0eSGarrett Wollman /* 1777df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 1778df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 1779df8bae1dSRodney W. Grimes */ 1780be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 178180ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1782df8bae1dSRodney W. Grimes 1783df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 17849b8b58e0SJonathan Lemon if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1785df8bae1dSRodney W. Grimes /* 1786df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 1787df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 1788df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 1789df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 1790df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 1791df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 1792df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 1793df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 1794df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 1795df8bae1dSRodney W. Grimes */ 1796df8bae1dSRodney W. Grimes tp->ts_recent = 0; 1797df8bae1dSRodney W. Grimes } else { 1798603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvduppack++; 1799603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvdupbyte += tlen; 1800603724d3SBjoern A. Zeeb V_tcpstat.tcps_pawsdrop++; 180107fd333dSMatthew Dillon if (tlen) 1802df8bae1dSRodney W. Grimes goto dropafterack; 18031ab4789dSMatthew Dillon goto drop; 18041ab4789dSMatthew Dillon } 1805df8bae1dSRodney W. Grimes } 1806df8bae1dSRodney W. Grimes 1807a0292f23SGarrett Wollman /* 180880ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 180980ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 181080ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 181180ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 181280ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 181380ab7c0eSGarrett Wollman */ 1814a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 1815a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1816a57815efSBosko Milekic goto dropwithreset; 1817a57815efSBosko Milekic } 181880ab7c0eSGarrett Wollman 1819fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 1820df8bae1dSRodney W. Grimes if (todrop > 0) { 1821fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1822fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 1823fb59c426SYoshinobu Inoue th->th_seq++; 1824fb59c426SYoshinobu Inoue if (th->th_urp > 1) 1825fb59c426SYoshinobu Inoue th->th_urp--; 1826df8bae1dSRodney W. Grimes else 1827fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1828df8bae1dSRodney W. Grimes todrop--; 1829df8bae1dSRodney W. Grimes } 1830df8bae1dSRodney W. Grimes /* 1831dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 1832df8bae1dSRodney W. Grimes */ 1833fb59c426SYoshinobu Inoue if (todrop > tlen 1834fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1835dac20301SGarrett Wollman /* 1836dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 1837dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 1838dac20301SGarrett Wollman * of sequence; drop it. 1839dac20301SGarrett Wollman */ 1840fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1841dac20301SGarrett Wollman 1842df8bae1dSRodney W. Grimes /* 1843dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 1844dac20301SGarrett Wollman * But keep on processing for RST or ACK. 1845df8bae1dSRodney W. Grimes */ 1846dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1847fb59c426SYoshinobu Inoue todrop = tlen; 1848603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvduppack++; 1849603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvdupbyte += todrop; 1850df8bae1dSRodney W. Grimes } else { 1851603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvpartduppack++; 1852603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvpartdupbyte += todrop; 1853df8bae1dSRodney W. Grimes } 1854fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 1855fb59c426SYoshinobu Inoue th->th_seq += todrop; 1856fb59c426SYoshinobu Inoue tlen -= todrop; 1857fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 1858fb59c426SYoshinobu Inoue th->th_urp -= todrop; 1859df8bae1dSRodney W. Grimes else { 1860fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1861fb59c426SYoshinobu Inoue th->th_urp = 0; 1862df8bae1dSRodney W. Grimes } 1863df8bae1dSRodney W. Grimes } 1864df8bae1dSRodney W. Grimes 1865df8bae1dSRodney W. Grimes /* 1866df8bae1dSRodney W. Grimes * If new data are received on a connection after the 1867df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 1868df8bae1dSRodney W. Grimes */ 1869df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 1870fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1871773673c1SAndre Oppermann char *s; 1872773673c1SAndre Oppermann 1873252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: SS_NOFDEREF && " 1874252ca428SRobert Watson "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked)); 1875252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1876252ca428SRobert Watson 1877773673c1SAndre Oppermann if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) { 1878e31d8aa3SMike Silbersack log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data after socket " 1879773673c1SAndre Oppermann "was closed, sending RST and removing tcpcb\n", 1880e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 1881773673c1SAndre Oppermann free(s, M_TCPLOG); 1882773673c1SAndre Oppermann } 1883df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1884603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvafterclose++; 1885a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1886df8bae1dSRodney W. Grimes goto dropwithreset; 1887df8bae1dSRodney W. Grimes } 1888df8bae1dSRodney W. Grimes 1889df8bae1dSRodney W. Grimes /* 1890df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 1891df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 1892df8bae1dSRodney W. Grimes */ 1893fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 1894df8bae1dSRodney W. Grimes if (todrop > 0) { 1895603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvpackafterwin++; 1896fb59c426SYoshinobu Inoue if (todrop >= tlen) { 1897603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbyteafterwin += tlen; 1898df8bae1dSRodney W. Grimes /* 1899df8bae1dSRodney W. Grimes * If window is closed can only take segments at 1900df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 1901df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 1902df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 1903df8bae1dSRodney W. Grimes * and ack. 1904df8bae1dSRodney W. Grimes */ 1905fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1906df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1907603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvwinprobe++; 1908df8bae1dSRodney W. Grimes } else 1909df8bae1dSRodney W. Grimes goto dropafterack; 1910df8bae1dSRodney W. Grimes } else 1911603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbyteafterwin += todrop; 1912df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1913fb59c426SYoshinobu Inoue tlen -= todrop; 1914fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 1915df8bae1dSRodney W. Grimes } 1916df8bae1dSRodney W. Grimes 1917df8bae1dSRodney W. Grimes /* 1918df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1919df8bae1dSRodney W. Grimes * record its timestamp. 1920cf09195bSPaul Saab * NOTE: 1921cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 1922a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1923cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 1924cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 1925cf09195bSPaul Saab * predicated on the sequence space of this segment. 1926cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 1927cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 1928cf09195bSPaul Saab * instead of RFC1323's 1929cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 1930cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 1931cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 1932cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 1933cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 1934df8bae1dSRodney W. Grimes */ 1935be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1936cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 1937cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 1938cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 19399b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1940a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1941df8bae1dSRodney W. Grimes } 1942df8bae1dSRodney W. Grimes 1943df8bae1dSRodney W. Grimes /* 1944df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 1945df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 1946df8bae1dSRodney W. Grimes */ 1947fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1948252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 1949252ca428SRobert Watson ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked)); 1950252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1951252ca428SRobert Watson 1952df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 1953a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1954122aad88SAndre Oppermann goto drop; 1955df8bae1dSRodney W. Grimes } 1956df8bae1dSRodney W. Grimes 1957a0292f23SGarrett Wollman /* 1958a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1959a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 1960a0292f23SGarrett Wollman * later processing; else drop segment and return. 1961a0292f23SGarrett Wollman */ 1962fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 1963a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 1964a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 1965a0292f23SGarrett Wollman goto step6; 19665e1aa279SDavid Malone else if (tp->t_flags & TF_ACKNOW) 19675e1aa279SDavid Malone goto dropafterack; 1968a0292f23SGarrett Wollman else 1969a0292f23SGarrett Wollman goto drop; 1970a0292f23SGarrett Wollman } 1971df8bae1dSRodney W. Grimes 1972df8bae1dSRodney W. Grimes /* 1973df8bae1dSRodney W. Grimes * Ack processing. 1974df8bae1dSRodney W. Grimes */ 1975df8bae1dSRodney W. Grimes switch (tp->t_state) { 1976df8bae1dSRodney W. Grimes 1977df8bae1dSRodney W. Grimes /* 1978764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1979764d8cefSBill Fenner * ESTABLISHED state and continue processing. 1980764d8cefSBill Fenner * The ACK was checked above. 1981df8bae1dSRodney W. Grimes */ 1982df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1983a0292f23SGarrett Wollman 1984603724d3SBjoern A. Zeeb V_tcpstat.tcps_connects++; 1985df8bae1dSRodney W. Grimes soisconnected(so); 1986df8bae1dSRodney W. Grimes /* Do window scaling? */ 1987df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1988df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1989df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 1990464fcfbcSAndre Oppermann tp->snd_wnd = tiwin; 1991df8bae1dSRodney W. Grimes } 1992a0292f23SGarrett Wollman /* 1993a0292f23SGarrett Wollman * Make transitions: 1994a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 1995a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 1996a0292f23SGarrett Wollman */ 19979b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1998a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1999a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 2000a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 20017ff19458SPaul Traina } else { 2002a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 2003b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 20047ff19458SPaul Traina } 2005a0292f23SGarrett Wollman /* 2006a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 2007a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 2008a0292f23SGarrett Wollman */ 2009fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 2010fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 2011a0292f23SGarrett Wollman (struct mbuf *)0); 2012fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 201393b0017fSPhilippe Charnier /* FALLTHROUGH */ 2014df8bae1dSRodney W. Grimes 2015df8bae1dSRodney W. Grimes /* 2016df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2017df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 2018fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 2019fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 2020df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 2021df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 2022df8bae1dSRodney W. Grimes */ 2023df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2024df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2025df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2026df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 2027df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2028df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 20295a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 2030603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvacktoomuch++; 20315a53ca16SPaul Saab goto dropafterack; 20325a53ca16SPaul Saab } 20333529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2034fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 2035fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 20365a53ca16SPaul Saab tcp_sack_doack(tp, &to, th->th_ack); 2037fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 2038fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 2039603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvdupack++; 2040df8bae1dSRodney W. Grimes /* 2041df8bae1dSRodney W. Grimes * If we have outstanding data (other than 2042df8bae1dSRodney W. Grimes * a window probe), this is a completely 2043df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 2044df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 2045df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 2046df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 2047df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 2048df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 2049df8bae1dSRodney W. Grimes * window so we send only this one 2050df8bae1dSRodney W. Grimes * packet. 2051df8bae1dSRodney W. Grimes * 2052df8bae1dSRodney W. Grimes * We know we're losing at the current 2053df8bae1dSRodney W. Grimes * window size so do congestion avoidance 2054df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 2055df8bae1dSRodney W. Grimes * and pull our congestion window back to 2056df8bae1dSRodney W. Grimes * the new ssthresh). 2057df8bae1dSRodney W. Grimes * 2058df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 2059df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 2060df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 2061df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 2062df8bae1dSRodney W. Grimes * network. 2063f2512ba1SRui Paulo * 2064f2512ba1SRui Paulo * When using TCP ECN, notify the peer that 2065f2512ba1SRui Paulo * we reduced the cwnd. 2066df8bae1dSRodney W. Grimes */ 2067b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_REXMT) || 2068fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 2069df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2070cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 2071603724d3SBjoern A. Zeeb ((V_tcp_do_newreno || 20723529149eSAndre Oppermann (tp->t_flags & TF_SACK_PERMIT)) && 20739d11646dSJeffrey Hsu IN_FASTRECOVERY(tp))) { 20743529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 20753529149eSAndre Oppermann IN_FASTRECOVERY(tp)) { 2076808f11b7SPaul Saab int awnd; 207725e6f9edSPaul Saab 207825e6f9edSPaul Saab /* 207925e6f9edSPaul Saab * Compute the amount of data in flight first. 208025e6f9edSPaul Saab * We can inject new data into the pipe iff 208125e6f9edSPaul Saab * we have less than 1/2 the original window's 208225e6f9edSPaul Saab * worth of data in flight. 208325e6f9edSPaul Saab */ 2084808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 20850077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 2086808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 208725e6f9edSPaul Saab tp->snd_cwnd += tp->t_maxseg; 208825e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 208925e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 209025e6f9edSPaul Saab } 209125e6f9edSPaul Saab } else 209246f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 209346f58482SJonathan Lemon (void) tcp_output(tp); 209446f58482SJonathan Lemon goto drop; 2095cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 2096cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 2097a0445c2eSJayanth Vijayaraghavan 2098a0445c2eSJayanth Vijayaraghavan /* 2099a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 2100a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 2101a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 2102a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 2103a0445c2eSJayanth Vijayaraghavan * recovery. 2104a0445c2eSJayanth Vijayaraghavan */ 21053529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 2106a0445c2eSJayanth Vijayaraghavan if (IN_FASTRECOVERY(tp)) { 2107a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 2108a0445c2eSJayanth Vijayaraghavan break; 2109a0445c2eSJayanth Vijayaraghavan } 2110603724d3SBjoern A. Zeeb } else if (V_tcp_do_newreno || 2111603724d3SBjoern A. Zeeb V_tcp_do_ecn) { 2112a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 21139d11646dSJeffrey Hsu tp->snd_recover)) { 2114cb942153SJeffrey Hsu tp->t_dupacks = 0; 2115cb942153SJeffrey Hsu break; 211646f58482SJonathan Lemon } 2117a0445c2eSJayanth Vijayaraghavan } 2118f2512ba1SRui Paulo tcp_congestion_exp(tp); 2119b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 21209b8b58e0SJonathan Lemon tp->t_rtttime = 0; 21213529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 2122603724d3SBjoern A. Zeeb V_tcpstat.tcps_sack_recovery_episode++; 2123a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 21248db456bfSPaul Saab tp->snd_cwnd = tp->t_maxseg; 21256d90faf3SPaul Saab (void) tcp_output(tp); 21266d90faf3SPaul Saab goto drop; 21276d90faf3SPaul Saab } 2128fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 2129df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 2130df8bae1dSRodney W. Grimes (void) tcp_output(tp); 213148d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 2132302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 2133302ce8d6SAndre Oppermann __func__)); 2134df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 213548d2549cSJeffrey Hsu tp->t_maxseg * 213648d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 2137df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 2138df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 2139df8bae1dSRodney W. Grimes goto drop; 2140603724d3SBjoern A. Zeeb } else if (V_tcp_do_rfc3042) { 2141582a954bSJeffrey Hsu u_long oldcwnd = tp->snd_cwnd; 214248d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 214348d2549cSJeffrey Hsu u_int sent; 214489c02376SJeffrey Hsu 2145582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 2146582a954bSJeffrey Hsu tp->t_dupacks == 2, 2147302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 2148302ce8d6SAndre Oppermann __func__)); 214961a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 215048d2549cSJeffrey Hsu tp->snd_limited = 0; 215161a36e3dSJeffrey Hsu tp->snd_cwnd = 215261a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 215361a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 215461a36e3dSJeffrey Hsu tp->t_maxseg; 2155582a954bSJeffrey Hsu (void) tcp_output(tp); 215648d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 215748d2549cSJeffrey Hsu if (sent > tp->t_maxseg) { 215889c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 215989c02376SJeffrey Hsu tp->snd_limited == 0) || 216089c02376SJeffrey Hsu (sent == tp->t_maxseg + 1 && 216189c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 2162302ce8d6SAndre Oppermann ("%s: sent too much", 2163302ce8d6SAndre Oppermann __func__)); 216448d2549cSJeffrey Hsu tp->snd_limited = 2; 216548d2549cSJeffrey Hsu } else if (sent > 0) 216648d2549cSJeffrey Hsu ++tp->snd_limited; 2167582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 2168582a954bSJeffrey Hsu goto drop; 2169df8bae1dSRodney W. Grimes } 2170df8bae1dSRodney W. Grimes } else 2171df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2172df8bae1dSRodney W. Grimes break; 2173df8bae1dSRodney W. Grimes } 2174cb942153SJeffrey Hsu 2175302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2176302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 2177cb942153SJeffrey Hsu 2178df8bae1dSRodney W. Grimes /* 2179df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2180df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2181df8bae1dSRodney W. Grimes */ 2182603724d3SBjoern A. Zeeb if (V_tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) { 21839d11646dSJeffrey Hsu if (IN_FASTRECOVERY(tp)) { 2184cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 21853529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 21866d90faf3SPaul Saab tcp_sack_partialack(tp, th); 21876d90faf3SPaul Saab else 2188c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2189c068736aSJeffrey Hsu } else { 2190c068736aSJeffrey Hsu /* 21916d90faf3SPaul Saab * Out of fast recovery. 2192c068736aSJeffrey Hsu * Window inflation should have left us 2193c068736aSJeffrey Hsu * with approximately snd_ssthresh 2194c068736aSJeffrey Hsu * outstanding data. 2195c068736aSJeffrey Hsu * But in case we would be inclined to 2196c068736aSJeffrey Hsu * send a burst, better to do it via 2197c068736aSJeffrey Hsu * the slow start mechanism. 2198c068736aSJeffrey Hsu */ 2199c068736aSJeffrey Hsu if (SEQ_GT(th->th_ack + 2200c068736aSJeffrey Hsu tp->snd_ssthresh, 2201c068736aSJeffrey Hsu tp->snd_max)) 2202c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_max - 2203c068736aSJeffrey Hsu th->th_ack + 2204c068736aSJeffrey Hsu tp->t_maxseg; 2205c068736aSJeffrey Hsu else 2206c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_ssthresh; 2207c068736aSJeffrey Hsu } 2208c068736aSJeffrey Hsu } 2209c068736aSJeffrey Hsu } else { 2210233e8c18SGarrett Wollman if (tp->t_dupacks >= tcprexmtthresh && 2211df8bae1dSRodney W. Grimes tp->snd_cwnd > tp->snd_ssthresh) 2212df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh; 221346f58482SJonathan Lemon } 2214cb942153SJeffrey Hsu tp->t_dupacks = 0; 2215a0292f23SGarrett Wollman /* 2216a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2217a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2218a0292f23SGarrett Wollman */ 2219a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2220a0292f23SGarrett Wollman /* 2221a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2222a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 222307e43e10SAndras Olah * synchronized). Go to non-starred state, 222407e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 222507e43e10SAndras Olah * we can do window scaling. 2226a0292f23SGarrett Wollman */ 2227a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2228a0292f23SGarrett Wollman tp->snd_una++; 222907e43e10SAndras Olah /* Do window scaling? */ 223007e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 223107e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 223207e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2233464fcfbcSAndre Oppermann /* Send window already scaled. */ 223407e43e10SAndras Olah } 2235a0292f23SGarrett Wollman } 2236a0292f23SGarrett Wollman 2237a0292f23SGarrett Wollman process_ACK: 2238252ca428SRobert Watson INP_INFO_LOCK_ASSERT(&V_tcbinfo); 2239252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2240252ca428SRobert Watson ("tcp_input: process_ACK ti_locked %d", ti_locked)); 22418501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 22427cfc6904SRobert Watson 2243fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 2244603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvackpack++; 2245603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvackbyte += acked; 2246df8bae1dSRodney W. Grimes 2247df8bae1dSRodney W. Grimes /* 22489b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 22499b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 22509b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 22519b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 22529b8b58e0SJonathan Lemon * we left off. 22539b8b58e0SJonathan Lemon */ 22549b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 2255603724d3SBjoern A. Zeeb ++V_tcpstat.tcps_sndrexmitbad; 22569b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 22579b8b58e0SJonathan Lemon tp->snd_ssthresh = tp->snd_ssthresh_prev; 22589d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 22599d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 22609d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 22619b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 22629b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; /* XXX probably not required */ 22639b8b58e0SJonathan Lemon } 22649b8b58e0SJonathan Lemon 22659b8b58e0SJonathan Lemon /* 2266df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2267df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2268df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2269df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2270df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2271df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2272df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2273fa55172bSMatthew Dillon * 2274fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2275fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2276fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2277fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2278df8bae1dSRodney W. Grimes */ 2279fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 2280fa55172bSMatthew Dillon to.to_tsecr) { 2281eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - to.to_tsecr) 2282eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 22839b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 2284fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2285eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2286eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 22879b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2288fa55172bSMatthew Dillon } 22891fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 2290df8bae1dSRodney W. Grimes 2291df8bae1dSRodney W. Grimes /* 2292df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2293df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2294df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2295df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2296df8bae1dSRodney W. Grimes */ 2297fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 2298b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2299df8bae1dSRodney W. Grimes needoutput = 1; 2300b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2301b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2302a0292f23SGarrett Wollman 2303a0292f23SGarrett Wollman /* 2304a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2305a0292f23SGarrett Wollman * skip rest of ACK processing. 2306a0292f23SGarrett Wollman */ 2307a0292f23SGarrett Wollman if (acked == 0) 2308a0292f23SGarrett Wollman goto step6; 2309a0292f23SGarrett Wollman 2310df8bae1dSRodney W. Grimes /* 2311df8bae1dSRodney W. Grimes * When new data is acked, open the congestion window. 231224cb0f22SLawrence Stewart * Method depends on which congestion control state we're 231324cb0f22SLawrence Stewart * in (slow start or cong avoid) and if ABC (RFC 3465) is 231424cb0f22SLawrence Stewart * enabled. 231524cb0f22SLawrence Stewart * 231624cb0f22SLawrence Stewart * slow start: cwnd <= ssthresh 231724cb0f22SLawrence Stewart * cong avoid: cwnd > ssthresh 231824cb0f22SLawrence Stewart * 231924cb0f22SLawrence Stewart * slow start and ABC (RFC 3465): 232024cb0f22SLawrence Stewart * Grow cwnd exponentially by the amount of data 232124cb0f22SLawrence Stewart * ACKed capping the max increment per ACK to 232224cb0f22SLawrence Stewart * (abc_l_var * maxseg) bytes. 232324cb0f22SLawrence Stewart * 232424cb0f22SLawrence Stewart * slow start without ABC (RFC 2581): 232524cb0f22SLawrence Stewart * Grow cwnd exponentially by maxseg per ACK. 232624cb0f22SLawrence Stewart * 232724cb0f22SLawrence Stewart * cong avoid and ABC (RFC 3465): 232824cb0f22SLawrence Stewart * Grow cwnd linearly by maxseg per RTT for each 232924cb0f22SLawrence Stewart * cwnd worth of ACKed data. 233024cb0f22SLawrence Stewart * 233124cb0f22SLawrence Stewart * cong avoid without ABC (RFC 2581): 233224cb0f22SLawrence Stewart * Grow cwnd linearly by approximately maxseg per RTT using 233324cb0f22SLawrence Stewart * maxseg^2 / cwnd per ACK as the increment. 233424cb0f22SLawrence Stewart * If cwnd > maxseg^2, fix the cwnd increment at 1 byte to 233524cb0f22SLawrence Stewart * avoid capping cwnd. 2336df8bae1dSRodney W. Grimes */ 2337603724d3SBjoern A. Zeeb if ((!V_tcp_do_newreno && !(tp->t_flags & TF_SACK_PERMIT)) || 23386d90faf3SPaul Saab !IN_FASTRECOVERY(tp)) { 2339ad3f9ab3SAndre Oppermann u_int cw = tp->snd_cwnd; 2340ad3f9ab3SAndre Oppermann u_int incr = tp->t_maxseg; 234124cb0f22SLawrence Stewart /* In congestion avoidance? */ 234224cb0f22SLawrence Stewart if (cw > tp->snd_ssthresh) { 234324cb0f22SLawrence Stewart if (V_tcp_do_rfc3465) { 234424cb0f22SLawrence Stewart tp->t_bytes_acked += acked; 234524cb0f22SLawrence Stewart if (tp->t_bytes_acked >= tp->snd_cwnd) 234624cb0f22SLawrence Stewart tp->t_bytes_acked -= cw; 234724cb0f22SLawrence Stewart else 234824cb0f22SLawrence Stewart incr = 0; 234924cb0f22SLawrence Stewart } 235024cb0f22SLawrence Stewart else 2351c10eb6d1SBjoern A. Zeeb incr = max((incr * incr / cw), 1); 235224cb0f22SLawrence Stewart /* 235324cb0f22SLawrence Stewart * In slow-start with ABC enabled and no RTO in sight? 235424cb0f22SLawrence Stewart * (Must not use abc_l_var > 1 if slow starting after an 235524cb0f22SLawrence Stewart * RTO. On RTO, snd_nxt = snd_una, so the snd_nxt == 235624cb0f22SLawrence Stewart * snd_max check is sufficient to handle this). 235724cb0f22SLawrence Stewart */ 235824cb0f22SLawrence Stewart } else if (V_tcp_do_rfc3465 && 235924cb0f22SLawrence Stewart tp->snd_nxt == tp->snd_max) 236024cb0f22SLawrence Stewart incr = min(acked, 236124cb0f22SLawrence Stewart V_tcp_abc_l_var * tp->t_maxseg); 236224cb0f22SLawrence Stewart /* ABC is on by default, so (incr == 0) frequently. */ 236324cb0f22SLawrence Stewart if (incr > 0) 2364df8bae1dSRodney W. Grimes tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale); 2365df8bae1dSRodney W. Grimes } 23665905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2367df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 2368df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 23695905999bSRobert Watson sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc); 2370df8bae1dSRodney W. Grimes ourfinisacked = 1; 2371df8bae1dSRodney W. Grimes } else { 23725905999bSRobert Watson sbdrop_locked(&so->so_snd, acked); 2373df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 2374df8bae1dSRodney W. Grimes ourfinisacked = 0; 2375df8bae1dSRodney W. Grimes } 2376564aab1fSAndre Oppermann /* NB: sowwakeup_locked() does an implicit unlock. */ 23771e4d7da7SRobert Watson sowwakeup_locked(so); 2378e8949f74SAndre Oppermann /* Detect una wraparound. */ 2379603724d3SBjoern A. Zeeb if ((V_tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) && 23806d90faf3SPaul Saab !IN_FASTRECOVERY(tp) && 23819d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 23829d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 23839d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 2384603724d3SBjoern A. Zeeb if ((V_tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) && 23856d90faf3SPaul Saab IN_FASTRECOVERY(tp) && 238624cb0f22SLawrence Stewart SEQ_GEQ(th->th_ack, tp->snd_recover)) { 23879d11646dSJeffrey Hsu EXIT_FASTRECOVERY(tp); 238824cb0f22SLawrence Stewart tp->t_bytes_acked = 0; 238924cb0f22SLawrence Stewart } 2390fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 23913529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 23926d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 23936d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 23946d90faf3SPaul Saab } 2395df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2396df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2397df8bae1dSRodney W. Grimes 2398df8bae1dSRodney W. Grimes switch (tp->t_state) { 2399df8bae1dSRodney W. Grimes 2400df8bae1dSRodney W. Grimes /* 2401df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2402df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2403df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2404df8bae1dSRodney W. Grimes */ 2405df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2406df8bae1dSRodney W. Grimes if (ourfinisacked) { 2407df8bae1dSRodney W. Grimes /* 2408df8bae1dSRodney W. Grimes * If we can't receive any more 2409df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2410df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2411df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2412df8bae1dSRodney W. Grimes * we'll hang forever. 2413e8949f74SAndre Oppermann * 2414e8949f74SAndre Oppermann * XXXjl: 2415340c35deSJonathan Lemon * we should release the tp also, and use a 2416340c35deSJonathan Lemon * compressed state. 2417340c35deSJonathan Lemon */ 2418c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 24197c72af87SMohan Srinivasan int timeout; 24207c72af87SMohan Srinivasan 242103e49181SSeigo Tanimura soisdisconnected(so); 24227c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 24237c72af87SMohan Srinivasan tcp_finwait2_timeout : tcp_maxidle; 2424b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 24254cc20ab1SSeigo Tanimura } 2426df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 2427df8bae1dSRodney W. Grimes } 2428df8bae1dSRodney W. Grimes break; 2429df8bae1dSRodney W. Grimes 2430df8bae1dSRodney W. Grimes /* 2431df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2432df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2433df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2434df8bae1dSRodney W. Grimes * the segment. 2435df8bae1dSRodney W. Grimes */ 2436df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2437df8bae1dSRodney W. Grimes if (ourfinisacked) { 2438252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 243911a20fb8SJeffrey Hsu tcp_twstart(tp); 2440603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2441340c35deSJonathan Lemon m_freem(m); 2442679d9708SAndre Oppermann return; 2443df8bae1dSRodney W. Grimes } 2444df8bae1dSRodney W. Grimes break; 2445df8bae1dSRodney W. Grimes 2446df8bae1dSRodney W. Grimes /* 2447df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2448df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2449df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2450df8bae1dSRodney W. Grimes * enter the closed state and return. 2451df8bae1dSRodney W. Grimes */ 2452df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2453df8bae1dSRodney W. Grimes if (ourfinisacked) { 2454252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2455df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2456df8bae1dSRodney W. Grimes goto drop; 2457df8bae1dSRodney W. Grimes } 2458df8bae1dSRodney W. Grimes break; 2459df8bae1dSRodney W. Grimes } 2460df8bae1dSRodney W. Grimes } 2461df8bae1dSRodney W. Grimes 2462df8bae1dSRodney W. Grimes step6: 2463252ca428SRobert Watson INP_INFO_LOCK_ASSERT(&V_tcbinfo); 2464252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2465252ca428SRobert Watson ("tcp_do_segment: step6 ti_locked %d", ti_locked)); 24668501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 24677cfc6904SRobert Watson 2468df8bae1dSRodney W. Grimes /* 2469df8bae1dSRodney W. Grimes * Update window information. 2470df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 2471df8bae1dSRodney W. Grimes */ 2472fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 2473fb59c426SYoshinobu Inoue (SEQ_LT(tp->snd_wl1, th->th_seq) || 2474fb59c426SYoshinobu Inoue (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2475fb59c426SYoshinobu Inoue (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2476df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 2477fb59c426SYoshinobu Inoue if (tlen == 0 && 2478fb59c426SYoshinobu Inoue tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2479603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvwinupd++; 2480df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 2481fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq; 2482fb59c426SYoshinobu Inoue tp->snd_wl2 = th->th_ack; 2483df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2484df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 2485df8bae1dSRodney W. Grimes needoutput = 1; 2486df8bae1dSRodney W. Grimes } 2487df8bae1dSRodney W. Grimes 2488df8bae1dSRodney W. Grimes /* 2489df8bae1dSRodney W. Grimes * Process segments with URG. 2490df8bae1dSRodney W. Grimes */ 2491fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2492df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2493df8bae1dSRodney W. Grimes /* 2494df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2495df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2496df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2497df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2498df8bae1dSRodney W. Grimes */ 249918ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2500fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2501fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2502fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 250318ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2504df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2505df8bae1dSRodney W. Grimes } 2506df8bae1dSRodney W. Grimes /* 2507df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2508df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2509df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2510df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2511df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2512df8bae1dSRodney W. Grimes * 2513df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2514df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2515df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2516df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2517df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2518df8bae1dSRodney W. Grimes * spec states (in one of two places). 2519df8bae1dSRodney W. Grimes */ 2520fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2521fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2522df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2523df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2524927c5ceaSRobert Watson if (so->so_oobmark == 0) 2525c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 2526df8bae1dSRodney W. Grimes sohasoutofband(so); 2527df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2528df8bae1dSRodney W. Grimes } 252918ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2530df8bae1dSRodney W. Grimes /* 2531df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2532df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2533df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2534df8bae1dSRodney W. Grimes * data may creep in... ick. 2535df8bae1dSRodney W. Grimes */ 253630613f56SJeffrey Hsu if (th->th_urp <= (u_long)tlen && 253730613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 253830613f56SJeffrey Hsu /* hdr drop is delayed */ 253930613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 254030613f56SJeffrey Hsu } 2541c068736aSJeffrey Hsu } else { 2542df8bae1dSRodney W. Grimes /* 2543df8bae1dSRodney W. Grimes * If no out of band data is expected, 2544df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2545df8bae1dSRodney W. Grimes * with the receive window. 2546df8bae1dSRodney W. Grimes */ 2547df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2548df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2549c068736aSJeffrey Hsu } 2550df8bae1dSRodney W. Grimes dodata: /* XXX */ 2551252ca428SRobert Watson INP_INFO_LOCK_ASSERT(&V_tcbinfo); 2552252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2553252ca428SRobert Watson ("tcp_do_segment: dodata ti_locked %d", ti_locked)); 25548501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 25557cfc6904SRobert Watson 2556df8bae1dSRodney W. Grimes /* 2557df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2558df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2559df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2560df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2561df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2562df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2563df8bae1dSRodney W. Grimes */ 2564fb59c426SYoshinobu Inoue if ((tlen || (thflags & TH_FIN)) && 2565df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 256669e03620SPaul Saab tcp_seq save_start = th->th_seq; 2567fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2568e4b64281SJesper Skriver /* 2569c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2570c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2571c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2572c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2573c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2574c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2575c068736aSJeffrey Hsu * conversions. 2576c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 2577c068736aSJeffrey Hsu * immediately when segments are out of order (so 2578c068736aSJeffrey Hsu * fast retransmit can work). 2579e4b64281SJesper Skriver */ 2580e4b64281SJesper Skriver if (th->th_seq == tp->rcv_nxt && 2581e4b64281SJesper Skriver LIST_EMPTY(&tp->t_segq) && 2582e4b64281SJesper Skriver TCPS_HAVEESTABLISHED(tp->t_state)) { 2583e4b64281SJesper Skriver if (DELAY_ACK(tp)) 25843bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2585e4b64281SJesper Skriver else 2586e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2587e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2588e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 2589603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvpack++; 2590603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbyte += tlen; 2591e4b64281SJesper Skriver ND6_HINT(tp); 25921e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2593c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2594c1c36a2cSMike Silbersack m_freem(m); 2595c1c36a2cSMike Silbersack else 25961e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 2597e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 25981e4d7da7SRobert Watson sorwakeup_locked(so); 2599e4b64281SJesper Skriver } else { 2600e8949f74SAndre Oppermann /* 2601e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 2602e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 2603e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 2604e8949f74SAndre Oppermann * when trimming from the head. 2605e8949f74SAndre Oppermann */ 2606e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 2607e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2608e4b64281SJesper Skriver } 26093529149eSAndre Oppermann if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 2610f194524fSAndre Oppermann tcp_update_sack_list(tp, save_start, save_start + tlen); 2611302ce8d6SAndre Oppermann #if 0 2612df8bae1dSRodney W. Grimes /* 2613df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2614df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2615df8bae1dSRodney W. Grimes * buffer size. 2616302ce8d6SAndre Oppermann * XXX: Unused. 2617df8bae1dSRodney W. Grimes */ 2618df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2619302ce8d6SAndre Oppermann #endif 2620df8bae1dSRodney W. Grimes } else { 2621df8bae1dSRodney W. Grimes m_freem(m); 2622fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2623df8bae1dSRodney W. Grimes } 2624df8bae1dSRodney W. Grimes 2625df8bae1dSRodney W. Grimes /* 2626df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2627df8bae1dSRodney W. Grimes * that the connection is closing. 2628df8bae1dSRodney W. Grimes */ 2629fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2630df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2631df8bae1dSRodney W. Grimes socantrcvmore(so); 2632a0292f23SGarrett Wollman /* 2633a0292f23SGarrett Wollman * If connection is half-synchronized 2634d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2635a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2636a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2637a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2638a0292f23SGarrett Wollman */ 26393bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 26403bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2641a0292f23SGarrett Wollman else 2642df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2643df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2644df8bae1dSRodney W. Grimes } 2645df8bae1dSRodney W. Grimes switch (tp->t_state) { 2646df8bae1dSRodney W. Grimes 2647df8bae1dSRodney W. Grimes /* 2648df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2649df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2650df8bae1dSRodney W. Grimes */ 2651df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 26529b8b58e0SJonathan Lemon tp->t_starttime = ticks; 26539b8b58e0SJonathan Lemon /* FALLTHROUGH */ 2654df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2655df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2656df8bae1dSRodney W. Grimes break; 2657df8bae1dSRodney W. Grimes 2658df8bae1dSRodney W. Grimes /* 2659df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2660df8bae1dSRodney W. Grimes * enter the CLOSING state. 2661df8bae1dSRodney W. Grimes */ 2662df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2663df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 2664df8bae1dSRodney W. Grimes break; 2665df8bae1dSRodney W. Grimes 2666df8bae1dSRodney W. Grimes /* 2667df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 2668df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 2669df8bae1dSRodney W. Grimes * standard timers. 2670df8bae1dSRodney W. Grimes */ 2671df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2672252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2673252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: dodata " 2674252ca428SRobert Watson "TCP_FIN_WAIT_2 ti_locked: %d", __func__, 2675252ca428SRobert Watson ti_locked)); 2676252ca428SRobert Watson 2677340c35deSJonathan Lemon tcp_twstart(tp); 2678603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2679679d9708SAndre Oppermann return; 2680df8bae1dSRodney W. Grimes } 2681df8bae1dSRodney W. Grimes } 2682252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 2683252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 2684252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 2685603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2686252ca428SRobert Watson else 2687252ca428SRobert Watson panic("%s: dodata epilogue ti_locked %d", __func__, 2688252ca428SRobert Watson ti_locked); 2689252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2690252ca428SRobert Watson 2691610ee2f9SDavid Greenman #ifdef TCPDEBUG 26924cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2693fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2694fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2695610ee2f9SDavid Greenman #endif 2696df8bae1dSRodney W. Grimes 2697df8bae1dSRodney W. Grimes /* 2698df8bae1dSRodney W. Grimes * Return any desired output. 2699df8bae1dSRodney W. Grimes */ 2700df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 2701df8bae1dSRodney W. Grimes (void) tcp_output(tp); 27027792ea27SJeffrey Hsu 2703a14c749fSJonathan Lemon check_delack: 2704252ca428SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 2705252ca428SRobert Watson __func__, ti_locked)); 2706603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 27078501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 2708252ca428SRobert Watson 2709a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 27103bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 2711b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 27123bfd6421SJonathan Lemon } 27138501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2714679d9708SAndre Oppermann return; 2715df8bae1dSRodney W. Grimes 2716df8bae1dSRodney W. Grimes dropafterack: 2717252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2718252ca428SRobert Watson ("tcp_do_segment: dropafterack ti_locked %d", ti_locked)); 2719252ca428SRobert Watson 2720df8bae1dSRodney W. Grimes /* 2721df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 2722df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 272380ab7c0eSGarrett Wollman * 272480ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 272580ab7c0eSGarrett Wollman * paths to this code happen after packets containing 272680ab7c0eSGarrett Wollman * RST have been dropped. 272780ab7c0eSGarrett Wollman * 272880ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 272980ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 273080ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 273180ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 273280ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 273380ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 2734df8bae1dSRodney W. Grimes */ 2735fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2736fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 2737a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 2738a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2739a57815efSBosko Milekic goto dropwithreset; 2740a57815efSBosko Milekic } 2741a0292f23SGarrett Wollman #ifdef TCPDEBUG 27424cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2743fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2744fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2745a0292f23SGarrett Wollman #endif 2746252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 2747252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 2748252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 2749603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2750252ca428SRobert Watson else 2751252ca428SRobert Watson panic("%s: dropafterack epilogue ti_locked %d", __func__, 2752252ca428SRobert Watson ti_locked); 2753252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2754252ca428SRobert Watson 2755df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2756df8bae1dSRodney W. Grimes (void) tcp_output(tp); 27578501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2758d6915262SRobert Watson m_freem(m); 2759679d9708SAndre Oppermann return; 2760df8bae1dSRodney W. Grimes 2761df8bae1dSRodney W. Grimes dropwithreset: 2762252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 2763252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 2764252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 2765dd8ac7f9SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 2766252ca428SRobert Watson else 2767252ca428SRobert Watson panic("%s: dropwithreset ti_locked %d", __func__, ti_locked); 2768252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2769a57815efSBosko Milekic 2770a0ca0871SRobert Watson if (tp != NULL) { 2771302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 27728501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2773dd8ac7f9SRobert Watson } else 2774a0ca0871SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 2775679d9708SAndre Oppermann return; 2776df8bae1dSRodney W. Grimes 2777df8bae1dSRodney W. Grimes drop: 2778252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 2779252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 2780252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 2781252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 2782252ca428SRobert Watson #ifdef INVARIANTS 2783252ca428SRobert Watson else 2784252ca428SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 2785252ca428SRobert Watson #endif 2786252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2787252ca428SRobert Watson 2788df8bae1dSRodney W. Grimes /* 2789df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 2790df8bae1dSRodney W. Grimes */ 2791610ee2f9SDavid Greenman #ifdef TCPDEBUG 27921c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2793fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2794fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2795a0292f23SGarrett Wollman #endif 27961c53f806SRobert Watson if (tp != NULL) 27978501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2798d6915262SRobert Watson m_freem(m); 2799302ce8d6SAndre Oppermann } 2800302ce8d6SAndre Oppermann 2801302ce8d6SAndre Oppermann /* 28020ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 28030ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 28040ca3f933SAndre Oppermann * tp may be NULL. 2805302ce8d6SAndre Oppermann */ 2806302ce8d6SAndre Oppermann static void 2807302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 2808302ce8d6SAndre Oppermann int tlen, int rstreason) 2809302ce8d6SAndre Oppermann { 2810302ce8d6SAndre Oppermann struct ip *ip; 2811302ce8d6SAndre Oppermann #ifdef INET6 2812302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 2813302ce8d6SAndre Oppermann #endif 28147a3244ccSRobert Watson 28157a3244ccSRobert Watson if (tp != NULL) { 28168501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 28177a3244ccSRobert Watson } 28187a3244ccSRobert Watson 28190ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 2820302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2821302ce8d6SAndre Oppermann goto drop; 2822302ce8d6SAndre Oppermann #ifdef INET6 2823302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 2824302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 2825302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2826302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2827302ce8d6SAndre Oppermann goto drop; 2828302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 2829302ce8d6SAndre Oppermann } else 2830302ce8d6SAndre Oppermann #endif 2831302ce8d6SAndre Oppermann { 2832302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 2833302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2834302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2835302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 2836302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2837302ce8d6SAndre Oppermann goto drop; 2838302ce8d6SAndre Oppermann } 2839302ce8d6SAndre Oppermann 2840302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 2841302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 2842302ce8d6SAndre Oppermann goto drop; 2843302ce8d6SAndre Oppermann 2844302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 2845302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 2846302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 2847302ce8d6SAndre Oppermann th->th_ack, TH_RST); 2848302ce8d6SAndre Oppermann } else { 2849302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 2850302ce8d6SAndre Oppermann tlen++; 2851302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2852302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 2853302ce8d6SAndre Oppermann } 2854302ce8d6SAndre Oppermann return; 2855302ce8d6SAndre Oppermann drop: 2856302ce8d6SAndre Oppermann m_freem(m); 2857df8bae1dSRodney W. Grimes } 2858df8bae1dSRodney W. Grimes 2859be2ac88cSJonathan Lemon /* 2860be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 2861be2ac88cSJonathan Lemon */ 28620312fbe9SPoul-Henning Kamp static void 2863ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 2864df8bae1dSRodney W. Grimes { 28658b615593SMarko Zec INIT_VNET_INET(curvnet); 2866df8bae1dSRodney W. Grimes int opt, optlen; 2867df8bae1dSRodney W. Grimes 2868be2ac88cSJonathan Lemon to->to_flags = 0; 2869df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 2870df8bae1dSRodney W. Grimes opt = cp[0]; 2871df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 2872df8bae1dSRodney W. Grimes break; 2873df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 2874df8bae1dSRodney W. Grimes optlen = 1; 2875df8bae1dSRodney W. Grimes else { 2876b474779fSJun-ichiro itojun Hagino if (cnt < 2) 2877b474779fSJun-ichiro itojun Hagino break; 2878df8bae1dSRodney W. Grimes optlen = cp[1]; 2879b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 2880df8bae1dSRodney W. Grimes break; 2881df8bae1dSRodney W. Grimes } 2882df8bae1dSRodney W. Grimes switch (opt) { 2883df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 2884df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 2885df8bae1dSRodney W. Grimes continue; 2886f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2887df8bae1dSRodney W. Grimes continue; 2888be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 2889be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 2890be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 2891fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 2892df8bae1dSRodney W. Grimes break; 2893df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 2894df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 2895df8bae1dSRodney W. Grimes continue; 2896f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2897df8bae1dSRodney W. Grimes continue; 2898be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 289902a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 2900df8bae1dSRodney W. Grimes break; 2901df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 2902df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 2903df8bae1dSRodney W. Grimes continue; 2904be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 2905a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2906a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 2907fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 2908a0292f23SGarrett Wollman bcopy((char *)cp + 6, 2909a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2910fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 2911df8bae1dSRodney W. Grimes break; 29121cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 29131cfd4b53SBruce M Simpson /* 29141cfd4b53SBruce M Simpson * XXX In order to reply to a host which has set the 29151cfd4b53SBruce M Simpson * TCP_SIGNATURE option in its initial SYN, we have to 29161cfd4b53SBruce M Simpson * record the fact that the option was observed here 29171cfd4b53SBruce M Simpson * for the syncache code to perform the correct response. 29181cfd4b53SBruce M Simpson */ 29191cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 29201cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 29211cfd4b53SBruce M Simpson continue; 2922df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 2923df47e437SAndre Oppermann to->to_signature = cp + 2; 29241cfd4b53SBruce M Simpson break; 2925265ed012SBruce M Simpson #endif 29266d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 2927f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 29286d90faf3SPaul Saab continue; 2929f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2930f72167f4SAndre Oppermann continue; 2931603724d3SBjoern A. Zeeb if (!V_tcp_do_sack) 2932f72167f4SAndre Oppermann continue; 2933fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 29346d90faf3SPaul Saab break; 29356d90faf3SPaul Saab case TCPOPT_SACK: 29365a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 29376d90faf3SPaul Saab continue; 2938b728e902SAndre Oppermann if (flags & TO_SYN) 2939b728e902SAndre Oppermann continue; 2940fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 29415a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 29425a53ca16SPaul Saab to->to_sacks = cp + 2; 2943603724d3SBjoern A. Zeeb V_tcpstat.tcps_sack_rcv_blocks++; 29446d90faf3SPaul Saab break; 2945be2ac88cSJonathan Lemon default: 2946be2ac88cSJonathan Lemon continue; 2947df8bae1dSRodney W. Grimes } 2948df8bae1dSRodney W. Grimes } 2949df8bae1dSRodney W. Grimes } 2950df8bae1dSRodney W. Grimes 2951df8bae1dSRodney W. Grimes /* 2952df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 2953df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 2954df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 2955df8bae1dSRodney W. Grimes * sequencing purposes. 2956df8bae1dSRodney W. Grimes */ 29570312fbe9SPoul-Henning Kamp static void 2958ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 2959ad3f9ab3SAndre Oppermann int off) 2960df8bae1dSRodney W. Grimes { 2961fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 2962df8bae1dSRodney W. Grimes 2963df8bae1dSRodney W. Grimes while (cnt >= 0) { 2964df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 2965df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 2966df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 2967df8bae1dSRodney W. Grimes 29688501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 29697a3244ccSRobert Watson 2970df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 2971df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 2972df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2973df8bae1dSRodney W. Grimes m->m_len--; 297469a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 297569a34685SYoshinobu Inoue m->m_pkthdr.len--; 2976df8bae1dSRodney W. Grimes return; 2977df8bae1dSRodney W. Grimes } 2978df8bae1dSRodney W. Grimes cnt -= m->m_len; 2979df8bae1dSRodney W. Grimes m = m->m_next; 29804dfdffe9SAndre Oppermann if (m == NULL) 2981df8bae1dSRodney W. Grimes break; 2982df8bae1dSRodney W. Grimes } 2983df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 2984df8bae1dSRodney W. Grimes } 2985df8bae1dSRodney W. Grimes 2986df8bae1dSRodney W. Grimes /* 2987df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 2988df8bae1dSRodney W. Grimes * and update averages and current timeout. 2989df8bae1dSRodney W. Grimes */ 29900312fbe9SPoul-Henning Kamp static void 2991ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 2992df8bae1dSRodney W. Grimes { 29938b615593SMarko Zec INIT_VNET_INET(tp->t_inpcb->inp_vnet); 2994ad3f9ab3SAndre Oppermann int delta; 2995233e8c18SGarrett Wollman 29968501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 29972be3bf22SRobert Watson 2998603724d3SBjoern A. Zeeb V_tcpstat.tcps_rttupdated++; 2999233e8c18SGarrett Wollman tp->t_rttupdated++; 3000233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 3001233e8c18SGarrett Wollman /* 3002233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 3003233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 3004233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 3005233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3006233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 3007233e8c18SGarrett Wollman */ 3008233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3009233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3010233e8c18SGarrett Wollman 3011233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 3012233e8c18SGarrett Wollman tp->t_srtt = 1; 3013233e8c18SGarrett Wollman 3014233e8c18SGarrett Wollman /* 3015233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 3016233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 3017233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 3018233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 3019233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 3020233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 3021233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3022233e8c18SGarrett Wollman * rfc793's wired-in beta. 3023233e8c18SGarrett Wollman */ 3024233e8c18SGarrett Wollman if (delta < 0) 3025233e8c18SGarrett Wollman delta = -delta; 3026233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3027233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 3028233e8c18SGarrett Wollman tp->t_rttvar = 1; 30291fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 30301fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3031233e8c18SGarrett Wollman } else { 3032233e8c18SGarrett Wollman /* 3033233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 3034233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 3035233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 3036233e8c18SGarrett Wollman */ 3037233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 3038233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 30391fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3040233e8c18SGarrett Wollman } 30419b8b58e0SJonathan Lemon tp->t_rtttime = 0; 3042df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 3043df8bae1dSRodney W. Grimes 3044df8bae1dSRodney W. Grimes /* 3045df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 3046df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 3047df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 3048df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 3049df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 3050df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 3051df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 3052df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 3053df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 3054df8bae1dSRodney W. Grimes */ 3055233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 30569e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3057df8bae1dSRodney W. Grimes 3058df8bae1dSRodney W. Grimes /* 3059df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 3060df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 3061df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 3062df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 3063df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 3064df8bae1dSRodney W. Grimes */ 3065df8bae1dSRodney W. Grimes tp->t_softerror = 0; 3066df8bae1dSRodney W. Grimes } 3067df8bae1dSRodney W. Grimes 3068df8bae1dSRodney W. Grimes /* 3069df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 3070df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 3071df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 3072df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 3073df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 3074df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 3075df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 3076df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 3077df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 3078df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 3079df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 3080df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 3081df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 3082a0292f23SGarrett Wollman * 3083a0292f23SGarrett Wollman * Also take into account the space needed for options that we 3084a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 3085a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 3086a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 3087a0292f23SGarrett Wollman * data in maxopd. 3088a0292f23SGarrett Wollman * 3089a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 3090a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 3091a0292f23SGarrett Wollman * MSS of our peer. 309297d8d152SAndre Oppermann * 309397d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 309497d8d152SAndre Oppermann * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt(). 3095df8bae1dSRodney W. Grimes */ 3096a0292f23SGarrett Wollman void 309791d6cfa6SBjoern A. Zeeb tcp_mss_update(struct tcpcb *tp, int offer, 309891d6cfa6SBjoern A. Zeeb struct hc_metrics_lite *metricptr, int *mtuflags) 3099df8bae1dSRodney W. Grimes { 31008b615593SMarko Zec INIT_VNET_INET(tp->t_inpcb->inp_vnet); 31013cee92e0SBjoern A. Zeeb int mss; 310297d8d152SAndre Oppermann u_long maxmtu; 3103c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 310497d8d152SAndre Oppermann struct hc_metrics_lite metrics; 3105a0292f23SGarrett Wollman int origoffer = offer; 3106fb59c426SYoshinobu Inoue #ifdef INET6 3107c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3108c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 3109c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3110c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 3111c068736aSJeffrey Hsu #else 3112c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 3113fb59c426SYoshinobu Inoue #endif 3114df8bae1dSRodney W. Grimes 31158501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 31167a3244ccSRobert Watson 3117e8949f74SAndre Oppermann /* Initialize. */ 311897d8d152SAndre Oppermann #ifdef INET6 311997d8d152SAndre Oppermann if (isipv6) { 312091d6cfa6SBjoern A. Zeeb maxmtu = tcp_maxmtu6(&inp->inp_inc, mtuflags); 3121603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_v6mssdflt; 312297d8d152SAndre Oppermann } else 312397d8d152SAndre Oppermann #endif 312497d8d152SAndre Oppermann { 312591d6cfa6SBjoern A. Zeeb maxmtu = tcp_maxmtu(&inp->inp_inc, mtuflags); 3126603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_mssdflt; 3127df8bae1dSRodney W. Grimes } 3128df8bae1dSRodney W. Grimes 312997d8d152SAndre Oppermann /* 3130e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 313197d8d152SAndre Oppermann */ 31326f01cac6SBjoern A. Zeeb if (maxmtu == 0) { 31336f01cac6SBjoern A. Zeeb /* 31348e5c87f4SBjoern A. Zeeb * In case we return early we need to initialize metrics 31356f01cac6SBjoern A. Zeeb * to a defined state as tcp_hc_get() would do for us 31366f01cac6SBjoern A. Zeeb * if there was no cache hit. 31376f01cac6SBjoern A. Zeeb */ 31386f01cac6SBjoern A. Zeeb if (metricptr != NULL) 31396f01cac6SBjoern A. Zeeb bzero(metricptr, sizeof(struct hc_metrics_lite)); 314097d8d152SAndre Oppermann return; 31416f01cac6SBjoern A. Zeeb } 314297d8d152SAndre Oppermann 3143e8949f74SAndre Oppermann /* What have we got? */ 314497d8d152SAndre Oppermann switch (offer) { 314597d8d152SAndre Oppermann case 0: 314697d8d152SAndre Oppermann /* 314797d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 3148c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 3149c3b02504SBjoern A. Zeeb * already assigned to t_maxopd above. 315097d8d152SAndre Oppermann */ 3151c3b02504SBjoern A. Zeeb offer = tp->t_maxopd; 315297d8d152SAndre Oppermann break; 315397d8d152SAndre Oppermann 315497d8d152SAndre Oppermann case -1: 3155a0292f23SGarrett Wollman /* 3156c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 3157a0292f23SGarrett Wollman */ 315897d8d152SAndre Oppermann /* FALLTHROUGH */ 315997d8d152SAndre Oppermann 316097d8d152SAndre Oppermann default: 3161a0292f23SGarrett Wollman /* 316253369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 316353369ac9SAndre Oppermann * to at least minmss. 316453369ac9SAndre Oppermann */ 3165603724d3SBjoern A. Zeeb offer = max(offer, V_tcp_minmss); 316697d8d152SAndre Oppermann } 3167a0292f23SGarrett Wollman 3168df8bae1dSRodney W. Grimes /* 3169e8949f74SAndre Oppermann * rmx information is now retrieved from tcp_hostcache. 3170df8bae1dSRodney W. Grimes */ 317197d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 31723cee92e0SBjoern A. Zeeb if (metricptr != NULL) 31733cee92e0SBjoern A. Zeeb bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 317497d8d152SAndre Oppermann 3175df8bae1dSRodney W. Grimes /* 3176e8949f74SAndre Oppermann * If there's a discovered mtu int tcp hostcache, use it 3177fb59c426SYoshinobu Inoue * else, use the link mtu. 3178df8bae1dSRodney W. Grimes */ 317997d8d152SAndre Oppermann if (metrics.rmx_mtu) 31802d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3181c068736aSJeffrey Hsu else { 318231b3783cSHajimu UMEMOTO #ifdef INET6 3183fb59c426SYoshinobu Inoue if (isipv6) { 318497d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3185603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 318697d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 3187603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_v6mssdflt); 3188b3399803SHajimu UMEMOTO } else 3189b3399803SHajimu UMEMOTO #endif 319097d8d152SAndre Oppermann { 319197d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3192603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 319397d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 3194603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_mssdflt); 3195a0292f23SGarrett Wollman } 31963cee92e0SBjoern A. Zeeb /* 31973cee92e0SBjoern A. Zeeb * XXX - The above conditional (mss = maxmtu - min_protoh) 31983cee92e0SBjoern A. Zeeb * probably violates the TCP spec. 31993cee92e0SBjoern A. Zeeb * The problem is that, since we don't know the 32003cee92e0SBjoern A. Zeeb * other end's MSS, we are supposed to use a conservative 32013cee92e0SBjoern A. Zeeb * default. But, if we do that, then MTU discovery will 32023cee92e0SBjoern A. Zeeb * never actually take place, because the conservative 32033cee92e0SBjoern A. Zeeb * default is much less than the MTUs typically seen 32043cee92e0SBjoern A. Zeeb * on the Internet today. For the moment, we'll sweep 32053cee92e0SBjoern A. Zeeb * this under the carpet. 32063cee92e0SBjoern A. Zeeb * 32073cee92e0SBjoern A. Zeeb * The conservative default might not actually be a problem 32083cee92e0SBjoern A. Zeeb * if the only case this occurs is when sending an initial 32093cee92e0SBjoern A. Zeeb * SYN with options and data to a host we've never talked 32103cee92e0SBjoern A. Zeeb * to before. Then, they will reply with an MSS value which 32113cee92e0SBjoern A. Zeeb * will get recorded and the new parameters should get 32123cee92e0SBjoern A. Zeeb * recomputed. For Further Study. 32133cee92e0SBjoern A. Zeeb */ 321497d8d152SAndre Oppermann } 3215a0292f23SGarrett Wollman mss = min(mss, offer); 321697d8d152SAndre Oppermann 3217a0292f23SGarrett Wollman /* 32183cee92e0SBjoern A. Zeeb * Sanity check: make sure that maxopd will be large 32193cee92e0SBjoern A. Zeeb * enough to allow some data on segments even if the 32203cee92e0SBjoern A. Zeeb * all the option space is used (40bytes). Otherwise 32213cee92e0SBjoern A. Zeeb * funny things may happen in tcp_output. 32223cee92e0SBjoern A. Zeeb */ 32233cee92e0SBjoern A. Zeeb mss = max(mss, 64); 32243cee92e0SBjoern A. Zeeb 32253cee92e0SBjoern A. Zeeb /* 3226a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 3227a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 3228a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 3229a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 3230a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 3231a0292f23SGarrett Wollman */ 3232a0292f23SGarrett Wollman tp->t_maxopd = mss; 3233a0292f23SGarrett Wollman 3234a0292f23SGarrett Wollman /* 3235e8949f74SAndre Oppermann * origoffer==-1 indicates that no segments were received yet. 3236c94c54e4SAndre Oppermann * In this case we just guess. 3237a0292f23SGarrett Wollman */ 3238a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 3239a0292f23SGarrett Wollman (origoffer == -1 || 3240a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 3241a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 3242a0292f23SGarrett Wollman 3243df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 3244df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 3245df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 3246df8bae1dSRodney W. Grimes #else 3247df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 3248df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 3249df8bae1dSRodney W. Grimes #endif 325097d8d152SAndre Oppermann tp->t_maxseg = mss; 32513cee92e0SBjoern A. Zeeb } 32523cee92e0SBjoern A. Zeeb 32533cee92e0SBjoern A. Zeeb void 32543cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer) 32553cee92e0SBjoern A. Zeeb { 32563cee92e0SBjoern A. Zeeb int rtt, mss; 32573cee92e0SBjoern A. Zeeb u_long bufsize; 32583cee92e0SBjoern A. Zeeb struct inpcb *inp; 32593cee92e0SBjoern A. Zeeb struct socket *so; 32603cee92e0SBjoern A. Zeeb struct hc_metrics_lite metrics; 326191d6cfa6SBjoern A. Zeeb int mtuflags = 0; 32623cee92e0SBjoern A. Zeeb #ifdef INET6 32633cee92e0SBjoern A. Zeeb int isipv6; 32643cee92e0SBjoern A. Zeeb #endif 32653cee92e0SBjoern A. Zeeb KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 326697021c24SMarko Zec INIT_VNET_INET(tp->t_vnet); 32673cee92e0SBjoern A. Zeeb 326891d6cfa6SBjoern A. Zeeb tcp_mss_update(tp, offer, &metrics, &mtuflags); 32693cee92e0SBjoern A. Zeeb 32703cee92e0SBjoern A. Zeeb mss = tp->t_maxseg; 32713cee92e0SBjoern A. Zeeb inp = tp->t_inpcb; 32723cee92e0SBjoern A. Zeeb #ifdef INET6 32733cee92e0SBjoern A. Zeeb isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 32743cee92e0SBjoern A. Zeeb #endif 327597d8d152SAndre Oppermann 3276df8bae1dSRodney W. Grimes /* 327797d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 327897d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 327997d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 328097d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 328197d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3282df8bae1dSRodney W. Grimes */ 3283c3b02504SBjoern A. Zeeb so = inp->inp_socket; 32843f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 328597d8d152SAndre Oppermann if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe) 328697d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 328797d8d152SAndre Oppermann else 3288df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3289df8bae1dSRodney W. Grimes if (bufsize < mss) 3290df8bae1dSRodney W. Grimes mss = bufsize; 3291df8bae1dSRodney W. Grimes else { 3292df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3293df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3294df8bae1dSRodney W. Grimes bufsize = sb_max; 329588c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 32963f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3297df8bae1dSRodney W. Grimes } 32983f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 3299df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 3300df8bae1dSRodney W. Grimes 33013f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 330297d8d152SAndre Oppermann if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe) 330397d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 330497d8d152SAndre Oppermann else 3305df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3306df8bae1dSRodney W. Grimes if (bufsize > mss) { 3307df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3308df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3309df8bae1dSRodney W. Grimes bufsize = sb_max; 331088c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 33113f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3312df8bae1dSRodney W. Grimes } 33133f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 3314a0292f23SGarrett Wollman /* 3315e8949f74SAndre Oppermann * While we're here, check the others too. 3316a0292f23SGarrett Wollman */ 331797d8d152SAndre Oppermann if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 331897d8d152SAndre Oppermann tp->t_srtt = rtt; 331997d8d152SAndre Oppermann tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 3320603724d3SBjoern A. Zeeb V_tcpstat.tcps_usedrtt++; 332197d8d152SAndre Oppermann if (metrics.rmx_rttvar) { 332297d8d152SAndre Oppermann tp->t_rttvar = metrics.rmx_rttvar; 3323603724d3SBjoern A. Zeeb V_tcpstat.tcps_usedrttvar++; 332497d8d152SAndre Oppermann } else { 332597d8d152SAndre Oppermann /* default variation is +- 1 rtt */ 332697d8d152SAndre Oppermann tp->t_rttvar = 332797d8d152SAndre Oppermann tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 332897d8d152SAndre Oppermann } 332997d8d152SAndre Oppermann TCPT_RANGESET(tp->t_rxtcur, 333097d8d152SAndre Oppermann ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 333197d8d152SAndre Oppermann tp->t_rttmin, TCPTV_REXMTMAX); 333297d8d152SAndre Oppermann } 333397d8d152SAndre Oppermann if (metrics.rmx_ssthresh) { 3334df8bae1dSRodney W. Grimes /* 3335df8bae1dSRodney W. Grimes * There's some sort of gateway or interface 3336df8bae1dSRodney W. Grimes * buffer limit on the path. Use this to set 3337df8bae1dSRodney W. Grimes * the slow start threshhold, but set the 3338df8bae1dSRodney W. Grimes * threshold to no less than 2*mss. 3339df8bae1dSRodney W. Grimes */ 334097d8d152SAndre Oppermann tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh); 3341603724d3SBjoern A. Zeeb V_tcpstat.tcps_usedssthresh++; 3342df8bae1dSRodney W. Grimes } 334397d8d152SAndre Oppermann if (metrics.rmx_bandwidth) 334497d8d152SAndre Oppermann tp->snd_bandwidth = metrics.rmx_bandwidth; 334597d8d152SAndre Oppermann 334697d8d152SAndre Oppermann /* 334797d8d152SAndre Oppermann * Set the slow-start flight size depending on whether this 334897d8d152SAndre Oppermann * is a local network or not. 334997d8d152SAndre Oppermann * 335097d8d152SAndre Oppermann * Extend this so we cache the cwnd too and retrieve it here. 335197d8d152SAndre Oppermann * Make cwnd even bigger than RFC3390 suggests but only if we 335297d8d152SAndre Oppermann * have previous experience with the remote host. Be careful 335397d8d152SAndre Oppermann * not make cwnd bigger than remote receive window or our own 335497d8d152SAndre Oppermann * send socket buffer. Maybe put some additional upper bound 335597d8d152SAndre Oppermann * on the retrieved cwnd. Should do incremental updates to 335697d8d152SAndre Oppermann * hostcache when cwnd collapses so next connection doesn't 335797d8d152SAndre Oppermann * overloads the path again. 335897d8d152SAndre Oppermann * 335997d8d152SAndre Oppermann * RFC3390 says only do this if SYN or SYN/ACK didn't got lost. 336097d8d152SAndre Oppermann * We currently check only in syncache_socket for that. 336197d8d152SAndre Oppermann */ 336297d8d152SAndre Oppermann #define TCP_METRICS_CWND 336397d8d152SAndre Oppermann #ifdef TCP_METRICS_CWND 336497d8d152SAndre Oppermann if (metrics.rmx_cwnd) 336597d8d152SAndre Oppermann tp->snd_cwnd = max(mss, 336697d8d152SAndre Oppermann min(metrics.rmx_cwnd / 2, 336797d8d152SAndre Oppermann min(tp->snd_wnd, so->so_snd.sb_hiwat))); 336897d8d152SAndre Oppermann else 336997d8d152SAndre Oppermann #endif 3370603724d3SBjoern A. Zeeb if (V_tcp_do_rfc3390) 337197d8d152SAndre Oppermann tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380)); 337297d8d152SAndre Oppermann #ifdef INET6 337397d8d152SAndre Oppermann else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) || 337497d8d152SAndre Oppermann (!isipv6 && in_localaddr(inp->inp_faddr))) 3375943ae302SAndre Oppermann #else 3376943ae302SAndre Oppermann else if (in_localaddr(inp->inp_faddr)) 337797d8d152SAndre Oppermann #endif 3378603724d3SBjoern A. Zeeb tp->snd_cwnd = mss * V_ss_fltsz_local; 337997d8d152SAndre Oppermann else 3380603724d3SBjoern A. Zeeb tp->snd_cwnd = mss * V_ss_fltsz; 338191d6cfa6SBjoern A. Zeeb 338291d6cfa6SBjoern A. Zeeb /* Check the interface for TSO capabilities. */ 338391d6cfa6SBjoern A. Zeeb if (mtuflags & CSUM_TSO) 338491d6cfa6SBjoern A. Zeeb tp->t_flags |= TF_TSO; 3385a0292f23SGarrett Wollman } 3386a0292f23SGarrett Wollman 3387a0292f23SGarrett Wollman /* 3388a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3389a0292f23SGarrett Wollman */ 3390a0292f23SGarrett Wollman int 3391ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 3392a0292f23SGarrett Wollman { 33938b615593SMarko Zec INIT_VNET_INET(curvnet); 339497d8d152SAndre Oppermann int mss = 0; 339597d8d152SAndre Oppermann u_long maxmtu = 0; 339697d8d152SAndre Oppermann u_long thcmtu = 0; 339797d8d152SAndre Oppermann size_t min_protoh; 3398a0292f23SGarrett Wollman 339997d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3400a0292f23SGarrett Wollman 340131b3783cSHajimu UMEMOTO #ifdef INET6 3402dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3403603724d3SBjoern A. Zeeb mss = V_tcp_v6mssdflt; 3404233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 340597d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 340697d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 340797d8d152SAndre Oppermann } else 340831b3783cSHajimu UMEMOTO #endif 340997d8d152SAndre Oppermann { 3410603724d3SBjoern A. Zeeb mss = V_tcp_mssdflt; 3411233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 341297d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 341397d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 341497d8d152SAndre Oppermann } 341597d8d152SAndre Oppermann if (maxmtu && thcmtu) 341697d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 341797d8d152SAndre Oppermann else if (maxmtu || thcmtu) 341897d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 341997d8d152SAndre Oppermann 342097d8d152SAndre Oppermann return (mss); 3421df8bae1dSRodney W. Grimes } 342246f58482SJonathan Lemon 342346f58482SJonathan Lemon 342446f58482SJonathan Lemon /* 3425c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3426c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3427c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3428c068736aSJeffrey Hsu * be started again. 342946f58482SJonathan Lemon */ 3430c068736aSJeffrey Hsu static void 3431ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 343246f58482SJonathan Lemon { 343346f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 343446f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 343546f58482SJonathan Lemon 34368501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 34377a3244ccSRobert Watson 3438b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 343946f58482SJonathan Lemon tp->t_rtttime = 0; 344046f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 34416b2a5f92SJayanth Vijayaraghavan /* 3442c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3443c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 34446b2a5f92SJayanth Vijayaraghavan */ 34456b2a5f92SJayanth Vijayaraghavan tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 3446a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 34476b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 344846f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 344946f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 345046f58482SJonathan Lemon tp->snd_nxt = onxt; 345146f58482SJonathan Lemon /* 345246f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 345346f58482SJonathan Lemon * not updated yet. 345446f58482SJonathan Lemon */ 3455d7587117SPaul Saab if (tp->snd_cwnd > th->th_ack - tp->snd_una) 3456d7587117SPaul Saab tp->snd_cwnd -= th->th_ack - tp->snd_una; 3457d7587117SPaul Saab else 3458d7587117SPaul Saab tp->snd_cwnd = 0; 3459d7587117SPaul Saab tp->snd_cwnd += tp->t_maxseg; 346046f58482SJonathan Lemon } 3461