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