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 34233e8c18SGarrett Wollman * $Id: tcp_input.c,v 1.38 1996/03/11 15:13:29 davidg Exp $ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37df8bae1dSRodney W. Grimes #ifndef TUBA_INCLUDE 38df8bae1dSRodney W. Grimes #include <sys/param.h> 392ee45d7dSDavid Greenman #include <sys/queue.h> 40df8bae1dSRodney W. Grimes #include <sys/systm.h> 4198163b98SPoul-Henning Kamp #include <sys/kernel.h> 4298163b98SPoul-Henning Kamp #include <sys/sysctl.h> 43df8bae1dSRodney W. Grimes #include <sys/malloc.h> 44df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 45df8bae1dSRodney W. Grimes #include <sys/protosw.h> 46df8bae1dSRodney W. Grimes #include <sys/socket.h> 47df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 48df8bae1dSRodney W. Grimes #include <sys/errno.h> 49df8bae1dSRodney W. Grimes 50e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 51e79adb8eSGarrett Wollman 52df8bae1dSRodney W. Grimes #include <net/if.h> 53df8bae1dSRodney W. Grimes #include <net/route.h> 54df8bae1dSRodney W. Grimes 55df8bae1dSRodney W. Grimes #include <netinet/in.h> 56df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 57df8bae1dSRodney W. Grimes #include <netinet/ip.h> 58df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 59df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 60df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 61df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 62df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 63df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 64df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 65df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 66610ee2f9SDavid Greenman #ifdef TCPDEBUG 67df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 68f708ef1bSPoul-Henning Kamp static struct tcpiphdr tcp_saveti; 69610ee2f9SDavid Greenman #endif 70df8bae1dSRodney W. Grimes 710312fbe9SPoul-Henning Kamp static int tcprexmtthresh = 3; 722f96f1f4SGarrett Wollman tcp_seq tcp_iss; 732f96f1f4SGarrett Wollman tcp_cc tcp_ccgen; 740312fbe9SPoul-Henning Kamp 752f96f1f4SGarrett Wollman struct tcpstat tcpstat; 7698163b98SPoul-Henning Kamp SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, 7798163b98SPoul-Henning Kamp CTLFLAG_RD, &tcpstat , tcpstat, ""); 780312fbe9SPoul-Henning Kamp 792f96f1f4SGarrett Wollman u_long tcp_now; 8015bd2b43SDavid Greenman struct inpcbhead tcb; 8115bd2b43SDavid Greenman struct inpcbinfo tcbinfo; 82df8bae1dSRodney W. Grimes 830312fbe9SPoul-Henning Kamp static void tcp_dooptions __P((struct tcpcb *, 840312fbe9SPoul-Henning Kamp u_char *, int, struct tcpiphdr *, struct tcpopt *)); 850312fbe9SPoul-Henning Kamp static void tcp_pulloutofband __P((struct socket *, 860312fbe9SPoul-Henning Kamp struct tcpiphdr *, struct mbuf *)); 870312fbe9SPoul-Henning Kamp static int tcp_reass __P((struct tcpcb *, struct tcpiphdr *, struct mbuf *)); 880312fbe9SPoul-Henning Kamp static void tcp_xmit_timer __P((struct tcpcb *, int)); 890312fbe9SPoul-Henning Kamp 90df8bae1dSRodney W. Grimes #endif /* TUBA_INCLUDE */ 91df8bae1dSRodney W. Grimes 92df8bae1dSRodney W. Grimes /* 93df8bae1dSRodney W. Grimes * Insert segment ti into reassembly queue of tcp with 94df8bae1dSRodney W. Grimes * control block tp. Return TH_FIN if reassembly now includes 95df8bae1dSRodney W. Grimes * a segment with FIN. The macro form does the common case inline 96df8bae1dSRodney W. Grimes * (segment is the next to be received on an established connection, 97df8bae1dSRodney W. Grimes * and the queue is empty), avoiding linkage into and removal 98df8bae1dSRodney W. Grimes * from the queue and repetition of various conversions. 99df8bae1dSRodney W. Grimes * Set DELACK for segments received in order, but ack immediately 100df8bae1dSRodney W. Grimes * when segments are out of order (so fast retransmit can work). 101df8bae1dSRodney W. Grimes */ 1026b067b07SDavid Greenman #ifdef TCP_ACK_HACK 103df8bae1dSRodney W. Grimes #define TCP_REASS(tp, ti, m, so, flags) { \ 104df8bae1dSRodney W. Grimes if ((ti)->ti_seq == (tp)->rcv_nxt && \ 105df8bae1dSRodney W. Grimes (tp)->seg_next == (struct tcpiphdr *)(tp) && \ 106df8bae1dSRodney W. Grimes (tp)->t_state == TCPS_ESTABLISHED) { \ 107d79940daSDavid Greenman if (ti->ti_flags & TH_PUSH) \ 108d79940daSDavid Greenman tp->t_flags |= TF_ACKNOW; \ 109d79940daSDavid Greenman else \ 110df8bae1dSRodney W. Grimes tp->t_flags |= TF_DELACK; \ 111df8bae1dSRodney W. Grimes (tp)->rcv_nxt += (ti)->ti_len; \ 112df8bae1dSRodney W. Grimes flags = (ti)->ti_flags & TH_FIN; \ 113df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpack++;\ 114df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 115df8bae1dSRodney W. Grimes sbappend(&(so)->so_rcv, (m)); \ 116df8bae1dSRodney W. Grimes sorwakeup(so); \ 117df8bae1dSRodney W. Grimes } else { \ 118df8bae1dSRodney W. Grimes (flags) = tcp_reass((tp), (ti), (m)); \ 119df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; \ 120df8bae1dSRodney W. Grimes } \ 121df8bae1dSRodney W. Grimes } 1226b067b07SDavid Greenman #else 1236b067b07SDavid Greenman #define TCP_REASS(tp, ti, m, so, flags) { \ 1246b067b07SDavid Greenman if ((ti)->ti_seq == (tp)->rcv_nxt && \ 1256b067b07SDavid Greenman (tp)->seg_next == (struct tcpiphdr *)(tp) && \ 1266b067b07SDavid Greenman (tp)->t_state == TCPS_ESTABLISHED) { \ 1276b067b07SDavid Greenman tp->t_flags |= TF_DELACK; \ 1286b067b07SDavid Greenman (tp)->rcv_nxt += (ti)->ti_len; \ 1296b067b07SDavid Greenman flags = (ti)->ti_flags & TH_FIN; \ 1306b067b07SDavid Greenman tcpstat.tcps_rcvpack++;\ 1316b067b07SDavid Greenman tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 1326b067b07SDavid Greenman sbappend(&(so)->so_rcv, (m)); \ 1336b067b07SDavid Greenman sorwakeup(so); \ 1346b067b07SDavid Greenman } else { \ 1356b067b07SDavid Greenman (flags) = tcp_reass((tp), (ti), (m)); \ 1366b067b07SDavid Greenman tp->t_flags |= TF_ACKNOW; \ 1376b067b07SDavid Greenman } \ 1386b067b07SDavid Greenman } 1396b067b07SDavid Greenman #endif 140df8bae1dSRodney W. Grimes #ifndef TUBA_INCLUDE 141df8bae1dSRodney W. Grimes 1420312fbe9SPoul-Henning Kamp static int 143df8bae1dSRodney W. Grimes tcp_reass(tp, ti, m) 144df8bae1dSRodney W. Grimes register struct tcpcb *tp; 145df8bae1dSRodney W. Grimes register struct tcpiphdr *ti; 146df8bae1dSRodney W. Grimes struct mbuf *m; 147df8bae1dSRodney W. Grimes { 148df8bae1dSRodney W. Grimes register struct tcpiphdr *q; 149df8bae1dSRodney W. Grimes struct socket *so = tp->t_inpcb->inp_socket; 150df8bae1dSRodney W. Grimes int flags; 151df8bae1dSRodney W. Grimes 152df8bae1dSRodney W. Grimes /* 153df8bae1dSRodney W. Grimes * Call with ti==0 after become established to 154df8bae1dSRodney W. Grimes * force pre-ESTABLISHED data up to user socket. 155df8bae1dSRodney W. Grimes */ 156df8bae1dSRodney W. Grimes if (ti == 0) 157df8bae1dSRodney W. Grimes goto present; 158df8bae1dSRodney W. Grimes 159df8bae1dSRodney W. Grimes /* 160df8bae1dSRodney W. Grimes * Find a segment which begins after this one does. 161df8bae1dSRodney W. Grimes */ 162df8bae1dSRodney W. Grimes for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 163df8bae1dSRodney W. Grimes q = (struct tcpiphdr *)q->ti_next) 164df8bae1dSRodney W. Grimes if (SEQ_GT(q->ti_seq, ti->ti_seq)) 165df8bae1dSRodney W. Grimes break; 166df8bae1dSRodney W. Grimes 167df8bae1dSRodney W. Grimes /* 168df8bae1dSRodney W. Grimes * If there is a preceding segment, it may provide some of 169df8bae1dSRodney W. Grimes * our data already. If so, drop the data from the incoming 170df8bae1dSRodney W. Grimes * segment. If it provides all of our data, drop us. 171df8bae1dSRodney W. Grimes */ 172df8bae1dSRodney W. Grimes if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 173df8bae1dSRodney W. Grimes register int i; 174df8bae1dSRodney W. Grimes q = (struct tcpiphdr *)q->ti_prev; 175df8bae1dSRodney W. Grimes /* conversion to int (in i) handles seq wraparound */ 176df8bae1dSRodney W. Grimes i = q->ti_seq + q->ti_len - ti->ti_seq; 177df8bae1dSRodney W. Grimes if (i > 0) { 178df8bae1dSRodney W. Grimes if (i >= ti->ti_len) { 179df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 180df8bae1dSRodney W. Grimes tcpstat.tcps_rcvdupbyte += ti->ti_len; 181df8bae1dSRodney W. Grimes m_freem(m); 182a0292f23SGarrett Wollman /* 183a0292f23SGarrett Wollman * Try to present any queued data 184a0292f23SGarrett Wollman * at the left window edge to the user. 185a0292f23SGarrett Wollman * This is needed after the 3-WHS 186a0292f23SGarrett Wollman * completes. 187a0292f23SGarrett Wollman */ 188a0292f23SGarrett Wollman goto present; /* ??? */ 189df8bae1dSRodney W. Grimes } 190df8bae1dSRodney W. Grimes m_adj(m, i); 191df8bae1dSRodney W. Grimes ti->ti_len -= i; 192df8bae1dSRodney W. Grimes ti->ti_seq += i; 193df8bae1dSRodney W. Grimes } 194df8bae1dSRodney W. Grimes q = (struct tcpiphdr *)(q->ti_next); 195df8bae1dSRodney W. Grimes } 196df8bae1dSRodney W. Grimes tcpstat.tcps_rcvoopack++; 197df8bae1dSRodney W. Grimes tcpstat.tcps_rcvoobyte += ti->ti_len; 198df8bae1dSRodney W. Grimes REASS_MBUF(ti) = m; /* XXX */ 199df8bae1dSRodney W. Grimes 200df8bae1dSRodney W. Grimes /* 201df8bae1dSRodney W. Grimes * While we overlap succeeding segments trim them or, 202df8bae1dSRodney W. Grimes * if they are completely covered, dequeue them. 203df8bae1dSRodney W. Grimes */ 204df8bae1dSRodney W. Grimes while (q != (struct tcpiphdr *)tp) { 205df8bae1dSRodney W. Grimes register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 206df8bae1dSRodney W. Grimes if (i <= 0) 207df8bae1dSRodney W. Grimes break; 208df8bae1dSRodney W. Grimes if (i < q->ti_len) { 209df8bae1dSRodney W. Grimes q->ti_seq += i; 210df8bae1dSRodney W. Grimes q->ti_len -= i; 211df8bae1dSRodney W. Grimes m_adj(REASS_MBUF(q), i); 212df8bae1dSRodney W. Grimes break; 213df8bae1dSRodney W. Grimes } 214df8bae1dSRodney W. Grimes q = (struct tcpiphdr *)q->ti_next; 215df8bae1dSRodney W. Grimes m = REASS_MBUF((struct tcpiphdr *)q->ti_prev); 216df8bae1dSRodney W. Grimes remque(q->ti_prev); 217df8bae1dSRodney W. Grimes m_freem(m); 218df8bae1dSRodney W. Grimes } 219df8bae1dSRodney W. Grimes 220df8bae1dSRodney W. Grimes /* 221df8bae1dSRodney W. Grimes * Stick new segment in its place. 222df8bae1dSRodney W. Grimes */ 223df8bae1dSRodney W. Grimes insque(ti, q->ti_prev); 224df8bae1dSRodney W. Grimes 225df8bae1dSRodney W. Grimes present: 226df8bae1dSRodney W. Grimes /* 227df8bae1dSRodney W. Grimes * Present data to user, advancing rcv_nxt through 228df8bae1dSRodney W. Grimes * completed sequence space. 229df8bae1dSRodney W. Grimes */ 230a0292f23SGarrett Wollman if (!TCPS_HAVEESTABLISHED(tp->t_state)) 231df8bae1dSRodney W. Grimes return (0); 232df8bae1dSRodney W. Grimes ti = tp->seg_next; 233df8bae1dSRodney W. Grimes if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 234df8bae1dSRodney W. Grimes return (0); 235df8bae1dSRodney W. Grimes do { 236df8bae1dSRodney W. Grimes tp->rcv_nxt += ti->ti_len; 237df8bae1dSRodney W. Grimes flags = ti->ti_flags & TH_FIN; 238df8bae1dSRodney W. Grimes remque(ti); 239df8bae1dSRodney W. Grimes m = REASS_MBUF(ti); 240df8bae1dSRodney W. Grimes ti = (struct tcpiphdr *)ti->ti_next; 241df8bae1dSRodney W. Grimes if (so->so_state & SS_CANTRCVMORE) 242df8bae1dSRodney W. Grimes m_freem(m); 243df8bae1dSRodney W. Grimes else 244df8bae1dSRodney W. Grimes sbappend(&so->so_rcv, m); 245df8bae1dSRodney W. Grimes } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 246df8bae1dSRodney W. Grimes sorwakeup(so); 247df8bae1dSRodney W. Grimes return (flags); 248df8bae1dSRodney W. Grimes } 249df8bae1dSRodney W. Grimes 250df8bae1dSRodney W. Grimes /* 251df8bae1dSRodney W. Grimes * TCP input routine, follows pages 65-76 of the 252df8bae1dSRodney W. Grimes * protocol specification dated September, 1981 very closely. 253df8bae1dSRodney W. Grimes */ 254df8bae1dSRodney W. Grimes void 255df8bae1dSRodney W. Grimes tcp_input(m, iphlen) 256df8bae1dSRodney W. Grimes register struct mbuf *m; 257df8bae1dSRodney W. Grimes int iphlen; 258df8bae1dSRodney W. Grimes { 259df8bae1dSRodney W. Grimes register struct tcpiphdr *ti; 260df8bae1dSRodney W. Grimes register struct inpcb *inp; 261e79adb8eSGarrett Wollman u_char *optp = NULL; 26226f9a767SRodney W. Grimes int optlen = 0; 263df8bae1dSRodney W. Grimes int len, tlen, off; 264df8bae1dSRodney W. Grimes register struct tcpcb *tp = 0; 265df8bae1dSRodney W. Grimes register int tiflags; 26626f9a767SRodney W. Grimes struct socket *so = 0; 267df8bae1dSRodney W. Grimes int todrop, acked, ourfinisacked, needoutput = 0; 268df8bae1dSRodney W. Grimes struct in_addr laddr; 269df8bae1dSRodney W. Grimes int dropsocket = 0; 270df8bae1dSRodney W. Grimes int iss = 0; 271a0292f23SGarrett Wollman u_long tiwin; 272a0292f23SGarrett Wollman struct tcpopt to; /* options in this segment */ 273a0292f23SGarrett Wollman struct rmxp_tao *taop; /* pointer to our TAO cache entry */ 274a0292f23SGarrett Wollman struct rmxp_tao tao_noncached; /* in case there's no cached entry */ 275610ee2f9SDavid Greenman #ifdef TCPDEBUG 276610ee2f9SDavid Greenman short ostate = 0; 277610ee2f9SDavid Greenman #endif 278df8bae1dSRodney W. Grimes 279a0292f23SGarrett Wollman bzero((char *)&to, sizeof(to)); 280a0292f23SGarrett Wollman 281df8bae1dSRodney W. Grimes tcpstat.tcps_rcvtotal++; 282df8bae1dSRodney W. Grimes /* 283df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 284df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 285df8bae1dSRodney W. Grimes */ 286df8bae1dSRodney W. Grimes ti = mtod(m, struct tcpiphdr *); 287df8bae1dSRodney W. Grimes if (iphlen > sizeof (struct ip)) 288df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 289df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 290df8bae1dSRodney W. Grimes if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 291df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 292df8bae1dSRodney W. Grimes return; 293df8bae1dSRodney W. Grimes } 294df8bae1dSRodney W. Grimes ti = mtod(m, struct tcpiphdr *); 295df8bae1dSRodney W. Grimes } 296df8bae1dSRodney W. Grimes 297df8bae1dSRodney W. Grimes /* 298df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 299df8bae1dSRodney W. Grimes */ 300df8bae1dSRodney W. Grimes tlen = ((struct ip *)ti)->ip_len; 301df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 302df8bae1dSRodney W. Grimes ti->ti_next = ti->ti_prev = 0; 303df8bae1dSRodney W. Grimes ti->ti_x1 = 0; 304df8bae1dSRodney W. Grimes ti->ti_len = (u_short)tlen; 305df8bae1dSRodney W. Grimes HTONS(ti->ti_len); 306623ae52eSPoul-Henning Kamp ti->ti_sum = in_cksum(m, len); 307623ae52eSPoul-Henning Kamp if (ti->ti_sum) { 308df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadsum++; 309df8bae1dSRodney W. Grimes goto drop; 310df8bae1dSRodney W. Grimes } 311df8bae1dSRodney W. Grimes #endif /* TUBA_INCLUDE */ 312df8bae1dSRodney W. Grimes 313df8bae1dSRodney W. Grimes /* 314df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 315df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 316df8bae1dSRodney W. Grimes */ 317df8bae1dSRodney W. Grimes off = ti->ti_off << 2; 318df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 319df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbadoff++; 320df8bae1dSRodney W. Grimes goto drop; 321df8bae1dSRodney W. Grimes } 322df8bae1dSRodney W. Grimes tlen -= off; 323df8bae1dSRodney W. Grimes ti->ti_len = tlen; 324df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 325df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 326df8bae1dSRodney W. Grimes if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 327df8bae1dSRodney W. Grimes tcpstat.tcps_rcvshort++; 328df8bae1dSRodney W. Grimes return; 329df8bae1dSRodney W. Grimes } 330df8bae1dSRodney W. Grimes ti = mtod(m, struct tcpiphdr *); 331df8bae1dSRodney W. Grimes } 332df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 333e79adb8eSGarrett Wollman optp = mtod(m, u_char *) + sizeof (struct tcpiphdr); 334df8bae1dSRodney W. Grimes } 335df8bae1dSRodney W. Grimes tiflags = ti->ti_flags; 336df8bae1dSRodney W. Grimes 337df8bae1dSRodney W. Grimes /* 338df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 339df8bae1dSRodney W. Grimes */ 340df8bae1dSRodney W. Grimes NTOHL(ti->ti_seq); 341df8bae1dSRodney W. Grimes NTOHL(ti->ti_ack); 342df8bae1dSRodney W. Grimes NTOHS(ti->ti_win); 343df8bae1dSRodney W. Grimes NTOHS(ti->ti_urp); 344df8bae1dSRodney W. Grimes 345df8bae1dSRodney W. Grimes /* 346755c1f07SAndras Olah * Drop TCP, IP headers and TCP options. 347755c1f07SAndras Olah */ 348755c1f07SAndras Olah m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 349755c1f07SAndras Olah m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); 350755c1f07SAndras Olah 351755c1f07SAndras Olah /* 352df8bae1dSRodney W. Grimes * Locate pcb for segment. 353df8bae1dSRodney W. Grimes */ 354df8bae1dSRodney W. Grimes findpcb: 3550d7b7d3eSDavid Greenman /* 3560d7b7d3eSDavid Greenman * First look for an exact match. 3570d7b7d3eSDavid Greenman */ 3580d7b7d3eSDavid Greenman inp = in_pcblookuphash(&tcbinfo, ti->ti_src, ti->ti_sport, 3590d7b7d3eSDavid Greenman ti->ti_dst, ti->ti_dport); 3600d7b7d3eSDavid Greenman /* 3610d7b7d3eSDavid Greenman * ...and if that fails, do a wildcard search. 3620d7b7d3eSDavid Greenman */ 3630d7b7d3eSDavid Greenman if (inp == NULL) { 3640d7b7d3eSDavid Greenman inp = in_pcblookup(&tcb, ti->ti_src, ti->ti_sport, 3650d7b7d3eSDavid Greenman ti->ti_dst, ti->ti_dport, INPLOOKUP_WILDCARD); 3660d7b7d3eSDavid Greenman } 367df8bae1dSRodney W. Grimes 368df8bae1dSRodney W. Grimes /* 369df8bae1dSRodney W. Grimes * If the state is CLOSED (i.e., TCB does not exist) then 370df8bae1dSRodney W. Grimes * all data in the incoming segment is discarded. 371df8bae1dSRodney W. Grimes * If the TCB exists but is in CLOSED state, it is embryonic, 372df8bae1dSRodney W. Grimes * but should either do a listen or a connect soon. 373df8bae1dSRodney W. Grimes */ 3740d7b7d3eSDavid Greenman if (inp == NULL) 375df8bae1dSRodney W. Grimes goto dropwithreset; 376df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 377df8bae1dSRodney W. Grimes if (tp == 0) 378df8bae1dSRodney W. Grimes goto dropwithreset; 379df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_CLOSED) 380df8bae1dSRodney W. Grimes goto drop; 381df8bae1dSRodney W. Grimes 382df8bae1dSRodney W. Grimes /* Unscale the window into a 32-bit value. */ 383df8bae1dSRodney W. Grimes if ((tiflags & TH_SYN) == 0) 384df8bae1dSRodney W. Grimes tiwin = ti->ti_win << tp->snd_scale; 385df8bae1dSRodney W. Grimes else 386df8bae1dSRodney W. Grimes tiwin = ti->ti_win; 387df8bae1dSRodney W. Grimes 388df8bae1dSRodney W. Grimes so = inp->inp_socket; 389df8bae1dSRodney W. Grimes if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) { 390610ee2f9SDavid Greenman #ifdef TCPDEBUG 391df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 392df8bae1dSRodney W. Grimes ostate = tp->t_state; 393df8bae1dSRodney W. Grimes tcp_saveti = *ti; 394df8bae1dSRodney W. Grimes } 395610ee2f9SDavid Greenman #endif 396df8bae1dSRodney W. Grimes if (so->so_options & SO_ACCEPTCONN) { 397a0292f23SGarrett Wollman register struct tcpcb *tp0 = tp; 398df8bae1dSRodney W. Grimes so = sonewconn(so, 0); 3991347f5b8SGuido van Rooij if (so == 0) { 4001347f5b8SGuido van Rooij tcpstat.tcps_listendrop++; 401df8bae1dSRodney W. Grimes goto drop; 4021347f5b8SGuido van Rooij } 403df8bae1dSRodney W. Grimes /* 404df8bae1dSRodney W. Grimes * This is ugly, but .... 405df8bae1dSRodney W. Grimes * 406df8bae1dSRodney W. Grimes * Mark socket as temporary until we're 407df8bae1dSRodney W. Grimes * committed to keeping it. The code at 408df8bae1dSRodney W. Grimes * ``drop'' and ``dropwithreset'' check the 409df8bae1dSRodney W. Grimes * flag dropsocket to see if the temporary 410df8bae1dSRodney W. Grimes * socket created here should be discarded. 411df8bae1dSRodney W. Grimes * We mark the socket as discardable until 412df8bae1dSRodney W. Grimes * we're committed to it below in TCPS_LISTEN. 413df8bae1dSRodney W. Grimes */ 414df8bae1dSRodney W. Grimes dropsocket++; 415df8bae1dSRodney W. Grimes inp = (struct inpcb *)so->so_pcb; 416df8bae1dSRodney W. Grimes inp->inp_laddr = ti->ti_dst; 417df8bae1dSRodney W. Grimes inp->inp_lport = ti->ti_dport; 41815bd2b43SDavid Greenman in_pcbrehash(inp); 419df8bae1dSRodney W. Grimes #if BSD>=43 420df8bae1dSRodney W. Grimes inp->inp_options = ip_srcroute(); 421df8bae1dSRodney W. Grimes #endif 422df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 423df8bae1dSRodney W. Grimes tp->t_state = TCPS_LISTEN; 424a0292f23SGarrett Wollman tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT); 425df8bae1dSRodney W. Grimes 426a0292f23SGarrett Wollman /* Compute proper scaling value from buffer space */ 427df8bae1dSRodney W. Grimes while (tp->request_r_scale < TCP_MAX_WINSHIFT && 428df8bae1dSRodney W. Grimes TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat) 429df8bae1dSRodney W. Grimes tp->request_r_scale++; 430df8bae1dSRodney W. Grimes } 431df8bae1dSRodney W. Grimes } 432df8bae1dSRodney W. Grimes 433df8bae1dSRodney W. Grimes /* 434df8bae1dSRodney W. Grimes * Segment received on connection. 435df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 436df8bae1dSRodney W. Grimes */ 437df8bae1dSRodney W. Grimes tp->t_idle = 0; 438df8bae1dSRodney W. Grimes tp->t_timer[TCPT_KEEP] = tcp_keepidle; 439df8bae1dSRodney W. Grimes 440df8bae1dSRodney W. Grimes /* 441df8bae1dSRodney W. Grimes * Process options if not in LISTEN state, 442df8bae1dSRodney W. Grimes * else do it below (after getting remote address). 443df8bae1dSRodney W. Grimes */ 444f9d5a964SDavid Greenman if (tp->t_state != TCPS_LISTEN) 445f9d5a964SDavid Greenman tcp_dooptions(tp, optp, optlen, ti, &to); 446df8bae1dSRodney W. Grimes 447df8bae1dSRodney W. Grimes /* 448df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 449df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 450df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 451df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 452df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 453df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 454df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 455df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 456df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 457df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 458df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 459df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 460a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 461a0292f23SGarrett Wollman * Since we check for TCPS_ESTABLISHED above, it can only 462a0292f23SGarrett Wollman * be TH_NEEDSYN. 463df8bae1dSRodney W. Grimes */ 464df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 465df8bae1dSRodney W. Grimes (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 466a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 467a0292f23SGarrett Wollman ((to.to_flag & TOF_TS) == 0 || 468a0292f23SGarrett Wollman TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && 469a0292f23SGarrett Wollman /* 470a0292f23SGarrett Wollman * Using the CC option is compulsory if once started: 471a0292f23SGarrett Wollman * the segment is OK if no T/TCP was negotiated or 472a0292f23SGarrett Wollman * if the segment has a CC option equal to CCrecv 473a0292f23SGarrett Wollman */ 474a0292f23SGarrett Wollman ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) || 475a0292f23SGarrett Wollman (to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv) && 476df8bae1dSRodney W. Grimes ti->ti_seq == tp->rcv_nxt && 477df8bae1dSRodney W. Grimes tiwin && tiwin == tp->snd_wnd && 478df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) { 479df8bae1dSRodney W. Grimes 480df8bae1dSRodney W. Grimes /* 481df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 482df8bae1dSRodney W. Grimes * record the timestamp. 483a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 484a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 485df8bae1dSRodney W. Grimes */ 486a0292f23SGarrett Wollman if ((to.to_flag & TOF_TS) != 0 && 487a0292f23SGarrett Wollman SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) { 488a0292f23SGarrett Wollman tp->ts_recent_age = tcp_now; 489a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 490df8bae1dSRodney W. Grimes } 491df8bae1dSRodney W. Grimes 492df8bae1dSRodney W. Grimes if (ti->ti_len == 0) { 493df8bae1dSRodney W. Grimes if (SEQ_GT(ti->ti_ack, tp->snd_una) && 494df8bae1dSRodney W. Grimes SEQ_LEQ(ti->ti_ack, tp->snd_max) && 495233e8c18SGarrett Wollman tp->snd_cwnd >= tp->snd_wnd && 496233e8c18SGarrett Wollman tp->t_dupacks < tcprexmtthresh) { 497df8bae1dSRodney W. Grimes /* 498df8bae1dSRodney W. Grimes * this is a pure ack for outstanding data. 499df8bae1dSRodney W. Grimes */ 500df8bae1dSRodney W. Grimes ++tcpstat.tcps_predack; 501a0292f23SGarrett Wollman if ((to.to_flag & TOF_TS) != 0) 502a0292f23SGarrett Wollman tcp_xmit_timer(tp, 503a0292f23SGarrett Wollman tcp_now - to.to_tsecr + 1); 504df8bae1dSRodney W. Grimes else if (tp->t_rtt && 505df8bae1dSRodney W. Grimes SEQ_GT(ti->ti_ack, tp->t_rtseq)) 506df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, tp->t_rtt); 507df8bae1dSRodney W. Grimes acked = ti->ti_ack - tp->snd_una; 508df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 509df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 510df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 511df8bae1dSRodney W. Grimes tp->snd_una = ti->ti_ack; 512df8bae1dSRodney W. Grimes m_freem(m); 513df8bae1dSRodney W. Grimes 514df8bae1dSRodney W. Grimes /* 515df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 516df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 517df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 518df8bae1dSRodney W. Grimes * If process is waiting for space, 519df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 520df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 521df8bae1dSRodney W. Grimes * decide between more output or persist. 522df8bae1dSRodney W. Grimes */ 523df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 524df8bae1dSRodney W. Grimes tp->t_timer[TCPT_REXMT] = 0; 525df8bae1dSRodney W. Grimes else if (tp->t_timer[TCPT_PERSIST] == 0) 526df8bae1dSRodney W. Grimes tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 527df8bae1dSRodney W. Grimes 528df8bae1dSRodney W. Grimes if (so->so_snd.sb_flags & SB_NOTIFY) 529df8bae1dSRodney W. Grimes sowwakeup(so); 530df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 531df8bae1dSRodney W. Grimes (void) tcp_output(tp); 532df8bae1dSRodney W. Grimes return; 533df8bae1dSRodney W. Grimes } 534df8bae1dSRodney W. Grimes } else if (ti->ti_ack == tp->snd_una && 535df8bae1dSRodney W. Grimes tp->seg_next == (struct tcpiphdr *)tp && 536df8bae1dSRodney W. Grimes ti->ti_len <= sbspace(&so->so_rcv)) { 537df8bae1dSRodney W. Grimes /* 538df8bae1dSRodney W. Grimes * this is a pure, in-sequence data packet 539df8bae1dSRodney W. Grimes * with nothing on the reassembly queue and 540df8bae1dSRodney W. Grimes * we have enough buffer space to take it. 541df8bae1dSRodney W. Grimes */ 542df8bae1dSRodney W. Grimes ++tcpstat.tcps_preddat; 543df8bae1dSRodney W. Grimes tp->rcv_nxt += ti->ti_len; 544df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpack++; 545df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyte += ti->ti_len; 546df8bae1dSRodney W. Grimes /* 547755c1f07SAndras Olah * Add data to socket buffer. 548df8bae1dSRodney W. Grimes */ 549df8bae1dSRodney W. Grimes sbappend(&so->so_rcv, m); 550df8bae1dSRodney W. Grimes sorwakeup(so); 5516b067b07SDavid Greenman #ifdef TCP_ACK_HACK 552e612a582SDavid Greenman /* 553e612a582SDavid Greenman * If this is a short packet, then ACK now - with Nagel 554e612a582SDavid Greenman * congestion avoidance sender won't send more until 555e612a582SDavid Greenman * he gets an ACK. 556e612a582SDavid Greenman */ 557afa70c96SDavid Greenman if (tiflags & TH_PUSH) { 558e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 559e612a582SDavid Greenman tcp_output(tp); 560e612a582SDavid Greenman } else { 561df8bae1dSRodney W. Grimes tp->t_flags |= TF_DELACK; 562e612a582SDavid Greenman } 5636b067b07SDavid Greenman #else 5646b067b07SDavid Greenman tp->t_flags |= TF_DELACK; 5656b067b07SDavid Greenman #endif 566df8bae1dSRodney W. Grimes return; 567df8bae1dSRodney W. Grimes } 568df8bae1dSRodney W. Grimes } 569df8bae1dSRodney W. Grimes 570df8bae1dSRodney W. Grimes /* 571df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 572df8bae1dSRodney W. Grimes * and then do TCP input processing. 573df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 574df8bae1dSRodney W. Grimes * but not less than advertised window. 575df8bae1dSRodney W. Grimes */ 576df8bae1dSRodney W. Grimes { int win; 577df8bae1dSRodney W. Grimes 578df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 579df8bae1dSRodney W. Grimes if (win < 0) 580df8bae1dSRodney W. Grimes win = 0; 581df8bae1dSRodney W. Grimes tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 582df8bae1dSRodney W. Grimes } 583df8bae1dSRodney W. Grimes 584df8bae1dSRodney W. Grimes switch (tp->t_state) { 585df8bae1dSRodney W. Grimes 586df8bae1dSRodney W. Grimes /* 587df8bae1dSRodney W. Grimes * If the state is LISTEN then ignore segment if it contains an RST. 588df8bae1dSRodney W. Grimes * If the segment contains an ACK then it is bad and send a RST. 589df8bae1dSRodney W. Grimes * If it does not contain a SYN then it is not interesting; drop it. 590df8bae1dSRodney W. Grimes * Don't bother responding if the destination was a broadcast. 591df8bae1dSRodney W. Grimes * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 592df8bae1dSRodney W. Grimes * tp->iss, and send a segment: 593df8bae1dSRodney W. Grimes * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 594df8bae1dSRodney W. Grimes * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 595df8bae1dSRodney W. Grimes * Fill in remote peer address fields if not previously specified. 596df8bae1dSRodney W. Grimes * Enter SYN_RECEIVED state, and process any other fields of this 597df8bae1dSRodney W. Grimes * segment in this state. 598df8bae1dSRodney W. Grimes */ 599df8bae1dSRodney W. Grimes case TCPS_LISTEN: { 600df8bae1dSRodney W. Grimes struct mbuf *am; 601df8bae1dSRodney W. Grimes register struct sockaddr_in *sin; 602df8bae1dSRodney W. Grimes 603df8bae1dSRodney W. Grimes if (tiflags & TH_RST) 604df8bae1dSRodney W. Grimes goto drop; 605df8bae1dSRodney W. Grimes if (tiflags & TH_ACK) 606df8bae1dSRodney W. Grimes goto dropwithreset; 607df8bae1dSRodney W. Grimes if ((tiflags & TH_SYN) == 0) 608df8bae1dSRodney W. Grimes goto drop; 609df8bae1dSRodney W. Grimes /* 610df8bae1dSRodney W. Grimes * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN 611df8bae1dSRodney W. Grimes * in_broadcast() should never return true on a received 612df8bae1dSRodney W. Grimes * packet with M_BCAST not set. 613df8bae1dSRodney W. Grimes */ 614df8bae1dSRodney W. Grimes if (m->m_flags & (M_BCAST|M_MCAST) || 615d4d0967eSDavid Greenman IN_MULTICAST(ntohl(ti->ti_dst.s_addr))) 616df8bae1dSRodney W. Grimes goto drop; 617df8bae1dSRodney W. Grimes am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */ 618df8bae1dSRodney W. Grimes if (am == NULL) 619df8bae1dSRodney W. Grimes goto drop; 620df8bae1dSRodney W. Grimes am->m_len = sizeof (struct sockaddr_in); 621df8bae1dSRodney W. Grimes sin = mtod(am, struct sockaddr_in *); 622df8bae1dSRodney W. Grimes sin->sin_family = AF_INET; 623df8bae1dSRodney W. Grimes sin->sin_len = sizeof(*sin); 624df8bae1dSRodney W. Grimes sin->sin_addr = ti->ti_src; 625df8bae1dSRodney W. Grimes sin->sin_port = ti->ti_sport; 626df8bae1dSRodney W. Grimes bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero)); 627df8bae1dSRodney W. Grimes laddr = inp->inp_laddr; 628df8bae1dSRodney W. Grimes if (inp->inp_laddr.s_addr == INADDR_ANY) 629df8bae1dSRodney W. Grimes inp->inp_laddr = ti->ti_dst; 630df8bae1dSRodney W. Grimes if (in_pcbconnect(inp, am)) { 631df8bae1dSRodney W. Grimes inp->inp_laddr = laddr; 632df8bae1dSRodney W. Grimes (void) m_free(am); 633df8bae1dSRodney W. Grimes goto drop; 634df8bae1dSRodney W. Grimes } 635df8bae1dSRodney W. Grimes (void) m_free(am); 636df8bae1dSRodney W. Grimes tp->t_template = tcp_template(tp); 637df8bae1dSRodney W. Grimes if (tp->t_template == 0) { 638df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ENOBUFS); 639df8bae1dSRodney W. Grimes dropsocket = 0; /* socket is already gone */ 640df8bae1dSRodney W. Grimes goto drop; 641df8bae1dSRodney W. Grimes } 642a0292f23SGarrett Wollman if ((taop = tcp_gettaocache(inp)) == NULL) { 643a0292f23SGarrett Wollman taop = &tao_noncached; 644a0292f23SGarrett Wollman bzero(taop, sizeof(*taop)); 645a0292f23SGarrett Wollman } 646f9d5a964SDavid Greenman tcp_dooptions(tp, optp, optlen, ti, &to); 647df8bae1dSRodney W. Grimes if (iss) 648df8bae1dSRodney W. Grimes tp->iss = iss; 649df8bae1dSRodney W. Grimes else 650df8bae1dSRodney W. Grimes tp->iss = tcp_iss; 651e79adb8eSGarrett Wollman tcp_iss += TCP_ISSINCR/4; 652df8bae1dSRodney W. Grimes tp->irs = ti->ti_seq; 653df8bae1dSRodney W. Grimes tcp_sendseqinit(tp); 654df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 655a0292f23SGarrett Wollman /* 656a0292f23SGarrett Wollman * Initialization of the tcpcb for transaction; 657a0292f23SGarrett Wollman * set SND.WND = SEG.WND, 658a0292f23SGarrett Wollman * initialize CCsend and CCrecv. 659a0292f23SGarrett Wollman */ 660a0292f23SGarrett Wollman tp->snd_wnd = tiwin; /* initial send-window */ 661a0292f23SGarrett Wollman tp->cc_send = CC_INC(tcp_ccgen); 662a0292f23SGarrett Wollman tp->cc_recv = to.to_cc; 663a0292f23SGarrett Wollman /* 664a0292f23SGarrett Wollman * Perform TAO test on incoming CC (SEG.CC) option, if any. 665a0292f23SGarrett Wollman * - compare SEG.CC against cached CC from the same host, 666a0292f23SGarrett Wollman * if any. 667a0292f23SGarrett Wollman * - if SEG.CC > chached value, SYN must be new and is accepted 668a0292f23SGarrett Wollman * immediately: save new CC in the cache, mark the socket 669a0292f23SGarrett Wollman * connected, enter ESTABLISHED state, turn on flag to 670a0292f23SGarrett Wollman * send a SYN in the next segment. 671a0292f23SGarrett Wollman * A virtual advertised window is set in rcv_adv to 672a0292f23SGarrett Wollman * initialize SWS prevention. Then enter normal segment 673a0292f23SGarrett Wollman * processing: drop SYN, process data and FIN. 674a0292f23SGarrett Wollman * - otherwise do a normal 3-way handshake. 675a0292f23SGarrett Wollman */ 676a0292f23SGarrett Wollman if ((to.to_flag & TOF_CC) != 0) { 677a0292f23SGarrett Wollman if (taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) { 678a0292f23SGarrett Wollman taop->tao_cc = to.to_cc; 679a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 680a0292f23SGarrett Wollman 681a0292f23SGarrett Wollman /* 682a0292f23SGarrett Wollman * If there is a FIN, or if there is data and the 683a0292f23SGarrett Wollman * connection is local, then delay SYN,ACK(SYN) in 684a0292f23SGarrett Wollman * the hope of piggy-backing it on a response 685a0292f23SGarrett Wollman * segment. Otherwise must send ACK now in case 686a0292f23SGarrett Wollman * the other side is slow starting. 687a0292f23SGarrett Wollman */ 688a0292f23SGarrett Wollman if ((tiflags & TH_FIN) || (ti->ti_len != 0 && 689a0292f23SGarrett Wollman in_localaddr(inp->inp_faddr))) 690a0292f23SGarrett Wollman tp->t_flags |= (TF_DELACK | TF_NEEDSYN); 691a0292f23SGarrett Wollman else 692a0292f23SGarrett Wollman tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 69307e43e10SAndras Olah 69407e43e10SAndras Olah /* 69507e43e10SAndras Olah * Limit the `virtual advertised window' to TCP_MAXWIN 69607e43e10SAndras Olah * here. Even if we requested window scaling, it will 69707e43e10SAndras Olah * become effective only later when our SYN is acked. 69807e43e10SAndras Olah */ 69907e43e10SAndras Olah tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN); 700a0292f23SGarrett Wollman tcpstat.tcps_connects++; 701a0292f23SGarrett Wollman soisconnected(so); 702a0292f23SGarrett Wollman tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; 703a0292f23SGarrett Wollman dropsocket = 0; /* committed to socket */ 704a0292f23SGarrett Wollman tcpstat.tcps_accepts++; 705a0292f23SGarrett Wollman goto trimthenstep6; 706a0292f23SGarrett Wollman } 707a0292f23SGarrett Wollman /* else do standard 3-way handshake */ 708a0292f23SGarrett Wollman } else { 709a0292f23SGarrett Wollman /* 710a0292f23SGarrett Wollman * No CC option, but maybe CC.NEW: 711a0292f23SGarrett Wollman * invalidate cached value. 712a0292f23SGarrett Wollman */ 713a0292f23SGarrett Wollman taop->tao_cc = 0; 714a0292f23SGarrett Wollman } 715a0292f23SGarrett Wollman /* 716a0292f23SGarrett Wollman * TAO test failed or there was no CC option, 717a0292f23SGarrett Wollman * do a standard 3-way handshake. 718a0292f23SGarrett Wollman */ 719df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 720df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 721df8bae1dSRodney W. Grimes tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; 722df8bae1dSRodney W. Grimes dropsocket = 0; /* committed to socket */ 723df8bae1dSRodney W. Grimes tcpstat.tcps_accepts++; 724df8bae1dSRodney W. Grimes goto trimthenstep6; 725df8bae1dSRodney W. Grimes } 726df8bae1dSRodney W. Grimes 727df8bae1dSRodney W. Grimes /* 728df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 729df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 730df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 731df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 732df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 733df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 734df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 735df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 736df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 737df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 738df8bae1dSRodney W. Grimes */ 739df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 740a0292f23SGarrett Wollman if ((taop = tcp_gettaocache(inp)) == NULL) { 741a0292f23SGarrett Wollman taop = &tao_noncached; 742a0292f23SGarrett Wollman bzero(taop, sizeof(*taop)); 743a0292f23SGarrett Wollman } 744a0292f23SGarrett Wollman 745a0292f23SGarrett Wollman if ((tiflags & TH_ACK) && 746a0292f23SGarrett Wollman (SEQ_LEQ(ti->ti_ack, tp->iss) || 747a0292f23SGarrett Wollman SEQ_GT(ti->ti_ack, tp->snd_max))) { 748a0292f23SGarrett Wollman /* 749a0292f23SGarrett Wollman * If we have a cached CCsent for the remote host, 750a0292f23SGarrett Wollman * hence we haven't just crashed and restarted, 751a0292f23SGarrett Wollman * do not send a RST. This may be a retransmission 752a0292f23SGarrett Wollman * from the other side after our earlier ACK was lost. 753a0292f23SGarrett Wollman * Our new SYN, when it arrives, will serve as the 754a0292f23SGarrett Wollman * needed ACK. 755a0292f23SGarrett Wollman */ 756a0292f23SGarrett Wollman if (taop->tao_ccsent != 0) 757a0292f23SGarrett Wollman goto drop; 758a0292f23SGarrett Wollman else 759a0292f23SGarrett Wollman goto dropwithreset; 760a0292f23SGarrett Wollman } 761df8bae1dSRodney W. Grimes if (tiflags & TH_RST) { 762df8bae1dSRodney W. Grimes if (tiflags & TH_ACK) 763df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 764df8bae1dSRodney W. Grimes goto drop; 765df8bae1dSRodney W. Grimes } 766df8bae1dSRodney W. Grimes if ((tiflags & TH_SYN) == 0) 767df8bae1dSRodney W. Grimes goto drop; 768a0292f23SGarrett Wollman tp->snd_wnd = ti->ti_win; /* initial send window */ 769a0292f23SGarrett Wollman tp->cc_recv = to.to_cc; /* foreign CC */ 770a0292f23SGarrett Wollman 771df8bae1dSRodney W. Grimes tp->irs = ti->ti_seq; 772df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 773845799c1SAndras Olah if (tiflags & TH_ACK) { 774a0292f23SGarrett Wollman /* 775a0292f23SGarrett Wollman * Our SYN was acked. If segment contains CC.ECHO 776a0292f23SGarrett Wollman * option, check it to make sure this segment really 777a0292f23SGarrett Wollman * matches our SYN. If not, just drop it as old 778a0292f23SGarrett Wollman * duplicate, but send an RST if we're still playing 779a0292f23SGarrett Wollman * by the old rules. 780a0292f23SGarrett Wollman */ 781a0292f23SGarrett Wollman if ((to.to_flag & TOF_CCECHO) && 782a0292f23SGarrett Wollman tp->cc_send != to.to_ccecho) { 783a0292f23SGarrett Wollman if (taop->tao_ccsent != 0) 784a0292f23SGarrett Wollman goto drop; 785a0292f23SGarrett Wollman else 786a0292f23SGarrett Wollman goto dropwithreset; 787a0292f23SGarrett Wollman } 788845799c1SAndras Olah tcpstat.tcps_connects++; 789845799c1SAndras Olah soisconnected(so); 790845799c1SAndras Olah /* Do window scaling on this connection? */ 791845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 792845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 793845799c1SAndras Olah tp->snd_scale = tp->requested_s_scale; 794845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 795845799c1SAndras Olah } 796a0292f23SGarrett Wollman /* Segment is acceptable, update cache if undefined. */ 797a0292f23SGarrett Wollman if (taop->tao_ccsent == 0) 798a0292f23SGarrett Wollman taop->tao_ccsent = to.to_ccecho; 799a0292f23SGarrett Wollman 800a0292f23SGarrett Wollman tp->rcv_adv += tp->rcv_wnd; 801a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 802a0292f23SGarrett Wollman /* 803a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 804a0292f23SGarrett Wollman * ACKNOW will be turned on later. 805a0292f23SGarrett Wollman */ 806a0292f23SGarrett Wollman if (ti->ti_len != 0) 807a0292f23SGarrett Wollman tp->t_flags |= TF_DELACK; 808a0292f23SGarrett Wollman else 809a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 810a0292f23SGarrett Wollman /* 811a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 812a0292f23SGarrett Wollman * Transitions: 813a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 814a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 815a0292f23SGarrett Wollman */ 816a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 817a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 818a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 819a0292f23SGarrett Wollman tiflags &= ~TH_SYN; 820a0292f23SGarrett Wollman } else 821a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 822a0292f23SGarrett Wollman 823a0292f23SGarrett Wollman } else { 824a0292f23SGarrett Wollman /* 825a0292f23SGarrett Wollman * Received initial SYN in SYN-SENT[*] state => simul- 826a0292f23SGarrett Wollman * taneous open. If segment contains CC option and there is 827a0292f23SGarrett Wollman * a cached CC, apply TAO test; if it succeeds, connection is 828a0292f23SGarrett Wollman * half-synchronized. Otherwise, do 3-way handshake: 829a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 830a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 831a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 832a0292f23SGarrett Wollman */ 833a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 834a0292f23SGarrett Wollman tp->t_timer[TCPT_REXMT] = 0; 835a0292f23SGarrett Wollman if (to.to_flag & TOF_CC) { 836a0292f23SGarrett Wollman if (taop->tao_cc != 0 && 837a0292f23SGarrett Wollman CC_GT(to.to_cc, taop->tao_cc)) { 838a0292f23SGarrett Wollman /* 839a0292f23SGarrett Wollman * update cache and make transition: 840a0292f23SGarrett Wollman * SYN-SENT -> ESTABLISHED* 841a0292f23SGarrett Wollman * SYN-SENT* -> FIN-WAIT-1* 842a0292f23SGarrett Wollman */ 843a0292f23SGarrett Wollman taop->tao_cc = to.to_cc; 844a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 845a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 846a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 847a0292f23SGarrett Wollman } else 848a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 849a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDSYN; 850df8bae1dSRodney W. Grimes } else 851df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 852a0292f23SGarrett Wollman } else { 853a0292f23SGarrett Wollman /* CC.NEW or no option => invalidate cache */ 854a0292f23SGarrett Wollman taop->tao_cc = 0; 855a0292f23SGarrett Wollman tp->t_state = TCPS_SYN_RECEIVED; 856a0292f23SGarrett Wollman } 857a0292f23SGarrett Wollman } 858df8bae1dSRodney W. Grimes 859df8bae1dSRodney W. Grimes trimthenstep6: 860df8bae1dSRodney W. Grimes /* 861df8bae1dSRodney W. Grimes * Advance ti->ti_seq to correspond to first data byte. 862df8bae1dSRodney W. Grimes * If data, trim to stay within window, 863df8bae1dSRodney W. Grimes * dropping FIN if necessary. 864df8bae1dSRodney W. Grimes */ 865df8bae1dSRodney W. Grimes ti->ti_seq++; 866df8bae1dSRodney W. Grimes if (ti->ti_len > tp->rcv_wnd) { 867df8bae1dSRodney W. Grimes todrop = ti->ti_len - tp->rcv_wnd; 868df8bae1dSRodney W. Grimes m_adj(m, -todrop); 869df8bae1dSRodney W. Grimes ti->ti_len = tp->rcv_wnd; 870df8bae1dSRodney W. Grimes tiflags &= ~TH_FIN; 871df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 872df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 873df8bae1dSRodney W. Grimes } 874df8bae1dSRodney W. Grimes tp->snd_wl1 = ti->ti_seq - 1; 875df8bae1dSRodney W. Grimes tp->rcv_up = ti->ti_seq; 876a0292f23SGarrett Wollman /* 877a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 878a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 879a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 880a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 881a0292f23SGarrett Wollman * Otherwise, goto step 6. 882a0292f23SGarrett Wollman */ 883a0292f23SGarrett Wollman if (tiflags & TH_ACK) 884a0292f23SGarrett Wollman goto process_ACK; 885df8bae1dSRodney W. Grimes goto step6; 886a0292f23SGarrett Wollman /* 887a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 888a0292f23SGarrett Wollman * if segment contains a SYN and CC [not CC.NEW] option: 889a0292f23SGarrett Wollman * if state == TIME_WAIT and connection duration > MSL, 890a0292f23SGarrett Wollman * drop packet and send RST; 891a0292f23SGarrett Wollman * 892a0292f23SGarrett Wollman * if SEG.CC > CCrecv then is new SYN, and can implicitly 893a0292f23SGarrett Wollman * ack the FIN (and data) in retransmission queue. 894a0292f23SGarrett Wollman * Complete close and delete TCPCB. Then reprocess 895a0292f23SGarrett Wollman * segment, hoping to find new TCPCB in LISTEN state; 896a0292f23SGarrett Wollman * 897a0292f23SGarrett Wollman * else must be old SYN; drop it. 898a0292f23SGarrett Wollman * else do normal processing. 899a0292f23SGarrett Wollman */ 900a0292f23SGarrett Wollman case TCPS_LAST_ACK: 901a0292f23SGarrett Wollman case TCPS_CLOSING: 902a0292f23SGarrett Wollman case TCPS_TIME_WAIT: 903a0292f23SGarrett Wollman if ((tiflags & TH_SYN) && 904a0292f23SGarrett Wollman (to.to_flag & TOF_CC) && tp->cc_recv != 0) { 905a0292f23SGarrett Wollman if (tp->t_state == TCPS_TIME_WAIT && 906a0292f23SGarrett Wollman tp->t_duration > TCPTV_MSL) 907a0292f23SGarrett Wollman goto dropwithreset; 908a0292f23SGarrett Wollman if (CC_GT(to.to_cc, tp->cc_recv)) { 909a0292f23SGarrett Wollman tp = tcp_close(tp); 910a0292f23SGarrett Wollman goto findpcb; 911a0292f23SGarrett Wollman } 912a0292f23SGarrett Wollman else 913a0292f23SGarrett Wollman goto drop; 914a0292f23SGarrett Wollman } 915a0292f23SGarrett Wollman break; /* continue normal processing */ 916df8bae1dSRodney W. Grimes } 917df8bae1dSRodney W. Grimes 918df8bae1dSRodney W. Grimes /* 919df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 920df8bae1dSRodney W. Grimes * First check timestamp, if present. 921a0292f23SGarrett Wollman * Then check the connection count, if present. 922df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 923df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 924df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 925df8bae1dSRodney W. Grimes * 926df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 927df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 928df8bae1dSRodney W. Grimes */ 929a0292f23SGarrett Wollman if ((to.to_flag & TOF_TS) != 0 && (tiflags & TH_RST) == 0 && 930a0292f23SGarrett Wollman tp->ts_recent && TSTMP_LT(to.to_tsval, tp->ts_recent)) { 931df8bae1dSRodney W. Grimes 932df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 933df8bae1dSRodney W. Grimes if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) { 934df8bae1dSRodney W. Grimes /* 935df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 936df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 937df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 938df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 939df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 940df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 941df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 942df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 943df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 944df8bae1dSRodney W. Grimes */ 945df8bae1dSRodney W. Grimes tp->ts_recent = 0; 946df8bae1dSRodney W. Grimes } else { 947df8bae1dSRodney W. Grimes tcpstat.tcps_rcvduppack++; 948df8bae1dSRodney W. Grimes tcpstat.tcps_rcvdupbyte += ti->ti_len; 949df8bae1dSRodney W. Grimes tcpstat.tcps_pawsdrop++; 950df8bae1dSRodney W. Grimes goto dropafterack; 951df8bae1dSRodney W. Grimes } 952df8bae1dSRodney W. Grimes } 953df8bae1dSRodney W. Grimes 954a0292f23SGarrett Wollman /* 955a0292f23SGarrett Wollman * T/TCP mechanism 956a0292f23SGarrett Wollman * If T/TCP was negotiated and the segment doesn't have CC, 957a0292f23SGarrett Wollman * or if it's CC is wrong then drop the segment. 958a0292f23SGarrett Wollman * RST segments do not have to comply with this. 959a0292f23SGarrett Wollman */ 960a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) && 961a0292f23SGarrett Wollman ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc) && 962a0292f23SGarrett Wollman (tiflags & TH_RST) == 0) 963a0292f23SGarrett Wollman goto dropafterack; 964a0292f23SGarrett Wollman 965df8bae1dSRodney W. Grimes todrop = tp->rcv_nxt - ti->ti_seq; 966df8bae1dSRodney W. Grimes if (todrop > 0) { 967df8bae1dSRodney W. Grimes if (tiflags & TH_SYN) { 968df8bae1dSRodney W. Grimes tiflags &= ~TH_SYN; 969df8bae1dSRodney W. Grimes ti->ti_seq++; 970df8bae1dSRodney W. Grimes if (ti->ti_urp > 1) 971df8bae1dSRodney W. Grimes ti->ti_urp--; 972df8bae1dSRodney W. Grimes else 973df8bae1dSRodney W. Grimes tiflags &= ~TH_URG; 974df8bae1dSRodney W. Grimes todrop--; 975df8bae1dSRodney W. Grimes } 976df8bae1dSRodney W. Grimes /* 977dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 978df8bae1dSRodney W. Grimes */ 979dac20301SGarrett Wollman if (todrop > ti->ti_len 980dac20301SGarrett Wollman || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) { 981dac20301SGarrett Wollman /* 982dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 983dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 984dac20301SGarrett Wollman * of sequence; drop it. 985dac20301SGarrett Wollman */ 986df8bae1dSRodney W. Grimes tiflags &= ~TH_FIN; 987dac20301SGarrett Wollman 988df8bae1dSRodney W. Grimes /* 989dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 990dac20301SGarrett Wollman * But keep on processing for RST or ACK. 991df8bae1dSRodney W. Grimes */ 992dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 993dac20301SGarrett Wollman todrop = ti->ti_len; 994dac20301SGarrett Wollman tcpstat.tcps_rcvduppack++; 995dac20301SGarrett Wollman tcpstat.tcps_rcvdupbyte += todrop; 996df8bae1dSRodney W. Grimes } else { 997df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartduppack++; 998df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpartdupbyte += todrop; 999df8bae1dSRodney W. Grimes } 1000df8bae1dSRodney W. Grimes m_adj(m, todrop); 1001df8bae1dSRodney W. Grimes ti->ti_seq += todrop; 1002df8bae1dSRodney W. Grimes ti->ti_len -= todrop; 1003df8bae1dSRodney W. Grimes if (ti->ti_urp > todrop) 1004df8bae1dSRodney W. Grimes ti->ti_urp -= todrop; 1005df8bae1dSRodney W. Grimes else { 1006df8bae1dSRodney W. Grimes tiflags &= ~TH_URG; 1007df8bae1dSRodney W. Grimes ti->ti_urp = 0; 1008df8bae1dSRodney W. Grimes } 1009df8bae1dSRodney W. Grimes } 1010df8bae1dSRodney W. Grimes 1011df8bae1dSRodney W. Grimes /* 1012df8bae1dSRodney W. Grimes * If new data are received on a connection after the 1013df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 1014df8bae1dSRodney W. Grimes */ 1015df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 1016df8bae1dSRodney W. Grimes tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { 1017df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1018df8bae1dSRodney W. Grimes tcpstat.tcps_rcvafterclose++; 1019df8bae1dSRodney W. Grimes goto dropwithreset; 1020df8bae1dSRodney W. Grimes } 1021df8bae1dSRodney W. Grimes 1022df8bae1dSRodney W. Grimes /* 1023df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 1024df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 1025df8bae1dSRodney W. Grimes */ 1026df8bae1dSRodney W. Grimes todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 1027df8bae1dSRodney W. Grimes if (todrop > 0) { 1028df8bae1dSRodney W. Grimes tcpstat.tcps_rcvpackafterwin++; 1029df8bae1dSRodney W. Grimes if (todrop >= ti->ti_len) { 1030df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 1031df8bae1dSRodney W. Grimes /* 1032df8bae1dSRodney W. Grimes * If a new connection request is received 1033df8bae1dSRodney W. Grimes * while in TIME_WAIT, drop the old connection 1034df8bae1dSRodney W. Grimes * and start over if the sequence numbers 1035df8bae1dSRodney W. Grimes * are above the previous ones. 1036df8bae1dSRodney W. Grimes */ 1037df8bae1dSRodney W. Grimes if (tiflags & TH_SYN && 1038df8bae1dSRodney W. Grimes tp->t_state == TCPS_TIME_WAIT && 1039df8bae1dSRodney W. Grimes SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 1040df8bae1dSRodney W. Grimes iss = tp->rcv_nxt + TCP_ISSINCR; 1041df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1042df8bae1dSRodney W. Grimes goto findpcb; 1043df8bae1dSRodney W. Grimes } 1044df8bae1dSRodney W. Grimes /* 1045df8bae1dSRodney W. Grimes * If window is closed can only take segments at 1046df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 1047df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 1048df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 1049df8bae1dSRodney W. Grimes * and ack. 1050df8bae1dSRodney W. Grimes */ 1051df8bae1dSRodney W. Grimes if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) { 1052df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1053df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinprobe++; 1054df8bae1dSRodney W. Grimes } else 1055df8bae1dSRodney W. Grimes goto dropafterack; 1056df8bae1dSRodney W. Grimes } else 1057df8bae1dSRodney W. Grimes tcpstat.tcps_rcvbyteafterwin += todrop; 1058df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1059df8bae1dSRodney W. Grimes ti->ti_len -= todrop; 1060df8bae1dSRodney W. Grimes tiflags &= ~(TH_PUSH|TH_FIN); 1061df8bae1dSRodney W. Grimes } 1062df8bae1dSRodney W. Grimes 1063df8bae1dSRodney W. Grimes /* 1064df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1065df8bae1dSRodney W. Grimes * record its timestamp. 1066a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1067a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1068df8bae1dSRodney W. Grimes */ 1069a0292f23SGarrett Wollman if ((to.to_flag & TOF_TS) != 0 && 1070a0292f23SGarrett Wollman SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) { 1071a0292f23SGarrett Wollman tp->ts_recent_age = tcp_now; 1072a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1073df8bae1dSRodney W. Grimes } 1074df8bae1dSRodney W. Grimes 1075df8bae1dSRodney W. Grimes /* 1076df8bae1dSRodney W. Grimes * If the RST bit is set examine the state: 1077df8bae1dSRodney W. Grimes * SYN_RECEIVED STATE: 1078df8bae1dSRodney W. Grimes * If passive open, return to LISTEN state. 1079df8bae1dSRodney W. Grimes * If active open, inform user that connection was refused. 1080df8bae1dSRodney W. Grimes * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 1081df8bae1dSRodney W. Grimes * Inform user that connection was reset, and close tcb. 1082df8bae1dSRodney W. Grimes * CLOSING, LAST_ACK, TIME_WAIT STATES 1083df8bae1dSRodney W. Grimes * Close the tcb. 1084df8bae1dSRodney W. Grimes */ 1085df8bae1dSRodney W. Grimes if (tiflags&TH_RST) switch (tp->t_state) { 1086df8bae1dSRodney W. Grimes 1087df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1088df8bae1dSRodney W. Grimes so->so_error = ECONNREFUSED; 1089df8bae1dSRodney W. Grimes goto close; 1090df8bae1dSRodney W. Grimes 1091df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1092df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1093df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 1094df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1095df8bae1dSRodney W. Grimes so->so_error = ECONNRESET; 1096df8bae1dSRodney W. Grimes close: 1097df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 1098df8bae1dSRodney W. Grimes tcpstat.tcps_drops++; 1099df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1100df8bae1dSRodney W. Grimes goto drop; 1101df8bae1dSRodney W. Grimes 1102df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1103df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 1104df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 1105df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1106df8bae1dSRodney W. Grimes goto drop; 1107df8bae1dSRodney W. Grimes } 1108df8bae1dSRodney W. Grimes 1109df8bae1dSRodney W. Grimes /* 1110df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 1111df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 1112df8bae1dSRodney W. Grimes */ 1113df8bae1dSRodney W. Grimes if (tiflags & TH_SYN) { 1114df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 1115df8bae1dSRodney W. Grimes goto dropwithreset; 1116df8bae1dSRodney W. Grimes } 1117df8bae1dSRodney W. Grimes 1118a0292f23SGarrett Wollman /* 1119a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1120a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 1121a0292f23SGarrett Wollman * later processing; else drop segment and return. 1122a0292f23SGarrett Wollman */ 1123a0292f23SGarrett Wollman if ((tiflags & TH_ACK) == 0) { 1124a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 1125a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 1126a0292f23SGarrett Wollman goto step6; 1127a0292f23SGarrett Wollman else 1128a0292f23SGarrett Wollman goto drop; 1129a0292f23SGarrett Wollman } 1130df8bae1dSRodney W. Grimes 1131df8bae1dSRodney W. Grimes /* 1132df8bae1dSRodney W. Grimes * Ack processing. 1133df8bae1dSRodney W. Grimes */ 1134df8bae1dSRodney W. Grimes switch (tp->t_state) { 1135df8bae1dSRodney W. Grimes 1136df8bae1dSRodney W. Grimes /* 1137df8bae1dSRodney W. Grimes * In SYN_RECEIVED state if the ack ACKs our SYN then enter 1138df8bae1dSRodney W. Grimes * ESTABLISHED state and continue processing, otherwise 1139df8bae1dSRodney W. Grimes * send an RST. 1140df8bae1dSRodney W. Grimes */ 1141df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1142df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_una, ti->ti_ack) || 1143df8bae1dSRodney W. Grimes SEQ_GT(ti->ti_ack, tp->snd_max)) 1144df8bae1dSRodney W. Grimes goto dropwithreset; 1145a0292f23SGarrett Wollman 1146df8bae1dSRodney W. Grimes tcpstat.tcps_connects++; 1147df8bae1dSRodney W. Grimes soisconnected(so); 1148df8bae1dSRodney W. Grimes /* Do window scaling? */ 1149df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1150df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1151df8bae1dSRodney W. Grimes tp->snd_scale = tp->requested_s_scale; 1152df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 1153df8bae1dSRodney W. Grimes } 1154a0292f23SGarrett Wollman /* 1155a0292f23SGarrett Wollman * Upon successful completion of 3-way handshake, 1156a0292f23SGarrett Wollman * update cache.CC if it was undefined, pass any queued 1157a0292f23SGarrett Wollman * data to the user, and advance state appropriately. 1158a0292f23SGarrett Wollman */ 1159a0292f23SGarrett Wollman if ((taop = tcp_gettaocache(inp)) != NULL && 1160a0292f23SGarrett Wollman taop->tao_cc == 0) 1161a0292f23SGarrett Wollman taop->tao_cc = tp->cc_recv; 1162a0292f23SGarrett Wollman 1163a0292f23SGarrett Wollman /* 1164a0292f23SGarrett Wollman * Make transitions: 1165a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 1166a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 1167a0292f23SGarrett Wollman */ 1168a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1169a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1170a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1171a0292f23SGarrett Wollman } else 1172a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1173a0292f23SGarrett Wollman /* 1174a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 1175a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 1176a0292f23SGarrett Wollman */ 1177a0292f23SGarrett Wollman if (ti->ti_len == 0 && (tiflags & TH_FIN) == 0) 1178a0292f23SGarrett Wollman (void) tcp_reass(tp, (struct tcpiphdr *)0, 1179a0292f23SGarrett Wollman (struct mbuf *)0); 1180df8bae1dSRodney W. Grimes tp->snd_wl1 = ti->ti_seq - 1; 1181df8bae1dSRodney W. Grimes /* fall into ... */ 1182df8bae1dSRodney W. Grimes 1183df8bae1dSRodney W. Grimes /* 1184df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1185df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 1186df8bae1dSRodney W. Grimes * tp->snd_una < ti->ti_ack <= tp->snd_max 1187df8bae1dSRodney W. Grimes * then advance tp->snd_una to ti->ti_ack and drop 1188df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 1189df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 1190df8bae1dSRodney W. Grimes */ 1191df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1192df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1193df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 1194df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1195df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1196df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 1197df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 1198df8bae1dSRodney W. Grimes 1199df8bae1dSRodney W. Grimes if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 1200df8bae1dSRodney W. Grimes if (ti->ti_len == 0 && tiwin == tp->snd_wnd) { 1201df8bae1dSRodney W. Grimes tcpstat.tcps_rcvdupack++; 1202df8bae1dSRodney W. Grimes /* 1203df8bae1dSRodney W. Grimes * If we have outstanding data (other than 1204df8bae1dSRodney W. Grimes * a window probe), this is a completely 1205df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 1206df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 1207df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 1208df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 1209df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 1210df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 1211df8bae1dSRodney W. Grimes * window so we send only this one 1212df8bae1dSRodney W. Grimes * packet. 1213df8bae1dSRodney W. Grimes * 1214df8bae1dSRodney W. Grimes * We know we're losing at the current 1215df8bae1dSRodney W. Grimes * window size so do congestion avoidance 1216df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 1217df8bae1dSRodney W. Grimes * and pull our congestion window back to 1218df8bae1dSRodney W. Grimes * the new ssthresh). 1219df8bae1dSRodney W. Grimes * 1220df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 1221df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 1222df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 1223df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 1224df8bae1dSRodney W. Grimes * network. 1225df8bae1dSRodney W. Grimes */ 1226df8bae1dSRodney W. Grimes if (tp->t_timer[TCPT_REXMT] == 0 || 1227df8bae1dSRodney W. Grimes ti->ti_ack != tp->snd_una) 1228df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1229df8bae1dSRodney W. Grimes else if (++tp->t_dupacks == tcprexmtthresh) { 1230df8bae1dSRodney W. Grimes tcp_seq onxt = tp->snd_nxt; 1231df8bae1dSRodney W. Grimes u_int win = 1232df8bae1dSRodney W. Grimes min(tp->snd_wnd, tp->snd_cwnd) / 2 / 1233df8bae1dSRodney W. Grimes tp->t_maxseg; 1234df8bae1dSRodney W. Grimes 1235df8bae1dSRodney W. Grimes if (win < 2) 1236df8bae1dSRodney W. Grimes win = 2; 1237df8bae1dSRodney W. Grimes tp->snd_ssthresh = win * tp->t_maxseg; 1238df8bae1dSRodney W. Grimes tp->t_timer[TCPT_REXMT] = 0; 1239df8bae1dSRodney W. Grimes tp->t_rtt = 0; 1240df8bae1dSRodney W. Grimes tp->snd_nxt = ti->ti_ack; 1241df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 1242df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1243df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 1244df8bae1dSRodney W. Grimes tp->t_maxseg * tp->t_dupacks; 1245df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 1246df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 1247df8bae1dSRodney W. Grimes goto drop; 1248df8bae1dSRodney W. Grimes } else if (tp->t_dupacks > tcprexmtthresh) { 1249df8bae1dSRodney W. Grimes tp->snd_cwnd += tp->t_maxseg; 1250df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1251df8bae1dSRodney W. Grimes goto drop; 1252df8bae1dSRodney W. Grimes } 1253df8bae1dSRodney W. Grimes } else 1254df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1255df8bae1dSRodney W. Grimes break; 1256df8bae1dSRodney W. Grimes } 1257df8bae1dSRodney W. Grimes /* 1258df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 1259df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 1260df8bae1dSRodney W. Grimes */ 1261233e8c18SGarrett Wollman if (tp->t_dupacks >= tcprexmtthresh && 1262df8bae1dSRodney W. Grimes tp->snd_cwnd > tp->snd_ssthresh) 1263df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh; 1264df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1265df8bae1dSRodney W. Grimes if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 1266df8bae1dSRodney W. Grimes tcpstat.tcps_rcvacktoomuch++; 1267df8bae1dSRodney W. Grimes goto dropafterack; 1268df8bae1dSRodney W. Grimes } 1269a0292f23SGarrett Wollman /* 1270a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 1271a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 1272a0292f23SGarrett Wollman */ 1273a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 1274a0292f23SGarrett Wollman /* 1275a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 1276a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 127707e43e10SAndras Olah * synchronized). Go to non-starred state, 127807e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 127907e43e10SAndras Olah * we can do window scaling. 1280a0292f23SGarrett Wollman */ 1281a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 1282a0292f23SGarrett Wollman tp->snd_una++; 128307e43e10SAndras Olah /* Do window scaling? */ 128407e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 128507e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 128607e43e10SAndras Olah tp->snd_scale = tp->requested_s_scale; 128707e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 128807e43e10SAndras Olah } 1289a0292f23SGarrett Wollman } 1290a0292f23SGarrett Wollman 1291a0292f23SGarrett Wollman process_ACK: 1292df8bae1dSRodney W. Grimes acked = ti->ti_ack - tp->snd_una; 1293df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackpack++; 1294df8bae1dSRodney W. Grimes tcpstat.tcps_rcvackbyte += acked; 1295df8bae1dSRodney W. Grimes 1296df8bae1dSRodney W. Grimes /* 1297df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 1298df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 1299df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 1300df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 1301df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 1302df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 1303df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 1304df8bae1dSRodney W. Grimes */ 1305a0292f23SGarrett Wollman if (to.to_flag & TOF_TS) 1306a0292f23SGarrett Wollman tcp_xmit_timer(tp, tcp_now - to.to_tsecr + 1); 1307df8bae1dSRodney W. Grimes else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) 1308df8bae1dSRodney W. Grimes tcp_xmit_timer(tp,tp->t_rtt); 1309df8bae1dSRodney W. Grimes 1310df8bae1dSRodney W. Grimes /* 1311df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 1312df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 1313df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 1314df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 1315df8bae1dSRodney W. Grimes */ 1316df8bae1dSRodney W. Grimes if (ti->ti_ack == tp->snd_max) { 1317df8bae1dSRodney W. Grimes tp->t_timer[TCPT_REXMT] = 0; 1318df8bae1dSRodney W. Grimes needoutput = 1; 1319df8bae1dSRodney W. Grimes } else if (tp->t_timer[TCPT_PERSIST] == 0) 1320df8bae1dSRodney W. Grimes tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 1321a0292f23SGarrett Wollman 1322a0292f23SGarrett Wollman /* 1323a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 1324a0292f23SGarrett Wollman * skip rest of ACK processing. 1325a0292f23SGarrett Wollman */ 1326a0292f23SGarrett Wollman if (acked == 0) 1327a0292f23SGarrett Wollman goto step6; 1328a0292f23SGarrett Wollman 1329df8bae1dSRodney W. Grimes /* 1330df8bae1dSRodney W. Grimes * When new data is acked, open the congestion window. 1331df8bae1dSRodney W. Grimes * If the window gives us less than ssthresh packets 1332df8bae1dSRodney W. Grimes * in flight, open exponentially (maxseg per packet). 1333df8bae1dSRodney W. Grimes * Otherwise open linearly: maxseg per window 133410be5648SGarrett Wollman * (maxseg^2 / cwnd per packet). 1335df8bae1dSRodney W. Grimes */ 1336df8bae1dSRodney W. Grimes { 1337df8bae1dSRodney W. Grimes register u_int cw = tp->snd_cwnd; 1338df8bae1dSRodney W. Grimes register u_int incr = tp->t_maxseg; 1339df8bae1dSRodney W. Grimes 1340df8bae1dSRodney W. Grimes if (cw > tp->snd_ssthresh) 134110be5648SGarrett Wollman incr = incr * incr / cw; 1342df8bae1dSRodney W. Grimes tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale); 1343df8bae1dSRodney W. Grimes } 1344df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 1345df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 1346df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 1347df8bae1dSRodney W. Grimes ourfinisacked = 1; 1348df8bae1dSRodney W. Grimes } else { 1349df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 1350df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 1351df8bae1dSRodney W. Grimes ourfinisacked = 0; 1352df8bae1dSRodney W. Grimes } 1353df8bae1dSRodney W. Grimes if (so->so_snd.sb_flags & SB_NOTIFY) 1354df8bae1dSRodney W. Grimes sowwakeup(so); 1355df8bae1dSRodney W. Grimes tp->snd_una = ti->ti_ack; 1356df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 1357df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 1358df8bae1dSRodney W. Grimes 1359df8bae1dSRodney W. Grimes switch (tp->t_state) { 1360df8bae1dSRodney W. Grimes 1361df8bae1dSRodney W. Grimes /* 1362df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 1363df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 1364df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 1365df8bae1dSRodney W. Grimes */ 1366df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1367df8bae1dSRodney W. Grimes if (ourfinisacked) { 1368df8bae1dSRodney W. Grimes /* 1369df8bae1dSRodney W. Grimes * If we can't receive any more 1370df8bae1dSRodney W. Grimes * data, then closing user can proceed. 1371df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 1372df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 1373df8bae1dSRodney W. Grimes * we'll hang forever. 1374df8bae1dSRodney W. Grimes */ 1375df8bae1dSRodney W. Grimes if (so->so_state & SS_CANTRCVMORE) { 1376df8bae1dSRodney W. Grimes soisdisconnected(so); 1377df8bae1dSRodney W. Grimes tp->t_timer[TCPT_2MSL] = tcp_maxidle; 1378df8bae1dSRodney W. Grimes } 1379df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 1380df8bae1dSRodney W. Grimes } 1381df8bae1dSRodney W. Grimes break; 1382df8bae1dSRodney W. Grimes 1383df8bae1dSRodney W. Grimes /* 1384df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 1385df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 1386df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 1387df8bae1dSRodney W. Grimes * the segment. 1388df8bae1dSRodney W. Grimes */ 1389df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1390df8bae1dSRodney W. Grimes if (ourfinisacked) { 1391df8bae1dSRodney W. Grimes tp->t_state = TCPS_TIME_WAIT; 1392df8bae1dSRodney W. Grimes tcp_canceltimers(tp); 1393a0292f23SGarrett Wollman /* Shorten TIME_WAIT [RFC-1644, p.28] */ 1394a0292f23SGarrett Wollman if (tp->cc_recv != 0 && 1395a0292f23SGarrett Wollman tp->t_duration < TCPTV_MSL) 1396a0292f23SGarrett Wollman tp->t_timer[TCPT_2MSL] = 1397a0292f23SGarrett Wollman tp->t_rxtcur * TCPTV_TWTRUNC; 1398a0292f23SGarrett Wollman else 1399df8bae1dSRodney W. Grimes tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1400df8bae1dSRodney W. Grimes soisdisconnected(so); 1401df8bae1dSRodney W. Grimes } 1402df8bae1dSRodney W. Grimes break; 1403df8bae1dSRodney W. Grimes 1404df8bae1dSRodney W. Grimes /* 1405df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 1406df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 1407df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 1408df8bae1dSRodney W. Grimes * enter the closed state and return. 1409df8bae1dSRodney W. Grimes */ 1410df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 1411df8bae1dSRodney W. Grimes if (ourfinisacked) { 1412df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1413df8bae1dSRodney W. Grimes goto drop; 1414df8bae1dSRodney W. Grimes } 1415df8bae1dSRodney W. Grimes break; 1416df8bae1dSRodney W. Grimes 1417df8bae1dSRodney W. Grimes /* 1418df8bae1dSRodney W. Grimes * In TIME_WAIT state the only thing that should arrive 1419df8bae1dSRodney W. Grimes * is a retransmission of the remote FIN. Acknowledge 1420df8bae1dSRodney W. Grimes * it and restart the finack timer. 1421df8bae1dSRodney W. Grimes */ 1422df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 1423df8bae1dSRodney W. Grimes tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1424df8bae1dSRodney W. Grimes goto dropafterack; 1425df8bae1dSRodney W. Grimes } 1426df8bae1dSRodney W. Grimes } 1427df8bae1dSRodney W. Grimes 1428df8bae1dSRodney W. Grimes step6: 1429df8bae1dSRodney W. Grimes /* 1430df8bae1dSRodney W. Grimes * Update window information. 1431df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 1432df8bae1dSRodney W. Grimes */ 1433df8bae1dSRodney W. Grimes if ((tiflags & TH_ACK) && 1434623ae52eSPoul-Henning Kamp (SEQ_LT(tp->snd_wl1, ti->ti_seq) || 1435623ae52eSPoul-Henning Kamp (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 1436623ae52eSPoul-Henning Kamp (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) { 1437df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 1438df8bae1dSRodney W. Grimes if (ti->ti_len == 0 && 1439df8bae1dSRodney W. Grimes tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd) 1440df8bae1dSRodney W. Grimes tcpstat.tcps_rcvwinupd++; 1441df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 1442df8bae1dSRodney W. Grimes tp->snd_wl1 = ti->ti_seq; 1443df8bae1dSRodney W. Grimes tp->snd_wl2 = ti->ti_ack; 1444df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 1445df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 1446df8bae1dSRodney W. Grimes needoutput = 1; 1447df8bae1dSRodney W. Grimes } 1448df8bae1dSRodney W. Grimes 1449df8bae1dSRodney W. Grimes /* 1450df8bae1dSRodney W. Grimes * Process segments with URG. 1451df8bae1dSRodney W. Grimes */ 1452df8bae1dSRodney W. Grimes if ((tiflags & TH_URG) && ti->ti_urp && 1453df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1454df8bae1dSRodney W. Grimes /* 1455df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 1456df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 1457df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 1458df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 1459df8bae1dSRodney W. Grimes */ 1460df8bae1dSRodney W. Grimes if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) { 1461df8bae1dSRodney W. Grimes ti->ti_urp = 0; /* XXX */ 1462df8bae1dSRodney W. Grimes tiflags &= ~TH_URG; /* XXX */ 1463df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 1464df8bae1dSRodney W. Grimes } 1465df8bae1dSRodney W. Grimes /* 1466df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 1467df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 1468df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 1469df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 1470df8bae1dSRodney W. Grimes * In these states we ignore the URG. 1471df8bae1dSRodney W. Grimes * 1472df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 1473df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 1474df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 1475df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 1476df8bae1dSRodney W. Grimes * of data past the urgent section as the original 1477df8bae1dSRodney W. Grimes * spec states (in one of two places). 1478df8bae1dSRodney W. Grimes */ 1479df8bae1dSRodney W. Grimes if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 1480df8bae1dSRodney W. Grimes tp->rcv_up = ti->ti_seq + ti->ti_urp; 1481df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 1482df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 1483df8bae1dSRodney W. Grimes if (so->so_oobmark == 0) 1484df8bae1dSRodney W. Grimes so->so_state |= SS_RCVATMARK; 1485df8bae1dSRodney W. Grimes sohasoutofband(so); 1486df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1487df8bae1dSRodney W. Grimes } 1488df8bae1dSRodney W. Grimes /* 1489df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 1490df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 1491df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 1492df8bae1dSRodney W. Grimes * data may creep in... ick. 1493df8bae1dSRodney W. Grimes */ 149426f9a767SRodney W. Grimes if (ti->ti_urp <= (u_long)ti->ti_len 1495df8bae1dSRodney W. Grimes #ifdef SO_OOBINLINE 1496df8bae1dSRodney W. Grimes && (so->so_options & SO_OOBINLINE) == 0 1497df8bae1dSRodney W. Grimes #endif 1498df8bae1dSRodney W. Grimes ) 1499df8bae1dSRodney W. Grimes tcp_pulloutofband(so, ti, m); 1500df8bae1dSRodney W. Grimes } else 1501df8bae1dSRodney W. Grimes /* 1502df8bae1dSRodney W. Grimes * If no out of band data is expected, 1503df8bae1dSRodney W. Grimes * pull receive urgent pointer along 1504df8bae1dSRodney W. Grimes * with the receive window. 1505df8bae1dSRodney W. Grimes */ 1506df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 1507df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 1508df8bae1dSRodney W. Grimes dodata: /* XXX */ 1509df8bae1dSRodney W. Grimes 1510df8bae1dSRodney W. Grimes /* 1511df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 1512df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 1513df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 1514df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 1515df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 1516df8bae1dSRodney W. Grimes * connection then we just ignore the text. 1517df8bae1dSRodney W. Grimes */ 1518df8bae1dSRodney W. Grimes if ((ti->ti_len || (tiflags&TH_FIN)) && 1519df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1520df8bae1dSRodney W. Grimes TCP_REASS(tp, ti, m, so, tiflags); 1521df8bae1dSRodney W. Grimes /* 1522df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 1523df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 1524df8bae1dSRodney W. Grimes * buffer size. 1525df8bae1dSRodney W. Grimes */ 1526df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 1527df8bae1dSRodney W. Grimes } else { 1528df8bae1dSRodney W. Grimes m_freem(m); 1529df8bae1dSRodney W. Grimes tiflags &= ~TH_FIN; 1530df8bae1dSRodney W. Grimes } 1531df8bae1dSRodney W. Grimes 1532df8bae1dSRodney W. Grimes /* 1533df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 1534df8bae1dSRodney W. Grimes * that the connection is closing. 1535df8bae1dSRodney W. Grimes */ 1536df8bae1dSRodney W. Grimes if (tiflags & TH_FIN) { 1537df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 1538df8bae1dSRodney W. Grimes socantrcvmore(so); 1539a0292f23SGarrett Wollman /* 1540a0292f23SGarrett Wollman * If connection is half-synchronized 1541d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 1542a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 1543a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 1544a0292f23SGarrett Wollman * more input can be expected, send ACK now. 1545a0292f23SGarrett Wollman */ 1546a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) 1547a0292f23SGarrett Wollman tp->t_flags |= TF_DELACK; 1548a0292f23SGarrett Wollman else 1549df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1550df8bae1dSRodney W. Grimes tp->rcv_nxt++; 1551df8bae1dSRodney W. Grimes } 1552df8bae1dSRodney W. Grimes switch (tp->t_state) { 1553df8bae1dSRodney W. Grimes 1554df8bae1dSRodney W. Grimes /* 1555df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 1556df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 1557df8bae1dSRodney W. Grimes */ 1558df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1559df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1560df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 1561df8bae1dSRodney W. Grimes break; 1562df8bae1dSRodney W. Grimes 1563df8bae1dSRodney W. Grimes /* 1564df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 1565df8bae1dSRodney W. Grimes * enter the CLOSING state. 1566df8bae1dSRodney W. Grimes */ 1567df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1568df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 1569df8bae1dSRodney W. Grimes break; 1570df8bae1dSRodney W. Grimes 1571df8bae1dSRodney W. Grimes /* 1572df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 1573df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 1574df8bae1dSRodney W. Grimes * standard timers. 1575df8bae1dSRodney W. Grimes */ 1576df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 1577df8bae1dSRodney W. Grimes tp->t_state = TCPS_TIME_WAIT; 1578df8bae1dSRodney W. Grimes tcp_canceltimers(tp); 1579a0292f23SGarrett Wollman /* Shorten TIME_WAIT [RFC-1644, p.28] */ 1580a0292f23SGarrett Wollman if (tp->cc_recv != 0 && 1581a0292f23SGarrett Wollman tp->t_duration < TCPTV_MSL) { 1582a0292f23SGarrett Wollman tp->t_timer[TCPT_2MSL] = 1583a0292f23SGarrett Wollman tp->t_rxtcur * TCPTV_TWTRUNC; 1584a0292f23SGarrett Wollman /* For transaction client, force ACK now. */ 1585a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1586a0292f23SGarrett Wollman } 1587a0292f23SGarrett Wollman else 1588df8bae1dSRodney W. Grimes tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1589df8bae1dSRodney W. Grimes soisdisconnected(so); 1590df8bae1dSRodney W. Grimes break; 1591df8bae1dSRodney W. Grimes 1592df8bae1dSRodney W. Grimes /* 1593df8bae1dSRodney W. Grimes * In TIME_WAIT state restart the 2 MSL time_wait timer. 1594df8bae1dSRodney W. Grimes */ 1595df8bae1dSRodney W. Grimes case TCPS_TIME_WAIT: 1596df8bae1dSRodney W. Grimes tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 1597df8bae1dSRodney W. Grimes break; 1598df8bae1dSRodney W. Grimes } 1599df8bae1dSRodney W. Grimes } 1600610ee2f9SDavid Greenman #ifdef TCPDEBUG 1601df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) 1602df8bae1dSRodney W. Grimes tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 1603610ee2f9SDavid Greenman #endif 1604df8bae1dSRodney W. Grimes 1605df8bae1dSRodney W. Grimes /* 1606df8bae1dSRodney W. Grimes * Return any desired output. 1607df8bae1dSRodney W. Grimes */ 1608df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 1609df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1610df8bae1dSRodney W. Grimes return; 1611df8bae1dSRodney W. Grimes 1612df8bae1dSRodney W. Grimes dropafterack: 1613df8bae1dSRodney W. Grimes /* 1614df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 1615df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 1616df8bae1dSRodney W. Grimes */ 1617df8bae1dSRodney W. Grimes if (tiflags & TH_RST) 1618df8bae1dSRodney W. Grimes goto drop; 1619a0292f23SGarrett Wollman #ifdef TCPDEBUG 1620a0292f23SGarrett Wollman if (so->so_options & SO_DEBUG) 1621a0292f23SGarrett Wollman tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1622a0292f23SGarrett Wollman #endif 1623df8bae1dSRodney W. Grimes m_freem(m); 1624df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1625df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1626df8bae1dSRodney W. Grimes return; 1627df8bae1dSRodney W. Grimes 1628df8bae1dSRodney W. Grimes dropwithreset: 1629df8bae1dSRodney W. Grimes /* 1630df8bae1dSRodney W. Grimes * Generate a RST, dropping incoming segment. 1631df8bae1dSRodney W. Grimes * Make ACK acceptable to originator of segment. 1632df8bae1dSRodney W. Grimes * Don't bother to respond if destination was broadcast/multicast. 1633df8bae1dSRodney W. Grimes */ 1634df8bae1dSRodney W. Grimes if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) || 1635d4d0967eSDavid Greenman IN_MULTICAST(ntohl(ti->ti_dst.s_addr))) 1636df8bae1dSRodney W. Grimes goto drop; 1637a0292f23SGarrett Wollman #ifdef TCPDEBUG 1638a0292f23SGarrett Wollman if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 1639a0292f23SGarrett Wollman tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1640a0292f23SGarrett Wollman #endif 1641df8bae1dSRodney W. Grimes if (tiflags & TH_ACK) 1642df8bae1dSRodney W. Grimes tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST); 1643df8bae1dSRodney W. Grimes else { 1644df8bae1dSRodney W. Grimes if (tiflags & TH_SYN) 1645df8bae1dSRodney W. Grimes ti->ti_len++; 1646df8bae1dSRodney W. Grimes tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0, 1647df8bae1dSRodney W. Grimes TH_RST|TH_ACK); 1648df8bae1dSRodney W. Grimes } 1649df8bae1dSRodney W. Grimes /* destroy temporarily created socket */ 1650df8bae1dSRodney W. Grimes if (dropsocket) 1651df8bae1dSRodney W. Grimes (void) soabort(so); 1652df8bae1dSRodney W. Grimes return; 1653df8bae1dSRodney W. Grimes 1654df8bae1dSRodney W. Grimes drop: 1655df8bae1dSRodney W. Grimes /* 1656df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 1657df8bae1dSRodney W. Grimes */ 1658610ee2f9SDavid Greenman #ifdef TCPDEBUG 1659a0292f23SGarrett Wollman if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 1660a0292f23SGarrett Wollman tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 1661a0292f23SGarrett Wollman #endif 1662df8bae1dSRodney W. Grimes m_freem(m); 1663df8bae1dSRodney W. Grimes /* destroy temporarily created socket */ 1664df8bae1dSRodney W. Grimes if (dropsocket) 1665df8bae1dSRodney W. Grimes (void) soabort(so); 1666df8bae1dSRodney W. Grimes return; 1667df8bae1dSRodney W. Grimes #ifndef TUBA_INCLUDE 1668df8bae1dSRodney W. Grimes } 1669df8bae1dSRodney W. Grimes 16700312fbe9SPoul-Henning Kamp static void 1671a0292f23SGarrett Wollman tcp_dooptions(tp, cp, cnt, ti, to) 1672df8bae1dSRodney W. Grimes struct tcpcb *tp; 1673df8bae1dSRodney W. Grimes u_char *cp; 1674df8bae1dSRodney W. Grimes int cnt; 1675df8bae1dSRodney W. Grimes struct tcpiphdr *ti; 1676a0292f23SGarrett Wollman struct tcpopt *to; 1677df8bae1dSRodney W. Grimes { 1678a0292f23SGarrett Wollman u_short mss = 0; 1679df8bae1dSRodney W. Grimes int opt, optlen; 1680df8bae1dSRodney W. Grimes 1681df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 1682df8bae1dSRodney W. Grimes opt = cp[0]; 1683df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 1684df8bae1dSRodney W. Grimes break; 1685df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 1686df8bae1dSRodney W. Grimes optlen = 1; 1687df8bae1dSRodney W. Grimes else { 1688df8bae1dSRodney W. Grimes optlen = cp[1]; 1689df8bae1dSRodney W. Grimes if (optlen <= 0) 1690df8bae1dSRodney W. Grimes break; 1691df8bae1dSRodney W. Grimes } 1692df8bae1dSRodney W. Grimes switch (opt) { 1693df8bae1dSRodney W. Grimes 1694df8bae1dSRodney W. Grimes default: 1695df8bae1dSRodney W. Grimes continue; 1696df8bae1dSRodney W. Grimes 1697df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 1698df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 1699df8bae1dSRodney W. Grimes continue; 1700df8bae1dSRodney W. Grimes if (!(ti->ti_flags & TH_SYN)) 1701df8bae1dSRodney W. Grimes continue; 1702df8bae1dSRodney W. Grimes bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); 1703df8bae1dSRodney W. Grimes NTOHS(mss); 1704df8bae1dSRodney W. Grimes break; 1705df8bae1dSRodney W. Grimes 1706df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 1707df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 1708df8bae1dSRodney W. Grimes continue; 1709df8bae1dSRodney W. Grimes if (!(ti->ti_flags & TH_SYN)) 1710df8bae1dSRodney W. Grimes continue; 1711df8bae1dSRodney W. Grimes tp->t_flags |= TF_RCVD_SCALE; 1712df8bae1dSRodney W. Grimes tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); 1713df8bae1dSRodney W. Grimes break; 1714df8bae1dSRodney W. Grimes 1715df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 1716df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 1717df8bae1dSRodney W. Grimes continue; 1718a0292f23SGarrett Wollman to->to_flag |= TOF_TS; 1719a0292f23SGarrett Wollman bcopy((char *)cp + 2, 1720a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 1721a0292f23SGarrett Wollman NTOHL(to->to_tsval); 1722a0292f23SGarrett Wollman bcopy((char *)cp + 6, 1723a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 1724a0292f23SGarrett Wollman NTOHL(to->to_tsecr); 1725df8bae1dSRodney W. Grimes 1726df8bae1dSRodney W. Grimes /* 1727df8bae1dSRodney W. Grimes * A timestamp received in a SYN makes 1728df8bae1dSRodney W. Grimes * it ok to send timestamp requests and replies. 1729df8bae1dSRodney W. Grimes */ 1730df8bae1dSRodney W. Grimes if (ti->ti_flags & TH_SYN) { 1731df8bae1dSRodney W. Grimes tp->t_flags |= TF_RCVD_TSTMP; 1732a0292f23SGarrett Wollman tp->ts_recent = to->to_tsval; 1733df8bae1dSRodney W. Grimes tp->ts_recent_age = tcp_now; 1734df8bae1dSRodney W. Grimes } 1735df8bae1dSRodney W. Grimes break; 1736a0292f23SGarrett Wollman case TCPOPT_CC: 1737a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 1738a0292f23SGarrett Wollman continue; 173940db8ef7SAndras Olah to->to_flag |= TOF_CC; 1740a0292f23SGarrett Wollman bcopy((char *)cp + 2, 1741a0292f23SGarrett Wollman (char *)&to->to_cc, sizeof(to->to_cc)); 1742a0292f23SGarrett Wollman NTOHL(to->to_cc); 1743a0292f23SGarrett Wollman /* 1744a0292f23SGarrett Wollman * A CC or CC.new option received in a SYN makes 1745a0292f23SGarrett Wollman * it ok to send CC in subsequent segments. 1746a0292f23SGarrett Wollman */ 1747a0292f23SGarrett Wollman if (ti->ti_flags & TH_SYN) 1748a0292f23SGarrett Wollman tp->t_flags |= TF_RCVD_CC; 1749a0292f23SGarrett Wollman break; 1750a0292f23SGarrett Wollman case TCPOPT_CCNEW: 1751a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 1752a0292f23SGarrett Wollman continue; 1753a0292f23SGarrett Wollman if (!(ti->ti_flags & TH_SYN)) 1754a0292f23SGarrett Wollman continue; 1755a0292f23SGarrett Wollman to->to_flag |= TOF_CCNEW; 1756a0292f23SGarrett Wollman bcopy((char *)cp + 2, 1757a0292f23SGarrett Wollman (char *)&to->to_cc, sizeof(to->to_cc)); 1758a0292f23SGarrett Wollman NTOHL(to->to_cc); 1759a0292f23SGarrett Wollman /* 1760a0292f23SGarrett Wollman * A CC or CC.new option received in a SYN makes 1761a0292f23SGarrett Wollman * it ok to send CC in subsequent segments. 1762a0292f23SGarrett Wollman */ 1763a0292f23SGarrett Wollman tp->t_flags |= TF_RCVD_CC; 1764a0292f23SGarrett Wollman break; 1765a0292f23SGarrett Wollman case TCPOPT_CCECHO: 1766a0292f23SGarrett Wollman if (optlen != TCPOLEN_CC) 1767a0292f23SGarrett Wollman continue; 1768a0292f23SGarrett Wollman if (!(ti->ti_flags & TH_SYN)) 1769a0292f23SGarrett Wollman continue; 1770a0292f23SGarrett Wollman to->to_flag |= TOF_CCECHO; 1771a0292f23SGarrett Wollman bcopy((char *)cp + 2, 1772a0292f23SGarrett Wollman (char *)&to->to_ccecho, sizeof(to->to_ccecho)); 1773a0292f23SGarrett Wollman NTOHL(to->to_ccecho); 1774a0292f23SGarrett Wollman break; 1775df8bae1dSRodney W. Grimes } 1776df8bae1dSRodney W. Grimes } 1777a0292f23SGarrett Wollman if (ti->ti_flags & TH_SYN) 1778a0292f23SGarrett Wollman tcp_mss(tp, mss); /* sets t_maxseg */ 1779df8bae1dSRodney W. Grimes } 1780df8bae1dSRodney W. Grimes 1781df8bae1dSRodney W. Grimes /* 1782df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 1783df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 1784df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 1785df8bae1dSRodney W. Grimes * sequencing purposes. 1786df8bae1dSRodney W. Grimes */ 17870312fbe9SPoul-Henning Kamp static void 1788df8bae1dSRodney W. Grimes tcp_pulloutofband(so, ti, m) 1789df8bae1dSRodney W. Grimes struct socket *so; 1790df8bae1dSRodney W. Grimes struct tcpiphdr *ti; 1791df8bae1dSRodney W. Grimes register struct mbuf *m; 1792df8bae1dSRodney W. Grimes { 1793df8bae1dSRodney W. Grimes int cnt = ti->ti_urp - 1; 1794df8bae1dSRodney W. Grimes 1795df8bae1dSRodney W. Grimes while (cnt >= 0) { 1796df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 1797df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 1798df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 1799df8bae1dSRodney W. Grimes 1800df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 1801df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 1802df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 1803df8bae1dSRodney W. Grimes m->m_len--; 1804df8bae1dSRodney W. Grimes return; 1805df8bae1dSRodney W. Grimes } 1806df8bae1dSRodney W. Grimes cnt -= m->m_len; 1807df8bae1dSRodney W. Grimes m = m->m_next; 1808df8bae1dSRodney W. Grimes if (m == 0) 1809df8bae1dSRodney W. Grimes break; 1810df8bae1dSRodney W. Grimes } 1811df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 1812df8bae1dSRodney W. Grimes } 1813df8bae1dSRodney W. Grimes 1814df8bae1dSRodney W. Grimes /* 1815df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 1816df8bae1dSRodney W. Grimes * and update averages and current timeout. 1817df8bae1dSRodney W. Grimes */ 18180312fbe9SPoul-Henning Kamp static void 1819df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt) 1820df8bae1dSRodney W. Grimes register struct tcpcb *tp; 1821df8bae1dSRodney W. Grimes short rtt; 1822df8bae1dSRodney W. Grimes { 1823233e8c18SGarrett Wollman #ifdef notdef 1824df8bae1dSRodney W. Grimes register short delta; 1825df8bae1dSRodney W. Grimes 1826df8bae1dSRodney W. Grimes tcpstat.tcps_rttupdated++; 1827fc978271SGarrett Wollman tp->t_rttupdated++; 1828df8bae1dSRodney W. Grimes if (tp->t_srtt != 0) { 1829df8bae1dSRodney W. Grimes /* 1830df8bae1dSRodney W. Grimes * srtt is stored as fixed point with 3 bits after the 1831df8bae1dSRodney W. Grimes * binary point (i.e., scaled by 8). The following magic 1832df8bae1dSRodney W. Grimes * is equivalent to the smoothing algorithm in rfc793 with 1833df8bae1dSRodney W. Grimes * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 1834df8bae1dSRodney W. Grimes * point). Adjust rtt to origin 0. 1835df8bae1dSRodney W. Grimes */ 1836df8bae1dSRodney W. Grimes delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT); 1837df8bae1dSRodney W. Grimes if ((tp->t_srtt += delta) <= 0) 1838df8bae1dSRodney W. Grimes tp->t_srtt = 1; 1839df8bae1dSRodney W. Grimes /* 1840df8bae1dSRodney W. Grimes * We accumulate a smoothed rtt variance (actually, a 1841df8bae1dSRodney W. Grimes * smoothed mean difference), then set the retransmit 1842df8bae1dSRodney W. Grimes * timer to smoothed rtt + 4 times the smoothed variance. 1843df8bae1dSRodney W. Grimes * rttvar is stored as fixed point with 2 bits after the 1844df8bae1dSRodney W. Grimes * binary point (scaled by 4). The following is 1845df8bae1dSRodney W. Grimes * equivalent to rfc793 smoothing with an alpha of .75 1846df8bae1dSRodney W. Grimes * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 1847df8bae1dSRodney W. Grimes * rfc793's wired-in beta. 1848df8bae1dSRodney W. Grimes */ 1849df8bae1dSRodney W. Grimes if (delta < 0) 1850df8bae1dSRodney W. Grimes delta = -delta; 1851df8bae1dSRodney W. Grimes delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT); 1852df8bae1dSRodney W. Grimes if ((tp->t_rttvar += delta) <= 0) 1853df8bae1dSRodney W. Grimes tp->t_rttvar = 1; 1854df8bae1dSRodney W. Grimes } else { 1855df8bae1dSRodney W. Grimes /* 1856df8bae1dSRodney W. Grimes * No rtt measurement yet - use the unsmoothed rtt. 1857df8bae1dSRodney W. Grimes * Set the variance to half the rtt (so our first 1858df8bae1dSRodney W. Grimes * retransmit happens at 3*rtt). 1859df8bae1dSRodney W. Grimes */ 1860df8bae1dSRodney W. Grimes tp->t_srtt = rtt << TCP_RTT_SHIFT; 1861df8bae1dSRodney W. Grimes tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 1862df8bae1dSRodney W. Grimes } 1863233e8c18SGarrett Wollman #else /* Peterson paper */ 1864233e8c18SGarrett Wollman register int delta; 1865233e8c18SGarrett Wollman 1866233e8c18SGarrett Wollman tcpstat.tcps_rttupdated++; 1867233e8c18SGarrett Wollman tp->t_rttupdated++; 1868233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 1869233e8c18SGarrett Wollman /* 1870233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 1871233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 1872233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 1873233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 1874233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 1875233e8c18SGarrett Wollman */ 1876233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 1877233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 1878233e8c18SGarrett Wollman 1879233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 1880233e8c18SGarrett Wollman tp->t_srtt = 1; 1881233e8c18SGarrett Wollman 1882233e8c18SGarrett Wollman /* 1883233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 1884233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 1885233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 1886233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 1887233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 1888233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 1889233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 1890233e8c18SGarrett Wollman * rfc793's wired-in beta. 1891233e8c18SGarrett Wollman */ 1892233e8c18SGarrett Wollman if (delta < 0) 1893233e8c18SGarrett Wollman delta = -delta; 1894233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 1895233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 1896233e8c18SGarrett Wollman tp->t_rttvar = 1; 1897233e8c18SGarrett Wollman } else { 1898233e8c18SGarrett Wollman /* 1899233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 1900233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 1901233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 1902233e8c18SGarrett Wollman */ 1903233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 1904233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 1905233e8c18SGarrett Wollman } 1906233e8c18SGarrett Wollman #endif 1907df8bae1dSRodney W. Grimes tp->t_rtt = 0; 1908df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 1909df8bae1dSRodney W. Grimes 1910df8bae1dSRodney W. Grimes /* 1911df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 1912df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 1913df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 1914df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 1915df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 1916df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 1917df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 1918df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 1919df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 1920df8bae1dSRodney W. Grimes */ 1921233e8c18SGarrett Wollman #ifdef notdef 1922df8bae1dSRodney W. Grimes TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 1923df8bae1dSRodney W. Grimes tp->t_rttmin, TCPTV_REXMTMAX); 1924233e8c18SGarrett Wollman #else /* Peterson */ 1925233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 1926233e8c18SGarrett Wollman max(tp->t_rttmin, TCPTV_MIN + rtt - 1), TCPTV_REXMTMAX); 1927233e8c18SGarrett Wollman #endif 1928df8bae1dSRodney W. Grimes 1929df8bae1dSRodney W. Grimes /* 1930df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 1931df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 1932df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 1933df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 1934df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 1935df8bae1dSRodney W. Grimes */ 1936df8bae1dSRodney W. Grimes tp->t_softerror = 0; 1937df8bae1dSRodney W. Grimes } 1938df8bae1dSRodney W. Grimes 1939df8bae1dSRodney W. Grimes /* 1940df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 1941df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 1942df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 1943df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 1944df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 1945df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 1946df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 1947df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 1948df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 1949df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 1950df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 1951df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 1952df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 1953a0292f23SGarrett Wollman * 1954a0292f23SGarrett Wollman * Also take into account the space needed for options that we 1955a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 1956a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 1957a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 1958a0292f23SGarrett Wollman * data in maxopd. 1959a0292f23SGarrett Wollman * 1960a0292f23SGarrett Wollman * NOTE that this routine is only called when we process an incoming 1961a0292f23SGarrett Wollman * segment, for outgoing segments only tcp_mssopt is called. 1962a0292f23SGarrett Wollman * 1963a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 1964a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 1965a0292f23SGarrett Wollman * MSS of our peer. 1966df8bae1dSRodney W. Grimes */ 1967a0292f23SGarrett Wollman void 1968df8bae1dSRodney W. Grimes tcp_mss(tp, offer) 1969a0292f23SGarrett Wollman struct tcpcb *tp; 1970a0292f23SGarrett Wollman int offer; 1971df8bae1dSRodney W. Grimes { 1972df8bae1dSRodney W. Grimes register struct rtentry *rt; 1973df8bae1dSRodney W. Grimes struct ifnet *ifp; 1974df8bae1dSRodney W. Grimes register int rtt, mss; 1975df8bae1dSRodney W. Grimes u_long bufsize; 1976df8bae1dSRodney W. Grimes struct inpcb *inp; 1977df8bae1dSRodney W. Grimes struct socket *so; 1978a0292f23SGarrett Wollman struct rmxp_tao *taop; 1979a0292f23SGarrett Wollman int origoffer = offer; 1980df8bae1dSRodney W. Grimes 1981df8bae1dSRodney W. Grimes inp = tp->t_inpcb; 1982a0292f23SGarrett Wollman if ((rt = tcp_rtlookup(inp)) == NULL) { 1983a0292f23SGarrett Wollman tp->t_maxopd = tp->t_maxseg = tcp_mssdflt; 1984a0292f23SGarrett Wollman return; 1985df8bae1dSRodney W. Grimes } 1986df8bae1dSRodney W. Grimes ifp = rt->rt_ifp; 1987df8bae1dSRodney W. Grimes so = inp->inp_socket; 1988df8bae1dSRodney W. Grimes 1989a0292f23SGarrett Wollman taop = rmx_taop(rt->rt_rmx); 1990a0292f23SGarrett Wollman /* 1991a0292f23SGarrett Wollman * Offer == -1 means that we didn't receive SYN yet, 1992a0292f23SGarrett Wollman * use cached value in that case; 1993a0292f23SGarrett Wollman */ 1994a0292f23SGarrett Wollman if (offer == -1) 1995a0292f23SGarrett Wollman offer = taop->tao_mssopt; 1996a0292f23SGarrett Wollman /* 1997a0292f23SGarrett Wollman * Offer == 0 means that there was no MSS on the SYN segment, 1998a0292f23SGarrett Wollman * in this case we use tcp_mssdflt. 1999a0292f23SGarrett Wollman */ 2000a0292f23SGarrett Wollman if (offer == 0) 2001a0292f23SGarrett Wollman offer = tcp_mssdflt; 2002a0292f23SGarrett Wollman else 2003a0292f23SGarrett Wollman /* 2004a0292f23SGarrett Wollman * Sanity check: make sure that maxopd will be large 2005a0292f23SGarrett Wollman * enough to allow some data on segments even is the 2006a0292f23SGarrett Wollman * all the option space is used (40bytes). Otherwise 2007a0292f23SGarrett Wollman * funny things may happen in tcp_output. 2008a0292f23SGarrett Wollman */ 2009a0292f23SGarrett Wollman offer = max(offer, 64); 2010a0292f23SGarrett Wollman taop->tao_mssopt = offer; 2011a0292f23SGarrett Wollman 2012df8bae1dSRodney W. Grimes /* 2013df8bae1dSRodney W. Grimes * While we're here, check if there's an initial rtt 2014df8bae1dSRodney W. Grimes * or rttvar. Convert from the route-table units 2015df8bae1dSRodney W. Grimes * to scaled multiples of the slow timeout timer. 2016df8bae1dSRodney W. Grimes */ 2017df8bae1dSRodney W. Grimes if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 2018df8bae1dSRodney W. Grimes /* 2019a0292f23SGarrett Wollman * XXX the lock bit for RTT indicates that the value 2020df8bae1dSRodney W. Grimes * is also a minimum value; this is subject to time. 2021df8bae1dSRodney W. Grimes */ 2022df8bae1dSRodney W. Grimes if (rt->rt_rmx.rmx_locks & RTV_RTT) 2023df8bae1dSRodney W. Grimes tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ); 2024df8bae1dSRodney W. Grimes tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); 2025dd224982SGarrett Wollman tcpstat.tcps_usedrtt++; 2026dd224982SGarrett Wollman if (rt->rt_rmx.rmx_rttvar) { 2027df8bae1dSRodney W. Grimes tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 2028df8bae1dSRodney W. Grimes (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); 2029dd224982SGarrett Wollman tcpstat.tcps_usedrttvar++; 2030dd224982SGarrett Wollman } else { 2031df8bae1dSRodney W. Grimes /* default variation is +- 1 rtt */ 2032df8bae1dSRodney W. Grimes tp->t_rttvar = 2033df8bae1dSRodney W. Grimes tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 2034dd224982SGarrett Wollman } 2035df8bae1dSRodney W. Grimes TCPT_RANGESET(tp->t_rxtcur, 2036df8bae1dSRodney W. Grimes ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 2037df8bae1dSRodney W. Grimes tp->t_rttmin, TCPTV_REXMTMAX); 2038df8bae1dSRodney W. Grimes } 2039df8bae1dSRodney W. Grimes /* 2040df8bae1dSRodney W. Grimes * if there's an mtu associated with the route, use it 2041df8bae1dSRodney W. Grimes */ 2042df8bae1dSRodney W. Grimes if (rt->rt_rmx.rmx_mtu) 2043df8bae1dSRodney W. Grimes mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr); 2044df8bae1dSRodney W. Grimes else 2045df8bae1dSRodney W. Grimes { 2046df8bae1dSRodney W. Grimes mss = ifp->if_mtu - sizeof(struct tcpiphdr); 2047a0292f23SGarrett Wollman if (!in_localaddr(inp->inp_faddr)) 2048a0292f23SGarrett Wollman mss = min(mss, tcp_mssdflt); 2049a0292f23SGarrett Wollman } 2050a0292f23SGarrett Wollman mss = min(mss, offer); 2051a0292f23SGarrett Wollman /* 2052a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 2053a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 2054a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 2055a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 2056a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 2057a0292f23SGarrett Wollman */ 2058a0292f23SGarrett Wollman tp->t_maxopd = mss; 2059a0292f23SGarrett Wollman 2060a0292f23SGarrett Wollman /* 2061a0292f23SGarrett Wollman * In case of T/TCP, origoffer==-1 indicates, that no segments 2062a0292f23SGarrett Wollman * were received yet. In this case we just guess, otherwise 2063a0292f23SGarrett Wollman * we do the same as before T/TCP. 2064a0292f23SGarrett Wollman */ 2065a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2066a0292f23SGarrett Wollman (origoffer == -1 || 2067a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2068a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 2069a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 2070a0292f23SGarrett Wollman (origoffer == -1 || 2071a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)) 2072a0292f23SGarrett Wollman mss -= TCPOLEN_CC_APPA; 2073a0292f23SGarrett Wollman 2074df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 2075df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2076df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 2077df8bae1dSRodney W. Grimes #else 2078df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2079df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 2080df8bae1dSRodney W. Grimes #endif 2081df8bae1dSRodney W. Grimes /* 2082df8bae1dSRodney W. Grimes * If there's a pipesize, change the socket buffer 2083df8bae1dSRodney W. Grimes * to that size. Make the socket buffers an integral 2084df8bae1dSRodney W. Grimes * number of mss units; if the mss is larger than 2085df8bae1dSRodney W. Grimes * the socket buffer, decrease the mss. 2086df8bae1dSRodney W. Grimes */ 2087df8bae1dSRodney W. Grimes #ifdef RTV_SPIPE 2088df8bae1dSRodney W. Grimes if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) 2089df8bae1dSRodney W. Grimes #endif 2090df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 2091df8bae1dSRodney W. Grimes if (bufsize < mss) 2092df8bae1dSRodney W. Grimes mss = bufsize; 2093df8bae1dSRodney W. Grimes else { 2094df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2095df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2096df8bae1dSRodney W. Grimes bufsize = sb_max; 2097df8bae1dSRodney W. Grimes (void)sbreserve(&so->so_snd, bufsize); 2098df8bae1dSRodney W. Grimes } 2099df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 2100df8bae1dSRodney W. Grimes 2101df8bae1dSRodney W. Grimes #ifdef RTV_RPIPE 2102df8bae1dSRodney W. Grimes if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) 2103df8bae1dSRodney W. Grimes #endif 2104df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 2105df8bae1dSRodney W. Grimes if (bufsize > mss) { 2106df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 2107df8bae1dSRodney W. Grimes if (bufsize > sb_max) 2108df8bae1dSRodney W. Grimes bufsize = sb_max; 2109df8bae1dSRodney W. Grimes (void)sbreserve(&so->so_rcv, bufsize); 2110df8bae1dSRodney W. Grimes } 2111a0292f23SGarrett Wollman /* 2112a0292f23SGarrett Wollman * Don't force slow-start on local network. 2113a0292f23SGarrett Wollman */ 2114a0292f23SGarrett Wollman if (!in_localaddr(inp->inp_faddr)) 2115df8bae1dSRodney W. Grimes tp->snd_cwnd = mss; 2116df8bae1dSRodney W. Grimes 2117df8bae1dSRodney W. Grimes if (rt->rt_rmx.rmx_ssthresh) { 2118df8bae1dSRodney W. Grimes /* 2119df8bae1dSRodney W. Grimes * There's some sort of gateway or interface 2120df8bae1dSRodney W. Grimes * buffer limit on the path. Use this to set 2121df8bae1dSRodney W. Grimes * the slow start threshhold, but set the 2122df8bae1dSRodney W. Grimes * threshold to no less than 2*mss. 2123df8bae1dSRodney W. Grimes */ 2124df8bae1dSRodney W. Grimes tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 2125dd224982SGarrett Wollman tcpstat.tcps_usedssthresh++; 2126df8bae1dSRodney W. Grimes } 2127a0292f23SGarrett Wollman } 2128a0292f23SGarrett Wollman 2129a0292f23SGarrett Wollman /* 2130a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 2131a0292f23SGarrett Wollman */ 2132a0292f23SGarrett Wollman int 2133a0292f23SGarrett Wollman tcp_mssopt(tp) 2134a0292f23SGarrett Wollman struct tcpcb *tp; 2135a0292f23SGarrett Wollman { 2136a0292f23SGarrett Wollman struct rtentry *rt; 2137a0292f23SGarrett Wollman 2138a0292f23SGarrett Wollman rt = tcp_rtlookup(tp->t_inpcb); 2139a0292f23SGarrett Wollman if (rt == NULL) 2140a0292f23SGarrett Wollman return tcp_mssdflt; 2141a0292f23SGarrett Wollman 2142a0292f23SGarrett Wollman return rt->rt_ifp->if_mtu - sizeof(struct tcpiphdr); 2143df8bae1dSRodney W. Grimes } 2144df8bae1dSRodney W. Grimes #endif /* TUBA_INCLUDE */ 2145