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