Home | History | Annotate | Line # | Download | only in netinet
tcp_subr.c revision 1.32
      1 /*	$NetBSD: tcp_subr.c,v 1.32 1997/10/18 21:18:33 kml Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1988, 1990, 1993
      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_subr.c	8.1 (Berkeley) 6/10/93
     36  */
     37 
     38 #include "rnd.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/proc.h>
     42 #include <sys/systm.h>
     43 #include <sys/malloc.h>
     44 #include <sys/mbuf.h>
     45 #include <sys/socket.h>
     46 #include <sys/socketvar.h>
     47 #include <sys/protosw.h>
     48 #include <sys/errno.h>
     49 #include <sys/kernel.h>
     50 #if NRND > 0
     51 #include <sys/rnd.h>
     52 #endif
     53 
     54 #include <net/route.h>
     55 #include <net/if.h>
     56 
     57 #include <netinet/in.h>
     58 #include <netinet/in_systm.h>
     59 #include <netinet/ip.h>
     60 #include <netinet/in_pcb.h>
     61 #include <netinet/ip_var.h>
     62 #include <netinet/ip_icmp.h>
     63 #include <netinet/tcp.h>
     64 #include <netinet/tcp_fsm.h>
     65 #include <netinet/tcp_seq.h>
     66 #include <netinet/tcp_timer.h>
     67 #include <netinet/tcp_var.h>
     68 #include <netinet/tcpip.h>
     69 
     70 /* patchable/settable parameters for tcp */
     71 int 	tcp_mssdflt = TCP_MSS;
     72 int 	tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
     73 int	tcp_do_rfc1323 = 1;
     74 
     75 #ifndef TCBHASHSIZE
     76 #define	TCBHASHSIZE	128
     77 #endif
     78 int	tcbhashsize = TCBHASHSIZE;
     79 
     80 /*
     81  * Tcp initialization
     82  */
     83 void
     84 tcp_init()
     85 {
     86 
     87 	in_pcbinit(&tcbtable, tcbhashsize, tcbhashsize);
     88 	if (max_protohdr < sizeof(struct tcpiphdr))
     89 		max_protohdr = sizeof(struct tcpiphdr);
     90 	if (max_linkhdr + sizeof(struct tcpiphdr) > MHLEN)
     91 		panic("tcp_init");
     92 }
     93 
     94 /*
     95  * Create template to be used to send tcp packets on a connection.
     96  * Call after host entry created, allocates an mbuf and fills
     97  * in a skeletal tcp/ip header, minimizing the amount of work
     98  * necessary when the connection is used.
     99  */
    100 struct tcpiphdr *
    101 tcp_template(tp)
    102 	struct tcpcb *tp;
    103 {
    104 	register struct inpcb *inp = tp->t_inpcb;
    105 	register struct tcpiphdr *n;
    106 
    107 	if ((n = tp->t_template) == 0) {
    108 		MALLOC(n, struct tcpiphdr *, sizeof (struct tcpiphdr),
    109 		    M_MBUF, M_NOWAIT);
    110 		if (n == NULL)
    111 			return (0);
    112 	}
    113 	bzero(n->ti_x1, sizeof n->ti_x1);
    114 	n->ti_pr = IPPROTO_TCP;
    115 	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
    116 	n->ti_src = inp->inp_laddr;
    117 	n->ti_dst = inp->inp_faddr;
    118 	n->ti_sport = inp->inp_lport;
    119 	n->ti_dport = inp->inp_fport;
    120 	n->ti_seq = 0;
    121 	n->ti_ack = 0;
    122 	n->ti_x2 = 0;
    123 	n->ti_off = 5;
    124 	n->ti_flags = 0;
    125 	n->ti_win = 0;
    126 	n->ti_sum = 0;
    127 	n->ti_urp = 0;
    128 	return (n);
    129 }
    130 
    131 /*
    132  * Send a single message to the TCP at address specified by
    133  * the given TCP/IP header.  If m == 0, then we make a copy
    134  * of the tcpiphdr at ti and send directly to the addressed host.
    135  * This is used to force keep alive messages out using the TCP
    136  * template for a connection tp->t_template.  If flags are given
    137  * then we send a message back to the TCP which originated the
    138  * segment ti, and discard the mbuf containing it and any other
    139  * attached mbufs.
    140  *
    141  * In any case the ack and sequence number of the transmitted
    142  * segment are as specified by the parameters.
    143  */
    144 int
    145 tcp_respond(tp, ti, m, ack, seq, flags)
    146 	struct tcpcb *tp;
    147 	register struct tcpiphdr *ti;
    148 	register struct mbuf *m;
    149 	tcp_seq ack, seq;
    150 	int flags;
    151 {
    152 	register int tlen;
    153 	int win = 0;
    154 	struct route *ro = 0;
    155 
    156 	if (tp) {
    157 		win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
    158 		ro = &tp->t_inpcb->inp_route;
    159 	}
    160 	if (m == 0) {
    161 		m = m_gethdr(M_DONTWAIT, MT_HEADER);
    162 		if (m == NULL)
    163 			return (ENOBUFS);
    164 #ifdef TCP_COMPAT_42
    165 		tlen = 1;
    166 #else
    167 		tlen = 0;
    168 #endif
    169 		m->m_data += max_linkhdr;
    170 		*mtod(m, struct tcpiphdr *) = *ti;
    171 		ti = mtod(m, struct tcpiphdr *);
    172 		flags = TH_ACK;
    173 	} else {
    174 		m_freem(m->m_next);
    175 		m->m_next = 0;
    176 		m->m_data = (caddr_t)ti;
    177 		m->m_len = sizeof (struct tcpiphdr);
    178 		tlen = 0;
    179 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
    180 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_int32_t);
    181 		xchg(ti->ti_dport, ti->ti_sport, u_int16_t);
    182 #undef xchg
    183 	}
    184 	bzero(ti->ti_x1, sizeof ti->ti_x1);
    185 	ti->ti_seq = htonl(seq);
    186 	ti->ti_ack = htonl(ack);
    187 	ti->ti_x2 = 0;
    188 	if ((flags & TH_SYN) == 0) {
    189 		if (tp)
    190 			ti->ti_win = htons((u_int16_t) (win >> tp->rcv_scale));
    191 		else
    192 			ti->ti_win = htons((u_int16_t)win);
    193 		ti->ti_off = sizeof (struct tcphdr) >> 2;
    194 		tlen += sizeof (struct tcphdr);
    195 	} else
    196 		tlen += ti->ti_off << 2;
    197 	ti->ti_len = htons((u_int16_t)tlen);
    198 	tlen += sizeof (struct ip);
    199 	m->m_len = tlen;
    200 	m->m_pkthdr.len = tlen;
    201 	m->m_pkthdr.rcvif = (struct ifnet *) 0;
    202 	ti->ti_flags = flags;
    203 	ti->ti_urp = 0;
    204 	ti->ti_sum = 0;
    205 	ti->ti_sum = in_cksum(m, tlen);
    206 	((struct ip *)ti)->ip_len = tlen;
    207 	((struct ip *)ti)->ip_ttl = ip_defttl;
    208 	return ip_output(m, NULL, ro, 0, NULL);
    209 }
    210 
    211 /*
    212  * Create a new TCP control block, making an
    213  * empty reassembly queue and hooking it to the argument
    214  * protocol control block.
    215  */
    216 struct tcpcb *
    217 tcp_newtcpcb(inp)
    218 	struct inpcb *inp;
    219 {
    220 	register struct tcpcb *tp;
    221 
    222 	tp = malloc(sizeof(*tp), M_PCB, M_NOWAIT);
    223 	if (tp == NULL)
    224 		return ((struct tcpcb *)0);
    225 	bzero((caddr_t)tp, sizeof(struct tcpcb));
    226 	LIST_INIT(&tp->segq);
    227 	tp->t_maxseg = tcp_mssdflt;
    228 	tp->t_ourmss = tcp_mssdflt;
    229 
    230 	tp->t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
    231 	tp->t_inpcb = inp;
    232 	/*
    233 	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
    234 	 * rtt estimate.  Set rttvar so that srtt + 2 * rttvar gives
    235 	 * reasonable initial retransmit time.
    236 	 */
    237 	tp->t_srtt = TCPTV_SRTTBASE;
    238 	tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << (TCP_RTTVAR_SHIFT + 2 - 1);
    239 	tp->t_rttmin = TCPTV_MIN;
    240 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
    241 	    TCPTV_MIN, TCPTV_REXMTMAX);
    242 	tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
    243 	tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
    244 	inp->inp_ip.ip_ttl = ip_defttl;
    245 	inp->inp_ppcb = (caddr_t)tp;
    246 	return (tp);
    247 }
    248 
    249 /*
    250  * Drop a TCP connection, reporting
    251  * the specified error.  If connection is synchronized,
    252  * then send a RST to peer.
    253  */
    254 struct tcpcb *
    255 tcp_drop(tp, errno)
    256 	register struct tcpcb *tp;
    257 	int errno;
    258 {
    259 	struct socket *so = tp->t_inpcb->inp_socket;
    260 
    261 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
    262 		tp->t_state = TCPS_CLOSED;
    263 		(void) tcp_output(tp);
    264 		tcpstat.tcps_drops++;
    265 	} else
    266 		tcpstat.tcps_conndrops++;
    267 	if (errno == ETIMEDOUT && tp->t_softerror)
    268 		errno = tp->t_softerror;
    269 	so->so_error = errno;
    270 	return (tcp_close(tp));
    271 }
    272 
    273 /*
    274  * Close a TCP control block:
    275  *	discard all space held by the tcp
    276  *	discard internet protocol block
    277  *	wake up any sleepers
    278  */
    279 struct tcpcb *
    280 tcp_close(tp)
    281 	register struct tcpcb *tp;
    282 {
    283 	register struct ipqent *qe;
    284 	struct inpcb *inp = tp->t_inpcb;
    285 	struct socket *so = inp->inp_socket;
    286 #ifdef RTV_RTT
    287 	register struct rtentry *rt;
    288 
    289 	/*
    290 	 * If we sent enough data to get some meaningful characteristics,
    291 	 * save them in the routing entry.  'Enough' is arbitrarily
    292 	 * defined as the sendpipesize (default 4K) * 16.  This would
    293 	 * give us 16 rtt samples assuming we only get one sample per
    294 	 * window (the usual case on a long haul net).  16 samples is
    295 	 * enough for the srtt filter to converge to within 5% of the correct
    296 	 * value; fewer samples and we could save a very bogus rtt.
    297 	 *
    298 	 * Don't update the default route's characteristics and don't
    299 	 * update anything that the user "locked".
    300 	 */
    301 	if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) &&
    302 	    (rt = inp->inp_route.ro_rt) &&
    303 	    !in_nullhost(satosin(rt_key(rt))->sin_addr)) {
    304 		register u_long i = 0;
    305 
    306 		if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
    307 			i = tp->t_srtt *
    308 			    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
    309 			if (rt->rt_rmx.rmx_rtt && i)
    310 				/*
    311 				 * filter this update to half the old & half
    312 				 * the new values, converting scale.
    313 				 * See route.h and tcp_var.h for a
    314 				 * description of the scaling constants.
    315 				 */
    316 				rt->rt_rmx.rmx_rtt =
    317 				    (rt->rt_rmx.rmx_rtt + i) / 2;
    318 			else
    319 				rt->rt_rmx.rmx_rtt = i;
    320 		}
    321 		if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
    322 			i = tp->t_rttvar *
    323 			    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTTVAR_SHIFT + 2));
    324 			if (rt->rt_rmx.rmx_rttvar && i)
    325 				rt->rt_rmx.rmx_rttvar =
    326 				    (rt->rt_rmx.rmx_rttvar + i) / 2;
    327 			else
    328 				rt->rt_rmx.rmx_rttvar = i;
    329 		}
    330 		/*
    331 		 * update the pipelimit (ssthresh) if it has been updated
    332 		 * already or if a pipesize was specified & the threshhold
    333 		 * got below half the pipesize.  I.e., wait for bad news
    334 		 * before we start updating, then update on both good
    335 		 * and bad news.
    336 		 */
    337 		if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
    338 		    (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh) ||
    339 		    i < (rt->rt_rmx.rmx_sendpipe / 2)) {
    340 			/*
    341 			 * convert the limit from user data bytes to
    342 			 * packets then to packet data bytes.
    343 			 */
    344 			i = (i + tp->t_maxseg / 2) / tp->t_maxseg;
    345 			if (i < 2)
    346 				i = 2;
    347 			i *= (u_long)(tp->t_maxseg + sizeof (struct tcpiphdr));
    348 			if (rt->rt_rmx.rmx_ssthresh)
    349 				rt->rt_rmx.rmx_ssthresh =
    350 				    (rt->rt_rmx.rmx_ssthresh + i) / 2;
    351 			else
    352 				rt->rt_rmx.rmx_ssthresh = i;
    353 		}
    354 	}
    355 #endif /* RTV_RTT */
    356 	/* free the reassembly queue, if any */
    357 	while ((qe = tp->segq.lh_first) != NULL) {
    358 		LIST_REMOVE(qe, ipqe_q);
    359 		m_freem(qe->ipqe_m);
    360 		FREE(qe, M_IPQ);
    361 	}
    362 	if (tp->t_template)
    363 		FREE(tp->t_template, M_MBUF);
    364 	free(tp, M_PCB);
    365 	inp->inp_ppcb = 0;
    366 	soisdisconnected(so);
    367 	in_pcbdetach(inp);
    368 	tcpstat.tcps_closed++;
    369 	return ((struct tcpcb *)0);
    370 }
    371 
    372 void
    373 tcp_drain()
    374 {
    375 
    376 }
    377 
    378 /*
    379  * Notify a tcp user of an asynchronous error;
    380  * store error as soft error, but wake up user
    381  * (for now, won't do anything until can select for soft error).
    382  */
    383 void
    384 tcp_notify(inp, error)
    385 	struct inpcb *inp;
    386 	int error;
    387 {
    388 	register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
    389 	register struct socket *so = inp->inp_socket;
    390 
    391 	/*
    392 	 * Ignore some errors if we are hooked up.
    393 	 * If connection hasn't completed, has retransmitted several times,
    394 	 * and receives a second error, give up now.  This is better
    395 	 * than waiting a long time to establish a connection that
    396 	 * can never complete.
    397 	 */
    398 	if (tp->t_state == TCPS_ESTABLISHED &&
    399 	     (error == EHOSTUNREACH || error == ENETUNREACH ||
    400 	      error == EHOSTDOWN)) {
    401 		return;
    402 	} else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
    403 	    tp->t_rxtshift > 3 && tp->t_softerror)
    404 		so->so_error = error;
    405 	else
    406 		tp->t_softerror = error;
    407 	wakeup((caddr_t) &so->so_timeo);
    408 	sorwakeup(so);
    409 	sowwakeup(so);
    410 }
    411 
    412 void *
    413 tcp_ctlinput(cmd, sa, v)
    414 	int cmd;
    415 	struct sockaddr *sa;
    416 	register void *v;
    417 {
    418 	register struct ip *ip = v;
    419 	register struct tcphdr *th;
    420 	extern int inetctlerrmap[];
    421 	void (*notify) __P((struct inpcb *, int)) = tcp_notify;
    422 	int errno;
    423 	int nmatch;
    424 
    425 	if ((unsigned)cmd >= PRC_NCMDS)
    426 		return NULL;
    427 	errno = inetctlerrmap[cmd];
    428 	if (cmd == PRC_QUENCH)
    429 		notify = tcp_quench;
    430 	else if (PRC_IS_REDIRECT(cmd))
    431 		notify = in_rtchange, ip = 0;
    432 	else if (cmd == PRC_MSGSIZE && ip_mtudisc)
    433 		notify = tcp_mtudisc, ip = 0;
    434 	else if (cmd == PRC_HOSTDEAD)
    435 		ip = 0;
    436 	else if (errno == 0)
    437 		return NULL;
    438 	if (ip) {
    439 		th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
    440 		nmatch = in_pcbnotify(&tcbtable, satosin(sa)->sin_addr,
    441 		    th->th_dport, ip->ip_src, th->th_sport, errno, notify);
    442 		if (nmatch == 0 && syn_cache_count &&
    443 		    (inetctlerrmap[cmd] == EHOSTUNREACH ||
    444 		    inetctlerrmap[cmd] == ENETUNREACH ||
    445 		    inetctlerrmap[cmd] == EHOSTDOWN))
    446 			syn_cache_unreach(ip, th);
    447 	} else
    448 		(void)in_pcbnotifyall(&tcbtable, satosin(sa)->sin_addr, errno,
    449 		    notify);
    450 	return NULL;
    451 }
    452 
    453 /*
    454  * When a source quench is received, close congestion window
    455  * to one segment.  We will gradually open it again as we proceed.
    456  */
    457 void
    458 tcp_quench(inp, errno)
    459 	struct inpcb *inp;
    460 	int errno;
    461 {
    462 	struct tcpcb *tp = intotcpcb(inp);
    463 
    464 	if (tp)
    465 		tp->snd_cwnd = tp->t_maxseg;
    466 }
    467 
    468 /*
    469  * On receipt of path MTU corrections, flush old route and replace it
    470  * with the new one.  Retransmit all unacknowledged packets, to ensure
    471  * that all packets will be received.
    472  */
    473 
    474 void
    475 tcp_mtudisc(inp, errno)
    476 	struct inpcb *inp;
    477 	int errno;
    478 {
    479 	struct tcpcb *tp = intotcpcb(inp);
    480 	struct rtentry *rt = in_pcbrtentry(inp);
    481 
    482 	if (tp != 0) {
    483 		if (rt != 0) {
    484 			/* If this was not a host route, remove and realloc */
    485 
    486 			if ((rt->rt_flags & RTF_HOST) == 0) {
    487 				in_rtchange(inp, errno);
    488 				rtfree(rt);
    489 				if ((rt = in_pcbrtentry(inp)) == 0)
    490 					return;
    491 			}
    492 		}
    493 
    494 		/* Resend unacknowledged packets: */
    495 
    496 		tp->snd_nxt = tp->snd_una;
    497 		tcp_output(tp);
    498 	}
    499 }
    500 
    501 
    502 /*
    503  * Compute the MSS to advertise to the peer.  Called only during
    504  * the 3-way handshake.  If we are the server (peer initiated
    505  * connection), we are called with the TCPCB for the listen
    506  * socket.  If we are the client (we initiated connection), we
    507  * are called witht he TCPCB for the actual connection.
    508  */
    509 int
    510 tcp_mss_to_advertise(tp)
    511 	const struct tcpcb *tp;
    512 {
    513 	extern u_long in_maxmtu;
    514 	struct inpcb *inp;
    515 	struct socket *so;
    516 	int mss;
    517 
    518 	inp = tp->t_inpcb;
    519 	so = inp->inp_socket;
    520 
    521 	/*
    522 	 * In order to avoid defeating path MTU discovery on the peer,
    523 	 * we advertise the max MTU of all attached networks as our MSS,
    524 	 * per RFC 1191, section 3.1.
    525 	 *
    526 	 * XXX Should we allow room for the timestamp option if
    527 	 * XXX rfc1323 is enabled?
    528 	 */
    529 	mss = in_maxmtu - sizeof(struct tcpiphdr);
    530 
    531 	return (mss);
    532 }
    533 
    534 /*
    535  * Set connection variables based on the peer's advertised MSS.
    536  * We are passed the TCPCB for the actual connection.  If we
    537  * are the server, we are called by the compressed state engine
    538  * when the 3-way handshake is complete.  If we are the client,
    539  * we are called when we recieve the SYN,ACK from the server.
    540  *
    541  * NOTE: Our advertised MSS value must be initialized in the TCPCB
    542  * before this routine is called!
    543  */
    544 void
    545 tcp_mss_from_peer(tp, offer)
    546 	struct tcpcb *tp;
    547 	int offer;
    548 {
    549 	struct inpcb *inp = tp->t_inpcb;
    550 	struct socket *so = inp->inp_socket;
    551 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
    552 	struct rtentry *rt = in_pcbrtentry(inp);
    553 #endif
    554 	u_long bufsize;
    555 	int mss;
    556 
    557 	/*
    558 	 * Assume our MSS is the MSS of the peer, unless they sent us
    559 	 * an offer.  Do not accept offers less than 32 bytes.
    560 	 */
    561 	mss = tp->t_ourmss;
    562 	if (offer)
    563 		mss = offer;
    564 	mss = max(mss, 32);		/* sanity */
    565 
    566 	/*
    567 	 * If there's a pipesize, change the socket buffer to that size.
    568 	 * Make the socket buffer an integral number of MSS units.  If
    569 	 * the MSS is larger than the socket buffer, artificially decrease
    570 	 * the MSS.
    571 	 */
    572 #ifdef RTV_SPIPE
    573 	if (rt != NULL && rt->rt_rmx.rmx_sendpipe != 0)
    574 		bufsize = rt->rt_rmx.rmx_sendpipe;
    575 	else
    576 #endif
    577 		bufsize = so->so_snd.sb_hiwat;
    578 	if (bufsize < mss)
    579 		mss = bufsize;
    580 	else {
    581 		bufsize = roundup(bufsize, mss);
    582 		if (bufsize > sb_max)
    583 			bufsize = sb_max;
    584 		(void) sbreserve(&so->so_snd, bufsize);
    585 	}
    586 	tp->t_maxseg = mss;
    587 
    588 	/* Initialize the initial congestion window. */
    589 	tp->snd_cwnd = mss;
    590 
    591 #ifdef RTV_SSTHRESH
    592 	if (rt != NULL && rt->rt_rmx.rmx_ssthresh) {
    593 		/*
    594 		 * There's some sort of gateway or interface buffer
    595 		 * limit on the path.  Use this to set the slow
    596 		 * start threshold, but set the threshold to no less
    597 		 * than 2 * MSS.
    598 		 */
    599 		tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
    600 	}
    601 #endif
    602 }
    603 
    604 /*
    605  * Processing necessary when a TCP connection is established.
    606  */
    607 void
    608 tcp_established(tp)
    609 	struct tcpcb *tp;
    610 {
    611 	struct inpcb *inp = tp->t_inpcb;
    612 	struct socket *so = inp->inp_socket;
    613 #ifdef RTV_RPIPE
    614 	struct rtentry *rt = in_pcbrtentry(inp);
    615 #endif
    616 	u_long bufsize;
    617 
    618 	tp->t_state = TCPS_ESTABLISHED;
    619 	tp->t_timer[TCPT_KEEP] = tcp_keepidle;
    620 
    621 #ifdef RTV_RPIPE
    622 	if (rt != NULL && rt->rt_rmx.rmx_recvpipe != 0)
    623 		bufsize = rt->rt_rmx.rmx_recvpipe;
    624 	else
    625 #endif
    626 		bufsize = so->so_rcv.sb_hiwat;
    627 	if (bufsize > tp->t_ourmss) {
    628 		bufsize = roundup(bufsize, tp->t_ourmss);
    629 		if (bufsize > sb_max)
    630 			bufsize = sb_max;
    631 		(void) sbreserve(&so->so_rcv, bufsize);
    632 	}
    633 }
    634 
    635 /*
    636  * Check if there's an initial rtt or rttvar.  Convert from the
    637  * route-table units to scaled multiples of the slow timeout timer.
    638  * Called only during the 3-way handshake.
    639  */
    640 void
    641 tcp_rmx_rtt(tp)
    642 	struct tcpcb *tp;
    643 {
    644 #ifdef RTV_RTT
    645 	struct rtentry *rt;
    646 	int rtt;
    647 
    648 	if ((rt = in_pcbrtentry(tp->t_inpcb)) == NULL)
    649 		return;
    650 
    651 	if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
    652 		/*
    653 		 * XXX The lock bit for MTU indicates that the value
    654 		 * is also a minimum value; this is subject to time.
    655 		 */
    656 		if (rt->rt_rmx.rmx_locks & RTV_RTT)
    657 			tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ);
    658 		tp->t_srtt = rtt /
    659 		    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
    660 		if (rt->rt_rmx.rmx_rttvar) {
    661 			tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
    662 			    ((RTM_RTTUNIT / PR_SLOWHZ) >>
    663 				(TCP_RTTVAR_SHIFT + 2));
    664 		} else {
    665 			/* Default variation is +- 1 rtt */
    666 			tp->t_rttvar =
    667 			    tp->t_srtt >> (TCP_RTT_SHIFT - TCP_RTTVAR_SHIFT);
    668 		}
    669 		TCPT_RANGESET(tp->t_rxtcur,
    670 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2),
    671 		    tp->t_rttmin, TCPTV_REXMTMAX);
    672 	}
    673 #endif
    674 }
    675 
    676 tcp_seq	 tcp_iss_seq = 0;	/* tcp initial seq # */
    677 
    678 /*
    679  * Get a new sequence value given a tcp control block
    680  */
    681 tcp_seq
    682 tcp_new_iss(tp, len, addin)
    683 	void            *tp;
    684 	u_long           len;
    685 	tcp_seq		 addin;
    686 {
    687 	tcp_seq          tcp_iss;
    688 
    689 	/*
    690 	 * add randomness about this connection, but do not estimate
    691 	 * entropy from the timing, since the physical device driver would
    692 	 * have done that for us.
    693 	 */
    694 #if NRND > 0
    695 	if (tp != NULL)
    696 		rnd_add_data(NULL, tp, len, 0);
    697 #endif
    698 
    699 	/*
    700 	 * randomize.
    701 	 */
    702 #if NRND > 0
    703 	rnd_extract_data(&tcp_iss, sizeof(tcp_iss), RND_EXTRACT_ANY);
    704 #else
    705 	tcp_iss = random();
    706 #endif
    707 
    708 	/*
    709 	 * If we were asked to add some amount to a known value,
    710 	 * we will take a random value obtained above, mask off the upper
    711 	 * bits, and add in the known value.  We also add in a constant to
    712 	 * ensure that we are at least a certain distance from the original
    713 	 * value.
    714 	 *
    715 	 * This is used when an old connection is in timed wait
    716 	 * and we have a new one coming in, for instance.
    717 	 */
    718 	if (addin != 0) {
    719 #ifdef TCPISS_DEBUG
    720 		printf("Random %08x, ", tcp_iss);
    721 #endif
    722 		tcp_iss &= TCP_ISS_RANDOM_MASK;
    723 		tcp_iss = tcp_iss + addin + TCP_ISSINCR;
    724 		tcp_iss_seq += TCP_ISSINCR;
    725 		tcp_iss += tcp_iss_seq;
    726 #ifdef TCPISS_DEBUG
    727 		printf("Old ISS %08x, ISS %08x\n", addin, tcp_iss);
    728 #endif
    729 	} else {
    730 		tcp_iss &= TCP_ISS_RANDOM_MASK;
    731 		tcp_iss_seq += TCP_ISSINCR;
    732 		tcp_iss += tcp_iss_seq;
    733 #ifdef TCPISS_DEBUG
    734 		printf("ISS %08x\n", tcp_iss);
    735 #endif
    736 	}
    737 
    738 #ifdef TCP_COMPAT_42
    739 	/*
    740 	 * limit it to the positive range for really old TCP implementations
    741 	 */
    742 	if ((int)tcp_iss < 0)
    743 		tcp_iss &= 0x7fffffff;		/* XXX */
    744 #endif
    745 
    746 	return tcp_iss;
    747 }
    748