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