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