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>
97 #include <netinet/tcp_hpts.h>
99 #include <netinet/tcp_pcap.h>
102 #include <netinet/tcp_debug.h>
105 #include <netinet/tcp_offload.h>
107 #include <netipsec/ipsec_support.h>
110 * TCP protocol interface to socket abstraction.
112 static int tcp_attach(struct socket *);
114 static int tcp_connect(struct tcpcb *, struct sockaddr *,
118 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
121 static void tcp_disconnect(struct tcpcb *);
122 static void tcp_usrclosed(struct tcpcb *);
123 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
126 #define TCPDEBUG0 int ostate = 0
127 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
128 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
129 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
133 #define TCPDEBUG2(req)
137 * TCP attaches to socket via pru_attach(), reserving space,
138 * and an internet control block.
141 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
144 struct tcpcb *tp = NULL;
149 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
152 error = tcp_attach(so);
156 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
157 so->so_linger = TCP_LINGERTIME;
162 TCPDEBUG2(PRU_ATTACH);
163 TCP_PROBE2(debug__user, tp, PRU_ATTACH);
168 * tcp_detach is called when the socket layer loses its final reference
169 * to the socket, be it a file descriptor reference, a reference from TCP,
170 * etc. At this point, there is only one case in which we will keep around
171 * inpcb state: time wait.
173 * This function can probably be re-absorbed back into tcp_usr_detach() now
174 * that there is a single detach path.
177 tcp_detach(struct socket *so, struct inpcb *inp)
181 INP_INFO_LOCK_ASSERT(&V_tcbinfo);
182 INP_WLOCK_ASSERT(inp);
184 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
185 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
189 if (inp->inp_flags & INP_TIMEWAIT) {
191 * There are two cases to handle: one in which the time wait
192 * state is being discarded (INP_DROPPED), and one in which
193 * this connection will remain in timewait. In the former,
194 * it is time to discard all state (except tcptw, which has
195 * already been discarded by the timewait close code, which
196 * should be further up the call stack somewhere). In the
197 * latter case, we detach from the socket, but leave the pcb
198 * present until timewait ends.
200 * XXXRW: Would it be cleaner to free the tcptw here?
202 * Astute question indeed, from twtcp perspective there are
203 * four cases to consider:
205 * #1 tcp_detach is called at tcptw creation time by
206 * tcp_twstart, then do not discard the newly created tcptw
207 * and leave inpcb present until timewait ends
208 * #2 tcp_detach is called at tcptw creation time by
209 * tcp_twstart, but connection is local and tw will be
210 * discarded immediately
211 * #3 tcp_detach is called at timewait end (or reuse) by
212 * tcp_twclose, then the tcptw has already been discarded
213 * (or reused) and inpcb is freed here
214 * #4 tcp_detach is called() after timewait ends (or reuse)
215 * (e.g. by soclose), then tcptw has already been discarded
216 * (or reused) and inpcb is freed here
218 * In all three cases the tcptw should not be freed here.
220 if (inp->inp_flags & INP_DROPPED) {
222 if (__predict_true(tp == NULL)) {
226 * This case should not happen as in TIMEWAIT
227 * state the inp should not be destroyed before
228 * its tcptw. If INVARIANTS is defined, panic.
231 panic("%s: Panic before an inp double-free: "
232 "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
235 log(LOG_ERR, "%s: Avoid an inp double-free: "
236 "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
247 * If the connection is not in timewait, we consider two
248 * two conditions: one in which no further processing is
249 * necessary (dropped || embryonic), and one in which TCP is
250 * not yet done, but no longer requires the socket, so the
251 * pcb will persist for the time being.
253 * XXXRW: Does the second case still occur?
255 if (inp->inp_flags & INP_DROPPED ||
256 tp->t_state < TCPS_SYN_SENT) {
268 * pru_detach() detaches the TCP protocol from the socket.
269 * If the protocol state is non-embryonic, then can't
270 * do this directly: have to initiate a pru_disconnect(),
271 * which may finish later; embryonic TCB's can just
275 tcp_usr_detach(struct socket *so)
281 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
282 if (!INP_INFO_WLOCKED(&V_tcbinfo)) {
283 INP_INFO_RLOCK(&V_tcbinfo);
287 KASSERT(inp->inp_socket != NULL,
288 ("tcp_usr_detach: inp_socket == NULL"));
291 INP_INFO_RUNLOCK(&V_tcbinfo);
296 * Give the socket an address.
299 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
303 struct tcpcb *tp = NULL;
304 struct sockaddr_in *sinp;
306 sinp = (struct sockaddr_in *)nam;
307 if (nam->sa_len != sizeof (*sinp))
310 * Must check for multicast addresses and disallow binding
313 if (sinp->sin_family == AF_INET &&
314 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
315 return (EAFNOSUPPORT);
319 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
321 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
327 INP_HASH_WLOCK(&V_tcbinfo);
328 error = in_pcbbind(inp, nam, td->td_ucred);
329 INP_HASH_WUNLOCK(&V_tcbinfo);
332 TCP_PROBE2(debug__user, tp, PRU_BIND);
341 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
345 struct tcpcb *tp = NULL;
346 struct sockaddr_in6 *sin6p;
348 sin6p = (struct sockaddr_in6 *)nam;
349 if (nam->sa_len != sizeof (*sin6p))
352 * Must check for multicast addresses and disallow binding
355 if (sin6p->sin6_family == AF_INET6 &&
356 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
357 return (EAFNOSUPPORT);
361 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
363 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
369 INP_HASH_WLOCK(&V_tcbinfo);
370 inp->inp_vflag &= ~INP_IPV4;
371 inp->inp_vflag |= INP_IPV6;
373 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
374 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
375 inp->inp_vflag |= INP_IPV4;
376 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
377 struct sockaddr_in sin;
379 in6_sin6_2_sin(&sin, sin6p);
380 inp->inp_vflag |= INP_IPV4;
381 inp->inp_vflag &= ~INP_IPV6;
382 error = in_pcbbind(inp, (struct sockaddr *)&sin,
384 INP_HASH_WUNLOCK(&V_tcbinfo);
389 error = in6_pcbbind(inp, nam, td->td_ucred);
390 INP_HASH_WUNLOCK(&V_tcbinfo);
393 TCP_PROBE2(debug__user, tp, PRU_BIND);
401 * Prepare to accept connections.
404 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
408 struct tcpcb *tp = NULL;
412 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
414 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
421 error = solisten_proto_check(so);
422 INP_HASH_WLOCK(&V_tcbinfo);
423 if (error == 0 && inp->inp_lport == 0)
424 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
425 INP_HASH_WUNLOCK(&V_tcbinfo);
427 tcp_state_change(tp, TCPS_LISTEN);
428 solisten_proto(so, backlog);
430 if ((so->so_options & SO_NO_OFFLOAD) == 0)
431 tcp_offload_listen_start(tp);
436 if (IS_FASTOPEN(tp->t_flags))
437 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
440 TCPDEBUG2(PRU_LISTEN);
441 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
449 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
453 struct tcpcb *tp = NULL;
457 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
459 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
466 error = solisten_proto_check(so);
467 INP_HASH_WLOCK(&V_tcbinfo);
468 if (error == 0 && inp->inp_lport == 0) {
469 inp->inp_vflag &= ~INP_IPV4;
470 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
471 inp->inp_vflag |= INP_IPV4;
472 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
474 INP_HASH_WUNLOCK(&V_tcbinfo);
476 tcp_state_change(tp, TCPS_LISTEN);
477 solisten_proto(so, backlog);
479 if ((so->so_options & SO_NO_OFFLOAD) == 0)
480 tcp_offload_listen_start(tp);
485 if (IS_FASTOPEN(tp->t_flags))
486 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
489 TCPDEBUG2(PRU_LISTEN);
490 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
498 * Initiate connection to peer.
499 * Create a template for use in transmissions on this connection.
500 * Enter SYN_SENT state, and mark socket as connecting.
501 * Start keep-alive timer, and seed output sequence space.
502 * Send initial segment on connection.
505 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
509 struct tcpcb *tp = NULL;
510 struct sockaddr_in *sinp;
512 sinp = (struct sockaddr_in *)nam;
513 if (nam->sa_len != sizeof (*sinp))
516 * Must disallow TCP ``connections'' to multicast addresses.
518 if (sinp->sin_family == AF_INET
519 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
520 return (EAFNOSUPPORT);
521 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
526 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
528 if (inp->inp_flags & INP_TIMEWAIT) {
532 if (inp->inp_flags & INP_DROPPED) {
533 error = ECONNREFUSED;
538 if ((error = tcp_connect(tp, nam, td)) != 0)
541 if (registered_toedevs > 0 &&
542 (so->so_options & SO_NO_OFFLOAD) == 0 &&
543 (error = tcp_offload_connect(so, nam)) == 0)
546 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
547 error = tp->t_fb->tfb_tcp_output(tp);
549 TCPDEBUG2(PRU_CONNECT);
550 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
558 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
562 struct tcpcb *tp = NULL;
563 struct sockaddr_in6 *sin6p;
567 sin6p = (struct sockaddr_in6 *)nam;
568 if (nam->sa_len != sizeof (*sin6p))
571 * Must disallow TCP ``connections'' to multicast addresses.
573 if (sin6p->sin6_family == AF_INET6
574 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
575 return (EAFNOSUPPORT);
578 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
580 if (inp->inp_flags & INP_TIMEWAIT) {
584 if (inp->inp_flags & INP_DROPPED) {
585 error = ECONNREFUSED;
592 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
593 * therefore probably require the hash lock, which isn't held here.
594 * Is this a significant problem?
596 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
597 struct sockaddr_in sin;
599 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
603 if ((inp->inp_vflag & INP_IPV4) == 0) {
604 error = EAFNOSUPPORT;
608 in6_sin6_2_sin(&sin, sin6p);
609 inp->inp_vflag |= INP_IPV4;
610 inp->inp_vflag &= ~INP_IPV6;
611 if ((error = prison_remote_ip4(td->td_ucred,
612 &sin.sin_addr)) != 0)
614 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
617 if (registered_toedevs > 0 &&
618 (so->so_options & SO_NO_OFFLOAD) == 0 &&
619 (error = tcp_offload_connect(so, nam)) == 0)
622 error = tp->t_fb->tfb_tcp_output(tp);
625 if ((inp->inp_vflag & INP_IPV6) == 0) {
626 error = EAFNOSUPPORT;
631 inp->inp_vflag &= ~INP_IPV4;
632 inp->inp_vflag |= INP_IPV6;
633 inp->inp_inc.inc_flags |= INC_ISIPV6;
634 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
636 if ((error = tcp6_connect(tp, nam, td)) != 0)
639 if (registered_toedevs > 0 &&
640 (so->so_options & SO_NO_OFFLOAD) == 0 &&
641 (error = tcp_offload_connect(so, nam)) == 0)
644 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
645 error = tp->t_fb->tfb_tcp_output(tp);
648 TCPDEBUG2(PRU_CONNECT);
649 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
656 * Initiate disconnect from peer.
657 * If connection never passed embryonic stage, just drop;
658 * else if don't need to let data drain, then can just drop anyways,
659 * else have to begin TCP shutdown process: mark socket disconnecting,
660 * drain unread data, state switch to reflect user close, and
661 * send segment (e.g. FIN) to peer. Socket will be really disconnected
662 * when peer sends FIN and acks ours.
664 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
667 tcp_usr_disconnect(struct socket *so)
670 struct tcpcb *tp = NULL;
674 INP_INFO_RLOCK(&V_tcbinfo);
676 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
678 if (inp->inp_flags & INP_TIMEWAIT)
680 if (inp->inp_flags & INP_DROPPED) {
688 TCPDEBUG2(PRU_DISCONNECT);
689 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
691 INP_INFO_RUNLOCK(&V_tcbinfo);
697 * Accept a connection. Essentially all the work is done at higher levels;
698 * just return the address of the peer, storing through addr.
701 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
704 struct inpcb *inp = NULL;
705 struct tcpcb *tp = NULL;
710 if (so->so_state & SS_ISDISCONNECTED)
711 return (ECONNABORTED);
714 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
716 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
717 error = ECONNABORTED;
724 * We inline in_getpeeraddr and COMMON_END here, so that we can
725 * copy the data of interest and defer the malloc until after we
728 port = inp->inp_fport;
729 addr = inp->inp_faddr;
732 TCPDEBUG2(PRU_ACCEPT);
733 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
736 *nam = in_sockaddr(port, &addr);
743 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
745 struct inpcb *inp = NULL;
747 struct tcpcb *tp = NULL;
749 struct in6_addr addr6;
754 if (so->so_state & SS_ISDISCONNECTED)
755 return (ECONNABORTED);
758 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
759 INP_INFO_RLOCK(&V_tcbinfo);
761 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
762 error = ECONNABORTED;
769 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
770 * copy the data of interest and defer the malloc until after we
773 if (inp->inp_vflag & INP_IPV4) {
775 port = inp->inp_fport;
776 addr = inp->inp_faddr;
778 port = inp->inp_fport;
779 addr6 = inp->in6p_faddr;
783 TCPDEBUG2(PRU_ACCEPT);
784 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
786 INP_INFO_RUNLOCK(&V_tcbinfo);
789 *nam = in6_v4mapsin6_sockaddr(port, &addr);
791 *nam = in6_sockaddr(port, &addr6);
798 * Mark the connection as being incapable of further output.
801 tcp_usr_shutdown(struct socket *so)
805 struct tcpcb *tp = NULL;
808 INP_INFO_RLOCK(&V_tcbinfo);
810 KASSERT(inp != NULL, ("inp == NULL"));
812 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
820 if (!(inp->inp_flags & INP_DROPPED))
821 error = tp->t_fb->tfb_tcp_output(tp);
824 TCPDEBUG2(PRU_SHUTDOWN);
825 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
827 INP_INFO_RUNLOCK(&V_tcbinfo);
833 * After a receive, possibly send window update to peer.
836 tcp_usr_rcvd(struct socket *so, int flags)
839 struct tcpcb *tp = NULL;
844 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
846 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
853 * For passively-created TFO connections, don't attempt a window
854 * update while still in SYN_RECEIVED as this may trigger an early
855 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
856 * application response data, or failing that, when the DELACK timer
859 if (IS_FASTOPEN(tp->t_flags) &&
860 (tp->t_state == TCPS_SYN_RECEIVED))
863 if (tp->t_flags & TF_TOE)
864 tcp_offload_rcvd(tp);
867 tp->t_fb->tfb_tcp_output(tp);
871 TCP_PROBE2(debug__user, tp, PRU_RCVD);
877 * Do a send by putting data in output queue and updating urgent
878 * marker if URG set. Possibly send more data. Unlike the other
879 * pru_*() routines, the mbuf chains are our responsibility. We
880 * must either enqueue them or free them. The other pru_* routines
881 * generally are caller-frees.
884 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
885 struct sockaddr *nam, struct mbuf *control, struct thread *td)
889 struct tcpcb *tp = NULL;
896 * We require the pcbinfo lock if we will close the socket as part of
899 if (flags & PRUS_EOF)
900 INP_INFO_RLOCK(&V_tcbinfo);
902 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
904 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
908 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
909 * for freeing memory.
911 if (m && (flags & PRUS_NOTREADY) == 0)
917 isipv6 = nam && nam->sa_family == AF_INET6;
922 /* TCP doesn't do control messages (rights, creds, etc) */
923 if (control->m_len) {
930 m_freem(control); /* empty control, just free it */
932 if (!(flags & PRUS_OOB)) {
933 sbappendstream(&so->so_snd, m, flags);
934 if (nam && tp->t_state < TCPS_SYN_SENT) {
936 * Do implied connect if not yet connected,
937 * initialize window to default value, and
938 * initialize maxseg using peer's cached MSS.
942 error = tcp6_connect(tp, nam, td);
944 #if defined(INET6) && defined(INET)
948 error = tcp_connect(tp, nam, td);
952 if (IS_FASTOPEN(tp->t_flags))
953 tcp_fastopen_connect(tp);
955 tp->snd_wnd = TTCP_CLIENT_SND_WND;
959 if (flags & PRUS_EOF) {
961 * Close the send side of the connection after
964 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
968 if (!(inp->inp_flags & INP_DROPPED) &&
969 !(flags & PRUS_NOTREADY)) {
970 if (flags & PRUS_MORETOCOME)
971 tp->t_flags |= TF_MORETOCOME;
972 error = tp->t_fb->tfb_tcp_output(tp);
973 if (flags & PRUS_MORETOCOME)
974 tp->t_flags &= ~TF_MORETOCOME;
978 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
980 SOCKBUF_LOCK(&so->so_snd);
981 if (sbspace(&so->so_snd) < -512) {
982 SOCKBUF_UNLOCK(&so->so_snd);
988 * According to RFC961 (Assigned Protocols),
989 * the urgent pointer points to the last octet
990 * of urgent data. We continue, however,
991 * to consider it to indicate the first octet
992 * of data past the urgent section.
993 * Otherwise, snd_up should be one lower.
995 sbappendstream_locked(&so->so_snd, m, flags);
996 SOCKBUF_UNLOCK(&so->so_snd);
997 if (nam && tp->t_state < TCPS_SYN_SENT) {
999 * Do implied connect if not yet connected,
1000 * initialize window to default value, and
1001 * initialize maxseg using peer's cached MSS.
1005 * Not going to contemplate SYN|URG
1007 if (IS_FASTOPEN(tp->t_flags))
1008 tp->t_flags &= ~TF_FASTOPEN;
1011 error = tcp6_connect(tp, nam, td);
1013 #if defined(INET6) && defined(INET)
1017 error = tcp_connect(tp, nam, td);
1021 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1024 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1025 if (!(flags & PRUS_NOTREADY)) {
1026 tp->t_flags |= TF_FORCEDATA;
1027 error = tp->t_fb->tfb_tcp_output(tp);
1028 tp->t_flags &= ~TF_FORCEDATA;
1031 TCP_LOG_EVENT(tp, NULL,
1032 &inp->inp_socket->so_rcv,
1033 &inp->inp_socket->so_snd,
1034 TCP_LOG_USERSEND, error,
1037 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
1038 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1039 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1040 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1042 if (flags & PRUS_EOF)
1043 INP_INFO_RUNLOCK(&V_tcbinfo);
1048 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1054 inp = sotoinpcb(so);
1056 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1058 for (int i = 0; i < count; i++)
1060 return (ECONNRESET);
1062 tp = intotcpcb(inp);
1064 SOCKBUF_LOCK(&so->so_snd);
1065 error = sbready(&so->so_snd, m, count);
1066 SOCKBUF_UNLOCK(&so->so_snd);
1068 error = tp->t_fb->tfb_tcp_output(tp);
1075 * Abort the TCP. Drop the connection abruptly.
1078 tcp_usr_abort(struct socket *so)
1081 struct tcpcb *tp = NULL;
1084 inp = sotoinpcb(so);
1085 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1087 INP_INFO_RLOCK(&V_tcbinfo);
1089 KASSERT(inp->inp_socket != NULL,
1090 ("tcp_usr_abort: inp_socket == NULL"));
1093 * If we still have full TCP state, and we're not dropped, drop.
1095 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1096 !(inp->inp_flags & INP_DROPPED)) {
1097 tp = intotcpcb(inp);
1099 tp = tcp_drop(tp, ECONNABORTED);
1102 TCPDEBUG2(PRU_ABORT);
1103 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1105 if (!(inp->inp_flags & INP_DROPPED)) {
1107 so->so_state |= SS_PROTOREF;
1109 inp->inp_flags |= INP_SOCKREF;
1113 INP_INFO_RUNLOCK(&V_tcbinfo);
1117 * TCP socket is closed. Start friendly disconnect.
1120 tcp_usr_close(struct socket *so)
1123 struct tcpcb *tp = NULL;
1126 inp = sotoinpcb(so);
1127 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1129 INP_INFO_RLOCK(&V_tcbinfo);
1131 KASSERT(inp->inp_socket != NULL,
1132 ("tcp_usr_close: inp_socket == NULL"));
1135 * If we still have full TCP state, and we're not dropped, initiate
1138 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1139 !(inp->inp_flags & INP_DROPPED)) {
1140 tp = intotcpcb(inp);
1143 TCPDEBUG2(PRU_CLOSE);
1144 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1146 if (!(inp->inp_flags & INP_DROPPED)) {
1148 so->so_state |= SS_PROTOREF;
1150 inp->inp_flags |= INP_SOCKREF;
1153 INP_INFO_RUNLOCK(&V_tcbinfo);
1157 * Receive out-of-band data.
1160 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1164 struct tcpcb *tp = NULL;
1167 inp = sotoinpcb(so);
1168 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1170 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1174 tp = intotcpcb(inp);
1176 if ((so->so_oobmark == 0 &&
1177 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1178 so->so_options & SO_OOBINLINE ||
1179 tp->t_oobflags & TCPOOB_HADDATA) {
1183 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1184 error = EWOULDBLOCK;
1188 *mtod(m, caddr_t) = tp->t_iobc;
1189 if ((flags & MSG_PEEK) == 0)
1190 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1193 TCPDEBUG2(PRU_RCVOOB);
1194 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1200 struct pr_usrreqs tcp_usrreqs = {
1201 .pru_abort = tcp_usr_abort,
1202 .pru_accept = tcp_usr_accept,
1203 .pru_attach = tcp_usr_attach,
1204 .pru_bind = tcp_usr_bind,
1205 .pru_connect = tcp_usr_connect,
1206 .pru_control = in_control,
1207 .pru_detach = tcp_usr_detach,
1208 .pru_disconnect = tcp_usr_disconnect,
1209 .pru_listen = tcp_usr_listen,
1210 .pru_peeraddr = in_getpeeraddr,
1211 .pru_rcvd = tcp_usr_rcvd,
1212 .pru_rcvoob = tcp_usr_rcvoob,
1213 .pru_send = tcp_usr_send,
1214 .pru_ready = tcp_usr_ready,
1215 .pru_shutdown = tcp_usr_shutdown,
1216 .pru_sockaddr = in_getsockaddr,
1217 .pru_sosetlabel = in_pcbsosetlabel,
1218 .pru_close = tcp_usr_close,
1223 struct pr_usrreqs tcp6_usrreqs = {
1224 .pru_abort = tcp_usr_abort,
1225 .pru_accept = tcp6_usr_accept,
1226 .pru_attach = tcp_usr_attach,
1227 .pru_bind = tcp6_usr_bind,
1228 .pru_connect = tcp6_usr_connect,
1229 .pru_control = in6_control,
1230 .pru_detach = tcp_usr_detach,
1231 .pru_disconnect = tcp_usr_disconnect,
1232 .pru_listen = tcp6_usr_listen,
1233 .pru_peeraddr = in6_mapped_peeraddr,
1234 .pru_rcvd = tcp_usr_rcvd,
1235 .pru_rcvoob = tcp_usr_rcvoob,
1236 .pru_send = tcp_usr_send,
1237 .pru_ready = tcp_usr_ready,
1238 .pru_shutdown = tcp_usr_shutdown,
1239 .pru_sockaddr = in6_mapped_sockaddr,
1240 .pru_sosetlabel = in_pcbsosetlabel,
1241 .pru_close = tcp_usr_close,
1247 * Common subroutine to open a TCP connection to remote host specified
1248 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1249 * port number if needed. Call in_pcbconnect_setup to do the routing and
1250 * to choose a local host address (interface). If there is an existing
1251 * incarnation of the same connection in TIME-WAIT state and if the remote
1252 * host was sending CC options and if the connection duration was < MSL, then
1253 * truncate the previous TIME-WAIT state and proceed.
1254 * Initialize connection parameters and enter SYN-SENT state.
1257 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1259 struct inpcb *inp = tp->t_inpcb, *oinp;
1260 struct socket *so = inp->inp_socket;
1261 struct in_addr laddr;
1265 INP_WLOCK_ASSERT(inp);
1266 INP_HASH_WLOCK(&V_tcbinfo);
1268 if (inp->inp_lport == 0) {
1269 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1275 * Cannot simply call in_pcbconnect, because there might be an
1276 * earlier incarnation of this same connection still in
1277 * TIME_WAIT state, creating an ADDRINUSE error.
1279 laddr = inp->inp_laddr;
1280 lport = inp->inp_lport;
1281 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1282 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1283 if (error && oinp == NULL)
1289 inp->inp_laddr = laddr;
1291 INP_HASH_WUNLOCK(&V_tcbinfo);
1294 * Compute window scaling to request:
1295 * Scale to fit into sweet spot. See tcp_syncache.c.
1296 * XXX: This should move to tcp_output().
1298 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1299 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1300 tp->request_r_scale++;
1303 TCPSTAT_INC(tcps_connattempt);
1304 tcp_state_change(tp, TCPS_SYN_SENT);
1305 tp->iss = tcp_new_isn(tp);
1306 tcp_sendseqinit(tp);
1311 INP_HASH_WUNLOCK(&V_tcbinfo);
1318 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1320 struct inpcb *inp = tp->t_inpcb;
1323 INP_WLOCK_ASSERT(inp);
1324 INP_HASH_WLOCK(&V_tcbinfo);
1326 if (inp->inp_lport == 0) {
1327 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1331 error = in6_pcbconnect(inp, nam, td->td_ucred);
1334 INP_HASH_WUNLOCK(&V_tcbinfo);
1336 /* Compute window scaling to request. */
1337 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1338 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1339 tp->request_r_scale++;
1341 soisconnecting(inp->inp_socket);
1342 TCPSTAT_INC(tcps_connattempt);
1343 tcp_state_change(tp, TCPS_SYN_SENT);
1344 tp->iss = tcp_new_isn(tp);
1345 tcp_sendseqinit(tp);
1350 INP_HASH_WUNLOCK(&V_tcbinfo);
1356 * Export TCP internal state information via a struct tcp_info, based on the
1357 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1358 * (TCP state machine, etc). We export all information using FreeBSD-native
1359 * constants -- for example, the numeric values for tcpi_state will differ
1363 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1366 INP_WLOCK_ASSERT(tp->t_inpcb);
1367 bzero(ti, sizeof(*ti));
1369 ti->tcpi_state = tp->t_state;
1370 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1371 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1372 if (tp->t_flags & TF_SACK_PERMIT)
1373 ti->tcpi_options |= TCPI_OPT_SACK;
1374 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1375 ti->tcpi_options |= TCPI_OPT_WSCALE;
1376 ti->tcpi_snd_wscale = tp->snd_scale;
1377 ti->tcpi_rcv_wscale = tp->rcv_scale;
1379 if (tp->t_flags & TF_ECN_PERMIT)
1380 ti->tcpi_options |= TCPI_OPT_ECN;
1382 ti->tcpi_rto = tp->t_rxtcur * tick;
1383 ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1384 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1385 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1387 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1388 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1391 * FreeBSD-specific extension fields for tcp_info.
1393 ti->tcpi_rcv_space = tp->rcv_wnd;
1394 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1395 ti->tcpi_snd_wnd = tp->snd_wnd;
1396 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1397 ti->tcpi_snd_nxt = tp->snd_nxt;
1398 ti->tcpi_snd_mss = tp->t_maxseg;
1399 ti->tcpi_rcv_mss = tp->t_maxseg;
1400 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1401 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1402 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1404 if (tp->t_flags & TF_TOE) {
1405 ti->tcpi_options |= TCPI_OPT_TOE;
1406 tcp_offload_tcp_info(tp, ti);
1412 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1413 * socket option arguments. When it re-acquires the lock after the copy, it
1414 * has to revalidate that the connection is still valid for the socket
1417 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \
1419 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1422 return (ECONNRESET); \
1424 tp = intotcpcb(inp); \
1426 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1429 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1434 struct tcp_function_block *blk;
1435 struct tcp_function_set fsn;
1438 inp = sotoinpcb(so);
1439 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1441 if (sopt->sopt_level != IPPROTO_TCP) {
1443 if (inp->inp_vflag & INP_IPV6PROTO) {
1445 error = ip6_ctloutput(so, sopt);
1448 #if defined(INET6) && defined(INET)
1454 error = ip_ctloutput(so, sopt);
1459 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1461 return (ECONNRESET);
1463 tp = intotcpcb(inp);
1465 * Protect the TCP option TCP_FUNCTION_BLK so
1466 * that a sub-function can *never* overwrite this.
1468 if ((sopt->sopt_dir == SOPT_SET) &&
1469 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1471 error = sooptcopyin(sopt, &fsn, sizeof fsn,
1475 INP_WLOCK_RECHECK(inp);
1476 blk = find_and_ref_tcp_functions(&fsn);
1481 if (tp->t_fb == blk) {
1482 /* You already have this */
1483 refcount_release(&blk->tfb_refcnt);
1487 if (tp->t_state != TCPS_CLOSED) {
1490 * The user has advanced the state
1491 * past the initial point, we may not
1492 * be able to switch.
1494 if (blk->tfb_tcp_handoff_ok != NULL) {
1496 * Does the stack provide a
1497 * query mechanism, if so it may
1498 * still be possible?
1500 error = (*blk->tfb_tcp_handoff_ok)(tp);
1503 refcount_release(&blk->tfb_refcnt);
1508 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1509 refcount_release(&blk->tfb_refcnt);
1514 * Release the old refcnt, the
1515 * lookup acquired a ref on the
1518 if (tp->t_fb->tfb_tcp_fb_fini) {
1520 * Tell the stack to cleanup with 0 i.e.
1521 * the tcb is not going away.
1523 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1526 /* Assure that we are not on any hpts */
1527 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
1529 if (blk->tfb_tcp_fb_init) {
1530 error = (*blk->tfb_tcp_fb_init)(tp);
1532 refcount_release(&blk->tfb_refcnt);
1533 if (tp->t_fb->tfb_tcp_fb_init) {
1534 if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) {
1535 /* Fall back failed, drop the connection */
1544 refcount_release(&tp->t_fb->tfb_refcnt);
1547 if (tp->t_flags & TF_TOE) {
1548 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1555 } else if ((sopt->sopt_dir == SOPT_GET) &&
1556 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1557 strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1558 TCP_FUNCTION_NAME_LEN_MAX);
1559 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1560 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1562 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1565 /* Pass in the INP locked, called must unlock it */
1566 return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
1570 * If this assert becomes untrue, we need to change the size of the buf
1571 * variable in tcp_default_ctloutput().
1574 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1575 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1579 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
1581 int error, opt, optval;
1584 struct cc_algo *algo;
1585 char *pbuf, buf[TCP_LOG_ID_LEN];
1589 * For TCP_CCALGOOPT forward the control to CC module, for both
1590 * SOPT_SET and SOPT_GET.
1592 switch (sopt->sopt_name) {
1595 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1596 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1597 sopt->sopt_valsize);
1602 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1603 if (CC_ALGO(tp)->ctl_output != NULL)
1604 error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1608 if (error == 0 && sopt->sopt_dir == SOPT_GET)
1609 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1614 switch (sopt->sopt_dir) {
1616 switch (sopt->sopt_name) {
1617 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1619 if (!TCPMD5_ENABLED()) {
1621 return (ENOPROTOOPT);
1623 error = TCPMD5_PCBCTL(inp, sopt);
1626 goto unlock_and_done;
1632 error = sooptcopyin(sopt, &optval, sizeof optval,
1637 INP_WLOCK_RECHECK(inp);
1638 switch (sopt->sopt_name) {
1646 opt = 0; /* dead code to fool gcc */
1653 tp->t_flags &= ~opt;
1656 if (tp->t_flags & TF_TOE) {
1657 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1666 error = sooptcopyin(sopt, &optval, sizeof optval,
1671 INP_WLOCK_RECHECK(inp);
1673 tp->t_flags |= TF_NOPUSH;
1674 else if (tp->t_flags & TF_NOPUSH) {
1675 tp->t_flags &= ~TF_NOPUSH;
1676 if (TCPS_HAVEESTABLISHED(tp->t_state))
1677 error = tp->t_fb->tfb_tcp_output(tp);
1679 goto unlock_and_done;
1683 error = sooptcopyin(sopt, &optval, sizeof optval,
1688 INP_WLOCK_RECHECK(inp);
1689 if (optval > 0 && optval <= tp->t_maxseg &&
1690 optval + 40 >= V_tcp_minmss)
1691 tp->t_maxseg = optval;
1694 goto unlock_and_done;
1701 case TCP_CONGESTION:
1703 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1706 buf[sopt->sopt_valsize] = '\0';
1707 INP_WLOCK_RECHECK(inp);
1709 STAILQ_FOREACH(algo, &cc_list, entries)
1710 if (strncmp(buf, algo->name,
1711 TCP_CA_NAME_MAX) == 0)
1720 * We hold a write lock over the tcb so it's safe to
1721 * do these things without ordering concerns.
1723 if (CC_ALGO(tp)->cb_destroy != NULL)
1724 CC_ALGO(tp)->cb_destroy(tp->ccv);
1727 * If something goes pear shaped initialising the new
1728 * algo, fall back to newreno (which does not
1729 * require initialisation).
1731 if (algo->cb_init != NULL &&
1732 algo->cb_init(tp->ccv) != 0) {
1733 CC_ALGO(tp) = &newreno_cc_algo;
1735 * The only reason init should fail is
1736 * because of malloc.
1747 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1751 if (ui > (UINT_MAX / hz)) {
1757 INP_WLOCK_RECHECK(inp);
1758 switch (sopt->sopt_name) {
1760 tp->t_keepidle = ui;
1762 * XXX: better check current remaining
1763 * timeout and "merge" it with new value.
1765 if ((tp->t_state > TCPS_LISTEN) &&
1766 (tp->t_state <= TCPS_CLOSING))
1767 tcp_timer_activate(tp, TT_KEEP,
1771 tp->t_keepintvl = ui;
1772 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1773 (TP_MAXIDLE(tp) > 0))
1774 tcp_timer_activate(tp, TT_2MSL,
1778 tp->t_keepinit = ui;
1779 if (tp->t_state == TCPS_SYN_RECEIVED ||
1780 tp->t_state == TCPS_SYN_SENT)
1781 tcp_timer_activate(tp, TT_KEEP,
1785 goto unlock_and_done;
1789 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1793 INP_WLOCK_RECHECK(inp);
1795 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1796 (TP_MAXIDLE(tp) > 0))
1797 tcp_timer_activate(tp, TT_2MSL,
1799 goto unlock_and_done;
1805 error = sooptcopyin(sopt, &optval, sizeof optval,
1810 INP_WLOCK_RECHECK(inp);
1812 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
1813 &(tp->t_outpkts) : &(tp->t_inpkts),
1817 goto unlock_and_done;
1820 case TCP_FASTOPEN: {
1821 struct tcp_fastopen tfo_optval;
1824 if (!V_tcp_fastopen_client_enable &&
1825 !V_tcp_fastopen_server_enable)
1828 error = sooptcopyin(sopt, &tfo_optval,
1829 sizeof(tfo_optval), sizeof(int));
1833 INP_WLOCK_RECHECK(inp);
1834 if (tfo_optval.enable) {
1835 if (tp->t_state == TCPS_LISTEN) {
1836 if (!V_tcp_fastopen_server_enable) {
1838 goto unlock_and_done;
1841 tp->t_flags |= TF_FASTOPEN;
1842 if (tp->t_tfo_pending == NULL)
1844 tcp_fastopen_alloc_counter();
1847 * If a pre-shared key was provided,
1848 * stash it in the client cookie
1849 * field of the tcpcb for use during
1852 if (sopt->sopt_valsize ==
1853 sizeof(tfo_optval)) {
1854 memcpy(tp->t_tfo_cookie.client,
1856 TCP_FASTOPEN_PSK_LEN);
1857 tp->t_tfo_client_cookie_len =
1858 TCP_FASTOPEN_PSK_LEN;
1860 tp->t_flags |= TF_FASTOPEN;
1863 tp->t_flags &= ~TF_FASTOPEN;
1864 goto unlock_and_done;
1870 error = sooptcopyin(sopt, &optval, sizeof optval,
1875 INP_WLOCK_RECHECK(inp);
1876 error = tcp_log_state_change(tp, optval);
1877 goto unlock_and_done;
1886 error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
1889 buf[sopt->sopt_valsize] = '\0';
1890 INP_WLOCK_RECHECK(inp);
1891 error = tcp_log_set_id(tp, buf);
1892 /* tcp_log_set_id() unlocks the INP. */
1899 sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
1902 buf[sopt->sopt_valsize] = '\0';
1903 INP_WLOCK_RECHECK(inp);
1904 if (sopt->sopt_name == TCP_LOGDUMP) {
1905 error = tcp_log_dump_tp_logbuf(tp, buf,
1909 tcp_log_dump_tp_bucket_logbufs(tp, buf);
1911 * tcp_log_dump_tp_bucket_logbufs() drops the
1920 error = ENOPROTOOPT;
1926 tp = intotcpcb(inp);
1927 switch (sopt->sopt_name) {
1928 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1930 if (!TCPMD5_ENABLED()) {
1932 return (ENOPROTOOPT);
1934 error = TCPMD5_PCBCTL(inp, sopt);
1939 optval = tp->t_flags & TF_NODELAY;
1941 error = sooptcopyout(sopt, &optval, sizeof optval);
1944 optval = tp->t_maxseg;
1946 error = sooptcopyout(sopt, &optval, sizeof optval);
1949 optval = tp->t_flags & TF_NOOPT;
1951 error = sooptcopyout(sopt, &optval, sizeof optval);
1954 optval = tp->t_flags & TF_NOPUSH;
1956 error = sooptcopyout(sopt, &optval, sizeof optval);
1959 tcp_fill_info(tp, &ti);
1961 error = sooptcopyout(sopt, &ti, sizeof ti);
1963 case TCP_CONGESTION:
1964 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1966 error = sooptcopyout(sopt, buf, len + 1);
1972 switch (sopt->sopt_name) {
1974 ui = TP_KEEPIDLE(tp) / hz;
1977 ui = TP_KEEPINTVL(tp) / hz;
1980 ui = TP_KEEPINIT(tp) / hz;
1983 ui = TP_KEEPCNT(tp);
1987 error = sooptcopyout(sopt, &ui, sizeof(ui));
1992 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
1993 &(tp->t_outpkts) : &(tp->t_inpkts));
1995 error = sooptcopyout(sopt, &optval, sizeof optval);
1999 optval = tp->t_flags & TF_FASTOPEN;
2001 error = sooptcopyout(sopt, &optval, sizeof optval);
2005 optval = tp->t_logstate;
2007 error = sooptcopyout(sopt, &optval, sizeof(optval));
2010 /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2011 error = tcp_log_getlogbuf(sopt, tp);
2014 len = tcp_log_get_id(tp, buf);
2016 error = sooptcopyout(sopt, buf, len + 1);
2026 error = ENOPROTOOPT;
2033 #undef INP_WLOCK_RECHECK
2034 #undef INP_WLOCK_RECHECK_CLEANUP
2037 * Attach TCP protocol to socket, allocating
2038 * internet protocol control block, tcp control block,
2039 * bufer space, and entering LISTEN state if to accept connections.
2042 tcp_attach(struct socket *so)
2048 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
2049 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
2053 so->so_rcv.sb_flags |= SB_AUTOSIZE;
2054 so->so_snd.sb_flags |= SB_AUTOSIZE;
2055 INP_INFO_RLOCK(&V_tcbinfo);
2056 error = in_pcballoc(so, &V_tcbinfo);
2058 INP_INFO_RUNLOCK(&V_tcbinfo);
2061 inp = sotoinpcb(so);
2063 if (inp->inp_vflag & INP_IPV6PROTO) {
2064 inp->inp_vflag |= INP_IPV6;
2065 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
2066 inp->inp_vflag |= INP_IPV4;
2067 inp->in6p_hops = -1; /* use kernel default */
2071 inp->inp_vflag |= INP_IPV4;
2072 tp = tcp_newtcpcb(inp);
2076 INP_INFO_RUNLOCK(&V_tcbinfo);
2079 tp->t_state = TCPS_CLOSED;
2081 INP_INFO_RUNLOCK(&V_tcbinfo);
2082 TCPSTATES_INC(TCPS_CLOSED);
2087 * Initiate (or continue) disconnect.
2088 * If embryonic state, just send reset (once).
2089 * If in ``let data drain'' option and linger null, just drop.
2090 * Otherwise (hard), mark socket disconnecting and drop
2091 * current input data; switch states based on user close, and
2092 * send segment to peer (with FIN).
2095 tcp_disconnect(struct tcpcb *tp)
2097 struct inpcb *inp = tp->t_inpcb;
2098 struct socket *so = inp->inp_socket;
2100 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2101 INP_WLOCK_ASSERT(inp);
2104 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2105 * socket is still open.
2107 if (tp->t_state < TCPS_ESTABLISHED) {
2110 ("tcp_disconnect: tcp_close() returned NULL"));
2111 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2112 tp = tcp_drop(tp, 0);
2114 ("tcp_disconnect: tcp_drop() returned NULL"));
2116 soisdisconnecting(so);
2117 sbflush(&so->so_rcv);
2119 if (!(inp->inp_flags & INP_DROPPED))
2120 tp->t_fb->tfb_tcp_output(tp);
2125 * User issued close, and wish to trail through shutdown states:
2126 * if never received SYN, just forget it. If got a SYN from peer,
2127 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2128 * If already got a FIN from peer, then almost done; go to LAST_ACK
2129 * state. In all other cases, have already sent FIN to peer (e.g.
2130 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2131 * for peer to send FIN or not respond to keep-alives, etc.
2132 * We can let the user exit from the close as soon as the FIN is acked.
2135 tcp_usrclosed(struct tcpcb *tp)
2138 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2139 INP_WLOCK_ASSERT(tp->t_inpcb);
2141 switch (tp->t_state) {
2144 tcp_offload_listen_stop(tp);
2146 tcp_state_change(tp, TCPS_CLOSED);
2151 * tcp_close() should never return NULL here as the socket is
2155 ("tcp_usrclosed: tcp_close() returned NULL"));
2159 case TCPS_SYN_RECEIVED:
2160 tp->t_flags |= TF_NEEDFIN;
2163 case TCPS_ESTABLISHED:
2164 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2167 case TCPS_CLOSE_WAIT:
2168 tcp_state_change(tp, TCPS_LAST_ACK);
2171 if (tp->t_state >= TCPS_FIN_WAIT_2) {
2172 soisdisconnected(tp->t_inpcb->inp_socket);
2173 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
2174 if (tp->t_state == TCPS_FIN_WAIT_2) {
2177 timeout = (tcp_fast_finwait2_recycle) ?
2178 tcp_finwait2_timeout : TP_MAXIDLE(tp);
2179 tcp_timer_activate(tp, TT_2MSL, timeout);
2186 db_print_indent(int indent)
2190 for (i = 0; i < indent; i++)
2195 db_print_tstate(int t_state)
2200 db_printf("TCPS_CLOSED");
2204 db_printf("TCPS_LISTEN");
2208 db_printf("TCPS_SYN_SENT");
2211 case TCPS_SYN_RECEIVED:
2212 db_printf("TCPS_SYN_RECEIVED");
2215 case TCPS_ESTABLISHED:
2216 db_printf("TCPS_ESTABLISHED");
2219 case TCPS_CLOSE_WAIT:
2220 db_printf("TCPS_CLOSE_WAIT");
2223 case TCPS_FIN_WAIT_1:
2224 db_printf("TCPS_FIN_WAIT_1");
2228 db_printf("TCPS_CLOSING");
2232 db_printf("TCPS_LAST_ACK");
2235 case TCPS_FIN_WAIT_2:
2236 db_printf("TCPS_FIN_WAIT_2");
2239 case TCPS_TIME_WAIT:
2240 db_printf("TCPS_TIME_WAIT");
2244 db_printf("unknown");
2250 db_print_tflags(u_int t_flags)
2255 if (t_flags & TF_ACKNOW) {
2256 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2259 if (t_flags & TF_DELACK) {
2260 db_printf("%sTF_DELACK", comma ? ", " : "");
2263 if (t_flags & TF_NODELAY) {
2264 db_printf("%sTF_NODELAY", comma ? ", " : "");
2267 if (t_flags & TF_NOOPT) {
2268 db_printf("%sTF_NOOPT", comma ? ", " : "");
2271 if (t_flags & TF_SENTFIN) {
2272 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2275 if (t_flags & TF_REQ_SCALE) {
2276 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2279 if (t_flags & TF_RCVD_SCALE) {
2280 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2283 if (t_flags & TF_REQ_TSTMP) {
2284 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2287 if (t_flags & TF_RCVD_TSTMP) {
2288 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2291 if (t_flags & TF_SACK_PERMIT) {
2292 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2295 if (t_flags & TF_NEEDSYN) {
2296 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2299 if (t_flags & TF_NEEDFIN) {
2300 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2303 if (t_flags & TF_NOPUSH) {
2304 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2307 if (t_flags & TF_MORETOCOME) {
2308 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2311 if (t_flags & TF_LQ_OVERFLOW) {
2312 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2315 if (t_flags & TF_LASTIDLE) {
2316 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2319 if (t_flags & TF_RXWIN0SENT) {
2320 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2323 if (t_flags & TF_FASTRECOVERY) {
2324 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2327 if (t_flags & TF_CONGRECOVERY) {
2328 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2331 if (t_flags & TF_WASFRECOVERY) {
2332 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2335 if (t_flags & TF_SIGNATURE) {
2336 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2339 if (t_flags & TF_FORCEDATA) {
2340 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2343 if (t_flags & TF_TSO) {
2344 db_printf("%sTF_TSO", comma ? ", " : "");
2347 if (t_flags & TF_ECN_PERMIT) {
2348 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2351 if (t_flags & TF_FASTOPEN) {
2352 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2358 db_print_toobflags(char t_oobflags)
2363 if (t_oobflags & TCPOOB_HAVEDATA) {
2364 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2367 if (t_oobflags & TCPOOB_HADDATA) {
2368 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2374 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2377 db_print_indent(indent);
2378 db_printf("%s at %p\n", name, tp);
2382 db_print_indent(indent);
2383 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2384 LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2386 db_print_indent(indent);
2387 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
2388 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2390 db_print_indent(indent);
2391 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2392 &tp->t_timers->tt_delack, tp->t_inpcb);
2394 db_print_indent(indent);
2395 db_printf("t_state: %d (", tp->t_state);
2396 db_print_tstate(tp->t_state);
2399 db_print_indent(indent);
2400 db_printf("t_flags: 0x%x (", tp->t_flags);
2401 db_print_tflags(tp->t_flags);
2404 db_print_indent(indent);
2405 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2406 tp->snd_una, tp->snd_max, tp->snd_nxt);
2408 db_print_indent(indent);
2409 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2410 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2412 db_print_indent(indent);
2413 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2414 tp->iss, tp->irs, tp->rcv_nxt);
2416 db_print_indent(indent);
2417 db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n",
2418 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2420 db_print_indent(indent);
2421 db_printf("snd_wnd: %u snd_cwnd: %u\n",
2422 tp->snd_wnd, tp->snd_cwnd);
2424 db_print_indent(indent);
2425 db_printf("snd_ssthresh: %u snd_recover: "
2426 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2428 db_print_indent(indent);
2429 db_printf("t_rcvtime: %u t_startime: %u\n",
2430 tp->t_rcvtime, tp->t_starttime);
2432 db_print_indent(indent);
2433 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2434 tp->t_rtttime, tp->t_rtseq);
2436 db_print_indent(indent);
2437 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2438 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2440 db_print_indent(indent);
2441 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2442 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2445 db_print_indent(indent);
2446 db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n",
2447 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2449 db_print_indent(indent);
2450 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2451 db_print_toobflags(tp->t_oobflags);
2452 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2454 db_print_indent(indent);
2455 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2456 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2458 db_print_indent(indent);
2459 db_printf("ts_recent: %u ts_recent_age: %u\n",
2460 tp->ts_recent, tp->ts_recent_age);
2462 db_print_indent(indent);
2463 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2464 "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2466 db_print_indent(indent);
2467 db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x "
2468 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2469 tp->snd_recover_prev, tp->t_badrxtwin);
2471 db_print_indent(indent);
2472 db_printf("snd_numholes: %d snd_holes first: %p\n",
2473 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2475 db_print_indent(indent);
2476 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
2477 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2479 /* Skip sackblks, sackhint. */
2481 db_print_indent(indent);
2482 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2483 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2486 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2491 db_printf("usage: show tcpcb <addr>\n");
2494 tp = (struct tcpcb *)addr;
2496 db_print_tcpcb(tp, "tcpcb", 0);