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 13712e2e970SAndre Oppermann SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0, 13812e2e970SAndre Oppermann "TCP Segment Reassembly Queue"); 13912e2e970SAndre Oppermann 14012e2e970SAndre Oppermann static int tcp_reass_maxseg = 0; 14112e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RDTUN, 14212e2e970SAndre Oppermann &tcp_reass_maxseg, 0, 14312e2e970SAndre Oppermann "Global maximum number of TCP Segments in Reassembly Queue"); 14412e2e970SAndre Oppermann 14512e2e970SAndre Oppermann int tcp_reass_qsize = 0; 14612e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, cursegments, CTLFLAG_RD, 14712e2e970SAndre Oppermann &tcp_reass_qsize, 0, 14812e2e970SAndre Oppermann "Global number of TCP Segments currently in Reassembly Queue"); 14912e2e970SAndre Oppermann 15012e2e970SAndre Oppermann static int tcp_reass_maxqlen = 48; 15112e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxqlen, CTLFLAG_RW, 15212e2e970SAndre Oppermann &tcp_reass_maxqlen, 0, 15312e2e970SAndre Oppermann "Maximum number of TCP Segments per individual Reassembly Queue"); 15412e2e970SAndre Oppermann 15512e2e970SAndre Oppermann static int tcp_reass_overflows = 0; 15612e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, overflows, CTLFLAG_RD, 15712e2e970SAndre Oppermann &tcp_reass_overflows, 0, 15812e2e970SAndre Oppermann "Global number of TCP Segment Reassembly Queue Overflows"); 15912e2e970SAndre Oppermann 1606741ecf5SAndre Oppermann int tcp_do_autorcvbuf = 1; 1616741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_RW, 1626741ecf5SAndre Oppermann &tcp_do_autorcvbuf, 0, "Enable automatic receive buffer sizing"); 1636741ecf5SAndre Oppermann 1646741ecf5SAndre Oppermann int tcp_autorcvbuf_inc = 16*1024; 1656741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_RW, 1666489fe65SAndre Oppermann &tcp_autorcvbuf_inc, 0, 1676489fe65SAndre Oppermann "Incrementor step size of automatic receive buffer"); 1686741ecf5SAndre Oppermann 1696741ecf5SAndre Oppermann int tcp_autorcvbuf_max = 256*1024; 1706741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_RW, 1716741ecf5SAndre Oppermann &tcp_autorcvbuf_max, 0, "Max size of automatic receive buffer"); 1726741ecf5SAndre Oppermann 17315bd2b43SDavid Greenman struct inpcbhead tcb; 174fb59c426SYoshinobu Inoue #define tcb6 tcb /* for KAME src sync over BSD*'s */ 17515bd2b43SDavid Greenman struct inpcbinfo tcbinfo; 176df8bae1dSRodney W. Grimes 1775a53ca16SPaul Saab static void tcp_dooptions(struct tcpopt *, u_char *, int, int); 178302ce8d6SAndre Oppermann static int tcp_do_segment(struct mbuf *, struct tcphdr *, 179302ce8d6SAndre Oppermann struct socket *, struct tcpcb *, int, int); 180302ce8d6SAndre Oppermann static void tcp_dropwithreset(struct mbuf *, struct tcphdr *, 181302ce8d6SAndre Oppermann struct tcpcb *, int, int); 1824d77a549SAlfred Perlstein static void tcp_pulloutofband(struct socket *, 1834d77a549SAlfred Perlstein struct tcphdr *, struct mbuf *, int); 1844d77a549SAlfred Perlstein static int tcp_reass(struct tcpcb *, struct tcphdr *, int *, 1854d77a549SAlfred Perlstein struct mbuf *); 1864d77a549SAlfred Perlstein static void tcp_xmit_timer(struct tcpcb *, int); 187c068736aSJeffrey Hsu static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 1883cbe7fafSRobert Watson static int tcp_timewait(struct inpcb *, struct tcpopt *, 189340c35deSJonathan Lemon struct tcphdr *, struct mbuf *, int); 1900312fbe9SPoul-Henning Kamp 191fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 192fb59c426SYoshinobu Inoue #ifdef INET6 193fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \ 194fb59c426SYoshinobu Inoue do { \ 195fb59c426SYoshinobu Inoue if ((tp) && (tp)->t_inpcb && \ 19697d8d152SAndre Oppermann ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \ 19797d8d152SAndre Oppermann nd6_nud_hint(NULL, NULL, 0); \ 198fb59c426SYoshinobu Inoue } while (0) 199fb59c426SYoshinobu Inoue #else 200fb59c426SYoshinobu Inoue #define ND6_HINT(tp) 201fb59c426SYoshinobu Inoue #endif 202df8bae1dSRodney W. Grimes 203df8bae1dSRodney W. Grimes /* 204262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 205262c1c1aSMatthew Dillon * - there is no delayed ack timer in progress and 206262c1c1aSMatthew Dillon * - our last ack wasn't a 0-sized window. We never want to delay 2073bfd6421SJonathan Lemon * the ack that opens up a 0-sized window and 2083bfd6421SJonathan Lemon * - delayed acks are enabled or 2093bfd6421SJonathan Lemon * - this is a half-synchronized T/TCP connection. 210d8c85a26SJonathan Lemon */ 211d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \ 212b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 213a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 2143bfd6421SJonathan Lemon (tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 215d8c85a26SJonathan Lemon 21612e2e970SAndre Oppermann /* Initialize TCP reassembly queue */ 2174f590175SPaul Saab static void 2184f590175SPaul Saab tcp_reass_zone_change(void *tag) 2194f590175SPaul Saab { 2204f590175SPaul Saab 2214f590175SPaul Saab tcp_reass_maxseg = nmbclusters / 16; 2224f590175SPaul Saab uma_zone_set_max(tcp_reass_zone, tcp_reass_maxseg); 2234f590175SPaul Saab } 2244f590175SPaul Saab 22512e2e970SAndre Oppermann uma_zone_t tcp_reass_zone; 22612e2e970SAndre Oppermann void 22712e2e970SAndre Oppermann tcp_reass_init() 22812e2e970SAndre Oppermann { 22912e2e970SAndre Oppermann tcp_reass_maxseg = nmbclusters / 16; 23012e2e970SAndre Oppermann TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments", 23112e2e970SAndre Oppermann &tcp_reass_maxseg); 23212e2e970SAndre Oppermann tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent), 23312e2e970SAndre Oppermann NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 23412e2e970SAndre Oppermann uma_zone_set_max(tcp_reass_zone, tcp_reass_maxseg); 2354f590175SPaul Saab EVENTHANDLER_REGISTER(nmbclusters_change, 2364f590175SPaul Saab tcp_reass_zone_change, NULL, EVENTHANDLER_PRI_ANY); 23712e2e970SAndre Oppermann } 23812e2e970SAndre Oppermann 2390312fbe9SPoul-Henning Kamp static int 240ad3f9ab3SAndre Oppermann tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m) 241df8bae1dSRodney W. Grimes { 242fb59c426SYoshinobu Inoue struct tseg_qent *q; 243fb59c426SYoshinobu Inoue struct tseg_qent *p = NULL; 244fb59c426SYoshinobu Inoue struct tseg_qent *nq; 24512e2e970SAndre Oppermann struct tseg_qent *te = NULL; 246df8bae1dSRodney W. Grimes struct socket *so = tp->t_inpcb->inp_socket; 247df8bae1dSRodney W. Grimes int flags; 248df8bae1dSRodney W. Grimes 249de30ea13SRobert Watson INP_LOCK_ASSERT(tp->t_inpcb); 250de30ea13SRobert Watson 251df8bae1dSRodney W. Grimes /* 25212e2e970SAndre Oppermann * XXX: tcp_reass() is rather inefficient with its data structures 25312e2e970SAndre Oppermann * and should be rewritten (see NetBSD for optimizations). While 25412e2e970SAndre Oppermann * doing that it should move to its own file tcp_reass.c. 25512e2e970SAndre Oppermann */ 25612e2e970SAndre Oppermann 25712e2e970SAndre Oppermann /* 258de30ea13SRobert Watson * Call with th==NULL after become established to 259df8bae1dSRodney W. Grimes * force pre-ESTABLISHED data up to user socket. 260df8bae1dSRodney W. Grimes */ 261de30ea13SRobert Watson if (th == NULL) 262df8bae1dSRodney W. Grimes goto present; 263df8bae1dSRodney W. Grimes 26412e2e970SAndre Oppermann /* 26512e2e970SAndre Oppermann * Limit the number of segments in the reassembly queue to prevent 26612e2e970SAndre Oppermann * holding on to too many segments (and thus running out of mbufs). 26712e2e970SAndre Oppermann * Make sure to let the missing segment through which caused this 26812e2e970SAndre Oppermann * queue. Always keep one global queue entry spare to be able to 26912e2e970SAndre Oppermann * process the missing segment. 27012e2e970SAndre Oppermann */ 27112e2e970SAndre Oppermann if (th->th_seq != tp->rcv_nxt && 27212e2e970SAndre Oppermann (tcp_reass_qsize + 1 >= tcp_reass_maxseg || 27312e2e970SAndre Oppermann tp->t_segqlen >= tcp_reass_maxqlen)) { 27412e2e970SAndre Oppermann tcp_reass_overflows++; 27512e2e970SAndre Oppermann tcpstat.tcps_rcvmemdrop++; 27612e2e970SAndre Oppermann m_freem(m); 277e346eeffSPaul Saab *tlenp = 0; 27812e2e970SAndre Oppermann return (0); 27912e2e970SAndre Oppermann } 28012e2e970SAndre Oppermann 28112e2e970SAndre Oppermann /* 28212e2e970SAndre Oppermann * Allocate a new queue entry. If we can't, or hit the zone limit 28312e2e970SAndre Oppermann * just drop the pkt. 28412e2e970SAndre Oppermann */ 28512e2e970SAndre Oppermann te = uma_zalloc(tcp_reass_zone, M_NOWAIT); 286fb59c426SYoshinobu Inoue if (te == NULL) { 287fb59c426SYoshinobu Inoue tcpstat.tcps_rcvmemdrop++; 288fb59c426SYoshinobu Inoue m_freem(m); 289e346eeffSPaul Saab *tlenp = 0; 290fb59c426SYoshinobu Inoue return (0); 291fb59c426SYoshinobu Inoue } 29212e2e970SAndre Oppermann tp->t_segqlen++; 29312e2e970SAndre Oppermann tcp_reass_qsize++; 2946effc713SDoug Rabson 295df8bae1dSRodney W. Grimes /* 296df8bae1dSRodney W. Grimes * Find a segment which begins after this one does. 297df8bae1dSRodney W. Grimes */ 298fb59c426SYoshinobu Inoue LIST_FOREACH(q, &tp->t_segq, tqe_q) { 299fb59c426SYoshinobu Inoue if (SEQ_GT(q->tqe_th->th_seq, th->th_seq)) 300df8bae1dSRodney W. Grimes break; 301fb59c426SYoshinobu Inoue p = q; 302fb59c426SYoshinobu Inoue } 303df8bae1dSRodney W. Grimes 304df8bae1dSRodney W. Grimes /* 305df8bae1dSRodney W. Grimes * If there is a preceding segment, it may provide some of 306df8bae1dSRodney W. Grimes * our data already. If so, drop the data from the incoming 307df8bae1dSRodney W. Grimes * segment. If it provides all of our data, drop us. 308df8bae1dSRodney W. Grimes */ 3096effc713SDoug Rabson if (p != NULL) { 310ad3f9ab3SAndre Oppermann int i; 311df8bae1dSRodney W. Grimes /* conversion to int (in i) handles seq wraparound */ 312fb59c426SYoshinobu Inoue i = p->tqe_th->th_seq + p->tqe_len - th->th_seq; 313df8bae1dSRodney W. Grimes if (i > 0) { 314fb59c426SYoshinobu Inoue if (i >= *tlenp) { 315df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 316fb59c426SYoshinobu Inoue tcpstat.tcps_rcvdupbyte += *tlenp; 317df8bae1dSRodney W. Grimes m_freem(m); 31812e2e970SAndre Oppermann uma_zfree(tcp_reass_zone, te); 31912e2e970SAndre Oppermann tp->t_segqlen--; 32012e2e970SAndre Oppermann tcp_reass_qsize--; 321a0292f23SGarrett Wollman /* 322a0292f23SGarrett Wollman * Try to present any queued data 323a0292f23SGarrett Wollman * at the left window edge to the user. 324a0292f23SGarrett Wollman * This is needed after the 3-WHS 325a0292f23SGarrett Wollman * completes. 326a0292f23SGarrett Wollman */ 327a0292f23SGarrett Wollman goto present; /* ??? */ 328df8bae1dSRodney W. Grimes } 329df8bae1dSRodney W. Grimes m_adj(m, i); 330fb59c426SYoshinobu Inoue *tlenp -= i; 331fb59c426SYoshinobu Inoue th->th_seq += i; 332df8bae1dSRodney W. Grimes } 333df8bae1dSRodney W. Grimes } 334df8bae1dSRodney W. Grimes tcpstat.tcps_rcvoopack++; 335fb59c426SYoshinobu Inoue tcpstat.tcps_rcvoobyte += *tlenp; 336df8bae1dSRodney W. Grimes 337df8bae1dSRodney W. Grimes /* 338df8bae1dSRodney W. Grimes * While we overlap succeeding segments trim them or, 339df8bae1dSRodney W. Grimes * if they are completely covered, dequeue them. 340df8bae1dSRodney W. Grimes */ 3416effc713SDoug Rabson while (q) { 342ad3f9ab3SAndre Oppermann int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq; 343df8bae1dSRodney W. Grimes if (i <= 0) 344df8bae1dSRodney W. Grimes break; 345fb59c426SYoshinobu Inoue if (i < q->tqe_len) { 346fb59c426SYoshinobu Inoue q->tqe_th->th_seq += i; 347fb59c426SYoshinobu Inoue q->tqe_len -= i; 348fb59c426SYoshinobu Inoue m_adj(q->tqe_m, i); 349df8bae1dSRodney W. Grimes break; 350df8bae1dSRodney W. Grimes } 3516effc713SDoug Rabson 352fb59c426SYoshinobu Inoue nq = LIST_NEXT(q, tqe_q); 353fb59c426SYoshinobu Inoue LIST_REMOVE(q, tqe_q); 354fb59c426SYoshinobu Inoue m_freem(q->tqe_m); 35512e2e970SAndre Oppermann uma_zfree(tcp_reass_zone, q); 35612e2e970SAndre Oppermann tp->t_segqlen--; 35712e2e970SAndre Oppermann tcp_reass_qsize--; 3586effc713SDoug Rabson q = nq; 359df8bae1dSRodney W. Grimes } 360df8bae1dSRodney W. Grimes 361fb59c426SYoshinobu Inoue /* Insert the new segment queue entry into place. */ 362fb59c426SYoshinobu Inoue te->tqe_m = m; 363fb59c426SYoshinobu Inoue te->tqe_th = th; 364fb59c426SYoshinobu Inoue te->tqe_len = *tlenp; 365fb59c426SYoshinobu Inoue 3666effc713SDoug Rabson if (p == NULL) { 367fb59c426SYoshinobu Inoue LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q); 3686effc713SDoug Rabson } else { 369fb59c426SYoshinobu Inoue LIST_INSERT_AFTER(p, te, tqe_q); 3706effc713SDoug Rabson } 371df8bae1dSRodney W. Grimes 372df8bae1dSRodney W. Grimes present: 373df8bae1dSRodney W. Grimes /* 374df8bae1dSRodney W. Grimes * Present data to user, advancing rcv_nxt through 375df8bae1dSRodney W. Grimes * completed sequence space. 376df8bae1dSRodney W. Grimes */ 377a0292f23SGarrett Wollman if (!TCPS_HAVEESTABLISHED(tp->t_state)) 378df8bae1dSRodney W. Grimes return (0); 379fb59c426SYoshinobu Inoue q = LIST_FIRST(&tp->t_segq); 380fb59c426SYoshinobu Inoue if (!q || q->tqe_th->th_seq != tp->rcv_nxt) 381df8bae1dSRodney W. Grimes return (0); 3821e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 383df8bae1dSRodney W. Grimes do { 384fb59c426SYoshinobu Inoue tp->rcv_nxt += q->tqe_len; 385fb59c426SYoshinobu Inoue flags = q->tqe_th->th_flags & TH_FIN; 386fb59c426SYoshinobu Inoue nq = LIST_NEXT(q, tqe_q); 387fb59c426SYoshinobu Inoue LIST_REMOVE(q, tqe_q); 388c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 389fb59c426SYoshinobu Inoue m_freem(q->tqe_m); 3904cc20ab1SSeigo Tanimura else 3911e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, q->tqe_m); 39212e2e970SAndre Oppermann uma_zfree(tcp_reass_zone, q); 39312e2e970SAndre Oppermann tp->t_segqlen--; 39412e2e970SAndre Oppermann tcp_reass_qsize--; 3956effc713SDoug Rabson q = nq; 396fb59c426SYoshinobu Inoue } while (q && q->tqe_th->th_seq == tp->rcv_nxt); 397fb59c426SYoshinobu Inoue ND6_HINT(tp); 3981e4d7da7SRobert Watson sorwakeup_locked(so); 399df8bae1dSRodney W. Grimes return (flags); 400df8bae1dSRodney W. Grimes } 401df8bae1dSRodney W. Grimes 402df8bae1dSRodney W. Grimes /* 403df8bae1dSRodney W. Grimes * TCP input routine, follows pages 65-76 of the 404df8bae1dSRodney W. Grimes * protocol specification dated September, 1981 very closely. 405df8bae1dSRodney W. Grimes */ 406fb59c426SYoshinobu Inoue #ifdef INET6 407fb59c426SYoshinobu Inoue int 408ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto) 409fb59c426SYoshinobu Inoue { 410ad3f9ab3SAndre Oppermann struct mbuf *m = *mp; 41133841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 412fb59c426SYoshinobu Inoue 413fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 414fb59c426SYoshinobu Inoue 415fb59c426SYoshinobu Inoue /* 416fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 417fb59c426SYoshinobu Inoue * better place to put this in? 418fb59c426SYoshinobu Inoue */ 41933841545SHajimu UMEMOTO ia6 = ip6_getdstifaddr(m); 42033841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 421fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 422fb59c426SYoshinobu Inoue 423fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 424fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 425fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 426fb59c426SYoshinobu Inoue return IPPROTO_DONE; 427fb59c426SYoshinobu Inoue } 428fb59c426SYoshinobu Inoue 429f0ffb944SJulian Elischer tcp_input(m, *offp); 430fb59c426SYoshinobu Inoue return IPPROTO_DONE; 431fb59c426SYoshinobu Inoue } 432fb59c426SYoshinobu Inoue #endif 433fb59c426SYoshinobu Inoue 434df8bae1dSRodney W. Grimes void 435ad3f9ab3SAndre Oppermann tcp_input(struct mbuf *m, int off0) 436df8bae1dSRodney W. Grimes { 437ad3f9ab3SAndre Oppermann struct tcphdr *th; 438ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 439ad3f9ab3SAndre Oppermann struct ipovly *ipov; 440ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 441302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 442302ce8d6SAndre Oppermann struct socket *so = NULL; 443e79adb8eSGarrett Wollman u_char *optp = NULL; 44426f9a767SRodney W. Grimes int optlen = 0; 445a0194ef1SBruce M Simpson int len, tlen, off; 446fb59c426SYoshinobu Inoue int drop_hdrlen; 447ad3f9ab3SAndre Oppermann int thflags; 448302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 4499b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 4509b932e9eSAndre Oppermann struct m_tag *fwd_tag; 4519b932e9eSAndre Oppermann #endif 452c068736aSJeffrey Hsu #ifdef INET6 453302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 454c068736aSJeffrey Hsu int isipv6; 4551d54aa3bSBjoern A. Zeeb char ip6buf[INET6_ADDRSTRLEN]; 456c068736aSJeffrey Hsu #else 457c068736aSJeffrey Hsu const int isipv6 = 0; 458c068736aSJeffrey Hsu #endif 459302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 460f76fcf6dSJeffrey Hsu 461610ee2f9SDavid Greenman #ifdef TCPDEBUG 462c068736aSJeffrey Hsu /* 463c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 464c068736aSJeffrey Hsu * now IPv6. 465c068736aSJeffrey Hsu */ 46614739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 467410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 468610ee2f9SDavid Greenman short ostate = 0; 469610ee2f9SDavid Greenman #endif 470c068736aSJeffrey Hsu 471fb59c426SYoshinobu Inoue #ifdef INET6 472fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 473fb59c426SYoshinobu Inoue #endif 474a0292f23SGarrett Wollman 475302ce8d6SAndre Oppermann to.to_flags = 0; 476df8bae1dSRodney W. Grimes tcpstat.tcps_rcvtotal++; 477fb59c426SYoshinobu Inoue 478fb59c426SYoshinobu Inoue if (isipv6) { 47904d3a452SHajimu UMEMOTO #ifdef INET6 480fb59c426SYoshinobu Inoue /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */ 481fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 482fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 483fb59c426SYoshinobu Inoue if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 484fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbadsum++; 485fb59c426SYoshinobu Inoue goto drop; 486fb59c426SYoshinobu Inoue } 487fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 48833841545SHajimu UMEMOTO 48933841545SHajimu UMEMOTO /* 49033841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 49133841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 49233841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 49333841545SHajimu UMEMOTO * 49433841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 49533841545SHajimu UMEMOTO * already dropped in ip6_input. 49633841545SHajimu UMEMOTO */ 49733841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 49833841545SHajimu UMEMOTO /* XXX stat */ 49933841545SHajimu UMEMOTO goto drop; 50033841545SHajimu UMEMOTO } 50104d3a452SHajimu UMEMOTO #else 50204d3a452SHajimu UMEMOTO th = NULL; /* XXX: avoid compiler warning */ 50304d3a452SHajimu UMEMOTO #endif 504c068736aSJeffrey Hsu } else { 505df8bae1dSRodney W. Grimes /* 506df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 507df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 508df8bae1dSRodney W. Grimes */ 509fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 510df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 511fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 512fb59c426SYoshinobu Inoue } 513df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 5144dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 5154dfdffe9SAndre Oppermann == NULL) { 516df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 517df8bae1dSRodney W. Grimes return; 518df8bae1dSRodney W. Grimes } 519df8bae1dSRodney W. Grimes } 520fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 521fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 522db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 523db4f9cc7SJonathan Lemon tlen = ip->ip_len; 524df8bae1dSRodney W. Grimes 525db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 526db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 527db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 528db4f9cc7SJonathan Lemon else 529db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 530c068736aSJeffrey Hsu ip->ip_dst.s_addr, 531c068736aSJeffrey Hsu htonl(m->m_pkthdr.csum_data + 532c068736aSJeffrey Hsu ip->ip_len + 533c068736aSJeffrey Hsu IPPROTO_TCP)); 534db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 5353c653157SHartmut Brandt #ifdef TCPDEBUG 5363c653157SHartmut Brandt ipov->ih_len = (u_short)tlen; 5373c653157SHartmut Brandt ipov->ih_len = htons(ipov->ih_len); 5383c653157SHartmut Brandt #endif 539db4f9cc7SJonathan Lemon } else { 540df8bae1dSRodney W. Grimes /* 541df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 542df8bae1dSRodney W. Grimes */ 543df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 544fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 545fb59c426SYoshinobu Inoue ipov->ih_len = (u_short)tlen; 546fd8e4ebcSMike Barcroft ipov->ih_len = htons(ipov->ih_len); 547fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 548db4f9cc7SJonathan Lemon } 549fb59c426SYoshinobu Inoue if (th->th_sum) { 550df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadsum++; 551df8bae1dSRodney W. Grimes goto drop; 552df8bae1dSRodney W. Grimes } 553fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 554fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 555fb59c426SYoshinobu Inoue } 556df8bae1dSRodney W. Grimes 557df8bae1dSRodney W. Grimes /* 558df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 559df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 560df8bae1dSRodney W. Grimes */ 561fb59c426SYoshinobu Inoue off = th->th_off << 2; 562df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 563df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadoff++; 564df8bae1dSRodney W. Grimes goto drop; 565df8bae1dSRodney W. Grimes } 566fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 567df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 568fb59c426SYoshinobu Inoue if (isipv6) { 56904d3a452SHajimu UMEMOTO #ifdef INET6 570fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 571fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 572fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 57304d3a452SHajimu UMEMOTO #endif 574c068736aSJeffrey Hsu } else { 575df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 576c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 5774dfdffe9SAndre Oppermann == NULL) { 578df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 579df8bae1dSRodney W. Grimes return; 580df8bae1dSRodney W. Grimes } 581fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 582fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 583fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 584fb59c426SYoshinobu Inoue } 585df8bae1dSRodney W. Grimes } 586df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 587fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 588df8bae1dSRodney W. Grimes } 589fb59c426SYoshinobu Inoue thflags = th->th_flags; 590df8bae1dSRodney W. Grimes 591e46cd3d4SDag-Erling Smørgrav /* 592e46cd3d4SDag-Erling Smørgrav * If the drop_synfin option is enabled, drop all packets with 593e46cd3d4SDag-Erling Smørgrav * both the SYN and FIN bits set. This prevents e.g. nmap from 594e46cd3d4SDag-Erling Smørgrav * identifying the TCP/IP stack. 595e46cd3d4SDag-Erling Smørgrav * 596a589a70eSGarrett Wollman * This is a violation of the TCP specification. 597e46cd3d4SDag-Erling Smørgrav */ 598fb59c426SYoshinobu Inoue if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN)) 599e46cd3d4SDag-Erling Smørgrav goto drop; 600e46cd3d4SDag-Erling Smørgrav 601df8bae1dSRodney W. Grimes /* 602df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 603df8bae1dSRodney W. Grimes */ 604fd8e4ebcSMike Barcroft th->th_seq = ntohl(th->th_seq); 605fd8e4ebcSMike Barcroft th->th_ack = ntohl(th->th_ack); 606fd8e4ebcSMike Barcroft th->th_win = ntohs(th->th_win); 607fd8e4ebcSMike Barcroft th->th_urp = ntohs(th->th_urp); 608df8bae1dSRodney W. Grimes 609df8bae1dSRodney W. Grimes /* 610794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 611755c1f07SAndras Olah */ 612fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 613755c1f07SAndras Olah 614755c1f07SAndras Olah /* 615df8bae1dSRodney W. Grimes * Locate pcb for segment. 616df8bae1dSRodney W. Grimes */ 617f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 618df8bae1dSRodney W. Grimes findpcb: 619302ce8d6SAndre Oppermann INP_INFO_WLOCK_ASSERT(&tcbinfo); 6209b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 6219b932e9eSAndre Oppermann /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */ 6229b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 6239b932e9eSAndre Oppermann 6249b932e9eSAndre Oppermann if (fwd_tag != NULL && isipv6 == 0) { /* IPv6 support is not yet */ 6259b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 6269b932e9eSAndre Oppermann 6279b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 628f9e354dfSJulian Elischer /* 6292b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 630f9e354dfSJulian Elischer * already got one like this? 631f9e354dfSJulian Elischer */ 6329b932e9eSAndre Oppermann inp = in_pcblookup_hash(&tcbinfo, 6339b932e9eSAndre Oppermann ip->ip_src, th->th_sport, 634c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 635c068736aSJeffrey Hsu 0, m->m_pkthdr.rcvif); 636f9e354dfSJulian Elischer if (!inp) { 6379b932e9eSAndre Oppermann /* It's new. Try to find the ambushing socket. */ 638f9e354dfSJulian Elischer inp = in_pcblookup_hash(&tcbinfo, 639fb59c426SYoshinobu Inoue ip->ip_src, th->th_sport, 6402b25acc1SLuigi Rizzo next_hop->sin_addr, 641c068736aSJeffrey Hsu next_hop->sin_port ? 642c068736aSJeffrey Hsu ntohs(next_hop->sin_port) : 643c068736aSJeffrey Hsu th->th_dport, 644421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 645421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 646f9e354dfSJulian Elischer } 6479b932e9eSAndre Oppermann /* Remove the tag from the packet. We don't need it anymore. */ 6489b932e9eSAndre Oppermann m_tag_delete(m, fwd_tag); 649b10fbdeaSAndre Oppermann } else 6509b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 651b10fbdeaSAndre Oppermann { 65204d3a452SHajimu UMEMOTO if (isipv6) { 65304d3a452SHajimu UMEMOTO #ifdef INET6 654c068736aSJeffrey Hsu inp = in6_pcblookup_hash(&tcbinfo, 655c068736aSJeffrey Hsu &ip6->ip6_src, th->th_sport, 656c068736aSJeffrey Hsu &ip6->ip6_dst, th->th_dport, 657421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 658421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 65904d3a452SHajimu UMEMOTO #endif 66004d3a452SHajimu UMEMOTO } else 661c068736aSJeffrey Hsu inp = in_pcblookup_hash(&tcbinfo, 662c068736aSJeffrey Hsu ip->ip_src, th->th_sport, 663c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 664421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 665421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 666fb59c426SYoshinobu Inoue } 667f9e354dfSJulian Elischer 668da0f4099SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC) 669d420fcdaSBruce M Simpson #ifdef INET6 6704dfdffe9SAndre Oppermann if (isipv6 && inp != NULL && ipsec6_in_reject(m, inp)) { 671fb59c426SYoshinobu Inoue #ifdef IPSEC 672fb59c426SYoshinobu Inoue ipsec6stat.in_polvio++; 673d420fcdaSBruce M Simpson #endif 674302ce8d6SAndre Oppermann goto dropunlock; 675d420fcdaSBruce M Simpson } else 676d420fcdaSBruce M Simpson #endif /* INET6 */ 677d420fcdaSBruce M Simpson if (inp != NULL && ipsec4_in_reject(m, inp)) { 678da0f4099SHajimu UMEMOTO #ifdef IPSEC 679fb59c426SYoshinobu Inoue ipsecstat.in_polvio++; 680d420fcdaSBruce M Simpson #endif 681302ce8d6SAndre Oppermann goto dropunlock; 682fb59c426SYoshinobu Inoue } 683da0f4099SHajimu UMEMOTO #endif /*IPSEC || FAST_IPSEC*/ 684df8bae1dSRodney W. Grimes 685df8bae1dSRodney W. Grimes /* 686574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 687574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 688df8bae1dSRodney W. Grimes */ 689816a3d83SPoul-Henning Kamp if (inp == NULL) { 690574b6964SAndre Oppermann /* 691574b6964SAndre Oppermann * Log communication attempts to ports that are not 692574b6964SAndre Oppermann * in use. 693574b6964SAndre Oppermann */ 694574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 695574b6964SAndre Oppermann tcp_log_in_vain == 2) { 696574b6964SAndre Oppermann #ifndef INET6 697fb59c426SYoshinobu Inoue char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"]; 698574b6964SAndre Oppermann #else 699574b6964SAndre Oppermann char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2]; 700fb59c426SYoshinobu Inoue if (isipv6) { 701ded7008aSJuli Mallett strcpy(dbuf, "["); 7021d54aa3bSBjoern A. Zeeb strcat(dbuf, 7031d54aa3bSBjoern A. Zeeb ip6_sprintf(ip6buf, &ip6->ip6_dst)); 704574b6964SAndre Oppermann strcat(dbuf, "]"); 705574b6964SAndre Oppermann strcpy(sbuf, "["); 7061d54aa3bSBjoern A. Zeeb strcat(sbuf, 7071d54aa3bSBjoern A. Zeeb ip6_sprintf(ip6buf, &ip6->ip6_src)); 708ded7008aSJuli Mallett strcat(sbuf, "]"); 709574b6964SAndre Oppermann } else 710574b6964SAndre Oppermann #endif /* INET6 */ 711574b6964SAndre Oppermann { 712fb59c426SYoshinobu Inoue strcpy(dbuf, inet_ntoa(ip->ip_dst)); 713fb59c426SYoshinobu Inoue strcpy(sbuf, inet_ntoa(ip->ip_src)); 714fb59c426SYoshinobu Inoue } 7152e4e1b4cSGeoff Rehmet log(LOG_INFO, 716c068736aSJeffrey Hsu "Connection attempt to TCP %s:%d " 71739eb27a4SCrist J. Clark "from %s:%d flags:0x%02x\n", 718fb59c426SYoshinobu Inoue dbuf, ntohs(th->th_dport), sbuf, 719fb59c426SYoshinobu Inoue ntohs(th->th_sport), thflags); 7202e4e1b4cSGeoff Rehmet } 721574b6964SAndre Oppermann /* 722574b6964SAndre Oppermann * When blackholing do not respond with a RST but 723574b6964SAndre Oppermann * completely ignore the segment and drop it. 724574b6964SAndre Oppermann */ 725574b6964SAndre Oppermann if ((blackhole == 1 && (thflags & TH_SYN)) || 726574b6964SAndre Oppermann blackhole == 2) 7271929eae1SAndre Oppermann goto dropunlock; 728574b6964SAndre Oppermann 729a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 730a57815efSBosko Milekic goto dropwithreset; 731816a3d83SPoul-Henning Kamp } 732f76fcf6dSJeffrey Hsu INP_LOCK(inp); 733936cd18dSAndre Oppermann 734936cd18dSAndre Oppermann /* Check the minimum TTL for socket. */ 73534f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 73634f83c52SGeorge V. Neville-Neil #ifdef INET6 73734f83c52SGeorge V. Neville-Neil if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim) 738302ce8d6SAndre Oppermann goto dropunlock; 73902707462SAndre Oppermann else 74034f83c52SGeorge V. Neville-Neil #endif 74134f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 742302ce8d6SAndre Oppermann goto dropunlock; 74334f83c52SGeorge V. Neville-Neil } 744936cd18dSAndre Oppermann 745340c35deSJonathan Lemon /* 746794235b7SAndre Oppermann * A previous connection in TIMEWAIT state is supposed to catch 747794235b7SAndre Oppermann * stray or duplicate segments arriving late. If this segment 748794235b7SAndre Oppermann * was a legitimate new connection attempt the old INPCB gets 749794235b7SAndre Oppermann * removed and we can try again to find a listening socket. 750340c35deSJonathan Lemon */ 751794235b7SAndre Oppermann if (inp->inp_vflag & INP_TIMEWAIT) { 752340c35deSJonathan Lemon if (thflags & TH_SYN) 753f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 7549fa198beSAndre Oppermann /* NB: tcp_timewait unlocks the INP and frees the mbuf. */ 7553cbe7fafSRobert Watson if (tcp_timewait(inp, &to, th, m, tlen)) 756340c35deSJonathan Lemon goto findpcb; 757340c35deSJonathan Lemon INP_INFO_WUNLOCK(&tcbinfo); 758340c35deSJonathan Lemon return; 759340c35deSJonathan Lemon } 760794235b7SAndre Oppermann /* 761794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 762794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 763794235b7SAndre Oppermann * segment and send an appropriate response. 764794235b7SAndre Oppermann */ 765df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 7664dfdffe9SAndre Oppermann if (tp == NULL) { 767a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 768a57815efSBosko Milekic goto dropwithreset; 76909f81a46SBosko Milekic } 770df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_CLOSED) 771302ce8d6SAndre Oppermann goto dropunlock; /* XXX: dropwithreset??? */ 772df8bae1dSRodney W. Grimes 773c488362eSRobert Watson #ifdef MAC 7741f82efb3SRobert Watson INP_LOCK_ASSERT(inp); 775a557af22SRobert Watson if (mac_check_inpcb_deliver(inp, m)) 776302ce8d6SAndre Oppermann goto dropunlock; 777c488362eSRobert Watson #endif 778a557af22SRobert Watson so = inp->inp_socket; 779302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 780610ee2f9SDavid Greenman #ifdef TCPDEBUG 781df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 782df8bae1dSRodney W. Grimes ostate = tp->t_state; 783fb59c426SYoshinobu Inoue if (isipv6) 784540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 785fb59c426SYoshinobu Inoue else 786540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 787fb59c426SYoshinobu Inoue tcp_savetcp = *th; 788df8bae1dSRodney W. Grimes } 789610ee2f9SDavid Greenman #endif 790db33b3e6SAndre Oppermann /* 791db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 792db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 793db33b3e6SAndre Oppermann * attempt or the completion of a previous one. 794db33b3e6SAndre Oppermann */ 795540e8b7eSJeffrey Hsu if (so->so_options & SO_ACCEPTCONN) { 796540e8b7eSJeffrey Hsu struct in_conninfo inc; 797540e8b7eSJeffrey Hsu 7987824d002SAndre Oppermann KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but " 7997824d002SAndre Oppermann "tp not listening", __func__)); 8007824d002SAndre Oppermann 8018bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 802be2ac88cSJonathan Lemon inc.inc_isipv6 = isipv6; 803302ce8d6SAndre Oppermann #ifdef INET6 804be2ac88cSJonathan Lemon if (isipv6) { 805be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 806be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 807302ce8d6SAndre Oppermann } else 808302ce8d6SAndre Oppermann #endif 809302ce8d6SAndre Oppermann { 810be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 811be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 812be2ac88cSJonathan Lemon } 813be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 814be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 815be2ac88cSJonathan Lemon 816fb59c426SYoshinobu Inoue /* 817be2ac88cSJonathan Lemon * If the state is LISTEN then ignore segment if it contains 818be2ac88cSJonathan Lemon * a RST. If the segment contains an ACK then it is bad and 819be2ac88cSJonathan Lemon * send a RST. If it does not contain a SYN then it is not 820be2ac88cSJonathan Lemon * interesting; drop it. 821be2ac88cSJonathan Lemon * 822be2ac88cSJonathan Lemon * If the state is SYN_RECEIVED (syncache) and seg contains 823be2ac88cSJonathan Lemon * an ACK, but not for our SYN/ACK, send a RST. If the seg 824be2ac88cSJonathan Lemon * contains a RST, check the sequence number to see if it 825be2ac88cSJonathan Lemon * is a valid reset segment. 826fb59c426SYoshinobu Inoue */ 827fb59c426SYoshinobu Inoue if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 828be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 829bf6d304aSAndre Oppermann /* 830bf6d304aSAndre Oppermann * Parse the TCP options here because 831bf6d304aSAndre Oppermann * syncookies need access to the reflected 832bf6d304aSAndre Oppermann * timestamp. 833bf6d304aSAndre Oppermann */ 834bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 8359fa198beSAndre Oppermann /* 8369fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 8379fa198beSAndre Oppermann * inp and tcpinfo locks. 8389fa198beSAndre Oppermann */ 839bf6d304aSAndre Oppermann if (!syncache_expand(&inc, &to, th, &so, m)) { 8404195b4afSPaul Traina /* 841e207f800SAndre Oppermann * No syncache entry or ACK was not 842be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 8434195b4afSPaul Traina */ 844be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 845be2ac88cSJonathan Lemon goto dropwithreset; 846be2ac88cSJonathan Lemon } 847f76fcf6dSJeffrey Hsu if (so == NULL) { 848be2ac88cSJonathan Lemon /* 849e207f800SAndre Oppermann * We completed the 3-way handshake 850e207f800SAndre Oppermann * but could not allocate a socket 851e207f800SAndre Oppermann * either due to memory shortage, 852e207f800SAndre Oppermann * listen queue length limits or 853e207f800SAndre Oppermann * global socket limits. 854be2ac88cSJonathan Lemon */ 855e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 856e207f800SAndre Oppermann goto dropwithreset; 857f76fcf6dSJeffrey Hsu } 858be2ac88cSJonathan Lemon /* 859be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 860be2ac88cSJonathan Lemon * Continue processing segment. 861be2ac88cSJonathan Lemon */ 8620c38fd0aSAndre Oppermann INP_UNLOCK(inp); /* listen socket */ 863be2ac88cSJonathan Lemon inp = sotoinpcb(so); 8640c38fd0aSAndre Oppermann INP_LOCK(inp); /* new connection */ 865be2ac88cSJonathan Lemon tp = intotcpcb(inp); 866be2ac88cSJonathan Lemon /* 867302ce8d6SAndre Oppermann * Process the segment and the data it 868302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 869302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 870302ce8d6SAndre Oppermann * XXX: The potential return value of 871302ce8d6SAndre Oppermann * TIME_WAIT nuked is supposed to be 872302ce8d6SAndre Oppermann * handled above. 873302ce8d6SAndre Oppermann */ 874302ce8d6SAndre Oppermann if (tcp_do_segment(m, th, so, tp, 875302ce8d6SAndre Oppermann drop_hdrlen, tlen)) 876302ce8d6SAndre Oppermann goto findpcb; /* TIME_WAIT nuked */ 877302ce8d6SAndre Oppermann return; 878be2ac88cSJonathan Lemon } 879be2ac88cSJonathan Lemon if (thflags & TH_RST) { 880be2ac88cSJonathan Lemon syncache_chkrst(&inc, th); 881302ce8d6SAndre Oppermann goto dropunlock; 882be2ac88cSJonathan Lemon } 883fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 884be2ac88cSJonathan Lemon syncache_badack(&inc); 885ebb0cbeaSPaul Traina tcpstat.tcps_badsyn++; 886a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 887a57815efSBosko Milekic goto dropwithreset; 8884195b4afSPaul Traina } 889302ce8d6SAndre Oppermann goto dropunlock; 890ebb0cbeaSPaul Traina } 89133841545SHajimu UMEMOTO 892be2ac88cSJonathan Lemon /* 893be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 894be2ac88cSJonathan Lemon */ 89533841545SHajimu UMEMOTO #ifdef INET6 89633841545SHajimu UMEMOTO /* 89733841545SHajimu UMEMOTO * If deprecated address is forbidden, 89833841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 89933841545SHajimu UMEMOTO * address to prevent any new inbound connection from 90033841545SHajimu UMEMOTO * getting established. 90133841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 90233841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 90333841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 90433841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 90533841545SHajimu UMEMOTO * for the exchange. 90633841545SHajimu UMEMOTO * 90733841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 90833841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 90933841545SHajimu UMEMOTO * SYN in this case. 91033841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 91133841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 91233841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 91333841545SHajimu UMEMOTO * used" 91433841545SHajimu UMEMOTO * 2. use of it with new communication: 91533841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 91633841545SHajimu UMEMOTO * with sufficient scope is available" 91733841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 91833841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 91933841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 92033841545SHajimu UMEMOTO * 92133841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 92233841545SHajimu UMEMOTO * multiple description text for deprecated address 92333841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 92433841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 92533841545SHajimu UMEMOTO */ 92633841545SHajimu UMEMOTO if (isipv6 && !ip6_use_deprecated) { 92733841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 92833841545SHajimu UMEMOTO 92933841545SHajimu UMEMOTO if ((ia6 = ip6_getdstifaddr(m)) && 93033841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 93133841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 93233841545SHajimu UMEMOTO goto dropwithreset; 93333841545SHajimu UMEMOTO } 93433841545SHajimu UMEMOTO } 93533841545SHajimu UMEMOTO #endif 93665f28919SJesper Skriver /* 937db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 938db33b3e6SAndre Oppermann * 939db33b3e6SAndre Oppermann * Don't bother responding if the destination was a 940db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 941db33b3e6SAndre Oppermann * 942be2ac88cSJonathan Lemon * If it is from this socket, drop it, it must be forged. 9432ca2159fSCrist J. Clark * 94493ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 94593ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 94693ec91baSCrist J. Clark * in_broadcast() to find them. 947be2ac88cSJonathan Lemon */ 948be2ac88cSJonathan Lemon if (m->m_flags & (M_BCAST|M_MCAST)) 949302ce8d6SAndre Oppermann goto dropunlock; 950be2ac88cSJonathan Lemon if (isipv6) { 951db33b3e6SAndre Oppermann #ifdef INET6 952db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 953db33b3e6SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) 954302ce8d6SAndre Oppermann goto dropunlock; 955be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 956be2ac88cSJonathan Lemon IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 957302ce8d6SAndre Oppermann goto dropunlock; 958db33b3e6SAndre Oppermann #endif 959c068736aSJeffrey Hsu } else { 960db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 961db33b3e6SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) 962302ce8d6SAndre Oppermann goto dropunlock; 963be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 964be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 9652ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 9662ca2159fSCrist J. Clark in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 967302ce8d6SAndre Oppermann goto dropunlock; 968c068736aSJeffrey Hsu } 969be2ac88cSJonathan Lemon /* 970db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 971db33b3e6SAndre Oppermann * for syncache. 972be2ac88cSJonathan Lemon */ 9733c653157SHartmut Brandt #ifdef TCPDEBUG 9743c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 9753c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 9763c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 9773c653157SHartmut Brandt #endif 978f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 9794d6e7130SAndre Oppermann syncache_add(&inc, &to, th, inp, &so, m); 980be2ac88cSJonathan Lemon /* 9814d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 9824d6e7130SAndre Oppermann * Everything unlocked already by syncache_add(). 983be2ac88cSJonathan Lemon */ 984be2ac88cSJonathan Lemon return; 985f76fcf6dSJeffrey Hsu } 986db33b3e6SAndre Oppermann 987302ce8d6SAndre Oppermann /* 988302ce8d6SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or 989302ce8d6SAndre Oppermann * later state. tcp_do_segment() always consumes the mbuf chain 990302ce8d6SAndre Oppermann * and unlocks the inpcb. 991302ce8d6SAndre Oppermann */ 992302ce8d6SAndre Oppermann if (tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen)) 993302ce8d6SAndre Oppermann goto findpcb; /* XXX: TIME_WAIT was nuked. */ 994302ce8d6SAndre Oppermann return; 995be2ac88cSJonathan Lemon 996302ce8d6SAndre Oppermann dropwithreset: 997995a7717SAndre Oppermann INP_INFO_WLOCK_ASSERT(&tcbinfo); 998302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 999302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 1000302ce8d6SAndre Oppermann dropunlock: 1001995a7717SAndre Oppermann INP_INFO_WLOCK_ASSERT(&tcbinfo); 10029fa198beSAndre Oppermann if (inp != NULL) 1003302ce8d6SAndre Oppermann INP_UNLOCK(inp); 1004302ce8d6SAndre Oppermann INP_INFO_WUNLOCK(&tcbinfo); 1005302ce8d6SAndre Oppermann drop: 1006995a7717SAndre Oppermann INP_INFO_UNLOCK_ASSERT(&tcbinfo); 1007302ce8d6SAndre Oppermann if (m != NULL) 1008302ce8d6SAndre Oppermann m_freem(m); 1009302ce8d6SAndre Oppermann return; 1010302ce8d6SAndre Oppermann } 1011302ce8d6SAndre Oppermann 1012302ce8d6SAndre Oppermann static int 1013302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 1014302ce8d6SAndre Oppermann struct tcpcb *tp, int drop_hdrlen, int tlen) 1015302ce8d6SAndre Oppermann { 1016302ce8d6SAndre Oppermann int thflags, acked, ourfinisacked, needoutput = 0; 1017302ce8d6SAndre Oppermann int headlocked = 1; 1018302ce8d6SAndre Oppermann int rstreason, todrop, win; 1019302ce8d6SAndre Oppermann u_long tiwin; 1020302ce8d6SAndre Oppermann struct tcpopt to; 1021302ce8d6SAndre Oppermann 102214739780SMaxim Konovalov #ifdef TCPDEBUG 102314739780SMaxim Konovalov /* 102414739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 102514739780SMaxim Konovalov * now IPv6. 102614739780SMaxim Konovalov */ 102714739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 102814739780SMaxim Konovalov struct tcphdr tcp_savetcp; 102914739780SMaxim Konovalov short ostate = 0; 103014739780SMaxim Konovalov #endif 1031302ce8d6SAndre Oppermann thflags = th->th_flags; 1032302ce8d6SAndre Oppermann 1033302ce8d6SAndre Oppermann INP_INFO_WLOCK_ASSERT(&tcbinfo); 1034302ce8d6SAndre Oppermann INP_LOCK_ASSERT(tp->t_inpcb); 1035302ce8d6SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", __func__)); 1036df8bae1dSRodney W. Grimes 1037df8bae1dSRodney W. Grimes /* 1038df8bae1dSRodney W. Grimes * Segment received on connection. 1039df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1040df8bae1dSRodney W. Grimes */ 10419b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 10427ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 1043b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 1044df8bae1dSRodney W. Grimes 1045df8bae1dSRodney W. Grimes /* 1046464fcfbcSAndre Oppermann * Unscale the window into a 32-bit value. 1047464fcfbcSAndre Oppermann * This value is bogus for the TCPS_SYN_SENT state 1048464fcfbcSAndre Oppermann * and is overwritten later. 1049464fcfbcSAndre Oppermann */ 1050464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 1051464fcfbcSAndre Oppermann 1052464fcfbcSAndre Oppermann /* 1053f72167f4SAndre Oppermann * Parse options on any incoming segment. 1054f72167f4SAndre Oppermann */ 1055302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1056302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1057302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1058f72167f4SAndre Oppermann 1059f72167f4SAndre Oppermann /* 1060f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1061bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1062bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1063bf6d304aSAndre Oppermann * was established. 1064f72167f4SAndre Oppermann */ 1065bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1066e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 1067bf6d304aSAndre Oppermann if (TSTMP_GT(to.to_tsecr, ticks)) 1068f72167f4SAndre Oppermann to.to_tsecr = 0; 1069bf6d304aSAndre Oppermann } 1070f72167f4SAndre Oppermann 1071f72167f4SAndre Oppermann /* 107297d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 107397d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 107497d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1075df8bae1dSRodney W. Grimes */ 10760a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1077464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 1078464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 1079be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 108002a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 1081464fcfbcSAndre Oppermann tp->snd_wnd = th->th_win << tp->snd_scale; 1082464fcfbcSAndre Oppermann tiwin = tp->snd_wnd; 1083be2ac88cSJonathan Lemon } 1084be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1085be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1086be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1087be2ac88cSJonathan Lemon tp->ts_recent_age = ticks; 1088be2ac88cSJonathan Lemon } 10894a32dc29SMohan Srinivasan /* Initial send window, already scaled. */ 10904a32dc29SMohan Srinivasan tp->snd_wnd = th->th_win; 1091be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1092be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 10936d90faf3SPaul Saab if (tp->sack_enable) { 1094fc30a251SAndre Oppermann if (!(to.to_flags & TOF_SACKPERM)) 10956d90faf3SPaul Saab tp->sack_enable = 0; 10966d90faf3SPaul Saab else 10976d90faf3SPaul Saab tp->t_flags |= TF_SACK_PERMIT; 10986d90faf3SPaul Saab } 10996d90faf3SPaul Saab 11006d90faf3SPaul Saab } 11016d90faf3SPaul Saab 1102df8bae1dSRodney W. Grimes /* 1103df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1104df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1105df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1106df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1107df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1108df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1109df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1110df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1111df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1112df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1113df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1114df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1115a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1116a0292f23SGarrett Wollman * Since we check for TCPS_ESTABLISHED above, it can only 1117a0292f23SGarrett Wollman * be TH_NEEDSYN. 1118df8bae1dSRodney W. Grimes */ 1119df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1120fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1121a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1122be2ac88cSJonathan Lemon ((to.to_flags & TOF_TS) == 0 || 1123a0292f23SGarrett Wollman TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 1124c94c54e4SAndre Oppermann th->th_seq == tp->rcv_nxt && tiwin && tiwin == tp->snd_wnd && 1125df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) { 1126df8bae1dSRodney W. Grimes 1127df8bae1dSRodney W. Grimes /* 1128df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1129df8bae1dSRodney W. Grimes * record the timestamp. 1130a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1131a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1132df8bae1dSRodney W. Grimes */ 1133be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1134fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 11359b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1136a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1137df8bae1dSRodney W. Grimes } 1138df8bae1dSRodney W. Grimes 1139fb59c426SYoshinobu Inoue if (tlen == 0) { 1140fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1141fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1142233e8c18SGarrett Wollman tp->snd_cwnd >= tp->snd_wnd && 11436d90faf3SPaul Saab ((!tcp_do_newreno && !tp->sack_enable && 1144cb942153SJeffrey Hsu tp->t_dupacks < tcprexmtthresh) || 11456d90faf3SPaul Saab ((tcp_do_newreno || tp->sack_enable) && 1146fc30a251SAndre Oppermann !IN_FASTRECOVERY(tp) && 1147fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1148482ac968SPaul Saab TAILQ_EMPTY(&tp->snd_holes)))) { 1149302ce8d6SAndre Oppermann KASSERT(headlocked, 1150302ce8d6SAndre Oppermann ("%s: headlocked", __func__)); 1151e0bef1cbSRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 1152e0bef1cbSRobert Watson headlocked = 0; 1153df8bae1dSRodney W. Grimes /* 1154df8bae1dSRodney W. Grimes * this is a pure ack for outstanding data. 1155df8bae1dSRodney W. Grimes */ 1156df8bae1dSRodney W. Grimes ++tcpstat.tcps_predack; 11579b8b58e0SJonathan Lemon /* 11589b8b58e0SJonathan Lemon * "bad retransmit" recovery 11599b8b58e0SJonathan Lemon */ 11609b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 11619b8b58e0SJonathan Lemon ticks < tp->t_badrxtwin) { 1162cb942153SJeffrey Hsu ++tcpstat.tcps_sndrexmitbad; 11639b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 11649b8b58e0SJonathan Lemon tp->snd_ssthresh = 11659b8b58e0SJonathan Lemon tp->snd_ssthresh_prev; 11669d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 11679d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 11689d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 11699b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 11709b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; 11719b8b58e0SJonathan Lemon } 1172fa55172bSMatthew Dillon 1173fa55172bSMatthew Dillon /* 1174fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1175fa55172bSMatthew Dillon * 1176fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1177fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1178fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1179fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1180fa55172bSMatthew Dillon */ 1181fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1182fa55172bSMatthew Dillon to.to_tsecr) { 1183eaf80179SAndre Oppermann if (!tp->t_rttlow || 1184eaf80179SAndre Oppermann tp->t_rttlow > ticks - to.to_tsecr) 1185eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 1186a0292f23SGarrett Wollman tcp_xmit_timer(tp, 11879b8b58e0SJonathan Lemon ticks - to.to_tsecr + 1); 1188fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1189fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1190eaf80179SAndre Oppermann if (!tp->t_rttlow || 1191eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1192eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1193c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1194c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1195fa55172bSMatthew Dillon } 11961fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 1197fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 1198df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 1199df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 1200df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 12019d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 12029d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 12039d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 12049d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 12051645d090SJeff Roberson /* 12061645d090SJeff Roberson * pull snd_wl2 up to prevent seq wrap relative 12071645d090SJeff Roberson * to th_ack. 12081645d090SJeff Roberson */ 1209cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1210b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1211df8bae1dSRodney W. Grimes m_freem(m); 1212fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 1213df8bae1dSRodney W. Grimes 1214df8bae1dSRodney W. Grimes /* 1215df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1216df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1217df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1218df8bae1dSRodney W. Grimes * If process is waiting for space, 1219df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1220df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1221df8bae1dSRodney W. Grimes * decide between more output or persist. 12223c653157SHartmut Brandt 12233c653157SHartmut Brandt #ifdef TCPDEBUG 12243c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 12253c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 12263c653157SHartmut Brandt (void *)tcp_saveipgen, 12273c653157SHartmut Brandt &tcp_savetcp, 0); 12283c653157SHartmut Brandt #endif 1229df8bae1dSRodney W. Grimes */ 1230df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1231b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1232b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1233b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1234b8152ba7SAndre Oppermann tp->t_rxtcur); 1235df8bae1dSRodney W. Grimes 1236df8bae1dSRodney W. Grimes sowwakeup(so); 1237df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 1238df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1239a14c749fSJonathan Lemon goto check_delack; 1240df8bae1dSRodney W. Grimes } 1241fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1242fb59c426SYoshinobu Inoue LIST_EMPTY(&tp->t_segq) && 1243fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 12446741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 12456741ecf5SAndre Oppermann 1246302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: headlocked", __func__)); 1247e0bef1cbSRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 1248e0bef1cbSRobert Watson headlocked = 0; 1249df8bae1dSRodney W. Grimes /* 1250df8bae1dSRodney W. Grimes * this is a pure, in-sequence data packet 1251df8bae1dSRodney W. Grimes * with nothing on the reassembly queue and 1252df8bae1dSRodney W. Grimes * we have enough buffer space to take it. 1253df8bae1dSRodney W. Grimes */ 12546d90faf3SPaul Saab /* Clean receiver SACK report if present */ 12556d90faf3SPaul Saab if (tp->sack_enable && tp->rcv_numsacks) 12566d90faf3SPaul Saab tcp_clean_sackreport(tp); 1257df8bae1dSRodney W. Grimes ++tcpstat.tcps_preddat; 1258fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 12591645d090SJeff Roberson /* 12601645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 12611645d090SJeff Roberson * th_seq. 12621645d090SJeff Roberson */ 12631645d090SJeff Roberson tp->snd_wl1 = th->th_seq; 12641645d090SJeff Roberson /* 12651645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 12661645d090SJeff Roberson * rcv_nxt. 12671645d090SJeff Roberson */ 12681645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 1269df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpack++; 1270fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyte += tlen; 1271fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 12723c653157SHartmut Brandt #ifdef TCPDEBUG 12733c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 12743c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 12753c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 12763c653157SHartmut Brandt #endif 12776741ecf5SAndre Oppermann /* 12786741ecf5SAndre Oppermann * Automatic sizing of receive socket buffer. Often the send 12796741ecf5SAndre Oppermann * buffer size is not optimally adjusted to the actual network 12806741ecf5SAndre Oppermann * conditions at hand (delay bandwidth product). Setting the 12816741ecf5SAndre Oppermann * buffer size too small limits throughput on links with high 12826741ecf5SAndre Oppermann * bandwidth and high delay (eg. trans-continental/oceanic links). 12836741ecf5SAndre Oppermann * 12846741ecf5SAndre Oppermann * On the receive side the socket buffer memory is only rarely 12856741ecf5SAndre Oppermann * used to any significant extent. This allows us to be much 12866741ecf5SAndre Oppermann * more aggressive in scaling the receive socket buffer. For 12876741ecf5SAndre Oppermann * the case that the buffer space is actually used to a large 12886741ecf5SAndre Oppermann * extent and we run out of kernel memory we can simply drop 12896741ecf5SAndre Oppermann * the new segments; TCP on the sender will just retransmit it 12906741ecf5SAndre Oppermann * later. Setting the buffer size too big may only consume too 12916741ecf5SAndre Oppermann * much kernel memory if the application doesn't read() from 12926741ecf5SAndre Oppermann * the socket or packet loss or reordering makes use of the 12936741ecf5SAndre Oppermann * reassembly queue. 12946741ecf5SAndre Oppermann * 12956741ecf5SAndre Oppermann * The criteria to step up the receive buffer one notch are: 12966741ecf5SAndre Oppermann * 1. the number of bytes received during the time it takes 12976741ecf5SAndre Oppermann * one timestamp to be reflected back to us (the RTT); 12986741ecf5SAndre Oppermann * 2. received bytes per RTT is within seven eighth of the 12996741ecf5SAndre Oppermann * current socket buffer size; 13006741ecf5SAndre Oppermann * 3. receive buffer size has not hit maximal automatic size; 13016741ecf5SAndre Oppermann * 13026741ecf5SAndre Oppermann * This algorithm does one step per RTT at most and only if 13036741ecf5SAndre Oppermann * we receive a bulk stream w/o packet losses or reorderings. 13046741ecf5SAndre Oppermann * Shrinking the buffer during idle times is not necessary as 13056741ecf5SAndre Oppermann * it doesn't consume any memory when idle. 13066741ecf5SAndre Oppermann * 13076741ecf5SAndre Oppermann * TODO: Only step up if the application is actually serving 13086741ecf5SAndre Oppermann * the buffer to better manage the socket buffer resources. 1309df8bae1dSRodney W. Grimes */ 13106741ecf5SAndre Oppermann if (tcp_do_autorcvbuf && 13116741ecf5SAndre Oppermann to.to_tsecr && 13126741ecf5SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 13136741ecf5SAndre Oppermann if (to.to_tsecr > tp->rfbuf_ts && 13146741ecf5SAndre Oppermann to.to_tsecr - tp->rfbuf_ts < hz) { 13156741ecf5SAndre Oppermann if (tp->rfbuf_cnt > 13166741ecf5SAndre Oppermann (so->so_rcv.sb_hiwat / 8 * 7) && 13176741ecf5SAndre Oppermann so->so_rcv.sb_hiwat < 13186741ecf5SAndre Oppermann tcp_autorcvbuf_max) { 13196741ecf5SAndre Oppermann newsize = 13206741ecf5SAndre Oppermann min(so->so_rcv.sb_hiwat + 13216741ecf5SAndre Oppermann tcp_autorcvbuf_inc, 13226741ecf5SAndre Oppermann tcp_autorcvbuf_max); 13236741ecf5SAndre Oppermann } 13246741ecf5SAndre Oppermann /* Start over with next RTT. */ 13256741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 13266741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 13276741ecf5SAndre Oppermann } else 13286741ecf5SAndre Oppermann tp->rfbuf_cnt += tlen; /* add up */ 13296741ecf5SAndre Oppermann } 13306741ecf5SAndre Oppermann 13316741ecf5SAndre Oppermann /* Add data to socket buffer. */ 13321e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1333c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1334c1c36a2cSMike Silbersack m_freem(m); 1335c1c36a2cSMike Silbersack } else { 13366741ecf5SAndre Oppermann /* 13376741ecf5SAndre Oppermann * Set new socket buffer size. 13386741ecf5SAndre Oppermann * Give up when limit is reached. 13396741ecf5SAndre Oppermann */ 13406741ecf5SAndre Oppermann if (newsize) 13416741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 13426741ecf5SAndre Oppermann newsize, so, curthread)) 13436741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1344fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 13451e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 1346c1c36a2cSMike Silbersack } 13471e4d7da7SRobert Watson sorwakeup_locked(so); 1348d8c85a26SJonathan Lemon if (DELAY_ACK(tp)) { 13493bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1350f498eeeeSDavid Greenman } else { 1351e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 1352e612a582SDavid Greenman tcp_output(tp); 1353e612a582SDavid Greenman } 1354a14c749fSJonathan Lemon goto check_delack; 1355df8bae1dSRodney W. Grimes } 1356df8bae1dSRodney W. Grimes } 1357df8bae1dSRodney W. Grimes 1358df8bae1dSRodney W. Grimes /* 1359df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1360df8bae1dSRodney W. Grimes * and then do TCP input processing. 1361df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1362df8bae1dSRodney W. Grimes * but not less than advertised window. 1363df8bae1dSRodney W. Grimes */ 1364df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1365df8bae1dSRodney W. Grimes if (win < 0) 1366df8bae1dSRodney W. Grimes win = 0; 136766e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1368df8bae1dSRodney W. Grimes 13696741ecf5SAndre Oppermann /* Reset receive buffer auto scaling when not in bulk receive mode. */ 13706741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 13716741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 13726741ecf5SAndre Oppermann 1373df8bae1dSRodney W. Grimes switch (tp->t_state) { 1374df8bae1dSRodney W. Grimes 1375df8bae1dSRodney W. Grimes /* 1376764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1377764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1378764d8cefSBill Fenner */ 1379764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1380fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1381fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1382a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1383a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1384a57815efSBosko Milekic goto dropwithreset; 1385a57815efSBosko Milekic } 1386764d8cefSBill Fenner break; 1387764d8cefSBill Fenner 1388764d8cefSBill Fenner /* 1389df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1390df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1391df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1392df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1393df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1394df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1395df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1396df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1397df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1398df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1399df8bae1dSRodney W. Grimes */ 1400df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1401fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1402fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1403fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1404a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1405a0292f23SGarrett Wollman goto dropwithreset; 1406a0292f23SGarrett Wollman } 1407fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 14081e2d989dSRobert Watson if (thflags & TH_ACK) { 1409302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: after_listen: " 1410302ce8d6SAndre Oppermann "tcp_drop.2: head not locked", __func__)); 1411df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 14121e2d989dSRobert Watson } 1413df8bae1dSRodney W. Grimes goto drop; 1414df8bae1dSRodney W. Grimes } 1415fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) == 0) 1416df8bae1dSRodney W. Grimes goto drop; 1417464fcfbcSAndre Oppermann 1418fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1419df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1420fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1421845799c1SAndras Olah tcpstat.tcps_connects++; 1422845799c1SAndras Olah soisconnected(so); 1423c488362eSRobert Watson #ifdef MAC 1424310e7cebSRobert Watson SOCK_LOCK(so); 1425c488362eSRobert Watson mac_set_socket_peer_from_mbuf(m, so); 1426310e7cebSRobert Watson SOCK_UNLOCK(so); 1427c488362eSRobert Watson #endif 1428845799c1SAndras Olah /* Do window scaling on this connection? */ 1429845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1430845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1431845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1432845799c1SAndras Olah } 1433a0292f23SGarrett Wollman tp->rcv_adv += tp->rcv_wnd; 1434a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1435a0292f23SGarrett Wollman /* 1436a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1437a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1438a0292f23SGarrett Wollman */ 1439d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && tlen != 0) 1440b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 1441b8152ba7SAndre Oppermann tcp_delacktime); 1442a0292f23SGarrett Wollman else 1443a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1444a0292f23SGarrett Wollman /* 1445a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1446a0292f23SGarrett Wollman * Transitions: 1447a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1448a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1449a0292f23SGarrett Wollman */ 14509b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1451a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1452a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1453a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1454fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 14557ff19458SPaul Traina } else { 1456a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1457b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 14587ff19458SPaul Traina } 1459a0292f23SGarrett Wollman } else { 1460a0292f23SGarrett Wollman /* 1461c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 1462c068736aSJeffrey Hsu * simultaneous open. If segment contains CC option 1463c068736aSJeffrey Hsu * and there is a cached CC, apply TAO test. 1464c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 1465c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 1466a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1467a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1468a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1469a0292f23SGarrett Wollman */ 14704b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 1471b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1472df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1473a0292f23SGarrett Wollman } 1474df8bae1dSRodney W. Grimes 1475302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: trimthenstep6: head not locked", 1476302ce8d6SAndre Oppermann __func__)); 1477302ce8d6SAndre Oppermann INP_LOCK_ASSERT(tp->t_inpcb); 14787cfc6904SRobert Watson 1479df8bae1dSRodney W. Grimes /* 1480fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1481df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1482df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1483df8bae1dSRodney W. Grimes */ 1484fb59c426SYoshinobu Inoue th->th_seq++; 1485fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1486fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1487df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1488fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1489fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1490df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1491df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1492df8bae1dSRodney W. Grimes } 1493fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1494fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 1495a0292f23SGarrett Wollman /* 1496a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1497a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1498a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1499a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1500a0292f23SGarrett Wollman * Otherwise, goto step 6. 1501a0292f23SGarrett Wollman */ 1502fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1503a0292f23SGarrett Wollman goto process_ACK; 1504c068736aSJeffrey Hsu 1505df8bae1dSRodney W. Grimes goto step6; 1506c068736aSJeffrey Hsu 1507a0292f23SGarrett Wollman /* 1508a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1509c94c54e4SAndre Oppermann * do normal processing. 1510a0292f23SGarrett Wollman * 1511c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 1512a0292f23SGarrett Wollman */ 1513a0292f23SGarrett Wollman case TCPS_LAST_ACK: 1514a0292f23SGarrett Wollman case TCPS_CLOSING: 1515a0292f23SGarrett Wollman case TCPS_TIME_WAIT: 1516302ce8d6SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: timewait", 1517302ce8d6SAndre Oppermann __func__)); 1518a0292f23SGarrett Wollman break; /* continue normal processing */ 1519df8bae1dSRodney W. Grimes } 1520df8bae1dSRodney W. Grimes 1521df8bae1dSRodney W. Grimes /* 1522df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 152380ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 152480ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 152580ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 152680ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 152780ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 152880ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 152980ab7c0eSGarrett Wollman * killing a connection). 153080ab7c0eSGarrett Wollman * Then check timestamp, if present. 1531a0292f23SGarrett Wollman * Then check the connection count, if present. 1532df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 1533df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 1534df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 1535df8bae1dSRodney W. Grimes * 153680ab7c0eSGarrett Wollman * 153780ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 153880ab7c0eSGarrett Wollman * if this is a valid reset segment. 153980ab7c0eSGarrett Wollman * RFC 793 page 37: 154080ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 154180ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 154280ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 154380ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 154480ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 15451a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 15461a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 15471a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 15481a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 154980dd2a81SMike Silbersack * Note 2: Paul Watson's paper "Slipping in the Window" has shown 155080dd2a81SMike Silbersack * that brute force RST attacks are possible. To combat this, 155180dd2a81SMike Silbersack * we use a much stricter check while in the ESTABLISHED state, 155280dd2a81SMike Silbersack * only accepting RSTs where the sequence number is equal to 155380dd2a81SMike Silbersack * last_ack_sent. In all other states (the states in which a 155480dd2a81SMike Silbersack * RST is more likely), the more permissive check is used. 155580ab7c0eSGarrett Wollman * If we have multiple segments in flight, the intial reset 155680ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 155780ab7c0eSGarrett Wollman * but they will eventually catch up. 155880ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 155980ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 156080ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 156180ab7c0eSGarrett Wollman * 156280ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 156380ab7c0eSGarrett Wollman * 156480ab7c0eSGarrett Wollman * DISCUSSION 156580ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 156680ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 156780ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 156880ab7c0eSGarrett Wollman * data. 156980ab7c0eSGarrett Wollman * 157080ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 157180ab7c0eSGarrett Wollman * the state: 157280ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 157380ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 157480ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 1575745bab7fSDima Dorfman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 157680ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 1577e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 157880ab7c0eSGarrett Wollman * Close the tcb. 1579e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 158080ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 158180ab7c0eSGarrett Wollman * RFC 1337. 158280ab7c0eSGarrett Wollman */ 1583fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 158495ad8418SQing Li if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 158595ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 158680ab7c0eSGarrett Wollman switch (tp->t_state) { 158780ab7c0eSGarrett Wollman 158880ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 158980ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 159080ab7c0eSGarrett Wollman goto close; 159180ab7c0eSGarrett Wollman 159280ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 159395ad8418SQing Li if (tcp_insecure_rst == 0 && 159495ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) && 159595ad8418SQing Li SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) && 159695ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 159795ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) { 159880dd2a81SMike Silbersack tcpstat.tcps_badrst++; 159980dd2a81SMike Silbersack goto drop; 160080dd2a81SMike Silbersack } 160180ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 160280ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 160380ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 160480ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 160580ab7c0eSGarrett Wollman close: 160680ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 160780ab7c0eSGarrett Wollman tcpstat.tcps_drops++; 1608302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: trimthenstep6: " 1609302ce8d6SAndre Oppermann "tcp_close: head not locked", __func__)); 161080ab7c0eSGarrett Wollman tp = tcp_close(tp); 161180ab7c0eSGarrett Wollman break; 161280ab7c0eSGarrett Wollman 161380ab7c0eSGarrett Wollman case TCPS_CLOSING: 161480ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 1615302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: trimthenstep6: " 1616302ce8d6SAndre Oppermann "tcp_close.2: head not locked", __func__)); 161780ab7c0eSGarrett Wollman tp = tcp_close(tp); 161880ab7c0eSGarrett Wollman break; 161980ab7c0eSGarrett Wollman 162080ab7c0eSGarrett Wollman case TCPS_TIME_WAIT: 1621340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, 1622302ce8d6SAndre Oppermann ("%s: timewait", __func__)); 162380ab7c0eSGarrett Wollman break; 162480ab7c0eSGarrett Wollman } 162580ab7c0eSGarrett Wollman } 162680ab7c0eSGarrett Wollman goto drop; 162780ab7c0eSGarrett Wollman } 162880ab7c0eSGarrett Wollman 162980ab7c0eSGarrett Wollman /* 1630df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 1631df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 1632df8bae1dSRodney W. Grimes */ 1633be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 163480ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1635df8bae1dSRodney W. Grimes 1636df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 16379b8b58e0SJonathan Lemon if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1638df8bae1dSRodney W. Grimes /* 1639df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 1640df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 1641df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 1642df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 1643df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 1644df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 1645df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 1646df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 1647df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 1648df8bae1dSRodney W. Grimes */ 1649df8bae1dSRodney W. Grimes tp->ts_recent = 0; 1650df8bae1dSRodney W. Grimes } else { 1651df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 1652fb59c426SYoshinobu Inoue tcpstat.tcps_rcvdupbyte += tlen; 1653df8bae1dSRodney W. Grimes tcpstat.tcps_pawsdrop++; 165407fd333dSMatthew Dillon if (tlen) 1655df8bae1dSRodney W. Grimes goto dropafterack; 16561ab4789dSMatthew Dillon goto drop; 16571ab4789dSMatthew Dillon } 1658df8bae1dSRodney W. Grimes } 1659df8bae1dSRodney W. Grimes 1660a0292f23SGarrett Wollman /* 166180ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 166280ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 166380ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 166480ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 166580ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 166680ab7c0eSGarrett Wollman */ 1667a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 1668a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1669a57815efSBosko Milekic goto dropwithreset; 1670a57815efSBosko Milekic } 167180ab7c0eSGarrett Wollman 1672fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 1673df8bae1dSRodney W. Grimes if (todrop > 0) { 1674fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1675fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 1676fb59c426SYoshinobu Inoue th->th_seq++; 1677fb59c426SYoshinobu Inoue if (th->th_urp > 1) 1678fb59c426SYoshinobu Inoue th->th_urp--; 1679df8bae1dSRodney W. Grimes else 1680fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1681df8bae1dSRodney W. Grimes todrop--; 1682df8bae1dSRodney W. Grimes } 1683df8bae1dSRodney W. Grimes /* 1684dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 1685df8bae1dSRodney W. Grimes */ 1686fb59c426SYoshinobu Inoue if (todrop > tlen 1687fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1688dac20301SGarrett Wollman /* 1689dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 1690dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 1691dac20301SGarrett Wollman * of sequence; drop it. 1692dac20301SGarrett Wollman */ 1693fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1694dac20301SGarrett Wollman 1695df8bae1dSRodney W. Grimes /* 1696dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 1697dac20301SGarrett Wollman * But keep on processing for RST or ACK. 1698df8bae1dSRodney W. Grimes */ 1699dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1700fb59c426SYoshinobu Inoue todrop = tlen; 1701dac20301SGarrett Wollman tcpstat.tcps_rcvduppack++; 1702dac20301SGarrett Wollman tcpstat.tcps_rcvdupbyte += todrop; 1703df8bae1dSRodney W. Grimes } else { 1704df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartduppack++; 1705df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartdupbyte += todrop; 1706df8bae1dSRodney W. Grimes } 1707fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 1708fb59c426SYoshinobu Inoue th->th_seq += todrop; 1709fb59c426SYoshinobu Inoue tlen -= todrop; 1710fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 1711fb59c426SYoshinobu Inoue th->th_urp -= todrop; 1712df8bae1dSRodney W. Grimes else { 1713fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1714fb59c426SYoshinobu Inoue th->th_urp = 0; 1715df8bae1dSRodney W. Grimes } 1716df8bae1dSRodney W. Grimes } 1717df8bae1dSRodney W. Grimes 1718df8bae1dSRodney W. Grimes /* 1719df8bae1dSRodney W. Grimes * If new data are received on a connection after the 1720df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 1721df8bae1dSRodney W. Grimes */ 1722df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 1723fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1724302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: trimthenstep6: tcp_close.3: head " 1725302ce8d6SAndre Oppermann "not locked", __func__)); 1726df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1727df8bae1dSRodney W. Grimes tcpstat.tcps_rcvafterclose++; 1728a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1729df8bae1dSRodney W. Grimes goto dropwithreset; 1730df8bae1dSRodney W. Grimes } 1731df8bae1dSRodney W. Grimes 1732df8bae1dSRodney W. Grimes /* 1733df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 1734df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 1735df8bae1dSRodney W. Grimes */ 1736fb59c426SYoshinobu Inoue todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd); 1737df8bae1dSRodney W. Grimes if (todrop > 0) { 1738df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1739fb59c426SYoshinobu Inoue if (todrop >= tlen) { 1740fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyteafterwin += tlen; 1741df8bae1dSRodney W. Grimes /* 1742df8bae1dSRodney W. Grimes * If a new connection request is received 1743df8bae1dSRodney W. Grimes * while in TIME_WAIT, drop the old connection 1744df8bae1dSRodney W. Grimes * and start over if the sequence numbers 1745df8bae1dSRodney W. Grimes * are above the previous ones. 1746df8bae1dSRodney W. Grimes */ 1747302ce8d6SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: timewait", 1748302ce8d6SAndre Oppermann __func__)); 1749fb59c426SYoshinobu Inoue if (thflags & TH_SYN && 1750df8bae1dSRodney W. Grimes tp->t_state == TCPS_TIME_WAIT && 1751fb59c426SYoshinobu Inoue SEQ_GT(th->th_seq, tp->rcv_nxt)) { 1752302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: trimthenstep6: " 1753302ce8d6SAndre Oppermann "tcp_close.4: head not locked", __func__)); 1754df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1755302ce8d6SAndre Oppermann /* XXX: Shouldn't be possible. */ 1756302ce8d6SAndre Oppermann return (1); 1757df8bae1dSRodney W. Grimes } 1758df8bae1dSRodney W. Grimes /* 1759df8bae1dSRodney W. Grimes * If window is closed can only take segments at 1760df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 1761df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 1762df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 1763df8bae1dSRodney W. Grimes * and ack. 1764df8bae1dSRodney W. Grimes */ 1765fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1766df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1767df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinprobe++; 1768df8bae1dSRodney W. Grimes } else 1769df8bae1dSRodney W. Grimes goto dropafterack; 1770df8bae1dSRodney W. Grimes } else 1771df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1772df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1773fb59c426SYoshinobu Inoue tlen -= todrop; 1774fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 1775df8bae1dSRodney W. Grimes } 1776df8bae1dSRodney W. Grimes 1777df8bae1dSRodney W. Grimes /* 1778df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1779df8bae1dSRodney W. Grimes * record its timestamp. 1780cf09195bSPaul Saab * NOTE: 1781cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 1782a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1783cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 1784cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 1785cf09195bSPaul Saab * predicated on the sequence space of this segment. 1786cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 1787cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 1788cf09195bSPaul Saab * instead of RFC1323's 1789cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 1790cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 1791cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 1792cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 1793cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 1794df8bae1dSRodney W. Grimes */ 1795be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1796cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 1797cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 1798cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 17999b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1800a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1801df8bae1dSRodney W. Grimes } 1802df8bae1dSRodney W. Grimes 1803df8bae1dSRodney W. Grimes /* 1804df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 1805df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 1806df8bae1dSRodney W. Grimes */ 1807fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1808302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: tcp_drop: trimthenstep6: " 1809302ce8d6SAndre Oppermann "head not locked", __func__)); 1810df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 1811a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1812122aad88SAndre Oppermann goto drop; 1813df8bae1dSRodney W. Grimes } 1814df8bae1dSRodney W. Grimes 1815a0292f23SGarrett Wollman /* 1816a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1817a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 1818a0292f23SGarrett Wollman * later processing; else drop segment and return. 1819a0292f23SGarrett Wollman */ 1820fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 1821a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 1822a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 1823a0292f23SGarrett Wollman goto step6; 18245e1aa279SDavid Malone else if (tp->t_flags & TF_ACKNOW) 18255e1aa279SDavid Malone goto dropafterack; 1826a0292f23SGarrett Wollman else 1827a0292f23SGarrett Wollman goto drop; 1828a0292f23SGarrett Wollman } 1829df8bae1dSRodney W. Grimes 1830df8bae1dSRodney W. Grimes /* 1831df8bae1dSRodney W. Grimes * Ack processing. 1832df8bae1dSRodney W. Grimes */ 1833df8bae1dSRodney W. Grimes switch (tp->t_state) { 1834df8bae1dSRodney W. Grimes 1835df8bae1dSRodney W. Grimes /* 1836764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1837764d8cefSBill Fenner * ESTABLISHED state and continue processing. 1838764d8cefSBill Fenner * The ACK was checked above. 1839df8bae1dSRodney W. Grimes */ 1840df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1841a0292f23SGarrett Wollman 1842df8bae1dSRodney W. Grimes tcpstat.tcps_connects++; 1843df8bae1dSRodney W. Grimes soisconnected(so); 1844df8bae1dSRodney W. Grimes /* Do window scaling? */ 1845df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1846df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1847df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 1848464fcfbcSAndre Oppermann tp->snd_wnd = tiwin; 1849df8bae1dSRodney W. Grimes } 1850a0292f23SGarrett Wollman /* 1851a0292f23SGarrett Wollman * Make transitions: 1852a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 1853a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 1854a0292f23SGarrett Wollman */ 18559b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1856a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1857a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1858a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 18597ff19458SPaul Traina } else { 1860a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1861b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 18627ff19458SPaul Traina } 1863a0292f23SGarrett Wollman /* 1864a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 1865a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 1866a0292f23SGarrett Wollman */ 1867fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 1868fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1869a0292f23SGarrett Wollman (struct mbuf *)0); 1870fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 187193b0017fSPhilippe Charnier /* FALLTHROUGH */ 1872df8bae1dSRodney W. Grimes 1873df8bae1dSRodney W. Grimes /* 1874df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1875df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 1876fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 1877fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 1878df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 1879df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 1880df8bae1dSRodney W. Grimes */ 1881df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1882df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1883df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 1884df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1885df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1886df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 1887df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 1888302ce8d6SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: timewait", 1889302ce8d6SAndre Oppermann __func__)); 18905a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 18915a53ca16SPaul Saab tcpstat.tcps_rcvacktoomuch++; 18925a53ca16SPaul Saab goto dropafterack; 18935a53ca16SPaul Saab } 1894482ac968SPaul Saab if (tp->sack_enable && 1895fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 1896fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 18975a53ca16SPaul Saab tcp_sack_doack(tp, &to, th->th_ack); 1898fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1899fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 1900df8bae1dSRodney W. Grimes tcpstat.tcps_rcvdupack++; 1901df8bae1dSRodney W. Grimes /* 1902df8bae1dSRodney W. Grimes * If we have outstanding data (other than 1903df8bae1dSRodney W. Grimes * a window probe), this is a completely 1904df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 1905df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 1906df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 1907df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 1908df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 1909df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 1910df8bae1dSRodney W. Grimes * window so we send only this one 1911df8bae1dSRodney W. Grimes * packet. 1912df8bae1dSRodney W. Grimes * 1913df8bae1dSRodney W. Grimes * We know we're losing at the current 1914df8bae1dSRodney W. Grimes * window size so do congestion avoidance 1915df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 1916df8bae1dSRodney W. Grimes * and pull our congestion window back to 1917df8bae1dSRodney W. Grimes * the new ssthresh). 1918df8bae1dSRodney W. Grimes * 1919df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 1920df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 1921df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 1922df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 1923df8bae1dSRodney W. Grimes * network. 1924df8bae1dSRodney W. Grimes */ 1925b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_REXMT) || 1926fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 1927df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1928cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 19296d90faf3SPaul Saab ((tcp_do_newreno || tp->sack_enable) && 19309d11646dSJeffrey Hsu IN_FASTRECOVERY(tp))) { 193125e6f9edSPaul Saab if (tp->sack_enable && IN_FASTRECOVERY(tp)) { 1932808f11b7SPaul Saab int awnd; 193325e6f9edSPaul Saab 193425e6f9edSPaul Saab /* 193525e6f9edSPaul Saab * Compute the amount of data in flight first. 193625e6f9edSPaul Saab * We can inject new data into the pipe iff 193725e6f9edSPaul Saab * we have less than 1/2 the original window's 193825e6f9edSPaul Saab * worth of data in flight. 193925e6f9edSPaul Saab */ 1940808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 19410077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 1942808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 194325e6f9edSPaul Saab tp->snd_cwnd += tp->t_maxseg; 194425e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 194525e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 194625e6f9edSPaul Saab } 194725e6f9edSPaul Saab } else 194846f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 194946f58482SJonathan Lemon (void) tcp_output(tp); 195046f58482SJonathan Lemon goto drop; 1951cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 1952cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 1953cb942153SJeffrey Hsu u_int win; 1954a0445c2eSJayanth Vijayaraghavan 1955a0445c2eSJayanth Vijayaraghavan /* 1956a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 1957a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 1958a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 1959a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 1960a0445c2eSJayanth Vijayaraghavan * recovery. 1961a0445c2eSJayanth Vijayaraghavan */ 1962a0445c2eSJayanth Vijayaraghavan if (tp->sack_enable) { 1963a0445c2eSJayanth Vijayaraghavan if (IN_FASTRECOVERY(tp)) { 1964a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 1965a0445c2eSJayanth Vijayaraghavan break; 1966a0445c2eSJayanth Vijayaraghavan } 1967a0445c2eSJayanth Vijayaraghavan } else if (tcp_do_newreno) { 1968a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 19699d11646dSJeffrey Hsu tp->snd_recover)) { 1970cb942153SJeffrey Hsu tp->t_dupacks = 0; 1971cb942153SJeffrey Hsu break; 197246f58482SJonathan Lemon } 1973a0445c2eSJayanth Vijayaraghavan } 1974cb942153SJeffrey Hsu win = min(tp->snd_wnd, tp->snd_cwnd) / 1975cb942153SJeffrey Hsu 2 / tp->t_maxseg; 1976df8bae1dSRodney W. Grimes if (win < 2) 1977df8bae1dSRodney W. Grimes win = 2; 1978df8bae1dSRodney W. Grimes tp->snd_ssthresh = win * tp->t_maxseg; 19799d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 198046f58482SJonathan Lemon tp->snd_recover = tp->snd_max; 1981b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 19829b8b58e0SJonathan Lemon tp->t_rtttime = 0; 19836d90faf3SPaul Saab if (tp->sack_enable) { 19846d90faf3SPaul Saab tcpstat.tcps_sack_recovery_episode++; 1985a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 19868db456bfSPaul Saab tp->snd_cwnd = tp->t_maxseg; 19876d90faf3SPaul Saab (void) tcp_output(tp); 19886d90faf3SPaul Saab goto drop; 19896d90faf3SPaul Saab } 1990fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 1991df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 1992df8bae1dSRodney W. Grimes (void) tcp_output(tp); 199348d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 1994302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 1995302ce8d6SAndre Oppermann __func__)); 1996df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 199748d2549cSJeffrey Hsu tp->t_maxseg * 199848d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 1999df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 2000df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 2001df8bae1dSRodney W. Grimes goto drop; 2002582a954bSJeffrey Hsu } else if (tcp_do_rfc3042) { 2003582a954bSJeffrey Hsu u_long oldcwnd = tp->snd_cwnd; 200448d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 200548d2549cSJeffrey Hsu u_int sent; 200689c02376SJeffrey Hsu 2007582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 2008582a954bSJeffrey Hsu tp->t_dupacks == 2, 2009302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 2010302ce8d6SAndre Oppermann __func__)); 201161a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 201248d2549cSJeffrey Hsu tp->snd_limited = 0; 201361a36e3dSJeffrey Hsu tp->snd_cwnd = 201461a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 201561a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 201661a36e3dSJeffrey Hsu tp->t_maxseg; 2017582a954bSJeffrey Hsu (void) tcp_output(tp); 201848d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 201948d2549cSJeffrey Hsu if (sent > tp->t_maxseg) { 202089c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 202189c02376SJeffrey Hsu tp->snd_limited == 0) || 202289c02376SJeffrey Hsu (sent == tp->t_maxseg + 1 && 202389c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 2024302ce8d6SAndre Oppermann ("%s: sent too much", 2025302ce8d6SAndre Oppermann __func__)); 202648d2549cSJeffrey Hsu tp->snd_limited = 2; 202748d2549cSJeffrey Hsu } else if (sent > 0) 202848d2549cSJeffrey Hsu ++tp->snd_limited; 2029582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 2030582a954bSJeffrey Hsu goto drop; 2031df8bae1dSRodney W. Grimes } 2032df8bae1dSRodney W. Grimes } else 2033df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2034df8bae1dSRodney W. Grimes break; 2035df8bae1dSRodney W. Grimes } 2036cb942153SJeffrey Hsu 2037302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2038302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 2039cb942153SJeffrey Hsu 2040df8bae1dSRodney W. Grimes /* 2041df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2042df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2043df8bae1dSRodney W. Grimes */ 20446d90faf3SPaul Saab if (tcp_do_newreno || tp->sack_enable) { 20459d11646dSJeffrey Hsu if (IN_FASTRECOVERY(tp)) { 2046cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 20476d90faf3SPaul Saab if (tp->sack_enable) 20486d90faf3SPaul Saab tcp_sack_partialack(tp, th); 20496d90faf3SPaul Saab else 2050c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2051c068736aSJeffrey Hsu } else { 2052c068736aSJeffrey Hsu /* 20536d90faf3SPaul Saab * Out of fast recovery. 2054c068736aSJeffrey Hsu * Window inflation should have left us 2055c068736aSJeffrey Hsu * with approximately snd_ssthresh 2056c068736aSJeffrey Hsu * outstanding data. 2057c068736aSJeffrey Hsu * But in case we would be inclined to 2058c068736aSJeffrey Hsu * send a burst, better to do it via 2059c068736aSJeffrey Hsu * the slow start mechanism. 2060c068736aSJeffrey Hsu */ 2061c068736aSJeffrey Hsu if (SEQ_GT(th->th_ack + 2062c068736aSJeffrey Hsu tp->snd_ssthresh, 2063c068736aSJeffrey Hsu tp->snd_max)) 2064c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_max - 2065c068736aSJeffrey Hsu th->th_ack + 2066c068736aSJeffrey Hsu tp->t_maxseg; 2067c068736aSJeffrey Hsu else 2068c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_ssthresh; 2069c068736aSJeffrey Hsu } 2070c068736aSJeffrey Hsu } 2071c068736aSJeffrey Hsu } else { 2072233e8c18SGarrett Wollman if (tp->t_dupacks >= tcprexmtthresh && 2073df8bae1dSRodney W. Grimes tp->snd_cwnd > tp->snd_ssthresh) 2074df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh; 207546f58482SJonathan Lemon } 2076cb942153SJeffrey Hsu tp->t_dupacks = 0; 2077a0292f23SGarrett Wollman /* 2078a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2079a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2080a0292f23SGarrett Wollman */ 2081a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2082a0292f23SGarrett Wollman /* 2083a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2084a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 208507e43e10SAndras Olah * synchronized). Go to non-starred state, 208607e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 208707e43e10SAndras Olah * we can do window scaling. 2088a0292f23SGarrett Wollman */ 2089a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2090a0292f23SGarrett Wollman tp->snd_una++; 209107e43e10SAndras Olah /* Do window scaling? */ 209207e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 209307e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 209407e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2095464fcfbcSAndre Oppermann /* Send window already scaled. */ 209607e43e10SAndras Olah } 2097a0292f23SGarrett Wollman } 2098a0292f23SGarrett Wollman 2099a0292f23SGarrett Wollman process_ACK: 2100302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: process_ACK: head not locked", 2101302ce8d6SAndre Oppermann __func__)); 2102302ce8d6SAndre Oppermann INP_LOCK_ASSERT(tp->t_inpcb); 21037cfc6904SRobert Watson 2104fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 2105df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 2106df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 2107df8bae1dSRodney W. Grimes 2108df8bae1dSRodney W. Grimes /* 21099b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 21109b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 21119b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 21129b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 21139b8b58e0SJonathan Lemon * we left off. 21149b8b58e0SJonathan Lemon */ 21159b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 2116d65bf08aSMatthew Dillon ++tcpstat.tcps_sndrexmitbad; 21179b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 21189b8b58e0SJonathan Lemon tp->snd_ssthresh = tp->snd_ssthresh_prev; 21199d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 21209d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 21219d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 21229b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 21239b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; /* XXX probably not required */ 21249b8b58e0SJonathan Lemon } 21259b8b58e0SJonathan Lemon 21269b8b58e0SJonathan Lemon /* 2127df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2128df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2129df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2130df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2131df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2132df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2133df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2134fa55172bSMatthew Dillon * 2135fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2136fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2137fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2138fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2139df8bae1dSRodney W. Grimes */ 2140fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 2141fa55172bSMatthew Dillon to.to_tsecr) { 2142eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - to.to_tsecr) 2143eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 21449b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 2145fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2146eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2147eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 21489b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2149fa55172bSMatthew Dillon } 21501fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 2151df8bae1dSRodney W. Grimes 2152df8bae1dSRodney W. Grimes /* 2153df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2154df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2155df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2156df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2157df8bae1dSRodney W. Grimes */ 2158fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 2159b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2160df8bae1dSRodney W. Grimes needoutput = 1; 2161b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2162b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2163a0292f23SGarrett Wollman 2164a0292f23SGarrett Wollman /* 2165a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2166a0292f23SGarrett Wollman * skip rest of ACK processing. 2167a0292f23SGarrett Wollman */ 2168a0292f23SGarrett Wollman if (acked == 0) 2169a0292f23SGarrett Wollman goto step6; 2170a0292f23SGarrett Wollman 2171df8bae1dSRodney W. Grimes /* 2172df8bae1dSRodney W. Grimes * When new data is acked, open the congestion window. 2173df8bae1dSRodney W. Grimes * If the window gives us less than ssthresh packets 2174df8bae1dSRodney W. Grimes * in flight, open exponentially (maxseg per packet). 2175df8bae1dSRodney W. Grimes * Otherwise open linearly: maxseg per window 217610be5648SGarrett Wollman * (maxseg^2 / cwnd per packet). 2177df8bae1dSRodney W. Grimes */ 21786d90faf3SPaul Saab if ((!tcp_do_newreno && !tp->sack_enable) || 21796d90faf3SPaul Saab !IN_FASTRECOVERY(tp)) { 2180ad3f9ab3SAndre Oppermann u_int cw = tp->snd_cwnd; 2181ad3f9ab3SAndre Oppermann u_int incr = tp->t_maxseg; 2182df8bae1dSRodney W. Grimes if (cw > tp->snd_ssthresh) 218310be5648SGarrett Wollman incr = incr * incr / cw; 2184df8bae1dSRodney W. Grimes tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale); 2185df8bae1dSRodney W. Grimes } 21865905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2187df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 2188df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 21895905999bSRobert Watson sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc); 2190df8bae1dSRodney W. Grimes ourfinisacked = 1; 2191df8bae1dSRodney W. Grimes } else { 21925905999bSRobert Watson sbdrop_locked(&so->so_snd, acked); 2193df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 2194df8bae1dSRodney W. Grimes ourfinisacked = 0; 2195df8bae1dSRodney W. Grimes } 21961e4d7da7SRobert Watson sowwakeup_locked(so); 2197cb942153SJeffrey Hsu /* detect una wraparound */ 21986d90faf3SPaul Saab if ((tcp_do_newreno || tp->sack_enable) && 21996d90faf3SPaul Saab !IN_FASTRECOVERY(tp) && 22009d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 22019d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 22029d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 22036d90faf3SPaul Saab if ((tcp_do_newreno || tp->sack_enable) && 22046d90faf3SPaul Saab IN_FASTRECOVERY(tp) && 22059d11646dSJeffrey Hsu SEQ_GEQ(th->th_ack, tp->snd_recover)) 22069d11646dSJeffrey Hsu EXIT_FASTRECOVERY(tp); 2207fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 22086d90faf3SPaul Saab if (tp->sack_enable) { 22096d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 22106d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 22116d90faf3SPaul Saab } 2212df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2213df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2214df8bae1dSRodney W. Grimes 2215df8bae1dSRodney W. Grimes switch (tp->t_state) { 2216df8bae1dSRodney W. Grimes 2217df8bae1dSRodney W. Grimes /* 2218df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2219df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2220df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2221df8bae1dSRodney W. Grimes */ 2222df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2223df8bae1dSRodney W. Grimes if (ourfinisacked) { 2224df8bae1dSRodney W. Grimes /* 2225df8bae1dSRodney W. Grimes * If we can't receive any more 2226df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2227df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2228df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2229df8bae1dSRodney W. Grimes * we'll hang forever. 2230df8bae1dSRodney W. Grimes */ 2231340c35deSJonathan Lemon /* XXXjl 2232340c35deSJonathan Lemon * we should release the tp also, and use a 2233340c35deSJonathan Lemon * compressed state. 2234340c35deSJonathan Lemon */ 2235c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 22367c72af87SMohan Srinivasan int timeout; 22377c72af87SMohan Srinivasan 223803e49181SSeigo Tanimura soisdisconnected(so); 22397c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 22407c72af87SMohan Srinivasan tcp_finwait2_timeout : tcp_maxidle; 2241b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 22424cc20ab1SSeigo Tanimura } 2243df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 2244df8bae1dSRodney W. Grimes } 2245df8bae1dSRodney W. Grimes break; 2246df8bae1dSRodney W. Grimes 2247df8bae1dSRodney W. Grimes /* 2248df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2249df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2250df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2251df8bae1dSRodney W. Grimes * the segment. 2252df8bae1dSRodney W. Grimes */ 2253df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2254df8bae1dSRodney W. Grimes if (ourfinisacked) { 2255302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: process_ACK: " 2256302ce8d6SAndre Oppermann "head not locked", __func__)); 225711a20fb8SJeffrey Hsu tcp_twstart(tp); 2258340c35deSJonathan Lemon INP_INFO_WUNLOCK(&tcbinfo); 2259302ce8d6SAndre Oppermann headlocked = 0; 2260340c35deSJonathan Lemon m_freem(m); 2261302ce8d6SAndre Oppermann return (0); 2262df8bae1dSRodney W. Grimes } 2263df8bae1dSRodney W. Grimes break; 2264df8bae1dSRodney W. Grimes 2265df8bae1dSRodney W. Grimes /* 2266df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2267df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2268df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2269df8bae1dSRodney W. Grimes * enter the closed state and return. 2270df8bae1dSRodney W. Grimes */ 2271df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2272df8bae1dSRodney W. Grimes if (ourfinisacked) { 2273302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: process_ACK: " 2274302ce8d6SAndre Oppermann "tcp_close: head not locked", __func__)); 2275df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2276df8bae1dSRodney W. Grimes goto drop; 2277df8bae1dSRodney W. Grimes } 2278df8bae1dSRodney W. Grimes break; 2279df8bae1dSRodney W. Grimes 2280df8bae1dSRodney W. Grimes /* 2281df8bae1dSRodney W. Grimes * In TIME_WAIT state the only thing that should arrive 2282df8bae1dSRodney W. Grimes * is a retransmission of the remote FIN. Acknowledge 2283df8bae1dSRodney W. Grimes * it and restart the finack timer. 2284df8bae1dSRodney W. Grimes */ 2285df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 2286302ce8d6SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, 2287302ce8d6SAndre Oppermann ("%s: timewait", __func__)); 2288b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, 2 * tcp_msl); 2289df8bae1dSRodney W. Grimes goto dropafterack; 2290df8bae1dSRodney W. Grimes } 2291df8bae1dSRodney W. Grimes } 2292df8bae1dSRodney W. Grimes 2293df8bae1dSRodney W. Grimes step6: 2294302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: step6: head not locked", __func__)); 2295302ce8d6SAndre Oppermann INP_LOCK_ASSERT(tp->t_inpcb); 22967cfc6904SRobert Watson 2297df8bae1dSRodney W. Grimes /* 2298df8bae1dSRodney W. Grimes * Update window information. 2299df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 2300df8bae1dSRodney W. Grimes */ 2301fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 2302fb59c426SYoshinobu Inoue (SEQ_LT(tp->snd_wl1, th->th_seq) || 2303fb59c426SYoshinobu Inoue (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2304fb59c426SYoshinobu Inoue (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2305df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 2306fb59c426SYoshinobu Inoue if (tlen == 0 && 2307fb59c426SYoshinobu Inoue tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2308df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinupd++; 2309df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 2310fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq; 2311fb59c426SYoshinobu Inoue tp->snd_wl2 = th->th_ack; 2312df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2313df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 2314df8bae1dSRodney W. Grimes needoutput = 1; 2315df8bae1dSRodney W. Grimes } 2316df8bae1dSRodney W. Grimes 2317df8bae1dSRodney W. Grimes /* 2318df8bae1dSRodney W. Grimes * Process segments with URG. 2319df8bae1dSRodney W. Grimes */ 2320fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2321df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2322df8bae1dSRodney W. Grimes /* 2323df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2324df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2325df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2326df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2327df8bae1dSRodney W. Grimes */ 232818ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2329fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2330fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2331fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 233218ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2333df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2334df8bae1dSRodney W. Grimes } 2335df8bae1dSRodney W. Grimes /* 2336df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2337df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2338df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2339df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2340df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2341df8bae1dSRodney W. Grimes * 2342df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2343df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2344df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2345df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2346df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2347df8bae1dSRodney W. Grimes * spec states (in one of two places). 2348df8bae1dSRodney W. Grimes */ 2349fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2350fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2351df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2352df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2353927c5ceaSRobert Watson if (so->so_oobmark == 0) 2354c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 2355df8bae1dSRodney W. Grimes sohasoutofband(so); 2356df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2357df8bae1dSRodney W. Grimes } 235818ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2359df8bae1dSRodney W. Grimes /* 2360df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2361df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2362df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2363df8bae1dSRodney W. Grimes * data may creep in... ick. 2364df8bae1dSRodney W. Grimes */ 236530613f56SJeffrey Hsu if (th->th_urp <= (u_long)tlen && 236630613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 236730613f56SJeffrey Hsu /* hdr drop is delayed */ 236830613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 236930613f56SJeffrey Hsu } 2370c068736aSJeffrey Hsu } else { 2371df8bae1dSRodney W. Grimes /* 2372df8bae1dSRodney W. Grimes * If no out of band data is expected, 2373df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2374df8bae1dSRodney W. Grimes * with the receive window. 2375df8bae1dSRodney W. Grimes */ 2376df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2377df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2378c068736aSJeffrey Hsu } 2379df8bae1dSRodney W. Grimes dodata: /* XXX */ 2380302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: dodata: head not locked", __func__)); 2381302ce8d6SAndre Oppermann INP_LOCK_ASSERT(tp->t_inpcb); 23827cfc6904SRobert Watson 2383df8bae1dSRodney W. Grimes /* 2384df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2385df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2386df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2387df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2388df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2389df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2390df8bae1dSRodney W. Grimes */ 2391fb59c426SYoshinobu Inoue if ((tlen || (thflags & TH_FIN)) && 2392df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 239369e03620SPaul Saab tcp_seq save_start = th->th_seq; 239469e03620SPaul Saab tcp_seq save_end = th->th_seq + tlen; 2395fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2396e4b64281SJesper Skriver /* 2397c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2398c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2399c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2400c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2401c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2402c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2403c068736aSJeffrey Hsu * conversions. 2404c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 2405c068736aSJeffrey Hsu * immediately when segments are out of order (so 2406c068736aSJeffrey Hsu * fast retransmit can work). 2407e4b64281SJesper Skriver */ 2408e4b64281SJesper Skriver if (th->th_seq == tp->rcv_nxt && 2409e4b64281SJesper Skriver LIST_EMPTY(&tp->t_segq) && 2410e4b64281SJesper Skriver TCPS_HAVEESTABLISHED(tp->t_state)) { 2411e4b64281SJesper Skriver if (DELAY_ACK(tp)) 24123bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2413e4b64281SJesper Skriver else 2414e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2415e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2416e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 2417e4b64281SJesper Skriver tcpstat.tcps_rcvpack++; 2418e4b64281SJesper Skriver tcpstat.tcps_rcvbyte += tlen; 2419e4b64281SJesper Skriver ND6_HINT(tp); 24201e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2421c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2422c1c36a2cSMike Silbersack m_freem(m); 2423c1c36a2cSMike Silbersack else 24241e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 24251e4d7da7SRobert Watson sorwakeup_locked(so); 2426e4b64281SJesper Skriver } else { 2427e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 2428e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2429e4b64281SJesper Skriver } 2430e346eeffSPaul Saab if (tlen > 0 && tp->sack_enable) 243169e03620SPaul Saab tcp_update_sack_list(tp, save_start, save_end); 2432302ce8d6SAndre Oppermann #if 0 2433df8bae1dSRodney W. Grimes /* 2434df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2435df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2436df8bae1dSRodney W. Grimes * buffer size. 2437302ce8d6SAndre Oppermann * XXX: Unused. 2438df8bae1dSRodney W. Grimes */ 2439df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2440302ce8d6SAndre Oppermann #endif 2441df8bae1dSRodney W. Grimes } else { 2442df8bae1dSRodney W. Grimes m_freem(m); 2443fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2444df8bae1dSRodney W. Grimes } 2445df8bae1dSRodney W. Grimes 2446df8bae1dSRodney W. Grimes /* 2447df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2448df8bae1dSRodney W. Grimes * that the connection is closing. 2449df8bae1dSRodney W. Grimes */ 2450fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2451df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2452df8bae1dSRodney W. Grimes socantrcvmore(so); 2453a0292f23SGarrett Wollman /* 2454a0292f23SGarrett Wollman * If connection is half-synchronized 2455d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2456a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2457a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2458a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2459a0292f23SGarrett Wollman */ 24603bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 24613bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2462a0292f23SGarrett Wollman else 2463df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2464df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2465df8bae1dSRodney W. Grimes } 2466df8bae1dSRodney W. Grimes switch (tp->t_state) { 2467df8bae1dSRodney W. Grimes 2468df8bae1dSRodney W. Grimes /* 2469df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2470df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2471df8bae1dSRodney W. Grimes */ 2472df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 24739b8b58e0SJonathan Lemon tp->t_starttime = ticks; 24749b8b58e0SJonathan Lemon /*FALLTHROUGH*/ 2475df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2476df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2477df8bae1dSRodney W. Grimes break; 2478df8bae1dSRodney W. Grimes 2479df8bae1dSRodney W. Grimes /* 2480df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2481df8bae1dSRodney W. Grimes * enter the CLOSING state. 2482df8bae1dSRodney W. Grimes */ 2483df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2484df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 2485df8bae1dSRodney W. Grimes break; 2486df8bae1dSRodney W. Grimes 2487df8bae1dSRodney W. Grimes /* 2488df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 2489df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 2490df8bae1dSRodney W. Grimes * standard timers. 2491df8bae1dSRodney W. Grimes */ 2492df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2493302ce8d6SAndre Oppermann KASSERT(headlocked == 1, ("%s: dodata: " 2494302ce8d6SAndre Oppermann "TCP_FIN_WAIT_2: head not locked", __func__)); 2495340c35deSJonathan Lemon tcp_twstart(tp); 249611a20fb8SJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2497302ce8d6SAndre Oppermann return (0); 2498df8bae1dSRodney W. Grimes 2499df8bae1dSRodney W. Grimes /* 2500df8bae1dSRodney W. Grimes * In TIME_WAIT state restart the 2 MSL time_wait timer. 2501df8bae1dSRodney W. Grimes */ 2502df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 2503302ce8d6SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, 2504302ce8d6SAndre Oppermann ("%s: timewait", __func__)); 2505b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, 2 * tcp_msl); 2506df8bae1dSRodney W. Grimes break; 2507df8bae1dSRodney W. Grimes } 2508df8bae1dSRodney W. Grimes } 2509e0bef1cbSRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 2510e0bef1cbSRobert Watson headlocked = 0; 2511610ee2f9SDavid Greenman #ifdef TCPDEBUG 25124cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2513fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2514fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2515610ee2f9SDavid Greenman #endif 2516df8bae1dSRodney W. Grimes 2517df8bae1dSRodney W. Grimes /* 2518df8bae1dSRodney W. Grimes * Return any desired output. 2519df8bae1dSRodney W. Grimes */ 2520df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 2521df8bae1dSRodney W. Grimes (void) tcp_output(tp); 25227792ea27SJeffrey Hsu 2523a14c749fSJonathan Lemon check_delack: 2524302ce8d6SAndre Oppermann KASSERT(headlocked == 0, ("%s: check_delack: head locked", 2525302ce8d6SAndre Oppermann __func__)); 25267824d002SAndre Oppermann INP_INFO_UNLOCK_ASSERT(&tcbinfo); 2527302ce8d6SAndre Oppermann INP_LOCK_ASSERT(tp->t_inpcb); 2528a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 25293bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 2530b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 25313bfd6421SJonathan Lemon } 2532302ce8d6SAndre Oppermann INP_UNLOCK(tp->t_inpcb); 2533302ce8d6SAndre Oppermann return (0); 2534df8bae1dSRodney W. Grimes 2535df8bae1dSRodney W. Grimes dropafterack: 2536302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: dropafterack: head not locked", __func__)); 2537df8bae1dSRodney W. Grimes /* 2538df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 2539df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 254080ab7c0eSGarrett Wollman * 254180ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 254280ab7c0eSGarrett Wollman * paths to this code happen after packets containing 254380ab7c0eSGarrett Wollman * RST have been dropped. 254480ab7c0eSGarrett Wollman * 254580ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 254680ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 254780ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 254880ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 254980ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 255080ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 2551df8bae1dSRodney W. Grimes */ 2552fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2553fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 2554a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 2555a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2556a57815efSBosko Milekic goto dropwithreset; 2557a57815efSBosko Milekic } 2558a0292f23SGarrett Wollman #ifdef TCPDEBUG 25594cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2560fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2561fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2562a0292f23SGarrett Wollman #endif 2563302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: headlocked should be 1", __func__)); 256442cf3289SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 2565df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2566df8bae1dSRodney W. Grimes (void) tcp_output(tp); 2567302ce8d6SAndre Oppermann INP_UNLOCK(tp->t_inpcb); 2568d6915262SRobert Watson m_freem(m); 2569302ce8d6SAndre Oppermann return (0); 2570df8bae1dSRodney W. Grimes 2571df8bae1dSRodney W. Grimes dropwithreset: 2572302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: dropwithreset: head not locked", __func__)); 2573a57815efSBosko Milekic 2574302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 2575c29afad6SSam Leffler 25761c53f806SRobert Watson if (tp != NULL) 2577302ce8d6SAndre Oppermann INP_UNLOCK(tp->t_inpcb); 2578f76fcf6dSJeffrey Hsu if (headlocked) 2579f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2580302ce8d6SAndre Oppermann return (0); 2581df8bae1dSRodney W. Grimes 2582df8bae1dSRodney W. Grimes drop: 2583df8bae1dSRodney W. Grimes /* 2584df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 2585df8bae1dSRodney W. Grimes */ 2586610ee2f9SDavid Greenman #ifdef TCPDEBUG 25871c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2588fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2589fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2590a0292f23SGarrett Wollman #endif 25911c53f806SRobert Watson if (tp != NULL) 2592302ce8d6SAndre Oppermann INP_UNLOCK(tp->t_inpcb); 2593f76fcf6dSJeffrey Hsu if (headlocked) 2594f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2595d6915262SRobert Watson m_freem(m); 2596302ce8d6SAndre Oppermann return (0); 2597302ce8d6SAndre Oppermann } 2598302ce8d6SAndre Oppermann 2599302ce8d6SAndre Oppermann 2600302ce8d6SAndre Oppermann /* 2601302ce8d6SAndre Oppermann * Issue RST on TCP segment. The mbuf must still include the original 2602302ce8d6SAndre Oppermann * packet header. 2603302ce8d6SAndre Oppermann */ 2604302ce8d6SAndre Oppermann static void 2605302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 2606302ce8d6SAndre Oppermann int tlen, int rstreason) 2607302ce8d6SAndre Oppermann { 2608302ce8d6SAndre Oppermann struct ip *ip; 2609302ce8d6SAndre Oppermann #ifdef INET6 2610302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 2611302ce8d6SAndre Oppermann #endif 2612302ce8d6SAndre Oppermann /* 2613302ce8d6SAndre Oppermann * Generate a RST, dropping incoming segment. 2614302ce8d6SAndre Oppermann * Make ACK acceptable to originator of segment. 2615302ce8d6SAndre Oppermann * Don't bother to respond if destination was broadcast/multicast. 2616b8152ba7SAndre Oppermann * tp may be NULL. 2617302ce8d6SAndre Oppermann */ 2618302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2619302ce8d6SAndre Oppermann goto drop; 2620302ce8d6SAndre Oppermann #ifdef INET6 2621302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 2622302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 2623302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2624302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2625302ce8d6SAndre Oppermann goto drop; 2626302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 2627302ce8d6SAndre Oppermann } else 2628302ce8d6SAndre Oppermann #endif 2629302ce8d6SAndre Oppermann { 2630302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 2631302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2632302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2633302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 2634302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2635302ce8d6SAndre Oppermann goto drop; 2636302ce8d6SAndre Oppermann } 2637302ce8d6SAndre Oppermann 2638302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 2639302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 2640302ce8d6SAndre Oppermann goto drop; 2641302ce8d6SAndre Oppermann 2642302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 2643302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 2644302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 2645302ce8d6SAndre Oppermann th->th_ack, TH_RST); 2646302ce8d6SAndre Oppermann } else { 2647302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 2648302ce8d6SAndre Oppermann tlen++; 2649302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2650302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 2651302ce8d6SAndre Oppermann } 2652302ce8d6SAndre Oppermann return; 2653302ce8d6SAndre Oppermann drop: 2654302ce8d6SAndre Oppermann m_freem(m); 2655df8bae1dSRodney W. Grimes return; 2656df8bae1dSRodney W. Grimes } 2657df8bae1dSRodney W. Grimes 2658be2ac88cSJonathan Lemon /* 2659be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 2660be2ac88cSJonathan Lemon */ 26610312fbe9SPoul-Henning Kamp static void 2662ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 2663df8bae1dSRodney W. Grimes { 2664df8bae1dSRodney W. Grimes int opt, optlen; 2665df8bae1dSRodney W. Grimes 2666be2ac88cSJonathan Lemon to->to_flags = 0; 2667df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 2668df8bae1dSRodney W. Grimes opt = cp[0]; 2669df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 2670df8bae1dSRodney W. Grimes break; 2671df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 2672df8bae1dSRodney W. Grimes optlen = 1; 2673df8bae1dSRodney W. Grimes else { 2674b474779fSJun-ichiro itojun Hagino if (cnt < 2) 2675b474779fSJun-ichiro itojun Hagino break; 2676df8bae1dSRodney W. Grimes optlen = cp[1]; 2677b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 2678df8bae1dSRodney W. Grimes break; 2679df8bae1dSRodney W. Grimes } 2680df8bae1dSRodney W. Grimes switch (opt) { 2681df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 2682df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 2683df8bae1dSRodney W. Grimes continue; 2684f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2685df8bae1dSRodney W. Grimes continue; 2686be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 2687be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 2688be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 2689fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 2690df8bae1dSRodney W. Grimes break; 2691df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 2692df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 2693df8bae1dSRodney W. Grimes continue; 2694f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2695df8bae1dSRodney W. Grimes continue; 2696be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 269702a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 2698df8bae1dSRodney W. Grimes break; 2699df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 2700df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 2701df8bae1dSRodney W. Grimes continue; 2702be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 2703a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2704a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 2705fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 2706a0292f23SGarrett Wollman bcopy((char *)cp + 6, 2707a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2708fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 2709df8bae1dSRodney W. Grimes break; 27101cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 27111cfd4b53SBruce M Simpson /* 27121cfd4b53SBruce M Simpson * XXX In order to reply to a host which has set the 27131cfd4b53SBruce M Simpson * TCP_SIGNATURE option in its initial SYN, we have to 27141cfd4b53SBruce M Simpson * record the fact that the option was observed here 27151cfd4b53SBruce M Simpson * for the syncache code to perform the correct response. 27161cfd4b53SBruce M Simpson */ 27171cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 27181cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 27191cfd4b53SBruce M Simpson continue; 2720df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 2721df47e437SAndre Oppermann to->to_signature = cp + 2; 27221cfd4b53SBruce M Simpson break; 2723265ed012SBruce M Simpson #endif 27246d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 2725f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 27266d90faf3SPaul Saab continue; 2727f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2728f72167f4SAndre Oppermann continue; 2729f72167f4SAndre Oppermann if (!tcp_do_sack) 2730f72167f4SAndre Oppermann continue; 2731fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 27326d90faf3SPaul Saab break; 27336d90faf3SPaul Saab case TCPOPT_SACK: 27345a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 27356d90faf3SPaul Saab continue; 2736b728e902SAndre Oppermann if (flags & TO_SYN) 2737b728e902SAndre Oppermann continue; 2738fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 27395a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 27405a53ca16SPaul Saab to->to_sacks = cp + 2; 27415a53ca16SPaul Saab tcpstat.tcps_sack_rcv_blocks++; 27426d90faf3SPaul Saab break; 2743be2ac88cSJonathan Lemon default: 2744be2ac88cSJonathan Lemon continue; 2745df8bae1dSRodney W. Grimes } 2746df8bae1dSRodney W. Grimes } 2747df8bae1dSRodney W. Grimes } 2748df8bae1dSRodney W. Grimes 2749df8bae1dSRodney W. Grimes /* 2750df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 2751df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 2752df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 2753df8bae1dSRodney W. Grimes * sequencing purposes. 2754df8bae1dSRodney W. Grimes */ 27550312fbe9SPoul-Henning Kamp static void 2756ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 2757ad3f9ab3SAndre Oppermann int off) 2758df8bae1dSRodney W. Grimes { 2759fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 2760df8bae1dSRodney W. Grimes 2761df8bae1dSRodney W. Grimes while (cnt >= 0) { 2762df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 2763df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 2764df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 2765df8bae1dSRodney W. Grimes 2766df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 2767df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 2768df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2769df8bae1dSRodney W. Grimes m->m_len--; 277069a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 277169a34685SYoshinobu Inoue m->m_pkthdr.len--; 2772df8bae1dSRodney W. Grimes return; 2773df8bae1dSRodney W. Grimes } 2774df8bae1dSRodney W. Grimes cnt -= m->m_len; 2775df8bae1dSRodney W. Grimes m = m->m_next; 27764dfdffe9SAndre Oppermann if (m == NULL) 2777df8bae1dSRodney W. Grimes break; 2778df8bae1dSRodney W. Grimes } 2779df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 2780df8bae1dSRodney W. Grimes } 2781df8bae1dSRodney W. Grimes 2782df8bae1dSRodney W. Grimes /* 2783df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 2784df8bae1dSRodney W. Grimes * and update averages and current timeout. 2785df8bae1dSRodney W. Grimes */ 27860312fbe9SPoul-Henning Kamp static void 2787ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 2788df8bae1dSRodney W. Grimes { 2789ad3f9ab3SAndre Oppermann int delta; 2790233e8c18SGarrett Wollman 27912be3bf22SRobert Watson INP_LOCK_ASSERT(tp->t_inpcb); 27922be3bf22SRobert Watson 2793233e8c18SGarrett Wollman tcpstat.tcps_rttupdated++; 2794233e8c18SGarrett Wollman tp->t_rttupdated++; 2795233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 2796233e8c18SGarrett Wollman /* 2797233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 2798233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 2799233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 2800233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2801233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 2802233e8c18SGarrett Wollman */ 2803233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2804233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2805233e8c18SGarrett Wollman 2806233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 2807233e8c18SGarrett Wollman tp->t_srtt = 1; 2808233e8c18SGarrett Wollman 2809233e8c18SGarrett Wollman /* 2810233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 2811233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 2812233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 2813233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 2814233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 2815233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 2816233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2817233e8c18SGarrett Wollman * rfc793's wired-in beta. 2818233e8c18SGarrett Wollman */ 2819233e8c18SGarrett Wollman if (delta < 0) 2820233e8c18SGarrett Wollman delta = -delta; 2821233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2822233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 2823233e8c18SGarrett Wollman tp->t_rttvar = 1; 28241fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 28251fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2826233e8c18SGarrett Wollman } else { 2827233e8c18SGarrett Wollman /* 2828233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 2829233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 2830233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 2831233e8c18SGarrett Wollman */ 2832233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 2833233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 28341fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2835233e8c18SGarrett Wollman } 28369b8b58e0SJonathan Lemon tp->t_rtttime = 0; 2837df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 2838df8bae1dSRodney W. Grimes 2839df8bae1dSRodney W. Grimes /* 2840df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 2841df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 2842df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 2843df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 2844df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 2845df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 2846df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 2847df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 2848df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 2849df8bae1dSRodney W. Grimes */ 2850233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 28519e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2852df8bae1dSRodney W. Grimes 2853df8bae1dSRodney W. Grimes /* 2854df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 2855df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 2856df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 2857df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 2858df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 2859df8bae1dSRodney W. Grimes */ 2860df8bae1dSRodney W. Grimes tp->t_softerror = 0; 2861df8bae1dSRodney W. Grimes } 2862df8bae1dSRodney W. Grimes 2863df8bae1dSRodney W. Grimes /* 2864df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 2865df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 2866df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 2867df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 2868df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2869df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 2870df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 2871df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 2872df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 2873df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 2874df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 2875df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 2876df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 2877a0292f23SGarrett Wollman * 2878a0292f23SGarrett Wollman * Also take into account the space needed for options that we 2879a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 2880a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 2881a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 2882a0292f23SGarrett Wollman * data in maxopd. 2883a0292f23SGarrett Wollman * 2884a0292f23SGarrett Wollman * 2885a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 2886a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 2887a0292f23SGarrett Wollman * MSS of our peer. 288897d8d152SAndre Oppermann * 288997d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 289097d8d152SAndre Oppermann * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt(). 2891df8bae1dSRodney W. Grimes */ 2892a0292f23SGarrett Wollman void 2893ad3f9ab3SAndre Oppermann tcp_mss(struct tcpcb *tp, int offer) 2894df8bae1dSRodney W. Grimes { 289597d8d152SAndre Oppermann int rtt, mss; 2896df8bae1dSRodney W. Grimes u_long bufsize; 289797d8d152SAndre Oppermann u_long maxmtu; 2898c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 2899df8bae1dSRodney W. Grimes struct socket *so; 290097d8d152SAndre Oppermann struct hc_metrics_lite metrics; 2901a0292f23SGarrett Wollman int origoffer = offer; 2902233dcce1SAndre Oppermann int mtuflags = 0; 2903fb59c426SYoshinobu Inoue #ifdef INET6 2904c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2905c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 2906c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 2907c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 2908c068736aSJeffrey Hsu #else 2909c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 2910fb59c426SYoshinobu Inoue #endif 2911df8bae1dSRodney W. Grimes 291297d8d152SAndre Oppermann /* initialize */ 291397d8d152SAndre Oppermann #ifdef INET6 291497d8d152SAndre Oppermann if (isipv6) { 2915233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(&inp->inp_inc, &mtuflags); 291697d8d152SAndre Oppermann tp->t_maxopd = tp->t_maxseg = tcp_v6mssdflt; 291797d8d152SAndre Oppermann } else 291897d8d152SAndre Oppermann #endif 291997d8d152SAndre Oppermann { 2920233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(&inp->inp_inc, &mtuflags); 292197d8d152SAndre Oppermann tp->t_maxopd = tp->t_maxseg = tcp_mssdflt; 2922df8bae1dSRodney W. Grimes } 2923df8bae1dSRodney W. Grimes so = inp->inp_socket; 2924df8bae1dSRodney W. Grimes 292597d8d152SAndre Oppermann /* 29262d166c02SAndre Oppermann * no route to sender, stay with default mss and return 292797d8d152SAndre Oppermann */ 292897d8d152SAndre Oppermann if (maxmtu == 0) 292997d8d152SAndre Oppermann return; 293097d8d152SAndre Oppermann 293197d8d152SAndre Oppermann /* what have we got? */ 293297d8d152SAndre Oppermann switch (offer) { 293397d8d152SAndre Oppermann case 0: 293497d8d152SAndre Oppermann /* 293597d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 293697d8d152SAndre Oppermann * segment, in this case we use tcp_mssdflt. 293797d8d152SAndre Oppermann */ 293897d8d152SAndre Oppermann offer = 293997d8d152SAndre Oppermann #ifdef INET6 294097d8d152SAndre Oppermann isipv6 ? tcp_v6mssdflt : 294197d8d152SAndre Oppermann #endif 294297d8d152SAndre Oppermann tcp_mssdflt; 294397d8d152SAndre Oppermann break; 294497d8d152SAndre Oppermann 294597d8d152SAndre Oppermann case -1: 2946a0292f23SGarrett Wollman /* 2947c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 2948a0292f23SGarrett Wollman */ 294997d8d152SAndre Oppermann /* FALLTHROUGH */ 295097d8d152SAndre Oppermann 295197d8d152SAndre Oppermann default: 2952a0292f23SGarrett Wollman /* 295353369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 295453369ac9SAndre Oppermann * to at least minmss. 295553369ac9SAndre Oppermann */ 295653369ac9SAndre Oppermann offer = max(offer, tcp_minmss); 295753369ac9SAndre Oppermann /* 2958a0292f23SGarrett Wollman * Sanity check: make sure that maxopd will be large 295997d8d152SAndre Oppermann * enough to allow some data on segments even if the 2960a0292f23SGarrett Wollman * all the option space is used (40bytes). Otherwise 2961a0292f23SGarrett Wollman * funny things may happen in tcp_output. 2962a0292f23SGarrett Wollman */ 2963a0292f23SGarrett Wollman offer = max(offer, 64); 296497d8d152SAndre Oppermann } 2965a0292f23SGarrett Wollman 2966df8bae1dSRodney W. Grimes /* 296797d8d152SAndre Oppermann * rmx information is now retrieved from tcp_hostcache 2968df8bae1dSRodney W. Grimes */ 296997d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 297097d8d152SAndre Oppermann 2971df8bae1dSRodney W. Grimes /* 297297d8d152SAndre Oppermann * if there's a discovered mtu int tcp hostcache, use it 2973fb59c426SYoshinobu Inoue * else, use the link mtu. 2974df8bae1dSRodney W. Grimes */ 297597d8d152SAndre Oppermann if (metrics.rmx_mtu) 29762d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 2977c068736aSJeffrey Hsu else { 297831b3783cSHajimu UMEMOTO #ifdef INET6 2979fb59c426SYoshinobu Inoue if (isipv6) { 298097d8d152SAndre Oppermann mss = maxmtu - min_protoh; 298197d8d152SAndre Oppermann if (!path_mtu_discovery && 298297d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 2983fb59c426SYoshinobu Inoue mss = min(mss, tcp_v6mssdflt); 2984b3399803SHajimu UMEMOTO } else 2985b3399803SHajimu UMEMOTO #endif 298697d8d152SAndre Oppermann { 298797d8d152SAndre Oppermann mss = maxmtu - min_protoh; 298897d8d152SAndre Oppermann if (!path_mtu_discovery && 298997d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 2990a0292f23SGarrett Wollman mss = min(mss, tcp_mssdflt); 2991a0292f23SGarrett Wollman } 299297d8d152SAndre Oppermann } 2993a0292f23SGarrett Wollman mss = min(mss, offer); 299497d8d152SAndre Oppermann 2995a0292f23SGarrett Wollman /* 2996a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 2997a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 2998a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 2999a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 3000a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 3001a0292f23SGarrett Wollman */ 3002a0292f23SGarrett Wollman tp->t_maxopd = mss; 3003a0292f23SGarrett Wollman 3004a0292f23SGarrett Wollman /* 3005c94c54e4SAndre Oppermann * origoffer==-1 indicates, that no segments were received yet. 3006c94c54e4SAndre Oppermann * In this case we just guess. 3007a0292f23SGarrett Wollman */ 3008a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 3009a0292f23SGarrett Wollman (origoffer == -1 || 3010a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 3011a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 301297d8d152SAndre Oppermann tp->t_maxseg = mss; 3013a0292f23SGarrett Wollman 3014df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 3015df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 3016df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 3017df8bae1dSRodney W. Grimes #else 3018df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 3019df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 3020df8bae1dSRodney W. Grimes #endif 302197d8d152SAndre Oppermann tp->t_maxseg = mss; 302297d8d152SAndre Oppermann 3023df8bae1dSRodney W. Grimes /* 302497d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 302597d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 302697d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 302797d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 302897d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3029df8bae1dSRodney W. Grimes */ 30303f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 303197d8d152SAndre Oppermann if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe) 303297d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 303397d8d152SAndre Oppermann else 3034df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3035df8bae1dSRodney W. Grimes if (bufsize < mss) 3036df8bae1dSRodney W. Grimes mss = bufsize; 3037df8bae1dSRodney W. Grimes else { 3038df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3039df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3040df8bae1dSRodney W. Grimes bufsize = sb_max; 304188c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 30423f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3043df8bae1dSRodney W. Grimes } 30443f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 3045df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 3046df8bae1dSRodney W. Grimes 30473f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 304897d8d152SAndre Oppermann if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe) 304997d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 305097d8d152SAndre Oppermann else 3051df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3052df8bae1dSRodney W. Grimes if (bufsize > mss) { 3053df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3054df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3055df8bae1dSRodney W. Grimes bufsize = sb_max; 305688c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 30573f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3058df8bae1dSRodney W. Grimes } 30593f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 3060a0292f23SGarrett Wollman /* 306197d8d152SAndre Oppermann * While we're here, check the others too 3062a0292f23SGarrett Wollman */ 306397d8d152SAndre Oppermann if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 306497d8d152SAndre Oppermann tp->t_srtt = rtt; 306597d8d152SAndre Oppermann tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 306697d8d152SAndre Oppermann tcpstat.tcps_usedrtt++; 306797d8d152SAndre Oppermann if (metrics.rmx_rttvar) { 306897d8d152SAndre Oppermann tp->t_rttvar = metrics.rmx_rttvar; 306997d8d152SAndre Oppermann tcpstat.tcps_usedrttvar++; 307097d8d152SAndre Oppermann } else { 307197d8d152SAndre Oppermann /* default variation is +- 1 rtt */ 307297d8d152SAndre Oppermann tp->t_rttvar = 307397d8d152SAndre Oppermann tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 307497d8d152SAndre Oppermann } 307597d8d152SAndre Oppermann TCPT_RANGESET(tp->t_rxtcur, 307697d8d152SAndre Oppermann ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 307797d8d152SAndre Oppermann tp->t_rttmin, TCPTV_REXMTMAX); 307897d8d152SAndre Oppermann } 307997d8d152SAndre Oppermann if (metrics.rmx_ssthresh) { 3080df8bae1dSRodney W. Grimes /* 3081df8bae1dSRodney W. Grimes * There's some sort of gateway or interface 3082df8bae1dSRodney W. Grimes * buffer limit on the path. Use this to set 3083df8bae1dSRodney W. Grimes * the slow start threshhold, but set the 3084df8bae1dSRodney W. Grimes * threshold to no less than 2*mss. 3085df8bae1dSRodney W. Grimes */ 308697d8d152SAndre Oppermann tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh); 3087dd224982SGarrett Wollman tcpstat.tcps_usedssthresh++; 3088df8bae1dSRodney W. Grimes } 308997d8d152SAndre Oppermann if (metrics.rmx_bandwidth) 309097d8d152SAndre Oppermann tp->snd_bandwidth = metrics.rmx_bandwidth; 309197d8d152SAndre Oppermann 309297d8d152SAndre Oppermann /* 309397d8d152SAndre Oppermann * Set the slow-start flight size depending on whether this 309497d8d152SAndre Oppermann * is a local network or not. 309597d8d152SAndre Oppermann * 309697d8d152SAndre Oppermann * Extend this so we cache the cwnd too and retrieve it here. 309797d8d152SAndre Oppermann * Make cwnd even bigger than RFC3390 suggests but only if we 309897d8d152SAndre Oppermann * have previous experience with the remote host. Be careful 309997d8d152SAndre Oppermann * not make cwnd bigger than remote receive window or our own 310097d8d152SAndre Oppermann * send socket buffer. Maybe put some additional upper bound 310197d8d152SAndre Oppermann * on the retrieved cwnd. Should do incremental updates to 310297d8d152SAndre Oppermann * hostcache when cwnd collapses so next connection doesn't 310397d8d152SAndre Oppermann * overloads the path again. 310497d8d152SAndre Oppermann * 310597d8d152SAndre Oppermann * RFC3390 says only do this if SYN or SYN/ACK didn't got lost. 310697d8d152SAndre Oppermann * We currently check only in syncache_socket for that. 310797d8d152SAndre Oppermann */ 310897d8d152SAndre Oppermann #define TCP_METRICS_CWND 310997d8d152SAndre Oppermann #ifdef TCP_METRICS_CWND 311097d8d152SAndre Oppermann if (metrics.rmx_cwnd) 311197d8d152SAndre Oppermann tp->snd_cwnd = max(mss, 311297d8d152SAndre Oppermann min(metrics.rmx_cwnd / 2, 311397d8d152SAndre Oppermann min(tp->snd_wnd, so->so_snd.sb_hiwat))); 311497d8d152SAndre Oppermann else 311597d8d152SAndre Oppermann #endif 311697d8d152SAndre Oppermann if (tcp_do_rfc3390) 311797d8d152SAndre Oppermann tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380)); 311897d8d152SAndre Oppermann #ifdef INET6 311997d8d152SAndre Oppermann else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) || 312097d8d152SAndre Oppermann (!isipv6 && in_localaddr(inp->inp_faddr))) 3121943ae302SAndre Oppermann #else 3122943ae302SAndre Oppermann else if (in_localaddr(inp->inp_faddr)) 312397d8d152SAndre Oppermann #endif 3124943ae302SAndre Oppermann tp->snd_cwnd = mss * ss_fltsz_local; 312597d8d152SAndre Oppermann else 312697d8d152SAndre Oppermann tp->snd_cwnd = mss * ss_fltsz; 3127233dcce1SAndre Oppermann 3128233dcce1SAndre Oppermann /* Check the interface for TSO capabilities. */ 3129233dcce1SAndre Oppermann if (mtuflags & CSUM_TSO) 3130233dcce1SAndre Oppermann tp->t_flags |= TF_TSO; 3131a0292f23SGarrett Wollman } 3132a0292f23SGarrett Wollman 3133a0292f23SGarrett Wollman /* 3134a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3135a0292f23SGarrett Wollman */ 3136a0292f23SGarrett Wollman int 3137ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 3138a0292f23SGarrett Wollman { 313997d8d152SAndre Oppermann int mss = 0; 314097d8d152SAndre Oppermann u_long maxmtu = 0; 314197d8d152SAndre Oppermann u_long thcmtu = 0; 314297d8d152SAndre Oppermann size_t min_protoh; 3143fb59c426SYoshinobu Inoue #ifdef INET6 314497d8d152SAndre Oppermann int isipv6 = inc->inc_isipv6 ? 1 : 0; 3145fb59c426SYoshinobu Inoue #endif 3146a0292f23SGarrett Wollman 314797d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3148a0292f23SGarrett Wollman 314931b3783cSHajimu UMEMOTO #ifdef INET6 315097d8d152SAndre Oppermann if (isipv6) { 315197d8d152SAndre Oppermann mss = tcp_v6mssdflt; 3152233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 315397d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 315497d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 315597d8d152SAndre Oppermann } else 315631b3783cSHajimu UMEMOTO #endif 315797d8d152SAndre Oppermann { 315897d8d152SAndre Oppermann mss = tcp_mssdflt; 3159233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 316097d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 316197d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 316297d8d152SAndre Oppermann } 316397d8d152SAndre Oppermann if (maxmtu && thcmtu) 316497d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 316597d8d152SAndre Oppermann else if (maxmtu || thcmtu) 316697d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 316797d8d152SAndre Oppermann 316897d8d152SAndre Oppermann return (mss); 3169df8bae1dSRodney W. Grimes } 317046f58482SJonathan Lemon 317146f58482SJonathan Lemon 317246f58482SJonathan Lemon /* 3173c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3174c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3175c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3176c068736aSJeffrey Hsu * be started again. 317746f58482SJonathan Lemon */ 3178c068736aSJeffrey Hsu static void 3179ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 318046f58482SJonathan Lemon { 318146f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 318246f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 318346f58482SJonathan Lemon 3184b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 318546f58482SJonathan Lemon tp->t_rtttime = 0; 318646f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 31876b2a5f92SJayanth Vijayaraghavan /* 3188c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3189c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 31906b2a5f92SJayanth Vijayaraghavan */ 31916b2a5f92SJayanth Vijayaraghavan tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 3192a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 31936b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 319446f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 319546f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 319646f58482SJonathan Lemon tp->snd_nxt = onxt; 319746f58482SJonathan Lemon /* 319846f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 319946f58482SJonathan Lemon * not updated yet. 320046f58482SJonathan Lemon */ 3201d7587117SPaul Saab if (tp->snd_cwnd > th->th_ack - tp->snd_una) 3202d7587117SPaul Saab tp->snd_cwnd -= th->th_ack - tp->snd_una; 3203d7587117SPaul Saab else 3204d7587117SPaul Saab tp->snd_cwnd = 0; 3205d7587117SPaul Saab tp->snd_cwnd += tp->t_maxseg; 320646f58482SJonathan Lemon } 3207340c35deSJonathan Lemon 3208340c35deSJonathan Lemon /* 3209340c35deSJonathan Lemon * Returns 1 if the TIME_WAIT state was killed and we should start over, 3210340c35deSJonathan Lemon * looking for a pcb in the listen state. Returns 0 otherwise. 3211340c35deSJonathan Lemon */ 3212340c35deSJonathan Lemon static int 3213ad3f9ab3SAndre Oppermann tcp_timewait(struct inpcb *inp, struct tcpopt *to, struct tcphdr *th, 3214ad3f9ab3SAndre Oppermann struct mbuf *m, int tlen) 3215340c35deSJonathan Lemon { 32163cbe7fafSRobert Watson struct tcptw *tw; 3217340c35deSJonathan Lemon int thflags; 3218340c35deSJonathan Lemon tcp_seq seq; 3219340c35deSJonathan Lemon #ifdef INET6 3220340c35deSJonathan Lemon int isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 3221340c35deSJonathan Lemon #else 3222340c35deSJonathan Lemon const int isipv6 = 0; 3223340c35deSJonathan Lemon #endif 3224340c35deSJonathan Lemon 3225751dea29SRuslan Ermilov /* tcbinfo lock required for tcp_twclose(), tcp_timer_2msl_reset(). */ 32263cbe7fafSRobert Watson INP_INFO_WLOCK_ASSERT(&tcbinfo); 32273cbe7fafSRobert Watson INP_LOCK_ASSERT(inp); 32283cbe7fafSRobert Watson 3229ae0e7143SRobert Watson /* 3230ae0e7143SRobert Watson * XXXRW: Time wait state for inpcb has been recycled, but inpcb is 3231ae0e7143SRobert Watson * still present. This is undesirable, but temporarily necessary 3232ae0e7143SRobert Watson * until we work out how to handle inpcb's who's timewait state has 3233ae0e7143SRobert Watson * been removed. 3234ae0e7143SRobert Watson */ 32353cbe7fafSRobert Watson tw = intotw(inp); 3236ae0e7143SRobert Watson if (tw == NULL) 3237ae0e7143SRobert Watson goto drop; 3238ae0e7143SRobert Watson 3239340c35deSJonathan Lemon thflags = th->th_flags; 3240340c35deSJonathan Lemon 3241340c35deSJonathan Lemon /* 3242340c35deSJonathan Lemon * NOTE: for FIN_WAIT_2 (to be added later), 3243340c35deSJonathan Lemon * must validate sequence number before accepting RST 3244340c35deSJonathan Lemon */ 3245340c35deSJonathan Lemon 3246340c35deSJonathan Lemon /* 3247340c35deSJonathan Lemon * If the segment contains RST: 3248340c35deSJonathan Lemon * Drop the segment - see Stevens, vol. 2, p. 964 and 3249340c35deSJonathan Lemon * RFC 1337. 3250340c35deSJonathan Lemon */ 3251340c35deSJonathan Lemon if (thflags & TH_RST) 3252340c35deSJonathan Lemon goto drop; 3253340c35deSJonathan Lemon 3254340c35deSJonathan Lemon #if 0 3255340c35deSJonathan Lemon /* PAWS not needed at the moment */ 3256340c35deSJonathan Lemon /* 3257340c35deSJonathan Lemon * RFC 1323 PAWS: If we have a timestamp reply on this segment 3258340c35deSJonathan Lemon * and it's less than ts_recent, drop it. 3259340c35deSJonathan Lemon */ 3260340c35deSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 3261340c35deSJonathan Lemon TSTMP_LT(to.to_tsval, tp->ts_recent)) { 3262340c35deSJonathan Lemon if ((thflags & TH_ACK) == 0) 3263340c35deSJonathan Lemon goto drop; 3264340c35deSJonathan Lemon goto ack; 3265340c35deSJonathan Lemon } 3266340c35deSJonathan Lemon /* 3267340c35deSJonathan Lemon * ts_recent is never updated because we never accept new segments. 3268340c35deSJonathan Lemon */ 3269340c35deSJonathan Lemon #endif 3270340c35deSJonathan Lemon 3271340c35deSJonathan Lemon /* 3272340c35deSJonathan Lemon * If a new connection request is received 3273340c35deSJonathan Lemon * while in TIME_WAIT, drop the old connection 3274340c35deSJonathan Lemon * and start over if the sequence numbers 3275340c35deSJonathan Lemon * are above the previous ones. 3276340c35deSJonathan Lemon */ 3277340c35deSJonathan Lemon if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) { 3278623dce13SRobert Watson tcp_twclose(tw, 0); 3279340c35deSJonathan Lemon return (1); 3280340c35deSJonathan Lemon } 3281340c35deSJonathan Lemon 3282340c35deSJonathan Lemon /* 3283340c35deSJonathan Lemon * Drop the the segment if it does not contain an ACK. 3284340c35deSJonathan Lemon */ 3285340c35deSJonathan Lemon if ((thflags & TH_ACK) == 0) 3286340c35deSJonathan Lemon goto drop; 3287340c35deSJonathan Lemon 3288340c35deSJonathan Lemon /* 3289340c35deSJonathan Lemon * Reset the 2MSL timer if this is a duplicate FIN. 3290340c35deSJonathan Lemon */ 3291340c35deSJonathan Lemon if (thflags & TH_FIN) { 3292340c35deSJonathan Lemon seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0); 3293340c35deSJonathan Lemon if (seq + 1 == tw->rcv_nxt) 3294751dea29SRuslan Ermilov tcp_timer_2msl_reset(tw, 1); 3295340c35deSJonathan Lemon } 3296340c35deSJonathan Lemon 3297340c35deSJonathan Lemon /* 3298272c5dfeSJonathan Lemon * Acknowledge the segment if it has data or is not a duplicate ACK. 3299340c35deSJonathan Lemon */ 3300272c5dfeSJonathan Lemon if (thflags != TH_ACK || tlen != 0 || 3301272c5dfeSJonathan Lemon th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt) 3302a7b6a14aSRobert Watson tcp_twrespond(tw, TH_ACK); 3303340c35deSJonathan Lemon goto drop; 3304340c35deSJonathan Lemon 3305340c35deSJonathan Lemon /* 3306340c35deSJonathan Lemon * Generate a RST, dropping incoming segment. 3307340c35deSJonathan Lemon * Make ACK acceptable to originator of segment. 3308340c35deSJonathan Lemon * Don't bother to respond if destination was broadcast/multicast. 3309340c35deSJonathan Lemon */ 3310340c35deSJonathan Lemon if (m->m_flags & (M_BCAST|M_MCAST)) 3311340c35deSJonathan Lemon goto drop; 3312340c35deSJonathan Lemon if (isipv6) { 3313340c35deSJonathan Lemon struct ip6_hdr *ip6; 3314340c35deSJonathan Lemon 3315340c35deSJonathan Lemon /* IPv6 anycast check is done at tcp6_input() */ 3316340c35deSJonathan Lemon ip6 = mtod(m, struct ip6_hdr *); 3317340c35deSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3318340c35deSJonathan Lemon IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3319340c35deSJonathan Lemon goto drop; 3320340c35deSJonathan Lemon } else { 3321340c35deSJonathan Lemon struct ip *ip; 3322340c35deSJonathan Lemon 3323340c35deSJonathan Lemon ip = mtod(m, struct ip *); 3324340c35deSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3325340c35deSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3326340c35deSJonathan Lemon ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3327340c35deSJonathan Lemon in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3328340c35deSJonathan Lemon goto drop; 3329340c35deSJonathan Lemon } 3330340c35deSJonathan Lemon if (thflags & TH_ACK) { 3331340c35deSJonathan Lemon tcp_respond(NULL, 3332340c35deSJonathan Lemon mtod(m, void *), th, m, 0, th->th_ack, TH_RST); 3333340c35deSJonathan Lemon } else { 3334340c35deSJonathan Lemon seq = th->th_seq + (thflags & TH_SYN ? 1 : 0); 3335340c35deSJonathan Lemon tcp_respond(NULL, 3336340c35deSJonathan Lemon mtod(m, void *), th, m, seq, 0, TH_RST|TH_ACK); 3337340c35deSJonathan Lemon } 33383cbe7fafSRobert Watson INP_UNLOCK(inp); 3339340c35deSJonathan Lemon return (0); 3340340c35deSJonathan Lemon 3341340c35deSJonathan Lemon drop: 33423cbe7fafSRobert Watson INP_UNLOCK(inp); 3343340c35deSJonathan Lemon m_freem(m); 3344340c35deSJonathan Lemon return (0); 3345340c35deSJonathan Lemon } 3346