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