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