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