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 (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1043 error = EAFNOSUPPORT;
1046 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1048 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1054 if ((inp->inp_vflag & INP_IPV4) == 0) {
1055 error = EAFNOSUPPORT;
1060 restoreflags = true;
1061 inp->inp_vflag &= ~INP_IPV6;
1063 in6_sin6_2_sin(sinp, sin6);
1065 ntohl(sinp->sin_addr.s_addr))) {
1066 error = EAFNOSUPPORT;
1071 if ((error = prison_remote_ip4(td->td_ucred,
1072 &sinp->sin_addr))) {
1079 error = EAFNOSUPPORT;
1085 if ((inp->inp_vflag & INP_IPV6) == 0) {
1088 error = EAFNOSUPPORT;
1091 restoreflags = true;
1092 inp->inp_vflag &= ~INP_IPV4;
1093 inp->inp_inc.inc_flags |= INC_ISIPV6;
1094 if ((error = prison_remote_ip6(td->td_ucred,
1095 &sin6->sin6_addr))) {
1108 error = EAFNOSUPPORT;
1113 /* TCP doesn't do control messages (rights, creds, etc) */
1114 if (control->m_len) {
1121 m_freem(control); /* empty control, just free it */
1123 if (!(flags & PRUS_OOB)) {
1124 sbappendstream(&so->so_snd, m, flags);
1125 if (nam && tp->t_state < TCPS_SYN_SENT) {
1127 * Do implied connect if not yet connected,
1128 * initialize window to default value, and
1129 * initialize maxseg using peer's cached MSS.
1133 error = tcp6_connect(tp, nam, td);
1135 #if defined(INET6) && defined(INET)
1139 error = tcp_connect(tp,
1140 (struct sockaddr *)sinp, td);
1143 * The bind operation in tcp_connect succeeded. We
1144 * no longer want to restore the flags if later
1147 if (error == 0 || inp->inp_lport != 0)
1148 restoreflags = false;
1152 if (IS_FASTOPEN(tp->t_flags))
1153 tcp_fastopen_connect(tp);
1155 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1159 if (flags & PRUS_EOF) {
1161 * Close the send side of the connection after
1167 if (!(inp->inp_flags & INP_DROPPED) &&
1168 !(flags & PRUS_NOTREADY)) {
1169 if (flags & PRUS_MORETOCOME)
1170 tp->t_flags |= TF_MORETOCOME;
1171 error = tp->t_fb->tfb_tcp_output(tp);
1172 if (flags & PRUS_MORETOCOME)
1173 tp->t_flags &= ~TF_MORETOCOME;
1177 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1179 SOCKBUF_LOCK(&so->so_snd);
1180 if (sbspace(&so->so_snd) < -512) {
1181 SOCKBUF_UNLOCK(&so->so_snd);
1187 * According to RFC961 (Assigned Protocols),
1188 * the urgent pointer points to the last octet
1189 * of urgent data. We continue, however,
1190 * to consider it to indicate the first octet
1191 * of data past the urgent section.
1192 * Otherwise, snd_up should be one lower.
1194 sbappendstream_locked(&so->so_snd, m, flags);
1195 SOCKBUF_UNLOCK(&so->so_snd);
1196 if (nam && tp->t_state < TCPS_SYN_SENT) {
1198 * Do implied connect if not yet connected,
1199 * initialize window to default value, and
1200 * initialize maxseg using peer's cached MSS.
1204 * Not going to contemplate SYN|URG
1206 if (IS_FASTOPEN(tp->t_flags))
1207 tp->t_flags &= ~TF_FASTOPEN;
1210 error = tcp6_connect(tp, nam, td);
1212 #if defined(INET6) && defined(INET)
1216 error = tcp_connect(tp,
1217 (struct sockaddr *)sinp, td);
1220 * The bind operation in tcp_connect succeeded. We
1221 * no longer want to restore the flags if later
1224 if (error == 0 || inp->inp_lport != 0)
1225 restoreflags = false;
1229 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1232 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1233 if (!(flags & PRUS_NOTREADY)) {
1234 tp->t_flags |= TF_FORCEDATA;
1235 error = tp->t_fb->tfb_tcp_output(tp);
1236 tp->t_flags &= ~TF_FORCEDATA;
1239 TCP_LOG_EVENT(tp, NULL,
1240 &inp->inp_socket->so_rcv,
1241 &inp->inp_socket->so_snd,
1242 TCP_LOG_USERSEND, error,
1246 * If the request was unsuccessful and we changed flags,
1247 * restore the original flags.
1249 if (error != 0 && restoreflags) {
1250 inp->inp_vflag = vflagsav;
1251 inp->inp_inc.inc_flags = incflagsav;
1253 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
1254 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1255 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1256 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1263 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1265 struct epoch_tracker et;
1270 inp = sotoinpcb(so);
1272 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1274 mb_free_notready(m, count);
1275 return (ECONNRESET);
1277 tp = intotcpcb(inp);
1279 SOCKBUF_LOCK(&so->so_snd);
1280 error = sbready(&so->so_snd, m, count);
1281 SOCKBUF_UNLOCK(&so->so_snd);
1283 NET_EPOCH_ENTER(et);
1284 error = tp->t_fb->tfb_tcp_output(tp);
1293 * Abort the TCP. Drop the connection abruptly.
1296 tcp_usr_abort(struct socket *so)
1299 struct tcpcb *tp = NULL;
1300 struct epoch_tracker et;
1303 inp = sotoinpcb(so);
1304 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1306 NET_EPOCH_ENTER(et);
1308 KASSERT(inp->inp_socket != NULL,
1309 ("tcp_usr_abort: inp_socket == NULL"));
1312 * If we still have full TCP state, and we're not dropped, drop.
1314 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1315 !(inp->inp_flags & INP_DROPPED)) {
1316 tp = intotcpcb(inp);
1318 tp = tcp_drop(tp, ECONNABORTED);
1321 TCPDEBUG2(PRU_ABORT);
1322 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1324 if (!(inp->inp_flags & INP_DROPPED)) {
1326 so->so_state |= SS_PROTOREF;
1328 inp->inp_flags |= INP_SOCKREF;
1336 * TCP socket is closed. Start friendly disconnect.
1339 tcp_usr_close(struct socket *so)
1342 struct tcpcb *tp = NULL;
1343 struct epoch_tracker et;
1346 inp = sotoinpcb(so);
1347 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1349 NET_EPOCH_ENTER(et);
1351 KASSERT(inp->inp_socket != NULL,
1352 ("tcp_usr_close: inp_socket == NULL"));
1355 * If we still have full TCP state, and we're not dropped, initiate
1358 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1359 !(inp->inp_flags & INP_DROPPED)) {
1360 tp = intotcpcb(inp);
1363 TCPDEBUG2(PRU_CLOSE);
1364 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1366 if (!(inp->inp_flags & INP_DROPPED)) {
1368 so->so_state |= SS_PROTOREF;
1370 inp->inp_flags |= INP_SOCKREF;
1377 tcp_pru_options_support(struct tcpcb *tp, int flags)
1380 * If the specific TCP stack has a pru_options
1381 * specified then it does not always support
1382 * all the PRU_XX options and we must ask it.
1383 * If the function is not specified then all
1384 * of the PRU_XX options are supported.
1388 if (tp->t_fb->tfb_pru_options) {
1389 ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1395 * Receive out-of-band data.
1398 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1402 struct tcpcb *tp = NULL;
1405 inp = sotoinpcb(so);
1406 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1408 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1412 tp = intotcpcb(inp);
1413 error = tcp_pru_options_support(tp, PRUS_OOB);
1418 if ((so->so_oobmark == 0 &&
1419 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1420 so->so_options & SO_OOBINLINE ||
1421 tp->t_oobflags & TCPOOB_HADDATA) {
1425 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1426 error = EWOULDBLOCK;
1430 *mtod(m, caddr_t) = tp->t_iobc;
1431 if ((flags & MSG_PEEK) == 0)
1432 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1435 TCPDEBUG2(PRU_RCVOOB);
1436 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1442 struct pr_usrreqs tcp_usrreqs = {
1443 .pru_abort = tcp_usr_abort,
1444 .pru_accept = tcp_usr_accept,
1445 .pru_attach = tcp_usr_attach,
1446 .pru_bind = tcp_usr_bind,
1447 .pru_connect = tcp_usr_connect,
1448 .pru_control = in_control,
1449 .pru_detach = tcp_usr_detach,
1450 .pru_disconnect = tcp_usr_disconnect,
1451 .pru_listen = tcp_usr_listen,
1452 .pru_peeraddr = in_getpeeraddr,
1453 .pru_rcvd = tcp_usr_rcvd,
1454 .pru_rcvoob = tcp_usr_rcvoob,
1455 .pru_send = tcp_usr_send,
1456 .pru_ready = tcp_usr_ready,
1457 .pru_shutdown = tcp_usr_shutdown,
1458 .pru_sockaddr = in_getsockaddr,
1459 .pru_sosetlabel = in_pcbsosetlabel,
1460 .pru_close = tcp_usr_close,
1465 struct pr_usrreqs tcp6_usrreqs = {
1466 .pru_abort = tcp_usr_abort,
1467 .pru_accept = tcp6_usr_accept,
1468 .pru_attach = tcp_usr_attach,
1469 .pru_bind = tcp6_usr_bind,
1470 .pru_connect = tcp6_usr_connect,
1471 .pru_control = in6_control,
1472 .pru_detach = tcp_usr_detach,
1473 .pru_disconnect = tcp_usr_disconnect,
1474 .pru_listen = tcp6_usr_listen,
1475 .pru_peeraddr = in6_mapped_peeraddr,
1476 .pru_rcvd = tcp_usr_rcvd,
1477 .pru_rcvoob = tcp_usr_rcvoob,
1478 .pru_send = tcp_usr_send,
1479 .pru_ready = tcp_usr_ready,
1480 .pru_shutdown = tcp_usr_shutdown,
1481 .pru_sockaddr = in6_mapped_sockaddr,
1482 .pru_sosetlabel = in_pcbsosetlabel,
1483 .pru_close = tcp_usr_close,
1489 * Common subroutine to open a TCP connection to remote host specified
1490 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1491 * port number if needed. Call in_pcbconnect_setup to do the routing and
1492 * to choose a local host address (interface). If there is an existing
1493 * incarnation of the same connection in TIME-WAIT state and if the remote
1494 * host was sending CC options and if the connection duration was < MSL, then
1495 * truncate the previous TIME-WAIT state and proceed.
1496 * Initialize connection parameters and enter SYN-SENT state.
1499 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1501 struct inpcb *inp = tp->t_inpcb, *oinp;
1502 struct socket *so = inp->inp_socket;
1503 struct in_addr laddr;
1508 INP_WLOCK_ASSERT(inp);
1509 INP_HASH_WLOCK(&V_tcbinfo);
1511 if (inp->inp_lport == 0) {
1512 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1518 * Cannot simply call in_pcbconnect, because there might be an
1519 * earlier incarnation of this same connection still in
1520 * TIME_WAIT state, creating an ADDRINUSE error.
1522 laddr = inp->inp_laddr;
1523 lport = inp->inp_lport;
1524 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1525 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1526 if (error && oinp == NULL)
1532 inp->inp_laddr = laddr;
1534 INP_HASH_WUNLOCK(&V_tcbinfo);
1537 * Compute window scaling to request:
1538 * Scale to fit into sweet spot. See tcp_syncache.c.
1539 * XXX: This should move to tcp_output().
1541 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1542 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1543 tp->request_r_scale++;
1546 TCPSTAT_INC(tcps_connattempt);
1547 tcp_state_change(tp, TCPS_SYN_SENT);
1548 tp->iss = tcp_new_isn(&inp->inp_inc);
1549 if (tp->t_flags & TF_REQ_TSTMP)
1550 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1551 tcp_sendseqinit(tp);
1556 INP_HASH_WUNLOCK(&V_tcbinfo);
1563 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1565 struct inpcb *inp = tp->t_inpcb;
1568 INP_WLOCK_ASSERT(inp);
1569 INP_HASH_WLOCK(&V_tcbinfo);
1571 if (inp->inp_lport == 0) {
1572 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1576 error = in6_pcbconnect(inp, nam, td->td_ucred);
1579 INP_HASH_WUNLOCK(&V_tcbinfo);
1581 /* Compute window scaling to request. */
1582 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1583 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1584 tp->request_r_scale++;
1586 soisconnecting(inp->inp_socket);
1587 TCPSTAT_INC(tcps_connattempt);
1588 tcp_state_change(tp, TCPS_SYN_SENT);
1589 tp->iss = tcp_new_isn(&inp->inp_inc);
1590 if (tp->t_flags & TF_REQ_TSTMP)
1591 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1592 tcp_sendseqinit(tp);
1597 INP_HASH_WUNLOCK(&V_tcbinfo);
1603 * Export TCP internal state information via a struct tcp_info, based on the
1604 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1605 * (TCP state machine, etc). We export all information using FreeBSD-native
1606 * constants -- for example, the numeric values for tcpi_state will differ
1610 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1613 INP_WLOCK_ASSERT(tp->t_inpcb);
1614 bzero(ti, sizeof(*ti));
1616 ti->tcpi_state = tp->t_state;
1617 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1618 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1619 if (tp->t_flags & TF_SACK_PERMIT)
1620 ti->tcpi_options |= TCPI_OPT_SACK;
1621 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1622 ti->tcpi_options |= TCPI_OPT_WSCALE;
1623 ti->tcpi_snd_wscale = tp->snd_scale;
1624 ti->tcpi_rcv_wscale = tp->rcv_scale;
1626 if (tp->t_flags2 & TF2_ECN_PERMIT)
1627 ti->tcpi_options |= TCPI_OPT_ECN;
1629 ti->tcpi_rto = tp->t_rxtcur * tick;
1630 ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1631 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1632 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1634 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1635 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1638 * FreeBSD-specific extension fields for tcp_info.
1640 ti->tcpi_rcv_space = tp->rcv_wnd;
1641 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1642 ti->tcpi_snd_wnd = tp->snd_wnd;
1643 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1644 ti->tcpi_snd_nxt = tp->snd_nxt;
1645 ti->tcpi_snd_mss = tp->t_maxseg;
1646 ti->tcpi_rcv_mss = tp->t_maxseg;
1647 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1648 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1649 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1651 if (tp->t_flags & TF_TOE) {
1652 ti->tcpi_options |= TCPI_OPT_TOE;
1653 tcp_offload_tcp_info(tp, ti);
1659 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1660 * socket option arguments. When it re-acquires the lock after the copy, it
1661 * has to revalidate that the connection is still valid for the socket
1664 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \
1666 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1669 return (ECONNRESET); \
1671 tp = intotcpcb(inp); \
1673 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1676 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1681 struct tcp_function_block *blk;
1682 struct tcp_function_set fsn;
1685 inp = sotoinpcb(so);
1686 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1687 if (sopt->sopt_level != IPPROTO_TCP) {
1689 if (inp->inp_vflag & INP_IPV6PROTO) {
1690 error = ip6_ctloutput(so, sopt);
1692 * In case of the IPV6_USE_MIN_MTU socket option,
1693 * the INC_IPV6MINMTU flag to announce a corresponding
1694 * MSS during the initial handshake.
1695 * If the TCP connection is not in the front states,
1696 * just reduce the MSS being used.
1697 * This avoids the sending of TCP segments which will
1698 * be fragmented at the IPv6 layer.
1701 (sopt->sopt_dir == SOPT_SET) &&
1702 (sopt->sopt_level == IPPROTO_IPV6) &&
1703 (sopt->sopt_name == IPV6_USE_MIN_MTU)) {
1705 if ((inp->inp_flags &
1706 (INP_TIMEWAIT | INP_DROPPED))) {
1708 return (ECONNRESET);
1710 inp->inp_inc.inc_flags |= INC_IPV6MINMTU;
1711 tp = intotcpcb(inp);
1712 if ((tp->t_state >= TCPS_SYN_SENT) &&
1713 (inp->inp_inc.inc_flags & INC_ISIPV6)) {
1714 struct ip6_pktopts *opt;
1716 opt = inp->in6p_outputopts;
1717 if ((opt != NULL) &&
1718 (opt->ip6po_minmtu ==
1719 IP6PO_MINMTU_ALL)) {
1720 if (tp->t_maxseg > TCP6_MSS) {
1721 tp->t_maxseg = TCP6_MSS;
1729 #if defined(INET6) && defined(INET)
1734 error = ip_ctloutput(so, sopt);
1740 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1742 return (ECONNRESET);
1744 tp = intotcpcb(inp);
1746 * Protect the TCP option TCP_FUNCTION_BLK so
1747 * that a sub-function can *never* overwrite this.
1749 if ((sopt->sopt_dir == SOPT_SET) &&
1750 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1752 error = sooptcopyin(sopt, &fsn, sizeof fsn,
1756 INP_WLOCK_RECHECK(inp);
1757 blk = find_and_ref_tcp_functions(&fsn);
1762 if (tp->t_fb == blk) {
1763 /* You already have this */
1764 refcount_release(&blk->tfb_refcnt);
1768 if (tp->t_state != TCPS_CLOSED) {
1770 * The user has advanced the state
1771 * past the initial point, we may not
1772 * be able to switch.
1774 if (blk->tfb_tcp_handoff_ok != NULL) {
1776 * Does the stack provide a
1777 * query mechanism, if so it may
1778 * still be possible?
1780 error = (*blk->tfb_tcp_handoff_ok)(tp);
1784 refcount_release(&blk->tfb_refcnt);
1789 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1790 refcount_release(&blk->tfb_refcnt);
1795 * Release the old refcnt, the
1796 * lookup acquired a ref on the
1799 if (tp->t_fb->tfb_tcp_fb_fini) {
1801 * Tell the stack to cleanup with 0 i.e.
1802 * the tcb is not going away.
1804 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1807 /* Assure that we are not on any hpts */
1808 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
1810 if (blk->tfb_tcp_fb_init) {
1811 error = (*blk->tfb_tcp_fb_init)(tp);
1813 refcount_release(&blk->tfb_refcnt);
1814 if (tp->t_fb->tfb_tcp_fb_init) {
1815 if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) {
1816 /* Fall back failed, drop the connection */
1825 refcount_release(&tp->t_fb->tfb_refcnt);
1828 if (tp->t_flags & TF_TOE) {
1829 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1836 } else if ((sopt->sopt_dir == SOPT_GET) &&
1837 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1838 strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1839 TCP_FUNCTION_NAME_LEN_MAX);
1840 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1841 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1843 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1846 /* Pass in the INP locked, called must unlock it */
1847 return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
1851 * If this assert becomes untrue, we need to change the size of the buf
1852 * variable in tcp_default_ctloutput().
1855 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1856 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1861 copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1863 struct tls_enable_v0 tls_v0;
1866 if (sopt->sopt_valsize == sizeof(tls_v0)) {
1867 error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1871 memset(tls, 0, sizeof(*tls));
1872 tls->cipher_key = tls_v0.cipher_key;
1873 tls->iv = tls_v0.iv;
1874 tls->auth_key = tls_v0.auth_key;
1875 tls->cipher_algorithm = tls_v0.cipher_algorithm;
1876 tls->cipher_key_len = tls_v0.cipher_key_len;
1877 tls->iv_len = tls_v0.iv_len;
1878 tls->auth_algorithm = tls_v0.auth_algorithm;
1879 tls->auth_key_len = tls_v0.auth_key_len;
1880 tls->flags = tls_v0.flags;
1881 tls->tls_vmajor = tls_v0.tls_vmajor;
1882 tls->tls_vminor = tls_v0.tls_vminor;
1886 return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1891 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
1893 int error, opt, optval;
1897 struct tls_enable tls;
1899 struct cc_algo *algo;
1900 char *pbuf, buf[TCP_LOG_ID_LEN];
1902 struct statsblob *sbp;
1907 * For TCP_CCALGOOPT forward the control to CC module, for both
1908 * SOPT_SET and SOPT_GET.
1910 switch (sopt->sopt_name) {
1913 if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
1915 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1916 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1917 sopt->sopt_valsize);
1922 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1923 if (CC_ALGO(tp)->ctl_output != NULL)
1924 error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1928 if (error == 0 && sopt->sopt_dir == SOPT_GET)
1929 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1934 switch (sopt->sopt_dir) {
1936 switch (sopt->sopt_name) {
1937 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1939 if (!TCPMD5_ENABLED()) {
1941 return (ENOPROTOOPT);
1943 error = TCPMD5_PCBCTL(inp, sopt);
1946 goto unlock_and_done;
1952 error = sooptcopyin(sopt, &optval, sizeof optval,
1957 INP_WLOCK_RECHECK(inp);
1958 switch (sopt->sopt_name) {
1966 opt = 0; /* dead code to fool gcc */
1973 tp->t_flags &= ~opt;
1976 if (tp->t_flags & TF_TOE) {
1977 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1986 error = sooptcopyin(sopt, &optval, sizeof optval,
1991 INP_WLOCK_RECHECK(inp);
1993 tp->t_flags |= TF_NOPUSH;
1994 else if (tp->t_flags & TF_NOPUSH) {
1995 tp->t_flags &= ~TF_NOPUSH;
1996 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
1997 struct epoch_tracker et;
1999 NET_EPOCH_ENTER(et);
2000 error = tp->t_fb->tfb_tcp_output(tp);
2004 goto unlock_and_done;
2008 error = sooptcopyin(sopt, &optval, sizeof optval,
2013 INP_WLOCK_RECHECK(inp);
2014 if (optval > 0 && optval <= tp->t_maxseg &&
2015 optval + 40 >= V_tcp_minmss)
2016 tp->t_maxseg = optval;
2019 goto unlock_and_done;
2029 error = sooptcopyin(sopt, &optval, sizeof optval,
2035 sbp = stats_blob_alloc(
2036 V_tcp_perconn_stats_dflt_tpl, 0);
2040 INP_WLOCK_RECHECK(inp);
2041 if ((tp->t_stats != NULL && sbp == NULL) ||
2042 (tp->t_stats == NULL && sbp != NULL)) {
2043 struct statsblob *t = tp->t_stats;
2049 stats_blob_destroy(sbp);
2051 return (EOPNOTSUPP);
2055 case TCP_CONGESTION:
2057 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
2060 buf[sopt->sopt_valsize] = '\0';
2061 INP_WLOCK_RECHECK(inp);
2063 STAILQ_FOREACH(algo, &cc_list, entries)
2064 if (strncmp(buf, algo->name,
2065 TCP_CA_NAME_MAX) == 0)
2074 * We hold a write lock over the tcb so it's safe to
2075 * do these things without ordering concerns.
2077 if (CC_ALGO(tp)->cb_destroy != NULL)
2078 CC_ALGO(tp)->cb_destroy(tp->ccv);
2082 * If something goes pear shaped initialising the new
2083 * algo, fall back to newreno (which does not
2084 * require initialisation).
2086 if (algo->cb_init != NULL &&
2087 algo->cb_init(tp->ccv) != 0) {
2088 CC_ALGO(tp) = &newreno_cc_algo;
2090 * The only reason init should fail is
2091 * because of malloc.
2099 case TCP_TXTLS_ENABLE:
2101 error = copyin_tls_enable(sopt, &tls);
2104 error = ktls_enable_tx(so, &tls);
2106 case TCP_TXTLS_MODE:
2108 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2112 INP_WLOCK_RECHECK(inp);
2113 error = ktls_set_tx_mode(so, ui);
2116 case TCP_RXTLS_ENABLE:
2118 error = sooptcopyin(sopt, &tls, sizeof(tls),
2122 error = ktls_enable_rx(so, &tls);
2130 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2134 if (ui > (UINT_MAX / hz)) {
2140 INP_WLOCK_RECHECK(inp);
2141 switch (sopt->sopt_name) {
2143 tp->t_keepidle = ui;
2145 * XXX: better check current remaining
2146 * timeout and "merge" it with new value.
2148 if ((tp->t_state > TCPS_LISTEN) &&
2149 (tp->t_state <= TCPS_CLOSING))
2150 tcp_timer_activate(tp, TT_KEEP,
2154 tp->t_keepintvl = ui;
2155 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2156 (TP_MAXIDLE(tp) > 0))
2157 tcp_timer_activate(tp, TT_2MSL,
2161 tp->t_keepinit = ui;
2162 if (tp->t_state == TCPS_SYN_RECEIVED ||
2163 tp->t_state == TCPS_SYN_SENT)
2164 tcp_timer_activate(tp, TT_KEEP,
2168 goto unlock_and_done;
2172 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2176 INP_WLOCK_RECHECK(inp);
2178 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2179 (TP_MAXIDLE(tp) > 0))
2180 tcp_timer_activate(tp, TT_2MSL,
2182 goto unlock_and_done;
2188 error = sooptcopyin(sopt, &optval, sizeof optval,
2193 INP_WLOCK_RECHECK(inp);
2195 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
2196 &(tp->t_outpkts) : &(tp->t_inpkts),
2200 goto unlock_and_done;
2203 case TCP_FASTOPEN: {
2204 struct tcp_fastopen tfo_optval;
2207 if (!V_tcp_fastopen_client_enable &&
2208 !V_tcp_fastopen_server_enable)
2211 error = sooptcopyin(sopt, &tfo_optval,
2212 sizeof(tfo_optval), sizeof(int));
2216 INP_WLOCK_RECHECK(inp);
2217 if (tfo_optval.enable) {
2218 if (tp->t_state == TCPS_LISTEN) {
2219 if (!V_tcp_fastopen_server_enable) {
2221 goto unlock_and_done;
2224 tp->t_flags |= TF_FASTOPEN;
2225 if (tp->t_tfo_pending == NULL)
2227 tcp_fastopen_alloc_counter();
2230 * If a pre-shared key was provided,
2231 * stash it in the client cookie
2232 * field of the tcpcb for use during
2235 if (sopt->sopt_valsize ==
2236 sizeof(tfo_optval)) {
2237 memcpy(tp->t_tfo_cookie.client,
2239 TCP_FASTOPEN_PSK_LEN);
2240 tp->t_tfo_client_cookie_len =
2241 TCP_FASTOPEN_PSK_LEN;
2243 tp->t_flags |= TF_FASTOPEN;
2246 tp->t_flags &= ~TF_FASTOPEN;
2247 goto unlock_and_done;
2253 error = sooptcopyin(sopt, &optval, sizeof optval,
2258 INP_WLOCK_RECHECK(inp);
2259 error = tcp_log_state_change(tp, optval);
2260 goto unlock_and_done;
2269 error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
2272 buf[sopt->sopt_valsize] = '\0';
2273 INP_WLOCK_RECHECK(inp);
2274 error = tcp_log_set_id(tp, buf);
2275 /* tcp_log_set_id() unlocks the INP. */
2282 sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
2285 buf[sopt->sopt_valsize] = '\0';
2286 INP_WLOCK_RECHECK(inp);
2287 if (sopt->sopt_name == TCP_LOGDUMP) {
2288 error = tcp_log_dump_tp_logbuf(tp, buf,
2292 tcp_log_dump_tp_bucket_logbufs(tp, buf);
2294 * tcp_log_dump_tp_bucket_logbufs() drops the
2303 error = ENOPROTOOPT;
2309 tp = intotcpcb(inp);
2310 switch (sopt->sopt_name) {
2311 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2313 if (!TCPMD5_ENABLED()) {
2315 return (ENOPROTOOPT);
2317 error = TCPMD5_PCBCTL(inp, sopt);
2322 optval = tp->t_flags & TF_NODELAY;
2324 error = sooptcopyout(sopt, &optval, sizeof optval);
2327 optval = tp->t_maxseg;
2329 error = sooptcopyout(sopt, &optval, sizeof optval);
2332 optval = tp->t_flags & TF_NOOPT;
2334 error = sooptcopyout(sopt, &optval, sizeof optval);
2337 optval = tp->t_flags & TF_NOPUSH;
2339 error = sooptcopyout(sopt, &optval, sizeof optval);
2342 tcp_fill_info(tp, &ti);
2344 error = sooptcopyout(sopt, &ti, sizeof ti);
2350 TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2353 socklen_t outsbsz = sopt->sopt_valsize;
2354 if (tp->t_stats == NULL)
2356 else if (outsbsz >= tp->t_stats->cursz)
2357 outsbsz = tp->t_stats->cursz;
2358 else if (outsbsz >= sizeof(struct statsblob))
2359 outsbsz = sizeof(struct statsblob);
2366 sbp = sopt->sopt_val;
2367 nheld = atop(round_page(((vm_offset_t)sbp) +
2368 (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2369 vm_page_t ma[nheld];
2370 if (vm_fault_quick_hold_pages(
2371 &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2372 outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2378 if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2379 SIZEOF_MEMBER(struct statsblob, flags))))
2382 INP_WLOCK_RECHECK(inp);
2383 error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2384 sbflags | SB_CLONE_USRDSTNOFAULT);
2386 sopt->sopt_valsize = outsbsz;
2388 vm_page_unhold_pages(ma, nheld);
2395 case TCP_CONGESTION:
2396 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2398 error = sooptcopyout(sopt, buf, len + 1);
2404 switch (sopt->sopt_name) {
2406 ui = TP_KEEPIDLE(tp) / hz;
2409 ui = TP_KEEPINTVL(tp) / hz;
2412 ui = TP_KEEPINIT(tp) / hz;
2415 ui = TP_KEEPCNT(tp);
2419 error = sooptcopyout(sopt, &ui, sizeof(ui));
2424 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
2425 &(tp->t_outpkts) : &(tp->t_inpkts));
2427 error = sooptcopyout(sopt, &optval, sizeof optval);
2431 optval = tp->t_flags & TF_FASTOPEN;
2433 error = sooptcopyout(sopt, &optval, sizeof optval);
2437 optval = tp->t_logstate;
2439 error = sooptcopyout(sopt, &optval, sizeof(optval));
2442 /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2443 error = tcp_log_getlogbuf(sopt, tp);
2446 len = tcp_log_get_id(tp, buf);
2448 error = sooptcopyout(sopt, buf, len + 1);
2457 case TCP_TXTLS_MODE:
2458 optval = ktls_get_tx_mode(so);
2460 error = sooptcopyout(sopt, &optval, sizeof(optval));
2462 case TCP_RXTLS_MODE:
2463 optval = ktls_get_rx_mode(so);
2465 error = sooptcopyout(sopt, &optval, sizeof(optval));
2470 error = ENOPROTOOPT;
2477 #undef INP_WLOCK_RECHECK
2478 #undef INP_WLOCK_RECHECK_CLEANUP
2481 * Initiate (or continue) disconnect.
2482 * If embryonic state, just send reset (once).
2483 * If in ``let data drain'' option and linger null, just drop.
2484 * Otherwise (hard), mark socket disconnecting and drop
2485 * current input data; switch states based on user close, and
2486 * send segment to peer (with FIN).
2489 tcp_disconnect(struct tcpcb *tp)
2491 struct inpcb *inp = tp->t_inpcb;
2492 struct socket *so = inp->inp_socket;
2495 INP_WLOCK_ASSERT(inp);
2498 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2499 * socket is still open.
2501 if (tp->t_state < TCPS_ESTABLISHED &&
2502 !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2505 ("tcp_disconnect: tcp_close() returned NULL"));
2506 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2507 tp = tcp_drop(tp, 0);
2509 ("tcp_disconnect: tcp_drop() returned NULL"));
2511 soisdisconnecting(so);
2512 sbflush(&so->so_rcv);
2514 if (!(inp->inp_flags & INP_DROPPED))
2515 tp->t_fb->tfb_tcp_output(tp);
2520 * User issued close, and wish to trail through shutdown states:
2521 * if never received SYN, just forget it. If got a SYN from peer,
2522 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2523 * If already got a FIN from peer, then almost done; go to LAST_ACK
2524 * state. In all other cases, have already sent FIN to peer (e.g.
2525 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2526 * for peer to send FIN or not respond to keep-alives, etc.
2527 * We can let the user exit from the close as soon as the FIN is acked.
2530 tcp_usrclosed(struct tcpcb *tp)
2534 INP_WLOCK_ASSERT(tp->t_inpcb);
2536 switch (tp->t_state) {
2539 tcp_offload_listen_stop(tp);
2541 tcp_state_change(tp, TCPS_CLOSED);
2546 * tcp_close() should never return NULL here as the socket is
2550 ("tcp_usrclosed: tcp_close() returned NULL"));
2554 case TCPS_SYN_RECEIVED:
2555 tp->t_flags |= TF_NEEDFIN;
2558 case TCPS_ESTABLISHED:
2559 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2562 case TCPS_CLOSE_WAIT:
2563 tcp_state_change(tp, TCPS_LAST_ACK);
2566 if (tp->t_state >= TCPS_FIN_WAIT_2) {
2567 soisdisconnected(tp->t_inpcb->inp_socket);
2568 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
2569 if (tp->t_state == TCPS_FIN_WAIT_2) {
2572 timeout = (tcp_fast_finwait2_recycle) ?
2573 tcp_finwait2_timeout : TP_MAXIDLE(tp);
2574 tcp_timer_activate(tp, TT_2MSL, timeout);
2581 db_print_indent(int indent)
2585 for (i = 0; i < indent; i++)
2590 db_print_tstate(int t_state)
2595 db_printf("TCPS_CLOSED");
2599 db_printf("TCPS_LISTEN");
2603 db_printf("TCPS_SYN_SENT");
2606 case TCPS_SYN_RECEIVED:
2607 db_printf("TCPS_SYN_RECEIVED");
2610 case TCPS_ESTABLISHED:
2611 db_printf("TCPS_ESTABLISHED");
2614 case TCPS_CLOSE_WAIT:
2615 db_printf("TCPS_CLOSE_WAIT");
2618 case TCPS_FIN_WAIT_1:
2619 db_printf("TCPS_FIN_WAIT_1");
2623 db_printf("TCPS_CLOSING");
2627 db_printf("TCPS_LAST_ACK");
2630 case TCPS_FIN_WAIT_2:
2631 db_printf("TCPS_FIN_WAIT_2");
2634 case TCPS_TIME_WAIT:
2635 db_printf("TCPS_TIME_WAIT");
2639 db_printf("unknown");
2645 db_print_tflags(u_int t_flags)
2650 if (t_flags & TF_ACKNOW) {
2651 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2654 if (t_flags & TF_DELACK) {
2655 db_printf("%sTF_DELACK", comma ? ", " : "");
2658 if (t_flags & TF_NODELAY) {
2659 db_printf("%sTF_NODELAY", comma ? ", " : "");
2662 if (t_flags & TF_NOOPT) {
2663 db_printf("%sTF_NOOPT", comma ? ", " : "");
2666 if (t_flags & TF_SENTFIN) {
2667 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2670 if (t_flags & TF_REQ_SCALE) {
2671 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2674 if (t_flags & TF_RCVD_SCALE) {
2675 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2678 if (t_flags & TF_REQ_TSTMP) {
2679 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2682 if (t_flags & TF_RCVD_TSTMP) {
2683 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2686 if (t_flags & TF_SACK_PERMIT) {
2687 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2690 if (t_flags & TF_NEEDSYN) {
2691 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2694 if (t_flags & TF_NEEDFIN) {
2695 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2698 if (t_flags & TF_NOPUSH) {
2699 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2702 if (t_flags & TF_MORETOCOME) {
2703 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2706 if (t_flags & TF_LQ_OVERFLOW) {
2707 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2710 if (t_flags & TF_LASTIDLE) {
2711 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2714 if (t_flags & TF_RXWIN0SENT) {
2715 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2718 if (t_flags & TF_FASTRECOVERY) {
2719 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2722 if (t_flags & TF_CONGRECOVERY) {
2723 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2726 if (t_flags & TF_WASFRECOVERY) {
2727 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2730 if (t_flags & TF_SIGNATURE) {
2731 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2734 if (t_flags & TF_FORCEDATA) {
2735 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2738 if (t_flags & TF_TSO) {
2739 db_printf("%sTF_TSO", comma ? ", " : "");
2742 if (t_flags & TF_FASTOPEN) {
2743 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2749 db_print_tflags2(u_int t_flags2)
2754 if (t_flags2 & TF2_ECN_PERMIT) {
2755 db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
2762 db_print_toobflags(char t_oobflags)
2767 if (t_oobflags & TCPOOB_HAVEDATA) {
2768 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2771 if (t_oobflags & TCPOOB_HADDATA) {
2772 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2778 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2781 db_print_indent(indent);
2782 db_printf("%s at %p\n", name, tp);
2786 db_print_indent(indent);
2787 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2788 TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2790 db_print_indent(indent);
2791 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
2792 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2794 db_print_indent(indent);
2795 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2796 &tp->t_timers->tt_delack, tp->t_inpcb);
2798 db_print_indent(indent);
2799 db_printf("t_state: %d (", tp->t_state);
2800 db_print_tstate(tp->t_state);
2803 db_print_indent(indent);
2804 db_printf("t_flags: 0x%x (", tp->t_flags);
2805 db_print_tflags(tp->t_flags);
2808 db_print_indent(indent);
2809 db_printf("t_flags2: 0x%x (", tp->t_flags2);
2810 db_print_tflags2(tp->t_flags2);
2813 db_print_indent(indent);
2814 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2815 tp->snd_una, tp->snd_max, tp->snd_nxt);
2817 db_print_indent(indent);
2818 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2819 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2821 db_print_indent(indent);
2822 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2823 tp->iss, tp->irs, tp->rcv_nxt);
2825 db_print_indent(indent);
2826 db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n",
2827 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2829 db_print_indent(indent);
2830 db_printf("snd_wnd: %u snd_cwnd: %u\n",
2831 tp->snd_wnd, tp->snd_cwnd);
2833 db_print_indent(indent);
2834 db_printf("snd_ssthresh: %u snd_recover: "
2835 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2837 db_print_indent(indent);
2838 db_printf("t_rcvtime: %u t_startime: %u\n",
2839 tp->t_rcvtime, tp->t_starttime);
2841 db_print_indent(indent);
2842 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2843 tp->t_rtttime, tp->t_rtseq);
2845 db_print_indent(indent);
2846 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2847 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2849 db_print_indent(indent);
2850 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2851 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2854 db_print_indent(indent);
2855 db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n",
2856 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2858 db_print_indent(indent);
2859 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2860 db_print_toobflags(tp->t_oobflags);
2861 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2863 db_print_indent(indent);
2864 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2865 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2867 db_print_indent(indent);
2868 db_printf("ts_recent: %u ts_recent_age: %u\n",
2869 tp->ts_recent, tp->ts_recent_age);
2871 db_print_indent(indent);
2872 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2873 "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2875 db_print_indent(indent);
2876 db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x "
2877 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2878 tp->snd_recover_prev, tp->t_badrxtwin);
2880 db_print_indent(indent);
2881 db_printf("snd_numholes: %d snd_holes first: %p\n",
2882 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2884 db_print_indent(indent);
2885 db_printf("snd_fack: 0x%08x rcv_numsacks: %d\n",
2886 tp->snd_fack, tp->rcv_numsacks);
2888 /* Skip sackblks, sackhint. */
2890 db_print_indent(indent);
2891 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2892 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2895 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2900 db_printf("usage: show tcpcb <addr>\n");
2903 tp = (struct tcpcb *)addr;
2905 db_print_tcpcb(tp, "tcpcb", 0);