xref: /freebsd/sys/netinet/tcp_subr.c (revision 5d3e7166)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
5  *	The Regents of the University of California.  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 University 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 REGENTS 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 REGENTS 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  *	@(#)tcp_subr.c	8.2 (Berkeley) 5/24/95
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include "opt_inet.h"
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
40 #include "opt_kern_tls.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/arb.h>
45 #include <sys/callout.h>
46 #include <sys/eventhandler.h>
47 #ifdef TCP_HHOOK
48 #include <sys/hhook.h>
49 #endif
50 #include <sys/kernel.h>
51 #ifdef TCP_HHOOK
52 #include <sys/khelp.h>
53 #endif
54 #ifdef KERN_TLS
55 #include <sys/ktls.h>
56 #endif
57 #include <sys/qmath.h>
58 #include <sys/stats.h>
59 #include <sys/sysctl.h>
60 #include <sys/jail.h>
61 #include <sys/malloc.h>
62 #include <sys/refcount.h>
63 #include <sys/mbuf.h>
64 #include <sys/priv.h>
65 #include <sys/proc.h>
66 #include <sys/sdt.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <sys/protosw.h>
70 #include <sys/random.h>
71 
72 #include <vm/uma.h>
73 
74 #include <net/route.h>
75 #include <net/route/nhop.h>
76 #include <net/if.h>
77 #include <net/if_var.h>
78 #include <net/if_private.h>
79 #include <net/vnet.h>
80 
81 #include <netinet/in.h>
82 #include <netinet/in_fib.h>
83 #include <netinet/in_kdtrace.h>
84 #include <netinet/in_pcb.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/in_var.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_icmp.h>
89 #include <netinet/ip_var.h>
90 #ifdef INET6
91 #include <netinet/icmp6.h>
92 #include <netinet/ip6.h>
93 #include <netinet6/in6_fib.h>
94 #include <netinet6/in6_pcb.h>
95 #include <netinet6/ip6_var.h>
96 #include <netinet6/scope6_var.h>
97 #include <netinet6/nd6.h>
98 #endif
99 
100 #include <netinet/tcp.h>
101 #ifdef INVARIANTS
102 #define TCPSTATES
103 #endif
104 #include <netinet/tcp_fsm.h>
105 #include <netinet/tcp_seq.h>
106 #include <netinet/tcp_timer.h>
107 #include <netinet/tcp_var.h>
108 #include <netinet/tcp_ecn.h>
109 #include <netinet/tcp_log_buf.h>
110 #include <netinet/tcp_syncache.h>
111 #include <netinet/tcp_hpts.h>
112 #include <netinet/tcp_lro.h>
113 #include <netinet/cc/cc.h>
114 #include <netinet/tcpip.h>
115 #include <netinet/tcp_fastopen.h>
116 #include <netinet/tcp_accounting.h>
117 #ifdef TCPPCAP
118 #include <netinet/tcp_pcap.h>
119 #endif
120 #ifdef TCP_OFFLOAD
121 #include <netinet/tcp_offload.h>
122 #endif
123 #include <netinet/udp.h>
124 #include <netinet/udp_var.h>
125 #ifdef INET6
126 #include <netinet6/tcp6_var.h>
127 #endif
128 
129 #include <netipsec/ipsec_support.h>
130 
131 #include <machine/in_cksum.h>
132 #include <crypto/siphash/siphash.h>
133 
134 #include <security/mac/mac_framework.h>
135 
136 #ifdef INET6
137 static ip6proto_ctlinput_t tcp6_ctlinput;
138 static udp_tun_icmp_t tcp6_ctlinput_viaudp;
139 #endif
140 
141 VNET_DEFINE(int, tcp_mssdflt) = TCP_MSS;
142 #ifdef INET6
143 VNET_DEFINE(int, tcp_v6mssdflt) = TCP6_MSS;
144 #endif
145 
146 #ifdef TCP_SAD_DETECTION
147 /*  Sack attack detection thresholds and such */
148 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, sack_attack,
149     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
150     "Sack Attack detection thresholds");
151 int32_t tcp_force_detection = 0;
152 SYSCTL_INT(_net_inet_tcp_sack_attack, OID_AUTO, force_detection,
153     CTLFLAG_RW,
154     &tcp_force_detection, 0,
155     "Do we force detection even if the INP has it off?");
156 int32_t tcp_sad_limit = 10000;
157 SYSCTL_INT(_net_inet_tcp_sack_attack, OID_AUTO, limit,
158     CTLFLAG_RW,
159     &tcp_sad_limit, 10000,
160     "If SaD is enabled, what is the limit to sendmap entries (0 = unlimited)?");
161 int32_t tcp_sack_to_ack_thresh = 700;	/* 70 % */
162 SYSCTL_INT(_net_inet_tcp_sack_attack, OID_AUTO, sack_to_ack_thresh,
163     CTLFLAG_RW,
164     &tcp_sack_to_ack_thresh, 700,
165     "Percentage of sacks to acks we must see above (10.1 percent is 101)?");
166 int32_t tcp_sack_to_move_thresh = 600;	/* 60 % */
167 SYSCTL_INT(_net_inet_tcp_sack_attack, OID_AUTO, move_thresh,
168     CTLFLAG_RW,
169     &tcp_sack_to_move_thresh, 600,
170     "Percentage of sack moves we must see above (10.1 percent is 101)");
171 int32_t tcp_restoral_thresh = 450;	/* 45 % (sack:2:ack -25%) (mv:ratio -15%) **/
172 SYSCTL_INT(_net_inet_tcp_sack_attack, OID_AUTO, restore_thresh,
173     CTLFLAG_RW,
174     &tcp_restoral_thresh, 450,
175     "Percentage of sack to ack percentage we must see below to restore(10.1 percent is 101)");
176 int32_t tcp_sad_decay_val = 800;
177 SYSCTL_INT(_net_inet_tcp_sack_attack, OID_AUTO, decay_per,
178     CTLFLAG_RW,
179     &tcp_sad_decay_val, 800,
180     "The decay percentage (10.1 percent equals 101 )");
181 int32_t tcp_map_minimum = 500;
182 SYSCTL_INT(_net_inet_tcp_sack_attack, OID_AUTO, nummaps,
183     CTLFLAG_RW,
184     &tcp_map_minimum, 500,
185     "Number of Map enteries before we start detection");
186 int32_t tcp_sad_pacing_interval = 2000;
187 SYSCTL_INT(_net_inet_tcp_sack_attack, OID_AUTO, sad_pacing_int,
188     CTLFLAG_RW,
189     &tcp_sad_pacing_interval, 2000,
190     "What is the minimum pacing interval for a classified attacker?");
191 
192 int32_t tcp_sad_low_pps = 100;
193 SYSCTL_INT(_net_inet_tcp_sack_attack, OID_AUTO, sad_low_pps,
194     CTLFLAG_RW,
195     &tcp_sad_low_pps, 100,
196     "What is the input pps that below which we do not decay?");
197 #endif
198 uint32_t tcp_ack_war_time_window = 1000;
199 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, ack_war_timewindow,
200     CTLFLAG_RW,
201     &tcp_ack_war_time_window, 1000,
202    "If the tcp_stack does ack-war prevention how many milliseconds are in its time window?");
203 uint32_t tcp_ack_war_cnt = 5;
204 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, ack_war_cnt,
205     CTLFLAG_RW,
206     &tcp_ack_war_cnt, 5,
207    "If the tcp_stack does ack-war prevention how many acks can be sent in its time window?");
208 
209 struct rwlock tcp_function_lock;
210 
211 static int
212 sysctl_net_inet_tcp_mss_check(SYSCTL_HANDLER_ARGS)
213 {
214 	int error, new;
215 
216 	new = V_tcp_mssdflt;
217 	error = sysctl_handle_int(oidp, &new, 0, req);
218 	if (error == 0 && req->newptr) {
219 		if (new < TCP_MINMSS)
220 			error = EINVAL;
221 		else
222 			V_tcp_mssdflt = new;
223 	}
224 	return (error);
225 }
226 
227 SYSCTL_PROC(_net_inet_tcp, TCPCTL_MSSDFLT, mssdflt,
228     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
229     &VNET_NAME(tcp_mssdflt), 0, &sysctl_net_inet_tcp_mss_check, "I",
230     "Default TCP Maximum Segment Size");
231 
232 #ifdef INET6
233 static int
234 sysctl_net_inet_tcp_mss_v6_check(SYSCTL_HANDLER_ARGS)
235 {
236 	int error, new;
237 
238 	new = V_tcp_v6mssdflt;
239 	error = sysctl_handle_int(oidp, &new, 0, req);
240 	if (error == 0 && req->newptr) {
241 		if (new < TCP_MINMSS)
242 			error = EINVAL;
243 		else
244 			V_tcp_v6mssdflt = new;
245 	}
246 	return (error);
247 }
248 
249 SYSCTL_PROC(_net_inet_tcp, TCPCTL_V6MSSDFLT, v6mssdflt,
250     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
251     &VNET_NAME(tcp_v6mssdflt), 0, &sysctl_net_inet_tcp_mss_v6_check, "I",
252    "Default TCP Maximum Segment Size for IPv6");
253 #endif /* INET6 */
254 
255 /*
256  * Minimum MSS we accept and use. This prevents DoS attacks where
257  * we are forced to a ridiculous low MSS like 20 and send hundreds
258  * of packets instead of one. The effect scales with the available
259  * bandwidth and quickly saturates the CPU and network interface
260  * with packet generation and sending. Set to zero to disable MINMSS
261  * checking. This setting prevents us from sending too small packets.
262  */
263 VNET_DEFINE(int, tcp_minmss) = TCP_MINMSS;
264 SYSCTL_INT(_net_inet_tcp, OID_AUTO, minmss, CTLFLAG_VNET | CTLFLAG_RW,
265      &VNET_NAME(tcp_minmss), 0,
266     "Minimum TCP Maximum Segment Size");
267 
268 VNET_DEFINE(int, tcp_do_rfc1323) = 1;
269 SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1323, rfc1323, CTLFLAG_VNET | CTLFLAG_RW,
270     &VNET_NAME(tcp_do_rfc1323), 0,
271     "Enable rfc1323 (high performance TCP) extensions");
272 
273 /*
274  * As of June 2021, several TCP stacks violate RFC 7323 from September 2014.
275  * Some stacks negotiate TS, but never send them after connection setup. Some
276  * stacks negotiate TS, but don't send them when sending keep-alive segments.
277  * These include modern widely deployed TCP stacks.
278  * Therefore tolerating violations for now...
279  */
280 VNET_DEFINE(int, tcp_tolerate_missing_ts) = 1;
281 SYSCTL_INT(_net_inet_tcp, OID_AUTO, tolerate_missing_ts, CTLFLAG_VNET | CTLFLAG_RW,
282     &VNET_NAME(tcp_tolerate_missing_ts), 0,
283     "Tolerate missing TCP timestamps");
284 
285 VNET_DEFINE(int, tcp_ts_offset_per_conn) = 1;
286 SYSCTL_INT(_net_inet_tcp, OID_AUTO, ts_offset_per_conn, CTLFLAG_VNET | CTLFLAG_RW,
287     &VNET_NAME(tcp_ts_offset_per_conn), 0,
288     "Initialize TCP timestamps per connection instead of per host pair");
289 
290 /* How many connections are pacing */
291 static volatile uint32_t number_of_tcp_connections_pacing = 0;
292 static uint32_t shadow_num_connections = 0;
293 static counter_u64_t tcp_pacing_failures;
294 
295 static int tcp_pacing_limit = 10000;
296 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pacing_limit, CTLFLAG_RW,
297     &tcp_pacing_limit, 1000,
298     "If the TCP stack does pacing, is there a limit (-1 = no, 0 = no pacing N = number of connections)");
299 
300 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, pacing_count, CTLFLAG_RD,
301     &shadow_num_connections, 0, "Number of TCP connections being paced");
302 
303 SYSCTL_COUNTER_U64(_net_inet_tcp, OID_AUTO, pacing_failures, CTLFLAG_RD,
304     &tcp_pacing_failures, "Number of times we failed to enable pacing to avoid exceeding the limit");
305 
306 static int	tcp_log_debug = 0;
307 SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_debug, CTLFLAG_RW,
308     &tcp_log_debug, 0, "Log errors caused by incoming TCP segments");
309 
310 static int	tcp_tcbhashsize;
311 SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcbhashsize, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
312     &tcp_tcbhashsize, 0, "Size of TCP control-block hashtable");
313 
314 static int	do_tcpdrain = 1;
315 SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_tcpdrain, CTLFLAG_RW, &do_tcpdrain, 0,
316     "Enable tcp_drain routine for extra help when low on mbufs");
317 
318 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, pcbcount, CTLFLAG_VNET | CTLFLAG_RD,
319     &VNET_NAME(tcbinfo.ipi_count), 0, "Number of active PCBs");
320 
321 VNET_DEFINE_STATIC(int, icmp_may_rst) = 1;
322 #define	V_icmp_may_rst			VNET(icmp_may_rst)
323 SYSCTL_INT(_net_inet_tcp, OID_AUTO, icmp_may_rst, CTLFLAG_VNET | CTLFLAG_RW,
324     &VNET_NAME(icmp_may_rst), 0,
325     "Certain ICMP unreachable messages may abort connections in SYN_SENT");
326 
327 VNET_DEFINE_STATIC(int, tcp_isn_reseed_interval) = 0;
328 #define	V_tcp_isn_reseed_interval	VNET(tcp_isn_reseed_interval)
329 SYSCTL_INT(_net_inet_tcp, OID_AUTO, isn_reseed_interval, CTLFLAG_VNET | CTLFLAG_RW,
330     &VNET_NAME(tcp_isn_reseed_interval), 0,
331     "Seconds between reseeding of ISN secret");
332 
333 static int	tcp_soreceive_stream;
334 SYSCTL_INT(_net_inet_tcp, OID_AUTO, soreceive_stream, CTLFLAG_RDTUN,
335     &tcp_soreceive_stream, 0, "Using soreceive_stream for TCP sockets");
336 
337 VNET_DEFINE(uma_zone_t, sack_hole_zone);
338 #define	V_sack_hole_zone		VNET(sack_hole_zone)
339 VNET_DEFINE(uint32_t, tcp_map_entries_limit) = 0;	/* unlimited */
340 static int
341 sysctl_net_inet_tcp_map_limit_check(SYSCTL_HANDLER_ARGS)
342 {
343 	int error;
344 	uint32_t new;
345 
346 	new = V_tcp_map_entries_limit;
347 	error = sysctl_handle_int(oidp, &new, 0, req);
348 	if (error == 0 && req->newptr) {
349 		/* only allow "0" and value > minimum */
350 		if (new > 0 && new < TCP_MIN_MAP_ENTRIES_LIMIT)
351 			error = EINVAL;
352 		else
353 			V_tcp_map_entries_limit = new;
354 	}
355 	return (error);
356 }
357 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, map_limit,
358     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
359     &VNET_NAME(tcp_map_entries_limit), 0,
360     &sysctl_net_inet_tcp_map_limit_check, "IU",
361     "Total sendmap entries limit");
362 
363 VNET_DEFINE(uint32_t, tcp_map_split_limit) = 0;	/* unlimited */
364 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, split_limit, CTLFLAG_VNET | CTLFLAG_RW,
365      &VNET_NAME(tcp_map_split_limit), 0,
366     "Total sendmap split entries limit");
367 
368 #ifdef TCP_HHOOK
369 VNET_DEFINE(struct hhook_head *, tcp_hhh[HHOOK_TCP_LAST+1]);
370 #endif
371 
372 #define TS_OFFSET_SECRET_LENGTH SIPHASH_KEY_LENGTH
373 VNET_DEFINE_STATIC(u_char, ts_offset_secret[TS_OFFSET_SECRET_LENGTH]);
374 #define	V_ts_offset_secret	VNET(ts_offset_secret)
375 
376 static int	tcp_default_fb_init(struct tcpcb *tp, void **ptr);
377 static void	tcp_default_fb_fini(struct tcpcb *tp, int tcb_is_purged);
378 static int	tcp_default_handoff_ok(struct tcpcb *tp);
379 static struct inpcb *tcp_notify(struct inpcb *, int);
380 static struct inpcb *tcp_mtudisc_notify(struct inpcb *, int);
381 static struct inpcb *tcp_mtudisc(struct inpcb *, int);
382 static struct inpcb *tcp_drop_syn_sent(struct inpcb *, int);
383 static char *	tcp_log_addr(struct in_conninfo *inc, struct tcphdr *th,
384 		    const void *ip4hdr, const void *ip6hdr);
385 static void	tcp_default_switch_failed(struct tcpcb *tp);
386 static ipproto_ctlinput_t	tcp_ctlinput;
387 static udp_tun_icmp_t		tcp_ctlinput_viaudp;
388 
389 static struct tcp_function_block tcp_def_funcblk = {
390 	.tfb_tcp_block_name = "freebsd",
391 	.tfb_tcp_output = tcp_default_output,
392 	.tfb_tcp_do_segment = tcp_do_segment,
393 	.tfb_tcp_ctloutput = tcp_default_ctloutput,
394 	.tfb_tcp_handoff_ok = tcp_default_handoff_ok,
395 	.tfb_tcp_fb_init = tcp_default_fb_init,
396 	.tfb_tcp_fb_fini = tcp_default_fb_fini,
397 	.tfb_switch_failed = tcp_default_switch_failed,
398 };
399 
400 static int tcp_fb_cnt = 0;
401 struct tcp_funchead t_functions;
402 VNET_DEFINE_STATIC(struct tcp_function_block *, tcp_func_set_ptr) = &tcp_def_funcblk;
403 #define	V_tcp_func_set_ptr VNET(tcp_func_set_ptr)
404 
405 void
406 tcp_record_dsack(struct tcpcb *tp, tcp_seq start, tcp_seq end, int tlp)
407 {
408 	TCPSTAT_INC(tcps_dsack_count);
409 	tp->t_dsack_pack++;
410 	if (tlp == 0) {
411 		if (SEQ_GT(end, start)) {
412 			tp->t_dsack_bytes += (end - start);
413 			TCPSTAT_ADD(tcps_dsack_bytes, (end - start));
414 		} else {
415 			tp->t_dsack_tlp_bytes += (start - end);
416 			TCPSTAT_ADD(tcps_dsack_bytes, (start - end));
417 		}
418 	} else {
419 		if (SEQ_GT(end, start)) {
420 			tp->t_dsack_bytes += (end - start);
421 			TCPSTAT_ADD(tcps_dsack_tlp_bytes, (end - start));
422 		} else {
423 			tp->t_dsack_tlp_bytes += (start - end);
424 			TCPSTAT_ADD(tcps_dsack_tlp_bytes, (start - end));
425 		}
426 	}
427 }
428 
429 static struct tcp_function_block *
430 find_tcp_functions_locked(struct tcp_function_set *fs)
431 {
432 	struct tcp_function *f;
433 	struct tcp_function_block *blk=NULL;
434 
435 	TAILQ_FOREACH(f, &t_functions, tf_next) {
436 		if (strcmp(f->tf_name, fs->function_set_name) == 0) {
437 			blk = f->tf_fb;
438 			break;
439 		}
440 	}
441 	return(blk);
442 }
443 
444 static struct tcp_function_block *
445 find_tcp_fb_locked(struct tcp_function_block *blk, struct tcp_function **s)
446 {
447 	struct tcp_function_block *rblk=NULL;
448 	struct tcp_function *f;
449 
450 	TAILQ_FOREACH(f, &t_functions, tf_next) {
451 		if (f->tf_fb == blk) {
452 			rblk = blk;
453 			if (s) {
454 				*s = f;
455 			}
456 			break;
457 		}
458 	}
459 	return (rblk);
460 }
461 
462 struct tcp_function_block *
463 find_and_ref_tcp_functions(struct tcp_function_set *fs)
464 {
465 	struct tcp_function_block *blk;
466 
467 	rw_rlock(&tcp_function_lock);
468 	blk = find_tcp_functions_locked(fs);
469 	if (blk)
470 		refcount_acquire(&blk->tfb_refcnt);
471 	rw_runlock(&tcp_function_lock);
472 	return(blk);
473 }
474 
475 struct tcp_function_block *
476 find_and_ref_tcp_fb(struct tcp_function_block *blk)
477 {
478 	struct tcp_function_block *rblk;
479 
480 	rw_rlock(&tcp_function_lock);
481 	rblk = find_tcp_fb_locked(blk, NULL);
482 	if (rblk)
483 		refcount_acquire(&rblk->tfb_refcnt);
484 	rw_runlock(&tcp_function_lock);
485 	return(rblk);
486 }
487 
488 /* Find a matching alias for the given tcp_function_block. */
489 int
490 find_tcp_function_alias(struct tcp_function_block *blk,
491     struct tcp_function_set *fs)
492 {
493 	struct tcp_function *f;
494 	int found;
495 
496 	found = 0;
497 	rw_rlock(&tcp_function_lock);
498 	TAILQ_FOREACH(f, &t_functions, tf_next) {
499 		if ((f->tf_fb == blk) &&
500 		    (strncmp(f->tf_name, blk->tfb_tcp_block_name,
501 		        TCP_FUNCTION_NAME_LEN_MAX) != 0)) {
502 			/* Matching function block with different name. */
503 			strncpy(fs->function_set_name, f->tf_name,
504 			    TCP_FUNCTION_NAME_LEN_MAX);
505 			found = 1;
506 			break;
507 		}
508 	}
509 	/* Null terminate the string appropriately. */
510 	if (found) {
511 		fs->function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
512 	} else {
513 		fs->function_set_name[0] = '\0';
514 	}
515 	rw_runlock(&tcp_function_lock);
516 	return (found);
517 }
518 
519 static struct tcp_function_block *
520 find_and_ref_tcp_default_fb(void)
521 {
522 	struct tcp_function_block *rblk;
523 
524 	rw_rlock(&tcp_function_lock);
525 	rblk = V_tcp_func_set_ptr;
526 	refcount_acquire(&rblk->tfb_refcnt);
527 	rw_runlock(&tcp_function_lock);
528 	return (rblk);
529 }
530 
531 void
532 tcp_switch_back_to_default(struct tcpcb *tp)
533 {
534 	struct tcp_function_block *tfb;
535 	void *ptr = NULL;
536 
537 	KASSERT(tp->t_fb != &tcp_def_funcblk,
538 	    ("%s: called by the built-in default stack", __func__));
539 
540 	/*
541 	 * Now, we'll find a new function block to use.
542 	 * Start by trying the current user-selected
543 	 * default, unless this stack is the user-selected
544 	 * default.
545 	 */
546 	tfb = find_and_ref_tcp_default_fb();
547 	if (tfb == tp->t_fb) {
548 		refcount_release(&tfb->tfb_refcnt);
549 		tfb = NULL;
550 	}
551 	/* Does the stack accept this connection? */
552 	if (tfb != NULL && tfb->tfb_tcp_handoff_ok != NULL &&
553 	    (*tfb->tfb_tcp_handoff_ok)(tp)) {
554 		refcount_release(&tfb->tfb_refcnt);
555 		tfb = NULL;
556 	}
557 	/* Try to use that stack. */
558 	if (tfb != NULL) {
559 		/* Initialize the new stack. If it succeeds, we are done. */
560 		if (tfb->tfb_tcp_fb_init == NULL ||
561 		    (*tfb->tfb_tcp_fb_init)(tp, &ptr) == 0) {
562 			/* Release the old stack */
563 			if (tp->t_fb->tfb_tcp_fb_fini != NULL)
564 				(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
565 			refcount_release(&tp->t_fb->tfb_refcnt);
566 			/* Now set in all the pointers */
567 			tp->t_fb = tfb;
568 			tp->t_fb_ptr = ptr;
569 			return;
570 		}
571 		/*
572 		 * Initialization failed. Release the reference count on
573 		 * the looked up default stack.
574 		 */
575 		refcount_release(&tfb->tfb_refcnt);
576 	}
577 
578 	/*
579 	 * If that wasn't feasible, use the built-in default
580 	 * stack which is not allowed to reject anyone.
581 	 */
582 	tfb = find_and_ref_tcp_fb(&tcp_def_funcblk);
583 	if (tfb == NULL) {
584 		/* there always should be a default */
585 		panic("Can't refer to tcp_def_funcblk");
586 	}
587 	if (tfb->tfb_tcp_handoff_ok != NULL) {
588 		if ((*tfb->tfb_tcp_handoff_ok) (tp)) {
589 			/* The default stack cannot say no */
590 			panic("Default stack rejects a new session?");
591 		}
592 	}
593 	if (tfb->tfb_tcp_fb_init != NULL &&
594 	    (*tfb->tfb_tcp_fb_init)(tp, &ptr)) {
595 		/* The default stack cannot fail */
596 		panic("Default stack initialization failed");
597 	}
598 	/* Now release the old stack */
599 	if (tp->t_fb->tfb_tcp_fb_fini != NULL)
600 		(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
601 	refcount_release(&tp->t_fb->tfb_refcnt);
602 	/* And set in the pointers to the new */
603 	tp->t_fb = tfb;
604 	tp->t_fb_ptr = ptr;
605 }
606 
607 static bool
608 tcp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *inp,
609     const struct sockaddr *sa, void *ctx)
610 {
611 	struct ip *iph;
612 #ifdef INET6
613 	struct ip6_hdr *ip6;
614 #endif
615 	struct udphdr *uh;
616 	struct tcphdr *th;
617 	int thlen;
618 	uint16_t port;
619 
620 	TCPSTAT_INC(tcps_tunneled_pkts);
621 	if ((m->m_flags & M_PKTHDR) == 0) {
622 		/* Can't handle one that is not a pkt hdr */
623 		TCPSTAT_INC(tcps_tunneled_errs);
624 		goto out;
625 	}
626 	thlen = sizeof(struct tcphdr);
627 	if (m->m_len < off + sizeof(struct udphdr) + thlen &&
628 	    (m =  m_pullup(m, off + sizeof(struct udphdr) + thlen)) == NULL) {
629 		TCPSTAT_INC(tcps_tunneled_errs);
630 		goto out;
631 	}
632 	iph = mtod(m, struct ip *);
633 	uh = (struct udphdr *)((caddr_t)iph + off);
634 	th = (struct tcphdr *)(uh + 1);
635 	thlen = th->th_off << 2;
636 	if (m->m_len < off + sizeof(struct udphdr) + thlen) {
637 		m =  m_pullup(m, off + sizeof(struct udphdr) + thlen);
638 		if (m == NULL) {
639 			TCPSTAT_INC(tcps_tunneled_errs);
640 			goto out;
641 		} else {
642 			iph = mtod(m, struct ip *);
643 			uh = (struct udphdr *)((caddr_t)iph + off);
644 			th = (struct tcphdr *)(uh + 1);
645 		}
646 	}
647 	m->m_pkthdr.tcp_tun_port = port = uh->uh_sport;
648 	bcopy(th, uh, m->m_len - off);
649 	m->m_len -= sizeof(struct udphdr);
650 	m->m_pkthdr.len -= sizeof(struct udphdr);
651 	/*
652 	 * We use the same algorithm for
653 	 * both UDP and TCP for c-sum. So
654 	 * the code in tcp_input will skip
655 	 * the checksum. So we do nothing
656 	 * with the flag (m->m_pkthdr.csum_flags).
657 	 */
658 	switch (iph->ip_v) {
659 #ifdef INET
660 	case IPVERSION:
661 		iph->ip_len = htons(ntohs(iph->ip_len) - sizeof(struct udphdr));
662 		tcp_input_with_port(&m, &off, IPPROTO_TCP, port);
663 		break;
664 #endif
665 #ifdef INET6
666 	case IPV6_VERSION >> 4:
667 		ip6 = mtod(m, struct ip6_hdr *);
668 		ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - sizeof(struct udphdr));
669 		tcp6_input_with_port(&m, &off, IPPROTO_TCP, port);
670 		break;
671 #endif
672 	default:
673 		goto out;
674 		break;
675 	}
676 	return (true);
677 out:
678 	m_freem(m);
679 
680 	return (true);
681 }
682 
683 static int
684 sysctl_net_inet_default_tcp_functions(SYSCTL_HANDLER_ARGS)
685 {
686 	int error=ENOENT;
687 	struct tcp_function_set fs;
688 	struct tcp_function_block *blk;
689 
690 	memset(&fs, 0, sizeof(fs));
691 	rw_rlock(&tcp_function_lock);
692 	blk = find_tcp_fb_locked(V_tcp_func_set_ptr, NULL);
693 	if (blk) {
694 		/* Found him */
695 		strcpy(fs.function_set_name, blk->tfb_tcp_block_name);
696 		fs.pcbcnt = blk->tfb_refcnt;
697 	}
698 	rw_runlock(&tcp_function_lock);
699 	error = sysctl_handle_string(oidp, fs.function_set_name,
700 				     sizeof(fs.function_set_name), req);
701 
702 	/* Check for error or no change */
703 	if (error != 0 || req->newptr == NULL)
704 		return(error);
705 
706 	rw_wlock(&tcp_function_lock);
707 	blk = find_tcp_functions_locked(&fs);
708 	if ((blk == NULL) ||
709 	    (blk->tfb_flags & TCP_FUNC_BEING_REMOVED)) {
710 		error = ENOENT;
711 		goto done;
712 	}
713 	V_tcp_func_set_ptr = blk;
714 done:
715 	rw_wunlock(&tcp_function_lock);
716 	return (error);
717 }
718 
719 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, functions_default,
720     CTLFLAG_VNET | CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
721     NULL, 0, sysctl_net_inet_default_tcp_functions, "A",
722     "Set/get the default TCP functions");
723 
724 static int
725 sysctl_net_inet_list_available(SYSCTL_HANDLER_ARGS)
726 {
727 	int error, cnt, linesz;
728 	struct tcp_function *f;
729 	char *buffer, *cp;
730 	size_t bufsz, outsz;
731 	bool alias;
732 
733 	cnt = 0;
734 	rw_rlock(&tcp_function_lock);
735 	TAILQ_FOREACH(f, &t_functions, tf_next) {
736 		cnt++;
737 	}
738 	rw_runlock(&tcp_function_lock);
739 
740 	bufsz = (cnt+2) * ((TCP_FUNCTION_NAME_LEN_MAX * 2) + 13) + 1;
741 	buffer = malloc(bufsz, M_TEMP, M_WAITOK);
742 
743 	error = 0;
744 	cp = buffer;
745 
746 	linesz = snprintf(cp, bufsz, "\n%-32s%c %-32s %s\n", "Stack", 'D',
747 	    "Alias", "PCB count");
748 	cp += linesz;
749 	bufsz -= linesz;
750 	outsz = linesz;
751 
752 	rw_rlock(&tcp_function_lock);
753 	TAILQ_FOREACH(f, &t_functions, tf_next) {
754 		alias = (f->tf_name != f->tf_fb->tfb_tcp_block_name);
755 		linesz = snprintf(cp, bufsz, "%-32s%c %-32s %u\n",
756 		    f->tf_fb->tfb_tcp_block_name,
757 		    (f->tf_fb == V_tcp_func_set_ptr) ? '*' : ' ',
758 		    alias ? f->tf_name : "-",
759 		    f->tf_fb->tfb_refcnt);
760 		if (linesz >= bufsz) {
761 			error = EOVERFLOW;
762 			break;
763 		}
764 		cp += linesz;
765 		bufsz -= linesz;
766 		outsz += linesz;
767 	}
768 	rw_runlock(&tcp_function_lock);
769 	if (error == 0)
770 		error = sysctl_handle_string(oidp, buffer, outsz + 1, req);
771 	free(buffer, M_TEMP);
772 	return (error);
773 }
774 
775 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, functions_available,
776     CTLFLAG_VNET | CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
777     NULL, 0, sysctl_net_inet_list_available, "A",
778     "list available TCP Function sets");
779 
780 VNET_DEFINE(int, tcp_udp_tunneling_port) = TCP_TUNNELING_PORT_DEFAULT;
781 
782 #ifdef INET
783 VNET_DEFINE(struct socket *, udp4_tun_socket) = NULL;
784 #define	V_udp4_tun_socket	VNET(udp4_tun_socket)
785 #endif
786 #ifdef INET6
787 VNET_DEFINE(struct socket *, udp6_tun_socket) = NULL;
788 #define	V_udp6_tun_socket	VNET(udp6_tun_socket)
789 #endif
790 
791 static struct sx tcpoudp_lock;
792 
793 static void
794 tcp_over_udp_stop(void)
795 {
796 
797 	sx_assert(&tcpoudp_lock, SA_XLOCKED);
798 
799 #ifdef INET
800 	if (V_udp4_tun_socket != NULL) {
801 		soclose(V_udp4_tun_socket);
802 		V_udp4_tun_socket = NULL;
803 	}
804 #endif
805 #ifdef INET6
806 	if (V_udp6_tun_socket != NULL) {
807 		soclose(V_udp6_tun_socket);
808 		V_udp6_tun_socket = NULL;
809 	}
810 #endif
811 }
812 
813 static int
814 tcp_over_udp_start(void)
815 {
816 	uint16_t port;
817 	int ret;
818 #ifdef INET
819 	struct sockaddr_in sin;
820 #endif
821 #ifdef INET6
822 	struct sockaddr_in6 sin6;
823 #endif
824 
825 	sx_assert(&tcpoudp_lock, SA_XLOCKED);
826 
827 	port = V_tcp_udp_tunneling_port;
828 	if (ntohs(port) == 0) {
829 		/* Must have a port set */
830 		return (EINVAL);
831 	}
832 #ifdef INET
833 	if (V_udp4_tun_socket != NULL) {
834 		/* Already running -- must stop first */
835 		return (EALREADY);
836 	}
837 #endif
838 #ifdef INET6
839 	if (V_udp6_tun_socket != NULL) {
840 		/* Already running -- must stop first */
841 		return (EALREADY);
842 	}
843 #endif
844 #ifdef INET
845 	if ((ret = socreate(PF_INET, &V_udp4_tun_socket,
846 	    SOCK_DGRAM, IPPROTO_UDP,
847 	    curthread->td_ucred, curthread))) {
848 		tcp_over_udp_stop();
849 		return (ret);
850 	}
851 	/* Call the special UDP hook. */
852 	if ((ret = udp_set_kernel_tunneling(V_udp4_tun_socket,
853 	    tcp_recv_udp_tunneled_packet,
854 	    tcp_ctlinput_viaudp,
855 	    NULL))) {
856 		tcp_over_udp_stop();
857 		return (ret);
858 	}
859 	/* Ok, we have a socket, bind it to the port. */
860 	memset(&sin, 0, sizeof(struct sockaddr_in));
861 	sin.sin_len = sizeof(struct sockaddr_in);
862 	sin.sin_family = AF_INET;
863 	sin.sin_port = htons(port);
864 	if ((ret = sobind(V_udp4_tun_socket,
865 	    (struct sockaddr *)&sin, curthread))) {
866 		tcp_over_udp_stop();
867 		return (ret);
868 	}
869 #endif
870 #ifdef INET6
871 	if ((ret = socreate(PF_INET6, &V_udp6_tun_socket,
872 	    SOCK_DGRAM, IPPROTO_UDP,
873 	    curthread->td_ucred, curthread))) {
874 		tcp_over_udp_stop();
875 		return (ret);
876 	}
877 	/* Call the special UDP hook. */
878 	if ((ret = udp_set_kernel_tunneling(V_udp6_tun_socket,
879 	    tcp_recv_udp_tunneled_packet,
880 	    tcp6_ctlinput_viaudp,
881 	    NULL))) {
882 		tcp_over_udp_stop();
883 		return (ret);
884 	}
885 	/* Ok, we have a socket, bind it to the port. */
886 	memset(&sin6, 0, sizeof(struct sockaddr_in6));
887 	sin6.sin6_len = sizeof(struct sockaddr_in6);
888 	sin6.sin6_family = AF_INET6;
889 	sin6.sin6_port = htons(port);
890 	if ((ret = sobind(V_udp6_tun_socket,
891 	    (struct sockaddr *)&sin6, curthread))) {
892 		tcp_over_udp_stop();
893 		return (ret);
894 	}
895 #endif
896 	return (0);
897 }
898 
899 static int
900 sysctl_net_inet_tcp_udp_tunneling_port_check(SYSCTL_HANDLER_ARGS)
901 {
902 	int error;
903 	uint32_t old, new;
904 
905 	old = V_tcp_udp_tunneling_port;
906 	new = old;
907 	error = sysctl_handle_int(oidp, &new, 0, req);
908 	if ((error == 0) &&
909 	    (req->newptr != NULL)) {
910 		if ((new < TCP_TUNNELING_PORT_MIN) ||
911 		    (new > TCP_TUNNELING_PORT_MAX)) {
912 			error = EINVAL;
913 		} else {
914 			sx_xlock(&tcpoudp_lock);
915 			V_tcp_udp_tunneling_port = new;
916 			if (old != 0) {
917 				tcp_over_udp_stop();
918 			}
919 			if (new != 0) {
920 				error = tcp_over_udp_start();
921 				if (error != 0) {
922 					V_tcp_udp_tunneling_port = 0;
923 				}
924 			}
925 			sx_xunlock(&tcpoudp_lock);
926 		}
927 	}
928 	return (error);
929 }
930 
931 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, udp_tunneling_port,
932     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
933     &VNET_NAME(tcp_udp_tunneling_port),
934     0, &sysctl_net_inet_tcp_udp_tunneling_port_check, "IU",
935     "Tunneling port for tcp over udp");
936 
937 VNET_DEFINE(int, tcp_udp_tunneling_overhead) = TCP_TUNNELING_OVERHEAD_DEFAULT;
938 
939 static int
940 sysctl_net_inet_tcp_udp_tunneling_overhead_check(SYSCTL_HANDLER_ARGS)
941 {
942 	int error, new;
943 
944 	new = V_tcp_udp_tunneling_overhead;
945 	error = sysctl_handle_int(oidp, &new, 0, req);
946 	if (error == 0 && req->newptr) {
947 		if ((new < TCP_TUNNELING_OVERHEAD_MIN) ||
948 		    (new > TCP_TUNNELING_OVERHEAD_MAX))
949 			error = EINVAL;
950 		else
951 			V_tcp_udp_tunneling_overhead = new;
952 	}
953 	return (error);
954 }
955 
956 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, udp_tunneling_overhead,
957     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
958     &VNET_NAME(tcp_udp_tunneling_overhead),
959     0, &sysctl_net_inet_tcp_udp_tunneling_overhead_check, "IU",
960     "MSS reduction when using tcp over udp");
961 
962 /*
963  * Exports one (struct tcp_function_info) for each alias/name.
964  */
965 static int
966 sysctl_net_inet_list_func_info(SYSCTL_HANDLER_ARGS)
967 {
968 	int cnt, error;
969 	struct tcp_function *f;
970 	struct tcp_function_info tfi;
971 
972 	/*
973 	 * We don't allow writes.
974 	 */
975 	if (req->newptr != NULL)
976 		return (EINVAL);
977 
978 	/*
979 	 * Wire the old buffer so we can directly copy the functions to
980 	 * user space without dropping the lock.
981 	 */
982 	if (req->oldptr != NULL) {
983 		error = sysctl_wire_old_buffer(req, 0);
984 		if (error)
985 			return (error);
986 	}
987 
988 	/*
989 	 * Walk the list and copy out matching entries. If INVARIANTS
990 	 * is compiled in, also walk the list to verify the length of
991 	 * the list matches what we have recorded.
992 	 */
993 	rw_rlock(&tcp_function_lock);
994 
995 	cnt = 0;
996 #ifndef INVARIANTS
997 	if (req->oldptr == NULL) {
998 		cnt = tcp_fb_cnt;
999 		goto skip_loop;
1000 	}
1001 #endif
1002 	TAILQ_FOREACH(f, &t_functions, tf_next) {
1003 #ifdef INVARIANTS
1004 		cnt++;
1005 #endif
1006 		if (req->oldptr != NULL) {
1007 			bzero(&tfi, sizeof(tfi));
1008 			tfi.tfi_refcnt = f->tf_fb->tfb_refcnt;
1009 			tfi.tfi_id = f->tf_fb->tfb_id;
1010 			(void)strlcpy(tfi.tfi_alias, f->tf_name,
1011 			    sizeof(tfi.tfi_alias));
1012 			(void)strlcpy(tfi.tfi_name,
1013 			    f->tf_fb->tfb_tcp_block_name, sizeof(tfi.tfi_name));
1014 			error = SYSCTL_OUT(req, &tfi, sizeof(tfi));
1015 			/*
1016 			 * Don't stop on error, as that is the
1017 			 * mechanism we use to accumulate length
1018 			 * information if the buffer was too short.
1019 			 */
1020 		}
1021 	}
1022 	KASSERT(cnt == tcp_fb_cnt,
1023 	    ("%s: cnt (%d) != tcp_fb_cnt (%d)", __func__, cnt, tcp_fb_cnt));
1024 #ifndef INVARIANTS
1025 skip_loop:
1026 #endif
1027 	rw_runlock(&tcp_function_lock);
1028 	if (req->oldptr == NULL)
1029 		error = SYSCTL_OUT(req, NULL,
1030 		    (cnt + 1) * sizeof(struct tcp_function_info));
1031 
1032 	return (error);
1033 }
1034 
1035 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, function_info,
1036 	    CTLTYPE_OPAQUE | CTLFLAG_SKIP | CTLFLAG_RD | CTLFLAG_MPSAFE,
1037 	    NULL, 0, sysctl_net_inet_list_func_info, "S,tcp_function_info",
1038 	    "List TCP function block name-to-ID mappings");
1039 
1040 /*
1041  * tfb_tcp_handoff_ok() function for the default stack.
1042  * Note that we'll basically try to take all comers.
1043  */
1044 static int
1045 tcp_default_handoff_ok(struct tcpcb *tp)
1046 {
1047 
1048 	return (0);
1049 }
1050 
1051 /*
1052  * tfb_tcp_fb_init() function for the default stack.
1053  *
1054  * This handles making sure we have appropriate timers set if you are
1055  * transitioning a socket that has some amount of setup done.
1056  *
1057  * The init() fuction from the default can *never* return non-zero i.e.
1058  * it is required to always succeed since it is the stack of last resort!
1059  */
1060 static int
1061 tcp_default_fb_init(struct tcpcb *tp, void **ptr)
1062 {
1063 	struct socket *so = tptosocket(tp);
1064 	int rexmt;
1065 
1066 	INP_WLOCK_ASSERT(tptoinpcb(tp));
1067 	/* We don't use the pointer */
1068 	*ptr = NULL;
1069 
1070 	KASSERT(tp->t_state >= 0 && tp->t_state < TCPS_TIME_WAIT,
1071 	    ("%s: connection %p in unexpected state %d", __func__, tp,
1072 	    tp->t_state));
1073 
1074 	/* Make sure we get no interesting mbuf queuing behavior */
1075 	/* All mbuf queue/ack compress flags should be off */
1076 	tcp_lro_features_off(tptoinpcb(tp));
1077 
1078 	/* Cancel the GP measurement in progress */
1079 	tp->t_flags &= ~TF_GPUTINPROG;
1080 	/* Validate the timers are not in usec, if they are convert */
1081 	tcp_change_time_units(tp, TCP_TMR_GRANULARITY_TICKS);
1082 	if ((tp->t_state == TCPS_SYN_SENT) ||
1083 	    (tp->t_state == TCPS_SYN_RECEIVED))
1084 		rexmt = tcp_rexmit_initial * tcp_backoff[tp->t_rxtshift];
1085 	else
1086 		rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];
1087 	if (tp->t_rxtshift == 0)
1088 		tp->t_rxtcur = rexmt;
1089 	else
1090 		TCPT_RANGESET(tp->t_rxtcur, rexmt, tp->t_rttmin, TCPTV_REXMTMAX);
1091 
1092 	/*
1093 	 * Nothing to do for ESTABLISHED or LISTEN states. And, we don't
1094 	 * know what to do for unexpected states (which includes TIME_WAIT).
1095 	 */
1096 	if (tp->t_state <= TCPS_LISTEN || tp->t_state >= TCPS_TIME_WAIT)
1097 		return (0);
1098 
1099 	/*
1100 	 * Make sure some kind of transmission timer is set if there is
1101 	 * outstanding data.
1102 	 */
1103 	if ((!TCPS_HAVEESTABLISHED(tp->t_state) || sbavail(&so->so_snd) ||
1104 	    tp->snd_una != tp->snd_max) && !(tcp_timer_active(tp, TT_REXMT) ||
1105 	    tcp_timer_active(tp, TT_PERSIST))) {
1106 		/*
1107 		 * If the session has established and it looks like it should
1108 		 * be in the persist state, set the persist timer. Otherwise,
1109 		 * set the retransmit timer.
1110 		 */
1111 		if (TCPS_HAVEESTABLISHED(tp->t_state) && tp->snd_wnd == 0 &&
1112 		    (int32_t)(tp->snd_nxt - tp->snd_una) <
1113 		    (int32_t)sbavail(&so->so_snd))
1114 			tcp_setpersist(tp);
1115 		else
1116 			tcp_timer_activate(tp, TT_REXMT, TP_RXTCUR(tp));
1117 	}
1118 
1119 	/* All non-embryonic sessions get a keepalive timer. */
1120 	if (!tcp_timer_active(tp, TT_KEEP))
1121 		tcp_timer_activate(tp, TT_KEEP,
1122 		    TCPS_HAVEESTABLISHED(tp->t_state) ? TP_KEEPIDLE(tp) :
1123 		    TP_KEEPINIT(tp));
1124 
1125 	/*
1126 	 * Make sure critical variables are initialized
1127 	 * if transitioning while in Recovery.
1128 	 */
1129 	if IN_FASTRECOVERY(tp->t_flags) {
1130 		if (tp->sackhint.recover_fs == 0)
1131 			tp->sackhint.recover_fs = max(1,
1132 			    tp->snd_nxt - tp->snd_una);
1133 	}
1134 
1135 	return (0);
1136 }
1137 
1138 /*
1139  * tfb_tcp_fb_fini() function for the default stack.
1140  *
1141  * This changes state as necessary (or prudent) to prepare for another stack
1142  * to assume responsibility for the connection.
1143  */
1144 static void
1145 tcp_default_fb_fini(struct tcpcb *tp, int tcb_is_purged)
1146 {
1147 
1148 	INP_WLOCK_ASSERT(tptoinpcb(tp));
1149 
1150 #ifdef TCP_BLACKBOX
1151 	tcp_log_flowend(tp);
1152 #endif
1153 	tp->t_acktime = 0;
1154 	return;
1155 }
1156 
1157 /*
1158  * Target size of TCP PCB hash tables. Must be a power of two.
1159  *
1160  * Note that this can be overridden by the kernel environment
1161  * variable net.inet.tcp.tcbhashsize
1162  */
1163 #ifndef TCBHASHSIZE
1164 #define TCBHASHSIZE	0
1165 #endif
1166 
1167 MALLOC_DEFINE(M_TCPLOG, "tcplog", "TCP address and flags print buffers");
1168 MALLOC_DEFINE(M_TCPFUNCTIONS, "tcpfunc", "TCP function set memory");
1169 
1170 static struct mtx isn_mtx;
1171 
1172 #define	ISN_LOCK_INIT()	mtx_init(&isn_mtx, "isn_mtx", NULL, MTX_DEF)
1173 #define	ISN_LOCK()	mtx_lock(&isn_mtx)
1174 #define	ISN_UNLOCK()	mtx_unlock(&isn_mtx)
1175 
1176 INPCBSTORAGE_DEFINE(tcpcbstor, tcpcb, "tcpinp", "tcp_inpcb", "tcp", "tcphash");
1177 
1178 /*
1179  * Take a value and get the next power of 2 that doesn't overflow.
1180  * Used to size the tcp_inpcb hash buckets.
1181  */
1182 static int
1183 maketcp_hashsize(int size)
1184 {
1185 	int hashsize;
1186 
1187 	/*
1188 	 * auto tune.
1189 	 * get the next power of 2 higher than maxsockets.
1190 	 */
1191 	hashsize = 1 << fls(size);
1192 	/* catch overflow, and just go one power of 2 smaller */
1193 	if (hashsize < size) {
1194 		hashsize = 1 << (fls(size) - 1);
1195 	}
1196 	return (hashsize);
1197 }
1198 
1199 static volatile int next_tcp_stack_id = 1;
1200 
1201 /*
1202  * Register a TCP function block with the name provided in the names
1203  * array.  (Note that this function does NOT automatically register
1204  * blk->tfb_tcp_block_name as a stack name.  Therefore, you should
1205  * explicitly include blk->tfb_tcp_block_name in the list of names if
1206  * you wish to register the stack with that name.)
1207  *
1208  * Either all name registrations will succeed or all will fail.  If
1209  * a name registration fails, the function will update the num_names
1210  * argument to point to the array index of the name that encountered
1211  * the failure.
1212  *
1213  * Returns 0 on success, or an error code on failure.
1214  */
1215 int
1216 register_tcp_functions_as_names(struct tcp_function_block *blk, int wait,
1217     const char *names[], int *num_names)
1218 {
1219 	struct tcp_function *n;
1220 	struct tcp_function_set fs;
1221 	int error, i;
1222 
1223 	KASSERT(names != NULL && *num_names > 0,
1224 	    ("%s: Called with 0-length name list", __func__));
1225 	KASSERT(names != NULL, ("%s: Called with NULL name list", __func__));
1226 	KASSERT(rw_initialized(&tcp_function_lock),
1227 	    ("%s: called too early", __func__));
1228 
1229 	if ((blk->tfb_tcp_output == NULL) ||
1230 	    (blk->tfb_tcp_do_segment == NULL) ||
1231 	    (blk->tfb_tcp_ctloutput == NULL) ||
1232 	    (strlen(blk->tfb_tcp_block_name) == 0)) {
1233 		/*
1234 		 * These functions are required and you
1235 		 * need a name.
1236 		 */
1237 		*num_names = 0;
1238 		return (EINVAL);
1239 	}
1240 
1241 	if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1242 		*num_names = 0;
1243 		return (EINVAL);
1244 	}
1245 
1246 	refcount_init(&blk->tfb_refcnt, 0);
1247 	blk->tfb_id = atomic_fetchadd_int(&next_tcp_stack_id, 1);
1248 	for (i = 0; i < *num_names; i++) {
1249 		n = malloc(sizeof(struct tcp_function), M_TCPFUNCTIONS, wait);
1250 		if (n == NULL) {
1251 			error = ENOMEM;
1252 			goto cleanup;
1253 		}
1254 		n->tf_fb = blk;
1255 
1256 		(void)strlcpy(fs.function_set_name, names[i],
1257 		    sizeof(fs.function_set_name));
1258 		rw_wlock(&tcp_function_lock);
1259 		if (find_tcp_functions_locked(&fs) != NULL) {
1260 			/* Duplicate name space not allowed */
1261 			rw_wunlock(&tcp_function_lock);
1262 			free(n, M_TCPFUNCTIONS);
1263 			error = EALREADY;
1264 			goto cleanup;
1265 		}
1266 		(void)strlcpy(n->tf_name, names[i], sizeof(n->tf_name));
1267 		TAILQ_INSERT_TAIL(&t_functions, n, tf_next);
1268 		tcp_fb_cnt++;
1269 		rw_wunlock(&tcp_function_lock);
1270 	}
1271 	return(0);
1272 
1273 cleanup:
1274 	/*
1275 	 * Deregister the names we just added. Because registration failed
1276 	 * for names[i], we don't need to deregister that name.
1277 	 */
1278 	*num_names = i;
1279 	rw_wlock(&tcp_function_lock);
1280 	while (--i >= 0) {
1281 		TAILQ_FOREACH(n, &t_functions, tf_next) {
1282 			if (!strncmp(n->tf_name, names[i],
1283 			    TCP_FUNCTION_NAME_LEN_MAX)) {
1284 				TAILQ_REMOVE(&t_functions, n, tf_next);
1285 				tcp_fb_cnt--;
1286 				n->tf_fb = NULL;
1287 				free(n, M_TCPFUNCTIONS);
1288 				break;
1289 			}
1290 		}
1291 	}
1292 	rw_wunlock(&tcp_function_lock);
1293 	return (error);
1294 }
1295 
1296 /*
1297  * Register a TCP function block using the name provided in the name
1298  * argument.
1299  *
1300  * Returns 0 on success, or an error code on failure.
1301  */
1302 int
1303 register_tcp_functions_as_name(struct tcp_function_block *blk, const char *name,
1304     int wait)
1305 {
1306 	const char *name_list[1];
1307 	int num_names, rv;
1308 
1309 	num_names = 1;
1310 	if (name != NULL)
1311 		name_list[0] = name;
1312 	else
1313 		name_list[0] = blk->tfb_tcp_block_name;
1314 	rv = register_tcp_functions_as_names(blk, wait, name_list, &num_names);
1315 	return (rv);
1316 }
1317 
1318 /*
1319  * Register a TCP function block using the name defined in
1320  * blk->tfb_tcp_block_name.
1321  *
1322  * Returns 0 on success, or an error code on failure.
1323  */
1324 int
1325 register_tcp_functions(struct tcp_function_block *blk, int wait)
1326 {
1327 
1328 	return (register_tcp_functions_as_name(blk, NULL, wait));
1329 }
1330 
1331 /*
1332  * Deregister all names associated with a function block. This
1333  * functionally removes the function block from use within the system.
1334  *
1335  * When called with a true quiesce argument, mark the function block
1336  * as being removed so no more stacks will use it and determine
1337  * whether the removal would succeed.
1338  *
1339  * When called with a false quiesce argument, actually attempt the
1340  * removal.
1341  *
1342  * When called with a force argument, attempt to switch all TCBs to
1343  * use the default stack instead of returning EBUSY.
1344  *
1345  * Returns 0 on success (or if the removal would succeed), or an error
1346  * code on failure.
1347  */
1348 int
1349 deregister_tcp_functions(struct tcp_function_block *blk, bool quiesce,
1350     bool force)
1351 {
1352 	struct tcp_function *f;
1353 	VNET_ITERATOR_DECL(vnet_iter);
1354 
1355 	if (blk == &tcp_def_funcblk) {
1356 		/* You can't un-register the default */
1357 		return (EPERM);
1358 	}
1359 	rw_wlock(&tcp_function_lock);
1360 	VNET_LIST_RLOCK_NOSLEEP();
1361 	VNET_FOREACH(vnet_iter) {
1362 		CURVNET_SET(vnet_iter);
1363 		if (blk == V_tcp_func_set_ptr) {
1364 			/* You can't free the current default in some vnet. */
1365 			CURVNET_RESTORE();
1366 			VNET_LIST_RUNLOCK_NOSLEEP();
1367 			rw_wunlock(&tcp_function_lock);
1368 			return (EBUSY);
1369 		}
1370 		CURVNET_RESTORE();
1371 	}
1372 	VNET_LIST_RUNLOCK_NOSLEEP();
1373 	/* Mark the block so no more stacks can use it. */
1374 	blk->tfb_flags |= TCP_FUNC_BEING_REMOVED;
1375 	/*
1376 	 * If TCBs are still attached to the stack, attempt to switch them
1377 	 * to the default stack.
1378 	 */
1379 	if (force && blk->tfb_refcnt) {
1380 		struct inpcb *inp;
1381 		struct tcpcb *tp;
1382 		VNET_ITERATOR_DECL(vnet_iter);
1383 
1384 		rw_wunlock(&tcp_function_lock);
1385 
1386 		VNET_LIST_RLOCK();
1387 		VNET_FOREACH(vnet_iter) {
1388 			CURVNET_SET(vnet_iter);
1389 			struct inpcb_iterator inpi = INP_ALL_ITERATOR(&V_tcbinfo,
1390 			    INPLOOKUP_WLOCKPCB);
1391 
1392 			while ((inp = inp_next(&inpi)) != NULL) {
1393 				tp = intotcpcb(inp);
1394 				if (tp == NULL || tp->t_fb != blk)
1395 					continue;
1396 				tcp_switch_back_to_default(tp);
1397 			}
1398 			CURVNET_RESTORE();
1399 		}
1400 		VNET_LIST_RUNLOCK();
1401 
1402 		rw_wlock(&tcp_function_lock);
1403 	}
1404 	if (blk->tfb_refcnt) {
1405 		/* TCBs still attached. */
1406 		rw_wunlock(&tcp_function_lock);
1407 		return (EBUSY);
1408 	}
1409 	if (quiesce) {
1410 		/* Skip removal. */
1411 		rw_wunlock(&tcp_function_lock);
1412 		return (0);
1413 	}
1414 	/* Remove any function names that map to this function block. */
1415 	while (find_tcp_fb_locked(blk, &f) != NULL) {
1416 		TAILQ_REMOVE(&t_functions, f, tf_next);
1417 		tcp_fb_cnt--;
1418 		f->tf_fb = NULL;
1419 		free(f, M_TCPFUNCTIONS);
1420 	}
1421 	rw_wunlock(&tcp_function_lock);
1422 	return (0);
1423 }
1424 
1425 static void
1426 tcp_drain(void)
1427 {
1428 	struct epoch_tracker et;
1429 	VNET_ITERATOR_DECL(vnet_iter);
1430 
1431 	if (!do_tcpdrain)
1432 		return;
1433 
1434 	NET_EPOCH_ENTER(et);
1435 	VNET_LIST_RLOCK_NOSLEEP();
1436 	VNET_FOREACH(vnet_iter) {
1437 		CURVNET_SET(vnet_iter);
1438 		struct inpcb_iterator inpi = INP_ALL_ITERATOR(&V_tcbinfo,
1439 		    INPLOOKUP_WLOCKPCB);
1440 		struct inpcb *inpb;
1441 		struct tcpcb *tcpb;
1442 
1443 	/*
1444 	 * Walk the tcpbs, if existing, and flush the reassembly queue,
1445 	 * if there is one...
1446 	 * XXX: The "Net/3" implementation doesn't imply that the TCP
1447 	 *      reassembly queue should be flushed, but in a situation
1448 	 *	where we're really low on mbufs, this is potentially
1449 	 *	useful.
1450 	 */
1451 		while ((inpb = inp_next(&inpi)) != NULL) {
1452 			if ((tcpb = intotcpcb(inpb)) != NULL) {
1453 				tcp_reass_flush(tcpb);
1454 				tcp_clean_sackreport(tcpb);
1455 #ifdef TCP_BLACKBOX
1456 				tcp_log_drain(tcpb);
1457 #endif
1458 #ifdef TCPPCAP
1459 				if (tcp_pcap_aggressive_free) {
1460 					/* Free the TCP PCAP queues. */
1461 					tcp_pcap_drain(&(tcpb->t_inpkts));
1462 					tcp_pcap_drain(&(tcpb->t_outpkts));
1463 				}
1464 #endif
1465 			}
1466 		}
1467 		CURVNET_RESTORE();
1468 	}
1469 	VNET_LIST_RUNLOCK_NOSLEEP();
1470 	NET_EPOCH_EXIT(et);
1471 }
1472 
1473 static void
1474 tcp_vnet_init(void *arg __unused)
1475 {
1476 
1477 #ifdef TCP_HHOOK
1478 	if (hhook_head_register(HHOOK_TYPE_TCP, HHOOK_TCP_EST_IN,
1479 	    &V_tcp_hhh[HHOOK_TCP_EST_IN], HHOOK_NOWAIT|HHOOK_HEADISINVNET) != 0)
1480 		printf("%s: WARNING: unable to register helper hook\n", __func__);
1481 	if (hhook_head_register(HHOOK_TYPE_TCP, HHOOK_TCP_EST_OUT,
1482 	    &V_tcp_hhh[HHOOK_TCP_EST_OUT], HHOOK_NOWAIT|HHOOK_HEADISINVNET) != 0)
1483 		printf("%s: WARNING: unable to register helper hook\n", __func__);
1484 #endif
1485 #ifdef STATS
1486 	if (tcp_stats_init())
1487 		printf("%s: WARNING: unable to initialise TCP stats\n",
1488 		    __func__);
1489 #endif
1490 	in_pcbinfo_init(&V_tcbinfo, &tcpcbstor, tcp_tcbhashsize,
1491 	    tcp_tcbhashsize);
1492 
1493 	syncache_init();
1494 	tcp_hc_init();
1495 
1496 	TUNABLE_INT_FETCH("net.inet.tcp.sack.enable", &V_tcp_do_sack);
1497 	V_sack_hole_zone = uma_zcreate("sackhole", sizeof(struct sackhole),
1498 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1499 
1500 	tcp_fastopen_init();
1501 
1502 	COUNTER_ARRAY_ALLOC(V_tcps_states, TCP_NSTATES, M_WAITOK);
1503 	VNET_PCPUSTAT_ALLOC(tcpstat, M_WAITOK);
1504 
1505 	V_tcp_msl = TCPTV_MSL;
1506 }
1507 VNET_SYSINIT(tcp_vnet_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
1508     tcp_vnet_init, NULL);
1509 
1510 static void
1511 tcp_init(void *arg __unused)
1512 {
1513 	const char *tcbhash_tuneable;
1514 	int hashsize;
1515 
1516 	tcp_reass_global_init();
1517 
1518 	/* XXX virtualize those below? */
1519 	tcp_delacktime = TCPTV_DELACK;
1520 	tcp_keepinit = TCPTV_KEEP_INIT;
1521 	tcp_keepidle = TCPTV_KEEP_IDLE;
1522 	tcp_keepintvl = TCPTV_KEEPINTVL;
1523 	tcp_maxpersistidle = TCPTV_KEEP_IDLE;
1524 	tcp_rexmit_initial = TCPTV_RTOBASE;
1525 	if (tcp_rexmit_initial < 1)
1526 		tcp_rexmit_initial = 1;
1527 	tcp_rexmit_min = TCPTV_MIN;
1528 	if (tcp_rexmit_min < 1)
1529 		tcp_rexmit_min = 1;
1530 	tcp_persmin = TCPTV_PERSMIN;
1531 	tcp_persmax = TCPTV_PERSMAX;
1532 	tcp_rexmit_slop = TCPTV_CPU_VAR;
1533 	tcp_finwait2_timeout = TCPTV_FINWAIT2_TIMEOUT;
1534 
1535 	/* Setup the tcp function block list */
1536 	TAILQ_INIT(&t_functions);
1537 	rw_init(&tcp_function_lock, "tcp_func_lock");
1538 	register_tcp_functions(&tcp_def_funcblk, M_WAITOK);
1539 	sx_init(&tcpoudp_lock, "TCP over UDP configuration");
1540 #ifdef TCP_BLACKBOX
1541 	/* Initialize the TCP logging data. */
1542 	tcp_log_init();
1543 #endif
1544 	arc4rand(&V_ts_offset_secret, sizeof(V_ts_offset_secret), 0);
1545 
1546 	if (tcp_soreceive_stream) {
1547 #ifdef INET
1548 		tcp_protosw.pr_soreceive = soreceive_stream;
1549 #endif
1550 #ifdef INET6
1551 		tcp6_protosw.pr_soreceive = soreceive_stream;
1552 #endif /* INET6 */
1553 	}
1554 
1555 #ifdef INET6
1556 	max_protohdr_grow(sizeof(struct ip6_hdr) + sizeof(struct tcphdr));
1557 #else /* INET6 */
1558 	max_protohdr_grow(sizeof(struct tcpiphdr));
1559 #endif /* INET6 */
1560 
1561 	ISN_LOCK_INIT();
1562 	EVENTHANDLER_REGISTER(shutdown_pre_sync, tcp_fini, NULL,
1563 		SHUTDOWN_PRI_DEFAULT);
1564 	EVENTHANDLER_REGISTER(vm_lowmem, tcp_drain, NULL, LOWMEM_PRI_DEFAULT);
1565 	EVENTHANDLER_REGISTER(mbuf_lowmem, tcp_drain, NULL, LOWMEM_PRI_DEFAULT);
1566 
1567 	tcp_inp_lro_direct_queue = counter_u64_alloc(M_WAITOK);
1568 	tcp_inp_lro_wokeup_queue = counter_u64_alloc(M_WAITOK);
1569 	tcp_inp_lro_compressed = counter_u64_alloc(M_WAITOK);
1570 	tcp_inp_lro_locks_taken = counter_u64_alloc(M_WAITOK);
1571 	tcp_extra_mbuf = counter_u64_alloc(M_WAITOK);
1572 	tcp_would_have_but = counter_u64_alloc(M_WAITOK);
1573 	tcp_comp_total = counter_u64_alloc(M_WAITOK);
1574 	tcp_uncomp_total = counter_u64_alloc(M_WAITOK);
1575 	tcp_bad_csums = counter_u64_alloc(M_WAITOK);
1576 	tcp_pacing_failures = counter_u64_alloc(M_WAITOK);
1577 #ifdef TCPPCAP
1578 	tcp_pcap_init();
1579 #endif
1580 
1581 	hashsize = TCBHASHSIZE;
1582 	tcbhash_tuneable = "net.inet.tcp.tcbhashsize";
1583 	TUNABLE_INT_FETCH(tcbhash_tuneable, &hashsize);
1584 	if (hashsize == 0) {
1585 		/*
1586 		 * Auto tune the hash size based on maxsockets.
1587 		 * A perfect hash would have a 1:1 mapping
1588 		 * (hashsize = maxsockets) however it's been
1589 		 * suggested that O(2) average is better.
1590 		 */
1591 		hashsize = maketcp_hashsize(maxsockets / 4);
1592 		/*
1593 		 * Our historical default is 512,
1594 		 * do not autotune lower than this.
1595 		 */
1596 		if (hashsize < 512)
1597 			hashsize = 512;
1598 		if (bootverbose)
1599 			printf("%s: %s auto tuned to %d\n", __func__,
1600 			    tcbhash_tuneable, hashsize);
1601 	}
1602 	/*
1603 	 * We require a hashsize to be a power of two.
1604 	 * Previously if it was not a power of two we would just reset it
1605 	 * back to 512, which could be a nasty surprise if you did not notice
1606 	 * the error message.
1607 	 * Instead what we do is clip it to the closest power of two lower
1608 	 * than the specified hash value.
1609 	 */
1610 	if (!powerof2(hashsize)) {
1611 		int oldhashsize = hashsize;
1612 
1613 		hashsize = maketcp_hashsize(hashsize);
1614 		/* prevent absurdly low value */
1615 		if (hashsize < 16)
1616 			hashsize = 16;
1617 		printf("%s: WARNING: TCB hash size not a power of 2, "
1618 		    "clipped from %d to %d.\n", __func__, oldhashsize,
1619 		    hashsize);
1620 	}
1621 	tcp_tcbhashsize = hashsize;
1622 
1623 #ifdef INET
1624 	IPPROTO_REGISTER(IPPROTO_TCP, tcp_input, tcp_ctlinput);
1625 #endif
1626 #ifdef INET6
1627 	IP6PROTO_REGISTER(IPPROTO_TCP, tcp6_input, tcp6_ctlinput);
1628 #endif
1629 }
1630 SYSINIT(tcp_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, tcp_init, NULL);
1631 
1632 #ifdef VIMAGE
1633 static void
1634 tcp_destroy(void *unused __unused)
1635 {
1636 	int n;
1637 #ifdef TCP_HHOOK
1638 	int error;
1639 #endif
1640 
1641 	/*
1642 	 * All our processes are gone, all our sockets should be cleaned
1643 	 * up, which means, we should be past the tcp_discardcb() calls.
1644 	 * Sleep to let all tcpcb timers really disappear and cleanup.
1645 	 */
1646 	for (;;) {
1647 		INP_INFO_WLOCK(&V_tcbinfo);
1648 		n = V_tcbinfo.ipi_count;
1649 		INP_INFO_WUNLOCK(&V_tcbinfo);
1650 		if (n == 0)
1651 			break;
1652 		pause("tcpdes", hz / 10);
1653 	}
1654 	tcp_hc_destroy();
1655 	syncache_destroy();
1656 	in_pcbinfo_destroy(&V_tcbinfo);
1657 	/* tcp_discardcb() clears the sack_holes up. */
1658 	uma_zdestroy(V_sack_hole_zone);
1659 
1660 	/*
1661 	 * Cannot free the zone until all tcpcbs are released as we attach
1662 	 * the allocations to them.
1663 	 */
1664 	tcp_fastopen_destroy();
1665 
1666 	COUNTER_ARRAY_FREE(V_tcps_states, TCP_NSTATES);
1667 	VNET_PCPUSTAT_FREE(tcpstat);
1668 
1669 #ifdef TCP_HHOOK
1670 	error = hhook_head_deregister(V_tcp_hhh[HHOOK_TCP_EST_IN]);
1671 	if (error != 0) {
1672 		printf("%s: WARNING: unable to deregister helper hook "
1673 		    "type=%d, id=%d: error %d returned\n", __func__,
1674 		    HHOOK_TYPE_TCP, HHOOK_TCP_EST_IN, error);
1675 	}
1676 	error = hhook_head_deregister(V_tcp_hhh[HHOOK_TCP_EST_OUT]);
1677 	if (error != 0) {
1678 		printf("%s: WARNING: unable to deregister helper hook "
1679 		    "type=%d, id=%d: error %d returned\n", __func__,
1680 		    HHOOK_TYPE_TCP, HHOOK_TCP_EST_OUT, error);
1681 	}
1682 #endif
1683 }
1684 VNET_SYSUNINIT(tcp, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, tcp_destroy, NULL);
1685 #endif
1686 
1687 void
1688 tcp_fini(void *xtp)
1689 {
1690 
1691 }
1692 
1693 /*
1694  * Fill in the IP and TCP headers for an outgoing packet, given the tcpcb.
1695  * tcp_template used to store this data in mbufs, but we now recopy it out
1696  * of the tcpcb each time to conserve mbufs.
1697  */
1698 void
1699 tcpip_fillheaders(struct inpcb *inp, uint16_t port, void *ip_ptr, void *tcp_ptr)
1700 {
1701 	struct tcphdr *th = (struct tcphdr *)tcp_ptr;
1702 
1703 	INP_WLOCK_ASSERT(inp);
1704 
1705 #ifdef INET6
1706 	if ((inp->inp_vflag & INP_IPV6) != 0) {
1707 		struct ip6_hdr *ip6;
1708 
1709 		ip6 = (struct ip6_hdr *)ip_ptr;
1710 		ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
1711 			(inp->inp_flow & IPV6_FLOWINFO_MASK);
1712 		ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
1713 			(IPV6_VERSION & IPV6_VERSION_MASK);
1714 		if (port == 0)
1715 			ip6->ip6_nxt = IPPROTO_TCP;
1716 		else
1717 			ip6->ip6_nxt = IPPROTO_UDP;
1718 		ip6->ip6_plen = htons(sizeof(struct tcphdr));
1719 		ip6->ip6_src = inp->in6p_laddr;
1720 		ip6->ip6_dst = inp->in6p_faddr;
1721 	}
1722 #endif /* INET6 */
1723 #if defined(INET6) && defined(INET)
1724 	else
1725 #endif
1726 #ifdef INET
1727 	{
1728 		struct ip *ip;
1729 
1730 		ip = (struct ip *)ip_ptr;
1731 		ip->ip_v = IPVERSION;
1732 		ip->ip_hl = 5;
1733 		ip->ip_tos = inp->inp_ip_tos;
1734 		ip->ip_len = 0;
1735 		ip->ip_id = 0;
1736 		ip->ip_off = 0;
1737 		ip->ip_ttl = inp->inp_ip_ttl;
1738 		ip->ip_sum = 0;
1739 		if (port == 0)
1740 			ip->ip_p = IPPROTO_TCP;
1741 		else
1742 			ip->ip_p = IPPROTO_UDP;
1743 		ip->ip_src = inp->inp_laddr;
1744 		ip->ip_dst = inp->inp_faddr;
1745 	}
1746 #endif /* INET */
1747 	th->th_sport = inp->inp_lport;
1748 	th->th_dport = inp->inp_fport;
1749 	th->th_seq = 0;
1750 	th->th_ack = 0;
1751 	th->th_off = 5;
1752 	tcp_set_flags(th, 0);
1753 	th->th_win = 0;
1754 	th->th_urp = 0;
1755 	th->th_sum = 0;		/* in_pseudo() is called later for ipv4 */
1756 }
1757 
1758 /*
1759  * Create template to be used to send tcp packets on a connection.
1760  * Allocates an mbuf and fills in a skeletal tcp/ip header.  The only
1761  * use for this function is in keepalives, which use tcp_respond.
1762  */
1763 struct tcptemp *
1764 tcpip_maketemplate(struct inpcb *inp)
1765 {
1766 	struct tcptemp *t;
1767 
1768 	t = malloc(sizeof(*t), M_TEMP, M_NOWAIT);
1769 	if (t == NULL)
1770 		return (NULL);
1771 	tcpip_fillheaders(inp, 0, (void *)&t->tt_ipgen, (void *)&t->tt_t);
1772 	return (t);
1773 }
1774 
1775 /*
1776  * Send a single message to the TCP at address specified by
1777  * the given TCP/IP header.  If m == NULL, then we make a copy
1778  * of the tcpiphdr at th and send directly to the addressed host.
1779  * This is used to force keep alive messages out using the TCP
1780  * template for a connection.  If flags are given then we send
1781  * a message back to the TCP which originated the segment th,
1782  * and discard the mbuf containing it and any other attached mbufs.
1783  *
1784  * In any case the ack and sequence number of the transmitted
1785  * segment are as specified by the parameters.
1786  *
1787  * NOTE: If m != NULL, then th must point to *inside* the mbuf.
1788  */
1789 void
1790 tcp_respond(struct tcpcb *tp, void *ipgen, struct tcphdr *th, struct mbuf *m,
1791     tcp_seq ack, tcp_seq seq, uint16_t flags)
1792 {
1793 	struct tcpopt to;
1794 	struct inpcb *inp;
1795 	struct ip *ip;
1796 	struct mbuf *optm;
1797 	struct udphdr *uh = NULL;
1798 	struct tcphdr *nth;
1799 	struct tcp_log_buffer *lgb;
1800 	u_char *optp;
1801 #ifdef INET6
1802 	struct ip6_hdr *ip6;
1803 	int isipv6;
1804 #endif /* INET6 */
1805 	int optlen, tlen, win, ulen;
1806 	int ect = 0;
1807 	bool incl_opts;
1808 	uint16_t port;
1809 	int output_ret;
1810 #ifdef INVARIANTS
1811 	int thflags = tcp_get_flags(th);
1812 #endif
1813 
1814 	KASSERT(tp != NULL || m != NULL, ("tcp_respond: tp and m both NULL"));
1815 	NET_EPOCH_ASSERT();
1816 
1817 #ifdef INET6
1818 	isipv6 = ((struct ip *)ipgen)->ip_v == (IPV6_VERSION >> 4);
1819 	ip6 = ipgen;
1820 #endif /* INET6 */
1821 	ip = ipgen;
1822 
1823 	if (tp != NULL) {
1824 		inp = tptoinpcb(tp);
1825 		INP_LOCK_ASSERT(inp);
1826 	} else
1827 		inp = NULL;
1828 
1829 	if (m != NULL) {
1830 #ifdef INET6
1831 		if (isipv6 && ip6 && (ip6->ip6_nxt == IPPROTO_UDP))
1832 			port = m->m_pkthdr.tcp_tun_port;
1833 		else
1834 #endif
1835 		if (ip && (ip->ip_p == IPPROTO_UDP))
1836 			port = m->m_pkthdr.tcp_tun_port;
1837 		else
1838 			port = 0;
1839 	} else
1840 		port = tp->t_port;
1841 
1842 	incl_opts = false;
1843 	win = 0;
1844 	if (tp != NULL) {
1845 		if (!(flags & TH_RST)) {
1846 			win = sbspace(&inp->inp_socket->so_rcv);
1847 			if (win > TCP_MAXWIN << tp->rcv_scale)
1848 				win = TCP_MAXWIN << tp->rcv_scale;
1849 		}
1850 		if ((tp->t_flags & TF_NOOPT) == 0)
1851 			incl_opts = true;
1852 	}
1853 	if (m == NULL) {
1854 		m = m_gethdr(M_NOWAIT, MT_DATA);
1855 		if (m == NULL)
1856 			return;
1857 		m->m_data += max_linkhdr;
1858 #ifdef INET6
1859 		if (isipv6) {
1860 			bcopy((caddr_t)ip6, mtod(m, caddr_t),
1861 			      sizeof(struct ip6_hdr));
1862 			ip6 = mtod(m, struct ip6_hdr *);
1863 			nth = (struct tcphdr *)(ip6 + 1);
1864 			if (port) {
1865 				/* Insert a UDP header */
1866 				uh = (struct udphdr *)nth;
1867 				uh->uh_sport = htons(V_tcp_udp_tunneling_port);
1868 				uh->uh_dport = port;
1869 				nth = (struct tcphdr *)(uh + 1);
1870 			}
1871 		} else
1872 #endif /* INET6 */
1873 		{
1874 			bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
1875 			ip = mtod(m, struct ip *);
1876 			nth = (struct tcphdr *)(ip + 1);
1877 			if (port) {
1878 				/* Insert a UDP header */
1879 				uh = (struct udphdr *)nth;
1880 				uh->uh_sport = htons(V_tcp_udp_tunneling_port);
1881 				uh->uh_dport = port;
1882 				nth = (struct tcphdr *)(uh + 1);
1883 			}
1884 		}
1885 		bcopy((caddr_t)th, (caddr_t)nth, sizeof(struct tcphdr));
1886 		flags = TH_ACK;
1887 	} else if ((!M_WRITABLE(m)) || (port != 0)) {
1888 		struct mbuf *n;
1889 
1890 		/* Can't reuse 'm', allocate a new mbuf. */
1891 		n = m_gethdr(M_NOWAIT, MT_DATA);
1892 		if (n == NULL) {
1893 			m_freem(m);
1894 			return;
1895 		}
1896 
1897 		if (!m_dup_pkthdr(n, m, M_NOWAIT)) {
1898 			m_freem(m);
1899 			m_freem(n);
1900 			return;
1901 		}
1902 
1903 		n->m_data += max_linkhdr;
1904 		/* m_len is set later */
1905 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
1906 #ifdef INET6
1907 		if (isipv6) {
1908 			bcopy((caddr_t)ip6, mtod(n, caddr_t),
1909 			      sizeof(struct ip6_hdr));
1910 			ip6 = mtod(n, struct ip6_hdr *);
1911 			xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr);
1912 			nth = (struct tcphdr *)(ip6 + 1);
1913 			if (port) {
1914 				/* Insert a UDP header */
1915 				uh = (struct udphdr *)nth;
1916 				uh->uh_sport = htons(V_tcp_udp_tunneling_port);
1917 				uh->uh_dport = port;
1918 				nth = (struct tcphdr *)(uh + 1);
1919 			}
1920 		} else
1921 #endif /* INET6 */
1922 		{
1923 			bcopy((caddr_t)ip, mtod(n, caddr_t), sizeof(struct ip));
1924 			ip = mtod(n, struct ip *);
1925 			xchg(ip->ip_dst.s_addr, ip->ip_src.s_addr, uint32_t);
1926 			nth = (struct tcphdr *)(ip + 1);
1927 			if (port) {
1928 				/* Insert a UDP header */
1929 				uh = (struct udphdr *)nth;
1930 				uh->uh_sport = htons(V_tcp_udp_tunneling_port);
1931 				uh->uh_dport = port;
1932 				nth = (struct tcphdr *)(uh + 1);
1933 			}
1934 		}
1935 		bcopy((caddr_t)th, (caddr_t)nth, sizeof(struct tcphdr));
1936 		xchg(nth->th_dport, nth->th_sport, uint16_t);
1937 		th = nth;
1938 		m_freem(m);
1939 		m = n;
1940 	} else {
1941 		/*
1942 		 *  reuse the mbuf.
1943 		 * XXX MRT We inherit the FIB, which is lucky.
1944 		 */
1945 		m_freem(m->m_next);
1946 		m->m_next = NULL;
1947 		m->m_data = (caddr_t)ipgen;
1948 		/* clear any receive flags for proper bpf timestamping */
1949 		m->m_flags &= ~(M_TSTMP | M_TSTMP_LRO);
1950 		/* m_len is set later */
1951 #ifdef INET6
1952 		if (isipv6) {
1953 			xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr);
1954 			nth = (struct tcphdr *)(ip6 + 1);
1955 		} else
1956 #endif /* INET6 */
1957 		{
1958 			xchg(ip->ip_dst.s_addr, ip->ip_src.s_addr, uint32_t);
1959 			nth = (struct tcphdr *)(ip + 1);
1960 		}
1961 		if (th != nth) {
1962 			/*
1963 			 * this is usually a case when an extension header
1964 			 * exists between the IPv6 header and the
1965 			 * TCP header.
1966 			 */
1967 			nth->th_sport = th->th_sport;
1968 			nth->th_dport = th->th_dport;
1969 		}
1970 		xchg(nth->th_dport, nth->th_sport, uint16_t);
1971 #undef xchg
1972 	}
1973 	tlen = 0;
1974 #ifdef INET6
1975 	if (isipv6)
1976 		tlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
1977 #endif
1978 #if defined(INET) && defined(INET6)
1979 	else
1980 #endif
1981 #ifdef INET
1982 		tlen = sizeof (struct tcpiphdr);
1983 #endif
1984 	if (port)
1985 		tlen += sizeof (struct udphdr);
1986 #ifdef INVARIANTS
1987 	m->m_len = 0;
1988 	KASSERT(M_TRAILINGSPACE(m) >= tlen,
1989 	    ("Not enough trailing space for message (m=%p, need=%d, have=%ld)",
1990 	    m, tlen, (long)M_TRAILINGSPACE(m)));
1991 #endif
1992 	m->m_len = tlen;
1993 	to.to_flags = 0;
1994 	if (incl_opts) {
1995 		ect = tcp_ecn_output_established(tp, &flags, 0, false);
1996 		/* Make sure we have room. */
1997 		if (M_TRAILINGSPACE(m) < TCP_MAXOLEN) {
1998 			m->m_next = m_get(M_NOWAIT, MT_DATA);
1999 			if (m->m_next) {
2000 				optp = mtod(m->m_next, u_char *);
2001 				optm = m->m_next;
2002 			} else
2003 				incl_opts = false;
2004 		} else {
2005 			optp = (u_char *) (nth + 1);
2006 			optm = m;
2007 		}
2008 	}
2009 	if (incl_opts) {
2010 		/* Timestamps. */
2011 		if (tp->t_flags & TF_RCVD_TSTMP) {
2012 			to.to_tsval = tcp_ts_getticks() + tp->ts_offset;
2013 			to.to_tsecr = tp->ts_recent;
2014 			to.to_flags |= TOF_TS;
2015 		}
2016 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2017 		/* TCP-MD5 (RFC2385). */
2018 		if (tp->t_flags & TF_SIGNATURE)
2019 			to.to_flags |= TOF_SIGNATURE;
2020 #endif
2021 		/* Add the options. */
2022 		tlen += optlen = tcp_addoptions(&to, optp);
2023 
2024 		/* Update m_len in the correct mbuf. */
2025 		optm->m_len += optlen;
2026 	} else
2027 		optlen = 0;
2028 #ifdef INET6
2029 	if (isipv6) {
2030 		if (uh) {
2031 			ulen = tlen - sizeof(struct ip6_hdr);
2032 			uh->uh_ulen = htons(ulen);
2033 		}
2034 		ip6->ip6_flow = htonl(ect << IPV6_FLOWLABEL_LEN);
2035 		ip6->ip6_vfc = IPV6_VERSION;
2036 		if (port)
2037 			ip6->ip6_nxt = IPPROTO_UDP;
2038 		else
2039 			ip6->ip6_nxt = IPPROTO_TCP;
2040 		ip6->ip6_plen = htons(tlen - sizeof(*ip6));
2041 	}
2042 #endif
2043 #if defined(INET) && defined(INET6)
2044 	else
2045 #endif
2046 #ifdef INET
2047 	{
2048 		if (uh) {
2049 			ulen = tlen - sizeof(struct ip);
2050 			uh->uh_ulen = htons(ulen);
2051 		}
2052 		ip->ip_tos = ect;
2053 		ip->ip_len = htons(tlen);
2054 		ip->ip_ttl = V_ip_defttl;
2055 		if (port) {
2056 			ip->ip_p = IPPROTO_UDP;
2057 		} else {
2058 			ip->ip_p = IPPROTO_TCP;
2059 		}
2060 		if (V_path_mtu_discovery)
2061 			ip->ip_off |= htons(IP_DF);
2062 	}
2063 #endif
2064 	m->m_pkthdr.len = tlen;
2065 	m->m_pkthdr.rcvif = NULL;
2066 #ifdef MAC
2067 	if (inp != NULL) {
2068 		/*
2069 		 * Packet is associated with a socket, so allow the
2070 		 * label of the response to reflect the socket label.
2071 		 */
2072 		INP_LOCK_ASSERT(inp);
2073 		mac_inpcb_create_mbuf(inp, m);
2074 	} else {
2075 		/*
2076 		 * Packet is not associated with a socket, so possibly
2077 		 * update the label in place.
2078 		 */
2079 		mac_netinet_tcp_reply(m);
2080 	}
2081 #endif
2082 	nth->th_seq = htonl(seq);
2083 	nth->th_ack = htonl(ack);
2084 	nth->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
2085 	tcp_set_flags(nth, flags);
2086 	if (tp && (flags & TH_RST)) {
2087 		/* Log the reset */
2088 		tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
2089 	}
2090 	if (tp != NULL)
2091 		nth->th_win = htons((u_short) (win >> tp->rcv_scale));
2092 	else
2093 		nth->th_win = htons((u_short)win);
2094 	nth->th_urp = 0;
2095 
2096 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2097 	if (to.to_flags & TOF_SIGNATURE) {
2098 		if (!TCPMD5_ENABLED() ||
2099 		    TCPMD5_OUTPUT(m, nth, to.to_signature) != 0) {
2100 			m_freem(m);
2101 			return;
2102 		}
2103 	}
2104 #endif
2105 
2106 #ifdef INET6
2107 	if (isipv6) {
2108 		if (port) {
2109 			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
2110 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
2111 			uh->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0);
2112 			nth->th_sum = 0;
2113 		} else {
2114 			m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
2115 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
2116 			nth->th_sum = in6_cksum_pseudo(ip6,
2117 			    tlen - sizeof(struct ip6_hdr), IPPROTO_TCP, 0);
2118 		}
2119 		ip6->ip6_hlim = in6_selecthlim(inp, NULL);
2120 	}
2121 #endif /* INET6 */
2122 #if defined(INET6) && defined(INET)
2123 	else
2124 #endif
2125 #ifdef INET
2126 	{
2127 		if (port) {
2128 			uh->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
2129 			    htons(ulen + IPPROTO_UDP));
2130 			m->m_pkthdr.csum_flags = CSUM_UDP;
2131 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
2132 			nth->th_sum = 0;
2133 		} else {
2134 			m->m_pkthdr.csum_flags = CSUM_TCP;
2135 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
2136 			nth->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
2137 			    htons((u_short)(tlen - sizeof(struct ip) + ip->ip_p)));
2138 		}
2139 	}
2140 #endif /* INET */
2141 	TCP_PROBE3(debug__output, tp, th, m);
2142 	if (flags & TH_RST)
2143 		TCP_PROBE5(accept__refused, NULL, NULL, m, tp, nth);
2144 	lgb = NULL;
2145 	if ((tp != NULL) && tcp_bblogging_on(tp)) {
2146 		if (INP_WLOCKED(inp)) {
2147 			union tcp_log_stackspecific log;
2148 			struct timeval tv;
2149 
2150 			memset(&log.u_bbr, 0, sizeof(log.u_bbr));
2151 			log.u_bbr.inhpts = inp->inp_in_hpts;
2152 			log.u_bbr.flex8 = 4;
2153 			log.u_bbr.pkts_out = tp->t_maxseg;
2154 			log.u_bbr.timeStamp = tcp_get_usecs(&tv);
2155 			log.u_bbr.delivered = 0;
2156 			lgb = tcp_log_event(tp, nth, NULL, NULL, TCP_LOG_OUT,
2157 			    ERRNO_UNK, 0, &log, false, NULL, NULL, 0, &tv);
2158 		} else {
2159 			/*
2160 			 * We can not log the packet, since we only own the
2161 			 * read lock, but a write lock is needed. The read lock
2162 			 * is not upgraded to a write lock, since only getting
2163 			 * the read lock was done intentionally to improve the
2164 			 * handling of SYN flooding attacks.
2165 			 * This happens only for pure SYN segments received in
2166 			 * the initial CLOSED state, or received in a more
2167 			 * advanced state than listen and the UDP encapsulation
2168 			 * port is unexpected.
2169 			 * The incoming SYN segments do not really belong to
2170 			 * the TCP connection and the handling does not change
2171 			 * the state of the TCP connection. Therefore, the
2172 			 * sending of the RST segments is not logged. Please
2173 			 * note that also the incoming SYN segments are not
2174 			 * logged.
2175 			 *
2176 			 * The following code ensures that the above description
2177 			 * is and stays correct.
2178 			 */
2179 			KASSERT((thflags & (TH_ACK|TH_SYN)) == TH_SYN &&
2180 			    (tp->t_state == TCPS_CLOSED ||
2181 			    (tp->t_state > TCPS_LISTEN && tp->t_port != port)),
2182 			    ("%s: Logging of TCP segment with flags 0x%b and "
2183 			    "UDP encapsulation port %u skipped in state %s",
2184 			    __func__, thflags, PRINT_TH_FLAGS,
2185 			    ntohs(port), tcpstates[tp->t_state]));
2186 		}
2187 	}
2188 
2189 	if (flags & TH_ACK)
2190 		TCPSTAT_INC(tcps_sndacks);
2191 	else if (flags & (TH_SYN|TH_FIN|TH_RST))
2192 		TCPSTAT_INC(tcps_sndctrl);
2193 	TCPSTAT_INC(tcps_sndtotal);
2194 
2195 #ifdef INET6
2196 	if (isipv6) {
2197 		TCP_PROBE5(send, NULL, tp, ip6, tp, nth);
2198 		output_ret = ip6_output(m, NULL, NULL, 0, NULL, NULL, inp);
2199 	}
2200 #endif /* INET6 */
2201 #if defined(INET) && defined(INET6)
2202 	else
2203 #endif
2204 #ifdef INET
2205 	{
2206 		TCP_PROBE5(send, NULL, tp, ip, tp, nth);
2207 		output_ret = ip_output(m, NULL, NULL, 0, NULL, inp);
2208 	}
2209 #endif
2210 	if (lgb != NULL)
2211 		lgb->tlb_errno = output_ret;
2212 }
2213 
2214 /*
2215  * Create a new TCP control block, making an empty reassembly queue and hooking
2216  * it to the argument protocol control block.  The `inp' parameter must have
2217  * come from the zone allocator set up by tcpcbstor declaration.
2218  */
2219 struct tcpcb *
2220 tcp_newtcpcb(struct inpcb *inp)
2221 {
2222 	struct tcpcb *tp = intotcpcb(inp);
2223 #ifdef INET6
2224 	int isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
2225 #endif /* INET6 */
2226 
2227 	/*
2228 	 * Historically allocation was done with M_ZERO.  There is a lot of
2229 	 * code that rely on that.  For now take safe approach and zero whole
2230 	 * tcpcb.  This definitely can be optimized.
2231 	 */
2232 	bzero(&tp->t_start_zero, t_zero_size);
2233 
2234 	/* Initialise cc_var struct for this tcpcb. */
2235 	tp->t_ccv.type = IPPROTO_TCP;
2236 	tp->t_ccv.ccvc.tcp = tp;
2237 	rw_rlock(&tcp_function_lock);
2238 	tp->t_fb = V_tcp_func_set_ptr;
2239 	refcount_acquire(&tp->t_fb->tfb_refcnt);
2240 	rw_runlock(&tcp_function_lock);
2241 	/*
2242 	 * Use the current system default CC algorithm.
2243 	 */
2244 	cc_attach(tp, CC_DEFAULT_ALGO());
2245 
2246 	if (CC_ALGO(tp)->cb_init != NULL)
2247 		if (CC_ALGO(tp)->cb_init(&tp->t_ccv, NULL) > 0) {
2248 			cc_detach(tp);
2249 			if (tp->t_fb->tfb_tcp_fb_fini)
2250 				(*tp->t_fb->tfb_tcp_fb_fini)(tp, 1);
2251 			refcount_release(&tp->t_fb->tfb_refcnt);
2252 			return (NULL);
2253 		}
2254 
2255 #ifdef TCP_HHOOK
2256 	if (khelp_init_osd(HELPER_CLASS_TCP, &tp->t_osd)) {
2257 		if (tp->t_fb->tfb_tcp_fb_fini)
2258 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 1);
2259 		refcount_release(&tp->t_fb->tfb_refcnt);
2260 		return (NULL);
2261 	}
2262 #endif
2263 
2264 	TAILQ_INIT(&tp->t_segq);
2265 	STAILQ_INIT(&tp->t_inqueue);
2266 	tp->t_maxseg =
2267 #ifdef INET6
2268 		isipv6 ? V_tcp_v6mssdflt :
2269 #endif /* INET6 */
2270 		V_tcp_mssdflt;
2271 
2272 	/* All mbuf queue/ack compress flags should be off */
2273 	tcp_lro_features_off(tptoinpcb(tp));
2274 
2275 	callout_init_rw(&tp->t_callout, &inp->inp_lock, CALLOUT_RETURNUNLOCKED);
2276 	for (int i = 0; i < TT_N; i++)
2277 		tp->t_timers[i] = SBT_MAX;
2278 
2279 	switch (V_tcp_do_rfc1323) {
2280 		case 0:
2281 			break;
2282 		default:
2283 		case 1:
2284 			tp->t_flags = (TF_REQ_SCALE|TF_REQ_TSTMP);
2285 			break;
2286 		case 2:
2287 			tp->t_flags = TF_REQ_SCALE;
2288 			break;
2289 		case 3:
2290 			tp->t_flags = TF_REQ_TSTMP;
2291 			break;
2292 	}
2293 	if (V_tcp_do_sack)
2294 		tp->t_flags |= TF_SACK_PERMIT;
2295 	TAILQ_INIT(&tp->snd_holes);
2296 
2297 	/*
2298 	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
2299 	 * rtt estimate.  Set rttvar so that srtt + 4 * rttvar gives
2300 	 * reasonable initial retransmit time.
2301 	 */
2302 	tp->t_srtt = TCPTV_SRTTBASE;
2303 	tp->t_rttvar = ((tcp_rexmit_initial - TCPTV_SRTTBASE) << TCP_RTTVAR_SHIFT) / 4;
2304 	tp->t_rttmin = tcp_rexmit_min;
2305 	tp->t_rxtcur = tcp_rexmit_initial;
2306 	tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
2307 	tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
2308 	tp->t_rcvtime = ticks;
2309 	/* We always start with ticks granularity */
2310 	tp->t_tmr_granularity = TCP_TMR_GRANULARITY_TICKS;
2311 	/*
2312 	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
2313 	 * because the socket may be bound to an IPv6 wildcard address,
2314 	 * which may match an IPv4-mapped IPv6 address.
2315 	 */
2316 	inp->inp_ip_ttl = V_ip_defttl;
2317 #ifdef TCPHPTS
2318 	/*
2319 	 * If using hpts lets drop a random number in so
2320 	 * not all new connections fall on the same CPU.
2321 	 */
2322 	inp->inp_hpts_cpu = hpts_random_cpu(inp);
2323 #endif
2324 #ifdef TCPPCAP
2325 	/*
2326 	 * Init the TCP PCAP queues.
2327 	 */
2328 	tcp_pcap_tcpcb_init(tp);
2329 #endif
2330 #ifdef TCP_BLACKBOX
2331 	/* Initialize the per-TCPCB log data. */
2332 	tcp_log_tcpcbinit(tp);
2333 #endif
2334 	tp->t_pacing_rate = -1;
2335 	if (tp->t_fb->tfb_tcp_fb_init) {
2336 		if ((*tp->t_fb->tfb_tcp_fb_init)(tp, &tp->t_fb_ptr)) {
2337 			refcount_release(&tp->t_fb->tfb_refcnt);
2338 			return (NULL);
2339 		}
2340 	}
2341 #ifdef STATS
2342 	if (V_tcp_perconn_stats_enable == 1)
2343 		tp->t_stats = stats_blob_alloc(V_tcp_perconn_stats_dflt_tpl, 0);
2344 #endif
2345 	if (V_tcp_do_lrd)
2346 		tp->t_flags |= TF_LRD;
2347 
2348 	return (tp);
2349 }
2350 
2351 /*
2352  * Drop a TCP connection, reporting
2353  * the specified error.  If connection is synchronized,
2354  * then send a RST to peer.
2355  */
2356 struct tcpcb *
2357 tcp_drop(struct tcpcb *tp, int errno)
2358 {
2359 	struct socket *so = tptosocket(tp);
2360 
2361 	NET_EPOCH_ASSERT();
2362 	INP_WLOCK_ASSERT(tptoinpcb(tp));
2363 
2364 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
2365 		tcp_state_change(tp, TCPS_CLOSED);
2366 		/* Don't use tcp_output() here due to possible recursion. */
2367 		(void)tcp_output_nodrop(tp);
2368 		TCPSTAT_INC(tcps_drops);
2369 	} else
2370 		TCPSTAT_INC(tcps_conndrops);
2371 	if (errno == ETIMEDOUT && tp->t_softerror)
2372 		errno = tp->t_softerror;
2373 	so->so_error = errno;
2374 	return (tcp_close(tp));
2375 }
2376 
2377 void
2378 tcp_discardcb(struct tcpcb *tp)
2379 {
2380 	struct inpcb *inp = tptoinpcb(tp);
2381 	struct socket *so = tptosocket(tp);
2382 #ifdef INET6
2383 	bool isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
2384 #endif
2385 
2386 	INP_WLOCK_ASSERT(inp);
2387 
2388 	tcp_timer_stop(tp);
2389 	if (tp->t_fb->tfb_tcp_timer_stop_all) {
2390 		tp->t_fb->tfb_tcp_timer_stop_all(tp);
2391 	}
2392 
2393 	/* free the reassembly queue, if any */
2394 	tcp_reass_flush(tp);
2395 
2396 #ifdef TCP_OFFLOAD
2397 	/* Disconnect offload device, if any. */
2398 	if (tp->t_flags & TF_TOE)
2399 		tcp_offload_detach(tp);
2400 #endif
2401 
2402 	tcp_free_sackholes(tp);
2403 
2404 #ifdef TCPPCAP
2405 	/* Free the TCP PCAP queues. */
2406 	tcp_pcap_drain(&(tp->t_inpkts));
2407 	tcp_pcap_drain(&(tp->t_outpkts));
2408 #endif
2409 
2410 	/* Allow the CC algorithm to clean up after itself. */
2411 	if (CC_ALGO(tp)->cb_destroy != NULL)
2412 		CC_ALGO(tp)->cb_destroy(&tp->t_ccv);
2413 	CC_DATA(tp) = NULL;
2414 	/* Detach from the CC algorithm */
2415 	cc_detach(tp);
2416 
2417 #ifdef TCP_HHOOK
2418 	khelp_destroy_osd(&tp->t_osd);
2419 #endif
2420 #ifdef STATS
2421 	stats_blob_destroy(tp->t_stats);
2422 #endif
2423 
2424 	CC_ALGO(tp) = NULL;
2425 
2426 	TCPSTATES_DEC(tp->t_state);
2427 
2428 	if (tp->t_fb->tfb_tcp_fb_fini)
2429 		(*tp->t_fb->tfb_tcp_fb_fini)(tp, 1);
2430 #ifdef TCP_BLACKBOX
2431 	tcp_log_tcpcbfini(tp);
2432 #endif
2433 	MPASS(STAILQ_EMPTY(&tp->t_inqueue));
2434 
2435 	/*
2436 	 * If we got enough samples through the srtt filter,
2437 	 * save the rtt and rttvar in the routing entry.
2438 	 * 'Enough' is arbitrarily defined as 4 rtt samples.
2439 	 * 4 samples is enough for the srtt filter to converge
2440 	 * to within enough % of the correct value; fewer samples
2441 	 * and we could save a bogus rtt. The danger is not high
2442 	 * as tcp quickly recovers from everything.
2443 	 * XXX: Works very well but needs some more statistics!
2444 	 *
2445 	 * XXXRRS: Updating must be after the stack fini() since
2446 	 * that may be converting some internal representation of
2447 	 * say srtt etc into the general one used by other stacks.
2448 	 * Lets also at least protect against the so being NULL
2449 	 * as RW stated below.
2450 	 */
2451 	if ((tp->t_rttupdated >= 4) && (so != NULL)) {
2452 		struct hc_metrics_lite metrics;
2453 		uint32_t ssthresh;
2454 
2455 		bzero(&metrics, sizeof(metrics));
2456 		/*
2457 		 * Update the ssthresh always when the conditions below
2458 		 * are satisfied. This gives us better new start value
2459 		 * for the congestion avoidance for new connections.
2460 		 * ssthresh is only set if packet loss occurred on a session.
2461 		 *
2462 		 * XXXRW: 'so' may be NULL here, and/or socket buffer may be
2463 		 * being torn down.  Ideally this code would not use 'so'.
2464 		 */
2465 		ssthresh = tp->snd_ssthresh;
2466 		if (ssthresh != 0 && ssthresh < so->so_snd.sb_hiwat / 2) {
2467 			/*
2468 			 * convert the limit from user data bytes to
2469 			 * packets then to packet data bytes.
2470 			 */
2471 			ssthresh = (ssthresh + tp->t_maxseg / 2) / tp->t_maxseg;
2472 			if (ssthresh < 2)
2473 				ssthresh = 2;
2474 			ssthresh *= (tp->t_maxseg +
2475 #ifdef INET6
2476 			    (isipv6 ? sizeof (struct ip6_hdr) +
2477 			    sizeof (struct tcphdr) :
2478 #endif
2479 			    sizeof (struct tcpiphdr)
2480 #ifdef INET6
2481 			    )
2482 #endif
2483 			    );
2484 		} else
2485 			ssthresh = 0;
2486 		metrics.rmx_ssthresh = ssthresh;
2487 
2488 		metrics.rmx_rtt = tp->t_srtt;
2489 		metrics.rmx_rttvar = tp->t_rttvar;
2490 		metrics.rmx_cwnd = tp->snd_cwnd;
2491 		metrics.rmx_sendpipe = 0;
2492 		metrics.rmx_recvpipe = 0;
2493 
2494 		tcp_hc_update(&inp->inp_inc, &metrics);
2495 	}
2496 
2497 	refcount_release(&tp->t_fb->tfb_refcnt);
2498 }
2499 
2500 /*
2501  * Attempt to close a TCP control block, marking it as dropped, and freeing
2502  * the socket if we hold the only reference.
2503  */
2504 struct tcpcb *
2505 tcp_close(struct tcpcb *tp)
2506 {
2507 	struct inpcb *inp = tptoinpcb(tp);
2508 	struct socket *so = tptosocket(tp);
2509 
2510 	INP_WLOCK_ASSERT(inp);
2511 
2512 #ifdef TCP_OFFLOAD
2513 	if (tp->t_state == TCPS_LISTEN)
2514 		tcp_offload_listen_stop(tp);
2515 #endif
2516 	/*
2517 	 * This releases the TFO pending counter resource for TFO listen
2518 	 * sockets as well as passively-created TFO sockets that transition
2519 	 * from SYN_RECEIVED to CLOSED.
2520 	 */
2521 	if (tp->t_tfo_pending) {
2522 		tcp_fastopen_decrement_counter(tp->t_tfo_pending);
2523 		tp->t_tfo_pending = NULL;
2524 	}
2525 #ifdef TCPHPTS
2526 	tcp_hpts_remove(inp);
2527 #endif
2528 	in_pcbdrop(inp);
2529 	TCPSTAT_INC(tcps_closed);
2530 	if (tp->t_state != TCPS_CLOSED)
2531 		tcp_state_change(tp, TCPS_CLOSED);
2532 	KASSERT(inp->inp_socket != NULL, ("tcp_close: inp_socket NULL"));
2533 	soisdisconnected(so);
2534 	if (inp->inp_flags & INP_SOCKREF) {
2535 		inp->inp_flags &= ~INP_SOCKREF;
2536 		INP_WUNLOCK(inp);
2537 		sorele(so);
2538 		return (NULL);
2539 	}
2540 	return (tp);
2541 }
2542 
2543 /*
2544  * Notify a tcp user of an asynchronous error;
2545  * store error as soft error, but wake up user
2546  * (for now, won't do anything until can select for soft error).
2547  *
2548  * Do not wake up user since there currently is no mechanism for
2549  * reporting soft errors (yet - a kqueue filter may be added).
2550  */
2551 static struct inpcb *
2552 tcp_notify(struct inpcb *inp, int error)
2553 {
2554 	struct tcpcb *tp;
2555 
2556 	INP_WLOCK_ASSERT(inp);
2557 
2558 	tp = intotcpcb(inp);
2559 	KASSERT(tp != NULL, ("tcp_notify: tp == NULL"));
2560 
2561 	/*
2562 	 * Ignore some errors if we are hooked up.
2563 	 * If connection hasn't completed, has retransmitted several times,
2564 	 * and receives a second error, give up now.  This is better
2565 	 * than waiting a long time to establish a connection that
2566 	 * can never complete.
2567 	 */
2568 	if (tp->t_state == TCPS_ESTABLISHED &&
2569 	    (error == EHOSTUNREACH || error == ENETUNREACH ||
2570 	     error == EHOSTDOWN)) {
2571 		if (inp->inp_route.ro_nh) {
2572 			NH_FREE(inp->inp_route.ro_nh);
2573 			inp->inp_route.ro_nh = (struct nhop_object *)NULL;
2574 		}
2575 		return (inp);
2576 	} else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 &&
2577 	    tp->t_softerror) {
2578 		tp = tcp_drop(tp, error);
2579 		if (tp != NULL)
2580 			return (inp);
2581 		else
2582 			return (NULL);
2583 	} else {
2584 		tp->t_softerror = error;
2585 		return (inp);
2586 	}
2587 #if 0
2588 	wakeup( &so->so_timeo);
2589 	sorwakeup(so);
2590 	sowwakeup(so);
2591 #endif
2592 }
2593 
2594 static int
2595 tcp_pcblist(SYSCTL_HANDLER_ARGS)
2596 {
2597 	struct inpcb_iterator inpi = INP_ALL_ITERATOR(&V_tcbinfo,
2598 	    INPLOOKUP_RLOCKPCB);
2599 	struct xinpgen xig;
2600 	struct inpcb *inp;
2601 	int error;
2602 
2603 	if (req->newptr != NULL)
2604 		return (EPERM);
2605 
2606 	if (req->oldptr == NULL) {
2607 		int n;
2608 
2609 		n = V_tcbinfo.ipi_count +
2610 		    counter_u64_fetch(V_tcps_states[TCPS_SYN_RECEIVED]);
2611 		n += imax(n / 8, 10);
2612 		req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xtcpcb);
2613 		return (0);
2614 	}
2615 
2616 	if ((error = sysctl_wire_old_buffer(req, 0)) != 0)
2617 		return (error);
2618 
2619 	bzero(&xig, sizeof(xig));
2620 	xig.xig_len = sizeof xig;
2621 	xig.xig_count = V_tcbinfo.ipi_count +
2622 	    counter_u64_fetch(V_tcps_states[TCPS_SYN_RECEIVED]);
2623 	xig.xig_gen = V_tcbinfo.ipi_gencnt;
2624 	xig.xig_sogen = so_gencnt;
2625 	error = SYSCTL_OUT(req, &xig, sizeof xig);
2626 	if (error)
2627 		return (error);
2628 
2629 	error = syncache_pcblist(req);
2630 	if (error)
2631 		return (error);
2632 
2633 	while ((inp = inp_next(&inpi)) != NULL) {
2634 		if (inp->inp_gencnt <= xig.xig_gen &&
2635 		    cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
2636 			struct xtcpcb xt;
2637 
2638 			tcp_inptoxtp(inp, &xt);
2639 			error = SYSCTL_OUT(req, &xt, sizeof xt);
2640 			if (error) {
2641 				INP_RUNLOCK(inp);
2642 				break;
2643 			} else
2644 				continue;
2645 		}
2646 	}
2647 
2648 	if (!error) {
2649 		/*
2650 		 * Give the user an updated idea of our state.
2651 		 * If the generation differs from what we told
2652 		 * her before, she knows that something happened
2653 		 * while we were processing this request, and it
2654 		 * might be necessary to retry.
2655 		 */
2656 		xig.xig_gen = V_tcbinfo.ipi_gencnt;
2657 		xig.xig_sogen = so_gencnt;
2658 		xig.xig_count = V_tcbinfo.ipi_count +
2659 		    counter_u64_fetch(V_tcps_states[TCPS_SYN_RECEIVED]);
2660 		error = SYSCTL_OUT(req, &xig, sizeof xig);
2661 	}
2662 
2663 	return (error);
2664 }
2665 
2666 SYSCTL_PROC(_net_inet_tcp, TCPCTL_PCBLIST, pcblist,
2667     CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
2668     NULL, 0, tcp_pcblist, "S,xtcpcb",
2669     "List of active TCP connections");
2670 
2671 #ifdef INET
2672 static int
2673 tcp_getcred(SYSCTL_HANDLER_ARGS)
2674 {
2675 	struct xucred xuc;
2676 	struct sockaddr_in addrs[2];
2677 	struct epoch_tracker et;
2678 	struct inpcb *inp;
2679 	int error;
2680 
2681 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
2682 	if (error)
2683 		return (error);
2684 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
2685 	if (error)
2686 		return (error);
2687 	NET_EPOCH_ENTER(et);
2688 	inp = in_pcblookup(&V_tcbinfo, addrs[1].sin_addr, addrs[1].sin_port,
2689 	    addrs[0].sin_addr, addrs[0].sin_port, INPLOOKUP_RLOCKPCB, NULL);
2690 	NET_EPOCH_EXIT(et);
2691 	if (inp != NULL) {
2692 		if (error == 0)
2693 			error = cr_canseeinpcb(req->td->td_ucred, inp);
2694 		if (error == 0)
2695 			cru2x(inp->inp_cred, &xuc);
2696 		INP_RUNLOCK(inp);
2697 	} else
2698 		error = ENOENT;
2699 	if (error == 0)
2700 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
2701 	return (error);
2702 }
2703 
2704 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, getcred,
2705     CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_NEEDGIANT,
2706     0, 0, tcp_getcred, "S,xucred",
2707     "Get the xucred of a TCP connection");
2708 #endif /* INET */
2709 
2710 #ifdef INET6
2711 static int
2712 tcp6_getcred(SYSCTL_HANDLER_ARGS)
2713 {
2714 	struct epoch_tracker et;
2715 	struct xucred xuc;
2716 	struct sockaddr_in6 addrs[2];
2717 	struct inpcb *inp;
2718 	int error;
2719 #ifdef INET
2720 	int mapped = 0;
2721 #endif
2722 
2723 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
2724 	if (error)
2725 		return (error);
2726 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
2727 	if (error)
2728 		return (error);
2729 	if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
2730 	    (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
2731 		return (error);
2732 	}
2733 	if (IN6_IS_ADDR_V4MAPPED(&addrs[0].sin6_addr)) {
2734 #ifdef INET
2735 		if (IN6_IS_ADDR_V4MAPPED(&addrs[1].sin6_addr))
2736 			mapped = 1;
2737 		else
2738 #endif
2739 			return (EINVAL);
2740 	}
2741 
2742 	NET_EPOCH_ENTER(et);
2743 #ifdef INET
2744 	if (mapped == 1)
2745 		inp = in_pcblookup(&V_tcbinfo,
2746 			*(struct in_addr *)&addrs[1].sin6_addr.s6_addr[12],
2747 			addrs[1].sin6_port,
2748 			*(struct in_addr *)&addrs[0].sin6_addr.s6_addr[12],
2749 			addrs[0].sin6_port, INPLOOKUP_RLOCKPCB, NULL);
2750 	else
2751 #endif
2752 		inp = in6_pcblookup(&V_tcbinfo,
2753 			&addrs[1].sin6_addr, addrs[1].sin6_port,
2754 			&addrs[0].sin6_addr, addrs[0].sin6_port,
2755 			INPLOOKUP_RLOCKPCB, NULL);
2756 	NET_EPOCH_EXIT(et);
2757 	if (inp != NULL) {
2758 		if (error == 0)
2759 			error = cr_canseeinpcb(req->td->td_ucred, inp);
2760 		if (error == 0)
2761 			cru2x(inp->inp_cred, &xuc);
2762 		INP_RUNLOCK(inp);
2763 	} else
2764 		error = ENOENT;
2765 	if (error == 0)
2766 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
2767 	return (error);
2768 }
2769 
2770 SYSCTL_PROC(_net_inet6_tcp6, OID_AUTO, getcred,
2771     CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_NEEDGIANT,
2772     0, 0, tcp6_getcred, "S,xucred",
2773     "Get the xucred of a TCP6 connection");
2774 #endif /* INET6 */
2775 
2776 #ifdef INET
2777 /* Path MTU to try next when a fragmentation-needed message is received. */
2778 static inline int
2779 tcp_next_pmtu(const struct icmp *icp, const struct ip *ip)
2780 {
2781 	int mtu = ntohs(icp->icmp_nextmtu);
2782 
2783 	/* If no alternative MTU was proposed, try the next smaller one. */
2784 	if (!mtu)
2785 		mtu = ip_next_mtu(ntohs(ip->ip_len), 1);
2786 	if (mtu < V_tcp_minmss + sizeof(struct tcpiphdr))
2787 		mtu = V_tcp_minmss + sizeof(struct tcpiphdr);
2788 
2789 	return (mtu);
2790 }
2791 
2792 static void
2793 tcp_ctlinput_with_port(struct icmp *icp, uint16_t port)
2794 {
2795 	struct ip *ip;
2796 	struct tcphdr *th;
2797 	struct inpcb *inp;
2798 	struct tcpcb *tp;
2799 	struct inpcb *(*notify)(struct inpcb *, int);
2800 	struct in_conninfo inc;
2801 	tcp_seq icmp_tcp_seq;
2802 	int errno, mtu;
2803 
2804 	errno = icmp_errmap(icp);
2805 	switch (errno) {
2806 	case 0:
2807 		return;
2808 	case EMSGSIZE:
2809 		notify = tcp_mtudisc_notify;
2810 		break;
2811 	case ECONNREFUSED:
2812 		if (V_icmp_may_rst)
2813 			notify = tcp_drop_syn_sent;
2814 		else
2815 			notify = tcp_notify;
2816 		break;
2817 	case EHOSTUNREACH:
2818 		if (V_icmp_may_rst && icp->icmp_type == ICMP_TIMXCEED)
2819 			notify = tcp_drop_syn_sent;
2820 		else
2821 			notify = tcp_notify;
2822 		break;
2823 	default:
2824 		notify = tcp_notify;
2825 	}
2826 
2827 	ip = &icp->icmp_ip;
2828 	th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
2829 	icmp_tcp_seq = th->th_seq;
2830 	inp = in_pcblookup(&V_tcbinfo, ip->ip_dst, th->th_dport, ip->ip_src,
2831 	    th->th_sport, INPLOOKUP_WLOCKPCB, NULL);
2832 	if (inp != NULL)  {
2833 		tp = intotcpcb(inp);
2834 #ifdef TCP_OFFLOAD
2835 		if (tp->t_flags & TF_TOE && errno == EMSGSIZE) {
2836 			/*
2837 			 * MTU discovery for offloaded connections.  Let
2838 			 * the TOE driver verify seq# and process it.
2839 			 */
2840 			mtu = tcp_next_pmtu(icp, ip);
2841 			tcp_offload_pmtu_update(tp, icmp_tcp_seq, mtu);
2842 			goto out;
2843 		}
2844 #endif
2845 		if (tp->t_port != port)
2846 			goto out;
2847 		if (SEQ_GEQ(ntohl(icmp_tcp_seq), tp->snd_una) &&
2848 		    SEQ_LT(ntohl(icmp_tcp_seq), tp->snd_max)) {
2849 			if (errno == EMSGSIZE) {
2850 				/*
2851 				 * MTU discovery: we got a needfrag and
2852 				 * will potentially try a lower MTU.
2853 				 */
2854 				mtu = tcp_next_pmtu(icp, ip);
2855 
2856 				/*
2857 				 * Only process the offered MTU if it
2858 				 * is smaller than the current one.
2859 				 */
2860 				if (mtu < tp->t_maxseg +
2861 				    sizeof(struct tcpiphdr)) {
2862 					bzero(&inc, sizeof(inc));
2863 					inc.inc_faddr = ip->ip_dst;
2864 					inc.inc_fibnum =
2865 					    inp->inp_inc.inc_fibnum;
2866 					tcp_hc_updatemtu(&inc, mtu);
2867 					inp = tcp_mtudisc(inp, mtu);
2868 				}
2869 			} else
2870 				inp = (*notify)(inp, errno);
2871 		}
2872 	} else {
2873 		bzero(&inc, sizeof(inc));
2874 		inc.inc_fport = th->th_dport;
2875 		inc.inc_lport = th->th_sport;
2876 		inc.inc_faddr = ip->ip_dst;
2877 		inc.inc_laddr = ip->ip_src;
2878 		syncache_unreach(&inc, icmp_tcp_seq, port);
2879 	}
2880 out:
2881 	if (inp != NULL)
2882 		INP_WUNLOCK(inp);
2883 }
2884 
2885 static void
2886 tcp_ctlinput(struct icmp *icmp)
2887 {
2888 	tcp_ctlinput_with_port(icmp, htons(0));
2889 }
2890 
2891 static void
2892 tcp_ctlinput_viaudp(udp_tun_icmp_param_t param)
2893 {
2894 	/* Its a tunneled TCP over UDP icmp */
2895 	struct icmp *icmp = param.icmp;
2896 	struct ip *outer_ip, *inner_ip;
2897 	struct udphdr *udp;
2898 	struct tcphdr *th, ttemp;
2899 	int i_hlen, o_len;
2900 	uint16_t port;
2901 
2902 	outer_ip = (struct ip *)((caddr_t)icmp - sizeof(struct ip));
2903 	inner_ip = &icmp->icmp_ip;
2904 	i_hlen = inner_ip->ip_hl << 2;
2905 	o_len = ntohs(outer_ip->ip_len);
2906 	if (o_len <
2907 	    (sizeof(struct ip) + 8 + i_hlen + sizeof(struct udphdr) + offsetof(struct tcphdr, th_ack))) {
2908 		/* Not enough data present */
2909 		return;
2910 	}
2911 	/* Ok lets strip out the inner udphdr header by copying up on top of it the tcp hdr */
2912 	udp = (struct udphdr *)(((caddr_t)inner_ip) + i_hlen);
2913 	if (ntohs(udp->uh_sport) != V_tcp_udp_tunneling_port) {
2914 		return;
2915 	}
2916 	port = udp->uh_dport;
2917 	th = (struct tcphdr *)(udp + 1);
2918 	memcpy(&ttemp, th, sizeof(struct tcphdr));
2919 	memcpy(udp, &ttemp, sizeof(struct tcphdr));
2920 	/* Now adjust down the size of the outer IP header */
2921 	o_len -= sizeof(struct udphdr);
2922 	outer_ip->ip_len = htons(o_len);
2923 	/* Now call in to the normal handling code */
2924 	tcp_ctlinput_with_port(icmp, port);
2925 }
2926 #endif /* INET */
2927 
2928 #ifdef INET6
2929 static inline int
2930 tcp6_next_pmtu(const struct icmp6_hdr *icmp6)
2931 {
2932 	int mtu = ntohl(icmp6->icmp6_mtu);
2933 
2934 	/*
2935 	 * If no alternative MTU was proposed, or the proposed MTU was too
2936 	 * small, set to the min.
2937 	 */
2938 	if (mtu < IPV6_MMTU)
2939 		mtu = IPV6_MMTU - 8;	/* XXXNP: what is the adjustment for? */
2940 	return (mtu);
2941 }
2942 
2943 static void
2944 tcp6_ctlinput_with_port(struct ip6ctlparam *ip6cp, uint16_t port)
2945 {
2946 	struct in6_addr *dst;
2947 	struct inpcb *(*notify)(struct inpcb *, int);
2948 	struct ip6_hdr *ip6;
2949 	struct mbuf *m;
2950 	struct inpcb *inp;
2951 	struct tcpcb *tp;
2952 	struct icmp6_hdr *icmp6;
2953 	struct in_conninfo inc;
2954 	struct tcp_ports {
2955 		uint16_t th_sport;
2956 		uint16_t th_dport;
2957 	} t_ports;
2958 	tcp_seq icmp_tcp_seq;
2959 	unsigned int mtu;
2960 	unsigned int off;
2961 	int errno;
2962 
2963 	icmp6 = ip6cp->ip6c_icmp6;
2964 	m = ip6cp->ip6c_m;
2965 	ip6 = ip6cp->ip6c_ip6;
2966 	off = ip6cp->ip6c_off;
2967 	dst = &ip6cp->ip6c_finaldst->sin6_addr;
2968 
2969 	errno = icmp6_errmap(icmp6);
2970 	switch (errno) {
2971 	case 0:
2972 		return;
2973 	case EMSGSIZE:
2974 		notify = tcp_mtudisc_notify;
2975 		break;
2976 	case ECONNREFUSED:
2977 		if (V_icmp_may_rst)
2978 			notify = tcp_drop_syn_sent;
2979 		else
2980 			notify = tcp_notify;
2981 		break;
2982 	case EHOSTUNREACH:
2983 		/*
2984 		 * There are only four ICMPs that may reset connection:
2985 		 * - administratively prohibited
2986 		 * - port unreachable
2987 		 * - time exceeded in transit
2988 		 * - unknown next header
2989 		 */
2990 		if (V_icmp_may_rst &&
2991 		    ((icmp6->icmp6_type == ICMP6_DST_UNREACH &&
2992 		     (icmp6->icmp6_code == ICMP6_DST_UNREACH_ADMIN ||
2993 		      icmp6->icmp6_code == ICMP6_DST_UNREACH_NOPORT)) ||
2994 		    (icmp6->icmp6_type == ICMP6_TIME_EXCEEDED &&
2995 		      icmp6->icmp6_code == ICMP6_TIME_EXCEED_TRANSIT) ||
2996 		    (icmp6->icmp6_type == ICMP6_PARAM_PROB &&
2997 		      icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER)))
2998 			notify = tcp_drop_syn_sent;
2999 		else
3000 			notify = tcp_notify;
3001 		break;
3002 	default:
3003 		notify = tcp_notify;
3004 	}
3005 
3006 	/* Check if we can safely get the ports from the tcp hdr */
3007 	if (m == NULL ||
3008 	    (m->m_pkthdr.len <
3009 		(int32_t) (off + sizeof(struct tcp_ports)))) {
3010 		return;
3011 	}
3012 	bzero(&t_ports, sizeof(struct tcp_ports));
3013 	m_copydata(m, off, sizeof(struct tcp_ports), (caddr_t)&t_ports);
3014 	inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_dst, t_ports.th_dport,
3015 	    &ip6->ip6_src, t_ports.th_sport, INPLOOKUP_WLOCKPCB, NULL);
3016 	off += sizeof(struct tcp_ports);
3017 	if (m->m_pkthdr.len < (int32_t) (off + sizeof(tcp_seq))) {
3018 		goto out;
3019 	}
3020 	m_copydata(m, off, sizeof(tcp_seq), (caddr_t)&icmp_tcp_seq);
3021 	if (inp != NULL)  {
3022 		tp = intotcpcb(inp);
3023 #ifdef TCP_OFFLOAD
3024 		if (tp->t_flags & TF_TOE && errno == EMSGSIZE) {
3025 			/* MTU discovery for offloaded connections. */
3026 			mtu = tcp6_next_pmtu(icmp6);
3027 			tcp_offload_pmtu_update(tp, icmp_tcp_seq, mtu);
3028 			goto out;
3029 		}
3030 #endif
3031 		if (tp->t_port != port)
3032 			goto out;
3033 		if (SEQ_GEQ(ntohl(icmp_tcp_seq), tp->snd_una) &&
3034 		    SEQ_LT(ntohl(icmp_tcp_seq), tp->snd_max)) {
3035 			if (errno == EMSGSIZE) {
3036 				/*
3037 				 * MTU discovery:
3038 				 * If we got a needfrag set the MTU
3039 				 * in the route to the suggested new
3040 				 * value (if given) and then notify.
3041 				 */
3042 				mtu = tcp6_next_pmtu(icmp6);
3043 
3044 				bzero(&inc, sizeof(inc));
3045 				inc.inc_fibnum = M_GETFIB(m);
3046 				inc.inc_flags |= INC_ISIPV6;
3047 				inc.inc6_faddr = *dst;
3048 				if (in6_setscope(&inc.inc6_faddr,
3049 					m->m_pkthdr.rcvif, NULL))
3050 					goto out;
3051 				/*
3052 				 * Only process the offered MTU if it
3053 				 * is smaller than the current one.
3054 				 */
3055 				if (mtu < tp->t_maxseg +
3056 				    sizeof (struct tcphdr) +
3057 				    sizeof (struct ip6_hdr)) {
3058 					tcp_hc_updatemtu(&inc, mtu);
3059 					tcp_mtudisc(inp, mtu);
3060 					ICMP6STAT_INC(icp6s_pmtuchg);
3061 				}
3062 			} else
3063 				inp = (*notify)(inp, errno);
3064 		}
3065 	} else {
3066 		bzero(&inc, sizeof(inc));
3067 		inc.inc_fibnum = M_GETFIB(m);
3068 		inc.inc_flags |= INC_ISIPV6;
3069 		inc.inc_fport = t_ports.th_dport;
3070 		inc.inc_lport = t_ports.th_sport;
3071 		inc.inc6_faddr = *dst;
3072 		inc.inc6_laddr = ip6->ip6_src;
3073 		syncache_unreach(&inc, icmp_tcp_seq, port);
3074 	}
3075 out:
3076 	if (inp != NULL)
3077 		INP_WUNLOCK(inp);
3078 }
3079 
3080 static void
3081 tcp6_ctlinput(struct ip6ctlparam *ctl)
3082 {
3083 	tcp6_ctlinput_with_port(ctl, htons(0));
3084 }
3085 
3086 static void
3087 tcp6_ctlinput_viaudp(udp_tun_icmp_param_t param)
3088 {
3089 	struct ip6ctlparam *ip6cp = param.ip6cp;
3090 	struct mbuf *m;
3091 	struct udphdr *udp;
3092 	uint16_t port;
3093 
3094 	m = m_pulldown(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(struct udphdr), NULL);
3095 	if (m == NULL) {
3096 		return;
3097 	}
3098 	udp = mtod(m, struct udphdr *);
3099 	if (ntohs(udp->uh_sport) != V_tcp_udp_tunneling_port) {
3100 		return;
3101 	}
3102 	port = udp->uh_dport;
3103 	m_adj(m, sizeof(struct udphdr));
3104 	if ((m->m_flags & M_PKTHDR) == 0) {
3105 		ip6cp->ip6c_m->m_pkthdr.len -= sizeof(struct udphdr);
3106 	}
3107 	/* Now call in to the normal handling code */
3108 	tcp6_ctlinput_with_port(ip6cp, port);
3109 }
3110 
3111 #endif /* INET6 */
3112 
3113 static uint32_t
3114 tcp_keyed_hash(struct in_conninfo *inc, u_char *key, u_int len)
3115 {
3116 	SIPHASH_CTX ctx;
3117 	uint32_t hash[2];
3118 
3119 	KASSERT(len >= SIPHASH_KEY_LENGTH,
3120 	    ("%s: keylen %u too short ", __func__, len));
3121 	SipHash24_Init(&ctx);
3122 	SipHash_SetKey(&ctx, (uint8_t *)key);
3123 	SipHash_Update(&ctx, &inc->inc_fport, sizeof(uint16_t));
3124 	SipHash_Update(&ctx, &inc->inc_lport, sizeof(uint16_t));
3125 	switch (inc->inc_flags & INC_ISIPV6) {
3126 #ifdef INET
3127 	case 0:
3128 		SipHash_Update(&ctx, &inc->inc_faddr, sizeof(struct in_addr));
3129 		SipHash_Update(&ctx, &inc->inc_laddr, sizeof(struct in_addr));
3130 		break;
3131 #endif
3132 #ifdef INET6
3133 	case INC_ISIPV6:
3134 		SipHash_Update(&ctx, &inc->inc6_faddr, sizeof(struct in6_addr));
3135 		SipHash_Update(&ctx, &inc->inc6_laddr, sizeof(struct in6_addr));
3136 		break;
3137 #endif
3138 	}
3139 	SipHash_Final((uint8_t *)hash, &ctx);
3140 
3141 	return (hash[0] ^ hash[1]);
3142 }
3143 
3144 uint32_t
3145 tcp_new_ts_offset(struct in_conninfo *inc)
3146 {
3147 	struct in_conninfo inc_store, *local_inc;
3148 
3149 	if (!V_tcp_ts_offset_per_conn) {
3150 		memcpy(&inc_store, inc, sizeof(struct in_conninfo));
3151 		inc_store.inc_lport = 0;
3152 		inc_store.inc_fport = 0;
3153 		local_inc = &inc_store;
3154 	} else {
3155 		local_inc = inc;
3156 	}
3157 	return (tcp_keyed_hash(local_inc, V_ts_offset_secret,
3158 	    sizeof(V_ts_offset_secret)));
3159 }
3160 
3161 /*
3162  * Following is where TCP initial sequence number generation occurs.
3163  *
3164  * There are two places where we must use initial sequence numbers:
3165  * 1.  In SYN-ACK packets.
3166  * 2.  In SYN packets.
3167  *
3168  * All ISNs for SYN-ACK packets are generated by the syncache.  See
3169  * tcp_syncache.c for details.
3170  *
3171  * The ISNs in SYN packets must be monotonic; TIME_WAIT recycling
3172  * depends on this property.  In addition, these ISNs should be
3173  * unguessable so as to prevent connection hijacking.  To satisfy
3174  * the requirements of this situation, the algorithm outlined in
3175  * RFC 1948 is used, with only small modifications.
3176  *
3177  * Implementation details:
3178  *
3179  * Time is based off the system timer, and is corrected so that it
3180  * increases by one megabyte per second.  This allows for proper
3181  * recycling on high speed LANs while still leaving over an hour
3182  * before rollover.
3183  *
3184  * As reading the *exact* system time is too expensive to be done
3185  * whenever setting up a TCP connection, we increment the time
3186  * offset in two ways.  First, a small random positive increment
3187  * is added to isn_offset for each connection that is set up.
3188  * Second, the function tcp_isn_tick fires once per clock tick
3189  * and increments isn_offset as necessary so that sequence numbers
3190  * are incremented at approximately ISN_BYTES_PER_SECOND.  The
3191  * random positive increments serve only to ensure that the same
3192  * exact sequence number is never sent out twice (as could otherwise
3193  * happen when a port is recycled in less than the system tick
3194  * interval.)
3195  *
3196  * net.inet.tcp.isn_reseed_interval controls the number of seconds
3197  * between seeding of isn_secret.  This is normally set to zero,
3198  * as reseeding should not be necessary.
3199  *
3200  * Locking of the global variables isn_secret, isn_last_reseed, isn_offset,
3201  * isn_offset_old, and isn_ctx is performed using the ISN lock.  In
3202  * general, this means holding an exclusive (write) lock.
3203  */
3204 
3205 #define ISN_BYTES_PER_SECOND 1048576
3206 #define ISN_STATIC_INCREMENT 4096
3207 #define ISN_RANDOM_INCREMENT (4096 - 1)
3208 #define ISN_SECRET_LENGTH    SIPHASH_KEY_LENGTH
3209 
3210 VNET_DEFINE_STATIC(u_char, isn_secret[ISN_SECRET_LENGTH]);
3211 VNET_DEFINE_STATIC(int, isn_last);
3212 VNET_DEFINE_STATIC(int, isn_last_reseed);
3213 VNET_DEFINE_STATIC(u_int32_t, isn_offset);
3214 VNET_DEFINE_STATIC(u_int32_t, isn_offset_old);
3215 
3216 #define	V_isn_secret			VNET(isn_secret)
3217 #define	V_isn_last			VNET(isn_last)
3218 #define	V_isn_last_reseed		VNET(isn_last_reseed)
3219 #define	V_isn_offset			VNET(isn_offset)
3220 #define	V_isn_offset_old		VNET(isn_offset_old)
3221 
3222 tcp_seq
3223 tcp_new_isn(struct in_conninfo *inc)
3224 {
3225 	tcp_seq new_isn;
3226 	u_int32_t projected_offset;
3227 
3228 	ISN_LOCK();
3229 	/* Seed if this is the first use, reseed if requested. */
3230 	if ((V_isn_last_reseed == 0) || ((V_tcp_isn_reseed_interval > 0) &&
3231 	     (((u_int)V_isn_last_reseed + (u_int)V_tcp_isn_reseed_interval*hz)
3232 		< (u_int)ticks))) {
3233 		arc4rand(&V_isn_secret, sizeof(V_isn_secret), 0);
3234 		V_isn_last_reseed = ticks;
3235 	}
3236 
3237 	/* Compute the hash and return the ISN. */
3238 	new_isn = (tcp_seq)tcp_keyed_hash(inc, V_isn_secret,
3239 	    sizeof(V_isn_secret));
3240 	V_isn_offset += ISN_STATIC_INCREMENT +
3241 		(arc4random() & ISN_RANDOM_INCREMENT);
3242 	if (ticks != V_isn_last) {
3243 		projected_offset = V_isn_offset_old +
3244 		    ISN_BYTES_PER_SECOND / hz * (ticks - V_isn_last);
3245 		if (SEQ_GT(projected_offset, V_isn_offset))
3246 			V_isn_offset = projected_offset;
3247 		V_isn_offset_old = V_isn_offset;
3248 		V_isn_last = ticks;
3249 	}
3250 	new_isn += V_isn_offset;
3251 	ISN_UNLOCK();
3252 	return (new_isn);
3253 }
3254 
3255 /*
3256  * When a specific ICMP unreachable message is received and the
3257  * connection state is SYN-SENT, drop the connection.  This behavior
3258  * is controlled by the icmp_may_rst sysctl.
3259  */
3260 static struct inpcb *
3261 tcp_drop_syn_sent(struct inpcb *inp, int errno)
3262 {
3263 	struct tcpcb *tp;
3264 
3265 	NET_EPOCH_ASSERT();
3266 	INP_WLOCK_ASSERT(inp);
3267 
3268 	tp = intotcpcb(inp);
3269 	if (tp->t_state != TCPS_SYN_SENT)
3270 		return (inp);
3271 
3272 	if (IS_FASTOPEN(tp->t_flags))
3273 		tcp_fastopen_disable_path(tp);
3274 
3275 	tp = tcp_drop(tp, errno);
3276 	if (tp != NULL)
3277 		return (inp);
3278 	else
3279 		return (NULL);
3280 }
3281 
3282 /*
3283  * When `need fragmentation' ICMP is received, update our idea of the MSS
3284  * based on the new value. Also nudge TCP to send something, since we
3285  * know the packet we just sent was dropped.
3286  * This duplicates some code in the tcp_mss() function in tcp_input.c.
3287  */
3288 static struct inpcb *
3289 tcp_mtudisc_notify(struct inpcb *inp, int error)
3290 {
3291 
3292 	return (tcp_mtudisc(inp, -1));
3293 }
3294 
3295 static struct inpcb *
3296 tcp_mtudisc(struct inpcb *inp, int mtuoffer)
3297 {
3298 	struct tcpcb *tp;
3299 	struct socket *so;
3300 
3301 	INP_WLOCK_ASSERT(inp);
3302 
3303 	tp = intotcpcb(inp);
3304 	KASSERT(tp != NULL, ("tcp_mtudisc: tp == NULL"));
3305 
3306 	tcp_mss_update(tp, -1, mtuoffer, NULL, NULL);
3307 
3308 	so = inp->inp_socket;
3309 	SOCKBUF_LOCK(&so->so_snd);
3310 	/* If the mss is larger than the socket buffer, decrease the mss. */
3311 	if (so->so_snd.sb_hiwat < tp->t_maxseg)
3312 		tp->t_maxseg = so->so_snd.sb_hiwat;
3313 	SOCKBUF_UNLOCK(&so->so_snd);
3314 
3315 	TCPSTAT_INC(tcps_mturesent);
3316 	tp->t_rtttime = 0;
3317 	tp->snd_nxt = tp->snd_una;
3318 	tcp_free_sackholes(tp);
3319 	tp->snd_recover = tp->snd_max;
3320 	if (tp->t_flags & TF_SACK_PERMIT)
3321 		EXIT_FASTRECOVERY(tp->t_flags);
3322 	if (tp->t_fb->tfb_tcp_mtu_chg != NULL) {
3323 		/*
3324 		 * Conceptually the snd_nxt setting
3325 		 * and freeing sack holes should
3326 		 * be done by the default stacks
3327 		 * own tfb_tcp_mtu_chg().
3328 		 */
3329 		tp->t_fb->tfb_tcp_mtu_chg(tp);
3330 	}
3331 	if (tcp_output(tp) < 0)
3332 		return (NULL);
3333 	else
3334 		return (inp);
3335 }
3336 
3337 #ifdef INET
3338 /*
3339  * Look-up the routing entry to the peer of this inpcb.  If no route
3340  * is found and it cannot be allocated, then return 0.  This routine
3341  * is called by TCP routines that access the rmx structure and by
3342  * tcp_mss_update to get the peer/interface MTU.
3343  */
3344 uint32_t
3345 tcp_maxmtu(struct in_conninfo *inc, struct tcp_ifcap *cap)
3346 {
3347 	struct nhop_object *nh;
3348 	struct ifnet *ifp;
3349 	uint32_t maxmtu = 0;
3350 
3351 	KASSERT(inc != NULL, ("tcp_maxmtu with NULL in_conninfo pointer"));
3352 
3353 	if (inc->inc_faddr.s_addr != INADDR_ANY) {
3354 		nh = fib4_lookup(inc->inc_fibnum, inc->inc_faddr, 0, NHR_NONE, 0);
3355 		if (nh == NULL)
3356 			return (0);
3357 
3358 		ifp = nh->nh_ifp;
3359 		maxmtu = nh->nh_mtu;
3360 
3361 		/* Report additional interface capabilities. */
3362 		if (cap != NULL) {
3363 			if (ifp->if_capenable & IFCAP_TSO4 &&
3364 			    ifp->if_hwassist & CSUM_TSO) {
3365 				cap->ifcap |= CSUM_TSO;
3366 				cap->tsomax = ifp->if_hw_tsomax;
3367 				cap->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
3368 				cap->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
3369 			}
3370 		}
3371 	}
3372 	return (maxmtu);
3373 }
3374 #endif /* INET */
3375 
3376 #ifdef INET6
3377 uint32_t
3378 tcp_maxmtu6(struct in_conninfo *inc, struct tcp_ifcap *cap)
3379 {
3380 	struct nhop_object *nh;
3381 	struct in6_addr dst6;
3382 	uint32_t scopeid;
3383 	struct ifnet *ifp;
3384 	uint32_t maxmtu = 0;
3385 
3386 	KASSERT(inc != NULL, ("tcp_maxmtu6 with NULL in_conninfo pointer"));
3387 
3388 	if (inc->inc_flags & INC_IPV6MINMTU)
3389 		return (IPV6_MMTU);
3390 
3391 	if (!IN6_IS_ADDR_UNSPECIFIED(&inc->inc6_faddr)) {
3392 		in6_splitscope(&inc->inc6_faddr, &dst6, &scopeid);
3393 		nh = fib6_lookup(inc->inc_fibnum, &dst6, scopeid, NHR_NONE, 0);
3394 		if (nh == NULL)
3395 			return (0);
3396 
3397 		ifp = nh->nh_ifp;
3398 		maxmtu = nh->nh_mtu;
3399 
3400 		/* Report additional interface capabilities. */
3401 		if (cap != NULL) {
3402 			if (ifp->if_capenable & IFCAP_TSO6 &&
3403 			    ifp->if_hwassist & CSUM_TSO) {
3404 				cap->ifcap |= CSUM_TSO;
3405 				cap->tsomax = ifp->if_hw_tsomax;
3406 				cap->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
3407 				cap->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
3408 			}
3409 		}
3410 	}
3411 
3412 	return (maxmtu);
3413 }
3414 
3415 /*
3416  * Handle setsockopt(IPV6_USE_MIN_MTU) by a TCP stack.
3417  *
3418  * XXXGL: we are updating inpcb here with INC_IPV6MINMTU flag.
3419  * The right place to do that is ip6_setpktopt() that has just been
3420  * executed.  By the way it just filled ip6po_minmtu for us.
3421  */
3422 void
3423 tcp6_use_min_mtu(struct tcpcb *tp)
3424 {
3425 	struct inpcb *inp = tptoinpcb(tp);
3426 
3427 	INP_WLOCK_ASSERT(inp);
3428 	/*
3429 	 * In case of the IPV6_USE_MIN_MTU socket
3430 	 * option, the INC_IPV6MINMTU flag to announce
3431 	 * a corresponding MSS during the initial
3432 	 * handshake.  If the TCP connection is not in
3433 	 * the front states, just reduce the MSS being
3434 	 * used.  This avoids the sending of TCP
3435 	 * segments which will be fragmented at the
3436 	 * IPv6 layer.
3437 	 */
3438 	inp->inp_inc.inc_flags |= INC_IPV6MINMTU;
3439 	if ((tp->t_state >= TCPS_SYN_SENT) &&
3440 	    (inp->inp_inc.inc_flags & INC_ISIPV6)) {
3441 		struct ip6_pktopts *opt;
3442 
3443 		opt = inp->in6p_outputopts;
3444 		if (opt != NULL && opt->ip6po_minmtu == IP6PO_MINMTU_ALL &&
3445 		    tp->t_maxseg > TCP6_MSS)
3446 			tp->t_maxseg = TCP6_MSS;
3447 	}
3448 }
3449 #endif /* INET6 */
3450 
3451 /*
3452  * Calculate effective SMSS per RFC5681 definition for a given TCP
3453  * connection at its current state, taking into account SACK and etc.
3454  */
3455 u_int
3456 tcp_maxseg(const struct tcpcb *tp)
3457 {
3458 	u_int optlen;
3459 
3460 	if (tp->t_flags & TF_NOOPT)
3461 		return (tp->t_maxseg);
3462 
3463 	/*
3464 	 * Here we have a simplified code from tcp_addoptions(),
3465 	 * without a proper loop, and having most of paddings hardcoded.
3466 	 * We might make mistakes with padding here in some edge cases,
3467 	 * but this is harmless, since result of tcp_maxseg() is used
3468 	 * only in cwnd and ssthresh estimations.
3469 	 */
3470 	if (TCPS_HAVEESTABLISHED(tp->t_state)) {
3471 		if (tp->t_flags & TF_RCVD_TSTMP)
3472 			optlen = TCPOLEN_TSTAMP_APPA;
3473 		else
3474 			optlen = 0;
3475 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
3476 		if (tp->t_flags & TF_SIGNATURE)
3477 			optlen += PADTCPOLEN(TCPOLEN_SIGNATURE);
3478 #endif
3479 		if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks > 0) {
3480 			optlen += TCPOLEN_SACKHDR;
3481 			optlen += tp->rcv_numsacks * TCPOLEN_SACK;
3482 			optlen = PADTCPOLEN(optlen);
3483 		}
3484 	} else {
3485 		if (tp->t_flags & TF_REQ_TSTMP)
3486 			optlen = TCPOLEN_TSTAMP_APPA;
3487 		else
3488 			optlen = PADTCPOLEN(TCPOLEN_MAXSEG);
3489 		if (tp->t_flags & TF_REQ_SCALE)
3490 			optlen += PADTCPOLEN(TCPOLEN_WINDOW);
3491 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
3492 		if (tp->t_flags & TF_SIGNATURE)
3493 			optlen += PADTCPOLEN(TCPOLEN_SIGNATURE);
3494 #endif
3495 		if (tp->t_flags & TF_SACK_PERMIT)
3496 			optlen += PADTCPOLEN(TCPOLEN_SACK_PERMITTED);
3497 	}
3498 #undef PAD
3499 	optlen = min(optlen, TCP_MAXOLEN);
3500 	return (tp->t_maxseg - optlen);
3501 }
3502 
3503 
3504 u_int
3505 tcp_fixed_maxseg(const struct tcpcb *tp)
3506 {
3507 	int optlen;
3508 
3509 	if (tp->t_flags & TF_NOOPT)
3510 		return (tp->t_maxseg);
3511 
3512 	/*
3513 	 * Here we have a simplified code from tcp_addoptions(),
3514 	 * without a proper loop, and having most of paddings hardcoded.
3515 	 * We only consider fixed options that we would send every
3516 	 * time I.e. SACK is not considered. This is important
3517 	 * for cc modules to figure out what the modulo of the
3518 	 * cwnd should be.
3519 	 */
3520 #define	PAD(len)	((((len) / 4) + !!((len) % 4)) * 4)
3521 	if (TCPS_HAVEESTABLISHED(tp->t_state)) {
3522 		if (tp->t_flags & TF_RCVD_TSTMP)
3523 			optlen = TCPOLEN_TSTAMP_APPA;
3524 		else
3525 			optlen = 0;
3526 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
3527 		if (tp->t_flags & TF_SIGNATURE)
3528 			optlen += PAD(TCPOLEN_SIGNATURE);
3529 #endif
3530 	} else {
3531 		if (tp->t_flags & TF_REQ_TSTMP)
3532 			optlen = TCPOLEN_TSTAMP_APPA;
3533 		else
3534 			optlen = PAD(TCPOLEN_MAXSEG);
3535 		if (tp->t_flags & TF_REQ_SCALE)
3536 			optlen += PAD(TCPOLEN_WINDOW);
3537 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
3538 		if (tp->t_flags & TF_SIGNATURE)
3539 			optlen += PAD(TCPOLEN_SIGNATURE);
3540 #endif
3541 		if (tp->t_flags & TF_SACK_PERMIT)
3542 			optlen += PAD(TCPOLEN_SACK_PERMITTED);
3543 	}
3544 #undef PAD
3545 	optlen = min(optlen, TCP_MAXOLEN);
3546 	return (tp->t_maxseg - optlen);
3547 }
3548 
3549 
3550 
3551 static int
3552 sysctl_drop(SYSCTL_HANDLER_ARGS)
3553 {
3554 	/* addrs[0] is a foreign socket, addrs[1] is a local one. */
3555 	struct sockaddr_storage addrs[2];
3556 	struct inpcb *inp;
3557 	struct tcpcb *tp;
3558 #ifdef INET
3559 	struct sockaddr_in *fin = NULL, *lin = NULL;
3560 #endif
3561 	struct epoch_tracker et;
3562 #ifdef INET6
3563 	struct sockaddr_in6 *fin6, *lin6;
3564 #endif
3565 	int error;
3566 
3567 	inp = NULL;
3568 #ifdef INET6
3569 	fin6 = lin6 = NULL;
3570 #endif
3571 	error = 0;
3572 
3573 	if (req->oldptr != NULL || req->oldlen != 0)
3574 		return (EINVAL);
3575 	if (req->newptr == NULL)
3576 		return (EPERM);
3577 	if (req->newlen < sizeof(addrs))
3578 		return (ENOMEM);
3579 	error = SYSCTL_IN(req, &addrs, sizeof(addrs));
3580 	if (error)
3581 		return (error);
3582 
3583 	switch (addrs[0].ss_family) {
3584 #ifdef INET6
3585 	case AF_INET6:
3586 		fin6 = (struct sockaddr_in6 *)&addrs[0];
3587 		lin6 = (struct sockaddr_in6 *)&addrs[1];
3588 		if (fin6->sin6_len != sizeof(struct sockaddr_in6) ||
3589 		    lin6->sin6_len != sizeof(struct sockaddr_in6))
3590 			return (EINVAL);
3591 		if (IN6_IS_ADDR_V4MAPPED(&fin6->sin6_addr)) {
3592 			if (!IN6_IS_ADDR_V4MAPPED(&lin6->sin6_addr))
3593 				return (EINVAL);
3594 			in6_sin6_2_sin_in_sock((struct sockaddr *)&addrs[0]);
3595 			in6_sin6_2_sin_in_sock((struct sockaddr *)&addrs[1]);
3596 #ifdef INET
3597 			fin = (struct sockaddr_in *)&addrs[0];
3598 			lin = (struct sockaddr_in *)&addrs[1];
3599 #endif
3600 			break;
3601 		}
3602 		error = sa6_embedscope(fin6, V_ip6_use_defzone);
3603 		if (error)
3604 			return (error);
3605 		error = sa6_embedscope(lin6, V_ip6_use_defzone);
3606 		if (error)
3607 			return (error);
3608 		break;
3609 #endif
3610 #ifdef INET
3611 	case AF_INET:
3612 		fin = (struct sockaddr_in *)&addrs[0];
3613 		lin = (struct sockaddr_in *)&addrs[1];
3614 		if (fin->sin_len != sizeof(struct sockaddr_in) ||
3615 		    lin->sin_len != sizeof(struct sockaddr_in))
3616 			return (EINVAL);
3617 		break;
3618 #endif
3619 	default:
3620 		return (EINVAL);
3621 	}
3622 	NET_EPOCH_ENTER(et);
3623 	switch (addrs[0].ss_family) {
3624 #ifdef INET6
3625 	case AF_INET6:
3626 		inp = in6_pcblookup(&V_tcbinfo, &fin6->sin6_addr,
3627 		    fin6->sin6_port, &lin6->sin6_addr, lin6->sin6_port,
3628 		    INPLOOKUP_WLOCKPCB, NULL);
3629 		break;
3630 #endif
3631 #ifdef INET
3632 	case AF_INET:
3633 		inp = in_pcblookup(&V_tcbinfo, fin->sin_addr, fin->sin_port,
3634 		    lin->sin_addr, lin->sin_port, INPLOOKUP_WLOCKPCB, NULL);
3635 		break;
3636 #endif
3637 	}
3638 	if (inp != NULL) {
3639 		if (!SOLISTENING(inp->inp_socket)) {
3640 			tp = intotcpcb(inp);
3641 			tp = tcp_drop(tp, ECONNABORTED);
3642 			if (tp != NULL)
3643 				INP_WUNLOCK(inp);
3644 		} else
3645 			INP_WUNLOCK(inp);
3646 	} else
3647 		error = ESRCH;
3648 	NET_EPOCH_EXIT(et);
3649 	return (error);
3650 }
3651 
3652 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DROP, drop,
3653     CTLFLAG_VNET | CTLTYPE_STRUCT | CTLFLAG_WR | CTLFLAG_SKIP |
3654     CTLFLAG_NEEDGIANT, NULL, 0, sysctl_drop, "",
3655     "Drop TCP connection");
3656 
3657 static int
3658 tcp_sysctl_setsockopt(SYSCTL_HANDLER_ARGS)
3659 {
3660 	return (sysctl_setsockopt(oidp, arg1, arg2, req, &V_tcbinfo,
3661 	    &tcp_ctloutput_set));
3662 }
3663 
3664 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, setsockopt,
3665     CTLFLAG_VNET | CTLTYPE_STRUCT | CTLFLAG_WR | CTLFLAG_SKIP |
3666     CTLFLAG_MPSAFE, NULL, 0, tcp_sysctl_setsockopt, "",
3667     "Set socket option for TCP endpoint");
3668 
3669 #ifdef KERN_TLS
3670 static int
3671 sysctl_switch_tls(SYSCTL_HANDLER_ARGS)
3672 {
3673 	/* addrs[0] is a foreign socket, addrs[1] is a local one. */
3674 	struct sockaddr_storage addrs[2];
3675 	struct inpcb *inp;
3676 #ifdef INET
3677 	struct sockaddr_in *fin = NULL, *lin = NULL;
3678 #endif
3679 	struct epoch_tracker et;
3680 #ifdef INET6
3681 	struct sockaddr_in6 *fin6, *lin6;
3682 #endif
3683 	int error;
3684 
3685 	inp = NULL;
3686 #ifdef INET6
3687 	fin6 = lin6 = NULL;
3688 #endif
3689 	error = 0;
3690 
3691 	if (req->oldptr != NULL || req->oldlen != 0)
3692 		return (EINVAL);
3693 	if (req->newptr == NULL)
3694 		return (EPERM);
3695 	if (req->newlen < sizeof(addrs))
3696 		return (ENOMEM);
3697 	error = SYSCTL_IN(req, &addrs, sizeof(addrs));
3698 	if (error)
3699 		return (error);
3700 
3701 	switch (addrs[0].ss_family) {
3702 #ifdef INET6
3703 	case AF_INET6:
3704 		fin6 = (struct sockaddr_in6 *)&addrs[0];
3705 		lin6 = (struct sockaddr_in6 *)&addrs[1];
3706 		if (fin6->sin6_len != sizeof(struct sockaddr_in6) ||
3707 		    lin6->sin6_len != sizeof(struct sockaddr_in6))
3708 			return (EINVAL);
3709 		if (IN6_IS_ADDR_V4MAPPED(&fin6->sin6_addr)) {
3710 			if (!IN6_IS_ADDR_V4MAPPED(&lin6->sin6_addr))
3711 				return (EINVAL);
3712 			in6_sin6_2_sin_in_sock((struct sockaddr *)&addrs[0]);
3713 			in6_sin6_2_sin_in_sock((struct sockaddr *)&addrs[1]);
3714 #ifdef INET
3715 			fin = (struct sockaddr_in *)&addrs[0];
3716 			lin = (struct sockaddr_in *)&addrs[1];
3717 #endif
3718 			break;
3719 		}
3720 		error = sa6_embedscope(fin6, V_ip6_use_defzone);
3721 		if (error)
3722 			return (error);
3723 		error = sa6_embedscope(lin6, V_ip6_use_defzone);
3724 		if (error)
3725 			return (error);
3726 		break;
3727 #endif
3728 #ifdef INET
3729 	case AF_INET:
3730 		fin = (struct sockaddr_in *)&addrs[0];
3731 		lin = (struct sockaddr_in *)&addrs[1];
3732 		if (fin->sin_len != sizeof(struct sockaddr_in) ||
3733 		    lin->sin_len != sizeof(struct sockaddr_in))
3734 			return (EINVAL);
3735 		break;
3736 #endif
3737 	default:
3738 		return (EINVAL);
3739 	}
3740 	NET_EPOCH_ENTER(et);
3741 	switch (addrs[0].ss_family) {
3742 #ifdef INET6
3743 	case AF_INET6:
3744 		inp = in6_pcblookup(&V_tcbinfo, &fin6->sin6_addr,
3745 		    fin6->sin6_port, &lin6->sin6_addr, lin6->sin6_port,
3746 		    INPLOOKUP_WLOCKPCB, NULL);
3747 		break;
3748 #endif
3749 #ifdef INET
3750 	case AF_INET:
3751 		inp = in_pcblookup(&V_tcbinfo, fin->sin_addr, fin->sin_port,
3752 		    lin->sin_addr, lin->sin_port, INPLOOKUP_WLOCKPCB, NULL);
3753 		break;
3754 #endif
3755 	}
3756 	NET_EPOCH_EXIT(et);
3757 	if (inp != NULL) {
3758 		struct socket *so;
3759 
3760 		so = inp->inp_socket;
3761 		soref(so);
3762 		error = ktls_set_tx_mode(so,
3763 		    arg2 == 0 ? TCP_TLS_MODE_SW : TCP_TLS_MODE_IFNET);
3764 		INP_WUNLOCK(inp);
3765 		sorele(so);
3766 	} else
3767 		error = ESRCH;
3768 	return (error);
3769 }
3770 
3771 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, switch_to_sw_tls,
3772     CTLFLAG_VNET | CTLTYPE_STRUCT | CTLFLAG_WR | CTLFLAG_SKIP |
3773     CTLFLAG_NEEDGIANT, NULL, 0, sysctl_switch_tls, "",
3774     "Switch TCP connection to SW TLS");
3775 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, switch_to_ifnet_tls,
3776     CTLFLAG_VNET | CTLTYPE_STRUCT | CTLFLAG_WR | CTLFLAG_SKIP |
3777     CTLFLAG_NEEDGIANT, NULL, 1, sysctl_switch_tls, "",
3778     "Switch TCP connection to ifnet TLS");
3779 #endif
3780 
3781 /*
3782  * Generate a standardized TCP log line for use throughout the
3783  * tcp subsystem.  Memory allocation is done with M_NOWAIT to
3784  * allow use in the interrupt context.
3785  *
3786  * NB: The caller MUST free(s, M_TCPLOG) the returned string.
3787  * NB: The function may return NULL if memory allocation failed.
3788  *
3789  * Due to header inclusion and ordering limitations the struct ip
3790  * and ip6_hdr pointers have to be passed as void pointers.
3791  */
3792 char *
3793 tcp_log_vain(struct in_conninfo *inc, struct tcphdr *th, const void *ip4hdr,
3794     const void *ip6hdr)
3795 {
3796 
3797 	/* Is logging enabled? */
3798 	if (V_tcp_log_in_vain == 0)
3799 		return (NULL);
3800 
3801 	return (tcp_log_addr(inc, th, ip4hdr, ip6hdr));
3802 }
3803 
3804 char *
3805 tcp_log_addrs(struct in_conninfo *inc, struct tcphdr *th, const void *ip4hdr,
3806     const void *ip6hdr)
3807 {
3808 
3809 	/* Is logging enabled? */
3810 	if (tcp_log_debug == 0)
3811 		return (NULL);
3812 
3813 	return (tcp_log_addr(inc, th, ip4hdr, ip6hdr));
3814 }
3815 
3816 static char *
3817 tcp_log_addr(struct in_conninfo *inc, struct tcphdr *th, const void *ip4hdr,
3818     const void *ip6hdr)
3819 {
3820 	char *s, *sp;
3821 	size_t size;
3822 #ifdef INET
3823 	const struct ip *ip = (const struct ip *)ip4hdr;
3824 #endif
3825 #ifdef INET6
3826 	const struct ip6_hdr *ip6 = (const struct ip6_hdr *)ip6hdr;
3827 #endif /* INET6 */
3828 
3829 	/*
3830 	 * The log line looks like this:
3831 	 * "TCP: [1.2.3.4]:50332 to [1.2.3.4]:80 tcpflags 0x2<SYN>"
3832 	 */
3833 	size = sizeof("TCP: []:12345 to []:12345 tcpflags 0x2<>") +
3834 	    sizeof(PRINT_TH_FLAGS) + 1 +
3835 #ifdef INET6
3836 	    2 * INET6_ADDRSTRLEN;
3837 #else
3838 	    2 * INET_ADDRSTRLEN;
3839 #endif /* INET6 */
3840 
3841 	s = malloc(size, M_TCPLOG, M_ZERO|M_NOWAIT);
3842 	if (s == NULL)
3843 		return (NULL);
3844 
3845 	strcat(s, "TCP: [");
3846 	sp = s + strlen(s);
3847 
3848 	if (inc && ((inc->inc_flags & INC_ISIPV6) == 0)) {
3849 		inet_ntoa_r(inc->inc_faddr, sp);
3850 		sp = s + strlen(s);
3851 		sprintf(sp, "]:%i to [", ntohs(inc->inc_fport));
3852 		sp = s + strlen(s);
3853 		inet_ntoa_r(inc->inc_laddr, sp);
3854 		sp = s + strlen(s);
3855 		sprintf(sp, "]:%i", ntohs(inc->inc_lport));
3856 #ifdef INET6
3857 	} else if (inc) {
3858 		ip6_sprintf(sp, &inc->inc6_faddr);
3859 		sp = s + strlen(s);
3860 		sprintf(sp, "]:%i to [", ntohs(inc->inc_fport));
3861 		sp = s + strlen(s);
3862 		ip6_sprintf(sp, &inc->inc6_laddr);
3863 		sp = s + strlen(s);
3864 		sprintf(sp, "]:%i", ntohs(inc->inc_lport));
3865 	} else if (ip6 && th) {
3866 		ip6_sprintf(sp, &ip6->ip6_src);
3867 		sp = s + strlen(s);
3868 		sprintf(sp, "]:%i to [", ntohs(th->th_sport));
3869 		sp = s + strlen(s);
3870 		ip6_sprintf(sp, &ip6->ip6_dst);
3871 		sp = s + strlen(s);
3872 		sprintf(sp, "]:%i", ntohs(th->th_dport));
3873 #endif /* INET6 */
3874 #ifdef INET
3875 	} else if (ip && th) {
3876 		inet_ntoa_r(ip->ip_src, sp);
3877 		sp = s + strlen(s);
3878 		sprintf(sp, "]:%i to [", ntohs(th->th_sport));
3879 		sp = s + strlen(s);
3880 		inet_ntoa_r(ip->ip_dst, sp);
3881 		sp = s + strlen(s);
3882 		sprintf(sp, "]:%i", ntohs(th->th_dport));
3883 #endif /* INET */
3884 	} else {
3885 		free(s, M_TCPLOG);
3886 		return (NULL);
3887 	}
3888 	sp = s + strlen(s);
3889 	if (th)
3890 		sprintf(sp, " tcpflags 0x%b", tcp_get_flags(th), PRINT_TH_FLAGS);
3891 	if (*(s + size - 1) != '\0')
3892 		panic("%s: string too long", __func__);
3893 	return (s);
3894 }
3895 
3896 /*
3897  * A subroutine which makes it easy to track TCP state changes with DTrace.
3898  * This function shouldn't be called for t_state initializations that don't
3899  * correspond to actual TCP state transitions.
3900  */
3901 void
3902 tcp_state_change(struct tcpcb *tp, int newstate)
3903 {
3904 #if defined(KDTRACE_HOOKS)
3905 	int pstate = tp->t_state;
3906 #endif
3907 
3908 	TCPSTATES_DEC(tp->t_state);
3909 	TCPSTATES_INC(newstate);
3910 	tp->t_state = newstate;
3911 	TCP_PROBE6(state__change, NULL, tp, NULL, tp, NULL, pstate);
3912 }
3913 
3914 /*
3915  * Create an external-format (``xtcpcb'') structure using the information in
3916  * the kernel-format tcpcb structure pointed to by tp.  This is done to
3917  * reduce the spew of irrelevant information over this interface, to isolate
3918  * user code from changes in the kernel structure, and potentially to provide
3919  * information-hiding if we decide that some of this information should be
3920  * hidden from users.
3921  */
3922 void
3923 tcp_inptoxtp(const struct inpcb *inp, struct xtcpcb *xt)
3924 {
3925 	struct tcpcb *tp = intotcpcb(inp);
3926 	sbintime_t now;
3927 
3928 	bzero(xt, sizeof(*xt));
3929 	xt->t_state = tp->t_state;
3930 	xt->t_logstate = tcp_get_bblog_state(tp);
3931 	xt->t_flags = tp->t_flags;
3932 	xt->t_sndzerowin = tp->t_sndzerowin;
3933 	xt->t_sndrexmitpack = tp->t_sndrexmitpack;
3934 	xt->t_rcvoopack = tp->t_rcvoopack;
3935 	xt->t_rcv_wnd = tp->rcv_wnd;
3936 	xt->t_snd_wnd = tp->snd_wnd;
3937 	xt->t_snd_cwnd = tp->snd_cwnd;
3938 	xt->t_snd_ssthresh = tp->snd_ssthresh;
3939 	xt->t_dsack_bytes = tp->t_dsack_bytes;
3940 	xt->t_dsack_tlp_bytes = tp->t_dsack_tlp_bytes;
3941 	xt->t_dsack_pack = tp->t_dsack_pack;
3942 	xt->t_maxseg = tp->t_maxseg;
3943 	xt->xt_ecn = (tp->t_flags2 & TF2_ECN_PERMIT) ? 1 : 0 +
3944 		     (tp->t_flags2 & TF2_ACE_PERMIT) ? 2 : 0;
3945 
3946 	now = getsbinuptime();
3947 #define	COPYTIMER(which,where)	do {					\
3948 	if (tp->t_timers[which] != SBT_MAX)				\
3949 		xt->where = (tp->t_timers[which] - now) / SBT_1MS;	\
3950 	else								\
3951 		xt->where = 0;						\
3952 } while (0)
3953 	COPYTIMER(TT_DELACK, tt_delack);
3954 	COPYTIMER(TT_REXMT, tt_rexmt);
3955 	COPYTIMER(TT_PERSIST, tt_persist);
3956 	COPYTIMER(TT_KEEP, tt_keep);
3957 	COPYTIMER(TT_2MSL, tt_2msl);
3958 #undef COPYTIMER
3959 	xt->t_rcvtime = 1000 * (ticks - tp->t_rcvtime) / hz;
3960 
3961 	xt->xt_encaps_port = tp->t_port;
3962 	bcopy(tp->t_fb->tfb_tcp_block_name, xt->xt_stack,
3963 	    TCP_FUNCTION_NAME_LEN_MAX);
3964 	bcopy(CC_ALGO(tp)->name, xt->xt_cc, TCP_CA_NAME_MAX);
3965 #ifdef TCP_BLACKBOX
3966 	(void)tcp_log_get_id(tp, xt->xt_logid);
3967 #endif
3968 
3969 	xt->xt_len = sizeof(struct xtcpcb);
3970 	in_pcbtoxinpcb(inp, &xt->xt_inp);
3971 	/*
3972 	 * TCP doesn't use inp_ppcb pointer, we embed inpcb into tcpcb.
3973 	 * Fixup the pointer that in_pcbtoxinpcb() has set.  When printing
3974 	 * TCP netstat(1) used to use this pointer, so this fixup needs to
3975 	 * stay for stable/14.
3976 	 */
3977 	xt->xt_inp.inp_ppcb = (uintptr_t)tp;
3978 }
3979 
3980 void
3981 tcp_log_end_status(struct tcpcb *tp, uint8_t status)
3982 {
3983 	uint32_t bit, i;
3984 
3985 	if ((tp == NULL) ||
3986 	    (status > TCP_EI_STATUS_MAX_VALUE) ||
3987 	    (status == 0)) {
3988 		/* Invalid */
3989 		return;
3990 	}
3991 	if (status > (sizeof(uint32_t) * 8)) {
3992 		/* Should this be a KASSERT? */
3993 		return;
3994 	}
3995 	bit = 1U << (status - 1);
3996 	if (bit & tp->t_end_info_status) {
3997 		/* already logged */
3998 		return;
3999 	}
4000 	for (i = 0; i < TCP_END_BYTE_INFO; i++) {
4001 		if (tp->t_end_info_bytes[i] == TCP_EI_EMPTY_SLOT) {
4002 			tp->t_end_info_bytes[i] = status;
4003 			tp->t_end_info_status |= bit;
4004 			break;
4005 		}
4006 	}
4007 }
4008 
4009 int
4010 tcp_can_enable_pacing(void)
4011 {
4012 
4013 	if ((tcp_pacing_limit == -1) ||
4014 	    (tcp_pacing_limit > number_of_tcp_connections_pacing)) {
4015 		atomic_fetchadd_int(&number_of_tcp_connections_pacing, 1);
4016 		shadow_num_connections = number_of_tcp_connections_pacing;
4017 		return (1);
4018 	} else {
4019 		counter_u64_add(tcp_pacing_failures, 1);
4020 		return (0);
4021 	}
4022 }
4023 
4024 static uint8_t tcp_pacing_warning = 0;
4025 
4026 void
4027 tcp_decrement_paced_conn(void)
4028 {
4029 	uint32_t ret;
4030 
4031 	ret = atomic_fetchadd_int(&number_of_tcp_connections_pacing, -1);
4032 	shadow_num_connections = number_of_tcp_connections_pacing;
4033 	KASSERT(ret != 0, ("tcp_paced_connection_exits -1 would cause wrap?"));
4034 	if (ret == 0) {
4035 		if (tcp_pacing_limit != -1) {
4036 			printf("Warning all pacing is now disabled, count decrements invalidly!\n");
4037 			tcp_pacing_limit = 0;
4038 		} else if (tcp_pacing_warning == 0) {
4039 			printf("Warning pacing count is invalid, invalid decrement\n");
4040 			tcp_pacing_warning = 1;
4041 		}
4042 	}
4043 }
4044 
4045 static void
4046 tcp_default_switch_failed(struct tcpcb *tp)
4047 {
4048 	/*
4049 	 * If a switch fails we only need to
4050 	 * care about two things:
4051 	 * a) The inp_flags2
4052 	 * and
4053 	 * b) The timer granularity.
4054 	 * Timeouts, at least for now, don't use the
4055 	 * old callout system in the other stacks so
4056 	 * those are hopefully safe.
4057 	 */
4058 	tcp_lro_features_off(tptoinpcb(tp));
4059 	tcp_change_time_units(tp, TCP_TMR_GRANULARITY_TICKS);
4060 }
4061 
4062 #ifdef TCP_ACCOUNTING
4063 int
4064 tcp_do_ack_accounting(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to, uint32_t tiwin, int mss)
4065 {
4066 	if (SEQ_LT(th->th_ack, tp->snd_una)) {
4067 		/* Do we have a SACK? */
4068 		if (to->to_flags & TOF_SACK) {
4069 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
4070 				tp->tcp_cnt_counters[ACK_SACK]++;
4071 			}
4072 			return (ACK_SACK);
4073 		} else {
4074 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
4075 				tp->tcp_cnt_counters[ACK_BEHIND]++;
4076 			}
4077 			return (ACK_BEHIND);
4078 		}
4079 	} else if (th->th_ack == tp->snd_una) {
4080 		/* Do we have a SACK? */
4081 		if (to->to_flags & TOF_SACK) {
4082 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
4083 				tp->tcp_cnt_counters[ACK_SACK]++;
4084 			}
4085 			return (ACK_SACK);
4086 		} else if (tiwin != tp->snd_wnd) {
4087 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
4088 				tp->tcp_cnt_counters[ACK_RWND]++;
4089 			}
4090 			return (ACK_RWND);
4091 		} else {
4092 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
4093 				tp->tcp_cnt_counters[ACK_DUPACK]++;
4094 			}
4095 			return (ACK_DUPACK);
4096 		}
4097 	} else {
4098 		if (!SEQ_GT(th->th_ack, tp->snd_max)) {
4099 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
4100 				tp->tcp_cnt_counters[CNT_OF_ACKS_IN] += (((th->th_ack - tp->snd_una) + mss - 1)/mss);
4101 			}
4102 		}
4103 		if (to->to_flags & TOF_SACK) {
4104 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
4105 				tp->tcp_cnt_counters[ACK_CUMACK_SACK]++;
4106 			}
4107 			return (ACK_CUMACK_SACK);
4108 		} else {
4109 			if (tp->t_flags2 & TF2_TCP_ACCOUNTING) {
4110 				tp->tcp_cnt_counters[ACK_CUMACK]++;
4111 			}
4112 			return (ACK_CUMACK);
4113 		}
4114 	}
4115 }
4116 #endif
4117 
4118 void
4119 tcp_change_time_units(struct tcpcb *tp, int granularity)
4120 {
4121 	if (tp->t_tmr_granularity == granularity) {
4122 		/* We are there */
4123 		return;
4124 	}
4125 	if (granularity == TCP_TMR_GRANULARITY_USEC) {
4126 		KASSERT((tp->t_tmr_granularity == TCP_TMR_GRANULARITY_TICKS),
4127 			("Granularity is not TICKS its %u in tp:%p",
4128 			 tp->t_tmr_granularity, tp));
4129 		tp->t_rttlow = TICKS_2_USEC(tp->t_rttlow);
4130 		if (tp->t_srtt > 1) {
4131 			uint32_t val, frac;
4132 
4133 			val = tp->t_srtt >> TCP_RTT_SHIFT;
4134 			frac = tp->t_srtt & 0x1f;
4135 			tp->t_srtt = TICKS_2_USEC(val);
4136 			/*
4137 			 * frac is the fractional part of the srtt (if any)
4138 			 * but its in ticks and every bit represents
4139 			 * 1/32nd of a hz.
4140 			 */
4141 			if (frac) {
4142 				if (hz == 1000) {
4143 					frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_MSEC) / (uint64_t)TCP_RTT_SCALE);
4144 				} else {
4145 					frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_SEC) / ((uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE));
4146 				}
4147 				tp->t_srtt += frac;
4148 			}
4149 		}
4150 		if (tp->t_rttvar) {
4151 			uint32_t val, frac;
4152 
4153 			val = tp->t_rttvar >> TCP_RTTVAR_SHIFT;
4154 			frac = tp->t_rttvar & 0x1f;
4155 			tp->t_rttvar = TICKS_2_USEC(val);
4156 			/*
4157 			 * frac is the fractional part of the srtt (if any)
4158 			 * but its in ticks and every bit represents
4159 			 * 1/32nd of a hz.
4160 			 */
4161 			if (frac) {
4162 				if (hz == 1000) {
4163 					frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_MSEC) / (uint64_t)TCP_RTT_SCALE);
4164 				} else {
4165 					frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_SEC) / ((uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE));
4166 				}
4167 				tp->t_rttvar += frac;
4168 			}
4169 		}
4170 		tp->t_tmr_granularity = TCP_TMR_GRANULARITY_USEC;
4171 	} else if (granularity == TCP_TMR_GRANULARITY_TICKS) {
4172 		/* Convert back to ticks, with  */
4173 		KASSERT((tp->t_tmr_granularity == TCP_TMR_GRANULARITY_USEC),
4174 			("Granularity is not USEC its %u in tp:%p",
4175 			 tp->t_tmr_granularity, tp));
4176 		if (tp->t_srtt > 1) {
4177 			uint32_t val, frac;
4178 
4179 			val = USEC_2_TICKS(tp->t_srtt);
4180 			frac = tp->t_srtt % (HPTS_USEC_IN_SEC / hz);
4181 			tp->t_srtt = val << TCP_RTT_SHIFT;
4182 			/*
4183 			 * frac is the fractional part here is left
4184 			 * over from converting to hz and shifting.
4185 			 * We need to convert this to the 5 bit
4186 			 * remainder.
4187 			 */
4188 			if (frac) {
4189 				if (hz == 1000) {
4190 					frac = (((uint64_t)frac *  (uint64_t)TCP_RTT_SCALE) / (uint64_t)HPTS_USEC_IN_MSEC);
4191 				} else {
4192 					frac = (((uint64_t)frac * (uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE) /(uint64_t)HPTS_USEC_IN_SEC);
4193 				}
4194 				tp->t_srtt += frac;
4195 			}
4196 		}
4197 		if (tp->t_rttvar) {
4198 			uint32_t val, frac;
4199 
4200 			val = USEC_2_TICKS(tp->t_rttvar);
4201 			frac = tp->t_srtt % (HPTS_USEC_IN_SEC / hz);
4202 			tp->t_rttvar = val <<  TCP_RTTVAR_SHIFT;
4203 			/*
4204 			 * frac is the fractional part here is left
4205 			 * over from converting to hz and shifting.
4206 			 * We need to convert this to the 5 bit
4207 			 * remainder.
4208 			 */
4209 			if (frac) {
4210 				if (hz == 1000) {
4211 					frac = (((uint64_t)frac *  (uint64_t)TCP_RTT_SCALE) / (uint64_t)HPTS_USEC_IN_MSEC);
4212 				} else {
4213 					frac = (((uint64_t)frac * (uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE) /(uint64_t)HPTS_USEC_IN_SEC);
4214 				}
4215 				tp->t_rttvar += frac;
4216 			}
4217 		}
4218 		tp->t_rttlow = USEC_2_TICKS(tp->t_rttlow);
4219 		tp->t_tmr_granularity = TCP_TMR_GRANULARITY_TICKS;
4220 	}
4221 #ifdef INVARIANTS
4222 	else {
4223 		panic("Unknown granularity:%d tp:%p",
4224 		      granularity, tp);
4225 	}
4226 #endif
4227 }
4228 
4229 void
4230 tcp_handle_orphaned_packets(struct tcpcb *tp)
4231 {
4232 	struct mbuf *save, *m, *prev;
4233 	/*
4234 	 * Called when a stack switch is occuring from the fini()
4235 	 * of the old stack. We assue the init() as already been
4236 	 * run of the new stack and it has set the inp_flags2 to
4237 	 * what it supports. This function will then deal with any
4238 	 * differences i.e. cleanup packets that maybe queued that
4239 	 * the newstack does not support.
4240 	 */
4241 
4242 	if (tptoinpcb(tp)->inp_flags2 & INP_MBUF_L_ACKS)
4243 		return;
4244 	if ((tptoinpcb(tp)->inp_flags2 & INP_SUPPORTS_MBUFQ) == 0 &&
4245 	    !STAILQ_EMPTY(&tp->t_inqueue)) {
4246 		/*
4247 		 * It is unsafe to process the packets since a
4248 		 * reset may be lurking in them (its rare but it
4249 		 * can occur). If we were to find a RST, then we
4250 		 * would end up dropping the connection and the
4251 		 * INP lock, so when we return the caller (tcp_usrreq)
4252 		 * will blow up when it trys to unlock the inp.
4253 		 * This new stack does not do any fancy LRO features
4254 		 * so all we can do is toss the packets.
4255 		 */
4256 		m = STAILQ_FIRST(&tp->t_inqueue);
4257 		STAILQ_INIT(&tp->t_inqueue);
4258 		STAILQ_FOREACH_FROM_SAFE(m, &tp->t_inqueue, m_stailqpkt, save)
4259 			m_freem(m);
4260 	} else {
4261 		/*
4262 		 * Here we have a stack that does mbuf queuing but
4263 		 * does not support compressed ack's. We must
4264 		 * walk all the mbufs and discard any compressed acks.
4265 		 */
4266 		STAILQ_FOREACH_SAFE(m, &tp->t_inqueue, m_stailqpkt, save) {
4267 			if (m->m_flags & M_ACKCMP) {
4268 				if (m == STAILQ_FIRST(&tp->t_inqueue))
4269 					STAILQ_REMOVE_HEAD(&tp->t_inqueue,
4270 					    m_stailqpkt);
4271 				else
4272 					STAILQ_REMOVE_AFTER(&tp->t_inqueue,
4273 					    prev, m_stailqpkt);
4274 				m_freem(m);
4275 			} else
4276 				prev = m;
4277 		}
4278 	}
4279 }
4280 
4281 #ifdef TCP_REQUEST_TRK
4282 uint32_t
4283 tcp_estimate_tls_overhead(struct socket *so, uint64_t tls_usr_bytes)
4284 {
4285 #ifdef KERN_TLS
4286 	struct ktls_session *tls;
4287 	uint32_t rec_oh, records;
4288 
4289 	tls = so->so_snd.sb_tls_info;
4290 	if (tls == NULL)
4291 	    return (0);
4292 
4293 	rec_oh = tls->params.tls_hlen + tls->params.tls_tlen;
4294 	records = ((tls_usr_bytes + tls->params.max_frame_len - 1)/tls->params.max_frame_len);
4295 	return (records * rec_oh);
4296 #else
4297 	return (0);
4298 #endif
4299 }
4300 
4301 extern uint32_t tcp_stale_entry_time;
4302 uint32_t tcp_stale_entry_time = 250000;
4303 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, usrlog_stale, CTLFLAG_RW,
4304     &tcp_stale_entry_time, 250000, "Time that a http entry without a sendfile ages out");
4305 
4306 void
4307 tcp_http_log_req_info(struct tcpcb *tp, struct http_sendfile_track *http,
4308     uint16_t slot, uint8_t val, uint64_t offset, uint64_t nbytes)
4309 {
4310 	if (tcp_bblogging_on(tp)) {
4311 		union tcp_log_stackspecific log;
4312 		struct timeval tv;
4313 
4314 		memset(&log.u_bbr, 0, sizeof(log.u_bbr));
4315 #ifdef TCPHPTS
4316 		log.u_bbr.inhpts = tcp_in_hpts(tptoinpcb(tp));
4317 #endif
4318 		log.u_bbr.flex8 = val;
4319 		log.u_bbr.rttProp = http->timestamp;
4320 		log.u_bbr.delRate = http->start;
4321 		log.u_bbr.cur_del_rate = http->end;
4322 		log.u_bbr.flex1 = http->start_seq;
4323 		log.u_bbr.flex2 = http->end_seq;
4324 		log.u_bbr.flex3 = http->flags;
4325 		log.u_bbr.flex4 = ((http->localtime >> 32) & 0x00000000ffffffff);
4326 		log.u_bbr.flex5 = (http->localtime & 0x00000000ffffffff);
4327 		log.u_bbr.flex7 = slot;
4328 		log.u_bbr.bw_inuse = offset;
4329 		/* nbytes = flex6 | epoch */
4330 		log.u_bbr.flex6 = ((nbytes >> 32) & 0x00000000ffffffff);
4331 		log.u_bbr.epoch = (nbytes & 0x00000000ffffffff);
4332 		/* cspr =  lt_epoch | pkts_out */
4333 		log.u_bbr.lt_epoch = ((http->cspr >> 32) & 0x00000000ffffffff);
4334 		log.u_bbr.pkts_out |= (http->cspr & 0x00000000ffffffff);
4335 		log.u_bbr.applimited = tp->t_http_closed;
4336 		log.u_bbr.applimited <<= 8;
4337 		log.u_bbr.applimited |= tp->t_http_open;
4338 		log.u_bbr.applimited <<= 8;
4339 		log.u_bbr.applimited |= tp->t_http_req;
4340 		log.u_bbr.timeStamp = tcp_get_usecs(&tv);
4341 		TCP_LOG_EVENTP(tp, NULL,
4342 		    &tptosocket(tp)->so_rcv,
4343 		    &tptosocket(tp)->so_snd,
4344 		    TCP_LOG_HTTP_T, 0,
4345 		    0, &log, false, &tv);
4346 	}
4347 }
4348 
4349 void
4350 tcp_http_free_a_slot(struct tcpcb *tp, struct http_sendfile_track *ent)
4351 {
4352 	if (tp->t_http_req > 0)
4353 		tp->t_http_req--;
4354 	if (ent->flags & TCP_HTTP_TRACK_FLG_OPEN) {
4355 		if (tp->t_http_open > 0)
4356 			tp->t_http_open--;
4357 	} else {
4358 		if (tp->t_http_closed > 0)
4359 			tp->t_http_closed--;
4360 	}
4361 	ent->flags = TCP_HTTP_TRACK_FLG_EMPTY;
4362 }
4363 
4364 static void
4365 tcp_http_check_for_stale_entries(struct tcpcb *tp, uint64_t ts, int rm_oldest)
4366 {
4367 	struct http_sendfile_track *ent;
4368 	uint64_t time_delta, oldest_delta;
4369 	int i, oldest, oldest_set = 0, cnt_rm = 0;
4370 
4371 	for(i = 0; i < MAX_TCP_HTTP_REQ; i++) {
4372 		ent = &tp->t_http_info[i];
4373 		if (ent->flags != TCP_HTTP_TRACK_FLG_USED) {
4374 			/*
4375 			 * We only care about closed end ranges
4376 			 * that are allocated and have no sendfile
4377 			 * ever touching them. They would be in
4378 			 * state USED.
4379 			 */
4380 			continue;
4381 		}
4382 		if (ts >= ent->localtime)
4383 			time_delta = ts - ent->localtime;
4384 		else
4385 			time_delta = 0;
4386 		if (time_delta &&
4387 		    ((oldest_delta < time_delta) || (oldest_set == 0))) {
4388 			oldest_set = 1;
4389 			oldest = i;
4390 			oldest_delta = time_delta;
4391 		}
4392 		if (tcp_stale_entry_time && (time_delta >= tcp_stale_entry_time)) {
4393 			/*
4394 			 * No sendfile in a our time-limit
4395 			 * time to purge it.
4396 			 */
4397 			cnt_rm++;
4398 			tcp_http_log_req_info(tp, &tp->t_http_info[i], i, TCP_HTTP_REQ_LOG_STALE,
4399 					      time_delta, 0);
4400 			tcp_http_free_a_slot(tp, ent);
4401 		}
4402 	}
4403 	if ((cnt_rm == 0) && rm_oldest && oldest_set) {
4404 		ent = &tp->t_http_info[oldest];
4405 		tcp_http_log_req_info(tp, &tp->t_http_info[i], i, TCP_HTTP_REQ_LOG_STALE,
4406 				      oldest_delta, 1);
4407 		tcp_http_free_a_slot(tp, ent);
4408 	}
4409 }
4410 
4411 int
4412 tcp_http_check_for_comp(struct tcpcb *tp, tcp_seq ack_point)
4413 {
4414 	int i, ret=0;
4415 	struct http_sendfile_track *ent;
4416 
4417 	/* Clean up any old closed end requests that are now completed */
4418 	if (tp->t_http_req == 0)
4419 		return(0);
4420 	if (tp->t_http_closed == 0)
4421 		return(0);
4422 	for(i = 0; i < MAX_TCP_HTTP_REQ; i++) {
4423 		ent = &tp->t_http_info[i];
4424 		/* Skip empty ones */
4425 		if (ent->flags == TCP_HTTP_TRACK_FLG_EMPTY)
4426 			continue;
4427 		/* Skip open ones */
4428 		if (ent->flags & TCP_HTTP_TRACK_FLG_OPEN)
4429 			continue;
4430 		if (SEQ_GEQ(ack_point, ent->end_seq)) {
4431 			/* We are past it -- free it */
4432 			tcp_http_log_req_info(tp, ent,
4433 					      i, TCP_HTTP_REQ_LOG_FREED, 0, 0);
4434 			tcp_http_free_a_slot(tp, ent);
4435 			ret++;
4436 		}
4437 	}
4438 	return (ret);
4439 }
4440 
4441 int
4442 tcp_http_is_entry_comp(struct tcpcb *tp, struct http_sendfile_track *ent, tcp_seq ack_point)
4443 {
4444 	if (tp->t_http_req == 0)
4445 		return(-1);
4446 	if (tp->t_http_closed == 0)
4447 		return(-1);
4448 	if (ent->flags == TCP_HTTP_TRACK_FLG_EMPTY)
4449 		return(-1);
4450 	if (SEQ_GEQ(ack_point, ent->end_seq)) {
4451 		return (1);
4452 	}
4453 	return (0);
4454 }
4455 
4456 struct http_sendfile_track *
4457 tcp_http_find_a_req_that_is_completed_by(struct tcpcb *tp, tcp_seq th_ack, int *ip)
4458 {
4459 	/*
4460 	 * Given an ack point (th_ack) walk through our entries and
4461 	 * return the first one found that th_ack goes past the
4462 	 * end_seq.
4463 	 */
4464 	struct http_sendfile_track *ent;
4465 	int i;
4466 
4467 	if (tp->t_http_req == 0) {
4468 		/* none open */
4469 		return (NULL);
4470 	}
4471 	for(i = 0; i < MAX_TCP_HTTP_REQ; i++) {
4472 		ent = &tp->t_http_info[i];
4473 		if (ent->flags == TCP_HTTP_TRACK_FLG_EMPTY)
4474 			continue;
4475 		if ((ent->flags & TCP_HTTP_TRACK_FLG_OPEN) == 0) {
4476 			if (SEQ_GEQ(th_ack, ent->end_seq)) {
4477 				*ip = i;
4478 				return (ent);
4479 			}
4480 		}
4481 	}
4482 	return (NULL);
4483 }
4484 
4485 struct http_sendfile_track *
4486 tcp_http_find_req_for_seq(struct tcpcb *tp, tcp_seq seq)
4487 {
4488 	struct http_sendfile_track *ent;
4489 	int i;
4490 
4491 	if (tp->t_http_req == 0) {
4492 		/* none open */
4493 		return (NULL);
4494 	}
4495 	for(i = 0; i < MAX_TCP_HTTP_REQ; i++) {
4496 		ent = &tp->t_http_info[i];
4497 		tcp_http_log_req_info(tp, ent, i, TCP_HTTP_REQ_LOG_SEARCH,
4498 				      (uint64_t)seq, 0);
4499 		if (ent->flags == TCP_HTTP_TRACK_FLG_EMPTY) {
4500 			continue;
4501 		}
4502 		if (ent->flags & TCP_HTTP_TRACK_FLG_OPEN) {
4503 			/*
4504 			 * An open end request only needs to
4505 			 * match the beginning seq or be
4506 			 * all we have (once we keep going on
4507 			 * a open end request we may have a seq
4508 			 * wrap).
4509 			 */
4510 			if ((SEQ_GEQ(seq, ent->start_seq)) ||
4511 			    (tp->t_http_closed == 0))
4512 				return (ent);
4513 		} else {
4514 			/*
4515 			 * For this one we need to
4516 			 * be a bit more careful if its
4517 			 * completed at least.
4518 			 */
4519 			if ((SEQ_GEQ(seq, ent->start_seq)) &&
4520 			    (SEQ_LT(seq, ent->end_seq))) {
4521 				return (ent);
4522 			}
4523 		}
4524 	}
4525 	return (NULL);
4526 }
4527 
4528 /* Should this be in its own file tcp_http.c ? */
4529 struct http_sendfile_track *
4530 tcp_http_alloc_req_full(struct tcpcb *tp, struct http_req *req, uint64_t ts, int rec_dups)
4531 {
4532 	struct http_sendfile_track *fil;
4533 	int i, allocated;
4534 
4535 	/* In case the stack does not check for completions do so now */
4536 	tcp_http_check_for_comp(tp, tp->snd_una);
4537 	/* Check for stale entries */
4538 	if (tp->t_http_req)
4539 		tcp_http_check_for_stale_entries(tp, ts,
4540 		    (tp->t_http_req >= MAX_TCP_HTTP_REQ));
4541 	/* Check to see if this is a duplicate of one not started */
4542 	if (tp->t_http_req) {
4543 		for(i = 0, allocated = 0; i < MAX_TCP_HTTP_REQ; i++) {
4544 			fil = &tp->t_http_info[i];
4545 			if (fil->flags != TCP_HTTP_TRACK_FLG_USED)
4546 				continue;
4547 			if ((fil->timestamp == req->timestamp) &&
4548 			    (fil->start == req->start) &&
4549 			    ((fil->flags & TCP_HTTP_TRACK_FLG_OPEN) ||
4550 			     (fil->end == req->end))) {
4551 				/*
4552 				 * We already have this request
4553 				 * and it has not been started with sendfile.
4554 				 * This probably means the user was returned
4555 				 * a 4xx of some sort and its going to age
4556 				 * out, lets not duplicate it.
4557 				 */
4558 				return(fil);
4559 			}
4560 		}
4561 	}
4562 	/* Ok if there is no room at the inn we are in trouble */
4563 	if (tp->t_http_req >= MAX_TCP_HTTP_REQ) {
4564 		tcp_trace_point(tp, TCP_TP_HTTP_LOG_FAIL);
4565 		for(i = 0; i < MAX_TCP_HTTP_REQ; i++) {
4566 			tcp_http_log_req_info(tp, &tp->t_http_info[i],
4567 			    i, TCP_HTTP_REQ_LOG_ALLOCFAIL, 0, 0);
4568 		}
4569 		return (NULL);
4570 	}
4571 	for(i = 0, allocated = 0; i < MAX_TCP_HTTP_REQ; i++) {
4572 		fil = &tp->t_http_info[i];
4573 		if (fil->flags == TCP_HTTP_TRACK_FLG_EMPTY) {
4574 			allocated = 1;
4575 			fil->flags = TCP_HTTP_TRACK_FLG_USED;
4576 			fil->timestamp = req->timestamp;
4577 			fil->localtime = ts;
4578 			fil->start = req->start;
4579 			if (req->flags & TCP_LOG_HTTPD_RANGE_END) {
4580 				fil->end = req->end;
4581 			} else {
4582 				fil->end = 0;
4583 				fil->flags |= TCP_HTTP_TRACK_FLG_OPEN;
4584 			}
4585 			/*
4586 			 * We can set the min boundaries to the TCP Sequence space,
4587 			 * but it might be found to be further up when sendfile
4588 			 * actually runs on this range (if it ever does).
4589 			 */
4590 			fil->sbcc_at_s = tptosocket(tp)->so_snd.sb_ccc;
4591 			fil->start_seq = tp->snd_una +
4592 			    tptosocket(tp)->so_snd.sb_ccc;
4593 			fil->end_seq = (fil->start_seq + ((uint32_t)(fil->end - fil->start)));
4594 			if (tptosocket(tp)->so_snd.sb_tls_info) {
4595 				/*
4596 				 * This session is doing TLS. Take a swag guess
4597 				 * at the overhead.
4598 				 */
4599 				fil->end_seq += tcp_estimate_tls_overhead(
4600 				    tptosocket(tp), (fil->end - fil->start));
4601 			}
4602 			tp->t_http_req++;
4603 			if (fil->flags & TCP_HTTP_TRACK_FLG_OPEN)
4604 				tp->t_http_open++;
4605 			else
4606 				tp->t_http_closed++;
4607 			tcp_http_log_req_info(tp, fil, i,
4608 			    TCP_HTTP_REQ_LOG_NEW, 0, 0);
4609 			break;
4610 		} else
4611 			fil = NULL;
4612 	}
4613 	return (fil);
4614 }
4615 
4616 void
4617 tcp_http_alloc_req(struct tcpcb *tp, union tcp_log_userdata *user, uint64_t ts)
4618 {
4619 	(void)tcp_http_alloc_req_full(tp, &user->http_req, ts, 1);
4620 }
4621 #endif
4622 
4623 void
4624 tcp_log_socket_option(struct tcpcb *tp, uint32_t option_num, uint32_t option_val, int err)
4625 {
4626 	if (tcp_bblogging_on(tp)) {
4627 		struct tcp_log_buffer *l;
4628 
4629 		l = tcp_log_event(tp, NULL,
4630 		        &tptosocket(tp)->so_rcv,
4631 		        &tptosocket(tp)->so_snd,
4632 		        TCP_LOG_SOCKET_OPT,
4633 		        err, 0, NULL, 1,
4634 		        NULL, NULL, 0, NULL);
4635 		if (l) {
4636 			l->tlb_flex1 = option_num;
4637 			l->tlb_flex2 = option_val;
4638 		}
4639 	}
4640 }
4641