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