Home | History | Annotate | Line # | Download | only in netinet
tcp_input.c revision 1.20
      1  1.20      cgd /*	$NetBSD: tcp_input.c,v 1.20 1995/11/21 01:07:39 cgd Exp $	*/
      2  1.10      cgd 
      3   1.1      cgd /*
      4   1.9  mycroft  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994
      5   1.9  mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1      cgd  *
      7   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1      cgd  * modification, are permitted provided that the following conditions
      9   1.1      cgd  * are met:
     10   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1      cgd  *    must display the following acknowledgement:
     17   1.1      cgd  *	This product includes software developed by the University of
     18   1.1      cgd  *	California, Berkeley and its contributors.
     19   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1      cgd  *    may be used to endorse or promote products derived from this software
     21   1.1      cgd  *    without specific prior written permission.
     22   1.1      cgd  *
     23   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1      cgd  * SUCH DAMAGE.
     34   1.1      cgd  *
     35  1.10      cgd  *	@(#)tcp_input.c	8.5 (Berkeley) 4/10/94
     36   1.1      cgd  */
     37   1.1      cgd 
     38   1.9  mycroft #ifndef TUBA_INCLUDE
     39   1.3  mycroft #include <sys/param.h>
     40   1.3  mycroft #include <sys/systm.h>
     41   1.3  mycroft #include <sys/malloc.h>
     42   1.3  mycroft #include <sys/mbuf.h>
     43   1.3  mycroft #include <sys/protosw.h>
     44   1.3  mycroft #include <sys/socket.h>
     45   1.3  mycroft #include <sys/socketvar.h>
     46   1.3  mycroft #include <sys/errno.h>
     47   1.1      cgd 
     48   1.3  mycroft #include <net/if.h>
     49   1.3  mycroft #include <net/route.h>
     50   1.1      cgd 
     51   1.3  mycroft #include <netinet/in.h>
     52   1.3  mycroft #include <netinet/in_systm.h>
     53   1.3  mycroft #include <netinet/ip.h>
     54   1.3  mycroft #include <netinet/in_pcb.h>
     55   1.3  mycroft #include <netinet/ip_var.h>
     56   1.3  mycroft #include <netinet/tcp.h>
     57   1.3  mycroft #include <netinet/tcp_fsm.h>
     58   1.3  mycroft #include <netinet/tcp_seq.h>
     59   1.3  mycroft #include <netinet/tcp_timer.h>
     60   1.3  mycroft #include <netinet/tcp_var.h>
     61   1.3  mycroft #include <netinet/tcpip.h>
     62   1.3  mycroft #include <netinet/tcp_debug.h>
     63   1.1      cgd 
     64   1.1      cgd int	tcprexmtthresh = 3;
     65   1.1      cgd struct	tcpiphdr tcp_saveti;
     66  1.18  mycroft struct	inpcb *tcp_last_inpcb = 0;
     67   1.1      cgd 
     68   1.9  mycroft extern u_long sb_max;
     69   1.9  mycroft 
     70   1.9  mycroft #endif /* TUBA_INCLUDE */
     71   1.9  mycroft #define TCP_PAWS_IDLE	(24 * 24 * 60 * 60 * PR_SLOWHZ)
     72   1.9  mycroft 
     73   1.9  mycroft /* for modulo comparisons of timestamps */
     74   1.9  mycroft #define TSTMP_LT(a,b)	((int)((a)-(b)) < 0)
     75   1.9  mycroft #define TSTMP_GEQ(a,b)	((int)((a)-(b)) >= 0)
     76   1.9  mycroft 
     77   1.9  mycroft 
     78   1.1      cgd /*
     79   1.1      cgd  * Insert segment ti into reassembly queue of tcp with
     80   1.1      cgd  * control block tp.  Return TH_FIN if reassembly now includes
     81   1.1      cgd  * a segment with FIN.  The macro form does the common case inline
     82   1.1      cgd  * (segment is the next to be received on an established connection,
     83   1.1      cgd  * and the queue is empty), avoiding linkage into and removal
     84   1.1      cgd  * from the queue and repetition of various conversions.
     85   1.1      cgd  * Set DELACK for segments received in order, but ack immediately
     86   1.1      cgd  * when segments are out of order (so fast retransmit can work).
     87   1.1      cgd  */
     88   1.1      cgd #define	TCP_REASS(tp, ti, m, so, flags) { \
     89   1.1      cgd 	if ((ti)->ti_seq == (tp)->rcv_nxt && \
     90  1.20      cgd 	    (tp)->segq.lh_first == NULL && \
     91   1.1      cgd 	    (tp)->t_state == TCPS_ESTABLISHED) { \
     92   1.8  mycroft 		if ((ti)->ti_flags & TH_PUSH) \
     93   1.8  mycroft 			tp->t_flags |= TF_ACKNOW; \
     94   1.8  mycroft 		else \
     95   1.8  mycroft 			tp->t_flags |= TF_DELACK; \
     96   1.1      cgd 		(tp)->rcv_nxt += (ti)->ti_len; \
     97   1.1      cgd 		flags = (ti)->ti_flags & TH_FIN; \
     98   1.1      cgd 		tcpstat.tcps_rcvpack++;\
     99   1.1      cgd 		tcpstat.tcps_rcvbyte += (ti)->ti_len;\
    100   1.1      cgd 		sbappend(&(so)->so_rcv, (m)); \
    101   1.1      cgd 		sorwakeup(so); \
    102   1.1      cgd 	} else { \
    103   1.1      cgd 		(flags) = tcp_reass((tp), (ti), (m)); \
    104   1.1      cgd 		tp->t_flags |= TF_ACKNOW; \
    105   1.1      cgd 	} \
    106   1.1      cgd }
    107   1.9  mycroft #ifndef TUBA_INCLUDE
    108   1.1      cgd 
    109   1.5  mycroft int
    110   1.1      cgd tcp_reass(tp, ti, m)
    111   1.1      cgd 	register struct tcpcb *tp;
    112   1.1      cgd 	register struct tcpiphdr *ti;
    113   1.1      cgd 	struct mbuf *m;
    114   1.1      cgd {
    115  1.20      cgd 	register struct ipqent *p, *q, *nq, *tiqe;
    116   1.1      cgd 	struct socket *so = tp->t_inpcb->inp_socket;
    117   1.1      cgd 	int flags;
    118   1.1      cgd 
    119   1.1      cgd 	/*
    120   1.1      cgd 	 * Call with ti==0 after become established to
    121   1.1      cgd 	 * force pre-ESTABLISHED data up to user socket.
    122   1.1      cgd 	 */
    123   1.1      cgd 	if (ti == 0)
    124   1.1      cgd 		goto present;
    125   1.1      cgd 
    126   1.1      cgd 	/*
    127  1.20      cgd 	 * Allocate a new queue entry, before we throw away any data.
    128  1.20      cgd 	 * If we can't, just drop the packet.  XXX
    129  1.20      cgd 	 */
    130  1.20      cgd 	MALLOC(tiqe, struct ipqent *, sizeof (struct ipqent), M_IPQ, M_NOWAIT);
    131  1.20      cgd 	if (tiqe == NULL) {
    132  1.20      cgd 		tcpstat.tcps_rcvmemdrop++;
    133  1.20      cgd 		m_freem(m);
    134  1.20      cgd 		return (0);
    135  1.20      cgd 	}
    136  1.20      cgd 
    137  1.20      cgd 	/*
    138   1.1      cgd 	 * Find a segment which begins after this one does.
    139   1.1      cgd 	 */
    140  1.20      cgd 	for (p = NULL, q = tp->segq.lh_first; q != NULL;
    141  1.20      cgd 	    p = q, q = q->ipqe_q.le_next)
    142  1.20      cgd 		if (SEQ_GT(q->ipqe_tcp->ti_seq, ti->ti_seq))
    143   1.1      cgd 			break;
    144   1.1      cgd 
    145   1.1      cgd 	/*
    146   1.1      cgd 	 * If there is a preceding segment, it may provide some of
    147   1.1      cgd 	 * our data already.  If so, drop the data from the incoming
    148   1.1      cgd 	 * segment.  If it provides all of our data, drop us.
    149   1.1      cgd 	 */
    150  1.20      cgd 	if (p != NULL) {
    151  1.20      cgd 		register struct tcpiphdr *phdr = p->ipqe_tcp;
    152   1.1      cgd 		register int i;
    153  1.20      cgd 
    154   1.1      cgd 		/* conversion to int (in i) handles seq wraparound */
    155  1.20      cgd 		i = phdr->ti_seq + phdr->ti_len - ti->ti_seq;
    156   1.1      cgd 		if (i > 0) {
    157   1.1      cgd 			if (i >= ti->ti_len) {
    158   1.1      cgd 				tcpstat.tcps_rcvduppack++;
    159   1.1      cgd 				tcpstat.tcps_rcvdupbyte += ti->ti_len;
    160   1.1      cgd 				m_freem(m);
    161  1.20      cgd 				FREE(tiqe, M_IPQ);
    162   1.1      cgd 				return (0);
    163   1.1      cgd 			}
    164   1.1      cgd 			m_adj(m, i);
    165   1.1      cgd 			ti->ti_len -= i;
    166   1.1      cgd 			ti->ti_seq += i;
    167   1.1      cgd 		}
    168   1.1      cgd 	}
    169   1.1      cgd 	tcpstat.tcps_rcvoopack++;
    170   1.1      cgd 	tcpstat.tcps_rcvoobyte += ti->ti_len;
    171   1.1      cgd 
    172   1.1      cgd 	/*
    173   1.1      cgd 	 * While we overlap succeeding segments trim them or,
    174   1.1      cgd 	 * if they are completely covered, dequeue them.
    175   1.1      cgd 	 */
    176  1.20      cgd 	for (; q != NULL; q = nq) {
    177  1.20      cgd 		register struct tcpiphdr *qhdr = q->ipqe_tcp;
    178  1.20      cgd 		register int i = (ti->ti_seq + ti->ti_len) - qhdr->ti_seq;
    179  1.20      cgd 
    180   1.1      cgd 		if (i <= 0)
    181   1.1      cgd 			break;
    182  1.20      cgd 		if (i < qhdr->ti_len) {
    183  1.20      cgd 			qhdr->ti_seq += i;
    184  1.20      cgd 			qhdr->ti_len -= i;
    185  1.20      cgd 			m_adj(q->ipqe_m, i);
    186   1.1      cgd 			break;
    187   1.1      cgd 		}
    188  1.20      cgd 		nq = q->ipqe_q.le_next;
    189  1.20      cgd 		m_freem(q->ipqe_m);
    190  1.20      cgd 		LIST_REMOVE(q, ipqe_q);
    191  1.20      cgd 		FREE(q, M_IPQ);
    192   1.1      cgd 	}
    193   1.1      cgd 
    194  1.20      cgd 	/* Insert the new fragment queue entry into place. */
    195  1.20      cgd 	tiqe->ipqe_m = m;
    196  1.20      cgd 	tiqe->ipqe_tcp = ti;
    197  1.20      cgd 	if (p == NULL) {
    198  1.20      cgd 		LIST_INSERT_HEAD(&tp->segq, tiqe, ipqe_q);
    199  1.20      cgd 	} else {
    200  1.20      cgd 		LIST_INSERT_AFTER(p, tiqe, ipqe_q);
    201  1.20      cgd 	}
    202   1.1      cgd 
    203   1.1      cgd present:
    204   1.1      cgd 	/*
    205   1.1      cgd 	 * Present data to user, advancing rcv_nxt through
    206   1.1      cgd 	 * completed sequence space.
    207   1.1      cgd 	 */
    208  1.11  mycroft 	if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
    209   1.1      cgd 		return (0);
    210  1.20      cgd 	q = tp->segq.lh_first;
    211  1.20      cgd 	if (q == NULL || q->ipqe_tcp->ti_seq != tp->rcv_nxt)
    212   1.1      cgd 		return (0);
    213  1.20      cgd 	if (tp->t_state == TCPS_SYN_RECEIVED && q->ipqe_tcp->ti_len)
    214   1.1      cgd 		return (0);
    215   1.1      cgd 	do {
    216  1.20      cgd 		tp->rcv_nxt += q->ipqe_tcp->ti_len;
    217  1.20      cgd 		flags = q->ipqe_tcp->ti_flags & TH_FIN;
    218  1.20      cgd 
    219  1.20      cgd 		nq = q->ipqe_q.le_next;
    220  1.20      cgd 		LIST_REMOVE(q, ipqe_q);
    221   1.1      cgd 		if (so->so_state & SS_CANTRCVMORE)
    222  1.20      cgd 			m_freem(q->ipqe_m);
    223   1.1      cgd 		else
    224  1.20      cgd 			sbappend(&so->so_rcv, q->ipqe_m);
    225  1.20      cgd 		FREE(q, M_IPQ);
    226  1.20      cgd 		q = nq;
    227  1.20      cgd 	} while (q != NULL && q->ipqe_tcp->ti_seq == tp->rcv_nxt);
    228   1.1      cgd 	sorwakeup(so);
    229   1.1      cgd 	return (flags);
    230   1.1      cgd }
    231   1.1      cgd 
    232   1.1      cgd /*
    233   1.1      cgd  * TCP input routine, follows pages 65-76 of the
    234   1.1      cgd  * protocol specification dated September, 1981 very closely.
    235   1.1      cgd  */
    236   1.5  mycroft void
    237   1.1      cgd tcp_input(m, iphlen)
    238   1.1      cgd 	register struct mbuf *m;
    239   1.1      cgd 	int iphlen;
    240   1.1      cgd {
    241   1.1      cgd 	register struct tcpiphdr *ti;
    242   1.1      cgd 	register struct inpcb *inp;
    243   1.9  mycroft 	caddr_t optp = NULL;
    244   1.9  mycroft 	int optlen;
    245   1.1      cgd 	int len, tlen, off;
    246   1.1      cgd 	register struct tcpcb *tp = 0;
    247   1.1      cgd 	register int tiflags;
    248   1.1      cgd 	struct socket *so;
    249   1.1      cgd 	int todrop, acked, ourfinisacked, needoutput = 0;
    250   1.1      cgd 	short ostate;
    251   1.1      cgd 	struct in_addr laddr;
    252   1.1      cgd 	int dropsocket = 0;
    253   1.1      cgd 	int iss = 0;
    254  1.12      cgd 	u_long tiwin;
    255  1.12      cgd 	u_int32_t ts_val, ts_ecr;
    256   1.9  mycroft 	int ts_present = 0;
    257   1.1      cgd 
    258   1.1      cgd 	tcpstat.tcps_rcvtotal++;
    259   1.1      cgd 	/*
    260   1.1      cgd 	 * Get IP and TCP header together in first mbuf.
    261   1.1      cgd 	 * Note: IP leaves IP header in first mbuf.
    262   1.1      cgd 	 */
    263   1.1      cgd 	ti = mtod(m, struct tcpiphdr *);
    264   1.1      cgd 	if (iphlen > sizeof (struct ip))
    265   1.1      cgd 		ip_stripoptions(m, (struct mbuf *)0);
    266   1.1      cgd 	if (m->m_len < sizeof (struct tcpiphdr)) {
    267   1.1      cgd 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
    268   1.1      cgd 			tcpstat.tcps_rcvshort++;
    269   1.1      cgd 			return;
    270   1.1      cgd 		}
    271   1.1      cgd 		ti = mtod(m, struct tcpiphdr *);
    272   1.1      cgd 	}
    273   1.1      cgd 
    274   1.1      cgd 	/*
    275   1.1      cgd 	 * Checksum extended TCP header and data.
    276   1.1      cgd 	 */
    277   1.1      cgd 	tlen = ((struct ip *)ti)->ip_len;
    278   1.1      cgd 	len = sizeof (struct ip) + tlen;
    279  1.20      cgd 	bzero(ti->ti_x1, sizeof ti->ti_x1);
    280  1.12      cgd 	ti->ti_len = (u_int16_t)tlen;
    281   1.1      cgd 	HTONS(ti->ti_len);
    282   1.1      cgd 	if (ti->ti_sum = in_cksum(m, len)) {
    283   1.1      cgd 		tcpstat.tcps_rcvbadsum++;
    284   1.1      cgd 		goto drop;
    285   1.1      cgd 	}
    286   1.9  mycroft #endif /* TUBA_INCLUDE */
    287   1.1      cgd 
    288   1.1      cgd 	/*
    289   1.1      cgd 	 * Check that TCP offset makes sense,
    290   1.1      cgd 	 * pull out TCP options and adjust length.		XXX
    291   1.1      cgd 	 */
    292   1.1      cgd 	off = ti->ti_off << 2;
    293   1.1      cgd 	if (off < sizeof (struct tcphdr) || off > tlen) {
    294   1.1      cgd 		tcpstat.tcps_rcvbadoff++;
    295   1.1      cgd 		goto drop;
    296   1.1      cgd 	}
    297   1.1      cgd 	tlen -= off;
    298   1.1      cgd 	ti->ti_len = tlen;
    299   1.1      cgd 	if (off > sizeof (struct tcphdr)) {
    300   1.1      cgd 		if (m->m_len < sizeof(struct ip) + off) {
    301   1.1      cgd 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
    302   1.1      cgd 				tcpstat.tcps_rcvshort++;
    303   1.1      cgd 				return;
    304   1.1      cgd 			}
    305   1.1      cgd 			ti = mtod(m, struct tcpiphdr *);
    306   1.1      cgd 		}
    307   1.9  mycroft 		optlen = off - sizeof (struct tcphdr);
    308   1.9  mycroft 		optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
    309   1.9  mycroft 		/*
    310   1.9  mycroft 		 * Do quick retrieval of timestamp options ("options
    311   1.9  mycroft 		 * prediction?").  If timestamp is the only option and it's
    312   1.9  mycroft 		 * formatted as recommended in RFC 1323 appendix A, we
    313   1.9  mycroft 		 * quickly get the values now and not bother calling
    314   1.9  mycroft 		 * tcp_dooptions(), etc.
    315   1.9  mycroft 		 */
    316   1.9  mycroft 		if ((optlen == TCPOLEN_TSTAMP_APPA ||
    317   1.9  mycroft 		     (optlen > TCPOLEN_TSTAMP_APPA &&
    318   1.9  mycroft 			optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
    319  1.12      cgd 		     *(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
    320   1.9  mycroft 		     (ti->ti_flags & TH_SYN) == 0) {
    321   1.9  mycroft 			ts_present = 1;
    322  1.12      cgd 			ts_val = ntohl(*(u_int32_t *)(optp + 4));
    323  1.12      cgd 			ts_ecr = ntohl(*(u_int32_t *)(optp + 8));
    324   1.9  mycroft 			optp = NULL;	/* we've parsed the options */
    325   1.1      cgd 		}
    326   1.1      cgd 	}
    327   1.1      cgd 	tiflags = ti->ti_flags;
    328   1.1      cgd 
    329   1.1      cgd 	/*
    330   1.1      cgd 	 * Convert TCP protocol specific fields to host format.
    331   1.1      cgd 	 */
    332   1.1      cgd 	NTOHL(ti->ti_seq);
    333   1.1      cgd 	NTOHL(ti->ti_ack);
    334   1.1      cgd 	NTOHS(ti->ti_win);
    335   1.1      cgd 	NTOHS(ti->ti_urp);
    336   1.1      cgd 
    337   1.1      cgd 	/*
    338   1.1      cgd 	 * Locate pcb for segment.
    339   1.1      cgd 	 */
    340   1.1      cgd findpcb:
    341   1.1      cgd 	inp = tcp_last_inpcb;
    342  1.18  mycroft 	if (inp == 0 ||
    343  1.18  mycroft 	    inp->inp_lport != ti->ti_dport ||
    344   1.1      cgd 	    inp->inp_fport != ti->ti_sport ||
    345   1.1      cgd 	    inp->inp_faddr.s_addr != ti->ti_src.s_addr ||
    346   1.1      cgd 	    inp->inp_laddr.s_addr != ti->ti_dst.s_addr) {
    347  1.18  mycroft 		++tcpstat.tcps_pcbcachemiss;
    348  1.18  mycroft 		inp = in_pcblookup(&tcbtable, ti->ti_src, ti->ti_sport,
    349   1.1      cgd 		    ti->ti_dst, ti->ti_dport, INPLOOKUP_WILDCARD);
    350  1.18  mycroft 		/*
    351  1.18  mycroft 		 * If the state is CLOSED (i.e., TCB does not exist) then
    352  1.18  mycroft 		 * all data in the incoming segment is discarded.
    353  1.18  mycroft 		 * If the TCB exists but is in CLOSED state, it is embryonic,
    354  1.18  mycroft 		 * but should either do a listen or a connect soon.
    355  1.18  mycroft 		 */
    356  1.18  mycroft 		if (inp == 0)
    357  1.18  mycroft 			goto dropwithreset;
    358  1.18  mycroft 		tcp_last_inpcb = inp;
    359   1.1      cgd 	}
    360   1.1      cgd 
    361   1.1      cgd 	tp = intotcpcb(inp);
    362   1.1      cgd 	if (tp == 0)
    363   1.1      cgd 		goto dropwithreset;
    364   1.1      cgd 	if (tp->t_state == TCPS_CLOSED)
    365   1.1      cgd 		goto drop;
    366   1.9  mycroft 
    367   1.9  mycroft 	/* Unscale the window into a 32-bit value. */
    368   1.9  mycroft 	if ((tiflags & TH_SYN) == 0)
    369   1.9  mycroft 		tiwin = ti->ti_win << tp->snd_scale;
    370   1.9  mycroft 	else
    371   1.9  mycroft 		tiwin = ti->ti_win;
    372   1.9  mycroft 
    373   1.1      cgd 	so = inp->inp_socket;
    374   1.1      cgd 	if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
    375   1.1      cgd 		if (so->so_options & SO_DEBUG) {
    376   1.1      cgd 			ostate = tp->t_state;
    377   1.1      cgd 			tcp_saveti = *ti;
    378   1.1      cgd 		}
    379   1.1      cgd 		if (so->so_options & SO_ACCEPTCONN) {
    380   1.1      cgd 			so = sonewconn(so, 0);
    381   1.1      cgd 			if (so == 0)
    382   1.1      cgd 				goto drop;
    383   1.1      cgd 			/*
    384   1.1      cgd 			 * This is ugly, but ....
    385   1.1      cgd 			 *
    386   1.1      cgd 			 * Mark socket as temporary until we're
    387   1.1      cgd 			 * committed to keeping it.  The code at
    388   1.1      cgd 			 * ``drop'' and ``dropwithreset'' check the
    389   1.1      cgd 			 * flag dropsocket to see if the temporary
    390   1.1      cgd 			 * socket created here should be discarded.
    391   1.1      cgd 			 * We mark the socket as discardable until
    392   1.1      cgd 			 * we're committed to it below in TCPS_LISTEN.
    393   1.1      cgd 			 */
    394   1.1      cgd 			dropsocket++;
    395   1.1      cgd 			inp = (struct inpcb *)so->so_pcb;
    396   1.1      cgd 			inp->inp_laddr = ti->ti_dst;
    397   1.1      cgd 			inp->inp_lport = ti->ti_dport;
    398   1.1      cgd #if BSD>=43
    399   1.1      cgd 			inp->inp_options = ip_srcroute();
    400   1.1      cgd #endif
    401   1.1      cgd 			tp = intotcpcb(inp);
    402   1.1      cgd 			tp->t_state = TCPS_LISTEN;
    403   1.9  mycroft 
    404   1.9  mycroft 			/* Compute proper scaling value from buffer space
    405   1.9  mycroft 			 */
    406   1.9  mycroft 			while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
    407   1.9  mycroft 			   TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat)
    408   1.9  mycroft 				tp->request_r_scale++;
    409   1.1      cgd 		}
    410   1.1      cgd 	}
    411   1.1      cgd 
    412   1.1      cgd 	/*
    413   1.1      cgd 	 * Segment received on connection.
    414   1.1      cgd 	 * Reset idle time and keep-alive timer.
    415   1.1      cgd 	 */
    416   1.1      cgd 	tp->t_idle = 0;
    417   1.1      cgd 	tp->t_timer[TCPT_KEEP] = tcp_keepidle;
    418   1.1      cgd 
    419   1.1      cgd 	/*
    420   1.1      cgd 	 * Process options if not in LISTEN state,
    421   1.1      cgd 	 * else do it below (after getting remote address).
    422   1.1      cgd 	 */
    423   1.9  mycroft 	if (optp && tp->t_state != TCPS_LISTEN)
    424   1.9  mycroft 		tcp_dooptions(tp, optp, optlen, ti,
    425   1.9  mycroft 			&ts_present, &ts_val, &ts_ecr);
    426   1.9  mycroft 
    427   1.9  mycroft 	/*
    428   1.1      cgd 	 * Header prediction: check for the two common cases
    429   1.1      cgd 	 * of a uni-directional data xfer.  If the packet has
    430   1.1      cgd 	 * no control flags, is in-sequence, the window didn't
    431   1.1      cgd 	 * change and we're not retransmitting, it's a
    432   1.1      cgd 	 * candidate.  If the length is zero and the ack moved
    433   1.1      cgd 	 * forward, we're the sender side of the xfer.  Just
    434   1.1      cgd 	 * free the data acked & wake any higher level process
    435   1.1      cgd 	 * that was blocked waiting for space.  If the length
    436   1.1      cgd 	 * is non-zero and the ack didn't move, we're the
    437   1.1      cgd 	 * receiver side.  If we're getting packets in-order
    438   1.1      cgd 	 * (the reassembly queue is empty), add the data to
    439   1.1      cgd 	 * the socket buffer and note that we need a delayed ack.
    440   1.1      cgd 	 */
    441   1.1      cgd 	if (tp->t_state == TCPS_ESTABLISHED &&
    442   1.1      cgd 	    (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
    443   1.9  mycroft 	    (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) &&
    444   1.1      cgd 	    ti->ti_seq == tp->rcv_nxt &&
    445   1.9  mycroft 	    tiwin && tiwin == tp->snd_wnd &&
    446   1.1      cgd 	    tp->snd_nxt == tp->snd_max) {
    447   1.9  mycroft 
    448   1.9  mycroft 		/*
    449   1.9  mycroft 		 * If last ACK falls within this segment's sequence numbers,
    450   1.9  mycroft 		 *  record the timestamp.
    451   1.9  mycroft 		 */
    452   1.9  mycroft 		if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
    453   1.9  mycroft 		   SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len)) {
    454   1.9  mycroft 			tp->ts_recent_age = tcp_now;
    455   1.9  mycroft 			tp->ts_recent = ts_val;
    456   1.9  mycroft 		}
    457   1.9  mycroft 
    458   1.1      cgd 		if (ti->ti_len == 0) {
    459   1.1      cgd 			if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
    460   1.1      cgd 			    SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
    461  1.15  mycroft 			    tp->snd_cwnd >= tp->snd_wnd &&
    462  1.15  mycroft 			    tp->t_dupacks < tcprexmtthresh) {
    463   1.1      cgd 				/*
    464   1.1      cgd 				 * this is a pure ack for outstanding data.
    465   1.1      cgd 				 */
    466   1.9  mycroft 				++tcpstat.tcps_predack;
    467   1.9  mycroft 				if (ts_present)
    468   1.9  mycroft 					tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
    469   1.9  mycroft 				else if (tp->t_rtt &&
    470   1.9  mycroft 					    SEQ_GT(ti->ti_ack, tp->t_rtseq))
    471   1.9  mycroft 					tcp_xmit_timer(tp, tp->t_rtt);
    472   1.1      cgd 				acked = ti->ti_ack - tp->snd_una;
    473   1.1      cgd 				tcpstat.tcps_rcvackpack++;
    474   1.1      cgd 				tcpstat.tcps_rcvackbyte += acked;
    475   1.1      cgd 				sbdrop(&so->so_snd, acked);
    476   1.1      cgd 				tp->snd_una = ti->ti_ack;
    477   1.1      cgd 				m_freem(m);
    478   1.1      cgd 
    479   1.1      cgd 				/*
    480   1.1      cgd 				 * If all outstanding data are acked, stop
    481   1.1      cgd 				 * retransmit timer, otherwise restart timer
    482   1.1      cgd 				 * using current (possibly backed-off) value.
    483   1.1      cgd 				 * If process is waiting for space,
    484   1.1      cgd 				 * wakeup/selwakeup/signal.  If data
    485   1.1      cgd 				 * are ready to send, let tcp_output
    486   1.1      cgd 				 * decide between more output or persist.
    487   1.1      cgd 				 */
    488   1.1      cgd 				if (tp->snd_una == tp->snd_max)
    489   1.1      cgd 					tp->t_timer[TCPT_REXMT] = 0;
    490   1.1      cgd 				else if (tp->t_timer[TCPT_PERSIST] == 0)
    491   1.1      cgd 					tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
    492   1.1      cgd 
    493  1.19  mycroft 				if (sb_notify(&so->so_snd))
    494   1.1      cgd 					sowwakeup(so);
    495   1.1      cgd 				if (so->so_snd.sb_cc)
    496   1.1      cgd 					(void) tcp_output(tp);
    497   1.1      cgd 				return;
    498   1.1      cgd 			}
    499   1.1      cgd 		} else if (ti->ti_ack == tp->snd_una &&
    500  1.20      cgd 		    tp->segq.lh_first == NULL &&
    501   1.1      cgd 		    ti->ti_len <= sbspace(&so->so_rcv)) {
    502   1.1      cgd 			/*
    503   1.1      cgd 			 * this is a pure, in-sequence data packet
    504   1.1      cgd 			 * with nothing on the reassembly queue and
    505   1.1      cgd 			 * we have enough buffer space to take it.
    506   1.1      cgd 			 */
    507   1.9  mycroft 			++tcpstat.tcps_preddat;
    508   1.1      cgd 			tp->rcv_nxt += ti->ti_len;
    509   1.1      cgd 			tcpstat.tcps_rcvpack++;
    510   1.1      cgd 			tcpstat.tcps_rcvbyte += ti->ti_len;
    511   1.1      cgd 			/*
    512   1.9  mycroft 			 * Drop TCP, IP headers and TCP options then add data
    513   1.9  mycroft 			 * to socket buffer.
    514   1.1      cgd 			 */
    515   1.9  mycroft 			m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
    516   1.9  mycroft 			m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
    517   1.1      cgd 			sbappend(&so->so_rcv, m);
    518   1.1      cgd 			sorwakeup(so);
    519   1.8  mycroft 			if (ti->ti_flags & TH_PUSH)
    520   1.8  mycroft 				tp->t_flags |= TF_ACKNOW;
    521   1.8  mycroft 			else
    522   1.8  mycroft 				tp->t_flags |= TF_DELACK;
    523   1.1      cgd 			return;
    524   1.1      cgd 		}
    525   1.1      cgd 	}
    526   1.1      cgd 
    527   1.1      cgd 	/*
    528   1.9  mycroft 	 * Drop TCP, IP headers and TCP options.
    529   1.1      cgd 	 */
    530   1.9  mycroft 	m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
    531   1.9  mycroft 	m->m_len  -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
    532   1.1      cgd 
    533   1.1      cgd 	/*
    534   1.1      cgd 	 * Calculate amount of space in receive window,
    535   1.1      cgd 	 * and then do TCP input processing.
    536   1.1      cgd 	 * Receive window is amount of space in rcv queue,
    537   1.1      cgd 	 * but not less than advertised window.
    538   1.1      cgd 	 */
    539   1.1      cgd 	{ int win;
    540   1.1      cgd 
    541   1.1      cgd 	win = sbspace(&so->so_rcv);
    542   1.1      cgd 	if (win < 0)
    543   1.1      cgd 		win = 0;
    544   1.1      cgd 	tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt));
    545   1.1      cgd 	}
    546   1.1      cgd 
    547   1.1      cgd 	switch (tp->t_state) {
    548   1.1      cgd 
    549   1.1      cgd 	/*
    550   1.1      cgd 	 * If the state is LISTEN then ignore segment if it contains an RST.
    551   1.1      cgd 	 * If the segment contains an ACK then it is bad and send a RST.
    552   1.1      cgd 	 * If it does not contain a SYN then it is not interesting; drop it.
    553   1.1      cgd 	 * Don't bother responding if the destination was a broadcast.
    554   1.1      cgd 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
    555   1.1      cgd 	 * tp->iss, and send a segment:
    556   1.1      cgd 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
    557   1.1      cgd 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
    558   1.1      cgd 	 * Fill in remote peer address fields if not previously specified.
    559   1.1      cgd 	 * Enter SYN_RECEIVED state, and process any other fields of this
    560   1.1      cgd 	 * segment in this state.
    561   1.1      cgd 	 */
    562   1.1      cgd 	case TCPS_LISTEN: {
    563   1.1      cgd 		struct mbuf *am;
    564   1.1      cgd 		register struct sockaddr_in *sin;
    565   1.1      cgd 
    566   1.1      cgd 		if (tiflags & TH_RST)
    567   1.1      cgd 			goto drop;
    568   1.1      cgd 		if (tiflags & TH_ACK)
    569   1.1      cgd 			goto dropwithreset;
    570   1.1      cgd 		if ((tiflags & TH_SYN) == 0)
    571   1.1      cgd 			goto drop;
    572   1.9  mycroft 		/*
    573   1.9  mycroft 		 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
    574   1.9  mycroft 		 * in_broadcast() should never return true on a received
    575   1.9  mycroft 		 * packet with M_BCAST not set.
    576   1.9  mycroft 		 */
    577   1.9  mycroft 		if (m->m_flags & (M_BCAST|M_MCAST) ||
    578  1.13  mycroft 		    IN_MULTICAST(ti->ti_dst.s_addr))
    579   1.1      cgd 			goto drop;
    580   1.1      cgd 		am = m_get(M_DONTWAIT, MT_SONAME);	/* XXX */
    581   1.1      cgd 		if (am == NULL)
    582   1.1      cgd 			goto drop;
    583   1.1      cgd 		am->m_len = sizeof (struct sockaddr_in);
    584   1.1      cgd 		sin = mtod(am, struct sockaddr_in *);
    585   1.1      cgd 		sin->sin_family = AF_INET;
    586   1.1      cgd 		sin->sin_len = sizeof(*sin);
    587   1.1      cgd 		sin->sin_addr = ti->ti_src;
    588   1.1      cgd 		sin->sin_port = ti->ti_sport;
    589   1.9  mycroft 		bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
    590   1.1      cgd 		laddr = inp->inp_laddr;
    591   1.1      cgd 		if (inp->inp_laddr.s_addr == INADDR_ANY)
    592   1.1      cgd 			inp->inp_laddr = ti->ti_dst;
    593   1.1      cgd 		if (in_pcbconnect(inp, am)) {
    594   1.1      cgd 			inp->inp_laddr = laddr;
    595   1.1      cgd 			(void) m_free(am);
    596   1.1      cgd 			goto drop;
    597   1.1      cgd 		}
    598   1.1      cgd 		(void) m_free(am);
    599   1.1      cgd 		tp->t_template = tcp_template(tp);
    600   1.1      cgd 		if (tp->t_template == 0) {
    601   1.1      cgd 			tp = tcp_drop(tp, ENOBUFS);
    602   1.1      cgd 			dropsocket = 0;		/* socket is already gone */
    603   1.1      cgd 			goto drop;
    604   1.1      cgd 		}
    605   1.9  mycroft 		if (optp)
    606   1.9  mycroft 			tcp_dooptions(tp, optp, optlen, ti,
    607   1.9  mycroft 				&ts_present, &ts_val, &ts_ecr);
    608   1.1      cgd 		if (iss)
    609   1.1      cgd 			tp->iss = iss;
    610   1.1      cgd 		else
    611   1.1      cgd 			tp->iss = tcp_iss;
    612   1.1      cgd 		tcp_iss += TCP_ISSINCR/2;
    613   1.1      cgd 		tp->irs = ti->ti_seq;
    614   1.1      cgd 		tcp_sendseqinit(tp);
    615   1.1      cgd 		tcp_rcvseqinit(tp);
    616   1.1      cgd 		tp->t_flags |= TF_ACKNOW;
    617   1.1      cgd 		tp->t_state = TCPS_SYN_RECEIVED;
    618   1.1      cgd 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
    619   1.1      cgd 		dropsocket = 0;		/* committed to socket */
    620   1.1      cgd 		tcpstat.tcps_accepts++;
    621   1.1      cgd 		goto trimthenstep6;
    622   1.1      cgd 		}
    623   1.1      cgd 
    624   1.1      cgd 	/*
    625   1.1      cgd 	 * If the state is SYN_SENT:
    626   1.1      cgd 	 *	if seg contains an ACK, but not for our SYN, drop the input.
    627   1.1      cgd 	 *	if seg contains a RST, then drop the connection.
    628   1.1      cgd 	 *	if seg does not contain SYN, then drop it.
    629   1.1      cgd 	 * Otherwise this is an acceptable SYN segment
    630   1.1      cgd 	 *	initialize tp->rcv_nxt and tp->irs
    631   1.1      cgd 	 *	if seg contains ack then advance tp->snd_una
    632   1.1      cgd 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
    633   1.1      cgd 	 *	arrange for segment to be acked (eventually)
    634   1.1      cgd 	 *	continue processing rest of data/controls, beginning with URG
    635   1.1      cgd 	 */
    636   1.1      cgd 	case TCPS_SYN_SENT:
    637   1.1      cgd 		if ((tiflags & TH_ACK) &&
    638   1.1      cgd 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
    639   1.1      cgd 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
    640   1.1      cgd 			goto dropwithreset;
    641   1.1      cgd 		if (tiflags & TH_RST) {
    642   1.1      cgd 			if (tiflags & TH_ACK)
    643   1.1      cgd 				tp = tcp_drop(tp, ECONNREFUSED);
    644   1.1      cgd 			goto drop;
    645   1.1      cgd 		}
    646   1.1      cgd 		if ((tiflags & TH_SYN) == 0)
    647   1.1      cgd 			goto drop;
    648   1.1      cgd 		if (tiflags & TH_ACK) {
    649   1.1      cgd 			tp->snd_una = ti->ti_ack;
    650   1.1      cgd 			if (SEQ_LT(tp->snd_nxt, tp->snd_una))
    651   1.1      cgd 				tp->snd_nxt = tp->snd_una;
    652   1.1      cgd 		}
    653   1.1      cgd 		tp->t_timer[TCPT_REXMT] = 0;
    654   1.1      cgd 		tp->irs = ti->ti_seq;
    655   1.1      cgd 		tcp_rcvseqinit(tp);
    656   1.1      cgd 		tp->t_flags |= TF_ACKNOW;
    657   1.1      cgd 		if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
    658   1.1      cgd 			tcpstat.tcps_connects++;
    659   1.1      cgd 			soisconnected(so);
    660   1.1      cgd 			tp->t_state = TCPS_ESTABLISHED;
    661   1.9  mycroft 			/* Do window scaling on this connection? */
    662   1.9  mycroft 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
    663   1.9  mycroft 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
    664   1.9  mycroft 				tp->snd_scale = tp->requested_s_scale;
    665   1.9  mycroft 				tp->rcv_scale = tp->request_r_scale;
    666   1.9  mycroft 			}
    667   1.1      cgd 			(void) tcp_reass(tp, (struct tcpiphdr *)0,
    668   1.1      cgd 				(struct mbuf *)0);
    669   1.1      cgd 			/*
    670   1.1      cgd 			 * if we didn't have to retransmit the SYN,
    671   1.1      cgd 			 * use its rtt as our initial srtt & rtt var.
    672   1.1      cgd 			 */
    673   1.1      cgd 			if (tp->t_rtt)
    674   1.9  mycroft 				tcp_xmit_timer(tp, tp->t_rtt);
    675   1.1      cgd 		} else
    676   1.1      cgd 			tp->t_state = TCPS_SYN_RECEIVED;
    677   1.1      cgd 
    678   1.1      cgd trimthenstep6:
    679   1.1      cgd 		/*
    680   1.1      cgd 		 * Advance ti->ti_seq to correspond to first data byte.
    681   1.1      cgd 		 * If data, trim to stay within window,
    682   1.1      cgd 		 * dropping FIN if necessary.
    683   1.1      cgd 		 */
    684   1.1      cgd 		ti->ti_seq++;
    685   1.1      cgd 		if (ti->ti_len > tp->rcv_wnd) {
    686   1.1      cgd 			todrop = ti->ti_len - tp->rcv_wnd;
    687   1.1      cgd 			m_adj(m, -todrop);
    688   1.1      cgd 			ti->ti_len = tp->rcv_wnd;
    689   1.1      cgd 			tiflags &= ~TH_FIN;
    690   1.1      cgd 			tcpstat.tcps_rcvpackafterwin++;
    691   1.1      cgd 			tcpstat.tcps_rcvbyteafterwin += todrop;
    692   1.1      cgd 		}
    693   1.1      cgd 		tp->snd_wl1 = ti->ti_seq - 1;
    694   1.1      cgd 		tp->rcv_up = ti->ti_seq;
    695   1.1      cgd 		goto step6;
    696   1.1      cgd 	}
    697   1.1      cgd 
    698   1.1      cgd 	/*
    699   1.1      cgd 	 * States other than LISTEN or SYN_SENT.
    700   1.9  mycroft 	 * First check timestamp, if present.
    701   1.9  mycroft 	 * Then check that at least some bytes of segment are within
    702   1.1      cgd 	 * receive window.  If segment begins before rcv_nxt,
    703   1.1      cgd 	 * drop leading data (and SYN); if nothing left, just ack.
    704   1.9  mycroft 	 *
    705   1.9  mycroft 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
    706   1.9  mycroft 	 * and it's less than ts_recent, drop it.
    707   1.1      cgd 	 */
    708   1.9  mycroft 	if (ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
    709   1.9  mycroft 	    TSTMP_LT(ts_val, tp->ts_recent)) {
    710   1.9  mycroft 
    711   1.9  mycroft 		/* Check to see if ts_recent is over 24 days old.  */
    712   1.9  mycroft 		if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
    713   1.9  mycroft 			/*
    714   1.9  mycroft 			 * Invalidate ts_recent.  If this segment updates
    715   1.9  mycroft 			 * ts_recent, the age will be reset later and ts_recent
    716   1.9  mycroft 			 * will get a valid value.  If it does not, setting
    717   1.9  mycroft 			 * ts_recent to zero will at least satisfy the
    718   1.9  mycroft 			 * requirement that zero be placed in the timestamp
    719   1.9  mycroft 			 * echo reply when ts_recent isn't valid.  The
    720   1.9  mycroft 			 * age isn't reset until we get a valid ts_recent
    721   1.9  mycroft 			 * because we don't want out-of-order segments to be
    722   1.9  mycroft 			 * dropped when ts_recent is old.
    723   1.9  mycroft 			 */
    724   1.9  mycroft 			tp->ts_recent = 0;
    725   1.9  mycroft 		} else {
    726   1.9  mycroft 			tcpstat.tcps_rcvduppack++;
    727   1.9  mycroft 			tcpstat.tcps_rcvdupbyte += ti->ti_len;
    728   1.9  mycroft 			tcpstat.tcps_pawsdrop++;
    729   1.9  mycroft 			goto dropafterack;
    730   1.9  mycroft 		}
    731   1.9  mycroft 	}
    732   1.9  mycroft 
    733   1.1      cgd 	todrop = tp->rcv_nxt - ti->ti_seq;
    734   1.1      cgd 	if (todrop > 0) {
    735   1.1      cgd 		if (tiflags & TH_SYN) {
    736   1.1      cgd 			tiflags &= ~TH_SYN;
    737   1.1      cgd 			ti->ti_seq++;
    738   1.9  mycroft 			if (ti->ti_urp > 1)
    739   1.1      cgd 				ti->ti_urp--;
    740   1.1      cgd 			else
    741   1.1      cgd 				tiflags &= ~TH_URG;
    742   1.1      cgd 			todrop--;
    743   1.1      cgd 		}
    744   1.9  mycroft 		if (todrop >= ti->ti_len) {
    745   1.1      cgd 			/*
    746   1.7  mycroft 			 * Any valid FIN must be to the left of the
    747   1.7  mycroft 			 * window.  At this point, FIN must be a
    748   1.7  mycroft 			 * duplicate or out-of-sequence, so drop it.
    749   1.7  mycroft 			 */
    750   1.7  mycroft 			tiflags &= ~TH_FIN;
    751   1.7  mycroft 			/*
    752   1.7  mycroft 			 * Send ACK to resynchronize, and drop any data,
    753   1.1      cgd 			 * but keep on processing for RST or ACK.
    754   1.1      cgd 			 */
    755   1.7  mycroft 			tp->t_flags |= TF_ACKNOW;
    756   1.7  mycroft 			tcpstat.tcps_rcvdupbyte += todrop = ti->ti_len;
    757   1.7  mycroft 			tcpstat.tcps_rcvduppack++;
    758   1.1      cgd 		} else {
    759   1.1      cgd 			tcpstat.tcps_rcvpartduppack++;
    760   1.1      cgd 			tcpstat.tcps_rcvpartdupbyte += todrop;
    761   1.1      cgd 		}
    762   1.1      cgd 		m_adj(m, todrop);
    763   1.1      cgd 		ti->ti_seq += todrop;
    764   1.1      cgd 		ti->ti_len -= todrop;
    765   1.1      cgd 		if (ti->ti_urp > todrop)
    766   1.1      cgd 			ti->ti_urp -= todrop;
    767   1.1      cgd 		else {
    768   1.1      cgd 			tiflags &= ~TH_URG;
    769   1.1      cgd 			ti->ti_urp = 0;
    770   1.1      cgd 		}
    771   1.1      cgd 	}
    772   1.1      cgd 
    773   1.1      cgd 	/*
    774   1.1      cgd 	 * If new data are received on a connection after the
    775   1.1      cgd 	 * user processes are gone, then RST the other end.
    776   1.1      cgd 	 */
    777   1.1      cgd 	if ((so->so_state & SS_NOFDREF) &&
    778   1.1      cgd 	    tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
    779   1.1      cgd 		tp = tcp_close(tp);
    780   1.1      cgd 		tcpstat.tcps_rcvafterclose++;
    781   1.1      cgd 		goto dropwithreset;
    782   1.1      cgd 	}
    783   1.1      cgd 
    784   1.1      cgd 	/*
    785   1.1      cgd 	 * If segment ends after window, drop trailing data
    786   1.1      cgd 	 * (and PUSH and FIN); if nothing left, just ACK.
    787   1.1      cgd 	 */
    788   1.1      cgd 	todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
    789   1.1      cgd 	if (todrop > 0) {
    790   1.1      cgd 		tcpstat.tcps_rcvpackafterwin++;
    791   1.1      cgd 		if (todrop >= ti->ti_len) {
    792   1.1      cgd 			tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
    793   1.1      cgd 			/*
    794   1.1      cgd 			 * If a new connection request is received
    795   1.1      cgd 			 * while in TIME_WAIT, drop the old connection
    796   1.1      cgd 			 * and start over if the sequence numbers
    797   1.1      cgd 			 * are above the previous ones.
    798   1.1      cgd 			 */
    799   1.1      cgd 			if (tiflags & TH_SYN &&
    800   1.1      cgd 			    tp->t_state == TCPS_TIME_WAIT &&
    801   1.1      cgd 			    SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
    802   1.1      cgd 				iss = tp->rcv_nxt + TCP_ISSINCR;
    803   1.1      cgd 				tp = tcp_close(tp);
    804   1.1      cgd 				goto findpcb;
    805   1.1      cgd 			}
    806   1.1      cgd 			/*
    807   1.1      cgd 			 * If window is closed can only take segments at
    808   1.1      cgd 			 * window edge, and have to drop data and PUSH from
    809   1.1      cgd 			 * incoming segments.  Continue processing, but
    810   1.1      cgd 			 * remember to ack.  Otherwise, drop segment
    811   1.1      cgd 			 * and ack.
    812   1.1      cgd 			 */
    813   1.1      cgd 			if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
    814   1.1      cgd 				tp->t_flags |= TF_ACKNOW;
    815   1.1      cgd 				tcpstat.tcps_rcvwinprobe++;
    816   1.1      cgd 			} else
    817   1.1      cgd 				goto dropafterack;
    818   1.1      cgd 		} else
    819   1.1      cgd 			tcpstat.tcps_rcvbyteafterwin += todrop;
    820   1.1      cgd 		m_adj(m, -todrop);
    821   1.1      cgd 		ti->ti_len -= todrop;
    822   1.1      cgd 		tiflags &= ~(TH_PUSH|TH_FIN);
    823   1.1      cgd 	}
    824   1.1      cgd 
    825   1.1      cgd 	/*
    826   1.9  mycroft 	 * If last ACK falls within this segment's sequence numbers,
    827   1.9  mycroft 	 * record its timestamp.
    828   1.9  mycroft 	 */
    829   1.9  mycroft 	if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
    830   1.9  mycroft 	    SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len +
    831   1.9  mycroft 		   ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
    832   1.9  mycroft 		tp->ts_recent_age = tcp_now;
    833   1.9  mycroft 		tp->ts_recent = ts_val;
    834   1.9  mycroft 	}
    835   1.9  mycroft 
    836   1.9  mycroft 	/*
    837   1.1      cgd 	 * If the RST bit is set examine the state:
    838   1.1      cgd 	 *    SYN_RECEIVED STATE:
    839   1.1      cgd 	 *	If passive open, return to LISTEN state.
    840   1.1      cgd 	 *	If active open, inform user that connection was refused.
    841   1.1      cgd 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
    842   1.1      cgd 	 *	Inform user that connection was reset, and close tcb.
    843   1.1      cgd 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
    844   1.1      cgd 	 *	Close the tcb.
    845   1.1      cgd 	 */
    846   1.1      cgd 	if (tiflags&TH_RST) switch (tp->t_state) {
    847   1.1      cgd 
    848   1.1      cgd 	case TCPS_SYN_RECEIVED:
    849   1.1      cgd 		so->so_error = ECONNREFUSED;
    850   1.1      cgd 		goto close;
    851   1.1      cgd 
    852   1.1      cgd 	case TCPS_ESTABLISHED:
    853   1.1      cgd 	case TCPS_FIN_WAIT_1:
    854   1.1      cgd 	case TCPS_FIN_WAIT_2:
    855   1.1      cgd 	case TCPS_CLOSE_WAIT:
    856   1.1      cgd 		so->so_error = ECONNRESET;
    857   1.1      cgd 	close:
    858   1.1      cgd 		tp->t_state = TCPS_CLOSED;
    859   1.1      cgd 		tcpstat.tcps_drops++;
    860   1.1      cgd 		tp = tcp_close(tp);
    861   1.1      cgd 		goto drop;
    862   1.1      cgd 
    863   1.1      cgd 	case TCPS_CLOSING:
    864   1.1      cgd 	case TCPS_LAST_ACK:
    865   1.1      cgd 	case TCPS_TIME_WAIT:
    866   1.1      cgd 		tp = tcp_close(tp);
    867   1.1      cgd 		goto drop;
    868   1.1      cgd 	}
    869   1.1      cgd 
    870   1.1      cgd 	/*
    871   1.1      cgd 	 * If a SYN is in the window, then this is an
    872   1.1      cgd 	 * error and we send an RST and drop the connection.
    873   1.1      cgd 	 */
    874   1.1      cgd 	if (tiflags & TH_SYN) {
    875   1.1      cgd 		tp = tcp_drop(tp, ECONNRESET);
    876   1.1      cgd 		goto dropwithreset;
    877   1.1      cgd 	}
    878   1.1      cgd 
    879   1.1      cgd 	/*
    880   1.1      cgd 	 * If the ACK bit is off we drop the segment and return.
    881   1.1      cgd 	 */
    882   1.1      cgd 	if ((tiflags & TH_ACK) == 0)
    883   1.1      cgd 		goto drop;
    884   1.9  mycroft 
    885   1.1      cgd 	/*
    886   1.1      cgd 	 * Ack processing.
    887   1.1      cgd 	 */
    888   1.1      cgd 	switch (tp->t_state) {
    889   1.1      cgd 
    890   1.1      cgd 	/*
    891   1.1      cgd 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
    892   1.1      cgd 	 * ESTABLISHED state and continue processing, otherwise
    893   1.1      cgd 	 * send an RST.
    894   1.1      cgd 	 */
    895   1.1      cgd 	case TCPS_SYN_RECEIVED:
    896   1.1      cgd 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
    897   1.1      cgd 		    SEQ_GT(ti->ti_ack, tp->snd_max))
    898   1.1      cgd 			goto dropwithreset;
    899   1.1      cgd 		tcpstat.tcps_connects++;
    900   1.1      cgd 		soisconnected(so);
    901   1.1      cgd 		tp->t_state = TCPS_ESTABLISHED;
    902   1.9  mycroft 		/* Do window scaling? */
    903   1.9  mycroft 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
    904   1.9  mycroft 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
    905   1.9  mycroft 			tp->snd_scale = tp->requested_s_scale;
    906   1.9  mycroft 			tp->rcv_scale = tp->request_r_scale;
    907   1.9  mycroft 		}
    908   1.1      cgd 		(void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
    909   1.1      cgd 		tp->snd_wl1 = ti->ti_seq - 1;
    910   1.1      cgd 		/* fall into ... */
    911   1.1      cgd 
    912   1.1      cgd 	/*
    913   1.1      cgd 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
    914   1.1      cgd 	 * ACKs.  If the ack is in the range
    915   1.1      cgd 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
    916   1.1      cgd 	 * then advance tp->snd_una to ti->ti_ack and drop
    917   1.1      cgd 	 * data from the retransmission queue.  If this ACK reflects
    918   1.1      cgd 	 * more up to date window information we update our window information.
    919   1.1      cgd 	 */
    920   1.1      cgd 	case TCPS_ESTABLISHED:
    921   1.1      cgd 	case TCPS_FIN_WAIT_1:
    922   1.1      cgd 	case TCPS_FIN_WAIT_2:
    923   1.1      cgd 	case TCPS_CLOSE_WAIT:
    924   1.1      cgd 	case TCPS_CLOSING:
    925   1.1      cgd 	case TCPS_LAST_ACK:
    926   1.1      cgd 	case TCPS_TIME_WAIT:
    927   1.1      cgd 
    928   1.1      cgd 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
    929   1.9  mycroft 			if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
    930   1.1      cgd 				tcpstat.tcps_rcvdupack++;
    931   1.1      cgd 				/*
    932   1.1      cgd 				 * If we have outstanding data (other than
    933   1.1      cgd 				 * a window probe), this is a completely
    934   1.1      cgd 				 * duplicate ack (ie, window info didn't
    935   1.1      cgd 				 * change), the ack is the biggest we've
    936   1.1      cgd 				 * seen and we've seen exactly our rexmt
    937   1.1      cgd 				 * threshhold of them, assume a packet
    938   1.1      cgd 				 * has been dropped and retransmit it.
    939   1.1      cgd 				 * Kludge snd_nxt & the congestion
    940   1.1      cgd 				 * window so we send only this one
    941   1.1      cgd 				 * packet.
    942   1.1      cgd 				 *
    943   1.1      cgd 				 * We know we're losing at the current
    944   1.1      cgd 				 * window size so do congestion avoidance
    945   1.1      cgd 				 * (set ssthresh to half the current window
    946   1.1      cgd 				 * and pull our congestion window back to
    947   1.1      cgd 				 * the new ssthresh).
    948   1.1      cgd 				 *
    949   1.1      cgd 				 * Dup acks mean that packets have left the
    950   1.9  mycroft 				 * network (they're now cached at the receiver)
    951   1.1      cgd 				 * so bump cwnd by the amount in the receiver
    952   1.1      cgd 				 * to keep a constant cwnd packets in the
    953   1.1      cgd 				 * network.
    954   1.1      cgd 				 */
    955   1.1      cgd 				if (tp->t_timer[TCPT_REXMT] == 0 ||
    956   1.1      cgd 				    ti->ti_ack != tp->snd_una)
    957   1.1      cgd 					tp->t_dupacks = 0;
    958   1.1      cgd 				else if (++tp->t_dupacks == tcprexmtthresh) {
    959   1.1      cgd 					tcp_seq onxt = tp->snd_nxt;
    960   1.1      cgd 					u_int win =
    961   1.1      cgd 					    min(tp->snd_wnd, tp->snd_cwnd) / 2 /
    962   1.1      cgd 						tp->t_maxseg;
    963   1.1      cgd 
    964   1.1      cgd 					if (win < 2)
    965   1.1      cgd 						win = 2;
    966   1.1      cgd 					tp->snd_ssthresh = win * tp->t_maxseg;
    967   1.1      cgd 					tp->t_timer[TCPT_REXMT] = 0;
    968   1.1      cgd 					tp->t_rtt = 0;
    969   1.1      cgd 					tp->snd_nxt = ti->ti_ack;
    970   1.1      cgd 					tp->snd_cwnd = tp->t_maxseg;
    971   1.1      cgd 					(void) tcp_output(tp);
    972   1.1      cgd 					tp->snd_cwnd = tp->snd_ssthresh +
    973   1.1      cgd 					       tp->t_maxseg * tp->t_dupacks;
    974   1.1      cgd 					if (SEQ_GT(onxt, tp->snd_nxt))
    975   1.1      cgd 						tp->snd_nxt = onxt;
    976   1.1      cgd 					goto drop;
    977   1.1      cgd 				} else if (tp->t_dupacks > tcprexmtthresh) {
    978   1.1      cgd 					tp->snd_cwnd += tp->t_maxseg;
    979   1.1      cgd 					(void) tcp_output(tp);
    980   1.1      cgd 					goto drop;
    981   1.1      cgd 				}
    982   1.1      cgd 			} else
    983   1.1      cgd 				tp->t_dupacks = 0;
    984   1.1      cgd 			break;
    985   1.1      cgd 		}
    986   1.1      cgd 		/*
    987   1.1      cgd 		 * If the congestion window was inflated to account
    988   1.1      cgd 		 * for the other side's cached packets, retract it.
    989   1.1      cgd 		 */
    990  1.16  mycroft 		if (tp->t_dupacks >= tcprexmtthresh &&
    991   1.1      cgd 		    tp->snd_cwnd > tp->snd_ssthresh)
    992   1.1      cgd 			tp->snd_cwnd = tp->snd_ssthresh;
    993   1.1      cgd 		tp->t_dupacks = 0;
    994   1.1      cgd 		if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
    995   1.1      cgd 			tcpstat.tcps_rcvacktoomuch++;
    996   1.1      cgd 			goto dropafterack;
    997   1.1      cgd 		}
    998   1.1      cgd 		acked = ti->ti_ack - tp->snd_una;
    999   1.1      cgd 		tcpstat.tcps_rcvackpack++;
   1000   1.1      cgd 		tcpstat.tcps_rcvackbyte += acked;
   1001   1.1      cgd 
   1002   1.1      cgd 		/*
   1003   1.9  mycroft 		 * If we have a timestamp reply, update smoothed
   1004   1.9  mycroft 		 * round trip time.  If no timestamp is present but
   1005   1.9  mycroft 		 * transmit timer is running and timed sequence
   1006   1.1      cgd 		 * number was acked, update smoothed round trip time.
   1007   1.1      cgd 		 * Since we now have an rtt measurement, cancel the
   1008   1.1      cgd 		 * timer backoff (cf., Phil Karn's retransmit alg.).
   1009   1.1      cgd 		 * Recompute the initial retransmit timer.
   1010   1.1      cgd 		 */
   1011   1.9  mycroft 		if (ts_present)
   1012   1.9  mycroft 			tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
   1013   1.9  mycroft 		else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
   1014   1.9  mycroft 			tcp_xmit_timer(tp,tp->t_rtt);
   1015   1.1      cgd 
   1016   1.1      cgd 		/*
   1017   1.1      cgd 		 * If all outstanding data is acked, stop retransmit
   1018   1.1      cgd 		 * timer and remember to restart (more output or persist).
   1019   1.1      cgd 		 * If there is more data to be acked, restart retransmit
   1020   1.1      cgd 		 * timer, using current (possibly backed-off) value.
   1021   1.1      cgd 		 */
   1022   1.1      cgd 		if (ti->ti_ack == tp->snd_max) {
   1023   1.1      cgd 			tp->t_timer[TCPT_REXMT] = 0;
   1024   1.1      cgd 			needoutput = 1;
   1025   1.1      cgd 		} else if (tp->t_timer[TCPT_PERSIST] == 0)
   1026   1.1      cgd 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
   1027   1.1      cgd 		/*
   1028   1.1      cgd 		 * When new data is acked, open the congestion window.
   1029   1.1      cgd 		 * If the window gives us less than ssthresh packets
   1030   1.1      cgd 		 * in flight, open exponentially (maxseg per packet).
   1031   1.1      cgd 		 * Otherwise open linearly: maxseg per window
   1032   1.1      cgd 		 * (maxseg^2 / cwnd per packet), plus a constant
   1033   1.1      cgd 		 * fraction of a packet (maxseg/8) to help larger windows
   1034   1.1      cgd 		 * open quickly enough.
   1035   1.1      cgd 		 */
   1036   1.1      cgd 		{
   1037   1.1      cgd 		register u_int cw = tp->snd_cwnd;
   1038   1.1      cgd 		register u_int incr = tp->t_maxseg;
   1039   1.1      cgd 
   1040   1.1      cgd 		if (cw > tp->snd_ssthresh)
   1041  1.16  mycroft 			incr = incr * incr / cw;
   1042   1.9  mycroft 		tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
   1043   1.1      cgd 		}
   1044   1.1      cgd 		if (acked > so->so_snd.sb_cc) {
   1045   1.1      cgd 			tp->snd_wnd -= so->so_snd.sb_cc;
   1046   1.1      cgd 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
   1047   1.1      cgd 			ourfinisacked = 1;
   1048   1.1      cgd 		} else {
   1049   1.1      cgd 			sbdrop(&so->so_snd, acked);
   1050   1.1      cgd 			tp->snd_wnd -= acked;
   1051   1.1      cgd 			ourfinisacked = 0;
   1052   1.1      cgd 		}
   1053  1.19  mycroft 		if (sb_notify(&so->so_snd))
   1054   1.1      cgd 			sowwakeup(so);
   1055   1.1      cgd 		tp->snd_una = ti->ti_ack;
   1056   1.1      cgd 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
   1057   1.1      cgd 			tp->snd_nxt = tp->snd_una;
   1058   1.1      cgd 
   1059   1.1      cgd 		switch (tp->t_state) {
   1060   1.1      cgd 
   1061   1.1      cgd 		/*
   1062   1.1      cgd 		 * In FIN_WAIT_1 STATE in addition to the processing
   1063   1.1      cgd 		 * for the ESTABLISHED state if our FIN is now acknowledged
   1064   1.1      cgd 		 * then enter FIN_WAIT_2.
   1065   1.1      cgd 		 */
   1066   1.1      cgd 		case TCPS_FIN_WAIT_1:
   1067   1.1      cgd 			if (ourfinisacked) {
   1068   1.1      cgd 				/*
   1069   1.1      cgd 				 * If we can't receive any more
   1070   1.1      cgd 				 * data, then closing user can proceed.
   1071   1.1      cgd 				 * Starting the timer is contrary to the
   1072   1.1      cgd 				 * specification, but if we don't get a FIN
   1073   1.1      cgd 				 * we'll hang forever.
   1074   1.1      cgd 				 */
   1075   1.1      cgd 				if (so->so_state & SS_CANTRCVMORE) {
   1076   1.1      cgd 					soisdisconnected(so);
   1077   1.1      cgd 					tp->t_timer[TCPT_2MSL] = tcp_maxidle;
   1078   1.1      cgd 				}
   1079   1.1      cgd 				tp->t_state = TCPS_FIN_WAIT_2;
   1080   1.1      cgd 			}
   1081   1.1      cgd 			break;
   1082   1.1      cgd 
   1083   1.1      cgd 	 	/*
   1084   1.1      cgd 		 * In CLOSING STATE in addition to the processing for
   1085   1.1      cgd 		 * the ESTABLISHED state if the ACK acknowledges our FIN
   1086   1.1      cgd 		 * then enter the TIME-WAIT state, otherwise ignore
   1087   1.1      cgd 		 * the segment.
   1088   1.1      cgd 		 */
   1089   1.1      cgd 		case TCPS_CLOSING:
   1090   1.1      cgd 			if (ourfinisacked) {
   1091   1.1      cgd 				tp->t_state = TCPS_TIME_WAIT;
   1092   1.1      cgd 				tcp_canceltimers(tp);
   1093   1.1      cgd 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
   1094   1.1      cgd 				soisdisconnected(so);
   1095   1.1      cgd 			}
   1096   1.1      cgd 			break;
   1097   1.1      cgd 
   1098   1.1      cgd 		/*
   1099   1.1      cgd 		 * In LAST_ACK, we may still be waiting for data to drain
   1100   1.1      cgd 		 * and/or to be acked, as well as for the ack of our FIN.
   1101   1.1      cgd 		 * If our FIN is now acknowledged, delete the TCB,
   1102   1.1      cgd 		 * enter the closed state and return.
   1103   1.1      cgd 		 */
   1104   1.1      cgd 		case TCPS_LAST_ACK:
   1105   1.1      cgd 			if (ourfinisacked) {
   1106   1.1      cgd 				tp = tcp_close(tp);
   1107   1.1      cgd 				goto drop;
   1108   1.1      cgd 			}
   1109   1.1      cgd 			break;
   1110   1.1      cgd 
   1111   1.1      cgd 		/*
   1112   1.1      cgd 		 * In TIME_WAIT state the only thing that should arrive
   1113   1.1      cgd 		 * is a retransmission of the remote FIN.  Acknowledge
   1114   1.1      cgd 		 * it and restart the finack timer.
   1115   1.1      cgd 		 */
   1116   1.1      cgd 		case TCPS_TIME_WAIT:
   1117   1.1      cgd 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
   1118   1.1      cgd 			goto dropafterack;
   1119   1.1      cgd 		}
   1120   1.1      cgd 	}
   1121   1.1      cgd 
   1122   1.1      cgd step6:
   1123   1.1      cgd 	/*
   1124   1.1      cgd 	 * Update window information.
   1125   1.1      cgd 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
   1126   1.1      cgd 	 */
   1127   1.1      cgd 	if ((tiflags & TH_ACK) &&
   1128   1.1      cgd 	    (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
   1129   1.1      cgd 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
   1130   1.9  mycroft 	     tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))) {
   1131   1.1      cgd 		/* keep track of pure window updates */
   1132   1.1      cgd 		if (ti->ti_len == 0 &&
   1133   1.9  mycroft 		    tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)
   1134   1.1      cgd 			tcpstat.tcps_rcvwinupd++;
   1135   1.9  mycroft 		tp->snd_wnd = tiwin;
   1136   1.1      cgd 		tp->snd_wl1 = ti->ti_seq;
   1137   1.1      cgd 		tp->snd_wl2 = ti->ti_ack;
   1138   1.1      cgd 		if (tp->snd_wnd > tp->max_sndwnd)
   1139   1.1      cgd 			tp->max_sndwnd = tp->snd_wnd;
   1140   1.1      cgd 		needoutput = 1;
   1141   1.1      cgd 	}
   1142   1.1      cgd 
   1143   1.1      cgd 	/*
   1144   1.1      cgd 	 * Process segments with URG.
   1145   1.1      cgd 	 */
   1146   1.1      cgd 	if ((tiflags & TH_URG) && ti->ti_urp &&
   1147   1.1      cgd 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
   1148   1.1      cgd 		/*
   1149   1.1      cgd 		 * This is a kludge, but if we receive and accept
   1150   1.1      cgd 		 * random urgent pointers, we'll crash in
   1151   1.1      cgd 		 * soreceive.  It's hard to imagine someone
   1152   1.1      cgd 		 * actually wanting to send this much urgent data.
   1153   1.1      cgd 		 */
   1154   1.9  mycroft 		if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) {
   1155   1.1      cgd 			ti->ti_urp = 0;			/* XXX */
   1156   1.1      cgd 			tiflags &= ~TH_URG;		/* XXX */
   1157   1.1      cgd 			goto dodata;			/* XXX */
   1158   1.1      cgd 		}
   1159   1.1      cgd 		/*
   1160   1.1      cgd 		 * If this segment advances the known urgent pointer,
   1161   1.1      cgd 		 * then mark the data stream.  This should not happen
   1162   1.1      cgd 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
   1163   1.9  mycroft 		 * a FIN has been received from the remote side.
   1164   1.1      cgd 		 * In these states we ignore the URG.
   1165   1.1      cgd 		 *
   1166   1.1      cgd 		 * According to RFC961 (Assigned Protocols),
   1167   1.1      cgd 		 * the urgent pointer points to the last octet
   1168   1.1      cgd 		 * of urgent data.  We continue, however,
   1169   1.1      cgd 		 * to consider it to indicate the first octet
   1170   1.9  mycroft 		 * of data past the urgent section as the original
   1171   1.1      cgd 		 * spec states (in one of two places).
   1172   1.1      cgd 		 */
   1173   1.1      cgd 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
   1174   1.1      cgd 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
   1175   1.1      cgd 			so->so_oobmark = so->so_rcv.sb_cc +
   1176   1.1      cgd 			    (tp->rcv_up - tp->rcv_nxt) - 1;
   1177   1.1      cgd 			if (so->so_oobmark == 0)
   1178   1.1      cgd 				so->so_state |= SS_RCVATMARK;
   1179   1.1      cgd 			sohasoutofband(so);
   1180   1.1      cgd 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
   1181   1.1      cgd 		}
   1182   1.1      cgd 		/*
   1183   1.1      cgd 		 * Remove out of band data so doesn't get presented to user.
   1184   1.1      cgd 		 * This can happen independent of advancing the URG pointer,
   1185   1.1      cgd 		 * but if two URG's are pending at once, some out-of-band
   1186   1.1      cgd 		 * data may creep in... ick.
   1187   1.1      cgd 		 */
   1188   1.1      cgd 		if (ti->ti_urp <= ti->ti_len
   1189   1.1      cgd #ifdef SO_OOBINLINE
   1190   1.1      cgd 		     && (so->so_options & SO_OOBINLINE) == 0
   1191   1.1      cgd #endif
   1192   1.1      cgd 		     )
   1193   1.1      cgd 			tcp_pulloutofband(so, ti, m);
   1194   1.1      cgd 	} else
   1195   1.1      cgd 		/*
   1196   1.1      cgd 		 * If no out of band data is expected,
   1197   1.1      cgd 		 * pull receive urgent pointer along
   1198   1.1      cgd 		 * with the receive window.
   1199   1.1      cgd 		 */
   1200   1.1      cgd 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
   1201   1.1      cgd 			tp->rcv_up = tp->rcv_nxt;
   1202   1.1      cgd dodata:							/* XXX */
   1203   1.1      cgd 
   1204   1.1      cgd 	/*
   1205   1.1      cgd 	 * Process the segment text, merging it into the TCP sequencing queue,
   1206   1.1      cgd 	 * and arranging for acknowledgment of receipt if necessary.
   1207   1.1      cgd 	 * This process logically involves adjusting tp->rcv_wnd as data
   1208   1.1      cgd 	 * is presented to the user (this happens in tcp_usrreq.c,
   1209   1.1      cgd 	 * case PRU_RCVD).  If a FIN has already been received on this
   1210   1.1      cgd 	 * connection then we just ignore the text.
   1211   1.1      cgd 	 */
   1212   1.1      cgd 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
   1213   1.1      cgd 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
   1214   1.1      cgd 		TCP_REASS(tp, ti, m, so, tiflags);
   1215   1.1      cgd 		/*
   1216   1.1      cgd 		 * Note the amount of data that peer has sent into
   1217   1.1      cgd 		 * our window, in order to estimate the sender's
   1218   1.1      cgd 		 * buffer size.
   1219   1.1      cgd 		 */
   1220   1.1      cgd 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
   1221   1.1      cgd 	} else {
   1222   1.1      cgd 		m_freem(m);
   1223   1.1      cgd 		tiflags &= ~TH_FIN;
   1224   1.1      cgd 	}
   1225   1.1      cgd 
   1226   1.1      cgd 	/*
   1227   1.1      cgd 	 * If FIN is received ACK the FIN and let the user know
   1228   1.1      cgd 	 * that the connection is closing.
   1229   1.1      cgd 	 */
   1230   1.1      cgd 	if (tiflags & TH_FIN) {
   1231   1.1      cgd 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
   1232   1.1      cgd 			socantrcvmore(so);
   1233   1.1      cgd 			tp->t_flags |= TF_ACKNOW;
   1234   1.1      cgd 			tp->rcv_nxt++;
   1235   1.1      cgd 		}
   1236   1.1      cgd 		switch (tp->t_state) {
   1237   1.1      cgd 
   1238   1.1      cgd 	 	/*
   1239   1.1      cgd 		 * In SYN_RECEIVED and ESTABLISHED STATES
   1240   1.1      cgd 		 * enter the CLOSE_WAIT state.
   1241   1.1      cgd 		 */
   1242   1.1      cgd 		case TCPS_SYN_RECEIVED:
   1243   1.1      cgd 		case TCPS_ESTABLISHED:
   1244   1.1      cgd 			tp->t_state = TCPS_CLOSE_WAIT;
   1245   1.1      cgd 			break;
   1246   1.1      cgd 
   1247   1.1      cgd 	 	/*
   1248   1.1      cgd 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
   1249   1.1      cgd 		 * enter the CLOSING state.
   1250   1.1      cgd 		 */
   1251   1.1      cgd 		case TCPS_FIN_WAIT_1:
   1252   1.1      cgd 			tp->t_state = TCPS_CLOSING;
   1253   1.1      cgd 			break;
   1254   1.1      cgd 
   1255   1.1      cgd 	 	/*
   1256   1.1      cgd 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
   1257   1.9  mycroft 		 * starting the time-wait timer, turning off the other
   1258   1.1      cgd 		 * standard timers.
   1259   1.1      cgd 		 */
   1260   1.1      cgd 		case TCPS_FIN_WAIT_2:
   1261   1.1      cgd 			tp->t_state = TCPS_TIME_WAIT;
   1262   1.1      cgd 			tcp_canceltimers(tp);
   1263   1.1      cgd 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
   1264   1.1      cgd 			soisdisconnected(so);
   1265   1.1      cgd 			break;
   1266   1.1      cgd 
   1267   1.1      cgd 		/*
   1268   1.1      cgd 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
   1269   1.1      cgd 		 */
   1270   1.1      cgd 		case TCPS_TIME_WAIT:
   1271   1.1      cgd 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
   1272   1.1      cgd 			break;
   1273   1.1      cgd 		}
   1274   1.1      cgd 	}
   1275   1.1      cgd 	if (so->so_options & SO_DEBUG)
   1276   1.1      cgd 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
   1277   1.1      cgd 
   1278   1.1      cgd 	/*
   1279   1.1      cgd 	 * Return any desired output.
   1280   1.1      cgd 	 */
   1281   1.1      cgd 	if (needoutput || (tp->t_flags & TF_ACKNOW))
   1282   1.1      cgd 		(void) tcp_output(tp);
   1283   1.1      cgd 	return;
   1284   1.1      cgd 
   1285   1.1      cgd dropafterack:
   1286   1.1      cgd 	/*
   1287   1.1      cgd 	 * Generate an ACK dropping incoming segment if it occupies
   1288   1.1      cgd 	 * sequence space, where the ACK reflects our state.
   1289   1.1      cgd 	 */
   1290   1.1      cgd 	if (tiflags & TH_RST)
   1291   1.1      cgd 		goto drop;
   1292   1.1      cgd 	m_freem(m);
   1293   1.1      cgd 	tp->t_flags |= TF_ACKNOW;
   1294   1.1      cgd 	(void) tcp_output(tp);
   1295   1.1      cgd 	return;
   1296   1.1      cgd 
   1297   1.1      cgd dropwithreset:
   1298   1.1      cgd 	/*
   1299   1.1      cgd 	 * Generate a RST, dropping incoming segment.
   1300   1.1      cgd 	 * Make ACK acceptable to originator of segment.
   1301   1.9  mycroft 	 * Don't bother to respond if destination was broadcast/multicast.
   1302   1.1      cgd 	 */
   1303   1.9  mycroft 	if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) ||
   1304  1.13  mycroft 	    IN_MULTICAST(ti->ti_dst.s_addr))
   1305   1.1      cgd 		goto drop;
   1306   1.1      cgd 	if (tiflags & TH_ACK)
   1307   1.1      cgd 		tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
   1308   1.1      cgd 	else {
   1309   1.1      cgd 		if (tiflags & TH_SYN)
   1310   1.1      cgd 			ti->ti_len++;
   1311   1.1      cgd 		tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
   1312   1.1      cgd 		    TH_RST|TH_ACK);
   1313   1.1      cgd 	}
   1314   1.1      cgd 	/* destroy temporarily created socket */
   1315   1.1      cgd 	if (dropsocket)
   1316   1.1      cgd 		(void) soabort(so);
   1317   1.1      cgd 	return;
   1318   1.1      cgd 
   1319   1.1      cgd drop:
   1320   1.1      cgd 	/*
   1321   1.1      cgd 	 * Drop space held by incoming segment and return.
   1322   1.1      cgd 	 */
   1323   1.1      cgd 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
   1324   1.1      cgd 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
   1325   1.1      cgd 	m_freem(m);
   1326   1.1      cgd 	/* destroy temporarily created socket */
   1327   1.1      cgd 	if (dropsocket)
   1328   1.1      cgd 		(void) soabort(so);
   1329   1.1      cgd 	return;
   1330   1.9  mycroft #ifndef TUBA_INCLUDE
   1331   1.1      cgd }
   1332   1.1      cgd 
   1333   1.5  mycroft void
   1334   1.9  mycroft tcp_dooptions(tp, cp, cnt, ti, ts_present, ts_val, ts_ecr)
   1335   1.1      cgd 	struct tcpcb *tp;
   1336   1.9  mycroft 	u_char *cp;
   1337   1.9  mycroft 	int cnt;
   1338   1.1      cgd 	struct tcpiphdr *ti;
   1339   1.9  mycroft 	int *ts_present;
   1340  1.12      cgd 	u_int32_t *ts_val, *ts_ecr;
   1341   1.1      cgd {
   1342  1.12      cgd 	u_int16_t mss;
   1343   1.9  mycroft 	int opt, optlen;
   1344   1.1      cgd 
   1345   1.1      cgd 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
   1346   1.1      cgd 		opt = cp[0];
   1347   1.1      cgd 		if (opt == TCPOPT_EOL)
   1348   1.1      cgd 			break;
   1349   1.1      cgd 		if (opt == TCPOPT_NOP)
   1350   1.1      cgd 			optlen = 1;
   1351   1.1      cgd 		else {
   1352   1.1      cgd 			optlen = cp[1];
   1353   1.1      cgd 			if (optlen <= 0)
   1354   1.1      cgd 				break;
   1355   1.1      cgd 		}
   1356   1.1      cgd 		switch (opt) {
   1357   1.1      cgd 
   1358   1.1      cgd 		default:
   1359   1.1      cgd 			continue;
   1360   1.1      cgd 
   1361   1.1      cgd 		case TCPOPT_MAXSEG:
   1362   1.9  mycroft 			if (optlen != TCPOLEN_MAXSEG)
   1363   1.1      cgd 				continue;
   1364   1.1      cgd 			if (!(ti->ti_flags & TH_SYN))
   1365   1.1      cgd 				continue;
   1366   1.1      cgd 			bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
   1367   1.1      cgd 			NTOHS(mss);
   1368   1.1      cgd 			(void) tcp_mss(tp, mss);	/* sets t_maxseg */
   1369   1.1      cgd 			break;
   1370   1.9  mycroft 
   1371   1.9  mycroft 		case TCPOPT_WINDOW:
   1372   1.9  mycroft 			if (optlen != TCPOLEN_WINDOW)
   1373   1.9  mycroft 				continue;
   1374   1.9  mycroft 			if (!(ti->ti_flags & TH_SYN))
   1375   1.9  mycroft 				continue;
   1376   1.9  mycroft 			tp->t_flags |= TF_RCVD_SCALE;
   1377   1.9  mycroft 			tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
   1378   1.9  mycroft 			break;
   1379   1.9  mycroft 
   1380   1.9  mycroft 		case TCPOPT_TIMESTAMP:
   1381   1.9  mycroft 			if (optlen != TCPOLEN_TIMESTAMP)
   1382   1.9  mycroft 				continue;
   1383   1.9  mycroft 			*ts_present = 1;
   1384   1.9  mycroft 			bcopy((char *)cp + 2, (char *) ts_val, sizeof(*ts_val));
   1385   1.9  mycroft 			NTOHL(*ts_val);
   1386   1.9  mycroft 			bcopy((char *)cp + 6, (char *) ts_ecr, sizeof(*ts_ecr));
   1387   1.9  mycroft 			NTOHL(*ts_ecr);
   1388   1.9  mycroft 
   1389   1.9  mycroft 			/*
   1390   1.9  mycroft 			 * A timestamp received in a SYN makes
   1391   1.9  mycroft 			 * it ok to send timestamp requests and replies.
   1392   1.9  mycroft 			 */
   1393   1.9  mycroft 			if (ti->ti_flags & TH_SYN) {
   1394   1.9  mycroft 				tp->t_flags |= TF_RCVD_TSTMP;
   1395   1.9  mycroft 				tp->ts_recent = *ts_val;
   1396   1.9  mycroft 				tp->ts_recent_age = tcp_now;
   1397   1.9  mycroft 			}
   1398   1.9  mycroft 			break;
   1399   1.1      cgd 		}
   1400   1.1      cgd 	}
   1401   1.1      cgd }
   1402   1.1      cgd 
   1403   1.1      cgd /*
   1404   1.1      cgd  * Pull out of band byte out of a segment so
   1405   1.1      cgd  * it doesn't appear in the user's data queue.
   1406   1.1      cgd  * It is still reflected in the segment length for
   1407   1.1      cgd  * sequencing purposes.
   1408   1.1      cgd  */
   1409   1.5  mycroft void
   1410   1.1      cgd tcp_pulloutofband(so, ti, m)
   1411   1.1      cgd 	struct socket *so;
   1412   1.1      cgd 	struct tcpiphdr *ti;
   1413   1.1      cgd 	register struct mbuf *m;
   1414   1.1      cgd {
   1415   1.1      cgd 	int cnt = ti->ti_urp - 1;
   1416   1.9  mycroft 
   1417   1.1      cgd 	while (cnt >= 0) {
   1418   1.1      cgd 		if (m->m_len > cnt) {
   1419   1.1      cgd 			char *cp = mtod(m, caddr_t) + cnt;
   1420   1.1      cgd 			struct tcpcb *tp = sototcpcb(so);
   1421   1.1      cgd 
   1422   1.1      cgd 			tp->t_iobc = *cp;
   1423   1.1      cgd 			tp->t_oobflags |= TCPOOB_HAVEDATA;
   1424   1.1      cgd 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
   1425   1.1      cgd 			m->m_len--;
   1426   1.1      cgd 			return;
   1427   1.1      cgd 		}
   1428   1.1      cgd 		cnt -= m->m_len;
   1429   1.1      cgd 		m = m->m_next;
   1430   1.1      cgd 		if (m == 0)
   1431   1.1      cgd 			break;
   1432   1.1      cgd 	}
   1433   1.1      cgd 	panic("tcp_pulloutofband");
   1434   1.1      cgd }
   1435   1.1      cgd 
   1436   1.1      cgd /*
   1437   1.1      cgd  * Collect new round-trip time estimate
   1438   1.1      cgd  * and update averages and current timeout.
   1439   1.1      cgd  */
   1440   1.5  mycroft void
   1441   1.9  mycroft tcp_xmit_timer(tp, rtt)
   1442   1.1      cgd 	register struct tcpcb *tp;
   1443   1.9  mycroft 	short rtt;
   1444   1.1      cgd {
   1445   1.1      cgd 	register short delta;
   1446   1.1      cgd 
   1447   1.1      cgd 	tcpstat.tcps_rttupdated++;
   1448  1.17  mycroft 	--rtt;
   1449   1.1      cgd 	if (tp->t_srtt != 0) {
   1450   1.1      cgd 		/*
   1451   1.1      cgd 		 * srtt is stored as fixed point with 3 bits after the
   1452   1.1      cgd 		 * binary point (i.e., scaled by 8).  The following magic
   1453   1.1      cgd 		 * is equivalent to the smoothing algorithm in rfc793 with
   1454   1.1      cgd 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
   1455   1.9  mycroft 		 * point).  Adjust rtt to origin 0.
   1456   1.1      cgd 		 */
   1457  1.16  mycroft 		delta = (rtt << 2) - (tp->t_srtt >> TCP_RTT_SHIFT);
   1458   1.1      cgd 		if ((tp->t_srtt += delta) <= 0)
   1459   1.1      cgd 			tp->t_srtt = 1;
   1460   1.1      cgd 		/*
   1461   1.1      cgd 		 * We accumulate a smoothed rtt variance (actually, a
   1462   1.1      cgd 		 * smoothed mean difference), then set the retransmit
   1463   1.1      cgd 		 * timer to smoothed rtt + 4 times the smoothed variance.
   1464   1.1      cgd 		 * rttvar is stored as fixed point with 2 bits after the
   1465   1.1      cgd 		 * binary point (scaled by 4).  The following is
   1466   1.1      cgd 		 * equivalent to rfc793 smoothing with an alpha of .75
   1467   1.1      cgd 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
   1468   1.1      cgd 		 * rfc793's wired-in beta.
   1469   1.1      cgd 		 */
   1470   1.1      cgd 		if (delta < 0)
   1471   1.1      cgd 			delta = -delta;
   1472   1.1      cgd 		delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
   1473   1.1      cgd 		if ((tp->t_rttvar += delta) <= 0)
   1474   1.1      cgd 			tp->t_rttvar = 1;
   1475   1.1      cgd 	} else {
   1476   1.9  mycroft 		/*
   1477   1.1      cgd 		 * No rtt measurement yet - use the unsmoothed rtt.
   1478   1.1      cgd 		 * Set the variance to half the rtt (so our first
   1479   1.9  mycroft 		 * retransmit happens at 3*rtt).
   1480   1.1      cgd 		 */
   1481  1.16  mycroft 		tp->t_srtt = rtt << (TCP_RTT_SHIFT + 2);
   1482  1.16  mycroft 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT + 2 - 1);
   1483   1.1      cgd 	}
   1484   1.1      cgd 	tp->t_rtt = 0;
   1485   1.1      cgd 	tp->t_rxtshift = 0;
   1486   1.1      cgd 
   1487   1.1      cgd 	/*
   1488   1.1      cgd 	 * the retransmit should happen at rtt + 4 * rttvar.
   1489   1.1      cgd 	 * Because of the way we do the smoothing, srtt and rttvar
   1490   1.1      cgd 	 * will each average +1/2 tick of bias.  When we compute
   1491   1.1      cgd 	 * the retransmit timer, we want 1/2 tick of rounding and
   1492   1.1      cgd 	 * 1 extra tick because of +-1/2 tick uncertainty in the
   1493   1.1      cgd 	 * firing of the timer.  The bias will give us exactly the
   1494   1.1      cgd 	 * 1.5 tick we need.  But, because the bias is
   1495   1.1      cgd 	 * statistical, we have to test that we don't drop below
   1496   1.1      cgd 	 * the minimum feasible timer (which is 2 ticks).
   1497   1.1      cgd 	 */
   1498   1.1      cgd 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
   1499  1.16  mycroft 	    rtt + 2, TCPTV_REXMTMAX);
   1500   1.9  mycroft 
   1501   1.1      cgd 	/*
   1502   1.1      cgd 	 * We received an ack for a packet that wasn't retransmitted;
   1503   1.1      cgd 	 * it is probably safe to discard any error indications we've
   1504   1.1      cgd 	 * received recently.  This isn't quite right, but close enough
   1505   1.1      cgd 	 * for now (a route might have failed after we sent a segment,
   1506   1.1      cgd 	 * and the return path might not be symmetrical).
   1507   1.1      cgd 	 */
   1508   1.1      cgd 	tp->t_softerror = 0;
   1509   1.1      cgd }
   1510   1.1      cgd 
   1511   1.1      cgd /*
   1512   1.1      cgd  * Determine a reasonable value for maxseg size.
   1513   1.1      cgd  * If the route is known, check route for mtu.
   1514   1.1      cgd  * If none, use an mss that can be handled on the outgoing
   1515   1.1      cgd  * interface without forcing IP to fragment; if bigger than
   1516   1.1      cgd  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
   1517   1.1      cgd  * to utilize large mbufs.  If no route is found, route has no mtu,
   1518   1.1      cgd  * or the destination isn't local, use a default, hopefully conservative
   1519   1.1      cgd  * size (usually 512 or the default IP max size, but no more than the mtu
   1520   1.1      cgd  * of the interface), as we can't discover anything about intervening
   1521   1.1      cgd  * gateways or networks.  We also initialize the congestion/slow start
   1522   1.1      cgd  * window to be a single segment if the destination isn't local.
   1523   1.1      cgd  * While looking at the routing entry, we also initialize other path-dependent
   1524   1.1      cgd  * parameters from pre-set or cached values in the routing entry.
   1525   1.1      cgd  */
   1526   1.5  mycroft int
   1527   1.1      cgd tcp_mss(tp, offer)
   1528   1.1      cgd 	register struct tcpcb *tp;
   1529   1.9  mycroft 	u_int offer;
   1530   1.1      cgd {
   1531   1.1      cgd 	struct route *ro;
   1532   1.1      cgd 	register struct rtentry *rt;
   1533   1.1      cgd 	struct ifnet *ifp;
   1534   1.1      cgd 	register int rtt, mss;
   1535   1.1      cgd 	u_long bufsize;
   1536   1.1      cgd 	struct inpcb *inp;
   1537   1.1      cgd 	struct socket *so;
   1538   1.9  mycroft 	extern int tcp_mssdflt;
   1539   1.1      cgd 
   1540   1.1      cgd 	inp = tp->t_inpcb;
   1541   1.1      cgd 	ro = &inp->inp_route;
   1542   1.1      cgd 
   1543   1.1      cgd 	if ((rt = ro->ro_rt) == (struct rtentry *)0) {
   1544   1.1      cgd 		/* No route yet, so try to acquire one */
   1545   1.1      cgd 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
   1546   1.1      cgd 			ro->ro_dst.sa_family = AF_INET;
   1547   1.1      cgd 			ro->ro_dst.sa_len = sizeof(ro->ro_dst);
   1548  1.14  mycroft 			satosin(&ro->ro_dst)->sin_addr = inp->inp_faddr;
   1549   1.1      cgd 			rtalloc(ro);
   1550   1.1      cgd 		}
   1551   1.1      cgd 		if ((rt = ro->ro_rt) == (struct rtentry *)0)
   1552   1.1      cgd 			return (tcp_mssdflt);
   1553   1.1      cgd 	}
   1554   1.1      cgd 	ifp = rt->rt_ifp;
   1555   1.1      cgd 	so = inp->inp_socket;
   1556   1.1      cgd 
   1557   1.1      cgd #ifdef RTV_MTU	/* if route characteristics exist ... */
   1558   1.1      cgd 	/*
   1559   1.1      cgd 	 * While we're here, check if there's an initial rtt
   1560   1.1      cgd 	 * or rttvar.  Convert from the route-table units
   1561   1.1      cgd 	 * to scaled multiples of the slow timeout timer.
   1562   1.1      cgd 	 */
   1563   1.1      cgd 	if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
   1564   1.9  mycroft 		/*
   1565   1.9  mycroft 		 * XXX the lock bit for MTU indicates that the value
   1566   1.9  mycroft 		 * is also a minimum value; this is subject to time.
   1567   1.9  mycroft 		 */
   1568   1.9  mycroft 		if (rt->rt_rmx.rmx_locks & RTV_RTT)
   1569   1.1      cgd 			tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ);
   1570   1.1      cgd 		tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
   1571   1.1      cgd 		if (rt->rt_rmx.rmx_rttvar)
   1572   1.1      cgd 			tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
   1573   1.1      cgd 			    (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
   1574   1.1      cgd 		else
   1575   1.1      cgd 			/* default variation is +- 1 rtt */
   1576   1.1      cgd 			tp->t_rttvar =
   1577   1.1      cgd 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
   1578   1.1      cgd 		TCPT_RANGESET(tp->t_rxtcur,
   1579   1.1      cgd 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
   1580   1.1      cgd 		    tp->t_rttmin, TCPTV_REXMTMAX);
   1581   1.1      cgd 	}
   1582   1.1      cgd 	/*
   1583   1.1      cgd 	 * if there's an mtu associated with the route, use it
   1584   1.1      cgd 	 */
   1585   1.1      cgd 	if (rt->rt_rmx.rmx_mtu)
   1586   1.1      cgd 		mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
   1587   1.1      cgd 	else
   1588   1.1      cgd #endif /* RTV_MTU */
   1589   1.1      cgd 	{
   1590   1.1      cgd 		mss = ifp->if_mtu - sizeof(struct tcpiphdr);
   1591   1.1      cgd #if	(MCLBYTES & (MCLBYTES - 1)) == 0
   1592   1.1      cgd 		if (mss > MCLBYTES)
   1593   1.1      cgd 			mss &= ~(MCLBYTES-1);
   1594   1.1      cgd #else
   1595   1.1      cgd 		if (mss > MCLBYTES)
   1596   1.1      cgd 			mss = mss / MCLBYTES * MCLBYTES;
   1597   1.1      cgd #endif
   1598   1.1      cgd 		if (!in_localaddr(inp->inp_faddr))
   1599   1.1      cgd 			mss = min(mss, tcp_mssdflt);
   1600   1.1      cgd 	}
   1601   1.1      cgd 	/*
   1602   1.1      cgd 	 * The current mss, t_maxseg, is initialized to the default value.
   1603   1.1      cgd 	 * If we compute a smaller value, reduce the current mss.
   1604   1.1      cgd 	 * If we compute a larger value, return it for use in sending
   1605   1.1      cgd 	 * a max seg size option, but don't store it for use
   1606   1.1      cgd 	 * unless we received an offer at least that large from peer.
   1607   1.1      cgd 	 * However, do not accept offers under 32 bytes.
   1608   1.1      cgd 	 */
   1609   1.1      cgd 	if (offer)
   1610   1.1      cgd 		mss = min(mss, offer);
   1611   1.1      cgd 	mss = max(mss, 32);		/* sanity */
   1612   1.1      cgd 	if (mss < tp->t_maxseg || offer != 0) {
   1613   1.1      cgd 		/*
   1614   1.1      cgd 		 * If there's a pipesize, change the socket buffer
   1615   1.1      cgd 		 * to that size.  Make the socket buffers an integral
   1616   1.1      cgd 		 * number of mss units; if the mss is larger than
   1617   1.1      cgd 		 * the socket buffer, decrease the mss.
   1618   1.1      cgd 		 */
   1619   1.1      cgd #ifdef RTV_SPIPE
   1620   1.1      cgd 		if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
   1621   1.1      cgd #endif
   1622   1.1      cgd 			bufsize = so->so_snd.sb_hiwat;
   1623   1.1      cgd 		if (bufsize < mss)
   1624   1.1      cgd 			mss = bufsize;
   1625   1.1      cgd 		else {
   1626   1.9  mycroft 			bufsize = roundup(bufsize, mss);
   1627   1.9  mycroft 			if (bufsize > sb_max)
   1628   1.9  mycroft 				bufsize = sb_max;
   1629   1.9  mycroft 			(void)sbreserve(&so->so_snd, bufsize);
   1630   1.1      cgd 		}
   1631   1.1      cgd 		tp->t_maxseg = mss;
   1632   1.1      cgd 
   1633   1.1      cgd #ifdef RTV_RPIPE
   1634   1.1      cgd 		if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
   1635   1.1      cgd #endif
   1636   1.1      cgd 			bufsize = so->so_rcv.sb_hiwat;
   1637   1.1      cgd 		if (bufsize > mss) {
   1638   1.9  mycroft 			bufsize = roundup(bufsize, mss);
   1639   1.9  mycroft 			if (bufsize > sb_max)
   1640   1.9  mycroft 				bufsize = sb_max;
   1641   1.9  mycroft 			(void)sbreserve(&so->so_rcv, bufsize);
   1642   1.1      cgd 		}
   1643   1.1      cgd 	}
   1644   1.1      cgd 	tp->snd_cwnd = mss;
   1645   1.1      cgd 
   1646   1.1      cgd #ifdef RTV_SSTHRESH
   1647   1.1      cgd 	if (rt->rt_rmx.rmx_ssthresh) {
   1648   1.1      cgd 		/*
   1649   1.1      cgd 		 * There's some sort of gateway or interface
   1650   1.1      cgd 		 * buffer limit on the path.  Use this to set
   1651   1.1      cgd 		 * the slow start threshhold, but set the
   1652   1.1      cgd 		 * threshold to no less than 2*mss.
   1653   1.1      cgd 		 */
   1654   1.1      cgd 		tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
   1655   1.1      cgd 	}
   1656   1.1      cgd #endif /* RTV_MTU */
   1657   1.1      cgd 	return (mss);
   1658   1.1      cgd }
   1659   1.9  mycroft #endif /* TUBA_INCLUDE */
   1660