2 * SPDX-License-Identifier: BSD-3-Clause
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.
10 * Portions of this software were developed by Robert N. M. Watson under
11 * contract to Juniper Networks, Inc.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
37 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
45 #include "opt_inet6.h"
46 #include "opt_ipsec.h"
47 #include "opt_kern_tls.h"
48 #include "opt_tcpdebug.h"
50 #include <sys/param.h>
51 #include <sys/systm.h>
53 #include <sys/limits.h>
54 #include <sys/malloc.h>
55 #include <sys/refcount.h>
56 #include <sys/kernel.h>
58 #include <sys/qmath.h>
59 #include <sys/sysctl.h>
62 #include <sys/domain.h>
64 #include <sys/socket.h>
65 #include <sys/socketvar.h>
66 #include <sys/protosw.h>
69 #include <sys/stats.h>
76 #include <net/if_var.h>
77 #include <net/route.h>
80 #include <netinet/in.h>
81 #include <netinet/in_kdtrace.h>
82 #include <netinet/in_pcb.h>
83 #include <netinet/in_systm.h>
84 #include <netinet/in_var.h>
85 #include <netinet/ip.h>
86 #include <netinet/ip_var.h>
88 #include <netinet/ip6.h>
89 #include <netinet6/in6_pcb.h>
90 #include <netinet6/ip6_var.h>
91 #include <netinet6/scope6_var.h>
93 #include <netinet/tcp.h>
94 #include <netinet/tcp_fsm.h>
95 #include <netinet/tcp_seq.h>
96 #include <netinet/tcp_timer.h>
97 #include <netinet/tcp_var.h>
98 #include <netinet/tcp_log_buf.h>
99 #include <netinet/tcpip.h>
100 #include <netinet/cc/cc.h>
101 #include <netinet/tcp_fastopen.h>
102 #include <netinet/tcp_hpts.h>
104 #include <netinet/tcp_pcap.h>
107 #include <netinet/tcp_debug.h>
110 #include <netinet/tcp_offload.h>
112 #include <netipsec/ipsec_support.h>
115 #include <vm/vm_param.h>
117 #include <vm/vm_extern.h>
118 #include <vm/vm_map.h>
119 #include <vm/vm_page.h>
122 * TCP protocol interface to socket abstraction.
125 static int tcp_connect(struct tcpcb *, struct sockaddr *,
129 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
132 static void tcp_disconnect(struct tcpcb *);
133 static void tcp_usrclosed(struct tcpcb *);
134 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
136 static int tcp_pru_options_support(struct tcpcb *tp, int flags);
139 #define TCPDEBUG0 int ostate = 0
140 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
141 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
142 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
146 #define TCPDEBUG2(req)
150 * tcp_require_unique port requires a globally-unique source port for each
151 * outgoing connection. The default is to require the 4-tuple to be unique.
153 VNET_DEFINE(int, tcp_require_unique_port) = 0;
154 SYSCTL_INT(_net_inet_tcp, OID_AUTO, require_unique_port,
155 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_require_unique_port), 0,
156 "Require globally-unique ephemeral port for outgoing connections");
157 #define V_tcp_require_unique_port VNET(tcp_require_unique_port)
160 * TCP attaches to socket via pru_attach(), reserving space,
161 * and an internet control block.
164 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
167 struct tcpcb *tp = NULL;
172 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
175 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
176 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
181 so->so_rcv.sb_flags |= SB_AUTOSIZE;
182 so->so_snd.sb_flags |= SB_AUTOSIZE;
183 error = in_pcballoc(so, &V_tcbinfo);
188 if (inp->inp_vflag & INP_IPV6PROTO) {
189 inp->inp_vflag |= INP_IPV6;
190 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
191 inp->inp_vflag |= INP_IPV4;
192 inp->in6p_hops = -1; /* use kernel default */
196 inp->inp_vflag |= INP_IPV4;
197 tp = tcp_newtcpcb(inp);
204 tp->t_state = TCPS_CLOSED;
206 TCPSTATES_INC(TCPS_CLOSED);
208 TCPDEBUG2(PRU_ATTACH);
209 TCP_PROBE2(debug__user, tp, PRU_ATTACH);
214 * tcp_usr_detach is called when the socket layer loses its final reference
215 * to the socket, be it a file descriptor reference, a reference from TCP,
216 * etc. At this point, there is only one case in which we will keep around
217 * inpcb state: time wait.
220 tcp_usr_detach(struct socket *so)
226 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
228 KASSERT(so->so_pcb == inp && inp->inp_socket == so,
229 ("%s: socket %p inp %p mismatch", __func__, so, inp));
233 if (inp->inp_flags & INP_TIMEWAIT) {
235 * There are two cases to handle: one in which the time wait
236 * state is being discarded (INP_DROPPED), and one in which
237 * this connection will remain in timewait. In the former,
238 * it is time to discard all state (except tcptw, which has
239 * already been discarded by the timewait close code, which
240 * should be further up the call stack somewhere). In the
241 * latter case, we detach from the socket, but leave the pcb
242 * present until timewait ends.
244 * XXXRW: Would it be cleaner to free the tcptw here?
246 * Astute question indeed, from twtcp perspective there are
247 * four cases to consider:
249 * #1 tcp_usr_detach is called at tcptw creation time by
250 * tcp_twstart, then do not discard the newly created tcptw
251 * and leave inpcb present until timewait ends
252 * #2 tcp_usr_detach is called at tcptw creation time by
253 * tcp_twstart, but connection is local and tw will be
254 * discarded immediately
255 * #3 tcp_usr_detach is called at timewait end (or reuse) by
256 * tcp_twclose, then the tcptw has already been discarded
257 * (or reused) and inpcb is freed here
258 * #4 tcp_usr_detach is called() after timewait ends (or reuse)
259 * (e.g. by soclose), then tcptw has already been discarded
260 * (or reused) and inpcb is freed here
262 * In all three cases the tcptw should not be freed here.
264 if (inp->inp_flags & INP_DROPPED) {
265 KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
266 "INP_DROPPED && tp != NULL"));
275 * If the connection is not in timewait, we consider two
276 * two conditions: one in which no further processing is
277 * necessary (dropped || embryonic), and one in which TCP is
278 * not yet done, but no longer requires the socket, so the
279 * pcb will persist for the time being.
281 * XXXRW: Does the second case still occur?
283 if (inp->inp_flags & INP_DROPPED ||
284 tp->t_state < TCPS_SYN_SENT) {
297 * Give the socket an address.
300 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
305 struct tcpcb *tp = NULL;
307 struct sockaddr_in *sinp;
309 sinp = (struct sockaddr_in *)nam;
310 if (nam->sa_family != AF_INET) {
312 * Preserve compatibility with old programs.
314 if (nam->sa_family != AF_UNSPEC ||
315 nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
316 sinp->sin_addr.s_addr != INADDR_ANY)
317 return (EAFNOSUPPORT);
318 nam->sa_family = AF_INET;
320 if (nam->sa_len != sizeof(*sinp))
324 * Must check for multicast addresses and disallow binding
327 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
328 return (EAFNOSUPPORT);
332 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
334 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
342 INP_HASH_WLOCK(&V_tcbinfo);
343 error = in_pcbbind(inp, nam, td->td_ucred);
344 INP_HASH_WUNLOCK(&V_tcbinfo);
347 TCP_PROBE2(debug__user, tp, PRU_BIND);
356 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
361 struct tcpcb *tp = NULL;
363 struct sockaddr_in6 *sin6;
366 sin6 = (struct sockaddr_in6 *)nam;
367 if (nam->sa_family != AF_INET6)
368 return (EAFNOSUPPORT);
369 if (nam->sa_len != sizeof(*sin6))
373 * Must check for multicast addresses and disallow binding
376 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
377 return (EAFNOSUPPORT);
381 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
383 vflagsav = inp->inp_vflag;
384 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
392 INP_HASH_WLOCK(&V_tcbinfo);
393 inp->inp_vflag &= ~INP_IPV4;
394 inp->inp_vflag |= INP_IPV6;
396 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
397 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
398 inp->inp_vflag |= INP_IPV4;
399 else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
400 struct sockaddr_in sin;
402 in6_sin6_2_sin(&sin, sin6);
403 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
404 error = EAFNOSUPPORT;
405 INP_HASH_WUNLOCK(&V_tcbinfo);
408 inp->inp_vflag |= INP_IPV4;
409 inp->inp_vflag &= ~INP_IPV6;
410 error = in_pcbbind(inp, (struct sockaddr *)&sin,
412 INP_HASH_WUNLOCK(&V_tcbinfo);
417 error = in6_pcbbind(inp, nam, td->td_ucred);
418 INP_HASH_WUNLOCK(&V_tcbinfo);
421 inp->inp_vflag = vflagsav;
423 TCP_PROBE2(debug__user, tp, PRU_BIND);
431 * Prepare to accept connections.
434 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
438 struct tcpcb *tp = NULL;
442 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
444 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
451 error = solisten_proto_check(so);
456 if (inp->inp_lport == 0) {
457 INP_HASH_WLOCK(&V_tcbinfo);
458 error = in_pcbbind(inp, NULL, td->td_ucred);
459 INP_HASH_WUNLOCK(&V_tcbinfo);
462 tcp_state_change(tp, TCPS_LISTEN);
463 solisten_proto(so, backlog);
465 if ((so->so_options & SO_NO_OFFLOAD) == 0)
466 tcp_offload_listen_start(tp);
469 solisten_proto_abort(so);
473 if (IS_FASTOPEN(tp->t_flags))
474 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
477 TCPDEBUG2(PRU_LISTEN);
478 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
486 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
490 struct tcpcb *tp = NULL;
495 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
497 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
501 vflagsav = inp->inp_vflag;
505 error = solisten_proto_check(so);
510 INP_HASH_WLOCK(&V_tcbinfo);
511 if (inp->inp_lport == 0) {
512 inp->inp_vflag &= ~INP_IPV4;
513 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
514 inp->inp_vflag |= INP_IPV4;
515 error = in6_pcbbind(inp, NULL, td->td_ucred);
517 INP_HASH_WUNLOCK(&V_tcbinfo);
519 tcp_state_change(tp, TCPS_LISTEN);
520 solisten_proto(so, backlog);
522 if ((so->so_options & SO_NO_OFFLOAD) == 0)
523 tcp_offload_listen_start(tp);
526 solisten_proto_abort(so);
530 if (IS_FASTOPEN(tp->t_flags))
531 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
534 inp->inp_vflag = vflagsav;
537 TCPDEBUG2(PRU_LISTEN);
538 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
546 * Initiate connection to peer.
547 * Create a template for use in transmissions on this connection.
548 * Enter SYN_SENT state, and mark socket as connecting.
549 * Start keep-alive timer, and seed output sequence space.
550 * Send initial segment on connection.
553 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
555 struct epoch_tracker et;
558 struct tcpcb *tp = NULL;
559 struct sockaddr_in *sinp;
561 sinp = (struct sockaddr_in *)nam;
562 if (nam->sa_family != AF_INET)
563 return (EAFNOSUPPORT);
564 if (nam->sa_len != sizeof (*sinp))
568 * Must disallow TCP ``connections'' to multicast addresses.
570 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
571 return (EAFNOSUPPORT);
572 if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST)
574 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
579 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
581 if (inp->inp_flags & INP_TIMEWAIT) {
585 if (inp->inp_flags & INP_DROPPED) {
586 error = ECONNREFUSED;
589 if (SOLISTENING(so)) {
596 if ((error = tcp_connect(tp, nam, td)) != 0)
599 if (registered_toedevs > 0 &&
600 (so->so_options & SO_NO_OFFLOAD) == 0 &&
601 (error = tcp_offload_connect(so, nam)) == 0)
604 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
605 error = tcp_output(tp);
606 KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
607 ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
611 TCPDEBUG2(PRU_CONNECT);
612 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
620 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
622 struct epoch_tracker et;
625 struct tcpcb *tp = NULL;
626 struct sockaddr_in6 *sin6;
632 sin6 = (struct sockaddr_in6 *)nam;
633 if (nam->sa_family != AF_INET6)
634 return (EAFNOSUPPORT);
635 if (nam->sa_len != sizeof (*sin6))
639 * Must disallow TCP ``connections'' to multicast addresses.
641 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
642 return (EAFNOSUPPORT);
645 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
647 vflagsav = inp->inp_vflag;
648 incflagsav = inp->inp_inc.inc_flags;
649 if (inp->inp_flags & INP_TIMEWAIT) {
653 if (inp->inp_flags & INP_DROPPED) {
654 error = ECONNREFUSED;
657 if (SOLISTENING(so)) {
665 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
666 * therefore probably require the hash lock, which isn't held here.
667 * Is this a significant problem?
669 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
670 struct sockaddr_in sin;
672 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
676 if ((inp->inp_vflag & INP_IPV4) == 0) {
677 error = EAFNOSUPPORT;
681 in6_sin6_2_sin(&sin, sin6);
682 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
683 error = EAFNOSUPPORT;
686 if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
690 if ((error = prison_remote_ip4(td->td_ucred,
691 &sin.sin_addr)) != 0)
693 inp->inp_vflag |= INP_IPV4;
694 inp->inp_vflag &= ~INP_IPV6;
696 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
699 if (registered_toedevs > 0 &&
700 (so->so_options & SO_NO_OFFLOAD) == 0 &&
701 (error = tcp_offload_connect(so, nam)) == 0)
704 error = tcp_output(tp);
707 if ((inp->inp_vflag & INP_IPV6) == 0) {
708 error = EAFNOSUPPORT;
713 if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
715 inp->inp_vflag &= ~INP_IPV4;
716 inp->inp_vflag |= INP_IPV6;
717 inp->inp_inc.inc_flags |= INC_ISIPV6;
718 if ((error = tcp6_connect(tp, nam, td)) != 0)
721 if (registered_toedevs > 0 &&
722 (so->so_options & SO_NO_OFFLOAD) == 0 &&
723 (error = tcp_offload_connect(so, nam)) == 0)
726 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
728 error = tcp_output(tp);
734 KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
735 ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
737 * If the implicit bind in the connect call fails, restore
738 * the flags we modified.
740 if (error != 0 && inp->inp_lport == 0) {
741 inp->inp_vflag = vflagsav;
742 inp->inp_inc.inc_flags = incflagsav;
745 TCPDEBUG2(PRU_CONNECT);
746 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
753 * Initiate disconnect from peer.
754 * If connection never passed embryonic stage, just drop;
755 * else if don't need to let data drain, then can just drop anyways,
756 * else have to begin TCP shutdown process: mark socket disconnecting,
757 * drain unread data, state switch to reflect user close, and
758 * send segment (e.g. FIN) to peer. Socket will be really disconnected
759 * when peer sends FIN and acks ours.
761 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
764 tcp_usr_disconnect(struct socket *so)
767 struct tcpcb *tp = NULL;
768 struct epoch_tracker et;
774 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
776 if (inp->inp_flags & INP_TIMEWAIT)
778 if (inp->inp_flags & INP_DROPPED) {
786 TCPDEBUG2(PRU_DISCONNECT);
787 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
795 * Accept a connection. Essentially all the work is done at higher levels;
796 * just return the address of the peer, storing through addr.
799 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
802 struct inpcb *inp = NULL;
804 struct tcpcb *tp = NULL;
810 if (so->so_state & SS_ISDISCONNECTED)
811 return (ECONNABORTED);
814 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
816 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
817 error = ECONNABORTED;
826 * We inline in_getpeeraddr and COMMON_END here, so that we can
827 * copy the data of interest and defer the malloc until after we
830 port = inp->inp_fport;
831 addr = inp->inp_faddr;
834 TCPDEBUG2(PRU_ACCEPT);
835 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
838 *nam = in_sockaddr(port, &addr);
845 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
847 struct inpcb *inp = NULL;
850 struct tcpcb *tp = NULL;
853 struct in6_addr addr6;
854 struct epoch_tracker et;
859 if (so->so_state & SS_ISDISCONNECTED)
860 return (ECONNABORTED);
863 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
866 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
867 error = ECONNABORTED;
876 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
877 * copy the data of interest and defer the malloc until after we
880 if (inp->inp_vflag & INP_IPV4) {
882 port = inp->inp_fport;
883 addr = inp->inp_faddr;
885 port = inp->inp_fport;
886 addr6 = inp->in6p_faddr;
890 TCPDEBUG2(PRU_ACCEPT);
891 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
896 *nam = in6_v4mapsin6_sockaddr(port, &addr);
898 *nam = in6_sockaddr(port, &addr6);
905 * Mark the connection as being incapable of further output.
908 tcp_usr_shutdown(struct socket *so)
912 struct tcpcb *tp = NULL;
913 struct epoch_tracker et;
917 KASSERT(inp != NULL, ("inp == NULL"));
919 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
928 if (!(inp->inp_flags & INP_DROPPED))
929 error = tcp_output_nodrop(tp);
930 TCPDEBUG2(PRU_SHUTDOWN);
931 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
932 error = tcp_unlock_or_drop(tp, error);
939 * After a receive, possibly send window update to peer.
942 tcp_usr_rcvd(struct socket *so, int flags)
944 struct epoch_tracker et;
946 struct tcpcb *tp = NULL;
947 int outrv = 0, error = 0;
951 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
953 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
961 * For passively-created TFO connections, don't attempt a window
962 * update while still in SYN_RECEIVED as this may trigger an early
963 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
964 * application response data, or failing that, when the DELACK timer
967 if (IS_FASTOPEN(tp->t_flags) &&
968 (tp->t_state == TCPS_SYN_RECEIVED))
971 if (tp->t_flags & TF_TOE)
972 tcp_offload_rcvd(tp);
975 outrv = tcp_output_nodrop(tp);
978 TCP_PROBE2(debug__user, tp, PRU_RCVD);
979 (void) tcp_unlock_or_drop(tp, outrv);
985 * Do a send by putting data in output queue and updating urgent
986 * marker if URG set. Possibly send more data. Unlike the other
987 * pru_*() routines, the mbuf chains are our responsibility. We
988 * must either enqueue them or free them. The other pru_* routines
989 * generally are caller-frees.
992 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
993 struct sockaddr *nam, struct mbuf *control, struct thread *td)
995 struct epoch_tracker et;
998 struct tcpcb *tp = NULL;
1001 struct sockaddr_in sin;
1003 struct sockaddr_in *sinp;
1008 u_int8_t incflagsav;
1013 if (control != NULL) {
1014 /* TCP doesn't do control messages (rights, creds, etc) */
1015 if (control->m_len) {
1019 m_freem(control); /* empty control, just free it */
1022 inp = sotoinpcb(so);
1023 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
1025 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1026 if (m != NULL && (flags & PRUS_NOTREADY) == 0)
1029 return (ECONNRESET);
1032 vflagsav = inp->inp_vflag;
1033 incflagsav = inp->inp_inc.inc_flags;
1034 restoreflags = false;
1035 tp = intotcpcb(inp);
1037 NET_EPOCH_ENTER(et);
1038 if ((flags & PRUS_OOB) != 0 &&
1039 (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
1043 if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
1044 if (tp->t_state == TCPS_LISTEN) {
1048 switch (nam->sa_family) {
1051 sinp = (struct sockaddr_in *)nam;
1052 if (sinp->sin_len != sizeof(struct sockaddr_in)) {
1056 if ((inp->inp_vflag & INP_IPV6) != 0) {
1057 error = EAFNOSUPPORT;
1060 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
1061 error = EAFNOSUPPORT;
1064 if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
1068 if ((error = prison_remote_ip4(td->td_ucred,
1079 struct sockaddr_in6 *sin6;
1081 sin6 = (struct sockaddr_in6 *)nam;
1082 if (sin6->sin6_len != sizeof(*sin6)) {
1086 if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
1087 error = EAFNOSUPPORT;
1090 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1091 error = EAFNOSUPPORT;
1094 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1096 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1100 if ((inp->inp_vflag & INP_IPV4) == 0) {
1101 error = EAFNOSUPPORT;
1104 restoreflags = true;
1105 inp->inp_vflag &= ~INP_IPV6;
1107 in6_sin6_2_sin(sinp, sin6);
1109 ntohl(sinp->sin_addr.s_addr))) {
1110 error = EAFNOSUPPORT;
1113 if ((error = prison_remote_ip4(td->td_ucred,
1118 error = EAFNOSUPPORT;
1122 if ((inp->inp_vflag & INP_IPV6) == 0) {
1123 error = EAFNOSUPPORT;
1126 restoreflags = true;
1127 inp->inp_vflag &= ~INP_IPV4;
1128 inp->inp_inc.inc_flags |= INC_ISIPV6;
1129 if ((error = prison_remote_ip6(td->td_ucred,
1138 error = EAFNOSUPPORT;
1142 if (!(flags & PRUS_OOB)) {
1143 sbappendstream(&so->so_snd, m, flags);
1145 if (nam && tp->t_state < TCPS_SYN_SENT) {
1146 KASSERT(tp->t_state == TCPS_CLOSED,
1147 ("%s: tp %p is listening", __func__, tp));
1150 * Do implied connect if not yet connected,
1151 * initialize window to default value, and
1152 * initialize maxseg using peer's cached MSS.
1156 error = tcp6_connect(tp, nam, td);
1158 #if defined(INET6) && defined(INET)
1162 error = tcp_connect(tp,
1163 (struct sockaddr *)sinp, td);
1166 * The bind operation in tcp_connect succeeded. We
1167 * no longer want to restore the flags if later
1170 if (error == 0 || inp->inp_lport != 0)
1171 restoreflags = false;
1174 /* m is freed if PRUS_NOTREADY is unset. */
1175 sbflush(&so->so_snd);
1178 if (IS_FASTOPEN(tp->t_flags))
1179 tcp_fastopen_connect(tp);
1181 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1185 if (flags & PRUS_EOF) {
1187 * Close the send side of the connection after
1193 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1194 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1195 (tp->t_fbyte_out == 0) &&
1196 (so->so_snd.sb_ccc > 0)) {
1197 tp->t_fbyte_out = ticks;
1198 if (tp->t_fbyte_out == 0)
1199 tp->t_fbyte_out = 1;
1200 if (tp->t_fbyte_out && tp->t_fbyte_in)
1201 tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1203 if (!(inp->inp_flags & INP_DROPPED) &&
1204 !(flags & PRUS_NOTREADY)) {
1205 if (flags & PRUS_MORETOCOME)
1206 tp->t_flags |= TF_MORETOCOME;
1207 error = tcp_output_nodrop(tp);
1208 if (flags & PRUS_MORETOCOME)
1209 tp->t_flags &= ~TF_MORETOCOME;
1213 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1215 SOCKBUF_LOCK(&so->so_snd);
1216 if (sbspace(&so->so_snd) < -512) {
1217 SOCKBUF_UNLOCK(&so->so_snd);
1222 * According to RFC961 (Assigned Protocols),
1223 * the urgent pointer points to the last octet
1224 * of urgent data. We continue, however,
1225 * to consider it to indicate the first octet
1226 * of data past the urgent section.
1227 * Otherwise, snd_up should be one lower.
1229 sbappendstream_locked(&so->so_snd, m, flags);
1230 SOCKBUF_UNLOCK(&so->so_snd);
1232 if (nam && tp->t_state < TCPS_SYN_SENT) {
1234 * Do implied connect if not yet connected,
1235 * initialize window to default value, and
1236 * initialize maxseg using peer's cached MSS.
1240 * Not going to contemplate SYN|URG
1242 if (IS_FASTOPEN(tp->t_flags))
1243 tp->t_flags &= ~TF_FASTOPEN;
1246 error = tcp6_connect(tp, nam, td);
1248 #if defined(INET6) && defined(INET)
1252 error = tcp_connect(tp,
1253 (struct sockaddr *)sinp, td);
1256 * The bind operation in tcp_connect succeeded. We
1257 * no longer want to restore the flags if later
1260 if (error == 0 || inp->inp_lport != 0)
1261 restoreflags = false;
1264 /* m is freed if PRUS_NOTREADY is unset. */
1265 sbflush(&so->so_snd);
1268 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1271 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1272 if ((flags & PRUS_NOTREADY) == 0) {
1273 tp->t_flags |= TF_FORCEDATA;
1274 error = tcp_output_nodrop(tp);
1275 tp->t_flags &= ~TF_FORCEDATA;
1278 TCP_LOG_EVENT(tp, NULL,
1279 &inp->inp_socket->so_rcv,
1280 &inp->inp_socket->so_snd,
1281 TCP_LOG_USERSEND, error,
1286 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
1287 * responsible for freeing memory.
1289 if (m != NULL && (flags & PRUS_NOTREADY) == 0)
1293 * If the request was unsuccessful and we changed flags,
1294 * restore the original flags.
1296 if (error != 0 && restoreflags) {
1297 inp->inp_vflag = vflagsav;
1298 inp->inp_inc.inc_flags = incflagsav;
1300 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
1301 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1302 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1303 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1304 error = tcp_unlock_or_drop(tp, error);
1310 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1312 struct epoch_tracker et;
1317 inp = sotoinpcb(so);
1319 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1321 mb_free_notready(m, count);
1322 return (ECONNRESET);
1324 tp = intotcpcb(inp);
1326 SOCKBUF_LOCK(&so->so_snd);
1327 error = sbready(&so->so_snd, m, count);
1328 SOCKBUF_UNLOCK(&so->so_snd);
1333 NET_EPOCH_ENTER(et);
1334 error = tcp_output_unlock(tp);
1341 * Abort the TCP. Drop the connection abruptly.
1344 tcp_usr_abort(struct socket *so)
1347 struct tcpcb *tp = NULL;
1348 struct epoch_tracker et;
1351 inp = sotoinpcb(so);
1352 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1354 NET_EPOCH_ENTER(et);
1356 KASSERT(inp->inp_socket != NULL,
1357 ("tcp_usr_abort: inp_socket == NULL"));
1360 * If we still have full TCP state, and we're not dropped, drop.
1362 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1363 !(inp->inp_flags & INP_DROPPED)) {
1364 tp = intotcpcb(inp);
1366 tp = tcp_drop(tp, ECONNABORTED);
1369 TCPDEBUG2(PRU_ABORT);
1370 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1372 if (!(inp->inp_flags & INP_DROPPED)) {
1374 so->so_state |= SS_PROTOREF;
1376 inp->inp_flags |= INP_SOCKREF;
1384 * TCP socket is closed. Start friendly disconnect.
1387 tcp_usr_close(struct socket *so)
1390 struct tcpcb *tp = NULL;
1391 struct epoch_tracker et;
1394 inp = sotoinpcb(so);
1395 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1397 NET_EPOCH_ENTER(et);
1399 KASSERT(inp->inp_socket != NULL,
1400 ("tcp_usr_close: inp_socket == NULL"));
1403 * If we still have full TCP state, and we're not dropped, initiate
1406 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1407 !(inp->inp_flags & INP_DROPPED)) {
1408 tp = intotcpcb(inp);
1411 TCPDEBUG2(PRU_CLOSE);
1412 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1414 if (!(inp->inp_flags & INP_DROPPED)) {
1416 so->so_state |= SS_PROTOREF;
1418 inp->inp_flags |= INP_SOCKREF;
1425 tcp_pru_options_support(struct tcpcb *tp, int flags)
1428 * If the specific TCP stack has a pru_options
1429 * specified then it does not always support
1430 * all the PRU_XX options and we must ask it.
1431 * If the function is not specified then all
1432 * of the PRU_XX options are supported.
1436 if (tp->t_fb->tfb_pru_options) {
1437 ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1443 * Receive out-of-band data.
1446 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1450 struct tcpcb *tp = NULL;
1453 inp = sotoinpcb(so);
1454 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1456 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1460 tp = intotcpcb(inp);
1461 error = tcp_pru_options_support(tp, PRUS_OOB);
1466 if ((so->so_oobmark == 0 &&
1467 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1468 so->so_options & SO_OOBINLINE ||
1469 tp->t_oobflags & TCPOOB_HADDATA) {
1473 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1474 error = EWOULDBLOCK;
1478 *mtod(m, caddr_t) = tp->t_iobc;
1479 if ((flags & MSG_PEEK) == 0)
1480 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1483 TCPDEBUG2(PRU_RCVOOB);
1484 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1490 struct pr_usrreqs tcp_usrreqs = {
1491 .pru_abort = tcp_usr_abort,
1492 .pru_accept = tcp_usr_accept,
1493 .pru_attach = tcp_usr_attach,
1494 .pru_bind = tcp_usr_bind,
1495 .pru_connect = tcp_usr_connect,
1496 .pru_control = in_control,
1497 .pru_detach = tcp_usr_detach,
1498 .pru_disconnect = tcp_usr_disconnect,
1499 .pru_listen = tcp_usr_listen,
1500 .pru_peeraddr = in_getpeeraddr,
1501 .pru_rcvd = tcp_usr_rcvd,
1502 .pru_rcvoob = tcp_usr_rcvoob,
1503 .pru_send = tcp_usr_send,
1504 .pru_ready = tcp_usr_ready,
1505 .pru_shutdown = tcp_usr_shutdown,
1506 .pru_sockaddr = in_getsockaddr,
1507 .pru_sosetlabel = in_pcbsosetlabel,
1508 .pru_close = tcp_usr_close,
1513 struct pr_usrreqs tcp6_usrreqs = {
1514 .pru_abort = tcp_usr_abort,
1515 .pru_accept = tcp6_usr_accept,
1516 .pru_attach = tcp_usr_attach,
1517 .pru_bind = tcp6_usr_bind,
1518 .pru_connect = tcp6_usr_connect,
1519 .pru_control = in6_control,
1520 .pru_detach = tcp_usr_detach,
1521 .pru_disconnect = tcp_usr_disconnect,
1522 .pru_listen = tcp6_usr_listen,
1523 .pru_peeraddr = in6_mapped_peeraddr,
1524 .pru_rcvd = tcp_usr_rcvd,
1525 .pru_rcvoob = tcp_usr_rcvoob,
1526 .pru_send = tcp_usr_send,
1527 .pru_ready = tcp_usr_ready,
1528 .pru_shutdown = tcp_usr_shutdown,
1529 .pru_sockaddr = in6_mapped_sockaddr,
1530 .pru_sosetlabel = in_pcbsosetlabel,
1531 .pru_close = tcp_usr_close,
1537 * Common subroutine to open a TCP connection to remote host specified
1538 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1539 * port number if needed. Call in_pcbconnect_setup to do the routing and
1540 * to choose a local host address (interface). If there is an existing
1541 * incarnation of the same connection in TIME-WAIT state and if the remote
1542 * host was sending CC options and if the connection duration was < MSL, then
1543 * truncate the previous TIME-WAIT state and proceed.
1544 * Initialize connection parameters and enter SYN-SENT state.
1547 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1549 struct inpcb *inp = tp->t_inpcb, *oinp;
1550 struct socket *so = inp->inp_socket;
1551 struct in_addr laddr;
1556 INP_WLOCK_ASSERT(inp);
1557 INP_HASH_WLOCK(&V_tcbinfo);
1559 if (V_tcp_require_unique_port && inp->inp_lport == 0) {
1560 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1566 * Cannot simply call in_pcbconnect, because there might be an
1567 * earlier incarnation of this same connection still in
1568 * TIME_WAIT state, creating an ADDRINUSE error.
1570 laddr = inp->inp_laddr;
1571 lport = inp->inp_lport;
1572 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1573 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1574 if (error && oinp == NULL)
1580 /* Handle initial bind if it hadn't been done in advance. */
1581 if (inp->inp_lport == 0) {
1582 inp->inp_lport = lport;
1583 if (in_pcbinshash(inp) != 0) {
1589 inp->inp_laddr = laddr;
1591 INP_HASH_WUNLOCK(&V_tcbinfo);
1594 * Compute window scaling to request:
1595 * Scale to fit into sweet spot. See tcp_syncache.c.
1596 * XXX: This should move to tcp_output().
1598 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1599 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1600 tp->request_r_scale++;
1603 TCPSTAT_INC(tcps_connattempt);
1604 tcp_state_change(tp, TCPS_SYN_SENT);
1605 tp->iss = tcp_new_isn(&inp->inp_inc);
1606 if (tp->t_flags & TF_REQ_TSTMP)
1607 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1608 tcp_sendseqinit(tp);
1613 INP_HASH_WUNLOCK(&V_tcbinfo);
1620 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1622 struct inpcb *inp = tp->t_inpcb;
1625 INP_WLOCK_ASSERT(inp);
1626 INP_HASH_WLOCK(&V_tcbinfo);
1628 if (V_tcp_require_unique_port && inp->inp_lport == 0) {
1629 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1633 error = in6_pcbconnect(inp, nam, td->td_ucred);
1636 INP_HASH_WUNLOCK(&V_tcbinfo);
1638 /* Compute window scaling to request. */
1639 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1640 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1641 tp->request_r_scale++;
1643 soisconnecting(inp->inp_socket);
1644 TCPSTAT_INC(tcps_connattempt);
1645 tcp_state_change(tp, TCPS_SYN_SENT);
1646 tp->iss = tcp_new_isn(&inp->inp_inc);
1647 if (tp->t_flags & TF_REQ_TSTMP)
1648 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1649 tcp_sendseqinit(tp);
1654 INP_HASH_WUNLOCK(&V_tcbinfo);
1660 * Export TCP internal state information via a struct tcp_info, based on the
1661 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1662 * (TCP state machine, etc). We export all information using FreeBSD-native
1663 * constants -- for example, the numeric values for tcpi_state will differ
1667 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1670 INP_WLOCK_ASSERT(tp->t_inpcb);
1671 bzero(ti, sizeof(*ti));
1673 ti->tcpi_state = tp->t_state;
1674 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1675 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1676 if (tp->t_flags & TF_SACK_PERMIT)
1677 ti->tcpi_options |= TCPI_OPT_SACK;
1678 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1679 ti->tcpi_options |= TCPI_OPT_WSCALE;
1680 ti->tcpi_snd_wscale = tp->snd_scale;
1681 ti->tcpi_rcv_wscale = tp->rcv_scale;
1683 if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
1684 ti->tcpi_options |= TCPI_OPT_ECN;
1686 ti->tcpi_rto = tp->t_rxtcur * tick;
1687 ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1688 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1689 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1691 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1692 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1695 * FreeBSD-specific extension fields for tcp_info.
1697 ti->tcpi_rcv_space = tp->rcv_wnd;
1698 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1699 ti->tcpi_snd_wnd = tp->snd_wnd;
1700 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1701 ti->tcpi_snd_nxt = tp->snd_nxt;
1702 ti->tcpi_snd_mss = tp->t_maxseg;
1703 ti->tcpi_rcv_mss = tp->t_maxseg;
1704 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1705 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1706 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1708 if (tp->t_flags & TF_TOE) {
1709 ti->tcpi_options |= TCPI_OPT_TOE;
1710 tcp_offload_tcp_info(tp, ti);
1716 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1717 * socket option arguments. When it re-acquires the lock after the copy, it
1718 * has to revalidate that the connection is still valid for the socket
1721 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \
1723 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1726 return (ECONNRESET); \
1728 tp = intotcpcb(inp); \
1730 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1733 tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt)
1735 struct socket *so = inp->inp_socket;
1736 struct tcpcb *tp = intotcpcb(inp);
1739 MPASS(sopt->sopt_dir == SOPT_SET);
1740 INP_WLOCK_ASSERT(inp);
1741 KASSERT((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) == 0,
1742 ("inp_flags == %x", inp->inp_flags));
1743 KASSERT(so != NULL, ("inp_socket == NULL"));
1745 if (sopt->sopt_level != IPPROTO_TCP) {
1748 if (inp->inp_vflag & INP_IPV6PROTO)
1749 error = ip6_ctloutput(so, sopt);
1751 #if defined(INET6) && defined(INET)
1755 error = ip_ctloutput(so, sopt);
1758 * When an IP-level socket option affects TCP, pass control
1759 * down to stack tfb_tcp_ctloutput, otherwise return what
1760 * IP level returned.
1762 switch (sopt->sopt_level) {
1765 if ((inp->inp_vflag & INP_IPV6PROTO) == 0)
1767 switch (sopt->sopt_name) {
1769 /* Notify tcp stacks that care (e.g. RACK). */
1771 case IPV6_USE_MIN_MTU:
1772 /* Update t_maxseg accordingly. */
1781 switch (sopt->sopt_name) {
1783 inp->inp_ip_tos &= ~IPTOS_ECN_MASK;
1786 /* Notify tcp stacks that care (e.g. RACK). */
1797 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1799 return (ECONNRESET);
1801 } else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1803 * Protect the TCP option TCP_FUNCTION_BLK so
1804 * that a sub-function can *never* overwrite this.
1806 struct tcp_function_set fsn;
1807 struct tcp_function_block *blk;
1810 error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1815 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1817 return (ECONNRESET);
1819 tp = intotcpcb(inp);
1821 blk = find_and_ref_tcp_functions(&fsn);
1826 if (tp->t_fb == blk) {
1827 /* You already have this */
1828 refcount_release(&blk->tfb_refcnt);
1832 if (tp->t_state != TCPS_CLOSED) {
1834 * The user has advanced the state
1835 * past the initial point, we may not
1836 * be able to switch.
1838 if (blk->tfb_tcp_handoff_ok != NULL) {
1840 * Does the stack provide a
1841 * query mechanism, if so it may
1842 * still be possible?
1844 error = (*blk->tfb_tcp_handoff_ok)(tp);
1848 refcount_release(&blk->tfb_refcnt);
1853 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1854 refcount_release(&blk->tfb_refcnt);
1859 * Release the old refcnt, the
1860 * lookup acquired a ref on the
1863 if (tp->t_fb->tfb_tcp_fb_fini) {
1864 struct epoch_tracker et;
1866 * Tell the stack to cleanup with 0 i.e.
1867 * the tcb is not going away.
1869 NET_EPOCH_ENTER(et);
1870 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1874 /* Assure that we are not on any hpts */
1875 tcp_hpts_remove(tp->t_inpcb);
1877 if (blk->tfb_tcp_fb_init) {
1878 error = (*blk->tfb_tcp_fb_init)(tp);
1880 refcount_release(&blk->tfb_refcnt);
1881 if (tp->t_fb->tfb_tcp_fb_init) {
1882 if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) {
1883 /* Fall back failed, drop the connection */
1892 refcount_release(&tp->t_fb->tfb_refcnt);
1895 if (tp->t_flags & TF_TOE) {
1896 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1905 /* Pass in the INP locked, callee must unlock it. */
1906 return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1910 tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1912 struct socket *so = inp->inp_socket;
1913 struct tcpcb *tp = intotcpcb(inp);
1916 MPASS(sopt->sopt_dir == SOPT_GET);
1917 INP_WLOCK_ASSERT(inp);
1918 KASSERT((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) == 0,
1919 ("inp_flags == %x", inp->inp_flags));
1920 KASSERT(so != NULL, ("inp_socket == NULL"));
1922 if (sopt->sopt_level != IPPROTO_TCP) {
1925 if (inp->inp_vflag & INP_IPV6PROTO)
1926 error = ip6_ctloutput(so, sopt);
1928 #if defined(INET6) && defined(INET)
1932 error = ip_ctloutput(so, sopt);
1936 if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1937 (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1938 struct tcp_function_set fsn;
1940 if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1941 memset(&fsn, 0, sizeof(fsn));
1942 find_tcp_function_alias(tp->t_fb, &fsn);
1944 strncpy(fsn.function_set_name,
1945 tp->t_fb->tfb_tcp_block_name,
1946 TCP_FUNCTION_NAME_LEN_MAX);
1947 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1949 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1951 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1955 /* Pass in the INP locked, callee must unlock it. */
1956 return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1960 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1964 inp = sotoinpcb(so);
1965 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1968 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1970 return (ECONNRESET);
1972 if (sopt->sopt_dir == SOPT_SET)
1973 return (tcp_ctloutput_set(inp, sopt));
1974 else if (sopt->sopt_dir == SOPT_GET)
1975 return (tcp_ctloutput_get(inp, sopt));
1977 panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
1981 * If this assert becomes untrue, we need to change the size of the buf
1982 * variable in tcp_default_ctloutput().
1985 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1986 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1991 copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1993 struct tls_enable_v0 tls_v0;
1996 if (sopt->sopt_valsize == sizeof(tls_v0)) {
1997 error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
2001 memset(tls, 0, sizeof(*tls));
2002 tls->cipher_key = tls_v0.cipher_key;
2003 tls->iv = tls_v0.iv;
2004 tls->auth_key = tls_v0.auth_key;
2005 tls->cipher_algorithm = tls_v0.cipher_algorithm;
2006 tls->cipher_key_len = tls_v0.cipher_key_len;
2007 tls->iv_len = tls_v0.iv_len;
2008 tls->auth_algorithm = tls_v0.auth_algorithm;
2009 tls->auth_key_len = tls_v0.auth_key_len;
2010 tls->flags = tls_v0.flags;
2011 tls->tls_vmajor = tls_v0.tls_vmajor;
2012 tls->tls_vminor = tls_v0.tls_vminor;
2016 return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
2020 extern struct cc_algo newreno_cc_algo;
2023 tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
2025 struct cc_algo *algo;
2028 struct cc_var cc_mem;
2029 char buf[TCP_CA_NAME_MAX];
2034 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
2037 buf[sopt->sopt_valsize] = '\0';
2039 STAILQ_FOREACH(algo, &cc_list, entries) {
2040 if (strncmp(buf, algo->name,
2041 TCP_CA_NAME_MAX) == 0) {
2042 if (algo->flags & CC_MODULE_BEING_REMOVED) {
2043 /* We can't "see" modules being unloaded */
2054 * With a reference the algorithm cannot be removed
2055 * so we hold a reference through the change process.
2059 if (algo->cb_init != NULL) {
2060 /* We can now pre-get the memory for the CC */
2061 mem_sz = (*algo->cc_data_sz)();
2065 ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
2072 * Make sure its all clean and zero and also get
2075 memset(&cc_mem, 0, sizeof(cc_mem));
2077 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
2080 free(ptr, M_CC_MEM);
2081 /* Release our temp reference */
2085 return (ECONNRESET);
2087 tp = intotcpcb(inp);
2089 memset(ptr, 0, mem_sz);
2090 cc_mem.ccvc.tcp = tp;
2092 * We once again hold a write lock over the tcb so it's
2093 * safe to do these things without ordering concerns.
2094 * Note here we init into stack memory.
2096 if (algo->cb_init != NULL)
2097 error = algo->cb_init(&cc_mem, ptr);
2101 * The CC algorithms, when given their memory
2102 * should not fail we could in theory have a
2107 * Touchdown, lets go ahead and move the
2108 * connection to the new CC module by
2109 * copying in the cc_mem after we call
2110 * the old ones cleanup (if any).
2112 if (CC_ALGO(tp)->cb_destroy != NULL)
2113 CC_ALGO(tp)->cb_destroy(tp->ccv);
2114 /* Detach the old CC from the tcpcb */
2116 /* Copy in our temp memory that was inited */
2117 memcpy(tp->ccv, &cc_mem, sizeof(struct cc_var));
2118 /* Now attach the new, which takes a reference */
2119 cc_attach(tp, algo);
2120 /* Ok now are we where we have gotten past any conn_init? */
2121 if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
2122 /* Yep run the connection init for the new CC */
2123 CC_ALGO(tp)->conn_init(tp->ccv);
2126 free(ptr, M_CC_MEM);
2128 /* Now lets release our temp reference */
2136 tcp_default_ctloutput(struct inpcb *inp, struct sockopt *sopt)
2138 struct tcpcb *tp = intotcpcb(inp);
2139 int error, opt, optval;
2143 struct tls_enable tls;
2144 struct socket *so = inp->inp_socket;
2146 char *pbuf, buf[TCP_LOG_ID_LEN];
2148 struct statsblob *sbp;
2152 INP_WLOCK_ASSERT(inp);
2153 KASSERT((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) == 0,
2154 ("inp_flags == %x", inp->inp_flags));
2155 KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
2157 switch (sopt->sopt_level) {
2160 MPASS(inp->inp_vflag & INP_IPV6PROTO);
2161 switch (sopt->sopt_name) {
2162 case IPV6_USE_MIN_MTU:
2163 tcp6_use_min_mtu(tp);
2177 * For TCP_CCALGOOPT forward the control to CC module, for both
2178 * SOPT_SET and SOPT_GET.
2180 switch (sopt->sopt_name) {
2183 if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2185 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2186 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2187 sopt->sopt_valsize);
2192 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2193 if (CC_ALGO(tp)->ctl_output != NULL)
2194 error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
2198 if (error == 0 && sopt->sopt_dir == SOPT_GET)
2199 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2204 switch (sopt->sopt_dir) {
2206 switch (sopt->sopt_name) {
2207 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2209 if (!TCPMD5_ENABLED()) {
2211 return (ENOPROTOOPT);
2213 error = TCPMD5_PCBCTL(inp, sopt);
2216 goto unlock_and_done;
2223 error = sooptcopyin(sopt, &optval, sizeof optval,
2228 INP_WLOCK_RECHECK(inp);
2229 switch (sopt->sopt_name) {
2240 opt = 0; /* dead code to fool gcc */
2247 tp->t_flags &= ~opt;
2250 if (tp->t_flags & TF_TOE) {
2251 tcp_offload_ctloutput(tp, sopt->sopt_dir,
2260 error = sooptcopyin(sopt, &optval, sizeof optval,
2265 INP_WLOCK_RECHECK(inp);
2267 tp->t_flags |= TF_NOPUSH;
2268 else if (tp->t_flags & TF_NOPUSH) {
2269 tp->t_flags &= ~TF_NOPUSH;
2270 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2271 struct epoch_tracker et;
2273 NET_EPOCH_ENTER(et);
2274 error = tcp_output_nodrop(tp);
2278 goto unlock_and_done;
2280 case TCP_REMOTE_UDP_ENCAPS_PORT:
2282 error = sooptcopyin(sopt, &optval, sizeof optval,
2286 if ((optval < TCP_TUNNELING_PORT_MIN) ||
2287 (optval > TCP_TUNNELING_PORT_MAX)) {
2288 /* Its got to be in range */
2291 if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
2292 /* You have to have enabled a UDP tunneling port first */
2295 INP_WLOCK_RECHECK(inp);
2296 if (tp->t_state != TCPS_CLOSED) {
2297 /* You can't change after you are connected */
2300 /* Ok we are all good set the port */
2301 tp->t_port = htons(optval);
2303 goto unlock_and_done;
2307 error = sooptcopyin(sopt, &optval, sizeof optval,
2312 INP_WLOCK_RECHECK(inp);
2313 if (optval > 0 && optval <= tp->t_maxseg &&
2314 optval + 40 >= V_tcp_minmss)
2315 tp->t_maxseg = optval;
2318 goto unlock_and_done;
2328 error = sooptcopyin(sopt, &optval, sizeof optval,
2334 sbp = stats_blob_alloc(
2335 V_tcp_perconn_stats_dflt_tpl, 0);
2339 INP_WLOCK_RECHECK(inp);
2340 if ((tp->t_stats != NULL && sbp == NULL) ||
2341 (tp->t_stats == NULL && sbp != NULL)) {
2342 struct statsblob *t = tp->t_stats;
2348 stats_blob_destroy(sbp);
2350 return (EOPNOTSUPP);
2354 case TCP_CONGESTION:
2355 error = tcp_set_cc_mod(inp, sopt);
2358 case TCP_REUSPORT_LB_NUMA:
2360 error = sooptcopyin(sopt, &optval, sizeof(optval),
2362 INP_WLOCK_RECHECK(inp);
2364 error = in_pcblbgroup_numa(inp, optval);
2369 case TCP_TXTLS_ENABLE:
2371 error = copyin_tls_enable(sopt, &tls);
2374 error = ktls_enable_tx(so, &tls);
2376 case TCP_TXTLS_MODE:
2378 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2382 INP_WLOCK_RECHECK(inp);
2383 error = ktls_set_tx_mode(so, ui);
2386 case TCP_RXTLS_ENABLE:
2388 error = sooptcopyin(sopt, &tls, sizeof(tls),
2392 error = ktls_enable_rx(so, &tls);
2400 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2404 if (ui > (UINT_MAX / hz)) {
2410 INP_WLOCK_RECHECK(inp);
2411 switch (sopt->sopt_name) {
2413 tp->t_keepidle = ui;
2415 * XXX: better check current remaining
2416 * timeout and "merge" it with new value.
2418 if ((tp->t_state > TCPS_LISTEN) &&
2419 (tp->t_state <= TCPS_CLOSING))
2420 tcp_timer_activate(tp, TT_KEEP,
2424 tp->t_keepintvl = ui;
2425 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2426 (TP_MAXIDLE(tp) > 0))
2427 tcp_timer_activate(tp, TT_2MSL,
2431 tp->t_keepinit = ui;
2432 if (tp->t_state == TCPS_SYN_RECEIVED ||
2433 tp->t_state == TCPS_SYN_SENT)
2434 tcp_timer_activate(tp, TT_KEEP,
2438 goto unlock_and_done;
2442 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2446 INP_WLOCK_RECHECK(inp);
2448 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2449 (TP_MAXIDLE(tp) > 0))
2450 tcp_timer_activate(tp, TT_2MSL,
2452 goto unlock_and_done;
2458 error = sooptcopyin(sopt, &optval, sizeof optval,
2463 INP_WLOCK_RECHECK(inp);
2465 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
2466 &(tp->t_outpkts) : &(tp->t_inpkts),
2470 goto unlock_and_done;
2473 case TCP_FASTOPEN: {
2474 struct tcp_fastopen tfo_optval;
2477 if (!V_tcp_fastopen_client_enable &&
2478 !V_tcp_fastopen_server_enable)
2481 error = sooptcopyin(sopt, &tfo_optval,
2482 sizeof(tfo_optval), sizeof(int));
2486 INP_WLOCK_RECHECK(inp);
2487 if ((tp->t_state != TCPS_CLOSED) &&
2488 (tp->t_state != TCPS_LISTEN)) {
2490 goto unlock_and_done;
2492 if (tfo_optval.enable) {
2493 if (tp->t_state == TCPS_LISTEN) {
2494 if (!V_tcp_fastopen_server_enable) {
2496 goto unlock_and_done;
2499 if (tp->t_tfo_pending == NULL)
2501 tcp_fastopen_alloc_counter();
2504 * If a pre-shared key was provided,
2505 * stash it in the client cookie
2506 * field of the tcpcb for use during
2509 if (sopt->sopt_valsize ==
2510 sizeof(tfo_optval)) {
2511 memcpy(tp->t_tfo_cookie.client,
2513 TCP_FASTOPEN_PSK_LEN);
2514 tp->t_tfo_client_cookie_len =
2515 TCP_FASTOPEN_PSK_LEN;
2518 tp->t_flags |= TF_FASTOPEN;
2520 tp->t_flags &= ~TF_FASTOPEN;
2521 goto unlock_and_done;
2527 error = sooptcopyin(sopt, &optval, sizeof optval,
2532 INP_WLOCK_RECHECK(inp);
2533 error = tcp_log_state_change(tp, optval);
2534 goto unlock_and_done;
2543 error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
2546 buf[sopt->sopt_valsize] = '\0';
2547 INP_WLOCK_RECHECK(inp);
2548 error = tcp_log_set_id(tp, buf);
2549 /* tcp_log_set_id() unlocks the INP. */
2556 sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
2559 buf[sopt->sopt_valsize] = '\0';
2560 INP_WLOCK_RECHECK(inp);
2561 if (sopt->sopt_name == TCP_LOGDUMP) {
2562 error = tcp_log_dump_tp_logbuf(tp, buf,
2566 tcp_log_dump_tp_bucket_logbufs(tp, buf);
2568 * tcp_log_dump_tp_bucket_logbufs() drops the
2577 error = ENOPROTOOPT;
2583 tp = intotcpcb(inp);
2584 switch (sopt->sopt_name) {
2585 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2587 if (!TCPMD5_ENABLED()) {
2589 return (ENOPROTOOPT);
2591 error = TCPMD5_PCBCTL(inp, sopt);
2596 optval = tp->t_flags & TF_NODELAY;
2598 error = sooptcopyout(sopt, &optval, sizeof optval);
2601 optval = tp->t_maxseg;
2603 error = sooptcopyout(sopt, &optval, sizeof optval);
2605 case TCP_REMOTE_UDP_ENCAPS_PORT:
2606 optval = ntohs(tp->t_port);
2608 error = sooptcopyout(sopt, &optval, sizeof optval);
2611 optval = tp->t_flags & TF_NOOPT;
2613 error = sooptcopyout(sopt, &optval, sizeof optval);
2616 optval = tp->t_flags & TF_NOPUSH;
2618 error = sooptcopyout(sopt, &optval, sizeof optval);
2621 tcp_fill_info(tp, &ti);
2623 error = sooptcopyout(sopt, &ti, sizeof ti);
2629 TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2632 socklen_t outsbsz = sopt->sopt_valsize;
2633 if (tp->t_stats == NULL)
2635 else if (outsbsz >= tp->t_stats->cursz)
2636 outsbsz = tp->t_stats->cursz;
2637 else if (outsbsz >= sizeof(struct statsblob))
2638 outsbsz = sizeof(struct statsblob);
2645 sbp = sopt->sopt_val;
2646 nheld = atop(round_page(((vm_offset_t)sbp) +
2647 (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2648 vm_page_t ma[nheld];
2649 if (vm_fault_quick_hold_pages(
2650 &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2651 outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2657 if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2658 SIZEOF_MEMBER(struct statsblob, flags))))
2661 INP_WLOCK_RECHECK(inp);
2662 error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2663 sbflags | SB_CLONE_USRDSTNOFAULT);
2665 sopt->sopt_valsize = outsbsz;
2667 vm_page_unhold_pages(ma, nheld);
2674 case TCP_CONGESTION:
2675 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2677 error = sooptcopyout(sopt, buf, len + 1);
2683 switch (sopt->sopt_name) {
2685 ui = TP_KEEPIDLE(tp) / hz;
2688 ui = TP_KEEPINTVL(tp) / hz;
2691 ui = TP_KEEPINIT(tp) / hz;
2694 ui = TP_KEEPCNT(tp);
2698 error = sooptcopyout(sopt, &ui, sizeof(ui));
2703 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
2704 &(tp->t_outpkts) : &(tp->t_inpkts));
2706 error = sooptcopyout(sopt, &optval, sizeof optval);
2710 optval = tp->t_flags & TF_FASTOPEN;
2712 error = sooptcopyout(sopt, &optval, sizeof optval);
2716 optval = tp->t_logstate;
2718 error = sooptcopyout(sopt, &optval, sizeof(optval));
2721 /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2722 error = tcp_log_getlogbuf(sopt, tp);
2725 len = tcp_log_get_id(tp, buf);
2727 error = sooptcopyout(sopt, buf, len + 1);
2736 case TCP_TXTLS_MODE:
2737 error = ktls_get_tx_mode(so, &optval);
2740 error = sooptcopyout(sopt, &optval,
2743 case TCP_RXTLS_MODE:
2744 error = ktls_get_rx_mode(so, &optval);
2747 error = sooptcopyout(sopt, &optval,
2752 optval = tp->t_flags & TF_LRD;
2754 error = sooptcopyout(sopt, &optval, sizeof optval);
2758 error = ENOPROTOOPT;
2765 #undef INP_WLOCK_RECHECK
2766 #undef INP_WLOCK_RECHECK_CLEANUP
2769 * Initiate (or continue) disconnect.
2770 * If embryonic state, just send reset (once).
2771 * If in ``let data drain'' option and linger null, just drop.
2772 * Otherwise (hard), mark socket disconnecting and drop
2773 * current input data; switch states based on user close, and
2774 * send segment to peer (with FIN).
2777 tcp_disconnect(struct tcpcb *tp)
2779 struct inpcb *inp = tp->t_inpcb;
2780 struct socket *so = inp->inp_socket;
2783 INP_WLOCK_ASSERT(inp);
2786 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2787 * socket is still open.
2789 if (tp->t_state < TCPS_ESTABLISHED &&
2790 !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2793 ("tcp_disconnect: tcp_close() returned NULL"));
2794 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2795 tp = tcp_drop(tp, 0);
2797 ("tcp_disconnect: tcp_drop() returned NULL"));
2799 soisdisconnecting(so);
2800 sbflush(&so->so_rcv);
2802 if (!(inp->inp_flags & INP_DROPPED))
2803 /* Ignore stack's drop request, we already at it. */
2804 (void)tcp_output_nodrop(tp);
2809 * User issued close, and wish to trail through shutdown states:
2810 * if never received SYN, just forget it. If got a SYN from peer,
2811 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2812 * If already got a FIN from peer, then almost done; go to LAST_ACK
2813 * state. In all other cases, have already sent FIN to peer (e.g.
2814 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2815 * for peer to send FIN or not respond to keep-alives, etc.
2816 * We can let the user exit from the close as soon as the FIN is acked.
2819 tcp_usrclosed(struct tcpcb *tp)
2823 INP_WLOCK_ASSERT(tp->t_inpcb);
2825 switch (tp->t_state) {
2828 tcp_offload_listen_stop(tp);
2830 tcp_state_change(tp, TCPS_CLOSED);
2835 * tcp_close() should never return NULL here as the socket is
2839 ("tcp_usrclosed: tcp_close() returned NULL"));
2843 case TCPS_SYN_RECEIVED:
2844 tp->t_flags |= TF_NEEDFIN;
2847 case TCPS_ESTABLISHED:
2848 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2851 case TCPS_CLOSE_WAIT:
2852 tcp_state_change(tp, TCPS_LAST_ACK);
2855 if (tp->t_state >= TCPS_FIN_WAIT_2) {
2856 soisdisconnected(tp->t_inpcb->inp_socket);
2857 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
2858 if (tp->t_state == TCPS_FIN_WAIT_2) {
2861 timeout = (tcp_fast_finwait2_recycle) ?
2862 tcp_finwait2_timeout : TP_MAXIDLE(tp);
2863 tcp_timer_activate(tp, TT_2MSL, timeout);
2870 db_print_indent(int indent)
2874 for (i = 0; i < indent; i++)
2879 db_print_tstate(int t_state)
2884 db_printf("TCPS_CLOSED");
2888 db_printf("TCPS_LISTEN");
2892 db_printf("TCPS_SYN_SENT");
2895 case TCPS_SYN_RECEIVED:
2896 db_printf("TCPS_SYN_RECEIVED");
2899 case TCPS_ESTABLISHED:
2900 db_printf("TCPS_ESTABLISHED");
2903 case TCPS_CLOSE_WAIT:
2904 db_printf("TCPS_CLOSE_WAIT");
2907 case TCPS_FIN_WAIT_1:
2908 db_printf("TCPS_FIN_WAIT_1");
2912 db_printf("TCPS_CLOSING");
2916 db_printf("TCPS_LAST_ACK");
2919 case TCPS_FIN_WAIT_2:
2920 db_printf("TCPS_FIN_WAIT_2");
2923 case TCPS_TIME_WAIT:
2924 db_printf("TCPS_TIME_WAIT");
2928 db_printf("unknown");
2934 db_print_tflags(u_int t_flags)
2939 if (t_flags & TF_ACKNOW) {
2940 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2943 if (t_flags & TF_DELACK) {
2944 db_printf("%sTF_DELACK", comma ? ", " : "");
2947 if (t_flags & TF_NODELAY) {
2948 db_printf("%sTF_NODELAY", comma ? ", " : "");
2951 if (t_flags & TF_NOOPT) {
2952 db_printf("%sTF_NOOPT", comma ? ", " : "");
2955 if (t_flags & TF_SENTFIN) {
2956 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2959 if (t_flags & TF_REQ_SCALE) {
2960 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2963 if (t_flags & TF_RCVD_SCALE) {
2964 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2967 if (t_flags & TF_REQ_TSTMP) {
2968 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2971 if (t_flags & TF_RCVD_TSTMP) {
2972 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2975 if (t_flags & TF_SACK_PERMIT) {
2976 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2979 if (t_flags & TF_NEEDSYN) {
2980 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2983 if (t_flags & TF_NEEDFIN) {
2984 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2987 if (t_flags & TF_NOPUSH) {
2988 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2991 if (t_flags & TF_PREVVALID) {
2992 db_printf("%sTF_PREVVALID", comma ? ", " : "");
2995 if (t_flags & TF_MORETOCOME) {
2996 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2999 if (t_flags & TF_LQ_OVERFLOW) {
3000 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
3003 if (t_flags & TF_LASTIDLE) {
3004 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
3007 if (t_flags & TF_RXWIN0SENT) {
3008 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
3011 if (t_flags & TF_FASTRECOVERY) {
3012 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
3015 if (t_flags & TF_CONGRECOVERY) {
3016 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
3019 if (t_flags & TF_WASFRECOVERY) {
3020 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
3023 if (t_flags & TF_WASCRECOVERY) {
3024 db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
3027 if (t_flags & TF_SIGNATURE) {
3028 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
3031 if (t_flags & TF_FORCEDATA) {
3032 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
3035 if (t_flags & TF_TSO) {
3036 db_printf("%sTF_TSO", comma ? ", " : "");
3039 if (t_flags & TF_FASTOPEN) {
3040 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
3046 db_print_tflags2(u_int t_flags2)
3051 if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
3052 db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
3055 if (t_flags2 & TF2_PLPMTU_PMTUD) {
3056 db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
3059 if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
3060 db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
3063 if (t_flags2 & TF2_LOG_AUTO) {
3064 db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
3067 if (t_flags2 & TF2_DROP_AF_DATA) {
3068 db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
3071 if (t_flags2 & TF2_ECN_PERMIT) {
3072 db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
3075 if (t_flags2 & TF2_ECN_SND_CWR) {
3076 db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
3079 if (t_flags2 & TF2_ECN_SND_ECE) {
3080 db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
3083 if (t_flags2 & TF2_ACE_PERMIT) {
3084 db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
3087 if (t_flags2 & TF2_FBYTES_COMPLETE) {
3088 db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
3094 db_print_toobflags(char t_oobflags)
3099 if (t_oobflags & TCPOOB_HAVEDATA) {
3100 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
3103 if (t_oobflags & TCPOOB_HADDATA) {
3104 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
3110 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
3113 db_print_indent(indent);
3114 db_printf("%s at %p\n", name, tp);
3118 db_print_indent(indent);
3119 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
3120 TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
3122 db_print_indent(indent);
3123 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
3124 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
3126 db_print_indent(indent);
3127 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
3128 &tp->t_timers->tt_delack, tp->t_inpcb);
3130 db_print_indent(indent);
3131 db_printf("t_state: %d (", tp->t_state);
3132 db_print_tstate(tp->t_state);
3135 db_print_indent(indent);
3136 db_printf("t_flags: 0x%x (", tp->t_flags);
3137 db_print_tflags(tp->t_flags);
3140 db_print_indent(indent);
3141 db_printf("t_flags2: 0x%x (", tp->t_flags2);
3142 db_print_tflags2(tp->t_flags2);
3145 db_print_indent(indent);
3146 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
3147 tp->snd_una, tp->snd_max, tp->snd_nxt);
3149 db_print_indent(indent);
3150 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
3151 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
3153 db_print_indent(indent);
3154 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
3155 tp->iss, tp->irs, tp->rcv_nxt);
3157 db_print_indent(indent);
3158 db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n",
3159 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
3161 db_print_indent(indent);
3162 db_printf("snd_wnd: %u snd_cwnd: %u\n",
3163 tp->snd_wnd, tp->snd_cwnd);
3165 db_print_indent(indent);
3166 db_printf("snd_ssthresh: %u snd_recover: "
3167 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3169 db_print_indent(indent);
3170 db_printf("t_rcvtime: %u t_startime: %u\n",
3171 tp->t_rcvtime, tp->t_starttime);
3173 db_print_indent(indent);
3174 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
3175 tp->t_rtttime, tp->t_rtseq);
3177 db_print_indent(indent);
3178 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
3179 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3181 db_print_indent(indent);
3182 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
3183 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
3186 db_print_indent(indent);
3187 db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n",
3188 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3190 db_print_indent(indent);
3191 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3192 db_print_toobflags(tp->t_oobflags);
3193 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
3195 db_print_indent(indent);
3196 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
3197 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3199 db_print_indent(indent);
3200 db_printf("ts_recent: %u ts_recent_age: %u\n",
3201 tp->ts_recent, tp->ts_recent_age);
3203 db_print_indent(indent);
3204 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
3205 "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3207 db_print_indent(indent);
3208 db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x "
3209 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3210 tp->snd_recover_prev, tp->t_badrxtwin);
3212 db_print_indent(indent);
3213 db_printf("snd_numholes: %d snd_holes first: %p\n",
3214 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3216 db_print_indent(indent);
3217 db_printf("snd_fack: 0x%08x rcv_numsacks: %d\n",
3218 tp->snd_fack, tp->rcv_numsacks);
3220 /* Skip sackblks, sackhint. */
3222 db_print_indent(indent);
3223 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
3224 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3227 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3232 db_printf("usage: show tcpcb <addr>\n");
3235 tp = (struct tcpcb *)addr;
3237 db_print_tcpcb(tp, "tcpcb", 0);