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> 63686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */ 64fb59c426SYoshinobu Inoue #include <netinet/in_var.h> 65686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 66df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 67df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 68fb59c426SYoshinobu Inoue #ifdef INET6 69686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 70686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h> 71686cdd19SJun-ichiro itojun Hagino #include <netinet6/nd6.h> 72fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 73686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 74fb59c426SYoshinobu Inoue #endif 75df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 76df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 77df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 78df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 79df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 80fb59c426SYoshinobu Inoue #ifdef INET6 81fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 82fb59c426SYoshinobu Inoue #endif 83df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 84610ee2f9SDavid Greenman #ifdef TCPDEBUG 85df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 86fb59c426SYoshinobu Inoue 87fb59c426SYoshinobu Inoue u_char tcp_saveipgen[40]; /* the size must be of max ip header, now IPv6 */ 88fb59c426SYoshinobu Inoue struct tcphdr tcp_savetcp; 89fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */ 90fb59c426SYoshinobu Inoue 91fb59c426SYoshinobu Inoue #ifdef IPSEC 92fb59c426SYoshinobu Inoue #include <netinet6/ipsec.h> 933a2a9f79SYoshinobu Inoue #ifdef INET6 943a2a9f79SYoshinobu Inoue #include <netinet6/ipsec6.h> 953a2a9f79SYoshinobu Inoue #endif 96fb59c426SYoshinobu Inoue #include <netkey/key.h> 97fb59c426SYoshinobu Inoue #endif /*IPSEC*/ 98fb59c426SYoshinobu Inoue 99db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 100db4f9cc7SJonathan Lemon 101fb59c426SYoshinobu Inoue MALLOC_DEFINE(M_TSEGQ, "tseg_qent", "TCP segment queue entry"); 102df8bae1dSRodney W. Grimes 1030312fbe9SPoul-Henning Kamp static int tcprexmtthresh = 3; 1042f96f1f4SGarrett Wollman tcp_seq tcp_iss; 1052f96f1f4SGarrett Wollman tcp_cc tcp_ccgen; 1060312fbe9SPoul-Henning Kamp 1072f96f1f4SGarrett Wollman struct tcpstat tcpstat; 1083d177f46SBill Fumerola SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RD, 1093d177f46SBill Fumerola &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 1100312fbe9SPoul-Henning Kamp 111d78a37adSPaul Traina static int log_in_vain = 0; 112816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 1133d177f46SBill Fumerola &log_in_vain, 0, "Log all incoming TCP connections"); 114816a3d83SPoul-Henning Kamp 11516f7f31fSGeoff Rehmet static int blackhole = 0; 11616f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 11716f7f31fSGeoff Rehmet &blackhole, 0, "Do not send RST when dropping refused connections"); 11816f7f31fSGeoff Rehmet 119f498eeeeSDavid Greenman int tcp_delack_enabled = 1; 12084e33c9eSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 1213d177f46SBill Fumerola &tcp_delack_enabled, 0, 1223d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 123f498eeeeSDavid Greenman 124e7f32693SJayanth Vijayaraghavan int tcp_lq_overflow = 1; 125e7f32693SJayanth Vijayaraghavan SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcp_lq_overflow, CTLFLAG_RW, 126e7f32693SJayanth Vijayaraghavan &tcp_lq_overflow, 0, 127e7f32693SJayanth Vijayaraghavan "Listen Queue Overflow"); 128e7f32693SJayanth Vijayaraghavan 129f8613305SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN 130f8613305SDag-Erling Smørgrav static int drop_synfin = 0; 131f8613305SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, 132f8613305SDag-Erling Smørgrav &drop_synfin, 0, "Drop TCP packets with SYN+FIN set"); 133f8613305SDag-Erling Smørgrav #endif 134f8613305SDag-Erling Smørgrav 135e46cd3d4SDag-Erling Smørgrav #ifdef TCP_RESTRICT_RST 136e46cd3d4SDag-Erling Smørgrav static int restrict_rst = 0; 137e46cd3d4SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, restrict_rst, CTLFLAG_RW, 138e46cd3d4SDag-Erling Smørgrav &restrict_rst, 0, "Restrict RST emission"); 139e46cd3d4SDag-Erling Smørgrav #endif 140e46cd3d4SDag-Erling Smørgrav 14115bd2b43SDavid Greenman struct inpcbhead tcb; 142fb59c426SYoshinobu Inoue #define tcb6 tcb /* for KAME src sync over BSD*'s */ 14315bd2b43SDavid Greenman struct inpcbinfo tcbinfo; 144df8bae1dSRodney W. Grimes 1450312fbe9SPoul-Henning Kamp static void tcp_dooptions __P((struct tcpcb *, 146fb59c426SYoshinobu Inoue u_char *, int, struct tcphdr *, struct tcpopt *)); 1470312fbe9SPoul-Henning Kamp static void tcp_pulloutofband __P((struct socket *, 148fb59c426SYoshinobu Inoue struct tcphdr *, struct mbuf *, int)); 149fb59c426SYoshinobu Inoue static int tcp_reass __P((struct tcpcb *, struct tcphdr *, int *, 150fb59c426SYoshinobu Inoue struct mbuf *)); 1510312fbe9SPoul-Henning Kamp static void tcp_xmit_timer __P((struct tcpcb *, int)); 15246f58482SJonathan Lemon static int tcp_newreno __P((struct tcpcb *, struct tcphdr *)); 1530312fbe9SPoul-Henning Kamp 154fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 155fb59c426SYoshinobu Inoue #ifdef INET6 156fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \ 157fb59c426SYoshinobu Inoue do { \ 158fb59c426SYoshinobu Inoue if ((tp) && (tp)->t_inpcb && \ 159fb59c426SYoshinobu Inoue ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0 && \ 160fb59c426SYoshinobu Inoue (tp)->t_inpcb->in6p_route.ro_rt) \ 161686cdd19SJun-ichiro itojun Hagino nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL, 0); \ 162fb59c426SYoshinobu Inoue } while (0) 163fb59c426SYoshinobu Inoue #else 164fb59c426SYoshinobu Inoue #define ND6_HINT(tp) 165fb59c426SYoshinobu Inoue #endif 166df8bae1dSRodney W. Grimes 167df8bae1dSRodney W. Grimes /* 168fb59c426SYoshinobu Inoue * Insert segment which inludes th into reassembly queue of tcp with 169df8bae1dSRodney W. Grimes * control block tp. Return TH_FIN if reassembly now includes 170df8bae1dSRodney W. Grimes * a segment with FIN. The macro form does the common case inline 171df8bae1dSRodney W. Grimes * (segment is the next to be received on an established connection, 172df8bae1dSRodney W. Grimes * and the queue is empty), avoiding linkage into and removal 173df8bae1dSRodney W. Grimes * from the queue and repetition of various conversions. 174df8bae1dSRodney W. Grimes * Set DELACK for segments received in order, but ack immediately 175df8bae1dSRodney W. Grimes * when segments are out of order (so fast retransmit can work). 176df8bae1dSRodney W. Grimes */ 177fb59c426SYoshinobu Inoue #define TCP_REASS(tp, th, tlenp, m, so, flags) { \ 178fb59c426SYoshinobu Inoue if ((th)->th_seq == (tp)->rcv_nxt && \ 179fb59c426SYoshinobu Inoue LIST_EMPTY(&(tp)->t_segq) && \ 1806b067b07SDavid Greenman (tp)->t_state == TCPS_ESTABLISHED) { \ 181f498eeeeSDavid Greenman if (tcp_delack_enabled) \ 1829b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, \ 1839b8b58e0SJonathan Lemon tcp_timer_delack, tp); \ 184f498eeeeSDavid Greenman else \ 185f498eeeeSDavid Greenman tp->t_flags |= TF_ACKNOW; \ 186fb59c426SYoshinobu Inoue (tp)->rcv_nxt += *(tlenp); \ 187fb59c426SYoshinobu Inoue flags = (th)->th_flags & TH_FIN; \ 1886b067b07SDavid Greenman tcpstat.tcps_rcvpack++;\ 189fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyte += *(tlenp);\ 190fb59c426SYoshinobu Inoue ND6_HINT(tp); \ 1916b067b07SDavid Greenman sbappend(&(so)->so_rcv, (m)); \ 1926b067b07SDavid Greenman sorwakeup(so); \ 1936b067b07SDavid Greenman } else { \ 194fb59c426SYoshinobu Inoue (flags) = tcp_reass((tp), (th), (tlenp), (m)); \ 1956b067b07SDavid Greenman tp->t_flags |= TF_ACKNOW; \ 1966b067b07SDavid Greenman } \ 1976b067b07SDavid Greenman } 198df8bae1dSRodney W. Grimes 1990312fbe9SPoul-Henning Kamp static int 200fb59c426SYoshinobu Inoue tcp_reass(tp, th, tlenp, m) 201df8bae1dSRodney W. Grimes register struct tcpcb *tp; 202fb59c426SYoshinobu Inoue register struct tcphdr *th; 203fb59c426SYoshinobu Inoue int *tlenp; 204df8bae1dSRodney W. Grimes struct mbuf *m; 205df8bae1dSRodney W. Grimes { 206fb59c426SYoshinobu Inoue struct tseg_qent *q; 207fb59c426SYoshinobu Inoue struct tseg_qent *p = NULL; 208fb59c426SYoshinobu Inoue struct tseg_qent *nq; 209fb59c426SYoshinobu Inoue struct tseg_qent *te; 210df8bae1dSRodney W. Grimes struct socket *so = tp->t_inpcb->inp_socket; 211df8bae1dSRodney W. Grimes int flags; 212df8bae1dSRodney W. Grimes 213df8bae1dSRodney W. Grimes /* 214fb59c426SYoshinobu Inoue * Call with th==0 after become established to 215df8bae1dSRodney W. Grimes * force pre-ESTABLISHED data up to user socket. 216df8bae1dSRodney W. Grimes */ 217fb59c426SYoshinobu Inoue if (th == 0) 218df8bae1dSRodney W. Grimes goto present; 219df8bae1dSRodney W. Grimes 220fb59c426SYoshinobu Inoue /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */ 221fb59c426SYoshinobu Inoue MALLOC(te, struct tseg_qent *, sizeof (struct tseg_qent), M_TSEGQ, 222fb59c426SYoshinobu Inoue M_NOWAIT); 223fb59c426SYoshinobu Inoue if (te == NULL) { 224fb59c426SYoshinobu Inoue tcpstat.tcps_rcvmemdrop++; 225fb59c426SYoshinobu Inoue m_freem(m); 226fb59c426SYoshinobu Inoue return (0); 227fb59c426SYoshinobu Inoue } 2286effc713SDoug Rabson 229df8bae1dSRodney W. Grimes /* 230df8bae1dSRodney W. Grimes * Find a segment which begins after this one does. 231df8bae1dSRodney W. Grimes */ 232fb59c426SYoshinobu Inoue LIST_FOREACH(q, &tp->t_segq, tqe_q) { 233fb59c426SYoshinobu Inoue if (SEQ_GT(q->tqe_th->th_seq, th->th_seq)) 234df8bae1dSRodney W. Grimes break; 235fb59c426SYoshinobu Inoue p = q; 236fb59c426SYoshinobu Inoue } 237df8bae1dSRodney W. Grimes 238df8bae1dSRodney W. Grimes /* 239df8bae1dSRodney W. Grimes * If there is a preceding segment, it may provide some of 240df8bae1dSRodney W. Grimes * our data already. If so, drop the data from the incoming 241df8bae1dSRodney W. Grimes * segment. If it provides all of our data, drop us. 242df8bae1dSRodney W. Grimes */ 2436effc713SDoug Rabson if (p != NULL) { 244df8bae1dSRodney W. Grimes register int i; 245df8bae1dSRodney W. Grimes /* conversion to int (in i) handles seq wraparound */ 246fb59c426SYoshinobu Inoue i = p->tqe_th->th_seq + p->tqe_len - th->th_seq; 247df8bae1dSRodney W. Grimes if (i > 0) { 248fb59c426SYoshinobu Inoue if (i >= *tlenp) { 249df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 250fb59c426SYoshinobu Inoue tcpstat.tcps_rcvdupbyte += *tlenp; 251df8bae1dSRodney W. Grimes m_freem(m); 252fb59c426SYoshinobu Inoue FREE(te, M_TSEGQ); 253a0292f23SGarrett Wollman /* 254a0292f23SGarrett Wollman * Try to present any queued data 255a0292f23SGarrett Wollman * at the left window edge to the user. 256a0292f23SGarrett Wollman * This is needed after the 3-WHS 257a0292f23SGarrett Wollman * completes. 258a0292f23SGarrett Wollman */ 259a0292f23SGarrett Wollman goto present; /* ??? */ 260df8bae1dSRodney W. Grimes } 261df8bae1dSRodney W. Grimes m_adj(m, i); 262fb59c426SYoshinobu Inoue *tlenp -= i; 263fb59c426SYoshinobu Inoue th->th_seq += i; 264df8bae1dSRodney W. Grimes } 265df8bae1dSRodney W. Grimes } 266df8bae1dSRodney W. Grimes tcpstat.tcps_rcvoopack++; 267fb59c426SYoshinobu Inoue tcpstat.tcps_rcvoobyte += *tlenp; 268df8bae1dSRodney W. Grimes 269df8bae1dSRodney W. Grimes /* 270df8bae1dSRodney W. Grimes * While we overlap succeeding segments trim them or, 271df8bae1dSRodney W. Grimes * if they are completely covered, dequeue them. 272df8bae1dSRodney W. Grimes */ 2736effc713SDoug Rabson while (q) { 274fb59c426SYoshinobu Inoue register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq; 275df8bae1dSRodney W. Grimes if (i <= 0) 276df8bae1dSRodney W. Grimes break; 277fb59c426SYoshinobu Inoue if (i < q->tqe_len) { 278fb59c426SYoshinobu Inoue q->tqe_th->th_seq += i; 279fb59c426SYoshinobu Inoue q->tqe_len -= i; 280fb59c426SYoshinobu Inoue m_adj(q->tqe_m, i); 281df8bae1dSRodney W. Grimes break; 282df8bae1dSRodney W. Grimes } 2836effc713SDoug Rabson 284fb59c426SYoshinobu Inoue nq = LIST_NEXT(q, tqe_q); 285fb59c426SYoshinobu Inoue LIST_REMOVE(q, tqe_q); 286fb59c426SYoshinobu Inoue m_freem(q->tqe_m); 287fb59c426SYoshinobu Inoue FREE(q, M_TSEGQ); 2886effc713SDoug Rabson q = nq; 289df8bae1dSRodney W. Grimes } 290df8bae1dSRodney W. Grimes 291fb59c426SYoshinobu Inoue /* Insert the new segment queue entry into place. */ 292fb59c426SYoshinobu Inoue te->tqe_m = m; 293fb59c426SYoshinobu Inoue te->tqe_th = th; 294fb59c426SYoshinobu Inoue te->tqe_len = *tlenp; 295fb59c426SYoshinobu Inoue 2966effc713SDoug Rabson if (p == NULL) { 297fb59c426SYoshinobu Inoue LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q); 2986effc713SDoug Rabson } else { 299fb59c426SYoshinobu Inoue LIST_INSERT_AFTER(p, te, tqe_q); 3006effc713SDoug Rabson } 301df8bae1dSRodney W. Grimes 302df8bae1dSRodney W. Grimes present: 303df8bae1dSRodney W. Grimes /* 304df8bae1dSRodney W. Grimes * Present data to user, advancing rcv_nxt through 305df8bae1dSRodney W. Grimes * completed sequence space. 306df8bae1dSRodney W. Grimes */ 307a0292f23SGarrett Wollman if (!TCPS_HAVEESTABLISHED(tp->t_state)) 308df8bae1dSRodney W. Grimes return (0); 309fb59c426SYoshinobu Inoue q = LIST_FIRST(&tp->t_segq); 310fb59c426SYoshinobu Inoue if (!q || q->tqe_th->th_seq != tp->rcv_nxt) 311df8bae1dSRodney W. Grimes return (0); 312df8bae1dSRodney W. Grimes do { 313fb59c426SYoshinobu Inoue tp->rcv_nxt += q->tqe_len; 314fb59c426SYoshinobu Inoue flags = q->tqe_th->th_flags & TH_FIN; 315fb59c426SYoshinobu Inoue nq = LIST_NEXT(q, tqe_q); 316fb59c426SYoshinobu Inoue LIST_REMOVE(q, tqe_q); 317df8bae1dSRodney W. Grimes if (so->so_state & SS_CANTRCVMORE) 318fb59c426SYoshinobu Inoue m_freem(q->tqe_m); 319df8bae1dSRodney W. Grimes else 320fb59c426SYoshinobu Inoue sbappend(&so->so_rcv, q->tqe_m); 321fb59c426SYoshinobu Inoue FREE(q, M_TSEGQ); 3226effc713SDoug Rabson q = nq; 323fb59c426SYoshinobu Inoue } while (q && q->tqe_th->th_seq == tp->rcv_nxt); 324fb59c426SYoshinobu Inoue ND6_HINT(tp); 325df8bae1dSRodney W. Grimes sorwakeup(so); 326df8bae1dSRodney W. Grimes return (flags); 327df8bae1dSRodney W. Grimes } 328df8bae1dSRodney W. Grimes 329df8bae1dSRodney W. Grimes /* 330df8bae1dSRodney W. Grimes * TCP input routine, follows pages 65-76 of the 331df8bae1dSRodney W. Grimes * protocol specification dated September, 1981 very closely. 332df8bae1dSRodney W. Grimes */ 333fb59c426SYoshinobu Inoue #ifdef INET6 334fb59c426SYoshinobu Inoue int 335fb59c426SYoshinobu Inoue tcp6_input(mp, offp, proto) 336fb59c426SYoshinobu Inoue struct mbuf **mp; 337fb59c426SYoshinobu Inoue int *offp, proto; 338fb59c426SYoshinobu Inoue { 339fb59c426SYoshinobu Inoue register struct mbuf *m = *mp; 340fb59c426SYoshinobu Inoue 341fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 342fb59c426SYoshinobu Inoue 343fb59c426SYoshinobu Inoue /* 344fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 345fb59c426SYoshinobu Inoue * better place to put this in? 346fb59c426SYoshinobu Inoue */ 347fb59c426SYoshinobu Inoue if (m->m_flags & M_ANYCAST6) { 348fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 349fb59c426SYoshinobu Inoue 350fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 351fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 352fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 353fb59c426SYoshinobu Inoue return IPPROTO_DONE; 354fb59c426SYoshinobu Inoue } 355fb59c426SYoshinobu Inoue 356fb59c426SYoshinobu Inoue tcp_input(m, *offp, proto); 357fb59c426SYoshinobu Inoue return IPPROTO_DONE; 358fb59c426SYoshinobu Inoue } 359fb59c426SYoshinobu Inoue #endif 360fb59c426SYoshinobu Inoue 361df8bae1dSRodney W. Grimes void 3626a800098SYoshinobu Inoue tcp_input(m, off0, proto) 363df8bae1dSRodney W. Grimes register struct mbuf *m; 3646a800098SYoshinobu Inoue int off0, proto; 365df8bae1dSRodney W. Grimes { 366fb59c426SYoshinobu Inoue register struct tcphdr *th; 367fb59c426SYoshinobu Inoue register struct ip *ip = NULL; 368fb59c426SYoshinobu Inoue register struct ipovly *ipov; 369df8bae1dSRodney W. Grimes register struct inpcb *inp; 370e79adb8eSGarrett Wollman u_char *optp = NULL; 37126f9a767SRodney W. Grimes int optlen = 0; 372df8bae1dSRodney W. Grimes int len, tlen, off; 373fb59c426SYoshinobu Inoue int drop_hdrlen; 374df8bae1dSRodney W. Grimes register struct tcpcb *tp = 0; 375fb59c426SYoshinobu Inoue register int thflags; 37626f9a767SRodney W. Grimes struct socket *so = 0; 377df8bae1dSRodney W. Grimes int todrop, acked, ourfinisacked, needoutput = 0; 378df8bae1dSRodney W. Grimes struct in_addr laddr; 379fb59c426SYoshinobu Inoue #ifdef INET6 380fb59c426SYoshinobu Inoue struct in6_addr laddr6; 381fb59c426SYoshinobu Inoue #endif 382df8bae1dSRodney W. Grimes int dropsocket = 0; 383df8bae1dSRodney W. Grimes int iss = 0; 384a0292f23SGarrett Wollman u_long tiwin; 385a0292f23SGarrett Wollman struct tcpopt to; /* options in this segment */ 386a0292f23SGarrett Wollman struct rmxp_tao *taop; /* pointer to our TAO cache entry */ 387a0292f23SGarrett Wollman struct rmxp_tao tao_noncached; /* in case there's no cached entry */ 388610ee2f9SDavid Greenman #ifdef TCPDEBUG 389610ee2f9SDavid Greenman short ostate = 0; 390610ee2f9SDavid Greenman #endif 391fb59c426SYoshinobu Inoue #ifdef INET6 392fb59c426SYoshinobu Inoue struct ip6_hdr *ip6 = NULL; 393fb59c426SYoshinobu Inoue int isipv6; 394fb59c426SYoshinobu Inoue #endif /* INET6 */ 39509f81a46SBosko Milekic int rstreason = 0; /* For badport_bandlim accounting purposes */ 396df8bae1dSRodney W. Grimes 397fb59c426SYoshinobu Inoue #ifdef INET6 398fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 399fb59c426SYoshinobu Inoue #endif 400a0292f23SGarrett Wollman bzero((char *)&to, sizeof(to)); 401a0292f23SGarrett Wollman 402df8bae1dSRodney W. Grimes tcpstat.tcps_rcvtotal++; 403fb59c426SYoshinobu Inoue 404fb59c426SYoshinobu Inoue #ifdef INET6 405fb59c426SYoshinobu Inoue if (isipv6) { 406fb59c426SYoshinobu Inoue /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */ 407fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 408fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 409fb59c426SYoshinobu Inoue if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 410fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbadsum++; 411fb59c426SYoshinobu Inoue goto drop; 412fb59c426SYoshinobu Inoue } 413fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 414fb59c426SYoshinobu Inoue } else 415fb59c426SYoshinobu Inoue #endif /* INET6 */ 416fb59c426SYoshinobu Inoue { 417df8bae1dSRodney W. Grimes /* 418df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 419df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 420df8bae1dSRodney W. Grimes */ 421fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 422df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 423fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 424fb59c426SYoshinobu Inoue } 425df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 426df8bae1dSRodney W. Grimes if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 427df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 428df8bae1dSRodney W. Grimes return; 429df8bae1dSRodney W. Grimes } 430df8bae1dSRodney W. Grimes } 431fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 432fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 433db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 434db4f9cc7SJonathan Lemon tlen = ip->ip_len; 435df8bae1dSRodney W. Grimes 436db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 437db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 438db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 439db4f9cc7SJonathan Lemon else 440db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 441db4f9cc7SJonathan Lemon ip->ip_dst.s_addr, htonl(m->m_pkthdr.csum_data + 442db4f9cc7SJonathan Lemon ip->ip_len + IPPROTO_TCP)); 443db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 444db4f9cc7SJonathan Lemon } else { 445df8bae1dSRodney W. Grimes /* 446df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 447df8bae1dSRodney W. Grimes */ 448df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 449fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 450fb59c426SYoshinobu Inoue ipov->ih_len = (u_short)tlen; 451fb59c426SYoshinobu Inoue HTONS(ipov->ih_len); 452fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 453db4f9cc7SJonathan Lemon } 454fb59c426SYoshinobu Inoue if (th->th_sum) { 455df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadsum++; 456df8bae1dSRodney W. Grimes goto drop; 457df8bae1dSRodney W. Grimes } 458fb59c426SYoshinobu Inoue #ifdef INET6 459fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 460fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 461fb59c426SYoshinobu Inoue #endif 462fb59c426SYoshinobu Inoue } 463df8bae1dSRodney W. Grimes 464df8bae1dSRodney W. Grimes /* 465df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 466df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 467df8bae1dSRodney W. Grimes */ 468fb59c426SYoshinobu Inoue off = th->th_off << 2; 469df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 470df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadoff++; 471df8bae1dSRodney W. Grimes goto drop; 472df8bae1dSRodney W. Grimes } 473fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 474df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 475fb59c426SYoshinobu Inoue #ifdef INET6 476fb59c426SYoshinobu Inoue if (isipv6) { 477fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 478fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 479fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 480fb59c426SYoshinobu Inoue } else 481fb59c426SYoshinobu Inoue #endif /* INET6 */ 482fb59c426SYoshinobu Inoue { 483df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 484df8bae1dSRodney W. Grimes if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 485df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 486df8bae1dSRodney W. Grimes return; 487df8bae1dSRodney W. Grimes } 488fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 489fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 490fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 491fb59c426SYoshinobu Inoue } 492df8bae1dSRodney W. Grimes } 493df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 494fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 495df8bae1dSRodney W. Grimes } 496fb59c426SYoshinobu Inoue thflags = th->th_flags; 497df8bae1dSRodney W. Grimes 498e46cd3d4SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN 499e46cd3d4SDag-Erling Smørgrav /* 500e46cd3d4SDag-Erling Smørgrav * If the drop_synfin option is enabled, drop all packets with 501e46cd3d4SDag-Erling Smørgrav * both the SYN and FIN bits set. This prevents e.g. nmap from 502e46cd3d4SDag-Erling Smørgrav * identifying the TCP/IP stack. 503e46cd3d4SDag-Erling Smørgrav * 504e46cd3d4SDag-Erling Smørgrav * This is incompatible with RFC1644 extensions (T/TCP). 505e46cd3d4SDag-Erling Smørgrav */ 506fb59c426SYoshinobu Inoue if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN)) 507e46cd3d4SDag-Erling Smørgrav goto drop; 508e46cd3d4SDag-Erling Smørgrav #endif 509e46cd3d4SDag-Erling Smørgrav 510df8bae1dSRodney W. Grimes /* 511df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 512df8bae1dSRodney W. Grimes */ 513fb59c426SYoshinobu Inoue NTOHL(th->th_seq); 514fb59c426SYoshinobu Inoue NTOHL(th->th_ack); 515fb59c426SYoshinobu Inoue NTOHS(th->th_win); 516fb59c426SYoshinobu Inoue NTOHS(th->th_urp); 517df8bae1dSRodney W. Grimes 518df8bae1dSRodney W. Grimes /* 519fb59c426SYoshinobu Inoue * Delay droping TCP, IP headers, IPv6 ext headers, and TCP options, 520fb59c426SYoshinobu Inoue * until after ip6_savecontrol() is called and before other functions 521fb59c426SYoshinobu Inoue * which don't want those proto headers. 522fb59c426SYoshinobu Inoue * Because ip6_savecontrol() is going to parse the mbuf to 523fb59c426SYoshinobu Inoue * search for data to be passed up to user-land, it wants mbuf 524fb59c426SYoshinobu Inoue * parameters to be unchanged. 525755c1f07SAndras Olah */ 526fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 527755c1f07SAndras Olah 528755c1f07SAndras Olah /* 529df8bae1dSRodney W. Grimes * Locate pcb for segment. 530df8bae1dSRodney W. Grimes */ 531df8bae1dSRodney W. Grimes findpcb: 532f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD 533fb59c426SYoshinobu Inoue if (ip_fw_fwd_addr != NULL 534fb59c426SYoshinobu Inoue #ifdef INET6 535fb59c426SYoshinobu Inoue && isipv6 == NULL /* IPv6 support is not yet */ 536fb59c426SYoshinobu Inoue #endif /* INET6 */ 537fb59c426SYoshinobu Inoue ) { 538f9e354dfSJulian Elischer /* 539f9e354dfSJulian Elischer * Diverted. Pretend to be the destination. 540f9e354dfSJulian Elischer * already got one like this? 541f9e354dfSJulian Elischer */ 542fb59c426SYoshinobu Inoue inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, 543fb59c426SYoshinobu Inoue ip->ip_dst, th->th_dport, 0, m->m_pkthdr.rcvif); 544f9e354dfSJulian Elischer if (!inp) { 545f9e354dfSJulian Elischer /* 546f9e354dfSJulian Elischer * No, then it's new. Try find the ambushing socket 547f9e354dfSJulian Elischer */ 548f9e354dfSJulian Elischer if (!ip_fw_fwd_addr->sin_port) { 549fb59c426SYoshinobu Inoue inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, 550fb59c426SYoshinobu Inoue th->th_sport, ip_fw_fwd_addr->sin_addr, 551fb59c426SYoshinobu Inoue th->th_dport, 1, m->m_pkthdr.rcvif); 552f9e354dfSJulian Elischer } else { 553f9e354dfSJulian Elischer inp = in_pcblookup_hash(&tcbinfo, 554fb59c426SYoshinobu Inoue ip->ip_src, th->th_sport, 555f9e354dfSJulian Elischer ip_fw_fwd_addr->sin_addr, 556cfa1ca9dSYoshinobu Inoue ntohs(ip_fw_fwd_addr->sin_port), 1, 557cfa1ca9dSYoshinobu Inoue m->m_pkthdr.rcvif); 558f9e354dfSJulian Elischer } 559f9e354dfSJulian Elischer } 560f9e354dfSJulian Elischer ip_fw_fwd_addr = NULL; 561f9e354dfSJulian Elischer } else 562f9e354dfSJulian Elischer #endif /* IPFIREWALL_FORWARD */ 563fb59c426SYoshinobu Inoue { 564fb59c426SYoshinobu Inoue #ifdef INET6 565fb59c426SYoshinobu Inoue if (isipv6) 566fb59c426SYoshinobu Inoue inp = in6_pcblookup_hash(&tcbinfo, &ip6->ip6_src, th->th_sport, 567fb59c426SYoshinobu Inoue &ip6->ip6_dst, th->th_dport, 1, 568fb59c426SYoshinobu Inoue m->m_pkthdr.rcvif); 569fb59c426SYoshinobu Inoue else 570fb59c426SYoshinobu Inoue #endif /* INET6 */ 571fb59c426SYoshinobu Inoue inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, 572fb59c426SYoshinobu Inoue ip->ip_dst, th->th_dport, 1, m->m_pkthdr.rcvif); 573fb59c426SYoshinobu Inoue } 574f9e354dfSJulian Elischer 575fb59c426SYoshinobu Inoue #ifdef IPSEC 576fb59c426SYoshinobu Inoue #ifdef INET6 577fb59c426SYoshinobu Inoue if (isipv6) { 578fb59c426SYoshinobu Inoue if (inp != NULL && ipsec6_in_reject_so(m, inp->inp_socket)) { 579fb59c426SYoshinobu Inoue ipsec6stat.in_polvio++; 580fb59c426SYoshinobu Inoue goto drop; 581fb59c426SYoshinobu Inoue } 582fb59c426SYoshinobu Inoue } else 583fb59c426SYoshinobu Inoue #endif /* INET6 */ 584fb59c426SYoshinobu Inoue if (inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) { 585fb59c426SYoshinobu Inoue ipsecstat.in_polvio++; 586fb59c426SYoshinobu Inoue goto drop; 587fb59c426SYoshinobu Inoue } 588fb59c426SYoshinobu Inoue #endif /*IPSEC*/ 589df8bae1dSRodney W. Grimes 590df8bae1dSRodney W. Grimes /* 591df8bae1dSRodney W. Grimes * If the state is CLOSED (i.e., TCB does not exist) then 592df8bae1dSRodney W. Grimes * all data in the incoming segment is discarded. 593df8bae1dSRodney W. Grimes * If the TCB exists but is in CLOSED state, it is embryonic, 594df8bae1dSRodney W. Grimes * but should either do a listen or a connect soon. 595df8bae1dSRodney W. Grimes */ 596816a3d83SPoul-Henning Kamp if (inp == NULL) { 5972e4e1b4cSGeoff Rehmet if (log_in_vain) { 598fb59c426SYoshinobu Inoue #ifdef INET6 599fb59c426SYoshinobu Inoue char dbuf[INET6_ADDRSTRLEN], sbuf[INET6_ADDRSTRLEN]; 600fb59c426SYoshinobu Inoue #else /* INET6 */ 601fb59c426SYoshinobu Inoue char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"]; 602fb59c426SYoshinobu Inoue #endif /* INET6 */ 60375cfc95fSAndrey A. Chernov 604fb59c426SYoshinobu Inoue #ifdef INET6 605fb59c426SYoshinobu Inoue if (isipv6) { 606fb59c426SYoshinobu Inoue strcpy(dbuf, ip6_sprintf(&ip6->ip6_dst)); 607fb59c426SYoshinobu Inoue strcpy(sbuf, ip6_sprintf(&ip6->ip6_src)); 608fb59c426SYoshinobu Inoue } else 609fb59c426SYoshinobu Inoue #endif 610fb59c426SYoshinobu Inoue { 611fb59c426SYoshinobu Inoue strcpy(dbuf, inet_ntoa(ip->ip_dst)); 612fb59c426SYoshinobu Inoue strcpy(sbuf, inet_ntoa(ip->ip_src)); 613fb59c426SYoshinobu Inoue } 6142e4e1b4cSGeoff Rehmet switch (log_in_vain) { 6152e4e1b4cSGeoff Rehmet case 1: 616fb59c426SYoshinobu Inoue if(thflags & TH_SYN) 617592071e8SBruce Evans log(LOG_INFO, 618592071e8SBruce Evans "Connection attempt to TCP %s:%d from %s:%d\n", 619fb59c426SYoshinobu Inoue dbuf, ntohs(th->th_dport), 620fb59c426SYoshinobu Inoue sbuf, 621fb59c426SYoshinobu Inoue ntohs(th->th_sport)); 6222e4e1b4cSGeoff Rehmet break; 6232e4e1b4cSGeoff Rehmet case 2: 6242e4e1b4cSGeoff Rehmet log(LOG_INFO, 6252e4e1b4cSGeoff Rehmet "Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n", 626fb59c426SYoshinobu Inoue dbuf, ntohs(th->th_dport), sbuf, 627fb59c426SYoshinobu Inoue ntohs(th->th_sport), thflags); 6282e4e1b4cSGeoff Rehmet break; 6292e4e1b4cSGeoff Rehmet default: 6302e4e1b4cSGeoff Rehmet break; 6312e4e1b4cSGeoff Rehmet } 63275cfc95fSAndrey A. Chernov } 6332e4e1b4cSGeoff Rehmet if (blackhole) { 6342e4e1b4cSGeoff Rehmet switch (blackhole) { 635828b7f40SGeoff Rehmet case 1: 636fb59c426SYoshinobu Inoue if (thflags & TH_SYN) 637828b7f40SGeoff Rehmet goto drop; 638828b7f40SGeoff Rehmet break; 639828b7f40SGeoff Rehmet case 2: 640828b7f40SGeoff Rehmet goto drop; 641828b7f40SGeoff Rehmet default: 642828b7f40SGeoff Rehmet goto drop; 6432e4e1b4cSGeoff Rehmet } 644828b7f40SGeoff Rehmet } 64509f81a46SBosko Milekic rstreason = BANDLIM_RST_NOTOPEN; 646173c0f9fSWarner Losh goto maybedropwithreset; 647816a3d83SPoul-Henning Kamp } 648df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 64909f81a46SBosko Milekic if (tp == 0) { 65009f81a46SBosko Milekic rstreason = BANDLIM_RST_NOTOPEN; 651173c0f9fSWarner Losh goto maybedropwithreset; 65209f81a46SBosko Milekic } 653df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_CLOSED) 654df8bae1dSRodney W. Grimes goto drop; 655df8bae1dSRodney W. Grimes 656df8bae1dSRodney W. Grimes /* Unscale the window into a 32-bit value. */ 657fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) == 0) 658fb59c426SYoshinobu Inoue tiwin = th->th_win << tp->snd_scale; 659df8bae1dSRodney W. Grimes else 660fb59c426SYoshinobu Inoue tiwin = th->th_win; 661fb59c426SYoshinobu Inoue 662fb59c426SYoshinobu Inoue #ifdef INET6 663fb59c426SYoshinobu Inoue /* save packet options if user wanted */ 6644739b807SYoshinobu Inoue if (isipv6 && inp->in6p_flags & INP_CONTROLOPTS) { 665fb59c426SYoshinobu Inoue if (inp->in6p_options) { 666fb59c426SYoshinobu Inoue m_freem(inp->in6p_options); 667fb59c426SYoshinobu Inoue inp->in6p_options = 0; 668fb59c426SYoshinobu Inoue } 669fb59c426SYoshinobu Inoue ip6_savecontrol(inp, &inp->in6p_options, ip6, m); 670fb59c426SYoshinobu Inoue } 6718972cdb1SYoshinobu Inoue /* else, should also do ip_srcroute() here? */ 672fb59c426SYoshinobu Inoue #endif /* INET6 */ 673df8bae1dSRodney W. Grimes 674df8bae1dSRodney W. Grimes so = inp->inp_socket; 675df8bae1dSRodney W. Grimes if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) { 676610ee2f9SDavid Greenman #ifdef TCPDEBUG 677df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 678df8bae1dSRodney W. Grimes ostate = tp->t_state; 679fb59c426SYoshinobu Inoue #ifdef INET6 680fb59c426SYoshinobu Inoue if (isipv6) 681fb59c426SYoshinobu Inoue bcopy((char *)ip6, (char *)tcp_saveipgen, 682fb59c426SYoshinobu Inoue sizeof(*ip6)); 683fb59c426SYoshinobu Inoue else 684fb59c426SYoshinobu Inoue #endif /* INET6 */ 685fb59c426SYoshinobu Inoue bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 686fb59c426SYoshinobu Inoue tcp_savetcp = *th; 687df8bae1dSRodney W. Grimes } 688610ee2f9SDavid Greenman #endif 689df8bae1dSRodney W. Grimes if (so->so_options & SO_ACCEPTCONN) { 690a0292f23SGarrett Wollman register struct tcpcb *tp0 = tp; 6914195b4afSPaul Traina struct socket *so2; 692fb59c426SYoshinobu Inoue #ifdef IPSEC 693fb59c426SYoshinobu Inoue struct socket *oso; 694fb59c426SYoshinobu Inoue #endif 695fb59c426SYoshinobu Inoue #ifdef INET6 696fb59c426SYoshinobu Inoue struct inpcb *oinp = sotoinpcb(so); 697fb59c426SYoshinobu Inoue #endif /* INET6 */ 698fb59c426SYoshinobu Inoue 699fb59c426SYoshinobu Inoue #ifndef IPSEC 700fb59c426SYoshinobu Inoue /* 701fb59c426SYoshinobu Inoue * Current IPsec implementation makes incorrect IPsec 702fb59c426SYoshinobu Inoue * cache if this check is done here. 703fb59c426SYoshinobu Inoue * So delay this until duplicated socket is created. 704fb59c426SYoshinobu Inoue */ 705fb59c426SYoshinobu Inoue if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 7064195b4afSPaul Traina /* 707ebb0cbeaSPaul Traina * Note: dropwithreset makes sure we don't 708ebb0cbeaSPaul Traina * send a RST in response to a RST. 7094195b4afSPaul Traina */ 710fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 711ebb0cbeaSPaul Traina tcpstat.tcps_badsyn++; 712173c0f9fSWarner Losh goto maybedropwithreset; 7134195b4afSPaul Traina } 714ebb0cbeaSPaul Traina goto drop; 715ebb0cbeaSPaul Traina } 716fb59c426SYoshinobu Inoue #endif 717ebb0cbeaSPaul Traina so2 = sonewconn(so, 0); 718ebb0cbeaSPaul Traina if (so2 == 0) { 719ebb0cbeaSPaul Traina tcpstat.tcps_listendrop++; 720ebb0cbeaSPaul Traina so2 = sodropablereq(so); 721a51764a8SPaul Traina if (so2) { 722e7f32693SJayanth Vijayaraghavan if (tcp_lq_overflow) 723e7f32693SJayanth Vijayaraghavan sototcpcb(so2)->t_flags |= 724e7f32693SJayanth Vijayaraghavan TF_LQ_OVERFLOW; 725ebb0cbeaSPaul Traina tcp_drop(sototcpcb(so2), ETIMEDOUT); 726a51764a8SPaul Traina so2 = sonewconn(so, 0); 727a51764a8SPaul Traina } 728a51764a8SPaul Traina if (!so2) 729df8bae1dSRodney W. Grimes goto drop; 7301347f5b8SGuido van Rooij } 731fb59c426SYoshinobu Inoue #ifdef IPSEC 732fb59c426SYoshinobu Inoue oso = so; 733fb59c426SYoshinobu Inoue #endif 7344195b4afSPaul Traina so = so2; 735df8bae1dSRodney W. Grimes /* 736df8bae1dSRodney W. Grimes * This is ugly, but .... 737df8bae1dSRodney W. Grimes * 738df8bae1dSRodney W. Grimes * Mark socket as temporary until we're 739df8bae1dSRodney W. Grimes * committed to keeping it. The code at 740df8bae1dSRodney W. Grimes * ``drop'' and ``dropwithreset'' check the 741df8bae1dSRodney W. Grimes * flag dropsocket to see if the temporary 742df8bae1dSRodney W. Grimes * socket created here should be discarded. 743df8bae1dSRodney W. Grimes * We mark the socket as discardable until 744df8bae1dSRodney W. Grimes * we're committed to it below in TCPS_LISTEN. 745df8bae1dSRodney W. Grimes */ 746df8bae1dSRodney W. Grimes dropsocket++; 747df8bae1dSRodney W. Grimes inp = (struct inpcb *)so->so_pcb; 748fb59c426SYoshinobu Inoue #ifdef INET6 749fb59c426SYoshinobu Inoue if (isipv6) 750fb59c426SYoshinobu Inoue inp->in6p_laddr = ip6->ip6_dst; 751fb59c426SYoshinobu Inoue else { 752fdaf052eSYoshinobu Inoue if ((inp->inp_flags & IN6P_BINDV6ONLY) == 0) { 753fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 754fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 755fb59c426SYoshinobu Inoue } 756fb59c426SYoshinobu Inoue #endif /* INET6 */ 757fb59c426SYoshinobu Inoue inp->inp_laddr = ip->ip_dst; 758fb59c426SYoshinobu Inoue #ifdef INET6 759fb59c426SYoshinobu Inoue } 760fb59c426SYoshinobu Inoue #endif /* INET6 */ 761fb59c426SYoshinobu Inoue inp->inp_lport = th->th_dport; 762c3229e05SDavid Greenman if (in_pcbinshash(inp) != 0) { 763c3229e05SDavid Greenman /* 7645a8c77a8SDavid E. O'Brien * Undo the assignments above if we failed to 7655a8c77a8SDavid E. O'Brien * put the PCB on the hash lists. 766c3229e05SDavid Greenman */ 767fb59c426SYoshinobu Inoue #ifdef INET6 768fb59c426SYoshinobu Inoue if (isipv6) 769fb59c426SYoshinobu Inoue inp->in6p_laddr = in6addr_any; 770fb59c426SYoshinobu Inoue else 771fb59c426SYoshinobu Inoue #endif /* INET6 */ 772c3229e05SDavid Greenman inp->inp_laddr.s_addr = INADDR_ANY; 773c3229e05SDavid Greenman inp->inp_lport = 0; 774c3229e05SDavid Greenman goto drop; 775c3229e05SDavid Greenman } 776fb59c426SYoshinobu Inoue #ifdef IPSEC 777fb59c426SYoshinobu Inoue /* 778fb59c426SYoshinobu Inoue * To avoid creating incorrectly cached IPsec 779fb59c426SYoshinobu Inoue * association, this is need to be done here. 780fb59c426SYoshinobu Inoue * 781fb59c426SYoshinobu Inoue * Subject: (KAME-snap 748) 782fb59c426SYoshinobu Inoue * From: Wayne Knowles <w.knowles@niwa.cri.nz> 783fb59c426SYoshinobu Inoue * ftp://ftp.kame.net/pub/mail-list/snap-users/748 784fb59c426SYoshinobu Inoue */ 785fb59c426SYoshinobu Inoue if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 786fb59c426SYoshinobu Inoue /* 787fb59c426SYoshinobu Inoue * Note: dropwithreset makes sure we don't 788fb59c426SYoshinobu Inoue * send a RST in response to a RST. 789fb59c426SYoshinobu Inoue */ 790fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 791fb59c426SYoshinobu Inoue tcpstat.tcps_badsyn++; 792173c0f9fSWarner Losh goto maybedropwithreset; 793fb59c426SYoshinobu Inoue } 794fb59c426SYoshinobu Inoue goto drop; 795fb59c426SYoshinobu Inoue } 796fb59c426SYoshinobu Inoue #endif 797fb59c426SYoshinobu Inoue #ifdef INET6 798fb59c426SYoshinobu Inoue if (isipv6) { 799fb59c426SYoshinobu Inoue /* 800fb59c426SYoshinobu Inoue * inherit socket options from the listening 801fb59c426SYoshinobu Inoue * socket. 802fb59c426SYoshinobu Inoue */ 803fb59c426SYoshinobu Inoue inp->inp_flags |= 804fb59c426SYoshinobu Inoue oinp->inp_flags & INP_CONTROLOPTS; 805fb59c426SYoshinobu Inoue if (inp->inp_flags & INP_CONTROLOPTS) { 806fb59c426SYoshinobu Inoue if (inp->in6p_options) { 807fb59c426SYoshinobu Inoue m_freem(inp->in6p_options); 808fb59c426SYoshinobu Inoue inp->in6p_options = 0; 809fb59c426SYoshinobu Inoue } 810fb59c426SYoshinobu Inoue ip6_savecontrol(inp, 811fb59c426SYoshinobu Inoue &inp->in6p_options, 812fb59c426SYoshinobu Inoue ip6, m); 813fb59c426SYoshinobu Inoue } 814fb59c426SYoshinobu Inoue } else 815fb59c426SYoshinobu Inoue #endif /* INET6 */ 816df8bae1dSRodney W. Grimes inp->inp_options = ip_srcroute(); 817fb59c426SYoshinobu Inoue #ifdef IPSEC 818fb59c426SYoshinobu Inoue /* copy old policy into new socket's */ 819fb59c426SYoshinobu Inoue if (ipsec_copy_policy(sotoinpcb(oso)->inp_sp, 820fb59c426SYoshinobu Inoue inp->inp_sp)) 821fb59c426SYoshinobu Inoue printf("tcp_input: could not copy policy\n"); 822fb59c426SYoshinobu Inoue #endif 823df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 824df8bae1dSRodney W. Grimes tp->t_state = TCPS_LISTEN; 825a0292f23SGarrett Wollman tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT); 826df8bae1dSRodney W. Grimes 827a0292f23SGarrett Wollman /* Compute proper scaling value from buffer space */ 828df8bae1dSRodney W. Grimes while (tp->request_r_scale < TCP_MAX_WINSHIFT && 8295a8c77a8SDavid E. O'Brien TCP_MAXWIN << tp->request_r_scale < 8305a8c77a8SDavid E. O'Brien so->so_rcv.sb_hiwat) 831df8bae1dSRodney W. Grimes tp->request_r_scale++; 832df8bae1dSRodney W. Grimes } 833df8bae1dSRodney W. Grimes } 834df8bae1dSRodney W. Grimes 835df8bae1dSRodney W. Grimes /* 836df8bae1dSRodney W. Grimes * Segment received on connection. 837df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 838df8bae1dSRodney W. Grimes */ 8399b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 8407ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 8419b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp); 842df8bae1dSRodney W. Grimes 843df8bae1dSRodney W. Grimes /* 844df8bae1dSRodney W. Grimes * Process options if not in LISTEN state, 845df8bae1dSRodney W. Grimes * else do it below (after getting remote address). 846df8bae1dSRodney W. Grimes */ 847f9d5a964SDavid Greenman if (tp->t_state != TCPS_LISTEN) 848fb59c426SYoshinobu Inoue tcp_dooptions(tp, optp, optlen, th, &to); 849df8bae1dSRodney W. Grimes 850df8bae1dSRodney W. Grimes /* 851df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 852df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 853df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 854df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 855df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 856df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 857df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 858df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 859df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 860df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 861df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 862df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 863a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 864a0292f23SGarrett Wollman * Since we check for TCPS_ESTABLISHED above, it can only 865a0292f23SGarrett Wollman * be TH_NEEDSYN. 866df8bae1dSRodney W. Grimes */ 867df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 868fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 869a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 870a0292f23SGarrett Wollman ((to.to_flag & TOF_TS) == 0 || 871a0292f23SGarrett Wollman TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 872a0292f23SGarrett Wollman /* 873a0292f23SGarrett Wollman * Using the CC option is compulsory if once started: 874a0292f23SGarrett Wollman * the segment is OK if no T/TCP was negotiated or 875a0292f23SGarrett Wollman * if the segment has a CC option equal to CCrecv 876a0292f23SGarrett Wollman */ 877a0292f23SGarrett Wollman ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) || 878831a80b0SMatthew Dillon ((to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) && 879fb59c426SYoshinobu Inoue th->th_seq == tp->rcv_nxt && 880df8bae1dSRodney W. Grimes tiwin && tiwin == tp->snd_wnd && 881df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) { 882df8bae1dSRodney W. Grimes 883df8bae1dSRodney W. Grimes /* 884df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 885df8bae1dSRodney W. Grimes * record the timestamp. 886a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 887a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 888df8bae1dSRodney W. Grimes */ 889a0292f23SGarrett Wollman if ((to.to_flag & TOF_TS) != 0 && 890fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 8919b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 892a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 893df8bae1dSRodney W. Grimes } 894df8bae1dSRodney W. Grimes 895fb59c426SYoshinobu Inoue if (tlen == 0) { 896fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 897fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 898233e8c18SGarrett Wollman tp->snd_cwnd >= tp->snd_wnd && 899233e8c18SGarrett Wollman tp->t_dupacks < tcprexmtthresh) { 900df8bae1dSRodney W. Grimes /* 901df8bae1dSRodney W. Grimes * this is a pure ack for outstanding data. 902df8bae1dSRodney W. Grimes */ 903df8bae1dSRodney W. Grimes ++tcpstat.tcps_predack; 9049b8b58e0SJonathan Lemon /* 9059b8b58e0SJonathan Lemon * "bad retransmit" recovery 9069b8b58e0SJonathan Lemon */ 9079b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 9089b8b58e0SJonathan Lemon ticks < tp->t_badrxtwin) { 9099b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 9109b8b58e0SJonathan Lemon tp->snd_ssthresh = 9119b8b58e0SJonathan Lemon tp->snd_ssthresh_prev; 9129b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 9139b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; 9149b8b58e0SJonathan Lemon } 915a0292f23SGarrett Wollman if ((to.to_flag & TOF_TS) != 0) 916a0292f23SGarrett Wollman tcp_xmit_timer(tp, 9179b8b58e0SJonathan Lemon ticks - to.to_tsecr + 1); 9189b8b58e0SJonathan Lemon else if (tp->t_rtttime && 919fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->t_rtseq)) 9209b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 921fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 922df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 923df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 924df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 925fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 926df8bae1dSRodney W. Grimes m_freem(m); 927fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 928df8bae1dSRodney W. Grimes 929df8bae1dSRodney W. Grimes /* 930df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 931df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 932df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 933df8bae1dSRodney W. Grimes * If process is waiting for space, 934df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 935df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 936df8bae1dSRodney W. Grimes * decide between more output or persist. 937df8bae1dSRodney W. Grimes */ 938df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 9399b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 9409b8b58e0SJonathan Lemon else if (!callout_active(tp->tt_persist)) 9419b8b58e0SJonathan Lemon callout_reset(tp->tt_rexmt, 9429b8b58e0SJonathan Lemon tp->t_rxtcur, 9439b8b58e0SJonathan Lemon tcp_timer_rexmt, tp); 944df8bae1dSRodney W. Grimes 945df8bae1dSRodney W. Grimes sowwakeup(so); 946df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 947df8bae1dSRodney W. Grimes (void) tcp_output(tp); 948df8bae1dSRodney W. Grimes return; 949df8bae1dSRodney W. Grimes } 950fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 951fb59c426SYoshinobu Inoue LIST_EMPTY(&tp->t_segq) && 952fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 953df8bae1dSRodney W. Grimes /* 954df8bae1dSRodney W. Grimes * this is a pure, in-sequence data packet 955df8bae1dSRodney W. Grimes * with nothing on the reassembly queue and 956df8bae1dSRodney W. Grimes * we have enough buffer space to take it. 957df8bae1dSRodney W. Grimes */ 958df8bae1dSRodney W. Grimes ++tcpstat.tcps_preddat; 959fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 960df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpack++; 961fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyte += tlen; 962fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 963df8bae1dSRodney W. Grimes /* 964755c1f07SAndras Olah * Add data to socket buffer. 965df8bae1dSRodney W. Grimes */ 966fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 967df8bae1dSRodney W. Grimes sbappend(&so->so_rcv, m); 968df8bae1dSRodney W. Grimes sorwakeup(so); 969f498eeeeSDavid Greenman if (tcp_delack_enabled) { 9709b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 9719b8b58e0SJonathan Lemon tcp_timer_delack, tp); 972f498eeeeSDavid Greenman } else { 973e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 974e612a582SDavid Greenman tcp_output(tp); 975e612a582SDavid Greenman } 976df8bae1dSRodney W. Grimes return; 977df8bae1dSRodney W. Grimes } 978df8bae1dSRodney W. Grimes } 979df8bae1dSRodney W. Grimes 980df8bae1dSRodney W. Grimes /* 981df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 982df8bae1dSRodney W. Grimes * and then do TCP input processing. 983df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 984df8bae1dSRodney W. Grimes * but not less than advertised window. 985df8bae1dSRodney W. Grimes */ 986df8bae1dSRodney W. Grimes { int win; 987df8bae1dSRodney W. Grimes 988df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 989df8bae1dSRodney W. Grimes if (win < 0) 990df8bae1dSRodney W. Grimes win = 0; 99166e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 992df8bae1dSRodney W. Grimes } 993df8bae1dSRodney W. Grimes 994df8bae1dSRodney W. Grimes switch (tp->t_state) { 995df8bae1dSRodney W. Grimes 996df8bae1dSRodney W. Grimes /* 997df8bae1dSRodney W. Grimes * If the state is LISTEN then ignore segment if it contains an RST. 998df8bae1dSRodney W. Grimes * If the segment contains an ACK then it is bad and send a RST. 999df8bae1dSRodney W. Grimes * If it does not contain a SYN then it is not interesting; drop it. 1000764d8cefSBill Fenner * If it is from this socket, drop it, it must be forged. 1001df8bae1dSRodney W. Grimes * Don't bother responding if the destination was a broadcast. 1002df8bae1dSRodney W. Grimes * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 1003df8bae1dSRodney W. Grimes * tp->iss, and send a segment: 1004df8bae1dSRodney W. Grimes * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 1005df8bae1dSRodney W. Grimes * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 1006df8bae1dSRodney W. Grimes * Fill in remote peer address fields if not previously specified. 1007df8bae1dSRodney W. Grimes * Enter SYN_RECEIVED state, and process any other fields of this 1008df8bae1dSRodney W. Grimes * segment in this state. 1009df8bae1dSRodney W. Grimes */ 1010df8bae1dSRodney W. Grimes case TCPS_LISTEN: { 1011df8bae1dSRodney W. Grimes register struct sockaddr_in *sin; 1012fb59c426SYoshinobu Inoue #ifdef INET6 1013fb59c426SYoshinobu Inoue register struct sockaddr_in6 *sin6; 1014fb59c426SYoshinobu Inoue #endif 1015df8bae1dSRodney W. Grimes 1016fb59c426SYoshinobu Inoue if (thflags & TH_RST) 1017df8bae1dSRodney W. Grimes goto drop; 1018fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1019173c0f9fSWarner Losh goto maybedropwithreset; 1020fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) == 0) 1021df8bae1dSRodney W. Grimes goto drop; 1022fb59c426SYoshinobu Inoue if (th->th_dport == th->th_sport) { 1023fb59c426SYoshinobu Inoue #ifdef INET6 1024fb59c426SYoshinobu Inoue if (isipv6) { 1025fb59c426SYoshinobu Inoue if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 1026fb59c426SYoshinobu Inoue &ip6->ip6_src)) 1027764d8cefSBill Fenner goto drop; 1028fb59c426SYoshinobu Inoue } else 1029fb59c426SYoshinobu Inoue #endif /* INET6 */ 1030fb59c426SYoshinobu Inoue if (ip->ip_dst.s_addr == ip->ip_src.s_addr) 1031fb59c426SYoshinobu Inoue goto drop; 1032fb59c426SYoshinobu Inoue } 1033df8bae1dSRodney W. Grimes /* 1034df8bae1dSRodney W. Grimes * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 1035df8bae1dSRodney W. Grimes * in_broadcast() should never return true on a received 1036df8bae1dSRodney W. Grimes * packet with M_BCAST not set. 1037173c0f9fSWarner Losh * 1038173c0f9fSWarner Losh * Packets with a multicast source address should also 1039173c0f9fSWarner Losh * be discarded. 1040df8bae1dSRodney W. Grimes */ 1041fb59c426SYoshinobu Inoue if (m->m_flags & (M_BCAST|M_MCAST)) 1042df8bae1dSRodney W. Grimes goto drop; 1043fb59c426SYoshinobu Inoue #ifdef INET6 1044fb59c426SYoshinobu Inoue if (isipv6) { 1045173c0f9fSWarner Losh if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1046173c0f9fSWarner Losh IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 1047fb59c426SYoshinobu Inoue goto drop; 1048fb59c426SYoshinobu Inoue } else 1049fb59c426SYoshinobu Inoue #endif 1050173c0f9fSWarner Losh if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1051173c0f9fSWarner Losh IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 1052173c0f9fSWarner Losh ip->ip_src.s_addr == htonl(INADDR_BROADCAST)) 1053fb59c426SYoshinobu Inoue goto drop; 1054fb59c426SYoshinobu Inoue #ifdef INET6 1055fb59c426SYoshinobu Inoue if (isipv6) { 1056fb59c426SYoshinobu Inoue MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, 10577cc0979fSDavid Malone M_SONAME, M_NOWAIT | M_ZERO); 1058fb59c426SYoshinobu Inoue if (sin6 == NULL) 1059fb59c426SYoshinobu Inoue goto drop; 1060fb59c426SYoshinobu Inoue sin6->sin6_family = AF_INET6; 1061fb59c426SYoshinobu Inoue sin6->sin6_len = sizeof(*sin6); 1062fb59c426SYoshinobu Inoue sin6->sin6_addr = ip6->ip6_src; 1063fb59c426SYoshinobu Inoue sin6->sin6_port = th->th_sport; 1064fb59c426SYoshinobu Inoue laddr6 = inp->in6p_laddr; 1065fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 1066fb59c426SYoshinobu Inoue inp->in6p_laddr = ip6->ip6_dst; 1067fb59c426SYoshinobu Inoue if (in6_pcbconnect(inp, (struct sockaddr *)sin6, 1068fb59c426SYoshinobu Inoue &proc0)) { 1069fb59c426SYoshinobu Inoue inp->in6p_laddr = laddr6; 1070fb59c426SYoshinobu Inoue FREE(sin6, M_SONAME); 1071fb59c426SYoshinobu Inoue goto drop; 1072fb59c426SYoshinobu Inoue } 1073fb59c426SYoshinobu Inoue FREE(sin6, M_SONAME); 1074fb59c426SYoshinobu Inoue } else 1075fb59c426SYoshinobu Inoue #endif 1076fb59c426SYoshinobu Inoue { 107757bf258eSGarrett Wollman MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, 107857bf258eSGarrett Wollman M_NOWAIT); 107957bf258eSGarrett Wollman if (sin == NULL) 1080df8bae1dSRodney W. Grimes goto drop; 1081df8bae1dSRodney W. Grimes sin->sin_family = AF_INET; 1082df8bae1dSRodney W. Grimes sin->sin_len = sizeof(*sin); 1083fb59c426SYoshinobu Inoue sin->sin_addr = ip->ip_src; 1084fb59c426SYoshinobu Inoue sin->sin_port = th->th_sport; 1085df8bae1dSRodney W. Grimes bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero)); 1086df8bae1dSRodney W. Grimes laddr = inp->inp_laddr; 1087df8bae1dSRodney W. Grimes if (inp->inp_laddr.s_addr == INADDR_ANY) 1088fb59c426SYoshinobu Inoue inp->inp_laddr = ip->ip_dst; 108957bf258eSGarrett Wollman if (in_pcbconnect(inp, (struct sockaddr *)sin, &proc0)) { 1090df8bae1dSRodney W. Grimes inp->inp_laddr = laddr; 109157bf258eSGarrett Wollman FREE(sin, M_SONAME); 1092df8bae1dSRodney W. Grimes goto drop; 1093df8bae1dSRodney W. Grimes } 109457bf258eSGarrett Wollman FREE(sin, M_SONAME); 1095fb59c426SYoshinobu Inoue } 1096df8bae1dSRodney W. Grimes tp->t_template = tcp_template(tp); 1097df8bae1dSRodney W. Grimes if (tp->t_template == 0) { 1098df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ENOBUFS); 1099df8bae1dSRodney W. Grimes dropsocket = 0; /* socket is already gone */ 1100df8bae1dSRodney W. Grimes goto drop; 1101df8bae1dSRodney W. Grimes } 1102a0292f23SGarrett Wollman if ((taop = tcp_gettaocache(inp)) == NULL) { 1103a0292f23SGarrett Wollman taop = &tao_noncached; 1104a0292f23SGarrett Wollman bzero(taop, sizeof(*taop)); 1105a0292f23SGarrett Wollman } 1106fb59c426SYoshinobu Inoue tcp_dooptions(tp, optp, optlen, th, &to); 1107df8bae1dSRodney W. Grimes if (iss) 1108df8bae1dSRodney W. Grimes tp->iss = iss; 1109df8bae1dSRodney W. Grimes else 1110df8bae1dSRodney W. Grimes tp->iss = tcp_iss; 1111e79adb8eSGarrett Wollman tcp_iss += TCP_ISSINCR/4; 1112fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1113df8bae1dSRodney W. Grimes tcp_sendseqinit(tp); 1114df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 111546f58482SJonathan Lemon tp->snd_recover = tp->snd_una; 1116a0292f23SGarrett Wollman /* 1117a0292f23SGarrett Wollman * Initialization of the tcpcb for transaction; 1118a0292f23SGarrett Wollman * set SND.WND = SEG.WND, 1119a0292f23SGarrett Wollman * initialize CCsend and CCrecv. 1120a0292f23SGarrett Wollman */ 1121a0292f23SGarrett Wollman tp->snd_wnd = tiwin; /* initial send-window */ 1122a0292f23SGarrett Wollman tp->cc_send = CC_INC(tcp_ccgen); 1123a0292f23SGarrett Wollman tp->cc_recv = to.to_cc; 1124a0292f23SGarrett Wollman /* 1125a0292f23SGarrett Wollman * Perform TAO test on incoming CC (SEG.CC) option, if any. 1126a0292f23SGarrett Wollman * - compare SEG.CC against cached CC from the same host, 1127a0292f23SGarrett Wollman * if any. 1128a0292f23SGarrett Wollman * - if SEG.CC > chached value, SYN must be new and is accepted 1129a0292f23SGarrett Wollman * immediately: save new CC in the cache, mark the socket 1130a0292f23SGarrett Wollman * connected, enter ESTABLISHED state, turn on flag to 1131a0292f23SGarrett Wollman * send a SYN in the next segment. 1132a0292f23SGarrett Wollman * A virtual advertised window is set in rcv_adv to 1133a0292f23SGarrett Wollman * initialize SWS prevention. Then enter normal segment 1134a0292f23SGarrett Wollman * processing: drop SYN, process data and FIN. 1135a0292f23SGarrett Wollman * - otherwise do a normal 3-way handshake. 1136a0292f23SGarrett Wollman */ 1137a0292f23SGarrett Wollman if ((to.to_flag & TOF_CC) != 0) { 1138068373b6SGuido van Rooij if (((tp->t_flags & TF_NOPUSH) != 0) && 113911ad4550SGuido van Rooij taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) { 114011ad4550SGuido van Rooij 1141a0292f23SGarrett Wollman taop->tao_cc = to.to_cc; 11429b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1143a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1144a0292f23SGarrett Wollman 1145a0292f23SGarrett Wollman /* 1146a0292f23SGarrett Wollman * If there is a FIN, or if there is data and the 1147a0292f23SGarrett Wollman * connection is local, then delay SYN,ACK(SYN) in 1148a0292f23SGarrett Wollman * the hope of piggy-backing it on a response 1149a0292f23SGarrett Wollman * segment. Otherwise must send ACK now in case 1150a0292f23SGarrett Wollman * the other side is slow starting. 1151a0292f23SGarrett Wollman */ 1152fb59c426SYoshinobu Inoue if (tcp_delack_enabled && ((thflags & TH_FIN) || 1153fb59c426SYoshinobu Inoue (tlen != 0 && 1154fb59c426SYoshinobu Inoue #ifdef INET6 1155fb59c426SYoshinobu Inoue ((isipv6 && in6_localaddr(&inp->in6p_faddr)) 1156fb59c426SYoshinobu Inoue || 1157fb59c426SYoshinobu Inoue (!isipv6 && 1158fb59c426SYoshinobu Inoue #endif 1159fb59c426SYoshinobu Inoue in_localaddr(inp->inp_faddr) 1160fb59c426SYoshinobu Inoue #ifdef INET6 1161fb59c426SYoshinobu Inoue )) 1162fb59c426SYoshinobu Inoue #endif 1163fb59c426SYoshinobu Inoue ))) { 11649b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 11659b8b58e0SJonathan Lemon tcp_timer_delack, tp); 11669b8b58e0SJonathan Lemon tp->t_flags |= TF_NEEDSYN; 11679b8b58e0SJonathan Lemon } else 1168a0292f23SGarrett Wollman tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 116907e43e10SAndras Olah 117007e43e10SAndras Olah /* 117107e43e10SAndras Olah * Limit the `virtual advertised window' to TCP_MAXWIN 117207e43e10SAndras Olah * here. Even if we requested window scaling, it will 117307e43e10SAndras Olah * become effective only later when our SYN is acked. 117407e43e10SAndras Olah */ 117507e43e10SAndras Olah tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN); 1176a0292f23SGarrett Wollman tcpstat.tcps_connects++; 1177a0292f23SGarrett Wollman soisconnected(so); 11789b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepinit, 11799b8b58e0SJonathan Lemon tcp_timer_keep, tp); 1180a0292f23SGarrett Wollman dropsocket = 0; /* committed to socket */ 1181a0292f23SGarrett Wollman tcpstat.tcps_accepts++; 1182a0292f23SGarrett Wollman goto trimthenstep6; 1183a0292f23SGarrett Wollman } 1184a0292f23SGarrett Wollman /* else do standard 3-way handshake */ 1185a0292f23SGarrett Wollman } else { 1186a0292f23SGarrett Wollman /* 1187a0292f23SGarrett Wollman * No CC option, but maybe CC.NEW: 1188a0292f23SGarrett Wollman * invalidate cached value. 1189a0292f23SGarrett Wollman */ 1190a0292f23SGarrett Wollman taop->tao_cc = 0; 1191a0292f23SGarrett Wollman } 1192a0292f23SGarrett Wollman /* 1193a0292f23SGarrett Wollman * TAO test failed or there was no CC option, 1194a0292f23SGarrett Wollman * do a standard 3-way handshake. 1195a0292f23SGarrett Wollman */ 1196df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1197df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 11989b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 1199df8bae1dSRodney W. Grimes dropsocket = 0; /* committed to socket */ 1200df8bae1dSRodney W. Grimes tcpstat.tcps_accepts++; 1201df8bae1dSRodney W. Grimes goto trimthenstep6; 1202df8bae1dSRodney W. Grimes } 1203df8bae1dSRodney W. Grimes 1204df8bae1dSRodney W. Grimes /* 1205764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1206764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1207764d8cefSBill Fenner */ 1208764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1209fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1210fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1211fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) 1212173c0f9fSWarner Losh goto maybedropwithreset; 1213764d8cefSBill Fenner break; 1214764d8cefSBill Fenner 1215764d8cefSBill Fenner /* 1216df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1217df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1218df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1219df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1220df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1221df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1222df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1223df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1224df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1225df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1226df8bae1dSRodney W. Grimes */ 1227df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1228a0292f23SGarrett Wollman if ((taop = tcp_gettaocache(inp)) == NULL) { 1229a0292f23SGarrett Wollman taop = &tao_noncached; 1230a0292f23SGarrett Wollman bzero(taop, sizeof(*taop)); 1231a0292f23SGarrett Wollman } 1232a0292f23SGarrett Wollman 1233fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1234fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1235fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1236a0292f23SGarrett Wollman /* 1237a0292f23SGarrett Wollman * If we have a cached CCsent for the remote host, 1238a0292f23SGarrett Wollman * hence we haven't just crashed and restarted, 1239a0292f23SGarrett Wollman * do not send a RST. This may be a retransmission 1240a0292f23SGarrett Wollman * from the other side after our earlier ACK was lost. 1241a0292f23SGarrett Wollman * Our new SYN, when it arrives, will serve as the 1242a0292f23SGarrett Wollman * needed ACK. 1243a0292f23SGarrett Wollman */ 1244a0292f23SGarrett Wollman if (taop->tao_ccsent != 0) 1245a0292f23SGarrett Wollman goto drop; 1246a0292f23SGarrett Wollman else 1247a0292f23SGarrett Wollman goto dropwithreset; 1248a0292f23SGarrett Wollman } 1249fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 1250fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1251df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 1252df8bae1dSRodney W. Grimes goto drop; 1253df8bae1dSRodney W. Grimes } 1254fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) == 0) 1255df8bae1dSRodney W. Grimes goto drop; 1256fb59c426SYoshinobu Inoue tp->snd_wnd = th->th_win; /* initial send window */ 1257a0292f23SGarrett Wollman tp->cc_recv = to.to_cc; /* foreign CC */ 1258a0292f23SGarrett Wollman 1259fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1260df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1261fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1262a0292f23SGarrett Wollman /* 1263a0292f23SGarrett Wollman * Our SYN was acked. If segment contains CC.ECHO 1264a0292f23SGarrett Wollman * option, check it to make sure this segment really 1265a0292f23SGarrett Wollman * matches our SYN. If not, just drop it as old 1266a0292f23SGarrett Wollman * duplicate, but send an RST if we're still playing 1267026650e5SBill Fenner * by the old rules. If no CC.ECHO option, make sure 1268026650e5SBill Fenner * we don't get fooled into using T/TCP. 1269a0292f23SGarrett Wollman */ 1270026650e5SBill Fenner if (to.to_flag & TOF_CCECHO) { 1271dfd5dee1SPeter Wemm if (tp->cc_send != to.to_ccecho) { 1272a0292f23SGarrett Wollman if (taop->tao_ccsent != 0) 1273a0292f23SGarrett Wollman goto drop; 1274a0292f23SGarrett Wollman else 1275a0292f23SGarrett Wollman goto dropwithreset; 1276dfd5dee1SPeter Wemm } 1277026650e5SBill Fenner } else 1278026650e5SBill Fenner tp->t_flags &= ~TF_RCVD_CC; 1279845799c1SAndras Olah tcpstat.tcps_connects++; 1280845799c1SAndras Olah soisconnected(so); 1281845799c1SAndras Olah /* Do window scaling on this connection? */ 1282845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1283845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1284845799c1SAndras Olah tp->snd_scale = tp->requested_s_scale; 1285845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1286845799c1SAndras Olah } 1287a0292f23SGarrett Wollman /* Segment is acceptable, update cache if undefined. */ 1288a0292f23SGarrett Wollman if (taop->tao_ccsent == 0) 1289a0292f23SGarrett Wollman taop->tao_ccsent = to.to_ccecho; 1290a0292f23SGarrett Wollman 1291a0292f23SGarrett Wollman tp->rcv_adv += tp->rcv_wnd; 1292a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1293a0292f23SGarrett Wollman /* 1294a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1295a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1296a0292f23SGarrett Wollman */ 1297fb59c426SYoshinobu Inoue if (tcp_delack_enabled && tlen != 0) 12989b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 12999b8b58e0SJonathan Lemon tcp_timer_delack, tp); 1300a0292f23SGarrett Wollman else 1301a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1302a0292f23SGarrett Wollman /* 1303a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1304a0292f23SGarrett Wollman * Transitions: 1305a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1306a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1307a0292f23SGarrett Wollman */ 13089b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1309a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1310a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1311a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1312fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 13137ff19458SPaul Traina } else { 1314a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 13159b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, 13169b8b58e0SJonathan Lemon tcp_timer_keep, tp); 13177ff19458SPaul Traina } 1318a0292f23SGarrett Wollman } else { 1319a0292f23SGarrett Wollman /* 1320a0292f23SGarrett Wollman * Received initial SYN in SYN-SENT[*] state => simul- 1321a0292f23SGarrett Wollman * taneous open. If segment contains CC option and there is 1322a0292f23SGarrett Wollman * a cached CC, apply TAO test; if it succeeds, connection is 1323a0292f23SGarrett Wollman * half-synchronized. Otherwise, do 3-way handshake: 1324a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1325a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1326a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1327a0292f23SGarrett Wollman */ 1328a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 13299b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 1330a0292f23SGarrett Wollman if (to.to_flag & TOF_CC) { 1331a0292f23SGarrett Wollman if (taop->tao_cc != 0 && 1332a0292f23SGarrett Wollman CC_GT(to.to_cc, taop->tao_cc)) { 1333a0292f23SGarrett Wollman /* 1334a0292f23SGarrett Wollman * update cache and make transition: 1335a0292f23SGarrett Wollman * SYN-SENT -> ESTABLISHED* 1336a0292f23SGarrett Wollman * SYN-SENT* -> FIN-WAIT-1* 1337a0292f23SGarrett Wollman */ 1338a0292f23SGarrett Wollman taop->tao_cc = to.to_cc; 13399b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1340a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1341a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1342a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 13437ff19458SPaul Traina } else { 1344a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 13459b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, 13469b8b58e0SJonathan Lemon tcp_keepidle, 13479b8b58e0SJonathan Lemon tcp_timer_keep, 13489b8b58e0SJonathan Lemon tp); 13497ff19458SPaul Traina } 1350a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDSYN; 1351df8bae1dSRodney W. Grimes } else 1352df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1353a0292f23SGarrett Wollman } else { 1354a0292f23SGarrett Wollman /* CC.NEW or no option => invalidate cache */ 1355a0292f23SGarrett Wollman taop->tao_cc = 0; 1356a0292f23SGarrett Wollman tp->t_state = TCPS_SYN_RECEIVED; 1357a0292f23SGarrett Wollman } 1358a0292f23SGarrett Wollman } 1359df8bae1dSRodney W. Grimes 1360df8bae1dSRodney W. Grimes trimthenstep6: 1361df8bae1dSRodney W. Grimes /* 1362fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1363df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1364df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1365df8bae1dSRodney W. Grimes */ 1366fb59c426SYoshinobu Inoue th->th_seq++; 1367fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1368fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1369df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1370fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1371fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1372df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1373df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1374df8bae1dSRodney W. Grimes } 1375fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1376fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 1377a0292f23SGarrett Wollman /* 1378a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1379a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1380a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1381a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1382a0292f23SGarrett Wollman * Otherwise, goto step 6. 1383a0292f23SGarrett Wollman */ 1384fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1385a0292f23SGarrett Wollman goto process_ACK; 1386df8bae1dSRodney W. Grimes goto step6; 1387a0292f23SGarrett Wollman /* 1388a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1389a0292f23SGarrett Wollman * if segment contains a SYN and CC [not CC.NEW] option: 1390a0292f23SGarrett Wollman * if state == TIME_WAIT and connection duration > MSL, 1391a0292f23SGarrett Wollman * drop packet and send RST; 1392a0292f23SGarrett Wollman * 1393a0292f23SGarrett Wollman * if SEG.CC > CCrecv then is new SYN, and can implicitly 1394a0292f23SGarrett Wollman * ack the FIN (and data) in retransmission queue. 1395a0292f23SGarrett Wollman * Complete close and delete TCPCB. Then reprocess 1396a0292f23SGarrett Wollman * segment, hoping to find new TCPCB in LISTEN state; 1397a0292f23SGarrett Wollman * 1398a0292f23SGarrett Wollman * else must be old SYN; drop it. 1399a0292f23SGarrett Wollman * else do normal processing. 1400a0292f23SGarrett Wollman */ 1401a0292f23SGarrett Wollman case TCPS_LAST_ACK: 1402a0292f23SGarrett Wollman case TCPS_CLOSING: 1403a0292f23SGarrett Wollman case TCPS_TIME_WAIT: 1404fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) && 1405a0292f23SGarrett Wollman (to.to_flag & TOF_CC) && tp->cc_recv != 0) { 1406a0292f23SGarrett Wollman if (tp->t_state == TCPS_TIME_WAIT && 14079b8b58e0SJonathan Lemon (ticks - tp->t_starttime) > tcp_msl) 1408a0292f23SGarrett Wollman goto dropwithreset; 1409a0292f23SGarrett Wollman if (CC_GT(to.to_cc, tp->cc_recv)) { 1410a0292f23SGarrett Wollman tp = tcp_close(tp); 1411a0292f23SGarrett Wollman goto findpcb; 1412a0292f23SGarrett Wollman } 1413a0292f23SGarrett Wollman else 1414a0292f23SGarrett Wollman goto drop; 1415a0292f23SGarrett Wollman } 1416a0292f23SGarrett Wollman break; /* continue normal processing */ 1417df8bae1dSRodney W. Grimes } 1418df8bae1dSRodney W. Grimes 1419df8bae1dSRodney W. Grimes /* 1420df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 142180ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 142280ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 142380ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 142480ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 142580ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 142680ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 142780ab7c0eSGarrett Wollman * killing a connection). 142880ab7c0eSGarrett Wollman * Then check timestamp, if present. 1429a0292f23SGarrett Wollman * Then check the connection count, if present. 1430df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 1431df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 1432df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 1433df8bae1dSRodney W. Grimes * 143480ab7c0eSGarrett Wollman * 143580ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 143680ab7c0eSGarrett Wollman * if this is a valid reset segment. 143780ab7c0eSGarrett Wollman * RFC 793 page 37: 143880ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 143980ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 144080ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 144180ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 144280ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 14431a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 14441a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 14451a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 14461a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 144780ab7c0eSGarrett Wollman * If we have multiple segments in flight, the intial reset 144880ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 144980ab7c0eSGarrett Wollman * but they will eventually catch up. 145080ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 145180ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 145280ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 145380ab7c0eSGarrett Wollman * 145480ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 145580ab7c0eSGarrett Wollman * 145680ab7c0eSGarrett Wollman * DISCUSSION 145780ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 145880ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 145980ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 146080ab7c0eSGarrett Wollman * data. 146180ab7c0eSGarrett Wollman * 146280ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 146380ab7c0eSGarrett Wollman * the state: 146480ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 146580ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 146680ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 146780ab7c0eSGarrett Wollman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 146880ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 1469e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 147080ab7c0eSGarrett Wollman * Close the tcb. 1471e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 147280ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 147380ab7c0eSGarrett Wollman * RFC 1337. 147480ab7c0eSGarrett Wollman */ 1475fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 1476fb59c426SYoshinobu Inoue if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 1477fb59c426SYoshinobu Inoue SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 147880ab7c0eSGarrett Wollman switch (tp->t_state) { 147980ab7c0eSGarrett Wollman 148080ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 148180ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 148280ab7c0eSGarrett Wollman goto close; 148380ab7c0eSGarrett Wollman 148480ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 148580ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 148680ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 148780ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 148880ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 148980ab7c0eSGarrett Wollman close: 149080ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 149180ab7c0eSGarrett Wollman tcpstat.tcps_drops++; 149280ab7c0eSGarrett Wollman tp = tcp_close(tp); 149380ab7c0eSGarrett Wollman break; 149480ab7c0eSGarrett Wollman 149580ab7c0eSGarrett Wollman case TCPS_CLOSING: 149680ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 149780ab7c0eSGarrett Wollman tp = tcp_close(tp); 149880ab7c0eSGarrett Wollman break; 149980ab7c0eSGarrett Wollman 150080ab7c0eSGarrett Wollman case TCPS_TIME_WAIT: 150180ab7c0eSGarrett Wollman break; 150280ab7c0eSGarrett Wollman } 150380ab7c0eSGarrett Wollman } 150480ab7c0eSGarrett Wollman goto drop; 150580ab7c0eSGarrett Wollman } 150680ab7c0eSGarrett Wollman 150780ab7c0eSGarrett Wollman /* 1508df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 1509df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 1510df8bae1dSRodney W. Grimes */ 151180ab7c0eSGarrett Wollman if ((to.to_flag & TOF_TS) != 0 && tp->ts_recent && 151280ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1513df8bae1dSRodney W. Grimes 1514df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 15159b8b58e0SJonathan Lemon if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1516df8bae1dSRodney W. Grimes /* 1517df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 1518df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 1519df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 1520df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 1521df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 1522df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 1523df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 1524df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 1525df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 1526df8bae1dSRodney W. Grimes */ 1527df8bae1dSRodney W. Grimes tp->ts_recent = 0; 1528df8bae1dSRodney W. Grimes } else { 1529df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 1530fb59c426SYoshinobu Inoue tcpstat.tcps_rcvdupbyte += tlen; 1531df8bae1dSRodney W. Grimes tcpstat.tcps_pawsdrop++; 1532df8bae1dSRodney W. Grimes goto dropafterack; 1533df8bae1dSRodney W. Grimes } 1534df8bae1dSRodney W. Grimes } 1535df8bae1dSRodney W. Grimes 1536a0292f23SGarrett Wollman /* 1537a0292f23SGarrett Wollman * T/TCP mechanism 1538a0292f23SGarrett Wollman * If T/TCP was negotiated and the segment doesn't have CC, 1539dc733423SDag-Erling Smørgrav * or if its CC is wrong then drop the segment. 1540a0292f23SGarrett Wollman * RST segments do not have to comply with this. 1541a0292f23SGarrett Wollman */ 1542a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) && 154380ab7c0eSGarrett Wollman ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc)) 1544a0292f23SGarrett Wollman goto dropafterack; 1545a0292f23SGarrett Wollman 154680ab7c0eSGarrett Wollman /* 154780ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 154880ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 154980ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 155080ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 155180ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 155280ab7c0eSGarrett Wollman */ 1553fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) 1554173c0f9fSWarner Losh goto maybedropwithreset; 155580ab7c0eSGarrett Wollman 1556fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 1557df8bae1dSRodney W. Grimes if (todrop > 0) { 1558fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1559fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 1560fb59c426SYoshinobu Inoue th->th_seq++; 1561fb59c426SYoshinobu Inoue if (th->th_urp > 1) 1562fb59c426SYoshinobu Inoue th->th_urp--; 1563df8bae1dSRodney W. Grimes else 1564fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1565df8bae1dSRodney W. Grimes todrop--; 1566df8bae1dSRodney W. Grimes } 1567df8bae1dSRodney W. Grimes /* 1568dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 1569df8bae1dSRodney W. Grimes */ 1570fb59c426SYoshinobu Inoue if (todrop > tlen 1571fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1572dac20301SGarrett Wollman /* 1573dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 1574dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 1575dac20301SGarrett Wollman * of sequence; drop it. 1576dac20301SGarrett Wollman */ 1577fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1578dac20301SGarrett Wollman 1579df8bae1dSRodney W. Grimes /* 1580dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 1581dac20301SGarrett Wollman * But keep on processing for RST or ACK. 1582df8bae1dSRodney W. Grimes */ 1583dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1584fb59c426SYoshinobu Inoue todrop = tlen; 1585dac20301SGarrett Wollman tcpstat.tcps_rcvduppack++; 1586dac20301SGarrett Wollman tcpstat.tcps_rcvdupbyte += todrop; 1587df8bae1dSRodney W. Grimes } else { 1588df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartduppack++; 1589df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartdupbyte += todrop; 1590df8bae1dSRodney W. Grimes } 1591fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 1592fb59c426SYoshinobu Inoue th->th_seq += todrop; 1593fb59c426SYoshinobu Inoue tlen -= todrop; 1594fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 1595fb59c426SYoshinobu Inoue th->th_urp -= todrop; 1596df8bae1dSRodney W. Grimes else { 1597fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1598fb59c426SYoshinobu Inoue th->th_urp = 0; 1599df8bae1dSRodney W. Grimes } 1600df8bae1dSRodney W. Grimes } 1601df8bae1dSRodney W. Grimes 1602df8bae1dSRodney W. Grimes /* 1603df8bae1dSRodney W. Grimes * If new data are received on a connection after the 1604df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 1605df8bae1dSRodney W. Grimes */ 1606df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 1607fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1608df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1609df8bae1dSRodney W. Grimes tcpstat.tcps_rcvafterclose++; 1610df8bae1dSRodney W. Grimes goto dropwithreset; 1611df8bae1dSRodney W. Grimes } 1612df8bae1dSRodney W. Grimes 1613df8bae1dSRodney W. Grimes /* 1614df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 1615df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 1616df8bae1dSRodney W. Grimes */ 1617fb59c426SYoshinobu Inoue todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd); 1618df8bae1dSRodney W. Grimes if (todrop > 0) { 1619df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1620fb59c426SYoshinobu Inoue if (todrop >= tlen) { 1621fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyteafterwin += tlen; 1622df8bae1dSRodney W. Grimes /* 1623df8bae1dSRodney W. Grimes * If a new connection request is received 1624df8bae1dSRodney W. Grimes * while in TIME_WAIT, drop the old connection 1625df8bae1dSRodney W. Grimes * and start over if the sequence numbers 1626df8bae1dSRodney W. Grimes * are above the previous ones. 1627df8bae1dSRodney W. Grimes */ 1628fb59c426SYoshinobu Inoue if (thflags & TH_SYN && 1629df8bae1dSRodney W. Grimes tp->t_state == TCPS_TIME_WAIT && 1630fb59c426SYoshinobu Inoue SEQ_GT(th->th_seq, tp->rcv_nxt)) { 163151b7b337SBill Fenner iss = tp->snd_nxt + TCP_ISSINCR; 1632df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1633df8bae1dSRodney W. Grimes goto findpcb; 1634df8bae1dSRodney W. Grimes } 1635df8bae1dSRodney W. Grimes /* 1636df8bae1dSRodney W. Grimes * If window is closed can only take segments at 1637df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 1638df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 1639df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 1640df8bae1dSRodney W. Grimes * and ack. 1641df8bae1dSRodney W. Grimes */ 1642fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1643df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1644df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinprobe++; 1645df8bae1dSRodney W. Grimes } else 1646df8bae1dSRodney W. Grimes goto dropafterack; 1647df8bae1dSRodney W. Grimes } else 1648df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1649df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1650fb59c426SYoshinobu Inoue tlen -= todrop; 1651fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 1652df8bae1dSRodney W. Grimes } 1653df8bae1dSRodney W. Grimes 1654df8bae1dSRodney W. Grimes /* 1655df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1656df8bae1dSRodney W. Grimes * record its timestamp. 1657a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1658a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1659df8bae1dSRodney W. Grimes */ 1660a0292f23SGarrett Wollman if ((to.to_flag & TOF_TS) != 0 && 1661fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 16629b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1663a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1664df8bae1dSRodney W. Grimes } 1665df8bae1dSRodney W. Grimes 1666df8bae1dSRodney W. Grimes /* 1667df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 1668df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 1669df8bae1dSRodney W. Grimes */ 1670fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1671df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 1672df8bae1dSRodney W. Grimes goto dropwithreset; 1673df8bae1dSRodney W. Grimes } 1674df8bae1dSRodney W. Grimes 1675a0292f23SGarrett Wollman /* 1676a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1677a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 1678a0292f23SGarrett Wollman * later processing; else drop segment and return. 1679a0292f23SGarrett Wollman */ 1680fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 1681a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 1682a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 1683a0292f23SGarrett Wollman goto step6; 1684a0292f23SGarrett Wollman else 1685a0292f23SGarrett Wollman goto drop; 1686a0292f23SGarrett Wollman } 1687df8bae1dSRodney W. Grimes 1688df8bae1dSRodney W. Grimes /* 1689df8bae1dSRodney W. Grimes * Ack processing. 1690df8bae1dSRodney W. Grimes */ 1691df8bae1dSRodney W. Grimes switch (tp->t_state) { 1692df8bae1dSRodney W. Grimes 1693df8bae1dSRodney W. Grimes /* 1694764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1695764d8cefSBill Fenner * ESTABLISHED state and continue processing. 1696764d8cefSBill Fenner * The ACK was checked above. 1697df8bae1dSRodney W. Grimes */ 1698df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1699a0292f23SGarrett Wollman 1700df8bae1dSRodney W. Grimes tcpstat.tcps_connects++; 1701df8bae1dSRodney W. Grimes soisconnected(so); 1702df8bae1dSRodney W. Grimes /* Do window scaling? */ 1703df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1704df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1705df8bae1dSRodney W. Grimes tp->snd_scale = tp->requested_s_scale; 1706df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 1707df8bae1dSRodney W. Grimes } 1708a0292f23SGarrett Wollman /* 1709a0292f23SGarrett Wollman * Upon successful completion of 3-way handshake, 1710a0292f23SGarrett Wollman * update cache.CC if it was undefined, pass any queued 1711a0292f23SGarrett Wollman * data to the user, and advance state appropriately. 1712a0292f23SGarrett Wollman */ 1713a0292f23SGarrett Wollman if ((taop = tcp_gettaocache(inp)) != NULL && 1714a0292f23SGarrett Wollman taop->tao_cc == 0) 1715a0292f23SGarrett Wollman taop->tao_cc = tp->cc_recv; 1716a0292f23SGarrett Wollman 1717a0292f23SGarrett Wollman /* 1718a0292f23SGarrett Wollman * Make transitions: 1719a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 1720a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 1721a0292f23SGarrett Wollman */ 17229b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1723a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1724a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1725a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 17267ff19458SPaul Traina } else { 1727a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 17289b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, 17299b8b58e0SJonathan Lemon tcp_timer_keep, tp); 17307ff19458SPaul Traina } 1731a0292f23SGarrett Wollman /* 1732a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 1733a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 1734a0292f23SGarrett Wollman */ 1735fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 1736fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1737a0292f23SGarrett Wollman (struct mbuf *)0); 1738fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1739df8bae1dSRodney W. Grimes /* fall into ... */ 1740df8bae1dSRodney W. Grimes 1741df8bae1dSRodney W. Grimes /* 1742df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1743df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 1744fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 1745fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 1746df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 1747df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 1748df8bae1dSRodney W. Grimes */ 1749df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1750df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1751df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 1752df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1753df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1754df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 1755df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 1756df8bae1dSRodney W. Grimes 1757fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1758fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 1759df8bae1dSRodney W. Grimes tcpstat.tcps_rcvdupack++; 1760df8bae1dSRodney W. Grimes /* 1761df8bae1dSRodney W. Grimes * If we have outstanding data (other than 1762df8bae1dSRodney W. Grimes * a window probe), this is a completely 1763df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 1764df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 1765df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 1766df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 1767df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 1768df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 1769df8bae1dSRodney W. Grimes * window so we send only this one 1770df8bae1dSRodney W. Grimes * packet. 1771df8bae1dSRodney W. Grimes * 1772df8bae1dSRodney W. Grimes * We know we're losing at the current 1773df8bae1dSRodney W. Grimes * window size so do congestion avoidance 1774df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 1775df8bae1dSRodney W. Grimes * and pull our congestion window back to 1776df8bae1dSRodney W. Grimes * the new ssthresh). 1777df8bae1dSRodney W. Grimes * 1778df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 1779df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 1780df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 1781df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 1782df8bae1dSRodney W. Grimes * network. 1783df8bae1dSRodney W. Grimes */ 17849b8b58e0SJonathan Lemon if (!callout_active(tp->tt_rexmt) || 1785fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 1786df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1787df8bae1dSRodney W. Grimes else if (++tp->t_dupacks == tcprexmtthresh) { 1788df8bae1dSRodney W. Grimes tcp_seq onxt = tp->snd_nxt; 1789df8bae1dSRodney W. Grimes u_int win = 1790df8bae1dSRodney W. Grimes min(tp->snd_wnd, tp->snd_cwnd) / 2 / 1791df8bae1dSRodney W. Grimes tp->t_maxseg; 179246f58482SJonathan Lemon if (tcp_do_newreno && SEQ_LT(th->th_ack, 179346f58482SJonathan Lemon tp->snd_recover)) { 179446f58482SJonathan Lemon /* False retransmit, should not 179546f58482SJonathan Lemon * cut window 179646f58482SJonathan Lemon */ 179746f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 179846f58482SJonathan Lemon tp->t_dupacks = 0; 179946f58482SJonathan Lemon (void) tcp_output(tp); 180046f58482SJonathan Lemon goto drop; 180146f58482SJonathan Lemon } 1802df8bae1dSRodney W. Grimes if (win < 2) 1803df8bae1dSRodney W. Grimes win = 2; 1804df8bae1dSRodney W. Grimes tp->snd_ssthresh = win * tp->t_maxseg; 180546f58482SJonathan Lemon tp->snd_recover = tp->snd_max; 18069b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 18079b8b58e0SJonathan Lemon tp->t_rtttime = 0; 1808fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 1809df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 1810df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1811df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 1812df8bae1dSRodney W. Grimes tp->t_maxseg * tp->t_dupacks; 1813df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 1814df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 1815df8bae1dSRodney W. Grimes goto drop; 1816df8bae1dSRodney W. Grimes } else if (tp->t_dupacks > tcprexmtthresh) { 1817df8bae1dSRodney W. Grimes tp->snd_cwnd += tp->t_maxseg; 1818df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1819df8bae1dSRodney W. Grimes goto drop; 1820df8bae1dSRodney W. Grimes } 1821df8bae1dSRodney W. Grimes } else 1822df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1823df8bae1dSRodney W. Grimes break; 1824df8bae1dSRodney W. Grimes } 1825df8bae1dSRodney W. Grimes /* 1826df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 1827df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 1828df8bae1dSRodney W. Grimes */ 182946f58482SJonathan Lemon if (tcp_do_newreno == 0) { 1830233e8c18SGarrett Wollman if (tp->t_dupacks >= tcprexmtthresh && 1831df8bae1dSRodney W. Grimes tp->snd_cwnd > tp->snd_ssthresh) 1832df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh; 1833df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 183446f58482SJonathan Lemon } else if (tp->t_dupacks >= tcprexmtthresh && 183546f58482SJonathan Lemon !tcp_newreno(tp, th)) { 183646f58482SJonathan Lemon /* 183746f58482SJonathan Lemon * Window inflation should have left us with approx. 183846f58482SJonathan Lemon * snd_ssthresh outstanding data. But in case we 183946f58482SJonathan Lemon * would be inclined to send a burst, better to do 184046f58482SJonathan Lemon * it via the slow start mechanism. 184146f58482SJonathan Lemon */ 184246f58482SJonathan Lemon if (SEQ_GT(th->th_ack + tp->snd_ssthresh, tp->snd_max)) 184346f58482SJonathan Lemon tp->snd_cwnd = 184446f58482SJonathan Lemon tp->snd_max - th->th_ack + tp->t_maxseg; 184546f58482SJonathan Lemon else 184646f58482SJonathan Lemon tp->snd_cwnd = tp->snd_ssthresh; 184746f58482SJonathan Lemon tp->t_dupacks = 0; 184846f58482SJonathan Lemon } 1849fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_max)) { 1850df8bae1dSRodney W. Grimes tcpstat.tcps_rcvacktoomuch++; 1851df8bae1dSRodney W. Grimes goto dropafterack; 1852df8bae1dSRodney W. Grimes } 1853a0292f23SGarrett Wollman /* 1854a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 1855a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 1856a0292f23SGarrett Wollman */ 1857a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 1858a0292f23SGarrett Wollman /* 1859a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 1860a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 186107e43e10SAndras Olah * synchronized). Go to non-starred state, 186207e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 186307e43e10SAndras Olah * we can do window scaling. 1864a0292f23SGarrett Wollman */ 1865a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 1866a0292f23SGarrett Wollman tp->snd_una++; 186707e43e10SAndras Olah /* Do window scaling? */ 186807e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 186907e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 187007e43e10SAndras Olah tp->snd_scale = tp->requested_s_scale; 187107e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 187207e43e10SAndras Olah } 1873a0292f23SGarrett Wollman } 1874a0292f23SGarrett Wollman 1875a0292f23SGarrett Wollman process_ACK: 1876fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 1877df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 1878df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 1879df8bae1dSRodney W. Grimes 1880df8bae1dSRodney W. Grimes /* 18819b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 18829b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 18839b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 18849b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 18859b8b58e0SJonathan Lemon * we left off. 18869b8b58e0SJonathan Lemon */ 18879b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 18889b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 18899b8b58e0SJonathan Lemon tp->snd_ssthresh = tp->snd_ssthresh_prev; 18909b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 18919b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; /* XXX probably not required */ 18929b8b58e0SJonathan Lemon } 18939b8b58e0SJonathan Lemon 18949b8b58e0SJonathan Lemon /* 1895df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 1896df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 1897df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 1898df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 1899df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 1900df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 1901df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 1902df8bae1dSRodney W. Grimes */ 1903a0292f23SGarrett Wollman if (to.to_flag & TOF_TS) 19049b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 1905fb59c426SYoshinobu Inoue else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) 19069b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 1907df8bae1dSRodney W. Grimes 1908df8bae1dSRodney W. Grimes /* 1909df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 1910df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 1911df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 1912df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 1913df8bae1dSRodney W. Grimes */ 1914fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 19159b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 1916df8bae1dSRodney W. Grimes needoutput = 1; 19179b8b58e0SJonathan Lemon } else if (!callout_active(tp->tt_persist)) 19189b8b58e0SJonathan Lemon callout_reset(tp->tt_rexmt, tp->t_rxtcur, 19199b8b58e0SJonathan Lemon tcp_timer_rexmt, tp); 1920a0292f23SGarrett Wollman 1921a0292f23SGarrett Wollman /* 1922a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 1923a0292f23SGarrett Wollman * skip rest of ACK processing. 1924a0292f23SGarrett Wollman */ 1925a0292f23SGarrett Wollman if (acked == 0) 1926a0292f23SGarrett Wollman goto step6; 1927a0292f23SGarrett Wollman 1928df8bae1dSRodney W. Grimes /* 1929df8bae1dSRodney W. Grimes * When new data is acked, open the congestion window. 1930df8bae1dSRodney W. Grimes * If the window gives us less than ssthresh packets 1931df8bae1dSRodney W. Grimes * in flight, open exponentially (maxseg per packet). 1932df8bae1dSRodney W. Grimes * Otherwise open linearly: maxseg per window 193310be5648SGarrett Wollman * (maxseg^2 / cwnd per packet). 1934df8bae1dSRodney W. Grimes */ 1935df8bae1dSRodney W. Grimes { 1936df8bae1dSRodney W. Grimes register u_int cw = tp->snd_cwnd; 1937df8bae1dSRodney W. Grimes register u_int incr = tp->t_maxseg; 1938df8bae1dSRodney W. Grimes 1939df8bae1dSRodney W. Grimes if (cw > tp->snd_ssthresh) 194010be5648SGarrett Wollman incr = incr * incr / cw; 19418735719eSJonathan Lemon /* 19428735719eSJonathan Lemon * If t_dupacks != 0 here, it indicates that we are still 19438735719eSJonathan Lemon * in NewReno fast recovery mode, so we leave the congestion 19448735719eSJonathan Lemon * window alone. 19458735719eSJonathan Lemon */ 19468735719eSJonathan Lemon if (tcp_do_newreno == 0 || tp->t_dupacks == 0) 1947df8bae1dSRodney W. Grimes tp->snd_cwnd = min(cw + incr,TCP_MAXWIN<<tp->snd_scale); 1948df8bae1dSRodney W. Grimes } 1949df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 1950df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 1951df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 1952df8bae1dSRodney W. Grimes ourfinisacked = 1; 1953df8bae1dSRodney W. Grimes } else { 1954df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 1955df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 1956df8bae1dSRodney W. Grimes ourfinisacked = 0; 1957df8bae1dSRodney W. Grimes } 1958df8bae1dSRodney W. Grimes sowwakeup(so); 1959fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 1960df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1961df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 1962df8bae1dSRodney W. Grimes 1963df8bae1dSRodney W. Grimes switch (tp->t_state) { 1964df8bae1dSRodney W. Grimes 1965df8bae1dSRodney W. Grimes /* 1966df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 1967df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 1968df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 1969df8bae1dSRodney W. Grimes */ 1970df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1971df8bae1dSRodney W. Grimes if (ourfinisacked) { 1972df8bae1dSRodney W. Grimes /* 1973df8bae1dSRodney W. Grimes * If we can't receive any more 1974df8bae1dSRodney W. Grimes * data, then closing user can proceed. 1975df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 1976df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 1977df8bae1dSRodney W. Grimes * we'll hang forever. 1978df8bae1dSRodney W. Grimes */ 1979df8bae1dSRodney W. Grimes if (so->so_state & SS_CANTRCVMORE) { 1980df8bae1dSRodney W. Grimes soisdisconnected(so); 19819b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, tcp_maxidle, 19829b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 1983df8bae1dSRodney W. Grimes } 1984df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 1985df8bae1dSRodney W. Grimes } 1986df8bae1dSRodney W. Grimes break; 1987df8bae1dSRodney W. Grimes 1988df8bae1dSRodney W. Grimes /* 1989df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 1990df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 1991df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 1992df8bae1dSRodney W. Grimes * the segment. 1993df8bae1dSRodney W. Grimes */ 1994df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1995df8bae1dSRodney W. Grimes if (ourfinisacked) { 1996df8bae1dSRodney W. Grimes tp->t_state = TCPS_TIME_WAIT; 1997df8bae1dSRodney W. Grimes tcp_canceltimers(tp); 1998a0292f23SGarrett Wollman /* Shorten TIME_WAIT [RFC-1644, p.28] */ 1999a0292f23SGarrett Wollman if (tp->cc_recv != 0 && 20009b8b58e0SJonathan Lemon (ticks - tp->t_starttime) < tcp_msl) 20019b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 20029b8b58e0SJonathan Lemon tp->t_rxtcur * 20039b8b58e0SJonathan Lemon TCPTV_TWTRUNC, 20049b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2005a0292f23SGarrett Wollman else 20069b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 20079b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2008df8bae1dSRodney W. Grimes soisdisconnected(so); 2009df8bae1dSRodney W. Grimes } 2010df8bae1dSRodney W. Grimes break; 2011df8bae1dSRodney W. Grimes 2012df8bae1dSRodney W. Grimes /* 2013df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2014df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2015df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2016df8bae1dSRodney W. Grimes * enter the closed state and return. 2017df8bae1dSRodney W. Grimes */ 2018df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2019df8bae1dSRodney W. Grimes if (ourfinisacked) { 2020df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2021df8bae1dSRodney W. Grimes goto drop; 2022df8bae1dSRodney W. Grimes } 2023df8bae1dSRodney W. Grimes break; 2024df8bae1dSRodney W. Grimes 2025df8bae1dSRodney W. Grimes /* 2026df8bae1dSRodney W. Grimes * In TIME_WAIT state the only thing that should arrive 2027df8bae1dSRodney W. Grimes * is a retransmission of the remote FIN. Acknowledge 2028df8bae1dSRodney W. Grimes * it and restart the finack timer. 2029df8bae1dSRodney W. Grimes */ 2030df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 20319b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 20329b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2033df8bae1dSRodney W. Grimes goto dropafterack; 2034df8bae1dSRodney W. Grimes } 2035df8bae1dSRodney W. Grimes } 2036df8bae1dSRodney W. Grimes 2037df8bae1dSRodney W. Grimes step6: 2038df8bae1dSRodney W. Grimes /* 2039df8bae1dSRodney W. Grimes * Update window information. 2040df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 2041df8bae1dSRodney W. Grimes */ 2042fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 2043fb59c426SYoshinobu Inoue (SEQ_LT(tp->snd_wl1, th->th_seq) || 2044fb59c426SYoshinobu Inoue (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2045fb59c426SYoshinobu Inoue (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2046df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 2047fb59c426SYoshinobu Inoue if (tlen == 0 && 2048fb59c426SYoshinobu Inoue tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2049df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinupd++; 2050df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 2051fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq; 2052fb59c426SYoshinobu Inoue tp->snd_wl2 = th->th_ack; 2053df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2054df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 2055df8bae1dSRodney W. Grimes needoutput = 1; 2056df8bae1dSRodney W. Grimes } 2057df8bae1dSRodney W. Grimes 2058df8bae1dSRodney W. Grimes /* 2059df8bae1dSRodney W. Grimes * Process segments with URG. 2060df8bae1dSRodney W. Grimes */ 2061fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2062df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2063df8bae1dSRodney W. Grimes /* 2064df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2065df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2066df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2067df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2068df8bae1dSRodney W. Grimes */ 2069fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2070fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2071fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 2072df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2073df8bae1dSRodney W. Grimes } 2074df8bae1dSRodney W. Grimes /* 2075df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2076df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2077df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2078df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2079df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2080df8bae1dSRodney W. Grimes * 2081df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2082df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2083df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2084df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2085df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2086df8bae1dSRodney W. Grimes * spec states (in one of two places). 2087df8bae1dSRodney W. Grimes */ 2088fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2089fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2090df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2091df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2092df8bae1dSRodney W. Grimes if (so->so_oobmark == 0) 2093df8bae1dSRodney W. Grimes so->so_state |= SS_RCVATMARK; 2094df8bae1dSRodney W. Grimes sohasoutofband(so); 2095df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2096df8bae1dSRodney W. Grimes } 2097df8bae1dSRodney W. Grimes /* 2098df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2099df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2100df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2101df8bae1dSRodney W. Grimes * data may creep in... ick. 2102df8bae1dSRodney W. Grimes */ 2103fb59c426SYoshinobu Inoue if (th->th_urp <= (u_long)tlen 2104df8bae1dSRodney W. Grimes #ifdef SO_OOBINLINE 2105df8bae1dSRodney W. Grimes && (so->so_options & SO_OOBINLINE) == 0 2106df8bae1dSRodney W. Grimes #endif 2107df8bae1dSRodney W. Grimes ) 2108fb59c426SYoshinobu Inoue tcp_pulloutofband(so, th, m, 2109fb59c426SYoshinobu Inoue drop_hdrlen); /* hdr drop is delayed */ 2110df8bae1dSRodney W. Grimes } else 2111df8bae1dSRodney W. Grimes /* 2112df8bae1dSRodney W. Grimes * If no out of band data is expected, 2113df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2114df8bae1dSRodney W. Grimes * with the receive window. 2115df8bae1dSRodney W. Grimes */ 2116df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2117df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2118df8bae1dSRodney W. Grimes dodata: /* XXX */ 2119df8bae1dSRodney W. Grimes 2120df8bae1dSRodney W. Grimes /* 2121df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2122df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2123df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2124df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2125df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2126df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2127df8bae1dSRodney W. Grimes */ 2128fb59c426SYoshinobu Inoue if ((tlen || (thflags&TH_FIN)) && 2129df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2130fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2131fb59c426SYoshinobu Inoue TCP_REASS(tp, th, &tlen, m, so, thflags); 2132df8bae1dSRodney W. Grimes /* 2133df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2134df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2135df8bae1dSRodney W. Grimes * buffer size. 2136df8bae1dSRodney W. Grimes */ 2137df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2138df8bae1dSRodney W. Grimes } else { 2139df8bae1dSRodney W. Grimes m_freem(m); 2140fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2141df8bae1dSRodney W. Grimes } 2142df8bae1dSRodney W. Grimes 2143df8bae1dSRodney W. Grimes /* 2144df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2145df8bae1dSRodney W. Grimes * that the connection is closing. 2146df8bae1dSRodney W. Grimes */ 2147fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2148df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2149df8bae1dSRodney W. Grimes socantrcvmore(so); 2150a0292f23SGarrett Wollman /* 2151a0292f23SGarrett Wollman * If connection is half-synchronized 2152d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2153a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2154a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2155a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2156a0292f23SGarrett Wollman */ 2157f498eeeeSDavid Greenman if (tcp_delack_enabled && (tp->t_flags & TF_NEEDSYN)) 21589b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 21599b8b58e0SJonathan Lemon tcp_timer_delack, tp); 2160a0292f23SGarrett Wollman else 2161df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2162df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2163df8bae1dSRodney W. Grimes } 2164df8bae1dSRodney W. Grimes switch (tp->t_state) { 2165df8bae1dSRodney W. Grimes 2166df8bae1dSRodney W. Grimes /* 2167df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2168df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2169df8bae1dSRodney W. Grimes */ 2170df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 21719b8b58e0SJonathan Lemon tp->t_starttime = ticks; 21729b8b58e0SJonathan Lemon /*FALLTHROUGH*/ 2173df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2174df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2175df8bae1dSRodney W. Grimes break; 2176df8bae1dSRodney W. Grimes 2177df8bae1dSRodney W. Grimes /* 2178df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2179df8bae1dSRodney W. Grimes * enter the CLOSING state. 2180df8bae1dSRodney W. Grimes */ 2181df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2182df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 2183df8bae1dSRodney W. Grimes break; 2184df8bae1dSRodney W. Grimes 2185df8bae1dSRodney W. Grimes /* 2186df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 2187df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 2188df8bae1dSRodney W. Grimes * standard timers. 2189df8bae1dSRodney W. Grimes */ 2190df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2191df8bae1dSRodney W. Grimes tp->t_state = TCPS_TIME_WAIT; 2192df8bae1dSRodney W. Grimes tcp_canceltimers(tp); 2193a0292f23SGarrett Wollman /* Shorten TIME_WAIT [RFC-1644, p.28] */ 2194a0292f23SGarrett Wollman if (tp->cc_recv != 0 && 21959b8b58e0SJonathan Lemon (ticks - tp->t_starttime) < tcp_msl) { 21969b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 21979b8b58e0SJonathan Lemon tp->t_rxtcur * TCPTV_TWTRUNC, 21989b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2199a0292f23SGarrett Wollman /* For transaction client, force ACK now. */ 2200a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2201a0292f23SGarrett Wollman } 2202a0292f23SGarrett Wollman else 22039b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 22049b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2205df8bae1dSRodney W. Grimes soisdisconnected(so); 2206df8bae1dSRodney W. Grimes break; 2207df8bae1dSRodney W. Grimes 2208df8bae1dSRodney W. Grimes /* 2209df8bae1dSRodney W. Grimes * In TIME_WAIT state restart the 2 MSL time_wait timer. 2210df8bae1dSRodney W. Grimes */ 2211df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 22129b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 22139b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2214df8bae1dSRodney W. Grimes break; 2215df8bae1dSRodney W. Grimes } 2216df8bae1dSRodney W. Grimes } 2217610ee2f9SDavid Greenman #ifdef TCPDEBUG 2218df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) 2219fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2220fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2221610ee2f9SDavid Greenman #endif 2222df8bae1dSRodney W. Grimes 2223df8bae1dSRodney W. Grimes /* 2224df8bae1dSRodney W. Grimes * Return any desired output. 2225df8bae1dSRodney W. Grimes */ 2226df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 2227df8bae1dSRodney W. Grimes (void) tcp_output(tp); 2228df8bae1dSRodney W. Grimes return; 2229df8bae1dSRodney W. Grimes 2230df8bae1dSRodney W. Grimes dropafterack: 2231df8bae1dSRodney W. Grimes /* 2232df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 2233df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 223480ab7c0eSGarrett Wollman * 223580ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 223680ab7c0eSGarrett Wollman * paths to this code happen after packets containing 223780ab7c0eSGarrett Wollman * RST have been dropped. 223880ab7c0eSGarrett Wollman * 223980ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 224080ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 224180ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 224280ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 224380ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 224480ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 2245df8bae1dSRodney W. Grimes */ 2246fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2247fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 2248fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max)) ) 2249173c0f9fSWarner Losh goto maybedropwithreset; 2250a0292f23SGarrett Wollman #ifdef TCPDEBUG 2251a0292f23SGarrett Wollman if (so->so_options & SO_DEBUG) 2252fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2253fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2254a0292f23SGarrett Wollman #endif 2255df8bae1dSRodney W. Grimes m_freem(m); 2256df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2257df8bae1dSRodney W. Grimes (void) tcp_output(tp); 2258df8bae1dSRodney W. Grimes return; 2259df8bae1dSRodney W. Grimes 2260173c0f9fSWarner Losh 2261173c0f9fSWarner Losh /* 2262173c0f9fSWarner Losh * Conditionally drop with reset or just drop depending on whether 2263173c0f9fSWarner Losh * we think we are under attack or not. 2264173c0f9fSWarner Losh */ 2265173c0f9fSWarner Losh maybedropwithreset: 226609f81a46SBosko Milekic if (rstreason != BANDLIM_RST_NOTOPEN) 226709f81a46SBosko Milekic rstreason = BANDLIM_RST_OPEN; 226809f81a46SBosko Milekic if (badport_bandlim(rstreason) < 0) 2269173c0f9fSWarner Losh goto drop; 2270173c0f9fSWarner Losh /* fall through */ 2271df8bae1dSRodney W. Grimes dropwithreset: 2272e46cd3d4SDag-Erling Smørgrav #ifdef TCP_RESTRICT_RST 2273e46cd3d4SDag-Erling Smørgrav if (restrict_rst) 2274e46cd3d4SDag-Erling Smørgrav goto drop; 2275e46cd3d4SDag-Erling Smørgrav #endif 2276df8bae1dSRodney W. Grimes /* 2277df8bae1dSRodney W. Grimes * Generate a RST, dropping incoming segment. 2278df8bae1dSRodney W. Grimes * Make ACK acceptable to originator of segment. 2279df8bae1dSRodney W. Grimes * Don't bother to respond if destination was broadcast/multicast. 2280df8bae1dSRodney W. Grimes */ 2281fb59c426SYoshinobu Inoue if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2282df8bae1dSRodney W. Grimes goto drop; 2283fb59c426SYoshinobu Inoue #ifdef INET6 2284fb59c426SYoshinobu Inoue if (isipv6) { 2285173c0f9fSWarner Losh if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2286173c0f9fSWarner Losh IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2287fb59c426SYoshinobu Inoue goto drop; 2288fb59c426SYoshinobu Inoue } else 2289fb59c426SYoshinobu Inoue #endif /* INET6 */ 2290173c0f9fSWarner Losh if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2291173c0f9fSWarner Losh IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2292173c0f9fSWarner Losh ip->ip_src.s_addr == htonl(INADDR_BROADCAST)) 2293fb59c426SYoshinobu Inoue goto drop; 2294fb59c426SYoshinobu Inoue /* IPv6 anycast check is done at tcp6_input() */ 2295a0292f23SGarrett Wollman #ifdef TCPDEBUG 2296a0292f23SGarrett Wollman if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2297fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2298fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2299a0292f23SGarrett Wollman #endif 2300fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 2301fb59c426SYoshinobu Inoue /* mtod() below is safe as long as hdr dropping is delayed */ 2302fb59c426SYoshinobu Inoue tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack, 2303fb59c426SYoshinobu Inoue TH_RST); 2304df8bae1dSRodney W. Grimes else { 2305fb59c426SYoshinobu Inoue if (thflags & TH_SYN) 2306fb59c426SYoshinobu Inoue tlen++; 2307fb59c426SYoshinobu Inoue /* mtod() below is safe as long as hdr dropping is delayed */ 2308fb59c426SYoshinobu Inoue tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2309fb59c426SYoshinobu Inoue (tcp_seq)0, TH_RST|TH_ACK); 2310df8bae1dSRodney W. Grimes } 2311df8bae1dSRodney W. Grimes /* destroy temporarily created socket */ 2312df8bae1dSRodney W. Grimes if (dropsocket) 2313df8bae1dSRodney W. Grimes (void) soabort(so); 2314df8bae1dSRodney W. Grimes return; 2315df8bae1dSRodney W. Grimes 2316df8bae1dSRodney W. Grimes drop: 2317df8bae1dSRodney W. Grimes /* 2318df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 2319df8bae1dSRodney W. Grimes */ 2320610ee2f9SDavid Greenman #ifdef TCPDEBUG 2321a0292f23SGarrett Wollman if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2322fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2323fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2324a0292f23SGarrett Wollman #endif 2325df8bae1dSRodney W. Grimes m_freem(m); 2326df8bae1dSRodney W. Grimes /* destroy temporarily created socket */ 2327df8bae1dSRodney W. Grimes if (dropsocket) 2328df8bae1dSRodney W. Grimes (void) soabort(so); 2329df8bae1dSRodney W. Grimes return; 2330df8bae1dSRodney W. Grimes } 2331df8bae1dSRodney W. Grimes 23320312fbe9SPoul-Henning Kamp static void 2333fb59c426SYoshinobu Inoue tcp_dooptions(tp, cp, cnt, th, to) 2334df8bae1dSRodney W. Grimes struct tcpcb *tp; 2335df8bae1dSRodney W. Grimes u_char *cp; 2336df8bae1dSRodney W. Grimes int cnt; 2337fb59c426SYoshinobu Inoue struct tcphdr *th; 2338a0292f23SGarrett Wollman struct tcpopt *to; 2339df8bae1dSRodney W. Grimes { 2340a0292f23SGarrett Wollman u_short mss = 0; 2341df8bae1dSRodney W. Grimes int opt, optlen; 2342df8bae1dSRodney W. Grimes 2343df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 2344df8bae1dSRodney W. Grimes opt = cp[0]; 2345df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 2346df8bae1dSRodney W. Grimes break; 2347df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 2348df8bae1dSRodney W. Grimes optlen = 1; 2349df8bae1dSRodney W. Grimes else { 2350b474779fSJun-ichiro itojun Hagino if (cnt < 2) 2351b474779fSJun-ichiro itojun Hagino break; 2352df8bae1dSRodney W. Grimes optlen = cp[1]; 2353b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 2354df8bae1dSRodney W. Grimes break; 2355df8bae1dSRodney W. Grimes } 2356df8bae1dSRodney W. Grimes switch (opt) { 2357df8bae1dSRodney W. Grimes 2358df8bae1dSRodney W. Grimes default: 2359df8bae1dSRodney W. Grimes continue; 2360df8bae1dSRodney W. Grimes 2361df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 2362df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 2363df8bae1dSRodney W. Grimes continue; 2364fb59c426SYoshinobu Inoue if (!(th->th_flags & TH_SYN)) 2365df8bae1dSRodney W. Grimes continue; 2366df8bae1dSRodney W. Grimes bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); 2367df8bae1dSRodney W. Grimes NTOHS(mss); 2368df8bae1dSRodney W. Grimes break; 2369df8bae1dSRodney W. Grimes 2370df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 2371df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 2372df8bae1dSRodney W. Grimes continue; 2373fb59c426SYoshinobu Inoue if (!(th->th_flags & TH_SYN)) 2374df8bae1dSRodney W. Grimes continue; 2375df8bae1dSRodney W. Grimes tp->t_flags |= TF_RCVD_SCALE; 2376df8bae1dSRodney W. Grimes tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 2377df8bae1dSRodney W. Grimes break; 2378df8bae1dSRodney W. Grimes 2379df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 2380df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 2381df8bae1dSRodney W. Grimes continue; 2382a0292f23SGarrett Wollman to->to_flag |= TOF_TS; 2383a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2384a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 2385a0292f23SGarrett Wollman NTOHL(to->to_tsval); 2386a0292f23SGarrett Wollman bcopy((char *)cp + 6, 2387a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2388a0292f23SGarrett Wollman NTOHL(to->to_tsecr); 2389df8bae1dSRodney W. Grimes 2390df8bae1dSRodney W. Grimes /* 2391df8bae1dSRodney W. Grimes * A timestamp received in a SYN makes 2392df8bae1dSRodney W. Grimes * it ok to send timestamp requests and replies. 2393df8bae1dSRodney W. Grimes */ 2394fb59c426SYoshinobu Inoue if (th->th_flags & TH_SYN) { 2395df8bae1dSRodney W. Grimes tp->t_flags |= TF_RCVD_TSTMP; 2396a0292f23SGarrett Wollman tp->ts_recent = to->to_tsval; 23979b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 2398df8bae1dSRodney W. Grimes } 2399df8bae1dSRodney W. Grimes break; 2400a0292f23SGarrett Wollman case TCPOPT_CC: 2401a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2402a0292f23SGarrett Wollman continue; 240340db8ef7SAndras Olah to->to_flag |= TOF_CC; 2404a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2405a0292f23SGarrett Wollman (char *)&to->to_cc, sizeof(to->to_cc)); 2406a0292f23SGarrett Wollman NTOHL(to->to_cc); 2407a0292f23SGarrett Wollman /* 2408a0292f23SGarrett Wollman * A CC or CC.new option received in a SYN makes 2409a0292f23SGarrett Wollman * it ok to send CC in subsequent segments. 2410a0292f23SGarrett Wollman */ 2411fb59c426SYoshinobu Inoue if (th->th_flags & TH_SYN) 2412a0292f23SGarrett Wollman tp->t_flags |= TF_RCVD_CC; 2413a0292f23SGarrett Wollman break; 2414a0292f23SGarrett Wollman case TCPOPT_CCNEW: 2415a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2416a0292f23SGarrett Wollman continue; 2417fb59c426SYoshinobu Inoue if (!(th->th_flags & TH_SYN)) 2418a0292f23SGarrett Wollman continue; 2419a0292f23SGarrett Wollman to->to_flag |= TOF_CCNEW; 2420a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2421a0292f23SGarrett Wollman (char *)&to->to_cc, sizeof(to->to_cc)); 2422a0292f23SGarrett Wollman NTOHL(to->to_cc); 2423a0292f23SGarrett Wollman /* 2424a0292f23SGarrett Wollman * A CC or CC.new option received in a SYN makes 2425a0292f23SGarrett Wollman * it ok to send CC in subsequent segments. 2426a0292f23SGarrett Wollman */ 2427a0292f23SGarrett Wollman tp->t_flags |= TF_RCVD_CC; 2428a0292f23SGarrett Wollman break; 2429a0292f23SGarrett Wollman case TCPOPT_CCECHO: 2430a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2431a0292f23SGarrett Wollman continue; 2432fb59c426SYoshinobu Inoue if (!(th->th_flags & TH_SYN)) 2433a0292f23SGarrett Wollman continue; 2434a0292f23SGarrett Wollman to->to_flag |= TOF_CCECHO; 2435a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2436a0292f23SGarrett Wollman (char *)&to->to_ccecho, sizeof(to->to_ccecho)); 2437a0292f23SGarrett Wollman NTOHL(to->to_ccecho); 2438a0292f23SGarrett Wollman break; 2439df8bae1dSRodney W. Grimes } 2440df8bae1dSRodney W. Grimes } 2441fb59c426SYoshinobu Inoue if (th->th_flags & TH_SYN) 2442a0292f23SGarrett Wollman tcp_mss(tp, mss); /* sets t_maxseg */ 2443df8bae1dSRodney W. Grimes } 2444df8bae1dSRodney W. Grimes 2445df8bae1dSRodney W. Grimes /* 2446df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 2447df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 2448df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 2449df8bae1dSRodney W. Grimes * sequencing purposes. 2450df8bae1dSRodney W. Grimes */ 24510312fbe9SPoul-Henning Kamp static void 2452fb59c426SYoshinobu Inoue tcp_pulloutofband(so, th, m, off) 2453df8bae1dSRodney W. Grimes struct socket *so; 2454fb59c426SYoshinobu Inoue struct tcphdr *th; 2455df8bae1dSRodney W. Grimes register struct mbuf *m; 2456fb59c426SYoshinobu Inoue int off; /* delayed to be droped hdrlen */ 2457df8bae1dSRodney W. Grimes { 2458fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 2459df8bae1dSRodney W. Grimes 2460df8bae1dSRodney W. Grimes while (cnt >= 0) { 2461df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 2462df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 2463df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 2464df8bae1dSRodney W. Grimes 2465df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 2466df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 2467df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2468df8bae1dSRodney W. Grimes m->m_len--; 246969a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 247069a34685SYoshinobu Inoue m->m_pkthdr.len--; 2471df8bae1dSRodney W. Grimes return; 2472df8bae1dSRodney W. Grimes } 2473df8bae1dSRodney W. Grimes cnt -= m->m_len; 2474df8bae1dSRodney W. Grimes m = m->m_next; 2475df8bae1dSRodney W. Grimes if (m == 0) 2476df8bae1dSRodney W. Grimes break; 2477df8bae1dSRodney W. Grimes } 2478df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 2479df8bae1dSRodney W. Grimes } 2480df8bae1dSRodney W. Grimes 2481df8bae1dSRodney W. Grimes /* 2482df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 2483df8bae1dSRodney W. Grimes * and update averages and current timeout. 2484df8bae1dSRodney W. Grimes */ 24850312fbe9SPoul-Henning Kamp static void 2486df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt) 2487df8bae1dSRodney W. Grimes register struct tcpcb *tp; 24889b8b58e0SJonathan Lemon int rtt; 2489df8bae1dSRodney W. Grimes { 2490233e8c18SGarrett Wollman register int delta; 2491233e8c18SGarrett Wollman 2492233e8c18SGarrett Wollman tcpstat.tcps_rttupdated++; 2493233e8c18SGarrett Wollman tp->t_rttupdated++; 2494233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 2495233e8c18SGarrett Wollman /* 2496233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 2497233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 2498233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 2499233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2500233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 2501233e8c18SGarrett Wollman */ 2502233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2503233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2504233e8c18SGarrett Wollman 2505233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 2506233e8c18SGarrett Wollman tp->t_srtt = 1; 2507233e8c18SGarrett Wollman 2508233e8c18SGarrett Wollman /* 2509233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 2510233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 2511233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 2512233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 2513233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 2514233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 2515233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2516233e8c18SGarrett Wollman * rfc793's wired-in beta. 2517233e8c18SGarrett Wollman */ 2518233e8c18SGarrett Wollman if (delta < 0) 2519233e8c18SGarrett Wollman delta = -delta; 2520233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2521233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 2522233e8c18SGarrett Wollman tp->t_rttvar = 1; 2523233e8c18SGarrett Wollman } else { 2524233e8c18SGarrett Wollman /* 2525233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 2526233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 2527233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 2528233e8c18SGarrett Wollman */ 2529233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 2530233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 2531233e8c18SGarrett Wollman } 25329b8b58e0SJonathan Lemon tp->t_rtttime = 0; 2533df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 2534df8bae1dSRodney W. Grimes 2535df8bae1dSRodney W. Grimes /* 2536df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 2537df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 2538df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 2539df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 2540df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 2541df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 2542df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 2543df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 2544df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 2545df8bae1dSRodney W. Grimes */ 2546233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 25479e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2548df8bae1dSRodney W. Grimes 2549df8bae1dSRodney W. Grimes /* 2550df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 2551df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 2552df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 2553df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 2554df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 2555df8bae1dSRodney W. Grimes */ 2556df8bae1dSRodney W. Grimes tp->t_softerror = 0; 2557df8bae1dSRodney W. Grimes } 2558df8bae1dSRodney W. Grimes 2559df8bae1dSRodney W. Grimes /* 2560df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 2561df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 2562df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 2563df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 2564df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2565df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 2566df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 2567df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 2568df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 2569df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 2570df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 2571df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 2572df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 2573a0292f23SGarrett Wollman * 2574a0292f23SGarrett Wollman * Also take into account the space needed for options that we 2575a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 2576a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 2577a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 2578a0292f23SGarrett Wollman * data in maxopd. 2579a0292f23SGarrett Wollman * 2580a0292f23SGarrett Wollman * NOTE that this routine is only called when we process an incoming 2581a0292f23SGarrett Wollman * segment, for outgoing segments only tcp_mssopt is called. 2582a0292f23SGarrett Wollman * 2583a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 2584a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 2585a0292f23SGarrett Wollman * MSS of our peer. 2586df8bae1dSRodney W. Grimes */ 2587a0292f23SGarrett Wollman void 2588df8bae1dSRodney W. Grimes tcp_mss(tp, offer) 2589a0292f23SGarrett Wollman struct tcpcb *tp; 2590a0292f23SGarrett Wollman int offer; 2591df8bae1dSRodney W. Grimes { 2592df8bae1dSRodney W. Grimes register struct rtentry *rt; 2593df8bae1dSRodney W. Grimes struct ifnet *ifp; 2594df8bae1dSRodney W. Grimes register int rtt, mss; 2595df8bae1dSRodney W. Grimes u_long bufsize; 2596df8bae1dSRodney W. Grimes struct inpcb *inp; 2597df8bae1dSRodney W. Grimes struct socket *so; 2598a0292f23SGarrett Wollman struct rmxp_tao *taop; 2599a0292f23SGarrett Wollman int origoffer = offer; 2600fb59c426SYoshinobu Inoue #ifdef INET6 2601fb59c426SYoshinobu Inoue int isipv6; 2602fb59c426SYoshinobu Inoue int min_protoh; 2603fb59c426SYoshinobu Inoue #endif 2604df8bae1dSRodney W. Grimes 2605df8bae1dSRodney W. Grimes inp = tp->t_inpcb; 2606fb59c426SYoshinobu Inoue #ifdef INET6 2607fb59c426SYoshinobu Inoue isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2608fb59c426SYoshinobu Inoue min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr) 2609fb59c426SYoshinobu Inoue : sizeof (struct tcpiphdr); 2610fb59c426SYoshinobu Inoue #else 2611fb59c426SYoshinobu Inoue #define min_protoh (sizeof (struct tcpiphdr)) 2612fb59c426SYoshinobu Inoue #endif 2613fb59c426SYoshinobu Inoue #ifdef INET6 2614fb59c426SYoshinobu Inoue if (isipv6) 2615fb59c426SYoshinobu Inoue rt = tcp_rtlookup6(inp); 2616fb59c426SYoshinobu Inoue else 2617fb59c426SYoshinobu Inoue #endif 2618fb59c426SYoshinobu Inoue rt = tcp_rtlookup(inp); 2619fb59c426SYoshinobu Inoue if (rt == NULL) { 2620fb59c426SYoshinobu Inoue tp->t_maxopd = tp->t_maxseg = 2621fb59c426SYoshinobu Inoue #ifdef INET6 2622fb59c426SYoshinobu Inoue isipv6 ? tcp_v6mssdflt : 2623fb59c426SYoshinobu Inoue #endif /* INET6 */ 2624fb59c426SYoshinobu Inoue tcp_mssdflt; 2625a0292f23SGarrett Wollman return; 2626df8bae1dSRodney W. Grimes } 2627df8bae1dSRodney W. Grimes ifp = rt->rt_ifp; 2628df8bae1dSRodney W. Grimes so = inp->inp_socket; 2629df8bae1dSRodney W. Grimes 2630a0292f23SGarrett Wollman taop = rmx_taop(rt->rt_rmx); 2631a0292f23SGarrett Wollman /* 2632a0292f23SGarrett Wollman * Offer == -1 means that we didn't receive SYN yet, 2633a0292f23SGarrett Wollman * use cached value in that case; 2634a0292f23SGarrett Wollman */ 2635a0292f23SGarrett Wollman if (offer == -1) 2636a0292f23SGarrett Wollman offer = taop->tao_mssopt; 2637a0292f23SGarrett Wollman /* 2638a0292f23SGarrett Wollman * Offer == 0 means that there was no MSS on the SYN segment, 2639a0292f23SGarrett Wollman * in this case we use tcp_mssdflt. 2640a0292f23SGarrett Wollman */ 2641a0292f23SGarrett Wollman if (offer == 0) 2642fb59c426SYoshinobu Inoue offer = 2643fb59c426SYoshinobu Inoue #ifdef INET6 2644fb59c426SYoshinobu Inoue isipv6 ? tcp_v6mssdflt : 2645fb59c426SYoshinobu Inoue #endif /* INET6 */ 2646fb59c426SYoshinobu Inoue tcp_mssdflt; 2647a0292f23SGarrett Wollman else 2648a0292f23SGarrett Wollman /* 2649a0292f23SGarrett Wollman * Sanity check: make sure that maxopd will be large 2650a0292f23SGarrett Wollman * enough to allow some data on segments even is the 2651a0292f23SGarrett Wollman * all the option space is used (40bytes). Otherwise 2652a0292f23SGarrett Wollman * funny things may happen in tcp_output. 2653a0292f23SGarrett Wollman */ 2654a0292f23SGarrett Wollman offer = max(offer, 64); 2655a0292f23SGarrett Wollman taop->tao_mssopt = offer; 2656a0292f23SGarrett Wollman 2657df8bae1dSRodney W. Grimes /* 2658df8bae1dSRodney W. Grimes * While we're here, check if there's an initial rtt 2659df8bae1dSRodney W. Grimes * or rttvar. Convert from the route-table units 2660df8bae1dSRodney W. Grimes * to scaled multiples of the slow timeout timer. 2661df8bae1dSRodney W. Grimes */ 2662df8bae1dSRodney W. Grimes if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 2663df8bae1dSRodney W. Grimes /* 2664a0292f23SGarrett Wollman * XXX the lock bit for RTT indicates that the value 2665df8bae1dSRodney W. Grimes * is also a minimum value; this is subject to time. 2666df8bae1dSRodney W. Grimes */ 2667df8bae1dSRodney W. Grimes if (rt->rt_rmx.rmx_locks & RTV_RTT) 26689b8b58e0SJonathan Lemon tp->t_rttmin = rtt / (RTM_RTTUNIT / hz); 26699b8b58e0SJonathan Lemon tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE)); 2670dd224982SGarrett Wollman tcpstat.tcps_usedrtt++; 2671dd224982SGarrett Wollman if (rt->rt_rmx.rmx_rttvar) { 2672df8bae1dSRodney W. Grimes tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 26739b8b58e0SJonathan Lemon (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE)); 2674dd224982SGarrett Wollman tcpstat.tcps_usedrttvar++; 2675dd224982SGarrett Wollman } else { 2676df8bae1dSRodney W. Grimes /* default variation is +- 1 rtt */ 2677df8bae1dSRodney W. Grimes tp->t_rttvar = 2678df8bae1dSRodney W. Grimes tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 2679dd224982SGarrett Wollman } 2680df8bae1dSRodney W. Grimes TCPT_RANGESET(tp->t_rxtcur, 2681df8bae1dSRodney W. Grimes ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 2682df8bae1dSRodney W. Grimes tp->t_rttmin, TCPTV_REXMTMAX); 2683df8bae1dSRodney W. Grimes } 2684df8bae1dSRodney W. Grimes /* 2685df8bae1dSRodney W. Grimes * if there's an mtu associated with the route, use it 2686fb59c426SYoshinobu Inoue * else, use the link mtu. 2687df8bae1dSRodney W. Grimes */ 2688df8bae1dSRodney W. Grimes if (rt->rt_rmx.rmx_mtu) 2689fb59c426SYoshinobu Inoue mss = rt->rt_rmx.rmx_mtu - min_protoh; 2690df8bae1dSRodney W. Grimes else 2691df8bae1dSRodney W. Grimes { 2692fb59c426SYoshinobu Inoue mss = 2693fb59c426SYoshinobu Inoue #ifdef INET6 2694fb59c426SYoshinobu Inoue (isipv6 ? nd_ifinfo[rt->rt_ifp->if_index].linkmtu : 2695fb59c426SYoshinobu Inoue #endif 2696fb59c426SYoshinobu Inoue ifp->if_mtu 2697fb59c426SYoshinobu Inoue #ifdef INET6 2698fb59c426SYoshinobu Inoue ) 2699fb59c426SYoshinobu Inoue #endif 2700fb59c426SYoshinobu Inoue - min_protoh; 2701fb59c426SYoshinobu Inoue #ifdef INET6 2702fb59c426SYoshinobu Inoue if (isipv6) { 2703fb59c426SYoshinobu Inoue if (!in6_localaddr(&inp->in6p_faddr)) 2704fb59c426SYoshinobu Inoue mss = min(mss, tcp_v6mssdflt); 2705fb59c426SYoshinobu Inoue } else 2706fb59c426SYoshinobu Inoue #endif 2707a0292f23SGarrett Wollman if (!in_localaddr(inp->inp_faddr)) 2708a0292f23SGarrett Wollman mss = min(mss, tcp_mssdflt); 2709a0292f23SGarrett Wollman } 2710a0292f23SGarrett Wollman mss = min(mss, offer); 2711a0292f23SGarrett Wollman /* 2712a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 2713a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 2714a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 2715a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 2716a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 2717a0292f23SGarrett Wollman */ 2718a0292f23SGarrett Wollman tp->t_maxopd = mss; 2719a0292f23SGarrett Wollman 2720a0292f23SGarrett Wollman /* 2721a0292f23SGarrett Wollman * In case of T/TCP, origoffer==-1 indicates, that no segments 2722a0292f23SGarrett Wollman * were received yet. In this case we just guess, otherwise 2723a0292f23SGarrett Wollman * we do the same as before T/TCP. 2724a0292f23SGarrett Wollman */ 2725a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2726a0292f23SGarrett Wollman (origoffer == -1 || 2727a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2728a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 2729a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 2730a0292f23SGarrett Wollman (origoffer == -1 || 2731a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)) 2732a0292f23SGarrett Wollman mss -= TCPOLEN_CC_APPA; 2733a0292f23SGarrett Wollman 2734df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 2735df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2736df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 2737df8bae1dSRodney W. Grimes #else 2738df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2739df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 2740df8bae1dSRodney W. Grimes #endif 2741df8bae1dSRodney W. Grimes /* 2742df8bae1dSRodney W. Grimes * If there's a pipesize, change the socket buffer 2743df8bae1dSRodney W. Grimes * to that size. Make the socket buffers an integral 2744df8bae1dSRodney W. Grimes * number of mss units; if the mss is larger than 2745df8bae1dSRodney W. Grimes * the socket buffer, decrease the mss. 2746df8bae1dSRodney W. Grimes */ 2747df8bae1dSRodney W. Grimes #ifdef RTV_SPIPE 2748df8bae1dSRodney W. Grimes if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 2749df8bae1dSRodney W. Grimes #endif 2750df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 2751df8bae1dSRodney W. Grimes if (bufsize < mss) 2752df8bae1dSRodney W. Grimes mss = bufsize; 2753df8bae1dSRodney W. Grimes else { 2754df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2755df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2756df8bae1dSRodney W. Grimes bufsize = sb_max; 2757ecf72308SBrian Feldman (void)sbreserve(&so->so_snd, bufsize, so, NULL); 2758df8bae1dSRodney W. Grimes } 2759df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 2760df8bae1dSRodney W. Grimes 2761df8bae1dSRodney W. Grimes #ifdef RTV_RPIPE 2762df8bae1dSRodney W. Grimes if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 2763df8bae1dSRodney W. Grimes #endif 2764df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 2765df8bae1dSRodney W. Grimes if (bufsize > mss) { 2766df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2767df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2768df8bae1dSRodney W. Grimes bufsize = sb_max; 2769ecf72308SBrian Feldman (void)sbreserve(&so->so_rcv, bufsize, so, NULL); 2770df8bae1dSRodney W. Grimes } 27719b8b58e0SJonathan Lemon 2772a0292f23SGarrett Wollman /* 27739b8b58e0SJonathan Lemon * Set the slow-start flight size depending on whether this 27749b8b58e0SJonathan Lemon * is a local network or not. 2775a0292f23SGarrett Wollman */ 2776fb59c426SYoshinobu Inoue if ( 2777fb59c426SYoshinobu Inoue #ifdef INET6 2778fb59c426SYoshinobu Inoue (isipv6 && in6_localaddr(&inp->in6p_faddr)) || 2779fb59c426SYoshinobu Inoue (!isipv6 && 2780fb59c426SYoshinobu Inoue #endif 2781fb59c426SYoshinobu Inoue in_localaddr(inp->inp_faddr) 2782fb59c426SYoshinobu Inoue #ifdef INET6 2783fb59c426SYoshinobu Inoue ) 2784fb59c426SYoshinobu Inoue #endif 2785fb59c426SYoshinobu Inoue ) 27869b8b58e0SJonathan Lemon tp->snd_cwnd = mss * ss_fltsz_local; 27879b8b58e0SJonathan Lemon else 27889b8b58e0SJonathan Lemon tp->snd_cwnd = mss * ss_fltsz; 2789df8bae1dSRodney W. Grimes 2790df8bae1dSRodney W. Grimes if (rt->rt_rmx.rmx_ssthresh) { 2791df8bae1dSRodney W. Grimes /* 2792df8bae1dSRodney W. Grimes * There's some sort of gateway or interface 2793df8bae1dSRodney W. Grimes * buffer limit on the path. Use this to set 2794df8bae1dSRodney W. Grimes * the slow start threshhold, but set the 2795df8bae1dSRodney W. Grimes * threshold to no less than 2*mss. 2796df8bae1dSRodney W. Grimes */ 2797df8bae1dSRodney W. Grimes tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 2798dd224982SGarrett Wollman tcpstat.tcps_usedssthresh++; 2799df8bae1dSRodney W. Grimes } 2800a0292f23SGarrett Wollman } 2801a0292f23SGarrett Wollman 2802a0292f23SGarrett Wollman /* 2803a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 2804a0292f23SGarrett Wollman */ 2805a0292f23SGarrett Wollman int 2806a0292f23SGarrett Wollman tcp_mssopt(tp) 2807a0292f23SGarrett Wollman struct tcpcb *tp; 2808a0292f23SGarrett Wollman { 2809a0292f23SGarrett Wollman struct rtentry *rt; 2810fb59c426SYoshinobu Inoue #ifdef INET6 2811fb59c426SYoshinobu Inoue int isipv6; 2812fb59c426SYoshinobu Inoue int min_protoh; 2813fb59c426SYoshinobu Inoue #endif 2814a0292f23SGarrett Wollman 2815fb59c426SYoshinobu Inoue #ifdef INET6 2816fb59c426SYoshinobu Inoue isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2817fb59c426SYoshinobu Inoue min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr) 2818fb59c426SYoshinobu Inoue : sizeof (struct tcpiphdr); 2819fb59c426SYoshinobu Inoue #else 2820fb59c426SYoshinobu Inoue #define min_protoh (sizeof (struct tcpiphdr)) 2821fb59c426SYoshinobu Inoue #endif 2822fb59c426SYoshinobu Inoue #ifdef INET6 2823fb59c426SYoshinobu Inoue if (isipv6) 2824fb59c426SYoshinobu Inoue rt = tcp_rtlookup6(tp->t_inpcb); 2825fb59c426SYoshinobu Inoue else 2826fb59c426SYoshinobu Inoue #endif /* INET6 */ 2827a0292f23SGarrett Wollman rt = tcp_rtlookup(tp->t_inpcb); 2828a0292f23SGarrett Wollman if (rt == NULL) 2829fb59c426SYoshinobu Inoue return 2830fb59c426SYoshinobu Inoue #ifdef INET6 2831fb59c426SYoshinobu Inoue isipv6 ? tcp_v6mssdflt : 2832fb59c426SYoshinobu Inoue #endif /* INET6 */ 2833fb59c426SYoshinobu Inoue tcp_mssdflt; 2834a0292f23SGarrett Wollman 2835fb59c426SYoshinobu Inoue return rt->rt_ifp->if_mtu - min_protoh; 2836df8bae1dSRodney W. Grimes } 283746f58482SJonathan Lemon 283846f58482SJonathan Lemon 283946f58482SJonathan Lemon /* 284046f58482SJonathan Lemon * Checks for partial ack. If partial ack arrives, force the retransmission 284146f58482SJonathan Lemon * of the next unacknowledged segment, do not clear tp->t_dupacks, and return 284246f58482SJonathan Lemon * 1. By setting snd_nxt to ti_ack, this forces retransmission timer to 284346f58482SJonathan Lemon * be started again. If the ack advances at least to tp->snd_recover, return 0. 284446f58482SJonathan Lemon */ 284546f58482SJonathan Lemon static int 284646f58482SJonathan Lemon tcp_newreno(tp, th) 284746f58482SJonathan Lemon struct tcpcb *tp; 284846f58482SJonathan Lemon struct tcphdr *th; 284946f58482SJonathan Lemon { 285046f58482SJonathan Lemon if (SEQ_LT(th->th_ack, tp->snd_recover)) { 285146f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 285246f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 285346f58482SJonathan Lemon 285446f58482SJonathan Lemon callout_stop(tp->tt_rexmt); 285546f58482SJonathan Lemon tp->t_rtttime = 0; 285646f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 28576b2a5f92SJayanth Vijayaraghavan /* 28586b2a5f92SJayanth Vijayaraghavan * Set snd_cwnd to one segment beyond acknowledged offset 28596b2a5f92SJayanth Vijayaraghavan * (tp->snd_una has not yet been updated when this function 28606b2a5f92SJayanth Vijayaraghavan * is called) 28616b2a5f92SJayanth Vijayaraghavan */ 28626b2a5f92SJayanth Vijayaraghavan tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 28636b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 286446f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 286546f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 286646f58482SJonathan Lemon tp->snd_nxt = onxt; 286746f58482SJonathan Lemon /* 286846f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 286946f58482SJonathan Lemon * not updated yet. 287046f58482SJonathan Lemon */ 287146f58482SJonathan Lemon tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg); 287246f58482SJonathan Lemon return (1); 287346f58482SJonathan Lemon } 287446f58482SJonathan Lemon return (0); 287546f58482SJonathan Lemon } 2876