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