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