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; 1010312fbe9SPoul-Henning Kamp 1022f96f1f4SGarrett Wollman struct tcpstat tcpstat; 103c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW, 1043d177f46SBill Fumerola &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 1050312fbe9SPoul-Henning Kamp 106d78a37adSPaul Traina static int log_in_vain = 0; 107816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 1083d177f46SBill Fumerola &log_in_vain, 0, "Log all incoming TCP connections"); 109816a3d83SPoul-Henning Kamp 11016f7f31fSGeoff Rehmet static int blackhole = 0; 11116f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 11216f7f31fSGeoff Rehmet &blackhole, 0, "Do not send RST when dropping refused connections"); 11316f7f31fSGeoff Rehmet 114f498eeeeSDavid Greenman int tcp_delack_enabled = 1; 11584e33c9eSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 1163d177f46SBill Fumerola &tcp_delack_enabled, 0, 1173d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 118f498eeeeSDavid Greenman 119f8613305SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN 120f8613305SDag-Erling Smørgrav static int drop_synfin = 0; 121f8613305SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, 122f8613305SDag-Erling Smørgrav &drop_synfin, 0, "Drop TCP packets with SYN+FIN set"); 123f8613305SDag-Erling Smørgrav #endif 124f8613305SDag-Erling Smørgrav 125dba7bc6aSAndre Oppermann static int tcp_do_rfc3042 = 1; 126582a954bSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW, 127582a954bSJeffrey Hsu &tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)"); 128582a954bSJeffrey Hsu 129dba7bc6aSAndre Oppermann static int tcp_do_rfc3390 = 1; 130da3a8a1aSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW, 131da3a8a1aSJeffrey Hsu &tcp_do_rfc3390, 0, 132da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 133da3a8a1aSJeffrey Hsu 13412e2e970SAndre Oppermann SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0, 13512e2e970SAndre Oppermann "TCP Segment Reassembly Queue"); 13612e2e970SAndre Oppermann 13712e2e970SAndre Oppermann static int tcp_reass_maxseg = 0; 13812e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RDTUN, 13912e2e970SAndre Oppermann &tcp_reass_maxseg, 0, 14012e2e970SAndre Oppermann "Global maximum number of TCP Segments in Reassembly Queue"); 14112e2e970SAndre Oppermann 14212e2e970SAndre Oppermann int tcp_reass_qsize = 0; 14312e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, cursegments, CTLFLAG_RD, 14412e2e970SAndre Oppermann &tcp_reass_qsize, 0, 14512e2e970SAndre Oppermann "Global number of TCP Segments currently in Reassembly Queue"); 14612e2e970SAndre Oppermann 14712e2e970SAndre Oppermann static int tcp_reass_maxqlen = 48; 14812e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxqlen, CTLFLAG_RW, 14912e2e970SAndre Oppermann &tcp_reass_maxqlen, 0, 15012e2e970SAndre Oppermann "Maximum number of TCP Segments per individual Reassembly Queue"); 15112e2e970SAndre Oppermann 15212e2e970SAndre Oppermann static int tcp_reass_overflows = 0; 15312e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, overflows, CTLFLAG_RD, 15412e2e970SAndre Oppermann &tcp_reass_overflows, 0, 15512e2e970SAndre Oppermann "Global number of TCP Segment Reassembly Queue Overflows"); 15612e2e970SAndre Oppermann 157a55db2b6SPaul Saab static int tcp_sack_recovery_initburst = 3; 158a55db2b6SPaul Saab SYSCTL_INT(_net_inet_tcp_sack, OID_AUTO, 159a55db2b6SPaul Saab initburst, CTLFLAG_RW, 160a55db2b6SPaul Saab &tcp_sack_recovery_initburst, 0, 161a55db2b6SPaul Saab "Initial Number of Rexmits when sack recovery is set up"); 162a55db2b6SPaul Saab 16315bd2b43SDavid Greenman struct inpcbhead tcb; 164fb59c426SYoshinobu Inoue #define tcb6 tcb /* for KAME src sync over BSD*'s */ 16515bd2b43SDavid Greenman struct inpcbinfo tcbinfo; 166f76fcf6dSJeffrey Hsu struct mtx *tcbinfo_mtx; 167df8bae1dSRodney W. Grimes 1686d90faf3SPaul Saab static void tcp_dooptions(struct tcpcb *, struct tcpopt *, u_char *, 1696d90faf3SPaul Saab int, int, struct tcphdr *); 1706d90faf3SPaul Saab 1714d77a549SAlfred Perlstein static void tcp_pulloutofband(struct socket *, 1724d77a549SAlfred Perlstein struct tcphdr *, struct mbuf *, int); 1734d77a549SAlfred Perlstein static int tcp_reass(struct tcpcb *, struct tcphdr *, int *, 1744d77a549SAlfred Perlstein struct mbuf *); 1754d77a549SAlfred Perlstein static void tcp_xmit_timer(struct tcpcb *, int); 176c068736aSJeffrey Hsu static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 177340c35deSJonathan Lemon static int tcp_timewait(struct tcptw *, struct tcpopt *, 178340c35deSJonathan Lemon struct tcphdr *, struct mbuf *, int); 1790312fbe9SPoul-Henning Kamp 180fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 181fb59c426SYoshinobu Inoue #ifdef INET6 182fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \ 183fb59c426SYoshinobu Inoue do { \ 184fb59c426SYoshinobu Inoue if ((tp) && (tp)->t_inpcb && \ 18597d8d152SAndre Oppermann ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \ 18697d8d152SAndre Oppermann nd6_nud_hint(NULL, NULL, 0); \ 187fb59c426SYoshinobu Inoue } while (0) 188fb59c426SYoshinobu Inoue #else 189fb59c426SYoshinobu Inoue #define ND6_HINT(tp) 190fb59c426SYoshinobu Inoue #endif 191df8bae1dSRodney W. Grimes 192df8bae1dSRodney W. Grimes /* 193262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 194262c1c1aSMatthew Dillon * - there is no delayed ack timer in progress and 195262c1c1aSMatthew Dillon * - our last ack wasn't a 0-sized window. We never want to delay 1963bfd6421SJonathan Lemon * the ack that opens up a 0-sized window and 1973bfd6421SJonathan Lemon * - delayed acks are enabled or 1983bfd6421SJonathan Lemon * - this is a half-synchronized T/TCP connection. 199d8c85a26SJonathan Lemon */ 200d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \ 201a14c749fSJonathan Lemon ((!callout_active(tp->tt_delack) && \ 202a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 2033bfd6421SJonathan Lemon (tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 204d8c85a26SJonathan Lemon 20512e2e970SAndre Oppermann /* Initialize TCP reassembly queue */ 20612e2e970SAndre Oppermann uma_zone_t tcp_reass_zone; 20712e2e970SAndre Oppermann void 20812e2e970SAndre Oppermann tcp_reass_init() 20912e2e970SAndre Oppermann { 21012e2e970SAndre Oppermann tcp_reass_maxseg = nmbclusters / 16; 21112e2e970SAndre Oppermann TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments", 21212e2e970SAndre Oppermann &tcp_reass_maxseg); 21312e2e970SAndre Oppermann tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent), 21412e2e970SAndre Oppermann NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 21512e2e970SAndre Oppermann uma_zone_set_max(tcp_reass_zone, tcp_reass_maxseg); 21612e2e970SAndre Oppermann } 21712e2e970SAndre Oppermann 2180312fbe9SPoul-Henning Kamp static int 219fb59c426SYoshinobu Inoue tcp_reass(tp, th, tlenp, m) 220df8bae1dSRodney W. Grimes register struct tcpcb *tp; 221fb59c426SYoshinobu Inoue register struct tcphdr *th; 222fb59c426SYoshinobu Inoue int *tlenp; 223df8bae1dSRodney W. Grimes struct mbuf *m; 224df8bae1dSRodney W. Grimes { 225fb59c426SYoshinobu Inoue struct tseg_qent *q; 226fb59c426SYoshinobu Inoue struct tseg_qent *p = NULL; 227fb59c426SYoshinobu Inoue struct tseg_qent *nq; 22812e2e970SAndre Oppermann struct tseg_qent *te = NULL; 229df8bae1dSRodney W. Grimes struct socket *so = tp->t_inpcb->inp_socket; 230df8bae1dSRodney W. Grimes int flags; 231df8bae1dSRodney W. Grimes 232df8bae1dSRodney W. Grimes /* 23312e2e970SAndre Oppermann * XXX: tcp_reass() is rather inefficient with its data structures 23412e2e970SAndre Oppermann * and should be rewritten (see NetBSD for optimizations). While 23512e2e970SAndre Oppermann * doing that it should move to its own file tcp_reass.c. 23612e2e970SAndre Oppermann */ 23712e2e970SAndre Oppermann 23812e2e970SAndre Oppermann /* 239fb59c426SYoshinobu Inoue * Call with th==0 after become established to 240df8bae1dSRodney W. Grimes * force pre-ESTABLISHED data up to user socket. 241df8bae1dSRodney W. Grimes */ 242fb59c426SYoshinobu Inoue if (th == 0) 243df8bae1dSRodney W. Grimes goto present; 244df8bae1dSRodney W. Grimes 24512e2e970SAndre Oppermann /* 24612e2e970SAndre Oppermann * Limit the number of segments in the reassembly queue to prevent 24712e2e970SAndre Oppermann * holding on to too many segments (and thus running out of mbufs). 24812e2e970SAndre Oppermann * Make sure to let the missing segment through which caused this 24912e2e970SAndre Oppermann * queue. Always keep one global queue entry spare to be able to 25012e2e970SAndre Oppermann * process the missing segment. 25112e2e970SAndre Oppermann */ 25212e2e970SAndre Oppermann if (th->th_seq != tp->rcv_nxt && 25312e2e970SAndre Oppermann (tcp_reass_qsize + 1 >= tcp_reass_maxseg || 25412e2e970SAndre Oppermann tp->t_segqlen >= tcp_reass_maxqlen)) { 25512e2e970SAndre Oppermann tcp_reass_overflows++; 25612e2e970SAndre Oppermann tcpstat.tcps_rcvmemdrop++; 25712e2e970SAndre Oppermann m_freem(m); 25812e2e970SAndre Oppermann return (0); 25912e2e970SAndre Oppermann } 26012e2e970SAndre Oppermann 26112e2e970SAndre Oppermann /* 26212e2e970SAndre Oppermann * Allocate a new queue entry. If we can't, or hit the zone limit 26312e2e970SAndre Oppermann * just drop the pkt. 26412e2e970SAndre Oppermann */ 26512e2e970SAndre Oppermann te = uma_zalloc(tcp_reass_zone, M_NOWAIT); 266fb59c426SYoshinobu Inoue if (te == NULL) { 267fb59c426SYoshinobu Inoue tcpstat.tcps_rcvmemdrop++; 268fb59c426SYoshinobu Inoue m_freem(m); 269fb59c426SYoshinobu Inoue return (0); 270fb59c426SYoshinobu Inoue } 27112e2e970SAndre Oppermann tp->t_segqlen++; 27212e2e970SAndre Oppermann tcp_reass_qsize++; 2736effc713SDoug Rabson 274df8bae1dSRodney W. Grimes /* 275df8bae1dSRodney W. Grimes * Find a segment which begins after this one does. 276df8bae1dSRodney W. Grimes */ 277fb59c426SYoshinobu Inoue LIST_FOREACH(q, &tp->t_segq, tqe_q) { 278fb59c426SYoshinobu Inoue if (SEQ_GT(q->tqe_th->th_seq, th->th_seq)) 279df8bae1dSRodney W. Grimes break; 280fb59c426SYoshinobu Inoue p = q; 281fb59c426SYoshinobu Inoue } 282df8bae1dSRodney W. Grimes 283df8bae1dSRodney W. Grimes /* 284df8bae1dSRodney W. Grimes * If there is a preceding segment, it may provide some of 285df8bae1dSRodney W. Grimes * our data already. If so, drop the data from the incoming 286df8bae1dSRodney W. Grimes * segment. If it provides all of our data, drop us. 287df8bae1dSRodney W. Grimes */ 2886effc713SDoug Rabson if (p != NULL) { 289df8bae1dSRodney W. Grimes register int i; 290df8bae1dSRodney W. Grimes /* conversion to int (in i) handles seq wraparound */ 291fb59c426SYoshinobu Inoue i = p->tqe_th->th_seq + p->tqe_len - th->th_seq; 292df8bae1dSRodney W. Grimes if (i > 0) { 293fb59c426SYoshinobu Inoue if (i >= *tlenp) { 294df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 295fb59c426SYoshinobu Inoue tcpstat.tcps_rcvdupbyte += *tlenp; 296df8bae1dSRodney W. Grimes m_freem(m); 29712e2e970SAndre Oppermann uma_zfree(tcp_reass_zone, te); 29812e2e970SAndre Oppermann tp->t_segqlen--; 29912e2e970SAndre Oppermann tcp_reass_qsize--; 300a0292f23SGarrett Wollman /* 301a0292f23SGarrett Wollman * Try to present any queued data 302a0292f23SGarrett Wollman * at the left window edge to the user. 303a0292f23SGarrett Wollman * This is needed after the 3-WHS 304a0292f23SGarrett Wollman * completes. 305a0292f23SGarrett Wollman */ 306a0292f23SGarrett Wollman goto present; /* ??? */ 307df8bae1dSRodney W. Grimes } 308df8bae1dSRodney W. Grimes m_adj(m, i); 309fb59c426SYoshinobu Inoue *tlenp -= i; 310fb59c426SYoshinobu Inoue th->th_seq += i; 311df8bae1dSRodney W. Grimes } 312df8bae1dSRodney W. Grimes } 313df8bae1dSRodney W. Grimes tcpstat.tcps_rcvoopack++; 314fb59c426SYoshinobu Inoue tcpstat.tcps_rcvoobyte += *tlenp; 315df8bae1dSRodney W. Grimes 316df8bae1dSRodney W. Grimes /* 317df8bae1dSRodney W. Grimes * While we overlap succeeding segments trim them or, 318df8bae1dSRodney W. Grimes * if they are completely covered, dequeue them. 319df8bae1dSRodney W. Grimes */ 3206effc713SDoug Rabson while (q) { 321fb59c426SYoshinobu Inoue register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq; 322df8bae1dSRodney W. Grimes if (i <= 0) 323df8bae1dSRodney W. Grimes break; 324fb59c426SYoshinobu Inoue if (i < q->tqe_len) { 325fb59c426SYoshinobu Inoue q->tqe_th->th_seq += i; 326fb59c426SYoshinobu Inoue q->tqe_len -= i; 327fb59c426SYoshinobu Inoue m_adj(q->tqe_m, i); 328df8bae1dSRodney W. Grimes break; 329df8bae1dSRodney W. Grimes } 3306effc713SDoug Rabson 331fb59c426SYoshinobu Inoue nq = LIST_NEXT(q, tqe_q); 332fb59c426SYoshinobu Inoue LIST_REMOVE(q, tqe_q); 333fb59c426SYoshinobu Inoue m_freem(q->tqe_m); 33412e2e970SAndre Oppermann uma_zfree(tcp_reass_zone, q); 33512e2e970SAndre Oppermann tp->t_segqlen--; 33612e2e970SAndre Oppermann tcp_reass_qsize--; 3376effc713SDoug Rabson q = nq; 338df8bae1dSRodney W. Grimes } 339df8bae1dSRodney W. Grimes 340fb59c426SYoshinobu Inoue /* Insert the new segment queue entry into place. */ 341fb59c426SYoshinobu Inoue te->tqe_m = m; 342fb59c426SYoshinobu Inoue te->tqe_th = th; 343fb59c426SYoshinobu Inoue te->tqe_len = *tlenp; 344fb59c426SYoshinobu Inoue 3456effc713SDoug Rabson if (p == NULL) { 346fb59c426SYoshinobu Inoue LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q); 3476effc713SDoug Rabson } else { 348fb59c426SYoshinobu Inoue LIST_INSERT_AFTER(p, te, tqe_q); 3496effc713SDoug Rabson } 350df8bae1dSRodney W. Grimes 351df8bae1dSRodney W. Grimes present: 352df8bae1dSRodney W. Grimes /* 353df8bae1dSRodney W. Grimes * Present data to user, advancing rcv_nxt through 354df8bae1dSRodney W. Grimes * completed sequence space. 355df8bae1dSRodney W. Grimes */ 356a0292f23SGarrett Wollman if (!TCPS_HAVEESTABLISHED(tp->t_state)) 357df8bae1dSRodney W. Grimes return (0); 358fb59c426SYoshinobu Inoue q = LIST_FIRST(&tp->t_segq); 359fb59c426SYoshinobu Inoue if (!q || q->tqe_th->th_seq != tp->rcv_nxt) 360df8bae1dSRodney W. Grimes return (0); 3611e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 362df8bae1dSRodney W. Grimes do { 363fb59c426SYoshinobu Inoue tp->rcv_nxt += q->tqe_len; 364fb59c426SYoshinobu Inoue flags = q->tqe_th->th_flags & TH_FIN; 365fb59c426SYoshinobu Inoue nq = LIST_NEXT(q, tqe_q); 366fb59c426SYoshinobu Inoue LIST_REMOVE(q, tqe_q); 3671e4d7da7SRobert Watson /* Unlocked read. */ 368c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 369fb59c426SYoshinobu Inoue m_freem(q->tqe_m); 3704cc20ab1SSeigo Tanimura else 3711e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, q->tqe_m); 37212e2e970SAndre Oppermann uma_zfree(tcp_reass_zone, q); 37312e2e970SAndre Oppermann tp->t_segqlen--; 37412e2e970SAndre Oppermann tcp_reass_qsize--; 3756effc713SDoug Rabson q = nq; 376fb59c426SYoshinobu Inoue } while (q && q->tqe_th->th_seq == tp->rcv_nxt); 377fb59c426SYoshinobu Inoue ND6_HINT(tp); 3781e4d7da7SRobert Watson sorwakeup_locked(so); 379df8bae1dSRodney W. Grimes return (flags); 380df8bae1dSRodney W. Grimes } 381df8bae1dSRodney W. Grimes 382df8bae1dSRodney W. Grimes /* 383df8bae1dSRodney W. Grimes * TCP input routine, follows pages 65-76 of the 384df8bae1dSRodney W. Grimes * protocol specification dated September, 1981 very closely. 385df8bae1dSRodney W. Grimes */ 386fb59c426SYoshinobu Inoue #ifdef INET6 387fb59c426SYoshinobu Inoue int 388fb59c426SYoshinobu Inoue tcp6_input(mp, offp, proto) 389fb59c426SYoshinobu Inoue struct mbuf **mp; 390fb59c426SYoshinobu Inoue int *offp, proto; 391fb59c426SYoshinobu Inoue { 392fb59c426SYoshinobu Inoue register struct mbuf *m = *mp; 39333841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 394fb59c426SYoshinobu Inoue 395fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 396fb59c426SYoshinobu Inoue 397fb59c426SYoshinobu Inoue /* 398fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 399fb59c426SYoshinobu Inoue * better place to put this in? 400fb59c426SYoshinobu Inoue */ 40133841545SHajimu UMEMOTO ia6 = ip6_getdstifaddr(m); 40233841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 403fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 404fb59c426SYoshinobu Inoue 405fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 406fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 407fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 408fb59c426SYoshinobu Inoue return IPPROTO_DONE; 409fb59c426SYoshinobu Inoue } 410fb59c426SYoshinobu Inoue 411f0ffb944SJulian Elischer tcp_input(m, *offp); 412fb59c426SYoshinobu Inoue return IPPROTO_DONE; 413fb59c426SYoshinobu Inoue } 414fb59c426SYoshinobu Inoue #endif 415fb59c426SYoshinobu Inoue 416df8bae1dSRodney W. Grimes void 417f0ffb944SJulian Elischer tcp_input(m, off0) 418df8bae1dSRodney W. Grimes register struct mbuf *m; 419f0ffb944SJulian Elischer int off0; 420df8bae1dSRodney W. Grimes { 421fb59c426SYoshinobu Inoue register struct tcphdr *th; 422fb59c426SYoshinobu Inoue register struct ip *ip = NULL; 423fb59c426SYoshinobu Inoue register struct ipovly *ipov; 424f76fcf6dSJeffrey Hsu register struct inpcb *inp = NULL; 425e79adb8eSGarrett Wollman u_char *optp = NULL; 42626f9a767SRodney W. Grimes int optlen = 0; 427a0194ef1SBruce M Simpson int len, tlen, off; 428fb59c426SYoshinobu Inoue int drop_hdrlen; 429df8bae1dSRodney W. Grimes register struct tcpcb *tp = 0; 430fb59c426SYoshinobu Inoue register int thflags; 43126f9a767SRodney W. Grimes struct socket *so = 0; 432df8bae1dSRodney W. Grimes int todrop, acked, ourfinisacked, needoutput = 0; 433a0292f23SGarrett Wollman u_long tiwin; 434a0292f23SGarrett Wollman struct tcpopt to; /* options in this segment */ 435f76fcf6dSJeffrey Hsu int headlocked = 0; 4369b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 4379b932e9eSAndre Oppermann struct m_tag *fwd_tag; 4389b932e9eSAndre Oppermann #endif 439c068736aSJeffrey Hsu int rstreason; /* For badport_bandlim accounting purposes */ 440c068736aSJeffrey Hsu 441c068736aSJeffrey Hsu struct ip6_hdr *ip6 = NULL; 442c068736aSJeffrey Hsu #ifdef INET6 443c068736aSJeffrey Hsu int isipv6; 444c068736aSJeffrey Hsu #else 445c068736aSJeffrey Hsu const int isipv6 = 0; 446c068736aSJeffrey Hsu #endif 447f76fcf6dSJeffrey Hsu 448610ee2f9SDavid Greenman #ifdef TCPDEBUG 449c068736aSJeffrey Hsu /* 450c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 451c068736aSJeffrey Hsu * now IPv6. 452c068736aSJeffrey Hsu */ 453410bb1bfSLuigi Rizzo u_char tcp_saveipgen[40]; 454410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 455610ee2f9SDavid Greenman short ostate = 0; 456610ee2f9SDavid Greenman #endif 457c068736aSJeffrey Hsu 458fb59c426SYoshinobu Inoue #ifdef INET6 459fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 460fb59c426SYoshinobu Inoue #endif 461a0292f23SGarrett Wollman bzero((char *)&to, sizeof(to)); 462a0292f23SGarrett Wollman 463df8bae1dSRodney W. Grimes tcpstat.tcps_rcvtotal++; 464fb59c426SYoshinobu Inoue 465fb59c426SYoshinobu Inoue if (isipv6) { 46604d3a452SHajimu UMEMOTO #ifdef INET6 467fb59c426SYoshinobu Inoue /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */ 468fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 469fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 470fb59c426SYoshinobu Inoue if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 471fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbadsum++; 472fb59c426SYoshinobu Inoue goto drop; 473fb59c426SYoshinobu Inoue } 474fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 47533841545SHajimu UMEMOTO 47633841545SHajimu UMEMOTO /* 47733841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 47833841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 47933841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 48033841545SHajimu UMEMOTO * 48133841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 48233841545SHajimu UMEMOTO * already dropped in ip6_input. 48333841545SHajimu UMEMOTO */ 48433841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 48533841545SHajimu UMEMOTO /* XXX stat */ 48633841545SHajimu UMEMOTO goto drop; 48733841545SHajimu UMEMOTO } 48804d3a452SHajimu UMEMOTO #else 48904d3a452SHajimu UMEMOTO th = NULL; /* XXX: avoid compiler warning */ 49004d3a452SHajimu UMEMOTO #endif 491c068736aSJeffrey Hsu } else { 492df8bae1dSRodney W. Grimes /* 493df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 494df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 495df8bae1dSRodney W. Grimes */ 496fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 497df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 498fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 499fb59c426SYoshinobu Inoue } 500df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 501df8bae1dSRodney W. Grimes if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 502df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 503df8bae1dSRodney W. Grimes return; 504df8bae1dSRodney W. Grimes } 505df8bae1dSRodney W. Grimes } 506fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 507fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 508db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 509db4f9cc7SJonathan Lemon tlen = ip->ip_len; 510df8bae1dSRodney W. Grimes 511db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 512db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 513db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 514db4f9cc7SJonathan Lemon else 515db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 516c068736aSJeffrey Hsu ip->ip_dst.s_addr, 517c068736aSJeffrey Hsu htonl(m->m_pkthdr.csum_data + 518c068736aSJeffrey Hsu ip->ip_len + 519c068736aSJeffrey Hsu IPPROTO_TCP)); 520db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 5213c653157SHartmut Brandt #ifdef TCPDEBUG 5223c653157SHartmut Brandt ipov->ih_len = (u_short)tlen; 5233c653157SHartmut Brandt ipov->ih_len = htons(ipov->ih_len); 5243c653157SHartmut Brandt #endif 525db4f9cc7SJonathan Lemon } else { 526df8bae1dSRodney W. Grimes /* 527df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 528df8bae1dSRodney W. Grimes */ 529df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 530fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 531fb59c426SYoshinobu Inoue ipov->ih_len = (u_short)tlen; 532fd8e4ebcSMike Barcroft ipov->ih_len = htons(ipov->ih_len); 533fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 534db4f9cc7SJonathan Lemon } 535fb59c426SYoshinobu Inoue if (th->th_sum) { 536df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadsum++; 537df8bae1dSRodney W. Grimes goto drop; 538df8bae1dSRodney W. Grimes } 539fb59c426SYoshinobu Inoue #ifdef INET6 540fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 541fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 542fb59c426SYoshinobu Inoue #endif 543fb59c426SYoshinobu Inoue } 544df8bae1dSRodney W. Grimes 545df8bae1dSRodney W. Grimes /* 546df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 547df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 548df8bae1dSRodney W. Grimes */ 549fb59c426SYoshinobu Inoue off = th->th_off << 2; 550df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 551df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadoff++; 552df8bae1dSRodney W. Grimes goto drop; 553df8bae1dSRodney W. Grimes } 554fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 555df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 556fb59c426SYoshinobu Inoue if (isipv6) { 55704d3a452SHajimu UMEMOTO #ifdef INET6 558fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 559fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 560fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 56104d3a452SHajimu UMEMOTO #endif 562c068736aSJeffrey Hsu } else { 563df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 564c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 565c068736aSJeffrey Hsu == 0) { 566df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 567df8bae1dSRodney W. Grimes return; 568df8bae1dSRodney W. Grimes } 569fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 570fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 571fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 572fb59c426SYoshinobu Inoue } 573df8bae1dSRodney W. Grimes } 574df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 575fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 576df8bae1dSRodney W. Grimes } 577fb59c426SYoshinobu Inoue thflags = th->th_flags; 578df8bae1dSRodney W. Grimes 579e46cd3d4SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN 580e46cd3d4SDag-Erling Smørgrav /* 581e46cd3d4SDag-Erling Smørgrav * If the drop_synfin option is enabled, drop all packets with 582e46cd3d4SDag-Erling Smørgrav * both the SYN and FIN bits set. This prevents e.g. nmap from 583e46cd3d4SDag-Erling Smørgrav * identifying the TCP/IP stack. 584e46cd3d4SDag-Erling Smørgrav * 585a589a70eSGarrett Wollman * This is a violation of the TCP specification. 586e46cd3d4SDag-Erling Smørgrav */ 587fb59c426SYoshinobu Inoue if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN)) 588e46cd3d4SDag-Erling Smørgrav goto drop; 589e46cd3d4SDag-Erling Smørgrav #endif 590e46cd3d4SDag-Erling Smørgrav 591df8bae1dSRodney W. Grimes /* 592df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 593df8bae1dSRodney W. Grimes */ 594fd8e4ebcSMike Barcroft th->th_seq = ntohl(th->th_seq); 595fd8e4ebcSMike Barcroft th->th_ack = ntohl(th->th_ack); 596fd8e4ebcSMike Barcroft th->th_win = ntohs(th->th_win); 597fd8e4ebcSMike Barcroft th->th_urp = ntohs(th->th_urp); 598df8bae1dSRodney W. Grimes 599df8bae1dSRodney W. Grimes /* 6003bfd6421SJonathan Lemon * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options, 601fb59c426SYoshinobu Inoue * until after ip6_savecontrol() is called and before other functions 602fb59c426SYoshinobu Inoue * which don't want those proto headers. 603fb59c426SYoshinobu Inoue * Because ip6_savecontrol() is going to parse the mbuf to 604fb59c426SYoshinobu Inoue * search for data to be passed up to user-land, it wants mbuf 605fb59c426SYoshinobu Inoue * parameters to be unchanged. 60688ff5695SSUZUKI Shinsuke * XXX: the call of ip6_savecontrol() has been obsoleted based on 60788ff5695SSUZUKI Shinsuke * latest version of the advanced API (20020110). 608755c1f07SAndras Olah */ 609fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 610755c1f07SAndras Olah 611755c1f07SAndras Olah /* 612df8bae1dSRodney W. Grimes * Locate pcb for segment. 613df8bae1dSRodney W. Grimes */ 614f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 615f76fcf6dSJeffrey Hsu headlocked = 1; 616df8bae1dSRodney W. Grimes findpcb: 6179b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 6189b932e9eSAndre Oppermann /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */ 6199b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 6209b932e9eSAndre Oppermann 6219b932e9eSAndre Oppermann if (fwd_tag != NULL && isipv6 == 0) { /* IPv6 support is not yet */ 6229b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 6239b932e9eSAndre Oppermann 6249b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 625f9e354dfSJulian Elischer /* 6262b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 627f9e354dfSJulian Elischer * already got one like this? 628f9e354dfSJulian Elischer */ 6299b932e9eSAndre Oppermann inp = in_pcblookup_hash(&tcbinfo, 6309b932e9eSAndre Oppermann ip->ip_src, th->th_sport, 631c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 632c068736aSJeffrey Hsu 0, m->m_pkthdr.rcvif); 633f9e354dfSJulian Elischer if (!inp) { 6349b932e9eSAndre Oppermann /* It's new. Try to find the ambushing socket. */ 635f9e354dfSJulian Elischer inp = in_pcblookup_hash(&tcbinfo, 636fb59c426SYoshinobu Inoue ip->ip_src, th->th_sport, 6372b25acc1SLuigi Rizzo next_hop->sin_addr, 638c068736aSJeffrey Hsu next_hop->sin_port ? 639c068736aSJeffrey Hsu ntohs(next_hop->sin_port) : 640c068736aSJeffrey Hsu th->th_dport, 641c068736aSJeffrey Hsu 1, m->m_pkthdr.rcvif); 642f9e354dfSJulian Elischer } 6439b932e9eSAndre Oppermann /* Remove the tag from the packet. We don't need it anymore. */ 6449b932e9eSAndre Oppermann m_tag_delete(m, fwd_tag); 645c068736aSJeffrey Hsu } else { 6469b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 64704d3a452SHajimu UMEMOTO if (isipv6) { 64804d3a452SHajimu UMEMOTO #ifdef INET6 649c068736aSJeffrey Hsu inp = in6_pcblookup_hash(&tcbinfo, 650c068736aSJeffrey Hsu &ip6->ip6_src, th->th_sport, 651c068736aSJeffrey Hsu &ip6->ip6_dst, th->th_dport, 652c068736aSJeffrey Hsu 1, m->m_pkthdr.rcvif); 65304d3a452SHajimu UMEMOTO #endif 65404d3a452SHajimu UMEMOTO } else 655c068736aSJeffrey Hsu inp = in_pcblookup_hash(&tcbinfo, 656c068736aSJeffrey Hsu ip->ip_src, th->th_sport, 657c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 658c068736aSJeffrey Hsu 1, m->m_pkthdr.rcvif); 6599b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 660fb59c426SYoshinobu Inoue } 6619b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 662f9e354dfSJulian Elischer 663da0f4099SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC) 664d420fcdaSBruce M Simpson #ifdef INET6 665da0f4099SHajimu UMEMOTO if (isipv6) { 666da0f4099SHajimu UMEMOTO if (inp != NULL && ipsec6_in_reject(m, inp)) { 667fb59c426SYoshinobu Inoue #ifdef IPSEC 668fb59c426SYoshinobu Inoue ipsec6stat.in_polvio++; 669d420fcdaSBruce M Simpson #endif 670fb59c426SYoshinobu Inoue goto drop; 671fb59c426SYoshinobu Inoue } 672d420fcdaSBruce M Simpson } else 673d420fcdaSBruce M Simpson #endif /* INET6 */ 674d420fcdaSBruce M Simpson if (inp != NULL && ipsec4_in_reject(m, inp)) { 675da0f4099SHajimu UMEMOTO #ifdef IPSEC 676fb59c426SYoshinobu Inoue ipsecstat.in_polvio++; 677d420fcdaSBruce M Simpson #endif 678fb59c426SYoshinobu Inoue goto drop; 679fb59c426SYoshinobu Inoue } 680da0f4099SHajimu UMEMOTO #endif /*IPSEC || FAST_IPSEC*/ 681df8bae1dSRodney W. Grimes 682df8bae1dSRodney W. Grimes /* 683df8bae1dSRodney W. Grimes * If the state is CLOSED (i.e., TCB does not exist) then 684df8bae1dSRodney W. Grimes * all data in the incoming segment is discarded. 685df8bae1dSRodney W. Grimes * If the TCB exists but is in CLOSED state, it is embryonic, 686df8bae1dSRodney W. Grimes * but should either do a listen or a connect soon. 687df8bae1dSRodney W. Grimes */ 688816a3d83SPoul-Henning Kamp if (inp == NULL) { 6892e4e1b4cSGeoff Rehmet if (log_in_vain) { 690fb59c426SYoshinobu Inoue #ifdef INET6 691ded7008aSJuli Mallett char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2]; 692c068736aSJeffrey Hsu #else 693fb59c426SYoshinobu Inoue char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"]; 694c068736aSJeffrey Hsu #endif 69575cfc95fSAndrey A. Chernov 696fb59c426SYoshinobu Inoue if (isipv6) { 69704d3a452SHajimu UMEMOTO #ifdef INET6 698ded7008aSJuli Mallett strcpy(dbuf, "["); 699ded7008aSJuli Mallett strcpy(sbuf, "["); 700ded7008aSJuli Mallett strcat(dbuf, ip6_sprintf(&ip6->ip6_dst)); 701ded7008aSJuli Mallett strcat(sbuf, ip6_sprintf(&ip6->ip6_src)); 702ded7008aSJuli Mallett strcat(dbuf, "]"); 703ded7008aSJuli Mallett strcat(sbuf, "]"); 70404d3a452SHajimu UMEMOTO #endif 705c068736aSJeffrey Hsu } else { 706fb59c426SYoshinobu Inoue strcpy(dbuf, inet_ntoa(ip->ip_dst)); 707fb59c426SYoshinobu Inoue strcpy(sbuf, inet_ntoa(ip->ip_src)); 708fb59c426SYoshinobu Inoue } 7092e4e1b4cSGeoff Rehmet switch (log_in_vain) { 7102e4e1b4cSGeoff Rehmet case 1: 71139eb27a4SCrist J. Clark if ((thflags & TH_SYN) == 0) 7122e4e1b4cSGeoff Rehmet break; 713e4d2978dSPoul-Henning Kamp /* FALLTHROUGH */ 7142e4e1b4cSGeoff Rehmet case 2: 7152e4e1b4cSGeoff Rehmet log(LOG_INFO, 716c068736aSJeffrey Hsu "Connection attempt to TCP %s:%d " 71739eb27a4SCrist J. Clark "from %s:%d flags:0x%02x\n", 718fb59c426SYoshinobu Inoue dbuf, ntohs(th->th_dport), sbuf, 719fb59c426SYoshinobu Inoue ntohs(th->th_sport), thflags); 7202e4e1b4cSGeoff Rehmet break; 7212e4e1b4cSGeoff Rehmet default: 7222e4e1b4cSGeoff Rehmet break; 7232e4e1b4cSGeoff Rehmet } 72475cfc95fSAndrey A. Chernov } 7252e4e1b4cSGeoff Rehmet if (blackhole) { 7262e4e1b4cSGeoff Rehmet switch (blackhole) { 727828b7f40SGeoff Rehmet case 1: 728fb59c426SYoshinobu Inoue if (thflags & TH_SYN) 729828b7f40SGeoff Rehmet goto drop; 730828b7f40SGeoff Rehmet break; 731828b7f40SGeoff Rehmet case 2: 732828b7f40SGeoff Rehmet goto drop; 733828b7f40SGeoff Rehmet default: 734828b7f40SGeoff Rehmet goto drop; 7352e4e1b4cSGeoff Rehmet } 736828b7f40SGeoff Rehmet } 737a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 738a57815efSBosko Milekic goto dropwithreset; 739816a3d83SPoul-Henning Kamp } 740f76fcf6dSJeffrey Hsu INP_LOCK(inp); 741340c35deSJonathan Lemon if (inp->inp_vflag & INP_TIMEWAIT) { 742340c35deSJonathan Lemon /* 743340c35deSJonathan Lemon * The only option of relevance is TOF_CC, and only if 744340c35deSJonathan Lemon * present in a SYN segment. See tcp_timewait(). 745340c35deSJonathan Lemon */ 746340c35deSJonathan Lemon if (thflags & TH_SYN) 7476d90faf3SPaul Saab tcp_dooptions((struct tcpcb *)NULL, &to, optp, optlen, 1, th); 748340c35deSJonathan Lemon if (tcp_timewait((struct tcptw *)inp->inp_ppcb, 749340c35deSJonathan Lemon &to, th, m, tlen)) 750340c35deSJonathan Lemon goto findpcb; 751340c35deSJonathan Lemon /* 752340c35deSJonathan Lemon * tcp_timewait unlocks inp. 753340c35deSJonathan Lemon */ 754340c35deSJonathan Lemon INP_INFO_WUNLOCK(&tcbinfo); 755340c35deSJonathan Lemon return; 756340c35deSJonathan Lemon } 757df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 75809f81a46SBosko Milekic if (tp == 0) { 759f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 760a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 761a57815efSBosko Milekic goto dropwithreset; 76209f81a46SBosko Milekic } 763df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_CLOSED) 764df8bae1dSRodney W. Grimes goto drop; 765df8bae1dSRodney W. Grimes 766df8bae1dSRodney W. Grimes /* Unscale the window into a 32-bit value. */ 767fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) == 0) 768fb59c426SYoshinobu Inoue tiwin = th->th_win << tp->snd_scale; 769df8bae1dSRodney W. Grimes else 770fb59c426SYoshinobu Inoue tiwin = th->th_win; 771fb59c426SYoshinobu Inoue 772c488362eSRobert Watson #ifdef MAC 7731f82efb3SRobert Watson INP_LOCK_ASSERT(inp); 774a557af22SRobert Watson if (mac_check_inpcb_deliver(inp, m)) 775c488362eSRobert Watson goto drop; 776c488362eSRobert Watson #endif 777a557af22SRobert Watson so = inp->inp_socket; 778610ee2f9SDavid Greenman #ifdef TCPDEBUG 779df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 780df8bae1dSRodney W. Grimes ostate = tp->t_state; 781fb59c426SYoshinobu Inoue if (isipv6) 782540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 783fb59c426SYoshinobu Inoue else 784540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 785fb59c426SYoshinobu Inoue tcp_savetcp = *th; 786df8bae1dSRodney W. Grimes } 787610ee2f9SDavid Greenman #endif 788540e8b7eSJeffrey Hsu if (so->so_options & SO_ACCEPTCONN) { 789540e8b7eSJeffrey Hsu struct in_conninfo inc; 790540e8b7eSJeffrey Hsu 791fb59c426SYoshinobu Inoue #ifdef INET6 792be2ac88cSJonathan Lemon inc.inc_isipv6 = isipv6; 793c068736aSJeffrey Hsu #endif 794be2ac88cSJonathan Lemon if (isipv6) { 795be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 796be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 797c068736aSJeffrey Hsu } else { 798be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 799be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 800be2ac88cSJonathan Lemon } 801be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 802be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 803be2ac88cSJonathan Lemon 804fb59c426SYoshinobu Inoue /* 805be2ac88cSJonathan Lemon * If the state is LISTEN then ignore segment if it contains 806be2ac88cSJonathan Lemon * a RST. If the segment contains an ACK then it is bad and 807be2ac88cSJonathan Lemon * send a RST. If it does not contain a SYN then it is not 808be2ac88cSJonathan Lemon * interesting; drop it. 809be2ac88cSJonathan Lemon * 810be2ac88cSJonathan Lemon * If the state is SYN_RECEIVED (syncache) and seg contains 811be2ac88cSJonathan Lemon * an ACK, but not for our SYN/ACK, send a RST. If the seg 812be2ac88cSJonathan Lemon * contains a RST, check the sequence number to see if it 813be2ac88cSJonathan Lemon * is a valid reset segment. 814fb59c426SYoshinobu Inoue */ 815fb59c426SYoshinobu Inoue if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 816be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 817be2ac88cSJonathan Lemon if (!syncache_expand(&inc, th, &so, m)) { 8184195b4afSPaul Traina /* 819be2ac88cSJonathan Lemon * No syncache entry, or ACK was not 820be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 8214195b4afSPaul Traina */ 822be2ac88cSJonathan Lemon tcpstat.tcps_badsyn++; 823be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 824be2ac88cSJonathan Lemon goto dropwithreset; 825be2ac88cSJonathan Lemon } 826f76fcf6dSJeffrey Hsu if (so == NULL) { 827be2ac88cSJonathan Lemon /* 828be2ac88cSJonathan Lemon * Could not complete 3-way handshake, 829be2ac88cSJonathan Lemon * connection is being closed down, and 830be2ac88cSJonathan Lemon * syncache will free mbuf. 831be2ac88cSJonathan Lemon */ 832f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 833f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 834be2ac88cSJonathan Lemon return; 835f76fcf6dSJeffrey Hsu } 836be2ac88cSJonathan Lemon /* 837be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 838be2ac88cSJonathan Lemon * Continue processing segment. 839be2ac88cSJonathan Lemon */ 840f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 841be2ac88cSJonathan Lemon inp = sotoinpcb(so); 842f76fcf6dSJeffrey Hsu INP_LOCK(inp); 843be2ac88cSJonathan Lemon tp = intotcpcb(inp); 844be2ac88cSJonathan Lemon /* 845be2ac88cSJonathan Lemon * This is what would have happened in 846e6658b12SRobert Watson * tcp_output() when the SYN,ACK was sent. 847be2ac88cSJonathan Lemon */ 848be2ac88cSJonathan Lemon tp->snd_up = tp->snd_una; 849be2ac88cSJonathan Lemon tp->snd_max = tp->snd_nxt = tp->iss + 1; 850be2ac88cSJonathan Lemon tp->last_ack_sent = tp->rcv_nxt; 851be2ac88cSJonathan Lemon /* 85241446225SJonathan Lemon * RFC1323: The window in SYN & SYN/ACK 85341446225SJonathan Lemon * segments is never scaled. 854be2ac88cSJonathan Lemon */ 855be2ac88cSJonathan Lemon tp->snd_wnd = tiwin; /* unscaled */ 856be2ac88cSJonathan Lemon goto after_listen; 857be2ac88cSJonathan Lemon } 858be2ac88cSJonathan Lemon if (thflags & TH_RST) { 859be2ac88cSJonathan Lemon syncache_chkrst(&inc, th); 860be2ac88cSJonathan Lemon goto drop; 861be2ac88cSJonathan Lemon } 862fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 863be2ac88cSJonathan Lemon syncache_badack(&inc); 864ebb0cbeaSPaul Traina tcpstat.tcps_badsyn++; 865a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 866a57815efSBosko Milekic goto dropwithreset; 8674195b4afSPaul Traina } 868ebb0cbeaSPaul Traina goto drop; 869ebb0cbeaSPaul Traina } 87033841545SHajimu UMEMOTO 871be2ac88cSJonathan Lemon /* 872be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 873be2ac88cSJonathan Lemon */ 87433841545SHajimu UMEMOTO #ifdef INET6 87533841545SHajimu UMEMOTO /* 87633841545SHajimu UMEMOTO * If deprecated address is forbidden, 87733841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 87833841545SHajimu UMEMOTO * address to prevent any new inbound connection from 87933841545SHajimu UMEMOTO * getting established. 88033841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 88133841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 88233841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 88333841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 88433841545SHajimu UMEMOTO * for the exchange. 88533841545SHajimu UMEMOTO * 88633841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 88733841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 88833841545SHajimu UMEMOTO * SYN in this case. 88933841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 89033841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 89133841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 89233841545SHajimu UMEMOTO * used" 89333841545SHajimu UMEMOTO * 2. use of it with new communication: 89433841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 89533841545SHajimu UMEMOTO * with sufficient scope is available" 89633841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 89733841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 89833841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 89933841545SHajimu UMEMOTO * 90033841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 90133841545SHajimu UMEMOTO * multiple description text for deprecated address 90233841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 90333841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 90433841545SHajimu UMEMOTO */ 90533841545SHajimu UMEMOTO if (isipv6 && !ip6_use_deprecated) { 90633841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 90733841545SHajimu UMEMOTO 90833841545SHajimu UMEMOTO if ((ia6 = ip6_getdstifaddr(m)) && 90933841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 910f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 91133841545SHajimu UMEMOTO tp = NULL; 91233841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 91333841545SHajimu UMEMOTO goto dropwithreset; 91433841545SHajimu UMEMOTO } 91533841545SHajimu UMEMOTO } 91633841545SHajimu UMEMOTO #endif 91765f28919SJesper Skriver /* 918be2ac88cSJonathan Lemon * If it is from this socket, drop it, it must be forged. 919be2ac88cSJonathan Lemon * Don't bother responding if the destination was a broadcast. 92065f28919SJesper Skriver */ 921be2ac88cSJonathan Lemon if (th->th_dport == th->th_sport) { 922fb59c426SYoshinobu Inoue if (isipv6) { 923be2ac88cSJonathan Lemon if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 924be2ac88cSJonathan Lemon &ip6->ip6_src)) 925be2ac88cSJonathan Lemon goto drop; 926c068736aSJeffrey Hsu } else { 927be2ac88cSJonathan Lemon if (ip->ip_dst.s_addr == ip->ip_src.s_addr) 928be2ac88cSJonathan Lemon goto drop; 929be2ac88cSJonathan Lemon } 930c068736aSJeffrey Hsu } 931be2ac88cSJonathan Lemon /* 932be2ac88cSJonathan Lemon * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 9332ca2159fSCrist J. Clark * 93493ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 93593ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 93693ec91baSCrist J. Clark * in_broadcast() to find them. 937be2ac88cSJonathan Lemon */ 938be2ac88cSJonathan Lemon if (m->m_flags & (M_BCAST|M_MCAST)) 939be2ac88cSJonathan Lemon goto drop; 940be2ac88cSJonathan Lemon if (isipv6) { 941be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 942be2ac88cSJonathan Lemon IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 943be2ac88cSJonathan Lemon goto drop; 944c068736aSJeffrey Hsu } else { 945be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 946be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 9472ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 9482ca2159fSCrist J. Clark in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 949be2ac88cSJonathan Lemon goto drop; 950c068736aSJeffrey Hsu } 951be2ac88cSJonathan Lemon /* 952be2ac88cSJonathan Lemon * SYN appears to be valid; create compressed TCP state 953be2ac88cSJonathan Lemon * for syncache, or perform t/tcp connection. 954be2ac88cSJonathan Lemon */ 955be2ac88cSJonathan Lemon if (so->so_qlen <= so->so_qlimit) { 9563c653157SHartmut Brandt #ifdef TCPDEBUG 9573c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 9583c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 9593c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 9603c653157SHartmut Brandt #endif 9616d90faf3SPaul Saab tcp_dooptions(tp, &to, optp, optlen, 1, th); 962be2ac88cSJonathan Lemon if (!syncache_add(&inc, &to, th, &so, m)) 963be2ac88cSJonathan Lemon goto drop; 964f76fcf6dSJeffrey Hsu if (so == NULL) { 965be2ac88cSJonathan Lemon /* 966be2ac88cSJonathan Lemon * Entry added to syncache, mbuf used to 967be2ac88cSJonathan Lemon * send SYN,ACK packet. 968be2ac88cSJonathan Lemon */ 969f76fcf6dSJeffrey Hsu KASSERT(headlocked, ("headlocked")); 970f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 971f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 972be2ac88cSJonathan Lemon return; 973f76fcf6dSJeffrey Hsu } 974be2ac88cSJonathan Lemon /* 975be2ac88cSJonathan Lemon * Segment passed TAO tests. 976be2ac88cSJonathan Lemon */ 977f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 978be2ac88cSJonathan Lemon inp = sotoinpcb(so); 979f76fcf6dSJeffrey Hsu INP_LOCK(inp); 980df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 981be2ac88cSJonathan Lemon tp->snd_wnd = tiwin; 982be2ac88cSJonathan Lemon tp->t_starttime = ticks; 983be2ac88cSJonathan Lemon tp->t_state = TCPS_ESTABLISHED; 984df8bae1dSRodney W. Grimes 985be2ac88cSJonathan Lemon /* 9863bfd6421SJonathan Lemon * T/TCP logic: 9873bfd6421SJonathan Lemon * If there is a FIN or if there is data, then 9883bfd6421SJonathan Lemon * delay SYN,ACK(SYN) in the hope of piggy-backing 9893bfd6421SJonathan Lemon * it on a response segment. Otherwise must send 9903bfd6421SJonathan Lemon * ACK now in case the other side is slow starting. 991be2ac88cSJonathan Lemon */ 9923bfd6421SJonathan Lemon if (thflags & TH_FIN || tlen != 0) 9933bfd6421SJonathan Lemon tp->t_flags |= (TF_DELACK | TF_NEEDSYN); 9943bfd6421SJonathan Lemon else 995f243998bSJonathan Lemon tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 996be2ac88cSJonathan Lemon tcpstat.tcps_connects++; 997be2ac88cSJonathan Lemon soisconnected(so); 998be2ac88cSJonathan Lemon goto trimthenstep6; 999df8bae1dSRodney W. Grimes } 1000be2ac88cSJonathan Lemon goto drop; 10014cc20ab1SSeigo Tanimura } 1002be2ac88cSJonathan Lemon after_listen: 10037cfc6904SRobert Watson KASSERT(headlocked, ("tcp_input(): after_listen head is not locked")); 10047cfc6904SRobert Watson INP_LOCK_ASSERT(inp); 1005be2ac88cSJonathan Lemon 1006be2ac88cSJonathan Lemon /* XXX temp debugging */ 1007be2ac88cSJonathan Lemon /* should not happen - syncache should pick up these connections */ 1008be2ac88cSJonathan Lemon if (tp->t_state == TCPS_LISTEN) 1009be2ac88cSJonathan Lemon panic("tcp_input: TCPS_LISTEN"); 1010df8bae1dSRodney W. Grimes 1011df8bae1dSRodney W. Grimes /* 101253369ac9SAndre Oppermann * This is the second part of the MSS DoS prevention code (after 101353369ac9SAndre Oppermann * minmss on the sending side) and it deals with too many too small 101453369ac9SAndre Oppermann * tcp packets in a too short timeframe (1 second). 101553369ac9SAndre Oppermann * 101653369ac9SAndre Oppermann * For every full second we count the number of received packets 101753369ac9SAndre Oppermann * and bytes. If we get a lot of packets per second for this connection 101853369ac9SAndre Oppermann * (tcp_minmssoverload) we take a closer look at it and compute the 101953369ac9SAndre Oppermann * average packet size for the past second. If that is less than 102053369ac9SAndre Oppermann * tcp_minmss we get too many packets with very small payload which 102153369ac9SAndre Oppermann * is not good and burdens our system (and every packet generates 102253369ac9SAndre Oppermann * a wakeup to the process connected to our socket). We can reasonable 102353369ac9SAndre Oppermann * expect this to be small packet DoS attack to exhaust our CPU 102453369ac9SAndre Oppermann * cycles. 102553369ac9SAndre Oppermann * 102653369ac9SAndre Oppermann * Care has to be taken for the minimum packet overload value. This 102753369ac9SAndre Oppermann * value defines the minimum number of packets per second before we 102853369ac9SAndre Oppermann * start to worry. This must not be too low to avoid killing for 102953369ac9SAndre Oppermann * example interactive connections with many small packets like 103053369ac9SAndre Oppermann * telnet or SSH. 103153369ac9SAndre Oppermann * 103253369ac9SAndre Oppermann * Setting either tcp_minmssoverload or tcp_minmss to "0" disables 103353369ac9SAndre Oppermann * this check. 103453369ac9SAndre Oppermann * 103553369ac9SAndre Oppermann * Account for packet if payload packet, skip over ACK, etc. 103653369ac9SAndre Oppermann */ 103753369ac9SAndre Oppermann if (tcp_minmss && tcp_minmssoverload && 103853369ac9SAndre Oppermann tp->t_state == TCPS_ESTABLISHED && tlen > 0) { 103953369ac9SAndre Oppermann if (tp->rcv_second > ticks) { 104053369ac9SAndre Oppermann tp->rcv_pps++; 104153369ac9SAndre Oppermann tp->rcv_byps += tlen + off; 104253369ac9SAndre Oppermann if (tp->rcv_pps > tcp_minmssoverload) { 104353369ac9SAndre Oppermann if ((tp->rcv_byps / tp->rcv_pps) < tcp_minmss) { 104453369ac9SAndre Oppermann printf("too many small tcp packets from " 104553369ac9SAndre Oppermann "%s:%u, av. %lubyte/packet, " 104653369ac9SAndre Oppermann "dropping connection\n", 104753369ac9SAndre Oppermann #ifdef INET6 104853369ac9SAndre Oppermann isipv6 ? 104953369ac9SAndre Oppermann ip6_sprintf(&inp->inp_inc.inc6_faddr) : 105053369ac9SAndre Oppermann #endif 105153369ac9SAndre Oppermann inet_ntoa(inp->inp_inc.inc_faddr), 105253369ac9SAndre Oppermann inp->inp_inc.inc_fport, 105353369ac9SAndre Oppermann tp->rcv_byps / tp->rcv_pps); 105453369ac9SAndre Oppermann tp = tcp_drop(tp, ECONNRESET); 105553369ac9SAndre Oppermann tcpstat.tcps_minmssdrops++; 105653369ac9SAndre Oppermann goto drop; 105753369ac9SAndre Oppermann } 105853369ac9SAndre Oppermann } 105953369ac9SAndre Oppermann } else { 106053369ac9SAndre Oppermann tp->rcv_second = ticks + hz; 106153369ac9SAndre Oppermann tp->rcv_pps = 1; 106253369ac9SAndre Oppermann tp->rcv_byps = tlen + off; 106353369ac9SAndre Oppermann } 106453369ac9SAndre Oppermann } 106553369ac9SAndre Oppermann 106653369ac9SAndre Oppermann /* 1067df8bae1dSRodney W. Grimes * Segment received on connection. 1068df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1069df8bae1dSRodney W. Grimes */ 10709b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 10717ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 10729b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp); 1073df8bae1dSRodney W. Grimes 1074df8bae1dSRodney W. Grimes /* 107597d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 107697d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 107797d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1078df8bae1dSRodney W. Grimes */ 10796d90faf3SPaul Saab tcp_dooptions(tp, &to, optp, optlen, thflags & TH_SYN, th); 1080be2ac88cSJonathan Lemon if (thflags & TH_SYN) { 1081be2ac88cSJonathan Lemon if (to.to_flags & TOF_SCALE) { 1082be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 1083be2ac88cSJonathan Lemon tp->requested_s_scale = to.to_requested_s_scale; 1084be2ac88cSJonathan Lemon } 1085be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1086be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1087be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1088be2ac88cSJonathan Lemon tp->ts_recent_age = ticks; 1089be2ac88cSJonathan Lemon } 1090be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1091be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 10926d90faf3SPaul Saab if (tp->sack_enable) { 10936d90faf3SPaul Saab if (!(to.to_flags & TOF_SACK)) 10946d90faf3SPaul Saab tp->sack_enable = 0; 10956d90faf3SPaul Saab else 10966d90faf3SPaul Saab tp->t_flags |= TF_SACK_PERMIT; 10976d90faf3SPaul Saab } 10986d90faf3SPaul Saab 10996d90faf3SPaul Saab } 11006d90faf3SPaul Saab 11016d90faf3SPaul Saab if (tp->sack_enable) { 11026d90faf3SPaul Saab /* Delete stale (cumulatively acked) SACK holes */ 11036d90faf3SPaul Saab tcp_del_sackholes(tp, th); 1104652178a1SPaul Saab tp->rcv_laststart = th->th_seq; /* last recv'd segment*/ 11056d90faf3SPaul Saab tp->rcv_lastend = th->th_seq + tlen; 1106be2ac88cSJonathan Lemon } 1107df8bae1dSRodney W. Grimes 1108df8bae1dSRodney W. Grimes /* 1109df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1110df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1111df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1112df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1113df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1114df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1115df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1116df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1117df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1118df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1119df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1120df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1121a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1122a0292f23SGarrett Wollman * Since we check for TCPS_ESTABLISHED above, it can only 1123a0292f23SGarrett Wollman * be TH_NEEDSYN. 1124df8bae1dSRodney W. Grimes */ 1125df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1126fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1127a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1128be2ac88cSJonathan Lemon ((to.to_flags & TOF_TS) == 0 || 1129a0292f23SGarrett Wollman TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 1130c94c54e4SAndre Oppermann th->th_seq == tp->rcv_nxt && tiwin && tiwin == tp->snd_wnd && 1131df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) { 1132df8bae1dSRodney W. Grimes 1133df8bae1dSRodney W. Grimes /* 1134df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1135df8bae1dSRodney W. Grimes * record the timestamp. 1136a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1137a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1138df8bae1dSRodney W. Grimes */ 1139be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1140fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 11419b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1142a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1143df8bae1dSRodney W. Grimes } 1144df8bae1dSRodney W. Grimes 1145fb59c426SYoshinobu Inoue if (tlen == 0) { 1146fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1147fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1148233e8c18SGarrett Wollman tp->snd_cwnd >= tp->snd_wnd && 11496d90faf3SPaul Saab ((!tcp_do_newreno && !tp->sack_enable && 1150cb942153SJeffrey Hsu tp->t_dupacks < tcprexmtthresh) || 11516d90faf3SPaul Saab ((tcp_do_newreno || tp->sack_enable) && 11526d90faf3SPaul Saab !IN_FASTRECOVERY(tp)))) { 1153f76fcf6dSJeffrey Hsu KASSERT(headlocked, ("headlocked")); 1154df8bae1dSRodney W. Grimes /* 1155df8bae1dSRodney W. Grimes * this is a pure ack for outstanding data. 1156df8bae1dSRodney W. Grimes */ 1157df8bae1dSRodney W. Grimes ++tcpstat.tcps_predack; 11589b8b58e0SJonathan Lemon /* 11599b8b58e0SJonathan Lemon * "bad retransmit" recovery 11609b8b58e0SJonathan Lemon */ 11619b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 11629b8b58e0SJonathan Lemon ticks < tp->t_badrxtwin) { 1163cb942153SJeffrey Hsu ++tcpstat.tcps_sndrexmitbad; 11649b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 11659b8b58e0SJonathan Lemon tp->snd_ssthresh = 11669b8b58e0SJonathan Lemon tp->snd_ssthresh_prev; 11679d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 11689d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 11699d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 11709b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 11719b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; 11729b8b58e0SJonathan Lemon } 1173fa55172bSMatthew Dillon 1174fa55172bSMatthew Dillon /* 1175fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1176fa55172bSMatthew Dillon * 1177fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1178fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1179fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1180fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1181fa55172bSMatthew Dillon */ 1182fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1183fa55172bSMatthew Dillon to.to_tsecr) { 1184a0292f23SGarrett Wollman tcp_xmit_timer(tp, 11859b8b58e0SJonathan Lemon ticks - to.to_tsecr + 1); 1186fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1187fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1188c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1189c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1190fa55172bSMatthew Dillon } 11911fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 1192fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 1193df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 1194df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 1195df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 11969d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 11979d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 11989d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 11999d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 12001645d090SJeff Roberson /* 12011645d090SJeff Roberson * pull snd_wl2 up to prevent seq wrap relative 12021645d090SJeff Roberson * to th_ack. 12031645d090SJeff Roberson */ 1204cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1205b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1206df8bae1dSRodney W. Grimes m_freem(m); 1207fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 1208df8bae1dSRodney W. Grimes 1209df8bae1dSRodney W. Grimes /* 1210df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1211df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1212df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1213df8bae1dSRodney W. Grimes * If process is waiting for space, 1214df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1215df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1216df8bae1dSRodney W. Grimes * decide between more output or persist. 12173c653157SHartmut Brandt 12183c653157SHartmut Brandt #ifdef TCPDEBUG 12193c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 12203c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 12213c653157SHartmut Brandt (void *)tcp_saveipgen, 12223c653157SHartmut Brandt &tcp_savetcp, 0); 12233c653157SHartmut Brandt #endif 1224df8bae1dSRodney W. Grimes */ 1225df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 12269b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 12279b8b58e0SJonathan Lemon else if (!callout_active(tp->tt_persist)) 12289b8b58e0SJonathan Lemon callout_reset(tp->tt_rexmt, 12299b8b58e0SJonathan Lemon tp->t_rxtcur, 12309b8b58e0SJonathan Lemon tcp_timer_rexmt, tp); 1231df8bae1dSRodney W. Grimes 1232df8bae1dSRodney W. Grimes sowwakeup(so); 1233df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 1234df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1235a14c749fSJonathan Lemon goto check_delack; 1236df8bae1dSRodney W. Grimes } 1237fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1238fb59c426SYoshinobu Inoue LIST_EMPTY(&tp->t_segq) && 1239fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 1240f76fcf6dSJeffrey Hsu KASSERT(headlocked, ("headlocked")); 1241df8bae1dSRodney W. Grimes /* 1242df8bae1dSRodney W. Grimes * this is a pure, in-sequence data packet 1243df8bae1dSRodney W. Grimes * with nothing on the reassembly queue and 1244df8bae1dSRodney W. Grimes * we have enough buffer space to take it. 1245df8bae1dSRodney W. Grimes */ 12466d90faf3SPaul Saab /* Clean receiver SACK report if present */ 12476d90faf3SPaul Saab if (tp->sack_enable && tp->rcv_numsacks) 12486d90faf3SPaul Saab tcp_clean_sackreport(tp); 1249df8bae1dSRodney W. Grimes ++tcpstat.tcps_preddat; 1250fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 12511645d090SJeff Roberson /* 12521645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 12531645d090SJeff Roberson * th_seq. 12541645d090SJeff Roberson */ 12551645d090SJeff Roberson tp->snd_wl1 = th->th_seq; 12561645d090SJeff Roberson /* 12571645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 12581645d090SJeff Roberson * rcv_nxt. 12591645d090SJeff Roberson */ 12601645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 1261df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpack++; 1262fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyte += tlen; 1263fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 1264df8bae1dSRodney W. Grimes /* 12653c653157SHartmut Brandt #ifdef TCPDEBUG 12663c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 12673c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 12683c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 12693c653157SHartmut Brandt #endif 1270755c1f07SAndras Olah * Add data to socket buffer. 1271df8bae1dSRodney W. Grimes */ 12721e4d7da7SRobert Watson /* Unlocked read. */ 12731e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1274c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1275c1c36a2cSMike Silbersack m_freem(m); 1276c1c36a2cSMike Silbersack } else { 1277fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 12781e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 1279c1c36a2cSMike Silbersack } 12801e4d7da7SRobert Watson sorwakeup_locked(so); 1281d8c85a26SJonathan Lemon if (DELAY_ACK(tp)) { 12823bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1283f498eeeeSDavid Greenman } else { 1284e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 1285e612a582SDavid Greenman tcp_output(tp); 1286e612a582SDavid Greenman } 1287a14c749fSJonathan Lemon goto check_delack; 1288df8bae1dSRodney W. Grimes } 1289df8bae1dSRodney W. Grimes } 1290df8bae1dSRodney W. Grimes 1291df8bae1dSRodney W. Grimes /* 1292df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1293df8bae1dSRodney W. Grimes * and then do TCP input processing. 1294df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1295df8bae1dSRodney W. Grimes * but not less than advertised window. 1296df8bae1dSRodney W. Grimes */ 1297df8bae1dSRodney W. Grimes { int win; 1298df8bae1dSRodney W. Grimes 1299df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1300df8bae1dSRodney W. Grimes if (win < 0) 1301df8bae1dSRodney W. Grimes win = 0; 130266e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1303df8bae1dSRodney W. Grimes } 1304df8bae1dSRodney W. Grimes 1305df8bae1dSRodney W. Grimes switch (tp->t_state) { 1306df8bae1dSRodney W. Grimes 1307df8bae1dSRodney W. Grimes /* 1308764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1309764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1310764d8cefSBill Fenner */ 1311764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1312fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1313fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1314a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1315a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1316a57815efSBosko Milekic goto dropwithreset; 1317a57815efSBosko Milekic } 1318764d8cefSBill Fenner break; 1319764d8cefSBill Fenner 1320764d8cefSBill Fenner /* 1321df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1322df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1323df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1324df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1325df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1326df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1327df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1328df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1329df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1330df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1331df8bae1dSRodney W. Grimes */ 1332df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1333fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1334fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1335fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1336a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1337a0292f23SGarrett Wollman goto dropwithreset; 1338a0292f23SGarrett Wollman } 1339fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 1340fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1341df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 1342df8bae1dSRodney W. Grimes goto drop; 1343df8bae1dSRodney W. Grimes } 1344fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) == 0) 1345df8bae1dSRodney W. Grimes goto drop; 1346fb59c426SYoshinobu Inoue tp->snd_wnd = th->th_win; /* initial send window */ 1347a0292f23SGarrett Wollman 1348fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1349df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1350fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1351845799c1SAndras Olah tcpstat.tcps_connects++; 1352845799c1SAndras Olah soisconnected(so); 1353c488362eSRobert Watson #ifdef MAC 1354310e7cebSRobert Watson SOCK_LOCK(so); 1355c488362eSRobert Watson mac_set_socket_peer_from_mbuf(m, so); 1356310e7cebSRobert Watson SOCK_UNLOCK(so); 1357c488362eSRobert Watson #endif 1358845799c1SAndras Olah /* Do window scaling on this connection? */ 1359845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1360845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1361845799c1SAndras Olah tp->snd_scale = tp->requested_s_scale; 1362845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1363845799c1SAndras Olah } 1364a0292f23SGarrett Wollman tp->rcv_adv += tp->rcv_wnd; 1365a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1366a0292f23SGarrett Wollman /* 1367a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1368a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1369a0292f23SGarrett Wollman */ 1370d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && tlen != 0) 13719b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 13729b8b58e0SJonathan Lemon tcp_timer_delack, tp); 1373a0292f23SGarrett Wollman else 1374a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1375a0292f23SGarrett Wollman /* 1376a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1377a0292f23SGarrett Wollman * Transitions: 1378a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1379a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1380a0292f23SGarrett Wollman */ 13819b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1382a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1383a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1384a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1385fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 13867ff19458SPaul Traina } else { 1387a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 13889b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, 13899b8b58e0SJonathan Lemon tcp_timer_keep, tp); 13907ff19458SPaul Traina } 1391a0292f23SGarrett Wollman } else { 1392a0292f23SGarrett Wollman /* 1393c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 1394c068736aSJeffrey Hsu * simultaneous open. If segment contains CC option 1395c068736aSJeffrey Hsu * and there is a cached CC, apply TAO test. 1396c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 1397c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 1398a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1399a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1400a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1401a0292f23SGarrett Wollman */ 1402a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 14039b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 1404df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1405a0292f23SGarrett Wollman } 1406df8bae1dSRodney W. Grimes 1407df8bae1dSRodney W. Grimes trimthenstep6: 14087cfc6904SRobert Watson KASSERT(headlocked, 14097cfc6904SRobert Watson ("tcp_input(): trimthenstep6 head is not locked")); 14107cfc6904SRobert Watson INP_LOCK_ASSERT(inp); 14117cfc6904SRobert Watson 1412df8bae1dSRodney W. Grimes /* 1413fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1414df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1415df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1416df8bae1dSRodney W. Grimes */ 1417fb59c426SYoshinobu Inoue th->th_seq++; 1418fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1419fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1420df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1421fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1422fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1423df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1424df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1425df8bae1dSRodney W. Grimes } 1426fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1427fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 1428a0292f23SGarrett Wollman /* 1429a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1430a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1431a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1432a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1433a0292f23SGarrett Wollman * Otherwise, goto step 6. 1434a0292f23SGarrett Wollman */ 1435fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1436a0292f23SGarrett Wollman goto process_ACK; 1437c068736aSJeffrey Hsu 1438df8bae1dSRodney W. Grimes goto step6; 1439c068736aSJeffrey Hsu 1440a0292f23SGarrett Wollman /* 1441a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1442c94c54e4SAndre Oppermann * do normal processing. 1443a0292f23SGarrett Wollman * 1444c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 1445a0292f23SGarrett Wollman */ 1446a0292f23SGarrett Wollman case TCPS_LAST_ACK: 1447a0292f23SGarrett Wollman case TCPS_CLOSING: 1448a0292f23SGarrett Wollman case TCPS_TIME_WAIT: 1449340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1450a0292f23SGarrett Wollman break; /* continue normal processing */ 1451df8bae1dSRodney W. Grimes } 1452df8bae1dSRodney W. Grimes 1453df8bae1dSRodney W. Grimes /* 1454df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 145580ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 145680ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 145780ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 145880ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 145980ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 146080ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 146180ab7c0eSGarrett Wollman * killing a connection). 146280ab7c0eSGarrett Wollman * Then check timestamp, if present. 1463a0292f23SGarrett Wollman * Then check the connection count, if present. 1464df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 1465df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 1466df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 1467df8bae1dSRodney W. Grimes * 146880ab7c0eSGarrett Wollman * 146980ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 147080ab7c0eSGarrett Wollman * if this is a valid reset segment. 147180ab7c0eSGarrett Wollman * RFC 793 page 37: 147280ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 147380ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 147480ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 147580ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 147680ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 14771a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 14781a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 14791a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 14801a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 148180dd2a81SMike Silbersack * Note 2: Paul Watson's paper "Slipping in the Window" has shown 148280dd2a81SMike Silbersack * that brute force RST attacks are possible. To combat this, 148380dd2a81SMike Silbersack * we use a much stricter check while in the ESTABLISHED state, 148480dd2a81SMike Silbersack * only accepting RSTs where the sequence number is equal to 148580dd2a81SMike Silbersack * last_ack_sent. In all other states (the states in which a 148680dd2a81SMike Silbersack * RST is more likely), the more permissive check is used. 148780ab7c0eSGarrett Wollman * If we have multiple segments in flight, the intial reset 148880ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 148980ab7c0eSGarrett Wollman * but they will eventually catch up. 149080ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 149180ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 149280ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 149380ab7c0eSGarrett Wollman * 149480ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 149580ab7c0eSGarrett Wollman * 149680ab7c0eSGarrett Wollman * DISCUSSION 149780ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 149880ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 149980ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 150080ab7c0eSGarrett Wollman * data. 150180ab7c0eSGarrett Wollman * 150280ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 150380ab7c0eSGarrett Wollman * the state: 150480ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 150580ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 150680ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 1507745bab7fSDima Dorfman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 150880ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 1509e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 151080ab7c0eSGarrett Wollman * Close the tcb. 1511e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 151280ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 151380ab7c0eSGarrett Wollman * RFC 1337. 151480ab7c0eSGarrett Wollman */ 1515fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 1516fb59c426SYoshinobu Inoue if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 1517fb59c426SYoshinobu Inoue SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 151880ab7c0eSGarrett Wollman switch (tp->t_state) { 151980ab7c0eSGarrett Wollman 152080ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 152180ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 152280ab7c0eSGarrett Wollman goto close; 152380ab7c0eSGarrett Wollman 152480ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 152580dd2a81SMike Silbersack if (tp->last_ack_sent != th->th_seq) { 152680dd2a81SMike Silbersack tcpstat.tcps_badrst++; 152780dd2a81SMike Silbersack goto drop; 152880dd2a81SMike Silbersack } 152980ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 153080ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 153180ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 153280ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 153380ab7c0eSGarrett Wollman close: 153480ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 153580ab7c0eSGarrett Wollman tcpstat.tcps_drops++; 153680ab7c0eSGarrett Wollman tp = tcp_close(tp); 153780ab7c0eSGarrett Wollman break; 153880ab7c0eSGarrett Wollman 153980ab7c0eSGarrett Wollman case TCPS_CLOSING: 154080ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 154180ab7c0eSGarrett Wollman tp = tcp_close(tp); 154280ab7c0eSGarrett Wollman break; 154380ab7c0eSGarrett Wollman 154480ab7c0eSGarrett Wollman case TCPS_TIME_WAIT: 1545340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, 1546340c35deSJonathan Lemon ("timewait")); 154780ab7c0eSGarrett Wollman break; 154880ab7c0eSGarrett Wollman } 154980ab7c0eSGarrett Wollman } 155080ab7c0eSGarrett Wollman goto drop; 155180ab7c0eSGarrett Wollman } 155280ab7c0eSGarrett Wollman 155380ab7c0eSGarrett Wollman /* 1554df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 1555df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 1556df8bae1dSRodney W. Grimes */ 1557be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 155880ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1559df8bae1dSRodney W. Grimes 1560df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 15619b8b58e0SJonathan Lemon if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1562df8bae1dSRodney W. Grimes /* 1563df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 1564df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 1565df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 1566df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 1567df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 1568df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 1569df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 1570df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 1571df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 1572df8bae1dSRodney W. Grimes */ 1573df8bae1dSRodney W. Grimes tp->ts_recent = 0; 1574df8bae1dSRodney W. Grimes } else { 1575df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 1576fb59c426SYoshinobu Inoue tcpstat.tcps_rcvdupbyte += tlen; 1577df8bae1dSRodney W. Grimes tcpstat.tcps_pawsdrop++; 157807fd333dSMatthew Dillon if (tlen) 1579df8bae1dSRodney W. Grimes goto dropafterack; 15801ab4789dSMatthew Dillon goto drop; 15811ab4789dSMatthew Dillon } 1582df8bae1dSRodney W. Grimes } 1583df8bae1dSRodney W. Grimes 1584a0292f23SGarrett Wollman /* 158580ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 158680ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 158780ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 158880ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 158980ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 159080ab7c0eSGarrett Wollman */ 1591a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 1592a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1593a57815efSBosko Milekic goto dropwithreset; 1594a57815efSBosko Milekic } 159580ab7c0eSGarrett Wollman 1596fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 1597df8bae1dSRodney W. Grimes if (todrop > 0) { 1598fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1599fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 1600fb59c426SYoshinobu Inoue th->th_seq++; 1601fb59c426SYoshinobu Inoue if (th->th_urp > 1) 1602fb59c426SYoshinobu Inoue th->th_urp--; 1603df8bae1dSRodney W. Grimes else 1604fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1605df8bae1dSRodney W. Grimes todrop--; 1606df8bae1dSRodney W. Grimes } 1607df8bae1dSRodney W. Grimes /* 1608dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 1609df8bae1dSRodney W. Grimes */ 1610fb59c426SYoshinobu Inoue if (todrop > tlen 1611fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1612dac20301SGarrett Wollman /* 1613dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 1614dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 1615dac20301SGarrett Wollman * of sequence; drop it. 1616dac20301SGarrett Wollman */ 1617fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1618dac20301SGarrett Wollman 1619df8bae1dSRodney W. Grimes /* 1620dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 1621dac20301SGarrett Wollman * But keep on processing for RST or ACK. 1622df8bae1dSRodney W. Grimes */ 1623dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1624fb59c426SYoshinobu Inoue todrop = tlen; 1625dac20301SGarrett Wollman tcpstat.tcps_rcvduppack++; 1626dac20301SGarrett Wollman tcpstat.tcps_rcvdupbyte += todrop; 1627df8bae1dSRodney W. Grimes } else { 1628df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartduppack++; 1629df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartdupbyte += todrop; 1630df8bae1dSRodney W. Grimes } 1631fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 1632fb59c426SYoshinobu Inoue th->th_seq += todrop; 1633fb59c426SYoshinobu Inoue tlen -= todrop; 1634fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 1635fb59c426SYoshinobu Inoue th->th_urp -= todrop; 1636df8bae1dSRodney W. Grimes else { 1637fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1638fb59c426SYoshinobu Inoue th->th_urp = 0; 1639df8bae1dSRodney W. Grimes } 1640df8bae1dSRodney W. Grimes } 1641df8bae1dSRodney W. Grimes 1642df8bae1dSRodney W. Grimes /* 1643df8bae1dSRodney W. Grimes * If new data are received on a connection after the 1644df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 1645df8bae1dSRodney W. Grimes */ 1646df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 1647fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1648df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1649df8bae1dSRodney W. Grimes tcpstat.tcps_rcvafterclose++; 1650a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1651df8bae1dSRodney W. Grimes goto dropwithreset; 1652df8bae1dSRodney W. Grimes } 1653df8bae1dSRodney W. Grimes 1654df8bae1dSRodney W. Grimes /* 1655df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 1656df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 1657df8bae1dSRodney W. Grimes */ 1658fb59c426SYoshinobu Inoue todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd); 1659df8bae1dSRodney W. Grimes if (todrop > 0) { 1660df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1661fb59c426SYoshinobu Inoue if (todrop >= tlen) { 1662fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyteafterwin += tlen; 1663df8bae1dSRodney W. Grimes /* 1664df8bae1dSRodney W. Grimes * If a new connection request is received 1665df8bae1dSRodney W. Grimes * while in TIME_WAIT, drop the old connection 1666df8bae1dSRodney W. Grimes * and start over if the sequence numbers 1667df8bae1dSRodney W. Grimes * are above the previous ones. 1668df8bae1dSRodney W. Grimes */ 1669340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1670fb59c426SYoshinobu Inoue if (thflags & TH_SYN && 1671df8bae1dSRodney W. Grimes tp->t_state == TCPS_TIME_WAIT && 1672fb59c426SYoshinobu Inoue SEQ_GT(th->th_seq, tp->rcv_nxt)) { 1673df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1674df8bae1dSRodney W. Grimes goto findpcb; 1675df8bae1dSRodney W. Grimes } 1676df8bae1dSRodney W. Grimes /* 1677df8bae1dSRodney W. Grimes * If window is closed can only take segments at 1678df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 1679df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 1680df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 1681df8bae1dSRodney W. Grimes * and ack. 1682df8bae1dSRodney W. Grimes */ 1683fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1684df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1685df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinprobe++; 1686df8bae1dSRodney W. Grimes } else 1687df8bae1dSRodney W. Grimes goto dropafterack; 1688df8bae1dSRodney W. Grimes } else 1689df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1690df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1691fb59c426SYoshinobu Inoue tlen -= todrop; 1692fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 1693df8bae1dSRodney W. Grimes } 1694df8bae1dSRodney W. Grimes 1695df8bae1dSRodney W. Grimes /* 1696df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1697df8bae1dSRodney W. Grimes * record its timestamp. 1698a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1699a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1700df8bae1dSRodney W. Grimes */ 1701be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1702fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 17039b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1704a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1705df8bae1dSRodney W. Grimes } 1706df8bae1dSRodney W. Grimes 1707df8bae1dSRodney W. Grimes /* 1708df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 1709df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 1710df8bae1dSRodney W. Grimes */ 1711fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1712df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 1713a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1714122aad88SAndre Oppermann goto drop; 1715df8bae1dSRodney W. Grimes } 1716df8bae1dSRodney W. Grimes 1717a0292f23SGarrett Wollman /* 1718a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1719a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 1720a0292f23SGarrett Wollman * later processing; else drop segment and return. 1721a0292f23SGarrett Wollman */ 1722fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 1723a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 1724a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 1725a0292f23SGarrett Wollman goto step6; 1726a0292f23SGarrett Wollman else 1727a0292f23SGarrett Wollman goto drop; 1728a0292f23SGarrett Wollman } 1729df8bae1dSRodney W. Grimes 1730df8bae1dSRodney W. Grimes /* 1731df8bae1dSRodney W. Grimes * Ack processing. 1732df8bae1dSRodney W. Grimes */ 1733df8bae1dSRodney W. Grimes switch (tp->t_state) { 1734df8bae1dSRodney W. Grimes 1735df8bae1dSRodney W. Grimes /* 1736764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1737764d8cefSBill Fenner * ESTABLISHED state and continue processing. 1738764d8cefSBill Fenner * The ACK was checked above. 1739df8bae1dSRodney W. Grimes */ 1740df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1741a0292f23SGarrett Wollman 1742df8bae1dSRodney W. Grimes tcpstat.tcps_connects++; 1743df8bae1dSRodney W. Grimes soisconnected(so); 1744df8bae1dSRodney W. Grimes /* Do window scaling? */ 1745df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1746df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1747df8bae1dSRodney W. Grimes tp->snd_scale = tp->requested_s_scale; 1748df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 1749df8bae1dSRodney W. Grimes } 1750a0292f23SGarrett Wollman /* 1751a0292f23SGarrett Wollman * Make transitions: 1752a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 1753a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 1754a0292f23SGarrett Wollman */ 17559b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1756a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1757a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1758a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 17597ff19458SPaul Traina } else { 1760a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 17619b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, 17629b8b58e0SJonathan Lemon tcp_timer_keep, tp); 17637ff19458SPaul Traina } 1764a0292f23SGarrett Wollman /* 1765a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 1766a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 1767a0292f23SGarrett Wollman */ 1768fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 1769fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1770a0292f23SGarrett Wollman (struct mbuf *)0); 1771fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 177293b0017fSPhilippe Charnier /* FALLTHROUGH */ 1773df8bae1dSRodney W. Grimes 1774df8bae1dSRodney W. Grimes /* 1775df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1776df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 1777fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 1778fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 1779df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 1780df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 1781df8bae1dSRodney W. Grimes */ 1782df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1783df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1784df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 1785df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1786df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1787df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 1788df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 1789340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1790fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1791fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 1792df8bae1dSRodney W. Grimes tcpstat.tcps_rcvdupack++; 1793df8bae1dSRodney W. Grimes /* 1794df8bae1dSRodney W. Grimes * If we have outstanding data (other than 1795df8bae1dSRodney W. Grimes * a window probe), this is a completely 1796df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 1797df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 1798df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 1799df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 1800df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 1801df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 1802df8bae1dSRodney W. Grimes * window so we send only this one 1803df8bae1dSRodney W. Grimes * packet. 1804df8bae1dSRodney W. Grimes * 1805df8bae1dSRodney W. Grimes * We know we're losing at the current 1806df8bae1dSRodney W. Grimes * window size so do congestion avoidance 1807df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 1808df8bae1dSRodney W. Grimes * and pull our congestion window back to 1809df8bae1dSRodney W. Grimes * the new ssthresh). 1810df8bae1dSRodney W. Grimes * 1811df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 1812df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 1813df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 1814df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 1815df8bae1dSRodney W. Grimes * network. 1816df8bae1dSRodney W. Grimes */ 18179b8b58e0SJonathan Lemon if (!callout_active(tp->tt_rexmt) || 1818fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 1819df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1820cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 18216d90faf3SPaul Saab ((tcp_do_newreno || tp->sack_enable) && 18229d11646dSJeffrey Hsu IN_FASTRECOVERY(tp))) { 182346f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 182446f58482SJonathan Lemon (void) tcp_output(tp); 182546f58482SJonathan Lemon goto drop; 1826cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 1827cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 1828cb942153SJeffrey Hsu u_int win; 1829a0445c2eSJayanth Vijayaraghavan 1830a0445c2eSJayanth Vijayaraghavan /* 1831a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 1832a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 1833a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 1834a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 1835a0445c2eSJayanth Vijayaraghavan * recovery. 1836a0445c2eSJayanth Vijayaraghavan */ 1837a0445c2eSJayanth Vijayaraghavan if (tp->sack_enable) { 1838a0445c2eSJayanth Vijayaraghavan if (IN_FASTRECOVERY(tp)) { 1839a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 1840a0445c2eSJayanth Vijayaraghavan break; 1841a0445c2eSJayanth Vijayaraghavan } 1842a0445c2eSJayanth Vijayaraghavan } else if (tcp_do_newreno) { 1843a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 18449d11646dSJeffrey Hsu tp->snd_recover)) { 1845cb942153SJeffrey Hsu tp->t_dupacks = 0; 1846cb942153SJeffrey Hsu break; 184746f58482SJonathan Lemon } 1848a0445c2eSJayanth Vijayaraghavan } 1849cb942153SJeffrey Hsu win = min(tp->snd_wnd, tp->snd_cwnd) / 1850cb942153SJeffrey Hsu 2 / tp->t_maxseg; 1851df8bae1dSRodney W. Grimes if (win < 2) 1852df8bae1dSRodney W. Grimes win = 2; 1853df8bae1dSRodney W. Grimes tp->snd_ssthresh = win * tp->t_maxseg; 18549d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 185546f58482SJonathan Lemon tp->snd_recover = tp->snd_max; 18569b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 18579b8b58e0SJonathan Lemon tp->t_rtttime = 0; 18586d90faf3SPaul Saab if (tp->sack_enable) { 18596d90faf3SPaul Saab tcpstat.tcps_sack_recovery_episode++; 1860a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 18616d90faf3SPaul Saab tp->snd_cwnd = 1862a55db2b6SPaul Saab tp->t_maxseg * tcp_sack_recovery_initburst; 18636d90faf3SPaul Saab (void) tcp_output(tp); 1864a0445c2eSJayanth Vijayaraghavan tp->snd_cwnd += 18656d90faf3SPaul Saab tp->snd_ssthresh; 18666d90faf3SPaul Saab goto drop; 18676d90faf3SPaul Saab } 18686d90faf3SPaul Saab 1869fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 1870df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 1871df8bae1dSRodney W. Grimes (void) tcp_output(tp); 187248d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 187348d2549cSJeffrey Hsu ("tp->snd_limited too big")); 1874df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 187548d2549cSJeffrey Hsu tp->t_maxseg * 187648d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 1877df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 1878df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 1879df8bae1dSRodney W. Grimes goto drop; 1880582a954bSJeffrey Hsu } else if (tcp_do_rfc3042) { 1881582a954bSJeffrey Hsu u_long oldcwnd = tp->snd_cwnd; 188248d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 188348d2549cSJeffrey Hsu u_int sent; 188489c02376SJeffrey Hsu 1885582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 1886582a954bSJeffrey Hsu tp->t_dupacks == 2, 1887582a954bSJeffrey Hsu ("dupacks not 1 or 2")); 188861a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 188948d2549cSJeffrey Hsu tp->snd_limited = 0; 189061a36e3dSJeffrey Hsu tp->snd_cwnd = 189161a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 189261a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 189361a36e3dSJeffrey Hsu tp->t_maxseg; 1894582a954bSJeffrey Hsu (void) tcp_output(tp); 189548d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 189648d2549cSJeffrey Hsu if (sent > tp->t_maxseg) { 189789c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 189889c02376SJeffrey Hsu tp->snd_limited == 0) || 189989c02376SJeffrey Hsu (sent == tp->t_maxseg + 1 && 190089c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 190148d2549cSJeffrey Hsu ("sent too much")); 190248d2549cSJeffrey Hsu tp->snd_limited = 2; 190348d2549cSJeffrey Hsu } else if (sent > 0) 190448d2549cSJeffrey Hsu ++tp->snd_limited; 1905582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 1906582a954bSJeffrey Hsu goto drop; 1907df8bae1dSRodney W. Grimes } 1908df8bae1dSRodney W. Grimes } else 1909df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1910df8bae1dSRodney W. Grimes break; 1911df8bae1dSRodney W. Grimes } 1912cb942153SJeffrey Hsu 1913cb942153SJeffrey Hsu KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una")); 1914cb942153SJeffrey Hsu 1915df8bae1dSRodney W. Grimes /* 1916df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 1917df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 1918df8bae1dSRodney W. Grimes */ 19196d90faf3SPaul Saab if (tcp_do_newreno || tp->sack_enable) { 19209d11646dSJeffrey Hsu if (IN_FASTRECOVERY(tp)) { 1921cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 19226d90faf3SPaul Saab if (tp->sack_enable) 19236d90faf3SPaul Saab tcp_sack_partialack(tp, th); 19246d90faf3SPaul Saab else 1925c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 1926c068736aSJeffrey Hsu } else { 1927c068736aSJeffrey Hsu /* 19286d90faf3SPaul Saab * Out of fast recovery. 1929c068736aSJeffrey Hsu * Window inflation should have left us 1930c068736aSJeffrey Hsu * with approximately snd_ssthresh 1931c068736aSJeffrey Hsu * outstanding data. 1932c068736aSJeffrey Hsu * But in case we would be inclined to 1933c068736aSJeffrey Hsu * send a burst, better to do it via 1934c068736aSJeffrey Hsu * the slow start mechanism. 1935c068736aSJeffrey Hsu */ 1936c068736aSJeffrey Hsu if (SEQ_GT(th->th_ack + 1937c068736aSJeffrey Hsu tp->snd_ssthresh, 1938c068736aSJeffrey Hsu tp->snd_max)) 1939c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_max - 1940c068736aSJeffrey Hsu th->th_ack + 1941c068736aSJeffrey Hsu tp->t_maxseg; 1942c068736aSJeffrey Hsu else 1943c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_ssthresh; 1944c068736aSJeffrey Hsu } 1945c068736aSJeffrey Hsu } 1946c068736aSJeffrey Hsu } else { 1947233e8c18SGarrett Wollman if (tp->t_dupacks >= tcprexmtthresh && 1948df8bae1dSRodney W. Grimes tp->snd_cwnd > tp->snd_ssthresh) 1949df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh; 195046f58482SJonathan Lemon } 1951cb942153SJeffrey Hsu tp->t_dupacks = 0; 1952fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_max)) { 1953df8bae1dSRodney W. Grimes tcpstat.tcps_rcvacktoomuch++; 1954df8bae1dSRodney W. Grimes goto dropafterack; 1955df8bae1dSRodney W. Grimes } 1956a0292f23SGarrett Wollman /* 1957a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 1958a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 1959a0292f23SGarrett Wollman */ 1960a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 1961a0292f23SGarrett Wollman /* 1962a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 1963a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 196407e43e10SAndras Olah * synchronized). Go to non-starred state, 196507e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 196607e43e10SAndras Olah * we can do window scaling. 1967a0292f23SGarrett Wollman */ 1968a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 1969a0292f23SGarrett Wollman tp->snd_una++; 197007e43e10SAndras Olah /* Do window scaling? */ 197107e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 197207e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 197307e43e10SAndras Olah tp->snd_scale = tp->requested_s_scale; 197407e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 197507e43e10SAndras Olah } 1976a0292f23SGarrett Wollman } 1977a0292f23SGarrett Wollman 1978a0292f23SGarrett Wollman process_ACK: 19797cfc6904SRobert Watson KASSERT(headlocked, 19807cfc6904SRobert Watson ("tcp_input(): process_ACK head is not locked")); 19817cfc6904SRobert Watson INP_LOCK_ASSERT(inp); 19827cfc6904SRobert Watson 1983fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 1984df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 1985df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 1986df8bae1dSRodney W. Grimes 1987df8bae1dSRodney W. Grimes /* 19889b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 19899b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 19909b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 19919b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 19929b8b58e0SJonathan Lemon * we left off. 19939b8b58e0SJonathan Lemon */ 19949b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 1995d65bf08aSMatthew Dillon ++tcpstat.tcps_sndrexmitbad; 19969b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 19979b8b58e0SJonathan Lemon tp->snd_ssthresh = tp->snd_ssthresh_prev; 19989d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 19999d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 20009d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 20019b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 20029b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; /* XXX probably not required */ 20039b8b58e0SJonathan Lemon } 20049b8b58e0SJonathan Lemon 20059b8b58e0SJonathan Lemon /* 2006df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2007df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2008df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2009df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2010df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2011df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2012df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2013fa55172bSMatthew Dillon * 2014fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2015fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2016fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2017fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2018df8bae1dSRodney W. Grimes */ 2019fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 2020fa55172bSMatthew Dillon to.to_tsecr) { 20219b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 2022fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 20239b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2024fa55172bSMatthew Dillon } 20251fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 2026df8bae1dSRodney W. Grimes 2027df8bae1dSRodney W. Grimes /* 2028df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2029df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2030df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2031df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2032df8bae1dSRodney W. Grimes */ 2033fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 20349b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 2035df8bae1dSRodney W. Grimes needoutput = 1; 20369b8b58e0SJonathan Lemon } else if (!callout_active(tp->tt_persist)) 20379b8b58e0SJonathan Lemon callout_reset(tp->tt_rexmt, tp->t_rxtcur, 20389b8b58e0SJonathan Lemon tcp_timer_rexmt, tp); 2039a0292f23SGarrett Wollman 2040a0292f23SGarrett Wollman /* 2041a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2042a0292f23SGarrett Wollman * skip rest of ACK processing. 2043a0292f23SGarrett Wollman */ 2044a0292f23SGarrett Wollman if (acked == 0) 2045a0292f23SGarrett Wollman goto step6; 2046a0292f23SGarrett Wollman 2047df8bae1dSRodney W. Grimes /* 2048df8bae1dSRodney W. Grimes * When new data is acked, open the congestion window. 2049df8bae1dSRodney W. Grimes * If the window gives us less than ssthresh packets 2050df8bae1dSRodney W. Grimes * in flight, open exponentially (maxseg per packet). 2051df8bae1dSRodney W. Grimes * Otherwise open linearly: maxseg per window 205210be5648SGarrett Wollman * (maxseg^2 / cwnd per packet). 2053df8bae1dSRodney W. Grimes */ 20546d90faf3SPaul Saab if ((!tcp_do_newreno && !tp->sack_enable) || 20556d90faf3SPaul Saab !IN_FASTRECOVERY(tp)) { 2056df8bae1dSRodney W. Grimes register u_int cw = tp->snd_cwnd; 2057df8bae1dSRodney W. Grimes register u_int incr = tp->t_maxseg; 2058df8bae1dSRodney W. Grimes if (cw > tp->snd_ssthresh) 205910be5648SGarrett Wollman incr = incr * incr / cw; 2060df8bae1dSRodney W. Grimes tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale); 2061df8bae1dSRodney W. Grimes } 20625905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2063df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 2064df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 20655905999bSRobert Watson sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc); 2066df8bae1dSRodney W. Grimes ourfinisacked = 1; 2067df8bae1dSRodney W. Grimes } else { 20685905999bSRobert Watson sbdrop_locked(&so->so_snd, acked); 2069df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 2070df8bae1dSRodney W. Grimes ourfinisacked = 0; 2071df8bae1dSRodney W. Grimes } 20721e4d7da7SRobert Watson sowwakeup_locked(so); 2073cb942153SJeffrey Hsu /* detect una wraparound */ 20746d90faf3SPaul Saab if ((tcp_do_newreno || tp->sack_enable) && 20756d90faf3SPaul Saab !IN_FASTRECOVERY(tp) && 20769d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 20779d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 20789d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 20796d90faf3SPaul Saab if ((tcp_do_newreno || tp->sack_enable) && 20806d90faf3SPaul Saab IN_FASTRECOVERY(tp) && 20819d11646dSJeffrey Hsu SEQ_GEQ(th->th_ack, tp->snd_recover)) 20829d11646dSJeffrey Hsu EXIT_FASTRECOVERY(tp); 2083fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 20846d90faf3SPaul Saab if (tp->sack_enable) { 20856d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 20866d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 20876d90faf3SPaul Saab } 2088df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2089df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2090df8bae1dSRodney W. Grimes 2091df8bae1dSRodney W. Grimes switch (tp->t_state) { 2092df8bae1dSRodney W. Grimes 2093df8bae1dSRodney W. Grimes /* 2094df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2095df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2096df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2097df8bae1dSRodney W. Grimes */ 2098df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2099df8bae1dSRodney W. Grimes if (ourfinisacked) { 2100df8bae1dSRodney W. Grimes /* 2101df8bae1dSRodney W. Grimes * If we can't receive any more 2102df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2103df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2104df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2105df8bae1dSRodney W. Grimes * we'll hang forever. 2106df8bae1dSRodney W. Grimes */ 2107340c35deSJonathan Lemon /* XXXjl 2108340c35deSJonathan Lemon * we should release the tp also, and use a 2109340c35deSJonathan Lemon * compressed state. 2110340c35deSJonathan Lemon */ 2111c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 211203e49181SSeigo Tanimura soisdisconnected(so); 21139b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, tcp_maxidle, 21149b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 21154cc20ab1SSeigo Tanimura } 2116df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 2117df8bae1dSRodney W. Grimes } 2118df8bae1dSRodney W. Grimes break; 2119df8bae1dSRodney W. Grimes 2120df8bae1dSRodney W. Grimes /* 2121df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2122df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2123df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2124df8bae1dSRodney W. Grimes * the segment. 2125df8bae1dSRodney W. Grimes */ 2126df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2127df8bae1dSRodney W. Grimes if (ourfinisacked) { 2128340c35deSJonathan Lemon KASSERT(headlocked, ("headlocked")); 212911a20fb8SJeffrey Hsu tcp_twstart(tp); 2130340c35deSJonathan Lemon INP_INFO_WUNLOCK(&tcbinfo); 2131340c35deSJonathan Lemon m_freem(m); 2132340c35deSJonathan Lemon return; 2133df8bae1dSRodney W. Grimes } 2134df8bae1dSRodney W. Grimes break; 2135df8bae1dSRodney W. Grimes 2136df8bae1dSRodney W. Grimes /* 2137df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2138df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2139df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2140df8bae1dSRodney W. Grimes * enter the closed state and return. 2141df8bae1dSRodney W. Grimes */ 2142df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2143df8bae1dSRodney W. Grimes if (ourfinisacked) { 2144df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2145df8bae1dSRodney W. Grimes goto drop; 2146df8bae1dSRodney W. Grimes } 2147df8bae1dSRodney W. Grimes break; 2148df8bae1dSRodney W. Grimes 2149df8bae1dSRodney W. Grimes /* 2150df8bae1dSRodney W. Grimes * In TIME_WAIT state the only thing that should arrive 2151df8bae1dSRodney W. Grimes * is a retransmission of the remote FIN. Acknowledge 2152df8bae1dSRodney W. Grimes * it and restart the finack timer. 2153df8bae1dSRodney W. Grimes */ 2154df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 2155340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 21569b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 21579b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2158df8bae1dSRodney W. Grimes goto dropafterack; 2159df8bae1dSRodney W. Grimes } 2160df8bae1dSRodney W. Grimes } 2161df8bae1dSRodney W. Grimes 2162df8bae1dSRodney W. Grimes step6: 21637cfc6904SRobert Watson KASSERT(headlocked, ("tcp_input(): step6 head is not locked")); 21647cfc6904SRobert Watson INP_LOCK_ASSERT(inp); 21657cfc6904SRobert Watson 2166df8bae1dSRodney W. Grimes /* 2167df8bae1dSRodney W. Grimes * Update window information. 2168df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 2169df8bae1dSRodney W. Grimes */ 2170fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 2171fb59c426SYoshinobu Inoue (SEQ_LT(tp->snd_wl1, th->th_seq) || 2172fb59c426SYoshinobu Inoue (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2173fb59c426SYoshinobu Inoue (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2174df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 2175fb59c426SYoshinobu Inoue if (tlen == 0 && 2176fb59c426SYoshinobu Inoue tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2177df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinupd++; 2178df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 2179fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq; 2180fb59c426SYoshinobu Inoue tp->snd_wl2 = th->th_ack; 2181df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2182df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 2183df8bae1dSRodney W. Grimes needoutput = 1; 2184df8bae1dSRodney W. Grimes } 2185df8bae1dSRodney W. Grimes 2186df8bae1dSRodney W. Grimes /* 2187df8bae1dSRodney W. Grimes * Process segments with URG. 2188df8bae1dSRodney W. Grimes */ 2189fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2190df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2191df8bae1dSRodney W. Grimes /* 2192df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2193df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2194df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2195df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2196df8bae1dSRodney W. Grimes */ 2197fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2198fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2199fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 2200df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2201df8bae1dSRodney W. Grimes } 2202df8bae1dSRodney W. Grimes /* 2203df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2204df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2205df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2206df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2207df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2208df8bae1dSRodney W. Grimes * 2209df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2210df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2211df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2212df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2213df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2214df8bae1dSRodney W. Grimes * spec states (in one of two places). 2215df8bae1dSRodney W. Grimes */ 2216fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2217fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2218927c5ceaSRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2219df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2220df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2221927c5ceaSRobert Watson if (so->so_oobmark == 0) 2222c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 22237721f5d7SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2224df8bae1dSRodney W. Grimes sohasoutofband(so); 2225df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2226df8bae1dSRodney W. Grimes } 2227df8bae1dSRodney W. Grimes /* 2228df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2229df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2230df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2231df8bae1dSRodney W. Grimes * data may creep in... ick. 2232df8bae1dSRodney W. Grimes */ 223330613f56SJeffrey Hsu if (th->th_urp <= (u_long)tlen && 223430613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 223530613f56SJeffrey Hsu /* hdr drop is delayed */ 223630613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 223730613f56SJeffrey Hsu } 2238c068736aSJeffrey Hsu } else { 2239df8bae1dSRodney W. Grimes /* 2240df8bae1dSRodney W. Grimes * If no out of band data is expected, 2241df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2242df8bae1dSRodney W. Grimes * with the receive window. 2243df8bae1dSRodney W. Grimes */ 2244df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2245df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2246c068736aSJeffrey Hsu } 2247df8bae1dSRodney W. Grimes dodata: /* XXX */ 22487cfc6904SRobert Watson KASSERT(headlocked, ("tcp_input(): dodata head is not locked")); 22497cfc6904SRobert Watson INP_LOCK_ASSERT(inp); 22507cfc6904SRobert Watson 2251df8bae1dSRodney W. Grimes /* 2252df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2253df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2254df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2255df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2256df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2257df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2258df8bae1dSRodney W. Grimes */ 2259fb59c426SYoshinobu Inoue if ((tlen || (thflags & TH_FIN)) && 2260df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2261fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2262e4b64281SJesper Skriver /* 2263c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2264c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2265c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2266c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2267c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2268c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2269c068736aSJeffrey Hsu * conversions. 2270c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 2271c068736aSJeffrey Hsu * immediately when segments are out of order (so 2272c068736aSJeffrey Hsu * fast retransmit can work). 2273e4b64281SJesper Skriver */ 2274e4b64281SJesper Skriver if (th->th_seq == tp->rcv_nxt && 2275e4b64281SJesper Skriver LIST_EMPTY(&tp->t_segq) && 2276e4b64281SJesper Skriver TCPS_HAVEESTABLISHED(tp->t_state)) { 2277e4b64281SJesper Skriver if (DELAY_ACK(tp)) 22783bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2279e4b64281SJesper Skriver else 2280e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2281e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2282e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 2283e4b64281SJesper Skriver tcpstat.tcps_rcvpack++; 2284e4b64281SJesper Skriver tcpstat.tcps_rcvbyte += tlen; 2285e4b64281SJesper Skriver ND6_HINT(tp); 22861e4d7da7SRobert Watson /* Unlocked read. */ 22871e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2288c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2289c1c36a2cSMike Silbersack m_freem(m); 2290c1c36a2cSMike Silbersack else 22911e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 22921e4d7da7SRobert Watson sorwakeup_locked(so); 2293e4b64281SJesper Skriver } else { 2294e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 2295e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2296e4b64281SJesper Skriver } 22976d90faf3SPaul Saab if (tp->sack_enable) 22986d90faf3SPaul Saab tcp_update_sack_list(tp); 2299df8bae1dSRodney W. Grimes /* 2300df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2301df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2302df8bae1dSRodney W. Grimes * buffer size. 2303df8bae1dSRodney W. Grimes */ 2304df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2305df8bae1dSRodney W. Grimes } else { 2306df8bae1dSRodney W. Grimes m_freem(m); 2307fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2308df8bae1dSRodney W. Grimes } 2309df8bae1dSRodney W. Grimes 2310df8bae1dSRodney W. Grimes /* 2311df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2312df8bae1dSRodney W. Grimes * that the connection is closing. 2313df8bae1dSRodney W. Grimes */ 2314fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2315df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2316df8bae1dSRodney W. Grimes socantrcvmore(so); 2317a0292f23SGarrett Wollman /* 2318a0292f23SGarrett Wollman * If connection is half-synchronized 2319d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2320a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2321a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2322a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2323a0292f23SGarrett Wollman */ 23243bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 23253bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2326a0292f23SGarrett Wollman else 2327df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2328df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2329df8bae1dSRodney W. Grimes } 2330df8bae1dSRodney W. Grimes switch (tp->t_state) { 2331df8bae1dSRodney W. Grimes 2332df8bae1dSRodney W. Grimes /* 2333df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2334df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2335df8bae1dSRodney W. Grimes */ 2336df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 23379b8b58e0SJonathan Lemon tp->t_starttime = ticks; 23389b8b58e0SJonathan Lemon /*FALLTHROUGH*/ 2339df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2340df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2341df8bae1dSRodney W. Grimes break; 2342df8bae1dSRodney W. Grimes 2343df8bae1dSRodney W. Grimes /* 2344df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2345df8bae1dSRodney W. Grimes * enter the CLOSING state. 2346df8bae1dSRodney W. Grimes */ 2347df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2348df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 2349df8bae1dSRodney W. Grimes break; 2350df8bae1dSRodney W. Grimes 2351df8bae1dSRodney W. Grimes /* 2352df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 2353df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 2354df8bae1dSRodney W. Grimes * standard timers. 2355df8bae1dSRodney W. Grimes */ 2356df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 235711a20fb8SJeffrey Hsu KASSERT(headlocked == 1, ("headlocked should be 1")); 2358340c35deSJonathan Lemon tcp_twstart(tp); 235911a20fb8SJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2360340c35deSJonathan Lemon return; 2361df8bae1dSRodney W. Grimes 2362df8bae1dSRodney W. Grimes /* 2363df8bae1dSRodney W. Grimes * In TIME_WAIT state restart the 2 MSL time_wait timer. 2364df8bae1dSRodney W. Grimes */ 2365df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 2366340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 23679b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 23689b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2369df8bae1dSRodney W. Grimes break; 2370df8bae1dSRodney W. Grimes } 2371df8bae1dSRodney W. Grimes } 2372610ee2f9SDavid Greenman #ifdef TCPDEBUG 23734cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2374fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2375fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2376610ee2f9SDavid Greenman #endif 2377df8bae1dSRodney W. Grimes 2378df8bae1dSRodney W. Grimes /* 2379df8bae1dSRodney W. Grimes * Return any desired output. 2380df8bae1dSRodney W. Grimes */ 2381df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 2382df8bae1dSRodney W. Grimes (void) tcp_output(tp); 23837792ea27SJeffrey Hsu 2384a14c749fSJonathan Lemon check_delack: 2385d6915262SRobert Watson KASSERT(headlocked == 1, ("headlocked should be 1")); 23867cfc6904SRobert Watson INP_LOCK_ASSERT(inp); 2387a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 23883bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 23893bfd6421SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 23903bfd6421SJonathan Lemon tcp_timer_delack, tp); 23913bfd6421SJonathan Lemon } 2392f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 2393d6915262SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 2394df8bae1dSRodney W. Grimes return; 2395df8bae1dSRodney W. Grimes 2396df8bae1dSRodney W. Grimes dropafterack: 2397df8bae1dSRodney W. Grimes /* 2398df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 2399df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 240080ab7c0eSGarrett Wollman * 240180ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 240280ab7c0eSGarrett Wollman * paths to this code happen after packets containing 240380ab7c0eSGarrett Wollman * RST have been dropped. 240480ab7c0eSGarrett Wollman * 240580ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 240680ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 240780ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 240880ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 240980ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 241080ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 2411df8bae1dSRodney W. Grimes */ 2412fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2413fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 2414a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 2415a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2416a57815efSBosko Milekic goto dropwithreset; 2417a57815efSBosko Milekic } 2418a0292f23SGarrett Wollman #ifdef TCPDEBUG 24194cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2420fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2421fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2422a0292f23SGarrett Wollman #endif 242385e8b243SJeffrey Hsu KASSERT(headlocked, ("headlocked should be 1")); 2424df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2425df8bae1dSRodney W. Grimes (void) tcp_output(tp); 2426f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 2427d6915262SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 2428d6915262SRobert Watson m_freem(m); 2429df8bae1dSRodney W. Grimes return; 2430df8bae1dSRodney W. Grimes 2431df8bae1dSRodney W. Grimes dropwithreset: 2432df8bae1dSRodney W. Grimes /* 2433df8bae1dSRodney W. Grimes * Generate a RST, dropping incoming segment. 2434df8bae1dSRodney W. Grimes * Make ACK acceptable to originator of segment. 2435df8bae1dSRodney W. Grimes * Don't bother to respond if destination was broadcast/multicast. 2436df8bae1dSRodney W. Grimes */ 2437fb59c426SYoshinobu Inoue if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2438df8bae1dSRodney W. Grimes goto drop; 2439fb59c426SYoshinobu Inoue if (isipv6) { 2440173c0f9fSWarner Losh if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2441173c0f9fSWarner Losh IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2442fb59c426SYoshinobu Inoue goto drop; 2443c068736aSJeffrey Hsu } else { 2444173c0f9fSWarner Losh if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2445173c0f9fSWarner Losh IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 24462ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 24472ca2159fSCrist J. Clark in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2448fb59c426SYoshinobu Inoue goto drop; 2449c068736aSJeffrey Hsu } 2450fb59c426SYoshinobu Inoue /* IPv6 anycast check is done at tcp6_input() */ 2451a57815efSBosko Milekic 2452a57815efSBosko Milekic /* 2453c59319bfSDag-Erling Smørgrav * Perform bandwidth limiting. 2454a57815efSBosko Milekic */ 2455a57815efSBosko Milekic if (badport_bandlim(rstreason) < 0) 2456a57815efSBosko Milekic goto drop; 2457a57815efSBosko Milekic 2458a0292f23SGarrett Wollman #ifdef TCPDEBUG 24594cc20ab1SSeigo Tanimura if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2460fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2461fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2462a0292f23SGarrett Wollman #endif 24636fd22cafSJeffrey Hsu 2464fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 2465fb59c426SYoshinobu Inoue /* mtod() below is safe as long as hdr dropping is delayed */ 2466fb59c426SYoshinobu Inoue tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack, 2467fb59c426SYoshinobu Inoue TH_RST); 2468df8bae1dSRodney W. Grimes else { 2469fb59c426SYoshinobu Inoue if (thflags & TH_SYN) 2470fb59c426SYoshinobu Inoue tlen++; 2471fb59c426SYoshinobu Inoue /* mtod() below is safe as long as hdr dropping is delayed */ 2472fb59c426SYoshinobu Inoue tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2473fb59c426SYoshinobu Inoue (tcp_seq)0, TH_RST|TH_ACK); 2474df8bae1dSRodney W. Grimes } 2475c29afad6SSam Leffler 2476c29afad6SSam Leffler if (tp) 2477c29afad6SSam Leffler INP_UNLOCK(inp); 2478f76fcf6dSJeffrey Hsu if (headlocked) 2479f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2480df8bae1dSRodney W. Grimes return; 2481df8bae1dSRodney W. Grimes 2482df8bae1dSRodney W. Grimes drop: 2483df8bae1dSRodney W. Grimes /* 2484df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 2485df8bae1dSRodney W. Grimes */ 2486610ee2f9SDavid Greenman #ifdef TCPDEBUG 24874cc20ab1SSeigo Tanimura if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2488fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2489fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2490a0292f23SGarrett Wollman #endif 2491f76fcf6dSJeffrey Hsu if (tp) 2492f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 2493f76fcf6dSJeffrey Hsu if (headlocked) 2494f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2495d6915262SRobert Watson m_freem(m); 2496df8bae1dSRodney W. Grimes return; 2497df8bae1dSRodney W. Grimes } 2498df8bae1dSRodney W. Grimes 2499be2ac88cSJonathan Lemon /* 2500be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 2501be2ac88cSJonathan Lemon */ 25020312fbe9SPoul-Henning Kamp static void 25036d90faf3SPaul Saab tcp_dooptions(tp, to, cp, cnt, is_syn, th) 25046d90faf3SPaul Saab struct tcpcb *tp; 2505be2ac88cSJonathan Lemon struct tcpopt *to; 2506df8bae1dSRodney W. Grimes u_char *cp; 2507df8bae1dSRodney W. Grimes int cnt; 2508152385d1SDavid E. O'Brien int is_syn; 25096d90faf3SPaul Saab struct tcphdr *th; 2510df8bae1dSRodney W. Grimes { 2511df8bae1dSRodney W. Grimes int opt, optlen; 2512df8bae1dSRodney W. Grimes 2513be2ac88cSJonathan Lemon to->to_flags = 0; 2514df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 2515df8bae1dSRodney W. Grimes opt = cp[0]; 2516df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 2517df8bae1dSRodney W. Grimes break; 2518df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 2519df8bae1dSRodney W. Grimes optlen = 1; 2520df8bae1dSRodney W. Grimes else { 2521b474779fSJun-ichiro itojun Hagino if (cnt < 2) 2522b474779fSJun-ichiro itojun Hagino break; 2523df8bae1dSRodney W. Grimes optlen = cp[1]; 2524b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 2525df8bae1dSRodney W. Grimes break; 2526df8bae1dSRodney W. Grimes } 2527df8bae1dSRodney W. Grimes switch (opt) { 2528df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 2529df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 2530df8bae1dSRodney W. Grimes continue; 2531be2ac88cSJonathan Lemon if (!is_syn) 2532df8bae1dSRodney W. Grimes continue; 2533be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 2534be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 2535be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 2536fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 2537df8bae1dSRodney W. Grimes break; 2538df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 2539df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 2540df8bae1dSRodney W. Grimes continue; 2541be2ac88cSJonathan Lemon if (! is_syn) 2542df8bae1dSRodney W. Grimes continue; 2543be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 2544be2ac88cSJonathan Lemon to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 2545df8bae1dSRodney W. Grimes break; 2546df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 2547df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 2548df8bae1dSRodney W. Grimes continue; 2549be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 2550a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2551a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 2552fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 2553a0292f23SGarrett Wollman bcopy((char *)cp + 6, 2554a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2555fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 2556df8bae1dSRodney W. Grimes break; 25571cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 25581cfd4b53SBruce M Simpson /* 25591cfd4b53SBruce M Simpson * XXX In order to reply to a host which has set the 25601cfd4b53SBruce M Simpson * TCP_SIGNATURE option in its initial SYN, we have to 25611cfd4b53SBruce M Simpson * record the fact that the option was observed here 25621cfd4b53SBruce M Simpson * for the syncache code to perform the correct response. 25631cfd4b53SBruce M Simpson */ 25641cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 25651cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 25661cfd4b53SBruce M Simpson continue; 25671cfd4b53SBruce M Simpson to->to_flags |= (TOF_SIGNATURE | TOF_SIGLEN); 25681cfd4b53SBruce M Simpson break; 2569265ed012SBruce M Simpson #endif 25706d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 25716d90faf3SPaul Saab if (!tcp_do_sack || 25726d90faf3SPaul Saab optlen != TCPOLEN_SACK_PERMITTED) 25736d90faf3SPaul Saab continue; 25746d90faf3SPaul Saab if (is_syn) { 25756d90faf3SPaul Saab /* MUST only be set on SYN */ 25766d90faf3SPaul Saab to->to_flags |= TOF_SACK; 25776d90faf3SPaul Saab } 25786d90faf3SPaul Saab break; 25796d90faf3SPaul Saab 25806d90faf3SPaul Saab case TCPOPT_SACK: 25816d90faf3SPaul Saab if (!tp || tcp_sack_option(tp, th, cp, optlen)) 25826d90faf3SPaul Saab continue; 25836d90faf3SPaul Saab break; 2584be2ac88cSJonathan Lemon default: 2585be2ac88cSJonathan Lemon continue; 2586df8bae1dSRodney W. Grimes } 2587df8bae1dSRodney W. Grimes } 2588df8bae1dSRodney W. Grimes } 2589df8bae1dSRodney W. Grimes 2590df8bae1dSRodney W. Grimes /* 2591df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 2592df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 2593df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 2594df8bae1dSRodney W. Grimes * sequencing purposes. 2595df8bae1dSRodney W. Grimes */ 25960312fbe9SPoul-Henning Kamp static void 2597fb59c426SYoshinobu Inoue tcp_pulloutofband(so, th, m, off) 2598df8bae1dSRodney W. Grimes struct socket *so; 2599fb59c426SYoshinobu Inoue struct tcphdr *th; 2600df8bae1dSRodney W. Grimes register struct mbuf *m; 2601fb59c426SYoshinobu Inoue int off; /* delayed to be droped hdrlen */ 2602df8bae1dSRodney W. Grimes { 2603fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 2604df8bae1dSRodney W. Grimes 2605df8bae1dSRodney W. Grimes while (cnt >= 0) { 2606df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 2607df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 2608df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 2609df8bae1dSRodney W. Grimes 2610df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 2611df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 2612df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2613df8bae1dSRodney W. Grimes m->m_len--; 261469a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 261569a34685SYoshinobu Inoue m->m_pkthdr.len--; 2616df8bae1dSRodney W. Grimes return; 2617df8bae1dSRodney W. Grimes } 2618df8bae1dSRodney W. Grimes cnt -= m->m_len; 2619df8bae1dSRodney W. Grimes m = m->m_next; 2620df8bae1dSRodney W. Grimes if (m == 0) 2621df8bae1dSRodney W. Grimes break; 2622df8bae1dSRodney W. Grimes } 2623df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 2624df8bae1dSRodney W. Grimes } 2625df8bae1dSRodney W. Grimes 2626df8bae1dSRodney W. Grimes /* 2627df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 2628df8bae1dSRodney W. Grimes * and update averages and current timeout. 2629df8bae1dSRodney W. Grimes */ 26300312fbe9SPoul-Henning Kamp static void 2631df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt) 2632df8bae1dSRodney W. Grimes register struct tcpcb *tp; 26339b8b58e0SJonathan Lemon int rtt; 2634df8bae1dSRodney W. Grimes { 2635233e8c18SGarrett Wollman register int delta; 2636233e8c18SGarrett Wollman 2637233e8c18SGarrett Wollman tcpstat.tcps_rttupdated++; 2638233e8c18SGarrett Wollman tp->t_rttupdated++; 2639233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 2640233e8c18SGarrett Wollman /* 2641233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 2642233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 2643233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 2644233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2645233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 2646233e8c18SGarrett Wollman */ 2647233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2648233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2649233e8c18SGarrett Wollman 2650233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 2651233e8c18SGarrett Wollman tp->t_srtt = 1; 2652233e8c18SGarrett Wollman 2653233e8c18SGarrett Wollman /* 2654233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 2655233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 2656233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 2657233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 2658233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 2659233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 2660233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2661233e8c18SGarrett Wollman * rfc793's wired-in beta. 2662233e8c18SGarrett Wollman */ 2663233e8c18SGarrett Wollman if (delta < 0) 2664233e8c18SGarrett Wollman delta = -delta; 2665233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2666233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 2667233e8c18SGarrett Wollman tp->t_rttvar = 1; 26681fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 26691fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2670233e8c18SGarrett Wollman } else { 2671233e8c18SGarrett Wollman /* 2672233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 2673233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 2674233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 2675233e8c18SGarrett Wollman */ 2676233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 2677233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 26781fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2679233e8c18SGarrett Wollman } 26809b8b58e0SJonathan Lemon tp->t_rtttime = 0; 2681df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 2682df8bae1dSRodney W. Grimes 2683df8bae1dSRodney W. Grimes /* 2684df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 2685df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 2686df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 2687df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 2688df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 2689df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 2690df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 2691df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 2692df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 2693df8bae1dSRodney W. Grimes */ 2694233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 26959e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2696df8bae1dSRodney W. Grimes 2697df8bae1dSRodney W. Grimes /* 2698df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 2699df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 2700df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 2701df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 2702df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 2703df8bae1dSRodney W. Grimes */ 2704df8bae1dSRodney W. Grimes tp->t_softerror = 0; 2705df8bae1dSRodney W. Grimes } 2706df8bae1dSRodney W. Grimes 2707df8bae1dSRodney W. Grimes /* 2708df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 2709df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 2710df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 2711df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 2712df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2713df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 2714df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 2715df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 2716df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 2717df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 2718df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 2719df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 2720df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 2721a0292f23SGarrett Wollman * 2722a0292f23SGarrett Wollman * Also take into account the space needed for options that we 2723a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 2724a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 2725a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 2726a0292f23SGarrett Wollman * data in maxopd. 2727a0292f23SGarrett Wollman * 2728a0292f23SGarrett Wollman * 2729a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 2730a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 2731a0292f23SGarrett Wollman * MSS of our peer. 273297d8d152SAndre Oppermann * 273397d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 273497d8d152SAndre Oppermann * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt(). 2735df8bae1dSRodney W. Grimes */ 2736a0292f23SGarrett Wollman void 2737df8bae1dSRodney W. Grimes tcp_mss(tp, offer) 2738a0292f23SGarrett Wollman struct tcpcb *tp; 2739a0292f23SGarrett Wollman int offer; 2740df8bae1dSRodney W. Grimes { 274197d8d152SAndre Oppermann int rtt, mss; 2742df8bae1dSRodney W. Grimes u_long bufsize; 274397d8d152SAndre Oppermann u_long maxmtu; 2744c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 2745df8bae1dSRodney W. Grimes struct socket *so; 274697d8d152SAndre Oppermann struct hc_metrics_lite metrics; 2747a0292f23SGarrett Wollman int origoffer = offer; 2748fb59c426SYoshinobu Inoue #ifdef INET6 2749c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2750c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 2751c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 2752c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 2753c068736aSJeffrey Hsu #else 2754c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 2755fb59c426SYoshinobu Inoue #endif 2756df8bae1dSRodney W. Grimes 275797d8d152SAndre Oppermann /* initialize */ 275897d8d152SAndre Oppermann #ifdef INET6 275997d8d152SAndre Oppermann if (isipv6) { 276097d8d152SAndre Oppermann maxmtu = tcp_maxmtu6(&inp->inp_inc); 276197d8d152SAndre Oppermann tp->t_maxopd = tp->t_maxseg = tcp_v6mssdflt; 276297d8d152SAndre Oppermann } else 276397d8d152SAndre Oppermann #endif 276497d8d152SAndre Oppermann { 276597d8d152SAndre Oppermann maxmtu = tcp_maxmtu(&inp->inp_inc); 276697d8d152SAndre Oppermann tp->t_maxopd = tp->t_maxseg = tcp_mssdflt; 2767df8bae1dSRodney W. Grimes } 2768df8bae1dSRodney W. Grimes so = inp->inp_socket; 2769df8bae1dSRodney W. Grimes 277097d8d152SAndre Oppermann /* 27712d166c02SAndre Oppermann * no route to sender, stay with default mss and return 277297d8d152SAndre Oppermann */ 277397d8d152SAndre Oppermann if (maxmtu == 0) 277497d8d152SAndre Oppermann return; 277597d8d152SAndre Oppermann 277697d8d152SAndre Oppermann /* what have we got? */ 277797d8d152SAndre Oppermann switch (offer) { 277897d8d152SAndre Oppermann case 0: 277997d8d152SAndre Oppermann /* 278097d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 278197d8d152SAndre Oppermann * segment, in this case we use tcp_mssdflt. 278297d8d152SAndre Oppermann */ 278397d8d152SAndre Oppermann offer = 278497d8d152SAndre Oppermann #ifdef INET6 278597d8d152SAndre Oppermann isipv6 ? tcp_v6mssdflt : 278697d8d152SAndre Oppermann #endif 278797d8d152SAndre Oppermann tcp_mssdflt; 278897d8d152SAndre Oppermann break; 278997d8d152SAndre Oppermann 279097d8d152SAndre Oppermann case -1: 2791a0292f23SGarrett Wollman /* 2792c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 2793a0292f23SGarrett Wollman */ 279497d8d152SAndre Oppermann /* FALLTHROUGH */ 279597d8d152SAndre Oppermann 279697d8d152SAndre Oppermann default: 2797a0292f23SGarrett Wollman /* 279853369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 279953369ac9SAndre Oppermann * to at least minmss. 280053369ac9SAndre Oppermann */ 280153369ac9SAndre Oppermann offer = max(offer, tcp_minmss); 280253369ac9SAndre Oppermann /* 2803a0292f23SGarrett Wollman * Sanity check: make sure that maxopd will be large 280497d8d152SAndre Oppermann * enough to allow some data on segments even if the 2805a0292f23SGarrett Wollman * all the option space is used (40bytes). Otherwise 2806a0292f23SGarrett Wollman * funny things may happen in tcp_output. 2807a0292f23SGarrett Wollman */ 2808a0292f23SGarrett Wollman offer = max(offer, 64); 280997d8d152SAndre Oppermann } 2810a0292f23SGarrett Wollman 2811df8bae1dSRodney W. Grimes /* 281297d8d152SAndre Oppermann * rmx information is now retrieved from tcp_hostcache 2813df8bae1dSRodney W. Grimes */ 281497d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 281597d8d152SAndre Oppermann 2816df8bae1dSRodney W. Grimes /* 281797d8d152SAndre Oppermann * if there's a discovered mtu int tcp hostcache, use it 2818fb59c426SYoshinobu Inoue * else, use the link mtu. 2819df8bae1dSRodney W. Grimes */ 282097d8d152SAndre Oppermann if (metrics.rmx_mtu) 28212d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 2822c068736aSJeffrey Hsu else { 282331b3783cSHajimu UMEMOTO #ifdef INET6 2824fb59c426SYoshinobu Inoue if (isipv6) { 282597d8d152SAndre Oppermann mss = maxmtu - min_protoh; 282697d8d152SAndre Oppermann if (!path_mtu_discovery && 282797d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 2828fb59c426SYoshinobu Inoue mss = min(mss, tcp_v6mssdflt); 2829b3399803SHajimu UMEMOTO } else 2830b3399803SHajimu UMEMOTO #endif 283197d8d152SAndre Oppermann { 283297d8d152SAndre Oppermann mss = maxmtu - min_protoh; 283397d8d152SAndre Oppermann if (!path_mtu_discovery && 283497d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 2835a0292f23SGarrett Wollman mss = min(mss, tcp_mssdflt); 2836a0292f23SGarrett Wollman } 283797d8d152SAndre Oppermann } 2838a0292f23SGarrett Wollman mss = min(mss, offer); 283997d8d152SAndre Oppermann 2840a0292f23SGarrett Wollman /* 2841a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 2842a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 2843a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 2844a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 2845a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 2846a0292f23SGarrett Wollman */ 2847a0292f23SGarrett Wollman tp->t_maxopd = mss; 2848a0292f23SGarrett Wollman 2849a0292f23SGarrett Wollman /* 2850c94c54e4SAndre Oppermann * origoffer==-1 indicates, that no segments were received yet. 2851c94c54e4SAndre Oppermann * In this case we just guess. 2852a0292f23SGarrett Wollman */ 2853a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2854a0292f23SGarrett Wollman (origoffer == -1 || 2855a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2856a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 285797d8d152SAndre Oppermann tp->t_maxseg = mss; 2858a0292f23SGarrett Wollman 2859df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 2860df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2861df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 2862df8bae1dSRodney W. Grimes #else 2863df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2864df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 2865df8bae1dSRodney W. Grimes #endif 286697d8d152SAndre Oppermann tp->t_maxseg = mss; 286797d8d152SAndre Oppermann 2868df8bae1dSRodney W. Grimes /* 286997d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 287097d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 287197d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 287297d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 287397d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 2874df8bae1dSRodney W. Grimes */ 28753f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 287697d8d152SAndre Oppermann if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe) 287797d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 287897d8d152SAndre Oppermann else 2879df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 2880df8bae1dSRodney W. Grimes if (bufsize < mss) 2881df8bae1dSRodney W. Grimes mss = bufsize; 2882df8bae1dSRodney W. Grimes else { 2883df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2884df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2885df8bae1dSRodney W. Grimes bufsize = sb_max; 288688c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 28873f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 2888df8bae1dSRodney W. Grimes } 28893f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 2890df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 2891df8bae1dSRodney W. Grimes 28923f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 289397d8d152SAndre Oppermann if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe) 289497d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 289597d8d152SAndre Oppermann else 2896df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 2897df8bae1dSRodney W. Grimes if (bufsize > mss) { 2898df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2899df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2900df8bae1dSRodney W. Grimes bufsize = sb_max; 290188c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 29023f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 2903df8bae1dSRodney W. Grimes } 29043f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2905a0292f23SGarrett Wollman /* 290697d8d152SAndre Oppermann * While we're here, check the others too 2907a0292f23SGarrett Wollman */ 290897d8d152SAndre Oppermann if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 290997d8d152SAndre Oppermann tp->t_srtt = rtt; 291097d8d152SAndre Oppermann tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 291197d8d152SAndre Oppermann tcpstat.tcps_usedrtt++; 291297d8d152SAndre Oppermann if (metrics.rmx_rttvar) { 291397d8d152SAndre Oppermann tp->t_rttvar = metrics.rmx_rttvar; 291497d8d152SAndre Oppermann tcpstat.tcps_usedrttvar++; 291597d8d152SAndre Oppermann } else { 291697d8d152SAndre Oppermann /* default variation is +- 1 rtt */ 291797d8d152SAndre Oppermann tp->t_rttvar = 291897d8d152SAndre Oppermann tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 291997d8d152SAndre Oppermann } 292097d8d152SAndre Oppermann TCPT_RANGESET(tp->t_rxtcur, 292197d8d152SAndre Oppermann ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 292297d8d152SAndre Oppermann tp->t_rttmin, TCPTV_REXMTMAX); 292397d8d152SAndre Oppermann } 292497d8d152SAndre Oppermann if (metrics.rmx_ssthresh) { 2925df8bae1dSRodney W. Grimes /* 2926df8bae1dSRodney W. Grimes * There's some sort of gateway or interface 2927df8bae1dSRodney W. Grimes * buffer limit on the path. Use this to set 2928df8bae1dSRodney W. Grimes * the slow start threshhold, but set the 2929df8bae1dSRodney W. Grimes * threshold to no less than 2*mss. 2930df8bae1dSRodney W. Grimes */ 293197d8d152SAndre Oppermann tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh); 2932dd224982SGarrett Wollman tcpstat.tcps_usedssthresh++; 2933df8bae1dSRodney W. Grimes } 293497d8d152SAndre Oppermann if (metrics.rmx_bandwidth) 293597d8d152SAndre Oppermann tp->snd_bandwidth = metrics.rmx_bandwidth; 293697d8d152SAndre Oppermann 293797d8d152SAndre Oppermann /* 293897d8d152SAndre Oppermann * Set the slow-start flight size depending on whether this 293997d8d152SAndre Oppermann * is a local network or not. 294097d8d152SAndre Oppermann * 294197d8d152SAndre Oppermann * Extend this so we cache the cwnd too and retrieve it here. 294297d8d152SAndre Oppermann * Make cwnd even bigger than RFC3390 suggests but only if we 294397d8d152SAndre Oppermann * have previous experience with the remote host. Be careful 294497d8d152SAndre Oppermann * not make cwnd bigger than remote receive window or our own 294597d8d152SAndre Oppermann * send socket buffer. Maybe put some additional upper bound 294697d8d152SAndre Oppermann * on the retrieved cwnd. Should do incremental updates to 294797d8d152SAndre Oppermann * hostcache when cwnd collapses so next connection doesn't 294897d8d152SAndre Oppermann * overloads the path again. 294997d8d152SAndre Oppermann * 295097d8d152SAndre Oppermann * RFC3390 says only do this if SYN or SYN/ACK didn't got lost. 295197d8d152SAndre Oppermann * We currently check only in syncache_socket for that. 295297d8d152SAndre Oppermann */ 295397d8d152SAndre Oppermann #define TCP_METRICS_CWND 295497d8d152SAndre Oppermann #ifdef TCP_METRICS_CWND 295597d8d152SAndre Oppermann if (metrics.rmx_cwnd) 295697d8d152SAndre Oppermann tp->snd_cwnd = max(mss, 295797d8d152SAndre Oppermann min(metrics.rmx_cwnd / 2, 295897d8d152SAndre Oppermann min(tp->snd_wnd, so->so_snd.sb_hiwat))); 295997d8d152SAndre Oppermann else 296097d8d152SAndre Oppermann #endif 296197d8d152SAndre Oppermann if (tcp_do_rfc3390) 296297d8d152SAndre Oppermann tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380)); 296397d8d152SAndre Oppermann #ifdef INET6 296497d8d152SAndre Oppermann else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) || 296597d8d152SAndre Oppermann (!isipv6 && in_localaddr(inp->inp_faddr))) 2966943ae302SAndre Oppermann #else 2967943ae302SAndre Oppermann else if (in_localaddr(inp->inp_faddr)) 296897d8d152SAndre Oppermann #endif 2969943ae302SAndre Oppermann tp->snd_cwnd = mss * ss_fltsz_local; 297097d8d152SAndre Oppermann else 297197d8d152SAndre Oppermann tp->snd_cwnd = mss * ss_fltsz; 2972a0292f23SGarrett Wollman } 2973a0292f23SGarrett Wollman 2974a0292f23SGarrett Wollman /* 2975a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 2976a0292f23SGarrett Wollman */ 2977a0292f23SGarrett Wollman int 297897d8d152SAndre Oppermann tcp_mssopt(inc) 297997d8d152SAndre Oppermann struct in_conninfo *inc; 2980a0292f23SGarrett Wollman { 298197d8d152SAndre Oppermann int mss = 0; 298297d8d152SAndre Oppermann u_long maxmtu = 0; 298397d8d152SAndre Oppermann u_long thcmtu = 0; 298497d8d152SAndre Oppermann size_t min_protoh; 2985fb59c426SYoshinobu Inoue #ifdef INET6 298697d8d152SAndre Oppermann int isipv6 = inc->inc_isipv6 ? 1 : 0; 2987fb59c426SYoshinobu Inoue #endif 2988a0292f23SGarrett Wollman 298997d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 2990a0292f23SGarrett Wollman 299131b3783cSHajimu UMEMOTO #ifdef INET6 299297d8d152SAndre Oppermann if (isipv6) { 299397d8d152SAndre Oppermann mss = tcp_v6mssdflt; 299497d8d152SAndre Oppermann maxmtu = tcp_maxmtu6(inc); 299597d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 299697d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 299797d8d152SAndre Oppermann } else 299831b3783cSHajimu UMEMOTO #endif 299997d8d152SAndre Oppermann { 300097d8d152SAndre Oppermann mss = tcp_mssdflt; 300197d8d152SAndre Oppermann maxmtu = tcp_maxmtu(inc); 300297d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 300397d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 300497d8d152SAndre Oppermann } 300597d8d152SAndre Oppermann if (maxmtu && thcmtu) 300697d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 300797d8d152SAndre Oppermann else if (maxmtu || thcmtu) 300897d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 300997d8d152SAndre Oppermann 301097d8d152SAndre Oppermann return (mss); 3011df8bae1dSRodney W. Grimes } 301246f58482SJonathan Lemon 301346f58482SJonathan Lemon 301446f58482SJonathan Lemon /* 3015c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3016c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3017c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3018c068736aSJeffrey Hsu * be started again. 301946f58482SJonathan Lemon */ 3020c068736aSJeffrey Hsu static void 3021c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th) 302246f58482SJonathan Lemon struct tcpcb *tp; 302346f58482SJonathan Lemon struct tcphdr *th; 302446f58482SJonathan Lemon { 302546f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 302646f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 302746f58482SJonathan Lemon 302846f58482SJonathan Lemon callout_stop(tp->tt_rexmt); 302946f58482SJonathan Lemon tp->t_rtttime = 0; 303046f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 30316b2a5f92SJayanth Vijayaraghavan /* 3032c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3033c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 30346b2a5f92SJayanth Vijayaraghavan */ 30356b2a5f92SJayanth Vijayaraghavan tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 3036a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 30376b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 303846f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 303946f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 304046f58482SJonathan Lemon tp->snd_nxt = onxt; 304146f58482SJonathan Lemon /* 304246f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 304346f58482SJonathan Lemon * not updated yet. 304446f58482SJonathan Lemon */ 304546f58482SJonathan Lemon tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg); 304646f58482SJonathan Lemon } 3047340c35deSJonathan Lemon 3048340c35deSJonathan Lemon /* 3049340c35deSJonathan Lemon * Returns 1 if the TIME_WAIT state was killed and we should start over, 3050340c35deSJonathan Lemon * looking for a pcb in the listen state. Returns 0 otherwise. 3051340c35deSJonathan Lemon */ 3052340c35deSJonathan Lemon static int 3053340c35deSJonathan Lemon tcp_timewait(tw, to, th, m, tlen) 3054340c35deSJonathan Lemon struct tcptw *tw; 3055340c35deSJonathan Lemon struct tcpopt *to; 3056340c35deSJonathan Lemon struct tcphdr *th; 3057340c35deSJonathan Lemon struct mbuf *m; 3058340c35deSJonathan Lemon int tlen; 3059340c35deSJonathan Lemon { 3060340c35deSJonathan Lemon int thflags; 3061340c35deSJonathan Lemon tcp_seq seq; 3062340c35deSJonathan Lemon #ifdef INET6 3063340c35deSJonathan Lemon int isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 3064340c35deSJonathan Lemon #else 3065340c35deSJonathan Lemon const int isipv6 = 0; 3066340c35deSJonathan Lemon #endif 3067340c35deSJonathan Lemon 3068340c35deSJonathan Lemon thflags = th->th_flags; 3069340c35deSJonathan Lemon 3070340c35deSJonathan Lemon /* 3071340c35deSJonathan Lemon * NOTE: for FIN_WAIT_2 (to be added later), 3072340c35deSJonathan Lemon * must validate sequence number before accepting RST 3073340c35deSJonathan Lemon */ 3074340c35deSJonathan Lemon 3075340c35deSJonathan Lemon /* 3076340c35deSJonathan Lemon * If the segment contains RST: 3077340c35deSJonathan Lemon * Drop the segment - see Stevens, vol. 2, p. 964 and 3078340c35deSJonathan Lemon * RFC 1337. 3079340c35deSJonathan Lemon */ 3080340c35deSJonathan Lemon if (thflags & TH_RST) 3081340c35deSJonathan Lemon goto drop; 3082340c35deSJonathan Lemon 3083340c35deSJonathan Lemon #if 0 3084340c35deSJonathan Lemon /* PAWS not needed at the moment */ 3085340c35deSJonathan Lemon /* 3086340c35deSJonathan Lemon * RFC 1323 PAWS: If we have a timestamp reply on this segment 3087340c35deSJonathan Lemon * and it's less than ts_recent, drop it. 3088340c35deSJonathan Lemon */ 3089340c35deSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 3090340c35deSJonathan Lemon TSTMP_LT(to.to_tsval, tp->ts_recent)) { 3091340c35deSJonathan Lemon if ((thflags & TH_ACK) == 0) 3092340c35deSJonathan Lemon goto drop; 3093340c35deSJonathan Lemon goto ack; 3094340c35deSJonathan Lemon } 3095340c35deSJonathan Lemon /* 3096340c35deSJonathan Lemon * ts_recent is never updated because we never accept new segments. 3097340c35deSJonathan Lemon */ 3098340c35deSJonathan Lemon #endif 3099340c35deSJonathan Lemon 3100340c35deSJonathan Lemon /* 3101340c35deSJonathan Lemon * If a new connection request is received 3102340c35deSJonathan Lemon * while in TIME_WAIT, drop the old connection 3103340c35deSJonathan Lemon * and start over if the sequence numbers 3104340c35deSJonathan Lemon * are above the previous ones. 3105340c35deSJonathan Lemon */ 3106340c35deSJonathan Lemon if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) { 3107607b0b0cSJonathan Lemon (void) tcp_twclose(tw, 0); 3108340c35deSJonathan Lemon return (1); 3109340c35deSJonathan Lemon } 3110340c35deSJonathan Lemon 3111340c35deSJonathan Lemon /* 3112340c35deSJonathan Lemon * Drop the the segment if it does not contain an ACK. 3113340c35deSJonathan Lemon */ 3114340c35deSJonathan Lemon if ((thflags & TH_ACK) == 0) 3115340c35deSJonathan Lemon goto drop; 3116340c35deSJonathan Lemon 3117340c35deSJonathan Lemon /* 3118340c35deSJonathan Lemon * Reset the 2MSL timer if this is a duplicate FIN. 3119340c35deSJonathan Lemon */ 3120340c35deSJonathan Lemon if (thflags & TH_FIN) { 3121340c35deSJonathan Lemon seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0); 3122340c35deSJonathan Lemon if (seq + 1 == tw->rcv_nxt) 3123607b0b0cSJonathan Lemon tcp_timer_2msl_reset(tw, 2 * tcp_msl); 3124340c35deSJonathan Lemon } 3125340c35deSJonathan Lemon 3126340c35deSJonathan Lemon /* 3127272c5dfeSJonathan Lemon * Acknowledge the segment if it has data or is not a duplicate ACK. 3128340c35deSJonathan Lemon */ 3129272c5dfeSJonathan Lemon if (thflags != TH_ACK || tlen != 0 || 3130272c5dfeSJonathan Lemon th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt) 3131a7b6a14aSRobert Watson tcp_twrespond(tw, TH_ACK); 3132340c35deSJonathan Lemon goto drop; 3133340c35deSJonathan Lemon 3134340c35deSJonathan Lemon /* 3135340c35deSJonathan Lemon * Generate a RST, dropping incoming segment. 3136340c35deSJonathan Lemon * Make ACK acceptable to originator of segment. 3137340c35deSJonathan Lemon * Don't bother to respond if destination was broadcast/multicast. 3138340c35deSJonathan Lemon */ 3139340c35deSJonathan Lemon if (m->m_flags & (M_BCAST|M_MCAST)) 3140340c35deSJonathan Lemon goto drop; 3141340c35deSJonathan Lemon if (isipv6) { 3142340c35deSJonathan Lemon struct ip6_hdr *ip6; 3143340c35deSJonathan Lemon 3144340c35deSJonathan Lemon /* IPv6 anycast check is done at tcp6_input() */ 3145340c35deSJonathan Lemon ip6 = mtod(m, struct ip6_hdr *); 3146340c35deSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3147340c35deSJonathan Lemon IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3148340c35deSJonathan Lemon goto drop; 3149340c35deSJonathan Lemon } else { 3150340c35deSJonathan Lemon struct ip *ip; 3151340c35deSJonathan Lemon 3152340c35deSJonathan Lemon ip = mtod(m, struct ip *); 3153340c35deSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3154340c35deSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3155340c35deSJonathan Lemon ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3156340c35deSJonathan Lemon in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3157340c35deSJonathan Lemon goto drop; 3158340c35deSJonathan Lemon } 3159340c35deSJonathan Lemon if (thflags & TH_ACK) { 3160340c35deSJonathan Lemon tcp_respond(NULL, 3161340c35deSJonathan Lemon mtod(m, void *), th, m, 0, th->th_ack, TH_RST); 3162340c35deSJonathan Lemon } else { 3163340c35deSJonathan Lemon seq = th->th_seq + (thflags & TH_SYN ? 1 : 0); 3164340c35deSJonathan Lemon tcp_respond(NULL, 3165340c35deSJonathan Lemon mtod(m, void *), th, m, seq, 0, TH_RST|TH_ACK); 3166340c35deSJonathan Lemon } 3167340c35deSJonathan Lemon INP_UNLOCK(tw->tw_inpcb); 3168340c35deSJonathan Lemon return (0); 3169340c35deSJonathan Lemon 3170340c35deSJonathan Lemon drop: 3171340c35deSJonathan Lemon INP_UNLOCK(tw->tw_inpcb); 3172340c35deSJonathan Lemon m_freem(m); 3173340c35deSJonathan Lemon return (0); 3174340c35deSJonathan Lemon } 3175