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