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