Home | History | Annotate | Line # | Download | only in netinet
tcp_subr.c revision 1.30
      1 /*	$NetBSD: tcp_subr.c,v 1.30 1997/10/13 00:48:12 explorer 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_HOSTDEAD)
    433 		ip = 0;
    434 	else if (errno == 0)
    435 		return NULL;
    436 	if (ip) {
    437 		th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
    438 		nmatch = in_pcbnotify(&tcbtable, satosin(sa)->sin_addr,
    439 		    th->th_dport, ip->ip_src, th->th_sport, errno, notify);
    440 		if (nmatch == 0 && syn_cache_count &&
    441 		    (inetctlerrmap[cmd] == EHOSTUNREACH ||
    442 		    inetctlerrmap[cmd] == ENETUNREACH ||
    443 		    inetctlerrmap[cmd] == EHOSTDOWN))
    444 			syn_cache_unreach(ip, th);
    445 	} else
    446 		(void)in_pcbnotifyall(&tcbtable, satosin(sa)->sin_addr, errno,
    447 		    notify);
    448 	return NULL;
    449 }
    450 
    451 /*
    452  * When a source quench is received, close congestion window
    453  * to one segment.  We will gradually open it again as we proceed.
    454  */
    455 void
    456 tcp_quench(inp, errno)
    457 	struct inpcb *inp;
    458 	int errno;
    459 {
    460 	struct tcpcb *tp = intotcpcb(inp);
    461 
    462 	if (tp)
    463 		tp->snd_cwnd = tp->t_maxseg;
    464 }
    465 
    466 /*
    467  * Compute the MSS to advertise to the peer.  Called only during
    468  * the 3-way handshake.  If we are the server (peer initiated
    469  * connection), we are called with the TCPCB for the listen
    470  * socket.  If we are the client (we initiated connection), we
    471  * are called witht he TCPCB for the actual connection.
    472  */
    473 int
    474 tcp_mss_to_advertise(tp)
    475 	const struct tcpcb *tp;
    476 {
    477 	extern u_long in_maxmtu;
    478 	struct inpcb *inp;
    479 	struct socket *so;
    480 	int mss;
    481 
    482 	inp = tp->t_inpcb;
    483 	so = inp->inp_socket;
    484 
    485 	/*
    486 	 * In order to avoid defeating path MTU discovery on the peer,
    487 	 * we advertise the max MTU of all attached networks as our MSS,
    488 	 * per RFC 1191, section 3.1.
    489 	 *
    490 	 * XXX Should we allow room for the timestamp option if
    491 	 * XXX rfc1323 is enabled?
    492 	 */
    493 	mss = in_maxmtu - sizeof(struct tcpiphdr);
    494 
    495 	return (mss);
    496 }
    497 
    498 /*
    499  * Set connection variables based on the peer's advertised MSS.
    500  * We are passed the TCPCB for the actual connection.  If we
    501  * are the server, we are called by the compressed state engine
    502  * when the 3-way handshake is complete.  If we are the client,
    503  * we are called when we recieve the SYN,ACK from the server.
    504  *
    505  * NOTE: Our advertised MSS value must be initialized in the TCPCB
    506  * before this routine is called!
    507  */
    508 void
    509 tcp_mss_from_peer(tp, offer)
    510 	struct tcpcb *tp;
    511 	int offer;
    512 {
    513 	struct inpcb *inp = tp->t_inpcb;
    514 	struct socket *so = inp->inp_socket;
    515 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
    516 	struct rtentry *rt = in_pcbrtentry(inp);
    517 #endif
    518 	u_long bufsize;
    519 	int mss;
    520 
    521 	/*
    522 	 * Assume our MSS is the MSS of the peer, unless they sent us
    523 	 * an offer.  Do not accept offers less than 32 bytes.
    524 	 */
    525 	mss = tp->t_ourmss;
    526 	if (offer)
    527 		mss = offer;
    528 	mss = max(mss, 32);		/* sanity */
    529 
    530 	/*
    531 	 * If there's a pipesize, change the socket buffer to that size.
    532 	 * Make the socket buffer an integral number of MSS units.  If
    533 	 * the MSS is larger than the socket buffer, artificially decrease
    534 	 * the MSS.
    535 	 */
    536 #ifdef RTV_SPIPE
    537 	if (rt != NULL && rt->rt_rmx.rmx_sendpipe != 0)
    538 		bufsize = rt->rt_rmx.rmx_sendpipe;
    539 	else
    540 #endif
    541 		bufsize = so->so_snd.sb_hiwat;
    542 	if (bufsize < mss)
    543 		mss = bufsize;
    544 	else {
    545 		bufsize = roundup(bufsize, mss);
    546 		if (bufsize > sb_max)
    547 			bufsize = sb_max;
    548 		(void) sbreserve(&so->so_snd, bufsize);
    549 	}
    550 	tp->t_maxseg = mss;
    551 
    552 	/* Initialize the initial congestion window. */
    553 	tp->snd_cwnd = mss;
    554 
    555 #ifdef RTV_SSTHRESH
    556 	if (rt != NULL && rt->rt_rmx.rmx_ssthresh) {
    557 		/*
    558 		 * There's some sort of gateway or interface buffer
    559 		 * limit on the path.  Use this to set the slow
    560 		 * start threshold, but set the threshold to no less
    561 		 * than 2 * MSS.
    562 		 */
    563 		tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
    564 	}
    565 #endif
    566 }
    567 
    568 /*
    569  * Processing necessary when a TCP connection is established.
    570  */
    571 void
    572 tcp_established(tp)
    573 	struct tcpcb *tp;
    574 {
    575 	struct inpcb *inp = tp->t_inpcb;
    576 	struct socket *so = inp->inp_socket;
    577 #ifdef RTV_RPIPE
    578 	struct rtentry *rt = in_pcbrtentry(inp);
    579 #endif
    580 	u_long bufsize;
    581 
    582 	tp->t_state = TCPS_ESTABLISHED;
    583 	tp->t_timer[TCPT_KEEP] = tcp_keepidle;
    584 
    585 #ifdef RTV_RPIPE
    586 	if (rt != NULL && rt->rt_rmx.rmx_recvpipe != 0)
    587 		bufsize = rt->rt_rmx.rmx_recvpipe;
    588 	else
    589 #endif
    590 		bufsize = so->so_rcv.sb_hiwat;
    591 	if (bufsize > tp->t_ourmss) {
    592 		bufsize = roundup(bufsize, tp->t_ourmss);
    593 		if (bufsize > sb_max)
    594 			bufsize = sb_max;
    595 		(void) sbreserve(&so->so_rcv, bufsize);
    596 	}
    597 }
    598 
    599 /*
    600  * Check if there's an initial rtt or rttvar.  Convert from the
    601  * route-table units to scaled multiples of the slow timeout timer.
    602  * Called only during the 3-way handshake.
    603  */
    604 void
    605 tcp_rmx_rtt(tp)
    606 	struct tcpcb *tp;
    607 {
    608 #ifdef RTV_RTT
    609 	struct rtentry *rt;
    610 	int rtt;
    611 
    612 	if ((rt = in_pcbrtentry(tp->t_inpcb)) == NULL)
    613 		return;
    614 
    615 	if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
    616 		/*
    617 		 * XXX The lock bit for MTU indicates that the value
    618 		 * is also a minimum value; this is subject to time.
    619 		 */
    620 		if (rt->rt_rmx.rmx_locks & RTV_RTT)
    621 			tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ);
    622 		tp->t_srtt = rtt /
    623 		    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
    624 		if (rt->rt_rmx.rmx_rttvar) {
    625 			tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
    626 			    ((RTM_RTTUNIT / PR_SLOWHZ) >>
    627 				(TCP_RTTVAR_SHIFT + 2));
    628 		} else {
    629 			/* Default variation is +- 1 rtt */
    630 			tp->t_rttvar =
    631 			    tp->t_srtt >> (TCP_RTT_SHIFT - TCP_RTTVAR_SHIFT);
    632 		}
    633 		TCPT_RANGESET(tp->t_rxtcur,
    634 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2),
    635 		    tp->t_rttmin, TCPTV_REXMTMAX);
    636 	}
    637 #endif
    638 }
    639 
    640 tcp_seq	 tcp_iss_seq = 0;	/* tcp initial seq # */
    641 
    642 /*
    643  * Get a new sequence value given a tcp control block
    644  */
    645 tcp_seq
    646 tcp_new_iss(tp, len, addin)
    647 	void            *tp;
    648 	u_long           len;
    649 	tcp_seq		 addin;
    650 {
    651 	tcp_seq          tcp_iss;
    652 
    653 	/*
    654 	 * add randomness about this connection, but do not estimate
    655 	 * entropy from the timing, since the physical device driver would
    656 	 * have done that for us.
    657 	 */
    658 #if NRND > 0
    659 	if (tp != NULL)
    660 		rnd_add_data(NULL, tp, len, 0);
    661 #endif
    662 
    663 	/*
    664 	 * randomize.
    665 	 */
    666 #if NRND > 0
    667 	rnd_extract_data(&tcp_iss, sizeof(tcp_iss), RND_EXTRACT_ANY);
    668 #else
    669 	tcp_iss = random();
    670 #endif
    671 
    672 	/*
    673 	 * If we were asked to add some amount to a known value,
    674 	 * we will take a random value obtained above, mask off the upper
    675 	 * bits, and add in the known value.  We also add in a constant to
    676 	 * ensure that we are at least a certain distance from the original
    677 	 * value.
    678 	 *
    679 	 * This is used when an old connection is in timed wait
    680 	 * and we have a new one coming in, for instance.
    681 	 */
    682 	if (addin != 0) {
    683 #ifdef TCPISS_DEBUG
    684 		printf("Random %08x, ", tcp_iss);
    685 #endif
    686 		tcp_iss &= TCP_ISS_RANDOM_MASK;
    687 		tcp_iss = tcp_iss + addin + TCP_ISSINCR;
    688 		tcp_iss_seq += TCP_ISSINCR;
    689 		tcp_iss += tcp_iss_seq;
    690 #ifdef TCPISS_DEBUG
    691 		printf("Old ISS %08x, ISS %08x\n", addin, tcp_iss);
    692 #endif
    693 	} else {
    694 		tcp_iss &= TCP_ISS_RANDOM_MASK;
    695 		tcp_iss_seq += TCP_ISSINCR;
    696 		tcp_iss += tcp_iss_seq;
    697 #ifdef TCPISS_DEBUG
    698 		printf("ISS %08x\n", tcp_iss);
    699 #endif
    700 	}
    701 
    702 #ifdef TCP_COMPAT_42
    703 	/*
    704 	 * limit it to the positive range for really old TCP implementations
    705 	 */
    706 	if ((int)tcp_iss < 0)
    707 		tcp_iss &= 0x7fffffff;		/* XXX */
    708 #endif
    709 
    710 	return tcp_iss;
    711 }
    712