xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 4d3fc8b0570b29fb0d6ee9525f104d52176ff0d4)
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 (tp->t_logstate != TCP_LOG_STATE_OFF) {
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 
1663 		INP_WUNLOCK(inp);
1664 		error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1665 		if (error)
1666 			return (error);
1667 
1668 		INP_WLOCK(inp);
1669 		if (inp->inp_flags & INP_DROPPED) {
1670 			INP_WUNLOCK(inp);
1671 			return (ECONNRESET);
1672 		}
1673 		tp = intotcpcb(inp);
1674 
1675 		blk = find_and_ref_tcp_functions(&fsn);
1676 		if (blk == NULL) {
1677 			INP_WUNLOCK(inp);
1678 			return (ENOENT);
1679 		}
1680 		if (tp->t_fb == blk) {
1681 			/* You already have this */
1682 			refcount_release(&blk->tfb_refcnt);
1683 			INP_WUNLOCK(inp);
1684 			return (0);
1685 		}
1686 		if (tp->t_state != TCPS_CLOSED) {
1687 			/*
1688 			 * The user has advanced the state
1689 			 * past the initial point, we may not
1690 			 * be able to switch.
1691 			 */
1692 			if (blk->tfb_tcp_handoff_ok != NULL) {
1693 				/*
1694 				 * Does the stack provide a
1695 				 * query mechanism, if so it may
1696 				 * still be possible?
1697 				 */
1698 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1699 			} else
1700 				error = EINVAL;
1701 			if (error) {
1702 				refcount_release(&blk->tfb_refcnt);
1703 				INP_WUNLOCK(inp);
1704 				return(error);
1705 			}
1706 		}
1707 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1708 			refcount_release(&blk->tfb_refcnt);
1709 			INP_WUNLOCK(inp);
1710 			return (ENOENT);
1711 		}
1712 		/*
1713 		 * Release the old refcnt, the
1714 		 * lookup acquired a ref on the
1715 		 * new one already.
1716 		 */
1717 		if (tp->t_fb->tfb_tcp_fb_fini) {
1718 			struct epoch_tracker et;
1719 			/*
1720 			 * Tell the stack to cleanup with 0 i.e.
1721 			 * the tcb is not going away.
1722 			 */
1723 			NET_EPOCH_ENTER(et);
1724 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1725 			NET_EPOCH_EXIT(et);
1726 		}
1727 #ifdef TCPHPTS
1728 		/* Assure that we are not on any hpts */
1729 		tcp_hpts_remove(tptoinpcb(tp));
1730 #endif
1731 		if (blk->tfb_tcp_fb_init) {
1732 			error = (*blk->tfb_tcp_fb_init)(tp);
1733 			if (error) {
1734 				refcount_release(&blk->tfb_refcnt);
1735 				if (tp->t_fb->tfb_tcp_fb_init) {
1736 					if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0)  {
1737 						/* Fall back failed, drop the connection */
1738 						INP_WUNLOCK(inp);
1739 						soabort(so);
1740 						return (error);
1741 					}
1742 				}
1743 				goto err_out;
1744 			}
1745 		}
1746 		refcount_release(&tp->t_fb->tfb_refcnt);
1747 		tp->t_fb = blk;
1748 #ifdef TCP_OFFLOAD
1749 		if (tp->t_flags & TF_TOE) {
1750 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
1751 			     sopt->sopt_name);
1752 		}
1753 #endif
1754 err_out:
1755 		INP_WUNLOCK(inp);
1756 		return (error);
1757 	}
1758 
1759 	/* Pass in the INP locked, callee must unlock it. */
1760 	return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1761 }
1762 
1763 static int
1764 tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1765 {
1766 	struct socket *so = inp->inp_socket;
1767 	struct tcpcb *tp = intotcpcb(inp);
1768 	int error = 0;
1769 
1770 	MPASS(sopt->sopt_dir == SOPT_GET);
1771 	INP_WLOCK_ASSERT(inp);
1772 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1773 	    ("inp_flags == %x", inp->inp_flags));
1774 	KASSERT(so != NULL, ("inp_socket == NULL"));
1775 
1776 	if (sopt->sopt_level != IPPROTO_TCP) {
1777 		INP_WUNLOCK(inp);
1778 #ifdef INET6
1779 		if (inp->inp_vflag & INP_IPV6PROTO)
1780 			error = ip6_ctloutput(so, sopt);
1781 #endif /* INET6 */
1782 #if defined(INET6) && defined(INET)
1783 		else
1784 #endif
1785 #ifdef INET
1786 			error = ip_ctloutput(so, sopt);
1787 #endif
1788 		return (error);
1789 	}
1790 	if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1791 	     (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1792 		struct tcp_function_set fsn;
1793 
1794 		if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1795 			memset(&fsn, 0, sizeof(fsn));
1796 			find_tcp_function_alias(tp->t_fb, &fsn);
1797 		} else {
1798 			strncpy(fsn.function_set_name,
1799 			    tp->t_fb->tfb_tcp_block_name,
1800 			    TCP_FUNCTION_NAME_LEN_MAX);
1801 			fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1802 		}
1803 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1804 		INP_WUNLOCK(inp);
1805 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
1806 		return (error);
1807 	}
1808 
1809 	/* Pass in the INP locked, callee must unlock it. */
1810 	return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1811 }
1812 
1813 int
1814 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1815 {
1816 	struct	inpcb *inp;
1817 
1818 	inp = sotoinpcb(so);
1819 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1820 
1821 	INP_WLOCK(inp);
1822 	if (inp->inp_flags & INP_DROPPED) {
1823 		INP_WUNLOCK(inp);
1824 		return (ECONNRESET);
1825 	}
1826 	if (sopt->sopt_dir == SOPT_SET)
1827 		return (tcp_ctloutput_set(inp, sopt));
1828 	else if (sopt->sopt_dir == SOPT_GET)
1829 		return (tcp_ctloutput_get(inp, sopt));
1830 	else
1831 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
1832 }
1833 
1834 /*
1835  * If this assert becomes untrue, we need to change the size of the buf
1836  * variable in tcp_default_ctloutput().
1837  */
1838 #ifdef CTASSERT
1839 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1840 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1841 #endif
1842 
1843 #ifdef KERN_TLS
1844 static int
1845 copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1846 {
1847 	struct tls_enable_v0 tls_v0;
1848 	int error;
1849 
1850 	if (sopt->sopt_valsize == sizeof(tls_v0)) {
1851 		error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1852 		    sizeof(tls_v0));
1853 		if (error)
1854 			return (error);
1855 		memset(tls, 0, sizeof(*tls));
1856 		tls->cipher_key = tls_v0.cipher_key;
1857 		tls->iv = tls_v0.iv;
1858 		tls->auth_key = tls_v0.auth_key;
1859 		tls->cipher_algorithm = tls_v0.cipher_algorithm;
1860 		tls->cipher_key_len = tls_v0.cipher_key_len;
1861 		tls->iv_len = tls_v0.iv_len;
1862 		tls->auth_algorithm = tls_v0.auth_algorithm;
1863 		tls->auth_key_len = tls_v0.auth_key_len;
1864 		tls->flags = tls_v0.flags;
1865 		tls->tls_vmajor = tls_v0.tls_vmajor;
1866 		tls->tls_vminor = tls_v0.tls_vminor;
1867 		return (0);
1868 	}
1869 
1870 	return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1871 }
1872 #endif
1873 
1874 extern struct cc_algo newreno_cc_algo;
1875 
1876 static int
1877 tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
1878 {
1879 	struct cc_algo *algo;
1880 	void *ptr = NULL;
1881 	struct tcpcb *tp;
1882 	struct cc_var cc_mem;
1883 	char	buf[TCP_CA_NAME_MAX];
1884 	size_t mem_sz;
1885 	int error;
1886 
1887 	INP_WUNLOCK(inp);
1888 	error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1889 	if (error)
1890 		return(error);
1891 	buf[sopt->sopt_valsize] = '\0';
1892 	CC_LIST_RLOCK();
1893 	STAILQ_FOREACH(algo, &cc_list, entries) {
1894 		if (strncmp(buf, algo->name,
1895 			    TCP_CA_NAME_MAX) == 0) {
1896 			if (algo->flags & CC_MODULE_BEING_REMOVED) {
1897 				/* We can't "see" modules being unloaded */
1898 				continue;
1899 			}
1900 			break;
1901 		}
1902 	}
1903 	if (algo == NULL) {
1904 		CC_LIST_RUNLOCK();
1905 		return(ESRCH);
1906 	}
1907 	/*
1908 	 * With a reference the algorithm cannot be removed
1909 	 * so we hold a reference through the change process.
1910 	 */
1911 	cc_refer(algo);
1912 	CC_LIST_RUNLOCK();
1913 	if (algo->cb_init != NULL) {
1914 		/* We can now pre-get the memory for the CC */
1915 		mem_sz = (*algo->cc_data_sz)();
1916 		if (mem_sz == 0) {
1917 			goto no_mem_needed;
1918 		}
1919 		ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
1920 	} else {
1921 no_mem_needed:
1922 		mem_sz = 0;
1923 		ptr = NULL;
1924 	}
1925 	/*
1926 	 * Make sure its all clean and zero and also get
1927 	 * back the inplock.
1928 	 */
1929 	memset(&cc_mem, 0, sizeof(cc_mem));
1930 	INP_WLOCK(inp);
1931 	if (inp->inp_flags & INP_DROPPED) {
1932 		INP_WUNLOCK(inp);
1933 		if (ptr)
1934 			free(ptr, M_CC_MEM);
1935 		/* Release our temp reference */
1936 		CC_LIST_RLOCK();
1937 		cc_release(algo);
1938 		CC_LIST_RUNLOCK();
1939 		return (ECONNRESET);
1940 	}
1941 	tp = intotcpcb(inp);
1942 	if (ptr != NULL)
1943 		memset(ptr, 0, mem_sz);
1944 	cc_mem.ccvc.tcp = tp;
1945 	/*
1946 	 * We once again hold a write lock over the tcb so it's
1947 	 * safe to do these things without ordering concerns.
1948 	 * Note here we init into stack memory.
1949 	 */
1950 	if (algo->cb_init != NULL)
1951 		error = algo->cb_init(&cc_mem, ptr);
1952 	else
1953 		error = 0;
1954 	/*
1955 	 * The CC algorithms, when given their memory
1956 	 * should not fail we could in theory have a
1957 	 * KASSERT here.
1958 	 */
1959 	if (error == 0) {
1960 		/*
1961 		 * Touchdown, lets go ahead and move the
1962 		 * connection to the new CC module by
1963 		 * copying in the cc_mem after we call
1964 		 * the old ones cleanup (if any).
1965 		 */
1966 		if (CC_ALGO(tp)->cb_destroy != NULL)
1967 			CC_ALGO(tp)->cb_destroy(&tp->t_ccv);
1968 		/* Detach the old CC from the tcpcb  */
1969 		cc_detach(tp);
1970 		/* Copy in our temp memory that was inited */
1971 		memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var));
1972 		/* Now attach the new, which takes a reference */
1973 		cc_attach(tp, algo);
1974 		/* Ok now are we where we have gotten past any conn_init? */
1975 		if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
1976 			/* Yep run the connection init for the new CC */
1977 			CC_ALGO(tp)->conn_init(&tp->t_ccv);
1978 		}
1979 	} else if (ptr)
1980 		free(ptr, M_CC_MEM);
1981 	INP_WUNLOCK(inp);
1982 	/* Now lets release our temp reference */
1983 	CC_LIST_RLOCK();
1984 	cc_release(algo);
1985 	CC_LIST_RUNLOCK();
1986 	return (error);
1987 }
1988 
1989 int
1990 tcp_default_ctloutput(struct inpcb *inp, struct sockopt *sopt)
1991 {
1992 	struct tcpcb *tp = intotcpcb(inp);
1993 	int	error, opt, optval;
1994 	u_int	ui;
1995 	struct	tcp_info ti;
1996 #ifdef KERN_TLS
1997 	struct tls_enable tls;
1998 	struct socket *so = inp->inp_socket;
1999 #endif
2000 	char	*pbuf, buf[TCP_LOG_ID_LEN];
2001 #ifdef STATS
2002 	struct statsblob *sbp;
2003 #endif
2004 	size_t	len;
2005 
2006 	INP_WLOCK_ASSERT(inp);
2007 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
2008 	    ("inp_flags == %x", inp->inp_flags));
2009 	KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
2010 
2011 	switch (sopt->sopt_level) {
2012 #ifdef INET6
2013 	case IPPROTO_IPV6:
2014 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
2015 		switch (sopt->sopt_name) {
2016 		case IPV6_USE_MIN_MTU:
2017 			tcp6_use_min_mtu(tp);
2018 			/* FALLTHROUGH */
2019 		}
2020 		INP_WUNLOCK(inp);
2021 		return (0);
2022 #endif
2023 #ifdef INET
2024 	case IPPROTO_IP:
2025 		INP_WUNLOCK(inp);
2026 		return (0);
2027 #endif
2028 	}
2029 
2030 	/*
2031 	 * For TCP_CCALGOOPT forward the control to CC module, for both
2032 	 * SOPT_SET and SOPT_GET.
2033 	 */
2034 	switch (sopt->sopt_name) {
2035 	case TCP_CCALGOOPT:
2036 		INP_WUNLOCK(inp);
2037 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2038 			return (EINVAL);
2039 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2040 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2041 		    sopt->sopt_valsize);
2042 		if (error) {
2043 			free(pbuf, M_TEMP);
2044 			return (error);
2045 		}
2046 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2047 		if (CC_ALGO(tp)->ctl_output != NULL)
2048 			error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf);
2049 		else
2050 			error = ENOENT;
2051 		INP_WUNLOCK(inp);
2052 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
2053 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2054 		free(pbuf, M_TEMP);
2055 		return (error);
2056 	}
2057 
2058 	switch (sopt->sopt_dir) {
2059 	case SOPT_SET:
2060 		switch (sopt->sopt_name) {
2061 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2062 		case TCP_MD5SIG:
2063 			INP_WUNLOCK(inp);
2064 			if (!TCPMD5_ENABLED())
2065 				return (ENOPROTOOPT);
2066 			error = TCPMD5_PCBCTL(inp, sopt);
2067 			if (error)
2068 				return (error);
2069 			INP_WLOCK_RECHECK(inp);
2070 			goto unlock_and_done;
2071 #endif /* IPSEC */
2072 
2073 		case TCP_NODELAY:
2074 		case TCP_NOOPT:
2075 		case TCP_LRD:
2076 			INP_WUNLOCK(inp);
2077 			error = sooptcopyin(sopt, &optval, sizeof optval,
2078 			    sizeof optval);
2079 			if (error)
2080 				return (error);
2081 
2082 			INP_WLOCK_RECHECK(inp);
2083 			switch (sopt->sopt_name) {
2084 			case TCP_NODELAY:
2085 				opt = TF_NODELAY;
2086 				break;
2087 			case TCP_NOOPT:
2088 				opt = TF_NOOPT;
2089 				break;
2090 			case TCP_LRD:
2091 				opt = TF_LRD;
2092 				break;
2093 			default:
2094 				opt = 0; /* dead code to fool gcc */
2095 				break;
2096 			}
2097 
2098 			if (optval)
2099 				tp->t_flags |= opt;
2100 			else
2101 				tp->t_flags &= ~opt;
2102 unlock_and_done:
2103 #ifdef TCP_OFFLOAD
2104 			if (tp->t_flags & TF_TOE) {
2105 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
2106 				    sopt->sopt_name);
2107 			}
2108 #endif
2109 			INP_WUNLOCK(inp);
2110 			break;
2111 
2112 		case TCP_NOPUSH:
2113 			INP_WUNLOCK(inp);
2114 			error = sooptcopyin(sopt, &optval, sizeof optval,
2115 			    sizeof optval);
2116 			if (error)
2117 				return (error);
2118 
2119 			INP_WLOCK_RECHECK(inp);
2120 			if (optval)
2121 				tp->t_flags |= TF_NOPUSH;
2122 			else if (tp->t_flags & TF_NOPUSH) {
2123 				tp->t_flags &= ~TF_NOPUSH;
2124 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2125 					struct epoch_tracker et;
2126 
2127 					NET_EPOCH_ENTER(et);
2128 					error = tcp_output_nodrop(tp);
2129 					NET_EPOCH_EXIT(et);
2130 				}
2131 			}
2132 			goto unlock_and_done;
2133 
2134 		case TCP_REMOTE_UDP_ENCAPS_PORT:
2135 			INP_WUNLOCK(inp);
2136 			error = sooptcopyin(sopt, &optval, sizeof optval,
2137 			    sizeof optval);
2138 			if (error)
2139 				return (error);
2140 			if ((optval < TCP_TUNNELING_PORT_MIN) ||
2141 			    (optval > TCP_TUNNELING_PORT_MAX)) {
2142 				/* Its got to be in range */
2143 				return (EINVAL);
2144 			}
2145 			if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
2146 				/* You have to have enabled a UDP tunneling port first */
2147 				return (EINVAL);
2148 			}
2149 			INP_WLOCK_RECHECK(inp);
2150 			if (tp->t_state != TCPS_CLOSED) {
2151 				/* You can't change after you are connected */
2152 				error = EINVAL;
2153 			} else {
2154 				/* Ok we are all good set the port */
2155 				tp->t_port = htons(optval);
2156 			}
2157 			goto unlock_and_done;
2158 
2159 		case TCP_MAXSEG:
2160 			INP_WUNLOCK(inp);
2161 			error = sooptcopyin(sopt, &optval, sizeof optval,
2162 			    sizeof optval);
2163 			if (error)
2164 				return (error);
2165 
2166 			INP_WLOCK_RECHECK(inp);
2167 			if (optval > 0 && optval <= tp->t_maxseg &&
2168 			    optval + 40 >= V_tcp_minmss)
2169 				tp->t_maxseg = optval;
2170 			else
2171 				error = EINVAL;
2172 			goto unlock_and_done;
2173 
2174 		case TCP_INFO:
2175 			INP_WUNLOCK(inp);
2176 			error = EINVAL;
2177 			break;
2178 
2179 		case TCP_STATS:
2180 			INP_WUNLOCK(inp);
2181 #ifdef STATS
2182 			error = sooptcopyin(sopt, &optval, sizeof optval,
2183 			    sizeof optval);
2184 			if (error)
2185 				return (error);
2186 
2187 			if (optval > 0)
2188 				sbp = stats_blob_alloc(
2189 				    V_tcp_perconn_stats_dflt_tpl, 0);
2190 			else
2191 				sbp = NULL;
2192 
2193 			INP_WLOCK_RECHECK(inp);
2194 			if ((tp->t_stats != NULL && sbp == NULL) ||
2195 			    (tp->t_stats == NULL && sbp != NULL)) {
2196 				struct statsblob *t = tp->t_stats;
2197 				tp->t_stats = sbp;
2198 				sbp = t;
2199 			}
2200 			INP_WUNLOCK(inp);
2201 
2202 			stats_blob_destroy(sbp);
2203 #else
2204 			return (EOPNOTSUPP);
2205 #endif /* !STATS */
2206 			break;
2207 
2208 		case TCP_CONGESTION:
2209 			error = tcp_set_cc_mod(inp, sopt);
2210 			break;
2211 
2212 		case TCP_REUSPORT_LB_NUMA:
2213 			INP_WUNLOCK(inp);
2214 			error = sooptcopyin(sopt, &optval, sizeof(optval),
2215 			    sizeof(optval));
2216 			INP_WLOCK_RECHECK(inp);
2217 			if (!error)
2218 				error = in_pcblbgroup_numa(inp, optval);
2219 			INP_WUNLOCK(inp);
2220 			break;
2221 
2222 #ifdef KERN_TLS
2223 		case TCP_TXTLS_ENABLE:
2224 			INP_WUNLOCK(inp);
2225 			error = copyin_tls_enable(sopt, &tls);
2226 			if (error)
2227 				break;
2228 			error = ktls_enable_tx(so, &tls);
2229 			break;
2230 		case TCP_TXTLS_MODE:
2231 			INP_WUNLOCK(inp);
2232 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2233 			if (error)
2234 				return (error);
2235 
2236 			INP_WLOCK_RECHECK(inp);
2237 			error = ktls_set_tx_mode(so, ui);
2238 			INP_WUNLOCK(inp);
2239 			break;
2240 		case TCP_RXTLS_ENABLE:
2241 			INP_WUNLOCK(inp);
2242 			error = sooptcopyin(sopt, &tls, sizeof(tls),
2243 			    sizeof(tls));
2244 			if (error)
2245 				break;
2246 			error = ktls_enable_rx(so, &tls);
2247 			break;
2248 #endif
2249 		case TCP_MAXUNACKTIME:
2250 		case TCP_KEEPIDLE:
2251 		case TCP_KEEPINTVL:
2252 		case TCP_KEEPINIT:
2253 			INP_WUNLOCK(inp);
2254 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2255 			if (error)
2256 				return (error);
2257 
2258 			if (ui > (UINT_MAX / hz)) {
2259 				error = EINVAL;
2260 				break;
2261 			}
2262 			ui *= hz;
2263 
2264 			INP_WLOCK_RECHECK(inp);
2265 			switch (sopt->sopt_name) {
2266 			case TCP_MAXUNACKTIME:
2267 				tp->t_maxunacktime = ui;
2268 				break;
2269 
2270 			case TCP_KEEPIDLE:
2271 				tp->t_keepidle = ui;
2272 				/*
2273 				 * XXX: better check current remaining
2274 				 * timeout and "merge" it with new value.
2275 				 */
2276 				if ((tp->t_state > TCPS_LISTEN) &&
2277 				    (tp->t_state <= TCPS_CLOSING))
2278 					tcp_timer_activate(tp, TT_KEEP,
2279 					    TP_KEEPIDLE(tp));
2280 				break;
2281 			case TCP_KEEPINTVL:
2282 				tp->t_keepintvl = ui;
2283 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2284 				    (TP_MAXIDLE(tp) > 0))
2285 					tcp_timer_activate(tp, TT_2MSL,
2286 					    TP_MAXIDLE(tp));
2287 				break;
2288 			case TCP_KEEPINIT:
2289 				tp->t_keepinit = ui;
2290 				if (tp->t_state == TCPS_SYN_RECEIVED ||
2291 				    tp->t_state == TCPS_SYN_SENT)
2292 					tcp_timer_activate(tp, TT_KEEP,
2293 					    TP_KEEPINIT(tp));
2294 				break;
2295 			}
2296 			goto unlock_and_done;
2297 
2298 		case TCP_KEEPCNT:
2299 			INP_WUNLOCK(inp);
2300 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2301 			if (error)
2302 				return (error);
2303 
2304 			INP_WLOCK_RECHECK(inp);
2305 			tp->t_keepcnt = ui;
2306 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2307 			    (TP_MAXIDLE(tp) > 0))
2308 				tcp_timer_activate(tp, TT_2MSL,
2309 				    TP_MAXIDLE(tp));
2310 			goto unlock_and_done;
2311 
2312 #ifdef TCPPCAP
2313 		case TCP_PCAP_OUT:
2314 		case TCP_PCAP_IN:
2315 			INP_WUNLOCK(inp);
2316 			error = sooptcopyin(sopt, &optval, sizeof optval,
2317 			    sizeof optval);
2318 			if (error)
2319 				return (error);
2320 
2321 			INP_WLOCK_RECHECK(inp);
2322 			if (optval >= 0)
2323 				tcp_pcap_set_sock_max(
2324 					(sopt->sopt_name == TCP_PCAP_OUT) ?
2325 					&(tp->t_outpkts) : &(tp->t_inpkts),
2326 					optval);
2327 			else
2328 				error = EINVAL;
2329 			goto unlock_and_done;
2330 #endif
2331 
2332 		case TCP_FASTOPEN: {
2333 			struct tcp_fastopen tfo_optval;
2334 
2335 			INP_WUNLOCK(inp);
2336 			if (!V_tcp_fastopen_client_enable &&
2337 			    !V_tcp_fastopen_server_enable)
2338 				return (EPERM);
2339 
2340 			error = sooptcopyin(sopt, &tfo_optval,
2341 				    sizeof(tfo_optval), sizeof(int));
2342 			if (error)
2343 				return (error);
2344 
2345 			INP_WLOCK_RECHECK(inp);
2346 			if ((tp->t_state != TCPS_CLOSED) &&
2347 			    (tp->t_state != TCPS_LISTEN)) {
2348 				error = EINVAL;
2349 				goto unlock_and_done;
2350 			}
2351 			if (tfo_optval.enable) {
2352 				if (tp->t_state == TCPS_LISTEN) {
2353 					if (!V_tcp_fastopen_server_enable) {
2354 						error = EPERM;
2355 						goto unlock_and_done;
2356 					}
2357 
2358 					if (tp->t_tfo_pending == NULL)
2359 						tp->t_tfo_pending =
2360 						    tcp_fastopen_alloc_counter();
2361 				} else {
2362 					/*
2363 					 * If a pre-shared key was provided,
2364 					 * stash it in the client cookie
2365 					 * field of the tcpcb for use during
2366 					 * connect.
2367 					 */
2368 					if (sopt->sopt_valsize ==
2369 					    sizeof(tfo_optval)) {
2370 						memcpy(tp->t_tfo_cookie.client,
2371 						       tfo_optval.psk,
2372 						       TCP_FASTOPEN_PSK_LEN);
2373 						tp->t_tfo_client_cookie_len =
2374 						    TCP_FASTOPEN_PSK_LEN;
2375 					}
2376 				}
2377 				tp->t_flags |= TF_FASTOPEN;
2378 			} else
2379 				tp->t_flags &= ~TF_FASTOPEN;
2380 			goto unlock_and_done;
2381 		}
2382 
2383 #ifdef TCP_BLACKBOX
2384 		case TCP_LOG:
2385 			INP_WUNLOCK(inp);
2386 			error = sooptcopyin(sopt, &optval, sizeof optval,
2387 			    sizeof optval);
2388 			if (error)
2389 				return (error);
2390 
2391 			INP_WLOCK_RECHECK(inp);
2392 			error = tcp_log_state_change(tp, optval);
2393 			goto unlock_and_done;
2394 
2395 		case TCP_LOGBUF:
2396 			INP_WUNLOCK(inp);
2397 			error = EINVAL;
2398 			break;
2399 
2400 		case TCP_LOGID:
2401 			INP_WUNLOCK(inp);
2402 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
2403 			if (error)
2404 				break;
2405 			buf[sopt->sopt_valsize] = '\0';
2406 			INP_WLOCK_RECHECK(inp);
2407 			error = tcp_log_set_id(tp, buf);
2408 			/* tcp_log_set_id() unlocks the INP. */
2409 			break;
2410 
2411 		case TCP_LOGDUMP:
2412 		case TCP_LOGDUMPID:
2413 			INP_WUNLOCK(inp);
2414 			error =
2415 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
2416 			if (error)
2417 				break;
2418 			buf[sopt->sopt_valsize] = '\0';
2419 			INP_WLOCK_RECHECK(inp);
2420 			if (sopt->sopt_name == TCP_LOGDUMP) {
2421 				error = tcp_log_dump_tp_logbuf(tp, buf,
2422 				    M_WAITOK, true);
2423 				INP_WUNLOCK(inp);
2424 			} else {
2425 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
2426 				/*
2427 				 * tcp_log_dump_tp_bucket_logbufs() drops the
2428 				 * INP lock.
2429 				 */
2430 			}
2431 			break;
2432 #endif
2433 
2434 		default:
2435 			INP_WUNLOCK(inp);
2436 			error = ENOPROTOOPT;
2437 			break;
2438 		}
2439 		break;
2440 
2441 	case SOPT_GET:
2442 		tp = intotcpcb(inp);
2443 		switch (sopt->sopt_name) {
2444 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2445 		case TCP_MD5SIG:
2446 			INP_WUNLOCK(inp);
2447 			if (!TCPMD5_ENABLED())
2448 				return (ENOPROTOOPT);
2449 			error = TCPMD5_PCBCTL(inp, sopt);
2450 			break;
2451 #endif
2452 
2453 		case TCP_NODELAY:
2454 			optval = tp->t_flags & TF_NODELAY;
2455 			INP_WUNLOCK(inp);
2456 			error = sooptcopyout(sopt, &optval, sizeof optval);
2457 			break;
2458 		case TCP_MAXSEG:
2459 			optval = tp->t_maxseg;
2460 			INP_WUNLOCK(inp);
2461 			error = sooptcopyout(sopt, &optval, sizeof optval);
2462 			break;
2463 		case TCP_REMOTE_UDP_ENCAPS_PORT:
2464 			optval = ntohs(tp->t_port);
2465 			INP_WUNLOCK(inp);
2466 			error = sooptcopyout(sopt, &optval, sizeof optval);
2467 			break;
2468 		case TCP_NOOPT:
2469 			optval = tp->t_flags & TF_NOOPT;
2470 			INP_WUNLOCK(inp);
2471 			error = sooptcopyout(sopt, &optval, sizeof optval);
2472 			break;
2473 		case TCP_NOPUSH:
2474 			optval = tp->t_flags & TF_NOPUSH;
2475 			INP_WUNLOCK(inp);
2476 			error = sooptcopyout(sopt, &optval, sizeof optval);
2477 			break;
2478 		case TCP_INFO:
2479 			tcp_fill_info(tp, &ti);
2480 			INP_WUNLOCK(inp);
2481 			error = sooptcopyout(sopt, &ti, sizeof ti);
2482 			break;
2483 		case TCP_STATS:
2484 			{
2485 #ifdef STATS
2486 			int nheld;
2487 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2488 
2489 			error = 0;
2490 			socklen_t outsbsz = sopt->sopt_valsize;
2491 			if (tp->t_stats == NULL)
2492 				error = ENOENT;
2493 			else if (outsbsz >= tp->t_stats->cursz)
2494 				outsbsz = tp->t_stats->cursz;
2495 			else if (outsbsz >= sizeof(struct statsblob))
2496 				outsbsz = sizeof(struct statsblob);
2497 			else
2498 				error = EINVAL;
2499 			INP_WUNLOCK(inp);
2500 			if (error)
2501 				break;
2502 
2503 			sbp = sopt->sopt_val;
2504 			nheld = atop(round_page(((vm_offset_t)sbp) +
2505 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2506 			vm_page_t ma[nheld];
2507 			if (vm_fault_quick_hold_pages(
2508 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2509 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2510 			    nheld) < 0) {
2511 				error = EFAULT;
2512 				break;
2513 			}
2514 
2515 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2516 			    SIZEOF_MEMBER(struct statsblob, flags))))
2517 				goto unhold;
2518 
2519 			INP_WLOCK_RECHECK(inp);
2520 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2521 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2522 			INP_WUNLOCK(inp);
2523 			sopt->sopt_valsize = outsbsz;
2524 unhold:
2525 			vm_page_unhold_pages(ma, nheld);
2526 #else
2527 			INP_WUNLOCK(inp);
2528 			error = EOPNOTSUPP;
2529 #endif /* !STATS */
2530 			break;
2531 			}
2532 		case TCP_CONGESTION:
2533 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2534 			INP_WUNLOCK(inp);
2535 			error = sooptcopyout(sopt, buf, len + 1);
2536 			break;
2537 		case TCP_MAXUNACKTIME:
2538 		case TCP_KEEPIDLE:
2539 		case TCP_KEEPINTVL:
2540 		case TCP_KEEPINIT:
2541 		case TCP_KEEPCNT:
2542 			switch (sopt->sopt_name) {
2543 			case TCP_MAXUNACKTIME:
2544 				ui = TP_MAXUNACKTIME(tp) / hz;
2545 				break;
2546 			case TCP_KEEPIDLE:
2547 				ui = TP_KEEPIDLE(tp) / hz;
2548 				break;
2549 			case TCP_KEEPINTVL:
2550 				ui = TP_KEEPINTVL(tp) / hz;
2551 				break;
2552 			case TCP_KEEPINIT:
2553 				ui = TP_KEEPINIT(tp) / hz;
2554 				break;
2555 			case TCP_KEEPCNT:
2556 				ui = TP_KEEPCNT(tp);
2557 				break;
2558 			}
2559 			INP_WUNLOCK(inp);
2560 			error = sooptcopyout(sopt, &ui, sizeof(ui));
2561 			break;
2562 #ifdef TCPPCAP
2563 		case TCP_PCAP_OUT:
2564 		case TCP_PCAP_IN:
2565 			optval = tcp_pcap_get_sock_max(
2566 					(sopt->sopt_name == TCP_PCAP_OUT) ?
2567 					&(tp->t_outpkts) : &(tp->t_inpkts));
2568 			INP_WUNLOCK(inp);
2569 			error = sooptcopyout(sopt, &optval, sizeof optval);
2570 			break;
2571 #endif
2572 		case TCP_FASTOPEN:
2573 			optval = tp->t_flags & TF_FASTOPEN;
2574 			INP_WUNLOCK(inp);
2575 			error = sooptcopyout(sopt, &optval, sizeof optval);
2576 			break;
2577 #ifdef TCP_BLACKBOX
2578 		case TCP_LOG:
2579 			optval = tp->t_logstate;
2580 			INP_WUNLOCK(inp);
2581 			error = sooptcopyout(sopt, &optval, sizeof(optval));
2582 			break;
2583 		case TCP_LOGBUF:
2584 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2585 			error = tcp_log_getlogbuf(sopt, tp);
2586 			break;
2587 		case TCP_LOGID:
2588 			len = tcp_log_get_id(tp, buf);
2589 			INP_WUNLOCK(inp);
2590 			error = sooptcopyout(sopt, buf, len + 1);
2591 			break;
2592 		case TCP_LOGDUMP:
2593 		case TCP_LOGDUMPID:
2594 			INP_WUNLOCK(inp);
2595 			error = EINVAL;
2596 			break;
2597 #endif
2598 #ifdef KERN_TLS
2599 		case TCP_TXTLS_MODE:
2600 			error = ktls_get_tx_mode(so, &optval);
2601 			INP_WUNLOCK(inp);
2602 			if (error == 0)
2603 				error = sooptcopyout(sopt, &optval,
2604 				    sizeof(optval));
2605 			break;
2606 		case TCP_RXTLS_MODE:
2607 			error = ktls_get_rx_mode(so, &optval);
2608 			INP_WUNLOCK(inp);
2609 			if (error == 0)
2610 				error = sooptcopyout(sopt, &optval,
2611 				    sizeof(optval));
2612 			break;
2613 #endif
2614 		case TCP_LRD:
2615 			optval = tp->t_flags & TF_LRD;
2616 			INP_WUNLOCK(inp);
2617 			error = sooptcopyout(sopt, &optval, sizeof optval);
2618 			break;
2619 		default:
2620 			INP_WUNLOCK(inp);
2621 			error = ENOPROTOOPT;
2622 			break;
2623 		}
2624 		break;
2625 	}
2626 	return (error);
2627 }
2628 #undef INP_WLOCK_RECHECK
2629 #undef INP_WLOCK_RECHECK_CLEANUP
2630 
2631 /*
2632  * Initiate (or continue) disconnect.
2633  * If embryonic state, just send reset (once).
2634  * If in ``let data drain'' option and linger null, just drop.
2635  * Otherwise (hard), mark socket disconnecting and drop
2636  * current input data; switch states based on user close, and
2637  * send segment to peer (with FIN).
2638  */
2639 static void
2640 tcp_disconnect(struct tcpcb *tp)
2641 {
2642 	struct inpcb *inp = tptoinpcb(tp);
2643 	struct socket *so = tptosocket(tp);
2644 
2645 	NET_EPOCH_ASSERT();
2646 	INP_WLOCK_ASSERT(inp);
2647 
2648 	/*
2649 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2650 	 * socket is still open.
2651 	 */
2652 	if (tp->t_state < TCPS_ESTABLISHED &&
2653 	    !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2654 		tp = tcp_close(tp);
2655 		KASSERT(tp != NULL,
2656 		    ("tcp_disconnect: tcp_close() returned NULL"));
2657 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2658 		tp = tcp_drop(tp, 0);
2659 		KASSERT(tp != NULL,
2660 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2661 	} else {
2662 		soisdisconnecting(so);
2663 		sbflush(&so->so_rcv);
2664 		tcp_usrclosed(tp);
2665 		if (!(inp->inp_flags & INP_DROPPED))
2666 			/* Ignore stack's drop request, we already at it. */
2667 			(void)tcp_output_nodrop(tp);
2668 	}
2669 }
2670 
2671 /*
2672  * User issued close, and wish to trail through shutdown states:
2673  * if never received SYN, just forget it.  If got a SYN from peer,
2674  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2675  * If already got a FIN from peer, then almost done; go to LAST_ACK
2676  * state.  In all other cases, have already sent FIN to peer (e.g.
2677  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2678  * for peer to send FIN or not respond to keep-alives, etc.
2679  * We can let the user exit from the close as soon as the FIN is acked.
2680  */
2681 static void
2682 tcp_usrclosed(struct tcpcb *tp)
2683 {
2684 
2685 	NET_EPOCH_ASSERT();
2686 	INP_WLOCK_ASSERT(tptoinpcb(tp));
2687 
2688 	switch (tp->t_state) {
2689 	case TCPS_LISTEN:
2690 #ifdef TCP_OFFLOAD
2691 		tcp_offload_listen_stop(tp);
2692 #endif
2693 		tcp_state_change(tp, TCPS_CLOSED);
2694 		/* FALLTHROUGH */
2695 	case TCPS_CLOSED:
2696 		tp = tcp_close(tp);
2697 		/*
2698 		 * tcp_close() should never return NULL here as the socket is
2699 		 * still open.
2700 		 */
2701 		KASSERT(tp != NULL,
2702 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2703 		break;
2704 
2705 	case TCPS_SYN_SENT:
2706 	case TCPS_SYN_RECEIVED:
2707 		tp->t_flags |= TF_NEEDFIN;
2708 		break;
2709 
2710 	case TCPS_ESTABLISHED:
2711 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2712 		break;
2713 
2714 	case TCPS_CLOSE_WAIT:
2715 		tcp_state_change(tp, TCPS_LAST_ACK);
2716 		break;
2717 	}
2718 	if (tp->t_acktime == 0)
2719 		tp->t_acktime = ticks;
2720 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
2721 		soisdisconnected(tptosocket(tp));
2722 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
2723 		if (tp->t_state == TCPS_FIN_WAIT_2) {
2724 			int timeout;
2725 
2726 			timeout = (tcp_fast_finwait2_recycle) ?
2727 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2728 			tcp_timer_activate(tp, TT_2MSL, timeout);
2729 		}
2730 	}
2731 }
2732 
2733 #ifdef DDB
2734 static void
2735 db_print_indent(int indent)
2736 {
2737 	int i;
2738 
2739 	for (i = 0; i < indent; i++)
2740 		db_printf(" ");
2741 }
2742 
2743 static void
2744 db_print_tstate(int t_state)
2745 {
2746 
2747 	switch (t_state) {
2748 	case TCPS_CLOSED:
2749 		db_printf("TCPS_CLOSED");
2750 		return;
2751 
2752 	case TCPS_LISTEN:
2753 		db_printf("TCPS_LISTEN");
2754 		return;
2755 
2756 	case TCPS_SYN_SENT:
2757 		db_printf("TCPS_SYN_SENT");
2758 		return;
2759 
2760 	case TCPS_SYN_RECEIVED:
2761 		db_printf("TCPS_SYN_RECEIVED");
2762 		return;
2763 
2764 	case TCPS_ESTABLISHED:
2765 		db_printf("TCPS_ESTABLISHED");
2766 		return;
2767 
2768 	case TCPS_CLOSE_WAIT:
2769 		db_printf("TCPS_CLOSE_WAIT");
2770 		return;
2771 
2772 	case TCPS_FIN_WAIT_1:
2773 		db_printf("TCPS_FIN_WAIT_1");
2774 		return;
2775 
2776 	case TCPS_CLOSING:
2777 		db_printf("TCPS_CLOSING");
2778 		return;
2779 
2780 	case TCPS_LAST_ACK:
2781 		db_printf("TCPS_LAST_ACK");
2782 		return;
2783 
2784 	case TCPS_FIN_WAIT_2:
2785 		db_printf("TCPS_FIN_WAIT_2");
2786 		return;
2787 
2788 	case TCPS_TIME_WAIT:
2789 		db_printf("TCPS_TIME_WAIT");
2790 		return;
2791 
2792 	default:
2793 		db_printf("unknown");
2794 		return;
2795 	}
2796 }
2797 
2798 static void
2799 db_print_tflags(u_int t_flags)
2800 {
2801 	int comma;
2802 
2803 	comma = 0;
2804 	if (t_flags & TF_ACKNOW) {
2805 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2806 		comma = 1;
2807 	}
2808 	if (t_flags & TF_DELACK) {
2809 		db_printf("%sTF_DELACK", comma ? ", " : "");
2810 		comma = 1;
2811 	}
2812 	if (t_flags & TF_NODELAY) {
2813 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2814 		comma = 1;
2815 	}
2816 	if (t_flags & TF_NOOPT) {
2817 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2818 		comma = 1;
2819 	}
2820 	if (t_flags & TF_SENTFIN) {
2821 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2822 		comma = 1;
2823 	}
2824 	if (t_flags & TF_REQ_SCALE) {
2825 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2826 		comma = 1;
2827 	}
2828 	if (t_flags & TF_RCVD_SCALE) {
2829 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2830 		comma = 1;
2831 	}
2832 	if (t_flags & TF_REQ_TSTMP) {
2833 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2834 		comma = 1;
2835 	}
2836 	if (t_flags & TF_RCVD_TSTMP) {
2837 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2838 		comma = 1;
2839 	}
2840 	if (t_flags & TF_SACK_PERMIT) {
2841 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2842 		comma = 1;
2843 	}
2844 	if (t_flags & TF_NEEDSYN) {
2845 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2846 		comma = 1;
2847 	}
2848 	if (t_flags & TF_NEEDFIN) {
2849 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2850 		comma = 1;
2851 	}
2852 	if (t_flags & TF_NOPUSH) {
2853 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2854 		comma = 1;
2855 	}
2856 	if (t_flags & TF_PREVVALID) {
2857 		db_printf("%sTF_PREVVALID", comma ? ", " : "");
2858 		comma = 1;
2859 	}
2860 	if (t_flags & TF_MORETOCOME) {
2861 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2862 		comma = 1;
2863 	}
2864 	if (t_flags & TF_SONOTCONN) {
2865 		db_printf("%sTF_SONOTCONN", comma ? ", " : "");
2866 		comma = 1;
2867 	}
2868 	if (t_flags & TF_LASTIDLE) {
2869 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2870 		comma = 1;
2871 	}
2872 	if (t_flags & TF_RXWIN0SENT) {
2873 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2874 		comma = 1;
2875 	}
2876 	if (t_flags & TF_FASTRECOVERY) {
2877 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2878 		comma = 1;
2879 	}
2880 	if (t_flags & TF_CONGRECOVERY) {
2881 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2882 		comma = 1;
2883 	}
2884 	if (t_flags & TF_WASFRECOVERY) {
2885 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2886 		comma = 1;
2887 	}
2888 	if (t_flags & TF_WASCRECOVERY) {
2889 		db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
2890 		comma = 1;
2891 	}
2892 	if (t_flags & TF_SIGNATURE) {
2893 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2894 		comma = 1;
2895 	}
2896 	if (t_flags & TF_FORCEDATA) {
2897 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2898 		comma = 1;
2899 	}
2900 	if (t_flags & TF_TSO) {
2901 		db_printf("%sTF_TSO", comma ? ", " : "");
2902 		comma = 1;
2903 	}
2904 	if (t_flags & TF_FASTOPEN) {
2905 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2906 		comma = 1;
2907 	}
2908 }
2909 
2910 static void
2911 db_print_tflags2(u_int t_flags2)
2912 {
2913 	int comma;
2914 
2915 	comma = 0;
2916 	if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
2917 		db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
2918 		comma = 1;
2919 	}
2920 	if (t_flags2 & TF2_PLPMTU_PMTUD) {
2921 		db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
2922 		comma = 1;
2923 	}
2924 	if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
2925 		db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
2926 		comma = 1;
2927 	}
2928 	if (t_flags2 & TF2_LOG_AUTO) {
2929 		db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
2930 		comma = 1;
2931 	}
2932 	if (t_flags2 & TF2_DROP_AF_DATA) {
2933 		db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
2934 		comma = 1;
2935 	}
2936 	if (t_flags2 & TF2_ECN_PERMIT) {
2937 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
2938 		comma = 1;
2939 	}
2940 	if (t_flags2 & TF2_ECN_SND_CWR) {
2941 		db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
2942 		comma = 1;
2943 	}
2944 	if (t_flags2 & TF2_ECN_SND_ECE) {
2945 		db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
2946 		comma = 1;
2947 	}
2948 	if (t_flags2 & TF2_ACE_PERMIT) {
2949 		db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
2950 		comma = 1;
2951 	}
2952 	if (t_flags2 & TF2_FBYTES_COMPLETE) {
2953 		db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
2954 		comma = 1;
2955 	}
2956 }
2957 
2958 static void
2959 db_print_toobflags(char t_oobflags)
2960 {
2961 	int comma;
2962 
2963 	comma = 0;
2964 	if (t_oobflags & TCPOOB_HAVEDATA) {
2965 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2966 		comma = 1;
2967 	}
2968 	if (t_oobflags & TCPOOB_HADDATA) {
2969 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2970 		comma = 1;
2971 	}
2972 }
2973 
2974 static void
2975 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2976 {
2977 
2978 	db_print_indent(indent);
2979 	db_printf("%s at %p\n", name, tp);
2980 
2981 	indent += 2;
2982 
2983 	db_print_indent(indent);
2984 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
2985 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2986 
2987 	db_print_indent(indent);
2988 	db_printf("t_callout: %p   t_timers: %p\n",
2989 	    &tp->t_callout, &tp->t_timers);
2990 
2991 	db_print_indent(indent);
2992 	db_printf("t_state: %d (", tp->t_state);
2993 	db_print_tstate(tp->t_state);
2994 	db_printf(")\n");
2995 
2996 	db_print_indent(indent);
2997 	db_printf("t_flags: 0x%x (", tp->t_flags);
2998 	db_print_tflags(tp->t_flags);
2999 	db_printf(")\n");
3000 
3001 	db_print_indent(indent);
3002 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
3003 	db_print_tflags2(tp->t_flags2);
3004 	db_printf(")\n");
3005 
3006 	db_print_indent(indent);
3007 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: 0x%08x\n",
3008 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
3009 
3010 	db_print_indent(indent);
3011 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
3012 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
3013 
3014 	db_print_indent(indent);
3015 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
3016 	    tp->iss, tp->irs, tp->rcv_nxt);
3017 
3018 	db_print_indent(indent);
3019 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
3020 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
3021 
3022 	db_print_indent(indent);
3023 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
3024 	   tp->snd_wnd, tp->snd_cwnd);
3025 
3026 	db_print_indent(indent);
3027 	db_printf("snd_ssthresh: %u   snd_recover: "
3028 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3029 
3030 	db_print_indent(indent);
3031 	db_printf("t_rcvtime: %u   t_startime: %u\n",
3032 	    tp->t_rcvtime, tp->t_starttime);
3033 
3034 	db_print_indent(indent);
3035 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
3036 	    tp->t_rtttime, tp->t_rtseq);
3037 
3038 	db_print_indent(indent);
3039 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
3040 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3041 
3042 	db_print_indent(indent);
3043 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u\n",
3044 	    tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin);
3045 
3046 	db_print_indent(indent);
3047 	db_printf("t_rttupdated: %u   max_sndwnd: %u   t_softerror: %d\n",
3048 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3049 
3050 	db_print_indent(indent);
3051 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3052 	db_print_toobflags(tp->t_oobflags);
3053 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
3054 
3055 	db_print_indent(indent);
3056 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
3057 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3058 
3059 	db_print_indent(indent);
3060 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
3061 	    tp->ts_recent, tp->ts_recent_age);
3062 
3063 	db_print_indent(indent);
3064 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
3065 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3066 
3067 	db_print_indent(indent);
3068 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
3069 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3070 	    tp->snd_recover_prev, tp->t_badrxtwin);
3071 
3072 	db_print_indent(indent);
3073 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
3074 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3075 
3076 	db_print_indent(indent);
3077 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
3078 	    tp->snd_fack, tp->rcv_numsacks);
3079 
3080 	/* Skip sackblks, sackhint. */
3081 
3082 	db_print_indent(indent);
3083 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
3084 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3085 }
3086 
3087 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3088 {
3089 	struct tcpcb *tp;
3090 
3091 	if (!have_addr) {
3092 		db_printf("usage: show tcpcb <addr>\n");
3093 		return;
3094 	}
3095 	tp = (struct tcpcb *)addr;
3096 
3097 	db_print_tcpcb(tp, "tcpcb", 0);
3098 }
3099 #endif
3100