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)
279 struct epoch_tracker et;
282 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
283 if (!INP_INFO_WLOCKED(&V_tcbinfo)) {
284 INP_INFO_RLOCK_ET(&V_tcbinfo, et);
288 KASSERT(inp->inp_socket != NULL,
289 ("tcp_usr_detach: inp_socket == NULL"));
292 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
297 * Give the socket an address.
300 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
304 struct tcpcb *tp = NULL;
305 struct sockaddr_in *sinp;
307 sinp = (struct sockaddr_in *)nam;
308 if (nam->sa_len != sizeof (*sinp))
311 * Must check for multicast addresses and disallow binding
314 if (sinp->sin_family == AF_INET &&
315 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
316 return (EAFNOSUPPORT);
320 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
322 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
328 INP_HASH_WLOCK(&V_tcbinfo);
329 error = in_pcbbind(inp, nam, td->td_ucred);
330 INP_HASH_WUNLOCK(&V_tcbinfo);
333 TCP_PROBE2(debug__user, tp, PRU_BIND);
342 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
346 struct tcpcb *tp = NULL;
347 struct sockaddr_in6 *sin6p;
349 sin6p = (struct sockaddr_in6 *)nam;
350 if (nam->sa_len != sizeof (*sin6p))
353 * Must check for multicast addresses and disallow binding
356 if (sin6p->sin6_family == AF_INET6 &&
357 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
358 return (EAFNOSUPPORT);
362 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
364 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
370 INP_HASH_WLOCK(&V_tcbinfo);
371 inp->inp_vflag &= ~INP_IPV4;
372 inp->inp_vflag |= INP_IPV6;
374 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
375 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
376 inp->inp_vflag |= INP_IPV4;
377 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
378 struct sockaddr_in sin;
380 in6_sin6_2_sin(&sin, sin6p);
381 inp->inp_vflag |= INP_IPV4;
382 inp->inp_vflag &= ~INP_IPV6;
383 error = in_pcbbind(inp, (struct sockaddr *)&sin,
385 INP_HASH_WUNLOCK(&V_tcbinfo);
390 error = in6_pcbbind(inp, nam, td->td_ucred);
391 INP_HASH_WUNLOCK(&V_tcbinfo);
394 TCP_PROBE2(debug__user, tp, PRU_BIND);
402 * Prepare to accept connections.
405 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
409 struct tcpcb *tp = NULL;
413 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
415 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
422 error = solisten_proto_check(so);
423 INP_HASH_WLOCK(&V_tcbinfo);
424 if (error == 0 && inp->inp_lport == 0)
425 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
426 INP_HASH_WUNLOCK(&V_tcbinfo);
428 tcp_state_change(tp, TCPS_LISTEN);
429 solisten_proto(so, backlog);
431 if ((so->so_options & SO_NO_OFFLOAD) == 0)
432 tcp_offload_listen_start(tp);
437 if (IS_FASTOPEN(tp->t_flags))
438 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
441 TCPDEBUG2(PRU_LISTEN);
442 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
450 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
454 struct tcpcb *tp = NULL;
458 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
460 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
467 error = solisten_proto_check(so);
468 INP_HASH_WLOCK(&V_tcbinfo);
469 if (error == 0 && inp->inp_lport == 0) {
470 inp->inp_vflag &= ~INP_IPV4;
471 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
472 inp->inp_vflag |= INP_IPV4;
473 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
475 INP_HASH_WUNLOCK(&V_tcbinfo);
477 tcp_state_change(tp, TCPS_LISTEN);
478 solisten_proto(so, backlog);
480 if ((so->so_options & SO_NO_OFFLOAD) == 0)
481 tcp_offload_listen_start(tp);
486 if (IS_FASTOPEN(tp->t_flags))
487 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
490 TCPDEBUG2(PRU_LISTEN);
491 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
499 * Initiate connection to peer.
500 * Create a template for use in transmissions on this connection.
501 * Enter SYN_SENT state, and mark socket as connecting.
502 * Start keep-alive timer, and seed output sequence space.
503 * Send initial segment on connection.
506 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
510 struct tcpcb *tp = NULL;
511 struct sockaddr_in *sinp;
513 sinp = (struct sockaddr_in *)nam;
514 if (nam->sa_len != sizeof (*sinp))
517 * Must disallow TCP ``connections'' to multicast addresses.
519 if (sinp->sin_family == AF_INET
520 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
521 return (EAFNOSUPPORT);
522 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
527 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
529 if (inp->inp_flags & INP_TIMEWAIT) {
533 if (inp->inp_flags & INP_DROPPED) {
534 error = ECONNREFUSED;
539 if ((error = tcp_connect(tp, nam, td)) != 0)
542 if (registered_toedevs > 0 &&
543 (so->so_options & SO_NO_OFFLOAD) == 0 &&
544 (error = tcp_offload_connect(so, nam)) == 0)
547 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
548 error = tp->t_fb->tfb_tcp_output(tp);
550 TCPDEBUG2(PRU_CONNECT);
551 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
559 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
563 struct tcpcb *tp = NULL;
564 struct sockaddr_in6 *sin6p;
568 sin6p = (struct sockaddr_in6 *)nam;
569 if (nam->sa_len != sizeof (*sin6p))
572 * Must disallow TCP ``connections'' to multicast addresses.
574 if (sin6p->sin6_family == AF_INET6
575 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
576 return (EAFNOSUPPORT);
579 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
581 if (inp->inp_flags & INP_TIMEWAIT) {
585 if (inp->inp_flags & INP_DROPPED) {
586 error = ECONNREFUSED;
593 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
594 * therefore probably require the hash lock, which isn't held here.
595 * Is this a significant problem?
597 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
598 struct sockaddr_in sin;
600 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
604 if ((inp->inp_vflag & INP_IPV4) == 0) {
605 error = EAFNOSUPPORT;
609 in6_sin6_2_sin(&sin, sin6p);
610 inp->inp_vflag |= INP_IPV4;
611 inp->inp_vflag &= ~INP_IPV6;
612 if ((error = prison_remote_ip4(td->td_ucred,
613 &sin.sin_addr)) != 0)
615 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
618 if (registered_toedevs > 0 &&
619 (so->so_options & SO_NO_OFFLOAD) == 0 &&
620 (error = tcp_offload_connect(so, nam)) == 0)
623 error = tp->t_fb->tfb_tcp_output(tp);
626 if ((inp->inp_vflag & INP_IPV6) == 0) {
627 error = EAFNOSUPPORT;
632 inp->inp_vflag &= ~INP_IPV4;
633 inp->inp_vflag |= INP_IPV6;
634 inp->inp_inc.inc_flags |= INC_ISIPV6;
635 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
637 if ((error = tcp6_connect(tp, nam, td)) != 0)
640 if (registered_toedevs > 0 &&
641 (so->so_options & SO_NO_OFFLOAD) == 0 &&
642 (error = tcp_offload_connect(so, nam)) == 0)
645 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
646 error = tp->t_fb->tfb_tcp_output(tp);
649 TCPDEBUG2(PRU_CONNECT);
650 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
657 * Initiate disconnect from peer.
658 * If connection never passed embryonic stage, just drop;
659 * else if don't need to let data drain, then can just drop anyways,
660 * else have to begin TCP shutdown process: mark socket disconnecting,
661 * drain unread data, state switch to reflect user close, and
662 * send segment (e.g. FIN) to peer. Socket will be really disconnected
663 * when peer sends FIN and acks ours.
665 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
668 tcp_usr_disconnect(struct socket *so)
671 struct tcpcb *tp = NULL;
672 struct epoch_tracker et;
676 INP_INFO_RLOCK_ET(&V_tcbinfo, et);
678 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
680 if (inp->inp_flags & INP_TIMEWAIT)
682 if (inp->inp_flags & INP_DROPPED) {
690 TCPDEBUG2(PRU_DISCONNECT);
691 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
693 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
699 * Accept a connection. Essentially all the work is done at higher levels;
700 * just return the address of the peer, storing through addr.
703 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
706 struct inpcb *inp = NULL;
707 struct tcpcb *tp = NULL;
712 if (so->so_state & SS_ISDISCONNECTED)
713 return (ECONNABORTED);
716 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
718 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
719 error = ECONNABORTED;
726 * We inline in_getpeeraddr and COMMON_END here, so that we can
727 * copy the data of interest and defer the malloc until after we
730 port = inp->inp_fport;
731 addr = inp->inp_faddr;
734 TCPDEBUG2(PRU_ACCEPT);
735 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
738 *nam = in_sockaddr(port, &addr);
745 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
747 struct inpcb *inp = NULL;
749 struct tcpcb *tp = NULL;
751 struct in6_addr addr6;
752 struct epoch_tracker et;
757 if (so->so_state & SS_ISDISCONNECTED)
758 return (ECONNABORTED);
761 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
762 INP_INFO_RLOCK_ET(&V_tcbinfo, et);
764 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
765 error = ECONNABORTED;
772 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
773 * copy the data of interest and defer the malloc until after we
776 if (inp->inp_vflag & INP_IPV4) {
778 port = inp->inp_fport;
779 addr = inp->inp_faddr;
781 port = inp->inp_fport;
782 addr6 = inp->in6p_faddr;
786 TCPDEBUG2(PRU_ACCEPT);
787 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
789 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
792 *nam = in6_v4mapsin6_sockaddr(port, &addr);
794 *nam = in6_sockaddr(port, &addr6);
801 * Mark the connection as being incapable of further output.
804 tcp_usr_shutdown(struct socket *so)
808 struct tcpcb *tp = NULL;
809 struct epoch_tracker et;
812 INP_INFO_RLOCK_ET(&V_tcbinfo, et);
814 KASSERT(inp != NULL, ("inp == NULL"));
816 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
824 if (!(inp->inp_flags & INP_DROPPED))
825 error = tp->t_fb->tfb_tcp_output(tp);
828 TCPDEBUG2(PRU_SHUTDOWN);
829 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
831 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
837 * After a receive, possibly send window update to peer.
840 tcp_usr_rcvd(struct socket *so, int flags)
843 struct tcpcb *tp = NULL;
848 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
850 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
857 * For passively-created TFO connections, don't attempt a window
858 * update while still in SYN_RECEIVED as this may trigger an early
859 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
860 * application response data, or failing that, when the DELACK timer
863 if (IS_FASTOPEN(tp->t_flags) &&
864 (tp->t_state == TCPS_SYN_RECEIVED))
867 if (tp->t_flags & TF_TOE)
868 tcp_offload_rcvd(tp);
871 tp->t_fb->tfb_tcp_output(tp);
875 TCP_PROBE2(debug__user, tp, PRU_RCVD);
881 * Do a send by putting data in output queue and updating urgent
882 * marker if URG set. Possibly send more data. Unlike the other
883 * pru_*() routines, the mbuf chains are our responsibility. We
884 * must either enqueue them or free them. The other pru_* routines
885 * generally are caller-frees.
888 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
889 struct sockaddr *nam, struct mbuf *control, struct thread *td)
893 struct tcpcb *tp = NULL;
894 struct epoch_tracker net_et;
901 * We require the pcbinfo lock if we will close the socket as part of
904 if (flags & PRUS_EOF)
905 INP_INFO_RLOCK_ET(&V_tcbinfo, net_et);
907 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
909 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
913 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
914 * for freeing memory.
916 if (m && (flags & PRUS_NOTREADY) == 0)
922 isipv6 = nam && nam->sa_family == AF_INET6;
927 /* TCP doesn't do control messages (rights, creds, etc) */
928 if (control->m_len) {
935 m_freem(control); /* empty control, just free it */
937 if (!(flags & PRUS_OOB)) {
938 sbappendstream(&so->so_snd, m, flags);
939 if (nam && tp->t_state < TCPS_SYN_SENT) {
941 * Do implied connect if not yet connected,
942 * initialize window to default value, and
943 * initialize maxseg using peer's cached MSS.
947 error = tcp6_connect(tp, nam, td);
949 #if defined(INET6) && defined(INET)
953 error = tcp_connect(tp, nam, td);
957 if (IS_FASTOPEN(tp->t_flags))
958 tcp_fastopen_connect(tp);
960 tp->snd_wnd = TTCP_CLIENT_SND_WND;
964 if (flags & PRUS_EOF) {
966 * Close the send side of the connection after
969 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
973 if (!(inp->inp_flags & INP_DROPPED) &&
974 !(flags & PRUS_NOTREADY)) {
975 if (flags & PRUS_MORETOCOME)
976 tp->t_flags |= TF_MORETOCOME;
977 error = tp->t_fb->tfb_tcp_output(tp);
978 if (flags & PRUS_MORETOCOME)
979 tp->t_flags &= ~TF_MORETOCOME;
983 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
985 SOCKBUF_LOCK(&so->so_snd);
986 if (sbspace(&so->so_snd) < -512) {
987 SOCKBUF_UNLOCK(&so->so_snd);
993 * According to RFC961 (Assigned Protocols),
994 * the urgent pointer points to the last octet
995 * of urgent data. We continue, however,
996 * to consider it to indicate the first octet
997 * of data past the urgent section.
998 * Otherwise, snd_up should be one lower.
1000 sbappendstream_locked(&so->so_snd, m, flags);
1001 SOCKBUF_UNLOCK(&so->so_snd);
1002 if (nam && tp->t_state < TCPS_SYN_SENT) {
1004 * Do implied connect if not yet connected,
1005 * initialize window to default value, and
1006 * initialize maxseg using peer's cached MSS.
1010 * Not going to contemplate SYN|URG
1012 if (IS_FASTOPEN(tp->t_flags))
1013 tp->t_flags &= ~TF_FASTOPEN;
1016 error = tcp6_connect(tp, nam, td);
1018 #if defined(INET6) && defined(INET)
1022 error = tcp_connect(tp, nam, td);
1026 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1029 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1030 if (!(flags & PRUS_NOTREADY)) {
1031 tp->t_flags |= TF_FORCEDATA;
1032 error = tp->t_fb->tfb_tcp_output(tp);
1033 tp->t_flags &= ~TF_FORCEDATA;
1036 TCP_LOG_EVENT(tp, NULL,
1037 &inp->inp_socket->so_rcv,
1038 &inp->inp_socket->so_snd,
1039 TCP_LOG_USERSEND, error,
1042 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
1043 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1044 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1045 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1047 if (flags & PRUS_EOF)
1048 INP_INFO_RUNLOCK_ET(&V_tcbinfo, net_et);
1053 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1059 inp = sotoinpcb(so);
1061 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1063 for (int i = 0; i < count; i++)
1065 return (ECONNRESET);
1067 tp = intotcpcb(inp);
1069 SOCKBUF_LOCK(&so->so_snd);
1070 error = sbready(&so->so_snd, m, count);
1071 SOCKBUF_UNLOCK(&so->so_snd);
1073 error = tp->t_fb->tfb_tcp_output(tp);
1080 * Abort the TCP. Drop the connection abruptly.
1083 tcp_usr_abort(struct socket *so)
1086 struct tcpcb *tp = NULL;
1087 struct epoch_tracker et;
1090 inp = sotoinpcb(so);
1091 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1093 INP_INFO_RLOCK_ET(&V_tcbinfo, et);
1095 KASSERT(inp->inp_socket != NULL,
1096 ("tcp_usr_abort: inp_socket == NULL"));
1099 * If we still have full TCP state, and we're not dropped, drop.
1101 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1102 !(inp->inp_flags & INP_DROPPED)) {
1103 tp = intotcpcb(inp);
1105 tp = tcp_drop(tp, ECONNABORTED);
1108 TCPDEBUG2(PRU_ABORT);
1109 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1111 if (!(inp->inp_flags & INP_DROPPED)) {
1113 so->so_state |= SS_PROTOREF;
1115 inp->inp_flags |= INP_SOCKREF;
1119 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
1123 * TCP socket is closed. Start friendly disconnect.
1126 tcp_usr_close(struct socket *so)
1129 struct tcpcb *tp = NULL;
1130 struct epoch_tracker et;
1133 inp = sotoinpcb(so);
1134 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1136 INP_INFO_RLOCK_ET(&V_tcbinfo, et);
1138 KASSERT(inp->inp_socket != NULL,
1139 ("tcp_usr_close: inp_socket == NULL"));
1142 * If we still have full TCP state, and we're not dropped, initiate
1145 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1146 !(inp->inp_flags & INP_DROPPED)) {
1147 tp = intotcpcb(inp);
1150 TCPDEBUG2(PRU_CLOSE);
1151 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1153 if (!(inp->inp_flags & INP_DROPPED)) {
1155 so->so_state |= SS_PROTOREF;
1157 inp->inp_flags |= INP_SOCKREF;
1160 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
1164 * Receive out-of-band data.
1167 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1171 struct tcpcb *tp = NULL;
1174 inp = sotoinpcb(so);
1175 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1177 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1181 tp = intotcpcb(inp);
1183 if ((so->so_oobmark == 0 &&
1184 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1185 so->so_options & SO_OOBINLINE ||
1186 tp->t_oobflags & TCPOOB_HADDATA) {
1190 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1191 error = EWOULDBLOCK;
1195 *mtod(m, caddr_t) = tp->t_iobc;
1196 if ((flags & MSG_PEEK) == 0)
1197 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1200 TCPDEBUG2(PRU_RCVOOB);
1201 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1207 struct pr_usrreqs tcp_usrreqs = {
1208 .pru_abort = tcp_usr_abort,
1209 .pru_accept = tcp_usr_accept,
1210 .pru_attach = tcp_usr_attach,
1211 .pru_bind = tcp_usr_bind,
1212 .pru_connect = tcp_usr_connect,
1213 .pru_control = in_control,
1214 .pru_detach = tcp_usr_detach,
1215 .pru_disconnect = tcp_usr_disconnect,
1216 .pru_listen = tcp_usr_listen,
1217 .pru_peeraddr = in_getpeeraddr,
1218 .pru_rcvd = tcp_usr_rcvd,
1219 .pru_rcvoob = tcp_usr_rcvoob,
1220 .pru_send = tcp_usr_send,
1221 .pru_ready = tcp_usr_ready,
1222 .pru_shutdown = tcp_usr_shutdown,
1223 .pru_sockaddr = in_getsockaddr,
1224 .pru_sosetlabel = in_pcbsosetlabel,
1225 .pru_close = tcp_usr_close,
1230 struct pr_usrreqs tcp6_usrreqs = {
1231 .pru_abort = tcp_usr_abort,
1232 .pru_accept = tcp6_usr_accept,
1233 .pru_attach = tcp_usr_attach,
1234 .pru_bind = tcp6_usr_bind,
1235 .pru_connect = tcp6_usr_connect,
1236 .pru_control = in6_control,
1237 .pru_detach = tcp_usr_detach,
1238 .pru_disconnect = tcp_usr_disconnect,
1239 .pru_listen = tcp6_usr_listen,
1240 .pru_peeraddr = in6_mapped_peeraddr,
1241 .pru_rcvd = tcp_usr_rcvd,
1242 .pru_rcvoob = tcp_usr_rcvoob,
1243 .pru_send = tcp_usr_send,
1244 .pru_ready = tcp_usr_ready,
1245 .pru_shutdown = tcp_usr_shutdown,
1246 .pru_sockaddr = in6_mapped_sockaddr,
1247 .pru_sosetlabel = in_pcbsosetlabel,
1248 .pru_close = tcp_usr_close,
1254 * Common subroutine to open a TCP connection to remote host specified
1255 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1256 * port number if needed. Call in_pcbconnect_setup to do the routing and
1257 * to choose a local host address (interface). If there is an existing
1258 * incarnation of the same connection in TIME-WAIT state and if the remote
1259 * host was sending CC options and if the connection duration was < MSL, then
1260 * truncate the previous TIME-WAIT state and proceed.
1261 * Initialize connection parameters and enter SYN-SENT state.
1264 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1266 struct inpcb *inp = tp->t_inpcb, *oinp;
1267 struct socket *so = inp->inp_socket;
1268 struct in_addr laddr;
1272 INP_WLOCK_ASSERT(inp);
1273 INP_HASH_WLOCK(&V_tcbinfo);
1275 if (inp->inp_lport == 0) {
1276 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1282 * Cannot simply call in_pcbconnect, because there might be an
1283 * earlier incarnation of this same connection still in
1284 * TIME_WAIT state, creating an ADDRINUSE error.
1286 laddr = inp->inp_laddr;
1287 lport = inp->inp_lport;
1288 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1289 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1290 if (error && oinp == NULL)
1296 inp->inp_laddr = laddr;
1298 INP_HASH_WUNLOCK(&V_tcbinfo);
1301 * Compute window scaling to request:
1302 * Scale to fit into sweet spot. See tcp_syncache.c.
1303 * XXX: This should move to tcp_output().
1305 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1306 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1307 tp->request_r_scale++;
1310 TCPSTAT_INC(tcps_connattempt);
1311 tcp_state_change(tp, TCPS_SYN_SENT);
1312 tp->iss = tcp_new_isn(tp);
1313 tcp_sendseqinit(tp);
1318 INP_HASH_WUNLOCK(&V_tcbinfo);
1325 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1327 struct inpcb *inp = tp->t_inpcb;
1330 INP_WLOCK_ASSERT(inp);
1331 INP_HASH_WLOCK(&V_tcbinfo);
1333 if (inp->inp_lport == 0) {
1334 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1338 error = in6_pcbconnect(inp, nam, td->td_ucred);
1341 INP_HASH_WUNLOCK(&V_tcbinfo);
1343 /* Compute window scaling to request. */
1344 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1345 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1346 tp->request_r_scale++;
1348 soisconnecting(inp->inp_socket);
1349 TCPSTAT_INC(tcps_connattempt);
1350 tcp_state_change(tp, TCPS_SYN_SENT);
1351 tp->iss = tcp_new_isn(tp);
1352 tcp_sendseqinit(tp);
1357 INP_HASH_WUNLOCK(&V_tcbinfo);
1363 * Export TCP internal state information via a struct tcp_info, based on the
1364 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1365 * (TCP state machine, etc). We export all information using FreeBSD-native
1366 * constants -- for example, the numeric values for tcpi_state will differ
1370 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1373 INP_WLOCK_ASSERT(tp->t_inpcb);
1374 bzero(ti, sizeof(*ti));
1376 ti->tcpi_state = tp->t_state;
1377 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1378 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1379 if (tp->t_flags & TF_SACK_PERMIT)
1380 ti->tcpi_options |= TCPI_OPT_SACK;
1381 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1382 ti->tcpi_options |= TCPI_OPT_WSCALE;
1383 ti->tcpi_snd_wscale = tp->snd_scale;
1384 ti->tcpi_rcv_wscale = tp->rcv_scale;
1386 if (tp->t_flags & TF_ECN_PERMIT)
1387 ti->tcpi_options |= TCPI_OPT_ECN;
1389 ti->tcpi_rto = tp->t_rxtcur * tick;
1390 ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1391 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1392 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1394 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1395 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1398 * FreeBSD-specific extension fields for tcp_info.
1400 ti->tcpi_rcv_space = tp->rcv_wnd;
1401 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1402 ti->tcpi_snd_wnd = tp->snd_wnd;
1403 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1404 ti->tcpi_snd_nxt = tp->snd_nxt;
1405 ti->tcpi_snd_mss = tp->t_maxseg;
1406 ti->tcpi_rcv_mss = tp->t_maxseg;
1407 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1408 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1409 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1411 if (tp->t_flags & TF_TOE) {
1412 ti->tcpi_options |= TCPI_OPT_TOE;
1413 tcp_offload_tcp_info(tp, ti);
1419 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1420 * socket option arguments. When it re-acquires the lock after the copy, it
1421 * has to revalidate that the connection is still valid for the socket
1424 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \
1426 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1429 return (ECONNRESET); \
1431 tp = intotcpcb(inp); \
1433 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1436 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1441 struct tcp_function_block *blk;
1442 struct tcp_function_set fsn;
1445 inp = sotoinpcb(so);
1446 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1448 if (sopt->sopt_level != IPPROTO_TCP) {
1450 if (inp->inp_vflag & INP_IPV6PROTO) {
1452 error = ip6_ctloutput(so, sopt);
1455 #if defined(INET6) && defined(INET)
1461 error = ip_ctloutput(so, sopt);
1466 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1468 return (ECONNRESET);
1470 tp = intotcpcb(inp);
1472 * Protect the TCP option TCP_FUNCTION_BLK so
1473 * that a sub-function can *never* overwrite this.
1475 if ((sopt->sopt_dir == SOPT_SET) &&
1476 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1478 error = sooptcopyin(sopt, &fsn, sizeof fsn,
1482 INP_WLOCK_RECHECK(inp);
1483 blk = find_and_ref_tcp_functions(&fsn);
1488 if (tp->t_fb == blk) {
1489 /* You already have this */
1490 refcount_release(&blk->tfb_refcnt);
1494 if (tp->t_state != TCPS_CLOSED) {
1497 * The user has advanced the state
1498 * past the initial point, we may not
1499 * be able to switch.
1501 if (blk->tfb_tcp_handoff_ok != NULL) {
1503 * Does the stack provide a
1504 * query mechanism, if so it may
1505 * still be possible?
1507 error = (*blk->tfb_tcp_handoff_ok)(tp);
1510 refcount_release(&blk->tfb_refcnt);
1515 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1516 refcount_release(&blk->tfb_refcnt);
1521 * Release the old refcnt, the
1522 * lookup acquired a ref on the
1525 if (tp->t_fb->tfb_tcp_fb_fini) {
1527 * Tell the stack to cleanup with 0 i.e.
1528 * the tcb is not going away.
1530 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1533 /* Assure that we are not on any hpts */
1534 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
1536 if (blk->tfb_tcp_fb_init) {
1537 error = (*blk->tfb_tcp_fb_init)(tp);
1539 refcount_release(&blk->tfb_refcnt);
1540 if (tp->t_fb->tfb_tcp_fb_init) {
1541 if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) {
1542 /* Fall back failed, drop the connection */
1551 refcount_release(&tp->t_fb->tfb_refcnt);
1554 if (tp->t_flags & TF_TOE) {
1555 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1562 } else if ((sopt->sopt_dir == SOPT_GET) &&
1563 (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1564 strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1565 TCP_FUNCTION_NAME_LEN_MAX);
1566 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1567 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1569 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1572 /* Pass in the INP locked, called must unlock it */
1573 return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
1577 * If this assert becomes untrue, we need to change the size of the buf
1578 * variable in tcp_default_ctloutput().
1581 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1582 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1586 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
1588 int error, opt, optval;
1591 struct cc_algo *algo;
1592 char *pbuf, buf[TCP_LOG_ID_LEN];
1596 * For TCP_CCALGOOPT forward the control to CC module, for both
1597 * SOPT_SET and SOPT_GET.
1599 switch (sopt->sopt_name) {
1602 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1603 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1604 sopt->sopt_valsize);
1609 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1610 if (CC_ALGO(tp)->ctl_output != NULL)
1611 error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1615 if (error == 0 && sopt->sopt_dir == SOPT_GET)
1616 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1621 switch (sopt->sopt_dir) {
1623 switch (sopt->sopt_name) {
1624 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1626 if (!TCPMD5_ENABLED()) {
1628 return (ENOPROTOOPT);
1630 error = TCPMD5_PCBCTL(inp, sopt);
1633 goto unlock_and_done;
1639 error = sooptcopyin(sopt, &optval, sizeof optval,
1644 INP_WLOCK_RECHECK(inp);
1645 switch (sopt->sopt_name) {
1653 opt = 0; /* dead code to fool gcc */
1660 tp->t_flags &= ~opt;
1663 if (tp->t_flags & TF_TOE) {
1664 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1673 error = sooptcopyin(sopt, &optval, sizeof optval,
1678 INP_WLOCK_RECHECK(inp);
1680 tp->t_flags |= TF_NOPUSH;
1681 else if (tp->t_flags & TF_NOPUSH) {
1682 tp->t_flags &= ~TF_NOPUSH;
1683 if (TCPS_HAVEESTABLISHED(tp->t_state))
1684 error = tp->t_fb->tfb_tcp_output(tp);
1686 goto unlock_and_done;
1690 error = sooptcopyin(sopt, &optval, sizeof optval,
1695 INP_WLOCK_RECHECK(inp);
1696 if (optval > 0 && optval <= tp->t_maxseg &&
1697 optval + 40 >= V_tcp_minmss)
1698 tp->t_maxseg = optval;
1701 goto unlock_and_done;
1708 case TCP_CONGESTION:
1710 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1713 buf[sopt->sopt_valsize] = '\0';
1714 INP_WLOCK_RECHECK(inp);
1716 STAILQ_FOREACH(algo, &cc_list, entries)
1717 if (strncmp(buf, algo->name,
1718 TCP_CA_NAME_MAX) == 0)
1727 * We hold a write lock over the tcb so it's safe to
1728 * do these things without ordering concerns.
1730 if (CC_ALGO(tp)->cb_destroy != NULL)
1731 CC_ALGO(tp)->cb_destroy(tp->ccv);
1735 * If something goes pear shaped initialising the new
1736 * algo, fall back to newreno (which does not
1737 * require initialisation).
1739 if (algo->cb_init != NULL &&
1740 algo->cb_init(tp->ccv) != 0) {
1741 CC_ALGO(tp) = &newreno_cc_algo;
1743 * The only reason init should fail is
1744 * because of malloc.
1755 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1759 if (ui > (UINT_MAX / hz)) {
1765 INP_WLOCK_RECHECK(inp);
1766 switch (sopt->sopt_name) {
1768 tp->t_keepidle = ui;
1770 * XXX: better check current remaining
1771 * timeout and "merge" it with new value.
1773 if ((tp->t_state > TCPS_LISTEN) &&
1774 (tp->t_state <= TCPS_CLOSING))
1775 tcp_timer_activate(tp, TT_KEEP,
1779 tp->t_keepintvl = ui;
1780 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1781 (TP_MAXIDLE(tp) > 0))
1782 tcp_timer_activate(tp, TT_2MSL,
1786 tp->t_keepinit = ui;
1787 if (tp->t_state == TCPS_SYN_RECEIVED ||
1788 tp->t_state == TCPS_SYN_SENT)
1789 tcp_timer_activate(tp, TT_KEEP,
1793 goto unlock_and_done;
1797 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1801 INP_WLOCK_RECHECK(inp);
1803 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1804 (TP_MAXIDLE(tp) > 0))
1805 tcp_timer_activate(tp, TT_2MSL,
1807 goto unlock_and_done;
1813 error = sooptcopyin(sopt, &optval, sizeof optval,
1818 INP_WLOCK_RECHECK(inp);
1820 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
1821 &(tp->t_outpkts) : &(tp->t_inpkts),
1825 goto unlock_and_done;
1828 case TCP_FASTOPEN: {
1829 struct tcp_fastopen tfo_optval;
1832 if (!V_tcp_fastopen_client_enable &&
1833 !V_tcp_fastopen_server_enable)
1836 error = sooptcopyin(sopt, &tfo_optval,
1837 sizeof(tfo_optval), sizeof(int));
1841 INP_WLOCK_RECHECK(inp);
1842 if (tfo_optval.enable) {
1843 if (tp->t_state == TCPS_LISTEN) {
1844 if (!V_tcp_fastopen_server_enable) {
1846 goto unlock_and_done;
1849 tp->t_flags |= TF_FASTOPEN;
1850 if (tp->t_tfo_pending == NULL)
1852 tcp_fastopen_alloc_counter();
1855 * If a pre-shared key was provided,
1856 * stash it in the client cookie
1857 * field of the tcpcb for use during
1860 if (sopt->sopt_valsize ==
1861 sizeof(tfo_optval)) {
1862 memcpy(tp->t_tfo_cookie.client,
1864 TCP_FASTOPEN_PSK_LEN);
1865 tp->t_tfo_client_cookie_len =
1866 TCP_FASTOPEN_PSK_LEN;
1868 tp->t_flags |= TF_FASTOPEN;
1871 tp->t_flags &= ~TF_FASTOPEN;
1872 goto unlock_and_done;
1878 error = sooptcopyin(sopt, &optval, sizeof optval,
1883 INP_WLOCK_RECHECK(inp);
1884 error = tcp_log_state_change(tp, optval);
1885 goto unlock_and_done;
1894 error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
1897 buf[sopt->sopt_valsize] = '\0';
1898 INP_WLOCK_RECHECK(inp);
1899 error = tcp_log_set_id(tp, buf);
1900 /* tcp_log_set_id() unlocks the INP. */
1907 sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
1910 buf[sopt->sopt_valsize] = '\0';
1911 INP_WLOCK_RECHECK(inp);
1912 if (sopt->sopt_name == TCP_LOGDUMP) {
1913 error = tcp_log_dump_tp_logbuf(tp, buf,
1917 tcp_log_dump_tp_bucket_logbufs(tp, buf);
1919 * tcp_log_dump_tp_bucket_logbufs() drops the
1928 error = ENOPROTOOPT;
1934 tp = intotcpcb(inp);
1935 switch (sopt->sopt_name) {
1936 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1938 if (!TCPMD5_ENABLED()) {
1940 return (ENOPROTOOPT);
1942 error = TCPMD5_PCBCTL(inp, sopt);
1947 optval = tp->t_flags & TF_NODELAY;
1949 error = sooptcopyout(sopt, &optval, sizeof optval);
1952 optval = tp->t_maxseg;
1954 error = sooptcopyout(sopt, &optval, sizeof optval);
1957 optval = tp->t_flags & TF_NOOPT;
1959 error = sooptcopyout(sopt, &optval, sizeof optval);
1962 optval = tp->t_flags & TF_NOPUSH;
1964 error = sooptcopyout(sopt, &optval, sizeof optval);
1967 tcp_fill_info(tp, &ti);
1969 error = sooptcopyout(sopt, &ti, sizeof ti);
1971 case TCP_CONGESTION:
1972 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1974 error = sooptcopyout(sopt, buf, len + 1);
1980 switch (sopt->sopt_name) {
1982 ui = TP_KEEPIDLE(tp) / hz;
1985 ui = TP_KEEPINTVL(tp) / hz;
1988 ui = TP_KEEPINIT(tp) / hz;
1991 ui = TP_KEEPCNT(tp);
1995 error = sooptcopyout(sopt, &ui, sizeof(ui));
2000 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
2001 &(tp->t_outpkts) : &(tp->t_inpkts));
2003 error = sooptcopyout(sopt, &optval, sizeof optval);
2007 optval = tp->t_flags & TF_FASTOPEN;
2009 error = sooptcopyout(sopt, &optval, sizeof optval);
2013 optval = tp->t_logstate;
2015 error = sooptcopyout(sopt, &optval, sizeof(optval));
2018 /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2019 error = tcp_log_getlogbuf(sopt, tp);
2022 len = tcp_log_get_id(tp, buf);
2024 error = sooptcopyout(sopt, buf, len + 1);
2034 error = ENOPROTOOPT;
2041 #undef INP_WLOCK_RECHECK
2042 #undef INP_WLOCK_RECHECK_CLEANUP
2045 * Attach TCP protocol to socket, allocating
2046 * internet protocol control block, tcp control block,
2047 * bufer space, and entering LISTEN state if to accept connections.
2050 tcp_attach(struct socket *so)
2054 struct epoch_tracker et;
2057 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
2058 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
2062 so->so_rcv.sb_flags |= SB_AUTOSIZE;
2063 so->so_snd.sb_flags |= SB_AUTOSIZE;
2064 INP_INFO_RLOCK_ET(&V_tcbinfo, et);
2065 error = in_pcballoc(so, &V_tcbinfo);
2067 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
2070 inp = sotoinpcb(so);
2072 if (inp->inp_vflag & INP_IPV6PROTO) {
2073 inp->inp_vflag |= INP_IPV6;
2074 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
2075 inp->inp_vflag |= INP_IPV4;
2076 inp->in6p_hops = -1; /* use kernel default */
2080 inp->inp_vflag |= INP_IPV4;
2081 tp = tcp_newtcpcb(inp);
2085 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
2088 tp->t_state = TCPS_CLOSED;
2090 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
2091 TCPSTATES_INC(TCPS_CLOSED);
2096 * Initiate (or continue) disconnect.
2097 * If embryonic state, just send reset (once).
2098 * If in ``let data drain'' option and linger null, just drop.
2099 * Otherwise (hard), mark socket disconnecting and drop
2100 * current input data; switch states based on user close, and
2101 * send segment to peer (with FIN).
2104 tcp_disconnect(struct tcpcb *tp)
2106 struct inpcb *inp = tp->t_inpcb;
2107 struct socket *so = inp->inp_socket;
2109 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2110 INP_WLOCK_ASSERT(inp);
2113 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2114 * socket is still open.
2116 if (tp->t_state < TCPS_ESTABLISHED) {
2119 ("tcp_disconnect: tcp_close() returned NULL"));
2120 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2121 tp = tcp_drop(tp, 0);
2123 ("tcp_disconnect: tcp_drop() returned NULL"));
2125 soisdisconnecting(so);
2126 sbflush(&so->so_rcv);
2128 if (!(inp->inp_flags & INP_DROPPED))
2129 tp->t_fb->tfb_tcp_output(tp);
2134 * User issued close, and wish to trail through shutdown states:
2135 * if never received SYN, just forget it. If got a SYN from peer,
2136 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2137 * If already got a FIN from peer, then almost done; go to LAST_ACK
2138 * state. In all other cases, have already sent FIN to peer (e.g.
2139 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2140 * for peer to send FIN or not respond to keep-alives, etc.
2141 * We can let the user exit from the close as soon as the FIN is acked.
2144 tcp_usrclosed(struct tcpcb *tp)
2147 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2148 INP_WLOCK_ASSERT(tp->t_inpcb);
2150 switch (tp->t_state) {
2153 tcp_offload_listen_stop(tp);
2155 tcp_state_change(tp, TCPS_CLOSED);
2160 * tcp_close() should never return NULL here as the socket is
2164 ("tcp_usrclosed: tcp_close() returned NULL"));
2168 case TCPS_SYN_RECEIVED:
2169 tp->t_flags |= TF_NEEDFIN;
2172 case TCPS_ESTABLISHED:
2173 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2176 case TCPS_CLOSE_WAIT:
2177 tcp_state_change(tp, TCPS_LAST_ACK);
2180 if (tp->t_state >= TCPS_FIN_WAIT_2) {
2181 soisdisconnected(tp->t_inpcb->inp_socket);
2182 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
2183 if (tp->t_state == TCPS_FIN_WAIT_2) {
2186 timeout = (tcp_fast_finwait2_recycle) ?
2187 tcp_finwait2_timeout : TP_MAXIDLE(tp);
2188 tcp_timer_activate(tp, TT_2MSL, timeout);
2195 db_print_indent(int indent)
2199 for (i = 0; i < indent; i++)
2204 db_print_tstate(int t_state)
2209 db_printf("TCPS_CLOSED");
2213 db_printf("TCPS_LISTEN");
2217 db_printf("TCPS_SYN_SENT");
2220 case TCPS_SYN_RECEIVED:
2221 db_printf("TCPS_SYN_RECEIVED");
2224 case TCPS_ESTABLISHED:
2225 db_printf("TCPS_ESTABLISHED");
2228 case TCPS_CLOSE_WAIT:
2229 db_printf("TCPS_CLOSE_WAIT");
2232 case TCPS_FIN_WAIT_1:
2233 db_printf("TCPS_FIN_WAIT_1");
2237 db_printf("TCPS_CLOSING");
2241 db_printf("TCPS_LAST_ACK");
2244 case TCPS_FIN_WAIT_2:
2245 db_printf("TCPS_FIN_WAIT_2");
2248 case TCPS_TIME_WAIT:
2249 db_printf("TCPS_TIME_WAIT");
2253 db_printf("unknown");
2259 db_print_tflags(u_int t_flags)
2264 if (t_flags & TF_ACKNOW) {
2265 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2268 if (t_flags & TF_DELACK) {
2269 db_printf("%sTF_DELACK", comma ? ", " : "");
2272 if (t_flags & TF_NODELAY) {
2273 db_printf("%sTF_NODELAY", comma ? ", " : "");
2276 if (t_flags & TF_NOOPT) {
2277 db_printf("%sTF_NOOPT", comma ? ", " : "");
2280 if (t_flags & TF_SENTFIN) {
2281 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2284 if (t_flags & TF_REQ_SCALE) {
2285 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2288 if (t_flags & TF_RCVD_SCALE) {
2289 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2292 if (t_flags & TF_REQ_TSTMP) {
2293 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2296 if (t_flags & TF_RCVD_TSTMP) {
2297 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2300 if (t_flags & TF_SACK_PERMIT) {
2301 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2304 if (t_flags & TF_NEEDSYN) {
2305 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2308 if (t_flags & TF_NEEDFIN) {
2309 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2312 if (t_flags & TF_NOPUSH) {
2313 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2316 if (t_flags & TF_MORETOCOME) {
2317 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2320 if (t_flags & TF_LQ_OVERFLOW) {
2321 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2324 if (t_flags & TF_LASTIDLE) {
2325 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2328 if (t_flags & TF_RXWIN0SENT) {
2329 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2332 if (t_flags & TF_FASTRECOVERY) {
2333 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2336 if (t_flags & TF_CONGRECOVERY) {
2337 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2340 if (t_flags & TF_WASFRECOVERY) {
2341 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2344 if (t_flags & TF_SIGNATURE) {
2345 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2348 if (t_flags & TF_FORCEDATA) {
2349 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2352 if (t_flags & TF_TSO) {
2353 db_printf("%sTF_TSO", comma ? ", " : "");
2356 if (t_flags & TF_ECN_PERMIT) {
2357 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2360 if (t_flags & TF_FASTOPEN) {
2361 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2367 db_print_toobflags(char t_oobflags)
2372 if (t_oobflags & TCPOOB_HAVEDATA) {
2373 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2376 if (t_oobflags & TCPOOB_HADDATA) {
2377 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2383 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2386 db_print_indent(indent);
2387 db_printf("%s at %p\n", name, tp);
2391 db_print_indent(indent);
2392 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2393 LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2395 db_print_indent(indent);
2396 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
2397 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2399 db_print_indent(indent);
2400 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2401 &tp->t_timers->tt_delack, tp->t_inpcb);
2403 db_print_indent(indent);
2404 db_printf("t_state: %d (", tp->t_state);
2405 db_print_tstate(tp->t_state);
2408 db_print_indent(indent);
2409 db_printf("t_flags: 0x%x (", tp->t_flags);
2410 db_print_tflags(tp->t_flags);
2413 db_print_indent(indent);
2414 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2415 tp->snd_una, tp->snd_max, tp->snd_nxt);
2417 db_print_indent(indent);
2418 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2419 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2421 db_print_indent(indent);
2422 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2423 tp->iss, tp->irs, tp->rcv_nxt);
2425 db_print_indent(indent);
2426 db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n",
2427 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2429 db_print_indent(indent);
2430 db_printf("snd_wnd: %u snd_cwnd: %u\n",
2431 tp->snd_wnd, tp->snd_cwnd);
2433 db_print_indent(indent);
2434 db_printf("snd_ssthresh: %u snd_recover: "
2435 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2437 db_print_indent(indent);
2438 db_printf("t_rcvtime: %u t_startime: %u\n",
2439 tp->t_rcvtime, tp->t_starttime);
2441 db_print_indent(indent);
2442 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2443 tp->t_rtttime, tp->t_rtseq);
2445 db_print_indent(indent);
2446 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2447 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2449 db_print_indent(indent);
2450 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2451 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2454 db_print_indent(indent);
2455 db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n",
2456 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2458 db_print_indent(indent);
2459 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2460 db_print_toobflags(tp->t_oobflags);
2461 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2463 db_print_indent(indent);
2464 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2465 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2467 db_print_indent(indent);
2468 db_printf("ts_recent: %u ts_recent_age: %u\n",
2469 tp->ts_recent, tp->ts_recent_age);
2471 db_print_indent(indent);
2472 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2473 "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2475 db_print_indent(indent);
2476 db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x "
2477 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2478 tp->snd_recover_prev, tp->t_badrxtwin);
2480 db_print_indent(indent);
2481 db_printf("snd_numholes: %d snd_holes first: %p\n",
2482 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2484 db_print_indent(indent);
2485 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
2486 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2488 /* Skip sackblks, sackhint. */
2490 db_print_indent(indent);
2491 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2492 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2495 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2500 db_printf("usage: show tcpcb <addr>\n");
2503 tp = (struct tcpcb *)addr;
2505 db_print_tcpcb(tp, "tcpcb", 0);