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 */ 380cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 39e46cd3d4SDag-Erling Smørgrav #include "opt_tcp_input.h" 400cc12cc5SJoerg Wunsch 41df8bae1dSRodney W. Grimes #include <sys/param.h> 42df8bae1dSRodney W. Grimes #include <sys/systm.h> 4398163b98SPoul-Henning Kamp #include <sys/kernel.h> 4498163b98SPoul-Henning Kamp #include <sys/sysctl.h> 45df8bae1dSRodney W. Grimes #include <sys/malloc.h> 46df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 47a29f300eSGarrett Wollman #include <sys/proc.h> /* for proc0 declaration */ 48df8bae1dSRodney W. Grimes #include <sys/protosw.h> 49df8bae1dSRodney W. Grimes #include <sys/socket.h> 50df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 51816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 52df8bae1dSRodney W. Grimes 53e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 54e79adb8eSGarrett Wollman 55df8bae1dSRodney W. Grimes #include <net/if.h> 56df8bae1dSRodney W. Grimes #include <net/route.h> 57df8bae1dSRodney W. Grimes 58df8bae1dSRodney W. Grimes #include <netinet/in.h> 59df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 60df8bae1dSRodney W. Grimes #include <netinet/ip.h> 6151508de1SMatthew Dillon #include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */ 62df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 63df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 6451508de1SMatthew Dillon #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 65df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 66df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 67df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 68df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 69df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 70df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 71610ee2f9SDavid Greenman #ifdef TCPDEBUG 72df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 73f708ef1bSPoul-Henning Kamp static struct tcpiphdr tcp_saveti; 74610ee2f9SDavid Greenman #endif 75df8bae1dSRodney W. Grimes 760312fbe9SPoul-Henning Kamp static int tcprexmtthresh = 3; 772f96f1f4SGarrett Wollman tcp_seq tcp_iss; 782f96f1f4SGarrett Wollman tcp_cc tcp_ccgen; 790312fbe9SPoul-Henning Kamp 802f96f1f4SGarrett Wollman struct tcpstat tcpstat; 813d177f46SBill Fumerola SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RD, 823d177f46SBill Fumerola &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 830312fbe9SPoul-Henning Kamp 84d78a37adSPaul Traina static int log_in_vain = 0; 85816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 863d177f46SBill Fumerola &log_in_vain, 0, "Log all incoming TCP connections"); 87816a3d83SPoul-Henning Kamp 8816f7f31fSGeoff Rehmet static int blackhole = 0; 8916f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 9016f7f31fSGeoff Rehmet &blackhole, 0, "Do not send RST when dropping refused connections"); 9116f7f31fSGeoff Rehmet 92f498eeeeSDavid Greenman int tcp_delack_enabled = 1; 9384e33c9eSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 943d177f46SBill Fumerola &tcp_delack_enabled, 0, 953d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 96f498eeeeSDavid Greenman 97e46cd3d4SDag-Erling Smørgrav #ifdef TCP_RESTRICT_RST 98e46cd3d4SDag-Erling Smørgrav static int restrict_rst = 0; 99e46cd3d4SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, restrict_rst, CTLFLAG_RW, 100e46cd3d4SDag-Erling Smørgrav &restrict_rst, 0, "Restrict RST emission"); 101e46cd3d4SDag-Erling Smørgrav #endif 102e46cd3d4SDag-Erling Smørgrav 103e46cd3d4SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN 104e46cd3d4SDag-Erling Smørgrav static int drop_synfin = 0; 105e46cd3d4SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, 106e46cd3d4SDag-Erling Smørgrav &drop_synfin, 0, "Drop TCP packets with FIN+ACK set"); 107e46cd3d4SDag-Erling Smørgrav #endif 108e46cd3d4SDag-Erling Smørgrav 10915bd2b43SDavid Greenman struct inpcbhead tcb; 11015bd2b43SDavid Greenman struct inpcbinfo tcbinfo; 111df8bae1dSRodney W. Grimes 1120312fbe9SPoul-Henning Kamp static void tcp_dooptions __P((struct tcpcb *, 1130312fbe9SPoul-Henning Kamp u_char *, int, struct tcpiphdr *, struct tcpopt *)); 1140312fbe9SPoul-Henning Kamp static void tcp_pulloutofband __P((struct socket *, 1150312fbe9SPoul-Henning Kamp struct tcpiphdr *, struct mbuf *)); 1160312fbe9SPoul-Henning Kamp static int tcp_reass __P((struct tcpcb *, struct tcpiphdr *, struct mbuf *)); 1170312fbe9SPoul-Henning Kamp static void tcp_xmit_timer __P((struct tcpcb *, int)); 1180312fbe9SPoul-Henning Kamp 119df8bae1dSRodney W. Grimes 120df8bae1dSRodney W. Grimes /* 121df8bae1dSRodney W. Grimes * Insert segment ti into reassembly queue of tcp with 122df8bae1dSRodney W. Grimes * control block tp. Return TH_FIN if reassembly now includes 123df8bae1dSRodney W. Grimes * a segment with FIN. The macro form does the common case inline 124df8bae1dSRodney W. Grimes * (segment is the next to be received on an established connection, 125df8bae1dSRodney W. Grimes * and the queue is empty), avoiding linkage into and removal 126df8bae1dSRodney W. Grimes * from the queue and repetition of various conversions. 127df8bae1dSRodney W. Grimes * Set DELACK for segments received in order, but ack immediately 128df8bae1dSRodney W. Grimes * when segments are out of order (so fast retransmit can work). 129df8bae1dSRodney W. Grimes */ 1306b067b07SDavid Greenman #define TCP_REASS(tp, ti, m, so, flags) { \ 1316b067b07SDavid Greenman if ((ti)->ti_seq == (tp)->rcv_nxt && \ 1326effc713SDoug Rabson (tp)->t_segq == NULL && \ 1336b067b07SDavid Greenman (tp)->t_state == TCPS_ESTABLISHED) { \ 134f498eeeeSDavid Greenman if (tcp_delack_enabled) \ 1359b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, \ 1369b8b58e0SJonathan Lemon tcp_timer_delack, tp); \ 137f498eeeeSDavid Greenman else \ 138f498eeeeSDavid Greenman tp->t_flags |= TF_ACKNOW; \ 1396b067b07SDavid Greenman (tp)->rcv_nxt += (ti)->ti_len; \ 1406b067b07SDavid Greenman flags = (ti)->ti_flags & TH_FIN; \ 1416b067b07SDavid Greenman tcpstat.tcps_rcvpack++;\ 1426b067b07SDavid Greenman tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 1436b067b07SDavid Greenman sbappend(&(so)->so_rcv, (m)); \ 1446b067b07SDavid Greenman sorwakeup(so); \ 1456b067b07SDavid Greenman } else { \ 1466b067b07SDavid Greenman (flags) = tcp_reass((tp), (ti), (m)); \ 1476b067b07SDavid Greenman tp->t_flags |= TF_ACKNOW; \ 1486b067b07SDavid Greenman } \ 1496b067b07SDavid Greenman } 150df8bae1dSRodney W. Grimes 1510312fbe9SPoul-Henning Kamp static int 152df8bae1dSRodney W. Grimes tcp_reass(tp, ti, m) 153df8bae1dSRodney W. Grimes register struct tcpcb *tp; 154df8bae1dSRodney W. Grimes register struct tcpiphdr *ti; 155df8bae1dSRodney W. Grimes struct mbuf *m; 156df8bae1dSRodney W. Grimes { 1576effc713SDoug Rabson struct mbuf *q; 1586effc713SDoug Rabson struct mbuf *p; 1596effc713SDoug Rabson struct mbuf *nq; 160df8bae1dSRodney W. Grimes struct socket *so = tp->t_inpcb->inp_socket; 161df8bae1dSRodney W. Grimes int flags; 162df8bae1dSRodney W. Grimes 1636effc713SDoug Rabson #define GETTCP(m) ((struct tcpiphdr *)m->m_pkthdr.header) 1646effc713SDoug Rabson 165df8bae1dSRodney W. Grimes /* 166df8bae1dSRodney W. Grimes * Call with ti==0 after become established to 167df8bae1dSRodney W. Grimes * force pre-ESTABLISHED data up to user socket. 168df8bae1dSRodney W. Grimes */ 169df8bae1dSRodney W. Grimes if (ti == 0) 170df8bae1dSRodney W. Grimes goto present; 171df8bae1dSRodney W. Grimes 1726effc713SDoug Rabson m->m_pkthdr.header = ti; 1736effc713SDoug Rabson 174df8bae1dSRodney W. Grimes /* 175df8bae1dSRodney W. Grimes * Find a segment which begins after this one does. 176df8bae1dSRodney W. Grimes */ 1776effc713SDoug Rabson for (q = tp->t_segq, p = NULL; q; p = q, q = q->m_nextpkt) 1786effc713SDoug Rabson if (SEQ_GT(GETTCP(q)->ti_seq, ti->ti_seq)) 179df8bae1dSRodney W. Grimes break; 180df8bae1dSRodney W. Grimes 181df8bae1dSRodney W. Grimes /* 182df8bae1dSRodney W. Grimes * If there is a preceding segment, it may provide some of 183df8bae1dSRodney W. Grimes * our data already. If so, drop the data from the incoming 184df8bae1dSRodney W. Grimes * segment. If it provides all of our data, drop us. 185df8bae1dSRodney W. Grimes */ 1866effc713SDoug Rabson if (p != NULL) { 187df8bae1dSRodney W. Grimes register int i; 188df8bae1dSRodney W. Grimes /* conversion to int (in i) handles seq wraparound */ 1896effc713SDoug Rabson i = GETTCP(p)->ti_seq + GETTCP(p)->ti_len - ti->ti_seq; 190df8bae1dSRodney W. Grimes if (i > 0) { 191df8bae1dSRodney W. Grimes if (i >= ti->ti_len) { 192df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 193df8bae1dSRodney W. Grimes tcpstat.tcps_rcvdupbyte += ti->ti_len; 194df8bae1dSRodney W. Grimes m_freem(m); 195a0292f23SGarrett Wollman /* 196a0292f23SGarrett Wollman * Try to present any queued data 197a0292f23SGarrett Wollman * at the left window edge to the user. 198a0292f23SGarrett Wollman * This is needed after the 3-WHS 199a0292f23SGarrett Wollman * completes. 200a0292f23SGarrett Wollman */ 201a0292f23SGarrett Wollman goto present; /* ??? */ 202df8bae1dSRodney W. Grimes } 203df8bae1dSRodney W. Grimes m_adj(m, i); 204df8bae1dSRodney W. Grimes ti->ti_len -= i; 205df8bae1dSRodney W. Grimes ti->ti_seq += i; 206df8bae1dSRodney W. Grimes } 207df8bae1dSRodney W. Grimes } 208df8bae1dSRodney W. Grimes tcpstat.tcps_rcvoopack++; 209df8bae1dSRodney W. Grimes tcpstat.tcps_rcvoobyte += ti->ti_len; 210df8bae1dSRodney W. Grimes 211df8bae1dSRodney W. Grimes /* 212df8bae1dSRodney W. Grimes * While we overlap succeeding segments trim them or, 213df8bae1dSRodney W. Grimes * if they are completely covered, dequeue them. 214df8bae1dSRodney W. Grimes */ 2156effc713SDoug Rabson while (q) { 2166effc713SDoug Rabson register int i = (ti->ti_seq + ti->ti_len) - GETTCP(q)->ti_seq; 217df8bae1dSRodney W. Grimes if (i <= 0) 218df8bae1dSRodney W. Grimes break; 2196effc713SDoug Rabson if (i < GETTCP(q)->ti_len) { 2206effc713SDoug Rabson GETTCP(q)->ti_seq += i; 2216effc713SDoug Rabson GETTCP(q)->ti_len -= i; 2226effc713SDoug Rabson m_adj(q, i); 223df8bae1dSRodney W. Grimes break; 224df8bae1dSRodney W. Grimes } 2256effc713SDoug Rabson 2266effc713SDoug Rabson nq = q->m_nextpkt; 2276effc713SDoug Rabson if (p) 2286effc713SDoug Rabson p->m_nextpkt = nq; 2296effc713SDoug Rabson else 2306effc713SDoug Rabson tp->t_segq = nq; 2316effc713SDoug Rabson m_freem(q); 2326effc713SDoug Rabson q = nq; 233df8bae1dSRodney W. Grimes } 234df8bae1dSRodney W. Grimes 2356effc713SDoug Rabson if (p == NULL) { 2366effc713SDoug Rabson m->m_nextpkt = tp->t_segq; 2376effc713SDoug Rabson tp->t_segq = m; 2386effc713SDoug Rabson } else { 2396effc713SDoug Rabson m->m_nextpkt = p->m_nextpkt; 2406effc713SDoug Rabson p->m_nextpkt = m; 2416effc713SDoug Rabson } 242df8bae1dSRodney W. Grimes 243df8bae1dSRodney W. Grimes present: 244df8bae1dSRodney W. Grimes /* 245df8bae1dSRodney W. Grimes * Present data to user, advancing rcv_nxt through 246df8bae1dSRodney W. Grimes * completed sequence space. 247df8bae1dSRodney W. Grimes */ 248a0292f23SGarrett Wollman if (!TCPS_HAVEESTABLISHED(tp->t_state)) 249df8bae1dSRodney W. Grimes return (0); 2506effc713SDoug Rabson q = tp->t_segq; 2516effc713SDoug Rabson if (!q || GETTCP(q)->ti_seq != tp->rcv_nxt) 252df8bae1dSRodney W. Grimes return (0); 253df8bae1dSRodney W. Grimes do { 2546effc713SDoug Rabson tp->rcv_nxt += GETTCP(q)->ti_len; 2556effc713SDoug Rabson flags = GETTCP(q)->ti_flags & TH_FIN; 2566effc713SDoug Rabson nq = q->m_nextpkt; 2576effc713SDoug Rabson tp->t_segq = nq; 2586effc713SDoug Rabson q->m_nextpkt = NULL; 259df8bae1dSRodney W. Grimes if (so->so_state & SS_CANTRCVMORE) 2606effc713SDoug Rabson m_freem(q); 261df8bae1dSRodney W. Grimes else 2626effc713SDoug Rabson sbappend(&so->so_rcv, q); 2636effc713SDoug Rabson q = nq; 2646effc713SDoug Rabson } while (q && GETTCP(q)->ti_seq == tp->rcv_nxt); 265df8bae1dSRodney W. Grimes sorwakeup(so); 266df8bae1dSRodney W. Grimes return (flags); 2676effc713SDoug Rabson 2686effc713SDoug Rabson #undef GETTCP 269df8bae1dSRodney W. Grimes } 270df8bae1dSRodney W. Grimes 271df8bae1dSRodney W. Grimes /* 272df8bae1dSRodney W. Grimes * TCP input routine, follows pages 65-76 of the 273df8bae1dSRodney W. Grimes * protocol specification dated September, 1981 very closely. 274df8bae1dSRodney W. Grimes */ 275df8bae1dSRodney W. Grimes void 276df8bae1dSRodney W. Grimes tcp_input(m, iphlen) 277df8bae1dSRodney W. Grimes register struct mbuf *m; 278df8bae1dSRodney W. Grimes int iphlen; 279df8bae1dSRodney W. Grimes { 280df8bae1dSRodney W. Grimes register struct tcpiphdr *ti; 281df8bae1dSRodney W. Grimes register struct inpcb *inp; 282e79adb8eSGarrett Wollman u_char *optp = NULL; 28326f9a767SRodney W. Grimes int optlen = 0; 284df8bae1dSRodney W. Grimes int len, tlen, off; 285df8bae1dSRodney W. Grimes register struct tcpcb *tp = 0; 286df8bae1dSRodney W. Grimes register int tiflags; 28726f9a767SRodney W. Grimes struct socket *so = 0; 288df8bae1dSRodney W. Grimes int todrop, acked, ourfinisacked, needoutput = 0; 289df8bae1dSRodney W. Grimes struct in_addr laddr; 290df8bae1dSRodney W. Grimes int dropsocket = 0; 291df8bae1dSRodney W. Grimes int iss = 0; 292a0292f23SGarrett Wollman u_long tiwin; 293a0292f23SGarrett Wollman struct tcpopt to; /* options in this segment */ 294a0292f23SGarrett Wollman struct rmxp_tao *taop; /* pointer to our TAO cache entry */ 295a0292f23SGarrett Wollman struct rmxp_tao tao_noncached; /* in case there's no cached entry */ 296610ee2f9SDavid Greenman #ifdef TCPDEBUG 297610ee2f9SDavid Greenman short ostate = 0; 298610ee2f9SDavid Greenman #endif 299df8bae1dSRodney W. Grimes 300a0292f23SGarrett Wollman bzero((char *)&to, sizeof(to)); 301a0292f23SGarrett Wollman 302df8bae1dSRodney W. Grimes tcpstat.tcps_rcvtotal++; 303df8bae1dSRodney W. Grimes /* 304df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 305df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 306df8bae1dSRodney W. Grimes */ 307df8bae1dSRodney W. Grimes ti = mtod(m, struct tcpiphdr *); 308df8bae1dSRodney W. Grimes if (iphlen > sizeof (struct ip)) 309df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 310df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 311df8bae1dSRodney W. Grimes if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 312df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 313df8bae1dSRodney W. Grimes return; 314df8bae1dSRodney W. Grimes } 315df8bae1dSRodney W. Grimes ti = mtod(m, struct tcpiphdr *); 316df8bae1dSRodney W. Grimes } 317df8bae1dSRodney W. Grimes 318df8bae1dSRodney W. Grimes /* 319df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 320df8bae1dSRodney W. Grimes */ 321df8bae1dSRodney W. Grimes tlen = ((struct ip *)ti)->ip_len; 322df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 3236effc713SDoug Rabson bzero(ti->ti_x1, sizeof(ti->ti_x1)); 324df8bae1dSRodney W. Grimes ti->ti_len = (u_short)tlen; 325df8bae1dSRodney W. Grimes HTONS(ti->ti_len); 326623ae52eSPoul-Henning Kamp ti->ti_sum = in_cksum(m, len); 327623ae52eSPoul-Henning Kamp if (ti->ti_sum) { 328df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadsum++; 329df8bae1dSRodney W. Grimes goto drop; 330df8bae1dSRodney W. Grimes } 331df8bae1dSRodney W. Grimes 332df8bae1dSRodney W. Grimes /* 333df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 334df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 335df8bae1dSRodney W. Grimes */ 336df8bae1dSRodney W. Grimes off = ti->ti_off << 2; 337df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 338df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadoff++; 339df8bae1dSRodney W. Grimes goto drop; 340df8bae1dSRodney W. Grimes } 341df8bae1dSRodney W. Grimes tlen -= off; 342df8bae1dSRodney W. Grimes ti->ti_len = tlen; 343df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 344df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 345df8bae1dSRodney W. Grimes if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 346df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 347df8bae1dSRodney W. Grimes return; 348df8bae1dSRodney W. Grimes } 349df8bae1dSRodney W. Grimes ti = mtod(m, struct tcpiphdr *); 350df8bae1dSRodney W. Grimes } 351df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 352e79adb8eSGarrett Wollman optp = mtod(m, u_char *) + sizeof (struct tcpiphdr); 353df8bae1dSRodney W. Grimes } 354df8bae1dSRodney W. Grimes tiflags = ti->ti_flags; 355df8bae1dSRodney W. Grimes 356e46cd3d4SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN 357e46cd3d4SDag-Erling Smørgrav /* 358e46cd3d4SDag-Erling Smørgrav * If the drop_synfin option is enabled, drop all packets with 359e46cd3d4SDag-Erling Smørgrav * both the SYN and FIN bits set. This prevents e.g. nmap from 360e46cd3d4SDag-Erling Smørgrav * identifying the TCP/IP stack. 361e46cd3d4SDag-Erling Smørgrav * 362e46cd3d4SDag-Erling Smørgrav * This is incompatible with RFC1644 extensions (T/TCP). 363e46cd3d4SDag-Erling Smørgrav */ 364e46cd3d4SDag-Erling Smørgrav if (drop_synfin && (tiflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN)) 365e46cd3d4SDag-Erling Smørgrav goto drop; 366e46cd3d4SDag-Erling Smørgrav #endif 367e46cd3d4SDag-Erling Smørgrav 368df8bae1dSRodney W. Grimes /* 369df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 370df8bae1dSRodney W. Grimes */ 371df8bae1dSRodney W. Grimes NTOHL(ti->ti_seq); 372df8bae1dSRodney W. Grimes NTOHL(ti->ti_ack); 373df8bae1dSRodney W. Grimes NTOHS(ti->ti_win); 374df8bae1dSRodney W. Grimes NTOHS(ti->ti_urp); 375df8bae1dSRodney W. Grimes 376df8bae1dSRodney W. Grimes /* 377755c1f07SAndras Olah * Drop TCP, IP headers and TCP options. 378755c1f07SAndras Olah */ 379755c1f07SAndras Olah m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 380755c1f07SAndras Olah m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 381755c1f07SAndras Olah 382755c1f07SAndras Olah /* 383df8bae1dSRodney W. Grimes * Locate pcb for segment. 384df8bae1dSRodney W. Grimes */ 385df8bae1dSRodney W. Grimes findpcb: 386f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD 387f9e354dfSJulian Elischer if (ip_fw_fwd_addr != NULL) { 388f9e354dfSJulian Elischer /* 389f9e354dfSJulian Elischer * Diverted. Pretend to be the destination. 390f9e354dfSJulian Elischer * already got one like this? 391f9e354dfSJulian Elischer */ 392f9e354dfSJulian Elischer inp = in_pcblookup_hash(&tcbinfo, ti->ti_src, ti->ti_sport, 393f9e354dfSJulian Elischer ti->ti_dst, ti->ti_dport, 0); 394f9e354dfSJulian Elischer if (!inp) { 395f9e354dfSJulian Elischer /* 396f9e354dfSJulian Elischer * No, then it's new. Try find the ambushing socket 397f9e354dfSJulian Elischer */ 398f9e354dfSJulian Elischer if (!ip_fw_fwd_addr->sin_port) { 399f9e354dfSJulian Elischer inp = in_pcblookup_hash(&tcbinfo, ti->ti_src, 400f9e354dfSJulian Elischer ti->ti_sport, ip_fw_fwd_addr->sin_addr, 401f9e354dfSJulian Elischer ti->ti_dport, 1); 402f9e354dfSJulian Elischer } else { 403f9e354dfSJulian Elischer inp = in_pcblookup_hash(&tcbinfo, 404f9e354dfSJulian Elischer ti->ti_src, ti->ti_sport, 405f9e354dfSJulian Elischer ip_fw_fwd_addr->sin_addr, 406f9e354dfSJulian Elischer ntohs(ip_fw_fwd_addr->sin_port), 1); 407f9e354dfSJulian Elischer } 408f9e354dfSJulian Elischer } 409f9e354dfSJulian Elischer ip_fw_fwd_addr = NULL; 410f9e354dfSJulian Elischer } else 411f9e354dfSJulian Elischer #endif /* IPFIREWALL_FORWARD */ 412f9e354dfSJulian Elischer 413c3229e05SDavid Greenman inp = in_pcblookup_hash(&tcbinfo, ti->ti_src, ti->ti_sport, 4146d6a026bSDavid Greenman ti->ti_dst, ti->ti_dport, 1); 415df8bae1dSRodney W. Grimes 416df8bae1dSRodney W. Grimes /* 417df8bae1dSRodney W. Grimes * If the state is CLOSED (i.e., TCB does not exist) then 418df8bae1dSRodney W. Grimes * all data in the incoming segment is discarded. 419df8bae1dSRodney W. Grimes * If the TCB exists but is in CLOSED state, it is embryonic, 420df8bae1dSRodney W. Grimes * but should either do a listen or a connect soon. 421df8bae1dSRodney W. Grimes */ 422816a3d83SPoul-Henning Kamp if (inp == NULL) { 4232e4e1b4cSGeoff Rehmet if (log_in_vain) { 424df5c0b8aSBill Fenner char buf[4*sizeof "123"]; 42575cfc95fSAndrey A. Chernov 42675cfc95fSAndrey A. Chernov strcpy(buf, inet_ntoa(ti->ti_dst)); 4272e4e1b4cSGeoff Rehmet switch (log_in_vain) { 4282e4e1b4cSGeoff Rehmet case 1: 4292e4e1b4cSGeoff Rehmet if(tiflags & TH_SYN) 430592071e8SBruce Evans log(LOG_INFO, 431592071e8SBruce Evans "Connection attempt to TCP %s:%d from %s:%d\n", 4325a8c77a8SDavid E. O'Brien buf, ntohs(ti->ti_dport), 4335a8c77a8SDavid E. O'Brien inet_ntoa(ti->ti_src), 434592071e8SBruce Evans ntohs(ti->ti_sport)); 4352e4e1b4cSGeoff Rehmet break; 4362e4e1b4cSGeoff Rehmet case 2: 4372e4e1b4cSGeoff Rehmet log(LOG_INFO, 4382e4e1b4cSGeoff Rehmet "Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n", 4392e4e1b4cSGeoff Rehmet buf, ntohs(ti->ti_dport), inet_ntoa(ti->ti_src), 4402e4e1b4cSGeoff Rehmet ntohs(ti->ti_sport), tiflags); 4412e4e1b4cSGeoff Rehmet break; 4422e4e1b4cSGeoff Rehmet default: 4432e4e1b4cSGeoff Rehmet break; 4442e4e1b4cSGeoff Rehmet } 44575cfc95fSAndrey A. Chernov } 44651508de1SMatthew Dillon #ifdef ICMP_BANDLIM 44751508de1SMatthew Dillon if (badport_bandlim(1) < 0) 44851508de1SMatthew Dillon goto drop; 44951508de1SMatthew Dillon #endif 4502e4e1b4cSGeoff Rehmet if (blackhole) { 4512e4e1b4cSGeoff Rehmet switch (blackhole) { 452828b7f40SGeoff Rehmet case 1: 453828b7f40SGeoff Rehmet if (tiflags & TH_SYN) 454828b7f40SGeoff Rehmet goto drop; 455828b7f40SGeoff Rehmet break; 456828b7f40SGeoff Rehmet case 2: 457828b7f40SGeoff Rehmet goto drop; 458828b7f40SGeoff Rehmet default: 459828b7f40SGeoff Rehmet goto drop; 4602e4e1b4cSGeoff Rehmet } 461828b7f40SGeoff Rehmet } 462df8bae1dSRodney W. Grimes goto dropwithreset; 463816a3d83SPoul-Henning Kamp } 464df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 465df8bae1dSRodney W. Grimes if (tp == 0) 466df8bae1dSRodney W. Grimes goto dropwithreset; 467df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_CLOSED) 468df8bae1dSRodney W. Grimes goto drop; 469df8bae1dSRodney W. Grimes 470df8bae1dSRodney W. Grimes /* Unscale the window into a 32-bit value. */ 471df8bae1dSRodney W. Grimes if ((tiflags & TH_SYN) == 0) 472df8bae1dSRodney W. Grimes tiwin = ti->ti_win << tp->snd_scale; 473df8bae1dSRodney W. Grimes else 474df8bae1dSRodney W. Grimes tiwin = ti->ti_win; 475df8bae1dSRodney W. Grimes 476df8bae1dSRodney W. Grimes so = inp->inp_socket; 477df8bae1dSRodney W. Grimes if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) { 478610ee2f9SDavid Greenman #ifdef TCPDEBUG 479df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 480df8bae1dSRodney W. Grimes ostate = tp->t_state; 481df8bae1dSRodney W. Grimes tcp_saveti = *ti; 482df8bae1dSRodney W. Grimes } 483610ee2f9SDavid Greenman #endif 484df8bae1dSRodney W. Grimes if (so->so_options & SO_ACCEPTCONN) { 485a0292f23SGarrett Wollman register struct tcpcb *tp0 = tp; 4864195b4afSPaul Traina struct socket *so2; 487ebb0cbeaSPaul Traina if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { 4884195b4afSPaul Traina /* 489ebb0cbeaSPaul Traina * Note: dropwithreset makes sure we don't 490ebb0cbeaSPaul Traina * send a RST in response to a RST. 4914195b4afSPaul Traina */ 492ebb0cbeaSPaul Traina if (tiflags & TH_ACK) { 493ebb0cbeaSPaul Traina tcpstat.tcps_badsyn++; 494ebb0cbeaSPaul Traina goto dropwithreset; 4954195b4afSPaul Traina } 496ebb0cbeaSPaul Traina goto drop; 497ebb0cbeaSPaul Traina } 498ebb0cbeaSPaul Traina so2 = sonewconn(so, 0); 499ebb0cbeaSPaul Traina if (so2 == 0) { 500ebb0cbeaSPaul Traina tcpstat.tcps_listendrop++; 501ebb0cbeaSPaul Traina so2 = sodropablereq(so); 502a51764a8SPaul Traina if (so2) { 503ebb0cbeaSPaul Traina tcp_drop(sototcpcb(so2), ETIMEDOUT); 504a51764a8SPaul Traina so2 = sonewconn(so, 0); 505a51764a8SPaul Traina } 506a51764a8SPaul Traina if (!so2) 507df8bae1dSRodney W. Grimes goto drop; 5081347f5b8SGuido van Rooij } 5094195b4afSPaul Traina so = so2; 510df8bae1dSRodney W. Grimes /* 511df8bae1dSRodney W. Grimes * This is ugly, but .... 512df8bae1dSRodney W. Grimes * 513df8bae1dSRodney W. Grimes * Mark socket as temporary until we're 514df8bae1dSRodney W. Grimes * committed to keeping it. The code at 515df8bae1dSRodney W. Grimes * ``drop'' and ``dropwithreset'' check the 516df8bae1dSRodney W. Grimes * flag dropsocket to see if the temporary 517df8bae1dSRodney W. Grimes * socket created here should be discarded. 518df8bae1dSRodney W. Grimes * We mark the socket as discardable until 519df8bae1dSRodney W. Grimes * we're committed to it below in TCPS_LISTEN. 520df8bae1dSRodney W. Grimes */ 521df8bae1dSRodney W. Grimes dropsocket++; 522df8bae1dSRodney W. Grimes inp = (struct inpcb *)so->so_pcb; 523df8bae1dSRodney W. Grimes inp->inp_laddr = ti->ti_dst; 524df8bae1dSRodney W. Grimes inp->inp_lport = ti->ti_dport; 525c3229e05SDavid Greenman if (in_pcbinshash(inp) != 0) { 526c3229e05SDavid Greenman /* 5275a8c77a8SDavid E. O'Brien * Undo the assignments above if we failed to 5285a8c77a8SDavid E. O'Brien * put the PCB on the hash lists. 529c3229e05SDavid Greenman */ 530c3229e05SDavid Greenman inp->inp_laddr.s_addr = INADDR_ANY; 531c3229e05SDavid Greenman inp->inp_lport = 0; 532c3229e05SDavid Greenman goto drop; 533c3229e05SDavid Greenman } 534df8bae1dSRodney W. Grimes inp->inp_options = ip_srcroute(); 535df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 536df8bae1dSRodney W. Grimes tp->t_state = TCPS_LISTEN; 537a0292f23SGarrett Wollman tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT); 538df8bae1dSRodney W. Grimes 539a0292f23SGarrett Wollman /* Compute proper scaling value from buffer space */ 540df8bae1dSRodney W. Grimes while (tp->request_r_scale < TCP_MAX_WINSHIFT && 5415a8c77a8SDavid E. O'Brien TCP_MAXWIN << tp->request_r_scale < 5425a8c77a8SDavid E. O'Brien so->so_rcv.sb_hiwat) 543df8bae1dSRodney W. Grimes tp->request_r_scale++; 544df8bae1dSRodney W. Grimes } 545df8bae1dSRodney W. Grimes } 546df8bae1dSRodney W. Grimes 547df8bae1dSRodney W. Grimes /* 548df8bae1dSRodney W. Grimes * Segment received on connection. 549df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 550df8bae1dSRodney W. Grimes */ 5519b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 5527ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 5539b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp); 554df8bae1dSRodney W. Grimes 555df8bae1dSRodney W. Grimes /* 556df8bae1dSRodney W. Grimes * Process options if not in LISTEN state, 557df8bae1dSRodney W. Grimes * else do it below (after getting remote address). 558df8bae1dSRodney W. Grimes */ 559f9d5a964SDavid Greenman if (tp->t_state != TCPS_LISTEN) 560f9d5a964SDavid Greenman tcp_dooptions(tp, optp, optlen, ti, &to); 561df8bae1dSRodney W. Grimes 562df8bae1dSRodney W. Grimes /* 563df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 564df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 565df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 566df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 567df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 568df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 569df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 570df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 571df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 572df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 573df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 574df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 575a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 576a0292f23SGarrett Wollman * Since we check for TCPS_ESTABLISHED above, it can only 577a0292f23SGarrett Wollman * be TH_NEEDSYN. 578df8bae1dSRodney W. Grimes */ 579df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 580df8bae1dSRodney W. Grimes (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 581a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 582a0292f23SGarrett Wollman ((to.to_flag & TOF_TS) == 0 || 583a0292f23SGarrett Wollman TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 584a0292f23SGarrett Wollman /* 585a0292f23SGarrett Wollman * Using the CC option is compulsory if once started: 586a0292f23SGarrett Wollman * the segment is OK if no T/TCP was negotiated or 587a0292f23SGarrett Wollman * if the segment has a CC option equal to CCrecv 588a0292f23SGarrett Wollman */ 589a0292f23SGarrett Wollman ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) || 590831a80b0SMatthew Dillon ((to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) && 591df8bae1dSRodney W. Grimes ti->ti_seq == tp->rcv_nxt && 592df8bae1dSRodney W. Grimes tiwin && tiwin == tp->snd_wnd && 593df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) { 594df8bae1dSRodney W. Grimes 595df8bae1dSRodney W. Grimes /* 596df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 597df8bae1dSRodney W. Grimes * record the timestamp. 598a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 599a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 600df8bae1dSRodney W. Grimes */ 601a0292f23SGarrett Wollman if ((to.to_flag & TOF_TS) != 0 && 602a0292f23SGarrett Wollman SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) { 6039b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 604a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 605df8bae1dSRodney W. Grimes } 606df8bae1dSRodney W. Grimes 607df8bae1dSRodney W. Grimes if (ti->ti_len == 0) { 608df8bae1dSRodney W. Grimes if (SEQ_GT(ti->ti_ack, tp->snd_una) && 609df8bae1dSRodney W. Grimes SEQ_LEQ(ti->ti_ack, tp->snd_max) && 610233e8c18SGarrett Wollman tp->snd_cwnd >= tp->snd_wnd && 611233e8c18SGarrett Wollman tp->t_dupacks < tcprexmtthresh) { 612df8bae1dSRodney W. Grimes /* 613df8bae1dSRodney W. Grimes * this is a pure ack for outstanding data. 614df8bae1dSRodney W. Grimes */ 615df8bae1dSRodney W. Grimes ++tcpstat.tcps_predack; 6169b8b58e0SJonathan Lemon /* 6179b8b58e0SJonathan Lemon * "bad retransmit" recovery 6189b8b58e0SJonathan Lemon */ 6199b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 6209b8b58e0SJonathan Lemon ticks < tp->t_badrxtwin) { 6219b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 6229b8b58e0SJonathan Lemon tp->snd_ssthresh = 6239b8b58e0SJonathan Lemon tp->snd_ssthresh_prev; 6249b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 6259b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; 6269b8b58e0SJonathan Lemon } 627a0292f23SGarrett Wollman if ((to.to_flag & TOF_TS) != 0) 628a0292f23SGarrett Wollman tcp_xmit_timer(tp, 6299b8b58e0SJonathan Lemon ticks - to.to_tsecr + 1); 6309b8b58e0SJonathan Lemon else if (tp->t_rtttime && 631df8bae1dSRodney W. Grimes SEQ_GT(ti->ti_ack, tp->t_rtseq)) 6329b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 633df8bae1dSRodney W. Grimes acked = ti->ti_ack - tp->snd_una; 634df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 635df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 636df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 637df8bae1dSRodney W. Grimes tp->snd_una = ti->ti_ack; 638df8bae1dSRodney W. Grimes m_freem(m); 639df8bae1dSRodney W. Grimes 640df8bae1dSRodney W. Grimes /* 641df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 642df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 643df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 644df8bae1dSRodney W. Grimes * If process is waiting for space, 645df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 646df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 647df8bae1dSRodney W. Grimes * decide between more output or persist. 648df8bae1dSRodney W. Grimes */ 649df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 6509b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 6519b8b58e0SJonathan Lemon else if (!callout_active(tp->tt_persist)) 6529b8b58e0SJonathan Lemon callout_reset(tp->tt_rexmt, 6539b8b58e0SJonathan Lemon tp->t_rxtcur, 6549b8b58e0SJonathan Lemon tcp_timer_rexmt, tp); 655df8bae1dSRodney W. Grimes 656df8bae1dSRodney W. Grimes sowwakeup(so); 657df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 658df8bae1dSRodney W. Grimes (void) tcp_output(tp); 659df8bae1dSRodney W. Grimes return; 660df8bae1dSRodney W. Grimes } 661df8bae1dSRodney W. Grimes } else if (ti->ti_ack == tp->snd_una && 6626effc713SDoug Rabson tp->t_segq == NULL && 663df8bae1dSRodney W. Grimes ti->ti_len <= sbspace(&so->so_rcv)) { 664df8bae1dSRodney W. Grimes /* 665df8bae1dSRodney W. Grimes * this is a pure, in-sequence data packet 666df8bae1dSRodney W. Grimes * with nothing on the reassembly queue and 667df8bae1dSRodney W. Grimes * we have enough buffer space to take it. 668df8bae1dSRodney W. Grimes */ 669df8bae1dSRodney W. Grimes ++tcpstat.tcps_preddat; 670df8bae1dSRodney W. Grimes tp->rcv_nxt += ti->ti_len; 671df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpack++; 672df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyte += ti->ti_len; 673df8bae1dSRodney W. Grimes /* 674755c1f07SAndras Olah * Add data to socket buffer. 675df8bae1dSRodney W. Grimes */ 676df8bae1dSRodney W. Grimes sbappend(&so->so_rcv, m); 677df8bae1dSRodney W. Grimes sorwakeup(so); 678f498eeeeSDavid Greenman if (tcp_delack_enabled) { 6799b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 6809b8b58e0SJonathan Lemon tcp_timer_delack, tp); 681f498eeeeSDavid Greenman } else { 682e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 683e612a582SDavid Greenman tcp_output(tp); 684e612a582SDavid Greenman } 685df8bae1dSRodney W. Grimes return; 686df8bae1dSRodney W. Grimes } 687df8bae1dSRodney W. Grimes } 688df8bae1dSRodney W. Grimes 689df8bae1dSRodney W. Grimes /* 690df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 691df8bae1dSRodney W. Grimes * and then do TCP input processing. 692df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 693df8bae1dSRodney W. Grimes * but not less than advertised window. 694df8bae1dSRodney W. Grimes */ 695df8bae1dSRodney W. Grimes { int win; 696df8bae1dSRodney W. Grimes 697df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 698df8bae1dSRodney W. Grimes if (win < 0) 699df8bae1dSRodney W. Grimes win = 0; 70066e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 701df8bae1dSRodney W. Grimes } 702df8bae1dSRodney W. Grimes 703df8bae1dSRodney W. Grimes switch (tp->t_state) { 704df8bae1dSRodney W. Grimes 705df8bae1dSRodney W. Grimes /* 706df8bae1dSRodney W. Grimes * If the state is LISTEN then ignore segment if it contains an RST. 707df8bae1dSRodney W. Grimes * If the segment contains an ACK then it is bad and send a RST. 708df8bae1dSRodney W. Grimes * If it does not contain a SYN then it is not interesting; drop it. 709764d8cefSBill Fenner * If it is from this socket, drop it, it must be forged. 710df8bae1dSRodney W. Grimes * Don't bother responding if the destination was a broadcast. 711df8bae1dSRodney W. Grimes * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 712df8bae1dSRodney W. Grimes * tp->iss, and send a segment: 713df8bae1dSRodney W. Grimes * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 714df8bae1dSRodney W. Grimes * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 715df8bae1dSRodney W. Grimes * Fill in remote peer address fields if not previously specified. 716df8bae1dSRodney W. Grimes * Enter SYN_RECEIVED state, and process any other fields of this 717df8bae1dSRodney W. Grimes * segment in this state. 718df8bae1dSRodney W. Grimes */ 719df8bae1dSRodney W. Grimes case TCPS_LISTEN: { 720df8bae1dSRodney W. Grimes register struct sockaddr_in *sin; 721df8bae1dSRodney W. Grimes 722df8bae1dSRodney W. Grimes if (tiflags & TH_RST) 723df8bae1dSRodney W. Grimes goto drop; 724df8bae1dSRodney W. Grimes if (tiflags & TH_ACK) 725df8bae1dSRodney W. Grimes goto dropwithreset; 726df8bae1dSRodney W. Grimes if ((tiflags & TH_SYN) == 0) 727df8bae1dSRodney W. Grimes goto drop; 728764d8cefSBill Fenner if ((ti->ti_dport == ti->ti_sport) && 729764d8cefSBill Fenner (ti->ti_dst.s_addr == ti->ti_src.s_addr)) 730764d8cefSBill Fenner goto drop; 731df8bae1dSRodney W. Grimes /* 732df8bae1dSRodney W. Grimes * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 733df8bae1dSRodney W. Grimes * in_broadcast() should never return true on a received 734df8bae1dSRodney W. Grimes * packet with M_BCAST not set. 735df8bae1dSRodney W. Grimes */ 736df8bae1dSRodney W. Grimes if (m->m_flags & (M_BCAST|M_MCAST) || 737d4d0967eSDavid Greenman IN_MULTICAST(ntohl(ti->ti_dst.s_addr))) 738df8bae1dSRodney W. Grimes goto drop; 73957bf258eSGarrett Wollman MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, 74057bf258eSGarrett Wollman M_NOWAIT); 74157bf258eSGarrett Wollman if (sin == NULL) 742df8bae1dSRodney W. Grimes goto drop; 743df8bae1dSRodney W. Grimes sin->sin_family = AF_INET; 744df8bae1dSRodney W. Grimes sin->sin_len = sizeof(*sin); 745df8bae1dSRodney W. Grimes sin->sin_addr = ti->ti_src; 746df8bae1dSRodney W. Grimes sin->sin_port = ti->ti_sport; 747df8bae1dSRodney W. Grimes bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero)); 748df8bae1dSRodney W. Grimes laddr = inp->inp_laddr; 749df8bae1dSRodney W. Grimes if (inp->inp_laddr.s_addr == INADDR_ANY) 750df8bae1dSRodney W. Grimes inp->inp_laddr = ti->ti_dst; 75157bf258eSGarrett Wollman if (in_pcbconnect(inp, (struct sockaddr *)sin, &proc0)) { 752df8bae1dSRodney W. Grimes inp->inp_laddr = laddr; 75357bf258eSGarrett Wollman FREE(sin, M_SONAME); 754df8bae1dSRodney W. Grimes goto drop; 755df8bae1dSRodney W. Grimes } 75657bf258eSGarrett Wollman FREE(sin, M_SONAME); 757df8bae1dSRodney W. Grimes tp->t_template = tcp_template(tp); 758df8bae1dSRodney W. Grimes if (tp->t_template == 0) { 759df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ENOBUFS); 760df8bae1dSRodney W. Grimes dropsocket = 0; /* socket is already gone */ 761df8bae1dSRodney W. Grimes goto drop; 762df8bae1dSRodney W. Grimes } 763a0292f23SGarrett Wollman if ((taop = tcp_gettaocache(inp)) == NULL) { 764a0292f23SGarrett Wollman taop = &tao_noncached; 765a0292f23SGarrett Wollman bzero(taop, sizeof(*taop)); 766a0292f23SGarrett Wollman } 767f9d5a964SDavid Greenman tcp_dooptions(tp, optp, optlen, ti, &to); 768df8bae1dSRodney W. Grimes if (iss) 769df8bae1dSRodney W. Grimes tp->iss = iss; 770df8bae1dSRodney W. Grimes else 771df8bae1dSRodney W. Grimes tp->iss = tcp_iss; 772e79adb8eSGarrett Wollman tcp_iss += TCP_ISSINCR/4; 773df8bae1dSRodney W. Grimes tp->irs = ti->ti_seq; 774df8bae1dSRodney W. Grimes tcp_sendseqinit(tp); 775df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 776a0292f23SGarrett Wollman /* 777a0292f23SGarrett Wollman * Initialization of the tcpcb for transaction; 778a0292f23SGarrett Wollman * set SND.WND = SEG.WND, 779a0292f23SGarrett Wollman * initialize CCsend and CCrecv. 780a0292f23SGarrett Wollman */ 781a0292f23SGarrett Wollman tp->snd_wnd = tiwin; /* initial send-window */ 782a0292f23SGarrett Wollman tp->cc_send = CC_INC(tcp_ccgen); 783a0292f23SGarrett Wollman tp->cc_recv = to.to_cc; 784a0292f23SGarrett Wollman /* 785a0292f23SGarrett Wollman * Perform TAO test on incoming CC (SEG.CC) option, if any. 786a0292f23SGarrett Wollman * - compare SEG.CC against cached CC from the same host, 787a0292f23SGarrett Wollman * if any. 788a0292f23SGarrett Wollman * - if SEG.CC > chached value, SYN must be new and is accepted 789a0292f23SGarrett Wollman * immediately: save new CC in the cache, mark the socket 790a0292f23SGarrett Wollman * connected, enter ESTABLISHED state, turn on flag to 791a0292f23SGarrett Wollman * send a SYN in the next segment. 792a0292f23SGarrett Wollman * A virtual advertised window is set in rcv_adv to 793a0292f23SGarrett Wollman * initialize SWS prevention. Then enter normal segment 794a0292f23SGarrett Wollman * processing: drop SYN, process data and FIN. 795a0292f23SGarrett Wollman * - otherwise do a normal 3-way handshake. 796a0292f23SGarrett Wollman */ 797a0292f23SGarrett Wollman if ((to.to_flag & TOF_CC) != 0) { 798068373b6SGuido van Rooij if (((tp->t_flags & TF_NOPUSH) != 0) && 79911ad4550SGuido van Rooij taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) { 80011ad4550SGuido van Rooij 801a0292f23SGarrett Wollman taop->tao_cc = to.to_cc; 8029b8b58e0SJonathan Lemon tp->t_starttime = ticks; 803a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 804a0292f23SGarrett Wollman 805a0292f23SGarrett Wollman /* 806a0292f23SGarrett Wollman * If there is a FIN, or if there is data and the 807a0292f23SGarrett Wollman * connection is local, then delay SYN,ACK(SYN) in 808a0292f23SGarrett Wollman * the hope of piggy-backing it on a response 809a0292f23SGarrett Wollman * segment. Otherwise must send ACK now in case 810a0292f23SGarrett Wollman * the other side is slow starting. 811a0292f23SGarrett Wollman */ 8125a8c77a8SDavid E. O'Brien if (tcp_delack_enabled && ((tiflags & TH_FIN) || 8135a8c77a8SDavid E. O'Brien (ti->ti_len != 0 && 8149b8b58e0SJonathan Lemon in_localaddr(inp->inp_faddr)))) { 8159b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 8169b8b58e0SJonathan Lemon tcp_timer_delack, tp); 8179b8b58e0SJonathan Lemon tp->t_flags |= TF_NEEDSYN; 8189b8b58e0SJonathan Lemon } else 819a0292f23SGarrett Wollman tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 82007e43e10SAndras Olah 82107e43e10SAndras Olah /* 82207e43e10SAndras Olah * Limit the `virtual advertised window' to TCP_MAXWIN 82307e43e10SAndras Olah * here. Even if we requested window scaling, it will 82407e43e10SAndras Olah * become effective only later when our SYN is acked. 82507e43e10SAndras Olah */ 82607e43e10SAndras Olah tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN); 827a0292f23SGarrett Wollman tcpstat.tcps_connects++; 828a0292f23SGarrett Wollman soisconnected(so); 8299b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepinit, 8309b8b58e0SJonathan Lemon tcp_timer_keep, tp); 831a0292f23SGarrett Wollman dropsocket = 0; /* committed to socket */ 832a0292f23SGarrett Wollman tcpstat.tcps_accepts++; 833a0292f23SGarrett Wollman goto trimthenstep6; 834a0292f23SGarrett Wollman } 835a0292f23SGarrett Wollman /* else do standard 3-way handshake */ 836a0292f23SGarrett Wollman } else { 837a0292f23SGarrett Wollman /* 838a0292f23SGarrett Wollman * No CC option, but maybe CC.NEW: 839a0292f23SGarrett Wollman * invalidate cached value. 840a0292f23SGarrett Wollman */ 841a0292f23SGarrett Wollman taop->tao_cc = 0; 842a0292f23SGarrett Wollman } 843a0292f23SGarrett Wollman /* 844a0292f23SGarrett Wollman * TAO test failed or there was no CC option, 845a0292f23SGarrett Wollman * do a standard 3-way handshake. 846a0292f23SGarrett Wollman */ 847df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 848df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 8499b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 850df8bae1dSRodney W. Grimes dropsocket = 0; /* committed to socket */ 851df8bae1dSRodney W. Grimes tcpstat.tcps_accepts++; 852df8bae1dSRodney W. Grimes goto trimthenstep6; 853df8bae1dSRodney W. Grimes } 854df8bae1dSRodney W. Grimes 855df8bae1dSRodney W. Grimes /* 856764d8cefSBill Fenner * If the state is SYN_RECEIVED: 857764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 858764d8cefSBill Fenner */ 859764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 86075daa6a5SBill Fenner if ((tiflags & TH_ACK) && 86175daa6a5SBill Fenner (SEQ_LEQ(ti->ti_ack, tp->snd_una) || 86275daa6a5SBill Fenner SEQ_GT(ti->ti_ack, tp->snd_max))) 863764d8cefSBill Fenner goto dropwithreset; 864764d8cefSBill Fenner break; 865764d8cefSBill Fenner 866764d8cefSBill Fenner /* 867df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 868df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 869df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 870df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 871df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 872df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 873df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 874df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 875df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 876df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 877df8bae1dSRodney W. Grimes */ 878df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 879a0292f23SGarrett Wollman if ((taop = tcp_gettaocache(inp)) == NULL) { 880a0292f23SGarrett Wollman taop = &tao_noncached; 881a0292f23SGarrett Wollman bzero(taop, sizeof(*taop)); 882a0292f23SGarrett Wollman } 883a0292f23SGarrett Wollman 884a0292f23SGarrett Wollman if ((tiflags & TH_ACK) && 885a0292f23SGarrett Wollman (SEQ_LEQ(ti->ti_ack, tp->iss) || 886a0292f23SGarrett Wollman SEQ_GT(ti->ti_ack, tp->snd_max))) { 887a0292f23SGarrett Wollman /* 888a0292f23SGarrett Wollman * If we have a cached CCsent for the remote host, 889a0292f23SGarrett Wollman * hence we haven't just crashed and restarted, 890a0292f23SGarrett Wollman * do not send a RST. This may be a retransmission 891a0292f23SGarrett Wollman * from the other side after our earlier ACK was lost. 892a0292f23SGarrett Wollman * Our new SYN, when it arrives, will serve as the 893a0292f23SGarrett Wollman * needed ACK. 894a0292f23SGarrett Wollman */ 895a0292f23SGarrett Wollman if (taop->tao_ccsent != 0) 896a0292f23SGarrett Wollman goto drop; 897a0292f23SGarrett Wollman else 898a0292f23SGarrett Wollman goto dropwithreset; 899a0292f23SGarrett Wollman } 900df8bae1dSRodney W. Grimes if (tiflags & TH_RST) { 901df8bae1dSRodney W. Grimes if (tiflags & TH_ACK) 902df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 903df8bae1dSRodney W. Grimes goto drop; 904df8bae1dSRodney W. Grimes } 905df8bae1dSRodney W. Grimes if ((tiflags & TH_SYN) == 0) 906df8bae1dSRodney W. Grimes goto drop; 907a0292f23SGarrett Wollman tp->snd_wnd = ti->ti_win; /* initial send window */ 908a0292f23SGarrett Wollman tp->cc_recv = to.to_cc; /* foreign CC */ 909a0292f23SGarrett Wollman 910df8bae1dSRodney W. Grimes tp->irs = ti->ti_seq; 911df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 912845799c1SAndras Olah if (tiflags & TH_ACK) { 913a0292f23SGarrett Wollman /* 914a0292f23SGarrett Wollman * Our SYN was acked. If segment contains CC.ECHO 915a0292f23SGarrett Wollman * option, check it to make sure this segment really 916a0292f23SGarrett Wollman * matches our SYN. If not, just drop it as old 917a0292f23SGarrett Wollman * duplicate, but send an RST if we're still playing 918026650e5SBill Fenner * by the old rules. If no CC.ECHO option, make sure 919026650e5SBill Fenner * we don't get fooled into using T/TCP. 920a0292f23SGarrett Wollman */ 921026650e5SBill Fenner if (to.to_flag & TOF_CCECHO) { 922dfd5dee1SPeter Wemm if (tp->cc_send != to.to_ccecho) { 923a0292f23SGarrett Wollman if (taop->tao_ccsent != 0) 924a0292f23SGarrett Wollman goto drop; 925a0292f23SGarrett Wollman else 926a0292f23SGarrett Wollman goto dropwithreset; 927dfd5dee1SPeter Wemm } 928026650e5SBill Fenner } else 929026650e5SBill Fenner tp->t_flags &= ~TF_RCVD_CC; 930845799c1SAndras Olah tcpstat.tcps_connects++; 931845799c1SAndras Olah soisconnected(so); 932845799c1SAndras Olah /* Do window scaling on this connection? */ 933845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 934845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 935845799c1SAndras Olah tp->snd_scale = tp->requested_s_scale; 936845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 937845799c1SAndras Olah } 938a0292f23SGarrett Wollman /* Segment is acceptable, update cache if undefined. */ 939a0292f23SGarrett Wollman if (taop->tao_ccsent == 0) 940a0292f23SGarrett Wollman taop->tao_ccsent = to.to_ccecho; 941a0292f23SGarrett Wollman 942a0292f23SGarrett Wollman tp->rcv_adv += tp->rcv_wnd; 943a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 944a0292f23SGarrett Wollman /* 945a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 946a0292f23SGarrett Wollman * ACKNOW will be turned on later. 947a0292f23SGarrett Wollman */ 948f498eeeeSDavid Greenman if (tcp_delack_enabled && ti->ti_len != 0) 9499b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 9509b8b58e0SJonathan Lemon tcp_timer_delack, tp); 951a0292f23SGarrett Wollman else 952a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 953a0292f23SGarrett Wollman /* 954a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 955a0292f23SGarrett Wollman * Transitions: 956a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 957a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 958a0292f23SGarrett Wollman */ 9599b8b58e0SJonathan Lemon tp->t_starttime = ticks; 960a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 961a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 962a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 963a0292f23SGarrett Wollman tiflags &= ~TH_SYN; 9647ff19458SPaul Traina } else { 965a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 9669b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, 9679b8b58e0SJonathan Lemon tcp_timer_keep, tp); 9687ff19458SPaul Traina } 969a0292f23SGarrett Wollman } else { 970a0292f23SGarrett Wollman /* 971a0292f23SGarrett Wollman * Received initial SYN in SYN-SENT[*] state => simul- 972a0292f23SGarrett Wollman * taneous open. If segment contains CC option and there is 973a0292f23SGarrett Wollman * a cached CC, apply TAO test; if it succeeds, connection is 974a0292f23SGarrett Wollman * half-synchronized. Otherwise, do 3-way handshake: 975a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 976a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 977a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 978a0292f23SGarrett Wollman */ 979a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 9809b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 981a0292f23SGarrett Wollman if (to.to_flag & TOF_CC) { 982a0292f23SGarrett Wollman if (taop->tao_cc != 0 && 983a0292f23SGarrett Wollman CC_GT(to.to_cc, taop->tao_cc)) { 984a0292f23SGarrett Wollman /* 985a0292f23SGarrett Wollman * update cache and make transition: 986a0292f23SGarrett Wollman * SYN-SENT -> ESTABLISHED* 987a0292f23SGarrett Wollman * SYN-SENT* -> FIN-WAIT-1* 988a0292f23SGarrett Wollman */ 989a0292f23SGarrett Wollman taop->tao_cc = to.to_cc; 9909b8b58e0SJonathan Lemon tp->t_starttime = ticks; 991a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 992a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 993a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 9947ff19458SPaul Traina } else { 995a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 9969b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, 9979b8b58e0SJonathan Lemon tcp_keepidle, 9989b8b58e0SJonathan Lemon tcp_timer_keep, 9999b8b58e0SJonathan Lemon tp); 10007ff19458SPaul Traina } 1001a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDSYN; 1002df8bae1dSRodney W. Grimes } else 1003df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1004a0292f23SGarrett Wollman } else { 1005a0292f23SGarrett Wollman /* CC.NEW or no option => invalidate cache */ 1006a0292f23SGarrett Wollman taop->tao_cc = 0; 1007a0292f23SGarrett Wollman tp->t_state = TCPS_SYN_RECEIVED; 1008a0292f23SGarrett Wollman } 1009a0292f23SGarrett Wollman } 1010df8bae1dSRodney W. Grimes 1011df8bae1dSRodney W. Grimes trimthenstep6: 1012df8bae1dSRodney W. Grimes /* 1013df8bae1dSRodney W. Grimes * Advance ti->ti_seq to correspond to first data byte. 1014df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1015df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1016df8bae1dSRodney W. Grimes */ 1017df8bae1dSRodney W. Grimes ti->ti_seq++; 1018df8bae1dSRodney W. Grimes if (ti->ti_len > tp->rcv_wnd) { 1019df8bae1dSRodney W. Grimes todrop = ti->ti_len - tp->rcv_wnd; 1020df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1021df8bae1dSRodney W. Grimes ti->ti_len = tp->rcv_wnd; 1022df8bae1dSRodney W. Grimes tiflags &= ~TH_FIN; 1023df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1024df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1025df8bae1dSRodney W. Grimes } 1026df8bae1dSRodney W. Grimes tp->snd_wl1 = ti->ti_seq - 1; 1027df8bae1dSRodney W. Grimes tp->rcv_up = ti->ti_seq; 1028a0292f23SGarrett Wollman /* 1029a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1030a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1031a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1032a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1033a0292f23SGarrett Wollman * Otherwise, goto step 6. 1034a0292f23SGarrett Wollman */ 1035a0292f23SGarrett Wollman if (tiflags & TH_ACK) 1036a0292f23SGarrett Wollman goto process_ACK; 1037df8bae1dSRodney W. Grimes goto step6; 1038a0292f23SGarrett Wollman /* 1039a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1040a0292f23SGarrett Wollman * if segment contains a SYN and CC [not CC.NEW] option: 1041a0292f23SGarrett Wollman * if state == TIME_WAIT and connection duration > MSL, 1042a0292f23SGarrett Wollman * drop packet and send RST; 1043a0292f23SGarrett Wollman * 1044a0292f23SGarrett Wollman * if SEG.CC > CCrecv then is new SYN, and can implicitly 1045a0292f23SGarrett Wollman * ack the FIN (and data) in retransmission queue. 1046a0292f23SGarrett Wollman * Complete close and delete TCPCB. Then reprocess 1047a0292f23SGarrett Wollman * segment, hoping to find new TCPCB in LISTEN state; 1048a0292f23SGarrett Wollman * 1049a0292f23SGarrett Wollman * else must be old SYN; drop it. 1050a0292f23SGarrett Wollman * else do normal processing. 1051a0292f23SGarrett Wollman */ 1052a0292f23SGarrett Wollman case TCPS_LAST_ACK: 1053a0292f23SGarrett Wollman case TCPS_CLOSING: 1054a0292f23SGarrett Wollman case TCPS_TIME_WAIT: 1055a0292f23SGarrett Wollman if ((tiflags & TH_SYN) && 1056a0292f23SGarrett Wollman (to.to_flag & TOF_CC) && tp->cc_recv != 0) { 1057a0292f23SGarrett Wollman if (tp->t_state == TCPS_TIME_WAIT && 10589b8b58e0SJonathan Lemon (ticks - tp->t_starttime) > tcp_msl) 1059a0292f23SGarrett Wollman goto dropwithreset; 1060a0292f23SGarrett Wollman if (CC_GT(to.to_cc, tp->cc_recv)) { 1061a0292f23SGarrett Wollman tp = tcp_close(tp); 1062a0292f23SGarrett Wollman goto findpcb; 1063a0292f23SGarrett Wollman } 1064a0292f23SGarrett Wollman else 1065a0292f23SGarrett Wollman goto drop; 1066a0292f23SGarrett Wollman } 1067a0292f23SGarrett Wollman break; /* continue normal processing */ 1068df8bae1dSRodney W. Grimes } 1069df8bae1dSRodney W. Grimes 1070df8bae1dSRodney W. Grimes /* 1071df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 107280ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 107380ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 107480ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 107580ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 107680ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 107780ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 107880ab7c0eSGarrett Wollman * killing a connection). 107980ab7c0eSGarrett Wollman * Then check timestamp, if present. 1080a0292f23SGarrett Wollman * Then check the connection count, if present. 1081df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 1082df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 1083df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 1084df8bae1dSRodney W. Grimes * 108580ab7c0eSGarrett Wollman * 108680ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 108780ab7c0eSGarrett Wollman * if this is a valid reset segment. 108880ab7c0eSGarrett Wollman * RFC 793 page 37: 108980ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 109080ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 109180ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 109280ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 109380ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 109480ab7c0eSGarrett Wollman * Also, it does not make sense to allow reset segments with 109580ab7c0eSGarrett Wollman * sequence numbers greater than last_ack_sent to be processed 109680ab7c0eSGarrett Wollman * since these sequence numbers are just the acknowledgement 109780ab7c0eSGarrett Wollman * numbers in our outgoing packets being echoed back at us, 109880ab7c0eSGarrett Wollman * and these acknowledgement numbers are monotonically 109980ab7c0eSGarrett Wollman * increasing. 110080ab7c0eSGarrett Wollman * If we have multiple segments in flight, the intial reset 110180ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 110280ab7c0eSGarrett Wollman * but they will eventually catch up. 110380ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 110480ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 110580ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 110680ab7c0eSGarrett Wollman * 110780ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 110880ab7c0eSGarrett Wollman * 110980ab7c0eSGarrett Wollman * DISCUSSION 111080ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 111180ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 111280ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 111380ab7c0eSGarrett Wollman * data. 111480ab7c0eSGarrett Wollman * 111580ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 111680ab7c0eSGarrett Wollman * the state: 111780ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 111880ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 111980ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 112080ab7c0eSGarrett Wollman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 112180ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 1122e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 112380ab7c0eSGarrett Wollman * Close the tcb. 1124e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 112580ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 112680ab7c0eSGarrett Wollman * RFC 1337. 112780ab7c0eSGarrett Wollman */ 112880ab7c0eSGarrett Wollman if (tiflags & TH_RST) { 112980ab7c0eSGarrett Wollman if (tp->last_ack_sent == ti->ti_seq) { 113080ab7c0eSGarrett Wollman switch (tp->t_state) { 113180ab7c0eSGarrett Wollman 113280ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 113380ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 113480ab7c0eSGarrett Wollman goto close; 113580ab7c0eSGarrett Wollman 113680ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 113780ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 113880ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 113980ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 114080ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 114180ab7c0eSGarrett Wollman close: 114280ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 114380ab7c0eSGarrett Wollman tcpstat.tcps_drops++; 114480ab7c0eSGarrett Wollman tp = tcp_close(tp); 114580ab7c0eSGarrett Wollman break; 114680ab7c0eSGarrett Wollman 114780ab7c0eSGarrett Wollman case TCPS_CLOSING: 114880ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 114980ab7c0eSGarrett Wollman tp = tcp_close(tp); 115080ab7c0eSGarrett Wollman break; 115180ab7c0eSGarrett Wollman 115280ab7c0eSGarrett Wollman case TCPS_TIME_WAIT: 115380ab7c0eSGarrett Wollman break; 115480ab7c0eSGarrett Wollman } 115580ab7c0eSGarrett Wollman } 115680ab7c0eSGarrett Wollman goto drop; 115780ab7c0eSGarrett Wollman } 115880ab7c0eSGarrett Wollman 115980ab7c0eSGarrett Wollman /* 1160df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 1161df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 1162df8bae1dSRodney W. Grimes */ 116380ab7c0eSGarrett Wollman if ((to.to_flag & TOF_TS) != 0 && tp->ts_recent && 116480ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1165df8bae1dSRodney W. Grimes 1166df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 11679b8b58e0SJonathan Lemon if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1168df8bae1dSRodney W. Grimes /* 1169df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 1170df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 1171df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 1172df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 1173df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 1174df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 1175df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 1176df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 1177df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 1178df8bae1dSRodney W. Grimes */ 1179df8bae1dSRodney W. Grimes tp->ts_recent = 0; 1180df8bae1dSRodney W. Grimes } else { 1181df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 1182df8bae1dSRodney W. Grimes tcpstat.tcps_rcvdupbyte += ti->ti_len; 1183df8bae1dSRodney W. Grimes tcpstat.tcps_pawsdrop++; 1184df8bae1dSRodney W. Grimes goto dropafterack; 1185df8bae1dSRodney W. Grimes } 1186df8bae1dSRodney W. Grimes } 1187df8bae1dSRodney W. Grimes 1188a0292f23SGarrett Wollman /* 1189a0292f23SGarrett Wollman * T/TCP mechanism 1190a0292f23SGarrett Wollman * If T/TCP was negotiated and the segment doesn't have CC, 1191dc733423SDag-Erling Smørgrav * or if its CC is wrong then drop the segment. 1192a0292f23SGarrett Wollman * RST segments do not have to comply with this. 1193a0292f23SGarrett Wollman */ 1194a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) && 119580ab7c0eSGarrett Wollman ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc)) 1196a0292f23SGarrett Wollman goto dropafterack; 1197a0292f23SGarrett Wollman 119880ab7c0eSGarrett Wollman /* 119980ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 120080ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 120180ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 120280ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 120380ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 120480ab7c0eSGarrett Wollman */ 120580ab7c0eSGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(ti->ti_seq, tp->irs)) 120680ab7c0eSGarrett Wollman goto dropwithreset; 120780ab7c0eSGarrett Wollman 1208df8bae1dSRodney W. Grimes todrop = tp->rcv_nxt - ti->ti_seq; 1209df8bae1dSRodney W. Grimes if (todrop > 0) { 1210df8bae1dSRodney W. Grimes if (tiflags & TH_SYN) { 1211df8bae1dSRodney W. Grimes tiflags &= ~TH_SYN; 1212df8bae1dSRodney W. Grimes ti->ti_seq++; 1213df8bae1dSRodney W. Grimes if (ti->ti_urp > 1) 1214df8bae1dSRodney W. Grimes ti->ti_urp--; 1215df8bae1dSRodney W. Grimes else 1216df8bae1dSRodney W. Grimes tiflags &= ~TH_URG; 1217df8bae1dSRodney W. Grimes todrop--; 1218df8bae1dSRodney W. Grimes } 1219df8bae1dSRodney W. Grimes /* 1220dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 1221df8bae1dSRodney W. Grimes */ 1222dac20301SGarrett Wollman if (todrop > ti->ti_len 1223dac20301SGarrett Wollman || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) { 1224dac20301SGarrett Wollman /* 1225dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 1226dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 1227dac20301SGarrett Wollman * of sequence; drop it. 1228dac20301SGarrett Wollman */ 1229df8bae1dSRodney W. Grimes tiflags &= ~TH_FIN; 1230dac20301SGarrett Wollman 1231df8bae1dSRodney W. Grimes /* 1232dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 1233dac20301SGarrett Wollman * But keep on processing for RST or ACK. 1234df8bae1dSRodney W. Grimes */ 1235dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1236dac20301SGarrett Wollman todrop = ti->ti_len; 1237dac20301SGarrett Wollman tcpstat.tcps_rcvduppack++; 1238dac20301SGarrett Wollman tcpstat.tcps_rcvdupbyte += todrop; 1239df8bae1dSRodney W. Grimes } else { 1240df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartduppack++; 1241df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartdupbyte += todrop; 1242df8bae1dSRodney W. Grimes } 1243df8bae1dSRodney W. Grimes m_adj(m, todrop); 1244df8bae1dSRodney W. Grimes ti->ti_seq += todrop; 1245df8bae1dSRodney W. Grimes ti->ti_len -= todrop; 1246df8bae1dSRodney W. Grimes if (ti->ti_urp > todrop) 1247df8bae1dSRodney W. Grimes ti->ti_urp -= todrop; 1248df8bae1dSRodney W. Grimes else { 1249df8bae1dSRodney W. Grimes tiflags &= ~TH_URG; 1250df8bae1dSRodney W. Grimes ti->ti_urp = 0; 1251df8bae1dSRodney W. Grimes } 1252df8bae1dSRodney W. Grimes } 1253df8bae1dSRodney W. Grimes 1254df8bae1dSRodney W. Grimes /* 1255df8bae1dSRodney W. Grimes * If new data are received on a connection after the 1256df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 1257df8bae1dSRodney W. Grimes */ 1258df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 1259df8bae1dSRodney W. Grimes tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { 1260df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1261df8bae1dSRodney W. Grimes tcpstat.tcps_rcvafterclose++; 1262df8bae1dSRodney W. Grimes goto dropwithreset; 1263df8bae1dSRodney W. Grimes } 1264df8bae1dSRodney W. Grimes 1265df8bae1dSRodney W. Grimes /* 1266df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 1267df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 1268df8bae1dSRodney W. Grimes */ 1269df8bae1dSRodney W. Grimes todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 1270df8bae1dSRodney W. Grimes if (todrop > 0) { 1271df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1272df8bae1dSRodney W. Grimes if (todrop >= ti->ti_len) { 1273df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 1274df8bae1dSRodney W. Grimes /* 1275df8bae1dSRodney W. Grimes * If a new connection request is received 1276df8bae1dSRodney W. Grimes * while in TIME_WAIT, drop the old connection 1277df8bae1dSRodney W. Grimes * and start over if the sequence numbers 1278df8bae1dSRodney W. Grimes * are above the previous ones. 1279df8bae1dSRodney W. Grimes */ 1280df8bae1dSRodney W. Grimes if (tiflags & TH_SYN && 1281df8bae1dSRodney W. Grimes tp->t_state == TCPS_TIME_WAIT && 1282df8bae1dSRodney W. Grimes SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 128351b7b337SBill Fenner iss = tp->snd_nxt + TCP_ISSINCR; 1284df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1285df8bae1dSRodney W. Grimes goto findpcb; 1286df8bae1dSRodney W. Grimes } 1287df8bae1dSRodney W. Grimes /* 1288df8bae1dSRodney W. Grimes * If window is closed can only take segments at 1289df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 1290df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 1291df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 1292df8bae1dSRodney W. Grimes * and ack. 1293df8bae1dSRodney W. Grimes */ 1294df8bae1dSRodney W. Grimes if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) { 1295df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1296df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinprobe++; 1297df8bae1dSRodney W. Grimes } else 1298df8bae1dSRodney W. Grimes goto dropafterack; 1299df8bae1dSRodney W. Grimes } else 1300df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1301df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1302df8bae1dSRodney W. Grimes ti->ti_len -= todrop; 1303df8bae1dSRodney W. Grimes tiflags &= ~(TH_PUSH|TH_FIN); 1304df8bae1dSRodney W. Grimes } 1305df8bae1dSRodney W. Grimes 1306df8bae1dSRodney W. Grimes /* 1307df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1308df8bae1dSRodney W. Grimes * record its timestamp. 1309a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1310a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1311df8bae1dSRodney W. Grimes */ 1312a0292f23SGarrett Wollman if ((to.to_flag & TOF_TS) != 0 && 1313a0292f23SGarrett Wollman SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) { 13149b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1315a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1316df8bae1dSRodney W. Grimes } 1317df8bae1dSRodney W. Grimes 1318df8bae1dSRodney W. Grimes /* 1319df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 1320df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 1321df8bae1dSRodney W. Grimes */ 1322df8bae1dSRodney W. Grimes if (tiflags & TH_SYN) { 1323df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 1324df8bae1dSRodney W. Grimes goto dropwithreset; 1325df8bae1dSRodney W. Grimes } 1326df8bae1dSRodney W. Grimes 1327a0292f23SGarrett Wollman /* 1328a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1329a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 1330a0292f23SGarrett Wollman * later processing; else drop segment and return. 1331a0292f23SGarrett Wollman */ 1332a0292f23SGarrett Wollman if ((tiflags & TH_ACK) == 0) { 1333a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 1334a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 1335a0292f23SGarrett Wollman goto step6; 1336a0292f23SGarrett Wollman else 1337a0292f23SGarrett Wollman goto drop; 1338a0292f23SGarrett Wollman } 1339df8bae1dSRodney W. Grimes 1340df8bae1dSRodney W. Grimes /* 1341df8bae1dSRodney W. Grimes * Ack processing. 1342df8bae1dSRodney W. Grimes */ 1343df8bae1dSRodney W. Grimes switch (tp->t_state) { 1344df8bae1dSRodney W. Grimes 1345df8bae1dSRodney W. Grimes /* 1346764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1347764d8cefSBill Fenner * ESTABLISHED state and continue processing. 1348764d8cefSBill Fenner * The ACK was checked above. 1349df8bae1dSRodney W. Grimes */ 1350df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1351a0292f23SGarrett Wollman 1352df8bae1dSRodney W. Grimes tcpstat.tcps_connects++; 1353df8bae1dSRodney W. Grimes soisconnected(so); 1354df8bae1dSRodney W. Grimes /* Do window scaling? */ 1355df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1356df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1357df8bae1dSRodney W. Grimes tp->snd_scale = tp->requested_s_scale; 1358df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 1359df8bae1dSRodney W. Grimes } 1360a0292f23SGarrett Wollman /* 1361a0292f23SGarrett Wollman * Upon successful completion of 3-way handshake, 1362a0292f23SGarrett Wollman * update cache.CC if it was undefined, pass any queued 1363a0292f23SGarrett Wollman * data to the user, and advance state appropriately. 1364a0292f23SGarrett Wollman */ 1365a0292f23SGarrett Wollman if ((taop = tcp_gettaocache(inp)) != NULL && 1366a0292f23SGarrett Wollman taop->tao_cc == 0) 1367a0292f23SGarrett Wollman taop->tao_cc = tp->cc_recv; 1368a0292f23SGarrett Wollman 1369a0292f23SGarrett Wollman /* 1370a0292f23SGarrett Wollman * Make transitions: 1371a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 1372a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 1373a0292f23SGarrett Wollman */ 13749b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1375a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1376a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1377a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 13787ff19458SPaul Traina } else { 1379a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 13809b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepidle, 13819b8b58e0SJonathan Lemon tcp_timer_keep, tp); 13827ff19458SPaul Traina } 1383a0292f23SGarrett Wollman /* 1384a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 1385a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 1386a0292f23SGarrett Wollman */ 1387a0292f23SGarrett Wollman if (ti->ti_len == 0 && (tiflags & TH_FIN) == 0) 1388a0292f23SGarrett Wollman (void) tcp_reass(tp, (struct tcpiphdr *)0, 1389a0292f23SGarrett Wollman (struct mbuf *)0); 1390df8bae1dSRodney W. Grimes tp->snd_wl1 = ti->ti_seq - 1; 1391df8bae1dSRodney W. Grimes /* fall into ... */ 1392df8bae1dSRodney W. Grimes 1393df8bae1dSRodney W. Grimes /* 1394df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1395df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 1396df8bae1dSRodney W. Grimes * tp->snd_una < ti->ti_ack <= tp->snd_max 1397df8bae1dSRodney W. Grimes * then advance tp->snd_una to ti->ti_ack and drop 1398df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 1399df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 1400df8bae1dSRodney W. Grimes */ 1401df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1402df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1403df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 1404df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1405df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1406df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 1407df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 1408df8bae1dSRodney W. Grimes 1409df8bae1dSRodney W. Grimes if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 1410df8bae1dSRodney W. Grimes if (ti->ti_len == 0 && tiwin == tp->snd_wnd) { 1411df8bae1dSRodney W. Grimes tcpstat.tcps_rcvdupack++; 1412df8bae1dSRodney W. Grimes /* 1413df8bae1dSRodney W. Grimes * If we have outstanding data (other than 1414df8bae1dSRodney W. Grimes * a window probe), this is a completely 1415df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 1416df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 1417df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 1418df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 1419df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 1420df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 1421df8bae1dSRodney W. Grimes * window so we send only this one 1422df8bae1dSRodney W. Grimes * packet. 1423df8bae1dSRodney W. Grimes * 1424df8bae1dSRodney W. Grimes * We know we're losing at the current 1425df8bae1dSRodney W. Grimes * window size so do congestion avoidance 1426df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 1427df8bae1dSRodney W. Grimes * and pull our congestion window back to 1428df8bae1dSRodney W. Grimes * the new ssthresh). 1429df8bae1dSRodney W. Grimes * 1430df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 1431df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 1432df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 1433df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 1434df8bae1dSRodney W. Grimes * network. 1435df8bae1dSRodney W. Grimes */ 14369b8b58e0SJonathan Lemon if (!callout_active(tp->tt_rexmt) || 1437df8bae1dSRodney W. Grimes ti->ti_ack != tp->snd_una) 1438df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1439df8bae1dSRodney W. Grimes else if (++tp->t_dupacks == tcprexmtthresh) { 1440df8bae1dSRodney W. Grimes tcp_seq onxt = tp->snd_nxt; 1441df8bae1dSRodney W. Grimes u_int win = 1442df8bae1dSRodney W. Grimes min(tp->snd_wnd, tp->snd_cwnd) / 2 / 1443df8bae1dSRodney W. Grimes tp->t_maxseg; 1444df8bae1dSRodney W. Grimes 1445df8bae1dSRodney W. Grimes if (win < 2) 1446df8bae1dSRodney W. Grimes win = 2; 1447df8bae1dSRodney W. Grimes tp->snd_ssthresh = win * tp->t_maxseg; 14489b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 14499b8b58e0SJonathan Lemon tp->t_rtttime = 0; 1450df8bae1dSRodney W. Grimes tp->snd_nxt = ti->ti_ack; 1451df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 1452df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1453df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 1454df8bae1dSRodney W. Grimes tp->t_maxseg * tp->t_dupacks; 1455df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 1456df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 1457df8bae1dSRodney W. Grimes goto drop; 1458df8bae1dSRodney W. Grimes } else if (tp->t_dupacks > tcprexmtthresh) { 1459df8bae1dSRodney W. Grimes tp->snd_cwnd += tp->t_maxseg; 1460df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1461df8bae1dSRodney W. Grimes goto drop; 1462df8bae1dSRodney W. Grimes } 1463df8bae1dSRodney W. Grimes } else 1464df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1465df8bae1dSRodney W. Grimes break; 1466df8bae1dSRodney W. Grimes } 1467df8bae1dSRodney W. Grimes /* 1468df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 1469df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 1470df8bae1dSRodney W. Grimes */ 1471233e8c18SGarrett Wollman if (tp->t_dupacks >= tcprexmtthresh && 1472df8bae1dSRodney W. Grimes tp->snd_cwnd > tp->snd_ssthresh) 1473df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh; 1474df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1475df8bae1dSRodney W. Grimes if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 1476df8bae1dSRodney W. Grimes tcpstat.tcps_rcvacktoomuch++; 1477df8bae1dSRodney W. Grimes goto dropafterack; 1478df8bae1dSRodney W. Grimes } 1479a0292f23SGarrett Wollman /* 1480a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 1481a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 1482a0292f23SGarrett Wollman */ 1483a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 1484a0292f23SGarrett Wollman /* 1485a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 1486a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 148707e43e10SAndras Olah * synchronized). Go to non-starred state, 148807e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 148907e43e10SAndras Olah * we can do window scaling. 1490a0292f23SGarrett Wollman */ 1491a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 1492a0292f23SGarrett Wollman tp->snd_una++; 149307e43e10SAndras Olah /* Do window scaling? */ 149407e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 149507e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 149607e43e10SAndras Olah tp->snd_scale = tp->requested_s_scale; 149707e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 149807e43e10SAndras Olah } 1499a0292f23SGarrett Wollman } 1500a0292f23SGarrett Wollman 1501a0292f23SGarrett Wollman process_ACK: 1502df8bae1dSRodney W. Grimes acked = ti->ti_ack - tp->snd_una; 1503df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 1504df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 1505df8bae1dSRodney W. Grimes 1506df8bae1dSRodney W. Grimes /* 15079b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 15089b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 15099b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 15109b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 15119b8b58e0SJonathan Lemon * we left off. 15129b8b58e0SJonathan Lemon */ 15139b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 15149b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 15159b8b58e0SJonathan Lemon tp->snd_ssthresh = tp->snd_ssthresh_prev; 15169b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 15179b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; /* XXX probably not required */ 15189b8b58e0SJonathan Lemon } 15199b8b58e0SJonathan Lemon 15209b8b58e0SJonathan Lemon /* 1521df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 1522df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 1523df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 1524df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 1525df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 1526df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 1527df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 1528df8bae1dSRodney W. Grimes */ 1529a0292f23SGarrett Wollman if (to.to_flag & TOF_TS) 15309b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 15319b8b58e0SJonathan Lemon else if (tp->t_rtttime && SEQ_GT(ti->ti_ack, tp->t_rtseq)) 15329b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 1533df8bae1dSRodney W. Grimes 1534df8bae1dSRodney W. Grimes /* 1535df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 1536df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 1537df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 1538df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 1539df8bae1dSRodney W. Grimes */ 1540df8bae1dSRodney W. Grimes if (ti->ti_ack == tp->snd_max) { 15419b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 1542df8bae1dSRodney W. Grimes needoutput = 1; 15439b8b58e0SJonathan Lemon } else if (!callout_active(tp->tt_persist)) 15449b8b58e0SJonathan Lemon callout_reset(tp->tt_rexmt, tp->t_rxtcur, 15459b8b58e0SJonathan Lemon tcp_timer_rexmt, tp); 1546a0292f23SGarrett Wollman 1547a0292f23SGarrett Wollman /* 1548a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 1549a0292f23SGarrett Wollman * skip rest of ACK processing. 1550a0292f23SGarrett Wollman */ 1551a0292f23SGarrett Wollman if (acked == 0) 1552a0292f23SGarrett Wollman goto step6; 1553a0292f23SGarrett Wollman 1554df8bae1dSRodney W. Grimes /* 1555df8bae1dSRodney W. Grimes * When new data is acked, open the congestion window. 1556df8bae1dSRodney W. Grimes * If the window gives us less than ssthresh packets 1557df8bae1dSRodney W. Grimes * in flight, open exponentially (maxseg per packet). 1558df8bae1dSRodney W. Grimes * Otherwise open linearly: maxseg per window 155910be5648SGarrett Wollman * (maxseg^2 / cwnd per packet). 1560df8bae1dSRodney W. Grimes */ 1561df8bae1dSRodney W. Grimes { 1562df8bae1dSRodney W. Grimes register u_int cw = tp->snd_cwnd; 1563df8bae1dSRodney W. Grimes register u_int incr = tp->t_maxseg; 1564df8bae1dSRodney W. Grimes 1565df8bae1dSRodney W. Grimes if (cw > tp->snd_ssthresh) 156610be5648SGarrett Wollman incr = incr * incr / cw; 1567df8bae1dSRodney W. Grimes tp->snd_cwnd = min(cw + incr, TCP_MAXWIN << tp->snd_scale); 1568df8bae1dSRodney W. Grimes } 1569df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 1570df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 1571df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 1572df8bae1dSRodney W. Grimes ourfinisacked = 1; 1573df8bae1dSRodney W. Grimes } else { 1574df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 1575df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 1576df8bae1dSRodney W. Grimes ourfinisacked = 0; 1577df8bae1dSRodney W. Grimes } 1578df8bae1dSRodney W. Grimes sowwakeup(so); 1579df8bae1dSRodney W. Grimes tp->snd_una = ti->ti_ack; 1580df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1581df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 1582df8bae1dSRodney W. Grimes 1583df8bae1dSRodney W. Grimes switch (tp->t_state) { 1584df8bae1dSRodney W. Grimes 1585df8bae1dSRodney W. Grimes /* 1586df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 1587df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 1588df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 1589df8bae1dSRodney W. Grimes */ 1590df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1591df8bae1dSRodney W. Grimes if (ourfinisacked) { 1592df8bae1dSRodney W. Grimes /* 1593df8bae1dSRodney W. Grimes * If we can't receive any more 1594df8bae1dSRodney W. Grimes * data, then closing user can proceed. 1595df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 1596df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 1597df8bae1dSRodney W. Grimes * we'll hang forever. 1598df8bae1dSRodney W. Grimes */ 1599df8bae1dSRodney W. Grimes if (so->so_state & SS_CANTRCVMORE) { 1600df8bae1dSRodney W. Grimes soisdisconnected(so); 16019b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, tcp_maxidle, 16029b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 1603df8bae1dSRodney W. Grimes } 1604df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 1605df8bae1dSRodney W. Grimes } 1606df8bae1dSRodney W. Grimes break; 1607df8bae1dSRodney W. Grimes 1608df8bae1dSRodney W. Grimes /* 1609df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 1610df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 1611df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 1612df8bae1dSRodney W. Grimes * the segment. 1613df8bae1dSRodney W. Grimes */ 1614df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1615df8bae1dSRodney W. Grimes if (ourfinisacked) { 1616df8bae1dSRodney W. Grimes tp->t_state = TCPS_TIME_WAIT; 1617df8bae1dSRodney W. Grimes tcp_canceltimers(tp); 1618a0292f23SGarrett Wollman /* Shorten TIME_WAIT [RFC-1644, p.28] */ 1619a0292f23SGarrett Wollman if (tp->cc_recv != 0 && 16209b8b58e0SJonathan Lemon (ticks - tp->t_starttime) < tcp_msl) 16219b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 16229b8b58e0SJonathan Lemon tp->t_rxtcur * 16239b8b58e0SJonathan Lemon TCPTV_TWTRUNC, 16249b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 1625a0292f23SGarrett Wollman else 16269b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 16279b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 1628df8bae1dSRodney W. Grimes soisdisconnected(so); 1629df8bae1dSRodney W. Grimes } 1630df8bae1dSRodney W. Grimes break; 1631df8bae1dSRodney W. Grimes 1632df8bae1dSRodney W. Grimes /* 1633df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 1634df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 1635df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 1636df8bae1dSRodney W. Grimes * enter the closed state and return. 1637df8bae1dSRodney W. Grimes */ 1638df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 1639df8bae1dSRodney W. Grimes if (ourfinisacked) { 1640df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1641df8bae1dSRodney W. Grimes goto drop; 1642df8bae1dSRodney W. Grimes } 1643df8bae1dSRodney W. Grimes break; 1644df8bae1dSRodney W. Grimes 1645df8bae1dSRodney W. Grimes /* 1646df8bae1dSRodney W. Grimes * In TIME_WAIT state the only thing that should arrive 1647df8bae1dSRodney W. Grimes * is a retransmission of the remote FIN. Acknowledge 1648df8bae1dSRodney W. Grimes * it and restart the finack timer. 1649df8bae1dSRodney W. Grimes */ 1650df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 16519b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 16529b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 1653df8bae1dSRodney W. Grimes goto dropafterack; 1654df8bae1dSRodney W. Grimes } 1655df8bae1dSRodney W. Grimes } 1656df8bae1dSRodney W. Grimes 1657df8bae1dSRodney W. Grimes step6: 1658df8bae1dSRodney W. Grimes /* 1659df8bae1dSRodney W. Grimes * Update window information. 1660df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 1661df8bae1dSRodney W. Grimes */ 1662df8bae1dSRodney W. Grimes if ((tiflags & TH_ACK) && 1663623ae52eSPoul-Henning Kamp (SEQ_LT(tp->snd_wl1, ti->ti_seq) || 1664623ae52eSPoul-Henning Kamp (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 1665623ae52eSPoul-Henning Kamp (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) { 1666df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 1667df8bae1dSRodney W. Grimes if (ti->ti_len == 0 && 1668df8bae1dSRodney W. Grimes tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd) 1669df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinupd++; 1670df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 1671df8bae1dSRodney W. Grimes tp->snd_wl1 = ti->ti_seq; 1672df8bae1dSRodney W. Grimes tp->snd_wl2 = ti->ti_ack; 1673df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 1674df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 1675df8bae1dSRodney W. Grimes needoutput = 1; 1676df8bae1dSRodney W. Grimes } 1677df8bae1dSRodney W. Grimes 1678df8bae1dSRodney W. Grimes /* 1679df8bae1dSRodney W. Grimes * Process segments with URG. 1680df8bae1dSRodney W. Grimes */ 1681df8bae1dSRodney W. Grimes if ((tiflags & TH_URG) && ti->ti_urp && 1682df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1683df8bae1dSRodney W. Grimes /* 1684df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 1685df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 1686df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 1687df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 1688df8bae1dSRodney W. Grimes */ 1689df8bae1dSRodney W. Grimes if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) { 1690df8bae1dSRodney W. Grimes ti->ti_urp = 0; /* XXX */ 1691df8bae1dSRodney W. Grimes tiflags &= ~TH_URG; /* XXX */ 1692df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 1693df8bae1dSRodney W. Grimes } 1694df8bae1dSRodney W. Grimes /* 1695df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 1696df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 1697df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 1698df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 1699df8bae1dSRodney W. Grimes * In these states we ignore the URG. 1700df8bae1dSRodney W. Grimes * 1701df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 1702df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 1703df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 1704df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 1705df8bae1dSRodney W. Grimes * of data past the urgent section as the original 1706df8bae1dSRodney W. Grimes * spec states (in one of two places). 1707df8bae1dSRodney W. Grimes */ 1708df8bae1dSRodney W. Grimes if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 1709df8bae1dSRodney W. Grimes tp->rcv_up = ti->ti_seq + ti->ti_urp; 1710df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 1711df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 1712df8bae1dSRodney W. Grimes if (so->so_oobmark == 0) 1713df8bae1dSRodney W. Grimes so->so_state |= SS_RCVATMARK; 1714df8bae1dSRodney W. Grimes sohasoutofband(so); 1715df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1716df8bae1dSRodney W. Grimes } 1717df8bae1dSRodney W. Grimes /* 1718df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 1719df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 1720df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 1721df8bae1dSRodney W. Grimes * data may creep in... ick. 1722df8bae1dSRodney W. Grimes */ 172326f9a767SRodney W. Grimes if (ti->ti_urp <= (u_long)ti->ti_len 1724df8bae1dSRodney W. Grimes #ifdef SO_OOBINLINE 1725df8bae1dSRodney W. Grimes && (so->so_options & SO_OOBINLINE) == 0 1726df8bae1dSRodney W. Grimes #endif 1727df8bae1dSRodney W. Grimes ) 1728df8bae1dSRodney W. Grimes tcp_pulloutofband(so, ti, m); 1729df8bae1dSRodney W. Grimes } else 1730df8bae1dSRodney W. Grimes /* 1731df8bae1dSRodney W. Grimes * If no out of band data is expected, 1732df8bae1dSRodney W. Grimes * pull receive urgent pointer along 1733df8bae1dSRodney W. Grimes * with the receive window. 1734df8bae1dSRodney W. Grimes */ 1735df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 1736df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 1737df8bae1dSRodney W. Grimes dodata: /* XXX */ 1738df8bae1dSRodney W. Grimes 1739df8bae1dSRodney W. Grimes /* 1740df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 1741df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 1742df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 1743df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 1744df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 1745df8bae1dSRodney W. Grimes * connection then we just ignore the text. 1746df8bae1dSRodney W. Grimes */ 1747df8bae1dSRodney W. Grimes if ((ti->ti_len || (tiflags&TH_FIN)) && 1748df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1749df8bae1dSRodney W. Grimes TCP_REASS(tp, ti, m, so, tiflags); 1750df8bae1dSRodney W. Grimes /* 1751df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 1752df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 1753df8bae1dSRodney W. Grimes * buffer size. 1754df8bae1dSRodney W. Grimes */ 1755df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 1756df8bae1dSRodney W. Grimes } else { 1757df8bae1dSRodney W. Grimes m_freem(m); 1758df8bae1dSRodney W. Grimes tiflags &= ~TH_FIN; 1759df8bae1dSRodney W. Grimes } 1760df8bae1dSRodney W. Grimes 1761df8bae1dSRodney W. Grimes /* 1762df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 1763df8bae1dSRodney W. Grimes * that the connection is closing. 1764df8bae1dSRodney W. Grimes */ 1765df8bae1dSRodney W. Grimes if (tiflags & TH_FIN) { 1766df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1767df8bae1dSRodney W. Grimes socantrcvmore(so); 1768a0292f23SGarrett Wollman /* 1769a0292f23SGarrett Wollman * If connection is half-synchronized 1770d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 1771a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 1772a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 1773a0292f23SGarrett Wollman * more input can be expected, send ACK now. 1774a0292f23SGarrett Wollman */ 1775f498eeeeSDavid Greenman if (tcp_delack_enabled && (tp->t_flags & TF_NEEDSYN)) 17769b8b58e0SJonathan Lemon callout_reset(tp->tt_delack, tcp_delacktime, 17779b8b58e0SJonathan Lemon tcp_timer_delack, tp); 1778a0292f23SGarrett Wollman else 1779df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1780df8bae1dSRodney W. Grimes tp->rcv_nxt++; 1781df8bae1dSRodney W. Grimes } 1782df8bae1dSRodney W. Grimes switch (tp->t_state) { 1783df8bae1dSRodney W. Grimes 1784df8bae1dSRodney W. Grimes /* 1785df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 1786df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 1787df8bae1dSRodney W. Grimes */ 1788df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 17899b8b58e0SJonathan Lemon tp->t_starttime = ticks; 17909b8b58e0SJonathan Lemon /*FALLTHROUGH*/ 1791df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1792df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 1793df8bae1dSRodney W. Grimes break; 1794df8bae1dSRodney W. Grimes 1795df8bae1dSRodney W. Grimes /* 1796df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 1797df8bae1dSRodney W. Grimes * enter the CLOSING state. 1798df8bae1dSRodney W. Grimes */ 1799df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1800df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 1801df8bae1dSRodney W. Grimes break; 1802df8bae1dSRodney W. Grimes 1803df8bae1dSRodney W. Grimes /* 1804df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 1805df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 1806df8bae1dSRodney W. Grimes * standard timers. 1807df8bae1dSRodney W. Grimes */ 1808df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 1809df8bae1dSRodney W. Grimes tp->t_state = TCPS_TIME_WAIT; 1810df8bae1dSRodney W. Grimes tcp_canceltimers(tp); 1811a0292f23SGarrett Wollman /* Shorten TIME_WAIT [RFC-1644, p.28] */ 1812a0292f23SGarrett Wollman if (tp->cc_recv != 0 && 18139b8b58e0SJonathan Lemon (ticks - tp->t_starttime) < tcp_msl) { 18149b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 18159b8b58e0SJonathan Lemon tp->t_rxtcur * TCPTV_TWTRUNC, 18169b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 1817a0292f23SGarrett Wollman /* For transaction client, force ACK now. */ 1818a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1819a0292f23SGarrett Wollman } 1820a0292f23SGarrett Wollman else 18219b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 18229b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 1823df8bae1dSRodney W. Grimes soisdisconnected(so); 1824df8bae1dSRodney W. Grimes break; 1825df8bae1dSRodney W. Grimes 1826df8bae1dSRodney W. Grimes /* 1827df8bae1dSRodney W. Grimes * In TIME_WAIT state restart the 2 MSL time_wait timer. 1828df8bae1dSRodney W. Grimes */ 1829df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 18309b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, 2 * tcp_msl, 18319b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 1832df8bae1dSRodney W. Grimes break; 1833df8bae1dSRodney W. Grimes } 1834df8bae1dSRodney W. Grimes } 1835610ee2f9SDavid Greenman #ifdef TCPDEBUG 1836df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) 1837df8bae1dSRodney W. Grimes tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 1838610ee2f9SDavid Greenman #endif 1839df8bae1dSRodney W. Grimes 1840df8bae1dSRodney W. Grimes /* 1841df8bae1dSRodney W. Grimes * Return any desired output. 1842df8bae1dSRodney W. Grimes */ 1843df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 1844df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1845df8bae1dSRodney W. Grimes return; 1846df8bae1dSRodney W. Grimes 1847df8bae1dSRodney W. Grimes dropafterack: 1848df8bae1dSRodney W. Grimes /* 1849df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 1850df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 185180ab7c0eSGarrett Wollman * 185280ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 185380ab7c0eSGarrett Wollman * paths to this code happen after packets containing 185480ab7c0eSGarrett Wollman * RST have been dropped. 185580ab7c0eSGarrett Wollman * 185680ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 185780ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 185880ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 185980ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 186080ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 186180ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 1862df8bae1dSRodney W. Grimes */ 186380ab7c0eSGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED && (tiflags & TH_ACK) && 186480ab7c0eSGarrett Wollman (SEQ_GT(tp->snd_una, ti->ti_ack) || 186580ab7c0eSGarrett Wollman SEQ_GT(ti->ti_ack, tp->snd_max)) ) 186680ab7c0eSGarrett Wollman goto dropwithreset; 1867a0292f23SGarrett Wollman #ifdef TCPDEBUG 1868a0292f23SGarrett Wollman if (so->so_options & SO_DEBUG) 1869a0292f23SGarrett Wollman tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1870a0292f23SGarrett Wollman #endif 1871df8bae1dSRodney W. Grimes m_freem(m); 1872df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1873df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1874df8bae1dSRodney W. Grimes return; 1875df8bae1dSRodney W. Grimes 1876df8bae1dSRodney W. Grimes dropwithreset: 1877e46cd3d4SDag-Erling Smørgrav #ifdef TCP_RESTRICT_RST 1878e46cd3d4SDag-Erling Smørgrav if (restrict_rst) 1879e46cd3d4SDag-Erling Smørgrav goto drop; 1880e46cd3d4SDag-Erling Smørgrav #endif 1881df8bae1dSRodney W. Grimes /* 1882df8bae1dSRodney W. Grimes * Generate a RST, dropping incoming segment. 1883df8bae1dSRodney W. Grimes * Make ACK acceptable to originator of segment. 1884df8bae1dSRodney W. Grimes * Don't bother to respond if destination was broadcast/multicast. 1885df8bae1dSRodney W. Grimes */ 1886df8bae1dSRodney W. Grimes if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) || 1887d4d0967eSDavid Greenman IN_MULTICAST(ntohl(ti->ti_dst.s_addr))) 1888df8bae1dSRodney W. Grimes goto drop; 1889a0292f23SGarrett Wollman #ifdef TCPDEBUG 1890a0292f23SGarrett Wollman if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 1891a0292f23SGarrett Wollman tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1892a0292f23SGarrett Wollman #endif 1893df8bae1dSRodney W. Grimes if (tiflags & TH_ACK) 1894df8bae1dSRodney W. Grimes tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST); 1895df8bae1dSRodney W. Grimes else { 1896df8bae1dSRodney W. Grimes if (tiflags & TH_SYN) 1897df8bae1dSRodney W. Grimes ti->ti_len++; 1898df8bae1dSRodney W. Grimes tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0, 1899df8bae1dSRodney W. Grimes TH_RST|TH_ACK); 1900df8bae1dSRodney W. Grimes } 1901df8bae1dSRodney W. Grimes /* destroy temporarily created socket */ 1902df8bae1dSRodney W. Grimes if (dropsocket) 1903df8bae1dSRodney W. Grimes (void) soabort(so); 1904df8bae1dSRodney W. Grimes return; 1905df8bae1dSRodney W. Grimes 1906df8bae1dSRodney W. Grimes drop: 1907df8bae1dSRodney W. Grimes /* 1908df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 1909df8bae1dSRodney W. Grimes */ 1910610ee2f9SDavid Greenman #ifdef TCPDEBUG 1911a0292f23SGarrett Wollman if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 1912a0292f23SGarrett Wollman tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1913a0292f23SGarrett Wollman #endif 1914df8bae1dSRodney W. Grimes m_freem(m); 1915df8bae1dSRodney W. Grimes /* destroy temporarily created socket */ 1916df8bae1dSRodney W. Grimes if (dropsocket) 1917df8bae1dSRodney W. Grimes (void) soabort(so); 1918df8bae1dSRodney W. Grimes return; 1919df8bae1dSRodney W. Grimes } 1920df8bae1dSRodney W. Grimes 19210312fbe9SPoul-Henning Kamp static void 1922a0292f23SGarrett Wollman tcp_dooptions(tp, cp, cnt, ti, to) 1923df8bae1dSRodney W. Grimes struct tcpcb *tp; 1924df8bae1dSRodney W. Grimes u_char *cp; 1925df8bae1dSRodney W. Grimes int cnt; 1926df8bae1dSRodney W. Grimes struct tcpiphdr *ti; 1927a0292f23SGarrett Wollman struct tcpopt *to; 1928df8bae1dSRodney W. Grimes { 1929a0292f23SGarrett Wollman u_short mss = 0; 1930df8bae1dSRodney W. Grimes int opt, optlen; 1931df8bae1dSRodney W. Grimes 1932df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 1933df8bae1dSRodney W. Grimes opt = cp[0]; 1934df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 1935df8bae1dSRodney W. Grimes break; 1936df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 1937df8bae1dSRodney W. Grimes optlen = 1; 1938df8bae1dSRodney W. Grimes else { 1939df8bae1dSRodney W. Grimes optlen = cp[1]; 1940df8bae1dSRodney W. Grimes if (optlen <= 0) 1941df8bae1dSRodney W. Grimes break; 1942df8bae1dSRodney W. Grimes } 1943df8bae1dSRodney W. Grimes switch (opt) { 1944df8bae1dSRodney W. Grimes 1945df8bae1dSRodney W. Grimes default: 1946df8bae1dSRodney W. Grimes continue; 1947df8bae1dSRodney W. Grimes 1948df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 1949df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 1950df8bae1dSRodney W. Grimes continue; 1951df8bae1dSRodney W. Grimes if (!(ti->ti_flags & TH_SYN)) 1952df8bae1dSRodney W. Grimes continue; 1953df8bae1dSRodney W. Grimes bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); 1954df8bae1dSRodney W. Grimes NTOHS(mss); 1955df8bae1dSRodney W. Grimes break; 1956df8bae1dSRodney W. Grimes 1957df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 1958df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 1959df8bae1dSRodney W. Grimes continue; 1960df8bae1dSRodney W. Grimes if (!(ti->ti_flags & TH_SYN)) 1961df8bae1dSRodney W. Grimes continue; 1962df8bae1dSRodney W. Grimes tp->t_flags |= TF_RCVD_SCALE; 1963df8bae1dSRodney W. Grimes tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 1964df8bae1dSRodney W. Grimes break; 1965df8bae1dSRodney W. Grimes 1966df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 1967df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 1968df8bae1dSRodney W. Grimes continue; 1969a0292f23SGarrett Wollman to->to_flag |= TOF_TS; 1970a0292f23SGarrett Wollman bcopy((char *)cp + 2, 1971a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 1972a0292f23SGarrett Wollman NTOHL(to->to_tsval); 1973a0292f23SGarrett Wollman bcopy((char *)cp + 6, 1974a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 1975a0292f23SGarrett Wollman NTOHL(to->to_tsecr); 1976df8bae1dSRodney W. Grimes 1977df8bae1dSRodney W. Grimes /* 1978df8bae1dSRodney W. Grimes * A timestamp received in a SYN makes 1979df8bae1dSRodney W. Grimes * it ok to send timestamp requests and replies. 1980df8bae1dSRodney W. Grimes */ 1981df8bae1dSRodney W. Grimes if (ti->ti_flags & TH_SYN) { 1982df8bae1dSRodney W. Grimes tp->t_flags |= TF_RCVD_TSTMP; 1983a0292f23SGarrett Wollman tp->ts_recent = to->to_tsval; 19849b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1985df8bae1dSRodney W. Grimes } 1986df8bae1dSRodney W. Grimes break; 1987a0292f23SGarrett Wollman case TCPOPT_CC: 1988a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 1989a0292f23SGarrett Wollman continue; 199040db8ef7SAndras Olah to->to_flag |= TOF_CC; 1991a0292f23SGarrett Wollman bcopy((char *)cp + 2, 1992a0292f23SGarrett Wollman (char *)&to->to_cc, sizeof(to->to_cc)); 1993a0292f23SGarrett Wollman NTOHL(to->to_cc); 1994a0292f23SGarrett Wollman /* 1995a0292f23SGarrett Wollman * A CC or CC.new option received in a SYN makes 1996a0292f23SGarrett Wollman * it ok to send CC in subsequent segments. 1997a0292f23SGarrett Wollman */ 1998a0292f23SGarrett Wollman if (ti->ti_flags & TH_SYN) 1999a0292f23SGarrett Wollman tp->t_flags |= TF_RCVD_CC; 2000a0292f23SGarrett Wollman break; 2001a0292f23SGarrett Wollman case TCPOPT_CCNEW: 2002a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2003a0292f23SGarrett Wollman continue; 2004a0292f23SGarrett Wollman if (!(ti->ti_flags & TH_SYN)) 2005a0292f23SGarrett Wollman continue; 2006a0292f23SGarrett Wollman to->to_flag |= TOF_CCNEW; 2007a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2008a0292f23SGarrett Wollman (char *)&to->to_cc, sizeof(to->to_cc)); 2009a0292f23SGarrett Wollman NTOHL(to->to_cc); 2010a0292f23SGarrett Wollman /* 2011a0292f23SGarrett Wollman * A CC or CC.new option received in a SYN makes 2012a0292f23SGarrett Wollman * it ok to send CC in subsequent segments. 2013a0292f23SGarrett Wollman */ 2014a0292f23SGarrett Wollman tp->t_flags |= TF_RCVD_CC; 2015a0292f23SGarrett Wollman break; 2016a0292f23SGarrett Wollman case TCPOPT_CCECHO: 2017a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 2018a0292f23SGarrett Wollman continue; 2019a0292f23SGarrett Wollman if (!(ti->ti_flags & TH_SYN)) 2020a0292f23SGarrett Wollman continue; 2021a0292f23SGarrett Wollman to->to_flag |= TOF_CCECHO; 2022a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2023a0292f23SGarrett Wollman (char *)&to->to_ccecho, sizeof(to->to_ccecho)); 2024a0292f23SGarrett Wollman NTOHL(to->to_ccecho); 2025a0292f23SGarrett Wollman break; 2026df8bae1dSRodney W. Grimes } 2027df8bae1dSRodney W. Grimes } 2028a0292f23SGarrett Wollman if (ti->ti_flags & TH_SYN) 2029a0292f23SGarrett Wollman tcp_mss(tp, mss); /* sets t_maxseg */ 2030df8bae1dSRodney W. Grimes } 2031df8bae1dSRodney W. Grimes 2032df8bae1dSRodney W. Grimes /* 2033df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 2034df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 2035df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 2036df8bae1dSRodney W. Grimes * sequencing purposes. 2037df8bae1dSRodney W. Grimes */ 20380312fbe9SPoul-Henning Kamp static void 2039df8bae1dSRodney W. Grimes tcp_pulloutofband(so, ti, m) 2040df8bae1dSRodney W. Grimes struct socket *so; 2041df8bae1dSRodney W. Grimes struct tcpiphdr *ti; 2042df8bae1dSRodney W. Grimes register struct mbuf *m; 2043df8bae1dSRodney W. Grimes { 2044df8bae1dSRodney W. Grimes int cnt = ti->ti_urp - 1; 2045df8bae1dSRodney W. Grimes 2046df8bae1dSRodney W. Grimes while (cnt >= 0) { 2047df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 2048df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 2049df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 2050df8bae1dSRodney W. Grimes 2051df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 2052df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 2053df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2054df8bae1dSRodney W. Grimes m->m_len--; 2055df8bae1dSRodney W. Grimes return; 2056df8bae1dSRodney W. Grimes } 2057df8bae1dSRodney W. Grimes cnt -= m->m_len; 2058df8bae1dSRodney W. Grimes m = m->m_next; 2059df8bae1dSRodney W. Grimes if (m == 0) 2060df8bae1dSRodney W. Grimes break; 2061df8bae1dSRodney W. Grimes } 2062df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 2063df8bae1dSRodney W. Grimes } 2064df8bae1dSRodney W. Grimes 2065df8bae1dSRodney W. Grimes /* 2066df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 2067df8bae1dSRodney W. Grimes * and update averages and current timeout. 2068df8bae1dSRodney W. Grimes */ 20690312fbe9SPoul-Henning Kamp static void 2070df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt) 2071df8bae1dSRodney W. Grimes register struct tcpcb *tp; 20729b8b58e0SJonathan Lemon int rtt; 2073df8bae1dSRodney W. Grimes { 2074233e8c18SGarrett Wollman register int delta; 2075233e8c18SGarrett Wollman 2076233e8c18SGarrett Wollman tcpstat.tcps_rttupdated++; 2077233e8c18SGarrett Wollman tp->t_rttupdated++; 2078233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 2079233e8c18SGarrett Wollman /* 2080233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 2081233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 2082233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 2083233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2084233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 2085233e8c18SGarrett Wollman */ 2086233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2087233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2088233e8c18SGarrett Wollman 2089233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 2090233e8c18SGarrett Wollman tp->t_srtt = 1; 2091233e8c18SGarrett Wollman 2092233e8c18SGarrett Wollman /* 2093233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 2094233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 2095233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 2096233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 2097233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 2098233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 2099233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2100233e8c18SGarrett Wollman * rfc793's wired-in beta. 2101233e8c18SGarrett Wollman */ 2102233e8c18SGarrett Wollman if (delta < 0) 2103233e8c18SGarrett Wollman delta = -delta; 2104233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2105233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 2106233e8c18SGarrett Wollman tp->t_rttvar = 1; 2107233e8c18SGarrett Wollman } else { 2108233e8c18SGarrett Wollman /* 2109233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 2110233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 2111233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 2112233e8c18SGarrett Wollman */ 2113233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 2114233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 2115233e8c18SGarrett Wollman } 21169b8b58e0SJonathan Lemon tp->t_rtttime = 0; 2117df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 2118df8bae1dSRodney W. Grimes 2119df8bae1dSRodney W. Grimes /* 2120df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 2121df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 2122df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 2123df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 2124df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 2125df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 2126df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 2127df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 2128df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 2129df8bae1dSRodney W. Grimes */ 2130233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 21319e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2132df8bae1dSRodney W. Grimes 2133df8bae1dSRodney W. Grimes /* 2134df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 2135df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 2136df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 2137df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 2138df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 2139df8bae1dSRodney W. Grimes */ 2140df8bae1dSRodney W. Grimes tp->t_softerror = 0; 2141df8bae1dSRodney W. Grimes } 2142df8bae1dSRodney W. Grimes 2143df8bae1dSRodney W. Grimes /* 2144df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 2145df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 2146df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 2147df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 2148df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2149df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 2150df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 2151df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 2152df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 2153df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 2154df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 2155df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 2156df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 2157a0292f23SGarrett Wollman * 2158a0292f23SGarrett Wollman * Also take into account the space needed for options that we 2159a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 2160a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 2161a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 2162a0292f23SGarrett Wollman * data in maxopd. 2163a0292f23SGarrett Wollman * 2164a0292f23SGarrett Wollman * NOTE that this routine is only called when we process an incoming 2165a0292f23SGarrett Wollman * segment, for outgoing segments only tcp_mssopt is called. 2166a0292f23SGarrett Wollman * 2167a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 2168a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 2169a0292f23SGarrett Wollman * MSS of our peer. 2170df8bae1dSRodney W. Grimes */ 2171a0292f23SGarrett Wollman void 2172df8bae1dSRodney W. Grimes tcp_mss(tp, offer) 2173a0292f23SGarrett Wollman struct tcpcb *tp; 2174a0292f23SGarrett Wollman int offer; 2175df8bae1dSRodney W. Grimes { 2176df8bae1dSRodney W. Grimes register struct rtentry *rt; 2177df8bae1dSRodney W. Grimes struct ifnet *ifp; 2178df8bae1dSRodney W. Grimes register int rtt, mss; 2179df8bae1dSRodney W. Grimes u_long bufsize; 2180df8bae1dSRodney W. Grimes struct inpcb *inp; 2181df8bae1dSRodney W. Grimes struct socket *so; 2182a0292f23SGarrett Wollman struct rmxp_tao *taop; 2183a0292f23SGarrett Wollman int origoffer = offer; 2184df8bae1dSRodney W. Grimes 2185df8bae1dSRodney W. Grimes inp = tp->t_inpcb; 2186a0292f23SGarrett Wollman if ((rt = tcp_rtlookup(inp)) == NULL) { 2187a0292f23SGarrett Wollman tp->t_maxopd = tp->t_maxseg = tcp_mssdflt; 2188a0292f23SGarrett Wollman return; 2189df8bae1dSRodney W. Grimes } 2190df8bae1dSRodney W. Grimes ifp = rt->rt_ifp; 2191df8bae1dSRodney W. Grimes so = inp->inp_socket; 2192df8bae1dSRodney W. Grimes 2193a0292f23SGarrett Wollman taop = rmx_taop(rt->rt_rmx); 2194a0292f23SGarrett Wollman /* 2195a0292f23SGarrett Wollman * Offer == -1 means that we didn't receive SYN yet, 2196a0292f23SGarrett Wollman * use cached value in that case; 2197a0292f23SGarrett Wollman */ 2198a0292f23SGarrett Wollman if (offer == -1) 2199a0292f23SGarrett Wollman offer = taop->tao_mssopt; 2200a0292f23SGarrett Wollman /* 2201a0292f23SGarrett Wollman * Offer == 0 means that there was no MSS on the SYN segment, 2202a0292f23SGarrett Wollman * in this case we use tcp_mssdflt. 2203a0292f23SGarrett Wollman */ 2204a0292f23SGarrett Wollman if (offer == 0) 2205a0292f23SGarrett Wollman offer = tcp_mssdflt; 2206a0292f23SGarrett Wollman else 2207a0292f23SGarrett Wollman /* 2208a0292f23SGarrett Wollman * Sanity check: make sure that maxopd will be large 2209a0292f23SGarrett Wollman * enough to allow some data on segments even is the 2210a0292f23SGarrett Wollman * all the option space is used (40bytes). Otherwise 2211a0292f23SGarrett Wollman * funny things may happen in tcp_output. 2212a0292f23SGarrett Wollman */ 2213a0292f23SGarrett Wollman offer = max(offer, 64); 2214a0292f23SGarrett Wollman taop->tao_mssopt = offer; 2215a0292f23SGarrett Wollman 2216df8bae1dSRodney W. Grimes /* 2217df8bae1dSRodney W. Grimes * While we're here, check if there's an initial rtt 2218df8bae1dSRodney W. Grimes * or rttvar. Convert from the route-table units 2219df8bae1dSRodney W. Grimes * to scaled multiples of the slow timeout timer. 2220df8bae1dSRodney W. Grimes */ 2221df8bae1dSRodney W. Grimes if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 2222df8bae1dSRodney W. Grimes /* 2223a0292f23SGarrett Wollman * XXX the lock bit for RTT indicates that the value 2224df8bae1dSRodney W. Grimes * is also a minimum value; this is subject to time. 2225df8bae1dSRodney W. Grimes */ 2226df8bae1dSRodney W. Grimes if (rt->rt_rmx.rmx_locks & RTV_RTT) 22279b8b58e0SJonathan Lemon tp->t_rttmin = rtt / (RTM_RTTUNIT / hz); 22289b8b58e0SJonathan Lemon tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE)); 2229dd224982SGarrett Wollman tcpstat.tcps_usedrtt++; 2230dd224982SGarrett Wollman if (rt->rt_rmx.rmx_rttvar) { 2231df8bae1dSRodney W. Grimes tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 22329b8b58e0SJonathan Lemon (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE)); 2233dd224982SGarrett Wollman tcpstat.tcps_usedrttvar++; 2234dd224982SGarrett Wollman } else { 2235df8bae1dSRodney W. Grimes /* default variation is +- 1 rtt */ 2236df8bae1dSRodney W. Grimes tp->t_rttvar = 2237df8bae1dSRodney W. Grimes tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 2238dd224982SGarrett Wollman } 2239df8bae1dSRodney W. Grimes TCPT_RANGESET(tp->t_rxtcur, 2240df8bae1dSRodney W. Grimes ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 2241df8bae1dSRodney W. Grimes tp->t_rttmin, TCPTV_REXMTMAX); 2242df8bae1dSRodney W. Grimes } 2243df8bae1dSRodney W. Grimes /* 2244df8bae1dSRodney W. Grimes * if there's an mtu associated with the route, use it 2245df8bae1dSRodney W. Grimes */ 2246df8bae1dSRodney W. Grimes if (rt->rt_rmx.rmx_mtu) 2247df8bae1dSRodney W. Grimes mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr); 2248df8bae1dSRodney W. Grimes else 2249df8bae1dSRodney W. Grimes { 2250df8bae1dSRodney W. Grimes mss = ifp->if_mtu - sizeof(struct tcpiphdr); 2251a0292f23SGarrett Wollman if (!in_localaddr(inp->inp_faddr)) 2252a0292f23SGarrett Wollman mss = min(mss, tcp_mssdflt); 2253a0292f23SGarrett Wollman } 2254a0292f23SGarrett Wollman mss = min(mss, offer); 2255a0292f23SGarrett Wollman /* 2256a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 2257a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 2258a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 2259a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 2260a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 2261a0292f23SGarrett Wollman */ 2262a0292f23SGarrett Wollman tp->t_maxopd = mss; 2263a0292f23SGarrett Wollman 2264a0292f23SGarrett Wollman /* 2265a0292f23SGarrett Wollman * In case of T/TCP, origoffer==-1 indicates, that no segments 2266a0292f23SGarrett Wollman * were received yet. In this case we just guess, otherwise 2267a0292f23SGarrett Wollman * we do the same as before T/TCP. 2268a0292f23SGarrett Wollman */ 2269a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2270a0292f23SGarrett Wollman (origoffer == -1 || 2271a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2272a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 2273a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 2274a0292f23SGarrett Wollman (origoffer == -1 || 2275a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)) 2276a0292f23SGarrett Wollman mss -= TCPOLEN_CC_APPA; 2277a0292f23SGarrett Wollman 2278df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 2279df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2280df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 2281df8bae1dSRodney W. Grimes #else 2282df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2283df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 2284df8bae1dSRodney W. Grimes #endif 2285df8bae1dSRodney W. Grimes /* 2286df8bae1dSRodney W. Grimes * If there's a pipesize, change the socket buffer 2287df8bae1dSRodney W. Grimes * to that size. Make the socket buffers an integral 2288df8bae1dSRodney W. Grimes * number of mss units; if the mss is larger than 2289df8bae1dSRodney W. Grimes * the socket buffer, decrease the mss. 2290df8bae1dSRodney W. Grimes */ 2291df8bae1dSRodney W. Grimes #ifdef RTV_SPIPE 2292df8bae1dSRodney W. Grimes if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 2293df8bae1dSRodney W. Grimes #endif 2294df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 2295df8bae1dSRodney W. Grimes if (bufsize < mss) 2296df8bae1dSRodney W. Grimes mss = bufsize; 2297df8bae1dSRodney W. Grimes else { 2298df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2299df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2300df8bae1dSRodney W. Grimes bufsize = sb_max; 2301df8bae1dSRodney W. Grimes (void)sbreserve(&so->so_snd, bufsize); 2302df8bae1dSRodney W. Grimes } 2303df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 2304df8bae1dSRodney W. Grimes 2305df8bae1dSRodney W. Grimes #ifdef RTV_RPIPE 2306df8bae1dSRodney W. Grimes if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 2307df8bae1dSRodney W. Grimes #endif 2308df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 2309df8bae1dSRodney W. Grimes if (bufsize > mss) { 2310df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2311df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2312df8bae1dSRodney W. Grimes bufsize = sb_max; 2313df8bae1dSRodney W. Grimes (void)sbreserve(&so->so_rcv, bufsize); 2314df8bae1dSRodney W. Grimes } 23159b8b58e0SJonathan Lemon 2316a0292f23SGarrett Wollman /* 23179b8b58e0SJonathan Lemon * Set the slow-start flight size depending on whether this 23189b8b58e0SJonathan Lemon * is a local network or not. 2319a0292f23SGarrett Wollman */ 23209b8b58e0SJonathan Lemon if (in_localaddr(inp->inp_faddr)) 23219b8b58e0SJonathan Lemon tp->snd_cwnd = mss * ss_fltsz_local; 23229b8b58e0SJonathan Lemon else 23239b8b58e0SJonathan Lemon tp->snd_cwnd = mss * ss_fltsz; 2324df8bae1dSRodney W. Grimes 2325df8bae1dSRodney W. Grimes if (rt->rt_rmx.rmx_ssthresh) { 2326df8bae1dSRodney W. Grimes /* 2327df8bae1dSRodney W. Grimes * There's some sort of gateway or interface 2328df8bae1dSRodney W. Grimes * buffer limit on the path. Use this to set 2329df8bae1dSRodney W. Grimes * the slow start threshhold, but set the 2330df8bae1dSRodney W. Grimes * threshold to no less than 2*mss. 2331df8bae1dSRodney W. Grimes */ 2332df8bae1dSRodney W. Grimes tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 2333dd224982SGarrett Wollman tcpstat.tcps_usedssthresh++; 2334df8bae1dSRodney W. Grimes } 2335a0292f23SGarrett Wollman } 2336a0292f23SGarrett Wollman 2337a0292f23SGarrett Wollman /* 2338a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 2339a0292f23SGarrett Wollman */ 2340a0292f23SGarrett Wollman int 2341a0292f23SGarrett Wollman tcp_mssopt(tp) 2342a0292f23SGarrett Wollman struct tcpcb *tp; 2343a0292f23SGarrett Wollman { 2344a0292f23SGarrett Wollman struct rtentry *rt; 2345a0292f23SGarrett Wollman 2346a0292f23SGarrett Wollman rt = tcp_rtlookup(tp->t_inpcb); 2347a0292f23SGarrett Wollman if (rt == NULL) 2348a0292f23SGarrett Wollman return tcp_mssdflt; 2349a0292f23SGarrett Wollman 2350a0292f23SGarrett Wollman return rt->rt_ifp->if_mtu - sizeof(struct tcpiphdr); 2351df8bae1dSRodney W. Grimes } 2352