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.
124 static int tcp_attach(struct socket *);
126 static int tcp_connect(struct tcpcb *, struct sockaddr *,
130 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
133 static void tcp_disconnect(struct tcpcb *);
134 static void tcp_usrclosed(struct tcpcb *);
135 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
138 #define TCPDEBUG0 int ostate = 0
139 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
140 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
141 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
145 #define TCPDEBUG2(req)
149 * TCP attaches to socket via pru_attach(), reserving space,
150 * and an internet control block.
153 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
156 struct tcpcb *tp = NULL;
161 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
164 error = tcp_attach(so);
168 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
169 so->so_linger = TCP_LINGERTIME;
174 TCPDEBUG2(PRU_ATTACH);
175 TCP_PROBE2(debug__user, tp, PRU_ATTACH);
180 * tcp_detach is called when the socket layer loses its final reference
181 * to the socket, be it a file descriptor reference, a reference from TCP,
182 * etc. At this point, there is only one case in which we will keep around
183 * inpcb state: time wait.
185 * This function can probably be re-absorbed back into tcp_usr_detach() now
186 * that there is a single detach path.
189 tcp_detach(struct socket *so, struct inpcb *inp)
193 INP_INFO_LOCK_ASSERT(&V_tcbinfo);
194 INP_WLOCK_ASSERT(inp);
196 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
197 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
201 if (inp->inp_flags & INP_TIMEWAIT) {
203 * There are two cases to handle: one in which the time wait
204 * state is being discarded (INP_DROPPED), and one in which
205 * this connection will remain in timewait. In the former,
206 * it is time to discard all state (except tcptw, which has
207 * already been discarded by the timewait close code, which
208 * should be further up the call stack somewhere). In the
209 * latter case, we detach from the socket, but leave the pcb
210 * present until timewait ends.
212 * XXXRW: Would it be cleaner to free the tcptw here?
214 * Astute question indeed, from twtcp perspective there are
215 * four cases to consider:
217 * #1 tcp_detach is called at tcptw creation time by
218 * tcp_twstart, then do not discard the newly created tcptw
219 * and leave inpcb present until timewait ends
220 * #2 tcp_detach is called at tcptw creation time by
221 * tcp_twstart, but connection is local and tw will be
222 * discarded immediately
223 * #3 tcp_detach is called at timewait end (or reuse) by
224 * tcp_twclose, then the tcptw has already been discarded
225 * (or reused) and inpcb is freed here
226 * #4 tcp_detach is called() after timewait ends (or reuse)
227 * (e.g. by soclose), then tcptw has already been discarded
228 * (or reused) and inpcb is freed here
230 * In all three cases the tcptw should not be freed here.
232 if (inp->inp_flags & INP_DROPPED) {
234 if (__predict_true(tp == NULL)) {
238 * This case should not happen as in TIMEWAIT
239 * state the inp should not be destroyed before
240 * its tcptw. If INVARIANTS is defined, panic.
243 panic("%s: Panic before an inp double-free: "
244 "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
247 log(LOG_ERR, "%s: Avoid an inp double-free: "
248 "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
259 * If the connection is not in timewait, we consider two
260 * two conditions: one in which no further processing is
261 * necessary (dropped || embryonic), and one in which TCP is
262 * not yet done, but no longer requires the socket, so the
263 * pcb will persist for the time being.
265 * XXXRW: Does the second case still occur?
267 if (inp->inp_flags & INP_DROPPED ||
268 tp->t_state < TCPS_SYN_SENT) {
280 * pru_detach() detaches the TCP protocol from the socket.
281 * If the protocol state is non-embryonic, then can't
282 * do this directly: have to initiate a pru_disconnect(),
283 * which may finish later; embryonic TCB's can just
287 tcp_usr_detach(struct socket *so)
291 struct epoch_tracker et;
294 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
295 if (!INP_INFO_WLOCKED(&V_tcbinfo)) {
300 KASSERT(inp->inp_socket != NULL,
301 ("tcp_usr_detach: inp_socket == NULL"));
309 * Give the socket an address.
312 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
316 struct tcpcb *tp = NULL;
317 struct sockaddr_in *sinp;
319 sinp = (struct sockaddr_in *)nam;
320 if (nam->sa_len != sizeof (*sinp))
323 * Must check for multicast addresses and disallow binding
326 if (sinp->sin_family == AF_INET &&
327 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
328 return (EAFNOSUPPORT);
332 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
334 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
340 INP_HASH_WLOCK(&V_tcbinfo);
341 error = in_pcbbind(inp, nam, td->td_ucred);
342 INP_HASH_WUNLOCK(&V_tcbinfo);
345 TCP_PROBE2(debug__user, tp, PRU_BIND);
354 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
358 struct tcpcb *tp = NULL;
359 struct sockaddr_in6 *sin6;
362 sin6 = (struct sockaddr_in6 *)nam;
363 if (nam->sa_len != sizeof (*sin6))
366 * Must check for multicast addresses and disallow binding
369 if (sin6->sin6_family == AF_INET6 &&
370 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
371 return (EAFNOSUPPORT);
375 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
377 vflagsav = inp->inp_vflag;
378 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
384 INP_HASH_WLOCK(&V_tcbinfo);
385 inp->inp_vflag &= ~INP_IPV4;
386 inp->inp_vflag |= INP_IPV6;
388 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
389 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
390 inp->inp_vflag |= INP_IPV4;
391 else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
392 struct sockaddr_in sin;
394 in6_sin6_2_sin(&sin, sin6);
395 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
396 error = EAFNOSUPPORT;
397 INP_HASH_WUNLOCK(&V_tcbinfo);
400 inp->inp_vflag |= INP_IPV4;
401 inp->inp_vflag &= ~INP_IPV6;
402 error = in_pcbbind(inp, (struct sockaddr *)&sin,
404 INP_HASH_WUNLOCK(&V_tcbinfo);
409 error = in6_pcbbind(inp, nam, td->td_ucred);
410 INP_HASH_WUNLOCK(&V_tcbinfo);
413 inp->inp_vflag = vflagsav;
415 TCP_PROBE2(debug__user, tp, PRU_BIND);
423 * Prepare to accept connections.
426 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
430 struct tcpcb *tp = NULL;
434 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
436 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
443 error = solisten_proto_check(so);
444 INP_HASH_WLOCK(&V_tcbinfo);
445 if (error == 0 && inp->inp_lport == 0)
446 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
447 INP_HASH_WUNLOCK(&V_tcbinfo);
449 tcp_state_change(tp, TCPS_LISTEN);
450 solisten_proto(so, backlog);
452 if ((so->so_options & SO_NO_OFFLOAD) == 0)
453 tcp_offload_listen_start(tp);
458 if (IS_FASTOPEN(tp->t_flags))
459 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
462 TCPDEBUG2(PRU_LISTEN);
463 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
471 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
475 struct tcpcb *tp = NULL;
480 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
482 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
486 vflagsav = inp->inp_vflag;
490 error = solisten_proto_check(so);
491 INP_HASH_WLOCK(&V_tcbinfo);
492 if (error == 0 && inp->inp_lport == 0) {
493 inp->inp_vflag &= ~INP_IPV4;
494 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
495 inp->inp_vflag |= INP_IPV4;
496 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
498 INP_HASH_WUNLOCK(&V_tcbinfo);
500 tcp_state_change(tp, TCPS_LISTEN);
501 solisten_proto(so, backlog);
503 if ((so->so_options & SO_NO_OFFLOAD) == 0)
504 tcp_offload_listen_start(tp);
509 if (IS_FASTOPEN(tp->t_flags))
510 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
513 inp->inp_vflag = vflagsav;
516 TCPDEBUG2(PRU_LISTEN);
517 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
525 * Initiate connection to peer.
526 * Create a template for use in transmissions on this connection.
527 * Enter SYN_SENT state, and mark socket as connecting.
528 * Start keep-alive timer, and seed output sequence space.
529 * Send initial segment on connection.
532 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
536 struct tcpcb *tp = NULL;
537 struct sockaddr_in *sinp;
539 sinp = (struct sockaddr_in *)nam;
540 if (nam->sa_len != sizeof (*sinp))
543 * Must disallow TCP ``connections'' to multicast addresses.
545 if (sinp->sin_family == AF_INET
546 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
547 return (EAFNOSUPPORT);
548 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
553 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
555 if (inp->inp_flags & INP_TIMEWAIT) {
559 if (inp->inp_flags & INP_DROPPED) {
560 error = ECONNREFUSED;
565 if ((error = tcp_connect(tp, nam, td)) != 0)
568 if (registered_toedevs > 0 &&
569 (so->so_options & SO_NO_OFFLOAD) == 0 &&
570 (error = tcp_offload_connect(so, nam)) == 0)
573 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
574 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)
589 struct tcpcb *tp = NULL;
590 struct sockaddr_in6 *sin6;
596 sin6 = (struct sockaddr_in6 *)nam;
597 if (nam->sa_len != sizeof (*sin6))
600 * Must disallow TCP ``connections'' to multicast addresses.
602 if (sin6->sin6_family == AF_INET6
603 && IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
604 return (EAFNOSUPPORT);
607 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
609 vflagsav = inp->inp_vflag;
610 incflagsav = inp->inp_inc.inc_flags;
611 if (inp->inp_flags & INP_TIMEWAIT) {
615 if (inp->inp_flags & INP_DROPPED) {
616 error = ECONNREFUSED;
623 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
624 * therefore probably require the hash lock, which isn't held here.
625 * Is this a significant problem?
627 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
628 struct sockaddr_in sin;
630 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
634 if ((inp->inp_vflag & INP_IPV4) == 0) {
635 error = EAFNOSUPPORT;
639 in6_sin6_2_sin(&sin, sin6);
640 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
641 error = EAFNOSUPPORT;
644 if ((error = prison_remote_ip4(td->td_ucred,
645 &sin.sin_addr)) != 0)
647 inp->inp_vflag |= INP_IPV4;
648 inp->inp_vflag &= ~INP_IPV6;
649 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
652 if (registered_toedevs > 0 &&
653 (so->so_options & SO_NO_OFFLOAD) == 0 &&
654 (error = tcp_offload_connect(so, nam)) == 0)
657 error = tp->t_fb->tfb_tcp_output(tp);
660 if ((inp->inp_vflag & INP_IPV6) == 0) {
661 error = EAFNOSUPPORT;
666 if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
668 inp->inp_vflag &= ~INP_IPV4;
669 inp->inp_vflag |= INP_IPV6;
670 inp->inp_inc.inc_flags |= INC_ISIPV6;
671 if ((error = tcp6_connect(tp, nam, td)) != 0)
674 if (registered_toedevs > 0 &&
675 (so->so_options & SO_NO_OFFLOAD) == 0 &&
676 (error = tcp_offload_connect(so, nam)) == 0)
679 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
680 error = tp->t_fb->tfb_tcp_output(tp);
684 * If the implicit bind in the connect call fails, restore
685 * the flags we modified.
687 if (error != 0 && inp->inp_lport == 0) {
688 inp->inp_vflag = vflagsav;
689 inp->inp_inc.inc_flags = incflagsav;
692 TCPDEBUG2(PRU_CONNECT);
693 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
700 * Initiate disconnect from peer.
701 * If connection never passed embryonic stage, just drop;
702 * else if don't need to let data drain, then can just drop anyways,
703 * else have to begin TCP shutdown process: mark socket disconnecting,
704 * drain unread data, state switch to reflect user close, and
705 * send segment (e.g. FIN) to peer. Socket will be really disconnected
706 * when peer sends FIN and acks ours.
708 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
711 tcp_usr_disconnect(struct socket *so)
714 struct tcpcb *tp = NULL;
715 struct epoch_tracker et;
721 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
723 if (inp->inp_flags & INP_TIMEWAIT)
725 if (inp->inp_flags & INP_DROPPED) {
733 TCPDEBUG2(PRU_DISCONNECT);
734 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
742 * Accept a connection. Essentially all the work is done at higher levels;
743 * just return the address of the peer, storing through addr.
746 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
749 struct inpcb *inp = NULL;
750 struct tcpcb *tp = NULL;
755 if (so->so_state & SS_ISDISCONNECTED)
756 return (ECONNABORTED);
759 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
761 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
762 error = ECONNABORTED;
769 * We inline in_getpeeraddr and COMMON_END here, so that we can
770 * copy the data of interest and defer the malloc until after we
773 port = inp->inp_fport;
774 addr = inp->inp_faddr;
777 TCPDEBUG2(PRU_ACCEPT);
778 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
781 *nam = in_sockaddr(port, &addr);
788 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
790 struct inpcb *inp = NULL;
792 struct tcpcb *tp = NULL;
794 struct in6_addr addr6;
795 struct epoch_tracker et;
800 if (so->so_state & SS_ISDISCONNECTED)
801 return (ECONNABORTED);
804 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
807 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
808 error = ECONNABORTED;
815 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
816 * copy the data of interest and defer the malloc until after we
819 if (inp->inp_vflag & INP_IPV4) {
821 port = inp->inp_fport;
822 addr = inp->inp_faddr;
824 port = inp->inp_fport;
825 addr6 = inp->in6p_faddr;
829 TCPDEBUG2(PRU_ACCEPT);
830 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
835 *nam = in6_v4mapsin6_sockaddr(port, &addr);
837 *nam = in6_sockaddr(port, &addr6);
844 * Mark the connection as being incapable of further output.
847 tcp_usr_shutdown(struct socket *so)
851 struct tcpcb *tp = NULL;
852 struct epoch_tracker et;
857 KASSERT(inp != NULL, ("inp == NULL"));
859 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
867 if (!(inp->inp_flags & INP_DROPPED))
868 error = tp->t_fb->tfb_tcp_output(tp);
871 TCPDEBUG2(PRU_SHUTDOWN);
872 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
880 * After a receive, possibly send window update to peer.
883 tcp_usr_rcvd(struct socket *so, int flags)
886 struct tcpcb *tp = NULL;
891 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
893 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
900 * For passively-created TFO connections, don't attempt a window
901 * update while still in SYN_RECEIVED as this may trigger an early
902 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
903 * application response data, or failing that, when the DELACK timer
906 if (IS_FASTOPEN(tp->t_flags) &&
907 (tp->t_state == TCPS_SYN_RECEIVED))
910 if (tp->t_flags & TF_TOE)
911 tcp_offload_rcvd(tp);
914 tp->t_fb->tfb_tcp_output(tp);
918 TCP_PROBE2(debug__user, tp, PRU_RCVD);
924 * Do a send by putting data in output queue and updating urgent
925 * marker if URG set. Possibly send more data. Unlike the other
926 * pru_*() routines, the mbuf chains are our responsibility. We
927 * must either enqueue them or free them. The other pru_* routines
928 * generally are caller-frees.
931 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
932 struct sockaddr *nam, struct mbuf *control, struct thread *td)
934 struct epoch_tracker et;
937 struct tcpcb *tp = NULL;
940 struct sockaddr_in sin;
942 struct sockaddr_in *sinp;
953 * We require the pcbinfo "read lock" if we will close the socket
954 * as part of this call.
956 if (flags & PRUS_EOF)
959 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
961 vflagsav = inp->inp_vflag;
962 incflagsav = inp->inp_inc.inc_flags;
963 restoreflags = false;
964 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
968 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
969 * for freeing memory.
971 if (m && (flags & PRUS_NOTREADY) == 0)
978 if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
979 switch (nam->sa_family) {
982 sinp = (struct sockaddr_in *)nam;
983 if (sinp->sin_len != sizeof(struct sockaddr_in)) {
989 if ((inp->inp_vflag & INP_IPV6) != 0) {
992 error = EAFNOSUPPORT;
995 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
998 error = EAFNOSUPPORT;
1001 if ((error = prison_remote_ip4(td->td_ucred,
1002 &sinp->sin_addr))) {
1015 struct sockaddr_in6 *sin6;
1017 sin6 = (struct sockaddr_in6 *)nam;
1018 if (sin6->sin6_len != sizeof(*sin6)) {
1024 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1027 error = EAFNOSUPPORT;
1030 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1032 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1038 if ((inp->inp_vflag & INP_IPV4) == 0) {
1039 error = EAFNOSUPPORT;
1044 restoreflags = true;
1045 inp->inp_vflag &= ~INP_IPV6;
1047 in6_sin6_2_sin(sinp, sin6);
1049 ntohl(sinp->sin_addr.s_addr))) {
1050 error = EAFNOSUPPORT;
1055 if ((error = prison_remote_ip4(td->td_ucred,
1056 &sinp->sin_addr))) {
1063 error = EAFNOSUPPORT;
1069 if ((inp->inp_vflag & INP_IPV6) == 0) {
1072 error = EAFNOSUPPORT;
1075 restoreflags = true;
1076 inp->inp_vflag &= ~INP_IPV4;
1077 inp->inp_inc.inc_flags |= INC_ISIPV6;
1078 if ((error = prison_remote_ip6(td->td_ucred,
1079 &sin6->sin6_addr))) {
1092 error = EAFNOSUPPORT;
1097 /* TCP doesn't do control messages (rights, creds, etc) */
1098 if (control->m_len) {
1105 m_freem(control); /* empty control, just free it */
1107 if (!(flags & PRUS_OOB)) {
1108 sbappendstream(&so->so_snd, m, flags);
1109 if (nam && tp->t_state < TCPS_SYN_SENT) {
1111 * Do implied connect if not yet connected,
1112 * initialize window to default value, and
1113 * initialize maxseg using peer's cached MSS.
1117 error = tcp6_connect(tp, nam, td);
1119 #if defined(INET6) && defined(INET)
1123 error = tcp_connect(tp,
1124 (struct sockaddr *)sinp, td);
1127 * The bind operation in tcp_connect succeeded. We
1128 * no longer want to restore the flags if later
1131 if (error == 0 || inp->inp_lport != 0)
1132 restoreflags = false;
1136 if (IS_FASTOPEN(tp->t_flags))
1137 tcp_fastopen_connect(tp);
1139 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1143 if (flags & PRUS_EOF) {
1145 * Close the send side of the connection after
1152 if (!(inp->inp_flags & INP_DROPPED) &&
1153 !(flags & PRUS_NOTREADY)) {
1154 if (flags & PRUS_MORETOCOME)
1155 tp->t_flags |= TF_MORETOCOME;
1156 error = tp->t_fb->tfb_tcp_output(tp);
1157 if (flags & PRUS_MORETOCOME)
1158 tp->t_flags &= ~TF_MORETOCOME;
1162 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1164 SOCKBUF_LOCK(&so->so_snd);
1165 if (sbspace(&so->so_snd) < -512) {
1166 SOCKBUF_UNLOCK(&so->so_snd);
1172 * According to RFC961 (Assigned Protocols),
1173 * the urgent pointer points to the last octet
1174 * of urgent data. We continue, however,
1175 * to consider it to indicate the first octet
1176 * of data past the urgent section.
1177 * Otherwise, snd_up should be one lower.
1179 sbappendstream_locked(&so->so_snd, m, flags);
1180 SOCKBUF_UNLOCK(&so->so_snd);
1181 if (nam && tp->t_state < TCPS_SYN_SENT) {
1183 * Do implied connect if not yet connected,
1184 * initialize window to default value, and
1185 * initialize maxseg using peer's cached MSS.
1189 * Not going to contemplate SYN|URG
1191 if (IS_FASTOPEN(tp->t_flags))
1192 tp->t_flags &= ~TF_FASTOPEN;
1195 error = tcp6_connect(tp, nam, td);
1197 #if defined(INET6) && defined(INET)
1201 error = tcp_connect(tp,
1202 (struct sockaddr *)sinp, td);
1205 * The bind operation in tcp_connect succeeded. We
1206 * no longer want to restore the flags if later
1209 if (error == 0 || inp->inp_lport != 0)
1210 restoreflags = false;
1214 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1217 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1218 if (!(flags & PRUS_NOTREADY)) {
1219 tp->t_flags |= TF_FORCEDATA;
1220 error = tp->t_fb->tfb_tcp_output(tp);
1221 tp->t_flags &= ~TF_FORCEDATA;
1224 TCP_LOG_EVENT(tp, NULL,
1225 &inp->inp_socket->so_rcv,
1226 &inp->inp_socket->so_snd,
1227 TCP_LOG_USERSEND, error,
1231 * If the request was unsuccessful and we changed flags,
1232 * restore the original flags.
1234 if (error != 0 && restoreflags) {
1235 inp->inp_vflag = vflagsav;
1236 inp->inp_inc.inc_flags = incflagsav;
1238 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
1239 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1240 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1241 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1243 if (flags & PRUS_EOF)
1249 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1255 inp = sotoinpcb(so);
1257 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1259 mb_free_notready(m, count);
1260 return (ECONNRESET);
1262 tp = intotcpcb(inp);
1264 SOCKBUF_LOCK(&so->so_snd);
1265 error = sbready(&so->so_snd, m, count);
1266 SOCKBUF_UNLOCK(&so->so_snd);
1268 error = tp->t_fb->tfb_tcp_output(tp);
1275 * Abort the TCP. Drop the connection abruptly.
1278 tcp_usr_abort(struct socket *so)
1281 struct tcpcb *tp = NULL;
1282 struct epoch_tracker et;
1285 inp = sotoinpcb(so);
1286 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1288 NET_EPOCH_ENTER(et);
1290 KASSERT(inp->inp_socket != NULL,
1291 ("tcp_usr_abort: inp_socket == NULL"));
1294 * If we still have full TCP state, and we're not dropped, drop.
1296 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1297 !(inp->inp_flags & INP_DROPPED)) {
1298 tp = intotcpcb(inp);
1300 tp = tcp_drop(tp, ECONNABORTED);
1303 TCPDEBUG2(PRU_ABORT);
1304 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1306 if (!(inp->inp_flags & INP_DROPPED)) {
1308 so->so_state |= SS_PROTOREF;
1310 inp->inp_flags |= INP_SOCKREF;
1318 * TCP socket is closed. Start friendly disconnect.
1321 tcp_usr_close(struct socket *so)
1324 struct tcpcb *tp = NULL;
1325 struct epoch_tracker et;
1328 inp = sotoinpcb(so);
1329 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1331 NET_EPOCH_ENTER(et);
1333 KASSERT(inp->inp_socket != NULL,
1334 ("tcp_usr_close: inp_socket == NULL"));
1337 * If we still have full TCP state, and we're not dropped, initiate
1340 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1341 !(inp->inp_flags & INP_DROPPED)) {
1342 tp = intotcpcb(inp);
1345 TCPDEBUG2(PRU_CLOSE);
1346 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1348 if (!(inp->inp_flags & INP_DROPPED)) {
1350 so->so_state |= SS_PROTOREF;
1352 inp->inp_flags |= INP_SOCKREF;
1359 * Receive out-of-band data.
1362 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1366 struct tcpcb *tp = NULL;
1369 inp = sotoinpcb(so);
1370 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1372 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1376 tp = intotcpcb(inp);
1378 if ((so->so_oobmark == 0 &&
1379 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1380 so->so_options & SO_OOBINLINE ||
1381 tp->t_oobflags & TCPOOB_HADDATA) {
1385 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1386 error = EWOULDBLOCK;
1390 *mtod(m, caddr_t) = tp->t_iobc;
1391 if ((flags & MSG_PEEK) == 0)
1392 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1395 TCPDEBUG2(PRU_RCVOOB);
1396 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1402 struct pr_usrreqs tcp_usrreqs = {
1403 .pru_abort = tcp_usr_abort,
1404 .pru_accept = tcp_usr_accept,
1405 .pru_attach = tcp_usr_attach,
1406 .pru_bind = tcp_usr_bind,
1407 .pru_connect = tcp_usr_connect,
1408 .pru_control = in_control,
1409 .pru_detach = tcp_usr_detach,
1410 .pru_disconnect = tcp_usr_disconnect,
1411 .pru_listen = tcp_usr_listen,
1412 .pru_peeraddr = in_getpeeraddr,
1413 .pru_rcvd = tcp_usr_rcvd,
1414 .pru_rcvoob = tcp_usr_rcvoob,
1415 .pru_send = tcp_usr_send,
1416 .pru_ready = tcp_usr_ready,
1417 .pru_shutdown = tcp_usr_shutdown,
1418 .pru_sockaddr = in_getsockaddr,
1419 .pru_sosetlabel = in_pcbsosetlabel,
1420 .pru_close = tcp_usr_close,
1425 struct pr_usrreqs tcp6_usrreqs = {
1426 .pru_abort = tcp_usr_abort,
1427 .pru_accept = tcp6_usr_accept,
1428 .pru_attach = tcp_usr_attach,
1429 .pru_bind = tcp6_usr_bind,
1430 .pru_connect = tcp6_usr_connect,
1431 .pru_control = in6_control,
1432 .pru_detach = tcp_usr_detach,
1433 .pru_disconnect = tcp_usr_disconnect,
1434 .pru_listen = tcp6_usr_listen,
1435 .pru_peeraddr = in6_mapped_peeraddr,
1436 .pru_rcvd = tcp_usr_rcvd,
1437 .pru_rcvoob = tcp_usr_rcvoob,
1438 .pru_send = tcp_usr_send,
1439 .pru_ready = tcp_usr_ready,
1440 .pru_shutdown = tcp_usr_shutdown,
1441 .pru_sockaddr = in6_mapped_sockaddr,
1442 .pru_sosetlabel = in_pcbsosetlabel,
1443 .pru_close = tcp_usr_close,
1449 * Common subroutine to open a TCP connection to remote host specified
1450 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1451 * port number if needed. Call in_pcbconnect_setup to do the routing and
1452 * to choose a local host address (interface). If there is an existing
1453 * incarnation of the same connection in TIME-WAIT state and if the remote
1454 * host was sending CC options and if the connection duration was < MSL, then
1455 * truncate the previous TIME-WAIT state and proceed.
1456 * Initialize connection parameters and enter SYN-SENT state.
1459 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1461 struct inpcb *inp = tp->t_inpcb, *oinp;
1462 struct socket *so = inp->inp_socket;
1463 struct in_addr laddr;
1467 INP_WLOCK_ASSERT(inp);
1468 INP_HASH_WLOCK(&V_tcbinfo);
1470 if (inp->inp_lport == 0) {
1471 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1477 * Cannot simply call in_pcbconnect, because there might be an
1478 * earlier incarnation of this same connection still in
1479 * TIME_WAIT state, creating an ADDRINUSE error.
1481 laddr = inp->inp_laddr;
1482 lport = inp->inp_lport;
1483 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1484 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1485 if (error && oinp == NULL)
1491 inp->inp_laddr = laddr;
1493 INP_HASH_WUNLOCK(&V_tcbinfo);
1496 * Compute window scaling to request:
1497 * Scale to fit into sweet spot. See tcp_syncache.c.
1498 * XXX: This should move to tcp_output().
1500 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1501 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1502 tp->request_r_scale++;
1505 TCPSTAT_INC(tcps_connattempt);
1506 tcp_state_change(tp, TCPS_SYN_SENT);
1507 tp->iss = tcp_new_isn(&inp->inp_inc);
1508 if (tp->t_flags & TF_REQ_TSTMP)
1509 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1510 tcp_sendseqinit(tp);
1515 INP_HASH_WUNLOCK(&V_tcbinfo);
1522 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1524 struct inpcb *inp = tp->t_inpcb;
1527 INP_WLOCK_ASSERT(inp);
1528 INP_HASH_WLOCK(&V_tcbinfo);
1530 if (inp->inp_lport == 0) {
1531 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1535 error = in6_pcbconnect(inp, nam, td->td_ucred);
1538 INP_HASH_WUNLOCK(&V_tcbinfo);
1540 /* Compute window scaling to request. */
1541 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1542 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1543 tp->request_r_scale++;
1545 soisconnecting(inp->inp_socket);
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);
1562 * Export TCP internal state information via a struct tcp_info, based on the
1563 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1564 * (TCP state machine, etc). We export all information using FreeBSD-native
1565 * constants -- for example, the numeric values for tcpi_state will differ
1569 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1572 INP_WLOCK_ASSERT(tp->t_inpcb);
1573 bzero(ti, sizeof(*ti));
1575 ti->tcpi_state = tp->t_state;
1576 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1577 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1578 if (tp->t_flags & TF_SACK_PERMIT)
1579 ti->tcpi_options |= TCPI_OPT_SACK;
1580 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1581 ti->tcpi_options |= TCPI_OPT_WSCALE;
1582 ti->tcpi_snd_wscale = tp->snd_scale;
1583 ti->tcpi_rcv_wscale = tp->rcv_scale;
1585 if (tp->t_flags2 & TF2_ECN_PERMIT)
1586 ti->tcpi_options |= TCPI_OPT_ECN;
1588 ti->tcpi_rto = tp->t_rxtcur * tick;
1589 ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1590 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1591 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1593 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1594 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1597 * FreeBSD-specific extension fields for tcp_info.
1599 ti->tcpi_rcv_space = tp->rcv_wnd;
1600 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1601 ti->tcpi_snd_wnd = tp->snd_wnd;
1602 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1603 ti->tcpi_snd_nxt = tp->snd_nxt;
1604 ti->tcpi_snd_mss = tp->t_maxseg;
1605 ti->tcpi_rcv_mss = tp->t_maxseg;
1606 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1607 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1608 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1610 if (tp->t_flags & TF_TOE) {
1611 ti->tcpi_options |= TCPI_OPT_TOE;
1612 tcp_offload_tcp_info(tp, ti);
1618 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1619 * socket option arguments. When it re-acquires the lock after the copy, it
1620 * has to revalidate that the connection is still valid for the socket
1623 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \
1625 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1628 return (ECONNRESET); \
1630 tp = intotcpcb(inp); \
1632 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1635 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1640 struct tcp_function_block *blk;
1641 struct tcp_function_set fsn;
1644 inp = sotoinpcb(so);
1645 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1646 if (sopt->sopt_level != IPPROTO_TCP) {
1648 if (inp->inp_vflag & INP_IPV6PROTO) {
1649 error = ip6_ctloutput(so, sopt);
1651 * In case of the IPV6_USE_MIN_MTU socket option,
1652 * the INC_IPV6MINMTU flag to announce a corresponding
1653 * MSS during the initial handshake.
1654 * If the TCP connection is not in the front states,
1655 * just reduce the MSS being used.
1656 * This avoids the sending of TCP segments which will
1657 * be fragmented at the IPv6 layer.
1660 (sopt->sopt_dir == SOPT_SET) &&
1661 (sopt->sopt_level == IPPROTO_IPV6) &&
1662 (sopt->sopt_name == IPV6_USE_MIN_MTU)) {
1664 if ((inp->inp_flags &
1665 (INP_TIMEWAIT | INP_DROPPED))) {
1667 return (ECONNRESET);
1669 inp->inp_inc.inc_flags |= INC_IPV6MINMTU;
1670 tp = intotcpcb(inp);
1671 if ((tp->t_state >= TCPS_SYN_SENT) &&
1672 (inp->inp_inc.inc_flags & INC_ISIPV6)) {
1673 struct ip6_pktopts *opt;
1675 opt = inp->in6p_outputopts;
1676 if ((opt != NULL) &&
1677 (opt->ip6po_minmtu ==
1678 IP6PO_MINMTU_ALL)) {
1679 if (tp->t_maxseg > TCP6_MSS) {
1680 tp->t_maxseg = TCP6_MSS;
1688 #if defined(INET6) && defined(INET)
1693 error = ip_ctloutput(so, sopt);
1699 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1701 return (ECONNRESET);
1703 tp = intotcpcb(inp);
1705 * Protect the TCP option TCP_FUNCTION_BLK so
1706 * that a sub-function can *never* overwrite this.
1708 if ((sopt->sopt_dir == SOPT_SET) &&
1709 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1711 error = sooptcopyin(sopt, &fsn, sizeof fsn,
1715 INP_WLOCK_RECHECK(inp);
1716 blk = find_and_ref_tcp_functions(&fsn);
1721 if (tp->t_fb == blk) {
1722 /* You already have this */
1723 refcount_release(&blk->tfb_refcnt);
1727 if (tp->t_state != TCPS_CLOSED) {
1729 * The user has advanced the state
1730 * past the initial point, we may not
1731 * be able to switch.
1733 if (blk->tfb_tcp_handoff_ok != NULL) {
1735 * Does the stack provide a
1736 * query mechanism, if so it may
1737 * still be possible?
1739 error = (*blk->tfb_tcp_handoff_ok)(tp);
1743 refcount_release(&blk->tfb_refcnt);
1748 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1749 refcount_release(&blk->tfb_refcnt);
1754 * Release the old refcnt, the
1755 * lookup acquired a ref on the
1758 if (tp->t_fb->tfb_tcp_fb_fini) {
1760 * Tell the stack to cleanup with 0 i.e.
1761 * the tcb is not going away.
1763 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1766 /* Assure that we are not on any hpts */
1767 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
1769 if (blk->tfb_tcp_fb_init) {
1770 error = (*blk->tfb_tcp_fb_init)(tp);
1772 refcount_release(&blk->tfb_refcnt);
1773 if (tp->t_fb->tfb_tcp_fb_init) {
1774 if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) {
1775 /* Fall back failed, drop the connection */
1784 refcount_release(&tp->t_fb->tfb_refcnt);
1787 if (tp->t_flags & TF_TOE) {
1788 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1795 } else if ((sopt->sopt_dir == SOPT_GET) &&
1796 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1797 strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1798 TCP_FUNCTION_NAME_LEN_MAX);
1799 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1800 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1802 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1805 /* Pass in the INP locked, called must unlock it */
1806 return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
1810 * If this assert becomes untrue, we need to change the size of the buf
1811 * variable in tcp_default_ctloutput().
1814 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1815 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1819 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
1821 int error, opt, optval;
1825 struct tls_enable tls;
1827 struct cc_algo *algo;
1828 char *pbuf, buf[TCP_LOG_ID_LEN];
1830 struct statsblob *sbp;
1835 * For TCP_CCALGOOPT forward the control to CC module, for both
1836 * SOPT_SET and SOPT_GET.
1838 switch (sopt->sopt_name) {
1841 if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
1843 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1844 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1845 sopt->sopt_valsize);
1850 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1851 if (CC_ALGO(tp)->ctl_output != NULL)
1852 error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1856 if (error == 0 && sopt->sopt_dir == SOPT_GET)
1857 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1862 switch (sopt->sopt_dir) {
1864 switch (sopt->sopt_name) {
1865 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1867 if (!TCPMD5_ENABLED()) {
1869 return (ENOPROTOOPT);
1871 error = TCPMD5_PCBCTL(inp, sopt);
1874 goto unlock_and_done;
1880 error = sooptcopyin(sopt, &optval, sizeof optval,
1885 INP_WLOCK_RECHECK(inp);
1886 switch (sopt->sopt_name) {
1894 opt = 0; /* dead code to fool gcc */
1901 tp->t_flags &= ~opt;
1904 if (tp->t_flags & TF_TOE) {
1905 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1914 error = sooptcopyin(sopt, &optval, sizeof optval,
1919 INP_WLOCK_RECHECK(inp);
1921 tp->t_flags |= TF_NOPUSH;
1922 else if (tp->t_flags & TF_NOPUSH) {
1923 tp->t_flags &= ~TF_NOPUSH;
1924 if (TCPS_HAVEESTABLISHED(tp->t_state))
1925 error = tp->t_fb->tfb_tcp_output(tp);
1927 goto unlock_and_done;
1931 error = sooptcopyin(sopt, &optval, sizeof optval,
1936 INP_WLOCK_RECHECK(inp);
1937 if (optval > 0 && optval <= tp->t_maxseg &&
1938 optval + 40 >= V_tcp_minmss)
1939 tp->t_maxseg = optval;
1942 goto unlock_and_done;
1952 error = sooptcopyin(sopt, &optval, sizeof optval,
1958 sbp = stats_blob_alloc(
1959 V_tcp_perconn_stats_dflt_tpl, 0);
1963 INP_WLOCK_RECHECK(inp);
1964 if ((tp->t_stats != NULL && sbp == NULL) ||
1965 (tp->t_stats == NULL && sbp != NULL)) {
1966 struct statsblob *t = tp->t_stats;
1972 stats_blob_destroy(sbp);
1974 return (EOPNOTSUPP);
1978 case TCP_CONGESTION:
1980 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1983 buf[sopt->sopt_valsize] = '\0';
1984 INP_WLOCK_RECHECK(inp);
1986 STAILQ_FOREACH(algo, &cc_list, entries)
1987 if (strncmp(buf, algo->name,
1988 TCP_CA_NAME_MAX) == 0)
1997 * We hold a write lock over the tcb so it's safe to
1998 * do these things without ordering concerns.
2000 if (CC_ALGO(tp)->cb_destroy != NULL)
2001 CC_ALGO(tp)->cb_destroy(tp->ccv);
2005 * If something goes pear shaped initialising the new
2006 * algo, fall back to newreno (which does not
2007 * require initialisation).
2009 if (algo->cb_init != NULL &&
2010 algo->cb_init(tp->ccv) != 0) {
2011 CC_ALGO(tp) = &newreno_cc_algo;
2013 * The only reason init should fail is
2014 * because of malloc.
2022 case TCP_TXTLS_ENABLE:
2024 error = sooptcopyin(sopt, &tls, sizeof(tls),
2028 error = ktls_enable_tx(so, &tls);
2030 case TCP_TXTLS_MODE:
2032 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2036 INP_WLOCK_RECHECK(inp);
2037 error = ktls_set_tx_mode(so, ui);
2046 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2050 if (ui > (UINT_MAX / hz)) {
2056 INP_WLOCK_RECHECK(inp);
2057 switch (sopt->sopt_name) {
2059 tp->t_keepidle = ui;
2061 * XXX: better check current remaining
2062 * timeout and "merge" it with new value.
2064 if ((tp->t_state > TCPS_LISTEN) &&
2065 (tp->t_state <= TCPS_CLOSING))
2066 tcp_timer_activate(tp, TT_KEEP,
2070 tp->t_keepintvl = ui;
2071 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2072 (TP_MAXIDLE(tp) > 0))
2073 tcp_timer_activate(tp, TT_2MSL,
2077 tp->t_keepinit = ui;
2078 if (tp->t_state == TCPS_SYN_RECEIVED ||
2079 tp->t_state == TCPS_SYN_SENT)
2080 tcp_timer_activate(tp, TT_KEEP,
2084 goto unlock_and_done;
2088 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2092 INP_WLOCK_RECHECK(inp);
2094 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2095 (TP_MAXIDLE(tp) > 0))
2096 tcp_timer_activate(tp, TT_2MSL,
2098 goto unlock_and_done;
2104 error = sooptcopyin(sopt, &optval, sizeof optval,
2109 INP_WLOCK_RECHECK(inp);
2111 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
2112 &(tp->t_outpkts) : &(tp->t_inpkts),
2116 goto unlock_and_done;
2119 case TCP_FASTOPEN: {
2120 struct tcp_fastopen tfo_optval;
2123 if (!V_tcp_fastopen_client_enable &&
2124 !V_tcp_fastopen_server_enable)
2127 error = sooptcopyin(sopt, &tfo_optval,
2128 sizeof(tfo_optval), sizeof(int));
2132 INP_WLOCK_RECHECK(inp);
2133 if (tfo_optval.enable) {
2134 if (tp->t_state == TCPS_LISTEN) {
2135 if (!V_tcp_fastopen_server_enable) {
2137 goto unlock_and_done;
2140 tp->t_flags |= TF_FASTOPEN;
2141 if (tp->t_tfo_pending == NULL)
2143 tcp_fastopen_alloc_counter();
2146 * If a pre-shared key was provided,
2147 * stash it in the client cookie
2148 * field of the tcpcb for use during
2151 if (sopt->sopt_valsize ==
2152 sizeof(tfo_optval)) {
2153 memcpy(tp->t_tfo_cookie.client,
2155 TCP_FASTOPEN_PSK_LEN);
2156 tp->t_tfo_client_cookie_len =
2157 TCP_FASTOPEN_PSK_LEN;
2159 tp->t_flags |= TF_FASTOPEN;
2162 tp->t_flags &= ~TF_FASTOPEN;
2163 goto unlock_and_done;
2169 error = sooptcopyin(sopt, &optval, sizeof optval,
2174 INP_WLOCK_RECHECK(inp);
2175 error = tcp_log_state_change(tp, optval);
2176 goto unlock_and_done;
2185 error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
2188 buf[sopt->sopt_valsize] = '\0';
2189 INP_WLOCK_RECHECK(inp);
2190 error = tcp_log_set_id(tp, buf);
2191 /* tcp_log_set_id() unlocks the INP. */
2198 sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
2201 buf[sopt->sopt_valsize] = '\0';
2202 INP_WLOCK_RECHECK(inp);
2203 if (sopt->sopt_name == TCP_LOGDUMP) {
2204 error = tcp_log_dump_tp_logbuf(tp, buf,
2208 tcp_log_dump_tp_bucket_logbufs(tp, buf);
2210 * tcp_log_dump_tp_bucket_logbufs() drops the
2219 error = ENOPROTOOPT;
2225 tp = intotcpcb(inp);
2226 switch (sopt->sopt_name) {
2227 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2229 if (!TCPMD5_ENABLED()) {
2231 return (ENOPROTOOPT);
2233 error = TCPMD5_PCBCTL(inp, sopt);
2238 optval = tp->t_flags & TF_NODELAY;
2240 error = sooptcopyout(sopt, &optval, sizeof optval);
2243 optval = tp->t_maxseg;
2245 error = sooptcopyout(sopt, &optval, sizeof optval);
2248 optval = tp->t_flags & TF_NOOPT;
2250 error = sooptcopyout(sopt, &optval, sizeof optval);
2253 optval = tp->t_flags & TF_NOPUSH;
2255 error = sooptcopyout(sopt, &optval, sizeof optval);
2258 tcp_fill_info(tp, &ti);
2260 error = sooptcopyout(sopt, &ti, sizeof ti);
2266 TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2269 socklen_t outsbsz = sopt->sopt_valsize;
2270 if (tp->t_stats == NULL)
2272 else if (outsbsz >= tp->t_stats->cursz)
2273 outsbsz = tp->t_stats->cursz;
2274 else if (outsbsz >= sizeof(struct statsblob))
2275 outsbsz = sizeof(struct statsblob);
2282 sbp = sopt->sopt_val;
2283 nheld = atop(round_page(((vm_offset_t)sbp) +
2284 (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2285 vm_page_t ma[nheld];
2286 if (vm_fault_quick_hold_pages(
2287 &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2288 outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2294 if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2295 SIZEOF_MEMBER(struct statsblob, flags))))
2298 INP_WLOCK_RECHECK(inp);
2299 error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2300 sbflags | SB_CLONE_USRDSTNOFAULT);
2302 sopt->sopt_valsize = outsbsz;
2304 vm_page_unhold_pages(ma, nheld);
2311 case TCP_CONGESTION:
2312 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2314 error = sooptcopyout(sopt, buf, len + 1);
2320 switch (sopt->sopt_name) {
2322 ui = TP_KEEPIDLE(tp) / hz;
2325 ui = TP_KEEPINTVL(tp) / hz;
2328 ui = TP_KEEPINIT(tp) / hz;
2331 ui = TP_KEEPCNT(tp);
2335 error = sooptcopyout(sopt, &ui, sizeof(ui));
2340 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
2341 &(tp->t_outpkts) : &(tp->t_inpkts));
2343 error = sooptcopyout(sopt, &optval, sizeof optval);
2347 optval = tp->t_flags & TF_FASTOPEN;
2349 error = sooptcopyout(sopt, &optval, sizeof optval);
2353 optval = tp->t_logstate;
2355 error = sooptcopyout(sopt, &optval, sizeof(optval));
2358 /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2359 error = tcp_log_getlogbuf(sopt, tp);
2362 len = tcp_log_get_id(tp, buf);
2364 error = sooptcopyout(sopt, buf, len + 1);
2373 case TCP_TXTLS_MODE:
2374 optval = ktls_get_tx_mode(so);
2376 error = sooptcopyout(sopt, &optval, sizeof(optval));
2381 error = ENOPROTOOPT;
2388 #undef INP_WLOCK_RECHECK
2389 #undef INP_WLOCK_RECHECK_CLEANUP
2392 * Attach TCP protocol to socket, allocating
2393 * internet protocol control block, tcp control block,
2394 * bufer space, and entering LISTEN state if to accept connections.
2397 tcp_attach(struct socket *so)
2401 struct epoch_tracker et;
2404 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
2405 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
2409 so->so_rcv.sb_flags |= SB_AUTOSIZE;
2410 so->so_snd.sb_flags |= SB_AUTOSIZE;
2411 NET_EPOCH_ENTER(et);
2412 error = in_pcballoc(so, &V_tcbinfo);
2417 inp = sotoinpcb(so);
2419 if (inp->inp_vflag & INP_IPV6PROTO) {
2420 inp->inp_vflag |= INP_IPV6;
2421 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
2422 inp->inp_vflag |= INP_IPV4;
2423 inp->in6p_hops = -1; /* use kernel default */
2427 inp->inp_vflag |= INP_IPV4;
2428 tp = tcp_newtcpcb(inp);
2435 tp->t_state = TCPS_CLOSED;
2438 TCPSTATES_INC(TCPS_CLOSED);
2443 * Initiate (or continue) disconnect.
2444 * If embryonic state, just send reset (once).
2445 * If in ``let data drain'' option and linger null, just drop.
2446 * Otherwise (hard), mark socket disconnecting and drop
2447 * current input data; switch states based on user close, and
2448 * send segment to peer (with FIN).
2451 tcp_disconnect(struct tcpcb *tp)
2453 struct inpcb *inp = tp->t_inpcb;
2454 struct socket *so = inp->inp_socket;
2457 INP_WLOCK_ASSERT(inp);
2460 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2461 * socket is still open.
2463 if (tp->t_state < TCPS_ESTABLISHED &&
2464 !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2467 ("tcp_disconnect: tcp_close() returned NULL"));
2468 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2469 tp = tcp_drop(tp, 0);
2471 ("tcp_disconnect: tcp_drop() returned NULL"));
2473 soisdisconnecting(so);
2474 sbflush(&so->so_rcv);
2476 if (!(inp->inp_flags & INP_DROPPED))
2477 tp->t_fb->tfb_tcp_output(tp);
2482 * User issued close, and wish to trail through shutdown states:
2483 * if never received SYN, just forget it. If got a SYN from peer,
2484 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2485 * If already got a FIN from peer, then almost done; go to LAST_ACK
2486 * state. In all other cases, have already sent FIN to peer (e.g.
2487 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2488 * for peer to send FIN or not respond to keep-alives, etc.
2489 * We can let the user exit from the close as soon as the FIN is acked.
2492 tcp_usrclosed(struct tcpcb *tp)
2496 INP_WLOCK_ASSERT(tp->t_inpcb);
2498 switch (tp->t_state) {
2501 tcp_offload_listen_stop(tp);
2503 tcp_state_change(tp, TCPS_CLOSED);
2508 * tcp_close() should never return NULL here as the socket is
2512 ("tcp_usrclosed: tcp_close() returned NULL"));
2516 case TCPS_SYN_RECEIVED:
2517 tp->t_flags |= TF_NEEDFIN;
2520 case TCPS_ESTABLISHED:
2521 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2524 case TCPS_CLOSE_WAIT:
2525 tcp_state_change(tp, TCPS_LAST_ACK);
2528 if (tp->t_state >= TCPS_FIN_WAIT_2) {
2529 soisdisconnected(tp->t_inpcb->inp_socket);
2530 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
2531 if (tp->t_state == TCPS_FIN_WAIT_2) {
2534 timeout = (tcp_fast_finwait2_recycle) ?
2535 tcp_finwait2_timeout : TP_MAXIDLE(tp);
2536 tcp_timer_activate(tp, TT_2MSL, timeout);
2543 db_print_indent(int indent)
2547 for (i = 0; i < indent; i++)
2552 db_print_tstate(int t_state)
2557 db_printf("TCPS_CLOSED");
2561 db_printf("TCPS_LISTEN");
2565 db_printf("TCPS_SYN_SENT");
2568 case TCPS_SYN_RECEIVED:
2569 db_printf("TCPS_SYN_RECEIVED");
2572 case TCPS_ESTABLISHED:
2573 db_printf("TCPS_ESTABLISHED");
2576 case TCPS_CLOSE_WAIT:
2577 db_printf("TCPS_CLOSE_WAIT");
2580 case TCPS_FIN_WAIT_1:
2581 db_printf("TCPS_FIN_WAIT_1");
2585 db_printf("TCPS_CLOSING");
2589 db_printf("TCPS_LAST_ACK");
2592 case TCPS_FIN_WAIT_2:
2593 db_printf("TCPS_FIN_WAIT_2");
2596 case TCPS_TIME_WAIT:
2597 db_printf("TCPS_TIME_WAIT");
2601 db_printf("unknown");
2607 db_print_tflags(u_int t_flags)
2612 if (t_flags & TF_ACKNOW) {
2613 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2616 if (t_flags & TF_DELACK) {
2617 db_printf("%sTF_DELACK", comma ? ", " : "");
2620 if (t_flags & TF_NODELAY) {
2621 db_printf("%sTF_NODELAY", comma ? ", " : "");
2624 if (t_flags & TF_NOOPT) {
2625 db_printf("%sTF_NOOPT", comma ? ", " : "");
2628 if (t_flags & TF_SENTFIN) {
2629 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2632 if (t_flags & TF_REQ_SCALE) {
2633 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2636 if (t_flags & TF_RCVD_SCALE) {
2637 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2640 if (t_flags & TF_REQ_TSTMP) {
2641 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2644 if (t_flags & TF_RCVD_TSTMP) {
2645 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2648 if (t_flags & TF_SACK_PERMIT) {
2649 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2652 if (t_flags & TF_NEEDSYN) {
2653 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2656 if (t_flags & TF_NEEDFIN) {
2657 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2660 if (t_flags & TF_NOPUSH) {
2661 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2664 if (t_flags & TF_MORETOCOME) {
2665 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2668 if (t_flags & TF_LQ_OVERFLOW) {
2669 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2672 if (t_flags & TF_LASTIDLE) {
2673 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2676 if (t_flags & TF_RXWIN0SENT) {
2677 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2680 if (t_flags & TF_FASTRECOVERY) {
2681 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2684 if (t_flags & TF_CONGRECOVERY) {
2685 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2688 if (t_flags & TF_WASFRECOVERY) {
2689 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2692 if (t_flags & TF_SIGNATURE) {
2693 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2696 if (t_flags & TF_FORCEDATA) {
2697 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2700 if (t_flags & TF_TSO) {
2701 db_printf("%sTF_TSO", comma ? ", " : "");
2704 if (t_flags & TF_FASTOPEN) {
2705 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2711 db_print_tflags2(u_int t_flags2)
2716 if (t_flags2 & TF2_ECN_PERMIT) {
2717 db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
2724 db_print_toobflags(char t_oobflags)
2729 if (t_oobflags & TCPOOB_HAVEDATA) {
2730 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2733 if (t_oobflags & TCPOOB_HADDATA) {
2734 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2740 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2743 db_print_indent(indent);
2744 db_printf("%s at %p\n", name, tp);
2748 db_print_indent(indent);
2749 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2750 TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2752 db_print_indent(indent);
2753 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
2754 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2756 db_print_indent(indent);
2757 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2758 &tp->t_timers->tt_delack, tp->t_inpcb);
2760 db_print_indent(indent);
2761 db_printf("t_state: %d (", tp->t_state);
2762 db_print_tstate(tp->t_state);
2765 db_print_indent(indent);
2766 db_printf("t_flags: 0x%x (", tp->t_flags);
2767 db_print_tflags(tp->t_flags);
2770 db_print_indent(indent);
2771 db_printf("t_flags2: 0x%x (", tp->t_flags2);
2772 db_print_tflags2(tp->t_flags2);
2775 db_print_indent(indent);
2776 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2777 tp->snd_una, tp->snd_max, tp->snd_nxt);
2779 db_print_indent(indent);
2780 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2781 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2783 db_print_indent(indent);
2784 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2785 tp->iss, tp->irs, tp->rcv_nxt);
2787 db_print_indent(indent);
2788 db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n",
2789 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2791 db_print_indent(indent);
2792 db_printf("snd_wnd: %u snd_cwnd: %u\n",
2793 tp->snd_wnd, tp->snd_cwnd);
2795 db_print_indent(indent);
2796 db_printf("snd_ssthresh: %u snd_recover: "
2797 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2799 db_print_indent(indent);
2800 db_printf("t_rcvtime: %u t_startime: %u\n",
2801 tp->t_rcvtime, tp->t_starttime);
2803 db_print_indent(indent);
2804 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2805 tp->t_rtttime, tp->t_rtseq);
2807 db_print_indent(indent);
2808 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2809 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2811 db_print_indent(indent);
2812 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2813 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2816 db_print_indent(indent);
2817 db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n",
2818 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2820 db_print_indent(indent);
2821 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2822 db_print_toobflags(tp->t_oobflags);
2823 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2825 db_print_indent(indent);
2826 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2827 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2829 db_print_indent(indent);
2830 db_printf("ts_recent: %u ts_recent_age: %u\n",
2831 tp->ts_recent, tp->ts_recent_age);
2833 db_print_indent(indent);
2834 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2835 "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2837 db_print_indent(indent);
2838 db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x "
2839 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2840 tp->snd_recover_prev, tp->t_badrxtwin);
2842 db_print_indent(indent);
2843 db_printf("snd_numholes: %d snd_holes first: %p\n",
2844 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2846 db_print_indent(indent);
2847 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
2848 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2850 /* Skip sackblks, sackhint. */
2852 db_print_indent(indent);
2853 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2854 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2857 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2862 db_printf("usage: show tcpcb <addr>\n");
2865 tp = (struct tcpcb *)addr;
2867 db_print_tcpcb(tp, "tcpcb", 0);