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>
106 #include <netinet/tcp_debug.h>
108 #include <netinet/tcp_offload.h>
110 #include <netipsec/ipsec_support.h>
113 #include <vm/vm_param.h>
115 #include <vm/vm_extern.h>
116 #include <vm/vm_map.h>
117 #include <vm/vm_page.h>
120 * TCP protocol interface to socket abstraction.
123 static int tcp_connect(struct tcpcb *, struct sockaddr *,
127 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
130 static void tcp_disconnect(struct tcpcb *);
131 static void tcp_usrclosed(struct tcpcb *);
132 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
134 static int tcp_pru_options_support(struct tcpcb *tp, int flags);
137 #define TCPDEBUG0 int ostate = 0
138 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
139 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
140 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
144 #define TCPDEBUG2(req)
148 * tcp_require_unique port requires a globally-unique source port for each
149 * outgoing connection. The default is to require the 4-tuple to be unique.
151 VNET_DEFINE(int, tcp_require_unique_port) = 0;
152 SYSCTL_INT(_net_inet_tcp, OID_AUTO, require_unique_port,
153 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_require_unique_port), 0,
154 "Require globally-unique ephemeral port for outgoing connections");
155 #define V_tcp_require_unique_port VNET(tcp_require_unique_port)
158 * TCP attaches to socket via pru_attach(), reserving space,
159 * and an internet control block.
162 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
165 struct tcpcb *tp = NULL;
170 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
173 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
177 so->so_rcv.sb_flags |= SB_AUTOSIZE;
178 so->so_snd.sb_flags |= SB_AUTOSIZE;
179 error = in_pcballoc(so, &V_tcbinfo);
183 tp = tcp_newtcpcb(inp);
190 tp->t_state = TCPS_CLOSED;
192 TCPSTATES_INC(TCPS_CLOSED);
194 TCPDEBUG2(PRU_ATTACH);
195 TCP_PROBE2(debug__user, tp, PRU_ATTACH);
200 * tcp_usr_detach is called when the socket layer loses its final reference
201 * to the socket, be it a file descriptor reference, a reference from TCP,
202 * etc. At this point, there is only one case in which we will keep around
203 * inpcb state: time wait.
206 tcp_usr_detach(struct socket *so)
212 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
214 KASSERT(so->so_pcb == inp && inp->inp_socket == so,
215 ("%s: socket %p inp %p mismatch", __func__, so, inp));
219 KASSERT(inp->inp_flags & INP_DROPPED ||
220 tp->t_state < TCPS_SYN_SENT,
221 ("%s: inp %p not dropped or embryonic", __func__, inp));
230 * Give the socket an address.
233 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
238 struct tcpcb *tp = NULL;
240 struct sockaddr_in *sinp;
242 sinp = (struct sockaddr_in *)nam;
243 if (nam->sa_family != AF_INET) {
245 * Preserve compatibility with old programs.
247 if (nam->sa_family != AF_UNSPEC ||
248 nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
249 sinp->sin_addr.s_addr != INADDR_ANY)
250 return (EAFNOSUPPORT);
251 nam->sa_family = AF_INET;
253 if (nam->sa_len != sizeof(*sinp))
257 * Must check for multicast addresses and disallow binding
260 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
261 return (EAFNOSUPPORT);
265 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
267 if (inp->inp_flags & INP_DROPPED) {
275 INP_HASH_WLOCK(&V_tcbinfo);
276 error = in_pcbbind(inp, nam, td->td_ucred);
277 INP_HASH_WUNLOCK(&V_tcbinfo);
280 TCP_PROBE2(debug__user, tp, PRU_BIND);
289 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
294 struct tcpcb *tp = NULL;
296 struct sockaddr_in6 *sin6;
299 sin6 = (struct sockaddr_in6 *)nam;
300 if (nam->sa_family != AF_INET6)
301 return (EAFNOSUPPORT);
302 if (nam->sa_len != sizeof(*sin6))
306 * Must check for multicast addresses and disallow binding
309 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
310 return (EAFNOSUPPORT);
314 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
316 vflagsav = inp->inp_vflag;
317 if (inp->inp_flags & INP_DROPPED) {
325 INP_HASH_WLOCK(&V_tcbinfo);
326 inp->inp_vflag &= ~INP_IPV4;
327 inp->inp_vflag |= INP_IPV6;
329 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
330 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
331 inp->inp_vflag |= INP_IPV4;
332 else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
333 struct sockaddr_in sin;
335 in6_sin6_2_sin(&sin, sin6);
336 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
337 error = EAFNOSUPPORT;
338 INP_HASH_WUNLOCK(&V_tcbinfo);
341 inp->inp_vflag |= INP_IPV4;
342 inp->inp_vflag &= ~INP_IPV6;
343 error = in_pcbbind(inp, (struct sockaddr *)&sin,
345 INP_HASH_WUNLOCK(&V_tcbinfo);
350 error = in6_pcbbind(inp, nam, td->td_ucred);
351 INP_HASH_WUNLOCK(&V_tcbinfo);
354 inp->inp_vflag = vflagsav;
356 TCP_PROBE2(debug__user, tp, PRU_BIND);
364 * Prepare to accept connections.
367 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
371 struct tcpcb *tp = NULL;
375 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
377 if (inp->inp_flags & INP_DROPPED) {
384 error = solisten_proto_check(so);
389 if (inp->inp_lport == 0) {
390 INP_HASH_WLOCK(&V_tcbinfo);
391 error = in_pcbbind(inp, NULL, td->td_ucred);
392 INP_HASH_WUNLOCK(&V_tcbinfo);
395 tcp_state_change(tp, TCPS_LISTEN);
396 solisten_proto(so, backlog);
398 if ((so->so_options & SO_NO_OFFLOAD) == 0)
399 tcp_offload_listen_start(tp);
402 solisten_proto_abort(so);
406 if (IS_FASTOPEN(tp->t_flags))
407 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
410 TCPDEBUG2(PRU_LISTEN);
411 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
419 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
423 struct tcpcb *tp = NULL;
428 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
430 if (inp->inp_flags & INP_DROPPED) {
434 vflagsav = inp->inp_vflag;
438 error = solisten_proto_check(so);
443 INP_HASH_WLOCK(&V_tcbinfo);
444 if (inp->inp_lport == 0) {
445 inp->inp_vflag &= ~INP_IPV4;
446 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
447 inp->inp_vflag |= INP_IPV4;
448 error = in6_pcbbind(inp, NULL, td->td_ucred);
450 INP_HASH_WUNLOCK(&V_tcbinfo);
452 tcp_state_change(tp, TCPS_LISTEN);
453 solisten_proto(so, backlog);
455 if ((so->so_options & SO_NO_OFFLOAD) == 0)
456 tcp_offload_listen_start(tp);
459 solisten_proto_abort(so);
463 if (IS_FASTOPEN(tp->t_flags))
464 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
467 inp->inp_vflag = vflagsav;
470 TCPDEBUG2(PRU_LISTEN);
471 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
479 * Initiate connection to peer.
480 * Create a template for use in transmissions on this connection.
481 * Enter SYN_SENT state, and mark socket as connecting.
482 * Start keep-alive timer, and seed output sequence space.
483 * Send initial segment on connection.
486 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
488 struct epoch_tracker et;
491 struct tcpcb *tp = NULL;
492 struct sockaddr_in *sinp;
494 sinp = (struct sockaddr_in *)nam;
495 if (nam->sa_family != AF_INET)
496 return (EAFNOSUPPORT);
497 if (nam->sa_len != sizeof (*sinp))
501 * Must disallow TCP ``connections'' to multicast addresses.
503 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
504 return (EAFNOSUPPORT);
505 if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST)
507 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
512 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
514 if (inp->inp_flags & INP_DROPPED) {
515 error = ECONNREFUSED;
518 if (SOLISTENING(so)) {
525 if ((error = tcp_connect(tp, nam, td)) != 0)
528 if (registered_toedevs > 0 &&
529 (so->so_options & SO_NO_OFFLOAD) == 0 &&
530 (error = tcp_offload_connect(so, nam)) == 0)
533 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
534 error = tcp_output(tp);
535 KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
536 ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
540 TCPDEBUG2(PRU_CONNECT);
541 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
549 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
551 struct epoch_tracker et;
554 struct tcpcb *tp = NULL;
555 struct sockaddr_in6 *sin6;
561 sin6 = (struct sockaddr_in6 *)nam;
562 if (nam->sa_family != AF_INET6)
563 return (EAFNOSUPPORT);
564 if (nam->sa_len != sizeof (*sin6))
568 * Must disallow TCP ``connections'' to multicast addresses.
570 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
571 return (EAFNOSUPPORT);
574 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
576 vflagsav = inp->inp_vflag;
577 incflagsav = inp->inp_inc.inc_flags;
578 if (inp->inp_flags & INP_DROPPED) {
579 error = ECONNREFUSED;
582 if (SOLISTENING(so)) {
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?
594 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
595 struct sockaddr_in sin;
597 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
601 if ((inp->inp_vflag & INP_IPV4) == 0) {
602 error = EAFNOSUPPORT;
606 in6_sin6_2_sin(&sin, sin6);
607 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
608 error = EAFNOSUPPORT;
611 if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
615 if ((error = prison_remote_ip4(td->td_ucred,
616 &sin.sin_addr)) != 0)
618 inp->inp_vflag |= INP_IPV4;
619 inp->inp_vflag &= ~INP_IPV6;
621 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
624 if (registered_toedevs > 0 &&
625 (so->so_options & SO_NO_OFFLOAD) == 0 &&
626 (error = tcp_offload_connect(so, nam)) == 0)
629 error = tcp_output(tp);
632 if ((inp->inp_vflag & INP_IPV6) == 0) {
633 error = EAFNOSUPPORT;
638 if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
640 inp->inp_vflag &= ~INP_IPV4;
641 inp->inp_vflag |= INP_IPV6;
642 inp->inp_inc.inc_flags |= INC_ISIPV6;
644 if ((error = tcp6_connect(tp, nam, td)) != 0)
647 if (registered_toedevs > 0 &&
648 (so->so_options & SO_NO_OFFLOAD) == 0 &&
649 (error = tcp_offload_connect(so, nam)) == 0)
652 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
653 error = tcp_output(tp);
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));
660 * If the implicit bind in the connect call fails, restore
661 * the flags we modified.
663 if (error != 0 && inp->inp_lport == 0) {
664 inp->inp_vflag = vflagsav;
665 inp->inp_inc.inc_flags = incflagsav;
668 TCPDEBUG2(PRU_CONNECT);
669 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
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.
684 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
687 tcp_usr_disconnect(struct socket *so)
690 struct tcpcb *tp = NULL;
691 struct epoch_tracker et;
697 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
699 if (inp->inp_flags & INP_DROPPED) {
707 TCPDEBUG2(PRU_DISCONNECT);
708 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
716 * Accept a connection. Essentially all the work is done at higher levels;
717 * just return the address of the peer, storing through addr.
720 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
723 struct inpcb *inp = NULL;
725 struct tcpcb *tp = NULL;
731 if (so->so_state & SS_ISDISCONNECTED)
732 return (ECONNABORTED);
735 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
737 if (inp->inp_flags & INP_DROPPED) {
738 error = ECONNABORTED;
747 * We inline in_getpeeraddr and COMMON_END here, so that we can
748 * copy the data of interest and defer the malloc until after we
751 port = inp->inp_fport;
752 addr = inp->inp_faddr;
755 TCPDEBUG2(PRU_ACCEPT);
756 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
759 *nam = in_sockaddr(port, &addr);
766 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
768 struct inpcb *inp = NULL;
771 struct tcpcb *tp = NULL;
774 struct in6_addr addr6;
775 struct epoch_tracker et;
780 if (so->so_state & SS_ISDISCONNECTED)
781 return (ECONNABORTED);
784 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
787 if (inp->inp_flags & INP_DROPPED) {
788 error = ECONNABORTED;
797 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
798 * copy the data of interest and defer the malloc until after we
801 if (inp->inp_vflag & INP_IPV4) {
803 port = inp->inp_fport;
804 addr = inp->inp_faddr;
806 port = inp->inp_fport;
807 addr6 = inp->in6p_faddr;
811 TCPDEBUG2(PRU_ACCEPT);
812 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
817 *nam = in6_v4mapsin6_sockaddr(port, &addr);
819 *nam = in6_sockaddr(port, &addr6);
826 * Mark the connection as being incapable of further output.
829 tcp_usr_shutdown(struct socket *so)
833 struct tcpcb *tp = NULL;
834 struct epoch_tracker et;
838 KASSERT(inp != NULL, ("inp == NULL"));
840 if (inp->inp_flags & INP_DROPPED) {
849 if (!(inp->inp_flags & INP_DROPPED))
850 error = tcp_output_nodrop(tp);
851 TCPDEBUG2(PRU_SHUTDOWN);
852 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
853 error = tcp_unlock_or_drop(tp, error);
860 * After a receive, possibly send window update to peer.
863 tcp_usr_rcvd(struct socket *so, int flags)
865 struct epoch_tracker et;
867 struct tcpcb *tp = NULL;
868 int outrv = 0, error = 0;
872 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
874 if (inp->inp_flags & INP_DROPPED) {
882 * For passively-created TFO connections, don't attempt a window
883 * update while still in SYN_RECEIVED as this may trigger an early
884 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
885 * application response data, or failing that, when the DELACK timer
888 if (IS_FASTOPEN(tp->t_flags) &&
889 (tp->t_state == TCPS_SYN_RECEIVED))
892 if (tp->t_flags & TF_TOE)
893 tcp_offload_rcvd(tp);
896 outrv = tcp_output_nodrop(tp);
899 TCP_PROBE2(debug__user, tp, PRU_RCVD);
900 (void) tcp_unlock_or_drop(tp, outrv);
906 * Do a send by putting data in output queue and updating urgent
907 * marker if URG set. Possibly send more data. Unlike the other
908 * pru_*() routines, the mbuf chains are our responsibility. We
909 * must either enqueue them or free them. The other pru_* routines
910 * generally are caller-frees.
913 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
914 struct sockaddr *nam, struct mbuf *control, struct thread *td)
916 struct epoch_tracker et;
919 struct tcpcb *tp = NULL;
922 struct sockaddr_in sin;
924 struct sockaddr_in *sinp;
934 if (control != NULL) {
935 /* TCP doesn't do control messages (rights, creds, etc) */
936 if (control->m_len) {
940 m_freem(control); /* empty control, just free it */
944 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
946 if (inp->inp_flags & INP_DROPPED) {
947 if (m != NULL && (flags & PRUS_NOTREADY) == 0)
953 vflagsav = inp->inp_vflag;
954 incflagsav = inp->inp_inc.inc_flags;
955 restoreflags = false;
959 if ((flags & PRUS_OOB) != 0 &&
960 (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
964 if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
965 if (tp->t_state == TCPS_LISTEN) {
969 switch (nam->sa_family) {
972 sinp = (struct sockaddr_in *)nam;
973 if (sinp->sin_len != sizeof(struct sockaddr_in)) {
977 if ((inp->inp_vflag & INP_IPV6) != 0) {
978 error = EAFNOSUPPORT;
981 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
982 error = EAFNOSUPPORT;
985 if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
989 if ((error = prison_remote_ip4(td->td_ucred,
1000 struct sockaddr_in6 *sin6;
1002 sin6 = (struct sockaddr_in6 *)nam;
1003 if (sin6->sin6_len != sizeof(*sin6)) {
1007 if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
1008 error = EAFNOSUPPORT;
1011 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1012 error = EAFNOSUPPORT;
1015 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1017 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1021 if ((inp->inp_vflag & INP_IPV4) == 0) {
1022 error = EAFNOSUPPORT;
1025 restoreflags = true;
1026 inp->inp_vflag &= ~INP_IPV6;
1028 in6_sin6_2_sin(sinp, sin6);
1030 ntohl(sinp->sin_addr.s_addr))) {
1031 error = EAFNOSUPPORT;
1034 if ((error = prison_remote_ip4(td->td_ucred,
1039 error = EAFNOSUPPORT;
1043 if ((inp->inp_vflag & INP_IPV6) == 0) {
1044 error = EAFNOSUPPORT;
1047 restoreflags = true;
1048 inp->inp_vflag &= ~INP_IPV4;
1049 inp->inp_inc.inc_flags |= INC_ISIPV6;
1050 if ((error = prison_remote_ip6(td->td_ucred,
1059 error = EAFNOSUPPORT;
1063 if (!(flags & PRUS_OOB)) {
1064 if (tp->t_acktime == 0)
1065 tp->t_acktime = ticks;
1066 sbappendstream(&so->so_snd, m, flags);
1068 if (nam && tp->t_state < TCPS_SYN_SENT) {
1069 KASSERT(tp->t_state == TCPS_CLOSED,
1070 ("%s: tp %p is listening", __func__, tp));
1073 * Do implied connect if not yet connected,
1074 * initialize window to default value, and
1075 * initialize maxseg using peer's cached MSS.
1079 error = tcp6_connect(tp, nam, td);
1081 #if defined(INET6) && defined(INET)
1085 error = tcp_connect(tp,
1086 (struct sockaddr *)sinp, td);
1089 * The bind operation in tcp_connect succeeded. We
1090 * no longer want to restore the flags if later
1093 if (error == 0 || inp->inp_lport != 0)
1094 restoreflags = false;
1097 /* m is freed if PRUS_NOTREADY is unset. */
1098 sbflush(&so->so_snd);
1101 if (IS_FASTOPEN(tp->t_flags))
1102 tcp_fastopen_connect(tp);
1104 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1108 if (flags & PRUS_EOF) {
1110 * Close the send side of the connection after
1116 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1117 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1118 (tp->t_fbyte_out == 0) &&
1119 (so->so_snd.sb_ccc > 0)) {
1120 tp->t_fbyte_out = ticks;
1121 if (tp->t_fbyte_out == 0)
1122 tp->t_fbyte_out = 1;
1123 if (tp->t_fbyte_out && tp->t_fbyte_in)
1124 tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1126 if (!(inp->inp_flags & INP_DROPPED) &&
1127 !(flags & PRUS_NOTREADY)) {
1128 if (flags & PRUS_MORETOCOME)
1129 tp->t_flags |= TF_MORETOCOME;
1130 error = tcp_output_nodrop(tp);
1131 if (flags & PRUS_MORETOCOME)
1132 tp->t_flags &= ~TF_MORETOCOME;
1136 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1138 SOCKBUF_LOCK(&so->so_snd);
1139 if (sbspace(&so->so_snd) < -512) {
1140 SOCKBUF_UNLOCK(&so->so_snd);
1145 * According to RFC961 (Assigned Protocols),
1146 * the urgent pointer points to the last octet
1147 * of urgent data. We continue, however,
1148 * to consider it to indicate the first octet
1149 * of data past the urgent section.
1150 * Otherwise, snd_up should be one lower.
1152 if (tp->t_acktime == 0)
1153 tp->t_acktime = ticks;
1154 sbappendstream_locked(&so->so_snd, m, flags);
1155 SOCKBUF_UNLOCK(&so->so_snd);
1157 if (nam && tp->t_state < TCPS_SYN_SENT) {
1159 * Do implied connect if not yet connected,
1160 * initialize window to default value, and
1161 * initialize maxseg using peer's cached MSS.
1165 * Not going to contemplate SYN|URG
1167 if (IS_FASTOPEN(tp->t_flags))
1168 tp->t_flags &= ~TF_FASTOPEN;
1171 error = tcp6_connect(tp, nam, td);
1173 #if defined(INET6) && defined(INET)
1177 error = tcp_connect(tp,
1178 (struct sockaddr *)sinp, td);
1181 * The bind operation in tcp_connect succeeded. We
1182 * no longer want to restore the flags if later
1185 if (error == 0 || inp->inp_lport != 0)
1186 restoreflags = false;
1189 /* m is freed if PRUS_NOTREADY is unset. */
1190 sbflush(&so->so_snd);
1193 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1196 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1197 if ((flags & PRUS_NOTREADY) == 0) {
1198 tp->t_flags |= TF_FORCEDATA;
1199 error = tcp_output_nodrop(tp);
1200 tp->t_flags &= ~TF_FORCEDATA;
1203 TCP_LOG_EVENT(tp, NULL,
1204 &inp->inp_socket->so_rcv,
1205 &inp->inp_socket->so_snd,
1206 TCP_LOG_USERSEND, error,
1211 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
1212 * responsible for freeing memory.
1214 if (m != NULL && (flags & PRUS_NOTREADY) == 0)
1218 * If the request was unsuccessful and we changed flags,
1219 * restore the original flags.
1221 if (error != 0 && restoreflags) {
1222 inp->inp_vflag = vflagsav;
1223 inp->inp_inc.inc_flags = incflagsav;
1225 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
1226 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1227 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1228 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1229 error = tcp_unlock_or_drop(tp, error);
1235 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1237 struct epoch_tracker et;
1242 inp = sotoinpcb(so);
1244 if (inp->inp_flags & INP_DROPPED) {
1246 mb_free_notready(m, count);
1247 return (ECONNRESET);
1249 tp = intotcpcb(inp);
1251 SOCKBUF_LOCK(&so->so_snd);
1252 error = sbready(&so->so_snd, m, count);
1253 SOCKBUF_UNLOCK(&so->so_snd);
1258 NET_EPOCH_ENTER(et);
1259 error = tcp_output_unlock(tp);
1266 * Abort the TCP. Drop the connection abruptly.
1269 tcp_usr_abort(struct socket *so)
1272 struct tcpcb *tp = NULL;
1273 struct epoch_tracker et;
1276 inp = sotoinpcb(so);
1277 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1279 NET_EPOCH_ENTER(et);
1281 KASSERT(inp->inp_socket != NULL,
1282 ("tcp_usr_abort: inp_socket == NULL"));
1285 * If we still have full TCP state, and we're not dropped, drop.
1287 if (!(inp->inp_flags & INP_DROPPED)) {
1288 tp = intotcpcb(inp);
1290 tp = tcp_drop(tp, ECONNABORTED);
1293 TCPDEBUG2(PRU_ABORT);
1294 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1296 if (!(inp->inp_flags & INP_DROPPED)) {
1298 inp->inp_flags |= INP_SOCKREF;
1306 * TCP socket is closed. Start friendly disconnect.
1309 tcp_usr_close(struct socket *so)
1312 struct tcpcb *tp = NULL;
1313 struct epoch_tracker et;
1316 inp = sotoinpcb(so);
1317 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1319 NET_EPOCH_ENTER(et);
1321 KASSERT(inp->inp_socket != NULL,
1322 ("tcp_usr_close: inp_socket == NULL"));
1325 * If we still have full TCP state, and we're not dropped, initiate
1328 if (!(inp->inp_flags & INP_DROPPED)) {
1329 tp = intotcpcb(inp);
1330 tp->t_flags |= TF_CLOSED;
1333 TCPDEBUG2(PRU_CLOSE);
1334 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1336 if (!(inp->inp_flags & INP_DROPPED)) {
1338 inp->inp_flags |= INP_SOCKREF;
1345 tcp_pru_options_support(struct tcpcb *tp, int flags)
1348 * If the specific TCP stack has a pru_options
1349 * specified then it does not always support
1350 * all the PRU_XX options and we must ask it.
1351 * If the function is not specified then all
1352 * of the PRU_XX options are supported.
1356 if (tp->t_fb->tfb_pru_options) {
1357 ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1363 * Receive out-of-band data.
1366 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1370 struct tcpcb *tp = NULL;
1373 inp = sotoinpcb(so);
1374 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1376 if (inp->inp_flags & INP_DROPPED) {
1380 tp = intotcpcb(inp);
1381 error = tcp_pru_options_support(tp, PRUS_OOB);
1386 if ((so->so_oobmark == 0 &&
1387 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1388 so->so_options & SO_OOBINLINE ||
1389 tp->t_oobflags & TCPOOB_HADDATA) {
1393 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1394 error = EWOULDBLOCK;
1398 *mtod(m, caddr_t) = tp->t_iobc;
1399 if ((flags & MSG_PEEK) == 0)
1400 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1403 TCPDEBUG2(PRU_RCVOOB);
1404 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1410 struct protosw tcp_protosw = {
1411 .pr_type = SOCK_STREAM,
1412 .pr_protocol = IPPROTO_TCP,
1413 .pr_flags = PR_CONNREQUIRED | PR_IMPLOPCL | PR_WANTRCVD |
1415 .pr_ctloutput = tcp_ctloutput,
1416 .pr_abort = tcp_usr_abort,
1417 .pr_accept = tcp_usr_accept,
1418 .pr_attach = tcp_usr_attach,
1419 .pr_bind = tcp_usr_bind,
1420 .pr_connect = tcp_usr_connect,
1421 .pr_control = in_control,
1422 .pr_detach = tcp_usr_detach,
1423 .pr_disconnect = tcp_usr_disconnect,
1424 .pr_listen = tcp_usr_listen,
1425 .pr_peeraddr = in_getpeeraddr,
1426 .pr_rcvd = tcp_usr_rcvd,
1427 .pr_rcvoob = tcp_usr_rcvoob,
1428 .pr_send = tcp_usr_send,
1429 .pr_ready = tcp_usr_ready,
1430 .pr_shutdown = tcp_usr_shutdown,
1431 .pr_sockaddr = in_getsockaddr,
1432 .pr_sosetlabel = in_pcbsosetlabel,
1433 .pr_close = tcp_usr_close,
1438 struct protosw tcp6_protosw = {
1439 .pr_type = SOCK_STREAM,
1440 .pr_protocol = IPPROTO_TCP,
1441 .pr_flags = PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD |
1443 .pr_ctloutput = tcp_ctloutput,
1444 .pr_abort = tcp_usr_abort,
1445 .pr_accept = tcp6_usr_accept,
1446 .pr_attach = tcp_usr_attach,
1447 .pr_bind = tcp6_usr_bind,
1448 .pr_connect = tcp6_usr_connect,
1449 .pr_control = in6_control,
1450 .pr_detach = tcp_usr_detach,
1451 .pr_disconnect = tcp_usr_disconnect,
1452 .pr_listen = tcp6_usr_listen,
1453 .pr_peeraddr = in6_mapped_peeraddr,
1454 .pr_rcvd = tcp_usr_rcvd,
1455 .pr_rcvoob = tcp_usr_rcvoob,
1456 .pr_send = tcp_usr_send,
1457 .pr_ready = tcp_usr_ready,
1458 .pr_shutdown = tcp_usr_shutdown,
1459 .pr_sockaddr = in6_mapped_sockaddr,
1460 .pr_sosetlabel = in_pcbsosetlabel,
1461 .pr_close = tcp_usr_close,
1467 * Common subroutine to open a TCP connection to remote host specified
1468 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1469 * port number if needed. Call in_pcbconnect_setup to do the routing and
1470 * to choose a local host address (interface). If there is an existing
1471 * incarnation of the same connection in TIME-WAIT state and if the remote
1472 * host was sending CC options and if the connection duration was < MSL, then
1473 * truncate the previous TIME-WAIT state and proceed.
1474 * Initialize connection parameters and enter SYN-SENT state.
1477 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1479 struct inpcb *inp = tptoinpcb(tp), *oinp;
1480 struct socket *so = tptosocket(tp);
1481 struct in_addr laddr;
1486 INP_WLOCK_ASSERT(inp);
1487 INP_HASH_WLOCK(&V_tcbinfo);
1489 if (V_tcp_require_unique_port && inp->inp_lport == 0) {
1490 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1496 * Cannot simply call in_pcbconnect, because there might be an
1497 * earlier incarnation of this same connection still in
1498 * TIME_WAIT state, creating an ADDRINUSE error.
1500 laddr = inp->inp_laddr;
1501 lport = inp->inp_lport;
1502 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1503 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1504 if (error && oinp == NULL)
1510 /* Handle initial bind if it hadn't been done in advance. */
1511 if (inp->inp_lport == 0) {
1512 inp->inp_lport = lport;
1513 if (in_pcbinshash(inp) != 0) {
1519 inp->inp_laddr = laddr;
1521 INP_HASH_WUNLOCK(&V_tcbinfo);
1524 * Compute window scaling to request:
1525 * Scale to fit into sweet spot. See tcp_syncache.c.
1526 * XXX: This should move to tcp_output().
1528 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1529 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1530 tp->request_r_scale++;
1533 TCPSTAT_INC(tcps_connattempt);
1534 tcp_state_change(tp, TCPS_SYN_SENT);
1535 tp->iss = tcp_new_isn(&inp->inp_inc);
1536 if (tp->t_flags & TF_REQ_TSTMP)
1537 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1538 tcp_sendseqinit(tp);
1543 INP_HASH_WUNLOCK(&V_tcbinfo);
1550 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1552 struct inpcb *inp = tptoinpcb(tp);
1555 INP_WLOCK_ASSERT(inp);
1556 INP_HASH_WLOCK(&V_tcbinfo);
1558 if (V_tcp_require_unique_port && inp->inp_lport == 0) {
1559 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1563 error = in6_pcbconnect(inp, nam, td->td_ucred);
1566 INP_HASH_WUNLOCK(&V_tcbinfo);
1568 /* Compute window scaling to request. */
1569 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1570 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1571 tp->request_r_scale++;
1573 soisconnecting(inp->inp_socket);
1574 TCPSTAT_INC(tcps_connattempt);
1575 tcp_state_change(tp, TCPS_SYN_SENT);
1576 tp->iss = tcp_new_isn(&inp->inp_inc);
1577 if (tp->t_flags & TF_REQ_TSTMP)
1578 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1579 tcp_sendseqinit(tp);
1584 INP_HASH_WUNLOCK(&V_tcbinfo);
1590 * Export TCP internal state information via a struct tcp_info, based on the
1591 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1592 * (TCP state machine, etc). We export all information using FreeBSD-native
1593 * constants -- for example, the numeric values for tcpi_state will differ
1597 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1600 INP_WLOCK_ASSERT(tptoinpcb(tp));
1601 bzero(ti, sizeof(*ti));
1603 ti->tcpi_state = tp->t_state;
1604 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1605 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1606 if (tp->t_flags & TF_SACK_PERMIT)
1607 ti->tcpi_options |= TCPI_OPT_SACK;
1608 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1609 ti->tcpi_options |= TCPI_OPT_WSCALE;
1610 ti->tcpi_snd_wscale = tp->snd_scale;
1611 ti->tcpi_rcv_wscale = tp->rcv_scale;
1613 if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
1614 ti->tcpi_options |= TCPI_OPT_ECN;
1616 ti->tcpi_rto = tp->t_rxtcur * tick;
1617 ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1618 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1619 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1621 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1622 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1625 * FreeBSD-specific extension fields for tcp_info.
1627 ti->tcpi_rcv_space = tp->rcv_wnd;
1628 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1629 ti->tcpi_snd_wnd = tp->snd_wnd;
1630 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1631 ti->tcpi_snd_nxt = tp->snd_nxt;
1632 ti->tcpi_snd_mss = tp->t_maxseg;
1633 ti->tcpi_rcv_mss = tp->t_maxseg;
1634 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1635 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1636 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1638 if (tp->t_flags & TF_TOE) {
1639 ti->tcpi_options |= TCPI_OPT_TOE;
1640 tcp_offload_tcp_info(tp, ti);
1644 * AccECN related counters.
1646 if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) ==
1647 (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
1649 * Internal counter starts at 5 for AccECN
1650 * but 0 for RFC3168 ECN.
1652 ti->tcpi_delivered_ce = tp->t_scep - 5;
1654 ti->tcpi_delivered_ce = tp->t_scep;
1655 ti->tcpi_received_ce = tp->t_rcep;
1659 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1660 * socket option arguments. When it re-acquires the lock after the copy, it
1661 * has to revalidate that the connection is still valid for the socket
1664 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \
1666 if (inp->inp_flags & INP_DROPPED) { \
1669 return (ECONNRESET); \
1671 tp = intotcpcb(inp); \
1673 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1676 tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt)
1678 struct socket *so = inp->inp_socket;
1679 struct tcpcb *tp = intotcpcb(inp);
1682 MPASS(sopt->sopt_dir == SOPT_SET);
1683 INP_WLOCK_ASSERT(inp);
1684 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1685 ("inp_flags == %x", inp->inp_flags));
1686 KASSERT(so != NULL, ("inp_socket == NULL"));
1688 if (sopt->sopt_level != IPPROTO_TCP) {
1691 if (inp->inp_vflag & INP_IPV6PROTO)
1692 error = ip6_ctloutput(so, sopt);
1694 #if defined(INET6) && defined(INET)
1698 error = ip_ctloutput(so, sopt);
1701 * When an IP-level socket option affects TCP, pass control
1702 * down to stack tfb_tcp_ctloutput, otherwise return what
1703 * IP level returned.
1705 switch (sopt->sopt_level) {
1708 if ((inp->inp_vflag & INP_IPV6PROTO) == 0)
1710 switch (sopt->sopt_name) {
1712 /* Notify tcp stacks that care (e.g. RACK). */
1714 case IPV6_USE_MIN_MTU:
1715 /* Update t_maxseg accordingly. */
1724 switch (sopt->sopt_name) {
1726 inp->inp_ip_tos &= ~IPTOS_ECN_MASK;
1729 /* Notify tcp stacks that care (e.g. RACK). */
1740 if (inp->inp_flags & INP_DROPPED) {
1742 return (ECONNRESET);
1744 } else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1746 * Protect the TCP option TCP_FUNCTION_BLK so
1747 * that a sub-function can *never* overwrite this.
1749 struct tcp_function_set fsn;
1750 struct tcp_function_block *blk;
1753 error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1758 if (inp->inp_flags & INP_DROPPED) {
1760 return (ECONNRESET);
1762 tp = intotcpcb(inp);
1764 blk = find_and_ref_tcp_functions(&fsn);
1769 if (tp->t_fb == blk) {
1770 /* You already have this */
1771 refcount_release(&blk->tfb_refcnt);
1775 if (tp->t_state != TCPS_CLOSED) {
1777 * The user has advanced the state
1778 * past the initial point, we may not
1779 * be able to switch.
1781 if (blk->tfb_tcp_handoff_ok != NULL) {
1783 * Does the stack provide a
1784 * query mechanism, if so it may
1785 * still be possible?
1787 error = (*blk->tfb_tcp_handoff_ok)(tp);
1791 refcount_release(&blk->tfb_refcnt);
1796 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1797 refcount_release(&blk->tfb_refcnt);
1802 * Release the old refcnt, the
1803 * lookup acquired a ref on the
1806 if (tp->t_fb->tfb_tcp_fb_fini) {
1807 struct epoch_tracker et;
1809 * Tell the stack to cleanup with 0 i.e.
1810 * the tcb is not going away.
1812 NET_EPOCH_ENTER(et);
1813 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1817 /* Assure that we are not on any hpts */
1818 tcp_hpts_remove(tptoinpcb(tp));
1820 if (blk->tfb_tcp_fb_init) {
1821 error = (*blk->tfb_tcp_fb_init)(tp);
1823 refcount_release(&blk->tfb_refcnt);
1824 if (tp->t_fb->tfb_tcp_fb_init) {
1825 if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) {
1826 /* Fall back failed, drop the connection */
1835 refcount_release(&tp->t_fb->tfb_refcnt);
1838 if (tp->t_flags & TF_TOE) {
1839 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1848 /* Pass in the INP locked, callee must unlock it. */
1849 return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1853 tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1855 struct socket *so = inp->inp_socket;
1856 struct tcpcb *tp = intotcpcb(inp);
1859 MPASS(sopt->sopt_dir == SOPT_GET);
1860 INP_WLOCK_ASSERT(inp);
1861 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1862 ("inp_flags == %x", inp->inp_flags));
1863 KASSERT(so != NULL, ("inp_socket == NULL"));
1865 if (sopt->sopt_level != IPPROTO_TCP) {
1868 if (inp->inp_vflag & INP_IPV6PROTO)
1869 error = ip6_ctloutput(so, sopt);
1871 #if defined(INET6) && defined(INET)
1875 error = ip_ctloutput(so, sopt);
1879 if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1880 (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1881 struct tcp_function_set fsn;
1883 if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1884 memset(&fsn, 0, sizeof(fsn));
1885 find_tcp_function_alias(tp->t_fb, &fsn);
1887 strncpy(fsn.function_set_name,
1888 tp->t_fb->tfb_tcp_block_name,
1889 TCP_FUNCTION_NAME_LEN_MAX);
1890 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1892 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1894 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1898 /* Pass in the INP locked, callee must unlock it. */
1899 return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1903 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1907 inp = sotoinpcb(so);
1908 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1911 if (inp->inp_flags & INP_DROPPED) {
1913 return (ECONNRESET);
1915 if (sopt->sopt_dir == SOPT_SET)
1916 return (tcp_ctloutput_set(inp, sopt));
1917 else if (sopt->sopt_dir == SOPT_GET)
1918 return (tcp_ctloutput_get(inp, sopt));
1920 panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
1924 * If this assert becomes untrue, we need to change the size of the buf
1925 * variable in tcp_default_ctloutput().
1928 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1929 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1934 copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1936 struct tls_enable_v0 tls_v0;
1939 if (sopt->sopt_valsize == sizeof(tls_v0)) {
1940 error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1944 memset(tls, 0, sizeof(*tls));
1945 tls->cipher_key = tls_v0.cipher_key;
1946 tls->iv = tls_v0.iv;
1947 tls->auth_key = tls_v0.auth_key;
1948 tls->cipher_algorithm = tls_v0.cipher_algorithm;
1949 tls->cipher_key_len = tls_v0.cipher_key_len;
1950 tls->iv_len = tls_v0.iv_len;
1951 tls->auth_algorithm = tls_v0.auth_algorithm;
1952 tls->auth_key_len = tls_v0.auth_key_len;
1953 tls->flags = tls_v0.flags;
1954 tls->tls_vmajor = tls_v0.tls_vmajor;
1955 tls->tls_vminor = tls_v0.tls_vminor;
1959 return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1963 extern struct cc_algo newreno_cc_algo;
1966 tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
1968 struct cc_algo *algo;
1971 struct cc_var cc_mem;
1972 char buf[TCP_CA_NAME_MAX];
1977 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1980 buf[sopt->sopt_valsize] = '\0';
1982 STAILQ_FOREACH(algo, &cc_list, entries) {
1983 if (strncmp(buf, algo->name,
1984 TCP_CA_NAME_MAX) == 0) {
1985 if (algo->flags & CC_MODULE_BEING_REMOVED) {
1986 /* We can't "see" modules being unloaded */
1997 * With a reference the algorithm cannot be removed
1998 * so we hold a reference through the change process.
2002 if (algo->cb_init != NULL) {
2003 /* We can now pre-get the memory for the CC */
2004 mem_sz = (*algo->cc_data_sz)();
2008 ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
2015 * Make sure its all clean and zero and also get
2018 memset(&cc_mem, 0, sizeof(cc_mem));
2020 if (inp->inp_flags & INP_DROPPED) {
2023 free(ptr, M_CC_MEM);
2024 /* Release our temp reference */
2028 return (ECONNRESET);
2030 tp = intotcpcb(inp);
2032 memset(ptr, 0, mem_sz);
2033 cc_mem.ccvc.tcp = tp;
2035 * We once again hold a write lock over the tcb so it's
2036 * safe to do these things without ordering concerns.
2037 * Note here we init into stack memory.
2039 if (algo->cb_init != NULL)
2040 error = algo->cb_init(&cc_mem, ptr);
2044 * The CC algorithms, when given their memory
2045 * should not fail we could in theory have a
2050 * Touchdown, lets go ahead and move the
2051 * connection to the new CC module by
2052 * copying in the cc_mem after we call
2053 * the old ones cleanup (if any).
2055 if (CC_ALGO(tp)->cb_destroy != NULL)
2056 CC_ALGO(tp)->cb_destroy(tp->ccv);
2057 /* Detach the old CC from the tcpcb */
2059 /* Copy in our temp memory that was inited */
2060 memcpy(tp->ccv, &cc_mem, sizeof(struct cc_var));
2061 /* Now attach the new, which takes a reference */
2062 cc_attach(tp, algo);
2063 /* Ok now are we where we have gotten past any conn_init? */
2064 if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
2065 /* Yep run the connection init for the new CC */
2066 CC_ALGO(tp)->conn_init(tp->ccv);
2069 free(ptr, M_CC_MEM);
2071 /* Now lets release our temp reference */
2079 tcp_default_ctloutput(struct inpcb *inp, struct sockopt *sopt)
2081 struct tcpcb *tp = intotcpcb(inp);
2082 int error, opt, optval;
2086 struct tls_enable tls;
2087 struct socket *so = inp->inp_socket;
2089 char *pbuf, buf[TCP_LOG_ID_LEN];
2091 struct statsblob *sbp;
2095 INP_WLOCK_ASSERT(inp);
2096 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
2097 ("inp_flags == %x", inp->inp_flags));
2098 KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
2100 switch (sopt->sopt_level) {
2103 MPASS(inp->inp_vflag & INP_IPV6PROTO);
2104 switch (sopt->sopt_name) {
2105 case IPV6_USE_MIN_MTU:
2106 tcp6_use_min_mtu(tp);
2120 * For TCP_CCALGOOPT forward the control to CC module, for both
2121 * SOPT_SET and SOPT_GET.
2123 switch (sopt->sopt_name) {
2126 if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2128 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2129 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2130 sopt->sopt_valsize);
2135 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2136 if (CC_ALGO(tp)->ctl_output != NULL)
2137 error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
2141 if (error == 0 && sopt->sopt_dir == SOPT_GET)
2142 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2147 switch (sopt->sopt_dir) {
2149 switch (sopt->sopt_name) {
2150 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2153 if (!TCPMD5_ENABLED())
2154 return (ENOPROTOOPT);
2155 error = TCPMD5_PCBCTL(inp, sopt);
2158 INP_WLOCK_RECHECK(inp);
2159 goto unlock_and_done;
2166 error = sooptcopyin(sopt, &optval, sizeof optval,
2171 INP_WLOCK_RECHECK(inp);
2172 switch (sopt->sopt_name) {
2183 opt = 0; /* dead code to fool gcc */
2190 tp->t_flags &= ~opt;
2193 if (tp->t_flags & TF_TOE) {
2194 tcp_offload_ctloutput(tp, sopt->sopt_dir,
2203 error = sooptcopyin(sopt, &optval, sizeof optval,
2208 INP_WLOCK_RECHECK(inp);
2210 tp->t_flags |= TF_NOPUSH;
2211 else if (tp->t_flags & TF_NOPUSH) {
2212 tp->t_flags &= ~TF_NOPUSH;
2213 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2214 struct epoch_tracker et;
2216 NET_EPOCH_ENTER(et);
2217 error = tcp_output_nodrop(tp);
2221 goto unlock_and_done;
2223 case TCP_REMOTE_UDP_ENCAPS_PORT:
2225 error = sooptcopyin(sopt, &optval, sizeof optval,
2229 if ((optval < TCP_TUNNELING_PORT_MIN) ||
2230 (optval > TCP_TUNNELING_PORT_MAX)) {
2231 /* Its got to be in range */
2234 if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
2235 /* You have to have enabled a UDP tunneling port first */
2238 INP_WLOCK_RECHECK(inp);
2239 if (tp->t_state != TCPS_CLOSED) {
2240 /* You can't change after you are connected */
2243 /* Ok we are all good set the port */
2244 tp->t_port = htons(optval);
2246 goto unlock_and_done;
2250 error = sooptcopyin(sopt, &optval, sizeof optval,
2255 INP_WLOCK_RECHECK(inp);
2256 if (optval > 0 && optval <= tp->t_maxseg &&
2257 optval + 40 >= V_tcp_minmss)
2258 tp->t_maxseg = optval;
2261 goto unlock_and_done;
2271 error = sooptcopyin(sopt, &optval, sizeof optval,
2277 sbp = stats_blob_alloc(
2278 V_tcp_perconn_stats_dflt_tpl, 0);
2282 INP_WLOCK_RECHECK(inp);
2283 if ((tp->t_stats != NULL && sbp == NULL) ||
2284 (tp->t_stats == NULL && sbp != NULL)) {
2285 struct statsblob *t = tp->t_stats;
2291 stats_blob_destroy(sbp);
2293 return (EOPNOTSUPP);
2297 case TCP_CONGESTION:
2298 error = tcp_set_cc_mod(inp, sopt);
2301 case TCP_REUSPORT_LB_NUMA:
2303 error = sooptcopyin(sopt, &optval, sizeof(optval),
2305 INP_WLOCK_RECHECK(inp);
2307 error = in_pcblbgroup_numa(inp, optval);
2312 case TCP_TXTLS_ENABLE:
2314 error = copyin_tls_enable(sopt, &tls);
2317 error = ktls_enable_tx(so, &tls);
2319 case TCP_TXTLS_MODE:
2321 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2325 INP_WLOCK_RECHECK(inp);
2326 error = ktls_set_tx_mode(so, ui);
2329 case TCP_RXTLS_ENABLE:
2331 error = sooptcopyin(sopt, &tls, sizeof(tls),
2335 error = ktls_enable_rx(so, &tls);
2338 case TCP_MAXUNACKTIME:
2343 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2347 if (ui > (UINT_MAX / hz)) {
2353 INP_WLOCK_RECHECK(inp);
2354 switch (sopt->sopt_name) {
2355 case TCP_MAXUNACKTIME:
2356 tp->t_maxunacktime = ui;
2360 tp->t_keepidle = ui;
2362 * XXX: better check current remaining
2363 * timeout and "merge" it with new value.
2365 if ((tp->t_state > TCPS_LISTEN) &&
2366 (tp->t_state <= TCPS_CLOSING))
2367 tcp_timer_activate(tp, TT_KEEP,
2371 tp->t_keepintvl = ui;
2372 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2373 (TP_MAXIDLE(tp) > 0))
2374 tcp_timer_activate(tp, TT_2MSL,
2378 tp->t_keepinit = ui;
2379 if (tp->t_state == TCPS_SYN_RECEIVED ||
2380 tp->t_state == TCPS_SYN_SENT)
2381 tcp_timer_activate(tp, TT_KEEP,
2385 goto unlock_and_done;
2389 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2393 INP_WLOCK_RECHECK(inp);
2395 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2396 (TP_MAXIDLE(tp) > 0))
2397 tcp_timer_activate(tp, TT_2MSL,
2399 goto unlock_and_done;
2405 error = sooptcopyin(sopt, &optval, sizeof optval,
2410 INP_WLOCK_RECHECK(inp);
2412 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
2413 &(tp->t_outpkts) : &(tp->t_inpkts),
2417 goto unlock_and_done;
2420 case TCP_FASTOPEN: {
2421 struct tcp_fastopen tfo_optval;
2424 if (!V_tcp_fastopen_client_enable &&
2425 !V_tcp_fastopen_server_enable)
2428 error = sooptcopyin(sopt, &tfo_optval,
2429 sizeof(tfo_optval), sizeof(int));
2433 INP_WLOCK_RECHECK(inp);
2434 if ((tp->t_state != TCPS_CLOSED) &&
2435 (tp->t_state != TCPS_LISTEN)) {
2437 goto unlock_and_done;
2439 if (tfo_optval.enable) {
2440 if (tp->t_state == TCPS_LISTEN) {
2441 if (!V_tcp_fastopen_server_enable) {
2443 goto unlock_and_done;
2446 if (tp->t_tfo_pending == NULL)
2448 tcp_fastopen_alloc_counter();
2451 * If a pre-shared key was provided,
2452 * stash it in the client cookie
2453 * field of the tcpcb for use during
2456 if (sopt->sopt_valsize ==
2457 sizeof(tfo_optval)) {
2458 memcpy(tp->t_tfo_cookie.client,
2460 TCP_FASTOPEN_PSK_LEN);
2461 tp->t_tfo_client_cookie_len =
2462 TCP_FASTOPEN_PSK_LEN;
2465 tp->t_flags |= TF_FASTOPEN;
2467 tp->t_flags &= ~TF_FASTOPEN;
2468 goto unlock_and_done;
2474 error = sooptcopyin(sopt, &optval, sizeof optval,
2479 INP_WLOCK_RECHECK(inp);
2480 error = tcp_log_state_change(tp, optval);
2481 goto unlock_and_done;
2490 error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
2493 buf[sopt->sopt_valsize] = '\0';
2494 INP_WLOCK_RECHECK(inp);
2495 error = tcp_log_set_id(tp, buf);
2496 /* tcp_log_set_id() unlocks the INP. */
2503 sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
2506 buf[sopt->sopt_valsize] = '\0';
2507 INP_WLOCK_RECHECK(inp);
2508 if (sopt->sopt_name == TCP_LOGDUMP) {
2509 error = tcp_log_dump_tp_logbuf(tp, buf,
2513 tcp_log_dump_tp_bucket_logbufs(tp, buf);
2515 * tcp_log_dump_tp_bucket_logbufs() drops the
2524 error = ENOPROTOOPT;
2530 tp = intotcpcb(inp);
2531 switch (sopt->sopt_name) {
2532 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2535 if (!TCPMD5_ENABLED())
2536 return (ENOPROTOOPT);
2537 error = TCPMD5_PCBCTL(inp, sopt);
2542 optval = tp->t_flags & TF_NODELAY;
2544 error = sooptcopyout(sopt, &optval, sizeof optval);
2547 optval = tp->t_maxseg;
2549 error = sooptcopyout(sopt, &optval, sizeof optval);
2551 case TCP_REMOTE_UDP_ENCAPS_PORT:
2552 optval = ntohs(tp->t_port);
2554 error = sooptcopyout(sopt, &optval, sizeof optval);
2557 optval = tp->t_flags & TF_NOOPT;
2559 error = sooptcopyout(sopt, &optval, sizeof optval);
2562 optval = tp->t_flags & TF_NOPUSH;
2564 error = sooptcopyout(sopt, &optval, sizeof optval);
2567 tcp_fill_info(tp, &ti);
2569 error = sooptcopyout(sopt, &ti, sizeof ti);
2575 TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2578 socklen_t outsbsz = sopt->sopt_valsize;
2579 if (tp->t_stats == NULL)
2581 else if (outsbsz >= tp->t_stats->cursz)
2582 outsbsz = tp->t_stats->cursz;
2583 else if (outsbsz >= sizeof(struct statsblob))
2584 outsbsz = sizeof(struct statsblob);
2591 sbp = sopt->sopt_val;
2592 nheld = atop(round_page(((vm_offset_t)sbp) +
2593 (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2594 vm_page_t ma[nheld];
2595 if (vm_fault_quick_hold_pages(
2596 &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2597 outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2603 if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2604 SIZEOF_MEMBER(struct statsblob, flags))))
2607 INP_WLOCK_RECHECK(inp);
2608 error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2609 sbflags | SB_CLONE_USRDSTNOFAULT);
2611 sopt->sopt_valsize = outsbsz;
2613 vm_page_unhold_pages(ma, nheld);
2620 case TCP_CONGESTION:
2621 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2623 error = sooptcopyout(sopt, buf, len + 1);
2625 case TCP_MAXUNACKTIME:
2630 switch (sopt->sopt_name) {
2631 case TCP_MAXUNACKTIME:
2632 ui = TP_MAXUNACKTIME(tp) / hz;
2635 ui = TP_KEEPIDLE(tp) / hz;
2638 ui = TP_KEEPINTVL(tp) / hz;
2641 ui = TP_KEEPINIT(tp) / hz;
2644 ui = TP_KEEPCNT(tp);
2648 error = sooptcopyout(sopt, &ui, sizeof(ui));
2653 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
2654 &(tp->t_outpkts) : &(tp->t_inpkts));
2656 error = sooptcopyout(sopt, &optval, sizeof optval);
2660 optval = tp->t_flags & TF_FASTOPEN;
2662 error = sooptcopyout(sopt, &optval, sizeof optval);
2666 optval = tp->t_logstate;
2668 error = sooptcopyout(sopt, &optval, sizeof(optval));
2671 /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2672 error = tcp_log_getlogbuf(sopt, tp);
2675 len = tcp_log_get_id(tp, buf);
2677 error = sooptcopyout(sopt, buf, len + 1);
2686 case TCP_TXTLS_MODE:
2687 error = ktls_get_tx_mode(so, &optval);
2690 error = sooptcopyout(sopt, &optval,
2693 case TCP_RXTLS_MODE:
2694 error = ktls_get_rx_mode(so, &optval);
2697 error = sooptcopyout(sopt, &optval,
2702 optval = tp->t_flags & TF_LRD;
2704 error = sooptcopyout(sopt, &optval, sizeof optval);
2708 error = ENOPROTOOPT;
2715 #undef INP_WLOCK_RECHECK
2716 #undef INP_WLOCK_RECHECK_CLEANUP
2719 * Initiate (or continue) disconnect.
2720 * If embryonic state, just send reset (once).
2721 * If in ``let data drain'' option and linger null, just drop.
2722 * Otherwise (hard), mark socket disconnecting and drop
2723 * current input data; switch states based on user close, and
2724 * send segment to peer (with FIN).
2727 tcp_disconnect(struct tcpcb *tp)
2729 struct inpcb *inp = tptoinpcb(tp);
2730 struct socket *so = tptosocket(tp);
2733 INP_WLOCK_ASSERT(inp);
2736 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2737 * socket is still open.
2739 if (tp->t_state < TCPS_ESTABLISHED &&
2740 !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2743 ("tcp_disconnect: tcp_close() returned NULL"));
2744 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2745 tp = tcp_drop(tp, 0);
2747 ("tcp_disconnect: tcp_drop() returned NULL"));
2749 soisdisconnecting(so);
2750 sbflush(&so->so_rcv);
2752 if (!(inp->inp_flags & INP_DROPPED))
2753 /* Ignore stack's drop request, we already at it. */
2754 (void)tcp_output_nodrop(tp);
2759 * User issued close, and wish to trail through shutdown states:
2760 * if never received SYN, just forget it. If got a SYN from peer,
2761 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2762 * If already got a FIN from peer, then almost done; go to LAST_ACK
2763 * state. In all other cases, have already sent FIN to peer (e.g.
2764 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2765 * for peer to send FIN or not respond to keep-alives, etc.
2766 * We can let the user exit from the close as soon as the FIN is acked.
2769 tcp_usrclosed(struct tcpcb *tp)
2773 INP_WLOCK_ASSERT(tptoinpcb(tp));
2775 switch (tp->t_state) {
2778 tcp_offload_listen_stop(tp);
2780 tcp_state_change(tp, TCPS_CLOSED);
2785 * tcp_close() should never return NULL here as the socket is
2789 ("tcp_usrclosed: tcp_close() returned NULL"));
2793 case TCPS_SYN_RECEIVED:
2794 tp->t_flags |= TF_NEEDFIN;
2797 case TCPS_ESTABLISHED:
2798 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2801 case TCPS_CLOSE_WAIT:
2802 tcp_state_change(tp, TCPS_LAST_ACK);
2805 if (tp->t_acktime == 0)
2806 tp->t_acktime = ticks;
2807 if (tp->t_state >= TCPS_FIN_WAIT_2) {
2808 soisdisconnected(tptosocket(tp));
2809 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
2810 if (tp->t_state == TCPS_FIN_WAIT_2) {
2813 timeout = (tcp_fast_finwait2_recycle) ?
2814 tcp_finwait2_timeout : TP_MAXIDLE(tp);
2815 tcp_timer_activate(tp, TT_2MSL, timeout);
2822 db_print_indent(int indent)
2826 for (i = 0; i < indent; i++)
2831 db_print_tstate(int t_state)
2836 db_printf("TCPS_CLOSED");
2840 db_printf("TCPS_LISTEN");
2844 db_printf("TCPS_SYN_SENT");
2847 case TCPS_SYN_RECEIVED:
2848 db_printf("TCPS_SYN_RECEIVED");
2851 case TCPS_ESTABLISHED:
2852 db_printf("TCPS_ESTABLISHED");
2855 case TCPS_CLOSE_WAIT:
2856 db_printf("TCPS_CLOSE_WAIT");
2859 case TCPS_FIN_WAIT_1:
2860 db_printf("TCPS_FIN_WAIT_1");
2864 db_printf("TCPS_CLOSING");
2868 db_printf("TCPS_LAST_ACK");
2871 case TCPS_FIN_WAIT_2:
2872 db_printf("TCPS_FIN_WAIT_2");
2875 case TCPS_TIME_WAIT:
2876 db_printf("TCPS_TIME_WAIT");
2880 db_printf("unknown");
2886 db_print_tflags(u_int t_flags)
2891 if (t_flags & TF_ACKNOW) {
2892 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2895 if (t_flags & TF_DELACK) {
2896 db_printf("%sTF_DELACK", comma ? ", " : "");
2899 if (t_flags & TF_NODELAY) {
2900 db_printf("%sTF_NODELAY", comma ? ", " : "");
2903 if (t_flags & TF_NOOPT) {
2904 db_printf("%sTF_NOOPT", comma ? ", " : "");
2907 if (t_flags & TF_SENTFIN) {
2908 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2911 if (t_flags & TF_REQ_SCALE) {
2912 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2915 if (t_flags & TF_RCVD_SCALE) {
2916 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2919 if (t_flags & TF_REQ_TSTMP) {
2920 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2923 if (t_flags & TF_RCVD_TSTMP) {
2924 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2927 if (t_flags & TF_SACK_PERMIT) {
2928 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2931 if (t_flags & TF_NEEDSYN) {
2932 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2935 if (t_flags & TF_NEEDFIN) {
2936 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2939 if (t_flags & TF_NOPUSH) {
2940 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2943 if (t_flags & TF_PREVVALID) {
2944 db_printf("%sTF_PREVVALID", comma ? ", " : "");
2947 if (t_flags & TF_MORETOCOME) {
2948 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2951 if (t_flags & TF_SONOTCONN) {
2952 db_printf("%sTF_SONOTCONN", comma ? ", " : "");
2955 if (t_flags & TF_LASTIDLE) {
2956 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2959 if (t_flags & TF_RXWIN0SENT) {
2960 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2963 if (t_flags & TF_FASTRECOVERY) {
2964 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2967 if (t_flags & TF_CONGRECOVERY) {
2968 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2971 if (t_flags & TF_WASFRECOVERY) {
2972 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2975 if (t_flags & TF_WASCRECOVERY) {
2976 db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
2979 if (t_flags & TF_SIGNATURE) {
2980 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2983 if (t_flags & TF_FORCEDATA) {
2984 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2987 if (t_flags & TF_TSO) {
2988 db_printf("%sTF_TSO", comma ? ", " : "");
2991 if (t_flags & TF_FASTOPEN) {
2992 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2998 db_print_tflags2(u_int t_flags2)
3003 if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
3004 db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
3007 if (t_flags2 & TF2_PLPMTU_PMTUD) {
3008 db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
3011 if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
3012 db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
3015 if (t_flags2 & TF2_LOG_AUTO) {
3016 db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
3019 if (t_flags2 & TF2_DROP_AF_DATA) {
3020 db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
3023 if (t_flags2 & TF2_ECN_PERMIT) {
3024 db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
3027 if (t_flags2 & TF2_ECN_SND_CWR) {
3028 db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
3031 if (t_flags2 & TF2_ECN_SND_ECE) {
3032 db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
3035 if (t_flags2 & TF2_ACE_PERMIT) {
3036 db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
3039 if (t_flags2 & TF2_FBYTES_COMPLETE) {
3040 db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
3046 db_print_toobflags(char t_oobflags)
3051 if (t_oobflags & TCPOOB_HAVEDATA) {
3052 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
3055 if (t_oobflags & TCPOOB_HADDATA) {
3056 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
3062 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
3065 db_print_indent(indent);
3066 db_printf("%s at %p\n", name, tp);
3070 db_print_indent(indent);
3071 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
3072 TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
3074 db_print_indent(indent);
3075 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
3076 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
3078 db_print_indent(indent);
3079 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
3080 &tp->t_timers->tt_delack, tp->t_inpcb);
3082 db_print_indent(indent);
3083 db_printf("t_state: %d (", tp->t_state);
3084 db_print_tstate(tp->t_state);
3087 db_print_indent(indent);
3088 db_printf("t_flags: 0x%x (", tp->t_flags);
3089 db_print_tflags(tp->t_flags);
3092 db_print_indent(indent);
3093 db_printf("t_flags2: 0x%x (", tp->t_flags2);
3094 db_print_tflags2(tp->t_flags2);
3097 db_print_indent(indent);
3098 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
3099 tp->snd_una, tp->snd_max, tp->snd_nxt);
3101 db_print_indent(indent);
3102 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
3103 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
3105 db_print_indent(indent);
3106 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
3107 tp->iss, tp->irs, tp->rcv_nxt);
3109 db_print_indent(indent);
3110 db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n",
3111 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
3113 db_print_indent(indent);
3114 db_printf("snd_wnd: %u snd_cwnd: %u\n",
3115 tp->snd_wnd, tp->snd_cwnd);
3117 db_print_indent(indent);
3118 db_printf("snd_ssthresh: %u snd_recover: "
3119 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3121 db_print_indent(indent);
3122 db_printf("t_rcvtime: %u t_startime: %u\n",
3123 tp->t_rcvtime, tp->t_starttime);
3125 db_print_indent(indent);
3126 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
3127 tp->t_rtttime, tp->t_rtseq);
3129 db_print_indent(indent);
3130 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
3131 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3133 db_print_indent(indent);
3134 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u\n",
3135 tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin);
3137 db_print_indent(indent);
3138 db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n",
3139 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3141 db_print_indent(indent);
3142 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3143 db_print_toobflags(tp->t_oobflags);
3144 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
3146 db_print_indent(indent);
3147 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
3148 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3150 db_print_indent(indent);
3151 db_printf("ts_recent: %u ts_recent_age: %u\n",
3152 tp->ts_recent, tp->ts_recent_age);
3154 db_print_indent(indent);
3155 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
3156 "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3158 db_print_indent(indent);
3159 db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x "
3160 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3161 tp->snd_recover_prev, tp->t_badrxtwin);
3163 db_print_indent(indent);
3164 db_printf("snd_numholes: %d snd_holes first: %p\n",
3165 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3167 db_print_indent(indent);
3168 db_printf("snd_fack: 0x%08x rcv_numsacks: %d\n",
3169 tp->snd_fack, tp->rcv_numsacks);
3171 /* Skip sackblks, sackhint. */
3173 db_print_indent(indent);
3174 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
3175 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3178 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3183 db_printf("usage: show tcpcb <addr>\n");
3186 tp = (struct tcpcb *)addr;
3188 db_print_tcpcb(tp, "tcpcb", 0);