xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 4c053c17f2c8a715988f215d16284879857ca376)
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_RECHECK(inp);
1713 	} else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1714 		/*
1715 		 * Protect the TCP option TCP_FUNCTION_BLK so
1716 		 * that a sub-function can *never* overwrite this.
1717 		 */
1718 		struct tcp_function_set fsn;
1719 		struct tcp_function_block *blk;
1720 		void *ptr = NULL;
1721 
1722 		INP_WUNLOCK(inp);
1723 		error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1724 		if (error)
1725 			return (error);
1726 
1727 		INP_WLOCK_RECHECK(inp);
1728 
1729 		blk = find_and_ref_tcp_functions(&fsn);
1730 		if (blk == NULL) {
1731 			INP_WUNLOCK(inp);
1732 			return (ENOENT);
1733 		}
1734 		if (tp->t_fb == blk) {
1735 			/* You already have this */
1736 			refcount_release(&blk->tfb_refcnt);
1737 			INP_WUNLOCK(inp);
1738 			return (0);
1739 		}
1740 		if (tp->t_state != TCPS_CLOSED) {
1741 			/*
1742 			 * The user has advanced the state
1743 			 * past the initial point, we may not
1744 			 * be able to switch.
1745 			 */
1746 			if (blk->tfb_tcp_handoff_ok != NULL) {
1747 				/*
1748 				 * Does the stack provide a
1749 				 * query mechanism, if so it may
1750 				 * still be possible?
1751 				 */
1752 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1753 			} else
1754 				error = EINVAL;
1755 			if (error) {
1756 				refcount_release(&blk->tfb_refcnt);
1757 				INP_WUNLOCK(inp);
1758 				return(error);
1759 			}
1760 		}
1761 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1762 			refcount_release(&blk->tfb_refcnt);
1763 			INP_WUNLOCK(inp);
1764 			return (ENOENT);
1765 		}
1766 		/*
1767 		 * Ensure the new stack takes ownership with a
1768 		 * clean slate on peak rate threshold.
1769 		 */
1770 		if (tp->t_fb->tfb_tcp_timer_stop_all != NULL)
1771 			tp->t_fb->tfb_tcp_timer_stop_all(tp);
1772 		if (blk->tfb_tcp_fb_init) {
1773 			error = (*blk->tfb_tcp_fb_init)(tp, &ptr);
1774 			if (error) {
1775 				/*
1776 				 * Release the ref count the lookup
1777 				 * acquired.
1778 				 */
1779 				refcount_release(&blk->tfb_refcnt);
1780 				/*
1781 				 * Now there is a chance that the
1782 				 * init() function mucked with some
1783 				 * things before it failed, such as
1784 				 * hpts or inp_flags2 or timer granularity.
1785 				 * It should not of, but lets give the old
1786 				 * stack a chance to reset to a known good state.
1787 				 */
1788 				if (tp->t_fb->tfb_switch_failed) {
1789 					(*tp->t_fb->tfb_switch_failed)(tp);
1790 				}
1791 			 	goto err_out;
1792 			}
1793 		}
1794 		if (tp->t_fb->tfb_tcp_fb_fini) {
1795 			struct epoch_tracker et;
1796 			/*
1797 			 * Tell the stack to cleanup with 0 i.e.
1798 			 * the tcb is not going away.
1799 			 */
1800 			NET_EPOCH_ENTER(et);
1801 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1802 			NET_EPOCH_EXIT(et);
1803 		}
1804 		/*
1805 		 * Release the old refcnt, the
1806 		 * lookup acquired a ref on the
1807 		 * new one already.
1808 		 */
1809 		refcount_release(&tp->t_fb->tfb_refcnt);
1810 		/*
1811 		 * Set in the new stack.
1812 		 */
1813 		tp->t_fb = blk;
1814 		tp->t_fb_ptr = ptr;
1815 #ifdef TCP_OFFLOAD
1816 		if (tp->t_flags & TF_TOE) {
1817 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
1818 			     sopt->sopt_name);
1819 		}
1820 #endif
1821 err_out:
1822 		INP_WUNLOCK(inp);
1823 		return (error);
1824 
1825 	}
1826 
1827 	/* Pass in the INP locked, callee must unlock it. */
1828 	return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1829 }
1830 
1831 static int
1832 tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1833 {
1834 	struct socket *so = inp->inp_socket;
1835 	struct tcpcb *tp = intotcpcb(inp);
1836 	int error = 0;
1837 
1838 	MPASS(sopt->sopt_dir == SOPT_GET);
1839 	INP_WLOCK_ASSERT(inp);
1840 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1841 	    ("inp_flags == %x", inp->inp_flags));
1842 	KASSERT(so != NULL, ("inp_socket == NULL"));
1843 
1844 	if (sopt->sopt_level != IPPROTO_TCP) {
1845 		INP_WUNLOCK(inp);
1846 #ifdef INET6
1847 		if (inp->inp_vflag & INP_IPV6PROTO)
1848 			error = ip6_ctloutput(so, sopt);
1849 #endif /* INET6 */
1850 #if defined(INET6) && defined(INET)
1851 		else
1852 #endif
1853 #ifdef INET
1854 			error = ip_ctloutput(so, sopt);
1855 #endif
1856 		return (error);
1857 	}
1858 	if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1859 	     (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1860 		struct tcp_function_set fsn;
1861 
1862 		if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1863 			memset(&fsn, 0, sizeof(fsn));
1864 			find_tcp_function_alias(tp->t_fb, &fsn);
1865 		} else {
1866 			strncpy(fsn.function_set_name,
1867 			    tp->t_fb->tfb_tcp_block_name,
1868 			    TCP_FUNCTION_NAME_LEN_MAX);
1869 			fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1870 		}
1871 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1872 		INP_WUNLOCK(inp);
1873 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
1874 		return (error);
1875 	}
1876 
1877 	/* Pass in the INP locked, callee must unlock it. */
1878 	return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1879 }
1880 
1881 int
1882 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1883 {
1884 	struct	inpcb *inp;
1885 
1886 	inp = sotoinpcb(so);
1887 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1888 
1889 	INP_WLOCK(inp);
1890 	if (inp->inp_flags & INP_DROPPED) {
1891 		INP_WUNLOCK(inp);
1892 		return (ECONNRESET);
1893 	}
1894 	if (sopt->sopt_dir == SOPT_SET)
1895 		return (tcp_ctloutput_set(inp, sopt));
1896 	else if (sopt->sopt_dir == SOPT_GET)
1897 		return (tcp_ctloutput_get(inp, sopt));
1898 	else
1899 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
1900 }
1901 
1902 /*
1903  * If this assert becomes untrue, we need to change the size of the buf
1904  * variable in tcp_default_ctloutput().
1905  */
1906 #ifdef CTASSERT
1907 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1908 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1909 #endif
1910 
1911 extern struct cc_algo newreno_cc_algo;
1912 
1913 static int
1914 tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
1915 {
1916 	struct cc_algo *algo;
1917 	void *ptr = NULL;
1918 	struct tcpcb *tp;
1919 	struct cc_var cc_mem;
1920 	char	buf[TCP_CA_NAME_MAX];
1921 	size_t mem_sz;
1922 	int error;
1923 
1924 	INP_WUNLOCK(inp);
1925 	error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1926 	if (error)
1927 		return(error);
1928 	buf[sopt->sopt_valsize] = '\0';
1929 	CC_LIST_RLOCK();
1930 	STAILQ_FOREACH(algo, &cc_list, entries) {
1931 		if (strncmp(buf, algo->name,
1932 			    TCP_CA_NAME_MAX) == 0) {
1933 			if (algo->flags & CC_MODULE_BEING_REMOVED) {
1934 				/* We can't "see" modules being unloaded */
1935 				continue;
1936 			}
1937 			break;
1938 		}
1939 	}
1940 	if (algo == NULL) {
1941 		CC_LIST_RUNLOCK();
1942 		return(ESRCH);
1943 	}
1944 	/*
1945 	 * With a reference the algorithm cannot be removed
1946 	 * so we hold a reference through the change process.
1947 	 */
1948 	cc_refer(algo);
1949 	CC_LIST_RUNLOCK();
1950 	if (algo->cb_init != NULL) {
1951 		/* We can now pre-get the memory for the CC */
1952 		mem_sz = (*algo->cc_data_sz)();
1953 		if (mem_sz == 0) {
1954 			goto no_mem_needed;
1955 		}
1956 		ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
1957 	} else {
1958 no_mem_needed:
1959 		mem_sz = 0;
1960 		ptr = NULL;
1961 	}
1962 	/*
1963 	 * Make sure its all clean and zero and also get
1964 	 * back the inplock.
1965 	 */
1966 	memset(&cc_mem, 0, sizeof(cc_mem));
1967 	INP_WLOCK(inp);
1968 	if (inp->inp_flags & INP_DROPPED) {
1969 		INP_WUNLOCK(inp);
1970 		if (ptr)
1971 			free(ptr, M_CC_MEM);
1972 		/* Release our temp reference */
1973 		CC_LIST_RLOCK();
1974 		cc_release(algo);
1975 		CC_LIST_RUNLOCK();
1976 		return (ECONNRESET);
1977 	}
1978 	tp = intotcpcb(inp);
1979 	if (ptr != NULL)
1980 		memset(ptr, 0, mem_sz);
1981 	cc_mem.ccvc.tcp = tp;
1982 	/*
1983 	 * We once again hold a write lock over the tcb so it's
1984 	 * safe to do these things without ordering concerns.
1985 	 * Note here we init into stack memory.
1986 	 */
1987 	if (algo->cb_init != NULL)
1988 		error = algo->cb_init(&cc_mem, ptr);
1989 	else
1990 		error = 0;
1991 	/*
1992 	 * The CC algorithms, when given their memory
1993 	 * should not fail we could in theory have a
1994 	 * KASSERT here.
1995 	 */
1996 	if (error == 0) {
1997 		/*
1998 		 * Touchdown, lets go ahead and move the
1999 		 * connection to the new CC module by
2000 		 * copying in the cc_mem after we call
2001 		 * the old ones cleanup (if any).
2002 		 */
2003 		if (CC_ALGO(tp)->cb_destroy != NULL)
2004 			CC_ALGO(tp)->cb_destroy(&tp->t_ccv);
2005 		/* Detach the old CC from the tcpcb  */
2006 		cc_detach(tp);
2007 		/* Copy in our temp memory that was inited */
2008 		memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var));
2009 		/* Now attach the new, which takes a reference */
2010 		cc_attach(tp, algo);
2011 		/* Ok now are we where we have gotten past any conn_init? */
2012 		if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
2013 			/* Yep run the connection init for the new CC */
2014 			CC_ALGO(tp)->conn_init(&tp->t_ccv);
2015 		}
2016 	} else if (ptr)
2017 		free(ptr, M_CC_MEM);
2018 	INP_WUNLOCK(inp);
2019 	/* Now lets release our temp reference */
2020 	CC_LIST_RLOCK();
2021 	cc_release(algo);
2022 	CC_LIST_RUNLOCK();
2023 	return (error);
2024 }
2025 
2026 int
2027 tcp_default_ctloutput(struct tcpcb *tp, struct sockopt *sopt)
2028 {
2029 	struct inpcb *inp = tptoinpcb(tp);
2030 	int	error, opt, optval;
2031 	u_int	ui;
2032 	struct	tcp_info ti;
2033 #ifdef KERN_TLS
2034 	struct tls_enable tls;
2035 	struct socket *so = inp->inp_socket;
2036 #endif
2037 	char	*pbuf, buf[TCP_LOG_ID_LEN];
2038 #ifdef STATS
2039 	struct statsblob *sbp;
2040 #endif
2041 	size_t	len;
2042 
2043 	INP_WLOCK_ASSERT(inp);
2044 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
2045 	    ("inp_flags == %x", inp->inp_flags));
2046 	KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
2047 
2048 	switch (sopt->sopt_level) {
2049 #ifdef INET6
2050 	case IPPROTO_IPV6:
2051 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
2052 		switch (sopt->sopt_name) {
2053 		case IPV6_USE_MIN_MTU:
2054 			tcp6_use_min_mtu(tp);
2055 			/* FALLTHROUGH */
2056 		}
2057 		INP_WUNLOCK(inp);
2058 		return (0);
2059 #endif
2060 #ifdef INET
2061 	case IPPROTO_IP:
2062 		INP_WUNLOCK(inp);
2063 		return (0);
2064 #endif
2065 	}
2066 
2067 	/*
2068 	 * For TCP_CCALGOOPT forward the control to CC module, for both
2069 	 * SOPT_SET and SOPT_GET.
2070 	 */
2071 	switch (sopt->sopt_name) {
2072 	case TCP_CCALGOOPT:
2073 		INP_WUNLOCK(inp);
2074 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2075 			return (EINVAL);
2076 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2077 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2078 		    sopt->sopt_valsize);
2079 		if (error) {
2080 			free(pbuf, M_TEMP);
2081 			return (error);
2082 		}
2083 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2084 		if (CC_ALGO(tp)->ctl_output != NULL)
2085 			error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf);
2086 		else
2087 			error = ENOENT;
2088 		INP_WUNLOCK(inp);
2089 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
2090 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2091 		free(pbuf, M_TEMP);
2092 		return (error);
2093 	}
2094 
2095 	switch (sopt->sopt_dir) {
2096 	case SOPT_SET:
2097 		switch (sopt->sopt_name) {
2098 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2099 		case TCP_MD5SIG:
2100 			INP_WUNLOCK(inp);
2101 			if (!TCPMD5_ENABLED())
2102 				return (ENOPROTOOPT);
2103 			error = TCPMD5_PCBCTL(inp, sopt);
2104 			if (error)
2105 				return (error);
2106 			INP_WLOCK_RECHECK(inp);
2107 			goto unlock_and_done;
2108 #endif /* IPSEC */
2109 
2110 		case TCP_NODELAY:
2111 		case TCP_NOOPT:
2112 			INP_WUNLOCK(inp);
2113 			error = sooptcopyin(sopt, &optval, sizeof optval,
2114 			    sizeof optval);
2115 			if (error)
2116 				return (error);
2117 
2118 			INP_WLOCK_RECHECK(inp);
2119 			switch (sopt->sopt_name) {
2120 			case TCP_NODELAY:
2121 				opt = TF_NODELAY;
2122 				break;
2123 			case TCP_NOOPT:
2124 				opt = TF_NOOPT;
2125 				break;
2126 			default:
2127 				opt = 0; /* dead code to fool gcc */
2128 				break;
2129 			}
2130 
2131 			if (optval)
2132 				tp->t_flags |= opt;
2133 			else
2134 				tp->t_flags &= ~opt;
2135 unlock_and_done:
2136 #ifdef TCP_OFFLOAD
2137 			if (tp->t_flags & TF_TOE) {
2138 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
2139 				    sopt->sopt_name);
2140 			}
2141 #endif
2142 			INP_WUNLOCK(inp);
2143 			break;
2144 
2145 		case TCP_NOPUSH:
2146 			INP_WUNLOCK(inp);
2147 			error = sooptcopyin(sopt, &optval, sizeof optval,
2148 			    sizeof optval);
2149 			if (error)
2150 				return (error);
2151 
2152 			INP_WLOCK_RECHECK(inp);
2153 			if (optval)
2154 				tp->t_flags |= TF_NOPUSH;
2155 			else if (tp->t_flags & TF_NOPUSH) {
2156 				tp->t_flags &= ~TF_NOPUSH;
2157 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2158 					struct epoch_tracker et;
2159 
2160 					NET_EPOCH_ENTER(et);
2161 					error = tcp_output_nodrop(tp);
2162 					NET_EPOCH_EXIT(et);
2163 				}
2164 			}
2165 			goto unlock_and_done;
2166 
2167 		case TCP_REMOTE_UDP_ENCAPS_PORT:
2168 			INP_WUNLOCK(inp);
2169 			error = sooptcopyin(sopt, &optval, sizeof optval,
2170 			    sizeof optval);
2171 			if (error)
2172 				return (error);
2173 			if ((optval < TCP_TUNNELING_PORT_MIN) ||
2174 			    (optval > TCP_TUNNELING_PORT_MAX)) {
2175 				/* Its got to be in range */
2176 				return (EINVAL);
2177 			}
2178 			if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
2179 				/* You have to have enabled a UDP tunneling port first */
2180 				return (EINVAL);
2181 			}
2182 			INP_WLOCK_RECHECK(inp);
2183 			if (tp->t_state != TCPS_CLOSED) {
2184 				/* You can't change after you are connected */
2185 				error = EINVAL;
2186 			} else {
2187 				/* Ok we are all good set the port */
2188 				tp->t_port = htons(optval);
2189 			}
2190 			goto unlock_and_done;
2191 
2192 		case TCP_MAXSEG:
2193 			INP_WUNLOCK(inp);
2194 			error = sooptcopyin(sopt, &optval, sizeof optval,
2195 			    sizeof optval);
2196 			if (error)
2197 				return (error);
2198 
2199 			INP_WLOCK_RECHECK(inp);
2200 			if (optval > 0 && optval <= tp->t_maxseg &&
2201 			    optval + 40 >= V_tcp_minmss) {
2202 				tp->t_maxseg = optval;
2203 				if (tp->t_maxseg < V_tcp_mssdflt) {
2204 					/*
2205 					 * The MSS is so small we should not process incoming
2206 					 * SACK's since we are subject to attack in such a
2207 					 * case.
2208 					 */
2209 					tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT;
2210 				} else {
2211 					tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT;
2212 				}
2213 			} else
2214 				error = EINVAL;
2215 			goto unlock_and_done;
2216 
2217 		case TCP_INFO:
2218 			INP_WUNLOCK(inp);
2219 			error = EINVAL;
2220 			break;
2221 
2222 		case TCP_STATS:
2223 			INP_WUNLOCK(inp);
2224 #ifdef STATS
2225 			error = sooptcopyin(sopt, &optval, sizeof optval,
2226 			    sizeof optval);
2227 			if (error)
2228 				return (error);
2229 
2230 			if (optval > 0)
2231 				sbp = stats_blob_alloc(
2232 				    V_tcp_perconn_stats_dflt_tpl, 0);
2233 			else
2234 				sbp = NULL;
2235 
2236 			INP_WLOCK_RECHECK(inp);
2237 			if ((tp->t_stats != NULL && sbp == NULL) ||
2238 			    (tp->t_stats == NULL && sbp != NULL)) {
2239 				struct statsblob *t = tp->t_stats;
2240 				tp->t_stats = sbp;
2241 				sbp = t;
2242 			}
2243 			INP_WUNLOCK(inp);
2244 
2245 			stats_blob_destroy(sbp);
2246 #else
2247 			return (EOPNOTSUPP);
2248 #endif /* !STATS */
2249 			break;
2250 
2251 		case TCP_CONGESTION:
2252 			error = tcp_set_cc_mod(inp, sopt);
2253 			break;
2254 
2255 		case TCP_REUSPORT_LB_NUMA:
2256 			INP_WUNLOCK(inp);
2257 			error = sooptcopyin(sopt, &optval, sizeof(optval),
2258 			    sizeof(optval));
2259 			INP_WLOCK_RECHECK(inp);
2260 			if (!error)
2261 				error = in_pcblbgroup_numa(inp, optval);
2262 			INP_WUNLOCK(inp);
2263 			break;
2264 
2265 #ifdef KERN_TLS
2266 		case TCP_TXTLS_ENABLE:
2267 			INP_WUNLOCK(inp);
2268 			error = ktls_copyin_tls_enable(sopt, &tls);
2269 			if (error != 0)
2270 				break;
2271 			error = ktls_enable_tx(so, &tls);
2272 			ktls_cleanup_tls_enable(&tls);
2273 			break;
2274 		case TCP_TXTLS_MODE:
2275 			INP_WUNLOCK(inp);
2276 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2277 			if (error != 0)
2278 				return (error);
2279 
2280 			INP_WLOCK_RECHECK(inp);
2281 			error = ktls_set_tx_mode(so, ui);
2282 			INP_WUNLOCK(inp);
2283 			break;
2284 		case TCP_RXTLS_ENABLE:
2285 			INP_WUNLOCK(inp);
2286 			error = ktls_copyin_tls_enable(sopt, &tls);
2287 			if (error != 0)
2288 				break;
2289 			error = ktls_enable_rx(so, &tls);
2290 			ktls_cleanup_tls_enable(&tls);
2291 			break;
2292 #endif
2293 		case TCP_MAXUNACKTIME:
2294 		case TCP_KEEPIDLE:
2295 		case TCP_KEEPINTVL:
2296 		case TCP_KEEPINIT:
2297 			INP_WUNLOCK(inp);
2298 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2299 			if (error)
2300 				return (error);
2301 
2302 			if (ui > (UINT_MAX / hz)) {
2303 				error = EINVAL;
2304 				break;
2305 			}
2306 			ui *= hz;
2307 
2308 			INP_WLOCK_RECHECK(inp);
2309 			switch (sopt->sopt_name) {
2310 			case TCP_MAXUNACKTIME:
2311 				tp->t_maxunacktime = ui;
2312 				break;
2313 
2314 			case TCP_KEEPIDLE:
2315 				tp->t_keepidle = ui;
2316 				/*
2317 				 * XXX: better check current remaining
2318 				 * timeout and "merge" it with new value.
2319 				 */
2320 				if ((tp->t_state > TCPS_LISTEN) &&
2321 				    (tp->t_state <= TCPS_CLOSING))
2322 					tcp_timer_activate(tp, TT_KEEP,
2323 					    TP_KEEPIDLE(tp));
2324 				break;
2325 			case TCP_KEEPINTVL:
2326 				tp->t_keepintvl = ui;
2327 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2328 				    (TP_MAXIDLE(tp) > 0))
2329 					tcp_timer_activate(tp, TT_2MSL,
2330 					    TP_MAXIDLE(tp));
2331 				break;
2332 			case TCP_KEEPINIT:
2333 				tp->t_keepinit = ui;
2334 				if (tp->t_state == TCPS_SYN_RECEIVED ||
2335 				    tp->t_state == TCPS_SYN_SENT)
2336 					tcp_timer_activate(tp, TT_KEEP,
2337 					    TP_KEEPINIT(tp));
2338 				break;
2339 			}
2340 			goto unlock_and_done;
2341 
2342 		case TCP_KEEPCNT:
2343 			INP_WUNLOCK(inp);
2344 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2345 			if (error)
2346 				return (error);
2347 
2348 			INP_WLOCK_RECHECK(inp);
2349 			tp->t_keepcnt = ui;
2350 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2351 			    (TP_MAXIDLE(tp) > 0))
2352 				tcp_timer_activate(tp, TT_2MSL,
2353 				    TP_MAXIDLE(tp));
2354 			goto unlock_and_done;
2355 
2356 #ifdef TCPPCAP
2357 		case TCP_PCAP_OUT:
2358 		case TCP_PCAP_IN:
2359 			INP_WUNLOCK(inp);
2360 			error = sooptcopyin(sopt, &optval, sizeof optval,
2361 			    sizeof optval);
2362 			if (error)
2363 				return (error);
2364 
2365 			INP_WLOCK_RECHECK(inp);
2366 			if (optval >= 0)
2367 				tcp_pcap_set_sock_max(
2368 					(sopt->sopt_name == TCP_PCAP_OUT) ?
2369 					&(tp->t_outpkts) : &(tp->t_inpkts),
2370 					optval);
2371 			else
2372 				error = EINVAL;
2373 			goto unlock_and_done;
2374 #endif
2375 
2376 		case TCP_FASTOPEN: {
2377 			struct tcp_fastopen tfo_optval;
2378 
2379 			INP_WUNLOCK(inp);
2380 			if (!V_tcp_fastopen_client_enable &&
2381 			    !V_tcp_fastopen_server_enable)
2382 				return (EPERM);
2383 
2384 			error = sooptcopyin(sopt, &tfo_optval,
2385 				    sizeof(tfo_optval), sizeof(int));
2386 			if (error)
2387 				return (error);
2388 
2389 			INP_WLOCK_RECHECK(inp);
2390 			if ((tp->t_state != TCPS_CLOSED) &&
2391 			    (tp->t_state != TCPS_LISTEN)) {
2392 				error = EINVAL;
2393 				goto unlock_and_done;
2394 			}
2395 			if (tfo_optval.enable) {
2396 				if (tp->t_state == TCPS_LISTEN) {
2397 					if (!V_tcp_fastopen_server_enable) {
2398 						error = EPERM;
2399 						goto unlock_and_done;
2400 					}
2401 
2402 					if (tp->t_tfo_pending == NULL)
2403 						tp->t_tfo_pending =
2404 						    tcp_fastopen_alloc_counter();
2405 				} else {
2406 					/*
2407 					 * If a pre-shared key was provided,
2408 					 * stash it in the client cookie
2409 					 * field of the tcpcb for use during
2410 					 * connect.
2411 					 */
2412 					if (sopt->sopt_valsize ==
2413 					    sizeof(tfo_optval)) {
2414 						memcpy(tp->t_tfo_cookie.client,
2415 						       tfo_optval.psk,
2416 						       TCP_FASTOPEN_PSK_LEN);
2417 						tp->t_tfo_client_cookie_len =
2418 						    TCP_FASTOPEN_PSK_LEN;
2419 					}
2420 				}
2421 				tp->t_flags |= TF_FASTOPEN;
2422 			} else
2423 				tp->t_flags &= ~TF_FASTOPEN;
2424 			goto unlock_and_done;
2425 		}
2426 
2427 #ifdef TCP_BLACKBOX
2428 		case TCP_LOG:
2429 			INP_WUNLOCK(inp);
2430 			error = sooptcopyin(sopt, &optval, sizeof optval,
2431 			    sizeof optval);
2432 			if (error)
2433 				return (error);
2434 
2435 			INP_WLOCK_RECHECK(inp);
2436 			error = tcp_log_state_change(tp, optval);
2437 			goto unlock_and_done;
2438 
2439 		case TCP_LOGBUF:
2440 			INP_WUNLOCK(inp);
2441 			error = EINVAL;
2442 			break;
2443 
2444 		case TCP_LOGID:
2445 			INP_WUNLOCK(inp);
2446 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
2447 			if (error)
2448 				break;
2449 			buf[sopt->sopt_valsize] = '\0';
2450 			INP_WLOCK_RECHECK(inp);
2451 			error = tcp_log_set_id(tp, buf);
2452 			/* tcp_log_set_id() unlocks the INP. */
2453 			break;
2454 
2455 		case TCP_LOGDUMP:
2456 		case TCP_LOGDUMPID:
2457 			INP_WUNLOCK(inp);
2458 			error =
2459 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
2460 			if (error)
2461 				break;
2462 			buf[sopt->sopt_valsize] = '\0';
2463 			INP_WLOCK_RECHECK(inp);
2464 			if (sopt->sopt_name == TCP_LOGDUMP) {
2465 				error = tcp_log_dump_tp_logbuf(tp, buf,
2466 				    M_WAITOK, true);
2467 				INP_WUNLOCK(inp);
2468 			} else {
2469 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
2470 				/*
2471 				 * tcp_log_dump_tp_bucket_logbufs() drops the
2472 				 * INP lock.
2473 				 */
2474 			}
2475 			break;
2476 #endif
2477 
2478 		default:
2479 			INP_WUNLOCK(inp);
2480 			error = ENOPROTOOPT;
2481 			break;
2482 		}
2483 		break;
2484 
2485 	case SOPT_GET:
2486 		tp = intotcpcb(inp);
2487 		switch (sopt->sopt_name) {
2488 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2489 		case TCP_MD5SIG:
2490 			INP_WUNLOCK(inp);
2491 			if (!TCPMD5_ENABLED())
2492 				return (ENOPROTOOPT);
2493 			error = TCPMD5_PCBCTL(inp, sopt);
2494 			break;
2495 #endif
2496 
2497 		case TCP_NODELAY:
2498 			optval = tp->t_flags & TF_NODELAY;
2499 			INP_WUNLOCK(inp);
2500 			error = sooptcopyout(sopt, &optval, sizeof optval);
2501 			break;
2502 		case TCP_MAXSEG:
2503 			optval = tp->t_maxseg;
2504 			INP_WUNLOCK(inp);
2505 			error = sooptcopyout(sopt, &optval, sizeof optval);
2506 			break;
2507 		case TCP_REMOTE_UDP_ENCAPS_PORT:
2508 			optval = ntohs(tp->t_port);
2509 			INP_WUNLOCK(inp);
2510 			error = sooptcopyout(sopt, &optval, sizeof optval);
2511 			break;
2512 		case TCP_NOOPT:
2513 			optval = tp->t_flags & TF_NOOPT;
2514 			INP_WUNLOCK(inp);
2515 			error = sooptcopyout(sopt, &optval, sizeof optval);
2516 			break;
2517 		case TCP_NOPUSH:
2518 			optval = tp->t_flags & TF_NOPUSH;
2519 			INP_WUNLOCK(inp);
2520 			error = sooptcopyout(sopt, &optval, sizeof optval);
2521 			break;
2522 		case TCP_INFO:
2523 			tcp_fill_info(tp, &ti);
2524 			INP_WUNLOCK(inp);
2525 			error = sooptcopyout(sopt, &ti, sizeof ti);
2526 			break;
2527 		case TCP_STATS:
2528 			{
2529 #ifdef STATS
2530 			int nheld;
2531 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2532 
2533 			error = 0;
2534 			socklen_t outsbsz = sopt->sopt_valsize;
2535 			if (tp->t_stats == NULL)
2536 				error = ENOENT;
2537 			else if (outsbsz >= tp->t_stats->cursz)
2538 				outsbsz = tp->t_stats->cursz;
2539 			else if (outsbsz >= sizeof(struct statsblob))
2540 				outsbsz = sizeof(struct statsblob);
2541 			else
2542 				error = EINVAL;
2543 			INP_WUNLOCK(inp);
2544 			if (error)
2545 				break;
2546 
2547 			sbp = sopt->sopt_val;
2548 			nheld = atop(round_page(((vm_offset_t)sbp) +
2549 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2550 			vm_page_t ma[nheld];
2551 			if (vm_fault_quick_hold_pages(
2552 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2553 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2554 			    nheld) < 0) {
2555 				error = EFAULT;
2556 				break;
2557 			}
2558 
2559 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2560 			    SIZEOF_MEMBER(struct statsblob, flags))))
2561 				goto unhold;
2562 
2563 			INP_WLOCK_RECHECK(inp);
2564 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2565 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2566 			INP_WUNLOCK(inp);
2567 			sopt->sopt_valsize = outsbsz;
2568 unhold:
2569 			vm_page_unhold_pages(ma, nheld);
2570 #else
2571 			INP_WUNLOCK(inp);
2572 			error = EOPNOTSUPP;
2573 #endif /* !STATS */
2574 			break;
2575 			}
2576 		case TCP_CONGESTION:
2577 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2578 			INP_WUNLOCK(inp);
2579 			error = sooptcopyout(sopt, buf, len + 1);
2580 			break;
2581 		case TCP_MAXUNACKTIME:
2582 		case TCP_KEEPIDLE:
2583 		case TCP_KEEPINTVL:
2584 		case TCP_KEEPINIT:
2585 		case TCP_KEEPCNT:
2586 			switch (sopt->sopt_name) {
2587 			case TCP_MAXUNACKTIME:
2588 				ui = TP_MAXUNACKTIME(tp) / hz;
2589 				break;
2590 			case TCP_KEEPIDLE:
2591 				ui = TP_KEEPIDLE(tp) / hz;
2592 				break;
2593 			case TCP_KEEPINTVL:
2594 				ui = TP_KEEPINTVL(tp) / hz;
2595 				break;
2596 			case TCP_KEEPINIT:
2597 				ui = TP_KEEPINIT(tp) / hz;
2598 				break;
2599 			case TCP_KEEPCNT:
2600 				ui = TP_KEEPCNT(tp);
2601 				break;
2602 			}
2603 			INP_WUNLOCK(inp);
2604 			error = sooptcopyout(sopt, &ui, sizeof(ui));
2605 			break;
2606 #ifdef TCPPCAP
2607 		case TCP_PCAP_OUT:
2608 		case TCP_PCAP_IN:
2609 			optval = tcp_pcap_get_sock_max(
2610 					(sopt->sopt_name == TCP_PCAP_OUT) ?
2611 					&(tp->t_outpkts) : &(tp->t_inpkts));
2612 			INP_WUNLOCK(inp);
2613 			error = sooptcopyout(sopt, &optval, sizeof optval);
2614 			break;
2615 #endif
2616 		case TCP_FASTOPEN:
2617 			optval = tp->t_flags & TF_FASTOPEN;
2618 			INP_WUNLOCK(inp);
2619 			error = sooptcopyout(sopt, &optval, sizeof optval);
2620 			break;
2621 #ifdef TCP_BLACKBOX
2622 		case TCP_LOG:
2623 			optval = tcp_get_bblog_state(tp);
2624 			INP_WUNLOCK(inp);
2625 			error = sooptcopyout(sopt, &optval, sizeof(optval));
2626 			break;
2627 		case TCP_LOGBUF:
2628 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2629 			error = tcp_log_getlogbuf(sopt, tp);
2630 			break;
2631 		case TCP_LOGID:
2632 			len = tcp_log_get_id(tp, buf);
2633 			INP_WUNLOCK(inp);
2634 			error = sooptcopyout(sopt, buf, len + 1);
2635 			break;
2636 		case TCP_LOGDUMP:
2637 		case TCP_LOGDUMPID:
2638 			INP_WUNLOCK(inp);
2639 			error = EINVAL;
2640 			break;
2641 #endif
2642 #ifdef KERN_TLS
2643 		case TCP_TXTLS_MODE:
2644 			error = ktls_get_tx_mode(so, &optval);
2645 			INP_WUNLOCK(inp);
2646 			if (error == 0)
2647 				error = sooptcopyout(sopt, &optval,
2648 				    sizeof(optval));
2649 			break;
2650 		case TCP_RXTLS_MODE:
2651 			error = ktls_get_rx_mode(so, &optval);
2652 			INP_WUNLOCK(inp);
2653 			if (error == 0)
2654 				error = sooptcopyout(sopt, &optval,
2655 				    sizeof(optval));
2656 			break;
2657 #endif
2658 		default:
2659 			INP_WUNLOCK(inp);
2660 			error = ENOPROTOOPT;
2661 			break;
2662 		}
2663 		break;
2664 	}
2665 	return (error);
2666 }
2667 #undef INP_WLOCK_RECHECK
2668 #undef INP_WLOCK_RECHECK_CLEANUP
2669 
2670 /*
2671  * Initiate (or continue) disconnect.
2672  * If embryonic state, just send reset (once).
2673  * If in ``let data drain'' option and linger null, just drop.
2674  * Otherwise (hard), mark socket disconnecting and drop
2675  * current input data; switch states based on user close, and
2676  * send segment to peer (with FIN).
2677  */
2678 static void
2679 tcp_disconnect(struct tcpcb *tp)
2680 {
2681 	struct inpcb *inp = tptoinpcb(tp);
2682 	struct socket *so = tptosocket(tp);
2683 
2684 	NET_EPOCH_ASSERT();
2685 	INP_WLOCK_ASSERT(inp);
2686 
2687 	/*
2688 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2689 	 * socket is still open.
2690 	 */
2691 	if (tp->t_state < TCPS_ESTABLISHED &&
2692 	    !(tp->t_state > TCPS_LISTEN && (tp->t_flags & TF_FASTOPEN))) {
2693 		tp = tcp_close(tp);
2694 		KASSERT(tp != NULL,
2695 		    ("tcp_disconnect: tcp_close() returned NULL"));
2696 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2697 		tp = tcp_drop(tp, 0);
2698 		KASSERT(tp != NULL,
2699 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2700 	} else {
2701 		soisdisconnecting(so);
2702 		sbflush(&so->so_rcv);
2703 		tcp_usrclosed(tp);
2704 		if (!(inp->inp_flags & INP_DROPPED))
2705 			/* Ignore stack's drop request, we already at it. */
2706 			(void)tcp_output_nodrop(tp);
2707 	}
2708 }
2709 
2710 /*
2711  * User issued close, and wish to trail through shutdown states:
2712  * if never received SYN, just forget it.  If got a SYN from peer,
2713  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2714  * If already got a FIN from peer, then almost done; go to LAST_ACK
2715  * state.  In all other cases, have already sent FIN to peer (e.g.
2716  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2717  * for peer to send FIN or not respond to keep-alives, etc.
2718  * We can let the user exit from the close as soon as the FIN is acked.
2719  */
2720 static void
2721 tcp_usrclosed(struct tcpcb *tp)
2722 {
2723 
2724 	NET_EPOCH_ASSERT();
2725 	INP_WLOCK_ASSERT(tptoinpcb(tp));
2726 
2727 	switch (tp->t_state) {
2728 	case TCPS_LISTEN:
2729 #ifdef TCP_OFFLOAD
2730 		tcp_offload_listen_stop(tp);
2731 #endif
2732 		tcp_state_change(tp, TCPS_CLOSED);
2733 		/* FALLTHROUGH */
2734 	case TCPS_CLOSED:
2735 		tp = tcp_close(tp);
2736 		/*
2737 		 * tcp_close() should never return NULL here as the socket is
2738 		 * still open.
2739 		 */
2740 		KASSERT(tp != NULL,
2741 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2742 		break;
2743 
2744 	case TCPS_SYN_SENT:
2745 	case TCPS_SYN_RECEIVED:
2746 		tp->t_flags |= TF_NEEDFIN;
2747 		break;
2748 
2749 	case TCPS_ESTABLISHED:
2750 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2751 		break;
2752 
2753 	case TCPS_CLOSE_WAIT:
2754 		tcp_state_change(tp, TCPS_LAST_ACK);
2755 		break;
2756 	}
2757 	if (tp->t_acktime == 0)
2758 		tp->t_acktime = ticks;
2759 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
2760 		tcp_free_sackholes(tp);
2761 		soisdisconnected(tptosocket(tp));
2762 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
2763 		if (tp->t_state == TCPS_FIN_WAIT_2) {
2764 			int timeout;
2765 
2766 			timeout = (tcp_fast_finwait2_recycle) ?
2767 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2768 			tcp_timer_activate(tp, TT_2MSL, timeout);
2769 		}
2770 	}
2771 }
2772 
2773 #ifdef DDB
2774 static void
2775 db_print_indent(int indent)
2776 {
2777 	int i;
2778 
2779 	for (i = 0; i < indent; i++)
2780 		db_printf(" ");
2781 }
2782 
2783 static void
2784 db_print_tstate(int t_state)
2785 {
2786 
2787 	switch (t_state) {
2788 	case TCPS_CLOSED:
2789 		db_printf("TCPS_CLOSED");
2790 		return;
2791 
2792 	case TCPS_LISTEN:
2793 		db_printf("TCPS_LISTEN");
2794 		return;
2795 
2796 	case TCPS_SYN_SENT:
2797 		db_printf("TCPS_SYN_SENT");
2798 		return;
2799 
2800 	case TCPS_SYN_RECEIVED:
2801 		db_printf("TCPS_SYN_RECEIVED");
2802 		return;
2803 
2804 	case TCPS_ESTABLISHED:
2805 		db_printf("TCPS_ESTABLISHED");
2806 		return;
2807 
2808 	case TCPS_CLOSE_WAIT:
2809 		db_printf("TCPS_CLOSE_WAIT");
2810 		return;
2811 
2812 	case TCPS_FIN_WAIT_1:
2813 		db_printf("TCPS_FIN_WAIT_1");
2814 		return;
2815 
2816 	case TCPS_CLOSING:
2817 		db_printf("TCPS_CLOSING");
2818 		return;
2819 
2820 	case TCPS_LAST_ACK:
2821 		db_printf("TCPS_LAST_ACK");
2822 		return;
2823 
2824 	case TCPS_FIN_WAIT_2:
2825 		db_printf("TCPS_FIN_WAIT_2");
2826 		return;
2827 
2828 	case TCPS_TIME_WAIT:
2829 		db_printf("TCPS_TIME_WAIT");
2830 		return;
2831 
2832 	default:
2833 		db_printf("unknown");
2834 		return;
2835 	}
2836 }
2837 
2838 static void
2839 db_print_tflags(u_int t_flags)
2840 {
2841 	int comma;
2842 
2843 	comma = 0;
2844 	if (t_flags & TF_ACKNOW) {
2845 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2846 		comma = 1;
2847 	}
2848 	if (t_flags & TF_DELACK) {
2849 		db_printf("%sTF_DELACK", comma ? ", " : "");
2850 		comma = 1;
2851 	}
2852 	if (t_flags & TF_NODELAY) {
2853 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2854 		comma = 1;
2855 	}
2856 	if (t_flags & TF_NOOPT) {
2857 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2858 		comma = 1;
2859 	}
2860 	if (t_flags & TF_SENTFIN) {
2861 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2862 		comma = 1;
2863 	}
2864 	if (t_flags & TF_REQ_SCALE) {
2865 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2866 		comma = 1;
2867 	}
2868 	if (t_flags & TF_RCVD_SCALE) {
2869 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2870 		comma = 1;
2871 	}
2872 	if (t_flags & TF_REQ_TSTMP) {
2873 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2874 		comma = 1;
2875 	}
2876 	if (t_flags & TF_RCVD_TSTMP) {
2877 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2878 		comma = 1;
2879 	}
2880 	if (t_flags & TF_SACK_PERMIT) {
2881 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2882 		comma = 1;
2883 	}
2884 	if (t_flags & TF_NEEDSYN) {
2885 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2886 		comma = 1;
2887 	}
2888 	if (t_flags & TF_NEEDFIN) {
2889 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2890 		comma = 1;
2891 	}
2892 	if (t_flags & TF_NOPUSH) {
2893 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2894 		comma = 1;
2895 	}
2896 	if (t_flags & TF_PREVVALID) {
2897 		db_printf("%sTF_PREVVALID", comma ? ", " : "");
2898 		comma = 1;
2899 	}
2900 	if (t_flags & TF_MORETOCOME) {
2901 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2902 		comma = 1;
2903 	}
2904 	if (t_flags & TF_SONOTCONN) {
2905 		db_printf("%sTF_SONOTCONN", comma ? ", " : "");
2906 		comma = 1;
2907 	}
2908 	if (t_flags & TF_LASTIDLE) {
2909 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2910 		comma = 1;
2911 	}
2912 	if (t_flags & TF_RXWIN0SENT) {
2913 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2914 		comma = 1;
2915 	}
2916 	if (t_flags & TF_FASTRECOVERY) {
2917 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2918 		comma = 1;
2919 	}
2920 	if (t_flags & TF_CONGRECOVERY) {
2921 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2922 		comma = 1;
2923 	}
2924 	if (t_flags & TF_WASFRECOVERY) {
2925 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2926 		comma = 1;
2927 	}
2928 	if (t_flags & TF_WASCRECOVERY) {
2929 		db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
2930 		comma = 1;
2931 	}
2932 	if (t_flags & TF_SIGNATURE) {
2933 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2934 		comma = 1;
2935 	}
2936 	if (t_flags & TF_FORCEDATA) {
2937 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2938 		comma = 1;
2939 	}
2940 	if (t_flags & TF_TSO) {
2941 		db_printf("%sTF_TSO", comma ? ", " : "");
2942 		comma = 1;
2943 	}
2944 	if (t_flags & TF_FASTOPEN) {
2945 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2946 		comma = 1;
2947 	}
2948 }
2949 
2950 static void
2951 db_print_tflags2(u_int t_flags2)
2952 {
2953 	int comma;
2954 
2955 	comma = 0;
2956 	if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
2957 		db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
2958 		comma = 1;
2959 	}
2960 	if (t_flags2 & TF2_PLPMTU_PMTUD) {
2961 		db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
2962 		comma = 1;
2963 	}
2964 	if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
2965 		db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
2966 		comma = 1;
2967 	}
2968 	if (t_flags2 & TF2_LOG_AUTO) {
2969 		db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
2970 		comma = 1;
2971 	}
2972 	if (t_flags2 & TF2_DROP_AF_DATA) {
2973 		db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
2974 		comma = 1;
2975 	}
2976 	if (t_flags2 & TF2_ECN_PERMIT) {
2977 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
2978 		comma = 1;
2979 	}
2980 	if (t_flags2 & TF2_ECN_SND_CWR) {
2981 		db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
2982 		comma = 1;
2983 	}
2984 	if (t_flags2 & TF2_ECN_SND_ECE) {
2985 		db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
2986 		comma = 1;
2987 	}
2988 	if (t_flags2 & TF2_ACE_PERMIT) {
2989 		db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
2990 		comma = 1;
2991 	}
2992 	if (t_flags2 & TF2_FBYTES_COMPLETE) {
2993 		db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
2994 		comma = 1;
2995 	}
2996 }
2997 
2998 static void
2999 db_print_toobflags(char t_oobflags)
3000 {
3001 	int comma;
3002 
3003 	comma = 0;
3004 	if (t_oobflags & TCPOOB_HAVEDATA) {
3005 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
3006 		comma = 1;
3007 	}
3008 	if (t_oobflags & TCPOOB_HADDATA) {
3009 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
3010 		comma = 1;
3011 	}
3012 }
3013 
3014 static void
3015 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
3016 {
3017 
3018 	db_print_indent(indent);
3019 	db_printf("%s at %p\n", name, tp);
3020 
3021 	indent += 2;
3022 
3023 	db_print_indent(indent);
3024 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
3025 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
3026 
3027 	db_print_indent(indent);
3028 	db_printf("t_callout: %p   t_timers: %p\n",
3029 	    &tp->t_callout, &tp->t_timers);
3030 
3031 	db_print_indent(indent);
3032 	db_printf("t_state: %d (", tp->t_state);
3033 	db_print_tstate(tp->t_state);
3034 	db_printf(")\n");
3035 
3036 	db_print_indent(indent);
3037 	db_printf("t_flags: 0x%x (", tp->t_flags);
3038 	db_print_tflags(tp->t_flags);
3039 	db_printf(")\n");
3040 
3041 	db_print_indent(indent);
3042 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
3043 	db_print_tflags2(tp->t_flags2);
3044 	db_printf(")\n");
3045 
3046 	db_print_indent(indent);
3047 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: 0x%08x\n",
3048 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
3049 
3050 	db_print_indent(indent);
3051 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
3052 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
3053 
3054 	db_print_indent(indent);
3055 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
3056 	    tp->iss, tp->irs, tp->rcv_nxt);
3057 
3058 	db_print_indent(indent);
3059 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
3060 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
3061 
3062 	db_print_indent(indent);
3063 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
3064 	   tp->snd_wnd, tp->snd_cwnd);
3065 
3066 	db_print_indent(indent);
3067 	db_printf("snd_ssthresh: %u   snd_recover: "
3068 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3069 
3070 	db_print_indent(indent);
3071 	db_printf("t_rcvtime: %u   t_startime: %u\n",
3072 	    tp->t_rcvtime, tp->t_starttime);
3073 
3074 	db_print_indent(indent);
3075 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
3076 	    tp->t_rtttime, tp->t_rtseq);
3077 
3078 	db_print_indent(indent);
3079 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
3080 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3081 
3082 	db_print_indent(indent);
3083 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u\n",
3084 	    tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin);
3085 
3086 	db_print_indent(indent);
3087 	db_printf("t_rttupdated: %u   max_sndwnd: %u   t_softerror: %d\n",
3088 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3089 
3090 	db_print_indent(indent);
3091 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3092 	db_print_toobflags(tp->t_oobflags);
3093 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
3094 
3095 	db_print_indent(indent);
3096 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
3097 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3098 
3099 	db_print_indent(indent);
3100 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
3101 	    tp->ts_recent, tp->ts_recent_age);
3102 
3103 	db_print_indent(indent);
3104 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
3105 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3106 
3107 	db_print_indent(indent);
3108 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
3109 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3110 	    tp->snd_recover_prev, tp->t_badrxtwin);
3111 
3112 	db_print_indent(indent);
3113 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
3114 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3115 
3116 	db_print_indent(indent);
3117 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
3118 	    tp->snd_fack, tp->rcv_numsacks);
3119 
3120 	/* Skip sackblks, sackhint. */
3121 
3122 	db_print_indent(indent);
3123 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
3124 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3125 }
3126 
3127 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3128 {
3129 	struct tcpcb *tp;
3130 
3131 	if (!have_addr) {
3132 		db_printf("usage: show tcpcb <addr>\n");
3133 		return;
3134 	}
3135 	tp = (struct tcpcb *)addr;
3136 
3137 	db_print_tcpcb(tp, "tcpcb", 0);
3138 }
3139 #endif
3140