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