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" 406d90faf3SPaul Saab #include "opt_tcp_sack.h" 410cc12cc5SJoerg Wunsch 42df8bae1dSRodney W. Grimes #include <sys/param.h> 4398163b98SPoul-Henning Kamp #include <sys/kernel.h> 44c488362eSRobert Watson #include <sys/mac.h> 45df8bae1dSRodney W. Grimes #include <sys/malloc.h> 46df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 47a29f300eSGarrett Wollman #include <sys/proc.h> /* for proc0 declaration */ 48df8bae1dSRodney W. Grimes #include <sys/protosw.h> 49960ed29cSSeigo Tanimura #include <sys/signalvar.h> 50df8bae1dSRodney W. Grimes #include <sys/socket.h> 51df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 52960ed29cSSeigo Tanimura #include <sys/sysctl.h> 53816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 54960ed29cSSeigo Tanimura #include <sys/systm.h> 55df8bae1dSRodney W. Grimes 56e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 57e79adb8eSGarrett Wollman 5812e2e970SAndre Oppermann #include <vm/uma.h> 5912e2e970SAndre Oppermann 60df8bae1dSRodney W. Grimes #include <net/if.h> 61df8bae1dSRodney W. Grimes #include <net/route.h> 62df8bae1dSRodney W. Grimes 63df8bae1dSRodney W. Grimes #include <netinet/in.h> 64960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 65df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 66960ed29cSSeigo Tanimura #include <netinet/in_var.h> 67df8bae1dSRodney W. Grimes #include <netinet/ip.h> 68686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */ 69686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 70df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 71686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 72686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h> 73686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 74960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h> 75960ed29cSSeigo Tanimura #include <netinet6/nd6.h> 76df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 77df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 78df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 79df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 80df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 81fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 82df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 83610ee2f9SDavid Greenman #ifdef TCPDEBUG 84df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 85fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */ 86fb59c426SYoshinobu Inoue 87b9234fafSSam Leffler #ifdef FAST_IPSEC 88b9234fafSSam Leffler #include <netipsec/ipsec.h> 89b9234fafSSam Leffler #include <netipsec/ipsec6.h> 90b9234fafSSam Leffler #endif /*FAST_IPSEC*/ 91b9234fafSSam Leffler 92fb59c426SYoshinobu Inoue #ifdef IPSEC 93fb59c426SYoshinobu Inoue #include <netinet6/ipsec.h> 943a2a9f79SYoshinobu Inoue #include <netinet6/ipsec6.h> 95fb59c426SYoshinobu Inoue #include <netkey/key.h> 96fb59c426SYoshinobu Inoue #endif /*IPSEC*/ 97fb59c426SYoshinobu Inoue 98db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 99db4f9cc7SJonathan Lemon 100c068736aSJeffrey Hsu static const int tcprexmtthresh = 3; 1012f96f1f4SGarrett Wollman tcp_cc tcp_ccgen; 1020312fbe9SPoul-Henning Kamp 1032f96f1f4SGarrett Wollman struct tcpstat tcpstat; 104c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW, 1053d177f46SBill Fumerola &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 1060312fbe9SPoul-Henning Kamp 107d78a37adSPaul Traina static int log_in_vain = 0; 108816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 1093d177f46SBill Fumerola &log_in_vain, 0, "Log all incoming TCP connections"); 110816a3d83SPoul-Henning Kamp 11116f7f31fSGeoff Rehmet static int blackhole = 0; 11216f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 11316f7f31fSGeoff Rehmet &blackhole, 0, "Do not send RST when dropping refused connections"); 11416f7f31fSGeoff Rehmet 115f498eeeeSDavid Greenman int tcp_delack_enabled = 1; 11684e33c9eSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 1173d177f46SBill Fumerola &tcp_delack_enabled, 0, 1183d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 119f498eeeeSDavid Greenman 120f8613305SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN 121f8613305SDag-Erling Smørgrav static int drop_synfin = 0; 122f8613305SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, 123f8613305SDag-Erling Smørgrav &drop_synfin, 0, "Drop TCP packets with SYN+FIN set"); 124f8613305SDag-Erling Smørgrav #endif 125f8613305SDag-Erling Smørgrav 126dba7bc6aSAndre Oppermann static int tcp_do_rfc3042 = 1; 127582a954bSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW, 128582a954bSJeffrey Hsu &tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)"); 129582a954bSJeffrey Hsu 130dba7bc6aSAndre Oppermann static int tcp_do_rfc3390 = 1; 131da3a8a1aSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW, 132da3a8a1aSJeffrey Hsu &tcp_do_rfc3390, 0, 133da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 134da3a8a1aSJeffrey Hsu 13512e2e970SAndre Oppermann SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0, 13612e2e970SAndre Oppermann "TCP Segment Reassembly Queue"); 13712e2e970SAndre Oppermann 13812e2e970SAndre Oppermann static int tcp_reass_maxseg = 0; 13912e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RDTUN, 14012e2e970SAndre Oppermann &tcp_reass_maxseg, 0, 14112e2e970SAndre Oppermann "Global maximum number of TCP Segments in Reassembly Queue"); 14212e2e970SAndre Oppermann 14312e2e970SAndre Oppermann int tcp_reass_qsize = 0; 14412e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, cursegments, CTLFLAG_RD, 14512e2e970SAndre Oppermann &tcp_reass_qsize, 0, 14612e2e970SAndre Oppermann "Global number of TCP Segments currently in Reassembly Queue"); 14712e2e970SAndre Oppermann 14812e2e970SAndre Oppermann static int tcp_reass_maxqlen = 48; 14912e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxqlen, CTLFLAG_RW, 15012e2e970SAndre Oppermann &tcp_reass_maxqlen, 0, 15112e2e970SAndre Oppermann "Maximum number of TCP Segments per individual Reassembly Queue"); 15212e2e970SAndre Oppermann 15312e2e970SAndre Oppermann static int tcp_reass_overflows = 0; 15412e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, overflows, CTLFLAG_RD, 15512e2e970SAndre Oppermann &tcp_reass_overflows, 0, 15612e2e970SAndre Oppermann "Global number of TCP Segment Reassembly Queue Overflows"); 15712e2e970SAndre Oppermann 15815bd2b43SDavid Greenman struct inpcbhead tcb; 159fb59c426SYoshinobu Inoue #define tcb6 tcb /* for KAME src sync over BSD*'s */ 16015bd2b43SDavid Greenman struct inpcbinfo tcbinfo; 161f76fcf6dSJeffrey Hsu struct mtx *tcbinfo_mtx; 162df8bae1dSRodney W. Grimes 1636d90faf3SPaul Saab static void tcp_dooptions(struct tcpcb *, struct tcpopt *, u_char *, 1646d90faf3SPaul Saab int, int, struct tcphdr *); 1656d90faf3SPaul Saab 1664d77a549SAlfred Perlstein static void tcp_pulloutofband(struct socket *, 1674d77a549SAlfred Perlstein struct tcphdr *, struct mbuf *, int); 1684d77a549SAlfred Perlstein static int tcp_reass(struct tcpcb *, struct tcphdr *, int *, 1694d77a549SAlfred Perlstein struct mbuf *); 1704d77a549SAlfred Perlstein static void tcp_xmit_timer(struct tcpcb *, int); 171c068736aSJeffrey Hsu static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 172340c35deSJonathan Lemon static int tcp_timewait(struct tcptw *, struct tcpopt *, 173340c35deSJonathan Lemon struct tcphdr *, struct mbuf *, int); 1740312fbe9SPoul-Henning Kamp 175fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 176fb59c426SYoshinobu Inoue #ifdef INET6 177fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \ 178fb59c426SYoshinobu Inoue do { \ 179fb59c426SYoshinobu Inoue if ((tp) && (tp)->t_inpcb && \ 18097d8d152SAndre Oppermann ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \ 18197d8d152SAndre Oppermann nd6_nud_hint(NULL, NULL, 0); \ 182fb59c426SYoshinobu Inoue } while (0) 183fb59c426SYoshinobu Inoue #else 184fb59c426SYoshinobu Inoue #define ND6_HINT(tp) 185fb59c426SYoshinobu Inoue #endif 186df8bae1dSRodney W. Grimes 187df8bae1dSRodney W. Grimes /* 188262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 189262c1c1aSMatthew Dillon * - there is no delayed ack timer in progress and 190262c1c1aSMatthew Dillon * - our last ack wasn't a 0-sized window. We never want to delay 1913bfd6421SJonathan Lemon * the ack that opens up a 0-sized window and 1923bfd6421SJonathan Lemon * - delayed acks are enabled or 1933bfd6421SJonathan Lemon * - this is a half-synchronized T/TCP connection. 194d8c85a26SJonathan Lemon */ 195d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \ 196a14c749fSJonathan Lemon ((!callout_active(tp->tt_delack) && \ 197a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 1983bfd6421SJonathan Lemon (tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 199d8c85a26SJonathan Lemon 20012e2e970SAndre Oppermann /* Initialize TCP reassembly queue */ 20112e2e970SAndre Oppermann uma_zone_t tcp_reass_zone; 20212e2e970SAndre Oppermann void 20312e2e970SAndre Oppermann tcp_reass_init() 20412e2e970SAndre Oppermann { 20512e2e970SAndre Oppermann tcp_reass_maxseg = nmbclusters / 16; 20612e2e970SAndre Oppermann TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments", 20712e2e970SAndre Oppermann &tcp_reass_maxseg); 20812e2e970SAndre Oppermann tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent), 20912e2e970SAndre Oppermann NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 21012e2e970SAndre Oppermann uma_zone_set_max(tcp_reass_zone, tcp_reass_maxseg); 21112e2e970SAndre Oppermann } 21212e2e970SAndre Oppermann 2130312fbe9SPoul-Henning Kamp static int 214fb59c426SYoshinobu Inoue tcp_reass(tp, th, tlenp, m) 215df8bae1dSRodney W. Grimes register struct tcpcb *tp; 216fb59c426SYoshinobu Inoue register struct tcphdr *th; 217fb59c426SYoshinobu Inoue int *tlenp; 218df8bae1dSRodney W. Grimes struct mbuf *m; 219df8bae1dSRodney W. Grimes { 220fb59c426SYoshinobu Inoue struct tseg_qent *q; 221fb59c426SYoshinobu Inoue struct tseg_qent *p = NULL; 222fb59c426SYoshinobu Inoue struct tseg_qent *nq; 22312e2e970SAndre Oppermann struct tseg_qent *te = NULL; 224df8bae1dSRodney W. Grimes struct socket *so = tp->t_inpcb->inp_socket; 225df8bae1dSRodney W. Grimes int flags; 226df8bae1dSRodney W. Grimes 227df8bae1dSRodney W. Grimes /* 22812e2e970SAndre Oppermann * XXX: tcp_reass() is rather inefficient with its data structures 22912e2e970SAndre Oppermann * and should be rewritten (see NetBSD for optimizations). While 23012e2e970SAndre Oppermann * doing that it should move to its own file tcp_reass.c. 23112e2e970SAndre Oppermann */ 23212e2e970SAndre Oppermann 23312e2e970SAndre Oppermann /* 234fb59c426SYoshinobu Inoue * Call with th==0 after become established to 235df8bae1dSRodney W. Grimes * force pre-ESTABLISHED data up to user socket. 236df8bae1dSRodney W. Grimes */ 237fb59c426SYoshinobu Inoue if (th == 0) 238df8bae1dSRodney W. Grimes goto present; 239df8bae1dSRodney W. Grimes 24012e2e970SAndre Oppermann /* 24112e2e970SAndre Oppermann * Limit the number of segments in the reassembly queue to prevent 24212e2e970SAndre Oppermann * holding on to too many segments (and thus running out of mbufs). 24312e2e970SAndre Oppermann * Make sure to let the missing segment through which caused this 24412e2e970SAndre Oppermann * queue. Always keep one global queue entry spare to be able to 24512e2e970SAndre Oppermann * process the missing segment. 24612e2e970SAndre Oppermann */ 24712e2e970SAndre Oppermann if (th->th_seq != tp->rcv_nxt && 24812e2e970SAndre Oppermann (tcp_reass_qsize + 1 >= tcp_reass_maxseg || 24912e2e970SAndre Oppermann tp->t_segqlen >= tcp_reass_maxqlen)) { 25012e2e970SAndre Oppermann tcp_reass_overflows++; 25112e2e970SAndre Oppermann tcpstat.tcps_rcvmemdrop++; 25212e2e970SAndre Oppermann m_freem(m); 25312e2e970SAndre Oppermann return (0); 25412e2e970SAndre Oppermann } 25512e2e970SAndre Oppermann 25612e2e970SAndre Oppermann /* 25712e2e970SAndre Oppermann * Allocate a new queue entry. If we can't, or hit the zone limit 25812e2e970SAndre Oppermann * just drop the pkt. 25912e2e970SAndre Oppermann */ 26012e2e970SAndre Oppermann te = uma_zalloc(tcp_reass_zone, M_NOWAIT); 261fb59c426SYoshinobu Inoue if (te == NULL) { 262fb59c426SYoshinobu Inoue tcpstat.tcps_rcvmemdrop++; 263fb59c426SYoshinobu Inoue m_freem(m); 264fb59c426SYoshinobu Inoue return (0); 265fb59c426SYoshinobu Inoue } 26612e2e970SAndre Oppermann tp->t_segqlen++; 26712e2e970SAndre Oppermann tcp_reass_qsize++; 2686effc713SDoug Rabson 269df8bae1dSRodney W. Grimes /* 270df8bae1dSRodney W. Grimes * Find a segment which begins after this one does. 271df8bae1dSRodney W. Grimes */ 272fb59c426SYoshinobu Inoue LIST_FOREACH(q, &tp->t_segq, tqe_q) { 273fb59c426SYoshinobu Inoue if (SEQ_GT(q->tqe_th->th_seq, th->th_seq)) 274df8bae1dSRodney W. Grimes break; 275fb59c426SYoshinobu Inoue p = q; 276fb59c426SYoshinobu Inoue } 277df8bae1dSRodney W. Grimes 278df8bae1dSRodney W. Grimes /* 279df8bae1dSRodney W. Grimes * If there is a preceding segment, it may provide some of 280df8bae1dSRodney W. Grimes * our data already. If so, drop the data from the incoming 281df8bae1dSRodney W. Grimes * segment. If it provides all of our data, drop us. 282df8bae1dSRodney W. Grimes */ 2836effc713SDoug Rabson if (p != NULL) { 284df8bae1dSRodney W. Grimes register int i; 285df8bae1dSRodney W. Grimes /* conversion to int (in i) handles seq wraparound */ 286fb59c426SYoshinobu Inoue i = p->tqe_th->th_seq + p->tqe_len - th->th_seq; 287df8bae1dSRodney W. Grimes if (i > 0) { 288fb59c426SYoshinobu Inoue if (i >= *tlenp) { 289df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 290fb59c426SYoshinobu Inoue tcpstat.tcps_rcvdupbyte += *tlenp; 291df8bae1dSRodney W. Grimes m_freem(m); 29212e2e970SAndre Oppermann uma_zfree(tcp_reass_zone, te); 29312e2e970SAndre Oppermann tp->t_segqlen--; 29412e2e970SAndre Oppermann tcp_reass_qsize--; 295a0292f23SGarrett Wollman /* 296a0292f23SGarrett Wollman * Try to present any queued data 297a0292f23SGarrett Wollman * at the left window edge to the user. 298a0292f23SGarrett Wollman * This is needed after the 3-WHS 299a0292f23SGarrett Wollman * completes. 300a0292f23SGarrett Wollman */ 301a0292f23SGarrett Wollman goto present; /* ??? */ 302df8bae1dSRodney W. Grimes } 303df8bae1dSRodney W. Grimes m_adj(m, i); 304fb59c426SYoshinobu Inoue *tlenp -= i; 305fb59c426SYoshinobu Inoue th->th_seq += i; 306df8bae1dSRodney W. Grimes } 307df8bae1dSRodney W. Grimes } 308df8bae1dSRodney W. Grimes tcpstat.tcps_rcvoopack++; 309fb59c426SYoshinobu Inoue tcpstat.tcps_rcvoobyte += *tlenp; 310df8bae1dSRodney W. Grimes 311df8bae1dSRodney W. Grimes /* 312df8bae1dSRodney W. Grimes * While we overlap succeeding segments trim them or, 313df8bae1dSRodney W. Grimes * if they are completely covered, dequeue them. 314df8bae1dSRodney W. Grimes */ 3156effc713SDoug Rabson while (q) { 316fb59c426SYoshinobu Inoue register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq; 317df8bae1dSRodney W. Grimes if (i <= 0) 318df8bae1dSRodney W. Grimes break; 319fb59c426SYoshinobu Inoue if (i < q->tqe_len) { 320fb59c426SYoshinobu Inoue q->tqe_th->th_seq += i; 321fb59c426SYoshinobu Inoue q->tqe_len -= i; 322fb59c426SYoshinobu Inoue m_adj(q->tqe_m, i); 323df8bae1dSRodney W. Grimes break; 324df8bae1dSRodney W. Grimes } 3256effc713SDoug Rabson 326fb59c426SYoshinobu Inoue nq = LIST_NEXT(q, tqe_q); 327fb59c426SYoshinobu Inoue LIST_REMOVE(q, tqe_q); 328fb59c426SYoshinobu Inoue m_freem(q->tqe_m); 32912e2e970SAndre Oppermann uma_zfree(tcp_reass_zone, q); 33012e2e970SAndre Oppermann tp->t_segqlen--; 33112e2e970SAndre Oppermann tcp_reass_qsize--; 3326effc713SDoug Rabson q = nq; 333df8bae1dSRodney W. Grimes } 334df8bae1dSRodney W. Grimes 335fb59c426SYoshinobu Inoue /* Insert the new segment queue entry into place. */ 336fb59c426SYoshinobu Inoue te->tqe_m = m; 337fb59c426SYoshinobu Inoue te->tqe_th = th; 338fb59c426SYoshinobu Inoue te->tqe_len = *tlenp; 339fb59c426SYoshinobu Inoue 3406effc713SDoug Rabson if (p == NULL) { 341fb59c426SYoshinobu Inoue LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q); 3426effc713SDoug Rabson } else { 343fb59c426SYoshinobu Inoue LIST_INSERT_AFTER(p, te, tqe_q); 3446effc713SDoug Rabson } 345df8bae1dSRodney W. Grimes 346df8bae1dSRodney W. Grimes present: 347df8bae1dSRodney W. Grimes /* 348df8bae1dSRodney W. Grimes * Present data to user, advancing rcv_nxt through 349df8bae1dSRodney W. Grimes * completed sequence space. 350df8bae1dSRodney W. Grimes */ 351a0292f23SGarrett Wollman if (!TCPS_HAVEESTABLISHED(tp->t_state)) 352df8bae1dSRodney W. Grimes return (0); 353fb59c426SYoshinobu Inoue q = LIST_FIRST(&tp->t_segq); 354fb59c426SYoshinobu Inoue if (!q || q->tqe_th->th_seq != tp->rcv_nxt) 355df8bae1dSRodney W. Grimes return (0); 356df8bae1dSRodney W. Grimes do { 357fb59c426SYoshinobu Inoue tp->rcv_nxt += q->tqe_len; 358fb59c426SYoshinobu Inoue flags = q->tqe_th->th_flags & TH_FIN; 359fb59c426SYoshinobu Inoue nq = LIST_NEXT(q, tqe_q); 360fb59c426SYoshinobu Inoue LIST_REMOVE(q, tqe_q); 361c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 362fb59c426SYoshinobu Inoue m_freem(q->tqe_m); 3634cc20ab1SSeigo Tanimura else 364395bb186SSam Leffler sbappendstream(&so->so_rcv, q->tqe_m); 36512e2e970SAndre Oppermann uma_zfree(tcp_reass_zone, q); 36612e2e970SAndre Oppermann tp->t_segqlen--; 36712e2e970SAndre Oppermann tcp_reass_qsize--; 3686effc713SDoug Rabson q = nq; 369fb59c426SYoshinobu Inoue } while (q && q->tqe_th->th_seq == tp->rcv_nxt); 370fb59c426SYoshinobu Inoue ND6_HINT(tp); 371df8bae1dSRodney W. Grimes sorwakeup(so); 372df8bae1dSRodney W. Grimes return (flags); 373df8bae1dSRodney W. Grimes } 374df8bae1dSRodney W. Grimes 375df8bae1dSRodney W. Grimes /* 376df8bae1dSRodney W. Grimes * TCP input routine, follows pages 65-76 of the 377df8bae1dSRodney W. Grimes * protocol specification dated September, 1981 very closely. 378df8bae1dSRodney W. Grimes */ 379fb59c426SYoshinobu Inoue #ifdef INET6 380fb59c426SYoshinobu Inoue int 381fb59c426SYoshinobu Inoue tcp6_input(mp, offp, proto) 382fb59c426SYoshinobu Inoue struct mbuf **mp; 383fb59c426SYoshinobu Inoue int *offp, proto; 384fb59c426SYoshinobu Inoue { 385fb59c426SYoshinobu Inoue register struct mbuf *m = *mp; 38633841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 387fb59c426SYoshinobu Inoue 388fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 389fb59c426SYoshinobu Inoue 390fb59c426SYoshinobu Inoue /* 391fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 392fb59c426SYoshinobu Inoue * better place to put this in? 393fb59c426SYoshinobu Inoue */ 39433841545SHajimu UMEMOTO ia6 = ip6_getdstifaddr(m); 39533841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 396fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 397fb59c426SYoshinobu Inoue 398fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 399fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 400fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 401fb59c426SYoshinobu Inoue return IPPROTO_DONE; 402fb59c426SYoshinobu Inoue } 403fb59c426SYoshinobu Inoue 404f0ffb944SJulian Elischer tcp_input(m, *offp); 405fb59c426SYoshinobu Inoue return IPPROTO_DONE; 406fb59c426SYoshinobu Inoue } 407fb59c426SYoshinobu Inoue #endif 408fb59c426SYoshinobu Inoue 409df8bae1dSRodney W. Grimes void 410f0ffb944SJulian Elischer tcp_input(m, off0) 411df8bae1dSRodney W. Grimes register struct mbuf *m; 412f0ffb944SJulian Elischer int off0; 413df8bae1dSRodney W. Grimes { 414fb59c426SYoshinobu Inoue register struct tcphdr *th; 415fb59c426SYoshinobu Inoue register struct ip *ip = NULL; 416fb59c426SYoshinobu Inoue register struct ipovly *ipov; 417f76fcf6dSJeffrey Hsu register struct inpcb *inp = NULL; 418e79adb8eSGarrett Wollman u_char *optp = NULL; 41926f9a767SRodney W. Grimes int optlen = 0; 420a0194ef1SBruce M Simpson int len, tlen, off; 421fb59c426SYoshinobu Inoue int drop_hdrlen; 422df8bae1dSRodney W. Grimes register struct tcpcb *tp = 0; 423fb59c426SYoshinobu Inoue register int thflags; 42426f9a767SRodney W. Grimes struct socket *so = 0; 425df8bae1dSRodney W. Grimes int todrop, acked, ourfinisacked, needoutput = 0; 426a0292f23SGarrett Wollman u_long tiwin; 427a0292f23SGarrett Wollman struct tcpopt to; /* options in this segment */ 42897d8d152SAndre Oppermann struct rmxp_tao tao; /* our TAO cache entry */ 429f76fcf6dSJeffrey Hsu int headlocked = 0; 43036e8826fSMax Laier struct sockaddr_in *next_hop = NULL; 431c068736aSJeffrey Hsu int rstreason; /* For badport_bandlim accounting purposes */ 432c068736aSJeffrey Hsu 433c068736aSJeffrey Hsu struct ip6_hdr *ip6 = NULL; 434c068736aSJeffrey Hsu #ifdef INET6 435c068736aSJeffrey Hsu int isipv6; 436c068736aSJeffrey Hsu #else 437c068736aSJeffrey Hsu const int isipv6 = 0; 438c068736aSJeffrey Hsu #endif 439f76fcf6dSJeffrey Hsu 440610ee2f9SDavid Greenman #ifdef TCPDEBUG 441c068736aSJeffrey Hsu /* 442c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 443c068736aSJeffrey Hsu * now IPv6. 444c068736aSJeffrey Hsu */ 445410bb1bfSLuigi Rizzo u_char tcp_saveipgen[40]; 446410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 447610ee2f9SDavid Greenman short ostate = 0; 448610ee2f9SDavid Greenman #endif 449c068736aSJeffrey Hsu 450ac9d7e26SMax Laier /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */ 4512f3f1e67SDarren Reed next_hop = m_claim_next(m, PACKET_TAG_IPFORWARD); 452fb59c426SYoshinobu Inoue #ifdef INET6 453fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 454fb59c426SYoshinobu Inoue #endif 45597d8d152SAndre Oppermann bzero(&tao, sizeof(tao)); 456a0292f23SGarrett Wollman bzero((char *)&to, sizeof(to)); 457a0292f23SGarrett Wollman 458df8bae1dSRodney W. Grimes tcpstat.tcps_rcvtotal++; 459fb59c426SYoshinobu Inoue 460fb59c426SYoshinobu Inoue if (isipv6) { 46104d3a452SHajimu UMEMOTO #ifdef INET6 462fb59c426SYoshinobu Inoue /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */ 463fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 464fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 465fb59c426SYoshinobu Inoue if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 466fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbadsum++; 467fb59c426SYoshinobu Inoue goto drop; 468fb59c426SYoshinobu Inoue } 469fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 47033841545SHajimu UMEMOTO 47133841545SHajimu UMEMOTO /* 47233841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 47333841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 47433841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 47533841545SHajimu UMEMOTO * 47633841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 47733841545SHajimu UMEMOTO * already dropped in ip6_input. 47833841545SHajimu UMEMOTO */ 47933841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 48033841545SHajimu UMEMOTO /* XXX stat */ 48133841545SHajimu UMEMOTO goto drop; 48233841545SHajimu UMEMOTO } 48304d3a452SHajimu UMEMOTO #else 48404d3a452SHajimu UMEMOTO th = NULL; /* XXX: avoid compiler warning */ 48504d3a452SHajimu UMEMOTO #endif 486c068736aSJeffrey Hsu } else { 487df8bae1dSRodney W. Grimes /* 488df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 489df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 490df8bae1dSRodney W. Grimes */ 491fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 492df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 493fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 494fb59c426SYoshinobu Inoue } 495df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 496df8bae1dSRodney W. Grimes if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 497df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 498df8bae1dSRodney W. Grimes return; 499df8bae1dSRodney W. Grimes } 500df8bae1dSRodney W. Grimes } 501fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 502fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 503db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 504db4f9cc7SJonathan Lemon tlen = ip->ip_len; 505df8bae1dSRodney W. Grimes 506db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 507db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 508db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 509db4f9cc7SJonathan Lemon else 510db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 511c068736aSJeffrey Hsu ip->ip_dst.s_addr, 512c068736aSJeffrey Hsu htonl(m->m_pkthdr.csum_data + 513c068736aSJeffrey Hsu ip->ip_len + 514c068736aSJeffrey Hsu IPPROTO_TCP)); 515db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 5163c653157SHartmut Brandt #ifdef TCPDEBUG 5173c653157SHartmut Brandt ipov->ih_len = (u_short)tlen; 5183c653157SHartmut Brandt ipov->ih_len = htons(ipov->ih_len); 5193c653157SHartmut Brandt #endif 520db4f9cc7SJonathan Lemon } else { 521df8bae1dSRodney W. Grimes /* 522df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 523df8bae1dSRodney W. Grimes */ 524df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 525fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 526fb59c426SYoshinobu Inoue ipov->ih_len = (u_short)tlen; 527fd8e4ebcSMike Barcroft ipov->ih_len = htons(ipov->ih_len); 528fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 529db4f9cc7SJonathan Lemon } 530fb59c426SYoshinobu Inoue if (th->th_sum) { 531df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadsum++; 532df8bae1dSRodney W. Grimes goto drop; 533df8bae1dSRodney W. Grimes } 534fb59c426SYoshinobu Inoue #ifdef INET6 535fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 536fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 537fb59c426SYoshinobu Inoue #endif 538fb59c426SYoshinobu Inoue } 539df8bae1dSRodney W. Grimes 540df8bae1dSRodney W. Grimes /* 541df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 542df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 543df8bae1dSRodney W. Grimes */ 544fb59c426SYoshinobu Inoue off = th->th_off << 2; 545df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 546df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadoff++; 547df8bae1dSRodney W. Grimes goto drop; 548df8bae1dSRodney W. Grimes } 549fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 550df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 551fb59c426SYoshinobu Inoue if (isipv6) { 55204d3a452SHajimu UMEMOTO #ifdef INET6 553fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 554fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 555fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 55604d3a452SHajimu UMEMOTO #endif 557c068736aSJeffrey Hsu } else { 558df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 559c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 560c068736aSJeffrey Hsu == 0) { 561df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 562df8bae1dSRodney W. Grimes return; 563df8bae1dSRodney W. Grimes } 564fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 565fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 566fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 567fb59c426SYoshinobu Inoue } 568df8bae1dSRodney W. Grimes } 569df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 570fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 571df8bae1dSRodney W. Grimes } 572fb59c426SYoshinobu Inoue thflags = th->th_flags; 573df8bae1dSRodney W. Grimes 574e46cd3d4SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN 575e46cd3d4SDag-Erling Smørgrav /* 576e46cd3d4SDag-Erling Smørgrav * If the drop_synfin option is enabled, drop all packets with 577e46cd3d4SDag-Erling Smørgrav * both the SYN and FIN bits set. This prevents e.g. nmap from 578e46cd3d4SDag-Erling Smørgrav * identifying the TCP/IP stack. 579e46cd3d4SDag-Erling Smørgrav * 580a589a70eSGarrett Wollman * This is a violation of the TCP specification. 581e46cd3d4SDag-Erling Smørgrav */ 582fb59c426SYoshinobu Inoue if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN)) 583e46cd3d4SDag-Erling Smørgrav goto drop; 584e46cd3d4SDag-Erling Smørgrav #endif 585e46cd3d4SDag-Erling Smørgrav 586df8bae1dSRodney W. Grimes /* 587df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 588df8bae1dSRodney W. Grimes */ 589fd8e4ebcSMike Barcroft th->th_seq = ntohl(th->th_seq); 590fd8e4ebcSMike Barcroft th->th_ack = ntohl(th->th_ack); 591fd8e4ebcSMike Barcroft th->th_win = ntohs(th->th_win); 592fd8e4ebcSMike Barcroft th->th_urp = ntohs(th->th_urp); 593df8bae1dSRodney W. Grimes 594df8bae1dSRodney W. Grimes /* 5953bfd6421SJonathan Lemon * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options, 596fb59c426SYoshinobu Inoue * until after ip6_savecontrol() is called and before other functions 597fb59c426SYoshinobu Inoue * which don't want those proto headers. 598fb59c426SYoshinobu Inoue * Because ip6_savecontrol() is going to parse the mbuf to 599fb59c426SYoshinobu Inoue * search for data to be passed up to user-land, it wants mbuf 600fb59c426SYoshinobu Inoue * parameters to be unchanged. 60188ff5695SSUZUKI Shinsuke * XXX: the call of ip6_savecontrol() has been obsoleted based on 60288ff5695SSUZUKI Shinsuke * latest version of the advanced API (20020110). 603755c1f07SAndras Olah */ 604fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 605755c1f07SAndras Olah 606755c1f07SAndras Olah /* 607df8bae1dSRodney W. Grimes * Locate pcb for segment. 608df8bae1dSRodney W. Grimes */ 609f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 610f76fcf6dSJeffrey Hsu headlocked = 1; 611df8bae1dSRodney W. Grimes findpcb: 6122b25acc1SLuigi Rizzo /* IPFIREWALL_FORWARD section */ 613c068736aSJeffrey Hsu if (next_hop != NULL && isipv6 == 0) { /* IPv6 support is not yet */ 614f9e354dfSJulian Elischer /* 6152b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 616f9e354dfSJulian Elischer * already got one like this? 617f9e354dfSJulian Elischer */ 618fb59c426SYoshinobu Inoue inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, 619c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 620c068736aSJeffrey Hsu 0, m->m_pkthdr.rcvif); 621f9e354dfSJulian Elischer if (!inp) { 622c068736aSJeffrey Hsu /* It's new. Try find the ambushing socket. */ 623f9e354dfSJulian Elischer inp = in_pcblookup_hash(&tcbinfo, 624fb59c426SYoshinobu Inoue ip->ip_src, th->th_sport, 6252b25acc1SLuigi Rizzo next_hop->sin_addr, 626c068736aSJeffrey Hsu next_hop->sin_port ? 627c068736aSJeffrey Hsu ntohs(next_hop->sin_port) : 628c068736aSJeffrey Hsu th->th_dport, 629c068736aSJeffrey Hsu 1, m->m_pkthdr.rcvif); 630f9e354dfSJulian Elischer } 631c068736aSJeffrey Hsu } else { 63204d3a452SHajimu UMEMOTO if (isipv6) { 63304d3a452SHajimu UMEMOTO #ifdef INET6 634c068736aSJeffrey Hsu inp = in6_pcblookup_hash(&tcbinfo, 635c068736aSJeffrey Hsu &ip6->ip6_src, th->th_sport, 636c068736aSJeffrey Hsu &ip6->ip6_dst, th->th_dport, 637c068736aSJeffrey Hsu 1, m->m_pkthdr.rcvif); 63804d3a452SHajimu UMEMOTO #endif 63904d3a452SHajimu UMEMOTO } else 640c068736aSJeffrey Hsu inp = in_pcblookup_hash(&tcbinfo, 641c068736aSJeffrey Hsu ip->ip_src, th->th_sport, 642c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 643c068736aSJeffrey Hsu 1, m->m_pkthdr.rcvif); 644fb59c426SYoshinobu Inoue } 645f9e354dfSJulian Elischer 646da0f4099SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC) 647d420fcdaSBruce M Simpson #ifdef INET6 648da0f4099SHajimu UMEMOTO if (isipv6) { 649da0f4099SHajimu UMEMOTO if (inp != NULL && ipsec6_in_reject(m, inp)) { 650fb59c426SYoshinobu Inoue #ifdef IPSEC 651fb59c426SYoshinobu Inoue ipsec6stat.in_polvio++; 652d420fcdaSBruce M Simpson #endif 653fb59c426SYoshinobu Inoue goto drop; 654fb59c426SYoshinobu Inoue } 655d420fcdaSBruce M Simpson } else 656d420fcdaSBruce M Simpson #endif /* INET6 */ 657d420fcdaSBruce M Simpson if (inp != NULL && ipsec4_in_reject(m, inp)) { 658da0f4099SHajimu UMEMOTO #ifdef IPSEC 659fb59c426SYoshinobu Inoue ipsecstat.in_polvio++; 660d420fcdaSBruce M Simpson #endif 661fb59c426SYoshinobu Inoue goto drop; 662fb59c426SYoshinobu Inoue } 663da0f4099SHajimu UMEMOTO #endif /*IPSEC || FAST_IPSEC*/ 664df8bae1dSRodney W. Grimes 665df8bae1dSRodney W. Grimes /* 666df8bae1dSRodney W. Grimes * If the state is CLOSED (i.e., TCB does not exist) then 667df8bae1dSRodney W. Grimes * all data in the incoming segment is discarded. 668df8bae1dSRodney W. Grimes * If the TCB exists but is in CLOSED state, it is embryonic, 669df8bae1dSRodney W. Grimes * but should either do a listen or a connect soon. 670df8bae1dSRodney W. Grimes */ 671816a3d83SPoul-Henning Kamp if (inp == NULL) { 6722e4e1b4cSGeoff Rehmet if (log_in_vain) { 673fb59c426SYoshinobu Inoue #ifdef INET6 674ded7008aSJuli Mallett char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2]; 675c068736aSJeffrey Hsu #else 676fb59c426SYoshinobu Inoue char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"]; 677c068736aSJeffrey Hsu #endif 67875cfc95fSAndrey A. Chernov 679fb59c426SYoshinobu Inoue if (isipv6) { 68004d3a452SHajimu UMEMOTO #ifdef INET6 681ded7008aSJuli Mallett strcpy(dbuf, "["); 682ded7008aSJuli Mallett strcpy(sbuf, "["); 683ded7008aSJuli Mallett strcat(dbuf, ip6_sprintf(&ip6->ip6_dst)); 684ded7008aSJuli Mallett strcat(sbuf, ip6_sprintf(&ip6->ip6_src)); 685ded7008aSJuli Mallett strcat(dbuf, "]"); 686ded7008aSJuli Mallett strcat(sbuf, "]"); 68704d3a452SHajimu UMEMOTO #endif 688c068736aSJeffrey Hsu } else { 689fb59c426SYoshinobu Inoue strcpy(dbuf, inet_ntoa(ip->ip_dst)); 690fb59c426SYoshinobu Inoue strcpy(sbuf, inet_ntoa(ip->ip_src)); 691fb59c426SYoshinobu Inoue } 6922e4e1b4cSGeoff Rehmet switch (log_in_vain) { 6932e4e1b4cSGeoff Rehmet case 1: 69439eb27a4SCrist J. Clark if ((thflags & TH_SYN) == 0) 6952e4e1b4cSGeoff Rehmet break; 696e4d2978dSPoul-Henning Kamp /* FALLTHROUGH */ 6972e4e1b4cSGeoff Rehmet case 2: 6982e4e1b4cSGeoff Rehmet log(LOG_INFO, 699c068736aSJeffrey Hsu "Connection attempt to TCP %s:%d " 70039eb27a4SCrist J. Clark "from %s:%d flags:0x%02x\n", 701fb59c426SYoshinobu Inoue dbuf, ntohs(th->th_dport), sbuf, 702fb59c426SYoshinobu Inoue ntohs(th->th_sport), thflags); 7032e4e1b4cSGeoff Rehmet break; 7042e4e1b4cSGeoff Rehmet default: 7052e4e1b4cSGeoff Rehmet break; 7062e4e1b4cSGeoff Rehmet } 70775cfc95fSAndrey A. Chernov } 7082e4e1b4cSGeoff Rehmet if (blackhole) { 7092e4e1b4cSGeoff Rehmet switch (blackhole) { 710828b7f40SGeoff Rehmet case 1: 711fb59c426SYoshinobu Inoue if (thflags & TH_SYN) 712828b7f40SGeoff Rehmet goto drop; 713828b7f40SGeoff Rehmet break; 714828b7f40SGeoff Rehmet case 2: 715828b7f40SGeoff Rehmet goto drop; 716828b7f40SGeoff Rehmet default: 717828b7f40SGeoff Rehmet goto drop; 7182e4e1b4cSGeoff Rehmet } 719828b7f40SGeoff Rehmet } 720a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 721a57815efSBosko Milekic goto dropwithreset; 722816a3d83SPoul-Henning Kamp } 723f76fcf6dSJeffrey Hsu INP_LOCK(inp); 724340c35deSJonathan Lemon if (inp->inp_vflag & INP_TIMEWAIT) { 725340c35deSJonathan Lemon /* 726340c35deSJonathan Lemon * The only option of relevance is TOF_CC, and only if 727340c35deSJonathan Lemon * present in a SYN segment. See tcp_timewait(). 728340c35deSJonathan Lemon */ 729340c35deSJonathan Lemon if (thflags & TH_SYN) 7306d90faf3SPaul Saab tcp_dooptions((struct tcpcb *)NULL, &to, optp, optlen, 1, th); 731340c35deSJonathan Lemon if (tcp_timewait((struct tcptw *)inp->inp_ppcb, 732340c35deSJonathan Lemon &to, th, m, tlen)) 733340c35deSJonathan Lemon goto findpcb; 734340c35deSJonathan Lemon /* 735340c35deSJonathan Lemon * tcp_timewait unlocks inp. 736340c35deSJonathan Lemon */ 737340c35deSJonathan Lemon INP_INFO_WUNLOCK(&tcbinfo); 738340c35deSJonathan Lemon return; 739340c35deSJonathan Lemon } 740df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 74109f81a46SBosko Milekic if (tp == 0) { 742f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 743a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 744a57815efSBosko Milekic goto dropwithreset; 74509f81a46SBosko Milekic } 746df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_CLOSED) 747df8bae1dSRodney W. Grimes goto drop; 748df8bae1dSRodney W. Grimes 749df8bae1dSRodney W. Grimes /* Unscale the window into a 32-bit value. */ 750fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) == 0) 751fb59c426SYoshinobu Inoue tiwin = th->th_win << tp->snd_scale; 752df8bae1dSRodney W. Grimes else 753fb59c426SYoshinobu Inoue tiwin = th->th_win; 754fb59c426SYoshinobu Inoue 755c488362eSRobert Watson #ifdef MAC 7561f82efb3SRobert Watson INP_LOCK_ASSERT(inp); 757a557af22SRobert Watson if (mac_check_inpcb_deliver(inp, m)) 758c488362eSRobert Watson goto drop; 759c488362eSRobert Watson #endif 760a557af22SRobert Watson so = inp->inp_socket; 761610ee2f9SDavid Greenman #ifdef TCPDEBUG 762df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 763df8bae1dSRodney W. Grimes ostate = tp->t_state; 764fb59c426SYoshinobu Inoue if (isipv6) 765540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 766fb59c426SYoshinobu Inoue else 767540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 768fb59c426SYoshinobu Inoue tcp_savetcp = *th; 769df8bae1dSRodney W. Grimes } 770610ee2f9SDavid Greenman #endif 771540e8b7eSJeffrey Hsu if (so->so_options & SO_ACCEPTCONN) { 772540e8b7eSJeffrey Hsu struct in_conninfo inc; 773540e8b7eSJeffrey Hsu 774fb59c426SYoshinobu Inoue #ifdef INET6 775be2ac88cSJonathan Lemon inc.inc_isipv6 = isipv6; 776c068736aSJeffrey Hsu #endif 777be2ac88cSJonathan Lemon if (isipv6) { 778be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 779be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 780c068736aSJeffrey Hsu } else { 781be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 782be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 783be2ac88cSJonathan Lemon } 784be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 785be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 786be2ac88cSJonathan Lemon 787fb59c426SYoshinobu Inoue /* 788be2ac88cSJonathan Lemon * If the state is LISTEN then ignore segment if it contains 789be2ac88cSJonathan Lemon * a RST. If the segment contains an ACK then it is bad and 790be2ac88cSJonathan Lemon * send a RST. If it does not contain a SYN then it is not 791be2ac88cSJonathan Lemon * interesting; drop it. 792be2ac88cSJonathan Lemon * 793be2ac88cSJonathan Lemon * If the state is SYN_RECEIVED (syncache) and seg contains 794be2ac88cSJonathan Lemon * an ACK, but not for our SYN/ACK, send a RST. If the seg 795be2ac88cSJonathan Lemon * contains a RST, check the sequence number to see if it 796be2ac88cSJonathan Lemon * is a valid reset segment. 797fb59c426SYoshinobu Inoue */ 798fb59c426SYoshinobu Inoue if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 799be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 800be2ac88cSJonathan Lemon if (!syncache_expand(&inc, th, &so, m)) { 8014195b4afSPaul Traina /* 802be2ac88cSJonathan Lemon * No syncache entry, or ACK was not 803be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 8044195b4afSPaul Traina */ 805be2ac88cSJonathan Lemon tcpstat.tcps_badsyn++; 806be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 807be2ac88cSJonathan Lemon goto dropwithreset; 808be2ac88cSJonathan Lemon } 809f76fcf6dSJeffrey Hsu if (so == NULL) { 810be2ac88cSJonathan Lemon /* 811be2ac88cSJonathan Lemon * Could not complete 3-way handshake, 812be2ac88cSJonathan Lemon * connection is being closed down, and 813be2ac88cSJonathan Lemon * syncache will free mbuf. 814be2ac88cSJonathan Lemon */ 815f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 816f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 817be2ac88cSJonathan Lemon return; 818f76fcf6dSJeffrey Hsu } 819be2ac88cSJonathan Lemon /* 820be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 821be2ac88cSJonathan Lemon * Continue processing segment. 822be2ac88cSJonathan Lemon */ 823f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 824be2ac88cSJonathan Lemon inp = sotoinpcb(so); 825f76fcf6dSJeffrey Hsu INP_LOCK(inp); 826be2ac88cSJonathan Lemon tp = intotcpcb(inp); 827be2ac88cSJonathan Lemon /* 828be2ac88cSJonathan Lemon * This is what would have happened in 829e6658b12SRobert Watson * tcp_output() when the SYN,ACK was sent. 830be2ac88cSJonathan Lemon */ 831be2ac88cSJonathan Lemon tp->snd_up = tp->snd_una; 832be2ac88cSJonathan Lemon tp->snd_max = tp->snd_nxt = tp->iss + 1; 833be2ac88cSJonathan Lemon tp->last_ack_sent = tp->rcv_nxt; 834be2ac88cSJonathan Lemon /* 83541446225SJonathan Lemon * RFC1323: The window in SYN & SYN/ACK 83641446225SJonathan Lemon * segments is never scaled. 837be2ac88cSJonathan Lemon */ 838be2ac88cSJonathan Lemon tp->snd_wnd = tiwin; /* unscaled */ 839be2ac88cSJonathan Lemon goto after_listen; 840be2ac88cSJonathan Lemon } 841be2ac88cSJonathan Lemon if (thflags & TH_RST) { 842be2ac88cSJonathan Lemon syncache_chkrst(&inc, th); 843be2ac88cSJonathan Lemon goto drop; 844be2ac88cSJonathan Lemon } 845fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 846be2ac88cSJonathan Lemon syncache_badack(&inc); 847ebb0cbeaSPaul Traina tcpstat.tcps_badsyn++; 848a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 849a57815efSBosko Milekic goto dropwithreset; 8504195b4afSPaul Traina } 851ebb0cbeaSPaul Traina goto drop; 852ebb0cbeaSPaul Traina } 85333841545SHajimu UMEMOTO 854be2ac88cSJonathan Lemon /* 855be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 856be2ac88cSJonathan Lemon */ 85733841545SHajimu UMEMOTO #ifdef INET6 85833841545SHajimu UMEMOTO /* 85933841545SHajimu UMEMOTO * If deprecated address is forbidden, 86033841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 86133841545SHajimu UMEMOTO * address to prevent any new inbound connection from 86233841545SHajimu UMEMOTO * getting established. 86333841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 86433841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 86533841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 86633841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 86733841545SHajimu UMEMOTO * for the exchange. 86833841545SHajimu UMEMOTO * 86933841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 87033841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 87133841545SHajimu UMEMOTO * SYN in this case. 87233841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 87333841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 87433841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 87533841545SHajimu UMEMOTO * used" 87633841545SHajimu UMEMOTO * 2. use of it with new communication: 87733841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 87833841545SHajimu UMEMOTO * with sufficient scope is available" 87933841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 88033841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 88133841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 88233841545SHajimu UMEMOTO * 88333841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 88433841545SHajimu UMEMOTO * multiple description text for deprecated address 88533841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 88633841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 88733841545SHajimu UMEMOTO */ 88833841545SHajimu UMEMOTO if (isipv6 && !ip6_use_deprecated) { 88933841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 89033841545SHajimu UMEMOTO 89133841545SHajimu UMEMOTO if ((ia6 = ip6_getdstifaddr(m)) && 89233841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 893f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 89433841545SHajimu UMEMOTO tp = NULL; 89533841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 89633841545SHajimu UMEMOTO goto dropwithreset; 89733841545SHajimu UMEMOTO } 89833841545SHajimu UMEMOTO } 89933841545SHajimu UMEMOTO #endif 90065f28919SJesper Skriver /* 901be2ac88cSJonathan Lemon * If it is from this socket, drop it, it must be forged. 902be2ac88cSJonathan Lemon * Don't bother responding if the destination was a broadcast. 90365f28919SJesper Skriver */ 904be2ac88cSJonathan Lemon if (th->th_dport == th->th_sport) { 905fb59c426SYoshinobu Inoue if (isipv6) { 906be2ac88cSJonathan Lemon if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 907be2ac88cSJonathan Lemon &ip6->ip6_src)) 908be2ac88cSJonathan Lemon goto drop; 909c068736aSJeffrey Hsu } else { 910be2ac88cSJonathan Lemon if (ip->ip_dst.s_addr == ip->ip_src.s_addr) 911be2ac88cSJonathan Lemon goto drop; 912be2ac88cSJonathan Lemon } 913c068736aSJeffrey Hsu } 914be2ac88cSJonathan Lemon /* 915be2ac88cSJonathan Lemon * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 9162ca2159fSCrist J. Clark * 91793ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 91893ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 91993ec91baSCrist J. Clark * in_broadcast() to find them. 920be2ac88cSJonathan Lemon */ 921be2ac88cSJonathan Lemon if (m->m_flags & (M_BCAST|M_MCAST)) 922be2ac88cSJonathan Lemon goto drop; 923be2ac88cSJonathan Lemon if (isipv6) { 924be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 925be2ac88cSJonathan Lemon IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 926be2ac88cSJonathan Lemon goto drop; 927c068736aSJeffrey Hsu } else { 928be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 929be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 9302ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 9312ca2159fSCrist J. Clark in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 932be2ac88cSJonathan Lemon goto drop; 933c068736aSJeffrey Hsu } 934be2ac88cSJonathan Lemon /* 935be2ac88cSJonathan Lemon * SYN appears to be valid; create compressed TCP state 936be2ac88cSJonathan Lemon * for syncache, or perform t/tcp connection. 937be2ac88cSJonathan Lemon */ 938be2ac88cSJonathan Lemon if (so->so_qlen <= so->so_qlimit) { 9393c653157SHartmut Brandt #ifdef TCPDEBUG 9403c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 9413c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 9423c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 9433c653157SHartmut Brandt #endif 9446d90faf3SPaul Saab tcp_dooptions(tp, &to, optp, optlen, 1, th); 945be2ac88cSJonathan Lemon if (!syncache_add(&inc, &to, th, &so, m)) 946be2ac88cSJonathan Lemon goto drop; 947f76fcf6dSJeffrey Hsu if (so == NULL) { 948be2ac88cSJonathan Lemon /* 949be2ac88cSJonathan Lemon * Entry added to syncache, mbuf used to 950be2ac88cSJonathan Lemon * send SYN,ACK packet. 951be2ac88cSJonathan Lemon */ 952f76fcf6dSJeffrey Hsu KASSERT(headlocked, ("headlocked")); 953f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 954f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 955be2ac88cSJonathan Lemon return; 956f76fcf6dSJeffrey Hsu } 957be2ac88cSJonathan Lemon /* 958be2ac88cSJonathan Lemon * Segment passed TAO tests. 959be2ac88cSJonathan Lemon */ 960f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 961be2ac88cSJonathan Lemon inp = sotoinpcb(so); 962f76fcf6dSJeffrey Hsu INP_LOCK(inp); 963df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 964be2ac88cSJonathan Lemon tp->snd_wnd = tiwin; 965be2ac88cSJonathan Lemon tp->t_starttime = ticks; 966be2ac88cSJonathan Lemon tp->t_state = TCPS_ESTABLISHED; 967df8bae1dSRodney W. Grimes 968be2ac88cSJonathan Lemon /* 9693bfd6421SJonathan Lemon * T/TCP logic: 9703bfd6421SJonathan Lemon * If there is a FIN or if there is data, then 9713bfd6421SJonathan Lemon * delay SYN,ACK(SYN) in the hope of piggy-backing 9723bfd6421SJonathan Lemon * it on a response segment. Otherwise must send 9733bfd6421SJonathan Lemon * ACK now in case the other side is slow starting. 974be2ac88cSJonathan Lemon */ 9753bfd6421SJonathan Lemon if (thflags & TH_FIN || tlen != 0) 9763bfd6421SJonathan Lemon tp->t_flags |= (TF_DELACK | TF_NEEDSYN); 9773bfd6421SJonathan Lemon else 978f243998bSJonathan Lemon tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 979be2ac88cSJonathan Lemon tcpstat.tcps_connects++; 980be2ac88cSJonathan Lemon soisconnected(so); 981be2ac88cSJonathan Lemon goto trimthenstep6; 982df8bae1dSRodney W. Grimes } 983be2ac88cSJonathan Lemon goto drop; 9844cc20ab1SSeigo Tanimura } 985be2ac88cSJonathan Lemon after_listen: 986be2ac88cSJonathan Lemon 987be2ac88cSJonathan Lemon /* XXX temp debugging */ 988be2ac88cSJonathan Lemon /* should not happen - syncache should pick up these connections */ 989be2ac88cSJonathan Lemon if (tp->t_state == TCPS_LISTEN) 990be2ac88cSJonathan Lemon panic("tcp_input: TCPS_LISTEN"); 991df8bae1dSRodney W. Grimes 992df8bae1dSRodney W. Grimes /* 99353369ac9SAndre Oppermann * This is the second part of the MSS DoS prevention code (after 99453369ac9SAndre Oppermann * minmss on the sending side) and it deals with too many too small 99553369ac9SAndre Oppermann * tcp packets in a too short timeframe (1 second). 99653369ac9SAndre Oppermann * 99753369ac9SAndre Oppermann * For every full second we count the number of received packets 99853369ac9SAndre Oppermann * and bytes. If we get a lot of packets per second for this connection 99953369ac9SAndre Oppermann * (tcp_minmssoverload) we take a closer look at it and compute the 100053369ac9SAndre Oppermann * average packet size for the past second. If that is less than 100153369ac9SAndre Oppermann * tcp_minmss we get too many packets with very small payload which 100253369ac9SAndre Oppermann * is not good and burdens our system (and every packet generates 100353369ac9SAndre Oppermann * a wakeup to the process connected to our socket). We can reasonable 100453369ac9SAndre Oppermann * expect this to be small packet DoS attack to exhaust our CPU 100553369ac9SAndre Oppermann * cycles. 100653369ac9SAndre Oppermann * 100753369ac9SAndre Oppermann * Care has to be taken for the minimum packet overload value. This 100853369ac9SAndre Oppermann * value defines the minimum number of packets per second before we 100953369ac9SAndre Oppermann * start to worry. This must not be too low to avoid killing for 101053369ac9SAndre Oppermann * example interactive connections with many small packets like 101153369ac9SAndre Oppermann * telnet or SSH. 101253369ac9SAndre Oppermann * 101353369ac9SAndre Oppermann * Setting either tcp_minmssoverload or tcp_minmss to "0" disables 101453369ac9SAndre Oppermann * this check. 101553369ac9SAndre Oppermann * 101653369ac9SAndre Oppermann * Account for packet if payload packet, skip over ACK, etc. 101753369ac9SAndre Oppermann */ 101853369ac9SAndre Oppermann if (tcp_minmss && tcp_minmssoverload && 101953369ac9SAndre Oppermann tp->t_state == TCPS_ESTABLISHED && tlen > 0) { 102053369ac9SAndre Oppermann if (tp->rcv_second > ticks) { 102153369ac9SAndre Oppermann tp->rcv_pps++; 102253369ac9SAndre Oppermann tp->rcv_byps += tlen + off; 102353369ac9SAndre Oppermann if (tp->rcv_pps > tcp_minmssoverload) { 102453369ac9SAndre Oppermann if ((tp->rcv_byps / tp->rcv_pps) < tcp_minmss) { 102553369ac9SAndre Oppermann printf("too many small tcp packets from " 102653369ac9SAndre Oppermann "%s:%u, av. %lubyte/packet, " 102753369ac9SAndre Oppermann "dropping connection\n", 102853369ac9SAndre Oppermann #ifdef INET6 102953369ac9SAndre Oppermann isipv6 ? 103053369ac9SAndre Oppermann ip6_sprintf(&inp->inp_inc.inc6_faddr) : 103153369ac9SAndre Oppermann #endif 103253369ac9SAndre Oppermann inet_ntoa(inp->inp_inc.inc_faddr), 103353369ac9SAndre Oppermann inp->inp_inc.inc_fport, 103453369ac9SAndre Oppermann tp->rcv_byps / tp->rcv_pps); 103553369ac9SAndre Oppermann tp = tcp_drop(tp, ECONNRESET); 103653369ac9SAndre Oppermann tcpstat.tcps_minmssdrops++; 103753369ac9SAndre Oppermann goto drop; 103853369ac9SAndre Oppermann } 103953369ac9SAndre Oppermann } 104053369ac9SAndre Oppermann } else { 104153369ac9SAndre Oppermann tp->rcv_second = ticks + hz; 104253369ac9SAndre Oppermann tp->rcv_pps = 1; 104353369ac9SAndre Oppermann tp->rcv_byps = tlen + off; 104453369ac9SAndre Oppermann } 104553369ac9SAndre Oppermann } 104653369ac9SAndre Oppermann 104753369ac9SAndre Oppermann /* 1048df8bae1dSRodney W. Grimes * Segment received on connection. 1049df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1050df8bae1dSRodney W. Grimes */ 10519b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 10527ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 10539b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp); 1054df8bae1dSRodney W. Grimes 1055df8bae1dSRodney W. Grimes /* 105697d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 105797d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 105897d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1059df8bae1dSRodney W. Grimes */ 10606d90faf3SPaul Saab tcp_dooptions(tp,&to, optp, optlen, thflags & TH_SYN,th); 1061be2ac88cSJonathan Lemon if (thflags & TH_SYN) { 1062be2ac88cSJonathan Lemon if (to.to_flags & TOF_SCALE) { 1063be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 1064be2ac88cSJonathan Lemon tp->requested_s_scale = to.to_requested_s_scale; 1065be2ac88cSJonathan Lemon } 1066be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1067be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1068be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1069be2ac88cSJonathan Lemon tp->ts_recent_age = ticks; 1070be2ac88cSJonathan Lemon } 1071be2ac88cSJonathan Lemon if (to.to_flags & (TOF_CC|TOF_CCNEW)) 1072be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_CC; 1073be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1074be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 10756d90faf3SPaul Saab if (tp->sack_enable) { 10766d90faf3SPaul Saab if (!(to.to_flags & TOF_SACK)) 10776d90faf3SPaul Saab tp->sack_enable = 0; 10786d90faf3SPaul Saab else 10796d90faf3SPaul Saab tp->t_flags |= TF_SACK_PERMIT; 10806d90faf3SPaul Saab } 10816d90faf3SPaul Saab 10826d90faf3SPaul Saab } 10836d90faf3SPaul Saab 10846d90faf3SPaul Saab if (tp->sack_enable) { 10856d90faf3SPaul Saab /* Delete stale (cumulatively acked) SACK holes */ 10866d90faf3SPaul Saab tcp_del_sackholes(tp, th); 10876d90faf3SPaul Saab tp->rcv_laststart = th->th_seq; /* last rec'vd segment*/ 10886d90faf3SPaul Saab tp->rcv_lastend = th->th_seq + tlen; 1089be2ac88cSJonathan Lemon } 1090df8bae1dSRodney W. Grimes 1091df8bae1dSRodney W. Grimes /* 1092df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1093df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1094df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1095df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1096df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1097df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1098df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1099df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1100df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1101df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1102df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1103df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1104a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1105a0292f23SGarrett Wollman * Since we check for TCPS_ESTABLISHED above, it can only 1106a0292f23SGarrett Wollman * be TH_NEEDSYN. 1107df8bae1dSRodney W. Grimes */ 1108df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1109fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1110a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1111be2ac88cSJonathan Lemon ((to.to_flags & TOF_TS) == 0 || 1112a0292f23SGarrett Wollman TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 1113a0292f23SGarrett Wollman /* 1114a0292f23SGarrett Wollman * Using the CC option is compulsory if once started: 1115a0292f23SGarrett Wollman * the segment is OK if no T/TCP was negotiated or 1116a0292f23SGarrett Wollman * if the segment has a CC option equal to CCrecv 1117a0292f23SGarrett Wollman */ 1118a0292f23SGarrett Wollman ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) || 1119be2ac88cSJonathan Lemon ((to.to_flags & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) && 1120fb59c426SYoshinobu Inoue th->th_seq == tp->rcv_nxt && 1121df8bae1dSRodney W. Grimes tiwin && tiwin == tp->snd_wnd && 1122df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) { 1123df8bae1dSRodney W. Grimes 1124df8bae1dSRodney W. Grimes /* 1125df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1126df8bae1dSRodney W. Grimes * record the timestamp. 1127a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1128a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1129df8bae1dSRodney W. Grimes */ 1130be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1131fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 11329b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1133a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1134df8bae1dSRodney W. Grimes } 1135df8bae1dSRodney W. Grimes 1136fb59c426SYoshinobu Inoue if (tlen == 0) { 1137fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1138fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1139233e8c18SGarrett Wollman tp->snd_cwnd >= tp->snd_wnd && 11406d90faf3SPaul Saab ((!tcp_do_newreno && !tp->sack_enable && 1141cb942153SJeffrey Hsu tp->t_dupacks < tcprexmtthresh) || 11426d90faf3SPaul Saab ((tcp_do_newreno || tp->sack_enable) && 11436d90faf3SPaul Saab !IN_FASTRECOVERY(tp)))) { 1144f76fcf6dSJeffrey Hsu KASSERT(headlocked, ("headlocked")); 1145f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 1146df8bae1dSRodney W. Grimes /* 1147df8bae1dSRodney W. Grimes * this is a pure ack for outstanding data. 1148df8bae1dSRodney W. Grimes */ 1149df8bae1dSRodney W. Grimes ++tcpstat.tcps_predack; 11509b8b58e0SJonathan Lemon /* 11519b8b58e0SJonathan Lemon * "bad retransmit" recovery 11529b8b58e0SJonathan Lemon */ 11539b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 11549b8b58e0SJonathan Lemon ticks < tp->t_badrxtwin) { 1155cb942153SJeffrey Hsu ++tcpstat.tcps_sndrexmitbad; 11569b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 11579b8b58e0SJonathan Lemon tp->snd_ssthresh = 11589b8b58e0SJonathan Lemon tp->snd_ssthresh_prev; 11599d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 11609d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 11619d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 11629b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 11639b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; 11649b8b58e0SJonathan Lemon } 1165fa55172bSMatthew Dillon 1166fa55172bSMatthew Dillon /* 1167fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1168fa55172bSMatthew Dillon * 1169fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1170fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1171fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1172fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1173fa55172bSMatthew Dillon */ 1174fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1175fa55172bSMatthew Dillon to.to_tsecr) { 1176a0292f23SGarrett Wollman tcp_xmit_timer(tp, 11779b8b58e0SJonathan Lemon ticks - to.to_tsecr + 1); 1178fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1179fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1180c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1181c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1182fa55172bSMatthew Dillon } 11831fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 1184fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 1185df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 1186df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 1187df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 11889d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 11899d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 11909d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 11919d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 11921645d090SJeff Roberson /* 11931645d090SJeff Roberson * pull snd_wl2 up to prevent seq wrap relative 11941645d090SJeff Roberson * to th_ack. 11951645d090SJeff Roberson */ 1196cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1197b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1198df8bae1dSRodney W. Grimes m_freem(m); 1199fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 1200df8bae1dSRodney W. Grimes 1201df8bae1dSRodney W. Grimes /* 1202df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1203df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1204df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1205df8bae1dSRodney W. Grimes * If process is waiting for space, 1206df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1207df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1208df8bae1dSRodney W. Grimes * decide between more output or persist. 12093c653157SHartmut Brandt 12103c653157SHartmut Brandt #ifdef TCPDEBUG 12113c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 12123c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 12133c653157SHartmut Brandt (void *)tcp_saveipgen, 12143c653157SHartmut Brandt &tcp_savetcp, 0); 12153c653157SHartmut Brandt #endif 1216df8bae1dSRodney W. Grimes */ 1217df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 12189b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 12199b8b58e0SJonathan Lemon else if (!callout_active(tp->tt_persist)) 12209b8b58e0SJonathan Lemon callout_reset(tp->tt_rexmt, 12219b8b58e0SJonathan Lemon tp->t_rxtcur, 12229b8b58e0SJonathan Lemon tcp_timer_rexmt, tp); 1223df8bae1dSRodney W. Grimes 1224df8bae1dSRodney W. Grimes sowwakeup(so); 1225df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 1226df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1227a14c749fSJonathan Lemon goto check_delack; 1228df8bae1dSRodney W. Grimes } 1229fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1230fb59c426SYoshinobu Inoue LIST_EMPTY(&tp->t_segq) && 1231fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 1232f76fcf6dSJeffrey Hsu KASSERT(headlocked, ("headlocked")); 1233f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 1234df8bae1dSRodney W. Grimes /* 1235df8bae1dSRodney W. Grimes * this is a pure, in-sequence data packet 1236df8bae1dSRodney W. Grimes * with nothing on the reassembly queue and 1237df8bae1dSRodney W. Grimes * we have enough buffer space to take it. 1238df8bae1dSRodney W. Grimes */ 12396d90faf3SPaul Saab /* Clean receiver SACK report if present */ 12406d90faf3SPaul Saab if (tp->sack_enable && tp->rcv_numsacks) 12416d90faf3SPaul Saab tcp_clean_sackreport(tp); 1242df8bae1dSRodney W. Grimes ++tcpstat.tcps_preddat; 1243fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 12441645d090SJeff Roberson /* 12451645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 12461645d090SJeff Roberson * th_seq. 12471645d090SJeff Roberson */ 12481645d090SJeff Roberson tp->snd_wl1 = th->th_seq; 12491645d090SJeff Roberson /* 12501645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 12511645d090SJeff Roberson * rcv_nxt. 12521645d090SJeff Roberson */ 12531645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 1254df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpack++; 1255fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyte += tlen; 1256fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 1257df8bae1dSRodney W. Grimes /* 12583c653157SHartmut Brandt #ifdef TCPDEBUG 12593c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 12603c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 12613c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 12623c653157SHartmut Brandt #endif 1263755c1f07SAndras Olah * Add data to socket buffer. 1264df8bae1dSRodney W. Grimes */ 1265c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1266c1c36a2cSMike Silbersack m_freem(m); 1267c1c36a2cSMike Silbersack } else { 1268fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 1269395bb186SSam Leffler sbappendstream(&so->so_rcv, m); 1270c1c36a2cSMike Silbersack } 1271df8bae1dSRodney W. Grimes sorwakeup(so); 1272d8c85a26SJonathan Lemon if (DELAY_ACK(tp)) { 12733bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1274f498eeeeSDavid Greenman } else { 1275e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 1276e612a582SDavid Greenman tcp_output(tp); 1277e612a582SDavid Greenman } 1278a14c749fSJonathan Lemon goto check_delack; 1279df8bae1dSRodney W. Grimes } 1280df8bae1dSRodney W. Grimes } 1281df8bae1dSRodney W. Grimes 1282df8bae1dSRodney W. Grimes /* 1283df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1284df8bae1dSRodney W. Grimes * and then do TCP input processing. 1285df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1286df8bae1dSRodney W. Grimes * but not less than advertised window. 1287df8bae1dSRodney W. Grimes */ 1288df8bae1dSRodney W. Grimes { int win; 1289df8bae1dSRodney W. Grimes 1290df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1291df8bae1dSRodney W. Grimes if (win < 0) 1292df8bae1dSRodney W. Grimes win = 0; 129366e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1294df8bae1dSRodney W. Grimes } 1295df8bae1dSRodney W. Grimes 1296df8bae1dSRodney W. Grimes switch (tp->t_state) { 1297df8bae1dSRodney W. Grimes 1298df8bae1dSRodney W. Grimes /* 1299764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1300764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1301764d8cefSBill Fenner */ 1302764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1303fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1304fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1305a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1306a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1307a57815efSBosko Milekic goto dropwithreset; 1308a57815efSBosko Milekic } 1309764d8cefSBill Fenner break; 1310764d8cefSBill Fenner 1311764d8cefSBill Fenner /* 1312df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1313df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1314df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1315df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1316df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1317df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1318df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1319df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1320df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1321df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1322df8bae1dSRodney W. Grimes */ 1323df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 132497d8d152SAndre Oppermann if (tcp_do_rfc1644) 132597d8d152SAndre Oppermann tcp_hc_gettao(&inp->inp_inc, &tao); 1326a0292f23SGarrett Wollman 1327fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1328fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1329fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1330a0292f23SGarrett Wollman /* 1331a0292f23SGarrett Wollman * If we have a cached CCsent for the remote host, 1332a0292f23SGarrett Wollman * hence we haven't just crashed and restarted, 1333a0292f23SGarrett Wollman * do not send a RST. This may be a retransmission 1334a0292f23SGarrett Wollman * from the other side after our earlier ACK was lost. 1335a0292f23SGarrett Wollman * Our new SYN, when it arrives, will serve as the 1336a0292f23SGarrett Wollman * needed ACK. 1337a0292f23SGarrett Wollman */ 133897d8d152SAndre Oppermann if (tao.tao_ccsent != 0) 1339a0292f23SGarrett Wollman goto drop; 1340a57815efSBosko Milekic else { 1341a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1342a0292f23SGarrett Wollman goto dropwithreset; 1343a0292f23SGarrett Wollman } 1344a57815efSBosko Milekic } 1345fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 1346fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1347df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 1348df8bae1dSRodney W. Grimes goto drop; 1349df8bae1dSRodney W. Grimes } 1350fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) == 0) 1351df8bae1dSRodney W. Grimes goto drop; 1352fb59c426SYoshinobu Inoue tp->snd_wnd = th->th_win; /* initial send window */ 1353a0292f23SGarrett Wollman tp->cc_recv = to.to_cc; /* foreign CC */ 1354a0292f23SGarrett Wollman 1355fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1356df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1357fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1358a0292f23SGarrett Wollman /* 1359a0292f23SGarrett Wollman * Our SYN was acked. If segment contains CC.ECHO 1360a0292f23SGarrett Wollman * option, check it to make sure this segment really 1361a0292f23SGarrett Wollman * matches our SYN. If not, just drop it as old 1362a0292f23SGarrett Wollman * duplicate, but send an RST if we're still playing 1363026650e5SBill Fenner * by the old rules. If no CC.ECHO option, make sure 1364026650e5SBill Fenner * we don't get fooled into using T/TCP. 1365a0292f23SGarrett Wollman */ 1366be2ac88cSJonathan Lemon if (to.to_flags & TOF_CCECHO) { 1367dfd5dee1SPeter Wemm if (tp->cc_send != to.to_ccecho) { 136897d8d152SAndre Oppermann if (tao.tao_ccsent != 0) 1369a0292f23SGarrett Wollman goto drop; 1370a57815efSBosko Milekic else { 1371a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1372a0292f23SGarrett Wollman goto dropwithreset; 1373dfd5dee1SPeter Wemm } 1374a57815efSBosko Milekic } 1375026650e5SBill Fenner } else 1376026650e5SBill Fenner tp->t_flags &= ~TF_RCVD_CC; 1377845799c1SAndras Olah tcpstat.tcps_connects++; 1378845799c1SAndras Olah soisconnected(so); 1379c488362eSRobert Watson #ifdef MAC 1380310e7cebSRobert Watson SOCK_LOCK(so); 1381c488362eSRobert Watson mac_set_socket_peer_from_mbuf(m, so); 1382310e7cebSRobert Watson SOCK_UNLOCK(so); 1383c488362eSRobert Watson #endif 1384845799c1SAndras Olah /* Do window scaling on this connection? */ 1385845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1386845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1387845799c1SAndras Olah tp->snd_scale = tp->requested_s_scale; 1388845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1389845799c1SAndras Olah } 1390a0292f23SGarrett Wollman /* Segment is acceptable, update cache if undefined. */ 139197d8d152SAndre Oppermann if (tao.tao_ccsent == 0 && tcp_do_rfc1644) 139297d8d152SAndre Oppermann tcp_hc_updatetao(&inp->inp_inc, TCP_HC_TAO_CCSENT, to.to_ccecho, 0); 1393a0292f23SGarrett Wollman 1394a0292f23SGarrett Wollman tp->rcv_adv += tp->rcv_wnd; 1395a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1396a0292f23SGarrett Wollman /* 1397a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1398a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1399a0292f23SGarrett Wollman */ 1400d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && tlen != 0) 14019b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 14029b8b58e0SJonathan Lemon tcp_timer_delack, tp); 1403a0292f23SGarrett Wollman else 1404a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1405a0292f23SGarrett Wollman /* 1406a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1407a0292f23SGarrett Wollman * Transitions: 1408a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1409a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1410a0292f23SGarrett Wollman */ 14119b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1412a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1413a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1414a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1415fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 14167ff19458SPaul Traina } else { 1417a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 14189b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, 14199b8b58e0SJonathan Lemon tcp_timer_keep, tp); 14207ff19458SPaul Traina } 1421a0292f23SGarrett Wollman } else { 1422a0292f23SGarrett Wollman /* 1423c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 1424c068736aSJeffrey Hsu * simultaneous open. If segment contains CC option 1425c068736aSJeffrey Hsu * and there is a cached CC, apply TAO test. 1426c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 1427c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 1428a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1429a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1430a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1431a0292f23SGarrett Wollman */ 1432a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 14339b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 1434be2ac88cSJonathan Lemon if (to.to_flags & TOF_CC) { 143597d8d152SAndre Oppermann if (tao.tao_cc != 0 && 143697d8d152SAndre Oppermann CC_GT(to.to_cc, tao.tao_cc)) { 1437a0292f23SGarrett Wollman /* 1438a0292f23SGarrett Wollman * update cache and make transition: 1439a0292f23SGarrett Wollman * SYN-SENT -> ESTABLISHED* 1440a0292f23SGarrett Wollman * SYN-SENT* -> FIN-WAIT-1* 1441a0292f23SGarrett Wollman */ 144297d8d152SAndre Oppermann tao.tao_cc = to.to_cc; 144397d8d152SAndre Oppermann tcp_hc_updatetao(&inp->inp_inc, 144497d8d152SAndre Oppermann TCP_HC_TAO_CC, to.to_cc, 0); 14459b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1446a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1447a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1448a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 14497ff19458SPaul Traina } else { 1450a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 14519b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, 14529b8b58e0SJonathan Lemon tcp_keepidle, 14539b8b58e0SJonathan Lemon tcp_timer_keep, 14549b8b58e0SJonathan Lemon tp); 14557ff19458SPaul Traina } 1456a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDSYN; 1457df8bae1dSRodney W. Grimes } else 1458df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1459a0292f23SGarrett Wollman } else { 146097d8d152SAndre Oppermann if (tcp_do_rfc1644) { 1461a0292f23SGarrett Wollman /* CC.NEW or no option => invalidate cache */ 146297d8d152SAndre Oppermann tao.tao_cc = 0; 146397d8d152SAndre Oppermann tcp_hc_updatetao(&inp->inp_inc, 146497d8d152SAndre Oppermann TCP_HC_TAO_CC, to.to_cc, 0); 146597d8d152SAndre Oppermann } 1466a0292f23SGarrett Wollman tp->t_state = TCPS_SYN_RECEIVED; 1467a0292f23SGarrett Wollman } 1468a0292f23SGarrett Wollman } 1469df8bae1dSRodney W. Grimes 1470df8bae1dSRodney W. Grimes trimthenstep6: 1471df8bae1dSRodney W. Grimes /* 1472fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1473df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1474df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1475df8bae1dSRodney W. Grimes */ 1476fb59c426SYoshinobu Inoue th->th_seq++; 1477fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1478fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1479df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1480fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1481fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1482df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1483df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1484df8bae1dSRodney W. Grimes } 1485fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1486fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 1487a0292f23SGarrett Wollman /* 1488a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1489a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1490a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1491a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1492a0292f23SGarrett Wollman * Otherwise, goto step 6. 1493a0292f23SGarrett Wollman */ 1494fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1495a0292f23SGarrett Wollman goto process_ACK; 1496c068736aSJeffrey Hsu 1497df8bae1dSRodney W. Grimes goto step6; 1498c068736aSJeffrey Hsu 1499a0292f23SGarrett Wollman /* 1500a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1501a0292f23SGarrett Wollman * if segment contains a SYN and CC [not CC.NEW] option: 1502a0292f23SGarrett Wollman * if state == TIME_WAIT and connection duration > MSL, 1503a0292f23SGarrett Wollman * drop packet and send RST; 1504a0292f23SGarrett Wollman * 1505a0292f23SGarrett Wollman * if SEG.CC > CCrecv then is new SYN, and can implicitly 1506a0292f23SGarrett Wollman * ack the FIN (and data) in retransmission queue. 1507a0292f23SGarrett Wollman * Complete close and delete TCPCB. Then reprocess 1508a0292f23SGarrett Wollman * segment, hoping to find new TCPCB in LISTEN state; 1509a0292f23SGarrett Wollman * 1510a0292f23SGarrett Wollman * else must be old SYN; drop it. 1511a0292f23SGarrett Wollman * else do normal processing. 1512a0292f23SGarrett Wollman */ 1513a0292f23SGarrett Wollman case TCPS_LAST_ACK: 1514a0292f23SGarrett Wollman case TCPS_CLOSING: 1515a0292f23SGarrett Wollman case TCPS_TIME_WAIT: 1516340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1517fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) && 1518be2ac88cSJonathan Lemon (to.to_flags & TOF_CC) && tp->cc_recv != 0) { 1519a0292f23SGarrett Wollman if (tp->t_state == TCPS_TIME_WAIT && 1520a57815efSBosko Milekic (ticks - tp->t_starttime) > tcp_msl) { 1521a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1522a0292f23SGarrett Wollman goto dropwithreset; 1523a57815efSBosko Milekic } 1524a0292f23SGarrett Wollman if (CC_GT(to.to_cc, tp->cc_recv)) { 1525a0292f23SGarrett Wollman tp = tcp_close(tp); 1526a0292f23SGarrett Wollman goto findpcb; 1527a0292f23SGarrett Wollman } 1528a0292f23SGarrett Wollman else 1529a0292f23SGarrett Wollman goto drop; 1530a0292f23SGarrett Wollman } 1531a0292f23SGarrett Wollman break; /* continue normal processing */ 1532df8bae1dSRodney W. Grimes } 1533df8bae1dSRodney W. Grimes 1534df8bae1dSRodney W. Grimes /* 1535df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 153680ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 153780ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 153880ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 153980ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 154080ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 154180ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 154280ab7c0eSGarrett Wollman * killing a connection). 154380ab7c0eSGarrett Wollman * Then check timestamp, if present. 1544a0292f23SGarrett Wollman * Then check the connection count, if present. 1545df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 1546df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 1547df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 1548df8bae1dSRodney W. Grimes * 154980ab7c0eSGarrett Wollman * 155080ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 155180ab7c0eSGarrett Wollman * if this is a valid reset segment. 155280ab7c0eSGarrett Wollman * RFC 793 page 37: 155380ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 155480ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 155580ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 155680ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 155780ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 15581a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 15591a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 15601a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 15611a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 156280dd2a81SMike Silbersack * Note 2: Paul Watson's paper "Slipping in the Window" has shown 156380dd2a81SMike Silbersack * that brute force RST attacks are possible. To combat this, 156480dd2a81SMike Silbersack * we use a much stricter check while in the ESTABLISHED state, 156580dd2a81SMike Silbersack * only accepting RSTs where the sequence number is equal to 156680dd2a81SMike Silbersack * last_ack_sent. In all other states (the states in which a 156780dd2a81SMike Silbersack * RST is more likely), the more permissive check is used. 156880ab7c0eSGarrett Wollman * If we have multiple segments in flight, the intial reset 156980ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 157080ab7c0eSGarrett Wollman * but they will eventually catch up. 157180ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 157280ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 157380ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 157480ab7c0eSGarrett Wollman * 157580ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 157680ab7c0eSGarrett Wollman * 157780ab7c0eSGarrett Wollman * DISCUSSION 157880ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 157980ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 158080ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 158180ab7c0eSGarrett Wollman * data. 158280ab7c0eSGarrett Wollman * 158380ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 158480ab7c0eSGarrett Wollman * the state: 158580ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 158680ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 158780ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 1588745bab7fSDima Dorfman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 158980ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 1590e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 159180ab7c0eSGarrett Wollman * Close the tcb. 1592e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 159380ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 159480ab7c0eSGarrett Wollman * RFC 1337. 159580ab7c0eSGarrett Wollman */ 1596fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 1597fb59c426SYoshinobu Inoue if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 1598fb59c426SYoshinobu Inoue SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 159980ab7c0eSGarrett Wollman switch (tp->t_state) { 160080ab7c0eSGarrett Wollman 160180ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 160280ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 160380ab7c0eSGarrett Wollman goto close; 160480ab7c0eSGarrett Wollman 160580ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 160680dd2a81SMike Silbersack if (tp->last_ack_sent != th->th_seq) { 160780dd2a81SMike Silbersack tcpstat.tcps_badrst++; 160880dd2a81SMike Silbersack goto drop; 160980dd2a81SMike Silbersack } 161080ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 161180ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 161280ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 161380ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 161480ab7c0eSGarrett Wollman close: 161580ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 161680ab7c0eSGarrett Wollman tcpstat.tcps_drops++; 161780ab7c0eSGarrett Wollman tp = tcp_close(tp); 161880ab7c0eSGarrett Wollman break; 161980ab7c0eSGarrett Wollman 162080ab7c0eSGarrett Wollman case TCPS_CLOSING: 162180ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 162280ab7c0eSGarrett Wollman tp = tcp_close(tp); 162380ab7c0eSGarrett Wollman break; 162480ab7c0eSGarrett Wollman 162580ab7c0eSGarrett Wollman case TCPS_TIME_WAIT: 1626340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, 1627340c35deSJonathan Lemon ("timewait")); 162880ab7c0eSGarrett Wollman break; 162980ab7c0eSGarrett Wollman } 163080ab7c0eSGarrett Wollman } 163180ab7c0eSGarrett Wollman goto drop; 163280ab7c0eSGarrett Wollman } 163380ab7c0eSGarrett Wollman 163480ab7c0eSGarrett Wollman /* 1635df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 1636df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 1637df8bae1dSRodney W. Grimes */ 1638be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 163980ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1640df8bae1dSRodney W. Grimes 1641df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 16429b8b58e0SJonathan Lemon if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1643df8bae1dSRodney W. Grimes /* 1644df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 1645df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 1646df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 1647df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 1648df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 1649df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 1650df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 1651df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 1652df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 1653df8bae1dSRodney W. Grimes */ 1654df8bae1dSRodney W. Grimes tp->ts_recent = 0; 1655df8bae1dSRodney W. Grimes } else { 1656df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 1657fb59c426SYoshinobu Inoue tcpstat.tcps_rcvdupbyte += tlen; 1658df8bae1dSRodney W. Grimes tcpstat.tcps_pawsdrop++; 165907fd333dSMatthew Dillon if (tlen) 1660df8bae1dSRodney W. Grimes goto dropafterack; 16611ab4789dSMatthew Dillon goto drop; 16621ab4789dSMatthew Dillon } 1663df8bae1dSRodney W. Grimes } 1664df8bae1dSRodney W. Grimes 1665a0292f23SGarrett Wollman /* 1666a0292f23SGarrett Wollman * T/TCP mechanism 1667a0292f23SGarrett Wollman * If T/TCP was negotiated and the segment doesn't have CC, 1668dc733423SDag-Erling Smørgrav * or if its CC is wrong then drop the segment. 1669a0292f23SGarrett Wollman * RST segments do not have to comply with this. 1670a0292f23SGarrett Wollman */ 1671a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) && 1672be2ac88cSJonathan Lemon ((to.to_flags & TOF_CC) == 0 || tp->cc_recv != to.to_cc)) 1673a0292f23SGarrett Wollman goto dropafterack; 1674a0292f23SGarrett Wollman 167580ab7c0eSGarrett Wollman /* 167680ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 167780ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 167880ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 167980ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 168080ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 168180ab7c0eSGarrett Wollman */ 1682a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 1683a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1684a57815efSBosko Milekic goto dropwithreset; 1685a57815efSBosko Milekic } 168680ab7c0eSGarrett Wollman 1687fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 1688df8bae1dSRodney W. Grimes if (todrop > 0) { 1689fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1690fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 1691fb59c426SYoshinobu Inoue th->th_seq++; 1692fb59c426SYoshinobu Inoue if (th->th_urp > 1) 1693fb59c426SYoshinobu Inoue th->th_urp--; 1694df8bae1dSRodney W. Grimes else 1695fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1696df8bae1dSRodney W. Grimes todrop--; 1697df8bae1dSRodney W. Grimes } 1698df8bae1dSRodney W. Grimes /* 1699dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 1700df8bae1dSRodney W. Grimes */ 1701fb59c426SYoshinobu Inoue if (todrop > tlen 1702fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1703dac20301SGarrett Wollman /* 1704dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 1705dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 1706dac20301SGarrett Wollman * of sequence; drop it. 1707dac20301SGarrett Wollman */ 1708fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1709dac20301SGarrett Wollman 1710df8bae1dSRodney W. Grimes /* 1711dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 1712dac20301SGarrett Wollman * But keep on processing for RST or ACK. 1713df8bae1dSRodney W. Grimes */ 1714dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1715fb59c426SYoshinobu Inoue todrop = tlen; 1716dac20301SGarrett Wollman tcpstat.tcps_rcvduppack++; 1717dac20301SGarrett Wollman tcpstat.tcps_rcvdupbyte += todrop; 1718df8bae1dSRodney W. Grimes } else { 1719df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartduppack++; 1720df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartdupbyte += todrop; 1721df8bae1dSRodney W. Grimes } 1722fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 1723fb59c426SYoshinobu Inoue th->th_seq += todrop; 1724fb59c426SYoshinobu Inoue tlen -= todrop; 1725fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 1726fb59c426SYoshinobu Inoue th->th_urp -= todrop; 1727df8bae1dSRodney W. Grimes else { 1728fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1729fb59c426SYoshinobu Inoue th->th_urp = 0; 1730df8bae1dSRodney W. Grimes } 1731df8bae1dSRodney W. Grimes } 1732df8bae1dSRodney W. Grimes 1733df8bae1dSRodney W. Grimes /* 1734df8bae1dSRodney W. Grimes * If new data are received on a connection after the 1735df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 1736df8bae1dSRodney W. Grimes */ 1737df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 1738fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1739df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1740df8bae1dSRodney W. Grimes tcpstat.tcps_rcvafterclose++; 1741a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1742df8bae1dSRodney W. Grimes goto dropwithreset; 1743df8bae1dSRodney W. Grimes } 1744df8bae1dSRodney W. Grimes 1745df8bae1dSRodney W. Grimes /* 1746df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 1747df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 1748df8bae1dSRodney W. Grimes */ 1749fb59c426SYoshinobu Inoue todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd); 1750df8bae1dSRodney W. Grimes if (todrop > 0) { 1751df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1752fb59c426SYoshinobu Inoue if (todrop >= tlen) { 1753fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyteafterwin += tlen; 1754df8bae1dSRodney W. Grimes /* 1755df8bae1dSRodney W. Grimes * If a new connection request is received 1756df8bae1dSRodney W. Grimes * while in TIME_WAIT, drop the old connection 1757df8bae1dSRodney W. Grimes * and start over if the sequence numbers 1758df8bae1dSRodney W. Grimes * are above the previous ones. 1759df8bae1dSRodney W. Grimes */ 1760340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1761fb59c426SYoshinobu Inoue if (thflags & TH_SYN && 1762df8bae1dSRodney W. Grimes tp->t_state == TCPS_TIME_WAIT && 1763fb59c426SYoshinobu Inoue SEQ_GT(th->th_seq, tp->rcv_nxt)) { 1764df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1765df8bae1dSRodney W. Grimes goto findpcb; 1766df8bae1dSRodney W. Grimes } 1767df8bae1dSRodney W. Grimes /* 1768df8bae1dSRodney W. Grimes * If window is closed can only take segments at 1769df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 1770df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 1771df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 1772df8bae1dSRodney W. Grimes * and ack. 1773df8bae1dSRodney W. Grimes */ 1774fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1775df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1776df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinprobe++; 1777df8bae1dSRodney W. Grimes } else 1778df8bae1dSRodney W. Grimes goto dropafterack; 1779df8bae1dSRodney W. Grimes } else 1780df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1781df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1782fb59c426SYoshinobu Inoue tlen -= todrop; 1783fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 1784df8bae1dSRodney W. Grimes } 1785df8bae1dSRodney W. Grimes 1786df8bae1dSRodney W. Grimes /* 1787df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1788df8bae1dSRodney W. Grimes * record its timestamp. 1789a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1790a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1791df8bae1dSRodney W. Grimes */ 1792be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1793fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 17949b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1795a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1796df8bae1dSRodney W. Grimes } 1797df8bae1dSRodney W. Grimes 1798df8bae1dSRodney W. Grimes /* 1799df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 1800df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 1801df8bae1dSRodney W. Grimes */ 1802fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1803df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 1804a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1805122aad88SAndre Oppermann goto drop; 1806df8bae1dSRodney W. Grimes } 1807df8bae1dSRodney W. Grimes 1808a0292f23SGarrett Wollman /* 1809a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1810a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 1811a0292f23SGarrett Wollman * later processing; else drop segment and return. 1812a0292f23SGarrett Wollman */ 1813fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 1814a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 1815a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 1816a0292f23SGarrett Wollman goto step6; 1817a0292f23SGarrett Wollman else 1818a0292f23SGarrett Wollman goto drop; 1819a0292f23SGarrett Wollman } 1820df8bae1dSRodney W. Grimes 1821df8bae1dSRodney W. Grimes /* 1822df8bae1dSRodney W. Grimes * Ack processing. 1823df8bae1dSRodney W. Grimes */ 1824df8bae1dSRodney W. Grimes switch (tp->t_state) { 1825df8bae1dSRodney W. Grimes 1826df8bae1dSRodney W. Grimes /* 1827764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1828764d8cefSBill Fenner * ESTABLISHED state and continue processing. 1829764d8cefSBill Fenner * The ACK was checked above. 1830df8bae1dSRodney W. Grimes */ 1831df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1832a0292f23SGarrett Wollman 1833df8bae1dSRodney W. Grimes tcpstat.tcps_connects++; 1834df8bae1dSRodney W. Grimes soisconnected(so); 1835df8bae1dSRodney W. Grimes /* Do window scaling? */ 1836df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1837df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1838df8bae1dSRodney W. Grimes tp->snd_scale = tp->requested_s_scale; 1839df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 1840df8bae1dSRodney W. Grimes } 1841a0292f23SGarrett Wollman /* 1842a0292f23SGarrett Wollman * Upon successful completion of 3-way handshake, 184397d8d152SAndre Oppermann * update cache.CC, pass any queued data to the user, 184497d8d152SAndre Oppermann * and advance state appropriately. 1845a0292f23SGarrett Wollman */ 184697d8d152SAndre Oppermann if (tcp_do_rfc1644) { 184797d8d152SAndre Oppermann tao.tao_cc = tp->cc_recv; 184897d8d152SAndre Oppermann tcp_hc_updatetao(&inp->inp_inc, TCP_HC_TAO_CC, 184997d8d152SAndre Oppermann tp->cc_recv, 0); 185097d8d152SAndre Oppermann } 1851a0292f23SGarrett Wollman /* 1852a0292f23SGarrett Wollman * Make transitions: 1853a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 1854a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 1855a0292f23SGarrett Wollman */ 18569b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1857a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1858a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1859a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 18607ff19458SPaul Traina } else { 1861a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 18629b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, 18639b8b58e0SJonathan Lemon tcp_timer_keep, tp); 18647ff19458SPaul Traina } 1865a0292f23SGarrett Wollman /* 1866a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 1867a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 1868a0292f23SGarrett Wollman */ 1869fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 1870fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1871a0292f23SGarrett Wollman (struct mbuf *)0); 1872fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 187393b0017fSPhilippe Charnier /* FALLTHROUGH */ 1874df8bae1dSRodney W. Grimes 1875df8bae1dSRodney W. Grimes /* 1876df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1877df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 1878fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 1879fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 1880df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 1881df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 1882df8bae1dSRodney W. Grimes */ 1883df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1884df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1885df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 1886df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1887df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1888df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 1889df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 1890340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1891fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1892fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 1893df8bae1dSRodney W. Grimes tcpstat.tcps_rcvdupack++; 1894df8bae1dSRodney W. Grimes /* 1895df8bae1dSRodney W. Grimes * If we have outstanding data (other than 1896df8bae1dSRodney W. Grimes * a window probe), this is a completely 1897df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 1898df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 1899df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 1900df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 1901df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 1902df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 1903df8bae1dSRodney W. Grimes * window so we send only this one 1904df8bae1dSRodney W. Grimes * packet. 1905df8bae1dSRodney W. Grimes * 1906df8bae1dSRodney W. Grimes * We know we're losing at the current 1907df8bae1dSRodney W. Grimes * window size so do congestion avoidance 1908df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 1909df8bae1dSRodney W. Grimes * and pull our congestion window back to 1910df8bae1dSRodney W. Grimes * the new ssthresh). 1911df8bae1dSRodney W. Grimes * 1912df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 1913df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 1914df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 1915df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 1916df8bae1dSRodney W. Grimes * network. 1917df8bae1dSRodney W. Grimes */ 19189b8b58e0SJonathan Lemon if (!callout_active(tp->tt_rexmt) || 1919fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 1920df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1921cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 19226d90faf3SPaul Saab ((tcp_do_newreno || tp->sack_enable) && 19239d11646dSJeffrey Hsu IN_FASTRECOVERY(tp))) { 192446f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 192546f58482SJonathan Lemon (void) tcp_output(tp); 192646f58482SJonathan Lemon goto drop; 1927cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 1928cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 1929cb942153SJeffrey Hsu u_int win; 19306d90faf3SPaul Saab if ((tcp_do_newreno || 19316d90faf3SPaul Saab tp->sack_enable) && 19329d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, 19339d11646dSJeffrey Hsu tp->snd_recover)) { 1934cb942153SJeffrey Hsu tp->t_dupacks = 0; 1935cb942153SJeffrey Hsu break; 193646f58482SJonathan Lemon } 1937cb942153SJeffrey Hsu win = min(tp->snd_wnd, tp->snd_cwnd) / 1938cb942153SJeffrey Hsu 2 / tp->t_maxseg; 1939df8bae1dSRodney W. Grimes if (win < 2) 1940df8bae1dSRodney W. Grimes win = 2; 1941df8bae1dSRodney W. Grimes tp->snd_ssthresh = win * tp->t_maxseg; 19429d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 194346f58482SJonathan Lemon tp->snd_recover = tp->snd_max; 19449b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 19459b8b58e0SJonathan Lemon tp->t_rtttime = 0; 19466d90faf3SPaul Saab if (tp->sack_enable) { 19476d90faf3SPaul Saab tcpstat.tcps_sack_recovery_episode++; 19486d90faf3SPaul Saab tp->snd_cwnd = 19496d90faf3SPaul Saab tp->t_maxseg * 19506d90faf3SPaul Saab tp->t_dupacks; 19516d90faf3SPaul Saab (void) tcp_output(tp); 19526d90faf3SPaul Saab tp->snd_cwnd = 19536d90faf3SPaul Saab tp->snd_ssthresh; 19546d90faf3SPaul Saab goto drop; 19556d90faf3SPaul Saab } 19566d90faf3SPaul Saab 1957fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 1958df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 1959df8bae1dSRodney W. Grimes (void) tcp_output(tp); 196048d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 196148d2549cSJeffrey Hsu ("tp->snd_limited too big")); 1962df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 196348d2549cSJeffrey Hsu tp->t_maxseg * 196448d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 1965df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 1966df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 1967df8bae1dSRodney W. Grimes goto drop; 1968582a954bSJeffrey Hsu } else if (tcp_do_rfc3042) { 1969582a954bSJeffrey Hsu u_long oldcwnd = tp->snd_cwnd; 197048d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 197148d2549cSJeffrey Hsu u_int sent; 197289c02376SJeffrey Hsu 1973582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 1974582a954bSJeffrey Hsu tp->t_dupacks == 2, 1975582a954bSJeffrey Hsu ("dupacks not 1 or 2")); 197661a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 197748d2549cSJeffrey Hsu tp->snd_limited = 0; 197861a36e3dSJeffrey Hsu tp->snd_cwnd = 197961a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 198061a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 198161a36e3dSJeffrey Hsu tp->t_maxseg; 1982582a954bSJeffrey Hsu (void) tcp_output(tp); 198348d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 198448d2549cSJeffrey Hsu if (sent > tp->t_maxseg) { 198589c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 198689c02376SJeffrey Hsu tp->snd_limited == 0) || 198789c02376SJeffrey Hsu (sent == tp->t_maxseg + 1 && 198889c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 198948d2549cSJeffrey Hsu ("sent too much")); 199048d2549cSJeffrey Hsu tp->snd_limited = 2; 199148d2549cSJeffrey Hsu } else if (sent > 0) 199248d2549cSJeffrey Hsu ++tp->snd_limited; 1993582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 1994582a954bSJeffrey Hsu goto drop; 1995df8bae1dSRodney W. Grimes } 1996df8bae1dSRodney W. Grimes } else 1997df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1998df8bae1dSRodney W. Grimes break; 1999df8bae1dSRodney W. Grimes } 2000cb942153SJeffrey Hsu 2001cb942153SJeffrey Hsu KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una")); 2002cb942153SJeffrey Hsu 2003df8bae1dSRodney W. Grimes /* 2004df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2005df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2006df8bae1dSRodney W. Grimes */ 20076d90faf3SPaul Saab if (tcp_do_newreno || tp->sack_enable) { 20089d11646dSJeffrey Hsu if (IN_FASTRECOVERY(tp)) { 2009cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 20106d90faf3SPaul Saab if (tp->sack_enable) 20116d90faf3SPaul Saab tcp_sack_partialack(tp, th); 20126d90faf3SPaul Saab else 2013c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2014c068736aSJeffrey Hsu } else { 2015c068736aSJeffrey Hsu /* 20166d90faf3SPaul Saab * Out of fast recovery. 2017c068736aSJeffrey Hsu * Window inflation should have left us 2018c068736aSJeffrey Hsu * with approximately snd_ssthresh 2019c068736aSJeffrey Hsu * outstanding data. 2020c068736aSJeffrey Hsu * But in case we would be inclined to 2021c068736aSJeffrey Hsu * send a burst, better to do it via 2022c068736aSJeffrey Hsu * the slow start mechanism. 2023c068736aSJeffrey Hsu */ 2024c068736aSJeffrey Hsu if (SEQ_GT(th->th_ack + 2025c068736aSJeffrey Hsu tp->snd_ssthresh, 2026c068736aSJeffrey Hsu tp->snd_max)) 2027c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_max - 2028c068736aSJeffrey Hsu th->th_ack + 2029c068736aSJeffrey Hsu tp->t_maxseg; 2030c068736aSJeffrey Hsu else 2031c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_ssthresh; 2032c068736aSJeffrey Hsu } 2033c068736aSJeffrey Hsu } 2034c068736aSJeffrey Hsu } else { 2035233e8c18SGarrett Wollman if (tp->t_dupacks >= tcprexmtthresh && 2036df8bae1dSRodney W. Grimes tp->snd_cwnd > tp->snd_ssthresh) 2037df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh; 203846f58482SJonathan Lemon } 2039cb942153SJeffrey Hsu tp->t_dupacks = 0; 2040fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_max)) { 2041df8bae1dSRodney W. Grimes tcpstat.tcps_rcvacktoomuch++; 2042df8bae1dSRodney W. Grimes goto dropafterack; 2043df8bae1dSRodney W. Grimes } 2044a0292f23SGarrett Wollman /* 2045a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2046a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2047a0292f23SGarrett Wollman */ 2048a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2049a0292f23SGarrett Wollman /* 2050a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2051a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 205207e43e10SAndras Olah * synchronized). Go to non-starred state, 205307e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 205407e43e10SAndras Olah * we can do window scaling. 2055a0292f23SGarrett Wollman */ 2056a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2057a0292f23SGarrett Wollman tp->snd_una++; 205807e43e10SAndras Olah /* Do window scaling? */ 205907e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 206007e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 206107e43e10SAndras Olah tp->snd_scale = tp->requested_s_scale; 206207e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 206307e43e10SAndras Olah } 2064a0292f23SGarrett Wollman } 2065a0292f23SGarrett Wollman 2066a0292f23SGarrett Wollman process_ACK: 2067fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 2068df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 2069df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 2070df8bae1dSRodney W. Grimes 2071df8bae1dSRodney W. Grimes /* 20729b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 20739b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 20749b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 20759b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 20769b8b58e0SJonathan Lemon * we left off. 20779b8b58e0SJonathan Lemon */ 20789b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 2079d65bf08aSMatthew Dillon ++tcpstat.tcps_sndrexmitbad; 20809b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 20819b8b58e0SJonathan Lemon tp->snd_ssthresh = tp->snd_ssthresh_prev; 20829d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 20839d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 20849d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 20859b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 20869b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; /* XXX probably not required */ 20879b8b58e0SJonathan Lemon } 20889b8b58e0SJonathan Lemon 20899b8b58e0SJonathan Lemon /* 2090df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2091df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2092df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2093df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2094df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2095df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2096df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2097fa55172bSMatthew Dillon * 2098fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2099fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2100fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2101fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2102df8bae1dSRodney W. Grimes */ 2103fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 2104fa55172bSMatthew Dillon to.to_tsecr) { 21059b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 2106fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 21079b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2108fa55172bSMatthew Dillon } 21091fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 2110df8bae1dSRodney W. Grimes 2111df8bae1dSRodney W. Grimes /* 2112df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2113df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2114df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2115df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2116df8bae1dSRodney W. Grimes */ 2117fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 21189b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 2119df8bae1dSRodney W. Grimes needoutput = 1; 21209b8b58e0SJonathan Lemon } else if (!callout_active(tp->tt_persist)) 21219b8b58e0SJonathan Lemon callout_reset(tp->tt_rexmt, tp->t_rxtcur, 21229b8b58e0SJonathan Lemon tcp_timer_rexmt, tp); 2123a0292f23SGarrett Wollman 2124a0292f23SGarrett Wollman /* 2125a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2126a0292f23SGarrett Wollman * skip rest of ACK processing. 2127a0292f23SGarrett Wollman */ 2128a0292f23SGarrett Wollman if (acked == 0) 2129a0292f23SGarrett Wollman goto step6; 2130a0292f23SGarrett Wollman 2131df8bae1dSRodney W. Grimes /* 2132df8bae1dSRodney W. Grimes * When new data is acked, open the congestion window. 2133df8bae1dSRodney W. Grimes * If the window gives us less than ssthresh packets 2134df8bae1dSRodney W. Grimes * in flight, open exponentially (maxseg per packet). 2135df8bae1dSRodney W. Grimes * Otherwise open linearly: maxseg per window 213610be5648SGarrett Wollman * (maxseg^2 / cwnd per packet). 2137df8bae1dSRodney W. Grimes */ 21386d90faf3SPaul Saab if ((!tcp_do_newreno && !tp->sack_enable) || 21396d90faf3SPaul Saab !IN_FASTRECOVERY(tp)) { 2140df8bae1dSRodney W. Grimes register u_int cw = tp->snd_cwnd; 2141df8bae1dSRodney W. Grimes register u_int incr = tp->t_maxseg; 2142df8bae1dSRodney W. Grimes if (cw > tp->snd_ssthresh) 214310be5648SGarrett Wollman incr = incr * incr / cw; 2144df8bae1dSRodney W. Grimes tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale); 2145df8bae1dSRodney W. Grimes } 2146df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 2147df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 2148df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 2149df8bae1dSRodney W. Grimes ourfinisacked = 1; 2150df8bae1dSRodney W. Grimes } else { 2151df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 2152df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 2153df8bae1dSRodney W. Grimes ourfinisacked = 0; 2154df8bae1dSRodney W. Grimes } 2155df8bae1dSRodney W. Grimes sowwakeup(so); 2156cb942153SJeffrey Hsu /* detect una wraparound */ 21576d90faf3SPaul Saab if ((tcp_do_newreno || tp->sack_enable) && 21586d90faf3SPaul Saab !IN_FASTRECOVERY(tp) && 21599d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 21609d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 21619d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 21626d90faf3SPaul Saab if ((tcp_do_newreno || tp->sack_enable) && 21636d90faf3SPaul Saab IN_FASTRECOVERY(tp) && 21649d11646dSJeffrey Hsu SEQ_GEQ(th->th_ack, tp->snd_recover)) 21659d11646dSJeffrey Hsu EXIT_FASTRECOVERY(tp); 2166fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 21676d90faf3SPaul Saab if (tp->sack_enable) { 21686d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 21696d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 21706d90faf3SPaul Saab } 2171df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2172df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2173df8bae1dSRodney W. Grimes 2174df8bae1dSRodney W. Grimes switch (tp->t_state) { 2175df8bae1dSRodney W. Grimes 2176df8bae1dSRodney W. Grimes /* 2177df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2178df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2179df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2180df8bae1dSRodney W. Grimes */ 2181df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2182df8bae1dSRodney W. Grimes if (ourfinisacked) { 2183df8bae1dSRodney W. Grimes /* 2184df8bae1dSRodney W. Grimes * If we can't receive any more 2185df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2186df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2187df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2188df8bae1dSRodney W. Grimes * we'll hang forever. 2189df8bae1dSRodney W. Grimes */ 2190340c35deSJonathan Lemon /* XXXjl 2191340c35deSJonathan Lemon * we should release the tp also, and use a 2192340c35deSJonathan Lemon * compressed state. 2193340c35deSJonathan Lemon */ 2194c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 219503e49181SSeigo Tanimura soisdisconnected(so); 21969b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, tcp_maxidle, 21979b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 21984cc20ab1SSeigo Tanimura } 2199df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 2200df8bae1dSRodney W. Grimes } 2201df8bae1dSRodney W. Grimes break; 2202df8bae1dSRodney W. Grimes 2203df8bae1dSRodney W. Grimes /* 2204df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2205df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2206df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2207df8bae1dSRodney W. Grimes * the segment. 2208df8bae1dSRodney W. Grimes */ 2209df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2210df8bae1dSRodney W. Grimes if (ourfinisacked) { 2211340c35deSJonathan Lemon KASSERT(headlocked, ("headlocked")); 221211a20fb8SJeffrey Hsu tcp_twstart(tp); 2213340c35deSJonathan Lemon INP_INFO_WUNLOCK(&tcbinfo); 2214340c35deSJonathan Lemon m_freem(m); 2215340c35deSJonathan Lemon return; 2216df8bae1dSRodney W. Grimes } 2217df8bae1dSRodney W. Grimes break; 2218df8bae1dSRodney W. Grimes 2219df8bae1dSRodney W. Grimes /* 2220df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2221df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2222df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2223df8bae1dSRodney W. Grimes * enter the closed state and return. 2224df8bae1dSRodney W. Grimes */ 2225df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2226df8bae1dSRodney W. Grimes if (ourfinisacked) { 2227df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2228df8bae1dSRodney W. Grimes goto drop; 2229df8bae1dSRodney W. Grimes } 2230df8bae1dSRodney W. Grimes break; 2231df8bae1dSRodney W. Grimes 2232df8bae1dSRodney W. Grimes /* 2233df8bae1dSRodney W. Grimes * In TIME_WAIT state the only thing that should arrive 2234df8bae1dSRodney W. Grimes * is a retransmission of the remote FIN. Acknowledge 2235df8bae1dSRodney W. Grimes * it and restart the finack timer. 2236df8bae1dSRodney W. Grimes */ 2237df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 2238340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 22399b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 22409b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2241df8bae1dSRodney W. Grimes goto dropafterack; 2242df8bae1dSRodney W. Grimes } 2243df8bae1dSRodney W. Grimes } 2244df8bae1dSRodney W. Grimes 2245df8bae1dSRodney W. Grimes step6: 2246df8bae1dSRodney W. Grimes /* 2247df8bae1dSRodney W. Grimes * Update window information. 2248df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 2249df8bae1dSRodney W. Grimes */ 2250fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 2251fb59c426SYoshinobu Inoue (SEQ_LT(tp->snd_wl1, th->th_seq) || 2252fb59c426SYoshinobu Inoue (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2253fb59c426SYoshinobu Inoue (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2254df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 2255fb59c426SYoshinobu Inoue if (tlen == 0 && 2256fb59c426SYoshinobu Inoue tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2257df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinupd++; 2258df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 2259fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq; 2260fb59c426SYoshinobu Inoue tp->snd_wl2 = th->th_ack; 2261df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2262df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 2263df8bae1dSRodney W. Grimes needoutput = 1; 2264df8bae1dSRodney W. Grimes } 2265df8bae1dSRodney W. Grimes 2266df8bae1dSRodney W. Grimes /* 2267df8bae1dSRodney W. Grimes * Process segments with URG. 2268df8bae1dSRodney W. Grimes */ 2269fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2270df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2271df8bae1dSRodney W. Grimes /* 2272df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2273df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2274df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2275df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2276df8bae1dSRodney W. Grimes */ 2277fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2278fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2279fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 2280df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2281df8bae1dSRodney W. Grimes } 2282df8bae1dSRodney W. Grimes /* 2283df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2284df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2285df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2286df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2287df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2288df8bae1dSRodney W. Grimes * 2289df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2290df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2291df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2292df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2293df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2294df8bae1dSRodney W. Grimes * spec states (in one of two places). 2295df8bae1dSRodney W. Grimes */ 2296fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2297fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2298df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2299df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 23007721f5d7SRobert Watson if (so->so_oobmark == 0) { 23017721f5d7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2302c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 23037721f5d7SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 23047721f5d7SRobert Watson } 2305df8bae1dSRodney W. Grimes sohasoutofband(so); 2306df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2307df8bae1dSRodney W. Grimes } 2308df8bae1dSRodney W. Grimes /* 2309df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2310df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2311df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2312df8bae1dSRodney W. Grimes * data may creep in... ick. 2313df8bae1dSRodney W. Grimes */ 231430613f56SJeffrey Hsu if (th->th_urp <= (u_long)tlen && 231530613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 231630613f56SJeffrey Hsu /* hdr drop is delayed */ 231730613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 231830613f56SJeffrey Hsu } 2319c068736aSJeffrey Hsu } else { 2320df8bae1dSRodney W. Grimes /* 2321df8bae1dSRodney W. Grimes * If no out of band data is expected, 2322df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2323df8bae1dSRodney W. Grimes * with the receive window. 2324df8bae1dSRodney W. Grimes */ 2325df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2326df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2327c068736aSJeffrey Hsu } 2328df8bae1dSRodney W. Grimes dodata: /* XXX */ 2329f76fcf6dSJeffrey Hsu KASSERT(headlocked, ("headlocked")); 2330df8bae1dSRodney W. Grimes /* 2331df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2332df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2333df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2334df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2335df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2336df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2337df8bae1dSRodney W. Grimes */ 2338fb59c426SYoshinobu Inoue if ((tlen || (thflags & TH_FIN)) && 2339df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2340fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2341e4b64281SJesper Skriver /* 2342c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2343c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2344c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2345c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2346c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2347c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2348c068736aSJeffrey Hsu * conversions. 2349c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 2350c068736aSJeffrey Hsu * immediately when segments are out of order (so 2351c068736aSJeffrey Hsu * fast retransmit can work). 2352e4b64281SJesper Skriver */ 2353e4b64281SJesper Skriver if (th->th_seq == tp->rcv_nxt && 2354e4b64281SJesper Skriver LIST_EMPTY(&tp->t_segq) && 2355e4b64281SJesper Skriver TCPS_HAVEESTABLISHED(tp->t_state)) { 2356e4b64281SJesper Skriver if (DELAY_ACK(tp)) 23573bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2358e4b64281SJesper Skriver else 2359e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2360e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2361e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 2362e4b64281SJesper Skriver tcpstat.tcps_rcvpack++; 2363e4b64281SJesper Skriver tcpstat.tcps_rcvbyte += tlen; 2364e4b64281SJesper Skriver ND6_HINT(tp); 2365c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2366c1c36a2cSMike Silbersack m_freem(m); 2367c1c36a2cSMike Silbersack else 2368395bb186SSam Leffler sbappendstream(&so->so_rcv, m); 2369e4b64281SJesper Skriver sorwakeup(so); 2370e4b64281SJesper Skriver } else { 2371e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 2372e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2373e4b64281SJesper Skriver } 23746d90faf3SPaul Saab if (tp->sack_enable) 23756d90faf3SPaul Saab tcp_update_sack_list(tp); 2376df8bae1dSRodney W. Grimes /* 2377df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2378df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2379df8bae1dSRodney W. Grimes * buffer size. 2380df8bae1dSRodney W. Grimes */ 2381df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2382df8bae1dSRodney W. Grimes } else { 2383df8bae1dSRodney W. Grimes m_freem(m); 2384fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2385df8bae1dSRodney W. Grimes } 2386df8bae1dSRodney W. Grimes 2387df8bae1dSRodney W. Grimes /* 2388df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2389df8bae1dSRodney W. Grimes * that the connection is closing. 2390df8bae1dSRodney W. Grimes */ 2391fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2392df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2393df8bae1dSRodney W. Grimes socantrcvmore(so); 2394a0292f23SGarrett Wollman /* 2395a0292f23SGarrett Wollman * If connection is half-synchronized 2396d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2397a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2398a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2399a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2400a0292f23SGarrett Wollman */ 24013bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 24023bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2403a0292f23SGarrett Wollman else 2404df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2405df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2406df8bae1dSRodney W. Grimes } 2407df8bae1dSRodney W. Grimes switch (tp->t_state) { 2408df8bae1dSRodney W. Grimes 2409df8bae1dSRodney W. Grimes /* 2410df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2411df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2412df8bae1dSRodney W. Grimes */ 2413df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 24149b8b58e0SJonathan Lemon tp->t_starttime = ticks; 24159b8b58e0SJonathan Lemon /*FALLTHROUGH*/ 2416df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2417df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2418df8bae1dSRodney W. Grimes break; 2419df8bae1dSRodney W. Grimes 2420df8bae1dSRodney W. Grimes /* 2421df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2422df8bae1dSRodney W. Grimes * enter the CLOSING state. 2423df8bae1dSRodney W. Grimes */ 2424df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2425df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 2426df8bae1dSRodney W. Grimes break; 2427df8bae1dSRodney W. Grimes 2428df8bae1dSRodney W. Grimes /* 2429df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 2430df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 2431df8bae1dSRodney W. Grimes * standard timers. 2432df8bae1dSRodney W. Grimes */ 2433df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 243411a20fb8SJeffrey Hsu KASSERT(headlocked == 1, ("headlocked should be 1")); 2435340c35deSJonathan Lemon tcp_twstart(tp); 243611a20fb8SJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2437340c35deSJonathan Lemon return; 2438df8bae1dSRodney W. Grimes 2439df8bae1dSRodney W. Grimes /* 2440df8bae1dSRodney W. Grimes * In TIME_WAIT state restart the 2 MSL time_wait timer. 2441df8bae1dSRodney W. Grimes */ 2442df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 2443340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 24449b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 24459b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2446df8bae1dSRodney W. Grimes break; 2447df8bae1dSRodney W. Grimes } 2448df8bae1dSRodney W. Grimes } 24497792ea27SJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2450610ee2f9SDavid Greenman #ifdef TCPDEBUG 24514cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2452fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2453fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2454610ee2f9SDavid Greenman #endif 2455df8bae1dSRodney W. Grimes 2456df8bae1dSRodney W. Grimes /* 2457df8bae1dSRodney W. Grimes * Return any desired output. 2458df8bae1dSRodney W. Grimes */ 2459df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 2460df8bae1dSRodney W. Grimes (void) tcp_output(tp); 24617792ea27SJeffrey Hsu 2462a14c749fSJonathan Lemon check_delack: 2463a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 24643bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 24653bfd6421SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 24663bfd6421SJonathan Lemon tcp_timer_delack, tp); 24673bfd6421SJonathan Lemon } 2468f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 2469df8bae1dSRodney W. Grimes return; 2470df8bae1dSRodney W. Grimes 2471df8bae1dSRodney W. Grimes dropafterack: 2472df8bae1dSRodney W. Grimes /* 2473df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 2474df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 247580ab7c0eSGarrett Wollman * 247680ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 247780ab7c0eSGarrett Wollman * paths to this code happen after packets containing 247880ab7c0eSGarrett Wollman * RST have been dropped. 247980ab7c0eSGarrett Wollman * 248080ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 248180ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 248280ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 248380ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 248480ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 248580ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 2486df8bae1dSRodney W. Grimes */ 2487fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2488fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 2489a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 2490a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2491a57815efSBosko Milekic goto dropwithreset; 2492a57815efSBosko Milekic } 2493a0292f23SGarrett Wollman #ifdef TCPDEBUG 24944cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2495fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2496fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2497a0292f23SGarrett Wollman #endif 249885e8b243SJeffrey Hsu KASSERT(headlocked, ("headlocked should be 1")); 2499f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2500df8bae1dSRodney W. Grimes m_freem(m); 2501df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2502df8bae1dSRodney W. Grimes (void) tcp_output(tp); 2503f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 2504df8bae1dSRodney W. Grimes return; 2505df8bae1dSRodney W. Grimes 2506df8bae1dSRodney W. Grimes dropwithreset: 2507df8bae1dSRodney W. Grimes /* 2508df8bae1dSRodney W. Grimes * Generate a RST, dropping incoming segment. 2509df8bae1dSRodney W. Grimes * Make ACK acceptable to originator of segment. 2510df8bae1dSRodney W. Grimes * Don't bother to respond if destination was broadcast/multicast. 2511df8bae1dSRodney W. Grimes */ 2512fb59c426SYoshinobu Inoue if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2513df8bae1dSRodney W. Grimes goto drop; 2514fb59c426SYoshinobu Inoue if (isipv6) { 2515173c0f9fSWarner Losh if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2516173c0f9fSWarner Losh IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2517fb59c426SYoshinobu Inoue goto drop; 2518c068736aSJeffrey Hsu } else { 2519173c0f9fSWarner Losh if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2520173c0f9fSWarner Losh IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 25212ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 25222ca2159fSCrist J. Clark in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2523fb59c426SYoshinobu Inoue goto drop; 2524c068736aSJeffrey Hsu } 2525fb59c426SYoshinobu Inoue /* IPv6 anycast check is done at tcp6_input() */ 2526a57815efSBosko Milekic 2527a57815efSBosko Milekic /* 2528c59319bfSDag-Erling Smørgrav * Perform bandwidth limiting. 2529a57815efSBosko Milekic */ 2530a57815efSBosko Milekic if (badport_bandlim(rstreason) < 0) 2531a57815efSBosko Milekic goto drop; 2532a57815efSBosko Milekic 2533a0292f23SGarrett Wollman #ifdef TCPDEBUG 25344cc20ab1SSeigo Tanimura if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2535fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2536fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2537a0292f23SGarrett Wollman #endif 25386fd22cafSJeffrey Hsu 2539fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 2540fb59c426SYoshinobu Inoue /* mtod() below is safe as long as hdr dropping is delayed */ 2541fb59c426SYoshinobu Inoue tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack, 2542fb59c426SYoshinobu Inoue TH_RST); 2543df8bae1dSRodney W. Grimes else { 2544fb59c426SYoshinobu Inoue if (thflags & TH_SYN) 2545fb59c426SYoshinobu Inoue tlen++; 2546fb59c426SYoshinobu Inoue /* mtod() below is safe as long as hdr dropping is delayed */ 2547fb59c426SYoshinobu Inoue tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2548fb59c426SYoshinobu Inoue (tcp_seq)0, TH_RST|TH_ACK); 2549df8bae1dSRodney W. Grimes } 2550c29afad6SSam Leffler 2551c29afad6SSam Leffler if (tp) 2552c29afad6SSam Leffler INP_UNLOCK(inp); 2553f76fcf6dSJeffrey Hsu if (headlocked) 2554f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2555df8bae1dSRodney W. Grimes return; 2556df8bae1dSRodney W. Grimes 2557df8bae1dSRodney W. Grimes drop: 2558df8bae1dSRodney W. Grimes /* 2559df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 2560df8bae1dSRodney W. Grimes */ 2561610ee2f9SDavid Greenman #ifdef TCPDEBUG 25624cc20ab1SSeigo Tanimura if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2563fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2564fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2565a0292f23SGarrett Wollman #endif 2566f76fcf6dSJeffrey Hsu if (tp) 2567f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 2568df8bae1dSRodney W. Grimes m_freem(m); 2569f76fcf6dSJeffrey Hsu if (headlocked) 2570f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2571df8bae1dSRodney W. Grimes return; 2572df8bae1dSRodney W. Grimes } 2573df8bae1dSRodney W. Grimes 2574be2ac88cSJonathan Lemon /* 2575be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 2576be2ac88cSJonathan Lemon */ 25770312fbe9SPoul-Henning Kamp static void 25786d90faf3SPaul Saab tcp_dooptions(tp, to, cp, cnt, is_syn, th) 25796d90faf3SPaul Saab struct tcpcb *tp; 2580be2ac88cSJonathan Lemon struct tcpopt *to; 2581df8bae1dSRodney W. Grimes u_char *cp; 2582df8bae1dSRodney W. Grimes int cnt; 2583152385d1SDavid E. O'Brien int is_syn; 25846d90faf3SPaul Saab struct tcphdr *th; 2585df8bae1dSRodney W. Grimes { 2586df8bae1dSRodney W. Grimes int opt, optlen; 2587df8bae1dSRodney W. Grimes 2588be2ac88cSJonathan Lemon to->to_flags = 0; 2589df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 2590df8bae1dSRodney W. Grimes opt = cp[0]; 2591df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 2592df8bae1dSRodney W. Grimes break; 2593df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 2594df8bae1dSRodney W. Grimes optlen = 1; 2595df8bae1dSRodney W. Grimes else { 2596b474779fSJun-ichiro itojun Hagino if (cnt < 2) 2597b474779fSJun-ichiro itojun Hagino break; 2598df8bae1dSRodney W. Grimes optlen = cp[1]; 2599b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 2600df8bae1dSRodney W. Grimes break; 2601df8bae1dSRodney W. Grimes } 2602df8bae1dSRodney W. Grimes switch (opt) { 2603df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 2604df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 2605df8bae1dSRodney W. Grimes continue; 2606be2ac88cSJonathan Lemon if (!is_syn) 2607df8bae1dSRodney W. Grimes continue; 2608be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 2609be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 2610be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 2611fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 2612df8bae1dSRodney W. Grimes break; 2613df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 2614df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 2615df8bae1dSRodney W. Grimes continue; 2616be2ac88cSJonathan Lemon if (! is_syn) 2617df8bae1dSRodney W. Grimes continue; 2618be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 2619be2ac88cSJonathan Lemon to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 2620df8bae1dSRodney W. Grimes break; 2621df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 2622df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 2623df8bae1dSRodney W. Grimes continue; 2624be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 2625a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2626a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 2627fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 2628a0292f23SGarrett Wollman bcopy((char *)cp + 6, 2629a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2630fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 2631df8bae1dSRodney W. Grimes break; 2632a0292f23SGarrett Wollman case TCPOPT_CC: 2633a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2634a0292f23SGarrett Wollman continue; 2635be2ac88cSJonathan Lemon to->to_flags |= TOF_CC; 2636a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2637a0292f23SGarrett Wollman (char *)&to->to_cc, sizeof(to->to_cc)); 2638fd8e4ebcSMike Barcroft to->to_cc = ntohl(to->to_cc); 2639a0292f23SGarrett Wollman break; 2640a0292f23SGarrett Wollman case TCPOPT_CCNEW: 2641a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2642a0292f23SGarrett Wollman continue; 2643be2ac88cSJonathan Lemon if (!is_syn) 2644a0292f23SGarrett Wollman continue; 2645be2ac88cSJonathan Lemon to->to_flags |= TOF_CCNEW; 2646a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2647a0292f23SGarrett Wollman (char *)&to->to_cc, sizeof(to->to_cc)); 2648fd8e4ebcSMike Barcroft to->to_cc = ntohl(to->to_cc); 2649a0292f23SGarrett Wollman break; 2650a0292f23SGarrett Wollman case TCPOPT_CCECHO: 2651a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2652a0292f23SGarrett Wollman continue; 2653be2ac88cSJonathan Lemon if (!is_syn) 2654a0292f23SGarrett Wollman continue; 2655be2ac88cSJonathan Lemon to->to_flags |= TOF_CCECHO; 2656a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2657a0292f23SGarrett Wollman (char *)&to->to_ccecho, sizeof(to->to_ccecho)); 2658fd8e4ebcSMike Barcroft to->to_ccecho = ntohl(to->to_ccecho); 2659a0292f23SGarrett Wollman break; 26601cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 26611cfd4b53SBruce M Simpson /* 26621cfd4b53SBruce M Simpson * XXX In order to reply to a host which has set the 26631cfd4b53SBruce M Simpson * TCP_SIGNATURE option in its initial SYN, we have to 26641cfd4b53SBruce M Simpson * record the fact that the option was observed here 26651cfd4b53SBruce M Simpson * for the syncache code to perform the correct response. 26661cfd4b53SBruce M Simpson */ 26671cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 26681cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 26691cfd4b53SBruce M Simpson continue; 26701cfd4b53SBruce M Simpson to->to_flags |= (TOF_SIGNATURE | TOF_SIGLEN); 26711cfd4b53SBruce M Simpson break; 2672265ed012SBruce M Simpson #endif 26736d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 26746d90faf3SPaul Saab if (!tcp_do_sack || 26756d90faf3SPaul Saab optlen != TCPOLEN_SACK_PERMITTED) 26766d90faf3SPaul Saab continue; 26776d90faf3SPaul Saab if (is_syn) { 26786d90faf3SPaul Saab /* MUST only be set on SYN */ 26796d90faf3SPaul Saab to->to_flags |= TOF_SACK; 26806d90faf3SPaul Saab } 26816d90faf3SPaul Saab break; 26826d90faf3SPaul Saab 26836d90faf3SPaul Saab case TCPOPT_SACK: 26846d90faf3SPaul Saab if (!tp || tcp_sack_option(tp, th, cp, optlen)) 26856d90faf3SPaul Saab continue; 26866d90faf3SPaul Saab break; 2687be2ac88cSJonathan Lemon default: 2688be2ac88cSJonathan Lemon continue; 2689df8bae1dSRodney W. Grimes } 2690df8bae1dSRodney W. Grimes } 2691df8bae1dSRodney W. Grimes } 2692df8bae1dSRodney W. Grimes 2693df8bae1dSRodney W. Grimes /* 2694df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 2695df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 2696df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 2697df8bae1dSRodney W. Grimes * sequencing purposes. 2698df8bae1dSRodney W. Grimes */ 26990312fbe9SPoul-Henning Kamp static void 2700fb59c426SYoshinobu Inoue tcp_pulloutofband(so, th, m, off) 2701df8bae1dSRodney W. Grimes struct socket *so; 2702fb59c426SYoshinobu Inoue struct tcphdr *th; 2703df8bae1dSRodney W. Grimes register struct mbuf *m; 2704fb59c426SYoshinobu Inoue int off; /* delayed to be droped hdrlen */ 2705df8bae1dSRodney W. Grimes { 2706fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 2707df8bae1dSRodney W. Grimes 2708df8bae1dSRodney W. Grimes while (cnt >= 0) { 2709df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 2710df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 2711df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 2712df8bae1dSRodney W. Grimes 2713df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 2714df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 2715df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2716df8bae1dSRodney W. Grimes m->m_len--; 271769a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 271869a34685SYoshinobu Inoue m->m_pkthdr.len--; 2719df8bae1dSRodney W. Grimes return; 2720df8bae1dSRodney W. Grimes } 2721df8bae1dSRodney W. Grimes cnt -= m->m_len; 2722df8bae1dSRodney W. Grimes m = m->m_next; 2723df8bae1dSRodney W. Grimes if (m == 0) 2724df8bae1dSRodney W. Grimes break; 2725df8bae1dSRodney W. Grimes } 2726df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 2727df8bae1dSRodney W. Grimes } 2728df8bae1dSRodney W. Grimes 2729df8bae1dSRodney W. Grimes /* 2730df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 2731df8bae1dSRodney W. Grimes * and update averages and current timeout. 2732df8bae1dSRodney W. Grimes */ 27330312fbe9SPoul-Henning Kamp static void 2734df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt) 2735df8bae1dSRodney W. Grimes register struct tcpcb *tp; 27369b8b58e0SJonathan Lemon int rtt; 2737df8bae1dSRodney W. Grimes { 2738233e8c18SGarrett Wollman register int delta; 2739233e8c18SGarrett Wollman 2740233e8c18SGarrett Wollman tcpstat.tcps_rttupdated++; 2741233e8c18SGarrett Wollman tp->t_rttupdated++; 2742233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 2743233e8c18SGarrett Wollman /* 2744233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 2745233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 2746233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 2747233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2748233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 2749233e8c18SGarrett Wollman */ 2750233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2751233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2752233e8c18SGarrett Wollman 2753233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 2754233e8c18SGarrett Wollman tp->t_srtt = 1; 2755233e8c18SGarrett Wollman 2756233e8c18SGarrett Wollman /* 2757233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 2758233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 2759233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 2760233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 2761233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 2762233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 2763233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2764233e8c18SGarrett Wollman * rfc793's wired-in beta. 2765233e8c18SGarrett Wollman */ 2766233e8c18SGarrett Wollman if (delta < 0) 2767233e8c18SGarrett Wollman delta = -delta; 2768233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2769233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 2770233e8c18SGarrett Wollman tp->t_rttvar = 1; 27711fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 27721fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2773233e8c18SGarrett Wollman } else { 2774233e8c18SGarrett Wollman /* 2775233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 2776233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 2777233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 2778233e8c18SGarrett Wollman */ 2779233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 2780233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 27811fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2782233e8c18SGarrett Wollman } 27839b8b58e0SJonathan Lemon tp->t_rtttime = 0; 2784df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 2785df8bae1dSRodney W. Grimes 2786df8bae1dSRodney W. Grimes /* 2787df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 2788df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 2789df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 2790df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 2791df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 2792df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 2793df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 2794df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 2795df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 2796df8bae1dSRodney W. Grimes */ 2797233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 27989e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2799df8bae1dSRodney W. Grimes 2800df8bae1dSRodney W. Grimes /* 2801df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 2802df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 2803df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 2804df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 2805df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 2806df8bae1dSRodney W. Grimes */ 2807df8bae1dSRodney W. Grimes tp->t_softerror = 0; 2808df8bae1dSRodney W. Grimes } 2809df8bae1dSRodney W. Grimes 2810df8bae1dSRodney W. Grimes /* 2811df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 2812df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 2813df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 2814df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 2815df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2816df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 2817df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 2818df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 2819df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 2820df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 2821df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 2822df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 2823df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 2824a0292f23SGarrett Wollman * 2825a0292f23SGarrett Wollman * Also take into account the space needed for options that we 2826a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 2827a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 2828a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 2829a0292f23SGarrett Wollman * data in maxopd. 2830a0292f23SGarrett Wollman * 2831a0292f23SGarrett Wollman * 2832a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 2833a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 2834a0292f23SGarrett Wollman * MSS of our peer. 283597d8d152SAndre Oppermann * 283697d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 283797d8d152SAndre Oppermann * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt(). 2838df8bae1dSRodney W. Grimes */ 2839a0292f23SGarrett Wollman void 2840df8bae1dSRodney W. Grimes tcp_mss(tp, offer) 2841a0292f23SGarrett Wollman struct tcpcb *tp; 2842a0292f23SGarrett Wollman int offer; 2843df8bae1dSRodney W. Grimes { 284497d8d152SAndre Oppermann int rtt, mss; 2845df8bae1dSRodney W. Grimes u_long bufsize; 284697d8d152SAndre Oppermann u_long maxmtu; 2847c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 2848df8bae1dSRodney W. Grimes struct socket *so; 284997d8d152SAndre Oppermann struct hc_metrics_lite metrics; 285097d8d152SAndre Oppermann struct rmxp_tao tao; 2851a0292f23SGarrett Wollman int origoffer = offer; 2852fb59c426SYoshinobu Inoue #ifdef INET6 2853c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2854c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 2855c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 2856c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 2857c068736aSJeffrey Hsu #else 2858c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 2859fb59c426SYoshinobu Inoue #endif 286097d8d152SAndre Oppermann bzero(&tao, sizeof(tao)); 2861df8bae1dSRodney W. Grimes 286297d8d152SAndre Oppermann /* initialize */ 286397d8d152SAndre Oppermann #ifdef INET6 286497d8d152SAndre Oppermann if (isipv6) { 286597d8d152SAndre Oppermann maxmtu = tcp_maxmtu6(&inp->inp_inc); 286697d8d152SAndre Oppermann tp->t_maxopd = tp->t_maxseg = tcp_v6mssdflt; 286797d8d152SAndre Oppermann } else 286897d8d152SAndre Oppermann #endif 286997d8d152SAndre Oppermann { 287097d8d152SAndre Oppermann maxmtu = tcp_maxmtu(&inp->inp_inc); 287197d8d152SAndre Oppermann tp->t_maxopd = tp->t_maxseg = tcp_mssdflt; 2872df8bae1dSRodney W. Grimes } 2873df8bae1dSRodney W. Grimes so = inp->inp_socket; 2874df8bae1dSRodney W. Grimes 287597d8d152SAndre Oppermann /* 28762d166c02SAndre Oppermann * no route to sender, stay with default mss and return 287797d8d152SAndre Oppermann */ 287897d8d152SAndre Oppermann if (maxmtu == 0) 287997d8d152SAndre Oppermann return; 288097d8d152SAndre Oppermann 288197d8d152SAndre Oppermann /* what have we got? */ 288297d8d152SAndre Oppermann switch (offer) { 288397d8d152SAndre Oppermann case 0: 288497d8d152SAndre Oppermann /* 288597d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 288697d8d152SAndre Oppermann * segment, in this case we use tcp_mssdflt. 288797d8d152SAndre Oppermann */ 288897d8d152SAndre Oppermann offer = 288997d8d152SAndre Oppermann #ifdef INET6 289097d8d152SAndre Oppermann isipv6 ? tcp_v6mssdflt : 289197d8d152SAndre Oppermann #endif 289297d8d152SAndre Oppermann tcp_mssdflt; 289397d8d152SAndre Oppermann break; 289497d8d152SAndre Oppermann 289597d8d152SAndre Oppermann case -1: 2896a0292f23SGarrett Wollman /* 2897a0292f23SGarrett Wollman * Offer == -1 means that we didn't receive SYN yet, 2898a0292f23SGarrett Wollman * use cached value in that case; 2899a0292f23SGarrett Wollman */ 290097d8d152SAndre Oppermann if (tcp_do_rfc1644) 290197d8d152SAndre Oppermann tcp_hc_gettao(&inp->inp_inc, &tao); 290297d8d152SAndre Oppermann if (tao.tao_mssopt != 0) 290397d8d152SAndre Oppermann offer = tao.tao_mssopt; 290497d8d152SAndre Oppermann /* FALLTHROUGH */ 290597d8d152SAndre Oppermann 290697d8d152SAndre Oppermann default: 2907a0292f23SGarrett Wollman /* 290853369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 290953369ac9SAndre Oppermann * to at least minmss. 291053369ac9SAndre Oppermann */ 291153369ac9SAndre Oppermann offer = max(offer, tcp_minmss); 291253369ac9SAndre Oppermann /* 2913a0292f23SGarrett Wollman * Sanity check: make sure that maxopd will be large 291497d8d152SAndre Oppermann * enough to allow some data on segments even if the 2915a0292f23SGarrett Wollman * all the option space is used (40bytes). Otherwise 2916a0292f23SGarrett Wollman * funny things may happen in tcp_output. 2917a0292f23SGarrett Wollman */ 2918a0292f23SGarrett Wollman offer = max(offer, 64); 291997d8d152SAndre Oppermann if (tcp_do_rfc1644) 292097d8d152SAndre Oppermann tcp_hc_updatetao(&inp->inp_inc, 292197d8d152SAndre Oppermann TCP_HC_TAO_MSSOPT, 0, offer); 292297d8d152SAndre Oppermann } 2923a0292f23SGarrett Wollman 2924df8bae1dSRodney W. Grimes /* 292597d8d152SAndre Oppermann * rmx information is now retrieved from tcp_hostcache 2926df8bae1dSRodney W. Grimes */ 292797d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 292897d8d152SAndre Oppermann 2929df8bae1dSRodney W. Grimes /* 293097d8d152SAndre Oppermann * if there's a discovered mtu int tcp hostcache, use it 2931fb59c426SYoshinobu Inoue * else, use the link mtu. 2932df8bae1dSRodney W. Grimes */ 293397d8d152SAndre Oppermann if (metrics.rmx_mtu) 29342d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 2935c068736aSJeffrey Hsu else { 293631b3783cSHajimu UMEMOTO #ifdef INET6 2937fb59c426SYoshinobu Inoue if (isipv6) { 293897d8d152SAndre Oppermann mss = maxmtu - min_protoh; 293997d8d152SAndre Oppermann if (!path_mtu_discovery && 294097d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 2941fb59c426SYoshinobu Inoue mss = min(mss, tcp_v6mssdflt); 2942b3399803SHajimu UMEMOTO } else 2943b3399803SHajimu UMEMOTO #endif 294497d8d152SAndre Oppermann { 294597d8d152SAndre Oppermann mss = maxmtu - min_protoh; 294697d8d152SAndre Oppermann if (!path_mtu_discovery && 294797d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 2948a0292f23SGarrett Wollman mss = min(mss, tcp_mssdflt); 2949a0292f23SGarrett Wollman } 295097d8d152SAndre Oppermann } 2951a0292f23SGarrett Wollman mss = min(mss, offer); 295297d8d152SAndre Oppermann 2953a0292f23SGarrett Wollman /* 2954a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 2955a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 2956a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 2957a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 2958a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 2959a0292f23SGarrett Wollman */ 2960a0292f23SGarrett Wollman tp->t_maxopd = mss; 2961a0292f23SGarrett Wollman 2962a0292f23SGarrett Wollman /* 2963a0292f23SGarrett Wollman * In case of T/TCP, origoffer==-1 indicates, that no segments 2964a0292f23SGarrett Wollman * were received yet. In this case we just guess, otherwise 2965a0292f23SGarrett Wollman * we do the same as before T/TCP. 2966a0292f23SGarrett Wollman */ 2967a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2968a0292f23SGarrett Wollman (origoffer == -1 || 2969a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2970a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 2971a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 2972a0292f23SGarrett Wollman (origoffer == -1 || 2973a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)) 2974a0292f23SGarrett Wollman mss -= TCPOLEN_CC_APPA; 297597d8d152SAndre Oppermann tp->t_maxseg = mss; 2976a0292f23SGarrett Wollman 2977df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 2978df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2979df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 2980df8bae1dSRodney W. Grimes #else 2981df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2982df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 2983df8bae1dSRodney W. Grimes #endif 298497d8d152SAndre Oppermann tp->t_maxseg = mss; 298597d8d152SAndre Oppermann 2986df8bae1dSRodney W. Grimes /* 298797d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 298897d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 298997d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 299097d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 299197d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 2992df8bae1dSRodney W. Grimes */ 299397d8d152SAndre Oppermann if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe) 299497d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 299597d8d152SAndre Oppermann else 2996df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 2997df8bae1dSRodney W. Grimes if (bufsize < mss) 2998df8bae1dSRodney W. Grimes mss = bufsize; 2999df8bae1dSRodney W. Grimes else { 3000df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3001df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3002df8bae1dSRodney W. Grimes bufsize = sb_max; 300388c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 3004ecf72308SBrian Feldman (void)sbreserve(&so->so_snd, bufsize, so, NULL); 3005df8bae1dSRodney W. Grimes } 3006df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 3007df8bae1dSRodney W. Grimes 300897d8d152SAndre Oppermann if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe) 300997d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 301097d8d152SAndre Oppermann else 3011df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3012df8bae1dSRodney W. Grimes if (bufsize > mss) { 3013df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3014df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3015df8bae1dSRodney W. Grimes bufsize = sb_max; 301688c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 3017ecf72308SBrian Feldman (void)sbreserve(&so->so_rcv, bufsize, so, NULL); 3018df8bae1dSRodney W. Grimes } 3019a0292f23SGarrett Wollman /* 302097d8d152SAndre Oppermann * While we're here, check the others too 3021a0292f23SGarrett Wollman */ 302297d8d152SAndre Oppermann if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 302397d8d152SAndre Oppermann tp->t_srtt = rtt; 302497d8d152SAndre Oppermann tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 302597d8d152SAndre Oppermann tcpstat.tcps_usedrtt++; 302697d8d152SAndre Oppermann if (metrics.rmx_rttvar) { 302797d8d152SAndre Oppermann tp->t_rttvar = metrics.rmx_rttvar; 302897d8d152SAndre Oppermann tcpstat.tcps_usedrttvar++; 302997d8d152SAndre Oppermann } else { 303097d8d152SAndre Oppermann /* default variation is +- 1 rtt */ 303197d8d152SAndre Oppermann tp->t_rttvar = 303297d8d152SAndre Oppermann tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 303397d8d152SAndre Oppermann } 303497d8d152SAndre Oppermann TCPT_RANGESET(tp->t_rxtcur, 303597d8d152SAndre Oppermann ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 303697d8d152SAndre Oppermann tp->t_rttmin, TCPTV_REXMTMAX); 303797d8d152SAndre Oppermann } 303897d8d152SAndre Oppermann if (metrics.rmx_ssthresh) { 3039df8bae1dSRodney W. Grimes /* 3040df8bae1dSRodney W. Grimes * There's some sort of gateway or interface 3041df8bae1dSRodney W. Grimes * buffer limit on the path. Use this to set 3042df8bae1dSRodney W. Grimes * the slow start threshhold, but set the 3043df8bae1dSRodney W. Grimes * threshold to no less than 2*mss. 3044df8bae1dSRodney W. Grimes */ 304597d8d152SAndre Oppermann tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh); 3046dd224982SGarrett Wollman tcpstat.tcps_usedssthresh++; 3047df8bae1dSRodney W. Grimes } 304897d8d152SAndre Oppermann if (metrics.rmx_bandwidth) 304997d8d152SAndre Oppermann tp->snd_bandwidth = metrics.rmx_bandwidth; 305097d8d152SAndre Oppermann 305197d8d152SAndre Oppermann /* 305297d8d152SAndre Oppermann * Set the slow-start flight size depending on whether this 305397d8d152SAndre Oppermann * is a local network or not. 305497d8d152SAndre Oppermann * 305597d8d152SAndre Oppermann * Extend this so we cache the cwnd too and retrieve it here. 305697d8d152SAndre Oppermann * Make cwnd even bigger than RFC3390 suggests but only if we 305797d8d152SAndre Oppermann * have previous experience with the remote host. Be careful 305897d8d152SAndre Oppermann * not make cwnd bigger than remote receive window or our own 305997d8d152SAndre Oppermann * send socket buffer. Maybe put some additional upper bound 306097d8d152SAndre Oppermann * on the retrieved cwnd. Should do incremental updates to 306197d8d152SAndre Oppermann * hostcache when cwnd collapses so next connection doesn't 306297d8d152SAndre Oppermann * overloads the path again. 306397d8d152SAndre Oppermann * 306497d8d152SAndre Oppermann * RFC3390 says only do this if SYN or SYN/ACK didn't got lost. 306597d8d152SAndre Oppermann * We currently check only in syncache_socket for that. 306697d8d152SAndre Oppermann */ 306797d8d152SAndre Oppermann #define TCP_METRICS_CWND 306897d8d152SAndre Oppermann #ifdef TCP_METRICS_CWND 306997d8d152SAndre Oppermann if (metrics.rmx_cwnd) 307097d8d152SAndre Oppermann tp->snd_cwnd = max(mss, 307197d8d152SAndre Oppermann min(metrics.rmx_cwnd / 2, 307297d8d152SAndre Oppermann min(tp->snd_wnd, so->so_snd.sb_hiwat))); 307397d8d152SAndre Oppermann else 307497d8d152SAndre Oppermann #endif 307597d8d152SAndre Oppermann if (tcp_do_rfc3390) 307697d8d152SAndre Oppermann tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380)); 307797d8d152SAndre Oppermann #ifdef INET6 307897d8d152SAndre Oppermann else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) || 307997d8d152SAndre Oppermann (!isipv6 && in_localaddr(inp->inp_faddr))) 3080943ae302SAndre Oppermann #else 3081943ae302SAndre Oppermann else if (in_localaddr(inp->inp_faddr)) 308297d8d152SAndre Oppermann #endif 3083943ae302SAndre Oppermann tp->snd_cwnd = mss * ss_fltsz_local; 308497d8d152SAndre Oppermann else 308597d8d152SAndre Oppermann tp->snd_cwnd = mss * ss_fltsz; 3086a0292f23SGarrett Wollman } 3087a0292f23SGarrett Wollman 3088a0292f23SGarrett Wollman /* 3089a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3090a0292f23SGarrett Wollman */ 3091a0292f23SGarrett Wollman int 309297d8d152SAndre Oppermann tcp_mssopt(inc) 309397d8d152SAndre Oppermann struct in_conninfo *inc; 3094a0292f23SGarrett Wollman { 309597d8d152SAndre Oppermann int mss = 0; 309697d8d152SAndre Oppermann u_long maxmtu = 0; 309797d8d152SAndre Oppermann u_long thcmtu = 0; 309897d8d152SAndre Oppermann size_t min_protoh; 3099fb59c426SYoshinobu Inoue #ifdef INET6 310097d8d152SAndre Oppermann int isipv6 = inc->inc_isipv6 ? 1 : 0; 3101fb59c426SYoshinobu Inoue #endif 3102a0292f23SGarrett Wollman 310397d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3104a0292f23SGarrett Wollman 310531b3783cSHajimu UMEMOTO #ifdef INET6 310697d8d152SAndre Oppermann if (isipv6) { 310797d8d152SAndre Oppermann mss = tcp_v6mssdflt; 310897d8d152SAndre Oppermann maxmtu = tcp_maxmtu6(inc); 310997d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 311097d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 311197d8d152SAndre Oppermann } else 311231b3783cSHajimu UMEMOTO #endif 311397d8d152SAndre Oppermann { 311497d8d152SAndre Oppermann mss = tcp_mssdflt; 311597d8d152SAndre Oppermann maxmtu = tcp_maxmtu(inc); 311697d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 311797d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 311897d8d152SAndre Oppermann } 311997d8d152SAndre Oppermann if (maxmtu && thcmtu) 312097d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 312197d8d152SAndre Oppermann else if (maxmtu || thcmtu) 312297d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 312397d8d152SAndre Oppermann 312497d8d152SAndre Oppermann return (mss); 3125df8bae1dSRodney W. Grimes } 312646f58482SJonathan Lemon 312746f58482SJonathan Lemon 312846f58482SJonathan Lemon /* 3129c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3130c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3131c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3132c068736aSJeffrey Hsu * be started again. 313346f58482SJonathan Lemon */ 3134c068736aSJeffrey Hsu static void 3135c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th) 313646f58482SJonathan Lemon struct tcpcb *tp; 313746f58482SJonathan Lemon struct tcphdr *th; 313846f58482SJonathan Lemon { 313946f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 314046f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 314146f58482SJonathan Lemon 314246f58482SJonathan Lemon callout_stop(tp->tt_rexmt); 314346f58482SJonathan Lemon tp->t_rtttime = 0; 314446f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 31456b2a5f92SJayanth Vijayaraghavan /* 3146c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3147c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 31486b2a5f92SJayanth Vijayaraghavan */ 31496b2a5f92SJayanth Vijayaraghavan tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 3150a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 31516b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 315246f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 315346f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 315446f58482SJonathan Lemon tp->snd_nxt = onxt; 315546f58482SJonathan Lemon /* 315646f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 315746f58482SJonathan Lemon * not updated yet. 315846f58482SJonathan Lemon */ 315946f58482SJonathan Lemon tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg); 316046f58482SJonathan Lemon } 3161340c35deSJonathan Lemon 3162340c35deSJonathan Lemon /* 3163340c35deSJonathan Lemon * Returns 1 if the TIME_WAIT state was killed and we should start over, 3164340c35deSJonathan Lemon * looking for a pcb in the listen state. Returns 0 otherwise. 3165340c35deSJonathan Lemon */ 3166340c35deSJonathan Lemon static int 3167340c35deSJonathan Lemon tcp_timewait(tw, to, th, m, tlen) 3168340c35deSJonathan Lemon struct tcptw *tw; 3169340c35deSJonathan Lemon struct tcpopt *to; 3170340c35deSJonathan Lemon struct tcphdr *th; 3171340c35deSJonathan Lemon struct mbuf *m; 3172340c35deSJonathan Lemon int tlen; 3173340c35deSJonathan Lemon { 3174340c35deSJonathan Lemon int thflags; 3175340c35deSJonathan Lemon tcp_seq seq; 3176340c35deSJonathan Lemon #ifdef INET6 3177340c35deSJonathan Lemon int isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 3178340c35deSJonathan Lemon #else 3179340c35deSJonathan Lemon const int isipv6 = 0; 3180340c35deSJonathan Lemon #endif 3181340c35deSJonathan Lemon 3182340c35deSJonathan Lemon thflags = th->th_flags; 3183340c35deSJonathan Lemon 3184340c35deSJonathan Lemon /* 3185340c35deSJonathan Lemon * NOTE: for FIN_WAIT_2 (to be added later), 3186340c35deSJonathan Lemon * must validate sequence number before accepting RST 3187340c35deSJonathan Lemon */ 3188340c35deSJonathan Lemon 3189340c35deSJonathan Lemon /* 3190340c35deSJonathan Lemon * If the segment contains RST: 3191340c35deSJonathan Lemon * Drop the segment - see Stevens, vol. 2, p. 964 and 3192340c35deSJonathan Lemon * RFC 1337. 3193340c35deSJonathan Lemon */ 3194340c35deSJonathan Lemon if (thflags & TH_RST) 3195340c35deSJonathan Lemon goto drop; 3196340c35deSJonathan Lemon 3197340c35deSJonathan Lemon /* 3198340c35deSJonathan Lemon * If segment contains a SYN and CC [not CC.NEW] option: 3199340c35deSJonathan Lemon * if connection duration > MSL, drop packet and send RST; 3200340c35deSJonathan Lemon * 3201340c35deSJonathan Lemon * if SEG.CC > CCrecv then is new SYN. 3202340c35deSJonathan Lemon * Complete close and delete TCPCB. Then reprocess 3203340c35deSJonathan Lemon * segment, hoping to find new TCPCB in LISTEN state; 3204340c35deSJonathan Lemon * 3205340c35deSJonathan Lemon * else must be old SYN; drop it. 3206340c35deSJonathan Lemon * else do normal processing. 3207340c35deSJonathan Lemon */ 3208340c35deSJonathan Lemon if ((thflags & TH_SYN) && (to->to_flags & TOF_CC) && tw->cc_recv != 0) { 3209340c35deSJonathan Lemon if ((ticks - tw->t_starttime) > tcp_msl) 3210340c35deSJonathan Lemon goto reset; 3211340c35deSJonathan Lemon if (CC_GT(to->to_cc, tw->cc_recv)) { 3212607b0b0cSJonathan Lemon (void) tcp_twclose(tw, 0); 3213340c35deSJonathan Lemon return (1); 3214340c35deSJonathan Lemon } 3215340c35deSJonathan Lemon goto drop; 3216340c35deSJonathan Lemon } 3217340c35deSJonathan Lemon 3218340c35deSJonathan Lemon #if 0 3219340c35deSJonathan Lemon /* PAWS not needed at the moment */ 3220340c35deSJonathan Lemon /* 3221340c35deSJonathan Lemon * RFC 1323 PAWS: If we have a timestamp reply on this segment 3222340c35deSJonathan Lemon * and it's less than ts_recent, drop it. 3223340c35deSJonathan Lemon */ 3224340c35deSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 3225340c35deSJonathan Lemon TSTMP_LT(to.to_tsval, tp->ts_recent)) { 3226340c35deSJonathan Lemon if ((thflags & TH_ACK) == 0) 3227340c35deSJonathan Lemon goto drop; 3228340c35deSJonathan Lemon goto ack; 3229340c35deSJonathan Lemon } 3230340c35deSJonathan Lemon /* 3231340c35deSJonathan Lemon * ts_recent is never updated because we never accept new segments. 3232340c35deSJonathan Lemon */ 3233340c35deSJonathan Lemon #endif 3234340c35deSJonathan Lemon 3235340c35deSJonathan Lemon /* 3236340c35deSJonathan Lemon * If a new connection request is received 3237340c35deSJonathan Lemon * while in TIME_WAIT, drop the old connection 3238340c35deSJonathan Lemon * and start over if the sequence numbers 3239340c35deSJonathan Lemon * are above the previous ones. 3240340c35deSJonathan Lemon */ 3241340c35deSJonathan Lemon if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) { 3242607b0b0cSJonathan Lemon (void) tcp_twclose(tw, 0); 3243340c35deSJonathan Lemon return (1); 3244340c35deSJonathan Lemon } 3245340c35deSJonathan Lemon 3246340c35deSJonathan Lemon /* 3247340c35deSJonathan Lemon * Drop the the segment if it does not contain an ACK. 3248340c35deSJonathan Lemon */ 3249340c35deSJonathan Lemon if ((thflags & TH_ACK) == 0) 3250340c35deSJonathan Lemon goto drop; 3251340c35deSJonathan Lemon 3252340c35deSJonathan Lemon /* 3253340c35deSJonathan Lemon * Reset the 2MSL timer if this is a duplicate FIN. 3254340c35deSJonathan Lemon */ 3255340c35deSJonathan Lemon if (thflags & TH_FIN) { 3256340c35deSJonathan Lemon seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0); 3257340c35deSJonathan Lemon if (seq + 1 == tw->rcv_nxt) 3258607b0b0cSJonathan Lemon tcp_timer_2msl_reset(tw, 2 * tcp_msl); 3259340c35deSJonathan Lemon } 3260340c35deSJonathan Lemon 3261340c35deSJonathan Lemon /* 3262272c5dfeSJonathan Lemon * Acknowledge the segment if it has data or is not a duplicate ACK. 3263340c35deSJonathan Lemon */ 3264272c5dfeSJonathan Lemon if (thflags != TH_ACK || tlen != 0 || 3265272c5dfeSJonathan Lemon th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt) 3266a7b6a14aSRobert Watson tcp_twrespond(tw, TH_ACK); 3267340c35deSJonathan Lemon goto drop; 3268340c35deSJonathan Lemon 3269340c35deSJonathan Lemon reset: 3270340c35deSJonathan Lemon /* 3271340c35deSJonathan Lemon * Generate a RST, dropping incoming segment. 3272340c35deSJonathan Lemon * Make ACK acceptable to originator of segment. 3273340c35deSJonathan Lemon * Don't bother to respond if destination was broadcast/multicast. 3274340c35deSJonathan Lemon */ 3275340c35deSJonathan Lemon if (m->m_flags & (M_BCAST|M_MCAST)) 3276340c35deSJonathan Lemon goto drop; 3277340c35deSJonathan Lemon if (isipv6) { 3278340c35deSJonathan Lemon struct ip6_hdr *ip6; 3279340c35deSJonathan Lemon 3280340c35deSJonathan Lemon /* IPv6 anycast check is done at tcp6_input() */ 3281340c35deSJonathan Lemon ip6 = mtod(m, struct ip6_hdr *); 3282340c35deSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3283340c35deSJonathan Lemon IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3284340c35deSJonathan Lemon goto drop; 3285340c35deSJonathan Lemon } else { 3286340c35deSJonathan Lemon struct ip *ip; 3287340c35deSJonathan Lemon 3288340c35deSJonathan Lemon ip = mtod(m, struct ip *); 3289340c35deSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3290340c35deSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3291340c35deSJonathan Lemon ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3292340c35deSJonathan Lemon in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3293340c35deSJonathan Lemon goto drop; 3294340c35deSJonathan Lemon } 3295340c35deSJonathan Lemon if (thflags & TH_ACK) { 3296340c35deSJonathan Lemon tcp_respond(NULL, 3297340c35deSJonathan Lemon mtod(m, void *), th, m, 0, th->th_ack, TH_RST); 3298340c35deSJonathan Lemon } else { 3299340c35deSJonathan Lemon seq = th->th_seq + (thflags & TH_SYN ? 1 : 0); 3300340c35deSJonathan Lemon tcp_respond(NULL, 3301340c35deSJonathan Lemon mtod(m, void *), th, m, seq, 0, TH_RST|TH_ACK); 3302340c35deSJonathan Lemon } 3303340c35deSJonathan Lemon INP_UNLOCK(tw->tw_inpcb); 3304340c35deSJonathan Lemon return (0); 3305340c35deSJonathan Lemon 3306340c35deSJonathan Lemon drop: 3307340c35deSJonathan Lemon INP_UNLOCK(tw->tw_inpcb); 3308340c35deSJonathan Lemon m_freem(m); 3309340c35deSJonathan Lemon return (0); 3310340c35deSJonathan Lemon } 3311