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