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