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" 40c488362eSRobert Watson #include "opt_mac.h" 410cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 42e46cd3d4SDag-Erling Smørgrav #include "opt_tcp_input.h" 430cc12cc5SJoerg Wunsch 44df8bae1dSRodney W. Grimes #include <sys/param.h> 4598163b98SPoul-Henning Kamp #include <sys/kernel.h> 46c488362eSRobert Watson #include <sys/mac.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> 51960ed29cSSeigo Tanimura #include <sys/signalvar.h> 52df8bae1dSRodney W. Grimes #include <sys/socket.h> 53df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 54960ed29cSSeigo Tanimura #include <sys/sysctl.h> 55816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 56960ed29cSSeigo Tanimura #include <sys/systm.h> 57df8bae1dSRodney W. Grimes 58e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 59e79adb8eSGarrett Wollman 60df8bae1dSRodney W. Grimes #include <net/if.h> 61df8bae1dSRodney W. Grimes #include <net/route.h> 62df8bae1dSRodney W. Grimes 63df8bae1dSRodney W. Grimes #include <netinet/in.h> 64960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 65df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 66960ed29cSSeigo Tanimura #include <netinet/in_var.h> 67df8bae1dSRodney W. Grimes #include <netinet/ip.h> 68686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */ 69686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 70df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 71686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 72686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h> 73686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 74960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h> 75960ed29cSSeigo Tanimura #include <netinet6/nd6.h> 76df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 77df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 78df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 79df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 80df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 81fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 82df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 83610ee2f9SDavid Greenman #ifdef TCPDEBUG 84df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 85fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */ 86fb59c426SYoshinobu Inoue 87b9234fafSSam Leffler #ifdef FAST_IPSEC 88b9234fafSSam Leffler #include <netipsec/ipsec.h> 89b9234fafSSam Leffler #include <netipsec/ipsec6.h> 90b9234fafSSam Leffler #endif /*FAST_IPSEC*/ 91b9234fafSSam Leffler 92fb59c426SYoshinobu Inoue #ifdef IPSEC 93fb59c426SYoshinobu Inoue #include <netinet6/ipsec.h> 943a2a9f79SYoshinobu Inoue #include <netinet6/ipsec6.h> 95fb59c426SYoshinobu Inoue #include <netkey/key.h> 96fb59c426SYoshinobu Inoue #endif /*IPSEC*/ 97fb59c426SYoshinobu Inoue 98db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 99db4f9cc7SJonathan Lemon 100fb59c426SYoshinobu Inoue MALLOC_DEFINE(M_TSEGQ, "tseg_qent", "TCP segment queue entry"); 101df8bae1dSRodney W. Grimes 102c068736aSJeffrey Hsu static const int tcprexmtthresh = 3; 1032f96f1f4SGarrett Wollman tcp_cc tcp_ccgen; 1040312fbe9SPoul-Henning Kamp 1052f96f1f4SGarrett Wollman struct tcpstat tcpstat; 106c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW, 1073d177f46SBill Fumerola &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 1080312fbe9SPoul-Henning Kamp 109d78a37adSPaul Traina static int log_in_vain = 0; 110816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 1113d177f46SBill Fumerola &log_in_vain, 0, "Log all incoming TCP connections"); 112816a3d83SPoul-Henning Kamp 11316f7f31fSGeoff Rehmet static int blackhole = 0; 11416f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 11516f7f31fSGeoff Rehmet &blackhole, 0, "Do not send RST when dropping refused connections"); 11616f7f31fSGeoff Rehmet 117f498eeeeSDavid Greenman int tcp_delack_enabled = 1; 11884e33c9eSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 1193d177f46SBill Fumerola &tcp_delack_enabled, 0, 1203d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 121f498eeeeSDavid Greenman 122f8613305SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN 123f8613305SDag-Erling Smørgrav static int drop_synfin = 0; 124f8613305SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, 125f8613305SDag-Erling Smørgrav &drop_synfin, 0, "Drop TCP packets with SYN+FIN set"); 126f8613305SDag-Erling Smørgrav #endif 127f8613305SDag-Erling Smørgrav 12815bd2b43SDavid Greenman struct inpcbhead tcb; 129fb59c426SYoshinobu Inoue #define tcb6 tcb /* for KAME src sync over BSD*'s */ 13015bd2b43SDavid Greenman struct inpcbinfo tcbinfo; 131f76fcf6dSJeffrey Hsu struct mtx *tcbinfo_mtx; 132df8bae1dSRodney W. Grimes 1334d77a549SAlfred Perlstein static void tcp_dooptions(struct tcpopt *, u_char *, int, int); 1344d77a549SAlfred Perlstein static void tcp_pulloutofband(struct socket *, 1354d77a549SAlfred Perlstein struct tcphdr *, struct mbuf *, int); 1364d77a549SAlfred Perlstein static int tcp_reass(struct tcpcb *, struct tcphdr *, int *, 1374d77a549SAlfred Perlstein struct mbuf *); 1384d77a549SAlfred Perlstein static void tcp_xmit_timer(struct tcpcb *, int); 139c068736aSJeffrey Hsu static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 140340c35deSJonathan Lemon static int tcp_timewait(struct tcptw *, struct tcpopt *, 141340c35deSJonathan Lemon struct tcphdr *, struct mbuf *, int); 1420312fbe9SPoul-Henning Kamp 143fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 144fb59c426SYoshinobu Inoue #ifdef INET6 145fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \ 146fb59c426SYoshinobu Inoue do { \ 147fb59c426SYoshinobu Inoue if ((tp) && (tp)->t_inpcb && \ 148fb59c426SYoshinobu Inoue ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0 && \ 149fb59c426SYoshinobu Inoue (tp)->t_inpcb->in6p_route.ro_rt) \ 150686cdd19SJun-ichiro itojun Hagino nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL, 0); \ 151fb59c426SYoshinobu Inoue } while (0) 152fb59c426SYoshinobu Inoue #else 153fb59c426SYoshinobu Inoue #define ND6_HINT(tp) 154fb59c426SYoshinobu Inoue #endif 155df8bae1dSRodney W. Grimes 156df8bae1dSRodney W. Grimes /* 157262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 158262c1c1aSMatthew Dillon * - there is no delayed ack timer in progress and 159262c1c1aSMatthew Dillon * - our last ack wasn't a 0-sized window. We never want to delay 1603bfd6421SJonathan Lemon * the ack that opens up a 0-sized window and 1613bfd6421SJonathan Lemon * - delayed acks are enabled or 1623bfd6421SJonathan Lemon * - this is a half-synchronized T/TCP connection. 163d8c85a26SJonathan Lemon */ 164d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \ 1653bfd6421SJonathan Lemon (((!callout_active(tp->tt_delack) && \ 1663bfd6421SJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0)) && \ 1673bfd6421SJonathan Lemon (tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 168d8c85a26SJonathan Lemon 1690312fbe9SPoul-Henning Kamp static int 170fb59c426SYoshinobu Inoue tcp_reass(tp, th, tlenp, m) 171df8bae1dSRodney W. Grimes register struct tcpcb *tp; 172fb59c426SYoshinobu Inoue register struct tcphdr *th; 173fb59c426SYoshinobu Inoue int *tlenp; 174df8bae1dSRodney W. Grimes struct mbuf *m; 175df8bae1dSRodney W. Grimes { 176fb59c426SYoshinobu Inoue struct tseg_qent *q; 177fb59c426SYoshinobu Inoue struct tseg_qent *p = NULL; 178fb59c426SYoshinobu Inoue struct tseg_qent *nq; 179fb59c426SYoshinobu Inoue struct tseg_qent *te; 180df8bae1dSRodney W. Grimes struct socket *so = tp->t_inpcb->inp_socket; 181df8bae1dSRodney W. Grimes int flags; 182df8bae1dSRodney W. Grimes 183df8bae1dSRodney W. Grimes /* 184fb59c426SYoshinobu Inoue * Call with th==0 after become established to 185df8bae1dSRodney W. Grimes * force pre-ESTABLISHED data up to user socket. 186df8bae1dSRodney W. Grimes */ 187fb59c426SYoshinobu Inoue if (th == 0) 188df8bae1dSRodney W. Grimes goto present; 189df8bae1dSRodney W. Grimes 190fb59c426SYoshinobu Inoue /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */ 191fb59c426SYoshinobu Inoue MALLOC(te, struct tseg_qent *, sizeof (struct tseg_qent), M_TSEGQ, 192fb59c426SYoshinobu Inoue M_NOWAIT); 193fb59c426SYoshinobu Inoue if (te == NULL) { 194fb59c426SYoshinobu Inoue tcpstat.tcps_rcvmemdrop++; 195fb59c426SYoshinobu Inoue m_freem(m); 196fb59c426SYoshinobu Inoue return (0); 197fb59c426SYoshinobu Inoue } 1986effc713SDoug Rabson 199df8bae1dSRodney W. Grimes /* 200df8bae1dSRodney W. Grimes * Find a segment which begins after this one does. 201df8bae1dSRodney W. Grimes */ 202fb59c426SYoshinobu Inoue LIST_FOREACH(q, &tp->t_segq, tqe_q) { 203fb59c426SYoshinobu Inoue if (SEQ_GT(q->tqe_th->th_seq, th->th_seq)) 204df8bae1dSRodney W. Grimes break; 205fb59c426SYoshinobu Inoue p = q; 206fb59c426SYoshinobu Inoue } 207df8bae1dSRodney W. Grimes 208df8bae1dSRodney W. Grimes /* 209df8bae1dSRodney W. Grimes * If there is a preceding segment, it may provide some of 210df8bae1dSRodney W. Grimes * our data already. If so, drop the data from the incoming 211df8bae1dSRodney W. Grimes * segment. If it provides all of our data, drop us. 212df8bae1dSRodney W. Grimes */ 2136effc713SDoug Rabson if (p != NULL) { 214df8bae1dSRodney W. Grimes register int i; 215df8bae1dSRodney W. Grimes /* conversion to int (in i) handles seq wraparound */ 216fb59c426SYoshinobu Inoue i = p->tqe_th->th_seq + p->tqe_len - th->th_seq; 217df8bae1dSRodney W. Grimes if (i > 0) { 218fb59c426SYoshinobu Inoue if (i >= *tlenp) { 219df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 220fb59c426SYoshinobu Inoue tcpstat.tcps_rcvdupbyte += *tlenp; 221df8bae1dSRodney W. Grimes m_freem(m); 222fb59c426SYoshinobu Inoue FREE(te, M_TSEGQ); 223a0292f23SGarrett Wollman /* 224a0292f23SGarrett Wollman * Try to present any queued data 225a0292f23SGarrett Wollman * at the left window edge to the user. 226a0292f23SGarrett Wollman * This is needed after the 3-WHS 227a0292f23SGarrett Wollman * completes. 228a0292f23SGarrett Wollman */ 229a0292f23SGarrett Wollman goto present; /* ??? */ 230df8bae1dSRodney W. Grimes } 231df8bae1dSRodney W. Grimes m_adj(m, i); 232fb59c426SYoshinobu Inoue *tlenp -= i; 233fb59c426SYoshinobu Inoue th->th_seq += i; 234df8bae1dSRodney W. Grimes } 235df8bae1dSRodney W. Grimes } 236df8bae1dSRodney W. Grimes tcpstat.tcps_rcvoopack++; 237fb59c426SYoshinobu Inoue tcpstat.tcps_rcvoobyte += *tlenp; 238df8bae1dSRodney W. Grimes 239df8bae1dSRodney W. Grimes /* 240df8bae1dSRodney W. Grimes * While we overlap succeeding segments trim them or, 241df8bae1dSRodney W. Grimes * if they are completely covered, dequeue them. 242df8bae1dSRodney W. Grimes */ 2436effc713SDoug Rabson while (q) { 244fb59c426SYoshinobu Inoue register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq; 245df8bae1dSRodney W. Grimes if (i <= 0) 246df8bae1dSRodney W. Grimes break; 247fb59c426SYoshinobu Inoue if (i < q->tqe_len) { 248fb59c426SYoshinobu Inoue q->tqe_th->th_seq += i; 249fb59c426SYoshinobu Inoue q->tqe_len -= i; 250fb59c426SYoshinobu Inoue m_adj(q->tqe_m, i); 251df8bae1dSRodney W. Grimes break; 252df8bae1dSRodney W. Grimes } 2536effc713SDoug Rabson 254fb59c426SYoshinobu Inoue nq = LIST_NEXT(q, tqe_q); 255fb59c426SYoshinobu Inoue LIST_REMOVE(q, tqe_q); 256fb59c426SYoshinobu Inoue m_freem(q->tqe_m); 257fb59c426SYoshinobu Inoue FREE(q, M_TSEGQ); 2586effc713SDoug Rabson q = nq; 259df8bae1dSRodney W. Grimes } 260df8bae1dSRodney W. Grimes 261fb59c426SYoshinobu Inoue /* Insert the new segment queue entry into place. */ 262fb59c426SYoshinobu Inoue te->tqe_m = m; 263fb59c426SYoshinobu Inoue te->tqe_th = th; 264fb59c426SYoshinobu Inoue te->tqe_len = *tlenp; 265fb59c426SYoshinobu Inoue 2666effc713SDoug Rabson if (p == NULL) { 267fb59c426SYoshinobu Inoue LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q); 2686effc713SDoug Rabson } else { 269fb59c426SYoshinobu Inoue LIST_INSERT_AFTER(p, te, tqe_q); 2706effc713SDoug Rabson } 271df8bae1dSRodney W. Grimes 272df8bae1dSRodney W. Grimes present: 273df8bae1dSRodney W. Grimes /* 274df8bae1dSRodney W. Grimes * Present data to user, advancing rcv_nxt through 275df8bae1dSRodney W. Grimes * completed sequence space. 276df8bae1dSRodney W. Grimes */ 277a0292f23SGarrett Wollman if (!TCPS_HAVEESTABLISHED(tp->t_state)) 278df8bae1dSRodney W. Grimes return (0); 279fb59c426SYoshinobu Inoue q = LIST_FIRST(&tp->t_segq); 280fb59c426SYoshinobu Inoue if (!q || q->tqe_th->th_seq != tp->rcv_nxt) 281df8bae1dSRodney W. Grimes return (0); 282df8bae1dSRodney W. Grimes do { 283fb59c426SYoshinobu Inoue tp->rcv_nxt += q->tqe_len; 284fb59c426SYoshinobu Inoue flags = q->tqe_th->th_flags & TH_FIN; 285fb59c426SYoshinobu Inoue nq = LIST_NEXT(q, tqe_q); 286fb59c426SYoshinobu Inoue LIST_REMOVE(q, tqe_q); 2874cc20ab1SSeigo Tanimura if (so->so_state & SS_CANTRCVMORE) 288fb59c426SYoshinobu Inoue m_freem(q->tqe_m); 2894cc20ab1SSeigo Tanimura else 290fb59c426SYoshinobu Inoue sbappend(&so->so_rcv, q->tqe_m); 291fb59c426SYoshinobu Inoue FREE(q, M_TSEGQ); 2926effc713SDoug Rabson q = nq; 293fb59c426SYoshinobu Inoue } while (q && q->tqe_th->th_seq == tp->rcv_nxt); 294fb59c426SYoshinobu Inoue ND6_HINT(tp); 295df8bae1dSRodney W. Grimes sorwakeup(so); 296df8bae1dSRodney W. Grimes return (flags); 297df8bae1dSRodney W. Grimes } 298df8bae1dSRodney W. Grimes 299df8bae1dSRodney W. Grimes /* 300df8bae1dSRodney W. Grimes * TCP input routine, follows pages 65-76 of the 301df8bae1dSRodney W. Grimes * protocol specification dated September, 1981 very closely. 302df8bae1dSRodney W. Grimes */ 303fb59c426SYoshinobu Inoue #ifdef INET6 304fb59c426SYoshinobu Inoue int 305fb59c426SYoshinobu Inoue tcp6_input(mp, offp, proto) 306fb59c426SYoshinobu Inoue struct mbuf **mp; 307fb59c426SYoshinobu Inoue int *offp, proto; 308fb59c426SYoshinobu Inoue { 309fb59c426SYoshinobu Inoue register struct mbuf *m = *mp; 31033841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 311fb59c426SYoshinobu Inoue 312fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 313fb59c426SYoshinobu Inoue 314fb59c426SYoshinobu Inoue /* 315fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 316fb59c426SYoshinobu Inoue * better place to put this in? 317fb59c426SYoshinobu Inoue */ 31833841545SHajimu UMEMOTO ia6 = ip6_getdstifaddr(m); 31933841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 320fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 321fb59c426SYoshinobu Inoue 322fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 323fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 324fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 325fb59c426SYoshinobu Inoue return IPPROTO_DONE; 326fb59c426SYoshinobu Inoue } 327fb59c426SYoshinobu Inoue 328f0ffb944SJulian Elischer tcp_input(m, *offp); 329fb59c426SYoshinobu Inoue return IPPROTO_DONE; 330fb59c426SYoshinobu Inoue } 331fb59c426SYoshinobu Inoue #endif 332fb59c426SYoshinobu Inoue 333df8bae1dSRodney W. Grimes void 334f0ffb944SJulian Elischer tcp_input(m, off0) 335df8bae1dSRodney W. Grimes register struct mbuf *m; 336f0ffb944SJulian Elischer int off0; 337df8bae1dSRodney W. Grimes { 338fb59c426SYoshinobu Inoue register struct tcphdr *th; 339fb59c426SYoshinobu Inoue register struct ip *ip = NULL; 340fb59c426SYoshinobu Inoue register struct ipovly *ipov; 341f76fcf6dSJeffrey Hsu register struct inpcb *inp = NULL; 342e79adb8eSGarrett Wollman u_char *optp = NULL; 34326f9a767SRodney W. Grimes int optlen = 0; 344df8bae1dSRodney W. Grimes int len, tlen, off; 345fb59c426SYoshinobu Inoue int drop_hdrlen; 346df8bae1dSRodney W. Grimes register struct tcpcb *tp = 0; 347fb59c426SYoshinobu Inoue register int thflags; 34826f9a767SRodney W. Grimes struct socket *so = 0; 349df8bae1dSRodney W. Grimes int todrop, acked, ourfinisacked, needoutput = 0; 350a0292f23SGarrett Wollman u_long tiwin; 351a0292f23SGarrett Wollman struct tcpopt to; /* options in this segment */ 352a0292f23SGarrett Wollman struct rmxp_tao *taop; /* pointer to our TAO cache entry */ 353a0292f23SGarrett Wollman struct rmxp_tao tao_noncached; /* in case there's no cached entry */ 354f76fcf6dSJeffrey Hsu int headlocked = 0; 355c068736aSJeffrey Hsu struct sockaddr_in *next_hop = NULL; 356c068736aSJeffrey Hsu int rstreason; /* For badport_bandlim accounting purposes */ 357c068736aSJeffrey Hsu 358c068736aSJeffrey Hsu struct ip6_hdr *ip6 = NULL; 359c068736aSJeffrey Hsu #ifdef INET6 360c068736aSJeffrey Hsu int isipv6; 361c068736aSJeffrey Hsu #else 362c068736aSJeffrey Hsu const int isipv6 = 0; 363c068736aSJeffrey Hsu #endif 364f76fcf6dSJeffrey Hsu 365610ee2f9SDavid Greenman #ifdef TCPDEBUG 366c068736aSJeffrey Hsu /* 367c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 368c068736aSJeffrey Hsu * now IPv6. 369c068736aSJeffrey Hsu */ 370410bb1bfSLuigi Rizzo u_char tcp_saveipgen[40]; 371410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 372610ee2f9SDavid Greenman short ostate = 0; 373610ee2f9SDavid Greenman #endif 374c068736aSJeffrey Hsu 3752b25acc1SLuigi Rizzo /* Grab info from MT_TAG mbufs prepended to the chain. */ 3762b25acc1SLuigi Rizzo for (;m && m->m_type == MT_TAG; m = m->m_next) { 3775d846453SSam Leffler if (m->_m_tag_id == PACKET_TAG_IPFORWARD) 3782b25acc1SLuigi Rizzo next_hop = (struct sockaddr_in *)m->m_hdr.mh_data; 3792b25acc1SLuigi Rizzo } 380fb59c426SYoshinobu Inoue #ifdef INET6 381fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 382fb59c426SYoshinobu Inoue #endif 383a0292f23SGarrett Wollman bzero((char *)&to, sizeof(to)); 384a0292f23SGarrett Wollman 385df8bae1dSRodney W. Grimes tcpstat.tcps_rcvtotal++; 386fb59c426SYoshinobu Inoue 387fb59c426SYoshinobu Inoue if (isipv6) { 388fb59c426SYoshinobu Inoue /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */ 389fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 390fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 391fb59c426SYoshinobu Inoue if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 392fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbadsum++; 393fb59c426SYoshinobu Inoue goto drop; 394fb59c426SYoshinobu Inoue } 395fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 39633841545SHajimu UMEMOTO 39733841545SHajimu UMEMOTO /* 39833841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 39933841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 40033841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 40133841545SHajimu UMEMOTO * 40233841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 40333841545SHajimu UMEMOTO * already dropped in ip6_input. 40433841545SHajimu UMEMOTO */ 40533841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 40633841545SHajimu UMEMOTO /* XXX stat */ 40733841545SHajimu UMEMOTO goto drop; 40833841545SHajimu UMEMOTO } 409c068736aSJeffrey Hsu } else { 410df8bae1dSRodney W. Grimes /* 411df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 412df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 413df8bae1dSRodney W. Grimes */ 414fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 415df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 416fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 417fb59c426SYoshinobu Inoue } 418df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 419df8bae1dSRodney W. Grimes if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 420df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 421df8bae1dSRodney W. Grimes return; 422df8bae1dSRodney W. Grimes } 423df8bae1dSRodney W. Grimes } 424fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 425fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 426db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 427db4f9cc7SJonathan Lemon tlen = ip->ip_len; 428df8bae1dSRodney W. Grimes 429db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 430db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 431db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 432db4f9cc7SJonathan Lemon else 433db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 434c068736aSJeffrey Hsu ip->ip_dst.s_addr, 435c068736aSJeffrey Hsu htonl(m->m_pkthdr.csum_data + 436c068736aSJeffrey Hsu ip->ip_len + 437c068736aSJeffrey Hsu IPPROTO_TCP)); 438db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 439db4f9cc7SJonathan Lemon } else { 440df8bae1dSRodney W. Grimes /* 441df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 442df8bae1dSRodney W. Grimes */ 443df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 444fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 445fb59c426SYoshinobu Inoue ipov->ih_len = (u_short)tlen; 446fd8e4ebcSMike Barcroft ipov->ih_len = htons(ipov->ih_len); 447fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 448db4f9cc7SJonathan Lemon } 449fb59c426SYoshinobu Inoue if (th->th_sum) { 450df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadsum++; 451df8bae1dSRodney W. Grimes goto drop; 452df8bae1dSRodney W. Grimes } 453fb59c426SYoshinobu Inoue #ifdef INET6 454fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 455fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 456fb59c426SYoshinobu Inoue #endif 457fb59c426SYoshinobu Inoue } 458df8bae1dSRodney W. Grimes 459df8bae1dSRodney W. Grimes /* 460df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 461df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 462df8bae1dSRodney W. Grimes */ 463fb59c426SYoshinobu Inoue off = th->th_off << 2; 464df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 465df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadoff++; 466df8bae1dSRodney W. Grimes goto drop; 467df8bae1dSRodney W. Grimes } 468fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 469df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 470fb59c426SYoshinobu Inoue if (isipv6) { 471fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 472fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 473fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 474c068736aSJeffrey Hsu } else { 475df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 476c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 477c068736aSJeffrey Hsu == 0) { 478df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 479df8bae1dSRodney W. Grimes return; 480df8bae1dSRodney W. Grimes } 481fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 482fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 483fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 484fb59c426SYoshinobu Inoue } 485df8bae1dSRodney W. Grimes } 486df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 487fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 488df8bae1dSRodney W. Grimes } 489fb59c426SYoshinobu Inoue thflags = th->th_flags; 490df8bae1dSRodney W. Grimes 491e46cd3d4SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN 492e46cd3d4SDag-Erling Smørgrav /* 493e46cd3d4SDag-Erling Smørgrav * If the drop_synfin option is enabled, drop all packets with 494e46cd3d4SDag-Erling Smørgrav * both the SYN and FIN bits set. This prevents e.g. nmap from 495e46cd3d4SDag-Erling Smørgrav * identifying the TCP/IP stack. 496e46cd3d4SDag-Erling Smørgrav * 497a589a70eSGarrett Wollman * This is a violation of the TCP specification. 498e46cd3d4SDag-Erling Smørgrav */ 499fb59c426SYoshinobu Inoue if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN)) 500e46cd3d4SDag-Erling Smørgrav goto drop; 501e46cd3d4SDag-Erling Smørgrav #endif 502e46cd3d4SDag-Erling Smørgrav 503df8bae1dSRodney W. Grimes /* 504df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 505df8bae1dSRodney W. Grimes */ 506fd8e4ebcSMike Barcroft th->th_seq = ntohl(th->th_seq); 507fd8e4ebcSMike Barcroft th->th_ack = ntohl(th->th_ack); 508fd8e4ebcSMike Barcroft th->th_win = ntohs(th->th_win); 509fd8e4ebcSMike Barcroft th->th_urp = ntohs(th->th_urp); 510df8bae1dSRodney W. Grimes 511df8bae1dSRodney W. Grimes /* 5123bfd6421SJonathan Lemon * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options, 513fb59c426SYoshinobu Inoue * until after ip6_savecontrol() is called and before other functions 514fb59c426SYoshinobu Inoue * which don't want those proto headers. 515fb59c426SYoshinobu Inoue * Because ip6_savecontrol() is going to parse the mbuf to 516fb59c426SYoshinobu Inoue * search for data to be passed up to user-land, it wants mbuf 517fb59c426SYoshinobu Inoue * parameters to be unchanged. 51888ff5695SSUZUKI Shinsuke * XXX: the call of ip6_savecontrol() has been obsoleted based on 51988ff5695SSUZUKI Shinsuke * latest version of the advanced API (20020110). 520755c1f07SAndras Olah */ 521fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 522755c1f07SAndras Olah 523755c1f07SAndras Olah /* 524df8bae1dSRodney W. Grimes * Locate pcb for segment. 525df8bae1dSRodney W. Grimes */ 526f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 527f76fcf6dSJeffrey Hsu headlocked = 1; 528df8bae1dSRodney W. Grimes findpcb: 5292b25acc1SLuigi Rizzo /* IPFIREWALL_FORWARD section */ 530c068736aSJeffrey Hsu if (next_hop != NULL && isipv6 == 0) { /* IPv6 support is not yet */ 531f9e354dfSJulian Elischer /* 5322b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 533f9e354dfSJulian Elischer * already got one like this? 534f9e354dfSJulian Elischer */ 535fb59c426SYoshinobu Inoue inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, 536c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 537c068736aSJeffrey Hsu 0, m->m_pkthdr.rcvif); 538f9e354dfSJulian Elischer if (!inp) { 539c068736aSJeffrey Hsu /* It's new. Try find the ambushing socket. */ 540f9e354dfSJulian Elischer inp = in_pcblookup_hash(&tcbinfo, 541fb59c426SYoshinobu Inoue ip->ip_src, th->th_sport, 5422b25acc1SLuigi Rizzo next_hop->sin_addr, 543c068736aSJeffrey Hsu next_hop->sin_port ? 544c068736aSJeffrey Hsu ntohs(next_hop->sin_port) : 545c068736aSJeffrey Hsu th->th_dport, 546c068736aSJeffrey Hsu 1, m->m_pkthdr.rcvif); 547f9e354dfSJulian Elischer } 548c068736aSJeffrey Hsu } else { 549fb59c426SYoshinobu Inoue if (isipv6) 550c068736aSJeffrey Hsu inp = in6_pcblookup_hash(&tcbinfo, 551c068736aSJeffrey Hsu &ip6->ip6_src, th->th_sport, 552c068736aSJeffrey Hsu &ip6->ip6_dst, th->th_dport, 553c068736aSJeffrey Hsu 1, m->m_pkthdr.rcvif); 554fb59c426SYoshinobu Inoue else 555c068736aSJeffrey Hsu inp = in_pcblookup_hash(&tcbinfo, 556c068736aSJeffrey Hsu ip->ip_src, th->th_sport, 557c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 558c068736aSJeffrey Hsu 1, m->m_pkthdr.rcvif); 559fb59c426SYoshinobu Inoue } 560f9e354dfSJulian Elischer 561fb59c426SYoshinobu Inoue #ifdef IPSEC 562fb59c426SYoshinobu Inoue if (isipv6) { 563fb59c426SYoshinobu Inoue if (inp != NULL && ipsec6_in_reject_so(m, inp->inp_socket)) { 564fb59c426SYoshinobu Inoue ipsec6stat.in_polvio++; 565fb59c426SYoshinobu Inoue goto drop; 566fb59c426SYoshinobu Inoue } 567c068736aSJeffrey Hsu } else { 568fb59c426SYoshinobu Inoue if (inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) { 569fb59c426SYoshinobu Inoue ipsecstat.in_polvio++; 570fb59c426SYoshinobu Inoue goto drop; 571fb59c426SYoshinobu Inoue } 572c068736aSJeffrey Hsu } 573c068736aSJeffrey Hsu #endif 574b9234fafSSam Leffler #ifdef FAST_IPSEC 575b9234fafSSam Leffler if (isipv6) { 576b9234fafSSam Leffler if (inp != NULL && ipsec6_in_reject(m, inp)) { 577b9234fafSSam Leffler goto drop; 578b9234fafSSam Leffler } 57958fcadfcSSam Leffler } else 58058fcadfcSSam Leffler if (inp != NULL && ipsec4_in_reject(m, inp)) { 581b9234fafSSam Leffler goto drop; 582b9234fafSSam Leffler } 583b9234fafSSam Leffler #endif /*FAST_IPSEC*/ 584df8bae1dSRodney W. Grimes 585df8bae1dSRodney W. Grimes /* 586df8bae1dSRodney W. Grimes * If the state is CLOSED (i.e., TCB does not exist) then 587df8bae1dSRodney W. Grimes * all data in the incoming segment is discarded. 588df8bae1dSRodney W. Grimes * If the TCB exists but is in CLOSED state, it is embryonic, 589df8bae1dSRodney W. Grimes * but should either do a listen or a connect soon. 590df8bae1dSRodney W. Grimes */ 591816a3d83SPoul-Henning Kamp if (inp == NULL) { 5922e4e1b4cSGeoff Rehmet if (log_in_vain) { 593fb59c426SYoshinobu Inoue #ifdef INET6 594ded7008aSJuli Mallett char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2]; 595c068736aSJeffrey Hsu #else 596fb59c426SYoshinobu Inoue char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"]; 597c068736aSJeffrey Hsu #endif 59875cfc95fSAndrey A. Chernov 599fb59c426SYoshinobu Inoue if (isipv6) { 600ded7008aSJuli Mallett strcpy(dbuf, "["); 601ded7008aSJuli Mallett strcpy(sbuf, "["); 602ded7008aSJuli Mallett strcat(dbuf, ip6_sprintf(&ip6->ip6_dst)); 603ded7008aSJuli Mallett strcat(sbuf, ip6_sprintf(&ip6->ip6_src)); 604ded7008aSJuli Mallett strcat(dbuf, "]"); 605ded7008aSJuli Mallett strcat(sbuf, "]"); 606c068736aSJeffrey Hsu } else { 607fb59c426SYoshinobu Inoue strcpy(dbuf, inet_ntoa(ip->ip_dst)); 608fb59c426SYoshinobu Inoue strcpy(sbuf, inet_ntoa(ip->ip_src)); 609fb59c426SYoshinobu Inoue } 6102e4e1b4cSGeoff Rehmet switch (log_in_vain) { 6112e4e1b4cSGeoff Rehmet case 1: 61239eb27a4SCrist J. Clark if ((thflags & TH_SYN) == 0) 6132e4e1b4cSGeoff Rehmet break; 6142e4e1b4cSGeoff Rehmet case 2: 6152e4e1b4cSGeoff Rehmet log(LOG_INFO, 616c068736aSJeffrey Hsu "Connection attempt to TCP %s:%d " 61739eb27a4SCrist J. Clark "from %s:%d flags:0x%02x\n", 618fb59c426SYoshinobu Inoue dbuf, ntohs(th->th_dport), sbuf, 619fb59c426SYoshinobu Inoue ntohs(th->th_sport), thflags); 6202e4e1b4cSGeoff Rehmet break; 6212e4e1b4cSGeoff Rehmet default: 6222e4e1b4cSGeoff Rehmet break; 6232e4e1b4cSGeoff Rehmet } 62475cfc95fSAndrey A. Chernov } 6252e4e1b4cSGeoff Rehmet if (blackhole) { 6262e4e1b4cSGeoff Rehmet switch (blackhole) { 627828b7f40SGeoff Rehmet case 1: 628fb59c426SYoshinobu Inoue if (thflags & TH_SYN) 629828b7f40SGeoff Rehmet goto drop; 630828b7f40SGeoff Rehmet break; 631828b7f40SGeoff Rehmet case 2: 632828b7f40SGeoff Rehmet goto drop; 633828b7f40SGeoff Rehmet default: 634828b7f40SGeoff Rehmet goto drop; 6352e4e1b4cSGeoff Rehmet } 636828b7f40SGeoff Rehmet } 637a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 638a57815efSBosko Milekic goto dropwithreset; 639816a3d83SPoul-Henning Kamp } 640f76fcf6dSJeffrey Hsu INP_LOCK(inp); 641340c35deSJonathan Lemon if (inp->inp_vflag & INP_TIMEWAIT) { 642340c35deSJonathan Lemon /* 643340c35deSJonathan Lemon * The only option of relevance is TOF_CC, and only if 644340c35deSJonathan Lemon * present in a SYN segment. See tcp_timewait(). 645340c35deSJonathan Lemon */ 646340c35deSJonathan Lemon if (thflags & TH_SYN) 647340c35deSJonathan Lemon tcp_dooptions(&to, optp, optlen, 1); 648340c35deSJonathan Lemon if (tcp_timewait((struct tcptw *)inp->inp_ppcb, 649340c35deSJonathan Lemon &to, th, m, tlen)) 650340c35deSJonathan Lemon goto findpcb; 651340c35deSJonathan Lemon /* 652340c35deSJonathan Lemon * tcp_timewait unlocks inp. 653340c35deSJonathan Lemon */ 654340c35deSJonathan Lemon INP_INFO_WUNLOCK(&tcbinfo); 655340c35deSJonathan Lemon return; 656340c35deSJonathan Lemon } 657df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 65809f81a46SBosko Milekic if (tp == 0) { 659f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 660a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 661a57815efSBosko Milekic goto dropwithreset; 66209f81a46SBosko Milekic } 663df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_CLOSED) 664df8bae1dSRodney W. Grimes goto drop; 665df8bae1dSRodney W. Grimes 666df8bae1dSRodney W. Grimes /* Unscale the window into a 32-bit value. */ 667fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) == 0) 668fb59c426SYoshinobu Inoue tiwin = th->th_win << tp->snd_scale; 669df8bae1dSRodney W. Grimes else 670fb59c426SYoshinobu Inoue tiwin = th->th_win; 671fb59c426SYoshinobu Inoue 672df8bae1dSRodney W. Grimes so = inp->inp_socket; 673c488362eSRobert Watson #ifdef MAC 674540e8b7eSJeffrey Hsu if (mac_check_socket_deliver(so, m)) 675c488362eSRobert Watson goto drop; 676c488362eSRobert Watson #endif 677610ee2f9SDavid Greenman #ifdef TCPDEBUG 678df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 679df8bae1dSRodney W. Grimes ostate = tp->t_state; 680fb59c426SYoshinobu Inoue if (isipv6) 681540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 682fb59c426SYoshinobu Inoue else 683540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 684fb59c426SYoshinobu Inoue tcp_savetcp = *th; 685df8bae1dSRodney W. Grimes } 686610ee2f9SDavid Greenman #endif 687540e8b7eSJeffrey Hsu if (so->so_options & SO_ACCEPTCONN) { 688540e8b7eSJeffrey Hsu struct in_conninfo inc; 689540e8b7eSJeffrey Hsu 690fb59c426SYoshinobu Inoue #ifdef INET6 691be2ac88cSJonathan Lemon inc.inc_isipv6 = isipv6; 692c068736aSJeffrey Hsu #endif 693be2ac88cSJonathan Lemon if (isipv6) { 694be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 695be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 696be2ac88cSJonathan Lemon inc.inc6_route.ro_rt = NULL; /* XXX */ 697c068736aSJeffrey Hsu } else { 698be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 699be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 700be2ac88cSJonathan Lemon inc.inc_route.ro_rt = NULL; /* XXX */ 701be2ac88cSJonathan Lemon } 702be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 703be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 704be2ac88cSJonathan Lemon 705fb59c426SYoshinobu Inoue /* 706be2ac88cSJonathan Lemon * If the state is LISTEN then ignore segment if it contains 707be2ac88cSJonathan Lemon * a RST. If the segment contains an ACK then it is bad and 708be2ac88cSJonathan Lemon * send a RST. If it does not contain a SYN then it is not 709be2ac88cSJonathan Lemon * interesting; drop it. 710be2ac88cSJonathan Lemon * 711be2ac88cSJonathan Lemon * If the state is SYN_RECEIVED (syncache) and seg contains 712be2ac88cSJonathan Lemon * an ACK, but not for our SYN/ACK, send a RST. If the seg 713be2ac88cSJonathan Lemon * contains a RST, check the sequence number to see if it 714be2ac88cSJonathan Lemon * is a valid reset segment. 715fb59c426SYoshinobu Inoue */ 716fb59c426SYoshinobu Inoue if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 717be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 718be2ac88cSJonathan Lemon if (!syncache_expand(&inc, th, &so, m)) { 7194195b4afSPaul Traina /* 720be2ac88cSJonathan Lemon * No syncache entry, or ACK was not 721be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 7224195b4afSPaul Traina */ 723be2ac88cSJonathan Lemon tcpstat.tcps_badsyn++; 724be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 725be2ac88cSJonathan Lemon goto dropwithreset; 726be2ac88cSJonathan Lemon } 727f76fcf6dSJeffrey Hsu if (so == NULL) { 728be2ac88cSJonathan Lemon /* 729be2ac88cSJonathan Lemon * Could not complete 3-way handshake, 730be2ac88cSJonathan Lemon * connection is being closed down, and 731be2ac88cSJonathan Lemon * syncache will free mbuf. 732be2ac88cSJonathan Lemon */ 733f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 734f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 735be2ac88cSJonathan Lemon return; 736f76fcf6dSJeffrey Hsu } 737be2ac88cSJonathan Lemon /* 738be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 739be2ac88cSJonathan Lemon * Continue processing segment. 740be2ac88cSJonathan Lemon */ 741f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 742be2ac88cSJonathan Lemon inp = sotoinpcb(so); 743f76fcf6dSJeffrey Hsu INP_LOCK(inp); 744be2ac88cSJonathan Lemon tp = intotcpcb(inp); 745be2ac88cSJonathan Lemon /* 746be2ac88cSJonathan Lemon * This is what would have happened in 747e6658b12SRobert Watson * tcp_output() when the SYN,ACK was sent. 748be2ac88cSJonathan Lemon */ 749be2ac88cSJonathan Lemon tp->snd_up = tp->snd_una; 750be2ac88cSJonathan Lemon tp->snd_max = tp->snd_nxt = tp->iss + 1; 751be2ac88cSJonathan Lemon tp->last_ack_sent = tp->rcv_nxt; 752be2ac88cSJonathan Lemon /* 75341446225SJonathan Lemon * RFC1323: The window in SYN & SYN/ACK 75441446225SJonathan Lemon * segments is never scaled. 755be2ac88cSJonathan Lemon */ 756be2ac88cSJonathan Lemon tp->snd_wnd = tiwin; /* unscaled */ 757be2ac88cSJonathan Lemon goto after_listen; 758be2ac88cSJonathan Lemon } 759be2ac88cSJonathan Lemon if (thflags & TH_RST) { 760be2ac88cSJonathan Lemon syncache_chkrst(&inc, th); 761be2ac88cSJonathan Lemon goto drop; 762be2ac88cSJonathan Lemon } 763fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 764be2ac88cSJonathan Lemon syncache_badack(&inc); 765ebb0cbeaSPaul Traina tcpstat.tcps_badsyn++; 766a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 767a57815efSBosko Milekic goto dropwithreset; 7684195b4afSPaul Traina } 769ebb0cbeaSPaul Traina goto drop; 770ebb0cbeaSPaul Traina } 77133841545SHajimu UMEMOTO 772be2ac88cSJonathan Lemon /* 773be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 774be2ac88cSJonathan Lemon */ 77533841545SHajimu UMEMOTO #ifdef INET6 77633841545SHajimu UMEMOTO /* 77733841545SHajimu UMEMOTO * If deprecated address is forbidden, 77833841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 77933841545SHajimu UMEMOTO * address to prevent any new inbound connection from 78033841545SHajimu UMEMOTO * getting established. 78133841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 78233841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 78333841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 78433841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 78533841545SHajimu UMEMOTO * for the exchange. 78633841545SHajimu UMEMOTO * 78733841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 78833841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 78933841545SHajimu UMEMOTO * SYN in this case. 79033841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 79133841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 79233841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 79333841545SHajimu UMEMOTO * used" 79433841545SHajimu UMEMOTO * 2. use of it with new communication: 79533841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 79633841545SHajimu UMEMOTO * with sufficient scope is available" 79733841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 79833841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 79933841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 80033841545SHajimu UMEMOTO * 80133841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 80233841545SHajimu UMEMOTO * multiple description text for deprecated address 80333841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 80433841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 80533841545SHajimu UMEMOTO */ 80633841545SHajimu UMEMOTO if (isipv6 && !ip6_use_deprecated) { 80733841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 80833841545SHajimu UMEMOTO 80933841545SHajimu UMEMOTO if ((ia6 = ip6_getdstifaddr(m)) && 81033841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 811f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 81233841545SHajimu UMEMOTO tp = NULL; 81333841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 81433841545SHajimu UMEMOTO goto dropwithreset; 81533841545SHajimu UMEMOTO } 81633841545SHajimu UMEMOTO } 81733841545SHajimu UMEMOTO #endif 81865f28919SJesper Skriver /* 819be2ac88cSJonathan Lemon * If it is from this socket, drop it, it must be forged. 820be2ac88cSJonathan Lemon * Don't bother responding if the destination was a broadcast. 82165f28919SJesper Skriver */ 822be2ac88cSJonathan Lemon if (th->th_dport == th->th_sport) { 823fb59c426SYoshinobu Inoue if (isipv6) { 824be2ac88cSJonathan Lemon if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 825be2ac88cSJonathan Lemon &ip6->ip6_src)) 826be2ac88cSJonathan Lemon goto drop; 827c068736aSJeffrey Hsu } else { 828be2ac88cSJonathan Lemon if (ip->ip_dst.s_addr == ip->ip_src.s_addr) 829be2ac88cSJonathan Lemon goto drop; 830be2ac88cSJonathan Lemon } 831c068736aSJeffrey Hsu } 832be2ac88cSJonathan Lemon /* 833be2ac88cSJonathan Lemon * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 8342ca2159fSCrist J. Clark * 83593ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 83693ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 83793ec91baSCrist J. Clark * in_broadcast() to find them. 838be2ac88cSJonathan Lemon */ 839be2ac88cSJonathan Lemon if (m->m_flags & (M_BCAST|M_MCAST)) 840be2ac88cSJonathan Lemon goto drop; 841be2ac88cSJonathan Lemon if (isipv6) { 842be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 843be2ac88cSJonathan Lemon IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 844be2ac88cSJonathan Lemon goto drop; 845c068736aSJeffrey Hsu } else { 846be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 847be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 8482ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 8492ca2159fSCrist J. Clark in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 850be2ac88cSJonathan Lemon goto drop; 851c068736aSJeffrey Hsu } 852be2ac88cSJonathan Lemon /* 853be2ac88cSJonathan Lemon * SYN appears to be valid; create compressed TCP state 854be2ac88cSJonathan Lemon * for syncache, or perform t/tcp connection. 855be2ac88cSJonathan Lemon */ 856be2ac88cSJonathan Lemon if (so->so_qlen <= so->so_qlimit) { 857be2ac88cSJonathan Lemon tcp_dooptions(&to, optp, optlen, 1); 858be2ac88cSJonathan Lemon if (!syncache_add(&inc, &to, th, &so, m)) 859be2ac88cSJonathan Lemon goto drop; 860f76fcf6dSJeffrey Hsu if (so == NULL) { 861be2ac88cSJonathan Lemon /* 862be2ac88cSJonathan Lemon * Entry added to syncache, mbuf used to 863be2ac88cSJonathan Lemon * send SYN,ACK packet. 864be2ac88cSJonathan Lemon */ 865f76fcf6dSJeffrey Hsu KASSERT(headlocked, ("headlocked")); 866f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 867f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 868be2ac88cSJonathan Lemon return; 869f76fcf6dSJeffrey Hsu } 870be2ac88cSJonathan Lemon /* 871be2ac88cSJonathan Lemon * Segment passed TAO tests. 872be2ac88cSJonathan Lemon */ 873f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 874be2ac88cSJonathan Lemon inp = sotoinpcb(so); 875f76fcf6dSJeffrey Hsu INP_LOCK(inp); 876df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 877be2ac88cSJonathan Lemon tp->snd_wnd = tiwin; 878be2ac88cSJonathan Lemon tp->t_starttime = ticks; 879be2ac88cSJonathan Lemon tp->t_state = TCPS_ESTABLISHED; 880df8bae1dSRodney W. Grimes 881be2ac88cSJonathan Lemon /* 8823bfd6421SJonathan Lemon * T/TCP logic: 8833bfd6421SJonathan Lemon * If there is a FIN or if there is data, then 8843bfd6421SJonathan Lemon * delay SYN,ACK(SYN) in the hope of piggy-backing 8853bfd6421SJonathan Lemon * it on a response segment. Otherwise must send 8863bfd6421SJonathan Lemon * ACK now in case the other side is slow starting. 887be2ac88cSJonathan Lemon */ 8883bfd6421SJonathan Lemon if (thflags & TH_FIN || tlen != 0) 8893bfd6421SJonathan Lemon tp->t_flags |= (TF_DELACK | TF_NEEDSYN); 8903bfd6421SJonathan Lemon else 891be2ac88cSJonathan Lemon tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 892be2ac88cSJonathan Lemon tcpstat.tcps_connects++; 893be2ac88cSJonathan Lemon soisconnected(so); 894be2ac88cSJonathan Lemon goto trimthenstep6; 895df8bae1dSRodney W. Grimes } 896be2ac88cSJonathan Lemon goto drop; 8974cc20ab1SSeigo Tanimura } 898be2ac88cSJonathan Lemon after_listen: 899be2ac88cSJonathan Lemon 900be2ac88cSJonathan Lemon /* XXX temp debugging */ 901be2ac88cSJonathan Lemon /* should not happen - syncache should pick up these connections */ 902be2ac88cSJonathan Lemon if (tp->t_state == TCPS_LISTEN) 903be2ac88cSJonathan Lemon panic("tcp_input: TCPS_LISTEN"); 904df8bae1dSRodney W. Grimes 905df8bae1dSRodney W. Grimes /* 906df8bae1dSRodney W. Grimes * Segment received on connection. 907df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 908df8bae1dSRodney W. Grimes */ 9099b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 9107ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 9119b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp); 912df8bae1dSRodney W. Grimes 913df8bae1dSRodney W. Grimes /* 914be2ac88cSJonathan Lemon * Process options. 915be2ac88cSJonathan Lemon * XXX this is tradtitional behavior, may need to be cleaned up. 916df8bae1dSRodney W. Grimes */ 917be2ac88cSJonathan Lemon tcp_dooptions(&to, optp, optlen, thflags & TH_SYN); 918be2ac88cSJonathan Lemon if (thflags & TH_SYN) { 919be2ac88cSJonathan Lemon if (to.to_flags & TOF_SCALE) { 920be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 921be2ac88cSJonathan Lemon tp->requested_s_scale = to.to_requested_s_scale; 922be2ac88cSJonathan Lemon } 923be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 924be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 925be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 926be2ac88cSJonathan Lemon tp->ts_recent_age = ticks; 927be2ac88cSJonathan Lemon } 928be2ac88cSJonathan Lemon if (to.to_flags & (TOF_CC|TOF_CCNEW)) 929be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_CC; 930be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 931be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 932be2ac88cSJonathan Lemon } 933df8bae1dSRodney W. Grimes 934df8bae1dSRodney W. Grimes /* 935df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 936df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 937df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 938df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 939df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 940df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 941df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 942df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 943df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 944df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 945df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 946df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 947a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 948a0292f23SGarrett Wollman * Since we check for TCPS_ESTABLISHED above, it can only 949a0292f23SGarrett Wollman * be TH_NEEDSYN. 950df8bae1dSRodney W. Grimes */ 951df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 952fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 953a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 954be2ac88cSJonathan Lemon ((to.to_flags & TOF_TS) == 0 || 955a0292f23SGarrett Wollman TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 956a0292f23SGarrett Wollman /* 957a0292f23SGarrett Wollman * Using the CC option is compulsory if once started: 958a0292f23SGarrett Wollman * the segment is OK if no T/TCP was negotiated or 959a0292f23SGarrett Wollman * if the segment has a CC option equal to CCrecv 960a0292f23SGarrett Wollman */ 961a0292f23SGarrett Wollman ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) || 962be2ac88cSJonathan Lemon ((to.to_flags & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) && 963fb59c426SYoshinobu Inoue th->th_seq == tp->rcv_nxt && 964df8bae1dSRodney W. Grimes tiwin && tiwin == tp->snd_wnd && 965df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) { 966df8bae1dSRodney W. Grimes 967df8bae1dSRodney W. Grimes /* 968df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 969df8bae1dSRodney W. Grimes * record the timestamp. 970a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 971a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 972df8bae1dSRodney W. Grimes */ 973be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 974fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 9759b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 976a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 977df8bae1dSRodney W. Grimes } 978df8bae1dSRodney W. Grimes 979fb59c426SYoshinobu Inoue if (tlen == 0) { 980fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 981fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 982233e8c18SGarrett Wollman tp->snd_cwnd >= tp->snd_wnd && 983cb942153SJeffrey Hsu ((!tcp_do_newreno && 984cb942153SJeffrey Hsu tp->t_dupacks < tcprexmtthresh) || 985cb942153SJeffrey Hsu (tcp_do_newreno && 986cb942153SJeffrey Hsu !SEQ_LT(tp->snd_una, tp->snd_recover)))) { 987f76fcf6dSJeffrey Hsu KASSERT(headlocked, ("headlocked")); 988f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 989f76fcf6dSJeffrey Hsu headlocked = 0; 990df8bae1dSRodney W. Grimes /* 991df8bae1dSRodney W. Grimes * this is a pure ack for outstanding data. 992df8bae1dSRodney W. Grimes */ 993df8bae1dSRodney W. Grimes ++tcpstat.tcps_predack; 9949b8b58e0SJonathan Lemon /* 9959b8b58e0SJonathan Lemon * "bad retransmit" recovery 9969b8b58e0SJonathan Lemon */ 9979b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 9989b8b58e0SJonathan Lemon ticks < tp->t_badrxtwin) { 999cb942153SJeffrey Hsu ++tcpstat.tcps_sndrexmitbad; 10009b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 10019b8b58e0SJonathan Lemon tp->snd_ssthresh = 10029b8b58e0SJonathan Lemon tp->snd_ssthresh_prev; 1003cb942153SJeffrey Hsu tp->snd_high = tp->snd_high_prev; 10049b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 10059b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; 10069b8b58e0SJonathan Lemon } 1007fa55172bSMatthew Dillon 1008fa55172bSMatthew Dillon /* 1009fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1010fa55172bSMatthew Dillon * 1011fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1012fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1013fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1014fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1015fa55172bSMatthew Dillon */ 1016fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1017fa55172bSMatthew Dillon to.to_tsecr) { 1018a0292f23SGarrett Wollman tcp_xmit_timer(tp, 10199b8b58e0SJonathan Lemon ticks - to.to_tsecr + 1); 1020fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1021fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1022c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1023c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1024fa55172bSMatthew Dillon } 10251fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 1026fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 1027df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 1028df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 1029df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 1030cb942153SJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_high) && 1031cb942153SJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_high)) 1032cb942153SJeffrey Hsu tp->snd_high = th->th_ack - 1; 1033cb942153SJeffrey Hsu tp->snd_una = tp->snd_recover = th->th_ack; 10341645d090SJeff Roberson /* 10351645d090SJeff Roberson * pull snd_wl2 up to prevent seq wrap relative 10361645d090SJeff Roberson * to th_ack. 10371645d090SJeff Roberson */ 1038cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1039b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1040df8bae1dSRodney W. Grimes m_freem(m); 1041fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 1042df8bae1dSRodney W. Grimes 1043df8bae1dSRodney W. Grimes /* 1044df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1045df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1046df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1047df8bae1dSRodney W. Grimes * If process is waiting for space, 1048df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1049df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1050df8bae1dSRodney W. Grimes * decide between more output or persist. 1051df8bae1dSRodney W. Grimes */ 1052df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 10539b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 10549b8b58e0SJonathan Lemon else if (!callout_active(tp->tt_persist)) 10559b8b58e0SJonathan Lemon callout_reset(tp->tt_rexmt, 10569b8b58e0SJonathan Lemon tp->t_rxtcur, 10579b8b58e0SJonathan Lemon tcp_timer_rexmt, tp); 1058df8bae1dSRodney W. Grimes 1059df8bae1dSRodney W. Grimes sowwakeup(so); 1060df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 1061df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1062f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 1063df8bae1dSRodney W. Grimes return; 1064df8bae1dSRodney W. Grimes } 1065fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1066fb59c426SYoshinobu Inoue LIST_EMPTY(&tp->t_segq) && 1067fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 1068f76fcf6dSJeffrey Hsu KASSERT(headlocked, ("headlocked")); 1069f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 1070f76fcf6dSJeffrey Hsu headlocked = 0; 1071df8bae1dSRodney W. Grimes /* 1072df8bae1dSRodney W. Grimes * this is a pure, in-sequence data packet 1073df8bae1dSRodney W. Grimes * with nothing on the reassembly queue and 1074df8bae1dSRodney W. Grimes * we have enough buffer space to take it. 1075df8bae1dSRodney W. Grimes */ 1076df8bae1dSRodney W. Grimes ++tcpstat.tcps_preddat; 1077fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 10781645d090SJeff Roberson /* 10791645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 10801645d090SJeff Roberson * th_seq. 10811645d090SJeff Roberson */ 10821645d090SJeff Roberson tp->snd_wl1 = th->th_seq; 10831645d090SJeff Roberson /* 10841645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 10851645d090SJeff Roberson * rcv_nxt. 10861645d090SJeff Roberson */ 10871645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 1088df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpack++; 1089fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyte += tlen; 1090fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 1091df8bae1dSRodney W. Grimes /* 1092755c1f07SAndras Olah * Add data to socket buffer. 1093df8bae1dSRodney W. Grimes */ 1094c1c36a2cSMike Silbersack if (so->so_state & SS_CANTRCVMORE) { 1095c1c36a2cSMike Silbersack m_freem(m); 1096c1c36a2cSMike Silbersack } else { 1097fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 1098df8bae1dSRodney W. Grimes sbappend(&so->so_rcv, m); 1099c1c36a2cSMike Silbersack } 1100df8bae1dSRodney W. Grimes sorwakeup(so); 1101d8c85a26SJonathan Lemon if (DELAY_ACK(tp)) { 11023bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1103f498eeeeSDavid Greenman } else { 1104e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 1105e612a582SDavid Greenman tcp_output(tp); 1106e612a582SDavid Greenman } 1107f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 1108df8bae1dSRodney W. Grimes return; 1109df8bae1dSRodney W. Grimes } 1110df8bae1dSRodney W. Grimes } 1111df8bae1dSRodney W. Grimes 1112df8bae1dSRodney W. Grimes /* 1113df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1114df8bae1dSRodney W. Grimes * and then do TCP input processing. 1115df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1116df8bae1dSRodney W. Grimes * but not less than advertised window. 1117df8bae1dSRodney W. Grimes */ 1118df8bae1dSRodney W. Grimes { int win; 1119df8bae1dSRodney W. Grimes 1120df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1121df8bae1dSRodney W. Grimes if (win < 0) 1122df8bae1dSRodney W. Grimes win = 0; 112366e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1124df8bae1dSRodney W. Grimes } 1125df8bae1dSRodney W. Grimes 1126df8bae1dSRodney W. Grimes switch (tp->t_state) { 1127df8bae1dSRodney W. Grimes 1128df8bae1dSRodney W. Grimes /* 1129764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1130764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1131764d8cefSBill Fenner */ 1132764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1133fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1134fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1135a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1136a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1137a57815efSBosko Milekic goto dropwithreset; 1138a57815efSBosko Milekic } 1139764d8cefSBill Fenner break; 1140764d8cefSBill Fenner 1141764d8cefSBill Fenner /* 1142df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1143df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1144df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1145df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1146df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1147df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1148df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1149df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1150df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1151df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1152df8bae1dSRodney W. Grimes */ 1153df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1154be2ac88cSJonathan Lemon if ((taop = tcp_gettaocache(&inp->inp_inc)) == NULL) { 1155a0292f23SGarrett Wollman taop = &tao_noncached; 1156a0292f23SGarrett Wollman bzero(taop, sizeof(*taop)); 1157a0292f23SGarrett Wollman } 1158a0292f23SGarrett Wollman 1159fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1160fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1161fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1162a0292f23SGarrett Wollman /* 1163a0292f23SGarrett Wollman * If we have a cached CCsent for the remote host, 1164a0292f23SGarrett Wollman * hence we haven't just crashed and restarted, 1165a0292f23SGarrett Wollman * do not send a RST. This may be a retransmission 1166a0292f23SGarrett Wollman * from the other side after our earlier ACK was lost. 1167a0292f23SGarrett Wollman * Our new SYN, when it arrives, will serve as the 1168a0292f23SGarrett Wollman * needed ACK. 1169a0292f23SGarrett Wollman */ 1170a0292f23SGarrett Wollman if (taop->tao_ccsent != 0) 1171a0292f23SGarrett Wollman goto drop; 1172a57815efSBosko Milekic else { 1173a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1174a0292f23SGarrett Wollman goto dropwithreset; 1175a0292f23SGarrett Wollman } 1176a57815efSBosko Milekic } 1177fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 1178fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1179df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 1180df8bae1dSRodney W. Grimes goto drop; 1181df8bae1dSRodney W. Grimes } 1182fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) == 0) 1183df8bae1dSRodney W. Grimes goto drop; 1184fb59c426SYoshinobu Inoue tp->snd_wnd = th->th_win; /* initial send window */ 1185a0292f23SGarrett Wollman tp->cc_recv = to.to_cc; /* foreign CC */ 1186a0292f23SGarrett Wollman 1187fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1188df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1189fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1190a0292f23SGarrett Wollman /* 1191a0292f23SGarrett Wollman * Our SYN was acked. If segment contains CC.ECHO 1192a0292f23SGarrett Wollman * option, check it to make sure this segment really 1193a0292f23SGarrett Wollman * matches our SYN. If not, just drop it as old 1194a0292f23SGarrett Wollman * duplicate, but send an RST if we're still playing 1195026650e5SBill Fenner * by the old rules. If no CC.ECHO option, make sure 1196026650e5SBill Fenner * we don't get fooled into using T/TCP. 1197a0292f23SGarrett Wollman */ 1198be2ac88cSJonathan Lemon if (to.to_flags & TOF_CCECHO) { 1199dfd5dee1SPeter Wemm if (tp->cc_send != to.to_ccecho) { 1200a0292f23SGarrett Wollman if (taop->tao_ccsent != 0) 1201a0292f23SGarrett Wollman goto drop; 1202a57815efSBosko Milekic else { 1203a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1204a0292f23SGarrett Wollman goto dropwithreset; 1205dfd5dee1SPeter Wemm } 1206a57815efSBosko Milekic } 1207026650e5SBill Fenner } else 1208026650e5SBill Fenner tp->t_flags &= ~TF_RCVD_CC; 1209845799c1SAndras Olah tcpstat.tcps_connects++; 1210845799c1SAndras Olah soisconnected(so); 1211c488362eSRobert Watson #ifdef MAC 1212c488362eSRobert Watson mac_set_socket_peer_from_mbuf(m, so); 1213c488362eSRobert Watson #endif 1214845799c1SAndras Olah /* Do window scaling on this connection? */ 1215845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1216845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1217845799c1SAndras Olah tp->snd_scale = tp->requested_s_scale; 1218845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1219845799c1SAndras Olah } 1220a0292f23SGarrett Wollman /* Segment is acceptable, update cache if undefined. */ 1221a0292f23SGarrett Wollman if (taop->tao_ccsent == 0) 1222a0292f23SGarrett Wollman taop->tao_ccsent = to.to_ccecho; 1223a0292f23SGarrett Wollman 1224a0292f23SGarrett Wollman tp->rcv_adv += tp->rcv_wnd; 1225a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1226a0292f23SGarrett Wollman /* 1227a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1228a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1229a0292f23SGarrett Wollman */ 1230d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && tlen != 0) 12319b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 12329b8b58e0SJonathan Lemon tcp_timer_delack, tp); 1233a0292f23SGarrett Wollman else 1234a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1235a0292f23SGarrett Wollman /* 1236a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1237a0292f23SGarrett Wollman * Transitions: 1238a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1239a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1240a0292f23SGarrett Wollman */ 12419b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1242a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1243a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1244a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1245fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 12467ff19458SPaul Traina } else { 1247a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 12489b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, 12499b8b58e0SJonathan Lemon tcp_timer_keep, tp); 12507ff19458SPaul Traina } 1251a0292f23SGarrett Wollman } else { 1252a0292f23SGarrett Wollman /* 1253c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 1254c068736aSJeffrey Hsu * simultaneous open. If segment contains CC option 1255c068736aSJeffrey Hsu * and there is a cached CC, apply TAO test. 1256c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 1257c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 1258a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1259a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1260a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1261a0292f23SGarrett Wollman */ 1262a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 12639b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 1264be2ac88cSJonathan Lemon if (to.to_flags & TOF_CC) { 1265a0292f23SGarrett Wollman if (taop->tao_cc != 0 && 1266a0292f23SGarrett Wollman CC_GT(to.to_cc, taop->tao_cc)) { 1267a0292f23SGarrett Wollman /* 1268a0292f23SGarrett Wollman * update cache and make transition: 1269a0292f23SGarrett Wollman * SYN-SENT -> ESTABLISHED* 1270a0292f23SGarrett Wollman * SYN-SENT* -> FIN-WAIT-1* 1271a0292f23SGarrett Wollman */ 1272a0292f23SGarrett Wollman taop->tao_cc = to.to_cc; 12739b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1274a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1275a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1276a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 12777ff19458SPaul Traina } else { 1278a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 12799b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, 12809b8b58e0SJonathan Lemon tcp_keepidle, 12819b8b58e0SJonathan Lemon tcp_timer_keep, 12829b8b58e0SJonathan Lemon tp); 12837ff19458SPaul Traina } 1284a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDSYN; 1285df8bae1dSRodney W. Grimes } else 1286df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1287a0292f23SGarrett Wollman } else { 1288a0292f23SGarrett Wollman /* CC.NEW or no option => invalidate cache */ 1289a0292f23SGarrett Wollman taop->tao_cc = 0; 1290a0292f23SGarrett Wollman tp->t_state = TCPS_SYN_RECEIVED; 1291a0292f23SGarrett Wollman } 1292a0292f23SGarrett Wollman } 1293df8bae1dSRodney W. Grimes 1294df8bae1dSRodney W. Grimes trimthenstep6: 1295df8bae1dSRodney W. Grimes /* 1296fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1297df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1298df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1299df8bae1dSRodney W. Grimes */ 1300fb59c426SYoshinobu Inoue th->th_seq++; 1301fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1302fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1303df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1304fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1305fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1306df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1307df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1308df8bae1dSRodney W. Grimes } 1309fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1310fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 1311a0292f23SGarrett Wollman /* 1312a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1313a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1314a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1315a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1316a0292f23SGarrett Wollman * Otherwise, goto step 6. 1317a0292f23SGarrett Wollman */ 1318fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1319a0292f23SGarrett Wollman goto process_ACK; 1320c068736aSJeffrey Hsu 1321df8bae1dSRodney W. Grimes goto step6; 1322c068736aSJeffrey Hsu 1323a0292f23SGarrett Wollman /* 1324a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1325a0292f23SGarrett Wollman * if segment contains a SYN and CC [not CC.NEW] option: 1326a0292f23SGarrett Wollman * if state == TIME_WAIT and connection duration > MSL, 1327a0292f23SGarrett Wollman * drop packet and send RST; 1328a0292f23SGarrett Wollman * 1329a0292f23SGarrett Wollman * if SEG.CC > CCrecv then is new SYN, and can implicitly 1330a0292f23SGarrett Wollman * ack the FIN (and data) in retransmission queue. 1331a0292f23SGarrett Wollman * Complete close and delete TCPCB. Then reprocess 1332a0292f23SGarrett Wollman * segment, hoping to find new TCPCB in LISTEN state; 1333a0292f23SGarrett Wollman * 1334a0292f23SGarrett Wollman * else must be old SYN; drop it. 1335a0292f23SGarrett Wollman * else do normal processing. 1336a0292f23SGarrett Wollman */ 1337a0292f23SGarrett Wollman case TCPS_LAST_ACK: 1338a0292f23SGarrett Wollman case TCPS_CLOSING: 1339a0292f23SGarrett Wollman case TCPS_TIME_WAIT: 1340340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1341fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) && 1342be2ac88cSJonathan Lemon (to.to_flags & TOF_CC) && tp->cc_recv != 0) { 1343a0292f23SGarrett Wollman if (tp->t_state == TCPS_TIME_WAIT && 1344a57815efSBosko Milekic (ticks - tp->t_starttime) > tcp_msl) { 1345a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1346a0292f23SGarrett Wollman goto dropwithreset; 1347a57815efSBosko Milekic } 1348a0292f23SGarrett Wollman if (CC_GT(to.to_cc, tp->cc_recv)) { 1349a0292f23SGarrett Wollman tp = tcp_close(tp); 1350a0292f23SGarrett Wollman goto findpcb; 1351a0292f23SGarrett Wollman } 1352a0292f23SGarrett Wollman else 1353a0292f23SGarrett Wollman goto drop; 1354a0292f23SGarrett Wollman } 1355a0292f23SGarrett Wollman break; /* continue normal processing */ 1356df8bae1dSRodney W. Grimes } 1357df8bae1dSRodney W. Grimes 1358df8bae1dSRodney W. Grimes /* 1359df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 136080ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 136180ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 136280ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 136380ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 136480ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 136580ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 136680ab7c0eSGarrett Wollman * killing a connection). 136780ab7c0eSGarrett Wollman * Then check timestamp, if present. 1368a0292f23SGarrett Wollman * Then check the connection count, if present. 1369df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 1370df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 1371df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 1372df8bae1dSRodney W. Grimes * 137380ab7c0eSGarrett Wollman * 137480ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 137580ab7c0eSGarrett Wollman * if this is a valid reset segment. 137680ab7c0eSGarrett Wollman * RFC 793 page 37: 137780ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 137880ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 137980ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 138080ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 138180ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 13821a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 13831a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 13841a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 13851a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 138680ab7c0eSGarrett Wollman * If we have multiple segments in flight, the intial reset 138780ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 138880ab7c0eSGarrett Wollman * but they will eventually catch up. 138980ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 139080ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 139180ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 139280ab7c0eSGarrett Wollman * 139380ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 139480ab7c0eSGarrett Wollman * 139580ab7c0eSGarrett Wollman * DISCUSSION 139680ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 139780ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 139880ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 139980ab7c0eSGarrett Wollman * data. 140080ab7c0eSGarrett Wollman * 140180ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 140280ab7c0eSGarrett Wollman * the state: 140380ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 140480ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 140580ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 1406745bab7fSDima Dorfman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 140780ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 1408e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 140980ab7c0eSGarrett Wollman * Close the tcb. 1410e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 141180ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 141280ab7c0eSGarrett Wollman * RFC 1337. 141380ab7c0eSGarrett Wollman */ 1414fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 1415fb59c426SYoshinobu Inoue if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 1416fb59c426SYoshinobu Inoue SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 141780ab7c0eSGarrett Wollman switch (tp->t_state) { 141880ab7c0eSGarrett Wollman 141980ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 142080ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 142180ab7c0eSGarrett Wollman goto close; 142280ab7c0eSGarrett Wollman 142380ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 142480ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 142580ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 142680ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 142780ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 142880ab7c0eSGarrett Wollman close: 142980ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 143080ab7c0eSGarrett Wollman tcpstat.tcps_drops++; 143180ab7c0eSGarrett Wollman tp = tcp_close(tp); 143280ab7c0eSGarrett Wollman break; 143380ab7c0eSGarrett Wollman 143480ab7c0eSGarrett Wollman case TCPS_CLOSING: 143580ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 143680ab7c0eSGarrett Wollman tp = tcp_close(tp); 143780ab7c0eSGarrett Wollman break; 143880ab7c0eSGarrett Wollman 143980ab7c0eSGarrett Wollman case TCPS_TIME_WAIT: 1440340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, 1441340c35deSJonathan Lemon ("timewait")); 144280ab7c0eSGarrett Wollman break; 144380ab7c0eSGarrett Wollman } 144480ab7c0eSGarrett Wollman } 144580ab7c0eSGarrett Wollman goto drop; 144680ab7c0eSGarrett Wollman } 144780ab7c0eSGarrett Wollman 144880ab7c0eSGarrett Wollman /* 1449df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 1450df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 1451df8bae1dSRodney W. Grimes */ 1452be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 145380ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1454df8bae1dSRodney W. Grimes 1455df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 14569b8b58e0SJonathan Lemon if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1457df8bae1dSRodney W. Grimes /* 1458df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 1459df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 1460df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 1461df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 1462df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 1463df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 1464df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 1465df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 1466df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 1467df8bae1dSRodney W. Grimes */ 1468df8bae1dSRodney W. Grimes tp->ts_recent = 0; 1469df8bae1dSRodney W. Grimes } else { 1470df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 1471fb59c426SYoshinobu Inoue tcpstat.tcps_rcvdupbyte += tlen; 1472df8bae1dSRodney W. Grimes tcpstat.tcps_pawsdrop++; 147307fd333dSMatthew Dillon if (tlen) 1474df8bae1dSRodney W. Grimes goto dropafterack; 14751ab4789dSMatthew Dillon goto drop; 14761ab4789dSMatthew Dillon } 1477df8bae1dSRodney W. Grimes } 1478df8bae1dSRodney W. Grimes 1479a0292f23SGarrett Wollman /* 1480a0292f23SGarrett Wollman * T/TCP mechanism 1481a0292f23SGarrett Wollman * If T/TCP was negotiated and the segment doesn't have CC, 1482dc733423SDag-Erling Smørgrav * or if its CC is wrong then drop the segment. 1483a0292f23SGarrett Wollman * RST segments do not have to comply with this. 1484a0292f23SGarrett Wollman */ 1485a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) && 1486be2ac88cSJonathan Lemon ((to.to_flags & TOF_CC) == 0 || tp->cc_recv != to.to_cc)) 1487a0292f23SGarrett Wollman goto dropafterack; 1488a0292f23SGarrett Wollman 148980ab7c0eSGarrett Wollman /* 149080ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 149180ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 149280ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 149380ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 149480ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 149580ab7c0eSGarrett Wollman */ 1496a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 1497a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1498a57815efSBosko Milekic goto dropwithreset; 1499a57815efSBosko Milekic } 150080ab7c0eSGarrett Wollman 1501fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 1502df8bae1dSRodney W. Grimes if (todrop > 0) { 1503fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1504fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 1505fb59c426SYoshinobu Inoue th->th_seq++; 1506fb59c426SYoshinobu Inoue if (th->th_urp > 1) 1507fb59c426SYoshinobu Inoue th->th_urp--; 1508df8bae1dSRodney W. Grimes else 1509fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1510df8bae1dSRodney W. Grimes todrop--; 1511df8bae1dSRodney W. Grimes } 1512df8bae1dSRodney W. Grimes /* 1513dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 1514df8bae1dSRodney W. Grimes */ 1515fb59c426SYoshinobu Inoue if (todrop > tlen 1516fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1517dac20301SGarrett Wollman /* 1518dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 1519dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 1520dac20301SGarrett Wollman * of sequence; drop it. 1521dac20301SGarrett Wollman */ 1522fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1523dac20301SGarrett Wollman 1524df8bae1dSRodney W. Grimes /* 1525dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 1526dac20301SGarrett Wollman * But keep on processing for RST or ACK. 1527df8bae1dSRodney W. Grimes */ 1528dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1529fb59c426SYoshinobu Inoue todrop = tlen; 1530dac20301SGarrett Wollman tcpstat.tcps_rcvduppack++; 1531dac20301SGarrett Wollman tcpstat.tcps_rcvdupbyte += todrop; 1532df8bae1dSRodney W. Grimes } else { 1533df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartduppack++; 1534df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartdupbyte += todrop; 1535df8bae1dSRodney W. Grimes } 1536fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 1537fb59c426SYoshinobu Inoue th->th_seq += todrop; 1538fb59c426SYoshinobu Inoue tlen -= todrop; 1539fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 1540fb59c426SYoshinobu Inoue th->th_urp -= todrop; 1541df8bae1dSRodney W. Grimes else { 1542fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1543fb59c426SYoshinobu Inoue th->th_urp = 0; 1544df8bae1dSRodney W. Grimes } 1545df8bae1dSRodney W. Grimes } 1546df8bae1dSRodney W. Grimes 1547df8bae1dSRodney W. Grimes /* 1548df8bae1dSRodney W. Grimes * If new data are received on a connection after the 1549df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 1550df8bae1dSRodney W. Grimes */ 1551df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 1552fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1553df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1554df8bae1dSRodney W. Grimes tcpstat.tcps_rcvafterclose++; 1555a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1556df8bae1dSRodney W. Grimes goto dropwithreset; 1557df8bae1dSRodney W. Grimes } 1558df8bae1dSRodney W. Grimes 1559df8bae1dSRodney W. Grimes /* 1560df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 1561df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 1562df8bae1dSRodney W. Grimes */ 1563fb59c426SYoshinobu Inoue todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd); 1564df8bae1dSRodney W. Grimes if (todrop > 0) { 1565df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1566fb59c426SYoshinobu Inoue if (todrop >= tlen) { 1567fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyteafterwin += tlen; 1568df8bae1dSRodney W. Grimes /* 1569df8bae1dSRodney W. Grimes * If a new connection request is received 1570df8bae1dSRodney W. Grimes * while in TIME_WAIT, drop the old connection 1571df8bae1dSRodney W. Grimes * and start over if the sequence numbers 1572df8bae1dSRodney W. Grimes * are above the previous ones. 1573df8bae1dSRodney W. Grimes */ 1574340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1575fb59c426SYoshinobu Inoue if (thflags & TH_SYN && 1576df8bae1dSRodney W. Grimes tp->t_state == TCPS_TIME_WAIT && 1577fb59c426SYoshinobu Inoue SEQ_GT(th->th_seq, tp->rcv_nxt)) { 1578df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1579df8bae1dSRodney W. Grimes goto findpcb; 1580df8bae1dSRodney W. Grimes } 1581df8bae1dSRodney W. Grimes /* 1582df8bae1dSRodney W. Grimes * If window is closed can only take segments at 1583df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 1584df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 1585df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 1586df8bae1dSRodney W. Grimes * and ack. 1587df8bae1dSRodney W. Grimes */ 1588fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1589df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1590df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinprobe++; 1591df8bae1dSRodney W. Grimes } else 1592df8bae1dSRodney W. Grimes goto dropafterack; 1593df8bae1dSRodney W. Grimes } else 1594df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1595df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1596fb59c426SYoshinobu Inoue tlen -= todrop; 1597fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 1598df8bae1dSRodney W. Grimes } 1599df8bae1dSRodney W. Grimes 1600df8bae1dSRodney W. Grimes /* 1601df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1602df8bae1dSRodney W. Grimes * record its timestamp. 1603a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1604a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1605df8bae1dSRodney W. Grimes */ 1606be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1607fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 16089b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1609a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1610df8bae1dSRodney W. Grimes } 1611df8bae1dSRodney W. Grimes 1612df8bae1dSRodney W. Grimes /* 1613df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 1614df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 1615df8bae1dSRodney W. Grimes */ 1616fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1617df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 1618a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1619df8bae1dSRodney W. Grimes goto dropwithreset; 1620df8bae1dSRodney W. Grimes } 1621df8bae1dSRodney W. Grimes 1622a0292f23SGarrett Wollman /* 1623a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1624a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 1625a0292f23SGarrett Wollman * later processing; else drop segment and return. 1626a0292f23SGarrett Wollman */ 1627fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 1628a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 1629a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 1630a0292f23SGarrett Wollman goto step6; 1631a0292f23SGarrett Wollman else 1632a0292f23SGarrett Wollman goto drop; 1633a0292f23SGarrett Wollman } 1634df8bae1dSRodney W. Grimes 1635df8bae1dSRodney W. Grimes /* 1636df8bae1dSRodney W. Grimes * Ack processing. 1637df8bae1dSRodney W. Grimes */ 1638df8bae1dSRodney W. Grimes switch (tp->t_state) { 1639df8bae1dSRodney W. Grimes 1640df8bae1dSRodney W. Grimes /* 1641764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1642764d8cefSBill Fenner * ESTABLISHED state and continue processing. 1643764d8cefSBill Fenner * The ACK was checked above. 1644df8bae1dSRodney W. Grimes */ 1645df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1646a0292f23SGarrett Wollman 1647df8bae1dSRodney W. Grimes tcpstat.tcps_connects++; 1648df8bae1dSRodney W. Grimes soisconnected(so); 1649df8bae1dSRodney W. Grimes /* Do window scaling? */ 1650df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1651df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1652df8bae1dSRodney W. Grimes tp->snd_scale = tp->requested_s_scale; 1653df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 1654df8bae1dSRodney W. Grimes } 1655a0292f23SGarrett Wollman /* 1656a0292f23SGarrett Wollman * Upon successful completion of 3-way handshake, 1657a0292f23SGarrett Wollman * update cache.CC if it was undefined, pass any queued 1658a0292f23SGarrett Wollman * data to the user, and advance state appropriately. 1659a0292f23SGarrett Wollman */ 1660be2ac88cSJonathan Lemon if ((taop = tcp_gettaocache(&inp->inp_inc)) != NULL && 1661a0292f23SGarrett Wollman taop->tao_cc == 0) 1662a0292f23SGarrett Wollman taop->tao_cc = tp->cc_recv; 1663a0292f23SGarrett Wollman 1664a0292f23SGarrett Wollman /* 1665a0292f23SGarrett Wollman * Make transitions: 1666a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 1667a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 1668a0292f23SGarrett Wollman */ 16699b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1670a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1671a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1672a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 16737ff19458SPaul Traina } else { 1674a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 16759b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, 16769b8b58e0SJonathan Lemon tcp_timer_keep, tp); 16777ff19458SPaul Traina } 1678a0292f23SGarrett Wollman /* 1679a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 1680a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 1681a0292f23SGarrett Wollman */ 1682fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 1683fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1684a0292f23SGarrett Wollman (struct mbuf *)0); 1685fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 168693b0017fSPhilippe Charnier /* FALLTHROUGH */ 1687df8bae1dSRodney W. Grimes 1688df8bae1dSRodney W. Grimes /* 1689df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1690df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 1691fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 1692fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 1693df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 1694df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 1695df8bae1dSRodney W. Grimes */ 1696df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1697df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1698df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 1699df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1700df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1701df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 1702df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 1703340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1704fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1705fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 1706df8bae1dSRodney W. Grimes tcpstat.tcps_rcvdupack++; 1707df8bae1dSRodney W. Grimes /* 1708df8bae1dSRodney W. Grimes * If we have outstanding data (other than 1709df8bae1dSRodney W. Grimes * a window probe), this is a completely 1710df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 1711df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 1712df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 1713df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 1714df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 1715df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 1716df8bae1dSRodney W. Grimes * window so we send only this one 1717df8bae1dSRodney W. Grimes * packet. 1718df8bae1dSRodney W. Grimes * 1719df8bae1dSRodney W. Grimes * We know we're losing at the current 1720df8bae1dSRodney W. Grimes * window size so do congestion avoidance 1721df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 1722df8bae1dSRodney W. Grimes * and pull our congestion window back to 1723df8bae1dSRodney W. Grimes * the new ssthresh). 1724df8bae1dSRodney W. Grimes * 1725df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 1726df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 1727df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 1728df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 1729df8bae1dSRodney W. Grimes * network. 1730df8bae1dSRodney W. Grimes */ 17319b8b58e0SJonathan Lemon if (!callout_active(tp->tt_rexmt) || 1732fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 1733df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1734cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 1735cb942153SJeffrey Hsu (tcp_do_newreno && 1736cb942153SJeffrey Hsu SEQ_LT(tp->snd_una, 1737cb942153SJeffrey Hsu tp->snd_recover))) { 173846f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 173946f58482SJonathan Lemon (void) tcp_output(tp); 174046f58482SJonathan Lemon goto drop; 1741cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 1742cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 1743cb942153SJeffrey Hsu u_int win; 1744cb942153SJeffrey Hsu if (tcp_do_newreno && 1745cb942153SJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_high)) { 1746cb942153SJeffrey Hsu tp->t_dupacks = 0; 1747cb942153SJeffrey Hsu break; 174846f58482SJonathan Lemon } 1749cb942153SJeffrey Hsu win = min(tp->snd_wnd, tp->snd_cwnd) / 1750cb942153SJeffrey Hsu 2 / tp->t_maxseg; 1751df8bae1dSRodney W. Grimes if (win < 2) 1752df8bae1dSRodney W. Grimes win = 2; 1753df8bae1dSRodney W. Grimes tp->snd_ssthresh = win * tp->t_maxseg; 175446f58482SJonathan Lemon tp->snd_recover = tp->snd_max; 17559b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 17569b8b58e0SJonathan Lemon tp->t_rtttime = 0; 1757fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 1758df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 1759df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1760df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 1761df8bae1dSRodney W. Grimes tp->t_maxseg * tp->t_dupacks; 1762df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 1763df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 1764df8bae1dSRodney W. Grimes goto drop; 1765df8bae1dSRodney W. Grimes } 1766df8bae1dSRodney W. Grimes } else 1767df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1768df8bae1dSRodney W. Grimes break; 1769df8bae1dSRodney W. Grimes } 1770cb942153SJeffrey Hsu 1771cb942153SJeffrey Hsu KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una")); 1772cb942153SJeffrey Hsu 1773df8bae1dSRodney W. Grimes /* 1774df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 1775df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 1776df8bae1dSRodney W. Grimes */ 1777c068736aSJeffrey Hsu if (tcp_do_newreno) { 1778cb942153SJeffrey Hsu if (SEQ_LT(tp->snd_una, tp->snd_recover)) { 1779cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 1780c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 1781c068736aSJeffrey Hsu } else { 1782c068736aSJeffrey Hsu /* 1783c068736aSJeffrey Hsu * Window inflation should have left us 1784c068736aSJeffrey Hsu * with approximately snd_ssthresh 1785c068736aSJeffrey Hsu * outstanding data. 1786c068736aSJeffrey Hsu * But in case we would be inclined to 1787c068736aSJeffrey Hsu * send a burst, better to do it via 1788c068736aSJeffrey Hsu * the slow start mechanism. 1789c068736aSJeffrey Hsu */ 1790c068736aSJeffrey Hsu if (SEQ_GT(th->th_ack + 1791c068736aSJeffrey Hsu tp->snd_ssthresh, 1792c068736aSJeffrey Hsu tp->snd_max)) 1793c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_max - 1794c068736aSJeffrey Hsu th->th_ack + 1795c068736aSJeffrey Hsu tp->t_maxseg; 1796c068736aSJeffrey Hsu else 1797c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_ssthresh; 1798c068736aSJeffrey Hsu } 1799c068736aSJeffrey Hsu } 1800c068736aSJeffrey Hsu } else { 1801233e8c18SGarrett Wollman if (tp->t_dupacks >= tcprexmtthresh && 1802df8bae1dSRodney W. Grimes tp->snd_cwnd > tp->snd_ssthresh) 1803df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh; 180446f58482SJonathan Lemon } 1805cb942153SJeffrey Hsu tp->t_dupacks = 0; 1806fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_max)) { 1807df8bae1dSRodney W. Grimes tcpstat.tcps_rcvacktoomuch++; 1808df8bae1dSRodney W. Grimes goto dropafterack; 1809df8bae1dSRodney W. Grimes } 1810a0292f23SGarrett Wollman /* 1811a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 1812a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 1813a0292f23SGarrett Wollman */ 1814a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 1815a0292f23SGarrett Wollman /* 1816a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 1817a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 181807e43e10SAndras Olah * synchronized). Go to non-starred state, 181907e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 182007e43e10SAndras Olah * we can do window scaling. 1821a0292f23SGarrett Wollman */ 1822a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 1823a0292f23SGarrett Wollman tp->snd_una++; 182407e43e10SAndras Olah /* Do window scaling? */ 182507e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 182607e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 182707e43e10SAndras Olah tp->snd_scale = tp->requested_s_scale; 182807e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 182907e43e10SAndras Olah } 1830a0292f23SGarrett Wollman } 1831a0292f23SGarrett Wollman 1832a0292f23SGarrett Wollman process_ACK: 1833fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 1834df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 1835df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 1836df8bae1dSRodney W. Grimes 1837df8bae1dSRodney W. Grimes /* 18389b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 18399b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 18409b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 18419b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 18429b8b58e0SJonathan Lemon * we left off. 18439b8b58e0SJonathan Lemon */ 18449b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 1845d65bf08aSMatthew Dillon ++tcpstat.tcps_sndrexmitbad; 18469b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 18479b8b58e0SJonathan Lemon tp->snd_ssthresh = tp->snd_ssthresh_prev; 1848cb942153SJeffrey Hsu tp->snd_high = tp->snd_high_prev; 18499b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 18509b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; /* XXX probably not required */ 18519b8b58e0SJonathan Lemon } 18529b8b58e0SJonathan Lemon 18539b8b58e0SJonathan Lemon /* 1854df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 1855df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 1856df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 1857df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 1858df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 1859df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 1860df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 1861fa55172bSMatthew Dillon * 1862fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1863fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1864fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1865fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1866df8bae1dSRodney W. Grimes */ 1867fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1868fa55172bSMatthew Dillon to.to_tsecr) { 18699b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 1870fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 18719b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 1872fa55172bSMatthew Dillon } 18731fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 1874df8bae1dSRodney W. Grimes 1875df8bae1dSRodney W. Grimes /* 1876df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 1877df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 1878df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 1879df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 1880df8bae1dSRodney W. Grimes */ 1881fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 18829b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 1883df8bae1dSRodney W. Grimes needoutput = 1; 18849b8b58e0SJonathan Lemon } else if (!callout_active(tp->tt_persist)) 18859b8b58e0SJonathan Lemon callout_reset(tp->tt_rexmt, tp->t_rxtcur, 18869b8b58e0SJonathan Lemon tcp_timer_rexmt, tp); 1887a0292f23SGarrett Wollman 1888a0292f23SGarrett Wollman /* 1889a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 1890a0292f23SGarrett Wollman * skip rest of ACK processing. 1891a0292f23SGarrett Wollman */ 1892a0292f23SGarrett Wollman if (acked == 0) 1893a0292f23SGarrett Wollman goto step6; 1894a0292f23SGarrett Wollman 1895df8bae1dSRodney W. Grimes /* 1896df8bae1dSRodney W. Grimes * When new data is acked, open the congestion window. 1897df8bae1dSRodney W. Grimes * If the window gives us less than ssthresh packets 1898df8bae1dSRodney W. Grimes * in flight, open exponentially (maxseg per packet). 1899df8bae1dSRodney W. Grimes * Otherwise open linearly: maxseg per window 190010be5648SGarrett Wollman * (maxseg^2 / cwnd per packet). 1901df8bae1dSRodney W. Grimes */ 1902cb942153SJeffrey Hsu if (!tcp_do_newreno || SEQ_GEQ(tp->snd_una, tp->snd_recover)) { 1903df8bae1dSRodney W. Grimes register u_int cw = tp->snd_cwnd; 1904df8bae1dSRodney W. Grimes register u_int incr = tp->t_maxseg; 1905df8bae1dSRodney W. Grimes if (cw > tp->snd_ssthresh) 190610be5648SGarrett Wollman incr = incr * incr / cw; 1907df8bae1dSRodney W. Grimes tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale); 1908df8bae1dSRodney W. Grimes } 1909df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 1910df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 1911df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 1912df8bae1dSRodney W. Grimes ourfinisacked = 1; 1913df8bae1dSRodney W. Grimes } else { 1914df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 1915df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 1916df8bae1dSRodney W. Grimes ourfinisacked = 0; 1917df8bae1dSRodney W. Grimes } 1918df8bae1dSRodney W. Grimes sowwakeup(so); 1919cb942153SJeffrey Hsu /* detect una wraparound */ 1920cb942153SJeffrey Hsu if (SEQ_GEQ(tp->snd_una, tp->snd_recover) && 1921cb942153SJeffrey Hsu SEQ_LT(th->th_ack, tp->snd_recover)) 1922cb942153SJeffrey Hsu tp->snd_recover = th->th_ack; 1923cb942153SJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_high) && 1924cb942153SJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_high)) 1925cb942153SJeffrey Hsu tp->snd_high = th->th_ack - 1; 1926fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 1927df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1928df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 1929df8bae1dSRodney W. Grimes 1930df8bae1dSRodney W. Grimes switch (tp->t_state) { 1931df8bae1dSRodney W. Grimes 1932df8bae1dSRodney W. Grimes /* 1933df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 1934df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 1935df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 1936df8bae1dSRodney W. Grimes */ 1937df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1938df8bae1dSRodney W. Grimes if (ourfinisacked) { 1939df8bae1dSRodney W. Grimes /* 1940df8bae1dSRodney W. Grimes * If we can't receive any more 1941df8bae1dSRodney W. Grimes * data, then closing user can proceed. 1942df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 1943df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 1944df8bae1dSRodney W. Grimes * we'll hang forever. 1945df8bae1dSRodney W. Grimes */ 1946340c35deSJonathan Lemon /* XXXjl 1947340c35deSJonathan Lemon * we should release the tp also, and use a 1948340c35deSJonathan Lemon * compressed state. 1949340c35deSJonathan Lemon */ 1950df8bae1dSRodney W. Grimes if (so->so_state & SS_CANTRCVMORE) { 195103e49181SSeigo Tanimura soisdisconnected(so); 19529b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, tcp_maxidle, 19539b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 19544cc20ab1SSeigo Tanimura } 1955df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 1956df8bae1dSRodney W. Grimes } 1957df8bae1dSRodney W. Grimes break; 1958df8bae1dSRodney W. Grimes 1959df8bae1dSRodney W. Grimes /* 1960df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 1961df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 1962df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 1963df8bae1dSRodney W. Grimes * the segment. 1964df8bae1dSRodney W. Grimes */ 1965df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1966df8bae1dSRodney W. Grimes if (ourfinisacked) { 1967340c35deSJonathan Lemon KASSERT(headlocked, ("headlocked")); 1968340c35deSJonathan Lemon INP_INFO_WUNLOCK(&tcbinfo); 1969340c35deSJonathan Lemon m_freem(m); 1970340c35deSJonathan Lemon tcp_twstart(tp); 1971340c35deSJonathan Lemon return; 1972df8bae1dSRodney W. Grimes } 1973df8bae1dSRodney W. Grimes break; 1974df8bae1dSRodney W. Grimes 1975df8bae1dSRodney W. Grimes /* 1976df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 1977df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 1978df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 1979df8bae1dSRodney W. Grimes * enter the closed state and return. 1980df8bae1dSRodney W. Grimes */ 1981df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 1982df8bae1dSRodney W. Grimes if (ourfinisacked) { 1983df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1984df8bae1dSRodney W. Grimes goto drop; 1985df8bae1dSRodney W. Grimes } 1986df8bae1dSRodney W. Grimes break; 1987df8bae1dSRodney W. Grimes 1988df8bae1dSRodney W. Grimes /* 1989df8bae1dSRodney W. Grimes * In TIME_WAIT state the only thing that should arrive 1990df8bae1dSRodney W. Grimes * is a retransmission of the remote FIN. Acknowledge 1991df8bae1dSRodney W. Grimes * it and restart the finack timer. 1992df8bae1dSRodney W. Grimes */ 1993df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 1994340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 19959b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 19969b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 1997df8bae1dSRodney W. Grimes goto dropafterack; 1998df8bae1dSRodney W. Grimes } 1999df8bae1dSRodney W. Grimes } 2000df8bae1dSRodney W. Grimes 2001df8bae1dSRodney W. Grimes step6: 2002df8bae1dSRodney W. Grimes /* 2003df8bae1dSRodney W. Grimes * Update window information. 2004df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 2005df8bae1dSRodney W. Grimes */ 2006fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 2007fb59c426SYoshinobu Inoue (SEQ_LT(tp->snd_wl1, th->th_seq) || 2008fb59c426SYoshinobu Inoue (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2009fb59c426SYoshinobu Inoue (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2010df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 2011fb59c426SYoshinobu Inoue if (tlen == 0 && 2012fb59c426SYoshinobu Inoue tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2013df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinupd++; 2014df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 2015fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq; 2016fb59c426SYoshinobu Inoue tp->snd_wl2 = th->th_ack; 2017df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2018df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 2019df8bae1dSRodney W. Grimes needoutput = 1; 2020df8bae1dSRodney W. Grimes } 2021df8bae1dSRodney W. Grimes 2022df8bae1dSRodney W. Grimes /* 2023df8bae1dSRodney W. Grimes * Process segments with URG. 2024df8bae1dSRodney W. Grimes */ 2025fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2026df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2027df8bae1dSRodney W. Grimes /* 2028df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2029df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2030df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2031df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2032df8bae1dSRodney W. Grimes */ 2033fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2034fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2035fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 2036df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2037df8bae1dSRodney W. Grimes } 2038df8bae1dSRodney W. Grimes /* 2039df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2040df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2041df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2042df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2043df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2044df8bae1dSRodney W. Grimes * 2045df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2046df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2047df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2048df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2049df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2050df8bae1dSRodney W. Grimes * spec states (in one of two places). 2051df8bae1dSRodney W. Grimes */ 2052fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2053fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2054df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2055df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 20564cc20ab1SSeigo Tanimura if (so->so_oobmark == 0) 2057df8bae1dSRodney W. Grimes so->so_state |= SS_RCVATMARK; 2058df8bae1dSRodney W. Grimes sohasoutofband(so); 2059df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2060df8bae1dSRodney W. Grimes } 2061df8bae1dSRodney W. Grimes /* 2062df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2063df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2064df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2065df8bae1dSRodney W. Grimes * data may creep in... ick. 2066df8bae1dSRodney W. Grimes */ 206730613f56SJeffrey Hsu if (th->th_urp <= (u_long)tlen && 206830613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 206930613f56SJeffrey Hsu /* hdr drop is delayed */ 207030613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 207130613f56SJeffrey Hsu } 2072c068736aSJeffrey Hsu } else { 2073df8bae1dSRodney W. Grimes /* 2074df8bae1dSRodney W. Grimes * If no out of band data is expected, 2075df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2076df8bae1dSRodney W. Grimes * with the receive window. 2077df8bae1dSRodney W. Grimes */ 2078df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2079df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2080c068736aSJeffrey Hsu } 2081df8bae1dSRodney W. Grimes dodata: /* XXX */ 2082f76fcf6dSJeffrey Hsu KASSERT(headlocked, ("headlocked")); 2083f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2084f76fcf6dSJeffrey Hsu headlocked = 0; 2085df8bae1dSRodney W. Grimes /* 2086df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2087df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2088df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2089df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2090df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2091df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2092df8bae1dSRodney W. Grimes */ 2093fb59c426SYoshinobu Inoue if ((tlen || (thflags & TH_FIN)) && 2094df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2095fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2096e4b64281SJesper Skriver /* 2097c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2098c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2099c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2100c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2101c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2102c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2103c068736aSJeffrey Hsu * conversions. 2104c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 2105c068736aSJeffrey Hsu * immediately when segments are out of order (so 2106c068736aSJeffrey Hsu * fast retransmit can work). 2107e4b64281SJesper Skriver */ 2108e4b64281SJesper Skriver if (th->th_seq == tp->rcv_nxt && 2109e4b64281SJesper Skriver LIST_EMPTY(&tp->t_segq) && 2110e4b64281SJesper Skriver TCPS_HAVEESTABLISHED(tp->t_state)) { 2111e4b64281SJesper Skriver if (DELAY_ACK(tp)) 21123bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2113e4b64281SJesper Skriver else 2114e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2115e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2116e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 2117e4b64281SJesper Skriver tcpstat.tcps_rcvpack++; 2118e4b64281SJesper Skriver tcpstat.tcps_rcvbyte += tlen; 2119e4b64281SJesper Skriver ND6_HINT(tp); 2120c1c36a2cSMike Silbersack if (so->so_state & SS_CANTRCVMORE) 2121c1c36a2cSMike Silbersack m_freem(m); 2122c1c36a2cSMike Silbersack else 2123e4b64281SJesper Skriver sbappend(&so->so_rcv, m); 2124e4b64281SJesper Skriver sorwakeup(so); 2125e4b64281SJesper Skriver } else { 2126e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 2127e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2128e4b64281SJesper Skriver } 2129e4b64281SJesper Skriver 2130df8bae1dSRodney W. Grimes /* 2131df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2132df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2133df8bae1dSRodney W. Grimes * buffer size. 2134df8bae1dSRodney W. Grimes */ 2135df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2136df8bae1dSRodney W. Grimes } else { 2137df8bae1dSRodney W. Grimes m_freem(m); 2138fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2139df8bae1dSRodney W. Grimes } 2140df8bae1dSRodney W. Grimes 2141df8bae1dSRodney W. Grimes /* 2142df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2143df8bae1dSRodney W. Grimes * that the connection is closing. 2144df8bae1dSRodney W. Grimes */ 2145fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2146df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2147df8bae1dSRodney W. Grimes socantrcvmore(so); 2148a0292f23SGarrett Wollman /* 2149a0292f23SGarrett Wollman * If connection is half-synchronized 2150d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2151a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2152a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2153a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2154a0292f23SGarrett Wollman */ 21553bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 21563bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2157a0292f23SGarrett Wollman else 2158df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2159df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2160df8bae1dSRodney W. Grimes } 2161df8bae1dSRodney W. Grimes switch (tp->t_state) { 2162df8bae1dSRodney W. Grimes 2163df8bae1dSRodney W. Grimes /* 2164df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2165df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2166df8bae1dSRodney W. Grimes */ 2167df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 21689b8b58e0SJonathan Lemon tp->t_starttime = ticks; 21699b8b58e0SJonathan Lemon /*FALLTHROUGH*/ 2170df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2171df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2172df8bae1dSRodney W. Grimes break; 2173df8bae1dSRodney W. Grimes 2174df8bae1dSRodney W. Grimes /* 2175df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2176df8bae1dSRodney W. Grimes * enter the CLOSING state. 2177df8bae1dSRodney W. Grimes */ 2178df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2179df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 2180df8bae1dSRodney W. Grimes break; 2181df8bae1dSRodney W. Grimes 2182df8bae1dSRodney W. Grimes /* 2183df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 2184df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 2185df8bae1dSRodney W. Grimes * standard timers. 2186df8bae1dSRodney W. Grimes */ 2187df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2188340c35deSJonathan Lemon KASSERT(headlocked == 0, ("headlocked")); 2189340c35deSJonathan Lemon tcp_twstart(tp); 2190340c35deSJonathan Lemon return; 2191df8bae1dSRodney W. Grimes 2192df8bae1dSRodney W. Grimes /* 2193df8bae1dSRodney W. Grimes * In TIME_WAIT state restart the 2 MSL time_wait timer. 2194df8bae1dSRodney W. Grimes */ 2195df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 2196340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 21979b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 21989b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2199df8bae1dSRodney W. Grimes break; 2200df8bae1dSRodney W. Grimes } 2201df8bae1dSRodney W. Grimes } 2202610ee2f9SDavid Greenman #ifdef TCPDEBUG 22034cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2204fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2205fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2206610ee2f9SDavid Greenman #endif 2207df8bae1dSRodney W. Grimes 2208df8bae1dSRodney W. Grimes /* 2209df8bae1dSRodney W. Grimes * Return any desired output. 2210df8bae1dSRodney W. Grimes */ 2211df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 2212df8bae1dSRodney W. Grimes (void) tcp_output(tp); 22133bfd6421SJonathan Lemon else if (tp->t_flags & TF_DELACK) { 22143bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 22153bfd6421SJonathan Lemon KASSERT(!callout_active(tp->tt_delack), 22163bfd6421SJonathan Lemon ("delayed ack already active")); 22173bfd6421SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 22183bfd6421SJonathan Lemon tcp_timer_delack, tp); 22193bfd6421SJonathan Lemon } 2220f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 2221df8bae1dSRodney W. Grimes return; 2222df8bae1dSRodney W. Grimes 2223df8bae1dSRodney W. Grimes dropafterack: 2224df8bae1dSRodney W. Grimes /* 2225df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 2226df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 222780ab7c0eSGarrett Wollman * 222880ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 222980ab7c0eSGarrett Wollman * paths to this code happen after packets containing 223080ab7c0eSGarrett Wollman * RST have been dropped. 223180ab7c0eSGarrett Wollman * 223280ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 223380ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 223480ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 223580ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 223680ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 223780ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 2238df8bae1dSRodney W. Grimes */ 2239fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2240fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 2241a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 2242a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2243a57815efSBosko Milekic goto dropwithreset; 2244a57815efSBosko Milekic } 2245a0292f23SGarrett Wollman #ifdef TCPDEBUG 22464cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2247fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2248fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2249a0292f23SGarrett Wollman #endif 225085e8b243SJeffrey Hsu KASSERT(headlocked, ("headlocked should be 1")); 2251f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2252df8bae1dSRodney W. Grimes m_freem(m); 2253df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2254df8bae1dSRodney W. Grimes (void) tcp_output(tp); 2255f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 2256df8bae1dSRodney W. Grimes return; 2257df8bae1dSRodney W. Grimes 2258df8bae1dSRodney W. Grimes dropwithreset: 2259df8bae1dSRodney W. Grimes /* 2260df8bae1dSRodney W. Grimes * Generate a RST, dropping incoming segment. 2261df8bae1dSRodney W. Grimes * Make ACK acceptable to originator of segment. 2262df8bae1dSRodney W. Grimes * Don't bother to respond if destination was broadcast/multicast. 2263df8bae1dSRodney W. Grimes */ 2264fb59c426SYoshinobu Inoue if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2265df8bae1dSRodney W. Grimes goto drop; 2266fb59c426SYoshinobu Inoue if (isipv6) { 2267173c0f9fSWarner Losh if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2268173c0f9fSWarner Losh IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2269fb59c426SYoshinobu Inoue goto drop; 2270c068736aSJeffrey Hsu } else { 2271173c0f9fSWarner Losh if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2272173c0f9fSWarner Losh IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 22732ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 22742ca2159fSCrist J. Clark in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2275fb59c426SYoshinobu Inoue goto drop; 2276c068736aSJeffrey Hsu } 2277fb59c426SYoshinobu Inoue /* IPv6 anycast check is done at tcp6_input() */ 2278a57815efSBosko Milekic 2279a57815efSBosko Milekic /* 2280c59319bfSDag-Erling Smørgrav * Perform bandwidth limiting. 2281a57815efSBosko Milekic */ 2282a57815efSBosko Milekic if (badport_bandlim(rstreason) < 0) 2283a57815efSBosko Milekic goto drop; 2284a57815efSBosko Milekic 2285a0292f23SGarrett Wollman #ifdef TCPDEBUG 22864cc20ab1SSeigo Tanimura if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2287fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2288fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2289a0292f23SGarrett Wollman #endif 22906fd22cafSJeffrey Hsu 22916fd22cafSJeffrey Hsu if (tp) 22926fd22cafSJeffrey Hsu INP_UNLOCK(inp); 22936fd22cafSJeffrey Hsu 2294fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 2295fb59c426SYoshinobu Inoue /* mtod() below is safe as long as hdr dropping is delayed */ 2296fb59c426SYoshinobu Inoue tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack, 2297fb59c426SYoshinobu Inoue TH_RST); 2298df8bae1dSRodney W. Grimes else { 2299fb59c426SYoshinobu Inoue if (thflags & TH_SYN) 2300fb59c426SYoshinobu Inoue tlen++; 2301fb59c426SYoshinobu Inoue /* mtod() below is safe as long as hdr dropping is delayed */ 2302fb59c426SYoshinobu Inoue tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2303fb59c426SYoshinobu Inoue (tcp_seq)0, TH_RST|TH_ACK); 2304df8bae1dSRodney W. Grimes } 2305f76fcf6dSJeffrey Hsu if (headlocked) 2306f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2307df8bae1dSRodney W. Grimes return; 2308df8bae1dSRodney W. Grimes 2309df8bae1dSRodney W. Grimes drop: 2310df8bae1dSRodney W. Grimes /* 2311df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 2312df8bae1dSRodney W. Grimes */ 2313610ee2f9SDavid Greenman #ifdef TCPDEBUG 23144cc20ab1SSeigo Tanimura if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2315fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2316fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2317a0292f23SGarrett Wollman #endif 2318f76fcf6dSJeffrey Hsu if (tp) 2319f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 2320df8bae1dSRodney W. Grimes m_freem(m); 2321f76fcf6dSJeffrey Hsu if (headlocked) 2322f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2323df8bae1dSRodney W. Grimes return; 2324df8bae1dSRodney W. Grimes } 2325df8bae1dSRodney W. Grimes 2326be2ac88cSJonathan Lemon /* 2327be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 2328be2ac88cSJonathan Lemon */ 23290312fbe9SPoul-Henning Kamp static void 2330be2ac88cSJonathan Lemon tcp_dooptions(to, cp, cnt, is_syn) 2331be2ac88cSJonathan Lemon struct tcpopt *to; 2332df8bae1dSRodney W. Grimes u_char *cp; 2333df8bae1dSRodney W. Grimes int cnt; 2334df8bae1dSRodney W. Grimes { 2335df8bae1dSRodney W. Grimes int opt, optlen; 2336df8bae1dSRodney W. Grimes 2337be2ac88cSJonathan Lemon to->to_flags = 0; 2338df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 2339df8bae1dSRodney W. Grimes opt = cp[0]; 2340df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 2341df8bae1dSRodney W. Grimes break; 2342df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 2343df8bae1dSRodney W. Grimes optlen = 1; 2344df8bae1dSRodney W. Grimes else { 2345b474779fSJun-ichiro itojun Hagino if (cnt < 2) 2346b474779fSJun-ichiro itojun Hagino break; 2347df8bae1dSRodney W. Grimes optlen = cp[1]; 2348b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 2349df8bae1dSRodney W. Grimes break; 2350df8bae1dSRodney W. Grimes } 2351df8bae1dSRodney W. Grimes switch (opt) { 2352df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 2353df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 2354df8bae1dSRodney W. Grimes continue; 2355be2ac88cSJonathan Lemon if (!is_syn) 2356df8bae1dSRodney W. Grimes continue; 2357be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 2358be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 2359be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 2360fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 2361df8bae1dSRodney W. Grimes break; 2362df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 2363df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 2364df8bae1dSRodney W. Grimes continue; 2365be2ac88cSJonathan Lemon if (! is_syn) 2366df8bae1dSRodney W. Grimes continue; 2367be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 2368be2ac88cSJonathan Lemon to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 2369df8bae1dSRodney W. Grimes break; 2370df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 2371df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 2372df8bae1dSRodney W. Grimes continue; 2373be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 2374a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2375a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 2376fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 2377a0292f23SGarrett Wollman bcopy((char *)cp + 6, 2378a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2379fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 2380df8bae1dSRodney W. Grimes break; 2381a0292f23SGarrett Wollman case TCPOPT_CC: 2382a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2383a0292f23SGarrett Wollman continue; 2384be2ac88cSJonathan Lemon to->to_flags |= TOF_CC; 2385a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2386a0292f23SGarrett Wollman (char *)&to->to_cc, sizeof(to->to_cc)); 2387fd8e4ebcSMike Barcroft to->to_cc = ntohl(to->to_cc); 2388a0292f23SGarrett Wollman break; 2389a0292f23SGarrett Wollman case TCPOPT_CCNEW: 2390a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2391a0292f23SGarrett Wollman continue; 2392be2ac88cSJonathan Lemon if (!is_syn) 2393a0292f23SGarrett Wollman continue; 2394be2ac88cSJonathan Lemon to->to_flags |= TOF_CCNEW; 2395a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2396a0292f23SGarrett Wollman (char *)&to->to_cc, sizeof(to->to_cc)); 2397fd8e4ebcSMike Barcroft to->to_cc = ntohl(to->to_cc); 2398a0292f23SGarrett Wollman break; 2399a0292f23SGarrett Wollman case TCPOPT_CCECHO: 2400a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2401a0292f23SGarrett Wollman continue; 2402be2ac88cSJonathan Lemon if (!is_syn) 2403a0292f23SGarrett Wollman continue; 2404be2ac88cSJonathan Lemon to->to_flags |= TOF_CCECHO; 2405a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2406a0292f23SGarrett Wollman (char *)&to->to_ccecho, sizeof(to->to_ccecho)); 2407fd8e4ebcSMike Barcroft to->to_ccecho = ntohl(to->to_ccecho); 2408a0292f23SGarrett Wollman break; 2409be2ac88cSJonathan Lemon default: 2410be2ac88cSJonathan Lemon continue; 2411df8bae1dSRodney W. Grimes } 2412df8bae1dSRodney W. Grimes } 2413df8bae1dSRodney W. Grimes } 2414df8bae1dSRodney W. Grimes 2415df8bae1dSRodney W. Grimes /* 2416df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 2417df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 2418df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 2419df8bae1dSRodney W. Grimes * sequencing purposes. 2420df8bae1dSRodney W. Grimes */ 24210312fbe9SPoul-Henning Kamp static void 2422fb59c426SYoshinobu Inoue tcp_pulloutofband(so, th, m, off) 2423df8bae1dSRodney W. Grimes struct socket *so; 2424fb59c426SYoshinobu Inoue struct tcphdr *th; 2425df8bae1dSRodney W. Grimes register struct mbuf *m; 2426fb59c426SYoshinobu Inoue int off; /* delayed to be droped hdrlen */ 2427df8bae1dSRodney W. Grimes { 2428fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 2429df8bae1dSRodney W. Grimes 2430df8bae1dSRodney W. Grimes while (cnt >= 0) { 2431df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 2432df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 2433df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 2434df8bae1dSRodney W. Grimes 2435df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 2436df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 2437df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2438df8bae1dSRodney W. Grimes m->m_len--; 243969a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 244069a34685SYoshinobu Inoue m->m_pkthdr.len--; 2441df8bae1dSRodney W. Grimes return; 2442df8bae1dSRodney W. Grimes } 2443df8bae1dSRodney W. Grimes cnt -= m->m_len; 2444df8bae1dSRodney W. Grimes m = m->m_next; 2445df8bae1dSRodney W. Grimes if (m == 0) 2446df8bae1dSRodney W. Grimes break; 2447df8bae1dSRodney W. Grimes } 2448df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 2449df8bae1dSRodney W. Grimes } 2450df8bae1dSRodney W. Grimes 2451df8bae1dSRodney W. Grimes /* 2452df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 2453df8bae1dSRodney W. Grimes * and update averages and current timeout. 2454df8bae1dSRodney W. Grimes */ 24550312fbe9SPoul-Henning Kamp static void 2456df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt) 2457df8bae1dSRodney W. Grimes register struct tcpcb *tp; 24589b8b58e0SJonathan Lemon int rtt; 2459df8bae1dSRodney W. Grimes { 2460233e8c18SGarrett Wollman register int delta; 2461233e8c18SGarrett Wollman 2462233e8c18SGarrett Wollman tcpstat.tcps_rttupdated++; 2463233e8c18SGarrett Wollman tp->t_rttupdated++; 2464233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 2465233e8c18SGarrett Wollman /* 2466233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 2467233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 2468233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 2469233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2470233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 2471233e8c18SGarrett Wollman */ 2472233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2473233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2474233e8c18SGarrett Wollman 2475233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 2476233e8c18SGarrett Wollman tp->t_srtt = 1; 2477233e8c18SGarrett Wollman 2478233e8c18SGarrett Wollman /* 2479233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 2480233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 2481233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 2482233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 2483233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 2484233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 2485233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2486233e8c18SGarrett Wollman * rfc793's wired-in beta. 2487233e8c18SGarrett Wollman */ 2488233e8c18SGarrett Wollman if (delta < 0) 2489233e8c18SGarrett Wollman delta = -delta; 2490233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2491233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 2492233e8c18SGarrett Wollman tp->t_rttvar = 1; 24931fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 24941fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2495233e8c18SGarrett Wollman } else { 2496233e8c18SGarrett Wollman /* 2497233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 2498233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 2499233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 2500233e8c18SGarrett Wollman */ 2501233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 2502233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 25031fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2504233e8c18SGarrett Wollman } 25059b8b58e0SJonathan Lemon tp->t_rtttime = 0; 2506df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 2507df8bae1dSRodney W. Grimes 2508df8bae1dSRodney W. Grimes /* 2509df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 2510df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 2511df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 2512df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 2513df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 2514df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 2515df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 2516df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 2517df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 2518df8bae1dSRodney W. Grimes */ 2519233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 25209e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2521df8bae1dSRodney W. Grimes 2522df8bae1dSRodney W. Grimes /* 2523df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 2524df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 2525df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 2526df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 2527df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 2528df8bae1dSRodney W. Grimes */ 2529df8bae1dSRodney W. Grimes tp->t_softerror = 0; 2530df8bae1dSRodney W. Grimes } 2531df8bae1dSRodney W. Grimes 2532df8bae1dSRodney W. Grimes /* 2533df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 2534df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 2535df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 2536df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 2537df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2538df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 2539df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 2540df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 2541df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 2542df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 2543df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 2544df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 2545df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 2546a0292f23SGarrett Wollman * 2547a0292f23SGarrett Wollman * Also take into account the space needed for options that we 2548a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 2549a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 2550a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 2551a0292f23SGarrett Wollman * data in maxopd. 2552a0292f23SGarrett Wollman * 2553a0292f23SGarrett Wollman * NOTE that this routine is only called when we process an incoming 2554a0292f23SGarrett Wollman * segment, for outgoing segments only tcp_mssopt is called. 2555a0292f23SGarrett Wollman * 2556a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 2557a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 2558a0292f23SGarrett Wollman * MSS of our peer. 2559df8bae1dSRodney W. Grimes */ 2560a0292f23SGarrett Wollman void 2561df8bae1dSRodney W. Grimes tcp_mss(tp, offer) 2562a0292f23SGarrett Wollman struct tcpcb *tp; 2563a0292f23SGarrett Wollman int offer; 2564df8bae1dSRodney W. Grimes { 2565df8bae1dSRodney W. Grimes register struct rtentry *rt; 2566df8bae1dSRodney W. Grimes struct ifnet *ifp; 2567df8bae1dSRodney W. Grimes register int rtt, mss; 2568df8bae1dSRodney W. Grimes u_long bufsize; 2569c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 2570df8bae1dSRodney W. Grimes struct socket *so; 2571a0292f23SGarrett Wollman struct rmxp_tao *taop; 2572a0292f23SGarrett Wollman int origoffer = offer; 2573fb59c426SYoshinobu Inoue #ifdef INET6 2574c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2575c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 2576c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 2577c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 2578c068736aSJeffrey Hsu #else 2579c068736aSJeffrey Hsu const int isipv6 = 0; 2580c068736aSJeffrey Hsu const size_t min_protoh = sizeof (struct tcpiphdr); 2581fb59c426SYoshinobu Inoue #endif 2582df8bae1dSRodney W. Grimes 2583fb59c426SYoshinobu Inoue if (isipv6) 2584be2ac88cSJonathan Lemon rt = tcp_rtlookup6(&inp->inp_inc); 2585fb59c426SYoshinobu Inoue else 2586be2ac88cSJonathan Lemon rt = tcp_rtlookup(&inp->inp_inc); 2587fb59c426SYoshinobu Inoue if (rt == NULL) { 2588fb59c426SYoshinobu Inoue tp->t_maxopd = tp->t_maxseg = 2589c068736aSJeffrey Hsu isipv6 ? tcp_v6mssdflt : tcp_mssdflt; 2590a0292f23SGarrett Wollman return; 2591df8bae1dSRodney W. Grimes } 2592df8bae1dSRodney W. Grimes ifp = rt->rt_ifp; 2593df8bae1dSRodney W. Grimes so = inp->inp_socket; 2594df8bae1dSRodney W. Grimes 2595a0292f23SGarrett Wollman taop = rmx_taop(rt->rt_rmx); 2596a0292f23SGarrett Wollman /* 2597a0292f23SGarrett Wollman * Offer == -1 means that we didn't receive SYN yet, 2598a0292f23SGarrett Wollman * use cached value in that case; 2599a0292f23SGarrett Wollman */ 2600a0292f23SGarrett Wollman if (offer == -1) 2601a0292f23SGarrett Wollman offer = taop->tao_mssopt; 2602a0292f23SGarrett Wollman /* 2603a0292f23SGarrett Wollman * Offer == 0 means that there was no MSS on the SYN segment, 2604a0292f23SGarrett Wollman * in this case we use tcp_mssdflt. 2605a0292f23SGarrett Wollman */ 2606a0292f23SGarrett Wollman if (offer == 0) 2607c068736aSJeffrey Hsu offer = isipv6 ? tcp_v6mssdflt : tcp_mssdflt; 2608a0292f23SGarrett Wollman else 2609a0292f23SGarrett Wollman /* 2610a0292f23SGarrett Wollman * Sanity check: make sure that maxopd will be large 2611a0292f23SGarrett Wollman * enough to allow some data on segments even is the 2612a0292f23SGarrett Wollman * all the option space is used (40bytes). Otherwise 2613a0292f23SGarrett Wollman * funny things may happen in tcp_output. 2614a0292f23SGarrett Wollman */ 2615a0292f23SGarrett Wollman offer = max(offer, 64); 2616a0292f23SGarrett Wollman taop->tao_mssopt = offer; 2617a0292f23SGarrett Wollman 2618df8bae1dSRodney W. Grimes /* 2619df8bae1dSRodney W. Grimes * While we're here, check if there's an initial rtt 2620df8bae1dSRodney W. Grimes * or rttvar. Convert from the route-table units 2621df8bae1dSRodney W. Grimes * to scaled multiples of the slow timeout timer. 2622df8bae1dSRodney W. Grimes */ 2623df8bae1dSRodney W. Grimes if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 2624df8bae1dSRodney W. Grimes /* 2625a0292f23SGarrett Wollman * XXX the lock bit for RTT indicates that the value 2626df8bae1dSRodney W. Grimes * is also a minimum value; this is subject to time. 2627df8bae1dSRodney W. Grimes */ 2628df8bae1dSRodney W. Grimes if (rt->rt_rmx.rmx_locks & RTV_RTT) 26299b8b58e0SJonathan Lemon tp->t_rttmin = rtt / (RTM_RTTUNIT / hz); 26309b8b58e0SJonathan Lemon tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE)); 26311fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 2632dd224982SGarrett Wollman tcpstat.tcps_usedrtt++; 2633dd224982SGarrett Wollman if (rt->rt_rmx.rmx_rttvar) { 2634df8bae1dSRodney W. Grimes tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 26359b8b58e0SJonathan Lemon (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE)); 2636dd224982SGarrett Wollman tcpstat.tcps_usedrttvar++; 2637dd224982SGarrett Wollman } else { 2638df8bae1dSRodney W. Grimes /* default variation is +- 1 rtt */ 2639df8bae1dSRodney W. Grimes tp->t_rttvar = 2640df8bae1dSRodney W. Grimes tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 2641dd224982SGarrett Wollman } 2642df8bae1dSRodney W. Grimes TCPT_RANGESET(tp->t_rxtcur, 2643df8bae1dSRodney W. Grimes ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 2644df8bae1dSRodney W. Grimes tp->t_rttmin, TCPTV_REXMTMAX); 2645df8bae1dSRodney W. Grimes } 2646df8bae1dSRodney W. Grimes /* 2647df8bae1dSRodney W. Grimes * if there's an mtu associated with the route, use it 2648fb59c426SYoshinobu Inoue * else, use the link mtu. 2649df8bae1dSRodney W. Grimes */ 2650df8bae1dSRodney W. Grimes if (rt->rt_rmx.rmx_mtu) 2651fb59c426SYoshinobu Inoue mss = rt->rt_rmx.rmx_mtu - min_protoh; 2652c068736aSJeffrey Hsu else { 2653fb59c426SYoshinobu Inoue if (isipv6) { 2654c068736aSJeffrey Hsu mss = nd_ifinfo[rt->rt_ifp->if_index].linkmtu - 2655c068736aSJeffrey Hsu min_protoh; 2656fb59c426SYoshinobu Inoue if (!in6_localaddr(&inp->in6p_faddr)) 2657fb59c426SYoshinobu Inoue mss = min(mss, tcp_v6mssdflt); 2658c068736aSJeffrey Hsu } else { 2659c068736aSJeffrey Hsu mss = ifp->if_mtu - min_protoh; 2660a0292f23SGarrett Wollman if (!in_localaddr(inp->inp_faddr)) 2661a0292f23SGarrett Wollman mss = min(mss, tcp_mssdflt); 2662a0292f23SGarrett Wollman } 2663c068736aSJeffrey Hsu } 2664a0292f23SGarrett Wollman mss = min(mss, offer); 2665a0292f23SGarrett Wollman /* 2666a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 2667a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 2668a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 2669a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 2670a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 2671a0292f23SGarrett Wollman */ 2672a0292f23SGarrett Wollman tp->t_maxopd = mss; 2673a0292f23SGarrett Wollman 2674a0292f23SGarrett Wollman /* 2675a0292f23SGarrett Wollman * In case of T/TCP, origoffer==-1 indicates, that no segments 2676a0292f23SGarrett Wollman * were received yet. In this case we just guess, otherwise 2677a0292f23SGarrett Wollman * we do the same as before T/TCP. 2678a0292f23SGarrett Wollman */ 2679a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2680a0292f23SGarrett Wollman (origoffer == -1 || 2681a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2682a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 2683a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 2684a0292f23SGarrett Wollman (origoffer == -1 || 2685a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)) 2686a0292f23SGarrett Wollman mss -= TCPOLEN_CC_APPA; 2687a0292f23SGarrett Wollman 2688df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 2689df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2690df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 2691df8bae1dSRodney W. Grimes #else 2692df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2693df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 2694df8bae1dSRodney W. Grimes #endif 2695df8bae1dSRodney W. Grimes /* 2696df8bae1dSRodney W. Grimes * If there's a pipesize, change the socket buffer 2697df8bae1dSRodney W. Grimes * to that size. Make the socket buffers an integral 2698df8bae1dSRodney W. Grimes * number of mss units; if the mss is larger than 2699df8bae1dSRodney W. Grimes * the socket buffer, decrease the mss. 2700df8bae1dSRodney W. Grimes */ 2701df8bae1dSRodney W. Grimes #ifdef RTV_SPIPE 2702df8bae1dSRodney W. Grimes if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 2703df8bae1dSRodney W. Grimes #endif 2704df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 2705df8bae1dSRodney W. Grimes if (bufsize < mss) 2706df8bae1dSRodney W. Grimes mss = bufsize; 2707df8bae1dSRodney W. Grimes else { 2708df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2709df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2710df8bae1dSRodney W. Grimes bufsize = sb_max; 271188c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 2712ecf72308SBrian Feldman (void)sbreserve(&so->so_snd, bufsize, so, NULL); 2713df8bae1dSRodney W. Grimes } 2714df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 2715df8bae1dSRodney W. Grimes 2716df8bae1dSRodney W. Grimes #ifdef RTV_RPIPE 2717df8bae1dSRodney W. Grimes if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 2718df8bae1dSRodney W. Grimes #endif 2719df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 2720df8bae1dSRodney W. Grimes if (bufsize > mss) { 2721df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2722df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2723df8bae1dSRodney W. Grimes bufsize = sb_max; 272488c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 2725ecf72308SBrian Feldman (void)sbreserve(&so->so_rcv, bufsize, so, NULL); 2726df8bae1dSRodney W. Grimes } 27279b8b58e0SJonathan Lemon 2728a0292f23SGarrett Wollman /* 27299b8b58e0SJonathan Lemon * Set the slow-start flight size depending on whether this 27309b8b58e0SJonathan Lemon * is a local network or not. 2731a0292f23SGarrett Wollman */ 2732c068736aSJeffrey Hsu if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) || 2733c068736aSJeffrey Hsu (!isipv6 && in_localaddr(inp->inp_faddr))) 27349b8b58e0SJonathan Lemon tp->snd_cwnd = mss * ss_fltsz_local; 27359b8b58e0SJonathan Lemon else 27369b8b58e0SJonathan Lemon tp->snd_cwnd = mss * ss_fltsz; 2737df8bae1dSRodney W. Grimes 2738df8bae1dSRodney W. Grimes if (rt->rt_rmx.rmx_ssthresh) { 2739df8bae1dSRodney W. Grimes /* 2740df8bae1dSRodney W. Grimes * There's some sort of gateway or interface 2741df8bae1dSRodney W. Grimes * buffer limit on the path. Use this to set 2742df8bae1dSRodney W. Grimes * the slow start threshhold, but set the 2743df8bae1dSRodney W. Grimes * threshold to no less than 2*mss. 2744df8bae1dSRodney W. Grimes */ 2745df8bae1dSRodney W. Grimes tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 2746dd224982SGarrett Wollman tcpstat.tcps_usedssthresh++; 2747df8bae1dSRodney W. Grimes } 2748a0292f23SGarrett Wollman } 2749a0292f23SGarrett Wollman 2750a0292f23SGarrett Wollman /* 2751a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 2752a0292f23SGarrett Wollman */ 2753a0292f23SGarrett Wollman int 2754a0292f23SGarrett Wollman tcp_mssopt(tp) 2755a0292f23SGarrett Wollman struct tcpcb *tp; 2756a0292f23SGarrett Wollman { 2757a0292f23SGarrett Wollman struct rtentry *rt; 2758fb59c426SYoshinobu Inoue #ifdef INET6 2759c068736aSJeffrey Hsu int isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2760c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 2761c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 2762c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 2763c068736aSJeffrey Hsu #else 2764c068736aSJeffrey Hsu const int isipv6 = 0; 2765c068736aSJeffrey Hsu const size_t min_protoh = sizeof (struct tcpiphdr); 2766fb59c426SYoshinobu Inoue #endif 2767a0292f23SGarrett Wollman 2768fb59c426SYoshinobu Inoue if (isipv6) 2769be2ac88cSJonathan Lemon rt = tcp_rtlookup6(&tp->t_inpcb->inp_inc); 2770fb59c426SYoshinobu Inoue else 2771be2ac88cSJonathan Lemon rt = tcp_rtlookup(&tp->t_inpcb->inp_inc); 2772a0292f23SGarrett Wollman if (rt == NULL) 2773c068736aSJeffrey Hsu return (isipv6 ? tcp_v6mssdflt : tcp_mssdflt); 2774a0292f23SGarrett Wollman 2775c068736aSJeffrey Hsu return (rt->rt_ifp->if_mtu - min_protoh); 2776df8bae1dSRodney W. Grimes } 277746f58482SJonathan Lemon 277846f58482SJonathan Lemon 277946f58482SJonathan Lemon /* 2780c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 2781c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 2782c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 2783c068736aSJeffrey Hsu * be started again. 278446f58482SJonathan Lemon */ 2785c068736aSJeffrey Hsu static void 2786c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th) 278746f58482SJonathan Lemon struct tcpcb *tp; 278846f58482SJonathan Lemon struct tcphdr *th; 278946f58482SJonathan Lemon { 279046f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 279146f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 279246f58482SJonathan Lemon 279346f58482SJonathan Lemon callout_stop(tp->tt_rexmt); 279446f58482SJonathan Lemon tp->t_rtttime = 0; 279546f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 27966b2a5f92SJayanth Vijayaraghavan /* 2797c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 2798c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 27996b2a5f92SJayanth Vijayaraghavan */ 28006b2a5f92SJayanth Vijayaraghavan tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 2801a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 28026b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 280346f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 280446f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 280546f58482SJonathan Lemon tp->snd_nxt = onxt; 280646f58482SJonathan Lemon /* 280746f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 280846f58482SJonathan Lemon * not updated yet. 280946f58482SJonathan Lemon */ 281046f58482SJonathan Lemon tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg); 281146f58482SJonathan Lemon } 2812340c35deSJonathan Lemon 2813340c35deSJonathan Lemon /* 2814340c35deSJonathan Lemon * Returns 1 if the TIME_WAIT state was killed and we should start over, 2815340c35deSJonathan Lemon * looking for a pcb in the listen state. Returns 0 otherwise. 2816340c35deSJonathan Lemon */ 2817340c35deSJonathan Lemon static int 2818340c35deSJonathan Lemon tcp_timewait(tw, to, th, m, tlen) 2819340c35deSJonathan Lemon struct tcptw *tw; 2820340c35deSJonathan Lemon struct tcpopt *to; 2821340c35deSJonathan Lemon struct tcphdr *th; 2822340c35deSJonathan Lemon struct mbuf *m; 2823340c35deSJonathan Lemon int tlen; 2824340c35deSJonathan Lemon { 2825340c35deSJonathan Lemon int thflags; 2826340c35deSJonathan Lemon tcp_seq seq; 2827340c35deSJonathan Lemon #ifdef INET6 2828340c35deSJonathan Lemon int isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 2829340c35deSJonathan Lemon #else 2830340c35deSJonathan Lemon const int isipv6 = 0; 2831340c35deSJonathan Lemon #endif 2832340c35deSJonathan Lemon 2833340c35deSJonathan Lemon thflags = th->th_flags; 2834340c35deSJonathan Lemon 2835340c35deSJonathan Lemon /* 2836340c35deSJonathan Lemon * NOTE: for FIN_WAIT_2 (to be added later), 2837340c35deSJonathan Lemon * must validate sequence number before accepting RST 2838340c35deSJonathan Lemon */ 2839340c35deSJonathan Lemon 2840340c35deSJonathan Lemon /* 2841340c35deSJonathan Lemon * If the segment contains RST: 2842340c35deSJonathan Lemon * Drop the segment - see Stevens, vol. 2, p. 964 and 2843340c35deSJonathan Lemon * RFC 1337. 2844340c35deSJonathan Lemon */ 2845340c35deSJonathan Lemon if (thflags & TH_RST) 2846340c35deSJonathan Lemon goto drop; 2847340c35deSJonathan Lemon 2848340c35deSJonathan Lemon /* 2849340c35deSJonathan Lemon * If segment contains a SYN and CC [not CC.NEW] option: 2850340c35deSJonathan Lemon * if connection duration > MSL, drop packet and send RST; 2851340c35deSJonathan Lemon * 2852340c35deSJonathan Lemon * if SEG.CC > CCrecv then is new SYN. 2853340c35deSJonathan Lemon * Complete close and delete TCPCB. Then reprocess 2854340c35deSJonathan Lemon * segment, hoping to find new TCPCB in LISTEN state; 2855340c35deSJonathan Lemon * 2856340c35deSJonathan Lemon * else must be old SYN; drop it. 2857340c35deSJonathan Lemon * else do normal processing. 2858340c35deSJonathan Lemon */ 2859340c35deSJonathan Lemon if ((thflags & TH_SYN) && (to->to_flags & TOF_CC) && tw->cc_recv != 0) { 2860340c35deSJonathan Lemon if ((ticks - tw->t_starttime) > tcp_msl) 2861340c35deSJonathan Lemon goto reset; 2862340c35deSJonathan Lemon if (CC_GT(to->to_cc, tw->cc_recv)) { 2863340c35deSJonathan Lemon tcp_twclose(tw); 2864340c35deSJonathan Lemon return (1); 2865340c35deSJonathan Lemon } 2866340c35deSJonathan Lemon goto drop; 2867340c35deSJonathan Lemon } 2868340c35deSJonathan Lemon 2869340c35deSJonathan Lemon #if 0 2870340c35deSJonathan Lemon /* PAWS not needed at the moment */ 2871340c35deSJonathan Lemon /* 2872340c35deSJonathan Lemon * RFC 1323 PAWS: If we have a timestamp reply on this segment 2873340c35deSJonathan Lemon * and it's less than ts_recent, drop it. 2874340c35deSJonathan Lemon */ 2875340c35deSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 2876340c35deSJonathan Lemon TSTMP_LT(to.to_tsval, tp->ts_recent)) { 2877340c35deSJonathan Lemon if ((thflags & TH_ACK) == 0) 2878340c35deSJonathan Lemon goto drop; 2879340c35deSJonathan Lemon goto ack; 2880340c35deSJonathan Lemon } 2881340c35deSJonathan Lemon /* 2882340c35deSJonathan Lemon * ts_recent is never updated because we never accept new segments. 2883340c35deSJonathan Lemon */ 2884340c35deSJonathan Lemon #endif 2885340c35deSJonathan Lemon 2886340c35deSJonathan Lemon /* 2887340c35deSJonathan Lemon * If a new connection request is received 2888340c35deSJonathan Lemon * while in TIME_WAIT, drop the old connection 2889340c35deSJonathan Lemon * and start over if the sequence numbers 2890340c35deSJonathan Lemon * are above the previous ones. 2891340c35deSJonathan Lemon */ 2892340c35deSJonathan Lemon if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) { 2893340c35deSJonathan Lemon tcp_twclose(tw); 2894340c35deSJonathan Lemon return (1); 2895340c35deSJonathan Lemon } 2896340c35deSJonathan Lemon 2897340c35deSJonathan Lemon /* 2898340c35deSJonathan Lemon * Drop the the segment if it does not contain an ACK. 2899340c35deSJonathan Lemon */ 2900340c35deSJonathan Lemon if ((thflags & TH_ACK) == 0) 2901340c35deSJonathan Lemon goto drop; 2902340c35deSJonathan Lemon 2903340c35deSJonathan Lemon /* 2904340c35deSJonathan Lemon * Reset the 2MSL timer if this is a duplicate FIN. 2905340c35deSJonathan Lemon */ 2906340c35deSJonathan Lemon if (thflags & TH_FIN) { 2907340c35deSJonathan Lemon seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0); 2908340c35deSJonathan Lemon if (seq + 1 == tw->rcv_nxt) 2909340c35deSJonathan Lemon callout_reset(tw->tt_2msl, 2910340c35deSJonathan Lemon 2 * tcp_msl, tcp_timer_2msl, tw); 2911340c35deSJonathan Lemon } 2912340c35deSJonathan Lemon 2913340c35deSJonathan Lemon /* 2914340c35deSJonathan Lemon * Acknowlege the segment, then drop it. 2915340c35deSJonathan Lemon */ 2916340c35deSJonathan Lemon tcp_twrespond(tw, TH_ACK); 2917340c35deSJonathan Lemon goto drop; 2918340c35deSJonathan Lemon 2919340c35deSJonathan Lemon reset: 2920340c35deSJonathan Lemon /* 2921340c35deSJonathan Lemon * Generate a RST, dropping incoming segment. 2922340c35deSJonathan Lemon * Make ACK acceptable to originator of segment. 2923340c35deSJonathan Lemon * Don't bother to respond if destination was broadcast/multicast. 2924340c35deSJonathan Lemon */ 2925340c35deSJonathan Lemon if (m->m_flags & (M_BCAST|M_MCAST)) 2926340c35deSJonathan Lemon goto drop; 2927340c35deSJonathan Lemon if (isipv6) { 2928340c35deSJonathan Lemon struct ip6_hdr *ip6; 2929340c35deSJonathan Lemon 2930340c35deSJonathan Lemon /* IPv6 anycast check is done at tcp6_input() */ 2931340c35deSJonathan Lemon ip6 = mtod(m, struct ip6_hdr *); 2932340c35deSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2933340c35deSJonathan Lemon IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2934340c35deSJonathan Lemon goto drop; 2935340c35deSJonathan Lemon } else { 2936340c35deSJonathan Lemon struct ip *ip; 2937340c35deSJonathan Lemon 2938340c35deSJonathan Lemon ip = mtod(m, struct ip *); 2939340c35deSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2940340c35deSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2941340c35deSJonathan Lemon ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 2942340c35deSJonathan Lemon in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2943340c35deSJonathan Lemon goto drop; 2944340c35deSJonathan Lemon } 2945340c35deSJonathan Lemon if (thflags & TH_ACK) { 2946340c35deSJonathan Lemon tcp_respond(NULL, 2947340c35deSJonathan Lemon mtod(m, void *), th, m, 0, th->th_ack, TH_RST); 2948340c35deSJonathan Lemon } else { 2949340c35deSJonathan Lemon seq = th->th_seq + (thflags & TH_SYN ? 1 : 0); 2950340c35deSJonathan Lemon tcp_respond(NULL, 2951340c35deSJonathan Lemon mtod(m, void *), th, m, seq, 0, TH_RST|TH_ACK); 2952340c35deSJonathan Lemon } 2953340c35deSJonathan Lemon INP_UNLOCK(tw->tw_inpcb); 2954340c35deSJonathan Lemon return (0); 2955340c35deSJonathan Lemon 2956340c35deSJonathan Lemon drop: 2957340c35deSJonathan Lemon INP_UNLOCK(tw->tw_inpcb); 2958340c35deSJonathan Lemon m_freem(m); 2959340c35deSJonathan Lemon return (0); 2960340c35deSJonathan Lemon } 2961