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 * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33e79adb8eSGarrett Wollman * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 34c3aac50fSPeter Wemm * $FreeBSD$ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37f9e354dfSJulian Elischer #include "opt_ipfw.h" /* for ipfw_fwd */ 38fb59c426SYoshinobu Inoue #include "opt_inet6.h" 393a2a9f79SYoshinobu Inoue #include "opt_ipsec.h" 400cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 41e46cd3d4SDag-Erling Smørgrav #include "opt_tcp_input.h" 420cc12cc5SJoerg Wunsch 43df8bae1dSRodney W. Grimes #include <sys/param.h> 44df8bae1dSRodney W. Grimes #include <sys/systm.h> 4598163b98SPoul-Henning Kamp #include <sys/kernel.h> 4698163b98SPoul-Henning Kamp #include <sys/sysctl.h> 47df8bae1dSRodney W. Grimes #include <sys/malloc.h> 48df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 49a29f300eSGarrett Wollman #include <sys/proc.h> /* for proc0 declaration */ 50df8bae1dSRodney W. Grimes #include <sys/protosw.h> 51df8bae1dSRodney W. Grimes #include <sys/socket.h> 52df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 53816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 54df8bae1dSRodney W. Grimes 55e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 56e79adb8eSGarrett Wollman 57df8bae1dSRodney W. Grimes #include <net/if.h> 58df8bae1dSRodney W. Grimes #include <net/route.h> 59df8bae1dSRodney W. Grimes 60df8bae1dSRodney W. Grimes #include <netinet/in.h> 61df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 62df8bae1dSRodney W. Grimes #include <netinet/ip.h> 6351508de1SMatthew Dillon #include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */ 64fb59c426SYoshinobu Inoue #ifdef INET6 65fb59c426SYoshinobu Inoue #include <netinet/ip6.h> 66fb59c426SYoshinobu Inoue #include <netinet/in_var.h> 67fb59c426SYoshinobu Inoue #include <netinet6/nd6.h> 68fb59c426SYoshinobu Inoue #include <netinet/icmp6.h> 69fb59c426SYoshinobu Inoue #endif 70df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 71fb59c426SYoshinobu Inoue #ifdef INET6 72fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h> 73fb59c426SYoshinobu Inoue #endif 74df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 75fb59c426SYoshinobu Inoue #ifdef INET6 76fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 77fb59c426SYoshinobu Inoue #endif 7851508de1SMatthew Dillon #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 79df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 80df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 81df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 82df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 83df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 84fb59c426SYoshinobu Inoue #ifdef INET6 85fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 86fb59c426SYoshinobu Inoue #endif 87df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 88610ee2f9SDavid Greenman #ifdef TCPDEBUG 89df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 90fb59c426SYoshinobu Inoue 91fb59c426SYoshinobu Inoue u_char tcp_saveipgen[40]; /* the size must be of max ip header, now IPv6 */ 92fb59c426SYoshinobu Inoue struct tcphdr tcp_savetcp; 93fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */ 94fb59c426SYoshinobu Inoue 95fb59c426SYoshinobu Inoue #ifdef IPSEC 96fb59c426SYoshinobu Inoue #include <netinet6/ipsec.h> 973a2a9f79SYoshinobu Inoue #ifdef INET6 983a2a9f79SYoshinobu Inoue #include <netinet6/ipsec6.h> 993a2a9f79SYoshinobu Inoue #endif 100fb59c426SYoshinobu Inoue #include <netkey/key.h> 101fb59c426SYoshinobu Inoue #endif /*IPSEC*/ 102fb59c426SYoshinobu Inoue 103db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 104db4f9cc7SJonathan Lemon 105fb59c426SYoshinobu Inoue MALLOC_DEFINE(M_TSEGQ, "tseg_qent", "TCP segment queue entry"); 106df8bae1dSRodney W. Grimes 1070312fbe9SPoul-Henning Kamp static int tcprexmtthresh = 3; 1082f96f1f4SGarrett Wollman tcp_seq tcp_iss; 1092f96f1f4SGarrett Wollman tcp_cc tcp_ccgen; 1100312fbe9SPoul-Henning Kamp 1112f96f1f4SGarrett Wollman struct tcpstat tcpstat; 1123d177f46SBill Fumerola SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RD, 1133d177f46SBill Fumerola &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 1140312fbe9SPoul-Henning Kamp 115d78a37adSPaul Traina static int log_in_vain = 0; 116816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 1173d177f46SBill Fumerola &log_in_vain, 0, "Log all incoming TCP connections"); 118816a3d83SPoul-Henning Kamp 11916f7f31fSGeoff Rehmet static int blackhole = 0; 12016f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 12116f7f31fSGeoff Rehmet &blackhole, 0, "Do not send RST when dropping refused connections"); 12216f7f31fSGeoff Rehmet 123f498eeeeSDavid Greenman int tcp_delack_enabled = 1; 12484e33c9eSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 1253d177f46SBill Fumerola &tcp_delack_enabled, 0, 1263d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 127f498eeeeSDavid Greenman 128f8613305SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN 129f8613305SDag-Erling Smørgrav static int drop_synfin = 0; 130f8613305SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, 131f8613305SDag-Erling Smørgrav &drop_synfin, 0, "Drop TCP packets with SYN+FIN set"); 132f8613305SDag-Erling Smørgrav #endif 133f8613305SDag-Erling Smørgrav 134e46cd3d4SDag-Erling Smørgrav #ifdef TCP_RESTRICT_RST 135e46cd3d4SDag-Erling Smørgrav static int restrict_rst = 0; 136e46cd3d4SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, restrict_rst, CTLFLAG_RW, 137e46cd3d4SDag-Erling Smørgrav &restrict_rst, 0, "Restrict RST emission"); 138e46cd3d4SDag-Erling Smørgrav #endif 139e46cd3d4SDag-Erling Smørgrav 14015bd2b43SDavid Greenman struct inpcbhead tcb; 141fb59c426SYoshinobu Inoue #define tcb6 tcb /* for KAME src sync over BSD*'s */ 14215bd2b43SDavid Greenman struct inpcbinfo tcbinfo; 143df8bae1dSRodney W. Grimes 1440312fbe9SPoul-Henning Kamp static void tcp_dooptions __P((struct tcpcb *, 145fb59c426SYoshinobu Inoue u_char *, int, struct tcphdr *, struct tcpopt *)); 1460312fbe9SPoul-Henning Kamp static void tcp_pulloutofband __P((struct socket *, 147fb59c426SYoshinobu Inoue struct tcphdr *, struct mbuf *, int)); 148fb59c426SYoshinobu Inoue static int tcp_reass __P((struct tcpcb *, struct tcphdr *, int *, 149fb59c426SYoshinobu Inoue struct mbuf *)); 1500312fbe9SPoul-Henning Kamp static void tcp_xmit_timer __P((struct tcpcb *, int)); 15146f58482SJonathan Lemon static int tcp_newreno __P((struct tcpcb *, struct tcphdr *)); 1520312fbe9SPoul-Henning Kamp 153fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 154fb59c426SYoshinobu Inoue #ifdef INET6 155fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \ 156fb59c426SYoshinobu Inoue do { \ 157fb59c426SYoshinobu Inoue if ((tp) && (tp)->t_inpcb && \ 158fb59c426SYoshinobu Inoue ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0 && \ 159fb59c426SYoshinobu Inoue (tp)->t_inpcb->in6p_route.ro_rt) \ 160fb59c426SYoshinobu Inoue nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL); \ 161fb59c426SYoshinobu Inoue } while (0) 162fb59c426SYoshinobu Inoue #else 163fb59c426SYoshinobu Inoue #define ND6_HINT(tp) 164fb59c426SYoshinobu Inoue #endif 165df8bae1dSRodney W. Grimes 166df8bae1dSRodney W. Grimes /* 167fb59c426SYoshinobu Inoue * Insert segment which inludes th into reassembly queue of tcp with 168df8bae1dSRodney W. Grimes * control block tp. Return TH_FIN if reassembly now includes 169df8bae1dSRodney W. Grimes * a segment with FIN. The macro form does the common case inline 170df8bae1dSRodney W. Grimes * (segment is the next to be received on an established connection, 171df8bae1dSRodney W. Grimes * and the queue is empty), avoiding linkage into and removal 172df8bae1dSRodney W. Grimes * from the queue and repetition of various conversions. 173df8bae1dSRodney W. Grimes * Set DELACK for segments received in order, but ack immediately 174df8bae1dSRodney W. Grimes * when segments are out of order (so fast retransmit can work). 175df8bae1dSRodney W. Grimes */ 176fb59c426SYoshinobu Inoue #define TCP_REASS(tp, th, tlenp, m, so, flags) { \ 177fb59c426SYoshinobu Inoue if ((th)->th_seq == (tp)->rcv_nxt && \ 178fb59c426SYoshinobu Inoue LIST_EMPTY(&(tp)->t_segq) && \ 1796b067b07SDavid Greenman (tp)->t_state == TCPS_ESTABLISHED) { \ 180f498eeeeSDavid Greenman if (tcp_delack_enabled) \ 1819b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, \ 1829b8b58e0SJonathan Lemon tcp_timer_delack, tp); \ 183f498eeeeSDavid Greenman else \ 184f498eeeeSDavid Greenman tp->t_flags |= TF_ACKNOW; \ 185fb59c426SYoshinobu Inoue (tp)->rcv_nxt += *(tlenp); \ 186fb59c426SYoshinobu Inoue flags = (th)->th_flags & TH_FIN; \ 1876b067b07SDavid Greenman tcpstat.tcps_rcvpack++;\ 188fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyte += *(tlenp);\ 189fb59c426SYoshinobu Inoue ND6_HINT(tp); \ 1906b067b07SDavid Greenman sbappend(&(so)->so_rcv, (m)); \ 1916b067b07SDavid Greenman sorwakeup(so); \ 1926b067b07SDavid Greenman } else { \ 193fb59c426SYoshinobu Inoue (flags) = tcp_reass((tp), (th), (tlenp), (m)); \ 1946b067b07SDavid Greenman tp->t_flags |= TF_ACKNOW; \ 1956b067b07SDavid Greenman } \ 1966b067b07SDavid Greenman } 197df8bae1dSRodney W. Grimes 1980312fbe9SPoul-Henning Kamp static int 199fb59c426SYoshinobu Inoue tcp_reass(tp, th, tlenp, m) 200df8bae1dSRodney W. Grimes register struct tcpcb *tp; 201fb59c426SYoshinobu Inoue register struct tcphdr *th; 202fb59c426SYoshinobu Inoue int *tlenp; 203df8bae1dSRodney W. Grimes struct mbuf *m; 204df8bae1dSRodney W. Grimes { 205fb59c426SYoshinobu Inoue struct tseg_qent *q; 206fb59c426SYoshinobu Inoue struct tseg_qent *p = NULL; 207fb59c426SYoshinobu Inoue struct tseg_qent *nq; 208fb59c426SYoshinobu Inoue struct tseg_qent *te; 209df8bae1dSRodney W. Grimes struct socket *so = tp->t_inpcb->inp_socket; 210df8bae1dSRodney W. Grimes int flags; 211df8bae1dSRodney W. Grimes 212df8bae1dSRodney W. Grimes /* 213fb59c426SYoshinobu Inoue * Call with th==0 after become established to 214df8bae1dSRodney W. Grimes * force pre-ESTABLISHED data up to user socket. 215df8bae1dSRodney W. Grimes */ 216fb59c426SYoshinobu Inoue if (th == 0) 217df8bae1dSRodney W. Grimes goto present; 218df8bae1dSRodney W. Grimes 219fb59c426SYoshinobu Inoue /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */ 220fb59c426SYoshinobu Inoue MALLOC(te, struct tseg_qent *, sizeof (struct tseg_qent), M_TSEGQ, 221fb59c426SYoshinobu Inoue M_NOWAIT); 222fb59c426SYoshinobu Inoue if (te == NULL) { 223fb59c426SYoshinobu Inoue tcpstat.tcps_rcvmemdrop++; 224fb59c426SYoshinobu Inoue m_freem(m); 225fb59c426SYoshinobu Inoue return (0); 226fb59c426SYoshinobu Inoue } 2276effc713SDoug Rabson 228df8bae1dSRodney W. Grimes /* 229df8bae1dSRodney W. Grimes * Find a segment which begins after this one does. 230df8bae1dSRodney W. Grimes */ 231fb59c426SYoshinobu Inoue LIST_FOREACH(q, &tp->t_segq, tqe_q) { 232fb59c426SYoshinobu Inoue if (SEQ_GT(q->tqe_th->th_seq, th->th_seq)) 233df8bae1dSRodney W. Grimes break; 234fb59c426SYoshinobu Inoue p = q; 235fb59c426SYoshinobu Inoue } 236df8bae1dSRodney W. Grimes 237df8bae1dSRodney W. Grimes /* 238df8bae1dSRodney W. Grimes * If there is a preceding segment, it may provide some of 239df8bae1dSRodney W. Grimes * our data already. If so, drop the data from the incoming 240df8bae1dSRodney W. Grimes * segment. If it provides all of our data, drop us. 241df8bae1dSRodney W. Grimes */ 2426effc713SDoug Rabson if (p != NULL) { 243df8bae1dSRodney W. Grimes register int i; 244df8bae1dSRodney W. Grimes /* conversion to int (in i) handles seq wraparound */ 245fb59c426SYoshinobu Inoue i = p->tqe_th->th_seq + p->tqe_len - th->th_seq; 246df8bae1dSRodney W. Grimes if (i > 0) { 247fb59c426SYoshinobu Inoue if (i >= *tlenp) { 248df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 249fb59c426SYoshinobu Inoue tcpstat.tcps_rcvdupbyte += *tlenp; 250df8bae1dSRodney W. Grimes m_freem(m); 251fb59c426SYoshinobu Inoue FREE(te, M_TSEGQ); 252a0292f23SGarrett Wollman /* 253a0292f23SGarrett Wollman * Try to present any queued data 254a0292f23SGarrett Wollman * at the left window edge to the user. 255a0292f23SGarrett Wollman * This is needed after the 3-WHS 256a0292f23SGarrett Wollman * completes. 257a0292f23SGarrett Wollman */ 258a0292f23SGarrett Wollman goto present; /* ??? */ 259df8bae1dSRodney W. Grimes } 260df8bae1dSRodney W. Grimes m_adj(m, i); 261fb59c426SYoshinobu Inoue *tlenp -= i; 262fb59c426SYoshinobu Inoue th->th_seq += i; 263df8bae1dSRodney W. Grimes } 264df8bae1dSRodney W. Grimes } 265df8bae1dSRodney W. Grimes tcpstat.tcps_rcvoopack++; 266fb59c426SYoshinobu Inoue tcpstat.tcps_rcvoobyte += *tlenp; 267df8bae1dSRodney W. Grimes 268df8bae1dSRodney W. Grimes /* 269df8bae1dSRodney W. Grimes * While we overlap succeeding segments trim them or, 270df8bae1dSRodney W. Grimes * if they are completely covered, dequeue them. 271df8bae1dSRodney W. Grimes */ 2726effc713SDoug Rabson while (q) { 273fb59c426SYoshinobu Inoue register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq; 274df8bae1dSRodney W. Grimes if (i <= 0) 275df8bae1dSRodney W. Grimes break; 276fb59c426SYoshinobu Inoue if (i < q->tqe_len) { 277fb59c426SYoshinobu Inoue q->tqe_th->th_seq += i; 278fb59c426SYoshinobu Inoue q->tqe_len -= i; 279fb59c426SYoshinobu Inoue m_adj(q->tqe_m, i); 280df8bae1dSRodney W. Grimes break; 281df8bae1dSRodney W. Grimes } 2826effc713SDoug Rabson 283fb59c426SYoshinobu Inoue nq = LIST_NEXT(q, tqe_q); 284fb59c426SYoshinobu Inoue LIST_REMOVE(q, tqe_q); 285fb59c426SYoshinobu Inoue m_freem(q->tqe_m); 286fb59c426SYoshinobu Inoue FREE(q, M_TSEGQ); 2876effc713SDoug Rabson q = nq; 288df8bae1dSRodney W. Grimes } 289df8bae1dSRodney W. Grimes 290fb59c426SYoshinobu Inoue /* Insert the new segment queue entry into place. */ 291fb59c426SYoshinobu Inoue te->tqe_m = m; 292fb59c426SYoshinobu Inoue te->tqe_th = th; 293fb59c426SYoshinobu Inoue te->tqe_len = *tlenp; 294fb59c426SYoshinobu Inoue 2956effc713SDoug Rabson if (p == NULL) { 296fb59c426SYoshinobu Inoue LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q); 2976effc713SDoug Rabson } else { 298fb59c426SYoshinobu Inoue LIST_INSERT_AFTER(p, te, tqe_q); 2996effc713SDoug Rabson } 300df8bae1dSRodney W. Grimes 301df8bae1dSRodney W. Grimes present: 302df8bae1dSRodney W. Grimes /* 303df8bae1dSRodney W. Grimes * Present data to user, advancing rcv_nxt through 304df8bae1dSRodney W. Grimes * completed sequence space. 305df8bae1dSRodney W. Grimes */ 306a0292f23SGarrett Wollman if (!TCPS_HAVEESTABLISHED(tp->t_state)) 307df8bae1dSRodney W. Grimes return (0); 308fb59c426SYoshinobu Inoue q = LIST_FIRST(&tp->t_segq); 309fb59c426SYoshinobu Inoue if (!q || q->tqe_th->th_seq != tp->rcv_nxt) 310df8bae1dSRodney W. Grimes return (0); 311df8bae1dSRodney W. Grimes do { 312fb59c426SYoshinobu Inoue tp->rcv_nxt += q->tqe_len; 313fb59c426SYoshinobu Inoue flags = q->tqe_th->th_flags & TH_FIN; 314fb59c426SYoshinobu Inoue nq = LIST_NEXT(q, tqe_q); 315fb59c426SYoshinobu Inoue LIST_REMOVE(q, tqe_q); 316df8bae1dSRodney W. Grimes if (so->so_state & SS_CANTRCVMORE) 317fb59c426SYoshinobu Inoue m_freem(q->tqe_m); 318df8bae1dSRodney W. Grimes else 319fb59c426SYoshinobu Inoue sbappend(&so->so_rcv, q->tqe_m); 320fb59c426SYoshinobu Inoue FREE(q, M_TSEGQ); 3216effc713SDoug Rabson q = nq; 322fb59c426SYoshinobu Inoue } while (q && q->tqe_th->th_seq == tp->rcv_nxt); 323fb59c426SYoshinobu Inoue ND6_HINT(tp); 324df8bae1dSRodney W. Grimes sorwakeup(so); 325df8bae1dSRodney W. Grimes return (flags); 326df8bae1dSRodney W. Grimes } 327df8bae1dSRodney W. Grimes 328df8bae1dSRodney W. Grimes /* 329df8bae1dSRodney W. Grimes * TCP input routine, follows pages 65-76 of the 330df8bae1dSRodney W. Grimes * protocol specification dated September, 1981 very closely. 331df8bae1dSRodney W. Grimes */ 332fb59c426SYoshinobu Inoue #ifdef INET6 333fb59c426SYoshinobu Inoue int 334fb59c426SYoshinobu Inoue tcp6_input(mp, offp, proto) 335fb59c426SYoshinobu Inoue struct mbuf **mp; 336fb59c426SYoshinobu Inoue int *offp, proto; 337fb59c426SYoshinobu Inoue { 338fb59c426SYoshinobu Inoue register struct mbuf *m = *mp; 339fb59c426SYoshinobu Inoue 340fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 341fb59c426SYoshinobu Inoue 342fb59c426SYoshinobu Inoue /* 343fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 344fb59c426SYoshinobu Inoue * better place to put this in? 345fb59c426SYoshinobu Inoue */ 346fb59c426SYoshinobu Inoue if (m->m_flags & M_ANYCAST6) { 347fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 348fb59c426SYoshinobu Inoue 349fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 350fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 351fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 352fb59c426SYoshinobu Inoue return IPPROTO_DONE; 353fb59c426SYoshinobu Inoue } 354fb59c426SYoshinobu Inoue 355fb59c426SYoshinobu Inoue tcp_input(m, *offp, proto); 356fb59c426SYoshinobu Inoue return IPPROTO_DONE; 357fb59c426SYoshinobu Inoue } 358fb59c426SYoshinobu Inoue #endif 359fb59c426SYoshinobu Inoue 360df8bae1dSRodney W. Grimes void 3616a800098SYoshinobu Inoue tcp_input(m, off0, proto) 362df8bae1dSRodney W. Grimes register struct mbuf *m; 3636a800098SYoshinobu Inoue int off0, proto; 364df8bae1dSRodney W. Grimes { 365fb59c426SYoshinobu Inoue register struct tcphdr *th; 366fb59c426SYoshinobu Inoue register struct ip *ip = NULL; 367fb59c426SYoshinobu Inoue register struct ipovly *ipov; 368df8bae1dSRodney W. Grimes register struct inpcb *inp; 369e79adb8eSGarrett Wollman u_char *optp = NULL; 37026f9a767SRodney W. Grimes int optlen = 0; 371df8bae1dSRodney W. Grimes int len, tlen, off; 372fb59c426SYoshinobu Inoue int drop_hdrlen; 373df8bae1dSRodney W. Grimes register struct tcpcb *tp = 0; 374fb59c426SYoshinobu Inoue register int thflags; 37526f9a767SRodney W. Grimes struct socket *so = 0; 376df8bae1dSRodney W. Grimes int todrop, acked, ourfinisacked, needoutput = 0; 377df8bae1dSRodney W. Grimes struct in_addr laddr; 378fb59c426SYoshinobu Inoue #ifdef INET6 379fb59c426SYoshinobu Inoue struct in6_addr laddr6; 380fb59c426SYoshinobu Inoue #endif 381df8bae1dSRodney W. Grimes int dropsocket = 0; 382df8bae1dSRodney W. Grimes int iss = 0; 383a0292f23SGarrett Wollman u_long tiwin; 384a0292f23SGarrett Wollman struct tcpopt to; /* options in this segment */ 385a0292f23SGarrett Wollman struct rmxp_tao *taop; /* pointer to our TAO cache entry */ 386a0292f23SGarrett Wollman struct rmxp_tao tao_noncached; /* in case there's no cached entry */ 387610ee2f9SDavid Greenman #ifdef TCPDEBUG 388610ee2f9SDavid Greenman short ostate = 0; 389610ee2f9SDavid Greenman #endif 390fb59c426SYoshinobu Inoue #ifdef INET6 391fb59c426SYoshinobu Inoue struct ip6_hdr *ip6 = NULL; 392fb59c426SYoshinobu Inoue int isipv6; 393fb59c426SYoshinobu Inoue #endif /* INET6 */ 394df8bae1dSRodney W. Grimes 395fb59c426SYoshinobu Inoue #ifdef INET6 396fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 397fb59c426SYoshinobu Inoue #endif 398a0292f23SGarrett Wollman bzero((char *)&to, sizeof(to)); 399a0292f23SGarrett Wollman 400df8bae1dSRodney W. Grimes tcpstat.tcps_rcvtotal++; 401fb59c426SYoshinobu Inoue 402fb59c426SYoshinobu Inoue #ifdef INET6 403fb59c426SYoshinobu Inoue if (isipv6) { 404fb59c426SYoshinobu Inoue /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */ 405fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 406fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 407fb59c426SYoshinobu Inoue if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 408fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbadsum++; 409fb59c426SYoshinobu Inoue goto drop; 410fb59c426SYoshinobu Inoue } 411fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 412fb59c426SYoshinobu Inoue } else 413fb59c426SYoshinobu Inoue #endif /* INET6 */ 414fb59c426SYoshinobu Inoue { 415df8bae1dSRodney W. Grimes /* 416df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 417df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 418df8bae1dSRodney W. Grimes */ 419fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 420df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 421fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 422fb59c426SYoshinobu Inoue } 423df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 424df8bae1dSRodney W. Grimes if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 425df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 426df8bae1dSRodney W. Grimes return; 427df8bae1dSRodney W. Grimes } 428df8bae1dSRodney W. Grimes } 429fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 430fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 431db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 432db4f9cc7SJonathan Lemon tlen = ip->ip_len; 433df8bae1dSRodney W. Grimes 434db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 435db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 436db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 437db4f9cc7SJonathan Lemon else 438db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 439db4f9cc7SJonathan Lemon ip->ip_dst.s_addr, htonl(m->m_pkthdr.csum_data + 440db4f9cc7SJonathan Lemon ip->ip_len + IPPROTO_TCP)); 441db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 442db4f9cc7SJonathan Lemon } else { 443df8bae1dSRodney W. Grimes /* 444df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 445df8bae1dSRodney W. Grimes */ 446df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 447fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 448fb59c426SYoshinobu Inoue ipov->ih_len = (u_short)tlen; 449fb59c426SYoshinobu Inoue HTONS(ipov->ih_len); 450fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 451db4f9cc7SJonathan Lemon } 452fb59c426SYoshinobu Inoue if (th->th_sum) { 453df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadsum++; 454df8bae1dSRodney W. Grimes goto drop; 455df8bae1dSRodney W. Grimes } 456fb59c426SYoshinobu Inoue #ifdef INET6 457fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 458fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 459fb59c426SYoshinobu Inoue #endif 460fb59c426SYoshinobu Inoue } 461df8bae1dSRodney W. Grimes 462df8bae1dSRodney W. Grimes /* 463df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 464df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 465df8bae1dSRodney W. Grimes */ 466fb59c426SYoshinobu Inoue off = th->th_off << 2; 467df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 468df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadoff++; 469df8bae1dSRodney W. Grimes goto drop; 470df8bae1dSRodney W. Grimes } 471fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 472df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 473fb59c426SYoshinobu Inoue #ifdef INET6 474fb59c426SYoshinobu Inoue if (isipv6) { 475fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 476fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 477fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 478fb59c426SYoshinobu Inoue } else 479fb59c426SYoshinobu Inoue #endif /* INET6 */ 480fb59c426SYoshinobu Inoue { 481df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 482df8bae1dSRodney W. Grimes if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 483df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 484df8bae1dSRodney W. Grimes return; 485df8bae1dSRodney W. Grimes } 486fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 487fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 488fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 489fb59c426SYoshinobu Inoue } 490df8bae1dSRodney W. Grimes } 491df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 492fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 493df8bae1dSRodney W. Grimes } 494fb59c426SYoshinobu Inoue thflags = th->th_flags; 495df8bae1dSRodney W. Grimes 496e46cd3d4SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN 497e46cd3d4SDag-Erling Smørgrav /* 498e46cd3d4SDag-Erling Smørgrav * If the drop_synfin option is enabled, drop all packets with 499e46cd3d4SDag-Erling Smørgrav * both the SYN and FIN bits set. This prevents e.g. nmap from 500e46cd3d4SDag-Erling Smørgrav * identifying the TCP/IP stack. 501e46cd3d4SDag-Erling Smørgrav * 502e46cd3d4SDag-Erling Smørgrav * This is incompatible with RFC1644 extensions (T/TCP). 503e46cd3d4SDag-Erling Smørgrav */ 504fb59c426SYoshinobu Inoue if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN)) 505e46cd3d4SDag-Erling Smørgrav goto drop; 506e46cd3d4SDag-Erling Smørgrav #endif 507e46cd3d4SDag-Erling Smørgrav 508df8bae1dSRodney W. Grimes /* 509df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 510df8bae1dSRodney W. Grimes */ 511fb59c426SYoshinobu Inoue NTOHL(th->th_seq); 512fb59c426SYoshinobu Inoue NTOHL(th->th_ack); 513fb59c426SYoshinobu Inoue NTOHS(th->th_win); 514fb59c426SYoshinobu Inoue NTOHS(th->th_urp); 515df8bae1dSRodney W. Grimes 516df8bae1dSRodney W. Grimes /* 517fb59c426SYoshinobu Inoue * Delay droping TCP, IP headers, IPv6 ext headers, and TCP options, 518fb59c426SYoshinobu Inoue * until after ip6_savecontrol() is called and before other functions 519fb59c426SYoshinobu Inoue * which don't want those proto headers. 520fb59c426SYoshinobu Inoue * Because ip6_savecontrol() is going to parse the mbuf to 521fb59c426SYoshinobu Inoue * search for data to be passed up to user-land, it wants mbuf 522fb59c426SYoshinobu Inoue * parameters to be unchanged. 523755c1f07SAndras Olah */ 524fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 525755c1f07SAndras Olah 526755c1f07SAndras Olah /* 527df8bae1dSRodney W. Grimes * Locate pcb for segment. 528df8bae1dSRodney W. Grimes */ 529df8bae1dSRodney W. Grimes findpcb: 530f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD 531fb59c426SYoshinobu Inoue if (ip_fw_fwd_addr != NULL 532fb59c426SYoshinobu Inoue #ifdef INET6 533fb59c426SYoshinobu Inoue && isipv6 == NULL /* IPv6 support is not yet */ 534fb59c426SYoshinobu Inoue #endif /* INET6 */ 535fb59c426SYoshinobu Inoue ) { 536f9e354dfSJulian Elischer /* 537f9e354dfSJulian Elischer * Diverted. Pretend to be the destination. 538f9e354dfSJulian Elischer * already got one like this? 539f9e354dfSJulian Elischer */ 540fb59c426SYoshinobu Inoue inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, 541fb59c426SYoshinobu Inoue ip->ip_dst, th->th_dport, 0, m->m_pkthdr.rcvif); 542f9e354dfSJulian Elischer if (!inp) { 543f9e354dfSJulian Elischer /* 544f9e354dfSJulian Elischer * No, then it's new. Try find the ambushing socket 545f9e354dfSJulian Elischer */ 546f9e354dfSJulian Elischer if (!ip_fw_fwd_addr->sin_port) { 547fb59c426SYoshinobu Inoue inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, 548fb59c426SYoshinobu Inoue th->th_sport, ip_fw_fwd_addr->sin_addr, 549fb59c426SYoshinobu Inoue th->th_dport, 1, m->m_pkthdr.rcvif); 550f9e354dfSJulian Elischer } else { 551f9e354dfSJulian Elischer inp = in_pcblookup_hash(&tcbinfo, 552fb59c426SYoshinobu Inoue ip->ip_src, th->th_sport, 553f9e354dfSJulian Elischer ip_fw_fwd_addr->sin_addr, 554cfa1ca9dSYoshinobu Inoue ntohs(ip_fw_fwd_addr->sin_port), 1, 555cfa1ca9dSYoshinobu Inoue m->m_pkthdr.rcvif); 556f9e354dfSJulian Elischer } 557f9e354dfSJulian Elischer } 558f9e354dfSJulian Elischer ip_fw_fwd_addr = NULL; 559f9e354dfSJulian Elischer } else 560f9e354dfSJulian Elischer #endif /* IPFIREWALL_FORWARD */ 561fb59c426SYoshinobu Inoue { 562fb59c426SYoshinobu Inoue #ifdef INET6 563fb59c426SYoshinobu Inoue if (isipv6) 564fb59c426SYoshinobu Inoue inp = in6_pcblookup_hash(&tcbinfo, &ip6->ip6_src, th->th_sport, 565fb59c426SYoshinobu Inoue &ip6->ip6_dst, th->th_dport, 1, 566fb59c426SYoshinobu Inoue m->m_pkthdr.rcvif); 567fb59c426SYoshinobu Inoue else 568fb59c426SYoshinobu Inoue #endif /* INET6 */ 569fb59c426SYoshinobu Inoue inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, 570fb59c426SYoshinobu Inoue ip->ip_dst, th->th_dport, 1, m->m_pkthdr.rcvif); 571fb59c426SYoshinobu Inoue } 572f9e354dfSJulian Elischer 573fb59c426SYoshinobu Inoue #ifdef IPSEC 574fb59c426SYoshinobu Inoue #ifdef INET6 575fb59c426SYoshinobu Inoue if (isipv6) { 576fb59c426SYoshinobu Inoue if (inp != NULL && ipsec6_in_reject_so(m, inp->inp_socket)) { 577fb59c426SYoshinobu Inoue ipsec6stat.in_polvio++; 578fb59c426SYoshinobu Inoue goto drop; 579fb59c426SYoshinobu Inoue } 580fb59c426SYoshinobu Inoue } else 581fb59c426SYoshinobu Inoue #endif /* INET6 */ 582fb59c426SYoshinobu Inoue if (inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) { 583fb59c426SYoshinobu Inoue ipsecstat.in_polvio++; 584fb59c426SYoshinobu Inoue goto drop; 585fb59c426SYoshinobu Inoue } 586fb59c426SYoshinobu Inoue #endif /*IPSEC*/ 587df8bae1dSRodney W. Grimes 588df8bae1dSRodney W. Grimes /* 589df8bae1dSRodney W. Grimes * If the state is CLOSED (i.e., TCB does not exist) then 590df8bae1dSRodney W. Grimes * all data in the incoming segment is discarded. 591df8bae1dSRodney W. Grimes * If the TCB exists but is in CLOSED state, it is embryonic, 592df8bae1dSRodney W. Grimes * but should either do a listen or a connect soon. 593df8bae1dSRodney W. Grimes */ 594816a3d83SPoul-Henning Kamp if (inp == NULL) { 5952e4e1b4cSGeoff Rehmet if (log_in_vain) { 596fb59c426SYoshinobu Inoue #ifdef INET6 597fb59c426SYoshinobu Inoue char dbuf[INET6_ADDRSTRLEN], sbuf[INET6_ADDRSTRLEN]; 598fb59c426SYoshinobu Inoue #else /* INET6 */ 599fb59c426SYoshinobu Inoue char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"]; 600fb59c426SYoshinobu Inoue #endif /* INET6 */ 60175cfc95fSAndrey A. Chernov 602fb59c426SYoshinobu Inoue #ifdef INET6 603fb59c426SYoshinobu Inoue if (isipv6) { 604fb59c426SYoshinobu Inoue strcpy(dbuf, ip6_sprintf(&ip6->ip6_dst)); 605fb59c426SYoshinobu Inoue strcpy(sbuf, ip6_sprintf(&ip6->ip6_src)); 606fb59c426SYoshinobu Inoue } else 607fb59c426SYoshinobu Inoue #endif 608fb59c426SYoshinobu Inoue { 609fb59c426SYoshinobu Inoue strcpy(dbuf, inet_ntoa(ip->ip_dst)); 610fb59c426SYoshinobu Inoue strcpy(sbuf, inet_ntoa(ip->ip_src)); 611fb59c426SYoshinobu Inoue } 6122e4e1b4cSGeoff Rehmet switch (log_in_vain) { 6132e4e1b4cSGeoff Rehmet case 1: 614fb59c426SYoshinobu Inoue if(thflags & TH_SYN) 615592071e8SBruce Evans log(LOG_INFO, 616592071e8SBruce Evans "Connection attempt to TCP %s:%d from %s:%d\n", 617fb59c426SYoshinobu Inoue dbuf, ntohs(th->th_dport), 618fb59c426SYoshinobu Inoue sbuf, 619fb59c426SYoshinobu Inoue ntohs(th->th_sport)); 6202e4e1b4cSGeoff Rehmet break; 6212e4e1b4cSGeoff Rehmet case 2: 6222e4e1b4cSGeoff Rehmet log(LOG_INFO, 6232e4e1b4cSGeoff Rehmet "Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n", 624fb59c426SYoshinobu Inoue dbuf, ntohs(th->th_dport), sbuf, 625fb59c426SYoshinobu Inoue ntohs(th->th_sport), thflags); 6262e4e1b4cSGeoff Rehmet break; 6272e4e1b4cSGeoff Rehmet default: 6282e4e1b4cSGeoff Rehmet break; 6292e4e1b4cSGeoff Rehmet } 63075cfc95fSAndrey A. Chernov } 6312e4e1b4cSGeoff Rehmet if (blackhole) { 6322e4e1b4cSGeoff Rehmet switch (blackhole) { 633828b7f40SGeoff Rehmet case 1: 634fb59c426SYoshinobu Inoue if (thflags & TH_SYN) 635828b7f40SGeoff Rehmet goto drop; 636828b7f40SGeoff Rehmet break; 637828b7f40SGeoff Rehmet case 2: 638828b7f40SGeoff Rehmet goto drop; 639828b7f40SGeoff Rehmet default: 640828b7f40SGeoff Rehmet goto drop; 6412e4e1b4cSGeoff Rehmet } 642828b7f40SGeoff Rehmet } 643173c0f9fSWarner Losh goto maybedropwithreset; 644816a3d83SPoul-Henning Kamp } 645df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 646df8bae1dSRodney W. Grimes if (tp == 0) 647173c0f9fSWarner Losh goto maybedropwithreset; 648df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_CLOSED) 649df8bae1dSRodney W. Grimes goto drop; 650df8bae1dSRodney W. Grimes 651df8bae1dSRodney W. Grimes /* Unscale the window into a 32-bit value. */ 652fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) == 0) 653fb59c426SYoshinobu Inoue tiwin = th->th_win << tp->snd_scale; 654df8bae1dSRodney W. Grimes else 655fb59c426SYoshinobu Inoue tiwin = th->th_win; 656fb59c426SYoshinobu Inoue 657fb59c426SYoshinobu Inoue #ifdef INET6 658fb59c426SYoshinobu Inoue /* save packet options if user wanted */ 6594739b807SYoshinobu Inoue if (isipv6 && inp->in6p_flags & INP_CONTROLOPTS) { 660fb59c426SYoshinobu Inoue if (inp->in6p_options) { 661fb59c426SYoshinobu Inoue m_freem(inp->in6p_options); 662fb59c426SYoshinobu Inoue inp->in6p_options = 0; 663fb59c426SYoshinobu Inoue } 664fb59c426SYoshinobu Inoue ip6_savecontrol(inp, &inp->in6p_options, ip6, m); 665fb59c426SYoshinobu Inoue } 6668972cdb1SYoshinobu Inoue /* else, should also do ip_srcroute() here? */ 667fb59c426SYoshinobu Inoue #endif /* INET6 */ 668df8bae1dSRodney W. Grimes 669df8bae1dSRodney W. Grimes so = inp->inp_socket; 670df8bae1dSRodney W. Grimes if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) { 671610ee2f9SDavid Greenman #ifdef TCPDEBUG 672df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 673df8bae1dSRodney W. Grimes ostate = tp->t_state; 674fb59c426SYoshinobu Inoue #ifdef INET6 675fb59c426SYoshinobu Inoue if (isipv6) 676fb59c426SYoshinobu Inoue bcopy((char *)ip6, (char *)tcp_saveipgen, 677fb59c426SYoshinobu Inoue sizeof(*ip6)); 678fb59c426SYoshinobu Inoue else 679fb59c426SYoshinobu Inoue #endif /* INET6 */ 680fb59c426SYoshinobu Inoue bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 681fb59c426SYoshinobu Inoue tcp_savetcp = *th; 682df8bae1dSRodney W. Grimes } 683610ee2f9SDavid Greenman #endif 684df8bae1dSRodney W. Grimes if (so->so_options & SO_ACCEPTCONN) { 685a0292f23SGarrett Wollman register struct tcpcb *tp0 = tp; 6864195b4afSPaul Traina struct socket *so2; 687fb59c426SYoshinobu Inoue #ifdef IPSEC 688fb59c426SYoshinobu Inoue struct socket *oso; 689fb59c426SYoshinobu Inoue #endif 690fb59c426SYoshinobu Inoue #ifdef INET6 691fb59c426SYoshinobu Inoue struct inpcb *oinp = sotoinpcb(so); 692fb59c426SYoshinobu Inoue #endif /* INET6 */ 693fb59c426SYoshinobu Inoue 694fb59c426SYoshinobu Inoue #ifndef IPSEC 695fb59c426SYoshinobu Inoue /* 696fb59c426SYoshinobu Inoue * Current IPsec implementation makes incorrect IPsec 697fb59c426SYoshinobu Inoue * cache if this check is done here. 698fb59c426SYoshinobu Inoue * So delay this until duplicated socket is created. 699fb59c426SYoshinobu Inoue */ 700fb59c426SYoshinobu Inoue if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 7014195b4afSPaul Traina /* 702ebb0cbeaSPaul Traina * Note: dropwithreset makes sure we don't 703ebb0cbeaSPaul Traina * send a RST in response to a RST. 7044195b4afSPaul Traina */ 705fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 706ebb0cbeaSPaul Traina tcpstat.tcps_badsyn++; 707173c0f9fSWarner Losh goto maybedropwithreset; 7084195b4afSPaul Traina } 709ebb0cbeaSPaul Traina goto drop; 710ebb0cbeaSPaul Traina } 711fb59c426SYoshinobu Inoue #endif 712ebb0cbeaSPaul Traina so2 = sonewconn(so, 0); 713ebb0cbeaSPaul Traina if (so2 == 0) { 714ebb0cbeaSPaul Traina tcpstat.tcps_listendrop++; 715ebb0cbeaSPaul Traina so2 = sodropablereq(so); 716a51764a8SPaul Traina if (so2) { 717ebb0cbeaSPaul Traina tcp_drop(sototcpcb(so2), ETIMEDOUT); 718a51764a8SPaul Traina so2 = sonewconn(so, 0); 719a51764a8SPaul Traina } 720a51764a8SPaul Traina if (!so2) 721df8bae1dSRodney W. Grimes goto drop; 7221347f5b8SGuido van Rooij } 723fb59c426SYoshinobu Inoue #ifdef IPSEC 724fb59c426SYoshinobu Inoue oso = so; 725fb59c426SYoshinobu Inoue #endif 7264195b4afSPaul Traina so = so2; 727df8bae1dSRodney W. Grimes /* 728df8bae1dSRodney W. Grimes * This is ugly, but .... 729df8bae1dSRodney W. Grimes * 730df8bae1dSRodney W. Grimes * Mark socket as temporary until we're 731df8bae1dSRodney W. Grimes * committed to keeping it. The code at 732df8bae1dSRodney W. Grimes * ``drop'' and ``dropwithreset'' check the 733df8bae1dSRodney W. Grimes * flag dropsocket to see if the temporary 734df8bae1dSRodney W. Grimes * socket created here should be discarded. 735df8bae1dSRodney W. Grimes * We mark the socket as discardable until 736df8bae1dSRodney W. Grimes * we're committed to it below in TCPS_LISTEN. 737df8bae1dSRodney W. Grimes */ 738df8bae1dSRodney W. Grimes dropsocket++; 739df8bae1dSRodney W. Grimes inp = (struct inpcb *)so->so_pcb; 740fb59c426SYoshinobu Inoue #ifdef INET6 741fb59c426SYoshinobu Inoue if (isipv6) 742fb59c426SYoshinobu Inoue inp->in6p_laddr = ip6->ip6_dst; 743fb59c426SYoshinobu Inoue else { 744fdaf052eSYoshinobu Inoue if ((inp->inp_flags & IN6P_BINDV6ONLY) == 0) { 745fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 746fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 747fb59c426SYoshinobu Inoue } 748fb59c426SYoshinobu Inoue #endif /* INET6 */ 749fb59c426SYoshinobu Inoue inp->inp_laddr = ip->ip_dst; 750fb59c426SYoshinobu Inoue #ifdef INET6 751fb59c426SYoshinobu Inoue } 752fb59c426SYoshinobu Inoue #endif /* INET6 */ 753fb59c426SYoshinobu Inoue inp->inp_lport = th->th_dport; 754c3229e05SDavid Greenman if (in_pcbinshash(inp) != 0) { 755c3229e05SDavid Greenman /* 7565a8c77a8SDavid E. O'Brien * Undo the assignments above if we failed to 7575a8c77a8SDavid E. O'Brien * put the PCB on the hash lists. 758c3229e05SDavid Greenman */ 759fb59c426SYoshinobu Inoue #ifdef INET6 760fb59c426SYoshinobu Inoue if (isipv6) 761fb59c426SYoshinobu Inoue inp->in6p_laddr = in6addr_any; 762fb59c426SYoshinobu Inoue else 763fb59c426SYoshinobu Inoue #endif /* INET6 */ 764c3229e05SDavid Greenman inp->inp_laddr.s_addr = INADDR_ANY; 765c3229e05SDavid Greenman inp->inp_lport = 0; 766c3229e05SDavid Greenman goto drop; 767c3229e05SDavid Greenman } 768fb59c426SYoshinobu Inoue #ifdef IPSEC 769fb59c426SYoshinobu Inoue /* 770fb59c426SYoshinobu Inoue * To avoid creating incorrectly cached IPsec 771fb59c426SYoshinobu Inoue * association, this is need to be done here. 772fb59c426SYoshinobu Inoue * 773fb59c426SYoshinobu Inoue * Subject: (KAME-snap 748) 774fb59c426SYoshinobu Inoue * From: Wayne Knowles <w.knowles@niwa.cri.nz> 775fb59c426SYoshinobu Inoue * ftp://ftp.kame.net/pub/mail-list/snap-users/748 776fb59c426SYoshinobu Inoue */ 777fb59c426SYoshinobu Inoue if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 778fb59c426SYoshinobu Inoue /* 779fb59c426SYoshinobu Inoue * Note: dropwithreset makes sure we don't 780fb59c426SYoshinobu Inoue * send a RST in response to a RST. 781fb59c426SYoshinobu Inoue */ 782fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 783fb59c426SYoshinobu Inoue tcpstat.tcps_badsyn++; 784173c0f9fSWarner Losh goto maybedropwithreset; 785fb59c426SYoshinobu Inoue } 786fb59c426SYoshinobu Inoue goto drop; 787fb59c426SYoshinobu Inoue } 788fb59c426SYoshinobu Inoue #endif 789fb59c426SYoshinobu Inoue #ifdef INET6 790fb59c426SYoshinobu Inoue if (isipv6) { 791fb59c426SYoshinobu Inoue /* 792fb59c426SYoshinobu Inoue * inherit socket options from the listening 793fb59c426SYoshinobu Inoue * socket. 794fb59c426SYoshinobu Inoue */ 795fb59c426SYoshinobu Inoue inp->inp_flags |= 796fb59c426SYoshinobu Inoue oinp->inp_flags & INP_CONTROLOPTS; 797fb59c426SYoshinobu Inoue if (inp->inp_flags & INP_CONTROLOPTS) { 798fb59c426SYoshinobu Inoue if (inp->in6p_options) { 799fb59c426SYoshinobu Inoue m_freem(inp->in6p_options); 800fb59c426SYoshinobu Inoue inp->in6p_options = 0; 801fb59c426SYoshinobu Inoue } 802fb59c426SYoshinobu Inoue ip6_savecontrol(inp, 803fb59c426SYoshinobu Inoue &inp->in6p_options, 804fb59c426SYoshinobu Inoue ip6, m); 805fb59c426SYoshinobu Inoue } 806fb59c426SYoshinobu Inoue } else 807fb59c426SYoshinobu Inoue #endif /* INET6 */ 808df8bae1dSRodney W. Grimes inp->inp_options = ip_srcroute(); 809fb59c426SYoshinobu Inoue #ifdef IPSEC 810fb59c426SYoshinobu Inoue /* copy old policy into new socket's */ 811fb59c426SYoshinobu Inoue if (ipsec_copy_policy(sotoinpcb(oso)->inp_sp, 812fb59c426SYoshinobu Inoue inp->inp_sp)) 813fb59c426SYoshinobu Inoue printf("tcp_input: could not copy policy\n"); 814fb59c426SYoshinobu Inoue #endif 815df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 816df8bae1dSRodney W. Grimes tp->t_state = TCPS_LISTEN; 817a0292f23SGarrett Wollman tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT); 818df8bae1dSRodney W. Grimes 819a0292f23SGarrett Wollman /* Compute proper scaling value from buffer space */ 820df8bae1dSRodney W. Grimes while (tp->request_r_scale < TCP_MAX_WINSHIFT && 8215a8c77a8SDavid E. O'Brien TCP_MAXWIN << tp->request_r_scale < 8225a8c77a8SDavid E. O'Brien so->so_rcv.sb_hiwat) 823df8bae1dSRodney W. Grimes tp->request_r_scale++; 824df8bae1dSRodney W. Grimes } 825df8bae1dSRodney W. Grimes } 826df8bae1dSRodney W. Grimes 827df8bae1dSRodney W. Grimes /* 828df8bae1dSRodney W. Grimes * Segment received on connection. 829df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 830df8bae1dSRodney W. Grimes */ 8319b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 8327ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 8339b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp); 834df8bae1dSRodney W. Grimes 835df8bae1dSRodney W. Grimes /* 836df8bae1dSRodney W. Grimes * Process options if not in LISTEN state, 837df8bae1dSRodney W. Grimes * else do it below (after getting remote address). 838df8bae1dSRodney W. Grimes */ 839f9d5a964SDavid Greenman if (tp->t_state != TCPS_LISTEN) 840fb59c426SYoshinobu Inoue tcp_dooptions(tp, optp, optlen, th, &to); 841df8bae1dSRodney W. Grimes 842df8bae1dSRodney W. Grimes /* 843df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 844df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 845df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 846df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 847df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 848df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 849df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 850df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 851df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 852df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 853df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 854df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 855a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 856a0292f23SGarrett Wollman * Since we check for TCPS_ESTABLISHED above, it can only 857a0292f23SGarrett Wollman * be TH_NEEDSYN. 858df8bae1dSRodney W. Grimes */ 859df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 860fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 861a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 862a0292f23SGarrett Wollman ((to.to_flag & TOF_TS) == 0 || 863a0292f23SGarrett Wollman TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 864a0292f23SGarrett Wollman /* 865a0292f23SGarrett Wollman * Using the CC option is compulsory if once started: 866a0292f23SGarrett Wollman * the segment is OK if no T/TCP was negotiated or 867a0292f23SGarrett Wollman * if the segment has a CC option equal to CCrecv 868a0292f23SGarrett Wollman */ 869a0292f23SGarrett Wollman ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) || 870831a80b0SMatthew Dillon ((to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) && 871fb59c426SYoshinobu Inoue th->th_seq == tp->rcv_nxt && 872df8bae1dSRodney W. Grimes tiwin && tiwin == tp->snd_wnd && 873df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) { 874df8bae1dSRodney W. Grimes 875df8bae1dSRodney W. Grimes /* 876df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 877df8bae1dSRodney W. Grimes * record the timestamp. 878a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 879a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 880df8bae1dSRodney W. Grimes */ 881a0292f23SGarrett Wollman if ((to.to_flag & TOF_TS) != 0 && 882fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 8839b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 884a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 885df8bae1dSRodney W. Grimes } 886df8bae1dSRodney W. Grimes 887fb59c426SYoshinobu Inoue if (tlen == 0) { 888fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 889fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 890233e8c18SGarrett Wollman tp->snd_cwnd >= tp->snd_wnd && 891233e8c18SGarrett Wollman tp->t_dupacks < tcprexmtthresh) { 892df8bae1dSRodney W. Grimes /* 893df8bae1dSRodney W. Grimes * this is a pure ack for outstanding data. 894df8bae1dSRodney W. Grimes */ 895df8bae1dSRodney W. Grimes ++tcpstat.tcps_predack; 8969b8b58e0SJonathan Lemon /* 8979b8b58e0SJonathan Lemon * "bad retransmit" recovery 8989b8b58e0SJonathan Lemon */ 8999b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 9009b8b58e0SJonathan Lemon ticks < tp->t_badrxtwin) { 9019b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 9029b8b58e0SJonathan Lemon tp->snd_ssthresh = 9039b8b58e0SJonathan Lemon tp->snd_ssthresh_prev; 9049b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 9059b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; 9069b8b58e0SJonathan Lemon } 907a0292f23SGarrett Wollman if ((to.to_flag & TOF_TS) != 0) 908a0292f23SGarrett Wollman tcp_xmit_timer(tp, 9099b8b58e0SJonathan Lemon ticks - to.to_tsecr + 1); 9109b8b58e0SJonathan Lemon else if (tp->t_rtttime && 911fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->t_rtseq)) 9129b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 913fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 914df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 915df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 916df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 917fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 918df8bae1dSRodney W. Grimes m_freem(m); 919fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 920df8bae1dSRodney W. Grimes 921df8bae1dSRodney W. Grimes /* 922df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 923df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 924df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 925df8bae1dSRodney W. Grimes * If process is waiting for space, 926df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 927df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 928df8bae1dSRodney W. Grimes * decide between more output or persist. 929df8bae1dSRodney W. Grimes */ 930df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 9319b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 9329b8b58e0SJonathan Lemon else if (!callout_active(tp->tt_persist)) 9339b8b58e0SJonathan Lemon callout_reset(tp->tt_rexmt, 9349b8b58e0SJonathan Lemon tp->t_rxtcur, 9359b8b58e0SJonathan Lemon tcp_timer_rexmt, tp); 936df8bae1dSRodney W. Grimes 937df8bae1dSRodney W. Grimes sowwakeup(so); 938df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 939df8bae1dSRodney W. Grimes (void) tcp_output(tp); 940df8bae1dSRodney W. Grimes return; 941df8bae1dSRodney W. Grimes } 942fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 943fb59c426SYoshinobu Inoue LIST_EMPTY(&tp->t_segq) && 944fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 945df8bae1dSRodney W. Grimes /* 946df8bae1dSRodney W. Grimes * this is a pure, in-sequence data packet 947df8bae1dSRodney W. Grimes * with nothing on the reassembly queue and 948df8bae1dSRodney W. Grimes * we have enough buffer space to take it. 949df8bae1dSRodney W. Grimes */ 950df8bae1dSRodney W. Grimes ++tcpstat.tcps_preddat; 951fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 952df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpack++; 953fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyte += tlen; 954fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 955df8bae1dSRodney W. Grimes /* 956755c1f07SAndras Olah * Add data to socket buffer. 957df8bae1dSRodney W. Grimes */ 958fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 959df8bae1dSRodney W. Grimes sbappend(&so->so_rcv, m); 960df8bae1dSRodney W. Grimes sorwakeup(so); 961f498eeeeSDavid Greenman if (tcp_delack_enabled) { 9629b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 9639b8b58e0SJonathan Lemon tcp_timer_delack, tp); 964f498eeeeSDavid Greenman } else { 965e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 966e612a582SDavid Greenman tcp_output(tp); 967e612a582SDavid Greenman } 968df8bae1dSRodney W. Grimes return; 969df8bae1dSRodney W. Grimes } 970df8bae1dSRodney W. Grimes } 971df8bae1dSRodney W. Grimes 972df8bae1dSRodney W. Grimes /* 973df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 974df8bae1dSRodney W. Grimes * and then do TCP input processing. 975df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 976df8bae1dSRodney W. Grimes * but not less than advertised window. 977df8bae1dSRodney W. Grimes */ 978df8bae1dSRodney W. Grimes { int win; 979df8bae1dSRodney W. Grimes 980df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 981df8bae1dSRodney W. Grimes if (win < 0) 982df8bae1dSRodney W. Grimes win = 0; 98366e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 984df8bae1dSRodney W. Grimes } 985df8bae1dSRodney W. Grimes 986df8bae1dSRodney W. Grimes switch (tp->t_state) { 987df8bae1dSRodney W. Grimes 988df8bae1dSRodney W. Grimes /* 989df8bae1dSRodney W. Grimes * If the state is LISTEN then ignore segment if it contains an RST. 990df8bae1dSRodney W. Grimes * If the segment contains an ACK then it is bad and send a RST. 991df8bae1dSRodney W. Grimes * If it does not contain a SYN then it is not interesting; drop it. 992764d8cefSBill Fenner * If it is from this socket, drop it, it must be forged. 993df8bae1dSRodney W. Grimes * Don't bother responding if the destination was a broadcast. 994df8bae1dSRodney W. Grimes * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 995df8bae1dSRodney W. Grimes * tp->iss, and send a segment: 996df8bae1dSRodney W. Grimes * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 997df8bae1dSRodney W. Grimes * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 998df8bae1dSRodney W. Grimes * Fill in remote peer address fields if not previously specified. 999df8bae1dSRodney W. Grimes * Enter SYN_RECEIVED state, and process any other fields of this 1000df8bae1dSRodney W. Grimes * segment in this state. 1001df8bae1dSRodney W. Grimes */ 1002df8bae1dSRodney W. Grimes case TCPS_LISTEN: { 1003df8bae1dSRodney W. Grimes register struct sockaddr_in *sin; 1004fb59c426SYoshinobu Inoue #ifdef INET6 1005fb59c426SYoshinobu Inoue register struct sockaddr_in6 *sin6; 1006fb59c426SYoshinobu Inoue #endif 1007df8bae1dSRodney W. Grimes 1008fb59c426SYoshinobu Inoue if (thflags & TH_RST) 1009df8bae1dSRodney W. Grimes goto drop; 1010fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1011173c0f9fSWarner Losh goto maybedropwithreset; 1012fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) == 0) 1013df8bae1dSRodney W. Grimes goto drop; 1014fb59c426SYoshinobu Inoue if (th->th_dport == th->th_sport) { 1015fb59c426SYoshinobu Inoue #ifdef INET6 1016fb59c426SYoshinobu Inoue if (isipv6) { 1017fb59c426SYoshinobu Inoue if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 1018fb59c426SYoshinobu Inoue &ip6->ip6_src)) 1019764d8cefSBill Fenner goto drop; 1020fb59c426SYoshinobu Inoue } else 1021fb59c426SYoshinobu Inoue #endif /* INET6 */ 1022fb59c426SYoshinobu Inoue if (ip->ip_dst.s_addr == ip->ip_src.s_addr) 1023fb59c426SYoshinobu Inoue goto drop; 1024fb59c426SYoshinobu Inoue } 1025df8bae1dSRodney W. Grimes /* 1026df8bae1dSRodney W. Grimes * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 1027df8bae1dSRodney W. Grimes * in_broadcast() should never return true on a received 1028df8bae1dSRodney W. Grimes * packet with M_BCAST not set. 1029173c0f9fSWarner Losh * 1030173c0f9fSWarner Losh * Packets with a multicast source address should also 1031173c0f9fSWarner Losh * be discarded. 1032df8bae1dSRodney W. Grimes */ 1033fb59c426SYoshinobu Inoue if (m->m_flags & (M_BCAST|M_MCAST)) 1034df8bae1dSRodney W. Grimes goto drop; 1035fb59c426SYoshinobu Inoue #ifdef INET6 1036fb59c426SYoshinobu Inoue if (isipv6) { 1037173c0f9fSWarner Losh if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1038173c0f9fSWarner Losh IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 1039fb59c426SYoshinobu Inoue goto drop; 1040fb59c426SYoshinobu Inoue } else 1041fb59c426SYoshinobu Inoue #endif 1042173c0f9fSWarner Losh if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1043173c0f9fSWarner Losh IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 1044173c0f9fSWarner Losh ip->ip_src.s_addr == htonl(INADDR_BROADCAST)) 1045fb59c426SYoshinobu Inoue goto drop; 1046fb59c426SYoshinobu Inoue #ifdef INET6 1047fb59c426SYoshinobu Inoue if (isipv6) { 1048fb59c426SYoshinobu Inoue MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, 1049fb59c426SYoshinobu Inoue M_SONAME, M_NOWAIT); 1050fb59c426SYoshinobu Inoue if (sin6 == NULL) 1051fb59c426SYoshinobu Inoue goto drop; 1052fb59c426SYoshinobu Inoue bzero(sin6, sizeof(*sin6)); 1053fb59c426SYoshinobu Inoue sin6->sin6_family = AF_INET6; 1054fb59c426SYoshinobu Inoue sin6->sin6_len = sizeof(*sin6); 1055fb59c426SYoshinobu Inoue sin6->sin6_addr = ip6->ip6_src; 1056fb59c426SYoshinobu Inoue sin6->sin6_port = th->th_sport; 1057fb59c426SYoshinobu Inoue laddr6 = inp->in6p_laddr; 1058fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 1059fb59c426SYoshinobu Inoue inp->in6p_laddr = ip6->ip6_dst; 1060fb59c426SYoshinobu Inoue if (in6_pcbconnect(inp, (struct sockaddr *)sin6, 1061fb59c426SYoshinobu Inoue &proc0)) { 1062fb59c426SYoshinobu Inoue inp->in6p_laddr = laddr6; 1063fb59c426SYoshinobu Inoue FREE(sin6, M_SONAME); 1064fb59c426SYoshinobu Inoue goto drop; 1065fb59c426SYoshinobu Inoue } 1066fb59c426SYoshinobu Inoue FREE(sin6, M_SONAME); 1067fb59c426SYoshinobu Inoue } else 1068fb59c426SYoshinobu Inoue #endif 1069fb59c426SYoshinobu Inoue { 107057bf258eSGarrett Wollman MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, 107157bf258eSGarrett Wollman M_NOWAIT); 107257bf258eSGarrett Wollman if (sin == NULL) 1073df8bae1dSRodney W. Grimes goto drop; 1074df8bae1dSRodney W. Grimes sin->sin_family = AF_INET; 1075df8bae1dSRodney W. Grimes sin->sin_len = sizeof(*sin); 1076fb59c426SYoshinobu Inoue sin->sin_addr = ip->ip_src; 1077fb59c426SYoshinobu Inoue sin->sin_port = th->th_sport; 1078df8bae1dSRodney W. Grimes bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero)); 1079df8bae1dSRodney W. Grimes laddr = inp->inp_laddr; 1080df8bae1dSRodney W. Grimes if (inp->inp_laddr.s_addr == INADDR_ANY) 1081fb59c426SYoshinobu Inoue inp->inp_laddr = ip->ip_dst; 108257bf258eSGarrett Wollman if (in_pcbconnect(inp, (struct sockaddr *)sin, &proc0)) { 1083df8bae1dSRodney W. Grimes inp->inp_laddr = laddr; 108457bf258eSGarrett Wollman FREE(sin, M_SONAME); 1085df8bae1dSRodney W. Grimes goto drop; 1086df8bae1dSRodney W. Grimes } 108757bf258eSGarrett Wollman FREE(sin, M_SONAME); 1088fb59c426SYoshinobu Inoue } 1089df8bae1dSRodney W. Grimes tp->t_template = tcp_template(tp); 1090df8bae1dSRodney W. Grimes if (tp->t_template == 0) { 1091df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ENOBUFS); 1092df8bae1dSRodney W. Grimes dropsocket = 0; /* socket is already gone */ 1093df8bae1dSRodney W. Grimes goto drop; 1094df8bae1dSRodney W. Grimes } 1095a0292f23SGarrett Wollman if ((taop = tcp_gettaocache(inp)) == NULL) { 1096a0292f23SGarrett Wollman taop = &tao_noncached; 1097a0292f23SGarrett Wollman bzero(taop, sizeof(*taop)); 1098a0292f23SGarrett Wollman } 1099fb59c426SYoshinobu Inoue tcp_dooptions(tp, optp, optlen, th, &to); 1100df8bae1dSRodney W. Grimes if (iss) 1101df8bae1dSRodney W. Grimes tp->iss = iss; 1102df8bae1dSRodney W. Grimes else 1103df8bae1dSRodney W. Grimes tp->iss = tcp_iss; 1104e79adb8eSGarrett Wollman tcp_iss += TCP_ISSINCR/4; 1105fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1106df8bae1dSRodney W. Grimes tcp_sendseqinit(tp); 1107df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 110846f58482SJonathan Lemon tp->snd_recover = tp->snd_una; 1109a0292f23SGarrett Wollman /* 1110a0292f23SGarrett Wollman * Initialization of the tcpcb for transaction; 1111a0292f23SGarrett Wollman * set SND.WND = SEG.WND, 1112a0292f23SGarrett Wollman * initialize CCsend and CCrecv. 1113a0292f23SGarrett Wollman */ 1114a0292f23SGarrett Wollman tp->snd_wnd = tiwin; /* initial send-window */ 1115a0292f23SGarrett Wollman tp->cc_send = CC_INC(tcp_ccgen); 1116a0292f23SGarrett Wollman tp->cc_recv = to.to_cc; 1117a0292f23SGarrett Wollman /* 1118a0292f23SGarrett Wollman * Perform TAO test on incoming CC (SEG.CC) option, if any. 1119a0292f23SGarrett Wollman * - compare SEG.CC against cached CC from the same host, 1120a0292f23SGarrett Wollman * if any. 1121a0292f23SGarrett Wollman * - if SEG.CC > chached value, SYN must be new and is accepted 1122a0292f23SGarrett Wollman * immediately: save new CC in the cache, mark the socket 1123a0292f23SGarrett Wollman * connected, enter ESTABLISHED state, turn on flag to 1124a0292f23SGarrett Wollman * send a SYN in the next segment. 1125a0292f23SGarrett Wollman * A virtual advertised window is set in rcv_adv to 1126a0292f23SGarrett Wollman * initialize SWS prevention. Then enter normal segment 1127a0292f23SGarrett Wollman * processing: drop SYN, process data and FIN. 1128a0292f23SGarrett Wollman * - otherwise do a normal 3-way handshake. 1129a0292f23SGarrett Wollman */ 1130a0292f23SGarrett Wollman if ((to.to_flag & TOF_CC) != 0) { 1131068373b6SGuido van Rooij if (((tp->t_flags & TF_NOPUSH) != 0) && 113211ad4550SGuido van Rooij taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) { 113311ad4550SGuido van Rooij 1134a0292f23SGarrett Wollman taop->tao_cc = to.to_cc; 11359b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1136a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1137a0292f23SGarrett Wollman 1138a0292f23SGarrett Wollman /* 1139a0292f23SGarrett Wollman * If there is a FIN, or if there is data and the 1140a0292f23SGarrett Wollman * connection is local, then delay SYN,ACK(SYN) in 1141a0292f23SGarrett Wollman * the hope of piggy-backing it on a response 1142a0292f23SGarrett Wollman * segment. Otherwise must send ACK now in case 1143a0292f23SGarrett Wollman * the other side is slow starting. 1144a0292f23SGarrett Wollman */ 1145fb59c426SYoshinobu Inoue if (tcp_delack_enabled && ((thflags & TH_FIN) || 1146fb59c426SYoshinobu Inoue (tlen != 0 && 1147fb59c426SYoshinobu Inoue #ifdef INET6 1148fb59c426SYoshinobu Inoue ((isipv6 && in6_localaddr(&inp->in6p_faddr)) 1149fb59c426SYoshinobu Inoue || 1150fb59c426SYoshinobu Inoue (!isipv6 && 1151fb59c426SYoshinobu Inoue #endif 1152fb59c426SYoshinobu Inoue in_localaddr(inp->inp_faddr) 1153fb59c426SYoshinobu Inoue #ifdef INET6 1154fb59c426SYoshinobu Inoue )) 1155fb59c426SYoshinobu Inoue #endif 1156fb59c426SYoshinobu Inoue ))) { 11579b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 11589b8b58e0SJonathan Lemon tcp_timer_delack, tp); 11599b8b58e0SJonathan Lemon tp->t_flags |= TF_NEEDSYN; 11609b8b58e0SJonathan Lemon } else 1161a0292f23SGarrett Wollman tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 116207e43e10SAndras Olah 116307e43e10SAndras Olah /* 116407e43e10SAndras Olah * Limit the `virtual advertised window' to TCP_MAXWIN 116507e43e10SAndras Olah * here. Even if we requested window scaling, it will 116607e43e10SAndras Olah * become effective only later when our SYN is acked. 116707e43e10SAndras Olah */ 116807e43e10SAndras Olah tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN); 1169a0292f23SGarrett Wollman tcpstat.tcps_connects++; 1170a0292f23SGarrett Wollman soisconnected(so); 11719b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepinit, 11729b8b58e0SJonathan Lemon tcp_timer_keep, tp); 1173a0292f23SGarrett Wollman dropsocket = 0; /* committed to socket */ 1174a0292f23SGarrett Wollman tcpstat.tcps_accepts++; 1175a0292f23SGarrett Wollman goto trimthenstep6; 1176a0292f23SGarrett Wollman } 1177a0292f23SGarrett Wollman /* else do standard 3-way handshake */ 1178a0292f23SGarrett Wollman } else { 1179a0292f23SGarrett Wollman /* 1180a0292f23SGarrett Wollman * No CC option, but maybe CC.NEW: 1181a0292f23SGarrett Wollman * invalidate cached value. 1182a0292f23SGarrett Wollman */ 1183a0292f23SGarrett Wollman taop->tao_cc = 0; 1184a0292f23SGarrett Wollman } 1185a0292f23SGarrett Wollman /* 1186a0292f23SGarrett Wollman * TAO test failed or there was no CC option, 1187a0292f23SGarrett Wollman * do a standard 3-way handshake. 1188a0292f23SGarrett Wollman */ 1189df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1190df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 11919b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 1192df8bae1dSRodney W. Grimes dropsocket = 0; /* committed to socket */ 1193df8bae1dSRodney W. Grimes tcpstat.tcps_accepts++; 1194df8bae1dSRodney W. Grimes goto trimthenstep6; 1195df8bae1dSRodney W. Grimes } 1196df8bae1dSRodney W. Grimes 1197df8bae1dSRodney W. Grimes /* 1198764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1199764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1200764d8cefSBill Fenner */ 1201764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1202fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1203fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1204fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) 1205173c0f9fSWarner Losh goto maybedropwithreset; 1206764d8cefSBill Fenner break; 1207764d8cefSBill Fenner 1208764d8cefSBill Fenner /* 1209df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1210df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1211df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1212df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1213df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1214df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1215df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1216df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1217df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1218df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1219df8bae1dSRodney W. Grimes */ 1220df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1221a0292f23SGarrett Wollman if ((taop = tcp_gettaocache(inp)) == NULL) { 1222a0292f23SGarrett Wollman taop = &tao_noncached; 1223a0292f23SGarrett Wollman bzero(taop, sizeof(*taop)); 1224a0292f23SGarrett Wollman } 1225a0292f23SGarrett Wollman 1226fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1227fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1228fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1229a0292f23SGarrett Wollman /* 1230a0292f23SGarrett Wollman * If we have a cached CCsent for the remote host, 1231a0292f23SGarrett Wollman * hence we haven't just crashed and restarted, 1232a0292f23SGarrett Wollman * do not send a RST. This may be a retransmission 1233a0292f23SGarrett Wollman * from the other side after our earlier ACK was lost. 1234a0292f23SGarrett Wollman * Our new SYN, when it arrives, will serve as the 1235a0292f23SGarrett Wollman * needed ACK. 1236a0292f23SGarrett Wollman */ 1237a0292f23SGarrett Wollman if (taop->tao_ccsent != 0) 1238a0292f23SGarrett Wollman goto drop; 1239a0292f23SGarrett Wollman else 1240a0292f23SGarrett Wollman goto dropwithreset; 1241a0292f23SGarrett Wollman } 1242fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 1243fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1244df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 1245df8bae1dSRodney W. Grimes goto drop; 1246df8bae1dSRodney W. Grimes } 1247fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) == 0) 1248df8bae1dSRodney W. Grimes goto drop; 1249fb59c426SYoshinobu Inoue tp->snd_wnd = th->th_win; /* initial send window */ 1250a0292f23SGarrett Wollman tp->cc_recv = to.to_cc; /* foreign CC */ 1251a0292f23SGarrett Wollman 1252fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1253df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1254fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1255a0292f23SGarrett Wollman /* 1256a0292f23SGarrett Wollman * Our SYN was acked. If segment contains CC.ECHO 1257a0292f23SGarrett Wollman * option, check it to make sure this segment really 1258a0292f23SGarrett Wollman * matches our SYN. If not, just drop it as old 1259a0292f23SGarrett Wollman * duplicate, but send an RST if we're still playing 1260026650e5SBill Fenner * by the old rules. If no CC.ECHO option, make sure 1261026650e5SBill Fenner * we don't get fooled into using T/TCP. 1262a0292f23SGarrett Wollman */ 1263026650e5SBill Fenner if (to.to_flag & TOF_CCECHO) { 1264dfd5dee1SPeter Wemm if (tp->cc_send != to.to_ccecho) { 1265a0292f23SGarrett Wollman if (taop->tao_ccsent != 0) 1266a0292f23SGarrett Wollman goto drop; 1267a0292f23SGarrett Wollman else 1268a0292f23SGarrett Wollman goto dropwithreset; 1269dfd5dee1SPeter Wemm } 1270026650e5SBill Fenner } else 1271026650e5SBill Fenner tp->t_flags &= ~TF_RCVD_CC; 1272845799c1SAndras Olah tcpstat.tcps_connects++; 1273845799c1SAndras Olah soisconnected(so); 1274845799c1SAndras Olah /* Do window scaling on this connection? */ 1275845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1276845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1277845799c1SAndras Olah tp->snd_scale = tp->requested_s_scale; 1278845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1279845799c1SAndras Olah } 1280a0292f23SGarrett Wollman /* Segment is acceptable, update cache if undefined. */ 1281a0292f23SGarrett Wollman if (taop->tao_ccsent == 0) 1282a0292f23SGarrett Wollman taop->tao_ccsent = to.to_ccecho; 1283a0292f23SGarrett Wollman 1284a0292f23SGarrett Wollman tp->rcv_adv += tp->rcv_wnd; 1285a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1286a0292f23SGarrett Wollman /* 1287a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1288a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1289a0292f23SGarrett Wollman */ 1290fb59c426SYoshinobu Inoue if (tcp_delack_enabled && tlen != 0) 12919b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 12929b8b58e0SJonathan Lemon tcp_timer_delack, tp); 1293a0292f23SGarrett Wollman else 1294a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1295a0292f23SGarrett Wollman /* 1296a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1297a0292f23SGarrett Wollman * Transitions: 1298a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1299a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1300a0292f23SGarrett Wollman */ 13019b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1302a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1303a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1304a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1305fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 13067ff19458SPaul Traina } else { 1307a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 13089b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, 13099b8b58e0SJonathan Lemon tcp_timer_keep, tp); 13107ff19458SPaul Traina } 1311a0292f23SGarrett Wollman } else { 1312a0292f23SGarrett Wollman /* 1313a0292f23SGarrett Wollman * Received initial SYN in SYN-SENT[*] state => simul- 1314a0292f23SGarrett Wollman * taneous open. If segment contains CC option and there is 1315a0292f23SGarrett Wollman * a cached CC, apply TAO test; if it succeeds, connection is 1316a0292f23SGarrett Wollman * half-synchronized. Otherwise, do 3-way handshake: 1317a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1318a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1319a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1320a0292f23SGarrett Wollman */ 1321a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 13229b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 1323a0292f23SGarrett Wollman if (to.to_flag & TOF_CC) { 1324a0292f23SGarrett Wollman if (taop->tao_cc != 0 && 1325a0292f23SGarrett Wollman CC_GT(to.to_cc, taop->tao_cc)) { 1326a0292f23SGarrett Wollman /* 1327a0292f23SGarrett Wollman * update cache and make transition: 1328a0292f23SGarrett Wollman * SYN-SENT -> ESTABLISHED* 1329a0292f23SGarrett Wollman * SYN-SENT* -> FIN-WAIT-1* 1330a0292f23SGarrett Wollman */ 1331a0292f23SGarrett Wollman taop->tao_cc = to.to_cc; 13329b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1333a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1334a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1335a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 13367ff19458SPaul Traina } else { 1337a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 13389b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, 13399b8b58e0SJonathan Lemon tcp_keepidle, 13409b8b58e0SJonathan Lemon tcp_timer_keep, 13419b8b58e0SJonathan Lemon tp); 13427ff19458SPaul Traina } 1343a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDSYN; 1344df8bae1dSRodney W. Grimes } else 1345df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1346a0292f23SGarrett Wollman } else { 1347a0292f23SGarrett Wollman /* CC.NEW or no option => invalidate cache */ 1348a0292f23SGarrett Wollman taop->tao_cc = 0; 1349a0292f23SGarrett Wollman tp->t_state = TCPS_SYN_RECEIVED; 1350a0292f23SGarrett Wollman } 1351a0292f23SGarrett Wollman } 1352df8bae1dSRodney W. Grimes 1353df8bae1dSRodney W. Grimes trimthenstep6: 1354df8bae1dSRodney W. Grimes /* 1355fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1356df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1357df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1358df8bae1dSRodney W. Grimes */ 1359fb59c426SYoshinobu Inoue th->th_seq++; 1360fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1361fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1362df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1363fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1364fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1365df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1366df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1367df8bae1dSRodney W. Grimes } 1368fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1369fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 1370a0292f23SGarrett Wollman /* 1371a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1372a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1373a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1374a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1375a0292f23SGarrett Wollman * Otherwise, goto step 6. 1376a0292f23SGarrett Wollman */ 1377fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1378a0292f23SGarrett Wollman goto process_ACK; 1379df8bae1dSRodney W. Grimes goto step6; 1380a0292f23SGarrett Wollman /* 1381a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1382a0292f23SGarrett Wollman * if segment contains a SYN and CC [not CC.NEW] option: 1383a0292f23SGarrett Wollman * if state == TIME_WAIT and connection duration > MSL, 1384a0292f23SGarrett Wollman * drop packet and send RST; 1385a0292f23SGarrett Wollman * 1386a0292f23SGarrett Wollman * if SEG.CC > CCrecv then is new SYN, and can implicitly 1387a0292f23SGarrett Wollman * ack the FIN (and data) in retransmission queue. 1388a0292f23SGarrett Wollman * Complete close and delete TCPCB. Then reprocess 1389a0292f23SGarrett Wollman * segment, hoping to find new TCPCB in LISTEN state; 1390a0292f23SGarrett Wollman * 1391a0292f23SGarrett Wollman * else must be old SYN; drop it. 1392a0292f23SGarrett Wollman * else do normal processing. 1393a0292f23SGarrett Wollman */ 1394a0292f23SGarrett Wollman case TCPS_LAST_ACK: 1395a0292f23SGarrett Wollman case TCPS_CLOSING: 1396a0292f23SGarrett Wollman case TCPS_TIME_WAIT: 1397fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) && 1398a0292f23SGarrett Wollman (to.to_flag & TOF_CC) && tp->cc_recv != 0) { 1399a0292f23SGarrett Wollman if (tp->t_state == TCPS_TIME_WAIT && 14009b8b58e0SJonathan Lemon (ticks - tp->t_starttime) > tcp_msl) 1401a0292f23SGarrett Wollman goto dropwithreset; 1402a0292f23SGarrett Wollman if (CC_GT(to.to_cc, tp->cc_recv)) { 1403a0292f23SGarrett Wollman tp = tcp_close(tp); 1404a0292f23SGarrett Wollman goto findpcb; 1405a0292f23SGarrett Wollman } 1406a0292f23SGarrett Wollman else 1407a0292f23SGarrett Wollman goto drop; 1408a0292f23SGarrett Wollman } 1409a0292f23SGarrett Wollman break; /* continue normal processing */ 1410df8bae1dSRodney W. Grimes } 1411df8bae1dSRodney W. Grimes 1412df8bae1dSRodney W. Grimes /* 1413df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 141480ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 141580ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 141680ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 141780ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 141880ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 141980ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 142080ab7c0eSGarrett Wollman * killing a connection). 142180ab7c0eSGarrett Wollman * Then check timestamp, if present. 1422a0292f23SGarrett Wollman * Then check the connection count, if present. 1423df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 1424df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 1425df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 1426df8bae1dSRodney W. Grimes * 142780ab7c0eSGarrett Wollman * 142880ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 142980ab7c0eSGarrett Wollman * if this is a valid reset segment. 143080ab7c0eSGarrett Wollman * RFC 793 page 37: 143180ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 143280ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 143380ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 143480ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 143580ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 14361a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 14371a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 14381a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 14391a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 144080ab7c0eSGarrett Wollman * If we have multiple segments in flight, the intial reset 144180ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 144280ab7c0eSGarrett Wollman * but they will eventually catch up. 144380ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 144480ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 144580ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 144680ab7c0eSGarrett Wollman * 144780ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 144880ab7c0eSGarrett Wollman * 144980ab7c0eSGarrett Wollman * DISCUSSION 145080ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 145180ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 145280ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 145380ab7c0eSGarrett Wollman * data. 145480ab7c0eSGarrett Wollman * 145580ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 145680ab7c0eSGarrett Wollman * the state: 145780ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 145880ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 145980ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 146080ab7c0eSGarrett Wollman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 146180ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 1462e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 146380ab7c0eSGarrett Wollman * Close the tcb. 1464e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 146580ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 146680ab7c0eSGarrett Wollman * RFC 1337. 146780ab7c0eSGarrett Wollman */ 1468fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 1469fb59c426SYoshinobu Inoue if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 1470fb59c426SYoshinobu Inoue SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 147180ab7c0eSGarrett Wollman switch (tp->t_state) { 147280ab7c0eSGarrett Wollman 147380ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 147480ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 147580ab7c0eSGarrett Wollman goto close; 147680ab7c0eSGarrett Wollman 147780ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 147880ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 147980ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 148080ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 148180ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 148280ab7c0eSGarrett Wollman close: 148380ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 148480ab7c0eSGarrett Wollman tcpstat.tcps_drops++; 148580ab7c0eSGarrett Wollman tp = tcp_close(tp); 148680ab7c0eSGarrett Wollman break; 148780ab7c0eSGarrett Wollman 148880ab7c0eSGarrett Wollman case TCPS_CLOSING: 148980ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 149080ab7c0eSGarrett Wollman tp = tcp_close(tp); 149180ab7c0eSGarrett Wollman break; 149280ab7c0eSGarrett Wollman 149380ab7c0eSGarrett Wollman case TCPS_TIME_WAIT: 149480ab7c0eSGarrett Wollman break; 149580ab7c0eSGarrett Wollman } 149680ab7c0eSGarrett Wollman } 149780ab7c0eSGarrett Wollman goto drop; 149880ab7c0eSGarrett Wollman } 149980ab7c0eSGarrett Wollman 150080ab7c0eSGarrett Wollman /* 1501df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 1502df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 1503df8bae1dSRodney W. Grimes */ 150480ab7c0eSGarrett Wollman if ((to.to_flag & TOF_TS) != 0 && tp->ts_recent && 150580ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1506df8bae1dSRodney W. Grimes 1507df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 15089b8b58e0SJonathan Lemon if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1509df8bae1dSRodney W. Grimes /* 1510df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 1511df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 1512df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 1513df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 1514df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 1515df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 1516df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 1517df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 1518df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 1519df8bae1dSRodney W. Grimes */ 1520df8bae1dSRodney W. Grimes tp->ts_recent = 0; 1521df8bae1dSRodney W. Grimes } else { 1522df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 1523fb59c426SYoshinobu Inoue tcpstat.tcps_rcvdupbyte += tlen; 1524df8bae1dSRodney W. Grimes tcpstat.tcps_pawsdrop++; 1525df8bae1dSRodney W. Grimes goto dropafterack; 1526df8bae1dSRodney W. Grimes } 1527df8bae1dSRodney W. Grimes } 1528df8bae1dSRodney W. Grimes 1529a0292f23SGarrett Wollman /* 1530a0292f23SGarrett Wollman * T/TCP mechanism 1531a0292f23SGarrett Wollman * If T/TCP was negotiated and the segment doesn't have CC, 1532dc733423SDag-Erling Smørgrav * or if its CC is wrong then drop the segment. 1533a0292f23SGarrett Wollman * RST segments do not have to comply with this. 1534a0292f23SGarrett Wollman */ 1535a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) && 153680ab7c0eSGarrett Wollman ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc)) 1537a0292f23SGarrett Wollman goto dropafterack; 1538a0292f23SGarrett Wollman 153980ab7c0eSGarrett Wollman /* 154080ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 154180ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 154280ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 154380ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 154480ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 154580ab7c0eSGarrett Wollman */ 1546fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) 1547173c0f9fSWarner Losh goto maybedropwithreset; 154880ab7c0eSGarrett Wollman 1549fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 1550df8bae1dSRodney W. Grimes if (todrop > 0) { 1551fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1552fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 1553fb59c426SYoshinobu Inoue th->th_seq++; 1554fb59c426SYoshinobu Inoue if (th->th_urp > 1) 1555fb59c426SYoshinobu Inoue th->th_urp--; 1556df8bae1dSRodney W. Grimes else 1557fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1558df8bae1dSRodney W. Grimes todrop--; 1559df8bae1dSRodney W. Grimes } 1560df8bae1dSRodney W. Grimes /* 1561dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 1562df8bae1dSRodney W. Grimes */ 1563fb59c426SYoshinobu Inoue if (todrop > tlen 1564fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1565dac20301SGarrett Wollman /* 1566dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 1567dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 1568dac20301SGarrett Wollman * of sequence; drop it. 1569dac20301SGarrett Wollman */ 1570fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1571dac20301SGarrett Wollman 1572df8bae1dSRodney W. Grimes /* 1573dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 1574dac20301SGarrett Wollman * But keep on processing for RST or ACK. 1575df8bae1dSRodney W. Grimes */ 1576dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1577fb59c426SYoshinobu Inoue todrop = tlen; 1578dac20301SGarrett Wollman tcpstat.tcps_rcvduppack++; 1579dac20301SGarrett Wollman tcpstat.tcps_rcvdupbyte += todrop; 1580df8bae1dSRodney W. Grimes } else { 1581df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartduppack++; 1582df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartdupbyte += todrop; 1583df8bae1dSRodney W. Grimes } 1584fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 1585fb59c426SYoshinobu Inoue th->th_seq += todrop; 1586fb59c426SYoshinobu Inoue tlen -= todrop; 1587fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 1588fb59c426SYoshinobu Inoue th->th_urp -= todrop; 1589df8bae1dSRodney W. Grimes else { 1590fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1591fb59c426SYoshinobu Inoue th->th_urp = 0; 1592df8bae1dSRodney W. Grimes } 1593df8bae1dSRodney W. Grimes } 1594df8bae1dSRodney W. Grimes 1595df8bae1dSRodney W. Grimes /* 1596df8bae1dSRodney W. Grimes * If new data are received on a connection after the 1597df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 1598df8bae1dSRodney W. Grimes */ 1599df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 1600fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1601df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1602df8bae1dSRodney W. Grimes tcpstat.tcps_rcvafterclose++; 1603df8bae1dSRodney W. Grimes goto dropwithreset; 1604df8bae1dSRodney W. Grimes } 1605df8bae1dSRodney W. Grimes 1606df8bae1dSRodney W. Grimes /* 1607df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 1608df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 1609df8bae1dSRodney W. Grimes */ 1610fb59c426SYoshinobu Inoue todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd); 1611df8bae1dSRodney W. Grimes if (todrop > 0) { 1612df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1613fb59c426SYoshinobu Inoue if (todrop >= tlen) { 1614fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyteafterwin += tlen; 1615df8bae1dSRodney W. Grimes /* 1616df8bae1dSRodney W. Grimes * If a new connection request is received 1617df8bae1dSRodney W. Grimes * while in TIME_WAIT, drop the old connection 1618df8bae1dSRodney W. Grimes * and start over if the sequence numbers 1619df8bae1dSRodney W. Grimes * are above the previous ones. 1620df8bae1dSRodney W. Grimes */ 1621fb59c426SYoshinobu Inoue if (thflags & TH_SYN && 1622df8bae1dSRodney W. Grimes tp->t_state == TCPS_TIME_WAIT && 1623fb59c426SYoshinobu Inoue SEQ_GT(th->th_seq, tp->rcv_nxt)) { 162451b7b337SBill Fenner iss = tp->snd_nxt + TCP_ISSINCR; 1625df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1626df8bae1dSRodney W. Grimes goto findpcb; 1627df8bae1dSRodney W. Grimes } 1628df8bae1dSRodney W. Grimes /* 1629df8bae1dSRodney W. Grimes * If window is closed can only take segments at 1630df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 1631df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 1632df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 1633df8bae1dSRodney W. Grimes * and ack. 1634df8bae1dSRodney W. Grimes */ 1635fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1636df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1637df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinprobe++; 1638df8bae1dSRodney W. Grimes } else 1639df8bae1dSRodney W. Grimes goto dropafterack; 1640df8bae1dSRodney W. Grimes } else 1641df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1642df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1643fb59c426SYoshinobu Inoue tlen -= todrop; 1644fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 1645df8bae1dSRodney W. Grimes } 1646df8bae1dSRodney W. Grimes 1647df8bae1dSRodney W. Grimes /* 1648df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1649df8bae1dSRodney W. Grimes * record its timestamp. 1650a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1651a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1652df8bae1dSRodney W. Grimes */ 1653a0292f23SGarrett Wollman if ((to.to_flag & TOF_TS) != 0 && 1654fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 16559b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1656a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1657df8bae1dSRodney W. Grimes } 1658df8bae1dSRodney W. Grimes 1659df8bae1dSRodney W. Grimes /* 1660df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 1661df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 1662df8bae1dSRodney W. Grimes */ 1663fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1664df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 1665df8bae1dSRodney W. Grimes goto dropwithreset; 1666df8bae1dSRodney W. Grimes } 1667df8bae1dSRodney W. Grimes 1668a0292f23SGarrett Wollman /* 1669a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1670a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 1671a0292f23SGarrett Wollman * later processing; else drop segment and return. 1672a0292f23SGarrett Wollman */ 1673fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 1674a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 1675a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 1676a0292f23SGarrett Wollman goto step6; 1677a0292f23SGarrett Wollman else 1678a0292f23SGarrett Wollman goto drop; 1679a0292f23SGarrett Wollman } 1680df8bae1dSRodney W. Grimes 1681df8bae1dSRodney W. Grimes /* 1682df8bae1dSRodney W. Grimes * Ack processing. 1683df8bae1dSRodney W. Grimes */ 1684df8bae1dSRodney W. Grimes switch (tp->t_state) { 1685df8bae1dSRodney W. Grimes 1686df8bae1dSRodney W. Grimes /* 1687764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1688764d8cefSBill Fenner * ESTABLISHED state and continue processing. 1689764d8cefSBill Fenner * The ACK was checked above. 1690df8bae1dSRodney W. Grimes */ 1691df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1692a0292f23SGarrett Wollman 1693df8bae1dSRodney W. Grimes tcpstat.tcps_connects++; 1694df8bae1dSRodney W. Grimes soisconnected(so); 1695df8bae1dSRodney W. Grimes /* Do window scaling? */ 1696df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1697df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1698df8bae1dSRodney W. Grimes tp->snd_scale = tp->requested_s_scale; 1699df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 1700df8bae1dSRodney W. Grimes } 1701a0292f23SGarrett Wollman /* 1702a0292f23SGarrett Wollman * Upon successful completion of 3-way handshake, 1703a0292f23SGarrett Wollman * update cache.CC if it was undefined, pass any queued 1704a0292f23SGarrett Wollman * data to the user, and advance state appropriately. 1705a0292f23SGarrett Wollman */ 1706a0292f23SGarrett Wollman if ((taop = tcp_gettaocache(inp)) != NULL && 1707a0292f23SGarrett Wollman taop->tao_cc == 0) 1708a0292f23SGarrett Wollman taop->tao_cc = tp->cc_recv; 1709a0292f23SGarrett Wollman 1710a0292f23SGarrett Wollman /* 1711a0292f23SGarrett Wollman * Make transitions: 1712a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 1713a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 1714a0292f23SGarrett Wollman */ 17159b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1716a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1717a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1718a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 17197ff19458SPaul Traina } else { 1720a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 17219b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, 17229b8b58e0SJonathan Lemon tcp_timer_keep, tp); 17237ff19458SPaul Traina } 1724a0292f23SGarrett Wollman /* 1725a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 1726a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 1727a0292f23SGarrett Wollman */ 1728fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 1729fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1730a0292f23SGarrett Wollman (struct mbuf *)0); 1731fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1732df8bae1dSRodney W. Grimes /* fall into ... */ 1733df8bae1dSRodney W. Grimes 1734df8bae1dSRodney W. Grimes /* 1735df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1736df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 1737fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 1738fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 1739df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 1740df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 1741df8bae1dSRodney W. Grimes */ 1742df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1743df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1744df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 1745df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1746df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1747df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 1748df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 1749df8bae1dSRodney W. Grimes 1750fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1751fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 1752df8bae1dSRodney W. Grimes tcpstat.tcps_rcvdupack++; 1753df8bae1dSRodney W. Grimes /* 1754df8bae1dSRodney W. Grimes * If we have outstanding data (other than 1755df8bae1dSRodney W. Grimes * a window probe), this is a completely 1756df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 1757df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 1758df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 1759df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 1760df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 1761df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 1762df8bae1dSRodney W. Grimes * window so we send only this one 1763df8bae1dSRodney W. Grimes * packet. 1764df8bae1dSRodney W. Grimes * 1765df8bae1dSRodney W. Grimes * We know we're losing at the current 1766df8bae1dSRodney W. Grimes * window size so do congestion avoidance 1767df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 1768df8bae1dSRodney W. Grimes * and pull our congestion window back to 1769df8bae1dSRodney W. Grimes * the new ssthresh). 1770df8bae1dSRodney W. Grimes * 1771df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 1772df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 1773df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 1774df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 1775df8bae1dSRodney W. Grimes * network. 1776df8bae1dSRodney W. Grimes */ 17779b8b58e0SJonathan Lemon if (!callout_active(tp->tt_rexmt) || 1778fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 1779df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1780df8bae1dSRodney W. Grimes else if (++tp->t_dupacks == tcprexmtthresh) { 1781df8bae1dSRodney W. Grimes tcp_seq onxt = tp->snd_nxt; 1782df8bae1dSRodney W. Grimes u_int win = 1783df8bae1dSRodney W. Grimes min(tp->snd_wnd, tp->snd_cwnd) / 2 / 1784df8bae1dSRodney W. Grimes tp->t_maxseg; 178546f58482SJonathan Lemon if (tcp_do_newreno && SEQ_LT(th->th_ack, 178646f58482SJonathan Lemon tp->snd_recover)) { 178746f58482SJonathan Lemon /* False retransmit, should not 178846f58482SJonathan Lemon * cut window 178946f58482SJonathan Lemon */ 179046f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 179146f58482SJonathan Lemon tp->t_dupacks = 0; 179246f58482SJonathan Lemon (void) tcp_output(tp); 179346f58482SJonathan Lemon goto drop; 179446f58482SJonathan Lemon } 1795df8bae1dSRodney W. Grimes if (win < 2) 1796df8bae1dSRodney W. Grimes win = 2; 1797df8bae1dSRodney W. Grimes tp->snd_ssthresh = win * tp->t_maxseg; 179846f58482SJonathan Lemon tp->snd_recover = tp->snd_max; 17999b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 18009b8b58e0SJonathan Lemon tp->t_rtttime = 0; 1801fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 1802df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 1803df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1804df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 1805df8bae1dSRodney W. Grimes tp->t_maxseg * tp->t_dupacks; 1806df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 1807df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 1808df8bae1dSRodney W. Grimes goto drop; 1809df8bae1dSRodney W. Grimes } else if (tp->t_dupacks > tcprexmtthresh) { 1810df8bae1dSRodney W. Grimes tp->snd_cwnd += tp->t_maxseg; 1811df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1812df8bae1dSRodney W. Grimes goto drop; 1813df8bae1dSRodney W. Grimes } 1814df8bae1dSRodney W. Grimes } else 1815df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1816df8bae1dSRodney W. Grimes break; 1817df8bae1dSRodney W. Grimes } 1818df8bae1dSRodney W. Grimes /* 1819df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 1820df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 1821df8bae1dSRodney W. Grimes */ 182246f58482SJonathan Lemon if (tcp_do_newreno == 0) { 1823233e8c18SGarrett Wollman if (tp->t_dupacks >= tcprexmtthresh && 1824df8bae1dSRodney W. Grimes tp->snd_cwnd > tp->snd_ssthresh) 1825df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh; 1826df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 182746f58482SJonathan Lemon } else if (tp->t_dupacks >= tcprexmtthresh && 182846f58482SJonathan Lemon !tcp_newreno(tp, th)) { 182946f58482SJonathan Lemon /* 183046f58482SJonathan Lemon * Window inflation should have left us with approx. 183146f58482SJonathan Lemon * snd_ssthresh outstanding data. But in case we 183246f58482SJonathan Lemon * would be inclined to send a burst, better to do 183346f58482SJonathan Lemon * it via the slow start mechanism. 183446f58482SJonathan Lemon */ 183546f58482SJonathan Lemon if (SEQ_GT(th->th_ack + tp->snd_ssthresh, tp->snd_max)) 183646f58482SJonathan Lemon tp->snd_cwnd = 183746f58482SJonathan Lemon tp->snd_max - th->th_ack + tp->t_maxseg; 183846f58482SJonathan Lemon else 183946f58482SJonathan Lemon tp->snd_cwnd = tp->snd_ssthresh; 184046f58482SJonathan Lemon tp->t_dupacks = 0; 184146f58482SJonathan Lemon } 1842fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_max)) { 1843df8bae1dSRodney W. Grimes tcpstat.tcps_rcvacktoomuch++; 1844df8bae1dSRodney W. Grimes goto dropafterack; 1845df8bae1dSRodney W. Grimes } 1846a0292f23SGarrett Wollman /* 1847a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 1848a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 1849a0292f23SGarrett Wollman */ 1850a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 1851a0292f23SGarrett Wollman /* 1852a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 1853a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 185407e43e10SAndras Olah * synchronized). Go to non-starred state, 185507e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 185607e43e10SAndras Olah * we can do window scaling. 1857a0292f23SGarrett Wollman */ 1858a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 1859a0292f23SGarrett Wollman tp->snd_una++; 186007e43e10SAndras Olah /* Do window scaling? */ 186107e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 186207e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 186307e43e10SAndras Olah tp->snd_scale = tp->requested_s_scale; 186407e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 186507e43e10SAndras Olah } 1866a0292f23SGarrett Wollman } 1867a0292f23SGarrett Wollman 1868a0292f23SGarrett Wollman process_ACK: 1869fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 1870df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 1871df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 1872df8bae1dSRodney W. Grimes 1873df8bae1dSRodney W. Grimes /* 18749b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 18759b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 18769b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 18779b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 18789b8b58e0SJonathan Lemon * we left off. 18799b8b58e0SJonathan Lemon */ 18809b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 18819b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 18829b8b58e0SJonathan Lemon tp->snd_ssthresh = tp->snd_ssthresh_prev; 18839b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 18849b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; /* XXX probably not required */ 18859b8b58e0SJonathan Lemon } 18869b8b58e0SJonathan Lemon 18879b8b58e0SJonathan Lemon /* 1888df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 1889df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 1890df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 1891df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 1892df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 1893df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 1894df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 1895df8bae1dSRodney W. Grimes */ 1896a0292f23SGarrett Wollman if (to.to_flag & TOF_TS) 18979b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 1898fb59c426SYoshinobu Inoue else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) 18999b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 1900df8bae1dSRodney W. Grimes 1901df8bae1dSRodney W. Grimes /* 1902df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 1903df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 1904df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 1905df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 1906df8bae1dSRodney W. Grimes */ 1907fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 19089b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 1909df8bae1dSRodney W. Grimes needoutput = 1; 19109b8b58e0SJonathan Lemon } else if (!callout_active(tp->tt_persist)) 19119b8b58e0SJonathan Lemon callout_reset(tp->tt_rexmt, tp->t_rxtcur, 19129b8b58e0SJonathan Lemon tcp_timer_rexmt, tp); 1913a0292f23SGarrett Wollman 1914a0292f23SGarrett Wollman /* 1915a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 1916a0292f23SGarrett Wollman * skip rest of ACK processing. 1917a0292f23SGarrett Wollman */ 1918a0292f23SGarrett Wollman if (acked == 0) 1919a0292f23SGarrett Wollman goto step6; 1920a0292f23SGarrett Wollman 1921df8bae1dSRodney W. Grimes /* 1922df8bae1dSRodney W. Grimes * When new data is acked, open the congestion window. 1923df8bae1dSRodney W. Grimes * If the window gives us less than ssthresh packets 1924df8bae1dSRodney W. Grimes * in flight, open exponentially (maxseg per packet). 1925df8bae1dSRodney W. Grimes * Otherwise open linearly: maxseg per window 192610be5648SGarrett Wollman * (maxseg^2 / cwnd per packet). 1927df8bae1dSRodney W. Grimes */ 1928df8bae1dSRodney W. Grimes { 1929df8bae1dSRodney W. Grimes register u_int cw = tp->snd_cwnd; 1930df8bae1dSRodney W. Grimes register u_int incr = tp->t_maxseg; 1931df8bae1dSRodney W. Grimes 1932df8bae1dSRodney W. Grimes if (cw > tp->snd_ssthresh) 193310be5648SGarrett Wollman incr = incr * incr / cw; 193446f58482SJonathan Lemon if (tcp_do_newreno == 0 || SEQ_GEQ(th->th_ack, tp->snd_recover)) 1935df8bae1dSRodney W. Grimes tp->snd_cwnd = min(cw + incr,TCP_MAXWIN<<tp->snd_scale); 1936df8bae1dSRodney W. Grimes } 1937df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 1938df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 1939df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 1940df8bae1dSRodney W. Grimes ourfinisacked = 1; 1941df8bae1dSRodney W. Grimes } else { 1942df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 1943df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 1944df8bae1dSRodney W. Grimes ourfinisacked = 0; 1945df8bae1dSRodney W. Grimes } 1946df8bae1dSRodney W. Grimes sowwakeup(so); 1947fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 1948df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1949df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 1950df8bae1dSRodney W. Grimes 1951df8bae1dSRodney W. Grimes switch (tp->t_state) { 1952df8bae1dSRodney W. Grimes 1953df8bae1dSRodney W. Grimes /* 1954df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 1955df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 1956df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 1957df8bae1dSRodney W. Grimes */ 1958df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1959df8bae1dSRodney W. Grimes if (ourfinisacked) { 1960df8bae1dSRodney W. Grimes /* 1961df8bae1dSRodney W. Grimes * If we can't receive any more 1962df8bae1dSRodney W. Grimes * data, then closing user can proceed. 1963df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 1964df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 1965df8bae1dSRodney W. Grimes * we'll hang forever. 1966df8bae1dSRodney W. Grimes */ 1967df8bae1dSRodney W. Grimes if (so->so_state & SS_CANTRCVMORE) { 1968df8bae1dSRodney W. Grimes soisdisconnected(so); 19699b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, tcp_maxidle, 19709b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 1971df8bae1dSRodney W. Grimes } 1972df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 1973df8bae1dSRodney W. Grimes } 1974df8bae1dSRodney W. Grimes break; 1975df8bae1dSRodney W. Grimes 1976df8bae1dSRodney W. Grimes /* 1977df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 1978df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 1979df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 1980df8bae1dSRodney W. Grimes * the segment. 1981df8bae1dSRodney W. Grimes */ 1982df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1983df8bae1dSRodney W. Grimes if (ourfinisacked) { 1984df8bae1dSRodney W. Grimes tp->t_state = TCPS_TIME_WAIT; 1985df8bae1dSRodney W. Grimes tcp_canceltimers(tp); 1986a0292f23SGarrett Wollman /* Shorten TIME_WAIT [RFC-1644, p.28] */ 1987a0292f23SGarrett Wollman if (tp->cc_recv != 0 && 19889b8b58e0SJonathan Lemon (ticks - tp->t_starttime) < tcp_msl) 19899b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 19909b8b58e0SJonathan Lemon tp->t_rxtcur * 19919b8b58e0SJonathan Lemon TCPTV_TWTRUNC, 19929b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 1993a0292f23SGarrett Wollman else 19949b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 19959b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 1996df8bae1dSRodney W. Grimes soisdisconnected(so); 1997df8bae1dSRodney W. Grimes } 1998df8bae1dSRodney W. Grimes break; 1999df8bae1dSRodney W. Grimes 2000df8bae1dSRodney W. Grimes /* 2001df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2002df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2003df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2004df8bae1dSRodney W. Grimes * enter the closed state and return. 2005df8bae1dSRodney W. Grimes */ 2006df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2007df8bae1dSRodney W. Grimes if (ourfinisacked) { 2008df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2009df8bae1dSRodney W. Grimes goto drop; 2010df8bae1dSRodney W. Grimes } 2011df8bae1dSRodney W. Grimes break; 2012df8bae1dSRodney W. Grimes 2013df8bae1dSRodney W. Grimes /* 2014df8bae1dSRodney W. Grimes * In TIME_WAIT state the only thing that should arrive 2015df8bae1dSRodney W. Grimes * is a retransmission of the remote FIN. Acknowledge 2016df8bae1dSRodney W. Grimes * it and restart the finack timer. 2017df8bae1dSRodney W. Grimes */ 2018df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 20199b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 20209b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2021df8bae1dSRodney W. Grimes goto dropafterack; 2022df8bae1dSRodney W. Grimes } 2023df8bae1dSRodney W. Grimes } 2024df8bae1dSRodney W. Grimes 2025df8bae1dSRodney W. Grimes step6: 2026df8bae1dSRodney W. Grimes /* 2027df8bae1dSRodney W. Grimes * Update window information. 2028df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 2029df8bae1dSRodney W. Grimes */ 2030fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 2031fb59c426SYoshinobu Inoue (SEQ_LT(tp->snd_wl1, th->th_seq) || 2032fb59c426SYoshinobu Inoue (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2033fb59c426SYoshinobu Inoue (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2034df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 2035fb59c426SYoshinobu Inoue if (tlen == 0 && 2036fb59c426SYoshinobu Inoue tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2037df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinupd++; 2038df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 2039fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq; 2040fb59c426SYoshinobu Inoue tp->snd_wl2 = th->th_ack; 2041df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2042df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 2043df8bae1dSRodney W. Grimes needoutput = 1; 2044df8bae1dSRodney W. Grimes } 2045df8bae1dSRodney W. Grimes 2046df8bae1dSRodney W. Grimes /* 2047df8bae1dSRodney W. Grimes * Process segments with URG. 2048df8bae1dSRodney W. Grimes */ 2049fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2050df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2051df8bae1dSRodney W. Grimes /* 2052df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2053df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2054df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2055df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2056df8bae1dSRodney W. Grimes */ 2057fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2058fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2059fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 2060df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2061df8bae1dSRodney W. Grimes } 2062df8bae1dSRodney W. Grimes /* 2063df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2064df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2065df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2066df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2067df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2068df8bae1dSRodney W. Grimes * 2069df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2070df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2071df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2072df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2073df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2074df8bae1dSRodney W. Grimes * spec states (in one of two places). 2075df8bae1dSRodney W. Grimes */ 2076fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2077fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2078df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2079df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2080df8bae1dSRodney W. Grimes if (so->so_oobmark == 0) 2081df8bae1dSRodney W. Grimes so->so_state |= SS_RCVATMARK; 2082df8bae1dSRodney W. Grimes sohasoutofband(so); 2083df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2084df8bae1dSRodney W. Grimes } 2085df8bae1dSRodney W. Grimes /* 2086df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2087df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2088df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2089df8bae1dSRodney W. Grimes * data may creep in... ick. 2090df8bae1dSRodney W. Grimes */ 2091fb59c426SYoshinobu Inoue if (th->th_urp <= (u_long)tlen 2092df8bae1dSRodney W. Grimes #ifdef SO_OOBINLINE 2093df8bae1dSRodney W. Grimes && (so->so_options & SO_OOBINLINE) == 0 2094df8bae1dSRodney W. Grimes #endif 2095df8bae1dSRodney W. Grimes ) 2096fb59c426SYoshinobu Inoue tcp_pulloutofband(so, th, m, 2097fb59c426SYoshinobu Inoue drop_hdrlen); /* hdr drop is delayed */ 2098df8bae1dSRodney W. Grimes } else 2099df8bae1dSRodney W. Grimes /* 2100df8bae1dSRodney W. Grimes * If no out of band data is expected, 2101df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2102df8bae1dSRodney W. Grimes * with the receive window. 2103df8bae1dSRodney W. Grimes */ 2104df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2105df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2106df8bae1dSRodney W. Grimes dodata: /* XXX */ 2107df8bae1dSRodney W. Grimes 2108df8bae1dSRodney W. Grimes /* 2109df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2110df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2111df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2112df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2113df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2114df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2115df8bae1dSRodney W. Grimes */ 2116fb59c426SYoshinobu Inoue if ((tlen || (thflags&TH_FIN)) && 2117df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2118fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2119fb59c426SYoshinobu Inoue TCP_REASS(tp, th, &tlen, m, so, thflags); 2120df8bae1dSRodney W. Grimes /* 2121df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2122df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2123df8bae1dSRodney W. Grimes * buffer size. 2124df8bae1dSRodney W. Grimes */ 2125df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2126df8bae1dSRodney W. Grimes } else { 2127df8bae1dSRodney W. Grimes m_freem(m); 2128fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2129df8bae1dSRodney W. Grimes } 2130df8bae1dSRodney W. Grimes 2131df8bae1dSRodney W. Grimes /* 2132df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2133df8bae1dSRodney W. Grimes * that the connection is closing. 2134df8bae1dSRodney W. Grimes */ 2135fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2136df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2137df8bae1dSRodney W. Grimes socantrcvmore(so); 2138a0292f23SGarrett Wollman /* 2139a0292f23SGarrett Wollman * If connection is half-synchronized 2140d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2141a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2142a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2143a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2144a0292f23SGarrett Wollman */ 2145f498eeeeSDavid Greenman if (tcp_delack_enabled && (tp->t_flags & TF_NEEDSYN)) 21469b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 21479b8b58e0SJonathan Lemon tcp_timer_delack, tp); 2148a0292f23SGarrett Wollman else 2149df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2150df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2151df8bae1dSRodney W. Grimes } 2152df8bae1dSRodney W. Grimes switch (tp->t_state) { 2153df8bae1dSRodney W. Grimes 2154df8bae1dSRodney W. Grimes /* 2155df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2156df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2157df8bae1dSRodney W. Grimes */ 2158df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 21599b8b58e0SJonathan Lemon tp->t_starttime = ticks; 21609b8b58e0SJonathan Lemon /*FALLTHROUGH*/ 2161df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2162df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2163df8bae1dSRodney W. Grimes break; 2164df8bae1dSRodney W. Grimes 2165df8bae1dSRodney W. Grimes /* 2166df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2167df8bae1dSRodney W. Grimes * enter the CLOSING state. 2168df8bae1dSRodney W. Grimes */ 2169df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2170df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 2171df8bae1dSRodney W. Grimes break; 2172df8bae1dSRodney W. Grimes 2173df8bae1dSRodney W. Grimes /* 2174df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 2175df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 2176df8bae1dSRodney W. Grimes * standard timers. 2177df8bae1dSRodney W. Grimes */ 2178df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2179df8bae1dSRodney W. Grimes tp->t_state = TCPS_TIME_WAIT; 2180df8bae1dSRodney W. Grimes tcp_canceltimers(tp); 2181a0292f23SGarrett Wollman /* Shorten TIME_WAIT [RFC-1644, p.28] */ 2182a0292f23SGarrett Wollman if (tp->cc_recv != 0 && 21839b8b58e0SJonathan Lemon (ticks - tp->t_starttime) < tcp_msl) { 21849b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 21859b8b58e0SJonathan Lemon tp->t_rxtcur * TCPTV_TWTRUNC, 21869b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2187a0292f23SGarrett Wollman /* For transaction client, force ACK now. */ 2188a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2189a0292f23SGarrett Wollman } 2190a0292f23SGarrett Wollman else 21919b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 21929b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2193df8bae1dSRodney W. Grimes soisdisconnected(so); 2194df8bae1dSRodney W. Grimes break; 2195df8bae1dSRodney W. Grimes 2196df8bae1dSRodney W. Grimes /* 2197df8bae1dSRodney W. Grimes * In TIME_WAIT state restart the 2 MSL time_wait timer. 2198df8bae1dSRodney W. Grimes */ 2199df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 22009b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 22019b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2202df8bae1dSRodney W. Grimes break; 2203df8bae1dSRodney W. Grimes } 2204df8bae1dSRodney W. Grimes } 2205610ee2f9SDavid Greenman #ifdef TCPDEBUG 2206df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) 2207fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2208fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2209610ee2f9SDavid Greenman #endif 2210df8bae1dSRodney W. Grimes 2211df8bae1dSRodney W. Grimes /* 2212df8bae1dSRodney W. Grimes * Return any desired output. 2213df8bae1dSRodney W. Grimes */ 2214df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 2215df8bae1dSRodney W. Grimes (void) tcp_output(tp); 2216df8bae1dSRodney W. Grimes return; 2217df8bae1dSRodney W. Grimes 2218df8bae1dSRodney W. Grimes dropafterack: 2219df8bae1dSRodney W. Grimes /* 2220df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 2221df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 222280ab7c0eSGarrett Wollman * 222380ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 222480ab7c0eSGarrett Wollman * paths to this code happen after packets containing 222580ab7c0eSGarrett Wollman * RST have been dropped. 222680ab7c0eSGarrett Wollman * 222780ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 222880ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 222980ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 223080ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 223180ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 223280ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 2233df8bae1dSRodney W. Grimes */ 2234fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2235fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 2236fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max)) ) 2237173c0f9fSWarner Losh goto maybedropwithreset; 2238a0292f23SGarrett Wollman #ifdef TCPDEBUG 2239a0292f23SGarrett Wollman if (so->so_options & SO_DEBUG) 2240fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2241fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2242a0292f23SGarrett Wollman #endif 2243df8bae1dSRodney W. Grimes m_freem(m); 2244df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2245df8bae1dSRodney W. Grimes (void) tcp_output(tp); 2246df8bae1dSRodney W. Grimes return; 2247df8bae1dSRodney W. Grimes 2248173c0f9fSWarner Losh 2249173c0f9fSWarner Losh /* 2250173c0f9fSWarner Losh * Conditionally drop with reset or just drop depending on whether 2251173c0f9fSWarner Losh * we think we are under attack or not. 2252173c0f9fSWarner Losh */ 2253173c0f9fSWarner Losh maybedropwithreset: 2254173c0f9fSWarner Losh #ifdef ICMP_BANDLIM 2255173c0f9fSWarner Losh if (badport_bandlim(1) < 0) 2256173c0f9fSWarner Losh goto drop; 2257173c0f9fSWarner Losh #endif 2258173c0f9fSWarner Losh /* fall through */ 2259df8bae1dSRodney W. Grimes dropwithreset: 2260e46cd3d4SDag-Erling Smørgrav #ifdef TCP_RESTRICT_RST 2261e46cd3d4SDag-Erling Smørgrav if (restrict_rst) 2262e46cd3d4SDag-Erling Smørgrav goto drop; 2263e46cd3d4SDag-Erling Smørgrav #endif 2264df8bae1dSRodney W. Grimes /* 2265df8bae1dSRodney W. Grimes * Generate a RST, dropping incoming segment. 2266df8bae1dSRodney W. Grimes * Make ACK acceptable to originator of segment. 2267df8bae1dSRodney W. Grimes * Don't bother to respond if destination was broadcast/multicast. 2268df8bae1dSRodney W. Grimes */ 2269fb59c426SYoshinobu Inoue if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2270df8bae1dSRodney W. Grimes goto drop; 2271fb59c426SYoshinobu Inoue #ifdef INET6 2272fb59c426SYoshinobu Inoue if (isipv6) { 2273173c0f9fSWarner Losh if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2274173c0f9fSWarner Losh IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2275fb59c426SYoshinobu Inoue goto drop; 2276fb59c426SYoshinobu Inoue } else 2277fb59c426SYoshinobu Inoue #endif /* INET6 */ 2278173c0f9fSWarner Losh if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2279173c0f9fSWarner Losh IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2280173c0f9fSWarner Losh ip->ip_src.s_addr == htonl(INADDR_BROADCAST)) 2281fb59c426SYoshinobu Inoue goto drop; 2282fb59c426SYoshinobu Inoue /* IPv6 anycast check is done at tcp6_input() */ 2283a0292f23SGarrett Wollman #ifdef TCPDEBUG 2284a0292f23SGarrett Wollman if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2285fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2286fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2287a0292f23SGarrett Wollman #endif 2288fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 2289fb59c426SYoshinobu Inoue /* mtod() below is safe as long as hdr dropping is delayed */ 2290fb59c426SYoshinobu Inoue tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack, 2291fb59c426SYoshinobu Inoue TH_RST); 2292df8bae1dSRodney W. Grimes else { 2293fb59c426SYoshinobu Inoue if (thflags & TH_SYN) 2294fb59c426SYoshinobu Inoue tlen++; 2295fb59c426SYoshinobu Inoue /* mtod() below is safe as long as hdr dropping is delayed */ 2296fb59c426SYoshinobu Inoue tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2297fb59c426SYoshinobu Inoue (tcp_seq)0, TH_RST|TH_ACK); 2298df8bae1dSRodney W. Grimes } 2299df8bae1dSRodney W. Grimes /* destroy temporarily created socket */ 2300df8bae1dSRodney W. Grimes if (dropsocket) 2301df8bae1dSRodney W. Grimes (void) soabort(so); 2302df8bae1dSRodney W. Grimes return; 2303df8bae1dSRodney W. Grimes 2304df8bae1dSRodney W. Grimes drop: 2305df8bae1dSRodney W. Grimes /* 2306df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 2307df8bae1dSRodney W. Grimes */ 2308610ee2f9SDavid Greenman #ifdef TCPDEBUG 2309a0292f23SGarrett Wollman if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2310fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2311fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2312a0292f23SGarrett Wollman #endif 2313df8bae1dSRodney W. Grimes m_freem(m); 2314df8bae1dSRodney W. Grimes /* destroy temporarily created socket */ 2315df8bae1dSRodney W. Grimes if (dropsocket) 2316df8bae1dSRodney W. Grimes (void) soabort(so); 2317df8bae1dSRodney W. Grimes return; 2318df8bae1dSRodney W. Grimes } 2319df8bae1dSRodney W. Grimes 23200312fbe9SPoul-Henning Kamp static void 2321fb59c426SYoshinobu Inoue tcp_dooptions(tp, cp, cnt, th, to) 2322df8bae1dSRodney W. Grimes struct tcpcb *tp; 2323df8bae1dSRodney W. Grimes u_char *cp; 2324df8bae1dSRodney W. Grimes int cnt; 2325fb59c426SYoshinobu Inoue struct tcphdr *th; 2326a0292f23SGarrett Wollman struct tcpopt *to; 2327df8bae1dSRodney W. Grimes { 2328a0292f23SGarrett Wollman u_short mss = 0; 2329df8bae1dSRodney W. Grimes int opt, optlen; 2330df8bae1dSRodney W. Grimes 2331df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 2332df8bae1dSRodney W. Grimes opt = cp[0]; 2333df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 2334df8bae1dSRodney W. Grimes break; 2335df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 2336df8bae1dSRodney W. Grimes optlen = 1; 2337df8bae1dSRodney W. Grimes else { 2338df8bae1dSRodney W. Grimes optlen = cp[1]; 2339df8bae1dSRodney W. Grimes if (optlen <= 0) 2340df8bae1dSRodney W. Grimes break; 2341df8bae1dSRodney W. Grimes } 2342df8bae1dSRodney W. Grimes switch (opt) { 2343df8bae1dSRodney W. Grimes 2344df8bae1dSRodney W. Grimes default: 2345df8bae1dSRodney W. Grimes continue; 2346df8bae1dSRodney W. Grimes 2347df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 2348df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 2349df8bae1dSRodney W. Grimes continue; 2350fb59c426SYoshinobu Inoue if (!(th->th_flags & TH_SYN)) 2351df8bae1dSRodney W. Grimes continue; 2352df8bae1dSRodney W. Grimes bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); 2353df8bae1dSRodney W. Grimes NTOHS(mss); 2354df8bae1dSRodney W. Grimes break; 2355df8bae1dSRodney W. Grimes 2356df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 2357df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 2358df8bae1dSRodney W. Grimes continue; 2359fb59c426SYoshinobu Inoue if (!(th->th_flags & TH_SYN)) 2360df8bae1dSRodney W. Grimes continue; 2361df8bae1dSRodney W. Grimes tp->t_flags |= TF_RCVD_SCALE; 2362df8bae1dSRodney W. Grimes tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 2363df8bae1dSRodney W. Grimes break; 2364df8bae1dSRodney W. Grimes 2365df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 2366df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 2367df8bae1dSRodney W. Grimes continue; 2368a0292f23SGarrett Wollman to->to_flag |= TOF_TS; 2369a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2370a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 2371a0292f23SGarrett Wollman NTOHL(to->to_tsval); 2372a0292f23SGarrett Wollman bcopy((char *)cp + 6, 2373a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2374a0292f23SGarrett Wollman NTOHL(to->to_tsecr); 2375df8bae1dSRodney W. Grimes 2376df8bae1dSRodney W. Grimes /* 2377df8bae1dSRodney W. Grimes * A timestamp received in a SYN makes 2378df8bae1dSRodney W. Grimes * it ok to send timestamp requests and replies. 2379df8bae1dSRodney W. Grimes */ 2380fb59c426SYoshinobu Inoue if (th->th_flags & TH_SYN) { 2381df8bae1dSRodney W. Grimes tp->t_flags |= TF_RCVD_TSTMP; 2382a0292f23SGarrett Wollman tp->ts_recent = to->to_tsval; 23839b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 2384df8bae1dSRodney W. Grimes } 2385df8bae1dSRodney W. Grimes break; 2386a0292f23SGarrett Wollman case TCPOPT_CC: 2387a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2388a0292f23SGarrett Wollman continue; 238940db8ef7SAndras Olah to->to_flag |= TOF_CC; 2390a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2391a0292f23SGarrett Wollman (char *)&to->to_cc, sizeof(to->to_cc)); 2392a0292f23SGarrett Wollman NTOHL(to->to_cc); 2393a0292f23SGarrett Wollman /* 2394a0292f23SGarrett Wollman * A CC or CC.new option received in a SYN makes 2395a0292f23SGarrett Wollman * it ok to send CC in subsequent segments. 2396a0292f23SGarrett Wollman */ 2397fb59c426SYoshinobu Inoue if (th->th_flags & TH_SYN) 2398a0292f23SGarrett Wollman tp->t_flags |= TF_RCVD_CC; 2399a0292f23SGarrett Wollman break; 2400a0292f23SGarrett Wollman case TCPOPT_CCNEW: 2401a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2402a0292f23SGarrett Wollman continue; 2403fb59c426SYoshinobu Inoue if (!(th->th_flags & TH_SYN)) 2404a0292f23SGarrett Wollman continue; 2405a0292f23SGarrett Wollman to->to_flag |= TOF_CCNEW; 2406a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2407a0292f23SGarrett Wollman (char *)&to->to_cc, sizeof(to->to_cc)); 2408a0292f23SGarrett Wollman NTOHL(to->to_cc); 2409a0292f23SGarrett Wollman /* 2410a0292f23SGarrett Wollman * A CC or CC.new option received in a SYN makes 2411a0292f23SGarrett Wollman * it ok to send CC in subsequent segments. 2412a0292f23SGarrett Wollman */ 2413a0292f23SGarrett Wollman tp->t_flags |= TF_RCVD_CC; 2414a0292f23SGarrett Wollman break; 2415a0292f23SGarrett Wollman case TCPOPT_CCECHO: 2416a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2417a0292f23SGarrett Wollman continue; 2418fb59c426SYoshinobu Inoue if (!(th->th_flags & TH_SYN)) 2419a0292f23SGarrett Wollman continue; 2420a0292f23SGarrett Wollman to->to_flag |= TOF_CCECHO; 2421a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2422a0292f23SGarrett Wollman (char *)&to->to_ccecho, sizeof(to->to_ccecho)); 2423a0292f23SGarrett Wollman NTOHL(to->to_ccecho); 2424a0292f23SGarrett Wollman break; 2425df8bae1dSRodney W. Grimes } 2426df8bae1dSRodney W. Grimes } 2427fb59c426SYoshinobu Inoue if (th->th_flags & TH_SYN) 2428a0292f23SGarrett Wollman tcp_mss(tp, mss); /* sets t_maxseg */ 2429df8bae1dSRodney W. Grimes } 2430df8bae1dSRodney W. Grimes 2431df8bae1dSRodney W. Grimes /* 2432df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 2433df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 2434df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 2435df8bae1dSRodney W. Grimes * sequencing purposes. 2436df8bae1dSRodney W. Grimes */ 24370312fbe9SPoul-Henning Kamp static void 2438fb59c426SYoshinobu Inoue tcp_pulloutofband(so, th, m, off) 2439df8bae1dSRodney W. Grimes struct socket *so; 2440fb59c426SYoshinobu Inoue struct tcphdr *th; 2441df8bae1dSRodney W. Grimes register struct mbuf *m; 2442fb59c426SYoshinobu Inoue int off; /* delayed to be droped hdrlen */ 2443df8bae1dSRodney W. Grimes { 2444fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 2445df8bae1dSRodney W. Grimes 2446df8bae1dSRodney W. Grimes while (cnt >= 0) { 2447df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 2448df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 2449df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 2450df8bae1dSRodney W. Grimes 2451df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 2452df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 2453df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2454df8bae1dSRodney W. Grimes m->m_len--; 245569a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 245669a34685SYoshinobu Inoue m->m_pkthdr.len--; 2457df8bae1dSRodney W. Grimes return; 2458df8bae1dSRodney W. Grimes } 2459df8bae1dSRodney W. Grimes cnt -= m->m_len; 2460df8bae1dSRodney W. Grimes m = m->m_next; 2461df8bae1dSRodney W. Grimes if (m == 0) 2462df8bae1dSRodney W. Grimes break; 2463df8bae1dSRodney W. Grimes } 2464df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 2465df8bae1dSRodney W. Grimes } 2466df8bae1dSRodney W. Grimes 2467df8bae1dSRodney W. Grimes /* 2468df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 2469df8bae1dSRodney W. Grimes * and update averages and current timeout. 2470df8bae1dSRodney W. Grimes */ 24710312fbe9SPoul-Henning Kamp static void 2472df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt) 2473df8bae1dSRodney W. Grimes register struct tcpcb *tp; 24749b8b58e0SJonathan Lemon int rtt; 2475df8bae1dSRodney W. Grimes { 2476233e8c18SGarrett Wollman register int delta; 2477233e8c18SGarrett Wollman 2478233e8c18SGarrett Wollman tcpstat.tcps_rttupdated++; 2479233e8c18SGarrett Wollman tp->t_rttupdated++; 2480233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 2481233e8c18SGarrett Wollman /* 2482233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 2483233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 2484233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 2485233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2486233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 2487233e8c18SGarrett Wollman */ 2488233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2489233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2490233e8c18SGarrett Wollman 2491233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 2492233e8c18SGarrett Wollman tp->t_srtt = 1; 2493233e8c18SGarrett Wollman 2494233e8c18SGarrett Wollman /* 2495233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 2496233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 2497233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 2498233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 2499233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 2500233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 2501233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2502233e8c18SGarrett Wollman * rfc793's wired-in beta. 2503233e8c18SGarrett Wollman */ 2504233e8c18SGarrett Wollman if (delta < 0) 2505233e8c18SGarrett Wollman delta = -delta; 2506233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2507233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 2508233e8c18SGarrett Wollman tp->t_rttvar = 1; 2509233e8c18SGarrett Wollman } else { 2510233e8c18SGarrett Wollman /* 2511233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 2512233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 2513233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 2514233e8c18SGarrett Wollman */ 2515233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 2516233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 2517233e8c18SGarrett Wollman } 25189b8b58e0SJonathan Lemon tp->t_rtttime = 0; 2519df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 2520df8bae1dSRodney W. Grimes 2521df8bae1dSRodney W. Grimes /* 2522df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 2523df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 2524df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 2525df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 2526df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 2527df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 2528df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 2529df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 2530df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 2531df8bae1dSRodney W. Grimes */ 2532233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 25339e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2534df8bae1dSRodney W. Grimes 2535df8bae1dSRodney W. Grimes /* 2536df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 2537df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 2538df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 2539df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 2540df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 2541df8bae1dSRodney W. Grimes */ 2542df8bae1dSRodney W. Grimes tp->t_softerror = 0; 2543df8bae1dSRodney W. Grimes } 2544df8bae1dSRodney W. Grimes 2545df8bae1dSRodney W. Grimes /* 2546df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 2547df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 2548df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 2549df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 2550df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2551df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 2552df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 2553df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 2554df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 2555df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 2556df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 2557df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 2558df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 2559a0292f23SGarrett Wollman * 2560a0292f23SGarrett Wollman * Also take into account the space needed for options that we 2561a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 2562a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 2563a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 2564a0292f23SGarrett Wollman * data in maxopd. 2565a0292f23SGarrett Wollman * 2566a0292f23SGarrett Wollman * NOTE that this routine is only called when we process an incoming 2567a0292f23SGarrett Wollman * segment, for outgoing segments only tcp_mssopt is called. 2568a0292f23SGarrett Wollman * 2569a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 2570a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 2571a0292f23SGarrett Wollman * MSS of our peer. 2572df8bae1dSRodney W. Grimes */ 2573a0292f23SGarrett Wollman void 2574df8bae1dSRodney W. Grimes tcp_mss(tp, offer) 2575a0292f23SGarrett Wollman struct tcpcb *tp; 2576a0292f23SGarrett Wollman int offer; 2577df8bae1dSRodney W. Grimes { 2578df8bae1dSRodney W. Grimes register struct rtentry *rt; 2579df8bae1dSRodney W. Grimes struct ifnet *ifp; 2580df8bae1dSRodney W. Grimes register int rtt, mss; 2581df8bae1dSRodney W. Grimes u_long bufsize; 2582df8bae1dSRodney W. Grimes struct inpcb *inp; 2583df8bae1dSRodney W. Grimes struct socket *so; 2584a0292f23SGarrett Wollman struct rmxp_tao *taop; 2585a0292f23SGarrett Wollman int origoffer = offer; 2586fb59c426SYoshinobu Inoue #ifdef INET6 2587fb59c426SYoshinobu Inoue int isipv6; 2588fb59c426SYoshinobu Inoue int min_protoh; 2589fb59c426SYoshinobu Inoue #endif 2590df8bae1dSRodney W. Grimes 2591df8bae1dSRodney W. Grimes inp = tp->t_inpcb; 2592fb59c426SYoshinobu Inoue #ifdef INET6 2593fb59c426SYoshinobu Inoue isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2594fb59c426SYoshinobu Inoue min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr) 2595fb59c426SYoshinobu Inoue : sizeof (struct tcpiphdr); 2596fb59c426SYoshinobu Inoue #else 2597fb59c426SYoshinobu Inoue #define min_protoh (sizeof (struct tcpiphdr)) 2598fb59c426SYoshinobu Inoue #endif 2599fb59c426SYoshinobu Inoue #ifdef INET6 2600fb59c426SYoshinobu Inoue if (isipv6) 2601fb59c426SYoshinobu Inoue rt = tcp_rtlookup6(inp); 2602fb59c426SYoshinobu Inoue else 2603fb59c426SYoshinobu Inoue #endif 2604fb59c426SYoshinobu Inoue rt = tcp_rtlookup(inp); 2605fb59c426SYoshinobu Inoue if (rt == NULL) { 2606fb59c426SYoshinobu Inoue tp->t_maxopd = tp->t_maxseg = 2607fb59c426SYoshinobu Inoue #ifdef INET6 2608fb59c426SYoshinobu Inoue isipv6 ? tcp_v6mssdflt : 2609fb59c426SYoshinobu Inoue #endif /* INET6 */ 2610fb59c426SYoshinobu Inoue tcp_mssdflt; 2611a0292f23SGarrett Wollman return; 2612df8bae1dSRodney W. Grimes } 2613df8bae1dSRodney W. Grimes ifp = rt->rt_ifp; 2614df8bae1dSRodney W. Grimes so = inp->inp_socket; 2615df8bae1dSRodney W. Grimes 2616a0292f23SGarrett Wollman taop = rmx_taop(rt->rt_rmx); 2617a0292f23SGarrett Wollman /* 2618a0292f23SGarrett Wollman * Offer == -1 means that we didn't receive SYN yet, 2619a0292f23SGarrett Wollman * use cached value in that case; 2620a0292f23SGarrett Wollman */ 2621a0292f23SGarrett Wollman if (offer == -1) 2622a0292f23SGarrett Wollman offer = taop->tao_mssopt; 2623a0292f23SGarrett Wollman /* 2624a0292f23SGarrett Wollman * Offer == 0 means that there was no MSS on the SYN segment, 2625a0292f23SGarrett Wollman * in this case we use tcp_mssdflt. 2626a0292f23SGarrett Wollman */ 2627a0292f23SGarrett Wollman if (offer == 0) 2628fb59c426SYoshinobu Inoue offer = 2629fb59c426SYoshinobu Inoue #ifdef INET6 2630fb59c426SYoshinobu Inoue isipv6 ? tcp_v6mssdflt : 2631fb59c426SYoshinobu Inoue #endif /* INET6 */ 2632fb59c426SYoshinobu Inoue tcp_mssdflt; 2633a0292f23SGarrett Wollman else 2634a0292f23SGarrett Wollman /* 2635a0292f23SGarrett Wollman * Sanity check: make sure that maxopd will be large 2636a0292f23SGarrett Wollman * enough to allow some data on segments even is the 2637a0292f23SGarrett Wollman * all the option space is used (40bytes). Otherwise 2638a0292f23SGarrett Wollman * funny things may happen in tcp_output. 2639a0292f23SGarrett Wollman */ 2640a0292f23SGarrett Wollman offer = max(offer, 64); 2641a0292f23SGarrett Wollman taop->tao_mssopt = offer; 2642a0292f23SGarrett Wollman 2643df8bae1dSRodney W. Grimes /* 2644df8bae1dSRodney W. Grimes * While we're here, check if there's an initial rtt 2645df8bae1dSRodney W. Grimes * or rttvar. Convert from the route-table units 2646df8bae1dSRodney W. Grimes * to scaled multiples of the slow timeout timer. 2647df8bae1dSRodney W. Grimes */ 2648df8bae1dSRodney W. Grimes if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 2649df8bae1dSRodney W. Grimes /* 2650a0292f23SGarrett Wollman * XXX the lock bit for RTT indicates that the value 2651df8bae1dSRodney W. Grimes * is also a minimum value; this is subject to time. 2652df8bae1dSRodney W. Grimes */ 2653df8bae1dSRodney W. Grimes if (rt->rt_rmx.rmx_locks & RTV_RTT) 26549b8b58e0SJonathan Lemon tp->t_rttmin = rtt / (RTM_RTTUNIT / hz); 26559b8b58e0SJonathan Lemon tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE)); 2656dd224982SGarrett Wollman tcpstat.tcps_usedrtt++; 2657dd224982SGarrett Wollman if (rt->rt_rmx.rmx_rttvar) { 2658df8bae1dSRodney W. Grimes tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 26599b8b58e0SJonathan Lemon (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE)); 2660dd224982SGarrett Wollman tcpstat.tcps_usedrttvar++; 2661dd224982SGarrett Wollman } else { 2662df8bae1dSRodney W. Grimes /* default variation is +- 1 rtt */ 2663df8bae1dSRodney W. Grimes tp->t_rttvar = 2664df8bae1dSRodney W. Grimes tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 2665dd224982SGarrett Wollman } 2666df8bae1dSRodney W. Grimes TCPT_RANGESET(tp->t_rxtcur, 2667df8bae1dSRodney W. Grimes ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 2668df8bae1dSRodney W. Grimes tp->t_rttmin, TCPTV_REXMTMAX); 2669df8bae1dSRodney W. Grimes } 2670df8bae1dSRodney W. Grimes /* 2671df8bae1dSRodney W. Grimes * if there's an mtu associated with the route, use it 2672fb59c426SYoshinobu Inoue * else, use the link mtu. 2673df8bae1dSRodney W. Grimes */ 2674df8bae1dSRodney W. Grimes if (rt->rt_rmx.rmx_mtu) 2675fb59c426SYoshinobu Inoue mss = rt->rt_rmx.rmx_mtu - min_protoh; 2676df8bae1dSRodney W. Grimes else 2677df8bae1dSRodney W. Grimes { 2678fb59c426SYoshinobu Inoue mss = 2679fb59c426SYoshinobu Inoue #ifdef INET6 2680fb59c426SYoshinobu Inoue (isipv6 ? nd_ifinfo[rt->rt_ifp->if_index].linkmtu : 2681fb59c426SYoshinobu Inoue #endif 2682fb59c426SYoshinobu Inoue ifp->if_mtu 2683fb59c426SYoshinobu Inoue #ifdef INET6 2684fb59c426SYoshinobu Inoue ) 2685fb59c426SYoshinobu Inoue #endif 2686fb59c426SYoshinobu Inoue - min_protoh; 2687fb59c426SYoshinobu Inoue #ifdef INET6 2688fb59c426SYoshinobu Inoue if (isipv6) { 2689fb59c426SYoshinobu Inoue if (!in6_localaddr(&inp->in6p_faddr)) 2690fb59c426SYoshinobu Inoue mss = min(mss, tcp_v6mssdflt); 2691fb59c426SYoshinobu Inoue } else 2692fb59c426SYoshinobu Inoue #endif 2693a0292f23SGarrett Wollman if (!in_localaddr(inp->inp_faddr)) 2694a0292f23SGarrett Wollman mss = min(mss, tcp_mssdflt); 2695a0292f23SGarrett Wollman } 2696a0292f23SGarrett Wollman mss = min(mss, offer); 2697a0292f23SGarrett Wollman /* 2698a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 2699a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 2700a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 2701a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 2702a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 2703a0292f23SGarrett Wollman */ 2704a0292f23SGarrett Wollman tp->t_maxopd = mss; 2705a0292f23SGarrett Wollman 2706a0292f23SGarrett Wollman /* 2707a0292f23SGarrett Wollman * In case of T/TCP, origoffer==-1 indicates, that no segments 2708a0292f23SGarrett Wollman * were received yet. In this case we just guess, otherwise 2709a0292f23SGarrett Wollman * we do the same as before T/TCP. 2710a0292f23SGarrett Wollman */ 2711a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2712a0292f23SGarrett Wollman (origoffer == -1 || 2713a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2714a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 2715a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 2716a0292f23SGarrett Wollman (origoffer == -1 || 2717a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)) 2718a0292f23SGarrett Wollman mss -= TCPOLEN_CC_APPA; 2719a0292f23SGarrett Wollman 2720df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 2721df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2722df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 2723df8bae1dSRodney W. Grimes #else 2724df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2725df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 2726df8bae1dSRodney W. Grimes #endif 2727df8bae1dSRodney W. Grimes /* 2728df8bae1dSRodney W. Grimes * If there's a pipesize, change the socket buffer 2729df8bae1dSRodney W. Grimes * to that size. Make the socket buffers an integral 2730df8bae1dSRodney W. Grimes * number of mss units; if the mss is larger than 2731df8bae1dSRodney W. Grimes * the socket buffer, decrease the mss. 2732df8bae1dSRodney W. Grimes */ 2733df8bae1dSRodney W. Grimes #ifdef RTV_SPIPE 2734df8bae1dSRodney W. Grimes if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 2735df8bae1dSRodney W. Grimes #endif 2736df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 2737df8bae1dSRodney W. Grimes if (bufsize < mss) 2738df8bae1dSRodney W. Grimes mss = bufsize; 2739df8bae1dSRodney W. Grimes else { 2740df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2741df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2742df8bae1dSRodney W. Grimes bufsize = sb_max; 2743ecf72308SBrian Feldman (void)sbreserve(&so->so_snd, bufsize, so, NULL); 2744df8bae1dSRodney W. Grimes } 2745df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 2746df8bae1dSRodney W. Grimes 2747df8bae1dSRodney W. Grimes #ifdef RTV_RPIPE 2748df8bae1dSRodney W. Grimes if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 2749df8bae1dSRodney W. Grimes #endif 2750df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 2751df8bae1dSRodney W. Grimes if (bufsize > mss) { 2752df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2753df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2754df8bae1dSRodney W. Grimes bufsize = sb_max; 2755ecf72308SBrian Feldman (void)sbreserve(&so->so_rcv, bufsize, so, NULL); 2756df8bae1dSRodney W. Grimes } 27579b8b58e0SJonathan Lemon 2758a0292f23SGarrett Wollman /* 27599b8b58e0SJonathan Lemon * Set the slow-start flight size depending on whether this 27609b8b58e0SJonathan Lemon * is a local network or not. 2761a0292f23SGarrett Wollman */ 2762fb59c426SYoshinobu Inoue if ( 2763fb59c426SYoshinobu Inoue #ifdef INET6 2764fb59c426SYoshinobu Inoue (isipv6 && in6_localaddr(&inp->in6p_faddr)) || 2765fb59c426SYoshinobu Inoue (!isipv6 && 2766fb59c426SYoshinobu Inoue #endif 2767fb59c426SYoshinobu Inoue in_localaddr(inp->inp_faddr) 2768fb59c426SYoshinobu Inoue #ifdef INET6 2769fb59c426SYoshinobu Inoue ) 2770fb59c426SYoshinobu Inoue #endif 2771fb59c426SYoshinobu Inoue ) 27729b8b58e0SJonathan Lemon tp->snd_cwnd = mss * ss_fltsz_local; 27739b8b58e0SJonathan Lemon else 27749b8b58e0SJonathan Lemon tp->snd_cwnd = mss * ss_fltsz; 2775df8bae1dSRodney W. Grimes 2776df8bae1dSRodney W. Grimes if (rt->rt_rmx.rmx_ssthresh) { 2777df8bae1dSRodney W. Grimes /* 2778df8bae1dSRodney W. Grimes * There's some sort of gateway or interface 2779df8bae1dSRodney W. Grimes * buffer limit on the path. Use this to set 2780df8bae1dSRodney W. Grimes * the slow start threshhold, but set the 2781df8bae1dSRodney W. Grimes * threshold to no less than 2*mss. 2782df8bae1dSRodney W. Grimes */ 2783df8bae1dSRodney W. Grimes tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 2784dd224982SGarrett Wollman tcpstat.tcps_usedssthresh++; 2785df8bae1dSRodney W. Grimes } 2786a0292f23SGarrett Wollman } 2787a0292f23SGarrett Wollman 2788a0292f23SGarrett Wollman /* 2789a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 2790a0292f23SGarrett Wollman */ 2791a0292f23SGarrett Wollman int 2792a0292f23SGarrett Wollman tcp_mssopt(tp) 2793a0292f23SGarrett Wollman struct tcpcb *tp; 2794a0292f23SGarrett Wollman { 2795a0292f23SGarrett Wollman struct rtentry *rt; 2796fb59c426SYoshinobu Inoue #ifdef INET6 2797fb59c426SYoshinobu Inoue int isipv6; 2798fb59c426SYoshinobu Inoue int min_protoh; 2799fb59c426SYoshinobu Inoue #endif 2800a0292f23SGarrett Wollman 2801fb59c426SYoshinobu Inoue #ifdef INET6 2802fb59c426SYoshinobu Inoue isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2803fb59c426SYoshinobu Inoue min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr) 2804fb59c426SYoshinobu Inoue : sizeof (struct tcpiphdr); 2805fb59c426SYoshinobu Inoue #else 2806fb59c426SYoshinobu Inoue #define min_protoh (sizeof (struct tcpiphdr)) 2807fb59c426SYoshinobu Inoue #endif 2808fb59c426SYoshinobu Inoue #ifdef INET6 2809fb59c426SYoshinobu Inoue if (isipv6) 2810fb59c426SYoshinobu Inoue rt = tcp_rtlookup6(tp->t_inpcb); 2811fb59c426SYoshinobu Inoue else 2812fb59c426SYoshinobu Inoue #endif /* INET6 */ 2813a0292f23SGarrett Wollman rt = tcp_rtlookup(tp->t_inpcb); 2814a0292f23SGarrett Wollman if (rt == NULL) 2815fb59c426SYoshinobu Inoue return 2816fb59c426SYoshinobu Inoue #ifdef INET6 2817fb59c426SYoshinobu Inoue isipv6 ? tcp_v6mssdflt : 2818fb59c426SYoshinobu Inoue #endif /* INET6 */ 2819fb59c426SYoshinobu Inoue tcp_mssdflt; 2820a0292f23SGarrett Wollman 2821fb59c426SYoshinobu Inoue return rt->rt_ifp->if_mtu - min_protoh; 2822df8bae1dSRodney W. Grimes } 282346f58482SJonathan Lemon 282446f58482SJonathan Lemon 282546f58482SJonathan Lemon /* 282646f58482SJonathan Lemon * Checks for partial ack. If partial ack arrives, force the retransmission 282746f58482SJonathan Lemon * of the next unacknowledged segment, do not clear tp->t_dupacks, and return 282846f58482SJonathan Lemon * 1. By setting snd_nxt to ti_ack, this forces retransmission timer to 282946f58482SJonathan Lemon * be started again. If the ack advances at least to tp->snd_recover, return 0. 283046f58482SJonathan Lemon */ 283146f58482SJonathan Lemon static int 283246f58482SJonathan Lemon tcp_newreno(tp, th) 283346f58482SJonathan Lemon struct tcpcb *tp; 283446f58482SJonathan Lemon struct tcphdr *th; 283546f58482SJonathan Lemon { 283646f58482SJonathan Lemon if (SEQ_LT(th->th_ack, tp->snd_recover)) { 283746f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 283846f58482SJonathan Lemon tcp_seq ouna = tp->snd_una; /* Haven't updated snd_una yet*/ 283946f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 284046f58482SJonathan Lemon 284146f58482SJonathan Lemon callout_stop(tp->tt_rexmt); 284246f58482SJonathan Lemon tp->t_rtttime = 0; 284346f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 284446f58482SJonathan Lemon tp->snd_cwnd = tp->t_maxseg; 284546f58482SJonathan Lemon tp->snd_una = th->th_ack; 284646f58482SJonathan Lemon (void) tcp_output(tp); 284746f58482SJonathan Lemon 284846f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 284946f58482SJonathan Lemon tp->snd_una = ouna; 285046f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 285146f58482SJonathan Lemon tp->snd_nxt = onxt; 285246f58482SJonathan Lemon /* 285346f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 285446f58482SJonathan Lemon * not updated yet. 285546f58482SJonathan Lemon */ 285646f58482SJonathan Lemon tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg); 285746f58482SJonathan Lemon return (1); 285846f58482SJonathan Lemon } 285946f58482SJonathan Lemon return (0); 286046f58482SJonathan Lemon } 2861