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);
460 INP_HASH_WLOCK(&V_tcbinfo);
461 if (error == 0 && inp->inp_lport == 0)
462 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
463 INP_HASH_WUNLOCK(&V_tcbinfo);
465 tcp_state_change(tp, TCPS_LISTEN);
466 solisten_proto(so, backlog);
468 if ((so->so_options & SO_NO_OFFLOAD) == 0)
469 tcp_offload_listen_start(tp);
474 if (IS_FASTOPEN(tp->t_flags))
475 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
478 TCPDEBUG2(PRU_LISTEN);
479 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
487 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
491 struct tcpcb *tp = NULL;
496 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
498 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
502 vflagsav = inp->inp_vflag;
506 error = solisten_proto_check(so);
507 INP_HASH_WLOCK(&V_tcbinfo);
508 if (error == 0 && inp->inp_lport == 0) {
509 inp->inp_vflag &= ~INP_IPV4;
510 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
511 inp->inp_vflag |= INP_IPV4;
512 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
514 INP_HASH_WUNLOCK(&V_tcbinfo);
516 tcp_state_change(tp, TCPS_LISTEN);
517 solisten_proto(so, backlog);
519 if ((so->so_options & SO_NO_OFFLOAD) == 0)
520 tcp_offload_listen_start(tp);
525 if (IS_FASTOPEN(tp->t_flags))
526 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
529 inp->inp_vflag = vflagsav;
532 TCPDEBUG2(PRU_LISTEN);
533 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
541 * Initiate connection to peer.
542 * Create a template for use in transmissions on this connection.
543 * Enter SYN_SENT state, and mark socket as connecting.
544 * Start keep-alive timer, and seed output sequence space.
545 * Send initial segment on connection.
548 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
550 struct epoch_tracker et;
553 struct tcpcb *tp = NULL;
554 struct sockaddr_in *sinp;
556 sinp = (struct sockaddr_in *)nam;
557 if (nam->sa_family != AF_INET)
558 return (EAFNOSUPPORT);
559 if (nam->sa_len != sizeof (*sinp))
563 * Must disallow TCP ``connections'' to multicast addresses.
565 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
566 return (EAFNOSUPPORT);
567 if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST)
569 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
574 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
576 if (inp->inp_flags & INP_TIMEWAIT) {
580 if (inp->inp_flags & INP_DROPPED) {
581 error = ECONNREFUSED;
587 if ((error = tcp_connect(tp, nam, td)) != 0)
590 if (registered_toedevs > 0 &&
591 (so->so_options & SO_NO_OFFLOAD) == 0 &&
592 (error = tcp_offload_connect(so, nam)) == 0)
595 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
596 error = tp->t_fb->tfb_tcp_output(tp);
600 TCPDEBUG2(PRU_CONNECT);
601 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
609 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
611 struct epoch_tracker et;
614 struct tcpcb *tp = NULL;
615 struct sockaddr_in6 *sin6;
621 sin6 = (struct sockaddr_in6 *)nam;
622 if (nam->sa_family != AF_INET6)
623 return (EAFNOSUPPORT);
624 if (nam->sa_len != sizeof (*sin6))
628 * Must disallow TCP ``connections'' to multicast addresses.
630 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
631 return (EAFNOSUPPORT);
634 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
636 vflagsav = inp->inp_vflag;
637 incflagsav = inp->inp_inc.inc_flags;
638 if (inp->inp_flags & INP_TIMEWAIT) {
642 if (inp->inp_flags & INP_DROPPED) {
643 error = ECONNREFUSED;
650 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
651 * therefore probably require the hash lock, which isn't held here.
652 * Is this a significant problem?
654 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
655 struct sockaddr_in sin;
657 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
661 if ((inp->inp_vflag & INP_IPV4) == 0) {
662 error = EAFNOSUPPORT;
666 in6_sin6_2_sin(&sin, sin6);
667 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
668 error = EAFNOSUPPORT;
671 if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
675 if ((error = prison_remote_ip4(td->td_ucred,
676 &sin.sin_addr)) != 0)
678 inp->inp_vflag |= INP_IPV4;
679 inp->inp_vflag &= ~INP_IPV6;
681 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
684 if (registered_toedevs > 0 &&
685 (so->so_options & SO_NO_OFFLOAD) == 0 &&
686 (error = tcp_offload_connect(so, nam)) == 0)
689 error = tp->t_fb->tfb_tcp_output(tp);
692 if ((inp->inp_vflag & INP_IPV6) == 0) {
693 error = EAFNOSUPPORT;
698 if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
700 inp->inp_vflag &= ~INP_IPV4;
701 inp->inp_vflag |= INP_IPV6;
702 inp->inp_inc.inc_flags |= INC_ISIPV6;
704 if ((error = tcp6_connect(tp, nam, td)) != 0)
707 if (registered_toedevs > 0 &&
708 (so->so_options & SO_NO_OFFLOAD) == 0 &&
709 (error = tcp_offload_connect(so, nam)) == 0)
712 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
713 error = tp->t_fb->tfb_tcp_output(tp);
718 * If the implicit bind in the connect call fails, restore
719 * the flags we modified.
721 if (error != 0 && inp->inp_lport == 0) {
722 inp->inp_vflag = vflagsav;
723 inp->inp_inc.inc_flags = incflagsav;
726 TCPDEBUG2(PRU_CONNECT);
727 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
734 * Initiate disconnect from peer.
735 * If connection never passed embryonic stage, just drop;
736 * else if don't need to let data drain, then can just drop anyways,
737 * else have to begin TCP shutdown process: mark socket disconnecting,
738 * drain unread data, state switch to reflect user close, and
739 * send segment (e.g. FIN) to peer. Socket will be really disconnected
740 * when peer sends FIN and acks ours.
742 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
745 tcp_usr_disconnect(struct socket *so)
748 struct tcpcb *tp = NULL;
749 struct epoch_tracker et;
755 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
757 if (inp->inp_flags & INP_TIMEWAIT)
759 if (inp->inp_flags & INP_DROPPED) {
767 TCPDEBUG2(PRU_DISCONNECT);
768 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
776 * Accept a connection. Essentially all the work is done at higher levels;
777 * just return the address of the peer, storing through addr.
780 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
783 struct inpcb *inp = NULL;
784 struct tcpcb *tp = NULL;
789 if (so->so_state & SS_ISDISCONNECTED)
790 return (ECONNABORTED);
793 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
795 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
796 error = ECONNABORTED;
803 * We inline in_getpeeraddr and COMMON_END here, so that we can
804 * copy the data of interest and defer the malloc until after we
807 port = inp->inp_fport;
808 addr = inp->inp_faddr;
811 TCPDEBUG2(PRU_ACCEPT);
812 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
815 *nam = in_sockaddr(port, &addr);
822 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
824 struct inpcb *inp = NULL;
826 struct tcpcb *tp = NULL;
828 struct in6_addr addr6;
829 struct epoch_tracker et;
834 if (so->so_state & SS_ISDISCONNECTED)
835 return (ECONNABORTED);
838 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
841 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
842 error = ECONNABORTED;
849 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
850 * copy the data of interest and defer the malloc until after we
853 if (inp->inp_vflag & INP_IPV4) {
855 port = inp->inp_fport;
856 addr = inp->inp_faddr;
858 port = inp->inp_fport;
859 addr6 = inp->in6p_faddr;
863 TCPDEBUG2(PRU_ACCEPT);
864 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
869 *nam = in6_v4mapsin6_sockaddr(port, &addr);
871 *nam = in6_sockaddr(port, &addr6);
878 * Mark the connection as being incapable of further output.
881 tcp_usr_shutdown(struct socket *so)
885 struct tcpcb *tp = NULL;
886 struct epoch_tracker et;
891 KASSERT(inp != NULL, ("inp == NULL"));
893 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
901 if (!(inp->inp_flags & INP_DROPPED))
902 error = tp->t_fb->tfb_tcp_output(tp);
905 TCPDEBUG2(PRU_SHUTDOWN);
906 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
914 * After a receive, possibly send window update to peer.
917 tcp_usr_rcvd(struct socket *so, int flags)
919 struct epoch_tracker et;
921 struct tcpcb *tp = NULL;
926 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
928 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
935 * For passively-created TFO connections, don't attempt a window
936 * update while still in SYN_RECEIVED as this may trigger an early
937 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
938 * application response data, or failing that, when the DELACK timer
941 if (IS_FASTOPEN(tp->t_flags) &&
942 (tp->t_state == TCPS_SYN_RECEIVED))
946 if (tp->t_flags & TF_TOE)
947 tcp_offload_rcvd(tp);
950 tp->t_fb->tfb_tcp_output(tp);
954 TCP_PROBE2(debug__user, tp, PRU_RCVD);
960 * Do a send by putting data in output queue and updating urgent
961 * marker if URG set. Possibly send more data. Unlike the other
962 * pru_*() routines, the mbuf chains are our responsibility. We
963 * must either enqueue them or free them. The other pru_* routines
964 * generally are caller-frees.
967 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
968 struct sockaddr *nam, struct mbuf *control, struct thread *td)
970 struct epoch_tracker et;
973 struct tcpcb *tp = NULL;
976 struct sockaddr_in sin;
978 struct sockaddr_in *sinp;
989 * We require the pcbinfo "read lock" if we will close the socket
990 * as part of this call.
994 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
996 vflagsav = inp->inp_vflag;
997 incflagsav = inp->inp_inc.inc_flags;
998 restoreflags = false;
999 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1005 if (control != NULL) {
1006 /* TCP doesn't do control messages (rights, creds, etc) */
1007 if (control->m_len) {
1012 m_freem(control); /* empty control, just free it */
1015 tp = intotcpcb(inp);
1016 if ((flags & PRUS_OOB) != 0 &&
1017 (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
1021 if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
1022 switch (nam->sa_family) {
1025 sinp = (struct sockaddr_in *)nam;
1026 if (sinp->sin_len != sizeof(struct sockaddr_in)) {
1030 if ((inp->inp_vflag & INP_IPV6) != 0) {
1031 error = EAFNOSUPPORT;
1034 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
1035 error = EAFNOSUPPORT;
1038 if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
1042 if ((error = prison_remote_ip4(td->td_ucred,
1053 struct sockaddr_in6 *sin6;
1055 sin6 = (struct sockaddr_in6 *)nam;
1056 if (sin6->sin6_len != sizeof(*sin6)) {
1060 if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
1061 error = EAFNOSUPPORT;
1064 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1065 error = EAFNOSUPPORT;
1068 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1070 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1074 if ((inp->inp_vflag & INP_IPV4) == 0) {
1075 error = EAFNOSUPPORT;
1078 restoreflags = true;
1079 inp->inp_vflag &= ~INP_IPV6;
1081 in6_sin6_2_sin(sinp, sin6);
1083 ntohl(sinp->sin_addr.s_addr))) {
1084 error = EAFNOSUPPORT;
1087 if ((error = prison_remote_ip4(td->td_ucred,
1092 error = EAFNOSUPPORT;
1096 if ((inp->inp_vflag & INP_IPV6) == 0) {
1097 error = EAFNOSUPPORT;
1100 restoreflags = true;
1101 inp->inp_vflag &= ~INP_IPV4;
1102 inp->inp_inc.inc_flags |= INC_ISIPV6;
1103 if ((error = prison_remote_ip6(td->td_ucred,
1112 error = EAFNOSUPPORT;
1116 if (!(flags & PRUS_OOB)) {
1117 sbappendstream(&so->so_snd, m, flags);
1119 if (nam && tp->t_state < TCPS_SYN_SENT) {
1121 * Do implied connect if not yet connected,
1122 * initialize window to default value, and
1123 * initialize maxseg using peer's cached MSS.
1127 error = tcp6_connect(tp, nam, td);
1129 #if defined(INET6) && defined(INET)
1133 error = tcp_connect(tp,
1134 (struct sockaddr *)sinp, td);
1137 * The bind operation in tcp_connect succeeded. We
1138 * no longer want to restore the flags if later
1141 if (error == 0 || inp->inp_lport != 0)
1142 restoreflags = false;
1145 /* m is freed if PRUS_NOTREADY is unset. */
1146 sbflush(&so->so_snd);
1149 if (IS_FASTOPEN(tp->t_flags))
1150 tcp_fastopen_connect(tp);
1152 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1156 if (flags & PRUS_EOF) {
1158 * Close the send side of the connection after
1164 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1165 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1166 (tp->t_fbyte_out == 0) &&
1167 (so->so_snd.sb_ccc > 0)) {
1168 tp->t_fbyte_out = ticks;
1169 if (tp->t_fbyte_out == 0)
1170 tp->t_fbyte_out = 1;
1171 if (tp->t_fbyte_out && tp->t_fbyte_in)
1172 tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1174 if (!(inp->inp_flags & INP_DROPPED) &&
1175 !(flags & PRUS_NOTREADY)) {
1176 if (flags & PRUS_MORETOCOME)
1177 tp->t_flags |= TF_MORETOCOME;
1178 error = tp->t_fb->tfb_tcp_output(tp);
1179 if (flags & PRUS_MORETOCOME)
1180 tp->t_flags &= ~TF_MORETOCOME;
1184 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1186 SOCKBUF_LOCK(&so->so_snd);
1187 if (sbspace(&so->so_snd) < -512) {
1188 SOCKBUF_UNLOCK(&so->so_snd);
1193 * According to RFC961 (Assigned Protocols),
1194 * the urgent pointer points to the last octet
1195 * of urgent data. We continue, however,
1196 * to consider it to indicate the first octet
1197 * of data past the urgent section.
1198 * Otherwise, snd_up should be one lower.
1200 sbappendstream_locked(&so->so_snd, m, flags);
1201 SOCKBUF_UNLOCK(&so->so_snd);
1203 if (nam && tp->t_state < TCPS_SYN_SENT) {
1205 * Do implied connect if not yet connected,
1206 * initialize window to default value, and
1207 * initialize maxseg using peer's cached MSS.
1211 * Not going to contemplate SYN|URG
1213 if (IS_FASTOPEN(tp->t_flags))
1214 tp->t_flags &= ~TF_FASTOPEN;
1217 error = tcp6_connect(tp, nam, td);
1219 #if defined(INET6) && defined(INET)
1223 error = tcp_connect(tp,
1224 (struct sockaddr *)sinp, td);
1227 * The bind operation in tcp_connect succeeded. We
1228 * no longer want to restore the flags if later
1231 if (error == 0 || inp->inp_lport != 0)
1232 restoreflags = false;
1235 /* m is freed if PRUS_NOTREADY is unset. */
1236 sbflush(&so->so_snd);
1239 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1242 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1243 if ((flags & PRUS_NOTREADY) == 0) {
1244 tp->t_flags |= TF_FORCEDATA;
1245 error = tp->t_fb->tfb_tcp_output(tp);
1246 tp->t_flags &= ~TF_FORCEDATA;
1249 TCP_LOG_EVENT(tp, NULL,
1250 &inp->inp_socket->so_rcv,
1251 &inp->inp_socket->so_snd,
1252 TCP_LOG_USERSEND, error,
1257 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
1258 * responsible for freeing memory.
1260 if (m != NULL && (flags & PRUS_NOTREADY) == 0)
1264 * If the request was unsuccessful and we changed flags,
1265 * restore the original flags.
1267 if (error != 0 && restoreflags) {
1268 inp->inp_vflag = vflagsav;
1269 inp->inp_inc.inc_flags = incflagsav;
1271 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
1272 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1273 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1274 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1281 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1283 struct epoch_tracker et;
1288 inp = sotoinpcb(so);
1290 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1292 mb_free_notready(m, count);
1293 return (ECONNRESET);
1295 tp = intotcpcb(inp);
1297 SOCKBUF_LOCK(&so->so_snd);
1298 error = sbready(&so->so_snd, m, count);
1299 SOCKBUF_UNLOCK(&so->so_snd);
1301 NET_EPOCH_ENTER(et);
1302 error = tp->t_fb->tfb_tcp_output(tp);
1311 * Abort the TCP. Drop the connection abruptly.
1314 tcp_usr_abort(struct socket *so)
1317 struct tcpcb *tp = NULL;
1318 struct epoch_tracker et;
1321 inp = sotoinpcb(so);
1322 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1324 NET_EPOCH_ENTER(et);
1326 KASSERT(inp->inp_socket != NULL,
1327 ("tcp_usr_abort: inp_socket == NULL"));
1330 * If we still have full TCP state, and we're not dropped, drop.
1332 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1333 !(inp->inp_flags & INP_DROPPED)) {
1334 tp = intotcpcb(inp);
1336 tp = tcp_drop(tp, ECONNABORTED);
1339 TCPDEBUG2(PRU_ABORT);
1340 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1342 if (!(inp->inp_flags & INP_DROPPED)) {
1344 so->so_state |= SS_PROTOREF;
1346 inp->inp_flags |= INP_SOCKREF;
1354 * TCP socket is closed. Start friendly disconnect.
1357 tcp_usr_close(struct socket *so)
1360 struct tcpcb *tp = NULL;
1361 struct epoch_tracker et;
1364 inp = sotoinpcb(so);
1365 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1367 NET_EPOCH_ENTER(et);
1369 KASSERT(inp->inp_socket != NULL,
1370 ("tcp_usr_close: inp_socket == NULL"));
1373 * If we still have full TCP state, and we're not dropped, initiate
1376 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1377 !(inp->inp_flags & INP_DROPPED)) {
1378 tp = intotcpcb(inp);
1381 TCPDEBUG2(PRU_CLOSE);
1382 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1384 if (!(inp->inp_flags & INP_DROPPED)) {
1386 so->so_state |= SS_PROTOREF;
1388 inp->inp_flags |= INP_SOCKREF;
1395 tcp_pru_options_support(struct tcpcb *tp, int flags)
1398 * If the specific TCP stack has a pru_options
1399 * specified then it does not always support
1400 * all the PRU_XX options and we must ask it.
1401 * If the function is not specified then all
1402 * of the PRU_XX options are supported.
1406 if (tp->t_fb->tfb_pru_options) {
1407 ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1413 * Receive out-of-band data.
1416 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1420 struct tcpcb *tp = NULL;
1423 inp = sotoinpcb(so);
1424 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1426 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1430 tp = intotcpcb(inp);
1431 error = tcp_pru_options_support(tp, PRUS_OOB);
1436 if ((so->so_oobmark == 0 &&
1437 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1438 so->so_options & SO_OOBINLINE ||
1439 tp->t_oobflags & TCPOOB_HADDATA) {
1443 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1444 error = EWOULDBLOCK;
1448 *mtod(m, caddr_t) = tp->t_iobc;
1449 if ((flags & MSG_PEEK) == 0)
1450 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1453 TCPDEBUG2(PRU_RCVOOB);
1454 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1460 struct pr_usrreqs tcp_usrreqs = {
1461 .pru_abort = tcp_usr_abort,
1462 .pru_accept = tcp_usr_accept,
1463 .pru_attach = tcp_usr_attach,
1464 .pru_bind = tcp_usr_bind,
1465 .pru_connect = tcp_usr_connect,
1466 .pru_control = in_control,
1467 .pru_detach = tcp_usr_detach,
1468 .pru_disconnect = tcp_usr_disconnect,
1469 .pru_listen = tcp_usr_listen,
1470 .pru_peeraddr = in_getpeeraddr,
1471 .pru_rcvd = tcp_usr_rcvd,
1472 .pru_rcvoob = tcp_usr_rcvoob,
1473 .pru_send = tcp_usr_send,
1474 .pru_ready = tcp_usr_ready,
1475 .pru_shutdown = tcp_usr_shutdown,
1476 .pru_sockaddr = in_getsockaddr,
1477 .pru_sosetlabel = in_pcbsosetlabel,
1478 .pru_close = tcp_usr_close,
1483 struct pr_usrreqs tcp6_usrreqs = {
1484 .pru_abort = tcp_usr_abort,
1485 .pru_accept = tcp6_usr_accept,
1486 .pru_attach = tcp_usr_attach,
1487 .pru_bind = tcp6_usr_bind,
1488 .pru_connect = tcp6_usr_connect,
1489 .pru_control = in6_control,
1490 .pru_detach = tcp_usr_detach,
1491 .pru_disconnect = tcp_usr_disconnect,
1492 .pru_listen = tcp6_usr_listen,
1493 .pru_peeraddr = in6_mapped_peeraddr,
1494 .pru_rcvd = tcp_usr_rcvd,
1495 .pru_rcvoob = tcp_usr_rcvoob,
1496 .pru_send = tcp_usr_send,
1497 .pru_ready = tcp_usr_ready,
1498 .pru_shutdown = tcp_usr_shutdown,
1499 .pru_sockaddr = in6_mapped_sockaddr,
1500 .pru_sosetlabel = in_pcbsosetlabel,
1501 .pru_close = tcp_usr_close,
1507 * Common subroutine to open a TCP connection to remote host specified
1508 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1509 * port number if needed. Call in_pcbconnect_setup to do the routing and
1510 * to choose a local host address (interface). If there is an existing
1511 * incarnation of the same connection in TIME-WAIT state and if the remote
1512 * host was sending CC options and if the connection duration was < MSL, then
1513 * truncate the previous TIME-WAIT state and proceed.
1514 * Initialize connection parameters and enter SYN-SENT state.
1517 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1519 struct inpcb *inp = tp->t_inpcb, *oinp;
1520 struct socket *so = inp->inp_socket;
1521 struct in_addr laddr;
1526 INP_WLOCK_ASSERT(inp);
1527 INP_HASH_WLOCK(&V_tcbinfo);
1529 if (V_tcp_require_unique_port && inp->inp_lport == 0) {
1530 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1536 * Cannot simply call in_pcbconnect, because there might be an
1537 * earlier incarnation of this same connection still in
1538 * TIME_WAIT state, creating an ADDRINUSE error.
1540 laddr = inp->inp_laddr;
1541 lport = inp->inp_lport;
1542 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1543 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1544 if (error && oinp == NULL)
1550 /* Handle initial bind if it hadn't been done in advance. */
1551 if (inp->inp_lport == 0) {
1552 inp->inp_lport = lport;
1553 if (in_pcbinshash(inp) != 0) {
1559 inp->inp_laddr = laddr;
1561 INP_HASH_WUNLOCK(&V_tcbinfo);
1564 * Compute window scaling to request:
1565 * Scale to fit into sweet spot. See tcp_syncache.c.
1566 * XXX: This should move to tcp_output().
1568 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1569 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1570 tp->request_r_scale++;
1573 TCPSTAT_INC(tcps_connattempt);
1574 tcp_state_change(tp, TCPS_SYN_SENT);
1575 tp->iss = tcp_new_isn(&inp->inp_inc);
1576 if (tp->t_flags & TF_REQ_TSTMP)
1577 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1578 tcp_sendseqinit(tp);
1583 INP_HASH_WUNLOCK(&V_tcbinfo);
1590 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1592 struct inpcb *inp = tp->t_inpcb;
1595 INP_WLOCK_ASSERT(inp);
1596 INP_HASH_WLOCK(&V_tcbinfo);
1598 if (V_tcp_require_unique_port && inp->inp_lport == 0) {
1599 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1603 error = in6_pcbconnect(inp, nam, td->td_ucred);
1606 INP_HASH_WUNLOCK(&V_tcbinfo);
1608 /* Compute window scaling to request. */
1609 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1610 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1611 tp->request_r_scale++;
1613 soisconnecting(inp->inp_socket);
1614 TCPSTAT_INC(tcps_connattempt);
1615 tcp_state_change(tp, TCPS_SYN_SENT);
1616 tp->iss = tcp_new_isn(&inp->inp_inc);
1617 if (tp->t_flags & TF_REQ_TSTMP)
1618 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1619 tcp_sendseqinit(tp);
1624 INP_HASH_WUNLOCK(&V_tcbinfo);
1630 * Export TCP internal state information via a struct tcp_info, based on the
1631 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1632 * (TCP state machine, etc). We export all information using FreeBSD-native
1633 * constants -- for example, the numeric values for tcpi_state will differ
1637 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1640 INP_WLOCK_ASSERT(tp->t_inpcb);
1641 bzero(ti, sizeof(*ti));
1643 ti->tcpi_state = tp->t_state;
1644 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1645 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1646 if (tp->t_flags & TF_SACK_PERMIT)
1647 ti->tcpi_options |= TCPI_OPT_SACK;
1648 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1649 ti->tcpi_options |= TCPI_OPT_WSCALE;
1650 ti->tcpi_snd_wscale = tp->snd_scale;
1651 ti->tcpi_rcv_wscale = tp->rcv_scale;
1653 if (tp->t_flags2 & TF2_ECN_PERMIT)
1654 ti->tcpi_options |= TCPI_OPT_ECN;
1656 ti->tcpi_rto = tp->t_rxtcur * tick;
1657 ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1658 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1659 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1661 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1662 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1665 * FreeBSD-specific extension fields for tcp_info.
1667 ti->tcpi_rcv_space = tp->rcv_wnd;
1668 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1669 ti->tcpi_snd_wnd = tp->snd_wnd;
1670 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1671 ti->tcpi_snd_nxt = tp->snd_nxt;
1672 ti->tcpi_snd_mss = tp->t_maxseg;
1673 ti->tcpi_rcv_mss = tp->t_maxseg;
1674 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1675 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1676 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1678 if (tp->t_flags & TF_TOE) {
1679 ti->tcpi_options |= TCPI_OPT_TOE;
1680 tcp_offload_tcp_info(tp, ti);
1686 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1687 * socket option arguments. When it re-acquires the lock after the copy, it
1688 * has to revalidate that the connection is still valid for the socket
1691 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \
1693 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1696 return (ECONNRESET); \
1698 tp = intotcpcb(inp); \
1700 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1703 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1708 struct tcp_function_block *blk;
1709 struct tcp_function_set fsn;
1712 inp = sotoinpcb(so);
1713 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1714 if (sopt->sopt_level != IPPROTO_TCP) {
1716 if (inp->inp_vflag & INP_IPV6PROTO) {
1717 error = ip6_ctloutput(so, sopt);
1719 * In case of the IPV6_USE_MIN_MTU socket option,
1720 * the INC_IPV6MINMTU flag to announce a corresponding
1721 * MSS during the initial handshake.
1722 * If the TCP connection is not in the front states,
1723 * just reduce the MSS being used.
1724 * This avoids the sending of TCP segments which will
1725 * be fragmented at the IPv6 layer.
1728 (sopt->sopt_dir == SOPT_SET) &&
1729 (sopt->sopt_level == IPPROTO_IPV6) &&
1730 (sopt->sopt_name == IPV6_USE_MIN_MTU)) {
1732 if ((inp->inp_flags &
1733 (INP_TIMEWAIT | INP_DROPPED))) {
1735 return (ECONNRESET);
1737 inp->inp_inc.inc_flags |= INC_IPV6MINMTU;
1738 tp = intotcpcb(inp);
1739 if ((tp->t_state >= TCPS_SYN_SENT) &&
1740 (inp->inp_inc.inc_flags & INC_ISIPV6)) {
1741 struct ip6_pktopts *opt;
1743 opt = inp->in6p_outputopts;
1744 if ((opt != NULL) &&
1745 (opt->ip6po_minmtu ==
1746 IP6PO_MINMTU_ALL)) {
1747 if (tp->t_maxseg > TCP6_MSS) {
1748 tp->t_maxseg = TCP6_MSS;
1756 #if defined(INET6) && defined(INET)
1761 error = ip_ctloutput(so, sopt);
1767 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1769 return (ECONNRESET);
1771 tp = intotcpcb(inp);
1773 * Protect the TCP option TCP_FUNCTION_BLK so
1774 * that a sub-function can *never* overwrite this.
1776 if ((sopt->sopt_dir == SOPT_SET) &&
1777 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1779 error = sooptcopyin(sopt, &fsn, sizeof fsn,
1783 INP_WLOCK_RECHECK(inp);
1784 blk = find_and_ref_tcp_functions(&fsn);
1789 if (tp->t_fb == blk) {
1790 /* You already have this */
1791 refcount_release(&blk->tfb_refcnt);
1795 if (tp->t_state != TCPS_CLOSED) {
1797 * The user has advanced the state
1798 * past the initial point, we may not
1799 * be able to switch.
1801 if (blk->tfb_tcp_handoff_ok != NULL) {
1803 * Does the stack provide a
1804 * query mechanism, if so it may
1805 * still be possible?
1807 error = (*blk->tfb_tcp_handoff_ok)(tp);
1811 refcount_release(&blk->tfb_refcnt);
1816 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1817 refcount_release(&blk->tfb_refcnt);
1822 * Release the old refcnt, the
1823 * lookup acquired a ref on the
1826 if (tp->t_fb->tfb_tcp_fb_fini) {
1827 struct epoch_tracker et;
1829 * Tell the stack to cleanup with 0 i.e.
1830 * the tcb is not going away.
1832 NET_EPOCH_ENTER(et);
1833 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1837 /* Assure that we are not on any hpts */
1838 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
1840 if (blk->tfb_tcp_fb_init) {
1841 error = (*blk->tfb_tcp_fb_init)(tp);
1843 refcount_release(&blk->tfb_refcnt);
1844 if (tp->t_fb->tfb_tcp_fb_init) {
1845 if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) {
1846 /* Fall back failed, drop the connection */
1855 refcount_release(&tp->t_fb->tfb_refcnt);
1858 if (tp->t_flags & TF_TOE) {
1859 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1866 } else if ((sopt->sopt_dir == SOPT_GET) &&
1867 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1868 strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1869 TCP_FUNCTION_NAME_LEN_MAX);
1870 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1871 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1873 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1876 /* Pass in the INP locked, called must unlock it */
1877 return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
1881 * If this assert becomes untrue, we need to change the size of the buf
1882 * variable in tcp_default_ctloutput().
1885 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1886 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1891 copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1893 struct tls_enable_v0 tls_v0;
1896 if (sopt->sopt_valsize == sizeof(tls_v0)) {
1897 error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1901 memset(tls, 0, sizeof(*tls));
1902 tls->cipher_key = tls_v0.cipher_key;
1903 tls->iv = tls_v0.iv;
1904 tls->auth_key = tls_v0.auth_key;
1905 tls->cipher_algorithm = tls_v0.cipher_algorithm;
1906 tls->cipher_key_len = tls_v0.cipher_key_len;
1907 tls->iv_len = tls_v0.iv_len;
1908 tls->auth_algorithm = tls_v0.auth_algorithm;
1909 tls->auth_key_len = tls_v0.auth_key_len;
1910 tls->flags = tls_v0.flags;
1911 tls->tls_vmajor = tls_v0.tls_vmajor;
1912 tls->tls_vminor = tls_v0.tls_vminor;
1916 return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1921 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
1923 int error, opt, optval;
1927 struct tls_enable tls;
1929 struct cc_algo *algo;
1930 char *pbuf, buf[TCP_LOG_ID_LEN];
1932 struct statsblob *sbp;
1937 * For TCP_CCALGOOPT forward the control to CC module, for both
1938 * SOPT_SET and SOPT_GET.
1940 switch (sopt->sopt_name) {
1943 if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
1945 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1946 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1947 sopt->sopt_valsize);
1952 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1953 if (CC_ALGO(tp)->ctl_output != NULL)
1954 error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1958 if (error == 0 && sopt->sopt_dir == SOPT_GET)
1959 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1964 switch (sopt->sopt_dir) {
1966 switch (sopt->sopt_name) {
1967 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1970 if (!TCPMD5_ENABLED())
1971 return (ENOPROTOOPT);
1972 error = TCPMD5_PCBCTL(inp, sopt);
1975 INP_WLOCK_RECHECK(inp);
1976 goto unlock_and_done;
1982 error = sooptcopyin(sopt, &optval, sizeof optval,
1987 INP_WLOCK_RECHECK(inp);
1988 switch (sopt->sopt_name) {
1996 opt = 0; /* dead code to fool gcc */
2003 tp->t_flags &= ~opt;
2006 if (tp->t_flags & TF_TOE) {
2007 tcp_offload_ctloutput(tp, sopt->sopt_dir,
2016 error = sooptcopyin(sopt, &optval, sizeof optval,
2021 INP_WLOCK_RECHECK(inp);
2023 tp->t_flags |= TF_NOPUSH;
2024 else if (tp->t_flags & TF_NOPUSH) {
2025 tp->t_flags &= ~TF_NOPUSH;
2026 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2027 struct epoch_tracker et;
2029 NET_EPOCH_ENTER(et);
2030 error = tp->t_fb->tfb_tcp_output(tp);
2034 goto unlock_and_done;
2036 case TCP_REMOTE_UDP_ENCAPS_PORT:
2038 error = sooptcopyin(sopt, &optval, sizeof optval,
2042 if ((optval < TCP_TUNNELING_PORT_MIN) ||
2043 (optval > TCP_TUNNELING_PORT_MAX)) {
2044 /* Its got to be in range */
2047 if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
2048 /* You have to have enabled a UDP tunneling port first */
2051 INP_WLOCK_RECHECK(inp);
2052 if (tp->t_state != TCPS_CLOSED) {
2053 /* You can't change after you are connected */
2056 /* Ok we are all good set the port */
2057 tp->t_port = htons(optval);
2059 goto unlock_and_done;
2063 error = sooptcopyin(sopt, &optval, sizeof optval,
2068 INP_WLOCK_RECHECK(inp);
2069 if (optval > 0 && optval <= tp->t_maxseg &&
2070 optval + 40 >= V_tcp_minmss)
2071 tp->t_maxseg = optval;
2074 goto unlock_and_done;
2084 error = sooptcopyin(sopt, &optval, sizeof optval,
2090 sbp = stats_blob_alloc(
2091 V_tcp_perconn_stats_dflt_tpl, 0);
2095 INP_WLOCK_RECHECK(inp);
2096 if ((tp->t_stats != NULL && sbp == NULL) ||
2097 (tp->t_stats == NULL && sbp != NULL)) {
2098 struct statsblob *t = tp->t_stats;
2104 stats_blob_destroy(sbp);
2106 return (EOPNOTSUPP);
2110 case TCP_CONGESTION:
2112 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
2115 buf[sopt->sopt_valsize] = '\0';
2116 INP_WLOCK_RECHECK(inp);
2118 STAILQ_FOREACH(algo, &cc_list, entries)
2119 if (strncmp(buf, algo->name,
2120 TCP_CA_NAME_MAX) == 0)
2129 * We hold a write lock over the tcb so it's safe to
2130 * do these things without ordering concerns.
2132 if (CC_ALGO(tp)->cb_destroy != NULL)
2133 CC_ALGO(tp)->cb_destroy(tp->ccv);
2137 * If something goes pear shaped initialising the new
2138 * algo, fall back to newreno (which does not
2139 * require initialisation).
2141 if (algo->cb_init != NULL &&
2142 algo->cb_init(tp->ccv) != 0) {
2143 CC_ALGO(tp) = &newreno_cc_algo;
2145 * The only reason init should fail is
2146 * because of malloc.
2153 case TCP_REUSPORT_LB_NUMA:
2155 error = sooptcopyin(sopt, &optval, sizeof(optval),
2157 INP_WLOCK_RECHECK(inp);
2159 error = in_pcblbgroup_numa(inp, optval);
2164 case TCP_TXTLS_ENABLE:
2166 error = copyin_tls_enable(sopt, &tls);
2169 error = ktls_enable_tx(so, &tls);
2171 case TCP_TXTLS_MODE:
2173 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2177 INP_WLOCK_RECHECK(inp);
2178 error = ktls_set_tx_mode(so, ui);
2181 case TCP_RXTLS_ENABLE:
2183 error = sooptcopyin(sopt, &tls, sizeof(tls),
2187 error = ktls_enable_rx(so, &tls);
2195 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2199 if (ui > (UINT_MAX / hz)) {
2205 INP_WLOCK_RECHECK(inp);
2206 switch (sopt->sopt_name) {
2208 tp->t_keepidle = ui;
2210 * XXX: better check current remaining
2211 * timeout and "merge" it with new value.
2213 if ((tp->t_state > TCPS_LISTEN) &&
2214 (tp->t_state <= TCPS_CLOSING))
2215 tcp_timer_activate(tp, TT_KEEP,
2219 tp->t_keepintvl = ui;
2220 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2221 (TP_MAXIDLE(tp) > 0))
2222 tcp_timer_activate(tp, TT_2MSL,
2226 tp->t_keepinit = ui;
2227 if (tp->t_state == TCPS_SYN_RECEIVED ||
2228 tp->t_state == TCPS_SYN_SENT)
2229 tcp_timer_activate(tp, TT_KEEP,
2233 goto unlock_and_done;
2237 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2241 INP_WLOCK_RECHECK(inp);
2243 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2244 (TP_MAXIDLE(tp) > 0))
2245 tcp_timer_activate(tp, TT_2MSL,
2247 goto unlock_and_done;
2253 error = sooptcopyin(sopt, &optval, sizeof optval,
2258 INP_WLOCK_RECHECK(inp);
2260 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
2261 &(tp->t_outpkts) : &(tp->t_inpkts),
2265 goto unlock_and_done;
2268 case TCP_FASTOPEN: {
2269 struct tcp_fastopen tfo_optval;
2272 if (!V_tcp_fastopen_client_enable &&
2273 !V_tcp_fastopen_server_enable)
2276 error = sooptcopyin(sopt, &tfo_optval,
2277 sizeof(tfo_optval), sizeof(int));
2281 INP_WLOCK_RECHECK(inp);
2282 if ((tp->t_state != TCPS_CLOSED) &&
2283 (tp->t_state != TCPS_LISTEN)) {
2285 goto unlock_and_done;
2287 if (tfo_optval.enable) {
2288 if (tp->t_state == TCPS_LISTEN) {
2289 if (!V_tcp_fastopen_server_enable) {
2291 goto unlock_and_done;
2294 if (tp->t_tfo_pending == NULL)
2296 tcp_fastopen_alloc_counter();
2299 * If a pre-shared key was provided,
2300 * stash it in the client cookie
2301 * field of the tcpcb for use during
2304 if (sopt->sopt_valsize ==
2305 sizeof(tfo_optval)) {
2306 memcpy(tp->t_tfo_cookie.client,
2308 TCP_FASTOPEN_PSK_LEN);
2309 tp->t_tfo_client_cookie_len =
2310 TCP_FASTOPEN_PSK_LEN;
2313 tp->t_flags |= TF_FASTOPEN;
2315 tp->t_flags &= ~TF_FASTOPEN;
2316 goto unlock_and_done;
2322 error = sooptcopyin(sopt, &optval, sizeof optval,
2327 INP_WLOCK_RECHECK(inp);
2328 error = tcp_log_state_change(tp, optval);
2329 goto unlock_and_done;
2338 error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
2341 buf[sopt->sopt_valsize] = '\0';
2342 INP_WLOCK_RECHECK(inp);
2343 error = tcp_log_set_id(tp, buf);
2344 /* tcp_log_set_id() unlocks the INP. */
2351 sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
2354 buf[sopt->sopt_valsize] = '\0';
2355 INP_WLOCK_RECHECK(inp);
2356 if (sopt->sopt_name == TCP_LOGDUMP) {
2357 error = tcp_log_dump_tp_logbuf(tp, buf,
2361 tcp_log_dump_tp_bucket_logbufs(tp, buf);
2363 * tcp_log_dump_tp_bucket_logbufs() drops the
2372 error = ENOPROTOOPT;
2378 tp = intotcpcb(inp);
2379 switch (sopt->sopt_name) {
2380 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2383 if (!TCPMD5_ENABLED())
2384 return (ENOPROTOOPT);
2385 error = TCPMD5_PCBCTL(inp, sopt);
2390 optval = tp->t_flags & TF_NODELAY;
2392 error = sooptcopyout(sopt, &optval, sizeof optval);
2395 optval = tp->t_maxseg;
2397 error = sooptcopyout(sopt, &optval, sizeof optval);
2399 case TCP_REMOTE_UDP_ENCAPS_PORT:
2400 optval = ntohs(tp->t_port);
2402 error = sooptcopyout(sopt, &optval, sizeof optval);
2405 optval = tp->t_flags & TF_NOOPT;
2407 error = sooptcopyout(sopt, &optval, sizeof optval);
2410 optval = tp->t_flags & TF_NOPUSH;
2412 error = sooptcopyout(sopt, &optval, sizeof optval);
2415 tcp_fill_info(tp, &ti);
2417 error = sooptcopyout(sopt, &ti, sizeof ti);
2423 TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2426 socklen_t outsbsz = sopt->sopt_valsize;
2427 if (tp->t_stats == NULL)
2429 else if (outsbsz >= tp->t_stats->cursz)
2430 outsbsz = tp->t_stats->cursz;
2431 else if (outsbsz >= sizeof(struct statsblob))
2432 outsbsz = sizeof(struct statsblob);
2439 sbp = sopt->sopt_val;
2440 nheld = atop(round_page(((vm_offset_t)sbp) +
2441 (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2442 vm_page_t ma[nheld];
2443 if (vm_fault_quick_hold_pages(
2444 &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2445 outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2451 if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2452 SIZEOF_MEMBER(struct statsblob, flags))))
2455 INP_WLOCK_RECHECK(inp);
2456 error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2457 sbflags | SB_CLONE_USRDSTNOFAULT);
2459 sopt->sopt_valsize = outsbsz;
2461 vm_page_unhold_pages(ma, nheld);
2468 case TCP_CONGESTION:
2469 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2471 error = sooptcopyout(sopt, buf, len + 1);
2477 switch (sopt->sopt_name) {
2479 ui = TP_KEEPIDLE(tp) / hz;
2482 ui = TP_KEEPINTVL(tp) / hz;
2485 ui = TP_KEEPINIT(tp) / hz;
2488 ui = TP_KEEPCNT(tp);
2492 error = sooptcopyout(sopt, &ui, sizeof(ui));
2497 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
2498 &(tp->t_outpkts) : &(tp->t_inpkts));
2500 error = sooptcopyout(sopt, &optval, sizeof optval);
2504 optval = tp->t_flags & TF_FASTOPEN;
2506 error = sooptcopyout(sopt, &optval, sizeof optval);
2510 optval = tp->t_logstate;
2512 error = sooptcopyout(sopt, &optval, sizeof(optval));
2515 /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2516 error = tcp_log_getlogbuf(sopt, tp);
2519 len = tcp_log_get_id(tp, buf);
2521 error = sooptcopyout(sopt, buf, len + 1);
2530 case TCP_TXTLS_MODE:
2531 optval = ktls_get_tx_mode(so);
2533 error = sooptcopyout(sopt, &optval, sizeof(optval));
2535 case TCP_RXTLS_MODE:
2536 optval = ktls_get_rx_mode(so);
2538 error = sooptcopyout(sopt, &optval, sizeof(optval));
2543 error = ENOPROTOOPT;
2550 #undef INP_WLOCK_RECHECK
2551 #undef INP_WLOCK_RECHECK_CLEANUP
2554 * Initiate (or continue) disconnect.
2555 * If embryonic state, just send reset (once).
2556 * If in ``let data drain'' option and linger null, just drop.
2557 * Otherwise (hard), mark socket disconnecting and drop
2558 * current input data; switch states based on user close, and
2559 * send segment to peer (with FIN).
2562 tcp_disconnect(struct tcpcb *tp)
2564 struct inpcb *inp = tp->t_inpcb;
2565 struct socket *so = inp->inp_socket;
2568 INP_WLOCK_ASSERT(inp);
2571 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2572 * socket is still open.
2574 if (tp->t_state < TCPS_ESTABLISHED &&
2575 !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2578 ("tcp_disconnect: tcp_close() returned NULL"));
2579 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2580 tp = tcp_drop(tp, 0);
2582 ("tcp_disconnect: tcp_drop() returned NULL"));
2584 soisdisconnecting(so);
2585 sbflush(&so->so_rcv);
2587 if (!(inp->inp_flags & INP_DROPPED))
2588 tp->t_fb->tfb_tcp_output(tp);
2593 * User issued close, and wish to trail through shutdown states:
2594 * if never received SYN, just forget it. If got a SYN from peer,
2595 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2596 * If already got a FIN from peer, then almost done; go to LAST_ACK
2597 * state. In all other cases, have already sent FIN to peer (e.g.
2598 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2599 * for peer to send FIN or not respond to keep-alives, etc.
2600 * We can let the user exit from the close as soon as the FIN is acked.
2603 tcp_usrclosed(struct tcpcb *tp)
2607 INP_WLOCK_ASSERT(tp->t_inpcb);
2609 switch (tp->t_state) {
2612 tcp_offload_listen_stop(tp);
2614 tcp_state_change(tp, TCPS_CLOSED);
2619 * tcp_close() should never return NULL here as the socket is
2623 ("tcp_usrclosed: tcp_close() returned NULL"));
2627 case TCPS_SYN_RECEIVED:
2628 tp->t_flags |= TF_NEEDFIN;
2631 case TCPS_ESTABLISHED:
2632 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2635 case TCPS_CLOSE_WAIT:
2636 tcp_state_change(tp, TCPS_LAST_ACK);
2639 if (tp->t_state >= TCPS_FIN_WAIT_2) {
2640 soisdisconnected(tp->t_inpcb->inp_socket);
2641 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
2642 if (tp->t_state == TCPS_FIN_WAIT_2) {
2645 timeout = (tcp_fast_finwait2_recycle) ?
2646 tcp_finwait2_timeout : TP_MAXIDLE(tp);
2647 tcp_timer_activate(tp, TT_2MSL, timeout);
2654 db_print_indent(int indent)
2658 for (i = 0; i < indent; i++)
2663 db_print_tstate(int t_state)
2668 db_printf("TCPS_CLOSED");
2672 db_printf("TCPS_LISTEN");
2676 db_printf("TCPS_SYN_SENT");
2679 case TCPS_SYN_RECEIVED:
2680 db_printf("TCPS_SYN_RECEIVED");
2683 case TCPS_ESTABLISHED:
2684 db_printf("TCPS_ESTABLISHED");
2687 case TCPS_CLOSE_WAIT:
2688 db_printf("TCPS_CLOSE_WAIT");
2691 case TCPS_FIN_WAIT_1:
2692 db_printf("TCPS_FIN_WAIT_1");
2696 db_printf("TCPS_CLOSING");
2700 db_printf("TCPS_LAST_ACK");
2703 case TCPS_FIN_WAIT_2:
2704 db_printf("TCPS_FIN_WAIT_2");
2707 case TCPS_TIME_WAIT:
2708 db_printf("TCPS_TIME_WAIT");
2712 db_printf("unknown");
2718 db_print_tflags(u_int t_flags)
2723 if (t_flags & TF_ACKNOW) {
2724 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2727 if (t_flags & TF_DELACK) {
2728 db_printf("%sTF_DELACK", comma ? ", " : "");
2731 if (t_flags & TF_NODELAY) {
2732 db_printf("%sTF_NODELAY", comma ? ", " : "");
2735 if (t_flags & TF_NOOPT) {
2736 db_printf("%sTF_NOOPT", comma ? ", " : "");
2739 if (t_flags & TF_SENTFIN) {
2740 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2743 if (t_flags & TF_REQ_SCALE) {
2744 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2747 if (t_flags & TF_RCVD_SCALE) {
2748 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2751 if (t_flags & TF_REQ_TSTMP) {
2752 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2755 if (t_flags & TF_RCVD_TSTMP) {
2756 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2759 if (t_flags & TF_SACK_PERMIT) {
2760 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2763 if (t_flags & TF_NEEDSYN) {
2764 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2767 if (t_flags & TF_NEEDFIN) {
2768 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2771 if (t_flags & TF_NOPUSH) {
2772 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2775 if (t_flags & TF_MORETOCOME) {
2776 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2779 if (t_flags & TF_LQ_OVERFLOW) {
2780 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2783 if (t_flags & TF_LASTIDLE) {
2784 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2787 if (t_flags & TF_RXWIN0SENT) {
2788 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2791 if (t_flags & TF_FASTRECOVERY) {
2792 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2795 if (t_flags & TF_CONGRECOVERY) {
2796 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2799 if (t_flags & TF_WASFRECOVERY) {
2800 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2803 if (t_flags & TF_SIGNATURE) {
2804 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2807 if (t_flags & TF_FORCEDATA) {
2808 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2811 if (t_flags & TF_TSO) {
2812 db_printf("%sTF_TSO", comma ? ", " : "");
2815 if (t_flags & TF_FASTOPEN) {
2816 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2822 db_print_tflags2(u_int t_flags2)
2827 if (t_flags2 & TF2_ECN_PERMIT) {
2828 db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
2834 db_print_toobflags(char t_oobflags)
2839 if (t_oobflags & TCPOOB_HAVEDATA) {
2840 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2843 if (t_oobflags & TCPOOB_HADDATA) {
2844 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2850 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2853 db_print_indent(indent);
2854 db_printf("%s at %p\n", name, tp);
2858 db_print_indent(indent);
2859 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2860 TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2862 db_print_indent(indent);
2863 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
2864 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2866 db_print_indent(indent);
2867 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2868 &tp->t_timers->tt_delack, tp->t_inpcb);
2870 db_print_indent(indent);
2871 db_printf("t_state: %d (", tp->t_state);
2872 db_print_tstate(tp->t_state);
2875 db_print_indent(indent);
2876 db_printf("t_flags: 0x%x (", tp->t_flags);
2877 db_print_tflags(tp->t_flags);
2880 db_print_indent(indent);
2881 db_printf("t_flags2: 0x%x (", tp->t_flags2);
2882 db_print_tflags2(tp->t_flags2);
2885 db_print_indent(indent);
2886 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2887 tp->snd_una, tp->snd_max, tp->snd_nxt);
2889 db_print_indent(indent);
2890 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2891 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2893 db_print_indent(indent);
2894 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2895 tp->iss, tp->irs, tp->rcv_nxt);
2897 db_print_indent(indent);
2898 db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n",
2899 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2901 db_print_indent(indent);
2902 db_printf("snd_wnd: %u snd_cwnd: %u\n",
2903 tp->snd_wnd, tp->snd_cwnd);
2905 db_print_indent(indent);
2906 db_printf("snd_ssthresh: %u snd_recover: "
2907 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2909 db_print_indent(indent);
2910 db_printf("t_rcvtime: %u t_startime: %u\n",
2911 tp->t_rcvtime, tp->t_starttime);
2913 db_print_indent(indent);
2914 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2915 tp->t_rtttime, tp->t_rtseq);
2917 db_print_indent(indent);
2918 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2919 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2921 db_print_indent(indent);
2922 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2923 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2926 db_print_indent(indent);
2927 db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n",
2928 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2930 db_print_indent(indent);
2931 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2932 db_print_toobflags(tp->t_oobflags);
2933 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2935 db_print_indent(indent);
2936 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2937 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2939 db_print_indent(indent);
2940 db_printf("ts_recent: %u ts_recent_age: %u\n",
2941 tp->ts_recent, tp->ts_recent_age);
2943 db_print_indent(indent);
2944 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2945 "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2947 db_print_indent(indent);
2948 db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x "
2949 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2950 tp->snd_recover_prev, tp->t_badrxtwin);
2952 db_print_indent(indent);
2953 db_printf("snd_numholes: %d snd_holes first: %p\n",
2954 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2956 db_print_indent(indent);
2957 db_printf("snd_fack: 0x%08x rcv_numsacks: %d\n",
2958 tp->snd_fack, tp->rcv_numsacks);
2960 /* Skip sackblks, sackhint. */
2962 db_print_indent(indent);
2963 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2964 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2967 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2972 db_printf("usage: show tcpcb <addr>\n");
2975 tp = (struct tcpcb *)addr;
2977 db_print_tcpcb(tp, "tcpcb", 0);