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