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