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"
49 #include <sys/param.h>
50 #include <sys/systm.h>
52 #include <sys/limits.h>
53 #include <sys/malloc.h>
54 #include <sys/refcount.h>
55 #include <sys/kernel.h>
57 #include <sys/qmath.h>
58 #include <sys/sysctl.h>
61 #include <sys/domain.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/protosw.h>
68 #include <sys/stats.h>
75 #include <net/if_var.h>
76 #include <net/route.h>
79 #include <netinet/in.h>
80 #include <netinet/in_kdtrace.h>
81 #include <netinet/in_pcb.h>
82 #include <netinet/in_systm.h>
83 #include <netinet/in_var.h>
84 #include <netinet/ip.h>
85 #include <netinet/ip_var.h>
87 #include <netinet/ip6.h>
88 #include <netinet6/in6_pcb.h>
89 #include <netinet6/ip6_var.h>
90 #include <netinet6/scope6_var.h>
92 #include <netinet/tcp.h>
93 #include <netinet/tcp_fsm.h>
94 #include <netinet/tcp_seq.h>
95 #include <netinet/tcp_timer.h>
96 #include <netinet/tcp_var.h>
97 #include <netinet/tcp_log_buf.h>
98 #include <netinet/tcpip.h>
99 #include <netinet/cc/cc.h>
100 #include <netinet/tcp_fastopen.h>
101 #include <netinet/tcp_hpts.h>
103 #include <netinet/tcp_pcap.h>
106 #include <netinet/tcp_offload.h>
108 #include <netipsec/ipsec_support.h>
111 #include <vm/vm_param.h>
113 #include <vm/vm_extern.h>
114 #include <vm/vm_map.h>
115 #include <vm/vm_page.h>
118 * TCP protocol interface to socket abstraction.
121 static int tcp_connect(struct tcpcb *, struct sockaddr_in *,
125 static int tcp6_connect(struct tcpcb *, struct sockaddr_in6 *,
128 static void tcp_disconnect(struct tcpcb *);
129 static void tcp_usrclosed(struct tcpcb *);
130 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
132 static int tcp_pru_options_support(struct tcpcb *tp, int flags);
135 tcp_bblog_pru(struct tcpcb *tp, uint32_t pru, int error)
137 struct tcp_log_buffer *lgb;
139 KASSERT(tp != NULL, ("tcp_bblog_pru: tp == NULL"));
140 INP_WLOCK_ASSERT(tptoinpcb(tp));
141 if (tcp_bblogging_on(tp)) {
142 lgb = tcp_log_event(tp, NULL, NULL, NULL, TCP_LOG_PRU, error,
143 0, NULL, false, NULL, NULL, 0, NULL);
149 lgb->tlb_errno = (uint32_t)error;
151 lgb->tlb_flex1 = pru;
156 * TCP attaches to socket via pru_attach(), reserving space,
157 * and an internet control block.
160 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
163 struct tcpcb *tp = NULL;
167 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
169 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
173 so->so_rcv.sb_flags |= SB_AUTOSIZE;
174 so->so_snd.sb_flags |= SB_AUTOSIZE;
175 error = in_pcballoc(so, &V_tcbinfo);
179 tp = tcp_newtcpcb(inp);
186 tp->t_state = TCPS_CLOSED;
187 tcp_bblog_pru(tp, PRU_ATTACH, error);
189 TCPSTATES_INC(TCPS_CLOSED);
191 TCP_PROBE2(debug__user, tp, PRU_ATTACH);
196 * tcp_usr_detach is called when the socket layer loses its final reference
197 * to the socket, be it a file descriptor reference, a reference from TCP,
198 * etc. At this point, there is only one case in which we will keep around
199 * inpcb state: time wait.
202 tcp_usr_detach(struct socket *so)
208 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
210 KASSERT(so->so_pcb == inp && inp->inp_socket == so,
211 ("%s: socket %p inp %p mismatch", __func__, so, inp));
215 KASSERT(inp->inp_flags & INP_DROPPED ||
216 tp->t_state < TCPS_SYN_SENT,
217 ("%s: inp %p not dropped or embryonic", __func__, inp));
226 * Give the socket an address.
229 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
234 struct sockaddr_in *sinp;
237 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
239 if (inp->inp_flags & INP_DROPPED) {
245 sinp = (struct sockaddr_in *)nam;
246 if (nam->sa_family != AF_INET) {
248 * Preserve compatibility with old programs.
250 if (nam->sa_family != AF_UNSPEC ||
251 nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
252 sinp->sin_addr.s_addr != INADDR_ANY) {
253 error = EAFNOSUPPORT;
256 nam->sa_family = AF_INET;
258 if (nam->sa_len != sizeof(*sinp)) {
263 * Must check for multicast addresses and disallow binding
266 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
267 error = EAFNOSUPPORT;
270 INP_HASH_WLOCK(&V_tcbinfo);
271 error = in_pcbbind(inp, sinp, td->td_ucred);
272 INP_HASH_WUNLOCK(&V_tcbinfo);
274 tcp_bblog_pru(tp, PRU_BIND, error);
275 TCP_PROBE2(debug__user, tp, PRU_BIND);
284 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
289 struct sockaddr_in6 *sin6;
293 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
295 if (inp->inp_flags & INP_DROPPED) {
301 vflagsav = inp->inp_vflag;
303 sin6 = (struct sockaddr_in6 *)nam;
304 if (nam->sa_family != AF_INET6) {
305 error = EAFNOSUPPORT;
308 if (nam->sa_len != sizeof(*sin6)) {
313 * Must check for multicast addresses and disallow binding
316 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
317 error = EAFNOSUPPORT;
321 INP_HASH_WLOCK(&V_tcbinfo);
322 inp->inp_vflag &= ~INP_IPV4;
323 inp->inp_vflag |= INP_IPV6;
325 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
326 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
327 inp->inp_vflag |= INP_IPV4;
328 else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
329 struct sockaddr_in sin;
331 in6_sin6_2_sin(&sin, sin6);
332 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
333 error = EAFNOSUPPORT;
334 INP_HASH_WUNLOCK(&V_tcbinfo);
337 inp->inp_vflag |= INP_IPV4;
338 inp->inp_vflag &= ~INP_IPV6;
339 error = in_pcbbind(inp, &sin, td->td_ucred);
340 INP_HASH_WUNLOCK(&V_tcbinfo);
345 error = in6_pcbbind(inp, sin6, td->td_ucred);
346 INP_HASH_WUNLOCK(&V_tcbinfo);
349 inp->inp_vflag = vflagsav;
350 tcp_bblog_pru(tp, PRU_BIND, error);
351 TCP_PROBE2(debug__user, tp, PRU_BIND);
359 * Prepare to accept connections.
362 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
369 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
371 if (inp->inp_flags & INP_DROPPED) {
378 error = solisten_proto_check(so);
383 if (inp->inp_lport == 0) {
384 INP_HASH_WLOCK(&V_tcbinfo);
385 error = in_pcbbind(inp, NULL, td->td_ucred);
386 INP_HASH_WUNLOCK(&V_tcbinfo);
389 tcp_state_change(tp, TCPS_LISTEN);
390 solisten_proto(so, backlog);
392 if ((so->so_options & SO_NO_OFFLOAD) == 0)
393 tcp_offload_listen_start(tp);
396 solisten_proto_abort(so);
400 if (IS_FASTOPEN(tp->t_flags))
401 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
404 tcp_bblog_pru(tp, PRU_LISTEN, error);
405 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
413 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
421 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
423 if (inp->inp_flags & INP_DROPPED) {
429 vflagsav = inp->inp_vflag;
432 error = solisten_proto_check(so);
437 INP_HASH_WLOCK(&V_tcbinfo);
438 if (inp->inp_lport == 0) {
439 inp->inp_vflag &= ~INP_IPV4;
440 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
441 inp->inp_vflag |= INP_IPV4;
442 error = in6_pcbbind(inp, NULL, td->td_ucred);
444 INP_HASH_WUNLOCK(&V_tcbinfo);
446 tcp_state_change(tp, TCPS_LISTEN);
447 solisten_proto(so, backlog);
449 if ((so->so_options & SO_NO_OFFLOAD) == 0)
450 tcp_offload_listen_start(tp);
453 solisten_proto_abort(so);
457 if (IS_FASTOPEN(tp->t_flags))
458 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
461 inp->inp_vflag = vflagsav;
464 tcp_bblog_pru(tp, PRU_LISTEN, error);
465 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
473 * Initiate connection to peer.
474 * Create a template for use in transmissions on this connection.
475 * Enter SYN_SENT state, and mark socket as connecting.
476 * Start keep-alive timer, and seed output sequence space.
477 * Send initial segment on connection.
480 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
482 struct epoch_tracker et;
486 struct sockaddr_in *sinp;
489 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
491 if (inp->inp_flags & INP_DROPPED) {
493 return (ECONNREFUSED);
497 sinp = (struct sockaddr_in *)nam;
498 if (nam->sa_family != AF_INET) {
499 error = EAFNOSUPPORT;
502 if (nam->sa_len != sizeof (*sinp)) {
507 * Must disallow TCP ``connections'' to multicast addresses.
509 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
510 error = EAFNOSUPPORT;
513 if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
517 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
519 if (SOLISTENING(so)) {
524 if ((error = tcp_connect(tp, sinp, td)) != 0)
527 if (registered_toedevs > 0 &&
528 (so->so_options & SO_NO_OFFLOAD) == 0 &&
529 (error = tcp_offload_connect(so, nam)) == 0)
532 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
533 error = tcp_output(tp);
534 KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
535 ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
539 tcp_bblog_pru(tp, PRU_CONNECT, error);
540 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
548 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
550 struct epoch_tracker et;
554 struct sockaddr_in6 *sin6;
559 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
561 if (inp->inp_flags & INP_DROPPED) {
563 return (ECONNREFUSED);
567 vflagsav = inp->inp_vflag;
568 incflagsav = inp->inp_inc.inc_flags;
570 sin6 = (struct sockaddr_in6 *)nam;
571 if (nam->sa_family != AF_INET6) {
572 error = EAFNOSUPPORT;
575 if (nam->sa_len != sizeof (*sin6)) {
580 * Must disallow TCP ``connections'' to multicast addresses.
582 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
583 error = EAFNOSUPPORT;
586 if (SOLISTENING(so)) {
592 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
593 * therefore probably require the hash lock, which isn't held here.
594 * Is this a significant problem?
596 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
597 struct sockaddr_in sin;
599 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
603 if ((inp->inp_vflag & INP_IPV4) == 0) {
604 error = EAFNOSUPPORT;
608 in6_sin6_2_sin(&sin, sin6);
609 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
610 error = EAFNOSUPPORT;
613 if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
617 if ((error = prison_remote_ip4(td->td_ucred,
618 &sin.sin_addr)) != 0)
620 inp->inp_vflag |= INP_IPV4;
621 inp->inp_vflag &= ~INP_IPV6;
623 if ((error = tcp_connect(tp, &sin, td)) != 0)
626 if (registered_toedevs > 0 &&
627 (so->so_options & SO_NO_OFFLOAD) == 0 &&
628 (error = tcp_offload_connect(so, nam)) == 0)
631 error = tcp_output(tp);
634 if ((inp->inp_vflag & INP_IPV6) == 0) {
635 error = EAFNOSUPPORT;
640 if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
642 inp->inp_vflag &= ~INP_IPV4;
643 inp->inp_vflag |= INP_IPV6;
644 inp->inp_inc.inc_flags |= INC_ISIPV6;
646 if ((error = tcp6_connect(tp, sin6, td)) != 0)
649 if (registered_toedevs > 0 &&
650 (so->so_options & SO_NO_OFFLOAD) == 0 &&
651 (error = tcp_offload_connect(so, nam)) == 0)
654 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
655 error = tcp_output(tp);
659 KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
660 ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
662 * If the implicit bind in the connect call fails, restore
663 * the flags we modified.
665 if (error != 0 && inp->inp_lport == 0) {
666 inp->inp_vflag = vflagsav;
667 inp->inp_inc.inc_flags = incflagsav;
670 tcp_bblog_pru(tp, PRU_CONNECT, error);
671 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
678 * Initiate disconnect from peer.
679 * If connection never passed embryonic stage, just drop;
680 * else if don't need to let data drain, then can just drop anyways,
681 * else have to begin TCP shutdown process: mark socket disconnecting,
682 * drain unread data, state switch to reflect user close, and
683 * send segment (e.g. FIN) to peer. Socket will be really disconnected
684 * when peer sends FIN and acks ours.
686 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
689 tcp_usr_disconnect(struct socket *so)
692 struct tcpcb *tp = NULL;
693 struct epoch_tracker et;
698 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
700 if (inp->inp_flags & INP_DROPPED) {
707 if (tp->t_state == TCPS_TIME_WAIT)
711 tcp_bblog_pru(tp, PRU_DISCONNECT, error);
712 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
720 * Accept a connection. Essentially all the work is done at higher levels;
721 * just return the address of the peer, storing through addr.
724 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
733 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
735 if (inp->inp_flags & INP_DROPPED) {
737 return (ECONNABORTED);
741 if (so->so_state & SS_ISDISCONNECTED) {
742 error = ECONNABORTED;
746 * We inline in_getpeeraddr and COMMON_END here, so that we can
747 * copy the data of interest and defer the malloc until after we
750 port = inp->inp_fport;
751 addr = inp->inp_faddr;
754 tcp_bblog_pru(tp, PRU_ACCEPT, error);
755 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
758 *nam = in_sockaddr(port, &addr);
765 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
771 struct in6_addr addr6;
772 struct epoch_tracker et;
777 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
778 NET_EPOCH_ENTER(et); /* XXXMT Why is this needed? */
780 if (inp->inp_flags & INP_DROPPED) {
783 return (ECONNABORTED);
787 if (so->so_state & SS_ISDISCONNECTED) {
788 error = ECONNABORTED;
792 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
793 * copy the data of interest and defer the malloc until after we
796 if (inp->inp_vflag & INP_IPV4) {
798 port = inp->inp_fport;
799 addr = inp->inp_faddr;
801 port = inp->inp_fport;
802 addr6 = inp->in6p_faddr;
806 tcp_bblog_pru(tp, PRU_ACCEPT, error);
807 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
812 *nam = in6_v4mapsin6_sockaddr(port, &addr);
814 *nam = in6_sockaddr(port, &addr6);
821 * Mark the connection as being incapable of further output.
824 tcp_usr_shutdown(struct socket *so)
829 struct epoch_tracker et;
832 KASSERT(inp != NULL, ("inp == NULL"));
834 if (inp->inp_flags & INP_DROPPED) {
843 if (!(inp->inp_flags & INP_DROPPED))
844 error = tcp_output_nodrop(tp);
845 tcp_bblog_pru(tp, PRU_SHUTDOWN, error);
846 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
847 error = tcp_unlock_or_drop(tp, error);
854 * After a receive, possibly send window update to peer.
857 tcp_usr_rcvd(struct socket *so, int flags)
859 struct epoch_tracker et;
862 int outrv = 0, error = 0;
865 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
867 if (inp->inp_flags & INP_DROPPED) {
875 * For passively-created TFO connections, don't attempt a window
876 * update while still in SYN_RECEIVED as this may trigger an early
877 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
878 * application response data, or failing that, when the DELACK timer
881 if (IS_FASTOPEN(tp->t_flags) &&
882 (tp->t_state == TCPS_SYN_RECEIVED))
885 if (tp->t_flags & TF_TOE)
886 tcp_offload_rcvd(tp);
889 outrv = tcp_output_nodrop(tp);
891 tcp_bblog_pru(tp, PRU_RCVD, error);
892 TCP_PROBE2(debug__user, tp, PRU_RCVD);
893 (void) tcp_unlock_or_drop(tp, outrv);
899 * Do a send by putting data in output queue and updating urgent
900 * marker if URG set. Possibly send more data. Unlike the other
901 * pru_*() routines, the mbuf chains are our responsibility. We
902 * must either enqueue them or free them. The other pru_* routines
903 * generally are caller-frees.
906 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
907 struct sockaddr *nam, struct mbuf *control, struct thread *td)
909 struct epoch_tracker et;
915 struct sockaddr_in sin;
917 struct sockaddr_in *sinp;
920 struct sockaddr_in6 *sin6;
928 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
930 if (inp->inp_flags & INP_DROPPED) {
931 if (m != NULL && (flags & PRUS_NOTREADY) == 0)
938 vflagsav = inp->inp_vflag;
939 incflagsav = inp->inp_inc.inc_flags;
940 restoreflags = false;
943 if (control != NULL) {
944 /* TCP doesn't do control messages (rights, creds, etc) */
945 if (control->m_len > 0) {
950 m_freem(control); /* empty control, just free it */
953 if ((flags & PRUS_OOB) != 0 &&
954 (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
957 if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
958 if (tp->t_state == TCPS_LISTEN) {
962 switch (nam->sa_family) {
965 sinp = (struct sockaddr_in *)nam;
966 if (sinp->sin_len != sizeof(struct sockaddr_in)) {
970 if ((inp->inp_vflag & INP_IPV6) != 0) {
971 error = EAFNOSUPPORT;
974 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
975 error = EAFNOSUPPORT;
978 if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
982 if ((error = prison_remote_ip4(td->td_ucred,
992 sin6 = (struct sockaddr_in6 *)nam;
993 if (sin6->sin6_len != sizeof(*sin6)) {
997 if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
998 error = EAFNOSUPPORT;
1001 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1002 error = EAFNOSUPPORT;
1005 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1007 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1011 if ((inp->inp_vflag & INP_IPV4) == 0) {
1012 error = EAFNOSUPPORT;
1015 restoreflags = true;
1016 inp->inp_vflag &= ~INP_IPV6;
1018 in6_sin6_2_sin(sinp, sin6);
1020 ntohl(sinp->sin_addr.s_addr))) {
1021 error = EAFNOSUPPORT;
1024 if ((error = prison_remote_ip4(td->td_ucred,
1029 error = EAFNOSUPPORT;
1033 if ((inp->inp_vflag & INP_IPV6) == 0) {
1034 error = EAFNOSUPPORT;
1037 restoreflags = true;
1038 inp->inp_vflag &= ~INP_IPV4;
1039 inp->inp_inc.inc_flags |= INC_ISIPV6;
1040 if ((error = prison_remote_ip6(td->td_ucred,
1048 error = EAFNOSUPPORT;
1052 if (!(flags & PRUS_OOB)) {
1053 if (tp->t_acktime == 0)
1054 tp->t_acktime = ticks;
1055 sbappendstream(&so->so_snd, m, flags);
1057 if (nam && tp->t_state < TCPS_SYN_SENT) {
1058 KASSERT(tp->t_state == TCPS_CLOSED,
1059 ("%s: tp %p is listening", __func__, tp));
1062 * Do implied connect if not yet connected,
1063 * initialize window to default value, and
1064 * initialize maxseg using peer's cached MSS.
1068 error = tcp6_connect(tp, sin6, td);
1070 #if defined(INET6) && defined(INET)
1074 error = tcp_connect(tp, sinp, td);
1077 * The bind operation in tcp_connect succeeded. We
1078 * no longer want to restore the flags if later
1081 if (error == 0 || inp->inp_lport != 0)
1082 restoreflags = false;
1085 /* m is freed if PRUS_NOTREADY is unset. */
1086 sbflush(&so->so_snd);
1089 if (IS_FASTOPEN(tp->t_flags))
1090 tcp_fastopen_connect(tp);
1092 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1096 if (flags & PRUS_EOF) {
1098 * Close the send side of the connection after
1104 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1105 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1106 (tp->t_fbyte_out == 0) &&
1107 (so->so_snd.sb_ccc > 0)) {
1108 tp->t_fbyte_out = ticks;
1109 if (tp->t_fbyte_out == 0)
1110 tp->t_fbyte_out = 1;
1111 if (tp->t_fbyte_out && tp->t_fbyte_in)
1112 tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1114 if (!(inp->inp_flags & INP_DROPPED) &&
1115 !(flags & PRUS_NOTREADY)) {
1116 if (flags & PRUS_MORETOCOME)
1117 tp->t_flags |= TF_MORETOCOME;
1118 error = tcp_output_nodrop(tp);
1119 if (flags & PRUS_MORETOCOME)
1120 tp->t_flags &= ~TF_MORETOCOME;
1124 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1126 SOCKBUF_LOCK(&so->so_snd);
1127 if (sbspace(&so->so_snd) < -512) {
1128 SOCKBUF_UNLOCK(&so->so_snd);
1133 * According to RFC961 (Assigned Protocols),
1134 * the urgent pointer points to the last octet
1135 * of urgent data. We continue, however,
1136 * to consider it to indicate the first octet
1137 * of data past the urgent section.
1138 * Otherwise, snd_up should be one lower.
1140 if (tp->t_acktime == 0)
1141 tp->t_acktime = ticks;
1142 sbappendstream_locked(&so->so_snd, m, flags);
1143 SOCKBUF_UNLOCK(&so->so_snd);
1145 if (nam && tp->t_state < TCPS_SYN_SENT) {
1147 * Do implied connect if not yet connected,
1148 * initialize window to default value, and
1149 * initialize maxseg using peer's cached MSS.
1153 * Not going to contemplate SYN|URG
1155 if (IS_FASTOPEN(tp->t_flags))
1156 tp->t_flags &= ~TF_FASTOPEN;
1159 error = tcp6_connect(tp, sin6, td);
1161 #if defined(INET6) && defined(INET)
1165 error = tcp_connect(tp, sinp, td);
1168 * The bind operation in tcp_connect succeeded. We
1169 * no longer want to restore the flags if later
1172 if (error == 0 || inp->inp_lport != 0)
1173 restoreflags = false;
1176 /* m is freed if PRUS_NOTREADY is unset. */
1177 sbflush(&so->so_snd);
1180 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1183 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1184 if ((flags & PRUS_NOTREADY) == 0) {
1185 tp->t_flags |= TF_FORCEDATA;
1186 error = tcp_output_nodrop(tp);
1187 tp->t_flags &= ~TF_FORCEDATA;
1190 TCP_LOG_EVENT(tp, NULL,
1191 &inp->inp_socket->so_rcv,
1192 &inp->inp_socket->so_snd,
1193 TCP_LOG_USERSEND, error,
1198 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
1199 * responsible for freeing memory.
1201 if (m != NULL && (flags & PRUS_NOTREADY) == 0)
1205 * If the request was unsuccessful and we changed flags,
1206 * restore the original flags.
1208 if (error != 0 && restoreflags) {
1209 inp->inp_vflag = vflagsav;
1210 inp->inp_inc.inc_flags = incflagsav;
1212 tcp_bblog_pru(tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1213 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND), error);
1214 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1215 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1216 error = tcp_unlock_or_drop(tp, error);
1222 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1224 struct epoch_tracker et;
1229 inp = sotoinpcb(so);
1231 if (inp->inp_flags & INP_DROPPED) {
1233 mb_free_notready(m, count);
1234 return (ECONNRESET);
1236 tp = intotcpcb(inp);
1238 SOCKBUF_LOCK(&so->so_snd);
1239 error = sbready(&so->so_snd, m, count);
1240 SOCKBUF_UNLOCK(&so->so_snd);
1245 NET_EPOCH_ENTER(et);
1246 error = tcp_output_unlock(tp);
1253 * Abort the TCP. Drop the connection abruptly.
1256 tcp_usr_abort(struct socket *so)
1260 struct epoch_tracker et;
1262 inp = sotoinpcb(so);
1263 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1265 NET_EPOCH_ENTER(et);
1267 KASSERT(inp->inp_socket != NULL,
1268 ("tcp_usr_abort: inp_socket == NULL"));
1271 * If we still have full TCP state, and we're not dropped, drop.
1273 if (!(inp->inp_flags & INP_DROPPED)) {
1274 tp = intotcpcb(inp);
1275 tp = tcp_drop(tp, ECONNABORTED);
1278 tcp_bblog_pru(tp, PRU_ABORT, 0);
1279 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1281 if (!(inp->inp_flags & INP_DROPPED)) {
1283 inp->inp_flags |= INP_SOCKREF;
1291 * TCP socket is closed. Start friendly disconnect.
1294 tcp_usr_close(struct socket *so)
1298 struct epoch_tracker et;
1300 inp = sotoinpcb(so);
1301 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1303 NET_EPOCH_ENTER(et);
1305 KASSERT(inp->inp_socket != NULL,
1306 ("tcp_usr_close: inp_socket == NULL"));
1309 * If we are still connected and we're not dropped, initiate
1312 if (!(inp->inp_flags & INP_DROPPED)) {
1313 tp = intotcpcb(inp);
1314 if (tp->t_state != TCPS_TIME_WAIT) {
1315 tp->t_flags |= TF_CLOSED;
1317 tcp_bblog_pru(tp, PRU_CLOSE, 0);
1318 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1321 if (!(inp->inp_flags & INP_DROPPED)) {
1323 inp->inp_flags |= INP_SOCKREF;
1330 tcp_pru_options_support(struct tcpcb *tp, int flags)
1333 * If the specific TCP stack has a pru_options
1334 * specified then it does not always support
1335 * all the PRU_XX options and we must ask it.
1336 * If the function is not specified then all
1337 * of the PRU_XX options are supported.
1341 if (tp->t_fb->tfb_pru_options) {
1342 ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1348 * Receive out-of-band data.
1351 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1357 inp = sotoinpcb(so);
1358 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1360 if (inp->inp_flags & INP_DROPPED) {
1362 return (ECONNRESET);
1364 tp = intotcpcb(inp);
1366 error = tcp_pru_options_support(tp, PRUS_OOB);
1370 if ((so->so_oobmark == 0 &&
1371 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1372 so->so_options & SO_OOBINLINE ||
1373 tp->t_oobflags & TCPOOB_HADDATA) {
1377 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1378 error = EWOULDBLOCK;
1382 *mtod(m, caddr_t) = tp->t_iobc;
1383 if ((flags & MSG_PEEK) == 0)
1384 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1387 tcp_bblog_pru(tp, PRU_RCVOOB, error);
1388 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1394 struct protosw tcp_protosw = {
1395 .pr_type = SOCK_STREAM,
1396 .pr_protocol = IPPROTO_TCP,
1397 .pr_flags = PR_CONNREQUIRED | PR_IMPLOPCL | PR_WANTRCVD |
1399 .pr_ctloutput = tcp_ctloutput,
1400 .pr_abort = tcp_usr_abort,
1401 .pr_accept = tcp_usr_accept,
1402 .pr_attach = tcp_usr_attach,
1403 .pr_bind = tcp_usr_bind,
1404 .pr_connect = tcp_usr_connect,
1405 .pr_control = in_control,
1406 .pr_detach = tcp_usr_detach,
1407 .pr_disconnect = tcp_usr_disconnect,
1408 .pr_listen = tcp_usr_listen,
1409 .pr_peeraddr = in_getpeeraddr,
1410 .pr_rcvd = tcp_usr_rcvd,
1411 .pr_rcvoob = tcp_usr_rcvoob,
1412 .pr_send = tcp_usr_send,
1413 .pr_ready = tcp_usr_ready,
1414 .pr_shutdown = tcp_usr_shutdown,
1415 .pr_sockaddr = in_getsockaddr,
1416 .pr_sosetlabel = in_pcbsosetlabel,
1417 .pr_close = tcp_usr_close,
1422 struct protosw tcp6_protosw = {
1423 .pr_type = SOCK_STREAM,
1424 .pr_protocol = IPPROTO_TCP,
1425 .pr_flags = PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD |
1427 .pr_ctloutput = tcp_ctloutput,
1428 .pr_abort = tcp_usr_abort,
1429 .pr_accept = tcp6_usr_accept,
1430 .pr_attach = tcp_usr_attach,
1431 .pr_bind = tcp6_usr_bind,
1432 .pr_connect = tcp6_usr_connect,
1433 .pr_control = in6_control,
1434 .pr_detach = tcp_usr_detach,
1435 .pr_disconnect = tcp_usr_disconnect,
1436 .pr_listen = tcp6_usr_listen,
1437 .pr_peeraddr = in6_mapped_peeraddr,
1438 .pr_rcvd = tcp_usr_rcvd,
1439 .pr_rcvoob = tcp_usr_rcvoob,
1440 .pr_send = tcp_usr_send,
1441 .pr_ready = tcp_usr_ready,
1442 .pr_shutdown = tcp_usr_shutdown,
1443 .pr_sockaddr = in6_mapped_sockaddr,
1444 .pr_sosetlabel = in_pcbsosetlabel,
1445 .pr_close = tcp_usr_close,
1451 * Common subroutine to open a TCP connection to remote host specified
1452 * by struct sockaddr_in. Call in_pcbconnect() to choose local host address
1453 * and assign a local port number and install the inpcb into the hash.
1454 * Initialize connection parameters and enter SYN-SENT state.
1457 tcp_connect(struct tcpcb *tp, struct sockaddr_in *sin, struct thread *td)
1459 struct inpcb *inp = tptoinpcb(tp);
1460 struct socket *so = tptosocket(tp);
1464 INP_WLOCK_ASSERT(inp);
1466 if (__predict_false((so->so_state &
1467 (SS_ISCONNECTING | SS_ISCONNECTED | SS_ISDISCONNECTING |
1468 SS_ISDISCONNECTED)) != 0))
1471 INP_HASH_WLOCK(&V_tcbinfo);
1472 error = in_pcbconnect(inp, sin, td->td_ucred, true);
1473 INP_HASH_WUNLOCK(&V_tcbinfo);
1478 * Compute window scaling to request:
1479 * Scale to fit into sweet spot. See tcp_syncache.c.
1480 * XXX: This should move to tcp_output().
1482 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1483 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1484 tp->request_r_scale++;
1487 TCPSTAT_INC(tcps_connattempt);
1488 tcp_state_change(tp, TCPS_SYN_SENT);
1489 tp->iss = tcp_new_isn(&inp->inp_inc);
1490 if (tp->t_flags & TF_REQ_TSTMP)
1491 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1492 tcp_sendseqinit(tp);
1500 tcp6_connect(struct tcpcb *tp, struct sockaddr_in6 *sin6, struct thread *td)
1502 struct inpcb *inp = tptoinpcb(tp);
1503 struct socket *so = tptosocket(tp);
1507 INP_WLOCK_ASSERT(inp);
1509 if (__predict_false((so->so_state &
1510 (SS_ISCONNECTING | SS_ISCONNECTED)) != 0))
1513 INP_HASH_WLOCK(&V_tcbinfo);
1514 error = in6_pcbconnect(inp, sin6, td->td_ucred, true);
1515 INP_HASH_WUNLOCK(&V_tcbinfo);
1519 /* Compute window scaling to request. */
1520 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1521 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1522 tp->request_r_scale++;
1525 TCPSTAT_INC(tcps_connattempt);
1526 tcp_state_change(tp, TCPS_SYN_SENT);
1527 tp->iss = tcp_new_isn(&inp->inp_inc);
1528 if (tp->t_flags & TF_REQ_TSTMP)
1529 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1530 tcp_sendseqinit(tp);
1537 * Export TCP internal state information via a struct tcp_info, based on the
1538 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1539 * (TCP state machine, etc). We export all information using FreeBSD-native
1540 * constants -- for example, the numeric values for tcpi_state will differ
1544 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1547 INP_WLOCK_ASSERT(tptoinpcb(tp));
1548 bzero(ti, sizeof(*ti));
1550 ti->tcpi_state = tp->t_state;
1551 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1552 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1553 if (tp->t_flags & TF_SACK_PERMIT)
1554 ti->tcpi_options |= TCPI_OPT_SACK;
1555 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1556 ti->tcpi_options |= TCPI_OPT_WSCALE;
1557 ti->tcpi_snd_wscale = tp->snd_scale;
1558 ti->tcpi_rcv_wscale = tp->rcv_scale;
1560 switch (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) {
1561 case TF2_ECN_PERMIT:
1562 ti->tcpi_options |= TCPI_OPT_ECN;
1564 case TF2_ACE_PERMIT:
1566 case TF2_ECN_PERMIT | TF2_ACE_PERMIT:
1567 ti->tcpi_options |= TCPI_OPT_ACE;
1572 if (IS_FASTOPEN(tp->t_flags))
1573 ti->tcpi_options |= TCPI_OPT_TFO;
1575 ti->tcpi_rto = tp->t_rxtcur * tick;
1576 ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1577 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1578 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1580 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1581 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1584 * FreeBSD-specific extension fields for tcp_info.
1586 ti->tcpi_rcv_space = tp->rcv_wnd;
1587 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1588 ti->tcpi_snd_wnd = tp->snd_wnd;
1589 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1590 ti->tcpi_snd_nxt = tp->snd_nxt;
1591 ti->tcpi_snd_mss = tp->t_maxseg;
1592 ti->tcpi_rcv_mss = tp->t_maxseg;
1593 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1594 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1595 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1597 if (tp->t_flags & TF_TOE) {
1598 ti->tcpi_options |= TCPI_OPT_TOE;
1599 tcp_offload_tcp_info(tp, ti);
1603 * AccECN related counters.
1605 if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) ==
1606 (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
1608 * Internal counter starts at 5 for AccECN
1609 * but 0 for RFC3168 ECN.
1611 ti->tcpi_delivered_ce = tp->t_scep - 5;
1613 ti->tcpi_delivered_ce = tp->t_scep;
1614 ti->tcpi_received_ce = tp->t_rcep;
1618 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1619 * socket option arguments. When it re-acquires the lock after the copy, it
1620 * has to revalidate that the connection is still valid for the socket
1623 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \
1625 if (inp->inp_flags & INP_DROPPED) { \
1628 return (ECONNRESET); \
1630 tp = intotcpcb(inp); \
1632 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1635 tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt)
1637 struct socket *so = inp->inp_socket;
1638 struct tcpcb *tp = intotcpcb(inp);
1641 MPASS(sopt->sopt_dir == SOPT_SET);
1642 INP_WLOCK_ASSERT(inp);
1643 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1644 ("inp_flags == %x", inp->inp_flags));
1645 KASSERT(so != NULL, ("inp_socket == NULL"));
1647 if (sopt->sopt_level != IPPROTO_TCP) {
1650 if (inp->inp_vflag & INP_IPV6PROTO)
1651 error = ip6_ctloutput(so, sopt);
1653 #if defined(INET6) && defined(INET)
1657 error = ip_ctloutput(so, sopt);
1660 * When an IP-level socket option affects TCP, pass control
1661 * down to stack tfb_tcp_ctloutput, otherwise return what
1662 * IP level returned.
1664 switch (sopt->sopt_level) {
1667 if ((inp->inp_vflag & INP_IPV6PROTO) == 0)
1669 switch (sopt->sopt_name) {
1671 /* Notify tcp stacks that care (e.g. RACK). */
1673 case IPV6_USE_MIN_MTU:
1674 /* Update t_maxseg accordingly. */
1683 switch (sopt->sopt_name) {
1685 inp->inp_ip_tos &= ~IPTOS_ECN_MASK;
1688 /* Notify tcp stacks that care (e.g. RACK). */
1699 if (inp->inp_flags & INP_DROPPED) {
1701 return (ECONNRESET);
1703 } else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1705 * Protect the TCP option TCP_FUNCTION_BLK so
1706 * that a sub-function can *never* overwrite this.
1708 struct tcp_function_set fsn;
1709 struct tcp_function_block *blk;
1713 error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1718 tp = intotcpcb(inp);
1720 blk = find_and_ref_tcp_functions(&fsn);
1725 if (tp->t_fb == blk) {
1726 /* You already have this */
1727 refcount_release(&blk->tfb_refcnt);
1731 if (tp->t_state != TCPS_CLOSED) {
1733 * The user has advanced the state
1734 * past the initial point, we may not
1735 * be able to switch.
1737 if (blk->tfb_tcp_handoff_ok != NULL) {
1739 * Does the stack provide a
1740 * query mechanism, if so it may
1741 * still be possible?
1743 error = (*blk->tfb_tcp_handoff_ok)(tp);
1747 refcount_release(&blk->tfb_refcnt);
1752 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1753 refcount_release(&blk->tfb_refcnt);
1758 * Ensure the new stack takes ownership with a
1759 * clean slate on peak rate threshold.
1762 /* Assure that we are not on any hpts */
1763 tcp_hpts_remove(tp);
1765 if (blk->tfb_tcp_fb_init) {
1766 error = (*blk->tfb_tcp_fb_init)(tp, &ptr);
1769 * Release the ref count the lookup
1772 refcount_release(&blk->tfb_refcnt);
1774 * Now there is a chance that the
1775 * init() function mucked with some
1776 * things before it failed, such as
1777 * hpts or inp_flags2 or timer granularity.
1778 * It should not of, but lets give the old
1779 * stack a chance to reset to a known good state.
1781 if (tp->t_fb->tfb_switch_failed) {
1782 (*tp->t_fb->tfb_switch_failed)(tp);
1787 if (tp->t_fb->tfb_tcp_fb_fini) {
1788 struct epoch_tracker et;
1790 * Tell the stack to cleanup with 0 i.e.
1791 * the tcb is not going away.
1793 NET_EPOCH_ENTER(et);
1794 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1798 * Release the old refcnt, the
1799 * lookup acquired a ref on the
1802 refcount_release(&tp->t_fb->tfb_refcnt);
1804 * Set in the new stack.
1809 if (tp->t_flags & TF_TOE) {
1810 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1820 /* Pass in the INP locked, callee must unlock it. */
1821 return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1825 tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1827 struct socket *so = inp->inp_socket;
1828 struct tcpcb *tp = intotcpcb(inp);
1831 MPASS(sopt->sopt_dir == SOPT_GET);
1832 INP_WLOCK_ASSERT(inp);
1833 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1834 ("inp_flags == %x", inp->inp_flags));
1835 KASSERT(so != NULL, ("inp_socket == NULL"));
1837 if (sopt->sopt_level != IPPROTO_TCP) {
1840 if (inp->inp_vflag & INP_IPV6PROTO)
1841 error = ip6_ctloutput(so, sopt);
1843 #if defined(INET6) && defined(INET)
1847 error = ip_ctloutput(so, sopt);
1851 if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1852 (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1853 struct tcp_function_set fsn;
1855 if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1856 memset(&fsn, 0, sizeof(fsn));
1857 find_tcp_function_alias(tp->t_fb, &fsn);
1859 strncpy(fsn.function_set_name,
1860 tp->t_fb->tfb_tcp_block_name,
1861 TCP_FUNCTION_NAME_LEN_MAX);
1862 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1864 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1866 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1870 /* Pass in the INP locked, callee must unlock it. */
1871 return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1875 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1879 inp = sotoinpcb(so);
1880 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1883 if (inp->inp_flags & INP_DROPPED) {
1885 return (ECONNRESET);
1887 if (sopt->sopt_dir == SOPT_SET)
1888 return (tcp_ctloutput_set(inp, sopt));
1889 else if (sopt->sopt_dir == SOPT_GET)
1890 return (tcp_ctloutput_get(inp, sopt));
1892 panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
1896 * If this assert becomes untrue, we need to change the size of the buf
1897 * variable in tcp_default_ctloutput().
1900 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1901 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1906 copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1908 struct tls_enable_v0 tls_v0;
1911 if (sopt->sopt_valsize == sizeof(tls_v0)) {
1912 error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1916 memset(tls, 0, sizeof(*tls));
1917 tls->cipher_key = tls_v0.cipher_key;
1918 tls->iv = tls_v0.iv;
1919 tls->auth_key = tls_v0.auth_key;
1920 tls->cipher_algorithm = tls_v0.cipher_algorithm;
1921 tls->cipher_key_len = tls_v0.cipher_key_len;
1922 tls->iv_len = tls_v0.iv_len;
1923 tls->auth_algorithm = tls_v0.auth_algorithm;
1924 tls->auth_key_len = tls_v0.auth_key_len;
1925 tls->flags = tls_v0.flags;
1926 tls->tls_vmajor = tls_v0.tls_vmajor;
1927 tls->tls_vminor = tls_v0.tls_vminor;
1931 return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1935 extern struct cc_algo newreno_cc_algo;
1938 tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
1940 struct cc_algo *algo;
1943 struct cc_var cc_mem;
1944 char buf[TCP_CA_NAME_MAX];
1949 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1952 buf[sopt->sopt_valsize] = '\0';
1954 STAILQ_FOREACH(algo, &cc_list, entries) {
1955 if (strncmp(buf, algo->name,
1956 TCP_CA_NAME_MAX) == 0) {
1957 if (algo->flags & CC_MODULE_BEING_REMOVED) {
1958 /* We can't "see" modules being unloaded */
1969 * With a reference the algorithm cannot be removed
1970 * so we hold a reference through the change process.
1974 if (algo->cb_init != NULL) {
1975 /* We can now pre-get the memory for the CC */
1976 mem_sz = (*algo->cc_data_sz)();
1980 ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
1987 * Make sure its all clean and zero and also get
1990 memset(&cc_mem, 0, sizeof(cc_mem));
1992 if (inp->inp_flags & INP_DROPPED) {
1995 free(ptr, M_CC_MEM);
1996 /* Release our temp reference */
2000 return (ECONNRESET);
2002 tp = intotcpcb(inp);
2004 memset(ptr, 0, mem_sz);
2005 cc_mem.ccvc.tcp = tp;
2007 * We once again hold a write lock over the tcb so it's
2008 * safe to do these things without ordering concerns.
2009 * Note here we init into stack memory.
2011 if (algo->cb_init != NULL)
2012 error = algo->cb_init(&cc_mem, ptr);
2016 * The CC algorithms, when given their memory
2017 * should not fail we could in theory have a
2022 * Touchdown, lets go ahead and move the
2023 * connection to the new CC module by
2024 * copying in the cc_mem after we call
2025 * the old ones cleanup (if any).
2027 if (CC_ALGO(tp)->cb_destroy != NULL)
2028 CC_ALGO(tp)->cb_destroy(&tp->t_ccv);
2029 /* Detach the old CC from the tcpcb */
2031 /* Copy in our temp memory that was inited */
2032 memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var));
2033 /* Now attach the new, which takes a reference */
2034 cc_attach(tp, algo);
2035 /* Ok now are we where we have gotten past any conn_init? */
2036 if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
2037 /* Yep run the connection init for the new CC */
2038 CC_ALGO(tp)->conn_init(&tp->t_ccv);
2041 free(ptr, M_CC_MEM);
2043 /* Now lets release our temp reference */
2051 tcp_default_ctloutput(struct tcpcb *tp, struct sockopt *sopt)
2053 struct inpcb *inp = tptoinpcb(tp);
2054 int error, opt, optval;
2058 struct tls_enable tls;
2059 struct socket *so = inp->inp_socket;
2061 char *pbuf, buf[TCP_LOG_ID_LEN];
2063 struct statsblob *sbp;
2067 INP_WLOCK_ASSERT(inp);
2068 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
2069 ("inp_flags == %x", inp->inp_flags));
2070 KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
2072 switch (sopt->sopt_level) {
2075 MPASS(inp->inp_vflag & INP_IPV6PROTO);
2076 switch (sopt->sopt_name) {
2077 case IPV6_USE_MIN_MTU:
2078 tcp6_use_min_mtu(tp);
2092 * For TCP_CCALGOOPT forward the control to CC module, for both
2093 * SOPT_SET and SOPT_GET.
2095 switch (sopt->sopt_name) {
2098 if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2100 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2101 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2102 sopt->sopt_valsize);
2107 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2108 if (CC_ALGO(tp)->ctl_output != NULL)
2109 error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf);
2113 if (error == 0 && sopt->sopt_dir == SOPT_GET)
2114 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2119 switch (sopt->sopt_dir) {
2121 switch (sopt->sopt_name) {
2122 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2125 if (!TCPMD5_ENABLED())
2126 return (ENOPROTOOPT);
2127 error = TCPMD5_PCBCTL(inp, sopt);
2130 INP_WLOCK_RECHECK(inp);
2131 goto unlock_and_done;
2138 error = sooptcopyin(sopt, &optval, sizeof optval,
2143 INP_WLOCK_RECHECK(inp);
2144 switch (sopt->sopt_name) {
2155 opt = 0; /* dead code to fool gcc */
2162 tp->t_flags &= ~opt;
2165 if (tp->t_flags & TF_TOE) {
2166 tcp_offload_ctloutput(tp, sopt->sopt_dir,
2175 error = sooptcopyin(sopt, &optval, sizeof optval,
2180 INP_WLOCK_RECHECK(inp);
2182 tp->t_flags |= TF_NOPUSH;
2183 else if (tp->t_flags & TF_NOPUSH) {
2184 tp->t_flags &= ~TF_NOPUSH;
2185 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2186 struct epoch_tracker et;
2188 NET_EPOCH_ENTER(et);
2189 error = tcp_output_nodrop(tp);
2193 goto unlock_and_done;
2195 case TCP_REMOTE_UDP_ENCAPS_PORT:
2197 error = sooptcopyin(sopt, &optval, sizeof optval,
2201 if ((optval < TCP_TUNNELING_PORT_MIN) ||
2202 (optval > TCP_TUNNELING_PORT_MAX)) {
2203 /* Its got to be in range */
2206 if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
2207 /* You have to have enabled a UDP tunneling port first */
2210 INP_WLOCK_RECHECK(inp);
2211 if (tp->t_state != TCPS_CLOSED) {
2212 /* You can't change after you are connected */
2215 /* Ok we are all good set the port */
2216 tp->t_port = htons(optval);
2218 goto unlock_and_done;
2222 error = sooptcopyin(sopt, &optval, sizeof optval,
2227 INP_WLOCK_RECHECK(inp);
2228 if (optval > 0 && optval <= tp->t_maxseg &&
2229 optval + 40 >= V_tcp_minmss)
2230 tp->t_maxseg = optval;
2233 goto unlock_and_done;
2243 error = sooptcopyin(sopt, &optval, sizeof optval,
2249 sbp = stats_blob_alloc(
2250 V_tcp_perconn_stats_dflt_tpl, 0);
2254 INP_WLOCK_RECHECK(inp);
2255 if ((tp->t_stats != NULL && sbp == NULL) ||
2256 (tp->t_stats == NULL && sbp != NULL)) {
2257 struct statsblob *t = tp->t_stats;
2263 stats_blob_destroy(sbp);
2265 return (EOPNOTSUPP);
2269 case TCP_CONGESTION:
2270 error = tcp_set_cc_mod(inp, sopt);
2273 case TCP_REUSPORT_LB_NUMA:
2275 error = sooptcopyin(sopt, &optval, sizeof(optval),
2277 INP_WLOCK_RECHECK(inp);
2279 error = in_pcblbgroup_numa(inp, optval);
2284 case TCP_TXTLS_ENABLE:
2286 error = copyin_tls_enable(sopt, &tls);
2289 error = ktls_enable_tx(so, &tls);
2291 case TCP_TXTLS_MODE:
2293 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2297 INP_WLOCK_RECHECK(inp);
2298 error = ktls_set_tx_mode(so, ui);
2301 case TCP_RXTLS_ENABLE:
2303 error = sooptcopyin(sopt, &tls, sizeof(tls),
2307 error = ktls_enable_rx(so, &tls);
2310 case TCP_MAXUNACKTIME:
2315 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2319 if (ui > (UINT_MAX / hz)) {
2325 INP_WLOCK_RECHECK(inp);
2326 switch (sopt->sopt_name) {
2327 case TCP_MAXUNACKTIME:
2328 tp->t_maxunacktime = ui;
2332 tp->t_keepidle = ui;
2334 * XXX: better check current remaining
2335 * timeout and "merge" it with new value.
2337 if ((tp->t_state > TCPS_LISTEN) &&
2338 (tp->t_state <= TCPS_CLOSING))
2339 tcp_timer_activate(tp, TT_KEEP,
2343 tp->t_keepintvl = ui;
2344 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2345 (TP_MAXIDLE(tp) > 0))
2346 tcp_timer_activate(tp, TT_2MSL,
2350 tp->t_keepinit = ui;
2351 if (tp->t_state == TCPS_SYN_RECEIVED ||
2352 tp->t_state == TCPS_SYN_SENT)
2353 tcp_timer_activate(tp, TT_KEEP,
2357 goto unlock_and_done;
2361 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2365 INP_WLOCK_RECHECK(inp);
2367 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2368 (TP_MAXIDLE(tp) > 0))
2369 tcp_timer_activate(tp, TT_2MSL,
2371 goto unlock_and_done;
2377 error = sooptcopyin(sopt, &optval, sizeof optval,
2382 INP_WLOCK_RECHECK(inp);
2384 tcp_pcap_set_sock_max(
2385 (sopt->sopt_name == TCP_PCAP_OUT) ?
2386 &(tp->t_outpkts) : &(tp->t_inpkts),
2390 goto unlock_and_done;
2393 case TCP_FASTOPEN: {
2394 struct tcp_fastopen tfo_optval;
2397 if (!V_tcp_fastopen_client_enable &&
2398 !V_tcp_fastopen_server_enable)
2401 error = sooptcopyin(sopt, &tfo_optval,
2402 sizeof(tfo_optval), sizeof(int));
2406 INP_WLOCK_RECHECK(inp);
2407 if ((tp->t_state != TCPS_CLOSED) &&
2408 (tp->t_state != TCPS_LISTEN)) {
2410 goto unlock_and_done;
2412 if (tfo_optval.enable) {
2413 if (tp->t_state == TCPS_LISTEN) {
2414 if (!V_tcp_fastopen_server_enable) {
2416 goto unlock_and_done;
2419 if (tp->t_tfo_pending == NULL)
2421 tcp_fastopen_alloc_counter();
2424 * If a pre-shared key was provided,
2425 * stash it in the client cookie
2426 * field of the tcpcb for use during
2429 if (sopt->sopt_valsize ==
2430 sizeof(tfo_optval)) {
2431 memcpy(tp->t_tfo_cookie.client,
2433 TCP_FASTOPEN_PSK_LEN);
2434 tp->t_tfo_client_cookie_len =
2435 TCP_FASTOPEN_PSK_LEN;
2438 tp->t_flags |= TF_FASTOPEN;
2440 tp->t_flags &= ~TF_FASTOPEN;
2441 goto unlock_and_done;
2447 error = sooptcopyin(sopt, &optval, sizeof optval,
2452 INP_WLOCK_RECHECK(inp);
2453 error = tcp_log_state_change(tp, optval);
2454 goto unlock_and_done;
2463 error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
2466 buf[sopt->sopt_valsize] = '\0';
2467 INP_WLOCK_RECHECK(inp);
2468 error = tcp_log_set_id(tp, buf);
2469 /* tcp_log_set_id() unlocks the INP. */
2476 sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
2479 buf[sopt->sopt_valsize] = '\0';
2480 INP_WLOCK_RECHECK(inp);
2481 if (sopt->sopt_name == TCP_LOGDUMP) {
2482 error = tcp_log_dump_tp_logbuf(tp, buf,
2486 tcp_log_dump_tp_bucket_logbufs(tp, buf);
2488 * tcp_log_dump_tp_bucket_logbufs() drops the
2497 error = ENOPROTOOPT;
2503 tp = intotcpcb(inp);
2504 switch (sopt->sopt_name) {
2505 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2508 if (!TCPMD5_ENABLED())
2509 return (ENOPROTOOPT);
2510 error = TCPMD5_PCBCTL(inp, sopt);
2515 optval = tp->t_flags & TF_NODELAY;
2517 error = sooptcopyout(sopt, &optval, sizeof optval);
2520 optval = tp->t_maxseg;
2522 error = sooptcopyout(sopt, &optval, sizeof optval);
2524 case TCP_REMOTE_UDP_ENCAPS_PORT:
2525 optval = ntohs(tp->t_port);
2527 error = sooptcopyout(sopt, &optval, sizeof optval);
2530 optval = tp->t_flags & TF_NOOPT;
2532 error = sooptcopyout(sopt, &optval, sizeof optval);
2535 optval = tp->t_flags & TF_NOPUSH;
2537 error = sooptcopyout(sopt, &optval, sizeof optval);
2540 tcp_fill_info(tp, &ti);
2542 error = sooptcopyout(sopt, &ti, sizeof ti);
2548 TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2551 socklen_t outsbsz = sopt->sopt_valsize;
2552 if (tp->t_stats == NULL)
2554 else if (outsbsz >= tp->t_stats->cursz)
2555 outsbsz = tp->t_stats->cursz;
2556 else if (outsbsz >= sizeof(struct statsblob))
2557 outsbsz = sizeof(struct statsblob);
2564 sbp = sopt->sopt_val;
2565 nheld = atop(round_page(((vm_offset_t)sbp) +
2566 (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2567 vm_page_t ma[nheld];
2568 if (vm_fault_quick_hold_pages(
2569 &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2570 outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2576 if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2577 SIZEOF_MEMBER(struct statsblob, flags))))
2580 INP_WLOCK_RECHECK(inp);
2581 error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2582 sbflags | SB_CLONE_USRDSTNOFAULT);
2584 sopt->sopt_valsize = outsbsz;
2586 vm_page_unhold_pages(ma, nheld);
2593 case TCP_CONGESTION:
2594 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2596 error = sooptcopyout(sopt, buf, len + 1);
2598 case TCP_MAXUNACKTIME:
2603 switch (sopt->sopt_name) {
2604 case TCP_MAXUNACKTIME:
2605 ui = TP_MAXUNACKTIME(tp) / hz;
2608 ui = TP_KEEPIDLE(tp) / hz;
2611 ui = TP_KEEPINTVL(tp) / hz;
2614 ui = TP_KEEPINIT(tp) / hz;
2617 ui = TP_KEEPCNT(tp);
2621 error = sooptcopyout(sopt, &ui, sizeof(ui));
2626 optval = tcp_pcap_get_sock_max(
2627 (sopt->sopt_name == TCP_PCAP_OUT) ?
2628 &(tp->t_outpkts) : &(tp->t_inpkts));
2630 error = sooptcopyout(sopt, &optval, sizeof optval);
2634 optval = tp->t_flags & TF_FASTOPEN;
2636 error = sooptcopyout(sopt, &optval, sizeof optval);
2640 optval = tcp_get_bblog_state(tp);
2642 error = sooptcopyout(sopt, &optval, sizeof(optval));
2645 /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2646 error = tcp_log_getlogbuf(sopt, tp);
2649 len = tcp_log_get_id(tp, buf);
2651 error = sooptcopyout(sopt, buf, len + 1);
2660 case TCP_TXTLS_MODE:
2661 error = ktls_get_tx_mode(so, &optval);
2664 error = sooptcopyout(sopt, &optval,
2667 case TCP_RXTLS_MODE:
2668 error = ktls_get_rx_mode(so, &optval);
2671 error = sooptcopyout(sopt, &optval,
2676 optval = tp->t_flags & TF_LRD;
2678 error = sooptcopyout(sopt, &optval, sizeof optval);
2682 error = ENOPROTOOPT;
2689 #undef INP_WLOCK_RECHECK
2690 #undef INP_WLOCK_RECHECK_CLEANUP
2693 * Initiate (or continue) disconnect.
2694 * If embryonic state, just send reset (once).
2695 * If in ``let data drain'' option and linger null, just drop.
2696 * Otherwise (hard), mark socket disconnecting and drop
2697 * current input data; switch states based on user close, and
2698 * send segment to peer (with FIN).
2701 tcp_disconnect(struct tcpcb *tp)
2703 struct inpcb *inp = tptoinpcb(tp);
2704 struct socket *so = tptosocket(tp);
2707 INP_WLOCK_ASSERT(inp);
2710 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2711 * socket is still open.
2713 if (tp->t_state < TCPS_ESTABLISHED &&
2714 !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2717 ("tcp_disconnect: tcp_close() returned NULL"));
2718 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2719 tp = tcp_drop(tp, 0);
2721 ("tcp_disconnect: tcp_drop() returned NULL"));
2723 soisdisconnecting(so);
2724 sbflush(&so->so_rcv);
2726 if (!(inp->inp_flags & INP_DROPPED))
2727 /* Ignore stack's drop request, we already at it. */
2728 (void)tcp_output_nodrop(tp);
2733 * User issued close, and wish to trail through shutdown states:
2734 * if never received SYN, just forget it. If got a SYN from peer,
2735 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2736 * If already got a FIN from peer, then almost done; go to LAST_ACK
2737 * state. In all other cases, have already sent FIN to peer (e.g.
2738 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2739 * for peer to send FIN or not respond to keep-alives, etc.
2740 * We can let the user exit from the close as soon as the FIN is acked.
2743 tcp_usrclosed(struct tcpcb *tp)
2747 INP_WLOCK_ASSERT(tptoinpcb(tp));
2749 switch (tp->t_state) {
2752 tcp_offload_listen_stop(tp);
2754 tcp_state_change(tp, TCPS_CLOSED);
2759 * tcp_close() should never return NULL here as the socket is
2763 ("tcp_usrclosed: tcp_close() returned NULL"));
2767 case TCPS_SYN_RECEIVED:
2768 tp->t_flags |= TF_NEEDFIN;
2771 case TCPS_ESTABLISHED:
2772 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2775 case TCPS_CLOSE_WAIT:
2776 tcp_state_change(tp, TCPS_LAST_ACK);
2779 if (tp->t_acktime == 0)
2780 tp->t_acktime = ticks;
2781 if (tp->t_state >= TCPS_FIN_WAIT_2) {
2782 soisdisconnected(tptosocket(tp));
2783 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
2784 if (tp->t_state == TCPS_FIN_WAIT_2) {
2787 timeout = (tcp_fast_finwait2_recycle) ?
2788 tcp_finwait2_timeout : TP_MAXIDLE(tp);
2789 tcp_timer_activate(tp, TT_2MSL, timeout);
2796 db_print_indent(int indent)
2800 for (i = 0; i < indent; i++)
2805 db_print_tstate(int t_state)
2810 db_printf("TCPS_CLOSED");
2814 db_printf("TCPS_LISTEN");
2818 db_printf("TCPS_SYN_SENT");
2821 case TCPS_SYN_RECEIVED:
2822 db_printf("TCPS_SYN_RECEIVED");
2825 case TCPS_ESTABLISHED:
2826 db_printf("TCPS_ESTABLISHED");
2829 case TCPS_CLOSE_WAIT:
2830 db_printf("TCPS_CLOSE_WAIT");
2833 case TCPS_FIN_WAIT_1:
2834 db_printf("TCPS_FIN_WAIT_1");
2838 db_printf("TCPS_CLOSING");
2842 db_printf("TCPS_LAST_ACK");
2845 case TCPS_FIN_WAIT_2:
2846 db_printf("TCPS_FIN_WAIT_2");
2849 case TCPS_TIME_WAIT:
2850 db_printf("TCPS_TIME_WAIT");
2854 db_printf("unknown");
2860 db_print_tflags(u_int t_flags)
2865 if (t_flags & TF_ACKNOW) {
2866 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2869 if (t_flags & TF_DELACK) {
2870 db_printf("%sTF_DELACK", comma ? ", " : "");
2873 if (t_flags & TF_NODELAY) {
2874 db_printf("%sTF_NODELAY", comma ? ", " : "");
2877 if (t_flags & TF_NOOPT) {
2878 db_printf("%sTF_NOOPT", comma ? ", " : "");
2881 if (t_flags & TF_SENTFIN) {
2882 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2885 if (t_flags & TF_REQ_SCALE) {
2886 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2889 if (t_flags & TF_RCVD_SCALE) {
2890 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2893 if (t_flags & TF_REQ_TSTMP) {
2894 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2897 if (t_flags & TF_RCVD_TSTMP) {
2898 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2901 if (t_flags & TF_SACK_PERMIT) {
2902 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2905 if (t_flags & TF_NEEDSYN) {
2906 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2909 if (t_flags & TF_NEEDFIN) {
2910 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2913 if (t_flags & TF_NOPUSH) {
2914 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2917 if (t_flags & TF_PREVVALID) {
2918 db_printf("%sTF_PREVVALID", comma ? ", " : "");
2921 if (t_flags & TF_MORETOCOME) {
2922 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2925 if (t_flags & TF_SONOTCONN) {
2926 db_printf("%sTF_SONOTCONN", comma ? ", " : "");
2929 if (t_flags & TF_LASTIDLE) {
2930 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2933 if (t_flags & TF_RXWIN0SENT) {
2934 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2937 if (t_flags & TF_FASTRECOVERY) {
2938 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2941 if (t_flags & TF_CONGRECOVERY) {
2942 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2945 if (t_flags & TF_WASFRECOVERY) {
2946 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2949 if (t_flags & TF_WASCRECOVERY) {
2950 db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
2953 if (t_flags & TF_SIGNATURE) {
2954 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2957 if (t_flags & TF_FORCEDATA) {
2958 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2961 if (t_flags & TF_TSO) {
2962 db_printf("%sTF_TSO", comma ? ", " : "");
2965 if (t_flags & TF_FASTOPEN) {
2966 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2972 db_print_tflags2(u_int t_flags2)
2977 if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
2978 db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
2981 if (t_flags2 & TF2_PLPMTU_PMTUD) {
2982 db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
2985 if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
2986 db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
2989 if (t_flags2 & TF2_LOG_AUTO) {
2990 db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
2993 if (t_flags2 & TF2_DROP_AF_DATA) {
2994 db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
2997 if (t_flags2 & TF2_ECN_PERMIT) {
2998 db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
3001 if (t_flags2 & TF2_ECN_SND_CWR) {
3002 db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
3005 if (t_flags2 & TF2_ECN_SND_ECE) {
3006 db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
3009 if (t_flags2 & TF2_ACE_PERMIT) {
3010 db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
3013 if (t_flags2 & TF2_FBYTES_COMPLETE) {
3014 db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
3020 db_print_toobflags(char t_oobflags)
3025 if (t_oobflags & TCPOOB_HAVEDATA) {
3026 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
3029 if (t_oobflags & TCPOOB_HADDATA) {
3030 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
3036 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
3039 db_print_indent(indent);
3040 db_printf("%s at %p\n", name, tp);
3044 db_print_indent(indent);
3045 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
3046 TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
3048 db_print_indent(indent);
3049 db_printf("t_callout: %p t_timers: %p\n",
3050 &tp->t_callout, &tp->t_timers);
3052 db_print_indent(indent);
3053 db_printf("t_state: %d (", tp->t_state);
3054 db_print_tstate(tp->t_state);
3057 db_print_indent(indent);
3058 db_printf("t_flags: 0x%x (", tp->t_flags);
3059 db_print_tflags(tp->t_flags);
3062 db_print_indent(indent);
3063 db_printf("t_flags2: 0x%x (", tp->t_flags2);
3064 db_print_tflags2(tp->t_flags2);
3067 db_print_indent(indent);
3068 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: 0x%08x\n",
3069 tp->snd_una, tp->snd_max, tp->snd_nxt);
3071 db_print_indent(indent);
3072 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
3073 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
3075 db_print_indent(indent);
3076 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
3077 tp->iss, tp->irs, tp->rcv_nxt);
3079 db_print_indent(indent);
3080 db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n",
3081 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
3083 db_print_indent(indent);
3084 db_printf("snd_wnd: %u snd_cwnd: %u\n",
3085 tp->snd_wnd, tp->snd_cwnd);
3087 db_print_indent(indent);
3088 db_printf("snd_ssthresh: %u snd_recover: "
3089 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3091 db_print_indent(indent);
3092 db_printf("t_rcvtime: %u t_startime: %u\n",
3093 tp->t_rcvtime, tp->t_starttime);
3095 db_print_indent(indent);
3096 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
3097 tp->t_rtttime, tp->t_rtseq);
3099 db_print_indent(indent);
3100 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
3101 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3103 db_print_indent(indent);
3104 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u\n",
3105 tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin);
3107 db_print_indent(indent);
3108 db_printf("t_rttupdated: %u max_sndwnd: %u t_softerror: %d\n",
3109 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3111 db_print_indent(indent);
3112 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3113 db_print_toobflags(tp->t_oobflags);
3114 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
3116 db_print_indent(indent);
3117 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
3118 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3120 db_print_indent(indent);
3121 db_printf("ts_recent: %u ts_recent_age: %u\n",
3122 tp->ts_recent, tp->ts_recent_age);
3124 db_print_indent(indent);
3125 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
3126 "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3128 db_print_indent(indent);
3129 db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x "
3130 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3131 tp->snd_recover_prev, tp->t_badrxtwin);
3133 db_print_indent(indent);
3134 db_printf("snd_numholes: %d snd_holes first: %p\n",
3135 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3137 db_print_indent(indent);
3138 db_printf("snd_fack: 0x%08x rcv_numsacks: %d\n",
3139 tp->snd_fack, tp->rcv_numsacks);
3141 /* Skip sackblks, sackhint. */
3143 db_print_indent(indent);
3144 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
3145 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3148 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3153 db_printf("usage: show tcpcb <addr>\n");
3156 tp = (struct tcpcb *)addr;
3158 db_print_tcpcb(tp, "tcpcb", 0);