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