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 attaches to socket via pru_attach(), reserving space,
151 * and an internet control block.
154 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
157 struct tcpcb *tp = NULL;
162 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
165 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
166 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
171 so->so_rcv.sb_flags |= SB_AUTOSIZE;
172 so->so_snd.sb_flags |= SB_AUTOSIZE;
173 error = in_pcballoc(so, &V_tcbinfo);
178 if (inp->inp_vflag & INP_IPV6PROTO) {
179 inp->inp_vflag |= INP_IPV6;
180 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
181 inp->inp_vflag |= INP_IPV4;
182 inp->in6p_hops = -1; /* use kernel default */
186 inp->inp_vflag |= INP_IPV4;
187 tp = tcp_newtcpcb(inp);
194 tp->t_state = TCPS_CLOSED;
196 TCPSTATES_INC(TCPS_CLOSED);
197 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
198 so->so_linger = TCP_LINGERTIME;
200 TCPDEBUG2(PRU_ATTACH);
201 TCP_PROBE2(debug__user, tp, PRU_ATTACH);
206 * tcp_usr_detach is called when the socket layer loses its final reference
207 * to the socket, be it a file descriptor reference, a reference from TCP,
208 * etc. At this point, there is only one case in which we will keep around
209 * inpcb state: time wait.
212 tcp_usr_detach(struct socket *so)
218 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
220 KASSERT(so->so_pcb == inp && inp->inp_socket == so,
221 ("%s: socket %p inp %p mismatch", __func__, so, inp));
225 if (inp->inp_flags & INP_TIMEWAIT) {
227 * There are two cases to handle: one in which the time wait
228 * state is being discarded (INP_DROPPED), and one in which
229 * this connection will remain in timewait. In the former,
230 * it is time to discard all state (except tcptw, which has
231 * already been discarded by the timewait close code, which
232 * should be further up the call stack somewhere). In the
233 * latter case, we detach from the socket, but leave the pcb
234 * present until timewait ends.
236 * XXXRW: Would it be cleaner to free the tcptw here?
238 * Astute question indeed, from twtcp perspective there are
239 * four cases to consider:
241 * #1 tcp_usr_detach is called at tcptw creation time by
242 * tcp_twstart, then do not discard the newly created tcptw
243 * and leave inpcb present until timewait ends
244 * #2 tcp_usr_detach is called at tcptw creation time by
245 * tcp_twstart, but connection is local and tw will be
246 * discarded immediately
247 * #3 tcp_usr_detach is called at timewait end (or reuse) by
248 * tcp_twclose, then the tcptw has already been discarded
249 * (or reused) and inpcb is freed here
250 * #4 tcp_usr_detach is called() after timewait ends (or reuse)
251 * (e.g. by soclose), then tcptw has already been discarded
252 * (or reused) and inpcb is freed here
254 * In all three cases the tcptw should not be freed here.
256 if (inp->inp_flags & INP_DROPPED) {
258 if (__predict_true(tp == NULL)) {
262 * This case should not happen as in TIMEWAIT
263 * state the inp should not be destroyed before
264 * its tcptw. If INVARIANTS is defined, panic.
267 panic("%s: Panic before an inp double-free: "
268 "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
271 log(LOG_ERR, "%s: Avoid an inp double-free: "
272 "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
283 * If the connection is not in timewait, we consider two
284 * two conditions: one in which no further processing is
285 * necessary (dropped || embryonic), and one in which TCP is
286 * not yet done, but no longer requires the socket, so the
287 * pcb will persist for the time being.
289 * XXXRW: Does the second case still occur?
291 if (inp->inp_flags & INP_DROPPED ||
292 tp->t_state < TCPS_SYN_SENT) {
305 * Give the socket an address.
308 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
312 struct tcpcb *tp = NULL;
313 struct sockaddr_in *sinp;
315 sinp = (struct sockaddr_in *)nam;
316 if (nam->sa_len != sizeof (*sinp))
319 * Must check for multicast addresses and disallow binding
322 if (sinp->sin_family == AF_INET &&
323 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
324 return (EAFNOSUPPORT);
328 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
330 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
336 INP_HASH_WLOCK(&V_tcbinfo);
337 error = in_pcbbind(inp, nam, td->td_ucred);
338 INP_HASH_WUNLOCK(&V_tcbinfo);
341 TCP_PROBE2(debug__user, tp, PRU_BIND);
350 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
354 struct tcpcb *tp = NULL;
355 struct sockaddr_in6 *sin6;
358 sin6 = (struct sockaddr_in6 *)nam;
359 if (nam->sa_len != sizeof (*sin6))
362 * Must check for multicast addresses and disallow binding
365 if (sin6->sin6_family == AF_INET6 &&
366 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
367 return (EAFNOSUPPORT);
371 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
373 vflagsav = inp->inp_vflag;
374 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
380 INP_HASH_WLOCK(&V_tcbinfo);
381 inp->inp_vflag &= ~INP_IPV4;
382 inp->inp_vflag |= INP_IPV6;
384 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
385 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
386 inp->inp_vflag |= INP_IPV4;
387 else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
388 struct sockaddr_in sin;
390 in6_sin6_2_sin(&sin, sin6);
391 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
392 error = EAFNOSUPPORT;
393 INP_HASH_WUNLOCK(&V_tcbinfo);
396 inp->inp_vflag |= INP_IPV4;
397 inp->inp_vflag &= ~INP_IPV6;
398 error = in_pcbbind(inp, (struct sockaddr *)&sin,
400 INP_HASH_WUNLOCK(&V_tcbinfo);
405 error = in6_pcbbind(inp, nam, td->td_ucred);
406 INP_HASH_WUNLOCK(&V_tcbinfo);
409 inp->inp_vflag = vflagsav;
411 TCP_PROBE2(debug__user, tp, PRU_BIND);
419 * Prepare to accept connections.
422 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
426 struct tcpcb *tp = NULL;
430 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
432 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
439 error = solisten_proto_check(so);
440 INP_HASH_WLOCK(&V_tcbinfo);
441 if (error == 0 && inp->inp_lport == 0)
442 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
443 INP_HASH_WUNLOCK(&V_tcbinfo);
445 tcp_state_change(tp, TCPS_LISTEN);
446 solisten_proto(so, backlog);
448 if ((so->so_options & SO_NO_OFFLOAD) == 0)
449 tcp_offload_listen_start(tp);
454 if (IS_FASTOPEN(tp->t_flags))
455 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
458 TCPDEBUG2(PRU_LISTEN);
459 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
467 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
471 struct tcpcb *tp = NULL;
476 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
478 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
482 vflagsav = inp->inp_vflag;
486 error = solisten_proto_check(so);
487 INP_HASH_WLOCK(&V_tcbinfo);
488 if (error == 0 && inp->inp_lport == 0) {
489 inp->inp_vflag &= ~INP_IPV4;
490 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
491 inp->inp_vflag |= INP_IPV4;
492 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
494 INP_HASH_WUNLOCK(&V_tcbinfo);
496 tcp_state_change(tp, TCPS_LISTEN);
497 solisten_proto(so, backlog);
499 if ((so->so_options & SO_NO_OFFLOAD) == 0)
500 tcp_offload_listen_start(tp);
505 if (IS_FASTOPEN(tp->t_flags))
506 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
509 inp->inp_vflag = vflagsav;
512 TCPDEBUG2(PRU_LISTEN);
513 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
521 * Initiate connection to peer.
522 * Create a template for use in transmissions on this connection.
523 * Enter SYN_SENT state, and mark socket as connecting.
524 * Start keep-alive timer, and seed output sequence space.
525 * Send initial segment on connection.
528 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
530 struct epoch_tracker et;
533 struct tcpcb *tp = NULL;
534 struct sockaddr_in *sinp;
536 sinp = (struct sockaddr_in *)nam;
537 if (nam->sa_len != sizeof (*sinp))
540 * Must disallow TCP ``connections'' to multicast addresses.
542 if (sinp->sin_family == AF_INET
543 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
544 return (EAFNOSUPPORT);
545 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
550 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
552 if (inp->inp_flags & INP_TIMEWAIT) {
556 if (inp->inp_flags & INP_DROPPED) {
557 error = ECONNREFUSED;
563 if ((error = tcp_connect(tp, nam, td)) != 0)
566 if (registered_toedevs > 0 &&
567 (so->so_options & SO_NO_OFFLOAD) == 0 &&
568 (error = tcp_offload_connect(so, nam)) == 0)
571 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
572 error = tp->t_fb->tfb_tcp_output(tp);
576 TCPDEBUG2(PRU_CONNECT);
577 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
585 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
587 struct epoch_tracker et;
590 struct tcpcb *tp = NULL;
591 struct sockaddr_in6 *sin6;
597 sin6 = (struct sockaddr_in6 *)nam;
598 if (nam->sa_len != sizeof (*sin6))
601 * Must disallow TCP ``connections'' to multicast addresses.
603 if (sin6->sin6_family == AF_INET6
604 && IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
605 return (EAFNOSUPPORT);
608 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
610 vflagsav = inp->inp_vflag;
611 incflagsav = inp->inp_inc.inc_flags;
612 if (inp->inp_flags & INP_TIMEWAIT) {
616 if (inp->inp_flags & INP_DROPPED) {
617 error = ECONNREFUSED;
624 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
625 * therefore probably require the hash lock, which isn't held here.
626 * Is this a significant problem?
628 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
629 struct sockaddr_in sin;
631 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
635 if ((inp->inp_vflag & INP_IPV4) == 0) {
636 error = EAFNOSUPPORT;
640 in6_sin6_2_sin(&sin, sin6);
641 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
642 error = EAFNOSUPPORT;
645 if ((error = prison_remote_ip4(td->td_ucred,
646 &sin.sin_addr)) != 0)
648 inp->inp_vflag |= INP_IPV4;
649 inp->inp_vflag &= ~INP_IPV6;
651 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
654 if (registered_toedevs > 0 &&
655 (so->so_options & SO_NO_OFFLOAD) == 0 &&
656 (error = tcp_offload_connect(so, nam)) == 0)
659 error = tp->t_fb->tfb_tcp_output(tp);
662 if ((inp->inp_vflag & INP_IPV6) == 0) {
663 error = EAFNOSUPPORT;
668 if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
670 inp->inp_vflag &= ~INP_IPV4;
671 inp->inp_vflag |= INP_IPV6;
672 inp->inp_inc.inc_flags |= INC_ISIPV6;
673 if ((error = tcp6_connect(tp, nam, td)) != 0)
676 if (registered_toedevs > 0 &&
677 (so->so_options & SO_NO_OFFLOAD) == 0 &&
678 (error = tcp_offload_connect(so, nam)) == 0)
681 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
683 error = tp->t_fb->tfb_tcp_output(tp);
690 * If the implicit bind in the connect call fails, restore
691 * the flags we modified.
693 if (error != 0 && inp->inp_lport == 0) {
694 inp->inp_vflag = vflagsav;
695 inp->inp_inc.inc_flags = incflagsav;
698 TCPDEBUG2(PRU_CONNECT);
699 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
706 * Initiate disconnect from peer.
707 * If connection never passed embryonic stage, just drop;
708 * else if don't need to let data drain, then can just drop anyways,
709 * else have to begin TCP shutdown process: mark socket disconnecting,
710 * drain unread data, state switch to reflect user close, and
711 * send segment (e.g. FIN) to peer. Socket will be really disconnected
712 * when peer sends FIN and acks ours.
714 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
717 tcp_usr_disconnect(struct socket *so)
720 struct tcpcb *tp = NULL;
721 struct epoch_tracker et;
727 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
729 if (inp->inp_flags & INP_TIMEWAIT)
731 if (inp->inp_flags & INP_DROPPED) {
739 TCPDEBUG2(PRU_DISCONNECT);
740 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
748 * Accept a connection. Essentially all the work is done at higher levels;
749 * just return the address of the peer, storing through addr.
752 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
755 struct inpcb *inp = NULL;
756 struct tcpcb *tp = NULL;
761 if (so->so_state & SS_ISDISCONNECTED)
762 return (ECONNABORTED);
765 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
767 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
768 error = ECONNABORTED;
775 * We inline in_getpeeraddr and COMMON_END here, so that we can
776 * copy the data of interest and defer the malloc until after we
779 port = inp->inp_fport;
780 addr = inp->inp_faddr;
783 TCPDEBUG2(PRU_ACCEPT);
784 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
787 *nam = in_sockaddr(port, &addr);
794 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
796 struct inpcb *inp = NULL;
798 struct tcpcb *tp = NULL;
800 struct in6_addr addr6;
801 struct epoch_tracker et;
806 if (so->so_state & SS_ISDISCONNECTED)
807 return (ECONNABORTED);
810 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
813 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
814 error = ECONNABORTED;
821 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
822 * copy the data of interest and defer the malloc until after we
825 if (inp->inp_vflag & INP_IPV4) {
827 port = inp->inp_fport;
828 addr = inp->inp_faddr;
830 port = inp->inp_fport;
831 addr6 = inp->in6p_faddr;
835 TCPDEBUG2(PRU_ACCEPT);
836 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
841 *nam = in6_v4mapsin6_sockaddr(port, &addr);
843 *nam = in6_sockaddr(port, &addr6);
850 * Mark the connection as being incapable of further output.
853 tcp_usr_shutdown(struct socket *so)
857 struct tcpcb *tp = NULL;
858 struct epoch_tracker et;
863 KASSERT(inp != NULL, ("inp == NULL"));
865 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
873 if (!(inp->inp_flags & INP_DROPPED))
874 error = tp->t_fb->tfb_tcp_output(tp);
877 TCPDEBUG2(PRU_SHUTDOWN);
878 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
886 * After a receive, possibly send window update to peer.
889 tcp_usr_rcvd(struct socket *so, int flags)
891 struct epoch_tracker et;
893 struct tcpcb *tp = NULL;
898 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
900 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
907 * For passively-created TFO connections, don't attempt a window
908 * update while still in SYN_RECEIVED as this may trigger an early
909 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
910 * application response data, or failing that, when the DELACK timer
913 if (IS_FASTOPEN(tp->t_flags) &&
914 (tp->t_state == TCPS_SYN_RECEIVED))
918 if (tp->t_flags & TF_TOE)
919 tcp_offload_rcvd(tp);
922 tp->t_fb->tfb_tcp_output(tp);
926 TCP_PROBE2(debug__user, tp, PRU_RCVD);
932 * Do a send by putting data in output queue and updating urgent
933 * marker if URG set. Possibly send more data. Unlike the other
934 * pru_*() routines, the mbuf chains are our responsibility. We
935 * must either enqueue them or free them. The other pru_* routines
936 * generally are caller-frees.
939 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
940 struct sockaddr *nam, struct mbuf *control, struct thread *td)
942 struct epoch_tracker et;
945 struct tcpcb *tp = NULL;
948 struct sockaddr_in sin;
950 struct sockaddr_in *sinp;
961 * We require the pcbinfo "read lock" if we will close the socket
962 * as part of this call.
966 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
968 vflagsav = inp->inp_vflag;
969 incflagsav = inp->inp_inc.inc_flags;
970 restoreflags = false;
971 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
975 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
976 * for freeing memory.
978 if (m && (flags & PRUS_NOTREADY) == 0)
984 if (flags & PRUS_OOB) {
985 if ((error = tcp_pru_options_support(tp, PRUS_OOB)) != 0) {
988 if (m && (flags & PRUS_NOTREADY) == 0)
994 if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
995 switch (nam->sa_family) {
998 sinp = (struct sockaddr_in *)nam;
999 if (sinp->sin_len != sizeof(struct sockaddr_in)) {
1005 if ((inp->inp_vflag & INP_IPV6) != 0) {
1008 error = EAFNOSUPPORT;
1011 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
1014 error = EAFNOSUPPORT;
1017 if ((error = prison_remote_ip4(td->td_ucred,
1018 &sinp->sin_addr))) {
1031 struct sockaddr_in6 *sin6;
1033 sin6 = (struct sockaddr_in6 *)nam;
1034 if (sin6->sin6_len != sizeof(*sin6)) {
1040 if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
1043 error = EAFNOSUPPORT;
1046 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1049 error = EAFNOSUPPORT;
1052 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1054 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1060 if ((inp->inp_vflag & INP_IPV4) == 0) {
1061 error = EAFNOSUPPORT;
1066 restoreflags = true;
1067 inp->inp_vflag &= ~INP_IPV6;
1069 in6_sin6_2_sin(sinp, sin6);
1071 ntohl(sinp->sin_addr.s_addr))) {
1072 error = EAFNOSUPPORT;
1077 if ((error = prison_remote_ip4(td->td_ucred,
1078 &sinp->sin_addr))) {
1085 error = EAFNOSUPPORT;
1091 if ((inp->inp_vflag & INP_IPV6) == 0) {
1094 error = EAFNOSUPPORT;
1097 restoreflags = true;
1098 inp->inp_vflag &= ~INP_IPV4;
1099 inp->inp_inc.inc_flags |= INC_ISIPV6;
1100 if ((error = prison_remote_ip6(td->td_ucred,
1101 &sin6->sin6_addr))) {
1114 error = EAFNOSUPPORT;
1119 /* TCP doesn't do control messages (rights, creds, etc) */
1120 if (control->m_len) {
1127 m_freem(control); /* empty control, just free it */
1129 if (!(flags & PRUS_OOB)) {
1130 sbappendstream(&so->so_snd, m, flags);
1131 if (nam && tp->t_state < TCPS_SYN_SENT) {
1133 * Do implied connect if not yet connected,
1134 * initialize window to default value, and
1135 * initialize maxseg using peer's cached MSS.
1139 error = tcp6_connect(tp, nam, td);
1141 #if defined(INET6) && defined(INET)
1145 error = tcp_connect(tp,
1146 (struct sockaddr *)sinp, td);
1149 * The bind operation in tcp_connect succeeded. We
1150 * no longer want to restore the flags if later
1153 if (error == 0 || inp->inp_lport != 0)
1154 restoreflags = false;
1158 if (IS_FASTOPEN(tp->t_flags))
1159 tcp_fastopen_connect(tp);
1161 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1165 if (flags & PRUS_EOF) {
1167 * Close the send side of the connection after
1173 if (!(inp->inp_flags & INP_DROPPED) &&
1174 !(flags & PRUS_NOTREADY)) {
1175 if (flags & PRUS_MORETOCOME)
1176 tp->t_flags |= TF_MORETOCOME;
1177 error = tp->t_fb->tfb_tcp_output(tp);
1178 if (flags & PRUS_MORETOCOME)
1179 tp->t_flags &= ~TF_MORETOCOME;
1183 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1185 SOCKBUF_LOCK(&so->so_snd);
1186 if (sbspace(&so->so_snd) < -512) {
1187 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);
1202 if (nam && tp->t_state < TCPS_SYN_SENT) {
1204 * Do implied connect if not yet connected,
1205 * initialize window to default value, and
1206 * initialize maxseg using peer's cached MSS.
1210 * Not going to contemplate SYN|URG
1212 if (IS_FASTOPEN(tp->t_flags))
1213 tp->t_flags &= ~TF_FASTOPEN;
1216 error = tcp6_connect(tp, nam, td);
1218 #if defined(INET6) && defined(INET)
1222 error = tcp_connect(tp,
1223 (struct sockaddr *)sinp, td);
1226 * The bind operation in tcp_connect succeeded. We
1227 * no longer want to restore the flags if later
1230 if (error == 0 || inp->inp_lport != 0)
1231 restoreflags = false;
1235 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1238 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1239 if (!(flags & PRUS_NOTREADY)) {
1240 tp->t_flags |= TF_FORCEDATA;
1241 error = tp->t_fb->tfb_tcp_output(tp);
1242 tp->t_flags &= ~TF_FORCEDATA;
1245 TCP_LOG_EVENT(tp, NULL,
1246 &inp->inp_socket->so_rcv,
1247 &inp->inp_socket->so_snd,
1248 TCP_LOG_USERSEND, error,
1252 * If the request was unsuccessful and we changed flags,
1253 * restore the original flags.
1255 if (error != 0 && restoreflags) {
1256 inp->inp_vflag = vflagsav;
1257 inp->inp_inc.inc_flags = incflagsav;
1259 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
1260 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1261 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1262 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1269 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1271 struct epoch_tracker et;
1276 inp = sotoinpcb(so);
1278 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1280 mb_free_notready(m, count);
1281 return (ECONNRESET);
1283 tp = intotcpcb(inp);
1285 SOCKBUF_LOCK(&so->so_snd);
1286 error = sbready(&so->so_snd, m, count);
1287 SOCKBUF_UNLOCK(&so->so_snd);
1289 NET_EPOCH_ENTER(et);
1290 error = tp->t_fb->tfb_tcp_output(tp);
1299 * Abort the TCP. Drop the connection abruptly.
1302 tcp_usr_abort(struct socket *so)
1305 struct tcpcb *tp = NULL;
1306 struct epoch_tracker et;
1309 inp = sotoinpcb(so);
1310 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1312 NET_EPOCH_ENTER(et);
1314 KASSERT(inp->inp_socket != NULL,
1315 ("tcp_usr_abort: inp_socket == NULL"));
1318 * If we still have full TCP state, and we're not dropped, drop.
1320 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1321 !(inp->inp_flags & INP_DROPPED)) {
1322 tp = intotcpcb(inp);
1324 tp = tcp_drop(tp, ECONNABORTED);
1327 TCPDEBUG2(PRU_ABORT);
1328 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1330 if (!(inp->inp_flags & INP_DROPPED)) {
1332 so->so_state |= SS_PROTOREF;
1334 inp->inp_flags |= INP_SOCKREF;
1342 * TCP socket is closed. Start friendly disconnect.
1345 tcp_usr_close(struct socket *so)
1348 struct tcpcb *tp = NULL;
1349 struct epoch_tracker et;
1352 inp = sotoinpcb(so);
1353 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1355 NET_EPOCH_ENTER(et);
1357 KASSERT(inp->inp_socket != NULL,
1358 ("tcp_usr_close: inp_socket == NULL"));
1361 * If we still have full TCP state, and we're not dropped, initiate
1364 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1365 !(inp->inp_flags & INP_DROPPED)) {
1366 tp = intotcpcb(inp);
1369 TCPDEBUG2(PRU_CLOSE);
1370 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1372 if (!(inp->inp_flags & INP_DROPPED)) {
1374 so->so_state |= SS_PROTOREF;
1376 inp->inp_flags |= INP_SOCKREF;
1383 tcp_pru_options_support(struct tcpcb *tp, int flags)
1386 * If the specific TCP stack has a pru_options
1387 * specified then it does not always support
1388 * all the PRU_XX options and we must ask it.
1389 * If the function is not specified then all
1390 * of the PRU_XX options are supported.
1394 if (tp->t_fb->tfb_pru_options) {
1395 ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1401 * Receive out-of-band data.
1404 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1408 struct tcpcb *tp = NULL;
1411 inp = sotoinpcb(so);
1412 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1414 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1418 tp = intotcpcb(inp);
1419 error = tcp_pru_options_support(tp, PRUS_OOB);
1424 if ((so->so_oobmark == 0 &&
1425 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1426 so->so_options & SO_OOBINLINE ||
1427 tp->t_oobflags & TCPOOB_HADDATA) {
1431 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1432 error = EWOULDBLOCK;
1436 *mtod(m, caddr_t) = tp->t_iobc;
1437 if ((flags & MSG_PEEK) == 0)
1438 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1441 TCPDEBUG2(PRU_RCVOOB);
1442 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1448 struct pr_usrreqs tcp_usrreqs = {
1449 .pru_abort = tcp_usr_abort,
1450 .pru_accept = tcp_usr_accept,
1451 .pru_attach = tcp_usr_attach,
1452 .pru_bind = tcp_usr_bind,
1453 .pru_connect = tcp_usr_connect,
1454 .pru_control = in_control,
1455 .pru_detach = tcp_usr_detach,
1456 .pru_disconnect = tcp_usr_disconnect,
1457 .pru_listen = tcp_usr_listen,
1458 .pru_peeraddr = in_getpeeraddr,
1459 .pru_rcvd = tcp_usr_rcvd,
1460 .pru_rcvoob = tcp_usr_rcvoob,
1461 .pru_send = tcp_usr_send,
1462 .pru_ready = tcp_usr_ready,
1463 .pru_shutdown = tcp_usr_shutdown,
1464 .pru_sockaddr = in_getsockaddr,
1465 .pru_sosetlabel = in_pcbsosetlabel,
1466 .pru_close = tcp_usr_close,
1471 struct pr_usrreqs tcp6_usrreqs = {
1472 .pru_abort = tcp_usr_abort,
1473 .pru_accept = tcp6_usr_accept,
1474 .pru_attach = tcp_usr_attach,
1475 .pru_bind = tcp6_usr_bind,
1476 .pru_connect = tcp6_usr_connect,
1477 .pru_control = in6_control,
1478 .pru_detach = tcp_usr_detach,
1479 .pru_disconnect = tcp_usr_disconnect,
1480 .pru_listen = tcp6_usr_listen,
1481 .pru_peeraddr = in6_mapped_peeraddr,
1482 .pru_rcvd = tcp_usr_rcvd,
1483 .pru_rcvoob = tcp_usr_rcvoob,
1484 .pru_send = tcp_usr_send,
1485 .pru_ready = tcp_usr_ready,
1486 .pru_shutdown = tcp_usr_shutdown,
1487 .pru_sockaddr = in6_mapped_sockaddr,
1488 .pru_sosetlabel = in_pcbsosetlabel,
1489 .pru_close = tcp_usr_close,
1495 * Common subroutine to open a TCP connection to remote host specified
1496 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1497 * port number if needed. Call in_pcbconnect_setup to do the routing and
1498 * to choose a local host address (interface). If there is an existing
1499 * incarnation of the same connection in TIME-WAIT state and if the remote
1500 * host was sending CC options and if the connection duration was < MSL, then
1501 * truncate the previous TIME-WAIT state and proceed.
1502 * Initialize connection parameters and enter SYN-SENT state.
1505 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1507 struct inpcb *inp = tp->t_inpcb, *oinp;
1508 struct socket *so = inp->inp_socket;
1509 struct in_addr laddr;
1514 INP_WLOCK_ASSERT(inp);
1515 INP_HASH_WLOCK(&V_tcbinfo);
1517 if (inp->inp_lport == 0) {
1518 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1524 * Cannot simply call in_pcbconnect, because there might be an
1525 * earlier incarnation of this same connection still in
1526 * TIME_WAIT state, creating an ADDRINUSE error.
1528 laddr = inp->inp_laddr;
1529 lport = inp->inp_lport;
1530 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1531 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1532 if (error && oinp == NULL)
1538 inp->inp_laddr = laddr;
1540 INP_HASH_WUNLOCK(&V_tcbinfo);
1543 * Compute window scaling to request:
1544 * Scale to fit into sweet spot. See tcp_syncache.c.
1545 * XXX: This should move to tcp_output().
1547 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1548 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1549 tp->request_r_scale++;
1552 TCPSTAT_INC(tcps_connattempt);
1553 tcp_state_change(tp, TCPS_SYN_SENT);
1554 tp->iss = tcp_new_isn(&inp->inp_inc);
1555 if (tp->t_flags & TF_REQ_TSTMP)
1556 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1557 tcp_sendseqinit(tp);
1562 INP_HASH_WUNLOCK(&V_tcbinfo);
1569 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1571 struct inpcb *inp = tp->t_inpcb;
1574 INP_WLOCK_ASSERT(inp);
1575 INP_HASH_WLOCK(&V_tcbinfo);
1577 if (inp->inp_lport == 0) {
1578 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1582 error = in6_pcbconnect(inp, nam, td->td_ucred);
1585 INP_HASH_WUNLOCK(&V_tcbinfo);
1587 /* Compute window scaling to request. */
1588 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1589 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1590 tp->request_r_scale++;
1592 soisconnecting(inp->inp_socket);
1593 TCPSTAT_INC(tcps_connattempt);
1594 tcp_state_change(tp, TCPS_SYN_SENT);
1595 tp->iss = tcp_new_isn(&inp->inp_inc);
1596 if (tp->t_flags & TF_REQ_TSTMP)
1597 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1598 tcp_sendseqinit(tp);
1603 INP_HASH_WUNLOCK(&V_tcbinfo);
1609 * Export TCP internal state information via a struct tcp_info, based on the
1610 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1611 * (TCP state machine, etc). We export all information using FreeBSD-native
1612 * constants -- for example, the numeric values for tcpi_state will differ
1616 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1619 INP_WLOCK_ASSERT(tp->t_inpcb);
1620 bzero(ti, sizeof(*ti));
1622 ti->tcpi_state = tp->t_state;
1623 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1624 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1625 if (tp->t_flags & TF_SACK_PERMIT)
1626 ti->tcpi_options |= TCPI_OPT_SACK;
1627 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1628 ti->tcpi_options |= TCPI_OPT_WSCALE;
1629 ti->tcpi_snd_wscale = tp->snd_scale;
1630 ti->tcpi_rcv_wscale = tp->rcv_scale;
1632 if (tp->t_flags2 & TF2_ECN_PERMIT)
1633 ti->tcpi_options |= TCPI_OPT_ECN;
1635 ti->tcpi_rto = tp->t_rxtcur * tick;
1636 ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1637 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1638 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1640 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1641 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1644 * FreeBSD-specific extension fields for tcp_info.
1646 ti->tcpi_rcv_space = tp->rcv_wnd;
1647 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1648 ti->tcpi_snd_wnd = tp->snd_wnd;
1649 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1650 ti->tcpi_snd_nxt = tp->snd_nxt;
1651 ti->tcpi_snd_mss = tp->t_maxseg;
1652 ti->tcpi_rcv_mss = tp->t_maxseg;
1653 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1654 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1655 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1657 if (tp->t_flags & TF_TOE) {
1658 ti->tcpi_options |= TCPI_OPT_TOE;
1659 tcp_offload_tcp_info(tp, ti);
1665 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1666 * socket option arguments. When it re-acquires the lock after the copy, it
1667 * has to revalidate that the connection is still valid for the socket
1670 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \
1672 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1675 return (ECONNRESET); \
1677 tp = intotcpcb(inp); \
1679 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1682 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1687 struct tcp_function_block *blk;
1688 struct tcp_function_set fsn;
1691 inp = sotoinpcb(so);
1692 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1693 if (sopt->sopt_level != IPPROTO_TCP) {
1695 if (inp->inp_vflag & INP_IPV6PROTO) {
1696 error = ip6_ctloutput(so, sopt);
1698 * In case of the IPV6_USE_MIN_MTU socket option,
1699 * the INC_IPV6MINMTU flag to announce a corresponding
1700 * MSS during the initial handshake.
1701 * If the TCP connection is not in the front states,
1702 * just reduce the MSS being used.
1703 * This avoids the sending of TCP segments which will
1704 * be fragmented at the IPv6 layer.
1707 (sopt->sopt_dir == SOPT_SET) &&
1708 (sopt->sopt_level == IPPROTO_IPV6) &&
1709 (sopt->sopt_name == IPV6_USE_MIN_MTU)) {
1711 if ((inp->inp_flags &
1712 (INP_TIMEWAIT | INP_DROPPED))) {
1714 return (ECONNRESET);
1716 inp->inp_inc.inc_flags |= INC_IPV6MINMTU;
1717 tp = intotcpcb(inp);
1718 if ((tp->t_state >= TCPS_SYN_SENT) &&
1719 (inp->inp_inc.inc_flags & INC_ISIPV6)) {
1720 struct ip6_pktopts *opt;
1722 opt = inp->in6p_outputopts;
1723 if ((opt != NULL) &&
1724 (opt->ip6po_minmtu ==
1725 IP6PO_MINMTU_ALL)) {
1726 if (tp->t_maxseg > TCP6_MSS) {
1727 tp->t_maxseg = TCP6_MSS;
1735 #if defined(INET6) && defined(INET)
1740 error = ip_ctloutput(so, sopt);
1746 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1748 return (ECONNRESET);
1750 tp = intotcpcb(inp);
1752 * Protect the TCP option TCP_FUNCTION_BLK so
1753 * that a sub-function can *never* overwrite this.
1755 if ((sopt->sopt_dir == SOPT_SET) &&
1756 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1758 error = sooptcopyin(sopt, &fsn, sizeof fsn,
1762 INP_WLOCK_RECHECK(inp);
1763 blk = find_and_ref_tcp_functions(&fsn);
1768 if (tp->t_fb == blk) {
1769 /* You already have this */
1770 refcount_release(&blk->tfb_refcnt);
1774 if (tp->t_state != TCPS_CLOSED) {
1776 * The user has advanced the state
1777 * past the initial point, we may not
1778 * be able to switch.
1780 if (blk->tfb_tcp_handoff_ok != NULL) {
1782 * Does the stack provide a
1783 * query mechanism, if so it may
1784 * still be possible?
1786 error = (*blk->tfb_tcp_handoff_ok)(tp);
1790 refcount_release(&blk->tfb_refcnt);
1795 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1796 refcount_release(&blk->tfb_refcnt);
1801 * Release the old refcnt, the
1802 * lookup acquired a ref on the
1805 if (tp->t_fb->tfb_tcp_fb_fini) {
1807 * Tell the stack to cleanup with 0 i.e.
1808 * the tcb is not going away.
1810 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1813 /* Assure that we are not on any hpts */
1814 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
1816 if (blk->tfb_tcp_fb_init) {
1817 error = (*blk->tfb_tcp_fb_init)(tp);
1819 refcount_release(&blk->tfb_refcnt);
1820 if (tp->t_fb->tfb_tcp_fb_init) {
1821 if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) {
1822 /* Fall back failed, drop the connection */
1831 refcount_release(&tp->t_fb->tfb_refcnt);
1834 if (tp->t_flags & TF_TOE) {
1835 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1842 } else if ((sopt->sopt_dir == SOPT_GET) &&
1843 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1844 strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1845 TCP_FUNCTION_NAME_LEN_MAX);
1846 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1847 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1849 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1852 /* Pass in the INP locked, called must unlock it */
1853 return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
1857 * If this assert becomes untrue, we need to change the size of the buf
1858 * variable in tcp_default_ctloutput().
1861 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1862 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1867 copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1869 struct tls_enable_v0 tls_v0;
1872 if (sopt->sopt_valsize == sizeof(tls_v0)) {
1873 error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1877 memset(tls, 0, sizeof(*tls));
1878 tls->cipher_key = tls_v0.cipher_key;
1879 tls->iv = tls_v0.iv;
1880 tls->auth_key = tls_v0.auth_key;
1881 tls->cipher_algorithm = tls_v0.cipher_algorithm;
1882 tls->cipher_key_len = tls_v0.cipher_key_len;
1883 tls->iv_len = tls_v0.iv_len;
1884 tls->auth_algorithm = tls_v0.auth_algorithm;
1885 tls->auth_key_len = tls_v0.auth_key_len;
1886 tls->flags = tls_v0.flags;
1887 tls->tls_vmajor = tls_v0.tls_vmajor;
1888 tls->tls_vminor = tls_v0.tls_vminor;
1892 return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1897 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
1899 int error, opt, optval;
1903 struct tls_enable tls;
1905 struct cc_algo *algo;
1906 char *pbuf, buf[TCP_LOG_ID_LEN];
1908 struct statsblob *sbp;
1913 * For TCP_CCALGOOPT forward the control to CC module, for both
1914 * SOPT_SET and SOPT_GET.
1916 switch (sopt->sopt_name) {
1919 if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
1921 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1922 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1923 sopt->sopt_valsize);
1928 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1929 if (CC_ALGO(tp)->ctl_output != NULL)
1930 error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1934 if (error == 0 && sopt->sopt_dir == SOPT_GET)
1935 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1940 switch (sopt->sopt_dir) {
1942 switch (sopt->sopt_name) {
1943 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1945 if (!TCPMD5_ENABLED()) {
1947 return (ENOPROTOOPT);
1949 error = TCPMD5_PCBCTL(inp, sopt);
1952 goto unlock_and_done;
1958 error = sooptcopyin(sopt, &optval, sizeof optval,
1963 INP_WLOCK_RECHECK(inp);
1964 switch (sopt->sopt_name) {
1972 opt = 0; /* dead code to fool gcc */
1979 tp->t_flags &= ~opt;
1982 if (tp->t_flags & TF_TOE) {
1983 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1992 error = sooptcopyin(sopt, &optval, sizeof optval,
1997 INP_WLOCK_RECHECK(inp);
1999 tp->t_flags |= TF_NOPUSH;
2000 else if (tp->t_flags & TF_NOPUSH) {
2001 tp->t_flags &= ~TF_NOPUSH;
2002 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2003 struct epoch_tracker et;
2005 NET_EPOCH_ENTER(et);
2006 error = tp->t_fb->tfb_tcp_output(tp);
2010 goto unlock_and_done;
2014 error = sooptcopyin(sopt, &optval, sizeof optval,
2019 INP_WLOCK_RECHECK(inp);
2020 if (optval > 0 && optval <= tp->t_maxseg &&
2021 optval + 40 >= V_tcp_minmss)
2022 tp->t_maxseg = optval;
2025 goto unlock_and_done;
2035 error = sooptcopyin(sopt, &optval, sizeof optval,
2041 sbp = stats_blob_alloc(
2042 V_tcp_perconn_stats_dflt_tpl, 0);
2046 INP_WLOCK_RECHECK(inp);
2047 if ((tp->t_stats != NULL && sbp == NULL) ||
2048 (tp->t_stats == NULL && sbp != NULL)) {
2049 struct statsblob *t = tp->t_stats;
2055 stats_blob_destroy(sbp);
2057 return (EOPNOTSUPP);
2061 case TCP_CONGESTION:
2063 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
2066 buf[sopt->sopt_valsize] = '\0';
2067 INP_WLOCK_RECHECK(inp);
2069 STAILQ_FOREACH(algo, &cc_list, entries)
2070 if (strncmp(buf, algo->name,
2071 TCP_CA_NAME_MAX) == 0)
2080 * We hold a write lock over the tcb so it's safe to
2081 * do these things without ordering concerns.
2083 if (CC_ALGO(tp)->cb_destroy != NULL)
2084 CC_ALGO(tp)->cb_destroy(tp->ccv);
2088 * If something goes pear shaped initialising the new
2089 * algo, fall back to newreno (which does not
2090 * require initialisation).
2092 if (algo->cb_init != NULL &&
2093 algo->cb_init(tp->ccv) != 0) {
2094 CC_ALGO(tp) = &newreno_cc_algo;
2096 * The only reason init should fail is
2097 * because of malloc.
2105 case TCP_TXTLS_ENABLE:
2107 error = copyin_tls_enable(sopt, &tls);
2110 error = ktls_enable_tx(so, &tls);
2112 case TCP_TXTLS_MODE:
2114 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2118 INP_WLOCK_RECHECK(inp);
2119 error = ktls_set_tx_mode(so, ui);
2122 case TCP_RXTLS_ENABLE:
2124 error = sooptcopyin(sopt, &tls, sizeof(tls),
2128 error = ktls_enable_rx(so, &tls);
2136 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2140 if (ui > (UINT_MAX / hz)) {
2146 INP_WLOCK_RECHECK(inp);
2147 switch (sopt->sopt_name) {
2149 tp->t_keepidle = ui;
2151 * XXX: better check current remaining
2152 * timeout and "merge" it with new value.
2154 if ((tp->t_state > TCPS_LISTEN) &&
2155 (tp->t_state <= TCPS_CLOSING))
2156 tcp_timer_activate(tp, TT_KEEP,
2160 tp->t_keepintvl = ui;
2161 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2162 (TP_MAXIDLE(tp) > 0))
2163 tcp_timer_activate(tp, TT_2MSL,
2167 tp->t_keepinit = ui;
2168 if (tp->t_state == TCPS_SYN_RECEIVED ||
2169 tp->t_state == TCPS_SYN_SENT)
2170 tcp_timer_activate(tp, TT_KEEP,
2174 goto unlock_and_done;
2178 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2182 INP_WLOCK_RECHECK(inp);
2184 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2185 (TP_MAXIDLE(tp) > 0))
2186 tcp_timer_activate(tp, TT_2MSL,
2188 goto unlock_and_done;
2194 error = sooptcopyin(sopt, &optval, sizeof optval,
2199 INP_WLOCK_RECHECK(inp);
2201 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
2202 &(tp->t_outpkts) : &(tp->t_inpkts),
2206 goto unlock_and_done;
2209 case TCP_FASTOPEN: {
2210 struct tcp_fastopen tfo_optval;
2213 if (!V_tcp_fastopen_client_enable &&
2214 !V_tcp_fastopen_server_enable)
2217 error = sooptcopyin(sopt, &tfo_optval,
2218 sizeof(tfo_optval), sizeof(int));
2222 INP_WLOCK_RECHECK(inp);
2223 if (tfo_optval.enable) {
2224 if (tp->t_state == TCPS_LISTEN) {
2225 if (!V_tcp_fastopen_server_enable) {
2227 goto unlock_and_done;
2230 tp->t_flags |= TF_FASTOPEN;
2231 if (tp->t_tfo_pending == NULL)
2233 tcp_fastopen_alloc_counter();
2236 * If a pre-shared key was provided,
2237 * stash it in the client cookie
2238 * field of the tcpcb for use during
2241 if (sopt->sopt_valsize ==
2242 sizeof(tfo_optval)) {
2243 memcpy(tp->t_tfo_cookie.client,
2245 TCP_FASTOPEN_PSK_LEN);
2246 tp->t_tfo_client_cookie_len =
2247 TCP_FASTOPEN_PSK_LEN;
2249 tp->t_flags |= TF_FASTOPEN;
2252 tp->t_flags &= ~TF_FASTOPEN;
2253 goto unlock_and_done;
2259 error = sooptcopyin(sopt, &optval, sizeof optval,
2264 INP_WLOCK_RECHECK(inp);
2265 error = tcp_log_state_change(tp, optval);
2266 goto unlock_and_done;
2275 error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
2278 buf[sopt->sopt_valsize] = '\0';
2279 INP_WLOCK_RECHECK(inp);
2280 error = tcp_log_set_id(tp, buf);
2281 /* tcp_log_set_id() unlocks the INP. */
2288 sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
2291 buf[sopt->sopt_valsize] = '\0';
2292 INP_WLOCK_RECHECK(inp);
2293 if (sopt->sopt_name == TCP_LOGDUMP) {
2294 error = tcp_log_dump_tp_logbuf(tp, buf,
2298 tcp_log_dump_tp_bucket_logbufs(tp, buf);
2300 * tcp_log_dump_tp_bucket_logbufs() drops the
2309 error = ENOPROTOOPT;
2315 tp = intotcpcb(inp);
2316 switch (sopt->sopt_name) {
2317 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2319 if (!TCPMD5_ENABLED()) {
2321 return (ENOPROTOOPT);
2323 error = TCPMD5_PCBCTL(inp, sopt);
2328 optval = tp->t_flags & TF_NODELAY;
2330 error = sooptcopyout(sopt, &optval, sizeof optval);
2333 optval = tp->t_maxseg;
2335 error = sooptcopyout(sopt, &optval, sizeof optval);
2338 optval = tp->t_flags & TF_NOOPT;
2340 error = sooptcopyout(sopt, &optval, sizeof optval);
2343 optval = tp->t_flags & TF_NOPUSH;
2345 error = sooptcopyout(sopt, &optval, sizeof optval);
2348 tcp_fill_info(tp, &ti);
2350 error = sooptcopyout(sopt, &ti, sizeof ti);
2356 TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2359 socklen_t outsbsz = sopt->sopt_valsize;
2360 if (tp->t_stats == NULL)
2362 else if (outsbsz >= tp->t_stats->cursz)
2363 outsbsz = tp->t_stats->cursz;
2364 else if (outsbsz >= sizeof(struct statsblob))
2365 outsbsz = sizeof(struct statsblob);
2372 sbp = sopt->sopt_val;
2373 nheld = atop(round_page(((vm_offset_t)sbp) +
2374 (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2375 vm_page_t ma[nheld];
2376 if (vm_fault_quick_hold_pages(
2377 &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2378 outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2384 if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2385 SIZEOF_MEMBER(struct statsblob, flags))))
2388 INP_WLOCK_RECHECK(inp);
2389 error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2390 sbflags | SB_CLONE_USRDSTNOFAULT);
2392 sopt->sopt_valsize = outsbsz;
2394 vm_page_unhold_pages(ma, nheld);
2401 case TCP_CONGESTION:
2402 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2404 error = sooptcopyout(sopt, buf, len + 1);
2410 switch (sopt->sopt_name) {
2412 ui = TP_KEEPIDLE(tp) / hz;
2415 ui = TP_KEEPINTVL(tp) / hz;
2418 ui = TP_KEEPINIT(tp) / hz;
2421 ui = TP_KEEPCNT(tp);
2425 error = sooptcopyout(sopt, &ui, sizeof(ui));
2430 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
2431 &(tp->t_outpkts) : &(tp->t_inpkts));
2433 error = sooptcopyout(sopt, &optval, sizeof optval);
2437 optval = tp->t_flags & TF_FASTOPEN;
2439 error = sooptcopyout(sopt, &optval, sizeof optval);
2443 optval = tp->t_logstate;
2445 error = sooptcopyout(sopt, &optval, sizeof(optval));
2448 /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2449 error = tcp_log_getlogbuf(sopt, tp);
2452 len = tcp_log_get_id(tp, buf);
2454 error = sooptcopyout(sopt, buf, len + 1);
2463 case TCP_TXTLS_MODE:
2464 optval = ktls_get_tx_mode(so);
2466 error = sooptcopyout(sopt, &optval, sizeof(optval));
2468 case TCP_RXTLS_MODE:
2469 optval = ktls_get_rx_mode(so);
2471 error = sooptcopyout(sopt, &optval, sizeof(optval));
2476 error = ENOPROTOOPT;
2483 #undef INP_WLOCK_RECHECK
2484 #undef INP_WLOCK_RECHECK_CLEANUP
2487 * Initiate (or continue) disconnect.
2488 * If embryonic state, just send reset (once).
2489 * If in ``let data drain'' option and linger null, just drop.
2490 * Otherwise (hard), mark socket disconnecting and drop
2491 * current input data; switch states based on user close, and
2492 * send segment to peer (with FIN).
2495 tcp_disconnect(struct tcpcb *tp)
2497 struct inpcb *inp = tp->t_inpcb;
2498 struct socket *so = inp->inp_socket;
2501 INP_WLOCK_ASSERT(inp);
2504 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2505 * socket is still open.
2507 if (tp->t_state < TCPS_ESTABLISHED &&
2508 !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2511 ("tcp_disconnect: tcp_close() returned NULL"));
2512 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2513 tp = tcp_drop(tp, 0);
2515 ("tcp_disconnect: tcp_drop() returned NULL"));
2517 soisdisconnecting(so);
2518 sbflush(&so->so_rcv);
2520 if (!(inp->inp_flags & INP_DROPPED))
2521 tp->t_fb->tfb_tcp_output(tp);
2526 * User issued close, and wish to trail through shutdown states:
2527 * if never received SYN, just forget it. If got a SYN from peer,
2528 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2529 * If already got a FIN from peer, then almost done; go to LAST_ACK
2530 * state. In all other cases, have already sent FIN to peer (e.g.
2531 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2532 * for peer to send FIN or not respond to keep-alives, etc.
2533 * We can let the user exit from the close as soon as the FIN is acked.
2536 tcp_usrclosed(struct tcpcb *tp)
2540 INP_WLOCK_ASSERT(tp->t_inpcb);
2542 switch (tp->t_state) {
2545 tcp_offload_listen_stop(tp);
2547 tcp_state_change(tp, TCPS_CLOSED);
2552 * tcp_close() should never return NULL here as the socket is
2556 ("tcp_usrclosed: tcp_close() returned NULL"));
2560 case TCPS_SYN_RECEIVED:
2561 tp->t_flags |= TF_NEEDFIN;
2564 case TCPS_ESTABLISHED:
2565 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2568 case TCPS_CLOSE_WAIT:
2569 tcp_state_change(tp, TCPS_LAST_ACK);
2572 if (tp->t_state >= TCPS_FIN_WAIT_2) {
2573 soisdisconnected(tp->t_inpcb->inp_socket);
2574 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
2575 if (tp->t_state == TCPS_FIN_WAIT_2) {
2578 timeout = (tcp_fast_finwait2_recycle) ?
2579 tcp_finwait2_timeout : TP_MAXIDLE(tp);
2580 tcp_timer_activate(tp, TT_2MSL, timeout);
2587 db_print_indent(int indent)
2591 for (i = 0; i < indent; i++)
2596 db_print_tstate(int t_state)
2601 db_printf("TCPS_CLOSED");
2605 db_printf("TCPS_LISTEN");
2609 db_printf("TCPS_SYN_SENT");
2612 case TCPS_SYN_RECEIVED:
2613 db_printf("TCPS_SYN_RECEIVED");
2616 case TCPS_ESTABLISHED:
2617 db_printf("TCPS_ESTABLISHED");
2620 case TCPS_CLOSE_WAIT:
2621 db_printf("TCPS_CLOSE_WAIT");
2624 case TCPS_FIN_WAIT_1:
2625 db_printf("TCPS_FIN_WAIT_1");
2629 db_printf("TCPS_CLOSING");
2633 db_printf("TCPS_LAST_ACK");
2636 case TCPS_FIN_WAIT_2:
2637 db_printf("TCPS_FIN_WAIT_2");
2640 case TCPS_TIME_WAIT:
2641 db_printf("TCPS_TIME_WAIT");
2645 db_printf("unknown");
2651 db_print_tflags(u_int t_flags)
2656 if (t_flags & TF_ACKNOW) {
2657 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2660 if (t_flags & TF_DELACK) {
2661 db_printf("%sTF_DELACK", comma ? ", " : "");
2664 if (t_flags & TF_NODELAY) {
2665 db_printf("%sTF_NODELAY", comma ? ", " : "");
2668 if (t_flags & TF_NOOPT) {
2669 db_printf("%sTF_NOOPT", comma ? ", " : "");
2672 if (t_flags & TF_SENTFIN) {
2673 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2676 if (t_flags & TF_REQ_SCALE) {
2677 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2680 if (t_flags & TF_RCVD_SCALE) {
2681 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2684 if (t_flags & TF_REQ_TSTMP) {
2685 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2688 if (t_flags & TF_RCVD_TSTMP) {
2689 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2692 if (t_flags & TF_SACK_PERMIT) {
2693 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2696 if (t_flags & TF_NEEDSYN) {
2697 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2700 if (t_flags & TF_NEEDFIN) {
2701 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2704 if (t_flags & TF_NOPUSH) {
2705 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2708 if (t_flags & TF_MORETOCOME) {
2709 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2712 if (t_flags & TF_LQ_OVERFLOW) {
2713 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2716 if (t_flags & TF_LASTIDLE) {
2717 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2720 if (t_flags & TF_RXWIN0SENT) {
2721 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2724 if (t_flags & TF_FASTRECOVERY) {
2725 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2728 if (t_flags & TF_CONGRECOVERY) {
2729 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2732 if (t_flags & TF_WASFRECOVERY) {
2733 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2736 if (t_flags & TF_SIGNATURE) {
2737 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2740 if (t_flags & TF_FORCEDATA) {
2741 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2744 if (t_flags & TF_TSO) {
2745 db_printf("%sTF_TSO", comma ? ", " : "");
2748 if (t_flags & TF_FASTOPEN) {
2749 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2755 db_print_tflags2(u_int t_flags2)
2760 if (t_flags2 & TF2_ECN_PERMIT) {
2761 db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
2768 db_print_toobflags(char t_oobflags)
2773 if (t_oobflags & TCPOOB_HAVEDATA) {
2774 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2777 if (t_oobflags & TCPOOB_HADDATA) {
2778 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2784 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2787 db_print_indent(indent);
2788 db_printf("%s at %p\n", name, tp);
2792 db_print_indent(indent);
2793 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2794 TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2796 db_print_indent(indent);
2797 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
2798 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2800 db_print_indent(indent);
2801 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2802 &tp->t_timers->tt_delack, tp->t_inpcb);
2804 db_print_indent(indent);
2805 db_printf("t_state: %d (", tp->t_state);
2806 db_print_tstate(tp->t_state);
2809 db_print_indent(indent);
2810 db_printf("t_flags: 0x%x (", tp->t_flags);
2811 db_print_tflags(tp->t_flags);
2814 db_print_indent(indent);
2815 db_printf("t_flags2: 0x%x (", tp->t_flags2);
2816 db_print_tflags2(tp->t_flags2);
2819 db_print_indent(indent);
2820 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2821 tp->snd_una, tp->snd_max, tp->snd_nxt);
2823 db_print_indent(indent);
2824 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2825 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2827 db_print_indent(indent);
2828 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2829 tp->iss, tp->irs, tp->rcv_nxt);
2831 db_print_indent(indent);
2832 db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n",
2833 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2835 db_print_indent(indent);
2836 db_printf("snd_wnd: %u snd_cwnd: %u\n",
2837 tp->snd_wnd, tp->snd_cwnd);
2839 db_print_indent(indent);
2840 db_printf("snd_ssthresh: %u snd_recover: "
2841 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2843 db_print_indent(indent);
2844 db_printf("t_rcvtime: %u t_startime: %u\n",
2845 tp->t_rcvtime, tp->t_starttime);
2847 db_print_indent(indent);
2848 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2849 tp->t_rtttime, tp->t_rtseq);
2851 db_print_indent(indent);
2852 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2853 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2855 db_print_indent(indent);
2856 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2857 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2860 db_print_indent(indent);
2861 db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n",
2862 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2864 db_print_indent(indent);
2865 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2866 db_print_toobflags(tp->t_oobflags);
2867 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2869 db_print_indent(indent);
2870 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2871 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2873 db_print_indent(indent);
2874 db_printf("ts_recent: %u ts_recent_age: %u\n",
2875 tp->ts_recent, tp->ts_recent_age);
2877 db_print_indent(indent);
2878 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2879 "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2881 db_print_indent(indent);
2882 db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x "
2883 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2884 tp->snd_recover_prev, tp->t_badrxtwin);
2886 db_print_indent(indent);
2887 db_printf("snd_numholes: %d snd_holes first: %p\n",
2888 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2890 db_print_indent(indent);
2891 db_printf("snd_fack: 0x%08x rcv_numsacks: %d\n",
2892 tp->snd_fack, tp->rcv_numsacks);
2894 /* Skip sackblks, sackhint. */
2896 db_print_indent(indent);
2897 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2898 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2901 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2906 db_printf("usage: show tcpcb <addr>\n");
2909 tp = (struct tcpcb *)addr;
2911 db_print_tcpcb(tp, "tcpcb", 0);