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 *);
137 #define TCPDEBUG0 int ostate = 0
138 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
139 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
140 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
144 #define TCPDEBUG2(req)
148 * TCP attaches to socket via pru_attach(), reserving space,
149 * and an internet control block.
152 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
155 struct tcpcb *tp = NULL;
160 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
163 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
164 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
169 so->so_rcv.sb_flags |= SB_AUTOSIZE;
170 so->so_snd.sb_flags |= SB_AUTOSIZE;
171 error = in_pcballoc(so, &V_tcbinfo);
176 if (inp->inp_vflag & INP_IPV6PROTO) {
177 inp->inp_vflag |= INP_IPV6;
178 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
179 inp->inp_vflag |= INP_IPV4;
180 inp->in6p_hops = -1; /* use kernel default */
184 inp->inp_vflag |= INP_IPV4;
185 tp = tcp_newtcpcb(inp);
192 tp->t_state = TCPS_CLOSED;
194 TCPSTATES_INC(TCPS_CLOSED);
195 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
196 so->so_linger = TCP_LINGERTIME;
198 TCPDEBUG2(PRU_ATTACH);
199 TCP_PROBE2(debug__user, tp, PRU_ATTACH);
204 * tcp_detach is called when the socket layer loses its final reference
205 * to the socket, be it a file descriptor reference, a reference from TCP,
206 * etc. At this point, there is only one case in which we will keep around
207 * inpcb state: time wait.
209 * This function can probably be re-absorbed back into tcp_usr_detach() now
210 * that there is a single detach path.
213 tcp_detach(struct socket *so, struct inpcb *inp)
217 INP_INFO_LOCK_ASSERT(&V_tcbinfo);
218 INP_WLOCK_ASSERT(inp);
220 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
221 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
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_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_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_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_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) {
304 * pru_detach() detaches the TCP protocol from the socket.
305 * If the protocol state is non-embryonic, then can't
306 * do this directly: have to initiate a pru_disconnect(),
307 * which may finish later; embryonic TCB's can just
311 tcp_usr_detach(struct socket *so)
315 struct epoch_tracker et;
318 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
319 if (!INP_INFO_WLOCKED(&V_tcbinfo)) {
324 KASSERT(inp->inp_socket != NULL,
325 ("tcp_usr_detach: inp_socket == NULL"));
333 * Give the socket an address.
336 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
340 struct tcpcb *tp = NULL;
341 struct sockaddr_in *sinp;
343 sinp = (struct sockaddr_in *)nam;
344 if (nam->sa_len != sizeof (*sinp))
347 * Must check for multicast addresses and disallow binding
350 if (sinp->sin_family == AF_INET &&
351 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
352 return (EAFNOSUPPORT);
356 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
358 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
364 INP_HASH_WLOCK(&V_tcbinfo);
365 error = in_pcbbind(inp, nam, td->td_ucred);
366 INP_HASH_WUNLOCK(&V_tcbinfo);
369 TCP_PROBE2(debug__user, tp, PRU_BIND);
378 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
382 struct tcpcb *tp = NULL;
383 struct sockaddr_in6 *sin6;
386 sin6 = (struct sockaddr_in6 *)nam;
387 if (nam->sa_len != sizeof (*sin6))
390 * Must check for multicast addresses and disallow binding
393 if (sin6->sin6_family == AF_INET6 &&
394 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
395 return (EAFNOSUPPORT);
399 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
401 vflagsav = inp->inp_vflag;
402 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
408 INP_HASH_WLOCK(&V_tcbinfo);
409 inp->inp_vflag &= ~INP_IPV4;
410 inp->inp_vflag |= INP_IPV6;
412 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
413 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
414 inp->inp_vflag |= INP_IPV4;
415 else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
416 struct sockaddr_in sin;
418 in6_sin6_2_sin(&sin, sin6);
419 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
420 error = EAFNOSUPPORT;
421 INP_HASH_WUNLOCK(&V_tcbinfo);
424 inp->inp_vflag |= INP_IPV4;
425 inp->inp_vflag &= ~INP_IPV6;
426 error = in_pcbbind(inp, (struct sockaddr *)&sin,
428 INP_HASH_WUNLOCK(&V_tcbinfo);
433 error = in6_pcbbind(inp, nam, td->td_ucred);
434 INP_HASH_WUNLOCK(&V_tcbinfo);
437 inp->inp_vflag = vflagsav;
439 TCP_PROBE2(debug__user, tp, PRU_BIND);
447 * Prepare to accept connections.
450 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
454 struct tcpcb *tp = NULL;
458 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
460 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
467 error = solisten_proto_check(so);
468 INP_HASH_WLOCK(&V_tcbinfo);
469 if (error == 0 && inp->inp_lport == 0)
470 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
471 INP_HASH_WUNLOCK(&V_tcbinfo);
473 tcp_state_change(tp, TCPS_LISTEN);
474 solisten_proto(so, backlog);
476 if ((so->so_options & SO_NO_OFFLOAD) == 0)
477 tcp_offload_listen_start(tp);
482 if (IS_FASTOPEN(tp->t_flags))
483 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
486 TCPDEBUG2(PRU_LISTEN);
487 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
495 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
499 struct tcpcb *tp = NULL;
504 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
506 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
510 vflagsav = inp->inp_vflag;
514 error = solisten_proto_check(so);
515 INP_HASH_WLOCK(&V_tcbinfo);
516 if (error == 0 && inp->inp_lport == 0) {
517 inp->inp_vflag &= ~INP_IPV4;
518 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
519 inp->inp_vflag |= INP_IPV4;
520 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
522 INP_HASH_WUNLOCK(&V_tcbinfo);
524 tcp_state_change(tp, TCPS_LISTEN);
525 solisten_proto(so, backlog);
527 if ((so->so_options & SO_NO_OFFLOAD) == 0)
528 tcp_offload_listen_start(tp);
533 if (IS_FASTOPEN(tp->t_flags))
534 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
537 inp->inp_vflag = vflagsav;
540 TCPDEBUG2(PRU_LISTEN);
541 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
549 * Initiate connection to peer.
550 * Create a template for use in transmissions on this connection.
551 * Enter SYN_SENT state, and mark socket as connecting.
552 * Start keep-alive timer, and seed output sequence space.
553 * Send initial segment on connection.
556 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
558 struct epoch_tracker et;
561 struct tcpcb *tp = NULL;
562 struct sockaddr_in *sinp;
564 sinp = (struct sockaddr_in *)nam;
565 if (nam->sa_len != sizeof (*sinp))
568 * Must disallow TCP ``connections'' to multicast addresses.
570 if (sinp->sin_family == AF_INET
571 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
572 return (EAFNOSUPPORT);
573 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
578 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
580 if (inp->inp_flags & INP_TIMEWAIT) {
584 if (inp->inp_flags & INP_DROPPED) {
585 error = ECONNREFUSED;
590 if ((error = tcp_connect(tp, nam, td)) != 0)
593 if (registered_toedevs > 0 &&
594 (so->so_options & SO_NO_OFFLOAD) == 0 &&
595 (error = tcp_offload_connect(so, nam)) == 0)
598 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
600 error = tp->t_fb->tfb_tcp_output(tp);
603 TCPDEBUG2(PRU_CONNECT);
604 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
612 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
614 struct epoch_tracker et;
617 struct tcpcb *tp = NULL;
618 struct sockaddr_in6 *sin6;
624 sin6 = (struct sockaddr_in6 *)nam;
625 if (nam->sa_len != sizeof (*sin6))
628 * Must disallow TCP ``connections'' to multicast addresses.
630 if (sin6->sin6_family == AF_INET6
631 && IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
632 return (EAFNOSUPPORT);
635 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
637 vflagsav = inp->inp_vflag;
638 incflagsav = inp->inp_inc.inc_flags;
639 if (inp->inp_flags & INP_TIMEWAIT) {
643 if (inp->inp_flags & INP_DROPPED) {
644 error = ECONNREFUSED;
651 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
652 * therefore probably require the hash lock, which isn't held here.
653 * Is this a significant problem?
655 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
656 struct sockaddr_in sin;
658 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
662 if ((inp->inp_vflag & INP_IPV4) == 0) {
663 error = EAFNOSUPPORT;
667 in6_sin6_2_sin(&sin, sin6);
668 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
669 error = EAFNOSUPPORT;
672 if ((error = prison_remote_ip4(td->td_ucred,
673 &sin.sin_addr)) != 0)
675 inp->inp_vflag |= INP_IPV4;
676 inp->inp_vflag &= ~INP_IPV6;
677 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
680 if (registered_toedevs > 0 &&
681 (so->so_options & SO_NO_OFFLOAD) == 0 &&
682 (error = tcp_offload_connect(so, nam)) == 0)
686 error = tp->t_fb->tfb_tcp_output(tp);
690 if ((inp->inp_vflag & INP_IPV6) == 0) {
691 error = EAFNOSUPPORT;
696 if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
698 inp->inp_vflag &= ~INP_IPV4;
699 inp->inp_vflag |= INP_IPV6;
700 inp->inp_inc.inc_flags |= INC_ISIPV6;
701 if ((error = tcp6_connect(tp, nam, td)) != 0)
704 if (registered_toedevs > 0 &&
705 (so->so_options & SO_NO_OFFLOAD) == 0 &&
706 (error = tcp_offload_connect(so, nam)) == 0)
709 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
711 error = tp->t_fb->tfb_tcp_output(tp);
715 * If the implicit bind in the connect call fails, restore
716 * the flags we modified.
718 if (error != 0 && inp->inp_lport == 0) {
719 inp->inp_vflag = vflagsav;
720 inp->inp_inc.inc_flags = incflagsav;
723 TCPDEBUG2(PRU_CONNECT);
724 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
731 * Initiate disconnect from peer.
732 * If connection never passed embryonic stage, just drop;
733 * else if don't need to let data drain, then can just drop anyways,
734 * else have to begin TCP shutdown process: mark socket disconnecting,
735 * drain unread data, state switch to reflect user close, and
736 * send segment (e.g. FIN) to peer. Socket will be really disconnected
737 * when peer sends FIN and acks ours.
739 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
742 tcp_usr_disconnect(struct socket *so)
745 struct tcpcb *tp = NULL;
746 struct epoch_tracker et;
752 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
754 if (inp->inp_flags & INP_TIMEWAIT)
756 if (inp->inp_flags & INP_DROPPED) {
764 TCPDEBUG2(PRU_DISCONNECT);
765 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
773 * Accept a connection. Essentially all the work is done at higher levels;
774 * just return the address of the peer, storing through addr.
777 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
780 struct inpcb *inp = NULL;
781 struct tcpcb *tp = NULL;
786 if (so->so_state & SS_ISDISCONNECTED)
787 return (ECONNABORTED);
790 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
792 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
793 error = ECONNABORTED;
800 * We inline in_getpeeraddr and COMMON_END here, so that we can
801 * copy the data of interest and defer the malloc until after we
804 port = inp->inp_fport;
805 addr = inp->inp_faddr;
808 TCPDEBUG2(PRU_ACCEPT);
809 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
812 *nam = in_sockaddr(port, &addr);
819 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
821 struct inpcb *inp = NULL;
823 struct tcpcb *tp = NULL;
825 struct in6_addr addr6;
826 struct epoch_tracker et;
831 if (so->so_state & SS_ISDISCONNECTED)
832 return (ECONNABORTED);
835 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
838 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
839 error = ECONNABORTED;
846 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
847 * copy the data of interest and defer the malloc until after we
850 if (inp->inp_vflag & INP_IPV4) {
852 port = inp->inp_fport;
853 addr = inp->inp_faddr;
855 port = inp->inp_fport;
856 addr6 = inp->in6p_faddr;
860 TCPDEBUG2(PRU_ACCEPT);
861 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
866 *nam = in6_v4mapsin6_sockaddr(port, &addr);
868 *nam = in6_sockaddr(port, &addr6);
875 * Mark the connection as being incapable of further output.
878 tcp_usr_shutdown(struct socket *so)
882 struct tcpcb *tp = NULL;
883 struct epoch_tracker et;
888 KASSERT(inp != NULL, ("inp == NULL"));
890 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
898 if (!(inp->inp_flags & INP_DROPPED))
899 error = tp->t_fb->tfb_tcp_output(tp);
902 TCPDEBUG2(PRU_SHUTDOWN);
903 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
911 * After a receive, possibly send window update to peer.
914 tcp_usr_rcvd(struct socket *so, int flags)
916 struct epoch_tracker et;
918 struct tcpcb *tp = NULL;
923 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
925 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
932 * For passively-created TFO connections, don't attempt a window
933 * update while still in SYN_RECEIVED as this may trigger an early
934 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
935 * application response data, or failing that, when the DELACK timer
938 if (IS_FASTOPEN(tp->t_flags) &&
939 (tp->t_state == TCPS_SYN_RECEIVED))
942 if (tp->t_flags & TF_TOE)
943 tcp_offload_rcvd(tp);
947 tp->t_fb->tfb_tcp_output(tp);
951 TCP_PROBE2(debug__user, tp, PRU_RCVD);
957 * Do a send by putting data in output queue and updating urgent
958 * marker if URG set. Possibly send more data. Unlike the other
959 * pru_*() routines, the mbuf chains are our responsibility. We
960 * must either enqueue them or free them. The other pru_* routines
961 * generally are caller-frees.
964 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
965 struct sockaddr *nam, struct mbuf *control, struct thread *td)
967 struct epoch_tracker et;
970 struct tcpcb *tp = NULL;
973 struct sockaddr_in sin;
975 struct sockaddr_in *sinp;
986 * We require the pcbinfo "read lock" if we will close the socket
987 * as part of this call.
991 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
993 vflagsav = inp->inp_vflag;
994 incflagsav = inp->inp_inc.inc_flags;
995 restoreflags = false;
996 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1000 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
1001 * for freeing memory.
1003 if (m && (flags & PRUS_NOTREADY) == 0)
1008 tp = intotcpcb(inp);
1010 if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
1011 switch (nam->sa_family) {
1014 sinp = (struct sockaddr_in *)nam;
1015 if (sinp->sin_len != sizeof(struct sockaddr_in)) {
1021 if ((inp->inp_vflag & INP_IPV6) != 0) {
1024 error = EAFNOSUPPORT;
1027 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
1030 error = EAFNOSUPPORT;
1033 if ((error = prison_remote_ip4(td->td_ucred,
1034 &sinp->sin_addr))) {
1047 struct sockaddr_in6 *sin6;
1049 sin6 = (struct sockaddr_in6 *)nam;
1050 if (sin6->sin6_len != sizeof(*sin6)) {
1056 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1059 error = EAFNOSUPPORT;
1062 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1064 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1070 if ((inp->inp_vflag & INP_IPV4) == 0) {
1071 error = EAFNOSUPPORT;
1076 restoreflags = true;
1077 inp->inp_vflag &= ~INP_IPV6;
1079 in6_sin6_2_sin(sinp, sin6);
1081 ntohl(sinp->sin_addr.s_addr))) {
1082 error = EAFNOSUPPORT;
1087 if ((error = prison_remote_ip4(td->td_ucred,
1088 &sinp->sin_addr))) {
1095 error = EAFNOSUPPORT;
1101 if ((inp->inp_vflag & INP_IPV6) == 0) {
1104 error = EAFNOSUPPORT;
1107 restoreflags = true;
1108 inp->inp_vflag &= ~INP_IPV4;
1109 inp->inp_inc.inc_flags |= INC_ISIPV6;
1110 if ((error = prison_remote_ip6(td->td_ucred,
1111 &sin6->sin6_addr))) {
1124 error = EAFNOSUPPORT;
1129 /* TCP doesn't do control messages (rights, creds, etc) */
1130 if (control->m_len) {
1137 m_freem(control); /* empty control, just free it */
1139 if (!(flags & PRUS_OOB)) {
1140 sbappendstream(&so->so_snd, m, flags);
1141 if (nam && tp->t_state < TCPS_SYN_SENT) {
1143 * Do implied connect if not yet connected,
1144 * initialize window to default value, and
1145 * initialize maxseg using peer's cached MSS.
1149 error = tcp6_connect(tp, nam, td);
1151 #if defined(INET6) && defined(INET)
1155 error = tcp_connect(tp,
1156 (struct sockaddr *)sinp, td);
1159 * The bind operation in tcp_connect succeeded. We
1160 * no longer want to restore the flags if later
1163 if (error == 0 || inp->inp_lport != 0)
1164 restoreflags = false;
1168 if (IS_FASTOPEN(tp->t_flags))
1169 tcp_fastopen_connect(tp);
1171 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1175 if (flags & PRUS_EOF) {
1177 * Close the send side of the connection after
1184 if (!(inp->inp_flags & INP_DROPPED) &&
1185 !(flags & PRUS_NOTREADY)) {
1186 if (flags & PRUS_MORETOCOME)
1187 tp->t_flags |= TF_MORETOCOME;
1188 error = tp->t_fb->tfb_tcp_output(tp);
1189 if (flags & PRUS_MORETOCOME)
1190 tp->t_flags &= ~TF_MORETOCOME;
1194 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1196 SOCKBUF_LOCK(&so->so_snd);
1197 if (sbspace(&so->so_snd) < -512) {
1198 SOCKBUF_UNLOCK(&so->so_snd);
1204 * According to RFC961 (Assigned Protocols),
1205 * the urgent pointer points to the last octet
1206 * of urgent data. We continue, however,
1207 * to consider it to indicate the first octet
1208 * of data past the urgent section.
1209 * Otherwise, snd_up should be one lower.
1211 sbappendstream_locked(&so->so_snd, m, flags);
1212 SOCKBUF_UNLOCK(&so->so_snd);
1213 if (nam && tp->t_state < TCPS_SYN_SENT) {
1215 * Do implied connect if not yet connected,
1216 * initialize window to default value, and
1217 * initialize maxseg using peer's cached MSS.
1221 * Not going to contemplate SYN|URG
1223 if (IS_FASTOPEN(tp->t_flags))
1224 tp->t_flags &= ~TF_FASTOPEN;
1227 error = tcp6_connect(tp, nam, td);
1229 #if defined(INET6) && defined(INET)
1233 error = tcp_connect(tp,
1234 (struct sockaddr *)sinp, td);
1237 * The bind operation in tcp_connect succeeded. We
1238 * no longer want to restore the flags if later
1241 if (error == 0 || inp->inp_lport != 0)
1242 restoreflags = false;
1246 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1249 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1250 if (!(flags & PRUS_NOTREADY)) {
1251 tp->t_flags |= TF_FORCEDATA;
1252 error = tp->t_fb->tfb_tcp_output(tp);
1253 tp->t_flags &= ~TF_FORCEDATA;
1256 TCP_LOG_EVENT(tp, NULL,
1257 &inp->inp_socket->so_rcv,
1258 &inp->inp_socket->so_snd,
1259 TCP_LOG_USERSEND, error,
1263 * If the request was unsuccessful and we changed flags,
1264 * restore the original flags.
1266 if (error != 0 && restoreflags) {
1267 inp->inp_vflag = vflagsav;
1268 inp->inp_inc.inc_flags = incflagsav;
1270 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
1271 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1272 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1273 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1280 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1282 struct epoch_tracker et;
1287 inp = sotoinpcb(so);
1289 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1291 mb_free_notready(m, count);
1292 return (ECONNRESET);
1294 tp = intotcpcb(inp);
1296 SOCKBUF_LOCK(&so->so_snd);
1297 error = sbready(&so->so_snd, m, count);
1298 SOCKBUF_UNLOCK(&so->so_snd);
1300 NET_EPOCH_ENTER(et);
1301 error = tp->t_fb->tfb_tcp_output(tp);
1310 * Abort the TCP. Drop the connection abruptly.
1313 tcp_usr_abort(struct socket *so)
1316 struct tcpcb *tp = NULL;
1317 struct epoch_tracker et;
1320 inp = sotoinpcb(so);
1321 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1323 NET_EPOCH_ENTER(et);
1325 KASSERT(inp->inp_socket != NULL,
1326 ("tcp_usr_abort: inp_socket == NULL"));
1329 * If we still have full TCP state, and we're not dropped, drop.
1331 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1332 !(inp->inp_flags & INP_DROPPED)) {
1333 tp = intotcpcb(inp);
1335 tp = tcp_drop(tp, ECONNABORTED);
1338 TCPDEBUG2(PRU_ABORT);
1339 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1341 if (!(inp->inp_flags & INP_DROPPED)) {
1343 so->so_state |= SS_PROTOREF;
1345 inp->inp_flags |= INP_SOCKREF;
1353 * TCP socket is closed. Start friendly disconnect.
1356 tcp_usr_close(struct socket *so)
1359 struct tcpcb *tp = NULL;
1360 struct epoch_tracker et;
1363 inp = sotoinpcb(so);
1364 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1366 NET_EPOCH_ENTER(et);
1368 KASSERT(inp->inp_socket != NULL,
1369 ("tcp_usr_close: inp_socket == NULL"));
1372 * If we still have full TCP state, and we're not dropped, initiate
1375 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1376 !(inp->inp_flags & INP_DROPPED)) {
1377 tp = intotcpcb(inp);
1380 TCPDEBUG2(PRU_CLOSE);
1381 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1383 if (!(inp->inp_flags & INP_DROPPED)) {
1385 so->so_state |= SS_PROTOREF;
1387 inp->inp_flags |= INP_SOCKREF;
1394 * Receive out-of-band data.
1397 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1401 struct tcpcb *tp = NULL;
1404 inp = sotoinpcb(so);
1405 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1407 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1411 tp = intotcpcb(inp);
1413 if ((so->so_oobmark == 0 &&
1414 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1415 so->so_options & SO_OOBINLINE ||
1416 tp->t_oobflags & TCPOOB_HADDATA) {
1420 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1421 error = EWOULDBLOCK;
1425 *mtod(m, caddr_t) = tp->t_iobc;
1426 if ((flags & MSG_PEEK) == 0)
1427 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1430 TCPDEBUG2(PRU_RCVOOB);
1431 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1437 struct pr_usrreqs tcp_usrreqs = {
1438 .pru_abort = tcp_usr_abort,
1439 .pru_accept = tcp_usr_accept,
1440 .pru_attach = tcp_usr_attach,
1441 .pru_bind = tcp_usr_bind,
1442 .pru_connect = tcp_usr_connect,
1443 .pru_control = in_control,
1444 .pru_detach = tcp_usr_detach,
1445 .pru_disconnect = tcp_usr_disconnect,
1446 .pru_listen = tcp_usr_listen,
1447 .pru_peeraddr = in_getpeeraddr,
1448 .pru_rcvd = tcp_usr_rcvd,
1449 .pru_rcvoob = tcp_usr_rcvoob,
1450 .pru_send = tcp_usr_send,
1451 .pru_ready = tcp_usr_ready,
1452 .pru_shutdown = tcp_usr_shutdown,
1453 .pru_sockaddr = in_getsockaddr,
1454 .pru_sosetlabel = in_pcbsosetlabel,
1455 .pru_close = tcp_usr_close,
1460 struct pr_usrreqs tcp6_usrreqs = {
1461 .pru_abort = tcp_usr_abort,
1462 .pru_accept = tcp6_usr_accept,
1463 .pru_attach = tcp_usr_attach,
1464 .pru_bind = tcp6_usr_bind,
1465 .pru_connect = tcp6_usr_connect,
1466 .pru_control = in6_control,
1467 .pru_detach = tcp_usr_detach,
1468 .pru_disconnect = tcp_usr_disconnect,
1469 .pru_listen = tcp6_usr_listen,
1470 .pru_peeraddr = in6_mapped_peeraddr,
1471 .pru_rcvd = tcp_usr_rcvd,
1472 .pru_rcvoob = tcp_usr_rcvoob,
1473 .pru_send = tcp_usr_send,
1474 .pru_ready = tcp_usr_ready,
1475 .pru_shutdown = tcp_usr_shutdown,
1476 .pru_sockaddr = in6_mapped_sockaddr,
1477 .pru_sosetlabel = in_pcbsosetlabel,
1478 .pru_close = tcp_usr_close,
1484 * Common subroutine to open a TCP connection to remote host specified
1485 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1486 * port number if needed. Call in_pcbconnect_setup to do the routing and
1487 * to choose a local host address (interface). If there is an existing
1488 * incarnation of the same connection in TIME-WAIT state and if the remote
1489 * host was sending CC options and if the connection duration was < MSL, then
1490 * truncate the previous TIME-WAIT state and proceed.
1491 * Initialize connection parameters and enter SYN-SENT state.
1494 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1496 struct inpcb *inp = tp->t_inpcb, *oinp;
1497 struct socket *so = inp->inp_socket;
1498 struct in_addr laddr;
1502 INP_WLOCK_ASSERT(inp);
1503 INP_HASH_WLOCK(&V_tcbinfo);
1505 if (inp->inp_lport == 0) {
1506 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1512 * Cannot simply call in_pcbconnect, because there might be an
1513 * earlier incarnation of this same connection still in
1514 * TIME_WAIT state, creating an ADDRINUSE error.
1516 laddr = inp->inp_laddr;
1517 lport = inp->inp_lport;
1518 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1519 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1520 if (error && oinp == NULL)
1526 inp->inp_laddr = laddr;
1528 INP_HASH_WUNLOCK(&V_tcbinfo);
1531 * Compute window scaling to request:
1532 * Scale to fit into sweet spot. See tcp_syncache.c.
1533 * XXX: This should move to tcp_output().
1535 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1536 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1537 tp->request_r_scale++;
1540 TCPSTAT_INC(tcps_connattempt);
1541 tcp_state_change(tp, TCPS_SYN_SENT);
1542 tp->iss = tcp_new_isn(&inp->inp_inc);
1543 if (tp->t_flags & TF_REQ_TSTMP)
1544 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1545 tcp_sendseqinit(tp);
1550 INP_HASH_WUNLOCK(&V_tcbinfo);
1557 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1559 struct inpcb *inp = tp->t_inpcb;
1562 INP_WLOCK_ASSERT(inp);
1563 INP_HASH_WLOCK(&V_tcbinfo);
1565 if (inp->inp_lport == 0) {
1566 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1570 error = in6_pcbconnect(inp, nam, td->td_ucred);
1573 INP_HASH_WUNLOCK(&V_tcbinfo);
1575 /* Compute window scaling to request. */
1576 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1577 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1578 tp->request_r_scale++;
1580 soisconnecting(inp->inp_socket);
1581 TCPSTAT_INC(tcps_connattempt);
1582 tcp_state_change(tp, TCPS_SYN_SENT);
1583 tp->iss = tcp_new_isn(&inp->inp_inc);
1584 if (tp->t_flags & TF_REQ_TSTMP)
1585 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1586 tcp_sendseqinit(tp);
1591 INP_HASH_WUNLOCK(&V_tcbinfo);
1597 * Export TCP internal state information via a struct tcp_info, based on the
1598 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1599 * (TCP state machine, etc). We export all information using FreeBSD-native
1600 * constants -- for example, the numeric values for tcpi_state will differ
1604 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1607 INP_WLOCK_ASSERT(tp->t_inpcb);
1608 bzero(ti, sizeof(*ti));
1610 ti->tcpi_state = tp->t_state;
1611 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1612 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1613 if (tp->t_flags & TF_SACK_PERMIT)
1614 ti->tcpi_options |= TCPI_OPT_SACK;
1615 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1616 ti->tcpi_options |= TCPI_OPT_WSCALE;
1617 ti->tcpi_snd_wscale = tp->snd_scale;
1618 ti->tcpi_rcv_wscale = tp->rcv_scale;
1620 if (tp->t_flags2 & TF2_ECN_PERMIT)
1621 ti->tcpi_options |= TCPI_OPT_ECN;
1623 ti->tcpi_rto = tp->t_rxtcur * tick;
1624 ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1625 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1626 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1628 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1629 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1632 * FreeBSD-specific extension fields for tcp_info.
1634 ti->tcpi_rcv_space = tp->rcv_wnd;
1635 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1636 ti->tcpi_snd_wnd = tp->snd_wnd;
1637 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1638 ti->tcpi_snd_nxt = tp->snd_nxt;
1639 ti->tcpi_snd_mss = tp->t_maxseg;
1640 ti->tcpi_rcv_mss = tp->t_maxseg;
1641 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1642 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1643 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1645 if (tp->t_flags & TF_TOE) {
1646 ti->tcpi_options |= TCPI_OPT_TOE;
1647 tcp_offload_tcp_info(tp, ti);
1653 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1654 * socket option arguments. When it re-acquires the lock after the copy, it
1655 * has to revalidate that the connection is still valid for the socket
1658 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \
1660 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1663 return (ECONNRESET); \
1665 tp = intotcpcb(inp); \
1667 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1670 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1675 struct tcp_function_block *blk;
1676 struct tcp_function_set fsn;
1679 inp = sotoinpcb(so);
1680 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1681 if (sopt->sopt_level != IPPROTO_TCP) {
1683 if (inp->inp_vflag & INP_IPV6PROTO) {
1684 error = ip6_ctloutput(so, sopt);
1686 * In case of the IPV6_USE_MIN_MTU socket option,
1687 * the INC_IPV6MINMTU flag to announce a corresponding
1688 * MSS during the initial handshake.
1689 * If the TCP connection is not in the front states,
1690 * just reduce the MSS being used.
1691 * This avoids the sending of TCP segments which will
1692 * be fragmented at the IPv6 layer.
1695 (sopt->sopt_dir == SOPT_SET) &&
1696 (sopt->sopt_level == IPPROTO_IPV6) &&
1697 (sopt->sopt_name == IPV6_USE_MIN_MTU)) {
1699 if ((inp->inp_flags &
1700 (INP_TIMEWAIT | INP_DROPPED))) {
1702 return (ECONNRESET);
1704 inp->inp_inc.inc_flags |= INC_IPV6MINMTU;
1705 tp = intotcpcb(inp);
1706 if ((tp->t_state >= TCPS_SYN_SENT) &&
1707 (inp->inp_inc.inc_flags & INC_ISIPV6)) {
1708 struct ip6_pktopts *opt;
1710 opt = inp->in6p_outputopts;
1711 if ((opt != NULL) &&
1712 (opt->ip6po_minmtu ==
1713 IP6PO_MINMTU_ALL)) {
1714 if (tp->t_maxseg > TCP6_MSS) {
1715 tp->t_maxseg = TCP6_MSS;
1723 #if defined(INET6) && defined(INET)
1728 error = ip_ctloutput(so, sopt);
1734 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1736 return (ECONNRESET);
1738 tp = intotcpcb(inp);
1740 * Protect the TCP option TCP_FUNCTION_BLK so
1741 * that a sub-function can *never* overwrite this.
1743 if ((sopt->sopt_dir == SOPT_SET) &&
1744 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1746 error = sooptcopyin(sopt, &fsn, sizeof fsn,
1750 INP_WLOCK_RECHECK(inp);
1751 blk = find_and_ref_tcp_functions(&fsn);
1756 if (tp->t_fb == blk) {
1757 /* You already have this */
1758 refcount_release(&blk->tfb_refcnt);
1762 if (tp->t_state != TCPS_CLOSED) {
1764 * The user has advanced the state
1765 * past the initial point, we may not
1766 * be able to switch.
1768 if (blk->tfb_tcp_handoff_ok != NULL) {
1770 * Does the stack provide a
1771 * query mechanism, if so it may
1772 * still be possible?
1774 error = (*blk->tfb_tcp_handoff_ok)(tp);
1778 refcount_release(&blk->tfb_refcnt);
1783 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1784 refcount_release(&blk->tfb_refcnt);
1789 * Release the old refcnt, the
1790 * lookup acquired a ref on the
1793 if (tp->t_fb->tfb_tcp_fb_fini) {
1795 * Tell the stack to cleanup with 0 i.e.
1796 * the tcb is not going away.
1798 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1801 /* Assure that we are not on any hpts */
1802 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
1804 if (blk->tfb_tcp_fb_init) {
1805 error = (*blk->tfb_tcp_fb_init)(tp);
1807 refcount_release(&blk->tfb_refcnt);
1808 if (tp->t_fb->tfb_tcp_fb_init) {
1809 if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) {
1810 /* Fall back failed, drop the connection */
1819 refcount_release(&tp->t_fb->tfb_refcnt);
1822 if (tp->t_flags & TF_TOE) {
1823 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1830 } else if ((sopt->sopt_dir == SOPT_GET) &&
1831 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1832 strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1833 TCP_FUNCTION_NAME_LEN_MAX);
1834 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1835 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1837 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1840 /* Pass in the INP locked, called must unlock it */
1841 return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
1845 * If this assert becomes untrue, we need to change the size of the buf
1846 * variable in tcp_default_ctloutput().
1849 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1850 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1854 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
1856 int error, opt, optval;
1860 struct tls_enable tls;
1862 struct cc_algo *algo;
1863 char *pbuf, buf[TCP_LOG_ID_LEN];
1865 struct statsblob *sbp;
1870 * For TCP_CCALGOOPT forward the control to CC module, for both
1871 * SOPT_SET and SOPT_GET.
1873 switch (sopt->sopt_name) {
1876 if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
1878 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1879 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1880 sopt->sopt_valsize);
1885 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1886 if (CC_ALGO(tp)->ctl_output != NULL)
1887 error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1891 if (error == 0 && sopt->sopt_dir == SOPT_GET)
1892 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1897 switch (sopt->sopt_dir) {
1899 switch (sopt->sopt_name) {
1900 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1902 if (!TCPMD5_ENABLED()) {
1904 return (ENOPROTOOPT);
1906 error = TCPMD5_PCBCTL(inp, sopt);
1909 goto unlock_and_done;
1915 error = sooptcopyin(sopt, &optval, sizeof optval,
1920 INP_WLOCK_RECHECK(inp);
1921 switch (sopt->sopt_name) {
1929 opt = 0; /* dead code to fool gcc */
1936 tp->t_flags &= ~opt;
1939 if (tp->t_flags & TF_TOE) {
1940 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1949 error = sooptcopyin(sopt, &optval, sizeof optval,
1954 INP_WLOCK_RECHECK(inp);
1956 tp->t_flags |= TF_NOPUSH;
1957 else if (tp->t_flags & TF_NOPUSH) {
1958 tp->t_flags &= ~TF_NOPUSH;
1959 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
1960 struct epoch_tracker et;
1962 NET_EPOCH_ENTER(et);
1963 error = tp->t_fb->tfb_tcp_output(tp);
1967 goto unlock_and_done;
1971 error = sooptcopyin(sopt, &optval, sizeof optval,
1976 INP_WLOCK_RECHECK(inp);
1977 if (optval > 0 && optval <= tp->t_maxseg &&
1978 optval + 40 >= V_tcp_minmss)
1979 tp->t_maxseg = optval;
1982 goto unlock_and_done;
1992 error = sooptcopyin(sopt, &optval, sizeof optval,
1998 sbp = stats_blob_alloc(
1999 V_tcp_perconn_stats_dflt_tpl, 0);
2003 INP_WLOCK_RECHECK(inp);
2004 if ((tp->t_stats != NULL && sbp == NULL) ||
2005 (tp->t_stats == NULL && sbp != NULL)) {
2006 struct statsblob *t = tp->t_stats;
2012 stats_blob_destroy(sbp);
2014 return (EOPNOTSUPP);
2018 case TCP_CONGESTION:
2020 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
2023 buf[sopt->sopt_valsize] = '\0';
2024 INP_WLOCK_RECHECK(inp);
2026 STAILQ_FOREACH(algo, &cc_list, entries)
2027 if (strncmp(buf, algo->name,
2028 TCP_CA_NAME_MAX) == 0)
2037 * We hold a write lock over the tcb so it's safe to
2038 * do these things without ordering concerns.
2040 if (CC_ALGO(tp)->cb_destroy != NULL)
2041 CC_ALGO(tp)->cb_destroy(tp->ccv);
2045 * If something goes pear shaped initialising the new
2046 * algo, fall back to newreno (which does not
2047 * require initialisation).
2049 if (algo->cb_init != NULL &&
2050 algo->cb_init(tp->ccv) != 0) {
2051 CC_ALGO(tp) = &newreno_cc_algo;
2053 * The only reason init should fail is
2054 * because of malloc.
2062 case TCP_TXTLS_ENABLE:
2064 error = sooptcopyin(sopt, &tls, sizeof(tls),
2068 error = ktls_enable_tx(so, &tls);
2070 case TCP_TXTLS_MODE:
2072 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2076 INP_WLOCK_RECHECK(inp);
2077 error = ktls_set_tx_mode(so, ui);
2086 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2090 if (ui > (UINT_MAX / hz)) {
2096 INP_WLOCK_RECHECK(inp);
2097 switch (sopt->sopt_name) {
2099 tp->t_keepidle = ui;
2101 * XXX: better check current remaining
2102 * timeout and "merge" it with new value.
2104 if ((tp->t_state > TCPS_LISTEN) &&
2105 (tp->t_state <= TCPS_CLOSING))
2106 tcp_timer_activate(tp, TT_KEEP,
2110 tp->t_keepintvl = ui;
2111 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2112 (TP_MAXIDLE(tp) > 0))
2113 tcp_timer_activate(tp, TT_2MSL,
2117 tp->t_keepinit = ui;
2118 if (tp->t_state == TCPS_SYN_RECEIVED ||
2119 tp->t_state == TCPS_SYN_SENT)
2120 tcp_timer_activate(tp, TT_KEEP,
2124 goto unlock_and_done;
2128 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2132 INP_WLOCK_RECHECK(inp);
2134 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2135 (TP_MAXIDLE(tp) > 0))
2136 tcp_timer_activate(tp, TT_2MSL,
2138 goto unlock_and_done;
2144 error = sooptcopyin(sopt, &optval, sizeof optval,
2149 INP_WLOCK_RECHECK(inp);
2151 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
2152 &(tp->t_outpkts) : &(tp->t_inpkts),
2156 goto unlock_and_done;
2159 case TCP_FASTOPEN: {
2160 struct tcp_fastopen tfo_optval;
2163 if (!V_tcp_fastopen_client_enable &&
2164 !V_tcp_fastopen_server_enable)
2167 error = sooptcopyin(sopt, &tfo_optval,
2168 sizeof(tfo_optval), sizeof(int));
2172 INP_WLOCK_RECHECK(inp);
2173 if (tfo_optval.enable) {
2174 if (tp->t_state == TCPS_LISTEN) {
2175 if (!V_tcp_fastopen_server_enable) {
2177 goto unlock_and_done;
2180 tp->t_flags |= TF_FASTOPEN;
2181 if (tp->t_tfo_pending == NULL)
2183 tcp_fastopen_alloc_counter();
2186 * If a pre-shared key was provided,
2187 * stash it in the client cookie
2188 * field of the tcpcb for use during
2191 if (sopt->sopt_valsize ==
2192 sizeof(tfo_optval)) {
2193 memcpy(tp->t_tfo_cookie.client,
2195 TCP_FASTOPEN_PSK_LEN);
2196 tp->t_tfo_client_cookie_len =
2197 TCP_FASTOPEN_PSK_LEN;
2199 tp->t_flags |= TF_FASTOPEN;
2202 tp->t_flags &= ~TF_FASTOPEN;
2203 goto unlock_and_done;
2209 error = sooptcopyin(sopt, &optval, sizeof optval,
2214 INP_WLOCK_RECHECK(inp);
2215 error = tcp_log_state_change(tp, optval);
2216 goto unlock_and_done;
2225 error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
2228 buf[sopt->sopt_valsize] = '\0';
2229 INP_WLOCK_RECHECK(inp);
2230 error = tcp_log_set_id(tp, buf);
2231 /* tcp_log_set_id() unlocks the INP. */
2238 sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
2241 buf[sopt->sopt_valsize] = '\0';
2242 INP_WLOCK_RECHECK(inp);
2243 if (sopt->sopt_name == TCP_LOGDUMP) {
2244 error = tcp_log_dump_tp_logbuf(tp, buf,
2248 tcp_log_dump_tp_bucket_logbufs(tp, buf);
2250 * tcp_log_dump_tp_bucket_logbufs() drops the
2259 error = ENOPROTOOPT;
2265 tp = intotcpcb(inp);
2266 switch (sopt->sopt_name) {
2267 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2269 if (!TCPMD5_ENABLED()) {
2271 return (ENOPROTOOPT);
2273 error = TCPMD5_PCBCTL(inp, sopt);
2278 optval = tp->t_flags & TF_NODELAY;
2280 error = sooptcopyout(sopt, &optval, sizeof optval);
2283 optval = tp->t_maxseg;
2285 error = sooptcopyout(sopt, &optval, sizeof optval);
2288 optval = tp->t_flags & TF_NOOPT;
2290 error = sooptcopyout(sopt, &optval, sizeof optval);
2293 optval = tp->t_flags & TF_NOPUSH;
2295 error = sooptcopyout(sopt, &optval, sizeof optval);
2298 tcp_fill_info(tp, &ti);
2300 error = sooptcopyout(sopt, &ti, sizeof ti);
2306 TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2309 socklen_t outsbsz = sopt->sopt_valsize;
2310 if (tp->t_stats == NULL)
2312 else if (outsbsz >= tp->t_stats->cursz)
2313 outsbsz = tp->t_stats->cursz;
2314 else if (outsbsz >= sizeof(struct statsblob))
2315 outsbsz = sizeof(struct statsblob);
2322 sbp = sopt->sopt_val;
2323 nheld = atop(round_page(((vm_offset_t)sbp) +
2324 (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2325 vm_page_t ma[nheld];
2326 if (vm_fault_quick_hold_pages(
2327 &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2328 outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2334 if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2335 SIZEOF_MEMBER(struct statsblob, flags))))
2338 INP_WLOCK_RECHECK(inp);
2339 error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2340 sbflags | SB_CLONE_USRDSTNOFAULT);
2342 sopt->sopt_valsize = outsbsz;
2344 vm_page_unhold_pages(ma, nheld);
2351 case TCP_CONGESTION:
2352 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2354 error = sooptcopyout(sopt, buf, len + 1);
2360 switch (sopt->sopt_name) {
2362 ui = TP_KEEPIDLE(tp) / hz;
2365 ui = TP_KEEPINTVL(tp) / hz;
2368 ui = TP_KEEPINIT(tp) / hz;
2371 ui = TP_KEEPCNT(tp);
2375 error = sooptcopyout(sopt, &ui, sizeof(ui));
2380 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
2381 &(tp->t_outpkts) : &(tp->t_inpkts));
2383 error = sooptcopyout(sopt, &optval, sizeof optval);
2387 optval = tp->t_flags & TF_FASTOPEN;
2389 error = sooptcopyout(sopt, &optval, sizeof optval);
2393 optval = tp->t_logstate;
2395 error = sooptcopyout(sopt, &optval, sizeof(optval));
2398 /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2399 error = tcp_log_getlogbuf(sopt, tp);
2402 len = tcp_log_get_id(tp, buf);
2404 error = sooptcopyout(sopt, buf, len + 1);
2413 case TCP_TXTLS_MODE:
2414 optval = ktls_get_tx_mode(so);
2416 error = sooptcopyout(sopt, &optval, sizeof(optval));
2421 error = ENOPROTOOPT;
2428 #undef INP_WLOCK_RECHECK
2429 #undef INP_WLOCK_RECHECK_CLEANUP
2432 * Initiate (or continue) disconnect.
2433 * If embryonic state, just send reset (once).
2434 * If in ``let data drain'' option and linger null, just drop.
2435 * Otherwise (hard), mark socket disconnecting and drop
2436 * current input data; switch states based on user close, and
2437 * send segment to peer (with FIN).
2440 tcp_disconnect(struct tcpcb *tp)
2442 struct inpcb *inp = tp->t_inpcb;
2443 struct socket *so = inp->inp_socket;
2446 INP_WLOCK_ASSERT(inp);
2449 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2450 * socket is still open.
2452 if (tp->t_state < TCPS_ESTABLISHED &&
2453 !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2456 ("tcp_disconnect: tcp_close() returned NULL"));
2457 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2458 tp = tcp_drop(tp, 0);
2460 ("tcp_disconnect: tcp_drop() returned NULL"));
2462 soisdisconnecting(so);
2463 sbflush(&so->so_rcv);
2465 if (!(inp->inp_flags & INP_DROPPED))
2466 tp->t_fb->tfb_tcp_output(tp);
2471 * User issued close, and wish to trail through shutdown states:
2472 * if never received SYN, just forget it. If got a SYN from peer,
2473 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2474 * If already got a FIN from peer, then almost done; go to LAST_ACK
2475 * state. In all other cases, have already sent FIN to peer (e.g.
2476 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2477 * for peer to send FIN or not respond to keep-alives, etc.
2478 * We can let the user exit from the close as soon as the FIN is acked.
2481 tcp_usrclosed(struct tcpcb *tp)
2485 INP_WLOCK_ASSERT(tp->t_inpcb);
2487 switch (tp->t_state) {
2490 tcp_offload_listen_stop(tp);
2492 tcp_state_change(tp, TCPS_CLOSED);
2497 * tcp_close() should never return NULL here as the socket is
2501 ("tcp_usrclosed: tcp_close() returned NULL"));
2505 case TCPS_SYN_RECEIVED:
2506 tp->t_flags |= TF_NEEDFIN;
2509 case TCPS_ESTABLISHED:
2510 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2513 case TCPS_CLOSE_WAIT:
2514 tcp_state_change(tp, TCPS_LAST_ACK);
2517 if (tp->t_state >= TCPS_FIN_WAIT_2) {
2518 soisdisconnected(tp->t_inpcb->inp_socket);
2519 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
2520 if (tp->t_state == TCPS_FIN_WAIT_2) {
2523 timeout = (tcp_fast_finwait2_recycle) ?
2524 tcp_finwait2_timeout : TP_MAXIDLE(tp);
2525 tcp_timer_activate(tp, TT_2MSL, timeout);
2532 db_print_indent(int indent)
2536 for (i = 0; i < indent; i++)
2541 db_print_tstate(int t_state)
2546 db_printf("TCPS_CLOSED");
2550 db_printf("TCPS_LISTEN");
2554 db_printf("TCPS_SYN_SENT");
2557 case TCPS_SYN_RECEIVED:
2558 db_printf("TCPS_SYN_RECEIVED");
2561 case TCPS_ESTABLISHED:
2562 db_printf("TCPS_ESTABLISHED");
2565 case TCPS_CLOSE_WAIT:
2566 db_printf("TCPS_CLOSE_WAIT");
2569 case TCPS_FIN_WAIT_1:
2570 db_printf("TCPS_FIN_WAIT_1");
2574 db_printf("TCPS_CLOSING");
2578 db_printf("TCPS_LAST_ACK");
2581 case TCPS_FIN_WAIT_2:
2582 db_printf("TCPS_FIN_WAIT_2");
2585 case TCPS_TIME_WAIT:
2586 db_printf("TCPS_TIME_WAIT");
2590 db_printf("unknown");
2596 db_print_tflags(u_int t_flags)
2601 if (t_flags & TF_ACKNOW) {
2602 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2605 if (t_flags & TF_DELACK) {
2606 db_printf("%sTF_DELACK", comma ? ", " : "");
2609 if (t_flags & TF_NODELAY) {
2610 db_printf("%sTF_NODELAY", comma ? ", " : "");
2613 if (t_flags & TF_NOOPT) {
2614 db_printf("%sTF_NOOPT", comma ? ", " : "");
2617 if (t_flags & TF_SENTFIN) {
2618 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2621 if (t_flags & TF_REQ_SCALE) {
2622 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2625 if (t_flags & TF_RCVD_SCALE) {
2626 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2629 if (t_flags & TF_REQ_TSTMP) {
2630 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2633 if (t_flags & TF_RCVD_TSTMP) {
2634 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2637 if (t_flags & TF_SACK_PERMIT) {
2638 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2641 if (t_flags & TF_NEEDSYN) {
2642 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2645 if (t_flags & TF_NEEDFIN) {
2646 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2649 if (t_flags & TF_NOPUSH) {
2650 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2653 if (t_flags & TF_MORETOCOME) {
2654 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2657 if (t_flags & TF_LQ_OVERFLOW) {
2658 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2661 if (t_flags & TF_LASTIDLE) {
2662 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2665 if (t_flags & TF_RXWIN0SENT) {
2666 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2669 if (t_flags & TF_FASTRECOVERY) {
2670 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2673 if (t_flags & TF_CONGRECOVERY) {
2674 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2677 if (t_flags & TF_WASFRECOVERY) {
2678 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2681 if (t_flags & TF_SIGNATURE) {
2682 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2685 if (t_flags & TF_FORCEDATA) {
2686 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2689 if (t_flags & TF_TSO) {
2690 db_printf("%sTF_TSO", comma ? ", " : "");
2693 if (t_flags & TF_FASTOPEN) {
2694 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2700 db_print_tflags2(u_int t_flags2)
2705 if (t_flags2 & TF2_ECN_PERMIT) {
2706 db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
2713 db_print_toobflags(char t_oobflags)
2718 if (t_oobflags & TCPOOB_HAVEDATA) {
2719 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2722 if (t_oobflags & TCPOOB_HADDATA) {
2723 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2729 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2732 db_print_indent(indent);
2733 db_printf("%s at %p\n", name, tp);
2737 db_print_indent(indent);
2738 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2739 TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2741 db_print_indent(indent);
2742 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
2743 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2745 db_print_indent(indent);
2746 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2747 &tp->t_timers->tt_delack, tp->t_inpcb);
2749 db_print_indent(indent);
2750 db_printf("t_state: %d (", tp->t_state);
2751 db_print_tstate(tp->t_state);
2754 db_print_indent(indent);
2755 db_printf("t_flags: 0x%x (", tp->t_flags);
2756 db_print_tflags(tp->t_flags);
2759 db_print_indent(indent);
2760 db_printf("t_flags2: 0x%x (", tp->t_flags2);
2761 db_print_tflags2(tp->t_flags2);
2764 db_print_indent(indent);
2765 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2766 tp->snd_una, tp->snd_max, tp->snd_nxt);
2768 db_print_indent(indent);
2769 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2770 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2772 db_print_indent(indent);
2773 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2774 tp->iss, tp->irs, tp->rcv_nxt);
2776 db_print_indent(indent);
2777 db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n",
2778 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2780 db_print_indent(indent);
2781 db_printf("snd_wnd: %u snd_cwnd: %u\n",
2782 tp->snd_wnd, tp->snd_cwnd);
2784 db_print_indent(indent);
2785 db_printf("snd_ssthresh: %u snd_recover: "
2786 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2788 db_print_indent(indent);
2789 db_printf("t_rcvtime: %u t_startime: %u\n",
2790 tp->t_rcvtime, tp->t_starttime);
2792 db_print_indent(indent);
2793 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2794 tp->t_rtttime, tp->t_rtseq);
2796 db_print_indent(indent);
2797 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2798 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2800 db_print_indent(indent);
2801 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2802 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2805 db_print_indent(indent);
2806 db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n",
2807 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2809 db_print_indent(indent);
2810 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2811 db_print_toobflags(tp->t_oobflags);
2812 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2814 db_print_indent(indent);
2815 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2816 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2818 db_print_indent(indent);
2819 db_printf("ts_recent: %u ts_recent_age: %u\n",
2820 tp->ts_recent, tp->ts_recent_age);
2822 db_print_indent(indent);
2823 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2824 "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2826 db_print_indent(indent);
2827 db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x "
2828 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2829 tp->snd_recover_prev, tp->t_badrxtwin);
2831 db_print_indent(indent);
2832 db_printf("snd_numholes: %d snd_holes first: %p\n",
2833 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2835 db_print_indent(indent);
2836 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
2837 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2839 /* Skip sackblks, sackhint. */
2841 db_print_indent(indent);
2842 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2843 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2846 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2851 db_printf("usage: show tcpcb <addr>\n");
2854 tp = (struct tcpcb *)addr;
2856 db_print_tcpcb(tp, "tcpcb", 0);