xref: /netbsd-src/sys/netinet/tcp_subr.c (revision 5aefcfdc06931dd97e76246d2fe0302f7b3fe094)
1 /*	$NetBSD: tcp_subr.c,v 1.105 2000/12/21 00:45:17 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, 2000 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_ipsec.h"
106 #include "opt_tcp_compat_42.h"
107 #include "rnd.h"
108 
109 #include <sys/param.h>
110 #include <sys/proc.h>
111 #include <sys/systm.h>
112 #include <sys/malloc.h>
113 #include <sys/mbuf.h>
114 #include <sys/socket.h>
115 #include <sys/socketvar.h>
116 #include <sys/protosw.h>
117 #include <sys/errno.h>
118 #include <sys/kernel.h>
119 #include <sys/pool.h>
120 #if NRND > 0
121 #include <sys/rnd.h>
122 #endif
123 
124 #include <net/route.h>
125 #include <net/if.h>
126 
127 #include <netinet/in.h>
128 #include <netinet/in_systm.h>
129 #include <netinet/ip.h>
130 #include <netinet/in_pcb.h>
131 #include <netinet/ip_var.h>
132 #include <netinet/ip_icmp.h>
133 
134 #ifdef INET6
135 #ifndef INET
136 #include <netinet/in.h>
137 #endif
138 #include <netinet/ip6.h>
139 #include <netinet6/in6_pcb.h>
140 #include <netinet6/ip6_var.h>
141 #include <netinet6/in6_var.h>
142 #include <netinet6/ip6protosw.h>
143 #include <netinet/icmp6.h>
144 #endif
145 
146 #include <netinet/tcp.h>
147 #include <netinet/tcp_fsm.h>
148 #include <netinet/tcp_seq.h>
149 #include <netinet/tcp_timer.h>
150 #include <netinet/tcp_var.h>
151 #include <netinet/tcpip.h>
152 
153 #ifdef IPSEC
154 #include <netinet6/ipsec.h>
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 int	tcp_rst_ppslim = 100;	/* 100pps */
178 
179 /* tcb hash */
180 #ifndef TCBHASHSIZE
181 #define	TCBHASHSIZE	128
182 #endif
183 int	tcbhashsize = TCBHASHSIZE;
184 
185 /* syn hash parameters */
186 #define	TCP_SYN_HASH_SIZE	293
187 #define	TCP_SYN_BUCKET_SIZE	35
188 int	tcp_syn_cache_size = TCP_SYN_HASH_SIZE;
189 int	tcp_syn_cache_limit = TCP_SYN_HASH_SIZE*TCP_SYN_BUCKET_SIZE;
190 int	tcp_syn_bucket_limit = 3*TCP_SYN_BUCKET_SIZE;
191 struct	syn_cache_head tcp_syn_cache[TCP_SYN_HASH_SIZE];
192 int	tcp_syn_cache_interval = 1;	/* runs timer twice a second */
193 
194 int	tcp_freeq __P((struct tcpcb *));
195 
196 #ifdef INET
197 void	tcp_mtudisc_callback __P((struct in_addr));
198 #endif
199 #ifdef INET6
200 void	tcp6_mtudisc_callback __P((struct in6_addr *));
201 #endif
202 
203 void	tcp_mtudisc __P((struct inpcb *, int));
204 #ifdef INET6
205 void	tcp6_mtudisc __P((struct in6pcb *, int));
206 #endif
207 
208 struct pool tcpcb_pool;
209 
210 /*
211  * Tcp initialization
212  */
213 void
214 tcp_init()
215 {
216 	int hlen;
217 
218 	pool_init(&tcpcb_pool, sizeof(struct tcpcb), 0, 0, 0, "tcpcbpl",
219 	    0, NULL, NULL, M_PCB);
220 	in_pcbinit(&tcbtable, tcbhashsize, tcbhashsize);
221 #ifdef INET6
222 	tcb6.in6p_next = tcb6.in6p_prev = &tcb6;
223 #endif
224 	LIST_INIT(&tcp_delacks);
225 
226 	hlen = sizeof(struct ip) + sizeof(struct tcphdr);
227 #ifdef INET6
228 	if (sizeof(struct ip) < sizeof(struct ip6_hdr))
229 		hlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
230 #endif
231 	if (max_protohdr < hlen)
232 		max_protohdr = hlen;
233 	if (max_linkhdr + hlen > MHLEN)
234 		panic("tcp_init");
235 
236 #ifdef INET
237 	icmp_mtudisc_callback_register(tcp_mtudisc_callback);
238 #endif
239 #ifdef INET6
240 	icmp6_mtudisc_callback_register(tcp6_mtudisc_callback);
241 #endif
242 
243 	/* Initialize the compressed state engine. */
244 	syn_cache_init();
245 }
246 
247 /*
248  * Create template to be used to send tcp packets on a connection.
249  * Call after host entry created, allocates an mbuf and fills
250  * in a skeletal tcp/ip header, minimizing the amount of work
251  * necessary when the connection is used.
252  */
253 struct mbuf *
254 tcp_template(tp)
255 	struct tcpcb *tp;
256 {
257 	struct inpcb *inp = tp->t_inpcb;
258 #ifdef INET6
259 	struct in6pcb *in6p = tp->t_in6pcb;
260 #endif
261 	struct tcphdr *n;
262 	struct mbuf *m;
263 	int hlen;
264 
265 	switch (tp->t_family) {
266 	case AF_INET:
267 		hlen = sizeof(struct ip);
268 		if (inp)
269 			break;
270 #ifdef INET6
271 		if (in6p) {
272 			/* mapped addr case */
273 			if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
274 			 && IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
275 				break;
276 		}
277 #endif
278 		return NULL;	/*EINVAL*/
279 #ifdef INET6
280 	case AF_INET6:
281 		hlen = sizeof(struct ip6_hdr);
282 		if (in6p) {
283 			/* more sainty check? */
284 			break;
285 		}
286 		return NULL;	/*EINVAL*/
287 #endif
288 	default:
289 		hlen = 0;	/*pacify gcc*/
290 		return NULL;	/*EAFNOSUPPORT*/
291 	}
292 #ifdef DIAGNOSTIC
293 	if (hlen + sizeof(struct tcphdr) > MCLBYTES)
294 		panic("mclbytes too small for t_template");
295 #endif
296 	m = tp->t_template;
297 	if (m && m->m_len == hlen + sizeof(struct tcphdr))
298 		;
299 	else {
300 		if (m)
301 			m_freem(m);
302 		m = tp->t_template = NULL;
303 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
304 		if (m && hlen + sizeof(struct tcphdr) > MHLEN) {
305 			MCLGET(m, M_DONTWAIT);
306 			if ((m->m_flags & M_EXT) == 0) {
307 				m_free(m);
308 				m = NULL;
309 			}
310 		}
311 		if (m == NULL)
312 			return NULL;
313 		m->m_pkthdr.len = m->m_len = hlen + sizeof(struct tcphdr);
314 	}
315 	bzero(mtod(m, caddr_t), m->m_len);
316 	switch (tp->t_family) {
317 	case AF_INET:
318 	    {
319 		struct ipovly *ipov;
320 		mtod(m, struct ip *)->ip_v = 4;
321 		ipov = mtod(m, struct ipovly *);
322 		ipov->ih_pr = IPPROTO_TCP;
323 		ipov->ih_len = htons(sizeof(struct tcphdr));
324 		if (inp) {
325 			ipov->ih_src = inp->inp_laddr;
326 			ipov->ih_dst = inp->inp_faddr;
327 		}
328 #ifdef INET6
329 		else if (in6p) {
330 			/* mapped addr case */
331 			bcopy(&in6p->in6p_laddr.s6_addr32[3], &ipov->ih_src,
332 				sizeof(ipov->ih_src));
333 			bcopy(&in6p->in6p_faddr.s6_addr32[3], &ipov->ih_dst,
334 				sizeof(ipov->ih_dst));
335 		}
336 #endif
337 		break;
338 	    }
339 #ifdef INET6
340 	case AF_INET6:
341 	    {
342 		struct ip6_hdr *ip6;
343 		mtod(m, struct ip *)->ip_v = 6;
344 		ip6 = mtod(m, struct ip6_hdr *);
345 		ip6->ip6_nxt = IPPROTO_TCP;
346 		ip6->ip6_plen = htons(sizeof(struct tcphdr));
347 		ip6->ip6_src = in6p->in6p_laddr;
348 		ip6->ip6_dst = in6p->in6p_faddr;
349 		ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
350 		if (ip6_auto_flowlabel) {
351 			ip6->ip6_flow &= ~IPV6_FLOWLABEL_MASK;
352 			ip6->ip6_flow |=
353 				(htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
354 		}
355 		ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
356 		ip6->ip6_vfc |= IPV6_VERSION;
357 		break;
358 	    }
359 #endif
360 	}
361 	n = (struct tcphdr *)(mtod(m, caddr_t) + hlen);
362 	if (inp) {
363 		n->th_sport = inp->inp_lport;
364 		n->th_dport = inp->inp_fport;
365 	}
366 #ifdef INET6
367 	else if (in6p) {
368 		n->th_sport = in6p->in6p_lport;
369 		n->th_dport = in6p->in6p_fport;
370 	}
371 #endif
372 	n->th_seq = 0;
373 	n->th_ack = 0;
374 	n->th_x2 = 0;
375 	n->th_off = 5;
376 	n->th_flags = 0;
377 	n->th_win = 0;
378 	n->th_sum = 0;
379 	n->th_urp = 0;
380 	return (m);
381 }
382 
383 /*
384  * Send a single message to the TCP at address specified by
385  * the given TCP/IP header.  If m == 0, then we make a copy
386  * of the tcpiphdr at ti and send directly to the addressed host.
387  * This is used to force keep alive messages out using the TCP
388  * template for a connection tp->t_template.  If flags are given
389  * then we send a message back to the TCP which originated the
390  * segment ti, and discard the mbuf containing it and any other
391  * attached mbufs.
392  *
393  * In any case the ack and sequence number of the transmitted
394  * segment are as specified by the parameters.
395  */
396 int
397 tcp_respond(tp, template, m, th0, ack, seq, flags)
398 	struct tcpcb *tp;
399 	struct mbuf *template;
400 	struct mbuf *m;
401 	struct tcphdr *th0;
402 	tcp_seq ack, seq;
403 	int flags;
404 {
405 	struct route *ro;
406 	int error, tlen, win = 0;
407 	int hlen;
408 	struct ip *ip;
409 #ifdef INET6
410 	struct ip6_hdr *ip6;
411 #endif
412 	int family;	/* family on packet, not inpcb/in6pcb! */
413 	struct tcphdr *th;
414 
415 	if (tp != NULL && (flags & TH_RST) == 0) {
416 #ifdef DIAGNOSTIC
417 		if (tp->t_inpcb && tp->t_in6pcb)
418 			panic("tcp_respond: both t_inpcb and t_in6pcb are set");
419 #endif
420 #ifdef INET
421 		if (tp->t_inpcb)
422 			win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
423 #endif
424 #ifdef INET6
425 		if (tp->t_in6pcb)
426 			win = sbspace(&tp->t_in6pcb->in6p_socket->so_rcv);
427 #endif
428 	}
429 
430 	ip = NULL;
431 #ifdef INET6
432 	ip6 = NULL;
433 #endif
434 	if (m == 0) {
435 		if (!template)
436 			return EINVAL;
437 
438 		/* get family information from template */
439 		switch (mtod(template, struct ip *)->ip_v) {
440 		case 4:
441 			family = AF_INET;
442 			hlen = sizeof(struct ip);
443 			break;
444 #ifdef INET6
445 		case 6:
446 			family = AF_INET6;
447 			hlen = sizeof(struct ip6_hdr);
448 			break;
449 #endif
450 		default:
451 			return EAFNOSUPPORT;
452 		}
453 
454 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
455 		if (m) {
456 			MCLGET(m, M_DONTWAIT);
457 			if ((m->m_flags & M_EXT) == 0) {
458 				m_free(m);
459 				m = NULL;
460 			}
461 		}
462 		if (m == NULL)
463 			return (ENOBUFS);
464 
465 		if (tcp_compat_42)
466 			tlen = 1;
467 		else
468 			tlen = 0;
469 
470 		m->m_data += max_linkhdr;
471 		bcopy(mtod(template, caddr_t), mtod(m, caddr_t),
472 			template->m_len);
473 		switch (family) {
474 		case AF_INET:
475 			ip = mtod(m, struct ip *);
476 			th = (struct tcphdr *)(ip + 1);
477 			break;
478 #ifdef INET6
479 		case AF_INET6:
480 			ip6 = mtod(m, struct ip6_hdr *);
481 			th = (struct tcphdr *)(ip6 + 1);
482 			break;
483 #endif
484 #if 0
485 		default:
486 			/* noone will visit here */
487 			m_freem(m);
488 			return EAFNOSUPPORT;
489 #endif
490 		}
491 		flags = TH_ACK;
492 	} else {
493 
494 		if ((m->m_flags & M_PKTHDR) == 0) {
495 #if 0
496 			printf("non PKTHDR to tcp_respond\n");
497 #endif
498 			m_freem(m);
499 			return EINVAL;
500 		}
501 #ifdef DIAGNOSTIC
502 		if (!th0)
503 			panic("th0 == NULL in tcp_respond");
504 #endif
505 
506 		/* get family information from m */
507 		switch (mtod(m, struct ip *)->ip_v) {
508 		case 4:
509 			family = AF_INET;
510 			hlen = sizeof(struct ip);
511 			ip = mtod(m, struct ip *);
512 			break;
513 #ifdef INET6
514 		case 6:
515 			family = AF_INET6;
516 			hlen = sizeof(struct ip6_hdr);
517 			ip6 = mtod(m, struct ip6_hdr *);
518 			break;
519 #endif
520 		default:
521 			m_freem(m);
522 			return EAFNOSUPPORT;
523 		}
524 		if ((flags & TH_SYN) == 0 || sizeof(*th0) > (th0->th_off << 2))
525 			tlen = sizeof(*th0);
526 		else
527 			tlen = th0->th_off << 2;
528 
529 		if (m->m_len > hlen + tlen && (m->m_flags & M_EXT) == 0 &&
530 		    mtod(m, caddr_t) + hlen == (caddr_t)th0) {
531 			m->m_len = hlen + tlen;
532 			m_freem(m->m_next);
533 			m->m_next = NULL;
534 		} else {
535 			struct mbuf *n;
536 
537 #ifdef DIAGNOSTIC
538 			if (max_linkhdr + hlen + tlen > MCLBYTES) {
539 				m_freem(m);
540 				return EMSGSIZE;
541 			}
542 #endif
543 			MGETHDR(n, M_DONTWAIT, MT_HEADER);
544 			if (n && max_linkhdr + hlen + tlen > MHLEN) {
545 				MCLGET(n, M_DONTWAIT);
546 				if ((n->m_flags & M_EXT) == 0) {
547 					m_freem(n);
548 					n = NULL;
549 				}
550 			}
551 			if (!n) {
552 				m_freem(m);
553 				return ENOBUFS;
554 			}
555 
556 			n->m_data += max_linkhdr;
557 			n->m_len = hlen + tlen;
558 			m_copyback(n, 0, hlen, mtod(m, caddr_t));
559 			m_copyback(n, hlen, tlen, (caddr_t)th0);
560 
561 			m_freem(m);
562 			m = n;
563 			n = NULL;
564 		}
565 
566 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
567 		switch (family) {
568 		case AF_INET:
569 			ip = mtod(m, struct ip *);
570 			th = (struct tcphdr *)(ip + 1);
571 			ip->ip_p = IPPROTO_TCP;
572 			xchg(ip->ip_dst, ip->ip_src, struct in_addr);
573 			ip->ip_p = IPPROTO_TCP;
574 			break;
575 #ifdef INET6
576 		case AF_INET6:
577 			ip6 = mtod(m, struct ip6_hdr *);
578 			th = (struct tcphdr *)(ip6 + 1);
579 			ip6->ip6_nxt = IPPROTO_TCP;
580 			xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr);
581 			ip6->ip6_nxt = IPPROTO_TCP;
582 			break;
583 #endif
584 #if 0
585 		default:
586 			/* noone will visit here */
587 			m_freem(m);
588 			return EAFNOSUPPORT;
589 #endif
590 		}
591 		xchg(th->th_dport, th->th_sport, u_int16_t);
592 #undef xchg
593 		tlen = 0;	/*be friendly with the following code*/
594 	}
595 	th->th_seq = htonl(seq);
596 	th->th_ack = htonl(ack);
597 	th->th_x2 = 0;
598 	if ((flags & TH_SYN) == 0) {
599 		if (tp)
600 			win >>= tp->rcv_scale;
601 		if (win > TCP_MAXWIN)
602 			win = TCP_MAXWIN;
603 		th->th_win = htons((u_int16_t)win);
604 		th->th_off = sizeof (struct tcphdr) >> 2;
605 		tlen += sizeof(*th);
606 	} else
607 		tlen += th->th_off << 2;
608 	m->m_len = hlen + tlen;
609 	m->m_pkthdr.len = hlen + tlen;
610 	m->m_pkthdr.rcvif = (struct ifnet *) 0;
611 	th->th_flags = flags;
612 	th->th_urp = 0;
613 
614 	switch (family) {
615 #ifdef INET
616 	case AF_INET:
617 	    {
618 		struct ipovly *ipov = (struct ipovly *)ip;
619 		bzero(ipov->ih_x1, sizeof ipov->ih_x1);
620 		ipov->ih_len = htons((u_int16_t)tlen);
621 
622 		th->th_sum = 0;
623 		th->th_sum = in_cksum(m, hlen + tlen);
624 		ip->ip_len = hlen + tlen;	/*will be flipped on output*/
625 		ip->ip_ttl = ip_defttl;
626 		break;
627 	    }
628 #endif
629 #ifdef INET6
630 	case AF_INET6:
631 	    {
632 		th->th_sum = 0;
633 		th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
634 				tlen);
635 		ip6->ip6_plen = ntohs(tlen);
636 		if (tp && tp->t_in6pcb) {
637 			struct ifnet *oifp;
638 			ro = (struct route *)&tp->t_in6pcb->in6p_route;
639 			oifp = ro->ro_rt ? ro->ro_rt->rt_ifp : NULL;
640 			ip6->ip6_hlim = in6_selecthlim(tp->t_in6pcb, oifp);
641 		} else
642 			ip6->ip6_hlim = ip6_defhlim;
643 		ip6->ip6_flow &= ~IPV6_FLOWINFO_MASK;
644 		if (ip6_auto_flowlabel) {
645 			ip6->ip6_flow |=
646 				(htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
647 		}
648 		break;
649 	    }
650 #endif
651 	}
652 
653 #ifdef IPSEC
654 	ipsec_setsocket(m, NULL);
655 #endif /*IPSEC*/
656 
657 	if (tp != NULL && tp->t_inpcb != NULL) {
658 		ro = &tp->t_inpcb->inp_route;
659 #ifdef IPSEC
660 		ipsec_setsocket(m, tp->t_inpcb->inp_socket);
661 #endif
662 #ifdef DIAGNOSTIC
663 		if (family != AF_INET)
664 			panic("tcp_respond: address family mismatch");
665 		if (!in_hosteq(ip->ip_dst, tp->t_inpcb->inp_faddr)) {
666 			panic("tcp_respond: ip_dst %x != inp_faddr %x",
667 			    ntohl(ip->ip_dst.s_addr),
668 			    ntohl(tp->t_inpcb->inp_faddr.s_addr));
669 		}
670 #endif
671 	}
672 #ifdef INET6
673 	else if (tp != NULL && tp->t_in6pcb != NULL) {
674 		ro = (struct route *)&tp->t_in6pcb->in6p_route;
675 #ifdef IPSEC
676 		ipsec_setsocket(m, tp->t_in6pcb->in6p_socket);
677 #endif
678 #ifdef DIAGNOSTIC
679 		if (family == AF_INET) {
680 			if (!IN6_IS_ADDR_V4MAPPED(&tp->t_in6pcb->in6p_faddr))
681 				panic("tcp_respond: not mapped addr");
682 			if (bcmp(&ip->ip_dst,
683 					&tp->t_in6pcb->in6p_faddr.s6_addr32[3],
684 					sizeof(ip->ip_dst)) != 0) {
685 				panic("tcp_respond: ip_dst != in6p_faddr");
686 			}
687 		} else if (family == AF_INET6) {
688 			if (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &tp->t_in6pcb->in6p_faddr))
689 				panic("tcp_respond: ip6_dst != in6p_faddr");
690 		} else
691 			panic("tcp_respond: address family mismatch");
692 #endif
693 	}
694 #endif
695 	else
696 		ro = NULL;
697 
698 	switch (family) {
699 #ifdef INET
700 	case AF_INET:
701 		error = ip_output(m, NULL, ro,
702 		    (ip_mtudisc ? IP_MTUDISC : 0),
703 		    NULL);
704 		break;
705 #endif
706 #ifdef INET6
707 	case AF_INET6:
708 		error = ip6_output(m, NULL, (struct route_in6 *)ro, 0, NULL,
709 			NULL);
710 		break;
711 #endif
712 	default:
713 		error = EAFNOSUPPORT;
714 		break;
715 	}
716 
717 	return (error);
718 }
719 
720 /*
721  * Create a new TCP control block, making an
722  * empty reassembly queue and hooking it to the argument
723  * protocol control block.
724  */
725 struct tcpcb *
726 tcp_newtcpcb(family, aux)
727 	int family;	/* selects inpcb, or in6pcb */
728 	void *aux;
729 {
730 	struct tcpcb *tp;
731 
732 	switch (family) {
733 	case PF_INET:
734 		break;
735 #ifdef INET6
736 	case PF_INET6:
737 		break;
738 #endif
739 	default:
740 		return NULL;
741 	}
742 
743 	tp = pool_get(&tcpcb_pool, PR_NOWAIT);
744 	if (tp == NULL)
745 		return (NULL);
746 	bzero((caddr_t)tp, sizeof(struct tcpcb));
747 	LIST_INIT(&tp->segq);
748 	LIST_INIT(&tp->timeq);
749 	tp->t_family = family;		/* may be overridden later on */
750 	tp->t_peermss = tcp_mssdflt;
751 	tp->t_ourmss = tcp_mssdflt;
752 	tp->t_segsz = tcp_mssdflt;
753 	LIST_INIT(&tp->t_sc);
754 
755 	tp->t_flags = 0;
756 	if (tcp_do_rfc1323 && tcp_do_win_scale)
757 		tp->t_flags |= TF_REQ_SCALE;
758 	if (tcp_do_rfc1323 && tcp_do_timestamps)
759 		tp->t_flags |= TF_REQ_TSTMP;
760 	if (tcp_do_sack == 2)
761 		tp->t_flags |= TF_WILL_SACK;
762 	else if (tcp_do_sack == 1)
763 		tp->t_flags |= TF_WILL_SACK|TF_IGNR_RXSACK;
764 	tp->t_flags |= TF_CANT_TXSACK;
765 	switch (family) {
766 	case PF_INET:
767 		tp->t_inpcb = (struct inpcb *)aux;
768 		break;
769 #ifdef INET6
770 	case PF_INET6:
771 		tp->t_in6pcb = (struct in6pcb *)aux;
772 		break;
773 #endif
774 	}
775 	/*
776 	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
777 	 * rtt estimate.  Set rttvar so that srtt + 2 * rttvar gives
778 	 * reasonable initial retransmit time.
779 	 */
780 	tp->t_srtt = TCPTV_SRTTBASE;
781 	tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << (TCP_RTTVAR_SHIFT + 2 - 1);
782 	tp->t_rttmin = TCPTV_MIN;
783 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
784 	    TCPTV_MIN, TCPTV_REXMTMAX);
785 	tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
786 	tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
787 	if (family == AF_INET) {
788 		struct inpcb *inp = (struct inpcb *)aux;
789 		inp->inp_ip.ip_ttl = ip_defttl;
790 		inp->inp_ppcb = (caddr_t)tp;
791 	}
792 #ifdef INET6
793 	else if (family == AF_INET6) {
794 		struct in6pcb *in6p = (struct in6pcb *)aux;
795 		in6p->in6p_ip6.ip6_hlim = in6_selecthlim(in6p,
796 			in6p->in6p_route.ro_rt ? in6p->in6p_route.ro_rt->rt_ifp
797 					       : NULL);
798 		in6p->in6p_ppcb = (caddr_t)tp;
799 	}
800 #endif
801 	return (tp);
802 }
803 
804 /*
805  * Drop a TCP connection, reporting
806  * the specified error.  If connection is synchronized,
807  * then send a RST to peer.
808  */
809 struct tcpcb *
810 tcp_drop(tp, errno)
811 	struct tcpcb *tp;
812 	int errno;
813 {
814 	struct socket *so = NULL;
815 
816 #ifdef DIAGNOSTIC
817 	if (tp->t_inpcb && tp->t_in6pcb)
818 		panic("tcp_drop: both t_inpcb and t_in6pcb are set");
819 #endif
820 #ifdef INET
821 	if (tp->t_inpcb)
822 		so = tp->t_inpcb->inp_socket;
823 #endif
824 #ifdef INET6
825 	if (tp->t_in6pcb)
826 		so = tp->t_in6pcb->in6p_socket;
827 #endif
828 	if (!so)
829 		return NULL;
830 
831 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
832 		tp->t_state = TCPS_CLOSED;
833 		(void) tcp_output(tp);
834 		tcpstat.tcps_drops++;
835 	} else
836 		tcpstat.tcps_conndrops++;
837 	if (errno == ETIMEDOUT && tp->t_softerror)
838 		errno = tp->t_softerror;
839 	so->so_error = errno;
840 	return (tcp_close(tp));
841 }
842 
843 /*
844  * Close a TCP control block:
845  *	discard all space held by the tcp
846  *	discard internet protocol block
847  *	wake up any sleepers
848  */
849 struct tcpcb *
850 tcp_close(tp)
851 	struct tcpcb *tp;
852 {
853 	struct inpcb *inp;
854 #ifdef INET6
855 	struct in6pcb *in6p;
856 #endif
857 	struct socket *so;
858 #ifdef RTV_RTT
859 	struct rtentry *rt;
860 #endif
861 	struct route *ro;
862 
863 	inp = tp->t_inpcb;
864 #ifdef INET6
865 	in6p = tp->t_in6pcb;
866 #endif
867 	so = NULL;
868 	ro = NULL;
869 	if (inp) {
870 		so = inp->inp_socket;
871 		ro = &inp->inp_route;
872 	}
873 #ifdef INET6
874 	else if (in6p) {
875 		so = in6p->in6p_socket;
876 		ro = (struct route *)&in6p->in6p_route;
877 	}
878 #endif
879 
880 #ifdef RTV_RTT
881 	/*
882 	 * If we sent enough data to get some meaningful characteristics,
883 	 * save them in the routing entry.  'Enough' is arbitrarily
884 	 * defined as the sendpipesize (default 4K) * 16.  This would
885 	 * give us 16 rtt samples assuming we only get one sample per
886 	 * window (the usual case on a long haul net).  16 samples is
887 	 * enough for the srtt filter to converge to within 5% of the correct
888 	 * value; fewer samples and we could save a very bogus rtt.
889 	 *
890 	 * Don't update the default route's characteristics and don't
891 	 * update anything that the user "locked".
892 	 */
893 	if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) &&
894 	    ro && (rt = ro->ro_rt) &&
895 	    !in_nullhost(satosin(rt_key(rt))->sin_addr)) {
896 		u_long i = 0;
897 
898 		if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
899 			i = tp->t_srtt *
900 			    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
901 			if (rt->rt_rmx.rmx_rtt && i)
902 				/*
903 				 * filter this update to half the old & half
904 				 * the new values, converting scale.
905 				 * See route.h and tcp_var.h for a
906 				 * description of the scaling constants.
907 				 */
908 				rt->rt_rmx.rmx_rtt =
909 				    (rt->rt_rmx.rmx_rtt + i) / 2;
910 			else
911 				rt->rt_rmx.rmx_rtt = i;
912 		}
913 		if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
914 			i = tp->t_rttvar *
915 			    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTTVAR_SHIFT + 2));
916 			if (rt->rt_rmx.rmx_rttvar && i)
917 				rt->rt_rmx.rmx_rttvar =
918 				    (rt->rt_rmx.rmx_rttvar + i) / 2;
919 			else
920 				rt->rt_rmx.rmx_rttvar = i;
921 		}
922 		/*
923 		 * update the pipelimit (ssthresh) if it has been updated
924 		 * already or if a pipesize was specified & the threshhold
925 		 * got below half the pipesize.  I.e., wait for bad news
926 		 * before we start updating, then update on both good
927 		 * and bad news.
928 		 */
929 		if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
930 		    (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh) ||
931 		    i < (rt->rt_rmx.rmx_sendpipe / 2)) {
932 			/*
933 			 * convert the limit from user data bytes to
934 			 * packets then to packet data bytes.
935 			 */
936 			i = (i + tp->t_segsz / 2) / tp->t_segsz;
937 			if (i < 2)
938 				i = 2;
939 			i *= (u_long)(tp->t_segsz + sizeof (struct tcpiphdr));
940 			if (rt->rt_rmx.rmx_ssthresh)
941 				rt->rt_rmx.rmx_ssthresh =
942 				    (rt->rt_rmx.rmx_ssthresh + i) / 2;
943 			else
944 				rt->rt_rmx.rmx_ssthresh = i;
945 		}
946 	}
947 #endif /* RTV_RTT */
948 	/* free the reassembly queue, if any */
949 	TCP_REASS_LOCK(tp);
950 	(void) tcp_freeq(tp);
951 	TCP_REASS_UNLOCK(tp);
952 
953 	TCP_CLEAR_DELACK(tp);
954 	syn_cache_cleanup(tp);
955 
956 	if (tp->t_template) {
957 		m_free(tp->t_template);
958 		tp->t_template = NULL;
959 	}
960 	pool_put(&tcpcb_pool, tp);
961 	if (inp) {
962 		inp->inp_ppcb = 0;
963 		soisdisconnected(so);
964 		in_pcbdetach(inp);
965 	}
966 #ifdef INET6
967 	else if (in6p) {
968 		in6p->in6p_ppcb = 0;
969 		soisdisconnected(so);
970 		in6_pcbdetach(in6p);
971 	}
972 #endif
973 	tcpstat.tcps_closed++;
974 	return ((struct tcpcb *)0);
975 }
976 
977 int
978 tcp_freeq(tp)
979 	struct tcpcb *tp;
980 {
981 	struct ipqent *qe;
982 	int rv = 0;
983 #ifdef TCPREASS_DEBUG
984 	int i = 0;
985 #endif
986 
987 	TCP_REASS_LOCK_CHECK(tp);
988 
989 	while ((qe = tp->segq.lh_first) != NULL) {
990 #ifdef TCPREASS_DEBUG
991 		printf("tcp_freeq[%p,%d]: %u:%u(%u) 0x%02x\n",
992 			tp, i++, qe->ipqe_seq, qe->ipqe_seq + qe->ipqe_len,
993 			qe->ipqe_len, qe->ipqe_flags & (TH_SYN|TH_FIN|TH_RST));
994 #endif
995 		LIST_REMOVE(qe, ipqe_q);
996 		LIST_REMOVE(qe, ipqe_timeq);
997 		m_freem(qe->ipqe_m);
998 		pool_put(&ipqent_pool, qe);
999 		rv = 1;
1000 	}
1001 	return (rv);
1002 }
1003 
1004 /*
1005  * Protocol drain routine.  Called when memory is in short supply.
1006  */
1007 void
1008 tcp_drain()
1009 {
1010 	struct inpcb *inp;
1011 	struct tcpcb *tp;
1012 
1013 	/*
1014 	 * Free the sequence queue of all TCP connections.
1015 	 */
1016 	inp = tcbtable.inpt_queue.cqh_first;
1017 	if (inp)						/* XXX */
1018 	for (; inp != (struct inpcb *)&tcbtable.inpt_queue;
1019 	    inp = inp->inp_queue.cqe_next) {
1020 		if ((tp = intotcpcb(inp)) != NULL) {
1021 			/*
1022 			 * We may be called from a device's interrupt
1023 			 * context.  If the tcpcb is already busy,
1024 			 * just bail out now.
1025 			 */
1026 			if (tcp_reass_lock_try(tp) == 0)
1027 				continue;
1028 			if (tcp_freeq(tp))
1029 				tcpstat.tcps_connsdrained++;
1030 			TCP_REASS_UNLOCK(tp);
1031 		}
1032 	}
1033 }
1034 
1035 /*
1036  * Notify a tcp user of an asynchronous error;
1037  * store error as soft error, but wake up user
1038  * (for now, won't do anything until can select for soft error).
1039  */
1040 void
1041 tcp_notify(inp, error)
1042 	struct inpcb *inp;
1043 	int error;
1044 {
1045 	struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
1046 	struct socket *so = inp->inp_socket;
1047 
1048 	/*
1049 	 * Ignore some errors if we are hooked up.
1050 	 * If connection hasn't completed, has retransmitted several times,
1051 	 * and receives a second error, give up now.  This is better
1052 	 * than waiting a long time to establish a connection that
1053 	 * can never complete.
1054 	 */
1055 	if (tp->t_state == TCPS_ESTABLISHED &&
1056 	     (error == EHOSTUNREACH || error == ENETUNREACH ||
1057 	      error == EHOSTDOWN)) {
1058 		return;
1059 	} else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
1060 	    tp->t_rxtshift > 3 && tp->t_softerror)
1061 		so->so_error = error;
1062 	else
1063 		tp->t_softerror = error;
1064 	wakeup((caddr_t) &so->so_timeo);
1065 	sorwakeup(so);
1066 	sowwakeup(so);
1067 }
1068 
1069 #ifdef INET6
1070 void
1071 tcp6_notify(in6p, error)
1072 	struct in6pcb *in6p;
1073 	int error;
1074 {
1075 	struct tcpcb *tp = (struct tcpcb *)in6p->in6p_ppcb;
1076 	struct socket *so = in6p->in6p_socket;
1077 
1078 	/*
1079 	 * Ignore some errors if we are hooked up.
1080 	 * If connection hasn't completed, has retransmitted several times,
1081 	 * and receives a second error, give up now.  This is better
1082 	 * than waiting a long time to establish a connection that
1083 	 * can never complete.
1084 	 */
1085 	if (tp->t_state == TCPS_ESTABLISHED &&
1086 	     (error == EHOSTUNREACH || error == ENETUNREACH ||
1087 	      error == EHOSTDOWN)) {
1088 		return;
1089 	} else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
1090 	    tp->t_rxtshift > 3 && tp->t_softerror)
1091 		so->so_error = error;
1092 	else
1093 		tp->t_softerror = error;
1094 	wakeup((caddr_t) &so->so_timeo);
1095 	sorwakeup(so);
1096 	sowwakeup(so);
1097 }
1098 #endif
1099 
1100 #ifdef INET6
1101 void
1102 tcp6_ctlinput(cmd, sa, d)
1103 	int cmd;
1104 	struct sockaddr *sa;
1105 	void *d;
1106 {
1107 	struct tcphdr *thp;
1108 	struct tcphdr th;
1109 	void (*notify) __P((struct in6pcb *, int)) = tcp6_notify;
1110 	int nmatch;
1111 	struct sockaddr_in6 sa6;
1112 	struct ip6_hdr *ip6;
1113 	struct mbuf *m;
1114 	int off;
1115 	struct in6_addr finaldst;
1116 	struct in6_addr s;
1117 
1118 	if (sa->sa_family != AF_INET6 ||
1119 	    sa->sa_len != sizeof(struct sockaddr_in6))
1120 		return;
1121 	if ((unsigned)cmd >= PRC_NCMDS)
1122 		return;
1123 	else if (cmd == PRC_QUENCH) {
1124 		/* XXX there's no PRC_QUENCH in IPv6 */
1125 		notify = tcp6_quench;
1126 	} else if (PRC_IS_REDIRECT(cmd))
1127 		notify = in6_rtchange, d = NULL;
1128 	else if (cmd == PRC_MSGSIZE)
1129 		; /* special code is present, see below */
1130 	else if (cmd == PRC_HOSTDEAD)
1131 		d = NULL;
1132 	else if (inet6ctlerrmap[cmd] == 0)
1133 		return;
1134 
1135 	/* if the parameter is from icmp6, decode it. */
1136 	if (d != NULL) {
1137 		struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
1138 		m = ip6cp->ip6c_m;
1139 		ip6 = ip6cp->ip6c_ip6;
1140 		off = ip6cp->ip6c_off;
1141 
1142 		/* translate addresses into internal form */
1143 		bcopy(ip6cp->ip6c_finaldst, &finaldst, sizeof(finaldst));
1144 		if (IN6_IS_ADDR_LINKLOCAL(&finaldst)) {
1145 			finaldst.s6_addr16[1] =
1146 			    htons(m->m_pkthdr.rcvif->if_index);
1147 		}
1148 		bcopy(&ip6->ip6_src, &s, sizeof(s));
1149 		if (IN6_IS_ADDR_LINKLOCAL(&s))
1150 			s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
1151 	} else {
1152 		m = NULL;
1153 		ip6 = NULL;
1154 	}
1155 
1156 	/* translate addresses into internal form */
1157 	sa6 = *(struct sockaddr_in6 *)sa;
1158 	if (IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr) && m && m->m_pkthdr.rcvif)
1159 		sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
1160 
1161 	if (ip6) {
1162 		/*
1163 		 * XXX: We assume that when ip6 is non NULL,
1164 		 * M and OFF are valid.
1165 		 */
1166 
1167 		/* check if we can safely examine src and dst ports */
1168 		if (m->m_pkthdr.len < off + sizeof(th))
1169 			return;
1170 
1171 		if (m->m_len < off + sizeof(th)) {
1172 			/*
1173 			 * this should be rare case,
1174 			 * so we compromise on this copy...
1175 			 */
1176 			m_copydata(m, off, sizeof(th), (caddr_t)&th);
1177 			thp = &th;
1178 		} else
1179 			thp = (struct tcphdr *)(mtod(m, caddr_t) + off);
1180 
1181 		if (cmd == PRC_MSGSIZE) {
1182 			int valid = 0;
1183 
1184 			/*
1185 			 * Check to see if we have a valid TCP connection
1186 			 * corresponding to the address in the ICMPv6 message
1187 			 * payload.
1188 			 */
1189 			if (in6_pcblookup_connect(&tcb6, &finaldst,
1190 			    thp->th_dport, &s, thp->th_sport, 0))
1191 				valid++;
1192 
1193 			/*
1194 			 * Now that we've validated that we are actually
1195 			 * communicating with the host indicated in the ICMPv6
1196 			 * message, recalculate the new MTU, and create the
1197 			 * corresponding routing entry.
1198 			 */
1199 			icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
1200 
1201 			return;
1202 		}
1203 
1204 		nmatch = in6_pcbnotify(&tcb6, (struct sockaddr *)&sa6,
1205 		    thp->th_dport, &s, thp->th_sport, cmd, notify);
1206 		if (nmatch == 0 && syn_cache_count &&
1207 		    (inet6ctlerrmap[cmd] == EHOSTUNREACH ||
1208 		     inet6ctlerrmap[cmd] == ENETUNREACH ||
1209 		     inet6ctlerrmap[cmd] == EHOSTDOWN)) {
1210 			struct sockaddr_in6 sin6;
1211 			bzero(&sin6, sizeof(sin6));
1212 			sin6.sin6_len = sizeof(sin6);
1213 			sin6.sin6_family = AF_INET6;
1214 			sin6.sin6_port = thp->th_sport;
1215 			sin6.sin6_addr = s;
1216 			syn_cache_unreach((struct sockaddr *)&sin6, sa, thp);
1217 		}
1218 	} else {
1219 		(void) in6_pcbnotify(&tcb6, (struct sockaddr *)&sa6, 0,
1220 				     &zeroin6_addr, 0, cmd, notify);
1221 	}
1222 }
1223 #endif
1224 
1225 #ifdef INET
1226 /* assumes that ip header and tcp header are contiguous on mbuf */
1227 void *
1228 tcp_ctlinput(cmd, sa, v)
1229 	int cmd;
1230 	struct sockaddr *sa;
1231 	void *v;
1232 {
1233 	struct ip *ip = v;
1234 	struct tcphdr *th;
1235 	struct icmp *icp;
1236 	extern int inetctlerrmap[];
1237 	void (*notify) __P((struct inpcb *, int)) = tcp_notify;
1238 	int errno;
1239 	int nmatch;
1240 
1241 	if (sa->sa_family != AF_INET ||
1242 	    sa->sa_len != sizeof(struct sockaddr_in))
1243 		return NULL;
1244 	if ((unsigned)cmd >= PRC_NCMDS)
1245 		return NULL;
1246 	errno = inetctlerrmap[cmd];
1247 	if (cmd == PRC_QUENCH)
1248 		notify = tcp_quench;
1249 	else if (PRC_IS_REDIRECT(cmd))
1250 		notify = in_rtchange, ip = 0;
1251 	else if (cmd == PRC_MSGSIZE && ip_mtudisc && ip && ip->ip_v == 4) {
1252 		/*
1253 		 * Check to see if we have a valid TCP connection
1254 		 * corresponding to the address in the ICMP message
1255 		 * payload.
1256 		 */
1257 		th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
1258 		if (in_pcblookup_connect(&tcbtable,
1259 					 ip->ip_dst, th->th_dport,
1260 					 ip->ip_src, th->th_sport) == NULL)
1261 			return NULL;
1262 
1263 		/*
1264 		 * Now that we've validated that we are actually communicating
1265 		 * with the host indicated in the ICMP message, locate the
1266 		 * ICMP header, recalculate the new MTU, and create the
1267 		 * corresponding routing entry.
1268 		 */
1269 		icp = (struct icmp *)((caddr_t)ip -
1270 		    offsetof(struct icmp, icmp_ip));
1271 		icmp_mtudisc(icp, ip->ip_dst);
1272 
1273 		return NULL;
1274 	} else if (cmd == PRC_HOSTDEAD)
1275 		ip = 0;
1276 	else if (errno == 0)
1277 		return NULL;
1278 	if (ip && ip->ip_v == 4 && sa->sa_family == AF_INET) {
1279 		th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
1280 		nmatch = in_pcbnotify(&tcbtable, satosin(sa)->sin_addr,
1281 		    th->th_dport, ip->ip_src, th->th_sport, errno, notify);
1282 		if (nmatch == 0 && syn_cache_count &&
1283 		    (inetctlerrmap[cmd] == EHOSTUNREACH ||
1284 		    inetctlerrmap[cmd] == ENETUNREACH ||
1285 		    inetctlerrmap[cmd] == EHOSTDOWN)) {
1286 			struct sockaddr_in sin;
1287 			bzero(&sin, sizeof(sin));
1288 			sin.sin_len = sizeof(sin);
1289 			sin.sin_family = AF_INET;
1290 			sin.sin_port = th->th_sport;
1291 			sin.sin_addr = ip->ip_src;
1292 			syn_cache_unreach((struct sockaddr *)&sin, sa, th);
1293 		}
1294 
1295 		/* XXX mapped address case */
1296 	} else
1297 		in_pcbnotifyall(&tcbtable, satosin(sa)->sin_addr, errno,
1298 		    notify);
1299 	return NULL;
1300 }
1301 
1302 /*
1303  * When a source quence is received, we are being notifed of congestion.
1304  * Close the congestion window down to the Loss Window (one segment).
1305  * We will gradually open it again as we proceed.
1306  */
1307 void
1308 tcp_quench(inp, errno)
1309 	struct inpcb *inp;
1310 	int errno;
1311 {
1312 	struct tcpcb *tp = intotcpcb(inp);
1313 
1314 	if (tp)
1315 		tp->snd_cwnd = tp->t_segsz;
1316 }
1317 #endif
1318 
1319 #ifdef INET6
1320 void
1321 tcp6_quench(in6p, errno)
1322 	struct in6pcb *in6p;
1323 	int errno;
1324 {
1325 	struct tcpcb *tp = in6totcpcb(in6p);
1326 
1327 	if (tp)
1328 		tp->snd_cwnd = tp->t_segsz;
1329 }
1330 #endif
1331 
1332 #ifdef INET
1333 /*
1334  * Path MTU Discovery handlers.
1335  */
1336 void
1337 tcp_mtudisc_callback(faddr)
1338 	struct in_addr faddr;
1339 {
1340 
1341 	in_pcbnotifyall(&tcbtable, faddr, EMSGSIZE, tcp_mtudisc);
1342 }
1343 
1344 /*
1345  * On receipt of path MTU corrections, flush old route and replace it
1346  * with the new one.  Retransmit all unacknowledged packets, to ensure
1347  * that all packets will be received.
1348  */
1349 void
1350 tcp_mtudisc(inp, errno)
1351 	struct inpcb *inp;
1352 	int errno;
1353 {
1354 	struct tcpcb *tp = intotcpcb(inp);
1355 	struct rtentry *rt = in_pcbrtentry(inp);
1356 
1357 	if (tp != 0) {
1358 		if (rt != 0) {
1359 			/*
1360 			 * If this was not a host route, remove and realloc.
1361 			 */
1362 			if ((rt->rt_flags & RTF_HOST) == 0) {
1363 				in_rtchange(inp, errno);
1364 				if ((rt = in_pcbrtentry(inp)) == 0)
1365 					return;
1366 			}
1367 
1368 			/*
1369 			 * Slow start out of the error condition.  We
1370 			 * use the MTU because we know it's smaller
1371 			 * than the previously transmitted segment.
1372 			 *
1373 			 * Note: This is more conservative than the
1374 			 * suggestion in draft-floyd-incr-init-win-03.
1375 			 */
1376 			if (rt->rt_rmx.rmx_mtu != 0)
1377 				tp->snd_cwnd =
1378 				    TCP_INITIAL_WINDOW(tcp_init_win,
1379 				    rt->rt_rmx.rmx_mtu);
1380 		}
1381 
1382 		/*
1383 		 * Resend unacknowledged packets.
1384 		 */
1385 		tp->snd_nxt = tp->snd_una;
1386 		tcp_output(tp);
1387 	}
1388 }
1389 #endif
1390 
1391 #ifdef INET6
1392 /*
1393  * Path MTU Discovery handlers.
1394  */
1395 void
1396 tcp6_mtudisc_callback(faddr)
1397 	struct in6_addr *faddr;
1398 {
1399 	struct sockaddr_in6 sin6;
1400 
1401 	bzero(&sin6, sizeof(sin6));
1402 	sin6.sin6_family = AF_INET6;
1403 	sin6.sin6_len = sizeof(struct sockaddr_in6);
1404 	sin6.sin6_addr = *faddr;
1405 	(void) in6_pcbnotify(&tcb6, (struct sockaddr *)&sin6, 0,
1406 	    &zeroin6_addr, 0, PRC_MSGSIZE, tcp6_mtudisc);
1407 }
1408 
1409 void
1410 tcp6_mtudisc(in6p, errno)
1411 	struct in6pcb *in6p;
1412 	int errno;
1413 {
1414 	struct tcpcb *tp = in6totcpcb(in6p);
1415 	struct rtentry *rt = in6_pcbrtentry(in6p);
1416 
1417 	if (tp != 0) {
1418 		if (rt != 0) {
1419 			/*
1420 			 * If this was not a host route, remove and realloc.
1421 			 */
1422 			if ((rt->rt_flags & RTF_HOST) == 0) {
1423 				in6_rtchange(in6p, errno);
1424 				if ((rt = in6_pcbrtentry(in6p)) == 0)
1425 					return;
1426 			}
1427 
1428 			/*
1429 			 * Slow start out of the error condition.  We
1430 			 * use the MTU because we know it's smaller
1431 			 * than the previously transmitted segment.
1432 			 *
1433 			 * Note: This is more conservative than the
1434 			 * suggestion in draft-floyd-incr-init-win-03.
1435 			 */
1436 			if (rt->rt_rmx.rmx_mtu != 0)
1437 				tp->snd_cwnd =
1438 				    TCP_INITIAL_WINDOW(tcp_init_win,
1439 				    rt->rt_rmx.rmx_mtu);
1440 		}
1441 
1442 		/*
1443 		 * Resend unacknowledged packets.
1444 		 */
1445 		tp->snd_nxt = tp->snd_una;
1446 		tcp_output(tp);
1447 	}
1448 }
1449 #endif /* INET6 */
1450 
1451 /*
1452  * Compute the MSS to advertise to the peer.  Called only during
1453  * the 3-way handshake.  If we are the server (peer initiated
1454  * connection), we are called with a pointer to the interface
1455  * on which the SYN packet arrived.  If we are the client (we
1456  * initiated connection), we are called with a pointer to the
1457  * interface out which this connection should go.
1458  *
1459  * NOTE: Do not subtract IP option/extension header size nor IPsec
1460  * header size from MSS advertisement.  MSS option must hold the maximum
1461  * segment size we can accept, so it must always be:
1462  *	 max(if mtu) - ip header - tcp header
1463  */
1464 u_long
1465 tcp_mss_to_advertise(ifp, af)
1466 	const struct ifnet *ifp;
1467 	int af;
1468 {
1469 	extern u_long in_maxmtu;
1470 	u_long mss = 0;
1471 	u_long hdrsiz;
1472 
1473 	/*
1474 	 * In order to avoid defeating path MTU discovery on the peer,
1475 	 * we advertise the max MTU of all attached networks as our MSS,
1476 	 * per RFC 1191, section 3.1.
1477 	 *
1478 	 * We provide the option to advertise just the MTU of
1479 	 * the interface on which we hope this connection will
1480 	 * be receiving.  If we are responding to a SYN, we
1481 	 * will have a pretty good idea about this, but when
1482 	 * initiating a connection there is a bit more doubt.
1483 	 *
1484 	 * We also need to ensure that loopback has a large enough
1485 	 * MSS, as the loopback MTU is never included in in_maxmtu.
1486 	 */
1487 
1488 	if (ifp != NULL)
1489 		mss = ifp->if_mtu;
1490 
1491 	if (tcp_mss_ifmtu == 0)
1492 		mss = max(in_maxmtu, mss);
1493 
1494 	switch (af) {
1495 	case AF_INET:
1496 		hdrsiz = sizeof(struct ip);
1497 		break;
1498 #ifdef INET6
1499 	case AF_INET6:
1500 		hdrsiz = sizeof(struct ip6_hdr);
1501 		break;
1502 #endif
1503 	default:
1504 		hdrsiz = 0;
1505 		break;
1506 	}
1507 	hdrsiz += sizeof(struct tcphdr);
1508 	if (mss > hdrsiz)
1509 		mss -= hdrsiz;
1510 
1511 	mss = max(tcp_mssdflt, mss);
1512 	return (mss);
1513 }
1514 
1515 /*
1516  * Set connection variables based on the peer's advertised MSS.
1517  * We are passed the TCPCB for the actual connection.  If we
1518  * are the server, we are called by the compressed state engine
1519  * when the 3-way handshake is complete.  If we are the client,
1520  * we are called when we recieve the SYN,ACK from the server.
1521  *
1522  * NOTE: Our advertised MSS value must be initialized in the TCPCB
1523  * before this routine is called!
1524  */
1525 void
1526 tcp_mss_from_peer(tp, offer)
1527 	struct tcpcb *tp;
1528 	int offer;
1529 {
1530 	struct socket *so;
1531 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
1532 	struct rtentry *rt;
1533 #endif
1534 	u_long bufsize;
1535 	int mss;
1536 
1537 #ifdef DIAGNOSTIC
1538 	if (tp->t_inpcb && tp->t_in6pcb)
1539 		panic("tcp_mss_from_peer: both t_inpcb and t_in6pcb are set");
1540 #endif
1541 	so = NULL;
1542 	rt = NULL;
1543 #ifdef INET
1544 	if (tp->t_inpcb) {
1545 		so = tp->t_inpcb->inp_socket;
1546 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
1547 		rt = in_pcbrtentry(tp->t_inpcb);
1548 #endif
1549 	}
1550 #endif
1551 #ifdef INET6
1552 	if (tp->t_in6pcb) {
1553 		so = tp->t_in6pcb->in6p_socket;
1554 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
1555 		rt = in6_pcbrtentry(tp->t_in6pcb);
1556 #endif
1557 	}
1558 #endif
1559 
1560 	/*
1561 	 * As per RFC1122, use the default MSS value, unless they
1562 	 * sent us an offer.  Do not accept offers less than 32 bytes.
1563 	 */
1564 	mss = tcp_mssdflt;
1565 	if (offer)
1566 		mss = offer;
1567 	mss = max(mss, 32);		/* sanity */
1568 	tp->t_peermss = mss;
1569 	mss -= tcp_optlen(tp);
1570 #ifdef INET
1571 	if (tp->t_inpcb)
1572 		mss -= ip_optlen(tp->t_inpcb);
1573 #endif
1574 #ifdef INET6
1575 	if (tp->t_in6pcb)
1576 		mss -= ip6_optlen(tp->t_in6pcb);
1577 #endif
1578 
1579 	/*
1580 	 * If there's a pipesize, change the socket buffer to that size.
1581 	 * Make the socket buffer an integral number of MSS units.  If
1582 	 * the MSS is larger than the socket buffer, artificially decrease
1583 	 * the MSS.
1584 	 */
1585 #ifdef RTV_SPIPE
1586 	if (rt != NULL && rt->rt_rmx.rmx_sendpipe != 0)
1587 		bufsize = rt->rt_rmx.rmx_sendpipe;
1588 	else
1589 #endif
1590 		bufsize = so->so_snd.sb_hiwat;
1591 	if (bufsize < mss)
1592 		mss = bufsize;
1593 	else {
1594 		bufsize = roundup(bufsize, mss);
1595 		if (bufsize > sb_max)
1596 			bufsize = sb_max;
1597 		(void) sbreserve(&so->so_snd, bufsize);
1598 	}
1599 	tp->t_segsz = mss;
1600 
1601 #ifdef RTV_SSTHRESH
1602 	if (rt != NULL && rt->rt_rmx.rmx_ssthresh) {
1603 		/*
1604 		 * There's some sort of gateway or interface buffer
1605 		 * limit on the path.  Use this to set the slow
1606 		 * start threshold, but set the threshold to no less
1607 		 * than 2 * MSS.
1608 		 */
1609 		tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
1610 	}
1611 #endif
1612 }
1613 
1614 /*
1615  * Processing necessary when a TCP connection is established.
1616  */
1617 void
1618 tcp_established(tp)
1619 	struct tcpcb *tp;
1620 {
1621 	struct socket *so;
1622 #ifdef RTV_RPIPE
1623 	struct rtentry *rt;
1624 #endif
1625 	u_long bufsize;
1626 
1627 #ifdef DIAGNOSTIC
1628 	if (tp->t_inpcb && tp->t_in6pcb)
1629 		panic("tcp_established: both t_inpcb and t_in6pcb are set");
1630 #endif
1631 	so = NULL;
1632 	rt = NULL;
1633 #ifdef INET
1634 	if (tp->t_inpcb) {
1635 		so = tp->t_inpcb->inp_socket;
1636 #if defined(RTV_RPIPE)
1637 		rt = in_pcbrtentry(tp->t_inpcb);
1638 #endif
1639 	}
1640 #endif
1641 #ifdef INET6
1642 	if (tp->t_in6pcb) {
1643 		so = tp->t_in6pcb->in6p_socket;
1644 #if defined(RTV_RPIPE)
1645 		rt = in6_pcbrtentry(tp->t_in6pcb);
1646 #endif
1647 	}
1648 #endif
1649 
1650 	tp->t_state = TCPS_ESTABLISHED;
1651 	TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
1652 
1653 #ifdef RTV_RPIPE
1654 	if (rt != NULL && rt->rt_rmx.rmx_recvpipe != 0)
1655 		bufsize = rt->rt_rmx.rmx_recvpipe;
1656 	else
1657 #endif
1658 		bufsize = so->so_rcv.sb_hiwat;
1659 	if (bufsize > tp->t_ourmss) {
1660 		bufsize = roundup(bufsize, tp->t_ourmss);
1661 		if (bufsize > sb_max)
1662 			bufsize = sb_max;
1663 		(void) sbreserve(&so->so_rcv, bufsize);
1664 	}
1665 }
1666 
1667 /*
1668  * Check if there's an initial rtt or rttvar.  Convert from the
1669  * route-table units to scaled multiples of the slow timeout timer.
1670  * Called only during the 3-way handshake.
1671  */
1672 void
1673 tcp_rmx_rtt(tp)
1674 	struct tcpcb *tp;
1675 {
1676 #ifdef RTV_RTT
1677 	struct rtentry *rt = NULL;
1678 	int rtt;
1679 
1680 #ifdef DIAGNOSTIC
1681 	if (tp->t_inpcb && tp->t_in6pcb)
1682 		panic("tcp_rmx_rtt: both t_inpcb and t_in6pcb are set");
1683 #endif
1684 #ifdef INET
1685 	if (tp->t_inpcb)
1686 		rt = in_pcbrtentry(tp->t_inpcb);
1687 #endif
1688 #ifdef INET6
1689 	if (tp->t_in6pcb)
1690 		rt = in6_pcbrtentry(tp->t_in6pcb);
1691 #endif
1692 	if (rt == NULL)
1693 		return;
1694 
1695 	if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
1696 		/*
1697 		 * XXX The lock bit for MTU indicates that the value
1698 		 * is also a minimum value; this is subject to time.
1699 		 */
1700 		if (rt->rt_rmx.rmx_locks & RTV_RTT)
1701 			TCPT_RANGESET(tp->t_rttmin,
1702 			    rtt / (RTM_RTTUNIT / PR_SLOWHZ),
1703 			    TCPTV_MIN, TCPTV_REXMTMAX);
1704 		tp->t_srtt = rtt /
1705 		    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
1706 		if (rt->rt_rmx.rmx_rttvar) {
1707 			tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
1708 			    ((RTM_RTTUNIT / PR_SLOWHZ) >>
1709 				(TCP_RTTVAR_SHIFT + 2));
1710 		} else {
1711 			/* Default variation is +- 1 rtt */
1712 			tp->t_rttvar =
1713 			    tp->t_srtt >> (TCP_RTT_SHIFT - TCP_RTTVAR_SHIFT);
1714 		}
1715 		TCPT_RANGESET(tp->t_rxtcur,
1716 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2),
1717 		    tp->t_rttmin, TCPTV_REXMTMAX);
1718 	}
1719 #endif
1720 }
1721 
1722 tcp_seq	 tcp_iss_seq = 0;	/* tcp initial seq # */
1723 
1724 /*
1725  * Get a new sequence value given a tcp control block
1726  */
1727 tcp_seq
1728 tcp_new_iss(tp, len, addin)
1729 	void            *tp;
1730 	u_long           len;
1731 	tcp_seq		 addin;
1732 {
1733 	tcp_seq          tcp_iss;
1734 
1735 	/*
1736 	 * Randomize.
1737 	 */
1738 #if NRND > 0
1739 	rnd_extract_data(&tcp_iss, sizeof(tcp_iss), RND_EXTRACT_ANY);
1740 #else
1741 	tcp_iss = random();
1742 #endif
1743 
1744 	/*
1745 	 * If we were asked to add some amount to a known value,
1746 	 * we will take a random value obtained above, mask off the upper
1747 	 * bits, and add in the known value.  We also add in a constant to
1748 	 * ensure that we are at least a certain distance from the original
1749 	 * value.
1750 	 *
1751 	 * This is used when an old connection is in timed wait
1752 	 * and we have a new one coming in, for instance.
1753 	 */
1754 	if (addin != 0) {
1755 #ifdef TCPISS_DEBUG
1756 		printf("Random %08x, ", tcp_iss);
1757 #endif
1758 		tcp_iss &= TCP_ISS_RANDOM_MASK;
1759 		tcp_iss += addin + TCP_ISSINCR;
1760 #ifdef TCPISS_DEBUG
1761 		printf("Old ISS %08x, ISS %08x\n", addin, tcp_iss);
1762 #endif
1763 	} else {
1764 		tcp_iss &= TCP_ISS_RANDOM_MASK;
1765 		tcp_iss += tcp_iss_seq;
1766 		tcp_iss_seq += TCP_ISSINCR;
1767 #ifdef TCPISS_DEBUG
1768 		printf("ISS %08x\n", tcp_iss);
1769 #endif
1770 	}
1771 
1772 	if (tcp_compat_42) {
1773 		/*
1774 		 * Limit it to the positive range for really old TCP
1775 		 * implementations.
1776 		 */
1777 		if (tcp_iss >= 0x80000000)
1778 			tcp_iss &= 0x7fffffff;		/* XXX */
1779 	}
1780 
1781 	return tcp_iss;
1782 }
1783 
1784 #ifdef IPSEC
1785 /* compute ESP/AH header size for TCP, including outer IP header. */
1786 size_t
1787 ipsec4_hdrsiz_tcp(tp)
1788 	struct tcpcb *tp;
1789 {
1790 	struct inpcb *inp;
1791 	size_t hdrsiz;
1792 
1793 	/* XXX mapped addr case (tp->t_in6pcb) */
1794 	if (!tp || !tp->t_template || !(inp = tp->t_inpcb))
1795 		return 0;
1796 	switch (tp->t_family) {
1797 	case AF_INET:
1798 		/* XXX: should use currect direction. */
1799 		hdrsiz = ipsec4_hdrsiz(tp->t_template, IPSEC_DIR_OUTBOUND, inp);
1800 		break;
1801 	default:
1802 		hdrsiz = 0;
1803 		break;
1804 	}
1805 
1806 	return hdrsiz;
1807 }
1808 
1809 #ifdef INET6
1810 size_t
1811 ipsec6_hdrsiz_tcp(tp)
1812 	struct tcpcb *tp;
1813 {
1814 	struct in6pcb *in6p;
1815 	size_t hdrsiz;
1816 
1817 	if (!tp || !tp->t_template || !(in6p = tp->t_in6pcb))
1818 		return 0;
1819 	switch (tp->t_family) {
1820 	case AF_INET6:
1821 		/* XXX: should use currect direction. */
1822 		hdrsiz = ipsec6_hdrsiz(tp->t_template, IPSEC_DIR_OUTBOUND, in6p);
1823 		break;
1824 	case AF_INET:
1825 		/* mapped address case - tricky */
1826 	default:
1827 		hdrsiz = 0;
1828 		break;
1829 	}
1830 
1831 	return hdrsiz;
1832 }
1833 #endif
1834 #endif /*IPSEC*/
1835 
1836 /*
1837  * Determine the length of the TCP options for this connection.
1838  *
1839  * XXX:  What do we do for SACK, when we add that?  Just reserve
1840  *       all of the space?  Otherwise we can't exactly be incrementing
1841  *       cwnd by an amount that varies depending on the amount we last
1842  *       had to SACK!
1843  */
1844 
1845 u_int
1846 tcp_optlen(tp)
1847 	struct tcpcb *tp;
1848 {
1849 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) ==
1850 	    (TF_REQ_TSTMP | TF_RCVD_TSTMP))
1851 		return TCPOLEN_TSTAMP_APPA;
1852 	else
1853 		return 0;
1854 }
1855