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_require_unique port requires a globally-unique source port for each
136 * outgoing connection. The default is to require the 4-tuple to be unique.
138 VNET_DEFINE(int, tcp_require_unique_port) = 0;
139 SYSCTL_INT(_net_inet_tcp, OID_AUTO, require_unique_port,
140 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_require_unique_port), 0,
141 "Require globally-unique ephemeral port for outgoing connections");
142 #define V_tcp_require_unique_port VNET(tcp_require_unique_port)
145 * TCP attaches to socket via pru_attach(), reserving space,
146 * and an internet control block.
149 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
152 struct tcpcb *tp = NULL;
156 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
158 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
162 so->so_rcv.sb_flags |= SB_AUTOSIZE;
163 so->so_snd.sb_flags |= SB_AUTOSIZE;
164 error = in_pcballoc(so, &V_tcbinfo);
168 tp = tcp_newtcpcb(inp);
175 tp->t_state = TCPS_CLOSED;
177 TCPSTATES_INC(TCPS_CLOSED);
179 TCP_PROBE2(debug__user, tp, PRU_ATTACH);
184 * tcp_usr_detach is called when the socket layer loses its final reference
185 * to the socket, be it a file descriptor reference, a reference from TCP,
186 * etc. At this point, there is only one case in which we will keep around
187 * inpcb state: time wait.
190 tcp_usr_detach(struct socket *so)
196 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
198 KASSERT(so->so_pcb == inp && inp->inp_socket == so,
199 ("%s: socket %p inp %p mismatch", __func__, so, inp));
203 KASSERT(inp->inp_flags & INP_DROPPED ||
204 tp->t_state < TCPS_SYN_SENT,
205 ("%s: inp %p not dropped or embryonic", __func__, inp));
214 * Give the socket an address.
217 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
222 struct tcpcb *tp = NULL;
224 struct sockaddr_in *sinp;
226 sinp = (struct sockaddr_in *)nam;
227 if (nam->sa_family != AF_INET) {
229 * Preserve compatibility with old programs.
231 if (nam->sa_family != AF_UNSPEC ||
232 nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
233 sinp->sin_addr.s_addr != INADDR_ANY)
234 return (EAFNOSUPPORT);
235 nam->sa_family = AF_INET;
237 if (nam->sa_len != sizeof(*sinp))
241 * Must check for multicast addresses and disallow binding
244 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
245 return (EAFNOSUPPORT);
248 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
250 if (inp->inp_flags & INP_DROPPED) {
257 INP_HASH_WLOCK(&V_tcbinfo);
258 error = in_pcbbind(inp, nam, td->td_ucred);
259 INP_HASH_WUNLOCK(&V_tcbinfo);
261 TCP_PROBE2(debug__user, tp, PRU_BIND);
270 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
275 struct tcpcb *tp = NULL;
277 struct sockaddr_in6 *sin6;
280 sin6 = (struct sockaddr_in6 *)nam;
281 if (nam->sa_family != AF_INET6)
282 return (EAFNOSUPPORT);
283 if (nam->sa_len != sizeof(*sin6))
287 * Must check for multicast addresses and disallow binding
290 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
291 return (EAFNOSUPPORT);
294 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
296 vflagsav = inp->inp_vflag;
297 if (inp->inp_flags & INP_DROPPED) {
304 INP_HASH_WLOCK(&V_tcbinfo);
305 inp->inp_vflag &= ~INP_IPV4;
306 inp->inp_vflag |= INP_IPV6;
308 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
309 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
310 inp->inp_vflag |= INP_IPV4;
311 else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
312 struct sockaddr_in sin;
314 in6_sin6_2_sin(&sin, sin6);
315 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
316 error = EAFNOSUPPORT;
317 INP_HASH_WUNLOCK(&V_tcbinfo);
320 inp->inp_vflag |= INP_IPV4;
321 inp->inp_vflag &= ~INP_IPV6;
322 error = in_pcbbind(inp, (struct sockaddr *)&sin,
324 INP_HASH_WUNLOCK(&V_tcbinfo);
329 error = in6_pcbbind(inp, nam, td->td_ucred);
330 INP_HASH_WUNLOCK(&V_tcbinfo);
333 inp->inp_vflag = vflagsav;
334 TCP_PROBE2(debug__user, tp, PRU_BIND);
342 * Prepare to accept connections.
345 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
349 struct tcpcb *tp = NULL;
352 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
354 if (inp->inp_flags & INP_DROPPED) {
360 error = solisten_proto_check(so);
365 if (inp->inp_lport == 0) {
366 INP_HASH_WLOCK(&V_tcbinfo);
367 error = in_pcbbind(inp, NULL, td->td_ucred);
368 INP_HASH_WUNLOCK(&V_tcbinfo);
371 tcp_state_change(tp, TCPS_LISTEN);
372 solisten_proto(so, backlog);
374 if ((so->so_options & SO_NO_OFFLOAD) == 0)
375 tcp_offload_listen_start(tp);
378 solisten_proto_abort(so);
382 if (IS_FASTOPEN(tp->t_flags))
383 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
386 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
394 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
398 struct tcpcb *tp = NULL;
402 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
404 if (inp->inp_flags & INP_DROPPED) {
408 vflagsav = inp->inp_vflag;
411 error = solisten_proto_check(so);
416 INP_HASH_WLOCK(&V_tcbinfo);
417 if (inp->inp_lport == 0) {
418 inp->inp_vflag &= ~INP_IPV4;
419 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
420 inp->inp_vflag |= INP_IPV4;
421 error = in6_pcbbind(inp, NULL, td->td_ucred);
423 INP_HASH_WUNLOCK(&V_tcbinfo);
425 tcp_state_change(tp, TCPS_LISTEN);
426 solisten_proto(so, backlog);
428 if ((so->so_options & SO_NO_OFFLOAD) == 0)
429 tcp_offload_listen_start(tp);
432 solisten_proto_abort(so);
436 if (IS_FASTOPEN(tp->t_flags))
437 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
440 inp->inp_vflag = vflagsav;
443 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
451 * Initiate connection to peer.
452 * Create a template for use in transmissions on this connection.
453 * Enter SYN_SENT state, and mark socket as connecting.
454 * Start keep-alive timer, and seed output sequence space.
455 * Send initial segment on connection.
458 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
460 struct epoch_tracker et;
463 struct tcpcb *tp = NULL;
464 struct sockaddr_in *sinp;
466 sinp = (struct sockaddr_in *)nam;
467 if (nam->sa_family != AF_INET)
468 return (EAFNOSUPPORT);
469 if (nam->sa_len != sizeof (*sinp))
473 * Must disallow TCP ``connections'' to multicast addresses.
475 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
476 return (EAFNOSUPPORT);
477 if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST)
479 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
483 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
485 if (inp->inp_flags & INP_DROPPED) {
486 error = ECONNREFUSED;
489 if (SOLISTENING(so)) {
495 if ((error = tcp_connect(tp, nam, td)) != 0)
498 if (registered_toedevs > 0 &&
499 (so->so_options & SO_NO_OFFLOAD) == 0 &&
500 (error = tcp_offload_connect(so, nam)) == 0)
503 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
504 error = tcp_output(tp);
505 KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
506 ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
510 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
518 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
520 struct epoch_tracker et;
523 struct tcpcb *tp = NULL;
524 struct sockaddr_in6 *sin6;
528 sin6 = (struct sockaddr_in6 *)nam;
529 if (nam->sa_family != AF_INET6)
530 return (EAFNOSUPPORT);
531 if (nam->sa_len != sizeof (*sin6))
535 * Must disallow TCP ``connections'' to multicast addresses.
537 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
538 return (EAFNOSUPPORT);
541 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
543 vflagsav = inp->inp_vflag;
544 incflagsav = inp->inp_inc.inc_flags;
545 if (inp->inp_flags & INP_DROPPED) {
546 error = ECONNREFUSED;
549 if (SOLISTENING(so)) {
556 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
557 * therefore probably require the hash lock, which isn't held here.
558 * Is this a significant problem?
560 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
561 struct sockaddr_in sin;
563 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
567 if ((inp->inp_vflag & INP_IPV4) == 0) {
568 error = EAFNOSUPPORT;
572 in6_sin6_2_sin(&sin, sin6);
573 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
574 error = EAFNOSUPPORT;
577 if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
581 if ((error = prison_remote_ip4(td->td_ucred,
582 &sin.sin_addr)) != 0)
584 inp->inp_vflag |= INP_IPV4;
585 inp->inp_vflag &= ~INP_IPV6;
587 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
590 if (registered_toedevs > 0 &&
591 (so->so_options & SO_NO_OFFLOAD) == 0 &&
592 (error = tcp_offload_connect(so, nam)) == 0)
595 error = tcp_output(tp);
598 if ((inp->inp_vflag & INP_IPV6) == 0) {
599 error = EAFNOSUPPORT;
604 if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
606 inp->inp_vflag &= ~INP_IPV4;
607 inp->inp_vflag |= INP_IPV6;
608 inp->inp_inc.inc_flags |= INC_ISIPV6;
610 if ((error = tcp6_connect(tp, nam, td)) != 0)
613 if (registered_toedevs > 0 &&
614 (so->so_options & SO_NO_OFFLOAD) == 0 &&
615 (error = tcp_offload_connect(so, nam)) == 0)
618 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
619 error = tcp_output(tp);
623 KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
624 ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
626 * If the implicit bind in the connect call fails, restore
627 * the flags we modified.
629 if (error != 0 && inp->inp_lport == 0) {
630 inp->inp_vflag = vflagsav;
631 inp->inp_inc.inc_flags = incflagsav;
634 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
641 * Initiate disconnect from peer.
642 * If connection never passed embryonic stage, just drop;
643 * else if don't need to let data drain, then can just drop anyways,
644 * else have to begin TCP shutdown process: mark socket disconnecting,
645 * drain unread data, state switch to reflect user close, and
646 * send segment (e.g. FIN) to peer. Socket will be really disconnected
647 * when peer sends FIN and acks ours.
649 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
652 tcp_usr_disconnect(struct socket *so)
655 struct tcpcb *tp = NULL;
656 struct epoch_tracker et;
661 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
663 if (inp->inp_flags & INP_DROPPED) {
670 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
678 * Accept a connection. Essentially all the work is done at higher levels;
679 * just return the address of the peer, storing through addr.
682 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
685 struct inpcb *inp = NULL;
687 struct tcpcb *tp = NULL;
692 if (so->so_state & SS_ISDISCONNECTED)
693 return (ECONNABORTED);
696 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
698 if (inp->inp_flags & INP_DROPPED) {
699 error = ECONNABORTED;
707 * We inline in_getpeeraddr and COMMON_END here, so that we can
708 * copy the data of interest and defer the malloc until after we
711 port = inp->inp_fport;
712 addr = inp->inp_faddr;
715 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
718 *nam = in_sockaddr(port, &addr);
725 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
727 struct inpcb *inp = NULL;
730 struct tcpcb *tp = NULL;
733 struct in6_addr addr6;
734 struct epoch_tracker et;
738 if (so->so_state & SS_ISDISCONNECTED)
739 return (ECONNABORTED);
742 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
745 if (inp->inp_flags & INP_DROPPED) {
746 error = ECONNABORTED;
754 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
755 * copy the data of interest and defer the malloc until after we
758 if (inp->inp_vflag & INP_IPV4) {
760 port = inp->inp_fport;
761 addr = inp->inp_faddr;
763 port = inp->inp_fport;
764 addr6 = inp->in6p_faddr;
768 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
773 *nam = in6_v4mapsin6_sockaddr(port, &addr);
775 *nam = in6_sockaddr(port, &addr6);
782 * Mark the connection as being incapable of further output.
785 tcp_usr_shutdown(struct socket *so)
789 struct tcpcb *tp = NULL;
790 struct epoch_tracker et;
793 KASSERT(inp != NULL, ("inp == NULL"));
795 if (inp->inp_flags & INP_DROPPED) {
803 if (!(inp->inp_flags & INP_DROPPED))
804 error = tcp_output_nodrop(tp);
805 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
806 error = tcp_unlock_or_drop(tp, error);
813 * After a receive, possibly send window update to peer.
816 tcp_usr_rcvd(struct socket *so, int flags)
818 struct epoch_tracker et;
820 struct tcpcb *tp = NULL;
821 int outrv = 0, error = 0;
824 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
826 if (inp->inp_flags & INP_DROPPED) {
833 * For passively-created TFO connections, don't attempt a window
834 * update while still in SYN_RECEIVED as this may trigger an early
835 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
836 * application response data, or failing that, when the DELACK timer
839 if (IS_FASTOPEN(tp->t_flags) &&
840 (tp->t_state == TCPS_SYN_RECEIVED))
843 if (tp->t_flags & TF_TOE)
844 tcp_offload_rcvd(tp);
847 outrv = tcp_output_nodrop(tp);
849 TCP_PROBE2(debug__user, tp, PRU_RCVD);
850 (void) tcp_unlock_or_drop(tp, outrv);
856 * Do a send by putting data in output queue and updating urgent
857 * marker if URG set. Possibly send more data. Unlike the other
858 * pru_*() routines, the mbuf chains are our responsibility. We
859 * must either enqueue them or free them. The other pru_* routines
860 * generally are caller-frees.
863 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
864 struct sockaddr *nam, struct mbuf *control, struct thread *td)
866 struct epoch_tracker et;
869 struct tcpcb *tp = NULL;
872 struct sockaddr_in sin;
874 struct sockaddr_in *sinp;
883 if (control != NULL) {
884 /* TCP doesn't do control messages (rights, creds, etc) */
885 if (control->m_len) {
889 m_freem(control); /* empty control, just free it */
893 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
895 if (inp->inp_flags & INP_DROPPED) {
896 if (m != NULL && (flags & PRUS_NOTREADY) == 0)
902 vflagsav = inp->inp_vflag;
903 incflagsav = inp->inp_inc.inc_flags;
904 restoreflags = false;
908 if ((flags & PRUS_OOB) != 0 &&
909 (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
912 if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
913 if (tp->t_state == TCPS_LISTEN) {
917 switch (nam->sa_family) {
920 sinp = (struct sockaddr_in *)nam;
921 if (sinp->sin_len != sizeof(struct sockaddr_in)) {
925 if ((inp->inp_vflag & INP_IPV6) != 0) {
926 error = EAFNOSUPPORT;
929 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
930 error = EAFNOSUPPORT;
933 if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
937 if ((error = prison_remote_ip4(td->td_ucred,
948 struct sockaddr_in6 *sin6;
950 sin6 = (struct sockaddr_in6 *)nam;
951 if (sin6->sin6_len != sizeof(*sin6)) {
955 if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
956 error = EAFNOSUPPORT;
959 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
960 error = EAFNOSUPPORT;
963 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
965 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
969 if ((inp->inp_vflag & INP_IPV4) == 0) {
970 error = EAFNOSUPPORT;
974 inp->inp_vflag &= ~INP_IPV6;
976 in6_sin6_2_sin(sinp, sin6);
978 ntohl(sinp->sin_addr.s_addr))) {
979 error = EAFNOSUPPORT;
982 if ((error = prison_remote_ip4(td->td_ucred,
987 error = EAFNOSUPPORT;
991 if ((inp->inp_vflag & INP_IPV6) == 0) {
992 error = EAFNOSUPPORT;
996 inp->inp_vflag &= ~INP_IPV4;
997 inp->inp_inc.inc_flags |= INC_ISIPV6;
998 if ((error = prison_remote_ip6(td->td_ucred,
1007 error = EAFNOSUPPORT;
1011 if (!(flags & PRUS_OOB)) {
1012 if (tp->t_acktime == 0)
1013 tp->t_acktime = ticks;
1014 sbappendstream(&so->so_snd, m, flags);
1016 if (nam && tp->t_state < TCPS_SYN_SENT) {
1017 KASSERT(tp->t_state == TCPS_CLOSED,
1018 ("%s: tp %p is listening", __func__, tp));
1021 * Do implied connect if not yet connected,
1022 * initialize window to default value, and
1023 * initialize maxseg using peer's cached MSS.
1027 error = tcp6_connect(tp, nam, td);
1029 #if defined(INET6) && defined(INET)
1033 error = tcp_connect(tp,
1034 (struct sockaddr *)sinp, td);
1037 * The bind operation in tcp_connect succeeded. We
1038 * no longer want to restore the flags if later
1041 if (error == 0 || inp->inp_lport != 0)
1042 restoreflags = false;
1045 /* m is freed if PRUS_NOTREADY is unset. */
1046 sbflush(&so->so_snd);
1049 if (IS_FASTOPEN(tp->t_flags))
1050 tcp_fastopen_connect(tp);
1052 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1056 if (flags & PRUS_EOF) {
1058 * Close the send side of the connection after
1064 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1065 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1066 (tp->t_fbyte_out == 0) &&
1067 (so->so_snd.sb_ccc > 0)) {
1068 tp->t_fbyte_out = ticks;
1069 if (tp->t_fbyte_out == 0)
1070 tp->t_fbyte_out = 1;
1071 if (tp->t_fbyte_out && tp->t_fbyte_in)
1072 tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1074 if (!(inp->inp_flags & INP_DROPPED) &&
1075 !(flags & PRUS_NOTREADY)) {
1076 if (flags & PRUS_MORETOCOME)
1077 tp->t_flags |= TF_MORETOCOME;
1078 error = tcp_output_nodrop(tp);
1079 if (flags & PRUS_MORETOCOME)
1080 tp->t_flags &= ~TF_MORETOCOME;
1084 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1086 SOCKBUF_LOCK(&so->so_snd);
1087 if (sbspace(&so->so_snd) < -512) {
1088 SOCKBUF_UNLOCK(&so->so_snd);
1093 * According to RFC961 (Assigned Protocols),
1094 * the urgent pointer points to the last octet
1095 * of urgent data. We continue, however,
1096 * to consider it to indicate the first octet
1097 * of data past the urgent section.
1098 * Otherwise, snd_up should be one lower.
1100 if (tp->t_acktime == 0)
1101 tp->t_acktime = ticks;
1102 sbappendstream_locked(&so->so_snd, m, flags);
1103 SOCKBUF_UNLOCK(&so->so_snd);
1105 if (nam && tp->t_state < TCPS_SYN_SENT) {
1107 * Do implied connect if not yet connected,
1108 * initialize window to default value, and
1109 * initialize maxseg using peer's cached MSS.
1113 * Not going to contemplate SYN|URG
1115 if (IS_FASTOPEN(tp->t_flags))
1116 tp->t_flags &= ~TF_FASTOPEN;
1119 error = tcp6_connect(tp, nam, td);
1121 #if defined(INET6) && defined(INET)
1125 error = tcp_connect(tp,
1126 (struct sockaddr *)sinp, td);
1129 * The bind operation in tcp_connect succeeded. We
1130 * no longer want to restore the flags if later
1133 if (error == 0 || inp->inp_lport != 0)
1134 restoreflags = false;
1137 /* m is freed if PRUS_NOTREADY is unset. */
1138 sbflush(&so->so_snd);
1141 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1144 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1145 if ((flags & PRUS_NOTREADY) == 0) {
1146 tp->t_flags |= TF_FORCEDATA;
1147 error = tcp_output_nodrop(tp);
1148 tp->t_flags &= ~TF_FORCEDATA;
1151 TCP_LOG_EVENT(tp, NULL,
1152 &inp->inp_socket->so_rcv,
1153 &inp->inp_socket->so_snd,
1154 TCP_LOG_USERSEND, error,
1159 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
1160 * responsible for freeing memory.
1162 if (m != NULL && (flags & PRUS_NOTREADY) == 0)
1166 * If the request was unsuccessful and we changed flags,
1167 * restore the original flags.
1169 if (error != 0 && restoreflags) {
1170 inp->inp_vflag = vflagsav;
1171 inp->inp_inc.inc_flags = incflagsav;
1173 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1174 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1175 error = tcp_unlock_or_drop(tp, error);
1181 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1183 struct epoch_tracker et;
1188 inp = sotoinpcb(so);
1190 if (inp->inp_flags & INP_DROPPED) {
1192 mb_free_notready(m, count);
1193 return (ECONNRESET);
1195 tp = intotcpcb(inp);
1197 SOCKBUF_LOCK(&so->so_snd);
1198 error = sbready(&so->so_snd, m, count);
1199 SOCKBUF_UNLOCK(&so->so_snd);
1204 NET_EPOCH_ENTER(et);
1205 error = tcp_output_unlock(tp);
1212 * Abort the TCP. Drop the connection abruptly.
1215 tcp_usr_abort(struct socket *so)
1218 struct tcpcb *tp = NULL;
1219 struct epoch_tracker et;
1221 inp = sotoinpcb(so);
1222 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1224 NET_EPOCH_ENTER(et);
1226 KASSERT(inp->inp_socket != NULL,
1227 ("tcp_usr_abort: inp_socket == NULL"));
1230 * If we still have full TCP state, and we're not dropped, drop.
1232 if (!(inp->inp_flags & INP_DROPPED)) {
1233 tp = intotcpcb(inp);
1234 tp = tcp_drop(tp, ECONNABORTED);
1237 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1239 if (!(inp->inp_flags & INP_DROPPED)) {
1241 inp->inp_flags |= INP_SOCKREF;
1249 * TCP socket is closed. Start friendly disconnect.
1252 tcp_usr_close(struct socket *so)
1255 struct tcpcb *tp = NULL;
1256 struct epoch_tracker et;
1258 inp = sotoinpcb(so);
1259 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1261 NET_EPOCH_ENTER(et);
1263 KASSERT(inp->inp_socket != NULL,
1264 ("tcp_usr_close: inp_socket == NULL"));
1267 * If we still have full TCP state, and we're not dropped, initiate
1270 if (!(inp->inp_flags & INP_DROPPED)) {
1271 tp = intotcpcb(inp);
1272 tp->t_flags |= TF_CLOSED;
1274 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1276 if (!(inp->inp_flags & INP_DROPPED)) {
1278 inp->inp_flags |= INP_SOCKREF;
1285 tcp_pru_options_support(struct tcpcb *tp, int flags)
1288 * If the specific TCP stack has a pru_options
1289 * specified then it does not always support
1290 * all the PRU_XX options and we must ask it.
1291 * If the function is not specified then all
1292 * of the PRU_XX options are supported.
1296 if (tp->t_fb->tfb_pru_options) {
1297 ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1303 * Receive out-of-band data.
1306 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1310 struct tcpcb *tp = NULL;
1312 inp = sotoinpcb(so);
1313 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1315 if (inp->inp_flags & INP_DROPPED) {
1319 tp = intotcpcb(inp);
1320 error = tcp_pru_options_support(tp, PRUS_OOB);
1324 if ((so->so_oobmark == 0 &&
1325 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1326 so->so_options & SO_OOBINLINE ||
1327 tp->t_oobflags & TCPOOB_HADDATA) {
1331 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1332 error = EWOULDBLOCK;
1336 *mtod(m, caddr_t) = tp->t_iobc;
1337 if ((flags & MSG_PEEK) == 0)
1338 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1341 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1347 struct protosw tcp_protosw = {
1348 .pr_type = SOCK_STREAM,
1349 .pr_protocol = IPPROTO_TCP,
1350 .pr_flags = PR_CONNREQUIRED | PR_IMPLOPCL | PR_WANTRCVD |
1352 .pr_ctloutput = tcp_ctloutput,
1353 .pr_abort = tcp_usr_abort,
1354 .pr_accept = tcp_usr_accept,
1355 .pr_attach = tcp_usr_attach,
1356 .pr_bind = tcp_usr_bind,
1357 .pr_connect = tcp_usr_connect,
1358 .pr_control = in_control,
1359 .pr_detach = tcp_usr_detach,
1360 .pr_disconnect = tcp_usr_disconnect,
1361 .pr_listen = tcp_usr_listen,
1362 .pr_peeraddr = in_getpeeraddr,
1363 .pr_rcvd = tcp_usr_rcvd,
1364 .pr_rcvoob = tcp_usr_rcvoob,
1365 .pr_send = tcp_usr_send,
1366 .pr_ready = tcp_usr_ready,
1367 .pr_shutdown = tcp_usr_shutdown,
1368 .pr_sockaddr = in_getsockaddr,
1369 .pr_sosetlabel = in_pcbsosetlabel,
1370 .pr_close = tcp_usr_close,
1375 struct protosw tcp6_protosw = {
1376 .pr_type = SOCK_STREAM,
1377 .pr_protocol = IPPROTO_TCP,
1378 .pr_flags = PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD |
1380 .pr_ctloutput = tcp_ctloutput,
1381 .pr_abort = tcp_usr_abort,
1382 .pr_accept = tcp6_usr_accept,
1383 .pr_attach = tcp_usr_attach,
1384 .pr_bind = tcp6_usr_bind,
1385 .pr_connect = tcp6_usr_connect,
1386 .pr_control = in6_control,
1387 .pr_detach = tcp_usr_detach,
1388 .pr_disconnect = tcp_usr_disconnect,
1389 .pr_listen = tcp6_usr_listen,
1390 .pr_peeraddr = in6_mapped_peeraddr,
1391 .pr_rcvd = tcp_usr_rcvd,
1392 .pr_rcvoob = tcp_usr_rcvoob,
1393 .pr_send = tcp_usr_send,
1394 .pr_ready = tcp_usr_ready,
1395 .pr_shutdown = tcp_usr_shutdown,
1396 .pr_sockaddr = in6_mapped_sockaddr,
1397 .pr_sosetlabel = in_pcbsosetlabel,
1398 .pr_close = tcp_usr_close,
1404 * Common subroutine to open a TCP connection to remote host specified
1405 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1406 * port number if needed. Call in_pcbconnect_setup to do the routing and
1407 * to choose a local host address (interface). If there is an existing
1408 * incarnation of the same connection in TIME-WAIT state and if the remote
1409 * host was sending CC options and if the connection duration was < MSL, then
1410 * truncate the previous TIME-WAIT state and proceed.
1411 * Initialize connection parameters and enter SYN-SENT state.
1414 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1416 struct inpcb *inp = tptoinpcb(tp), *oinp;
1417 struct socket *so = tptosocket(tp);
1418 struct in_addr laddr;
1423 INP_WLOCK_ASSERT(inp);
1424 INP_HASH_WLOCK(&V_tcbinfo);
1426 if (V_tcp_require_unique_port && inp->inp_lport == 0) {
1427 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1433 * Cannot simply call in_pcbconnect, because there might be an
1434 * earlier incarnation of this same connection still in
1435 * TIME_WAIT state, creating an ADDRINUSE error.
1437 laddr = inp->inp_laddr;
1438 lport = inp->inp_lport;
1439 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1440 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1441 if (error && oinp == NULL)
1447 /* Handle initial bind if it hadn't been done in advance. */
1448 if (inp->inp_lport == 0) {
1449 inp->inp_lport = lport;
1450 if (in_pcbinshash(inp) != 0) {
1456 inp->inp_laddr = laddr;
1458 INP_HASH_WUNLOCK(&V_tcbinfo);
1461 * Compute window scaling to request:
1462 * Scale to fit into sweet spot. See tcp_syncache.c.
1463 * XXX: This should move to tcp_output().
1465 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1466 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1467 tp->request_r_scale++;
1470 TCPSTAT_INC(tcps_connattempt);
1471 tcp_state_change(tp, TCPS_SYN_SENT);
1472 tp->iss = tcp_new_isn(&inp->inp_inc);
1473 if (tp->t_flags & TF_REQ_TSTMP)
1474 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1475 tcp_sendseqinit(tp);
1480 INP_HASH_WUNLOCK(&V_tcbinfo);
1487 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1489 struct inpcb *inp = tptoinpcb(tp);
1492 INP_WLOCK_ASSERT(inp);
1493 INP_HASH_WLOCK(&V_tcbinfo);
1495 if (V_tcp_require_unique_port && inp->inp_lport == 0) {
1496 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1500 error = in6_pcbconnect(inp, nam, td->td_ucred);
1503 INP_HASH_WUNLOCK(&V_tcbinfo);
1505 /* Compute window scaling to request. */
1506 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1507 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1508 tp->request_r_scale++;
1510 soisconnecting(inp->inp_socket);
1511 TCPSTAT_INC(tcps_connattempt);
1512 tcp_state_change(tp, TCPS_SYN_SENT);
1513 tp->iss = tcp_new_isn(&inp->inp_inc);
1514 if (tp->t_flags & TF_REQ_TSTMP)
1515 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1516 tcp_sendseqinit(tp);
1521 INP_HASH_WUNLOCK(&V_tcbinfo);
1527 * Export TCP internal state information via a struct tcp_info, based on the
1528 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1529 * (TCP state machine, etc). We export all information using FreeBSD-native
1530 * constants -- for example, the numeric values for tcpi_state will differ
1534 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1537 INP_WLOCK_ASSERT(tptoinpcb(tp));
1538 bzero(ti, sizeof(*ti));
1540 ti->tcpi_state = tp->t_state;
1541 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1542 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1543 if (tp->t_flags & TF_SACK_PERMIT)
1544 ti->tcpi_options |= TCPI_OPT_SACK;
1545 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1546 ti->tcpi_options |= TCPI_OPT_WSCALE;
1547 ti->tcpi_snd_wscale = tp->snd_scale;
1548 ti->tcpi_rcv_wscale = tp->rcv_scale;
1550 if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
1551 ti->tcpi_options |= TCPI_OPT_ECN;
1553 ti->tcpi_rto = tp->t_rxtcur * tick;
1554 ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1555 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1556 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1558 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1559 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1562 * FreeBSD-specific extension fields for tcp_info.
1564 ti->tcpi_rcv_space = tp->rcv_wnd;
1565 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1566 ti->tcpi_snd_wnd = tp->snd_wnd;
1567 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1568 ti->tcpi_snd_nxt = tp->snd_nxt;
1569 ti->tcpi_snd_mss = tp->t_maxseg;
1570 ti->tcpi_rcv_mss = tp->t_maxseg;
1571 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1572 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1573 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1575 if (tp->t_flags & TF_TOE) {
1576 ti->tcpi_options |= TCPI_OPT_TOE;
1577 tcp_offload_tcp_info(tp, ti);
1581 * AccECN related counters.
1583 if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) ==
1584 (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
1586 * Internal counter starts at 5 for AccECN
1587 * but 0 for RFC3168 ECN.
1589 ti->tcpi_delivered_ce = tp->t_scep - 5;
1591 ti->tcpi_delivered_ce = tp->t_scep;
1592 ti->tcpi_received_ce = tp->t_rcep;
1596 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1597 * socket option arguments. When it re-acquires the lock after the copy, it
1598 * has to revalidate that the connection is still valid for the socket
1601 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \
1603 if (inp->inp_flags & INP_DROPPED) { \
1606 return (ECONNRESET); \
1608 tp = intotcpcb(inp); \
1610 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1613 tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt)
1615 struct socket *so = inp->inp_socket;
1616 struct tcpcb *tp = intotcpcb(inp);
1619 MPASS(sopt->sopt_dir == SOPT_SET);
1620 INP_WLOCK_ASSERT(inp);
1621 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1622 ("inp_flags == %x", inp->inp_flags));
1623 KASSERT(so != NULL, ("inp_socket == NULL"));
1625 if (sopt->sopt_level != IPPROTO_TCP) {
1628 if (inp->inp_vflag & INP_IPV6PROTO)
1629 error = ip6_ctloutput(so, sopt);
1631 #if defined(INET6) && defined(INET)
1635 error = ip_ctloutput(so, sopt);
1638 * When an IP-level socket option affects TCP, pass control
1639 * down to stack tfb_tcp_ctloutput, otherwise return what
1640 * IP level returned.
1642 switch (sopt->sopt_level) {
1645 if ((inp->inp_vflag & INP_IPV6PROTO) == 0)
1647 switch (sopt->sopt_name) {
1649 /* Notify tcp stacks that care (e.g. RACK). */
1651 case IPV6_USE_MIN_MTU:
1652 /* Update t_maxseg accordingly. */
1661 switch (sopt->sopt_name) {
1663 inp->inp_ip_tos &= ~IPTOS_ECN_MASK;
1666 /* Notify tcp stacks that care (e.g. RACK). */
1677 if (inp->inp_flags & INP_DROPPED) {
1679 return (ECONNRESET);
1681 } else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1683 * Protect the TCP option TCP_FUNCTION_BLK so
1684 * that a sub-function can *never* overwrite this.
1686 struct tcp_function_set fsn;
1687 struct tcp_function_block *blk;
1690 error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1695 if (inp->inp_flags & INP_DROPPED) {
1697 return (ECONNRESET);
1699 tp = intotcpcb(inp);
1701 blk = find_and_ref_tcp_functions(&fsn);
1706 if (tp->t_fb == blk) {
1707 /* You already have this */
1708 refcount_release(&blk->tfb_refcnt);
1712 if (tp->t_state != TCPS_CLOSED) {
1714 * The user has advanced the state
1715 * past the initial point, we may not
1716 * be able to switch.
1718 if (blk->tfb_tcp_handoff_ok != NULL) {
1720 * Does the stack provide a
1721 * query mechanism, if so it may
1722 * still be possible?
1724 error = (*blk->tfb_tcp_handoff_ok)(tp);
1728 refcount_release(&blk->tfb_refcnt);
1733 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1734 refcount_release(&blk->tfb_refcnt);
1739 * Release the old refcnt, the
1740 * lookup acquired a ref on the
1743 if (tp->t_fb->tfb_tcp_fb_fini) {
1744 struct epoch_tracker et;
1746 * Tell the stack to cleanup with 0 i.e.
1747 * the tcb is not going away.
1749 NET_EPOCH_ENTER(et);
1750 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1754 /* Assure that we are not on any hpts */
1755 tcp_hpts_remove(tptoinpcb(tp));
1757 if (blk->tfb_tcp_fb_init) {
1758 error = (*blk->tfb_tcp_fb_init)(tp);
1760 refcount_release(&blk->tfb_refcnt);
1761 if (tp->t_fb->tfb_tcp_fb_init) {
1762 if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) {
1763 /* Fall back failed, drop the connection */
1772 refcount_release(&tp->t_fb->tfb_refcnt);
1775 if (tp->t_flags & TF_TOE) {
1776 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1785 /* Pass in the INP locked, callee must unlock it. */
1786 return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1790 tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1792 struct socket *so = inp->inp_socket;
1793 struct tcpcb *tp = intotcpcb(inp);
1796 MPASS(sopt->sopt_dir == SOPT_GET);
1797 INP_WLOCK_ASSERT(inp);
1798 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1799 ("inp_flags == %x", inp->inp_flags));
1800 KASSERT(so != NULL, ("inp_socket == NULL"));
1802 if (sopt->sopt_level != IPPROTO_TCP) {
1805 if (inp->inp_vflag & INP_IPV6PROTO)
1806 error = ip6_ctloutput(so, sopt);
1808 #if defined(INET6) && defined(INET)
1812 error = ip_ctloutput(so, sopt);
1816 if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1817 (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1818 struct tcp_function_set fsn;
1820 if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1821 memset(&fsn, 0, sizeof(fsn));
1822 find_tcp_function_alias(tp->t_fb, &fsn);
1824 strncpy(fsn.function_set_name,
1825 tp->t_fb->tfb_tcp_block_name,
1826 TCP_FUNCTION_NAME_LEN_MAX);
1827 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1829 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1831 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1835 /* Pass in the INP locked, callee must unlock it. */
1836 return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1840 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1844 inp = sotoinpcb(so);
1845 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1848 if (inp->inp_flags & INP_DROPPED) {
1850 return (ECONNRESET);
1852 if (sopt->sopt_dir == SOPT_SET)
1853 return (tcp_ctloutput_set(inp, sopt));
1854 else if (sopt->sopt_dir == SOPT_GET)
1855 return (tcp_ctloutput_get(inp, sopt));
1857 panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
1861 * If this assert becomes untrue, we need to change the size of the buf
1862 * variable in tcp_default_ctloutput().
1865 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1866 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1871 copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1873 struct tls_enable_v0 tls_v0;
1876 if (sopt->sopt_valsize == sizeof(tls_v0)) {
1877 error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1881 memset(tls, 0, sizeof(*tls));
1882 tls->cipher_key = tls_v0.cipher_key;
1883 tls->iv = tls_v0.iv;
1884 tls->auth_key = tls_v0.auth_key;
1885 tls->cipher_algorithm = tls_v0.cipher_algorithm;
1886 tls->cipher_key_len = tls_v0.cipher_key_len;
1887 tls->iv_len = tls_v0.iv_len;
1888 tls->auth_algorithm = tls_v0.auth_algorithm;
1889 tls->auth_key_len = tls_v0.auth_key_len;
1890 tls->flags = tls_v0.flags;
1891 tls->tls_vmajor = tls_v0.tls_vmajor;
1892 tls->tls_vminor = tls_v0.tls_vminor;
1896 return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1900 extern struct cc_algo newreno_cc_algo;
1903 tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
1905 struct cc_algo *algo;
1908 struct cc_var cc_mem;
1909 char buf[TCP_CA_NAME_MAX];
1914 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1917 buf[sopt->sopt_valsize] = '\0';
1919 STAILQ_FOREACH(algo, &cc_list, entries) {
1920 if (strncmp(buf, algo->name,
1921 TCP_CA_NAME_MAX) == 0) {
1922 if (algo->flags & CC_MODULE_BEING_REMOVED) {
1923 /* We can't "see" modules being unloaded */
1934 * With a reference the algorithm cannot be removed
1935 * so we hold a reference through the change process.
1939 if (algo->cb_init != NULL) {
1940 /* We can now pre-get the memory for the CC */
1941 mem_sz = (*algo->cc_data_sz)();
1945 ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
1952 * Make sure its all clean and zero and also get
1955 memset(&cc_mem, 0, sizeof(cc_mem));
1957 if (inp->inp_flags & INP_DROPPED) {
1960 free(ptr, M_CC_MEM);
1961 /* Release our temp reference */
1965 return (ECONNRESET);
1967 tp = intotcpcb(inp);
1969 memset(ptr, 0, mem_sz);
1970 cc_mem.ccvc.tcp = tp;
1972 * We once again hold a write lock over the tcb so it's
1973 * safe to do these things without ordering concerns.
1974 * Note here we init into stack memory.
1976 if (algo->cb_init != NULL)
1977 error = algo->cb_init(&cc_mem, ptr);
1981 * The CC algorithms, when given their memory
1982 * should not fail we could in theory have a
1987 * Touchdown, lets go ahead and move the
1988 * connection to the new CC module by
1989 * copying in the cc_mem after we call
1990 * the old ones cleanup (if any).
1992 if (CC_ALGO(tp)->cb_destroy != NULL)
1993 CC_ALGO(tp)->cb_destroy(&tp->t_ccv);
1994 /* Detach the old CC from the tcpcb */
1996 /* Copy in our temp memory that was inited */
1997 memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var));
1998 /* Now attach the new, which takes a reference */
1999 cc_attach(tp, algo);
2000 /* Ok now are we where we have gotten past any conn_init? */
2001 if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
2002 /* Yep run the connection init for the new CC */
2003 CC_ALGO(tp)->conn_init(&tp->t_ccv);
2006 free(ptr, M_CC_MEM);
2008 /* Now lets release our temp reference */
2016 tcp_default_ctloutput(struct inpcb *inp, struct sockopt *sopt)
2018 struct tcpcb *tp = intotcpcb(inp);
2019 int error, opt, optval;
2023 struct tls_enable tls;
2024 struct socket *so = inp->inp_socket;
2026 char *pbuf, buf[TCP_LOG_ID_LEN];
2028 struct statsblob *sbp;
2032 INP_WLOCK_ASSERT(inp);
2033 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
2034 ("inp_flags == %x", inp->inp_flags));
2035 KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
2037 switch (sopt->sopt_level) {
2040 MPASS(inp->inp_vflag & INP_IPV6PROTO);
2041 switch (sopt->sopt_name) {
2042 case IPV6_USE_MIN_MTU:
2043 tcp6_use_min_mtu(tp);
2057 * For TCP_CCALGOOPT forward the control to CC module, for both
2058 * SOPT_SET and SOPT_GET.
2060 switch (sopt->sopt_name) {
2063 if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2065 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2066 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2067 sopt->sopt_valsize);
2072 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2073 if (CC_ALGO(tp)->ctl_output != NULL)
2074 error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf);
2078 if (error == 0 && sopt->sopt_dir == SOPT_GET)
2079 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2084 switch (sopt->sopt_dir) {
2086 switch (sopt->sopt_name) {
2087 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2090 if (!TCPMD5_ENABLED())
2091 return (ENOPROTOOPT);
2092 error = TCPMD5_PCBCTL(inp, sopt);
2095 INP_WLOCK_RECHECK(inp);
2096 goto unlock_and_done;
2103 error = sooptcopyin(sopt, &optval, sizeof optval,
2108 INP_WLOCK_RECHECK(inp);
2109 switch (sopt->sopt_name) {
2120 opt = 0; /* dead code to fool gcc */
2127 tp->t_flags &= ~opt;
2130 if (tp->t_flags & TF_TOE) {
2131 tcp_offload_ctloutput(tp, sopt->sopt_dir,
2140 error = sooptcopyin(sopt, &optval, sizeof optval,
2145 INP_WLOCK_RECHECK(inp);
2147 tp->t_flags |= TF_NOPUSH;
2148 else if (tp->t_flags & TF_NOPUSH) {
2149 tp->t_flags &= ~TF_NOPUSH;
2150 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2151 struct epoch_tracker et;
2153 NET_EPOCH_ENTER(et);
2154 error = tcp_output_nodrop(tp);
2158 goto unlock_and_done;
2160 case TCP_REMOTE_UDP_ENCAPS_PORT:
2162 error = sooptcopyin(sopt, &optval, sizeof optval,
2166 if ((optval < TCP_TUNNELING_PORT_MIN) ||
2167 (optval > TCP_TUNNELING_PORT_MAX)) {
2168 /* Its got to be in range */
2171 if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
2172 /* You have to have enabled a UDP tunneling port first */
2175 INP_WLOCK_RECHECK(inp);
2176 if (tp->t_state != TCPS_CLOSED) {
2177 /* You can't change after you are connected */
2180 /* Ok we are all good set the port */
2181 tp->t_port = htons(optval);
2183 goto unlock_and_done;
2187 error = sooptcopyin(sopt, &optval, sizeof optval,
2192 INP_WLOCK_RECHECK(inp);
2193 if (optval > 0 && optval <= tp->t_maxseg &&
2194 optval + 40 >= V_tcp_minmss)
2195 tp->t_maxseg = optval;
2198 goto unlock_and_done;
2208 error = sooptcopyin(sopt, &optval, sizeof optval,
2214 sbp = stats_blob_alloc(
2215 V_tcp_perconn_stats_dflt_tpl, 0);
2219 INP_WLOCK_RECHECK(inp);
2220 if ((tp->t_stats != NULL && sbp == NULL) ||
2221 (tp->t_stats == NULL && sbp != NULL)) {
2222 struct statsblob *t = tp->t_stats;
2228 stats_blob_destroy(sbp);
2230 return (EOPNOTSUPP);
2234 case TCP_CONGESTION:
2235 error = tcp_set_cc_mod(inp, sopt);
2238 case TCP_REUSPORT_LB_NUMA:
2240 error = sooptcopyin(sopt, &optval, sizeof(optval),
2242 INP_WLOCK_RECHECK(inp);
2244 error = in_pcblbgroup_numa(inp, optval);
2249 case TCP_TXTLS_ENABLE:
2251 error = copyin_tls_enable(sopt, &tls);
2254 error = ktls_enable_tx(so, &tls);
2256 case TCP_TXTLS_MODE:
2258 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2262 INP_WLOCK_RECHECK(inp);
2263 error = ktls_set_tx_mode(so, ui);
2266 case TCP_RXTLS_ENABLE:
2268 error = sooptcopyin(sopt, &tls, sizeof(tls),
2272 error = ktls_enable_rx(so, &tls);
2275 case TCP_MAXUNACKTIME:
2280 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2284 if (ui > (UINT_MAX / hz)) {
2290 INP_WLOCK_RECHECK(inp);
2291 switch (sopt->sopt_name) {
2292 case TCP_MAXUNACKTIME:
2293 tp->t_maxunacktime = ui;
2297 tp->t_keepidle = ui;
2299 * XXX: better check current remaining
2300 * timeout and "merge" it with new value.
2302 if ((tp->t_state > TCPS_LISTEN) &&
2303 (tp->t_state <= TCPS_CLOSING))
2304 tcp_timer_activate(tp, TT_KEEP,
2308 tp->t_keepintvl = ui;
2309 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2310 (TP_MAXIDLE(tp) > 0))
2311 tcp_timer_activate(tp, TT_2MSL,
2315 tp->t_keepinit = ui;
2316 if (tp->t_state == TCPS_SYN_RECEIVED ||
2317 tp->t_state == TCPS_SYN_SENT)
2318 tcp_timer_activate(tp, TT_KEEP,
2322 goto unlock_and_done;
2326 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2330 INP_WLOCK_RECHECK(inp);
2332 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2333 (TP_MAXIDLE(tp) > 0))
2334 tcp_timer_activate(tp, TT_2MSL,
2336 goto unlock_and_done;
2342 error = sooptcopyin(sopt, &optval, sizeof optval,
2347 INP_WLOCK_RECHECK(inp);
2349 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
2350 &(tp->t_outpkts) : &(tp->t_inpkts),
2354 goto unlock_and_done;
2357 case TCP_FASTOPEN: {
2358 struct tcp_fastopen tfo_optval;
2361 if (!V_tcp_fastopen_client_enable &&
2362 !V_tcp_fastopen_server_enable)
2365 error = sooptcopyin(sopt, &tfo_optval,
2366 sizeof(tfo_optval), sizeof(int));
2370 INP_WLOCK_RECHECK(inp);
2371 if ((tp->t_state != TCPS_CLOSED) &&
2372 (tp->t_state != TCPS_LISTEN)) {
2374 goto unlock_and_done;
2376 if (tfo_optval.enable) {
2377 if (tp->t_state == TCPS_LISTEN) {
2378 if (!V_tcp_fastopen_server_enable) {
2380 goto unlock_and_done;
2383 if (tp->t_tfo_pending == NULL)
2385 tcp_fastopen_alloc_counter();
2388 * If a pre-shared key was provided,
2389 * stash it in the client cookie
2390 * field of the tcpcb for use during
2393 if (sopt->sopt_valsize ==
2394 sizeof(tfo_optval)) {
2395 memcpy(tp->t_tfo_cookie.client,
2397 TCP_FASTOPEN_PSK_LEN);
2398 tp->t_tfo_client_cookie_len =
2399 TCP_FASTOPEN_PSK_LEN;
2402 tp->t_flags |= TF_FASTOPEN;
2404 tp->t_flags &= ~TF_FASTOPEN;
2405 goto unlock_and_done;
2411 error = sooptcopyin(sopt, &optval, sizeof optval,
2416 INP_WLOCK_RECHECK(inp);
2417 error = tcp_log_state_change(tp, optval);
2418 goto unlock_and_done;
2427 error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
2430 buf[sopt->sopt_valsize] = '\0';
2431 INP_WLOCK_RECHECK(inp);
2432 error = tcp_log_set_id(tp, buf);
2433 /* tcp_log_set_id() unlocks the INP. */
2440 sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
2443 buf[sopt->sopt_valsize] = '\0';
2444 INP_WLOCK_RECHECK(inp);
2445 if (sopt->sopt_name == TCP_LOGDUMP) {
2446 error = tcp_log_dump_tp_logbuf(tp, buf,
2450 tcp_log_dump_tp_bucket_logbufs(tp, buf);
2452 * tcp_log_dump_tp_bucket_logbufs() drops the
2461 error = ENOPROTOOPT;
2467 tp = intotcpcb(inp);
2468 switch (sopt->sopt_name) {
2469 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2472 if (!TCPMD5_ENABLED())
2473 return (ENOPROTOOPT);
2474 error = TCPMD5_PCBCTL(inp, sopt);
2479 optval = tp->t_flags & TF_NODELAY;
2481 error = sooptcopyout(sopt, &optval, sizeof optval);
2484 optval = tp->t_maxseg;
2486 error = sooptcopyout(sopt, &optval, sizeof optval);
2488 case TCP_REMOTE_UDP_ENCAPS_PORT:
2489 optval = ntohs(tp->t_port);
2491 error = sooptcopyout(sopt, &optval, sizeof optval);
2494 optval = tp->t_flags & TF_NOOPT;
2496 error = sooptcopyout(sopt, &optval, sizeof optval);
2499 optval = tp->t_flags & TF_NOPUSH;
2501 error = sooptcopyout(sopt, &optval, sizeof optval);
2504 tcp_fill_info(tp, &ti);
2506 error = sooptcopyout(sopt, &ti, sizeof ti);
2512 TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2515 socklen_t outsbsz = sopt->sopt_valsize;
2516 if (tp->t_stats == NULL)
2518 else if (outsbsz >= tp->t_stats->cursz)
2519 outsbsz = tp->t_stats->cursz;
2520 else if (outsbsz >= sizeof(struct statsblob))
2521 outsbsz = sizeof(struct statsblob);
2528 sbp = sopt->sopt_val;
2529 nheld = atop(round_page(((vm_offset_t)sbp) +
2530 (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2531 vm_page_t ma[nheld];
2532 if (vm_fault_quick_hold_pages(
2533 &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2534 outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2540 if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2541 SIZEOF_MEMBER(struct statsblob, flags))))
2544 INP_WLOCK_RECHECK(inp);
2545 error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2546 sbflags | SB_CLONE_USRDSTNOFAULT);
2548 sopt->sopt_valsize = outsbsz;
2550 vm_page_unhold_pages(ma, nheld);
2557 case TCP_CONGESTION:
2558 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2560 error = sooptcopyout(sopt, buf, len + 1);
2562 case TCP_MAXUNACKTIME:
2567 switch (sopt->sopt_name) {
2568 case TCP_MAXUNACKTIME:
2569 ui = TP_MAXUNACKTIME(tp) / hz;
2572 ui = TP_KEEPIDLE(tp) / hz;
2575 ui = TP_KEEPINTVL(tp) / hz;
2578 ui = TP_KEEPINIT(tp) / hz;
2581 ui = TP_KEEPCNT(tp);
2585 error = sooptcopyout(sopt, &ui, sizeof(ui));
2590 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
2591 &(tp->t_outpkts) : &(tp->t_inpkts));
2593 error = sooptcopyout(sopt, &optval, sizeof optval);
2597 optval = tp->t_flags & TF_FASTOPEN;
2599 error = sooptcopyout(sopt, &optval, sizeof optval);
2603 optval = tp->t_logstate;
2605 error = sooptcopyout(sopt, &optval, sizeof(optval));
2608 /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2609 error = tcp_log_getlogbuf(sopt, tp);
2612 len = tcp_log_get_id(tp, buf);
2614 error = sooptcopyout(sopt, buf, len + 1);
2623 case TCP_TXTLS_MODE:
2624 error = ktls_get_tx_mode(so, &optval);
2627 error = sooptcopyout(sopt, &optval,
2630 case TCP_RXTLS_MODE:
2631 error = ktls_get_rx_mode(so, &optval);
2634 error = sooptcopyout(sopt, &optval,
2639 optval = tp->t_flags & TF_LRD;
2641 error = sooptcopyout(sopt, &optval, sizeof optval);
2645 error = ENOPROTOOPT;
2652 #undef INP_WLOCK_RECHECK
2653 #undef INP_WLOCK_RECHECK_CLEANUP
2656 * Initiate (or continue) disconnect.
2657 * If embryonic state, just send reset (once).
2658 * If in ``let data drain'' option and linger null, just drop.
2659 * Otherwise (hard), mark socket disconnecting and drop
2660 * current input data; switch states based on user close, and
2661 * send segment to peer (with FIN).
2664 tcp_disconnect(struct tcpcb *tp)
2666 struct inpcb *inp = tptoinpcb(tp);
2667 struct socket *so = tptosocket(tp);
2670 INP_WLOCK_ASSERT(inp);
2673 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2674 * socket is still open.
2676 if (tp->t_state < TCPS_ESTABLISHED &&
2677 !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2680 ("tcp_disconnect: tcp_close() returned NULL"));
2681 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2682 tp = tcp_drop(tp, 0);
2684 ("tcp_disconnect: tcp_drop() returned NULL"));
2686 soisdisconnecting(so);
2687 sbflush(&so->so_rcv);
2689 if (!(inp->inp_flags & INP_DROPPED))
2690 /* Ignore stack's drop request, we already at it. */
2691 (void)tcp_output_nodrop(tp);
2696 * User issued close, and wish to trail through shutdown states:
2697 * if never received SYN, just forget it. If got a SYN from peer,
2698 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2699 * If already got a FIN from peer, then almost done; go to LAST_ACK
2700 * state. In all other cases, have already sent FIN to peer (e.g.
2701 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2702 * for peer to send FIN or not respond to keep-alives, etc.
2703 * We can let the user exit from the close as soon as the FIN is acked.
2706 tcp_usrclosed(struct tcpcb *tp)
2710 INP_WLOCK_ASSERT(tptoinpcb(tp));
2712 switch (tp->t_state) {
2715 tcp_offload_listen_stop(tp);
2717 tcp_state_change(tp, TCPS_CLOSED);
2722 * tcp_close() should never return NULL here as the socket is
2726 ("tcp_usrclosed: tcp_close() returned NULL"));
2730 case TCPS_SYN_RECEIVED:
2731 tp->t_flags |= TF_NEEDFIN;
2734 case TCPS_ESTABLISHED:
2735 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2738 case TCPS_CLOSE_WAIT:
2739 tcp_state_change(tp, TCPS_LAST_ACK);
2742 if (tp->t_acktime == 0)
2743 tp->t_acktime = ticks;
2744 if (tp->t_state >= TCPS_FIN_WAIT_2) {
2745 soisdisconnected(tptosocket(tp));
2746 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
2747 if (tp->t_state == TCPS_FIN_WAIT_2) {
2750 timeout = (tcp_fast_finwait2_recycle) ?
2751 tcp_finwait2_timeout : TP_MAXIDLE(tp);
2752 tcp_timer_activate(tp, TT_2MSL, timeout);
2759 db_print_indent(int indent)
2763 for (i = 0; i < indent; i++)
2768 db_print_tstate(int t_state)
2773 db_printf("TCPS_CLOSED");
2777 db_printf("TCPS_LISTEN");
2781 db_printf("TCPS_SYN_SENT");
2784 case TCPS_SYN_RECEIVED:
2785 db_printf("TCPS_SYN_RECEIVED");
2788 case TCPS_ESTABLISHED:
2789 db_printf("TCPS_ESTABLISHED");
2792 case TCPS_CLOSE_WAIT:
2793 db_printf("TCPS_CLOSE_WAIT");
2796 case TCPS_FIN_WAIT_1:
2797 db_printf("TCPS_FIN_WAIT_1");
2801 db_printf("TCPS_CLOSING");
2805 db_printf("TCPS_LAST_ACK");
2808 case TCPS_FIN_WAIT_2:
2809 db_printf("TCPS_FIN_WAIT_2");
2812 case TCPS_TIME_WAIT:
2813 db_printf("TCPS_TIME_WAIT");
2817 db_printf("unknown");
2823 db_print_tflags(u_int t_flags)
2828 if (t_flags & TF_ACKNOW) {
2829 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2832 if (t_flags & TF_DELACK) {
2833 db_printf("%sTF_DELACK", comma ? ", " : "");
2836 if (t_flags & TF_NODELAY) {
2837 db_printf("%sTF_NODELAY", comma ? ", " : "");
2840 if (t_flags & TF_NOOPT) {
2841 db_printf("%sTF_NOOPT", comma ? ", " : "");
2844 if (t_flags & TF_SENTFIN) {
2845 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2848 if (t_flags & TF_REQ_SCALE) {
2849 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2852 if (t_flags & TF_RCVD_SCALE) {
2853 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2856 if (t_flags & TF_REQ_TSTMP) {
2857 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2860 if (t_flags & TF_RCVD_TSTMP) {
2861 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2864 if (t_flags & TF_SACK_PERMIT) {
2865 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2868 if (t_flags & TF_NEEDSYN) {
2869 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2872 if (t_flags & TF_NEEDFIN) {
2873 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2876 if (t_flags & TF_NOPUSH) {
2877 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2880 if (t_flags & TF_PREVVALID) {
2881 db_printf("%sTF_PREVVALID", comma ? ", " : "");
2884 if (t_flags & TF_MORETOCOME) {
2885 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2888 if (t_flags & TF_SONOTCONN) {
2889 db_printf("%sTF_SONOTCONN", comma ? ", " : "");
2892 if (t_flags & TF_LASTIDLE) {
2893 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2896 if (t_flags & TF_RXWIN0SENT) {
2897 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2900 if (t_flags & TF_FASTRECOVERY) {
2901 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2904 if (t_flags & TF_CONGRECOVERY) {
2905 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2908 if (t_flags & TF_WASFRECOVERY) {
2909 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2912 if (t_flags & TF_WASCRECOVERY) {
2913 db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
2916 if (t_flags & TF_SIGNATURE) {
2917 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2920 if (t_flags & TF_FORCEDATA) {
2921 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2924 if (t_flags & TF_TSO) {
2925 db_printf("%sTF_TSO", comma ? ", " : "");
2928 if (t_flags & TF_FASTOPEN) {
2929 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2935 db_print_tflags2(u_int t_flags2)
2940 if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
2941 db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
2944 if (t_flags2 & TF2_PLPMTU_PMTUD) {
2945 db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
2948 if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
2949 db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
2952 if (t_flags2 & TF2_LOG_AUTO) {
2953 db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
2956 if (t_flags2 & TF2_DROP_AF_DATA) {
2957 db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
2960 if (t_flags2 & TF2_ECN_PERMIT) {
2961 db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
2964 if (t_flags2 & TF2_ECN_SND_CWR) {
2965 db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
2968 if (t_flags2 & TF2_ECN_SND_ECE) {
2969 db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
2972 if (t_flags2 & TF2_ACE_PERMIT) {
2973 db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
2976 if (t_flags2 & TF2_FBYTES_COMPLETE) {
2977 db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
2983 db_print_toobflags(char t_oobflags)
2988 if (t_oobflags & TCPOOB_HAVEDATA) {
2989 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2992 if (t_oobflags & TCPOOB_HADDATA) {
2993 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2999 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
3002 db_print_indent(indent);
3003 db_printf("%s at %p\n", name, tp);
3007 db_print_indent(indent);
3008 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
3009 TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
3011 db_print_indent(indent);
3012 db_printf("t_callout: %p t_timers: %p\n",
3013 &tp->t_callout, &tp->t_timers);
3015 db_print_indent(indent);
3016 db_printf("t_state: %d (", tp->t_state);
3017 db_print_tstate(tp->t_state);
3020 db_print_indent(indent);
3021 db_printf("t_flags: 0x%x (", tp->t_flags);
3022 db_print_tflags(tp->t_flags);
3025 db_print_indent(indent);
3026 db_printf("t_flags2: 0x%x (", tp->t_flags2);
3027 db_print_tflags2(tp->t_flags2);
3030 db_print_indent(indent);
3031 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
3032 tp->snd_una, tp->snd_max, tp->snd_nxt);
3034 db_print_indent(indent);
3035 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
3036 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
3038 db_print_indent(indent);
3039 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
3040 tp->iss, tp->irs, tp->rcv_nxt);
3042 db_print_indent(indent);
3043 db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n",
3044 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
3046 db_print_indent(indent);
3047 db_printf("snd_wnd: %u snd_cwnd: %u\n",
3048 tp->snd_wnd, tp->snd_cwnd);
3050 db_print_indent(indent);
3051 db_printf("snd_ssthresh: %u snd_recover: "
3052 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3054 db_print_indent(indent);
3055 db_printf("t_rcvtime: %u t_startime: %u\n",
3056 tp->t_rcvtime, tp->t_starttime);
3058 db_print_indent(indent);
3059 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
3060 tp->t_rtttime, tp->t_rtseq);
3062 db_print_indent(indent);
3063 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
3064 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3066 db_print_indent(indent);
3067 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u\n",
3068 tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin);
3070 db_print_indent(indent);
3071 db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n",
3072 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3074 db_print_indent(indent);
3075 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3076 db_print_toobflags(tp->t_oobflags);
3077 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
3079 db_print_indent(indent);
3080 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
3081 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3083 db_print_indent(indent);
3084 db_printf("ts_recent: %u ts_recent_age: %u\n",
3085 tp->ts_recent, tp->ts_recent_age);
3087 db_print_indent(indent);
3088 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
3089 "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3091 db_print_indent(indent);
3092 db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x "
3093 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3094 tp->snd_recover_prev, tp->t_badrxtwin);
3096 db_print_indent(indent);
3097 db_printf("snd_numholes: %d snd_holes first: %p\n",
3098 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3100 db_print_indent(indent);
3101 db_printf("snd_fack: 0x%08x rcv_numsacks: %d\n",
3102 tp->snd_fack, tp->rcv_numsacks);
3104 /* Skip sackblks, sackhint. */
3106 db_print_indent(indent);
3107 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
3108 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3111 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3116 db_printf("usage: show tcpcb <addr>\n");
3119 tp = (struct tcpcb *)addr;
3121 db_print_tcpcb(tp, "tcpcb", 0);