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