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