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