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