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> 54df8bae1dSRodney W. Grimes 55e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 56e79adb8eSGarrett Wollman 5712e2e970SAndre Oppermann #include <vm/uma.h> 5812e2e970SAndre Oppermann 59df8bae1dSRodney W. Grimes #include <net/if.h> 60df8bae1dSRodney W. Grimes #include <net/route.h> 61df8bae1dSRodney W. Grimes 62773673c1SAndre Oppermann #define TCPSTATES /* for logging */ 63773673c1SAndre Oppermann 64df8bae1dSRodney W. Grimes #include <netinet/in.h> 65960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 66df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 67960ed29cSSeigo Tanimura #include <netinet/in_var.h> 68df8bae1dSRodney W. Grimes #include <netinet/ip.h> 698e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 70686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 71df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 72ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 73686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 74686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h> 75686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 76960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h> 77960ed29cSSeigo Tanimura #include <netinet6/nd6.h> 78df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 79df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 80df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 81df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 82df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 83fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 84df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 85c325962bSMike Silbersack #include <netinet/tcp_syncache.h> 86610ee2f9SDavid Greenman #ifdef TCPDEBUG 87df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 88fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */ 89fb59c426SYoshinobu Inoue 90b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 91b9234fafSSam Leffler #include <netipsec/ipsec.h> 92b9234fafSSam Leffler #include <netipsec/ipsec6.h> 93b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 94b9234fafSSam Leffler 95db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 96db4f9cc7SJonathan Lemon 97aed55708SRobert Watson #include <security/mac/mac_framework.h> 98aed55708SRobert Watson 99c068736aSJeffrey Hsu static const int tcprexmtthresh = 3; 1000312fbe9SPoul-Henning Kamp 1012f96f1f4SGarrett Wollman struct tcpstat tcpstat; 102c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW, 1033d177f46SBill Fumerola &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 1040312fbe9SPoul-Henning Kamp 105773673c1SAndre Oppermann int tcp_log_in_vain = 0; 106816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 107574b6964SAndre Oppermann &tcp_log_in_vain, 0, "Log all incoming TCP segments to closed ports"); 108816a3d83SPoul-Henning Kamp 10916f7f31fSGeoff Rehmet static int blackhole = 0; 11016f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 111574b6964SAndre Oppermann &blackhole, 0, "Do not send RST on segments to closed ports"); 11216f7f31fSGeoff Rehmet 113f498eeeeSDavid Greenman int tcp_delack_enabled = 1; 11484e33c9eSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 1153d177f46SBill Fumerola &tcp_delack_enabled, 0, 1163d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 117f498eeeeSDavid Greenman 118f8613305SDag-Erling Smørgrav static int drop_synfin = 0; 119f8613305SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, 120f8613305SDag-Erling Smørgrav &drop_synfin, 0, "Drop TCP packets with SYN+FIN set"); 121f8613305SDag-Erling Smørgrav 122dba7bc6aSAndre Oppermann static int tcp_do_rfc3042 = 1; 123582a954bSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW, 124582a954bSJeffrey Hsu &tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)"); 125582a954bSJeffrey Hsu 126dba7bc6aSAndre Oppermann static int tcp_do_rfc3390 = 1; 127da3a8a1aSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW, 128da3a8a1aSJeffrey Hsu &tcp_do_rfc3390, 0, 129da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 130da3a8a1aSJeffrey Hsu 131a69968eeSMike Silbersack static int tcp_insecure_rst = 0; 132a69968eeSMike Silbersack SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_RW, 133a69968eeSMike Silbersack &tcp_insecure_rst, 0, 1346489fe65SAndre Oppermann "Follow the old (insecure) criteria for accepting RST packets"); 135a69968eeSMike Silbersack 1366741ecf5SAndre Oppermann int tcp_do_autorcvbuf = 1; 1376741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_RW, 1386741ecf5SAndre Oppermann &tcp_do_autorcvbuf, 0, "Enable automatic receive buffer sizing"); 1396741ecf5SAndre Oppermann 1406741ecf5SAndre Oppermann int tcp_autorcvbuf_inc = 16*1024; 1416741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_RW, 1426489fe65SAndre Oppermann &tcp_autorcvbuf_inc, 0, 1436489fe65SAndre Oppermann "Incrementor step size of automatic receive buffer"); 1446741ecf5SAndre Oppermann 1456741ecf5SAndre Oppermann int tcp_autorcvbuf_max = 256*1024; 1466741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_RW, 1476741ecf5SAndre Oppermann &tcp_autorcvbuf_max, 0, "Max size of automatic receive buffer"); 1486741ecf5SAndre Oppermann 14915bd2b43SDavid Greenman struct inpcbhead tcb; 150fb59c426SYoshinobu Inoue #define tcb6 tcb /* for KAME src sync over BSD*'s */ 15115bd2b43SDavid Greenman struct inpcbinfo tcbinfo; 152df8bae1dSRodney W. Grimes 1535a53ca16SPaul Saab static void tcp_dooptions(struct tcpopt *, u_char *, int, int); 154679d9708SAndre Oppermann static void tcp_do_segment(struct mbuf *, struct tcphdr *, 155302ce8d6SAndre Oppermann struct socket *, struct tcpcb *, int, int); 156302ce8d6SAndre Oppermann static void tcp_dropwithreset(struct mbuf *, struct tcphdr *, 157302ce8d6SAndre Oppermann struct tcpcb *, int, int); 1584d77a549SAlfred Perlstein static void tcp_pulloutofband(struct socket *, 1594d77a549SAlfred Perlstein struct tcphdr *, struct mbuf *, int); 1604d77a549SAlfred Perlstein static void tcp_xmit_timer(struct tcpcb *, int); 161c068736aSJeffrey Hsu static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 1620312fbe9SPoul-Henning Kamp 163fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 164fb59c426SYoshinobu Inoue #ifdef INET6 165fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \ 166fb59c426SYoshinobu Inoue do { \ 167fb59c426SYoshinobu Inoue if ((tp) && (tp)->t_inpcb && \ 16897d8d152SAndre Oppermann ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \ 16997d8d152SAndre Oppermann nd6_nud_hint(NULL, NULL, 0); \ 170fb59c426SYoshinobu Inoue } while (0) 171fb59c426SYoshinobu Inoue #else 172fb59c426SYoshinobu Inoue #define ND6_HINT(tp) 173fb59c426SYoshinobu Inoue #endif 174df8bae1dSRodney W. Grimes 175df8bae1dSRodney W. Grimes /* 176262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 177262c1c1aSMatthew Dillon * - there is no delayed ack timer in progress and 178262c1c1aSMatthew Dillon * - our last ack wasn't a 0-sized window. We never want to delay 1793bfd6421SJonathan Lemon * the ack that opens up a 0-sized window and 1803bfd6421SJonathan Lemon * - delayed acks are enabled or 1813bfd6421SJonathan Lemon * - this is a half-synchronized T/TCP connection. 182d8c85a26SJonathan Lemon */ 183d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \ 184b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 185a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 1863bfd6421SJonathan Lemon (tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 187d8c85a26SJonathan Lemon 188df8bae1dSRodney W. Grimes 189df8bae1dSRodney W. Grimes /* 1908d573cc1SAndre Oppermann * TCP input handling is split into multiple parts: 1918d573cc1SAndre Oppermann * tcp6_input is a thin wrapper around tcp_input for the extended 1928d573cc1SAndre Oppermann * ip6_protox[] call format in ip6_input 1938d573cc1SAndre Oppermann * tcp_input handles primary segment validation, inpcb lookup and 1948d573cc1SAndre Oppermann * SYN processing on listen sockets 1958d573cc1SAndre Oppermann * tcp_do_segment processes the ACK and text of the segment for 1968d573cc1SAndre Oppermann * establishing, established and closing connections 197df8bae1dSRodney W. Grimes */ 198fb59c426SYoshinobu Inoue #ifdef INET6 199fb59c426SYoshinobu Inoue int 200ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto) 201fb59c426SYoshinobu Inoue { 202ad3f9ab3SAndre Oppermann struct mbuf *m = *mp; 20333841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 204fb59c426SYoshinobu Inoue 205fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 206fb59c426SYoshinobu Inoue 207fb59c426SYoshinobu Inoue /* 208fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 209fb59c426SYoshinobu Inoue * better place to put this in? 210fb59c426SYoshinobu Inoue */ 21133841545SHajimu UMEMOTO ia6 = ip6_getdstifaddr(m); 21233841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 213fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 214fb59c426SYoshinobu Inoue 215fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 216fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 217fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 218fb59c426SYoshinobu Inoue return IPPROTO_DONE; 219fb59c426SYoshinobu Inoue } 220fb59c426SYoshinobu Inoue 221f0ffb944SJulian Elischer tcp_input(m, *offp); 222fb59c426SYoshinobu Inoue return IPPROTO_DONE; 223fb59c426SYoshinobu Inoue } 224fb59c426SYoshinobu Inoue #endif 225fb59c426SYoshinobu Inoue 226df8bae1dSRodney W. Grimes void 227ad3f9ab3SAndre Oppermann tcp_input(struct mbuf *m, int off0) 228df8bae1dSRodney W. Grimes { 229ad3f9ab3SAndre Oppermann struct tcphdr *th; 230ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 231ad3f9ab3SAndre Oppermann struct ipovly *ipov; 232ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 233302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 234302ce8d6SAndre Oppermann struct socket *so = NULL; 235e79adb8eSGarrett Wollman u_char *optp = NULL; 23626f9a767SRodney W. Grimes int optlen = 0; 237a0194ef1SBruce M Simpson int len, tlen, off; 238fb59c426SYoshinobu Inoue int drop_hdrlen; 239ad3f9ab3SAndre Oppermann int thflags; 240302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 2419b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 2429b932e9eSAndre Oppermann struct m_tag *fwd_tag; 2439b932e9eSAndre Oppermann #endif 244c068736aSJeffrey Hsu #ifdef INET6 245302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 246c068736aSJeffrey Hsu int isipv6; 247c068736aSJeffrey Hsu #else 248a160e630SAndre Oppermann const void *ip6 = NULL; 249c068736aSJeffrey Hsu const int isipv6 = 0; 250c068736aSJeffrey Hsu #endif 251302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 252a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 253f76fcf6dSJeffrey Hsu 254610ee2f9SDavid Greenman #ifdef TCPDEBUG 255c068736aSJeffrey Hsu /* 256c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 257c068736aSJeffrey Hsu * now IPv6. 258c068736aSJeffrey Hsu */ 25914739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 260410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 261610ee2f9SDavid Greenman short ostate = 0; 262610ee2f9SDavid Greenman #endif 263c068736aSJeffrey Hsu 264fb59c426SYoshinobu Inoue #ifdef INET6 265fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 266fb59c426SYoshinobu Inoue #endif 267a0292f23SGarrett Wollman 268302ce8d6SAndre Oppermann to.to_flags = 0; 269df8bae1dSRodney W. Grimes tcpstat.tcps_rcvtotal++; 270fb59c426SYoshinobu Inoue 271fb59c426SYoshinobu Inoue if (isipv6) { 27204d3a452SHajimu UMEMOTO #ifdef INET6 2738d573cc1SAndre Oppermann /* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */ 274fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 275fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 276fb59c426SYoshinobu Inoue if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 277fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbadsum++; 278fb59c426SYoshinobu Inoue goto drop; 279fb59c426SYoshinobu Inoue } 280fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 28133841545SHajimu UMEMOTO 28233841545SHajimu UMEMOTO /* 28333841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 28433841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 28533841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 28633841545SHajimu UMEMOTO * 28733841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 28833841545SHajimu UMEMOTO * already dropped in ip6_input. 28933841545SHajimu UMEMOTO */ 29033841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 29133841545SHajimu UMEMOTO /* XXX stat */ 29233841545SHajimu UMEMOTO goto drop; 29333841545SHajimu UMEMOTO } 29404d3a452SHajimu UMEMOTO #else 2958d573cc1SAndre Oppermann th = NULL; /* XXX: Avoid compiler warning. */ 29604d3a452SHajimu UMEMOTO #endif 297c068736aSJeffrey Hsu } else { 298df8bae1dSRodney W. Grimes /* 299df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 300df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 301df8bae1dSRodney W. Grimes */ 302fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 303df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 304fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 305fb59c426SYoshinobu Inoue } 306df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 3074dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 3084dfdffe9SAndre Oppermann == NULL) { 309df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 310df8bae1dSRodney W. Grimes return; 311df8bae1dSRodney W. Grimes } 312df8bae1dSRodney W. Grimes } 313fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 314fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 315db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 316db4f9cc7SJonathan Lemon tlen = ip->ip_len; 317df8bae1dSRodney W. Grimes 318db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 319db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 320db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 321db4f9cc7SJonathan Lemon else 322db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 323c068736aSJeffrey Hsu ip->ip_dst.s_addr, 324c068736aSJeffrey Hsu htonl(m->m_pkthdr.csum_data + 325c068736aSJeffrey Hsu ip->ip_len + 326c068736aSJeffrey Hsu IPPROTO_TCP)); 327db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 3283c653157SHartmut Brandt #ifdef TCPDEBUG 3293c653157SHartmut Brandt ipov->ih_len = (u_short)tlen; 3303c653157SHartmut Brandt ipov->ih_len = htons(ipov->ih_len); 3313c653157SHartmut Brandt #endif 332db4f9cc7SJonathan Lemon } else { 333df8bae1dSRodney W. Grimes /* 334df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 335df8bae1dSRodney W. Grimes */ 336df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 337fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 338fb59c426SYoshinobu Inoue ipov->ih_len = (u_short)tlen; 339fd8e4ebcSMike Barcroft ipov->ih_len = htons(ipov->ih_len); 340fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 341db4f9cc7SJonathan Lemon } 342fb59c426SYoshinobu Inoue if (th->th_sum) { 343df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadsum++; 344df8bae1dSRodney W. Grimes goto drop; 345df8bae1dSRodney W. Grimes } 346fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 347fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 348fb59c426SYoshinobu Inoue } 349df8bae1dSRodney W. Grimes 350df8bae1dSRodney W. Grimes /* 351df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 352df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 353df8bae1dSRodney W. Grimes */ 354fb59c426SYoshinobu Inoue off = th->th_off << 2; 355df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 356df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadoff++; 357df8bae1dSRodney W. Grimes goto drop; 358df8bae1dSRodney W. Grimes } 359fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 360df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 361fb59c426SYoshinobu Inoue if (isipv6) { 36204d3a452SHajimu UMEMOTO #ifdef INET6 363fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 364fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 365fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 36604d3a452SHajimu UMEMOTO #endif 367c068736aSJeffrey Hsu } else { 368df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 369c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 3704dfdffe9SAndre Oppermann == NULL) { 371df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 372df8bae1dSRodney W. Grimes return; 373df8bae1dSRodney W. Grimes } 374fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 375fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 376fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 377fb59c426SYoshinobu Inoue } 378df8bae1dSRodney W. Grimes } 379df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 380fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 381df8bae1dSRodney W. Grimes } 382fb59c426SYoshinobu Inoue thflags = th->th_flags; 383df8bae1dSRodney W. Grimes 384e46cd3d4SDag-Erling Smørgrav /* 385df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 386df8bae1dSRodney W. Grimes */ 387fd8e4ebcSMike Barcroft th->th_seq = ntohl(th->th_seq); 388fd8e4ebcSMike Barcroft th->th_ack = ntohl(th->th_ack); 389fd8e4ebcSMike Barcroft th->th_win = ntohs(th->th_win); 390fd8e4ebcSMike Barcroft th->th_urp = ntohs(th->th_urp); 391df8bae1dSRodney W. Grimes 392df8bae1dSRodney W. Grimes /* 393794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 394755c1f07SAndras Olah */ 395fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 396755c1f07SAndras Olah 397755c1f07SAndras Olah /* 398df8bae1dSRodney W. Grimes * Locate pcb for segment. 399df8bae1dSRodney W. Grimes */ 400f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 401df8bae1dSRodney W. Grimes findpcb: 402302ce8d6SAndre Oppermann INP_INFO_WLOCK_ASSERT(&tcbinfo); 4039b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 4048d573cc1SAndre Oppermann /* 4058d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 4068d573cc1SAndre Oppermann */ 4079b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 4089b932e9eSAndre Oppermann 4099b932e9eSAndre Oppermann if (fwd_tag != NULL && isipv6 == 0) { /* IPv6 support is not yet */ 4109b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 4119b932e9eSAndre Oppermann 4129b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 413f9e354dfSJulian Elischer /* 4142b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 415f9e354dfSJulian Elischer * already got one like this? 416f9e354dfSJulian Elischer */ 4179b932e9eSAndre Oppermann inp = in_pcblookup_hash(&tcbinfo, 4189b932e9eSAndre Oppermann ip->ip_src, th->th_sport, 419c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 420c068736aSJeffrey Hsu 0, m->m_pkthdr.rcvif); 421f9e354dfSJulian Elischer if (!inp) { 4229b932e9eSAndre Oppermann /* It's new. Try to find the ambushing socket. */ 423f9e354dfSJulian Elischer inp = in_pcblookup_hash(&tcbinfo, 424fb59c426SYoshinobu Inoue ip->ip_src, th->th_sport, 4252b25acc1SLuigi Rizzo next_hop->sin_addr, 426c068736aSJeffrey Hsu next_hop->sin_port ? 427c068736aSJeffrey Hsu ntohs(next_hop->sin_port) : 428c068736aSJeffrey Hsu th->th_dport, 429421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 430421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 431f9e354dfSJulian Elischer } 4329b932e9eSAndre Oppermann /* Remove the tag from the packet. We don't need it anymore. */ 4339b932e9eSAndre Oppermann m_tag_delete(m, fwd_tag); 434b10fbdeaSAndre Oppermann } else 4359b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 436b10fbdeaSAndre Oppermann { 43704d3a452SHajimu UMEMOTO if (isipv6) { 43804d3a452SHajimu UMEMOTO #ifdef INET6 439c068736aSJeffrey Hsu inp = in6_pcblookup_hash(&tcbinfo, 440c068736aSJeffrey Hsu &ip6->ip6_src, th->th_sport, 441c068736aSJeffrey Hsu &ip6->ip6_dst, th->th_dport, 442421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 443421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 44404d3a452SHajimu UMEMOTO #endif 44504d3a452SHajimu UMEMOTO } else 446c068736aSJeffrey Hsu inp = in_pcblookup_hash(&tcbinfo, 447c068736aSJeffrey Hsu ip->ip_src, th->th_sport, 448c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 449421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 450421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 451fb59c426SYoshinobu Inoue } 452f9e354dfSJulian Elischer 453df8bae1dSRodney W. Grimes /* 454574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 455574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 4568b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 457df8bae1dSRodney W. Grimes */ 458816a3d83SPoul-Henning Kamp if (inp == NULL) { 459574b6964SAndre Oppermann /* 460574b6964SAndre Oppermann * Log communication attempts to ports that are not 461574b6964SAndre Oppermann * in use. 462574b6964SAndre Oppermann */ 463574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 464574b6964SAndre Oppermann tcp_log_in_vain == 2) { 4659fb5d4c0SPeter Wemm if ((s = tcp_log_addrs(NULL, th, (void *)ip, ip6))) 466df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 467df541e5fSAndre Oppermann "to closed port\n", s, __func__); 4682e4e1b4cSGeoff Rehmet } 469574b6964SAndre Oppermann /* 470574b6964SAndre Oppermann * When blackholing do not respond with a RST but 471574b6964SAndre Oppermann * completely ignore the segment and drop it. 472574b6964SAndre Oppermann */ 473574b6964SAndre Oppermann if ((blackhole == 1 && (thflags & TH_SYN)) || 474574b6964SAndre Oppermann blackhole == 2) 4751929eae1SAndre Oppermann goto dropunlock; 476574b6964SAndre Oppermann 477a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 478a57815efSBosko Milekic goto dropwithreset; 479816a3d83SPoul-Henning Kamp } 4808501a69cSRobert Watson INP_WLOCK(inp); 481936cd18dSAndre Oppermann 482a2589465SKen Smith #ifdef IPSEC 483a2589465SKen Smith #ifdef INET6 484a2589465SKen Smith if (isipv6 && ipsec6_in_reject(m, inp)) { 485a2589465SKen Smith ipsec6stat.in_polvio++; 486a2589465SKen Smith goto dropunlock; 487a2589465SKen Smith } else 488a2589465SKen Smith #endif /* INET6 */ 489a2589465SKen Smith if (ipsec4_in_reject(m, inp) != 0) { 490a2589465SKen Smith ipsec4stat.in_polvio++; 491a2589465SKen Smith goto dropunlock; 492a2589465SKen Smith } 493a2589465SKen Smith #endif /* IPSEC */ 494a2589465SKen Smith 4958d573cc1SAndre Oppermann /* 4968d573cc1SAndre Oppermann * Check the minimum TTL for socket. 4978d573cc1SAndre Oppermann */ 49834f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 49934f83c52SGeorge V. Neville-Neil #ifdef INET6 50034f83c52SGeorge V. Neville-Neil if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim) 501302ce8d6SAndre Oppermann goto dropunlock; 50202707462SAndre Oppermann else 50334f83c52SGeorge V. Neville-Neil #endif 50434f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 505302ce8d6SAndre Oppermann goto dropunlock; 50634f83c52SGeorge V. Neville-Neil } 507936cd18dSAndre Oppermann 508340c35deSJonathan Lemon /* 509794235b7SAndre Oppermann * A previous connection in TIMEWAIT state is supposed to catch 510794235b7SAndre Oppermann * stray or duplicate segments arriving late. If this segment 511794235b7SAndre Oppermann * was a legitimate new connection attempt the old INPCB gets 512794235b7SAndre Oppermann * removed and we can try again to find a listening socket. 513340c35deSJonathan Lemon */ 514794235b7SAndre Oppermann if (inp->inp_vflag & INP_TIMEWAIT) { 515340c35deSJonathan Lemon if (thflags & TH_SYN) 516f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 5178d573cc1SAndre Oppermann /* 5188d573cc1SAndre Oppermann * NB: tcp_twcheck unlocks the INP and frees the mbuf. 5198d573cc1SAndre Oppermann */ 5202104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 521340c35deSJonathan Lemon goto findpcb; 522340c35deSJonathan Lemon INP_INFO_WUNLOCK(&tcbinfo); 523340c35deSJonathan Lemon return; 524340c35deSJonathan Lemon } 525794235b7SAndre Oppermann /* 526794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 527794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 528794235b7SAndre Oppermann * segment and send an appropriate response. 529794235b7SAndre Oppermann */ 530df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 531a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 532a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 533a57815efSBosko Milekic goto dropwithreset; 53409f81a46SBosko Milekic } 535df8bae1dSRodney W. Grimes 536c488362eSRobert Watson #ifdef MAC 5378501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 53830d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 539302ce8d6SAndre Oppermann goto dropunlock; 540c488362eSRobert Watson #endif 541a557af22SRobert Watson so = inp->inp_socket; 542302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 543610ee2f9SDavid Greenman #ifdef TCPDEBUG 544df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 545df8bae1dSRodney W. Grimes ostate = tp->t_state; 546d30d90dcSMaxim Konovalov if (isipv6) { 54710fe523eSMaxim Konovalov #ifdef INET6 548540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 549d30d90dcSMaxim Konovalov #endif 550d30d90dcSMaxim Konovalov } else 551540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 552fb59c426SYoshinobu Inoue tcp_savetcp = *th; 553df8bae1dSRodney W. Grimes } 554610ee2f9SDavid Greenman #endif 555db33b3e6SAndre Oppermann /* 556db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 557db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 558db33b3e6SAndre Oppermann * attempt or the completion of a previous one. 559db33b3e6SAndre Oppermann */ 560540e8b7eSJeffrey Hsu if (so->so_options & SO_ACCEPTCONN) { 561540e8b7eSJeffrey Hsu struct in_conninfo inc; 562540e8b7eSJeffrey Hsu 5637824d002SAndre Oppermann KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but " 5647824d002SAndre Oppermann "tp not listening", __func__)); 5657824d002SAndre Oppermann 5668bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 567be2ac88cSJonathan Lemon inc.inc_isipv6 = isipv6; 568302ce8d6SAndre Oppermann #ifdef INET6 569be2ac88cSJonathan Lemon if (isipv6) { 570be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 571be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 572302ce8d6SAndre Oppermann } else 573302ce8d6SAndre Oppermann #endif 574302ce8d6SAndre Oppermann { 575be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 576be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 577be2ac88cSJonathan Lemon } 578be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 579be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 580be2ac88cSJonathan Lemon 581fb59c426SYoshinobu Inoue /* 5828d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 5838d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 5848d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 585fb59c426SYoshinobu Inoue */ 586be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 587bf6d304aSAndre Oppermann /* 588bf6d304aSAndre Oppermann * Parse the TCP options here because 589bf6d304aSAndre Oppermann * syncookies need access to the reflected 590bf6d304aSAndre Oppermann * timestamp. 591bf6d304aSAndre Oppermann */ 592bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 5939fa198beSAndre Oppermann /* 5949fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 5959fa198beSAndre Oppermann * inp and tcpinfo locks. 5969fa198beSAndre Oppermann */ 597bf6d304aSAndre Oppermann if (!syncache_expand(&inc, &to, th, &so, m)) { 5984195b4afSPaul Traina /* 599e207f800SAndre Oppermann * No syncache entry or ACK was not 600be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 6018d573cc1SAndre Oppermann * NB: syncache did its own logging 6028d573cc1SAndre Oppermann * of the failure cause. 6034195b4afSPaul Traina */ 604be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 605be2ac88cSJonathan Lemon goto dropwithreset; 606be2ac88cSJonathan Lemon } 607f76fcf6dSJeffrey Hsu if (so == NULL) { 608be2ac88cSJonathan Lemon /* 609e207f800SAndre Oppermann * We completed the 3-way handshake 610e207f800SAndre Oppermann * but could not allocate a socket 611e207f800SAndre Oppermann * either due to memory shortage, 612e207f800SAndre Oppermann * listen queue length limits or 613a160e630SAndre Oppermann * global socket limits. Send RST 614a160e630SAndre Oppermann * or wait and have the remote end 615a160e630SAndre Oppermann * retransmit the ACK for another 616a160e630SAndre Oppermann * try. 617be2ac88cSJonathan Lemon */ 618a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 619a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 6208d573cc1SAndre Oppermann "Socket allocation failed due to " 6218d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 622a160e630SAndre Oppermann s, __func__, (tcp_sc_rst_sock_fail ? 623a160e630SAndre Oppermann "sending RST" : "try again")); 624a160e630SAndre Oppermann if (tcp_sc_rst_sock_fail) { 625e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 626e207f800SAndre Oppermann goto dropwithreset; 627a160e630SAndre Oppermann } else 628a160e630SAndre Oppermann goto dropunlock; 629f76fcf6dSJeffrey Hsu } 630be2ac88cSJonathan Lemon /* 631be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 6328d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 6338d573cc1SAndre Oppermann * created socket and update the tp variable. 634be2ac88cSJonathan Lemon */ 6358501a69cSRobert Watson INP_WUNLOCK(inp); /* listen socket */ 636be2ac88cSJonathan Lemon inp = sotoinpcb(so); 6378501a69cSRobert Watson INP_WLOCK(inp); /* new connection */ 638be2ac88cSJonathan Lemon tp = intotcpcb(inp); 6398d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 6408d573cc1SAndre Oppermann ("%s: ", __func__)); 641be2ac88cSJonathan Lemon /* 642302ce8d6SAndre Oppermann * Process the segment and the data it 643302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 644302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 645302ce8d6SAndre Oppermann */ 6468d573cc1SAndre Oppermann tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen); 647679d9708SAndre Oppermann INP_INFO_UNLOCK_ASSERT(&tcbinfo); 648302ce8d6SAndre Oppermann return; 649be2ac88cSJonathan Lemon } 650a160e630SAndre Oppermann /* 6518d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 6528d573cc1SAndre Oppermann * 653a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 654a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 655a160e630SAndre Oppermann * retransmits. 65619bc77c5SAndre Oppermann * 65719bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 65819bc77c5SAndre Oppermann * causes. 659a160e630SAndre Oppermann */ 660a160e630SAndre Oppermann if (thflags & TH_RST) { 66119bc77c5SAndre Oppermann syncache_chkrst(&inc, th); 662a160e630SAndre Oppermann goto dropunlock; 663a160e630SAndre Oppermann } 664a160e630SAndre Oppermann /* 665a160e630SAndre Oppermann * We can't do anything without SYN. 666a160e630SAndre Oppermann */ 667a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 668a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 669a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 670773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 6718d573cc1SAndre Oppermann s, __func__); 672a160e630SAndre Oppermann tcpstat.tcps_badsyn++; 673a160e630SAndre Oppermann goto dropunlock; 674a160e630SAndre Oppermann } 675a160e630SAndre Oppermann /* 676a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 677a160e630SAndre Oppermann */ 678fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 679a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 680a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 6818d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 6828d573cc1SAndre Oppermann s, __func__); 683a160e630SAndre Oppermann syncache_badack(&inc); /* XXX: Not needed! */ 684ebb0cbeaSPaul Traina tcpstat.tcps_badsyn++; 685a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 686a57815efSBosko Milekic goto dropwithreset; 6874195b4afSPaul Traina } 688a160e630SAndre Oppermann /* 689a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 690a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 691a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 692a160e630SAndre Oppermann * TCP/IP stack. 693a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 694a160e630SAndre Oppermann * and is constantly refining its stack detection 695a160e630SAndre Oppermann * strategies. 6968d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 697a160e630SAndre Oppermann * and was used by RFC1644. 698a160e630SAndre Oppermann */ 699a160e630SAndre Oppermann if ((thflags & TH_FIN) && drop_synfin) { 700a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 701a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 702773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 7038d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 704a160e630SAndre Oppermann tcpstat.tcps_badsyn++; 705302ce8d6SAndre Oppermann goto dropunlock; 706ebb0cbeaSPaul Traina } 707be2ac88cSJonathan Lemon /* 708be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 709a160e630SAndre Oppermann * 710a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 711a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 712a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 713be2ac88cSJonathan Lemon */ 714a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 715a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 716a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 717a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 71833841545SHajimu UMEMOTO #ifdef INET6 71933841545SHajimu UMEMOTO /* 72033841545SHajimu UMEMOTO * If deprecated address is forbidden, 72133841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 72233841545SHajimu UMEMOTO * address to prevent any new inbound connection from 72333841545SHajimu UMEMOTO * getting established. 72433841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 72533841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 72633841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 72733841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 72833841545SHajimu UMEMOTO * for the exchange. 72933841545SHajimu UMEMOTO * 73033841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 73133841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 73233841545SHajimu UMEMOTO * SYN in this case. 73333841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 73433841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 73533841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 73633841545SHajimu UMEMOTO * used" 73733841545SHajimu UMEMOTO * 2. use of it with new communication: 73833841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 73933841545SHajimu UMEMOTO * with sufficient scope is available" 74033841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 74133841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 74233841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 74333841545SHajimu UMEMOTO * 74433841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 74533841545SHajimu UMEMOTO * multiple description text for deprecated address 74633841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 74733841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 74833841545SHajimu UMEMOTO */ 74933841545SHajimu UMEMOTO if (isipv6 && !ip6_use_deprecated) { 75033841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 75133841545SHajimu UMEMOTO 75233841545SHajimu UMEMOTO if ((ia6 = ip6_getdstifaddr(m)) && 75333841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 754a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 755a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 7568d573cc1SAndre Oppermann "Connection attempt to deprecated " 7578d573cc1SAndre Oppermann "IPv6 address rejected\n", 758a160e630SAndre Oppermann s, __func__); 75933841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 76033841545SHajimu UMEMOTO goto dropwithreset; 76133841545SHajimu UMEMOTO } 76233841545SHajimu UMEMOTO } 76333841545SHajimu UMEMOTO #endif 76465f28919SJesper Skriver /* 765db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 7668d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 767db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 7688d573cc1SAndre Oppermann * If it is from this socket it must be forged. 7698d573cc1SAndre Oppermann * Don't respond if the source or destination is a 7708d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 77193ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 77293ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 77393ec91baSCrist J. Clark * in_broadcast() to find them. 774be2ac88cSJonathan Lemon */ 7758d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 7768d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 7778d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 7788d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 779773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 780302ce8d6SAndre Oppermann goto dropunlock; 7818d573cc1SAndre Oppermann } 782be2ac88cSJonathan Lemon if (isipv6) { 783db33b3e6SAndre Oppermann #ifdef INET6 784db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 785a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 786a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 787a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 7888d573cc1SAndre Oppermann "Connection attempt to/from self " 789773673c1SAndre Oppermann "ignored\n", s, __func__); 790302ce8d6SAndre Oppermann goto dropunlock; 791a160e630SAndre Oppermann } 792be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 793a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 794a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 795a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 7968d573cc1SAndre Oppermann "Connection attempt from/to multicast " 797773673c1SAndre Oppermann "address ignored\n", s, __func__); 798302ce8d6SAndre Oppermann goto dropunlock; 799a160e630SAndre Oppermann } 800db33b3e6SAndre Oppermann #endif 801c068736aSJeffrey Hsu } else { 802db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 803a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 804a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 805a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 8068d573cc1SAndre Oppermann "Connection attempt from/to self " 807773673c1SAndre Oppermann "ignored\n", s, __func__); 808302ce8d6SAndre Oppermann goto dropunlock; 809a160e630SAndre Oppermann } 810be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 811be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 8122ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 813a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 814a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 815a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 8168d573cc1SAndre Oppermann "Connection attempt from/to broad- " 817773673c1SAndre Oppermann "or multicast address ignored\n", 818a160e630SAndre Oppermann s, __func__); 819302ce8d6SAndre Oppermann goto dropunlock; 820c068736aSJeffrey Hsu } 821a160e630SAndre Oppermann } 822be2ac88cSJonathan Lemon /* 823db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 824db33b3e6SAndre Oppermann * for syncache. 825be2ac88cSJonathan Lemon */ 8263c653157SHartmut Brandt #ifdef TCPDEBUG 8273c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 8283c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 8293c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 8303c653157SHartmut Brandt #endif 831f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 8324d6e7130SAndre Oppermann syncache_add(&inc, &to, th, inp, &so, m); 833be2ac88cSJonathan Lemon /* 8344d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 835a160e630SAndre Oppermann * Everything already unlocked by syncache_add(). 836be2ac88cSJonathan Lemon */ 837a160e630SAndre Oppermann INP_INFO_UNLOCK_ASSERT(&tcbinfo); 838be2ac88cSJonathan Lemon return; 839f76fcf6dSJeffrey Hsu } 840db33b3e6SAndre Oppermann 841302ce8d6SAndre Oppermann /* 8428d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 8438d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 8448d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 845302ce8d6SAndre Oppermann */ 846679d9708SAndre Oppermann tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen); 847679d9708SAndre Oppermann INP_INFO_UNLOCK_ASSERT(&tcbinfo); 848302ce8d6SAndre Oppermann return; 849be2ac88cSJonathan Lemon 850302ce8d6SAndre Oppermann dropwithreset: 851995a7717SAndre Oppermann INP_INFO_WLOCK_ASSERT(&tcbinfo); 852302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 853302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 854302ce8d6SAndre Oppermann dropunlock: 855995a7717SAndre Oppermann INP_INFO_WLOCK_ASSERT(&tcbinfo); 8569fa198beSAndre Oppermann if (inp != NULL) 8578501a69cSRobert Watson INP_WUNLOCK(inp); 858302ce8d6SAndre Oppermann INP_INFO_WUNLOCK(&tcbinfo); 859302ce8d6SAndre Oppermann drop: 860995a7717SAndre Oppermann INP_INFO_UNLOCK_ASSERT(&tcbinfo); 861a160e630SAndre Oppermann if (s != NULL) 862a160e630SAndre Oppermann free(s, M_TCPLOG); 863302ce8d6SAndre Oppermann if (m != NULL) 864302ce8d6SAndre Oppermann m_freem(m); 865302ce8d6SAndre Oppermann return; 866302ce8d6SAndre Oppermann } 867302ce8d6SAndre Oppermann 868679d9708SAndre Oppermann static void 869302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 870302ce8d6SAndre Oppermann struct tcpcb *tp, int drop_hdrlen, int tlen) 871302ce8d6SAndre Oppermann { 872302ce8d6SAndre Oppermann int thflags, acked, ourfinisacked, needoutput = 0; 873302ce8d6SAndre Oppermann int headlocked = 1; 874302ce8d6SAndre Oppermann int rstreason, todrop, win; 875302ce8d6SAndre Oppermann u_long tiwin; 876302ce8d6SAndre Oppermann struct tcpopt to; 877302ce8d6SAndre Oppermann 87814739780SMaxim Konovalov #ifdef TCPDEBUG 87914739780SMaxim Konovalov /* 88014739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 88114739780SMaxim Konovalov * now IPv6. 88214739780SMaxim Konovalov */ 88314739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 88414739780SMaxim Konovalov struct tcphdr tcp_savetcp; 88514739780SMaxim Konovalov short ostate = 0; 88614739780SMaxim Konovalov #endif 887302ce8d6SAndre Oppermann thflags = th->th_flags; 888302ce8d6SAndre Oppermann 889302ce8d6SAndre Oppermann INP_INFO_WLOCK_ASSERT(&tcbinfo); 8908501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 891679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 892679d9708SAndre Oppermann __func__)); 893679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 894679d9708SAndre Oppermann __func__)); 895df8bae1dSRodney W. Grimes 896df8bae1dSRodney W. Grimes /* 897df8bae1dSRodney W. Grimes * Segment received on connection. 898df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 899e8949f74SAndre Oppermann * XXX: This should be done after segment 900e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 901df8bae1dSRodney W. Grimes */ 9029b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 9037ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 904b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 905df8bae1dSRodney W. Grimes 906df8bae1dSRodney W. Grimes /* 907464fcfbcSAndre Oppermann * Unscale the window into a 32-bit value. 908e8949f74SAndre Oppermann * For the SYN_SENT state the scale is zero. 909464fcfbcSAndre Oppermann */ 910464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 911464fcfbcSAndre Oppermann 912464fcfbcSAndre Oppermann /* 913f72167f4SAndre Oppermann * Parse options on any incoming segment. 914f72167f4SAndre Oppermann */ 915302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 916302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 917302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 918f72167f4SAndre Oppermann 919f72167f4SAndre Oppermann /* 920f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 921bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 922bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 923bf6d304aSAndre Oppermann * was established. 924f72167f4SAndre Oppermann */ 925bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 926e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 927bf6d304aSAndre Oppermann if (TSTMP_GT(to.to_tsecr, ticks)) 928f72167f4SAndre Oppermann to.to_tsecr = 0; 929bf6d304aSAndre Oppermann } 930f72167f4SAndre Oppermann 931f72167f4SAndre Oppermann /* 93297d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 93397d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 9345396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 9355396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 93697d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 937df8bae1dSRodney W. Grimes */ 9380a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 939464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 940464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 941be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 94202a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 943be2ac88cSJonathan Lemon } 9445396d0f8SAndre Oppermann /* 9455396d0f8SAndre Oppermann * Initial send window. It will be updated with 9465396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 9475396d0f8SAndre Oppermann */ 9485396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 949be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 950be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 951be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 952be2ac88cSJonathan Lemon tp->ts_recent_age = ticks; 953be2ac88cSJonathan Lemon } 954be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 955be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 9563529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 9573529149eSAndre Oppermann (to.to_flags & TOF_SACKPERM) == 0) 9583529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 9596d90faf3SPaul Saab } 9606d90faf3SPaul Saab 961df8bae1dSRodney W. Grimes /* 962df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 963df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 964df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 965df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 966df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 967df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 968df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 969df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 970df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 971df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 972df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 973df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 974a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 975c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 976a0292f23SGarrett Wollman * be TH_NEEDSYN. 977df8bae1dSRodney W. Grimes */ 978df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 979c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 980fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 981c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 982c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 983a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 984c5ad39b9SAndre Oppermann LIST_EMPTY(&tp->t_segq) && 985be2ac88cSJonathan Lemon ((to.to_flags & TOF_TS) == 0 || 986c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 987df8bae1dSRodney W. Grimes 988df8bae1dSRodney W. Grimes /* 989df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 990df8bae1dSRodney W. Grimes * record the timestamp. 991a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 992a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 993df8bae1dSRodney W. Grimes */ 994be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 995fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 9969b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 997a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 998df8bae1dSRodney W. Grimes } 999df8bae1dSRodney W. Grimes 1000fb59c426SYoshinobu Inoue if (tlen == 0) { 1001fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1002fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1003233e8c18SGarrett Wollman tp->snd_cwnd >= tp->snd_wnd && 10043529149eSAndre Oppermann ((!tcp_do_newreno && 10053529149eSAndre Oppermann !(tp->t_flags & TF_SACK_PERMIT) && 1006cb942153SJeffrey Hsu tp->t_dupacks < tcprexmtthresh) || 10073529149eSAndre Oppermann ((tcp_do_newreno || 10083529149eSAndre Oppermann (tp->t_flags & TF_SACK_PERMIT)) && 1009fc30a251SAndre Oppermann !IN_FASTRECOVERY(tp) && 1010fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1011482ac968SPaul Saab TAILQ_EMPTY(&tp->snd_holes)))) { 1012302ce8d6SAndre Oppermann KASSERT(headlocked, 1013302ce8d6SAndre Oppermann ("%s: headlocked", __func__)); 1014e0bef1cbSRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 1015e0bef1cbSRobert Watson headlocked = 0; 1016df8bae1dSRodney W. Grimes /* 1017e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1018df8bae1dSRodney W. Grimes */ 1019df8bae1dSRodney W. Grimes ++tcpstat.tcps_predack; 10209b8b58e0SJonathan Lemon /* 1021e8949f74SAndre Oppermann * "bad retransmit" recovery. 10229b8b58e0SJonathan Lemon */ 10239b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 10249b8b58e0SJonathan Lemon ticks < tp->t_badrxtwin) { 1025cb942153SJeffrey Hsu ++tcpstat.tcps_sndrexmitbad; 10269b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 10279b8b58e0SJonathan Lemon tp->snd_ssthresh = 10289b8b58e0SJonathan Lemon tp->snd_ssthresh_prev; 10299d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 10309d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 10319d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 10329b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 10339b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; 10349b8b58e0SJonathan Lemon } 1035fa55172bSMatthew Dillon 1036fa55172bSMatthew Dillon /* 1037fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1038fa55172bSMatthew Dillon * 1039fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1040fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1041fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1042fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1043fa55172bSMatthew Dillon */ 1044fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1045fa55172bSMatthew Dillon to.to_tsecr) { 1046eaf80179SAndre Oppermann if (!tp->t_rttlow || 1047eaf80179SAndre Oppermann tp->t_rttlow > ticks - to.to_tsecr) 1048eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 1049a0292f23SGarrett Wollman tcp_xmit_timer(tp, 10509b8b58e0SJonathan Lemon ticks - to.to_tsecr + 1); 1051fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1052fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1053eaf80179SAndre Oppermann if (!tp->t_rttlow || 1054eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1055eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1056c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1057c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1058fa55172bSMatthew Dillon } 10591fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 1060fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 1061df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 1062df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 1063df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 10649d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 10659d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 10669d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 10679d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 10681645d090SJeff Roberson /* 1069e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 10701645d090SJeff Roberson * to th_ack. 10711645d090SJeff Roberson */ 1072cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1073b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1074df8bae1dSRodney W. Grimes m_freem(m); 1075e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made. */ 1076df8bae1dSRodney W. Grimes 1077df8bae1dSRodney W. Grimes /* 1078df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1079df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1080df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1081df8bae1dSRodney W. Grimes * If process is waiting for space, 1082df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1083df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1084df8bae1dSRodney W. Grimes * decide between more output or persist. 1085e8949f74SAndre Oppermann */ 10863c653157SHartmut Brandt #ifdef TCPDEBUG 10873c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 10883c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 10893c653157SHartmut Brandt (void *)tcp_saveipgen, 10903c653157SHartmut Brandt &tcp_savetcp, 0); 10913c653157SHartmut Brandt #endif 1092df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1093b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1094b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1095b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1096b8152ba7SAndre Oppermann tp->t_rxtcur); 1097df8bae1dSRodney W. Grimes sowwakeup(so); 1098df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 1099df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1100a14c749fSJonathan Lemon goto check_delack; 1101df8bae1dSRodney W. Grimes } 1102fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1103fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 11046741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 11056741ecf5SAndre Oppermann 1106302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: headlocked", __func__)); 1107e0bef1cbSRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 1108e0bef1cbSRobert Watson headlocked = 0; 1109df8bae1dSRodney W. Grimes /* 1110e8949f74SAndre Oppermann * This is a pure, in-sequence data packet 1111df8bae1dSRodney W. Grimes * with nothing on the reassembly queue and 1112df8bae1dSRodney W. Grimes * we have enough buffer space to take it. 1113df8bae1dSRodney W. Grimes */ 11146d90faf3SPaul Saab /* Clean receiver SACK report if present */ 11153529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 11166d90faf3SPaul Saab tcp_clean_sackreport(tp); 1117df8bae1dSRodney W. Grimes ++tcpstat.tcps_preddat; 1118fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 11191645d090SJeff Roberson /* 11201645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 11211645d090SJeff Roberson * th_seq. 11221645d090SJeff Roberson */ 11231645d090SJeff Roberson tp->snd_wl1 = th->th_seq; 11241645d090SJeff Roberson /* 11251645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 11261645d090SJeff Roberson * rcv_nxt. 11271645d090SJeff Roberson */ 11281645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 1129df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpack++; 1130fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyte += tlen; 1131e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made */ 11323c653157SHartmut Brandt #ifdef TCPDEBUG 11333c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 11343c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 11353c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 11363c653157SHartmut Brandt #endif 11376741ecf5SAndre Oppermann /* 11386741ecf5SAndre Oppermann * Automatic sizing of receive socket buffer. Often the send 11396741ecf5SAndre Oppermann * buffer size is not optimally adjusted to the actual network 11406741ecf5SAndre Oppermann * conditions at hand (delay bandwidth product). Setting the 11416741ecf5SAndre Oppermann * buffer size too small limits throughput on links with high 11426741ecf5SAndre Oppermann * bandwidth and high delay (eg. trans-continental/oceanic links). 11436741ecf5SAndre Oppermann * 11446741ecf5SAndre Oppermann * On the receive side the socket buffer memory is only rarely 11456741ecf5SAndre Oppermann * used to any significant extent. This allows us to be much 11466741ecf5SAndre Oppermann * more aggressive in scaling the receive socket buffer. For 11476741ecf5SAndre Oppermann * the case that the buffer space is actually used to a large 11486741ecf5SAndre Oppermann * extent and we run out of kernel memory we can simply drop 11496741ecf5SAndre Oppermann * the new segments; TCP on the sender will just retransmit it 11506741ecf5SAndre Oppermann * later. Setting the buffer size too big may only consume too 11516741ecf5SAndre Oppermann * much kernel memory if the application doesn't read() from 11526741ecf5SAndre Oppermann * the socket or packet loss or reordering makes use of the 11536741ecf5SAndre Oppermann * reassembly queue. 11546741ecf5SAndre Oppermann * 11556741ecf5SAndre Oppermann * The criteria to step up the receive buffer one notch are: 11566741ecf5SAndre Oppermann * 1. the number of bytes received during the time it takes 11576741ecf5SAndre Oppermann * one timestamp to be reflected back to us (the RTT); 11586741ecf5SAndre Oppermann * 2. received bytes per RTT is within seven eighth of the 11596741ecf5SAndre Oppermann * current socket buffer size; 11606741ecf5SAndre Oppermann * 3. receive buffer size has not hit maximal automatic size; 11616741ecf5SAndre Oppermann * 11626741ecf5SAndre Oppermann * This algorithm does one step per RTT at most and only if 11636741ecf5SAndre Oppermann * we receive a bulk stream w/o packet losses or reorderings. 11646741ecf5SAndre Oppermann * Shrinking the buffer during idle times is not necessary as 11656741ecf5SAndre Oppermann * it doesn't consume any memory when idle. 11666741ecf5SAndre Oppermann * 11676741ecf5SAndre Oppermann * TODO: Only step up if the application is actually serving 11686741ecf5SAndre Oppermann * the buffer to better manage the socket buffer resources. 1169df8bae1dSRodney W. Grimes */ 11706741ecf5SAndre Oppermann if (tcp_do_autorcvbuf && 11716741ecf5SAndre Oppermann to.to_tsecr && 11726741ecf5SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 11736741ecf5SAndre Oppermann if (to.to_tsecr > tp->rfbuf_ts && 11746741ecf5SAndre Oppermann to.to_tsecr - tp->rfbuf_ts < hz) { 11756741ecf5SAndre Oppermann if (tp->rfbuf_cnt > 11766741ecf5SAndre Oppermann (so->so_rcv.sb_hiwat / 8 * 7) && 11776741ecf5SAndre Oppermann so->so_rcv.sb_hiwat < 11786741ecf5SAndre Oppermann tcp_autorcvbuf_max) { 11796741ecf5SAndre Oppermann newsize = 11806741ecf5SAndre Oppermann min(so->so_rcv.sb_hiwat + 11816741ecf5SAndre Oppermann tcp_autorcvbuf_inc, 11826741ecf5SAndre Oppermann tcp_autorcvbuf_max); 11836741ecf5SAndre Oppermann } 11846741ecf5SAndre Oppermann /* Start over with next RTT. */ 11856741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 11866741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 11876741ecf5SAndre Oppermann } else 11886741ecf5SAndre Oppermann tp->rfbuf_cnt += tlen; /* add up */ 11896741ecf5SAndre Oppermann } 11906741ecf5SAndre Oppermann 11916741ecf5SAndre Oppermann /* Add data to socket buffer. */ 11921e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1193c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1194c1c36a2cSMike Silbersack m_freem(m); 1195c1c36a2cSMike Silbersack } else { 11966741ecf5SAndre Oppermann /* 11976741ecf5SAndre Oppermann * Set new socket buffer size. 11986741ecf5SAndre Oppermann * Give up when limit is reached. 11996741ecf5SAndre Oppermann */ 12006741ecf5SAndre Oppermann if (newsize) 12016741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 12026741ecf5SAndre Oppermann newsize, so, curthread)) 12036741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1204fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 12051e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 1206c1c36a2cSMike Silbersack } 1207e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 12081e4d7da7SRobert Watson sorwakeup_locked(so); 1209d8c85a26SJonathan Lemon if (DELAY_ACK(tp)) { 12103bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1211f498eeeeSDavid Greenman } else { 1212e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 1213e612a582SDavid Greenman tcp_output(tp); 1214e612a582SDavid Greenman } 1215a14c749fSJonathan Lemon goto check_delack; 1216df8bae1dSRodney W. Grimes } 1217df8bae1dSRodney W. Grimes } 1218df8bae1dSRodney W. Grimes 1219df8bae1dSRodney W. Grimes /* 1220df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1221df8bae1dSRodney W. Grimes * and then do TCP input processing. 1222df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1223df8bae1dSRodney W. Grimes * but not less than advertised window. 1224df8bae1dSRodney W. Grimes */ 1225df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1226df8bae1dSRodney W. Grimes if (win < 0) 1227df8bae1dSRodney W. Grimes win = 0; 122866e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1229df8bae1dSRodney W. Grimes 12306741ecf5SAndre Oppermann /* Reset receive buffer auto scaling when not in bulk receive mode. */ 12316741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 12326741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 12336741ecf5SAndre Oppermann 1234df8bae1dSRodney W. Grimes switch (tp->t_state) { 1235df8bae1dSRodney W. Grimes 1236df8bae1dSRodney W. Grimes /* 1237764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1238764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1239764d8cefSBill Fenner */ 1240764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1241fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1242fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1243a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1244a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1245a57815efSBosko Milekic goto dropwithreset; 1246a57815efSBosko Milekic } 1247764d8cefSBill Fenner break; 1248764d8cefSBill Fenner 1249764d8cefSBill Fenner /* 1250df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1251df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1252df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1253df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1254df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1255df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1256df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1257df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1258df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1259df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1260df8bae1dSRodney W. Grimes */ 1261df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1262fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1263fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1264fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1265a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1266a0292f23SGarrett Wollman goto dropwithreset; 1267a0292f23SGarrett Wollman } 12680ca3f933SAndre Oppermann if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) 1269df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 12700ca3f933SAndre Oppermann if (thflags & TH_RST) 1271df8bae1dSRodney W. Grimes goto drop; 12720ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 1273df8bae1dSRodney W. Grimes goto drop; 1274464fcfbcSAndre Oppermann 1275fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1276df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1277fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1278845799c1SAndras Olah tcpstat.tcps_connects++; 1279845799c1SAndras Olah soisconnected(so); 1280c488362eSRobert Watson #ifdef MAC 1281310e7cebSRobert Watson SOCK_LOCK(so); 128230d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 1283310e7cebSRobert Watson SOCK_UNLOCK(so); 1284c488362eSRobert Watson #endif 1285845799c1SAndras Olah /* Do window scaling on this connection? */ 1286845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1287845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1288845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1289845799c1SAndras Olah } 1290a0292f23SGarrett Wollman tp->rcv_adv += tp->rcv_wnd; 1291a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1292a0292f23SGarrett Wollman /* 1293a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1294a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1295a0292f23SGarrett Wollman */ 1296d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && tlen != 0) 1297b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 1298b8152ba7SAndre Oppermann tcp_delacktime); 1299a0292f23SGarrett Wollman else 1300a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1301a0292f23SGarrett Wollman /* 1302a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1303a0292f23SGarrett Wollman * Transitions: 1304a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1305a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1306a0292f23SGarrett Wollman */ 13079b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1308a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1309a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1310a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1311fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 13127ff19458SPaul Traina } else { 1313a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1314b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 13157ff19458SPaul Traina } 1316a0292f23SGarrett Wollman } else { 1317a0292f23SGarrett Wollman /* 1318c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 1319c068736aSJeffrey Hsu * simultaneous open. If segment contains CC option 1320c068736aSJeffrey Hsu * and there is a cached CC, apply TAO test. 1321c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 1322c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 1323a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1324a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1325a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1326a0292f23SGarrett Wollman */ 13274b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 1328b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1329df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1330a0292f23SGarrett Wollman } 1331df8bae1dSRodney W. Grimes 1332302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: trimthenstep6: head not locked", 1333302ce8d6SAndre Oppermann __func__)); 13348501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 13357cfc6904SRobert Watson 1336df8bae1dSRodney W. Grimes /* 1337fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1338df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1339df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1340df8bae1dSRodney W. Grimes */ 1341fb59c426SYoshinobu Inoue th->th_seq++; 1342fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1343fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1344df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1345fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1346fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1347df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1348df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1349df8bae1dSRodney W. Grimes } 1350fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1351fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 1352a0292f23SGarrett Wollman /* 1353a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1354a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1355a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1356a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1357a0292f23SGarrett Wollman * Otherwise, goto step 6. 1358a0292f23SGarrett Wollman */ 1359fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1360a0292f23SGarrett Wollman goto process_ACK; 1361c068736aSJeffrey Hsu 1362df8bae1dSRodney W. Grimes goto step6; 1363c068736aSJeffrey Hsu 1364a0292f23SGarrett Wollman /* 1365a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1366c94c54e4SAndre Oppermann * do normal processing. 1367a0292f23SGarrett Wollman * 1368c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 1369a0292f23SGarrett Wollman */ 1370a0292f23SGarrett Wollman case TCPS_LAST_ACK: 1371a0292f23SGarrett Wollman case TCPS_CLOSING: 1372a0292f23SGarrett Wollman break; /* continue normal processing */ 1373df8bae1dSRodney W. Grimes } 1374df8bae1dSRodney W. Grimes 1375df8bae1dSRodney W. Grimes /* 1376df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 137780ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 137880ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 137980ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 138080ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 138180ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 138280ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 138380ab7c0eSGarrett Wollman * killing a connection). 138480ab7c0eSGarrett Wollman * Then check timestamp, if present. 1385a0292f23SGarrett Wollman * Then check the connection count, if present. 1386df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 1387df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 1388df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 1389df8bae1dSRodney W. Grimes * 139080ab7c0eSGarrett Wollman * 139180ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 139280ab7c0eSGarrett Wollman * if this is a valid reset segment. 139380ab7c0eSGarrett Wollman * RFC 793 page 37: 139480ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 139580ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 139680ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 139780ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 139880ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 13991a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 14001a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 14011a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 14021a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 140380dd2a81SMike Silbersack * Note 2: Paul Watson's paper "Slipping in the Window" has shown 140480dd2a81SMike Silbersack * that brute force RST attacks are possible. To combat this, 140580dd2a81SMike Silbersack * we use a much stricter check while in the ESTABLISHED state, 140680dd2a81SMike Silbersack * only accepting RSTs where the sequence number is equal to 140780dd2a81SMike Silbersack * last_ack_sent. In all other states (the states in which a 140880dd2a81SMike Silbersack * RST is more likely), the more permissive check is used. 140980ab7c0eSGarrett Wollman * If we have multiple segments in flight, the intial reset 141080ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 141180ab7c0eSGarrett Wollman * but they will eventually catch up. 141280ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 141380ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 141480ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 141580ab7c0eSGarrett Wollman * 141680ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 141780ab7c0eSGarrett Wollman * 141880ab7c0eSGarrett Wollman * DISCUSSION 141980ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 142080ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 142180ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 142280ab7c0eSGarrett Wollman * data. 142380ab7c0eSGarrett Wollman * 142480ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 142580ab7c0eSGarrett Wollman * the state: 142680ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 142780ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 142880ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 1429745bab7fSDima Dorfman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 143080ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 1431e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 143280ab7c0eSGarrett Wollman * Close the tcb. 1433e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 143480ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 143580ab7c0eSGarrett Wollman * RFC 1337. 143680ab7c0eSGarrett Wollman */ 1437fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 143895ad8418SQing Li if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 143995ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 144080ab7c0eSGarrett Wollman switch (tp->t_state) { 144180ab7c0eSGarrett Wollman 144280ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 144380ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 144480ab7c0eSGarrett Wollman goto close; 144580ab7c0eSGarrett Wollman 144680ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 144795ad8418SQing Li if (tcp_insecure_rst == 0 && 144895ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) && 144995ad8418SQing Li SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) && 145095ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 145195ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) { 145280dd2a81SMike Silbersack tcpstat.tcps_badrst++; 145380dd2a81SMike Silbersack goto drop; 145480dd2a81SMike Silbersack } 1455564aab1fSAndre Oppermann /* FALLTHROUGH */ 145680ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 145780ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 145880ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 145980ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 146080ab7c0eSGarrett Wollman close: 146180ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 146280ab7c0eSGarrett Wollman tcpstat.tcps_drops++; 1463302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: trimthenstep6: " 1464302ce8d6SAndre Oppermann "tcp_close: head not locked", __func__)); 146580ab7c0eSGarrett Wollman tp = tcp_close(tp); 146680ab7c0eSGarrett Wollman break; 146780ab7c0eSGarrett Wollman 146880ab7c0eSGarrett Wollman case TCPS_CLOSING: 146980ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 1470302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: trimthenstep6: " 1471302ce8d6SAndre Oppermann "tcp_close.2: head not locked", __func__)); 147280ab7c0eSGarrett Wollman tp = tcp_close(tp); 147380ab7c0eSGarrett Wollman break; 147480ab7c0eSGarrett Wollman } 147580ab7c0eSGarrett Wollman } 147680ab7c0eSGarrett Wollman goto drop; 147780ab7c0eSGarrett Wollman } 147880ab7c0eSGarrett Wollman 147980ab7c0eSGarrett Wollman /* 1480df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 1481df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 1482df8bae1dSRodney W. Grimes */ 1483be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 148480ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1485df8bae1dSRodney W. Grimes 1486df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 14879b8b58e0SJonathan Lemon if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1488df8bae1dSRodney W. Grimes /* 1489df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 1490df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 1491df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 1492df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 1493df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 1494df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 1495df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 1496df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 1497df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 1498df8bae1dSRodney W. Grimes */ 1499df8bae1dSRodney W. Grimes tp->ts_recent = 0; 1500df8bae1dSRodney W. Grimes } else { 1501df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 1502fb59c426SYoshinobu Inoue tcpstat.tcps_rcvdupbyte += tlen; 1503df8bae1dSRodney W. Grimes tcpstat.tcps_pawsdrop++; 150407fd333dSMatthew Dillon if (tlen) 1505df8bae1dSRodney W. Grimes goto dropafterack; 15061ab4789dSMatthew Dillon goto drop; 15071ab4789dSMatthew Dillon } 1508df8bae1dSRodney W. Grimes } 1509df8bae1dSRodney W. Grimes 1510a0292f23SGarrett Wollman /* 151180ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 151280ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 151380ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 151480ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 151580ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 151680ab7c0eSGarrett Wollman */ 1517a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 1518a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1519a57815efSBosko Milekic goto dropwithreset; 1520a57815efSBosko Milekic } 152180ab7c0eSGarrett Wollman 1522fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 1523df8bae1dSRodney W. Grimes if (todrop > 0) { 1524fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1525fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 1526fb59c426SYoshinobu Inoue th->th_seq++; 1527fb59c426SYoshinobu Inoue if (th->th_urp > 1) 1528fb59c426SYoshinobu Inoue th->th_urp--; 1529df8bae1dSRodney W. Grimes else 1530fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1531df8bae1dSRodney W. Grimes todrop--; 1532df8bae1dSRodney W. Grimes } 1533df8bae1dSRodney W. Grimes /* 1534dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 1535df8bae1dSRodney W. Grimes */ 1536fb59c426SYoshinobu Inoue if (todrop > tlen 1537fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1538dac20301SGarrett Wollman /* 1539dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 1540dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 1541dac20301SGarrett Wollman * of sequence; drop it. 1542dac20301SGarrett Wollman */ 1543fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1544dac20301SGarrett Wollman 1545df8bae1dSRodney W. Grimes /* 1546dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 1547dac20301SGarrett Wollman * But keep on processing for RST or ACK. 1548df8bae1dSRodney W. Grimes */ 1549dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1550fb59c426SYoshinobu Inoue todrop = tlen; 1551dac20301SGarrett Wollman tcpstat.tcps_rcvduppack++; 1552dac20301SGarrett Wollman tcpstat.tcps_rcvdupbyte += todrop; 1553df8bae1dSRodney W. Grimes } else { 1554df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartduppack++; 1555df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartdupbyte += todrop; 1556df8bae1dSRodney W. Grimes } 1557fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 1558fb59c426SYoshinobu Inoue th->th_seq += todrop; 1559fb59c426SYoshinobu Inoue tlen -= todrop; 1560fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 1561fb59c426SYoshinobu Inoue th->th_urp -= todrop; 1562df8bae1dSRodney W. Grimes else { 1563fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1564fb59c426SYoshinobu Inoue th->th_urp = 0; 1565df8bae1dSRodney W. Grimes } 1566df8bae1dSRodney W. Grimes } 1567df8bae1dSRodney W. Grimes 1568df8bae1dSRodney W. Grimes /* 1569df8bae1dSRodney W. Grimes * If new data are received on a connection after the 1570df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 1571df8bae1dSRodney W. Grimes */ 1572df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 1573fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1574773673c1SAndre Oppermann char *s; 1575773673c1SAndre Oppermann 1576302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: trimthenstep6: tcp_close.3: head " 1577302ce8d6SAndre Oppermann "not locked", __func__)); 1578773673c1SAndre Oppermann if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) { 1579e31d8aa3SMike Silbersack log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data after socket " 1580773673c1SAndre Oppermann "was closed, sending RST and removing tcpcb\n", 1581e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 1582773673c1SAndre Oppermann free(s, M_TCPLOG); 1583773673c1SAndre Oppermann } 1584df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1585df8bae1dSRodney W. Grimes tcpstat.tcps_rcvafterclose++; 1586a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1587df8bae1dSRodney W. Grimes goto dropwithreset; 1588df8bae1dSRodney W. Grimes } 1589df8bae1dSRodney W. Grimes 1590df8bae1dSRodney W. Grimes /* 1591df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 1592df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 1593df8bae1dSRodney W. Grimes */ 1594fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 1595df8bae1dSRodney W. Grimes if (todrop > 0) { 1596df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1597fb59c426SYoshinobu Inoue if (todrop >= tlen) { 1598fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyteafterwin += tlen; 1599df8bae1dSRodney W. Grimes /* 1600df8bae1dSRodney W. Grimes * If window is closed can only take segments at 1601df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 1602df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 1603df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 1604df8bae1dSRodney W. Grimes * and ack. 1605df8bae1dSRodney W. Grimes */ 1606fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1607df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1608df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinprobe++; 1609df8bae1dSRodney W. Grimes } else 1610df8bae1dSRodney W. Grimes goto dropafterack; 1611df8bae1dSRodney W. Grimes } else 1612df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1613df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1614fb59c426SYoshinobu Inoue tlen -= todrop; 1615fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 1616df8bae1dSRodney W. Grimes } 1617df8bae1dSRodney W. Grimes 1618df8bae1dSRodney W. Grimes /* 1619df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1620df8bae1dSRodney W. Grimes * record its timestamp. 1621cf09195bSPaul Saab * NOTE: 1622cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 1623a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1624cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 1625cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 1626cf09195bSPaul Saab * predicated on the sequence space of this segment. 1627cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 1628cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 1629cf09195bSPaul Saab * instead of RFC1323's 1630cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 1631cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 1632cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 1633cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 1634cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 1635df8bae1dSRodney W. Grimes */ 1636be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1637cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 1638cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 1639cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 16409b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1641a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1642df8bae1dSRodney W. Grimes } 1643df8bae1dSRodney W. Grimes 1644df8bae1dSRodney W. Grimes /* 1645df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 1646df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 1647df8bae1dSRodney W. Grimes */ 1648fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1649302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: tcp_drop: trimthenstep6: " 1650302ce8d6SAndre Oppermann "head not locked", __func__)); 1651df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 1652a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1653122aad88SAndre Oppermann goto drop; 1654df8bae1dSRodney W. Grimes } 1655df8bae1dSRodney W. Grimes 1656a0292f23SGarrett Wollman /* 1657a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1658a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 1659a0292f23SGarrett Wollman * later processing; else drop segment and return. 1660a0292f23SGarrett Wollman */ 1661fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 1662a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 1663a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 1664a0292f23SGarrett Wollman goto step6; 16655e1aa279SDavid Malone else if (tp->t_flags & TF_ACKNOW) 16665e1aa279SDavid Malone goto dropafterack; 1667a0292f23SGarrett Wollman else 1668a0292f23SGarrett Wollman goto drop; 1669a0292f23SGarrett Wollman } 1670df8bae1dSRodney W. Grimes 1671df8bae1dSRodney W. Grimes /* 1672df8bae1dSRodney W. Grimes * Ack processing. 1673df8bae1dSRodney W. Grimes */ 1674df8bae1dSRodney W. Grimes switch (tp->t_state) { 1675df8bae1dSRodney W. Grimes 1676df8bae1dSRodney W. Grimes /* 1677764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1678764d8cefSBill Fenner * ESTABLISHED state and continue processing. 1679764d8cefSBill Fenner * The ACK was checked above. 1680df8bae1dSRodney W. Grimes */ 1681df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1682a0292f23SGarrett Wollman 1683df8bae1dSRodney W. Grimes tcpstat.tcps_connects++; 1684df8bae1dSRodney W. Grimes soisconnected(so); 1685df8bae1dSRodney W. Grimes /* Do window scaling? */ 1686df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1687df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1688df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 1689464fcfbcSAndre Oppermann tp->snd_wnd = tiwin; 1690df8bae1dSRodney W. Grimes } 1691a0292f23SGarrett Wollman /* 1692a0292f23SGarrett Wollman * Make transitions: 1693a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 1694a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 1695a0292f23SGarrett Wollman */ 16969b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1697a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1698a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1699a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 17007ff19458SPaul Traina } else { 1701a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1702b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 17037ff19458SPaul Traina } 1704a0292f23SGarrett Wollman /* 1705a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 1706a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 1707a0292f23SGarrett Wollman */ 1708fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 1709fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1710a0292f23SGarrett Wollman (struct mbuf *)0); 1711fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 171293b0017fSPhilippe Charnier /* FALLTHROUGH */ 1713df8bae1dSRodney W. Grimes 1714df8bae1dSRodney W. Grimes /* 1715df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1716df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 1717fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 1718fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 1719df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 1720df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 1721df8bae1dSRodney W. Grimes */ 1722df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1723df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1724df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 1725df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1726df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1727df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 17285a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 17295a53ca16SPaul Saab tcpstat.tcps_rcvacktoomuch++; 17305a53ca16SPaul Saab goto dropafterack; 17315a53ca16SPaul Saab } 17323529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 1733fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 1734fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 17355a53ca16SPaul Saab tcp_sack_doack(tp, &to, th->th_ack); 1736fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1737fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 1738df8bae1dSRodney W. Grimes tcpstat.tcps_rcvdupack++; 1739df8bae1dSRodney W. Grimes /* 1740df8bae1dSRodney W. Grimes * If we have outstanding data (other than 1741df8bae1dSRodney W. Grimes * a window probe), this is a completely 1742df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 1743df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 1744df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 1745df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 1746df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 1747df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 1748df8bae1dSRodney W. Grimes * window so we send only this one 1749df8bae1dSRodney W. Grimes * packet. 1750df8bae1dSRodney W. Grimes * 1751df8bae1dSRodney W. Grimes * We know we're losing at the current 1752df8bae1dSRodney W. Grimes * window size so do congestion avoidance 1753df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 1754df8bae1dSRodney W. Grimes * and pull our congestion window back to 1755df8bae1dSRodney W. Grimes * the new ssthresh). 1756df8bae1dSRodney W. Grimes * 1757df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 1758df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 1759df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 1760df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 1761df8bae1dSRodney W. Grimes * network. 1762df8bae1dSRodney W. Grimes */ 1763b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_REXMT) || 1764fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 1765df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1766cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 17673529149eSAndre Oppermann ((tcp_do_newreno || 17683529149eSAndre Oppermann (tp->t_flags & TF_SACK_PERMIT)) && 17699d11646dSJeffrey Hsu IN_FASTRECOVERY(tp))) { 17703529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 17713529149eSAndre Oppermann IN_FASTRECOVERY(tp)) { 1772808f11b7SPaul Saab int awnd; 177325e6f9edSPaul Saab 177425e6f9edSPaul Saab /* 177525e6f9edSPaul Saab * Compute the amount of data in flight first. 177625e6f9edSPaul Saab * We can inject new data into the pipe iff 177725e6f9edSPaul Saab * we have less than 1/2 the original window's 177825e6f9edSPaul Saab * worth of data in flight. 177925e6f9edSPaul Saab */ 1780808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 17810077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 1782808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 178325e6f9edSPaul Saab tp->snd_cwnd += tp->t_maxseg; 178425e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 178525e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 178625e6f9edSPaul Saab } 178725e6f9edSPaul Saab } else 178846f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 178946f58482SJonathan Lemon (void) tcp_output(tp); 179046f58482SJonathan Lemon goto drop; 1791cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 1792cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 1793cb942153SJeffrey Hsu u_int win; 1794a0445c2eSJayanth Vijayaraghavan 1795a0445c2eSJayanth Vijayaraghavan /* 1796a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 1797a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 1798a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 1799a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 1800a0445c2eSJayanth Vijayaraghavan * recovery. 1801a0445c2eSJayanth Vijayaraghavan */ 18023529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 1803a0445c2eSJayanth Vijayaraghavan if (IN_FASTRECOVERY(tp)) { 1804a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 1805a0445c2eSJayanth Vijayaraghavan break; 1806a0445c2eSJayanth Vijayaraghavan } 1807a0445c2eSJayanth Vijayaraghavan } else if (tcp_do_newreno) { 1808a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 18099d11646dSJeffrey Hsu tp->snd_recover)) { 1810cb942153SJeffrey Hsu tp->t_dupacks = 0; 1811cb942153SJeffrey Hsu break; 181246f58482SJonathan Lemon } 1813a0445c2eSJayanth Vijayaraghavan } 1814cb942153SJeffrey Hsu win = min(tp->snd_wnd, tp->snd_cwnd) / 1815cb942153SJeffrey Hsu 2 / tp->t_maxseg; 1816df8bae1dSRodney W. Grimes if (win < 2) 1817df8bae1dSRodney W. Grimes win = 2; 1818df8bae1dSRodney W. Grimes tp->snd_ssthresh = win * tp->t_maxseg; 18199d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 182046f58482SJonathan Lemon tp->snd_recover = tp->snd_max; 1821b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 18229b8b58e0SJonathan Lemon tp->t_rtttime = 0; 18233529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 18246d90faf3SPaul Saab tcpstat.tcps_sack_recovery_episode++; 1825a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 18268db456bfSPaul Saab tp->snd_cwnd = tp->t_maxseg; 18276d90faf3SPaul Saab (void) tcp_output(tp); 18286d90faf3SPaul Saab goto drop; 18296d90faf3SPaul Saab } 1830fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 1831df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 1832df8bae1dSRodney W. Grimes (void) tcp_output(tp); 183348d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 1834302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 1835302ce8d6SAndre Oppermann __func__)); 1836df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 183748d2549cSJeffrey Hsu tp->t_maxseg * 183848d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 1839df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 1840df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 1841df8bae1dSRodney W. Grimes goto drop; 1842582a954bSJeffrey Hsu } else if (tcp_do_rfc3042) { 1843582a954bSJeffrey Hsu u_long oldcwnd = tp->snd_cwnd; 184448d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 184548d2549cSJeffrey Hsu u_int sent; 184689c02376SJeffrey Hsu 1847582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 1848582a954bSJeffrey Hsu tp->t_dupacks == 2, 1849302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 1850302ce8d6SAndre Oppermann __func__)); 185161a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 185248d2549cSJeffrey Hsu tp->snd_limited = 0; 185361a36e3dSJeffrey Hsu tp->snd_cwnd = 185461a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 185561a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 185661a36e3dSJeffrey Hsu tp->t_maxseg; 1857582a954bSJeffrey Hsu (void) tcp_output(tp); 185848d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 185948d2549cSJeffrey Hsu if (sent > tp->t_maxseg) { 186089c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 186189c02376SJeffrey Hsu tp->snd_limited == 0) || 186289c02376SJeffrey Hsu (sent == tp->t_maxseg + 1 && 186389c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 1864302ce8d6SAndre Oppermann ("%s: sent too much", 1865302ce8d6SAndre Oppermann __func__)); 186648d2549cSJeffrey Hsu tp->snd_limited = 2; 186748d2549cSJeffrey Hsu } else if (sent > 0) 186848d2549cSJeffrey Hsu ++tp->snd_limited; 1869582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 1870582a954bSJeffrey Hsu goto drop; 1871df8bae1dSRodney W. Grimes } 1872df8bae1dSRodney W. Grimes } else 1873df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1874df8bae1dSRodney W. Grimes break; 1875df8bae1dSRodney W. Grimes } 1876cb942153SJeffrey Hsu 1877302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 1878302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 1879cb942153SJeffrey Hsu 1880df8bae1dSRodney W. Grimes /* 1881df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 1882df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 1883df8bae1dSRodney W. Grimes */ 18843529149eSAndre Oppermann if (tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) { 18859d11646dSJeffrey Hsu if (IN_FASTRECOVERY(tp)) { 1886cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 18873529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 18886d90faf3SPaul Saab tcp_sack_partialack(tp, th); 18896d90faf3SPaul Saab else 1890c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 1891c068736aSJeffrey Hsu } else { 1892c068736aSJeffrey Hsu /* 18936d90faf3SPaul Saab * Out of fast recovery. 1894c068736aSJeffrey Hsu * Window inflation should have left us 1895c068736aSJeffrey Hsu * with approximately snd_ssthresh 1896c068736aSJeffrey Hsu * outstanding data. 1897c068736aSJeffrey Hsu * But in case we would be inclined to 1898c068736aSJeffrey Hsu * send a burst, better to do it via 1899c068736aSJeffrey Hsu * the slow start mechanism. 1900c068736aSJeffrey Hsu */ 1901c068736aSJeffrey Hsu if (SEQ_GT(th->th_ack + 1902c068736aSJeffrey Hsu tp->snd_ssthresh, 1903c068736aSJeffrey Hsu tp->snd_max)) 1904c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_max - 1905c068736aSJeffrey Hsu th->th_ack + 1906c068736aSJeffrey Hsu tp->t_maxseg; 1907c068736aSJeffrey Hsu else 1908c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_ssthresh; 1909c068736aSJeffrey Hsu } 1910c068736aSJeffrey Hsu } 1911c068736aSJeffrey Hsu } else { 1912233e8c18SGarrett Wollman if (tp->t_dupacks >= tcprexmtthresh && 1913df8bae1dSRodney W. Grimes tp->snd_cwnd > tp->snd_ssthresh) 1914df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh; 191546f58482SJonathan Lemon } 1916cb942153SJeffrey Hsu tp->t_dupacks = 0; 1917a0292f23SGarrett Wollman /* 1918a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 1919a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 1920a0292f23SGarrett Wollman */ 1921a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 1922a0292f23SGarrett Wollman /* 1923a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 1924a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 192507e43e10SAndras Olah * synchronized). Go to non-starred state, 192607e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 192707e43e10SAndras Olah * we can do window scaling. 1928a0292f23SGarrett Wollman */ 1929a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 1930a0292f23SGarrett Wollman tp->snd_una++; 193107e43e10SAndras Olah /* Do window scaling? */ 193207e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 193307e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 193407e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 1935464fcfbcSAndre Oppermann /* Send window already scaled. */ 193607e43e10SAndras Olah } 1937a0292f23SGarrett Wollman } 1938a0292f23SGarrett Wollman 1939a0292f23SGarrett Wollman process_ACK: 1940302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: process_ACK: head not locked", 1941302ce8d6SAndre Oppermann __func__)); 19428501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 19437cfc6904SRobert Watson 1944fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 1945df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 1946df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 1947df8bae1dSRodney W. Grimes 1948df8bae1dSRodney W. Grimes /* 19499b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 19509b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 19519b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 19529b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 19539b8b58e0SJonathan Lemon * we left off. 19549b8b58e0SJonathan Lemon */ 19559b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 1956d65bf08aSMatthew Dillon ++tcpstat.tcps_sndrexmitbad; 19579b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 19589b8b58e0SJonathan Lemon tp->snd_ssthresh = tp->snd_ssthresh_prev; 19599d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 19609d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 19619d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 19629b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 19639b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; /* XXX probably not required */ 19649b8b58e0SJonathan Lemon } 19659b8b58e0SJonathan Lemon 19669b8b58e0SJonathan Lemon /* 1967df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 1968df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 1969df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 1970df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 1971df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 1972df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 1973df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 1974fa55172bSMatthew Dillon * 1975fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1976fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1977fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1978fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1979df8bae1dSRodney W. Grimes */ 1980fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1981fa55172bSMatthew Dillon to.to_tsecr) { 1982eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - to.to_tsecr) 1983eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 19849b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 1985fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 1986eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 1987eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 19889b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 1989fa55172bSMatthew Dillon } 19901fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 1991df8bae1dSRodney W. Grimes 1992df8bae1dSRodney W. Grimes /* 1993df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 1994df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 1995df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 1996df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 1997df8bae1dSRodney W. Grimes */ 1998fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 1999b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2000df8bae1dSRodney W. Grimes needoutput = 1; 2001b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2002b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2003a0292f23SGarrett Wollman 2004a0292f23SGarrett Wollman /* 2005a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2006a0292f23SGarrett Wollman * skip rest of ACK processing. 2007a0292f23SGarrett Wollman */ 2008a0292f23SGarrett Wollman if (acked == 0) 2009a0292f23SGarrett Wollman goto step6; 2010a0292f23SGarrett Wollman 2011df8bae1dSRodney W. Grimes /* 2012df8bae1dSRodney W. Grimes * When new data is acked, open the congestion window. 2013df8bae1dSRodney W. Grimes * If the window gives us less than ssthresh packets 2014df8bae1dSRodney W. Grimes * in flight, open exponentially (maxseg per packet). 2015df8bae1dSRodney W. Grimes * Otherwise open linearly: maxseg per window 201610be5648SGarrett Wollman * (maxseg^2 / cwnd per packet). 2017df8bae1dSRodney W. Grimes */ 20183529149eSAndre Oppermann if ((!tcp_do_newreno && !(tp->t_flags & TF_SACK_PERMIT)) || 20196d90faf3SPaul Saab !IN_FASTRECOVERY(tp)) { 2020ad3f9ab3SAndre Oppermann u_int cw = tp->snd_cwnd; 2021ad3f9ab3SAndre Oppermann u_int incr = tp->t_maxseg; 2022df8bae1dSRodney W. Grimes if (cw > tp->snd_ssthresh) 202310be5648SGarrett Wollman incr = incr * incr / cw; 2024df8bae1dSRodney W. Grimes tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale); 2025df8bae1dSRodney W. Grimes } 20265905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2027df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 2028df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 20295905999bSRobert Watson sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc); 2030df8bae1dSRodney W. Grimes ourfinisacked = 1; 2031df8bae1dSRodney W. Grimes } else { 20325905999bSRobert Watson sbdrop_locked(&so->so_snd, acked); 2033df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 2034df8bae1dSRodney W. Grimes ourfinisacked = 0; 2035df8bae1dSRodney W. Grimes } 2036564aab1fSAndre Oppermann /* NB: sowwakeup_locked() does an implicit unlock. */ 20371e4d7da7SRobert Watson sowwakeup_locked(so); 2038e8949f74SAndre Oppermann /* Detect una wraparound. */ 20393529149eSAndre Oppermann if ((tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) && 20406d90faf3SPaul Saab !IN_FASTRECOVERY(tp) && 20419d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 20429d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 20439d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 20443529149eSAndre Oppermann if ((tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) && 20456d90faf3SPaul Saab IN_FASTRECOVERY(tp) && 20469d11646dSJeffrey Hsu SEQ_GEQ(th->th_ack, tp->snd_recover)) 20479d11646dSJeffrey Hsu EXIT_FASTRECOVERY(tp); 2048fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 20493529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 20506d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 20516d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 20526d90faf3SPaul Saab } 2053df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2054df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2055df8bae1dSRodney W. Grimes 2056df8bae1dSRodney W. Grimes switch (tp->t_state) { 2057df8bae1dSRodney W. Grimes 2058df8bae1dSRodney W. Grimes /* 2059df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2060df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2061df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2062df8bae1dSRodney W. Grimes */ 2063df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2064df8bae1dSRodney W. Grimes if (ourfinisacked) { 2065df8bae1dSRodney W. Grimes /* 2066df8bae1dSRodney W. Grimes * If we can't receive any more 2067df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2068df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2069df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2070df8bae1dSRodney W. Grimes * we'll hang forever. 2071e8949f74SAndre Oppermann * 2072e8949f74SAndre Oppermann * XXXjl: 2073340c35deSJonathan Lemon * we should release the tp also, and use a 2074340c35deSJonathan Lemon * compressed state. 2075340c35deSJonathan Lemon */ 2076c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 20777c72af87SMohan Srinivasan int timeout; 20787c72af87SMohan Srinivasan 207903e49181SSeigo Tanimura soisdisconnected(so); 20807c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 20817c72af87SMohan Srinivasan tcp_finwait2_timeout : tcp_maxidle; 2082b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 20834cc20ab1SSeigo Tanimura } 2084df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 2085df8bae1dSRodney W. Grimes } 2086df8bae1dSRodney W. Grimes break; 2087df8bae1dSRodney W. Grimes 2088df8bae1dSRodney W. Grimes /* 2089df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2090df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2091df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2092df8bae1dSRodney W. Grimes * the segment. 2093df8bae1dSRodney W. Grimes */ 2094df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2095df8bae1dSRodney W. Grimes if (ourfinisacked) { 2096302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: process_ACK: " 2097302ce8d6SAndre Oppermann "head not locked", __func__)); 209811a20fb8SJeffrey Hsu tcp_twstart(tp); 2099340c35deSJonathan Lemon INP_INFO_WUNLOCK(&tcbinfo); 2100302ce8d6SAndre Oppermann headlocked = 0; 2101340c35deSJonathan Lemon m_freem(m); 2102679d9708SAndre Oppermann return; 2103df8bae1dSRodney W. Grimes } 2104df8bae1dSRodney W. Grimes break; 2105df8bae1dSRodney W. Grimes 2106df8bae1dSRodney W. Grimes /* 2107df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2108df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2109df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2110df8bae1dSRodney W. Grimes * enter the closed state and return. 2111df8bae1dSRodney W. Grimes */ 2112df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2113df8bae1dSRodney W. Grimes if (ourfinisacked) { 2114302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: process_ACK: " 2115302ce8d6SAndre Oppermann "tcp_close: head not locked", __func__)); 2116df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2117df8bae1dSRodney W. Grimes goto drop; 2118df8bae1dSRodney W. Grimes } 2119df8bae1dSRodney W. Grimes break; 2120df8bae1dSRodney W. Grimes } 2121df8bae1dSRodney W. Grimes } 2122df8bae1dSRodney W. Grimes 2123df8bae1dSRodney W. Grimes step6: 2124302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: step6: head not locked", __func__)); 21258501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 21267cfc6904SRobert Watson 2127df8bae1dSRodney W. Grimes /* 2128df8bae1dSRodney W. Grimes * Update window information. 2129df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 2130df8bae1dSRodney W. Grimes */ 2131fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 2132fb59c426SYoshinobu Inoue (SEQ_LT(tp->snd_wl1, th->th_seq) || 2133fb59c426SYoshinobu Inoue (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2134fb59c426SYoshinobu Inoue (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2135df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 2136fb59c426SYoshinobu Inoue if (tlen == 0 && 2137fb59c426SYoshinobu Inoue tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2138df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinupd++; 2139df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 2140fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq; 2141fb59c426SYoshinobu Inoue tp->snd_wl2 = th->th_ack; 2142df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2143df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 2144df8bae1dSRodney W. Grimes needoutput = 1; 2145df8bae1dSRodney W. Grimes } 2146df8bae1dSRodney W. Grimes 2147df8bae1dSRodney W. Grimes /* 2148df8bae1dSRodney W. Grimes * Process segments with URG. 2149df8bae1dSRodney W. Grimes */ 2150fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2151df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2152df8bae1dSRodney W. Grimes /* 2153df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2154df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2155df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2156df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2157df8bae1dSRodney W. Grimes */ 215818ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2159fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2160fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2161fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 216218ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2163df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2164df8bae1dSRodney W. Grimes } 2165df8bae1dSRodney W. Grimes /* 2166df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2167df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2168df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2169df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2170df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2171df8bae1dSRodney W. Grimes * 2172df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2173df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2174df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2175df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2176df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2177df8bae1dSRodney W. Grimes * spec states (in one of two places). 2178df8bae1dSRodney W. Grimes */ 2179fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2180fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2181df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2182df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2183927c5ceaSRobert Watson if (so->so_oobmark == 0) 2184c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 2185df8bae1dSRodney W. Grimes sohasoutofband(so); 2186df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2187df8bae1dSRodney W. Grimes } 218818ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2189df8bae1dSRodney W. Grimes /* 2190df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2191df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2192df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2193df8bae1dSRodney W. Grimes * data may creep in... ick. 2194df8bae1dSRodney W. Grimes */ 219530613f56SJeffrey Hsu if (th->th_urp <= (u_long)tlen && 219630613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 219730613f56SJeffrey Hsu /* hdr drop is delayed */ 219830613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 219930613f56SJeffrey Hsu } 2200c068736aSJeffrey Hsu } else { 2201df8bae1dSRodney W. Grimes /* 2202df8bae1dSRodney W. Grimes * If no out of band data is expected, 2203df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2204df8bae1dSRodney W. Grimes * with the receive window. 2205df8bae1dSRodney W. Grimes */ 2206df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2207df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2208c068736aSJeffrey Hsu } 2209df8bae1dSRodney W. Grimes dodata: /* XXX */ 2210302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: dodata: head not locked", __func__)); 22118501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 22127cfc6904SRobert Watson 2213df8bae1dSRodney W. Grimes /* 2214df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2215df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2216df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2217df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2218df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2219df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2220df8bae1dSRodney W. Grimes */ 2221fb59c426SYoshinobu Inoue if ((tlen || (thflags & TH_FIN)) && 2222df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 222369e03620SPaul Saab tcp_seq save_start = th->th_seq; 2224fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2225e4b64281SJesper Skriver /* 2226c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2227c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2228c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2229c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2230c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2231c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2232c068736aSJeffrey Hsu * conversions. 2233c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 2234c068736aSJeffrey Hsu * immediately when segments are out of order (so 2235c068736aSJeffrey Hsu * fast retransmit can work). 2236e4b64281SJesper Skriver */ 2237e4b64281SJesper Skriver if (th->th_seq == tp->rcv_nxt && 2238e4b64281SJesper Skriver LIST_EMPTY(&tp->t_segq) && 2239e4b64281SJesper Skriver TCPS_HAVEESTABLISHED(tp->t_state)) { 2240e4b64281SJesper Skriver if (DELAY_ACK(tp)) 22413bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2242e4b64281SJesper Skriver else 2243e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2244e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2245e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 2246e4b64281SJesper Skriver tcpstat.tcps_rcvpack++; 2247e4b64281SJesper Skriver tcpstat.tcps_rcvbyte += tlen; 2248e4b64281SJesper Skriver ND6_HINT(tp); 22491e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2250c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2251c1c36a2cSMike Silbersack m_freem(m); 2252c1c36a2cSMike Silbersack else 22531e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 2254e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 22551e4d7da7SRobert Watson sorwakeup_locked(so); 2256e4b64281SJesper Skriver } else { 2257e8949f74SAndre Oppermann /* 2258e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 2259e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 2260e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 2261e8949f74SAndre Oppermann * when trimming from the head. 2262e8949f74SAndre Oppermann */ 2263e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 2264e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2265e4b64281SJesper Skriver } 22663529149eSAndre Oppermann if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 2267f194524fSAndre Oppermann tcp_update_sack_list(tp, save_start, save_start + tlen); 2268302ce8d6SAndre Oppermann #if 0 2269df8bae1dSRodney W. Grimes /* 2270df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2271df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2272df8bae1dSRodney W. Grimes * buffer size. 2273302ce8d6SAndre Oppermann * XXX: Unused. 2274df8bae1dSRodney W. Grimes */ 2275df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2276302ce8d6SAndre Oppermann #endif 2277df8bae1dSRodney W. Grimes } else { 2278df8bae1dSRodney W. Grimes m_freem(m); 2279fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2280df8bae1dSRodney W. Grimes } 2281df8bae1dSRodney W. Grimes 2282df8bae1dSRodney W. Grimes /* 2283df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2284df8bae1dSRodney W. Grimes * that the connection is closing. 2285df8bae1dSRodney W. Grimes */ 2286fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2287df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2288df8bae1dSRodney W. Grimes socantrcvmore(so); 2289a0292f23SGarrett Wollman /* 2290a0292f23SGarrett Wollman * If connection is half-synchronized 2291d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2292a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2293a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2294a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2295a0292f23SGarrett Wollman */ 22963bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 22973bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2298a0292f23SGarrett Wollman else 2299df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2300df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2301df8bae1dSRodney W. Grimes } 2302df8bae1dSRodney W. Grimes switch (tp->t_state) { 2303df8bae1dSRodney W. Grimes 2304df8bae1dSRodney W. Grimes /* 2305df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2306df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2307df8bae1dSRodney W. Grimes */ 2308df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 23099b8b58e0SJonathan Lemon tp->t_starttime = ticks; 23109b8b58e0SJonathan Lemon /* FALLTHROUGH */ 2311df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2312df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2313df8bae1dSRodney W. Grimes break; 2314df8bae1dSRodney W. Grimes 2315df8bae1dSRodney W. Grimes /* 2316df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2317df8bae1dSRodney W. Grimes * enter the CLOSING state. 2318df8bae1dSRodney W. Grimes */ 2319df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2320df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 2321df8bae1dSRodney W. Grimes break; 2322df8bae1dSRodney W. Grimes 2323df8bae1dSRodney W. Grimes /* 2324df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 2325df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 2326df8bae1dSRodney W. Grimes * standard timers. 2327df8bae1dSRodney W. Grimes */ 2328df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2329302ce8d6SAndre Oppermann KASSERT(headlocked == 1, ("%s: dodata: " 2330302ce8d6SAndre Oppermann "TCP_FIN_WAIT_2: head not locked", __func__)); 2331340c35deSJonathan Lemon tcp_twstart(tp); 233211a20fb8SJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2333679d9708SAndre Oppermann return; 2334df8bae1dSRodney W. Grimes } 2335df8bae1dSRodney W. Grimes } 2336e0bef1cbSRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 2337e0bef1cbSRobert Watson headlocked = 0; 2338610ee2f9SDavid Greenman #ifdef TCPDEBUG 23394cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2340fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2341fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2342610ee2f9SDavid Greenman #endif 2343df8bae1dSRodney W. Grimes 2344df8bae1dSRodney W. Grimes /* 2345df8bae1dSRodney W. Grimes * Return any desired output. 2346df8bae1dSRodney W. Grimes */ 2347df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 2348df8bae1dSRodney W. Grimes (void) tcp_output(tp); 23497792ea27SJeffrey Hsu 2350a14c749fSJonathan Lemon check_delack: 2351302ce8d6SAndre Oppermann KASSERT(headlocked == 0, ("%s: check_delack: head locked", 2352302ce8d6SAndre Oppermann __func__)); 23537824d002SAndre Oppermann INP_INFO_UNLOCK_ASSERT(&tcbinfo); 23548501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 2355a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 23563bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 2357b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 23583bfd6421SJonathan Lemon } 23598501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2360679d9708SAndre Oppermann return; 2361df8bae1dSRodney W. Grimes 2362df8bae1dSRodney W. Grimes dropafterack: 2363302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: dropafterack: head not locked", __func__)); 2364df8bae1dSRodney W. Grimes /* 2365df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 2366df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 236780ab7c0eSGarrett Wollman * 236880ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 236980ab7c0eSGarrett Wollman * paths to this code happen after packets containing 237080ab7c0eSGarrett Wollman * RST have been dropped. 237180ab7c0eSGarrett Wollman * 237280ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 237380ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 237480ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 237580ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 237680ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 237780ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 2378df8bae1dSRodney W. Grimes */ 2379fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2380fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 2381a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 2382a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2383a57815efSBosko Milekic goto dropwithreset; 2384a57815efSBosko Milekic } 2385a0292f23SGarrett Wollman #ifdef TCPDEBUG 23864cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2387fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2388fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2389a0292f23SGarrett Wollman #endif 2390302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: headlocked should be 1", __func__)); 239142cf3289SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 2392df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2393df8bae1dSRodney W. Grimes (void) tcp_output(tp); 23948501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2395d6915262SRobert Watson m_freem(m); 2396679d9708SAndre Oppermann return; 2397df8bae1dSRodney W. Grimes 2398df8bae1dSRodney W. Grimes dropwithreset: 2399302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: dropwithreset: head not locked", __func__)); 2400a57815efSBosko Milekic 2401302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 2402c29afad6SSam Leffler 24031c53f806SRobert Watson if (tp != NULL) 24048501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2405f76fcf6dSJeffrey Hsu if (headlocked) 2406f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2407679d9708SAndre Oppermann return; 2408df8bae1dSRodney W. Grimes 2409df8bae1dSRodney W. Grimes drop: 2410df8bae1dSRodney W. Grimes /* 2411df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 2412df8bae1dSRodney W. Grimes */ 2413610ee2f9SDavid Greenman #ifdef TCPDEBUG 24141c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2415fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2416fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2417a0292f23SGarrett Wollman #endif 24181c53f806SRobert Watson if (tp != NULL) 24198501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2420f76fcf6dSJeffrey Hsu if (headlocked) 2421f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2422d6915262SRobert Watson m_freem(m); 2423679d9708SAndre Oppermann return; 2424302ce8d6SAndre Oppermann } 2425302ce8d6SAndre Oppermann 2426302ce8d6SAndre Oppermann /* 24270ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 24280ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 24290ca3f933SAndre Oppermann * tp may be NULL. 2430302ce8d6SAndre Oppermann */ 2431302ce8d6SAndre Oppermann static void 2432302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 2433302ce8d6SAndre Oppermann int tlen, int rstreason) 2434302ce8d6SAndre Oppermann { 2435302ce8d6SAndre Oppermann struct ip *ip; 2436302ce8d6SAndre Oppermann #ifdef INET6 2437302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 2438302ce8d6SAndre Oppermann #endif 24397a3244ccSRobert Watson 24407a3244ccSRobert Watson if (tp != NULL) { 24418501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 24427a3244ccSRobert Watson } 24437a3244ccSRobert Watson 24440ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 2445302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2446302ce8d6SAndre Oppermann goto drop; 2447302ce8d6SAndre Oppermann #ifdef INET6 2448302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 2449302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 2450302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2451302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2452302ce8d6SAndre Oppermann goto drop; 2453302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 2454302ce8d6SAndre Oppermann } else 2455302ce8d6SAndre Oppermann #endif 2456302ce8d6SAndre Oppermann { 2457302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 2458302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2459302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2460302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 2461302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2462302ce8d6SAndre Oppermann goto drop; 2463302ce8d6SAndre Oppermann } 2464302ce8d6SAndre Oppermann 2465302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 2466302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 2467302ce8d6SAndre Oppermann goto drop; 2468302ce8d6SAndre Oppermann 2469302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 2470302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 2471302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 2472302ce8d6SAndre Oppermann th->th_ack, TH_RST); 2473302ce8d6SAndre Oppermann } else { 2474302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 2475302ce8d6SAndre Oppermann tlen++; 2476302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2477302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 2478302ce8d6SAndre Oppermann } 2479302ce8d6SAndre Oppermann return; 2480302ce8d6SAndre Oppermann drop: 2481302ce8d6SAndre Oppermann m_freem(m); 2482df8bae1dSRodney W. Grimes return; 2483df8bae1dSRodney W. Grimes } 2484df8bae1dSRodney W. Grimes 2485be2ac88cSJonathan Lemon /* 2486be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 2487be2ac88cSJonathan Lemon */ 24880312fbe9SPoul-Henning Kamp static void 2489ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 2490df8bae1dSRodney W. Grimes { 2491df8bae1dSRodney W. Grimes int opt, optlen; 2492df8bae1dSRodney W. Grimes 2493be2ac88cSJonathan Lemon to->to_flags = 0; 2494df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 2495df8bae1dSRodney W. Grimes opt = cp[0]; 2496df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 2497df8bae1dSRodney W. Grimes break; 2498df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 2499df8bae1dSRodney W. Grimes optlen = 1; 2500df8bae1dSRodney W. Grimes else { 2501b474779fSJun-ichiro itojun Hagino if (cnt < 2) 2502b474779fSJun-ichiro itojun Hagino break; 2503df8bae1dSRodney W. Grimes optlen = cp[1]; 2504b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 2505df8bae1dSRodney W. Grimes break; 2506df8bae1dSRodney W. Grimes } 2507df8bae1dSRodney W. Grimes switch (opt) { 2508df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 2509df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 2510df8bae1dSRodney W. Grimes continue; 2511f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2512df8bae1dSRodney W. Grimes continue; 2513be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 2514be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 2515be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 2516fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 2517df8bae1dSRodney W. Grimes break; 2518df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 2519df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 2520df8bae1dSRodney W. Grimes continue; 2521f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2522df8bae1dSRodney W. Grimes continue; 2523be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 252402a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 2525df8bae1dSRodney W. Grimes break; 2526df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 2527df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 2528df8bae1dSRodney W. Grimes continue; 2529be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 2530a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2531a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 2532fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 2533a0292f23SGarrett Wollman bcopy((char *)cp + 6, 2534a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2535fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 2536df8bae1dSRodney W. Grimes break; 25371cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 25381cfd4b53SBruce M Simpson /* 25391cfd4b53SBruce M Simpson * XXX In order to reply to a host which has set the 25401cfd4b53SBruce M Simpson * TCP_SIGNATURE option in its initial SYN, we have to 25411cfd4b53SBruce M Simpson * record the fact that the option was observed here 25421cfd4b53SBruce M Simpson * for the syncache code to perform the correct response. 25431cfd4b53SBruce M Simpson */ 25441cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 25451cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 25461cfd4b53SBruce M Simpson continue; 2547df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 2548df47e437SAndre Oppermann to->to_signature = cp + 2; 25491cfd4b53SBruce M Simpson break; 2550265ed012SBruce M Simpson #endif 25516d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 2552f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 25536d90faf3SPaul Saab continue; 2554f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2555f72167f4SAndre Oppermann continue; 2556f72167f4SAndre Oppermann if (!tcp_do_sack) 2557f72167f4SAndre Oppermann continue; 2558fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 25596d90faf3SPaul Saab break; 25606d90faf3SPaul Saab case TCPOPT_SACK: 25615a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 25626d90faf3SPaul Saab continue; 2563b728e902SAndre Oppermann if (flags & TO_SYN) 2564b728e902SAndre Oppermann continue; 2565fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 25665a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 25675a53ca16SPaul Saab to->to_sacks = cp + 2; 25685a53ca16SPaul Saab tcpstat.tcps_sack_rcv_blocks++; 25696d90faf3SPaul Saab break; 2570be2ac88cSJonathan Lemon default: 2571be2ac88cSJonathan Lemon continue; 2572df8bae1dSRodney W. Grimes } 2573df8bae1dSRodney W. Grimes } 2574df8bae1dSRodney W. Grimes } 2575df8bae1dSRodney W. Grimes 2576df8bae1dSRodney W. Grimes /* 2577df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 2578df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 2579df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 2580df8bae1dSRodney W. Grimes * sequencing purposes. 2581df8bae1dSRodney W. Grimes */ 25820312fbe9SPoul-Henning Kamp static void 2583ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 2584ad3f9ab3SAndre Oppermann int off) 2585df8bae1dSRodney W. Grimes { 2586fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 2587df8bae1dSRodney W. Grimes 2588df8bae1dSRodney W. Grimes while (cnt >= 0) { 2589df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 2590df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 2591df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 2592df8bae1dSRodney W. Grimes 25938501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 25947a3244ccSRobert Watson 2595df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 2596df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 2597df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2598df8bae1dSRodney W. Grimes m->m_len--; 259969a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 260069a34685SYoshinobu Inoue m->m_pkthdr.len--; 2601df8bae1dSRodney W. Grimes return; 2602df8bae1dSRodney W. Grimes } 2603df8bae1dSRodney W. Grimes cnt -= m->m_len; 2604df8bae1dSRodney W. Grimes m = m->m_next; 26054dfdffe9SAndre Oppermann if (m == NULL) 2606df8bae1dSRodney W. Grimes break; 2607df8bae1dSRodney W. Grimes } 2608df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 2609df8bae1dSRodney W. Grimes } 2610df8bae1dSRodney W. Grimes 2611df8bae1dSRodney W. Grimes /* 2612df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 2613df8bae1dSRodney W. Grimes * and update averages and current timeout. 2614df8bae1dSRodney W. Grimes */ 26150312fbe9SPoul-Henning Kamp static void 2616ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 2617df8bae1dSRodney W. Grimes { 2618ad3f9ab3SAndre Oppermann int delta; 2619233e8c18SGarrett Wollman 26208501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 26212be3bf22SRobert Watson 2622233e8c18SGarrett Wollman tcpstat.tcps_rttupdated++; 2623233e8c18SGarrett Wollman tp->t_rttupdated++; 2624233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 2625233e8c18SGarrett Wollman /* 2626233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 2627233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 2628233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 2629233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2630233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 2631233e8c18SGarrett Wollman */ 2632233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2633233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2634233e8c18SGarrett Wollman 2635233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 2636233e8c18SGarrett Wollman tp->t_srtt = 1; 2637233e8c18SGarrett Wollman 2638233e8c18SGarrett Wollman /* 2639233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 2640233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 2641233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 2642233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 2643233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 2644233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 2645233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2646233e8c18SGarrett Wollman * rfc793's wired-in beta. 2647233e8c18SGarrett Wollman */ 2648233e8c18SGarrett Wollman if (delta < 0) 2649233e8c18SGarrett Wollman delta = -delta; 2650233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2651233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 2652233e8c18SGarrett Wollman tp->t_rttvar = 1; 26531fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 26541fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2655233e8c18SGarrett Wollman } else { 2656233e8c18SGarrett Wollman /* 2657233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 2658233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 2659233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 2660233e8c18SGarrett Wollman */ 2661233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 2662233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 26631fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2664233e8c18SGarrett Wollman } 26659b8b58e0SJonathan Lemon tp->t_rtttime = 0; 2666df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 2667df8bae1dSRodney W. Grimes 2668df8bae1dSRodney W. Grimes /* 2669df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 2670df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 2671df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 2672df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 2673df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 2674df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 2675df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 2676df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 2677df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 2678df8bae1dSRodney W. Grimes */ 2679233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 26809e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2681df8bae1dSRodney W. Grimes 2682df8bae1dSRodney W. Grimes /* 2683df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 2684df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 2685df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 2686df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 2687df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 2688df8bae1dSRodney W. Grimes */ 2689df8bae1dSRodney W. Grimes tp->t_softerror = 0; 2690df8bae1dSRodney W. Grimes } 2691df8bae1dSRodney W. Grimes 2692df8bae1dSRodney W. Grimes /* 2693df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 2694df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 2695df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 2696df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 2697df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2698df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 2699df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 2700df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 2701df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 2702df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 2703df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 2704df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 2705df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 2706a0292f23SGarrett Wollman * 2707a0292f23SGarrett Wollman * Also take into account the space needed for options that we 2708a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 2709a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 2710a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 2711a0292f23SGarrett Wollman * data in maxopd. 2712a0292f23SGarrett Wollman * 2713a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 2714a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 2715a0292f23SGarrett Wollman * MSS of our peer. 271697d8d152SAndre Oppermann * 271797d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 271897d8d152SAndre Oppermann * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt(). 2719df8bae1dSRodney W. Grimes */ 2720a0292f23SGarrett Wollman void 2721ad3f9ab3SAndre Oppermann tcp_mss(struct tcpcb *tp, int offer) 2722df8bae1dSRodney W. Grimes { 272397d8d152SAndre Oppermann int rtt, mss; 2724df8bae1dSRodney W. Grimes u_long bufsize; 272597d8d152SAndre Oppermann u_long maxmtu; 2726c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 2727df8bae1dSRodney W. Grimes struct socket *so; 272897d8d152SAndre Oppermann struct hc_metrics_lite metrics; 2729a0292f23SGarrett Wollman int origoffer = offer; 2730233dcce1SAndre Oppermann int mtuflags = 0; 2731fb59c426SYoshinobu Inoue #ifdef INET6 2732c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2733c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 2734c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 2735c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 2736c068736aSJeffrey Hsu #else 2737c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 2738fb59c426SYoshinobu Inoue #endif 2739df8bae1dSRodney W. Grimes 27408501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 27417a3244ccSRobert Watson 2742e8949f74SAndre Oppermann /* Initialize. */ 274397d8d152SAndre Oppermann #ifdef INET6 274497d8d152SAndre Oppermann if (isipv6) { 2745233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(&inp->inp_inc, &mtuflags); 274697d8d152SAndre Oppermann tp->t_maxopd = tp->t_maxseg = tcp_v6mssdflt; 274797d8d152SAndre Oppermann } else 274897d8d152SAndre Oppermann #endif 274997d8d152SAndre Oppermann { 2750233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(&inp->inp_inc, &mtuflags); 275197d8d152SAndre Oppermann tp->t_maxopd = tp->t_maxseg = tcp_mssdflt; 2752df8bae1dSRodney W. Grimes } 2753df8bae1dSRodney W. Grimes 275497d8d152SAndre Oppermann /* 2755e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 275697d8d152SAndre Oppermann */ 275797d8d152SAndre Oppermann if (maxmtu == 0) 275897d8d152SAndre Oppermann return; 275997d8d152SAndre Oppermann 2760e8949f74SAndre Oppermann /* What have we got? */ 276197d8d152SAndre Oppermann switch (offer) { 276297d8d152SAndre Oppermann case 0: 276397d8d152SAndre Oppermann /* 276497d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 2765c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 2766c3b02504SBjoern A. Zeeb * already assigned to t_maxopd above. 276797d8d152SAndre Oppermann */ 2768c3b02504SBjoern A. Zeeb offer = tp->t_maxopd; 276997d8d152SAndre Oppermann break; 277097d8d152SAndre Oppermann 277197d8d152SAndre Oppermann case -1: 2772a0292f23SGarrett Wollman /* 2773c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 2774a0292f23SGarrett Wollman */ 277597d8d152SAndre Oppermann /* FALLTHROUGH */ 277697d8d152SAndre Oppermann 277797d8d152SAndre Oppermann default: 2778a0292f23SGarrett Wollman /* 277953369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 278053369ac9SAndre Oppermann * to at least minmss. 278153369ac9SAndre Oppermann */ 278253369ac9SAndre Oppermann offer = max(offer, tcp_minmss); 278353369ac9SAndre Oppermann /* 2784a0292f23SGarrett Wollman * Sanity check: make sure that maxopd will be large 278597d8d152SAndre Oppermann * enough to allow some data on segments even if the 2786a0292f23SGarrett Wollman * all the option space is used (40bytes). Otherwise 2787a0292f23SGarrett Wollman * funny things may happen in tcp_output. 2788a0292f23SGarrett Wollman */ 2789a0292f23SGarrett Wollman offer = max(offer, 64); 279097d8d152SAndre Oppermann } 2791a0292f23SGarrett Wollman 2792df8bae1dSRodney W. Grimes /* 2793e8949f74SAndre Oppermann * rmx information is now retrieved from tcp_hostcache. 2794df8bae1dSRodney W. Grimes */ 279597d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 279697d8d152SAndre Oppermann 2797df8bae1dSRodney W. Grimes /* 2798e8949f74SAndre Oppermann * If there's a discovered mtu int tcp hostcache, use it 2799fb59c426SYoshinobu Inoue * else, use the link mtu. 2800df8bae1dSRodney W. Grimes */ 280197d8d152SAndre Oppermann if (metrics.rmx_mtu) 28022d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 2803c068736aSJeffrey Hsu else { 280431b3783cSHajimu UMEMOTO #ifdef INET6 2805fb59c426SYoshinobu Inoue if (isipv6) { 280697d8d152SAndre Oppermann mss = maxmtu - min_protoh; 280797d8d152SAndre Oppermann if (!path_mtu_discovery && 280897d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 2809fb59c426SYoshinobu Inoue mss = min(mss, tcp_v6mssdflt); 2810b3399803SHajimu UMEMOTO } else 2811b3399803SHajimu UMEMOTO #endif 281297d8d152SAndre Oppermann { 281397d8d152SAndre Oppermann mss = maxmtu - min_protoh; 281497d8d152SAndre Oppermann if (!path_mtu_discovery && 281597d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 2816a0292f23SGarrett Wollman mss = min(mss, tcp_mssdflt); 2817a0292f23SGarrett Wollman } 281897d8d152SAndre Oppermann } 2819a0292f23SGarrett Wollman mss = min(mss, offer); 282097d8d152SAndre Oppermann 2821a0292f23SGarrett Wollman /* 2822a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 2823a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 2824a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 2825a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 2826a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 2827a0292f23SGarrett Wollman */ 2828a0292f23SGarrett Wollman tp->t_maxopd = mss; 2829a0292f23SGarrett Wollman 2830a0292f23SGarrett Wollman /* 2831e8949f74SAndre Oppermann * origoffer==-1 indicates that no segments were received yet. 2832c94c54e4SAndre Oppermann * In this case we just guess. 2833a0292f23SGarrett Wollman */ 2834a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2835a0292f23SGarrett Wollman (origoffer == -1 || 2836a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2837a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 2838a0292f23SGarrett Wollman 2839df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 2840df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2841df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 2842df8bae1dSRodney W. Grimes #else 2843df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2844df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 2845df8bae1dSRodney W. Grimes #endif 284697d8d152SAndre Oppermann tp->t_maxseg = mss; 284797d8d152SAndre Oppermann 2848df8bae1dSRodney W. Grimes /* 284997d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 285097d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 285197d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 285297d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 285397d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 2854df8bae1dSRodney W. Grimes */ 2855c3b02504SBjoern A. Zeeb so = inp->inp_socket; 28563f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 285797d8d152SAndre Oppermann if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe) 285897d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 285997d8d152SAndre Oppermann else 2860df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 2861df8bae1dSRodney W. Grimes if (bufsize < mss) 2862df8bae1dSRodney W. Grimes mss = bufsize; 2863df8bae1dSRodney W. Grimes else { 2864df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2865df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2866df8bae1dSRodney W. Grimes bufsize = sb_max; 286788c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 28683f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 2869df8bae1dSRodney W. Grimes } 28703f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 2871df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 2872df8bae1dSRodney W. Grimes 28733f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 287497d8d152SAndre Oppermann if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe) 287597d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 287697d8d152SAndre Oppermann else 2877df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 2878df8bae1dSRodney W. Grimes if (bufsize > mss) { 2879df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2880df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2881df8bae1dSRodney W. Grimes bufsize = sb_max; 288288c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 28833f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 2884df8bae1dSRodney W. Grimes } 28853f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2886a0292f23SGarrett Wollman /* 2887e8949f74SAndre Oppermann * While we're here, check the others too. 2888a0292f23SGarrett Wollman */ 288997d8d152SAndre Oppermann if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 289097d8d152SAndre Oppermann tp->t_srtt = rtt; 289197d8d152SAndre Oppermann tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 289297d8d152SAndre Oppermann tcpstat.tcps_usedrtt++; 289397d8d152SAndre Oppermann if (metrics.rmx_rttvar) { 289497d8d152SAndre Oppermann tp->t_rttvar = metrics.rmx_rttvar; 289597d8d152SAndre Oppermann tcpstat.tcps_usedrttvar++; 289697d8d152SAndre Oppermann } else { 289797d8d152SAndre Oppermann /* default variation is +- 1 rtt */ 289897d8d152SAndre Oppermann tp->t_rttvar = 289997d8d152SAndre Oppermann tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 290097d8d152SAndre Oppermann } 290197d8d152SAndre Oppermann TCPT_RANGESET(tp->t_rxtcur, 290297d8d152SAndre Oppermann ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 290397d8d152SAndre Oppermann tp->t_rttmin, TCPTV_REXMTMAX); 290497d8d152SAndre Oppermann } 290597d8d152SAndre Oppermann if (metrics.rmx_ssthresh) { 2906df8bae1dSRodney W. Grimes /* 2907df8bae1dSRodney W. Grimes * There's some sort of gateway or interface 2908df8bae1dSRodney W. Grimes * buffer limit on the path. Use this to set 2909df8bae1dSRodney W. Grimes * the slow start threshhold, but set the 2910df8bae1dSRodney W. Grimes * threshold to no less than 2*mss. 2911df8bae1dSRodney W. Grimes */ 291297d8d152SAndre Oppermann tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh); 2913dd224982SGarrett Wollman tcpstat.tcps_usedssthresh++; 2914df8bae1dSRodney W. Grimes } 291597d8d152SAndre Oppermann if (metrics.rmx_bandwidth) 291697d8d152SAndre Oppermann tp->snd_bandwidth = metrics.rmx_bandwidth; 291797d8d152SAndre Oppermann 291897d8d152SAndre Oppermann /* 291997d8d152SAndre Oppermann * Set the slow-start flight size depending on whether this 292097d8d152SAndre Oppermann * is a local network or not. 292197d8d152SAndre Oppermann * 292297d8d152SAndre Oppermann * Extend this so we cache the cwnd too and retrieve it here. 292397d8d152SAndre Oppermann * Make cwnd even bigger than RFC3390 suggests but only if we 292497d8d152SAndre Oppermann * have previous experience with the remote host. Be careful 292597d8d152SAndre Oppermann * not make cwnd bigger than remote receive window or our own 292697d8d152SAndre Oppermann * send socket buffer. Maybe put some additional upper bound 292797d8d152SAndre Oppermann * on the retrieved cwnd. Should do incremental updates to 292897d8d152SAndre Oppermann * hostcache when cwnd collapses so next connection doesn't 292997d8d152SAndre Oppermann * overloads the path again. 293097d8d152SAndre Oppermann * 293197d8d152SAndre Oppermann * RFC3390 says only do this if SYN or SYN/ACK didn't got lost. 293297d8d152SAndre Oppermann * We currently check only in syncache_socket for that. 293397d8d152SAndre Oppermann */ 293497d8d152SAndre Oppermann #define TCP_METRICS_CWND 293597d8d152SAndre Oppermann #ifdef TCP_METRICS_CWND 293697d8d152SAndre Oppermann if (metrics.rmx_cwnd) 293797d8d152SAndre Oppermann tp->snd_cwnd = max(mss, 293897d8d152SAndre Oppermann min(metrics.rmx_cwnd / 2, 293997d8d152SAndre Oppermann min(tp->snd_wnd, so->so_snd.sb_hiwat))); 294097d8d152SAndre Oppermann else 294197d8d152SAndre Oppermann #endif 294297d8d152SAndre Oppermann if (tcp_do_rfc3390) 294397d8d152SAndre Oppermann tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380)); 294497d8d152SAndre Oppermann #ifdef INET6 294597d8d152SAndre Oppermann else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) || 294697d8d152SAndre Oppermann (!isipv6 && in_localaddr(inp->inp_faddr))) 2947943ae302SAndre Oppermann #else 2948943ae302SAndre Oppermann else if (in_localaddr(inp->inp_faddr)) 294997d8d152SAndre Oppermann #endif 2950943ae302SAndre Oppermann tp->snd_cwnd = mss * ss_fltsz_local; 295197d8d152SAndre Oppermann else 295297d8d152SAndre Oppermann tp->snd_cwnd = mss * ss_fltsz; 2953233dcce1SAndre Oppermann 2954233dcce1SAndre Oppermann /* Check the interface for TSO capabilities. */ 2955233dcce1SAndre Oppermann if (mtuflags & CSUM_TSO) 2956233dcce1SAndre Oppermann tp->t_flags |= TF_TSO; 2957a0292f23SGarrett Wollman } 2958a0292f23SGarrett Wollman 2959a0292f23SGarrett Wollman /* 2960a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 2961a0292f23SGarrett Wollman */ 2962a0292f23SGarrett Wollman int 2963ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 2964a0292f23SGarrett Wollman { 296597d8d152SAndre Oppermann int mss = 0; 296697d8d152SAndre Oppermann u_long maxmtu = 0; 296797d8d152SAndre Oppermann u_long thcmtu = 0; 296897d8d152SAndre Oppermann size_t min_protoh; 2969fb59c426SYoshinobu Inoue #ifdef INET6 297097d8d152SAndre Oppermann int isipv6 = inc->inc_isipv6 ? 1 : 0; 2971fb59c426SYoshinobu Inoue #endif 2972a0292f23SGarrett Wollman 297397d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 2974a0292f23SGarrett Wollman 297531b3783cSHajimu UMEMOTO #ifdef INET6 297697d8d152SAndre Oppermann if (isipv6) { 297797d8d152SAndre Oppermann mss = tcp_v6mssdflt; 2978233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 297997d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 298097d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 298197d8d152SAndre Oppermann } else 298231b3783cSHajimu UMEMOTO #endif 298397d8d152SAndre Oppermann { 298497d8d152SAndre Oppermann mss = tcp_mssdflt; 2985233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 298697d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 298797d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 298897d8d152SAndre Oppermann } 298997d8d152SAndre Oppermann if (maxmtu && thcmtu) 299097d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 299197d8d152SAndre Oppermann else if (maxmtu || thcmtu) 299297d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 299397d8d152SAndre Oppermann 299497d8d152SAndre Oppermann return (mss); 2995df8bae1dSRodney W. Grimes } 299646f58482SJonathan Lemon 299746f58482SJonathan Lemon 299846f58482SJonathan Lemon /* 2999c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3000c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3001c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3002c068736aSJeffrey Hsu * be started again. 300346f58482SJonathan Lemon */ 3004c068736aSJeffrey Hsu static void 3005ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 300646f58482SJonathan Lemon { 300746f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 300846f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 300946f58482SJonathan Lemon 30108501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 30117a3244ccSRobert Watson 3012b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 301346f58482SJonathan Lemon tp->t_rtttime = 0; 301446f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 30156b2a5f92SJayanth Vijayaraghavan /* 3016c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3017c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 30186b2a5f92SJayanth Vijayaraghavan */ 30196b2a5f92SJayanth Vijayaraghavan tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 3020a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 30216b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 302246f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 302346f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 302446f58482SJonathan Lemon tp->snd_nxt = onxt; 302546f58482SJonathan Lemon /* 302646f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 302746f58482SJonathan Lemon * not updated yet. 302846f58482SJonathan Lemon */ 3029d7587117SPaul Saab if (tp->snd_cwnd > th->th_ack - tp->snd_una) 3030d7587117SPaul Saab tp->snd_cwnd -= th->th_ack - tp->snd_una; 3031d7587117SPaul Saab else 3032d7587117SPaul Saab tp->snd_cwnd = 0; 3033d7587117SPaul Saab tp->snd_cwnd += tp->t_maxseg; 303446f58482SJonathan Lemon } 3035