2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1988, 1993
5 * The Regents of the University of California.
6 * Copyright (c) 2006-2007 Robert N. M. Watson
7 * Copyright (c) 2010-2011 Juniper Networks, Inc.
10 * Portions of this software were developed by Robert N. M. Watson under
11 * contract to Juniper Networks, Inc.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
45 #include "opt_inet6.h"
46 #include "opt_ipsec.h"
47 #include "opt_kern_tls.h"
48 #include "opt_tcpdebug.h"
50 #include <sys/param.h>
51 #include <sys/systm.h>
53 #include <sys/limits.h>
54 #include <sys/malloc.h>
55 #include <sys/refcount.h>
56 #include <sys/kernel.h>
58 #include <sys/qmath.h>
59 #include <sys/sysctl.h>
62 #include <sys/domain.h>
64 #include <sys/socket.h>
65 #include <sys/socketvar.h>
66 #include <sys/protosw.h>
69 #include <sys/syslog.h>
70 #include <sys/stats.h>
77 #include <net/if_var.h>
78 #include <net/route.h>
81 #include <netinet/in.h>
82 #include <netinet/in_kdtrace.h>
83 #include <netinet/in_pcb.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/in_var.h>
86 #include <netinet/ip_var.h>
88 #include <netinet/ip6.h>
89 #include <netinet6/in6_pcb.h>
90 #include <netinet6/ip6_var.h>
91 #include <netinet6/scope6_var.h>
93 #include <netinet/tcp.h>
94 #include <netinet/tcp_fsm.h>
95 #include <netinet/tcp_seq.h>
96 #include <netinet/tcp_timer.h>
97 #include <netinet/tcp_var.h>
98 #include <netinet/tcp_log_buf.h>
99 #include <netinet/tcpip.h>
100 #include <netinet/cc/cc.h>
101 #include <netinet/tcp_fastopen.h>
102 #include <netinet/tcp_hpts.h>
104 #include <netinet/tcp_pcap.h>
107 #include <netinet/tcp_debug.h>
110 #include <netinet/tcp_offload.h>
112 #include <netipsec/ipsec_support.h>
115 #include <vm/vm_param.h>
117 #include <vm/vm_extern.h>
118 #include <vm/vm_map.h>
119 #include <vm/vm_page.h>
122 * TCP protocol interface to socket abstraction.
125 static int tcp_connect(struct tcpcb *, struct sockaddr *,
129 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
132 static void tcp_disconnect(struct tcpcb *);
133 static void tcp_usrclosed(struct tcpcb *);
134 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
136 static int tcp_pru_options_support(struct tcpcb *tp, int flags);
139 #define TCPDEBUG0 int ostate = 0
140 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
141 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
142 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
146 #define TCPDEBUG2(req)
150 * tcp_require_unique port requires a globally-unique source port for each
151 * outgoing connection. The default is to require the 4-tuple to be unique.
153 VNET_DEFINE(int, tcp_require_unique_port) = 0;
154 SYSCTL_INT(_net_inet_tcp, OID_AUTO, require_unique_port,
155 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_require_unique_port), 0,
156 "Require globally-unique ephemeral port for outgoing connections");
157 #define V_tcp_require_unique_port VNET(tcp_require_unique_port)
160 * TCP attaches to socket via pru_attach(), reserving space,
161 * and an internet control block.
164 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
167 struct tcpcb *tp = NULL;
172 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
175 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
176 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
181 so->so_rcv.sb_flags |= SB_AUTOSIZE;
182 so->so_snd.sb_flags |= SB_AUTOSIZE;
183 error = in_pcballoc(so, &V_tcbinfo);
188 if (inp->inp_vflag & INP_IPV6PROTO) {
189 inp->inp_vflag |= INP_IPV6;
190 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
191 inp->inp_vflag |= INP_IPV4;
192 inp->in6p_hops = -1; /* use kernel default */
196 inp->inp_vflag |= INP_IPV4;
197 tp = tcp_newtcpcb(inp);
204 tp->t_state = TCPS_CLOSED;
206 TCPSTATES_INC(TCPS_CLOSED);
208 TCPDEBUG2(PRU_ATTACH);
209 TCP_PROBE2(debug__user, tp, PRU_ATTACH);
214 * tcp_usr_detach is called when the socket layer loses its final reference
215 * to the socket, be it a file descriptor reference, a reference from TCP,
216 * etc. At this point, there is only one case in which we will keep around
217 * inpcb state: time wait.
220 tcp_usr_detach(struct socket *so)
226 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
228 KASSERT(so->so_pcb == inp && inp->inp_socket == so,
229 ("%s: socket %p inp %p mismatch", __func__, so, inp));
233 if (inp->inp_flags & INP_TIMEWAIT) {
235 * There are two cases to handle: one in which the time wait
236 * state is being discarded (INP_DROPPED), and one in which
237 * this connection will remain in timewait. In the former,
238 * it is time to discard all state (except tcptw, which has
239 * already been discarded by the timewait close code, which
240 * should be further up the call stack somewhere). In the
241 * latter case, we detach from the socket, but leave the pcb
242 * present until timewait ends.
244 * XXXRW: Would it be cleaner to free the tcptw here?
246 * Astute question indeed, from twtcp perspective there are
247 * four cases to consider:
249 * #1 tcp_usr_detach is called at tcptw creation time by
250 * tcp_twstart, then do not discard the newly created tcptw
251 * and leave inpcb present until timewait ends
252 * #2 tcp_usr_detach is called at tcptw creation time by
253 * tcp_twstart, but connection is local and tw will be
254 * discarded immediately
255 * #3 tcp_usr_detach is called at timewait end (or reuse) by
256 * tcp_twclose, then the tcptw has already been discarded
257 * (or reused) and inpcb is freed here
258 * #4 tcp_usr_detach is called() after timewait ends (or reuse)
259 * (e.g. by soclose), then tcptw has already been discarded
260 * (or reused) and inpcb is freed here
262 * In all three cases the tcptw should not be freed here.
264 if (inp->inp_flags & INP_DROPPED) {
266 if (__predict_true(tp == NULL)) {
270 * This case should not happen as in TIMEWAIT
271 * state the inp should not be destroyed before
272 * its tcptw. If INVARIANTS is defined, panic.
275 panic("%s: Panic before an inp double-free: "
276 "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
279 log(LOG_ERR, "%s: Avoid an inp double-free: "
280 "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
291 * If the connection is not in timewait, we consider two
292 * two conditions: one in which no further processing is
293 * necessary (dropped || embryonic), and one in which TCP is
294 * not yet done, but no longer requires the socket, so the
295 * pcb will persist for the time being.
297 * XXXRW: Does the second case still occur?
299 if (inp->inp_flags & INP_DROPPED ||
300 tp->t_state < TCPS_SYN_SENT) {
313 * Give the socket an address.
316 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
320 struct tcpcb *tp = NULL;
321 struct sockaddr_in *sinp;
323 sinp = (struct sockaddr_in *)nam;
324 if (nam->sa_family != AF_INET) {
326 * Preserve compatibility with old programs.
328 if (nam->sa_family != AF_UNSPEC ||
329 nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
330 sinp->sin_addr.s_addr != INADDR_ANY)
331 return (EAFNOSUPPORT);
332 nam->sa_family = AF_INET;
334 if (nam->sa_len != sizeof(*sinp))
338 * Must check for multicast addresses and disallow binding
341 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
342 return (EAFNOSUPPORT);
346 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
348 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
354 INP_HASH_WLOCK(&V_tcbinfo);
355 error = in_pcbbind(inp, nam, td->td_ucred);
356 INP_HASH_WUNLOCK(&V_tcbinfo);
359 TCP_PROBE2(debug__user, tp, PRU_BIND);
368 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
372 struct tcpcb *tp = NULL;
373 struct sockaddr_in6 *sin6;
376 sin6 = (struct sockaddr_in6 *)nam;
377 if (nam->sa_family != AF_INET6)
378 return (EAFNOSUPPORT);
379 if (nam->sa_len != sizeof(*sin6))
383 * Must check for multicast addresses and disallow binding
386 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
387 return (EAFNOSUPPORT);
391 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
393 vflagsav = inp->inp_vflag;
394 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
400 INP_HASH_WLOCK(&V_tcbinfo);
401 inp->inp_vflag &= ~INP_IPV4;
402 inp->inp_vflag |= INP_IPV6;
404 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
405 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
406 inp->inp_vflag |= INP_IPV4;
407 else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
408 struct sockaddr_in sin;
410 in6_sin6_2_sin(&sin, sin6);
411 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
412 error = EAFNOSUPPORT;
413 INP_HASH_WUNLOCK(&V_tcbinfo);
416 inp->inp_vflag |= INP_IPV4;
417 inp->inp_vflag &= ~INP_IPV6;
418 error = in_pcbbind(inp, (struct sockaddr *)&sin,
420 INP_HASH_WUNLOCK(&V_tcbinfo);
425 error = in6_pcbbind(inp, nam, td->td_ucred);
426 INP_HASH_WUNLOCK(&V_tcbinfo);
429 inp->inp_vflag = vflagsav;
431 TCP_PROBE2(debug__user, tp, PRU_BIND);
439 * Prepare to accept connections.
442 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
446 struct tcpcb *tp = NULL;
450 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
452 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
459 error = solisten_proto_check(so);
464 if (inp->inp_lport == 0) {
465 INP_HASH_WLOCK(&V_tcbinfo);
466 error = in_pcbbind(inp, NULL, td->td_ucred);
467 INP_HASH_WUNLOCK(&V_tcbinfo);
470 tcp_state_change(tp, TCPS_LISTEN);
471 solisten_proto(so, backlog);
473 if ((so->so_options & SO_NO_OFFLOAD) == 0)
474 tcp_offload_listen_start(tp);
477 solisten_proto_abort(so);
481 if (IS_FASTOPEN(tp->t_flags))
482 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
485 TCPDEBUG2(PRU_LISTEN);
486 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
494 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
498 struct tcpcb *tp = NULL;
503 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
505 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
509 vflagsav = inp->inp_vflag;
513 error = solisten_proto_check(so);
518 INP_HASH_WLOCK(&V_tcbinfo);
519 if (inp->inp_lport == 0) {
520 inp->inp_vflag &= ~INP_IPV4;
521 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
522 inp->inp_vflag |= INP_IPV4;
523 error = in6_pcbbind(inp, NULL, td->td_ucred);
525 INP_HASH_WUNLOCK(&V_tcbinfo);
527 tcp_state_change(tp, TCPS_LISTEN);
528 solisten_proto(so, backlog);
530 if ((so->so_options & SO_NO_OFFLOAD) == 0)
531 tcp_offload_listen_start(tp);
534 solisten_proto_abort(so);
538 if (IS_FASTOPEN(tp->t_flags))
539 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
542 inp->inp_vflag = vflagsav;
545 TCPDEBUG2(PRU_LISTEN);
546 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
554 * Initiate connection to peer.
555 * Create a template for use in transmissions on this connection.
556 * Enter SYN_SENT state, and mark socket as connecting.
557 * Start keep-alive timer, and seed output sequence space.
558 * Send initial segment on connection.
561 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
563 struct epoch_tracker et;
566 struct tcpcb *tp = NULL;
567 struct sockaddr_in *sinp;
569 sinp = (struct sockaddr_in *)nam;
570 if (nam->sa_family != AF_INET)
571 return (EAFNOSUPPORT);
572 if (nam->sa_len != sizeof (*sinp))
576 * Must disallow TCP ``connections'' to multicast addresses.
578 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
579 return (EAFNOSUPPORT);
580 if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST)
582 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
587 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
589 if (inp->inp_flags & INP_TIMEWAIT) {
593 if (inp->inp_flags & INP_DROPPED) {
594 error = ECONNREFUSED;
597 if (SOLISTENING(so)) {
604 if ((error = tcp_connect(tp, nam, td)) != 0)
607 if (registered_toedevs > 0 &&
608 (so->so_options & SO_NO_OFFLOAD) == 0 &&
609 (error = tcp_offload_connect(so, nam)) == 0)
612 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
613 error = tp->t_fb->tfb_tcp_output(tp);
617 TCPDEBUG2(PRU_CONNECT);
618 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
626 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
628 struct epoch_tracker et;
631 struct tcpcb *tp = NULL;
632 struct sockaddr_in6 *sin6;
638 sin6 = (struct sockaddr_in6 *)nam;
639 if (nam->sa_family != AF_INET6)
640 return (EAFNOSUPPORT);
641 if (nam->sa_len != sizeof (*sin6))
645 * Must disallow TCP ``connections'' to multicast addresses.
647 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
648 return (EAFNOSUPPORT);
651 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
653 vflagsav = inp->inp_vflag;
654 incflagsav = inp->inp_inc.inc_flags;
655 if (inp->inp_flags & INP_TIMEWAIT) {
659 if (inp->inp_flags & INP_DROPPED) {
660 error = ECONNREFUSED;
663 if (SOLISTENING(so)) {
671 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
672 * therefore probably require the hash lock, which isn't held here.
673 * Is this a significant problem?
675 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
676 struct sockaddr_in sin;
678 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
682 if ((inp->inp_vflag & INP_IPV4) == 0) {
683 error = EAFNOSUPPORT;
687 in6_sin6_2_sin(&sin, sin6);
688 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
689 error = EAFNOSUPPORT;
692 if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
696 if ((error = prison_remote_ip4(td->td_ucred,
697 &sin.sin_addr)) != 0)
699 inp->inp_vflag |= INP_IPV4;
700 inp->inp_vflag &= ~INP_IPV6;
702 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
705 if (registered_toedevs > 0 &&
706 (so->so_options & SO_NO_OFFLOAD) == 0 &&
707 (error = tcp_offload_connect(so, nam)) == 0)
710 error = tp->t_fb->tfb_tcp_output(tp);
713 if ((inp->inp_vflag & INP_IPV6) == 0) {
714 error = EAFNOSUPPORT;
719 if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
721 inp->inp_vflag &= ~INP_IPV4;
722 inp->inp_vflag |= INP_IPV6;
723 inp->inp_inc.inc_flags |= INC_ISIPV6;
724 if ((error = tcp6_connect(tp, nam, td)) != 0)
727 if (registered_toedevs > 0 &&
728 (so->so_options & SO_NO_OFFLOAD) == 0 &&
729 (error = tcp_offload_connect(so, nam)) == 0)
732 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
734 error = tp->t_fb->tfb_tcp_output(tp);
741 * If the implicit bind in the connect call fails, restore
742 * the flags we modified.
744 if (error != 0 && inp->inp_lport == 0) {
745 inp->inp_vflag = vflagsav;
746 inp->inp_inc.inc_flags = incflagsav;
749 TCPDEBUG2(PRU_CONNECT);
750 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
757 * Initiate disconnect from peer.
758 * If connection never passed embryonic stage, just drop;
759 * else if don't need to let data drain, then can just drop anyways,
760 * else have to begin TCP shutdown process: mark socket disconnecting,
761 * drain unread data, state switch to reflect user close, and
762 * send segment (e.g. FIN) to peer. Socket will be really disconnected
763 * when peer sends FIN and acks ours.
765 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
768 tcp_usr_disconnect(struct socket *so)
771 struct tcpcb *tp = NULL;
772 struct epoch_tracker et;
778 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
780 if (inp->inp_flags & INP_TIMEWAIT)
782 if (inp->inp_flags & INP_DROPPED) {
790 TCPDEBUG2(PRU_DISCONNECT);
791 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
799 * Accept a connection. Essentially all the work is done at higher levels;
800 * just return the address of the peer, storing through addr.
803 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
806 struct inpcb *inp = NULL;
807 struct tcpcb *tp = NULL;
812 if (so->so_state & SS_ISDISCONNECTED)
813 return (ECONNABORTED);
816 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
818 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
819 error = ECONNABORTED;
826 * We inline in_getpeeraddr and COMMON_END here, so that we can
827 * copy the data of interest and defer the malloc until after we
830 port = inp->inp_fport;
831 addr = inp->inp_faddr;
834 TCPDEBUG2(PRU_ACCEPT);
835 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
838 *nam = in_sockaddr(port, &addr);
845 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
847 struct inpcb *inp = NULL;
849 struct tcpcb *tp = NULL;
851 struct in6_addr addr6;
852 struct epoch_tracker et;
857 if (so->so_state & SS_ISDISCONNECTED)
858 return (ECONNABORTED);
861 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
864 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
865 error = ECONNABORTED;
872 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
873 * copy the data of interest and defer the malloc until after we
876 if (inp->inp_vflag & INP_IPV4) {
878 port = inp->inp_fport;
879 addr = inp->inp_faddr;
881 port = inp->inp_fport;
882 addr6 = inp->in6p_faddr;
886 TCPDEBUG2(PRU_ACCEPT);
887 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
892 *nam = in6_v4mapsin6_sockaddr(port, &addr);
894 *nam = in6_sockaddr(port, &addr6);
901 * Mark the connection as being incapable of further output.
904 tcp_usr_shutdown(struct socket *so)
908 struct tcpcb *tp = NULL;
909 struct epoch_tracker et;
914 KASSERT(inp != NULL, ("inp == NULL"));
916 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
924 if (!(inp->inp_flags & INP_DROPPED))
925 error = tp->t_fb->tfb_tcp_output(tp);
928 TCPDEBUG2(PRU_SHUTDOWN);
929 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
937 * After a receive, possibly send window update to peer.
940 tcp_usr_rcvd(struct socket *so, int flags)
942 struct epoch_tracker et;
944 struct tcpcb *tp = NULL;
949 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
951 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
958 * For passively-created TFO connections, don't attempt a window
959 * update while still in SYN_RECEIVED as this may trigger an early
960 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
961 * application response data, or failing that, when the DELACK timer
964 if (IS_FASTOPEN(tp->t_flags) &&
965 (tp->t_state == TCPS_SYN_RECEIVED))
969 if (tp->t_flags & TF_TOE)
970 tcp_offload_rcvd(tp);
973 tp->t_fb->tfb_tcp_output(tp);
977 TCP_PROBE2(debug__user, tp, PRU_RCVD);
983 * Do a send by putting data in output queue and updating urgent
984 * marker if URG set. Possibly send more data. Unlike the other
985 * pru_*() routines, the mbuf chains are our responsibility. We
986 * must either enqueue them or free them. The other pru_* routines
987 * generally are caller-frees.
990 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
991 struct sockaddr *nam, struct mbuf *control, struct thread *td)
993 struct epoch_tracker et;
996 struct tcpcb *tp = NULL;
999 struct sockaddr_in sin;
1001 struct sockaddr_in *sinp;
1006 u_int8_t incflagsav;
1012 * We require the pcbinfo "read lock" if we will close the socket
1013 * as part of this call.
1015 NET_EPOCH_ENTER(et);
1016 inp = sotoinpcb(so);
1017 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
1019 vflagsav = inp->inp_vflag;
1020 incflagsav = inp->inp_inc.inc_flags;
1021 restoreflags = false;
1022 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1028 if (control != NULL) {
1029 /* TCP doesn't do control messages (rights, creds, etc) */
1030 if (control->m_len) {
1035 m_freem(control); /* empty control, just free it */
1038 tp = intotcpcb(inp);
1039 if ((flags & PRUS_OOB) != 0 &&
1040 (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
1044 if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
1045 if (tp->t_state == TCPS_LISTEN) {
1049 switch (nam->sa_family) {
1052 sinp = (struct sockaddr_in *)nam;
1053 if (sinp->sin_len != sizeof(struct sockaddr_in)) {
1057 if ((inp->inp_vflag & INP_IPV6) != 0) {
1058 error = EAFNOSUPPORT;
1061 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
1062 error = EAFNOSUPPORT;
1065 if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
1069 if ((error = prison_remote_ip4(td->td_ucred,
1080 struct sockaddr_in6 *sin6;
1082 sin6 = (struct sockaddr_in6 *)nam;
1083 if (sin6->sin6_len != sizeof(*sin6)) {
1087 if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
1088 error = EAFNOSUPPORT;
1091 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1092 error = EAFNOSUPPORT;
1095 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1097 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1101 if ((inp->inp_vflag & INP_IPV4) == 0) {
1102 error = EAFNOSUPPORT;
1105 restoreflags = true;
1106 inp->inp_vflag &= ~INP_IPV6;
1108 in6_sin6_2_sin(sinp, sin6);
1110 ntohl(sinp->sin_addr.s_addr))) {
1111 error = EAFNOSUPPORT;
1114 if ((error = prison_remote_ip4(td->td_ucred,
1119 error = EAFNOSUPPORT;
1123 if ((inp->inp_vflag & INP_IPV6) == 0) {
1124 error = EAFNOSUPPORT;
1127 restoreflags = true;
1128 inp->inp_vflag &= ~INP_IPV4;
1129 inp->inp_inc.inc_flags |= INC_ISIPV6;
1130 if ((error = prison_remote_ip6(td->td_ucred,
1139 error = EAFNOSUPPORT;
1143 if (!(flags & PRUS_OOB)) {
1144 sbappendstream(&so->so_snd, m, flags);
1146 if (nam && tp->t_state < TCPS_SYN_SENT) {
1147 KASSERT(tp->t_state == TCPS_CLOSED,
1148 ("%s: tp %p is listening", __func__, tp));
1151 * Do implied connect if not yet connected,
1152 * initialize window to default value, and
1153 * initialize maxseg using peer's cached MSS.
1157 error = tcp6_connect(tp, nam, td);
1159 #if defined(INET6) && defined(INET)
1163 error = tcp_connect(tp,
1164 (struct sockaddr *)sinp, td);
1167 * The bind operation in tcp_connect succeeded. We
1168 * no longer want to restore the flags if later
1171 if (error == 0 || inp->inp_lport != 0)
1172 restoreflags = false;
1175 /* m is freed if PRUS_NOTREADY is unset. */
1176 sbflush(&so->so_snd);
1179 if (IS_FASTOPEN(tp->t_flags))
1180 tcp_fastopen_connect(tp);
1182 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1186 if (flags & PRUS_EOF) {
1188 * Close the send side of the connection after
1194 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1195 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1196 (tp->t_fbyte_out == 0) &&
1197 (so->so_snd.sb_ccc > 0)) {
1198 tp->t_fbyte_out = ticks;
1199 if (tp->t_fbyte_out == 0)
1200 tp->t_fbyte_out = 1;
1201 if (tp->t_fbyte_out && tp->t_fbyte_in)
1202 tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1204 if (!(inp->inp_flags & INP_DROPPED) &&
1205 !(flags & PRUS_NOTREADY)) {
1206 if (flags & PRUS_MORETOCOME)
1207 tp->t_flags |= TF_MORETOCOME;
1208 error = tp->t_fb->tfb_tcp_output(tp);
1209 if (flags & PRUS_MORETOCOME)
1210 tp->t_flags &= ~TF_MORETOCOME;
1214 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1216 SOCKBUF_LOCK(&so->so_snd);
1217 if (sbspace(&so->so_snd) < -512) {
1218 SOCKBUF_UNLOCK(&so->so_snd);
1223 * According to RFC961 (Assigned Protocols),
1224 * the urgent pointer points to the last octet
1225 * of urgent data. We continue, however,
1226 * to consider it to indicate the first octet
1227 * of data past the urgent section.
1228 * Otherwise, snd_up should be one lower.
1230 sbappendstream_locked(&so->so_snd, m, flags);
1231 SOCKBUF_UNLOCK(&so->so_snd);
1233 if (nam && tp->t_state < TCPS_SYN_SENT) {
1235 * Do implied connect if not yet connected,
1236 * initialize window to default value, and
1237 * initialize maxseg using peer's cached MSS.
1241 * Not going to contemplate SYN|URG
1243 if (IS_FASTOPEN(tp->t_flags))
1244 tp->t_flags &= ~TF_FASTOPEN;
1247 error = tcp6_connect(tp, nam, td);
1249 #if defined(INET6) && defined(INET)
1253 error = tcp_connect(tp,
1254 (struct sockaddr *)sinp, td);
1257 * The bind operation in tcp_connect succeeded. We
1258 * no longer want to restore the flags if later
1261 if (error == 0 || inp->inp_lport != 0)
1262 restoreflags = false;
1265 /* m is freed if PRUS_NOTREADY is unset. */
1266 sbflush(&so->so_snd);
1269 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1272 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1273 if ((flags & PRUS_NOTREADY) == 0) {
1274 tp->t_flags |= TF_FORCEDATA;
1275 error = tp->t_fb->tfb_tcp_output(tp);
1276 tp->t_flags &= ~TF_FORCEDATA;
1279 TCP_LOG_EVENT(tp, NULL,
1280 &inp->inp_socket->so_rcv,
1281 &inp->inp_socket->so_snd,
1282 TCP_LOG_USERSEND, error,
1287 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
1288 * responsible for freeing memory.
1290 if (m != NULL && (flags & PRUS_NOTREADY) == 0)
1294 * If the request was unsuccessful and we changed flags,
1295 * restore the original flags.
1297 if (error != 0 && restoreflags) {
1298 inp->inp_vflag = vflagsav;
1299 inp->inp_inc.inc_flags = incflagsav;
1301 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
1302 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1303 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1304 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1311 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1313 struct epoch_tracker et;
1318 inp = sotoinpcb(so);
1320 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1322 mb_free_notready(m, count);
1323 return (ECONNRESET);
1325 tp = intotcpcb(inp);
1327 SOCKBUF_LOCK(&so->so_snd);
1328 error = sbready(&so->so_snd, m, count);
1329 SOCKBUF_UNLOCK(&so->so_snd);
1331 NET_EPOCH_ENTER(et);
1332 error = tp->t_fb->tfb_tcp_output(tp);
1341 * Abort the TCP. Drop the connection abruptly.
1344 tcp_usr_abort(struct socket *so)
1347 struct tcpcb *tp = NULL;
1348 struct epoch_tracker et;
1351 inp = sotoinpcb(so);
1352 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1354 NET_EPOCH_ENTER(et);
1356 KASSERT(inp->inp_socket != NULL,
1357 ("tcp_usr_abort: inp_socket == NULL"));
1360 * If we still have full TCP state, and we're not dropped, drop.
1362 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1363 !(inp->inp_flags & INP_DROPPED)) {
1364 tp = intotcpcb(inp);
1366 tp = tcp_drop(tp, ECONNABORTED);
1369 TCPDEBUG2(PRU_ABORT);
1370 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1372 if (!(inp->inp_flags & INP_DROPPED)) {
1374 so->so_state |= SS_PROTOREF;
1376 inp->inp_flags |= INP_SOCKREF;
1384 * TCP socket is closed. Start friendly disconnect.
1387 tcp_usr_close(struct socket *so)
1390 struct tcpcb *tp = NULL;
1391 struct epoch_tracker et;
1394 inp = sotoinpcb(so);
1395 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1397 NET_EPOCH_ENTER(et);
1399 KASSERT(inp->inp_socket != NULL,
1400 ("tcp_usr_close: inp_socket == NULL"));
1403 * If we still have full TCP state, and we're not dropped, initiate
1406 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1407 !(inp->inp_flags & INP_DROPPED)) {
1408 tp = intotcpcb(inp);
1411 TCPDEBUG2(PRU_CLOSE);
1412 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1414 if (!(inp->inp_flags & INP_DROPPED)) {
1416 so->so_state |= SS_PROTOREF;
1418 inp->inp_flags |= INP_SOCKREF;
1425 tcp_pru_options_support(struct tcpcb *tp, int flags)
1428 * If the specific TCP stack has a pru_options
1429 * specified then it does not always support
1430 * all the PRU_XX options and we must ask it.
1431 * If the function is not specified then all
1432 * of the PRU_XX options are supported.
1436 if (tp->t_fb->tfb_pru_options) {
1437 ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1443 * Receive out-of-band data.
1446 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1450 struct tcpcb *tp = NULL;
1453 inp = sotoinpcb(so);
1454 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1456 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1460 tp = intotcpcb(inp);
1461 error = tcp_pru_options_support(tp, PRUS_OOB);
1466 if ((so->so_oobmark == 0 &&
1467 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1468 so->so_options & SO_OOBINLINE ||
1469 tp->t_oobflags & TCPOOB_HADDATA) {
1473 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1474 error = EWOULDBLOCK;
1478 *mtod(m, caddr_t) = tp->t_iobc;
1479 if ((flags & MSG_PEEK) == 0)
1480 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1483 TCPDEBUG2(PRU_RCVOOB);
1484 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1490 struct pr_usrreqs tcp_usrreqs = {
1491 .pru_abort = tcp_usr_abort,
1492 .pru_accept = tcp_usr_accept,
1493 .pru_attach = tcp_usr_attach,
1494 .pru_bind = tcp_usr_bind,
1495 .pru_connect = tcp_usr_connect,
1496 .pru_control = in_control,
1497 .pru_detach = tcp_usr_detach,
1498 .pru_disconnect = tcp_usr_disconnect,
1499 .pru_listen = tcp_usr_listen,
1500 .pru_peeraddr = in_getpeeraddr,
1501 .pru_rcvd = tcp_usr_rcvd,
1502 .pru_rcvoob = tcp_usr_rcvoob,
1503 .pru_send = tcp_usr_send,
1504 .pru_ready = tcp_usr_ready,
1505 .pru_shutdown = tcp_usr_shutdown,
1506 .pru_sockaddr = in_getsockaddr,
1507 .pru_sosetlabel = in_pcbsosetlabel,
1508 .pru_close = tcp_usr_close,
1513 struct pr_usrreqs tcp6_usrreqs = {
1514 .pru_abort = tcp_usr_abort,
1515 .pru_accept = tcp6_usr_accept,
1516 .pru_attach = tcp_usr_attach,
1517 .pru_bind = tcp6_usr_bind,
1518 .pru_connect = tcp6_usr_connect,
1519 .pru_control = in6_control,
1520 .pru_detach = tcp_usr_detach,
1521 .pru_disconnect = tcp_usr_disconnect,
1522 .pru_listen = tcp6_usr_listen,
1523 .pru_peeraddr = in6_mapped_peeraddr,
1524 .pru_rcvd = tcp_usr_rcvd,
1525 .pru_rcvoob = tcp_usr_rcvoob,
1526 .pru_send = tcp_usr_send,
1527 .pru_ready = tcp_usr_ready,
1528 .pru_shutdown = tcp_usr_shutdown,
1529 .pru_sockaddr = in6_mapped_sockaddr,
1530 .pru_sosetlabel = in_pcbsosetlabel,
1531 .pru_close = tcp_usr_close,
1537 * Common subroutine to open a TCP connection to remote host specified
1538 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1539 * port number if needed. Call in_pcbconnect_setup to do the routing and
1540 * to choose a local host address (interface). If there is an existing
1541 * incarnation of the same connection in TIME-WAIT state and if the remote
1542 * host was sending CC options and if the connection duration was < MSL, then
1543 * truncate the previous TIME-WAIT state and proceed.
1544 * Initialize connection parameters and enter SYN-SENT state.
1547 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1549 struct inpcb *inp = tp->t_inpcb, *oinp;
1550 struct socket *so = inp->inp_socket;
1551 struct in_addr laddr;
1556 INP_WLOCK_ASSERT(inp);
1557 INP_HASH_WLOCK(&V_tcbinfo);
1559 if (V_tcp_require_unique_port && inp->inp_lport == 0) {
1560 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1566 * Cannot simply call in_pcbconnect, because there might be an
1567 * earlier incarnation of this same connection still in
1568 * TIME_WAIT state, creating an ADDRINUSE error.
1570 laddr = inp->inp_laddr;
1571 lport = inp->inp_lport;
1572 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1573 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1574 if (error && oinp == NULL)
1580 /* Handle initial bind if it hadn't been done in advance. */
1581 if (inp->inp_lport == 0) {
1582 inp->inp_lport = lport;
1583 if (in_pcbinshash(inp) != 0) {
1589 inp->inp_laddr = laddr;
1591 INP_HASH_WUNLOCK(&V_tcbinfo);
1594 * Compute window scaling to request:
1595 * Scale to fit into sweet spot. See tcp_syncache.c.
1596 * XXX: This should move to tcp_output().
1598 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1599 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1600 tp->request_r_scale++;
1603 TCPSTAT_INC(tcps_connattempt);
1604 tcp_state_change(tp, TCPS_SYN_SENT);
1605 tp->iss = tcp_new_isn(&inp->inp_inc);
1606 if (tp->t_flags & TF_REQ_TSTMP)
1607 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1608 tcp_sendseqinit(tp);
1613 INP_HASH_WUNLOCK(&V_tcbinfo);
1620 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1622 struct inpcb *inp = tp->t_inpcb;
1625 INP_WLOCK_ASSERT(inp);
1626 INP_HASH_WLOCK(&V_tcbinfo);
1628 if (V_tcp_require_unique_port && inp->inp_lport == 0) {
1629 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1633 error = in6_pcbconnect(inp, nam, td->td_ucred);
1636 INP_HASH_WUNLOCK(&V_tcbinfo);
1638 /* Compute window scaling to request. */
1639 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1640 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1641 tp->request_r_scale++;
1643 soisconnecting(inp->inp_socket);
1644 TCPSTAT_INC(tcps_connattempt);
1645 tcp_state_change(tp, TCPS_SYN_SENT);
1646 tp->iss = tcp_new_isn(&inp->inp_inc);
1647 if (tp->t_flags & TF_REQ_TSTMP)
1648 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1649 tcp_sendseqinit(tp);
1654 INP_HASH_WUNLOCK(&V_tcbinfo);
1660 * Export TCP internal state information via a struct tcp_info, based on the
1661 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1662 * (TCP state machine, etc). We export all information using FreeBSD-native
1663 * constants -- for example, the numeric values for tcpi_state will differ
1667 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1670 INP_WLOCK_ASSERT(tp->t_inpcb);
1671 bzero(ti, sizeof(*ti));
1673 ti->tcpi_state = tp->t_state;
1674 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1675 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1676 if (tp->t_flags & TF_SACK_PERMIT)
1677 ti->tcpi_options |= TCPI_OPT_SACK;
1678 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1679 ti->tcpi_options |= TCPI_OPT_WSCALE;
1680 ti->tcpi_snd_wscale = tp->snd_scale;
1681 ti->tcpi_rcv_wscale = tp->rcv_scale;
1683 if (tp->t_flags2 & TF2_ECN_PERMIT)
1684 ti->tcpi_options |= TCPI_OPT_ECN;
1686 ti->tcpi_rto = tp->t_rxtcur * tick;
1687 ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1688 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1689 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1691 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1692 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1695 * FreeBSD-specific extension fields for tcp_info.
1697 ti->tcpi_rcv_space = tp->rcv_wnd;
1698 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1699 ti->tcpi_snd_wnd = tp->snd_wnd;
1700 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1701 ti->tcpi_snd_nxt = tp->snd_nxt;
1702 ti->tcpi_snd_mss = tp->t_maxseg;
1703 ti->tcpi_rcv_mss = tp->t_maxseg;
1704 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1705 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1706 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1708 if (tp->t_flags & TF_TOE) {
1709 ti->tcpi_options |= TCPI_OPT_TOE;
1710 tcp_offload_tcp_info(tp, ti);
1716 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1717 * socket option arguments. When it re-acquires the lock after the copy, it
1718 * has to revalidate that the connection is still valid for the socket
1721 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \
1723 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1726 return (ECONNRESET); \
1728 tp = intotcpcb(inp); \
1730 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1733 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1738 struct tcp_function_block *blk;
1739 struct tcp_function_set fsn;
1742 inp = sotoinpcb(so);
1743 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1744 if (sopt->sopt_level != IPPROTO_TCP) {
1746 if (inp->inp_vflag & INP_IPV6PROTO) {
1747 error = ip6_ctloutput(so, sopt);
1749 * In case of the IPV6_USE_MIN_MTU socket option,
1750 * the INC_IPV6MINMTU flag to announce a corresponding
1751 * MSS during the initial handshake.
1752 * If the TCP connection is not in the front states,
1753 * just reduce the MSS being used.
1754 * This avoids the sending of TCP segments which will
1755 * be fragmented at the IPv6 layer.
1758 (sopt->sopt_dir == SOPT_SET) &&
1759 (sopt->sopt_level == IPPROTO_IPV6) &&
1760 (sopt->sopt_name == IPV6_USE_MIN_MTU)) {
1762 if ((inp->inp_flags &
1763 (INP_TIMEWAIT | INP_DROPPED))) {
1765 return (ECONNRESET);
1767 inp->inp_inc.inc_flags |= INC_IPV6MINMTU;
1768 tp = intotcpcb(inp);
1769 if ((tp->t_state >= TCPS_SYN_SENT) &&
1770 (inp->inp_inc.inc_flags & INC_ISIPV6)) {
1771 struct ip6_pktopts *opt;
1773 opt = inp->in6p_outputopts;
1774 if ((opt != NULL) &&
1775 (opt->ip6po_minmtu ==
1776 IP6PO_MINMTU_ALL)) {
1777 if (tp->t_maxseg > TCP6_MSS) {
1778 tp->t_maxseg = TCP6_MSS;
1786 #if defined(INET6) && defined(INET)
1791 error = ip_ctloutput(so, sopt);
1797 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1799 return (ECONNRESET);
1801 tp = intotcpcb(inp);
1803 * Protect the TCP option TCP_FUNCTION_BLK so
1804 * that a sub-function can *never* overwrite this.
1806 if ((sopt->sopt_dir == SOPT_SET) &&
1807 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1809 error = sooptcopyin(sopt, &fsn, sizeof fsn,
1813 INP_WLOCK_RECHECK(inp);
1814 blk = find_and_ref_tcp_functions(&fsn);
1819 if (tp->t_fb == blk) {
1820 /* You already have this */
1821 refcount_release(&blk->tfb_refcnt);
1825 if (tp->t_state != TCPS_CLOSED) {
1827 * The user has advanced the state
1828 * past the initial point, we may not
1829 * be able to switch.
1831 if (blk->tfb_tcp_handoff_ok != NULL) {
1833 * Does the stack provide a
1834 * query mechanism, if so it may
1835 * still be possible?
1837 error = (*blk->tfb_tcp_handoff_ok)(tp);
1841 refcount_release(&blk->tfb_refcnt);
1846 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1847 refcount_release(&blk->tfb_refcnt);
1852 * Release the old refcnt, the
1853 * lookup acquired a ref on the
1856 if (tp->t_fb->tfb_tcp_fb_fini) {
1857 struct epoch_tracker et;
1859 * Tell the stack to cleanup with 0 i.e.
1860 * the tcb is not going away.
1862 NET_EPOCH_ENTER(et);
1863 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1867 /* Assure that we are not on any hpts */
1868 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
1870 if (blk->tfb_tcp_fb_init) {
1871 error = (*blk->tfb_tcp_fb_init)(tp);
1873 refcount_release(&blk->tfb_refcnt);
1874 if (tp->t_fb->tfb_tcp_fb_init) {
1875 if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) {
1876 /* Fall back failed, drop the connection */
1885 refcount_release(&tp->t_fb->tfb_refcnt);
1888 if (tp->t_flags & TF_TOE) {
1889 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1896 } else if ((sopt->sopt_dir == SOPT_GET) &&
1897 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1898 strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1899 TCP_FUNCTION_NAME_LEN_MAX);
1900 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1901 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1903 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1906 /* Pass in the INP locked, called must unlock it */
1907 return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
1911 * If this assert becomes untrue, we need to change the size of the buf
1912 * variable in tcp_default_ctloutput().
1915 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1916 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1921 copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1923 struct tls_enable_v0 tls_v0;
1926 if (sopt->sopt_valsize == sizeof(tls_v0)) {
1927 error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1931 memset(tls, 0, sizeof(*tls));
1932 tls->cipher_key = tls_v0.cipher_key;
1933 tls->iv = tls_v0.iv;
1934 tls->auth_key = tls_v0.auth_key;
1935 tls->cipher_algorithm = tls_v0.cipher_algorithm;
1936 tls->cipher_key_len = tls_v0.cipher_key_len;
1937 tls->iv_len = tls_v0.iv_len;
1938 tls->auth_algorithm = tls_v0.auth_algorithm;
1939 tls->auth_key_len = tls_v0.auth_key_len;
1940 tls->flags = tls_v0.flags;
1941 tls->tls_vmajor = tls_v0.tls_vmajor;
1942 tls->tls_vminor = tls_v0.tls_vminor;
1946 return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1951 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
1953 int error, opt, optval;
1957 struct tls_enable tls;
1959 struct cc_algo *algo;
1960 char *pbuf, buf[TCP_LOG_ID_LEN];
1962 struct statsblob *sbp;
1967 * For TCP_CCALGOOPT forward the control to CC module, for both
1968 * SOPT_SET and SOPT_GET.
1970 switch (sopt->sopt_name) {
1973 if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
1975 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1976 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1977 sopt->sopt_valsize);
1982 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1983 if (CC_ALGO(tp)->ctl_output != NULL)
1984 error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1988 if (error == 0 && sopt->sopt_dir == SOPT_GET)
1989 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1994 switch (sopt->sopt_dir) {
1996 switch (sopt->sopt_name) {
1997 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1999 if (!TCPMD5_ENABLED()) {
2001 return (ENOPROTOOPT);
2003 error = TCPMD5_PCBCTL(inp, sopt);
2006 goto unlock_and_done;
2013 error = sooptcopyin(sopt, &optval, sizeof optval,
2018 INP_WLOCK_RECHECK(inp);
2019 switch (sopt->sopt_name) {
2030 opt = 0; /* dead code to fool gcc */
2037 tp->t_flags &= ~opt;
2040 if (tp->t_flags & TF_TOE) {
2041 tcp_offload_ctloutput(tp, sopt->sopt_dir,
2050 error = sooptcopyin(sopt, &optval, sizeof optval,
2055 INP_WLOCK_RECHECK(inp);
2057 tp->t_flags |= TF_NOPUSH;
2058 else if (tp->t_flags & TF_NOPUSH) {
2059 tp->t_flags &= ~TF_NOPUSH;
2060 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2061 struct epoch_tracker et;
2063 NET_EPOCH_ENTER(et);
2064 error = tp->t_fb->tfb_tcp_output(tp);
2068 goto unlock_and_done;
2070 case TCP_REMOTE_UDP_ENCAPS_PORT:
2072 error = sooptcopyin(sopt, &optval, sizeof optval,
2076 if ((optval < TCP_TUNNELING_PORT_MIN) ||
2077 (optval > TCP_TUNNELING_PORT_MAX)) {
2078 /* Its got to be in range */
2081 if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
2082 /* You have to have enabled a UDP tunneling port first */
2085 INP_WLOCK_RECHECK(inp);
2086 if (tp->t_state != TCPS_CLOSED) {
2087 /* You can't change after you are connected */
2090 /* Ok we are all good set the port */
2091 tp->t_port = htons(optval);
2093 goto unlock_and_done;
2097 error = sooptcopyin(sopt, &optval, sizeof optval,
2102 INP_WLOCK_RECHECK(inp);
2103 if (optval > 0 && optval <= tp->t_maxseg &&
2104 optval + 40 >= V_tcp_minmss)
2105 tp->t_maxseg = optval;
2108 goto unlock_and_done;
2118 error = sooptcopyin(sopt, &optval, sizeof optval,
2124 sbp = stats_blob_alloc(
2125 V_tcp_perconn_stats_dflt_tpl, 0);
2129 INP_WLOCK_RECHECK(inp);
2130 if ((tp->t_stats != NULL && sbp == NULL) ||
2131 (tp->t_stats == NULL && sbp != NULL)) {
2132 struct statsblob *t = tp->t_stats;
2138 stats_blob_destroy(sbp);
2140 return (EOPNOTSUPP);
2144 case TCP_CONGESTION:
2146 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
2149 buf[sopt->sopt_valsize] = '\0';
2150 INP_WLOCK_RECHECK(inp);
2152 STAILQ_FOREACH(algo, &cc_list, entries)
2153 if (strncmp(buf, algo->name,
2154 TCP_CA_NAME_MAX) == 0)
2163 * We hold a write lock over the tcb so it's safe to
2164 * do these things without ordering concerns.
2166 if (CC_ALGO(tp)->cb_destroy != NULL)
2167 CC_ALGO(tp)->cb_destroy(tp->ccv);
2171 * If something goes pear shaped initialising the new
2172 * algo, fall back to newreno (which does not
2173 * require initialisation).
2175 if (algo->cb_init != NULL &&
2176 algo->cb_init(tp->ccv) != 0) {
2177 CC_ALGO(tp) = &newreno_cc_algo;
2179 * The only reason init should fail is
2180 * because of malloc.
2187 case TCP_REUSPORT_LB_NUMA:
2189 error = sooptcopyin(sopt, &optval, sizeof(optval),
2191 INP_WLOCK_RECHECK(inp);
2193 error = in_pcblbgroup_numa(inp, optval);
2198 case TCP_TXTLS_ENABLE:
2200 error = copyin_tls_enable(sopt, &tls);
2203 error = ktls_enable_tx(so, &tls);
2205 case TCP_TXTLS_MODE:
2207 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2211 INP_WLOCK_RECHECK(inp);
2212 error = ktls_set_tx_mode(so, ui);
2215 case TCP_RXTLS_ENABLE:
2217 error = sooptcopyin(sopt, &tls, sizeof(tls),
2221 error = ktls_enable_rx(so, &tls);
2229 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2233 if (ui > (UINT_MAX / hz)) {
2239 INP_WLOCK_RECHECK(inp);
2240 switch (sopt->sopt_name) {
2242 tp->t_keepidle = ui;
2244 * XXX: better check current remaining
2245 * timeout and "merge" it with new value.
2247 if ((tp->t_state > TCPS_LISTEN) &&
2248 (tp->t_state <= TCPS_CLOSING))
2249 tcp_timer_activate(tp, TT_KEEP,
2253 tp->t_keepintvl = ui;
2254 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2255 (TP_MAXIDLE(tp) > 0))
2256 tcp_timer_activate(tp, TT_2MSL,
2260 tp->t_keepinit = ui;
2261 if (tp->t_state == TCPS_SYN_RECEIVED ||
2262 tp->t_state == TCPS_SYN_SENT)
2263 tcp_timer_activate(tp, TT_KEEP,
2267 goto unlock_and_done;
2271 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2275 INP_WLOCK_RECHECK(inp);
2277 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2278 (TP_MAXIDLE(tp) > 0))
2279 tcp_timer_activate(tp, TT_2MSL,
2281 goto unlock_and_done;
2287 error = sooptcopyin(sopt, &optval, sizeof optval,
2292 INP_WLOCK_RECHECK(inp);
2294 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
2295 &(tp->t_outpkts) : &(tp->t_inpkts),
2299 goto unlock_and_done;
2302 case TCP_FASTOPEN: {
2303 struct tcp_fastopen tfo_optval;
2306 if (!V_tcp_fastopen_client_enable &&
2307 !V_tcp_fastopen_server_enable)
2310 error = sooptcopyin(sopt, &tfo_optval,
2311 sizeof(tfo_optval), sizeof(int));
2315 INP_WLOCK_RECHECK(inp);
2316 if ((tp->t_state != TCPS_CLOSED) &&
2317 (tp->t_state != TCPS_LISTEN)) {
2319 goto unlock_and_done;
2321 if (tfo_optval.enable) {
2322 if (tp->t_state == TCPS_LISTEN) {
2323 if (!V_tcp_fastopen_server_enable) {
2325 goto unlock_and_done;
2328 if (tp->t_tfo_pending == NULL)
2330 tcp_fastopen_alloc_counter();
2333 * If a pre-shared key was provided,
2334 * stash it in the client cookie
2335 * field of the tcpcb for use during
2338 if (sopt->sopt_valsize ==
2339 sizeof(tfo_optval)) {
2340 memcpy(tp->t_tfo_cookie.client,
2342 TCP_FASTOPEN_PSK_LEN);
2343 tp->t_tfo_client_cookie_len =
2344 TCP_FASTOPEN_PSK_LEN;
2347 tp->t_flags |= TF_FASTOPEN;
2349 tp->t_flags &= ~TF_FASTOPEN;
2350 goto unlock_and_done;
2356 error = sooptcopyin(sopt, &optval, sizeof optval,
2361 INP_WLOCK_RECHECK(inp);
2362 error = tcp_log_state_change(tp, optval);
2363 goto unlock_and_done;
2372 error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
2375 buf[sopt->sopt_valsize] = '\0';
2376 INP_WLOCK_RECHECK(inp);
2377 error = tcp_log_set_id(tp, buf);
2378 /* tcp_log_set_id() unlocks the INP. */
2385 sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
2388 buf[sopt->sopt_valsize] = '\0';
2389 INP_WLOCK_RECHECK(inp);
2390 if (sopt->sopt_name == TCP_LOGDUMP) {
2391 error = tcp_log_dump_tp_logbuf(tp, buf,
2395 tcp_log_dump_tp_bucket_logbufs(tp, buf);
2397 * tcp_log_dump_tp_bucket_logbufs() drops the
2406 error = ENOPROTOOPT;
2412 tp = intotcpcb(inp);
2413 switch (sopt->sopt_name) {
2414 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2416 if (!TCPMD5_ENABLED()) {
2418 return (ENOPROTOOPT);
2420 error = TCPMD5_PCBCTL(inp, sopt);
2425 optval = tp->t_flags & TF_NODELAY;
2427 error = sooptcopyout(sopt, &optval, sizeof optval);
2430 optval = tp->t_maxseg;
2432 error = sooptcopyout(sopt, &optval, sizeof optval);
2434 case TCP_REMOTE_UDP_ENCAPS_PORT:
2435 optval = ntohs(tp->t_port);
2437 error = sooptcopyout(sopt, &optval, sizeof optval);
2440 optval = tp->t_flags & TF_NOOPT;
2442 error = sooptcopyout(sopt, &optval, sizeof optval);
2445 optval = tp->t_flags & TF_NOPUSH;
2447 error = sooptcopyout(sopt, &optval, sizeof optval);
2450 tcp_fill_info(tp, &ti);
2452 error = sooptcopyout(sopt, &ti, sizeof ti);
2458 TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2461 socklen_t outsbsz = sopt->sopt_valsize;
2462 if (tp->t_stats == NULL)
2464 else if (outsbsz >= tp->t_stats->cursz)
2465 outsbsz = tp->t_stats->cursz;
2466 else if (outsbsz >= sizeof(struct statsblob))
2467 outsbsz = sizeof(struct statsblob);
2474 sbp = sopt->sopt_val;
2475 nheld = atop(round_page(((vm_offset_t)sbp) +
2476 (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2477 vm_page_t ma[nheld];
2478 if (vm_fault_quick_hold_pages(
2479 &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2480 outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2486 if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2487 SIZEOF_MEMBER(struct statsblob, flags))))
2490 INP_WLOCK_RECHECK(inp);
2491 error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2492 sbflags | SB_CLONE_USRDSTNOFAULT);
2494 sopt->sopt_valsize = outsbsz;
2496 vm_page_unhold_pages(ma, nheld);
2503 case TCP_CONGESTION:
2504 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2506 error = sooptcopyout(sopt, buf, len + 1);
2512 switch (sopt->sopt_name) {
2514 ui = TP_KEEPIDLE(tp) / hz;
2517 ui = TP_KEEPINTVL(tp) / hz;
2520 ui = TP_KEEPINIT(tp) / hz;
2523 ui = TP_KEEPCNT(tp);
2527 error = sooptcopyout(sopt, &ui, sizeof(ui));
2532 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
2533 &(tp->t_outpkts) : &(tp->t_inpkts));
2535 error = sooptcopyout(sopt, &optval, sizeof optval);
2539 optval = tp->t_flags & TF_FASTOPEN;
2541 error = sooptcopyout(sopt, &optval, sizeof optval);
2545 optval = tp->t_logstate;
2547 error = sooptcopyout(sopt, &optval, sizeof(optval));
2550 /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2551 error = tcp_log_getlogbuf(sopt, tp);
2554 len = tcp_log_get_id(tp, buf);
2556 error = sooptcopyout(sopt, buf, len + 1);
2565 case TCP_TXTLS_MODE:
2566 optval = ktls_get_tx_mode(so);
2568 error = sooptcopyout(sopt, &optval, sizeof(optval));
2570 case TCP_RXTLS_MODE:
2571 optval = ktls_get_rx_mode(so);
2573 error = sooptcopyout(sopt, &optval, sizeof(optval));
2577 optval = tp->t_flags & TF_LRD;
2579 error = sooptcopyout(sopt, &optval, sizeof optval);
2583 error = ENOPROTOOPT;
2590 #undef INP_WLOCK_RECHECK
2591 #undef INP_WLOCK_RECHECK_CLEANUP
2594 * Initiate (or continue) disconnect.
2595 * If embryonic state, just send reset (once).
2596 * If in ``let data drain'' option and linger null, just drop.
2597 * Otherwise (hard), mark socket disconnecting and drop
2598 * current input data; switch states based on user close, and
2599 * send segment to peer (with FIN).
2602 tcp_disconnect(struct tcpcb *tp)
2604 struct inpcb *inp = tp->t_inpcb;
2605 struct socket *so = inp->inp_socket;
2608 INP_WLOCK_ASSERT(inp);
2611 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2612 * socket is still open.
2614 if (tp->t_state < TCPS_ESTABLISHED &&
2615 !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2618 ("tcp_disconnect: tcp_close() returned NULL"));
2619 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2620 tp = tcp_drop(tp, 0);
2622 ("tcp_disconnect: tcp_drop() returned NULL"));
2624 soisdisconnecting(so);
2625 sbflush(&so->so_rcv);
2627 if (!(inp->inp_flags & INP_DROPPED))
2628 tp->t_fb->tfb_tcp_output(tp);
2633 * User issued close, and wish to trail through shutdown states:
2634 * if never received SYN, just forget it. If got a SYN from peer,
2635 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2636 * If already got a FIN from peer, then almost done; go to LAST_ACK
2637 * state. In all other cases, have already sent FIN to peer (e.g.
2638 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2639 * for peer to send FIN or not respond to keep-alives, etc.
2640 * We can let the user exit from the close as soon as the FIN is acked.
2643 tcp_usrclosed(struct tcpcb *tp)
2647 INP_WLOCK_ASSERT(tp->t_inpcb);
2649 switch (tp->t_state) {
2652 tcp_offload_listen_stop(tp);
2654 tcp_state_change(tp, TCPS_CLOSED);
2659 * tcp_close() should never return NULL here as the socket is
2663 ("tcp_usrclosed: tcp_close() returned NULL"));
2667 case TCPS_SYN_RECEIVED:
2668 tp->t_flags |= TF_NEEDFIN;
2671 case TCPS_ESTABLISHED:
2672 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2675 case TCPS_CLOSE_WAIT:
2676 tcp_state_change(tp, TCPS_LAST_ACK);
2679 if (tp->t_state >= TCPS_FIN_WAIT_2) {
2680 soisdisconnected(tp->t_inpcb->inp_socket);
2681 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
2682 if (tp->t_state == TCPS_FIN_WAIT_2) {
2685 timeout = (tcp_fast_finwait2_recycle) ?
2686 tcp_finwait2_timeout : TP_MAXIDLE(tp);
2687 tcp_timer_activate(tp, TT_2MSL, timeout);
2694 db_print_indent(int indent)
2698 for (i = 0; i < indent; i++)
2703 db_print_tstate(int t_state)
2708 db_printf("TCPS_CLOSED");
2712 db_printf("TCPS_LISTEN");
2716 db_printf("TCPS_SYN_SENT");
2719 case TCPS_SYN_RECEIVED:
2720 db_printf("TCPS_SYN_RECEIVED");
2723 case TCPS_ESTABLISHED:
2724 db_printf("TCPS_ESTABLISHED");
2727 case TCPS_CLOSE_WAIT:
2728 db_printf("TCPS_CLOSE_WAIT");
2731 case TCPS_FIN_WAIT_1:
2732 db_printf("TCPS_FIN_WAIT_1");
2736 db_printf("TCPS_CLOSING");
2740 db_printf("TCPS_LAST_ACK");
2743 case TCPS_FIN_WAIT_2:
2744 db_printf("TCPS_FIN_WAIT_2");
2747 case TCPS_TIME_WAIT:
2748 db_printf("TCPS_TIME_WAIT");
2752 db_printf("unknown");
2758 db_print_tflags(u_int t_flags)
2763 if (t_flags & TF_ACKNOW) {
2764 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2767 if (t_flags & TF_DELACK) {
2768 db_printf("%sTF_DELACK", comma ? ", " : "");
2771 if (t_flags & TF_NODELAY) {
2772 db_printf("%sTF_NODELAY", comma ? ", " : "");
2775 if (t_flags & TF_NOOPT) {
2776 db_printf("%sTF_NOOPT", comma ? ", " : "");
2779 if (t_flags & TF_SENTFIN) {
2780 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2783 if (t_flags & TF_REQ_SCALE) {
2784 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2787 if (t_flags & TF_RCVD_SCALE) {
2788 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2791 if (t_flags & TF_REQ_TSTMP) {
2792 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2795 if (t_flags & TF_RCVD_TSTMP) {
2796 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2799 if (t_flags & TF_SACK_PERMIT) {
2800 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2803 if (t_flags & TF_NEEDSYN) {
2804 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2807 if (t_flags & TF_NEEDFIN) {
2808 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2811 if (t_flags & TF_NOPUSH) {
2812 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2815 if (t_flags & TF_MORETOCOME) {
2816 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2819 if (t_flags & TF_LQ_OVERFLOW) {
2820 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2823 if (t_flags & TF_LASTIDLE) {
2824 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2827 if (t_flags & TF_RXWIN0SENT) {
2828 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2831 if (t_flags & TF_FASTRECOVERY) {
2832 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2835 if (t_flags & TF_CONGRECOVERY) {
2836 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2839 if (t_flags & TF_WASFRECOVERY) {
2840 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2843 if (t_flags & TF_SIGNATURE) {
2844 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2847 if (t_flags & TF_FORCEDATA) {
2848 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2851 if (t_flags & TF_TSO) {
2852 db_printf("%sTF_TSO", comma ? ", " : "");
2855 if (t_flags & TF_FASTOPEN) {
2856 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2862 db_print_tflags2(u_int t_flags2)
2867 if (t_flags2 & TF2_ECN_PERMIT) {
2868 db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
2874 db_print_toobflags(char t_oobflags)
2879 if (t_oobflags & TCPOOB_HAVEDATA) {
2880 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2883 if (t_oobflags & TCPOOB_HADDATA) {
2884 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2890 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2893 db_print_indent(indent);
2894 db_printf("%s at %p\n", name, tp);
2898 db_print_indent(indent);
2899 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2900 TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2902 db_print_indent(indent);
2903 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
2904 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2906 db_print_indent(indent);
2907 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2908 &tp->t_timers->tt_delack, tp->t_inpcb);
2910 db_print_indent(indent);
2911 db_printf("t_state: %d (", tp->t_state);
2912 db_print_tstate(tp->t_state);
2915 db_print_indent(indent);
2916 db_printf("t_flags: 0x%x (", tp->t_flags);
2917 db_print_tflags(tp->t_flags);
2920 db_print_indent(indent);
2921 db_printf("t_flags2: 0x%x (", tp->t_flags2);
2922 db_print_tflags2(tp->t_flags2);
2925 db_print_indent(indent);
2926 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2927 tp->snd_una, tp->snd_max, tp->snd_nxt);
2929 db_print_indent(indent);
2930 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2931 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2933 db_print_indent(indent);
2934 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2935 tp->iss, tp->irs, tp->rcv_nxt);
2937 db_print_indent(indent);
2938 db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n",
2939 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2941 db_print_indent(indent);
2942 db_printf("snd_wnd: %u snd_cwnd: %u\n",
2943 tp->snd_wnd, tp->snd_cwnd);
2945 db_print_indent(indent);
2946 db_printf("snd_ssthresh: %u snd_recover: "
2947 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2949 db_print_indent(indent);
2950 db_printf("t_rcvtime: %u t_startime: %u\n",
2951 tp->t_rcvtime, tp->t_starttime);
2953 db_print_indent(indent);
2954 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2955 tp->t_rtttime, tp->t_rtseq);
2957 db_print_indent(indent);
2958 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2959 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2961 db_print_indent(indent);
2962 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2963 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2966 db_print_indent(indent);
2967 db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n",
2968 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2970 db_print_indent(indent);
2971 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2972 db_print_toobflags(tp->t_oobflags);
2973 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2975 db_print_indent(indent);
2976 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2977 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2979 db_print_indent(indent);
2980 db_printf("ts_recent: %u ts_recent_age: %u\n",
2981 tp->ts_recent, tp->ts_recent_age);
2983 db_print_indent(indent);
2984 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2985 "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2987 db_print_indent(indent);
2988 db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x "
2989 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2990 tp->snd_recover_prev, tp->t_badrxtwin);
2992 db_print_indent(indent);
2993 db_printf("snd_numholes: %d snd_holes first: %p\n",
2994 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2996 db_print_indent(indent);
2997 db_printf("snd_fack: 0x%08x rcv_numsacks: %d\n",
2998 tp->snd_fack, tp->rcv_numsacks);
3000 /* Skip sackblks, sackhint. */
3002 db_print_indent(indent);
3003 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
3004 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3007 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3012 db_printf("usage: show tcpcb <addr>\n");
3015 tp = (struct tcpcb *)addr;
3017 db_print_tcpcb(tp, "tcpcb", 0);