Home | History | Annotate | Line # | Download | only in netinet
tcp_subr.c revision 1.68
      1 /*	$NetBSD: tcp_subr.c,v 1.68 1999/07/02 12:45:32 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
     34  * All rights reserved.
     35  *
     36  * This code is derived from software contributed to The NetBSD Foundation
     37  * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
     38  * Facility, NASA Ames Research Center.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. All advertising materials mentioning features or use of this software
     49  *    must display the following acknowledgement:
     50  *	This product includes software developed by the NetBSD
     51  *	Foundation, Inc. and its contributors.
     52  * 4. Neither the name of The NetBSD Foundation nor the names of its
     53  *    contributors may be used to endorse or promote products derived
     54  *    from this software without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     57  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     58  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     59  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     60  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     66  * POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 /*
     70  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
     71  *	The Regents of the University of California.  All rights reserved.
     72  *
     73  * Redistribution and use in source and binary forms, with or without
     74  * modification, are permitted provided that the following conditions
     75  * are met:
     76  * 1. Redistributions of source code must retain the above copyright
     77  *    notice, this list of conditions and the following disclaimer.
     78  * 2. Redistributions in binary form must reproduce the above copyright
     79  *    notice, this list of conditions and the following disclaimer in the
     80  *    documentation and/or other materials provided with the distribution.
     81  * 3. All advertising materials mentioning features or use of this software
     82  *    must display the following acknowledgement:
     83  *	This product includes software developed by the University of
     84  *	California, Berkeley and its contributors.
     85  * 4. Neither the name of the University nor the names of its contributors
     86  *    may be used to endorse or promote products derived from this software
     87  *    without specific prior written permission.
     88  *
     89  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     90  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     91  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     92  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     93  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     94  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     95  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     96  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     97  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     98  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     99  * SUCH DAMAGE.
    100  *
    101  *	@(#)tcp_subr.c	8.2 (Berkeley) 5/24/95
    102  */
    103 
    104 #include "opt_inet.h"
    105 #include "opt_tcp_compat_42.h"
    106 #include "rnd.h"
    107 
    108 #include <sys/param.h>
    109 #include <sys/proc.h>
    110 #include <sys/systm.h>
    111 #include <sys/malloc.h>
    112 #include <sys/mbuf.h>
    113 #include <sys/socket.h>
    114 #include <sys/socketvar.h>
    115 #include <sys/protosw.h>
    116 #include <sys/errno.h>
    117 #include <sys/kernel.h>
    118 #include <sys/pool.h>
    119 #if NRND > 0
    120 #include <sys/rnd.h>
    121 #endif
    122 
    123 #include <net/route.h>
    124 #include <net/if.h>
    125 
    126 #include <netinet/in.h>
    127 #include <netinet/in_systm.h>
    128 #include <netinet/ip.h>
    129 #include <netinet/in_pcb.h>
    130 #include <netinet/ip_var.h>
    131 #include <netinet/ip_icmp.h>
    132 
    133 #ifdef INET6
    134 #ifndef INET
    135 #include <netinet/in.h>
    136 #endif
    137 #include <netinet/ip6.h>
    138 #include <netinet6/in6_pcb.h>
    139 #include <netinet6/ip6_var.h>
    140 #endif
    141 
    142 #include <netinet/tcp.h>
    143 #include <netinet/tcp_fsm.h>
    144 #include <netinet/tcp_seq.h>
    145 #include <netinet/tcp_timer.h>
    146 #include <netinet/tcp_var.h>
    147 #include <netinet/tcpip.h>
    148 
    149 #ifdef IPSEC
    150 #include <netinet6/ipsec.h>
    151 #include <netinet6/ah.h>
    152 #ifdef IPSEC_ESP
    153 #include <netinet6/esp.h>
    154 #endif
    155 #endif /*IPSEC*/
    156 
    157 #ifdef INET6
    158 struct in6pcb tcb6;
    159 #endif
    160 
    161 /* patchable/settable parameters for tcp */
    162 int 	tcp_mssdflt = TCP_MSS;
    163 int 	tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
    164 int	tcp_do_rfc1323 = 1;	/* window scaling / timestamps (obsolete) */
    165 int	tcp_do_sack = 1;	/* selective acknowledgement */
    166 int	tcp_do_win_scale = 1;	/* RFC1323 window scaling */
    167 int	tcp_do_timestamps = 1;	/* RFC1323 timestamps */
    168 int	tcp_do_newreno = 0;	/* Use the New Reno algorithms */
    169 int	tcp_ack_on_push = 0;	/* set to enable immediate ACK-on-PUSH */
    170 int	tcp_init_win = 1;
    171 int	tcp_mss_ifmtu = 0;
    172 #ifdef TCP_COMPAT_42
    173 int	tcp_compat_42 = 1;
    174 #else
    175 int	tcp_compat_42 = 0;
    176 #endif
    177 
    178 #ifndef TCBHASHSIZE
    179 #define	TCBHASHSIZE	128
    180 #endif
    181 int	tcbhashsize = TCBHASHSIZE;
    182 
    183 int	tcp_freeq __P((struct tcpcb *));
    184 
    185 struct pool tcpcb_pool;
    186 
    187 /*
    188  * Tcp initialization
    189  */
    190 void
    191 tcp_init()
    192 {
    193 	int hlen;
    194 
    195 	pool_init(&tcpcb_pool, sizeof(struct tcpcb), 0, 0, 0, "tcpcbpl",
    196 	    0, NULL, NULL, M_PCB);
    197 	in_pcbinit(&tcbtable, tcbhashsize, tcbhashsize);
    198 #ifdef INET6
    199 	tcb6.in6p_next = tcb6.in6p_prev = &tcb6;
    200 #endif
    201 	LIST_INIT(&tcp_delacks);
    202 
    203 	hlen = sizeof(struct ip) + sizeof(struct tcphdr);
    204 #ifdef INET6
    205 	if (sizeof(struct ip) < sizeof(struct ip6_hdr))
    206 		hlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
    207 #endif
    208 	if (max_protohdr < hlen)
    209 		max_protohdr = hlen;
    210 	if (max_linkhdr + hlen > MHLEN)
    211 		panic("tcp_init");
    212 
    213 	/* Initialize the compressed state engine. */
    214 	syn_cache_init();
    215 }
    216 
    217 /*
    218  * Create template to be used to send tcp packets on a connection.
    219  * Call after host entry created, allocates an mbuf and fills
    220  * in a skeletal tcp/ip header, minimizing the amount of work
    221  * necessary when the connection is used.
    222  */
    223 struct mbuf *
    224 tcp_template(tp)
    225 	struct tcpcb *tp;
    226 {
    227 	register struct inpcb *inp = tp->t_inpcb;
    228 #ifdef INET6
    229 	register struct in6pcb *in6p = tp->t_in6pcb;
    230 #endif
    231 	register struct tcphdr *n;
    232 	register struct mbuf *m;
    233 	int hlen;
    234 
    235 	switch (tp->t_family) {
    236 	case AF_INET:
    237 		hlen = sizeof(struct ip);
    238 		if (inp)
    239 			break;
    240 #ifdef INET6
    241 		if (in6p) {
    242 			/* mapped addr case */
    243 			if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
    244 			 && IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
    245 				break;
    246 		}
    247 #endif
    248 		return NULL;	/*EINVAL*/
    249 #ifdef INET6
    250 	case AF_INET6:
    251 		hlen = sizeof(struct ip6_hdr);
    252 		if (in6p) {
    253 			/* more sainty check? */
    254 			break;
    255 		}
    256 		return NULL;	/*EINVAL*/
    257 #endif
    258 	default:
    259 		hlen = 0;	/*pacify gcc*/
    260 		return NULL;	/*EAFNOSUPPORT*/
    261 	}
    262 	if ((m = tp->t_template) == 0) {
    263 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    264 		if (m) {
    265 			MCLGET(m, M_DONTWAIT);
    266 			if ((m->m_flags & M_EXT) == 0) {
    267 				m_free(m);
    268 				m = NULL;
    269 			}
    270 		}
    271 		if (m == NULL)
    272 			return NULL;
    273 		m->m_len = hlen + sizeof(struct tcphdr);
    274 	}
    275 	bzero(mtod(m, caddr_t), m->m_len);
    276 	switch (tp->t_family) {
    277 	case AF_INET:
    278 	    {
    279 		struct ipovly *ipov;
    280 		mtod(m, struct ip *)->ip_v = 4;
    281 		ipov = mtod(m, struct ipovly *);
    282 		ipov->ih_pr = IPPROTO_TCP;
    283 		ipov->ih_len = htons(sizeof(struct tcphdr));
    284 		if (inp) {
    285 			ipov->ih_src = inp->inp_laddr;
    286 			ipov->ih_dst = inp->inp_faddr;
    287 		}
    288 #ifdef INET6
    289 		else if (in6p) {
    290 			/* mapped addr case */
    291 			bcopy(&in6p->in6p_laddr.s6_addr32[3], &ipov->ih_src,
    292 				sizeof(ipov->ih_src));
    293 			bcopy(&in6p->in6p_faddr.s6_addr32[3], &ipov->ih_dst,
    294 				sizeof(ipov->ih_dst));
    295 		}
    296 #endif
    297 		break;
    298 	    }
    299 #ifdef INET6
    300 	case AF_INET6:
    301 	    {
    302 		struct ip6_hdr *ip6;
    303 		mtod(m, struct ip *)->ip_v = 6;
    304 		ip6 = mtod(m, struct ip6_hdr *);
    305 		ip6->ip6_nxt = IPPROTO_TCP;
    306 		ip6->ip6_plen = htons(sizeof(struct tcphdr));
    307 		ip6->ip6_src = in6p->in6p_laddr;
    308 		ip6->ip6_dst = in6p->in6p_faddr;
    309 		ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
    310 		if (ip6_auto_flowlabel) {
    311 			ip6->ip6_flow &= ~IPV6_FLOWLABEL_MASK;
    312 			ip6->ip6_flow |=
    313 				(htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
    314 		}
    315 		ip6->ip6_vfc = IPV6_VERSION;
    316 		break;
    317 	    }
    318 #endif
    319 	}
    320 	n = (struct tcphdr *)(mtod(m, caddr_t) + hlen);
    321 	if (inp) {
    322 		n->th_sport = inp->inp_lport;
    323 		n->th_dport = inp->inp_fport;
    324 	}
    325 #ifdef INET6
    326 	else if (in6p) {
    327 		n->th_sport = in6p->in6p_lport;
    328 		n->th_dport = in6p->in6p_fport;
    329 	}
    330 #endif
    331 	n->th_seq = 0;
    332 	n->th_ack = 0;
    333 	n->th_x2 = 0;
    334 	n->th_off = 5;
    335 	n->th_flags = 0;
    336 	n->th_win = 0;
    337 	n->th_sum = 0;
    338 	n->th_urp = 0;
    339 	return (m);
    340 }
    341 
    342 /*
    343  * Send a single message to the TCP at address specified by
    344  * the given TCP/IP header.  If m == 0, then we make a copy
    345  * of the tcpiphdr at ti and send directly to the addressed host.
    346  * This is used to force keep alive messages out using the TCP
    347  * template for a connection tp->t_template.  If flags are given
    348  * then we send a message back to the TCP which originated the
    349  * segment ti, and discard the mbuf containing it and any other
    350  * attached mbufs.
    351  *
    352  * In any case the ack and sequence number of the transmitted
    353  * segment are as specified by the parameters.
    354  */
    355 int
    356 tcp_respond(tp, template, m, ack, seq, flags)
    357 	struct tcpcb *tp;
    358 	struct mbuf *template;		/* XXX should be struct mbuf? */
    359 	register struct mbuf *m;
    360 	tcp_seq ack, seq;
    361 	int flags;
    362 {
    363 #ifndef INET6
    364 	struct route iproute;
    365 #else
    366 	struct route_in6 iproute;	/* sizeof(route_in6) > sizeof(route) */
    367 #endif
    368 	struct route *ro;
    369 	struct rtentry *rt;
    370 	int error, tlen, win = 0;
    371 	int hlen;
    372 	struct ip *ip;
    373 #ifdef INET6
    374 	struct ip6_hdr *ip6;
    375 #endif
    376 	int family;	/* family on packet, not inpcb/in6pcb! */
    377 	struct tcphdr *th;
    378 
    379 	if (tp != NULL && (flags & TH_RST) == 0) {
    380 		if (tp->t_inpcb)
    381 			win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
    382 #ifdef INET6
    383 		else if (tp->t_in6pcb)
    384 			win = sbspace(&tp->t_in6pcb->in6p_socket->so_rcv);
    385 #endif
    386 	}
    387 
    388 	ip = NULL;
    389 #ifdef INET6
    390 	ip6 = NULL;
    391 #endif
    392 	if (m == 0) {
    393 		if (template
    394 		 && template->m_len < hlen + sizeof(struct tcphdr)) {
    395 			if (m)
    396 				m_freem(m);
    397 			return EINVAL;
    398 		}
    399 
    400 		/* get family information from template */
    401 		switch (mtod(template, struct ip *)->ip_v) {
    402 		case 4:
    403 			family = AF_INET;
    404 			hlen = sizeof(struct ip);
    405 			break;
    406 #ifdef INET6
    407 		case 6:
    408 			family = AF_INET6;
    409 			hlen = sizeof(struct ip6_hdr);
    410 			break;
    411 #endif
    412 		default:
    413 			if (m)
    414 				m_freem(m);
    415 			return EAFNOSUPPORT;
    416 		}
    417 
    418 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    419 		if (m) {
    420 			MCLGET(m, M_DONTWAIT);
    421 			if ((m->m_flags & M_EXT)) {
    422 				m_free(m);
    423 				m = NULL;
    424 			}
    425 		}
    426 		if (m == NULL)
    427 			return (ENOBUFS);
    428 
    429 		if (tcp_compat_42)
    430 			tlen = 1;
    431 		else
    432 			tlen = 0;
    433 
    434 		m->m_data += max_linkhdr;
    435 		bcopy(mtod(template, caddr_t), mtod(m, caddr_t),
    436 			template->m_len);
    437 		switch (family) {
    438 		case AF_INET:
    439 			ip = mtod(m, struct ip *);
    440 			th = (struct tcphdr *)(ip + 1);
    441 			break;
    442 #ifdef INET6
    443 		case AF_INET6:
    444 			ip6 = mtod(m, struct ip6_hdr *);
    445 			th = (struct tcphdr *)(ip6 + 1);
    446 			break;
    447 #endif
    448 		default:	/*pacify gcc*/
    449 			ip = NULL;
    450 			ip6 = NULL;
    451 			th = NULL;
    452 			break;
    453 		}
    454 		flags = TH_ACK;
    455 	} else {
    456 		/* get family information from m */
    457 		switch (mtod(m, struct ip *)->ip_v) {
    458 		case 4:
    459 			family = AF_INET;
    460 			hlen = sizeof(struct ip);
    461 			break;
    462 #ifdef INET6
    463 		case 6:
    464 			family = AF_INET6;
    465 			hlen = sizeof(struct ip6_hdr);
    466 			break;
    467 #endif
    468 		default:
    469 			if (m)
    470 				m_freem(m);
    471 			return EAFNOSUPPORT;
    472 		}
    473 
    474 		/* template pointer almost has no meaning */
    475 		m_freem(m->m_next);
    476 		m->m_next = 0;
    477 		m->m_len = hlen + sizeof(struct tcphdr);
    478 		if ((m->m_flags & M_PKTHDR) == 0) {
    479 			printf("non PKTHDR to tcp_respond\n");
    480 			m_freem(m);
    481 			return EINVAL;
    482 		}
    483 
    484 		tlen = 0;
    485 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
    486 		switch (family) {
    487 		case AF_INET:
    488 			ip = mtod(m, struct ip *);
    489 			th = (struct tcphdr *)(ip + 1);
    490 			xchg(ip->ip_dst, ip->ip_src, struct in_addr);
    491 			break;
    492 #ifdef INET6
    493 		case AF_INET6:
    494 			ip6 = mtod(m, struct ip6_hdr *);
    495 			th = (struct tcphdr *)(ip6 + 1);
    496 			xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr);
    497 			break;
    498 #endif
    499 		}
    500 		xchg(th->th_dport, th->th_sport, u_int16_t);
    501 #undef xchg
    502 	}
    503 	th->th_seq = htonl(seq);
    504 	th->th_ack = htonl(ack);
    505 	th->th_x2 = 0;
    506 	if ((flags & TH_SYN) == 0) {
    507 		if (tp)
    508 			th->th_win = htons((u_int16_t) (win >> tp->rcv_scale));
    509 		else
    510 			th->th_win = htons((u_int16_t)win);
    511 		th->th_off = sizeof (struct tcphdr) >> 2;
    512 		tlen += sizeof (struct tcphdr);
    513 	} else
    514 		tlen += th->th_off << 2;
    515 	m->m_len = hlen + tlen;
    516 	m->m_pkthdr.len = hlen + tlen;
    517 	m->m_pkthdr.rcvif = (struct ifnet *) 0;
    518 	th->th_flags = flags;
    519 	th->th_urp = 0;
    520 
    521 	switch (family) {
    522 	case AF_INET:
    523 	    {
    524 		struct ipovly *ipov = (struct ipovly *)ip;
    525 		bzero(ipov->ih_x1, sizeof ipov->ih_x1);
    526 		ipov->ih_len = htons((u_int16_t)tlen);
    527 
    528 		th->th_sum = 0;
    529 		th->th_sum = in_cksum(m, hlen + tlen);
    530 		ip->ip_len = hlen + tlen;	/*will be flipped on output*/
    531 		ip->ip_ttl = ip_defttl;
    532 		break;
    533 	    }
    534 #ifdef INET6
    535 	case AF_INET6:
    536 	    {
    537 		th->th_sum = 0;
    538 		th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
    539 				tlen);
    540 		ip6->ip6_plen = ntohs(tlen);
    541 		ip6->ip6_hlim = ip6_defhlim;
    542 		ip6->ip6_flow &= ~IPV6_FLOWINFO_MASK;
    543 		if (ip6_auto_flowlabel) {
    544 			ip6->ip6_flow |=
    545 				(htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
    546 		}
    547 		break;
    548 	    }
    549 #endif
    550 	}
    551 
    552 #ifdef IPSEC
    553 	m->m_pkthdr.rcvif = NULL;
    554 #endif /*IPSEC*/
    555 
    556 	/*
    557 	 * If we're doing Path MTU discovery, we need to set DF unless
    558 	 * the route's MTU is locked.  If we lack a route, we need to
    559 	 * look it up now.
    560 	 *
    561 	 * ip_output() could do this for us, but it's convenient to just
    562 	 * do it here unconditionally.
    563 	 */
    564 	if (tp != NULL && tp->t_inpcb != NULL) {
    565 		ro = &tp->t_inpcb->inp_route;
    566 #ifdef IPSEC
    567 		m->m_pkthdr.rcvif = (struct ifnet *)tp->t_inpcb->inp_socket;
    568 #endif
    569 #ifdef DIAGNOSTIC
    570 		if (family != AF_INET)
    571 			panic("tcp_respond: address family mismatch");
    572 		if (!in_hosteq(ip->ip_dst, tp->t_inpcb->inp_faddr)) {
    573 			panic("tcp_respond: ip_dst %x != inp_faddr %x",
    574 			    ntohl(ip->ip_dst.s_addr),
    575 			    ntohl(tp->t_inpcb->inp_faddr.s_addr));
    576 		}
    577 #endif
    578 	}
    579 #ifdef INET6
    580 	else if (tp != NULL && tp->t_in6pcb != NULL) {
    581 		ro = (struct route *)&tp->t_in6pcb->in6p_route;
    582 #ifdef IPSEC
    583 		m->m_pkthdr.rcvif = (struct ifnet *)tp->t_in6pcb->in6p_socket;
    584 #endif
    585 #ifdef DIAGNOSTIC
    586 		if (family == AF_INET) {
    587 			if (!IN6_IS_ADDR_V4MAPPED(&tp->t_in6pcb->in6p_faddr))
    588 				panic("tcp_respond: not mapped addr");
    589 			if (bcmp(&ip->ip_dst,
    590 					&tp->t_in6pcb->in6p_faddr.s6_addr32[3],
    591 					sizeof(ip->ip_dst)) != 0) {
    592 				panic("tcp_respond: ip_dst != in6p_faddr");
    593 			}
    594 		} else if (family == AF_INET6) {
    595 			if (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &tp->t_in6pcb->in6p_faddr))
    596 				panic("tcp_respond: ip6_dst != in6p_faddr");
    597 		} else
    598 			panic("tcp_respond: address family mismatch");
    599 #endif
    600 	}
    601 #endif
    602 	else {
    603 		ro = (struct route *)&iproute;
    604 		bzero(ro, sizeof(iproute));
    605 	}
    606 	if ((rt = ro->ro_rt) == NULL || (rt->rt_flags & RTF_UP) == 0) {
    607 		if (ro->ro_rt != NULL) {
    608 			RTFREE(ro->ro_rt);
    609 			ro->ro_rt = NULL;
    610 		}
    611 		switch (family) {
    612 		case AF_INET:
    613 		    {
    614 			struct sockaddr_in *dst;
    615 			dst = satosin(&ro->ro_dst);
    616 			dst->sin_family = AF_INET;
    617 			dst->sin_len = sizeof(*dst);
    618 			dst->sin_addr = ip->ip_dst;
    619 			break;
    620 		    }
    621 #ifdef INET6
    622 		case AF_INET6:
    623 		    {
    624 			struct sockaddr_in6 *dst;
    625 			dst = satosin6(&ro->ro_dst);
    626 			bzero(dst, sizeof(*dst));
    627 			dst->sin6_family = AF_INET6;
    628 			dst->sin6_len = sizeof(*dst);
    629 			dst->sin6_addr = ip6->ip6_dst;
    630 			break;
    631 		    }
    632 #endif
    633 		}
    634 		rtalloc(ro);
    635 		if ((rt = ro->ro_rt) == NULL) {
    636 			m_freem(m);
    637 			switch (family) {
    638 			case AF_INET:
    639 				ipstat.ips_noroute++;
    640 				break;
    641 #ifdef INET6
    642 			case AF_INET6:
    643 				ip6stat.ip6s_noroute++;
    644 				break;
    645 #endif
    646 			}
    647 			return (EHOSTUNREACH);
    648 		}
    649 	}
    650 	switch (family) {
    651 	case AF_INET:
    652 		if (ip_mtudisc != 0 && (rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
    653 			ip->ip_off |= IP_DF;
    654 
    655 		error = ip_output(m, NULL, ro, 0, NULL);
    656 		break;
    657 #ifdef INET6
    658 	case AF_INET6:
    659 		error = ip6_output(m, NULL, (struct route_in6 *)ro, 0, NULL);
    660 		break;
    661 #endif
    662 	default:
    663 		error = EAFNOSUPPORT;
    664 		break;
    665 	}
    666 
    667 	if (ro == (struct route *)&iproute) {
    668 		RTFREE(ro->ro_rt);
    669 		ro->ro_rt = NULL;
    670 	}
    671 
    672 	return (error);
    673 }
    674 
    675 /*
    676  * Create a new TCP control block, making an
    677  * empty reassembly queue and hooking it to the argument
    678  * protocol control block.
    679  */
    680 struct tcpcb *
    681 tcp_newtcpcb(family, aux)
    682 	int family;	/* selects inpcb, or in6pcb */
    683 	void *aux;
    684 {
    685 	register struct tcpcb *tp;
    686 
    687 	switch (family) {
    688 	case PF_INET:
    689 		break;
    690 #ifdef INET6
    691 	case PF_INET6:
    692 		break;
    693 #endif
    694 	default:
    695 		return NULL;
    696 	}
    697 
    698 	tp = pool_get(&tcpcb_pool, PR_NOWAIT);
    699 	if (tp == NULL)
    700 		return (NULL);
    701 	bzero((caddr_t)tp, sizeof(struct tcpcb));
    702 	LIST_INIT(&tp->segq);
    703 	LIST_INIT(&tp->timeq);
    704 	tp->t_family = family;		/* may be overridden later on */
    705 	tp->t_peermss = tcp_mssdflt;
    706 	tp->t_ourmss = tcp_mssdflt;
    707 	tp->t_segsz = tcp_mssdflt;
    708 
    709 	tp->t_flags = 0;
    710 	if (tcp_do_rfc1323 && tcp_do_win_scale)
    711 		tp->t_flags |= TF_REQ_SCALE;
    712 	if (tcp_do_rfc1323 && tcp_do_timestamps)
    713 		tp->t_flags |= TF_REQ_TSTMP;
    714 	if (tcp_do_sack == 2)
    715 		tp->t_flags |= TF_WILL_SACK;
    716 	else if (tcp_do_sack == 1)
    717 		tp->t_flags |= TF_WILL_SACK|TF_IGNR_RXSACK;
    718 	tp->t_flags |= TF_CANT_TXSACK;
    719 	switch (family) {
    720 	case PF_INET:
    721 		tp->t_inpcb = (struct inpcb *)aux;
    722 		break;
    723 #ifdef INET6
    724 	case PF_INET6:
    725 		tp->t_in6pcb = (struct in6pcb *)aux;
    726 		break;
    727 #endif
    728 	}
    729 	/*
    730 	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
    731 	 * rtt estimate.  Set rttvar so that srtt + 2 * rttvar gives
    732 	 * reasonable initial retransmit time.
    733 	 */
    734 	tp->t_srtt = TCPTV_SRTTBASE;
    735 	tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << (TCP_RTTVAR_SHIFT + 2 - 1);
    736 	tp->t_rttmin = TCPTV_MIN;
    737 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
    738 	    TCPTV_MIN, TCPTV_REXMTMAX);
    739 	tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
    740 	tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
    741 	if (family == AF_INET) {
    742 		struct inpcb *inp = (struct inpcb *)aux;
    743 		inp->inp_ip.ip_ttl = ip_defttl;
    744 		inp->inp_ppcb = (caddr_t)tp;
    745 	}
    746 #ifdef INET6
    747 	else if (family == AF_INET6) {
    748 		struct in6pcb *in6p = (struct in6pcb *)aux;
    749 		in6p->in6p_ip6.ip6_hlim = ip6_defhlim;
    750 		in6p->in6p_ppcb = (caddr_t)tp;
    751 	}
    752 #endif
    753 	return (tp);
    754 }
    755 
    756 /*
    757  * Drop a TCP connection, reporting
    758  * the specified error.  If connection is synchronized,
    759  * then send a RST to peer.
    760  */
    761 struct tcpcb *
    762 tcp_drop(tp, errno)
    763 	register struct tcpcb *tp;
    764 	int errno;
    765 {
    766 	struct socket *so;
    767 
    768 	if (tp->t_inpcb)
    769 		so = tp->t_inpcb->inp_socket;
    770 #ifdef INET6
    771 	else if (tp->t_in6pcb)
    772 		so = tp->t_in6pcb->in6p_socket;
    773 #endif
    774 	else
    775 		return NULL;
    776 
    777 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
    778 		tp->t_state = TCPS_CLOSED;
    779 		(void) tcp_output(tp);
    780 		tcpstat.tcps_drops++;
    781 	} else
    782 		tcpstat.tcps_conndrops++;
    783 	if (errno == ETIMEDOUT && tp->t_softerror)
    784 		errno = tp->t_softerror;
    785 	so->so_error = errno;
    786 	return (tcp_close(tp));
    787 }
    788 
    789 /*
    790  * Close a TCP control block:
    791  *	discard all space held by the tcp
    792  *	discard internet protocol block
    793  *	wake up any sleepers
    794  */
    795 struct tcpcb *
    796 tcp_close(tp)
    797 	register struct tcpcb *tp;
    798 {
    799 	struct inpcb *inp;
    800 #ifdef INET6
    801 	struct in6pcb *in6p;
    802 #endif
    803 	struct socket *so;
    804 #ifdef RTV_RTT
    805 	register struct rtentry *rt;
    806 #endif
    807 	struct route *ro;
    808 
    809 	inp = tp->t_inpcb;
    810 #ifdef INET6
    811 	in6p = tp->t_in6pcb;
    812 #endif
    813 	so = NULL;
    814 	ro = NULL;
    815 	if (inp) {
    816 		so = inp->inp_socket;
    817 		ro = &inp->inp_route;
    818 	}
    819 #ifdef INET6
    820 	else if (in6p) {
    821 		so = in6p->in6p_socket;
    822 		ro = (struct route *)&in6p->in6p_route;
    823 	}
    824 #endif
    825 
    826 #ifdef RTV_RTT
    827 	/*
    828 	 * If we sent enough data to get some meaningful characteristics,
    829 	 * save them in the routing entry.  'Enough' is arbitrarily
    830 	 * defined as the sendpipesize (default 4K) * 16.  This would
    831 	 * give us 16 rtt samples assuming we only get one sample per
    832 	 * window (the usual case on a long haul net).  16 samples is
    833 	 * enough for the srtt filter to converge to within 5% of the correct
    834 	 * value; fewer samples and we could save a very bogus rtt.
    835 	 *
    836 	 * Don't update the default route's characteristics and don't
    837 	 * update anything that the user "locked".
    838 	 */
    839 	if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) &&
    840 	    ro && (rt = ro->ro_rt) &&
    841 	    !in_nullhost(satosin(rt_key(rt))->sin_addr)) {
    842 		register u_long i = 0;
    843 
    844 		if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
    845 			i = tp->t_srtt *
    846 			    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
    847 			if (rt->rt_rmx.rmx_rtt && i)
    848 				/*
    849 				 * filter this update to half the old & half
    850 				 * the new values, converting scale.
    851 				 * See route.h and tcp_var.h for a
    852 				 * description of the scaling constants.
    853 				 */
    854 				rt->rt_rmx.rmx_rtt =
    855 				    (rt->rt_rmx.rmx_rtt + i) / 2;
    856 			else
    857 				rt->rt_rmx.rmx_rtt = i;
    858 		}
    859 		if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
    860 			i = tp->t_rttvar *
    861 			    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTTVAR_SHIFT + 2));
    862 			if (rt->rt_rmx.rmx_rttvar && i)
    863 				rt->rt_rmx.rmx_rttvar =
    864 				    (rt->rt_rmx.rmx_rttvar + i) / 2;
    865 			else
    866 				rt->rt_rmx.rmx_rttvar = i;
    867 		}
    868 		/*
    869 		 * update the pipelimit (ssthresh) if it has been updated
    870 		 * already or if a pipesize was specified & the threshhold
    871 		 * got below half the pipesize.  I.e., wait for bad news
    872 		 * before we start updating, then update on both good
    873 		 * and bad news.
    874 		 */
    875 		if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
    876 		    (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh) ||
    877 		    i < (rt->rt_rmx.rmx_sendpipe / 2)) {
    878 			/*
    879 			 * convert the limit from user data bytes to
    880 			 * packets then to packet data bytes.
    881 			 */
    882 			i = (i + tp->t_segsz / 2) / tp->t_segsz;
    883 			if (i < 2)
    884 				i = 2;
    885 			i *= (u_long)(tp->t_segsz + sizeof (struct tcpiphdr));
    886 			if (rt->rt_rmx.rmx_ssthresh)
    887 				rt->rt_rmx.rmx_ssthresh =
    888 				    (rt->rt_rmx.rmx_ssthresh + i) / 2;
    889 			else
    890 				rt->rt_rmx.rmx_ssthresh = i;
    891 		}
    892 	}
    893 #endif /* RTV_RTT */
    894 	/* free the reassembly queue, if any */
    895 	TCP_REASS_LOCK(tp);
    896 	(void) tcp_freeq(tp);
    897 	TCP_REASS_UNLOCK(tp);
    898 
    899 	TCP_CLEAR_DELACK(tp);
    900 
    901 	if (tp->t_template) {
    902 		m_free(tp->t_template);
    903 		tp->t_template = NULL;
    904 	}
    905 	pool_put(&tcpcb_pool, tp);
    906 	if (inp) {
    907 		inp->inp_ppcb = 0;
    908 		soisdisconnected(so);
    909 		in_pcbdetach(inp);
    910 	}
    911 #ifdef INET6
    912 	else if (in6p) {
    913 		in6p->in6p_ppcb = 0;
    914 		soisdisconnected(so);
    915 		in6_pcbdetach(in6p);
    916 	}
    917 #endif
    918 	tcpstat.tcps_closed++;
    919 	return ((struct tcpcb *)0);
    920 }
    921 
    922 int
    923 tcp_freeq(tp)
    924 	struct tcpcb *tp;
    925 {
    926 	register struct ipqent *qe;
    927 	int rv = 0;
    928 #ifdef TCPREASS_DEBUG
    929 	int i = 0;
    930 #endif
    931 
    932 	TCP_REASS_LOCK_CHECK(tp);
    933 
    934 	while ((qe = tp->segq.lh_first) != NULL) {
    935 #ifdef TCPREASS_DEBUG
    936 		printf("tcp_freeq[%p,%d]: %u:%u(%u) 0x%02x\n",
    937 			tp, i++, qe->ipqe_seq, qe->ipqe_seq + qe->ipqe_len,
    938 			qe->ipqe_len, qe->ipqe_flags & (TH_SYN|TH_FIN|TH_RST));
    939 #endif
    940 		LIST_REMOVE(qe, ipqe_q);
    941 		LIST_REMOVE(qe, ipqe_timeq);
    942 		m_freem(qe->ipqe_m);
    943 		pool_put(&ipqent_pool, qe);
    944 		rv = 1;
    945 	}
    946 	return (rv);
    947 }
    948 
    949 /*
    950  * Protocol drain routine.  Called when memory is in short supply.
    951  */
    952 void
    953 tcp_drain()
    954 {
    955 	register struct inpcb *inp;
    956 	register struct tcpcb *tp;
    957 
    958 	/*
    959 	 * Free the sequence queue of all TCP connections.
    960 	 */
    961 	inp = tcbtable.inpt_queue.cqh_first;
    962 	if (inp)						/* XXX */
    963 	for (; inp != (struct inpcb *)&tcbtable.inpt_queue;
    964 	    inp = inp->inp_queue.cqe_next) {
    965 		if ((tp = intotcpcb(inp)) != NULL) {
    966 			/*
    967 			 * We may be called from a device's interrupt
    968 			 * context.  If the tcpcb is already busy,
    969 			 * just bail out now.
    970 			 */
    971 			if (tcp_reass_lock_try(tp) == 0)
    972 				continue;
    973 			if (tcp_freeq(tp))
    974 				tcpstat.tcps_connsdrained++;
    975 			TCP_REASS_UNLOCK(tp);
    976 		}
    977 	}
    978 }
    979 
    980 /*
    981  * Notify a tcp user of an asynchronous error;
    982  * store error as soft error, but wake up user
    983  * (for now, won't do anything until can select for soft error).
    984  */
    985 void
    986 tcp_notify(inp, error)
    987 	struct inpcb *inp;
    988 	int error;
    989 {
    990 	register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
    991 	register struct socket *so = inp->inp_socket;
    992 
    993 	/*
    994 	 * Ignore some errors if we are hooked up.
    995 	 * If connection hasn't completed, has retransmitted several times,
    996 	 * and receives a second error, give up now.  This is better
    997 	 * than waiting a long time to establish a connection that
    998 	 * can never complete.
    999 	 */
   1000 	if (tp->t_state == TCPS_ESTABLISHED &&
   1001 	     (error == EHOSTUNREACH || error == ENETUNREACH ||
   1002 	      error == EHOSTDOWN)) {
   1003 		return;
   1004 	} else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
   1005 	    tp->t_rxtshift > 3 && tp->t_softerror)
   1006 		so->so_error = error;
   1007 	else
   1008 		tp->t_softerror = error;
   1009 	wakeup((caddr_t) &so->so_timeo);
   1010 	sorwakeup(so);
   1011 	sowwakeup(so);
   1012 }
   1013 
   1014 #if defined(INET6) && !defined(TCP6)
   1015 void
   1016 tcp6_ctlinput(cmd, sa, ip6, m, off)
   1017 	int cmd;
   1018 	struct sockaddr *sa;
   1019 	register struct ip6_hdr *ip6;
   1020 	struct mbuf *m;
   1021 	int off;
   1022 {
   1023 	(void)tcp_ctlinput(cmd, sa, (void *)ip6);
   1024 }
   1025 #endif
   1026 
   1027 /* assumes that ip header and tcp header are contiguous on mbuf */
   1028 void *
   1029 tcp_ctlinput(cmd, sa, v)
   1030 	int cmd;
   1031 	struct sockaddr *sa;
   1032 	register void *v;
   1033 {
   1034 	register struct ip *ip = v;
   1035 	register struct tcphdr *th;
   1036 	extern int inetctlerrmap[];
   1037 	void (*notify) __P((struct inpcb *, int)) = tcp_notify;
   1038 	int errno;
   1039 	int nmatch;
   1040 
   1041 	if ((unsigned)cmd >= PRC_NCMDS)
   1042 		return NULL;
   1043 	errno = inetctlerrmap[cmd];
   1044 	if (cmd == PRC_QUENCH)
   1045 		notify = tcp_quench;
   1046 	else if (PRC_IS_REDIRECT(cmd))
   1047 		notify = in_rtchange, ip = 0;
   1048 	else if (cmd == PRC_MSGSIZE && ip_mtudisc)
   1049 		notify = tcp_mtudisc, ip = 0;
   1050 	else if (cmd == PRC_HOSTDEAD)
   1051 		ip = 0;
   1052 	else if (errno == 0)
   1053 		return NULL;
   1054 	if (ip && ip->ip_v == 4 && sa->sa_family == AF_INET) {
   1055 		th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
   1056 		nmatch = in_pcbnotify(&tcbtable, satosin(sa)->sin_addr,
   1057 		    th->th_dport, ip->ip_src, th->th_sport, errno, notify);
   1058 		if (nmatch == 0 && syn_cache_count &&
   1059 		    (inetctlerrmap[cmd] == EHOSTUNREACH ||
   1060 		    inetctlerrmap[cmd] == ENETUNREACH ||
   1061 		    inetctlerrmap[cmd] == EHOSTDOWN)) {
   1062 			struct sockaddr_in sin;
   1063 			bzero(&sin, sizeof(sin));
   1064 			sin.sin_len = sizeof(sin);
   1065 			sin.sin_family = AF_INET;
   1066 			sin.sin_port = th->th_sport;
   1067 			sin.sin_addr = ip->ip_src;
   1068 			syn_cache_unreach((struct sockaddr *)&sin, sa, th);
   1069 		}
   1070 
   1071 		/* XXX mapped address case */
   1072 	}
   1073 #ifdef INET6
   1074 	else if (ip && ip->ip_v == 6 && sa->sa_family == AF_INET6) {
   1075 		/* XXX do something for ip6 */
   1076 	}
   1077 #endif
   1078 	else {
   1079 		(void)in_pcbnotifyall(&tcbtable, satosin(sa)->sin_addr, errno,
   1080 		    notify);
   1081 #ifdef INET6
   1082 		/* XXX do something for ip6 */
   1083 #endif
   1084 	}
   1085 	return NULL;
   1086 }
   1087 
   1088 /*
   1089  * When a source quence is received, we are being notifed of congestion.
   1090  * Close the congestion window down to the Loss Window (one segment).
   1091  * We will gradually open it again as we proceed.
   1092  */
   1093 void
   1094 tcp_quench(inp, errno)
   1095 	struct inpcb *inp;
   1096 	int errno;
   1097 {
   1098 	struct tcpcb *tp = intotcpcb(inp);
   1099 
   1100 	if (tp)
   1101 		tp->snd_cwnd = tp->t_segsz;
   1102 }
   1103 
   1104 /*
   1105  * On receipt of path MTU corrections, flush old route and replace it
   1106  * with the new one.  Retransmit all unacknowledged packets, to ensure
   1107  * that all packets will be received.
   1108  */
   1109 void
   1110 tcp_mtudisc(inp, errno)
   1111 	struct inpcb *inp;
   1112 	int errno;
   1113 {
   1114 	struct tcpcb *tp = intotcpcb(inp);
   1115 	struct rtentry *rt = in_pcbrtentry(inp);
   1116 
   1117 	if (tp != 0) {
   1118 		if (rt != 0) {
   1119 			/*
   1120 			 * If this was not a host route, remove and realloc.
   1121 			 */
   1122 			if ((rt->rt_flags & RTF_HOST) == 0) {
   1123 				in_rtchange(inp, errno);
   1124 				if ((rt = in_pcbrtentry(inp)) == 0)
   1125 					return;
   1126 			}
   1127 
   1128 			/*
   1129 			 * Slow start out of the error condition.  We
   1130 			 * use the MTU because we know it's smaller
   1131 			 * than the previously transmitted segment.
   1132 			 *
   1133 			 * Note: This is more conservative than the
   1134 			 * suggestion in draft-floyd-incr-init-win-03.
   1135 			 */
   1136 			if (rt->rt_rmx.rmx_mtu != 0)
   1137 				tp->snd_cwnd =
   1138 				    TCP_INITIAL_WINDOW(tcp_init_win,
   1139 				    rt->rt_rmx.rmx_mtu);
   1140 		}
   1141 
   1142 		/*
   1143 		 * Resend unacknowledged packets.
   1144 		 */
   1145 		tp->snd_nxt = tp->snd_una;
   1146 		tcp_output(tp);
   1147 	}
   1148 }
   1149 
   1150 
   1151 /*
   1152  * Compute the MSS to advertise to the peer.  Called only during
   1153  * the 3-way handshake.  If we are the server (peer initiated
   1154  * connection), we are called with a pointer to the interface
   1155  * on which the SYN packet arrived.  If we are the client (we
   1156  * initiated connection), we are called with a pointer to the
   1157  * interface out which this connection should go.
   1158  */
   1159 u_long
   1160 tcp_mss_to_advertise(ifp)
   1161 	const struct ifnet *ifp;
   1162 {
   1163 	extern u_long in_maxmtu;
   1164 	u_long mss = 0;
   1165 
   1166 	/*
   1167 	 * In order to avoid defeating path MTU discovery on the peer,
   1168 	 * we advertise the max MTU of all attached networks as our MSS,
   1169 	 * per RFC 1191, section 3.1.
   1170 	 *
   1171 	 * We provide the option to advertise just the MTU of
   1172 	 * the interface on which we hope this connection will
   1173 	 * be receiving.  If we are responding to a SYN, we
   1174 	 * will have a pretty good idea about this, but when
   1175 	 * initiating a connection there is a bit more doubt.
   1176 	 *
   1177 	 * We also need to ensure that loopback has a large enough
   1178 	 * MSS, as the loopback MTU is never included in in_maxmtu.
   1179 	 */
   1180 
   1181 	if (ifp != NULL)
   1182 		mss = ifp->if_mtu;
   1183 
   1184 	if (tcp_mss_ifmtu == 0)
   1185 		mss = max(in_maxmtu, mss);
   1186 
   1187 	if (mss > sizeof(struct tcpiphdr))
   1188 		mss -= sizeof(struct tcpiphdr);
   1189 
   1190 	mss = max(tcp_mssdflt, mss);
   1191 	return (mss);
   1192 }
   1193 
   1194 /*
   1195  * Set connection variables based on the peer's advertised MSS.
   1196  * We are passed the TCPCB for the actual connection.  If we
   1197  * are the server, we are called by the compressed state engine
   1198  * when the 3-way handshake is complete.  If we are the client,
   1199  * we are called when we recieve the SYN,ACK from the server.
   1200  *
   1201  * NOTE: Our advertised MSS value must be initialized in the TCPCB
   1202  * before this routine is called!
   1203  */
   1204 void
   1205 tcp_mss_from_peer(tp, offer)
   1206 	struct tcpcb *tp;
   1207 	int offer;
   1208 {
   1209 	struct socket *so;
   1210 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
   1211 	struct rtentry *rt;
   1212 #endif
   1213 	u_long bufsize;
   1214 	int mss;
   1215 
   1216 	so = NULL;
   1217 	rt = NULL;
   1218 	if (tp->t_inpcb) {
   1219 		so = tp->t_inpcb->inp_socket;
   1220 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
   1221 		rt = in_pcbrtentry(tp->t_inpcb);
   1222 #endif
   1223 	}
   1224 #ifdef INET6
   1225 	else if (tp->t_in6pcb) {
   1226 		so = tp->t_in6pcb->in6p_socket;
   1227 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
   1228 #ifdef TCP6
   1229 		rt = NULL;
   1230 #else
   1231 		rt = in6_pcbrtentry(tp->t_in6pcb);
   1232 #endif
   1233 #endif
   1234 	}
   1235 #endif
   1236 
   1237 	/*
   1238 	 * As per RFC1122, use the default MSS value, unless they
   1239 	 * sent us an offer.  Do not accept offers less than 32 bytes.
   1240 	 */
   1241 	mss = tcp_mssdflt;
   1242 	if (offer)
   1243 		mss = offer;
   1244 	mss = max(mss, 32);		/* sanity */
   1245 	tp->t_peermss = mss;
   1246 	mss -= tcp_optlen(tp);
   1247 	if (tp->t_inpcb)
   1248 		mss -= ip_optlen(tp->t_inpcb);
   1249 #ifdef INET6
   1250 	else if (tp->t_in6pcb)
   1251 		mss -= ip6_optlen(tp->t_in6pcb);
   1252 #endif
   1253 
   1254 	/*
   1255 	 * If there's a pipesize, change the socket buffer to that size.
   1256 	 * Make the socket buffer an integral number of MSS units.  If
   1257 	 * the MSS is larger than the socket buffer, artificially decrease
   1258 	 * the MSS.
   1259 	 */
   1260 #ifdef RTV_SPIPE
   1261 	if (rt != NULL && rt->rt_rmx.rmx_sendpipe != 0)
   1262 		bufsize = rt->rt_rmx.rmx_sendpipe;
   1263 	else
   1264 #endif
   1265 		bufsize = so->so_snd.sb_hiwat;
   1266 	if (bufsize < mss)
   1267 		mss = bufsize;
   1268 	else {
   1269 		bufsize = roundup(bufsize, mss);
   1270 		if (bufsize > sb_max)
   1271 			bufsize = sb_max;
   1272 		(void) sbreserve(&so->so_snd, bufsize);
   1273 	}
   1274 	tp->t_segsz = mss;
   1275 
   1276 #ifdef RTV_SSTHRESH
   1277 	if (rt != NULL && rt->rt_rmx.rmx_ssthresh) {
   1278 		/*
   1279 		 * There's some sort of gateway or interface buffer
   1280 		 * limit on the path.  Use this to set the slow
   1281 		 * start threshold, but set the threshold to no less
   1282 		 * than 2 * MSS.
   1283 		 */
   1284 		tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
   1285 	}
   1286 #endif
   1287 }
   1288 
   1289 /*
   1290  * Processing necessary when a TCP connection is established.
   1291  */
   1292 void
   1293 tcp_established(tp)
   1294 	struct tcpcb *tp;
   1295 {
   1296 	struct socket *so;
   1297 #ifdef RTV_RPIPE
   1298 	struct rtentry *rt;
   1299 #endif
   1300 	u_long bufsize;
   1301 
   1302 	so = NULL;
   1303 	rt = NULL;
   1304 	if (tp->t_inpcb) {
   1305 		so = tp->t_inpcb->inp_socket;
   1306 #if defined(RTV_RPIPE)
   1307 		rt = in_pcbrtentry(tp->t_inpcb);
   1308 #endif
   1309 	}
   1310 #ifdef INET6
   1311 	else if (tp->t_in6pcb) {
   1312 		so = tp->t_in6pcb->in6p_socket;
   1313 #if defined(RTV_RPIPE)
   1314 #ifdef TCP6
   1315 		rt = NULL;
   1316 #else
   1317 		rt = in6_pcbrtentry(tp->t_in6pcb);
   1318 #endif
   1319 #endif
   1320 	}
   1321 #endif
   1322 
   1323 	tp->t_state = TCPS_ESTABLISHED;
   1324 	TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
   1325 
   1326 #ifdef RTV_RPIPE
   1327 	if (rt != NULL && rt->rt_rmx.rmx_recvpipe != 0)
   1328 		bufsize = rt->rt_rmx.rmx_recvpipe;
   1329 	else
   1330 #endif
   1331 		bufsize = so->so_rcv.sb_hiwat;
   1332 	if (bufsize > tp->t_ourmss) {
   1333 		bufsize = roundup(bufsize, tp->t_ourmss);
   1334 		if (bufsize > sb_max)
   1335 			bufsize = sb_max;
   1336 		(void) sbreserve(&so->so_rcv, bufsize);
   1337 	}
   1338 }
   1339 
   1340 /*
   1341  * Check if there's an initial rtt or rttvar.  Convert from the
   1342  * route-table units to scaled multiples of the slow timeout timer.
   1343  * Called only during the 3-way handshake.
   1344  */
   1345 void
   1346 tcp_rmx_rtt(tp)
   1347 	struct tcpcb *tp;
   1348 {
   1349 #ifdef RTV_RTT
   1350 	struct rtentry *rt = NULL;
   1351 	int rtt;
   1352 
   1353 	if (tp->t_inpcb)
   1354 		rt = in_pcbrtentry(tp->t_inpcb);
   1355 #ifdef INET6
   1356 	else if (tp->t_in6pcb) {
   1357 #ifdef TCP6
   1358 		rt = NULL;
   1359 #else
   1360 		rt = in6_pcbrtentry(tp->t_in6pcb);
   1361 #endif
   1362 	}
   1363 #endif
   1364 	if (rt == NULL)
   1365 		return;
   1366 
   1367 	if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
   1368 		/*
   1369 		 * XXX The lock bit for MTU indicates that the value
   1370 		 * is also a minimum value; this is subject to time.
   1371 		 */
   1372 		if (rt->rt_rmx.rmx_locks & RTV_RTT)
   1373 			TCPT_RANGESET(tp->t_rttmin,
   1374 			    rtt / (RTM_RTTUNIT / PR_SLOWHZ),
   1375 			    TCPTV_MIN, TCPTV_REXMTMAX);
   1376 		tp->t_srtt = rtt /
   1377 		    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
   1378 		if (rt->rt_rmx.rmx_rttvar) {
   1379 			tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
   1380 			    ((RTM_RTTUNIT / PR_SLOWHZ) >>
   1381 				(TCP_RTTVAR_SHIFT + 2));
   1382 		} else {
   1383 			/* Default variation is +- 1 rtt */
   1384 			tp->t_rttvar =
   1385 			    tp->t_srtt >> (TCP_RTT_SHIFT - TCP_RTTVAR_SHIFT);
   1386 		}
   1387 		TCPT_RANGESET(tp->t_rxtcur,
   1388 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2),
   1389 		    tp->t_rttmin, TCPTV_REXMTMAX);
   1390 	}
   1391 #endif
   1392 }
   1393 
   1394 tcp_seq	 tcp_iss_seq = 0;	/* tcp initial seq # */
   1395 
   1396 /*
   1397  * Get a new sequence value given a tcp control block
   1398  */
   1399 tcp_seq
   1400 tcp_new_iss(tp, len, addin)
   1401 	void            *tp;
   1402 	u_long           len;
   1403 	tcp_seq		 addin;
   1404 {
   1405 	tcp_seq          tcp_iss;
   1406 
   1407 	/*
   1408 	 * Randomize.
   1409 	 */
   1410 #if NRND > 0
   1411 	rnd_extract_data(&tcp_iss, sizeof(tcp_iss), RND_EXTRACT_ANY);
   1412 #else
   1413 	tcp_iss = random();
   1414 #endif
   1415 
   1416 	/*
   1417 	 * If we were asked to add some amount to a known value,
   1418 	 * we will take a random value obtained above, mask off the upper
   1419 	 * bits, and add in the known value.  We also add in a constant to
   1420 	 * ensure that we are at least a certain distance from the original
   1421 	 * value.
   1422 	 *
   1423 	 * This is used when an old connection is in timed wait
   1424 	 * and we have a new one coming in, for instance.
   1425 	 */
   1426 	if (addin != 0) {
   1427 #ifdef TCPISS_DEBUG
   1428 		printf("Random %08x, ", tcp_iss);
   1429 #endif
   1430 		tcp_iss &= TCP_ISS_RANDOM_MASK;
   1431 		tcp_iss += addin + TCP_ISSINCR;
   1432 #ifdef TCPISS_DEBUG
   1433 		printf("Old ISS %08x, ISS %08x\n", addin, tcp_iss);
   1434 #endif
   1435 	} else {
   1436 		tcp_iss &= TCP_ISS_RANDOM_MASK;
   1437 		tcp_iss += tcp_iss_seq;
   1438 		tcp_iss_seq += TCP_ISSINCR;
   1439 #ifdef TCPISS_DEBUG
   1440 		printf("ISS %08x\n", tcp_iss);
   1441 #endif
   1442 	}
   1443 
   1444 	if (tcp_compat_42) {
   1445 		/*
   1446 		 * Limit it to the positive range for really old TCP
   1447 		 * implementations.
   1448 		 */
   1449 		if (tcp_iss >= 0x80000000)
   1450 			tcp_iss &= 0x7fffffff;		/* XXX */
   1451 	}
   1452 
   1453 	return tcp_iss;
   1454 }
   1455 
   1456 #ifdef IPSEC
   1457 /* compute ESP/AH header size for TCP, including outer IP header. */
   1458 size_t
   1459 ipsec4_hdrsiz_tcp(tp)
   1460 	struct tcpcb *tp;
   1461 {
   1462 	struct inpcb *inp;
   1463 	size_t hdrsiz;
   1464 
   1465 	/* XXX mapped addr case (tp->t_in6pcb) */
   1466 	if (!tp || !tp->t_template || !(inp = tp->t_inpcb))
   1467 		return 0;
   1468 	switch (tp->t_family) {
   1469 	case AF_INET:
   1470 		hdrsiz = ipsec4_hdrsiz(tp->t_template, inp);
   1471 		break;
   1472 	default:
   1473 		hdrsiz = 0;
   1474 		break;
   1475 	}
   1476 
   1477 	return hdrsiz;
   1478 }
   1479 
   1480 #if defined(INET6) && !defined(TCP6)
   1481 size_t
   1482 ipsec6_hdrsiz_tcp(tp)
   1483 	struct tcpcb *tp;
   1484 {
   1485 	struct in6pcb *in6p;
   1486 	size_t hdrsiz;
   1487 
   1488 	if (!tp || !tp->t_template || !(in6p = tp->t_in6pcb))
   1489 		return 0;
   1490 	switch (tp->t_family) {
   1491 	case AF_INET6:
   1492 		hdrsiz = ipsec6_hdrsiz(tp->t_template, in6p);
   1493 		break;
   1494 	case AF_INET:
   1495 		/* mapped address case - tricky */
   1496 	default:
   1497 		hdrsiz = 0;
   1498 		break;
   1499 	}
   1500 
   1501 	return hdrsiz;
   1502 }
   1503 #endif
   1504 #endif /*IPSEC*/
   1505 
   1506 /*
   1507  * Determine the length of the TCP options for this connection.
   1508  *
   1509  * XXX:  What do we do for SACK, when we add that?  Just reserve
   1510  *       all of the space?  Otherwise we can't exactly be incrementing
   1511  *       cwnd by an amount that varies depending on the amount we last
   1512  *       had to SACK!
   1513  */
   1514 
   1515 u_int
   1516 tcp_optlen(tp)
   1517 	struct tcpcb *tp;
   1518 {
   1519 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) ==
   1520 	    (TF_REQ_TSTMP | TF_RCVD_TSTMP))
   1521 		return TCPOLEN_TSTAMP_APPA;
   1522 	else
   1523 		return 0;
   1524 }
   1525