xref: /netbsd/sys/netinet/tcp_subr.c (revision da7deee6)
1 /*	$NetBSD: tcp_subr.c,v 1.296 2022/11/04 09:01:53 ozaki-r 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, 2001, 2008 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  *
49  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
50  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
51  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
53  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
57  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59  * POSSIBILITY OF SUCH DAMAGE.
60  */
61 
62 /*
63  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
64  *	The Regents of the University of California.  All rights reserved.
65  *
66  * Redistribution and use in source and binary forms, with or without
67  * modification, are permitted provided that the following conditions
68  * are met:
69  * 1. Redistributions of source code must retain the above copyright
70  *    notice, this list of conditions and the following disclaimer.
71  * 2. Redistributions in binary form must reproduce the above copyright
72  *    notice, this list of conditions and the following disclaimer in the
73  *    documentation and/or other materials provided with the distribution.
74  * 3. Neither the name of the University nor the names of its contributors
75  *    may be used to endorse or promote products derived from this software
76  *    without specific prior written permission.
77  *
78  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88  * SUCH DAMAGE.
89  *
90  *	@(#)tcp_subr.c	8.2 (Berkeley) 5/24/95
91  */
92 
93 #include <sys/cdefs.h>
94 __KERNEL_RCSID(0, "$NetBSD: tcp_subr.c,v 1.296 2022/11/04 09:01:53 ozaki-r Exp $");
95 
96 #ifdef _KERNEL_OPT
97 #include "opt_inet.h"
98 #include "opt_ipsec.h"
99 #include "opt_inet_csum.h"
100 #include "opt_mbuftrace.h"
101 #endif
102 
103 #include <sys/param.h>
104 #include <sys/atomic.h>
105 #include <sys/proc.h>
106 #include <sys/systm.h>
107 #include <sys/mbuf.h>
108 #include <sys/once.h>
109 #include <sys/socket.h>
110 #include <sys/socketvar.h>
111 #include <sys/protosw.h>
112 #include <sys/errno.h>
113 #include <sys/kernel.h>
114 #include <sys/pool.h>
115 #include <sys/md5.h>
116 #include <sys/cprng.h>
117 
118 #include <net/route.h>
119 #include <net/if.h>
120 
121 #include <netinet/in.h>
122 #include <netinet/in_systm.h>
123 #include <netinet/ip.h>
124 #include <netinet/in_pcb.h>
125 #include <netinet/ip_var.h>
126 #include <netinet/ip_icmp.h>
127 
128 #ifdef INET6
129 #include <netinet/ip6.h>
130 #include <netinet6/in6_pcb.h>
131 #include <netinet6/ip6_var.h>
132 #include <netinet6/in6_var.h>
133 #include <netinet6/ip6protosw.h>
134 #include <netinet/icmp6.h>
135 #include <netinet6/nd6.h>
136 #endif
137 
138 #include <netinet/tcp.h>
139 #include <netinet/tcp_fsm.h>
140 #include <netinet/tcp_seq.h>
141 #include <netinet/tcp_timer.h>
142 #include <netinet/tcp_var.h>
143 #include <netinet/tcp_vtw.h>
144 #include <netinet/tcp_private.h>
145 #include <netinet/tcp_congctl.h>
146 #include <netinet/tcp_syncache.h>
147 
148 #ifdef IPSEC
149 #include <netipsec/ipsec.h>
150 #ifdef INET6
151 #include <netipsec/ipsec6.h>
152 #endif
153 #include <netipsec/key.h>
154 #endif
155 
156 
157 struct	inpcbtable tcbtable;	/* head of queue of active tcpcb's */
158 u_int32_t tcp_now;		/* slow ticks, for RFC 1323 timestamps */
159 
160 percpu_t *tcpstat_percpu;
161 
162 /* patchable/settable parameters for tcp */
163 int 	tcp_mssdflt = TCP_MSS;
164 int	tcp_minmss = TCP_MINMSS;
165 int 	tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
166 int	tcp_do_rfc1323 = 1;	/* window scaling / timestamps (obsolete) */
167 int	tcp_do_rfc1948 = 0;	/* ISS by cryptographic hash */
168 int	tcp_do_sack = 1;	/* selective acknowledgement */
169 int	tcp_do_win_scale = 1;	/* RFC1323 window scaling */
170 int	tcp_do_timestamps = 1;	/* RFC1323 timestamps */
171 int	tcp_ack_on_push = 0;	/* set to enable immediate ACK-on-PUSH */
172 int	tcp_do_ecn = 0;		/* Explicit Congestion Notification */
173 #ifndef TCP_INIT_WIN
174 #define	TCP_INIT_WIN	4	/* initial slow start window */
175 #endif
176 #ifndef TCP_INIT_WIN_LOCAL
177 #define	TCP_INIT_WIN_LOCAL 4	/* initial slow start window for local nets */
178 #endif
179 /*
180  * Up to 5 we scale linearly, to reach 3 * 1460; then (iw) * 1460.
181  * This is to simulate current behavior for iw == 4
182  */
183 int tcp_init_win_max[] = {
184 	 1 * 1460,
185 	 1 * 1460,
186 	 2 * 1460,
187 	 2 * 1460,
188 	 3 * 1460,
189 	 5 * 1460,
190 	 6 * 1460,
191 	 7 * 1460,
192 	 8 * 1460,
193 	 9 * 1460,
194 	10 * 1460
195 };
196 int	tcp_init_win = TCP_INIT_WIN;
197 int	tcp_init_win_local = TCP_INIT_WIN_LOCAL;
198 int	tcp_mss_ifmtu = 0;
199 int	tcp_rst_ppslim = 100;	/* 100pps */
200 int	tcp_ackdrop_ppslim = 100;	/* 100pps */
201 int	tcp_do_loopback_cksum = 0;
202 int	tcp_do_abc = 1;		/* RFC3465 Appropriate byte counting. */
203 int	tcp_abc_aggressive = 1;	/* 1: L=2*SMSS  0: L=1*SMSS */
204 int	tcp_sack_tp_maxholes = 32;
205 int	tcp_sack_globalmaxholes = 1024;
206 int	tcp_sack_globalholes = 0;
207 int	tcp_ecn_maxretries = 1;
208 int	tcp_msl_enable = 1;		/* enable TIME_WAIT truncation	*/
209 int	tcp_msl_loop   = PR_SLOWHZ;	/* MSL for loopback		*/
210 int	tcp_msl_local  = 5 * PR_SLOWHZ;	/* MSL for 'local'		*/
211 int	tcp_msl_remote = TCPTV_MSL;	/* MSL otherwise		*/
212 int	tcp_msl_remote_threshold = TCPTV_SRTTDFLT;	/* RTT threshold */
213 int	tcp_rttlocal = 0;		/* Use RTT to decide who's 'local' */
214 
215 int	tcp4_vtw_enable = 0;		/* 1 to enable */
216 int	tcp6_vtw_enable = 0;		/* 1 to enable */
217 int	tcp_vtw_was_enabled = 0;
218 int	tcp_vtw_entries = 1 << 4;	/* 16 vestigial TIME_WAIT entries */
219 
220 /* tcb hash */
221 #ifndef TCBHASHSIZE
222 #define	TCBHASHSIZE	128
223 #endif
224 int	tcbhashsize = TCBHASHSIZE;
225 
226 int	tcp_freeq(struct tcpcb *);
227 static int	tcp_iss_secret_init(void);
228 
229 static void	tcp_mtudisc_callback(struct in_addr);
230 
231 #ifdef INET6
232 static void	tcp6_mtudisc(struct inpcb *, int);
233 #endif
234 
235 static struct pool tcpcb_pool;
236 
237 static int tcp_drainwanted;
238 
239 #ifdef TCP_CSUM_COUNTERS
240 #include <sys/device.h>
241 
242 struct evcnt tcp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
243     NULL, "tcp", "hwcsum bad");
244 struct evcnt tcp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
245     NULL, "tcp", "hwcsum ok");
246 struct evcnt tcp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
247     NULL, "tcp", "hwcsum data");
248 struct evcnt tcp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
249     NULL, "tcp", "swcsum");
250 
251 EVCNT_ATTACH_STATIC(tcp_hwcsum_bad);
252 EVCNT_ATTACH_STATIC(tcp_hwcsum_ok);
253 EVCNT_ATTACH_STATIC(tcp_hwcsum_data);
254 EVCNT_ATTACH_STATIC(tcp_swcsum);
255 
256 #if defined(INET6)
257 struct evcnt tcp6_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
258     NULL, "tcp6", "hwcsum bad");
259 struct evcnt tcp6_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
260     NULL, "tcp6", "hwcsum ok");
261 struct evcnt tcp6_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
262     NULL, "tcp6", "hwcsum data");
263 struct evcnt tcp6_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
264     NULL, "tcp6", "swcsum");
265 
266 EVCNT_ATTACH_STATIC(tcp6_hwcsum_bad);
267 EVCNT_ATTACH_STATIC(tcp6_hwcsum_ok);
268 EVCNT_ATTACH_STATIC(tcp6_hwcsum_data);
269 EVCNT_ATTACH_STATIC(tcp6_swcsum);
270 #endif /* defined(INET6) */
271 #endif /* TCP_CSUM_COUNTERS */
272 
273 
274 #ifdef TCP_OUTPUT_COUNTERS
275 #include <sys/device.h>
276 
277 struct evcnt tcp_output_bigheader = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
278     NULL, "tcp", "output big header");
279 struct evcnt tcp_output_predict_hit = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
280     NULL, "tcp", "output predict hit");
281 struct evcnt tcp_output_predict_miss = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
282     NULL, "tcp", "output predict miss");
283 struct evcnt tcp_output_copysmall = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
284     NULL, "tcp", "output copy small");
285 struct evcnt tcp_output_copybig = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
286     NULL, "tcp", "output copy big");
287 struct evcnt tcp_output_refbig = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
288     NULL, "tcp", "output reference big");
289 
290 EVCNT_ATTACH_STATIC(tcp_output_bigheader);
291 EVCNT_ATTACH_STATIC(tcp_output_predict_hit);
292 EVCNT_ATTACH_STATIC(tcp_output_predict_miss);
293 EVCNT_ATTACH_STATIC(tcp_output_copysmall);
294 EVCNT_ATTACH_STATIC(tcp_output_copybig);
295 EVCNT_ATTACH_STATIC(tcp_output_refbig);
296 
297 #endif /* TCP_OUTPUT_COUNTERS */
298 
299 #ifdef TCP_REASS_COUNTERS
300 #include <sys/device.h>
301 
302 struct evcnt tcp_reass_ = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
303     NULL, "tcp_reass", "calls");
304 struct evcnt tcp_reass_empty = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
305     &tcp_reass_, "tcp_reass", "insert into empty queue");
306 struct evcnt tcp_reass_iteration[8] = {
307     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", ">7 iterations"),
308     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "1 iteration"),
309     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "2 iterations"),
310     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "3 iterations"),
311     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "4 iterations"),
312     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "5 iterations"),
313     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "6 iterations"),
314     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "7 iterations"),
315 };
316 struct evcnt tcp_reass_prependfirst = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
317     &tcp_reass_, "tcp_reass", "prepend to first");
318 struct evcnt tcp_reass_prepend = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
319     &tcp_reass_, "tcp_reass", "prepend");
320 struct evcnt tcp_reass_insert = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
321     &tcp_reass_, "tcp_reass", "insert");
322 struct evcnt tcp_reass_inserttail = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
323     &tcp_reass_, "tcp_reass", "insert at tail");
324 struct evcnt tcp_reass_append = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
325     &tcp_reass_, "tcp_reass", "append");
326 struct evcnt tcp_reass_appendtail = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
327     &tcp_reass_, "tcp_reass", "append to tail fragment");
328 struct evcnt tcp_reass_overlaptail = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
329     &tcp_reass_, "tcp_reass", "overlap at end");
330 struct evcnt tcp_reass_overlapfront = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
331     &tcp_reass_, "tcp_reass", "overlap at start");
332 struct evcnt tcp_reass_segdup = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
333     &tcp_reass_, "tcp_reass", "duplicate segment");
334 struct evcnt tcp_reass_fragdup = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
335     &tcp_reass_, "tcp_reass", "duplicate fragment");
336 
337 EVCNT_ATTACH_STATIC(tcp_reass_);
338 EVCNT_ATTACH_STATIC(tcp_reass_empty);
339 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 0);
340 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 1);
341 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 2);
342 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 3);
343 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 4);
344 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 5);
345 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 6);
346 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 7);
347 EVCNT_ATTACH_STATIC(tcp_reass_prependfirst);
348 EVCNT_ATTACH_STATIC(tcp_reass_prepend);
349 EVCNT_ATTACH_STATIC(tcp_reass_insert);
350 EVCNT_ATTACH_STATIC(tcp_reass_inserttail);
351 EVCNT_ATTACH_STATIC(tcp_reass_append);
352 EVCNT_ATTACH_STATIC(tcp_reass_appendtail);
353 EVCNT_ATTACH_STATIC(tcp_reass_overlaptail);
354 EVCNT_ATTACH_STATIC(tcp_reass_overlapfront);
355 EVCNT_ATTACH_STATIC(tcp_reass_segdup);
356 EVCNT_ATTACH_STATIC(tcp_reass_fragdup);
357 
358 #endif /* TCP_REASS_COUNTERS */
359 
360 #ifdef MBUFTRACE
361 struct mowner tcp_mowner = MOWNER_INIT("tcp", "");
362 struct mowner tcp_rx_mowner = MOWNER_INIT("tcp", "rx");
363 struct mowner tcp_tx_mowner = MOWNER_INIT("tcp", "tx");
364 struct mowner tcp_sock_mowner = MOWNER_INIT("tcp", "sock");
365 struct mowner tcp_sock_rx_mowner = MOWNER_INIT("tcp", "sock rx");
366 struct mowner tcp_sock_tx_mowner = MOWNER_INIT("tcp", "sock tx");
367 #endif
368 
369 static int
do_tcpinit(void)370 do_tcpinit(void)
371 {
372 
373 	inpcb_init(&tcbtable, tcbhashsize, tcbhashsize);
374 	pool_init(&tcpcb_pool, sizeof(struct tcpcb), 0, 0, 0, "tcpcbpl",
375 	    NULL, IPL_SOFTNET);
376 
377 	tcp_usrreq_init();
378 
379 	/* Initialize timer state. */
380 	tcp_timer_init();
381 
382 	/* Initialize the compressed state engine. */
383 	syn_cache_init();
384 
385 	/* Initialize the congestion control algorithms. */
386 	tcp_congctl_init();
387 
388 	/* Initialize the TCPCB template. */
389 	tcp_tcpcb_template();
390 
391 	/* Initialize reassembly queue */
392 	tcpipqent_init();
393 
394 	/* SACK */
395 	tcp_sack_init();
396 
397 	MOWNER_ATTACH(&tcp_tx_mowner);
398 	MOWNER_ATTACH(&tcp_rx_mowner);
399 	MOWNER_ATTACH(&tcp_reass_mowner);
400 	MOWNER_ATTACH(&tcp_sock_mowner);
401 	MOWNER_ATTACH(&tcp_sock_tx_mowner);
402 	MOWNER_ATTACH(&tcp_sock_rx_mowner);
403 	MOWNER_ATTACH(&tcp_mowner);
404 
405 	tcpstat_percpu = percpu_alloc(sizeof(uint64_t) * TCP_NSTATS);
406 
407 	vtw_earlyinit();
408 
409 	tcp_slowtimo_init();
410 
411 	return 0;
412 }
413 
414 void
tcp_init_common(unsigned basehlen)415 tcp_init_common(unsigned basehlen)
416 {
417 	static ONCE_DECL(dotcpinit);
418 	unsigned hlen = basehlen + sizeof(struct tcphdr);
419 	unsigned oldhlen;
420 
421 	if (max_linkhdr + hlen > MHLEN)
422 		panic("tcp_init");
423 	while ((oldhlen = max_protohdr) < hlen)
424 		atomic_cas_uint(&max_protohdr, oldhlen, hlen);
425 
426 	RUN_ONCE(&dotcpinit, do_tcpinit);
427 }
428 
429 /*
430  * Tcp initialization
431  */
432 void
tcp_init(void)433 tcp_init(void)
434 {
435 
436 	icmp_mtudisc_callback_register(tcp_mtudisc_callback);
437 
438 	tcp_init_common(sizeof(struct ip));
439 }
440 
441 /*
442  * Create template to be used to send tcp packets on a connection.
443  * Call after host entry created, allocates an mbuf and fills
444  * in a skeletal tcp/ip header, minimizing the amount of work
445  * necessary when the connection is used.
446  */
447 struct mbuf *
tcp_template(struct tcpcb * tp)448 tcp_template(struct tcpcb *tp)
449 {
450 	struct inpcb *inp = tp->t_inpcb;
451 	struct tcphdr *n;
452 	struct mbuf *m;
453 	int hlen;
454 
455 	switch (tp->t_family) {
456 	case AF_INET:
457 		hlen = sizeof(struct ip);
458 		if (inp->inp_af == AF_INET)
459 			break;
460 #ifdef INET6
461 		if (inp->inp_af == AF_INET6) {
462 			/* mapped addr case */
463 			if (IN6_IS_ADDR_V4MAPPED(&in6p_laddr(inp))
464 			 && IN6_IS_ADDR_V4MAPPED(&in6p_faddr(inp)))
465 				break;
466 		}
467 #endif
468 		return NULL;	/*EINVAL*/
469 #ifdef INET6
470 	case AF_INET6:
471 		hlen = sizeof(struct ip6_hdr);
472 		if (inp != NULL) {
473 			/* more sainty check? */
474 			break;
475 		}
476 		return NULL;	/*EINVAL*/
477 #endif
478 	default:
479 		return NULL;	/*EAFNOSUPPORT*/
480 	}
481 
482 	KASSERT(hlen + sizeof(struct tcphdr) <= MCLBYTES);
483 
484 	m = tp->t_template;
485 	if (m && m->m_len == hlen + sizeof(struct tcphdr)) {
486 		;
487 	} else {
488 		if (m)
489 			m_freem(m);
490 		m = tp->t_template = NULL;
491 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
492 		if (m && hlen + sizeof(struct tcphdr) > MHLEN) {
493 			MCLGET(m, M_DONTWAIT);
494 			if ((m->m_flags & M_EXT) == 0) {
495 				m_free(m);
496 				m = NULL;
497 			}
498 		}
499 		if (m == NULL)
500 			return NULL;
501 		MCLAIM(m, &tcp_mowner);
502 		m->m_pkthdr.len = m->m_len = hlen + sizeof(struct tcphdr);
503 	}
504 
505 	memset(mtod(m, void *), 0, m->m_len);
506 
507 	n = (struct tcphdr *)(mtod(m, char *) + hlen);
508 
509 	switch (tp->t_family) {
510 	case AF_INET:
511 	    {
512 		struct ipovly *ipov;
513 		mtod(m, struct ip *)->ip_v = 4;
514 		mtod(m, struct ip *)->ip_hl = hlen >> 2;
515 		ipov = mtod(m, struct ipovly *);
516 		ipov->ih_pr = IPPROTO_TCP;
517 		ipov->ih_len = htons(sizeof(struct tcphdr));
518 		if (inp->inp_af == AF_INET) {
519 			ipov->ih_src = in4p_laddr(inp);
520 			ipov->ih_dst = in4p_faddr(inp);
521 		}
522 #ifdef INET6
523 		else if (inp->inp_af == AF_INET6) {
524 			/* mapped addr case */
525 			bcopy(&in6p_laddr(inp).s6_addr32[3], &ipov->ih_src,
526 				sizeof(ipov->ih_src));
527 			bcopy(&in6p_faddr(inp).s6_addr32[3], &ipov->ih_dst,
528 				sizeof(ipov->ih_dst));
529 		}
530 #endif
531 
532 		/*
533 		 * Compute the pseudo-header portion of the checksum
534 		 * now.  We incrementally add in the TCP option and
535 		 * payload lengths later, and then compute the TCP
536 		 * checksum right before the packet is sent off onto
537 		 * the wire.
538 		 */
539 		n->th_sum = in_cksum_phdr(ipov->ih_src.s_addr,
540 		    ipov->ih_dst.s_addr,
541 		    htons(sizeof(struct tcphdr) + IPPROTO_TCP));
542 		break;
543 	    }
544 #ifdef INET6
545 	case AF_INET6:
546 	    {
547 		struct ip6_hdr *ip6;
548 		mtod(m, struct ip *)->ip_v = 6;
549 		ip6 = mtod(m, struct ip6_hdr *);
550 		ip6->ip6_nxt = IPPROTO_TCP;
551 		ip6->ip6_plen = htons(sizeof(struct tcphdr));
552 		ip6->ip6_src = in6p_laddr(inp);
553 		ip6->ip6_dst = in6p_faddr(inp);
554 		ip6->ip6_flow = in6p_flowinfo(inp) & IPV6_FLOWINFO_MASK;
555 		if (ip6_auto_flowlabel) {
556 			ip6->ip6_flow &= ~IPV6_FLOWLABEL_MASK;
557 			ip6->ip6_flow |=
558 			    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
559 		}
560 		ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
561 		ip6->ip6_vfc |= IPV6_VERSION;
562 
563 		/*
564 		 * Compute the pseudo-header portion of the checksum
565 		 * now.  We incrementally add in the TCP option and
566 		 * payload lengths later, and then compute the TCP
567 		 * checksum right before the packet is sent off onto
568 		 * the wire.
569 		 */
570 		n->th_sum = in6_cksum_phdr(&in6p_laddr(inp),
571 		    &in6p_faddr(inp), htonl(sizeof(struct tcphdr)),
572 		    htonl(IPPROTO_TCP));
573 		break;
574 	    }
575 #endif
576 	}
577 
578 	n->th_sport = inp->inp_lport;
579 	n->th_dport = inp->inp_fport;
580 
581 	n->th_seq = 0;
582 	n->th_ack = 0;
583 	n->th_x2 = 0;
584 	n->th_off = 5;
585 	n->th_flags = 0;
586 	n->th_win = 0;
587 	n->th_urp = 0;
588 	return m;
589 }
590 
591 /*
592  * Send a single message to the TCP at address specified by
593  * the given TCP/IP header.  If m == 0, then we make a copy
594  * of the tcpiphdr at ti and send directly to the addressed host.
595  * This is used to force keep alive messages out using the TCP
596  * template for a connection tp->t_template.  If flags are given
597  * then we send a message back to the TCP which originated the
598  * segment ti, and discard the mbuf containing it and any other
599  * attached mbufs.
600  *
601  * In any case the ack and sequence number of the transmitted
602  * segment are as specified by the parameters.
603  */
604 int
tcp_respond(struct tcpcb * tp,struct mbuf * mtemplate,struct mbuf * m,struct tcphdr * th0,tcp_seq ack,tcp_seq seq,int flags)605 tcp_respond(struct tcpcb *tp, struct mbuf *mtemplate, struct mbuf *m,
606     struct tcphdr *th0, tcp_seq ack, tcp_seq seq, int flags)
607 {
608 	struct route *ro;
609 	int error, tlen, win = 0;
610 	int hlen;
611 	struct ip *ip;
612 #ifdef INET6
613 	struct ip6_hdr *ip6;
614 #endif
615 	int family;	/* family on packet, not inpcb! */
616 	struct tcphdr *th;
617 
618 	if (tp != NULL && (flags & TH_RST) == 0) {
619 		KASSERT(tp->t_inpcb != NULL);
620 
621 		win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
622 	}
623 
624 	th = NULL;	/* Quell uninitialized warning */
625 	ip = NULL;
626 #ifdef INET6
627 	ip6 = NULL;
628 #endif
629 	if (m == NULL) {
630 		if (!mtemplate)
631 			return EINVAL;
632 
633 		/* get family information from template */
634 		switch (mtod(mtemplate, struct ip *)->ip_v) {
635 		case 4:
636 			family = AF_INET;
637 			hlen = sizeof(struct ip);
638 			break;
639 #ifdef INET6
640 		case 6:
641 			family = AF_INET6;
642 			hlen = sizeof(struct ip6_hdr);
643 			break;
644 #endif
645 		default:
646 			return EAFNOSUPPORT;
647 		}
648 
649 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
650 		if (m) {
651 			MCLAIM(m, &tcp_tx_mowner);
652 			MCLGET(m, M_DONTWAIT);
653 			if ((m->m_flags & M_EXT) == 0) {
654 				m_free(m);
655 				m = NULL;
656 			}
657 		}
658 		if (m == NULL)
659 			return ENOBUFS;
660 
661 		tlen = 0;
662 
663 		m->m_data += max_linkhdr;
664 		bcopy(mtod(mtemplate, void *), mtod(m, void *),
665 			mtemplate->m_len);
666 		switch (family) {
667 		case AF_INET:
668 			ip = mtod(m, struct ip *);
669 			th = (struct tcphdr *)(ip + 1);
670 			break;
671 #ifdef INET6
672 		case AF_INET6:
673 			ip6 = mtod(m, struct ip6_hdr *);
674 			th = (struct tcphdr *)(ip6 + 1);
675 			break;
676 #endif
677 		}
678 		flags = TH_ACK;
679 	} else {
680 		if ((m->m_flags & M_PKTHDR) == 0) {
681 			m_freem(m);
682 			return EINVAL;
683 		}
684 		KASSERT(th0 != NULL);
685 
686 		/* get family information from m */
687 		switch (mtod(m, struct ip *)->ip_v) {
688 		case 4:
689 			family = AF_INET;
690 			hlen = sizeof(struct ip);
691 			ip = mtod(m, struct ip *);
692 			break;
693 #ifdef INET6
694 		case 6:
695 			family = AF_INET6;
696 			hlen = sizeof(struct ip6_hdr);
697 			ip6 = mtod(m, struct ip6_hdr *);
698 			break;
699 #endif
700 		default:
701 			m_freem(m);
702 			return EAFNOSUPPORT;
703 		}
704 		/* clear h/w csum flags inherited from rx packet */
705 		m->m_pkthdr.csum_flags = 0;
706 
707 		if ((flags & TH_SYN) == 0 || sizeof(*th0) > (th0->th_off << 2))
708 			tlen = sizeof(*th0);
709 		else
710 			tlen = th0->th_off << 2;
711 
712 		if (m->m_len > hlen + tlen && (m->m_flags & M_EXT) == 0 &&
713 		    mtod(m, char *) + hlen == (char *)th0) {
714 			m->m_len = hlen + tlen;
715 			m_freem(m->m_next);
716 			m->m_next = NULL;
717 		} else {
718 			struct mbuf *n;
719 
720 			KASSERT(max_linkhdr + hlen + tlen <= MCLBYTES);
721 
722 			MGETHDR(n, M_DONTWAIT, MT_HEADER);
723 			if (n && max_linkhdr + hlen + tlen > MHLEN) {
724 				MCLGET(n, M_DONTWAIT);
725 				if ((n->m_flags & M_EXT) == 0) {
726 					m_freem(n);
727 					n = NULL;
728 				}
729 			}
730 			if (!n) {
731 				m_freem(m);
732 				return ENOBUFS;
733 			}
734 
735 			MCLAIM(n, &tcp_tx_mowner);
736 			n->m_data += max_linkhdr;
737 			n->m_len = hlen + tlen;
738 			m_copyback(n, 0, hlen, mtod(m, void *));
739 			m_copyback(n, hlen, tlen, (void *)th0);
740 
741 			m_freem(m);
742 			m = n;
743 			n = NULL;
744 		}
745 
746 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
747 		switch (family) {
748 		case AF_INET:
749 			ip = mtod(m, struct ip *);
750 			th = (struct tcphdr *)(ip + 1);
751 			ip->ip_p = IPPROTO_TCP;
752 			xchg(ip->ip_dst, ip->ip_src, struct in_addr);
753 			ip->ip_p = IPPROTO_TCP;
754 			break;
755 #ifdef INET6
756 		case AF_INET6:
757 			ip6 = mtod(m, struct ip6_hdr *);
758 			th = (struct tcphdr *)(ip6 + 1);
759 			ip6->ip6_nxt = IPPROTO_TCP;
760 			xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr);
761 			ip6->ip6_nxt = IPPROTO_TCP;
762 			break;
763 #endif
764 		}
765 		xchg(th->th_dport, th->th_sport, u_int16_t);
766 #undef xchg
767 		tlen = 0;	/*be friendly with the following code*/
768 	}
769 	th->th_seq = htonl(seq);
770 	th->th_ack = htonl(ack);
771 	th->th_x2 = 0;
772 	if ((flags & TH_SYN) == 0) {
773 		if (tp)
774 			win >>= tp->rcv_scale;
775 		if (win > TCP_MAXWIN)
776 			win = TCP_MAXWIN;
777 		th->th_win = htons((u_int16_t)win);
778 		th->th_off = sizeof (struct tcphdr) >> 2;
779 		tlen += sizeof(*th);
780 	} else {
781 		tlen += th->th_off << 2;
782 	}
783 	m->m_len = hlen + tlen;
784 	m->m_pkthdr.len = hlen + tlen;
785 	m_reset_rcvif(m);
786 	th->th_flags = flags;
787 	th->th_urp = 0;
788 
789 	switch (family) {
790 	case AF_INET:
791 	    {
792 		struct ipovly *ipov = (struct ipovly *)ip;
793 		memset(ipov->ih_x1, 0, sizeof ipov->ih_x1);
794 		ipov->ih_len = htons((u_int16_t)tlen);
795 
796 		th->th_sum = 0;
797 		th->th_sum = in_cksum(m, hlen + tlen);
798 		ip->ip_len = htons(hlen + tlen);
799 		ip->ip_ttl = ip_defttl;
800 		break;
801 	    }
802 #ifdef INET6
803 	case AF_INET6:
804 	    {
805 		th->th_sum = 0;
806 		th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
807 		    tlen);
808 		ip6->ip6_plen = htons(tlen);
809 		if (tp && tp->t_inpcb->inp_af == AF_INET6)
810 			ip6->ip6_hlim = in6pcb_selecthlim_rt(tp->t_inpcb);
811 		else
812 			ip6->ip6_hlim = ip6_defhlim;
813 		ip6->ip6_flow &= ~IPV6_FLOWINFO_MASK;
814 		if (ip6_auto_flowlabel) {
815 			ip6->ip6_flow |=
816 			    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
817 		}
818 		break;
819 	    }
820 #endif
821 	}
822 
823 	if (tp != NULL && tp->t_inpcb->inp_af == AF_INET) {
824 		ro = &tp->t_inpcb->inp_route;
825 		KASSERT(family == AF_INET);
826 		KASSERT(in_hosteq(ip->ip_dst, in4p_faddr(tp->t_inpcb)));
827 	}
828 #ifdef INET6
829 	else if (tp != NULL && tp->t_inpcb->inp_af == AF_INET6) {
830 		ro = (struct route *)&tp->t_inpcb->inp_route;
831 
832 #ifdef DIAGNOSTIC
833 		if (family == AF_INET) {
834 			if (!IN6_IS_ADDR_V4MAPPED(&in6p_faddr(tp->t_inpcb)))
835 				panic("tcp_respond: not mapped addr");
836 			if (memcmp(&ip->ip_dst,
837 			    &in6p_faddr(tp->t_inpcb).s6_addr32[3],
838 			    sizeof(ip->ip_dst)) != 0) {
839 				panic("tcp_respond: ip_dst != in6p_faddr");
840 			}
841 		} else if (family == AF_INET6) {
842 			if (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
843 			    &in6p_faddr(tp->t_inpcb)))
844 				panic("tcp_respond: ip6_dst != in6p_faddr");
845 		} else
846 			panic("tcp_respond: address family mismatch");
847 #endif
848 	}
849 #endif
850 	else
851 		ro = NULL;
852 
853 	switch (family) {
854 	case AF_INET:
855 		error = ip_output(m, NULL, ro,
856 		    (tp && tp->t_mtudisc ? IP_MTUDISC : 0), NULL,
857 		    tp ? tp->t_inpcb : NULL);
858 		break;
859 #ifdef INET6
860 	case AF_INET6:
861 		error = ip6_output(m, NULL, ro, 0, NULL,
862 		    tp ? tp->t_inpcb : NULL, NULL);
863 		break;
864 #endif
865 	default:
866 		error = EAFNOSUPPORT;
867 		break;
868 	}
869 
870 	return error;
871 }
872 
873 /*
874  * Template TCPCB.  Rather than zeroing a new TCPCB and initializing
875  * a bunch of members individually, we maintain this template for the
876  * static and mostly-static components of the TCPCB, and copy it into
877  * the new TCPCB instead.
878  */
879 static struct tcpcb tcpcb_template = {
880 	.t_srtt = TCPTV_SRTTBASE,
881 	.t_rttmin = TCPTV_MIN,
882 
883 	.snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT,
884 	.snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT,
885 	.snd_numholes = 0,
886 	.snd_cubic_wmax = 0,
887 	.snd_cubic_wmax_last = 0,
888 	.snd_cubic_ctime = 0,
889 
890 	.t_partialacks = -1,
891 	.t_bytes_acked = 0,
892 	.t_sndrexmitpack = 0,
893 	.t_rcvoopack = 0,
894 	.t_sndzerowin = 0,
895 };
896 
897 /*
898  * Updates the TCPCB template whenever a parameter that would affect
899  * the template is changed.
900  */
901 void
tcp_tcpcb_template(void)902 tcp_tcpcb_template(void)
903 {
904 	struct tcpcb *tp = &tcpcb_template;
905 	int flags;
906 
907 	tp->t_peermss = tcp_mssdflt;
908 	tp->t_ourmss = tcp_mssdflt;
909 	tp->t_segsz = tcp_mssdflt;
910 
911 	flags = 0;
912 	if (tcp_do_rfc1323 && tcp_do_win_scale)
913 		flags |= TF_REQ_SCALE;
914 	if (tcp_do_rfc1323 && tcp_do_timestamps)
915 		flags |= TF_REQ_TSTMP;
916 	tp->t_flags = flags;
917 
918 	/*
919 	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
920 	 * rtt estimate.  Set rttvar so that srtt + 2 * rttvar gives
921 	 * reasonable initial retransmit time.
922 	 */
923 	tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << (TCP_RTTVAR_SHIFT + 2 - 1);
924 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
925 	    TCPTV_MIN, TCPTV_REXMTMAX);
926 
927 	/* Keep Alive */
928 	tp->t_keepinit = MIN(tcp_keepinit, TCP_TIMER_MAXTICKS);
929 	tp->t_keepidle = MIN(tcp_keepidle, TCP_TIMER_MAXTICKS);
930 	tp->t_keepintvl = MIN(tcp_keepintvl, TCP_TIMER_MAXTICKS);
931 	tp->t_keepcnt = MAX(1, MIN(tcp_keepcnt, TCP_TIMER_MAXTICKS));
932 	tp->t_maxidle = tp->t_keepcnt * MIN(tp->t_keepintvl,
933 	    TCP_TIMER_MAXTICKS/tp->t_keepcnt);
934 
935 	/* MSL */
936 	tp->t_msl = TCPTV_MSL;
937 }
938 
939 /*
940  * Create a new TCP control block, making an
941  * empty reassembly queue and hooking it to the argument
942  * protocol control block.
943  */
944 struct tcpcb *
tcp_newtcpcb(int family,struct inpcb * inp)945 tcp_newtcpcb(int family, struct inpcb *inp)
946 {
947 	struct tcpcb *tp;
948 	int i;
949 
950 	/* XXX Consider using a pool_cache for speed. */
951 	tp = pool_get(&tcpcb_pool, PR_NOWAIT);	/* splsoftnet via tcp_usrreq */
952 	if (tp == NULL)
953 		return NULL;
954 	memcpy(tp, &tcpcb_template, sizeof(*tp));
955 	TAILQ_INIT(&tp->segq);
956 	TAILQ_INIT(&tp->timeq);
957 	tp->t_family = family;		/* may be overridden later on */
958 	TAILQ_INIT(&tp->snd_holes);
959 	LIST_INIT(&tp->t_sc);		/* XXX can template this */
960 
961 	/* Don't sweat this loop; hopefully the compiler will unroll it. */
962 	for (i = 0; i < TCPT_NTIMERS; i++) {
963 		callout_init(&tp->t_timer[i], CALLOUT_MPSAFE);
964 		TCP_TIMER_INIT(tp, i);
965 	}
966 	callout_init(&tp->t_delack_ch, CALLOUT_MPSAFE);
967 
968 	switch (family) {
969 	case AF_INET:
970 		in4p_ip(inp).ip_ttl = ip_defttl;
971 		inp->inp_ppcb = (void *)tp;
972 
973 		tp->t_inpcb = inp;
974 		tp->t_mtudisc = ip_mtudisc;
975 		break;
976 #ifdef INET6
977 	case AF_INET6:
978 		in6p_ip6(inp).ip6_hlim = in6pcb_selecthlim_rt(inp);
979 		inp->inp_ppcb = (void *)tp;
980 
981 		tp->t_inpcb = inp;
982 		/* for IPv6, always try to run path MTU discovery */
983 		tp->t_mtudisc = 1;
984 		break;
985 #endif /* INET6 */
986 	default:
987 		for (i = 0; i < TCPT_NTIMERS; i++)
988 			callout_destroy(&tp->t_timer[i]);
989 		callout_destroy(&tp->t_delack_ch);
990 		pool_put(&tcpcb_pool, tp);	/* splsoftnet via tcp_usrreq */
991 		return NULL;
992 	}
993 
994 	/*
995 	 * Initialize our timebase.  When we send timestamps, we take
996 	 * the delta from tcp_now -- this means each connection always
997 	 * gets a timebase of 1, which makes it, among other things,
998 	 * more difficult to determine how long a system has been up,
999 	 * and thus how many TCP sequence increments have occurred.
1000 	 *
1001 	 * We start with 1, because 0 doesn't work with linux, which
1002 	 * considers timestamp 0 in a SYN packet as a bug and disables
1003 	 * timestamps.
1004 	 */
1005 	tp->ts_timebase = tcp_now - 1;
1006 
1007 	tcp_congctl_select(tp, tcp_congctl_global_name);
1008 
1009 	return tp;
1010 }
1011 
1012 /*
1013  * Drop a TCP connection, reporting
1014  * the specified error.  If connection is synchronized,
1015  * then send a RST to peer.
1016  */
1017 struct tcpcb *
tcp_drop(struct tcpcb * tp,int errno)1018 tcp_drop(struct tcpcb *tp, int errno)
1019 {
1020 	struct socket *so;
1021 
1022 	KASSERT(tp->t_inpcb != NULL);
1023 
1024 	so = tp->t_inpcb->inp_socket;
1025 	if (so == NULL)
1026 		return NULL;
1027 
1028 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
1029 		tp->t_state = TCPS_CLOSED;
1030 		(void) tcp_output(tp);
1031 		TCP_STATINC(TCP_STAT_DROPS);
1032 	} else
1033 		TCP_STATINC(TCP_STAT_CONNDROPS);
1034 	if (errno == ETIMEDOUT && tp->t_softerror)
1035 		errno = tp->t_softerror;
1036 	so->so_error = errno;
1037 	return (tcp_close(tp));
1038 }
1039 
1040 /*
1041  * Close a TCP control block:
1042  *	discard all space held by the tcp
1043  *	discard internet protocol block
1044  *	wake up any sleepers
1045  */
1046 struct tcpcb *
tcp_close(struct tcpcb * tp)1047 tcp_close(struct tcpcb *tp)
1048 {
1049 	struct inpcb *inp;
1050 	struct socket *so;
1051 #ifdef RTV_RTT
1052 	struct rtentry *rt = NULL;
1053 #endif
1054 	struct route *ro;
1055 	int j;
1056 
1057 	inp = tp->t_inpcb;
1058 	so = inp->inp_socket;
1059 	ro = &inp->inp_route;
1060 
1061 #ifdef RTV_RTT
1062 	/*
1063 	 * If we sent enough data to get some meaningful characteristics,
1064 	 * save them in the routing entry.  'Enough' is arbitrarily
1065 	 * defined as the sendpipesize (default 4K) * 16.  This would
1066 	 * give us 16 rtt samples assuming we only get one sample per
1067 	 * window (the usual case on a long haul net).  16 samples is
1068 	 * enough for the srtt filter to converge to within 5% of the correct
1069 	 * value; fewer samples and we could save a very bogus rtt.
1070 	 *
1071 	 * Don't update the default route's characteristics and don't
1072 	 * update anything that the user "locked".
1073 	 */
1074 	if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) &&
1075 	    ro && (rt = rtcache_validate(ro)) != NULL &&
1076 	    !in_nullhost(satocsin(rt_getkey(rt))->sin_addr)) {
1077 		u_long i = 0;
1078 
1079 		if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
1080 			i = tp->t_srtt *
1081 			    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
1082 			if (rt->rt_rmx.rmx_rtt && i)
1083 				/*
1084 				 * filter this update to half the old & half
1085 				 * the new values, converting scale.
1086 				 * See route.h and tcp_var.h for a
1087 				 * description of the scaling constants.
1088 				 */
1089 				rt->rt_rmx.rmx_rtt =
1090 				    (rt->rt_rmx.rmx_rtt + i) / 2;
1091 			else
1092 				rt->rt_rmx.rmx_rtt = i;
1093 		}
1094 		if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
1095 			i = tp->t_rttvar *
1096 			    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTTVAR_SHIFT + 2));
1097 			if (rt->rt_rmx.rmx_rttvar && i)
1098 				rt->rt_rmx.rmx_rttvar =
1099 				    (rt->rt_rmx.rmx_rttvar + i) / 2;
1100 			else
1101 				rt->rt_rmx.rmx_rttvar = i;
1102 		}
1103 		/*
1104 		 * update the pipelimit (ssthresh) if it has been updated
1105 		 * already or if a pipesize was specified & the threshold
1106 		 * got below half the pipesize.  I.e., wait for bad news
1107 		 * before we start updating, then update on both good
1108 		 * and bad news.
1109 		 */
1110 		if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
1111 		    (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh) ||
1112 		    i < (rt->rt_rmx.rmx_sendpipe / 2)) {
1113 			/*
1114 			 * convert the limit from user data bytes to
1115 			 * packets then to packet data bytes.
1116 			 */
1117 			i = (i + tp->t_segsz / 2) / tp->t_segsz;
1118 			if (i < 2)
1119 				i = 2;
1120 			i *= (u_long)(tp->t_segsz + sizeof (struct tcpiphdr));
1121 			if (rt->rt_rmx.rmx_ssthresh)
1122 				rt->rt_rmx.rmx_ssthresh =
1123 				    (rt->rt_rmx.rmx_ssthresh + i) / 2;
1124 			else
1125 				rt->rt_rmx.rmx_ssthresh = i;
1126 		}
1127 	}
1128 	rtcache_unref(rt, ro);
1129 #endif /* RTV_RTT */
1130 	/* free the reassembly queue, if any */
1131 	TCP_REASS_LOCK(tp);
1132 	(void) tcp_freeq(tp);
1133 	TCP_REASS_UNLOCK(tp);
1134 
1135 	/* free the SACK holes list. */
1136 	tcp_free_sackholes(tp);
1137 	tcp_congctl_release(tp);
1138 	syn_cache_cleanup(tp);
1139 
1140 	if (tp->t_template) {
1141 		m_free(tp->t_template);
1142 		tp->t_template = NULL;
1143 	}
1144 
1145 	/*
1146 	 * Detaching the pcb will unlock the socket/tcpcb, and stopping
1147 	 * the timers can also drop the lock.  We need to prevent access
1148 	 * to the tcpcb as it's half torn down.  Flag the pcb as dead
1149 	 * (prevents access by timers) and only then detach it.
1150 	 */
1151 	tp->t_flags |= TF_DEAD;
1152 	inp->inp_ppcb = NULL;
1153 	soisdisconnected(so);
1154 	inpcb_destroy(inp);
1155 	/*
1156 	 * pcb is no longer visble elsewhere, so we can safely release
1157 	 * the lock in callout_halt() if needed.
1158 	 */
1159 	TCP_STATINC(TCP_STAT_CLOSED);
1160 	for (j = 0; j < TCPT_NTIMERS; j++) {
1161 		callout_halt(&tp->t_timer[j], softnet_lock);
1162 		callout_destroy(&tp->t_timer[j]);
1163 	}
1164 	callout_halt(&tp->t_delack_ch, softnet_lock);
1165 	callout_destroy(&tp->t_delack_ch);
1166 	pool_put(&tcpcb_pool, tp);
1167 
1168 	return NULL;
1169 }
1170 
1171 int
tcp_freeq(struct tcpcb * tp)1172 tcp_freeq(struct tcpcb *tp)
1173 {
1174 	struct ipqent *qe;
1175 	int rv = 0;
1176 
1177 	TCP_REASS_LOCK_CHECK(tp);
1178 
1179 	while ((qe = TAILQ_FIRST(&tp->segq)) != NULL) {
1180 		TAILQ_REMOVE(&tp->segq, qe, ipqe_q);
1181 		TAILQ_REMOVE(&tp->timeq, qe, ipqe_timeq);
1182 		m_freem(qe->ipqe_m);
1183 		tcpipqent_free(qe);
1184 		rv = 1;
1185 	}
1186 	tp->t_segqlen = 0;
1187 	KASSERT(TAILQ_EMPTY(&tp->timeq));
1188 	return (rv);
1189 }
1190 
1191 void
tcp_fasttimo(void)1192 tcp_fasttimo(void)
1193 {
1194 	if (tcp_drainwanted) {
1195 		tcp_drain();
1196 		tcp_drainwanted = 0;
1197 	}
1198 }
1199 
1200 void
tcp_drainstub(void)1201 tcp_drainstub(void)
1202 {
1203 	tcp_drainwanted = 1;
1204 }
1205 
1206 /*
1207  * Protocol drain routine.  Called when memory is in short supply.
1208  * Called from pr_fasttimo thus a callout context.
1209  */
1210 void
tcp_drain(void)1211 tcp_drain(void)
1212 {
1213 	struct inpcb *inp;
1214 	struct tcpcb *tp;
1215 
1216 	mutex_enter(softnet_lock);
1217 	KERNEL_LOCK(1, NULL);
1218 
1219 	/*
1220 	 * Free the sequence queue of all TCP connections.
1221 	 */
1222 	TAILQ_FOREACH(inp, &tcbtable.inpt_queue, inp_queue) {
1223 		tp = intotcpcb(inp);
1224 		if (tp != NULL) {
1225 			/*
1226 			 * If the tcpcb is already busy,
1227 			 * just bail out now.
1228 			 */
1229 			if (tcp_reass_lock_try(tp) == 0)
1230 				continue;
1231 			if (tcp_freeq(tp))
1232 				TCP_STATINC(TCP_STAT_CONNSDRAINED);
1233 			TCP_REASS_UNLOCK(tp);
1234 		}
1235 	}
1236 
1237 	KERNEL_UNLOCK_ONE(NULL);
1238 	mutex_exit(softnet_lock);
1239 }
1240 
1241 /*
1242  * Notify a tcp user of an asynchronous error;
1243  * store error as soft error, but wake up user
1244  * (for now, won't do anything until can select for soft error).
1245  */
1246 void
tcp_notify(struct inpcb * inp,int error)1247 tcp_notify(struct inpcb *inp, int error)
1248 {
1249 	struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
1250 	struct socket *so = inp->inp_socket;
1251 
1252 	/*
1253 	 * Ignore some errors if we are hooked up.
1254 	 * If connection hasn't completed, has retransmitted several times,
1255 	 * and receives a second error, give up now.  This is better
1256 	 * than waiting a long time to establish a connection that
1257 	 * can never complete.
1258 	 */
1259 	if (tp->t_state == TCPS_ESTABLISHED &&
1260 	     (error == EHOSTUNREACH || error == ENETUNREACH ||
1261 	      error == EHOSTDOWN)) {
1262 		return;
1263 	} else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
1264 	    tp->t_rxtshift > 3 && tp->t_softerror)
1265 		so->so_error = error;
1266 	else
1267 		tp->t_softerror = error;
1268 	cv_broadcast(&so->so_cv);
1269 	sorwakeup(so);
1270 	sowwakeup(so);
1271 }
1272 
1273 #ifdef INET6
1274 void *
tcp6_ctlinput(int cmd,const struct sockaddr * sa,void * d)1275 tcp6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
1276 {
1277 	struct tcphdr th;
1278 	void (*notify)(struct inpcb *, int) = tcp_notify;
1279 	int nmatch;
1280 	struct ip6_hdr *ip6;
1281 	const struct sockaddr_in6 *sa6_src = NULL;
1282 	const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa;
1283 	struct mbuf *m;
1284 	int off;
1285 
1286 	if (sa->sa_family != AF_INET6 ||
1287 	    sa->sa_len != sizeof(struct sockaddr_in6))
1288 		return NULL;
1289 	if ((unsigned)cmd >= PRC_NCMDS)
1290 		return NULL;
1291 	else if (cmd == PRC_QUENCH) {
1292 		/*
1293 		 * Don't honor ICMP Source Quench messages meant for
1294 		 * TCP connections.
1295 		 */
1296 		return NULL;
1297 	} else if (PRC_IS_REDIRECT(cmd))
1298 		notify = in6pcb_rtchange, d = NULL;
1299 	else if (cmd == PRC_MSGSIZE)
1300 		; /* special code is present, see below */
1301 	else if (cmd == PRC_HOSTDEAD)
1302 		d = NULL;
1303 	else if (inet6ctlerrmap[cmd] == 0)
1304 		return NULL;
1305 
1306 	/* if the parameter is from icmp6, decode it. */
1307 	if (d != NULL) {
1308 		struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
1309 		m = ip6cp->ip6c_m;
1310 		ip6 = ip6cp->ip6c_ip6;
1311 		off = ip6cp->ip6c_off;
1312 		sa6_src = ip6cp->ip6c_src;
1313 	} else {
1314 		m = NULL;
1315 		ip6 = NULL;
1316 		sa6_src = &sa6_any;
1317 		off = 0;
1318 	}
1319 
1320 	if (ip6) {
1321 		/* check if we can safely examine src and dst ports */
1322 		if (m->m_pkthdr.len < off + sizeof(th)) {
1323 			if (cmd == PRC_MSGSIZE)
1324 				icmp6_mtudisc_update((struct ip6ctlparam *)d, 0);
1325 			return NULL;
1326 		}
1327 
1328 		memset(&th, 0, sizeof(th));
1329 		m_copydata(m, off, sizeof(th), (void *)&th);
1330 
1331 		if (cmd == PRC_MSGSIZE) {
1332 			int valid = 0;
1333 
1334 			/*
1335 			 * Check to see if we have a valid TCP connection
1336 			 * corresponding to the address in the ICMPv6 message
1337 			 * payload.
1338 			 */
1339 			if (in6pcb_lookup(&tcbtable, &sa6->sin6_addr,
1340 			    th.th_dport,
1341 			    (const struct in6_addr *)&sa6_src->sin6_addr,
1342 						  th.th_sport, 0, 0))
1343 				valid++;
1344 
1345 			/*
1346 			 * Depending on the value of "valid" and routing table
1347 			 * size (mtudisc_{hi,lo}wat), we will:
1348 			 * - recalcurate the new MTU and create the
1349 			 *   corresponding routing entry, or
1350 			 * - ignore the MTU change notification.
1351 			 */
1352 			icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
1353 
1354 			/*
1355 			 * no need to call in6pcb_notify, it should have been
1356 			 * called via callback if necessary
1357 			 */
1358 			return NULL;
1359 		}
1360 
1361 		nmatch = in6pcb_notify(&tcbtable, sa, th.th_dport,
1362 		    (const struct sockaddr *)sa6_src, th.th_sport, cmd, NULL, notify);
1363 		if (nmatch == 0 && syn_cache_count &&
1364 		    (inet6ctlerrmap[cmd] == EHOSTUNREACH ||
1365 		     inet6ctlerrmap[cmd] == ENETUNREACH ||
1366 		     inet6ctlerrmap[cmd] == EHOSTDOWN))
1367 			syn_cache_unreach((const struct sockaddr *)sa6_src,
1368 					  sa, &th);
1369 	} else {
1370 		(void) in6pcb_notify(&tcbtable, sa, 0,
1371 		    (const struct sockaddr *)sa6_src, 0, cmd, NULL, notify);
1372 	}
1373 
1374 	return NULL;
1375 }
1376 #endif
1377 
1378 /* assumes that ip header and tcp header are contiguous on mbuf */
1379 void *
tcp_ctlinput(int cmd,const struct sockaddr * sa,void * v)1380 tcp_ctlinput(int cmd, const struct sockaddr *sa, void *v)
1381 {
1382 	struct ip *ip = v;
1383 	struct tcphdr *th;
1384 	struct icmp *icp;
1385 	extern const int inetctlerrmap[];
1386 	void (*notify)(struct inpcb *, int) = tcp_notify;
1387 	int errno;
1388 	int nmatch;
1389 	struct tcpcb *tp;
1390 	u_int mtu;
1391 	tcp_seq seq;
1392 	struct inpcb *inp;
1393 #ifdef INET6
1394 	struct in6_addr src6, dst6;
1395 #endif
1396 
1397 	if (sa->sa_family != AF_INET ||
1398 	    sa->sa_len != sizeof(struct sockaddr_in))
1399 		return NULL;
1400 	if ((unsigned)cmd >= PRC_NCMDS)
1401 		return NULL;
1402 	errno = inetctlerrmap[cmd];
1403 	if (cmd == PRC_QUENCH)
1404 		/*
1405 		 * Don't honor ICMP Source Quench messages meant for
1406 		 * TCP connections.
1407 		 */
1408 		return NULL;
1409 	else if (PRC_IS_REDIRECT(cmd))
1410 		notify = inpcb_rtchange, ip = 0;
1411 	else if (cmd == PRC_MSGSIZE && ip && ip->ip_v == 4) {
1412 		/*
1413 		 * Check to see if we have a valid TCP connection
1414 		 * corresponding to the address in the ICMP message
1415 		 * payload.
1416 		 *
1417 		 * Boundary check is made in icmp_input(), with ICMP_ADVLENMIN.
1418 		 */
1419 		th = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2));
1420 #ifdef INET6
1421 		in6_in_2_v4mapin6(&ip->ip_src, &src6);
1422 		in6_in_2_v4mapin6(&ip->ip_dst, &dst6);
1423 #endif
1424 		if ((inp = inpcb_lookup(&tcbtable, ip->ip_dst,
1425 		    th->th_dport, ip->ip_src, th->th_sport, 0)) != NULL)
1426 			;
1427 #ifdef INET6
1428 		else if ((inp = in6pcb_lookup(&tcbtable, &dst6,
1429 		    th->th_dport, &src6, th->th_sport, 0, 0)) != NULL)
1430 			;
1431 #endif
1432 		else
1433 			return NULL;
1434 
1435 		/*
1436 		 * Now that we've validated that we are actually communicating
1437 		 * with the host indicated in the ICMP message, locate the
1438 		 * ICMP header, recalculate the new MTU, and create the
1439 		 * corresponding routing entry.
1440 		 */
1441 		icp = (struct icmp *)((char *)ip -
1442 		    offsetof(struct icmp, icmp_ip));
1443 		tp = intotcpcb(inp);
1444 		if (tp == NULL)
1445 			return NULL;
1446 		seq = ntohl(th->th_seq);
1447 		if (SEQ_LT(seq, tp->snd_una) || SEQ_GT(seq, tp->snd_max))
1448 			return NULL;
1449 		/*
1450 		 * If the ICMP message advertises a Next-Hop MTU
1451 		 * equal or larger than the maximum packet size we have
1452 		 * ever sent, drop the message.
1453 		 */
1454 		mtu = (u_int)ntohs(icp->icmp_nextmtu);
1455 		if (mtu >= tp->t_pmtud_mtu_sent)
1456 			return NULL;
1457 		if (mtu >= tcp_hdrsz(tp) + tp->t_pmtud_mss_acked) {
1458 			/*
1459 			 * Calculate new MTU, and create corresponding
1460 			 * route (traditional PMTUD).
1461 			 */
1462 			tp->t_flags &= ~TF_PMTUD_PEND;
1463 			icmp_mtudisc(icp, ip->ip_dst);
1464 		} else {
1465 			/*
1466 			 * Record the information got in the ICMP
1467 			 * message; act on it later.
1468 			 * If we had already recorded an ICMP message,
1469 			 * replace the old one only if the new message
1470 			 * refers to an older TCP segment
1471 			 */
1472 			if (tp->t_flags & TF_PMTUD_PEND) {
1473 				if (SEQ_LT(tp->t_pmtud_th_seq, seq))
1474 					return NULL;
1475 			} else
1476 				tp->t_flags |= TF_PMTUD_PEND;
1477 			tp->t_pmtud_th_seq = seq;
1478 			tp->t_pmtud_nextmtu = icp->icmp_nextmtu;
1479 			tp->t_pmtud_ip_len = icp->icmp_ip.ip_len;
1480 			tp->t_pmtud_ip_hl = icp->icmp_ip.ip_hl;
1481 		}
1482 		return NULL;
1483 	} else if (cmd == PRC_HOSTDEAD)
1484 		ip = 0;
1485 	else if (errno == 0)
1486 		return NULL;
1487 	if (ip && ip->ip_v == 4 && sa->sa_family == AF_INET) {
1488 		th = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2));
1489 		nmatch = inpcb_notify(&tcbtable, satocsin(sa)->sin_addr,
1490 		    th->th_dport, ip->ip_src, th->th_sport, errno, notify);
1491 		if (nmatch == 0 && syn_cache_count &&
1492 		    (inetctlerrmap[cmd] == EHOSTUNREACH ||
1493 		    inetctlerrmap[cmd] == ENETUNREACH ||
1494 		    inetctlerrmap[cmd] == EHOSTDOWN)) {
1495 			struct sockaddr_in sin;
1496 			memset(&sin, 0, sizeof(sin));
1497 			sin.sin_len = sizeof(sin);
1498 			sin.sin_family = AF_INET;
1499 			sin.sin_port = th->th_sport;
1500 			sin.sin_addr = ip->ip_src;
1501 			syn_cache_unreach((struct sockaddr *)&sin, sa, th);
1502 		}
1503 
1504 		/* XXX mapped address case */
1505 	} else
1506 		inpcb_notifyall(&tcbtable, satocsin(sa)->sin_addr, errno,
1507 		    notify);
1508 	return NULL;
1509 }
1510 
1511 /*
1512  * When a source quench is received, we are being notified of congestion.
1513  * Close the congestion window down to the Loss Window (one segment).
1514  * We will gradually open it again as we proceed.
1515  */
1516 void
tcp_quench(struct inpcb * inp)1517 tcp_quench(struct inpcb *inp)
1518 {
1519 	struct tcpcb *tp = intotcpcb(inp);
1520 
1521 	if (tp) {
1522 		tp->snd_cwnd = tp->t_segsz;
1523 		tp->t_bytes_acked = 0;
1524 	}
1525 }
1526 
1527 /*
1528  * Path MTU Discovery handlers.
1529  */
1530 void
tcp_mtudisc_callback(struct in_addr faddr)1531 tcp_mtudisc_callback(struct in_addr faddr)
1532 {
1533 #ifdef INET6
1534 	struct in6_addr in6;
1535 #endif
1536 
1537 	inpcb_notifyall(&tcbtable, faddr, EMSGSIZE, tcp_mtudisc);
1538 #ifdef INET6
1539 	in6_in_2_v4mapin6(&faddr, &in6);
1540 	tcp6_mtudisc_callback(&in6);
1541 #endif
1542 }
1543 
1544 /*
1545  * On receipt of path MTU corrections, flush old route and replace it
1546  * with the new one.  Retransmit all unacknowledged packets, to ensure
1547  * that all packets will be received.
1548  */
1549 void
tcp_mtudisc(struct inpcb * inp,int errno)1550 tcp_mtudisc(struct inpcb *inp, int errno)
1551 {
1552 	struct tcpcb *tp = intotcpcb(inp);
1553 	struct rtentry *rt;
1554 
1555 	if (tp == NULL)
1556 		return;
1557 
1558 	rt = inpcb_rtentry(inp);
1559 	if (rt != NULL) {
1560 		/*
1561 		 * If this was not a host route, remove and realloc.
1562 		 */
1563 		if ((rt->rt_flags & RTF_HOST) == 0) {
1564 			inpcb_rtentry_unref(rt, inp);
1565 			inpcb_rtchange(inp, errno);
1566 			if ((rt = inpcb_rtentry(inp)) == NULL)
1567 				return;
1568 		}
1569 
1570 		/*
1571 		 * Slow start out of the error condition.  We
1572 		 * use the MTU because we know it's smaller
1573 		 * than the previously transmitted segment.
1574 		 *
1575 		 * Note: This is more conservative than the
1576 		 * suggestion in draft-floyd-incr-init-win-03.
1577 		 */
1578 		if (rt->rt_rmx.rmx_mtu != 0)
1579 			tp->snd_cwnd =
1580 			    TCP_INITIAL_WINDOW(tcp_init_win,
1581 			    rt->rt_rmx.rmx_mtu);
1582 		inpcb_rtentry_unref(rt, inp);
1583 	}
1584 
1585 	/*
1586 	 * Resend unacknowledged packets.
1587 	 */
1588 	tp->snd_nxt = tp->sack_newdata = tp->snd_una;
1589 	tcp_output(tp);
1590 }
1591 
1592 #ifdef INET6
1593 /*
1594  * Path MTU Discovery handlers.
1595  */
1596 void
tcp6_mtudisc_callback(struct in6_addr * faddr)1597 tcp6_mtudisc_callback(struct in6_addr *faddr)
1598 {
1599 	struct sockaddr_in6 sin6;
1600 
1601 	memset(&sin6, 0, sizeof(sin6));
1602 	sin6.sin6_family = AF_INET6;
1603 	sin6.sin6_len = sizeof(struct sockaddr_in6);
1604 	sin6.sin6_addr = *faddr;
1605 	(void) in6pcb_notify(&tcbtable, (struct sockaddr *)&sin6, 0,
1606 	    (const struct sockaddr *)&sa6_any, 0, PRC_MSGSIZE, NULL, tcp6_mtudisc);
1607 }
1608 
1609 void
tcp6_mtudisc(struct inpcb * inp,int errno)1610 tcp6_mtudisc(struct inpcb *inp, int errno)
1611 {
1612 	struct tcpcb *tp = intotcpcb(inp);
1613 	struct rtentry *rt;
1614 
1615 	if (tp == NULL)
1616 		return;
1617 
1618 	rt = in6pcb_rtentry(inp);
1619 	if (rt != NULL) {
1620 		/*
1621 		 * If this was not a host route, remove and realloc.
1622 		 */
1623 		if ((rt->rt_flags & RTF_HOST) == 0) {
1624 			in6pcb_rtentry_unref(rt, inp);
1625 			in6pcb_rtchange(inp, errno);
1626 			rt = in6pcb_rtentry(inp);
1627 			if (rt == NULL)
1628 				return;
1629 		}
1630 
1631 		/*
1632 		 * Slow start out of the error condition.  We
1633 		 * use the MTU because we know it's smaller
1634 		 * than the previously transmitted segment.
1635 		 *
1636 		 * Note: This is more conservative than the
1637 		 * suggestion in draft-floyd-incr-init-win-03.
1638 		 */
1639 		if (rt->rt_rmx.rmx_mtu != 0) {
1640 			tp->snd_cwnd = TCP_INITIAL_WINDOW(tcp_init_win,
1641 			    rt->rt_rmx.rmx_mtu);
1642 		}
1643 		in6pcb_rtentry_unref(rt, inp);
1644 	}
1645 
1646 	/*
1647 	 * Resend unacknowledged packets.
1648 	 */
1649 	tp->snd_nxt = tp->sack_newdata = tp->snd_una;
1650 	tcp_output(tp);
1651 }
1652 #endif /* INET6 */
1653 
1654 /*
1655  * Compute the MSS to advertise to the peer.  Called only during
1656  * the 3-way handshake.  If we are the server (peer initiated
1657  * connection), we are called with a pointer to the interface
1658  * on which the SYN packet arrived.  If we are the client (we
1659  * initiated connection), we are called with a pointer to the
1660  * interface out which this connection should go.
1661  *
1662  * NOTE: Do not subtract IP option/extension header size nor IPsec
1663  * header size from MSS advertisement.  MSS option must hold the maximum
1664  * segment size we can accept, so it must always be:
1665  *	 max(if mtu) - ip header - tcp header
1666  */
1667 u_long
tcp_mss_to_advertise(const struct ifnet * ifp,int af)1668 tcp_mss_to_advertise(const struct ifnet *ifp, int af)
1669 {
1670 	extern u_long in_maxmtu;
1671 	u_long mss = 0;
1672 	u_long hdrsiz;
1673 
1674 	/*
1675 	 * In order to avoid defeating path MTU discovery on the peer,
1676 	 * we advertise the max MTU of all attached networks as our MSS,
1677 	 * per RFC 1191, section 3.1.
1678 	 *
1679 	 * We provide the option to advertise just the MTU of
1680 	 * the interface on which we hope this connection will
1681 	 * be receiving.  If we are responding to a SYN, we
1682 	 * will have a pretty good idea about this, but when
1683 	 * initiating a connection there is a bit more doubt.
1684 	 *
1685 	 * We also need to ensure that loopback has a large enough
1686 	 * MSS, as the loopback MTU is never included in in_maxmtu.
1687 	 */
1688 
1689 	if (ifp != NULL)
1690 		switch (af) {
1691 #ifdef INET6
1692 		case AF_INET6:	/* FALLTHROUGH */
1693 #endif
1694 		case AF_INET:
1695 			mss = ifp->if_mtu;
1696 			break;
1697 		}
1698 
1699 	if (tcp_mss_ifmtu == 0)
1700 		switch (af) {
1701 #ifdef INET6
1702 		case AF_INET6:	/* FALLTHROUGH */
1703 #endif
1704 		case AF_INET:
1705 			mss = uimax(in_maxmtu, mss);
1706 			break;
1707 		}
1708 
1709 	switch (af) {
1710 	case AF_INET:
1711 		hdrsiz = sizeof(struct ip);
1712 		break;
1713 #ifdef INET6
1714 	case AF_INET6:
1715 		hdrsiz = sizeof(struct ip6_hdr);
1716 		break;
1717 #endif
1718 	default:
1719 		hdrsiz = 0;
1720 		break;
1721 	}
1722 	hdrsiz += sizeof(struct tcphdr);
1723 	if (mss > hdrsiz)
1724 		mss -= hdrsiz;
1725 
1726 	mss = uimax(tcp_mssdflt, mss);
1727 	return (mss);
1728 }
1729 
1730 /*
1731  * Set connection variables based on the peer's advertised MSS.
1732  * We are passed the TCPCB for the actual connection.  If we
1733  * are the server, we are called by the compressed state engine
1734  * when the 3-way handshake is complete.  If we are the client,
1735  * we are called when we receive the SYN,ACK from the server.
1736  *
1737  * NOTE: Our advertised MSS value must be initialized in the TCPCB
1738  * before this routine is called!
1739  */
1740 void
tcp_mss_from_peer(struct tcpcb * tp,int offer)1741 tcp_mss_from_peer(struct tcpcb *tp, int offer)
1742 {
1743 	struct socket *so;
1744 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
1745 	struct rtentry *rt;
1746 #endif
1747 	u_long bufsize;
1748 	int mss;
1749 
1750 	KASSERT(tp->t_inpcb != NULL);
1751 
1752 	so = NULL;
1753 	rt = NULL;
1754 
1755 	so = tp->t_inpcb->inp_socket;
1756 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
1757 	rt = inpcb_rtentry(tp->t_inpcb);
1758 #endif
1759 
1760 	/*
1761 	 * As per RFC1122, use the default MSS value, unless they
1762 	 * sent us an offer.  Do not accept offers less than 256 bytes.
1763 	 */
1764 	mss = tcp_mssdflt;
1765 	if (offer)
1766 		mss = offer;
1767 	mss = uimax(mss, 256);		/* sanity */
1768 	tp->t_peermss = mss;
1769 	mss -= tcp_optlen(tp);
1770 	if (tp->t_inpcb->inp_af == AF_INET)
1771 		mss -= ip_optlen(tp->t_inpcb);
1772 #ifdef INET6
1773 	if (tp->t_inpcb->inp_af == AF_INET6)
1774 		mss -= ip6_optlen(tp->t_inpcb);
1775 #endif
1776 	/*
1777 	 * XXX XXX What if mss goes negative or zero? This can happen if a
1778 	 * socket has large IPv6 options. We crash below.
1779 	 */
1780 
1781 	/*
1782 	 * If there's a pipesize, change the socket buffer to that size.
1783 	 * Make the socket buffer an integral number of MSS units.  If
1784 	 * the MSS is larger than the socket buffer, artificially decrease
1785 	 * the MSS.
1786 	 */
1787 #ifdef RTV_SPIPE
1788 	if (rt != NULL && rt->rt_rmx.rmx_sendpipe != 0)
1789 		bufsize = rt->rt_rmx.rmx_sendpipe;
1790 	else
1791 #endif
1792 	{
1793 		KASSERT(so != NULL);
1794 		bufsize = so->so_snd.sb_hiwat;
1795 	}
1796 	if (bufsize < mss)
1797 		mss = bufsize;
1798 	else {
1799 		bufsize = roundup(bufsize, mss);
1800 		if (bufsize > sb_max)
1801 			bufsize = sb_max;
1802 		(void) sbreserve(&so->so_snd, bufsize, so);
1803 	}
1804 	tp->t_segsz = mss;
1805 
1806 #ifdef RTV_SSTHRESH
1807 	if (rt != NULL && rt->rt_rmx.rmx_ssthresh) {
1808 		/*
1809 		 * There's some sort of gateway or interface buffer
1810 		 * limit on the path.  Use this to set the slow
1811 		 * start threshold, but set the threshold to no less
1812 		 * than 2 * MSS.
1813 		 */
1814 		tp->snd_ssthresh = uimax(2 * mss, rt->rt_rmx.rmx_ssthresh);
1815 	}
1816 #endif
1817 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
1818 	inpcb_rtentry_unref(rt, tp->t_inpcb);
1819 #endif
1820 }
1821 
1822 /*
1823  * Processing necessary when a TCP connection is established.
1824  */
1825 void
tcp_established(struct tcpcb * tp)1826 tcp_established(struct tcpcb *tp)
1827 {
1828 	struct socket *so;
1829 #ifdef RTV_RPIPE
1830 	struct rtentry *rt;
1831 #endif
1832 	u_long bufsize;
1833 
1834 	KASSERT(tp->t_inpcb != NULL);
1835 
1836 	so = NULL;
1837 	rt = NULL;
1838 
1839 	/* This is a while() to reduce the dreadful stairstepping below */
1840 	while (tp->t_inpcb->inp_af == AF_INET) {
1841 		so = tp->t_inpcb->inp_socket;
1842 #if defined(RTV_RPIPE)
1843 		rt = inpcb_rtentry(tp->t_inpcb);
1844 #endif
1845 		if (__predict_true(tcp_msl_enable)) {
1846 			if (in4p_laddr(tp->t_inpcb).s_addr == INADDR_LOOPBACK) {
1847 				tp->t_msl = tcp_msl_loop ? tcp_msl_loop : (TCPTV_MSL >> 2);
1848 				break;
1849 			}
1850 
1851 			if (__predict_false(tcp_rttlocal)) {
1852 				/* This may be adjusted by tcp_input */
1853 				tp->t_msl = tcp_msl_local ? tcp_msl_local : (TCPTV_MSL >> 1);
1854 				break;
1855 			}
1856 			if (in_localaddr(in4p_faddr(tp->t_inpcb))) {
1857 				tp->t_msl = tcp_msl_local ? tcp_msl_local : (TCPTV_MSL >> 1);
1858 				break;
1859 			}
1860 		}
1861 		tp->t_msl = tcp_msl_remote ? tcp_msl_remote : TCPTV_MSL;
1862 		break;
1863 	}
1864 
1865 	/* Clamp to a reasonable range.  */
1866 	tp->t_msl = MIN(tp->t_msl, TCP_MAXMSL);
1867 
1868 #ifdef INET6
1869 	while (tp->t_inpcb->inp_af == AF_INET6) {
1870 		so = tp->t_inpcb->inp_socket;
1871 #if defined(RTV_RPIPE)
1872 		rt = in6pcb_rtentry(tp->t_inpcb);
1873 #endif
1874 		if (__predict_true(tcp_msl_enable)) {
1875 			extern const struct in6_addr in6addr_loopback;
1876 
1877 			if (IN6_ARE_ADDR_EQUAL(&in6p_laddr(tp->t_inpcb),
1878 			    &in6addr_loopback)) {
1879 				tp->t_msl = tcp_msl_loop ? tcp_msl_loop : (TCPTV_MSL >> 2);
1880 				break;
1881 			}
1882 
1883 			if (__predict_false(tcp_rttlocal)) {
1884 				/* This may be adjusted by tcp_input */
1885 				tp->t_msl = tcp_msl_local ? tcp_msl_local : (TCPTV_MSL >> 1);
1886 				break;
1887 			}
1888 			if (in6_localaddr(&in6p_faddr(tp->t_inpcb))) {
1889 				tp->t_msl = tcp_msl_local ? tcp_msl_local : (TCPTV_MSL >> 1);
1890 				break;
1891 			}
1892 		}
1893 		tp->t_msl = tcp_msl_remote ? tcp_msl_remote : TCPTV_MSL;
1894 		break;
1895 	}
1896 
1897 	/* Clamp to a reasonable range.  */
1898 	tp->t_msl = MIN(tp->t_msl, TCP_MAXMSL);
1899 #endif
1900 
1901 	tp->t_state = TCPS_ESTABLISHED;
1902 	TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepidle);
1903 
1904 #ifdef RTV_RPIPE
1905 	if (rt != NULL && rt->rt_rmx.rmx_recvpipe != 0)
1906 		bufsize = rt->rt_rmx.rmx_recvpipe;
1907 	else
1908 #endif
1909 	{
1910 		KASSERT(so != NULL);
1911 		bufsize = so->so_rcv.sb_hiwat;
1912 	}
1913 	if (bufsize > tp->t_ourmss) {
1914 		bufsize = roundup(bufsize, tp->t_ourmss);
1915 		if (bufsize > sb_max)
1916 			bufsize = sb_max;
1917 		(void) sbreserve(&so->so_rcv, bufsize, so);
1918 	}
1919 #ifdef RTV_RPIPE
1920 	inpcb_rtentry_unref(rt, tp->t_inpcb);
1921 #endif
1922 }
1923 
1924 /*
1925  * Check if there's an initial rtt or rttvar.  Convert from the
1926  * route-table units to scaled multiples of the slow timeout timer.
1927  * Called only during the 3-way handshake.
1928  */
1929 void
tcp_rmx_rtt(struct tcpcb * tp)1930 tcp_rmx_rtt(struct tcpcb *tp)
1931 {
1932 #ifdef RTV_RTT
1933 	struct rtentry *rt = NULL;
1934 	int rtt;
1935 
1936 	KASSERT(tp->t_inpcb != NULL);
1937 
1938 	rt = inpcb_rtentry(tp->t_inpcb);
1939 	if (rt == NULL)
1940 		return;
1941 
1942 	if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
1943 		/*
1944 		 * XXX The lock bit for MTU indicates that the value
1945 		 * is also a minimum value; this is subject to time.
1946 		 */
1947 		if (rt->rt_rmx.rmx_locks & RTV_RTT)
1948 			TCPT_RANGESET(tp->t_rttmin,
1949 			    rtt / (RTM_RTTUNIT / PR_SLOWHZ),
1950 			    TCPTV_MIN, TCPTV_REXMTMAX);
1951 		tp->t_srtt = rtt /
1952 		    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
1953 		if (rt->rt_rmx.rmx_rttvar) {
1954 			tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
1955 			    ((RTM_RTTUNIT / PR_SLOWHZ) >>
1956 				(TCP_RTTVAR_SHIFT + 2));
1957 		} else {
1958 			/* Default variation is +- 1 rtt */
1959 			tp->t_rttvar =
1960 			    tp->t_srtt >> (TCP_RTT_SHIFT - TCP_RTTVAR_SHIFT);
1961 		}
1962 		TCPT_RANGESET(tp->t_rxtcur,
1963 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2),
1964 		    tp->t_rttmin, TCPTV_REXMTMAX);
1965 	}
1966 	inpcb_rtentry_unref(rt, tp->t_inpcb);
1967 #endif
1968 }
1969 
1970 tcp_seq	 tcp_iss_seq = 0;	/* tcp initial seq # */
1971 
1972 /*
1973  * Get a new sequence value given a tcp control block
1974  */
1975 tcp_seq
tcp_new_iss(struct tcpcb * tp)1976 tcp_new_iss(struct tcpcb *tp)
1977 {
1978 
1979 	if (tp->t_inpcb->inp_af == AF_INET) {
1980 		return tcp_new_iss1(&in4p_laddr(tp->t_inpcb),
1981 		    &in4p_faddr(tp->t_inpcb), tp->t_inpcb->inp_lport,
1982 		    tp->t_inpcb->inp_fport, sizeof(in4p_laddr(tp->t_inpcb)));
1983 	}
1984 #ifdef INET6
1985 	if (tp->t_inpcb->inp_af == AF_INET6) {
1986 		return tcp_new_iss1(&in6p_laddr(tp->t_inpcb),
1987 		    &in6p_faddr(tp->t_inpcb), tp->t_inpcb->inp_lport,
1988 		    tp->t_inpcb->inp_fport, sizeof(in6p_laddr(tp->t_inpcb)));
1989 	}
1990 #endif
1991 
1992 	panic("tcp_new_iss: unreachable");
1993 }
1994 
1995 static u_int8_t tcp_iss_secret[16];	/* 128 bits; should be plenty */
1996 
1997 /*
1998  * Initialize RFC 1948 ISS Secret
1999  */
2000 static int
tcp_iss_secret_init(void)2001 tcp_iss_secret_init(void)
2002 {
2003 	cprng_strong(kern_cprng,
2004 	    tcp_iss_secret, sizeof(tcp_iss_secret), 0);
2005 
2006 	return 0;
2007 }
2008 
2009 /*
2010  * This routine actually generates a new TCP initial sequence number.
2011  */
2012 tcp_seq
tcp_new_iss1(void * laddr,void * faddr,u_int16_t lport,u_int16_t fport,size_t addrsz)2013 tcp_new_iss1(void *laddr, void *faddr, u_int16_t lport, u_int16_t fport,
2014     size_t addrsz)
2015 {
2016 	tcp_seq tcp_iss;
2017 
2018 	if (tcp_do_rfc1948) {
2019 		MD5_CTX ctx;
2020 		u_int8_t hash[16];	/* XXX MD5 knowledge */
2021 		static ONCE_DECL(tcp_iss_secret_control);
2022 
2023 		/*
2024 		 * If we haven't been here before, initialize our cryptographic
2025 		 * hash secret.
2026 		 */
2027 		RUN_ONCE(&tcp_iss_secret_control, tcp_iss_secret_init);
2028 
2029 		/*
2030 		 * Compute the base value of the ISS.  It is a hash
2031 		 * of (saddr, sport, daddr, dport, secret).
2032 		 */
2033 		MD5Init(&ctx);
2034 
2035 		MD5Update(&ctx, (u_char *) laddr, addrsz);
2036 		MD5Update(&ctx, (u_char *) &lport, sizeof(lport));
2037 
2038 		MD5Update(&ctx, (u_char *) faddr, addrsz);
2039 		MD5Update(&ctx, (u_char *) &fport, sizeof(fport));
2040 
2041 		MD5Update(&ctx, tcp_iss_secret, sizeof(tcp_iss_secret));
2042 
2043 		MD5Final(hash, &ctx);
2044 
2045 		memcpy(&tcp_iss, hash, sizeof(tcp_iss));
2046 
2047 #ifdef TCPISS_DEBUG
2048 		printf("ISS hash 0x%08x, ", tcp_iss);
2049 #endif
2050 	} else {
2051 		/*
2052 		 * Randomize.
2053 		 */
2054 		tcp_iss = cprng_fast32() & TCP_ISS_RANDOM_MASK;
2055 #ifdef TCPISS_DEBUG
2056 		printf("ISS random 0x%08x, ", tcp_iss);
2057 #endif
2058 	}
2059 
2060 	/*
2061 	 * Add the offset in to the computed value.
2062 	 */
2063 	tcp_iss += tcp_iss_seq;
2064 #ifdef TCPISS_DEBUG
2065 	printf("ISS %08x\n", tcp_iss);
2066 #endif
2067 	return tcp_iss;
2068 }
2069 
2070 #if defined(IPSEC)
2071 /* compute ESP/AH header size for TCP, including outer IP header. */
2072 size_t
ipsec4_hdrsiz_tcp(struct tcpcb * tp)2073 ipsec4_hdrsiz_tcp(struct tcpcb *tp)
2074 {
2075 	struct inpcb *inp;
2076 	size_t hdrsiz;
2077 
2078 	/* XXX mapped addr case (tp->t_inpcb) */
2079 	if (!tp || !tp->t_template || !(inp = tp->t_inpcb))
2080 		return 0;
2081 	switch (tp->t_family) {
2082 	case AF_INET:
2083 		/* XXX: should use correct direction. */
2084 		hdrsiz = ipsec_hdrsiz(tp->t_template, IPSEC_DIR_OUTBOUND, inp);
2085 		break;
2086 	default:
2087 		hdrsiz = 0;
2088 		break;
2089 	}
2090 
2091 	return hdrsiz;
2092 }
2093 
2094 #ifdef INET6
2095 size_t
ipsec6_hdrsiz_tcp(struct tcpcb * tp)2096 ipsec6_hdrsiz_tcp(struct tcpcb *tp)
2097 {
2098 	struct inpcb *inp;
2099 	size_t hdrsiz;
2100 
2101 	if (!tp || !tp->t_template || !(inp = tp->t_inpcb))
2102 		return 0;
2103 	switch (tp->t_family) {
2104 	case AF_INET6:
2105 		/* XXX: should use correct direction. */
2106 		hdrsiz = ipsec_hdrsiz(tp->t_template, IPSEC_DIR_OUTBOUND, inp);
2107 		break;
2108 	case AF_INET:
2109 		/* mapped address case - tricky */
2110 	default:
2111 		hdrsiz = 0;
2112 		break;
2113 	}
2114 
2115 	return hdrsiz;
2116 }
2117 #endif
2118 #endif /*IPSEC*/
2119 
2120 /*
2121  * Determine the length of the TCP options for this connection.
2122  *
2123  * XXX:  What do we do for SACK, when we add that?  Just reserve
2124  *       all of the space?  Otherwise we can't exactly be incrementing
2125  *       cwnd by an amount that varies depending on the amount we last
2126  *       had to SACK!
2127  */
2128 
2129 u_int
tcp_optlen(struct tcpcb * tp)2130 tcp_optlen(struct tcpcb *tp)
2131 {
2132 	u_int optlen;
2133 
2134 	optlen = 0;
2135 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) ==
2136 	    (TF_REQ_TSTMP | TF_RCVD_TSTMP))
2137 		optlen += TCPOLEN_TSTAMP_APPA;
2138 
2139 #ifdef TCP_SIGNATURE
2140 	if (tp->t_flags & TF_SIGNATURE)
2141 		optlen += TCPOLEN_SIGLEN;
2142 #endif
2143 
2144 	return optlen;
2145 }
2146 
2147 u_int
tcp_hdrsz(struct tcpcb * tp)2148 tcp_hdrsz(struct tcpcb *tp)
2149 {
2150 	u_int hlen;
2151 
2152 	switch (tp->t_family) {
2153 #ifdef INET6
2154 	case AF_INET6:
2155 		hlen = sizeof(struct ip6_hdr);
2156 		break;
2157 #endif
2158 	case AF_INET:
2159 		hlen = sizeof(struct ip);
2160 		break;
2161 	default:
2162 		hlen = 0;
2163 		break;
2164 	}
2165 	hlen += sizeof(struct tcphdr);
2166 
2167 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
2168 	    (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)
2169 		hlen += TCPOLEN_TSTAMP_APPA;
2170 #ifdef TCP_SIGNATURE
2171 	if (tp->t_flags & TF_SIGNATURE)
2172 		hlen += TCPOLEN_SIGLEN;
2173 #endif
2174 	return hlen;
2175 }
2176 
2177 void
tcp_statinc(u_int stat)2178 tcp_statinc(u_int stat)
2179 {
2180 
2181 	KASSERT(stat < TCP_NSTATS);
2182 	TCP_STATINC(stat);
2183 }
2184 
2185 void
tcp_statadd(u_int stat,uint64_t val)2186 tcp_statadd(u_int stat, uint64_t val)
2187 {
2188 
2189 	KASSERT(stat < TCP_NSTATS);
2190 	TCP_STATADD(stat, val);
2191 }
2192