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 244c068736aSJeffrey Hsu const int isipv6 = 0; 245c068736aSJeffrey Hsu #endif 246302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 247f76fcf6dSJeffrey Hsu 248610ee2f9SDavid Greenman #ifdef TCPDEBUG 249c068736aSJeffrey Hsu /* 250c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 251c068736aSJeffrey Hsu * now IPv6. 252c068736aSJeffrey Hsu */ 25314739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 254410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 255610ee2f9SDavid Greenman short ostate = 0; 256610ee2f9SDavid Greenman #endif 257c068736aSJeffrey Hsu 258fb59c426SYoshinobu Inoue #ifdef INET6 259fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 260fb59c426SYoshinobu Inoue #endif 261a0292f23SGarrett Wollman 262302ce8d6SAndre Oppermann to.to_flags = 0; 263df8bae1dSRodney W. Grimes tcpstat.tcps_rcvtotal++; 264fb59c426SYoshinobu Inoue 265fb59c426SYoshinobu Inoue if (isipv6) { 26604d3a452SHajimu UMEMOTO #ifdef INET6 267fb59c426SYoshinobu Inoue /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */ 268fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 269fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 270fb59c426SYoshinobu Inoue if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 271fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbadsum++; 272fb59c426SYoshinobu Inoue goto drop; 273fb59c426SYoshinobu Inoue } 274fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 27533841545SHajimu UMEMOTO 27633841545SHajimu UMEMOTO /* 27733841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 27833841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 27933841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 28033841545SHajimu UMEMOTO * 28133841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 28233841545SHajimu UMEMOTO * already dropped in ip6_input. 28333841545SHajimu UMEMOTO */ 28433841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 28533841545SHajimu UMEMOTO /* XXX stat */ 28633841545SHajimu UMEMOTO goto drop; 28733841545SHajimu UMEMOTO } 28804d3a452SHajimu UMEMOTO #else 28904d3a452SHajimu UMEMOTO th = NULL; /* XXX: avoid compiler warning */ 29004d3a452SHajimu UMEMOTO #endif 291c068736aSJeffrey Hsu } else { 292df8bae1dSRodney W. Grimes /* 293df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 294df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 295df8bae1dSRodney W. Grimes */ 296fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 297df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 298fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 299fb59c426SYoshinobu Inoue } 300df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 3014dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 3024dfdffe9SAndre Oppermann == NULL) { 303df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 304df8bae1dSRodney W. Grimes return; 305df8bae1dSRodney W. Grimes } 306df8bae1dSRodney W. Grimes } 307fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 308fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 309db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 310db4f9cc7SJonathan Lemon tlen = ip->ip_len; 311df8bae1dSRodney W. Grimes 312db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 313db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 314db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 315db4f9cc7SJonathan Lemon else 316db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 317c068736aSJeffrey Hsu ip->ip_dst.s_addr, 318c068736aSJeffrey Hsu htonl(m->m_pkthdr.csum_data + 319c068736aSJeffrey Hsu ip->ip_len + 320c068736aSJeffrey Hsu IPPROTO_TCP)); 321db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 3223c653157SHartmut Brandt #ifdef TCPDEBUG 3233c653157SHartmut Brandt ipov->ih_len = (u_short)tlen; 3243c653157SHartmut Brandt ipov->ih_len = htons(ipov->ih_len); 3253c653157SHartmut Brandt #endif 326db4f9cc7SJonathan Lemon } else { 327df8bae1dSRodney W. Grimes /* 328df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 329df8bae1dSRodney W. Grimes */ 330df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 331fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 332fb59c426SYoshinobu Inoue ipov->ih_len = (u_short)tlen; 333fd8e4ebcSMike Barcroft ipov->ih_len = htons(ipov->ih_len); 334fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 335db4f9cc7SJonathan Lemon } 336fb59c426SYoshinobu Inoue if (th->th_sum) { 337df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadsum++; 338df8bae1dSRodney W. Grimes goto drop; 339df8bae1dSRodney W. Grimes } 340fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 341fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 342fb59c426SYoshinobu Inoue } 343df8bae1dSRodney W. Grimes 344df8bae1dSRodney W. Grimes /* 345df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 346df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 347df8bae1dSRodney W. Grimes */ 348fb59c426SYoshinobu Inoue off = th->th_off << 2; 349df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 350df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadoff++; 351df8bae1dSRodney W. Grimes goto drop; 352df8bae1dSRodney W. Grimes } 353fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 354df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 355fb59c426SYoshinobu Inoue if (isipv6) { 35604d3a452SHajimu UMEMOTO #ifdef INET6 357fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 358fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 359fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 36004d3a452SHajimu UMEMOTO #endif 361c068736aSJeffrey Hsu } else { 362df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 363c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 3644dfdffe9SAndre Oppermann == NULL) { 365df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 366df8bae1dSRodney W. Grimes return; 367df8bae1dSRodney W. Grimes } 368fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 369fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 370fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 371fb59c426SYoshinobu Inoue } 372df8bae1dSRodney W. Grimes } 373df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 374fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 375df8bae1dSRodney W. Grimes } 376fb59c426SYoshinobu Inoue thflags = th->th_flags; 377df8bae1dSRodney W. Grimes 378e46cd3d4SDag-Erling Smørgrav /* 379e46cd3d4SDag-Erling Smørgrav * If the drop_synfin option is enabled, drop all packets with 380e46cd3d4SDag-Erling Smørgrav * both the SYN and FIN bits set. This prevents e.g. nmap from 381e46cd3d4SDag-Erling Smørgrav * identifying the TCP/IP stack. 382e46cd3d4SDag-Erling Smørgrav * 383a589a70eSGarrett Wollman * This is a violation of the TCP specification. 384e46cd3d4SDag-Erling Smørgrav */ 385fb59c426SYoshinobu Inoue if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN)) 386e46cd3d4SDag-Erling Smørgrav goto drop; 387e46cd3d4SDag-Erling Smørgrav 388df8bae1dSRodney W. Grimes /* 389df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 390df8bae1dSRodney W. Grimes */ 391fd8e4ebcSMike Barcroft th->th_seq = ntohl(th->th_seq); 392fd8e4ebcSMike Barcroft th->th_ack = ntohl(th->th_ack); 393fd8e4ebcSMike Barcroft th->th_win = ntohs(th->th_win); 394fd8e4ebcSMike Barcroft th->th_urp = ntohs(th->th_urp); 395df8bae1dSRodney W. Grimes 396df8bae1dSRodney W. Grimes /* 397794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 398755c1f07SAndras Olah */ 399fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 400755c1f07SAndras Olah 401755c1f07SAndras Olah /* 402df8bae1dSRodney W. Grimes * Locate pcb for segment. 403df8bae1dSRodney W. Grimes */ 404f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 405df8bae1dSRodney W. Grimes findpcb: 406302ce8d6SAndre Oppermann INP_INFO_WLOCK_ASSERT(&tcbinfo); 4079b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 4089b932e9eSAndre Oppermann /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */ 4099b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 4109b932e9eSAndre Oppermann 4119b932e9eSAndre Oppermann if (fwd_tag != NULL && isipv6 == 0) { /* IPv6 support is not yet */ 4129b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 4139b932e9eSAndre Oppermann 4149b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 415f9e354dfSJulian Elischer /* 4162b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 417f9e354dfSJulian Elischer * already got one like this? 418f9e354dfSJulian Elischer */ 4199b932e9eSAndre Oppermann inp = in_pcblookup_hash(&tcbinfo, 4209b932e9eSAndre Oppermann ip->ip_src, th->th_sport, 421c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 422c068736aSJeffrey Hsu 0, m->m_pkthdr.rcvif); 423f9e354dfSJulian Elischer if (!inp) { 4249b932e9eSAndre Oppermann /* It's new. Try to find the ambushing socket. */ 425f9e354dfSJulian Elischer inp = in_pcblookup_hash(&tcbinfo, 426fb59c426SYoshinobu Inoue ip->ip_src, th->th_sport, 4272b25acc1SLuigi Rizzo next_hop->sin_addr, 428c068736aSJeffrey Hsu next_hop->sin_port ? 429c068736aSJeffrey Hsu ntohs(next_hop->sin_port) : 430c068736aSJeffrey Hsu th->th_dport, 431421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 432421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 433f9e354dfSJulian Elischer } 4349b932e9eSAndre Oppermann /* Remove the tag from the packet. We don't need it anymore. */ 4359b932e9eSAndre Oppermann m_tag_delete(m, fwd_tag); 436b10fbdeaSAndre Oppermann } else 4379b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 438b10fbdeaSAndre Oppermann { 43904d3a452SHajimu UMEMOTO if (isipv6) { 44004d3a452SHajimu UMEMOTO #ifdef INET6 441c068736aSJeffrey Hsu inp = in6_pcblookup_hash(&tcbinfo, 442c068736aSJeffrey Hsu &ip6->ip6_src, th->th_sport, 443c068736aSJeffrey Hsu &ip6->ip6_dst, th->th_dport, 444421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 445421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 44604d3a452SHajimu UMEMOTO #endif 44704d3a452SHajimu UMEMOTO } else 448c068736aSJeffrey Hsu inp = in_pcblookup_hash(&tcbinfo, 449c068736aSJeffrey Hsu ip->ip_src, th->th_sport, 450c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 451421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 452421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 453fb59c426SYoshinobu Inoue } 454f9e354dfSJulian Elischer 455da0f4099SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC) 456d420fcdaSBruce M Simpson #ifdef INET6 4574dfdffe9SAndre Oppermann if (isipv6 && inp != NULL && ipsec6_in_reject(m, inp)) { 458fb59c426SYoshinobu Inoue #ifdef IPSEC 459fb59c426SYoshinobu Inoue ipsec6stat.in_polvio++; 460d420fcdaSBruce M Simpson #endif 461302ce8d6SAndre Oppermann goto dropunlock; 462d420fcdaSBruce M Simpson } else 463d420fcdaSBruce M Simpson #endif /* INET6 */ 464d420fcdaSBruce M Simpson if (inp != NULL && ipsec4_in_reject(m, inp)) { 465da0f4099SHajimu UMEMOTO #ifdef IPSEC 466fb59c426SYoshinobu Inoue ipsecstat.in_polvio++; 467d420fcdaSBruce M Simpson #endif 468302ce8d6SAndre Oppermann goto dropunlock; 469fb59c426SYoshinobu Inoue } 470da0f4099SHajimu UMEMOTO #endif /*IPSEC || FAST_IPSEC*/ 471df8bae1dSRodney W. Grimes 472df8bae1dSRodney W. Grimes /* 473574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 474574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 475df8bae1dSRodney W. Grimes */ 476816a3d83SPoul-Henning Kamp if (inp == NULL) { 477574b6964SAndre Oppermann /* 478574b6964SAndre Oppermann * Log communication attempts to ports that are not 479574b6964SAndre Oppermann * in use. 480574b6964SAndre Oppermann */ 481574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 482574b6964SAndre Oppermann tcp_log_in_vain == 2) { 483df541e5fSAndre Oppermann char *s; 484df541e5fSAndre Oppermann #ifdef INET6 485df541e5fSAndre Oppermann s = tcp_log_addrs(NULL, th, (void *)ip, (void *)ip6); 486574b6964SAndre Oppermann #else 487df541e5fSAndre Oppermann s = tcp_log_addrs(NULL, th, (void *)ip, NULL); 488574b6964SAndre Oppermann #endif /* INET6 */ 489df541e5fSAndre Oppermann if (s != NULL) { 490df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 491df541e5fSAndre Oppermann "to closed port\n", s, __func__); 492df541e5fSAndre Oppermann free(s, M_TCPLOG); 493fb59c426SYoshinobu Inoue } 4942e4e1b4cSGeoff Rehmet } 495574b6964SAndre Oppermann /* 496574b6964SAndre Oppermann * When blackholing do not respond with a RST but 497574b6964SAndre Oppermann * completely ignore the segment and drop it. 498574b6964SAndre Oppermann */ 499574b6964SAndre Oppermann if ((blackhole == 1 && (thflags & TH_SYN)) || 500574b6964SAndre Oppermann blackhole == 2) 5011929eae1SAndre Oppermann goto dropunlock; 502574b6964SAndre Oppermann 503a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 504a57815efSBosko Milekic goto dropwithreset; 505816a3d83SPoul-Henning Kamp } 506f76fcf6dSJeffrey Hsu INP_LOCK(inp); 507936cd18dSAndre Oppermann 508936cd18dSAndre Oppermann /* Check the minimum TTL for socket. */ 50934f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 51034f83c52SGeorge V. Neville-Neil #ifdef INET6 51134f83c52SGeorge V. Neville-Neil if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim) 512302ce8d6SAndre Oppermann goto dropunlock; 51302707462SAndre Oppermann else 51434f83c52SGeorge V. Neville-Neil #endif 51534f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 516302ce8d6SAndre Oppermann goto dropunlock; 51734f83c52SGeorge V. Neville-Neil } 518936cd18dSAndre Oppermann 519340c35deSJonathan Lemon /* 520794235b7SAndre Oppermann * A previous connection in TIMEWAIT state is supposed to catch 521794235b7SAndre Oppermann * stray or duplicate segments arriving late. If this segment 522794235b7SAndre Oppermann * was a legitimate new connection attempt the old INPCB gets 523794235b7SAndre Oppermann * removed and we can try again to find a listening socket. 524340c35deSJonathan Lemon */ 525794235b7SAndre Oppermann if (inp->inp_vflag & INP_TIMEWAIT) { 526340c35deSJonathan Lemon if (thflags & TH_SYN) 527f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 5282104448fSAndre Oppermann /* NB: tcp_twcheck unlocks the INP and frees the mbuf. */ 5292104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 530340c35deSJonathan Lemon goto findpcb; 531340c35deSJonathan Lemon INP_INFO_WUNLOCK(&tcbinfo); 532340c35deSJonathan Lemon return; 533340c35deSJonathan Lemon } 534794235b7SAndre Oppermann /* 535794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 536794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 537794235b7SAndre Oppermann * segment and send an appropriate response. 538794235b7SAndre Oppermann */ 539df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 5404dfdffe9SAndre Oppermann if (tp == NULL) { 541a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 542a57815efSBosko Milekic goto dropwithreset; 54309f81a46SBosko Milekic } 544df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_CLOSED) 545302ce8d6SAndre Oppermann goto dropunlock; /* XXX: dropwithreset??? */ 546df8bae1dSRodney W. Grimes 547c488362eSRobert Watson #ifdef MAC 5481f82efb3SRobert Watson INP_LOCK_ASSERT(inp); 549a557af22SRobert Watson if (mac_check_inpcb_deliver(inp, m)) 550302ce8d6SAndre Oppermann goto dropunlock; 551c488362eSRobert Watson #endif 552a557af22SRobert Watson so = inp->inp_socket; 553302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 554610ee2f9SDavid Greenman #ifdef TCPDEBUG 555df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 556df8bae1dSRodney W. Grimes ostate = tp->t_state; 557d30d90dcSMaxim Konovalov if (isipv6) { 55810fe523eSMaxim Konovalov #ifdef INET6 559540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 560d30d90dcSMaxim Konovalov #endif 561d30d90dcSMaxim Konovalov } else 562540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 563fb59c426SYoshinobu Inoue tcp_savetcp = *th; 564df8bae1dSRodney W. Grimes } 565610ee2f9SDavid Greenman #endif 566db33b3e6SAndre Oppermann /* 567db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 568db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 569db33b3e6SAndre Oppermann * attempt or the completion of a previous one. 570db33b3e6SAndre Oppermann */ 571540e8b7eSJeffrey Hsu if (so->so_options & SO_ACCEPTCONN) { 572540e8b7eSJeffrey Hsu struct in_conninfo inc; 573540e8b7eSJeffrey Hsu 5747824d002SAndre Oppermann KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but " 5757824d002SAndre Oppermann "tp not listening", __func__)); 5767824d002SAndre Oppermann 5778bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 578be2ac88cSJonathan Lemon inc.inc_isipv6 = isipv6; 579302ce8d6SAndre Oppermann #ifdef INET6 580be2ac88cSJonathan Lemon if (isipv6) { 581be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 582be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 583302ce8d6SAndre Oppermann } else 584302ce8d6SAndre Oppermann #endif 585302ce8d6SAndre Oppermann { 586be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 587be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 588be2ac88cSJonathan Lemon } 589be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 590be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 591be2ac88cSJonathan Lemon 592fb59c426SYoshinobu Inoue /* 593be2ac88cSJonathan Lemon * If the state is LISTEN then ignore segment if it contains 594be2ac88cSJonathan Lemon * a RST. If the segment contains an ACK then it is bad and 595be2ac88cSJonathan Lemon * send a RST. If it does not contain a SYN then it is not 596be2ac88cSJonathan Lemon * interesting; drop it. 597be2ac88cSJonathan Lemon * 598be2ac88cSJonathan Lemon * If the state is SYN_RECEIVED (syncache) and seg contains 599be2ac88cSJonathan Lemon * an ACK, but not for our SYN/ACK, send a RST. If the seg 600be2ac88cSJonathan Lemon * contains a RST, check the sequence number to see if it 601be2ac88cSJonathan Lemon * is a valid reset segment. 602fb59c426SYoshinobu Inoue */ 603fb59c426SYoshinobu Inoue if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 604be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 605bf6d304aSAndre Oppermann /* 606bf6d304aSAndre Oppermann * Parse the TCP options here because 607bf6d304aSAndre Oppermann * syncookies need access to the reflected 608bf6d304aSAndre Oppermann * timestamp. 609bf6d304aSAndre Oppermann */ 610bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 6119fa198beSAndre Oppermann /* 6129fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 6139fa198beSAndre Oppermann * inp and tcpinfo locks. 6149fa198beSAndre Oppermann */ 615bf6d304aSAndre Oppermann if (!syncache_expand(&inc, &to, th, &so, m)) { 6164195b4afSPaul Traina /* 617e207f800SAndre Oppermann * No syncache entry or ACK was not 618be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 6194195b4afSPaul Traina */ 620be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 621be2ac88cSJonathan Lemon goto dropwithreset; 622be2ac88cSJonathan Lemon } 623f76fcf6dSJeffrey Hsu if (so == NULL) { 624be2ac88cSJonathan Lemon /* 625e207f800SAndre Oppermann * We completed the 3-way handshake 626e207f800SAndre Oppermann * but could not allocate a socket 627e207f800SAndre Oppermann * either due to memory shortage, 628e207f800SAndre Oppermann * listen queue length limits or 629e207f800SAndre Oppermann * global socket limits. 630be2ac88cSJonathan Lemon */ 631e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 632e207f800SAndre Oppermann goto dropwithreset; 633f76fcf6dSJeffrey Hsu } 634be2ac88cSJonathan Lemon /* 635be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 636be2ac88cSJonathan Lemon * Continue processing segment. 637be2ac88cSJonathan Lemon */ 6380c38fd0aSAndre Oppermann INP_UNLOCK(inp); /* listen socket */ 639be2ac88cSJonathan Lemon inp = sotoinpcb(so); 6400c38fd0aSAndre Oppermann INP_LOCK(inp); /* new connection */ 641be2ac88cSJonathan Lemon tp = intotcpcb(inp); 642be2ac88cSJonathan Lemon /* 643302ce8d6SAndre Oppermann * Process the segment and the data it 644302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 645302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 646302ce8d6SAndre Oppermann */ 647679d9708SAndre Oppermann tcp_do_segment(m, th, so, tp, drop_hdrlen, 648679d9708SAndre Oppermann tlen); 649679d9708SAndre Oppermann INP_INFO_UNLOCK_ASSERT(&tcbinfo); 650302ce8d6SAndre Oppermann return; 651be2ac88cSJonathan Lemon } 652be2ac88cSJonathan Lemon if (thflags & TH_RST) { 653be2ac88cSJonathan Lemon syncache_chkrst(&inc, th); 654302ce8d6SAndre Oppermann goto dropunlock; 655be2ac88cSJonathan Lemon } 656fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 657be2ac88cSJonathan Lemon syncache_badack(&inc); 658ebb0cbeaSPaul Traina tcpstat.tcps_badsyn++; 659a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 660a57815efSBosko Milekic goto dropwithreset; 6614195b4afSPaul Traina } 662302ce8d6SAndre Oppermann goto dropunlock; 663ebb0cbeaSPaul Traina } 66433841545SHajimu UMEMOTO 665be2ac88cSJonathan Lemon /* 666be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 667be2ac88cSJonathan Lemon */ 66833841545SHajimu UMEMOTO #ifdef INET6 66933841545SHajimu UMEMOTO /* 67033841545SHajimu UMEMOTO * If deprecated address is forbidden, 67133841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 67233841545SHajimu UMEMOTO * address to prevent any new inbound connection from 67333841545SHajimu UMEMOTO * getting established. 67433841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 67533841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 67633841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 67733841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 67833841545SHajimu UMEMOTO * for the exchange. 67933841545SHajimu UMEMOTO * 68033841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 68133841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 68233841545SHajimu UMEMOTO * SYN in this case. 68333841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 68433841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 68533841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 68633841545SHajimu UMEMOTO * used" 68733841545SHajimu UMEMOTO * 2. use of it with new communication: 68833841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 68933841545SHajimu UMEMOTO * with sufficient scope is available" 69033841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 69133841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 69233841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 69333841545SHajimu UMEMOTO * 69433841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 69533841545SHajimu UMEMOTO * multiple description text for deprecated address 69633841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 69733841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 69833841545SHajimu UMEMOTO */ 69933841545SHajimu UMEMOTO if (isipv6 && !ip6_use_deprecated) { 70033841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 70133841545SHajimu UMEMOTO 70233841545SHajimu UMEMOTO if ((ia6 = ip6_getdstifaddr(m)) && 70333841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 70433841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 70533841545SHajimu UMEMOTO goto dropwithreset; 70633841545SHajimu UMEMOTO } 70733841545SHajimu UMEMOTO } 70833841545SHajimu UMEMOTO #endif 70965f28919SJesper Skriver /* 710db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 711db33b3e6SAndre Oppermann * 712db33b3e6SAndre Oppermann * Don't bother responding if the destination was a 713db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 714db33b3e6SAndre Oppermann * 715be2ac88cSJonathan Lemon * If it is from this socket, drop it, it must be forged. 7162ca2159fSCrist J. Clark * 71793ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 71893ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 71993ec91baSCrist J. Clark * in_broadcast() to find them. 720be2ac88cSJonathan Lemon */ 721be2ac88cSJonathan Lemon if (m->m_flags & (M_BCAST|M_MCAST)) 722302ce8d6SAndre Oppermann goto dropunlock; 723be2ac88cSJonathan Lemon if (isipv6) { 724db33b3e6SAndre Oppermann #ifdef INET6 725db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 726db33b3e6SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) 727302ce8d6SAndre Oppermann goto dropunlock; 728be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 729be2ac88cSJonathan Lemon IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 730302ce8d6SAndre Oppermann goto dropunlock; 731db33b3e6SAndre Oppermann #endif 732c068736aSJeffrey Hsu } else { 733db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 734db33b3e6SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) 735302ce8d6SAndre Oppermann goto dropunlock; 736be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 737be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 7382ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 7392ca2159fSCrist J. Clark in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 740302ce8d6SAndre Oppermann goto dropunlock; 741c068736aSJeffrey Hsu } 742be2ac88cSJonathan Lemon /* 743db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 744db33b3e6SAndre Oppermann * for syncache. 745be2ac88cSJonathan Lemon */ 7463c653157SHartmut Brandt #ifdef TCPDEBUG 7473c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 7483c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 7493c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 7503c653157SHartmut Brandt #endif 751f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 7524d6e7130SAndre Oppermann syncache_add(&inc, &to, th, inp, &so, m); 753be2ac88cSJonathan Lemon /* 7544d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 7554d6e7130SAndre Oppermann * Everything unlocked already by syncache_add(). 756be2ac88cSJonathan Lemon */ 757be2ac88cSJonathan Lemon return; 758f76fcf6dSJeffrey Hsu } 759db33b3e6SAndre Oppermann 760302ce8d6SAndre Oppermann /* 7611cd6eadfSRobert Watson * Segment belongs to a connection in SYN_SENT, ESTABLISHED or late 7621cd6eadfSRobert Watson * state. tcp_do_segment() always consumes the mbuf chain, unlocks the 7631cd6eadfSRobert Watson * inpcb, and unlocks the pcbinfo. 764302ce8d6SAndre Oppermann */ 765679d9708SAndre Oppermann tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen); 766679d9708SAndre Oppermann INP_INFO_UNLOCK_ASSERT(&tcbinfo); 767302ce8d6SAndre Oppermann return; 768be2ac88cSJonathan Lemon 769302ce8d6SAndre Oppermann dropwithreset: 770995a7717SAndre Oppermann INP_INFO_WLOCK_ASSERT(&tcbinfo); 771302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 772302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 773302ce8d6SAndre Oppermann dropunlock: 774995a7717SAndre Oppermann INP_INFO_WLOCK_ASSERT(&tcbinfo); 7759fa198beSAndre Oppermann if (inp != NULL) 776302ce8d6SAndre Oppermann INP_UNLOCK(inp); 777302ce8d6SAndre Oppermann INP_INFO_WUNLOCK(&tcbinfo); 778302ce8d6SAndre Oppermann drop: 779995a7717SAndre Oppermann INP_INFO_UNLOCK_ASSERT(&tcbinfo); 780302ce8d6SAndre Oppermann if (m != NULL) 781302ce8d6SAndre Oppermann m_freem(m); 782302ce8d6SAndre Oppermann return; 783302ce8d6SAndre Oppermann } 784302ce8d6SAndre Oppermann 785679d9708SAndre Oppermann static void 786302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 787302ce8d6SAndre Oppermann struct tcpcb *tp, int drop_hdrlen, int tlen) 788302ce8d6SAndre Oppermann { 789302ce8d6SAndre Oppermann int thflags, acked, ourfinisacked, needoutput = 0; 790302ce8d6SAndre Oppermann int headlocked = 1; 791302ce8d6SAndre Oppermann int rstreason, todrop, win; 792302ce8d6SAndre Oppermann u_long tiwin; 793302ce8d6SAndre Oppermann struct tcpopt to; 794302ce8d6SAndre Oppermann 79514739780SMaxim Konovalov #ifdef TCPDEBUG 79614739780SMaxim Konovalov /* 79714739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 79814739780SMaxim Konovalov * now IPv6. 79914739780SMaxim Konovalov */ 80014739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 80114739780SMaxim Konovalov struct tcphdr tcp_savetcp; 80214739780SMaxim Konovalov short ostate = 0; 80314739780SMaxim Konovalov #endif 804302ce8d6SAndre Oppermann thflags = th->th_flags; 805302ce8d6SAndre Oppermann 806302ce8d6SAndre Oppermann INP_INFO_WLOCK_ASSERT(&tcbinfo); 807302ce8d6SAndre Oppermann INP_LOCK_ASSERT(tp->t_inpcb); 808679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 809679d9708SAndre Oppermann __func__)); 810679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 811679d9708SAndre Oppermann __func__)); 812df8bae1dSRodney W. Grimes 813df8bae1dSRodney W. Grimes /* 814df8bae1dSRodney W. Grimes * Segment received on connection. 815df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 816df8bae1dSRodney W. Grimes */ 8179b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 8187ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 819b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 820df8bae1dSRodney W. Grimes 821df8bae1dSRodney W. Grimes /* 822464fcfbcSAndre Oppermann * Unscale the window into a 32-bit value. 823464fcfbcSAndre Oppermann * This value is bogus for the TCPS_SYN_SENT state 824464fcfbcSAndre Oppermann * and is overwritten later. 825464fcfbcSAndre Oppermann */ 826464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 827464fcfbcSAndre Oppermann 828464fcfbcSAndre Oppermann /* 829f72167f4SAndre Oppermann * Parse options on any incoming segment. 830f72167f4SAndre Oppermann */ 831302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 832302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 833302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 834f72167f4SAndre Oppermann 835f72167f4SAndre Oppermann /* 836f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 837bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 838bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 839bf6d304aSAndre Oppermann * was established. 840f72167f4SAndre Oppermann */ 841bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 842e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 843bf6d304aSAndre Oppermann if (TSTMP_GT(to.to_tsecr, ticks)) 844f72167f4SAndre Oppermann to.to_tsecr = 0; 845bf6d304aSAndre Oppermann } 846f72167f4SAndre Oppermann 847f72167f4SAndre Oppermann /* 84897d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 84997d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 85097d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 851df8bae1dSRodney W. Grimes */ 8520a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 853464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 854464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 855be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 85602a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 857464fcfbcSAndre Oppermann tp->snd_wnd = th->th_win << tp->snd_scale; 858464fcfbcSAndre Oppermann tiwin = tp->snd_wnd; 859be2ac88cSJonathan Lemon } 860be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 861be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 862be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 863be2ac88cSJonathan Lemon tp->ts_recent_age = ticks; 864be2ac88cSJonathan Lemon } 8654a32dc29SMohan Srinivasan /* Initial send window, already scaled. */ 8664a32dc29SMohan Srinivasan tp->snd_wnd = th->th_win; 867be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 868be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 8693529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 8703529149eSAndre Oppermann (to.to_flags & TOF_SACKPERM) == 0) 8713529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 8726d90faf3SPaul Saab } 8736d90faf3SPaul Saab 874df8bae1dSRodney W. Grimes /* 875df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 876df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 877df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 878df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 879df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 880df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 881df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 882df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 883df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 884df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 885df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 886df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 887a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 888c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 889a0292f23SGarrett Wollman * be TH_NEEDSYN. 890df8bae1dSRodney W. Grimes */ 891df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 892c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 893fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 894c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 895c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 896a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 897c5ad39b9SAndre Oppermann LIST_EMPTY(&tp->t_segq) && 898be2ac88cSJonathan Lemon ((to.to_flags & TOF_TS) == 0 || 899c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 900df8bae1dSRodney W. Grimes 901df8bae1dSRodney W. Grimes /* 902df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 903df8bae1dSRodney W. Grimes * record the timestamp. 904a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 905a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 906df8bae1dSRodney W. Grimes */ 907be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 908fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 9099b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 910a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 911df8bae1dSRodney W. Grimes } 912df8bae1dSRodney W. Grimes 913fb59c426SYoshinobu Inoue if (tlen == 0) { 914fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 915fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 916233e8c18SGarrett Wollman tp->snd_cwnd >= tp->snd_wnd && 9173529149eSAndre Oppermann ((!tcp_do_newreno && 9183529149eSAndre Oppermann !(tp->t_flags & TF_SACK_PERMIT) && 919cb942153SJeffrey Hsu tp->t_dupacks < tcprexmtthresh) || 9203529149eSAndre Oppermann ((tcp_do_newreno || 9213529149eSAndre Oppermann (tp->t_flags & TF_SACK_PERMIT)) && 922fc30a251SAndre Oppermann !IN_FASTRECOVERY(tp) && 923fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 924482ac968SPaul Saab TAILQ_EMPTY(&tp->snd_holes)))) { 925302ce8d6SAndre Oppermann KASSERT(headlocked, 926302ce8d6SAndre Oppermann ("%s: headlocked", __func__)); 927e0bef1cbSRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 928e0bef1cbSRobert Watson headlocked = 0; 929df8bae1dSRodney W. Grimes /* 930df8bae1dSRodney W. Grimes * this is a pure ack for outstanding data. 931df8bae1dSRodney W. Grimes */ 932df8bae1dSRodney W. Grimes ++tcpstat.tcps_predack; 9339b8b58e0SJonathan Lemon /* 9349b8b58e0SJonathan Lemon * "bad retransmit" recovery 9359b8b58e0SJonathan Lemon */ 9369b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 9379b8b58e0SJonathan Lemon ticks < tp->t_badrxtwin) { 938cb942153SJeffrey Hsu ++tcpstat.tcps_sndrexmitbad; 9399b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 9409b8b58e0SJonathan Lemon tp->snd_ssthresh = 9419b8b58e0SJonathan Lemon tp->snd_ssthresh_prev; 9429d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 9439d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 9449d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 9459b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 9469b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; 9479b8b58e0SJonathan Lemon } 948fa55172bSMatthew Dillon 949fa55172bSMatthew Dillon /* 950fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 951fa55172bSMatthew Dillon * 952fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 953fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 954fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 955fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 956fa55172bSMatthew Dillon */ 957fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 958fa55172bSMatthew Dillon to.to_tsecr) { 959eaf80179SAndre Oppermann if (!tp->t_rttlow || 960eaf80179SAndre Oppermann tp->t_rttlow > ticks - to.to_tsecr) 961eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 962a0292f23SGarrett Wollman tcp_xmit_timer(tp, 9639b8b58e0SJonathan Lemon ticks - to.to_tsecr + 1); 964fa55172bSMatthew Dillon } else if (tp->t_rtttime && 965fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 966eaf80179SAndre Oppermann if (!tp->t_rttlow || 967eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 968eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 969c068736aSJeffrey Hsu tcp_xmit_timer(tp, 970c068736aSJeffrey Hsu ticks - tp->t_rtttime); 971fa55172bSMatthew Dillon } 9721fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 973fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 974df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 975df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 976df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 9779d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 9789d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 9799d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 9809d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 9811645d090SJeff Roberson /* 9821645d090SJeff Roberson * pull snd_wl2 up to prevent seq wrap relative 9831645d090SJeff Roberson * to th_ack. 9841645d090SJeff Roberson */ 985cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 986b5addd85SJeffrey Hsu tp->t_dupacks = 0; 987df8bae1dSRodney W. Grimes m_freem(m); 988fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 989df8bae1dSRodney W. Grimes 990df8bae1dSRodney W. Grimes /* 991df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 992df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 993df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 994df8bae1dSRodney W. Grimes * If process is waiting for space, 995df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 996df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 997df8bae1dSRodney W. Grimes * decide between more output or persist. 9983c653157SHartmut Brandt 9993c653157SHartmut Brandt #ifdef TCPDEBUG 10003c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 10013c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 10023c653157SHartmut Brandt (void *)tcp_saveipgen, 10033c653157SHartmut Brandt &tcp_savetcp, 0); 10043c653157SHartmut Brandt #endif 1005df8bae1dSRodney W. Grimes */ 1006df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1007b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1008b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1009b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1010b8152ba7SAndre Oppermann tp->t_rxtcur); 1011df8bae1dSRodney W. Grimes 1012df8bae1dSRodney W. Grimes sowwakeup(so); 1013df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 1014df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1015a14c749fSJonathan Lemon goto check_delack; 1016df8bae1dSRodney W. Grimes } 1017fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1018fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 10196741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 10206741ecf5SAndre Oppermann 1021302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: headlocked", __func__)); 1022e0bef1cbSRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 1023e0bef1cbSRobert Watson headlocked = 0; 1024df8bae1dSRodney W. Grimes /* 1025df8bae1dSRodney W. Grimes * this is a pure, in-sequence data packet 1026df8bae1dSRodney W. Grimes * with nothing on the reassembly queue and 1027df8bae1dSRodney W. Grimes * we have enough buffer space to take it. 1028df8bae1dSRodney W. Grimes */ 10296d90faf3SPaul Saab /* Clean receiver SACK report if present */ 10303529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 10316d90faf3SPaul Saab tcp_clean_sackreport(tp); 1032df8bae1dSRodney W. Grimes ++tcpstat.tcps_preddat; 1033fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 10341645d090SJeff Roberson /* 10351645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 10361645d090SJeff Roberson * th_seq. 10371645d090SJeff Roberson */ 10381645d090SJeff Roberson tp->snd_wl1 = th->th_seq; 10391645d090SJeff Roberson /* 10401645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 10411645d090SJeff Roberson * rcv_nxt. 10421645d090SJeff Roberson */ 10431645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 1044df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpack++; 1045fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyte += tlen; 1046fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 10473c653157SHartmut Brandt #ifdef TCPDEBUG 10483c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 10493c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 10503c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 10513c653157SHartmut Brandt #endif 10526741ecf5SAndre Oppermann /* 10536741ecf5SAndre Oppermann * Automatic sizing of receive socket buffer. Often the send 10546741ecf5SAndre Oppermann * buffer size is not optimally adjusted to the actual network 10556741ecf5SAndre Oppermann * conditions at hand (delay bandwidth product). Setting the 10566741ecf5SAndre Oppermann * buffer size too small limits throughput on links with high 10576741ecf5SAndre Oppermann * bandwidth and high delay (eg. trans-continental/oceanic links). 10586741ecf5SAndre Oppermann * 10596741ecf5SAndre Oppermann * On the receive side the socket buffer memory is only rarely 10606741ecf5SAndre Oppermann * used to any significant extent. This allows us to be much 10616741ecf5SAndre Oppermann * more aggressive in scaling the receive socket buffer. For 10626741ecf5SAndre Oppermann * the case that the buffer space is actually used to a large 10636741ecf5SAndre Oppermann * extent and we run out of kernel memory we can simply drop 10646741ecf5SAndre Oppermann * the new segments; TCP on the sender will just retransmit it 10656741ecf5SAndre Oppermann * later. Setting the buffer size too big may only consume too 10666741ecf5SAndre Oppermann * much kernel memory if the application doesn't read() from 10676741ecf5SAndre Oppermann * the socket or packet loss or reordering makes use of the 10686741ecf5SAndre Oppermann * reassembly queue. 10696741ecf5SAndre Oppermann * 10706741ecf5SAndre Oppermann * The criteria to step up the receive buffer one notch are: 10716741ecf5SAndre Oppermann * 1. the number of bytes received during the time it takes 10726741ecf5SAndre Oppermann * one timestamp to be reflected back to us (the RTT); 10736741ecf5SAndre Oppermann * 2. received bytes per RTT is within seven eighth of the 10746741ecf5SAndre Oppermann * current socket buffer size; 10756741ecf5SAndre Oppermann * 3. receive buffer size has not hit maximal automatic size; 10766741ecf5SAndre Oppermann * 10776741ecf5SAndre Oppermann * This algorithm does one step per RTT at most and only if 10786741ecf5SAndre Oppermann * we receive a bulk stream w/o packet losses or reorderings. 10796741ecf5SAndre Oppermann * Shrinking the buffer during idle times is not necessary as 10806741ecf5SAndre Oppermann * it doesn't consume any memory when idle. 10816741ecf5SAndre Oppermann * 10826741ecf5SAndre Oppermann * TODO: Only step up if the application is actually serving 10836741ecf5SAndre Oppermann * the buffer to better manage the socket buffer resources. 1084df8bae1dSRodney W. Grimes */ 10856741ecf5SAndre Oppermann if (tcp_do_autorcvbuf && 10866741ecf5SAndre Oppermann to.to_tsecr && 10876741ecf5SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 10886741ecf5SAndre Oppermann if (to.to_tsecr > tp->rfbuf_ts && 10896741ecf5SAndre Oppermann to.to_tsecr - tp->rfbuf_ts < hz) { 10906741ecf5SAndre Oppermann if (tp->rfbuf_cnt > 10916741ecf5SAndre Oppermann (so->so_rcv.sb_hiwat / 8 * 7) && 10926741ecf5SAndre Oppermann so->so_rcv.sb_hiwat < 10936741ecf5SAndre Oppermann tcp_autorcvbuf_max) { 10946741ecf5SAndre Oppermann newsize = 10956741ecf5SAndre Oppermann min(so->so_rcv.sb_hiwat + 10966741ecf5SAndre Oppermann tcp_autorcvbuf_inc, 10976741ecf5SAndre Oppermann tcp_autorcvbuf_max); 10986741ecf5SAndre Oppermann } 10996741ecf5SAndre Oppermann /* Start over with next RTT. */ 11006741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 11016741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 11026741ecf5SAndre Oppermann } else 11036741ecf5SAndre Oppermann tp->rfbuf_cnt += tlen; /* add up */ 11046741ecf5SAndre Oppermann } 11056741ecf5SAndre Oppermann 11066741ecf5SAndre Oppermann /* Add data to socket buffer. */ 11071e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1108c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1109c1c36a2cSMike Silbersack m_freem(m); 1110c1c36a2cSMike Silbersack } else { 11116741ecf5SAndre Oppermann /* 11126741ecf5SAndre Oppermann * Set new socket buffer size. 11136741ecf5SAndre Oppermann * Give up when limit is reached. 11146741ecf5SAndre Oppermann */ 11156741ecf5SAndre Oppermann if (newsize) 11166741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 11176741ecf5SAndre Oppermann newsize, so, curthread)) 11186741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1119fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 11201e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 1121c1c36a2cSMike Silbersack } 11221e4d7da7SRobert Watson sorwakeup_locked(so); 1123d8c85a26SJonathan Lemon if (DELAY_ACK(tp)) { 11243bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1125f498eeeeSDavid Greenman } else { 1126e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 1127e612a582SDavid Greenman tcp_output(tp); 1128e612a582SDavid Greenman } 1129a14c749fSJonathan Lemon goto check_delack; 1130df8bae1dSRodney W. Grimes } 1131df8bae1dSRodney W. Grimes } 1132df8bae1dSRodney W. Grimes 1133df8bae1dSRodney W. Grimes /* 1134df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1135df8bae1dSRodney W. Grimes * and then do TCP input processing. 1136df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1137df8bae1dSRodney W. Grimes * but not less than advertised window. 1138df8bae1dSRodney W. Grimes */ 1139df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1140df8bae1dSRodney W. Grimes if (win < 0) 1141df8bae1dSRodney W. Grimes win = 0; 114266e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1143df8bae1dSRodney W. Grimes 11446741ecf5SAndre Oppermann /* Reset receive buffer auto scaling when not in bulk receive mode. */ 11456741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 11466741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 11476741ecf5SAndre Oppermann 1148df8bae1dSRodney W. Grimes switch (tp->t_state) { 1149df8bae1dSRodney W. Grimes 1150df8bae1dSRodney W. Grimes /* 1151764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1152764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1153764d8cefSBill Fenner */ 1154764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1155fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1156fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1157a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1158a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1159a57815efSBosko Milekic goto dropwithreset; 1160a57815efSBosko Milekic } 1161764d8cefSBill Fenner break; 1162764d8cefSBill Fenner 1163764d8cefSBill Fenner /* 1164df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1165df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1166df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1167df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1168df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1169df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1170df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1171df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1172df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1173df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1174df8bae1dSRodney W. Grimes */ 1175df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1176fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1177fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1178fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1179a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1180a0292f23SGarrett Wollman goto dropwithreset; 1181a0292f23SGarrett Wollman } 11820ca3f933SAndre Oppermann if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) 1183df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 11840ca3f933SAndre Oppermann if (thflags & TH_RST) 1185df8bae1dSRodney W. Grimes goto drop; 11860ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 1187df8bae1dSRodney W. Grimes goto drop; 1188464fcfbcSAndre Oppermann 1189fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1190df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1191fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1192845799c1SAndras Olah tcpstat.tcps_connects++; 1193845799c1SAndras Olah soisconnected(so); 1194c488362eSRobert Watson #ifdef MAC 1195310e7cebSRobert Watson SOCK_LOCK(so); 1196c488362eSRobert Watson mac_set_socket_peer_from_mbuf(m, so); 1197310e7cebSRobert Watson SOCK_UNLOCK(so); 1198c488362eSRobert Watson #endif 1199845799c1SAndras Olah /* Do window scaling on this connection? */ 1200845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1201845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1202845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1203845799c1SAndras Olah } 1204a0292f23SGarrett Wollman tp->rcv_adv += tp->rcv_wnd; 1205a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1206a0292f23SGarrett Wollman /* 1207a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1208a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1209a0292f23SGarrett Wollman */ 1210d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && tlen != 0) 1211b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 1212b8152ba7SAndre Oppermann tcp_delacktime); 1213a0292f23SGarrett Wollman else 1214a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1215a0292f23SGarrett Wollman /* 1216a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1217a0292f23SGarrett Wollman * Transitions: 1218a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1219a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1220a0292f23SGarrett Wollman */ 12219b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1222a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1223a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1224a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1225fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 12267ff19458SPaul Traina } else { 1227a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1228b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 12297ff19458SPaul Traina } 1230a0292f23SGarrett Wollman } else { 1231a0292f23SGarrett Wollman /* 1232c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 1233c068736aSJeffrey Hsu * simultaneous open. If segment contains CC option 1234c068736aSJeffrey Hsu * and there is a cached CC, apply TAO test. 1235c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 1236c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 1237a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1238a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1239a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1240a0292f23SGarrett Wollman */ 12414b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 1242b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1243df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1244a0292f23SGarrett Wollman } 1245df8bae1dSRodney W. Grimes 1246302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: trimthenstep6: head not locked", 1247302ce8d6SAndre Oppermann __func__)); 1248302ce8d6SAndre Oppermann INP_LOCK_ASSERT(tp->t_inpcb); 12497cfc6904SRobert Watson 1250df8bae1dSRodney W. Grimes /* 1251fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1252df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1253df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1254df8bae1dSRodney W. Grimes */ 1255fb59c426SYoshinobu Inoue th->th_seq++; 1256fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1257fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1258df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1259fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1260fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1261df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1262df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1263df8bae1dSRodney W. Grimes } 1264fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1265fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 1266a0292f23SGarrett Wollman /* 1267a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1268a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1269a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1270a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1271a0292f23SGarrett Wollman * Otherwise, goto step 6. 1272a0292f23SGarrett Wollman */ 1273fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1274a0292f23SGarrett Wollman goto process_ACK; 1275c068736aSJeffrey Hsu 1276df8bae1dSRodney W. Grimes goto step6; 1277c068736aSJeffrey Hsu 1278a0292f23SGarrett Wollman /* 1279a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1280c94c54e4SAndre Oppermann * do normal processing. 1281a0292f23SGarrett Wollman * 1282c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 1283a0292f23SGarrett Wollman */ 1284a0292f23SGarrett Wollman case TCPS_LAST_ACK: 1285a0292f23SGarrett Wollman case TCPS_CLOSING: 1286a0292f23SGarrett Wollman break; /* continue normal processing */ 1287df8bae1dSRodney W. Grimes } 1288df8bae1dSRodney W. Grimes 1289df8bae1dSRodney W. Grimes /* 1290df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 129180ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 129280ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 129380ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 129480ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 129580ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 129680ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 129780ab7c0eSGarrett Wollman * killing a connection). 129880ab7c0eSGarrett Wollman * Then check timestamp, if present. 1299a0292f23SGarrett Wollman * Then check the connection count, if present. 1300df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 1301df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 1302df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 1303df8bae1dSRodney W. Grimes * 130480ab7c0eSGarrett Wollman * 130580ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 130680ab7c0eSGarrett Wollman * if this is a valid reset segment. 130780ab7c0eSGarrett Wollman * RFC 793 page 37: 130880ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 130980ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 131080ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 131180ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 131280ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 13131a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 13141a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 13151a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 13161a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 131780dd2a81SMike Silbersack * Note 2: Paul Watson's paper "Slipping in the Window" has shown 131880dd2a81SMike Silbersack * that brute force RST attacks are possible. To combat this, 131980dd2a81SMike Silbersack * we use a much stricter check while in the ESTABLISHED state, 132080dd2a81SMike Silbersack * only accepting RSTs where the sequence number is equal to 132180dd2a81SMike Silbersack * last_ack_sent. In all other states (the states in which a 132280dd2a81SMike Silbersack * RST is more likely), the more permissive check is used. 132380ab7c0eSGarrett Wollman * If we have multiple segments in flight, the intial reset 132480ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 132580ab7c0eSGarrett Wollman * but they will eventually catch up. 132680ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 132780ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 132880ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 132980ab7c0eSGarrett Wollman * 133080ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 133180ab7c0eSGarrett Wollman * 133280ab7c0eSGarrett Wollman * DISCUSSION 133380ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 133480ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 133580ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 133680ab7c0eSGarrett Wollman * data. 133780ab7c0eSGarrett Wollman * 133880ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 133980ab7c0eSGarrett Wollman * the state: 134080ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 134180ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 134280ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 1343745bab7fSDima Dorfman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 134480ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 1345e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 134680ab7c0eSGarrett Wollman * Close the tcb. 1347e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 134880ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 134980ab7c0eSGarrett Wollman * RFC 1337. 135080ab7c0eSGarrett Wollman */ 1351fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 135295ad8418SQing Li if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 135395ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 135480ab7c0eSGarrett Wollman switch (tp->t_state) { 135580ab7c0eSGarrett Wollman 135680ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 135780ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 135880ab7c0eSGarrett Wollman goto close; 135980ab7c0eSGarrett Wollman 136080ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 136195ad8418SQing Li if (tcp_insecure_rst == 0 && 136295ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) && 136395ad8418SQing Li SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) && 136495ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 136595ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) { 136680dd2a81SMike Silbersack tcpstat.tcps_badrst++; 136780dd2a81SMike Silbersack goto drop; 136880dd2a81SMike Silbersack } 136980ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 137080ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 137180ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 137280ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 137380ab7c0eSGarrett Wollman close: 137480ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 137580ab7c0eSGarrett Wollman tcpstat.tcps_drops++; 1376302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: trimthenstep6: " 1377302ce8d6SAndre Oppermann "tcp_close: head not locked", __func__)); 137880ab7c0eSGarrett Wollman tp = tcp_close(tp); 137980ab7c0eSGarrett Wollman break; 138080ab7c0eSGarrett Wollman 138180ab7c0eSGarrett Wollman case TCPS_CLOSING: 138280ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 1383302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: trimthenstep6: " 1384302ce8d6SAndre Oppermann "tcp_close.2: head not locked", __func__)); 138580ab7c0eSGarrett Wollman tp = tcp_close(tp); 138680ab7c0eSGarrett Wollman break; 138780ab7c0eSGarrett Wollman } 138880ab7c0eSGarrett Wollman } 138980ab7c0eSGarrett Wollman goto drop; 139080ab7c0eSGarrett Wollman } 139180ab7c0eSGarrett Wollman 139280ab7c0eSGarrett Wollman /* 1393df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 1394df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 1395df8bae1dSRodney W. Grimes */ 1396be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 139780ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1398df8bae1dSRodney W. Grimes 1399df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 14009b8b58e0SJonathan Lemon if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1401df8bae1dSRodney W. Grimes /* 1402df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 1403df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 1404df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 1405df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 1406df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 1407df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 1408df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 1409df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 1410df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 1411df8bae1dSRodney W. Grimes */ 1412df8bae1dSRodney W. Grimes tp->ts_recent = 0; 1413df8bae1dSRodney W. Grimes } else { 1414df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 1415fb59c426SYoshinobu Inoue tcpstat.tcps_rcvdupbyte += tlen; 1416df8bae1dSRodney W. Grimes tcpstat.tcps_pawsdrop++; 141707fd333dSMatthew Dillon if (tlen) 1418df8bae1dSRodney W. Grimes goto dropafterack; 14191ab4789dSMatthew Dillon goto drop; 14201ab4789dSMatthew Dillon } 1421df8bae1dSRodney W. Grimes } 1422df8bae1dSRodney W. Grimes 1423a0292f23SGarrett Wollman /* 142480ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 142580ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 142680ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 142780ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 142880ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 142980ab7c0eSGarrett Wollman */ 1430a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 1431a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1432a57815efSBosko Milekic goto dropwithreset; 1433a57815efSBosko Milekic } 143480ab7c0eSGarrett Wollman 1435fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 1436df8bae1dSRodney W. Grimes if (todrop > 0) { 1437fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1438fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 1439fb59c426SYoshinobu Inoue th->th_seq++; 1440fb59c426SYoshinobu Inoue if (th->th_urp > 1) 1441fb59c426SYoshinobu Inoue th->th_urp--; 1442df8bae1dSRodney W. Grimes else 1443fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1444df8bae1dSRodney W. Grimes todrop--; 1445df8bae1dSRodney W. Grimes } 1446df8bae1dSRodney W. Grimes /* 1447dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 1448df8bae1dSRodney W. Grimes */ 1449fb59c426SYoshinobu Inoue if (todrop > tlen 1450fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1451dac20301SGarrett Wollman /* 1452dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 1453dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 1454dac20301SGarrett Wollman * of sequence; drop it. 1455dac20301SGarrett Wollman */ 1456fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1457dac20301SGarrett Wollman 1458df8bae1dSRodney W. Grimes /* 1459dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 1460dac20301SGarrett Wollman * But keep on processing for RST or ACK. 1461df8bae1dSRodney W. Grimes */ 1462dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1463fb59c426SYoshinobu Inoue todrop = tlen; 1464dac20301SGarrett Wollman tcpstat.tcps_rcvduppack++; 1465dac20301SGarrett Wollman tcpstat.tcps_rcvdupbyte += todrop; 1466df8bae1dSRodney W. Grimes } else { 1467df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartduppack++; 1468df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartdupbyte += todrop; 1469df8bae1dSRodney W. Grimes } 1470fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 1471fb59c426SYoshinobu Inoue th->th_seq += todrop; 1472fb59c426SYoshinobu Inoue tlen -= todrop; 1473fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 1474fb59c426SYoshinobu Inoue th->th_urp -= todrop; 1475df8bae1dSRodney W. Grimes else { 1476fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1477fb59c426SYoshinobu Inoue th->th_urp = 0; 1478df8bae1dSRodney W. Grimes } 1479df8bae1dSRodney W. Grimes } 1480df8bae1dSRodney W. Grimes 1481df8bae1dSRodney W. Grimes /* 1482df8bae1dSRodney W. Grimes * If new data are received on a connection after the 1483df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 1484df8bae1dSRodney W. Grimes */ 1485df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 1486fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1487302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: trimthenstep6: tcp_close.3: head " 1488302ce8d6SAndre Oppermann "not locked", __func__)); 1489df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1490df8bae1dSRodney W. Grimes tcpstat.tcps_rcvafterclose++; 1491a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1492df8bae1dSRodney W. Grimes goto dropwithreset; 1493df8bae1dSRodney W. Grimes } 1494df8bae1dSRodney W. Grimes 1495df8bae1dSRodney W. Grimes /* 1496df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 1497df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 1498df8bae1dSRodney W. Grimes */ 1499fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 1500df8bae1dSRodney W. Grimes if (todrop > 0) { 1501df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1502fb59c426SYoshinobu Inoue if (todrop >= tlen) { 1503fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyteafterwin += tlen; 1504df8bae1dSRodney W. Grimes /* 1505df8bae1dSRodney W. Grimes * If window is closed can only take segments at 1506df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 1507df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 1508df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 1509df8bae1dSRodney W. Grimes * and ack. 1510df8bae1dSRodney W. Grimes */ 1511fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1512df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1513df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinprobe++; 1514df8bae1dSRodney W. Grimes } else 1515df8bae1dSRodney W. Grimes goto dropafterack; 1516df8bae1dSRodney W. Grimes } else 1517df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1518df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1519fb59c426SYoshinobu Inoue tlen -= todrop; 1520fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 1521df8bae1dSRodney W. Grimes } 1522df8bae1dSRodney W. Grimes 1523df8bae1dSRodney W. Grimes /* 1524df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1525df8bae1dSRodney W. Grimes * record its timestamp. 1526cf09195bSPaul Saab * NOTE: 1527cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 1528a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1529cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 1530cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 1531cf09195bSPaul Saab * predicated on the sequence space of this segment. 1532cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 1533cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 1534cf09195bSPaul Saab * instead of RFC1323's 1535cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 1536cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 1537cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 1538cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 1539cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 1540df8bae1dSRodney W. Grimes */ 1541be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1542cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 1543cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 1544cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 15459b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1546a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1547df8bae1dSRodney W. Grimes } 1548df8bae1dSRodney W. Grimes 1549df8bae1dSRodney W. Grimes /* 1550df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 1551df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 1552df8bae1dSRodney W. Grimes */ 1553fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1554302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: tcp_drop: trimthenstep6: " 1555302ce8d6SAndre Oppermann "head not locked", __func__)); 1556df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 1557a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1558122aad88SAndre Oppermann goto drop; 1559df8bae1dSRodney W. Grimes } 1560df8bae1dSRodney W. Grimes 1561a0292f23SGarrett Wollman /* 1562a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1563a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 1564a0292f23SGarrett Wollman * later processing; else drop segment and return. 1565a0292f23SGarrett Wollman */ 1566fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 1567a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 1568a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 1569a0292f23SGarrett Wollman goto step6; 15705e1aa279SDavid Malone else if (tp->t_flags & TF_ACKNOW) 15715e1aa279SDavid Malone goto dropafterack; 1572a0292f23SGarrett Wollman else 1573a0292f23SGarrett Wollman goto drop; 1574a0292f23SGarrett Wollman } 1575df8bae1dSRodney W. Grimes 1576df8bae1dSRodney W. Grimes /* 1577df8bae1dSRodney W. Grimes * Ack processing. 1578df8bae1dSRodney W. Grimes */ 1579df8bae1dSRodney W. Grimes switch (tp->t_state) { 1580df8bae1dSRodney W. Grimes 1581df8bae1dSRodney W. Grimes /* 1582764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1583764d8cefSBill Fenner * ESTABLISHED state and continue processing. 1584764d8cefSBill Fenner * The ACK was checked above. 1585df8bae1dSRodney W. Grimes */ 1586df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1587a0292f23SGarrett Wollman 1588df8bae1dSRodney W. Grimes tcpstat.tcps_connects++; 1589df8bae1dSRodney W. Grimes soisconnected(so); 1590df8bae1dSRodney W. Grimes /* Do window scaling? */ 1591df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1592df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1593df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 1594464fcfbcSAndre Oppermann tp->snd_wnd = tiwin; 1595df8bae1dSRodney W. Grimes } 1596a0292f23SGarrett Wollman /* 1597a0292f23SGarrett Wollman * Make transitions: 1598a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 1599a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 1600a0292f23SGarrett Wollman */ 16019b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1602a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1603a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1604a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 16057ff19458SPaul Traina } else { 1606a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1607b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 16087ff19458SPaul Traina } 1609a0292f23SGarrett Wollman /* 1610a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 1611a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 1612a0292f23SGarrett Wollman */ 1613fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 1614fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1615a0292f23SGarrett Wollman (struct mbuf *)0); 1616fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 161793b0017fSPhilippe Charnier /* FALLTHROUGH */ 1618df8bae1dSRodney W. Grimes 1619df8bae1dSRodney W. Grimes /* 1620df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1621df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 1622fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 1623fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 1624df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 1625df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 1626df8bae1dSRodney W. Grimes */ 1627df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1628df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1629df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 1630df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1631df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1632df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 16335a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 16345a53ca16SPaul Saab tcpstat.tcps_rcvacktoomuch++; 16355a53ca16SPaul Saab goto dropafterack; 16365a53ca16SPaul Saab } 16373529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 1638fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 1639fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 16405a53ca16SPaul Saab tcp_sack_doack(tp, &to, th->th_ack); 1641fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1642fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 1643df8bae1dSRodney W. Grimes tcpstat.tcps_rcvdupack++; 1644df8bae1dSRodney W. Grimes /* 1645df8bae1dSRodney W. Grimes * If we have outstanding data (other than 1646df8bae1dSRodney W. Grimes * a window probe), this is a completely 1647df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 1648df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 1649df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 1650df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 1651df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 1652df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 1653df8bae1dSRodney W. Grimes * window so we send only this one 1654df8bae1dSRodney W. Grimes * packet. 1655df8bae1dSRodney W. Grimes * 1656df8bae1dSRodney W. Grimes * We know we're losing at the current 1657df8bae1dSRodney W. Grimes * window size so do congestion avoidance 1658df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 1659df8bae1dSRodney W. Grimes * and pull our congestion window back to 1660df8bae1dSRodney W. Grimes * the new ssthresh). 1661df8bae1dSRodney W. Grimes * 1662df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 1663df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 1664df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 1665df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 1666df8bae1dSRodney W. Grimes * network. 1667df8bae1dSRodney W. Grimes */ 1668b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_REXMT) || 1669fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 1670df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1671cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 16723529149eSAndre Oppermann ((tcp_do_newreno || 16733529149eSAndre Oppermann (tp->t_flags & TF_SACK_PERMIT)) && 16749d11646dSJeffrey Hsu IN_FASTRECOVERY(tp))) { 16753529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 16763529149eSAndre Oppermann IN_FASTRECOVERY(tp)) { 1677808f11b7SPaul Saab int awnd; 167825e6f9edSPaul Saab 167925e6f9edSPaul Saab /* 168025e6f9edSPaul Saab * Compute the amount of data in flight first. 168125e6f9edSPaul Saab * We can inject new data into the pipe iff 168225e6f9edSPaul Saab * we have less than 1/2 the original window's 168325e6f9edSPaul Saab * worth of data in flight. 168425e6f9edSPaul Saab */ 1685808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 16860077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 1687808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 168825e6f9edSPaul Saab tp->snd_cwnd += tp->t_maxseg; 168925e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 169025e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 169125e6f9edSPaul Saab } 169225e6f9edSPaul Saab } else 169346f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 169446f58482SJonathan Lemon (void) tcp_output(tp); 169546f58482SJonathan Lemon goto drop; 1696cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 1697cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 1698cb942153SJeffrey Hsu u_int win; 1699a0445c2eSJayanth Vijayaraghavan 1700a0445c2eSJayanth Vijayaraghavan /* 1701a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 1702a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 1703a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 1704a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 1705a0445c2eSJayanth Vijayaraghavan * recovery. 1706a0445c2eSJayanth Vijayaraghavan */ 17073529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 1708a0445c2eSJayanth Vijayaraghavan if (IN_FASTRECOVERY(tp)) { 1709a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 1710a0445c2eSJayanth Vijayaraghavan break; 1711a0445c2eSJayanth Vijayaraghavan } 1712a0445c2eSJayanth Vijayaraghavan } else if (tcp_do_newreno) { 1713a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 17149d11646dSJeffrey Hsu tp->snd_recover)) { 1715cb942153SJeffrey Hsu tp->t_dupacks = 0; 1716cb942153SJeffrey Hsu break; 171746f58482SJonathan Lemon } 1718a0445c2eSJayanth Vijayaraghavan } 1719cb942153SJeffrey Hsu win = min(tp->snd_wnd, tp->snd_cwnd) / 1720cb942153SJeffrey Hsu 2 / tp->t_maxseg; 1721df8bae1dSRodney W. Grimes if (win < 2) 1722df8bae1dSRodney W. Grimes win = 2; 1723df8bae1dSRodney W. Grimes tp->snd_ssthresh = win * tp->t_maxseg; 17249d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 172546f58482SJonathan Lemon tp->snd_recover = tp->snd_max; 1726b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 17279b8b58e0SJonathan Lemon tp->t_rtttime = 0; 17283529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 17296d90faf3SPaul Saab tcpstat.tcps_sack_recovery_episode++; 1730a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 17318db456bfSPaul Saab tp->snd_cwnd = tp->t_maxseg; 17326d90faf3SPaul Saab (void) tcp_output(tp); 17336d90faf3SPaul Saab goto drop; 17346d90faf3SPaul Saab } 1735fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 1736df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 1737df8bae1dSRodney W. Grimes (void) tcp_output(tp); 173848d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 1739302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 1740302ce8d6SAndre Oppermann __func__)); 1741df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 174248d2549cSJeffrey Hsu tp->t_maxseg * 174348d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 1744df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 1745df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 1746df8bae1dSRodney W. Grimes goto drop; 1747582a954bSJeffrey Hsu } else if (tcp_do_rfc3042) { 1748582a954bSJeffrey Hsu u_long oldcwnd = tp->snd_cwnd; 174948d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 175048d2549cSJeffrey Hsu u_int sent; 175189c02376SJeffrey Hsu 1752582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 1753582a954bSJeffrey Hsu tp->t_dupacks == 2, 1754302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 1755302ce8d6SAndre Oppermann __func__)); 175661a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 175748d2549cSJeffrey Hsu tp->snd_limited = 0; 175861a36e3dSJeffrey Hsu tp->snd_cwnd = 175961a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 176061a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 176161a36e3dSJeffrey Hsu tp->t_maxseg; 1762582a954bSJeffrey Hsu (void) tcp_output(tp); 176348d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 176448d2549cSJeffrey Hsu if (sent > tp->t_maxseg) { 176589c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 176689c02376SJeffrey Hsu tp->snd_limited == 0) || 176789c02376SJeffrey Hsu (sent == tp->t_maxseg + 1 && 176889c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 1769302ce8d6SAndre Oppermann ("%s: sent too much", 1770302ce8d6SAndre Oppermann __func__)); 177148d2549cSJeffrey Hsu tp->snd_limited = 2; 177248d2549cSJeffrey Hsu } else if (sent > 0) 177348d2549cSJeffrey Hsu ++tp->snd_limited; 1774582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 1775582a954bSJeffrey Hsu goto drop; 1776df8bae1dSRodney W. Grimes } 1777df8bae1dSRodney W. Grimes } else 1778df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1779df8bae1dSRodney W. Grimes break; 1780df8bae1dSRodney W. Grimes } 1781cb942153SJeffrey Hsu 1782302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 1783302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 1784cb942153SJeffrey Hsu 1785df8bae1dSRodney W. Grimes /* 1786df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 1787df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 1788df8bae1dSRodney W. Grimes */ 17893529149eSAndre Oppermann if (tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) { 17909d11646dSJeffrey Hsu if (IN_FASTRECOVERY(tp)) { 1791cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 17923529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 17936d90faf3SPaul Saab tcp_sack_partialack(tp, th); 17946d90faf3SPaul Saab else 1795c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 1796c068736aSJeffrey Hsu } else { 1797c068736aSJeffrey Hsu /* 17986d90faf3SPaul Saab * Out of fast recovery. 1799c068736aSJeffrey Hsu * Window inflation should have left us 1800c068736aSJeffrey Hsu * with approximately snd_ssthresh 1801c068736aSJeffrey Hsu * outstanding data. 1802c068736aSJeffrey Hsu * But in case we would be inclined to 1803c068736aSJeffrey Hsu * send a burst, better to do it via 1804c068736aSJeffrey Hsu * the slow start mechanism. 1805c068736aSJeffrey Hsu */ 1806c068736aSJeffrey Hsu if (SEQ_GT(th->th_ack + 1807c068736aSJeffrey Hsu tp->snd_ssthresh, 1808c068736aSJeffrey Hsu tp->snd_max)) 1809c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_max - 1810c068736aSJeffrey Hsu th->th_ack + 1811c068736aSJeffrey Hsu tp->t_maxseg; 1812c068736aSJeffrey Hsu else 1813c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_ssthresh; 1814c068736aSJeffrey Hsu } 1815c068736aSJeffrey Hsu } 1816c068736aSJeffrey Hsu } else { 1817233e8c18SGarrett Wollman if (tp->t_dupacks >= tcprexmtthresh && 1818df8bae1dSRodney W. Grimes tp->snd_cwnd > tp->snd_ssthresh) 1819df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh; 182046f58482SJonathan Lemon } 1821cb942153SJeffrey Hsu tp->t_dupacks = 0; 1822a0292f23SGarrett Wollman /* 1823a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 1824a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 1825a0292f23SGarrett Wollman */ 1826a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 1827a0292f23SGarrett Wollman /* 1828a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 1829a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 183007e43e10SAndras Olah * synchronized). Go to non-starred state, 183107e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 183207e43e10SAndras Olah * we can do window scaling. 1833a0292f23SGarrett Wollman */ 1834a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 1835a0292f23SGarrett Wollman tp->snd_una++; 183607e43e10SAndras Olah /* Do window scaling? */ 183707e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 183807e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 183907e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 1840464fcfbcSAndre Oppermann /* Send window already scaled. */ 184107e43e10SAndras Olah } 1842a0292f23SGarrett Wollman } 1843a0292f23SGarrett Wollman 1844a0292f23SGarrett Wollman process_ACK: 1845302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: process_ACK: head not locked", 1846302ce8d6SAndre Oppermann __func__)); 1847302ce8d6SAndre Oppermann INP_LOCK_ASSERT(tp->t_inpcb); 18487cfc6904SRobert Watson 1849fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 1850df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 1851df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 1852df8bae1dSRodney W. Grimes 1853df8bae1dSRodney W. Grimes /* 18549b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 18559b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 18569b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 18579b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 18589b8b58e0SJonathan Lemon * we left off. 18599b8b58e0SJonathan Lemon */ 18609b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 1861d65bf08aSMatthew Dillon ++tcpstat.tcps_sndrexmitbad; 18629b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 18639b8b58e0SJonathan Lemon tp->snd_ssthresh = tp->snd_ssthresh_prev; 18649d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 18659d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 18669d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 18679b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 18689b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; /* XXX probably not required */ 18699b8b58e0SJonathan Lemon } 18709b8b58e0SJonathan Lemon 18719b8b58e0SJonathan Lemon /* 1872df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 1873df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 1874df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 1875df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 1876df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 1877df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 1878df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 1879fa55172bSMatthew Dillon * 1880fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1881fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1882fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1883fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1884df8bae1dSRodney W. Grimes */ 1885fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1886fa55172bSMatthew Dillon to.to_tsecr) { 1887eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - to.to_tsecr) 1888eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 18899b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 1890fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 1891eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 1892eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 18939b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 1894fa55172bSMatthew Dillon } 18951fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 1896df8bae1dSRodney W. Grimes 1897df8bae1dSRodney W. Grimes /* 1898df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 1899df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 1900df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 1901df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 1902df8bae1dSRodney W. Grimes */ 1903fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 1904b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1905df8bae1dSRodney W. Grimes needoutput = 1; 1906b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 1907b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 1908a0292f23SGarrett Wollman 1909a0292f23SGarrett Wollman /* 1910a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 1911a0292f23SGarrett Wollman * skip rest of ACK processing. 1912a0292f23SGarrett Wollman */ 1913a0292f23SGarrett Wollman if (acked == 0) 1914a0292f23SGarrett Wollman goto step6; 1915a0292f23SGarrett Wollman 1916df8bae1dSRodney W. Grimes /* 1917df8bae1dSRodney W. Grimes * When new data is acked, open the congestion window. 1918df8bae1dSRodney W. Grimes * If the window gives us less than ssthresh packets 1919df8bae1dSRodney W. Grimes * in flight, open exponentially (maxseg per packet). 1920df8bae1dSRodney W. Grimes * Otherwise open linearly: maxseg per window 192110be5648SGarrett Wollman * (maxseg^2 / cwnd per packet). 1922df8bae1dSRodney W. Grimes */ 19233529149eSAndre Oppermann if ((!tcp_do_newreno && !(tp->t_flags & TF_SACK_PERMIT)) || 19246d90faf3SPaul Saab !IN_FASTRECOVERY(tp)) { 1925ad3f9ab3SAndre Oppermann u_int cw = tp->snd_cwnd; 1926ad3f9ab3SAndre Oppermann u_int incr = tp->t_maxseg; 1927df8bae1dSRodney W. Grimes if (cw > tp->snd_ssthresh) 192810be5648SGarrett Wollman incr = incr * incr / cw; 1929df8bae1dSRodney W. Grimes tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale); 1930df8bae1dSRodney W. Grimes } 19315905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 1932df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 1933df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 19345905999bSRobert Watson sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc); 1935df8bae1dSRodney W. Grimes ourfinisacked = 1; 1936df8bae1dSRodney W. Grimes } else { 19375905999bSRobert Watson sbdrop_locked(&so->so_snd, acked); 1938df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 1939df8bae1dSRodney W. Grimes ourfinisacked = 0; 1940df8bae1dSRodney W. Grimes } 19411e4d7da7SRobert Watson sowwakeup_locked(so); 1942cb942153SJeffrey Hsu /* detect una wraparound */ 19433529149eSAndre Oppermann if ((tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) && 19446d90faf3SPaul Saab !IN_FASTRECOVERY(tp) && 19459d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 19469d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 19479d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 19483529149eSAndre Oppermann if ((tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) && 19496d90faf3SPaul Saab IN_FASTRECOVERY(tp) && 19509d11646dSJeffrey Hsu SEQ_GEQ(th->th_ack, tp->snd_recover)) 19519d11646dSJeffrey Hsu EXIT_FASTRECOVERY(tp); 1952fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 19533529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 19546d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 19556d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 19566d90faf3SPaul Saab } 1957df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1958df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 1959df8bae1dSRodney W. Grimes 1960df8bae1dSRodney W. Grimes switch (tp->t_state) { 1961df8bae1dSRodney W. Grimes 1962df8bae1dSRodney W. Grimes /* 1963df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 1964df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 1965df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 1966df8bae1dSRodney W. Grimes */ 1967df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1968df8bae1dSRodney W. Grimes if (ourfinisacked) { 1969df8bae1dSRodney W. Grimes /* 1970df8bae1dSRodney W. Grimes * If we can't receive any more 1971df8bae1dSRodney W. Grimes * data, then closing user can proceed. 1972df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 1973df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 1974df8bae1dSRodney W. Grimes * we'll hang forever. 1975df8bae1dSRodney W. Grimes */ 1976340c35deSJonathan Lemon /* XXXjl 1977340c35deSJonathan Lemon * we should release the tp also, and use a 1978340c35deSJonathan Lemon * compressed state. 1979340c35deSJonathan Lemon */ 1980c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 19817c72af87SMohan Srinivasan int timeout; 19827c72af87SMohan Srinivasan 198303e49181SSeigo Tanimura soisdisconnected(so); 19847c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 19857c72af87SMohan Srinivasan tcp_finwait2_timeout : tcp_maxidle; 1986b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 19874cc20ab1SSeigo Tanimura } 1988df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 1989df8bae1dSRodney W. Grimes } 1990df8bae1dSRodney W. Grimes break; 1991df8bae1dSRodney W. Grimes 1992df8bae1dSRodney W. Grimes /* 1993df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 1994df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 1995df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 1996df8bae1dSRodney W. Grimes * the segment. 1997df8bae1dSRodney W. Grimes */ 1998df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1999df8bae1dSRodney W. Grimes if (ourfinisacked) { 2000302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: process_ACK: " 2001302ce8d6SAndre Oppermann "head not locked", __func__)); 200211a20fb8SJeffrey Hsu tcp_twstart(tp); 2003340c35deSJonathan Lemon INP_INFO_WUNLOCK(&tcbinfo); 2004302ce8d6SAndre Oppermann headlocked = 0; 2005340c35deSJonathan Lemon m_freem(m); 2006679d9708SAndre Oppermann return; 2007df8bae1dSRodney W. Grimes } 2008df8bae1dSRodney W. Grimes break; 2009df8bae1dSRodney W. Grimes 2010df8bae1dSRodney W. Grimes /* 2011df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2012df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2013df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2014df8bae1dSRodney W. Grimes * enter the closed state and return. 2015df8bae1dSRodney W. Grimes */ 2016df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2017df8bae1dSRodney W. Grimes if (ourfinisacked) { 2018302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: process_ACK: " 2019302ce8d6SAndre Oppermann "tcp_close: head not locked", __func__)); 2020df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2021df8bae1dSRodney W. Grimes goto drop; 2022df8bae1dSRodney W. Grimes } 2023df8bae1dSRodney W. Grimes break; 2024df8bae1dSRodney W. Grimes } 2025df8bae1dSRodney W. Grimes } 2026df8bae1dSRodney W. Grimes 2027df8bae1dSRodney W. Grimes step6: 2028302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: step6: head not locked", __func__)); 2029302ce8d6SAndre Oppermann INP_LOCK_ASSERT(tp->t_inpcb); 20307cfc6904SRobert Watson 2031df8bae1dSRodney W. Grimes /* 2032df8bae1dSRodney W. Grimes * Update window information. 2033df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 2034df8bae1dSRodney W. Grimes */ 2035fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 2036fb59c426SYoshinobu Inoue (SEQ_LT(tp->snd_wl1, th->th_seq) || 2037fb59c426SYoshinobu Inoue (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2038fb59c426SYoshinobu Inoue (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2039df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 2040fb59c426SYoshinobu Inoue if (tlen == 0 && 2041fb59c426SYoshinobu Inoue tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2042df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinupd++; 2043df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 2044fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq; 2045fb59c426SYoshinobu Inoue tp->snd_wl2 = th->th_ack; 2046df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2047df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 2048df8bae1dSRodney W. Grimes needoutput = 1; 2049df8bae1dSRodney W. Grimes } 2050df8bae1dSRodney W. Grimes 2051df8bae1dSRodney W. Grimes /* 2052df8bae1dSRodney W. Grimes * Process segments with URG. 2053df8bae1dSRodney W. Grimes */ 2054fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2055df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2056df8bae1dSRodney W. Grimes /* 2057df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2058df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2059df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2060df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2061df8bae1dSRodney W. Grimes */ 206218ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2063fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2064fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2065fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 206618ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2067df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2068df8bae1dSRodney W. Grimes } 2069df8bae1dSRodney W. Grimes /* 2070df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2071df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2072df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2073df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2074df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2075df8bae1dSRodney W. Grimes * 2076df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2077df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2078df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2079df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2080df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2081df8bae1dSRodney W. Grimes * spec states (in one of two places). 2082df8bae1dSRodney W. Grimes */ 2083fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2084fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2085df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2086df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2087927c5ceaSRobert Watson if (so->so_oobmark == 0) 2088c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 2089df8bae1dSRodney W. Grimes sohasoutofband(so); 2090df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2091df8bae1dSRodney W. Grimes } 209218ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2093df8bae1dSRodney W. Grimes /* 2094df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2095df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2096df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2097df8bae1dSRodney W. Grimes * data may creep in... ick. 2098df8bae1dSRodney W. Grimes */ 209930613f56SJeffrey Hsu if (th->th_urp <= (u_long)tlen && 210030613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 210130613f56SJeffrey Hsu /* hdr drop is delayed */ 210230613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 210330613f56SJeffrey Hsu } 2104c068736aSJeffrey Hsu } else { 2105df8bae1dSRodney W. Grimes /* 2106df8bae1dSRodney W. Grimes * If no out of band data is expected, 2107df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2108df8bae1dSRodney W. Grimes * with the receive window. 2109df8bae1dSRodney W. Grimes */ 2110df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2111df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2112c068736aSJeffrey Hsu } 2113df8bae1dSRodney W. Grimes dodata: /* XXX */ 2114302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: dodata: head not locked", __func__)); 2115302ce8d6SAndre Oppermann INP_LOCK_ASSERT(tp->t_inpcb); 21167cfc6904SRobert Watson 2117df8bae1dSRodney W. Grimes /* 2118df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2119df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2120df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2121df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2122df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2123df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2124df8bae1dSRodney W. Grimes */ 2125fb59c426SYoshinobu Inoue if ((tlen || (thflags & TH_FIN)) && 2126df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 212769e03620SPaul Saab tcp_seq save_start = th->th_seq; 212869e03620SPaul Saab tcp_seq save_end = th->th_seq + tlen; 2129fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2130e4b64281SJesper Skriver /* 2131c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2132c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2133c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2134c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2135c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2136c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2137c068736aSJeffrey Hsu * conversions. 2138c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 2139c068736aSJeffrey Hsu * immediately when segments are out of order (so 2140c068736aSJeffrey Hsu * fast retransmit can work). 2141e4b64281SJesper Skriver */ 2142e4b64281SJesper Skriver if (th->th_seq == tp->rcv_nxt && 2143e4b64281SJesper Skriver LIST_EMPTY(&tp->t_segq) && 2144e4b64281SJesper Skriver TCPS_HAVEESTABLISHED(tp->t_state)) { 2145e4b64281SJesper Skriver if (DELAY_ACK(tp)) 21463bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2147e4b64281SJesper Skriver else 2148e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2149e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2150e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 2151e4b64281SJesper Skriver tcpstat.tcps_rcvpack++; 2152e4b64281SJesper Skriver tcpstat.tcps_rcvbyte += tlen; 2153e4b64281SJesper Skriver ND6_HINT(tp); 21541e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2155c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2156c1c36a2cSMike Silbersack m_freem(m); 2157c1c36a2cSMike Silbersack else 21581e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 21591e4d7da7SRobert Watson sorwakeup_locked(so); 2160e4b64281SJesper Skriver } else { 2161e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 2162e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2163e4b64281SJesper Skriver } 21643529149eSAndre Oppermann if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 216569e03620SPaul Saab tcp_update_sack_list(tp, save_start, save_end); 2166302ce8d6SAndre Oppermann #if 0 2167df8bae1dSRodney W. Grimes /* 2168df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2169df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2170df8bae1dSRodney W. Grimes * buffer size. 2171302ce8d6SAndre Oppermann * XXX: Unused. 2172df8bae1dSRodney W. Grimes */ 2173df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2174302ce8d6SAndre Oppermann #endif 2175df8bae1dSRodney W. Grimes } else { 2176df8bae1dSRodney W. Grimes m_freem(m); 2177fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2178df8bae1dSRodney W. Grimes } 2179df8bae1dSRodney W. Grimes 2180df8bae1dSRodney W. Grimes /* 2181df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2182df8bae1dSRodney W. Grimes * that the connection is closing. 2183df8bae1dSRodney W. Grimes */ 2184fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2185df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2186df8bae1dSRodney W. Grimes socantrcvmore(so); 2187a0292f23SGarrett Wollman /* 2188a0292f23SGarrett Wollman * If connection is half-synchronized 2189d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2190a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2191a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2192a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2193a0292f23SGarrett Wollman */ 21943bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 21953bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2196a0292f23SGarrett Wollman else 2197df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2198df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2199df8bae1dSRodney W. Grimes } 2200df8bae1dSRodney W. Grimes switch (tp->t_state) { 2201df8bae1dSRodney W. Grimes 2202df8bae1dSRodney W. Grimes /* 2203df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2204df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2205df8bae1dSRodney W. Grimes */ 2206df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 22079b8b58e0SJonathan Lemon tp->t_starttime = ticks; 22089b8b58e0SJonathan Lemon /*FALLTHROUGH*/ 2209df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2210df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2211df8bae1dSRodney W. Grimes break; 2212df8bae1dSRodney W. Grimes 2213df8bae1dSRodney W. Grimes /* 2214df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2215df8bae1dSRodney W. Grimes * enter the CLOSING state. 2216df8bae1dSRodney W. Grimes */ 2217df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2218df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 2219df8bae1dSRodney W. Grimes break; 2220df8bae1dSRodney W. Grimes 2221df8bae1dSRodney W. Grimes /* 2222df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 2223df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 2224df8bae1dSRodney W. Grimes * standard timers. 2225df8bae1dSRodney W. Grimes */ 2226df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2227302ce8d6SAndre Oppermann KASSERT(headlocked == 1, ("%s: dodata: " 2228302ce8d6SAndre Oppermann "TCP_FIN_WAIT_2: head not locked", __func__)); 2229340c35deSJonathan Lemon tcp_twstart(tp); 223011a20fb8SJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2231679d9708SAndre Oppermann return; 2232df8bae1dSRodney W. Grimes } 2233df8bae1dSRodney W. Grimes } 2234e0bef1cbSRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 2235e0bef1cbSRobert Watson headlocked = 0; 2236610ee2f9SDavid Greenman #ifdef TCPDEBUG 22374cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2238fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2239fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2240610ee2f9SDavid Greenman #endif 2241df8bae1dSRodney W. Grimes 2242df8bae1dSRodney W. Grimes /* 2243df8bae1dSRodney W. Grimes * Return any desired output. 2244df8bae1dSRodney W. Grimes */ 2245df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 2246df8bae1dSRodney W. Grimes (void) tcp_output(tp); 22477792ea27SJeffrey Hsu 2248a14c749fSJonathan Lemon check_delack: 2249302ce8d6SAndre Oppermann KASSERT(headlocked == 0, ("%s: check_delack: head locked", 2250302ce8d6SAndre Oppermann __func__)); 22517824d002SAndre Oppermann INP_INFO_UNLOCK_ASSERT(&tcbinfo); 2252302ce8d6SAndre Oppermann INP_LOCK_ASSERT(tp->t_inpcb); 2253a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 22543bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 2255b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 22563bfd6421SJonathan Lemon } 2257302ce8d6SAndre Oppermann INP_UNLOCK(tp->t_inpcb); 2258679d9708SAndre Oppermann return; 2259df8bae1dSRodney W. Grimes 2260df8bae1dSRodney W. Grimes dropafterack: 2261302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: dropafterack: head not locked", __func__)); 2262df8bae1dSRodney W. Grimes /* 2263df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 2264df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 226580ab7c0eSGarrett Wollman * 226680ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 226780ab7c0eSGarrett Wollman * paths to this code happen after packets containing 226880ab7c0eSGarrett Wollman * RST have been dropped. 226980ab7c0eSGarrett Wollman * 227080ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 227180ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 227280ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 227380ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 227480ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 227580ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 2276df8bae1dSRodney W. Grimes */ 2277fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2278fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 2279a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 2280a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2281a57815efSBosko Milekic goto dropwithreset; 2282a57815efSBosko Milekic } 2283a0292f23SGarrett Wollman #ifdef TCPDEBUG 22844cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2285fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2286fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2287a0292f23SGarrett Wollman #endif 2288302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: headlocked should be 1", __func__)); 228942cf3289SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 2290df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2291df8bae1dSRodney W. Grimes (void) tcp_output(tp); 2292302ce8d6SAndre Oppermann INP_UNLOCK(tp->t_inpcb); 2293d6915262SRobert Watson m_freem(m); 2294679d9708SAndre Oppermann return; 2295df8bae1dSRodney W. Grimes 2296df8bae1dSRodney W. Grimes dropwithreset: 2297302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: dropwithreset: head not locked", __func__)); 2298a57815efSBosko Milekic 2299302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 2300c29afad6SSam Leffler 23011c53f806SRobert Watson if (tp != NULL) 2302302ce8d6SAndre Oppermann INP_UNLOCK(tp->t_inpcb); 2303f76fcf6dSJeffrey Hsu if (headlocked) 2304f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2305679d9708SAndre Oppermann return; 2306df8bae1dSRodney W. Grimes 2307df8bae1dSRodney W. Grimes drop: 2308df8bae1dSRodney W. Grimes /* 2309df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 2310df8bae1dSRodney W. Grimes */ 2311610ee2f9SDavid Greenman #ifdef TCPDEBUG 23121c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2313fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2314fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2315a0292f23SGarrett Wollman #endif 23161c53f806SRobert Watson if (tp != NULL) 2317302ce8d6SAndre Oppermann INP_UNLOCK(tp->t_inpcb); 2318f76fcf6dSJeffrey Hsu if (headlocked) 2319f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2320d6915262SRobert Watson m_freem(m); 2321679d9708SAndre Oppermann return; 2322302ce8d6SAndre Oppermann } 2323302ce8d6SAndre Oppermann 2324302ce8d6SAndre Oppermann /* 23250ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 23260ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 23270ca3f933SAndre Oppermann * tp may be NULL. 2328302ce8d6SAndre Oppermann */ 2329302ce8d6SAndre Oppermann static void 2330302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 2331302ce8d6SAndre Oppermann int tlen, int rstreason) 2332302ce8d6SAndre Oppermann { 2333302ce8d6SAndre Oppermann struct ip *ip; 2334302ce8d6SAndre Oppermann #ifdef INET6 2335302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 2336302ce8d6SAndre Oppermann #endif 23370ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 2338302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2339302ce8d6SAndre Oppermann goto drop; 2340302ce8d6SAndre Oppermann #ifdef INET6 2341302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 2342302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 2343302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2344302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2345302ce8d6SAndre Oppermann goto drop; 2346302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 2347302ce8d6SAndre Oppermann } else 2348302ce8d6SAndre Oppermann #endif 2349302ce8d6SAndre Oppermann { 2350302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 2351302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2352302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2353302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 2354302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2355302ce8d6SAndre Oppermann goto drop; 2356302ce8d6SAndre Oppermann } 2357302ce8d6SAndre Oppermann 2358302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 2359302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 2360302ce8d6SAndre Oppermann goto drop; 2361302ce8d6SAndre Oppermann 2362302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 2363302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 2364302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 2365302ce8d6SAndre Oppermann th->th_ack, TH_RST); 2366302ce8d6SAndre Oppermann } else { 2367302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 2368302ce8d6SAndre Oppermann tlen++; 2369302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2370302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 2371302ce8d6SAndre Oppermann } 2372302ce8d6SAndre Oppermann return; 2373302ce8d6SAndre Oppermann drop: 2374302ce8d6SAndre Oppermann m_freem(m); 2375df8bae1dSRodney W. Grimes return; 2376df8bae1dSRodney W. Grimes } 2377df8bae1dSRodney W. Grimes 2378be2ac88cSJonathan Lemon /* 2379be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 2380be2ac88cSJonathan Lemon */ 23810312fbe9SPoul-Henning Kamp static void 2382ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 2383df8bae1dSRodney W. Grimes { 2384df8bae1dSRodney W. Grimes int opt, optlen; 2385df8bae1dSRodney W. Grimes 2386be2ac88cSJonathan Lemon to->to_flags = 0; 2387df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 2388df8bae1dSRodney W. Grimes opt = cp[0]; 2389df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 2390df8bae1dSRodney W. Grimes break; 2391df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 2392df8bae1dSRodney W. Grimes optlen = 1; 2393df8bae1dSRodney W. Grimes else { 2394b474779fSJun-ichiro itojun Hagino if (cnt < 2) 2395b474779fSJun-ichiro itojun Hagino break; 2396df8bae1dSRodney W. Grimes optlen = cp[1]; 2397b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 2398df8bae1dSRodney W. Grimes break; 2399df8bae1dSRodney W. Grimes } 2400df8bae1dSRodney W. Grimes switch (opt) { 2401df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 2402df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 2403df8bae1dSRodney W. Grimes continue; 2404f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2405df8bae1dSRodney W. Grimes continue; 2406be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 2407be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 2408be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 2409fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 2410df8bae1dSRodney W. Grimes break; 2411df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 2412df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 2413df8bae1dSRodney W. Grimes continue; 2414f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2415df8bae1dSRodney W. Grimes continue; 2416be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 241702a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 2418df8bae1dSRodney W. Grimes break; 2419df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 2420df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 2421df8bae1dSRodney W. Grimes continue; 2422be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 2423a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2424a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 2425fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 2426a0292f23SGarrett Wollman bcopy((char *)cp + 6, 2427a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2428fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 2429df8bae1dSRodney W. Grimes break; 24301cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 24311cfd4b53SBruce M Simpson /* 24321cfd4b53SBruce M Simpson * XXX In order to reply to a host which has set the 24331cfd4b53SBruce M Simpson * TCP_SIGNATURE option in its initial SYN, we have to 24341cfd4b53SBruce M Simpson * record the fact that the option was observed here 24351cfd4b53SBruce M Simpson * for the syncache code to perform the correct response. 24361cfd4b53SBruce M Simpson */ 24371cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 24381cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 24391cfd4b53SBruce M Simpson continue; 2440df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 2441df47e437SAndre Oppermann to->to_signature = cp + 2; 24421cfd4b53SBruce M Simpson break; 2443265ed012SBruce M Simpson #endif 24446d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 2445f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 24466d90faf3SPaul Saab continue; 2447f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2448f72167f4SAndre Oppermann continue; 2449f72167f4SAndre Oppermann if (!tcp_do_sack) 2450f72167f4SAndre Oppermann continue; 2451fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 24526d90faf3SPaul Saab break; 24536d90faf3SPaul Saab case TCPOPT_SACK: 24545a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 24556d90faf3SPaul Saab continue; 2456b728e902SAndre Oppermann if (flags & TO_SYN) 2457b728e902SAndre Oppermann continue; 2458fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 24595a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 24605a53ca16SPaul Saab to->to_sacks = cp + 2; 24615a53ca16SPaul Saab tcpstat.tcps_sack_rcv_blocks++; 24626d90faf3SPaul Saab break; 2463be2ac88cSJonathan Lemon default: 2464be2ac88cSJonathan Lemon continue; 2465df8bae1dSRodney W. Grimes } 2466df8bae1dSRodney W. Grimes } 2467df8bae1dSRodney W. Grimes } 2468df8bae1dSRodney W. Grimes 2469df8bae1dSRodney W. Grimes /* 2470df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 2471df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 2472df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 2473df8bae1dSRodney W. Grimes * sequencing purposes. 2474df8bae1dSRodney W. Grimes */ 24750312fbe9SPoul-Henning Kamp static void 2476ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 2477ad3f9ab3SAndre Oppermann int off) 2478df8bae1dSRodney W. Grimes { 2479fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 2480df8bae1dSRodney W. Grimes 2481df8bae1dSRodney W. Grimes while (cnt >= 0) { 2482df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 2483df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 2484df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 2485df8bae1dSRodney W. Grimes 2486df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 2487df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 2488df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2489df8bae1dSRodney W. Grimes m->m_len--; 249069a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 249169a34685SYoshinobu Inoue m->m_pkthdr.len--; 2492df8bae1dSRodney W. Grimes return; 2493df8bae1dSRodney W. Grimes } 2494df8bae1dSRodney W. Grimes cnt -= m->m_len; 2495df8bae1dSRodney W. Grimes m = m->m_next; 24964dfdffe9SAndre Oppermann if (m == NULL) 2497df8bae1dSRodney W. Grimes break; 2498df8bae1dSRodney W. Grimes } 2499df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 2500df8bae1dSRodney W. Grimes } 2501df8bae1dSRodney W. Grimes 2502df8bae1dSRodney W. Grimes /* 2503df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 2504df8bae1dSRodney W. Grimes * and update averages and current timeout. 2505df8bae1dSRodney W. Grimes */ 25060312fbe9SPoul-Henning Kamp static void 2507ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 2508df8bae1dSRodney W. Grimes { 2509ad3f9ab3SAndre Oppermann int delta; 2510233e8c18SGarrett Wollman 25112be3bf22SRobert Watson INP_LOCK_ASSERT(tp->t_inpcb); 25122be3bf22SRobert Watson 2513233e8c18SGarrett Wollman tcpstat.tcps_rttupdated++; 2514233e8c18SGarrett Wollman tp->t_rttupdated++; 2515233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 2516233e8c18SGarrett Wollman /* 2517233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 2518233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 2519233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 2520233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2521233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 2522233e8c18SGarrett Wollman */ 2523233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2524233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2525233e8c18SGarrett Wollman 2526233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 2527233e8c18SGarrett Wollman tp->t_srtt = 1; 2528233e8c18SGarrett Wollman 2529233e8c18SGarrett Wollman /* 2530233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 2531233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 2532233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 2533233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 2534233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 2535233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 2536233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2537233e8c18SGarrett Wollman * rfc793's wired-in beta. 2538233e8c18SGarrett Wollman */ 2539233e8c18SGarrett Wollman if (delta < 0) 2540233e8c18SGarrett Wollman delta = -delta; 2541233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2542233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 2543233e8c18SGarrett Wollman tp->t_rttvar = 1; 25441fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 25451fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2546233e8c18SGarrett Wollman } else { 2547233e8c18SGarrett Wollman /* 2548233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 2549233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 2550233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 2551233e8c18SGarrett Wollman */ 2552233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 2553233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 25541fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2555233e8c18SGarrett Wollman } 25569b8b58e0SJonathan Lemon tp->t_rtttime = 0; 2557df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 2558df8bae1dSRodney W. Grimes 2559df8bae1dSRodney W. Grimes /* 2560df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 2561df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 2562df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 2563df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 2564df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 2565df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 2566df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 2567df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 2568df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 2569df8bae1dSRodney W. Grimes */ 2570233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 25719e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2572df8bae1dSRodney W. Grimes 2573df8bae1dSRodney W. Grimes /* 2574df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 2575df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 2576df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 2577df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 2578df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 2579df8bae1dSRodney W. Grimes */ 2580df8bae1dSRodney W. Grimes tp->t_softerror = 0; 2581df8bae1dSRodney W. Grimes } 2582df8bae1dSRodney W. Grimes 2583df8bae1dSRodney W. Grimes /* 2584df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 2585df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 2586df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 2587df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 2588df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2589df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 2590df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 2591df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 2592df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 2593df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 2594df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 2595df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 2596df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 2597a0292f23SGarrett Wollman * 2598a0292f23SGarrett Wollman * Also take into account the space needed for options that we 2599a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 2600a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 2601a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 2602a0292f23SGarrett Wollman * data in maxopd. 2603a0292f23SGarrett Wollman * 2604a0292f23SGarrett Wollman * 2605a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 2606a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 2607a0292f23SGarrett Wollman * MSS of our peer. 260897d8d152SAndre Oppermann * 260997d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 261097d8d152SAndre Oppermann * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt(). 2611df8bae1dSRodney W. Grimes */ 2612a0292f23SGarrett Wollman void 2613ad3f9ab3SAndre Oppermann tcp_mss(struct tcpcb *tp, int offer) 2614df8bae1dSRodney W. Grimes { 261597d8d152SAndre Oppermann int rtt, mss; 2616df8bae1dSRodney W. Grimes u_long bufsize; 261797d8d152SAndre Oppermann u_long maxmtu; 2618c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 2619df8bae1dSRodney W. Grimes struct socket *so; 262097d8d152SAndre Oppermann struct hc_metrics_lite metrics; 2621a0292f23SGarrett Wollman int origoffer = offer; 2622233dcce1SAndre Oppermann int mtuflags = 0; 2623fb59c426SYoshinobu Inoue #ifdef INET6 2624c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2625c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 2626c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 2627c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 2628c068736aSJeffrey Hsu #else 2629c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 2630fb59c426SYoshinobu Inoue #endif 2631df8bae1dSRodney W. Grimes 263297d8d152SAndre Oppermann /* initialize */ 263397d8d152SAndre Oppermann #ifdef INET6 263497d8d152SAndre Oppermann if (isipv6) { 2635233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(&inp->inp_inc, &mtuflags); 263697d8d152SAndre Oppermann tp->t_maxopd = tp->t_maxseg = tcp_v6mssdflt; 263797d8d152SAndre Oppermann } else 263897d8d152SAndre Oppermann #endif 263997d8d152SAndre Oppermann { 2640233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(&inp->inp_inc, &mtuflags); 264197d8d152SAndre Oppermann tp->t_maxopd = tp->t_maxseg = tcp_mssdflt; 2642df8bae1dSRodney W. Grimes } 2643df8bae1dSRodney W. Grimes so = inp->inp_socket; 2644df8bae1dSRodney W. Grimes 264597d8d152SAndre Oppermann /* 26462d166c02SAndre Oppermann * no route to sender, stay with default mss and return 264797d8d152SAndre Oppermann */ 264897d8d152SAndre Oppermann if (maxmtu == 0) 264997d8d152SAndre Oppermann return; 265097d8d152SAndre Oppermann 265197d8d152SAndre Oppermann /* what have we got? */ 265297d8d152SAndre Oppermann switch (offer) { 265397d8d152SAndre Oppermann case 0: 265497d8d152SAndre Oppermann /* 265597d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 265697d8d152SAndre Oppermann * segment, in this case we use tcp_mssdflt. 265797d8d152SAndre Oppermann */ 265897d8d152SAndre Oppermann offer = 265997d8d152SAndre Oppermann #ifdef INET6 266097d8d152SAndre Oppermann isipv6 ? tcp_v6mssdflt : 266197d8d152SAndre Oppermann #endif 266297d8d152SAndre Oppermann tcp_mssdflt; 266397d8d152SAndre Oppermann break; 266497d8d152SAndre Oppermann 266597d8d152SAndre Oppermann case -1: 2666a0292f23SGarrett Wollman /* 2667c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 2668a0292f23SGarrett Wollman */ 266997d8d152SAndre Oppermann /* FALLTHROUGH */ 267097d8d152SAndre Oppermann 267197d8d152SAndre Oppermann default: 2672a0292f23SGarrett Wollman /* 267353369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 267453369ac9SAndre Oppermann * to at least minmss. 267553369ac9SAndre Oppermann */ 267653369ac9SAndre Oppermann offer = max(offer, tcp_minmss); 267753369ac9SAndre Oppermann /* 2678a0292f23SGarrett Wollman * Sanity check: make sure that maxopd will be large 267997d8d152SAndre Oppermann * enough to allow some data on segments even if the 2680a0292f23SGarrett Wollman * all the option space is used (40bytes). Otherwise 2681a0292f23SGarrett Wollman * funny things may happen in tcp_output. 2682a0292f23SGarrett Wollman */ 2683a0292f23SGarrett Wollman offer = max(offer, 64); 268497d8d152SAndre Oppermann } 2685a0292f23SGarrett Wollman 2686df8bae1dSRodney W. Grimes /* 268797d8d152SAndre Oppermann * rmx information is now retrieved from tcp_hostcache 2688df8bae1dSRodney W. Grimes */ 268997d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 269097d8d152SAndre Oppermann 2691df8bae1dSRodney W. Grimes /* 269297d8d152SAndre Oppermann * if there's a discovered mtu int tcp hostcache, use it 2693fb59c426SYoshinobu Inoue * else, use the link mtu. 2694df8bae1dSRodney W. Grimes */ 269597d8d152SAndre Oppermann if (metrics.rmx_mtu) 26962d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 2697c068736aSJeffrey Hsu else { 269831b3783cSHajimu UMEMOTO #ifdef INET6 2699fb59c426SYoshinobu Inoue if (isipv6) { 270097d8d152SAndre Oppermann mss = maxmtu - min_protoh; 270197d8d152SAndre Oppermann if (!path_mtu_discovery && 270297d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 2703fb59c426SYoshinobu Inoue mss = min(mss, tcp_v6mssdflt); 2704b3399803SHajimu UMEMOTO } else 2705b3399803SHajimu UMEMOTO #endif 270697d8d152SAndre Oppermann { 270797d8d152SAndre Oppermann mss = maxmtu - min_protoh; 270897d8d152SAndre Oppermann if (!path_mtu_discovery && 270997d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 2710a0292f23SGarrett Wollman mss = min(mss, tcp_mssdflt); 2711a0292f23SGarrett Wollman } 271297d8d152SAndre Oppermann } 2713a0292f23SGarrett Wollman mss = min(mss, offer); 271497d8d152SAndre Oppermann 2715a0292f23SGarrett Wollman /* 2716a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 2717a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 2718a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 2719a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 2720a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 2721a0292f23SGarrett Wollman */ 2722a0292f23SGarrett Wollman tp->t_maxopd = mss; 2723a0292f23SGarrett Wollman 2724a0292f23SGarrett Wollman /* 2725c94c54e4SAndre Oppermann * origoffer==-1 indicates, that no segments were received yet. 2726c94c54e4SAndre Oppermann * In this case we just guess. 2727a0292f23SGarrett Wollman */ 2728a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2729a0292f23SGarrett Wollman (origoffer == -1 || 2730a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2731a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 273297d8d152SAndre Oppermann tp->t_maxseg = mss; 2733a0292f23SGarrett Wollman 2734df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 2735df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2736df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 2737df8bae1dSRodney W. Grimes #else 2738df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2739df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 2740df8bae1dSRodney W. Grimes #endif 274197d8d152SAndre Oppermann tp->t_maxseg = mss; 274297d8d152SAndre Oppermann 2743df8bae1dSRodney W. Grimes /* 274497d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 274597d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 274697d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 274797d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 274897d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 2749df8bae1dSRodney W. Grimes */ 27503f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 275197d8d152SAndre Oppermann if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe) 275297d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 275397d8d152SAndre Oppermann else 2754df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 2755df8bae1dSRodney W. Grimes if (bufsize < mss) 2756df8bae1dSRodney W. Grimes mss = bufsize; 2757df8bae1dSRodney W. Grimes else { 2758df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2759df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2760df8bae1dSRodney W. Grimes bufsize = sb_max; 276188c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 27623f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 2763df8bae1dSRodney W. Grimes } 27643f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 2765df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 2766df8bae1dSRodney W. Grimes 27673f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 276897d8d152SAndre Oppermann if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe) 276997d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 277097d8d152SAndre Oppermann else 2771df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 2772df8bae1dSRodney W. Grimes if (bufsize > mss) { 2773df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2774df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2775df8bae1dSRodney W. Grimes bufsize = sb_max; 277688c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 27773f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 2778df8bae1dSRodney W. Grimes } 27793f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2780a0292f23SGarrett Wollman /* 278197d8d152SAndre Oppermann * While we're here, check the others too 2782a0292f23SGarrett Wollman */ 278397d8d152SAndre Oppermann if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 278497d8d152SAndre Oppermann tp->t_srtt = rtt; 278597d8d152SAndre Oppermann tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 278697d8d152SAndre Oppermann tcpstat.tcps_usedrtt++; 278797d8d152SAndre Oppermann if (metrics.rmx_rttvar) { 278897d8d152SAndre Oppermann tp->t_rttvar = metrics.rmx_rttvar; 278997d8d152SAndre Oppermann tcpstat.tcps_usedrttvar++; 279097d8d152SAndre Oppermann } else { 279197d8d152SAndre Oppermann /* default variation is +- 1 rtt */ 279297d8d152SAndre Oppermann tp->t_rttvar = 279397d8d152SAndre Oppermann tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 279497d8d152SAndre Oppermann } 279597d8d152SAndre Oppermann TCPT_RANGESET(tp->t_rxtcur, 279697d8d152SAndre Oppermann ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 279797d8d152SAndre Oppermann tp->t_rttmin, TCPTV_REXMTMAX); 279897d8d152SAndre Oppermann } 279997d8d152SAndre Oppermann if (metrics.rmx_ssthresh) { 2800df8bae1dSRodney W. Grimes /* 2801df8bae1dSRodney W. Grimes * There's some sort of gateway or interface 2802df8bae1dSRodney W. Grimes * buffer limit on the path. Use this to set 2803df8bae1dSRodney W. Grimes * the slow start threshhold, but set the 2804df8bae1dSRodney W. Grimes * threshold to no less than 2*mss. 2805df8bae1dSRodney W. Grimes */ 280697d8d152SAndre Oppermann tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh); 2807dd224982SGarrett Wollman tcpstat.tcps_usedssthresh++; 2808df8bae1dSRodney W. Grimes } 280997d8d152SAndre Oppermann if (metrics.rmx_bandwidth) 281097d8d152SAndre Oppermann tp->snd_bandwidth = metrics.rmx_bandwidth; 281197d8d152SAndre Oppermann 281297d8d152SAndre Oppermann /* 281397d8d152SAndre Oppermann * Set the slow-start flight size depending on whether this 281497d8d152SAndre Oppermann * is a local network or not. 281597d8d152SAndre Oppermann * 281697d8d152SAndre Oppermann * Extend this so we cache the cwnd too and retrieve it here. 281797d8d152SAndre Oppermann * Make cwnd even bigger than RFC3390 suggests but only if we 281897d8d152SAndre Oppermann * have previous experience with the remote host. Be careful 281997d8d152SAndre Oppermann * not make cwnd bigger than remote receive window or our own 282097d8d152SAndre Oppermann * send socket buffer. Maybe put some additional upper bound 282197d8d152SAndre Oppermann * on the retrieved cwnd. Should do incremental updates to 282297d8d152SAndre Oppermann * hostcache when cwnd collapses so next connection doesn't 282397d8d152SAndre Oppermann * overloads the path again. 282497d8d152SAndre Oppermann * 282597d8d152SAndre Oppermann * RFC3390 says only do this if SYN or SYN/ACK didn't got lost. 282697d8d152SAndre Oppermann * We currently check only in syncache_socket for that. 282797d8d152SAndre Oppermann */ 282897d8d152SAndre Oppermann #define TCP_METRICS_CWND 282997d8d152SAndre Oppermann #ifdef TCP_METRICS_CWND 283097d8d152SAndre Oppermann if (metrics.rmx_cwnd) 283197d8d152SAndre Oppermann tp->snd_cwnd = max(mss, 283297d8d152SAndre Oppermann min(metrics.rmx_cwnd / 2, 283397d8d152SAndre Oppermann min(tp->snd_wnd, so->so_snd.sb_hiwat))); 283497d8d152SAndre Oppermann else 283597d8d152SAndre Oppermann #endif 283697d8d152SAndre Oppermann if (tcp_do_rfc3390) 283797d8d152SAndre Oppermann tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380)); 283897d8d152SAndre Oppermann #ifdef INET6 283997d8d152SAndre Oppermann else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) || 284097d8d152SAndre Oppermann (!isipv6 && in_localaddr(inp->inp_faddr))) 2841943ae302SAndre Oppermann #else 2842943ae302SAndre Oppermann else if (in_localaddr(inp->inp_faddr)) 284397d8d152SAndre Oppermann #endif 2844943ae302SAndre Oppermann tp->snd_cwnd = mss * ss_fltsz_local; 284597d8d152SAndre Oppermann else 284697d8d152SAndre Oppermann tp->snd_cwnd = mss * ss_fltsz; 2847233dcce1SAndre Oppermann 2848233dcce1SAndre Oppermann /* Check the interface for TSO capabilities. */ 2849233dcce1SAndre Oppermann if (mtuflags & CSUM_TSO) 2850233dcce1SAndre Oppermann tp->t_flags |= TF_TSO; 2851a0292f23SGarrett Wollman } 2852a0292f23SGarrett Wollman 2853a0292f23SGarrett Wollman /* 2854a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 2855a0292f23SGarrett Wollman */ 2856a0292f23SGarrett Wollman int 2857ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 2858a0292f23SGarrett Wollman { 285997d8d152SAndre Oppermann int mss = 0; 286097d8d152SAndre Oppermann u_long maxmtu = 0; 286197d8d152SAndre Oppermann u_long thcmtu = 0; 286297d8d152SAndre Oppermann size_t min_protoh; 2863fb59c426SYoshinobu Inoue #ifdef INET6 286497d8d152SAndre Oppermann int isipv6 = inc->inc_isipv6 ? 1 : 0; 2865fb59c426SYoshinobu Inoue #endif 2866a0292f23SGarrett Wollman 286797d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 2868a0292f23SGarrett Wollman 286931b3783cSHajimu UMEMOTO #ifdef INET6 287097d8d152SAndre Oppermann if (isipv6) { 287197d8d152SAndre Oppermann mss = tcp_v6mssdflt; 2872233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 287397d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 287497d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 287597d8d152SAndre Oppermann } else 287631b3783cSHajimu UMEMOTO #endif 287797d8d152SAndre Oppermann { 287897d8d152SAndre Oppermann mss = tcp_mssdflt; 2879233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 288097d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 288197d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 288297d8d152SAndre Oppermann } 288397d8d152SAndre Oppermann if (maxmtu && thcmtu) 288497d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 288597d8d152SAndre Oppermann else if (maxmtu || thcmtu) 288697d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 288797d8d152SAndre Oppermann 288897d8d152SAndre Oppermann return (mss); 2889df8bae1dSRodney W. Grimes } 289046f58482SJonathan Lemon 289146f58482SJonathan Lemon 289246f58482SJonathan Lemon /* 2893c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 2894c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 2895c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 2896c068736aSJeffrey Hsu * be started again. 289746f58482SJonathan Lemon */ 2898c068736aSJeffrey Hsu static void 2899ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 290046f58482SJonathan Lemon { 290146f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 290246f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 290346f58482SJonathan Lemon 2904b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 290546f58482SJonathan Lemon tp->t_rtttime = 0; 290646f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 29076b2a5f92SJayanth Vijayaraghavan /* 2908c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 2909c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 29106b2a5f92SJayanth Vijayaraghavan */ 29116b2a5f92SJayanth Vijayaraghavan tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 2912a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 29136b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 291446f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 291546f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 291646f58482SJonathan Lemon tp->snd_nxt = onxt; 291746f58482SJonathan Lemon /* 291846f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 291946f58482SJonathan Lemon * not updated yet. 292046f58482SJonathan Lemon */ 2921d7587117SPaul Saab if (tp->snd_cwnd > th->th_ack - tp->snd_una) 2922d7587117SPaul Saab tp->snd_cwnd -= th->th_ack - tp->snd_una; 2923d7587117SPaul Saab else 2924d7587117SPaul Saab tp->snd_cwnd = 0; 2925d7587117SPaul Saab tp->snd_cwnd += tp->t_maxseg; 292646f58482SJonathan Lemon } 2927