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);
207 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
208 so->so_linger = TCP_LINGERTIME;
210 TCPDEBUG2(PRU_ATTACH);
211 TCP_PROBE2(debug__user, tp, PRU_ATTACH);
216 * tcp_usr_detach is called when the socket layer loses its final reference
217 * to the socket, be it a file descriptor reference, a reference from TCP,
218 * etc. At this point, there is only one case in which we will keep around
219 * inpcb state: time wait.
222 tcp_usr_detach(struct socket *so)
228 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
230 KASSERT(so->so_pcb == inp && inp->inp_socket == so,
231 ("%s: socket %p inp %p mismatch", __func__, so, inp));
235 if (inp->inp_flags & INP_TIMEWAIT) {
237 * There are two cases to handle: one in which the time wait
238 * state is being discarded (INP_DROPPED), and one in which
239 * this connection will remain in timewait. In the former,
240 * it is time to discard all state (except tcptw, which has
241 * already been discarded by the timewait close code, which
242 * should be further up the call stack somewhere). In the
243 * latter case, we detach from the socket, but leave the pcb
244 * present until timewait ends.
246 * XXXRW: Would it be cleaner to free the tcptw here?
248 * Astute question indeed, from twtcp perspective there are
249 * four cases to consider:
251 * #1 tcp_usr_detach is called at tcptw creation time by
252 * tcp_twstart, then do not discard the newly created tcptw
253 * and leave inpcb present until timewait ends
254 * #2 tcp_usr_detach is called at tcptw creation time by
255 * tcp_twstart, but connection is local and tw will be
256 * discarded immediately
257 * #3 tcp_usr_detach is called at timewait end (or reuse) by
258 * tcp_twclose, then the tcptw has already been discarded
259 * (or reused) and inpcb is freed here
260 * #4 tcp_usr_detach is called() after timewait ends (or reuse)
261 * (e.g. by soclose), then tcptw has already been discarded
262 * (or reused) and inpcb is freed here
264 * In all three cases the tcptw should not be freed here.
266 if (inp->inp_flags & INP_DROPPED) {
268 if (__predict_true(tp == NULL)) {
272 * This case should not happen as in TIMEWAIT
273 * state the inp should not be destroyed before
274 * its tcptw. If INVARIANTS is defined, panic.
277 panic("%s: Panic before an inp double-free: "
278 "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
281 log(LOG_ERR, "%s: Avoid an inp double-free: "
282 "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
293 * If the connection is not in timewait, we consider two
294 * two conditions: one in which no further processing is
295 * necessary (dropped || embryonic), and one in which TCP is
296 * not yet done, but no longer requires the socket, so the
297 * pcb will persist for the time being.
299 * XXXRW: Does the second case still occur?
301 if (inp->inp_flags & INP_DROPPED ||
302 tp->t_state < TCPS_SYN_SENT) {
315 * Give the socket an address.
318 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
322 struct tcpcb *tp = NULL;
323 struct sockaddr_in *sinp;
325 sinp = (struct sockaddr_in *)nam;
326 if (nam->sa_len != sizeof (*sinp))
329 * Must check for multicast addresses and disallow binding
332 if (sinp->sin_family == AF_INET &&
333 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
334 return (EAFNOSUPPORT);
338 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
340 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
346 INP_HASH_WLOCK(&V_tcbinfo);
347 error = in_pcbbind(inp, nam, td->td_ucred);
348 INP_HASH_WUNLOCK(&V_tcbinfo);
351 TCP_PROBE2(debug__user, tp, PRU_BIND);
360 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
364 struct tcpcb *tp = NULL;
365 struct sockaddr_in6 *sin6;
368 sin6 = (struct sockaddr_in6 *)nam;
369 if (nam->sa_len != sizeof (*sin6))
372 * Must check for multicast addresses and disallow binding
375 if (sin6->sin6_family == AF_INET6 &&
376 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
377 return (EAFNOSUPPORT);
381 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
383 vflagsav = inp->inp_vflag;
384 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
390 INP_HASH_WLOCK(&V_tcbinfo);
391 inp->inp_vflag &= ~INP_IPV4;
392 inp->inp_vflag |= INP_IPV6;
394 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
395 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
396 inp->inp_vflag |= INP_IPV4;
397 else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
398 struct sockaddr_in sin;
400 in6_sin6_2_sin(&sin, sin6);
401 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
402 error = EAFNOSUPPORT;
403 INP_HASH_WUNLOCK(&V_tcbinfo);
406 inp->inp_vflag |= INP_IPV4;
407 inp->inp_vflag &= ~INP_IPV6;
408 error = in_pcbbind(inp, (struct sockaddr *)&sin,
410 INP_HASH_WUNLOCK(&V_tcbinfo);
415 error = in6_pcbbind(inp, nam, td->td_ucred);
416 INP_HASH_WUNLOCK(&V_tcbinfo);
419 inp->inp_vflag = vflagsav;
421 TCP_PROBE2(debug__user, tp, PRU_BIND);
429 * Prepare to accept connections.
432 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
436 struct tcpcb *tp = NULL;
440 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
442 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
449 error = solisten_proto_check(so);
450 INP_HASH_WLOCK(&V_tcbinfo);
451 if (error == 0 && inp->inp_lport == 0)
452 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
453 INP_HASH_WUNLOCK(&V_tcbinfo);
455 tcp_state_change(tp, TCPS_LISTEN);
456 solisten_proto(so, backlog);
458 if ((so->so_options & SO_NO_OFFLOAD) == 0)
459 tcp_offload_listen_start(tp);
464 if (IS_FASTOPEN(tp->t_flags))
465 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
468 TCPDEBUG2(PRU_LISTEN);
469 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
477 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
481 struct tcpcb *tp = NULL;
486 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
488 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
492 vflagsav = inp->inp_vflag;
496 error = solisten_proto_check(so);
497 INP_HASH_WLOCK(&V_tcbinfo);
498 if (error == 0 && inp->inp_lport == 0) {
499 inp->inp_vflag &= ~INP_IPV4;
500 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
501 inp->inp_vflag |= INP_IPV4;
502 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
504 INP_HASH_WUNLOCK(&V_tcbinfo);
506 tcp_state_change(tp, TCPS_LISTEN);
507 solisten_proto(so, backlog);
509 if ((so->so_options & SO_NO_OFFLOAD) == 0)
510 tcp_offload_listen_start(tp);
515 if (IS_FASTOPEN(tp->t_flags))
516 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
519 inp->inp_vflag = vflagsav;
522 TCPDEBUG2(PRU_LISTEN);
523 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
531 * Initiate connection to peer.
532 * Create a template for use in transmissions on this connection.
533 * Enter SYN_SENT state, and mark socket as connecting.
534 * Start keep-alive timer, and seed output sequence space.
535 * Send initial segment on connection.
538 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
540 struct epoch_tracker et;
543 struct tcpcb *tp = NULL;
544 struct sockaddr_in *sinp;
546 sinp = (struct sockaddr_in *)nam;
547 if (nam->sa_len != sizeof (*sinp))
550 * Must disallow TCP ``connections'' to multicast addresses.
552 if (sinp->sin_family == AF_INET
553 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
554 return (EAFNOSUPPORT);
555 if ((sinp->sin_family == AF_INET) &&
556 ((ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) ||
557 (sinp->sin_addr.s_addr == INADDR_ANY)))
558 return(EAFNOSUPPORT);
559 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
564 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
566 if (inp->inp_flags & INP_TIMEWAIT) {
570 if (inp->inp_flags & INP_DROPPED) {
571 error = ECONNREFUSED;
577 if ((error = tcp_connect(tp, nam, td)) != 0)
580 if (registered_toedevs > 0 &&
581 (so->so_options & SO_NO_OFFLOAD) == 0 &&
582 (error = tcp_offload_connect(so, nam)) == 0)
585 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
586 error = tp->t_fb->tfb_tcp_output(tp);
590 TCPDEBUG2(PRU_CONNECT);
591 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
599 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
601 struct epoch_tracker et;
604 struct tcpcb *tp = NULL;
605 struct sockaddr_in6 *sin6;
611 sin6 = (struct sockaddr_in6 *)nam;
612 if (nam->sa_len != sizeof (*sin6))
615 * Must disallow TCP ``connections'' to multicast addresses.
617 if (sin6->sin6_family == AF_INET6
618 && IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
619 return (EAFNOSUPPORT);
622 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
624 vflagsav = inp->inp_vflag;
625 incflagsav = inp->inp_inc.inc_flags;
626 if (inp->inp_flags & INP_TIMEWAIT) {
630 if (inp->inp_flags & INP_DROPPED) {
631 error = ECONNREFUSED;
638 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
639 * therefore probably require the hash lock, which isn't held here.
640 * Is this a significant problem?
642 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
643 struct sockaddr_in sin;
645 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
649 if ((inp->inp_vflag & INP_IPV4) == 0) {
650 error = EAFNOSUPPORT;
654 in6_sin6_2_sin(&sin, sin6);
655 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
656 error = EAFNOSUPPORT;
659 if ((ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) ||
660 (sin.sin_addr.s_addr == INADDR_ANY)) {
661 error = EAFNOSUPPORT;
664 if ((error = prison_remote_ip4(td->td_ucred,
665 &sin.sin_addr)) != 0)
667 inp->inp_vflag |= INP_IPV4;
668 inp->inp_vflag &= ~INP_IPV6;
670 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
673 if (registered_toedevs > 0 &&
674 (so->so_options & SO_NO_OFFLOAD) == 0 &&
675 (error = tcp_offload_connect(so, nam)) == 0)
678 error = tp->t_fb->tfb_tcp_output(tp);
681 if ((inp->inp_vflag & INP_IPV6) == 0) {
682 error = EAFNOSUPPORT;
687 if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
689 inp->inp_vflag &= ~INP_IPV4;
690 inp->inp_vflag |= INP_IPV6;
691 inp->inp_inc.inc_flags |= INC_ISIPV6;
692 if ((error = tcp6_connect(tp, nam, td)) != 0)
695 if (registered_toedevs > 0 &&
696 (so->so_options & SO_NO_OFFLOAD) == 0 &&
697 (error = tcp_offload_connect(so, nam)) == 0)
700 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
702 error = tp->t_fb->tfb_tcp_output(tp);
709 * If the implicit bind in the connect call fails, restore
710 * the flags we modified.
712 if (error != 0 && inp->inp_lport == 0) {
713 inp->inp_vflag = vflagsav;
714 inp->inp_inc.inc_flags = incflagsav;
717 TCPDEBUG2(PRU_CONNECT);
718 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
725 * Initiate disconnect from peer.
726 * If connection never passed embryonic stage, just drop;
727 * else if don't need to let data drain, then can just drop anyways,
728 * else have to begin TCP shutdown process: mark socket disconnecting,
729 * drain unread data, state switch to reflect user close, and
730 * send segment (e.g. FIN) to peer. Socket will be really disconnected
731 * when peer sends FIN and acks ours.
733 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
736 tcp_usr_disconnect(struct socket *so)
739 struct tcpcb *tp = NULL;
740 struct epoch_tracker et;
746 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
748 if (inp->inp_flags & INP_TIMEWAIT)
750 if (inp->inp_flags & INP_DROPPED) {
758 TCPDEBUG2(PRU_DISCONNECT);
759 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
767 * Accept a connection. Essentially all the work is done at higher levels;
768 * just return the address of the peer, storing through addr.
771 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
774 struct inpcb *inp = NULL;
775 struct tcpcb *tp = NULL;
780 if (so->so_state & SS_ISDISCONNECTED)
781 return (ECONNABORTED);
784 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
786 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
787 error = ECONNABORTED;
794 * We inline in_getpeeraddr and COMMON_END here, so that we can
795 * copy the data of interest and defer the malloc until after we
798 port = inp->inp_fport;
799 addr = inp->inp_faddr;
802 TCPDEBUG2(PRU_ACCEPT);
803 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
806 *nam = in_sockaddr(port, &addr);
813 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
815 struct inpcb *inp = NULL;
817 struct tcpcb *tp = NULL;
819 struct in6_addr addr6;
820 struct epoch_tracker et;
825 if (so->so_state & SS_ISDISCONNECTED)
826 return (ECONNABORTED);
829 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
832 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
833 error = ECONNABORTED;
840 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
841 * copy the data of interest and defer the malloc until after we
844 if (inp->inp_vflag & INP_IPV4) {
846 port = inp->inp_fport;
847 addr = inp->inp_faddr;
849 port = inp->inp_fport;
850 addr6 = inp->in6p_faddr;
854 TCPDEBUG2(PRU_ACCEPT);
855 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
860 *nam = in6_v4mapsin6_sockaddr(port, &addr);
862 *nam = in6_sockaddr(port, &addr6);
869 * Mark the connection as being incapable of further output.
872 tcp_usr_shutdown(struct socket *so)
876 struct tcpcb *tp = NULL;
877 struct epoch_tracker et;
882 KASSERT(inp != NULL, ("inp == NULL"));
884 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
892 if (!(inp->inp_flags & INP_DROPPED))
893 error = tp->t_fb->tfb_tcp_output(tp);
896 TCPDEBUG2(PRU_SHUTDOWN);
897 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
905 * After a receive, possibly send window update to peer.
908 tcp_usr_rcvd(struct socket *so, int flags)
910 struct epoch_tracker et;
912 struct tcpcb *tp = NULL;
917 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
919 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
926 * For passively-created TFO connections, don't attempt a window
927 * update while still in SYN_RECEIVED as this may trigger an early
928 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
929 * application response data, or failing that, when the DELACK timer
932 if (IS_FASTOPEN(tp->t_flags) &&
933 (tp->t_state == TCPS_SYN_RECEIVED))
937 if (tp->t_flags & TF_TOE)
938 tcp_offload_rcvd(tp);
941 tp->t_fb->tfb_tcp_output(tp);
945 TCP_PROBE2(debug__user, tp, PRU_RCVD);
951 * Do a send by putting data in output queue and updating urgent
952 * marker if URG set. Possibly send more data. Unlike the other
953 * pru_*() routines, the mbuf chains are our responsibility. We
954 * must either enqueue them or free them. The other pru_* routines
955 * generally are caller-frees.
958 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
959 struct sockaddr *nam, struct mbuf *control, struct thread *td)
961 struct epoch_tracker et;
964 struct tcpcb *tp = NULL;
967 struct sockaddr_in sin;
969 struct sockaddr_in *sinp;
980 * We require the pcbinfo "read lock" if we will close the socket
981 * as part of this call.
985 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
987 vflagsav = inp->inp_vflag;
988 incflagsav = inp->inp_inc.inc_flags;
989 restoreflags = false;
990 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
994 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
995 * for freeing memory.
997 if (m && (flags & PRUS_NOTREADY) == 0)
1002 tp = intotcpcb(inp);
1003 if (flags & PRUS_OOB) {
1004 if ((error = tcp_pru_options_support(tp, PRUS_OOB)) != 0) {
1007 if (m && (flags & PRUS_NOTREADY) == 0)
1013 if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
1014 switch (nam->sa_family) {
1017 sinp = (struct sockaddr_in *)nam;
1018 if (sinp->sin_len != sizeof(struct sockaddr_in)) {
1024 if ((inp->inp_vflag & INP_IPV6) != 0) {
1027 error = EAFNOSUPPORT;
1030 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
1033 error = EAFNOSUPPORT;
1036 if ((ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) ||
1037 (sinp->sin_addr.s_addr == INADDR_ANY)) {
1040 error = EAFNOSUPPORT;
1043 if ((error = prison_remote_ip4(td->td_ucred,
1044 &sinp->sin_addr))) {
1057 struct sockaddr_in6 *sin6;
1059 sin6 = (struct sockaddr_in6 *)nam;
1060 if (sin6->sin6_len != sizeof(*sin6)) {
1066 if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
1069 error = EAFNOSUPPORT;
1072 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1075 error = EAFNOSUPPORT;
1078 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1080 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1086 if ((inp->inp_vflag & INP_IPV4) == 0) {
1087 error = EAFNOSUPPORT;
1092 restoreflags = true;
1093 inp->inp_vflag &= ~INP_IPV6;
1095 in6_sin6_2_sin(sinp, sin6);
1097 ntohl(sinp->sin_addr.s_addr))) {
1098 error = EAFNOSUPPORT;
1103 if ((error = prison_remote_ip4(td->td_ucred,
1104 &sinp->sin_addr))) {
1111 error = EAFNOSUPPORT;
1117 if ((inp->inp_vflag & INP_IPV6) == 0) {
1120 error = EAFNOSUPPORT;
1123 restoreflags = true;
1124 inp->inp_vflag &= ~INP_IPV4;
1125 inp->inp_inc.inc_flags |= INC_ISIPV6;
1126 if ((error = prison_remote_ip6(td->td_ucred,
1127 &sin6->sin6_addr))) {
1140 error = EAFNOSUPPORT;
1145 /* TCP doesn't do control messages (rights, creds, etc) */
1146 if (control->m_len) {
1153 m_freem(control); /* empty control, just free it */
1155 if (!(flags & PRUS_OOB)) {
1156 sbappendstream(&so->so_snd, m, flags);
1157 if (nam && tp->t_state < TCPS_SYN_SENT) {
1159 * Do implied connect if not yet connected,
1160 * initialize window to default value, and
1161 * initialize maxseg using peer's cached MSS.
1165 error = tcp6_connect(tp, nam, td);
1167 #if defined(INET6) && defined(INET)
1171 error = tcp_connect(tp,
1172 (struct sockaddr *)sinp, td);
1175 * The bind operation in tcp_connect succeeded. We
1176 * no longer want to restore the flags if later
1179 if (error == 0 || inp->inp_lport != 0)
1180 restoreflags = false;
1184 if (IS_FASTOPEN(tp->t_flags))
1185 tcp_fastopen_connect(tp);
1187 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1191 if (flags & PRUS_EOF) {
1193 * Close the send side of the connection after
1199 if (!(inp->inp_flags & INP_DROPPED) &&
1200 !(flags & PRUS_NOTREADY)) {
1201 if (flags & PRUS_MORETOCOME)
1202 tp->t_flags |= TF_MORETOCOME;
1203 error = tp->t_fb->tfb_tcp_output(tp);
1204 if (flags & PRUS_MORETOCOME)
1205 tp->t_flags &= ~TF_MORETOCOME;
1209 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1211 SOCKBUF_LOCK(&so->so_snd);
1212 if (sbspace(&so->so_snd) < -512) {
1213 SOCKBUF_UNLOCK(&so->so_snd);
1219 * According to RFC961 (Assigned Protocols),
1220 * the urgent pointer points to the last octet
1221 * of urgent data. We continue, however,
1222 * to consider it to indicate the first octet
1223 * of data past the urgent section.
1224 * Otherwise, snd_up should be one lower.
1226 sbappendstream_locked(&so->so_snd, m, flags);
1227 SOCKBUF_UNLOCK(&so->so_snd);
1228 if (nam && tp->t_state < TCPS_SYN_SENT) {
1230 * Do implied connect if not yet connected,
1231 * initialize window to default value, and
1232 * initialize maxseg using peer's cached MSS.
1236 * Not going to contemplate SYN|URG
1238 if (IS_FASTOPEN(tp->t_flags))
1239 tp->t_flags &= ~TF_FASTOPEN;
1242 error = tcp6_connect(tp, nam, td);
1244 #if defined(INET6) && defined(INET)
1248 error = tcp_connect(tp,
1249 (struct sockaddr *)sinp, td);
1252 * The bind operation in tcp_connect succeeded. We
1253 * no longer want to restore the flags if later
1256 if (error == 0 || inp->inp_lport != 0)
1257 restoreflags = false;
1261 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1264 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1265 if (!(flags & PRUS_NOTREADY)) {
1266 tp->t_flags |= TF_FORCEDATA;
1267 error = tp->t_fb->tfb_tcp_output(tp);
1268 tp->t_flags &= ~TF_FORCEDATA;
1271 TCP_LOG_EVENT(tp, NULL,
1272 &inp->inp_socket->so_rcv,
1273 &inp->inp_socket->so_snd,
1274 TCP_LOG_USERSEND, error,
1278 * If the request was unsuccessful and we changed flags,
1279 * restore the original flags.
1281 if (error != 0 && restoreflags) {
1282 inp->inp_vflag = vflagsav;
1283 inp->inp_inc.inc_flags = incflagsav;
1285 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
1286 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1287 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1288 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1295 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1297 struct epoch_tracker et;
1302 inp = sotoinpcb(so);
1304 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1306 mb_free_notready(m, count);
1307 return (ECONNRESET);
1309 tp = intotcpcb(inp);
1311 SOCKBUF_LOCK(&so->so_snd);
1312 error = sbready(&so->so_snd, m, count);
1313 SOCKBUF_UNLOCK(&so->so_snd);
1315 NET_EPOCH_ENTER(et);
1316 error = tp->t_fb->tfb_tcp_output(tp);
1325 * Abort the TCP. Drop the connection abruptly.
1328 tcp_usr_abort(struct socket *so)
1331 struct tcpcb *tp = NULL;
1332 struct epoch_tracker et;
1335 inp = sotoinpcb(so);
1336 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1338 NET_EPOCH_ENTER(et);
1340 KASSERT(inp->inp_socket != NULL,
1341 ("tcp_usr_abort: inp_socket == NULL"));
1344 * If we still have full TCP state, and we're not dropped, drop.
1346 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1347 !(inp->inp_flags & INP_DROPPED)) {
1348 tp = intotcpcb(inp);
1350 tp = tcp_drop(tp, ECONNABORTED);
1353 TCPDEBUG2(PRU_ABORT);
1354 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1356 if (!(inp->inp_flags & INP_DROPPED)) {
1358 so->so_state |= SS_PROTOREF;
1360 inp->inp_flags |= INP_SOCKREF;
1368 * TCP socket is closed. Start friendly disconnect.
1371 tcp_usr_close(struct socket *so)
1374 struct tcpcb *tp = NULL;
1375 struct epoch_tracker et;
1378 inp = sotoinpcb(so);
1379 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1381 NET_EPOCH_ENTER(et);
1383 KASSERT(inp->inp_socket != NULL,
1384 ("tcp_usr_close: inp_socket == NULL"));
1387 * If we still have full TCP state, and we're not dropped, initiate
1390 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1391 !(inp->inp_flags & INP_DROPPED)) {
1392 tp = intotcpcb(inp);
1395 TCPDEBUG2(PRU_CLOSE);
1396 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1398 if (!(inp->inp_flags & INP_DROPPED)) {
1400 so->so_state |= SS_PROTOREF;
1402 inp->inp_flags |= INP_SOCKREF;
1409 tcp_pru_options_support(struct tcpcb *tp, int flags)
1412 * If the specific TCP stack has a pru_options
1413 * specified then it does not always support
1414 * all the PRU_XX options and we must ask it.
1415 * If the function is not specified then all
1416 * of the PRU_XX options are supported.
1420 if (tp->t_fb->tfb_pru_options) {
1421 ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1427 * Receive out-of-band data.
1430 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1434 struct tcpcb *tp = NULL;
1437 inp = sotoinpcb(so);
1438 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1440 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1444 tp = intotcpcb(inp);
1445 error = tcp_pru_options_support(tp, PRUS_OOB);
1450 if ((so->so_oobmark == 0 &&
1451 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1452 so->so_options & SO_OOBINLINE ||
1453 tp->t_oobflags & TCPOOB_HADDATA) {
1457 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1458 error = EWOULDBLOCK;
1462 *mtod(m, caddr_t) = tp->t_iobc;
1463 if ((flags & MSG_PEEK) == 0)
1464 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1467 TCPDEBUG2(PRU_RCVOOB);
1468 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1474 struct pr_usrreqs tcp_usrreqs = {
1475 .pru_abort = tcp_usr_abort,
1476 .pru_accept = tcp_usr_accept,
1477 .pru_attach = tcp_usr_attach,
1478 .pru_bind = tcp_usr_bind,
1479 .pru_connect = tcp_usr_connect,
1480 .pru_control = in_control,
1481 .pru_detach = tcp_usr_detach,
1482 .pru_disconnect = tcp_usr_disconnect,
1483 .pru_listen = tcp_usr_listen,
1484 .pru_peeraddr = in_getpeeraddr,
1485 .pru_rcvd = tcp_usr_rcvd,
1486 .pru_rcvoob = tcp_usr_rcvoob,
1487 .pru_send = tcp_usr_send,
1488 .pru_ready = tcp_usr_ready,
1489 .pru_shutdown = tcp_usr_shutdown,
1490 .pru_sockaddr = in_getsockaddr,
1491 .pru_sosetlabel = in_pcbsosetlabel,
1492 .pru_close = tcp_usr_close,
1497 struct pr_usrreqs tcp6_usrreqs = {
1498 .pru_abort = tcp_usr_abort,
1499 .pru_accept = tcp6_usr_accept,
1500 .pru_attach = tcp_usr_attach,
1501 .pru_bind = tcp6_usr_bind,
1502 .pru_connect = tcp6_usr_connect,
1503 .pru_control = in6_control,
1504 .pru_detach = tcp_usr_detach,
1505 .pru_disconnect = tcp_usr_disconnect,
1506 .pru_listen = tcp6_usr_listen,
1507 .pru_peeraddr = in6_mapped_peeraddr,
1508 .pru_rcvd = tcp_usr_rcvd,
1509 .pru_rcvoob = tcp_usr_rcvoob,
1510 .pru_send = tcp_usr_send,
1511 .pru_ready = tcp_usr_ready,
1512 .pru_shutdown = tcp_usr_shutdown,
1513 .pru_sockaddr = in6_mapped_sockaddr,
1514 .pru_sosetlabel = in_pcbsosetlabel,
1515 .pru_close = tcp_usr_close,
1521 * Common subroutine to open a TCP connection to remote host specified
1522 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1523 * port number if needed. Call in_pcbconnect_setup to do the routing and
1524 * to choose a local host address (interface). If there is an existing
1525 * incarnation of the same connection in TIME-WAIT state and if the remote
1526 * host was sending CC options and if the connection duration was < MSL, then
1527 * truncate the previous TIME-WAIT state and proceed.
1528 * Initialize connection parameters and enter SYN-SENT state.
1531 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1533 struct inpcb *inp = tp->t_inpcb, *oinp;
1534 struct socket *so = inp->inp_socket;
1535 struct in_addr laddr;
1540 INP_WLOCK_ASSERT(inp);
1541 INP_HASH_WLOCK(&V_tcbinfo);
1543 if (V_tcp_require_unique_port && inp->inp_lport == 0) {
1544 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1550 * Cannot simply call in_pcbconnect, because there might be an
1551 * earlier incarnation of this same connection still in
1552 * TIME_WAIT state, creating an ADDRINUSE error.
1554 laddr = inp->inp_laddr;
1555 lport = inp->inp_lport;
1556 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1557 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1558 if (error && oinp == NULL)
1564 /* Handle initial bind if it hadn't been done in advance. */
1565 if (inp->inp_lport == 0) {
1566 inp->inp_lport = lport;
1567 if (in_pcbinshash(inp) != 0) {
1573 inp->inp_laddr = laddr;
1575 INP_HASH_WUNLOCK(&V_tcbinfo);
1578 * Compute window scaling to request:
1579 * Scale to fit into sweet spot. See tcp_syncache.c.
1580 * XXX: This should move to tcp_output().
1582 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1583 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1584 tp->request_r_scale++;
1587 TCPSTAT_INC(tcps_connattempt);
1588 tcp_state_change(tp, TCPS_SYN_SENT);
1589 tp->iss = tcp_new_isn(&inp->inp_inc);
1590 if (tp->t_flags & TF_REQ_TSTMP)
1591 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1592 tcp_sendseqinit(tp);
1597 INP_HASH_WUNLOCK(&V_tcbinfo);
1604 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1606 struct inpcb *inp = tp->t_inpcb;
1609 INP_WLOCK_ASSERT(inp);
1610 INP_HASH_WLOCK(&V_tcbinfo);
1612 if (V_tcp_require_unique_port && inp->inp_lport == 0) {
1613 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1617 error = in6_pcbconnect(inp, nam, td->td_ucred);
1620 INP_HASH_WUNLOCK(&V_tcbinfo);
1622 /* Compute window scaling to request. */
1623 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1624 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1625 tp->request_r_scale++;
1627 soisconnecting(inp->inp_socket);
1628 TCPSTAT_INC(tcps_connattempt);
1629 tcp_state_change(tp, TCPS_SYN_SENT);
1630 tp->iss = tcp_new_isn(&inp->inp_inc);
1631 if (tp->t_flags & TF_REQ_TSTMP)
1632 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1633 tcp_sendseqinit(tp);
1638 INP_HASH_WUNLOCK(&V_tcbinfo);
1644 * Export TCP internal state information via a struct tcp_info, based on the
1645 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1646 * (TCP state machine, etc). We export all information using FreeBSD-native
1647 * constants -- for example, the numeric values for tcpi_state will differ
1651 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1654 INP_WLOCK_ASSERT(tp->t_inpcb);
1655 bzero(ti, sizeof(*ti));
1657 ti->tcpi_state = tp->t_state;
1658 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1659 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1660 if (tp->t_flags & TF_SACK_PERMIT)
1661 ti->tcpi_options |= TCPI_OPT_SACK;
1662 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1663 ti->tcpi_options |= TCPI_OPT_WSCALE;
1664 ti->tcpi_snd_wscale = tp->snd_scale;
1665 ti->tcpi_rcv_wscale = tp->rcv_scale;
1667 if (tp->t_flags2 & TF2_ECN_PERMIT)
1668 ti->tcpi_options |= TCPI_OPT_ECN;
1670 ti->tcpi_rto = tp->t_rxtcur * tick;
1671 ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1672 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1673 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1675 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1676 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1679 * FreeBSD-specific extension fields for tcp_info.
1681 ti->tcpi_rcv_space = tp->rcv_wnd;
1682 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1683 ti->tcpi_snd_wnd = tp->snd_wnd;
1684 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1685 ti->tcpi_snd_nxt = tp->snd_nxt;
1686 ti->tcpi_snd_mss = tp->t_maxseg;
1687 ti->tcpi_rcv_mss = tp->t_maxseg;
1688 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1689 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1690 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1692 if (tp->t_flags & TF_TOE) {
1693 ti->tcpi_options |= TCPI_OPT_TOE;
1694 tcp_offload_tcp_info(tp, ti);
1700 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1701 * socket option arguments. When it re-acquires the lock after the copy, it
1702 * has to revalidate that the connection is still valid for the socket
1705 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \
1707 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1710 return (ECONNRESET); \
1712 tp = intotcpcb(inp); \
1714 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1717 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1722 struct tcp_function_block *blk;
1723 struct tcp_function_set fsn;
1726 inp = sotoinpcb(so);
1727 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1728 if (sopt->sopt_level != IPPROTO_TCP) {
1730 if (inp->inp_vflag & INP_IPV6PROTO) {
1731 error = ip6_ctloutput(so, sopt);
1733 * In case of the IPV6_USE_MIN_MTU socket option,
1734 * the INC_IPV6MINMTU flag to announce a corresponding
1735 * MSS during the initial handshake.
1736 * If the TCP connection is not in the front states,
1737 * just reduce the MSS being used.
1738 * This avoids the sending of TCP segments which will
1739 * be fragmented at the IPv6 layer.
1742 (sopt->sopt_dir == SOPT_SET) &&
1743 (sopt->sopt_level == IPPROTO_IPV6) &&
1744 (sopt->sopt_name == IPV6_USE_MIN_MTU)) {
1746 if ((inp->inp_flags &
1747 (INP_TIMEWAIT | INP_DROPPED))) {
1749 return (ECONNRESET);
1751 inp->inp_inc.inc_flags |= INC_IPV6MINMTU;
1752 tp = intotcpcb(inp);
1753 if ((tp->t_state >= TCPS_SYN_SENT) &&
1754 (inp->inp_inc.inc_flags & INC_ISIPV6)) {
1755 struct ip6_pktopts *opt;
1757 opt = inp->in6p_outputopts;
1758 if ((opt != NULL) &&
1759 (opt->ip6po_minmtu ==
1760 IP6PO_MINMTU_ALL)) {
1761 if (tp->t_maxseg > TCP6_MSS) {
1762 tp->t_maxseg = TCP6_MSS;
1770 #if defined(INET6) && defined(INET)
1775 error = ip_ctloutput(so, sopt);
1781 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1783 return (ECONNRESET);
1785 tp = intotcpcb(inp);
1787 * Protect the TCP option TCP_FUNCTION_BLK so
1788 * that a sub-function can *never* overwrite this.
1790 if ((sopt->sopt_dir == SOPT_SET) &&
1791 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1793 error = sooptcopyin(sopt, &fsn, sizeof fsn,
1797 INP_WLOCK_RECHECK(inp);
1798 blk = find_and_ref_tcp_functions(&fsn);
1803 if (tp->t_fb == blk) {
1804 /* You already have this */
1805 refcount_release(&blk->tfb_refcnt);
1809 if (tp->t_state != TCPS_CLOSED) {
1811 * The user has advanced the state
1812 * past the initial point, we may not
1813 * be able to switch.
1815 if (blk->tfb_tcp_handoff_ok != NULL) {
1817 * Does the stack provide a
1818 * query mechanism, if so it may
1819 * still be possible?
1821 error = (*blk->tfb_tcp_handoff_ok)(tp);
1825 refcount_release(&blk->tfb_refcnt);
1830 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1831 refcount_release(&blk->tfb_refcnt);
1836 * Release the old refcnt, the
1837 * lookup acquired a ref on the
1840 if (tp->t_fb->tfb_tcp_fb_fini) {
1842 * Tell the stack to cleanup with 0 i.e.
1843 * the tcb is not going away.
1845 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1848 /* Assure that we are not on any hpts */
1849 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
1851 if (blk->tfb_tcp_fb_init) {
1852 error = (*blk->tfb_tcp_fb_init)(tp);
1854 refcount_release(&blk->tfb_refcnt);
1855 if (tp->t_fb->tfb_tcp_fb_init) {
1856 if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) {
1857 /* Fall back failed, drop the connection */
1866 refcount_release(&tp->t_fb->tfb_refcnt);
1869 if (tp->t_flags & TF_TOE) {
1870 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1877 } else if ((sopt->sopt_dir == SOPT_GET) &&
1878 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1879 strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1880 TCP_FUNCTION_NAME_LEN_MAX);
1881 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1882 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1884 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1887 /* Pass in the INP locked, called must unlock it */
1888 return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
1892 * If this assert becomes untrue, we need to change the size of the buf
1893 * variable in tcp_default_ctloutput().
1896 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1897 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1902 copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1904 struct tls_enable_v0 tls_v0;
1907 if (sopt->sopt_valsize == sizeof(tls_v0)) {
1908 error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1912 memset(tls, 0, sizeof(*tls));
1913 tls->cipher_key = tls_v0.cipher_key;
1914 tls->iv = tls_v0.iv;
1915 tls->auth_key = tls_v0.auth_key;
1916 tls->cipher_algorithm = tls_v0.cipher_algorithm;
1917 tls->cipher_key_len = tls_v0.cipher_key_len;
1918 tls->iv_len = tls_v0.iv_len;
1919 tls->auth_algorithm = tls_v0.auth_algorithm;
1920 tls->auth_key_len = tls_v0.auth_key_len;
1921 tls->flags = tls_v0.flags;
1922 tls->tls_vmajor = tls_v0.tls_vmajor;
1923 tls->tls_vminor = tls_v0.tls_vminor;
1927 return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1932 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
1934 int error, opt, optval;
1938 struct tls_enable tls;
1940 struct cc_algo *algo;
1941 char *pbuf, buf[TCP_LOG_ID_LEN];
1943 struct statsblob *sbp;
1948 * For TCP_CCALGOOPT forward the control to CC module, for both
1949 * SOPT_SET and SOPT_GET.
1951 switch (sopt->sopt_name) {
1954 if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
1956 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1957 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1958 sopt->sopt_valsize);
1963 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1964 if (CC_ALGO(tp)->ctl_output != NULL)
1965 error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1969 if (error == 0 && sopt->sopt_dir == SOPT_GET)
1970 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1975 switch (sopt->sopt_dir) {
1977 switch (sopt->sopt_name) {
1978 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1980 if (!TCPMD5_ENABLED()) {
1982 return (ENOPROTOOPT);
1984 error = TCPMD5_PCBCTL(inp, sopt);
1987 goto unlock_and_done;
1993 error = sooptcopyin(sopt, &optval, sizeof optval,
1998 INP_WLOCK_RECHECK(inp);
1999 switch (sopt->sopt_name) {
2007 opt = 0; /* dead code to fool gcc */
2014 tp->t_flags &= ~opt;
2017 if (tp->t_flags & TF_TOE) {
2018 tcp_offload_ctloutput(tp, sopt->sopt_dir,
2027 error = sooptcopyin(sopt, &optval, sizeof optval,
2032 INP_WLOCK_RECHECK(inp);
2034 tp->t_flags |= TF_NOPUSH;
2035 else if (tp->t_flags & TF_NOPUSH) {
2036 tp->t_flags &= ~TF_NOPUSH;
2037 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2038 struct epoch_tracker et;
2040 NET_EPOCH_ENTER(et);
2041 error = tp->t_fb->tfb_tcp_output(tp);
2045 goto unlock_and_done;
2049 error = sooptcopyin(sopt, &optval, sizeof optval,
2054 INP_WLOCK_RECHECK(inp);
2055 if (optval > 0 && optval <= tp->t_maxseg &&
2056 optval + 40 >= V_tcp_minmss)
2057 tp->t_maxseg = optval;
2060 goto unlock_and_done;
2070 error = sooptcopyin(sopt, &optval, sizeof optval,
2076 sbp = stats_blob_alloc(
2077 V_tcp_perconn_stats_dflt_tpl, 0);
2081 INP_WLOCK_RECHECK(inp);
2082 if ((tp->t_stats != NULL && sbp == NULL) ||
2083 (tp->t_stats == NULL && sbp != NULL)) {
2084 struct statsblob *t = tp->t_stats;
2090 stats_blob_destroy(sbp);
2092 return (EOPNOTSUPP);
2096 case TCP_CONGESTION:
2098 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
2101 buf[sopt->sopt_valsize] = '\0';
2102 INP_WLOCK_RECHECK(inp);
2104 STAILQ_FOREACH(algo, &cc_list, entries)
2105 if (strncmp(buf, algo->name,
2106 TCP_CA_NAME_MAX) == 0)
2115 * We hold a write lock over the tcb so it's safe to
2116 * do these things without ordering concerns.
2118 if (CC_ALGO(tp)->cb_destroy != NULL)
2119 CC_ALGO(tp)->cb_destroy(tp->ccv);
2123 * If something goes pear shaped initialising the new
2124 * algo, fall back to newreno (which does not
2125 * require initialisation).
2127 if (algo->cb_init != NULL &&
2128 algo->cb_init(tp->ccv) != 0) {
2129 CC_ALGO(tp) = &newreno_cc_algo;
2131 * The only reason init should fail is
2132 * because of malloc.
2140 case TCP_TXTLS_ENABLE:
2142 error = copyin_tls_enable(sopt, &tls);
2145 error = ktls_enable_tx(so, &tls);
2147 case TCP_TXTLS_MODE:
2149 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2153 INP_WLOCK_RECHECK(inp);
2154 error = ktls_set_tx_mode(so, ui);
2157 case TCP_RXTLS_ENABLE:
2159 error = sooptcopyin(sopt, &tls, sizeof(tls),
2163 error = ktls_enable_rx(so, &tls);
2171 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2175 if (ui > (UINT_MAX / hz)) {
2181 INP_WLOCK_RECHECK(inp);
2182 switch (sopt->sopt_name) {
2184 tp->t_keepidle = ui;
2186 * XXX: better check current remaining
2187 * timeout and "merge" it with new value.
2189 if ((tp->t_state > TCPS_LISTEN) &&
2190 (tp->t_state <= TCPS_CLOSING))
2191 tcp_timer_activate(tp, TT_KEEP,
2195 tp->t_keepintvl = ui;
2196 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2197 (TP_MAXIDLE(tp) > 0))
2198 tcp_timer_activate(tp, TT_2MSL,
2202 tp->t_keepinit = ui;
2203 if (tp->t_state == TCPS_SYN_RECEIVED ||
2204 tp->t_state == TCPS_SYN_SENT)
2205 tcp_timer_activate(tp, TT_KEEP,
2209 goto unlock_and_done;
2213 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2217 INP_WLOCK_RECHECK(inp);
2219 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2220 (TP_MAXIDLE(tp) > 0))
2221 tcp_timer_activate(tp, TT_2MSL,
2223 goto unlock_and_done;
2229 error = sooptcopyin(sopt, &optval, sizeof optval,
2234 INP_WLOCK_RECHECK(inp);
2236 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
2237 &(tp->t_outpkts) : &(tp->t_inpkts),
2241 goto unlock_and_done;
2244 case TCP_FASTOPEN: {
2245 struct tcp_fastopen tfo_optval;
2248 if (!V_tcp_fastopen_client_enable &&
2249 !V_tcp_fastopen_server_enable)
2252 error = sooptcopyin(sopt, &tfo_optval,
2253 sizeof(tfo_optval), sizeof(int));
2257 INP_WLOCK_RECHECK(inp);
2258 if ((tp->t_state != TCPS_CLOSED) &&
2259 (tp->t_state != TCPS_LISTEN)) {
2261 goto unlock_and_done;
2263 if (tfo_optval.enable) {
2264 if (tp->t_state == TCPS_LISTEN) {
2265 if (!V_tcp_fastopen_server_enable) {
2267 goto unlock_and_done;
2270 if (tp->t_tfo_pending == NULL)
2272 tcp_fastopen_alloc_counter();
2275 * If a pre-shared key was provided,
2276 * stash it in the client cookie
2277 * field of the tcpcb for use during
2280 if (sopt->sopt_valsize ==
2281 sizeof(tfo_optval)) {
2282 memcpy(tp->t_tfo_cookie.client,
2284 TCP_FASTOPEN_PSK_LEN);
2285 tp->t_tfo_client_cookie_len =
2286 TCP_FASTOPEN_PSK_LEN;
2289 tp->t_flags |= TF_FASTOPEN;
2291 tp->t_flags &= ~TF_FASTOPEN;
2292 goto unlock_and_done;
2298 error = sooptcopyin(sopt, &optval, sizeof optval,
2303 INP_WLOCK_RECHECK(inp);
2304 error = tcp_log_state_change(tp, optval);
2305 goto unlock_and_done;
2314 error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
2317 buf[sopt->sopt_valsize] = '\0';
2318 INP_WLOCK_RECHECK(inp);
2319 error = tcp_log_set_id(tp, buf);
2320 /* tcp_log_set_id() unlocks the INP. */
2327 sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
2330 buf[sopt->sopt_valsize] = '\0';
2331 INP_WLOCK_RECHECK(inp);
2332 if (sopt->sopt_name == TCP_LOGDUMP) {
2333 error = tcp_log_dump_tp_logbuf(tp, buf,
2337 tcp_log_dump_tp_bucket_logbufs(tp, buf);
2339 * tcp_log_dump_tp_bucket_logbufs() drops the
2348 error = ENOPROTOOPT;
2354 tp = intotcpcb(inp);
2355 switch (sopt->sopt_name) {
2356 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2358 if (!TCPMD5_ENABLED()) {
2360 return (ENOPROTOOPT);
2362 error = TCPMD5_PCBCTL(inp, sopt);
2367 optval = tp->t_flags & TF_NODELAY;
2369 error = sooptcopyout(sopt, &optval, sizeof optval);
2372 optval = tp->t_maxseg;
2374 error = sooptcopyout(sopt, &optval, sizeof optval);
2377 optval = tp->t_flags & TF_NOOPT;
2379 error = sooptcopyout(sopt, &optval, sizeof optval);
2382 optval = tp->t_flags & TF_NOPUSH;
2384 error = sooptcopyout(sopt, &optval, sizeof optval);
2387 tcp_fill_info(tp, &ti);
2389 error = sooptcopyout(sopt, &ti, sizeof ti);
2395 TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2398 socklen_t outsbsz = sopt->sopt_valsize;
2399 if (tp->t_stats == NULL)
2401 else if (outsbsz >= tp->t_stats->cursz)
2402 outsbsz = tp->t_stats->cursz;
2403 else if (outsbsz >= sizeof(struct statsblob))
2404 outsbsz = sizeof(struct statsblob);
2411 sbp = sopt->sopt_val;
2412 nheld = atop(round_page(((vm_offset_t)sbp) +
2413 (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2414 vm_page_t ma[nheld];
2415 if (vm_fault_quick_hold_pages(
2416 &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2417 outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2423 if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2424 SIZEOF_MEMBER(struct statsblob, flags))))
2427 INP_WLOCK_RECHECK(inp);
2428 error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2429 sbflags | SB_CLONE_USRDSTNOFAULT);
2431 sopt->sopt_valsize = outsbsz;
2433 vm_page_unhold_pages(ma, nheld);
2440 case TCP_CONGESTION:
2441 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2443 error = sooptcopyout(sopt, buf, len + 1);
2449 switch (sopt->sopt_name) {
2451 ui = TP_KEEPIDLE(tp) / hz;
2454 ui = TP_KEEPINTVL(tp) / hz;
2457 ui = TP_KEEPINIT(tp) / hz;
2460 ui = TP_KEEPCNT(tp);
2464 error = sooptcopyout(sopt, &ui, sizeof(ui));
2469 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
2470 &(tp->t_outpkts) : &(tp->t_inpkts));
2472 error = sooptcopyout(sopt, &optval, sizeof optval);
2476 optval = tp->t_flags & TF_FASTOPEN;
2478 error = sooptcopyout(sopt, &optval, sizeof optval);
2482 optval = tp->t_logstate;
2484 error = sooptcopyout(sopt, &optval, sizeof(optval));
2487 /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2488 error = tcp_log_getlogbuf(sopt, tp);
2491 len = tcp_log_get_id(tp, buf);
2493 error = sooptcopyout(sopt, buf, len + 1);
2502 case TCP_TXTLS_MODE:
2503 optval = ktls_get_tx_mode(so);
2505 error = sooptcopyout(sopt, &optval, sizeof(optval));
2507 case TCP_RXTLS_MODE:
2508 optval = ktls_get_rx_mode(so);
2510 error = sooptcopyout(sopt, &optval, sizeof(optval));
2515 error = ENOPROTOOPT;
2522 #undef INP_WLOCK_RECHECK
2523 #undef INP_WLOCK_RECHECK_CLEANUP
2526 * Initiate (or continue) disconnect.
2527 * If embryonic state, just send reset (once).
2528 * If in ``let data drain'' option and linger null, just drop.
2529 * Otherwise (hard), mark socket disconnecting and drop
2530 * current input data; switch states based on user close, and
2531 * send segment to peer (with FIN).
2534 tcp_disconnect(struct tcpcb *tp)
2536 struct inpcb *inp = tp->t_inpcb;
2537 struct socket *so = inp->inp_socket;
2540 INP_WLOCK_ASSERT(inp);
2543 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2544 * socket is still open.
2546 if (tp->t_state < TCPS_ESTABLISHED &&
2547 !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2550 ("tcp_disconnect: tcp_close() returned NULL"));
2551 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2552 tp = tcp_drop(tp, 0);
2554 ("tcp_disconnect: tcp_drop() returned NULL"));
2556 soisdisconnecting(so);
2557 sbflush(&so->so_rcv);
2559 if (!(inp->inp_flags & INP_DROPPED))
2560 tp->t_fb->tfb_tcp_output(tp);
2565 * User issued close, and wish to trail through shutdown states:
2566 * if never received SYN, just forget it. If got a SYN from peer,
2567 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2568 * If already got a FIN from peer, then almost done; go to LAST_ACK
2569 * state. In all other cases, have already sent FIN to peer (e.g.
2570 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2571 * for peer to send FIN or not respond to keep-alives, etc.
2572 * We can let the user exit from the close as soon as the FIN is acked.
2575 tcp_usrclosed(struct tcpcb *tp)
2579 INP_WLOCK_ASSERT(tp->t_inpcb);
2581 switch (tp->t_state) {
2584 tcp_offload_listen_stop(tp);
2586 tcp_state_change(tp, TCPS_CLOSED);
2591 * tcp_close() should never return NULL here as the socket is
2595 ("tcp_usrclosed: tcp_close() returned NULL"));
2599 case TCPS_SYN_RECEIVED:
2600 tp->t_flags |= TF_NEEDFIN;
2603 case TCPS_ESTABLISHED:
2604 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2607 case TCPS_CLOSE_WAIT:
2608 tcp_state_change(tp, TCPS_LAST_ACK);
2611 if (tp->t_state >= TCPS_FIN_WAIT_2) {
2612 soisdisconnected(tp->t_inpcb->inp_socket);
2613 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
2614 if (tp->t_state == TCPS_FIN_WAIT_2) {
2617 timeout = (tcp_fast_finwait2_recycle) ?
2618 tcp_finwait2_timeout : TP_MAXIDLE(tp);
2619 tcp_timer_activate(tp, TT_2MSL, timeout);
2626 db_print_indent(int indent)
2630 for (i = 0; i < indent; i++)
2635 db_print_tstate(int t_state)
2640 db_printf("TCPS_CLOSED");
2644 db_printf("TCPS_LISTEN");
2648 db_printf("TCPS_SYN_SENT");
2651 case TCPS_SYN_RECEIVED:
2652 db_printf("TCPS_SYN_RECEIVED");
2655 case TCPS_ESTABLISHED:
2656 db_printf("TCPS_ESTABLISHED");
2659 case TCPS_CLOSE_WAIT:
2660 db_printf("TCPS_CLOSE_WAIT");
2663 case TCPS_FIN_WAIT_1:
2664 db_printf("TCPS_FIN_WAIT_1");
2668 db_printf("TCPS_CLOSING");
2672 db_printf("TCPS_LAST_ACK");
2675 case TCPS_FIN_WAIT_2:
2676 db_printf("TCPS_FIN_WAIT_2");
2679 case TCPS_TIME_WAIT:
2680 db_printf("TCPS_TIME_WAIT");
2684 db_printf("unknown");
2690 db_print_tflags(u_int t_flags)
2695 if (t_flags & TF_ACKNOW) {
2696 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2699 if (t_flags & TF_DELACK) {
2700 db_printf("%sTF_DELACK", comma ? ", " : "");
2703 if (t_flags & TF_NODELAY) {
2704 db_printf("%sTF_NODELAY", comma ? ", " : "");
2707 if (t_flags & TF_NOOPT) {
2708 db_printf("%sTF_NOOPT", comma ? ", " : "");
2711 if (t_flags & TF_SENTFIN) {
2712 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2715 if (t_flags & TF_REQ_SCALE) {
2716 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2719 if (t_flags & TF_RCVD_SCALE) {
2720 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2723 if (t_flags & TF_REQ_TSTMP) {
2724 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2727 if (t_flags & TF_RCVD_TSTMP) {
2728 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2731 if (t_flags & TF_SACK_PERMIT) {
2732 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2735 if (t_flags & TF_NEEDSYN) {
2736 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2739 if (t_flags & TF_NEEDFIN) {
2740 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2743 if (t_flags & TF_NOPUSH) {
2744 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2747 if (t_flags & TF_MORETOCOME) {
2748 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2751 if (t_flags & TF_LQ_OVERFLOW) {
2752 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2755 if (t_flags & TF_LASTIDLE) {
2756 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2759 if (t_flags & TF_RXWIN0SENT) {
2760 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2763 if (t_flags & TF_FASTRECOVERY) {
2764 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2767 if (t_flags & TF_CONGRECOVERY) {
2768 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2771 if (t_flags & TF_WASFRECOVERY) {
2772 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2775 if (t_flags & TF_SIGNATURE) {
2776 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2779 if (t_flags & TF_FORCEDATA) {
2780 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2783 if (t_flags & TF_TSO) {
2784 db_printf("%sTF_TSO", comma ? ", " : "");
2787 if (t_flags & TF_FASTOPEN) {
2788 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2794 db_print_tflags2(u_int t_flags2)
2799 if (t_flags2 & TF2_ECN_PERMIT) {
2800 db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
2807 db_print_toobflags(char t_oobflags)
2812 if (t_oobflags & TCPOOB_HAVEDATA) {
2813 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2816 if (t_oobflags & TCPOOB_HADDATA) {
2817 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2823 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2826 db_print_indent(indent);
2827 db_printf("%s at %p\n", name, tp);
2831 db_print_indent(indent);
2832 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2833 TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2835 db_print_indent(indent);
2836 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
2837 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2839 db_print_indent(indent);
2840 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2841 &tp->t_timers->tt_delack, tp->t_inpcb);
2843 db_print_indent(indent);
2844 db_printf("t_state: %d (", tp->t_state);
2845 db_print_tstate(tp->t_state);
2848 db_print_indent(indent);
2849 db_printf("t_flags: 0x%x (", tp->t_flags);
2850 db_print_tflags(tp->t_flags);
2853 db_print_indent(indent);
2854 db_printf("t_flags2: 0x%x (", tp->t_flags2);
2855 db_print_tflags2(tp->t_flags2);
2858 db_print_indent(indent);
2859 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2860 tp->snd_una, tp->snd_max, tp->snd_nxt);
2862 db_print_indent(indent);
2863 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2864 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2866 db_print_indent(indent);
2867 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2868 tp->iss, tp->irs, tp->rcv_nxt);
2870 db_print_indent(indent);
2871 db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n",
2872 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2874 db_print_indent(indent);
2875 db_printf("snd_wnd: %u snd_cwnd: %u\n",
2876 tp->snd_wnd, tp->snd_cwnd);
2878 db_print_indent(indent);
2879 db_printf("snd_ssthresh: %u snd_recover: "
2880 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2882 db_print_indent(indent);
2883 db_printf("t_rcvtime: %u t_startime: %u\n",
2884 tp->t_rcvtime, tp->t_starttime);
2886 db_print_indent(indent);
2887 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2888 tp->t_rtttime, tp->t_rtseq);
2890 db_print_indent(indent);
2891 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2892 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2894 db_print_indent(indent);
2895 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2896 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2899 db_print_indent(indent);
2900 db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n",
2901 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2903 db_print_indent(indent);
2904 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2905 db_print_toobflags(tp->t_oobflags);
2906 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2908 db_print_indent(indent);
2909 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2910 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2912 db_print_indent(indent);
2913 db_printf("ts_recent: %u ts_recent_age: %u\n",
2914 tp->ts_recent, tp->ts_recent_age);
2916 db_print_indent(indent);
2917 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2918 "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2920 db_print_indent(indent);
2921 db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x "
2922 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2923 tp->snd_recover_prev, tp->t_badrxtwin);
2925 db_print_indent(indent);
2926 db_printf("snd_numholes: %d snd_holes first: %p\n",
2927 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2929 db_print_indent(indent);
2930 db_printf("snd_fack: 0x%08x rcv_numsacks: %d\n",
2931 tp->snd_fack, tp->rcv_numsacks);
2933 /* Skip sackblks, sackhint. */
2935 db_print_indent(indent);
2936 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2937 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2940 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2945 db_printf("usage: show tcpcb <addr>\n");
2948 tp = (struct tcpcb *)addr;
2950 db_print_tcpcb(tp, "tcpcb", 0);