xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 2bfd8b5b9419b0ceb3dd0295fdf413d32969e5b2)
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 		soref(so);
1374 		inp->inp_flags |= INP_SOCKREF;
1375 	}
1376 	INP_WUNLOCK(inp);
1377 dropped:
1378 	NET_EPOCH_EXIT(et);
1379 }
1380 
1381 /*
1382  * TCP socket is closed.  Start friendly disconnect.
1383  */
1384 static void
1385 tcp_usr_close(struct socket *so)
1386 {
1387 	struct inpcb *inp;
1388 	struct tcpcb *tp = NULL;
1389 	struct epoch_tracker et;
1390 	TCPDEBUG0;
1391 
1392 	inp = sotoinpcb(so);
1393 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1394 
1395 	NET_EPOCH_ENTER(et);
1396 	INP_WLOCK(inp);
1397 	KASSERT(inp->inp_socket != NULL,
1398 	    ("tcp_usr_close: inp_socket == NULL"));
1399 
1400 	/*
1401 	 * If we still have full TCP state, and we're not dropped, initiate
1402 	 * a disconnect.
1403 	 */
1404 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1405 	    !(inp->inp_flags & INP_DROPPED)) {
1406 		tp = intotcpcb(inp);
1407 		tp->t_flags |= TF_CLOSED;
1408 		TCPDEBUG1();
1409 		tcp_disconnect(tp);
1410 		TCPDEBUG2(PRU_CLOSE);
1411 		TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1412 	}
1413 	if (!(inp->inp_flags & INP_DROPPED)) {
1414 		soref(so);
1415 		inp->inp_flags |= INP_SOCKREF;
1416 	}
1417 	INP_WUNLOCK(inp);
1418 	NET_EPOCH_EXIT(et);
1419 }
1420 
1421 static int
1422 tcp_pru_options_support(struct tcpcb *tp, int flags)
1423 {
1424 	/*
1425 	 * If the specific TCP stack has a pru_options
1426 	 * specified then it does not always support
1427 	 * all the PRU_XX options and we must ask it.
1428 	 * If the function is not specified then all
1429 	 * of the PRU_XX options are supported.
1430 	 */
1431 	int ret = 0;
1432 
1433 	if (tp->t_fb->tfb_pru_options) {
1434 		ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1435 	}
1436 	return (ret);
1437 }
1438 
1439 /*
1440  * Receive out-of-band data.
1441  */
1442 static int
1443 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1444 {
1445 	int error = 0;
1446 	struct inpcb *inp;
1447 	struct tcpcb *tp = NULL;
1448 
1449 	TCPDEBUG0;
1450 	inp = sotoinpcb(so);
1451 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1452 	INP_WLOCK(inp);
1453 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1454 		error = ECONNRESET;
1455 		goto out;
1456 	}
1457 	tp = intotcpcb(inp);
1458 	error = tcp_pru_options_support(tp, PRUS_OOB);
1459 	if (error) {
1460 		goto out;
1461 	}
1462 	TCPDEBUG1();
1463 	if ((so->so_oobmark == 0 &&
1464 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1465 	    so->so_options & SO_OOBINLINE ||
1466 	    tp->t_oobflags & TCPOOB_HADDATA) {
1467 		error = EINVAL;
1468 		goto out;
1469 	}
1470 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1471 		error = EWOULDBLOCK;
1472 		goto out;
1473 	}
1474 	m->m_len = 1;
1475 	*mtod(m, caddr_t) = tp->t_iobc;
1476 	if ((flags & MSG_PEEK) == 0)
1477 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1478 
1479 out:
1480 	TCPDEBUG2(PRU_RCVOOB);
1481 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1482 	INP_WUNLOCK(inp);
1483 	return (error);
1484 }
1485 
1486 #ifdef INET
1487 struct pr_usrreqs tcp_usrreqs = {
1488 	.pru_abort =		tcp_usr_abort,
1489 	.pru_accept =		tcp_usr_accept,
1490 	.pru_attach =		tcp_usr_attach,
1491 	.pru_bind =		tcp_usr_bind,
1492 	.pru_connect =		tcp_usr_connect,
1493 	.pru_control =		in_control,
1494 	.pru_detach =		tcp_usr_detach,
1495 	.pru_disconnect =	tcp_usr_disconnect,
1496 	.pru_listen =		tcp_usr_listen,
1497 	.pru_peeraddr =		in_getpeeraddr,
1498 	.pru_rcvd =		tcp_usr_rcvd,
1499 	.pru_rcvoob =		tcp_usr_rcvoob,
1500 	.pru_send =		tcp_usr_send,
1501 	.pru_ready =		tcp_usr_ready,
1502 	.pru_shutdown =		tcp_usr_shutdown,
1503 	.pru_sockaddr =		in_getsockaddr,
1504 	.pru_sosetlabel =	in_pcbsosetlabel,
1505 	.pru_close =		tcp_usr_close,
1506 };
1507 #endif /* INET */
1508 
1509 #ifdef INET6
1510 struct pr_usrreqs tcp6_usrreqs = {
1511 	.pru_abort =		tcp_usr_abort,
1512 	.pru_accept =		tcp6_usr_accept,
1513 	.pru_attach =		tcp_usr_attach,
1514 	.pru_bind =		tcp6_usr_bind,
1515 	.pru_connect =		tcp6_usr_connect,
1516 	.pru_control =		in6_control,
1517 	.pru_detach =		tcp_usr_detach,
1518 	.pru_disconnect =	tcp_usr_disconnect,
1519 	.pru_listen =		tcp6_usr_listen,
1520 	.pru_peeraddr =		in6_mapped_peeraddr,
1521 	.pru_rcvd =		tcp_usr_rcvd,
1522 	.pru_rcvoob =		tcp_usr_rcvoob,
1523 	.pru_send =		tcp_usr_send,
1524 	.pru_ready =		tcp_usr_ready,
1525 	.pru_shutdown =		tcp_usr_shutdown,
1526 	.pru_sockaddr =		in6_mapped_sockaddr,
1527 	.pru_sosetlabel =	in_pcbsosetlabel,
1528 	.pru_close =		tcp_usr_close,
1529 };
1530 #endif /* INET6 */
1531 
1532 #ifdef INET
1533 /*
1534  * Common subroutine to open a TCP connection to remote host specified
1535  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
1536  * port number if needed.  Call in_pcbconnect_setup to do the routing and
1537  * to choose a local host address (interface).  If there is an existing
1538  * incarnation of the same connection in TIME-WAIT state and if the remote
1539  * host was sending CC options and if the connection duration was < MSL, then
1540  * truncate the previous TIME-WAIT state and proceed.
1541  * Initialize connection parameters and enter SYN-SENT state.
1542  */
1543 static int
1544 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1545 {
1546 	struct inpcb *inp = tp->t_inpcb, *oinp;
1547 	struct socket *so = inp->inp_socket;
1548 	struct in_addr laddr;
1549 	u_short lport;
1550 	int error;
1551 
1552 	NET_EPOCH_ASSERT();
1553 	INP_WLOCK_ASSERT(inp);
1554 	INP_HASH_WLOCK(&V_tcbinfo);
1555 
1556 	if (V_tcp_require_unique_port && inp->inp_lport == 0) {
1557 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1558 		if (error)
1559 			goto out;
1560 	}
1561 
1562 	/*
1563 	 * Cannot simply call in_pcbconnect, because there might be an
1564 	 * earlier incarnation of this same connection still in
1565 	 * TIME_WAIT state, creating an ADDRINUSE error.
1566 	 */
1567 	laddr = inp->inp_laddr;
1568 	lport = inp->inp_lport;
1569 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1570 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1571 	if (error && oinp == NULL)
1572 		goto out;
1573 	if (oinp) {
1574 		error = EADDRINUSE;
1575 		goto out;
1576 	}
1577 	/* Handle initial bind if it hadn't been done in advance. */
1578 	if (inp->inp_lport == 0) {
1579 		inp->inp_lport = lport;
1580 		if (in_pcbinshash(inp) != 0) {
1581 			inp->inp_lport = 0;
1582 			error = EAGAIN;
1583 			goto out;
1584 		}
1585 	}
1586 	inp->inp_laddr = laddr;
1587 	in_pcbrehash(inp);
1588 	INP_HASH_WUNLOCK(&V_tcbinfo);
1589 
1590 	/*
1591 	 * Compute window scaling to request:
1592 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1593 	 * XXX: This should move to tcp_output().
1594 	 */
1595 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1596 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1597 		tp->request_r_scale++;
1598 
1599 	soisconnecting(so);
1600 	TCPSTAT_INC(tcps_connattempt);
1601 	tcp_state_change(tp, TCPS_SYN_SENT);
1602 	tp->iss = tcp_new_isn(&inp->inp_inc);
1603 	if (tp->t_flags & TF_REQ_TSTMP)
1604 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1605 	tcp_sendseqinit(tp);
1606 
1607 	return 0;
1608 
1609 out:
1610 	INP_HASH_WUNLOCK(&V_tcbinfo);
1611 	return (error);
1612 }
1613 #endif /* INET */
1614 
1615 #ifdef INET6
1616 static int
1617 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1618 {
1619 	struct inpcb *inp = tp->t_inpcb;
1620 	int error;
1621 
1622 	INP_WLOCK_ASSERT(inp);
1623 	INP_HASH_WLOCK(&V_tcbinfo);
1624 
1625 	if (V_tcp_require_unique_port && inp->inp_lport == 0) {
1626 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1627 		if (error)
1628 			goto out;
1629 	}
1630 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1631 	if (error != 0)
1632 		goto out;
1633 	INP_HASH_WUNLOCK(&V_tcbinfo);
1634 
1635 	/* Compute window scaling to request.  */
1636 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1637 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1638 		tp->request_r_scale++;
1639 
1640 	soisconnecting(inp->inp_socket);
1641 	TCPSTAT_INC(tcps_connattempt);
1642 	tcp_state_change(tp, TCPS_SYN_SENT);
1643 	tp->iss = tcp_new_isn(&inp->inp_inc);
1644 	if (tp->t_flags & TF_REQ_TSTMP)
1645 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1646 	tcp_sendseqinit(tp);
1647 
1648 	return 0;
1649 
1650 out:
1651 	INP_HASH_WUNLOCK(&V_tcbinfo);
1652 	return error;
1653 }
1654 #endif /* INET6 */
1655 
1656 /*
1657  * Export TCP internal state information via a struct tcp_info, based on the
1658  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1659  * (TCP state machine, etc).  We export all information using FreeBSD-native
1660  * constants -- for example, the numeric values for tcpi_state will differ
1661  * from Linux.
1662  */
1663 static void
1664 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1665 {
1666 
1667 	INP_WLOCK_ASSERT(tp->t_inpcb);
1668 	bzero(ti, sizeof(*ti));
1669 
1670 	ti->tcpi_state = tp->t_state;
1671 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1672 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1673 	if (tp->t_flags & TF_SACK_PERMIT)
1674 		ti->tcpi_options |= TCPI_OPT_SACK;
1675 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1676 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1677 		ti->tcpi_snd_wscale = tp->snd_scale;
1678 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1679 	}
1680 	if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
1681 		ti->tcpi_options |= TCPI_OPT_ECN;
1682 
1683 	ti->tcpi_rto = tp->t_rxtcur * tick;
1684 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1685 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1686 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1687 
1688 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1689 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1690 
1691 	/*
1692 	 * FreeBSD-specific extension fields for tcp_info.
1693 	 */
1694 	ti->tcpi_rcv_space = tp->rcv_wnd;
1695 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1696 	ti->tcpi_snd_wnd = tp->snd_wnd;
1697 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1698 	ti->tcpi_snd_nxt = tp->snd_nxt;
1699 	ti->tcpi_snd_mss = tp->t_maxseg;
1700 	ti->tcpi_rcv_mss = tp->t_maxseg;
1701 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1702 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1703 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1704 #ifdef TCP_OFFLOAD
1705 	if (tp->t_flags & TF_TOE) {
1706 		ti->tcpi_options |= TCPI_OPT_TOE;
1707 		tcp_offload_tcp_info(tp, ti);
1708 	}
1709 #endif
1710 }
1711 
1712 /*
1713  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1714  * socket option arguments.  When it re-acquires the lock after the copy, it
1715  * has to revalidate that the connection is still valid for the socket
1716  * option.
1717  */
1718 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
1719 	INP_WLOCK(inp);							\
1720 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
1721 		INP_WUNLOCK(inp);					\
1722 		cleanup;						\
1723 		return (ECONNRESET);					\
1724 	}								\
1725 	tp = intotcpcb(inp);						\
1726 } while(0)
1727 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1728 
1729 int
1730 tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt)
1731 {
1732 	struct socket *so = inp->inp_socket;
1733 	struct tcpcb *tp = intotcpcb(inp);
1734 	int error = 0;
1735 
1736 	MPASS(sopt->sopt_dir == SOPT_SET);
1737 	INP_WLOCK_ASSERT(inp);
1738 	KASSERT((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) == 0,
1739 	    ("inp_flags == %x", inp->inp_flags));
1740 	KASSERT(so != NULL, ("inp_socket == NULL"));
1741 
1742 	if (sopt->sopt_level != IPPROTO_TCP) {
1743 		INP_WUNLOCK(inp);
1744 #ifdef INET6
1745 		if (inp->inp_vflag & INP_IPV6PROTO)
1746 			error = ip6_ctloutput(so, sopt);
1747 #endif
1748 #if defined(INET6) && defined(INET)
1749 		else
1750 #endif
1751 #ifdef INET
1752 			error = ip_ctloutput(so, sopt);
1753 #endif
1754 		/*
1755 		 * When an IP-level socket option affects TCP, pass control
1756 		 * down to stack tfb_tcp_ctloutput, otherwise return what
1757 		 * IP level returned.
1758 		 */
1759 		switch (sopt->sopt_level) {
1760 #ifdef INET6
1761 		case IPPROTO_IPV6:
1762 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0)
1763 				return (error);
1764 			switch (sopt->sopt_name) {
1765 			case IPV6_TCLASS:
1766 				/* Notify tcp stacks that care (e.g. RACK). */
1767 				break;
1768 			case IPV6_USE_MIN_MTU:
1769 				/* Update t_maxseg accordingly. */
1770 				break;
1771 			default:
1772 				return (error);
1773 			}
1774 			break;
1775 #endif
1776 #ifdef INET
1777 		case IPPROTO_IP:
1778 			switch (sopt->sopt_name) {
1779 			case IP_TOS:
1780 				inp->inp_ip_tos &= ~IPTOS_ECN_MASK;
1781 				break;
1782 			case IP_TTL:
1783 				/* Notify tcp stacks that care (e.g. RACK). */
1784 				break;
1785 			default:
1786 				return (error);
1787 			}
1788 			break;
1789 #endif
1790 		default:
1791 			return (error);
1792 		}
1793 		INP_WLOCK(inp);
1794 		if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1795 			INP_WUNLOCK(inp);
1796 			return (ECONNRESET);
1797 		}
1798 	} else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1799 		/*
1800 		 * Protect the TCP option TCP_FUNCTION_BLK so
1801 		 * that a sub-function can *never* overwrite this.
1802 		 */
1803 		struct tcp_function_set fsn;
1804 		struct tcp_function_block *blk;
1805 
1806 		INP_WUNLOCK(inp);
1807 		error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1808 		if (error)
1809 			return (error);
1810 
1811 		INP_WLOCK(inp);
1812 		if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1813 			INP_WUNLOCK(inp);
1814 			return (ECONNRESET);
1815 		}
1816 		tp = intotcpcb(inp);
1817 
1818 		blk = find_and_ref_tcp_functions(&fsn);
1819 		if (blk == NULL) {
1820 			INP_WUNLOCK(inp);
1821 			return (ENOENT);
1822 		}
1823 		if (tp->t_fb == blk) {
1824 			/* You already have this */
1825 			refcount_release(&blk->tfb_refcnt);
1826 			INP_WUNLOCK(inp);
1827 			return (0);
1828 		}
1829 		if (tp->t_state != TCPS_CLOSED) {
1830 			/*
1831 			 * The user has advanced the state
1832 			 * past the initial point, we may not
1833 			 * be able to switch.
1834 			 */
1835 			if (blk->tfb_tcp_handoff_ok != NULL) {
1836 				/*
1837 				 * Does the stack provide a
1838 				 * query mechanism, if so it may
1839 				 * still be possible?
1840 				 */
1841 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1842 			} else
1843 				error = EINVAL;
1844 			if (error) {
1845 				refcount_release(&blk->tfb_refcnt);
1846 				INP_WUNLOCK(inp);
1847 				return(error);
1848 			}
1849 		}
1850 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1851 			refcount_release(&blk->tfb_refcnt);
1852 			INP_WUNLOCK(inp);
1853 			return (ENOENT);
1854 		}
1855 		/*
1856 		 * Release the old refcnt, the
1857 		 * lookup acquired a ref on the
1858 		 * new one already.
1859 		 */
1860 		if (tp->t_fb->tfb_tcp_fb_fini) {
1861 			struct epoch_tracker et;
1862 			/*
1863 			 * Tell the stack to cleanup with 0 i.e.
1864 			 * the tcb is not going away.
1865 			 */
1866 			NET_EPOCH_ENTER(et);
1867 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1868 			NET_EPOCH_EXIT(et);
1869 		}
1870 #ifdef TCPHPTS
1871 		/* Assure that we are not on any hpts */
1872 		tcp_hpts_remove(tp->t_inpcb);
1873 #endif
1874 		if (blk->tfb_tcp_fb_init) {
1875 			error = (*blk->tfb_tcp_fb_init)(tp);
1876 			if (error) {
1877 				refcount_release(&blk->tfb_refcnt);
1878 				if (tp->t_fb->tfb_tcp_fb_init) {
1879 					if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0)  {
1880 						/* Fall back failed, drop the connection */
1881 						INP_WUNLOCK(inp);
1882 						soabort(so);
1883 						return (error);
1884 					}
1885 				}
1886 				goto err_out;
1887 			}
1888 		}
1889 		refcount_release(&tp->t_fb->tfb_refcnt);
1890 		tp->t_fb = blk;
1891 #ifdef TCP_OFFLOAD
1892 		if (tp->t_flags & TF_TOE) {
1893 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
1894 			     sopt->sopt_name);
1895 		}
1896 #endif
1897 err_out:
1898 		INP_WUNLOCK(inp);
1899 		return (error);
1900 	}
1901 
1902 	/* Pass in the INP locked, callee must unlock it. */
1903 	return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1904 }
1905 
1906 static int
1907 tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1908 {
1909 	struct socket *so = inp->inp_socket;
1910 	struct tcpcb *tp = intotcpcb(inp);
1911 	int error = 0;
1912 
1913 	MPASS(sopt->sopt_dir == SOPT_GET);
1914 	INP_WLOCK_ASSERT(inp);
1915 	KASSERT((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) == 0,
1916 	    ("inp_flags == %x", inp->inp_flags));
1917 	KASSERT(so != NULL, ("inp_socket == NULL"));
1918 
1919 	if (sopt->sopt_level != IPPROTO_TCP) {
1920 		INP_WUNLOCK(inp);
1921 #ifdef INET6
1922 		if (inp->inp_vflag & INP_IPV6PROTO)
1923 			error = ip6_ctloutput(so, sopt);
1924 #endif /* INET6 */
1925 #if defined(INET6) && defined(INET)
1926 		else
1927 #endif
1928 #ifdef INET
1929 			error = ip_ctloutput(so, sopt);
1930 #endif
1931 		return (error);
1932 	}
1933 	if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1934 	     (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1935 		struct tcp_function_set fsn;
1936 
1937 		if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1938 			memset(&fsn, 0, sizeof(fsn));
1939 			find_tcp_function_alias(tp->t_fb, &fsn);
1940 		} else {
1941 			strncpy(fsn.function_set_name,
1942 			    tp->t_fb->tfb_tcp_block_name,
1943 			    TCP_FUNCTION_NAME_LEN_MAX);
1944 			fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1945 		}
1946 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1947 		INP_WUNLOCK(inp);
1948 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
1949 		return (error);
1950 	}
1951 
1952 	/* Pass in the INP locked, callee must unlock it. */
1953 	return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1954 }
1955 
1956 int
1957 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1958 {
1959 	struct	inpcb *inp;
1960 
1961 	inp = sotoinpcb(so);
1962 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1963 
1964 	INP_WLOCK(inp);
1965 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1966 		INP_WUNLOCK(inp);
1967 		return (ECONNRESET);
1968 	}
1969 	if (sopt->sopt_dir == SOPT_SET)
1970 		return (tcp_ctloutput_set(inp, sopt));
1971 	else if (sopt->sopt_dir == SOPT_GET)
1972 		return (tcp_ctloutput_get(inp, sopt));
1973 	else
1974 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
1975 }
1976 
1977 /*
1978  * If this assert becomes untrue, we need to change the size of the buf
1979  * variable in tcp_default_ctloutput().
1980  */
1981 #ifdef CTASSERT
1982 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1983 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1984 #endif
1985 
1986 #ifdef KERN_TLS
1987 static int
1988 copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1989 {
1990 	struct tls_enable_v0 tls_v0;
1991 	int error;
1992 
1993 	if (sopt->sopt_valsize == sizeof(tls_v0)) {
1994 		error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1995 		    sizeof(tls_v0));
1996 		if (error)
1997 			return (error);
1998 		memset(tls, 0, sizeof(*tls));
1999 		tls->cipher_key = tls_v0.cipher_key;
2000 		tls->iv = tls_v0.iv;
2001 		tls->auth_key = tls_v0.auth_key;
2002 		tls->cipher_algorithm = tls_v0.cipher_algorithm;
2003 		tls->cipher_key_len = tls_v0.cipher_key_len;
2004 		tls->iv_len = tls_v0.iv_len;
2005 		tls->auth_algorithm = tls_v0.auth_algorithm;
2006 		tls->auth_key_len = tls_v0.auth_key_len;
2007 		tls->flags = tls_v0.flags;
2008 		tls->tls_vmajor = tls_v0.tls_vmajor;
2009 		tls->tls_vminor = tls_v0.tls_vminor;
2010 		return (0);
2011 	}
2012 
2013 	return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
2014 }
2015 #endif
2016 
2017 extern struct cc_algo newreno_cc_algo;
2018 
2019 static int
2020 tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
2021 {
2022 	struct cc_algo *algo;
2023 	void *ptr = NULL;
2024 	struct tcpcb *tp;
2025 	struct cc_var cc_mem;
2026 	char	buf[TCP_CA_NAME_MAX];
2027 	size_t mem_sz;
2028 	int error;
2029 
2030 	INP_WUNLOCK(inp);
2031 	error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
2032 	if (error)
2033 		return(error);
2034 	buf[sopt->sopt_valsize] = '\0';
2035 	CC_LIST_RLOCK();
2036 	STAILQ_FOREACH(algo, &cc_list, entries) {
2037 		if (strncmp(buf, algo->name,
2038 			    TCP_CA_NAME_MAX) == 0) {
2039 			if (algo->flags & CC_MODULE_BEING_REMOVED) {
2040 				/* We can't "see" modules being unloaded */
2041 				continue;
2042 			}
2043 			break;
2044 		}
2045 	}
2046 	if (algo == NULL) {
2047 		CC_LIST_RUNLOCK();
2048 		return(ESRCH);
2049 	}
2050 	/*
2051 	 * With a reference the algorithm cannot be removed
2052 	 * so we hold a reference through the change process.
2053 	 */
2054 	cc_refer(algo);
2055 	CC_LIST_RUNLOCK();
2056 	if (algo->cb_init != NULL) {
2057 		/* We can now pre-get the memory for the CC */
2058 		mem_sz = (*algo->cc_data_sz)();
2059 		if (mem_sz == 0) {
2060 			goto no_mem_needed;
2061 		}
2062 		ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
2063 	} else {
2064 no_mem_needed:
2065 		mem_sz = 0;
2066 		ptr = NULL;
2067 	}
2068 	/*
2069 	 * Make sure its all clean and zero and also get
2070 	 * back the inplock.
2071 	 */
2072 	memset(&cc_mem, 0, sizeof(cc_mem));
2073 	INP_WLOCK(inp);
2074 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
2075 		INP_WUNLOCK(inp);
2076 		if (ptr)
2077 			free(ptr, M_CC_MEM);
2078 		/* Release our temp reference */
2079 		CC_LIST_RLOCK();
2080 		cc_release(algo);
2081 		CC_LIST_RUNLOCK();
2082 		return (ECONNRESET);
2083 	}
2084 	tp = intotcpcb(inp);
2085 	if (ptr != NULL)
2086 		memset(ptr, 0, mem_sz);
2087 	cc_mem.ccvc.tcp = tp;
2088 	/*
2089 	 * We once again hold a write lock over the tcb so it's
2090 	 * safe to do these things without ordering concerns.
2091 	 * Note here we init into stack memory.
2092 	 */
2093 	if (algo->cb_init != NULL)
2094 		error = algo->cb_init(&cc_mem, ptr);
2095 	else
2096 		error = 0;
2097 	/*
2098 	 * The CC algorithms, when given their memory
2099 	 * should not fail we could in theory have a
2100 	 * KASSERT here.
2101 	 */
2102 	if (error == 0) {
2103 		/*
2104 		 * Touchdown, lets go ahead and move the
2105 		 * connection to the new CC module by
2106 		 * copying in the cc_mem after we call
2107 		 * the old ones cleanup (if any).
2108 		 */
2109 		if (CC_ALGO(tp)->cb_destroy != NULL)
2110 			CC_ALGO(tp)->cb_destroy(tp->ccv);
2111 		/* Detach the old CC from the tcpcb  */
2112 		cc_detach(tp);
2113 		/* Copy in our temp memory that was inited */
2114 		memcpy(tp->ccv, &cc_mem, sizeof(struct cc_var));
2115 		/* Now attach the new, which takes a reference */
2116 		cc_attach(tp, algo);
2117 		/* Ok now are we where we have gotten past any conn_init? */
2118 		if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
2119 			/* Yep run the connection init for the new CC */
2120 			CC_ALGO(tp)->conn_init(tp->ccv);
2121 		}
2122 	} else if (ptr)
2123 		free(ptr, M_CC_MEM);
2124 	INP_WUNLOCK(inp);
2125 	/* Now lets release our temp reference */
2126 	CC_LIST_RLOCK();
2127 	cc_release(algo);
2128 	CC_LIST_RUNLOCK();
2129 	return (error);
2130 }
2131 
2132 int
2133 tcp_default_ctloutput(struct inpcb *inp, struct sockopt *sopt)
2134 {
2135 	struct tcpcb *tp = intotcpcb(inp);
2136 	int	error, opt, optval;
2137 	u_int	ui;
2138 	struct	tcp_info ti;
2139 #ifdef KERN_TLS
2140 	struct tls_enable tls;
2141 	struct socket *so = inp->inp_socket;
2142 #endif
2143 	char	*pbuf, buf[TCP_LOG_ID_LEN];
2144 #ifdef STATS
2145 	struct statsblob *sbp;
2146 #endif
2147 	size_t	len;
2148 
2149 	INP_WLOCK_ASSERT(inp);
2150 	KASSERT((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) == 0,
2151 	    ("inp_flags == %x", inp->inp_flags));
2152 	KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
2153 
2154 	switch (sopt->sopt_level) {
2155 #ifdef INET6
2156 	case IPPROTO_IPV6:
2157 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
2158 		switch (sopt->sopt_name) {
2159 		case IPV6_USE_MIN_MTU:
2160 			tcp6_use_min_mtu(tp);
2161 			/* FALLTHROUGH */
2162 		}
2163 		INP_WUNLOCK(inp);
2164 		return (0);
2165 #endif
2166 #ifdef INET
2167 	case IPPROTO_IP:
2168 		INP_WUNLOCK(inp);
2169 		return (0);
2170 #endif
2171 	}
2172 
2173 	/*
2174 	 * For TCP_CCALGOOPT forward the control to CC module, for both
2175 	 * SOPT_SET and SOPT_GET.
2176 	 */
2177 	switch (sopt->sopt_name) {
2178 	case TCP_CCALGOOPT:
2179 		INP_WUNLOCK(inp);
2180 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2181 			return (EINVAL);
2182 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2183 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2184 		    sopt->sopt_valsize);
2185 		if (error) {
2186 			free(pbuf, M_TEMP);
2187 			return (error);
2188 		}
2189 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2190 		if (CC_ALGO(tp)->ctl_output != NULL)
2191 			error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
2192 		else
2193 			error = ENOENT;
2194 		INP_WUNLOCK(inp);
2195 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
2196 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2197 		free(pbuf, M_TEMP);
2198 		return (error);
2199 	}
2200 
2201 	switch (sopt->sopt_dir) {
2202 	case SOPT_SET:
2203 		switch (sopt->sopt_name) {
2204 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2205 		case TCP_MD5SIG:
2206 			INP_WUNLOCK(inp);
2207 			if (!TCPMD5_ENABLED())
2208 				return (ENOPROTOOPT);
2209 			error = TCPMD5_PCBCTL(inp, sopt);
2210 			if (error)
2211 				return (error);
2212 			INP_WLOCK_RECHECK(inp);
2213 			goto unlock_and_done;
2214 #endif /* IPSEC */
2215 
2216 		case TCP_NODELAY:
2217 		case TCP_NOOPT:
2218 		case TCP_LRD:
2219 			INP_WUNLOCK(inp);
2220 			error = sooptcopyin(sopt, &optval, sizeof optval,
2221 			    sizeof optval);
2222 			if (error)
2223 				return (error);
2224 
2225 			INP_WLOCK_RECHECK(inp);
2226 			switch (sopt->sopt_name) {
2227 			case TCP_NODELAY:
2228 				opt = TF_NODELAY;
2229 				break;
2230 			case TCP_NOOPT:
2231 				opt = TF_NOOPT;
2232 				break;
2233 			case TCP_LRD:
2234 				opt = TF_LRD;
2235 				break;
2236 			default:
2237 				opt = 0; /* dead code to fool gcc */
2238 				break;
2239 			}
2240 
2241 			if (optval)
2242 				tp->t_flags |= opt;
2243 			else
2244 				tp->t_flags &= ~opt;
2245 unlock_and_done:
2246 #ifdef TCP_OFFLOAD
2247 			if (tp->t_flags & TF_TOE) {
2248 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
2249 				    sopt->sopt_name);
2250 			}
2251 #endif
2252 			INP_WUNLOCK(inp);
2253 			break;
2254 
2255 		case TCP_NOPUSH:
2256 			INP_WUNLOCK(inp);
2257 			error = sooptcopyin(sopt, &optval, sizeof optval,
2258 			    sizeof optval);
2259 			if (error)
2260 				return (error);
2261 
2262 			INP_WLOCK_RECHECK(inp);
2263 			if (optval)
2264 				tp->t_flags |= TF_NOPUSH;
2265 			else if (tp->t_flags & TF_NOPUSH) {
2266 				tp->t_flags &= ~TF_NOPUSH;
2267 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2268 					struct epoch_tracker et;
2269 
2270 					NET_EPOCH_ENTER(et);
2271 					error = tcp_output_nodrop(tp);
2272 					NET_EPOCH_EXIT(et);
2273 				}
2274 			}
2275 			goto unlock_and_done;
2276 
2277 		case TCP_REMOTE_UDP_ENCAPS_PORT:
2278 			INP_WUNLOCK(inp);
2279 			error = sooptcopyin(sopt, &optval, sizeof optval,
2280 			    sizeof optval);
2281 			if (error)
2282 				return (error);
2283 			if ((optval < TCP_TUNNELING_PORT_MIN) ||
2284 			    (optval > TCP_TUNNELING_PORT_MAX)) {
2285 				/* Its got to be in range */
2286 				return (EINVAL);
2287 			}
2288 			if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
2289 				/* You have to have enabled a UDP tunneling port first */
2290 				return (EINVAL);
2291 			}
2292 			INP_WLOCK_RECHECK(inp);
2293 			if (tp->t_state != TCPS_CLOSED) {
2294 				/* You can't change after you are connected */
2295 				error = EINVAL;
2296 			} else {
2297 				/* Ok we are all good set the port */
2298 				tp->t_port = htons(optval);
2299 			}
2300 			goto unlock_and_done;
2301 
2302 		case TCP_MAXSEG:
2303 			INP_WUNLOCK(inp);
2304 			error = sooptcopyin(sopt, &optval, sizeof optval,
2305 			    sizeof optval);
2306 			if (error)
2307 				return (error);
2308 
2309 			INP_WLOCK_RECHECK(inp);
2310 			if (optval > 0 && optval <= tp->t_maxseg &&
2311 			    optval + 40 >= V_tcp_minmss)
2312 				tp->t_maxseg = optval;
2313 			else
2314 				error = EINVAL;
2315 			goto unlock_and_done;
2316 
2317 		case TCP_INFO:
2318 			INP_WUNLOCK(inp);
2319 			error = EINVAL;
2320 			break;
2321 
2322 		case TCP_STATS:
2323 			INP_WUNLOCK(inp);
2324 #ifdef STATS
2325 			error = sooptcopyin(sopt, &optval, sizeof optval,
2326 			    sizeof optval);
2327 			if (error)
2328 				return (error);
2329 
2330 			if (optval > 0)
2331 				sbp = stats_blob_alloc(
2332 				    V_tcp_perconn_stats_dflt_tpl, 0);
2333 			else
2334 				sbp = NULL;
2335 
2336 			INP_WLOCK_RECHECK(inp);
2337 			if ((tp->t_stats != NULL && sbp == NULL) ||
2338 			    (tp->t_stats == NULL && sbp != NULL)) {
2339 				struct statsblob *t = tp->t_stats;
2340 				tp->t_stats = sbp;
2341 				sbp = t;
2342 			}
2343 			INP_WUNLOCK(inp);
2344 
2345 			stats_blob_destroy(sbp);
2346 #else
2347 			return (EOPNOTSUPP);
2348 #endif /* !STATS */
2349 			break;
2350 
2351 		case TCP_CONGESTION:
2352 			error = tcp_set_cc_mod(inp, sopt);
2353 			break;
2354 
2355 		case TCP_REUSPORT_LB_NUMA:
2356 			INP_WUNLOCK(inp);
2357 			error = sooptcopyin(sopt, &optval, sizeof(optval),
2358 			    sizeof(optval));
2359 			INP_WLOCK_RECHECK(inp);
2360 			if (!error)
2361 				error = in_pcblbgroup_numa(inp, optval);
2362 			INP_WUNLOCK(inp);
2363 			break;
2364 
2365 #ifdef KERN_TLS
2366 		case TCP_TXTLS_ENABLE:
2367 			INP_WUNLOCK(inp);
2368 			error = copyin_tls_enable(sopt, &tls);
2369 			if (error)
2370 				break;
2371 			error = ktls_enable_tx(so, &tls);
2372 			break;
2373 		case TCP_TXTLS_MODE:
2374 			INP_WUNLOCK(inp);
2375 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2376 			if (error)
2377 				return (error);
2378 
2379 			INP_WLOCK_RECHECK(inp);
2380 			error = ktls_set_tx_mode(so, ui);
2381 			INP_WUNLOCK(inp);
2382 			break;
2383 		case TCP_RXTLS_ENABLE:
2384 			INP_WUNLOCK(inp);
2385 			error = sooptcopyin(sopt, &tls, sizeof(tls),
2386 			    sizeof(tls));
2387 			if (error)
2388 				break;
2389 			error = ktls_enable_rx(so, &tls);
2390 			break;
2391 #endif
2392 
2393 		case TCP_KEEPIDLE:
2394 		case TCP_KEEPINTVL:
2395 		case TCP_KEEPINIT:
2396 			INP_WUNLOCK(inp);
2397 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2398 			if (error)
2399 				return (error);
2400 
2401 			if (ui > (UINT_MAX / hz)) {
2402 				error = EINVAL;
2403 				break;
2404 			}
2405 			ui *= hz;
2406 
2407 			INP_WLOCK_RECHECK(inp);
2408 			switch (sopt->sopt_name) {
2409 			case TCP_KEEPIDLE:
2410 				tp->t_keepidle = ui;
2411 				/*
2412 				 * XXX: better check current remaining
2413 				 * timeout and "merge" it with new value.
2414 				 */
2415 				if ((tp->t_state > TCPS_LISTEN) &&
2416 				    (tp->t_state <= TCPS_CLOSING))
2417 					tcp_timer_activate(tp, TT_KEEP,
2418 					    TP_KEEPIDLE(tp));
2419 				break;
2420 			case TCP_KEEPINTVL:
2421 				tp->t_keepintvl = ui;
2422 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2423 				    (TP_MAXIDLE(tp) > 0))
2424 					tcp_timer_activate(tp, TT_2MSL,
2425 					    TP_MAXIDLE(tp));
2426 				break;
2427 			case TCP_KEEPINIT:
2428 				tp->t_keepinit = ui;
2429 				if (tp->t_state == TCPS_SYN_RECEIVED ||
2430 				    tp->t_state == TCPS_SYN_SENT)
2431 					tcp_timer_activate(tp, TT_KEEP,
2432 					    TP_KEEPINIT(tp));
2433 				break;
2434 			}
2435 			goto unlock_and_done;
2436 
2437 		case TCP_KEEPCNT:
2438 			INP_WUNLOCK(inp);
2439 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2440 			if (error)
2441 				return (error);
2442 
2443 			INP_WLOCK_RECHECK(inp);
2444 			tp->t_keepcnt = ui;
2445 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2446 			    (TP_MAXIDLE(tp) > 0))
2447 				tcp_timer_activate(tp, TT_2MSL,
2448 				    TP_MAXIDLE(tp));
2449 			goto unlock_and_done;
2450 
2451 #ifdef TCPPCAP
2452 		case TCP_PCAP_OUT:
2453 		case TCP_PCAP_IN:
2454 			INP_WUNLOCK(inp);
2455 			error = sooptcopyin(sopt, &optval, sizeof optval,
2456 			    sizeof optval);
2457 			if (error)
2458 				return (error);
2459 
2460 			INP_WLOCK_RECHECK(inp);
2461 			if (optval >= 0)
2462 				tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
2463 					&(tp->t_outpkts) : &(tp->t_inpkts),
2464 					optval);
2465 			else
2466 				error = EINVAL;
2467 			goto unlock_and_done;
2468 #endif
2469 
2470 		case TCP_FASTOPEN: {
2471 			struct tcp_fastopen tfo_optval;
2472 
2473 			INP_WUNLOCK(inp);
2474 			if (!V_tcp_fastopen_client_enable &&
2475 			    !V_tcp_fastopen_server_enable)
2476 				return (EPERM);
2477 
2478 			error = sooptcopyin(sopt, &tfo_optval,
2479 				    sizeof(tfo_optval), sizeof(int));
2480 			if (error)
2481 				return (error);
2482 
2483 			INP_WLOCK_RECHECK(inp);
2484 			if ((tp->t_state != TCPS_CLOSED) &&
2485 			    (tp->t_state != TCPS_LISTEN)) {
2486 				error = EINVAL;
2487 				goto unlock_and_done;
2488 			}
2489 			if (tfo_optval.enable) {
2490 				if (tp->t_state == TCPS_LISTEN) {
2491 					if (!V_tcp_fastopen_server_enable) {
2492 						error = EPERM;
2493 						goto unlock_and_done;
2494 					}
2495 
2496 					if (tp->t_tfo_pending == NULL)
2497 						tp->t_tfo_pending =
2498 						    tcp_fastopen_alloc_counter();
2499 				} else {
2500 					/*
2501 					 * If a pre-shared key was provided,
2502 					 * stash it in the client cookie
2503 					 * field of the tcpcb for use during
2504 					 * connect.
2505 					 */
2506 					if (sopt->sopt_valsize ==
2507 					    sizeof(tfo_optval)) {
2508 						memcpy(tp->t_tfo_cookie.client,
2509 						       tfo_optval.psk,
2510 						       TCP_FASTOPEN_PSK_LEN);
2511 						tp->t_tfo_client_cookie_len =
2512 						    TCP_FASTOPEN_PSK_LEN;
2513 					}
2514 				}
2515 				tp->t_flags |= TF_FASTOPEN;
2516 			} else
2517 				tp->t_flags &= ~TF_FASTOPEN;
2518 			goto unlock_and_done;
2519 		}
2520 
2521 #ifdef TCP_BLACKBOX
2522 		case TCP_LOG:
2523 			INP_WUNLOCK(inp);
2524 			error = sooptcopyin(sopt, &optval, sizeof optval,
2525 			    sizeof optval);
2526 			if (error)
2527 				return (error);
2528 
2529 			INP_WLOCK_RECHECK(inp);
2530 			error = tcp_log_state_change(tp, optval);
2531 			goto unlock_and_done;
2532 
2533 		case TCP_LOGBUF:
2534 			INP_WUNLOCK(inp);
2535 			error = EINVAL;
2536 			break;
2537 
2538 		case TCP_LOGID:
2539 			INP_WUNLOCK(inp);
2540 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
2541 			if (error)
2542 				break;
2543 			buf[sopt->sopt_valsize] = '\0';
2544 			INP_WLOCK_RECHECK(inp);
2545 			error = tcp_log_set_id(tp, buf);
2546 			/* tcp_log_set_id() unlocks the INP. */
2547 			break;
2548 
2549 		case TCP_LOGDUMP:
2550 		case TCP_LOGDUMPID:
2551 			INP_WUNLOCK(inp);
2552 			error =
2553 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
2554 			if (error)
2555 				break;
2556 			buf[sopt->sopt_valsize] = '\0';
2557 			INP_WLOCK_RECHECK(inp);
2558 			if (sopt->sopt_name == TCP_LOGDUMP) {
2559 				error = tcp_log_dump_tp_logbuf(tp, buf,
2560 				    M_WAITOK, true);
2561 				INP_WUNLOCK(inp);
2562 			} else {
2563 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
2564 				/*
2565 				 * tcp_log_dump_tp_bucket_logbufs() drops the
2566 				 * INP lock.
2567 				 */
2568 			}
2569 			break;
2570 #endif
2571 
2572 		default:
2573 			INP_WUNLOCK(inp);
2574 			error = ENOPROTOOPT;
2575 			break;
2576 		}
2577 		break;
2578 
2579 	case SOPT_GET:
2580 		tp = intotcpcb(inp);
2581 		switch (sopt->sopt_name) {
2582 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2583 		case TCP_MD5SIG:
2584 			INP_WUNLOCK(inp);
2585 			if (!TCPMD5_ENABLED())
2586 				return (ENOPROTOOPT);
2587 			error = TCPMD5_PCBCTL(inp, sopt);
2588 			break;
2589 #endif
2590 
2591 		case TCP_NODELAY:
2592 			optval = tp->t_flags & TF_NODELAY;
2593 			INP_WUNLOCK(inp);
2594 			error = sooptcopyout(sopt, &optval, sizeof optval);
2595 			break;
2596 		case TCP_MAXSEG:
2597 			optval = tp->t_maxseg;
2598 			INP_WUNLOCK(inp);
2599 			error = sooptcopyout(sopt, &optval, sizeof optval);
2600 			break;
2601 		case TCP_REMOTE_UDP_ENCAPS_PORT:
2602 			optval = ntohs(tp->t_port);
2603 			INP_WUNLOCK(inp);
2604 			error = sooptcopyout(sopt, &optval, sizeof optval);
2605 			break;
2606 		case TCP_NOOPT:
2607 			optval = tp->t_flags & TF_NOOPT;
2608 			INP_WUNLOCK(inp);
2609 			error = sooptcopyout(sopt, &optval, sizeof optval);
2610 			break;
2611 		case TCP_NOPUSH:
2612 			optval = tp->t_flags & TF_NOPUSH;
2613 			INP_WUNLOCK(inp);
2614 			error = sooptcopyout(sopt, &optval, sizeof optval);
2615 			break;
2616 		case TCP_INFO:
2617 			tcp_fill_info(tp, &ti);
2618 			INP_WUNLOCK(inp);
2619 			error = sooptcopyout(sopt, &ti, sizeof ti);
2620 			break;
2621 		case TCP_STATS:
2622 			{
2623 #ifdef STATS
2624 			int nheld;
2625 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2626 
2627 			error = 0;
2628 			socklen_t outsbsz = sopt->sopt_valsize;
2629 			if (tp->t_stats == NULL)
2630 				error = ENOENT;
2631 			else if (outsbsz >= tp->t_stats->cursz)
2632 				outsbsz = tp->t_stats->cursz;
2633 			else if (outsbsz >= sizeof(struct statsblob))
2634 				outsbsz = sizeof(struct statsblob);
2635 			else
2636 				error = EINVAL;
2637 			INP_WUNLOCK(inp);
2638 			if (error)
2639 				break;
2640 
2641 			sbp = sopt->sopt_val;
2642 			nheld = atop(round_page(((vm_offset_t)sbp) +
2643 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2644 			vm_page_t ma[nheld];
2645 			if (vm_fault_quick_hold_pages(
2646 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2647 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2648 			    nheld) < 0) {
2649 				error = EFAULT;
2650 				break;
2651 			}
2652 
2653 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2654 			    SIZEOF_MEMBER(struct statsblob, flags))))
2655 				goto unhold;
2656 
2657 			INP_WLOCK_RECHECK(inp);
2658 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2659 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2660 			INP_WUNLOCK(inp);
2661 			sopt->sopt_valsize = outsbsz;
2662 unhold:
2663 			vm_page_unhold_pages(ma, nheld);
2664 #else
2665 			INP_WUNLOCK(inp);
2666 			error = EOPNOTSUPP;
2667 #endif /* !STATS */
2668 			break;
2669 			}
2670 		case TCP_CONGESTION:
2671 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2672 			INP_WUNLOCK(inp);
2673 			error = sooptcopyout(sopt, buf, len + 1);
2674 			break;
2675 		case TCP_KEEPIDLE:
2676 		case TCP_KEEPINTVL:
2677 		case TCP_KEEPINIT:
2678 		case TCP_KEEPCNT:
2679 			switch (sopt->sopt_name) {
2680 			case TCP_KEEPIDLE:
2681 				ui = TP_KEEPIDLE(tp) / hz;
2682 				break;
2683 			case TCP_KEEPINTVL:
2684 				ui = TP_KEEPINTVL(tp) / hz;
2685 				break;
2686 			case TCP_KEEPINIT:
2687 				ui = TP_KEEPINIT(tp) / hz;
2688 				break;
2689 			case TCP_KEEPCNT:
2690 				ui = TP_KEEPCNT(tp);
2691 				break;
2692 			}
2693 			INP_WUNLOCK(inp);
2694 			error = sooptcopyout(sopt, &ui, sizeof(ui));
2695 			break;
2696 #ifdef TCPPCAP
2697 		case TCP_PCAP_OUT:
2698 		case TCP_PCAP_IN:
2699 			optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
2700 					&(tp->t_outpkts) : &(tp->t_inpkts));
2701 			INP_WUNLOCK(inp);
2702 			error = sooptcopyout(sopt, &optval, sizeof optval);
2703 			break;
2704 #endif
2705 		case TCP_FASTOPEN:
2706 			optval = tp->t_flags & TF_FASTOPEN;
2707 			INP_WUNLOCK(inp);
2708 			error = sooptcopyout(sopt, &optval, sizeof optval);
2709 			break;
2710 #ifdef TCP_BLACKBOX
2711 		case TCP_LOG:
2712 			optval = tp->t_logstate;
2713 			INP_WUNLOCK(inp);
2714 			error = sooptcopyout(sopt, &optval, sizeof(optval));
2715 			break;
2716 		case TCP_LOGBUF:
2717 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2718 			error = tcp_log_getlogbuf(sopt, tp);
2719 			break;
2720 		case TCP_LOGID:
2721 			len = tcp_log_get_id(tp, buf);
2722 			INP_WUNLOCK(inp);
2723 			error = sooptcopyout(sopt, buf, len + 1);
2724 			break;
2725 		case TCP_LOGDUMP:
2726 		case TCP_LOGDUMPID:
2727 			INP_WUNLOCK(inp);
2728 			error = EINVAL;
2729 			break;
2730 #endif
2731 #ifdef KERN_TLS
2732 		case TCP_TXTLS_MODE:
2733 			error = ktls_get_tx_mode(so, &optval);
2734 			INP_WUNLOCK(inp);
2735 			if (error == 0)
2736 				error = sooptcopyout(sopt, &optval,
2737 				    sizeof(optval));
2738 			break;
2739 		case TCP_RXTLS_MODE:
2740 			error = ktls_get_rx_mode(so, &optval);
2741 			INP_WUNLOCK(inp);
2742 			if (error == 0)
2743 				error = sooptcopyout(sopt, &optval,
2744 				    sizeof(optval));
2745 			break;
2746 #endif
2747 		case TCP_LRD:
2748 			optval = tp->t_flags & TF_LRD;
2749 			INP_WUNLOCK(inp);
2750 			error = sooptcopyout(sopt, &optval, sizeof optval);
2751 			break;
2752 		default:
2753 			INP_WUNLOCK(inp);
2754 			error = ENOPROTOOPT;
2755 			break;
2756 		}
2757 		break;
2758 	}
2759 	return (error);
2760 }
2761 #undef INP_WLOCK_RECHECK
2762 #undef INP_WLOCK_RECHECK_CLEANUP
2763 
2764 /*
2765  * Initiate (or continue) disconnect.
2766  * If embryonic state, just send reset (once).
2767  * If in ``let data drain'' option and linger null, just drop.
2768  * Otherwise (hard), mark socket disconnecting and drop
2769  * current input data; switch states based on user close, and
2770  * send segment to peer (with FIN).
2771  */
2772 static void
2773 tcp_disconnect(struct tcpcb *tp)
2774 {
2775 	struct inpcb *inp = tp->t_inpcb;
2776 	struct socket *so = inp->inp_socket;
2777 
2778 	NET_EPOCH_ASSERT();
2779 	INP_WLOCK_ASSERT(inp);
2780 
2781 	/*
2782 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2783 	 * socket is still open.
2784 	 */
2785 	if (tp->t_state < TCPS_ESTABLISHED &&
2786 	    !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2787 		tp = tcp_close(tp);
2788 		KASSERT(tp != NULL,
2789 		    ("tcp_disconnect: tcp_close() returned NULL"));
2790 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2791 		tp = tcp_drop(tp, 0);
2792 		KASSERT(tp != NULL,
2793 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2794 	} else {
2795 		soisdisconnecting(so);
2796 		sbflush(&so->so_rcv);
2797 		tcp_usrclosed(tp);
2798 		if (!(inp->inp_flags & INP_DROPPED))
2799 			/* Ignore stack's drop request, we already at it. */
2800 			(void)tcp_output_nodrop(tp);
2801 	}
2802 }
2803 
2804 /*
2805  * User issued close, and wish to trail through shutdown states:
2806  * if never received SYN, just forget it.  If got a SYN from peer,
2807  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2808  * If already got a FIN from peer, then almost done; go to LAST_ACK
2809  * state.  In all other cases, have already sent FIN to peer (e.g.
2810  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2811  * for peer to send FIN or not respond to keep-alives, etc.
2812  * We can let the user exit from the close as soon as the FIN is acked.
2813  */
2814 static void
2815 tcp_usrclosed(struct tcpcb *tp)
2816 {
2817 
2818 	NET_EPOCH_ASSERT();
2819 	INP_WLOCK_ASSERT(tp->t_inpcb);
2820 
2821 	switch (tp->t_state) {
2822 	case TCPS_LISTEN:
2823 #ifdef TCP_OFFLOAD
2824 		tcp_offload_listen_stop(tp);
2825 #endif
2826 		tcp_state_change(tp, TCPS_CLOSED);
2827 		/* FALLTHROUGH */
2828 	case TCPS_CLOSED:
2829 		tp = tcp_close(tp);
2830 		/*
2831 		 * tcp_close() should never return NULL here as the socket is
2832 		 * still open.
2833 		 */
2834 		KASSERT(tp != NULL,
2835 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2836 		break;
2837 
2838 	case TCPS_SYN_SENT:
2839 	case TCPS_SYN_RECEIVED:
2840 		tp->t_flags |= TF_NEEDFIN;
2841 		break;
2842 
2843 	case TCPS_ESTABLISHED:
2844 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2845 		break;
2846 
2847 	case TCPS_CLOSE_WAIT:
2848 		tcp_state_change(tp, TCPS_LAST_ACK);
2849 		break;
2850 	}
2851 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
2852 		soisdisconnected(tp->t_inpcb->inp_socket);
2853 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
2854 		if (tp->t_state == TCPS_FIN_WAIT_2) {
2855 			int timeout;
2856 
2857 			timeout = (tcp_fast_finwait2_recycle) ?
2858 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2859 			tcp_timer_activate(tp, TT_2MSL, timeout);
2860 		}
2861 	}
2862 }
2863 
2864 #ifdef DDB
2865 static void
2866 db_print_indent(int indent)
2867 {
2868 	int i;
2869 
2870 	for (i = 0; i < indent; i++)
2871 		db_printf(" ");
2872 }
2873 
2874 static void
2875 db_print_tstate(int t_state)
2876 {
2877 
2878 	switch (t_state) {
2879 	case TCPS_CLOSED:
2880 		db_printf("TCPS_CLOSED");
2881 		return;
2882 
2883 	case TCPS_LISTEN:
2884 		db_printf("TCPS_LISTEN");
2885 		return;
2886 
2887 	case TCPS_SYN_SENT:
2888 		db_printf("TCPS_SYN_SENT");
2889 		return;
2890 
2891 	case TCPS_SYN_RECEIVED:
2892 		db_printf("TCPS_SYN_RECEIVED");
2893 		return;
2894 
2895 	case TCPS_ESTABLISHED:
2896 		db_printf("TCPS_ESTABLISHED");
2897 		return;
2898 
2899 	case TCPS_CLOSE_WAIT:
2900 		db_printf("TCPS_CLOSE_WAIT");
2901 		return;
2902 
2903 	case TCPS_FIN_WAIT_1:
2904 		db_printf("TCPS_FIN_WAIT_1");
2905 		return;
2906 
2907 	case TCPS_CLOSING:
2908 		db_printf("TCPS_CLOSING");
2909 		return;
2910 
2911 	case TCPS_LAST_ACK:
2912 		db_printf("TCPS_LAST_ACK");
2913 		return;
2914 
2915 	case TCPS_FIN_WAIT_2:
2916 		db_printf("TCPS_FIN_WAIT_2");
2917 		return;
2918 
2919 	case TCPS_TIME_WAIT:
2920 		db_printf("TCPS_TIME_WAIT");
2921 		return;
2922 
2923 	default:
2924 		db_printf("unknown");
2925 		return;
2926 	}
2927 }
2928 
2929 static void
2930 db_print_tflags(u_int t_flags)
2931 {
2932 	int comma;
2933 
2934 	comma = 0;
2935 	if (t_flags & TF_ACKNOW) {
2936 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2937 		comma = 1;
2938 	}
2939 	if (t_flags & TF_DELACK) {
2940 		db_printf("%sTF_DELACK", comma ? ", " : "");
2941 		comma = 1;
2942 	}
2943 	if (t_flags & TF_NODELAY) {
2944 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2945 		comma = 1;
2946 	}
2947 	if (t_flags & TF_NOOPT) {
2948 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2949 		comma = 1;
2950 	}
2951 	if (t_flags & TF_SENTFIN) {
2952 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2953 		comma = 1;
2954 	}
2955 	if (t_flags & TF_REQ_SCALE) {
2956 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2957 		comma = 1;
2958 	}
2959 	if (t_flags & TF_RCVD_SCALE) {
2960 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2961 		comma = 1;
2962 	}
2963 	if (t_flags & TF_REQ_TSTMP) {
2964 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2965 		comma = 1;
2966 	}
2967 	if (t_flags & TF_RCVD_TSTMP) {
2968 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2969 		comma = 1;
2970 	}
2971 	if (t_flags & TF_SACK_PERMIT) {
2972 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2973 		comma = 1;
2974 	}
2975 	if (t_flags & TF_NEEDSYN) {
2976 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2977 		comma = 1;
2978 	}
2979 	if (t_flags & TF_NEEDFIN) {
2980 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2981 		comma = 1;
2982 	}
2983 	if (t_flags & TF_NOPUSH) {
2984 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2985 		comma = 1;
2986 	}
2987 	if (t_flags & TF_PREVVALID) {
2988 		db_printf("%sTF_PREVVALID", comma ? ", " : "");
2989 		comma = 1;
2990 	}
2991 	if (t_flags & TF_MORETOCOME) {
2992 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2993 		comma = 1;
2994 	}
2995 	if (t_flags & TF_LQ_OVERFLOW) {
2996 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2997 		comma = 1;
2998 	}
2999 	if (t_flags & TF_LASTIDLE) {
3000 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
3001 		comma = 1;
3002 	}
3003 	if (t_flags & TF_RXWIN0SENT) {
3004 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
3005 		comma = 1;
3006 	}
3007 	if (t_flags & TF_FASTRECOVERY) {
3008 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
3009 		comma = 1;
3010 	}
3011 	if (t_flags & TF_CONGRECOVERY) {
3012 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
3013 		comma = 1;
3014 	}
3015 	if (t_flags & TF_WASFRECOVERY) {
3016 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
3017 		comma = 1;
3018 	}
3019 	if (t_flags & TF_WASCRECOVERY) {
3020 		db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
3021 		comma = 1;
3022 	}
3023 	if (t_flags & TF_SIGNATURE) {
3024 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
3025 		comma = 1;
3026 	}
3027 	if (t_flags & TF_FORCEDATA) {
3028 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
3029 		comma = 1;
3030 	}
3031 	if (t_flags & TF_TSO) {
3032 		db_printf("%sTF_TSO", comma ? ", " : "");
3033 		comma = 1;
3034 	}
3035 	if (t_flags & TF_FASTOPEN) {
3036 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
3037 		comma = 1;
3038 	}
3039 }
3040 
3041 static void
3042 db_print_tflags2(u_int t_flags2)
3043 {
3044 	int comma;
3045 
3046 	comma = 0;
3047 	if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
3048 		db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
3049 		comma = 1;
3050 	}
3051 	if (t_flags2 & TF2_PLPMTU_PMTUD) {
3052 		db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
3053 		comma = 1;
3054 	}
3055 	if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
3056 		db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
3057 		comma = 1;
3058 	}
3059 	if (t_flags2 & TF2_LOG_AUTO) {
3060 		db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
3061 		comma = 1;
3062 	}
3063 	if (t_flags2 & TF2_DROP_AF_DATA) {
3064 		db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
3065 		comma = 1;
3066 	}
3067 	if (t_flags2 & TF2_ECN_PERMIT) {
3068 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
3069 		comma = 1;
3070 	}
3071 	if (t_flags2 & TF2_ECN_SND_CWR) {
3072 		db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
3073 		comma = 1;
3074 	}
3075 	if (t_flags2 & TF2_ECN_SND_ECE) {
3076 		db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
3077 		comma = 1;
3078 	}
3079 	if (t_flags2 & TF2_ACE_PERMIT) {
3080 		db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
3081 		comma = 1;
3082 	}
3083 	if (t_flags2 & TF2_FBYTES_COMPLETE) {
3084 		db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
3085 		comma = 1;
3086 	}
3087 }
3088 
3089 static void
3090 db_print_toobflags(char t_oobflags)
3091 {
3092 	int comma;
3093 
3094 	comma = 0;
3095 	if (t_oobflags & TCPOOB_HAVEDATA) {
3096 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
3097 		comma = 1;
3098 	}
3099 	if (t_oobflags & TCPOOB_HADDATA) {
3100 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
3101 		comma = 1;
3102 	}
3103 }
3104 
3105 static void
3106 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
3107 {
3108 
3109 	db_print_indent(indent);
3110 	db_printf("%s at %p\n", name, tp);
3111 
3112 	indent += 2;
3113 
3114 	db_print_indent(indent);
3115 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
3116 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
3117 
3118 	db_print_indent(indent);
3119 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
3120 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
3121 
3122 	db_print_indent(indent);
3123 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
3124 	    &tp->t_timers->tt_delack, tp->t_inpcb);
3125 
3126 	db_print_indent(indent);
3127 	db_printf("t_state: %d (", tp->t_state);
3128 	db_print_tstate(tp->t_state);
3129 	db_printf(")\n");
3130 
3131 	db_print_indent(indent);
3132 	db_printf("t_flags: 0x%x (", tp->t_flags);
3133 	db_print_tflags(tp->t_flags);
3134 	db_printf(")\n");
3135 
3136 	db_print_indent(indent);
3137 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
3138 	db_print_tflags2(tp->t_flags2);
3139 	db_printf(")\n");
3140 
3141 	db_print_indent(indent);
3142 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
3143 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
3144 
3145 	db_print_indent(indent);
3146 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
3147 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
3148 
3149 	db_print_indent(indent);
3150 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
3151 	    tp->iss, tp->irs, tp->rcv_nxt);
3152 
3153 	db_print_indent(indent);
3154 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
3155 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
3156 
3157 	db_print_indent(indent);
3158 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
3159 	   tp->snd_wnd, tp->snd_cwnd);
3160 
3161 	db_print_indent(indent);
3162 	db_printf("snd_ssthresh: %u   snd_recover: "
3163 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3164 
3165 	db_print_indent(indent);
3166 	db_printf("t_rcvtime: %u   t_startime: %u\n",
3167 	    tp->t_rcvtime, tp->t_starttime);
3168 
3169 	db_print_indent(indent);
3170 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
3171 	    tp->t_rtttime, tp->t_rtseq);
3172 
3173 	db_print_indent(indent);
3174 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
3175 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3176 
3177 	db_print_indent(indent);
3178 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
3179 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
3180 	    tp->t_rttbest);
3181 
3182 	db_print_indent(indent);
3183 	db_printf("t_rttupdated: %lu   max_sndwnd: %u   t_softerror: %d\n",
3184 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3185 
3186 	db_print_indent(indent);
3187 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3188 	db_print_toobflags(tp->t_oobflags);
3189 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
3190 
3191 	db_print_indent(indent);
3192 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
3193 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3194 
3195 	db_print_indent(indent);
3196 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
3197 	    tp->ts_recent, tp->ts_recent_age);
3198 
3199 	db_print_indent(indent);
3200 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
3201 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3202 
3203 	db_print_indent(indent);
3204 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
3205 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3206 	    tp->snd_recover_prev, tp->t_badrxtwin);
3207 
3208 	db_print_indent(indent);
3209 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
3210 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3211 
3212 	db_print_indent(indent);
3213 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
3214 	    tp->snd_fack, tp->rcv_numsacks);
3215 
3216 	/* Skip sackblks, sackhint. */
3217 
3218 	db_print_indent(indent);
3219 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
3220 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3221 }
3222 
3223 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3224 {
3225 	struct tcpcb *tp;
3226 
3227 	if (!have_addr) {
3228 		db_printf("usage: show tcpcb <addr>\n");
3229 		return;
3230 	}
3231 	tp = (struct tcpcb *)addr;
3232 
3233 	db_print_tcpcb(tp, "tcpcb", 0);
3234 }
3235 #endif
3236