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 13515bd2b43SDavid Greenman struct inpcbhead tcb; 136fb59c426SYoshinobu Inoue #define tcb6 tcb /* for KAME src sync over BSD*'s */ 13715bd2b43SDavid Greenman struct inpcbinfo tcbinfo; 138df8bae1dSRodney W. Grimes 1390312fbe9SPoul-Henning Kamp static void tcp_dooptions __P((struct tcpcb *, 140fb59c426SYoshinobu Inoue u_char *, int, struct tcphdr *, struct tcpopt *)); 1410312fbe9SPoul-Henning Kamp static void tcp_pulloutofband __P((struct socket *, 142fb59c426SYoshinobu Inoue struct tcphdr *, struct mbuf *, int)); 143fb59c426SYoshinobu Inoue static int tcp_reass __P((struct tcpcb *, struct tcphdr *, int *, 144fb59c426SYoshinobu Inoue struct mbuf *)); 1450312fbe9SPoul-Henning Kamp static void tcp_xmit_timer __P((struct tcpcb *, int)); 14646f58482SJonathan Lemon static int tcp_newreno __P((struct tcpcb *, struct tcphdr *)); 1470312fbe9SPoul-Henning Kamp 148fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 149fb59c426SYoshinobu Inoue #ifdef INET6 150fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \ 151fb59c426SYoshinobu Inoue do { \ 152fb59c426SYoshinobu Inoue if ((tp) && (tp)->t_inpcb && \ 153fb59c426SYoshinobu Inoue ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0 && \ 154fb59c426SYoshinobu Inoue (tp)->t_inpcb->in6p_route.ro_rt) \ 155686cdd19SJun-ichiro itojun Hagino nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL, 0); \ 156fb59c426SYoshinobu Inoue } while (0) 157fb59c426SYoshinobu Inoue #else 158fb59c426SYoshinobu Inoue #define ND6_HINT(tp) 159fb59c426SYoshinobu Inoue #endif 160df8bae1dSRodney W. Grimes 161df8bae1dSRodney W. Grimes /* 162d8c85a26SJonathan Lemon * Indicate whether this ack should be delayed. 163d8c85a26SJonathan Lemon */ 164d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \ 165d8c85a26SJonathan Lemon (tcp_delack_enabled && !callout_pending(tp->tt_delack)) 166d8c85a26SJonathan Lemon 1670312fbe9SPoul-Henning Kamp static int 168fb59c426SYoshinobu Inoue tcp_reass(tp, th, tlenp, m) 169df8bae1dSRodney W. Grimes register struct tcpcb *tp; 170fb59c426SYoshinobu Inoue register struct tcphdr *th; 171fb59c426SYoshinobu Inoue int *tlenp; 172df8bae1dSRodney W. Grimes struct mbuf *m; 173df8bae1dSRodney W. Grimes { 174fb59c426SYoshinobu Inoue struct tseg_qent *q; 175fb59c426SYoshinobu Inoue struct tseg_qent *p = NULL; 176fb59c426SYoshinobu Inoue struct tseg_qent *nq; 177fb59c426SYoshinobu Inoue struct tseg_qent *te; 178df8bae1dSRodney W. Grimes struct socket *so = tp->t_inpcb->inp_socket; 179df8bae1dSRodney W. Grimes int flags; 180df8bae1dSRodney W. Grimes 181df8bae1dSRodney W. Grimes /* 182fb59c426SYoshinobu Inoue * Call with th==0 after become established to 183df8bae1dSRodney W. Grimes * force pre-ESTABLISHED data up to user socket. 184df8bae1dSRodney W. Grimes */ 185fb59c426SYoshinobu Inoue if (th == 0) 186df8bae1dSRodney W. Grimes goto present; 187df8bae1dSRodney W. Grimes 188fb59c426SYoshinobu Inoue /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */ 189fb59c426SYoshinobu Inoue MALLOC(te, struct tseg_qent *, sizeof (struct tseg_qent), M_TSEGQ, 190fb59c426SYoshinobu Inoue M_NOWAIT); 191fb59c426SYoshinobu Inoue if (te == NULL) { 192fb59c426SYoshinobu Inoue tcpstat.tcps_rcvmemdrop++; 193fb59c426SYoshinobu Inoue m_freem(m); 194fb59c426SYoshinobu Inoue return (0); 195fb59c426SYoshinobu Inoue } 1966effc713SDoug Rabson 197df8bae1dSRodney W. Grimes /* 198df8bae1dSRodney W. Grimes * Find a segment which begins after this one does. 199df8bae1dSRodney W. Grimes */ 200fb59c426SYoshinobu Inoue LIST_FOREACH(q, &tp->t_segq, tqe_q) { 201fb59c426SYoshinobu Inoue if (SEQ_GT(q->tqe_th->th_seq, th->th_seq)) 202df8bae1dSRodney W. Grimes break; 203fb59c426SYoshinobu Inoue p = q; 204fb59c426SYoshinobu Inoue } 205df8bae1dSRodney W. Grimes 206df8bae1dSRodney W. Grimes /* 207df8bae1dSRodney W. Grimes * If there is a preceding segment, it may provide some of 208df8bae1dSRodney W. Grimes * our data already. If so, drop the data from the incoming 209df8bae1dSRodney W. Grimes * segment. If it provides all of our data, drop us. 210df8bae1dSRodney W. Grimes */ 2116effc713SDoug Rabson if (p != NULL) { 212df8bae1dSRodney W. Grimes register int i; 213df8bae1dSRodney W. Grimes /* conversion to int (in i) handles seq wraparound */ 214fb59c426SYoshinobu Inoue i = p->tqe_th->th_seq + p->tqe_len - th->th_seq; 215df8bae1dSRodney W. Grimes if (i > 0) { 216fb59c426SYoshinobu Inoue if (i >= *tlenp) { 217df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 218fb59c426SYoshinobu Inoue tcpstat.tcps_rcvdupbyte += *tlenp; 219df8bae1dSRodney W. Grimes m_freem(m); 220fb59c426SYoshinobu Inoue FREE(te, M_TSEGQ); 221a0292f23SGarrett Wollman /* 222a0292f23SGarrett Wollman * Try to present any queued data 223a0292f23SGarrett Wollman * at the left window edge to the user. 224a0292f23SGarrett Wollman * This is needed after the 3-WHS 225a0292f23SGarrett Wollman * completes. 226a0292f23SGarrett Wollman */ 227a0292f23SGarrett Wollman goto present; /* ??? */ 228df8bae1dSRodney W. Grimes } 229df8bae1dSRodney W. Grimes m_adj(m, i); 230fb59c426SYoshinobu Inoue *tlenp -= i; 231fb59c426SYoshinobu Inoue th->th_seq += i; 232df8bae1dSRodney W. Grimes } 233df8bae1dSRodney W. Grimes } 234df8bae1dSRodney W. Grimes tcpstat.tcps_rcvoopack++; 235fb59c426SYoshinobu Inoue tcpstat.tcps_rcvoobyte += *tlenp; 236df8bae1dSRodney W. Grimes 237df8bae1dSRodney W. Grimes /* 238df8bae1dSRodney W. Grimes * While we overlap succeeding segments trim them or, 239df8bae1dSRodney W. Grimes * if they are completely covered, dequeue them. 240df8bae1dSRodney W. Grimes */ 2416effc713SDoug Rabson while (q) { 242fb59c426SYoshinobu Inoue register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq; 243df8bae1dSRodney W. Grimes if (i <= 0) 244df8bae1dSRodney W. Grimes break; 245fb59c426SYoshinobu Inoue if (i < q->tqe_len) { 246fb59c426SYoshinobu Inoue q->tqe_th->th_seq += i; 247fb59c426SYoshinobu Inoue q->tqe_len -= i; 248fb59c426SYoshinobu Inoue m_adj(q->tqe_m, i); 249df8bae1dSRodney W. Grimes break; 250df8bae1dSRodney W. Grimes } 2516effc713SDoug Rabson 252fb59c426SYoshinobu Inoue nq = LIST_NEXT(q, tqe_q); 253fb59c426SYoshinobu Inoue LIST_REMOVE(q, tqe_q); 254fb59c426SYoshinobu Inoue m_freem(q->tqe_m); 255fb59c426SYoshinobu Inoue FREE(q, M_TSEGQ); 2566effc713SDoug Rabson q = nq; 257df8bae1dSRodney W. Grimes } 258df8bae1dSRodney W. Grimes 259fb59c426SYoshinobu Inoue /* Insert the new segment queue entry into place. */ 260fb59c426SYoshinobu Inoue te->tqe_m = m; 261fb59c426SYoshinobu Inoue te->tqe_th = th; 262fb59c426SYoshinobu Inoue te->tqe_len = *tlenp; 263fb59c426SYoshinobu Inoue 2646effc713SDoug Rabson if (p == NULL) { 265fb59c426SYoshinobu Inoue LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q); 2666effc713SDoug Rabson } else { 267fb59c426SYoshinobu Inoue LIST_INSERT_AFTER(p, te, tqe_q); 2686effc713SDoug Rabson } 269df8bae1dSRodney W. Grimes 270df8bae1dSRodney W. Grimes present: 271df8bae1dSRodney W. Grimes /* 272df8bae1dSRodney W. Grimes * Present data to user, advancing rcv_nxt through 273df8bae1dSRodney W. Grimes * completed sequence space. 274df8bae1dSRodney W. Grimes */ 275a0292f23SGarrett Wollman if (!TCPS_HAVEESTABLISHED(tp->t_state)) 276df8bae1dSRodney W. Grimes return (0); 277fb59c426SYoshinobu Inoue q = LIST_FIRST(&tp->t_segq); 278fb59c426SYoshinobu Inoue if (!q || q->tqe_th->th_seq != tp->rcv_nxt) 279df8bae1dSRodney W. Grimes return (0); 280df8bae1dSRodney W. Grimes do { 281fb59c426SYoshinobu Inoue tp->rcv_nxt += q->tqe_len; 282fb59c426SYoshinobu Inoue flags = q->tqe_th->th_flags & TH_FIN; 283fb59c426SYoshinobu Inoue nq = LIST_NEXT(q, tqe_q); 284fb59c426SYoshinobu Inoue LIST_REMOVE(q, tqe_q); 285df8bae1dSRodney W. Grimes if (so->so_state & SS_CANTRCVMORE) 286fb59c426SYoshinobu Inoue m_freem(q->tqe_m); 287df8bae1dSRodney W. Grimes else 288fb59c426SYoshinobu Inoue sbappend(&so->so_rcv, q->tqe_m); 289fb59c426SYoshinobu Inoue FREE(q, M_TSEGQ); 2906effc713SDoug Rabson q = nq; 291fb59c426SYoshinobu Inoue } while (q && q->tqe_th->th_seq == tp->rcv_nxt); 292fb59c426SYoshinobu Inoue ND6_HINT(tp); 293df8bae1dSRodney W. Grimes sorwakeup(so); 294df8bae1dSRodney W. Grimes return (flags); 295df8bae1dSRodney W. Grimes } 296df8bae1dSRodney W. Grimes 297df8bae1dSRodney W. Grimes /* 298df8bae1dSRodney W. Grimes * TCP input routine, follows pages 65-76 of the 299df8bae1dSRodney W. Grimes * protocol specification dated September, 1981 very closely. 300df8bae1dSRodney W. Grimes */ 301fb59c426SYoshinobu Inoue #ifdef INET6 302fb59c426SYoshinobu Inoue int 303fb59c426SYoshinobu Inoue tcp6_input(mp, offp, proto) 304fb59c426SYoshinobu Inoue struct mbuf **mp; 305fb59c426SYoshinobu Inoue int *offp, proto; 306fb59c426SYoshinobu Inoue { 307fb59c426SYoshinobu Inoue register struct mbuf *m = *mp; 308fb59c426SYoshinobu Inoue 309fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 310fb59c426SYoshinobu Inoue 311fb59c426SYoshinobu Inoue /* 312fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 313fb59c426SYoshinobu Inoue * better place to put this in? 314fb59c426SYoshinobu Inoue */ 315fb59c426SYoshinobu Inoue if (m->m_flags & M_ANYCAST6) { 316fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 317fb59c426SYoshinobu Inoue 318fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 319fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 320fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 321fb59c426SYoshinobu Inoue return IPPROTO_DONE; 322fb59c426SYoshinobu Inoue } 323fb59c426SYoshinobu Inoue 324fb59c426SYoshinobu Inoue tcp_input(m, *offp, proto); 325fb59c426SYoshinobu Inoue return IPPROTO_DONE; 326fb59c426SYoshinobu Inoue } 327fb59c426SYoshinobu Inoue #endif 328fb59c426SYoshinobu Inoue 329df8bae1dSRodney W. Grimes void 3306a800098SYoshinobu Inoue tcp_input(m, off0, proto) 331df8bae1dSRodney W. Grimes register struct mbuf *m; 3326a800098SYoshinobu Inoue int off0, proto; 333df8bae1dSRodney W. Grimes { 334fb59c426SYoshinobu Inoue register struct tcphdr *th; 335fb59c426SYoshinobu Inoue register struct ip *ip = NULL; 336fb59c426SYoshinobu Inoue register struct ipovly *ipov; 337df8bae1dSRodney W. Grimes register struct inpcb *inp; 338e79adb8eSGarrett Wollman u_char *optp = NULL; 33926f9a767SRodney W. Grimes int optlen = 0; 340df8bae1dSRodney W. Grimes int len, tlen, off; 341fb59c426SYoshinobu Inoue int drop_hdrlen; 342df8bae1dSRodney W. Grimes register struct tcpcb *tp = 0; 343fb59c426SYoshinobu Inoue register int thflags; 34426f9a767SRodney W. Grimes struct socket *so = 0; 345df8bae1dSRodney W. Grimes int todrop, acked, ourfinisacked, needoutput = 0; 346df8bae1dSRodney W. Grimes struct in_addr laddr; 347fb59c426SYoshinobu Inoue #ifdef INET6 348fb59c426SYoshinobu Inoue struct in6_addr laddr6; 349fb59c426SYoshinobu Inoue #endif 350df8bae1dSRodney W. Grimes int dropsocket = 0; 351df8bae1dSRodney W. Grimes int iss = 0; 352a0292f23SGarrett Wollman u_long tiwin; 353a0292f23SGarrett Wollman struct tcpopt to; /* options in this segment */ 354a0292f23SGarrett Wollman struct rmxp_tao *taop; /* pointer to our TAO cache entry */ 355a0292f23SGarrett Wollman struct rmxp_tao tao_noncached; /* in case there's no cached entry */ 356610ee2f9SDavid Greenman #ifdef TCPDEBUG 357610ee2f9SDavid Greenman short ostate = 0; 358610ee2f9SDavid Greenman #endif 359fb59c426SYoshinobu Inoue #ifdef INET6 360fb59c426SYoshinobu Inoue struct ip6_hdr *ip6 = NULL; 361fb59c426SYoshinobu Inoue int isipv6; 362fb59c426SYoshinobu Inoue #endif /* INET6 */ 363a57815efSBosko Milekic int rstreason; /* For badport_bandlim accounting purposes */ 364df8bae1dSRodney W. Grimes 365fb59c426SYoshinobu Inoue #ifdef INET6 366fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 367fb59c426SYoshinobu Inoue #endif 368a0292f23SGarrett Wollman bzero((char *)&to, sizeof(to)); 369a0292f23SGarrett Wollman 370df8bae1dSRodney W. Grimes tcpstat.tcps_rcvtotal++; 371fb59c426SYoshinobu Inoue 372fb59c426SYoshinobu Inoue #ifdef INET6 373fb59c426SYoshinobu Inoue if (isipv6) { 374fb59c426SYoshinobu Inoue /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */ 375fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 376fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 377fb59c426SYoshinobu Inoue if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 378fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbadsum++; 379fb59c426SYoshinobu Inoue goto drop; 380fb59c426SYoshinobu Inoue } 381fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 382fb59c426SYoshinobu Inoue } else 383fb59c426SYoshinobu Inoue #endif /* INET6 */ 384fb59c426SYoshinobu Inoue { 385df8bae1dSRodney W. Grimes /* 386df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 387df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 388df8bae1dSRodney W. Grimes */ 389fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 390df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 391fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 392fb59c426SYoshinobu Inoue } 393df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 394df8bae1dSRodney W. Grimes if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 395df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 396df8bae1dSRodney W. Grimes return; 397df8bae1dSRodney W. Grimes } 398df8bae1dSRodney W. Grimes } 399fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 400fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 401db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 402db4f9cc7SJonathan Lemon tlen = ip->ip_len; 403df8bae1dSRodney W. Grimes 404db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 405db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 406db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 407db4f9cc7SJonathan Lemon else 408db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 409db4f9cc7SJonathan Lemon ip->ip_dst.s_addr, htonl(m->m_pkthdr.csum_data + 410db4f9cc7SJonathan Lemon ip->ip_len + IPPROTO_TCP)); 411db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 412db4f9cc7SJonathan Lemon } else { 413df8bae1dSRodney W. Grimes /* 414df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 415df8bae1dSRodney W. Grimes */ 416df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 417fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 418fb59c426SYoshinobu Inoue ipov->ih_len = (u_short)tlen; 419fb59c426SYoshinobu Inoue HTONS(ipov->ih_len); 420fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 421db4f9cc7SJonathan Lemon } 422fb59c426SYoshinobu Inoue if (th->th_sum) { 423df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadsum++; 424df8bae1dSRodney W. Grimes goto drop; 425df8bae1dSRodney W. Grimes } 426fb59c426SYoshinobu Inoue #ifdef INET6 427fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 428fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 429fb59c426SYoshinobu Inoue #endif 430fb59c426SYoshinobu Inoue } 431df8bae1dSRodney W. Grimes 432df8bae1dSRodney W. Grimes /* 433df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 434df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 435df8bae1dSRodney W. Grimes */ 436fb59c426SYoshinobu Inoue off = th->th_off << 2; 437df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 438df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadoff++; 439df8bae1dSRodney W. Grimes goto drop; 440df8bae1dSRodney W. Grimes } 441fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 442df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 443fb59c426SYoshinobu Inoue #ifdef INET6 444fb59c426SYoshinobu Inoue if (isipv6) { 445fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 446fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 447fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 448fb59c426SYoshinobu Inoue } else 449fb59c426SYoshinobu Inoue #endif /* INET6 */ 450fb59c426SYoshinobu Inoue { 451df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 452df8bae1dSRodney W. Grimes if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 453df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 454df8bae1dSRodney W. Grimes return; 455df8bae1dSRodney W. Grimes } 456fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 457fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 458fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 459fb59c426SYoshinobu Inoue } 460df8bae1dSRodney W. Grimes } 461df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 462fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 463df8bae1dSRodney W. Grimes } 464fb59c426SYoshinobu Inoue thflags = th->th_flags; 465df8bae1dSRodney W. Grimes 466e46cd3d4SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN 467e46cd3d4SDag-Erling Smørgrav /* 468e46cd3d4SDag-Erling Smørgrav * If the drop_synfin option is enabled, drop all packets with 469e46cd3d4SDag-Erling Smørgrav * both the SYN and FIN bits set. This prevents e.g. nmap from 470e46cd3d4SDag-Erling Smørgrav * identifying the TCP/IP stack. 471e46cd3d4SDag-Erling Smørgrav * 472a589a70eSGarrett Wollman * This is a violation of the TCP specification. 473e46cd3d4SDag-Erling Smørgrav */ 474fb59c426SYoshinobu Inoue if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN)) 475e46cd3d4SDag-Erling Smørgrav goto drop; 476e46cd3d4SDag-Erling Smørgrav #endif 477e46cd3d4SDag-Erling Smørgrav 478df8bae1dSRodney W. Grimes /* 479df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 480df8bae1dSRodney W. Grimes */ 481fb59c426SYoshinobu Inoue NTOHL(th->th_seq); 482fb59c426SYoshinobu Inoue NTOHL(th->th_ack); 483fb59c426SYoshinobu Inoue NTOHS(th->th_win); 484fb59c426SYoshinobu Inoue NTOHS(th->th_urp); 485df8bae1dSRodney W. Grimes 486df8bae1dSRodney W. Grimes /* 487fb59c426SYoshinobu Inoue * Delay droping TCP, IP headers, IPv6 ext headers, and TCP options, 488fb59c426SYoshinobu Inoue * until after ip6_savecontrol() is called and before other functions 489fb59c426SYoshinobu Inoue * which don't want those proto headers. 490fb59c426SYoshinobu Inoue * Because ip6_savecontrol() is going to parse the mbuf to 491fb59c426SYoshinobu Inoue * search for data to be passed up to user-land, it wants mbuf 492fb59c426SYoshinobu Inoue * parameters to be unchanged. 493755c1f07SAndras Olah */ 494fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 495755c1f07SAndras Olah 496755c1f07SAndras Olah /* 497df8bae1dSRodney W. Grimes * Locate pcb for segment. 498df8bae1dSRodney W. Grimes */ 499df8bae1dSRodney W. Grimes findpcb: 500f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD 501fb59c426SYoshinobu Inoue if (ip_fw_fwd_addr != NULL 502fb59c426SYoshinobu Inoue #ifdef INET6 503fb59c426SYoshinobu Inoue && isipv6 == NULL /* IPv6 support is not yet */ 504fb59c426SYoshinobu Inoue #endif /* INET6 */ 505fb59c426SYoshinobu Inoue ) { 506f9e354dfSJulian Elischer /* 507f9e354dfSJulian Elischer * Diverted. Pretend to be the destination. 508f9e354dfSJulian Elischer * already got one like this? 509f9e354dfSJulian Elischer */ 510fb59c426SYoshinobu Inoue inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, 511fb59c426SYoshinobu Inoue ip->ip_dst, th->th_dport, 0, m->m_pkthdr.rcvif); 512f9e354dfSJulian Elischer if (!inp) { 513f9e354dfSJulian Elischer /* 514f9e354dfSJulian Elischer * No, then it's new. Try find the ambushing socket 515f9e354dfSJulian Elischer */ 516f9e354dfSJulian Elischer if (!ip_fw_fwd_addr->sin_port) { 517fb59c426SYoshinobu Inoue inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, 518fb59c426SYoshinobu Inoue th->th_sport, ip_fw_fwd_addr->sin_addr, 519fb59c426SYoshinobu Inoue th->th_dport, 1, m->m_pkthdr.rcvif); 520f9e354dfSJulian Elischer } else { 521f9e354dfSJulian Elischer inp = in_pcblookup_hash(&tcbinfo, 522fb59c426SYoshinobu Inoue ip->ip_src, th->th_sport, 523f9e354dfSJulian Elischer ip_fw_fwd_addr->sin_addr, 524cfa1ca9dSYoshinobu Inoue ntohs(ip_fw_fwd_addr->sin_port), 1, 525cfa1ca9dSYoshinobu Inoue m->m_pkthdr.rcvif); 526f9e354dfSJulian Elischer } 527f9e354dfSJulian Elischer } 528f9e354dfSJulian Elischer ip_fw_fwd_addr = NULL; 529f9e354dfSJulian Elischer } else 530f9e354dfSJulian Elischer #endif /* IPFIREWALL_FORWARD */ 531fb59c426SYoshinobu Inoue { 532fb59c426SYoshinobu Inoue #ifdef INET6 533fb59c426SYoshinobu Inoue if (isipv6) 534fb59c426SYoshinobu Inoue inp = in6_pcblookup_hash(&tcbinfo, &ip6->ip6_src, th->th_sport, 535fb59c426SYoshinobu Inoue &ip6->ip6_dst, th->th_dport, 1, 536fb59c426SYoshinobu Inoue m->m_pkthdr.rcvif); 537fb59c426SYoshinobu Inoue else 538fb59c426SYoshinobu Inoue #endif /* INET6 */ 539fb59c426SYoshinobu Inoue inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, 540fb59c426SYoshinobu Inoue ip->ip_dst, th->th_dport, 1, m->m_pkthdr.rcvif); 541fb59c426SYoshinobu Inoue } 542f9e354dfSJulian Elischer 543fb59c426SYoshinobu Inoue #ifdef IPSEC 544fb59c426SYoshinobu Inoue #ifdef INET6 545fb59c426SYoshinobu Inoue if (isipv6) { 546fb59c426SYoshinobu Inoue if (inp != NULL && ipsec6_in_reject_so(m, inp->inp_socket)) { 547fb59c426SYoshinobu Inoue ipsec6stat.in_polvio++; 548fb59c426SYoshinobu Inoue goto drop; 549fb59c426SYoshinobu Inoue } 550fb59c426SYoshinobu Inoue } else 551fb59c426SYoshinobu Inoue #endif /* INET6 */ 552fb59c426SYoshinobu Inoue if (inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) { 553fb59c426SYoshinobu Inoue ipsecstat.in_polvio++; 554fb59c426SYoshinobu Inoue goto drop; 555fb59c426SYoshinobu Inoue } 556fb59c426SYoshinobu Inoue #endif /*IPSEC*/ 557df8bae1dSRodney W. Grimes 558df8bae1dSRodney W. Grimes /* 559df8bae1dSRodney W. Grimes * If the state is CLOSED (i.e., TCB does not exist) then 560df8bae1dSRodney W. Grimes * all data in the incoming segment is discarded. 561df8bae1dSRodney W. Grimes * If the TCB exists but is in CLOSED state, it is embryonic, 562df8bae1dSRodney W. Grimes * but should either do a listen or a connect soon. 563df8bae1dSRodney W. Grimes */ 564816a3d83SPoul-Henning Kamp if (inp == NULL) { 5652e4e1b4cSGeoff Rehmet if (log_in_vain) { 566fb59c426SYoshinobu Inoue #ifdef INET6 567fb59c426SYoshinobu Inoue char dbuf[INET6_ADDRSTRLEN], sbuf[INET6_ADDRSTRLEN]; 568fb59c426SYoshinobu Inoue #else /* INET6 */ 569fb59c426SYoshinobu Inoue char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"]; 570fb59c426SYoshinobu Inoue #endif /* INET6 */ 57175cfc95fSAndrey A. Chernov 572fb59c426SYoshinobu Inoue #ifdef INET6 573fb59c426SYoshinobu Inoue if (isipv6) { 574fb59c426SYoshinobu Inoue strcpy(dbuf, ip6_sprintf(&ip6->ip6_dst)); 575fb59c426SYoshinobu Inoue strcpy(sbuf, ip6_sprintf(&ip6->ip6_src)); 576fb59c426SYoshinobu Inoue } else 577fb59c426SYoshinobu Inoue #endif 578fb59c426SYoshinobu Inoue { 579fb59c426SYoshinobu Inoue strcpy(dbuf, inet_ntoa(ip->ip_dst)); 580fb59c426SYoshinobu Inoue strcpy(sbuf, inet_ntoa(ip->ip_src)); 581fb59c426SYoshinobu Inoue } 5822e4e1b4cSGeoff Rehmet switch (log_in_vain) { 5832e4e1b4cSGeoff Rehmet case 1: 584fb59c426SYoshinobu Inoue if(thflags & TH_SYN) 585592071e8SBruce Evans log(LOG_INFO, 586592071e8SBruce Evans "Connection attempt to TCP %s:%d from %s:%d\n", 587fb59c426SYoshinobu Inoue dbuf, ntohs(th->th_dport), 588fb59c426SYoshinobu Inoue sbuf, 589fb59c426SYoshinobu Inoue ntohs(th->th_sport)); 5902e4e1b4cSGeoff Rehmet break; 5912e4e1b4cSGeoff Rehmet case 2: 5922e4e1b4cSGeoff Rehmet log(LOG_INFO, 5932e4e1b4cSGeoff Rehmet "Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n", 594fb59c426SYoshinobu Inoue dbuf, ntohs(th->th_dport), sbuf, 595fb59c426SYoshinobu Inoue ntohs(th->th_sport), thflags); 5962e4e1b4cSGeoff Rehmet break; 5972e4e1b4cSGeoff Rehmet default: 5982e4e1b4cSGeoff Rehmet break; 5992e4e1b4cSGeoff Rehmet } 60075cfc95fSAndrey A. Chernov } 6012e4e1b4cSGeoff Rehmet if (blackhole) { 6022e4e1b4cSGeoff Rehmet switch (blackhole) { 603828b7f40SGeoff Rehmet case 1: 604fb59c426SYoshinobu Inoue if (thflags & TH_SYN) 605828b7f40SGeoff Rehmet goto drop; 606828b7f40SGeoff Rehmet break; 607828b7f40SGeoff Rehmet case 2: 608828b7f40SGeoff Rehmet goto drop; 609828b7f40SGeoff Rehmet default: 610828b7f40SGeoff Rehmet goto drop; 6112e4e1b4cSGeoff Rehmet } 612828b7f40SGeoff Rehmet } 613a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 614a57815efSBosko Milekic goto dropwithreset; 615816a3d83SPoul-Henning Kamp } 616df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 61709f81a46SBosko Milekic if (tp == 0) { 618a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 619a57815efSBosko Milekic goto dropwithreset; 62009f81a46SBosko Milekic } 621df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_CLOSED) 622df8bae1dSRodney W. Grimes goto drop; 623df8bae1dSRodney W. Grimes 624df8bae1dSRodney W. Grimes /* Unscale the window into a 32-bit value. */ 625fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) == 0) 626fb59c426SYoshinobu Inoue tiwin = th->th_win << tp->snd_scale; 627df8bae1dSRodney W. Grimes else 628fb59c426SYoshinobu Inoue tiwin = th->th_win; 629fb59c426SYoshinobu Inoue 630fb59c426SYoshinobu Inoue #ifdef INET6 631fb59c426SYoshinobu Inoue /* save packet options if user wanted */ 6324739b807SYoshinobu Inoue if (isipv6 && inp->in6p_flags & INP_CONTROLOPTS) { 633fb59c426SYoshinobu Inoue if (inp->in6p_options) { 634fb59c426SYoshinobu Inoue m_freem(inp->in6p_options); 635fb59c426SYoshinobu Inoue inp->in6p_options = 0; 636fb59c426SYoshinobu Inoue } 637fb59c426SYoshinobu Inoue ip6_savecontrol(inp, &inp->in6p_options, ip6, m); 638fb59c426SYoshinobu Inoue } 6398972cdb1SYoshinobu Inoue /* else, should also do ip_srcroute() here? */ 640fb59c426SYoshinobu Inoue #endif /* INET6 */ 641df8bae1dSRodney W. Grimes 642df8bae1dSRodney W. Grimes so = inp->inp_socket; 643df8bae1dSRodney W. Grimes if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) { 644610ee2f9SDavid Greenman #ifdef TCPDEBUG 645df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 646df8bae1dSRodney W. Grimes ostate = tp->t_state; 647fb59c426SYoshinobu Inoue #ifdef INET6 648fb59c426SYoshinobu Inoue if (isipv6) 649fb59c426SYoshinobu Inoue bcopy((char *)ip6, (char *)tcp_saveipgen, 650fb59c426SYoshinobu Inoue sizeof(*ip6)); 651fb59c426SYoshinobu Inoue else 652fb59c426SYoshinobu Inoue #endif /* INET6 */ 653fb59c426SYoshinobu Inoue bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 654fb59c426SYoshinobu Inoue tcp_savetcp = *th; 655df8bae1dSRodney W. Grimes } 656610ee2f9SDavid Greenman #endif 657df8bae1dSRodney W. Grimes if (so->so_options & SO_ACCEPTCONN) { 658a0292f23SGarrett Wollman register struct tcpcb *tp0 = tp; 6594195b4afSPaul Traina struct socket *so2; 660fb59c426SYoshinobu Inoue #ifdef IPSEC 661fb59c426SYoshinobu Inoue struct socket *oso; 662fb59c426SYoshinobu Inoue #endif 663fb59c426SYoshinobu Inoue #ifdef INET6 664fb59c426SYoshinobu Inoue struct inpcb *oinp = sotoinpcb(so); 665fb59c426SYoshinobu Inoue #endif /* INET6 */ 666fb59c426SYoshinobu Inoue 667fb59c426SYoshinobu Inoue #ifndef IPSEC 668fb59c426SYoshinobu Inoue /* 669fb59c426SYoshinobu Inoue * Current IPsec implementation makes incorrect IPsec 670fb59c426SYoshinobu Inoue * cache if this check is done here. 671fb59c426SYoshinobu Inoue * So delay this until duplicated socket is created. 672fb59c426SYoshinobu Inoue */ 673fb59c426SYoshinobu Inoue if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 6744195b4afSPaul Traina /* 675ebb0cbeaSPaul Traina * Note: dropwithreset makes sure we don't 676ebb0cbeaSPaul Traina * send a RST in response to a RST. 6774195b4afSPaul Traina */ 678fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 679ebb0cbeaSPaul Traina tcpstat.tcps_badsyn++; 680a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 681a57815efSBosko Milekic goto dropwithreset; 6824195b4afSPaul Traina } 683ebb0cbeaSPaul Traina goto drop; 684ebb0cbeaSPaul Traina } 685fb59c426SYoshinobu Inoue #endif 686ebb0cbeaSPaul Traina so2 = sonewconn(so, 0); 687ebb0cbeaSPaul Traina if (so2 == 0) { 68865f28919SJesper Skriver /* 68965f28919SJesper Skriver * If we were unable to create a new socket 69065f28919SJesper Skriver * for this SYN, we call sodropablereq to 69165f28919SJesper Skriver * see if there are any other sockets we 69265f28919SJesper Skriver * can kick out of the listen queue. If 69365f28919SJesper Skriver * so, we'll silently drop the socket 69465f28919SJesper Skriver * sodropablereq told us to drop and 69565f28919SJesper Skriver * create a new one. 69665f28919SJesper Skriver * 69765f28919SJesper Skriver * If sodropablereq returns 0, we'll 69865f28919SJesper Skriver * simply drop the incoming SYN, as we 69965f28919SJesper Skriver * can not allocate a socket for it. 70065f28919SJesper Skriver */ 701ebb0cbeaSPaul Traina tcpstat.tcps_listendrop++; 702ebb0cbeaSPaul Traina so2 = sodropablereq(so); 703a51764a8SPaul Traina if (so2) { 704e7f32693SJayanth Vijayaraghavan if (tcp_lq_overflow) 705e7f32693SJayanth Vijayaraghavan sototcpcb(so2)->t_flags |= 706e7f32693SJayanth Vijayaraghavan TF_LQ_OVERFLOW; 70765f28919SJesper Skriver tcp_close(sototcpcb(so2)); 708a51764a8SPaul Traina so2 = sonewconn(so, 0); 709a51764a8SPaul Traina } 710a51764a8SPaul Traina if (!so2) 711df8bae1dSRodney W. Grimes goto drop; 7121347f5b8SGuido van Rooij } 713fb59c426SYoshinobu Inoue #ifdef IPSEC 714fb59c426SYoshinobu Inoue oso = so; 715fb59c426SYoshinobu Inoue #endif 7164195b4afSPaul Traina so = so2; 717df8bae1dSRodney W. Grimes /* 718df8bae1dSRodney W. Grimes * This is ugly, but .... 719df8bae1dSRodney W. Grimes * 720df8bae1dSRodney W. Grimes * Mark socket as temporary until we're 721df8bae1dSRodney W. Grimes * committed to keeping it. The code at 722df8bae1dSRodney W. Grimes * ``drop'' and ``dropwithreset'' check the 723df8bae1dSRodney W. Grimes * flag dropsocket to see if the temporary 724df8bae1dSRodney W. Grimes * socket created here should be discarded. 725df8bae1dSRodney W. Grimes * We mark the socket as discardable until 726df8bae1dSRodney W. Grimes * we're committed to it below in TCPS_LISTEN. 727df8bae1dSRodney W. Grimes */ 728df8bae1dSRodney W. Grimes dropsocket++; 729df8bae1dSRodney W. Grimes inp = (struct inpcb *)so->so_pcb; 730fb59c426SYoshinobu Inoue #ifdef INET6 731fb59c426SYoshinobu Inoue if (isipv6) 732fb59c426SYoshinobu Inoue inp->in6p_laddr = ip6->ip6_dst; 733fb59c426SYoshinobu Inoue else { 734fdaf052eSYoshinobu Inoue if ((inp->inp_flags & IN6P_BINDV6ONLY) == 0) { 735fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 736fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 737fb59c426SYoshinobu Inoue } 738fb59c426SYoshinobu Inoue #endif /* INET6 */ 739fb59c426SYoshinobu Inoue inp->inp_laddr = ip->ip_dst; 740fb59c426SYoshinobu Inoue #ifdef INET6 741fb59c426SYoshinobu Inoue } 742fb59c426SYoshinobu Inoue #endif /* INET6 */ 743fb59c426SYoshinobu Inoue inp->inp_lport = th->th_dport; 744c3229e05SDavid Greenman if (in_pcbinshash(inp) != 0) { 745c3229e05SDavid Greenman /* 7465a8c77a8SDavid E. O'Brien * Undo the assignments above if we failed to 7475a8c77a8SDavid E. O'Brien * put the PCB on the hash lists. 748c3229e05SDavid Greenman */ 749fb59c426SYoshinobu Inoue #ifdef INET6 750fb59c426SYoshinobu Inoue if (isipv6) 751fb59c426SYoshinobu Inoue inp->in6p_laddr = in6addr_any; 752fb59c426SYoshinobu Inoue else 753fb59c426SYoshinobu Inoue #endif /* INET6 */ 754c3229e05SDavid Greenman inp->inp_laddr.s_addr = INADDR_ANY; 755c3229e05SDavid Greenman inp->inp_lport = 0; 756c3229e05SDavid Greenman goto drop; 757c3229e05SDavid Greenman } 758fb59c426SYoshinobu Inoue #ifdef IPSEC 759fb59c426SYoshinobu Inoue /* 760fb59c426SYoshinobu Inoue * To avoid creating incorrectly cached IPsec 761fb59c426SYoshinobu Inoue * association, this is need to be done here. 762fb59c426SYoshinobu Inoue * 763fb59c426SYoshinobu Inoue * Subject: (KAME-snap 748) 764fb59c426SYoshinobu Inoue * From: Wayne Knowles <w.knowles@niwa.cri.nz> 765fb59c426SYoshinobu Inoue * ftp://ftp.kame.net/pub/mail-list/snap-users/748 766fb59c426SYoshinobu Inoue */ 767fb59c426SYoshinobu Inoue if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 768fb59c426SYoshinobu Inoue /* 769fb59c426SYoshinobu Inoue * Note: dropwithreset makes sure we don't 770fb59c426SYoshinobu Inoue * send a RST in response to a RST. 771fb59c426SYoshinobu Inoue */ 772fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 773fb59c426SYoshinobu Inoue tcpstat.tcps_badsyn++; 774a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 775a57815efSBosko Milekic goto dropwithreset; 776fb59c426SYoshinobu Inoue } 777fb59c426SYoshinobu Inoue goto drop; 778fb59c426SYoshinobu Inoue } 779fb59c426SYoshinobu Inoue #endif 780fb59c426SYoshinobu Inoue #ifdef INET6 781fb59c426SYoshinobu Inoue if (isipv6) { 782fb59c426SYoshinobu Inoue /* 783fb59c426SYoshinobu Inoue * inherit socket options from the listening 784fb59c426SYoshinobu Inoue * socket. 785fb59c426SYoshinobu Inoue */ 786fb59c426SYoshinobu Inoue inp->inp_flags |= 787fb59c426SYoshinobu Inoue oinp->inp_flags & INP_CONTROLOPTS; 788fb59c426SYoshinobu Inoue if (inp->inp_flags & INP_CONTROLOPTS) { 789fb59c426SYoshinobu Inoue if (inp->in6p_options) { 790fb59c426SYoshinobu Inoue m_freem(inp->in6p_options); 791fb59c426SYoshinobu Inoue inp->in6p_options = 0; 792fb59c426SYoshinobu Inoue } 793fb59c426SYoshinobu Inoue ip6_savecontrol(inp, 794fb59c426SYoshinobu Inoue &inp->in6p_options, 795fb59c426SYoshinobu Inoue ip6, m); 796fb59c426SYoshinobu Inoue } 797fb59c426SYoshinobu Inoue } else 798fb59c426SYoshinobu Inoue #endif /* INET6 */ 799df8bae1dSRodney W. Grimes inp->inp_options = ip_srcroute(); 800fb59c426SYoshinobu Inoue #ifdef IPSEC 801fb59c426SYoshinobu Inoue /* copy old policy into new socket's */ 802fb59c426SYoshinobu Inoue if (ipsec_copy_policy(sotoinpcb(oso)->inp_sp, 803fb59c426SYoshinobu Inoue inp->inp_sp)) 804fb59c426SYoshinobu Inoue printf("tcp_input: could not copy policy\n"); 805fb59c426SYoshinobu Inoue #endif 806df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 807df8bae1dSRodney W. Grimes tp->t_state = TCPS_LISTEN; 808a0292f23SGarrett Wollman tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT); 809df8bae1dSRodney W. Grimes 810a0292f23SGarrett Wollman /* Compute proper scaling value from buffer space */ 811df8bae1dSRodney W. Grimes while (tp->request_r_scale < TCP_MAX_WINSHIFT && 8125a8c77a8SDavid E. O'Brien TCP_MAXWIN << tp->request_r_scale < 8135a8c77a8SDavid E. O'Brien so->so_rcv.sb_hiwat) 814df8bae1dSRodney W. Grimes tp->request_r_scale++; 815df8bae1dSRodney W. Grimes } 816df8bae1dSRodney W. Grimes } 817df8bae1dSRodney W. Grimes 818df8bae1dSRodney W. Grimes /* 819df8bae1dSRodney W. Grimes * Segment received on connection. 820df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 821df8bae1dSRodney W. Grimes */ 8229b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 8237ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 8249b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp); 825df8bae1dSRodney W. Grimes 826df8bae1dSRodney W. Grimes /* 827df8bae1dSRodney W. Grimes * Process options if not in LISTEN state, 828df8bae1dSRodney W. Grimes * else do it below (after getting remote address). 829df8bae1dSRodney W. Grimes */ 830f9d5a964SDavid Greenman if (tp->t_state != TCPS_LISTEN) 831fb59c426SYoshinobu Inoue tcp_dooptions(tp, optp, optlen, th, &to); 832df8bae1dSRodney W. Grimes 833df8bae1dSRodney W. Grimes /* 834df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 835df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 836df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 837df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 838df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 839df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 840df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 841df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 842df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 843df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 844df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 845df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 846a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 847a0292f23SGarrett Wollman * Since we check for TCPS_ESTABLISHED above, it can only 848a0292f23SGarrett Wollman * be TH_NEEDSYN. 849df8bae1dSRodney W. Grimes */ 850df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 851fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 852a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 853a0292f23SGarrett Wollman ((to.to_flag & TOF_TS) == 0 || 854a0292f23SGarrett Wollman TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 855a0292f23SGarrett Wollman /* 856a0292f23SGarrett Wollman * Using the CC option is compulsory if once started: 857a0292f23SGarrett Wollman * the segment is OK if no T/TCP was negotiated or 858a0292f23SGarrett Wollman * if the segment has a CC option equal to CCrecv 859a0292f23SGarrett Wollman */ 860a0292f23SGarrett Wollman ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) || 861831a80b0SMatthew Dillon ((to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) && 862fb59c426SYoshinobu Inoue th->th_seq == tp->rcv_nxt && 863df8bae1dSRodney W. Grimes tiwin && tiwin == tp->snd_wnd && 864df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) { 865df8bae1dSRodney W. Grimes 866df8bae1dSRodney W. Grimes /* 867df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 868df8bae1dSRodney W. Grimes * record the timestamp. 869a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 870a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 871df8bae1dSRodney W. Grimes */ 872a0292f23SGarrett Wollman if ((to.to_flag & TOF_TS) != 0 && 873fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 8749b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 875a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 876df8bae1dSRodney W. Grimes } 877df8bae1dSRodney W. Grimes 878fb59c426SYoshinobu Inoue if (tlen == 0) { 879fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 880fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 881233e8c18SGarrett Wollman tp->snd_cwnd >= tp->snd_wnd && 882233e8c18SGarrett Wollman tp->t_dupacks < tcprexmtthresh) { 883df8bae1dSRodney W. Grimes /* 884df8bae1dSRodney W. Grimes * this is a pure ack for outstanding data. 885df8bae1dSRodney W. Grimes */ 886df8bae1dSRodney W. Grimes ++tcpstat.tcps_predack; 8879b8b58e0SJonathan Lemon /* 8889b8b58e0SJonathan Lemon * "bad retransmit" recovery 8899b8b58e0SJonathan Lemon */ 8909b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 8919b8b58e0SJonathan Lemon ticks < tp->t_badrxtwin) { 8929b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 8939b8b58e0SJonathan Lemon tp->snd_ssthresh = 8949b8b58e0SJonathan Lemon tp->snd_ssthresh_prev; 8959b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 8969b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; 8979b8b58e0SJonathan Lemon } 898a0292f23SGarrett Wollman if ((to.to_flag & TOF_TS) != 0) 899a0292f23SGarrett Wollman tcp_xmit_timer(tp, 9009b8b58e0SJonathan Lemon ticks - to.to_tsecr + 1); 9019b8b58e0SJonathan Lemon else if (tp->t_rtttime && 902fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->t_rtseq)) 9039b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 904fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 905df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 906df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 907df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 908fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 909df8bae1dSRodney W. Grimes m_freem(m); 910fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 911df8bae1dSRodney W. Grimes 912df8bae1dSRodney W. Grimes /* 913df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 914df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 915df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 916df8bae1dSRodney W. Grimes * If process is waiting for space, 917df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 918df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 919df8bae1dSRodney W. Grimes * decide between more output or persist. 920df8bae1dSRodney W. Grimes */ 921df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 9229b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 9239b8b58e0SJonathan Lemon else if (!callout_active(tp->tt_persist)) 9249b8b58e0SJonathan Lemon callout_reset(tp->tt_rexmt, 9259b8b58e0SJonathan Lemon tp->t_rxtcur, 9269b8b58e0SJonathan Lemon tcp_timer_rexmt, tp); 927df8bae1dSRodney W. Grimes 928df8bae1dSRodney W. Grimes sowwakeup(so); 929df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 930df8bae1dSRodney W. Grimes (void) tcp_output(tp); 931df8bae1dSRodney W. Grimes return; 932df8bae1dSRodney W. Grimes } 933fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 934fb59c426SYoshinobu Inoue LIST_EMPTY(&tp->t_segq) && 935fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 936df8bae1dSRodney W. Grimes /* 937df8bae1dSRodney W. Grimes * this is a pure, in-sequence data packet 938df8bae1dSRodney W. Grimes * with nothing on the reassembly queue and 939df8bae1dSRodney W. Grimes * we have enough buffer space to take it. 940df8bae1dSRodney W. Grimes */ 941df8bae1dSRodney W. Grimes ++tcpstat.tcps_preddat; 942fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 943df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpack++; 944fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyte += tlen; 945fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 946df8bae1dSRodney W. Grimes /* 947755c1f07SAndras Olah * Add data to socket buffer. 948df8bae1dSRodney W. Grimes */ 949fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 950df8bae1dSRodney W. Grimes sbappend(&so->so_rcv, m); 951df8bae1dSRodney W. Grimes sorwakeup(so); 952d8c85a26SJonathan Lemon if (DELAY_ACK(tp)) { 9539b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 9549b8b58e0SJonathan Lemon tcp_timer_delack, tp); 955f498eeeeSDavid Greenman } else { 956e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 957e612a582SDavid Greenman tcp_output(tp); 958e612a582SDavid Greenman } 959df8bae1dSRodney W. Grimes return; 960df8bae1dSRodney W. Grimes } 961df8bae1dSRodney W. Grimes } 962df8bae1dSRodney W. Grimes 963df8bae1dSRodney W. Grimes /* 964df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 965df8bae1dSRodney W. Grimes * and then do TCP input processing. 966df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 967df8bae1dSRodney W. Grimes * but not less than advertised window. 968df8bae1dSRodney W. Grimes */ 969df8bae1dSRodney W. Grimes { int win; 970df8bae1dSRodney W. Grimes 971df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 972df8bae1dSRodney W. Grimes if (win < 0) 973df8bae1dSRodney W. Grimes win = 0; 97466e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 975df8bae1dSRodney W. Grimes } 976df8bae1dSRodney W. Grimes 977df8bae1dSRodney W. Grimes switch (tp->t_state) { 978df8bae1dSRodney W. Grimes 979df8bae1dSRodney W. Grimes /* 980df8bae1dSRodney W. Grimes * If the state is LISTEN then ignore segment if it contains an RST. 981df8bae1dSRodney W. Grimes * If the segment contains an ACK then it is bad and send a RST. 982df8bae1dSRodney W. Grimes * If it does not contain a SYN then it is not interesting; drop it. 983764d8cefSBill Fenner * If it is from this socket, drop it, it must be forged. 984df8bae1dSRodney W. Grimes * Don't bother responding if the destination was a broadcast. 985df8bae1dSRodney W. Grimes * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 986df8bae1dSRodney W. Grimes * tp->iss, and send a segment: 987df8bae1dSRodney W. Grimes * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 988df8bae1dSRodney W. Grimes * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 989df8bae1dSRodney W. Grimes * Fill in remote peer address fields if not previously specified. 990df8bae1dSRodney W. Grimes * Enter SYN_RECEIVED state, and process any other fields of this 991df8bae1dSRodney W. Grimes * segment in this state. 992df8bae1dSRodney W. Grimes */ 993df8bae1dSRodney W. Grimes case TCPS_LISTEN: { 994df8bae1dSRodney W. Grimes register struct sockaddr_in *sin; 995fb59c426SYoshinobu Inoue #ifdef INET6 996fb59c426SYoshinobu Inoue register struct sockaddr_in6 *sin6; 997fb59c426SYoshinobu Inoue #endif 998df8bae1dSRodney W. Grimes 999fb59c426SYoshinobu Inoue if (thflags & TH_RST) 1000df8bae1dSRodney W. Grimes goto drop; 1001a57815efSBosko Milekic if (thflags & TH_ACK) { 1002a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1003a57815efSBosko Milekic goto dropwithreset; 1004a57815efSBosko Milekic } 1005fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) == 0) 1006df8bae1dSRodney W. Grimes goto drop; 1007fb59c426SYoshinobu Inoue if (th->th_dport == th->th_sport) { 1008fb59c426SYoshinobu Inoue #ifdef INET6 1009fb59c426SYoshinobu Inoue if (isipv6) { 1010fb59c426SYoshinobu Inoue if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 1011fb59c426SYoshinobu Inoue &ip6->ip6_src)) 1012764d8cefSBill Fenner goto drop; 1013fb59c426SYoshinobu Inoue } else 1014fb59c426SYoshinobu Inoue #endif /* INET6 */ 1015fb59c426SYoshinobu Inoue if (ip->ip_dst.s_addr == ip->ip_src.s_addr) 1016fb59c426SYoshinobu Inoue goto drop; 1017fb59c426SYoshinobu Inoue } 1018df8bae1dSRodney W. Grimes /* 1019df8bae1dSRodney W. Grimes * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 1020df8bae1dSRodney W. Grimes * in_broadcast() should never return true on a received 1021df8bae1dSRodney W. Grimes * packet with M_BCAST not set. 1022173c0f9fSWarner Losh * 1023173c0f9fSWarner Losh * Packets with a multicast source address should also 1024173c0f9fSWarner Losh * be discarded. 1025df8bae1dSRodney W. Grimes */ 1026fb59c426SYoshinobu Inoue if (m->m_flags & (M_BCAST|M_MCAST)) 1027df8bae1dSRodney W. Grimes goto drop; 1028fb59c426SYoshinobu Inoue #ifdef INET6 1029fb59c426SYoshinobu Inoue if (isipv6) { 1030173c0f9fSWarner Losh if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1031173c0f9fSWarner Losh IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 1032fb59c426SYoshinobu Inoue goto drop; 1033fb59c426SYoshinobu Inoue } else 1034fb59c426SYoshinobu Inoue #endif 1035173c0f9fSWarner Losh if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1036173c0f9fSWarner Losh IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 1037173c0f9fSWarner Losh ip->ip_src.s_addr == htonl(INADDR_BROADCAST)) 1038fb59c426SYoshinobu Inoue goto drop; 1039fb59c426SYoshinobu Inoue #ifdef INET6 1040fb59c426SYoshinobu Inoue if (isipv6) { 1041fb59c426SYoshinobu Inoue MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, 10427cc0979fSDavid Malone M_SONAME, M_NOWAIT | M_ZERO); 1043fb59c426SYoshinobu Inoue if (sin6 == NULL) 1044fb59c426SYoshinobu Inoue goto drop; 1045fb59c426SYoshinobu Inoue sin6->sin6_family = AF_INET6; 1046fb59c426SYoshinobu Inoue sin6->sin6_len = sizeof(*sin6); 1047fb59c426SYoshinobu Inoue sin6->sin6_addr = ip6->ip6_src; 1048fb59c426SYoshinobu Inoue sin6->sin6_port = th->th_sport; 1049fb59c426SYoshinobu Inoue laddr6 = inp->in6p_laddr; 1050fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 1051fb59c426SYoshinobu Inoue inp->in6p_laddr = ip6->ip6_dst; 1052fb59c426SYoshinobu Inoue if (in6_pcbconnect(inp, (struct sockaddr *)sin6, 1053fb59c426SYoshinobu Inoue &proc0)) { 1054fb59c426SYoshinobu Inoue inp->in6p_laddr = laddr6; 1055fb59c426SYoshinobu Inoue FREE(sin6, M_SONAME); 1056fb59c426SYoshinobu Inoue goto drop; 1057fb59c426SYoshinobu Inoue } 1058fb59c426SYoshinobu Inoue FREE(sin6, M_SONAME); 1059fb59c426SYoshinobu Inoue } else 1060fb59c426SYoshinobu Inoue #endif 1061fb59c426SYoshinobu Inoue { 106257bf258eSGarrett Wollman MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, 106357bf258eSGarrett Wollman M_NOWAIT); 106457bf258eSGarrett Wollman if (sin == NULL) 1065df8bae1dSRodney W. Grimes goto drop; 1066df8bae1dSRodney W. Grimes sin->sin_family = AF_INET; 1067df8bae1dSRodney W. Grimes sin->sin_len = sizeof(*sin); 1068fb59c426SYoshinobu Inoue sin->sin_addr = ip->ip_src; 1069fb59c426SYoshinobu Inoue sin->sin_port = th->th_sport; 1070df8bae1dSRodney W. Grimes bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero)); 1071df8bae1dSRodney W. Grimes laddr = inp->inp_laddr; 1072df8bae1dSRodney W. Grimes if (inp->inp_laddr.s_addr == INADDR_ANY) 1073fb59c426SYoshinobu Inoue inp->inp_laddr = ip->ip_dst; 107457bf258eSGarrett Wollman if (in_pcbconnect(inp, (struct sockaddr *)sin, &proc0)) { 1075df8bae1dSRodney W. Grimes inp->inp_laddr = laddr; 107657bf258eSGarrett Wollman FREE(sin, M_SONAME); 1077df8bae1dSRodney W. Grimes goto drop; 1078df8bae1dSRodney W. Grimes } 107957bf258eSGarrett Wollman FREE(sin, M_SONAME); 1080fb59c426SYoshinobu Inoue } 1081df8bae1dSRodney W. Grimes tp->t_template = tcp_template(tp); 1082df8bae1dSRodney W. Grimes if (tp->t_template == 0) { 1083df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ENOBUFS); 1084df8bae1dSRodney W. Grimes dropsocket = 0; /* socket is already gone */ 1085df8bae1dSRodney W. Grimes goto drop; 1086df8bae1dSRodney W. Grimes } 1087a0292f23SGarrett Wollman if ((taop = tcp_gettaocache(inp)) == NULL) { 1088a0292f23SGarrett Wollman taop = &tao_noncached; 1089a0292f23SGarrett Wollman bzero(taop, sizeof(*taop)); 1090a0292f23SGarrett Wollman } 1091fb59c426SYoshinobu Inoue tcp_dooptions(tp, optp, optlen, th, &to); 1092df8bae1dSRodney W. Grimes if (iss) 1093df8bae1dSRodney W. Grimes tp->iss = iss; 1094f0a04f3fSKris Kennaway else { 1095f0a04f3fSKris Kennaway tp->iss = tcp_rndiss_next(); 1096f0a04f3fSKris Kennaway } 1097fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1098df8bae1dSRodney W. Grimes tcp_sendseqinit(tp); 1099df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 110046f58482SJonathan Lemon tp->snd_recover = tp->snd_una; 1101a0292f23SGarrett Wollman /* 1102a0292f23SGarrett Wollman * Initialization of the tcpcb for transaction; 1103a0292f23SGarrett Wollman * set SND.WND = SEG.WND, 1104a0292f23SGarrett Wollman * initialize CCsend and CCrecv. 1105a0292f23SGarrett Wollman */ 1106a0292f23SGarrett Wollman tp->snd_wnd = tiwin; /* initial send-window */ 1107a0292f23SGarrett Wollman tp->cc_send = CC_INC(tcp_ccgen); 1108a0292f23SGarrett Wollman tp->cc_recv = to.to_cc; 1109a0292f23SGarrett Wollman /* 1110a0292f23SGarrett Wollman * Perform TAO test on incoming CC (SEG.CC) option, if any. 1111a0292f23SGarrett Wollman * - compare SEG.CC against cached CC from the same host, 1112a0292f23SGarrett Wollman * if any. 1113a0292f23SGarrett Wollman * - if SEG.CC > chached value, SYN must be new and is accepted 1114a0292f23SGarrett Wollman * immediately: save new CC in the cache, mark the socket 1115a0292f23SGarrett Wollman * connected, enter ESTABLISHED state, turn on flag to 1116a0292f23SGarrett Wollman * send a SYN in the next segment. 1117a0292f23SGarrett Wollman * A virtual advertised window is set in rcv_adv to 1118a0292f23SGarrett Wollman * initialize SWS prevention. Then enter normal segment 1119a0292f23SGarrett Wollman * processing: drop SYN, process data and FIN. 1120a0292f23SGarrett Wollman * - otherwise do a normal 3-way handshake. 1121a0292f23SGarrett Wollman */ 1122a0292f23SGarrett Wollman if ((to.to_flag & TOF_CC) != 0) { 1123068373b6SGuido van Rooij if (((tp->t_flags & TF_NOPUSH) != 0) && 112411ad4550SGuido van Rooij taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) { 112511ad4550SGuido van Rooij 1126a0292f23SGarrett Wollman taop->tao_cc = to.to_cc; 11279b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1128a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1129a0292f23SGarrett Wollman 1130a0292f23SGarrett Wollman /* 1131a0292f23SGarrett Wollman * If there is a FIN, or if there is data and the 1132a0292f23SGarrett Wollman * connection is local, then delay SYN,ACK(SYN) in 1133a0292f23SGarrett Wollman * the hope of piggy-backing it on a response 1134a0292f23SGarrett Wollman * segment. Otherwise must send ACK now in case 1135a0292f23SGarrett Wollman * the other side is slow starting. 1136a0292f23SGarrett Wollman */ 1137d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && ((thflags & TH_FIN) || 1138fb59c426SYoshinobu Inoue (tlen != 0 && 1139fb59c426SYoshinobu Inoue #ifdef INET6 1140fb59c426SYoshinobu Inoue ((isipv6 && in6_localaddr(&inp->in6p_faddr)) 1141fb59c426SYoshinobu Inoue || 1142fb59c426SYoshinobu Inoue (!isipv6 && 1143fb59c426SYoshinobu Inoue #endif 1144fb59c426SYoshinobu Inoue in_localaddr(inp->inp_faddr) 1145fb59c426SYoshinobu Inoue #ifdef INET6 1146fb59c426SYoshinobu Inoue )) 1147fb59c426SYoshinobu Inoue #endif 1148fb59c426SYoshinobu Inoue ))) { 11499b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 11509b8b58e0SJonathan Lemon tcp_timer_delack, tp); 11519b8b58e0SJonathan Lemon tp->t_flags |= TF_NEEDSYN; 11529b8b58e0SJonathan Lemon } else 1153a0292f23SGarrett Wollman tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 115407e43e10SAndras Olah 115507e43e10SAndras Olah /* 115607e43e10SAndras Olah * Limit the `virtual advertised window' to TCP_MAXWIN 115707e43e10SAndras Olah * here. Even if we requested window scaling, it will 115807e43e10SAndras Olah * become effective only later when our SYN is acked. 115907e43e10SAndras Olah */ 116007e43e10SAndras Olah tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN); 1161a0292f23SGarrett Wollman tcpstat.tcps_connects++; 1162a0292f23SGarrett Wollman soisconnected(so); 11639b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepinit, 11649b8b58e0SJonathan Lemon tcp_timer_keep, tp); 1165a0292f23SGarrett Wollman dropsocket = 0; /* committed to socket */ 1166a0292f23SGarrett Wollman tcpstat.tcps_accepts++; 1167a0292f23SGarrett Wollman goto trimthenstep6; 1168a0292f23SGarrett Wollman } 1169a0292f23SGarrett Wollman /* else do standard 3-way handshake */ 1170a0292f23SGarrett Wollman } else { 1171a0292f23SGarrett Wollman /* 1172a0292f23SGarrett Wollman * No CC option, but maybe CC.NEW: 1173a0292f23SGarrett Wollman * invalidate cached value. 1174a0292f23SGarrett Wollman */ 1175a0292f23SGarrett Wollman taop->tao_cc = 0; 1176a0292f23SGarrett Wollman } 1177a0292f23SGarrett Wollman /* 1178a0292f23SGarrett Wollman * TAO test failed or there was no CC option, 1179a0292f23SGarrett Wollman * do a standard 3-way handshake. 1180a0292f23SGarrett Wollman */ 1181df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1182df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 11839b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 1184df8bae1dSRodney W. Grimes dropsocket = 0; /* committed to socket */ 1185df8bae1dSRodney W. Grimes tcpstat.tcps_accepts++; 1186df8bae1dSRodney W. Grimes goto trimthenstep6; 1187df8bae1dSRodney W. Grimes } 1188df8bae1dSRodney W. Grimes 1189df8bae1dSRodney W. Grimes /* 1190764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1191764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1192764d8cefSBill Fenner */ 1193764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1194fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1195fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1196a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1197a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1198a57815efSBosko Milekic goto dropwithreset; 1199a57815efSBosko Milekic } 1200764d8cefSBill Fenner break; 1201764d8cefSBill Fenner 1202764d8cefSBill Fenner /* 1203df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1204df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1205df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1206df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1207df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1208df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1209df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1210df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1211df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1212df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1213df8bae1dSRodney W. Grimes */ 1214df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1215a0292f23SGarrett Wollman if ((taop = tcp_gettaocache(inp)) == NULL) { 1216a0292f23SGarrett Wollman taop = &tao_noncached; 1217a0292f23SGarrett Wollman bzero(taop, sizeof(*taop)); 1218a0292f23SGarrett Wollman } 1219a0292f23SGarrett Wollman 1220fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1221fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1222fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1223a0292f23SGarrett Wollman /* 1224a0292f23SGarrett Wollman * If we have a cached CCsent for the remote host, 1225a0292f23SGarrett Wollman * hence we haven't just crashed and restarted, 1226a0292f23SGarrett Wollman * do not send a RST. This may be a retransmission 1227a0292f23SGarrett Wollman * from the other side after our earlier ACK was lost. 1228a0292f23SGarrett Wollman * Our new SYN, when it arrives, will serve as the 1229a0292f23SGarrett Wollman * needed ACK. 1230a0292f23SGarrett Wollman */ 1231a0292f23SGarrett Wollman if (taop->tao_ccsent != 0) 1232a0292f23SGarrett Wollman goto drop; 1233a57815efSBosko Milekic else { 1234a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1235a0292f23SGarrett Wollman goto dropwithreset; 1236a0292f23SGarrett Wollman } 1237a57815efSBosko Milekic } 1238fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 1239fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1240df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 1241df8bae1dSRodney W. Grimes goto drop; 1242df8bae1dSRodney W. Grimes } 1243fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) == 0) 1244df8bae1dSRodney W. Grimes goto drop; 1245fb59c426SYoshinobu Inoue tp->snd_wnd = th->th_win; /* initial send window */ 1246a0292f23SGarrett Wollman tp->cc_recv = to.to_cc; /* foreign CC */ 1247a0292f23SGarrett Wollman 1248fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1249df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1250fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1251a0292f23SGarrett Wollman /* 1252a0292f23SGarrett Wollman * Our SYN was acked. If segment contains CC.ECHO 1253a0292f23SGarrett Wollman * option, check it to make sure this segment really 1254a0292f23SGarrett Wollman * matches our SYN. If not, just drop it as old 1255a0292f23SGarrett Wollman * duplicate, but send an RST if we're still playing 1256026650e5SBill Fenner * by the old rules. If no CC.ECHO option, make sure 1257026650e5SBill Fenner * we don't get fooled into using T/TCP. 1258a0292f23SGarrett Wollman */ 1259026650e5SBill Fenner if (to.to_flag & TOF_CCECHO) { 1260dfd5dee1SPeter Wemm if (tp->cc_send != to.to_ccecho) { 1261a0292f23SGarrett Wollman if (taop->tao_ccsent != 0) 1262a0292f23SGarrett Wollman goto drop; 1263a57815efSBosko Milekic else { 1264a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1265a0292f23SGarrett Wollman goto dropwithreset; 1266dfd5dee1SPeter Wemm } 1267a57815efSBosko Milekic } 1268026650e5SBill Fenner } else 1269026650e5SBill Fenner tp->t_flags &= ~TF_RCVD_CC; 1270845799c1SAndras Olah tcpstat.tcps_connects++; 1271845799c1SAndras Olah soisconnected(so); 1272845799c1SAndras Olah /* Do window scaling on this connection? */ 1273845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1274845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1275845799c1SAndras Olah tp->snd_scale = tp->requested_s_scale; 1276845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1277845799c1SAndras Olah } 1278a0292f23SGarrett Wollman /* Segment is acceptable, update cache if undefined. */ 1279a0292f23SGarrett Wollman if (taop->tao_ccsent == 0) 1280a0292f23SGarrett Wollman taop->tao_ccsent = to.to_ccecho; 1281a0292f23SGarrett Wollman 1282a0292f23SGarrett Wollman tp->rcv_adv += tp->rcv_wnd; 1283a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1284a0292f23SGarrett Wollman /* 1285a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1286a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1287a0292f23SGarrett Wollman */ 1288d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && tlen != 0) 12899b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 12909b8b58e0SJonathan Lemon tcp_timer_delack, tp); 1291a0292f23SGarrett Wollman else 1292a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1293a0292f23SGarrett Wollman /* 1294a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1295a0292f23SGarrett Wollman * Transitions: 1296a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1297a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1298a0292f23SGarrett Wollman */ 12999b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1300a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1301a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1302a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1303fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 13047ff19458SPaul Traina } else { 1305a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 13069b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, 13079b8b58e0SJonathan Lemon tcp_timer_keep, tp); 13087ff19458SPaul Traina } 1309a0292f23SGarrett Wollman } else { 1310a0292f23SGarrett Wollman /* 1311a0292f23SGarrett Wollman * Received initial SYN in SYN-SENT[*] state => simul- 1312a0292f23SGarrett Wollman * taneous open. If segment contains CC option and there is 1313a0292f23SGarrett Wollman * a cached CC, apply TAO test; if it succeeds, connection is 1314a0292f23SGarrett Wollman * half-synchronized. Otherwise, do 3-way handshake: 1315a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1316a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1317a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1318a0292f23SGarrett Wollman */ 1319a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 13209b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 1321a0292f23SGarrett Wollman if (to.to_flag & TOF_CC) { 1322a0292f23SGarrett Wollman if (taop->tao_cc != 0 && 1323a0292f23SGarrett Wollman CC_GT(to.to_cc, taop->tao_cc)) { 1324a0292f23SGarrett Wollman /* 1325a0292f23SGarrett Wollman * update cache and make transition: 1326a0292f23SGarrett Wollman * SYN-SENT -> ESTABLISHED* 1327a0292f23SGarrett Wollman * SYN-SENT* -> FIN-WAIT-1* 1328a0292f23SGarrett Wollman */ 1329a0292f23SGarrett Wollman taop->tao_cc = to.to_cc; 13309b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1331a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1332a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1333a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 13347ff19458SPaul Traina } else { 1335a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 13369b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, 13379b8b58e0SJonathan Lemon tcp_keepidle, 13389b8b58e0SJonathan Lemon tcp_timer_keep, 13399b8b58e0SJonathan Lemon tp); 13407ff19458SPaul Traina } 1341a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDSYN; 1342df8bae1dSRodney W. Grimes } else 1343df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1344a0292f23SGarrett Wollman } else { 1345a0292f23SGarrett Wollman /* CC.NEW or no option => invalidate cache */ 1346a0292f23SGarrett Wollman taop->tao_cc = 0; 1347a0292f23SGarrett Wollman tp->t_state = TCPS_SYN_RECEIVED; 1348a0292f23SGarrett Wollman } 1349a0292f23SGarrett Wollman } 1350df8bae1dSRodney W. Grimes 1351df8bae1dSRodney W. Grimes trimthenstep6: 1352df8bae1dSRodney W. Grimes /* 1353fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1354df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1355df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1356df8bae1dSRodney W. Grimes */ 1357fb59c426SYoshinobu Inoue th->th_seq++; 1358fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1359fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1360df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1361fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1362fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1363df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1364df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1365df8bae1dSRodney W. Grimes } 1366fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1367fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 1368a0292f23SGarrett Wollman /* 1369a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1370a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1371a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1372a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1373a0292f23SGarrett Wollman * Otherwise, goto step 6. 1374a0292f23SGarrett Wollman */ 1375fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1376a0292f23SGarrett Wollman goto process_ACK; 1377df8bae1dSRodney W. Grimes goto step6; 1378a0292f23SGarrett Wollman /* 1379a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1380a0292f23SGarrett Wollman * if segment contains a SYN and CC [not CC.NEW] option: 1381a0292f23SGarrett Wollman * if state == TIME_WAIT and connection duration > MSL, 1382a0292f23SGarrett Wollman * drop packet and send RST; 1383a0292f23SGarrett Wollman * 1384a0292f23SGarrett Wollman * if SEG.CC > CCrecv then is new SYN, and can implicitly 1385a0292f23SGarrett Wollman * ack the FIN (and data) in retransmission queue. 1386a0292f23SGarrett Wollman * Complete close and delete TCPCB. Then reprocess 1387a0292f23SGarrett Wollman * segment, hoping to find new TCPCB in LISTEN state; 1388a0292f23SGarrett Wollman * 1389a0292f23SGarrett Wollman * else must be old SYN; drop it. 1390a0292f23SGarrett Wollman * else do normal processing. 1391a0292f23SGarrett Wollman */ 1392a0292f23SGarrett Wollman case TCPS_LAST_ACK: 1393a0292f23SGarrett Wollman case TCPS_CLOSING: 1394a0292f23SGarrett Wollman case TCPS_TIME_WAIT: 1395fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) && 1396a0292f23SGarrett Wollman (to.to_flag & TOF_CC) && tp->cc_recv != 0) { 1397a0292f23SGarrett Wollman if (tp->t_state == TCPS_TIME_WAIT && 1398a57815efSBosko Milekic (ticks - tp->t_starttime) > tcp_msl) { 1399a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1400a0292f23SGarrett Wollman goto dropwithreset; 1401a57815efSBosko Milekic } 1402a0292f23SGarrett Wollman if (CC_GT(to.to_cc, tp->cc_recv)) { 1403a0292f23SGarrett Wollman tp = tcp_close(tp); 1404a0292f23SGarrett Wollman goto findpcb; 1405a0292f23SGarrett Wollman } 1406a0292f23SGarrett Wollman else 1407a0292f23SGarrett Wollman goto drop; 1408a0292f23SGarrett Wollman } 1409a0292f23SGarrett Wollman break; /* continue normal processing */ 1410df8bae1dSRodney W. Grimes } 1411df8bae1dSRodney W. Grimes 1412df8bae1dSRodney W. Grimes /* 1413df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 141480ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 141580ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 141680ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 141780ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 141880ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 141980ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 142080ab7c0eSGarrett Wollman * killing a connection). 142180ab7c0eSGarrett Wollman * Then check timestamp, if present. 1422a0292f23SGarrett Wollman * Then check the connection count, if present. 1423df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 1424df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 1425df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 1426df8bae1dSRodney W. Grimes * 142780ab7c0eSGarrett Wollman * 142880ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 142980ab7c0eSGarrett Wollman * if this is a valid reset segment. 143080ab7c0eSGarrett Wollman * RFC 793 page 37: 143180ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 143280ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 143380ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 143480ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 143580ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 14361a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 14371a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 14381a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 14391a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 144080ab7c0eSGarrett Wollman * If we have multiple segments in flight, the intial reset 144180ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 144280ab7c0eSGarrett Wollman * but they will eventually catch up. 144380ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 144480ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 144580ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 144680ab7c0eSGarrett Wollman * 144780ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 144880ab7c0eSGarrett Wollman * 144980ab7c0eSGarrett Wollman * DISCUSSION 145080ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 145180ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 145280ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 145380ab7c0eSGarrett Wollman * data. 145480ab7c0eSGarrett Wollman * 145580ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 145680ab7c0eSGarrett Wollman * the state: 145780ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 145880ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 145980ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 146080ab7c0eSGarrett Wollman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 146180ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 1462e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 146380ab7c0eSGarrett Wollman * Close the tcb. 1464e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 146580ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 146680ab7c0eSGarrett Wollman * RFC 1337. 146780ab7c0eSGarrett Wollman */ 1468fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 1469fb59c426SYoshinobu Inoue if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 1470fb59c426SYoshinobu Inoue SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 147180ab7c0eSGarrett Wollman switch (tp->t_state) { 147280ab7c0eSGarrett Wollman 147380ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 147480ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 147580ab7c0eSGarrett Wollman goto close; 147680ab7c0eSGarrett Wollman 147780ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 147880ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 147980ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 148080ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 148180ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 148280ab7c0eSGarrett Wollman close: 148380ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 148480ab7c0eSGarrett Wollman tcpstat.tcps_drops++; 148580ab7c0eSGarrett Wollman tp = tcp_close(tp); 148680ab7c0eSGarrett Wollman break; 148780ab7c0eSGarrett Wollman 148880ab7c0eSGarrett Wollman case TCPS_CLOSING: 148980ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 149080ab7c0eSGarrett Wollman tp = tcp_close(tp); 149180ab7c0eSGarrett Wollman break; 149280ab7c0eSGarrett Wollman 149380ab7c0eSGarrett Wollman case TCPS_TIME_WAIT: 149480ab7c0eSGarrett Wollman break; 149580ab7c0eSGarrett Wollman } 149680ab7c0eSGarrett Wollman } 149780ab7c0eSGarrett Wollman goto drop; 149880ab7c0eSGarrett Wollman } 149980ab7c0eSGarrett Wollman 150080ab7c0eSGarrett Wollman /* 1501df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 1502df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 1503df8bae1dSRodney W. Grimes */ 150480ab7c0eSGarrett Wollman if ((to.to_flag & TOF_TS) != 0 && tp->ts_recent && 150580ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1506df8bae1dSRodney W. Grimes 1507df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 15089b8b58e0SJonathan Lemon if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1509df8bae1dSRodney W. Grimes /* 1510df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 1511df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 1512df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 1513df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 1514df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 1515df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 1516df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 1517df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 1518df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 1519df8bae1dSRodney W. Grimes */ 1520df8bae1dSRodney W. Grimes tp->ts_recent = 0; 1521df8bae1dSRodney W. Grimes } else { 1522df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 1523fb59c426SYoshinobu Inoue tcpstat.tcps_rcvdupbyte += tlen; 1524df8bae1dSRodney W. Grimes tcpstat.tcps_pawsdrop++; 1525df8bae1dSRodney W. Grimes goto dropafterack; 1526df8bae1dSRodney W. Grimes } 1527df8bae1dSRodney W. Grimes } 1528df8bae1dSRodney W. Grimes 1529a0292f23SGarrett Wollman /* 1530a0292f23SGarrett Wollman * T/TCP mechanism 1531a0292f23SGarrett Wollman * If T/TCP was negotiated and the segment doesn't have CC, 1532dc733423SDag-Erling Smørgrav * or if its CC is wrong then drop the segment. 1533a0292f23SGarrett Wollman * RST segments do not have to comply with this. 1534a0292f23SGarrett Wollman */ 1535a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) && 153680ab7c0eSGarrett Wollman ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc)) 1537a0292f23SGarrett Wollman goto dropafterack; 1538a0292f23SGarrett Wollman 153980ab7c0eSGarrett Wollman /* 154080ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 154180ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 154280ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 154380ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 154480ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 154580ab7c0eSGarrett Wollman */ 1546a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 1547a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1548a57815efSBosko Milekic goto dropwithreset; 1549a57815efSBosko Milekic } 155080ab7c0eSGarrett Wollman 1551fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 1552df8bae1dSRodney W. Grimes if (todrop > 0) { 1553fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1554fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 1555fb59c426SYoshinobu Inoue th->th_seq++; 1556fb59c426SYoshinobu Inoue if (th->th_urp > 1) 1557fb59c426SYoshinobu Inoue th->th_urp--; 1558df8bae1dSRodney W. Grimes else 1559fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1560df8bae1dSRodney W. Grimes todrop--; 1561df8bae1dSRodney W. Grimes } 1562df8bae1dSRodney W. Grimes /* 1563dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 1564df8bae1dSRodney W. Grimes */ 1565fb59c426SYoshinobu Inoue if (todrop > tlen 1566fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1567dac20301SGarrett Wollman /* 1568dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 1569dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 1570dac20301SGarrett Wollman * of sequence; drop it. 1571dac20301SGarrett Wollman */ 1572fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1573dac20301SGarrett Wollman 1574df8bae1dSRodney W. Grimes /* 1575dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 1576dac20301SGarrett Wollman * But keep on processing for RST or ACK. 1577df8bae1dSRodney W. Grimes */ 1578dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1579fb59c426SYoshinobu Inoue todrop = tlen; 1580dac20301SGarrett Wollman tcpstat.tcps_rcvduppack++; 1581dac20301SGarrett Wollman tcpstat.tcps_rcvdupbyte += todrop; 1582df8bae1dSRodney W. Grimes } else { 1583df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartduppack++; 1584df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartdupbyte += todrop; 1585df8bae1dSRodney W. Grimes } 1586fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 1587fb59c426SYoshinobu Inoue th->th_seq += todrop; 1588fb59c426SYoshinobu Inoue tlen -= todrop; 1589fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 1590fb59c426SYoshinobu Inoue th->th_urp -= todrop; 1591df8bae1dSRodney W. Grimes else { 1592fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1593fb59c426SYoshinobu Inoue th->th_urp = 0; 1594df8bae1dSRodney W. Grimes } 1595df8bae1dSRodney W. Grimes } 1596df8bae1dSRodney W. Grimes 1597df8bae1dSRodney W. Grimes /* 1598df8bae1dSRodney W. Grimes * If new data are received on a connection after the 1599df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 1600df8bae1dSRodney W. Grimes */ 1601df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 1602fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1603df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1604df8bae1dSRodney W. Grimes tcpstat.tcps_rcvafterclose++; 1605a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1606df8bae1dSRodney W. Grimes goto dropwithreset; 1607df8bae1dSRodney W. Grimes } 1608df8bae1dSRodney W. Grimes 1609df8bae1dSRodney W. Grimes /* 1610df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 1611df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 1612df8bae1dSRodney W. Grimes */ 1613fb59c426SYoshinobu Inoue todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd); 1614df8bae1dSRodney W. Grimes if (todrop > 0) { 1615df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1616fb59c426SYoshinobu Inoue if (todrop >= tlen) { 1617fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyteafterwin += tlen; 1618df8bae1dSRodney W. Grimes /* 1619df8bae1dSRodney W. Grimes * If a new connection request is received 1620df8bae1dSRodney W. Grimes * while in TIME_WAIT, drop the old connection 1621df8bae1dSRodney W. Grimes * and start over if the sequence numbers 1622df8bae1dSRodney W. Grimes * are above the previous ones. 1623df8bae1dSRodney W. Grimes */ 1624fb59c426SYoshinobu Inoue if (thflags & TH_SYN && 1625df8bae1dSRodney W. Grimes tp->t_state == TCPS_TIME_WAIT && 1626fb59c426SYoshinobu Inoue SEQ_GT(th->th_seq, tp->rcv_nxt)) { 1627f0a04f3fSKris Kennaway iss = tcp_rndiss_next(); 1628df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1629df8bae1dSRodney W. Grimes goto findpcb; 1630df8bae1dSRodney W. Grimes } 1631df8bae1dSRodney W. Grimes /* 1632df8bae1dSRodney W. Grimes * If window is closed can only take segments at 1633df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 1634df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 1635df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 1636df8bae1dSRodney W. Grimes * and ack. 1637df8bae1dSRodney W. Grimes */ 1638fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1639df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1640df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinprobe++; 1641df8bae1dSRodney W. Grimes } else 1642df8bae1dSRodney W. Grimes goto dropafterack; 1643df8bae1dSRodney W. Grimes } else 1644df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1645df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1646fb59c426SYoshinobu Inoue tlen -= todrop; 1647fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 1648df8bae1dSRodney W. Grimes } 1649df8bae1dSRodney W. Grimes 1650df8bae1dSRodney W. Grimes /* 1651df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1652df8bae1dSRodney W. Grimes * record its timestamp. 1653a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1654a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1655df8bae1dSRodney W. Grimes */ 1656a0292f23SGarrett Wollman if ((to.to_flag & TOF_TS) != 0 && 1657fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 16589b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1659a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1660df8bae1dSRodney W. Grimes } 1661df8bae1dSRodney W. Grimes 1662df8bae1dSRodney W. Grimes /* 1663df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 1664df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 1665df8bae1dSRodney W. Grimes */ 1666fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1667df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 1668a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1669df8bae1dSRodney W. Grimes goto dropwithreset; 1670df8bae1dSRodney W. Grimes } 1671df8bae1dSRodney W. Grimes 1672a0292f23SGarrett Wollman /* 1673a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1674a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 1675a0292f23SGarrett Wollman * later processing; else drop segment and return. 1676a0292f23SGarrett Wollman */ 1677fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 1678a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 1679a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 1680a0292f23SGarrett Wollman goto step6; 1681a0292f23SGarrett Wollman else 1682a0292f23SGarrett Wollman goto drop; 1683a0292f23SGarrett Wollman } 1684df8bae1dSRodney W. Grimes 1685df8bae1dSRodney W. Grimes /* 1686df8bae1dSRodney W. Grimes * Ack processing. 1687df8bae1dSRodney W. Grimes */ 1688df8bae1dSRodney W. Grimes switch (tp->t_state) { 1689df8bae1dSRodney W. Grimes 1690df8bae1dSRodney W. Grimes /* 1691764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1692764d8cefSBill Fenner * ESTABLISHED state and continue processing. 1693764d8cefSBill Fenner * The ACK was checked above. 1694df8bae1dSRodney W. Grimes */ 1695df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1696a0292f23SGarrett Wollman 1697df8bae1dSRodney W. Grimes tcpstat.tcps_connects++; 1698df8bae1dSRodney W. Grimes soisconnected(so); 1699df8bae1dSRodney W. Grimes /* Do window scaling? */ 1700df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1701df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1702df8bae1dSRodney W. Grimes tp->snd_scale = tp->requested_s_scale; 1703df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 1704df8bae1dSRodney W. Grimes } 1705a0292f23SGarrett Wollman /* 1706a0292f23SGarrett Wollman * Upon successful completion of 3-way handshake, 1707a0292f23SGarrett Wollman * update cache.CC if it was undefined, pass any queued 1708a0292f23SGarrett Wollman * data to the user, and advance state appropriately. 1709a0292f23SGarrett Wollman */ 1710a0292f23SGarrett Wollman if ((taop = tcp_gettaocache(inp)) != NULL && 1711a0292f23SGarrett Wollman taop->tao_cc == 0) 1712a0292f23SGarrett Wollman taop->tao_cc = tp->cc_recv; 1713a0292f23SGarrett Wollman 1714a0292f23SGarrett Wollman /* 1715a0292f23SGarrett Wollman * Make transitions: 1716a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 1717a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 1718a0292f23SGarrett Wollman */ 17199b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1720a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1721a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1722a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 17237ff19458SPaul Traina } else { 1724a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 17259b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, 17269b8b58e0SJonathan Lemon tcp_timer_keep, tp); 17277ff19458SPaul Traina } 1728a0292f23SGarrett Wollman /* 1729a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 1730a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 1731a0292f23SGarrett Wollman */ 1732fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 1733fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1734a0292f23SGarrett Wollman (struct mbuf *)0); 1735fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1736df8bae1dSRodney W. Grimes /* fall into ... */ 1737df8bae1dSRodney W. Grimes 1738df8bae1dSRodney W. Grimes /* 1739df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1740df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 1741fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 1742fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 1743df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 1744df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 1745df8bae1dSRodney W. Grimes */ 1746df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1747df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1748df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 1749df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1750df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1751df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 1752df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 1753df8bae1dSRodney W. Grimes 1754fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1755fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 1756df8bae1dSRodney W. Grimes tcpstat.tcps_rcvdupack++; 1757df8bae1dSRodney W. Grimes /* 1758df8bae1dSRodney W. Grimes * If we have outstanding data (other than 1759df8bae1dSRodney W. Grimes * a window probe), this is a completely 1760df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 1761df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 1762df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 1763df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 1764df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 1765df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 1766df8bae1dSRodney W. Grimes * window so we send only this one 1767df8bae1dSRodney W. Grimes * packet. 1768df8bae1dSRodney W. Grimes * 1769df8bae1dSRodney W. Grimes * We know we're losing at the current 1770df8bae1dSRodney W. Grimes * window size so do congestion avoidance 1771df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 1772df8bae1dSRodney W. Grimes * and pull our congestion window back to 1773df8bae1dSRodney W. Grimes * the new ssthresh). 1774df8bae1dSRodney W. Grimes * 1775df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 1776df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 1777df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 1778df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 1779df8bae1dSRodney W. Grimes * network. 1780df8bae1dSRodney W. Grimes */ 17819b8b58e0SJonathan Lemon if (!callout_active(tp->tt_rexmt) || 1782fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 1783df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1784df8bae1dSRodney W. Grimes else if (++tp->t_dupacks == tcprexmtthresh) { 1785df8bae1dSRodney W. Grimes tcp_seq onxt = tp->snd_nxt; 1786df8bae1dSRodney W. Grimes u_int win = 1787df8bae1dSRodney W. Grimes min(tp->snd_wnd, tp->snd_cwnd) / 2 / 1788df8bae1dSRodney W. Grimes tp->t_maxseg; 178946f58482SJonathan Lemon if (tcp_do_newreno && SEQ_LT(th->th_ack, 179046f58482SJonathan Lemon tp->snd_recover)) { 179146f58482SJonathan Lemon /* False retransmit, should not 179246f58482SJonathan Lemon * cut window 179346f58482SJonathan Lemon */ 179446f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 179546f58482SJonathan Lemon tp->t_dupacks = 0; 179646f58482SJonathan Lemon (void) tcp_output(tp); 179746f58482SJonathan Lemon goto drop; 179846f58482SJonathan Lemon } 1799df8bae1dSRodney W. Grimes if (win < 2) 1800df8bae1dSRodney W. Grimes win = 2; 1801df8bae1dSRodney W. Grimes tp->snd_ssthresh = win * tp->t_maxseg; 180246f58482SJonathan Lemon tp->snd_recover = tp->snd_max; 18039b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 18049b8b58e0SJonathan Lemon tp->t_rtttime = 0; 1805fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 1806df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 1807df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1808df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 1809df8bae1dSRodney W. Grimes tp->t_maxseg * tp->t_dupacks; 1810df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 1811df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 1812df8bae1dSRodney W. Grimes goto drop; 1813df8bae1dSRodney W. Grimes } else if (tp->t_dupacks > tcprexmtthresh) { 1814df8bae1dSRodney W. Grimes tp->snd_cwnd += tp->t_maxseg; 1815df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1816df8bae1dSRodney W. Grimes goto drop; 1817df8bae1dSRodney W. Grimes } 1818df8bae1dSRodney W. Grimes } else 1819df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1820df8bae1dSRodney W. Grimes break; 1821df8bae1dSRodney W. Grimes } 1822df8bae1dSRodney W. Grimes /* 1823df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 1824df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 1825df8bae1dSRodney W. Grimes */ 182646f58482SJonathan Lemon if (tcp_do_newreno == 0) { 1827233e8c18SGarrett Wollman if (tp->t_dupacks >= tcprexmtthresh && 1828df8bae1dSRodney W. Grimes tp->snd_cwnd > tp->snd_ssthresh) 1829df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh; 1830df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 183146f58482SJonathan Lemon } else if (tp->t_dupacks >= tcprexmtthresh && 183246f58482SJonathan Lemon !tcp_newreno(tp, th)) { 183346f58482SJonathan Lemon /* 183446f58482SJonathan Lemon * Window inflation should have left us with approx. 183546f58482SJonathan Lemon * snd_ssthresh outstanding data. But in case we 183646f58482SJonathan Lemon * would be inclined to send a burst, better to do 183746f58482SJonathan Lemon * it via the slow start mechanism. 183846f58482SJonathan Lemon */ 183946f58482SJonathan Lemon if (SEQ_GT(th->th_ack + tp->snd_ssthresh, tp->snd_max)) 184046f58482SJonathan Lemon tp->snd_cwnd = 184146f58482SJonathan Lemon tp->snd_max - th->th_ack + tp->t_maxseg; 184246f58482SJonathan Lemon else 184346f58482SJonathan Lemon tp->snd_cwnd = tp->snd_ssthresh; 184446f58482SJonathan Lemon tp->t_dupacks = 0; 184546f58482SJonathan Lemon } 1846fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_max)) { 1847df8bae1dSRodney W. Grimes tcpstat.tcps_rcvacktoomuch++; 1848df8bae1dSRodney W. Grimes goto dropafterack; 1849df8bae1dSRodney W. Grimes } 1850a0292f23SGarrett Wollman /* 1851a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 1852a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 1853a0292f23SGarrett Wollman */ 1854a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 1855a0292f23SGarrett Wollman /* 1856a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 1857a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 185807e43e10SAndras Olah * synchronized). Go to non-starred state, 185907e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 186007e43e10SAndras Olah * we can do window scaling. 1861a0292f23SGarrett Wollman */ 1862a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 1863a0292f23SGarrett Wollman tp->snd_una++; 186407e43e10SAndras Olah /* Do window scaling? */ 186507e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 186607e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 186707e43e10SAndras Olah tp->snd_scale = tp->requested_s_scale; 186807e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 186907e43e10SAndras Olah } 1870a0292f23SGarrett Wollman } 1871a0292f23SGarrett Wollman 1872a0292f23SGarrett Wollman process_ACK: 1873fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 1874df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 1875df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 1876df8bae1dSRodney W. Grimes 1877df8bae1dSRodney W. Grimes /* 18789b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 18799b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 18809b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 18819b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 18829b8b58e0SJonathan Lemon * we left off. 18839b8b58e0SJonathan Lemon */ 18849b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 18859b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 18869b8b58e0SJonathan Lemon tp->snd_ssthresh = tp->snd_ssthresh_prev; 18879b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 18889b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; /* XXX probably not required */ 18899b8b58e0SJonathan Lemon } 18909b8b58e0SJonathan Lemon 18919b8b58e0SJonathan Lemon /* 1892df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 1893df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 1894df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 1895df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 1896df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 1897df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 1898df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 1899df8bae1dSRodney W. Grimes */ 1900a0292f23SGarrett Wollman if (to.to_flag & TOF_TS) 19019b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 1902fb59c426SYoshinobu Inoue else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) 19039b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 1904df8bae1dSRodney W. Grimes 1905df8bae1dSRodney W. Grimes /* 1906df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 1907df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 1908df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 1909df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 1910df8bae1dSRodney W. Grimes */ 1911fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 19129b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 1913df8bae1dSRodney W. Grimes needoutput = 1; 19149b8b58e0SJonathan Lemon } else if (!callout_active(tp->tt_persist)) 19159b8b58e0SJonathan Lemon callout_reset(tp->tt_rexmt, tp->t_rxtcur, 19169b8b58e0SJonathan Lemon tcp_timer_rexmt, tp); 1917a0292f23SGarrett Wollman 1918a0292f23SGarrett Wollman /* 1919a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 1920a0292f23SGarrett Wollman * skip rest of ACK processing. 1921a0292f23SGarrett Wollman */ 1922a0292f23SGarrett Wollman if (acked == 0) 1923a0292f23SGarrett Wollman goto step6; 1924a0292f23SGarrett Wollman 1925df8bae1dSRodney W. Grimes /* 1926df8bae1dSRodney W. Grimes * When new data is acked, open the congestion window. 1927df8bae1dSRodney W. Grimes * If the window gives us less than ssthresh packets 1928df8bae1dSRodney W. Grimes * in flight, open exponentially (maxseg per packet). 1929df8bae1dSRodney W. Grimes * Otherwise open linearly: maxseg per window 193010be5648SGarrett Wollman * (maxseg^2 / cwnd per packet). 1931df8bae1dSRodney W. Grimes */ 1932df8bae1dSRodney W. Grimes { 1933df8bae1dSRodney W. Grimes register u_int cw = tp->snd_cwnd; 1934df8bae1dSRodney W. Grimes register u_int incr = tp->t_maxseg; 1935df8bae1dSRodney W. Grimes 1936df8bae1dSRodney W. Grimes if (cw > tp->snd_ssthresh) 193710be5648SGarrett Wollman incr = incr * incr / cw; 19388735719eSJonathan Lemon /* 19398735719eSJonathan Lemon * If t_dupacks != 0 here, it indicates that we are still 19408735719eSJonathan Lemon * in NewReno fast recovery mode, so we leave the congestion 19418735719eSJonathan Lemon * window alone. 19428735719eSJonathan Lemon */ 19438735719eSJonathan Lemon if (tcp_do_newreno == 0 || tp->t_dupacks == 0) 1944df8bae1dSRodney W. Grimes tp->snd_cwnd = min(cw + incr,TCP_MAXWIN<<tp->snd_scale); 1945df8bae1dSRodney W. Grimes } 1946df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 1947df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 1948df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 1949df8bae1dSRodney W. Grimes ourfinisacked = 1; 1950df8bae1dSRodney W. Grimes } else { 1951df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 1952df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 1953df8bae1dSRodney W. Grimes ourfinisacked = 0; 1954df8bae1dSRodney W. Grimes } 1955df8bae1dSRodney W. Grimes sowwakeup(so); 1956fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 1957df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1958df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 1959df8bae1dSRodney W. Grimes 1960df8bae1dSRodney W. Grimes switch (tp->t_state) { 1961df8bae1dSRodney W. Grimes 1962df8bae1dSRodney W. Grimes /* 1963df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 1964df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 1965df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 1966df8bae1dSRodney W. Grimes */ 1967df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1968df8bae1dSRodney W. Grimes if (ourfinisacked) { 1969df8bae1dSRodney W. Grimes /* 1970df8bae1dSRodney W. Grimes * If we can't receive any more 1971df8bae1dSRodney W. Grimes * data, then closing user can proceed. 1972df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 1973df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 1974df8bae1dSRodney W. Grimes * we'll hang forever. 1975df8bae1dSRodney W. Grimes */ 1976df8bae1dSRodney W. Grimes if (so->so_state & SS_CANTRCVMORE) { 1977df8bae1dSRodney W. Grimes soisdisconnected(so); 19789b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, tcp_maxidle, 19799b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 1980df8bae1dSRodney W. Grimes } 1981df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 1982df8bae1dSRodney W. Grimes } 1983df8bae1dSRodney W. Grimes break; 1984df8bae1dSRodney W. Grimes 1985df8bae1dSRodney W. Grimes /* 1986df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 1987df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 1988df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 1989df8bae1dSRodney W. Grimes * the segment. 1990df8bae1dSRodney W. Grimes */ 1991df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1992df8bae1dSRodney W. Grimes if (ourfinisacked) { 1993df8bae1dSRodney W. Grimes tp->t_state = TCPS_TIME_WAIT; 1994df8bae1dSRodney W. Grimes tcp_canceltimers(tp); 1995a0292f23SGarrett Wollman /* Shorten TIME_WAIT [RFC-1644, p.28] */ 1996a0292f23SGarrett Wollman if (tp->cc_recv != 0 && 19979b8b58e0SJonathan Lemon (ticks - tp->t_starttime) < tcp_msl) 19989b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 19999b8b58e0SJonathan Lemon tp->t_rxtcur * 20009b8b58e0SJonathan Lemon TCPTV_TWTRUNC, 20019b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2002a0292f23SGarrett Wollman else 20039b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 20049b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2005df8bae1dSRodney W. Grimes soisdisconnected(so); 2006df8bae1dSRodney W. Grimes } 2007df8bae1dSRodney W. Grimes break; 2008df8bae1dSRodney W. Grimes 2009df8bae1dSRodney W. Grimes /* 2010df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2011df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2012df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2013df8bae1dSRodney W. Grimes * enter the closed state and return. 2014df8bae1dSRodney W. Grimes */ 2015df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2016df8bae1dSRodney W. Grimes if (ourfinisacked) { 2017df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2018df8bae1dSRodney W. Grimes goto drop; 2019df8bae1dSRodney W. Grimes } 2020df8bae1dSRodney W. Grimes break; 2021df8bae1dSRodney W. Grimes 2022df8bae1dSRodney W. Grimes /* 2023df8bae1dSRodney W. Grimes * In TIME_WAIT state the only thing that should arrive 2024df8bae1dSRodney W. Grimes * is a retransmission of the remote FIN. Acknowledge 2025df8bae1dSRodney W. Grimes * it and restart the finack timer. 2026df8bae1dSRodney W. Grimes */ 2027df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 20289b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 20299b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2030df8bae1dSRodney W. Grimes goto dropafterack; 2031df8bae1dSRodney W. Grimes } 2032df8bae1dSRodney W. Grimes } 2033df8bae1dSRodney W. Grimes 2034df8bae1dSRodney W. Grimes step6: 2035df8bae1dSRodney W. Grimes /* 2036df8bae1dSRodney W. Grimes * Update window information. 2037df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 2038df8bae1dSRodney W. Grimes */ 2039fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 2040fb59c426SYoshinobu Inoue (SEQ_LT(tp->snd_wl1, th->th_seq) || 2041fb59c426SYoshinobu Inoue (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2042fb59c426SYoshinobu Inoue (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2043df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 2044fb59c426SYoshinobu Inoue if (tlen == 0 && 2045fb59c426SYoshinobu Inoue tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2046df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinupd++; 2047df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 2048fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq; 2049fb59c426SYoshinobu Inoue tp->snd_wl2 = th->th_ack; 2050df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2051df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 2052df8bae1dSRodney W. Grimes needoutput = 1; 2053df8bae1dSRodney W. Grimes } 2054df8bae1dSRodney W. Grimes 2055df8bae1dSRodney W. Grimes /* 2056df8bae1dSRodney W. Grimes * Process segments with URG. 2057df8bae1dSRodney W. Grimes */ 2058fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2059df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2060df8bae1dSRodney W. Grimes /* 2061df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2062df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2063df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2064df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2065df8bae1dSRodney W. Grimes */ 2066fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2067fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2068fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 2069df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2070df8bae1dSRodney W. Grimes } 2071df8bae1dSRodney W. Grimes /* 2072df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2073df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2074df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2075df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2076df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2077df8bae1dSRodney W. Grimes * 2078df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2079df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2080df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2081df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2082df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2083df8bae1dSRodney W. Grimes * spec states (in one of two places). 2084df8bae1dSRodney W. Grimes */ 2085fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2086fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2087df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2088df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2089df8bae1dSRodney W. Grimes if (so->so_oobmark == 0) 2090df8bae1dSRodney W. Grimes so->so_state |= SS_RCVATMARK; 2091df8bae1dSRodney W. Grimes sohasoutofband(so); 2092df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2093df8bae1dSRodney W. Grimes } 2094df8bae1dSRodney W. Grimes /* 2095df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2096df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2097df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2098df8bae1dSRodney W. Grimes * data may creep in... ick. 2099df8bae1dSRodney W. Grimes */ 2100fb59c426SYoshinobu Inoue if (th->th_urp <= (u_long)tlen 2101df8bae1dSRodney W. Grimes #ifdef SO_OOBINLINE 2102df8bae1dSRodney W. Grimes && (so->so_options & SO_OOBINLINE) == 0 2103df8bae1dSRodney W. Grimes #endif 2104df8bae1dSRodney W. Grimes ) 2105fb59c426SYoshinobu Inoue tcp_pulloutofband(so, th, m, 2106fb59c426SYoshinobu Inoue drop_hdrlen); /* hdr drop is delayed */ 2107df8bae1dSRodney W. Grimes } else 2108df8bae1dSRodney W. Grimes /* 2109df8bae1dSRodney W. Grimes * If no out of band data is expected, 2110df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2111df8bae1dSRodney W. Grimes * with the receive window. 2112df8bae1dSRodney W. Grimes */ 2113df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2114df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2115df8bae1dSRodney W. Grimes dodata: /* XXX */ 2116df8bae1dSRodney W. Grimes 2117df8bae1dSRodney W. Grimes /* 2118df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2119df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2120df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2121df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2122df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2123df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2124df8bae1dSRodney W. Grimes */ 2125fb59c426SYoshinobu Inoue if ((tlen || (thflags&TH_FIN)) && 2126df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2127fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2128e4b64281SJesper Skriver /* 2129e4b64281SJesper Skriver * Insert segment which inludes th into reassembly queue of tcp with 2130e4b64281SJesper Skriver * control block tp. Return TH_FIN if reassembly now includes 2131e4b64281SJesper Skriver * a segment with FIN. This handle the common case inline (segment 2132e4b64281SJesper Skriver * is the next to be received on an established connection, and the 2133e4b64281SJesper Skriver * queue is empty), avoiding linkage into and removal from the queue 2134e4b64281SJesper Skriver * and repetition of various conversions. 2135e4b64281SJesper Skriver * Set DELACK for segments received in order, but ack immediately 2136e4b64281SJesper Skriver * when segments are out of order (so fast retransmit can work). 2137e4b64281SJesper Skriver */ 2138e4b64281SJesper Skriver if (th->th_seq == tp->rcv_nxt && 2139e4b64281SJesper Skriver LIST_EMPTY(&tp->t_segq) && 2140e4b64281SJesper Skriver TCPS_HAVEESTABLISHED(tp->t_state)) { 2141e4b64281SJesper Skriver if (DELAY_ACK(tp)) 2142e4b64281SJesper Skriver callout_reset(tp->tt_delack, tcp_delacktime, 2143e4b64281SJesper Skriver tcp_timer_delack, tp); 2144e4b64281SJesper Skriver else 2145e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2146e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2147e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 2148e4b64281SJesper Skriver tcpstat.tcps_rcvpack++; 2149e4b64281SJesper Skriver tcpstat.tcps_rcvbyte += tlen; 2150e4b64281SJesper Skriver ND6_HINT(tp); 2151e4b64281SJesper Skriver sbappend(&so->so_rcv, m); 2152e4b64281SJesper Skriver sorwakeup(so); 2153e4b64281SJesper Skriver } else { 2154e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 2155e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2156e4b64281SJesper Skriver } 2157e4b64281SJesper Skriver 2158df8bae1dSRodney W. Grimes /* 2159df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2160df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2161df8bae1dSRodney W. Grimes * buffer size. 2162df8bae1dSRodney W. Grimes */ 2163df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2164df8bae1dSRodney W. Grimes } else { 2165df8bae1dSRodney W. Grimes m_freem(m); 2166fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2167df8bae1dSRodney W. Grimes } 2168df8bae1dSRodney W. Grimes 2169df8bae1dSRodney W. Grimes /* 2170df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2171df8bae1dSRodney W. Grimes * that the connection is closing. 2172df8bae1dSRodney W. Grimes */ 2173fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2174df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2175df8bae1dSRodney W. Grimes socantrcvmore(so); 2176a0292f23SGarrett Wollman /* 2177a0292f23SGarrett Wollman * If connection is half-synchronized 2178d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2179a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2180a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2181a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2182a0292f23SGarrett Wollman */ 2183d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && (tp->t_flags & TF_NEEDSYN)) 21849b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 21859b8b58e0SJonathan Lemon tcp_timer_delack, tp); 2186a0292f23SGarrett Wollman else 2187df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2188df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2189df8bae1dSRodney W. Grimes } 2190df8bae1dSRodney W. Grimes switch (tp->t_state) { 2191df8bae1dSRodney W. Grimes 2192df8bae1dSRodney W. Grimes /* 2193df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2194df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2195df8bae1dSRodney W. Grimes */ 2196df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 21979b8b58e0SJonathan Lemon tp->t_starttime = ticks; 21989b8b58e0SJonathan Lemon /*FALLTHROUGH*/ 2199df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2200df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2201df8bae1dSRodney W. Grimes break; 2202df8bae1dSRodney W. Grimes 2203df8bae1dSRodney W. Grimes /* 2204df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2205df8bae1dSRodney W. Grimes * enter the CLOSING state. 2206df8bae1dSRodney W. Grimes */ 2207df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2208df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 2209df8bae1dSRodney W. Grimes break; 2210df8bae1dSRodney W. Grimes 2211df8bae1dSRodney W. Grimes /* 2212df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 2213df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 2214df8bae1dSRodney W. Grimes * standard timers. 2215df8bae1dSRodney W. Grimes */ 2216df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2217df8bae1dSRodney W. Grimes tp->t_state = TCPS_TIME_WAIT; 2218df8bae1dSRodney W. Grimes tcp_canceltimers(tp); 2219a0292f23SGarrett Wollman /* Shorten TIME_WAIT [RFC-1644, p.28] */ 2220a0292f23SGarrett Wollman if (tp->cc_recv != 0 && 22219b8b58e0SJonathan Lemon (ticks - tp->t_starttime) < tcp_msl) { 22229b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 22239b8b58e0SJonathan Lemon tp->t_rxtcur * TCPTV_TWTRUNC, 22249b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2225a0292f23SGarrett Wollman /* For transaction client, force ACK now. */ 2226a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2227a0292f23SGarrett Wollman } 2228a0292f23SGarrett Wollman else 22299b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 22309b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2231df8bae1dSRodney W. Grimes soisdisconnected(so); 2232df8bae1dSRodney W. Grimes break; 2233df8bae1dSRodney W. Grimes 2234df8bae1dSRodney W. Grimes /* 2235df8bae1dSRodney W. Grimes * In TIME_WAIT state restart the 2 MSL time_wait timer. 2236df8bae1dSRodney W. Grimes */ 2237df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 22389b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 22399b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2240df8bae1dSRodney W. Grimes break; 2241df8bae1dSRodney W. Grimes } 2242df8bae1dSRodney W. Grimes } 2243610ee2f9SDavid Greenman #ifdef TCPDEBUG 2244df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) 2245fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2246fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2247610ee2f9SDavid Greenman #endif 2248df8bae1dSRodney W. Grimes 2249df8bae1dSRodney W. Grimes /* 2250df8bae1dSRodney W. Grimes * Return any desired output. 2251df8bae1dSRodney W. Grimes */ 2252df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 2253df8bae1dSRodney W. Grimes (void) tcp_output(tp); 2254df8bae1dSRodney W. Grimes return; 2255df8bae1dSRodney W. Grimes 2256df8bae1dSRodney W. Grimes dropafterack: 2257df8bae1dSRodney W. Grimes /* 2258df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 2259df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 226080ab7c0eSGarrett Wollman * 226180ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 226280ab7c0eSGarrett Wollman * paths to this code happen after packets containing 226380ab7c0eSGarrett Wollman * RST have been dropped. 226480ab7c0eSGarrett Wollman * 226580ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 226680ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 226780ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 226880ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 226980ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 227080ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 2271df8bae1dSRodney W. Grimes */ 2272fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2273fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 2274a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 2275a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2276a57815efSBosko Milekic goto dropwithreset; 2277a57815efSBosko Milekic } 2278a0292f23SGarrett Wollman #ifdef TCPDEBUG 2279a0292f23SGarrett Wollman if (so->so_options & SO_DEBUG) 2280fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2281fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2282a0292f23SGarrett Wollman #endif 2283df8bae1dSRodney W. Grimes m_freem(m); 2284df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2285df8bae1dSRodney W. Grimes (void) tcp_output(tp); 2286df8bae1dSRodney W. Grimes return; 2287df8bae1dSRodney W. Grimes 2288df8bae1dSRodney W. Grimes dropwithreset: 2289df8bae1dSRodney W. Grimes /* 2290df8bae1dSRodney W. Grimes * Generate a RST, dropping incoming segment. 2291df8bae1dSRodney W. Grimes * Make ACK acceptable to originator of segment. 2292df8bae1dSRodney W. Grimes * Don't bother to respond if destination was broadcast/multicast. 2293df8bae1dSRodney W. Grimes */ 2294fb59c426SYoshinobu Inoue if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2295df8bae1dSRodney W. Grimes goto drop; 2296fb59c426SYoshinobu Inoue #ifdef INET6 2297fb59c426SYoshinobu Inoue if (isipv6) { 2298173c0f9fSWarner Losh if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2299173c0f9fSWarner Losh IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2300fb59c426SYoshinobu Inoue goto drop; 2301fb59c426SYoshinobu Inoue } else 2302fb59c426SYoshinobu Inoue #endif /* INET6 */ 2303173c0f9fSWarner Losh if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2304173c0f9fSWarner Losh IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2305173c0f9fSWarner Losh ip->ip_src.s_addr == htonl(INADDR_BROADCAST)) 2306fb59c426SYoshinobu Inoue goto drop; 2307fb59c426SYoshinobu Inoue /* IPv6 anycast check is done at tcp6_input() */ 2308a57815efSBosko Milekic 2309a57815efSBosko Milekic /* 2310c59319bfSDag-Erling Smørgrav * Perform bandwidth limiting. 2311a57815efSBosko Milekic */ 2312a57815efSBosko Milekic if (badport_bandlim(rstreason) < 0) 2313a57815efSBosko Milekic goto drop; 2314a57815efSBosko Milekic 2315a0292f23SGarrett Wollman #ifdef TCPDEBUG 2316a0292f23SGarrett Wollman if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2317fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2318fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2319a0292f23SGarrett Wollman #endif 2320fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 2321fb59c426SYoshinobu Inoue /* mtod() below is safe as long as hdr dropping is delayed */ 2322fb59c426SYoshinobu Inoue tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack, 2323fb59c426SYoshinobu Inoue TH_RST); 2324df8bae1dSRodney W. Grimes else { 2325fb59c426SYoshinobu Inoue if (thflags & TH_SYN) 2326fb59c426SYoshinobu Inoue tlen++; 2327fb59c426SYoshinobu Inoue /* mtod() below is safe as long as hdr dropping is delayed */ 2328fb59c426SYoshinobu Inoue tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2329fb59c426SYoshinobu Inoue (tcp_seq)0, TH_RST|TH_ACK); 2330df8bae1dSRodney W. Grimes } 2331df8bae1dSRodney W. Grimes /* destroy temporarily created socket */ 2332df8bae1dSRodney W. Grimes if (dropsocket) 2333df8bae1dSRodney W. Grimes (void) soabort(so); 2334df8bae1dSRodney W. Grimes return; 2335df8bae1dSRodney W. Grimes 2336df8bae1dSRodney W. Grimes drop: 2337df8bae1dSRodney W. Grimes /* 2338df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 2339df8bae1dSRodney W. Grimes */ 2340610ee2f9SDavid Greenman #ifdef TCPDEBUG 2341a0292f23SGarrett Wollman if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2342fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2343fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2344a0292f23SGarrett Wollman #endif 2345df8bae1dSRodney W. Grimes m_freem(m); 2346df8bae1dSRodney W. Grimes /* destroy temporarily created socket */ 2347df8bae1dSRodney W. Grimes if (dropsocket) 2348df8bae1dSRodney W. Grimes (void) soabort(so); 2349df8bae1dSRodney W. Grimes return; 2350df8bae1dSRodney W. Grimes } 2351df8bae1dSRodney W. Grimes 23520312fbe9SPoul-Henning Kamp static void 2353fb59c426SYoshinobu Inoue tcp_dooptions(tp, cp, cnt, th, to) 2354df8bae1dSRodney W. Grimes struct tcpcb *tp; 2355df8bae1dSRodney W. Grimes u_char *cp; 2356df8bae1dSRodney W. Grimes int cnt; 2357fb59c426SYoshinobu Inoue struct tcphdr *th; 2358a0292f23SGarrett Wollman struct tcpopt *to; 2359df8bae1dSRodney W. Grimes { 2360a0292f23SGarrett Wollman u_short mss = 0; 2361df8bae1dSRodney W. Grimes int opt, optlen; 2362df8bae1dSRodney W. Grimes 2363df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 2364df8bae1dSRodney W. Grimes opt = cp[0]; 2365df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 2366df8bae1dSRodney W. Grimes break; 2367df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 2368df8bae1dSRodney W. Grimes optlen = 1; 2369df8bae1dSRodney W. Grimes else { 2370b474779fSJun-ichiro itojun Hagino if (cnt < 2) 2371b474779fSJun-ichiro itojun Hagino break; 2372df8bae1dSRodney W. Grimes optlen = cp[1]; 2373b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 2374df8bae1dSRodney W. Grimes break; 2375df8bae1dSRodney W. Grimes } 2376df8bae1dSRodney W. Grimes switch (opt) { 2377df8bae1dSRodney W. Grimes 2378df8bae1dSRodney W. Grimes default: 2379df8bae1dSRodney W. Grimes continue; 2380df8bae1dSRodney W. Grimes 2381df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 2382df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 2383df8bae1dSRodney W. Grimes continue; 2384fb59c426SYoshinobu Inoue if (!(th->th_flags & TH_SYN)) 2385df8bae1dSRodney W. Grimes continue; 2386df8bae1dSRodney W. Grimes bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); 2387df8bae1dSRodney W. Grimes NTOHS(mss); 2388df8bae1dSRodney W. Grimes break; 2389df8bae1dSRodney W. Grimes 2390df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 2391df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 2392df8bae1dSRodney W. Grimes continue; 2393fb59c426SYoshinobu Inoue if (!(th->th_flags & TH_SYN)) 2394df8bae1dSRodney W. Grimes continue; 2395df8bae1dSRodney W. Grimes tp->t_flags |= TF_RCVD_SCALE; 2396df8bae1dSRodney W. Grimes tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 2397df8bae1dSRodney W. Grimes break; 2398df8bae1dSRodney W. Grimes 2399df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 2400df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 2401df8bae1dSRodney W. Grimes continue; 2402a0292f23SGarrett Wollman to->to_flag |= TOF_TS; 2403a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2404a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 2405a0292f23SGarrett Wollman NTOHL(to->to_tsval); 2406a0292f23SGarrett Wollman bcopy((char *)cp + 6, 2407a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2408a0292f23SGarrett Wollman NTOHL(to->to_tsecr); 2409df8bae1dSRodney W. Grimes 2410df8bae1dSRodney W. Grimes /* 2411df8bae1dSRodney W. Grimes * A timestamp received in a SYN makes 2412df8bae1dSRodney W. Grimes * it ok to send timestamp requests and replies. 2413df8bae1dSRodney W. Grimes */ 2414fb59c426SYoshinobu Inoue if (th->th_flags & TH_SYN) { 2415df8bae1dSRodney W. Grimes tp->t_flags |= TF_RCVD_TSTMP; 2416a0292f23SGarrett Wollman tp->ts_recent = to->to_tsval; 24179b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 2418df8bae1dSRodney W. Grimes } 2419df8bae1dSRodney W. Grimes break; 2420a0292f23SGarrett Wollman case TCPOPT_CC: 2421a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2422a0292f23SGarrett Wollman continue; 242340db8ef7SAndras Olah to->to_flag |= TOF_CC; 2424a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2425a0292f23SGarrett Wollman (char *)&to->to_cc, sizeof(to->to_cc)); 2426a0292f23SGarrett Wollman NTOHL(to->to_cc); 2427a0292f23SGarrett Wollman /* 2428a0292f23SGarrett Wollman * A CC or CC.new option received in a SYN makes 2429a0292f23SGarrett Wollman * it ok to send CC in subsequent segments. 2430a0292f23SGarrett Wollman */ 2431fb59c426SYoshinobu Inoue if (th->th_flags & TH_SYN) 2432a0292f23SGarrett Wollman tp->t_flags |= TF_RCVD_CC; 2433a0292f23SGarrett Wollman break; 2434a0292f23SGarrett Wollman case TCPOPT_CCNEW: 2435a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2436a0292f23SGarrett Wollman continue; 2437fb59c426SYoshinobu Inoue if (!(th->th_flags & TH_SYN)) 2438a0292f23SGarrett Wollman continue; 2439a0292f23SGarrett Wollman to->to_flag |= TOF_CCNEW; 2440a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2441a0292f23SGarrett Wollman (char *)&to->to_cc, sizeof(to->to_cc)); 2442a0292f23SGarrett Wollman NTOHL(to->to_cc); 2443a0292f23SGarrett Wollman /* 2444a0292f23SGarrett Wollman * A CC or CC.new option received in a SYN makes 2445a0292f23SGarrett Wollman * it ok to send CC in subsequent segments. 2446a0292f23SGarrett Wollman */ 2447a0292f23SGarrett Wollman tp->t_flags |= TF_RCVD_CC; 2448a0292f23SGarrett Wollman break; 2449a0292f23SGarrett Wollman case TCPOPT_CCECHO: 2450a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2451a0292f23SGarrett Wollman continue; 2452fb59c426SYoshinobu Inoue if (!(th->th_flags & TH_SYN)) 2453a0292f23SGarrett Wollman continue; 2454a0292f23SGarrett Wollman to->to_flag |= TOF_CCECHO; 2455a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2456a0292f23SGarrett Wollman (char *)&to->to_ccecho, sizeof(to->to_ccecho)); 2457a0292f23SGarrett Wollman NTOHL(to->to_ccecho); 2458a0292f23SGarrett Wollman break; 2459df8bae1dSRodney W. Grimes } 2460df8bae1dSRodney W. Grimes } 2461fb59c426SYoshinobu Inoue if (th->th_flags & TH_SYN) 2462a0292f23SGarrett Wollman tcp_mss(tp, mss); /* sets t_maxseg */ 2463df8bae1dSRodney W. Grimes } 2464df8bae1dSRodney W. Grimes 2465df8bae1dSRodney W. Grimes /* 2466df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 2467df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 2468df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 2469df8bae1dSRodney W. Grimes * sequencing purposes. 2470df8bae1dSRodney W. Grimes */ 24710312fbe9SPoul-Henning Kamp static void 2472fb59c426SYoshinobu Inoue tcp_pulloutofband(so, th, m, off) 2473df8bae1dSRodney W. Grimes struct socket *so; 2474fb59c426SYoshinobu Inoue struct tcphdr *th; 2475df8bae1dSRodney W. Grimes register struct mbuf *m; 2476fb59c426SYoshinobu Inoue int off; /* delayed to be droped hdrlen */ 2477df8bae1dSRodney W. Grimes { 2478fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 2479df8bae1dSRodney W. Grimes 2480df8bae1dSRodney W. Grimes while (cnt >= 0) { 2481df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 2482df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 2483df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 2484df8bae1dSRodney W. Grimes 2485df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 2486df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 2487df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2488df8bae1dSRodney W. Grimes m->m_len--; 248969a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 249069a34685SYoshinobu Inoue m->m_pkthdr.len--; 2491df8bae1dSRodney W. Grimes return; 2492df8bae1dSRodney W. Grimes } 2493df8bae1dSRodney W. Grimes cnt -= m->m_len; 2494df8bae1dSRodney W. Grimes m = m->m_next; 2495df8bae1dSRodney W. Grimes if (m == 0) 2496df8bae1dSRodney W. Grimes break; 2497df8bae1dSRodney W. Grimes } 2498df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 2499df8bae1dSRodney W. Grimes } 2500df8bae1dSRodney W. Grimes 2501df8bae1dSRodney W. Grimes /* 2502df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 2503df8bae1dSRodney W. Grimes * and update averages and current timeout. 2504df8bae1dSRodney W. Grimes */ 25050312fbe9SPoul-Henning Kamp static void 2506df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt) 2507df8bae1dSRodney W. Grimes register struct tcpcb *tp; 25089b8b58e0SJonathan Lemon int rtt; 2509df8bae1dSRodney W. Grimes { 2510233e8c18SGarrett Wollman register int delta; 2511233e8c18SGarrett Wollman 2512233e8c18SGarrett Wollman tcpstat.tcps_rttupdated++; 2513233e8c18SGarrett Wollman tp->t_rttupdated++; 2514233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 2515233e8c18SGarrett Wollman /* 2516233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 2517233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 2518233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 2519233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2520233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 2521233e8c18SGarrett Wollman */ 2522233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2523233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2524233e8c18SGarrett Wollman 2525233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 2526233e8c18SGarrett Wollman tp->t_srtt = 1; 2527233e8c18SGarrett Wollman 2528233e8c18SGarrett Wollman /* 2529233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 2530233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 2531233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 2532233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 2533233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 2534233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 2535233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2536233e8c18SGarrett Wollman * rfc793's wired-in beta. 2537233e8c18SGarrett Wollman */ 2538233e8c18SGarrett Wollman if (delta < 0) 2539233e8c18SGarrett Wollman delta = -delta; 2540233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2541233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 2542233e8c18SGarrett Wollman tp->t_rttvar = 1; 2543233e8c18SGarrett Wollman } else { 2544233e8c18SGarrett Wollman /* 2545233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 2546233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 2547233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 2548233e8c18SGarrett Wollman */ 2549233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 2550233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 2551233e8c18SGarrett Wollman } 25529b8b58e0SJonathan Lemon tp->t_rtttime = 0; 2553df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 2554df8bae1dSRodney W. Grimes 2555df8bae1dSRodney W. Grimes /* 2556df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 2557df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 2558df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 2559df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 2560df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 2561df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 2562df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 2563df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 2564df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 2565df8bae1dSRodney W. Grimes */ 2566233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 25679e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2568df8bae1dSRodney W. Grimes 2569df8bae1dSRodney W. Grimes /* 2570df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 2571df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 2572df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 2573df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 2574df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 2575df8bae1dSRodney W. Grimes */ 2576df8bae1dSRodney W. Grimes tp->t_softerror = 0; 2577df8bae1dSRodney W. Grimes } 2578df8bae1dSRodney W. Grimes 2579df8bae1dSRodney W. Grimes /* 2580df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 2581df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 2582df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 2583df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 2584df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2585df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 2586df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 2587df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 2588df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 2589df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 2590df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 2591df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 2592df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 2593a0292f23SGarrett Wollman * 2594a0292f23SGarrett Wollman * Also take into account the space needed for options that we 2595a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 2596a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 2597a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 2598a0292f23SGarrett Wollman * data in maxopd. 2599a0292f23SGarrett Wollman * 2600a0292f23SGarrett Wollman * NOTE that this routine is only called when we process an incoming 2601a0292f23SGarrett Wollman * segment, for outgoing segments only tcp_mssopt is called. 2602a0292f23SGarrett Wollman * 2603a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 2604a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 2605a0292f23SGarrett Wollman * MSS of our peer. 2606df8bae1dSRodney W. Grimes */ 2607a0292f23SGarrett Wollman void 2608df8bae1dSRodney W. Grimes tcp_mss(tp, offer) 2609a0292f23SGarrett Wollman struct tcpcb *tp; 2610a0292f23SGarrett Wollman int offer; 2611df8bae1dSRodney W. Grimes { 2612df8bae1dSRodney W. Grimes register struct rtentry *rt; 2613df8bae1dSRodney W. Grimes struct ifnet *ifp; 2614df8bae1dSRodney W. Grimes register int rtt, mss; 2615df8bae1dSRodney W. Grimes u_long bufsize; 2616df8bae1dSRodney W. Grimes struct inpcb *inp; 2617df8bae1dSRodney W. Grimes struct socket *so; 2618a0292f23SGarrett Wollman struct rmxp_tao *taop; 2619a0292f23SGarrett Wollman int origoffer = offer; 2620fb59c426SYoshinobu Inoue #ifdef INET6 2621fb59c426SYoshinobu Inoue int isipv6; 2622fb59c426SYoshinobu Inoue int min_protoh; 2623fb59c426SYoshinobu Inoue #endif 2624df8bae1dSRodney W. Grimes 2625df8bae1dSRodney W. Grimes inp = tp->t_inpcb; 2626fb59c426SYoshinobu Inoue #ifdef INET6 2627fb59c426SYoshinobu Inoue isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2628fb59c426SYoshinobu Inoue min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr) 2629fb59c426SYoshinobu Inoue : sizeof (struct tcpiphdr); 2630fb59c426SYoshinobu Inoue #else 2631fb59c426SYoshinobu Inoue #define min_protoh (sizeof (struct tcpiphdr)) 2632fb59c426SYoshinobu Inoue #endif 2633fb59c426SYoshinobu Inoue #ifdef INET6 2634fb59c426SYoshinobu Inoue if (isipv6) 2635fb59c426SYoshinobu Inoue rt = tcp_rtlookup6(inp); 2636fb59c426SYoshinobu Inoue else 2637fb59c426SYoshinobu Inoue #endif 2638fb59c426SYoshinobu Inoue rt = tcp_rtlookup(inp); 2639fb59c426SYoshinobu Inoue if (rt == NULL) { 2640fb59c426SYoshinobu Inoue tp->t_maxopd = tp->t_maxseg = 2641fb59c426SYoshinobu Inoue #ifdef INET6 2642fb59c426SYoshinobu Inoue isipv6 ? tcp_v6mssdflt : 2643fb59c426SYoshinobu Inoue #endif /* INET6 */ 2644fb59c426SYoshinobu Inoue tcp_mssdflt; 2645a0292f23SGarrett Wollman return; 2646df8bae1dSRodney W. Grimes } 2647df8bae1dSRodney W. Grimes ifp = rt->rt_ifp; 2648df8bae1dSRodney W. Grimes so = inp->inp_socket; 2649df8bae1dSRodney W. Grimes 2650a0292f23SGarrett Wollman taop = rmx_taop(rt->rt_rmx); 2651a0292f23SGarrett Wollman /* 2652a0292f23SGarrett Wollman * Offer == -1 means that we didn't receive SYN yet, 2653a0292f23SGarrett Wollman * use cached value in that case; 2654a0292f23SGarrett Wollman */ 2655a0292f23SGarrett Wollman if (offer == -1) 2656a0292f23SGarrett Wollman offer = taop->tao_mssopt; 2657a0292f23SGarrett Wollman /* 2658a0292f23SGarrett Wollman * Offer == 0 means that there was no MSS on the SYN segment, 2659a0292f23SGarrett Wollman * in this case we use tcp_mssdflt. 2660a0292f23SGarrett Wollman */ 2661a0292f23SGarrett Wollman if (offer == 0) 2662fb59c426SYoshinobu Inoue offer = 2663fb59c426SYoshinobu Inoue #ifdef INET6 2664fb59c426SYoshinobu Inoue isipv6 ? tcp_v6mssdflt : 2665fb59c426SYoshinobu Inoue #endif /* INET6 */ 2666fb59c426SYoshinobu Inoue tcp_mssdflt; 2667a0292f23SGarrett Wollman else 2668a0292f23SGarrett Wollman /* 2669a0292f23SGarrett Wollman * Sanity check: make sure that maxopd will be large 2670a0292f23SGarrett Wollman * enough to allow some data on segments even is the 2671a0292f23SGarrett Wollman * all the option space is used (40bytes). Otherwise 2672a0292f23SGarrett Wollman * funny things may happen in tcp_output. 2673a0292f23SGarrett Wollman */ 2674a0292f23SGarrett Wollman offer = max(offer, 64); 2675a0292f23SGarrett Wollman taop->tao_mssopt = offer; 2676a0292f23SGarrett Wollman 2677df8bae1dSRodney W. Grimes /* 2678df8bae1dSRodney W. Grimes * While we're here, check if there's an initial rtt 2679df8bae1dSRodney W. Grimes * or rttvar. Convert from the route-table units 2680df8bae1dSRodney W. Grimes * to scaled multiples of the slow timeout timer. 2681df8bae1dSRodney W. Grimes */ 2682df8bae1dSRodney W. Grimes if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 2683df8bae1dSRodney W. Grimes /* 2684a0292f23SGarrett Wollman * XXX the lock bit for RTT indicates that the value 2685df8bae1dSRodney W. Grimes * is also a minimum value; this is subject to time. 2686df8bae1dSRodney W. Grimes */ 2687df8bae1dSRodney W. Grimes if (rt->rt_rmx.rmx_locks & RTV_RTT) 26889b8b58e0SJonathan Lemon tp->t_rttmin = rtt / (RTM_RTTUNIT / hz); 26899b8b58e0SJonathan Lemon tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE)); 2690dd224982SGarrett Wollman tcpstat.tcps_usedrtt++; 2691dd224982SGarrett Wollman if (rt->rt_rmx.rmx_rttvar) { 2692df8bae1dSRodney W. Grimes tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 26939b8b58e0SJonathan Lemon (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE)); 2694dd224982SGarrett Wollman tcpstat.tcps_usedrttvar++; 2695dd224982SGarrett Wollman } else { 2696df8bae1dSRodney W. Grimes /* default variation is +- 1 rtt */ 2697df8bae1dSRodney W. Grimes tp->t_rttvar = 2698df8bae1dSRodney W. Grimes tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 2699dd224982SGarrett Wollman } 2700df8bae1dSRodney W. Grimes TCPT_RANGESET(tp->t_rxtcur, 2701df8bae1dSRodney W. Grimes ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 2702df8bae1dSRodney W. Grimes tp->t_rttmin, TCPTV_REXMTMAX); 2703df8bae1dSRodney W. Grimes } 2704df8bae1dSRodney W. Grimes /* 2705df8bae1dSRodney W. Grimes * if there's an mtu associated with the route, use it 2706fb59c426SYoshinobu Inoue * else, use the link mtu. 2707df8bae1dSRodney W. Grimes */ 2708df8bae1dSRodney W. Grimes if (rt->rt_rmx.rmx_mtu) 2709fb59c426SYoshinobu Inoue mss = rt->rt_rmx.rmx_mtu - min_protoh; 2710df8bae1dSRodney W. Grimes else 2711df8bae1dSRodney W. Grimes { 2712fb59c426SYoshinobu Inoue mss = 2713fb59c426SYoshinobu Inoue #ifdef INET6 2714fb59c426SYoshinobu Inoue (isipv6 ? nd_ifinfo[rt->rt_ifp->if_index].linkmtu : 2715fb59c426SYoshinobu Inoue #endif 2716fb59c426SYoshinobu Inoue ifp->if_mtu 2717fb59c426SYoshinobu Inoue #ifdef INET6 2718fb59c426SYoshinobu Inoue ) 2719fb59c426SYoshinobu Inoue #endif 2720fb59c426SYoshinobu Inoue - min_protoh; 2721fb59c426SYoshinobu Inoue #ifdef INET6 2722fb59c426SYoshinobu Inoue if (isipv6) { 2723fb59c426SYoshinobu Inoue if (!in6_localaddr(&inp->in6p_faddr)) 2724fb59c426SYoshinobu Inoue mss = min(mss, tcp_v6mssdflt); 2725fb59c426SYoshinobu Inoue } else 2726fb59c426SYoshinobu Inoue #endif 2727a0292f23SGarrett Wollman if (!in_localaddr(inp->inp_faddr)) 2728a0292f23SGarrett Wollman mss = min(mss, tcp_mssdflt); 2729a0292f23SGarrett Wollman } 2730a0292f23SGarrett Wollman mss = min(mss, offer); 2731a0292f23SGarrett Wollman /* 2732a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 2733a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 2734a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 2735a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 2736a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 2737a0292f23SGarrett Wollman */ 2738a0292f23SGarrett Wollman tp->t_maxopd = mss; 2739a0292f23SGarrett Wollman 2740a0292f23SGarrett Wollman /* 2741a0292f23SGarrett Wollman * In case of T/TCP, origoffer==-1 indicates, that no segments 2742a0292f23SGarrett Wollman * were received yet. In this case we just guess, otherwise 2743a0292f23SGarrett Wollman * we do the same as before T/TCP. 2744a0292f23SGarrett Wollman */ 2745a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2746a0292f23SGarrett Wollman (origoffer == -1 || 2747a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2748a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 2749a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 2750a0292f23SGarrett Wollman (origoffer == -1 || 2751a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)) 2752a0292f23SGarrett Wollman mss -= TCPOLEN_CC_APPA; 2753a0292f23SGarrett Wollman 2754df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 2755df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2756df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 2757df8bae1dSRodney W. Grimes #else 2758df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2759df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 2760df8bae1dSRodney W. Grimes #endif 2761df8bae1dSRodney W. Grimes /* 2762df8bae1dSRodney W. Grimes * If there's a pipesize, change the socket buffer 2763df8bae1dSRodney W. Grimes * to that size. Make the socket buffers an integral 2764df8bae1dSRodney W. Grimes * number of mss units; if the mss is larger than 2765df8bae1dSRodney W. Grimes * the socket buffer, decrease the mss. 2766df8bae1dSRodney W. Grimes */ 2767df8bae1dSRodney W. Grimes #ifdef RTV_SPIPE 2768df8bae1dSRodney W. Grimes if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 2769df8bae1dSRodney W. Grimes #endif 2770df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 2771df8bae1dSRodney W. Grimes if (bufsize < mss) 2772df8bae1dSRodney W. Grimes mss = bufsize; 2773df8bae1dSRodney W. Grimes else { 2774df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2775df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2776df8bae1dSRodney W. Grimes bufsize = sb_max; 2777ecf72308SBrian Feldman (void)sbreserve(&so->so_snd, bufsize, so, NULL); 2778df8bae1dSRodney W. Grimes } 2779df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 2780df8bae1dSRodney W. Grimes 2781df8bae1dSRodney W. Grimes #ifdef RTV_RPIPE 2782df8bae1dSRodney W. Grimes if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 2783df8bae1dSRodney W. Grimes #endif 2784df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 2785df8bae1dSRodney W. Grimes if (bufsize > mss) { 2786df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2787df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2788df8bae1dSRodney W. Grimes bufsize = sb_max; 2789ecf72308SBrian Feldman (void)sbreserve(&so->so_rcv, bufsize, so, NULL); 2790df8bae1dSRodney W. Grimes } 27919b8b58e0SJonathan Lemon 2792a0292f23SGarrett Wollman /* 27939b8b58e0SJonathan Lemon * Set the slow-start flight size depending on whether this 27949b8b58e0SJonathan Lemon * is a local network or not. 2795a0292f23SGarrett Wollman */ 2796fb59c426SYoshinobu Inoue if ( 2797fb59c426SYoshinobu Inoue #ifdef INET6 2798fb59c426SYoshinobu Inoue (isipv6 && in6_localaddr(&inp->in6p_faddr)) || 2799fb59c426SYoshinobu Inoue (!isipv6 && 2800fb59c426SYoshinobu Inoue #endif 2801fb59c426SYoshinobu Inoue in_localaddr(inp->inp_faddr) 2802fb59c426SYoshinobu Inoue #ifdef INET6 2803fb59c426SYoshinobu Inoue ) 2804fb59c426SYoshinobu Inoue #endif 2805fb59c426SYoshinobu Inoue ) 28069b8b58e0SJonathan Lemon tp->snd_cwnd = mss * ss_fltsz_local; 28079b8b58e0SJonathan Lemon else 28089b8b58e0SJonathan Lemon tp->snd_cwnd = mss * ss_fltsz; 2809df8bae1dSRodney W. Grimes 2810df8bae1dSRodney W. Grimes if (rt->rt_rmx.rmx_ssthresh) { 2811df8bae1dSRodney W. Grimes /* 2812df8bae1dSRodney W. Grimes * There's some sort of gateway or interface 2813df8bae1dSRodney W. Grimes * buffer limit on the path. Use this to set 2814df8bae1dSRodney W. Grimes * the slow start threshhold, but set the 2815df8bae1dSRodney W. Grimes * threshold to no less than 2*mss. 2816df8bae1dSRodney W. Grimes */ 2817df8bae1dSRodney W. Grimes tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 2818dd224982SGarrett Wollman tcpstat.tcps_usedssthresh++; 2819df8bae1dSRodney W. Grimes } 2820a0292f23SGarrett Wollman } 2821a0292f23SGarrett Wollman 2822a0292f23SGarrett Wollman /* 2823a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 2824a0292f23SGarrett Wollman */ 2825a0292f23SGarrett Wollman int 2826a0292f23SGarrett Wollman tcp_mssopt(tp) 2827a0292f23SGarrett Wollman struct tcpcb *tp; 2828a0292f23SGarrett Wollman { 2829a0292f23SGarrett Wollman struct rtentry *rt; 2830fb59c426SYoshinobu Inoue #ifdef INET6 2831fb59c426SYoshinobu Inoue int isipv6; 2832fb59c426SYoshinobu Inoue int min_protoh; 2833fb59c426SYoshinobu Inoue #endif 2834a0292f23SGarrett Wollman 2835fb59c426SYoshinobu Inoue #ifdef INET6 2836fb59c426SYoshinobu Inoue isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2837fb59c426SYoshinobu Inoue min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr) 2838fb59c426SYoshinobu Inoue : sizeof (struct tcpiphdr); 2839fb59c426SYoshinobu Inoue #else 2840fb59c426SYoshinobu Inoue #define min_protoh (sizeof (struct tcpiphdr)) 2841fb59c426SYoshinobu Inoue #endif 2842fb59c426SYoshinobu Inoue #ifdef INET6 2843fb59c426SYoshinobu Inoue if (isipv6) 2844fb59c426SYoshinobu Inoue rt = tcp_rtlookup6(tp->t_inpcb); 2845fb59c426SYoshinobu Inoue else 2846fb59c426SYoshinobu Inoue #endif /* INET6 */ 2847a0292f23SGarrett Wollman rt = tcp_rtlookup(tp->t_inpcb); 2848a0292f23SGarrett Wollman if (rt == NULL) 2849fb59c426SYoshinobu Inoue return 2850fb59c426SYoshinobu Inoue #ifdef INET6 2851fb59c426SYoshinobu Inoue isipv6 ? tcp_v6mssdflt : 2852fb59c426SYoshinobu Inoue #endif /* INET6 */ 2853fb59c426SYoshinobu Inoue tcp_mssdflt; 2854a0292f23SGarrett Wollman 2855fb59c426SYoshinobu Inoue return rt->rt_ifp->if_mtu - min_protoh; 2856df8bae1dSRodney W. Grimes } 285746f58482SJonathan Lemon 285846f58482SJonathan Lemon 285946f58482SJonathan Lemon /* 286046f58482SJonathan Lemon * Checks for partial ack. If partial ack arrives, force the retransmission 286146f58482SJonathan Lemon * of the next unacknowledged segment, do not clear tp->t_dupacks, and return 286246f58482SJonathan Lemon * 1. By setting snd_nxt to ti_ack, this forces retransmission timer to 286346f58482SJonathan Lemon * be started again. If the ack advances at least to tp->snd_recover, return 0. 286446f58482SJonathan Lemon */ 286546f58482SJonathan Lemon static int 286646f58482SJonathan Lemon tcp_newreno(tp, th) 286746f58482SJonathan Lemon struct tcpcb *tp; 286846f58482SJonathan Lemon struct tcphdr *th; 286946f58482SJonathan Lemon { 287046f58482SJonathan Lemon if (SEQ_LT(th->th_ack, tp->snd_recover)) { 287146f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 287246f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 287346f58482SJonathan Lemon 287446f58482SJonathan Lemon callout_stop(tp->tt_rexmt); 287546f58482SJonathan Lemon tp->t_rtttime = 0; 287646f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 28776b2a5f92SJayanth Vijayaraghavan /* 28786b2a5f92SJayanth Vijayaraghavan * Set snd_cwnd to one segment beyond acknowledged offset 28796b2a5f92SJayanth Vijayaraghavan * (tp->snd_una has not yet been updated when this function 28806b2a5f92SJayanth Vijayaraghavan * is called) 28816b2a5f92SJayanth Vijayaraghavan */ 28826b2a5f92SJayanth Vijayaraghavan tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 28836b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 288446f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 288546f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 288646f58482SJonathan Lemon tp->snd_nxt = onxt; 288746f58482SJonathan Lemon /* 288846f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 288946f58482SJonathan Lemon * not updated yet. 289046f58482SJonathan Lemon */ 289146f58482SJonathan Lemon tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg); 289246f58482SJonathan Lemon return (1); 289346f58482SJonathan Lemon } 289446f58482SJonathan Lemon return (0); 289546f58482SJonathan Lemon } 2896