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 128582a954bSJeffrey Hsu static int tcp_do_rfc3042 = 0; 129582a954bSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW, 130582a954bSJeffrey Hsu &tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)"); 131582a954bSJeffrey Hsu 132da3a8a1aSJeffrey Hsu static int tcp_do_rfc3390 = 0; 133da3a8a1aSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW, 134da3a8a1aSJeffrey Hsu &tcp_do_rfc3390, 0, 135da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 136da3a8a1aSJeffrey Hsu 13715bd2b43SDavid Greenman struct inpcbhead tcb; 138fb59c426SYoshinobu Inoue #define tcb6 tcb /* for KAME src sync over BSD*'s */ 13915bd2b43SDavid Greenman struct inpcbinfo tcbinfo; 140f76fcf6dSJeffrey Hsu struct mtx *tcbinfo_mtx; 141df8bae1dSRodney W. Grimes 1424d77a549SAlfred Perlstein static void tcp_dooptions(struct tcpopt *, u_char *, int, int); 1434d77a549SAlfred Perlstein static void tcp_pulloutofband(struct socket *, 1444d77a549SAlfred Perlstein struct tcphdr *, struct mbuf *, int); 1454d77a549SAlfred Perlstein static int tcp_reass(struct tcpcb *, struct tcphdr *, int *, 1464d77a549SAlfred Perlstein struct mbuf *); 1474d77a549SAlfred Perlstein static void tcp_xmit_timer(struct tcpcb *, int); 148c068736aSJeffrey Hsu static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 149340c35deSJonathan Lemon static int tcp_timewait(struct tcptw *, struct tcpopt *, 150340c35deSJonathan Lemon struct tcphdr *, struct mbuf *, int); 1510312fbe9SPoul-Henning Kamp 152fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 153fb59c426SYoshinobu Inoue #ifdef INET6 154fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \ 155fb59c426SYoshinobu Inoue do { \ 156fb59c426SYoshinobu Inoue if ((tp) && (tp)->t_inpcb && \ 157fb59c426SYoshinobu Inoue ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0 && \ 158fb59c426SYoshinobu Inoue (tp)->t_inpcb->in6p_route.ro_rt) \ 159686cdd19SJun-ichiro itojun Hagino nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL, 0); \ 160fb59c426SYoshinobu Inoue } while (0) 161fb59c426SYoshinobu Inoue #else 162fb59c426SYoshinobu Inoue #define ND6_HINT(tp) 163fb59c426SYoshinobu Inoue #endif 164df8bae1dSRodney W. Grimes 165df8bae1dSRodney W. Grimes /* 166262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 167262c1c1aSMatthew Dillon * - there is no delayed ack timer in progress and 168262c1c1aSMatthew Dillon * - our last ack wasn't a 0-sized window. We never want to delay 1693bfd6421SJonathan Lemon * the ack that opens up a 0-sized window and 1703bfd6421SJonathan Lemon * - delayed acks are enabled or 1713bfd6421SJonathan Lemon * - this is a half-synchronized T/TCP connection. 172d8c85a26SJonathan Lemon */ 173d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \ 174a14c749fSJonathan Lemon ((!callout_active(tp->tt_delack) && \ 175a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 1763bfd6421SJonathan Lemon (tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 177d8c85a26SJonathan Lemon 1780312fbe9SPoul-Henning Kamp static int 179fb59c426SYoshinobu Inoue tcp_reass(tp, th, tlenp, m) 180df8bae1dSRodney W. Grimes register struct tcpcb *tp; 181fb59c426SYoshinobu Inoue register struct tcphdr *th; 182fb59c426SYoshinobu Inoue int *tlenp; 183df8bae1dSRodney W. Grimes struct mbuf *m; 184df8bae1dSRodney W. Grimes { 185fb59c426SYoshinobu Inoue struct tseg_qent *q; 186fb59c426SYoshinobu Inoue struct tseg_qent *p = NULL; 187fb59c426SYoshinobu Inoue struct tseg_qent *nq; 188fb59c426SYoshinobu Inoue struct tseg_qent *te; 189df8bae1dSRodney W. Grimes struct socket *so = tp->t_inpcb->inp_socket; 190df8bae1dSRodney W. Grimes int flags; 191df8bae1dSRodney W. Grimes 192df8bae1dSRodney W. Grimes /* 193fb59c426SYoshinobu Inoue * Call with th==0 after become established to 194df8bae1dSRodney W. Grimes * force pre-ESTABLISHED data up to user socket. 195df8bae1dSRodney W. Grimes */ 196fb59c426SYoshinobu Inoue if (th == 0) 197df8bae1dSRodney W. Grimes goto present; 198df8bae1dSRodney W. Grimes 199fb59c426SYoshinobu Inoue /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */ 200fb59c426SYoshinobu Inoue MALLOC(te, struct tseg_qent *, sizeof (struct tseg_qent), M_TSEGQ, 201fb59c426SYoshinobu Inoue M_NOWAIT); 202fb59c426SYoshinobu Inoue if (te == NULL) { 203fb59c426SYoshinobu Inoue tcpstat.tcps_rcvmemdrop++; 204fb59c426SYoshinobu Inoue m_freem(m); 205fb59c426SYoshinobu Inoue return (0); 206fb59c426SYoshinobu Inoue } 2076effc713SDoug Rabson 208df8bae1dSRodney W. Grimes /* 209df8bae1dSRodney W. Grimes * Find a segment which begins after this one does. 210df8bae1dSRodney W. Grimes */ 211fb59c426SYoshinobu Inoue LIST_FOREACH(q, &tp->t_segq, tqe_q) { 212fb59c426SYoshinobu Inoue if (SEQ_GT(q->tqe_th->th_seq, th->th_seq)) 213df8bae1dSRodney W. Grimes break; 214fb59c426SYoshinobu Inoue p = q; 215fb59c426SYoshinobu Inoue } 216df8bae1dSRodney W. Grimes 217df8bae1dSRodney W. Grimes /* 218df8bae1dSRodney W. Grimes * If there is a preceding segment, it may provide some of 219df8bae1dSRodney W. Grimes * our data already. If so, drop the data from the incoming 220df8bae1dSRodney W. Grimes * segment. If it provides all of our data, drop us. 221df8bae1dSRodney W. Grimes */ 2226effc713SDoug Rabson if (p != NULL) { 223df8bae1dSRodney W. Grimes register int i; 224df8bae1dSRodney W. Grimes /* conversion to int (in i) handles seq wraparound */ 225fb59c426SYoshinobu Inoue i = p->tqe_th->th_seq + p->tqe_len - th->th_seq; 226df8bae1dSRodney W. Grimes if (i > 0) { 227fb59c426SYoshinobu Inoue if (i >= *tlenp) { 228df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 229fb59c426SYoshinobu Inoue tcpstat.tcps_rcvdupbyte += *tlenp; 230df8bae1dSRodney W. Grimes m_freem(m); 231fb59c426SYoshinobu Inoue FREE(te, M_TSEGQ); 232a0292f23SGarrett Wollman /* 233a0292f23SGarrett Wollman * Try to present any queued data 234a0292f23SGarrett Wollman * at the left window edge to the user. 235a0292f23SGarrett Wollman * This is needed after the 3-WHS 236a0292f23SGarrett Wollman * completes. 237a0292f23SGarrett Wollman */ 238a0292f23SGarrett Wollman goto present; /* ??? */ 239df8bae1dSRodney W. Grimes } 240df8bae1dSRodney W. Grimes m_adj(m, i); 241fb59c426SYoshinobu Inoue *tlenp -= i; 242fb59c426SYoshinobu Inoue th->th_seq += i; 243df8bae1dSRodney W. Grimes } 244df8bae1dSRodney W. Grimes } 245df8bae1dSRodney W. Grimes tcpstat.tcps_rcvoopack++; 246fb59c426SYoshinobu Inoue tcpstat.tcps_rcvoobyte += *tlenp; 247df8bae1dSRodney W. Grimes 248df8bae1dSRodney W. Grimes /* 249df8bae1dSRodney W. Grimes * While we overlap succeeding segments trim them or, 250df8bae1dSRodney W. Grimes * if they are completely covered, dequeue them. 251df8bae1dSRodney W. Grimes */ 2526effc713SDoug Rabson while (q) { 253fb59c426SYoshinobu Inoue register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq; 254df8bae1dSRodney W. Grimes if (i <= 0) 255df8bae1dSRodney W. Grimes break; 256fb59c426SYoshinobu Inoue if (i < q->tqe_len) { 257fb59c426SYoshinobu Inoue q->tqe_th->th_seq += i; 258fb59c426SYoshinobu Inoue q->tqe_len -= i; 259fb59c426SYoshinobu Inoue m_adj(q->tqe_m, i); 260df8bae1dSRodney W. Grimes break; 261df8bae1dSRodney W. Grimes } 2626effc713SDoug Rabson 263fb59c426SYoshinobu Inoue nq = LIST_NEXT(q, tqe_q); 264fb59c426SYoshinobu Inoue LIST_REMOVE(q, tqe_q); 265fb59c426SYoshinobu Inoue m_freem(q->tqe_m); 266fb59c426SYoshinobu Inoue FREE(q, M_TSEGQ); 2676effc713SDoug Rabson q = nq; 268df8bae1dSRodney W. Grimes } 269df8bae1dSRodney W. Grimes 270fb59c426SYoshinobu Inoue /* Insert the new segment queue entry into place. */ 271fb59c426SYoshinobu Inoue te->tqe_m = m; 272fb59c426SYoshinobu Inoue te->tqe_th = th; 273fb59c426SYoshinobu Inoue te->tqe_len = *tlenp; 274fb59c426SYoshinobu Inoue 2756effc713SDoug Rabson if (p == NULL) { 276fb59c426SYoshinobu Inoue LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q); 2776effc713SDoug Rabson } else { 278fb59c426SYoshinobu Inoue LIST_INSERT_AFTER(p, te, tqe_q); 2796effc713SDoug Rabson } 280df8bae1dSRodney W. Grimes 281df8bae1dSRodney W. Grimes present: 282df8bae1dSRodney W. Grimes /* 283df8bae1dSRodney W. Grimes * Present data to user, advancing rcv_nxt through 284df8bae1dSRodney W. Grimes * completed sequence space. 285df8bae1dSRodney W. Grimes */ 286a0292f23SGarrett Wollman if (!TCPS_HAVEESTABLISHED(tp->t_state)) 287df8bae1dSRodney W. Grimes return (0); 288fb59c426SYoshinobu Inoue q = LIST_FIRST(&tp->t_segq); 289fb59c426SYoshinobu Inoue if (!q || q->tqe_th->th_seq != tp->rcv_nxt) 290df8bae1dSRodney W. Grimes return (0); 291df8bae1dSRodney W. Grimes do { 292fb59c426SYoshinobu Inoue tp->rcv_nxt += q->tqe_len; 293fb59c426SYoshinobu Inoue flags = q->tqe_th->th_flags & TH_FIN; 294fb59c426SYoshinobu Inoue nq = LIST_NEXT(q, tqe_q); 295fb59c426SYoshinobu Inoue LIST_REMOVE(q, tqe_q); 2964cc20ab1SSeigo Tanimura if (so->so_state & SS_CANTRCVMORE) 297fb59c426SYoshinobu Inoue m_freem(q->tqe_m); 2984cc20ab1SSeigo Tanimura else 299fb59c426SYoshinobu Inoue sbappend(&so->so_rcv, q->tqe_m); 300fb59c426SYoshinobu Inoue FREE(q, M_TSEGQ); 3016effc713SDoug Rabson q = nq; 302fb59c426SYoshinobu Inoue } while (q && q->tqe_th->th_seq == tp->rcv_nxt); 303fb59c426SYoshinobu Inoue ND6_HINT(tp); 304df8bae1dSRodney W. Grimes sorwakeup(so); 305df8bae1dSRodney W. Grimes return (flags); 306df8bae1dSRodney W. Grimes } 307df8bae1dSRodney W. Grimes 308df8bae1dSRodney W. Grimes /* 309df8bae1dSRodney W. Grimes * TCP input routine, follows pages 65-76 of the 310df8bae1dSRodney W. Grimes * protocol specification dated September, 1981 very closely. 311df8bae1dSRodney W. Grimes */ 312fb59c426SYoshinobu Inoue #ifdef INET6 313fb59c426SYoshinobu Inoue int 314fb59c426SYoshinobu Inoue tcp6_input(mp, offp, proto) 315fb59c426SYoshinobu Inoue struct mbuf **mp; 316fb59c426SYoshinobu Inoue int *offp, proto; 317fb59c426SYoshinobu Inoue { 318fb59c426SYoshinobu Inoue register struct mbuf *m = *mp; 31933841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 320fb59c426SYoshinobu Inoue 321fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 322fb59c426SYoshinobu Inoue 323fb59c426SYoshinobu Inoue /* 324fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 325fb59c426SYoshinobu Inoue * better place to put this in? 326fb59c426SYoshinobu Inoue */ 32733841545SHajimu UMEMOTO ia6 = ip6_getdstifaddr(m); 32833841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 329fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 330fb59c426SYoshinobu Inoue 331fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 332fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 333fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 334fb59c426SYoshinobu Inoue return IPPROTO_DONE; 335fb59c426SYoshinobu Inoue } 336fb59c426SYoshinobu Inoue 337f0ffb944SJulian Elischer tcp_input(m, *offp); 338fb59c426SYoshinobu Inoue return IPPROTO_DONE; 339fb59c426SYoshinobu Inoue } 340fb59c426SYoshinobu Inoue #endif 341fb59c426SYoshinobu Inoue 342df8bae1dSRodney W. Grimes void 343f0ffb944SJulian Elischer tcp_input(m, off0) 344df8bae1dSRodney W. Grimes register struct mbuf *m; 345f0ffb944SJulian Elischer int off0; 346df8bae1dSRodney W. Grimes { 347fb59c426SYoshinobu Inoue register struct tcphdr *th; 348fb59c426SYoshinobu Inoue register struct ip *ip = NULL; 349fb59c426SYoshinobu Inoue register struct ipovly *ipov; 350f76fcf6dSJeffrey Hsu register struct inpcb *inp = NULL; 351e79adb8eSGarrett Wollman u_char *optp = NULL; 35226f9a767SRodney W. Grimes int optlen = 0; 353df8bae1dSRodney W. Grimes int len, tlen, off; 354fb59c426SYoshinobu Inoue int drop_hdrlen; 355df8bae1dSRodney W. Grimes register struct tcpcb *tp = 0; 356fb59c426SYoshinobu Inoue register int thflags; 35726f9a767SRodney W. Grimes struct socket *so = 0; 358df8bae1dSRodney W. Grimes int todrop, acked, ourfinisacked, needoutput = 0; 359a0292f23SGarrett Wollman u_long tiwin; 360a0292f23SGarrett Wollman struct tcpopt to; /* options in this segment */ 361a0292f23SGarrett Wollman struct rmxp_tao *taop; /* pointer to our TAO cache entry */ 362a0292f23SGarrett Wollman struct rmxp_tao tao_noncached; /* in case there's no cached entry */ 363f76fcf6dSJeffrey Hsu int headlocked = 0; 364c068736aSJeffrey Hsu struct sockaddr_in *next_hop = NULL; 365c068736aSJeffrey Hsu int rstreason; /* For badport_bandlim accounting purposes */ 366c068736aSJeffrey Hsu 367c068736aSJeffrey Hsu struct ip6_hdr *ip6 = NULL; 368c068736aSJeffrey Hsu #ifdef INET6 369c068736aSJeffrey Hsu int isipv6; 370c068736aSJeffrey Hsu #else 371c068736aSJeffrey Hsu const int isipv6 = 0; 372c068736aSJeffrey Hsu #endif 373f76fcf6dSJeffrey Hsu 374610ee2f9SDavid Greenman #ifdef TCPDEBUG 375c068736aSJeffrey Hsu /* 376c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 377c068736aSJeffrey Hsu * now IPv6. 378c068736aSJeffrey Hsu */ 379410bb1bfSLuigi Rizzo u_char tcp_saveipgen[40]; 380410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 381610ee2f9SDavid Greenman short ostate = 0; 382610ee2f9SDavid Greenman #endif 383c068736aSJeffrey Hsu 3842b25acc1SLuigi Rizzo /* Grab info from MT_TAG mbufs prepended to the chain. */ 3852b25acc1SLuigi Rizzo for (;m && m->m_type == MT_TAG; m = m->m_next) { 3865d846453SSam Leffler if (m->_m_tag_id == PACKET_TAG_IPFORWARD) 3872b25acc1SLuigi Rizzo next_hop = (struct sockaddr_in *)m->m_hdr.mh_data; 3882b25acc1SLuigi Rizzo } 389fb59c426SYoshinobu Inoue #ifdef INET6 390fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 391fb59c426SYoshinobu Inoue #endif 392a0292f23SGarrett Wollman bzero((char *)&to, sizeof(to)); 393a0292f23SGarrett Wollman 394df8bae1dSRodney W. Grimes tcpstat.tcps_rcvtotal++; 395fb59c426SYoshinobu Inoue 396fb59c426SYoshinobu Inoue if (isipv6) { 397fb59c426SYoshinobu Inoue /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */ 398fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 399fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 400fb59c426SYoshinobu Inoue if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 401fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbadsum++; 402fb59c426SYoshinobu Inoue goto drop; 403fb59c426SYoshinobu Inoue } 404fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 40533841545SHajimu UMEMOTO 40633841545SHajimu UMEMOTO /* 40733841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 40833841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 40933841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 41033841545SHajimu UMEMOTO * 41133841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 41233841545SHajimu UMEMOTO * already dropped in ip6_input. 41333841545SHajimu UMEMOTO */ 41433841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 41533841545SHajimu UMEMOTO /* XXX stat */ 41633841545SHajimu UMEMOTO goto drop; 41733841545SHajimu UMEMOTO } 418c068736aSJeffrey Hsu } else { 419df8bae1dSRodney W. Grimes /* 420df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 421df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 422df8bae1dSRodney W. Grimes */ 423fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 424df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 425fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 426fb59c426SYoshinobu Inoue } 427df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 428df8bae1dSRodney W. Grimes if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 429df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 430df8bae1dSRodney W. Grimes return; 431df8bae1dSRodney W. Grimes } 432df8bae1dSRodney W. Grimes } 433fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 434fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 435db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 436db4f9cc7SJonathan Lemon tlen = ip->ip_len; 437df8bae1dSRodney W. Grimes 438db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 439db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 440db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 441db4f9cc7SJonathan Lemon else 442db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 443c068736aSJeffrey Hsu ip->ip_dst.s_addr, 444c068736aSJeffrey Hsu htonl(m->m_pkthdr.csum_data + 445c068736aSJeffrey Hsu ip->ip_len + 446c068736aSJeffrey Hsu IPPROTO_TCP)); 447db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 448db4f9cc7SJonathan Lemon } else { 449df8bae1dSRodney W. Grimes /* 450df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 451df8bae1dSRodney W. Grimes */ 452df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 453fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 454fb59c426SYoshinobu Inoue ipov->ih_len = (u_short)tlen; 455fd8e4ebcSMike Barcroft ipov->ih_len = htons(ipov->ih_len); 456fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 457db4f9cc7SJonathan Lemon } 458fb59c426SYoshinobu Inoue if (th->th_sum) { 459df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadsum++; 460df8bae1dSRodney W. Grimes goto drop; 461df8bae1dSRodney W. Grimes } 462fb59c426SYoshinobu Inoue #ifdef INET6 463fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 464fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 465fb59c426SYoshinobu Inoue #endif 466fb59c426SYoshinobu Inoue } 467df8bae1dSRodney W. Grimes 468df8bae1dSRodney W. Grimes /* 469df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 470df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 471df8bae1dSRodney W. Grimes */ 472fb59c426SYoshinobu Inoue off = th->th_off << 2; 473df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 474df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadoff++; 475df8bae1dSRodney W. Grimes goto drop; 476df8bae1dSRodney W. Grimes } 477fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 478df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 479fb59c426SYoshinobu Inoue if (isipv6) { 480fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 481fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 482fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 483c068736aSJeffrey Hsu } else { 484df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 485c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 486c068736aSJeffrey Hsu == 0) { 487df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 488df8bae1dSRodney W. Grimes return; 489df8bae1dSRodney W. Grimes } 490fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 491fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 492fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 493fb59c426SYoshinobu Inoue } 494df8bae1dSRodney W. Grimes } 495df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 496fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 497df8bae1dSRodney W. Grimes } 498fb59c426SYoshinobu Inoue thflags = th->th_flags; 499df8bae1dSRodney W. Grimes 500e46cd3d4SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN 501e46cd3d4SDag-Erling Smørgrav /* 502e46cd3d4SDag-Erling Smørgrav * If the drop_synfin option is enabled, drop all packets with 503e46cd3d4SDag-Erling Smørgrav * both the SYN and FIN bits set. This prevents e.g. nmap from 504e46cd3d4SDag-Erling Smørgrav * identifying the TCP/IP stack. 505e46cd3d4SDag-Erling Smørgrav * 506a589a70eSGarrett Wollman * This is a violation of the TCP specification. 507e46cd3d4SDag-Erling Smørgrav */ 508fb59c426SYoshinobu Inoue if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN)) 509e46cd3d4SDag-Erling Smørgrav goto drop; 510e46cd3d4SDag-Erling Smørgrav #endif 511e46cd3d4SDag-Erling Smørgrav 512df8bae1dSRodney W. Grimes /* 513df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 514df8bae1dSRodney W. Grimes */ 515fd8e4ebcSMike Barcroft th->th_seq = ntohl(th->th_seq); 516fd8e4ebcSMike Barcroft th->th_ack = ntohl(th->th_ack); 517fd8e4ebcSMike Barcroft th->th_win = ntohs(th->th_win); 518fd8e4ebcSMike Barcroft th->th_urp = ntohs(th->th_urp); 519df8bae1dSRodney W. Grimes 520df8bae1dSRodney W. Grimes /* 5213bfd6421SJonathan Lemon * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options, 522fb59c426SYoshinobu Inoue * until after ip6_savecontrol() is called and before other functions 523fb59c426SYoshinobu Inoue * which don't want those proto headers. 524fb59c426SYoshinobu Inoue * Because ip6_savecontrol() is going to parse the mbuf to 525fb59c426SYoshinobu Inoue * search for data to be passed up to user-land, it wants mbuf 526fb59c426SYoshinobu Inoue * parameters to be unchanged. 52788ff5695SSUZUKI Shinsuke * XXX: the call of ip6_savecontrol() has been obsoleted based on 52888ff5695SSUZUKI Shinsuke * latest version of the advanced API (20020110). 529755c1f07SAndras Olah */ 530fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 531755c1f07SAndras Olah 532755c1f07SAndras Olah /* 533df8bae1dSRodney W. Grimes * Locate pcb for segment. 534df8bae1dSRodney W. Grimes */ 535f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 536f76fcf6dSJeffrey Hsu headlocked = 1; 537df8bae1dSRodney W. Grimes findpcb: 5382b25acc1SLuigi Rizzo /* IPFIREWALL_FORWARD section */ 539c068736aSJeffrey Hsu if (next_hop != NULL && isipv6 == 0) { /* IPv6 support is not yet */ 540f9e354dfSJulian Elischer /* 5412b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 542f9e354dfSJulian Elischer * already got one like this? 543f9e354dfSJulian Elischer */ 544fb59c426SYoshinobu Inoue inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, 545c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 546c068736aSJeffrey Hsu 0, m->m_pkthdr.rcvif); 547f9e354dfSJulian Elischer if (!inp) { 548c068736aSJeffrey Hsu /* It's new. Try find the ambushing socket. */ 549f9e354dfSJulian Elischer inp = in_pcblookup_hash(&tcbinfo, 550fb59c426SYoshinobu Inoue ip->ip_src, th->th_sport, 5512b25acc1SLuigi Rizzo next_hop->sin_addr, 552c068736aSJeffrey Hsu next_hop->sin_port ? 553c068736aSJeffrey Hsu ntohs(next_hop->sin_port) : 554c068736aSJeffrey Hsu th->th_dport, 555c068736aSJeffrey Hsu 1, m->m_pkthdr.rcvif); 556f9e354dfSJulian Elischer } 557c068736aSJeffrey Hsu } else { 558fb59c426SYoshinobu Inoue if (isipv6) 559c068736aSJeffrey Hsu inp = in6_pcblookup_hash(&tcbinfo, 560c068736aSJeffrey Hsu &ip6->ip6_src, th->th_sport, 561c068736aSJeffrey Hsu &ip6->ip6_dst, th->th_dport, 562c068736aSJeffrey Hsu 1, m->m_pkthdr.rcvif); 563fb59c426SYoshinobu Inoue else 564c068736aSJeffrey Hsu inp = in_pcblookup_hash(&tcbinfo, 565c068736aSJeffrey Hsu ip->ip_src, th->th_sport, 566c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 567c068736aSJeffrey Hsu 1, m->m_pkthdr.rcvif); 568fb59c426SYoshinobu Inoue } 569f9e354dfSJulian Elischer 570fb59c426SYoshinobu Inoue #ifdef IPSEC 571fb59c426SYoshinobu Inoue if (isipv6) { 572fb59c426SYoshinobu Inoue if (inp != NULL && ipsec6_in_reject_so(m, inp->inp_socket)) { 573fb59c426SYoshinobu Inoue ipsec6stat.in_polvio++; 574fb59c426SYoshinobu Inoue goto drop; 575fb59c426SYoshinobu Inoue } 576c068736aSJeffrey Hsu } else { 577fb59c426SYoshinobu Inoue if (inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) { 578fb59c426SYoshinobu Inoue ipsecstat.in_polvio++; 579fb59c426SYoshinobu Inoue goto drop; 580fb59c426SYoshinobu Inoue } 581c068736aSJeffrey Hsu } 582c068736aSJeffrey Hsu #endif 583b9234fafSSam Leffler #ifdef FAST_IPSEC 584b9234fafSSam Leffler if (isipv6) { 585b9234fafSSam Leffler if (inp != NULL && ipsec6_in_reject(m, inp)) { 586b9234fafSSam Leffler goto drop; 587b9234fafSSam Leffler } 58858fcadfcSSam Leffler } else 58958fcadfcSSam Leffler if (inp != NULL && ipsec4_in_reject(m, inp)) { 590b9234fafSSam Leffler goto drop; 591b9234fafSSam Leffler } 592b9234fafSSam Leffler #endif /*FAST_IPSEC*/ 593df8bae1dSRodney W. Grimes 594df8bae1dSRodney W. Grimes /* 595df8bae1dSRodney W. Grimes * If the state is CLOSED (i.e., TCB does not exist) then 596df8bae1dSRodney W. Grimes * all data in the incoming segment is discarded. 597df8bae1dSRodney W. Grimes * If the TCB exists but is in CLOSED state, it is embryonic, 598df8bae1dSRodney W. Grimes * but should either do a listen or a connect soon. 599df8bae1dSRodney W. Grimes */ 600816a3d83SPoul-Henning Kamp if (inp == NULL) { 6012e4e1b4cSGeoff Rehmet if (log_in_vain) { 602fb59c426SYoshinobu Inoue #ifdef INET6 603ded7008aSJuli Mallett char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2]; 604c068736aSJeffrey Hsu #else 605fb59c426SYoshinobu Inoue char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"]; 606c068736aSJeffrey Hsu #endif 60775cfc95fSAndrey A. Chernov 608fb59c426SYoshinobu Inoue if (isipv6) { 609ded7008aSJuli Mallett strcpy(dbuf, "["); 610ded7008aSJuli Mallett strcpy(sbuf, "["); 611ded7008aSJuli Mallett strcat(dbuf, ip6_sprintf(&ip6->ip6_dst)); 612ded7008aSJuli Mallett strcat(sbuf, ip6_sprintf(&ip6->ip6_src)); 613ded7008aSJuli Mallett strcat(dbuf, "]"); 614ded7008aSJuli Mallett strcat(sbuf, "]"); 615c068736aSJeffrey Hsu } else { 616fb59c426SYoshinobu Inoue strcpy(dbuf, inet_ntoa(ip->ip_dst)); 617fb59c426SYoshinobu Inoue strcpy(sbuf, inet_ntoa(ip->ip_src)); 618fb59c426SYoshinobu Inoue } 6192e4e1b4cSGeoff Rehmet switch (log_in_vain) { 6202e4e1b4cSGeoff Rehmet case 1: 62139eb27a4SCrist J. Clark if ((thflags & TH_SYN) == 0) 6222e4e1b4cSGeoff Rehmet break; 623e4d2978dSPoul-Henning Kamp /* FALLTHROUGH */ 6242e4e1b4cSGeoff Rehmet case 2: 6252e4e1b4cSGeoff Rehmet log(LOG_INFO, 626c068736aSJeffrey Hsu "Connection attempt to TCP %s:%d " 62739eb27a4SCrist J. Clark "from %s:%d flags:0x%02x\n", 628fb59c426SYoshinobu Inoue dbuf, ntohs(th->th_dport), sbuf, 629fb59c426SYoshinobu Inoue ntohs(th->th_sport), thflags); 6302e4e1b4cSGeoff Rehmet break; 6312e4e1b4cSGeoff Rehmet default: 6322e4e1b4cSGeoff Rehmet break; 6332e4e1b4cSGeoff Rehmet } 63475cfc95fSAndrey A. Chernov } 6352e4e1b4cSGeoff Rehmet if (blackhole) { 6362e4e1b4cSGeoff Rehmet switch (blackhole) { 637828b7f40SGeoff Rehmet case 1: 638fb59c426SYoshinobu Inoue if (thflags & TH_SYN) 639828b7f40SGeoff Rehmet goto drop; 640828b7f40SGeoff Rehmet break; 641828b7f40SGeoff Rehmet case 2: 642828b7f40SGeoff Rehmet goto drop; 643828b7f40SGeoff Rehmet default: 644828b7f40SGeoff Rehmet goto drop; 6452e4e1b4cSGeoff Rehmet } 646828b7f40SGeoff Rehmet } 647a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 648a57815efSBosko Milekic goto dropwithreset; 649816a3d83SPoul-Henning Kamp } 650f76fcf6dSJeffrey Hsu INP_LOCK(inp); 651340c35deSJonathan Lemon if (inp->inp_vflag & INP_TIMEWAIT) { 652340c35deSJonathan Lemon /* 653340c35deSJonathan Lemon * The only option of relevance is TOF_CC, and only if 654340c35deSJonathan Lemon * present in a SYN segment. See tcp_timewait(). 655340c35deSJonathan Lemon */ 656340c35deSJonathan Lemon if (thflags & TH_SYN) 657340c35deSJonathan Lemon tcp_dooptions(&to, optp, optlen, 1); 658340c35deSJonathan Lemon if (tcp_timewait((struct tcptw *)inp->inp_ppcb, 659340c35deSJonathan Lemon &to, th, m, tlen)) 660340c35deSJonathan Lemon goto findpcb; 661340c35deSJonathan Lemon /* 662340c35deSJonathan Lemon * tcp_timewait unlocks inp. 663340c35deSJonathan Lemon */ 664340c35deSJonathan Lemon INP_INFO_WUNLOCK(&tcbinfo); 665340c35deSJonathan Lemon return; 666340c35deSJonathan Lemon } 667df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 66809f81a46SBosko Milekic if (tp == 0) { 669f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 670a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 671a57815efSBosko Milekic goto dropwithreset; 67209f81a46SBosko Milekic } 673df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_CLOSED) 674df8bae1dSRodney W. Grimes goto drop; 675df8bae1dSRodney W. Grimes 676df8bae1dSRodney W. Grimes /* Unscale the window into a 32-bit value. */ 677fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) == 0) 678fb59c426SYoshinobu Inoue tiwin = th->th_win << tp->snd_scale; 679df8bae1dSRodney W. Grimes else 680fb59c426SYoshinobu Inoue tiwin = th->th_win; 681fb59c426SYoshinobu Inoue 682df8bae1dSRodney W. Grimes so = inp->inp_socket; 683c488362eSRobert Watson #ifdef MAC 684540e8b7eSJeffrey Hsu if (mac_check_socket_deliver(so, m)) 685c488362eSRobert Watson goto drop; 686c488362eSRobert Watson #endif 687610ee2f9SDavid Greenman #ifdef TCPDEBUG 688df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 689df8bae1dSRodney W. Grimes ostate = tp->t_state; 690fb59c426SYoshinobu Inoue if (isipv6) 691540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 692fb59c426SYoshinobu Inoue else 693540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 694fb59c426SYoshinobu Inoue tcp_savetcp = *th; 695df8bae1dSRodney W. Grimes } 696610ee2f9SDavid Greenman #endif 697540e8b7eSJeffrey Hsu if (so->so_options & SO_ACCEPTCONN) { 698540e8b7eSJeffrey Hsu struct in_conninfo inc; 699540e8b7eSJeffrey Hsu 700fb59c426SYoshinobu Inoue #ifdef INET6 701be2ac88cSJonathan Lemon inc.inc_isipv6 = isipv6; 702c068736aSJeffrey Hsu #endif 703be2ac88cSJonathan Lemon if (isipv6) { 704be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 705be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 706be2ac88cSJonathan Lemon inc.inc6_route.ro_rt = NULL; /* XXX */ 707c068736aSJeffrey Hsu } else { 708be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 709be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 710be2ac88cSJonathan Lemon inc.inc_route.ro_rt = NULL; /* XXX */ 711be2ac88cSJonathan Lemon } 712be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 713be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 714be2ac88cSJonathan Lemon 715fb59c426SYoshinobu Inoue /* 716be2ac88cSJonathan Lemon * If the state is LISTEN then ignore segment if it contains 717be2ac88cSJonathan Lemon * a RST. If the segment contains an ACK then it is bad and 718be2ac88cSJonathan Lemon * send a RST. If it does not contain a SYN then it is not 719be2ac88cSJonathan Lemon * interesting; drop it. 720be2ac88cSJonathan Lemon * 721be2ac88cSJonathan Lemon * If the state is SYN_RECEIVED (syncache) and seg contains 722be2ac88cSJonathan Lemon * an ACK, but not for our SYN/ACK, send a RST. If the seg 723be2ac88cSJonathan Lemon * contains a RST, check the sequence number to see if it 724be2ac88cSJonathan Lemon * is a valid reset segment. 725fb59c426SYoshinobu Inoue */ 726fb59c426SYoshinobu Inoue if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 727be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 728be2ac88cSJonathan Lemon if (!syncache_expand(&inc, th, &so, m)) { 7294195b4afSPaul Traina /* 730be2ac88cSJonathan Lemon * No syncache entry, or ACK was not 731be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 7324195b4afSPaul Traina */ 733be2ac88cSJonathan Lemon tcpstat.tcps_badsyn++; 734be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 735be2ac88cSJonathan Lemon goto dropwithreset; 736be2ac88cSJonathan Lemon } 737f76fcf6dSJeffrey Hsu if (so == NULL) { 738be2ac88cSJonathan Lemon /* 739be2ac88cSJonathan Lemon * Could not complete 3-way handshake, 740be2ac88cSJonathan Lemon * connection is being closed down, and 741be2ac88cSJonathan Lemon * syncache will free mbuf. 742be2ac88cSJonathan Lemon */ 743f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 744f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 745be2ac88cSJonathan Lemon return; 746f76fcf6dSJeffrey Hsu } 747be2ac88cSJonathan Lemon /* 748be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 749be2ac88cSJonathan Lemon * Continue processing segment. 750be2ac88cSJonathan Lemon */ 751f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 752be2ac88cSJonathan Lemon inp = sotoinpcb(so); 753f76fcf6dSJeffrey Hsu INP_LOCK(inp); 754be2ac88cSJonathan Lemon tp = intotcpcb(inp); 755be2ac88cSJonathan Lemon /* 756be2ac88cSJonathan Lemon * This is what would have happened in 757e6658b12SRobert Watson * tcp_output() when the SYN,ACK was sent. 758be2ac88cSJonathan Lemon */ 759be2ac88cSJonathan Lemon tp->snd_up = tp->snd_una; 760be2ac88cSJonathan Lemon tp->snd_max = tp->snd_nxt = tp->iss + 1; 761be2ac88cSJonathan Lemon tp->last_ack_sent = tp->rcv_nxt; 762be2ac88cSJonathan Lemon /* 76341446225SJonathan Lemon * RFC1323: The window in SYN & SYN/ACK 76441446225SJonathan Lemon * segments is never scaled. 765be2ac88cSJonathan Lemon */ 766be2ac88cSJonathan Lemon tp->snd_wnd = tiwin; /* unscaled */ 767be2ac88cSJonathan Lemon goto after_listen; 768be2ac88cSJonathan Lemon } 769be2ac88cSJonathan Lemon if (thflags & TH_RST) { 770be2ac88cSJonathan Lemon syncache_chkrst(&inc, th); 771be2ac88cSJonathan Lemon goto drop; 772be2ac88cSJonathan Lemon } 773fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 774be2ac88cSJonathan Lemon syncache_badack(&inc); 775ebb0cbeaSPaul Traina tcpstat.tcps_badsyn++; 776a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 777a57815efSBosko Milekic goto dropwithreset; 7784195b4afSPaul Traina } 779ebb0cbeaSPaul Traina goto drop; 780ebb0cbeaSPaul Traina } 78133841545SHajimu UMEMOTO 782be2ac88cSJonathan Lemon /* 783be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 784be2ac88cSJonathan Lemon */ 78533841545SHajimu UMEMOTO #ifdef INET6 78633841545SHajimu UMEMOTO /* 78733841545SHajimu UMEMOTO * If deprecated address is forbidden, 78833841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 78933841545SHajimu UMEMOTO * address to prevent any new inbound connection from 79033841545SHajimu UMEMOTO * getting established. 79133841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 79233841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 79333841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 79433841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 79533841545SHajimu UMEMOTO * for the exchange. 79633841545SHajimu UMEMOTO * 79733841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 79833841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 79933841545SHajimu UMEMOTO * SYN in this case. 80033841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 80133841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 80233841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 80333841545SHajimu UMEMOTO * used" 80433841545SHajimu UMEMOTO * 2. use of it with new communication: 80533841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 80633841545SHajimu UMEMOTO * with sufficient scope is available" 80733841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 80833841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 80933841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 81033841545SHajimu UMEMOTO * 81133841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 81233841545SHajimu UMEMOTO * multiple description text for deprecated address 81333841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 81433841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 81533841545SHajimu UMEMOTO */ 81633841545SHajimu UMEMOTO if (isipv6 && !ip6_use_deprecated) { 81733841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 81833841545SHajimu UMEMOTO 81933841545SHajimu UMEMOTO if ((ia6 = ip6_getdstifaddr(m)) && 82033841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 821f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 82233841545SHajimu UMEMOTO tp = NULL; 82333841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 82433841545SHajimu UMEMOTO goto dropwithreset; 82533841545SHajimu UMEMOTO } 82633841545SHajimu UMEMOTO } 82733841545SHajimu UMEMOTO #endif 82865f28919SJesper Skriver /* 829be2ac88cSJonathan Lemon * If it is from this socket, drop it, it must be forged. 830be2ac88cSJonathan Lemon * Don't bother responding if the destination was a broadcast. 83165f28919SJesper Skriver */ 832be2ac88cSJonathan Lemon if (th->th_dport == th->th_sport) { 833fb59c426SYoshinobu Inoue if (isipv6) { 834be2ac88cSJonathan Lemon if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 835be2ac88cSJonathan Lemon &ip6->ip6_src)) 836be2ac88cSJonathan Lemon goto drop; 837c068736aSJeffrey Hsu } else { 838be2ac88cSJonathan Lemon if (ip->ip_dst.s_addr == ip->ip_src.s_addr) 839be2ac88cSJonathan Lemon goto drop; 840be2ac88cSJonathan Lemon } 841c068736aSJeffrey Hsu } 842be2ac88cSJonathan Lemon /* 843be2ac88cSJonathan Lemon * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 8442ca2159fSCrist J. Clark * 84593ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 84693ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 84793ec91baSCrist J. Clark * in_broadcast() to find them. 848be2ac88cSJonathan Lemon */ 849be2ac88cSJonathan Lemon if (m->m_flags & (M_BCAST|M_MCAST)) 850be2ac88cSJonathan Lemon goto drop; 851be2ac88cSJonathan Lemon if (isipv6) { 852be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 853be2ac88cSJonathan Lemon IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 854be2ac88cSJonathan Lemon goto drop; 855c068736aSJeffrey Hsu } else { 856be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 857be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 8582ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 8592ca2159fSCrist J. Clark in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 860be2ac88cSJonathan Lemon goto drop; 861c068736aSJeffrey Hsu } 862be2ac88cSJonathan Lemon /* 863be2ac88cSJonathan Lemon * SYN appears to be valid; create compressed TCP state 864be2ac88cSJonathan Lemon * for syncache, or perform t/tcp connection. 865be2ac88cSJonathan Lemon */ 866be2ac88cSJonathan Lemon if (so->so_qlen <= so->so_qlimit) { 867be2ac88cSJonathan Lemon tcp_dooptions(&to, optp, optlen, 1); 868be2ac88cSJonathan Lemon if (!syncache_add(&inc, &to, th, &so, m)) 869be2ac88cSJonathan Lemon goto drop; 870f76fcf6dSJeffrey Hsu if (so == NULL) { 871be2ac88cSJonathan Lemon /* 872be2ac88cSJonathan Lemon * Entry added to syncache, mbuf used to 873be2ac88cSJonathan Lemon * send SYN,ACK packet. 874be2ac88cSJonathan Lemon */ 875f76fcf6dSJeffrey Hsu KASSERT(headlocked, ("headlocked")); 876f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 877f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 878be2ac88cSJonathan Lemon return; 879f76fcf6dSJeffrey Hsu } 880be2ac88cSJonathan Lemon /* 881be2ac88cSJonathan Lemon * Segment passed TAO tests. 882be2ac88cSJonathan Lemon */ 883f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 884be2ac88cSJonathan Lemon inp = sotoinpcb(so); 885f76fcf6dSJeffrey Hsu INP_LOCK(inp); 886df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 887be2ac88cSJonathan Lemon tp->snd_wnd = tiwin; 888be2ac88cSJonathan Lemon tp->t_starttime = ticks; 889be2ac88cSJonathan Lemon tp->t_state = TCPS_ESTABLISHED; 890df8bae1dSRodney W. Grimes 891be2ac88cSJonathan Lemon /* 8923bfd6421SJonathan Lemon * T/TCP logic: 8933bfd6421SJonathan Lemon * If there is a FIN or if there is data, then 8943bfd6421SJonathan Lemon * delay SYN,ACK(SYN) in the hope of piggy-backing 8953bfd6421SJonathan Lemon * it on a response segment. Otherwise must send 8963bfd6421SJonathan Lemon * ACK now in case the other side is slow starting. 897be2ac88cSJonathan Lemon */ 8983bfd6421SJonathan Lemon if (thflags & TH_FIN || tlen != 0) 8993bfd6421SJonathan Lemon tp->t_flags |= (TF_DELACK | TF_NEEDSYN); 9003bfd6421SJonathan Lemon else 901f243998bSJonathan Lemon tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 902be2ac88cSJonathan Lemon tcpstat.tcps_connects++; 903be2ac88cSJonathan Lemon soisconnected(so); 904be2ac88cSJonathan Lemon goto trimthenstep6; 905df8bae1dSRodney W. Grimes } 906be2ac88cSJonathan Lemon goto drop; 9074cc20ab1SSeigo Tanimura } 908be2ac88cSJonathan Lemon after_listen: 909be2ac88cSJonathan Lemon 910be2ac88cSJonathan Lemon /* XXX temp debugging */ 911be2ac88cSJonathan Lemon /* should not happen - syncache should pick up these connections */ 912be2ac88cSJonathan Lemon if (tp->t_state == TCPS_LISTEN) 913be2ac88cSJonathan Lemon panic("tcp_input: TCPS_LISTEN"); 914df8bae1dSRodney W. Grimes 915df8bae1dSRodney W. Grimes /* 916df8bae1dSRodney W. Grimes * Segment received on connection. 917df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 918df8bae1dSRodney W. Grimes */ 9199b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 9207ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 9219b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp); 922df8bae1dSRodney W. Grimes 923df8bae1dSRodney W. Grimes /* 924be2ac88cSJonathan Lemon * Process options. 925be2ac88cSJonathan Lemon * XXX this is tradtitional behavior, may need to be cleaned up. 926df8bae1dSRodney W. Grimes */ 927be2ac88cSJonathan Lemon tcp_dooptions(&to, optp, optlen, thflags & TH_SYN); 928be2ac88cSJonathan Lemon if (thflags & TH_SYN) { 929be2ac88cSJonathan Lemon if (to.to_flags & TOF_SCALE) { 930be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 931be2ac88cSJonathan Lemon tp->requested_s_scale = to.to_requested_s_scale; 932be2ac88cSJonathan Lemon } 933be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 934be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 935be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 936be2ac88cSJonathan Lemon tp->ts_recent_age = ticks; 937be2ac88cSJonathan Lemon } 938be2ac88cSJonathan Lemon if (to.to_flags & (TOF_CC|TOF_CCNEW)) 939be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_CC; 940be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 941be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 942be2ac88cSJonathan Lemon } 943df8bae1dSRodney W. Grimes 944df8bae1dSRodney W. Grimes /* 945df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 946df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 947df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 948df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 949df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 950df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 951df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 952df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 953df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 954df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 955df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 956df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 957a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 958a0292f23SGarrett Wollman * Since we check for TCPS_ESTABLISHED above, it can only 959a0292f23SGarrett Wollman * be TH_NEEDSYN. 960df8bae1dSRodney W. Grimes */ 961df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 962fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 963a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 964be2ac88cSJonathan Lemon ((to.to_flags & TOF_TS) == 0 || 965a0292f23SGarrett Wollman TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 966a0292f23SGarrett Wollman /* 967a0292f23SGarrett Wollman * Using the CC option is compulsory if once started: 968a0292f23SGarrett Wollman * the segment is OK if no T/TCP was negotiated or 969a0292f23SGarrett Wollman * if the segment has a CC option equal to CCrecv 970a0292f23SGarrett Wollman */ 971a0292f23SGarrett Wollman ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) || 972be2ac88cSJonathan Lemon ((to.to_flags & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) && 973fb59c426SYoshinobu Inoue th->th_seq == tp->rcv_nxt && 974df8bae1dSRodney W. Grimes tiwin && tiwin == tp->snd_wnd && 975df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) { 976df8bae1dSRodney W. Grimes 977df8bae1dSRodney W. Grimes /* 978df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 979df8bae1dSRodney W. Grimes * record the timestamp. 980a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 981a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 982df8bae1dSRodney W. Grimes */ 983be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 984fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 9859b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 986a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 987df8bae1dSRodney W. Grimes } 988df8bae1dSRodney W. Grimes 989fb59c426SYoshinobu Inoue if (tlen == 0) { 990fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 991fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 992233e8c18SGarrett Wollman tp->snd_cwnd >= tp->snd_wnd && 993cb942153SJeffrey Hsu ((!tcp_do_newreno && 994cb942153SJeffrey Hsu tp->t_dupacks < tcprexmtthresh) || 9959d11646dSJeffrey Hsu (tcp_do_newreno && !IN_FASTRECOVERY(tp)))) { 996f76fcf6dSJeffrey Hsu KASSERT(headlocked, ("headlocked")); 997f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 998df8bae1dSRodney W. Grimes /* 999df8bae1dSRodney W. Grimes * this is a pure ack for outstanding data. 1000df8bae1dSRodney W. Grimes */ 1001df8bae1dSRodney W. Grimes ++tcpstat.tcps_predack; 10029b8b58e0SJonathan Lemon /* 10039b8b58e0SJonathan Lemon * "bad retransmit" recovery 10049b8b58e0SJonathan Lemon */ 10059b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 10069b8b58e0SJonathan Lemon ticks < tp->t_badrxtwin) { 1007cb942153SJeffrey Hsu ++tcpstat.tcps_sndrexmitbad; 10089b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 10099b8b58e0SJonathan Lemon tp->snd_ssthresh = 10109b8b58e0SJonathan Lemon tp->snd_ssthresh_prev; 10119d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 10129d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 10139d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 10149b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 10159b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; 10169b8b58e0SJonathan Lemon } 1017fa55172bSMatthew Dillon 1018fa55172bSMatthew Dillon /* 1019fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1020fa55172bSMatthew Dillon * 1021fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1022fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1023fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1024fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1025fa55172bSMatthew Dillon */ 1026fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1027fa55172bSMatthew Dillon to.to_tsecr) { 1028a0292f23SGarrett Wollman tcp_xmit_timer(tp, 10299b8b58e0SJonathan Lemon ticks - to.to_tsecr + 1); 1030fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1031fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1032c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1033c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1034fa55172bSMatthew Dillon } 10351fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 1036fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 1037df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 1038df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 1039df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 10409d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 10419d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 10429d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 10439d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 10441645d090SJeff Roberson /* 10451645d090SJeff Roberson * pull snd_wl2 up to prevent seq wrap relative 10461645d090SJeff Roberson * to th_ack. 10471645d090SJeff Roberson */ 1048cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1049b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1050df8bae1dSRodney W. Grimes m_freem(m); 1051fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 1052df8bae1dSRodney W. Grimes 1053df8bae1dSRodney W. Grimes /* 1054df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1055df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1056df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1057df8bae1dSRodney W. Grimes * If process is waiting for space, 1058df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1059df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1060df8bae1dSRodney W. Grimes * decide between more output or persist. 1061df8bae1dSRodney W. Grimes */ 1062df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 10639b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 10649b8b58e0SJonathan Lemon else if (!callout_active(tp->tt_persist)) 10659b8b58e0SJonathan Lemon callout_reset(tp->tt_rexmt, 10669b8b58e0SJonathan Lemon tp->t_rxtcur, 10679b8b58e0SJonathan Lemon tcp_timer_rexmt, tp); 1068df8bae1dSRodney W. Grimes 1069df8bae1dSRodney W. Grimes sowwakeup(so); 1070df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 1071df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1072a14c749fSJonathan Lemon goto check_delack; 1073df8bae1dSRodney W. Grimes } 1074fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1075fb59c426SYoshinobu Inoue LIST_EMPTY(&tp->t_segq) && 1076fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 1077f76fcf6dSJeffrey Hsu KASSERT(headlocked, ("headlocked")); 1078f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 1079df8bae1dSRodney W. Grimes /* 1080df8bae1dSRodney W. Grimes * this is a pure, in-sequence data packet 1081df8bae1dSRodney W. Grimes * with nothing on the reassembly queue and 1082df8bae1dSRodney W. Grimes * we have enough buffer space to take it. 1083df8bae1dSRodney W. Grimes */ 1084df8bae1dSRodney W. Grimes ++tcpstat.tcps_preddat; 1085fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 10861645d090SJeff Roberson /* 10871645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 10881645d090SJeff Roberson * th_seq. 10891645d090SJeff Roberson */ 10901645d090SJeff Roberson tp->snd_wl1 = th->th_seq; 10911645d090SJeff Roberson /* 10921645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 10931645d090SJeff Roberson * rcv_nxt. 10941645d090SJeff Roberson */ 10951645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 1096df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpack++; 1097fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyte += tlen; 1098fb59c426SYoshinobu Inoue ND6_HINT(tp); /* some progress has been done */ 1099df8bae1dSRodney W. Grimes /* 1100755c1f07SAndras Olah * Add data to socket buffer. 1101df8bae1dSRodney W. Grimes */ 1102c1c36a2cSMike Silbersack if (so->so_state & SS_CANTRCVMORE) { 1103c1c36a2cSMike Silbersack m_freem(m); 1104c1c36a2cSMike Silbersack } else { 1105fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 1106df8bae1dSRodney W. Grimes sbappend(&so->so_rcv, m); 1107c1c36a2cSMike Silbersack } 1108df8bae1dSRodney W. Grimes sorwakeup(so); 1109d8c85a26SJonathan Lemon if (DELAY_ACK(tp)) { 11103bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1111f498eeeeSDavid Greenman } else { 1112e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 1113e612a582SDavid Greenman tcp_output(tp); 1114e612a582SDavid Greenman } 1115a14c749fSJonathan Lemon goto check_delack; 1116df8bae1dSRodney W. Grimes } 1117df8bae1dSRodney W. Grimes } 1118df8bae1dSRodney W. Grimes 1119df8bae1dSRodney W. Grimes /* 1120df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1121df8bae1dSRodney W. Grimes * and then do TCP input processing. 1122df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1123df8bae1dSRodney W. Grimes * but not less than advertised window. 1124df8bae1dSRodney W. Grimes */ 1125df8bae1dSRodney W. Grimes { int win; 1126df8bae1dSRodney W. Grimes 1127df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1128df8bae1dSRodney W. Grimes if (win < 0) 1129df8bae1dSRodney W. Grimes win = 0; 113066e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1131df8bae1dSRodney W. Grimes } 1132df8bae1dSRodney W. Grimes 1133df8bae1dSRodney W. Grimes switch (tp->t_state) { 1134df8bae1dSRodney W. Grimes 1135df8bae1dSRodney W. Grimes /* 1136764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1137764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1138764d8cefSBill Fenner */ 1139764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1140fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1141fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1142a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1143a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1144a57815efSBosko Milekic goto dropwithreset; 1145a57815efSBosko Milekic } 1146764d8cefSBill Fenner break; 1147764d8cefSBill Fenner 1148764d8cefSBill Fenner /* 1149df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1150df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1151df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1152df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1153df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1154df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1155df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1156df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1157df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1158df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1159df8bae1dSRodney W. Grimes */ 1160df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1161be2ac88cSJonathan Lemon if ((taop = tcp_gettaocache(&inp->inp_inc)) == NULL) { 1162a0292f23SGarrett Wollman taop = &tao_noncached; 1163a0292f23SGarrett Wollman bzero(taop, sizeof(*taop)); 1164a0292f23SGarrett Wollman } 1165a0292f23SGarrett Wollman 1166fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1167fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1168fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1169a0292f23SGarrett Wollman /* 1170a0292f23SGarrett Wollman * If we have a cached CCsent for the remote host, 1171a0292f23SGarrett Wollman * hence we haven't just crashed and restarted, 1172a0292f23SGarrett Wollman * do not send a RST. This may be a retransmission 1173a0292f23SGarrett Wollman * from the other side after our earlier ACK was lost. 1174a0292f23SGarrett Wollman * Our new SYN, when it arrives, will serve as the 1175a0292f23SGarrett Wollman * needed ACK. 1176a0292f23SGarrett Wollman */ 1177a0292f23SGarrett Wollman if (taop->tao_ccsent != 0) 1178a0292f23SGarrett Wollman goto drop; 1179a57815efSBosko Milekic else { 1180a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1181a0292f23SGarrett Wollman goto dropwithreset; 1182a0292f23SGarrett Wollman } 1183a57815efSBosko Milekic } 1184fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 1185fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1186df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 1187df8bae1dSRodney W. Grimes goto drop; 1188df8bae1dSRodney W. Grimes } 1189fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) == 0) 1190df8bae1dSRodney W. Grimes goto drop; 1191fb59c426SYoshinobu Inoue tp->snd_wnd = th->th_win; /* initial send window */ 1192a0292f23SGarrett Wollman tp->cc_recv = to.to_cc; /* foreign CC */ 1193a0292f23SGarrett Wollman 1194fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1195df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1196fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1197a0292f23SGarrett Wollman /* 1198a0292f23SGarrett Wollman * Our SYN was acked. If segment contains CC.ECHO 1199a0292f23SGarrett Wollman * option, check it to make sure this segment really 1200a0292f23SGarrett Wollman * matches our SYN. If not, just drop it as old 1201a0292f23SGarrett Wollman * duplicate, but send an RST if we're still playing 1202026650e5SBill Fenner * by the old rules. If no CC.ECHO option, make sure 1203026650e5SBill Fenner * we don't get fooled into using T/TCP. 1204a0292f23SGarrett Wollman */ 1205be2ac88cSJonathan Lemon if (to.to_flags & TOF_CCECHO) { 1206dfd5dee1SPeter Wemm if (tp->cc_send != to.to_ccecho) { 1207a0292f23SGarrett Wollman if (taop->tao_ccsent != 0) 1208a0292f23SGarrett Wollman goto drop; 1209a57815efSBosko Milekic else { 1210a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1211a0292f23SGarrett Wollman goto dropwithreset; 1212dfd5dee1SPeter Wemm } 1213a57815efSBosko Milekic } 1214026650e5SBill Fenner } else 1215026650e5SBill Fenner tp->t_flags &= ~TF_RCVD_CC; 1216845799c1SAndras Olah tcpstat.tcps_connects++; 1217845799c1SAndras Olah soisconnected(so); 1218c488362eSRobert Watson #ifdef MAC 1219c488362eSRobert Watson mac_set_socket_peer_from_mbuf(m, so); 1220c488362eSRobert Watson #endif 1221845799c1SAndras Olah /* Do window scaling on this connection? */ 1222845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1223845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1224845799c1SAndras Olah tp->snd_scale = tp->requested_s_scale; 1225845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1226845799c1SAndras Olah } 1227a0292f23SGarrett Wollman /* Segment is acceptable, update cache if undefined. */ 1228a0292f23SGarrett Wollman if (taop->tao_ccsent == 0) 1229a0292f23SGarrett Wollman taop->tao_ccsent = to.to_ccecho; 1230a0292f23SGarrett Wollman 1231a0292f23SGarrett Wollman tp->rcv_adv += tp->rcv_wnd; 1232a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1233a0292f23SGarrett Wollman /* 1234a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1235a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1236a0292f23SGarrett Wollman */ 1237d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && tlen != 0) 12389b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 12399b8b58e0SJonathan Lemon tcp_timer_delack, tp); 1240a0292f23SGarrett Wollman else 1241a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1242a0292f23SGarrett Wollman /* 1243a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1244a0292f23SGarrett Wollman * Transitions: 1245a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1246a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1247a0292f23SGarrett Wollman */ 12489b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1249a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1250a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1251a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1252fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 12537ff19458SPaul Traina } else { 1254a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 12559b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, 12569b8b58e0SJonathan Lemon tcp_timer_keep, tp); 12577ff19458SPaul Traina } 1258a0292f23SGarrett Wollman } else { 1259a0292f23SGarrett Wollman /* 1260c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 1261c068736aSJeffrey Hsu * simultaneous open. If segment contains CC option 1262c068736aSJeffrey Hsu * and there is a cached CC, apply TAO test. 1263c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 1264c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 1265a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1266a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1267a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1268a0292f23SGarrett Wollman */ 1269a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 12709b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 1271be2ac88cSJonathan Lemon if (to.to_flags & TOF_CC) { 1272a0292f23SGarrett Wollman if (taop->tao_cc != 0 && 1273a0292f23SGarrett Wollman CC_GT(to.to_cc, taop->tao_cc)) { 1274a0292f23SGarrett Wollman /* 1275a0292f23SGarrett Wollman * update cache and make transition: 1276a0292f23SGarrett Wollman * SYN-SENT -> ESTABLISHED* 1277a0292f23SGarrett Wollman * SYN-SENT* -> FIN-WAIT-1* 1278a0292f23SGarrett Wollman */ 1279a0292f23SGarrett Wollman taop->tao_cc = to.to_cc; 12809b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1281a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1282a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1283a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 12847ff19458SPaul Traina } else { 1285a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 12869b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, 12879b8b58e0SJonathan Lemon tcp_keepidle, 12889b8b58e0SJonathan Lemon tcp_timer_keep, 12899b8b58e0SJonathan Lemon tp); 12907ff19458SPaul Traina } 1291a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDSYN; 1292df8bae1dSRodney W. Grimes } else 1293df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1294a0292f23SGarrett Wollman } else { 1295a0292f23SGarrett Wollman /* CC.NEW or no option => invalidate cache */ 1296a0292f23SGarrett Wollman taop->tao_cc = 0; 1297a0292f23SGarrett Wollman tp->t_state = TCPS_SYN_RECEIVED; 1298a0292f23SGarrett Wollman } 1299a0292f23SGarrett Wollman } 1300df8bae1dSRodney W. Grimes 1301df8bae1dSRodney W. Grimes trimthenstep6: 1302df8bae1dSRodney W. Grimes /* 1303fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1304df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1305df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1306df8bae1dSRodney W. Grimes */ 1307fb59c426SYoshinobu Inoue th->th_seq++; 1308fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1309fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1310df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1311fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1312fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1313df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1314df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1315df8bae1dSRodney W. Grimes } 1316fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1317fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 1318a0292f23SGarrett Wollman /* 1319a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1320a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1321a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1322a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1323a0292f23SGarrett Wollman * Otherwise, goto step 6. 1324a0292f23SGarrett Wollman */ 1325fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1326a0292f23SGarrett Wollman goto process_ACK; 1327c068736aSJeffrey Hsu 1328df8bae1dSRodney W. Grimes goto step6; 1329c068736aSJeffrey Hsu 1330a0292f23SGarrett Wollman /* 1331a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1332a0292f23SGarrett Wollman * if segment contains a SYN and CC [not CC.NEW] option: 1333a0292f23SGarrett Wollman * if state == TIME_WAIT and connection duration > MSL, 1334a0292f23SGarrett Wollman * drop packet and send RST; 1335a0292f23SGarrett Wollman * 1336a0292f23SGarrett Wollman * if SEG.CC > CCrecv then is new SYN, and can implicitly 1337a0292f23SGarrett Wollman * ack the FIN (and data) in retransmission queue. 1338a0292f23SGarrett Wollman * Complete close and delete TCPCB. Then reprocess 1339a0292f23SGarrett Wollman * segment, hoping to find new TCPCB in LISTEN state; 1340a0292f23SGarrett Wollman * 1341a0292f23SGarrett Wollman * else must be old SYN; drop it. 1342a0292f23SGarrett Wollman * else do normal processing. 1343a0292f23SGarrett Wollman */ 1344a0292f23SGarrett Wollman case TCPS_LAST_ACK: 1345a0292f23SGarrett Wollman case TCPS_CLOSING: 1346a0292f23SGarrett Wollman case TCPS_TIME_WAIT: 1347340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1348fb59c426SYoshinobu Inoue if ((thflags & TH_SYN) && 1349be2ac88cSJonathan Lemon (to.to_flags & TOF_CC) && tp->cc_recv != 0) { 1350a0292f23SGarrett Wollman if (tp->t_state == TCPS_TIME_WAIT && 1351a57815efSBosko Milekic (ticks - tp->t_starttime) > tcp_msl) { 1352a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1353a0292f23SGarrett Wollman goto dropwithreset; 1354a57815efSBosko Milekic } 1355a0292f23SGarrett Wollman if (CC_GT(to.to_cc, tp->cc_recv)) { 1356a0292f23SGarrett Wollman tp = tcp_close(tp); 1357a0292f23SGarrett Wollman goto findpcb; 1358a0292f23SGarrett Wollman } 1359a0292f23SGarrett Wollman else 1360a0292f23SGarrett Wollman goto drop; 1361a0292f23SGarrett Wollman } 1362a0292f23SGarrett Wollman break; /* continue normal processing */ 1363df8bae1dSRodney W. Grimes } 1364df8bae1dSRodney W. Grimes 1365df8bae1dSRodney W. Grimes /* 1366df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 136780ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 136880ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 136980ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 137080ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 137180ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 137280ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 137380ab7c0eSGarrett Wollman * killing a connection). 137480ab7c0eSGarrett Wollman * Then check timestamp, if present. 1375a0292f23SGarrett Wollman * Then check the connection count, if present. 1376df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 1377df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 1378df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 1379df8bae1dSRodney W. Grimes * 138080ab7c0eSGarrett Wollman * 138180ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 138280ab7c0eSGarrett Wollman * if this is a valid reset segment. 138380ab7c0eSGarrett Wollman * RFC 793 page 37: 138480ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 138580ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 138680ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 138780ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 138880ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 13891a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 13901a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 13911a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 13921a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 139380ab7c0eSGarrett Wollman * If we have multiple segments in flight, the intial reset 139480ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 139580ab7c0eSGarrett Wollman * but they will eventually catch up. 139680ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 139780ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 139880ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 139980ab7c0eSGarrett Wollman * 140080ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 140180ab7c0eSGarrett Wollman * 140280ab7c0eSGarrett Wollman * DISCUSSION 140380ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 140480ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 140580ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 140680ab7c0eSGarrett Wollman * data. 140780ab7c0eSGarrett Wollman * 140880ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 140980ab7c0eSGarrett Wollman * the state: 141080ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 141180ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 141280ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 1413745bab7fSDima Dorfman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 141480ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 1415e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 141680ab7c0eSGarrett Wollman * Close the tcb. 1417e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 141880ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 141980ab7c0eSGarrett Wollman * RFC 1337. 142080ab7c0eSGarrett Wollman */ 1421fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 1422fb59c426SYoshinobu Inoue if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) && 1423fb59c426SYoshinobu Inoue SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 142480ab7c0eSGarrett Wollman switch (tp->t_state) { 142580ab7c0eSGarrett Wollman 142680ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 142780ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 142880ab7c0eSGarrett Wollman goto close; 142980ab7c0eSGarrett Wollman 143080ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 143180ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 143280ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 143380ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 143480ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 143580ab7c0eSGarrett Wollman close: 143680ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 143780ab7c0eSGarrett Wollman tcpstat.tcps_drops++; 143880ab7c0eSGarrett Wollman tp = tcp_close(tp); 143980ab7c0eSGarrett Wollman break; 144080ab7c0eSGarrett Wollman 144180ab7c0eSGarrett Wollman case TCPS_CLOSING: 144280ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 144380ab7c0eSGarrett Wollman tp = tcp_close(tp); 144480ab7c0eSGarrett Wollman break; 144580ab7c0eSGarrett Wollman 144680ab7c0eSGarrett Wollman case TCPS_TIME_WAIT: 1447340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, 1448340c35deSJonathan Lemon ("timewait")); 144980ab7c0eSGarrett Wollman break; 145080ab7c0eSGarrett Wollman } 145180ab7c0eSGarrett Wollman } 145280ab7c0eSGarrett Wollman goto drop; 145380ab7c0eSGarrett Wollman } 145480ab7c0eSGarrett Wollman 145580ab7c0eSGarrett Wollman /* 1456df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 1457df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 1458df8bae1dSRodney W. Grimes */ 1459be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 146080ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1461df8bae1dSRodney W. Grimes 1462df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 14639b8b58e0SJonathan Lemon if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1464df8bae1dSRodney W. Grimes /* 1465df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 1466df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 1467df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 1468df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 1469df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 1470df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 1471df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 1472df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 1473df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 1474df8bae1dSRodney W. Grimes */ 1475df8bae1dSRodney W. Grimes tp->ts_recent = 0; 1476df8bae1dSRodney W. Grimes } else { 1477df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 1478fb59c426SYoshinobu Inoue tcpstat.tcps_rcvdupbyte += tlen; 1479df8bae1dSRodney W. Grimes tcpstat.tcps_pawsdrop++; 148007fd333dSMatthew Dillon if (tlen) 1481df8bae1dSRodney W. Grimes goto dropafterack; 14821ab4789dSMatthew Dillon goto drop; 14831ab4789dSMatthew Dillon } 1484df8bae1dSRodney W. Grimes } 1485df8bae1dSRodney W. Grimes 1486a0292f23SGarrett Wollman /* 1487a0292f23SGarrett Wollman * T/TCP mechanism 1488a0292f23SGarrett Wollman * If T/TCP was negotiated and the segment doesn't have CC, 1489dc733423SDag-Erling Smørgrav * or if its CC is wrong then drop the segment. 1490a0292f23SGarrett Wollman * RST segments do not have to comply with this. 1491a0292f23SGarrett Wollman */ 1492a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) && 1493be2ac88cSJonathan Lemon ((to.to_flags & TOF_CC) == 0 || tp->cc_recv != to.to_cc)) 1494a0292f23SGarrett Wollman goto dropafterack; 1495a0292f23SGarrett Wollman 149680ab7c0eSGarrett Wollman /* 149780ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 149880ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 149980ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 150080ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 150180ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 150280ab7c0eSGarrett Wollman */ 1503a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 1504a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1505a57815efSBosko Milekic goto dropwithreset; 1506a57815efSBosko Milekic } 150780ab7c0eSGarrett Wollman 1508fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 1509df8bae1dSRodney W. Grimes if (todrop > 0) { 1510fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1511fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 1512fb59c426SYoshinobu Inoue th->th_seq++; 1513fb59c426SYoshinobu Inoue if (th->th_urp > 1) 1514fb59c426SYoshinobu Inoue th->th_urp--; 1515df8bae1dSRodney W. Grimes else 1516fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1517df8bae1dSRodney W. Grimes todrop--; 1518df8bae1dSRodney W. Grimes } 1519df8bae1dSRodney W. Grimes /* 1520dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 1521df8bae1dSRodney W. Grimes */ 1522fb59c426SYoshinobu Inoue if (todrop > tlen 1523fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1524dac20301SGarrett Wollman /* 1525dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 1526dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 1527dac20301SGarrett Wollman * of sequence; drop it. 1528dac20301SGarrett Wollman */ 1529fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1530dac20301SGarrett Wollman 1531df8bae1dSRodney W. Grimes /* 1532dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 1533dac20301SGarrett Wollman * But keep on processing for RST or ACK. 1534df8bae1dSRodney W. Grimes */ 1535dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1536fb59c426SYoshinobu Inoue todrop = tlen; 1537dac20301SGarrett Wollman tcpstat.tcps_rcvduppack++; 1538dac20301SGarrett Wollman tcpstat.tcps_rcvdupbyte += todrop; 1539df8bae1dSRodney W. Grimes } else { 1540df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartduppack++; 1541df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartdupbyte += todrop; 1542df8bae1dSRodney W. Grimes } 1543fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 1544fb59c426SYoshinobu Inoue th->th_seq += todrop; 1545fb59c426SYoshinobu Inoue tlen -= todrop; 1546fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 1547fb59c426SYoshinobu Inoue th->th_urp -= todrop; 1548df8bae1dSRodney W. Grimes else { 1549fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1550fb59c426SYoshinobu Inoue th->th_urp = 0; 1551df8bae1dSRodney W. Grimes } 1552df8bae1dSRodney W. Grimes } 1553df8bae1dSRodney W. Grimes 1554df8bae1dSRodney W. Grimes /* 1555df8bae1dSRodney W. Grimes * If new data are received on a connection after the 1556df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 1557df8bae1dSRodney W. Grimes */ 1558df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 1559fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1560df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1561df8bae1dSRodney W. Grimes tcpstat.tcps_rcvafterclose++; 1562a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1563df8bae1dSRodney W. Grimes goto dropwithreset; 1564df8bae1dSRodney W. Grimes } 1565df8bae1dSRodney W. Grimes 1566df8bae1dSRodney W. Grimes /* 1567df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 1568df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 1569df8bae1dSRodney W. Grimes */ 1570fb59c426SYoshinobu Inoue todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd); 1571df8bae1dSRodney W. Grimes if (todrop > 0) { 1572df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1573fb59c426SYoshinobu Inoue if (todrop >= tlen) { 1574fb59c426SYoshinobu Inoue tcpstat.tcps_rcvbyteafterwin += tlen; 1575df8bae1dSRodney W. Grimes /* 1576df8bae1dSRodney W. Grimes * If a new connection request is received 1577df8bae1dSRodney W. Grimes * while in TIME_WAIT, drop the old connection 1578df8bae1dSRodney W. Grimes * and start over if the sequence numbers 1579df8bae1dSRodney W. Grimes * are above the previous ones. 1580df8bae1dSRodney W. Grimes */ 1581340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1582fb59c426SYoshinobu Inoue if (thflags & TH_SYN && 1583df8bae1dSRodney W. Grimes tp->t_state == TCPS_TIME_WAIT && 1584fb59c426SYoshinobu Inoue SEQ_GT(th->th_seq, tp->rcv_nxt)) { 1585df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1586df8bae1dSRodney W. Grimes goto findpcb; 1587df8bae1dSRodney W. Grimes } 1588df8bae1dSRodney W. Grimes /* 1589df8bae1dSRodney W. Grimes * If window is closed can only take segments at 1590df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 1591df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 1592df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 1593df8bae1dSRodney W. Grimes * and ack. 1594df8bae1dSRodney W. Grimes */ 1595fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1596df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1597df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinprobe++; 1598df8bae1dSRodney W. Grimes } else 1599df8bae1dSRodney W. Grimes goto dropafterack; 1600df8bae1dSRodney W. Grimes } else 1601df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1602df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1603fb59c426SYoshinobu Inoue tlen -= todrop; 1604fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 1605df8bae1dSRodney W. Grimes } 1606df8bae1dSRodney W. Grimes 1607df8bae1dSRodney W. Grimes /* 1608df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1609df8bae1dSRodney W. Grimes * record its timestamp. 1610a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1611a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1612df8bae1dSRodney W. Grimes */ 1613be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1614fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 16159b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1616a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1617df8bae1dSRodney W. Grimes } 1618df8bae1dSRodney W. Grimes 1619df8bae1dSRodney W. Grimes /* 1620df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 1621df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 1622df8bae1dSRodney W. Grimes */ 1623fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1624df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 1625a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1626df8bae1dSRodney W. Grimes goto dropwithreset; 1627df8bae1dSRodney W. Grimes } 1628df8bae1dSRodney W. Grimes 1629a0292f23SGarrett Wollman /* 1630a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1631a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 1632a0292f23SGarrett Wollman * later processing; else drop segment and return. 1633a0292f23SGarrett Wollman */ 1634fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 1635a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 1636a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 1637a0292f23SGarrett Wollman goto step6; 1638a0292f23SGarrett Wollman else 1639a0292f23SGarrett Wollman goto drop; 1640a0292f23SGarrett Wollman } 1641df8bae1dSRodney W. Grimes 1642df8bae1dSRodney W. Grimes /* 1643df8bae1dSRodney W. Grimes * Ack processing. 1644df8bae1dSRodney W. Grimes */ 1645df8bae1dSRodney W. Grimes switch (tp->t_state) { 1646df8bae1dSRodney W. Grimes 1647df8bae1dSRodney W. Grimes /* 1648764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1649764d8cefSBill Fenner * ESTABLISHED state and continue processing. 1650764d8cefSBill Fenner * The ACK was checked above. 1651df8bae1dSRodney W. Grimes */ 1652df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1653a0292f23SGarrett Wollman 1654df8bae1dSRodney W. Grimes tcpstat.tcps_connects++; 1655df8bae1dSRodney W. Grimes soisconnected(so); 1656df8bae1dSRodney W. Grimes /* Do window scaling? */ 1657df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1658df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1659df8bae1dSRodney W. Grimes tp->snd_scale = tp->requested_s_scale; 1660df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 1661df8bae1dSRodney W. Grimes } 1662a0292f23SGarrett Wollman /* 1663a0292f23SGarrett Wollman * Upon successful completion of 3-way handshake, 1664a0292f23SGarrett Wollman * update cache.CC if it was undefined, pass any queued 1665a0292f23SGarrett Wollman * data to the user, and advance state appropriately. 1666a0292f23SGarrett Wollman */ 1667be2ac88cSJonathan Lemon if ((taop = tcp_gettaocache(&inp->inp_inc)) != NULL && 1668a0292f23SGarrett Wollman taop->tao_cc == 0) 1669a0292f23SGarrett Wollman taop->tao_cc = tp->cc_recv; 1670a0292f23SGarrett Wollman 1671a0292f23SGarrett Wollman /* 1672a0292f23SGarrett Wollman * Make transitions: 1673a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 1674a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 1675a0292f23SGarrett Wollman */ 16769b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1677a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1678a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1679a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 16807ff19458SPaul Traina } else { 1681a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 16829b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, 16839b8b58e0SJonathan Lemon tcp_timer_keep, tp); 16847ff19458SPaul Traina } 1685a0292f23SGarrett Wollman /* 1686a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 1687a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 1688a0292f23SGarrett Wollman */ 1689fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 1690fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1691a0292f23SGarrett Wollman (struct mbuf *)0); 1692fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 169393b0017fSPhilippe Charnier /* FALLTHROUGH */ 1694df8bae1dSRodney W. Grimes 1695df8bae1dSRodney W. Grimes /* 1696df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1697df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 1698fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 1699fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 1700df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 1701df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 1702df8bae1dSRodney W. Grimes */ 1703df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1704df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1705df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 1706df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1707df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1708df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 1709df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 1710340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 1711fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1712fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 1713df8bae1dSRodney W. Grimes tcpstat.tcps_rcvdupack++; 1714df8bae1dSRodney W. Grimes /* 1715df8bae1dSRodney W. Grimes * If we have outstanding data (other than 1716df8bae1dSRodney W. Grimes * a window probe), this is a completely 1717df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 1718df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 1719df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 1720df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 1721df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 1722df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 1723df8bae1dSRodney W. Grimes * window so we send only this one 1724df8bae1dSRodney W. Grimes * packet. 1725df8bae1dSRodney W. Grimes * 1726df8bae1dSRodney W. Grimes * We know we're losing at the current 1727df8bae1dSRodney W. Grimes * window size so do congestion avoidance 1728df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 1729df8bae1dSRodney W. Grimes * and pull our congestion window back to 1730df8bae1dSRodney W. Grimes * the new ssthresh). 1731df8bae1dSRodney W. Grimes * 1732df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 1733df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 1734df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 1735df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 1736df8bae1dSRodney W. Grimes * network. 1737df8bae1dSRodney W. Grimes */ 17389b8b58e0SJonathan Lemon if (!callout_active(tp->tt_rexmt) || 1739fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 1740df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1741cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 1742cb942153SJeffrey Hsu (tcp_do_newreno && 17439d11646dSJeffrey Hsu IN_FASTRECOVERY(tp))) { 174446f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 174546f58482SJonathan Lemon (void) tcp_output(tp); 174646f58482SJonathan Lemon goto drop; 1747cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 1748cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 1749cb942153SJeffrey Hsu u_int win; 1750cb942153SJeffrey Hsu if (tcp_do_newreno && 17519d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, 17529d11646dSJeffrey Hsu tp->snd_recover)) { 1753cb942153SJeffrey Hsu tp->t_dupacks = 0; 1754cb942153SJeffrey Hsu break; 175546f58482SJonathan Lemon } 1756cb942153SJeffrey Hsu win = min(tp->snd_wnd, tp->snd_cwnd) / 1757cb942153SJeffrey Hsu 2 / tp->t_maxseg; 1758df8bae1dSRodney W. Grimes if (win < 2) 1759df8bae1dSRodney W. Grimes win = 2; 1760df8bae1dSRodney W. Grimes tp->snd_ssthresh = win * tp->t_maxseg; 17619d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 176246f58482SJonathan Lemon tp->snd_recover = tp->snd_max; 17639b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 17649b8b58e0SJonathan Lemon tp->t_rtttime = 0; 1765fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 1766df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 1767df8bae1dSRodney W. Grimes (void) tcp_output(tp); 176848d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 176948d2549cSJeffrey Hsu ("tp->snd_limited too big")); 1770df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 177148d2549cSJeffrey Hsu tp->t_maxseg * 177248d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 1773df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 1774df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 1775df8bae1dSRodney W. Grimes goto drop; 1776582a954bSJeffrey Hsu } else if (tcp_do_rfc3042) { 1777582a954bSJeffrey Hsu u_long oldcwnd = tp->snd_cwnd; 177848d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 177948d2549cSJeffrey Hsu u_int sent; 1780582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 1781582a954bSJeffrey Hsu tp->t_dupacks == 2, 1782582a954bSJeffrey Hsu ("dupacks not 1 or 2")); 178348d2549cSJeffrey Hsu if (tp->t_dupacks == 1) { 178448d2549cSJeffrey Hsu tp->snd_limited = 0; 178548d2549cSJeffrey Hsu tp->snd_cwnd += tp->t_maxseg; 178648d2549cSJeffrey Hsu } else { 178748d2549cSJeffrey Hsu tp->snd_cwnd += 178848d2549cSJeffrey Hsu tp->t_maxseg * 2; 178948d2549cSJeffrey Hsu } 1790582a954bSJeffrey Hsu (void) tcp_output(tp); 179148d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 179248d2549cSJeffrey Hsu if (sent > tp->t_maxseg) { 179348d2549cSJeffrey Hsu KASSERT(tp->snd_limited == 0 && 179448d2549cSJeffrey Hsu tp->t_dupacks == 2, 179548d2549cSJeffrey Hsu ("sent too much")); 179648d2549cSJeffrey Hsu tp->snd_limited = 2; 179748d2549cSJeffrey Hsu } else if (sent > 0) 179848d2549cSJeffrey Hsu ++tp->snd_limited; 1799582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 1800582a954bSJeffrey Hsu goto drop; 1801df8bae1dSRodney W. Grimes } 1802df8bae1dSRodney W. Grimes } else 1803df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1804df8bae1dSRodney W. Grimes break; 1805df8bae1dSRodney W. Grimes } 1806cb942153SJeffrey Hsu 1807cb942153SJeffrey Hsu KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una")); 1808cb942153SJeffrey Hsu 1809df8bae1dSRodney W. Grimes /* 1810df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 1811df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 1812df8bae1dSRodney W. Grimes */ 1813c068736aSJeffrey Hsu if (tcp_do_newreno) { 18149d11646dSJeffrey Hsu if (IN_FASTRECOVERY(tp)) { 1815cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 1816c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 1817c068736aSJeffrey Hsu } else { 1818c068736aSJeffrey Hsu /* 1819c068736aSJeffrey Hsu * Window inflation should have left us 1820c068736aSJeffrey Hsu * with approximately snd_ssthresh 1821c068736aSJeffrey Hsu * outstanding data. 1822c068736aSJeffrey Hsu * But in case we would be inclined to 1823c068736aSJeffrey Hsu * send a burst, better to do it via 1824c068736aSJeffrey Hsu * the slow start mechanism. 1825c068736aSJeffrey Hsu */ 1826c068736aSJeffrey Hsu if (SEQ_GT(th->th_ack + 1827c068736aSJeffrey Hsu tp->snd_ssthresh, 1828c068736aSJeffrey Hsu tp->snd_max)) 1829c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_max - 1830c068736aSJeffrey Hsu th->th_ack + 1831c068736aSJeffrey Hsu tp->t_maxseg; 1832c068736aSJeffrey Hsu else 1833c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_ssthresh; 1834c068736aSJeffrey Hsu } 1835c068736aSJeffrey Hsu } 1836c068736aSJeffrey Hsu } else { 1837233e8c18SGarrett Wollman if (tp->t_dupacks >= tcprexmtthresh && 1838df8bae1dSRodney W. Grimes tp->snd_cwnd > tp->snd_ssthresh) 1839df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh; 184046f58482SJonathan Lemon } 1841cb942153SJeffrey Hsu tp->t_dupacks = 0; 1842fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_max)) { 1843df8bae1dSRodney W. Grimes tcpstat.tcps_rcvacktoomuch++; 1844df8bae1dSRodney W. Grimes goto dropafterack; 1845df8bae1dSRodney W. Grimes } 1846a0292f23SGarrett Wollman /* 1847a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 1848a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 1849a0292f23SGarrett Wollman */ 1850a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 1851a0292f23SGarrett Wollman /* 1852a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 1853a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 185407e43e10SAndras Olah * synchronized). Go to non-starred state, 185507e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 185607e43e10SAndras Olah * we can do window scaling. 1857a0292f23SGarrett Wollman */ 1858a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 1859a0292f23SGarrett Wollman tp->snd_una++; 186007e43e10SAndras Olah /* Do window scaling? */ 186107e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 186207e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 186307e43e10SAndras Olah tp->snd_scale = tp->requested_s_scale; 186407e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 186507e43e10SAndras Olah } 1866a0292f23SGarrett Wollman } 1867a0292f23SGarrett Wollman 1868a0292f23SGarrett Wollman process_ACK: 1869fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 1870df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 1871df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 1872df8bae1dSRodney W. Grimes 1873df8bae1dSRodney W. Grimes /* 18749b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 18759b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 18769b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 18779b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 18789b8b58e0SJonathan Lemon * we left off. 18799b8b58e0SJonathan Lemon */ 18809b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 1881d65bf08aSMatthew Dillon ++tcpstat.tcps_sndrexmitbad; 18829b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 18839b8b58e0SJonathan Lemon tp->snd_ssthresh = tp->snd_ssthresh_prev; 18849d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 18859d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 18869d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 18879b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 18889b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; /* XXX probably not required */ 18899b8b58e0SJonathan Lemon } 18909b8b58e0SJonathan Lemon 18919b8b58e0SJonathan Lemon /* 1892df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 1893df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 1894df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 1895df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 1896df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 1897df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 1898df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 1899fa55172bSMatthew Dillon * 1900fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1901fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1902fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1903fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1904df8bae1dSRodney W. Grimes */ 1905fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1906fa55172bSMatthew Dillon to.to_tsecr) { 19079b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 1908fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 19099b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 1910fa55172bSMatthew Dillon } 19111fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 1912df8bae1dSRodney W. Grimes 1913df8bae1dSRodney W. Grimes /* 1914df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 1915df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 1916df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 1917df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 1918df8bae1dSRodney W. Grimes */ 1919fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 19209b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 1921df8bae1dSRodney W. Grimes needoutput = 1; 19229b8b58e0SJonathan Lemon } else if (!callout_active(tp->tt_persist)) 19239b8b58e0SJonathan Lemon callout_reset(tp->tt_rexmt, tp->t_rxtcur, 19249b8b58e0SJonathan Lemon tcp_timer_rexmt, tp); 1925a0292f23SGarrett Wollman 1926a0292f23SGarrett Wollman /* 1927a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 1928a0292f23SGarrett Wollman * skip rest of ACK processing. 1929a0292f23SGarrett Wollman */ 1930a0292f23SGarrett Wollman if (acked == 0) 1931a0292f23SGarrett Wollman goto step6; 1932a0292f23SGarrett Wollman 1933df8bae1dSRodney W. Grimes /* 1934df8bae1dSRodney W. Grimes * When new data is acked, open the congestion window. 1935df8bae1dSRodney W. Grimes * If the window gives us less than ssthresh packets 1936df8bae1dSRodney W. Grimes * in flight, open exponentially (maxseg per packet). 1937df8bae1dSRodney W. Grimes * Otherwise open linearly: maxseg per window 193810be5648SGarrett Wollman * (maxseg^2 / cwnd per packet). 1939df8bae1dSRodney W. Grimes */ 19409d11646dSJeffrey Hsu if (!tcp_do_newreno || !IN_FASTRECOVERY(tp)) { 1941df8bae1dSRodney W. Grimes register u_int cw = tp->snd_cwnd; 1942df8bae1dSRodney W. Grimes register u_int incr = tp->t_maxseg; 1943df8bae1dSRodney W. Grimes if (cw > tp->snd_ssthresh) 194410be5648SGarrett Wollman incr = incr * incr / cw; 1945df8bae1dSRodney W. Grimes tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale); 1946df8bae1dSRodney W. Grimes } 1947df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 1948df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 1949df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 1950df8bae1dSRodney W. Grimes ourfinisacked = 1; 1951df8bae1dSRodney W. Grimes } else { 1952df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 1953df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 1954df8bae1dSRodney W. Grimes ourfinisacked = 0; 1955df8bae1dSRodney W. Grimes } 1956df8bae1dSRodney W. Grimes sowwakeup(so); 1957cb942153SJeffrey Hsu /* detect una wraparound */ 19589d11646dSJeffrey Hsu if (tcp_do_newreno && !IN_FASTRECOVERY(tp) && 19599d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 19609d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 19619d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 19629d11646dSJeffrey Hsu if (tcp_do_newreno && IN_FASTRECOVERY(tp) && 19639d11646dSJeffrey Hsu SEQ_GEQ(th->th_ack, tp->snd_recover)) 19649d11646dSJeffrey Hsu EXIT_FASTRECOVERY(tp); 1965fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 1966df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1967df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 1968df8bae1dSRodney W. Grimes 1969df8bae1dSRodney W. Grimes switch (tp->t_state) { 1970df8bae1dSRodney W. Grimes 1971df8bae1dSRodney W. Grimes /* 1972df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 1973df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 1974df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 1975df8bae1dSRodney W. Grimes */ 1976df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1977df8bae1dSRodney W. Grimes if (ourfinisacked) { 1978df8bae1dSRodney W. Grimes /* 1979df8bae1dSRodney W. Grimes * If we can't receive any more 1980df8bae1dSRodney W. Grimes * data, then closing user can proceed. 1981df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 1982df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 1983df8bae1dSRodney W. Grimes * we'll hang forever. 1984df8bae1dSRodney W. Grimes */ 1985340c35deSJonathan Lemon /* XXXjl 1986340c35deSJonathan Lemon * we should release the tp also, and use a 1987340c35deSJonathan Lemon * compressed state. 1988340c35deSJonathan Lemon */ 1989df8bae1dSRodney W. Grimes if (so->so_state & SS_CANTRCVMORE) { 199003e49181SSeigo Tanimura soisdisconnected(so); 19919b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, tcp_maxidle, 19929b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 19934cc20ab1SSeigo Tanimura } 1994df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 1995df8bae1dSRodney W. Grimes } 1996df8bae1dSRodney W. Grimes break; 1997df8bae1dSRodney W. Grimes 1998df8bae1dSRodney W. Grimes /* 1999df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2000df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2001df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2002df8bae1dSRodney W. Grimes * the segment. 2003df8bae1dSRodney W. Grimes */ 2004df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2005df8bae1dSRodney W. Grimes if (ourfinisacked) { 2006340c35deSJonathan Lemon KASSERT(headlocked, ("headlocked")); 200711a20fb8SJeffrey Hsu tcp_twstart(tp); 2008340c35deSJonathan Lemon INP_INFO_WUNLOCK(&tcbinfo); 2009340c35deSJonathan Lemon m_freem(m); 2010340c35deSJonathan Lemon return; 2011df8bae1dSRodney W. Grimes } 2012df8bae1dSRodney W. Grimes break; 2013df8bae1dSRodney W. Grimes 2014df8bae1dSRodney W. Grimes /* 2015df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2016df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2017df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2018df8bae1dSRodney W. Grimes * enter the closed state and return. 2019df8bae1dSRodney W. Grimes */ 2020df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2021df8bae1dSRodney W. Grimes if (ourfinisacked) { 2022df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2023df8bae1dSRodney W. Grimes goto drop; 2024df8bae1dSRodney W. Grimes } 2025df8bae1dSRodney W. Grimes break; 2026df8bae1dSRodney W. Grimes 2027df8bae1dSRodney W. Grimes /* 2028df8bae1dSRodney W. Grimes * In TIME_WAIT state the only thing that should arrive 2029df8bae1dSRodney W. Grimes * is a retransmission of the remote FIN. Acknowledge 2030df8bae1dSRodney W. Grimes * it and restart the finack timer. 2031df8bae1dSRodney W. Grimes */ 2032df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 2033340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 20349b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 20359b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2036df8bae1dSRodney W. Grimes goto dropafterack; 2037df8bae1dSRodney W. Grimes } 2038df8bae1dSRodney W. Grimes } 2039df8bae1dSRodney W. Grimes 2040df8bae1dSRodney W. Grimes step6: 2041df8bae1dSRodney W. Grimes /* 2042df8bae1dSRodney W. Grimes * Update window information. 2043df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 2044df8bae1dSRodney W. Grimes */ 2045fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 2046fb59c426SYoshinobu Inoue (SEQ_LT(tp->snd_wl1, th->th_seq) || 2047fb59c426SYoshinobu Inoue (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2048fb59c426SYoshinobu Inoue (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2049df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 2050fb59c426SYoshinobu Inoue if (tlen == 0 && 2051fb59c426SYoshinobu Inoue tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2052df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinupd++; 2053df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 2054fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq; 2055fb59c426SYoshinobu Inoue tp->snd_wl2 = th->th_ack; 2056df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2057df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 2058df8bae1dSRodney W. Grimes needoutput = 1; 2059df8bae1dSRodney W. Grimes } 2060df8bae1dSRodney W. Grimes 2061df8bae1dSRodney W. Grimes /* 2062df8bae1dSRodney W. Grimes * Process segments with URG. 2063df8bae1dSRodney W. Grimes */ 2064fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2065df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2066df8bae1dSRodney W. Grimes /* 2067df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2068df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2069df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2070df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2071df8bae1dSRodney W. Grimes */ 2072fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2073fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2074fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 2075df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2076df8bae1dSRodney W. Grimes } 2077df8bae1dSRodney W. Grimes /* 2078df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2079df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2080df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2081df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2082df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2083df8bae1dSRodney W. Grimes * 2084df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2085df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2086df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2087df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2088df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2089df8bae1dSRodney W. Grimes * spec states (in one of two places). 2090df8bae1dSRodney W. Grimes */ 2091fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2092fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2093df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2094df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 20954cc20ab1SSeigo Tanimura if (so->so_oobmark == 0) 2096df8bae1dSRodney W. Grimes so->so_state |= SS_RCVATMARK; 2097df8bae1dSRodney W. Grimes sohasoutofband(so); 2098df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2099df8bae1dSRodney W. Grimes } 2100df8bae1dSRodney W. Grimes /* 2101df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2102df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2103df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2104df8bae1dSRodney W. Grimes * data may creep in... ick. 2105df8bae1dSRodney W. Grimes */ 210630613f56SJeffrey Hsu if (th->th_urp <= (u_long)tlen && 210730613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 210830613f56SJeffrey Hsu /* hdr drop is delayed */ 210930613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 211030613f56SJeffrey Hsu } 2111c068736aSJeffrey Hsu } else { 2112df8bae1dSRodney W. Grimes /* 2113df8bae1dSRodney W. Grimes * If no out of band data is expected, 2114df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2115df8bae1dSRodney W. Grimes * with the receive window. 2116df8bae1dSRodney W. Grimes */ 2117df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2118df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2119c068736aSJeffrey Hsu } 2120df8bae1dSRodney W. Grimes dodata: /* XXX */ 2121f76fcf6dSJeffrey Hsu KASSERT(headlocked, ("headlocked")); 2122df8bae1dSRodney W. Grimes /* 2123df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2124df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2125df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2126df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2127df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2128df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2129df8bae1dSRodney W. Grimes */ 2130fb59c426SYoshinobu Inoue if ((tlen || (thflags & TH_FIN)) && 2131df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2132fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2133e4b64281SJesper Skriver /* 2134c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2135c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2136c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2137c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2138c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2139c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2140c068736aSJeffrey Hsu * conversions. 2141c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 2142c068736aSJeffrey Hsu * immediately when segments are out of order (so 2143c068736aSJeffrey Hsu * fast retransmit can work). 2144e4b64281SJesper Skriver */ 2145e4b64281SJesper Skriver if (th->th_seq == tp->rcv_nxt && 2146e4b64281SJesper Skriver LIST_EMPTY(&tp->t_segq) && 2147e4b64281SJesper Skriver TCPS_HAVEESTABLISHED(tp->t_state)) { 2148e4b64281SJesper Skriver if (DELAY_ACK(tp)) 21493bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2150e4b64281SJesper Skriver else 2151e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2152e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2153e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 2154e4b64281SJesper Skriver tcpstat.tcps_rcvpack++; 2155e4b64281SJesper Skriver tcpstat.tcps_rcvbyte += tlen; 2156e4b64281SJesper Skriver ND6_HINT(tp); 2157c1c36a2cSMike Silbersack if (so->so_state & SS_CANTRCVMORE) 2158c1c36a2cSMike Silbersack m_freem(m); 2159c1c36a2cSMike Silbersack else 2160e4b64281SJesper Skriver sbappend(&so->so_rcv, m); 2161e4b64281SJesper Skriver sorwakeup(so); 2162e4b64281SJesper Skriver } else { 2163e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 2164e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2165e4b64281SJesper Skriver } 2166e4b64281SJesper Skriver 2167df8bae1dSRodney W. Grimes /* 2168df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2169df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2170df8bae1dSRodney W. Grimes * buffer size. 2171df8bae1dSRodney W. Grimes */ 2172df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2173df8bae1dSRodney W. Grimes } else { 2174df8bae1dSRodney W. Grimes m_freem(m); 2175fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2176df8bae1dSRodney W. Grimes } 2177df8bae1dSRodney W. Grimes 2178df8bae1dSRodney W. Grimes /* 2179df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2180df8bae1dSRodney W. Grimes * that the connection is closing. 2181df8bae1dSRodney W. Grimes */ 2182fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2183df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2184df8bae1dSRodney W. Grimes socantrcvmore(so); 2185a0292f23SGarrett Wollman /* 2186a0292f23SGarrett Wollman * If connection is half-synchronized 2187d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2188a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2189a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2190a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2191a0292f23SGarrett Wollman */ 21923bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 21933bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2194a0292f23SGarrett Wollman else 2195df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2196df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2197df8bae1dSRodney W. Grimes } 2198df8bae1dSRodney W. Grimes switch (tp->t_state) { 2199df8bae1dSRodney W. Grimes 2200df8bae1dSRodney W. Grimes /* 2201df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2202df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2203df8bae1dSRodney W. Grimes */ 2204df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 22059b8b58e0SJonathan Lemon tp->t_starttime = ticks; 22069b8b58e0SJonathan Lemon /*FALLTHROUGH*/ 2207df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2208df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2209df8bae1dSRodney W. Grimes break; 2210df8bae1dSRodney W. Grimes 2211df8bae1dSRodney W. Grimes /* 2212df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2213df8bae1dSRodney W. Grimes * enter the CLOSING state. 2214df8bae1dSRodney W. Grimes */ 2215df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2216df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 2217df8bae1dSRodney W. Grimes break; 2218df8bae1dSRodney W. Grimes 2219df8bae1dSRodney W. Grimes /* 2220df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 2221df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 2222df8bae1dSRodney W. Grimes * standard timers. 2223df8bae1dSRodney W. Grimes */ 2224df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 222511a20fb8SJeffrey Hsu KASSERT(headlocked == 1, ("headlocked should be 1")); 2226340c35deSJonathan Lemon tcp_twstart(tp); 222711a20fb8SJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2228340c35deSJonathan Lemon return; 2229df8bae1dSRodney W. Grimes 2230df8bae1dSRodney W. Grimes /* 2231df8bae1dSRodney W. Grimes * In TIME_WAIT state restart the 2 MSL time_wait timer. 2232df8bae1dSRodney W. Grimes */ 2233df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 2234340c35deSJonathan Lemon KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait")); 22359b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 22369b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 2237df8bae1dSRodney W. Grimes break; 2238df8bae1dSRodney W. Grimes } 2239df8bae1dSRodney W. Grimes } 22407792ea27SJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2241610ee2f9SDavid Greenman #ifdef TCPDEBUG 22424cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2243fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2244fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2245610ee2f9SDavid Greenman #endif 2246df8bae1dSRodney W. Grimes 2247df8bae1dSRodney W. Grimes /* 2248df8bae1dSRodney W. Grimes * Return any desired output. 2249df8bae1dSRodney W. Grimes */ 2250df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 2251df8bae1dSRodney W. Grimes (void) tcp_output(tp); 22527792ea27SJeffrey Hsu 2253a14c749fSJonathan Lemon check_delack: 2254a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 22553bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 22563bfd6421SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 22573bfd6421SJonathan Lemon tcp_timer_delack, tp); 22583bfd6421SJonathan Lemon } 2259f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 2260df8bae1dSRodney W. Grimes return; 2261df8bae1dSRodney W. Grimes 2262df8bae1dSRodney W. Grimes dropafterack: 2263df8bae1dSRodney W. Grimes /* 2264df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 2265df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 226680ab7c0eSGarrett Wollman * 226780ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 226880ab7c0eSGarrett Wollman * paths to this code happen after packets containing 226980ab7c0eSGarrett Wollman * RST have been dropped. 227080ab7c0eSGarrett Wollman * 227180ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 227280ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 227380ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 227480ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 227580ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 227680ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 2277df8bae1dSRodney W. Grimes */ 2278fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2279fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 2280a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 2281a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2282a57815efSBosko Milekic goto dropwithreset; 2283a57815efSBosko Milekic } 2284a0292f23SGarrett Wollman #ifdef TCPDEBUG 22854cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2286fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2287fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2288a0292f23SGarrett Wollman #endif 228985e8b243SJeffrey Hsu KASSERT(headlocked, ("headlocked should be 1")); 2290f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2291df8bae1dSRodney W. Grimes m_freem(m); 2292df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2293df8bae1dSRodney W. Grimes (void) tcp_output(tp); 2294f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 2295df8bae1dSRodney W. Grimes return; 2296df8bae1dSRodney W. Grimes 2297df8bae1dSRodney W. Grimes dropwithreset: 2298df8bae1dSRodney W. Grimes /* 2299df8bae1dSRodney W. Grimes * Generate a RST, dropping incoming segment. 2300df8bae1dSRodney W. Grimes * Make ACK acceptable to originator of segment. 2301df8bae1dSRodney W. Grimes * Don't bother to respond if destination was broadcast/multicast. 2302df8bae1dSRodney W. Grimes */ 2303fb59c426SYoshinobu Inoue if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2304df8bae1dSRodney W. Grimes goto drop; 2305fb59c426SYoshinobu Inoue if (isipv6) { 2306173c0f9fSWarner Losh if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2307173c0f9fSWarner Losh IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2308fb59c426SYoshinobu Inoue goto drop; 2309c068736aSJeffrey Hsu } else { 2310173c0f9fSWarner Losh if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2311173c0f9fSWarner Losh IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 23122ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 23132ca2159fSCrist J. Clark in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2314fb59c426SYoshinobu Inoue goto drop; 2315c068736aSJeffrey Hsu } 2316fb59c426SYoshinobu Inoue /* IPv6 anycast check is done at tcp6_input() */ 2317a57815efSBosko Milekic 2318a57815efSBosko Milekic /* 2319c59319bfSDag-Erling Smørgrav * Perform bandwidth limiting. 2320a57815efSBosko Milekic */ 2321a57815efSBosko Milekic if (badport_bandlim(rstreason) < 0) 2322a57815efSBosko Milekic goto drop; 2323a57815efSBosko Milekic 2324a0292f23SGarrett Wollman #ifdef TCPDEBUG 23254cc20ab1SSeigo Tanimura if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2326fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2327fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2328a0292f23SGarrett Wollman #endif 23296fd22cafSJeffrey Hsu 23306fd22cafSJeffrey Hsu if (tp) 23316fd22cafSJeffrey Hsu INP_UNLOCK(inp); 23326fd22cafSJeffrey Hsu 2333fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 2334fb59c426SYoshinobu Inoue /* mtod() below is safe as long as hdr dropping is delayed */ 2335fb59c426SYoshinobu Inoue tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack, 2336fb59c426SYoshinobu Inoue TH_RST); 2337df8bae1dSRodney W. Grimes else { 2338fb59c426SYoshinobu Inoue if (thflags & TH_SYN) 2339fb59c426SYoshinobu Inoue tlen++; 2340fb59c426SYoshinobu Inoue /* mtod() below is safe as long as hdr dropping is delayed */ 2341fb59c426SYoshinobu Inoue tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2342fb59c426SYoshinobu Inoue (tcp_seq)0, TH_RST|TH_ACK); 2343df8bae1dSRodney W. Grimes } 2344f76fcf6dSJeffrey Hsu if (headlocked) 2345f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2346df8bae1dSRodney W. Grimes return; 2347df8bae1dSRodney W. Grimes 2348df8bae1dSRodney W. Grimes drop: 2349df8bae1dSRodney W. Grimes /* 2350df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 2351df8bae1dSRodney W. Grimes */ 2352610ee2f9SDavid Greenman #ifdef TCPDEBUG 23534cc20ab1SSeigo Tanimura if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2354fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2355fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2356a0292f23SGarrett Wollman #endif 2357f76fcf6dSJeffrey Hsu if (tp) 2358f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 2359df8bae1dSRodney W. Grimes m_freem(m); 2360f76fcf6dSJeffrey Hsu if (headlocked) 2361f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2362df8bae1dSRodney W. Grimes return; 2363df8bae1dSRodney W. Grimes } 2364df8bae1dSRodney W. Grimes 2365be2ac88cSJonathan Lemon /* 2366be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 2367be2ac88cSJonathan Lemon */ 23680312fbe9SPoul-Henning Kamp static void 2369be2ac88cSJonathan Lemon tcp_dooptions(to, cp, cnt, is_syn) 2370be2ac88cSJonathan Lemon struct tcpopt *to; 2371df8bae1dSRodney W. Grimes u_char *cp; 2372df8bae1dSRodney W. Grimes int cnt; 2373152385d1SDavid E. O'Brien int is_syn; 2374df8bae1dSRodney W. Grimes { 2375df8bae1dSRodney W. Grimes int opt, optlen; 2376df8bae1dSRodney W. Grimes 2377be2ac88cSJonathan Lemon to->to_flags = 0; 2378df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 2379df8bae1dSRodney W. Grimes opt = cp[0]; 2380df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 2381df8bae1dSRodney W. Grimes break; 2382df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 2383df8bae1dSRodney W. Grimes optlen = 1; 2384df8bae1dSRodney W. Grimes else { 2385b474779fSJun-ichiro itojun Hagino if (cnt < 2) 2386b474779fSJun-ichiro itojun Hagino break; 2387df8bae1dSRodney W. Grimes optlen = cp[1]; 2388b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 2389df8bae1dSRodney W. Grimes break; 2390df8bae1dSRodney W. Grimes } 2391df8bae1dSRodney W. Grimes switch (opt) { 2392df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 2393df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 2394df8bae1dSRodney W. Grimes continue; 2395be2ac88cSJonathan Lemon if (!is_syn) 2396df8bae1dSRodney W. Grimes continue; 2397be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 2398be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 2399be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 2400fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 2401df8bae1dSRodney W. Grimes break; 2402df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 2403df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 2404df8bae1dSRodney W. Grimes continue; 2405be2ac88cSJonathan Lemon if (! is_syn) 2406df8bae1dSRodney W. Grimes continue; 2407be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 2408be2ac88cSJonathan Lemon to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 2409df8bae1dSRodney W. Grimes break; 2410df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 2411df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 2412df8bae1dSRodney W. Grimes continue; 2413be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 2414a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2415a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 2416fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 2417a0292f23SGarrett Wollman bcopy((char *)cp + 6, 2418a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2419fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 2420df8bae1dSRodney W. Grimes break; 2421a0292f23SGarrett Wollman case TCPOPT_CC: 2422a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2423a0292f23SGarrett Wollman continue; 2424be2ac88cSJonathan Lemon to->to_flags |= TOF_CC; 2425a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2426a0292f23SGarrett Wollman (char *)&to->to_cc, sizeof(to->to_cc)); 2427fd8e4ebcSMike Barcroft to->to_cc = ntohl(to->to_cc); 2428a0292f23SGarrett Wollman break; 2429a0292f23SGarrett Wollman case TCPOPT_CCNEW: 2430a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2431a0292f23SGarrett Wollman continue; 2432be2ac88cSJonathan Lemon if (!is_syn) 2433a0292f23SGarrett Wollman continue; 2434be2ac88cSJonathan Lemon to->to_flags |= TOF_CCNEW; 2435a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2436a0292f23SGarrett Wollman (char *)&to->to_cc, sizeof(to->to_cc)); 2437fd8e4ebcSMike Barcroft to->to_cc = ntohl(to->to_cc); 2438a0292f23SGarrett Wollman break; 2439a0292f23SGarrett Wollman case TCPOPT_CCECHO: 2440a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2441a0292f23SGarrett Wollman continue; 2442be2ac88cSJonathan Lemon if (!is_syn) 2443a0292f23SGarrett Wollman continue; 2444be2ac88cSJonathan Lemon to->to_flags |= TOF_CCECHO; 2445a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2446a0292f23SGarrett Wollman (char *)&to->to_ccecho, sizeof(to->to_ccecho)); 2447fd8e4ebcSMike Barcroft to->to_ccecho = ntohl(to->to_ccecho); 2448a0292f23SGarrett Wollman break; 2449be2ac88cSJonathan Lemon default: 2450be2ac88cSJonathan Lemon continue; 2451df8bae1dSRodney W. Grimes } 2452df8bae1dSRodney W. Grimes } 2453df8bae1dSRodney W. Grimes } 2454df8bae1dSRodney W. Grimes 2455df8bae1dSRodney W. Grimes /* 2456df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 2457df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 2458df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 2459df8bae1dSRodney W. Grimes * sequencing purposes. 2460df8bae1dSRodney W. Grimes */ 24610312fbe9SPoul-Henning Kamp static void 2462fb59c426SYoshinobu Inoue tcp_pulloutofband(so, th, m, off) 2463df8bae1dSRodney W. Grimes struct socket *so; 2464fb59c426SYoshinobu Inoue struct tcphdr *th; 2465df8bae1dSRodney W. Grimes register struct mbuf *m; 2466fb59c426SYoshinobu Inoue int off; /* delayed to be droped hdrlen */ 2467df8bae1dSRodney W. Grimes { 2468fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 2469df8bae1dSRodney W. Grimes 2470df8bae1dSRodney W. Grimes while (cnt >= 0) { 2471df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 2472df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 2473df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 2474df8bae1dSRodney W. Grimes 2475df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 2476df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 2477df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2478df8bae1dSRodney W. Grimes m->m_len--; 247969a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 248069a34685SYoshinobu Inoue m->m_pkthdr.len--; 2481df8bae1dSRodney W. Grimes return; 2482df8bae1dSRodney W. Grimes } 2483df8bae1dSRodney W. Grimes cnt -= m->m_len; 2484df8bae1dSRodney W. Grimes m = m->m_next; 2485df8bae1dSRodney W. Grimes if (m == 0) 2486df8bae1dSRodney W. Grimes break; 2487df8bae1dSRodney W. Grimes } 2488df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 2489df8bae1dSRodney W. Grimes } 2490df8bae1dSRodney W. Grimes 2491df8bae1dSRodney W. Grimes /* 2492df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 2493df8bae1dSRodney W. Grimes * and update averages and current timeout. 2494df8bae1dSRodney W. Grimes */ 24950312fbe9SPoul-Henning Kamp static void 2496df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt) 2497df8bae1dSRodney W. Grimes register struct tcpcb *tp; 24989b8b58e0SJonathan Lemon int rtt; 2499df8bae1dSRodney W. Grimes { 2500233e8c18SGarrett Wollman register int delta; 2501233e8c18SGarrett Wollman 2502233e8c18SGarrett Wollman tcpstat.tcps_rttupdated++; 2503233e8c18SGarrett Wollman tp->t_rttupdated++; 2504233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 2505233e8c18SGarrett Wollman /* 2506233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 2507233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 2508233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 2509233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2510233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 2511233e8c18SGarrett Wollman */ 2512233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2513233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2514233e8c18SGarrett Wollman 2515233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 2516233e8c18SGarrett Wollman tp->t_srtt = 1; 2517233e8c18SGarrett Wollman 2518233e8c18SGarrett Wollman /* 2519233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 2520233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 2521233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 2522233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 2523233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 2524233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 2525233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2526233e8c18SGarrett Wollman * rfc793's wired-in beta. 2527233e8c18SGarrett Wollman */ 2528233e8c18SGarrett Wollman if (delta < 0) 2529233e8c18SGarrett Wollman delta = -delta; 2530233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2531233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 2532233e8c18SGarrett Wollman tp->t_rttvar = 1; 25331fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 25341fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2535233e8c18SGarrett Wollman } else { 2536233e8c18SGarrett Wollman /* 2537233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 2538233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 2539233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 2540233e8c18SGarrett Wollman */ 2541233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 2542233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 25431fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2544233e8c18SGarrett Wollman } 25459b8b58e0SJonathan Lemon tp->t_rtttime = 0; 2546df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 2547df8bae1dSRodney W. Grimes 2548df8bae1dSRodney W. Grimes /* 2549df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 2550df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 2551df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 2552df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 2553df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 2554df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 2555df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 2556df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 2557df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 2558df8bae1dSRodney W. Grimes */ 2559233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 25609e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2561df8bae1dSRodney W. Grimes 2562df8bae1dSRodney W. Grimes /* 2563df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 2564df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 2565df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 2566df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 2567df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 2568df8bae1dSRodney W. Grimes */ 2569df8bae1dSRodney W. Grimes tp->t_softerror = 0; 2570df8bae1dSRodney W. Grimes } 2571df8bae1dSRodney W. Grimes 2572df8bae1dSRodney W. Grimes /* 2573df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 2574df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 2575df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 2576df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 2577df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2578df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 2579df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 2580df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 2581df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 2582df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 2583df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 2584df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 2585df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 2586a0292f23SGarrett Wollman * 2587a0292f23SGarrett Wollman * Also take into account the space needed for options that we 2588a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 2589a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 2590a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 2591a0292f23SGarrett Wollman * data in maxopd. 2592a0292f23SGarrett Wollman * 2593a0292f23SGarrett Wollman * NOTE that this routine is only called when we process an incoming 2594a0292f23SGarrett Wollman * segment, for outgoing segments only tcp_mssopt is called. 2595a0292f23SGarrett Wollman * 2596a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 2597a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 2598a0292f23SGarrett Wollman * MSS of our peer. 2599df8bae1dSRodney W. Grimes */ 2600a0292f23SGarrett Wollman void 2601df8bae1dSRodney W. Grimes tcp_mss(tp, offer) 2602a0292f23SGarrett Wollman struct tcpcb *tp; 2603a0292f23SGarrett Wollman int offer; 2604df8bae1dSRodney W. Grimes { 2605df8bae1dSRodney W. Grimes register struct rtentry *rt; 2606df8bae1dSRodney W. Grimes struct ifnet *ifp; 2607df8bae1dSRodney W. Grimes register int rtt, mss; 2608df8bae1dSRodney W. Grimes u_long bufsize; 2609c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 2610df8bae1dSRodney W. Grimes struct socket *so; 2611a0292f23SGarrett Wollman struct rmxp_tao *taop; 2612a0292f23SGarrett Wollman int origoffer = offer; 2613fb59c426SYoshinobu Inoue #ifdef INET6 2614c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2615c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 2616c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 2617c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 2618c068736aSJeffrey Hsu #else 2619c068736aSJeffrey Hsu const int isipv6 = 0; 2620c068736aSJeffrey Hsu const size_t min_protoh = sizeof (struct tcpiphdr); 2621fb59c426SYoshinobu Inoue #endif 2622df8bae1dSRodney W. Grimes 2623fb59c426SYoshinobu Inoue if (isipv6) 2624be2ac88cSJonathan Lemon rt = tcp_rtlookup6(&inp->inp_inc); 2625fb59c426SYoshinobu Inoue else 2626be2ac88cSJonathan Lemon rt = tcp_rtlookup(&inp->inp_inc); 2627fb59c426SYoshinobu Inoue if (rt == NULL) { 2628fb59c426SYoshinobu Inoue tp->t_maxopd = tp->t_maxseg = 2629c068736aSJeffrey Hsu isipv6 ? tcp_v6mssdflt : tcp_mssdflt; 2630a0292f23SGarrett Wollman return; 2631df8bae1dSRodney W. Grimes } 2632df8bae1dSRodney W. Grimes ifp = rt->rt_ifp; 2633df8bae1dSRodney W. Grimes so = inp->inp_socket; 2634df8bae1dSRodney W. Grimes 2635a0292f23SGarrett Wollman taop = rmx_taop(rt->rt_rmx); 2636a0292f23SGarrett Wollman /* 2637a0292f23SGarrett Wollman * Offer == -1 means that we didn't receive SYN yet, 2638a0292f23SGarrett Wollman * use cached value in that case; 2639a0292f23SGarrett Wollman */ 2640a0292f23SGarrett Wollman if (offer == -1) 2641a0292f23SGarrett Wollman offer = taop->tao_mssopt; 2642a0292f23SGarrett Wollman /* 2643a0292f23SGarrett Wollman * Offer == 0 means that there was no MSS on the SYN segment, 2644a0292f23SGarrett Wollman * in this case we use tcp_mssdflt. 2645a0292f23SGarrett Wollman */ 2646a0292f23SGarrett Wollman if (offer == 0) 2647c068736aSJeffrey Hsu offer = isipv6 ? tcp_v6mssdflt : tcp_mssdflt; 2648a0292f23SGarrett Wollman else 2649a0292f23SGarrett Wollman /* 2650a0292f23SGarrett Wollman * Sanity check: make sure that maxopd will be large 2651a0292f23SGarrett Wollman * enough to allow some data on segments even is the 2652a0292f23SGarrett Wollman * all the option space is used (40bytes). Otherwise 2653a0292f23SGarrett Wollman * funny things may happen in tcp_output. 2654a0292f23SGarrett Wollman */ 2655a0292f23SGarrett Wollman offer = max(offer, 64); 2656a0292f23SGarrett Wollman taop->tao_mssopt = offer; 2657a0292f23SGarrett Wollman 2658df8bae1dSRodney W. Grimes /* 2659df8bae1dSRodney W. Grimes * While we're here, check if there's an initial rtt 2660df8bae1dSRodney W. Grimes * or rttvar. Convert from the route-table units 2661df8bae1dSRodney W. Grimes * to scaled multiples of the slow timeout timer. 2662df8bae1dSRodney W. Grimes */ 2663df8bae1dSRodney W. Grimes if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 2664df8bae1dSRodney W. Grimes /* 2665a0292f23SGarrett Wollman * XXX the lock bit for RTT indicates that the value 2666df8bae1dSRodney W. Grimes * is also a minimum value; this is subject to time. 2667df8bae1dSRodney W. Grimes */ 2668df8bae1dSRodney W. Grimes if (rt->rt_rmx.rmx_locks & RTV_RTT) 26699b8b58e0SJonathan Lemon tp->t_rttmin = rtt / (RTM_RTTUNIT / hz); 26709b8b58e0SJonathan Lemon tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE)); 26711fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 2672dd224982SGarrett Wollman tcpstat.tcps_usedrtt++; 2673dd224982SGarrett Wollman if (rt->rt_rmx.rmx_rttvar) { 2674df8bae1dSRodney W. Grimes tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 26759b8b58e0SJonathan Lemon (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE)); 2676dd224982SGarrett Wollman tcpstat.tcps_usedrttvar++; 2677dd224982SGarrett Wollman } else { 2678df8bae1dSRodney W. Grimes /* default variation is +- 1 rtt */ 2679df8bae1dSRodney W. Grimes tp->t_rttvar = 2680df8bae1dSRodney W. Grimes tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 2681dd224982SGarrett Wollman } 2682df8bae1dSRodney W. Grimes TCPT_RANGESET(tp->t_rxtcur, 2683df8bae1dSRodney W. Grimes ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 2684df8bae1dSRodney W. Grimes tp->t_rttmin, TCPTV_REXMTMAX); 2685df8bae1dSRodney W. Grimes } 2686df8bae1dSRodney W. Grimes /* 2687df8bae1dSRodney W. Grimes * if there's an mtu associated with the route, use it 2688fb59c426SYoshinobu Inoue * else, use the link mtu. 2689df8bae1dSRodney W. Grimes */ 2690df8bae1dSRodney W. Grimes if (rt->rt_rmx.rmx_mtu) 2691fb59c426SYoshinobu Inoue mss = rt->rt_rmx.rmx_mtu - min_protoh; 2692c068736aSJeffrey Hsu else { 2693fb59c426SYoshinobu Inoue if (isipv6) { 2694c068736aSJeffrey Hsu mss = nd_ifinfo[rt->rt_ifp->if_index].linkmtu - 2695c068736aSJeffrey Hsu min_protoh; 2696fb59c426SYoshinobu Inoue if (!in6_localaddr(&inp->in6p_faddr)) 2697fb59c426SYoshinobu Inoue mss = min(mss, tcp_v6mssdflt); 2698c068736aSJeffrey Hsu } else { 2699c068736aSJeffrey Hsu mss = ifp->if_mtu - min_protoh; 2700a0292f23SGarrett Wollman if (!in_localaddr(inp->inp_faddr)) 2701a0292f23SGarrett Wollman mss = min(mss, tcp_mssdflt); 2702a0292f23SGarrett Wollman } 2703c068736aSJeffrey Hsu } 2704a0292f23SGarrett Wollman mss = min(mss, offer); 2705a0292f23SGarrett Wollman /* 2706a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 2707a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 2708a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 2709a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 2710a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 2711a0292f23SGarrett Wollman */ 2712a0292f23SGarrett Wollman tp->t_maxopd = mss; 2713a0292f23SGarrett Wollman 2714a0292f23SGarrett Wollman /* 2715a0292f23SGarrett Wollman * In case of T/TCP, origoffer==-1 indicates, that no segments 2716a0292f23SGarrett Wollman * were received yet. In this case we just guess, otherwise 2717a0292f23SGarrett Wollman * we do the same as before T/TCP. 2718a0292f23SGarrett Wollman */ 2719a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2720a0292f23SGarrett Wollman (origoffer == -1 || 2721a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2722a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 2723a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 2724a0292f23SGarrett Wollman (origoffer == -1 || 2725a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)) 2726a0292f23SGarrett Wollman mss -= TCPOLEN_CC_APPA; 2727a0292f23SGarrett Wollman 2728df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 2729df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2730df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 2731df8bae1dSRodney W. Grimes #else 2732df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2733df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 2734df8bae1dSRodney W. Grimes #endif 2735df8bae1dSRodney W. Grimes /* 2736df8bae1dSRodney W. Grimes * If there's a pipesize, change the socket buffer 2737df8bae1dSRodney W. Grimes * to that size. Make the socket buffers an integral 2738df8bae1dSRodney W. Grimes * number of mss units; if the mss is larger than 2739df8bae1dSRodney W. Grimes * the socket buffer, decrease the mss. 2740df8bae1dSRodney W. Grimes */ 2741df8bae1dSRodney W. Grimes #ifdef RTV_SPIPE 2742df8bae1dSRodney W. Grimes if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 2743df8bae1dSRodney W. Grimes #endif 2744df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 2745df8bae1dSRodney W. Grimes if (bufsize < mss) 2746df8bae1dSRodney W. Grimes mss = bufsize; 2747df8bae1dSRodney W. Grimes else { 2748df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2749df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2750df8bae1dSRodney W. Grimes bufsize = sb_max; 275188c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 2752ecf72308SBrian Feldman (void)sbreserve(&so->so_snd, bufsize, so, NULL); 2753df8bae1dSRodney W. Grimes } 2754df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 2755df8bae1dSRodney W. Grimes 2756df8bae1dSRodney W. Grimes #ifdef RTV_RPIPE 2757df8bae1dSRodney W. Grimes if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 2758df8bae1dSRodney W. Grimes #endif 2759df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 2760df8bae1dSRodney W. Grimes if (bufsize > mss) { 2761df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2762df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2763df8bae1dSRodney W. Grimes bufsize = sb_max; 276488c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 2765ecf72308SBrian Feldman (void)sbreserve(&so->so_rcv, bufsize, so, NULL); 2766df8bae1dSRodney W. Grimes } 27679b8b58e0SJonathan Lemon 2768a0292f23SGarrett Wollman /* 27699b8b58e0SJonathan Lemon * Set the slow-start flight size depending on whether this 27709b8b58e0SJonathan Lemon * is a local network or not. 2771a0292f23SGarrett Wollman */ 2772da3a8a1aSJeffrey Hsu if (tcp_do_rfc3390) 2773da3a8a1aSJeffrey Hsu tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380)); 2774da3a8a1aSJeffrey Hsu else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) || 2775c068736aSJeffrey Hsu (!isipv6 && in_localaddr(inp->inp_faddr))) 27769b8b58e0SJonathan Lemon tp->snd_cwnd = mss * ss_fltsz_local; 27779b8b58e0SJonathan Lemon else 27789b8b58e0SJonathan Lemon tp->snd_cwnd = mss * ss_fltsz; 2779df8bae1dSRodney W. Grimes 2780df8bae1dSRodney W. Grimes if (rt->rt_rmx.rmx_ssthresh) { 2781df8bae1dSRodney W. Grimes /* 2782df8bae1dSRodney W. Grimes * There's some sort of gateway or interface 2783df8bae1dSRodney W. Grimes * buffer limit on the path. Use this to set 2784df8bae1dSRodney W. Grimes * the slow start threshhold, but set the 2785df8bae1dSRodney W. Grimes * threshold to no less than 2*mss. 2786df8bae1dSRodney W. Grimes */ 2787df8bae1dSRodney W. Grimes tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 2788dd224982SGarrett Wollman tcpstat.tcps_usedssthresh++; 2789df8bae1dSRodney W. Grimes } 2790a0292f23SGarrett Wollman } 2791a0292f23SGarrett Wollman 2792a0292f23SGarrett Wollman /* 2793a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 2794a0292f23SGarrett Wollman */ 2795a0292f23SGarrett Wollman int 2796a0292f23SGarrett Wollman tcp_mssopt(tp) 2797a0292f23SGarrett Wollman struct tcpcb *tp; 2798a0292f23SGarrett Wollman { 2799a0292f23SGarrett Wollman struct rtentry *rt; 2800fb59c426SYoshinobu Inoue #ifdef INET6 2801c068736aSJeffrey Hsu int isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2802c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 2803c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 2804c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 2805c068736aSJeffrey Hsu #else 2806c068736aSJeffrey Hsu const int isipv6 = 0; 2807c068736aSJeffrey Hsu const size_t min_protoh = sizeof (struct tcpiphdr); 2808fb59c426SYoshinobu Inoue #endif 2809a0292f23SGarrett Wollman 2810fb59c426SYoshinobu Inoue if (isipv6) 2811be2ac88cSJonathan Lemon rt = tcp_rtlookup6(&tp->t_inpcb->inp_inc); 2812fb59c426SYoshinobu Inoue else 2813be2ac88cSJonathan Lemon rt = tcp_rtlookup(&tp->t_inpcb->inp_inc); 2814a0292f23SGarrett Wollman if (rt == NULL) 2815c068736aSJeffrey Hsu return (isipv6 ? tcp_v6mssdflt : tcp_mssdflt); 2816a0292f23SGarrett Wollman 2817c068736aSJeffrey Hsu return (rt->rt_ifp->if_mtu - min_protoh); 2818df8bae1dSRodney W. Grimes } 281946f58482SJonathan Lemon 282046f58482SJonathan Lemon 282146f58482SJonathan Lemon /* 2822c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 2823c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 2824c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 2825c068736aSJeffrey Hsu * be started again. 282646f58482SJonathan Lemon */ 2827c068736aSJeffrey Hsu static void 2828c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th) 282946f58482SJonathan Lemon struct tcpcb *tp; 283046f58482SJonathan Lemon struct tcphdr *th; 283146f58482SJonathan Lemon { 283246f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 283346f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 283446f58482SJonathan Lemon 283546f58482SJonathan Lemon callout_stop(tp->tt_rexmt); 283646f58482SJonathan Lemon tp->t_rtttime = 0; 283746f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 28386b2a5f92SJayanth Vijayaraghavan /* 2839c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 2840c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 28416b2a5f92SJayanth Vijayaraghavan */ 28426b2a5f92SJayanth Vijayaraghavan tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 2843a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 28446b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 284546f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 284646f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 284746f58482SJonathan Lemon tp->snd_nxt = onxt; 284846f58482SJonathan Lemon /* 284946f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 285046f58482SJonathan Lemon * not updated yet. 285146f58482SJonathan Lemon */ 285246f58482SJonathan Lemon tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg); 285346f58482SJonathan Lemon } 2854340c35deSJonathan Lemon 2855340c35deSJonathan Lemon /* 2856340c35deSJonathan Lemon * Returns 1 if the TIME_WAIT state was killed and we should start over, 2857340c35deSJonathan Lemon * looking for a pcb in the listen state. Returns 0 otherwise. 2858340c35deSJonathan Lemon */ 2859340c35deSJonathan Lemon static int 2860340c35deSJonathan Lemon tcp_timewait(tw, to, th, m, tlen) 2861340c35deSJonathan Lemon struct tcptw *tw; 2862340c35deSJonathan Lemon struct tcpopt *to; 2863340c35deSJonathan Lemon struct tcphdr *th; 2864340c35deSJonathan Lemon struct mbuf *m; 2865340c35deSJonathan Lemon int tlen; 2866340c35deSJonathan Lemon { 2867340c35deSJonathan Lemon int thflags; 2868340c35deSJonathan Lemon tcp_seq seq; 2869340c35deSJonathan Lemon #ifdef INET6 2870340c35deSJonathan Lemon int isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 2871340c35deSJonathan Lemon #else 2872340c35deSJonathan Lemon const int isipv6 = 0; 2873340c35deSJonathan Lemon #endif 2874340c35deSJonathan Lemon 2875340c35deSJonathan Lemon thflags = th->th_flags; 2876340c35deSJonathan Lemon 2877340c35deSJonathan Lemon /* 2878340c35deSJonathan Lemon * NOTE: for FIN_WAIT_2 (to be added later), 2879340c35deSJonathan Lemon * must validate sequence number before accepting RST 2880340c35deSJonathan Lemon */ 2881340c35deSJonathan Lemon 2882340c35deSJonathan Lemon /* 2883340c35deSJonathan Lemon * If the segment contains RST: 2884340c35deSJonathan Lemon * Drop the segment - see Stevens, vol. 2, p. 964 and 2885340c35deSJonathan Lemon * RFC 1337. 2886340c35deSJonathan Lemon */ 2887340c35deSJonathan Lemon if (thflags & TH_RST) 2888340c35deSJonathan Lemon goto drop; 2889340c35deSJonathan Lemon 2890340c35deSJonathan Lemon /* 2891340c35deSJonathan Lemon * If segment contains a SYN and CC [not CC.NEW] option: 2892340c35deSJonathan Lemon * if connection duration > MSL, drop packet and send RST; 2893340c35deSJonathan Lemon * 2894340c35deSJonathan Lemon * if SEG.CC > CCrecv then is new SYN. 2895340c35deSJonathan Lemon * Complete close and delete TCPCB. Then reprocess 2896340c35deSJonathan Lemon * segment, hoping to find new TCPCB in LISTEN state; 2897340c35deSJonathan Lemon * 2898340c35deSJonathan Lemon * else must be old SYN; drop it. 2899340c35deSJonathan Lemon * else do normal processing. 2900340c35deSJonathan Lemon */ 2901340c35deSJonathan Lemon if ((thflags & TH_SYN) && (to->to_flags & TOF_CC) && tw->cc_recv != 0) { 2902340c35deSJonathan Lemon if ((ticks - tw->t_starttime) > tcp_msl) 2903340c35deSJonathan Lemon goto reset; 2904340c35deSJonathan Lemon if (CC_GT(to->to_cc, tw->cc_recv)) { 2905607b0b0cSJonathan Lemon (void) tcp_twclose(tw, 0); 2906340c35deSJonathan Lemon return (1); 2907340c35deSJonathan Lemon } 2908340c35deSJonathan Lemon goto drop; 2909340c35deSJonathan Lemon } 2910340c35deSJonathan Lemon 2911340c35deSJonathan Lemon #if 0 2912340c35deSJonathan Lemon /* PAWS not needed at the moment */ 2913340c35deSJonathan Lemon /* 2914340c35deSJonathan Lemon * RFC 1323 PAWS: If we have a timestamp reply on this segment 2915340c35deSJonathan Lemon * and it's less than ts_recent, drop it. 2916340c35deSJonathan Lemon */ 2917340c35deSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 2918340c35deSJonathan Lemon TSTMP_LT(to.to_tsval, tp->ts_recent)) { 2919340c35deSJonathan Lemon if ((thflags & TH_ACK) == 0) 2920340c35deSJonathan Lemon goto drop; 2921340c35deSJonathan Lemon goto ack; 2922340c35deSJonathan Lemon } 2923340c35deSJonathan Lemon /* 2924340c35deSJonathan Lemon * ts_recent is never updated because we never accept new segments. 2925340c35deSJonathan Lemon */ 2926340c35deSJonathan Lemon #endif 2927340c35deSJonathan Lemon 2928340c35deSJonathan Lemon /* 2929340c35deSJonathan Lemon * If a new connection request is received 2930340c35deSJonathan Lemon * while in TIME_WAIT, drop the old connection 2931340c35deSJonathan Lemon * and start over if the sequence numbers 2932340c35deSJonathan Lemon * are above the previous ones. 2933340c35deSJonathan Lemon */ 2934340c35deSJonathan Lemon if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) { 2935607b0b0cSJonathan Lemon (void) tcp_twclose(tw, 0); 2936340c35deSJonathan Lemon return (1); 2937340c35deSJonathan Lemon } 2938340c35deSJonathan Lemon 2939340c35deSJonathan Lemon /* 2940340c35deSJonathan Lemon * Drop the the segment if it does not contain an ACK. 2941340c35deSJonathan Lemon */ 2942340c35deSJonathan Lemon if ((thflags & TH_ACK) == 0) 2943340c35deSJonathan Lemon goto drop; 2944340c35deSJonathan Lemon 2945340c35deSJonathan Lemon /* 2946340c35deSJonathan Lemon * Reset the 2MSL timer if this is a duplicate FIN. 2947340c35deSJonathan Lemon */ 2948340c35deSJonathan Lemon if (thflags & TH_FIN) { 2949340c35deSJonathan Lemon seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0); 2950340c35deSJonathan Lemon if (seq + 1 == tw->rcv_nxt) 2951607b0b0cSJonathan Lemon tcp_timer_2msl_reset(tw, 2 * tcp_msl); 2952340c35deSJonathan Lemon } 2953340c35deSJonathan Lemon 2954340c35deSJonathan Lemon /* 2955272c5dfeSJonathan Lemon * Acknowledge the segment if it has data or is not a duplicate ACK. 2956340c35deSJonathan Lemon */ 2957272c5dfeSJonathan Lemon if (thflags != TH_ACK || tlen != 0 || 2958272c5dfeSJonathan Lemon th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt) 2959430c6354SRobert Watson tcp_twrespond(tw, NULL, m, TH_ACK); 2960340c35deSJonathan Lemon goto drop; 2961340c35deSJonathan Lemon 2962340c35deSJonathan Lemon reset: 2963340c35deSJonathan Lemon /* 2964340c35deSJonathan Lemon * Generate a RST, dropping incoming segment. 2965340c35deSJonathan Lemon * Make ACK acceptable to originator of segment. 2966340c35deSJonathan Lemon * Don't bother to respond if destination was broadcast/multicast. 2967340c35deSJonathan Lemon */ 2968340c35deSJonathan Lemon if (m->m_flags & (M_BCAST|M_MCAST)) 2969340c35deSJonathan Lemon goto drop; 2970340c35deSJonathan Lemon if (isipv6) { 2971340c35deSJonathan Lemon struct ip6_hdr *ip6; 2972340c35deSJonathan Lemon 2973340c35deSJonathan Lemon /* IPv6 anycast check is done at tcp6_input() */ 2974340c35deSJonathan Lemon ip6 = mtod(m, struct ip6_hdr *); 2975340c35deSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2976340c35deSJonathan Lemon IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2977340c35deSJonathan Lemon goto drop; 2978340c35deSJonathan Lemon } else { 2979340c35deSJonathan Lemon struct ip *ip; 2980340c35deSJonathan Lemon 2981340c35deSJonathan Lemon ip = mtod(m, struct ip *); 2982340c35deSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2983340c35deSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2984340c35deSJonathan Lemon ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 2985340c35deSJonathan Lemon in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2986340c35deSJonathan Lemon goto drop; 2987340c35deSJonathan Lemon } 2988340c35deSJonathan Lemon if (thflags & TH_ACK) { 2989340c35deSJonathan Lemon tcp_respond(NULL, 2990340c35deSJonathan Lemon mtod(m, void *), th, m, 0, th->th_ack, TH_RST); 2991340c35deSJonathan Lemon } else { 2992340c35deSJonathan Lemon seq = th->th_seq + (thflags & TH_SYN ? 1 : 0); 2993340c35deSJonathan Lemon tcp_respond(NULL, 2994340c35deSJonathan Lemon mtod(m, void *), th, m, seq, 0, TH_RST|TH_ACK); 2995340c35deSJonathan Lemon } 2996340c35deSJonathan Lemon INP_UNLOCK(tw->tw_inpcb); 2997340c35deSJonathan Lemon return (0); 2998340c35deSJonathan Lemon 2999340c35deSJonathan Lemon drop: 3000340c35deSJonathan Lemon INP_UNLOCK(tw->tw_inpcb); 3001340c35deSJonathan Lemon m_freem(m); 3002340c35deSJonathan Lemon return (0); 3003340c35deSJonathan Lemon } 3004