xref: /freebsd/sys/netinet/tcp_usrreq.c (revision ff8da9b2bab43920a19c16855ac3d30b5ccb1df2)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1988, 1993
5  *	The Regents of the University of California.
6  * Copyright (c) 2006-2007 Robert N. M. Watson
7  * Copyright (c) 2010-2011 Juniper Networks, Inc.
8  * All rights reserved.
9  *
10  * Portions of this software were developed by Robert N. M. Watson under
11  * contract to Juniper Networks, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
38  */
39 
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 #include "opt_ddb.h"
44 #include "opt_inet.h"
45 #include "opt_inet6.h"
46 #include "opt_ipsec.h"
47 #include "opt_kern_tls.h"
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/arb.h>
52 #include <sys/limits.h>
53 #include <sys/malloc.h>
54 #include <sys/refcount.h>
55 #include <sys/kernel.h>
56 #include <sys/ktls.h>
57 #include <sys/qmath.h>
58 #include <sys/sysctl.h>
59 #include <sys/mbuf.h>
60 #ifdef INET6
61 #include <sys/domain.h>
62 #endif /* INET6 */
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/protosw.h>
66 #include <sys/proc.h>
67 #include <sys/jail.h>
68 #include <sys/stats.h>
69 
70 #ifdef DDB
71 #include <ddb/ddb.h>
72 #endif
73 
74 #include <net/if.h>
75 #include <net/if_var.h>
76 #include <net/route.h>
77 #include <net/vnet.h>
78 
79 #include <netinet/in.h>
80 #include <netinet/in_kdtrace.h>
81 #include <netinet/in_pcb.h>
82 #include <netinet/in_systm.h>
83 #include <netinet/in_var.h>
84 #include <netinet/ip.h>
85 #include <netinet/ip_var.h>
86 #ifdef INET6
87 #include <netinet/ip6.h>
88 #include <netinet6/in6_pcb.h>
89 #include <netinet6/ip6_var.h>
90 #include <netinet6/scope6_var.h>
91 #endif
92 #include <netinet/tcp.h>
93 #include <netinet/tcp_fsm.h>
94 #include <netinet/tcp_seq.h>
95 #include <netinet/tcp_timer.h>
96 #include <netinet/tcp_var.h>
97 #include <netinet/tcp_log_buf.h>
98 #include <netinet/tcpip.h>
99 #include <netinet/cc/cc.h>
100 #include <netinet/tcp_fastopen.h>
101 #include <netinet/tcp_hpts.h>
102 #ifdef TCPPCAP
103 #include <netinet/tcp_pcap.h>
104 #endif
105 #ifdef TCP_OFFLOAD
106 #include <netinet/tcp_offload.h>
107 #endif
108 #include <netipsec/ipsec_support.h>
109 
110 #include <vm/vm.h>
111 #include <vm/vm_param.h>
112 #include <vm/pmap.h>
113 #include <vm/vm_extern.h>
114 #include <vm/vm_map.h>
115 #include <vm/vm_page.h>
116 
117 /*
118  * TCP protocol interface to socket abstraction.
119  */
120 #ifdef INET
121 static int	tcp_connect(struct tcpcb *, struct sockaddr_in *,
122 		    struct thread *td);
123 #endif /* INET */
124 #ifdef INET6
125 static int	tcp6_connect(struct tcpcb *, struct sockaddr_in6 *,
126 		    struct thread *td);
127 #endif /* INET6 */
128 static void	tcp_disconnect(struct tcpcb *);
129 static void	tcp_usrclosed(struct tcpcb *);
130 static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
131 
132 static int	tcp_pru_options_support(struct tcpcb *tp, int flags);
133 
134 /*
135  * TCP attaches to socket via pru_attach(), reserving space,
136  * and an internet control block.
137  */
138 static int
139 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
140 {
141 	struct inpcb *inp;
142 	struct tcpcb *tp = NULL;
143 	int error;
144 
145 	inp = sotoinpcb(so);
146 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
147 
148 	error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
149 	if (error)
150 		goto out;
151 
152 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
153 	so->so_snd.sb_flags |= SB_AUTOSIZE;
154 	error = in_pcballoc(so, &V_tcbinfo);
155 	if (error)
156 		goto out;
157 	inp = sotoinpcb(so);
158 	tp = tcp_newtcpcb(inp);
159 	if (tp == NULL) {
160 		error = ENOBUFS;
161 		in_pcbdetach(inp);
162 		in_pcbfree(inp);
163 		goto out;
164 	}
165 	tp->t_state = TCPS_CLOSED;
166 	INP_WUNLOCK(inp);
167 	TCPSTATES_INC(TCPS_CLOSED);
168 out:
169 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
170 	return (error);
171 }
172 
173 /*
174  * tcp_usr_detach is called when the socket layer loses its final reference
175  * to the socket, be it a file descriptor reference, a reference from TCP,
176  * etc.  At this point, there is only one case in which we will keep around
177  * inpcb state: time wait.
178  */
179 static void
180 tcp_usr_detach(struct socket *so)
181 {
182 	struct inpcb *inp;
183 	struct tcpcb *tp;
184 
185 	inp = sotoinpcb(so);
186 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
187 	INP_WLOCK(inp);
188 	KASSERT(so->so_pcb == inp && inp->inp_socket == so,
189 		("%s: socket %p inp %p mismatch", __func__, so, inp));
190 
191 	tp = intotcpcb(inp);
192 
193 	KASSERT(inp->inp_flags & INP_DROPPED ||
194 	    tp->t_state < TCPS_SYN_SENT,
195 	    ("%s: inp %p not dropped or embryonic", __func__, inp));
196 
197 	tcp_discardcb(tp);
198 	in_pcbdetach(inp);
199 	in_pcbfree(inp);
200 }
201 
202 #ifdef INET
203 /*
204  * Give the socket an address.
205  */
206 static int
207 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
208 {
209 	int error = 0;
210 	struct inpcb *inp;
211 #ifdef KDTRACE_HOOKS
212 	struct tcpcb *tp = NULL;
213 #endif
214 	struct sockaddr_in *sinp;
215 
216 	sinp = (struct sockaddr_in *)nam;
217 	if (nam->sa_family != AF_INET) {
218 		/*
219 		 * Preserve compatibility with old programs.
220 		 */
221 		if (nam->sa_family != AF_UNSPEC ||
222 		    nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
223 		    sinp->sin_addr.s_addr != INADDR_ANY)
224 			return (EAFNOSUPPORT);
225 		nam->sa_family = AF_INET;
226 	}
227 	if (nam->sa_len != sizeof(*sinp))
228 		return (EINVAL);
229 
230 	/*
231 	 * Must check for multicast addresses and disallow binding
232 	 * to them.
233 	 */
234 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
235 		return (EAFNOSUPPORT);
236 
237 	inp = sotoinpcb(so);
238 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
239 	INP_WLOCK(inp);
240 	if (inp->inp_flags & INP_DROPPED) {
241 		error = EINVAL;
242 		goto out;
243 	}
244 #ifdef KDTRACE_HOOKS
245 	tp = intotcpcb(inp);
246 #endif
247 	INP_HASH_WLOCK(&V_tcbinfo);
248 	error = in_pcbbind(inp, sinp, td->td_ucred);
249 	INP_HASH_WUNLOCK(&V_tcbinfo);
250 out:
251 	TCP_PROBE2(debug__user, tp, PRU_BIND);
252 	INP_WUNLOCK(inp);
253 
254 	return (error);
255 }
256 #endif /* INET */
257 
258 #ifdef INET6
259 static int
260 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
261 {
262 	int error = 0;
263 	struct inpcb *inp;
264 #ifdef KDTRACE_HOOKS
265 	struct tcpcb *tp = NULL;
266 #endif
267 	struct sockaddr_in6 *sin6;
268 	u_char vflagsav;
269 
270 	sin6 = (struct sockaddr_in6 *)nam;
271 	if (nam->sa_family != AF_INET6)
272 		return (EAFNOSUPPORT);
273 	if (nam->sa_len != sizeof(*sin6))
274 		return (EINVAL);
275 
276 	/*
277 	 * Must check for multicast addresses and disallow binding
278 	 * to them.
279 	 */
280 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
281 		return (EAFNOSUPPORT);
282 
283 	inp = sotoinpcb(so);
284 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
285 	INP_WLOCK(inp);
286 	vflagsav = inp->inp_vflag;
287 	if (inp->inp_flags & INP_DROPPED) {
288 		error = EINVAL;
289 		goto out;
290 	}
291 #ifdef KDTRACE_HOOKS
292 	tp = intotcpcb(inp);
293 #endif
294 	INP_HASH_WLOCK(&V_tcbinfo);
295 	inp->inp_vflag &= ~INP_IPV4;
296 	inp->inp_vflag |= INP_IPV6;
297 #ifdef INET
298 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
299 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
300 			inp->inp_vflag |= INP_IPV4;
301 		else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
302 			struct sockaddr_in sin;
303 
304 			in6_sin6_2_sin(&sin, sin6);
305 			if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
306 				error = EAFNOSUPPORT;
307 				INP_HASH_WUNLOCK(&V_tcbinfo);
308 				goto out;
309 			}
310 			inp->inp_vflag |= INP_IPV4;
311 			inp->inp_vflag &= ~INP_IPV6;
312 			error = in_pcbbind(inp, &sin, td->td_ucred);
313 			INP_HASH_WUNLOCK(&V_tcbinfo);
314 			goto out;
315 		}
316 	}
317 #endif
318 	error = in6_pcbbind(inp, sin6, td->td_ucred);
319 	INP_HASH_WUNLOCK(&V_tcbinfo);
320 out:
321 	if (error != 0)
322 		inp->inp_vflag = vflagsav;
323 	TCP_PROBE2(debug__user, tp, PRU_BIND);
324 	INP_WUNLOCK(inp);
325 	return (error);
326 }
327 #endif /* INET6 */
328 
329 #ifdef INET
330 /*
331  * Prepare to accept connections.
332  */
333 static int
334 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
335 {
336 	int error = 0;
337 	struct inpcb *inp;
338 	struct tcpcb *tp = NULL;
339 
340 	inp = sotoinpcb(so);
341 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
342 	INP_WLOCK(inp);
343 	if (inp->inp_flags & INP_DROPPED) {
344 		error = EINVAL;
345 		goto out;
346 	}
347 	tp = intotcpcb(inp);
348 	SOCK_LOCK(so);
349 	error = solisten_proto_check(so);
350 	if (error != 0) {
351 		SOCK_UNLOCK(so);
352 		goto out;
353 	}
354 	if (inp->inp_lport == 0) {
355 		INP_HASH_WLOCK(&V_tcbinfo);
356 		error = in_pcbbind(inp, NULL, td->td_ucred);
357 		INP_HASH_WUNLOCK(&V_tcbinfo);
358 	}
359 	if (error == 0) {
360 		tcp_state_change(tp, TCPS_LISTEN);
361 		solisten_proto(so, backlog);
362 #ifdef TCP_OFFLOAD
363 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
364 			tcp_offload_listen_start(tp);
365 #endif
366 	} else {
367 		solisten_proto_abort(so);
368 	}
369 	SOCK_UNLOCK(so);
370 
371 	if (IS_FASTOPEN(tp->t_flags))
372 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
373 
374 out:
375 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
376 	INP_WUNLOCK(inp);
377 	return (error);
378 }
379 #endif /* INET */
380 
381 #ifdef INET6
382 static int
383 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
384 {
385 	int error = 0;
386 	struct inpcb *inp;
387 	struct tcpcb *tp = NULL;
388 	u_char vflagsav;
389 
390 	inp = sotoinpcb(so);
391 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
392 	INP_WLOCK(inp);
393 	if (inp->inp_flags & INP_DROPPED) {
394 		error = EINVAL;
395 		goto out;
396 	}
397 	vflagsav = inp->inp_vflag;
398 	tp = intotcpcb(inp);
399 	SOCK_LOCK(so);
400 	error = solisten_proto_check(so);
401 	if (error != 0) {
402 		SOCK_UNLOCK(so);
403 		goto out;
404 	}
405 	INP_HASH_WLOCK(&V_tcbinfo);
406 	if (inp->inp_lport == 0) {
407 		inp->inp_vflag &= ~INP_IPV4;
408 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
409 			inp->inp_vflag |= INP_IPV4;
410 		error = in6_pcbbind(inp, NULL, td->td_ucred);
411 	}
412 	INP_HASH_WUNLOCK(&V_tcbinfo);
413 	if (error == 0) {
414 		tcp_state_change(tp, TCPS_LISTEN);
415 		solisten_proto(so, backlog);
416 #ifdef TCP_OFFLOAD
417 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
418 			tcp_offload_listen_start(tp);
419 #endif
420 	} else {
421 		solisten_proto_abort(so);
422 	}
423 	SOCK_UNLOCK(so);
424 
425 	if (IS_FASTOPEN(tp->t_flags))
426 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
427 
428 	if (error != 0)
429 		inp->inp_vflag = vflagsav;
430 
431 out:
432 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
433 	INP_WUNLOCK(inp);
434 	return (error);
435 }
436 #endif /* INET6 */
437 
438 #ifdef INET
439 /*
440  * Initiate connection to peer.
441  * Create a template for use in transmissions on this connection.
442  * Enter SYN_SENT state, and mark socket as connecting.
443  * Start keep-alive timer, and seed output sequence space.
444  * Send initial segment on connection.
445  */
446 static int
447 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
448 {
449 	struct epoch_tracker et;
450 	int error = 0;
451 	struct inpcb *inp;
452 	struct tcpcb *tp = NULL;
453 	struct sockaddr_in *sinp;
454 
455 	sinp = (struct sockaddr_in *)nam;
456 	if (nam->sa_family != AF_INET)
457 		return (EAFNOSUPPORT);
458 	if (nam->sa_len != sizeof (*sinp))
459 		return (EINVAL);
460 
461 	/*
462 	 * Must disallow TCP ``connections'' to multicast addresses.
463 	 */
464 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
465 		return (EAFNOSUPPORT);
466 	if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST)
467 		return (EACCES);
468 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
469 		return (error);
470 
471 	inp = sotoinpcb(so);
472 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
473 	INP_WLOCK(inp);
474 	if (inp->inp_flags & INP_DROPPED) {
475 		error = ECONNREFUSED;
476 		goto out;
477 	}
478 	if (SOLISTENING(so)) {
479 		error = EOPNOTSUPP;
480 		goto out;
481 	}
482 	tp = intotcpcb(inp);
483 	NET_EPOCH_ENTER(et);
484 	if ((error = tcp_connect(tp, sinp, td)) != 0)
485 		goto out_in_epoch;
486 #ifdef TCP_OFFLOAD
487 	if (registered_toedevs > 0 &&
488 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
489 	    (error = tcp_offload_connect(so, nam)) == 0)
490 		goto out_in_epoch;
491 #endif
492 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
493 	error = tcp_output(tp);
494 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
495 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
496 out_in_epoch:
497 	NET_EPOCH_EXIT(et);
498 out:
499 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
500 	INP_WUNLOCK(inp);
501 	return (error);
502 }
503 #endif /* INET */
504 
505 #ifdef INET6
506 static int
507 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
508 {
509 	struct epoch_tracker et;
510 	int error = 0;
511 	struct inpcb *inp;
512 	struct tcpcb *tp = NULL;
513 	struct sockaddr_in6 *sin6;
514 	u_int8_t incflagsav;
515 	u_char vflagsav;
516 
517 	sin6 = (struct sockaddr_in6 *)nam;
518 	if (nam->sa_family != AF_INET6)
519 		return (EAFNOSUPPORT);
520 	if (nam->sa_len != sizeof (*sin6))
521 		return (EINVAL);
522 
523 	/*
524 	 * Must disallow TCP ``connections'' to multicast addresses.
525 	 */
526 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
527 		return (EAFNOSUPPORT);
528 
529 	inp = sotoinpcb(so);
530 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
531 	INP_WLOCK(inp);
532 	vflagsav = inp->inp_vflag;
533 	incflagsav = inp->inp_inc.inc_flags;
534 	if (inp->inp_flags & INP_DROPPED) {
535 		error = ECONNREFUSED;
536 		goto out;
537 	}
538 	if (SOLISTENING(so)) {
539 		error = EINVAL;
540 		goto out;
541 	}
542 	tp = intotcpcb(inp);
543 #ifdef INET
544 	/*
545 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
546 	 * therefore probably require the hash lock, which isn't held here.
547 	 * Is this a significant problem?
548 	 */
549 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
550 		struct sockaddr_in sin;
551 
552 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
553 			error = EINVAL;
554 			goto out;
555 		}
556 		if ((inp->inp_vflag & INP_IPV4) == 0) {
557 			error = EAFNOSUPPORT;
558 			goto out;
559 		}
560 
561 		in6_sin6_2_sin(&sin, sin6);
562 		if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
563 			error = EAFNOSUPPORT;
564 			goto out;
565 		}
566 		if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
567 			error = EACCES;
568 			goto out;
569 		}
570 		if ((error = prison_remote_ip4(td->td_ucred,
571 		    &sin.sin_addr)) != 0)
572 			goto out;
573 		inp->inp_vflag |= INP_IPV4;
574 		inp->inp_vflag &= ~INP_IPV6;
575 		NET_EPOCH_ENTER(et);
576 		if ((error = tcp_connect(tp, &sin, td)) != 0)
577 			goto out_in_epoch;
578 #ifdef TCP_OFFLOAD
579 		if (registered_toedevs > 0 &&
580 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
581 		    (error = tcp_offload_connect(so, nam)) == 0)
582 			goto out_in_epoch;
583 #endif
584 		error = tcp_output(tp);
585 		goto out_in_epoch;
586 	} else {
587 		if ((inp->inp_vflag & INP_IPV6) == 0) {
588 			error = EAFNOSUPPORT;
589 			goto out;
590 		}
591 	}
592 #endif
593 	if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
594 		goto out;
595 	inp->inp_vflag &= ~INP_IPV4;
596 	inp->inp_vflag |= INP_IPV6;
597 	inp->inp_inc.inc_flags |= INC_ISIPV6;
598 	NET_EPOCH_ENTER(et);
599 	if ((error = tcp6_connect(tp, sin6, td)) != 0)
600 		goto out_in_epoch;
601 #ifdef TCP_OFFLOAD
602 	if (registered_toedevs > 0 &&
603 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
604 	    (error = tcp_offload_connect(so, nam)) == 0)
605 		goto out_in_epoch;
606 #endif
607 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
608 	error = tcp_output(tp);
609 out_in_epoch:
610 	NET_EPOCH_EXIT(et);
611 out:
612 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
613 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
614 	/*
615 	 * If the implicit bind in the connect call fails, restore
616 	 * the flags we modified.
617 	 */
618 	if (error != 0 && inp->inp_lport == 0) {
619 		inp->inp_vflag = vflagsav;
620 		inp->inp_inc.inc_flags = incflagsav;
621 	}
622 
623 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
624 	INP_WUNLOCK(inp);
625 	return (error);
626 }
627 #endif /* INET6 */
628 
629 /*
630  * Initiate disconnect from peer.
631  * If connection never passed embryonic stage, just drop;
632  * else if don't need to let data drain, then can just drop anyways,
633  * else have to begin TCP shutdown process: mark socket disconnecting,
634  * drain unread data, state switch to reflect user close, and
635  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
636  * when peer sends FIN and acks ours.
637  *
638  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
639  */
640 static int
641 tcp_usr_disconnect(struct socket *so)
642 {
643 	struct inpcb *inp;
644 	struct tcpcb *tp = NULL;
645 	struct epoch_tracker et;
646 	int error = 0;
647 
648 	NET_EPOCH_ENTER(et);
649 	inp = sotoinpcb(so);
650 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
651 	INP_WLOCK(inp);
652 	if (inp->inp_flags & INP_DROPPED) {
653 		error = ECONNRESET;
654 		goto out;
655 	}
656 	tp = intotcpcb(inp);
657 	if (tp->t_state == TCPS_TIME_WAIT)
658 		goto out;
659 	tcp_disconnect(tp);
660 out:
661 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
662 	INP_WUNLOCK(inp);
663 	NET_EPOCH_EXIT(et);
664 	return (error);
665 }
666 
667 #ifdef INET
668 /*
669  * Accept a connection.  Essentially all the work is done at higher levels;
670  * just return the address of the peer, storing through addr.
671  */
672 static int
673 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
674 {
675 	int error = 0;
676 	struct inpcb *inp = NULL;
677 #ifdef KDTRACE_HOOKS
678 	struct tcpcb *tp = NULL;
679 #endif
680 	struct in_addr addr;
681 	in_port_t port = 0;
682 
683 	if (so->so_state & SS_ISDISCONNECTED)
684 		return (ECONNABORTED);
685 
686 	inp = sotoinpcb(so);
687 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
688 	INP_WLOCK(inp);
689 	if (inp->inp_flags & INP_DROPPED) {
690 		error = ECONNABORTED;
691 		goto out;
692 	}
693 #ifdef KDTRACE_HOOKS
694 	tp = intotcpcb(inp);
695 #endif
696 
697 	/*
698 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
699 	 * copy the data of interest and defer the malloc until after we
700 	 * release the lock.
701 	 */
702 	port = inp->inp_fport;
703 	addr = inp->inp_faddr;
704 
705 out:
706 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
707 	INP_WUNLOCK(inp);
708 	if (error == 0)
709 		*nam = in_sockaddr(port, &addr);
710 	return error;
711 }
712 #endif /* INET */
713 
714 #ifdef INET6
715 static int
716 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
717 {
718 	struct inpcb *inp = NULL;
719 	int error = 0;
720 #ifdef KDTRACE_HOOKS
721 	struct tcpcb *tp = NULL;
722 #endif
723 	struct in_addr addr;
724 	struct in6_addr addr6;
725 	struct epoch_tracker et;
726 	in_port_t port = 0;
727 	int v4 = 0;
728 
729 	if (so->so_state & SS_ISDISCONNECTED)
730 		return (ECONNABORTED);
731 
732 	inp = sotoinpcb(so);
733 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
734 	NET_EPOCH_ENTER(et);
735 	INP_WLOCK(inp);
736 	if (inp->inp_flags & INP_DROPPED) {
737 		error = ECONNABORTED;
738 		goto out;
739 	}
740 #ifdef KDTRACE_HOOKS
741 	tp = intotcpcb(inp);
742 #endif
743 
744 	/*
745 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
746 	 * copy the data of interest and defer the malloc until after we
747 	 * release the lock.
748 	 */
749 	if (inp->inp_vflag & INP_IPV4) {
750 		v4 = 1;
751 		port = inp->inp_fport;
752 		addr = inp->inp_faddr;
753 	} else {
754 		port = inp->inp_fport;
755 		addr6 = inp->in6p_faddr;
756 	}
757 
758 out:
759 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
760 	INP_WUNLOCK(inp);
761 	NET_EPOCH_EXIT(et);
762 	if (error == 0) {
763 		if (v4)
764 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
765 		else
766 			*nam = in6_sockaddr(port, &addr6);
767 	}
768 	return error;
769 }
770 #endif /* INET6 */
771 
772 /*
773  * Mark the connection as being incapable of further output.
774  */
775 static int
776 tcp_usr_shutdown(struct socket *so)
777 {
778 	int error = 0;
779 	struct inpcb *inp;
780 	struct tcpcb *tp = NULL;
781 	struct epoch_tracker et;
782 
783 	inp = sotoinpcb(so);
784 	KASSERT(inp != NULL, ("inp == NULL"));
785 	INP_WLOCK(inp);
786 	if (inp->inp_flags & INP_DROPPED) {
787 		INP_WUNLOCK(inp);
788 		return (ECONNRESET);
789 	}
790 	tp = intotcpcb(inp);
791 	NET_EPOCH_ENTER(et);
792 	socantsendmore(so);
793 	tcp_usrclosed(tp);
794 	if (!(inp->inp_flags & INP_DROPPED))
795 		error = tcp_output_nodrop(tp);
796 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
797 	error = tcp_unlock_or_drop(tp, error);
798 	NET_EPOCH_EXIT(et);
799 
800 	return (error);
801 }
802 
803 /*
804  * After a receive, possibly send window update to peer.
805  */
806 static int
807 tcp_usr_rcvd(struct socket *so, int flags)
808 {
809 	struct epoch_tracker et;
810 	struct inpcb *inp;
811 	struct tcpcb *tp = NULL;
812 	int outrv = 0, error = 0;
813 
814 	inp = sotoinpcb(so);
815 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
816 	INP_WLOCK(inp);
817 	if (inp->inp_flags & INP_DROPPED) {
818 		INP_WUNLOCK(inp);
819 		return (ECONNRESET);
820 	}
821 	tp = intotcpcb(inp);
822 	NET_EPOCH_ENTER(et);
823 	/*
824 	 * For passively-created TFO connections, don't attempt a window
825 	 * update while still in SYN_RECEIVED as this may trigger an early
826 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
827 	 * application response data, or failing that, when the DELACK timer
828 	 * expires.
829 	 */
830 	if (IS_FASTOPEN(tp->t_flags) &&
831 	    (tp->t_state == TCPS_SYN_RECEIVED))
832 		goto out;
833 #ifdef TCP_OFFLOAD
834 	if (tp->t_flags & TF_TOE)
835 		tcp_offload_rcvd(tp);
836 	else
837 #endif
838 		outrv = tcp_output_nodrop(tp);
839 out:
840 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
841 	(void) tcp_unlock_or_drop(tp, outrv);
842 	NET_EPOCH_EXIT(et);
843 	return (error);
844 }
845 
846 /*
847  * Do a send by putting data in output queue and updating urgent
848  * marker if URG set.  Possibly send more data.  Unlike the other
849  * pru_*() routines, the mbuf chains are our responsibility.  We
850  * must either enqueue them or free them.  The other pru_* routines
851  * generally are caller-frees.
852  */
853 static int
854 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
855     struct sockaddr *nam, struct mbuf *control, struct thread *td)
856 {
857 	struct epoch_tracker et;
858 	int error = 0;
859 	struct inpcb *inp;
860 	struct tcpcb *tp = NULL;
861 #ifdef INET
862 #ifdef INET6
863 	struct sockaddr_in sin;
864 #endif
865 	struct sockaddr_in *sinp;
866 #endif
867 #ifdef INET6
868 	struct sockaddr_in6 *sin6;
869 	int isipv6;
870 #endif
871 	u_int8_t incflagsav;
872 	u_char vflagsav;
873 	bool restoreflags;
874 
875 	if (control != NULL) {
876 		/* TCP doesn't do control messages (rights, creds, etc) */
877 		if (control->m_len) {
878 			m_freem(control);
879 			return (EINVAL);
880 		}
881 		m_freem(control);	/* empty control, just free it */
882 	}
883 
884 	inp = sotoinpcb(so);
885 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
886 	INP_WLOCK(inp);
887 	if (inp->inp_flags & INP_DROPPED) {
888 		if (m != NULL && (flags & PRUS_NOTREADY) == 0)
889 			m_freem(m);
890 		INP_WUNLOCK(inp);
891 		return (ECONNRESET);
892 	}
893 
894 	vflagsav = inp->inp_vflag;
895 	incflagsav = inp->inp_inc.inc_flags;
896 	restoreflags = false;
897 	tp = intotcpcb(inp);
898 
899 	NET_EPOCH_ENTER(et);
900 	if ((flags & PRUS_OOB) != 0 &&
901 	    (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
902 		goto out;
903 
904 	if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
905 		if (tp->t_state == TCPS_LISTEN) {
906 			error = EINVAL;
907 			goto out;
908 		}
909 		switch (nam->sa_family) {
910 #ifdef INET
911 		case AF_INET:
912 			sinp = (struct sockaddr_in *)nam;
913 			if (sinp->sin_len != sizeof(struct sockaddr_in)) {
914 				error = EINVAL;
915 				goto out;
916 			}
917 			if ((inp->inp_vflag & INP_IPV6) != 0) {
918 				error = EAFNOSUPPORT;
919 				goto out;
920 			}
921 			if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
922 				error = EAFNOSUPPORT;
923 				goto out;
924 			}
925 			if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
926 				error = EACCES;
927 				goto out;
928 			}
929 			if ((error = prison_remote_ip4(td->td_ucred,
930 			    &sinp->sin_addr)))
931 				goto out;
932 #ifdef INET6
933 			isipv6 = 0;
934 #endif
935 			break;
936 #endif /* INET */
937 #ifdef INET6
938 		case AF_INET6:
939 			sin6 = (struct sockaddr_in6 *)nam;
940 			if (sin6->sin6_len != sizeof(*sin6)) {
941 				error = EINVAL;
942 				goto out;
943 			}
944 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
945 				error = EAFNOSUPPORT;
946 				goto out;
947 			}
948 			if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
949 				error = EAFNOSUPPORT;
950 				goto out;
951 			}
952 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
953 #ifdef INET
954 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
955 					error = EINVAL;
956 					goto out;
957 				}
958 				if ((inp->inp_vflag & INP_IPV4) == 0) {
959 					error = EAFNOSUPPORT;
960 					goto out;
961 				}
962 				restoreflags = true;
963 				inp->inp_vflag &= ~INP_IPV6;
964 				sinp = &sin;
965 				in6_sin6_2_sin(sinp, sin6);
966 				if (IN_MULTICAST(
967 				    ntohl(sinp->sin_addr.s_addr))) {
968 					error = EAFNOSUPPORT;
969 					goto out;
970 				}
971 				if ((error = prison_remote_ip4(td->td_ucred,
972 				    &sinp->sin_addr)))
973 					goto out;
974 				isipv6 = 0;
975 #else /* !INET */
976 				error = EAFNOSUPPORT;
977 				goto out;
978 #endif /* INET */
979 			} else {
980 				if ((inp->inp_vflag & INP_IPV6) == 0) {
981 					error = EAFNOSUPPORT;
982 					goto out;
983 				}
984 				restoreflags = true;
985 				inp->inp_vflag &= ~INP_IPV4;
986 				inp->inp_inc.inc_flags |= INC_ISIPV6;
987 				if ((error = prison_remote_ip6(td->td_ucred,
988 				    &sin6->sin6_addr)))
989 					goto out;
990 				isipv6 = 1;
991 			}
992 			break;
993 #endif /* INET6 */
994 		default:
995 			error = EAFNOSUPPORT;
996 			goto out;
997 		}
998 	}
999 	if (!(flags & PRUS_OOB)) {
1000 		if (tp->t_acktime == 0)
1001 			tp->t_acktime = ticks;
1002 		sbappendstream(&so->so_snd, m, flags);
1003 		m = NULL;
1004 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1005 			KASSERT(tp->t_state == TCPS_CLOSED,
1006 			    ("%s: tp %p is listening", __func__, tp));
1007 
1008 			/*
1009 			 * Do implied connect if not yet connected,
1010 			 * initialize window to default value, and
1011 			 * initialize maxseg using peer's cached MSS.
1012 			 */
1013 #ifdef INET6
1014 			if (isipv6)
1015 				error = tcp6_connect(tp, sin6, td);
1016 #endif /* INET6 */
1017 #if defined(INET6) && defined(INET)
1018 			else
1019 #endif
1020 #ifdef INET
1021 				error = tcp_connect(tp, sinp, td);
1022 #endif
1023 			/*
1024 			 * The bind operation in tcp_connect succeeded. We
1025 			 * no longer want to restore the flags if later
1026 			 * operations fail.
1027 			 */
1028 			if (error == 0 || inp->inp_lport != 0)
1029 				restoreflags = false;
1030 
1031 			if (error) {
1032 				/* m is freed if PRUS_NOTREADY is unset. */
1033 				sbflush(&so->so_snd);
1034 				goto out;
1035 			}
1036 			if (IS_FASTOPEN(tp->t_flags))
1037 				tcp_fastopen_connect(tp);
1038 			else {
1039 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
1040 				tcp_mss(tp, -1);
1041 			}
1042 		}
1043 		if (flags & PRUS_EOF) {
1044 			/*
1045 			 * Close the send side of the connection after
1046 			 * the data is sent.
1047 			 */
1048 			socantsendmore(so);
1049 			tcp_usrclosed(tp);
1050 		}
1051 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1052 		    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1053 		    (tp->t_fbyte_out == 0) &&
1054 		    (so->so_snd.sb_ccc > 0)) {
1055 			tp->t_fbyte_out = ticks;
1056 			if (tp->t_fbyte_out == 0)
1057 				tp->t_fbyte_out = 1;
1058 			if (tp->t_fbyte_out && tp->t_fbyte_in)
1059 				tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1060 		}
1061 		if (!(inp->inp_flags & INP_DROPPED) &&
1062 		    !(flags & PRUS_NOTREADY)) {
1063 			if (flags & PRUS_MORETOCOME)
1064 				tp->t_flags |= TF_MORETOCOME;
1065 			error = tcp_output_nodrop(tp);
1066 			if (flags & PRUS_MORETOCOME)
1067 				tp->t_flags &= ~TF_MORETOCOME;
1068 		}
1069 	} else {
1070 		/*
1071 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1072 		 */
1073 		SOCKBUF_LOCK(&so->so_snd);
1074 		if (sbspace(&so->so_snd) < -512) {
1075 			SOCKBUF_UNLOCK(&so->so_snd);
1076 			error = ENOBUFS;
1077 			goto out;
1078 		}
1079 		/*
1080 		 * According to RFC961 (Assigned Protocols),
1081 		 * the urgent pointer points to the last octet
1082 		 * of urgent data.  We continue, however,
1083 		 * to consider it to indicate the first octet
1084 		 * of data past the urgent section.
1085 		 * Otherwise, snd_up should be one lower.
1086 		 */
1087 		if (tp->t_acktime == 0)
1088 			tp->t_acktime = ticks;
1089 		sbappendstream_locked(&so->so_snd, m, flags);
1090 		SOCKBUF_UNLOCK(&so->so_snd);
1091 		m = NULL;
1092 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1093 			/*
1094 			 * Do implied connect if not yet connected,
1095 			 * initialize window to default value, and
1096 			 * initialize maxseg using peer's cached MSS.
1097 			 */
1098 
1099 			/*
1100 			 * Not going to contemplate SYN|URG
1101 			 */
1102 			if (IS_FASTOPEN(tp->t_flags))
1103 				tp->t_flags &= ~TF_FASTOPEN;
1104 #ifdef INET6
1105 			if (isipv6)
1106 				error = tcp6_connect(tp, sin6, td);
1107 #endif /* INET6 */
1108 #if defined(INET6) && defined(INET)
1109 			else
1110 #endif
1111 #ifdef INET
1112 				error = tcp_connect(tp, sinp, td);
1113 #endif
1114 			/*
1115 			 * The bind operation in tcp_connect succeeded. We
1116 			 * no longer want to restore the flags if later
1117 			 * operations fail.
1118 			 */
1119 			if (error == 0 || inp->inp_lport != 0)
1120 				restoreflags = false;
1121 
1122 			if (error != 0) {
1123 				/* m is freed if PRUS_NOTREADY is unset. */
1124 				sbflush(&so->so_snd);
1125 				goto out;
1126 			}
1127 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1128 			tcp_mss(tp, -1);
1129 		}
1130 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1131 		if ((flags & PRUS_NOTREADY) == 0) {
1132 			tp->t_flags |= TF_FORCEDATA;
1133 			error = tcp_output_nodrop(tp);
1134 			tp->t_flags &= ~TF_FORCEDATA;
1135 		}
1136 	}
1137 	TCP_LOG_EVENT(tp, NULL,
1138 	    &inp->inp_socket->so_rcv,
1139 	    &inp->inp_socket->so_snd,
1140 	    TCP_LOG_USERSEND, error,
1141 	    0, NULL, false);
1142 
1143 out:
1144 	/*
1145 	 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
1146 	 * responsible for freeing memory.
1147 	 */
1148 	if (m != NULL && (flags & PRUS_NOTREADY) == 0)
1149 		m_freem(m);
1150 
1151 	/*
1152 	 * If the request was unsuccessful and we changed flags,
1153 	 * restore the original flags.
1154 	 */
1155 	if (error != 0 && restoreflags) {
1156 		inp->inp_vflag = vflagsav;
1157 		inp->inp_inc.inc_flags = incflagsav;
1158 	}
1159 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1160 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1161 	error = tcp_unlock_or_drop(tp, error);
1162 	NET_EPOCH_EXIT(et);
1163 	return (error);
1164 }
1165 
1166 static int
1167 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1168 {
1169 	struct epoch_tracker et;
1170 	struct inpcb *inp;
1171 	struct tcpcb *tp;
1172 	int error;
1173 
1174 	inp = sotoinpcb(so);
1175 	INP_WLOCK(inp);
1176 	if (inp->inp_flags & INP_DROPPED) {
1177 		INP_WUNLOCK(inp);
1178 		mb_free_notready(m, count);
1179 		return (ECONNRESET);
1180 	}
1181 	tp = intotcpcb(inp);
1182 
1183 	SOCKBUF_LOCK(&so->so_snd);
1184 	error = sbready(&so->so_snd, m, count);
1185 	SOCKBUF_UNLOCK(&so->so_snd);
1186 	if (error) {
1187 		INP_WUNLOCK(inp);
1188 		return (error);
1189 	}
1190 	NET_EPOCH_ENTER(et);
1191 	error = tcp_output_unlock(tp);
1192 	NET_EPOCH_EXIT(et);
1193 
1194 	return (error);
1195 }
1196 
1197 /*
1198  * Abort the TCP.  Drop the connection abruptly.
1199  */
1200 static void
1201 tcp_usr_abort(struct socket *so)
1202 {
1203 	struct inpcb *inp;
1204 	struct tcpcb *tp = NULL;
1205 	struct epoch_tracker et;
1206 
1207 	inp = sotoinpcb(so);
1208 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1209 
1210 	NET_EPOCH_ENTER(et);
1211 	INP_WLOCK(inp);
1212 	KASSERT(inp->inp_socket != NULL,
1213 	    ("tcp_usr_abort: inp_socket == NULL"));
1214 
1215 	/*
1216 	 * If we still have full TCP state, and we're not dropped, drop.
1217 	 */
1218 	if (!(inp->inp_flags & INP_DROPPED)) {
1219 		tp = intotcpcb(inp);
1220 		tp = tcp_drop(tp, ECONNABORTED);
1221 		if (tp == NULL)
1222 			goto dropped;
1223 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1224 	}
1225 	if (!(inp->inp_flags & INP_DROPPED)) {
1226 		soref(so);
1227 		inp->inp_flags |= INP_SOCKREF;
1228 	}
1229 	INP_WUNLOCK(inp);
1230 dropped:
1231 	NET_EPOCH_EXIT(et);
1232 }
1233 
1234 /*
1235  * TCP socket is closed.  Start friendly disconnect.
1236  */
1237 static void
1238 tcp_usr_close(struct socket *so)
1239 {
1240 	struct inpcb *inp;
1241 	struct tcpcb *tp = NULL;
1242 	struct epoch_tracker et;
1243 
1244 	inp = sotoinpcb(so);
1245 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1246 
1247 	NET_EPOCH_ENTER(et);
1248 	INP_WLOCK(inp);
1249 	KASSERT(inp->inp_socket != NULL,
1250 	    ("tcp_usr_close: inp_socket == NULL"));
1251 
1252 	/*
1253 	 * If we are still connected and we're not dropped, initiate
1254 	 * a disconnect.
1255 	 */
1256 	if (!(inp->inp_flags & INP_DROPPED)) {
1257 		tp = intotcpcb(inp);
1258 		if (tp->t_state != TCPS_TIME_WAIT) {
1259 			tp->t_flags |= TF_CLOSED;
1260 			tcp_disconnect(tp);
1261 			TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1262 		}
1263 	}
1264 	if (!(inp->inp_flags & INP_DROPPED)) {
1265 		soref(so);
1266 		inp->inp_flags |= INP_SOCKREF;
1267 	}
1268 	INP_WUNLOCK(inp);
1269 	NET_EPOCH_EXIT(et);
1270 }
1271 
1272 static int
1273 tcp_pru_options_support(struct tcpcb *tp, int flags)
1274 {
1275 	/*
1276 	 * If the specific TCP stack has a pru_options
1277 	 * specified then it does not always support
1278 	 * all the PRU_XX options and we must ask it.
1279 	 * If the function is not specified then all
1280 	 * of the PRU_XX options are supported.
1281 	 */
1282 	int ret = 0;
1283 
1284 	if (tp->t_fb->tfb_pru_options) {
1285 		ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1286 	}
1287 	return (ret);
1288 }
1289 
1290 /*
1291  * Receive out-of-band data.
1292  */
1293 static int
1294 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1295 {
1296 	int error = 0;
1297 	struct inpcb *inp;
1298 	struct tcpcb *tp = NULL;
1299 
1300 	inp = sotoinpcb(so);
1301 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1302 	INP_WLOCK(inp);
1303 	if (inp->inp_flags & INP_DROPPED) {
1304 		error = ECONNRESET;
1305 		goto out;
1306 	}
1307 	tp = intotcpcb(inp);
1308 	error = tcp_pru_options_support(tp, PRUS_OOB);
1309 	if (error) {
1310 		goto out;
1311 	}
1312 	if ((so->so_oobmark == 0 &&
1313 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1314 	    so->so_options & SO_OOBINLINE ||
1315 	    tp->t_oobflags & TCPOOB_HADDATA) {
1316 		error = EINVAL;
1317 		goto out;
1318 	}
1319 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1320 		error = EWOULDBLOCK;
1321 		goto out;
1322 	}
1323 	m->m_len = 1;
1324 	*mtod(m, caddr_t) = tp->t_iobc;
1325 	if ((flags & MSG_PEEK) == 0)
1326 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1327 
1328 out:
1329 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1330 	INP_WUNLOCK(inp);
1331 	return (error);
1332 }
1333 
1334 #ifdef INET
1335 struct protosw tcp_protosw = {
1336 	.pr_type =		SOCK_STREAM,
1337 	.pr_protocol =		IPPROTO_TCP,
1338 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL | PR_WANTRCVD |
1339 				    PR_CAPATTACH,
1340 	.pr_ctloutput =		tcp_ctloutput,
1341 	.pr_abort =		tcp_usr_abort,
1342 	.pr_accept =		tcp_usr_accept,
1343 	.pr_attach =		tcp_usr_attach,
1344 	.pr_bind =		tcp_usr_bind,
1345 	.pr_connect =		tcp_usr_connect,
1346 	.pr_control =		in_control,
1347 	.pr_detach =		tcp_usr_detach,
1348 	.pr_disconnect =	tcp_usr_disconnect,
1349 	.pr_listen =		tcp_usr_listen,
1350 	.pr_peeraddr =		in_getpeeraddr,
1351 	.pr_rcvd =		tcp_usr_rcvd,
1352 	.pr_rcvoob =		tcp_usr_rcvoob,
1353 	.pr_send =		tcp_usr_send,
1354 	.pr_ready =		tcp_usr_ready,
1355 	.pr_shutdown =		tcp_usr_shutdown,
1356 	.pr_sockaddr =		in_getsockaddr,
1357 	.pr_sosetlabel =	in_pcbsosetlabel,
1358 	.pr_close =		tcp_usr_close,
1359 };
1360 #endif /* INET */
1361 
1362 #ifdef INET6
1363 struct protosw tcp6_protosw = {
1364 	.pr_type =		SOCK_STREAM,
1365 	.pr_protocol =		IPPROTO_TCP,
1366 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD |
1367 				    PR_CAPATTACH,
1368 	.pr_ctloutput =		tcp_ctloutput,
1369 	.pr_abort =		tcp_usr_abort,
1370 	.pr_accept =		tcp6_usr_accept,
1371 	.pr_attach =		tcp_usr_attach,
1372 	.pr_bind =		tcp6_usr_bind,
1373 	.pr_connect =		tcp6_usr_connect,
1374 	.pr_control =		in6_control,
1375 	.pr_detach =		tcp_usr_detach,
1376 	.pr_disconnect =	tcp_usr_disconnect,
1377 	.pr_listen =		tcp6_usr_listen,
1378 	.pr_peeraddr =		in6_mapped_peeraddr,
1379 	.pr_rcvd =		tcp_usr_rcvd,
1380 	.pr_rcvoob =		tcp_usr_rcvoob,
1381 	.pr_send =		tcp_usr_send,
1382 	.pr_ready =		tcp_usr_ready,
1383 	.pr_shutdown =		tcp_usr_shutdown,
1384 	.pr_sockaddr =		in6_mapped_sockaddr,
1385 	.pr_sosetlabel =	in_pcbsosetlabel,
1386 	.pr_close =		tcp_usr_close,
1387 };
1388 #endif /* INET6 */
1389 
1390 #ifdef INET
1391 /*
1392  * Common subroutine to open a TCP connection to remote host specified
1393  * by struct sockaddr_in.  Call in_pcbconnect() to choose local host address
1394  * and assign a local port number and install the inpcb into the hash.
1395  * Initialize connection parameters and enter SYN-SENT state.
1396  */
1397 static int
1398 tcp_connect(struct tcpcb *tp, struct sockaddr_in *sin, struct thread *td)
1399 {
1400 	struct inpcb *inp = tptoinpcb(tp);
1401 	struct socket *so = tptosocket(tp);
1402 	int error;
1403 
1404 	NET_EPOCH_ASSERT();
1405 	INP_WLOCK_ASSERT(inp);
1406 
1407 	if (__predict_false((so->so_state &
1408 	    (SS_ISCONNECTING | SS_ISCONNECTED)) != 0))
1409 		return (EISCONN);
1410 
1411 	INP_HASH_WLOCK(&V_tcbinfo);
1412 	error = in_pcbconnect(inp, sin, td->td_ucred, true);
1413 	INP_HASH_WUNLOCK(&V_tcbinfo);
1414 	if (error != 0)
1415 		return (error);
1416 
1417 	/*
1418 	 * Compute window scaling to request:
1419 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1420 	 * XXX: This should move to tcp_output().
1421 	 */
1422 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1423 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1424 		tp->request_r_scale++;
1425 
1426 	soisconnecting(so);
1427 	TCPSTAT_INC(tcps_connattempt);
1428 	tcp_state_change(tp, TCPS_SYN_SENT);
1429 	tp->iss = tcp_new_isn(&inp->inp_inc);
1430 	if (tp->t_flags & TF_REQ_TSTMP)
1431 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1432 	tcp_sendseqinit(tp);
1433 
1434 	return (0);
1435 }
1436 #endif /* INET */
1437 
1438 #ifdef INET6
1439 static int
1440 tcp6_connect(struct tcpcb *tp, struct sockaddr_in6 *sin6, struct thread *td)
1441 {
1442 	struct inpcb *inp = tptoinpcb(tp);
1443 	struct socket *so = tptosocket(tp);
1444 	int error;
1445 
1446 	NET_EPOCH_ASSERT();
1447 	INP_WLOCK_ASSERT(inp);
1448 
1449 	if (__predict_false((so->so_state &
1450 	    (SS_ISCONNECTING | SS_ISCONNECTED)) != 0))
1451 		return (EISCONN);
1452 
1453 	INP_HASH_WLOCK(&V_tcbinfo);
1454 	error = in6_pcbconnect(inp, sin6, td->td_ucred, true);
1455 	INP_HASH_WUNLOCK(&V_tcbinfo);
1456 	if (error != 0)
1457 		return (error);
1458 
1459 	/* Compute window scaling to request.  */
1460 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1461 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1462 		tp->request_r_scale++;
1463 
1464 	soisconnecting(so);
1465 	TCPSTAT_INC(tcps_connattempt);
1466 	tcp_state_change(tp, TCPS_SYN_SENT);
1467 	tp->iss = tcp_new_isn(&inp->inp_inc);
1468 	if (tp->t_flags & TF_REQ_TSTMP)
1469 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1470 	tcp_sendseqinit(tp);
1471 
1472 	return (0);
1473 }
1474 #endif /* INET6 */
1475 
1476 /*
1477  * Export TCP internal state information via a struct tcp_info, based on the
1478  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1479  * (TCP state machine, etc).  We export all information using FreeBSD-native
1480  * constants -- for example, the numeric values for tcpi_state will differ
1481  * from Linux.
1482  */
1483 static void
1484 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1485 {
1486 
1487 	INP_WLOCK_ASSERT(tptoinpcb(tp));
1488 	bzero(ti, sizeof(*ti));
1489 
1490 	ti->tcpi_state = tp->t_state;
1491 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1492 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1493 	if (tp->t_flags & TF_SACK_PERMIT)
1494 		ti->tcpi_options |= TCPI_OPT_SACK;
1495 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1496 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1497 		ti->tcpi_snd_wscale = tp->snd_scale;
1498 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1499 	}
1500 	if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
1501 		ti->tcpi_options |= TCPI_OPT_ECN;
1502 
1503 	ti->tcpi_rto = tp->t_rxtcur * tick;
1504 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1505 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1506 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1507 
1508 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1509 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1510 
1511 	/*
1512 	 * FreeBSD-specific extension fields for tcp_info.
1513 	 */
1514 	ti->tcpi_rcv_space = tp->rcv_wnd;
1515 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1516 	ti->tcpi_snd_wnd = tp->snd_wnd;
1517 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1518 	ti->tcpi_snd_nxt = tp->snd_nxt;
1519 	ti->tcpi_snd_mss = tp->t_maxseg;
1520 	ti->tcpi_rcv_mss = tp->t_maxseg;
1521 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1522 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1523 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1524 #ifdef TCP_OFFLOAD
1525 	if (tp->t_flags & TF_TOE) {
1526 		ti->tcpi_options |= TCPI_OPT_TOE;
1527 		tcp_offload_tcp_info(tp, ti);
1528 	}
1529 #endif
1530 	/*
1531 	 * AccECN related counters.
1532 	 */
1533 	if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) ==
1534 	    (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
1535 		/*
1536 		 * Internal counter starts at 5 for AccECN
1537 		 * but 0 for RFC3168 ECN.
1538 		 */
1539 		ti->tcpi_delivered_ce = tp->t_scep - 5;
1540 	else
1541 		ti->tcpi_delivered_ce = tp->t_scep;
1542 	ti->tcpi_received_ce = tp->t_rcep;
1543 }
1544 
1545 /*
1546  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1547  * socket option arguments.  When it re-acquires the lock after the copy, it
1548  * has to revalidate that the connection is still valid for the socket
1549  * option.
1550  */
1551 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
1552 	INP_WLOCK(inp);							\
1553 	if (inp->inp_flags & INP_DROPPED) {				\
1554 		INP_WUNLOCK(inp);					\
1555 		cleanup;						\
1556 		return (ECONNRESET);					\
1557 	}								\
1558 	tp = intotcpcb(inp);						\
1559 } while(0)
1560 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1561 
1562 int
1563 tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt)
1564 {
1565 	struct socket *so = inp->inp_socket;
1566 	struct tcpcb *tp = intotcpcb(inp);
1567 	int error = 0;
1568 
1569 	MPASS(sopt->sopt_dir == SOPT_SET);
1570 	INP_WLOCK_ASSERT(inp);
1571 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1572 	    ("inp_flags == %x", inp->inp_flags));
1573 	KASSERT(so != NULL, ("inp_socket == NULL"));
1574 
1575 	if (sopt->sopt_level != IPPROTO_TCP) {
1576 		INP_WUNLOCK(inp);
1577 #ifdef INET6
1578 		if (inp->inp_vflag & INP_IPV6PROTO)
1579 			error = ip6_ctloutput(so, sopt);
1580 #endif
1581 #if defined(INET6) && defined(INET)
1582 		else
1583 #endif
1584 #ifdef INET
1585 			error = ip_ctloutput(so, sopt);
1586 #endif
1587 		/*
1588 		 * When an IP-level socket option affects TCP, pass control
1589 		 * down to stack tfb_tcp_ctloutput, otherwise return what
1590 		 * IP level returned.
1591 		 */
1592 		switch (sopt->sopt_level) {
1593 #ifdef INET6
1594 		case IPPROTO_IPV6:
1595 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0)
1596 				return (error);
1597 			switch (sopt->sopt_name) {
1598 			case IPV6_TCLASS:
1599 				/* Notify tcp stacks that care (e.g. RACK). */
1600 				break;
1601 			case IPV6_USE_MIN_MTU:
1602 				/* Update t_maxseg accordingly. */
1603 				break;
1604 			default:
1605 				return (error);
1606 			}
1607 			break;
1608 #endif
1609 #ifdef INET
1610 		case IPPROTO_IP:
1611 			switch (sopt->sopt_name) {
1612 			case IP_TOS:
1613 				inp->inp_ip_tos &= ~IPTOS_ECN_MASK;
1614 				break;
1615 			case IP_TTL:
1616 				/* Notify tcp stacks that care (e.g. RACK). */
1617 				break;
1618 			default:
1619 				return (error);
1620 			}
1621 			break;
1622 #endif
1623 		default:
1624 			return (error);
1625 		}
1626 		INP_WLOCK(inp);
1627 		if (inp->inp_flags & INP_DROPPED) {
1628 			INP_WUNLOCK(inp);
1629 			return (ECONNRESET);
1630 		}
1631 	} else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1632 		/*
1633 		 * Protect the TCP option TCP_FUNCTION_BLK so
1634 		 * that a sub-function can *never* overwrite this.
1635 		 */
1636 		struct tcp_function_set fsn;
1637 		struct tcp_function_block *blk;
1638 
1639 		INP_WUNLOCK(inp);
1640 		error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1641 		if (error)
1642 			return (error);
1643 
1644 		INP_WLOCK(inp);
1645 		if (inp->inp_flags & INP_DROPPED) {
1646 			INP_WUNLOCK(inp);
1647 			return (ECONNRESET);
1648 		}
1649 		tp = intotcpcb(inp);
1650 
1651 		blk = find_and_ref_tcp_functions(&fsn);
1652 		if (blk == NULL) {
1653 			INP_WUNLOCK(inp);
1654 			return (ENOENT);
1655 		}
1656 		if (tp->t_fb == blk) {
1657 			/* You already have this */
1658 			refcount_release(&blk->tfb_refcnt);
1659 			INP_WUNLOCK(inp);
1660 			return (0);
1661 		}
1662 		if (tp->t_state != TCPS_CLOSED) {
1663 			/*
1664 			 * The user has advanced the state
1665 			 * past the initial point, we may not
1666 			 * be able to switch.
1667 			 */
1668 			if (blk->tfb_tcp_handoff_ok != NULL) {
1669 				/*
1670 				 * Does the stack provide a
1671 				 * query mechanism, if so it may
1672 				 * still be possible?
1673 				 */
1674 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1675 			} else
1676 				error = EINVAL;
1677 			if (error) {
1678 				refcount_release(&blk->tfb_refcnt);
1679 				INP_WUNLOCK(inp);
1680 				return(error);
1681 			}
1682 		}
1683 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1684 			refcount_release(&blk->tfb_refcnt);
1685 			INP_WUNLOCK(inp);
1686 			return (ENOENT);
1687 		}
1688 		/*
1689 		 * Release the old refcnt, the
1690 		 * lookup acquired a ref on the
1691 		 * new one already.
1692 		 */
1693 		if (tp->t_fb->tfb_tcp_fb_fini) {
1694 			struct epoch_tracker et;
1695 			/*
1696 			 * Tell the stack to cleanup with 0 i.e.
1697 			 * the tcb is not going away.
1698 			 */
1699 			NET_EPOCH_ENTER(et);
1700 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1701 			NET_EPOCH_EXIT(et);
1702 		}
1703 #ifdef TCPHPTS
1704 		/* Assure that we are not on any hpts */
1705 		tcp_hpts_remove(tptoinpcb(tp));
1706 #endif
1707 		if (blk->tfb_tcp_fb_init) {
1708 			error = (*blk->tfb_tcp_fb_init)(tp);
1709 			if (error) {
1710 				refcount_release(&blk->tfb_refcnt);
1711 				if (tp->t_fb->tfb_tcp_fb_init) {
1712 					if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0)  {
1713 						/* Fall back failed, drop the connection */
1714 						INP_WUNLOCK(inp);
1715 						soabort(so);
1716 						return (error);
1717 					}
1718 				}
1719 				goto err_out;
1720 			}
1721 		}
1722 		refcount_release(&tp->t_fb->tfb_refcnt);
1723 		tp->t_fb = blk;
1724 #ifdef TCP_OFFLOAD
1725 		if (tp->t_flags & TF_TOE) {
1726 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
1727 			     sopt->sopt_name);
1728 		}
1729 #endif
1730 err_out:
1731 		INP_WUNLOCK(inp);
1732 		return (error);
1733 	}
1734 
1735 	/* Pass in the INP locked, callee must unlock it. */
1736 	return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1737 }
1738 
1739 static int
1740 tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1741 {
1742 	struct socket *so = inp->inp_socket;
1743 	struct tcpcb *tp = intotcpcb(inp);
1744 	int error = 0;
1745 
1746 	MPASS(sopt->sopt_dir == SOPT_GET);
1747 	INP_WLOCK_ASSERT(inp);
1748 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1749 	    ("inp_flags == %x", inp->inp_flags));
1750 	KASSERT(so != NULL, ("inp_socket == NULL"));
1751 
1752 	if (sopt->sopt_level != IPPROTO_TCP) {
1753 		INP_WUNLOCK(inp);
1754 #ifdef INET6
1755 		if (inp->inp_vflag & INP_IPV6PROTO)
1756 			error = ip6_ctloutput(so, sopt);
1757 #endif /* INET6 */
1758 #if defined(INET6) && defined(INET)
1759 		else
1760 #endif
1761 #ifdef INET
1762 			error = ip_ctloutput(so, sopt);
1763 #endif
1764 		return (error);
1765 	}
1766 	if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1767 	     (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1768 		struct tcp_function_set fsn;
1769 
1770 		if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1771 			memset(&fsn, 0, sizeof(fsn));
1772 			find_tcp_function_alias(tp->t_fb, &fsn);
1773 		} else {
1774 			strncpy(fsn.function_set_name,
1775 			    tp->t_fb->tfb_tcp_block_name,
1776 			    TCP_FUNCTION_NAME_LEN_MAX);
1777 			fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1778 		}
1779 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1780 		INP_WUNLOCK(inp);
1781 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
1782 		return (error);
1783 	}
1784 
1785 	/* Pass in the INP locked, callee must unlock it. */
1786 	return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1787 }
1788 
1789 int
1790 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1791 {
1792 	struct	inpcb *inp;
1793 
1794 	inp = sotoinpcb(so);
1795 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1796 
1797 	INP_WLOCK(inp);
1798 	if (inp->inp_flags & INP_DROPPED) {
1799 		INP_WUNLOCK(inp);
1800 		return (ECONNRESET);
1801 	}
1802 	if (sopt->sopt_dir == SOPT_SET)
1803 		return (tcp_ctloutput_set(inp, sopt));
1804 	else if (sopt->sopt_dir == SOPT_GET)
1805 		return (tcp_ctloutput_get(inp, sopt));
1806 	else
1807 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
1808 }
1809 
1810 /*
1811  * If this assert becomes untrue, we need to change the size of the buf
1812  * variable in tcp_default_ctloutput().
1813  */
1814 #ifdef CTASSERT
1815 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1816 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1817 #endif
1818 
1819 #ifdef KERN_TLS
1820 static int
1821 copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1822 {
1823 	struct tls_enable_v0 tls_v0;
1824 	int error;
1825 
1826 	if (sopt->sopt_valsize == sizeof(tls_v0)) {
1827 		error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1828 		    sizeof(tls_v0));
1829 		if (error)
1830 			return (error);
1831 		memset(tls, 0, sizeof(*tls));
1832 		tls->cipher_key = tls_v0.cipher_key;
1833 		tls->iv = tls_v0.iv;
1834 		tls->auth_key = tls_v0.auth_key;
1835 		tls->cipher_algorithm = tls_v0.cipher_algorithm;
1836 		tls->cipher_key_len = tls_v0.cipher_key_len;
1837 		tls->iv_len = tls_v0.iv_len;
1838 		tls->auth_algorithm = tls_v0.auth_algorithm;
1839 		tls->auth_key_len = tls_v0.auth_key_len;
1840 		tls->flags = tls_v0.flags;
1841 		tls->tls_vmajor = tls_v0.tls_vmajor;
1842 		tls->tls_vminor = tls_v0.tls_vminor;
1843 		return (0);
1844 	}
1845 
1846 	return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1847 }
1848 #endif
1849 
1850 extern struct cc_algo newreno_cc_algo;
1851 
1852 static int
1853 tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
1854 {
1855 	struct cc_algo *algo;
1856 	void *ptr = NULL;
1857 	struct tcpcb *tp;
1858 	struct cc_var cc_mem;
1859 	char	buf[TCP_CA_NAME_MAX];
1860 	size_t mem_sz;
1861 	int error;
1862 
1863 	INP_WUNLOCK(inp);
1864 	error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1865 	if (error)
1866 		return(error);
1867 	buf[sopt->sopt_valsize] = '\0';
1868 	CC_LIST_RLOCK();
1869 	STAILQ_FOREACH(algo, &cc_list, entries) {
1870 		if (strncmp(buf, algo->name,
1871 			    TCP_CA_NAME_MAX) == 0) {
1872 			if (algo->flags & CC_MODULE_BEING_REMOVED) {
1873 				/* We can't "see" modules being unloaded */
1874 				continue;
1875 			}
1876 			break;
1877 		}
1878 	}
1879 	if (algo == NULL) {
1880 		CC_LIST_RUNLOCK();
1881 		return(ESRCH);
1882 	}
1883 	/*
1884 	 * With a reference the algorithm cannot be removed
1885 	 * so we hold a reference through the change process.
1886 	 */
1887 	cc_refer(algo);
1888 	CC_LIST_RUNLOCK();
1889 	if (algo->cb_init != NULL) {
1890 		/* We can now pre-get the memory for the CC */
1891 		mem_sz = (*algo->cc_data_sz)();
1892 		if (mem_sz == 0) {
1893 			goto no_mem_needed;
1894 		}
1895 		ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
1896 	} else {
1897 no_mem_needed:
1898 		mem_sz = 0;
1899 		ptr = NULL;
1900 	}
1901 	/*
1902 	 * Make sure its all clean and zero and also get
1903 	 * back the inplock.
1904 	 */
1905 	memset(&cc_mem, 0, sizeof(cc_mem));
1906 	INP_WLOCK(inp);
1907 	if (inp->inp_flags & INP_DROPPED) {
1908 		INP_WUNLOCK(inp);
1909 		if (ptr)
1910 			free(ptr, M_CC_MEM);
1911 		/* Release our temp reference */
1912 		CC_LIST_RLOCK();
1913 		cc_release(algo);
1914 		CC_LIST_RUNLOCK();
1915 		return (ECONNRESET);
1916 	}
1917 	tp = intotcpcb(inp);
1918 	if (ptr != NULL)
1919 		memset(ptr, 0, mem_sz);
1920 	cc_mem.ccvc.tcp = tp;
1921 	/*
1922 	 * We once again hold a write lock over the tcb so it's
1923 	 * safe to do these things without ordering concerns.
1924 	 * Note here we init into stack memory.
1925 	 */
1926 	if (algo->cb_init != NULL)
1927 		error = algo->cb_init(&cc_mem, ptr);
1928 	else
1929 		error = 0;
1930 	/*
1931 	 * The CC algorithms, when given their memory
1932 	 * should not fail we could in theory have a
1933 	 * KASSERT here.
1934 	 */
1935 	if (error == 0) {
1936 		/*
1937 		 * Touchdown, lets go ahead and move the
1938 		 * connection to the new CC module by
1939 		 * copying in the cc_mem after we call
1940 		 * the old ones cleanup (if any).
1941 		 */
1942 		if (CC_ALGO(tp)->cb_destroy != NULL)
1943 			CC_ALGO(tp)->cb_destroy(&tp->t_ccv);
1944 		/* Detach the old CC from the tcpcb  */
1945 		cc_detach(tp);
1946 		/* Copy in our temp memory that was inited */
1947 		memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var));
1948 		/* Now attach the new, which takes a reference */
1949 		cc_attach(tp, algo);
1950 		/* Ok now are we where we have gotten past any conn_init? */
1951 		if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
1952 			/* Yep run the connection init for the new CC */
1953 			CC_ALGO(tp)->conn_init(&tp->t_ccv);
1954 		}
1955 	} else if (ptr)
1956 		free(ptr, M_CC_MEM);
1957 	INP_WUNLOCK(inp);
1958 	/* Now lets release our temp reference */
1959 	CC_LIST_RLOCK();
1960 	cc_release(algo);
1961 	CC_LIST_RUNLOCK();
1962 	return (error);
1963 }
1964 
1965 int
1966 tcp_default_ctloutput(struct inpcb *inp, struct sockopt *sopt)
1967 {
1968 	struct tcpcb *tp = intotcpcb(inp);
1969 	int	error, opt, optval;
1970 	u_int	ui;
1971 	struct	tcp_info ti;
1972 #ifdef KERN_TLS
1973 	struct tls_enable tls;
1974 	struct socket *so = inp->inp_socket;
1975 #endif
1976 	char	*pbuf, buf[TCP_LOG_ID_LEN];
1977 #ifdef STATS
1978 	struct statsblob *sbp;
1979 #endif
1980 	size_t	len;
1981 
1982 	INP_WLOCK_ASSERT(inp);
1983 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1984 	    ("inp_flags == %x", inp->inp_flags));
1985 	KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
1986 
1987 	switch (sopt->sopt_level) {
1988 #ifdef INET6
1989 	case IPPROTO_IPV6:
1990 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
1991 		switch (sopt->sopt_name) {
1992 		case IPV6_USE_MIN_MTU:
1993 			tcp6_use_min_mtu(tp);
1994 			/* FALLTHROUGH */
1995 		}
1996 		INP_WUNLOCK(inp);
1997 		return (0);
1998 #endif
1999 #ifdef INET
2000 	case IPPROTO_IP:
2001 		INP_WUNLOCK(inp);
2002 		return (0);
2003 #endif
2004 	}
2005 
2006 	/*
2007 	 * For TCP_CCALGOOPT forward the control to CC module, for both
2008 	 * SOPT_SET and SOPT_GET.
2009 	 */
2010 	switch (sopt->sopt_name) {
2011 	case TCP_CCALGOOPT:
2012 		INP_WUNLOCK(inp);
2013 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2014 			return (EINVAL);
2015 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2016 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2017 		    sopt->sopt_valsize);
2018 		if (error) {
2019 			free(pbuf, M_TEMP);
2020 			return (error);
2021 		}
2022 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2023 		if (CC_ALGO(tp)->ctl_output != NULL)
2024 			error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf);
2025 		else
2026 			error = ENOENT;
2027 		INP_WUNLOCK(inp);
2028 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
2029 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2030 		free(pbuf, M_TEMP);
2031 		return (error);
2032 	}
2033 
2034 	switch (sopt->sopt_dir) {
2035 	case SOPT_SET:
2036 		switch (sopt->sopt_name) {
2037 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2038 		case TCP_MD5SIG:
2039 			INP_WUNLOCK(inp);
2040 			if (!TCPMD5_ENABLED())
2041 				return (ENOPROTOOPT);
2042 			error = TCPMD5_PCBCTL(inp, sopt);
2043 			if (error)
2044 				return (error);
2045 			INP_WLOCK_RECHECK(inp);
2046 			goto unlock_and_done;
2047 #endif /* IPSEC */
2048 
2049 		case TCP_NODELAY:
2050 		case TCP_NOOPT:
2051 		case TCP_LRD:
2052 			INP_WUNLOCK(inp);
2053 			error = sooptcopyin(sopt, &optval, sizeof optval,
2054 			    sizeof optval);
2055 			if (error)
2056 				return (error);
2057 
2058 			INP_WLOCK_RECHECK(inp);
2059 			switch (sopt->sopt_name) {
2060 			case TCP_NODELAY:
2061 				opt = TF_NODELAY;
2062 				break;
2063 			case TCP_NOOPT:
2064 				opt = TF_NOOPT;
2065 				break;
2066 			case TCP_LRD:
2067 				opt = TF_LRD;
2068 				break;
2069 			default:
2070 				opt = 0; /* dead code to fool gcc */
2071 				break;
2072 			}
2073 
2074 			if (optval)
2075 				tp->t_flags |= opt;
2076 			else
2077 				tp->t_flags &= ~opt;
2078 unlock_and_done:
2079 #ifdef TCP_OFFLOAD
2080 			if (tp->t_flags & TF_TOE) {
2081 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
2082 				    sopt->sopt_name);
2083 			}
2084 #endif
2085 			INP_WUNLOCK(inp);
2086 			break;
2087 
2088 		case TCP_NOPUSH:
2089 			INP_WUNLOCK(inp);
2090 			error = sooptcopyin(sopt, &optval, sizeof optval,
2091 			    sizeof optval);
2092 			if (error)
2093 				return (error);
2094 
2095 			INP_WLOCK_RECHECK(inp);
2096 			if (optval)
2097 				tp->t_flags |= TF_NOPUSH;
2098 			else if (tp->t_flags & TF_NOPUSH) {
2099 				tp->t_flags &= ~TF_NOPUSH;
2100 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2101 					struct epoch_tracker et;
2102 
2103 					NET_EPOCH_ENTER(et);
2104 					error = tcp_output_nodrop(tp);
2105 					NET_EPOCH_EXIT(et);
2106 				}
2107 			}
2108 			goto unlock_and_done;
2109 
2110 		case TCP_REMOTE_UDP_ENCAPS_PORT:
2111 			INP_WUNLOCK(inp);
2112 			error = sooptcopyin(sopt, &optval, sizeof optval,
2113 			    sizeof optval);
2114 			if (error)
2115 				return (error);
2116 			if ((optval < TCP_TUNNELING_PORT_MIN) ||
2117 			    (optval > TCP_TUNNELING_PORT_MAX)) {
2118 				/* Its got to be in range */
2119 				return (EINVAL);
2120 			}
2121 			if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
2122 				/* You have to have enabled a UDP tunneling port first */
2123 				return (EINVAL);
2124 			}
2125 			INP_WLOCK_RECHECK(inp);
2126 			if (tp->t_state != TCPS_CLOSED) {
2127 				/* You can't change after you are connected */
2128 				error = EINVAL;
2129 			} else {
2130 				/* Ok we are all good set the port */
2131 				tp->t_port = htons(optval);
2132 			}
2133 			goto unlock_and_done;
2134 
2135 		case TCP_MAXSEG:
2136 			INP_WUNLOCK(inp);
2137 			error = sooptcopyin(sopt, &optval, sizeof optval,
2138 			    sizeof optval);
2139 			if (error)
2140 				return (error);
2141 
2142 			INP_WLOCK_RECHECK(inp);
2143 			if (optval > 0 && optval <= tp->t_maxseg &&
2144 			    optval + 40 >= V_tcp_minmss)
2145 				tp->t_maxseg = optval;
2146 			else
2147 				error = EINVAL;
2148 			goto unlock_and_done;
2149 
2150 		case TCP_INFO:
2151 			INP_WUNLOCK(inp);
2152 			error = EINVAL;
2153 			break;
2154 
2155 		case TCP_STATS:
2156 			INP_WUNLOCK(inp);
2157 #ifdef STATS
2158 			error = sooptcopyin(sopt, &optval, sizeof optval,
2159 			    sizeof optval);
2160 			if (error)
2161 				return (error);
2162 
2163 			if (optval > 0)
2164 				sbp = stats_blob_alloc(
2165 				    V_tcp_perconn_stats_dflt_tpl, 0);
2166 			else
2167 				sbp = NULL;
2168 
2169 			INP_WLOCK_RECHECK(inp);
2170 			if ((tp->t_stats != NULL && sbp == NULL) ||
2171 			    (tp->t_stats == NULL && sbp != NULL)) {
2172 				struct statsblob *t = tp->t_stats;
2173 				tp->t_stats = sbp;
2174 				sbp = t;
2175 			}
2176 			INP_WUNLOCK(inp);
2177 
2178 			stats_blob_destroy(sbp);
2179 #else
2180 			return (EOPNOTSUPP);
2181 #endif /* !STATS */
2182 			break;
2183 
2184 		case TCP_CONGESTION:
2185 			error = tcp_set_cc_mod(inp, sopt);
2186 			break;
2187 
2188 		case TCP_REUSPORT_LB_NUMA:
2189 			INP_WUNLOCK(inp);
2190 			error = sooptcopyin(sopt, &optval, sizeof(optval),
2191 			    sizeof(optval));
2192 			INP_WLOCK_RECHECK(inp);
2193 			if (!error)
2194 				error = in_pcblbgroup_numa(inp, optval);
2195 			INP_WUNLOCK(inp);
2196 			break;
2197 
2198 #ifdef KERN_TLS
2199 		case TCP_TXTLS_ENABLE:
2200 			INP_WUNLOCK(inp);
2201 			error = copyin_tls_enable(sopt, &tls);
2202 			if (error)
2203 				break;
2204 			error = ktls_enable_tx(so, &tls);
2205 			break;
2206 		case TCP_TXTLS_MODE:
2207 			INP_WUNLOCK(inp);
2208 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2209 			if (error)
2210 				return (error);
2211 
2212 			INP_WLOCK_RECHECK(inp);
2213 			error = ktls_set_tx_mode(so, ui);
2214 			INP_WUNLOCK(inp);
2215 			break;
2216 		case TCP_RXTLS_ENABLE:
2217 			INP_WUNLOCK(inp);
2218 			error = sooptcopyin(sopt, &tls, sizeof(tls),
2219 			    sizeof(tls));
2220 			if (error)
2221 				break;
2222 			error = ktls_enable_rx(so, &tls);
2223 			break;
2224 #endif
2225 		case TCP_MAXUNACKTIME:
2226 		case TCP_KEEPIDLE:
2227 		case TCP_KEEPINTVL:
2228 		case TCP_KEEPINIT:
2229 			INP_WUNLOCK(inp);
2230 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2231 			if (error)
2232 				return (error);
2233 
2234 			if (ui > (UINT_MAX / hz)) {
2235 				error = EINVAL;
2236 				break;
2237 			}
2238 			ui *= hz;
2239 
2240 			INP_WLOCK_RECHECK(inp);
2241 			switch (sopt->sopt_name) {
2242 			case TCP_MAXUNACKTIME:
2243 				tp->t_maxunacktime = ui;
2244 				break;
2245 
2246 			case TCP_KEEPIDLE:
2247 				tp->t_keepidle = ui;
2248 				/*
2249 				 * XXX: better check current remaining
2250 				 * timeout and "merge" it with new value.
2251 				 */
2252 				if ((tp->t_state > TCPS_LISTEN) &&
2253 				    (tp->t_state <= TCPS_CLOSING))
2254 					tcp_timer_activate(tp, TT_KEEP,
2255 					    TP_KEEPIDLE(tp));
2256 				break;
2257 			case TCP_KEEPINTVL:
2258 				tp->t_keepintvl = ui;
2259 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2260 				    (TP_MAXIDLE(tp) > 0))
2261 					tcp_timer_activate(tp, TT_2MSL,
2262 					    TP_MAXIDLE(tp));
2263 				break;
2264 			case TCP_KEEPINIT:
2265 				tp->t_keepinit = ui;
2266 				if (tp->t_state == TCPS_SYN_RECEIVED ||
2267 				    tp->t_state == TCPS_SYN_SENT)
2268 					tcp_timer_activate(tp, TT_KEEP,
2269 					    TP_KEEPINIT(tp));
2270 				break;
2271 			}
2272 			goto unlock_and_done;
2273 
2274 		case TCP_KEEPCNT:
2275 			INP_WUNLOCK(inp);
2276 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2277 			if (error)
2278 				return (error);
2279 
2280 			INP_WLOCK_RECHECK(inp);
2281 			tp->t_keepcnt = ui;
2282 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2283 			    (TP_MAXIDLE(tp) > 0))
2284 				tcp_timer_activate(tp, TT_2MSL,
2285 				    TP_MAXIDLE(tp));
2286 			goto unlock_and_done;
2287 
2288 #ifdef TCPPCAP
2289 		case TCP_PCAP_OUT:
2290 		case TCP_PCAP_IN:
2291 			INP_WUNLOCK(inp);
2292 			error = sooptcopyin(sopt, &optval, sizeof optval,
2293 			    sizeof optval);
2294 			if (error)
2295 				return (error);
2296 
2297 			INP_WLOCK_RECHECK(inp);
2298 			if (optval >= 0)
2299 				tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
2300 					&(tp->t_outpkts) : &(tp->t_inpkts),
2301 					optval);
2302 			else
2303 				error = EINVAL;
2304 			goto unlock_and_done;
2305 #endif
2306 
2307 		case TCP_FASTOPEN: {
2308 			struct tcp_fastopen tfo_optval;
2309 
2310 			INP_WUNLOCK(inp);
2311 			if (!V_tcp_fastopen_client_enable &&
2312 			    !V_tcp_fastopen_server_enable)
2313 				return (EPERM);
2314 
2315 			error = sooptcopyin(sopt, &tfo_optval,
2316 				    sizeof(tfo_optval), sizeof(int));
2317 			if (error)
2318 				return (error);
2319 
2320 			INP_WLOCK_RECHECK(inp);
2321 			if ((tp->t_state != TCPS_CLOSED) &&
2322 			    (tp->t_state != TCPS_LISTEN)) {
2323 				error = EINVAL;
2324 				goto unlock_and_done;
2325 			}
2326 			if (tfo_optval.enable) {
2327 				if (tp->t_state == TCPS_LISTEN) {
2328 					if (!V_tcp_fastopen_server_enable) {
2329 						error = EPERM;
2330 						goto unlock_and_done;
2331 					}
2332 
2333 					if (tp->t_tfo_pending == NULL)
2334 						tp->t_tfo_pending =
2335 						    tcp_fastopen_alloc_counter();
2336 				} else {
2337 					/*
2338 					 * If a pre-shared key was provided,
2339 					 * stash it in the client cookie
2340 					 * field of the tcpcb for use during
2341 					 * connect.
2342 					 */
2343 					if (sopt->sopt_valsize ==
2344 					    sizeof(tfo_optval)) {
2345 						memcpy(tp->t_tfo_cookie.client,
2346 						       tfo_optval.psk,
2347 						       TCP_FASTOPEN_PSK_LEN);
2348 						tp->t_tfo_client_cookie_len =
2349 						    TCP_FASTOPEN_PSK_LEN;
2350 					}
2351 				}
2352 				tp->t_flags |= TF_FASTOPEN;
2353 			} else
2354 				tp->t_flags &= ~TF_FASTOPEN;
2355 			goto unlock_and_done;
2356 		}
2357 
2358 #ifdef TCP_BLACKBOX
2359 		case TCP_LOG:
2360 			INP_WUNLOCK(inp);
2361 			error = sooptcopyin(sopt, &optval, sizeof optval,
2362 			    sizeof optval);
2363 			if (error)
2364 				return (error);
2365 
2366 			INP_WLOCK_RECHECK(inp);
2367 			error = tcp_log_state_change(tp, optval);
2368 			goto unlock_and_done;
2369 
2370 		case TCP_LOGBUF:
2371 			INP_WUNLOCK(inp);
2372 			error = EINVAL;
2373 			break;
2374 
2375 		case TCP_LOGID:
2376 			INP_WUNLOCK(inp);
2377 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
2378 			if (error)
2379 				break;
2380 			buf[sopt->sopt_valsize] = '\0';
2381 			INP_WLOCK_RECHECK(inp);
2382 			error = tcp_log_set_id(tp, buf);
2383 			/* tcp_log_set_id() unlocks the INP. */
2384 			break;
2385 
2386 		case TCP_LOGDUMP:
2387 		case TCP_LOGDUMPID:
2388 			INP_WUNLOCK(inp);
2389 			error =
2390 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
2391 			if (error)
2392 				break;
2393 			buf[sopt->sopt_valsize] = '\0';
2394 			INP_WLOCK_RECHECK(inp);
2395 			if (sopt->sopt_name == TCP_LOGDUMP) {
2396 				error = tcp_log_dump_tp_logbuf(tp, buf,
2397 				    M_WAITOK, true);
2398 				INP_WUNLOCK(inp);
2399 			} else {
2400 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
2401 				/*
2402 				 * tcp_log_dump_tp_bucket_logbufs() drops the
2403 				 * INP lock.
2404 				 */
2405 			}
2406 			break;
2407 #endif
2408 
2409 		default:
2410 			INP_WUNLOCK(inp);
2411 			error = ENOPROTOOPT;
2412 			break;
2413 		}
2414 		break;
2415 
2416 	case SOPT_GET:
2417 		tp = intotcpcb(inp);
2418 		switch (sopt->sopt_name) {
2419 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2420 		case TCP_MD5SIG:
2421 			INP_WUNLOCK(inp);
2422 			if (!TCPMD5_ENABLED())
2423 				return (ENOPROTOOPT);
2424 			error = TCPMD5_PCBCTL(inp, sopt);
2425 			break;
2426 #endif
2427 
2428 		case TCP_NODELAY:
2429 			optval = tp->t_flags & TF_NODELAY;
2430 			INP_WUNLOCK(inp);
2431 			error = sooptcopyout(sopt, &optval, sizeof optval);
2432 			break;
2433 		case TCP_MAXSEG:
2434 			optval = tp->t_maxseg;
2435 			INP_WUNLOCK(inp);
2436 			error = sooptcopyout(sopt, &optval, sizeof optval);
2437 			break;
2438 		case TCP_REMOTE_UDP_ENCAPS_PORT:
2439 			optval = ntohs(tp->t_port);
2440 			INP_WUNLOCK(inp);
2441 			error = sooptcopyout(sopt, &optval, sizeof optval);
2442 			break;
2443 		case TCP_NOOPT:
2444 			optval = tp->t_flags & TF_NOOPT;
2445 			INP_WUNLOCK(inp);
2446 			error = sooptcopyout(sopt, &optval, sizeof optval);
2447 			break;
2448 		case TCP_NOPUSH:
2449 			optval = tp->t_flags & TF_NOPUSH;
2450 			INP_WUNLOCK(inp);
2451 			error = sooptcopyout(sopt, &optval, sizeof optval);
2452 			break;
2453 		case TCP_INFO:
2454 			tcp_fill_info(tp, &ti);
2455 			INP_WUNLOCK(inp);
2456 			error = sooptcopyout(sopt, &ti, sizeof ti);
2457 			break;
2458 		case TCP_STATS:
2459 			{
2460 #ifdef STATS
2461 			int nheld;
2462 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2463 
2464 			error = 0;
2465 			socklen_t outsbsz = sopt->sopt_valsize;
2466 			if (tp->t_stats == NULL)
2467 				error = ENOENT;
2468 			else if (outsbsz >= tp->t_stats->cursz)
2469 				outsbsz = tp->t_stats->cursz;
2470 			else if (outsbsz >= sizeof(struct statsblob))
2471 				outsbsz = sizeof(struct statsblob);
2472 			else
2473 				error = EINVAL;
2474 			INP_WUNLOCK(inp);
2475 			if (error)
2476 				break;
2477 
2478 			sbp = sopt->sopt_val;
2479 			nheld = atop(round_page(((vm_offset_t)sbp) +
2480 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2481 			vm_page_t ma[nheld];
2482 			if (vm_fault_quick_hold_pages(
2483 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2484 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2485 			    nheld) < 0) {
2486 				error = EFAULT;
2487 				break;
2488 			}
2489 
2490 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2491 			    SIZEOF_MEMBER(struct statsblob, flags))))
2492 				goto unhold;
2493 
2494 			INP_WLOCK_RECHECK(inp);
2495 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2496 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2497 			INP_WUNLOCK(inp);
2498 			sopt->sopt_valsize = outsbsz;
2499 unhold:
2500 			vm_page_unhold_pages(ma, nheld);
2501 #else
2502 			INP_WUNLOCK(inp);
2503 			error = EOPNOTSUPP;
2504 #endif /* !STATS */
2505 			break;
2506 			}
2507 		case TCP_CONGESTION:
2508 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2509 			INP_WUNLOCK(inp);
2510 			error = sooptcopyout(sopt, buf, len + 1);
2511 			break;
2512 		case TCP_MAXUNACKTIME:
2513 		case TCP_KEEPIDLE:
2514 		case TCP_KEEPINTVL:
2515 		case TCP_KEEPINIT:
2516 		case TCP_KEEPCNT:
2517 			switch (sopt->sopt_name) {
2518 			case TCP_MAXUNACKTIME:
2519 				ui = TP_MAXUNACKTIME(tp) / hz;
2520 				break;
2521 			case TCP_KEEPIDLE:
2522 				ui = TP_KEEPIDLE(tp) / hz;
2523 				break;
2524 			case TCP_KEEPINTVL:
2525 				ui = TP_KEEPINTVL(tp) / hz;
2526 				break;
2527 			case TCP_KEEPINIT:
2528 				ui = TP_KEEPINIT(tp) / hz;
2529 				break;
2530 			case TCP_KEEPCNT:
2531 				ui = TP_KEEPCNT(tp);
2532 				break;
2533 			}
2534 			INP_WUNLOCK(inp);
2535 			error = sooptcopyout(sopt, &ui, sizeof(ui));
2536 			break;
2537 #ifdef TCPPCAP
2538 		case TCP_PCAP_OUT:
2539 		case TCP_PCAP_IN:
2540 			optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
2541 					&(tp->t_outpkts) : &(tp->t_inpkts));
2542 			INP_WUNLOCK(inp);
2543 			error = sooptcopyout(sopt, &optval, sizeof optval);
2544 			break;
2545 #endif
2546 		case TCP_FASTOPEN:
2547 			optval = tp->t_flags & TF_FASTOPEN;
2548 			INP_WUNLOCK(inp);
2549 			error = sooptcopyout(sopt, &optval, sizeof optval);
2550 			break;
2551 #ifdef TCP_BLACKBOX
2552 		case TCP_LOG:
2553 			optval = tp->t_logstate;
2554 			INP_WUNLOCK(inp);
2555 			error = sooptcopyout(sopt, &optval, sizeof(optval));
2556 			break;
2557 		case TCP_LOGBUF:
2558 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2559 			error = tcp_log_getlogbuf(sopt, tp);
2560 			break;
2561 		case TCP_LOGID:
2562 			len = tcp_log_get_id(tp, buf);
2563 			INP_WUNLOCK(inp);
2564 			error = sooptcopyout(sopt, buf, len + 1);
2565 			break;
2566 		case TCP_LOGDUMP:
2567 		case TCP_LOGDUMPID:
2568 			INP_WUNLOCK(inp);
2569 			error = EINVAL;
2570 			break;
2571 #endif
2572 #ifdef KERN_TLS
2573 		case TCP_TXTLS_MODE:
2574 			error = ktls_get_tx_mode(so, &optval);
2575 			INP_WUNLOCK(inp);
2576 			if (error == 0)
2577 				error = sooptcopyout(sopt, &optval,
2578 				    sizeof(optval));
2579 			break;
2580 		case TCP_RXTLS_MODE:
2581 			error = ktls_get_rx_mode(so, &optval);
2582 			INP_WUNLOCK(inp);
2583 			if (error == 0)
2584 				error = sooptcopyout(sopt, &optval,
2585 				    sizeof(optval));
2586 			break;
2587 #endif
2588 		case TCP_LRD:
2589 			optval = tp->t_flags & TF_LRD;
2590 			INP_WUNLOCK(inp);
2591 			error = sooptcopyout(sopt, &optval, sizeof optval);
2592 			break;
2593 		default:
2594 			INP_WUNLOCK(inp);
2595 			error = ENOPROTOOPT;
2596 			break;
2597 		}
2598 		break;
2599 	}
2600 	return (error);
2601 }
2602 #undef INP_WLOCK_RECHECK
2603 #undef INP_WLOCK_RECHECK_CLEANUP
2604 
2605 /*
2606  * Initiate (or continue) disconnect.
2607  * If embryonic state, just send reset (once).
2608  * If in ``let data drain'' option and linger null, just drop.
2609  * Otherwise (hard), mark socket disconnecting and drop
2610  * current input data; switch states based on user close, and
2611  * send segment to peer (with FIN).
2612  */
2613 static void
2614 tcp_disconnect(struct tcpcb *tp)
2615 {
2616 	struct inpcb *inp = tptoinpcb(tp);
2617 	struct socket *so = tptosocket(tp);
2618 
2619 	NET_EPOCH_ASSERT();
2620 	INP_WLOCK_ASSERT(inp);
2621 
2622 	/*
2623 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2624 	 * socket is still open.
2625 	 */
2626 	if (tp->t_state < TCPS_ESTABLISHED &&
2627 	    !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2628 		tp = tcp_close(tp);
2629 		KASSERT(tp != NULL,
2630 		    ("tcp_disconnect: tcp_close() returned NULL"));
2631 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2632 		tp = tcp_drop(tp, 0);
2633 		KASSERT(tp != NULL,
2634 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2635 	} else {
2636 		soisdisconnecting(so);
2637 		sbflush(&so->so_rcv);
2638 		tcp_usrclosed(tp);
2639 		if (!(inp->inp_flags & INP_DROPPED))
2640 			/* Ignore stack's drop request, we already at it. */
2641 			(void)tcp_output_nodrop(tp);
2642 	}
2643 }
2644 
2645 /*
2646  * User issued close, and wish to trail through shutdown states:
2647  * if never received SYN, just forget it.  If got a SYN from peer,
2648  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2649  * If already got a FIN from peer, then almost done; go to LAST_ACK
2650  * state.  In all other cases, have already sent FIN to peer (e.g.
2651  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2652  * for peer to send FIN or not respond to keep-alives, etc.
2653  * We can let the user exit from the close as soon as the FIN is acked.
2654  */
2655 static void
2656 tcp_usrclosed(struct tcpcb *tp)
2657 {
2658 
2659 	NET_EPOCH_ASSERT();
2660 	INP_WLOCK_ASSERT(tptoinpcb(tp));
2661 
2662 	switch (tp->t_state) {
2663 	case TCPS_LISTEN:
2664 #ifdef TCP_OFFLOAD
2665 		tcp_offload_listen_stop(tp);
2666 #endif
2667 		tcp_state_change(tp, TCPS_CLOSED);
2668 		/* FALLTHROUGH */
2669 	case TCPS_CLOSED:
2670 		tp = tcp_close(tp);
2671 		/*
2672 		 * tcp_close() should never return NULL here as the socket is
2673 		 * still open.
2674 		 */
2675 		KASSERT(tp != NULL,
2676 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2677 		break;
2678 
2679 	case TCPS_SYN_SENT:
2680 	case TCPS_SYN_RECEIVED:
2681 		tp->t_flags |= TF_NEEDFIN;
2682 		break;
2683 
2684 	case TCPS_ESTABLISHED:
2685 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2686 		break;
2687 
2688 	case TCPS_CLOSE_WAIT:
2689 		tcp_state_change(tp, TCPS_LAST_ACK);
2690 		break;
2691 	}
2692 	if (tp->t_acktime == 0)
2693 		tp->t_acktime = ticks;
2694 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
2695 		soisdisconnected(tptosocket(tp));
2696 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
2697 		if (tp->t_state == TCPS_FIN_WAIT_2) {
2698 			int timeout;
2699 
2700 			timeout = (tcp_fast_finwait2_recycle) ?
2701 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2702 			tcp_timer_activate(tp, TT_2MSL, timeout);
2703 		}
2704 	}
2705 }
2706 
2707 #ifdef DDB
2708 static void
2709 db_print_indent(int indent)
2710 {
2711 	int i;
2712 
2713 	for (i = 0; i < indent; i++)
2714 		db_printf(" ");
2715 }
2716 
2717 static void
2718 db_print_tstate(int t_state)
2719 {
2720 
2721 	switch (t_state) {
2722 	case TCPS_CLOSED:
2723 		db_printf("TCPS_CLOSED");
2724 		return;
2725 
2726 	case TCPS_LISTEN:
2727 		db_printf("TCPS_LISTEN");
2728 		return;
2729 
2730 	case TCPS_SYN_SENT:
2731 		db_printf("TCPS_SYN_SENT");
2732 		return;
2733 
2734 	case TCPS_SYN_RECEIVED:
2735 		db_printf("TCPS_SYN_RECEIVED");
2736 		return;
2737 
2738 	case TCPS_ESTABLISHED:
2739 		db_printf("TCPS_ESTABLISHED");
2740 		return;
2741 
2742 	case TCPS_CLOSE_WAIT:
2743 		db_printf("TCPS_CLOSE_WAIT");
2744 		return;
2745 
2746 	case TCPS_FIN_WAIT_1:
2747 		db_printf("TCPS_FIN_WAIT_1");
2748 		return;
2749 
2750 	case TCPS_CLOSING:
2751 		db_printf("TCPS_CLOSING");
2752 		return;
2753 
2754 	case TCPS_LAST_ACK:
2755 		db_printf("TCPS_LAST_ACK");
2756 		return;
2757 
2758 	case TCPS_FIN_WAIT_2:
2759 		db_printf("TCPS_FIN_WAIT_2");
2760 		return;
2761 
2762 	case TCPS_TIME_WAIT:
2763 		db_printf("TCPS_TIME_WAIT");
2764 		return;
2765 
2766 	default:
2767 		db_printf("unknown");
2768 		return;
2769 	}
2770 }
2771 
2772 static void
2773 db_print_tflags(u_int t_flags)
2774 {
2775 	int comma;
2776 
2777 	comma = 0;
2778 	if (t_flags & TF_ACKNOW) {
2779 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2780 		comma = 1;
2781 	}
2782 	if (t_flags & TF_DELACK) {
2783 		db_printf("%sTF_DELACK", comma ? ", " : "");
2784 		comma = 1;
2785 	}
2786 	if (t_flags & TF_NODELAY) {
2787 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2788 		comma = 1;
2789 	}
2790 	if (t_flags & TF_NOOPT) {
2791 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2792 		comma = 1;
2793 	}
2794 	if (t_flags & TF_SENTFIN) {
2795 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2796 		comma = 1;
2797 	}
2798 	if (t_flags & TF_REQ_SCALE) {
2799 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2800 		comma = 1;
2801 	}
2802 	if (t_flags & TF_RCVD_SCALE) {
2803 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2804 		comma = 1;
2805 	}
2806 	if (t_flags & TF_REQ_TSTMP) {
2807 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2808 		comma = 1;
2809 	}
2810 	if (t_flags & TF_RCVD_TSTMP) {
2811 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2812 		comma = 1;
2813 	}
2814 	if (t_flags & TF_SACK_PERMIT) {
2815 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2816 		comma = 1;
2817 	}
2818 	if (t_flags & TF_NEEDSYN) {
2819 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2820 		comma = 1;
2821 	}
2822 	if (t_flags & TF_NEEDFIN) {
2823 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2824 		comma = 1;
2825 	}
2826 	if (t_flags & TF_NOPUSH) {
2827 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2828 		comma = 1;
2829 	}
2830 	if (t_flags & TF_PREVVALID) {
2831 		db_printf("%sTF_PREVVALID", comma ? ", " : "");
2832 		comma = 1;
2833 	}
2834 	if (t_flags & TF_MORETOCOME) {
2835 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2836 		comma = 1;
2837 	}
2838 	if (t_flags & TF_SONOTCONN) {
2839 		db_printf("%sTF_SONOTCONN", comma ? ", " : "");
2840 		comma = 1;
2841 	}
2842 	if (t_flags & TF_LASTIDLE) {
2843 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2844 		comma = 1;
2845 	}
2846 	if (t_flags & TF_RXWIN0SENT) {
2847 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2848 		comma = 1;
2849 	}
2850 	if (t_flags & TF_FASTRECOVERY) {
2851 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2852 		comma = 1;
2853 	}
2854 	if (t_flags & TF_CONGRECOVERY) {
2855 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2856 		comma = 1;
2857 	}
2858 	if (t_flags & TF_WASFRECOVERY) {
2859 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2860 		comma = 1;
2861 	}
2862 	if (t_flags & TF_WASCRECOVERY) {
2863 		db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
2864 		comma = 1;
2865 	}
2866 	if (t_flags & TF_SIGNATURE) {
2867 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2868 		comma = 1;
2869 	}
2870 	if (t_flags & TF_FORCEDATA) {
2871 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2872 		comma = 1;
2873 	}
2874 	if (t_flags & TF_TSO) {
2875 		db_printf("%sTF_TSO", comma ? ", " : "");
2876 		comma = 1;
2877 	}
2878 	if (t_flags & TF_FASTOPEN) {
2879 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2880 		comma = 1;
2881 	}
2882 }
2883 
2884 static void
2885 db_print_tflags2(u_int t_flags2)
2886 {
2887 	int comma;
2888 
2889 	comma = 0;
2890 	if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
2891 		db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
2892 		comma = 1;
2893 	}
2894 	if (t_flags2 & TF2_PLPMTU_PMTUD) {
2895 		db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
2896 		comma = 1;
2897 	}
2898 	if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
2899 		db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
2900 		comma = 1;
2901 	}
2902 	if (t_flags2 & TF2_LOG_AUTO) {
2903 		db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
2904 		comma = 1;
2905 	}
2906 	if (t_flags2 & TF2_DROP_AF_DATA) {
2907 		db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
2908 		comma = 1;
2909 	}
2910 	if (t_flags2 & TF2_ECN_PERMIT) {
2911 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
2912 		comma = 1;
2913 	}
2914 	if (t_flags2 & TF2_ECN_SND_CWR) {
2915 		db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
2916 		comma = 1;
2917 	}
2918 	if (t_flags2 & TF2_ECN_SND_ECE) {
2919 		db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
2920 		comma = 1;
2921 	}
2922 	if (t_flags2 & TF2_ACE_PERMIT) {
2923 		db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
2924 		comma = 1;
2925 	}
2926 	if (t_flags2 & TF2_FBYTES_COMPLETE) {
2927 		db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
2928 		comma = 1;
2929 	}
2930 }
2931 
2932 static void
2933 db_print_toobflags(char t_oobflags)
2934 {
2935 	int comma;
2936 
2937 	comma = 0;
2938 	if (t_oobflags & TCPOOB_HAVEDATA) {
2939 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2940 		comma = 1;
2941 	}
2942 	if (t_oobflags & TCPOOB_HADDATA) {
2943 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2944 		comma = 1;
2945 	}
2946 }
2947 
2948 static void
2949 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2950 {
2951 
2952 	db_print_indent(indent);
2953 	db_printf("%s at %p\n", name, tp);
2954 
2955 	indent += 2;
2956 
2957 	db_print_indent(indent);
2958 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
2959 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2960 
2961 	db_print_indent(indent);
2962 	db_printf("t_callout: %p   t_timers: %p\n",
2963 	    &tp->t_callout, &tp->t_timers);
2964 
2965 	db_print_indent(indent);
2966 	db_printf("t_state: %d (", tp->t_state);
2967 	db_print_tstate(tp->t_state);
2968 	db_printf(")\n");
2969 
2970 	db_print_indent(indent);
2971 	db_printf("t_flags: 0x%x (", tp->t_flags);
2972 	db_print_tflags(tp->t_flags);
2973 	db_printf(")\n");
2974 
2975 	db_print_indent(indent);
2976 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
2977 	db_print_tflags2(tp->t_flags2);
2978 	db_printf(")\n");
2979 
2980 	db_print_indent(indent);
2981 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: 0x%08x\n",
2982 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
2983 
2984 	db_print_indent(indent);
2985 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
2986 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2987 
2988 	db_print_indent(indent);
2989 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
2990 	    tp->iss, tp->irs, tp->rcv_nxt);
2991 
2992 	db_print_indent(indent);
2993 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
2994 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2995 
2996 	db_print_indent(indent);
2997 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
2998 	   tp->snd_wnd, tp->snd_cwnd);
2999 
3000 	db_print_indent(indent);
3001 	db_printf("snd_ssthresh: %u   snd_recover: "
3002 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3003 
3004 	db_print_indent(indent);
3005 	db_printf("t_rcvtime: %u   t_startime: %u\n",
3006 	    tp->t_rcvtime, tp->t_starttime);
3007 
3008 	db_print_indent(indent);
3009 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
3010 	    tp->t_rtttime, tp->t_rtseq);
3011 
3012 	db_print_indent(indent);
3013 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
3014 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3015 
3016 	db_print_indent(indent);
3017 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u\n",
3018 	    tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin);
3019 
3020 	db_print_indent(indent);
3021 	db_printf("t_rttupdated: %u   max_sndwnd: %u   t_softerror: %d\n",
3022 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3023 
3024 	db_print_indent(indent);
3025 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3026 	db_print_toobflags(tp->t_oobflags);
3027 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
3028 
3029 	db_print_indent(indent);
3030 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
3031 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3032 
3033 	db_print_indent(indent);
3034 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
3035 	    tp->ts_recent, tp->ts_recent_age);
3036 
3037 	db_print_indent(indent);
3038 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
3039 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3040 
3041 	db_print_indent(indent);
3042 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
3043 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3044 	    tp->snd_recover_prev, tp->t_badrxtwin);
3045 
3046 	db_print_indent(indent);
3047 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
3048 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3049 
3050 	db_print_indent(indent);
3051 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
3052 	    tp->snd_fack, tp->rcv_numsacks);
3053 
3054 	/* Skip sackblks, sackhint. */
3055 
3056 	db_print_indent(indent);
3057 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
3058 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3059 }
3060 
3061 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3062 {
3063 	struct tcpcb *tp;
3064 
3065 	if (!have_addr) {
3066 		db_printf("usage: show tcpcb <addr>\n");
3067 		return;
3068 	}
3069 	tp = (struct tcpcb *)addr;
3070 
3071 	db_print_tcpcb(tp, "tcpcb", 0);
3072 }
3073 #endif
3074