xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 6e76489098c6dc415ac3f2ae084154c3c22558ec)
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 pru_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) || so->so_options & SO_REUSEPORT_LB) {
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) || so->so_options & SO_REUSEPORT_LB) {
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  * pru_*() routines, the mbuf chains are our responsibility.  We
911  * must either enqueue them or free them.  The other pru_* 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_ready =		tcp_usr_ready,
1423 	.pr_shutdown =		tcp_usr_shutdown,
1424 	.pr_sockaddr =		in_getsockaddr,
1425 	.pr_sosetlabel =	in_pcbsosetlabel,
1426 	.pr_close =		tcp_usr_close,
1427 };
1428 #endif /* INET */
1429 
1430 #ifdef INET6
1431 struct protosw tcp6_protosw = {
1432 	.pr_type =		SOCK_STREAM,
1433 	.pr_protocol =		IPPROTO_TCP,
1434 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD |
1435 				    PR_CAPATTACH,
1436 	.pr_ctloutput =		tcp_ctloutput,
1437 	.pr_abort =		tcp_usr_abort,
1438 	.pr_accept =		tcp6_usr_accept,
1439 	.pr_attach =		tcp_usr_attach,
1440 	.pr_bind =		tcp6_usr_bind,
1441 	.pr_connect =		tcp6_usr_connect,
1442 	.pr_control =		in6_control,
1443 	.pr_detach =		tcp_usr_detach,
1444 	.pr_disconnect =	tcp_usr_disconnect,
1445 	.pr_listen =		tcp6_usr_listen,
1446 	.pr_peeraddr =		in6_mapped_peeraddr,
1447 	.pr_rcvd =		tcp_usr_rcvd,
1448 	.pr_rcvoob =		tcp_usr_rcvoob,
1449 	.pr_send =		tcp_usr_send,
1450 	.pr_ready =		tcp_usr_ready,
1451 	.pr_shutdown =		tcp_usr_shutdown,
1452 	.pr_sockaddr =		in6_mapped_sockaddr,
1453 	.pr_sosetlabel =	in_pcbsosetlabel,
1454 	.pr_close =		tcp_usr_close,
1455 };
1456 #endif /* INET6 */
1457 
1458 #ifdef INET
1459 /*
1460  * Common subroutine to open a TCP connection to remote host specified
1461  * by struct sockaddr_in.  Call in_pcbconnect() to choose local host address
1462  * and assign a local port number and install the inpcb into the hash.
1463  * Initialize connection parameters and enter SYN-SENT state.
1464  */
1465 static int
tcp_connect(struct tcpcb * tp,struct sockaddr_in * sin,struct thread * td)1466 tcp_connect(struct tcpcb *tp, struct sockaddr_in *sin, struct thread *td)
1467 {
1468 	struct inpcb *inp = tptoinpcb(tp);
1469 	struct socket *so = tptosocket(tp);
1470 	int error;
1471 
1472 	NET_EPOCH_ASSERT();
1473 	INP_WLOCK_ASSERT(inp);
1474 
1475 	if (__predict_false((so->so_state &
1476 	    (SS_ISCONNECTING | SS_ISCONNECTED | SS_ISDISCONNECTING |
1477 	    SS_ISDISCONNECTED)) != 0))
1478 		return (EISCONN);
1479 
1480 	INP_HASH_WLOCK(&V_tcbinfo);
1481 	error = in_pcbconnect(inp, sin, td->td_ucred);
1482 	INP_HASH_WUNLOCK(&V_tcbinfo);
1483 	if (error != 0)
1484 		return (error);
1485 
1486 	/*
1487 	 * Compute window scaling to request:
1488 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1489 	 * XXX: This should move to tcp_output().
1490 	 */
1491 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1492 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1493 		tp->request_r_scale++;
1494 
1495 	soisconnecting(so);
1496 	TCPSTAT_INC(tcps_connattempt);
1497 	tcp_state_change(tp, TCPS_SYN_SENT);
1498 	tp->iss = tcp_new_isn(&inp->inp_inc);
1499 	if (tp->t_flags & TF_REQ_TSTMP)
1500 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1501 	tcp_sendseqinit(tp);
1502 
1503 	return (0);
1504 }
1505 #endif /* INET */
1506 
1507 #ifdef INET6
1508 static int
tcp6_connect(struct tcpcb * tp,struct sockaddr_in6 * sin6,struct thread * td)1509 tcp6_connect(struct tcpcb *tp, struct sockaddr_in6 *sin6, struct thread *td)
1510 {
1511 	struct inpcb *inp = tptoinpcb(tp);
1512 	struct socket *so = tptosocket(tp);
1513 	int error;
1514 
1515 	NET_EPOCH_ASSERT();
1516 	INP_WLOCK_ASSERT(inp);
1517 
1518 	if (__predict_false((so->so_state &
1519 	    (SS_ISCONNECTING | SS_ISCONNECTED)) != 0))
1520 		return (EISCONN);
1521 
1522 	INP_HASH_WLOCK(&V_tcbinfo);
1523 	error = in6_pcbconnect(inp, sin6, td->td_ucred, true);
1524 	INP_HASH_WUNLOCK(&V_tcbinfo);
1525 	if (error != 0)
1526 		return (error);
1527 
1528 	/* Compute window scaling to request.  */
1529 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1530 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1531 		tp->request_r_scale++;
1532 
1533 	soisconnecting(so);
1534 	TCPSTAT_INC(tcps_connattempt);
1535 	tcp_state_change(tp, TCPS_SYN_SENT);
1536 	tp->iss = tcp_new_isn(&inp->inp_inc);
1537 	if (tp->t_flags & TF_REQ_TSTMP)
1538 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1539 	tcp_sendseqinit(tp);
1540 
1541 	return (0);
1542 }
1543 #endif /* INET6 */
1544 
1545 /*
1546  * Export TCP internal state information via a struct tcp_info, based on the
1547  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1548  * (TCP state machine, etc).  We export all information using FreeBSD-native
1549  * constants -- for example, the numeric values for tcpi_state will differ
1550  * from Linux.
1551  */
1552 void
tcp_fill_info(const struct tcpcb * tp,struct tcp_info * ti)1553 tcp_fill_info(const struct tcpcb *tp, struct tcp_info *ti)
1554 {
1555 
1556 	INP_LOCK_ASSERT(tptoinpcb(tp));
1557 	bzero(ti, sizeof(*ti));
1558 
1559 	ti->tcpi_state = tp->t_state;
1560 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1561 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1562 	if (tp->t_flags & TF_SACK_PERMIT)
1563 		ti->tcpi_options |= TCPI_OPT_SACK;
1564 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1565 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1566 		ti->tcpi_snd_wscale = tp->snd_scale;
1567 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1568 	}
1569 	switch (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) {
1570 		case TF2_ECN_PERMIT:
1571 			ti->tcpi_options |= TCPI_OPT_ECN;
1572 			break;
1573 		case TF2_ACE_PERMIT:
1574 			/* FALLTHROUGH */
1575 		case TF2_ECN_PERMIT | TF2_ACE_PERMIT:
1576 			ti->tcpi_options |= TCPI_OPT_ACE;
1577 			break;
1578 		default:
1579 			break;
1580 	}
1581 	if (tp->t_flags & TF_FASTOPEN)
1582 		ti->tcpi_options |= TCPI_OPT_TFO;
1583 
1584 	ti->tcpi_rto = tp->t_rxtcur * tick;
1585 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1586 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1587 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1588 
1589 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1590 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1591 
1592 	/*
1593 	 * FreeBSD-specific extension fields for tcp_info.
1594 	 */
1595 	ti->tcpi_rcv_space = tp->rcv_wnd;
1596 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1597 	ti->tcpi_snd_wnd = tp->snd_wnd;
1598 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1599 	ti->tcpi_snd_nxt = tp->snd_nxt;
1600 	ti->tcpi_snd_mss = tp->t_maxseg;
1601 	ti->tcpi_rcv_mss = tp->t_maxseg;
1602 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1603 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1604 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1605 	ti->tcpi_snd_una = tp->snd_una;
1606 	ti->tcpi_snd_max = tp->snd_max;
1607 	ti->tcpi_rcv_numsacks = tp->rcv_numsacks;
1608 	ti->tcpi_rcv_adv = tp->rcv_adv;
1609 	ti->tcpi_dupacks = tp->t_dupacks;
1610 	ti->tcpi_rttmin = tp->t_rttlow;
1611 #ifdef TCP_OFFLOAD
1612 	if (tp->t_flags & TF_TOE) {
1613 		ti->tcpi_options |= TCPI_OPT_TOE;
1614 		tcp_offload_tcp_info(tp, ti);
1615 	}
1616 #endif
1617 	/*
1618 	 * AccECN related counters.
1619 	 */
1620 	if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) ==
1621 	    (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
1622 		/*
1623 		 * Internal counter starts at 5 for AccECN
1624 		 * but 0 for RFC3168 ECN.
1625 		 */
1626 		ti->tcpi_delivered_ce = tp->t_scep - 5;
1627 	else
1628 		ti->tcpi_delivered_ce = tp->t_scep;
1629 	ti->tcpi_received_ce = tp->t_rcep;
1630 }
1631 
1632 /*
1633  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1634  * socket option arguments.  When it re-acquires the lock after the copy, it
1635  * has to revalidate that the connection is still valid for the socket
1636  * option.
1637  */
1638 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
1639 	INP_WLOCK(inp);							\
1640 	if (inp->inp_flags & INP_DROPPED) {				\
1641 		INP_WUNLOCK(inp);					\
1642 		cleanup;						\
1643 		return (ECONNRESET);					\
1644 	}								\
1645 	tp = intotcpcb(inp);						\
1646 } while(0)
1647 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1648 
1649 int
tcp_ctloutput_set(struct inpcb * inp,struct sockopt * sopt)1650 tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt)
1651 {
1652 	struct socket *so = inp->inp_socket;
1653 	struct tcpcb *tp = intotcpcb(inp);
1654 	int error = 0;
1655 
1656 	MPASS(sopt->sopt_dir == SOPT_SET);
1657 	INP_WLOCK_ASSERT(inp);
1658 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1659 	    ("inp_flags == %x", inp->inp_flags));
1660 	KASSERT(so != NULL, ("inp_socket == NULL"));
1661 
1662 	if (sopt->sopt_level != IPPROTO_TCP) {
1663 		INP_WUNLOCK(inp);
1664 #ifdef INET6
1665 		if (inp->inp_vflag & INP_IPV6PROTO)
1666 			error = ip6_ctloutput(so, sopt);
1667 #endif
1668 #if defined(INET6) && defined(INET)
1669 		else
1670 #endif
1671 #ifdef INET
1672 			error = ip_ctloutput(so, sopt);
1673 #endif
1674 		/*
1675 		 * When an IP-level socket option affects TCP, pass control
1676 		 * down to stack tfb_tcp_ctloutput, otherwise return what
1677 		 * IP level returned.
1678 		 */
1679 		switch (sopt->sopt_level) {
1680 #ifdef INET6
1681 		case IPPROTO_IPV6:
1682 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0)
1683 				return (error);
1684 			switch (sopt->sopt_name) {
1685 			case IPV6_TCLASS:
1686 				/* Notify tcp stacks that care (e.g. RACK). */
1687 				break;
1688 			case IPV6_USE_MIN_MTU:
1689 				/* Update t_maxseg accordingly. */
1690 				break;
1691 			default:
1692 				return (error);
1693 			}
1694 			break;
1695 #endif
1696 #ifdef INET
1697 		case IPPROTO_IP:
1698 			switch (sopt->sopt_name) {
1699 			case IP_TOS:
1700 				inp->inp_ip_tos &= ~IPTOS_ECN_MASK;
1701 				break;
1702 			case IP_TTL:
1703 				/* Notify tcp stacks that care (e.g. RACK). */
1704 				break;
1705 			default:
1706 				return (error);
1707 			}
1708 			break;
1709 #endif
1710 		default:
1711 			return (error);
1712 		}
1713 		INP_WLOCK_RECHECK(inp);
1714 	} else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1715 		/*
1716 		 * Protect the TCP option TCP_FUNCTION_BLK so
1717 		 * that a sub-function can *never* overwrite this.
1718 		 */
1719 		struct tcp_function_set fsn;
1720 		struct tcp_function_block *blk;
1721 		void *ptr = NULL;
1722 
1723 		INP_WUNLOCK(inp);
1724 		error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1725 		if (error)
1726 			return (error);
1727 
1728 		INP_WLOCK_RECHECK(inp);
1729 
1730 		blk = find_and_ref_tcp_functions(&fsn);
1731 		if (blk == NULL) {
1732 			INP_WUNLOCK(inp);
1733 			return (ENOENT);
1734 		}
1735 		if (tp->t_fb == blk) {
1736 			/* You already have this */
1737 			refcount_release(&blk->tfb_refcnt);
1738 			INP_WUNLOCK(inp);
1739 			return (0);
1740 		}
1741 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1742 			refcount_release(&blk->tfb_refcnt);
1743 			INP_WUNLOCK(inp);
1744 			return (ENOENT);
1745 		}
1746 		error = (*blk->tfb_tcp_handoff_ok)(tp);
1747 		if (error) {
1748 			refcount_release(&blk->tfb_refcnt);
1749 			INP_WUNLOCK(inp);
1750 			return (error);
1751 		}
1752 		/*
1753 		 * Ensure the new stack takes ownership with a
1754 		 * clean slate on peak rate threshold.
1755 		 */
1756 		if (tp->t_fb->tfb_tcp_timer_stop_all != NULL)
1757 			tp->t_fb->tfb_tcp_timer_stop_all(tp);
1758 		if (blk->tfb_tcp_fb_init) {
1759 			error = (*blk->tfb_tcp_fb_init)(tp, &ptr);
1760 			if (error) {
1761 				/*
1762 				 * Release the ref count the lookup
1763 				 * acquired.
1764 				 */
1765 				refcount_release(&blk->tfb_refcnt);
1766 				/*
1767 				 * Now there is a chance that the
1768 				 * init() function mucked with some
1769 				 * things before it failed, such as
1770 				 * hpts or inp_flags2 or timer granularity.
1771 				 * It should not of, but lets give the old
1772 				 * stack a chance to reset to a known good state.
1773 				 */
1774 				if (tp->t_fb->tfb_switch_failed) {
1775 					(*tp->t_fb->tfb_switch_failed)(tp);
1776 				}
1777 			 	goto err_out;
1778 			}
1779 		}
1780 		if (tp->t_fb->tfb_tcp_fb_fini) {
1781 			struct epoch_tracker et;
1782 			/*
1783 			 * Tell the stack to cleanup with 0 i.e.
1784 			 * the tcb is not going away.
1785 			 */
1786 			NET_EPOCH_ENTER(et);
1787 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1788 			NET_EPOCH_EXIT(et);
1789 		}
1790 		/*
1791 		 * Release the old refcnt, the
1792 		 * lookup acquired a ref on the
1793 		 * new one already.
1794 		 */
1795 		refcount_release(&tp->t_fb->tfb_refcnt);
1796 		/*
1797 		 * Set in the new stack.
1798 		 */
1799 		tp->t_fb = blk;
1800 		tp->t_fb_ptr = ptr;
1801 #ifdef TCP_OFFLOAD
1802 		if (tp->t_flags & TF_TOE) {
1803 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
1804 			     sopt->sopt_name);
1805 		}
1806 #endif
1807 err_out:
1808 		INP_WUNLOCK(inp);
1809 		return (error);
1810 
1811 	}
1812 
1813 	/* Pass in the INP locked, callee must unlock it. */
1814 	return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1815 }
1816 
1817 static int
tcp_ctloutput_get(struct inpcb * inp,struct sockopt * sopt)1818 tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1819 {
1820 	struct socket *so = inp->inp_socket;
1821 	struct tcpcb *tp = intotcpcb(inp);
1822 	int error = 0;
1823 
1824 	MPASS(sopt->sopt_dir == SOPT_GET);
1825 	INP_WLOCK_ASSERT(inp);
1826 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1827 	    ("inp_flags == %x", inp->inp_flags));
1828 	KASSERT(so != NULL, ("inp_socket == NULL"));
1829 
1830 	if (sopt->sopt_level != IPPROTO_TCP) {
1831 		INP_WUNLOCK(inp);
1832 #ifdef INET6
1833 		if (inp->inp_vflag & INP_IPV6PROTO)
1834 			error = ip6_ctloutput(so, sopt);
1835 #endif /* INET6 */
1836 #if defined(INET6) && defined(INET)
1837 		else
1838 #endif
1839 #ifdef INET
1840 			error = ip_ctloutput(so, sopt);
1841 #endif
1842 		return (error);
1843 	}
1844 	if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1845 	     (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1846 		struct tcp_function_set fsn;
1847 
1848 		if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1849 			memset(&fsn, 0, sizeof(fsn));
1850 			find_tcp_function_alias(tp->t_fb, &fsn);
1851 		} else {
1852 			strncpy(fsn.function_set_name,
1853 			    tp->t_fb->tfb_tcp_block_name,
1854 			    TCP_FUNCTION_NAME_LEN_MAX);
1855 			fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1856 		}
1857 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1858 		INP_WUNLOCK(inp);
1859 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
1860 		return (error);
1861 	}
1862 
1863 	/* Pass in the INP locked, callee must unlock it. */
1864 	return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1865 }
1866 
1867 int
tcp_ctloutput(struct socket * so,struct sockopt * sopt)1868 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1869 {
1870 	struct	inpcb *inp;
1871 
1872 	inp = sotoinpcb(so);
1873 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1874 
1875 	INP_WLOCK(inp);
1876 	if (inp->inp_flags & INP_DROPPED) {
1877 		INP_WUNLOCK(inp);
1878 		return (ECONNRESET);
1879 	}
1880 	if (sopt->sopt_dir == SOPT_SET)
1881 		return (tcp_ctloutput_set(inp, sopt));
1882 	else if (sopt->sopt_dir == SOPT_GET)
1883 		return (tcp_ctloutput_get(inp, sopt));
1884 	else
1885 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
1886 }
1887 
1888 /*
1889  * If this assert becomes untrue, we need to change the size of the buf
1890  * variable in tcp_default_ctloutput().
1891  */
1892 #ifdef CTASSERT
1893 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1894 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1895 #endif
1896 
1897 extern struct cc_algo newreno_cc_algo;
1898 
1899 static int
tcp_set_cc_mod(struct inpcb * inp,struct sockopt * sopt)1900 tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
1901 {
1902 	struct cc_algo *algo;
1903 	void *ptr = NULL;
1904 	struct tcpcb *tp;
1905 	struct cc_var cc_mem;
1906 	char	buf[TCP_CA_NAME_MAX];
1907 	size_t mem_sz;
1908 	int error;
1909 
1910 	INP_WUNLOCK(inp);
1911 	error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1912 	if (error)
1913 		return(error);
1914 	buf[sopt->sopt_valsize] = '\0';
1915 	CC_LIST_RLOCK();
1916 	STAILQ_FOREACH(algo, &cc_list, entries) {
1917 		if (strncmp(buf, algo->name,
1918 			    TCP_CA_NAME_MAX) == 0) {
1919 			if (algo->flags & CC_MODULE_BEING_REMOVED) {
1920 				/* We can't "see" modules being unloaded */
1921 				continue;
1922 			}
1923 			break;
1924 		}
1925 	}
1926 	if (algo == NULL) {
1927 		CC_LIST_RUNLOCK();
1928 		return(ESRCH);
1929 	}
1930 	/*
1931 	 * With a reference the algorithm cannot be removed
1932 	 * so we hold a reference through the change process.
1933 	 */
1934 	cc_refer(algo);
1935 	CC_LIST_RUNLOCK();
1936 	if (algo->cb_init != NULL) {
1937 		/* We can now pre-get the memory for the CC */
1938 		mem_sz = (*algo->cc_data_sz)();
1939 		if (mem_sz == 0) {
1940 			goto no_mem_needed;
1941 		}
1942 		ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
1943 	} else {
1944 no_mem_needed:
1945 		mem_sz = 0;
1946 		ptr = NULL;
1947 	}
1948 	/*
1949 	 * Make sure its all clean and zero and also get
1950 	 * back the inplock.
1951 	 */
1952 	memset(&cc_mem, 0, sizeof(cc_mem));
1953 	INP_WLOCK(inp);
1954 	if (inp->inp_flags & INP_DROPPED) {
1955 		INP_WUNLOCK(inp);
1956 		if (ptr)
1957 			free(ptr, M_CC_MEM);
1958 		/* Release our temp reference */
1959 		CC_LIST_RLOCK();
1960 		cc_release(algo);
1961 		CC_LIST_RUNLOCK();
1962 		return (ECONNRESET);
1963 	}
1964 	tp = intotcpcb(inp);
1965 	if (ptr != NULL)
1966 		memset(ptr, 0, mem_sz);
1967 	cc_mem.tp = tp;
1968 	/*
1969 	 * We once again hold a write lock over the tcb so it's
1970 	 * safe to do these things without ordering concerns.
1971 	 * Note here we init into stack memory.
1972 	 */
1973 	if (algo->cb_init != NULL)
1974 		error = algo->cb_init(&cc_mem, ptr);
1975 	else
1976 		error = 0;
1977 	/*
1978 	 * The CC algorithms, when given their memory
1979 	 * should not fail we could in theory have a
1980 	 * KASSERT here.
1981 	 */
1982 	if (error == 0) {
1983 		/*
1984 		 * Touchdown, lets go ahead and move the
1985 		 * connection to the new CC module by
1986 		 * copying in the cc_mem after we call
1987 		 * the old ones cleanup (if any).
1988 		 */
1989 		if (CC_ALGO(tp)->cb_destroy != NULL)
1990 			CC_ALGO(tp)->cb_destroy(&tp->t_ccv);
1991 		/* Detach the old CC from the tcpcb  */
1992 		cc_detach(tp);
1993 		/* Copy in our temp memory that was inited */
1994 		memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var));
1995 		/* Now attach the new, which takes a reference */
1996 		cc_attach(tp, algo);
1997 		/* Ok now are we where we have gotten past any conn_init? */
1998 		if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
1999 			/* Yep run the connection init for the new CC */
2000 			CC_ALGO(tp)->conn_init(&tp->t_ccv);
2001 		}
2002 	} else if (ptr)
2003 		free(ptr, M_CC_MEM);
2004 	INP_WUNLOCK(inp);
2005 	/* Now lets release our temp reference */
2006 	CC_LIST_RLOCK();
2007 	cc_release(algo);
2008 	CC_LIST_RUNLOCK();
2009 	return (error);
2010 }
2011 
2012 int
tcp_default_ctloutput(struct tcpcb * tp,struct sockopt * sopt)2013 tcp_default_ctloutput(struct tcpcb *tp, struct sockopt *sopt)
2014 {
2015 	struct inpcb *inp = tptoinpcb(tp);
2016 	int	error, opt, optval;
2017 	u_int	ui;
2018 	struct	tcp_info ti;
2019 #ifdef KERN_TLS
2020 	struct tls_enable tls;
2021 	struct socket *so = inp->inp_socket;
2022 #endif
2023 	char	*pbuf, buf[TCP_LOG_ID_LEN];
2024 #ifdef STATS
2025 	struct statsblob *sbp;
2026 #endif
2027 	size_t	len;
2028 
2029 	INP_WLOCK_ASSERT(inp);
2030 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
2031 	    ("inp_flags == %x", inp->inp_flags));
2032 	KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
2033 
2034 	switch (sopt->sopt_level) {
2035 #ifdef INET6
2036 	case IPPROTO_IPV6:
2037 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
2038 		switch (sopt->sopt_name) {
2039 		case IPV6_USE_MIN_MTU:
2040 			tcp6_use_min_mtu(tp);
2041 			/* FALLTHROUGH */
2042 		}
2043 		INP_WUNLOCK(inp);
2044 		return (0);
2045 #endif
2046 #ifdef INET
2047 	case IPPROTO_IP:
2048 		INP_WUNLOCK(inp);
2049 		return (0);
2050 #endif
2051 	}
2052 
2053 	/*
2054 	 * For TCP_CCALGOOPT forward the control to CC module, for both
2055 	 * SOPT_SET and SOPT_GET.
2056 	 */
2057 	switch (sopt->sopt_name) {
2058 	case TCP_CCALGOOPT:
2059 		INP_WUNLOCK(inp);
2060 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2061 			return (EINVAL);
2062 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2063 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2064 		    sopt->sopt_valsize);
2065 		if (error) {
2066 			free(pbuf, M_TEMP);
2067 			return (error);
2068 		}
2069 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2070 		if (CC_ALGO(tp)->ctl_output != NULL)
2071 			error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf);
2072 		else
2073 			error = ENOENT;
2074 		INP_WUNLOCK(inp);
2075 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
2076 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2077 		free(pbuf, M_TEMP);
2078 		return (error);
2079 	}
2080 
2081 	switch (sopt->sopt_dir) {
2082 	case SOPT_SET:
2083 		switch (sopt->sopt_name) {
2084 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2085 		case TCP_MD5SIG:
2086 			INP_WUNLOCK(inp);
2087 			if (!TCPMD5_ENABLED())
2088 				return (ENOPROTOOPT);
2089 			error = TCPMD5_PCBCTL(inp, sopt);
2090 			if (error)
2091 				return (error);
2092 			INP_WLOCK_RECHECK(inp);
2093 			goto unlock_and_done;
2094 #endif /* IPSEC */
2095 
2096 		case TCP_NODELAY:
2097 		case TCP_NOOPT:
2098 			INP_WUNLOCK(inp);
2099 			error = sooptcopyin(sopt, &optval, sizeof optval,
2100 			    sizeof optval);
2101 			if (error)
2102 				return (error);
2103 
2104 			INP_WLOCK_RECHECK(inp);
2105 			switch (sopt->sopt_name) {
2106 			case TCP_NODELAY:
2107 				opt = TF_NODELAY;
2108 				break;
2109 			case TCP_NOOPT:
2110 				opt = TF_NOOPT;
2111 				break;
2112 			default:
2113 				opt = 0; /* dead code to fool gcc */
2114 				break;
2115 			}
2116 
2117 			if (optval)
2118 				tp->t_flags |= opt;
2119 			else
2120 				tp->t_flags &= ~opt;
2121 unlock_and_done:
2122 #ifdef TCP_OFFLOAD
2123 			if (tp->t_flags & TF_TOE) {
2124 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
2125 				    sopt->sopt_name);
2126 			}
2127 #endif
2128 			INP_WUNLOCK(inp);
2129 			break;
2130 
2131 		case TCP_NOPUSH:
2132 			INP_WUNLOCK(inp);
2133 			error = sooptcopyin(sopt, &optval, sizeof optval,
2134 			    sizeof optval);
2135 			if (error)
2136 				return (error);
2137 
2138 			INP_WLOCK_RECHECK(inp);
2139 			if (optval)
2140 				tp->t_flags |= TF_NOPUSH;
2141 			else if (tp->t_flags & TF_NOPUSH) {
2142 				tp->t_flags &= ~TF_NOPUSH;
2143 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2144 					struct epoch_tracker et;
2145 
2146 					NET_EPOCH_ENTER(et);
2147 					error = tcp_output_nodrop(tp);
2148 					NET_EPOCH_EXIT(et);
2149 				}
2150 			}
2151 			goto unlock_and_done;
2152 
2153 		case TCP_REMOTE_UDP_ENCAPS_PORT:
2154 			INP_WUNLOCK(inp);
2155 			error = sooptcopyin(sopt, &optval, sizeof optval,
2156 			    sizeof optval);
2157 			if (error)
2158 				return (error);
2159 			if ((optval < TCP_TUNNELING_PORT_MIN) ||
2160 			    (optval > TCP_TUNNELING_PORT_MAX)) {
2161 				/* Its got to be in range */
2162 				return (EINVAL);
2163 			}
2164 			if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
2165 				/* You have to have enabled a UDP tunneling port first */
2166 				return (EINVAL);
2167 			}
2168 			INP_WLOCK_RECHECK(inp);
2169 			if (tp->t_state != TCPS_CLOSED) {
2170 				/* You can't change after you are connected */
2171 				error = EINVAL;
2172 			} else {
2173 				/* Ok we are all good set the port */
2174 				tp->t_port = htons(optval);
2175 			}
2176 			goto unlock_and_done;
2177 
2178 		case TCP_MAXSEG:
2179 			INP_WUNLOCK(inp);
2180 			error = sooptcopyin(sopt, &optval, sizeof optval,
2181 			    sizeof optval);
2182 			if (error)
2183 				return (error);
2184 
2185 			INP_WLOCK_RECHECK(inp);
2186 			if (optval > 0 && optval <= tp->t_maxseg &&
2187 			    optval + 40 >= V_tcp_minmss) {
2188 				tp->t_maxseg = optval;
2189 				if (tp->t_maxseg < V_tcp_mssdflt) {
2190 					/*
2191 					 * The MSS is so small we should not process incoming
2192 					 * SACK's since we are subject to attack in such a
2193 					 * case.
2194 					 */
2195 					tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT;
2196 				} else {
2197 					tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT;
2198 				}
2199 			} else
2200 				error = EINVAL;
2201 			goto unlock_and_done;
2202 
2203 		case TCP_INFO:
2204 			INP_WUNLOCK(inp);
2205 			error = EINVAL;
2206 			break;
2207 
2208 		case TCP_STATS:
2209 			INP_WUNLOCK(inp);
2210 #ifdef STATS
2211 			error = sooptcopyin(sopt, &optval, sizeof optval,
2212 			    sizeof optval);
2213 			if (error)
2214 				return (error);
2215 
2216 			if (optval > 0)
2217 				sbp = stats_blob_alloc(
2218 				    V_tcp_perconn_stats_dflt_tpl, 0);
2219 			else
2220 				sbp = NULL;
2221 
2222 			INP_WLOCK_RECHECK(inp);
2223 			if ((tp->t_stats != NULL && sbp == NULL) ||
2224 			    (tp->t_stats == NULL && sbp != NULL)) {
2225 				struct statsblob *t = tp->t_stats;
2226 				tp->t_stats = sbp;
2227 				sbp = t;
2228 			}
2229 			INP_WUNLOCK(inp);
2230 
2231 			stats_blob_destroy(sbp);
2232 #else
2233 			return (EOPNOTSUPP);
2234 #endif /* !STATS */
2235 			break;
2236 
2237 		case TCP_CONGESTION:
2238 			error = tcp_set_cc_mod(inp, sopt);
2239 			break;
2240 
2241 		case TCP_REUSPORT_LB_NUMA:
2242 			INP_WUNLOCK(inp);
2243 			error = sooptcopyin(sopt, &optval, sizeof(optval),
2244 			    sizeof(optval));
2245 			INP_WLOCK_RECHECK(inp);
2246 			if (!error)
2247 				error = in_pcblbgroup_numa(inp, optval);
2248 			INP_WUNLOCK(inp);
2249 			break;
2250 
2251 #ifdef KERN_TLS
2252 		case TCP_TXTLS_ENABLE:
2253 			INP_WUNLOCK(inp);
2254 			error = ktls_copyin_tls_enable(sopt, &tls);
2255 			if (error != 0)
2256 				break;
2257 			error = ktls_enable_tx(so, &tls);
2258 			ktls_cleanup_tls_enable(&tls);
2259 			break;
2260 		case TCP_TXTLS_MODE:
2261 			INP_WUNLOCK(inp);
2262 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2263 			if (error != 0)
2264 				return (error);
2265 
2266 			INP_WLOCK_RECHECK(inp);
2267 			error = ktls_set_tx_mode(so, ui);
2268 			INP_WUNLOCK(inp);
2269 			break;
2270 		case TCP_RXTLS_ENABLE:
2271 			INP_WUNLOCK(inp);
2272 			error = ktls_copyin_tls_enable(sopt, &tls);
2273 			if (error != 0)
2274 				break;
2275 			error = ktls_enable_rx(so, &tls);
2276 			ktls_cleanup_tls_enable(&tls);
2277 			break;
2278 #endif
2279 		case TCP_MAXUNACKTIME:
2280 		case TCP_KEEPIDLE:
2281 		case TCP_KEEPINTVL:
2282 		case TCP_KEEPINIT:
2283 			INP_WUNLOCK(inp);
2284 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2285 			if (error)
2286 				return (error);
2287 
2288 			if (ui > (UINT_MAX / hz)) {
2289 				error = EINVAL;
2290 				break;
2291 			}
2292 			ui *= hz;
2293 
2294 			INP_WLOCK_RECHECK(inp);
2295 			switch (sopt->sopt_name) {
2296 			case TCP_MAXUNACKTIME:
2297 				tp->t_maxunacktime = ui;
2298 				break;
2299 
2300 			case TCP_KEEPIDLE:
2301 				tp->t_keepidle = ui;
2302 				/*
2303 				 * XXX: better check current remaining
2304 				 * timeout and "merge" it with new value.
2305 				 */
2306 				if ((tp->t_state > TCPS_LISTEN) &&
2307 				    (tp->t_state <= TCPS_CLOSING))
2308 					tcp_timer_activate(tp, TT_KEEP,
2309 					    TP_KEEPIDLE(tp));
2310 				break;
2311 			case TCP_KEEPINTVL:
2312 				tp->t_keepintvl = ui;
2313 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2314 				    (TP_MAXIDLE(tp) > 0))
2315 					tcp_timer_activate(tp, TT_2MSL,
2316 					    TP_MAXIDLE(tp));
2317 				break;
2318 			case TCP_KEEPINIT:
2319 				tp->t_keepinit = ui;
2320 				if (tp->t_state == TCPS_SYN_RECEIVED ||
2321 				    tp->t_state == TCPS_SYN_SENT)
2322 					tcp_timer_activate(tp, TT_KEEP,
2323 					    TP_KEEPINIT(tp));
2324 				break;
2325 			}
2326 			goto unlock_and_done;
2327 
2328 		case TCP_KEEPCNT:
2329 			INP_WUNLOCK(inp);
2330 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2331 			if (error)
2332 				return (error);
2333 
2334 			INP_WLOCK_RECHECK(inp);
2335 			tp->t_keepcnt = ui;
2336 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2337 			    (TP_MAXIDLE(tp) > 0))
2338 				tcp_timer_activate(tp, TT_2MSL,
2339 				    TP_MAXIDLE(tp));
2340 			goto unlock_and_done;
2341 
2342 		case TCP_FASTOPEN: {
2343 			struct tcp_fastopen tfo_optval;
2344 
2345 			INP_WUNLOCK(inp);
2346 			if (!V_tcp_fastopen_client_enable &&
2347 			    !V_tcp_fastopen_server_enable)
2348 				return (EPERM);
2349 
2350 			error = sooptcopyin(sopt, &tfo_optval,
2351 				    sizeof(tfo_optval), sizeof(int));
2352 			if (error)
2353 				return (error);
2354 
2355 			INP_WLOCK_RECHECK(inp);
2356 			if ((tp->t_state != TCPS_CLOSED) &&
2357 			    (tp->t_state != TCPS_LISTEN)) {
2358 				error = EINVAL;
2359 				goto unlock_and_done;
2360 			}
2361 			if (tfo_optval.enable) {
2362 				if (tp->t_state == TCPS_LISTEN) {
2363 					if (!V_tcp_fastopen_server_enable) {
2364 						error = EPERM;
2365 						goto unlock_and_done;
2366 					}
2367 
2368 					if (tp->t_tfo_pending == NULL)
2369 						tp->t_tfo_pending =
2370 						    tcp_fastopen_alloc_counter();
2371 				} else {
2372 					/*
2373 					 * If a pre-shared key was provided,
2374 					 * stash it in the client cookie
2375 					 * field of the tcpcb for use during
2376 					 * connect.
2377 					 */
2378 					if (sopt->sopt_valsize ==
2379 					    sizeof(tfo_optval)) {
2380 						memcpy(tp->t_tfo_cookie.client,
2381 						       tfo_optval.psk,
2382 						       TCP_FASTOPEN_PSK_LEN);
2383 						tp->t_tfo_client_cookie_len =
2384 						    TCP_FASTOPEN_PSK_LEN;
2385 					}
2386 				}
2387 				tp->t_flags |= TF_FASTOPEN;
2388 			} else
2389 				tp->t_flags &= ~TF_FASTOPEN;
2390 			goto unlock_and_done;
2391 		}
2392 
2393 #ifdef TCP_BLACKBOX
2394 		case TCP_LOG:
2395 			INP_WUNLOCK(inp);
2396 			error = sooptcopyin(sopt, &optval, sizeof optval,
2397 			    sizeof optval);
2398 			if (error)
2399 				return (error);
2400 
2401 			INP_WLOCK_RECHECK(inp);
2402 			error = tcp_log_state_change(tp, optval);
2403 			goto unlock_and_done;
2404 
2405 		case TCP_LOGBUF:
2406 			INP_WUNLOCK(inp);
2407 			error = EINVAL;
2408 			break;
2409 
2410 		case TCP_LOGID:
2411 			INP_WUNLOCK(inp);
2412 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
2413 			if (error)
2414 				break;
2415 			buf[sopt->sopt_valsize] = '\0';
2416 			INP_WLOCK_RECHECK(inp);
2417 			error = tcp_log_set_id(tp, buf);
2418 			/* tcp_log_set_id() unlocks the INP. */
2419 			break;
2420 
2421 		case TCP_LOGDUMP:
2422 		case TCP_LOGDUMPID:
2423 			INP_WUNLOCK(inp);
2424 			error =
2425 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
2426 			if (error)
2427 				break;
2428 			buf[sopt->sopt_valsize] = '\0';
2429 			INP_WLOCK_RECHECK(inp);
2430 			if (sopt->sopt_name == TCP_LOGDUMP) {
2431 				error = tcp_log_dump_tp_logbuf(tp, buf,
2432 				    M_WAITOK, true);
2433 				INP_WUNLOCK(inp);
2434 			} else {
2435 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
2436 				/*
2437 				 * tcp_log_dump_tp_bucket_logbufs() drops the
2438 				 * INP lock.
2439 				 */
2440 			}
2441 			break;
2442 #endif
2443 
2444 		default:
2445 			INP_WUNLOCK(inp);
2446 			error = ENOPROTOOPT;
2447 			break;
2448 		}
2449 		break;
2450 
2451 	case SOPT_GET:
2452 		tp = intotcpcb(inp);
2453 		switch (sopt->sopt_name) {
2454 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2455 		case TCP_MD5SIG:
2456 			INP_WUNLOCK(inp);
2457 			if (!TCPMD5_ENABLED())
2458 				return (ENOPROTOOPT);
2459 			error = TCPMD5_PCBCTL(inp, sopt);
2460 			break;
2461 #endif
2462 
2463 		case TCP_NODELAY:
2464 			optval = tp->t_flags & TF_NODELAY;
2465 			INP_WUNLOCK(inp);
2466 			error = sooptcopyout(sopt, &optval, sizeof optval);
2467 			break;
2468 		case TCP_MAXSEG:
2469 			optval = tp->t_maxseg;
2470 			INP_WUNLOCK(inp);
2471 			error = sooptcopyout(sopt, &optval, sizeof optval);
2472 			break;
2473 		case TCP_REMOTE_UDP_ENCAPS_PORT:
2474 			optval = ntohs(tp->t_port);
2475 			INP_WUNLOCK(inp);
2476 			error = sooptcopyout(sopt, &optval, sizeof optval);
2477 			break;
2478 		case TCP_NOOPT:
2479 			optval = tp->t_flags & TF_NOOPT;
2480 			INP_WUNLOCK(inp);
2481 			error = sooptcopyout(sopt, &optval, sizeof optval);
2482 			break;
2483 		case TCP_NOPUSH:
2484 			optval = tp->t_flags & TF_NOPUSH;
2485 			INP_WUNLOCK(inp);
2486 			error = sooptcopyout(sopt, &optval, sizeof optval);
2487 			break;
2488 		case TCP_INFO:
2489 			tcp_fill_info(tp, &ti);
2490 			INP_WUNLOCK(inp);
2491 			error = sooptcopyout(sopt, &ti, sizeof ti);
2492 			break;
2493 		case TCP_STATS:
2494 			{
2495 #ifdef STATS
2496 			int nheld;
2497 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2498 
2499 			error = 0;
2500 			socklen_t outsbsz = sopt->sopt_valsize;
2501 			if (tp->t_stats == NULL)
2502 				error = ENOENT;
2503 			else if (outsbsz >= tp->t_stats->cursz)
2504 				outsbsz = tp->t_stats->cursz;
2505 			else if (outsbsz >= sizeof(struct statsblob))
2506 				outsbsz = sizeof(struct statsblob);
2507 			else
2508 				error = EINVAL;
2509 			INP_WUNLOCK(inp);
2510 			if (error)
2511 				break;
2512 
2513 			sbp = sopt->sopt_val;
2514 			nheld = atop(round_page(((vm_offset_t)sbp) +
2515 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2516 			vm_page_t ma[nheld];
2517 			if (vm_fault_quick_hold_pages(
2518 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2519 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2520 			    nheld) < 0) {
2521 				error = EFAULT;
2522 				break;
2523 			}
2524 
2525 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2526 			    SIZEOF_MEMBER(struct statsblob, flags))))
2527 				goto unhold;
2528 
2529 			INP_WLOCK_RECHECK(inp);
2530 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2531 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2532 			INP_WUNLOCK(inp);
2533 			sopt->sopt_valsize = outsbsz;
2534 unhold:
2535 			vm_page_unhold_pages(ma, nheld);
2536 #else
2537 			INP_WUNLOCK(inp);
2538 			error = EOPNOTSUPP;
2539 #endif /* !STATS */
2540 			break;
2541 			}
2542 		case TCP_CONGESTION:
2543 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2544 			INP_WUNLOCK(inp);
2545 			error = sooptcopyout(sopt, buf, len + 1);
2546 			break;
2547 		case TCP_MAXUNACKTIME:
2548 		case TCP_KEEPIDLE:
2549 		case TCP_KEEPINTVL:
2550 		case TCP_KEEPINIT:
2551 		case TCP_KEEPCNT:
2552 			switch (sopt->sopt_name) {
2553 			case TCP_MAXUNACKTIME:
2554 				ui = TP_MAXUNACKTIME(tp) / hz;
2555 				break;
2556 			case TCP_KEEPIDLE:
2557 				ui = TP_KEEPIDLE(tp) / hz;
2558 				break;
2559 			case TCP_KEEPINTVL:
2560 				ui = TP_KEEPINTVL(tp) / hz;
2561 				break;
2562 			case TCP_KEEPINIT:
2563 				ui = TP_KEEPINIT(tp) / hz;
2564 				break;
2565 			case TCP_KEEPCNT:
2566 				ui = TP_KEEPCNT(tp);
2567 				break;
2568 			}
2569 			INP_WUNLOCK(inp);
2570 			error = sooptcopyout(sopt, &ui, sizeof(ui));
2571 			break;
2572 		case TCP_FASTOPEN:
2573 			optval = tp->t_flags & TF_FASTOPEN;
2574 			INP_WUNLOCK(inp);
2575 			error = sooptcopyout(sopt, &optval, sizeof optval);
2576 			break;
2577 #ifdef TCP_BLACKBOX
2578 		case TCP_LOG:
2579 			optval = tcp_get_bblog_state(tp);
2580 			INP_WUNLOCK(inp);
2581 			error = sooptcopyout(sopt, &optval, sizeof(optval));
2582 			break;
2583 		case TCP_LOGBUF:
2584 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2585 			error = tcp_log_getlogbuf(sopt, tp);
2586 			break;
2587 		case TCP_LOGID:
2588 			len = tcp_log_get_id(tp, buf);
2589 			INP_WUNLOCK(inp);
2590 			error = sooptcopyout(sopt, buf, len + 1);
2591 			break;
2592 		case TCP_LOGDUMP:
2593 		case TCP_LOGDUMPID:
2594 			INP_WUNLOCK(inp);
2595 			error = EINVAL;
2596 			break;
2597 #endif
2598 #ifdef KERN_TLS
2599 		case TCP_TXTLS_MODE:
2600 			error = ktls_get_tx_mode(so, &optval);
2601 			INP_WUNLOCK(inp);
2602 			if (error == 0)
2603 				error = sooptcopyout(sopt, &optval,
2604 				    sizeof(optval));
2605 			break;
2606 		case TCP_RXTLS_MODE:
2607 			error = ktls_get_rx_mode(so, &optval);
2608 			INP_WUNLOCK(inp);
2609 			if (error == 0)
2610 				error = sooptcopyout(sopt, &optval,
2611 				    sizeof(optval));
2612 			break;
2613 #endif
2614 		default:
2615 			INP_WUNLOCK(inp);
2616 			error = ENOPROTOOPT;
2617 			break;
2618 		}
2619 		break;
2620 	}
2621 	return (error);
2622 }
2623 #undef INP_WLOCK_RECHECK
2624 #undef INP_WLOCK_RECHECK_CLEANUP
2625 
2626 /*
2627  * Initiate (or continue) disconnect.
2628  * If embryonic state, just send reset (once).
2629  * If in ``let data drain'' option and linger null, just drop.
2630  * Otherwise (hard), mark socket disconnecting and drop
2631  * current input data; switch states based on user close, and
2632  * send segment to peer (with FIN).
2633  */
2634 static void
tcp_disconnect(struct tcpcb * tp)2635 tcp_disconnect(struct tcpcb *tp)
2636 {
2637 	struct inpcb *inp = tptoinpcb(tp);
2638 	struct socket *so = tptosocket(tp);
2639 
2640 	NET_EPOCH_ASSERT();
2641 	INP_WLOCK_ASSERT(inp);
2642 
2643 	/*
2644 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2645 	 * socket is still open.
2646 	 */
2647 	if (tp->t_state < TCPS_ESTABLISHED &&
2648 	    !(tp->t_state > TCPS_LISTEN && (tp->t_flags & TF_FASTOPEN))) {
2649 		tp = tcp_close(tp);
2650 		KASSERT(tp != NULL,
2651 		    ("tcp_disconnect: tcp_close() returned NULL"));
2652 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2653 		tp = tcp_drop(tp, 0);
2654 		KASSERT(tp != NULL,
2655 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2656 	} else {
2657 		soisdisconnecting(so);
2658 		sbflush(&so->so_rcv);
2659 		tcp_usrclosed(tp);
2660 		if (!(inp->inp_flags & INP_DROPPED))
2661 			/* Ignore stack's drop request, we already at it. */
2662 			(void)tcp_output_nodrop(tp);
2663 	}
2664 }
2665 
2666 /*
2667  * User issued close, and wish to trail through shutdown states:
2668  * if never received SYN, just forget it.  If got a SYN from peer,
2669  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2670  * If already got a FIN from peer, then almost done; go to LAST_ACK
2671  * state.  In all other cases, have already sent FIN to peer (e.g.
2672  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2673  * for peer to send FIN or not respond to keep-alives, etc.
2674  * We can let the user exit from the close as soon as the FIN is acked.
2675  */
2676 static void
tcp_usrclosed(struct tcpcb * tp)2677 tcp_usrclosed(struct tcpcb *tp)
2678 {
2679 
2680 	NET_EPOCH_ASSERT();
2681 	INP_WLOCK_ASSERT(tptoinpcb(tp));
2682 
2683 	switch (tp->t_state) {
2684 	case TCPS_LISTEN:
2685 #ifdef TCP_OFFLOAD
2686 		tcp_offload_listen_stop(tp);
2687 #endif
2688 		tcp_state_change(tp, TCPS_CLOSED);
2689 		/* FALLTHROUGH */
2690 	case TCPS_CLOSED:
2691 		tp = tcp_close(tp);
2692 		/*
2693 		 * tcp_close() should never return NULL here as the socket is
2694 		 * still open.
2695 		 */
2696 		KASSERT(tp != NULL,
2697 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2698 		break;
2699 
2700 	case TCPS_SYN_SENT:
2701 	case TCPS_SYN_RECEIVED:
2702 		tp->t_flags |= TF_NEEDFIN;
2703 		break;
2704 
2705 	case TCPS_ESTABLISHED:
2706 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2707 		break;
2708 
2709 	case TCPS_CLOSE_WAIT:
2710 		tcp_state_change(tp, TCPS_LAST_ACK);
2711 		break;
2712 	}
2713 	if (tp->t_acktime == 0)
2714 		tp->t_acktime = ticks;
2715 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
2716 		tcp_free_sackholes(tp);
2717 		soisdisconnected(tptosocket(tp));
2718 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
2719 		if (tp->t_state == TCPS_FIN_WAIT_2) {
2720 			int timeout;
2721 
2722 			timeout = (tcp_fast_finwait2_recycle) ?
2723 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2724 			tcp_timer_activate(tp, TT_2MSL, timeout);
2725 		}
2726 	}
2727 }
2728 
2729 #ifdef DDB
2730 static void
db_print_indent(int indent)2731 db_print_indent(int indent)
2732 {
2733 	int i;
2734 
2735 	for (i = 0; i < indent; i++)
2736 		db_printf(" ");
2737 }
2738 
2739 static void
db_print_tstate(int t_state)2740 db_print_tstate(int t_state)
2741 {
2742 
2743 	switch (t_state) {
2744 	case TCPS_CLOSED:
2745 		db_printf("TCPS_CLOSED");
2746 		return;
2747 
2748 	case TCPS_LISTEN:
2749 		db_printf("TCPS_LISTEN");
2750 		return;
2751 
2752 	case TCPS_SYN_SENT:
2753 		db_printf("TCPS_SYN_SENT");
2754 		return;
2755 
2756 	case TCPS_SYN_RECEIVED:
2757 		db_printf("TCPS_SYN_RECEIVED");
2758 		return;
2759 
2760 	case TCPS_ESTABLISHED:
2761 		db_printf("TCPS_ESTABLISHED");
2762 		return;
2763 
2764 	case TCPS_CLOSE_WAIT:
2765 		db_printf("TCPS_CLOSE_WAIT");
2766 		return;
2767 
2768 	case TCPS_FIN_WAIT_1:
2769 		db_printf("TCPS_FIN_WAIT_1");
2770 		return;
2771 
2772 	case TCPS_CLOSING:
2773 		db_printf("TCPS_CLOSING");
2774 		return;
2775 
2776 	case TCPS_LAST_ACK:
2777 		db_printf("TCPS_LAST_ACK");
2778 		return;
2779 
2780 	case TCPS_FIN_WAIT_2:
2781 		db_printf("TCPS_FIN_WAIT_2");
2782 		return;
2783 
2784 	case TCPS_TIME_WAIT:
2785 		db_printf("TCPS_TIME_WAIT");
2786 		return;
2787 
2788 	default:
2789 		db_printf("unknown");
2790 		return;
2791 	}
2792 }
2793 
2794 static void
db_print_tflags(u_int t_flags)2795 db_print_tflags(u_int t_flags)
2796 {
2797 	int comma;
2798 
2799 	comma = 0;
2800 	if (t_flags & TF_ACKNOW) {
2801 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2802 		comma = 1;
2803 	}
2804 	if (t_flags & TF_DELACK) {
2805 		db_printf("%sTF_DELACK", comma ? ", " : "");
2806 		comma = 1;
2807 	}
2808 	if (t_flags & TF_NODELAY) {
2809 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2810 		comma = 1;
2811 	}
2812 	if (t_flags & TF_NOOPT) {
2813 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2814 		comma = 1;
2815 	}
2816 	if (t_flags & TF_SENTFIN) {
2817 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2818 		comma = 1;
2819 	}
2820 	if (t_flags & TF_REQ_SCALE) {
2821 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2822 		comma = 1;
2823 	}
2824 	if (t_flags & TF_RCVD_SCALE) {
2825 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2826 		comma = 1;
2827 	}
2828 	if (t_flags & TF_REQ_TSTMP) {
2829 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2830 		comma = 1;
2831 	}
2832 	if (t_flags & TF_RCVD_TSTMP) {
2833 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2834 		comma = 1;
2835 	}
2836 	if (t_flags & TF_SACK_PERMIT) {
2837 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2838 		comma = 1;
2839 	}
2840 	if (t_flags & TF_NEEDSYN) {
2841 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2842 		comma = 1;
2843 	}
2844 	if (t_flags & TF_NEEDFIN) {
2845 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2846 		comma = 1;
2847 	}
2848 	if (t_flags & TF_NOPUSH) {
2849 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2850 		comma = 1;
2851 	}
2852 	if (t_flags & TF_PREVVALID) {
2853 		db_printf("%sTF_PREVVALID", comma ? ", " : "");
2854 		comma = 1;
2855 	}
2856 	if (t_flags & TF_WAKESOR) {
2857 		db_printf("%sTF_WAKESOR", comma ? ", " : "");
2858 		comma = 1;
2859 	}
2860 	if (t_flags & TF_GPUTINPROG) {
2861 		db_printf("%sTF_GPUTINPROG", comma ? ", " : "");
2862 		comma = 1;
2863 	}
2864 	if (t_flags & TF_MORETOCOME) {
2865 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2866 		comma = 1;
2867 	}
2868 	if (t_flags & TF_SONOTCONN) {
2869 		db_printf("%sTF_SONOTCONN", comma ? ", " : "");
2870 		comma = 1;
2871 	}
2872 	if (t_flags & TF_LASTIDLE) {
2873 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2874 		comma = 1;
2875 	}
2876 	if (t_flags & TF_RXWIN0SENT) {
2877 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2878 		comma = 1;
2879 	}
2880 	if (t_flags & TF_FASTRECOVERY) {
2881 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2882 		comma = 1;
2883 	}
2884 	if (t_flags & TF_WASFRECOVERY) {
2885 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2886 		comma = 1;
2887 	}
2888 	if (t_flags & TF_SIGNATURE) {
2889 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2890 		comma = 1;
2891 	}
2892 	if (t_flags & TF_FORCEDATA) {
2893 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2894 		comma = 1;
2895 	}
2896 	if (t_flags & TF_TSO) {
2897 		db_printf("%sTF_TSO", comma ? ", " : "");
2898 		comma = 1;
2899 	}
2900 	if (t_flags & TF_TOE) {
2901 		db_printf("%sTF_TOE", comma ? ", " : "");
2902 		comma = 1;
2903 	}
2904 	if (t_flags & TF_CLOSED) {
2905 		db_printf("%sTF_CLOSED", comma ? ", " : "");
2906 		comma = 1;
2907 	}
2908 	if (t_flags & TF_SENTSYN) {
2909 		db_printf("%sTF_SENTSYN", comma ? ", " : "");
2910 		comma = 1;
2911 	}
2912 	if (t_flags & TF_LRD) {
2913 		db_printf("%sTF_LRD", comma ? ", " : "");
2914 		comma = 1;
2915 	}
2916 	if (t_flags & TF_CONGRECOVERY) {
2917 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2918 		comma = 1;
2919 	}
2920 	if (t_flags & TF_WASCRECOVERY) {
2921 		db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
2922 		comma = 1;
2923 	}
2924 	if (t_flags & TF_FASTOPEN) {
2925 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2926 		comma = 1;
2927 	}
2928 }
2929 
2930 static void
db_print_tflags2(u_int t_flags2)2931 db_print_tflags2(u_int t_flags2)
2932 {
2933 	int comma;
2934 
2935 	comma = 0;
2936 	if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
2937 		db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
2938 		comma = 1;
2939 	}
2940 	if (t_flags2 & TF2_PLPMTU_PMTUD) {
2941 		db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
2942 		comma = 1;
2943 	}
2944 	if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
2945 		db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
2946 		comma = 1;
2947 	}
2948 	if (t_flags2 & TF2_LOG_AUTO) {
2949 		db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
2950 		comma = 1;
2951 	}
2952 	if (t_flags2 & TF2_DROP_AF_DATA) {
2953 		db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
2954 		comma = 1;
2955 	}
2956 	if (t_flags2 & TF2_ECN_PERMIT) {
2957 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
2958 		comma = 1;
2959 	}
2960 	if (t_flags2 & TF2_ECN_SND_CWR) {
2961 		db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
2962 		comma = 1;
2963 	}
2964 	if (t_flags2 & TF2_ECN_SND_ECE) {
2965 		db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
2966 		comma = 1;
2967 	}
2968 	if (t_flags2 & TF2_ACE_PERMIT) {
2969 		db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
2970 		comma = 1;
2971 	}
2972 	if (t_flags2 & TF2_HPTS_CPU_SET) {
2973 		db_printf("%sTF2_HPTS_CPU_SET", comma ? ", " : "");
2974 		comma = 1;
2975 	}
2976 	if (t_flags2 & TF2_FBYTES_COMPLETE) {
2977 		db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
2978 		comma = 1;
2979 	}
2980 	if (t_flags2 & TF2_ECN_USE_ECT1) {
2981 		db_printf("%sTF2_ECN_USE_ECT1", comma ? ", " : "");
2982 		comma = 1;
2983 	}
2984 	if (t_flags2 & TF2_TCP_ACCOUNTING) {
2985 		db_printf("%sTF2_TCP_ACCOUNTING", comma ? ", " : "");
2986 		comma = 1;
2987 	}
2988 	if (t_flags2 & TF2_HPTS_CALLS) {
2989 		db_printf("%sTF2_HPTS_CALLS", comma ? ", " : "");
2990 		comma = 1;
2991 	}
2992 	if (t_flags2 & TF2_MBUF_L_ACKS) {
2993 		db_printf("%sTF2_MBUF_L_ACKS", comma ? ", " : "");
2994 		comma = 1;
2995 	}
2996 	if (t_flags2 & TF2_MBUF_ACKCMP) {
2997 		db_printf("%sTF2_MBUF_ACKCMP", comma ? ", " : "");
2998 		comma = 1;
2999 	}
3000 	if (t_flags2 & TF2_SUPPORTS_MBUFQ) {
3001 		db_printf("%sTF2_SUPPORTS_MBUFQ", comma ? ", " : "");
3002 		comma = 1;
3003 	}
3004 	if (t_flags2 & TF2_MBUF_QUEUE_READY) {
3005 		db_printf("%sTF2_MBUF_QUEUE_READY", comma ? ", " : "");
3006 		comma = 1;
3007 	}
3008 	if (t_flags2 & TF2_DONT_SACK_QUEUE) {
3009 		db_printf("%sTF2_DONT_SACK_QUEUE", comma ? ", " : "");
3010 		comma = 1;
3011 	}
3012 	if (t_flags2 & TF2_CANNOT_DO_ECN) {
3013 		db_printf("%sTF2_CANNOT_DO_ECN", comma ? ", " : "");
3014 		comma = 1;
3015 	}
3016 	if (t_flags2 & TF2_PROC_SACK_PROHIBIT) {
3017 		db_printf("%sTF2_PROC_SACK_PROHIBIT", comma ? ", " : "");
3018 		comma = 1;
3019 	}
3020 	if (t_flags2 & TF2_IPSEC_TSO) {
3021 		db_printf("%sTF2_IPSEC_TSO", comma ? ", " : "");
3022 		comma = 1;
3023 	}
3024 	if (t_flags2 & TF2_NO_ISS_CHECK) {
3025 		db_printf("%sTF2_NO_ISS_CHECK", comma ? ", " : "");
3026 		comma = 1;
3027 	}
3028 }
3029 
3030 static void
db_print_toobflags(char t_oobflags)3031 db_print_toobflags(char t_oobflags)
3032 {
3033 	int comma;
3034 
3035 	comma = 0;
3036 	if (t_oobflags & TCPOOB_HAVEDATA) {
3037 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
3038 		comma = 1;
3039 	}
3040 	if (t_oobflags & TCPOOB_HADDATA) {
3041 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
3042 		comma = 1;
3043 	}
3044 }
3045 
3046 static void
db_print_tcpcb(struct tcpcb * tp,const char * name,int indent)3047 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
3048 {
3049 
3050 	db_print_indent(indent);
3051 	db_printf("%s at %p\n", name, tp);
3052 
3053 	indent += 2;
3054 
3055 	db_print_indent(indent);
3056 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
3057 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
3058 
3059 	db_print_indent(indent);
3060 	db_printf("t_callout: %p   t_timers: %p\n",
3061 	    &tp->t_callout, &tp->t_timers);
3062 
3063 	db_print_indent(indent);
3064 	db_printf("t_state: %d (", tp->t_state);
3065 	db_print_tstate(tp->t_state);
3066 	db_printf(")\n");
3067 
3068 	db_print_indent(indent);
3069 	db_printf("t_flags: 0x%x (", tp->t_flags);
3070 	db_print_tflags(tp->t_flags);
3071 	db_printf(")\n");
3072 
3073 	db_print_indent(indent);
3074 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
3075 	db_print_tflags2(tp->t_flags2);
3076 	db_printf(")\n");
3077 
3078 	db_print_indent(indent);
3079 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: 0x%08x\n",
3080 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
3081 
3082 	db_print_indent(indent);
3083 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
3084 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
3085 
3086 	db_print_indent(indent);
3087 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
3088 	    tp->iss, tp->irs, tp->rcv_nxt);
3089 
3090 	db_print_indent(indent);
3091 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
3092 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
3093 
3094 	db_print_indent(indent);
3095 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
3096 	   tp->snd_wnd, tp->snd_cwnd);
3097 
3098 	db_print_indent(indent);
3099 	db_printf("snd_ssthresh: %u   snd_recover: "
3100 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3101 
3102 	db_print_indent(indent);
3103 	db_printf("t_rcvtime: %u   t_startime: %u\n",
3104 	    tp->t_rcvtime, tp->t_starttime);
3105 
3106 	db_print_indent(indent);
3107 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
3108 	    tp->t_rtttime, tp->t_rtseq);
3109 
3110 	db_print_indent(indent);
3111 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
3112 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3113 
3114 	db_print_indent(indent);
3115 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u\n",
3116 	    tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin);
3117 
3118 	db_print_indent(indent);
3119 	db_printf("t_rttupdated: %u   max_sndwnd: %u   t_softerror: %d\n",
3120 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3121 
3122 	db_print_indent(indent);
3123 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3124 	db_print_toobflags(tp->t_oobflags);
3125 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
3126 
3127 	db_print_indent(indent);
3128 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
3129 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3130 
3131 	db_print_indent(indent);
3132 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
3133 	    tp->ts_recent, tp->ts_recent_age);
3134 
3135 	db_print_indent(indent);
3136 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
3137 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3138 
3139 	db_print_indent(indent);
3140 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
3141 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3142 	    tp->snd_recover_prev, tp->t_badrxtwin);
3143 
3144 	db_print_indent(indent);
3145 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
3146 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3147 
3148 	db_print_indent(indent);
3149 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
3150 	    tp->snd_fack, tp->rcv_numsacks);
3151 
3152 	/* Skip sackblks, sackhint. */
3153 
3154 	db_print_indent(indent);
3155 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
3156 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3157 }
3158 
DB_SHOW_COMMAND(tcpcb,db_show_tcpcb)3159 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3160 {
3161 	struct tcpcb *tp;
3162 
3163 	if (!have_addr) {
3164 		db_printf("usage: show tcpcb <addr>\n");
3165 		return;
3166 	}
3167 	tp = (struct tcpcb *)addr;
3168 
3169 	db_print_tcpcb(tp, "tcpcb", 0);
3170 }
3171 #endif
3172