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 30c3aac50fSPeter Wemm * $FreeBSD$ 31df8bae1dSRodney W. Grimes */ 32df8bae1dSRodney W. Grimes 33f9e354dfSJulian Elischer #include "opt_ipfw.h" /* for ipfw_fwd */ 341cfd4b53SBruce M Simpson #include "opt_inet.h" 35fb59c426SYoshinobu Inoue #include "opt_inet6.h" 363a2a9f79SYoshinobu Inoue #include "opt_ipsec.h" 37c488362eSRobert Watson #include "opt_mac.h" 380cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 390cc12cc5SJoerg Wunsch 40df8bae1dSRodney W. Grimes #include <sys/param.h> 4198163b98SPoul-Henning Kamp #include <sys/kernel.h> 42df8bae1dSRodney W. Grimes #include <sys/malloc.h> 43df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 44a29f300eSGarrett Wollman #include <sys/proc.h> /* for proc0 declaration */ 45df8bae1dSRodney W. Grimes #include <sys/protosw.h> 46960ed29cSSeigo Tanimura #include <sys/signalvar.h> 47df8bae1dSRodney W. Grimes #include <sys/socket.h> 48df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 49960ed29cSSeigo Tanimura #include <sys/sysctl.h> 50816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 51960ed29cSSeigo Tanimura #include <sys/systm.h> 52df8bae1dSRodney W. Grimes 53e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 54e79adb8eSGarrett Wollman 5512e2e970SAndre Oppermann #include <vm/uma.h> 5612e2e970SAndre Oppermann 57df8bae1dSRodney W. Grimes #include <net/if.h> 58df8bae1dSRodney W. Grimes #include <net/route.h> 59df8bae1dSRodney W. Grimes 60df8bae1dSRodney W. Grimes #include <netinet/in.h> 61960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 62df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 63960ed29cSSeigo Tanimura #include <netinet/in_var.h> 64df8bae1dSRodney W. Grimes #include <netinet/ip.h> 658e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 66686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 67df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 68ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 69686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 70686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h> 71686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 72960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h> 73960ed29cSSeigo Tanimura #include <netinet6/nd6.h> 74df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 75df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 76df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 77df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 78df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 79fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 80df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 81610ee2f9SDavid Greenman #ifdef TCPDEBUG 82df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 83fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */ 84fb59c426SYoshinobu Inoue 85b9234fafSSam Leffler #ifdef FAST_IPSEC 86b9234fafSSam Leffler #include <netipsec/ipsec.h> 87b9234fafSSam Leffler #include <netipsec/ipsec6.h> 88b9234fafSSam Leffler #endif /*FAST_IPSEC*/ 89b9234fafSSam Leffler 90fb59c426SYoshinobu Inoue #ifdef IPSEC 91fb59c426SYoshinobu Inoue #include <netinet6/ipsec.h> 923a2a9f79SYoshinobu Inoue #include <netinet6/ipsec6.h> 93fb59c426SYoshinobu Inoue #include <netkey/key.h> 94fb59c426SYoshinobu Inoue #endif /*IPSEC*/ 95fb59c426SYoshinobu Inoue 96db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 97db4f9cc7SJonathan Lemon 98aed55708SRobert Watson #include <security/mac/mac_framework.h> 99aed55708SRobert Watson 100c068736aSJeffrey Hsu static const int tcprexmtthresh = 3; 1010312fbe9SPoul-Henning Kamp 1022f96f1f4SGarrett Wollman struct tcpstat tcpstat; 103c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW, 1043d177f46SBill Fumerola &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 1050312fbe9SPoul-Henning Kamp 106afdb4274SRobert Watson static int tcp_log_in_vain = 0; 107816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 108574b6964SAndre Oppermann &tcp_log_in_vain, 0, "Log all incoming TCP segments to closed ports"); 109816a3d83SPoul-Henning Kamp 11016f7f31fSGeoff Rehmet static int blackhole = 0; 11116f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 112574b6964SAndre Oppermann &blackhole, 0, "Do not send RST on segments to closed ports"); 11316f7f31fSGeoff Rehmet 114f498eeeeSDavid Greenman int tcp_delack_enabled = 1; 11584e33c9eSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 1163d177f46SBill Fumerola &tcp_delack_enabled, 0, 1173d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 118f498eeeeSDavid Greenman 119f8613305SDag-Erling Smørgrav static int drop_synfin = 0; 120f8613305SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, 121f8613305SDag-Erling Smørgrav &drop_synfin, 0, "Drop TCP packets with SYN+FIN set"); 122f8613305SDag-Erling Smørgrav 123dba7bc6aSAndre Oppermann static int tcp_do_rfc3042 = 1; 124582a954bSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW, 125582a954bSJeffrey Hsu &tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)"); 126582a954bSJeffrey Hsu 127dba7bc6aSAndre Oppermann static int tcp_do_rfc3390 = 1; 128da3a8a1aSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW, 129da3a8a1aSJeffrey Hsu &tcp_do_rfc3390, 0, 130da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 131da3a8a1aSJeffrey Hsu 132a69968eeSMike Silbersack static int tcp_insecure_rst = 0; 133a69968eeSMike Silbersack SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_RW, 134a69968eeSMike Silbersack &tcp_insecure_rst, 0, 1356489fe65SAndre Oppermann "Follow the old (insecure) criteria for accepting RST packets"); 136a69968eeSMike Silbersack 1376741ecf5SAndre Oppermann int tcp_do_autorcvbuf = 1; 1386741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_RW, 1396741ecf5SAndre Oppermann &tcp_do_autorcvbuf, 0, "Enable automatic receive buffer sizing"); 1406741ecf5SAndre Oppermann 1416741ecf5SAndre Oppermann int tcp_autorcvbuf_inc = 16*1024; 1426741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_RW, 1436489fe65SAndre Oppermann &tcp_autorcvbuf_inc, 0, 1446489fe65SAndre Oppermann "Incrementor step size of automatic receive buffer"); 1456741ecf5SAndre Oppermann 1466741ecf5SAndre Oppermann int tcp_autorcvbuf_max = 256*1024; 1476741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_RW, 1486741ecf5SAndre Oppermann &tcp_autorcvbuf_max, 0, "Max size of automatic receive buffer"); 1496741ecf5SAndre Oppermann 15015bd2b43SDavid Greenman struct inpcbhead tcb; 151fb59c426SYoshinobu Inoue #define tcb6 tcb /* for KAME src sync over BSD*'s */ 15215bd2b43SDavid Greenman struct inpcbinfo tcbinfo; 153df8bae1dSRodney W. Grimes 1545a53ca16SPaul Saab static void tcp_dooptions(struct tcpopt *, u_char *, int, int); 155679d9708SAndre Oppermann static void tcp_do_segment(struct mbuf *, struct tcphdr *, 156302ce8d6SAndre Oppermann struct socket *, struct tcpcb *, int, int); 157302ce8d6SAndre Oppermann static void tcp_dropwithreset(struct mbuf *, struct tcphdr *, 158302ce8d6SAndre Oppermann struct tcpcb *, int, int); 1594d77a549SAlfred Perlstein static void tcp_pulloutofband(struct socket *, 1604d77a549SAlfred Perlstein struct tcphdr *, struct mbuf *, int); 1614d77a549SAlfred Perlstein static void tcp_xmit_timer(struct tcpcb *, int); 162c068736aSJeffrey Hsu static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 1630312fbe9SPoul-Henning Kamp 164fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 165fb59c426SYoshinobu Inoue #ifdef INET6 166fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \ 167fb59c426SYoshinobu Inoue do { \ 168fb59c426SYoshinobu Inoue if ((tp) && (tp)->t_inpcb && \ 16997d8d152SAndre Oppermann ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \ 17097d8d152SAndre Oppermann nd6_nud_hint(NULL, NULL, 0); \ 171fb59c426SYoshinobu Inoue } while (0) 172fb59c426SYoshinobu Inoue #else 173fb59c426SYoshinobu Inoue #define ND6_HINT(tp) 174fb59c426SYoshinobu Inoue #endif 175df8bae1dSRodney W. Grimes 176df8bae1dSRodney W. Grimes /* 177262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 178262c1c1aSMatthew Dillon * - there is no delayed ack timer in progress and 179262c1c1aSMatthew Dillon * - our last ack wasn't a 0-sized window. We never want to delay 1803bfd6421SJonathan Lemon * the ack that opens up a 0-sized window and 1813bfd6421SJonathan Lemon * - delayed acks are enabled or 1823bfd6421SJonathan Lemon * - this is a half-synchronized T/TCP connection. 183d8c85a26SJonathan Lemon */ 184d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \ 185b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 186a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 1873bfd6421SJonathan Lemon (tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 188d8c85a26SJonathan Lemon 189df8bae1dSRodney W. Grimes 190df8bae1dSRodney W. Grimes /* 191df8bae1dSRodney W. Grimes * TCP input routine, follows pages 65-76 of the 192df8bae1dSRodney W. Grimes * protocol specification dated September, 1981 very closely. 193df8bae1dSRodney W. Grimes */ 194fb59c426SYoshinobu Inoue #ifdef INET6 195fb59c426SYoshinobu Inoue int 196ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto) 197fb59c426SYoshinobu Inoue { 198ad3f9ab3SAndre Oppermann struct mbuf *m = *mp; 19933841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 200fb59c426SYoshinobu Inoue 201fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 202fb59c426SYoshinobu Inoue 203fb59c426SYoshinobu Inoue /* 204fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 205fb59c426SYoshinobu Inoue * better place to put this in? 206fb59c426SYoshinobu Inoue */ 20733841545SHajimu UMEMOTO ia6 = ip6_getdstifaddr(m); 20833841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 209fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 210fb59c426SYoshinobu Inoue 211fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 212fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 213fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 214fb59c426SYoshinobu Inoue return IPPROTO_DONE; 215fb59c426SYoshinobu Inoue } 216fb59c426SYoshinobu Inoue 217f0ffb944SJulian Elischer tcp_input(m, *offp); 218fb59c426SYoshinobu Inoue return IPPROTO_DONE; 219fb59c426SYoshinobu Inoue } 220fb59c426SYoshinobu Inoue #endif 221fb59c426SYoshinobu Inoue 222df8bae1dSRodney W. Grimes void 223ad3f9ab3SAndre Oppermann tcp_input(struct mbuf *m, int off0) 224df8bae1dSRodney W. Grimes { 225ad3f9ab3SAndre Oppermann struct tcphdr *th; 226ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 227ad3f9ab3SAndre Oppermann struct ipovly *ipov; 228ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 229302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 230302ce8d6SAndre Oppermann struct socket *so = NULL; 231e79adb8eSGarrett Wollman u_char *optp = NULL; 23226f9a767SRodney W. Grimes int optlen = 0; 233a0194ef1SBruce M Simpson int len, tlen, off; 234fb59c426SYoshinobu Inoue int drop_hdrlen; 235ad3f9ab3SAndre Oppermann int thflags; 236302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 2379b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 2389b932e9eSAndre Oppermann struct m_tag *fwd_tag; 2399b932e9eSAndre Oppermann #endif 240c068736aSJeffrey Hsu #ifdef INET6 241302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 242c068736aSJeffrey Hsu int isipv6; 243c068736aSJeffrey Hsu #else 244a160e630SAndre Oppermann const void *ip6 = NULL; 245c068736aSJeffrey Hsu const int isipv6 = 0; 246c068736aSJeffrey Hsu #endif 247302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 248a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 249f76fcf6dSJeffrey Hsu 250610ee2f9SDavid Greenman #ifdef TCPDEBUG 251c068736aSJeffrey Hsu /* 252c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 253c068736aSJeffrey Hsu * now IPv6. 254c068736aSJeffrey Hsu */ 25514739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 256410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 257610ee2f9SDavid Greenman short ostate = 0; 258610ee2f9SDavid Greenman #endif 259c068736aSJeffrey Hsu 260fb59c426SYoshinobu Inoue #ifdef INET6 261fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 262fb59c426SYoshinobu Inoue #endif 263a0292f23SGarrett Wollman 264302ce8d6SAndre Oppermann to.to_flags = 0; 265df8bae1dSRodney W. Grimes tcpstat.tcps_rcvtotal++; 266fb59c426SYoshinobu Inoue 267fb59c426SYoshinobu Inoue if (isipv6) { 26804d3a452SHajimu UMEMOTO #ifdef INET6 269fb59c426SYoshinobu Inoue /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */ 270fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 271fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 272fb59c426SYoshinobu Inoue if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 273fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbadsum++; 274fb59c426SYoshinobu Inoue goto drop; 275fb59c426SYoshinobu Inoue } 276fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 27733841545SHajimu UMEMOTO 27833841545SHajimu UMEMOTO /* 27933841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 28033841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 28133841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 28233841545SHajimu UMEMOTO * 28333841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 28433841545SHajimu UMEMOTO * already dropped in ip6_input. 28533841545SHajimu UMEMOTO */ 28633841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 28733841545SHajimu UMEMOTO /* XXX stat */ 28833841545SHajimu UMEMOTO goto drop; 28933841545SHajimu UMEMOTO } 29004d3a452SHajimu UMEMOTO #else 29104d3a452SHajimu UMEMOTO th = NULL; /* XXX: avoid compiler warning */ 29204d3a452SHajimu UMEMOTO #endif 293c068736aSJeffrey Hsu } else { 294df8bae1dSRodney W. Grimes /* 295df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 296df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 297df8bae1dSRodney W. Grimes */ 298fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 299df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 300fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 301fb59c426SYoshinobu Inoue } 302df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 3034dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 3044dfdffe9SAndre Oppermann == NULL) { 305df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 306df8bae1dSRodney W. Grimes return; 307df8bae1dSRodney W. Grimes } 308df8bae1dSRodney W. Grimes } 309fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 310fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 311db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 312db4f9cc7SJonathan Lemon tlen = ip->ip_len; 313df8bae1dSRodney W. Grimes 314db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 315db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 316db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 317db4f9cc7SJonathan Lemon else 318db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 319c068736aSJeffrey Hsu ip->ip_dst.s_addr, 320c068736aSJeffrey Hsu htonl(m->m_pkthdr.csum_data + 321c068736aSJeffrey Hsu ip->ip_len + 322c068736aSJeffrey Hsu IPPROTO_TCP)); 323db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 3243c653157SHartmut Brandt #ifdef TCPDEBUG 3253c653157SHartmut Brandt ipov->ih_len = (u_short)tlen; 3263c653157SHartmut Brandt ipov->ih_len = htons(ipov->ih_len); 3273c653157SHartmut Brandt #endif 328db4f9cc7SJonathan Lemon } else { 329df8bae1dSRodney W. Grimes /* 330df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 331df8bae1dSRodney W. Grimes */ 332df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 333fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 334fb59c426SYoshinobu Inoue ipov->ih_len = (u_short)tlen; 335fd8e4ebcSMike Barcroft ipov->ih_len = htons(ipov->ih_len); 336fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 337db4f9cc7SJonathan Lemon } 338fb59c426SYoshinobu Inoue if (th->th_sum) { 339df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadsum++; 340df8bae1dSRodney W. Grimes goto drop; 341df8bae1dSRodney W. Grimes } 342fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 343fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 344fb59c426SYoshinobu Inoue } 345df8bae1dSRodney W. Grimes 346df8bae1dSRodney W. Grimes /* 347df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 348df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 349df8bae1dSRodney W. Grimes */ 350fb59c426SYoshinobu Inoue off = th->th_off << 2; 351df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 352df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadoff++; 353df8bae1dSRodney W. Grimes goto drop; 354df8bae1dSRodney W. Grimes } 355fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 356df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 357fb59c426SYoshinobu Inoue if (isipv6) { 35804d3a452SHajimu UMEMOTO #ifdef INET6 359fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 360fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 361fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 36204d3a452SHajimu UMEMOTO #endif 363c068736aSJeffrey Hsu } else { 364df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 365c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 3664dfdffe9SAndre Oppermann == NULL) { 367df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 368df8bae1dSRodney W. Grimes return; 369df8bae1dSRodney W. Grimes } 370fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 371fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 372fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 373fb59c426SYoshinobu Inoue } 374df8bae1dSRodney W. Grimes } 375df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 376fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 377df8bae1dSRodney W. Grimes } 378fb59c426SYoshinobu Inoue thflags = th->th_flags; 379df8bae1dSRodney W. Grimes 380e46cd3d4SDag-Erling Smørgrav /* 381df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 382df8bae1dSRodney W. Grimes */ 383fd8e4ebcSMike Barcroft th->th_seq = ntohl(th->th_seq); 384fd8e4ebcSMike Barcroft th->th_ack = ntohl(th->th_ack); 385fd8e4ebcSMike Barcroft th->th_win = ntohs(th->th_win); 386fd8e4ebcSMike Barcroft th->th_urp = ntohs(th->th_urp); 387df8bae1dSRodney W. Grimes 388df8bae1dSRodney W. Grimes /* 389794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 390755c1f07SAndras Olah */ 391fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 392755c1f07SAndras Olah 393755c1f07SAndras Olah /* 394df8bae1dSRodney W. Grimes * Locate pcb for segment. 395df8bae1dSRodney W. Grimes */ 396f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 397df8bae1dSRodney W. Grimes findpcb: 398302ce8d6SAndre Oppermann INP_INFO_WLOCK_ASSERT(&tcbinfo); 3999b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 4009b932e9eSAndre Oppermann /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */ 4019b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 4029b932e9eSAndre Oppermann 4039b932e9eSAndre Oppermann if (fwd_tag != NULL && isipv6 == 0) { /* IPv6 support is not yet */ 4049b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 4059b932e9eSAndre Oppermann 4069b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 407f9e354dfSJulian Elischer /* 4082b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 409f9e354dfSJulian Elischer * already got one like this? 410f9e354dfSJulian Elischer */ 4119b932e9eSAndre Oppermann inp = in_pcblookup_hash(&tcbinfo, 4129b932e9eSAndre Oppermann ip->ip_src, th->th_sport, 413c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 414c068736aSJeffrey Hsu 0, m->m_pkthdr.rcvif); 415f9e354dfSJulian Elischer if (!inp) { 4169b932e9eSAndre Oppermann /* It's new. Try to find the ambushing socket. */ 417f9e354dfSJulian Elischer inp = in_pcblookup_hash(&tcbinfo, 418fb59c426SYoshinobu Inoue ip->ip_src, th->th_sport, 4192b25acc1SLuigi Rizzo next_hop->sin_addr, 420c068736aSJeffrey Hsu next_hop->sin_port ? 421c068736aSJeffrey Hsu ntohs(next_hop->sin_port) : 422c068736aSJeffrey Hsu th->th_dport, 423421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 424421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 425f9e354dfSJulian Elischer } 4269b932e9eSAndre Oppermann /* Remove the tag from the packet. We don't need it anymore. */ 4279b932e9eSAndre Oppermann m_tag_delete(m, fwd_tag); 428b10fbdeaSAndre Oppermann } else 4299b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 430b10fbdeaSAndre Oppermann { 43104d3a452SHajimu UMEMOTO if (isipv6) { 43204d3a452SHajimu UMEMOTO #ifdef INET6 433c068736aSJeffrey Hsu inp = in6_pcblookup_hash(&tcbinfo, 434c068736aSJeffrey Hsu &ip6->ip6_src, th->th_sport, 435c068736aSJeffrey Hsu &ip6->ip6_dst, th->th_dport, 436421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 437421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 43804d3a452SHajimu UMEMOTO #endif 43904d3a452SHajimu UMEMOTO } else 440c068736aSJeffrey Hsu inp = in_pcblookup_hash(&tcbinfo, 441c068736aSJeffrey Hsu ip->ip_src, th->th_sport, 442c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 443421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 444421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 445fb59c426SYoshinobu Inoue } 446f9e354dfSJulian Elischer 447da0f4099SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC) 448d420fcdaSBruce M Simpson #ifdef INET6 4494dfdffe9SAndre Oppermann if (isipv6 && inp != NULL && ipsec6_in_reject(m, inp)) { 450fb59c426SYoshinobu Inoue #ifdef IPSEC 451fb59c426SYoshinobu Inoue ipsec6stat.in_polvio++; 452d420fcdaSBruce M Simpson #endif 453302ce8d6SAndre Oppermann goto dropunlock; 454d420fcdaSBruce M Simpson } else 455d420fcdaSBruce M Simpson #endif /* INET6 */ 456d420fcdaSBruce M Simpson if (inp != NULL && ipsec4_in_reject(m, inp)) { 457da0f4099SHajimu UMEMOTO #ifdef IPSEC 458fb59c426SYoshinobu Inoue ipsecstat.in_polvio++; 459d420fcdaSBruce M Simpson #endif 460302ce8d6SAndre Oppermann goto dropunlock; 461fb59c426SYoshinobu Inoue } 462da0f4099SHajimu UMEMOTO #endif /*IPSEC || FAST_IPSEC*/ 463df8bae1dSRodney W. Grimes 464df8bae1dSRodney W. Grimes /* 465574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 466574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 467df8bae1dSRodney W. Grimes */ 468816a3d83SPoul-Henning Kamp if (inp == NULL) { 469574b6964SAndre Oppermann /* 470574b6964SAndre Oppermann * Log communication attempts to ports that are not 471574b6964SAndre Oppermann * in use. 472574b6964SAndre Oppermann */ 473574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 474574b6964SAndre Oppermann tcp_log_in_vain == 2) { 475a160e630SAndre Oppermann if ((s = tcp_log_addrs(NULL, th, (void *)ip, 476a160e630SAndre Oppermann (void *)ip6))) 477df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 478df541e5fSAndre Oppermann "to closed port\n", s, __func__); 4792e4e1b4cSGeoff Rehmet } 480574b6964SAndre Oppermann /* 481574b6964SAndre Oppermann * When blackholing do not respond with a RST but 482574b6964SAndre Oppermann * completely ignore the segment and drop it. 483574b6964SAndre Oppermann */ 484574b6964SAndre Oppermann if ((blackhole == 1 && (thflags & TH_SYN)) || 485574b6964SAndre Oppermann blackhole == 2) 4861929eae1SAndre Oppermann goto dropunlock; 487574b6964SAndre Oppermann 488a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 489a57815efSBosko Milekic goto dropwithreset; 490816a3d83SPoul-Henning Kamp } 491f76fcf6dSJeffrey Hsu INP_LOCK(inp); 492936cd18dSAndre Oppermann 493936cd18dSAndre Oppermann /* Check the minimum TTL for socket. */ 49434f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 49534f83c52SGeorge V. Neville-Neil #ifdef INET6 49634f83c52SGeorge V. Neville-Neil if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim) 497302ce8d6SAndre Oppermann goto dropunlock; 49802707462SAndre Oppermann else 49934f83c52SGeorge V. Neville-Neil #endif 50034f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 501302ce8d6SAndre Oppermann goto dropunlock; 50234f83c52SGeorge V. Neville-Neil } 503936cd18dSAndre Oppermann 504340c35deSJonathan Lemon /* 505794235b7SAndre Oppermann * A previous connection in TIMEWAIT state is supposed to catch 506794235b7SAndre Oppermann * stray or duplicate segments arriving late. If this segment 507794235b7SAndre Oppermann * was a legitimate new connection attempt the old INPCB gets 508794235b7SAndre Oppermann * removed and we can try again to find a listening socket. 509340c35deSJonathan Lemon */ 510794235b7SAndre Oppermann if (inp->inp_vflag & INP_TIMEWAIT) { 511340c35deSJonathan Lemon if (thflags & TH_SYN) 512f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 5132104448fSAndre Oppermann /* NB: tcp_twcheck unlocks the INP and frees the mbuf. */ 5142104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 515340c35deSJonathan Lemon goto findpcb; 516340c35deSJonathan Lemon INP_INFO_WUNLOCK(&tcbinfo); 517340c35deSJonathan Lemon return; 518340c35deSJonathan Lemon } 519794235b7SAndre Oppermann /* 520794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 521794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 522794235b7SAndre Oppermann * segment and send an appropriate response. 523794235b7SAndre Oppermann */ 524df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 525a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 526a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 527a57815efSBosko Milekic goto dropwithreset; 52809f81a46SBosko Milekic } 529df8bae1dSRodney W. Grimes 530c488362eSRobert Watson #ifdef MAC 5311f82efb3SRobert Watson INP_LOCK_ASSERT(inp); 532a557af22SRobert Watson if (mac_check_inpcb_deliver(inp, m)) 533302ce8d6SAndre Oppermann goto dropunlock; 534c488362eSRobert Watson #endif 535a557af22SRobert Watson so = inp->inp_socket; 536302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 537610ee2f9SDavid Greenman #ifdef TCPDEBUG 538df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 539df8bae1dSRodney W. Grimes ostate = tp->t_state; 540d30d90dcSMaxim Konovalov if (isipv6) { 54110fe523eSMaxim Konovalov #ifdef INET6 542540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 543d30d90dcSMaxim Konovalov #endif 544d30d90dcSMaxim Konovalov } else 545540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 546fb59c426SYoshinobu Inoue tcp_savetcp = *th; 547df8bae1dSRodney W. Grimes } 548610ee2f9SDavid Greenman #endif 549db33b3e6SAndre Oppermann /* 550db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 551db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 552db33b3e6SAndre Oppermann * attempt or the completion of a previous one. 553db33b3e6SAndre Oppermann */ 554540e8b7eSJeffrey Hsu if (so->so_options & SO_ACCEPTCONN) { 555540e8b7eSJeffrey Hsu struct in_conninfo inc; 556540e8b7eSJeffrey Hsu 5577824d002SAndre Oppermann KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but " 5587824d002SAndre Oppermann "tp not listening", __func__)); 5597824d002SAndre Oppermann 5608bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 561be2ac88cSJonathan Lemon inc.inc_isipv6 = isipv6; 562302ce8d6SAndre Oppermann #ifdef INET6 563be2ac88cSJonathan Lemon if (isipv6) { 564be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 565be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 566302ce8d6SAndre Oppermann } else 567302ce8d6SAndre Oppermann #endif 568302ce8d6SAndre Oppermann { 569be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 570be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 571be2ac88cSJonathan Lemon } 572be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 573be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 574be2ac88cSJonathan Lemon 575fb59c426SYoshinobu Inoue /* 576be2ac88cSJonathan Lemon * If the state is LISTEN then ignore segment if it contains 577be2ac88cSJonathan Lemon * a RST. If the segment contains an ACK then it is bad and 578be2ac88cSJonathan Lemon * send a RST. If it does not contain a SYN then it is not 579be2ac88cSJonathan Lemon * interesting; drop it. 580be2ac88cSJonathan Lemon * 581be2ac88cSJonathan Lemon * If the state is SYN_RECEIVED (syncache) and seg contains 582be2ac88cSJonathan Lemon * an ACK, but not for our SYN/ACK, send a RST. If the seg 583be2ac88cSJonathan Lemon * contains a RST, check the sequence number to see if it 584be2ac88cSJonathan Lemon * is a valid reset segment. 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. 6014195b4afSPaul Traina */ 602be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 603be2ac88cSJonathan Lemon goto dropwithreset; 604be2ac88cSJonathan Lemon } 605f76fcf6dSJeffrey Hsu if (so == NULL) { 606be2ac88cSJonathan Lemon /* 607e207f800SAndre Oppermann * We completed the 3-way handshake 608e207f800SAndre Oppermann * but could not allocate a socket 609e207f800SAndre Oppermann * either due to memory shortage, 610e207f800SAndre Oppermann * listen queue length limits or 611a160e630SAndre Oppermann * global socket limits. Send RST 612a160e630SAndre Oppermann * or wait and have the remote end 613a160e630SAndre Oppermann * retransmit the ACK for another 614a160e630SAndre Oppermann * try. 615be2ac88cSJonathan Lemon */ 616a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 617a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 618a160e630SAndre Oppermann "Socket allocation failure, %s\n", 619a160e630SAndre Oppermann s, __func__, (tcp_sc_rst_sock_fail ? 620a160e630SAndre Oppermann "sending RST" : "try again")); 621a160e630SAndre Oppermann if (tcp_sc_rst_sock_fail) { 622e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 623e207f800SAndre Oppermann goto dropwithreset; 624a160e630SAndre Oppermann } else 625a160e630SAndre Oppermann goto dropunlock; 626f76fcf6dSJeffrey Hsu } 627be2ac88cSJonathan Lemon /* 628be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 629be2ac88cSJonathan Lemon * Continue processing segment. 630be2ac88cSJonathan Lemon */ 6310c38fd0aSAndre Oppermann INP_UNLOCK(inp); /* listen socket */ 632be2ac88cSJonathan Lemon inp = sotoinpcb(so); 6330c38fd0aSAndre Oppermann INP_LOCK(inp); /* new connection */ 634be2ac88cSJonathan Lemon tp = intotcpcb(inp); 635be2ac88cSJonathan Lemon /* 636302ce8d6SAndre Oppermann * Process the segment and the data it 637302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 638302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 639302ce8d6SAndre Oppermann */ 640679d9708SAndre Oppermann tcp_do_segment(m, th, so, tp, drop_hdrlen, 641679d9708SAndre Oppermann tlen); 642679d9708SAndre Oppermann INP_INFO_UNLOCK_ASSERT(&tcbinfo); 643302ce8d6SAndre Oppermann return; 644be2ac88cSJonathan Lemon } 645a160e630SAndre Oppermann /* 646a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 647a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 648a160e630SAndre Oppermann * retransmits. 649a160e630SAndre Oppermann */ 650a160e630SAndre Oppermann if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) { 651a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 652a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 653a160e630SAndre Oppermann "SYN|ACK was rejected\n", s, __func__); 654be2ac88cSJonathan Lemon syncache_chkrst(&inc, th); 655302ce8d6SAndre Oppermann goto dropunlock; 656be2ac88cSJonathan Lemon } 657a160e630SAndre Oppermann /* 658a160e630SAndre Oppermann * Spurious RST. Ignore. 659a160e630SAndre Oppermann */ 660a160e630SAndre Oppermann if (thflags & TH_RST) { 661a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 662a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 663a160e630SAndre Oppermann "Spurious RST\n", s, __func__); 664a160e630SAndre Oppermann goto dropunlock; 665a160e630SAndre Oppermann } 666a160e630SAndre Oppermann /* 667a160e630SAndre Oppermann * We can't do anything without SYN. 668a160e630SAndre Oppermann */ 669a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 670a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 671a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 672a160e630SAndre Oppermann "SYN missing\n", s, __func__); 673a160e630SAndre Oppermann tcpstat.tcps_badsyn++; 674a160e630SAndre Oppermann goto dropunlock; 675a160e630SAndre Oppermann } 676a160e630SAndre Oppermann /* 677a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 678a160e630SAndre Oppermann */ 679fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 680a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 681a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 682a160e630SAndre Oppermann "SYN|ACK bogus\n", 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. 696a160e630SAndre Oppermann * 697a160e630SAndre Oppermann * This is a violation of the TCP specification 698a160e630SAndre Oppermann * and was used by RFC1644. 699a160e630SAndre Oppermann */ 700a160e630SAndre Oppermann if ((thflags & TH_FIN) && drop_synfin) { 701a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 702a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 703a160e630SAndre Oppermann "SYN|FIN ignored\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: " 756a160e630SAndre Oppermann "Deprecated IPv6 address\n", 757a160e630SAndre Oppermann s, __func__); 75833841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 75933841545SHajimu UMEMOTO goto dropwithreset; 76033841545SHajimu UMEMOTO } 76133841545SHajimu UMEMOTO } 76233841545SHajimu UMEMOTO #endif 76365f28919SJesper Skriver /* 764db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 765db33b3e6SAndre Oppermann * 766db33b3e6SAndre Oppermann * Don't bother responding if the destination was a 767db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 768db33b3e6SAndre Oppermann * 769be2ac88cSJonathan Lemon * If it is from this socket, drop it, it must be forged. 7702ca2159fSCrist J. Clark * 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 */ 775be2ac88cSJonathan Lemon if (m->m_flags & (M_BCAST|M_MCAST)) 776302ce8d6SAndre Oppermann goto dropunlock; 777be2ac88cSJonathan Lemon if (isipv6) { 778db33b3e6SAndre Oppermann #ifdef INET6 779db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 780a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 781a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 782a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 783a160e630SAndre Oppermann "Connection to self\n", s, __func__); 784302ce8d6SAndre Oppermann goto dropunlock; 785a160e630SAndre Oppermann } 786be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 787a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 788a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 789a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 790a160e630SAndre Oppermann "Connection to multicast address\n", 791a160e630SAndre Oppermann s, __func__); 792302ce8d6SAndre Oppermann goto dropunlock; 793a160e630SAndre Oppermann } 794db33b3e6SAndre Oppermann #endif 795c068736aSJeffrey Hsu } else { 796db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 797a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 798a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 799a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 800a160e630SAndre Oppermann "Connection to self\n", s, __func__); 801302ce8d6SAndre Oppermann goto dropunlock; 802a160e630SAndre Oppermann } 803be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 804be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 8052ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 806a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 807a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 808a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 809a160e630SAndre Oppermann "Connection to multicast address\n", 810a160e630SAndre Oppermann s, __func__); 811302ce8d6SAndre Oppermann goto dropunlock; 812c068736aSJeffrey Hsu } 813a160e630SAndre Oppermann } 814be2ac88cSJonathan Lemon /* 815db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 816db33b3e6SAndre Oppermann * for syncache. 817be2ac88cSJonathan Lemon */ 8183c653157SHartmut Brandt #ifdef TCPDEBUG 8193c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 8203c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 8213c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 8223c653157SHartmut Brandt #endif 823f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 8244d6e7130SAndre Oppermann syncache_add(&inc, &to, th, inp, &so, m); 825be2ac88cSJonathan Lemon /* 8264d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 827a160e630SAndre Oppermann * Everything already unlocked by syncache_add(). 828be2ac88cSJonathan Lemon */ 829a160e630SAndre Oppermann INP_INFO_UNLOCK_ASSERT(&tcbinfo); 830be2ac88cSJonathan Lemon return; 831f76fcf6dSJeffrey Hsu } 832db33b3e6SAndre Oppermann 833302ce8d6SAndre Oppermann /* 8341cd6eadfSRobert Watson * Segment belongs to a connection in SYN_SENT, ESTABLISHED or late 8351cd6eadfSRobert Watson * state. tcp_do_segment() always consumes the mbuf chain, unlocks the 8361cd6eadfSRobert Watson * inpcb, and unlocks the pcbinfo. 837302ce8d6SAndre Oppermann */ 838679d9708SAndre Oppermann tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen); 839679d9708SAndre Oppermann INP_INFO_UNLOCK_ASSERT(&tcbinfo); 840302ce8d6SAndre Oppermann return; 841be2ac88cSJonathan Lemon 842302ce8d6SAndre Oppermann dropwithreset: 843995a7717SAndre Oppermann INP_INFO_WLOCK_ASSERT(&tcbinfo); 844302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 845302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 846302ce8d6SAndre Oppermann dropunlock: 847995a7717SAndre Oppermann INP_INFO_WLOCK_ASSERT(&tcbinfo); 8489fa198beSAndre Oppermann if (inp != NULL) 849302ce8d6SAndre Oppermann INP_UNLOCK(inp); 850302ce8d6SAndre Oppermann INP_INFO_WUNLOCK(&tcbinfo); 851302ce8d6SAndre Oppermann drop: 852995a7717SAndre Oppermann INP_INFO_UNLOCK_ASSERT(&tcbinfo); 853a160e630SAndre Oppermann if (s != NULL) 854a160e630SAndre Oppermann free(s, M_TCPLOG); 855302ce8d6SAndre Oppermann if (m != NULL) 856302ce8d6SAndre Oppermann m_freem(m); 857302ce8d6SAndre Oppermann return; 858302ce8d6SAndre Oppermann } 859302ce8d6SAndre Oppermann 860679d9708SAndre Oppermann static void 861302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 862302ce8d6SAndre Oppermann struct tcpcb *tp, int drop_hdrlen, int tlen) 863302ce8d6SAndre Oppermann { 864302ce8d6SAndre Oppermann int thflags, acked, ourfinisacked, needoutput = 0; 865302ce8d6SAndre Oppermann int headlocked = 1; 866302ce8d6SAndre Oppermann int rstreason, todrop, win; 867302ce8d6SAndre Oppermann u_long tiwin; 868302ce8d6SAndre Oppermann struct tcpopt to; 869302ce8d6SAndre Oppermann 87014739780SMaxim Konovalov #ifdef TCPDEBUG 87114739780SMaxim Konovalov /* 87214739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 87314739780SMaxim Konovalov * now IPv6. 87414739780SMaxim Konovalov */ 87514739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 87614739780SMaxim Konovalov struct tcphdr tcp_savetcp; 87714739780SMaxim Konovalov short ostate = 0; 87814739780SMaxim Konovalov #endif 879302ce8d6SAndre Oppermann thflags = th->th_flags; 880302ce8d6SAndre Oppermann 881302ce8d6SAndre Oppermann INP_INFO_WLOCK_ASSERT(&tcbinfo); 882302ce8d6SAndre Oppermann INP_LOCK_ASSERT(tp->t_inpcb); 883679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 884679d9708SAndre Oppermann __func__)); 885679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 886679d9708SAndre Oppermann __func__)); 887df8bae1dSRodney W. Grimes 888df8bae1dSRodney W. Grimes /* 889df8bae1dSRodney W. Grimes * Segment received on connection. 890df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 891df8bae1dSRodney W. Grimes */ 8929b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 8937ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 894b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 895df8bae1dSRodney W. Grimes 896df8bae1dSRodney W. Grimes /* 897464fcfbcSAndre Oppermann * Unscale the window into a 32-bit value. 898464fcfbcSAndre Oppermann * This value is bogus for the TCPS_SYN_SENT state 899464fcfbcSAndre Oppermann * and is overwritten later. 900464fcfbcSAndre Oppermann */ 901464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 902464fcfbcSAndre Oppermann 903464fcfbcSAndre Oppermann /* 904f72167f4SAndre Oppermann * Parse options on any incoming segment. 905f72167f4SAndre Oppermann */ 906302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 907302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 908302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 909f72167f4SAndre Oppermann 910f72167f4SAndre Oppermann /* 911f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 912bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 913bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 914bf6d304aSAndre Oppermann * was established. 915f72167f4SAndre Oppermann */ 916bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 917e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 918bf6d304aSAndre Oppermann if (TSTMP_GT(to.to_tsecr, ticks)) 919f72167f4SAndre Oppermann to.to_tsecr = 0; 920bf6d304aSAndre Oppermann } 921f72167f4SAndre Oppermann 922f72167f4SAndre Oppermann /* 92397d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 92497d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 92597d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 926df8bae1dSRodney W. Grimes */ 9270a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 928464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 929464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 930be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 93102a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 932464fcfbcSAndre Oppermann tp->snd_wnd = th->th_win << tp->snd_scale; 933464fcfbcSAndre Oppermann tiwin = tp->snd_wnd; 934be2ac88cSJonathan Lemon } 935be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 936be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 937be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 938be2ac88cSJonathan Lemon tp->ts_recent_age = ticks; 939be2ac88cSJonathan Lemon } 9404a32dc29SMohan Srinivasan /* Initial send window, already scaled. */ 9414a32dc29SMohan Srinivasan tp->snd_wnd = th->th_win; 942be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 943be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 9443529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 9453529149eSAndre Oppermann (to.to_flags & TOF_SACKPERM) == 0) 9463529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 9476d90faf3SPaul Saab } 9486d90faf3SPaul Saab 949df8bae1dSRodney W. Grimes /* 950df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 951df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 952df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 953df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 954df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 955df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 956df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 957df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 958df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 959df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 960df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 961df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 962a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 963c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 964a0292f23SGarrett Wollman * be TH_NEEDSYN. 965df8bae1dSRodney W. Grimes */ 966df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 967c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 968fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 969c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 970c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 971a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 972c5ad39b9SAndre Oppermann LIST_EMPTY(&tp->t_segq) && 973be2ac88cSJonathan Lemon ((to.to_flags & TOF_TS) == 0 || 974c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 975df8bae1dSRodney W. Grimes 976df8bae1dSRodney W. Grimes /* 977df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 978df8bae1dSRodney W. Grimes * record the timestamp. 979a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 980a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 981df8bae1dSRodney W. Grimes */ 982be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 983fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 9849b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 985a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 986df8bae1dSRodney W. Grimes } 987df8bae1dSRodney W. Grimes 988fb59c426SYoshinobu Inoue if (tlen == 0) { 989fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 990fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 991233e8c18SGarrett Wollman tp->snd_cwnd >= tp->snd_wnd && 9923529149eSAndre Oppermann ((!tcp_do_newreno && 9933529149eSAndre Oppermann !(tp->t_flags & TF_SACK_PERMIT) && 994cb942153SJeffrey Hsu tp->t_dupacks < tcprexmtthresh) || 9953529149eSAndre Oppermann ((tcp_do_newreno || 9963529149eSAndre Oppermann (tp->t_flags & TF_SACK_PERMIT)) && 997fc30a251SAndre Oppermann !IN_FASTRECOVERY(tp) && 998fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 999482ac968SPaul Saab TAILQ_EMPTY(&tp->snd_holes)))) { 1000302ce8d6SAndre Oppermann KASSERT(headlocked, 1001302ce8d6SAndre Oppermann ("%s: headlocked", __func__)); 1002e0bef1cbSRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 1003e0bef1cbSRobert Watson headlocked = 0; 1004df8bae1dSRodney W. Grimes /* 1005df8bae1dSRodney W. Grimes * this is a pure ack for outstanding data. 1006df8bae1dSRodney W. Grimes */ 1007df8bae1dSRodney W. Grimes ++tcpstat.tcps_predack; 10089b8b58e0SJonathan Lemon /* 10099b8b58e0SJonathan Lemon * "bad retransmit" recovery 10109b8b58e0SJonathan Lemon */ 10119b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 10129b8b58e0SJonathan Lemon ticks < tp->t_badrxtwin) { 1013cb942153SJeffrey Hsu ++tcpstat.tcps_sndrexmitbad; 10149b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 10159b8b58e0SJonathan Lemon tp->snd_ssthresh = 10169b8b58e0SJonathan Lemon tp->snd_ssthresh_prev; 10179d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 10189d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 10199d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 10209b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 10219b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; 10229b8b58e0SJonathan Lemon } 1023fa55172bSMatthew Dillon 1024fa55172bSMatthew Dillon /* 1025fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1026fa55172bSMatthew Dillon * 1027fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1028fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1029fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1030fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1031fa55172bSMatthew Dillon */ 1032fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1033fa55172bSMatthew Dillon to.to_tsecr) { 1034eaf80179SAndre Oppermann if (!tp->t_rttlow || 1035eaf80179SAndre Oppermann tp->t_rttlow > ticks - to.to_tsecr) 1036eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 1037a0292f23SGarrett Wollman tcp_xmit_timer(tp, 10389b8b58e0SJonathan Lemon ticks - to.to_tsecr + 1); 1039fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1040fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1041eaf80179SAndre Oppermann if (!tp->t_rttlow || 1042eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1043eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1044c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1045c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1046fa55172bSMatthew Dillon } 10471fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 1048fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 1049df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 1050df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 1051df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 10529d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 10539d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 10549d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 10559d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 10561645d090SJeff Roberson /* 10571645d090SJeff Roberson * pull snd_wl2 up to prevent seq wrap relative 10581645d090SJeff Roberson * to th_ack. 10591645d090SJeff Roberson */ 1060cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1061b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1062df8bae1dSRodney W. Grimes m_freem(m); 1063fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 1064df8bae1dSRodney W. Grimes 1065df8bae1dSRodney W. Grimes /* 1066df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1067df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1068df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1069df8bae1dSRodney W. Grimes * If process is waiting for space, 1070df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1071df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1072df8bae1dSRodney W. Grimes * decide between more output or persist. 10733c653157SHartmut Brandt 10743c653157SHartmut Brandt #ifdef TCPDEBUG 10753c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 10763c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 10773c653157SHartmut Brandt (void *)tcp_saveipgen, 10783c653157SHartmut Brandt &tcp_savetcp, 0); 10793c653157SHartmut Brandt #endif 1080df8bae1dSRodney W. Grimes */ 1081df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1082b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1083b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1084b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1085b8152ba7SAndre Oppermann tp->t_rxtcur); 1086df8bae1dSRodney W. Grimes 1087df8bae1dSRodney W. Grimes sowwakeup(so); 1088df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 1089df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1090a14c749fSJonathan Lemon goto check_delack; 1091df8bae1dSRodney W. Grimes } 1092fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1093fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 10946741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 10956741ecf5SAndre Oppermann 1096302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: headlocked", __func__)); 1097e0bef1cbSRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 1098e0bef1cbSRobert Watson headlocked = 0; 1099df8bae1dSRodney W. Grimes /* 1100df8bae1dSRodney W. Grimes * this is a pure, in-sequence data packet 1101df8bae1dSRodney W. Grimes * with nothing on the reassembly queue and 1102df8bae1dSRodney W. Grimes * we have enough buffer space to take it. 1103df8bae1dSRodney W. Grimes */ 11046d90faf3SPaul Saab /* Clean receiver SACK report if present */ 11053529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 11066d90faf3SPaul Saab tcp_clean_sackreport(tp); 1107df8bae1dSRodney W. Grimes ++tcpstat.tcps_preddat; 1108fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 11091645d090SJeff Roberson /* 11101645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 11111645d090SJeff Roberson * th_seq. 11121645d090SJeff Roberson */ 11131645d090SJeff Roberson tp->snd_wl1 = th->th_seq; 11141645d090SJeff Roberson /* 11151645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 11161645d090SJeff Roberson * rcv_nxt. 11171645d090SJeff Roberson */ 11181645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 1119df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpack++; 1120fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyte += tlen; 1121fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 11223c653157SHartmut Brandt #ifdef TCPDEBUG 11233c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 11243c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 11253c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 11263c653157SHartmut Brandt #endif 11276741ecf5SAndre Oppermann /* 11286741ecf5SAndre Oppermann * Automatic sizing of receive socket buffer. Often the send 11296741ecf5SAndre Oppermann * buffer size is not optimally adjusted to the actual network 11306741ecf5SAndre Oppermann * conditions at hand (delay bandwidth product). Setting the 11316741ecf5SAndre Oppermann * buffer size too small limits throughput on links with high 11326741ecf5SAndre Oppermann * bandwidth and high delay (eg. trans-continental/oceanic links). 11336741ecf5SAndre Oppermann * 11346741ecf5SAndre Oppermann * On the receive side the socket buffer memory is only rarely 11356741ecf5SAndre Oppermann * used to any significant extent. This allows us to be much 11366741ecf5SAndre Oppermann * more aggressive in scaling the receive socket buffer. For 11376741ecf5SAndre Oppermann * the case that the buffer space is actually used to a large 11386741ecf5SAndre Oppermann * extent and we run out of kernel memory we can simply drop 11396741ecf5SAndre Oppermann * the new segments; TCP on the sender will just retransmit it 11406741ecf5SAndre Oppermann * later. Setting the buffer size too big may only consume too 11416741ecf5SAndre Oppermann * much kernel memory if the application doesn't read() from 11426741ecf5SAndre Oppermann * the socket or packet loss or reordering makes use of the 11436741ecf5SAndre Oppermann * reassembly queue. 11446741ecf5SAndre Oppermann * 11456741ecf5SAndre Oppermann * The criteria to step up the receive buffer one notch are: 11466741ecf5SAndre Oppermann * 1. the number of bytes received during the time it takes 11476741ecf5SAndre Oppermann * one timestamp to be reflected back to us (the RTT); 11486741ecf5SAndre Oppermann * 2. received bytes per RTT is within seven eighth of the 11496741ecf5SAndre Oppermann * current socket buffer size; 11506741ecf5SAndre Oppermann * 3. receive buffer size has not hit maximal automatic size; 11516741ecf5SAndre Oppermann * 11526741ecf5SAndre Oppermann * This algorithm does one step per RTT at most and only if 11536741ecf5SAndre Oppermann * we receive a bulk stream w/o packet losses or reorderings. 11546741ecf5SAndre Oppermann * Shrinking the buffer during idle times is not necessary as 11556741ecf5SAndre Oppermann * it doesn't consume any memory when idle. 11566741ecf5SAndre Oppermann * 11576741ecf5SAndre Oppermann * TODO: Only step up if the application is actually serving 11586741ecf5SAndre Oppermann * the buffer to better manage the socket buffer resources. 1159df8bae1dSRodney W. Grimes */ 11606741ecf5SAndre Oppermann if (tcp_do_autorcvbuf && 11616741ecf5SAndre Oppermann to.to_tsecr && 11626741ecf5SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 11636741ecf5SAndre Oppermann if (to.to_tsecr > tp->rfbuf_ts && 11646741ecf5SAndre Oppermann to.to_tsecr - tp->rfbuf_ts < hz) { 11656741ecf5SAndre Oppermann if (tp->rfbuf_cnt > 11666741ecf5SAndre Oppermann (so->so_rcv.sb_hiwat / 8 * 7) && 11676741ecf5SAndre Oppermann so->so_rcv.sb_hiwat < 11686741ecf5SAndre Oppermann tcp_autorcvbuf_max) { 11696741ecf5SAndre Oppermann newsize = 11706741ecf5SAndre Oppermann min(so->so_rcv.sb_hiwat + 11716741ecf5SAndre Oppermann tcp_autorcvbuf_inc, 11726741ecf5SAndre Oppermann tcp_autorcvbuf_max); 11736741ecf5SAndre Oppermann } 11746741ecf5SAndre Oppermann /* Start over with next RTT. */ 11756741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 11766741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 11776741ecf5SAndre Oppermann } else 11786741ecf5SAndre Oppermann tp->rfbuf_cnt += tlen; /* add up */ 11796741ecf5SAndre Oppermann } 11806741ecf5SAndre Oppermann 11816741ecf5SAndre Oppermann /* Add data to socket buffer. */ 11821e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1183c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1184c1c36a2cSMike Silbersack m_freem(m); 1185c1c36a2cSMike Silbersack } else { 11866741ecf5SAndre Oppermann /* 11876741ecf5SAndre Oppermann * Set new socket buffer size. 11886741ecf5SAndre Oppermann * Give up when limit is reached. 11896741ecf5SAndre Oppermann */ 11906741ecf5SAndre Oppermann if (newsize) 11916741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 11926741ecf5SAndre Oppermann newsize, so, curthread)) 11936741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1194fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 11951e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 1196c1c36a2cSMike Silbersack } 11971e4d7da7SRobert Watson sorwakeup_locked(so); 1198d8c85a26SJonathan Lemon if (DELAY_ACK(tp)) { 11993bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1200f498eeeeSDavid Greenman } else { 1201e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 1202e612a582SDavid Greenman tcp_output(tp); 1203e612a582SDavid Greenman } 1204a14c749fSJonathan Lemon goto check_delack; 1205df8bae1dSRodney W. Grimes } 1206df8bae1dSRodney W. Grimes } 1207df8bae1dSRodney W. Grimes 1208df8bae1dSRodney W. Grimes /* 1209df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1210df8bae1dSRodney W. Grimes * and then do TCP input processing. 1211df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1212df8bae1dSRodney W. Grimes * but not less than advertised window. 1213df8bae1dSRodney W. Grimes */ 1214df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1215df8bae1dSRodney W. Grimes if (win < 0) 1216df8bae1dSRodney W. Grimes win = 0; 121766e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1218df8bae1dSRodney W. Grimes 12196741ecf5SAndre Oppermann /* Reset receive buffer auto scaling when not in bulk receive mode. */ 12206741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 12216741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 12226741ecf5SAndre Oppermann 1223df8bae1dSRodney W. Grimes switch (tp->t_state) { 1224df8bae1dSRodney W. Grimes 1225df8bae1dSRodney W. Grimes /* 1226764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1227764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1228764d8cefSBill Fenner */ 1229764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1230fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1231fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1232a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1233a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1234a57815efSBosko Milekic goto dropwithreset; 1235a57815efSBosko Milekic } 1236764d8cefSBill Fenner break; 1237764d8cefSBill Fenner 1238764d8cefSBill Fenner /* 1239df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1240df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1241df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1242df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1243df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1244df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1245df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1246df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1247df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1248df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1249df8bae1dSRodney W. Grimes */ 1250df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1251fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1252fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1253fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1254a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1255a0292f23SGarrett Wollman goto dropwithreset; 1256a0292f23SGarrett Wollman } 12570ca3f933SAndre Oppermann if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) 1258df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 12590ca3f933SAndre Oppermann if (thflags & TH_RST) 1260df8bae1dSRodney W. Grimes goto drop; 12610ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 1262df8bae1dSRodney W. Grimes goto drop; 1263464fcfbcSAndre Oppermann 1264fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1265df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1266fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1267845799c1SAndras Olah tcpstat.tcps_connects++; 1268845799c1SAndras Olah soisconnected(so); 1269c488362eSRobert Watson #ifdef MAC 1270310e7cebSRobert Watson SOCK_LOCK(so); 1271c488362eSRobert Watson mac_set_socket_peer_from_mbuf(m, so); 1272310e7cebSRobert Watson SOCK_UNLOCK(so); 1273c488362eSRobert Watson #endif 1274845799c1SAndras Olah /* Do window scaling on this connection? */ 1275845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1276845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1277845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1278845799c1SAndras Olah } 1279a0292f23SGarrett Wollman tp->rcv_adv += tp->rcv_wnd; 1280a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1281a0292f23SGarrett Wollman /* 1282a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1283a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1284a0292f23SGarrett Wollman */ 1285d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && tlen != 0) 1286b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 1287b8152ba7SAndre Oppermann tcp_delacktime); 1288a0292f23SGarrett Wollman else 1289a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1290a0292f23SGarrett Wollman /* 1291a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1292a0292f23SGarrett Wollman * Transitions: 1293a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1294a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1295a0292f23SGarrett Wollman */ 12969b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1297a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1298a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1299a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1300fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 13017ff19458SPaul Traina } else { 1302a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1303b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 13047ff19458SPaul Traina } 1305a0292f23SGarrett Wollman } else { 1306a0292f23SGarrett Wollman /* 1307c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 1308c068736aSJeffrey Hsu * simultaneous open. If segment contains CC option 1309c068736aSJeffrey Hsu * and there is a cached CC, apply TAO test. 1310c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 1311c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 1312a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1313a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1314a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1315a0292f23SGarrett Wollman */ 13164b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 1317b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1318df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1319a0292f23SGarrett Wollman } 1320df8bae1dSRodney W. Grimes 1321302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: trimthenstep6: head not locked", 1322302ce8d6SAndre Oppermann __func__)); 1323302ce8d6SAndre Oppermann INP_LOCK_ASSERT(tp->t_inpcb); 13247cfc6904SRobert Watson 1325df8bae1dSRodney W. Grimes /* 1326fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1327df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1328df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1329df8bae1dSRodney W. Grimes */ 1330fb59c426SYoshinobu Inoue th->th_seq++; 1331fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1332fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1333df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1334fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1335fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1336df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1337df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1338df8bae1dSRodney W. Grimes } 1339fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1340fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 1341a0292f23SGarrett Wollman /* 1342a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1343a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1344a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1345a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1346a0292f23SGarrett Wollman * Otherwise, goto step 6. 1347a0292f23SGarrett Wollman */ 1348fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1349a0292f23SGarrett Wollman goto process_ACK; 1350c068736aSJeffrey Hsu 1351df8bae1dSRodney W. Grimes goto step6; 1352c068736aSJeffrey Hsu 1353a0292f23SGarrett Wollman /* 1354a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1355c94c54e4SAndre Oppermann * do normal processing. 1356a0292f23SGarrett Wollman * 1357c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 1358a0292f23SGarrett Wollman */ 1359a0292f23SGarrett Wollman case TCPS_LAST_ACK: 1360a0292f23SGarrett Wollman case TCPS_CLOSING: 1361a0292f23SGarrett Wollman break; /* continue normal processing */ 1362df8bae1dSRodney W. Grimes } 1363df8bae1dSRodney W. Grimes 1364df8bae1dSRodney W. Grimes /* 1365df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 136680ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 136780ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 136880ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 136980ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 137080ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 137180ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 137280ab7c0eSGarrett Wollman * killing a connection). 137380ab7c0eSGarrett Wollman * Then check timestamp, if present. 1374a0292f23SGarrett Wollman * Then check the connection count, if present. 1375df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 1376df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 1377df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 1378df8bae1dSRodney W. Grimes * 137980ab7c0eSGarrett Wollman * 138080ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 138180ab7c0eSGarrett Wollman * if this is a valid reset segment. 138280ab7c0eSGarrett Wollman * RFC 793 page 37: 138380ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 138480ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 138580ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 138680ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 138780ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 13881a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 13891a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 13901a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 13911a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 139280dd2a81SMike Silbersack * Note 2: Paul Watson's paper "Slipping in the Window" has shown 139380dd2a81SMike Silbersack * that brute force RST attacks are possible. To combat this, 139480dd2a81SMike Silbersack * we use a much stricter check while in the ESTABLISHED state, 139580dd2a81SMike Silbersack * only accepting RSTs where the sequence number is equal to 139680dd2a81SMike Silbersack * last_ack_sent. In all other states (the states in which a 139780dd2a81SMike Silbersack * RST is more likely), the more permissive check is used. 139880ab7c0eSGarrett Wollman * If we have multiple segments in flight, the intial reset 139980ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 140080ab7c0eSGarrett Wollman * but they will eventually catch up. 140180ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 140280ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 140380ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 140480ab7c0eSGarrett Wollman * 140580ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 140680ab7c0eSGarrett Wollman * 140780ab7c0eSGarrett Wollman * DISCUSSION 140880ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 140980ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 141080ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 141180ab7c0eSGarrett Wollman * data. 141280ab7c0eSGarrett Wollman * 141380ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 141480ab7c0eSGarrett Wollman * the state: 141580ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 141680ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 141780ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 1418745bab7fSDima Dorfman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 141980ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 1420e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 142180ab7c0eSGarrett Wollman * Close the tcb. 1422e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 142380ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 142480ab7c0eSGarrett Wollman * RFC 1337. 142580ab7c0eSGarrett Wollman */ 1426fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 142795ad8418SQing Li if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 142895ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 142980ab7c0eSGarrett Wollman switch (tp->t_state) { 143080ab7c0eSGarrett Wollman 143180ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 143280ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 143380ab7c0eSGarrett Wollman goto close; 143480ab7c0eSGarrett Wollman 143580ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 143695ad8418SQing Li if (tcp_insecure_rst == 0 && 143795ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) && 143895ad8418SQing Li SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) && 143995ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 144095ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) { 144180dd2a81SMike Silbersack tcpstat.tcps_badrst++; 144280dd2a81SMike Silbersack goto drop; 144380dd2a81SMike Silbersack } 144480ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 144580ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 144680ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 144780ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 144880ab7c0eSGarrett Wollman close: 144980ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 145080ab7c0eSGarrett Wollman tcpstat.tcps_drops++; 1451302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: trimthenstep6: " 1452302ce8d6SAndre Oppermann "tcp_close: head not locked", __func__)); 145380ab7c0eSGarrett Wollman tp = tcp_close(tp); 145480ab7c0eSGarrett Wollman break; 145580ab7c0eSGarrett Wollman 145680ab7c0eSGarrett Wollman case TCPS_CLOSING: 145780ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 1458302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: trimthenstep6: " 1459302ce8d6SAndre Oppermann "tcp_close.2: head not locked", __func__)); 146080ab7c0eSGarrett Wollman tp = tcp_close(tp); 146180ab7c0eSGarrett Wollman break; 146280ab7c0eSGarrett Wollman } 146380ab7c0eSGarrett Wollman } 146480ab7c0eSGarrett Wollman goto drop; 146580ab7c0eSGarrett Wollman } 146680ab7c0eSGarrett Wollman 146780ab7c0eSGarrett Wollman /* 1468df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 1469df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 1470df8bae1dSRodney W. Grimes */ 1471be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 147280ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1473df8bae1dSRodney W. Grimes 1474df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 14759b8b58e0SJonathan Lemon if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1476df8bae1dSRodney W. Grimes /* 1477df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 1478df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 1479df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 1480df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 1481df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 1482df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 1483df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 1484df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 1485df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 1486df8bae1dSRodney W. Grimes */ 1487df8bae1dSRodney W. Grimes tp->ts_recent = 0; 1488df8bae1dSRodney W. Grimes } else { 1489df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 1490fb59c426SYoshinobu Inoue tcpstat.tcps_rcvdupbyte += tlen; 1491df8bae1dSRodney W. Grimes tcpstat.tcps_pawsdrop++; 149207fd333dSMatthew Dillon if (tlen) 1493df8bae1dSRodney W. Grimes goto dropafterack; 14941ab4789dSMatthew Dillon goto drop; 14951ab4789dSMatthew Dillon } 1496df8bae1dSRodney W. Grimes } 1497df8bae1dSRodney W. Grimes 1498a0292f23SGarrett Wollman /* 149980ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 150080ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 150180ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 150280ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 150380ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 150480ab7c0eSGarrett Wollman */ 1505a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 1506a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1507a57815efSBosko Milekic goto dropwithreset; 1508a57815efSBosko Milekic } 150980ab7c0eSGarrett Wollman 1510fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 1511df8bae1dSRodney W. Grimes if (todrop > 0) { 1512fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1513fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 1514fb59c426SYoshinobu Inoue th->th_seq++; 1515fb59c426SYoshinobu Inoue if (th->th_urp > 1) 1516fb59c426SYoshinobu Inoue th->th_urp--; 1517df8bae1dSRodney W. Grimes else 1518fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1519df8bae1dSRodney W. Grimes todrop--; 1520df8bae1dSRodney W. Grimes } 1521df8bae1dSRodney W. Grimes /* 1522dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 1523df8bae1dSRodney W. Grimes */ 1524fb59c426SYoshinobu Inoue if (todrop > tlen 1525fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1526dac20301SGarrett Wollman /* 1527dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 1528dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 1529dac20301SGarrett Wollman * of sequence; drop it. 1530dac20301SGarrett Wollman */ 1531fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1532dac20301SGarrett Wollman 1533df8bae1dSRodney W. Grimes /* 1534dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 1535dac20301SGarrett Wollman * But keep on processing for RST or ACK. 1536df8bae1dSRodney W. Grimes */ 1537dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1538fb59c426SYoshinobu Inoue todrop = tlen; 1539dac20301SGarrett Wollman tcpstat.tcps_rcvduppack++; 1540dac20301SGarrett Wollman tcpstat.tcps_rcvdupbyte += todrop; 1541df8bae1dSRodney W. Grimes } else { 1542df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartduppack++; 1543df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartdupbyte += todrop; 1544df8bae1dSRodney W. Grimes } 1545fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 1546fb59c426SYoshinobu Inoue th->th_seq += todrop; 1547fb59c426SYoshinobu Inoue tlen -= todrop; 1548fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 1549fb59c426SYoshinobu Inoue th->th_urp -= todrop; 1550df8bae1dSRodney W. Grimes else { 1551fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1552fb59c426SYoshinobu Inoue th->th_urp = 0; 1553df8bae1dSRodney W. Grimes } 1554df8bae1dSRodney W. Grimes } 1555df8bae1dSRodney W. Grimes 1556df8bae1dSRodney W. Grimes /* 1557df8bae1dSRodney W. Grimes * If new data are received on a connection after the 1558df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 1559df8bae1dSRodney W. Grimes */ 1560df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 1561fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1562302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: trimthenstep6: tcp_close.3: head " 1563302ce8d6SAndre Oppermann "not locked", __func__)); 1564df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1565df8bae1dSRodney W. Grimes tcpstat.tcps_rcvafterclose++; 1566a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1567df8bae1dSRodney W. Grimes goto dropwithreset; 1568df8bae1dSRodney W. Grimes } 1569df8bae1dSRodney W. Grimes 1570df8bae1dSRodney W. Grimes /* 1571df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 1572df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 1573df8bae1dSRodney W. Grimes */ 1574fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 1575df8bae1dSRodney W. Grimes if (todrop > 0) { 1576df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1577fb59c426SYoshinobu Inoue if (todrop >= tlen) { 1578fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyteafterwin += tlen; 1579df8bae1dSRodney W. Grimes /* 1580df8bae1dSRodney W. Grimes * If window is closed can only take segments at 1581df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 1582df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 1583df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 1584df8bae1dSRodney W. Grimes * and ack. 1585df8bae1dSRodney W. Grimes */ 1586fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1587df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1588df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinprobe++; 1589df8bae1dSRodney W. Grimes } else 1590df8bae1dSRodney W. Grimes goto dropafterack; 1591df8bae1dSRodney W. Grimes } else 1592df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1593df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1594fb59c426SYoshinobu Inoue tlen -= todrop; 1595fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 1596df8bae1dSRodney W. Grimes } 1597df8bae1dSRodney W. Grimes 1598df8bae1dSRodney W. Grimes /* 1599df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1600df8bae1dSRodney W. Grimes * record its timestamp. 1601cf09195bSPaul Saab * NOTE: 1602cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 1603a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1604cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 1605cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 1606cf09195bSPaul Saab * predicated on the sequence space of this segment. 1607cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 1608cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 1609cf09195bSPaul Saab * instead of RFC1323's 1610cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 1611cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 1612cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 1613cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 1614cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 1615df8bae1dSRodney W. Grimes */ 1616be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1617cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 1618cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 1619cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 16209b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1621a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1622df8bae1dSRodney W. Grimes } 1623df8bae1dSRodney W. Grimes 1624df8bae1dSRodney W. Grimes /* 1625df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 1626df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 1627df8bae1dSRodney W. Grimes */ 1628fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1629302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: tcp_drop: trimthenstep6: " 1630302ce8d6SAndre Oppermann "head not locked", __func__)); 1631df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 1632a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1633122aad88SAndre Oppermann goto drop; 1634df8bae1dSRodney W. Grimes } 1635df8bae1dSRodney W. Grimes 1636a0292f23SGarrett Wollman /* 1637a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1638a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 1639a0292f23SGarrett Wollman * later processing; else drop segment and return. 1640a0292f23SGarrett Wollman */ 1641fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 1642a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 1643a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 1644a0292f23SGarrett Wollman goto step6; 16455e1aa279SDavid Malone else if (tp->t_flags & TF_ACKNOW) 16465e1aa279SDavid Malone goto dropafterack; 1647a0292f23SGarrett Wollman else 1648a0292f23SGarrett Wollman goto drop; 1649a0292f23SGarrett Wollman } 1650df8bae1dSRodney W. Grimes 1651df8bae1dSRodney W. Grimes /* 1652df8bae1dSRodney W. Grimes * Ack processing. 1653df8bae1dSRodney W. Grimes */ 1654df8bae1dSRodney W. Grimes switch (tp->t_state) { 1655df8bae1dSRodney W. Grimes 1656df8bae1dSRodney W. Grimes /* 1657764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1658764d8cefSBill Fenner * ESTABLISHED state and continue processing. 1659764d8cefSBill Fenner * The ACK was checked above. 1660df8bae1dSRodney W. Grimes */ 1661df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1662a0292f23SGarrett Wollman 1663df8bae1dSRodney W. Grimes tcpstat.tcps_connects++; 1664df8bae1dSRodney W. Grimes soisconnected(so); 1665df8bae1dSRodney W. Grimes /* Do window scaling? */ 1666df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1667df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1668df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 1669464fcfbcSAndre Oppermann tp->snd_wnd = tiwin; 1670df8bae1dSRodney W. Grimes } 1671a0292f23SGarrett Wollman /* 1672a0292f23SGarrett Wollman * Make transitions: 1673a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 1674a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 1675a0292f23SGarrett Wollman */ 16769b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1677a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1678a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1679a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 16807ff19458SPaul Traina } else { 1681a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1682b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 16837ff19458SPaul Traina } 1684a0292f23SGarrett Wollman /* 1685a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 1686a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 1687a0292f23SGarrett Wollman */ 1688fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 1689fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1690a0292f23SGarrett Wollman (struct mbuf *)0); 1691fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 169293b0017fSPhilippe Charnier /* FALLTHROUGH */ 1693df8bae1dSRodney W. Grimes 1694df8bae1dSRodney W. Grimes /* 1695df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1696df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 1697fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 1698fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 1699df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 1700df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 1701df8bae1dSRodney W. Grimes */ 1702df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1703df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1704df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 1705df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1706df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1707df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 17085a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 17095a53ca16SPaul Saab tcpstat.tcps_rcvacktoomuch++; 17105a53ca16SPaul Saab goto dropafterack; 17115a53ca16SPaul Saab } 17123529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 1713fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 1714fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 17155a53ca16SPaul Saab tcp_sack_doack(tp, &to, th->th_ack); 1716fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1717fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 1718df8bae1dSRodney W. Grimes tcpstat.tcps_rcvdupack++; 1719df8bae1dSRodney W. Grimes /* 1720df8bae1dSRodney W. Grimes * If we have outstanding data (other than 1721df8bae1dSRodney W. Grimes * a window probe), this is a completely 1722df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 1723df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 1724df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 1725df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 1726df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 1727df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 1728df8bae1dSRodney W. Grimes * window so we send only this one 1729df8bae1dSRodney W. Grimes * packet. 1730df8bae1dSRodney W. Grimes * 1731df8bae1dSRodney W. Grimes * We know we're losing at the current 1732df8bae1dSRodney W. Grimes * window size so do congestion avoidance 1733df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 1734df8bae1dSRodney W. Grimes * and pull our congestion window back to 1735df8bae1dSRodney W. Grimes * the new ssthresh). 1736df8bae1dSRodney W. Grimes * 1737df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 1738df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 1739df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 1740df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 1741df8bae1dSRodney W. Grimes * network. 1742df8bae1dSRodney W. Grimes */ 1743b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_REXMT) || 1744fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 1745df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1746cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 17473529149eSAndre Oppermann ((tcp_do_newreno || 17483529149eSAndre Oppermann (tp->t_flags & TF_SACK_PERMIT)) && 17499d11646dSJeffrey Hsu IN_FASTRECOVERY(tp))) { 17503529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 17513529149eSAndre Oppermann IN_FASTRECOVERY(tp)) { 1752808f11b7SPaul Saab int awnd; 175325e6f9edSPaul Saab 175425e6f9edSPaul Saab /* 175525e6f9edSPaul Saab * Compute the amount of data in flight first. 175625e6f9edSPaul Saab * We can inject new data into the pipe iff 175725e6f9edSPaul Saab * we have less than 1/2 the original window's 175825e6f9edSPaul Saab * worth of data in flight. 175925e6f9edSPaul Saab */ 1760808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 17610077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 1762808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 176325e6f9edSPaul Saab tp->snd_cwnd += tp->t_maxseg; 176425e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 176525e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 176625e6f9edSPaul Saab } 176725e6f9edSPaul Saab } else 176846f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 176946f58482SJonathan Lemon (void) tcp_output(tp); 177046f58482SJonathan Lemon goto drop; 1771cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 1772cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 1773cb942153SJeffrey Hsu u_int win; 1774a0445c2eSJayanth Vijayaraghavan 1775a0445c2eSJayanth Vijayaraghavan /* 1776a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 1777a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 1778a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 1779a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 1780a0445c2eSJayanth Vijayaraghavan * recovery. 1781a0445c2eSJayanth Vijayaraghavan */ 17823529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 1783a0445c2eSJayanth Vijayaraghavan if (IN_FASTRECOVERY(tp)) { 1784a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 1785a0445c2eSJayanth Vijayaraghavan break; 1786a0445c2eSJayanth Vijayaraghavan } 1787a0445c2eSJayanth Vijayaraghavan } else if (tcp_do_newreno) { 1788a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 17899d11646dSJeffrey Hsu tp->snd_recover)) { 1790cb942153SJeffrey Hsu tp->t_dupacks = 0; 1791cb942153SJeffrey Hsu break; 179246f58482SJonathan Lemon } 1793a0445c2eSJayanth Vijayaraghavan } 1794cb942153SJeffrey Hsu win = min(tp->snd_wnd, tp->snd_cwnd) / 1795cb942153SJeffrey Hsu 2 / tp->t_maxseg; 1796df8bae1dSRodney W. Grimes if (win < 2) 1797df8bae1dSRodney W. Grimes win = 2; 1798df8bae1dSRodney W. Grimes tp->snd_ssthresh = win * tp->t_maxseg; 17999d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 180046f58482SJonathan Lemon tp->snd_recover = tp->snd_max; 1801b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 18029b8b58e0SJonathan Lemon tp->t_rtttime = 0; 18033529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 18046d90faf3SPaul Saab tcpstat.tcps_sack_recovery_episode++; 1805a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 18068db456bfSPaul Saab tp->snd_cwnd = tp->t_maxseg; 18076d90faf3SPaul Saab (void) tcp_output(tp); 18086d90faf3SPaul Saab goto drop; 18096d90faf3SPaul Saab } 1810fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 1811df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 1812df8bae1dSRodney W. Grimes (void) tcp_output(tp); 181348d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 1814302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 1815302ce8d6SAndre Oppermann __func__)); 1816df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 181748d2549cSJeffrey Hsu tp->t_maxseg * 181848d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 1819df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 1820df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 1821df8bae1dSRodney W. Grimes goto drop; 1822582a954bSJeffrey Hsu } else if (tcp_do_rfc3042) { 1823582a954bSJeffrey Hsu u_long oldcwnd = tp->snd_cwnd; 182448d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 182548d2549cSJeffrey Hsu u_int sent; 182689c02376SJeffrey Hsu 1827582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 1828582a954bSJeffrey Hsu tp->t_dupacks == 2, 1829302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 1830302ce8d6SAndre Oppermann __func__)); 183161a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 183248d2549cSJeffrey Hsu tp->snd_limited = 0; 183361a36e3dSJeffrey Hsu tp->snd_cwnd = 183461a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 183561a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 183661a36e3dSJeffrey Hsu tp->t_maxseg; 1837582a954bSJeffrey Hsu (void) tcp_output(tp); 183848d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 183948d2549cSJeffrey Hsu if (sent > tp->t_maxseg) { 184089c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 184189c02376SJeffrey Hsu tp->snd_limited == 0) || 184289c02376SJeffrey Hsu (sent == tp->t_maxseg + 1 && 184389c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 1844302ce8d6SAndre Oppermann ("%s: sent too much", 1845302ce8d6SAndre Oppermann __func__)); 184648d2549cSJeffrey Hsu tp->snd_limited = 2; 184748d2549cSJeffrey Hsu } else if (sent > 0) 184848d2549cSJeffrey Hsu ++tp->snd_limited; 1849582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 1850582a954bSJeffrey Hsu goto drop; 1851df8bae1dSRodney W. Grimes } 1852df8bae1dSRodney W. Grimes } else 1853df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1854df8bae1dSRodney W. Grimes break; 1855df8bae1dSRodney W. Grimes } 1856cb942153SJeffrey Hsu 1857302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 1858302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 1859cb942153SJeffrey Hsu 1860df8bae1dSRodney W. Grimes /* 1861df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 1862df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 1863df8bae1dSRodney W. Grimes */ 18643529149eSAndre Oppermann if (tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) { 18659d11646dSJeffrey Hsu if (IN_FASTRECOVERY(tp)) { 1866cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 18673529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 18686d90faf3SPaul Saab tcp_sack_partialack(tp, th); 18696d90faf3SPaul Saab else 1870c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 1871c068736aSJeffrey Hsu } else { 1872c068736aSJeffrey Hsu /* 18736d90faf3SPaul Saab * Out of fast recovery. 1874c068736aSJeffrey Hsu * Window inflation should have left us 1875c068736aSJeffrey Hsu * with approximately snd_ssthresh 1876c068736aSJeffrey Hsu * outstanding data. 1877c068736aSJeffrey Hsu * But in case we would be inclined to 1878c068736aSJeffrey Hsu * send a burst, better to do it via 1879c068736aSJeffrey Hsu * the slow start mechanism. 1880c068736aSJeffrey Hsu */ 1881c068736aSJeffrey Hsu if (SEQ_GT(th->th_ack + 1882c068736aSJeffrey Hsu tp->snd_ssthresh, 1883c068736aSJeffrey Hsu tp->snd_max)) 1884c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_max - 1885c068736aSJeffrey Hsu th->th_ack + 1886c068736aSJeffrey Hsu tp->t_maxseg; 1887c068736aSJeffrey Hsu else 1888c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_ssthresh; 1889c068736aSJeffrey Hsu } 1890c068736aSJeffrey Hsu } 1891c068736aSJeffrey Hsu } else { 1892233e8c18SGarrett Wollman if (tp->t_dupacks >= tcprexmtthresh && 1893df8bae1dSRodney W. Grimes tp->snd_cwnd > tp->snd_ssthresh) 1894df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh; 189546f58482SJonathan Lemon } 1896cb942153SJeffrey Hsu tp->t_dupacks = 0; 1897a0292f23SGarrett Wollman /* 1898a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 1899a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 1900a0292f23SGarrett Wollman */ 1901a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 1902a0292f23SGarrett Wollman /* 1903a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 1904a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 190507e43e10SAndras Olah * synchronized). Go to non-starred state, 190607e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 190707e43e10SAndras Olah * we can do window scaling. 1908a0292f23SGarrett Wollman */ 1909a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 1910a0292f23SGarrett Wollman tp->snd_una++; 191107e43e10SAndras Olah /* Do window scaling? */ 191207e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 191307e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 191407e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 1915464fcfbcSAndre Oppermann /* Send window already scaled. */ 191607e43e10SAndras Olah } 1917a0292f23SGarrett Wollman } 1918a0292f23SGarrett Wollman 1919a0292f23SGarrett Wollman process_ACK: 1920302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: process_ACK: head not locked", 1921302ce8d6SAndre Oppermann __func__)); 1922302ce8d6SAndre Oppermann INP_LOCK_ASSERT(tp->t_inpcb); 19237cfc6904SRobert Watson 1924fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 1925df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 1926df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 1927df8bae1dSRodney W. Grimes 1928df8bae1dSRodney W. Grimes /* 19299b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 19309b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 19319b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 19329b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 19339b8b58e0SJonathan Lemon * we left off. 19349b8b58e0SJonathan Lemon */ 19359b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 1936d65bf08aSMatthew Dillon ++tcpstat.tcps_sndrexmitbad; 19379b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 19389b8b58e0SJonathan Lemon tp->snd_ssthresh = tp->snd_ssthresh_prev; 19399d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 19409d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 19419d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 19429b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 19439b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; /* XXX probably not required */ 19449b8b58e0SJonathan Lemon } 19459b8b58e0SJonathan Lemon 19469b8b58e0SJonathan Lemon /* 1947df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 1948df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 1949df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 1950df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 1951df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 1952df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 1953df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 1954fa55172bSMatthew Dillon * 1955fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1956fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1957fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1958fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1959df8bae1dSRodney W. Grimes */ 1960fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1961fa55172bSMatthew Dillon to.to_tsecr) { 1962eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - to.to_tsecr) 1963eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 19649b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 1965fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 1966eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 1967eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 19689b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 1969fa55172bSMatthew Dillon } 19701fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 1971df8bae1dSRodney W. Grimes 1972df8bae1dSRodney W. Grimes /* 1973df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 1974df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 1975df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 1976df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 1977df8bae1dSRodney W. Grimes */ 1978fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 1979b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1980df8bae1dSRodney W. Grimes needoutput = 1; 1981b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 1982b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 1983a0292f23SGarrett Wollman 1984a0292f23SGarrett Wollman /* 1985a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 1986a0292f23SGarrett Wollman * skip rest of ACK processing. 1987a0292f23SGarrett Wollman */ 1988a0292f23SGarrett Wollman if (acked == 0) 1989a0292f23SGarrett Wollman goto step6; 1990a0292f23SGarrett Wollman 1991df8bae1dSRodney W. Grimes /* 1992df8bae1dSRodney W. Grimes * When new data is acked, open the congestion window. 1993df8bae1dSRodney W. Grimes * If the window gives us less than ssthresh packets 1994df8bae1dSRodney W. Grimes * in flight, open exponentially (maxseg per packet). 1995df8bae1dSRodney W. Grimes * Otherwise open linearly: maxseg per window 199610be5648SGarrett Wollman * (maxseg^2 / cwnd per packet). 1997df8bae1dSRodney W. Grimes */ 19983529149eSAndre Oppermann if ((!tcp_do_newreno && !(tp->t_flags & TF_SACK_PERMIT)) || 19996d90faf3SPaul Saab !IN_FASTRECOVERY(tp)) { 2000ad3f9ab3SAndre Oppermann u_int cw = tp->snd_cwnd; 2001ad3f9ab3SAndre Oppermann u_int incr = tp->t_maxseg; 2002df8bae1dSRodney W. Grimes if (cw > tp->snd_ssthresh) 200310be5648SGarrett Wollman incr = incr * incr / cw; 2004df8bae1dSRodney W. Grimes tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale); 2005df8bae1dSRodney W. Grimes } 20065905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2007df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 2008df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 20095905999bSRobert Watson sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc); 2010df8bae1dSRodney W. Grimes ourfinisacked = 1; 2011df8bae1dSRodney W. Grimes } else { 20125905999bSRobert Watson sbdrop_locked(&so->so_snd, acked); 2013df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 2014df8bae1dSRodney W. Grimes ourfinisacked = 0; 2015df8bae1dSRodney W. Grimes } 20161e4d7da7SRobert Watson sowwakeup_locked(so); 2017cb942153SJeffrey Hsu /* detect una wraparound */ 20183529149eSAndre Oppermann if ((tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) && 20196d90faf3SPaul Saab !IN_FASTRECOVERY(tp) && 20209d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 20219d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 20229d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 20233529149eSAndre Oppermann if ((tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) && 20246d90faf3SPaul Saab IN_FASTRECOVERY(tp) && 20259d11646dSJeffrey Hsu SEQ_GEQ(th->th_ack, tp->snd_recover)) 20269d11646dSJeffrey Hsu EXIT_FASTRECOVERY(tp); 2027fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 20283529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 20296d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 20306d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 20316d90faf3SPaul Saab } 2032df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2033df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2034df8bae1dSRodney W. Grimes 2035df8bae1dSRodney W. Grimes switch (tp->t_state) { 2036df8bae1dSRodney W. Grimes 2037df8bae1dSRodney W. Grimes /* 2038df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2039df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2040df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2041df8bae1dSRodney W. Grimes */ 2042df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2043df8bae1dSRodney W. Grimes if (ourfinisacked) { 2044df8bae1dSRodney W. Grimes /* 2045df8bae1dSRodney W. Grimes * If we can't receive any more 2046df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2047df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2048df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2049df8bae1dSRodney W. Grimes * we'll hang forever. 2050df8bae1dSRodney W. Grimes */ 2051340c35deSJonathan Lemon /* XXXjl 2052340c35deSJonathan Lemon * we should release the tp also, and use a 2053340c35deSJonathan Lemon * compressed state. 2054340c35deSJonathan Lemon */ 2055c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 20567c72af87SMohan Srinivasan int timeout; 20577c72af87SMohan Srinivasan 205803e49181SSeigo Tanimura soisdisconnected(so); 20597c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 20607c72af87SMohan Srinivasan tcp_finwait2_timeout : tcp_maxidle; 2061b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 20624cc20ab1SSeigo Tanimura } 2063df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 2064df8bae1dSRodney W. Grimes } 2065df8bae1dSRodney W. Grimes break; 2066df8bae1dSRodney W. Grimes 2067df8bae1dSRodney W. Grimes /* 2068df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2069df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2070df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2071df8bae1dSRodney W. Grimes * the segment. 2072df8bae1dSRodney W. Grimes */ 2073df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2074df8bae1dSRodney W. Grimes if (ourfinisacked) { 2075302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: process_ACK: " 2076302ce8d6SAndre Oppermann "head not locked", __func__)); 207711a20fb8SJeffrey Hsu tcp_twstart(tp); 2078340c35deSJonathan Lemon INP_INFO_WUNLOCK(&tcbinfo); 2079302ce8d6SAndre Oppermann headlocked = 0; 2080340c35deSJonathan Lemon m_freem(m); 2081679d9708SAndre Oppermann return; 2082df8bae1dSRodney W. Grimes } 2083df8bae1dSRodney W. Grimes break; 2084df8bae1dSRodney W. Grimes 2085df8bae1dSRodney W. Grimes /* 2086df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2087df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2088df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2089df8bae1dSRodney W. Grimes * enter the closed state and return. 2090df8bae1dSRodney W. Grimes */ 2091df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2092df8bae1dSRodney W. Grimes if (ourfinisacked) { 2093302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: process_ACK: " 2094302ce8d6SAndre Oppermann "tcp_close: head not locked", __func__)); 2095df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2096df8bae1dSRodney W. Grimes goto drop; 2097df8bae1dSRodney W. Grimes } 2098df8bae1dSRodney W. Grimes break; 2099df8bae1dSRodney W. Grimes } 2100df8bae1dSRodney W. Grimes } 2101df8bae1dSRodney W. Grimes 2102df8bae1dSRodney W. Grimes step6: 2103302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: step6: head not locked", __func__)); 2104302ce8d6SAndre Oppermann INP_LOCK_ASSERT(tp->t_inpcb); 21057cfc6904SRobert Watson 2106df8bae1dSRodney W. Grimes /* 2107df8bae1dSRodney W. Grimes * Update window information. 2108df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 2109df8bae1dSRodney W. Grimes */ 2110fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 2111fb59c426SYoshinobu Inoue (SEQ_LT(tp->snd_wl1, th->th_seq) || 2112fb59c426SYoshinobu Inoue (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2113fb59c426SYoshinobu Inoue (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2114df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 2115fb59c426SYoshinobu Inoue if (tlen == 0 && 2116fb59c426SYoshinobu Inoue tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2117df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinupd++; 2118df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 2119fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq; 2120fb59c426SYoshinobu Inoue tp->snd_wl2 = th->th_ack; 2121df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2122df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 2123df8bae1dSRodney W. Grimes needoutput = 1; 2124df8bae1dSRodney W. Grimes } 2125df8bae1dSRodney W. Grimes 2126df8bae1dSRodney W. Grimes /* 2127df8bae1dSRodney W. Grimes * Process segments with URG. 2128df8bae1dSRodney W. Grimes */ 2129fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2130df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2131df8bae1dSRodney W. Grimes /* 2132df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2133df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2134df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2135df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2136df8bae1dSRodney W. Grimes */ 213718ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2138fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2139fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2140fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 214118ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2142df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2143df8bae1dSRodney W. Grimes } 2144df8bae1dSRodney W. Grimes /* 2145df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2146df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2147df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2148df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2149df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2150df8bae1dSRodney W. Grimes * 2151df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2152df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2153df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2154df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2155df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2156df8bae1dSRodney W. Grimes * spec states (in one of two places). 2157df8bae1dSRodney W. Grimes */ 2158fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2159fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2160df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2161df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2162927c5ceaSRobert Watson if (so->so_oobmark == 0) 2163c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 2164df8bae1dSRodney W. Grimes sohasoutofband(so); 2165df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2166df8bae1dSRodney W. Grimes } 216718ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2168df8bae1dSRodney W. Grimes /* 2169df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2170df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2171df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2172df8bae1dSRodney W. Grimes * data may creep in... ick. 2173df8bae1dSRodney W. Grimes */ 217430613f56SJeffrey Hsu if (th->th_urp <= (u_long)tlen && 217530613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 217630613f56SJeffrey Hsu /* hdr drop is delayed */ 217730613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 217830613f56SJeffrey Hsu } 2179c068736aSJeffrey Hsu } else { 2180df8bae1dSRodney W. Grimes /* 2181df8bae1dSRodney W. Grimes * If no out of band data is expected, 2182df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2183df8bae1dSRodney W. Grimes * with the receive window. 2184df8bae1dSRodney W. Grimes */ 2185df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2186df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2187c068736aSJeffrey Hsu } 2188df8bae1dSRodney W. Grimes dodata: /* XXX */ 2189302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: dodata: head not locked", __func__)); 2190302ce8d6SAndre Oppermann INP_LOCK_ASSERT(tp->t_inpcb); 21917cfc6904SRobert Watson 2192df8bae1dSRodney W. Grimes /* 2193df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2194df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2195df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2196df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2197df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2198df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2199df8bae1dSRodney W. Grimes */ 2200fb59c426SYoshinobu Inoue if ((tlen || (thflags & TH_FIN)) && 2201df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 220269e03620SPaul Saab tcp_seq save_start = th->th_seq; 220369e03620SPaul Saab tcp_seq save_end = th->th_seq + tlen; 2204fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2205e4b64281SJesper Skriver /* 2206c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2207c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2208c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2209c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2210c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2211c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2212c068736aSJeffrey Hsu * conversions. 2213c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 2214c068736aSJeffrey Hsu * immediately when segments are out of order (so 2215c068736aSJeffrey Hsu * fast retransmit can work). 2216e4b64281SJesper Skriver */ 2217e4b64281SJesper Skriver if (th->th_seq == tp->rcv_nxt && 2218e4b64281SJesper Skriver LIST_EMPTY(&tp->t_segq) && 2219e4b64281SJesper Skriver TCPS_HAVEESTABLISHED(tp->t_state)) { 2220e4b64281SJesper Skriver if (DELAY_ACK(tp)) 22213bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2222e4b64281SJesper Skriver else 2223e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2224e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2225e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 2226e4b64281SJesper Skriver tcpstat.tcps_rcvpack++; 2227e4b64281SJesper Skriver tcpstat.tcps_rcvbyte += tlen; 2228e4b64281SJesper Skriver ND6_HINT(tp); 22291e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2230c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2231c1c36a2cSMike Silbersack m_freem(m); 2232c1c36a2cSMike Silbersack else 22331e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 22341e4d7da7SRobert Watson sorwakeup_locked(so); 2235e4b64281SJesper Skriver } else { 2236e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 2237e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2238e4b64281SJesper Skriver } 22393529149eSAndre Oppermann if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 224069e03620SPaul Saab tcp_update_sack_list(tp, save_start, save_end); 2241302ce8d6SAndre Oppermann #if 0 2242df8bae1dSRodney W. Grimes /* 2243df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2244df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2245df8bae1dSRodney W. Grimes * buffer size. 2246302ce8d6SAndre Oppermann * XXX: Unused. 2247df8bae1dSRodney W. Grimes */ 2248df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2249302ce8d6SAndre Oppermann #endif 2250df8bae1dSRodney W. Grimes } else { 2251df8bae1dSRodney W. Grimes m_freem(m); 2252fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2253df8bae1dSRodney W. Grimes } 2254df8bae1dSRodney W. Grimes 2255df8bae1dSRodney W. Grimes /* 2256df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2257df8bae1dSRodney W. Grimes * that the connection is closing. 2258df8bae1dSRodney W. Grimes */ 2259fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2260df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2261df8bae1dSRodney W. Grimes socantrcvmore(so); 2262a0292f23SGarrett Wollman /* 2263a0292f23SGarrett Wollman * If connection is half-synchronized 2264d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2265a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2266a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2267a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2268a0292f23SGarrett Wollman */ 22693bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 22703bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2271a0292f23SGarrett Wollman else 2272df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2273df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2274df8bae1dSRodney W. Grimes } 2275df8bae1dSRodney W. Grimes switch (tp->t_state) { 2276df8bae1dSRodney W. Grimes 2277df8bae1dSRodney W. Grimes /* 2278df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2279df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2280df8bae1dSRodney W. Grimes */ 2281df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 22829b8b58e0SJonathan Lemon tp->t_starttime = ticks; 22839b8b58e0SJonathan Lemon /*FALLTHROUGH*/ 2284df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2285df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2286df8bae1dSRodney W. Grimes break; 2287df8bae1dSRodney W. Grimes 2288df8bae1dSRodney W. Grimes /* 2289df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2290df8bae1dSRodney W. Grimes * enter the CLOSING state. 2291df8bae1dSRodney W. Grimes */ 2292df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2293df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 2294df8bae1dSRodney W. Grimes break; 2295df8bae1dSRodney W. Grimes 2296df8bae1dSRodney W. Grimes /* 2297df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 2298df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 2299df8bae1dSRodney W. Grimes * standard timers. 2300df8bae1dSRodney W. Grimes */ 2301df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2302302ce8d6SAndre Oppermann KASSERT(headlocked == 1, ("%s: dodata: " 2303302ce8d6SAndre Oppermann "TCP_FIN_WAIT_2: head not locked", __func__)); 2304340c35deSJonathan Lemon tcp_twstart(tp); 230511a20fb8SJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2306679d9708SAndre Oppermann return; 2307df8bae1dSRodney W. Grimes } 2308df8bae1dSRodney W. Grimes } 2309e0bef1cbSRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 2310e0bef1cbSRobert Watson headlocked = 0; 2311610ee2f9SDavid Greenman #ifdef TCPDEBUG 23124cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2313fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2314fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2315610ee2f9SDavid Greenman #endif 2316df8bae1dSRodney W. Grimes 2317df8bae1dSRodney W. Grimes /* 2318df8bae1dSRodney W. Grimes * Return any desired output. 2319df8bae1dSRodney W. Grimes */ 2320df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 2321df8bae1dSRodney W. Grimes (void) tcp_output(tp); 23227792ea27SJeffrey Hsu 2323a14c749fSJonathan Lemon check_delack: 2324302ce8d6SAndre Oppermann KASSERT(headlocked == 0, ("%s: check_delack: head locked", 2325302ce8d6SAndre Oppermann __func__)); 23267824d002SAndre Oppermann INP_INFO_UNLOCK_ASSERT(&tcbinfo); 2327302ce8d6SAndre Oppermann INP_LOCK_ASSERT(tp->t_inpcb); 2328a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 23293bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 2330b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 23313bfd6421SJonathan Lemon } 2332302ce8d6SAndre Oppermann INP_UNLOCK(tp->t_inpcb); 2333679d9708SAndre Oppermann return; 2334df8bae1dSRodney W. Grimes 2335df8bae1dSRodney W. Grimes dropafterack: 2336302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: dropafterack: head not locked", __func__)); 2337df8bae1dSRodney W. Grimes /* 2338df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 2339df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 234080ab7c0eSGarrett Wollman * 234180ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 234280ab7c0eSGarrett Wollman * paths to this code happen after packets containing 234380ab7c0eSGarrett Wollman * RST have been dropped. 234480ab7c0eSGarrett Wollman * 234580ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 234680ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 234780ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 234880ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 234980ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 235080ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 2351df8bae1dSRodney W. Grimes */ 2352fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2353fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 2354a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 2355a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2356a57815efSBosko Milekic goto dropwithreset; 2357a57815efSBosko Milekic } 2358a0292f23SGarrett Wollman #ifdef TCPDEBUG 23594cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2360fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2361fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2362a0292f23SGarrett Wollman #endif 2363302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: headlocked should be 1", __func__)); 236442cf3289SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 2365df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2366df8bae1dSRodney W. Grimes (void) tcp_output(tp); 2367302ce8d6SAndre Oppermann INP_UNLOCK(tp->t_inpcb); 2368d6915262SRobert Watson m_freem(m); 2369679d9708SAndre Oppermann return; 2370df8bae1dSRodney W. Grimes 2371df8bae1dSRodney W. Grimes dropwithreset: 2372302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: dropwithreset: head not locked", __func__)); 2373a57815efSBosko Milekic 2374302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 2375c29afad6SSam Leffler 23761c53f806SRobert Watson if (tp != NULL) 2377302ce8d6SAndre Oppermann INP_UNLOCK(tp->t_inpcb); 2378f76fcf6dSJeffrey Hsu if (headlocked) 2379f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2380679d9708SAndre Oppermann return; 2381df8bae1dSRodney W. Grimes 2382df8bae1dSRodney W. Grimes drop: 2383df8bae1dSRodney W. Grimes /* 2384df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 2385df8bae1dSRodney W. Grimes */ 2386610ee2f9SDavid Greenman #ifdef TCPDEBUG 23871c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2388fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2389fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2390a0292f23SGarrett Wollman #endif 23911c53f806SRobert Watson if (tp != NULL) 2392302ce8d6SAndre Oppermann INP_UNLOCK(tp->t_inpcb); 2393f76fcf6dSJeffrey Hsu if (headlocked) 2394f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2395d6915262SRobert Watson m_freem(m); 2396679d9708SAndre Oppermann return; 2397302ce8d6SAndre Oppermann } 2398302ce8d6SAndre Oppermann 2399302ce8d6SAndre Oppermann /* 24000ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 24010ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 24020ca3f933SAndre Oppermann * tp may be NULL. 2403302ce8d6SAndre Oppermann */ 2404302ce8d6SAndre Oppermann static void 2405302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 2406302ce8d6SAndre Oppermann int tlen, int rstreason) 2407302ce8d6SAndre Oppermann { 2408302ce8d6SAndre Oppermann struct ip *ip; 2409302ce8d6SAndre Oppermann #ifdef INET6 2410302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 2411302ce8d6SAndre Oppermann #endif 24120ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 2413302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2414302ce8d6SAndre Oppermann goto drop; 2415302ce8d6SAndre Oppermann #ifdef INET6 2416302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 2417302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 2418302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2419302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2420302ce8d6SAndre Oppermann goto drop; 2421302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 2422302ce8d6SAndre Oppermann } else 2423302ce8d6SAndre Oppermann #endif 2424302ce8d6SAndre Oppermann { 2425302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 2426302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2427302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2428302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 2429302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2430302ce8d6SAndre Oppermann goto drop; 2431302ce8d6SAndre Oppermann } 2432302ce8d6SAndre Oppermann 2433302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 2434302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 2435302ce8d6SAndre Oppermann goto drop; 2436302ce8d6SAndre Oppermann 2437302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 2438302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 2439302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 2440302ce8d6SAndre Oppermann th->th_ack, TH_RST); 2441302ce8d6SAndre Oppermann } else { 2442302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 2443302ce8d6SAndre Oppermann tlen++; 2444302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2445302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 2446302ce8d6SAndre Oppermann } 2447302ce8d6SAndre Oppermann return; 2448302ce8d6SAndre Oppermann drop: 2449302ce8d6SAndre Oppermann m_freem(m); 2450df8bae1dSRodney W. Grimes return; 2451df8bae1dSRodney W. Grimes } 2452df8bae1dSRodney W. Grimes 2453be2ac88cSJonathan Lemon /* 2454be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 2455be2ac88cSJonathan Lemon */ 24560312fbe9SPoul-Henning Kamp static void 2457ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 2458df8bae1dSRodney W. Grimes { 2459df8bae1dSRodney W. Grimes int opt, optlen; 2460df8bae1dSRodney W. Grimes 2461be2ac88cSJonathan Lemon to->to_flags = 0; 2462df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 2463df8bae1dSRodney W. Grimes opt = cp[0]; 2464df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 2465df8bae1dSRodney W. Grimes break; 2466df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 2467df8bae1dSRodney W. Grimes optlen = 1; 2468df8bae1dSRodney W. Grimes else { 2469b474779fSJun-ichiro itojun Hagino if (cnt < 2) 2470b474779fSJun-ichiro itojun Hagino break; 2471df8bae1dSRodney W. Grimes optlen = cp[1]; 2472b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 2473df8bae1dSRodney W. Grimes break; 2474df8bae1dSRodney W. Grimes } 2475df8bae1dSRodney W. Grimes switch (opt) { 2476df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 2477df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 2478df8bae1dSRodney W. Grimes continue; 2479f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2480df8bae1dSRodney W. Grimes continue; 2481be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 2482be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 2483be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 2484fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 2485df8bae1dSRodney W. Grimes break; 2486df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 2487df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 2488df8bae1dSRodney W. Grimes continue; 2489f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2490df8bae1dSRodney W. Grimes continue; 2491be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 249202a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 2493df8bae1dSRodney W. Grimes break; 2494df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 2495df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 2496df8bae1dSRodney W. Grimes continue; 2497be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 2498a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2499a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 2500fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 2501a0292f23SGarrett Wollman bcopy((char *)cp + 6, 2502a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2503fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 2504df8bae1dSRodney W. Grimes break; 25051cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 25061cfd4b53SBruce M Simpson /* 25071cfd4b53SBruce M Simpson * XXX In order to reply to a host which has set the 25081cfd4b53SBruce M Simpson * TCP_SIGNATURE option in its initial SYN, we have to 25091cfd4b53SBruce M Simpson * record the fact that the option was observed here 25101cfd4b53SBruce M Simpson * for the syncache code to perform the correct response. 25111cfd4b53SBruce M Simpson */ 25121cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 25131cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 25141cfd4b53SBruce M Simpson continue; 2515df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 2516df47e437SAndre Oppermann to->to_signature = cp + 2; 25171cfd4b53SBruce M Simpson break; 2518265ed012SBruce M Simpson #endif 25196d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 2520f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 25216d90faf3SPaul Saab continue; 2522f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2523f72167f4SAndre Oppermann continue; 2524f72167f4SAndre Oppermann if (!tcp_do_sack) 2525f72167f4SAndre Oppermann continue; 2526fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 25276d90faf3SPaul Saab break; 25286d90faf3SPaul Saab case TCPOPT_SACK: 25295a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 25306d90faf3SPaul Saab continue; 2531b728e902SAndre Oppermann if (flags & TO_SYN) 2532b728e902SAndre Oppermann continue; 2533fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 25345a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 25355a53ca16SPaul Saab to->to_sacks = cp + 2; 25365a53ca16SPaul Saab tcpstat.tcps_sack_rcv_blocks++; 25376d90faf3SPaul Saab break; 2538be2ac88cSJonathan Lemon default: 2539be2ac88cSJonathan Lemon continue; 2540df8bae1dSRodney W. Grimes } 2541df8bae1dSRodney W. Grimes } 2542df8bae1dSRodney W. Grimes } 2543df8bae1dSRodney W. Grimes 2544df8bae1dSRodney W. Grimes /* 2545df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 2546df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 2547df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 2548df8bae1dSRodney W. Grimes * sequencing purposes. 2549df8bae1dSRodney W. Grimes */ 25500312fbe9SPoul-Henning Kamp static void 2551ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 2552ad3f9ab3SAndre Oppermann int off) 2553df8bae1dSRodney W. Grimes { 2554fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 2555df8bae1dSRodney W. Grimes 2556df8bae1dSRodney W. Grimes while (cnt >= 0) { 2557df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 2558df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 2559df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 2560df8bae1dSRodney W. Grimes 2561df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 2562df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 2563df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2564df8bae1dSRodney W. Grimes m->m_len--; 256569a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 256669a34685SYoshinobu Inoue m->m_pkthdr.len--; 2567df8bae1dSRodney W. Grimes return; 2568df8bae1dSRodney W. Grimes } 2569df8bae1dSRodney W. Grimes cnt -= m->m_len; 2570df8bae1dSRodney W. Grimes m = m->m_next; 25714dfdffe9SAndre Oppermann if (m == NULL) 2572df8bae1dSRodney W. Grimes break; 2573df8bae1dSRodney W. Grimes } 2574df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 2575df8bae1dSRodney W. Grimes } 2576df8bae1dSRodney W. Grimes 2577df8bae1dSRodney W. Grimes /* 2578df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 2579df8bae1dSRodney W. Grimes * and update averages and current timeout. 2580df8bae1dSRodney W. Grimes */ 25810312fbe9SPoul-Henning Kamp static void 2582ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 2583df8bae1dSRodney W. Grimes { 2584ad3f9ab3SAndre Oppermann int delta; 2585233e8c18SGarrett Wollman 25862be3bf22SRobert Watson INP_LOCK_ASSERT(tp->t_inpcb); 25872be3bf22SRobert Watson 2588233e8c18SGarrett Wollman tcpstat.tcps_rttupdated++; 2589233e8c18SGarrett Wollman tp->t_rttupdated++; 2590233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 2591233e8c18SGarrett Wollman /* 2592233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 2593233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 2594233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 2595233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2596233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 2597233e8c18SGarrett Wollman */ 2598233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2599233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2600233e8c18SGarrett Wollman 2601233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 2602233e8c18SGarrett Wollman tp->t_srtt = 1; 2603233e8c18SGarrett Wollman 2604233e8c18SGarrett Wollman /* 2605233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 2606233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 2607233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 2608233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 2609233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 2610233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 2611233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2612233e8c18SGarrett Wollman * rfc793's wired-in beta. 2613233e8c18SGarrett Wollman */ 2614233e8c18SGarrett Wollman if (delta < 0) 2615233e8c18SGarrett Wollman delta = -delta; 2616233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2617233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 2618233e8c18SGarrett Wollman tp->t_rttvar = 1; 26191fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 26201fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2621233e8c18SGarrett Wollman } else { 2622233e8c18SGarrett Wollman /* 2623233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 2624233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 2625233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 2626233e8c18SGarrett Wollman */ 2627233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 2628233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 26291fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2630233e8c18SGarrett Wollman } 26319b8b58e0SJonathan Lemon tp->t_rtttime = 0; 2632df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 2633df8bae1dSRodney W. Grimes 2634df8bae1dSRodney W. Grimes /* 2635df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 2636df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 2637df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 2638df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 2639df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 2640df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 2641df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 2642df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 2643df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 2644df8bae1dSRodney W. Grimes */ 2645233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 26469e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2647df8bae1dSRodney W. Grimes 2648df8bae1dSRodney W. Grimes /* 2649df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 2650df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 2651df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 2652df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 2653df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 2654df8bae1dSRodney W. Grimes */ 2655df8bae1dSRodney W. Grimes tp->t_softerror = 0; 2656df8bae1dSRodney W. Grimes } 2657df8bae1dSRodney W. Grimes 2658df8bae1dSRodney W. Grimes /* 2659df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 2660df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 2661df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 2662df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 2663df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2664df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 2665df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 2666df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 2667df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 2668df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 2669df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 2670df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 2671df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 2672a0292f23SGarrett Wollman * 2673a0292f23SGarrett Wollman * Also take into account the space needed for options that we 2674a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 2675a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 2676a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 2677a0292f23SGarrett Wollman * data in maxopd. 2678a0292f23SGarrett Wollman * 2679a0292f23SGarrett Wollman * 2680a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 2681a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 2682a0292f23SGarrett Wollman * MSS of our peer. 268397d8d152SAndre Oppermann * 268497d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 268597d8d152SAndre Oppermann * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt(). 2686df8bae1dSRodney W. Grimes */ 2687a0292f23SGarrett Wollman void 2688ad3f9ab3SAndre Oppermann tcp_mss(struct tcpcb *tp, int offer) 2689df8bae1dSRodney W. Grimes { 269097d8d152SAndre Oppermann int rtt, mss; 2691df8bae1dSRodney W. Grimes u_long bufsize; 269297d8d152SAndre Oppermann u_long maxmtu; 2693c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 2694df8bae1dSRodney W. Grimes struct socket *so; 269597d8d152SAndre Oppermann struct hc_metrics_lite metrics; 2696a0292f23SGarrett Wollman int origoffer = offer; 2697233dcce1SAndre Oppermann int mtuflags = 0; 2698fb59c426SYoshinobu Inoue #ifdef INET6 2699c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2700c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 2701c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 2702c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 2703c068736aSJeffrey Hsu #else 2704c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 2705fb59c426SYoshinobu Inoue #endif 2706df8bae1dSRodney W. Grimes 270797d8d152SAndre Oppermann /* initialize */ 270897d8d152SAndre Oppermann #ifdef INET6 270997d8d152SAndre Oppermann if (isipv6) { 2710233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(&inp->inp_inc, &mtuflags); 271197d8d152SAndre Oppermann tp->t_maxopd = tp->t_maxseg = tcp_v6mssdflt; 271297d8d152SAndre Oppermann } else 271397d8d152SAndre Oppermann #endif 271497d8d152SAndre Oppermann { 2715233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(&inp->inp_inc, &mtuflags); 271697d8d152SAndre Oppermann tp->t_maxopd = tp->t_maxseg = tcp_mssdflt; 2717df8bae1dSRodney W. Grimes } 2718df8bae1dSRodney W. Grimes so = inp->inp_socket; 2719df8bae1dSRodney W. Grimes 272097d8d152SAndre Oppermann /* 27212d166c02SAndre Oppermann * no route to sender, stay with default mss and return 272297d8d152SAndre Oppermann */ 272397d8d152SAndre Oppermann if (maxmtu == 0) 272497d8d152SAndre Oppermann return; 272597d8d152SAndre Oppermann 272697d8d152SAndre Oppermann /* what have we got? */ 272797d8d152SAndre Oppermann switch (offer) { 272897d8d152SAndre Oppermann case 0: 272997d8d152SAndre Oppermann /* 273097d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 273197d8d152SAndre Oppermann * segment, in this case we use tcp_mssdflt. 273297d8d152SAndre Oppermann */ 273397d8d152SAndre Oppermann offer = 273497d8d152SAndre Oppermann #ifdef INET6 273597d8d152SAndre Oppermann isipv6 ? tcp_v6mssdflt : 273697d8d152SAndre Oppermann #endif 273797d8d152SAndre Oppermann tcp_mssdflt; 273897d8d152SAndre Oppermann break; 273997d8d152SAndre Oppermann 274097d8d152SAndre Oppermann case -1: 2741a0292f23SGarrett Wollman /* 2742c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 2743a0292f23SGarrett Wollman */ 274497d8d152SAndre Oppermann /* FALLTHROUGH */ 274597d8d152SAndre Oppermann 274697d8d152SAndre Oppermann default: 2747a0292f23SGarrett Wollman /* 274853369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 274953369ac9SAndre Oppermann * to at least minmss. 275053369ac9SAndre Oppermann */ 275153369ac9SAndre Oppermann offer = max(offer, tcp_minmss); 275253369ac9SAndre Oppermann /* 2753a0292f23SGarrett Wollman * Sanity check: make sure that maxopd will be large 275497d8d152SAndre Oppermann * enough to allow some data on segments even if the 2755a0292f23SGarrett Wollman * all the option space is used (40bytes). Otherwise 2756a0292f23SGarrett Wollman * funny things may happen in tcp_output. 2757a0292f23SGarrett Wollman */ 2758a0292f23SGarrett Wollman offer = max(offer, 64); 275997d8d152SAndre Oppermann } 2760a0292f23SGarrett Wollman 2761df8bae1dSRodney W. Grimes /* 276297d8d152SAndre Oppermann * rmx information is now retrieved from tcp_hostcache 2763df8bae1dSRodney W. Grimes */ 276497d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 276597d8d152SAndre Oppermann 2766df8bae1dSRodney W. Grimes /* 276797d8d152SAndre Oppermann * if there's a discovered mtu int tcp hostcache, use it 2768fb59c426SYoshinobu Inoue * else, use the link mtu. 2769df8bae1dSRodney W. Grimes */ 277097d8d152SAndre Oppermann if (metrics.rmx_mtu) 27712d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 2772c068736aSJeffrey Hsu else { 277331b3783cSHajimu UMEMOTO #ifdef INET6 2774fb59c426SYoshinobu Inoue if (isipv6) { 277597d8d152SAndre Oppermann mss = maxmtu - min_protoh; 277697d8d152SAndre Oppermann if (!path_mtu_discovery && 277797d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 2778fb59c426SYoshinobu Inoue mss = min(mss, tcp_v6mssdflt); 2779b3399803SHajimu UMEMOTO } else 2780b3399803SHajimu UMEMOTO #endif 278197d8d152SAndre Oppermann { 278297d8d152SAndre Oppermann mss = maxmtu - min_protoh; 278397d8d152SAndre Oppermann if (!path_mtu_discovery && 278497d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 2785a0292f23SGarrett Wollman mss = min(mss, tcp_mssdflt); 2786a0292f23SGarrett Wollman } 278797d8d152SAndre Oppermann } 2788a0292f23SGarrett Wollman mss = min(mss, offer); 278997d8d152SAndre Oppermann 2790a0292f23SGarrett Wollman /* 2791a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 2792a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 2793a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 2794a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 2795a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 2796a0292f23SGarrett Wollman */ 2797a0292f23SGarrett Wollman tp->t_maxopd = mss; 2798a0292f23SGarrett Wollman 2799a0292f23SGarrett Wollman /* 2800c94c54e4SAndre Oppermann * origoffer==-1 indicates, that no segments were received yet. 2801c94c54e4SAndre Oppermann * In this case we just guess. 2802a0292f23SGarrett Wollman */ 2803a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2804a0292f23SGarrett Wollman (origoffer == -1 || 2805a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2806a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 280797d8d152SAndre Oppermann tp->t_maxseg = mss; 2808a0292f23SGarrett Wollman 2809df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 2810df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2811df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 2812df8bae1dSRodney W. Grimes #else 2813df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2814df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 2815df8bae1dSRodney W. Grimes #endif 281697d8d152SAndre Oppermann tp->t_maxseg = mss; 281797d8d152SAndre Oppermann 2818df8bae1dSRodney W. Grimes /* 281997d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 282097d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 282197d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 282297d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 282397d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 2824df8bae1dSRodney W. Grimes */ 28253f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 282697d8d152SAndre Oppermann if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe) 282797d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 282897d8d152SAndre Oppermann else 2829df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 2830df8bae1dSRodney W. Grimes if (bufsize < mss) 2831df8bae1dSRodney W. Grimes mss = bufsize; 2832df8bae1dSRodney W. Grimes else { 2833df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2834df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2835df8bae1dSRodney W. Grimes bufsize = sb_max; 283688c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 28373f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 2838df8bae1dSRodney W. Grimes } 28393f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 2840df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 2841df8bae1dSRodney W. Grimes 28423f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 284397d8d152SAndre Oppermann if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe) 284497d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 284597d8d152SAndre Oppermann else 2846df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 2847df8bae1dSRodney W. Grimes if (bufsize > mss) { 2848df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2849df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2850df8bae1dSRodney W. Grimes bufsize = sb_max; 285188c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 28523f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 2853df8bae1dSRodney W. Grimes } 28543f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2855a0292f23SGarrett Wollman /* 285697d8d152SAndre Oppermann * While we're here, check the others too 2857a0292f23SGarrett Wollman */ 285897d8d152SAndre Oppermann if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 285997d8d152SAndre Oppermann tp->t_srtt = rtt; 286097d8d152SAndre Oppermann tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 286197d8d152SAndre Oppermann tcpstat.tcps_usedrtt++; 286297d8d152SAndre Oppermann if (metrics.rmx_rttvar) { 286397d8d152SAndre Oppermann tp->t_rttvar = metrics.rmx_rttvar; 286497d8d152SAndre Oppermann tcpstat.tcps_usedrttvar++; 286597d8d152SAndre Oppermann } else { 286697d8d152SAndre Oppermann /* default variation is +- 1 rtt */ 286797d8d152SAndre Oppermann tp->t_rttvar = 286897d8d152SAndre Oppermann tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 286997d8d152SAndre Oppermann } 287097d8d152SAndre Oppermann TCPT_RANGESET(tp->t_rxtcur, 287197d8d152SAndre Oppermann ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 287297d8d152SAndre Oppermann tp->t_rttmin, TCPTV_REXMTMAX); 287397d8d152SAndre Oppermann } 287497d8d152SAndre Oppermann if (metrics.rmx_ssthresh) { 2875df8bae1dSRodney W. Grimes /* 2876df8bae1dSRodney W. Grimes * There's some sort of gateway or interface 2877df8bae1dSRodney W. Grimes * buffer limit on the path. Use this to set 2878df8bae1dSRodney W. Grimes * the slow start threshhold, but set the 2879df8bae1dSRodney W. Grimes * threshold to no less than 2*mss. 2880df8bae1dSRodney W. Grimes */ 288197d8d152SAndre Oppermann tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh); 2882dd224982SGarrett Wollman tcpstat.tcps_usedssthresh++; 2883df8bae1dSRodney W. Grimes } 288497d8d152SAndre Oppermann if (metrics.rmx_bandwidth) 288597d8d152SAndre Oppermann tp->snd_bandwidth = metrics.rmx_bandwidth; 288697d8d152SAndre Oppermann 288797d8d152SAndre Oppermann /* 288897d8d152SAndre Oppermann * Set the slow-start flight size depending on whether this 288997d8d152SAndre Oppermann * is a local network or not. 289097d8d152SAndre Oppermann * 289197d8d152SAndre Oppermann * Extend this so we cache the cwnd too and retrieve it here. 289297d8d152SAndre Oppermann * Make cwnd even bigger than RFC3390 suggests but only if we 289397d8d152SAndre Oppermann * have previous experience with the remote host. Be careful 289497d8d152SAndre Oppermann * not make cwnd bigger than remote receive window or our own 289597d8d152SAndre Oppermann * send socket buffer. Maybe put some additional upper bound 289697d8d152SAndre Oppermann * on the retrieved cwnd. Should do incremental updates to 289797d8d152SAndre Oppermann * hostcache when cwnd collapses so next connection doesn't 289897d8d152SAndre Oppermann * overloads the path again. 289997d8d152SAndre Oppermann * 290097d8d152SAndre Oppermann * RFC3390 says only do this if SYN or SYN/ACK didn't got lost. 290197d8d152SAndre Oppermann * We currently check only in syncache_socket for that. 290297d8d152SAndre Oppermann */ 290397d8d152SAndre Oppermann #define TCP_METRICS_CWND 290497d8d152SAndre Oppermann #ifdef TCP_METRICS_CWND 290597d8d152SAndre Oppermann if (metrics.rmx_cwnd) 290697d8d152SAndre Oppermann tp->snd_cwnd = max(mss, 290797d8d152SAndre Oppermann min(metrics.rmx_cwnd / 2, 290897d8d152SAndre Oppermann min(tp->snd_wnd, so->so_snd.sb_hiwat))); 290997d8d152SAndre Oppermann else 291097d8d152SAndre Oppermann #endif 291197d8d152SAndre Oppermann if (tcp_do_rfc3390) 291297d8d152SAndre Oppermann tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380)); 291397d8d152SAndre Oppermann #ifdef INET6 291497d8d152SAndre Oppermann else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) || 291597d8d152SAndre Oppermann (!isipv6 && in_localaddr(inp->inp_faddr))) 2916943ae302SAndre Oppermann #else 2917943ae302SAndre Oppermann else if (in_localaddr(inp->inp_faddr)) 291897d8d152SAndre Oppermann #endif 2919943ae302SAndre Oppermann tp->snd_cwnd = mss * ss_fltsz_local; 292097d8d152SAndre Oppermann else 292197d8d152SAndre Oppermann tp->snd_cwnd = mss * ss_fltsz; 2922233dcce1SAndre Oppermann 2923233dcce1SAndre Oppermann /* Check the interface for TSO capabilities. */ 2924233dcce1SAndre Oppermann if (mtuflags & CSUM_TSO) 2925233dcce1SAndre Oppermann tp->t_flags |= TF_TSO; 2926a0292f23SGarrett Wollman } 2927a0292f23SGarrett Wollman 2928a0292f23SGarrett Wollman /* 2929a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 2930a0292f23SGarrett Wollman */ 2931a0292f23SGarrett Wollman int 2932ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 2933a0292f23SGarrett Wollman { 293497d8d152SAndre Oppermann int mss = 0; 293597d8d152SAndre Oppermann u_long maxmtu = 0; 293697d8d152SAndre Oppermann u_long thcmtu = 0; 293797d8d152SAndre Oppermann size_t min_protoh; 2938fb59c426SYoshinobu Inoue #ifdef INET6 293997d8d152SAndre Oppermann int isipv6 = inc->inc_isipv6 ? 1 : 0; 2940fb59c426SYoshinobu Inoue #endif 2941a0292f23SGarrett Wollman 294297d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 2943a0292f23SGarrett Wollman 294431b3783cSHajimu UMEMOTO #ifdef INET6 294597d8d152SAndre Oppermann if (isipv6) { 294697d8d152SAndre Oppermann mss = tcp_v6mssdflt; 2947233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 294897d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 294997d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 295097d8d152SAndre Oppermann } else 295131b3783cSHajimu UMEMOTO #endif 295297d8d152SAndre Oppermann { 295397d8d152SAndre Oppermann mss = tcp_mssdflt; 2954233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 295597d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 295697d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 295797d8d152SAndre Oppermann } 295897d8d152SAndre Oppermann if (maxmtu && thcmtu) 295997d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 296097d8d152SAndre Oppermann else if (maxmtu || thcmtu) 296197d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 296297d8d152SAndre Oppermann 296397d8d152SAndre Oppermann return (mss); 2964df8bae1dSRodney W. Grimes } 296546f58482SJonathan Lemon 296646f58482SJonathan Lemon 296746f58482SJonathan Lemon /* 2968c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 2969c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 2970c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 2971c068736aSJeffrey Hsu * be started again. 297246f58482SJonathan Lemon */ 2973c068736aSJeffrey Hsu static void 2974ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 297546f58482SJonathan Lemon { 297646f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 297746f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 297846f58482SJonathan Lemon 2979b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 298046f58482SJonathan Lemon tp->t_rtttime = 0; 298146f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 29826b2a5f92SJayanth Vijayaraghavan /* 2983c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 2984c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 29856b2a5f92SJayanth Vijayaraghavan */ 29866b2a5f92SJayanth Vijayaraghavan tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 2987a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 29886b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 298946f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 299046f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 299146f58482SJonathan Lemon tp->snd_nxt = onxt; 299246f58482SJonathan Lemon /* 299346f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 299446f58482SJonathan Lemon * not updated yet. 299546f58482SJonathan Lemon */ 2996d7587117SPaul Saab if (tp->snd_cwnd > th->th_ack - tp->snd_una) 2997d7587117SPaul Saab tp->snd_cwnd -= th->th_ack - tp->snd_una; 2998d7587117SPaul Saab else 2999d7587117SPaul Saab tp->snd_cwnd = 0; 3000d7587117SPaul Saab tp->snd_cwnd += tp->t_maxseg; 300146f58482SJonathan Lemon } 3002