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