1df8bae1dSRodney W. Grimes /* 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" 39e46cd3d4SDag-Erling Smørgrav #include "opt_tcp_input.h" 400cc12cc5SJoerg Wunsch 41df8bae1dSRodney W. Grimes #include <sys/param.h> 4298163b98SPoul-Henning Kamp #include <sys/kernel.h> 43c488362eSRobert Watson #include <sys/mac.h> 44df8bae1dSRodney W. Grimes #include <sys/malloc.h> 45df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 46a29f300eSGarrett Wollman #include <sys/proc.h> /* for proc0 declaration */ 47df8bae1dSRodney W. Grimes #include <sys/protosw.h> 48960ed29cSSeigo Tanimura #include <sys/signalvar.h> 49df8bae1dSRodney W. Grimes #include <sys/socket.h> 50df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 51960ed29cSSeigo Tanimura #include <sys/sysctl.h> 52816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 53960ed29cSSeigo Tanimura #include <sys/systm.h> 54df8bae1dSRodney W. Grimes 55e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 56e79adb8eSGarrett Wollman 5712e2e970SAndre Oppermann #include <vm/uma.h> 5812e2e970SAndre Oppermann 59df8bae1dSRodney W. Grimes #include <net/if.h> 60df8bae1dSRodney W. Grimes #include <net/route.h> 61df8bae1dSRodney W. Grimes 62df8bae1dSRodney W. Grimes #include <netinet/in.h> 63960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 64df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 65960ed29cSSeigo Tanimura #include <netinet/in_var.h> 66df8bae1dSRodney W. Grimes #include <netinet/ip.h> 67686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */ 68686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 69df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 70686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 71686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h> 72686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 73960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h> 74960ed29cSSeigo Tanimura #include <netinet6/nd6.h> 75df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 76df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 77df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 78df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 79df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 80fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 81df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 82610ee2f9SDavid Greenman #ifdef TCPDEBUG 83df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 84fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */ 85fb59c426SYoshinobu Inoue 86b9234fafSSam Leffler #ifdef FAST_IPSEC 87b9234fafSSam Leffler #include <netipsec/ipsec.h> 88b9234fafSSam Leffler #include <netipsec/ipsec6.h> 89b9234fafSSam Leffler #endif /*FAST_IPSEC*/ 90b9234fafSSam Leffler 91fb59c426SYoshinobu Inoue #ifdef IPSEC 92fb59c426SYoshinobu Inoue #include <netinet6/ipsec.h> 933a2a9f79SYoshinobu Inoue #include <netinet6/ipsec6.h> 94fb59c426SYoshinobu Inoue #include <netkey/key.h> 95fb59c426SYoshinobu Inoue #endif /*IPSEC*/ 96fb59c426SYoshinobu Inoue 97db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 98db4f9cc7SJonathan Lemon 99c068736aSJeffrey Hsu static const int tcprexmtthresh = 3; 1002f96f1f4SGarrett Wollman tcp_cc tcp_ccgen; 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 106d78a37adSPaul Traina static int log_in_vain = 0; 107816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 1083d177f46SBill Fumerola &log_in_vain, 0, "Log all incoming TCP connections"); 109816a3d83SPoul-Henning Kamp 11016f7f31fSGeoff Rehmet static int blackhole = 0; 11116f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 11216f7f31fSGeoff Rehmet &blackhole, 0, "Do not send RST when dropping refused connections"); 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 #ifdef TCP_DROP_SYNFIN 120f8613305SDag-Erling Smørgrav static int drop_synfin = 0; 121f8613305SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, 122f8613305SDag-Erling Smørgrav &drop_synfin, 0, "Drop TCP packets with SYN+FIN set"); 123f8613305SDag-Erling Smørgrav #endif 124f8613305SDag-Erling Smørgrav 125dba7bc6aSAndre Oppermann static int tcp_do_rfc3042 = 1; 126582a954bSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW, 127582a954bSJeffrey Hsu &tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)"); 128582a954bSJeffrey Hsu 129dba7bc6aSAndre Oppermann static int tcp_do_rfc3390 = 1; 130da3a8a1aSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW, 131da3a8a1aSJeffrey Hsu &tcp_do_rfc3390, 0, 132da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 133da3a8a1aSJeffrey Hsu 13412e2e970SAndre Oppermann SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0, 13512e2e970SAndre Oppermann "TCP Segment Reassembly Queue"); 13612e2e970SAndre Oppermann 13712e2e970SAndre Oppermann static int tcp_reass_maxseg = 0; 13812e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RDTUN, 13912e2e970SAndre Oppermann &tcp_reass_maxseg, 0, 14012e2e970SAndre Oppermann "Global maximum number of TCP Segments in Reassembly Queue"); 14112e2e970SAndre Oppermann 14212e2e970SAndre Oppermann int tcp_reass_qsize = 0; 14312e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, cursegments, CTLFLAG_RD, 14412e2e970SAndre Oppermann &tcp_reass_qsize, 0, 14512e2e970SAndre Oppermann "Global number of TCP Segments currently in Reassembly Queue"); 14612e2e970SAndre Oppermann 14712e2e970SAndre Oppermann static int tcp_reass_maxqlen = 48; 14812e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxqlen, CTLFLAG_RW, 14912e2e970SAndre Oppermann &tcp_reass_maxqlen, 0, 15012e2e970SAndre Oppermann "Maximum number of TCP Segments per individual Reassembly Queue"); 15112e2e970SAndre Oppermann 15212e2e970SAndre Oppermann static int tcp_reass_overflows = 0; 15312e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, overflows, CTLFLAG_RD, 15412e2e970SAndre Oppermann &tcp_reass_overflows, 0, 15512e2e970SAndre Oppermann "Global number of TCP Segment Reassembly Queue Overflows"); 15612e2e970SAndre Oppermann 15715bd2b43SDavid Greenman struct inpcbhead tcb; 158fb59c426SYoshinobu Inoue #define tcb6 tcb /* for KAME src sync over BSD*'s */ 15915bd2b43SDavid Greenman struct inpcbinfo tcbinfo; 160f76fcf6dSJeffrey Hsu struct mtx *tcbinfo_mtx; 161df8bae1dSRodney W. Grimes 1624d77a549SAlfred Perlstein static void tcp_dooptions(struct tcpopt *, u_char *, int, int); 1634d77a549SAlfred Perlstein static void tcp_pulloutofband(struct socket *, 1644d77a549SAlfred Perlstein struct tcphdr *, struct mbuf *, int); 1654d77a549SAlfred Perlstein static int tcp_reass(struct tcpcb *, struct tcphdr *, int *, 1664d77a549SAlfred Perlstein struct mbuf *); 1674d77a549SAlfred Perlstein static void tcp_xmit_timer(struct tcpcb *, int); 168c068736aSJeffrey Hsu static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 169340c35deSJonathan Lemon static int tcp_timewait(struct tcptw *, struct tcpopt *, 170340c35deSJonathan Lemon struct tcphdr *, struct mbuf *, int); 1710312fbe9SPoul-Henning Kamp 172fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 173fb59c426SYoshinobu Inoue #ifdef INET6 174fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \ 175fb59c426SYoshinobu Inoue do { \ 176fb59c426SYoshinobu Inoue if ((tp) && (tp)->t_inpcb && \ 17797d8d152SAndre Oppermann ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \ 17897d8d152SAndre Oppermann nd6_nud_hint(NULL, NULL, 0); \ 179fb59c426SYoshinobu Inoue } while (0) 180fb59c426SYoshinobu Inoue #else 181fb59c426SYoshinobu Inoue #define ND6_HINT(tp) 182fb59c426SYoshinobu Inoue #endif 183df8bae1dSRodney W. Grimes 184df8bae1dSRodney W. Grimes /* 185262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 186262c1c1aSMatthew Dillon * - there is no delayed ack timer in progress and 187262c1c1aSMatthew Dillon * - our last ack wasn't a 0-sized window. We never want to delay 1883bfd6421SJonathan Lemon * the ack that opens up a 0-sized window and 1893bfd6421SJonathan Lemon * - delayed acks are enabled or 1903bfd6421SJonathan Lemon * - this is a half-synchronized T/TCP connection. 191d8c85a26SJonathan Lemon */ 192d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \ 193a14c749fSJonathan Lemon ((!callout_active(tp->tt_delack) && \ 194a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 1953bfd6421SJonathan Lemon (tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 196d8c85a26SJonathan Lemon 19712e2e970SAndre Oppermann /* Initialize TCP reassembly queue */ 19812e2e970SAndre Oppermann uma_zone_t tcp_reass_zone; 19912e2e970SAndre Oppermann void 20012e2e970SAndre Oppermann tcp_reass_init() 20112e2e970SAndre Oppermann { 20212e2e970SAndre Oppermann tcp_reass_maxseg = nmbclusters / 16; 20312e2e970SAndre Oppermann TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments", 20412e2e970SAndre Oppermann &tcp_reass_maxseg); 20512e2e970SAndre Oppermann tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent), 20612e2e970SAndre Oppermann NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 20712e2e970SAndre Oppermann uma_zone_set_max(tcp_reass_zone, tcp_reass_maxseg); 20812e2e970SAndre Oppermann } 20912e2e970SAndre Oppermann 2100312fbe9SPoul-Henning Kamp static int 211fb59c426SYoshinobu Inoue tcp_reass(tp, th, tlenp, m) 212df8bae1dSRodney W. Grimes register struct tcpcb *tp; 213fb59c426SYoshinobu Inoue register struct tcphdr *th; 214fb59c426SYoshinobu Inoue int *tlenp; 215df8bae1dSRodney W. Grimes struct mbuf *m; 216df8bae1dSRodney W. Grimes { 217fb59c426SYoshinobu Inoue struct tseg_qent *q; 218fb59c426SYoshinobu Inoue struct tseg_qent *p = NULL; 219fb59c426SYoshinobu Inoue struct tseg_qent *nq; 22012e2e970SAndre Oppermann struct tseg_qent *te = NULL; 221df8bae1dSRodney W. Grimes struct socket *so = tp->t_inpcb->inp_socket; 222df8bae1dSRodney W. Grimes int flags; 223df8bae1dSRodney W. Grimes 224df8bae1dSRodney W. Grimes /* 22512e2e970SAndre Oppermann * XXX: tcp_reass() is rather inefficient with its data structures 22612e2e970SAndre Oppermann * and should be rewritten (see NetBSD for optimizations). While 22712e2e970SAndre Oppermann * doing that it should move to its own file tcp_reass.c. 22812e2e970SAndre Oppermann */ 22912e2e970SAndre Oppermann 23012e2e970SAndre Oppermann /* 231fb59c426SYoshinobu Inoue * Call with th==0 after become established to 232df8bae1dSRodney W. Grimes * force pre-ESTABLISHED data up to user socket. 233df8bae1dSRodney W. Grimes */ 234fb59c426SYoshinobu Inoue if (th == 0) 235df8bae1dSRodney W. Grimes goto present; 236df8bae1dSRodney W. Grimes 23712e2e970SAndre Oppermann /* 23812e2e970SAndre Oppermann * Limit the number of segments in the reassembly queue to prevent 23912e2e970SAndre Oppermann * holding on to too many segments (and thus running out of mbufs). 24012e2e970SAndre Oppermann * Make sure to let the missing segment through which caused this 24112e2e970SAndre Oppermann * queue. Always keep one global queue entry spare to be able to 24212e2e970SAndre Oppermann * process the missing segment. 24312e2e970SAndre Oppermann */ 24412e2e970SAndre Oppermann if (th->th_seq != tp->rcv_nxt && 24512e2e970SAndre Oppermann (tcp_reass_qsize + 1 >= tcp_reass_maxseg || 24612e2e970SAndre Oppermann tp->t_segqlen >= tcp_reass_maxqlen)) { 24712e2e970SAndre Oppermann tcp_reass_overflows++; 24812e2e970SAndre Oppermann tcpstat.tcps_rcvmemdrop++; 24912e2e970SAndre Oppermann m_freem(m); 25012e2e970SAndre Oppermann return (0); 25112e2e970SAndre Oppermann } 25212e2e970SAndre Oppermann 25312e2e970SAndre Oppermann /* 25412e2e970SAndre Oppermann * Allocate a new queue entry. If we can't, or hit the zone limit 25512e2e970SAndre Oppermann * just drop the pkt. 25612e2e970SAndre Oppermann */ 25712e2e970SAndre Oppermann te = uma_zalloc(tcp_reass_zone, M_NOWAIT); 258fb59c426SYoshinobu Inoue if (te == NULL) { 259fb59c426SYoshinobu Inoue tcpstat.tcps_rcvmemdrop++; 260fb59c426SYoshinobu Inoue m_freem(m); 261fb59c426SYoshinobu Inoue return (0); 262fb59c426SYoshinobu Inoue } 26312e2e970SAndre Oppermann tp->t_segqlen++; 26412e2e970SAndre Oppermann tcp_reass_qsize++; 2656effc713SDoug Rabson 266df8bae1dSRodney W. Grimes /* 267df8bae1dSRodney W. Grimes * Find a segment which begins after this one does. 268df8bae1dSRodney W. Grimes */ 269fb59c426SYoshinobu Inoue LIST_FOREACH(q, &tp->t_segq, tqe_q) { 270fb59c426SYoshinobu Inoue if (SEQ_GT(q->tqe_th->th_seq, th->th_seq)) 271df8bae1dSRodney W. Grimes break; 272fb59c426SYoshinobu Inoue p = q; 273fb59c426SYoshinobu Inoue } 274df8bae1dSRodney W. Grimes 275df8bae1dSRodney W. Grimes /* 276df8bae1dSRodney W. Grimes * If there is a preceding segment, it may provide some of 277df8bae1dSRodney W. Grimes * our data already. If so, drop the data from the incoming 278df8bae1dSRodney W. Grimes * segment. If it provides all of our data, drop us. 279df8bae1dSRodney W. Grimes */ 2806effc713SDoug Rabson if (p != NULL) { 281df8bae1dSRodney W. Grimes register int i; 282df8bae1dSRodney W. Grimes /* conversion to int (in i) handles seq wraparound */ 283fb59c426SYoshinobu Inoue i = p->tqe_th->th_seq + p->tqe_len - th->th_seq; 284df8bae1dSRodney W. Grimes if (i > 0) { 285fb59c426SYoshinobu Inoue if (i >= *tlenp) { 286df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 287fb59c426SYoshinobu Inoue tcpstat.tcps_rcvdupbyte += *tlenp; 288df8bae1dSRodney W. Grimes m_freem(m); 28912e2e970SAndre Oppermann uma_zfree(tcp_reass_zone, te); 29012e2e970SAndre Oppermann tp->t_segqlen--; 29112e2e970SAndre Oppermann tcp_reass_qsize--; 292a0292f23SGarrett Wollman /* 293a0292f23SGarrett Wollman * Try to present any queued data 294a0292f23SGarrett Wollman * at the left window edge to the user. 295a0292f23SGarrett Wollman * This is needed after the 3-WHS 296a0292f23SGarrett Wollman * completes. 297a0292f23SGarrett Wollman */ 298a0292f23SGarrett Wollman goto present; /* ??? */ 299df8bae1dSRodney W. Grimes } 300df8bae1dSRodney W. Grimes m_adj(m, i); 301fb59c426SYoshinobu Inoue *tlenp -= i; 302fb59c426SYoshinobu Inoue th->th_seq += i; 303df8bae1dSRodney W. Grimes } 304df8bae1dSRodney W. Grimes } 305df8bae1dSRodney W. Grimes tcpstat.tcps_rcvoopack++; 306fb59c426SYoshinobu Inoue tcpstat.tcps_rcvoobyte += *tlenp; 307df8bae1dSRodney W. Grimes 308df8bae1dSRodney W. Grimes /* 309df8bae1dSRodney W. Grimes * While we overlap succeeding segments trim them or, 310df8bae1dSRodney W. Grimes * if they are completely covered, dequeue them. 311df8bae1dSRodney W. Grimes */ 3126effc713SDoug Rabson while (q) { 313fb59c426SYoshinobu Inoue register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq; 314df8bae1dSRodney W. Grimes if (i <= 0) 315df8bae1dSRodney W. Grimes break; 316fb59c426SYoshinobu Inoue if (i < q->tqe_len) { 317fb59c426SYoshinobu Inoue q->tqe_th->th_seq += i; 318fb59c426SYoshinobu Inoue q->tqe_len -= i; 319fb59c426SYoshinobu Inoue m_adj(q->tqe_m, i); 320df8bae1dSRodney W. Grimes break; 321df8bae1dSRodney W. Grimes } 3226effc713SDoug Rabson 323fb59c426SYoshinobu Inoue nq = LIST_NEXT(q, tqe_q); 324fb59c426SYoshinobu Inoue LIST_REMOVE(q, tqe_q); 325fb59c426SYoshinobu Inoue m_freem(q->tqe_m); 32612e2e970SAndre Oppermann uma_zfree(tcp_reass_zone, q); 32712e2e970SAndre Oppermann tp->t_segqlen--; 32812e2e970SAndre Oppermann tcp_reass_qsize--; 3296effc713SDoug Rabson q = nq; 330df8bae1dSRodney W. Grimes } 331df8bae1dSRodney W. Grimes 332fb59c426SYoshinobu Inoue /* Insert the new segment queue entry into place. */ 333fb59c426SYoshinobu Inoue te->tqe_m = m; 334fb59c426SYoshinobu Inoue te->tqe_th = th; 335fb59c426SYoshinobu Inoue te->tqe_len = *tlenp; 336fb59c426SYoshinobu Inoue 3376effc713SDoug Rabson if (p == NULL) { 338fb59c426SYoshinobu Inoue LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q); 3396effc713SDoug Rabson } else { 340fb59c426SYoshinobu Inoue LIST_INSERT_AFTER(p, te, tqe_q); 3416effc713SDoug Rabson } 342df8bae1dSRodney W. Grimes 343df8bae1dSRodney W. Grimes present: 344df8bae1dSRodney W. Grimes /* 345df8bae1dSRodney W. Grimes * Present data to user, advancing rcv_nxt through 346df8bae1dSRodney W. Grimes * completed sequence space. 347df8bae1dSRodney W. Grimes */ 348a0292f23SGarrett Wollman if (!TCPS_HAVEESTABLISHED(tp->t_state)) 349df8bae1dSRodney W. Grimes return (0); 350fb59c426SYoshinobu Inoue q = LIST_FIRST(&tp->t_segq); 351fb59c426SYoshinobu Inoue if (!q || q->tqe_th->th_seq != tp->rcv_nxt) 352df8bae1dSRodney W. Grimes return (0); 353df8bae1dSRodney W. Grimes do { 354fb59c426SYoshinobu Inoue tp->rcv_nxt += q->tqe_len; 355fb59c426SYoshinobu Inoue flags = q->tqe_th->th_flags & TH_FIN; 356fb59c426SYoshinobu Inoue nq = LIST_NEXT(q, tqe_q); 357fb59c426SYoshinobu Inoue LIST_REMOVE(q, tqe_q); 358c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 359fb59c426SYoshinobu Inoue m_freem(q->tqe_m); 3604cc20ab1SSeigo Tanimura else 361395bb186SSam Leffler sbappendstream(&so->so_rcv, q->tqe_m); 36212e2e970SAndre Oppermann uma_zfree(tcp_reass_zone, q); 36312e2e970SAndre Oppermann tp->t_segqlen--; 36412e2e970SAndre Oppermann tcp_reass_qsize--; 3656effc713SDoug Rabson q = nq; 366fb59c426SYoshinobu Inoue } while (q && q->tqe_th->th_seq == tp->rcv_nxt); 367fb59c426SYoshinobu Inoue ND6_HINT(tp); 368df8bae1dSRodney W. Grimes sorwakeup(so); 369df8bae1dSRodney W. Grimes return (flags); 370df8bae1dSRodney W. Grimes } 371df8bae1dSRodney W. Grimes 372df8bae1dSRodney W. Grimes /* 373df8bae1dSRodney W. Grimes * TCP input routine, follows pages 65-76 of the 374df8bae1dSRodney W. Grimes * protocol specification dated September, 1981 very closely. 375df8bae1dSRodney W. Grimes */ 376fb59c426SYoshinobu Inoue #ifdef INET6 377fb59c426SYoshinobu Inoue int 378fb59c426SYoshinobu Inoue tcp6_input(mp, offp, proto) 379fb59c426SYoshinobu Inoue struct mbuf **mp; 380fb59c426SYoshinobu Inoue int *offp, proto; 381fb59c426SYoshinobu Inoue { 382fb59c426SYoshinobu Inoue register struct mbuf *m = *mp; 38333841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 384fb59c426SYoshinobu Inoue 385fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 386fb59c426SYoshinobu Inoue 387fb59c426SYoshinobu Inoue /* 388fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 389fb59c426SYoshinobu Inoue * better place to put this in? 390fb59c426SYoshinobu Inoue */ 39133841545SHajimu UMEMOTO ia6 = ip6_getdstifaddr(m); 39233841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 393fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 394fb59c426SYoshinobu Inoue 395fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 396fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 397fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 398fb59c426SYoshinobu Inoue return IPPROTO_DONE; 399fb59c426SYoshinobu Inoue } 400fb59c426SYoshinobu Inoue 401f0ffb944SJulian Elischer tcp_input(m, *offp); 402fb59c426SYoshinobu Inoue return IPPROTO_DONE; 403fb59c426SYoshinobu Inoue } 404fb59c426SYoshinobu Inoue #endif 405fb59c426SYoshinobu Inoue 406df8bae1dSRodney W. Grimes void 407f0ffb944SJulian Elischer tcp_input(m, off0) 408df8bae1dSRodney W. Grimes register struct mbuf *m; 409f0ffb944SJulian Elischer int off0; 410df8bae1dSRodney W. Grimes { 411fb59c426SYoshinobu Inoue register struct tcphdr *th; 412fb59c426SYoshinobu Inoue register struct ip *ip = NULL; 413fb59c426SYoshinobu Inoue register struct ipovly *ipov; 414f76fcf6dSJeffrey Hsu register struct inpcb *inp = NULL; 415e79adb8eSGarrett Wollman u_char *optp = NULL; 41626f9a767SRodney W. Grimes int optlen = 0; 417a0194ef1SBruce M Simpson int len, tlen, off; 418fb59c426SYoshinobu Inoue int drop_hdrlen; 419df8bae1dSRodney W. Grimes register struct tcpcb *tp = 0; 420fb59c426SYoshinobu Inoue register int thflags; 42126f9a767SRodney W. Grimes struct socket *so = 0; 422df8bae1dSRodney W. Grimes int todrop, acked, ourfinisacked, needoutput = 0; 423a0292f23SGarrett Wollman u_long tiwin; 424a0292f23SGarrett Wollman struct tcpopt to; /* options in this segment */ 42597d8d152SAndre Oppermann struct rmxp_tao tao; /* our TAO cache entry */ 426f76fcf6dSJeffrey Hsu int headlocked = 0; 42736e8826fSMax Laier struct sockaddr_in *next_hop = NULL; 428c068736aSJeffrey Hsu int rstreason; /* For badport_bandlim accounting purposes */ 429c068736aSJeffrey Hsu 430c068736aSJeffrey Hsu struct ip6_hdr *ip6 = NULL; 431c068736aSJeffrey Hsu #ifdef INET6 432c068736aSJeffrey Hsu int isipv6; 433c068736aSJeffrey Hsu #else 434c068736aSJeffrey Hsu const int isipv6 = 0; 435c068736aSJeffrey Hsu #endif 436f76fcf6dSJeffrey Hsu 437610ee2f9SDavid Greenman #ifdef TCPDEBUG 438c068736aSJeffrey Hsu /* 439c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 440c068736aSJeffrey Hsu * now IPv6. 441c068736aSJeffrey Hsu */ 442410bb1bfSLuigi Rizzo u_char tcp_saveipgen[40]; 443410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 444610ee2f9SDavid Greenman short ostate = 0; 445610ee2f9SDavid Greenman #endif 446c068736aSJeffrey Hsu 447ac9d7e26SMax Laier /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */ 4482f3f1e67SDarren Reed next_hop = m_claim_next(m, PACKET_TAG_IPFORWARD); 449fb59c426SYoshinobu Inoue #ifdef INET6 450fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 451fb59c426SYoshinobu Inoue #endif 45297d8d152SAndre Oppermann bzero(&tao, sizeof(tao)); 453a0292f23SGarrett Wollman bzero((char *)&to, sizeof(to)); 454a0292f23SGarrett Wollman 455df8bae1dSRodney W. Grimes tcpstat.tcps_rcvtotal++; 456fb59c426SYoshinobu Inoue 457fb59c426SYoshinobu Inoue if (isipv6) { 45804d3a452SHajimu UMEMOTO #ifdef INET6 459fb59c426SYoshinobu Inoue /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */ 460fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 461fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 462fb59c426SYoshinobu Inoue if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 463fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbadsum++; 464fb59c426SYoshinobu Inoue goto drop; 465fb59c426SYoshinobu Inoue } 466fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 46733841545SHajimu UMEMOTO 46833841545SHajimu UMEMOTO /* 46933841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 47033841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 47133841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 47233841545SHajimu UMEMOTO * 47333841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 47433841545SHajimu UMEMOTO * already dropped in ip6_input. 47533841545SHajimu UMEMOTO */ 47633841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 47733841545SHajimu UMEMOTO /* XXX stat */ 47833841545SHajimu UMEMOTO goto drop; 47933841545SHajimu UMEMOTO } 48004d3a452SHajimu UMEMOTO #else 48104d3a452SHajimu UMEMOTO th = NULL; /* XXX: avoid compiler warning */ 48204d3a452SHajimu UMEMOTO #endif 483c068736aSJeffrey Hsu } else { 484df8bae1dSRodney W. Grimes /* 485df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 486df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 487df8bae1dSRodney W. Grimes */ 488fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 489df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 490fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 491fb59c426SYoshinobu Inoue } 492df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 493df8bae1dSRodney W. Grimes if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 494df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 495df8bae1dSRodney W. Grimes return; 496df8bae1dSRodney W. Grimes } 497df8bae1dSRodney W. Grimes } 498fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 499fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 500db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 501db4f9cc7SJonathan Lemon tlen = ip->ip_len; 502df8bae1dSRodney W. Grimes 503db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 504db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 505db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 506db4f9cc7SJonathan Lemon else 507db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 508c068736aSJeffrey Hsu ip->ip_dst.s_addr, 509c068736aSJeffrey Hsu htonl(m->m_pkthdr.csum_data + 510c068736aSJeffrey Hsu ip->ip_len + 511c068736aSJeffrey Hsu IPPROTO_TCP)); 512db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 5133c653157SHartmut Brandt #ifdef TCPDEBUG 5143c653157SHartmut Brandt ipov->ih_len = (u_short)tlen; 5153c653157SHartmut Brandt ipov->ih_len = htons(ipov->ih_len); 5163c653157SHartmut Brandt #endif 517db4f9cc7SJonathan Lemon } else { 518df8bae1dSRodney W. Grimes /* 519df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 520df8bae1dSRodney W. Grimes */ 521df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 522fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 523fb59c426SYoshinobu Inoue ipov->ih_len = (u_short)tlen; 524fd8e4ebcSMike Barcroft ipov->ih_len = htons(ipov->ih_len); 525fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 526db4f9cc7SJonathan Lemon } 527fb59c426SYoshinobu Inoue if (th->th_sum) { 528df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadsum++; 529df8bae1dSRodney W. Grimes goto drop; 530df8bae1dSRodney W. Grimes } 531fb59c426SYoshinobu Inoue #ifdef INET6 532fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 533fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 534fb59c426SYoshinobu Inoue #endif 535fb59c426SYoshinobu Inoue } 536df8bae1dSRodney W. Grimes 537df8bae1dSRodney W. Grimes /* 538df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 539df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 540df8bae1dSRodney W. Grimes */ 541fb59c426SYoshinobu Inoue off = th->th_off << 2; 542df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 543df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadoff++; 544df8bae1dSRodney W. Grimes goto drop; 545df8bae1dSRodney W. Grimes } 546fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 547df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 548fb59c426SYoshinobu Inoue if (isipv6) { 54904d3a452SHajimu UMEMOTO #ifdef INET6 550fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 551fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 552fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 55304d3a452SHajimu UMEMOTO #endif 554c068736aSJeffrey Hsu } else { 555df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 556c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 557c068736aSJeffrey Hsu == 0) { 558df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 559df8bae1dSRodney W. Grimes return; 560df8bae1dSRodney W. Grimes } 561fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 562fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 563fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 564fb59c426SYoshinobu Inoue } 565df8bae1dSRodney W. Grimes } 566df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 567fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 568df8bae1dSRodney W. Grimes } 569fb59c426SYoshinobu Inoue thflags = th->th_flags; 570df8bae1dSRodney W. Grimes 571e46cd3d4SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN 572e46cd3d4SDag-Erling Smørgrav /* 573e46cd3d4SDag-Erling Smørgrav * If the drop_synfin option is enabled, drop all packets with 574e46cd3d4SDag-Erling Smørgrav * both the SYN and FIN bits set. This prevents e.g. nmap from 575e46cd3d4SDag-Erling Smørgrav * identifying the TCP/IP stack. 576e46cd3d4SDag-Erling Smørgrav * 577a589a70eSGarrett Wollman * This is a violation of the TCP specification. 578e46cd3d4SDag-Erling Smørgrav */ 579fb59c426SYoshinobu Inoue if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN)) 580e46cd3d4SDag-Erling Smørgrav goto drop; 581e46cd3d4SDag-Erling Smørgrav #endif 582e46cd3d4SDag-Erling Smørgrav 583df8bae1dSRodney W. Grimes /* 584df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 585df8bae1dSRodney W. Grimes */ 586fd8e4ebcSMike Barcroft th->th_seq = ntohl(th->th_seq); 587fd8e4ebcSMike Barcroft th->th_ack = ntohl(th->th_ack); 588fd8e4ebcSMike Barcroft th->th_win = ntohs(th->th_win); 589fd8e4ebcSMike Barcroft th->th_urp = ntohs(th->th_urp); 590df8bae1dSRodney W. Grimes 591df8bae1dSRodney W. Grimes /* 5923bfd6421SJonathan Lemon * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options, 593fb59c426SYoshinobu Inoue * until after ip6_savecontrol() is called and before other functions 594fb59c426SYoshinobu Inoue * which don't want those proto headers. 595fb59c426SYoshinobu Inoue * Because ip6_savecontrol() is going to parse the mbuf to 596fb59c426SYoshinobu Inoue * search for data to be passed up to user-land, it wants mbuf 597fb59c426SYoshinobu Inoue * parameters to be unchanged. 59888ff5695SSUZUKI Shinsuke * XXX: the call of ip6_savecontrol() has been obsoleted based on 59988ff5695SSUZUKI Shinsuke * latest version of the advanced API (20020110). 600755c1f07SAndras Olah */ 601fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 602755c1f07SAndras Olah 603755c1f07SAndras Olah /* 604df8bae1dSRodney W. Grimes * Locate pcb for segment. 605df8bae1dSRodney W. Grimes */ 606f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 607f76fcf6dSJeffrey Hsu headlocked = 1; 608df8bae1dSRodney W. Grimes findpcb: 6092b25acc1SLuigi Rizzo /* IPFIREWALL_FORWARD section */ 610c068736aSJeffrey Hsu if (next_hop != NULL && isipv6 == 0) { /* IPv6 support is not yet */ 611f9e354dfSJulian Elischer /* 6122b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 613f9e354dfSJulian Elischer * already got one like this? 614f9e354dfSJulian Elischer */ 615fb59c426SYoshinobu Inoue inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, 616c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 617c068736aSJeffrey Hsu 0, m->m_pkthdr.rcvif); 618f9e354dfSJulian Elischer if (!inp) { 619c068736aSJeffrey Hsu /* It's new. Try find the ambushing socket. */ 620f9e354dfSJulian Elischer inp = in_pcblookup_hash(&tcbinfo, 621fb59c426SYoshinobu Inoue ip->ip_src, th->th_sport, 6222b25acc1SLuigi Rizzo next_hop->sin_addr, 623c068736aSJeffrey Hsu next_hop->sin_port ? 624c068736aSJeffrey Hsu ntohs(next_hop->sin_port) : 625c068736aSJeffrey Hsu th->th_dport, 626c068736aSJeffrey Hsu 1, m->m_pkthdr.rcvif); 627f9e354dfSJulian Elischer } 628c068736aSJeffrey Hsu } else { 62904d3a452SHajimu UMEMOTO if (isipv6) { 63004d3a452SHajimu UMEMOTO #ifdef INET6 631c068736aSJeffrey Hsu inp = in6_pcblookup_hash(&tcbinfo, 632c068736aSJeffrey Hsu &ip6->ip6_src, th->th_sport, 633c068736aSJeffrey Hsu &ip6->ip6_dst, th->th_dport, 634c068736aSJeffrey Hsu 1, m->m_pkthdr.rcvif); 63504d3a452SHajimu UMEMOTO #endif 63604d3a452SHajimu UMEMOTO } else 637c068736aSJeffrey Hsu inp = in_pcblookup_hash(&tcbinfo, 638c068736aSJeffrey Hsu ip->ip_src, th->th_sport, 639c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 640c068736aSJeffrey Hsu 1, m->m_pkthdr.rcvif); 641fb59c426SYoshinobu Inoue } 642f9e354dfSJulian Elischer 643da0f4099SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC) 644da0f4099SHajimu UMEMOTO if (isipv6) { 645da0f4099SHajimu UMEMOTO if (inp != NULL && ipsec6_in_reject(m, inp)) { 646fb59c426SYoshinobu Inoue #ifdef IPSEC 647fb59c426SYoshinobu Inoue ipsec6stat.in_polvio++; 648da0f4099SHajimu UMEMOTO #endif /*IPSEC*/ 649fb59c426SYoshinobu Inoue goto drop; 650fb59c426SYoshinobu Inoue } 651da0f4099SHajimu UMEMOTO } else if (inp != NULL && ipsec4_in_reject(m, inp)) { 652da0f4099SHajimu UMEMOTO #ifdef IPSEC 653fb59c426SYoshinobu Inoue ipsecstat.in_polvio++; 654da0f4099SHajimu UMEMOTO #endif /*IPSEC*/ 655fb59c426SYoshinobu Inoue goto drop; 656fb59c426SYoshinobu Inoue } 657da0f4099SHajimu UMEMOTO #endif /*IPSEC || FAST_IPSEC*/ 658df8bae1dSRodney W. Grimes 659df8bae1dSRodney W. Grimes /* 660df8bae1dSRodney W. Grimes * If the state is CLOSED (i.e., TCB does not exist) then 661df8bae1dSRodney W. Grimes * all data in the incoming segment is discarded. 662df8bae1dSRodney W. Grimes * If the TCB exists but is in CLOSED state, it is embryonic, 663df8bae1dSRodney W. Grimes * but should either do a listen or a connect soon. 664df8bae1dSRodney W. Grimes */ 665816a3d83SPoul-Henning Kamp if (inp == NULL) { 6662e4e1b4cSGeoff Rehmet if (log_in_vain) { 667fb59c426SYoshinobu Inoue #ifdef INET6 668ded7008aSJuli Mallett char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2]; 669c068736aSJeffrey Hsu #else 670fb59c426SYoshinobu Inoue char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"]; 671c068736aSJeffrey Hsu #endif 67275cfc95fSAndrey A. Chernov 673fb59c426SYoshinobu Inoue if (isipv6) { 67404d3a452SHajimu UMEMOTO #ifdef INET6 675ded7008aSJuli Mallett strcpy(dbuf, "["); 676ded7008aSJuli Mallett strcpy(sbuf, "["); 677ded7008aSJuli Mallett strcat(dbuf, ip6_sprintf(&ip6->ip6_dst)); 678ded7008aSJuli Mallett strcat(sbuf, ip6_sprintf(&ip6->ip6_src)); 679ded7008aSJuli Mallett strcat(dbuf, "]"); 680ded7008aSJuli Mallett strcat(sbuf, "]"); 68104d3a452SHajimu UMEMOTO #endif 682c068736aSJeffrey Hsu } else { 683fb59c426SYoshinobu Inoue strcpy(dbuf, inet_ntoa(ip->ip_dst)); 684fb59c426SYoshinobu Inoue strcpy(sbuf, inet_ntoa(ip->ip_src)); 685fb59c426SYoshinobu Inoue } 6862e4e1b4cSGeoff Rehmet switch (log_in_vain) { 6872e4e1b4cSGeoff Rehmet case 1: 68839eb27a4SCrist J. Clark if ((thflags & TH_SYN) == 0) 6892e4e1b4cSGeoff Rehmet break; 690e4d2978dSPoul-Henning Kamp /* FALLTHROUGH */ 6912e4e1b4cSGeoff Rehmet case 2: 6922e4e1b4cSGeoff Rehmet log(LOG_INFO, 693c068736aSJeffrey Hsu "Connection attempt to TCP %s:%d " 69439eb27a4SCrist J. Clark "from %s:%d flags:0x%02x\n", 695fb59c426SYoshinobu Inoue dbuf, ntohs(th->th_dport), sbuf, 696fb59c426SYoshinobu Inoue ntohs(th->th_sport), thflags); 6972e4e1b4cSGeoff Rehmet break; 6982e4e1b4cSGeoff Rehmet default: 6992e4e1b4cSGeoff Rehmet break; 7002e4e1b4cSGeoff Rehmet } 70175cfc95fSAndrey A. Chernov } 7022e4e1b4cSGeoff Rehmet if (blackhole) { 7032e4e1b4cSGeoff Rehmet switch (blackhole) { 704828b7f40SGeoff Rehmet case 1: 705fb59c426SYoshinobu Inoue if (thflags & TH_SYN) 706828b7f40SGeoff Rehmet goto drop; 707828b7f40SGeoff Rehmet break; 708828b7f40SGeoff Rehmet case 2: 709828b7f40SGeoff Rehmet goto drop; 710828b7f40SGeoff Rehmet default: 711828b7f40SGeoff Rehmet goto drop; 7122e4e1b4cSGeoff Rehmet } 713828b7f40SGeoff Rehmet } 714a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 715a57815efSBosko Milekic goto dropwithreset; 716816a3d83SPoul-Henning Kamp } 717f76fcf6dSJeffrey Hsu INP_LOCK(inp); 718340c35deSJonathan Lemon if (inp->inp_vflag & INP_TIMEWAIT) { 719340c35deSJonathan Lemon /* 720340c35deSJonathan Lemon * The only option of relevance is TOF_CC, and only if 721340c35deSJonathan Lemon * present in a SYN segment. See tcp_timewait(). 722340c35deSJonathan Lemon */ 723340c35deSJonathan Lemon if (thflags & TH_SYN) 724340c35deSJonathan Lemon tcp_dooptions(&to, optp, optlen, 1); 725340c35deSJonathan Lemon if (tcp_timewait((struct tcptw *)inp->inp_ppcb, 726340c35deSJonathan Lemon &to, th, m, tlen)) 727340c35deSJonathan Lemon goto findpcb; 728340c35deSJonathan Lemon /* 729340c35deSJonathan Lemon * tcp_timewait unlocks inp. 730340c35deSJonathan Lemon */ 731340c35deSJonathan Lemon INP_INFO_WUNLOCK(&tcbinfo); 732340c35deSJonathan Lemon return; 733340c35deSJonathan Lemon } 734df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 73509f81a46SBosko Milekic if (tp == 0) { 736f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 737a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 738a57815efSBosko Milekic goto dropwithreset; 73909f81a46SBosko Milekic } 740df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_CLOSED) 741df8bae1dSRodney W. Grimes goto drop; 742df8bae1dSRodney W. Grimes 743df8bae1dSRodney W. Grimes /* Unscale the window into a 32-bit value. */ 744fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) == 0) 745fb59c426SYoshinobu Inoue tiwin = th->th_win << tp->snd_scale; 746df8bae1dSRodney W. Grimes else 747fb59c426SYoshinobu Inoue tiwin = th->th_win; 748fb59c426SYoshinobu Inoue 749c488362eSRobert Watson #ifdef MAC 750a557af22SRobert Watson if (mac_check_inpcb_deliver(inp, m)) 751c488362eSRobert Watson goto drop; 752c488362eSRobert Watson #endif 753a557af22SRobert Watson so = inp->inp_socket; 754610ee2f9SDavid Greenman #ifdef TCPDEBUG 755df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 756df8bae1dSRodney W. Grimes ostate = tp->t_state; 757fb59c426SYoshinobu Inoue if (isipv6) 758540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 759fb59c426SYoshinobu Inoue else 760540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 761fb59c426SYoshinobu Inoue tcp_savetcp = *th; 762df8bae1dSRodney W. Grimes } 763610ee2f9SDavid Greenman #endif 764540e8b7eSJeffrey Hsu if (so->so_options & SO_ACCEPTCONN) { 765540e8b7eSJeffrey Hsu struct in_conninfo inc; 766540e8b7eSJeffrey Hsu 767fb59c426SYoshinobu Inoue #ifdef INET6 768be2ac88cSJonathan Lemon inc.inc_isipv6 = isipv6; 769c068736aSJeffrey Hsu #endif 770be2ac88cSJonathan Lemon if (isipv6) { 771be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 772be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 773c068736aSJeffrey Hsu } else { 774be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 775be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 776be2ac88cSJonathan Lemon } 777be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 778be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 779be2ac88cSJonathan Lemon 780fb59c426SYoshinobu Inoue /* 781be2ac88cSJonathan Lemon * If the state is LISTEN then ignore segment if it contains 782be2ac88cSJonathan Lemon * a RST. If the segment contains an ACK then it is bad and 783be2ac88cSJonathan Lemon * send a RST. If it does not contain a SYN then it is not 784be2ac88cSJonathan Lemon * interesting; drop it. 785be2ac88cSJonathan Lemon * 786be2ac88cSJonathan Lemon * If the state is SYN_RECEIVED (syncache) and seg contains 787be2ac88cSJonathan Lemon * an ACK, but not for our SYN/ACK, send a RST. If the seg 788be2ac88cSJonathan Lemon * contains a RST, check the sequence number to see if it 789be2ac88cSJonathan Lemon * is a valid reset segment. 790fb59c426SYoshinobu Inoue */ 791fb59c426SYoshinobu Inoue if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 792be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 793be2ac88cSJonathan Lemon if (!syncache_expand(&inc, th, &so, m)) { 7944195b4afSPaul Traina /* 795be2ac88cSJonathan Lemon * No syncache entry, or ACK was not 796be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 7974195b4afSPaul Traina */ 798be2ac88cSJonathan Lemon tcpstat.tcps_badsyn++; 799be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 800be2ac88cSJonathan Lemon goto dropwithreset; 801be2ac88cSJonathan Lemon } 802f76fcf6dSJeffrey Hsu if (so == NULL) { 803be2ac88cSJonathan Lemon /* 804be2ac88cSJonathan Lemon * Could not complete 3-way handshake, 805be2ac88cSJonathan Lemon * connection is being closed down, and 806be2ac88cSJonathan Lemon * syncache will free mbuf. 807be2ac88cSJonathan Lemon */ 808f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 809f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 810be2ac88cSJonathan Lemon return; 811f76fcf6dSJeffrey Hsu } 812be2ac88cSJonathan Lemon /* 813be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 814be2ac88cSJonathan Lemon * Continue processing segment. 815be2ac88cSJonathan Lemon */ 816f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 817be2ac88cSJonathan Lemon inp = sotoinpcb(so); 818f76fcf6dSJeffrey Hsu INP_LOCK(inp); 819be2ac88cSJonathan Lemon tp = intotcpcb(inp); 820be2ac88cSJonathan Lemon /* 821be2ac88cSJonathan Lemon * This is what would have happened in 822e6658b12SRobert Watson * tcp_output() when the SYN,ACK was sent. 823be2ac88cSJonathan Lemon */ 824be2ac88cSJonathan Lemon tp->snd_up = tp->snd_una; 825be2ac88cSJonathan Lemon tp->snd_max = tp->snd_nxt = tp->iss + 1; 826be2ac88cSJonathan Lemon tp->last_ack_sent = tp->rcv_nxt; 827be2ac88cSJonathan Lemon /* 82841446225SJonathan Lemon * RFC1323: The window in SYN & SYN/ACK 82941446225SJonathan Lemon * segments is never scaled. 830be2ac88cSJonathan Lemon */ 831be2ac88cSJonathan Lemon tp->snd_wnd = tiwin; /* unscaled */ 832be2ac88cSJonathan Lemon goto after_listen; 833be2ac88cSJonathan Lemon } 834be2ac88cSJonathan Lemon if (thflags & TH_RST) { 835be2ac88cSJonathan Lemon syncache_chkrst(&inc, th); 836be2ac88cSJonathan Lemon goto drop; 837be2ac88cSJonathan Lemon } 838fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 839be2ac88cSJonathan Lemon syncache_badack(&inc); 840ebb0cbeaSPaul Traina tcpstat.tcps_badsyn++; 841a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 842a57815efSBosko Milekic goto dropwithreset; 8434195b4afSPaul Traina } 844ebb0cbeaSPaul Traina goto drop; 845ebb0cbeaSPaul Traina } 84633841545SHajimu UMEMOTO 847be2ac88cSJonathan Lemon /* 848be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 849be2ac88cSJonathan Lemon */ 85033841545SHajimu UMEMOTO #ifdef INET6 85133841545SHajimu UMEMOTO /* 85233841545SHajimu UMEMOTO * If deprecated address is forbidden, 85333841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 85433841545SHajimu UMEMOTO * address to prevent any new inbound connection from 85533841545SHajimu UMEMOTO * getting established. 85633841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 85733841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 85833841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 85933841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 86033841545SHajimu UMEMOTO * for the exchange. 86133841545SHajimu UMEMOTO * 86233841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 86333841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 86433841545SHajimu UMEMOTO * SYN in this case. 86533841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 86633841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 86733841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 86833841545SHajimu UMEMOTO * used" 86933841545SHajimu UMEMOTO * 2. use of it with new communication: 87033841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 87133841545SHajimu UMEMOTO * with sufficient scope is available" 87233841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 87333841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 87433841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 87533841545SHajimu UMEMOTO * 87633841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 87733841545SHajimu UMEMOTO * multiple description text for deprecated address 87833841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 87933841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 88033841545SHajimu UMEMOTO */ 88133841545SHajimu UMEMOTO if (isipv6 && !ip6_use_deprecated) { 88233841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 88333841545SHajimu UMEMOTO 88433841545SHajimu UMEMOTO if ((ia6 = ip6_getdstifaddr(m)) && 88533841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 886f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 88733841545SHajimu UMEMOTO tp = NULL; 88833841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 88933841545SHajimu UMEMOTO goto dropwithreset; 89033841545SHajimu UMEMOTO } 89133841545SHajimu UMEMOTO } 89233841545SHajimu UMEMOTO #endif 89365f28919SJesper Skriver /* 894be2ac88cSJonathan Lemon * If it is from this socket, drop it, it must be forged. 895be2ac88cSJonathan Lemon * Don't bother responding if the destination was a broadcast. 89665f28919SJesper Skriver */ 897be2ac88cSJonathan Lemon if (th->th_dport == th->th_sport) { 898fb59c426SYoshinobu Inoue if (isipv6) { 899be2ac88cSJonathan Lemon if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 900be2ac88cSJonathan Lemon &ip6->ip6_src)) 901be2ac88cSJonathan Lemon goto drop; 902c068736aSJeffrey Hsu } else { 903be2ac88cSJonathan Lemon if (ip->ip_dst.s_addr == ip->ip_src.s_addr) 904be2ac88cSJonathan Lemon goto drop; 905be2ac88cSJonathan Lemon } 906c068736aSJeffrey Hsu } 907be2ac88cSJonathan Lemon /* 908be2ac88cSJonathan Lemon * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 9092ca2159fSCrist J. Clark * 91093ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 91193ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 91293ec91baSCrist J. Clark * in_broadcast() to find them. 913be2ac88cSJonathan Lemon */ 914be2ac88cSJonathan Lemon if (m->m_flags & (M_BCAST|M_MCAST)) 915be2ac88cSJonathan Lemon goto drop; 916be2ac88cSJonathan Lemon if (isipv6) { 917be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 918be2ac88cSJonathan Lemon IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 919be2ac88cSJonathan Lemon goto drop; 920c068736aSJeffrey Hsu } else { 921be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 922be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 9232ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 9242ca2159fSCrist J. Clark in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 925be2ac88cSJonathan Lemon goto drop; 926c068736aSJeffrey Hsu } 927be2ac88cSJonathan Lemon /* 928be2ac88cSJonathan Lemon * SYN appears to be valid; create compressed TCP state 929be2ac88cSJonathan Lemon * for syncache, or perform t/tcp connection. 930be2ac88cSJonathan Lemon */ 931be2ac88cSJonathan Lemon if (so->so_qlen <= so->so_qlimit) { 9323c653157SHartmut Brandt #ifdef TCPDEBUG 9333c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 9343c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 9353c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 9363c653157SHartmut Brandt #endif 937be2ac88cSJonathan Lemon tcp_dooptions(&to, optp, optlen, 1); 938be2ac88cSJonathan Lemon if (!syncache_add(&inc, &to, th, &so, m)) 939be2ac88cSJonathan Lemon goto drop; 940f76fcf6dSJeffrey Hsu if (so == NULL) { 941be2ac88cSJonathan Lemon /* 942be2ac88cSJonathan Lemon * Entry added to syncache, mbuf used to 943be2ac88cSJonathan Lemon * send SYN,ACK packet. 944be2ac88cSJonathan Lemon */ 945f76fcf6dSJeffrey Hsu KASSERT(headlocked, ("headlocked")); 946f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 947f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 948be2ac88cSJonathan Lemon return; 949f76fcf6dSJeffrey Hsu } 950be2ac88cSJonathan Lemon /* 951be2ac88cSJonathan Lemon * Segment passed TAO tests. 952be2ac88cSJonathan Lemon */ 953f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 954be2ac88cSJonathan Lemon inp = sotoinpcb(so); 955f76fcf6dSJeffrey Hsu INP_LOCK(inp); 956df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 957be2ac88cSJonathan Lemon tp->snd_wnd = tiwin; 958be2ac88cSJonathan Lemon tp->t_starttime = ticks; 959be2ac88cSJonathan Lemon tp->t_state = TCPS_ESTABLISHED; 960df8bae1dSRodney W. Grimes 961be2ac88cSJonathan Lemon /* 9623bfd6421SJonathan Lemon * T/TCP logic: 9633bfd6421SJonathan Lemon * If there is a FIN or if there is data, then 9643bfd6421SJonathan Lemon * delay SYN,ACK(SYN) in the hope of piggy-backing 9653bfd6421SJonathan Lemon * it on a response segment. Otherwise must send 9663bfd6421SJonathan Lemon * ACK now in case the other side is slow starting. 967be2ac88cSJonathan Lemon */ 9683bfd6421SJonathan Lemon if (thflags & TH_FIN || tlen != 0) 9693bfd6421SJonathan Lemon tp->t_flags |= (TF_DELACK | TF_NEEDSYN); 9703bfd6421SJonathan Lemon else 971f243998bSJonathan Lemon tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 972be2ac88cSJonathan Lemon tcpstat.tcps_connects++; 973be2ac88cSJonathan Lemon soisconnected(so); 974be2ac88cSJonathan Lemon goto trimthenstep6; 975df8bae1dSRodney W. Grimes } 976be2ac88cSJonathan Lemon goto drop; 9774cc20ab1SSeigo Tanimura } 978be2ac88cSJonathan Lemon after_listen: 979be2ac88cSJonathan Lemon 980be2ac88cSJonathan Lemon /* XXX temp debugging */ 981be2ac88cSJonathan Lemon /* should not happen - syncache should pick up these connections */ 982be2ac88cSJonathan Lemon if (tp->t_state == TCPS_LISTEN) 983be2ac88cSJonathan Lemon panic("tcp_input: TCPS_LISTEN"); 984df8bae1dSRodney W. Grimes 985df8bae1dSRodney W. Grimes /* 98653369ac9SAndre Oppermann * This is the second part of the MSS DoS prevention code (after 98753369ac9SAndre Oppermann * minmss on the sending side) and it deals with too many too small 98853369ac9SAndre Oppermann * tcp packets in a too short timeframe (1 second). 98953369ac9SAndre Oppermann * 99053369ac9SAndre Oppermann * For every full second we count the number of received packets 99153369ac9SAndre Oppermann * and bytes. If we get a lot of packets per second for this connection 99253369ac9SAndre Oppermann * (tcp_minmssoverload) we take a closer look at it and compute the 99353369ac9SAndre Oppermann * average packet size for the past second. If that is less than 99453369ac9SAndre Oppermann * tcp_minmss we get too many packets with very small payload which 99553369ac9SAndre Oppermann * is not good and burdens our system (and every packet generates 99653369ac9SAndre Oppermann * a wakeup to the process connected to our socket). We can reasonable 99753369ac9SAndre Oppermann * expect this to be small packet DoS attack to exhaust our CPU 99853369ac9SAndre Oppermann * cycles. 99953369ac9SAndre Oppermann * 100053369ac9SAndre Oppermann * Care has to be taken for the minimum packet overload value. This 100153369ac9SAndre Oppermann * value defines the minimum number of packets per second before we 100253369ac9SAndre Oppermann * start to worry. This must not be too low to avoid killing for 100353369ac9SAndre Oppermann * example interactive connections with many small packets like 100453369ac9SAndre Oppermann * telnet or SSH. 100553369ac9SAndre Oppermann * 100653369ac9SAndre Oppermann * Setting either tcp_minmssoverload or tcp_minmss to "0" disables 100753369ac9SAndre Oppermann * this check. 100853369ac9SAndre Oppermann * 100953369ac9SAndre Oppermann * Account for packet if payload packet, skip over ACK, etc. 101053369ac9SAndre Oppermann */ 101153369ac9SAndre Oppermann if (tcp_minmss && tcp_minmssoverload && 101253369ac9SAndre Oppermann tp->t_state == TCPS_ESTABLISHED && tlen > 0) { 101353369ac9SAndre Oppermann if (tp->rcv_second > ticks) { 101453369ac9SAndre Oppermann tp->rcv_pps++; 101553369ac9SAndre Oppermann tp->rcv_byps += tlen + off; 101653369ac9SAndre Oppermann if (tp->rcv_pps > tcp_minmssoverload) { 101753369ac9SAndre Oppermann if ((tp->rcv_byps / tp->rcv_pps) < tcp_minmss) { 101853369ac9SAndre Oppermann printf("too many small tcp packets from " 101953369ac9SAndre Oppermann "%s:%u, av. %lubyte/packet, " 102053369ac9SAndre Oppermann "dropping connection\n", 102153369ac9SAndre Oppermann #ifdef INET6 102253369ac9SAndre Oppermann isipv6 ? 102353369ac9SAndre Oppermann ip6_sprintf(&inp->inp_inc.inc6_faddr) : 102453369ac9SAndre Oppermann #endif 102553369ac9SAndre Oppermann inet_ntoa(inp->inp_inc.inc_faddr), 102653369ac9SAndre Oppermann inp->inp_inc.inc_fport, 102753369ac9SAndre Oppermann tp->rcv_byps / tp->rcv_pps); 102853369ac9SAndre Oppermann tp = tcp_drop(tp, ECONNRESET); 102953369ac9SAndre Oppermann tcpstat.tcps_minmssdrops++; 103053369ac9SAndre Oppermann goto drop; 103153369ac9SAndre Oppermann } 103253369ac9SAndre Oppermann } 103353369ac9SAndre Oppermann } else { 103453369ac9SAndre Oppermann tp->rcv_second = ticks + hz; 103553369ac9SAndre Oppermann tp->rcv_pps = 1; 103653369ac9SAndre Oppermann tp->rcv_byps = tlen + off; 103753369ac9SAndre Oppermann } 103853369ac9SAndre Oppermann } 103953369ac9SAndre Oppermann 104053369ac9SAndre Oppermann /* 1041df8bae1dSRodney W. Grimes * Segment received on connection. 1042df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1043df8bae1dSRodney W. Grimes */ 10449b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 10457ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 10469b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp); 1047df8bae1dSRodney W. Grimes 1048df8bae1dSRodney W. Grimes /* 104997d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 105097d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 105197d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1052df8bae1dSRodney W. Grimes */ 1053be2ac88cSJonathan Lemon tcp_dooptions(&to, optp, optlen, thflags & TH_SYN); 1054be2ac88cSJonathan Lemon if (thflags & TH_SYN) { 1055be2ac88cSJonathan Lemon if (to.to_flags & TOF_SCALE) { 1056be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 1057be2ac88cSJonathan Lemon tp->requested_s_scale = to.to_requested_s_scale; 1058be2ac88cSJonathan Lemon } 1059be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1060be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1061be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1062be2ac88cSJonathan Lemon tp->ts_recent_age = ticks; 1063be2ac88cSJonathan Lemon } 1064be2ac88cSJonathan Lemon if (to.to_flags & (TOF_CC|TOF_CCNEW)) 1065be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_CC; 1066be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1067be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 1068be2ac88cSJonathan Lemon } 1069df8bae1dSRodney W. Grimes 1070df8bae1dSRodney W. Grimes /* 1071df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1072df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1073df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1074df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1075df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1076df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1077df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1078df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1079df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1080df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1081df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1082df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1083a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1084a0292f23SGarrett Wollman * Since we check for TCPS_ESTABLISHED above, it can only 1085a0292f23SGarrett Wollman * be TH_NEEDSYN. 1086df8bae1dSRodney W. Grimes */ 1087df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1088fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1089a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1090be2ac88cSJonathan Lemon ((to.to_flags & TOF_TS) == 0 || 1091a0292f23SGarrett Wollman TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 1092a0292f23SGarrett Wollman /* 1093a0292f23SGarrett Wollman * Using the CC option is compulsory if once started: 1094a0292f23SGarrett Wollman * the segment is OK if no T/TCP was negotiated or 1095a0292f23SGarrett Wollman * if the segment has a CC option equal to CCrecv 1096a0292f23SGarrett Wollman */ 1097a0292f23SGarrett Wollman ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) || 1098be2ac88cSJonathan Lemon ((to.to_flags & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) && 1099fb59c426SYoshinobu Inoue th->th_seq == tp->rcv_nxt && 1100df8bae1dSRodney W. Grimes tiwin && tiwin == tp->snd_wnd && 1101df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) { 1102df8bae1dSRodney W. Grimes 1103df8bae1dSRodney W. Grimes /* 1104df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1105df8bae1dSRodney W. Grimes * record the timestamp. 1106a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1107a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1108df8bae1dSRodney W. Grimes */ 1109be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1110fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 11119b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1112a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1113df8bae1dSRodney W. Grimes } 1114df8bae1dSRodney W. Grimes 1115fb59c426SYoshinobu Inoue if (tlen == 0) { 1116fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1117fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1118233e8c18SGarrett Wollman tp->snd_cwnd >= tp->snd_wnd && 1119cb942153SJeffrey Hsu ((!tcp_do_newreno && 1120cb942153SJeffrey Hsu tp->t_dupacks < tcprexmtthresh) || 11219d11646dSJeffrey Hsu (tcp_do_newreno && !IN_FASTRECOVERY(tp)))) { 1122f76fcf6dSJeffrey Hsu KASSERT(headlocked, ("headlocked")); 1123f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 1124df8bae1dSRodney W. Grimes /* 1125df8bae1dSRodney W. Grimes * this is a pure ack for outstanding data. 1126df8bae1dSRodney W. Grimes */ 1127df8bae1dSRodney W. Grimes ++tcpstat.tcps_predack; 11289b8b58e0SJonathan Lemon /* 11299b8b58e0SJonathan Lemon * "bad retransmit" recovery 11309b8b58e0SJonathan Lemon */ 11319b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 11329b8b58e0SJonathan Lemon ticks < tp->t_badrxtwin) { 1133cb942153SJeffrey Hsu ++tcpstat.tcps_sndrexmitbad; 11349b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 11359b8b58e0SJonathan Lemon tp->snd_ssthresh = 11369b8b58e0SJonathan Lemon tp->snd_ssthresh_prev; 11379d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 11389d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 11399d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 11409b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 11419b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; 11429b8b58e0SJonathan Lemon } 1143fa55172bSMatthew Dillon 1144fa55172bSMatthew Dillon /* 1145fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1146fa55172bSMatthew Dillon * 1147fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1148fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1149fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1150fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1151fa55172bSMatthew Dillon */ 1152fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1153fa55172bSMatthew Dillon to.to_tsecr) { 1154a0292f23SGarrett Wollman tcp_xmit_timer(tp, 11559b8b58e0SJonathan Lemon ticks - to.to_tsecr + 1); 1156fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1157fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1158c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1159c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1160fa55172bSMatthew Dillon } 11611fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 1162fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 1163df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 1164df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 1165df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 11669d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 11679d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 11689d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 11699d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 11701645d090SJeff Roberson /* 11711645d090SJeff Roberson * pull snd_wl2 up to prevent seq wrap relative 11721645d090SJeff Roberson * to th_ack. 11731645d090SJeff Roberson */ 1174cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1175b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1176df8bae1dSRodney W. Grimes m_freem(m); 1177fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 1178df8bae1dSRodney W. Grimes 1179df8bae1dSRodney W. Grimes /* 1180df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1181df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1182df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1183df8bae1dSRodney W. Grimes * If process is waiting for space, 1184df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1185df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1186df8bae1dSRodney W. Grimes * decide between more output or persist. 11873c653157SHartmut Brandt 11883c653157SHartmut Brandt #ifdef TCPDEBUG 11893c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 11903c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 11913c653157SHartmut Brandt (void *)tcp_saveipgen, 11923c653157SHartmut Brandt &tcp_savetcp, 0); 11933c653157SHartmut Brandt #endif 1194df8bae1dSRodney W. Grimes */ 1195df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 11969b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 11979b8b58e0SJonathan Lemon else if (!callout_active(tp->tt_persist)) 11989b8b58e0SJonathan Lemon callout_reset(tp->tt_rexmt, 11999b8b58e0SJonathan Lemon tp->t_rxtcur, 12009b8b58e0SJonathan Lemon tcp_timer_rexmt, tp); 1201df8bae1dSRodney W. Grimes 1202df8bae1dSRodney W. Grimes sowwakeup(so); 1203df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 1204df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1205a14c749fSJonathan Lemon goto check_delack; 1206df8bae1dSRodney W. Grimes } 1207fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1208fb59c426SYoshinobu Inoue LIST_EMPTY(&tp->t_segq) && 1209fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 1210f76fcf6dSJeffrey Hsu KASSERT(headlocked, ("headlocked")); 1211f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 1212df8bae1dSRodney W. Grimes /* 1213df8bae1dSRodney W. Grimes * this is a pure, in-sequence data packet 1214df8bae1dSRodney W. Grimes * with nothing on the reassembly queue and 1215df8bae1dSRodney W. Grimes * we have enough buffer space to take it. 1216df8bae1dSRodney W. Grimes */ 1217df8bae1dSRodney W. Grimes ++tcpstat.tcps_preddat; 1218fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 12191645d090SJeff Roberson /* 12201645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 12211645d090SJeff Roberson * th_seq. 12221645d090SJeff Roberson */ 12231645d090SJeff Roberson tp->snd_wl1 = th->th_seq; 12241645d090SJeff Roberson /* 12251645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 12261645d090SJeff Roberson * rcv_nxt. 12271645d090SJeff Roberson */ 12281645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 1229df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpack++; 1230fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyte += tlen; 1231fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 1232df8bae1dSRodney W. Grimes /* 12333c653157SHartmut Brandt #ifdef TCPDEBUG 12343c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 12353c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 12363c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 12373c653157SHartmut Brandt #endif 1238755c1f07SAndras Olah * Add data to socket buffer. 1239df8bae1dSRodney W. Grimes */ 1240c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1241c1c36a2cSMike Silbersack m_freem(m); 1242c1c36a2cSMike Silbersack } else { 1243fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 1244395bb186SSam Leffler sbappendstream(&so->so_rcv, m); 1245c1c36a2cSMike Silbersack } 1246df8bae1dSRodney W. Grimes sorwakeup(so); 1247d8c85a26SJonathan Lemon if (DELAY_ACK(tp)) { 12483bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1249f498eeeeSDavid Greenman } else { 1250e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 1251e612a582SDavid Greenman tcp_output(tp); 1252e612a582SDavid Greenman } 1253a14c749fSJonathan Lemon goto check_delack; 1254df8bae1dSRodney W. Grimes } 1255df8bae1dSRodney W. Grimes } 1256df8bae1dSRodney W. Grimes 1257df8bae1dSRodney W. Grimes /* 1258df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1259df8bae1dSRodney W. Grimes * and then do TCP input processing. 1260df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1261df8bae1dSRodney W. Grimes * but not less than advertised window. 1262df8bae1dSRodney W. Grimes */ 1263df8bae1dSRodney W. Grimes { int win; 1264df8bae1dSRodney W. Grimes 1265df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1266df8bae1dSRodney W. Grimes if (win < 0) 1267df8bae1dSRodney W. Grimes win = 0; 126866e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1269df8bae1dSRodney W. Grimes } 1270df8bae1dSRodney W. Grimes 1271df8bae1dSRodney W. Grimes switch (tp->t_state) { 1272df8bae1dSRodney W. Grimes 1273df8bae1dSRodney W. Grimes /* 1274764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1275764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1276764d8cefSBill Fenner */ 1277764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1278fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1279fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1280a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1281a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1282a57815efSBosko Milekic goto dropwithreset; 1283a57815efSBosko Milekic } 1284764d8cefSBill Fenner break; 1285764d8cefSBill Fenner 1286764d8cefSBill Fenner /* 1287df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1288df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1289df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1290df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1291df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1292df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1293df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1294df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1295df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1296df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1297df8bae1dSRodney W. Grimes */ 1298df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 129997d8d152SAndre Oppermann if (tcp_do_rfc1644) 130097d8d152SAndre Oppermann tcp_hc_gettao(&inp->inp_inc, &tao); 1301a0292f23SGarrett Wollman 1302fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1303fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1304fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1305a0292f23SGarrett Wollman /* 1306a0292f23SGarrett Wollman * If we have a cached CCsent for the remote host, 1307a0292f23SGarrett Wollman * hence we haven't just crashed and restarted, 1308a0292f23SGarrett Wollman * do not send a RST. This may be a retransmission 1309a0292f23SGarrett Wollman * from the other side after our earlier ACK was lost. 1310a0292f23SGarrett Wollman * Our new SYN, when it arrives, will serve as the 1311a0292f23SGarrett Wollman * needed ACK. 1312a0292f23SGarrett Wollman */ 131397d8d152SAndre Oppermann if (tao.tao_ccsent != 0) 1314a0292f23SGarrett Wollman goto drop; 1315a57815efSBosko Milekic else { 1316a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1317a0292f23SGarrett Wollman goto dropwithreset; 1318a0292f23SGarrett Wollman } 1319a57815efSBosko Milekic } 1320fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 1321fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1322df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 1323df8bae1dSRodney W. Grimes goto drop; 1324df8bae1dSRodney W. Grimes } 1325fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) == 0) 1326df8bae1dSRodney W. Grimes goto drop; 1327fb59c426SYoshinobu Inoue tp->snd_wnd = th->th_win; /* initial send window */ 1328a0292f23SGarrett Wollman tp->cc_recv = to.to_cc; /* foreign CC */ 1329a0292f23SGarrett Wollman 1330fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1331df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1332fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1333a0292f23SGarrett Wollman /* 1334a0292f23SGarrett Wollman * Our SYN was acked. If segment contains CC.ECHO 1335a0292f23SGarrett Wollman * option, check it to make sure this segment really 1336a0292f23SGarrett Wollman * matches our SYN. If not, just drop it as old 1337a0292f23SGarrett Wollman * duplicate, but send an RST if we're still playing 1338026650e5SBill Fenner * by the old rules. If no CC.ECHO option, make sure 1339026650e5SBill Fenner * we don't get fooled into using T/TCP. 1340a0292f23SGarrett Wollman */ 1341be2ac88cSJonathan Lemon if (to.to_flags & TOF_CCECHO) { 1342dfd5dee1SPeter Wemm if (tp->cc_send != to.to_ccecho) { 134397d8d152SAndre Oppermann if (tao.tao_ccsent != 0) 1344a0292f23SGarrett Wollman goto drop; 1345a57815efSBosko Milekic else { 1346a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1347a0292f23SGarrett Wollman goto dropwithreset; 1348dfd5dee1SPeter Wemm } 1349a57815efSBosko Milekic } 1350026650e5SBill Fenner } else 1351026650e5SBill Fenner tp->t_flags &= ~TF_RCVD_CC; 1352845799c1SAndras Olah tcpstat.tcps_connects++; 1353845799c1SAndras Olah soisconnected(so); 1354c488362eSRobert Watson #ifdef MAC 1355310e7cebSRobert Watson SOCK_LOCK(so); 1356c488362eSRobert Watson mac_set_socket_peer_from_mbuf(m, so); 1357310e7cebSRobert Watson SOCK_UNLOCK(so); 1358c488362eSRobert Watson #endif 1359845799c1SAndras Olah /* Do window scaling on this connection? */ 1360845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1361845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1362845799c1SAndras Olah tp->snd_scale = tp->requested_s_scale; 1363845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1364845799c1SAndras Olah } 1365a0292f23SGarrett Wollman /* Segment is acceptable, update cache if undefined. */ 136697d8d152SAndre Oppermann if (tao.tao_ccsent == 0 && tcp_do_rfc1644) 136797d8d152SAndre Oppermann tcp_hc_updatetao(&inp->inp_inc, TCP_HC_TAO_CCSENT, to.to_ccecho, 0); 1368a0292f23SGarrett Wollman 1369a0292f23SGarrett Wollman tp->rcv_adv += tp->rcv_wnd; 1370a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1371a0292f23SGarrett Wollman /* 1372a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1373a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1374a0292f23SGarrett Wollman */ 1375d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && tlen != 0) 13769b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 13779b8b58e0SJonathan Lemon tcp_timer_delack, tp); 1378a0292f23SGarrett Wollman else 1379a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1380a0292f23SGarrett Wollman /* 1381a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1382a0292f23SGarrett Wollman * Transitions: 1383a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1384a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1385a0292f23SGarrett Wollman */ 13869b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1387a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1388a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1389a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1390fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 13917ff19458SPaul Traina } else { 1392a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 13939b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, 13949b8b58e0SJonathan Lemon tcp_timer_keep, tp); 13957ff19458SPaul Traina } 1396a0292f23SGarrett Wollman } else { 1397a0292f23SGarrett Wollman /* 1398c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 1399c068736aSJeffrey Hsu * simultaneous open. If segment contains CC option 1400c068736aSJeffrey Hsu * and there is a cached CC, apply TAO test. 1401c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 1402c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 1403a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1404a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1405a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1406a0292f23SGarrett Wollman */ 1407a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 14089b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 1409be2ac88cSJonathan Lemon if (to.to_flags & TOF_CC) { 141097d8d152SAndre Oppermann if (tao.tao_cc != 0 && 141197d8d152SAndre Oppermann CC_GT(to.to_cc, tao.tao_cc)) { 1412a0292f23SGarrett Wollman /* 1413a0292f23SGarrett Wollman * update cache and make transition: 1414a0292f23SGarrett Wollman * SYN-SENT -> ESTABLISHED* 1415a0292f23SGarrett Wollman * SYN-SENT* -> FIN-WAIT-1* 1416a0292f23SGarrett Wollman */ 141797d8d152SAndre Oppermann tao.tao_cc = to.to_cc; 141897d8d152SAndre Oppermann tcp_hc_updatetao(&inp->inp_inc, 141997d8d152SAndre Oppermann TCP_HC_TAO_CC, to.to_cc, 0); 14209b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1421a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1422a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1423a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 14247ff19458SPaul Traina } else { 1425a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 14269b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, 14279b8b58e0SJonathan Lemon tcp_keepidle, 14289b8b58e0SJonathan Lemon tcp_timer_keep, 14299b8b58e0SJonathan Lemon tp); 14307ff19458SPaul Traina } 1431a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDSYN; 1432df8bae1dSRodney W. Grimes } else 1433df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1434a0292f23SGarrett Wollman } else { 143597d8d152SAndre Oppermann if (tcp_do_rfc1644) { 1436a0292f23SGarrett Wollman /* CC.NEW or no option => invalidate cache */ 143797d8d152SAndre Oppermann tao.tao_cc = 0; 143897d8d152SAndre Oppermann tcp_hc_updatetao(&inp->inp_inc, 143997d8d152SAndre Oppermann TCP_HC_TAO_CC, to.to_cc, 0); 144097d8d152SAndre Oppermann } 1441a0292f23SGarrett Wollman tp->t_state = TCPS_SYN_RECEIVED; 1442a0292f23SGarrett Wollman } 1443a0292f23SGarrett Wollman } 1444df8bae1dSRodney W. Grimes 1445df8bae1dSRodney W. Grimes trimthenstep6: 1446df8bae1dSRodney W. Grimes /* 1447fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1448df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1449df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1450df8bae1dSRodney W. Grimes */ 1451fb59c426SYoshinobu Inoue th->th_seq++; 1452fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1453fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1454df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1455fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1456fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1457df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1458df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1459df8bae1dSRodney W. Grimes } 1460fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1461fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 1462a0292f23SGarrett Wollman /* 1463a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1464a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1465a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1466a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1467a0292f23SGarrett Wollman * Otherwise, goto step 6. 1468a0292f23SGarrett Wollman */ 1469fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1470a0292f23SGarrett Wollman goto process_ACK; 1471c068736aSJeffrey Hsu 1472df8bae1dSRodney W. Grimes goto step6; 1473c068736aSJeffrey Hsu 1474a0292f23SGarrett Wollman /* 1475a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1476a0292f23SGarrett Wollman * if segment contains a SYN and CC [not CC.NEW] option: 1477a0292f23SGarrett Wollman * if state == TIME_WAIT and connection duration > MSL, 1478a0292f23SGarrett Wollman * drop packet and send RST; 1479a0292f23SGarrett Wollman * 1480a0292f23SGarrett Wollman * if SEG.CC > CCrecv then is new SYN, and can implicitly 1481a0292f23SGarrett Wollman * ack the FIN (and data) in retransmission queue. 1482a0292f23SGarrett Wollman * Complete close and delete TCPCB. Then reprocess 1483a0292f23SGarrett Wollman * segment, hoping to find new TCPCB in LISTEN state; 1484a0292f23SGarrett Wollman * 1485a0292f23SGarrett Wollman * else must be old SYN; drop it. 1486a0292f23SGarrett Wollman * else do normal processing. 1487a0292f23SGarrett Wollman */ 1488a0292f23SGarrett Wollman case TCPS_LAST_ACK: 1489a0292f23SGarrett Wollman case TCPS_CLOSING: 1490a0292f23SGarrett Wollman case TCPS_TIME_WAIT: 1491340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1492fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) && 1493be2ac88cSJonathan Lemon (to.to_flags & TOF_CC) && tp->cc_recv != 0) { 1494a0292f23SGarrett Wollman if (tp->t_state == TCPS_TIME_WAIT && 1495a57815efSBosko Milekic (ticks - tp->t_starttime) > tcp_msl) { 1496a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1497a0292f23SGarrett Wollman goto dropwithreset; 1498a57815efSBosko Milekic } 1499a0292f23SGarrett Wollman if (CC_GT(to.to_cc, tp->cc_recv)) { 1500a0292f23SGarrett Wollman tp = tcp_close(tp); 1501a0292f23SGarrett Wollman goto findpcb; 1502a0292f23SGarrett Wollman } 1503a0292f23SGarrett Wollman else 1504a0292f23SGarrett Wollman goto drop; 1505a0292f23SGarrett Wollman } 1506a0292f23SGarrett Wollman break; /* continue normal processing */ 1507df8bae1dSRodney W. Grimes } 1508df8bae1dSRodney W. Grimes 1509df8bae1dSRodney W. Grimes /* 1510df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 151180ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 151280ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 151380ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 151480ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 151580ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 151680ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 151780ab7c0eSGarrett Wollman * killing a connection). 151880ab7c0eSGarrett Wollman * Then check timestamp, if present. 1519a0292f23SGarrett Wollman * Then check the connection count, if present. 1520df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 1521df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 1522df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 1523df8bae1dSRodney W. Grimes * 152480ab7c0eSGarrett Wollman * 152580ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 152680ab7c0eSGarrett Wollman * if this is a valid reset segment. 152780ab7c0eSGarrett Wollman * RFC 793 page 37: 152880ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 152980ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 153080ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 153180ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 153280ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 15331a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 15341a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 15351a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 15361a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 153780dd2a81SMike Silbersack * Note 2: Paul Watson's paper "Slipping in the Window" has shown 153880dd2a81SMike Silbersack * that brute force RST attacks are possible. To combat this, 153980dd2a81SMike Silbersack * we use a much stricter check while in the ESTABLISHED state, 154080dd2a81SMike Silbersack * only accepting RSTs where the sequence number is equal to 154180dd2a81SMike Silbersack * last_ack_sent. In all other states (the states in which a 154280dd2a81SMike Silbersack * RST is more likely), the more permissive check is used. 154380ab7c0eSGarrett Wollman * If we have multiple segments in flight, the intial reset 154480ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 154580ab7c0eSGarrett Wollman * but they will eventually catch up. 154680ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 154780ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 154880ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 154980ab7c0eSGarrett Wollman * 155080ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 155180ab7c0eSGarrett Wollman * 155280ab7c0eSGarrett Wollman * DISCUSSION 155380ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 155480ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 155580ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 155680ab7c0eSGarrett Wollman * data. 155780ab7c0eSGarrett Wollman * 155880ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 155980ab7c0eSGarrett Wollman * the state: 156080ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 156180ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 156280ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 1563745bab7fSDima Dorfman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 156480ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 1565e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 156680ab7c0eSGarrett Wollman * Close the tcb. 1567e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 156880ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 156980ab7c0eSGarrett Wollman * RFC 1337. 157080ab7c0eSGarrett Wollman */ 1571fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 1572fb59c426SYoshinobu Inoue if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 1573fb59c426SYoshinobu Inoue SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 157480ab7c0eSGarrett Wollman switch (tp->t_state) { 157580ab7c0eSGarrett Wollman 157680ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 157780ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 157880ab7c0eSGarrett Wollman goto close; 157980ab7c0eSGarrett Wollman 158080ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 158180dd2a81SMike Silbersack if (tp->last_ack_sent != th->th_seq) { 158280dd2a81SMike Silbersack tcpstat.tcps_badrst++; 158380dd2a81SMike Silbersack goto drop; 158480dd2a81SMike Silbersack } 158580ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 158680ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 158780ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 158880ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 158980ab7c0eSGarrett Wollman close: 159080ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 159180ab7c0eSGarrett Wollman tcpstat.tcps_drops++; 159280ab7c0eSGarrett Wollman tp = tcp_close(tp); 159380ab7c0eSGarrett Wollman break; 159480ab7c0eSGarrett Wollman 159580ab7c0eSGarrett Wollman case TCPS_CLOSING: 159680ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 159780ab7c0eSGarrett Wollman tp = tcp_close(tp); 159880ab7c0eSGarrett Wollman break; 159980ab7c0eSGarrett Wollman 160080ab7c0eSGarrett Wollman case TCPS_TIME_WAIT: 1601340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, 1602340c35deSJonathan Lemon ("timewait")); 160380ab7c0eSGarrett Wollman break; 160480ab7c0eSGarrett Wollman } 160580ab7c0eSGarrett Wollman } 160680ab7c0eSGarrett Wollman goto drop; 160780ab7c0eSGarrett Wollman } 160880ab7c0eSGarrett Wollman 160980ab7c0eSGarrett Wollman /* 1610df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 1611df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 1612df8bae1dSRodney W. Grimes */ 1613be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 161480ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1615df8bae1dSRodney W. Grimes 1616df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 16179b8b58e0SJonathan Lemon if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1618df8bae1dSRodney W. Grimes /* 1619df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 1620df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 1621df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 1622df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 1623df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 1624df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 1625df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 1626df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 1627df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 1628df8bae1dSRodney W. Grimes */ 1629df8bae1dSRodney W. Grimes tp->ts_recent = 0; 1630df8bae1dSRodney W. Grimes } else { 1631df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 1632fb59c426SYoshinobu Inoue tcpstat.tcps_rcvdupbyte += tlen; 1633df8bae1dSRodney W. Grimes tcpstat.tcps_pawsdrop++; 163407fd333dSMatthew Dillon if (tlen) 1635df8bae1dSRodney W. Grimes goto dropafterack; 16361ab4789dSMatthew Dillon goto drop; 16371ab4789dSMatthew Dillon } 1638df8bae1dSRodney W. Grimes } 1639df8bae1dSRodney W. Grimes 1640a0292f23SGarrett Wollman /* 1641a0292f23SGarrett Wollman * T/TCP mechanism 1642a0292f23SGarrett Wollman * If T/TCP was negotiated and the segment doesn't have CC, 1643dc733423SDag-Erling Smørgrav * or if its CC is wrong then drop the segment. 1644a0292f23SGarrett Wollman * RST segments do not have to comply with this. 1645a0292f23SGarrett Wollman */ 1646a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) && 1647be2ac88cSJonathan Lemon ((to.to_flags & TOF_CC) == 0 || tp->cc_recv != to.to_cc)) 1648a0292f23SGarrett Wollman goto dropafterack; 1649a0292f23SGarrett Wollman 165080ab7c0eSGarrett Wollman /* 165180ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 165280ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 165380ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 165480ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 165580ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 165680ab7c0eSGarrett Wollman */ 1657a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 1658a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1659a57815efSBosko Milekic goto dropwithreset; 1660a57815efSBosko Milekic } 166180ab7c0eSGarrett Wollman 1662fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 1663df8bae1dSRodney W. Grimes if (todrop > 0) { 1664fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1665fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 1666fb59c426SYoshinobu Inoue th->th_seq++; 1667fb59c426SYoshinobu Inoue if (th->th_urp > 1) 1668fb59c426SYoshinobu Inoue th->th_urp--; 1669df8bae1dSRodney W. Grimes else 1670fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1671df8bae1dSRodney W. Grimes todrop--; 1672df8bae1dSRodney W. Grimes } 1673df8bae1dSRodney W. Grimes /* 1674dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 1675df8bae1dSRodney W. Grimes */ 1676fb59c426SYoshinobu Inoue if (todrop > tlen 1677fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1678dac20301SGarrett Wollman /* 1679dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 1680dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 1681dac20301SGarrett Wollman * of sequence; drop it. 1682dac20301SGarrett Wollman */ 1683fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1684dac20301SGarrett Wollman 1685df8bae1dSRodney W. Grimes /* 1686dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 1687dac20301SGarrett Wollman * But keep on processing for RST or ACK. 1688df8bae1dSRodney W. Grimes */ 1689dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1690fb59c426SYoshinobu Inoue todrop = tlen; 1691dac20301SGarrett Wollman tcpstat.tcps_rcvduppack++; 1692dac20301SGarrett Wollman tcpstat.tcps_rcvdupbyte += todrop; 1693df8bae1dSRodney W. Grimes } else { 1694df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartduppack++; 1695df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartdupbyte += todrop; 1696df8bae1dSRodney W. Grimes } 1697fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 1698fb59c426SYoshinobu Inoue th->th_seq += todrop; 1699fb59c426SYoshinobu Inoue tlen -= todrop; 1700fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 1701fb59c426SYoshinobu Inoue th->th_urp -= todrop; 1702df8bae1dSRodney W. Grimes else { 1703fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1704fb59c426SYoshinobu Inoue th->th_urp = 0; 1705df8bae1dSRodney W. Grimes } 1706df8bae1dSRodney W. Grimes } 1707df8bae1dSRodney W. Grimes 1708df8bae1dSRodney W. Grimes /* 1709df8bae1dSRodney W. Grimes * If new data are received on a connection after the 1710df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 1711df8bae1dSRodney W. Grimes */ 1712df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 1713fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1714df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1715df8bae1dSRodney W. Grimes tcpstat.tcps_rcvafterclose++; 1716a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1717df8bae1dSRodney W. Grimes goto dropwithreset; 1718df8bae1dSRodney W. Grimes } 1719df8bae1dSRodney W. Grimes 1720df8bae1dSRodney W. Grimes /* 1721df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 1722df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 1723df8bae1dSRodney W. Grimes */ 1724fb59c426SYoshinobu Inoue todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd); 1725df8bae1dSRodney W. Grimes if (todrop > 0) { 1726df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1727fb59c426SYoshinobu Inoue if (todrop >= tlen) { 1728fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyteafterwin += tlen; 1729df8bae1dSRodney W. Grimes /* 1730df8bae1dSRodney W. Grimes * If a new connection request is received 1731df8bae1dSRodney W. Grimes * while in TIME_WAIT, drop the old connection 1732df8bae1dSRodney W. Grimes * and start over if the sequence numbers 1733df8bae1dSRodney W. Grimes * are above the previous ones. 1734df8bae1dSRodney W. Grimes */ 1735340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1736fb59c426SYoshinobu Inoue if (thflags & TH_SYN && 1737df8bae1dSRodney W. Grimes tp->t_state == TCPS_TIME_WAIT && 1738fb59c426SYoshinobu Inoue SEQ_GT(th->th_seq, tp->rcv_nxt)) { 1739df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1740df8bae1dSRodney W. Grimes goto findpcb; 1741df8bae1dSRodney W. Grimes } 1742df8bae1dSRodney W. Grimes /* 1743df8bae1dSRodney W. Grimes * If window is closed can only take segments at 1744df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 1745df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 1746df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 1747df8bae1dSRodney W. Grimes * and ack. 1748df8bae1dSRodney W. Grimes */ 1749fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1750df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1751df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinprobe++; 1752df8bae1dSRodney W. Grimes } else 1753df8bae1dSRodney W. Grimes goto dropafterack; 1754df8bae1dSRodney W. Grimes } else 1755df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1756df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1757fb59c426SYoshinobu Inoue tlen -= todrop; 1758fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 1759df8bae1dSRodney W. Grimes } 1760df8bae1dSRodney W. Grimes 1761df8bae1dSRodney W. Grimes /* 1762df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1763df8bae1dSRodney W. Grimes * record its timestamp. 1764a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1765a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1766df8bae1dSRodney W. Grimes */ 1767be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1768fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 17699b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1770a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1771df8bae1dSRodney W. Grimes } 1772df8bae1dSRodney W. Grimes 1773df8bae1dSRodney W. Grimes /* 1774df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 1775df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 1776df8bae1dSRodney W. Grimes */ 1777fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1778df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 1779a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1780122aad88SAndre Oppermann goto drop; 1781df8bae1dSRodney W. Grimes } 1782df8bae1dSRodney W. Grimes 1783a0292f23SGarrett Wollman /* 1784a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1785a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 1786a0292f23SGarrett Wollman * later processing; else drop segment and return. 1787a0292f23SGarrett Wollman */ 1788fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 1789a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 1790a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 1791a0292f23SGarrett Wollman goto step6; 1792a0292f23SGarrett Wollman else 1793a0292f23SGarrett Wollman goto drop; 1794a0292f23SGarrett Wollman } 1795df8bae1dSRodney W. Grimes 1796df8bae1dSRodney W. Grimes /* 1797df8bae1dSRodney W. Grimes * Ack processing. 1798df8bae1dSRodney W. Grimes */ 1799df8bae1dSRodney W. Grimes switch (tp->t_state) { 1800df8bae1dSRodney W. Grimes 1801df8bae1dSRodney W. Grimes /* 1802764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1803764d8cefSBill Fenner * ESTABLISHED state and continue processing. 1804764d8cefSBill Fenner * The ACK was checked above. 1805df8bae1dSRodney W. Grimes */ 1806df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1807a0292f23SGarrett Wollman 1808df8bae1dSRodney W. Grimes tcpstat.tcps_connects++; 1809df8bae1dSRodney W. Grimes soisconnected(so); 1810df8bae1dSRodney W. Grimes /* Do window scaling? */ 1811df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1812df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1813df8bae1dSRodney W. Grimes tp->snd_scale = tp->requested_s_scale; 1814df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 1815df8bae1dSRodney W. Grimes } 1816a0292f23SGarrett Wollman /* 1817a0292f23SGarrett Wollman * Upon successful completion of 3-way handshake, 181897d8d152SAndre Oppermann * update cache.CC, pass any queued data to the user, 181997d8d152SAndre Oppermann * and advance state appropriately. 1820a0292f23SGarrett Wollman */ 182197d8d152SAndre Oppermann if (tcp_do_rfc1644) { 182297d8d152SAndre Oppermann tao.tao_cc = tp->cc_recv; 182397d8d152SAndre Oppermann tcp_hc_updatetao(&inp->inp_inc, TCP_HC_TAO_CC, 182497d8d152SAndre Oppermann tp->cc_recv, 0); 182597d8d152SAndre Oppermann } 1826a0292f23SGarrett Wollman /* 1827a0292f23SGarrett Wollman * Make transitions: 1828a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 1829a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 1830a0292f23SGarrett Wollman */ 18319b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1832a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1833a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1834a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 18357ff19458SPaul Traina } else { 1836a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 18379b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, 18389b8b58e0SJonathan Lemon tcp_timer_keep, tp); 18397ff19458SPaul Traina } 1840a0292f23SGarrett Wollman /* 1841a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 1842a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 1843a0292f23SGarrett Wollman */ 1844fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 1845fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1846a0292f23SGarrett Wollman (struct mbuf *)0); 1847fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 184893b0017fSPhilippe Charnier /* FALLTHROUGH */ 1849df8bae1dSRodney W. Grimes 1850df8bae1dSRodney W. Grimes /* 1851df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1852df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 1853fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 1854fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 1855df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 1856df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 1857df8bae1dSRodney W. Grimes */ 1858df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1859df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1860df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 1861df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1862df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1863df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 1864df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 1865340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1866fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1867fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 1868df8bae1dSRodney W. Grimes tcpstat.tcps_rcvdupack++; 1869df8bae1dSRodney W. Grimes /* 1870df8bae1dSRodney W. Grimes * If we have outstanding data (other than 1871df8bae1dSRodney W. Grimes * a window probe), this is a completely 1872df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 1873df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 1874df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 1875df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 1876df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 1877df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 1878df8bae1dSRodney W. Grimes * window so we send only this one 1879df8bae1dSRodney W. Grimes * packet. 1880df8bae1dSRodney W. Grimes * 1881df8bae1dSRodney W. Grimes * We know we're losing at the current 1882df8bae1dSRodney W. Grimes * window size so do congestion avoidance 1883df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 1884df8bae1dSRodney W. Grimes * and pull our congestion window back to 1885df8bae1dSRodney W. Grimes * the new ssthresh). 1886df8bae1dSRodney W. Grimes * 1887df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 1888df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 1889df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 1890df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 1891df8bae1dSRodney W. Grimes * network. 1892df8bae1dSRodney W. Grimes */ 18939b8b58e0SJonathan Lemon if (!callout_active(tp->tt_rexmt) || 1894fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 1895df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1896cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 1897cb942153SJeffrey Hsu (tcp_do_newreno && 18989d11646dSJeffrey Hsu IN_FASTRECOVERY(tp))) { 189946f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 190046f58482SJonathan Lemon (void) tcp_output(tp); 190146f58482SJonathan Lemon goto drop; 1902cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 1903cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 1904cb942153SJeffrey Hsu u_int win; 1905cb942153SJeffrey Hsu if (tcp_do_newreno && 19069d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, 19079d11646dSJeffrey Hsu tp->snd_recover)) { 1908cb942153SJeffrey Hsu tp->t_dupacks = 0; 1909cb942153SJeffrey Hsu break; 191046f58482SJonathan Lemon } 1911cb942153SJeffrey Hsu win = min(tp->snd_wnd, tp->snd_cwnd) / 1912cb942153SJeffrey Hsu 2 / tp->t_maxseg; 1913df8bae1dSRodney W. Grimes if (win < 2) 1914df8bae1dSRodney W. Grimes win = 2; 1915df8bae1dSRodney W. Grimes tp->snd_ssthresh = win * tp->t_maxseg; 19169d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 191746f58482SJonathan Lemon tp->snd_recover = tp->snd_max; 19189b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 19199b8b58e0SJonathan Lemon tp->t_rtttime = 0; 1920fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 1921df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 1922df8bae1dSRodney W. Grimes (void) tcp_output(tp); 192348d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 192448d2549cSJeffrey Hsu ("tp->snd_limited too big")); 1925df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 192648d2549cSJeffrey Hsu tp->t_maxseg * 192748d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 1928df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 1929df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 1930df8bae1dSRodney W. Grimes goto drop; 1931582a954bSJeffrey Hsu } else if (tcp_do_rfc3042) { 1932582a954bSJeffrey Hsu u_long oldcwnd = tp->snd_cwnd; 193348d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 193448d2549cSJeffrey Hsu u_int sent; 193589c02376SJeffrey Hsu 1936582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 1937582a954bSJeffrey Hsu tp->t_dupacks == 2, 1938582a954bSJeffrey Hsu ("dupacks not 1 or 2")); 193961a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 194048d2549cSJeffrey Hsu tp->snd_limited = 0; 194161a36e3dSJeffrey Hsu tp->snd_cwnd = 194261a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 194361a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 194461a36e3dSJeffrey Hsu tp->t_maxseg; 1945582a954bSJeffrey Hsu (void) tcp_output(tp); 194648d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 194748d2549cSJeffrey Hsu if (sent > tp->t_maxseg) { 194889c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 194989c02376SJeffrey Hsu tp->snd_limited == 0) || 195089c02376SJeffrey Hsu (sent == tp->t_maxseg + 1 && 195189c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 195248d2549cSJeffrey Hsu ("sent too much")); 195348d2549cSJeffrey Hsu tp->snd_limited = 2; 195448d2549cSJeffrey Hsu } else if (sent > 0) 195548d2549cSJeffrey Hsu ++tp->snd_limited; 1956582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 1957582a954bSJeffrey Hsu goto drop; 1958df8bae1dSRodney W. Grimes } 1959df8bae1dSRodney W. Grimes } else 1960df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1961df8bae1dSRodney W. Grimes break; 1962df8bae1dSRodney W. Grimes } 1963cb942153SJeffrey Hsu 1964cb942153SJeffrey Hsu KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una")); 1965cb942153SJeffrey Hsu 1966df8bae1dSRodney W. Grimes /* 1967df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 1968df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 1969df8bae1dSRodney W. Grimes */ 1970c068736aSJeffrey Hsu if (tcp_do_newreno) { 19719d11646dSJeffrey Hsu if (IN_FASTRECOVERY(tp)) { 1972cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 1973c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 1974c068736aSJeffrey Hsu } else { 1975c068736aSJeffrey Hsu /* 1976c068736aSJeffrey Hsu * Window inflation should have left us 1977c068736aSJeffrey Hsu * with approximately snd_ssthresh 1978c068736aSJeffrey Hsu * outstanding data. 1979c068736aSJeffrey Hsu * But in case we would be inclined to 1980c068736aSJeffrey Hsu * send a burst, better to do it via 1981c068736aSJeffrey Hsu * the slow start mechanism. 1982c068736aSJeffrey Hsu */ 1983c068736aSJeffrey Hsu if (SEQ_GT(th->th_ack + 1984c068736aSJeffrey Hsu tp->snd_ssthresh, 1985c068736aSJeffrey Hsu tp->snd_max)) 1986c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_max - 1987c068736aSJeffrey Hsu th->th_ack + 1988c068736aSJeffrey Hsu tp->t_maxseg; 1989c068736aSJeffrey Hsu else 1990c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_ssthresh; 1991c068736aSJeffrey Hsu } 1992c068736aSJeffrey Hsu } 1993c068736aSJeffrey Hsu } else { 1994233e8c18SGarrett Wollman if (tp->t_dupacks >= tcprexmtthresh && 1995df8bae1dSRodney W. Grimes tp->snd_cwnd > tp->snd_ssthresh) 1996df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh; 199746f58482SJonathan Lemon } 1998cb942153SJeffrey Hsu tp->t_dupacks = 0; 1999fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_max)) { 2000df8bae1dSRodney W. Grimes tcpstat.tcps_rcvacktoomuch++; 2001df8bae1dSRodney W. Grimes goto dropafterack; 2002df8bae1dSRodney W. Grimes } 2003a0292f23SGarrett Wollman /* 2004a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2005a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2006a0292f23SGarrett Wollman */ 2007a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2008a0292f23SGarrett Wollman /* 2009a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2010a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 201107e43e10SAndras Olah * synchronized). Go to non-starred state, 201207e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 201307e43e10SAndras Olah * we can do window scaling. 2014a0292f23SGarrett Wollman */ 2015a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2016a0292f23SGarrett Wollman tp->snd_una++; 201707e43e10SAndras Olah /* Do window scaling? */ 201807e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 201907e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 202007e43e10SAndras Olah tp->snd_scale = tp->requested_s_scale; 202107e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 202207e43e10SAndras Olah } 2023a0292f23SGarrett Wollman } 2024a0292f23SGarrett Wollman 2025a0292f23SGarrett Wollman process_ACK: 2026fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 2027df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 2028df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 2029df8bae1dSRodney W. Grimes 2030df8bae1dSRodney W. Grimes /* 20319b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 20329b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 20339b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 20349b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 20359b8b58e0SJonathan Lemon * we left off. 20369b8b58e0SJonathan Lemon */ 20379b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 2038d65bf08aSMatthew Dillon ++tcpstat.tcps_sndrexmitbad; 20399b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 20409b8b58e0SJonathan Lemon tp->snd_ssthresh = tp->snd_ssthresh_prev; 20419d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 20429d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 20439d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 20449b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 20459b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; /* XXX probably not required */ 20469b8b58e0SJonathan Lemon } 20479b8b58e0SJonathan Lemon 20489b8b58e0SJonathan Lemon /* 2049df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2050df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2051df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2052df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2053df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2054df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2055df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2056fa55172bSMatthew Dillon * 2057fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2058fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2059fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2060fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2061df8bae1dSRodney W. Grimes */ 2062fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 2063fa55172bSMatthew Dillon to.to_tsecr) { 20649b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 2065fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 20669b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2067fa55172bSMatthew Dillon } 20681fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 2069df8bae1dSRodney W. Grimes 2070df8bae1dSRodney W. Grimes /* 2071df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2072df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2073df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2074df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2075df8bae1dSRodney W. Grimes */ 2076fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 20779b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 2078df8bae1dSRodney W. Grimes needoutput = 1; 20799b8b58e0SJonathan Lemon } else if (!callout_active(tp->tt_persist)) 20809b8b58e0SJonathan Lemon callout_reset(tp->tt_rexmt, tp->t_rxtcur, 20819b8b58e0SJonathan Lemon tcp_timer_rexmt, tp); 2082a0292f23SGarrett Wollman 2083a0292f23SGarrett Wollman /* 2084a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2085a0292f23SGarrett Wollman * skip rest of ACK processing. 2086a0292f23SGarrett Wollman */ 2087a0292f23SGarrett Wollman if (acked == 0) 2088a0292f23SGarrett Wollman goto step6; 2089a0292f23SGarrett Wollman 2090df8bae1dSRodney W. Grimes /* 2091df8bae1dSRodney W. Grimes * When new data is acked, open the congestion window. 2092df8bae1dSRodney W. Grimes * If the window gives us less than ssthresh packets 2093df8bae1dSRodney W. Grimes * in flight, open exponentially (maxseg per packet). 2094df8bae1dSRodney W. Grimes * Otherwise open linearly: maxseg per window 209510be5648SGarrett Wollman * (maxseg^2 / cwnd per packet). 2096df8bae1dSRodney W. Grimes */ 20979d11646dSJeffrey Hsu if (!tcp_do_newreno || !IN_FASTRECOVERY(tp)) { 2098df8bae1dSRodney W. Grimes register u_int cw = tp->snd_cwnd; 2099df8bae1dSRodney W. Grimes register u_int incr = tp->t_maxseg; 2100df8bae1dSRodney W. Grimes if (cw > tp->snd_ssthresh) 210110be5648SGarrett Wollman incr = incr * incr / cw; 2102df8bae1dSRodney W. Grimes tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale); 2103df8bae1dSRodney W. Grimes } 2104df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 2105df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 2106df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 2107df8bae1dSRodney W. Grimes ourfinisacked = 1; 2108df8bae1dSRodney W. Grimes } else { 2109df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 2110df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 2111df8bae1dSRodney W. Grimes ourfinisacked = 0; 2112df8bae1dSRodney W. Grimes } 2113df8bae1dSRodney W. Grimes sowwakeup(so); 2114cb942153SJeffrey Hsu /* detect una wraparound */ 21159d11646dSJeffrey Hsu if (tcp_do_newreno && !IN_FASTRECOVERY(tp) && 21169d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 21179d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 21189d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 21199d11646dSJeffrey Hsu if (tcp_do_newreno && IN_FASTRECOVERY(tp) && 21209d11646dSJeffrey Hsu SEQ_GEQ(th->th_ack, tp->snd_recover)) 21219d11646dSJeffrey Hsu EXIT_FASTRECOVERY(tp); 2122fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 2123df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2124df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2125df8bae1dSRodney W. Grimes 2126df8bae1dSRodney W. Grimes switch (tp->t_state) { 2127df8bae1dSRodney W. Grimes 2128df8bae1dSRodney W. Grimes /* 2129df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2130df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2131df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2132df8bae1dSRodney W. Grimes */ 2133df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2134df8bae1dSRodney W. Grimes if (ourfinisacked) { 2135df8bae1dSRodney W. Grimes /* 2136df8bae1dSRodney W. Grimes * If we can't receive any more 2137df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2138df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2139df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2140df8bae1dSRodney W. Grimes * we'll hang forever. 2141df8bae1dSRodney W. Grimes */ 2142340c35deSJonathan Lemon /* XXXjl 2143340c35deSJonathan Lemon * we should release the tp also, and use a 2144340c35deSJonathan Lemon * compressed state. 2145340c35deSJonathan Lemon */ 2146c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 214703e49181SSeigo Tanimura soisdisconnected(so); 21489b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, tcp_maxidle, 21499b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 21504cc20ab1SSeigo Tanimura } 2151df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 2152df8bae1dSRodney W. Grimes } 2153df8bae1dSRodney W. Grimes break; 2154df8bae1dSRodney W. Grimes 2155df8bae1dSRodney W. Grimes /* 2156df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2157df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2158df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2159df8bae1dSRodney W. Grimes * the segment. 2160df8bae1dSRodney W. Grimes */ 2161df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2162df8bae1dSRodney W. Grimes if (ourfinisacked) { 2163340c35deSJonathan Lemon KASSERT(headlocked, ("headlocked")); 216411a20fb8SJeffrey Hsu tcp_twstart(tp); 2165340c35deSJonathan Lemon INP_INFO_WUNLOCK(&tcbinfo); 2166340c35deSJonathan Lemon m_freem(m); 2167340c35deSJonathan Lemon return; 2168df8bae1dSRodney W. Grimes } 2169df8bae1dSRodney W. Grimes break; 2170df8bae1dSRodney W. Grimes 2171df8bae1dSRodney W. Grimes /* 2172df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2173df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2174df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2175df8bae1dSRodney W. Grimes * enter the closed state and return. 2176df8bae1dSRodney W. Grimes */ 2177df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2178df8bae1dSRodney W. Grimes if (ourfinisacked) { 2179df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2180df8bae1dSRodney W. Grimes goto drop; 2181df8bae1dSRodney W. Grimes } 2182df8bae1dSRodney W. Grimes break; 2183df8bae1dSRodney W. Grimes 2184df8bae1dSRodney W. Grimes /* 2185df8bae1dSRodney W. Grimes * In TIME_WAIT state the only thing that should arrive 2186df8bae1dSRodney W. Grimes * is a retransmission of the remote FIN. Acknowledge 2187df8bae1dSRodney W. Grimes * it and restart the finack timer. 2188df8bae1dSRodney W. Grimes */ 2189df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 2190340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 21919b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 21929b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2193df8bae1dSRodney W. Grimes goto dropafterack; 2194df8bae1dSRodney W. Grimes } 2195df8bae1dSRodney W. Grimes } 2196df8bae1dSRodney W. Grimes 2197df8bae1dSRodney W. Grimes step6: 2198df8bae1dSRodney W. Grimes /* 2199df8bae1dSRodney W. Grimes * Update window information. 2200df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 2201df8bae1dSRodney W. Grimes */ 2202fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 2203fb59c426SYoshinobu Inoue (SEQ_LT(tp->snd_wl1, th->th_seq) || 2204fb59c426SYoshinobu Inoue (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2205fb59c426SYoshinobu Inoue (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2206df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 2207fb59c426SYoshinobu Inoue if (tlen == 0 && 2208fb59c426SYoshinobu Inoue tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2209df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinupd++; 2210df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 2211fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq; 2212fb59c426SYoshinobu Inoue tp->snd_wl2 = th->th_ack; 2213df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2214df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 2215df8bae1dSRodney W. Grimes needoutput = 1; 2216df8bae1dSRodney W. Grimes } 2217df8bae1dSRodney W. Grimes 2218df8bae1dSRodney W. Grimes /* 2219df8bae1dSRodney W. Grimes * Process segments with URG. 2220df8bae1dSRodney W. Grimes */ 2221fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2222df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2223df8bae1dSRodney W. Grimes /* 2224df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2225df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2226df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2227df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2228df8bae1dSRodney W. Grimes */ 2229fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2230fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2231fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 2232df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2233df8bae1dSRodney W. Grimes } 2234df8bae1dSRodney W. Grimes /* 2235df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2236df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2237df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2238df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2239df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2240df8bae1dSRodney W. Grimes * 2241df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2242df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2243df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2244df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2245df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2246df8bae1dSRodney W. Grimes * spec states (in one of two places). 2247df8bae1dSRodney W. Grimes */ 2248fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2249fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2250df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2251df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 22524cc20ab1SSeigo Tanimura if (so->so_oobmark == 0) 2253c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 2254df8bae1dSRodney W. Grimes sohasoutofband(so); 2255df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2256df8bae1dSRodney W. Grimes } 2257df8bae1dSRodney W. Grimes /* 2258df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2259df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2260df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2261df8bae1dSRodney W. Grimes * data may creep in... ick. 2262df8bae1dSRodney W. Grimes */ 226330613f56SJeffrey Hsu if (th->th_urp <= (u_long)tlen && 226430613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 226530613f56SJeffrey Hsu /* hdr drop is delayed */ 226630613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 226730613f56SJeffrey Hsu } 2268c068736aSJeffrey Hsu } else { 2269df8bae1dSRodney W. Grimes /* 2270df8bae1dSRodney W. Grimes * If no out of band data is expected, 2271df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2272df8bae1dSRodney W. Grimes * with the receive window. 2273df8bae1dSRodney W. Grimes */ 2274df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2275df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2276c068736aSJeffrey Hsu } 2277df8bae1dSRodney W. Grimes dodata: /* XXX */ 2278f76fcf6dSJeffrey Hsu KASSERT(headlocked, ("headlocked")); 2279df8bae1dSRodney W. Grimes /* 2280df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2281df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2282df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2283df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2284df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2285df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2286df8bae1dSRodney W. Grimes */ 2287fb59c426SYoshinobu Inoue if ((tlen || (thflags & TH_FIN)) && 2288df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2289fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2290e4b64281SJesper Skriver /* 2291c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2292c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2293c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2294c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2295c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2296c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2297c068736aSJeffrey Hsu * conversions. 2298c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 2299c068736aSJeffrey Hsu * immediately when segments are out of order (so 2300c068736aSJeffrey Hsu * fast retransmit can work). 2301e4b64281SJesper Skriver */ 2302e4b64281SJesper Skriver if (th->th_seq == tp->rcv_nxt && 2303e4b64281SJesper Skriver LIST_EMPTY(&tp->t_segq) && 2304e4b64281SJesper Skriver TCPS_HAVEESTABLISHED(tp->t_state)) { 2305e4b64281SJesper Skriver if (DELAY_ACK(tp)) 23063bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2307e4b64281SJesper Skriver else 2308e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2309e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2310e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 2311e4b64281SJesper Skriver tcpstat.tcps_rcvpack++; 2312e4b64281SJesper Skriver tcpstat.tcps_rcvbyte += tlen; 2313e4b64281SJesper Skriver ND6_HINT(tp); 2314c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2315c1c36a2cSMike Silbersack m_freem(m); 2316c1c36a2cSMike Silbersack else 2317395bb186SSam Leffler sbappendstream(&so->so_rcv, m); 2318e4b64281SJesper Skriver sorwakeup(so); 2319e4b64281SJesper Skriver } else { 2320e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 2321e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2322e4b64281SJesper Skriver } 2323e4b64281SJesper Skriver 2324df8bae1dSRodney W. Grimes /* 2325df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2326df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2327df8bae1dSRodney W. Grimes * buffer size. 2328df8bae1dSRodney W. Grimes */ 2329df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2330df8bae1dSRodney W. Grimes } else { 2331df8bae1dSRodney W. Grimes m_freem(m); 2332fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2333df8bae1dSRodney W. Grimes } 2334df8bae1dSRodney W. Grimes 2335df8bae1dSRodney W. Grimes /* 2336df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2337df8bae1dSRodney W. Grimes * that the connection is closing. 2338df8bae1dSRodney W. Grimes */ 2339fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2340df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2341df8bae1dSRodney W. Grimes socantrcvmore(so); 2342a0292f23SGarrett Wollman /* 2343a0292f23SGarrett Wollman * If connection is half-synchronized 2344d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2345a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2346a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2347a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2348a0292f23SGarrett Wollman */ 23493bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 23503bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2351a0292f23SGarrett Wollman else 2352df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2353df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2354df8bae1dSRodney W. Grimes } 2355df8bae1dSRodney W. Grimes switch (tp->t_state) { 2356df8bae1dSRodney W. Grimes 2357df8bae1dSRodney W. Grimes /* 2358df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2359df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2360df8bae1dSRodney W. Grimes */ 2361df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 23629b8b58e0SJonathan Lemon tp->t_starttime = ticks; 23639b8b58e0SJonathan Lemon /*FALLTHROUGH*/ 2364df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2365df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2366df8bae1dSRodney W. Grimes break; 2367df8bae1dSRodney W. Grimes 2368df8bae1dSRodney W. Grimes /* 2369df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2370df8bae1dSRodney W. Grimes * enter the CLOSING state. 2371df8bae1dSRodney W. Grimes */ 2372df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2373df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 2374df8bae1dSRodney W. Grimes break; 2375df8bae1dSRodney W. Grimes 2376df8bae1dSRodney W. Grimes /* 2377df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 2378df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 2379df8bae1dSRodney W. Grimes * standard timers. 2380df8bae1dSRodney W. Grimes */ 2381df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 238211a20fb8SJeffrey Hsu KASSERT(headlocked == 1, ("headlocked should be 1")); 2383340c35deSJonathan Lemon tcp_twstart(tp); 238411a20fb8SJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2385340c35deSJonathan Lemon return; 2386df8bae1dSRodney W. Grimes 2387df8bae1dSRodney W. Grimes /* 2388df8bae1dSRodney W. Grimes * In TIME_WAIT state restart the 2 MSL time_wait timer. 2389df8bae1dSRodney W. Grimes */ 2390df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 2391340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 23929b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 23939b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2394df8bae1dSRodney W. Grimes break; 2395df8bae1dSRodney W. Grimes } 2396df8bae1dSRodney W. Grimes } 23977792ea27SJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2398610ee2f9SDavid Greenman #ifdef TCPDEBUG 23994cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2400fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2401fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2402610ee2f9SDavid Greenman #endif 2403df8bae1dSRodney W. Grimes 2404df8bae1dSRodney W. Grimes /* 2405df8bae1dSRodney W. Grimes * Return any desired output. 2406df8bae1dSRodney W. Grimes */ 2407df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 2408df8bae1dSRodney W. Grimes (void) tcp_output(tp); 24097792ea27SJeffrey Hsu 2410a14c749fSJonathan Lemon check_delack: 2411a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 24123bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 24133bfd6421SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 24143bfd6421SJonathan Lemon tcp_timer_delack, tp); 24153bfd6421SJonathan Lemon } 2416f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 2417df8bae1dSRodney W. Grimes return; 2418df8bae1dSRodney W. Grimes 2419df8bae1dSRodney W. Grimes dropafterack: 2420df8bae1dSRodney W. Grimes /* 2421df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 2422df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 242380ab7c0eSGarrett Wollman * 242480ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 242580ab7c0eSGarrett Wollman * paths to this code happen after packets containing 242680ab7c0eSGarrett Wollman * RST have been dropped. 242780ab7c0eSGarrett Wollman * 242880ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 242980ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 243080ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 243180ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 243280ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 243380ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 2434df8bae1dSRodney W. Grimes */ 2435fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2436fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 2437a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 2438a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2439a57815efSBosko Milekic goto dropwithreset; 2440a57815efSBosko Milekic } 2441a0292f23SGarrett Wollman #ifdef TCPDEBUG 24424cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2443fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2444fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2445a0292f23SGarrett Wollman #endif 244685e8b243SJeffrey Hsu KASSERT(headlocked, ("headlocked should be 1")); 2447f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2448df8bae1dSRodney W. Grimes m_freem(m); 2449df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2450df8bae1dSRodney W. Grimes (void) tcp_output(tp); 2451f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 2452df8bae1dSRodney W. Grimes return; 2453df8bae1dSRodney W. Grimes 2454df8bae1dSRodney W. Grimes dropwithreset: 2455df8bae1dSRodney W. Grimes /* 2456df8bae1dSRodney W. Grimes * Generate a RST, dropping incoming segment. 2457df8bae1dSRodney W. Grimes * Make ACK acceptable to originator of segment. 2458df8bae1dSRodney W. Grimes * Don't bother to respond if destination was broadcast/multicast. 2459df8bae1dSRodney W. Grimes */ 2460fb59c426SYoshinobu Inoue if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2461df8bae1dSRodney W. Grimes goto drop; 2462fb59c426SYoshinobu Inoue if (isipv6) { 2463173c0f9fSWarner Losh if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2464173c0f9fSWarner Losh IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2465fb59c426SYoshinobu Inoue goto drop; 2466c068736aSJeffrey Hsu } else { 2467173c0f9fSWarner Losh if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2468173c0f9fSWarner Losh IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 24692ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 24702ca2159fSCrist J. Clark in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2471fb59c426SYoshinobu Inoue goto drop; 2472c068736aSJeffrey Hsu } 2473fb59c426SYoshinobu Inoue /* IPv6 anycast check is done at tcp6_input() */ 2474a57815efSBosko Milekic 2475a57815efSBosko Milekic /* 2476c59319bfSDag-Erling Smørgrav * Perform bandwidth limiting. 2477a57815efSBosko Milekic */ 2478a57815efSBosko Milekic if (badport_bandlim(rstreason) < 0) 2479a57815efSBosko Milekic goto drop; 2480a57815efSBosko Milekic 2481a0292f23SGarrett Wollman #ifdef TCPDEBUG 24824cc20ab1SSeigo Tanimura if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2483fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2484fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2485a0292f23SGarrett Wollman #endif 24866fd22cafSJeffrey Hsu 2487fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 2488fb59c426SYoshinobu Inoue /* mtod() below is safe as long as hdr dropping is delayed */ 2489fb59c426SYoshinobu Inoue tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack, 2490fb59c426SYoshinobu Inoue TH_RST); 2491df8bae1dSRodney W. Grimes else { 2492fb59c426SYoshinobu Inoue if (thflags & TH_SYN) 2493fb59c426SYoshinobu Inoue tlen++; 2494fb59c426SYoshinobu Inoue /* mtod() below is safe as long as hdr dropping is delayed */ 2495fb59c426SYoshinobu Inoue tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2496fb59c426SYoshinobu Inoue (tcp_seq)0, TH_RST|TH_ACK); 2497df8bae1dSRodney W. Grimes } 2498c29afad6SSam Leffler 2499c29afad6SSam Leffler if (tp) 2500c29afad6SSam Leffler INP_UNLOCK(inp); 2501f76fcf6dSJeffrey Hsu if (headlocked) 2502f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2503df8bae1dSRodney W. Grimes return; 2504df8bae1dSRodney W. Grimes 2505df8bae1dSRodney W. Grimes drop: 2506df8bae1dSRodney W. Grimes /* 2507df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 2508df8bae1dSRodney W. Grimes */ 2509610ee2f9SDavid Greenman #ifdef TCPDEBUG 25104cc20ab1SSeigo Tanimura if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2511fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2512fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2513a0292f23SGarrett Wollman #endif 2514f76fcf6dSJeffrey Hsu if (tp) 2515f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 2516df8bae1dSRodney W. Grimes m_freem(m); 2517f76fcf6dSJeffrey Hsu if (headlocked) 2518f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2519df8bae1dSRodney W. Grimes return; 2520df8bae1dSRodney W. Grimes } 2521df8bae1dSRodney W. Grimes 2522be2ac88cSJonathan Lemon /* 2523be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 2524be2ac88cSJonathan Lemon */ 25250312fbe9SPoul-Henning Kamp static void 2526be2ac88cSJonathan Lemon tcp_dooptions(to, cp, cnt, is_syn) 2527be2ac88cSJonathan Lemon struct tcpopt *to; 2528df8bae1dSRodney W. Grimes u_char *cp; 2529df8bae1dSRodney W. Grimes int cnt; 2530152385d1SDavid E. O'Brien int is_syn; 2531df8bae1dSRodney W. Grimes { 2532df8bae1dSRodney W. Grimes int opt, optlen; 2533df8bae1dSRodney W. Grimes 2534be2ac88cSJonathan Lemon to->to_flags = 0; 2535df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 2536df8bae1dSRodney W. Grimes opt = cp[0]; 2537df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 2538df8bae1dSRodney W. Grimes break; 2539df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 2540df8bae1dSRodney W. Grimes optlen = 1; 2541df8bae1dSRodney W. Grimes else { 2542b474779fSJun-ichiro itojun Hagino if (cnt < 2) 2543b474779fSJun-ichiro itojun Hagino break; 2544df8bae1dSRodney W. Grimes optlen = cp[1]; 2545b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 2546df8bae1dSRodney W. Grimes break; 2547df8bae1dSRodney W. Grimes } 2548df8bae1dSRodney W. Grimes switch (opt) { 2549df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 2550df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 2551df8bae1dSRodney W. Grimes continue; 2552be2ac88cSJonathan Lemon if (!is_syn) 2553df8bae1dSRodney W. Grimes continue; 2554be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 2555be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 2556be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 2557fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 2558df8bae1dSRodney W. Grimes break; 2559df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 2560df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 2561df8bae1dSRodney W. Grimes continue; 2562be2ac88cSJonathan Lemon if (! is_syn) 2563df8bae1dSRodney W. Grimes continue; 2564be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 2565be2ac88cSJonathan Lemon to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 2566df8bae1dSRodney W. Grimes break; 2567df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 2568df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 2569df8bae1dSRodney W. Grimes continue; 2570be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 2571a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2572a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 2573fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 2574a0292f23SGarrett Wollman bcopy((char *)cp + 6, 2575a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2576fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 2577df8bae1dSRodney W. Grimes break; 2578a0292f23SGarrett Wollman case TCPOPT_CC: 2579a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2580a0292f23SGarrett Wollman continue; 2581be2ac88cSJonathan Lemon to->to_flags |= TOF_CC; 2582a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2583a0292f23SGarrett Wollman (char *)&to->to_cc, sizeof(to->to_cc)); 2584fd8e4ebcSMike Barcroft to->to_cc = ntohl(to->to_cc); 2585a0292f23SGarrett Wollman break; 2586a0292f23SGarrett Wollman case TCPOPT_CCNEW: 2587a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2588a0292f23SGarrett Wollman continue; 2589be2ac88cSJonathan Lemon if (!is_syn) 2590a0292f23SGarrett Wollman continue; 2591be2ac88cSJonathan Lemon to->to_flags |= TOF_CCNEW; 2592a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2593a0292f23SGarrett Wollman (char *)&to->to_cc, sizeof(to->to_cc)); 2594fd8e4ebcSMike Barcroft to->to_cc = ntohl(to->to_cc); 2595a0292f23SGarrett Wollman break; 2596a0292f23SGarrett Wollman case TCPOPT_CCECHO: 2597a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2598a0292f23SGarrett Wollman continue; 2599be2ac88cSJonathan Lemon if (!is_syn) 2600a0292f23SGarrett Wollman continue; 2601be2ac88cSJonathan Lemon to->to_flags |= TOF_CCECHO; 2602a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2603a0292f23SGarrett Wollman (char *)&to->to_ccecho, sizeof(to->to_ccecho)); 2604fd8e4ebcSMike Barcroft to->to_ccecho = ntohl(to->to_ccecho); 2605a0292f23SGarrett Wollman break; 26061cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 26071cfd4b53SBruce M Simpson /* 26081cfd4b53SBruce M Simpson * XXX In order to reply to a host which has set the 26091cfd4b53SBruce M Simpson * TCP_SIGNATURE option in its initial SYN, we have to 26101cfd4b53SBruce M Simpson * record the fact that the option was observed here 26111cfd4b53SBruce M Simpson * for the syncache code to perform the correct response. 26121cfd4b53SBruce M Simpson */ 26131cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 26141cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 26151cfd4b53SBruce M Simpson continue; 26161cfd4b53SBruce M Simpson to->to_flags |= (TOF_SIGNATURE | TOF_SIGLEN); 26171cfd4b53SBruce M Simpson break; 2618265ed012SBruce M Simpson #endif 2619be2ac88cSJonathan Lemon default: 2620be2ac88cSJonathan Lemon continue; 2621df8bae1dSRodney W. Grimes } 2622df8bae1dSRodney W. Grimes } 2623df8bae1dSRodney W. Grimes } 2624df8bae1dSRodney W. Grimes 2625df8bae1dSRodney W. Grimes /* 2626df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 2627df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 2628df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 2629df8bae1dSRodney W. Grimes * sequencing purposes. 2630df8bae1dSRodney W. Grimes */ 26310312fbe9SPoul-Henning Kamp static void 2632fb59c426SYoshinobu Inoue tcp_pulloutofband(so, th, m, off) 2633df8bae1dSRodney W. Grimes struct socket *so; 2634fb59c426SYoshinobu Inoue struct tcphdr *th; 2635df8bae1dSRodney W. Grimes register struct mbuf *m; 2636fb59c426SYoshinobu Inoue int off; /* delayed to be droped hdrlen */ 2637df8bae1dSRodney W. Grimes { 2638fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 2639df8bae1dSRodney W. Grimes 2640df8bae1dSRodney W. Grimes while (cnt >= 0) { 2641df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 2642df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 2643df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 2644df8bae1dSRodney W. Grimes 2645df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 2646df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 2647df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2648df8bae1dSRodney W. Grimes m->m_len--; 264969a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 265069a34685SYoshinobu Inoue m->m_pkthdr.len--; 2651df8bae1dSRodney W. Grimes return; 2652df8bae1dSRodney W. Grimes } 2653df8bae1dSRodney W. Grimes cnt -= m->m_len; 2654df8bae1dSRodney W. Grimes m = m->m_next; 2655df8bae1dSRodney W. Grimes if (m == 0) 2656df8bae1dSRodney W. Grimes break; 2657df8bae1dSRodney W. Grimes } 2658df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 2659df8bae1dSRodney W. Grimes } 2660df8bae1dSRodney W. Grimes 2661df8bae1dSRodney W. Grimes /* 2662df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 2663df8bae1dSRodney W. Grimes * and update averages and current timeout. 2664df8bae1dSRodney W. Grimes */ 26650312fbe9SPoul-Henning Kamp static void 2666df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt) 2667df8bae1dSRodney W. Grimes register struct tcpcb *tp; 26689b8b58e0SJonathan Lemon int rtt; 2669df8bae1dSRodney W. Grimes { 2670233e8c18SGarrett Wollman register int delta; 2671233e8c18SGarrett Wollman 2672233e8c18SGarrett Wollman tcpstat.tcps_rttupdated++; 2673233e8c18SGarrett Wollman tp->t_rttupdated++; 2674233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 2675233e8c18SGarrett Wollman /* 2676233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 2677233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 2678233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 2679233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2680233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 2681233e8c18SGarrett Wollman */ 2682233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2683233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2684233e8c18SGarrett Wollman 2685233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 2686233e8c18SGarrett Wollman tp->t_srtt = 1; 2687233e8c18SGarrett Wollman 2688233e8c18SGarrett Wollman /* 2689233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 2690233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 2691233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 2692233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 2693233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 2694233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 2695233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2696233e8c18SGarrett Wollman * rfc793's wired-in beta. 2697233e8c18SGarrett Wollman */ 2698233e8c18SGarrett Wollman if (delta < 0) 2699233e8c18SGarrett Wollman delta = -delta; 2700233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2701233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 2702233e8c18SGarrett Wollman tp->t_rttvar = 1; 27031fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 27041fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2705233e8c18SGarrett Wollman } else { 2706233e8c18SGarrett Wollman /* 2707233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 2708233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 2709233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 2710233e8c18SGarrett Wollman */ 2711233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 2712233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 27131fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2714233e8c18SGarrett Wollman } 27159b8b58e0SJonathan Lemon tp->t_rtttime = 0; 2716df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 2717df8bae1dSRodney W. Grimes 2718df8bae1dSRodney W. Grimes /* 2719df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 2720df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 2721df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 2722df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 2723df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 2724df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 2725df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 2726df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 2727df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 2728df8bae1dSRodney W. Grimes */ 2729233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 27309e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2731df8bae1dSRodney W. Grimes 2732df8bae1dSRodney W. Grimes /* 2733df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 2734df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 2735df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 2736df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 2737df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 2738df8bae1dSRodney W. Grimes */ 2739df8bae1dSRodney W. Grimes tp->t_softerror = 0; 2740df8bae1dSRodney W. Grimes } 2741df8bae1dSRodney W. Grimes 2742df8bae1dSRodney W. Grimes /* 2743df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 2744df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 2745df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 2746df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 2747df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2748df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 2749df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 2750df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 2751df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 2752df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 2753df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 2754df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 2755df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 2756a0292f23SGarrett Wollman * 2757a0292f23SGarrett Wollman * Also take into account the space needed for options that we 2758a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 2759a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 2760a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 2761a0292f23SGarrett Wollman * data in maxopd. 2762a0292f23SGarrett Wollman * 2763a0292f23SGarrett Wollman * 2764a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 2765a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 2766a0292f23SGarrett Wollman * MSS of our peer. 276797d8d152SAndre Oppermann * 276897d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 276997d8d152SAndre Oppermann * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt(). 2770df8bae1dSRodney W. Grimes */ 2771a0292f23SGarrett Wollman void 2772df8bae1dSRodney W. Grimes tcp_mss(tp, offer) 2773a0292f23SGarrett Wollman struct tcpcb *tp; 2774a0292f23SGarrett Wollman int offer; 2775df8bae1dSRodney W. Grimes { 277697d8d152SAndre Oppermann int rtt, mss; 2777df8bae1dSRodney W. Grimes u_long bufsize; 277897d8d152SAndre Oppermann u_long maxmtu; 2779c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 2780df8bae1dSRodney W. Grimes struct socket *so; 278197d8d152SAndre Oppermann struct hc_metrics_lite metrics; 278297d8d152SAndre Oppermann struct rmxp_tao tao; 2783a0292f23SGarrett Wollman int origoffer = offer; 2784fb59c426SYoshinobu Inoue #ifdef INET6 2785c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2786c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 2787c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 2788c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 2789c068736aSJeffrey Hsu #else 2790c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 2791fb59c426SYoshinobu Inoue #endif 279297d8d152SAndre Oppermann bzero(&tao, sizeof(tao)); 2793df8bae1dSRodney W. Grimes 279497d8d152SAndre Oppermann /* initialize */ 279597d8d152SAndre Oppermann #ifdef INET6 279697d8d152SAndre Oppermann if (isipv6) { 279797d8d152SAndre Oppermann maxmtu = tcp_maxmtu6(&inp->inp_inc); 279897d8d152SAndre Oppermann tp->t_maxopd = tp->t_maxseg = tcp_v6mssdflt; 279997d8d152SAndre Oppermann } else 280097d8d152SAndre Oppermann #endif 280197d8d152SAndre Oppermann { 280297d8d152SAndre Oppermann maxmtu = tcp_maxmtu(&inp->inp_inc); 280397d8d152SAndre Oppermann tp->t_maxopd = tp->t_maxseg = tcp_mssdflt; 2804df8bae1dSRodney W. Grimes } 2805df8bae1dSRodney W. Grimes so = inp->inp_socket; 2806df8bae1dSRodney W. Grimes 280797d8d152SAndre Oppermann /* 28082d166c02SAndre Oppermann * no route to sender, stay with default mss and return 280997d8d152SAndre Oppermann */ 281097d8d152SAndre Oppermann if (maxmtu == 0) 281197d8d152SAndre Oppermann return; 281297d8d152SAndre Oppermann 281397d8d152SAndre Oppermann /* what have we got? */ 281497d8d152SAndre Oppermann switch (offer) { 281597d8d152SAndre Oppermann case 0: 281697d8d152SAndre Oppermann /* 281797d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 281897d8d152SAndre Oppermann * segment, in this case we use tcp_mssdflt. 281997d8d152SAndre Oppermann */ 282097d8d152SAndre Oppermann offer = 282197d8d152SAndre Oppermann #ifdef INET6 282297d8d152SAndre Oppermann isipv6 ? tcp_v6mssdflt : 282397d8d152SAndre Oppermann #endif 282497d8d152SAndre Oppermann tcp_mssdflt; 282597d8d152SAndre Oppermann break; 282697d8d152SAndre Oppermann 282797d8d152SAndre Oppermann case -1: 2828a0292f23SGarrett Wollman /* 2829a0292f23SGarrett Wollman * Offer == -1 means that we didn't receive SYN yet, 2830a0292f23SGarrett Wollman * use cached value in that case; 2831a0292f23SGarrett Wollman */ 283297d8d152SAndre Oppermann if (tcp_do_rfc1644) 283397d8d152SAndre Oppermann tcp_hc_gettao(&inp->inp_inc, &tao); 283497d8d152SAndre Oppermann if (tao.tao_mssopt != 0) 283597d8d152SAndre Oppermann offer = tao.tao_mssopt; 283697d8d152SAndre Oppermann /* FALLTHROUGH */ 283797d8d152SAndre Oppermann 283897d8d152SAndre Oppermann default: 2839a0292f23SGarrett Wollman /* 284053369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 284153369ac9SAndre Oppermann * to at least minmss. 284253369ac9SAndre Oppermann */ 284353369ac9SAndre Oppermann offer = max(offer, tcp_minmss); 284453369ac9SAndre Oppermann /* 2845a0292f23SGarrett Wollman * Sanity check: make sure that maxopd will be large 284697d8d152SAndre Oppermann * enough to allow some data on segments even if the 2847a0292f23SGarrett Wollman * all the option space is used (40bytes). Otherwise 2848a0292f23SGarrett Wollman * funny things may happen in tcp_output. 2849a0292f23SGarrett Wollman */ 2850a0292f23SGarrett Wollman offer = max(offer, 64); 285197d8d152SAndre Oppermann if (tcp_do_rfc1644) 285297d8d152SAndre Oppermann tcp_hc_updatetao(&inp->inp_inc, 285397d8d152SAndre Oppermann TCP_HC_TAO_MSSOPT, 0, offer); 285497d8d152SAndre Oppermann } 2855a0292f23SGarrett Wollman 2856df8bae1dSRodney W. Grimes /* 285797d8d152SAndre Oppermann * rmx information is now retrieved from tcp_hostcache 2858df8bae1dSRodney W. Grimes */ 285997d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 286097d8d152SAndre Oppermann 2861df8bae1dSRodney W. Grimes /* 286297d8d152SAndre Oppermann * if there's a discovered mtu int tcp hostcache, use it 2863fb59c426SYoshinobu Inoue * else, use the link mtu. 2864df8bae1dSRodney W. Grimes */ 286597d8d152SAndre Oppermann if (metrics.rmx_mtu) 28662d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 2867c068736aSJeffrey Hsu else { 286831b3783cSHajimu UMEMOTO #ifdef INET6 2869fb59c426SYoshinobu Inoue if (isipv6) { 287097d8d152SAndre Oppermann mss = maxmtu - min_protoh; 287197d8d152SAndre Oppermann if (!path_mtu_discovery && 287297d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 2873fb59c426SYoshinobu Inoue mss = min(mss, tcp_v6mssdflt); 2874b3399803SHajimu UMEMOTO } else 2875b3399803SHajimu UMEMOTO #endif 287697d8d152SAndre Oppermann { 287797d8d152SAndre Oppermann mss = maxmtu - min_protoh; 287897d8d152SAndre Oppermann if (!path_mtu_discovery && 287997d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 2880a0292f23SGarrett Wollman mss = min(mss, tcp_mssdflt); 2881a0292f23SGarrett Wollman } 288297d8d152SAndre Oppermann } 2883a0292f23SGarrett Wollman mss = min(mss, offer); 288497d8d152SAndre Oppermann 2885a0292f23SGarrett Wollman /* 2886a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 2887a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 2888a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 2889a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 2890a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 2891a0292f23SGarrett Wollman */ 2892a0292f23SGarrett Wollman tp->t_maxopd = mss; 2893a0292f23SGarrett Wollman 2894a0292f23SGarrett Wollman /* 2895a0292f23SGarrett Wollman * In case of T/TCP, origoffer==-1 indicates, that no segments 2896a0292f23SGarrett Wollman * were received yet. In this case we just guess, otherwise 2897a0292f23SGarrett Wollman * we do the same as before T/TCP. 2898a0292f23SGarrett Wollman */ 2899a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2900a0292f23SGarrett Wollman (origoffer == -1 || 2901a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2902a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 2903a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 2904a0292f23SGarrett Wollman (origoffer == -1 || 2905a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)) 2906a0292f23SGarrett Wollman mss -= TCPOLEN_CC_APPA; 290797d8d152SAndre Oppermann tp->t_maxseg = mss; 2908a0292f23SGarrett Wollman 2909df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 2910df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2911df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 2912df8bae1dSRodney W. Grimes #else 2913df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2914df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 2915df8bae1dSRodney W. Grimes #endif 291697d8d152SAndre Oppermann tp->t_maxseg = mss; 291797d8d152SAndre Oppermann 2918df8bae1dSRodney W. Grimes /* 291997d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 292097d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 292197d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 292297d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 292397d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 2924df8bae1dSRodney W. Grimes */ 292597d8d152SAndre Oppermann if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe) 292697d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 292797d8d152SAndre Oppermann else 2928df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 2929df8bae1dSRodney W. Grimes if (bufsize < mss) 2930df8bae1dSRodney W. Grimes mss = bufsize; 2931df8bae1dSRodney W. Grimes else { 2932df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2933df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2934df8bae1dSRodney W. Grimes bufsize = sb_max; 293588c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 2936ecf72308SBrian Feldman (void)sbreserve(&so->so_snd, bufsize, so, NULL); 2937df8bae1dSRodney W. Grimes } 2938df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 2939df8bae1dSRodney W. Grimes 294097d8d152SAndre Oppermann if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe) 294197d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 294297d8d152SAndre Oppermann else 2943df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 2944df8bae1dSRodney W. Grimes if (bufsize > mss) { 2945df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2946df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2947df8bae1dSRodney W. Grimes bufsize = sb_max; 294888c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 2949ecf72308SBrian Feldman (void)sbreserve(&so->so_rcv, bufsize, so, NULL); 2950df8bae1dSRodney W. Grimes } 2951a0292f23SGarrett Wollman /* 295297d8d152SAndre Oppermann * While we're here, check the others too 2953a0292f23SGarrett Wollman */ 295497d8d152SAndre Oppermann if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 295597d8d152SAndre Oppermann tp->t_srtt = rtt; 295697d8d152SAndre Oppermann tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 295797d8d152SAndre Oppermann tcpstat.tcps_usedrtt++; 295897d8d152SAndre Oppermann if (metrics.rmx_rttvar) { 295997d8d152SAndre Oppermann tp->t_rttvar = metrics.rmx_rttvar; 296097d8d152SAndre Oppermann tcpstat.tcps_usedrttvar++; 296197d8d152SAndre Oppermann } else { 296297d8d152SAndre Oppermann /* default variation is +- 1 rtt */ 296397d8d152SAndre Oppermann tp->t_rttvar = 296497d8d152SAndre Oppermann tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 296597d8d152SAndre Oppermann } 296697d8d152SAndre Oppermann TCPT_RANGESET(tp->t_rxtcur, 296797d8d152SAndre Oppermann ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 296897d8d152SAndre Oppermann tp->t_rttmin, TCPTV_REXMTMAX); 296997d8d152SAndre Oppermann } 297097d8d152SAndre Oppermann if (metrics.rmx_ssthresh) { 2971df8bae1dSRodney W. Grimes /* 2972df8bae1dSRodney W. Grimes * There's some sort of gateway or interface 2973df8bae1dSRodney W. Grimes * buffer limit on the path. Use this to set 2974df8bae1dSRodney W. Grimes * the slow start threshhold, but set the 2975df8bae1dSRodney W. Grimes * threshold to no less than 2*mss. 2976df8bae1dSRodney W. Grimes */ 297797d8d152SAndre Oppermann tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh); 2978dd224982SGarrett Wollman tcpstat.tcps_usedssthresh++; 2979df8bae1dSRodney W. Grimes } 298097d8d152SAndre Oppermann if (metrics.rmx_bandwidth) 298197d8d152SAndre Oppermann tp->snd_bandwidth = metrics.rmx_bandwidth; 298297d8d152SAndre Oppermann 298397d8d152SAndre Oppermann /* 298497d8d152SAndre Oppermann * Set the slow-start flight size depending on whether this 298597d8d152SAndre Oppermann * is a local network or not. 298697d8d152SAndre Oppermann * 298797d8d152SAndre Oppermann * Extend this so we cache the cwnd too and retrieve it here. 298897d8d152SAndre Oppermann * Make cwnd even bigger than RFC3390 suggests but only if we 298997d8d152SAndre Oppermann * have previous experience with the remote host. Be careful 299097d8d152SAndre Oppermann * not make cwnd bigger than remote receive window or our own 299197d8d152SAndre Oppermann * send socket buffer. Maybe put some additional upper bound 299297d8d152SAndre Oppermann * on the retrieved cwnd. Should do incremental updates to 299397d8d152SAndre Oppermann * hostcache when cwnd collapses so next connection doesn't 299497d8d152SAndre Oppermann * overloads the path again. 299597d8d152SAndre Oppermann * 299697d8d152SAndre Oppermann * RFC3390 says only do this if SYN or SYN/ACK didn't got lost. 299797d8d152SAndre Oppermann * We currently check only in syncache_socket for that. 299897d8d152SAndre Oppermann */ 299997d8d152SAndre Oppermann #define TCP_METRICS_CWND 300097d8d152SAndre Oppermann #ifdef TCP_METRICS_CWND 300197d8d152SAndre Oppermann if (metrics.rmx_cwnd) 300297d8d152SAndre Oppermann tp->snd_cwnd = max(mss, 300397d8d152SAndre Oppermann min(metrics.rmx_cwnd / 2, 300497d8d152SAndre Oppermann min(tp->snd_wnd, so->so_snd.sb_hiwat))); 300597d8d152SAndre Oppermann else 300697d8d152SAndre Oppermann #endif 300797d8d152SAndre Oppermann if (tcp_do_rfc3390) 300897d8d152SAndre Oppermann tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380)); 300997d8d152SAndre Oppermann #ifdef INET6 301097d8d152SAndre Oppermann else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) || 301197d8d152SAndre Oppermann (!isipv6 && in_localaddr(inp->inp_faddr))) 3012943ae302SAndre Oppermann #else 3013943ae302SAndre Oppermann else if (in_localaddr(inp->inp_faddr)) 301497d8d152SAndre Oppermann #endif 3015943ae302SAndre Oppermann tp->snd_cwnd = mss * ss_fltsz_local; 301697d8d152SAndre Oppermann else 301797d8d152SAndre Oppermann tp->snd_cwnd = mss * ss_fltsz; 3018a0292f23SGarrett Wollman } 3019a0292f23SGarrett Wollman 3020a0292f23SGarrett Wollman /* 3021a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3022a0292f23SGarrett Wollman */ 3023a0292f23SGarrett Wollman int 302497d8d152SAndre Oppermann tcp_mssopt(inc) 302597d8d152SAndre Oppermann struct in_conninfo *inc; 3026a0292f23SGarrett Wollman { 302797d8d152SAndre Oppermann int mss = 0; 302897d8d152SAndre Oppermann u_long maxmtu = 0; 302997d8d152SAndre Oppermann u_long thcmtu = 0; 303097d8d152SAndre Oppermann size_t min_protoh; 3031fb59c426SYoshinobu Inoue #ifdef INET6 303297d8d152SAndre Oppermann int isipv6 = inc->inc_isipv6 ? 1 : 0; 3033fb59c426SYoshinobu Inoue #endif 3034a0292f23SGarrett Wollman 303597d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3036a0292f23SGarrett Wollman 303731b3783cSHajimu UMEMOTO #ifdef INET6 303897d8d152SAndre Oppermann if (isipv6) { 303997d8d152SAndre Oppermann mss = tcp_v6mssdflt; 304097d8d152SAndre Oppermann maxmtu = tcp_maxmtu6(inc); 304197d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 304297d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 304397d8d152SAndre Oppermann } else 304431b3783cSHajimu UMEMOTO #endif 304597d8d152SAndre Oppermann { 304697d8d152SAndre Oppermann mss = tcp_mssdflt; 304797d8d152SAndre Oppermann maxmtu = tcp_maxmtu(inc); 304897d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 304997d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 305097d8d152SAndre Oppermann } 305197d8d152SAndre Oppermann if (maxmtu && thcmtu) 305297d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 305397d8d152SAndre Oppermann else if (maxmtu || thcmtu) 305497d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 305597d8d152SAndre Oppermann 305697d8d152SAndre Oppermann return (mss); 3057df8bae1dSRodney W. Grimes } 305846f58482SJonathan Lemon 305946f58482SJonathan Lemon 306046f58482SJonathan Lemon /* 3061c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3062c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3063c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3064c068736aSJeffrey Hsu * be started again. 306546f58482SJonathan Lemon */ 3066c068736aSJeffrey Hsu static void 3067c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th) 306846f58482SJonathan Lemon struct tcpcb *tp; 306946f58482SJonathan Lemon struct tcphdr *th; 307046f58482SJonathan Lemon { 307146f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 307246f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 307346f58482SJonathan Lemon 307446f58482SJonathan Lemon callout_stop(tp->tt_rexmt); 307546f58482SJonathan Lemon tp->t_rtttime = 0; 307646f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 30776b2a5f92SJayanth Vijayaraghavan /* 3078c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3079c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 30806b2a5f92SJayanth Vijayaraghavan */ 30816b2a5f92SJayanth Vijayaraghavan tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 3082a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 30836b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 308446f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 308546f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 308646f58482SJonathan Lemon tp->snd_nxt = onxt; 308746f58482SJonathan Lemon /* 308846f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 308946f58482SJonathan Lemon * not updated yet. 309046f58482SJonathan Lemon */ 309146f58482SJonathan Lemon tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg); 309246f58482SJonathan Lemon } 3093340c35deSJonathan Lemon 3094340c35deSJonathan Lemon /* 3095340c35deSJonathan Lemon * Returns 1 if the TIME_WAIT state was killed and we should start over, 3096340c35deSJonathan Lemon * looking for a pcb in the listen state. Returns 0 otherwise. 3097340c35deSJonathan Lemon */ 3098340c35deSJonathan Lemon static int 3099340c35deSJonathan Lemon tcp_timewait(tw, to, th, m, tlen) 3100340c35deSJonathan Lemon struct tcptw *tw; 3101340c35deSJonathan Lemon struct tcpopt *to; 3102340c35deSJonathan Lemon struct tcphdr *th; 3103340c35deSJonathan Lemon struct mbuf *m; 3104340c35deSJonathan Lemon int tlen; 3105340c35deSJonathan Lemon { 3106340c35deSJonathan Lemon int thflags; 3107340c35deSJonathan Lemon tcp_seq seq; 3108340c35deSJonathan Lemon #ifdef INET6 3109340c35deSJonathan Lemon int isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 3110340c35deSJonathan Lemon #else 3111340c35deSJonathan Lemon const int isipv6 = 0; 3112340c35deSJonathan Lemon #endif 3113340c35deSJonathan Lemon 3114340c35deSJonathan Lemon thflags = th->th_flags; 3115340c35deSJonathan Lemon 3116340c35deSJonathan Lemon /* 3117340c35deSJonathan Lemon * NOTE: for FIN_WAIT_2 (to be added later), 3118340c35deSJonathan Lemon * must validate sequence number before accepting RST 3119340c35deSJonathan Lemon */ 3120340c35deSJonathan Lemon 3121340c35deSJonathan Lemon /* 3122340c35deSJonathan Lemon * If the segment contains RST: 3123340c35deSJonathan Lemon * Drop the segment - see Stevens, vol. 2, p. 964 and 3124340c35deSJonathan Lemon * RFC 1337. 3125340c35deSJonathan Lemon */ 3126340c35deSJonathan Lemon if (thflags & TH_RST) 3127340c35deSJonathan Lemon goto drop; 3128340c35deSJonathan Lemon 3129340c35deSJonathan Lemon /* 3130340c35deSJonathan Lemon * If segment contains a SYN and CC [not CC.NEW] option: 3131340c35deSJonathan Lemon * if connection duration > MSL, drop packet and send RST; 3132340c35deSJonathan Lemon * 3133340c35deSJonathan Lemon * if SEG.CC > CCrecv then is new SYN. 3134340c35deSJonathan Lemon * Complete close and delete TCPCB. Then reprocess 3135340c35deSJonathan Lemon * segment, hoping to find new TCPCB in LISTEN state; 3136340c35deSJonathan Lemon * 3137340c35deSJonathan Lemon * else must be old SYN; drop it. 3138340c35deSJonathan Lemon * else do normal processing. 3139340c35deSJonathan Lemon */ 3140340c35deSJonathan Lemon if ((thflags & TH_SYN) && (to->to_flags & TOF_CC) && tw->cc_recv != 0) { 3141340c35deSJonathan Lemon if ((ticks - tw->t_starttime) > tcp_msl) 3142340c35deSJonathan Lemon goto reset; 3143340c35deSJonathan Lemon if (CC_GT(to->to_cc, tw->cc_recv)) { 3144607b0b0cSJonathan Lemon (void) tcp_twclose(tw, 0); 3145340c35deSJonathan Lemon return (1); 3146340c35deSJonathan Lemon } 3147340c35deSJonathan Lemon goto drop; 3148340c35deSJonathan Lemon } 3149340c35deSJonathan Lemon 3150340c35deSJonathan Lemon #if 0 3151340c35deSJonathan Lemon /* PAWS not needed at the moment */ 3152340c35deSJonathan Lemon /* 3153340c35deSJonathan Lemon * RFC 1323 PAWS: If we have a timestamp reply on this segment 3154340c35deSJonathan Lemon * and it's less than ts_recent, drop it. 3155340c35deSJonathan Lemon */ 3156340c35deSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 3157340c35deSJonathan Lemon TSTMP_LT(to.to_tsval, tp->ts_recent)) { 3158340c35deSJonathan Lemon if ((thflags & TH_ACK) == 0) 3159340c35deSJonathan Lemon goto drop; 3160340c35deSJonathan Lemon goto ack; 3161340c35deSJonathan Lemon } 3162340c35deSJonathan Lemon /* 3163340c35deSJonathan Lemon * ts_recent is never updated because we never accept new segments. 3164340c35deSJonathan Lemon */ 3165340c35deSJonathan Lemon #endif 3166340c35deSJonathan Lemon 3167340c35deSJonathan Lemon /* 3168340c35deSJonathan Lemon * If a new connection request is received 3169340c35deSJonathan Lemon * while in TIME_WAIT, drop the old connection 3170340c35deSJonathan Lemon * and start over if the sequence numbers 3171340c35deSJonathan Lemon * are above the previous ones. 3172340c35deSJonathan Lemon */ 3173340c35deSJonathan Lemon if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) { 3174607b0b0cSJonathan Lemon (void) tcp_twclose(tw, 0); 3175340c35deSJonathan Lemon return (1); 3176340c35deSJonathan Lemon } 3177340c35deSJonathan Lemon 3178340c35deSJonathan Lemon /* 3179340c35deSJonathan Lemon * Drop the the segment if it does not contain an ACK. 3180340c35deSJonathan Lemon */ 3181340c35deSJonathan Lemon if ((thflags & TH_ACK) == 0) 3182340c35deSJonathan Lemon goto drop; 3183340c35deSJonathan Lemon 3184340c35deSJonathan Lemon /* 3185340c35deSJonathan Lemon * Reset the 2MSL timer if this is a duplicate FIN. 3186340c35deSJonathan Lemon */ 3187340c35deSJonathan Lemon if (thflags & TH_FIN) { 3188340c35deSJonathan Lemon seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0); 3189340c35deSJonathan Lemon if (seq + 1 == tw->rcv_nxt) 3190607b0b0cSJonathan Lemon tcp_timer_2msl_reset(tw, 2 * tcp_msl); 3191340c35deSJonathan Lemon } 3192340c35deSJonathan Lemon 3193340c35deSJonathan Lemon /* 3194272c5dfeSJonathan Lemon * Acknowledge the segment if it has data or is not a duplicate ACK. 3195340c35deSJonathan Lemon */ 3196272c5dfeSJonathan Lemon if (thflags != TH_ACK || tlen != 0 || 3197272c5dfeSJonathan Lemon th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt) 3198a7b6a14aSRobert Watson tcp_twrespond(tw, TH_ACK); 3199340c35deSJonathan Lemon goto drop; 3200340c35deSJonathan Lemon 3201340c35deSJonathan Lemon reset: 3202340c35deSJonathan Lemon /* 3203340c35deSJonathan Lemon * Generate a RST, dropping incoming segment. 3204340c35deSJonathan Lemon * Make ACK acceptable to originator of segment. 3205340c35deSJonathan Lemon * Don't bother to respond if destination was broadcast/multicast. 3206340c35deSJonathan Lemon */ 3207340c35deSJonathan Lemon if (m->m_flags & (M_BCAST|M_MCAST)) 3208340c35deSJonathan Lemon goto drop; 3209340c35deSJonathan Lemon if (isipv6) { 3210340c35deSJonathan Lemon struct ip6_hdr *ip6; 3211340c35deSJonathan Lemon 3212340c35deSJonathan Lemon /* IPv6 anycast check is done at tcp6_input() */ 3213340c35deSJonathan Lemon ip6 = mtod(m, struct ip6_hdr *); 3214340c35deSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3215340c35deSJonathan Lemon IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3216340c35deSJonathan Lemon goto drop; 3217340c35deSJonathan Lemon } else { 3218340c35deSJonathan Lemon struct ip *ip; 3219340c35deSJonathan Lemon 3220340c35deSJonathan Lemon ip = mtod(m, struct ip *); 3221340c35deSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3222340c35deSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3223340c35deSJonathan Lemon ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3224340c35deSJonathan Lemon in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3225340c35deSJonathan Lemon goto drop; 3226340c35deSJonathan Lemon } 3227340c35deSJonathan Lemon if (thflags & TH_ACK) { 3228340c35deSJonathan Lemon tcp_respond(NULL, 3229340c35deSJonathan Lemon mtod(m, void *), th, m, 0, th->th_ack, TH_RST); 3230340c35deSJonathan Lemon } else { 3231340c35deSJonathan Lemon seq = th->th_seq + (thflags & TH_SYN ? 1 : 0); 3232340c35deSJonathan Lemon tcp_respond(NULL, 3233340c35deSJonathan Lemon mtod(m, void *), th, m, seq, 0, TH_RST|TH_ACK); 3234340c35deSJonathan Lemon } 3235340c35deSJonathan Lemon INP_UNLOCK(tw->tw_inpcb); 3236340c35deSJonathan Lemon return (0); 3237340c35deSJonathan Lemon 3238340c35deSJonathan Lemon drop: 3239340c35deSJonathan Lemon INP_UNLOCK(tw->tw_inpcb); 3240340c35deSJonathan Lemon m_freem(m); 3241340c35deSJonathan Lemon return (0); 3242340c35deSJonathan Lemon } 3243