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