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