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 *,
125 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
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 attaches to socket via pru_attach(), reserving space,
136 * and an internet control block.
139 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
142 struct tcpcb *tp = NULL;
146 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
148 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
152 so->so_rcv.sb_flags |= SB_AUTOSIZE;
153 so->so_snd.sb_flags |= SB_AUTOSIZE;
154 error = in_pcballoc(so, &V_tcbinfo);
158 tp = tcp_newtcpcb(inp);
165 tp->t_state = TCPS_CLOSED;
167 TCPSTATES_INC(TCPS_CLOSED);
169 TCP_PROBE2(debug__user, tp, PRU_ATTACH);
174 * tcp_usr_detach is called when the socket layer loses its final reference
175 * to the socket, be it a file descriptor reference, a reference from TCP,
176 * etc. At this point, there is only one case in which we will keep around
177 * inpcb state: time wait.
180 tcp_usr_detach(struct socket *so)
186 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
188 KASSERT(so->so_pcb == inp && inp->inp_socket == so,
189 ("%s: socket %p inp %p mismatch", __func__, so, inp));
193 KASSERT(inp->inp_flags & INP_DROPPED ||
194 tp->t_state < TCPS_SYN_SENT,
195 ("%s: inp %p not dropped or embryonic", __func__, inp));
204 * Give the socket an address.
207 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
212 struct tcpcb *tp = NULL;
214 struct sockaddr_in *sinp;
216 sinp = (struct sockaddr_in *)nam;
217 if (nam->sa_family != AF_INET) {
219 * Preserve compatibility with old programs.
221 if (nam->sa_family != AF_UNSPEC ||
222 nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
223 sinp->sin_addr.s_addr != INADDR_ANY)
224 return (EAFNOSUPPORT);
225 nam->sa_family = AF_INET;
227 if (nam->sa_len != sizeof(*sinp))
231 * Must check for multicast addresses and disallow binding
234 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
235 return (EAFNOSUPPORT);
238 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
240 if (inp->inp_flags & INP_DROPPED) {
247 INP_HASH_WLOCK(&V_tcbinfo);
248 error = in_pcbbind(inp, nam, td->td_ucred);
249 INP_HASH_WUNLOCK(&V_tcbinfo);
251 TCP_PROBE2(debug__user, tp, PRU_BIND);
260 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
265 struct tcpcb *tp = NULL;
267 struct sockaddr_in6 *sin6;
270 sin6 = (struct sockaddr_in6 *)nam;
271 if (nam->sa_family != AF_INET6)
272 return (EAFNOSUPPORT);
273 if (nam->sa_len != sizeof(*sin6))
277 * Must check for multicast addresses and disallow binding
280 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
281 return (EAFNOSUPPORT);
284 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
286 vflagsav = inp->inp_vflag;
287 if (inp->inp_flags & INP_DROPPED) {
294 INP_HASH_WLOCK(&V_tcbinfo);
295 inp->inp_vflag &= ~INP_IPV4;
296 inp->inp_vflag |= INP_IPV6;
298 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
299 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
300 inp->inp_vflag |= INP_IPV4;
301 else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
302 struct sockaddr_in sin;
304 in6_sin6_2_sin(&sin, sin6);
305 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
306 error = EAFNOSUPPORT;
307 INP_HASH_WUNLOCK(&V_tcbinfo);
310 inp->inp_vflag |= INP_IPV4;
311 inp->inp_vflag &= ~INP_IPV6;
312 error = in_pcbbind(inp, (struct sockaddr *)&sin,
314 INP_HASH_WUNLOCK(&V_tcbinfo);
319 error = in6_pcbbind(inp, nam, td->td_ucred);
320 INP_HASH_WUNLOCK(&V_tcbinfo);
323 inp->inp_vflag = vflagsav;
324 TCP_PROBE2(debug__user, tp, PRU_BIND);
332 * Prepare to accept connections.
335 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
339 struct tcpcb *tp = NULL;
342 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
344 if (inp->inp_flags & INP_DROPPED) {
350 error = solisten_proto_check(so);
355 if (inp->inp_lport == 0) {
356 INP_HASH_WLOCK(&V_tcbinfo);
357 error = in_pcbbind(inp, NULL, td->td_ucred);
358 INP_HASH_WUNLOCK(&V_tcbinfo);
361 tcp_state_change(tp, TCPS_LISTEN);
362 solisten_proto(so, backlog);
364 if ((so->so_options & SO_NO_OFFLOAD) == 0)
365 tcp_offload_listen_start(tp);
368 solisten_proto_abort(so);
372 if (IS_FASTOPEN(tp->t_flags))
373 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
376 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
384 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
388 struct tcpcb *tp = NULL;
392 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
394 if (inp->inp_flags & INP_DROPPED) {
398 vflagsav = inp->inp_vflag;
401 error = solisten_proto_check(so);
406 INP_HASH_WLOCK(&V_tcbinfo);
407 if (inp->inp_lport == 0) {
408 inp->inp_vflag &= ~INP_IPV4;
409 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
410 inp->inp_vflag |= INP_IPV4;
411 error = in6_pcbbind(inp, NULL, td->td_ucred);
413 INP_HASH_WUNLOCK(&V_tcbinfo);
415 tcp_state_change(tp, TCPS_LISTEN);
416 solisten_proto(so, backlog);
418 if ((so->so_options & SO_NO_OFFLOAD) == 0)
419 tcp_offload_listen_start(tp);
422 solisten_proto_abort(so);
426 if (IS_FASTOPEN(tp->t_flags))
427 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
430 inp->inp_vflag = vflagsav;
433 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
441 * Initiate connection to peer.
442 * Create a template for use in transmissions on this connection.
443 * Enter SYN_SENT state, and mark socket as connecting.
444 * Start keep-alive timer, and seed output sequence space.
445 * Send initial segment on connection.
448 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
450 struct epoch_tracker et;
453 struct tcpcb *tp = NULL;
454 struct sockaddr_in *sinp;
456 sinp = (struct sockaddr_in *)nam;
457 if (nam->sa_family != AF_INET)
458 return (EAFNOSUPPORT);
459 if (nam->sa_len != sizeof (*sinp))
463 * Must disallow TCP ``connections'' to multicast addresses.
465 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
466 return (EAFNOSUPPORT);
467 if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST)
469 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
473 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
475 if (inp->inp_flags & INP_DROPPED) {
476 error = ECONNREFUSED;
479 if (SOLISTENING(so)) {
485 if ((error = tcp_connect(tp, nam, td)) != 0)
488 if (registered_toedevs > 0 &&
489 (so->so_options & SO_NO_OFFLOAD) == 0 &&
490 (error = tcp_offload_connect(so, nam)) == 0)
493 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
494 error = tcp_output(tp);
495 KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
496 ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
500 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
508 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
510 struct epoch_tracker et;
513 struct tcpcb *tp = NULL;
514 struct sockaddr_in6 *sin6;
518 sin6 = (struct sockaddr_in6 *)nam;
519 if (nam->sa_family != AF_INET6)
520 return (EAFNOSUPPORT);
521 if (nam->sa_len != sizeof (*sin6))
525 * Must disallow TCP ``connections'' to multicast addresses.
527 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
528 return (EAFNOSUPPORT);
531 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
533 vflagsav = inp->inp_vflag;
534 incflagsav = inp->inp_inc.inc_flags;
535 if (inp->inp_flags & INP_DROPPED) {
536 error = ECONNREFUSED;
539 if (SOLISTENING(so)) {
546 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
547 * therefore probably require the hash lock, which isn't held here.
548 * Is this a significant problem?
550 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
551 struct sockaddr_in sin;
553 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
557 if ((inp->inp_vflag & INP_IPV4) == 0) {
558 error = EAFNOSUPPORT;
562 in6_sin6_2_sin(&sin, sin6);
563 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
564 error = EAFNOSUPPORT;
567 if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
571 if ((error = prison_remote_ip4(td->td_ucred,
572 &sin.sin_addr)) != 0)
574 inp->inp_vflag |= INP_IPV4;
575 inp->inp_vflag &= ~INP_IPV6;
577 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
580 if (registered_toedevs > 0 &&
581 (so->so_options & SO_NO_OFFLOAD) == 0 &&
582 (error = tcp_offload_connect(so, nam)) == 0)
585 error = tcp_output(tp);
588 if ((inp->inp_vflag & INP_IPV6) == 0) {
589 error = EAFNOSUPPORT;
594 if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
596 inp->inp_vflag &= ~INP_IPV4;
597 inp->inp_vflag |= INP_IPV6;
598 inp->inp_inc.inc_flags |= INC_ISIPV6;
600 if ((error = tcp6_connect(tp, nam, td)) != 0)
603 if (registered_toedevs > 0 &&
604 (so->so_options & SO_NO_OFFLOAD) == 0 &&
605 (error = tcp_offload_connect(so, nam)) == 0)
608 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
609 error = tcp_output(tp);
613 KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
614 ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
616 * If the implicit bind in the connect call fails, restore
617 * the flags we modified.
619 if (error != 0 && inp->inp_lport == 0) {
620 inp->inp_vflag = vflagsav;
621 inp->inp_inc.inc_flags = incflagsav;
624 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
631 * Initiate disconnect from peer.
632 * If connection never passed embryonic stage, just drop;
633 * else if don't need to let data drain, then can just drop anyways,
634 * else have to begin TCP shutdown process: mark socket disconnecting,
635 * drain unread data, state switch to reflect user close, and
636 * send segment (e.g. FIN) to peer. Socket will be really disconnected
637 * when peer sends FIN and acks ours.
639 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
642 tcp_usr_disconnect(struct socket *so)
645 struct tcpcb *tp = NULL;
646 struct epoch_tracker et;
651 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
653 if (inp->inp_flags & INP_DROPPED) {
660 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
668 * Accept a connection. Essentially all the work is done at higher levels;
669 * just return the address of the peer, storing through addr.
672 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
675 struct inpcb *inp = NULL;
677 struct tcpcb *tp = NULL;
682 if (so->so_state & SS_ISDISCONNECTED)
683 return (ECONNABORTED);
686 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
688 if (inp->inp_flags & INP_DROPPED) {
689 error = ECONNABORTED;
697 * We inline in_getpeeraddr and COMMON_END here, so that we can
698 * copy the data of interest and defer the malloc until after we
701 port = inp->inp_fport;
702 addr = inp->inp_faddr;
705 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
708 *nam = in_sockaddr(port, &addr);
715 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
717 struct inpcb *inp = NULL;
720 struct tcpcb *tp = NULL;
723 struct in6_addr addr6;
724 struct epoch_tracker et;
728 if (so->so_state & SS_ISDISCONNECTED)
729 return (ECONNABORTED);
732 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
735 if (inp->inp_flags & INP_DROPPED) {
736 error = ECONNABORTED;
744 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
745 * copy the data of interest and defer the malloc until after we
748 if (inp->inp_vflag & INP_IPV4) {
750 port = inp->inp_fport;
751 addr = inp->inp_faddr;
753 port = inp->inp_fport;
754 addr6 = inp->in6p_faddr;
758 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
763 *nam = in6_v4mapsin6_sockaddr(port, &addr);
765 *nam = in6_sockaddr(port, &addr6);
772 * Mark the connection as being incapable of further output.
775 tcp_usr_shutdown(struct socket *so)
779 struct tcpcb *tp = NULL;
780 struct epoch_tracker et;
783 KASSERT(inp != NULL, ("inp == NULL"));
785 if (inp->inp_flags & INP_DROPPED) {
793 if (!(inp->inp_flags & INP_DROPPED))
794 error = tcp_output_nodrop(tp);
795 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
796 error = tcp_unlock_or_drop(tp, error);
803 * After a receive, possibly send window update to peer.
806 tcp_usr_rcvd(struct socket *so, int flags)
808 struct epoch_tracker et;
810 struct tcpcb *tp = NULL;
811 int outrv = 0, error = 0;
814 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
816 if (inp->inp_flags & INP_DROPPED) {
823 * For passively-created TFO connections, don't attempt a window
824 * update while still in SYN_RECEIVED as this may trigger an early
825 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
826 * application response data, or failing that, when the DELACK timer
829 if (IS_FASTOPEN(tp->t_flags) &&
830 (tp->t_state == TCPS_SYN_RECEIVED))
833 if (tp->t_flags & TF_TOE)
834 tcp_offload_rcvd(tp);
837 outrv = tcp_output_nodrop(tp);
839 TCP_PROBE2(debug__user, tp, PRU_RCVD);
840 (void) tcp_unlock_or_drop(tp, outrv);
846 * Do a send by putting data in output queue and updating urgent
847 * marker if URG set. Possibly send more data. Unlike the other
848 * pru_*() routines, the mbuf chains are our responsibility. We
849 * must either enqueue them or free them. The other pru_* routines
850 * generally are caller-frees.
853 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
854 struct sockaddr *nam, struct mbuf *control, struct thread *td)
856 struct epoch_tracker et;
859 struct tcpcb *tp = NULL;
862 struct sockaddr_in sin;
864 struct sockaddr_in *sinp;
873 if (control != NULL) {
874 /* TCP doesn't do control messages (rights, creds, etc) */
875 if (control->m_len) {
879 m_freem(control); /* empty control, just free it */
883 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
885 if (inp->inp_flags & INP_DROPPED) {
886 if (m != NULL && (flags & PRUS_NOTREADY) == 0)
892 vflagsav = inp->inp_vflag;
893 incflagsav = inp->inp_inc.inc_flags;
894 restoreflags = false;
898 if ((flags & PRUS_OOB) != 0 &&
899 (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
902 if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
903 if (tp->t_state == TCPS_LISTEN) {
907 switch (nam->sa_family) {
910 sinp = (struct sockaddr_in *)nam;
911 if (sinp->sin_len != sizeof(struct sockaddr_in)) {
915 if ((inp->inp_vflag & INP_IPV6) != 0) {
916 error = EAFNOSUPPORT;
919 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
920 error = EAFNOSUPPORT;
923 if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
927 if ((error = prison_remote_ip4(td->td_ucred,
938 struct sockaddr_in6 *sin6;
940 sin6 = (struct sockaddr_in6 *)nam;
941 if (sin6->sin6_len != sizeof(*sin6)) {
945 if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
946 error = EAFNOSUPPORT;
949 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
950 error = EAFNOSUPPORT;
953 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
955 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
959 if ((inp->inp_vflag & INP_IPV4) == 0) {
960 error = EAFNOSUPPORT;
964 inp->inp_vflag &= ~INP_IPV6;
966 in6_sin6_2_sin(sinp, sin6);
968 ntohl(sinp->sin_addr.s_addr))) {
969 error = EAFNOSUPPORT;
972 if ((error = prison_remote_ip4(td->td_ucred,
977 error = EAFNOSUPPORT;
981 if ((inp->inp_vflag & INP_IPV6) == 0) {
982 error = EAFNOSUPPORT;
986 inp->inp_vflag &= ~INP_IPV4;
987 inp->inp_inc.inc_flags |= INC_ISIPV6;
988 if ((error = prison_remote_ip6(td->td_ucred,
997 error = EAFNOSUPPORT;
1001 if (!(flags & PRUS_OOB)) {
1002 if (tp->t_acktime == 0)
1003 tp->t_acktime = ticks;
1004 sbappendstream(&so->so_snd, m, flags);
1006 if (nam && tp->t_state < TCPS_SYN_SENT) {
1007 KASSERT(tp->t_state == TCPS_CLOSED,
1008 ("%s: tp %p is listening", __func__, tp));
1011 * Do implied connect if not yet connected,
1012 * initialize window to default value, and
1013 * initialize maxseg using peer's cached MSS.
1017 error = tcp6_connect(tp, nam, td);
1019 #if defined(INET6) && defined(INET)
1023 error = tcp_connect(tp,
1024 (struct sockaddr *)sinp, td);
1027 * The bind operation in tcp_connect succeeded. We
1028 * no longer want to restore the flags if later
1031 if (error == 0 || inp->inp_lport != 0)
1032 restoreflags = false;
1035 /* m is freed if PRUS_NOTREADY is unset. */
1036 sbflush(&so->so_snd);
1039 if (IS_FASTOPEN(tp->t_flags))
1040 tcp_fastopen_connect(tp);
1042 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1046 if (flags & PRUS_EOF) {
1048 * Close the send side of the connection after
1054 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1055 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1056 (tp->t_fbyte_out == 0) &&
1057 (so->so_snd.sb_ccc > 0)) {
1058 tp->t_fbyte_out = ticks;
1059 if (tp->t_fbyte_out == 0)
1060 tp->t_fbyte_out = 1;
1061 if (tp->t_fbyte_out && tp->t_fbyte_in)
1062 tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1064 if (!(inp->inp_flags & INP_DROPPED) &&
1065 !(flags & PRUS_NOTREADY)) {
1066 if (flags & PRUS_MORETOCOME)
1067 tp->t_flags |= TF_MORETOCOME;
1068 error = tcp_output_nodrop(tp);
1069 if (flags & PRUS_MORETOCOME)
1070 tp->t_flags &= ~TF_MORETOCOME;
1074 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1076 SOCKBUF_LOCK(&so->so_snd);
1077 if (sbspace(&so->so_snd) < -512) {
1078 SOCKBUF_UNLOCK(&so->so_snd);
1083 * According to RFC961 (Assigned Protocols),
1084 * the urgent pointer points to the last octet
1085 * of urgent data. We continue, however,
1086 * to consider it to indicate the first octet
1087 * of data past the urgent section.
1088 * Otherwise, snd_up should be one lower.
1090 if (tp->t_acktime == 0)
1091 tp->t_acktime = ticks;
1092 sbappendstream_locked(&so->so_snd, m, flags);
1093 SOCKBUF_UNLOCK(&so->so_snd);
1095 if (nam && tp->t_state < TCPS_SYN_SENT) {
1097 * Do implied connect if not yet connected,
1098 * initialize window to default value, and
1099 * initialize maxseg using peer's cached MSS.
1103 * Not going to contemplate SYN|URG
1105 if (IS_FASTOPEN(tp->t_flags))
1106 tp->t_flags &= ~TF_FASTOPEN;
1109 error = tcp6_connect(tp, nam, td);
1111 #if defined(INET6) && defined(INET)
1115 error = tcp_connect(tp,
1116 (struct sockaddr *)sinp, td);
1119 * The bind operation in tcp_connect succeeded. We
1120 * no longer want to restore the flags if later
1123 if (error == 0 || inp->inp_lport != 0)
1124 restoreflags = false;
1127 /* m is freed if PRUS_NOTREADY is unset. */
1128 sbflush(&so->so_snd);
1131 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1134 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1135 if ((flags & PRUS_NOTREADY) == 0) {
1136 tp->t_flags |= TF_FORCEDATA;
1137 error = tcp_output_nodrop(tp);
1138 tp->t_flags &= ~TF_FORCEDATA;
1141 TCP_LOG_EVENT(tp, NULL,
1142 &inp->inp_socket->so_rcv,
1143 &inp->inp_socket->so_snd,
1144 TCP_LOG_USERSEND, error,
1149 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
1150 * responsible for freeing memory.
1152 if (m != NULL && (flags & PRUS_NOTREADY) == 0)
1156 * If the request was unsuccessful and we changed flags,
1157 * restore the original flags.
1159 if (error != 0 && restoreflags) {
1160 inp->inp_vflag = vflagsav;
1161 inp->inp_inc.inc_flags = incflagsav;
1163 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1164 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1165 error = tcp_unlock_or_drop(tp, error);
1171 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1173 struct epoch_tracker et;
1178 inp = sotoinpcb(so);
1180 if (inp->inp_flags & INP_DROPPED) {
1182 mb_free_notready(m, count);
1183 return (ECONNRESET);
1185 tp = intotcpcb(inp);
1187 SOCKBUF_LOCK(&so->so_snd);
1188 error = sbready(&so->so_snd, m, count);
1189 SOCKBUF_UNLOCK(&so->so_snd);
1194 NET_EPOCH_ENTER(et);
1195 error = tcp_output_unlock(tp);
1202 * Abort the TCP. Drop the connection abruptly.
1205 tcp_usr_abort(struct socket *so)
1208 struct tcpcb *tp = NULL;
1209 struct epoch_tracker et;
1211 inp = sotoinpcb(so);
1212 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1214 NET_EPOCH_ENTER(et);
1216 KASSERT(inp->inp_socket != NULL,
1217 ("tcp_usr_abort: inp_socket == NULL"));
1220 * If we still have full TCP state, and we're not dropped, drop.
1222 if (!(inp->inp_flags & INP_DROPPED)) {
1223 tp = intotcpcb(inp);
1224 tp = tcp_drop(tp, ECONNABORTED);
1227 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1229 if (!(inp->inp_flags & INP_DROPPED)) {
1231 inp->inp_flags |= INP_SOCKREF;
1239 * TCP socket is closed. Start friendly disconnect.
1242 tcp_usr_close(struct socket *so)
1245 struct tcpcb *tp = NULL;
1246 struct epoch_tracker et;
1248 inp = sotoinpcb(so);
1249 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1251 NET_EPOCH_ENTER(et);
1253 KASSERT(inp->inp_socket != NULL,
1254 ("tcp_usr_close: inp_socket == NULL"));
1257 * If we still have full TCP state, and we're not dropped, initiate
1260 if (!(inp->inp_flags & INP_DROPPED)) {
1261 tp = intotcpcb(inp);
1262 tp->t_flags |= TF_CLOSED;
1264 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1266 if (!(inp->inp_flags & INP_DROPPED)) {
1268 inp->inp_flags |= INP_SOCKREF;
1275 tcp_pru_options_support(struct tcpcb *tp, int flags)
1278 * If the specific TCP stack has a pru_options
1279 * specified then it does not always support
1280 * all the PRU_XX options and we must ask it.
1281 * If the function is not specified then all
1282 * of the PRU_XX options are supported.
1286 if (tp->t_fb->tfb_pru_options) {
1287 ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1293 * Receive out-of-band data.
1296 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1300 struct tcpcb *tp = NULL;
1302 inp = sotoinpcb(so);
1303 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1305 if (inp->inp_flags & INP_DROPPED) {
1309 tp = intotcpcb(inp);
1310 error = tcp_pru_options_support(tp, PRUS_OOB);
1314 if ((so->so_oobmark == 0 &&
1315 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1316 so->so_options & SO_OOBINLINE ||
1317 tp->t_oobflags & TCPOOB_HADDATA) {
1321 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1322 error = EWOULDBLOCK;
1326 *mtod(m, caddr_t) = tp->t_iobc;
1327 if ((flags & MSG_PEEK) == 0)
1328 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1331 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1337 struct protosw tcp_protosw = {
1338 .pr_type = SOCK_STREAM,
1339 .pr_protocol = IPPROTO_TCP,
1340 .pr_flags = PR_CONNREQUIRED | PR_IMPLOPCL | PR_WANTRCVD |
1342 .pr_ctloutput = tcp_ctloutput,
1343 .pr_abort = tcp_usr_abort,
1344 .pr_accept = tcp_usr_accept,
1345 .pr_attach = tcp_usr_attach,
1346 .pr_bind = tcp_usr_bind,
1347 .pr_connect = tcp_usr_connect,
1348 .pr_control = in_control,
1349 .pr_detach = tcp_usr_detach,
1350 .pr_disconnect = tcp_usr_disconnect,
1351 .pr_listen = tcp_usr_listen,
1352 .pr_peeraddr = in_getpeeraddr,
1353 .pr_rcvd = tcp_usr_rcvd,
1354 .pr_rcvoob = tcp_usr_rcvoob,
1355 .pr_send = tcp_usr_send,
1356 .pr_ready = tcp_usr_ready,
1357 .pr_shutdown = tcp_usr_shutdown,
1358 .pr_sockaddr = in_getsockaddr,
1359 .pr_sosetlabel = in_pcbsosetlabel,
1360 .pr_close = tcp_usr_close,
1365 struct protosw tcp6_protosw = {
1366 .pr_type = SOCK_STREAM,
1367 .pr_protocol = IPPROTO_TCP,
1368 .pr_flags = PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD |
1370 .pr_ctloutput = tcp_ctloutput,
1371 .pr_abort = tcp_usr_abort,
1372 .pr_accept = tcp6_usr_accept,
1373 .pr_attach = tcp_usr_attach,
1374 .pr_bind = tcp6_usr_bind,
1375 .pr_connect = tcp6_usr_connect,
1376 .pr_control = in6_control,
1377 .pr_detach = tcp_usr_detach,
1378 .pr_disconnect = tcp_usr_disconnect,
1379 .pr_listen = tcp6_usr_listen,
1380 .pr_peeraddr = in6_mapped_peeraddr,
1381 .pr_rcvd = tcp_usr_rcvd,
1382 .pr_rcvoob = tcp_usr_rcvoob,
1383 .pr_send = tcp_usr_send,
1384 .pr_ready = tcp_usr_ready,
1385 .pr_shutdown = tcp_usr_shutdown,
1386 .pr_sockaddr = in6_mapped_sockaddr,
1387 .pr_sosetlabel = in_pcbsosetlabel,
1388 .pr_close = tcp_usr_close,
1394 * Common subroutine to open a TCP connection to remote host specified
1395 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1396 * port number if needed. Call in_pcbconnect_setup to do the routing and
1397 * to choose a local host address (interface). If there is an existing
1398 * incarnation of the same connection in TIME-WAIT state and if the remote
1399 * host was sending CC options and if the connection duration was < MSL, then
1400 * truncate the previous TIME-WAIT state and proceed.
1401 * Initialize connection parameters and enter SYN-SENT state.
1404 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1406 struct inpcb *inp = tptoinpcb(tp), *oinp;
1407 struct socket *so = tptosocket(tp);
1408 struct in_addr laddr;
1413 INP_WLOCK_ASSERT(inp);
1415 INP_HASH_WLOCK(&V_tcbinfo);
1417 * Cannot simply call in_pcbconnect, because there might be an
1418 * earlier incarnation of this same connection still in
1419 * TIME_WAIT state, creating an ADDRINUSE error.
1421 laddr = inp->inp_laddr;
1422 lport = inp->inp_lport;
1423 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1424 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1425 if (error && oinp == NULL)
1431 /* Handle initial bind if it hadn't been done in advance. */
1432 if (inp->inp_lport == 0) {
1433 inp->inp_lport = lport;
1434 if (in_pcbinshash(inp) != 0) {
1440 inp->inp_laddr = laddr;
1442 INP_HASH_WUNLOCK(&V_tcbinfo);
1445 * Compute window scaling to request:
1446 * Scale to fit into sweet spot. See tcp_syncache.c.
1447 * XXX: This should move to tcp_output().
1449 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1450 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1451 tp->request_r_scale++;
1454 TCPSTAT_INC(tcps_connattempt);
1455 tcp_state_change(tp, TCPS_SYN_SENT);
1456 tp->iss = tcp_new_isn(&inp->inp_inc);
1457 if (tp->t_flags & TF_REQ_TSTMP)
1458 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1459 tcp_sendseqinit(tp);
1464 INP_HASH_WUNLOCK(&V_tcbinfo);
1471 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1473 struct inpcb *inp = tptoinpcb(tp);
1474 struct epoch_tracker et;
1477 INP_WLOCK_ASSERT(inp);
1479 NET_EPOCH_ENTER(et);
1480 INP_HASH_WLOCK(&V_tcbinfo);
1481 error = in6_pcbconnect(inp, nam, td->td_ucred, true);
1482 INP_HASH_WUNLOCK(&V_tcbinfo);
1487 /* Compute window scaling to request. */
1488 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1489 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1490 tp->request_r_scale++;
1492 soisconnecting(inp->inp_socket);
1493 TCPSTAT_INC(tcps_connattempt);
1494 tcp_state_change(tp, TCPS_SYN_SENT);
1495 tp->iss = tcp_new_isn(&inp->inp_inc);
1496 if (tp->t_flags & TF_REQ_TSTMP)
1497 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1498 tcp_sendseqinit(tp);
1505 * Export TCP internal state information via a struct tcp_info, based on the
1506 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1507 * (TCP state machine, etc). We export all information using FreeBSD-native
1508 * constants -- for example, the numeric values for tcpi_state will differ
1512 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1515 INP_WLOCK_ASSERT(tptoinpcb(tp));
1516 bzero(ti, sizeof(*ti));
1518 ti->tcpi_state = tp->t_state;
1519 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1520 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1521 if (tp->t_flags & TF_SACK_PERMIT)
1522 ti->tcpi_options |= TCPI_OPT_SACK;
1523 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1524 ti->tcpi_options |= TCPI_OPT_WSCALE;
1525 ti->tcpi_snd_wscale = tp->snd_scale;
1526 ti->tcpi_rcv_wscale = tp->rcv_scale;
1528 if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
1529 ti->tcpi_options |= TCPI_OPT_ECN;
1531 ti->tcpi_rto = tp->t_rxtcur * tick;
1532 ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1533 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1534 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1536 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1537 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1540 * FreeBSD-specific extension fields for tcp_info.
1542 ti->tcpi_rcv_space = tp->rcv_wnd;
1543 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1544 ti->tcpi_snd_wnd = tp->snd_wnd;
1545 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1546 ti->tcpi_snd_nxt = tp->snd_nxt;
1547 ti->tcpi_snd_mss = tp->t_maxseg;
1548 ti->tcpi_rcv_mss = tp->t_maxseg;
1549 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1550 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1551 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1553 if (tp->t_flags & TF_TOE) {
1554 ti->tcpi_options |= TCPI_OPT_TOE;
1555 tcp_offload_tcp_info(tp, ti);
1559 * AccECN related counters.
1561 if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) ==
1562 (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
1564 * Internal counter starts at 5 for AccECN
1565 * but 0 for RFC3168 ECN.
1567 ti->tcpi_delivered_ce = tp->t_scep - 5;
1569 ti->tcpi_delivered_ce = tp->t_scep;
1570 ti->tcpi_received_ce = tp->t_rcep;
1574 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1575 * socket option arguments. When it re-acquires the lock after the copy, it
1576 * has to revalidate that the connection is still valid for the socket
1579 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \
1581 if (inp->inp_flags & INP_DROPPED) { \
1584 return (ECONNRESET); \
1586 tp = intotcpcb(inp); \
1588 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1591 tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt)
1593 struct socket *so = inp->inp_socket;
1594 struct tcpcb *tp = intotcpcb(inp);
1597 MPASS(sopt->sopt_dir == SOPT_SET);
1598 INP_WLOCK_ASSERT(inp);
1599 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1600 ("inp_flags == %x", inp->inp_flags));
1601 KASSERT(so != NULL, ("inp_socket == NULL"));
1603 if (sopt->sopt_level != IPPROTO_TCP) {
1606 if (inp->inp_vflag & INP_IPV6PROTO)
1607 error = ip6_ctloutput(so, sopt);
1609 #if defined(INET6) && defined(INET)
1613 error = ip_ctloutput(so, sopt);
1616 * When an IP-level socket option affects TCP, pass control
1617 * down to stack tfb_tcp_ctloutput, otherwise return what
1618 * IP level returned.
1620 switch (sopt->sopt_level) {
1623 if ((inp->inp_vflag & INP_IPV6PROTO) == 0)
1625 switch (sopt->sopt_name) {
1627 /* Notify tcp stacks that care (e.g. RACK). */
1629 case IPV6_USE_MIN_MTU:
1630 /* Update t_maxseg accordingly. */
1639 switch (sopt->sopt_name) {
1641 inp->inp_ip_tos &= ~IPTOS_ECN_MASK;
1644 /* Notify tcp stacks that care (e.g. RACK). */
1655 if (inp->inp_flags & INP_DROPPED) {
1657 return (ECONNRESET);
1659 } else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1661 * Protect the TCP option TCP_FUNCTION_BLK so
1662 * that a sub-function can *never* overwrite this.
1664 struct tcp_function_set fsn;
1665 struct tcp_function_block *blk;
1668 error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1673 if (inp->inp_flags & INP_DROPPED) {
1675 return (ECONNRESET);
1677 tp = intotcpcb(inp);
1679 blk = find_and_ref_tcp_functions(&fsn);
1684 if (tp->t_fb == blk) {
1685 /* You already have this */
1686 refcount_release(&blk->tfb_refcnt);
1690 if (tp->t_state != TCPS_CLOSED) {
1692 * The user has advanced the state
1693 * past the initial point, we may not
1694 * be able to switch.
1696 if (blk->tfb_tcp_handoff_ok != NULL) {
1698 * Does the stack provide a
1699 * query mechanism, if so it may
1700 * still be possible?
1702 error = (*blk->tfb_tcp_handoff_ok)(tp);
1706 refcount_release(&blk->tfb_refcnt);
1711 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1712 refcount_release(&blk->tfb_refcnt);
1717 * Release the old refcnt, the
1718 * lookup acquired a ref on the
1721 if (tp->t_fb->tfb_tcp_fb_fini) {
1722 struct epoch_tracker et;
1724 * Tell the stack to cleanup with 0 i.e.
1725 * the tcb is not going away.
1727 NET_EPOCH_ENTER(et);
1728 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1732 /* Assure that we are not on any hpts */
1733 tcp_hpts_remove(tptoinpcb(tp));
1735 if (blk->tfb_tcp_fb_init) {
1736 error = (*blk->tfb_tcp_fb_init)(tp);
1738 refcount_release(&blk->tfb_refcnt);
1739 if (tp->t_fb->tfb_tcp_fb_init) {
1740 if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) {
1741 /* Fall back failed, drop the connection */
1750 refcount_release(&tp->t_fb->tfb_refcnt);
1753 if (tp->t_flags & TF_TOE) {
1754 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1763 /* Pass in the INP locked, callee must unlock it. */
1764 return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1768 tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1770 struct socket *so = inp->inp_socket;
1771 struct tcpcb *tp = intotcpcb(inp);
1774 MPASS(sopt->sopt_dir == SOPT_GET);
1775 INP_WLOCK_ASSERT(inp);
1776 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1777 ("inp_flags == %x", inp->inp_flags));
1778 KASSERT(so != NULL, ("inp_socket == NULL"));
1780 if (sopt->sopt_level != IPPROTO_TCP) {
1783 if (inp->inp_vflag & INP_IPV6PROTO)
1784 error = ip6_ctloutput(so, sopt);
1786 #if defined(INET6) && defined(INET)
1790 error = ip_ctloutput(so, sopt);
1794 if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1795 (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1796 struct tcp_function_set fsn;
1798 if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1799 memset(&fsn, 0, sizeof(fsn));
1800 find_tcp_function_alias(tp->t_fb, &fsn);
1802 strncpy(fsn.function_set_name,
1803 tp->t_fb->tfb_tcp_block_name,
1804 TCP_FUNCTION_NAME_LEN_MAX);
1805 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1807 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1809 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1813 /* Pass in the INP locked, callee must unlock it. */
1814 return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1818 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1822 inp = sotoinpcb(so);
1823 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1826 if (inp->inp_flags & INP_DROPPED) {
1828 return (ECONNRESET);
1830 if (sopt->sopt_dir == SOPT_SET)
1831 return (tcp_ctloutput_set(inp, sopt));
1832 else if (sopt->sopt_dir == SOPT_GET)
1833 return (tcp_ctloutput_get(inp, sopt));
1835 panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
1839 * If this assert becomes untrue, we need to change the size of the buf
1840 * variable in tcp_default_ctloutput().
1843 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1844 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1849 copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1851 struct tls_enable_v0 tls_v0;
1854 if (sopt->sopt_valsize == sizeof(tls_v0)) {
1855 error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1859 memset(tls, 0, sizeof(*tls));
1860 tls->cipher_key = tls_v0.cipher_key;
1861 tls->iv = tls_v0.iv;
1862 tls->auth_key = tls_v0.auth_key;
1863 tls->cipher_algorithm = tls_v0.cipher_algorithm;
1864 tls->cipher_key_len = tls_v0.cipher_key_len;
1865 tls->iv_len = tls_v0.iv_len;
1866 tls->auth_algorithm = tls_v0.auth_algorithm;
1867 tls->auth_key_len = tls_v0.auth_key_len;
1868 tls->flags = tls_v0.flags;
1869 tls->tls_vmajor = tls_v0.tls_vmajor;
1870 tls->tls_vminor = tls_v0.tls_vminor;
1874 return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1878 extern struct cc_algo newreno_cc_algo;
1881 tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
1883 struct cc_algo *algo;
1886 struct cc_var cc_mem;
1887 char buf[TCP_CA_NAME_MAX];
1892 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1895 buf[sopt->sopt_valsize] = '\0';
1897 STAILQ_FOREACH(algo, &cc_list, entries) {
1898 if (strncmp(buf, algo->name,
1899 TCP_CA_NAME_MAX) == 0) {
1900 if (algo->flags & CC_MODULE_BEING_REMOVED) {
1901 /* We can't "see" modules being unloaded */
1912 * With a reference the algorithm cannot be removed
1913 * so we hold a reference through the change process.
1917 if (algo->cb_init != NULL) {
1918 /* We can now pre-get the memory for the CC */
1919 mem_sz = (*algo->cc_data_sz)();
1923 ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
1930 * Make sure its all clean and zero and also get
1933 memset(&cc_mem, 0, sizeof(cc_mem));
1935 if (inp->inp_flags & INP_DROPPED) {
1938 free(ptr, M_CC_MEM);
1939 /* Release our temp reference */
1943 return (ECONNRESET);
1945 tp = intotcpcb(inp);
1947 memset(ptr, 0, mem_sz);
1948 cc_mem.ccvc.tcp = tp;
1950 * We once again hold a write lock over the tcb so it's
1951 * safe to do these things without ordering concerns.
1952 * Note here we init into stack memory.
1954 if (algo->cb_init != NULL)
1955 error = algo->cb_init(&cc_mem, ptr);
1959 * The CC algorithms, when given their memory
1960 * should not fail we could in theory have a
1965 * Touchdown, lets go ahead and move the
1966 * connection to the new CC module by
1967 * copying in the cc_mem after we call
1968 * the old ones cleanup (if any).
1970 if (CC_ALGO(tp)->cb_destroy != NULL)
1971 CC_ALGO(tp)->cb_destroy(&tp->t_ccv);
1972 /* Detach the old CC from the tcpcb */
1974 /* Copy in our temp memory that was inited */
1975 memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var));
1976 /* Now attach the new, which takes a reference */
1977 cc_attach(tp, algo);
1978 /* Ok now are we where we have gotten past any conn_init? */
1979 if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
1980 /* Yep run the connection init for the new CC */
1981 CC_ALGO(tp)->conn_init(&tp->t_ccv);
1984 free(ptr, M_CC_MEM);
1986 /* Now lets release our temp reference */
1994 tcp_default_ctloutput(struct inpcb *inp, struct sockopt *sopt)
1996 struct tcpcb *tp = intotcpcb(inp);
1997 int error, opt, optval;
2001 struct tls_enable tls;
2002 struct socket *so = inp->inp_socket;
2004 char *pbuf, buf[TCP_LOG_ID_LEN];
2006 struct statsblob *sbp;
2010 INP_WLOCK_ASSERT(inp);
2011 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
2012 ("inp_flags == %x", inp->inp_flags));
2013 KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
2015 switch (sopt->sopt_level) {
2018 MPASS(inp->inp_vflag & INP_IPV6PROTO);
2019 switch (sopt->sopt_name) {
2020 case IPV6_USE_MIN_MTU:
2021 tcp6_use_min_mtu(tp);
2035 * For TCP_CCALGOOPT forward the control to CC module, for both
2036 * SOPT_SET and SOPT_GET.
2038 switch (sopt->sopt_name) {
2041 if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2043 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2044 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2045 sopt->sopt_valsize);
2050 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2051 if (CC_ALGO(tp)->ctl_output != NULL)
2052 error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf);
2056 if (error == 0 && sopt->sopt_dir == SOPT_GET)
2057 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2062 switch (sopt->sopt_dir) {
2064 switch (sopt->sopt_name) {
2065 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2068 if (!TCPMD5_ENABLED())
2069 return (ENOPROTOOPT);
2070 error = TCPMD5_PCBCTL(inp, sopt);
2073 INP_WLOCK_RECHECK(inp);
2074 goto unlock_and_done;
2081 error = sooptcopyin(sopt, &optval, sizeof optval,
2086 INP_WLOCK_RECHECK(inp);
2087 switch (sopt->sopt_name) {
2098 opt = 0; /* dead code to fool gcc */
2105 tp->t_flags &= ~opt;
2108 if (tp->t_flags & TF_TOE) {
2109 tcp_offload_ctloutput(tp, sopt->sopt_dir,
2118 error = sooptcopyin(sopt, &optval, sizeof optval,
2123 INP_WLOCK_RECHECK(inp);
2125 tp->t_flags |= TF_NOPUSH;
2126 else if (tp->t_flags & TF_NOPUSH) {
2127 tp->t_flags &= ~TF_NOPUSH;
2128 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2129 struct epoch_tracker et;
2131 NET_EPOCH_ENTER(et);
2132 error = tcp_output_nodrop(tp);
2136 goto unlock_and_done;
2138 case TCP_REMOTE_UDP_ENCAPS_PORT:
2140 error = sooptcopyin(sopt, &optval, sizeof optval,
2144 if ((optval < TCP_TUNNELING_PORT_MIN) ||
2145 (optval > TCP_TUNNELING_PORT_MAX)) {
2146 /* Its got to be in range */
2149 if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
2150 /* You have to have enabled a UDP tunneling port first */
2153 INP_WLOCK_RECHECK(inp);
2154 if (tp->t_state != TCPS_CLOSED) {
2155 /* You can't change after you are connected */
2158 /* Ok we are all good set the port */
2159 tp->t_port = htons(optval);
2161 goto unlock_and_done;
2165 error = sooptcopyin(sopt, &optval, sizeof optval,
2170 INP_WLOCK_RECHECK(inp);
2171 if (optval > 0 && optval <= tp->t_maxseg &&
2172 optval + 40 >= V_tcp_minmss)
2173 tp->t_maxseg = optval;
2176 goto unlock_and_done;
2186 error = sooptcopyin(sopt, &optval, sizeof optval,
2192 sbp = stats_blob_alloc(
2193 V_tcp_perconn_stats_dflt_tpl, 0);
2197 INP_WLOCK_RECHECK(inp);
2198 if ((tp->t_stats != NULL && sbp == NULL) ||
2199 (tp->t_stats == NULL && sbp != NULL)) {
2200 struct statsblob *t = tp->t_stats;
2206 stats_blob_destroy(sbp);
2208 return (EOPNOTSUPP);
2212 case TCP_CONGESTION:
2213 error = tcp_set_cc_mod(inp, sopt);
2216 case TCP_REUSPORT_LB_NUMA:
2218 error = sooptcopyin(sopt, &optval, sizeof(optval),
2220 INP_WLOCK_RECHECK(inp);
2222 error = in_pcblbgroup_numa(inp, optval);
2227 case TCP_TXTLS_ENABLE:
2229 error = copyin_tls_enable(sopt, &tls);
2232 error = ktls_enable_tx(so, &tls);
2234 case TCP_TXTLS_MODE:
2236 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2240 INP_WLOCK_RECHECK(inp);
2241 error = ktls_set_tx_mode(so, ui);
2244 case TCP_RXTLS_ENABLE:
2246 error = sooptcopyin(sopt, &tls, sizeof(tls),
2250 error = ktls_enable_rx(so, &tls);
2253 case TCP_MAXUNACKTIME:
2258 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2262 if (ui > (UINT_MAX / hz)) {
2268 INP_WLOCK_RECHECK(inp);
2269 switch (sopt->sopt_name) {
2270 case TCP_MAXUNACKTIME:
2271 tp->t_maxunacktime = ui;
2275 tp->t_keepidle = ui;
2277 * XXX: better check current remaining
2278 * timeout and "merge" it with new value.
2280 if ((tp->t_state > TCPS_LISTEN) &&
2281 (tp->t_state <= TCPS_CLOSING))
2282 tcp_timer_activate(tp, TT_KEEP,
2286 tp->t_keepintvl = ui;
2287 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2288 (TP_MAXIDLE(tp) > 0))
2289 tcp_timer_activate(tp, TT_2MSL,
2293 tp->t_keepinit = ui;
2294 if (tp->t_state == TCPS_SYN_RECEIVED ||
2295 tp->t_state == TCPS_SYN_SENT)
2296 tcp_timer_activate(tp, TT_KEEP,
2300 goto unlock_and_done;
2304 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2308 INP_WLOCK_RECHECK(inp);
2310 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2311 (TP_MAXIDLE(tp) > 0))
2312 tcp_timer_activate(tp, TT_2MSL,
2314 goto unlock_and_done;
2320 error = sooptcopyin(sopt, &optval, sizeof optval,
2325 INP_WLOCK_RECHECK(inp);
2327 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
2328 &(tp->t_outpkts) : &(tp->t_inpkts),
2332 goto unlock_and_done;
2335 case TCP_FASTOPEN: {
2336 struct tcp_fastopen tfo_optval;
2339 if (!V_tcp_fastopen_client_enable &&
2340 !V_tcp_fastopen_server_enable)
2343 error = sooptcopyin(sopt, &tfo_optval,
2344 sizeof(tfo_optval), sizeof(int));
2348 INP_WLOCK_RECHECK(inp);
2349 if ((tp->t_state != TCPS_CLOSED) &&
2350 (tp->t_state != TCPS_LISTEN)) {
2352 goto unlock_and_done;
2354 if (tfo_optval.enable) {
2355 if (tp->t_state == TCPS_LISTEN) {
2356 if (!V_tcp_fastopen_server_enable) {
2358 goto unlock_and_done;
2361 if (tp->t_tfo_pending == NULL)
2363 tcp_fastopen_alloc_counter();
2366 * If a pre-shared key was provided,
2367 * stash it in the client cookie
2368 * field of the tcpcb for use during
2371 if (sopt->sopt_valsize ==
2372 sizeof(tfo_optval)) {
2373 memcpy(tp->t_tfo_cookie.client,
2375 TCP_FASTOPEN_PSK_LEN);
2376 tp->t_tfo_client_cookie_len =
2377 TCP_FASTOPEN_PSK_LEN;
2380 tp->t_flags |= TF_FASTOPEN;
2382 tp->t_flags &= ~TF_FASTOPEN;
2383 goto unlock_and_done;
2389 error = sooptcopyin(sopt, &optval, sizeof optval,
2394 INP_WLOCK_RECHECK(inp);
2395 error = tcp_log_state_change(tp, optval);
2396 goto unlock_and_done;
2405 error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
2408 buf[sopt->sopt_valsize] = '\0';
2409 INP_WLOCK_RECHECK(inp);
2410 error = tcp_log_set_id(tp, buf);
2411 /* tcp_log_set_id() unlocks the INP. */
2418 sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
2421 buf[sopt->sopt_valsize] = '\0';
2422 INP_WLOCK_RECHECK(inp);
2423 if (sopt->sopt_name == TCP_LOGDUMP) {
2424 error = tcp_log_dump_tp_logbuf(tp, buf,
2428 tcp_log_dump_tp_bucket_logbufs(tp, buf);
2430 * tcp_log_dump_tp_bucket_logbufs() drops the
2439 error = ENOPROTOOPT;
2445 tp = intotcpcb(inp);
2446 switch (sopt->sopt_name) {
2447 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2450 if (!TCPMD5_ENABLED())
2451 return (ENOPROTOOPT);
2452 error = TCPMD5_PCBCTL(inp, sopt);
2457 optval = tp->t_flags & TF_NODELAY;
2459 error = sooptcopyout(sopt, &optval, sizeof optval);
2462 optval = tp->t_maxseg;
2464 error = sooptcopyout(sopt, &optval, sizeof optval);
2466 case TCP_REMOTE_UDP_ENCAPS_PORT:
2467 optval = ntohs(tp->t_port);
2469 error = sooptcopyout(sopt, &optval, sizeof optval);
2472 optval = tp->t_flags & TF_NOOPT;
2474 error = sooptcopyout(sopt, &optval, sizeof optval);
2477 optval = tp->t_flags & TF_NOPUSH;
2479 error = sooptcopyout(sopt, &optval, sizeof optval);
2482 tcp_fill_info(tp, &ti);
2484 error = sooptcopyout(sopt, &ti, sizeof ti);
2490 TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2493 socklen_t outsbsz = sopt->sopt_valsize;
2494 if (tp->t_stats == NULL)
2496 else if (outsbsz >= tp->t_stats->cursz)
2497 outsbsz = tp->t_stats->cursz;
2498 else if (outsbsz >= sizeof(struct statsblob))
2499 outsbsz = sizeof(struct statsblob);
2506 sbp = sopt->sopt_val;
2507 nheld = atop(round_page(((vm_offset_t)sbp) +
2508 (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2509 vm_page_t ma[nheld];
2510 if (vm_fault_quick_hold_pages(
2511 &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2512 outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2518 if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2519 SIZEOF_MEMBER(struct statsblob, flags))))
2522 INP_WLOCK_RECHECK(inp);
2523 error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2524 sbflags | SB_CLONE_USRDSTNOFAULT);
2526 sopt->sopt_valsize = outsbsz;
2528 vm_page_unhold_pages(ma, nheld);
2535 case TCP_CONGESTION:
2536 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2538 error = sooptcopyout(sopt, buf, len + 1);
2540 case TCP_MAXUNACKTIME:
2545 switch (sopt->sopt_name) {
2546 case TCP_MAXUNACKTIME:
2547 ui = TP_MAXUNACKTIME(tp) / hz;
2550 ui = TP_KEEPIDLE(tp) / hz;
2553 ui = TP_KEEPINTVL(tp) / hz;
2556 ui = TP_KEEPINIT(tp) / hz;
2559 ui = TP_KEEPCNT(tp);
2563 error = sooptcopyout(sopt, &ui, sizeof(ui));
2568 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
2569 &(tp->t_outpkts) : &(tp->t_inpkts));
2571 error = sooptcopyout(sopt, &optval, sizeof optval);
2575 optval = tp->t_flags & TF_FASTOPEN;
2577 error = sooptcopyout(sopt, &optval, sizeof optval);
2581 optval = tp->t_logstate;
2583 error = sooptcopyout(sopt, &optval, sizeof(optval));
2586 /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2587 error = tcp_log_getlogbuf(sopt, tp);
2590 len = tcp_log_get_id(tp, buf);
2592 error = sooptcopyout(sopt, buf, len + 1);
2601 case TCP_TXTLS_MODE:
2602 error = ktls_get_tx_mode(so, &optval);
2605 error = sooptcopyout(sopt, &optval,
2608 case TCP_RXTLS_MODE:
2609 error = ktls_get_rx_mode(so, &optval);
2612 error = sooptcopyout(sopt, &optval,
2617 optval = tp->t_flags & TF_LRD;
2619 error = sooptcopyout(sopt, &optval, sizeof optval);
2623 error = ENOPROTOOPT;
2630 #undef INP_WLOCK_RECHECK
2631 #undef INP_WLOCK_RECHECK_CLEANUP
2634 * Initiate (or continue) disconnect.
2635 * If embryonic state, just send reset (once).
2636 * If in ``let data drain'' option and linger null, just drop.
2637 * Otherwise (hard), mark socket disconnecting and drop
2638 * current input data; switch states based on user close, and
2639 * send segment to peer (with FIN).
2642 tcp_disconnect(struct tcpcb *tp)
2644 struct inpcb *inp = tptoinpcb(tp);
2645 struct socket *so = tptosocket(tp);
2648 INP_WLOCK_ASSERT(inp);
2651 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2652 * socket is still open.
2654 if (tp->t_state < TCPS_ESTABLISHED &&
2655 !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2658 ("tcp_disconnect: tcp_close() returned NULL"));
2659 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2660 tp = tcp_drop(tp, 0);
2662 ("tcp_disconnect: tcp_drop() returned NULL"));
2664 soisdisconnecting(so);
2665 sbflush(&so->so_rcv);
2667 if (!(inp->inp_flags & INP_DROPPED))
2668 /* Ignore stack's drop request, we already at it. */
2669 (void)tcp_output_nodrop(tp);
2674 * User issued close, and wish to trail through shutdown states:
2675 * if never received SYN, just forget it. If got a SYN from peer,
2676 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2677 * If already got a FIN from peer, then almost done; go to LAST_ACK
2678 * state. In all other cases, have already sent FIN to peer (e.g.
2679 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2680 * for peer to send FIN or not respond to keep-alives, etc.
2681 * We can let the user exit from the close as soon as the FIN is acked.
2684 tcp_usrclosed(struct tcpcb *tp)
2688 INP_WLOCK_ASSERT(tptoinpcb(tp));
2690 switch (tp->t_state) {
2693 tcp_offload_listen_stop(tp);
2695 tcp_state_change(tp, TCPS_CLOSED);
2700 * tcp_close() should never return NULL here as the socket is
2704 ("tcp_usrclosed: tcp_close() returned NULL"));
2708 case TCPS_SYN_RECEIVED:
2709 tp->t_flags |= TF_NEEDFIN;
2712 case TCPS_ESTABLISHED:
2713 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2716 case TCPS_CLOSE_WAIT:
2717 tcp_state_change(tp, TCPS_LAST_ACK);
2720 if (tp->t_acktime == 0)
2721 tp->t_acktime = ticks;
2722 if (tp->t_state >= TCPS_FIN_WAIT_2) {
2723 soisdisconnected(tptosocket(tp));
2724 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
2725 if (tp->t_state == TCPS_FIN_WAIT_2) {
2728 timeout = (tcp_fast_finwait2_recycle) ?
2729 tcp_finwait2_timeout : TP_MAXIDLE(tp);
2730 tcp_timer_activate(tp, TT_2MSL, timeout);
2737 db_print_indent(int indent)
2741 for (i = 0; i < indent; i++)
2746 db_print_tstate(int t_state)
2751 db_printf("TCPS_CLOSED");
2755 db_printf("TCPS_LISTEN");
2759 db_printf("TCPS_SYN_SENT");
2762 case TCPS_SYN_RECEIVED:
2763 db_printf("TCPS_SYN_RECEIVED");
2766 case TCPS_ESTABLISHED:
2767 db_printf("TCPS_ESTABLISHED");
2770 case TCPS_CLOSE_WAIT:
2771 db_printf("TCPS_CLOSE_WAIT");
2774 case TCPS_FIN_WAIT_1:
2775 db_printf("TCPS_FIN_WAIT_1");
2779 db_printf("TCPS_CLOSING");
2783 db_printf("TCPS_LAST_ACK");
2786 case TCPS_FIN_WAIT_2:
2787 db_printf("TCPS_FIN_WAIT_2");
2790 case TCPS_TIME_WAIT:
2791 db_printf("TCPS_TIME_WAIT");
2795 db_printf("unknown");
2801 db_print_tflags(u_int t_flags)
2806 if (t_flags & TF_ACKNOW) {
2807 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2810 if (t_flags & TF_DELACK) {
2811 db_printf("%sTF_DELACK", comma ? ", " : "");
2814 if (t_flags & TF_NODELAY) {
2815 db_printf("%sTF_NODELAY", comma ? ", " : "");
2818 if (t_flags & TF_NOOPT) {
2819 db_printf("%sTF_NOOPT", comma ? ", " : "");
2822 if (t_flags & TF_SENTFIN) {
2823 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2826 if (t_flags & TF_REQ_SCALE) {
2827 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2830 if (t_flags & TF_RCVD_SCALE) {
2831 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2834 if (t_flags & TF_REQ_TSTMP) {
2835 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2838 if (t_flags & TF_RCVD_TSTMP) {
2839 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2842 if (t_flags & TF_SACK_PERMIT) {
2843 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2846 if (t_flags & TF_NEEDSYN) {
2847 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2850 if (t_flags & TF_NEEDFIN) {
2851 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2854 if (t_flags & TF_NOPUSH) {
2855 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2858 if (t_flags & TF_PREVVALID) {
2859 db_printf("%sTF_PREVVALID", comma ? ", " : "");
2862 if (t_flags & TF_MORETOCOME) {
2863 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2866 if (t_flags & TF_SONOTCONN) {
2867 db_printf("%sTF_SONOTCONN", comma ? ", " : "");
2870 if (t_flags & TF_LASTIDLE) {
2871 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2874 if (t_flags & TF_RXWIN0SENT) {
2875 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2878 if (t_flags & TF_FASTRECOVERY) {
2879 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2882 if (t_flags & TF_CONGRECOVERY) {
2883 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2886 if (t_flags & TF_WASFRECOVERY) {
2887 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2890 if (t_flags & TF_WASCRECOVERY) {
2891 db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
2894 if (t_flags & TF_SIGNATURE) {
2895 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2898 if (t_flags & TF_FORCEDATA) {
2899 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2902 if (t_flags & TF_TSO) {
2903 db_printf("%sTF_TSO", comma ? ", " : "");
2906 if (t_flags & TF_FASTOPEN) {
2907 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2913 db_print_tflags2(u_int t_flags2)
2918 if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
2919 db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
2922 if (t_flags2 & TF2_PLPMTU_PMTUD) {
2923 db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
2926 if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
2927 db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
2930 if (t_flags2 & TF2_LOG_AUTO) {
2931 db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
2934 if (t_flags2 & TF2_DROP_AF_DATA) {
2935 db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
2938 if (t_flags2 & TF2_ECN_PERMIT) {
2939 db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
2942 if (t_flags2 & TF2_ECN_SND_CWR) {
2943 db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
2946 if (t_flags2 & TF2_ECN_SND_ECE) {
2947 db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
2950 if (t_flags2 & TF2_ACE_PERMIT) {
2951 db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
2954 if (t_flags2 & TF2_FBYTES_COMPLETE) {
2955 db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
2961 db_print_toobflags(char t_oobflags)
2966 if (t_oobflags & TCPOOB_HAVEDATA) {
2967 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2970 if (t_oobflags & TCPOOB_HADDATA) {
2971 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2977 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2980 db_print_indent(indent);
2981 db_printf("%s at %p\n", name, tp);
2985 db_print_indent(indent);
2986 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2987 TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2989 db_print_indent(indent);
2990 db_printf("t_callout: %p t_timers: %p\n",
2991 &tp->t_callout, &tp->t_timers);
2993 db_print_indent(indent);
2994 db_printf("t_state: %d (", tp->t_state);
2995 db_print_tstate(tp->t_state);
2998 db_print_indent(indent);
2999 db_printf("t_flags: 0x%x (", tp->t_flags);
3000 db_print_tflags(tp->t_flags);
3003 db_print_indent(indent);
3004 db_printf("t_flags2: 0x%x (", tp->t_flags2);
3005 db_print_tflags2(tp->t_flags2);
3008 db_print_indent(indent);
3009 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
3010 tp->snd_una, tp->snd_max, tp->snd_nxt);
3012 db_print_indent(indent);
3013 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
3014 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
3016 db_print_indent(indent);
3017 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
3018 tp->iss, tp->irs, tp->rcv_nxt);
3020 db_print_indent(indent);
3021 db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n",
3022 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
3024 db_print_indent(indent);
3025 db_printf("snd_wnd: %u snd_cwnd: %u\n",
3026 tp->snd_wnd, tp->snd_cwnd);
3028 db_print_indent(indent);
3029 db_printf("snd_ssthresh: %u snd_recover: "
3030 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3032 db_print_indent(indent);
3033 db_printf("t_rcvtime: %u t_startime: %u\n",
3034 tp->t_rcvtime, tp->t_starttime);
3036 db_print_indent(indent);
3037 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
3038 tp->t_rtttime, tp->t_rtseq);
3040 db_print_indent(indent);
3041 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
3042 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3044 db_print_indent(indent);
3045 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u\n",
3046 tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin);
3048 db_print_indent(indent);
3049 db_printf("t_rttupdated: %u max_sndwnd: %u t_softerror: %d\n",
3050 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3052 db_print_indent(indent);
3053 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3054 db_print_toobflags(tp->t_oobflags);
3055 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
3057 db_print_indent(indent);
3058 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
3059 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3061 db_print_indent(indent);
3062 db_printf("ts_recent: %u ts_recent_age: %u\n",
3063 tp->ts_recent, tp->ts_recent_age);
3065 db_print_indent(indent);
3066 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
3067 "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3069 db_print_indent(indent);
3070 db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x "
3071 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3072 tp->snd_recover_prev, tp->t_badrxtwin);
3074 db_print_indent(indent);
3075 db_printf("snd_numholes: %d snd_holes first: %p\n",
3076 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3078 db_print_indent(indent);
3079 db_printf("snd_fack: 0x%08x rcv_numsacks: %d\n",
3080 tp->snd_fack, tp->rcv_numsacks);
3082 /* Skip sackblks, sackhint. */
3084 db_print_indent(indent);
3085 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
3086 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3089 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3094 db_printf("usage: show tcpcb <addr>\n");
3097 tp = (struct tcpcb *)addr;
3099 db_print_tcpcb(tp, "tcpcb", 0);