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