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_tcpdebug.h"
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/limits.h>
52 #include <sys/malloc.h>
53 #include <sys/refcount.h>
54 #include <sys/kernel.h>
55 #include <sys/sysctl.h>
58 #include <sys/domain.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/protosw.h>
65 #include <sys/syslog.h>
72 #include <net/if_var.h>
73 #include <net/route.h>
76 #include <netinet/in.h>
77 #include <netinet/in_kdtrace.h>
78 #include <netinet/in_pcb.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/in_var.h>
81 #include <netinet/ip_var.h>
83 #include <netinet/ip6.h>
84 #include <netinet6/in6_pcb.h>
85 #include <netinet6/ip6_var.h>
86 #include <netinet6/scope6_var.h>
88 #include <netinet/tcp.h>
89 #include <netinet/tcp_fsm.h>
90 #include <netinet/tcp_seq.h>
91 #include <netinet/tcp_timer.h>
92 #include <netinet/tcp_var.h>
93 #include <netinet/tcp_log_buf.h>
94 #include <netinet/tcpip.h>
95 #include <netinet/cc/cc.h>
96 #include <netinet/tcp_fastopen.h>
98 #include <netinet/tcp_pcap.h>
101 #include <netinet/tcp_debug.h>
104 #include <netinet/tcp_offload.h>
106 #include <netipsec/ipsec_support.h>
109 * TCP protocol interface to socket abstraction.
111 static int tcp_attach(struct socket *);
113 static int tcp_connect(struct tcpcb *, struct sockaddr *,
117 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
120 static void tcp_disconnect(struct tcpcb *);
121 static void tcp_usrclosed(struct tcpcb *);
122 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
125 #define TCPDEBUG0 int ostate = 0
126 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
127 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
128 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
132 #define TCPDEBUG2(req)
136 * TCP attaches to socket via pru_attach(), reserving space,
137 * and an internet control block.
140 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
143 struct tcpcb *tp = NULL;
148 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
151 error = tcp_attach(so);
155 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
156 so->so_linger = TCP_LINGERTIME;
161 TCPDEBUG2(PRU_ATTACH);
162 TCP_PROBE2(debug__user, tp, PRU_ATTACH);
167 * tcp_detach is called when the socket layer loses its final reference
168 * to the socket, be it a file descriptor reference, a reference from TCP,
169 * etc. At this point, there is only one case in which we will keep around
170 * inpcb state: time wait.
172 * This function can probably be re-absorbed back into tcp_usr_detach() now
173 * that there is a single detach path.
176 tcp_detach(struct socket *so, struct inpcb *inp)
180 INP_INFO_LOCK_ASSERT(&V_tcbinfo);
181 INP_WLOCK_ASSERT(inp);
183 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
184 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
188 if (inp->inp_flags & INP_TIMEWAIT) {
190 * There are two cases to handle: one in which the time wait
191 * state is being discarded (INP_DROPPED), and one in which
192 * this connection will remain in timewait. In the former,
193 * it is time to discard all state (except tcptw, which has
194 * already been discarded by the timewait close code, which
195 * should be further up the call stack somewhere). In the
196 * latter case, we detach from the socket, but leave the pcb
197 * present until timewait ends.
199 * XXXRW: Would it be cleaner to free the tcptw here?
201 * Astute question indeed, from twtcp perspective there are
202 * four cases to consider:
204 * #1 tcp_detach is called at tcptw creation time by
205 * tcp_twstart, then do not discard the newly created tcptw
206 * and leave inpcb present until timewait ends
207 * #2 tcp_detach is called at tcptw creation time by
208 * tcp_twstart, but connection is local and tw will be
209 * discarded immediately
210 * #3 tcp_detach is called at timewait end (or reuse) by
211 * tcp_twclose, then the tcptw has already been discarded
212 * (or reused) and inpcb is freed here
213 * #4 tcp_detach is called() after timewait ends (or reuse)
214 * (e.g. by soclose), then tcptw has already been discarded
215 * (or reused) and inpcb is freed here
217 * In all three cases the tcptw should not be freed here.
219 if (inp->inp_flags & INP_DROPPED) {
221 if (__predict_true(tp == NULL)) {
225 * This case should not happen as in TIMEWAIT
226 * state the inp should not be destroyed before
227 * its tcptw. If INVARIANTS is defined, panic.
230 panic("%s: Panic before an inp double-free: "
231 "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
234 log(LOG_ERR, "%s: Avoid an inp double-free: "
235 "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
246 * If the connection is not in timewait, we consider two
247 * two conditions: one in which no further processing is
248 * necessary (dropped || embryonic), and one in which TCP is
249 * not yet done, but no longer requires the socket, so the
250 * pcb will persist for the time being.
252 * XXXRW: Does the second case still occur?
254 if (inp->inp_flags & INP_DROPPED ||
255 tp->t_state < TCPS_SYN_SENT) {
267 * pru_detach() detaches the TCP protocol from the socket.
268 * If the protocol state is non-embryonic, then can't
269 * do this directly: have to initiate a pru_disconnect(),
270 * which may finish later; embryonic TCB's can just
274 tcp_usr_detach(struct socket *so)
280 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
281 if (!INP_INFO_WLOCKED(&V_tcbinfo)) {
282 INP_INFO_RLOCK(&V_tcbinfo);
286 KASSERT(inp->inp_socket != NULL,
287 ("tcp_usr_detach: inp_socket == NULL"));
290 INP_INFO_RUNLOCK(&V_tcbinfo);
295 * Give the socket an address.
298 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
302 struct tcpcb *tp = NULL;
303 struct sockaddr_in *sinp;
305 sinp = (struct sockaddr_in *)nam;
306 if (nam->sa_len != sizeof (*sinp))
309 * Must check for multicast addresses and disallow binding
312 if (sinp->sin_family == AF_INET &&
313 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
314 return (EAFNOSUPPORT);
318 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
320 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
326 INP_HASH_WLOCK(&V_tcbinfo);
327 error = in_pcbbind(inp, nam, td->td_ucred);
328 INP_HASH_WUNLOCK(&V_tcbinfo);
331 TCP_PROBE2(debug__user, tp, PRU_BIND);
340 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
344 struct tcpcb *tp = NULL;
345 struct sockaddr_in6 *sin6p;
347 sin6p = (struct sockaddr_in6 *)nam;
348 if (nam->sa_len != sizeof (*sin6p))
351 * Must check for multicast addresses and disallow binding
354 if (sin6p->sin6_family == AF_INET6 &&
355 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
356 return (EAFNOSUPPORT);
360 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
362 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
368 INP_HASH_WLOCK(&V_tcbinfo);
369 inp->inp_vflag &= ~INP_IPV4;
370 inp->inp_vflag |= INP_IPV6;
372 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
373 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
374 inp->inp_vflag |= INP_IPV4;
375 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
376 struct sockaddr_in sin;
378 in6_sin6_2_sin(&sin, sin6p);
379 inp->inp_vflag |= INP_IPV4;
380 inp->inp_vflag &= ~INP_IPV6;
381 error = in_pcbbind(inp, (struct sockaddr *)&sin,
383 INP_HASH_WUNLOCK(&V_tcbinfo);
388 error = in6_pcbbind(inp, nam, td->td_ucred);
389 INP_HASH_WUNLOCK(&V_tcbinfo);
392 TCP_PROBE2(debug__user, tp, PRU_BIND);
400 * Prepare to accept connections.
403 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
407 struct tcpcb *tp = NULL;
411 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
413 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
420 error = solisten_proto_check(so);
421 INP_HASH_WLOCK(&V_tcbinfo);
422 if (error == 0 && inp->inp_lport == 0)
423 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
424 INP_HASH_WUNLOCK(&V_tcbinfo);
426 tcp_state_change(tp, TCPS_LISTEN);
427 solisten_proto(so, backlog);
429 if ((so->so_options & SO_NO_OFFLOAD) == 0)
430 tcp_offload_listen_start(tp);
435 if (IS_FASTOPEN(tp->t_flags))
436 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
439 TCPDEBUG2(PRU_LISTEN);
440 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
448 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
452 struct tcpcb *tp = NULL;
456 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
458 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
465 error = solisten_proto_check(so);
466 INP_HASH_WLOCK(&V_tcbinfo);
467 if (error == 0 && inp->inp_lport == 0) {
468 inp->inp_vflag &= ~INP_IPV4;
469 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
470 inp->inp_vflag |= INP_IPV4;
471 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
473 INP_HASH_WUNLOCK(&V_tcbinfo);
475 tcp_state_change(tp, TCPS_LISTEN);
476 solisten_proto(so, backlog);
478 if ((so->so_options & SO_NO_OFFLOAD) == 0)
479 tcp_offload_listen_start(tp);
484 if (IS_FASTOPEN(tp->t_flags))
485 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
488 TCPDEBUG2(PRU_LISTEN);
489 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
497 * Initiate connection to peer.
498 * Create a template for use in transmissions on this connection.
499 * Enter SYN_SENT state, and mark socket as connecting.
500 * Start keep-alive timer, and seed output sequence space.
501 * Send initial segment on connection.
504 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
508 struct tcpcb *tp = NULL;
509 struct sockaddr_in *sinp;
511 sinp = (struct sockaddr_in *)nam;
512 if (nam->sa_len != sizeof (*sinp))
515 * Must disallow TCP ``connections'' to multicast addresses.
517 if (sinp->sin_family == AF_INET
518 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
519 return (EAFNOSUPPORT);
520 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
525 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
527 if (inp->inp_flags & INP_TIMEWAIT) {
531 if (inp->inp_flags & INP_DROPPED) {
532 error = ECONNREFUSED;
537 if ((error = tcp_connect(tp, nam, td)) != 0)
540 if (registered_toedevs > 0 &&
541 (so->so_options & SO_NO_OFFLOAD) == 0 &&
542 (error = tcp_offload_connect(so, nam)) == 0)
545 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
546 error = tp->t_fb->tfb_tcp_output(tp);
548 TCPDEBUG2(PRU_CONNECT);
549 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
557 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
561 struct tcpcb *tp = NULL;
562 struct sockaddr_in6 *sin6p;
566 sin6p = (struct sockaddr_in6 *)nam;
567 if (nam->sa_len != sizeof (*sin6p))
570 * Must disallow TCP ``connections'' to multicast addresses.
572 if (sin6p->sin6_family == AF_INET6
573 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
574 return (EAFNOSUPPORT);
577 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
579 if (inp->inp_flags & INP_TIMEWAIT) {
583 if (inp->inp_flags & INP_DROPPED) {
584 error = ECONNREFUSED;
591 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
592 * therefore probably require the hash lock, which isn't held here.
593 * Is this a significant problem?
595 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
596 struct sockaddr_in sin;
598 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
602 if ((inp->inp_vflag & INP_IPV4) == 0) {
603 error = EAFNOSUPPORT;
607 in6_sin6_2_sin(&sin, sin6p);
608 inp->inp_vflag |= INP_IPV4;
609 inp->inp_vflag &= ~INP_IPV6;
610 if ((error = prison_remote_ip4(td->td_ucred,
611 &sin.sin_addr)) != 0)
613 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
616 if (registered_toedevs > 0 &&
617 (so->so_options & SO_NO_OFFLOAD) == 0 &&
618 (error = tcp_offload_connect(so, nam)) == 0)
621 error = tp->t_fb->tfb_tcp_output(tp);
624 if ((inp->inp_vflag & INP_IPV6) == 0) {
625 error = EAFNOSUPPORT;
630 inp->inp_vflag &= ~INP_IPV4;
631 inp->inp_vflag |= INP_IPV6;
632 inp->inp_inc.inc_flags |= INC_ISIPV6;
633 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
635 if ((error = tcp6_connect(tp, nam, td)) != 0)
638 if (registered_toedevs > 0 &&
639 (so->so_options & SO_NO_OFFLOAD) == 0 &&
640 (error = tcp_offload_connect(so, nam)) == 0)
643 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
644 error = tp->t_fb->tfb_tcp_output(tp);
647 TCPDEBUG2(PRU_CONNECT);
648 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
655 * Initiate disconnect from peer.
656 * If connection never passed embryonic stage, just drop;
657 * else if don't need to let data drain, then can just drop anyways,
658 * else have to begin TCP shutdown process: mark socket disconnecting,
659 * drain unread data, state switch to reflect user close, and
660 * send segment (e.g. FIN) to peer. Socket will be really disconnected
661 * when peer sends FIN and acks ours.
663 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
666 tcp_usr_disconnect(struct socket *so)
669 struct tcpcb *tp = NULL;
673 INP_INFO_RLOCK(&V_tcbinfo);
675 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
677 if (inp->inp_flags & INP_TIMEWAIT)
679 if (inp->inp_flags & INP_DROPPED) {
687 TCPDEBUG2(PRU_DISCONNECT);
688 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
690 INP_INFO_RUNLOCK(&V_tcbinfo);
696 * Accept a connection. Essentially all the work is done at higher levels;
697 * just return the address of the peer, storing through addr.
700 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
703 struct inpcb *inp = NULL;
704 struct tcpcb *tp = NULL;
709 if (so->so_state & SS_ISDISCONNECTED)
710 return (ECONNABORTED);
713 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
715 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
716 error = ECONNABORTED;
723 * We inline in_getpeeraddr and COMMON_END here, so that we can
724 * copy the data of interest and defer the malloc until after we
727 port = inp->inp_fport;
728 addr = inp->inp_faddr;
731 TCPDEBUG2(PRU_ACCEPT);
732 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
735 *nam = in_sockaddr(port, &addr);
742 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
744 struct inpcb *inp = NULL;
746 struct tcpcb *tp = NULL;
748 struct in6_addr addr6;
753 if (so->so_state & SS_ISDISCONNECTED)
754 return (ECONNABORTED);
757 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
758 INP_INFO_RLOCK(&V_tcbinfo);
760 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
761 error = ECONNABORTED;
768 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
769 * copy the data of interest and defer the malloc until after we
772 if (inp->inp_vflag & INP_IPV4) {
774 port = inp->inp_fport;
775 addr = inp->inp_faddr;
777 port = inp->inp_fport;
778 addr6 = inp->in6p_faddr;
782 TCPDEBUG2(PRU_ACCEPT);
783 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
785 INP_INFO_RUNLOCK(&V_tcbinfo);
788 *nam = in6_v4mapsin6_sockaddr(port, &addr);
790 *nam = in6_sockaddr(port, &addr6);
797 * Mark the connection as being incapable of further output.
800 tcp_usr_shutdown(struct socket *so)
804 struct tcpcb *tp = NULL;
807 INP_INFO_RLOCK(&V_tcbinfo);
809 KASSERT(inp != NULL, ("inp == NULL"));
811 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
819 if (!(inp->inp_flags & INP_DROPPED))
820 error = tp->t_fb->tfb_tcp_output(tp);
823 TCPDEBUG2(PRU_SHUTDOWN);
824 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
826 INP_INFO_RUNLOCK(&V_tcbinfo);
832 * After a receive, possibly send window update to peer.
835 tcp_usr_rcvd(struct socket *so, int flags)
838 struct tcpcb *tp = NULL;
843 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
845 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
852 * For passively-created TFO connections, don't attempt a window
853 * update while still in SYN_RECEIVED as this may trigger an early
854 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
855 * application response data, or failing that, when the DELACK timer
858 if (IS_FASTOPEN(tp->t_flags) &&
859 (tp->t_state == TCPS_SYN_RECEIVED))
862 if (tp->t_flags & TF_TOE)
863 tcp_offload_rcvd(tp);
866 tp->t_fb->tfb_tcp_output(tp);
870 TCP_PROBE2(debug__user, tp, PRU_RCVD);
876 * Do a send by putting data in output queue and updating urgent
877 * marker if URG set. Possibly send more data. Unlike the other
878 * pru_*() routines, the mbuf chains are our responsibility. We
879 * must either enqueue them or free them. The other pru_* routines
880 * generally are caller-frees.
883 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
884 struct sockaddr *nam, struct mbuf *control, struct thread *td)
888 struct tcpcb *tp = NULL;
895 * We require the pcbinfo lock if we will close the socket as part of
898 if (flags & PRUS_EOF)
899 INP_INFO_RLOCK(&V_tcbinfo);
901 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
903 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
907 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
908 * for freeing memory.
910 if (m && (flags & PRUS_NOTREADY) == 0)
916 isipv6 = nam && nam->sa_family == AF_INET6;
921 /* TCP doesn't do control messages (rights, creds, etc) */
922 if (control->m_len) {
929 m_freem(control); /* empty control, just free it */
931 if (!(flags & PRUS_OOB)) {
932 sbappendstream(&so->so_snd, m, flags);
933 if (nam && tp->t_state < TCPS_SYN_SENT) {
935 * Do implied connect if not yet connected,
936 * initialize window to default value, and
937 * initialize maxseg using peer's cached MSS.
941 error = tcp6_connect(tp, nam, td);
943 #if defined(INET6) && defined(INET)
947 error = tcp_connect(tp, nam, td);
951 if (IS_FASTOPEN(tp->t_flags))
952 tcp_fastopen_connect(tp);
954 tp->snd_wnd = TTCP_CLIENT_SND_WND;
958 if (flags & PRUS_EOF) {
960 * Close the send side of the connection after
963 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
967 if (!(inp->inp_flags & INP_DROPPED) &&
968 !(flags & PRUS_NOTREADY)) {
969 if (flags & PRUS_MORETOCOME)
970 tp->t_flags |= TF_MORETOCOME;
971 error = tp->t_fb->tfb_tcp_output(tp);
972 if (flags & PRUS_MORETOCOME)
973 tp->t_flags &= ~TF_MORETOCOME;
977 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
979 SOCKBUF_LOCK(&so->so_snd);
980 if (sbspace(&so->so_snd) < -512) {
981 SOCKBUF_UNLOCK(&so->so_snd);
987 * According to RFC961 (Assigned Protocols),
988 * the urgent pointer points to the last octet
989 * of urgent data. We continue, however,
990 * to consider it to indicate the first octet
991 * of data past the urgent section.
992 * Otherwise, snd_up should be one lower.
994 sbappendstream_locked(&so->so_snd, m, flags);
995 SOCKBUF_UNLOCK(&so->so_snd);
996 if (nam && tp->t_state < TCPS_SYN_SENT) {
998 * Do implied connect if not yet connected,
999 * initialize window to default value, and
1000 * initialize maxseg using peer's cached MSS.
1004 * Not going to contemplate SYN|URG
1006 if (IS_FASTOPEN(tp->t_flags))
1007 tp->t_flags &= ~TF_FASTOPEN;
1010 error = tcp6_connect(tp, nam, td);
1012 #if defined(INET6) && defined(INET)
1016 error = tcp_connect(tp, nam, td);
1020 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1023 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1024 if (!(flags & PRUS_NOTREADY)) {
1025 tp->t_flags |= TF_FORCEDATA;
1026 error = tp->t_fb->tfb_tcp_output(tp);
1027 tp->t_flags &= ~TF_FORCEDATA;
1030 TCP_LOG_EVENT(tp, NULL,
1031 &inp->inp_socket->so_rcv,
1032 &inp->inp_socket->so_snd,
1033 TCP_LOG_USERSEND, error,
1036 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
1037 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1038 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1039 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1041 if (flags & PRUS_EOF)
1042 INP_INFO_RUNLOCK(&V_tcbinfo);
1047 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1053 inp = sotoinpcb(so);
1055 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1057 for (int i = 0; i < count; i++)
1059 return (ECONNRESET);
1061 tp = intotcpcb(inp);
1063 SOCKBUF_LOCK(&so->so_snd);
1064 error = sbready(&so->so_snd, m, count);
1065 SOCKBUF_UNLOCK(&so->so_snd);
1067 error = tp->t_fb->tfb_tcp_output(tp);
1074 * Abort the TCP. Drop the connection abruptly.
1077 tcp_usr_abort(struct socket *so)
1080 struct tcpcb *tp = NULL;
1083 inp = sotoinpcb(so);
1084 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1086 INP_INFO_RLOCK(&V_tcbinfo);
1088 KASSERT(inp->inp_socket != NULL,
1089 ("tcp_usr_abort: inp_socket == NULL"));
1092 * If we still have full TCP state, and we're not dropped, drop.
1094 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1095 !(inp->inp_flags & INP_DROPPED)) {
1096 tp = intotcpcb(inp);
1098 tp = tcp_drop(tp, ECONNABORTED);
1101 TCPDEBUG2(PRU_ABORT);
1102 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1104 if (!(inp->inp_flags & INP_DROPPED)) {
1106 so->so_state |= SS_PROTOREF;
1108 inp->inp_flags |= INP_SOCKREF;
1112 INP_INFO_RUNLOCK(&V_tcbinfo);
1116 * TCP socket is closed. Start friendly disconnect.
1119 tcp_usr_close(struct socket *so)
1122 struct tcpcb *tp = NULL;
1125 inp = sotoinpcb(so);
1126 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1128 INP_INFO_RLOCK(&V_tcbinfo);
1130 KASSERT(inp->inp_socket != NULL,
1131 ("tcp_usr_close: inp_socket == NULL"));
1134 * If we still have full TCP state, and we're not dropped, initiate
1137 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1138 !(inp->inp_flags & INP_DROPPED)) {
1139 tp = intotcpcb(inp);
1142 TCPDEBUG2(PRU_CLOSE);
1143 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1145 if (!(inp->inp_flags & INP_DROPPED)) {
1147 so->so_state |= SS_PROTOREF;
1149 inp->inp_flags |= INP_SOCKREF;
1152 INP_INFO_RUNLOCK(&V_tcbinfo);
1156 * Receive out-of-band data.
1159 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1163 struct tcpcb *tp = NULL;
1166 inp = sotoinpcb(so);
1167 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1169 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1173 tp = intotcpcb(inp);
1175 if ((so->so_oobmark == 0 &&
1176 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1177 so->so_options & SO_OOBINLINE ||
1178 tp->t_oobflags & TCPOOB_HADDATA) {
1182 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1183 error = EWOULDBLOCK;
1187 *mtod(m, caddr_t) = tp->t_iobc;
1188 if ((flags & MSG_PEEK) == 0)
1189 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1192 TCPDEBUG2(PRU_RCVOOB);
1193 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1199 struct pr_usrreqs tcp_usrreqs = {
1200 .pru_abort = tcp_usr_abort,
1201 .pru_accept = tcp_usr_accept,
1202 .pru_attach = tcp_usr_attach,
1203 .pru_bind = tcp_usr_bind,
1204 .pru_connect = tcp_usr_connect,
1205 .pru_control = in_control,
1206 .pru_detach = tcp_usr_detach,
1207 .pru_disconnect = tcp_usr_disconnect,
1208 .pru_listen = tcp_usr_listen,
1209 .pru_peeraddr = in_getpeeraddr,
1210 .pru_rcvd = tcp_usr_rcvd,
1211 .pru_rcvoob = tcp_usr_rcvoob,
1212 .pru_send = tcp_usr_send,
1213 .pru_ready = tcp_usr_ready,
1214 .pru_shutdown = tcp_usr_shutdown,
1215 .pru_sockaddr = in_getsockaddr,
1216 .pru_sosetlabel = in_pcbsosetlabel,
1217 .pru_close = tcp_usr_close,
1222 struct pr_usrreqs tcp6_usrreqs = {
1223 .pru_abort = tcp_usr_abort,
1224 .pru_accept = tcp6_usr_accept,
1225 .pru_attach = tcp_usr_attach,
1226 .pru_bind = tcp6_usr_bind,
1227 .pru_connect = tcp6_usr_connect,
1228 .pru_control = in6_control,
1229 .pru_detach = tcp_usr_detach,
1230 .pru_disconnect = tcp_usr_disconnect,
1231 .pru_listen = tcp6_usr_listen,
1232 .pru_peeraddr = in6_mapped_peeraddr,
1233 .pru_rcvd = tcp_usr_rcvd,
1234 .pru_rcvoob = tcp_usr_rcvoob,
1235 .pru_send = tcp_usr_send,
1236 .pru_ready = tcp_usr_ready,
1237 .pru_shutdown = tcp_usr_shutdown,
1238 .pru_sockaddr = in6_mapped_sockaddr,
1239 .pru_sosetlabel = in_pcbsosetlabel,
1240 .pru_close = tcp_usr_close,
1246 * Common subroutine to open a TCP connection to remote host specified
1247 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1248 * port number if needed. Call in_pcbconnect_setup to do the routing and
1249 * to choose a local host address (interface). If there is an existing
1250 * incarnation of the same connection in TIME-WAIT state and if the remote
1251 * host was sending CC options and if the connection duration was < MSL, then
1252 * truncate the previous TIME-WAIT state and proceed.
1253 * Initialize connection parameters and enter SYN-SENT state.
1256 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1258 struct inpcb *inp = tp->t_inpcb, *oinp;
1259 struct socket *so = inp->inp_socket;
1260 struct in_addr laddr;
1264 INP_WLOCK_ASSERT(inp);
1265 INP_HASH_WLOCK(&V_tcbinfo);
1267 if (inp->inp_lport == 0) {
1268 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1274 * Cannot simply call in_pcbconnect, because there might be an
1275 * earlier incarnation of this same connection still in
1276 * TIME_WAIT state, creating an ADDRINUSE error.
1278 laddr = inp->inp_laddr;
1279 lport = inp->inp_lport;
1280 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1281 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1282 if (error && oinp == NULL)
1288 inp->inp_laddr = laddr;
1290 INP_HASH_WUNLOCK(&V_tcbinfo);
1293 * Compute window scaling to request:
1294 * Scale to fit into sweet spot. See tcp_syncache.c.
1295 * XXX: This should move to tcp_output().
1297 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1298 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1299 tp->request_r_scale++;
1302 TCPSTAT_INC(tcps_connattempt);
1303 tcp_state_change(tp, TCPS_SYN_SENT);
1304 tp->iss = tcp_new_isn(tp);
1305 tcp_sendseqinit(tp);
1310 INP_HASH_WUNLOCK(&V_tcbinfo);
1317 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1319 struct inpcb *inp = tp->t_inpcb;
1322 INP_WLOCK_ASSERT(inp);
1323 INP_HASH_WLOCK(&V_tcbinfo);
1325 if (inp->inp_lport == 0) {
1326 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1330 error = in6_pcbconnect(inp, nam, td->td_ucred);
1333 INP_HASH_WUNLOCK(&V_tcbinfo);
1335 /* Compute window scaling to request. */
1336 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1337 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1338 tp->request_r_scale++;
1340 soisconnecting(inp->inp_socket);
1341 TCPSTAT_INC(tcps_connattempt);
1342 tcp_state_change(tp, TCPS_SYN_SENT);
1343 tp->iss = tcp_new_isn(tp);
1344 tcp_sendseqinit(tp);
1349 INP_HASH_WUNLOCK(&V_tcbinfo);
1355 * Export TCP internal state information via a struct tcp_info, based on the
1356 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1357 * (TCP state machine, etc). We export all information using FreeBSD-native
1358 * constants -- for example, the numeric values for tcpi_state will differ
1362 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1365 INP_WLOCK_ASSERT(tp->t_inpcb);
1366 bzero(ti, sizeof(*ti));
1368 ti->tcpi_state = tp->t_state;
1369 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1370 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1371 if (tp->t_flags & TF_SACK_PERMIT)
1372 ti->tcpi_options |= TCPI_OPT_SACK;
1373 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1374 ti->tcpi_options |= TCPI_OPT_WSCALE;
1375 ti->tcpi_snd_wscale = tp->snd_scale;
1376 ti->tcpi_rcv_wscale = tp->rcv_scale;
1378 if (tp->t_flags & TF_ECN_PERMIT)
1379 ti->tcpi_options |= TCPI_OPT_ECN;
1381 ti->tcpi_rto = tp->t_rxtcur * tick;
1382 ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1383 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1384 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1386 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1387 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1390 * FreeBSD-specific extension fields for tcp_info.
1392 ti->tcpi_rcv_space = tp->rcv_wnd;
1393 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1394 ti->tcpi_snd_wnd = tp->snd_wnd;
1395 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1396 ti->tcpi_snd_nxt = tp->snd_nxt;
1397 ti->tcpi_snd_mss = tp->t_maxseg;
1398 ti->tcpi_rcv_mss = tp->t_maxseg;
1399 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1400 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1401 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1403 if (tp->t_flags & TF_TOE) {
1404 ti->tcpi_options |= TCPI_OPT_TOE;
1405 tcp_offload_tcp_info(tp, ti);
1411 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1412 * socket option arguments. When it re-acquires the lock after the copy, it
1413 * has to revalidate that the connection is still valid for the socket
1416 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \
1418 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1421 return (ECONNRESET); \
1423 tp = intotcpcb(inp); \
1425 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1428 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1433 struct tcp_function_block *blk;
1434 struct tcp_function_set fsn;
1437 inp = sotoinpcb(so);
1438 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1440 if (sopt->sopt_level != IPPROTO_TCP) {
1442 if (inp->inp_vflag & INP_IPV6PROTO) {
1444 error = ip6_ctloutput(so, sopt);
1447 #if defined(INET6) && defined(INET)
1453 error = ip_ctloutput(so, sopt);
1458 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1460 return (ECONNRESET);
1462 tp = intotcpcb(inp);
1464 * Protect the TCP option TCP_FUNCTION_BLK so
1465 * that a sub-function can *never* overwrite this.
1467 if ((sopt->sopt_dir == SOPT_SET) &&
1468 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1470 error = sooptcopyin(sopt, &fsn, sizeof fsn,
1474 INP_WLOCK_RECHECK(inp);
1475 blk = find_and_ref_tcp_functions(&fsn);
1480 if (tp->t_fb == blk) {
1481 /* You already have this */
1482 refcount_release(&blk->tfb_refcnt);
1486 if (tp->t_state != TCPS_CLOSED) {
1489 * The user has advanced the state
1490 * past the initial point, we may not
1491 * be able to switch.
1493 if (blk->tfb_tcp_handoff_ok != NULL) {
1495 * Does the stack provide a
1496 * query mechanism, if so it may
1497 * still be possible?
1499 error = (*blk->tfb_tcp_handoff_ok)(tp);
1502 refcount_release(&blk->tfb_refcnt);
1507 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1508 refcount_release(&blk->tfb_refcnt);
1513 * Release the old refcnt, the
1514 * lookup acquired a ref on the
1517 if (tp->t_fb->tfb_tcp_fb_fini) {
1519 * Tell the stack to cleanup with 0 i.e.
1520 * the tcb is not going away.
1522 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1525 /* Assure that we are not on any hpts */
1526 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
1528 if (blk->tfb_tcp_fb_init) {
1529 error = (*blk->tfb_tcp_fb_init)(tp);
1531 refcount_release(&blk->tfb_refcnt);
1532 if (tp->t_fb->tfb_tcp_fb_init) {
1533 if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) {
1534 /* Fall back failed, drop the connection */
1543 refcount_release(&tp->t_fb->tfb_refcnt);
1546 if (tp->t_flags & TF_TOE) {
1547 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1554 } else if ((sopt->sopt_dir == SOPT_GET) &&
1555 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1556 strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1557 TCP_FUNCTION_NAME_LEN_MAX);
1558 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1559 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1561 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1564 /* Pass in the INP locked, called must unlock it */
1565 return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
1569 * If this assert becomes untrue, we need to change the size of the buf
1570 * variable in tcp_default_ctloutput().
1573 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1574 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1578 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
1580 int error, opt, optval;
1583 struct cc_algo *algo;
1584 char *pbuf, buf[TCP_LOG_ID_LEN];
1588 * For TCP_CCALGOOPT forward the control to CC module, for both
1589 * SOPT_SET and SOPT_GET.
1591 switch (sopt->sopt_name) {
1594 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1595 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1596 sopt->sopt_valsize);
1601 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1602 if (CC_ALGO(tp)->ctl_output != NULL)
1603 error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1607 if (error == 0 && sopt->sopt_dir == SOPT_GET)
1608 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1613 switch (sopt->sopt_dir) {
1615 switch (sopt->sopt_name) {
1616 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1618 if (!TCPMD5_ENABLED()) {
1620 return (ENOPROTOOPT);
1622 error = TCPMD5_PCBCTL(inp, sopt);
1625 goto unlock_and_done;
1631 error = sooptcopyin(sopt, &optval, sizeof optval,
1636 INP_WLOCK_RECHECK(inp);
1637 switch (sopt->sopt_name) {
1645 opt = 0; /* dead code to fool gcc */
1652 tp->t_flags &= ~opt;
1655 if (tp->t_flags & TF_TOE) {
1656 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1665 error = sooptcopyin(sopt, &optval, sizeof optval,
1670 INP_WLOCK_RECHECK(inp);
1672 tp->t_flags |= TF_NOPUSH;
1673 else if (tp->t_flags & TF_NOPUSH) {
1674 tp->t_flags &= ~TF_NOPUSH;
1675 if (TCPS_HAVEESTABLISHED(tp->t_state))
1676 error = tp->t_fb->tfb_tcp_output(tp);
1678 goto unlock_and_done;
1682 error = sooptcopyin(sopt, &optval, sizeof optval,
1687 INP_WLOCK_RECHECK(inp);
1688 if (optval > 0 && optval <= tp->t_maxseg &&
1689 optval + 40 >= V_tcp_minmss)
1690 tp->t_maxseg = optval;
1693 goto unlock_and_done;
1700 case TCP_CONGESTION:
1702 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1705 buf[sopt->sopt_valsize] = '\0';
1706 INP_WLOCK_RECHECK(inp);
1708 STAILQ_FOREACH(algo, &cc_list, entries)
1709 if (strncmp(buf, algo->name,
1710 TCP_CA_NAME_MAX) == 0)
1719 * We hold a write lock over the tcb so it's safe to
1720 * do these things without ordering concerns.
1722 if (CC_ALGO(tp)->cb_destroy != NULL)
1723 CC_ALGO(tp)->cb_destroy(tp->ccv);
1726 * If something goes pear shaped initialising the new
1727 * algo, fall back to newreno (which does not
1728 * require initialisation).
1730 if (algo->cb_init != NULL &&
1731 algo->cb_init(tp->ccv) != 0) {
1732 CC_ALGO(tp) = &newreno_cc_algo;
1734 * The only reason init should fail is
1735 * because of malloc.
1746 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1750 if (ui > (UINT_MAX / hz)) {
1756 INP_WLOCK_RECHECK(inp);
1757 switch (sopt->sopt_name) {
1759 tp->t_keepidle = ui;
1761 * XXX: better check current remaining
1762 * timeout and "merge" it with new value.
1764 if ((tp->t_state > TCPS_LISTEN) &&
1765 (tp->t_state <= TCPS_CLOSING))
1766 tcp_timer_activate(tp, TT_KEEP,
1770 tp->t_keepintvl = ui;
1771 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1772 (TP_MAXIDLE(tp) > 0))
1773 tcp_timer_activate(tp, TT_2MSL,
1777 tp->t_keepinit = ui;
1778 if (tp->t_state == TCPS_SYN_RECEIVED ||
1779 tp->t_state == TCPS_SYN_SENT)
1780 tcp_timer_activate(tp, TT_KEEP,
1784 goto unlock_and_done;
1788 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1792 INP_WLOCK_RECHECK(inp);
1794 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1795 (TP_MAXIDLE(tp) > 0))
1796 tcp_timer_activate(tp, TT_2MSL,
1798 goto unlock_and_done;
1804 error = sooptcopyin(sopt, &optval, sizeof optval,
1809 INP_WLOCK_RECHECK(inp);
1811 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
1812 &(tp->t_outpkts) : &(tp->t_inpkts),
1816 goto unlock_and_done;
1819 case TCP_FASTOPEN: {
1820 struct tcp_fastopen tfo_optval;
1823 if (!V_tcp_fastopen_client_enable &&
1824 !V_tcp_fastopen_server_enable)
1827 error = sooptcopyin(sopt, &tfo_optval,
1828 sizeof(tfo_optval), sizeof(int));
1832 INP_WLOCK_RECHECK(inp);
1833 if (tfo_optval.enable) {
1834 if (tp->t_state == TCPS_LISTEN) {
1835 if (!V_tcp_fastopen_server_enable) {
1837 goto unlock_and_done;
1840 tp->t_flags |= TF_FASTOPEN;
1841 if (tp->t_tfo_pending == NULL)
1843 tcp_fastopen_alloc_counter();
1846 * If a pre-shared key was provided,
1847 * stash it in the client cookie
1848 * field of the tcpcb for use during
1851 if (sopt->sopt_valsize ==
1852 sizeof(tfo_optval)) {
1853 memcpy(tp->t_tfo_cookie.client,
1855 TCP_FASTOPEN_PSK_LEN);
1856 tp->t_tfo_client_cookie_len =
1857 TCP_FASTOPEN_PSK_LEN;
1859 tp->t_flags |= TF_FASTOPEN;
1862 tp->t_flags &= ~TF_FASTOPEN;
1863 goto unlock_and_done;
1869 error = sooptcopyin(sopt, &optval, sizeof optval,
1874 INP_WLOCK_RECHECK(inp);
1875 error = tcp_log_state_change(tp, optval);
1876 goto unlock_and_done;
1885 error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
1888 buf[sopt->sopt_valsize] = '\0';
1889 INP_WLOCK_RECHECK(inp);
1890 error = tcp_log_set_id(tp, buf);
1891 /* tcp_log_set_id() unlocks the INP. */
1898 sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
1901 buf[sopt->sopt_valsize] = '\0';
1902 INP_WLOCK_RECHECK(inp);
1903 if (sopt->sopt_name == TCP_LOGDUMP) {
1904 error = tcp_log_dump_tp_logbuf(tp, buf,
1908 tcp_log_dump_tp_bucket_logbufs(tp, buf);
1910 * tcp_log_dump_tp_bucket_logbufs() drops the
1919 error = ENOPROTOOPT;
1925 tp = intotcpcb(inp);
1926 switch (sopt->sopt_name) {
1927 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1929 if (!TCPMD5_ENABLED()) {
1931 return (ENOPROTOOPT);
1933 error = TCPMD5_PCBCTL(inp, sopt);
1938 optval = tp->t_flags & TF_NODELAY;
1940 error = sooptcopyout(sopt, &optval, sizeof optval);
1943 optval = tp->t_maxseg;
1945 error = sooptcopyout(sopt, &optval, sizeof optval);
1948 optval = tp->t_flags & TF_NOOPT;
1950 error = sooptcopyout(sopt, &optval, sizeof optval);
1953 optval = tp->t_flags & TF_NOPUSH;
1955 error = sooptcopyout(sopt, &optval, sizeof optval);
1958 tcp_fill_info(tp, &ti);
1960 error = sooptcopyout(sopt, &ti, sizeof ti);
1962 case TCP_CONGESTION:
1963 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1965 error = sooptcopyout(sopt, buf, len + 1);
1971 switch (sopt->sopt_name) {
1973 ui = TP_KEEPIDLE(tp) / hz;
1976 ui = TP_KEEPINTVL(tp) / hz;
1979 ui = TP_KEEPINIT(tp) / hz;
1982 ui = TP_KEEPCNT(tp);
1986 error = sooptcopyout(sopt, &ui, sizeof(ui));
1991 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
1992 &(tp->t_outpkts) : &(tp->t_inpkts));
1994 error = sooptcopyout(sopt, &optval, sizeof optval);
1998 optval = tp->t_flags & TF_FASTOPEN;
2000 error = sooptcopyout(sopt, &optval, sizeof optval);
2004 optval = tp->t_logstate;
2006 error = sooptcopyout(sopt, &optval, sizeof(optval));
2009 /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2010 error = tcp_log_getlogbuf(sopt, tp);
2013 len = tcp_log_get_id(tp, buf);
2015 error = sooptcopyout(sopt, buf, len + 1);
2025 error = ENOPROTOOPT;
2032 #undef INP_WLOCK_RECHECK
2033 #undef INP_WLOCK_RECHECK_CLEANUP
2036 * Attach TCP protocol to socket, allocating
2037 * internet protocol control block, tcp control block,
2038 * bufer space, and entering LISTEN state if to accept connections.
2041 tcp_attach(struct socket *so)
2047 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
2048 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
2052 so->so_rcv.sb_flags |= SB_AUTOSIZE;
2053 so->so_snd.sb_flags |= SB_AUTOSIZE;
2054 INP_INFO_RLOCK(&V_tcbinfo);
2055 error = in_pcballoc(so, &V_tcbinfo);
2057 INP_INFO_RUNLOCK(&V_tcbinfo);
2060 inp = sotoinpcb(so);
2062 if (inp->inp_vflag & INP_IPV6PROTO) {
2063 inp->inp_vflag |= INP_IPV6;
2064 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
2065 inp->inp_vflag |= INP_IPV4;
2066 inp->in6p_hops = -1; /* use kernel default */
2070 inp->inp_vflag |= INP_IPV4;
2071 tp = tcp_newtcpcb(inp);
2075 INP_INFO_RUNLOCK(&V_tcbinfo);
2078 tp->t_state = TCPS_CLOSED;
2080 INP_INFO_RUNLOCK(&V_tcbinfo);
2081 TCPSTATES_INC(TCPS_CLOSED);
2086 * Initiate (or continue) disconnect.
2087 * If embryonic state, just send reset (once).
2088 * If in ``let data drain'' option and linger null, just drop.
2089 * Otherwise (hard), mark socket disconnecting and drop
2090 * current input data; switch states based on user close, and
2091 * send segment to peer (with FIN).
2094 tcp_disconnect(struct tcpcb *tp)
2096 struct inpcb *inp = tp->t_inpcb;
2097 struct socket *so = inp->inp_socket;
2099 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2100 INP_WLOCK_ASSERT(inp);
2103 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2104 * socket is still open.
2106 if (tp->t_state < TCPS_ESTABLISHED) {
2109 ("tcp_disconnect: tcp_close() returned NULL"));
2110 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2111 tp = tcp_drop(tp, 0);
2113 ("tcp_disconnect: tcp_drop() returned NULL"));
2115 soisdisconnecting(so);
2116 sbflush(&so->so_rcv);
2118 if (!(inp->inp_flags & INP_DROPPED))
2119 tp->t_fb->tfb_tcp_output(tp);
2124 * User issued close, and wish to trail through shutdown states:
2125 * if never received SYN, just forget it. If got a SYN from peer,
2126 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2127 * If already got a FIN from peer, then almost done; go to LAST_ACK
2128 * state. In all other cases, have already sent FIN to peer (e.g.
2129 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2130 * for peer to send FIN or not respond to keep-alives, etc.
2131 * We can let the user exit from the close as soon as the FIN is acked.
2134 tcp_usrclosed(struct tcpcb *tp)
2137 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2138 INP_WLOCK_ASSERT(tp->t_inpcb);
2140 switch (tp->t_state) {
2143 tcp_offload_listen_stop(tp);
2145 tcp_state_change(tp, TCPS_CLOSED);
2150 * tcp_close() should never return NULL here as the socket is
2154 ("tcp_usrclosed: tcp_close() returned NULL"));
2158 case TCPS_SYN_RECEIVED:
2159 tp->t_flags |= TF_NEEDFIN;
2162 case TCPS_ESTABLISHED:
2163 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2166 case TCPS_CLOSE_WAIT:
2167 tcp_state_change(tp, TCPS_LAST_ACK);
2170 if (tp->t_state >= TCPS_FIN_WAIT_2) {
2171 soisdisconnected(tp->t_inpcb->inp_socket);
2172 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
2173 if (tp->t_state == TCPS_FIN_WAIT_2) {
2176 timeout = (tcp_fast_finwait2_recycle) ?
2177 tcp_finwait2_timeout : TP_MAXIDLE(tp);
2178 tcp_timer_activate(tp, TT_2MSL, timeout);
2185 db_print_indent(int indent)
2189 for (i = 0; i < indent; i++)
2194 db_print_tstate(int t_state)
2199 db_printf("TCPS_CLOSED");
2203 db_printf("TCPS_LISTEN");
2207 db_printf("TCPS_SYN_SENT");
2210 case TCPS_SYN_RECEIVED:
2211 db_printf("TCPS_SYN_RECEIVED");
2214 case TCPS_ESTABLISHED:
2215 db_printf("TCPS_ESTABLISHED");
2218 case TCPS_CLOSE_WAIT:
2219 db_printf("TCPS_CLOSE_WAIT");
2222 case TCPS_FIN_WAIT_1:
2223 db_printf("TCPS_FIN_WAIT_1");
2227 db_printf("TCPS_CLOSING");
2231 db_printf("TCPS_LAST_ACK");
2234 case TCPS_FIN_WAIT_2:
2235 db_printf("TCPS_FIN_WAIT_2");
2238 case TCPS_TIME_WAIT:
2239 db_printf("TCPS_TIME_WAIT");
2243 db_printf("unknown");
2249 db_print_tflags(u_int t_flags)
2254 if (t_flags & TF_ACKNOW) {
2255 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2258 if (t_flags & TF_DELACK) {
2259 db_printf("%sTF_DELACK", comma ? ", " : "");
2262 if (t_flags & TF_NODELAY) {
2263 db_printf("%sTF_NODELAY", comma ? ", " : "");
2266 if (t_flags & TF_NOOPT) {
2267 db_printf("%sTF_NOOPT", comma ? ", " : "");
2270 if (t_flags & TF_SENTFIN) {
2271 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2274 if (t_flags & TF_REQ_SCALE) {
2275 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2278 if (t_flags & TF_RCVD_SCALE) {
2279 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2282 if (t_flags & TF_REQ_TSTMP) {
2283 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2286 if (t_flags & TF_RCVD_TSTMP) {
2287 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2290 if (t_flags & TF_SACK_PERMIT) {
2291 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2294 if (t_flags & TF_NEEDSYN) {
2295 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2298 if (t_flags & TF_NEEDFIN) {
2299 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2302 if (t_flags & TF_NOPUSH) {
2303 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2306 if (t_flags & TF_MORETOCOME) {
2307 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2310 if (t_flags & TF_LQ_OVERFLOW) {
2311 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2314 if (t_flags & TF_LASTIDLE) {
2315 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2318 if (t_flags & TF_RXWIN0SENT) {
2319 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2322 if (t_flags & TF_FASTRECOVERY) {
2323 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2326 if (t_flags & TF_CONGRECOVERY) {
2327 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2330 if (t_flags & TF_WASFRECOVERY) {
2331 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2334 if (t_flags & TF_SIGNATURE) {
2335 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2338 if (t_flags & TF_FORCEDATA) {
2339 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2342 if (t_flags & TF_TSO) {
2343 db_printf("%sTF_TSO", comma ? ", " : "");
2346 if (t_flags & TF_ECN_PERMIT) {
2347 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2350 if (t_flags & TF_FASTOPEN) {
2351 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2357 db_print_toobflags(char t_oobflags)
2362 if (t_oobflags & TCPOOB_HAVEDATA) {
2363 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2366 if (t_oobflags & TCPOOB_HADDATA) {
2367 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2373 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2376 db_print_indent(indent);
2377 db_printf("%s at %p\n", name, tp);
2381 db_print_indent(indent);
2382 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2383 LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2385 db_print_indent(indent);
2386 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
2387 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2389 db_print_indent(indent);
2390 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2391 &tp->t_timers->tt_delack, tp->t_inpcb);
2393 db_print_indent(indent);
2394 db_printf("t_state: %d (", tp->t_state);
2395 db_print_tstate(tp->t_state);
2398 db_print_indent(indent);
2399 db_printf("t_flags: 0x%x (", tp->t_flags);
2400 db_print_tflags(tp->t_flags);
2403 db_print_indent(indent);
2404 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2405 tp->snd_una, tp->snd_max, tp->snd_nxt);
2407 db_print_indent(indent);
2408 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2409 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2411 db_print_indent(indent);
2412 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2413 tp->iss, tp->irs, tp->rcv_nxt);
2415 db_print_indent(indent);
2416 db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n",
2417 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2419 db_print_indent(indent);
2420 db_printf("snd_wnd: %u snd_cwnd: %u\n",
2421 tp->snd_wnd, tp->snd_cwnd);
2423 db_print_indent(indent);
2424 db_printf("snd_ssthresh: %u snd_recover: "
2425 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2427 db_print_indent(indent);
2428 db_printf("t_rcvtime: %u t_startime: %u\n",
2429 tp->t_rcvtime, tp->t_starttime);
2431 db_print_indent(indent);
2432 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2433 tp->t_rtttime, tp->t_rtseq);
2435 db_print_indent(indent);
2436 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2437 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2439 db_print_indent(indent);
2440 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2441 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2444 db_print_indent(indent);
2445 db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n",
2446 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2448 db_print_indent(indent);
2449 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2450 db_print_toobflags(tp->t_oobflags);
2451 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2453 db_print_indent(indent);
2454 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2455 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2457 db_print_indent(indent);
2458 db_printf("ts_recent: %u ts_recent_age: %u\n",
2459 tp->ts_recent, tp->ts_recent_age);
2461 db_print_indent(indent);
2462 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2463 "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2465 db_print_indent(indent);
2466 db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x "
2467 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2468 tp->snd_recover_prev, tp->t_badrxtwin);
2470 db_print_indent(indent);
2471 db_printf("snd_numholes: %d snd_holes first: %p\n",
2472 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2474 db_print_indent(indent);
2475 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
2476 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2478 /* Skip sackblks, sackhint. */
2480 db_print_indent(indent);
2481 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2482 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2485 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2490 db_printf("usage: show tcpcb <addr>\n");
2493 tp = (struct tcpcb *)addr;
2495 db_print_tcpcb(tp, "tcpcb", 0);