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