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