1c398230bSWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1993 5623dce13SRobert Watson * The Regents of the University of California. 6497057eeSRobert Watson * Copyright (c) 2006-2007 Robert N. M. Watson 7fa046d87SRobert Watson * Copyright (c) 2010-2011 Juniper Networks, Inc. 8623dce13SRobert Watson * All rights reserved. 9df8bae1dSRodney W. Grimes * 10fa046d87SRobert Watson * Portions of this software were developed by Robert N. M. Watson under 11fa046d87SRobert Watson * contract to Juniper Networks, Inc. 12fa046d87SRobert Watson * 13df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 14df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 15df8bae1dSRodney W. Grimes * are met: 16df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 17df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 18df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 19df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 20df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 21fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 22df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 23df8bae1dSRodney W. Grimes * without specific prior written permission. 24df8bae1dSRodney W. Grimes * 25df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35df8bae1dSRodney W. Grimes * SUCH DAMAGE. 36df8bae1dSRodney W. Grimes * 371fdbc7aeSGarrett Wollman * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94 38df8bae1dSRodney W. Grimes */ 39df8bae1dSRodney W. Grimes 404b421e2dSMike Silbersack #include <sys/cdefs.h> 414b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 424b421e2dSMike Silbersack 43497057eeSRobert Watson #include "opt_ddb.h" 441cfd4b53SBruce M Simpson #include "opt_inet.h" 45fb59c426SYoshinobu Inoue #include "opt_inet6.h" 46fcf59617SAndrey V. Elsukov #include "opt_ipsec.h" 47b2e60773SJohn Baldwin #include "opt_kern_tls.h" 480cc12cc5SJoerg Wunsch 49df8bae1dSRodney W. Grimes #include <sys/param.h> 50df8bae1dSRodney W. Grimes #include <sys/systm.h> 51adc56f5aSEdward Tomasz Napierala #include <sys/arb.h> 529077f387SGleb Smirnoff #include <sys/limits.h> 53f76fcf6dSJeffrey Hsu #include <sys/malloc.h> 5455bceb1eSRandall Stewart #include <sys/refcount.h> 55c7a82f90SGarrett Wollman #include <sys/kernel.h> 56b2e60773SJohn Baldwin #include <sys/ktls.h> 57adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h> 5898163b98SPoul-Henning Kamp #include <sys/sysctl.h> 59df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 60fb59c426SYoshinobu Inoue #ifdef INET6 61fb59c426SYoshinobu Inoue #include <sys/domain.h> 62fb59c426SYoshinobu Inoue #endif /* INET6 */ 63df8bae1dSRodney W. Grimes #include <sys/socket.h> 64df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 65df8bae1dSRodney W. Grimes #include <sys/protosw.h> 6691421ba2SRobert Watson #include <sys/proc.h> 6791421ba2SRobert Watson #include <sys/jail.h> 68adc56f5aSEdward Tomasz Napierala #include <sys/stats.h> 69df8bae1dSRodney W. Grimes 70497057eeSRobert Watson #ifdef DDB 71497057eeSRobert Watson #include <ddb/ddb.h> 72497057eeSRobert Watson #endif 73497057eeSRobert Watson 74df8bae1dSRodney W. Grimes #include <net/if.h> 7576039bc8SGleb Smirnoff #include <net/if_var.h> 76df8bae1dSRodney W. Grimes #include <net/route.h> 77530c0060SRobert Watson #include <net/vnet.h> 78df8bae1dSRodney W. Grimes 79df8bae1dSRodney W. Grimes #include <netinet/in.h> 805d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h> 81df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 82b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h> 83b5e8ce9fSBruce Evans #include <netinet/in_var.h> 843b0ee680SRichard Scheffenegger #include <netinet/ip.h> 85df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 86fb59c426SYoshinobu Inoue #ifdef INET6 87b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h> 88b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h> 89fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 90a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h> 91fb59c426SYoshinobu Inoue #endif 922de3e790SGleb Smirnoff #include <netinet/tcp.h> 93df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 94df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 95df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 96df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 972529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h> 98df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 994644fda3SGleb Smirnoff #include <netinet/cc/cc.h> 100c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h> 101fd389e7cSRandall Stewart #include <netinet/tcp_hpts.h> 10286a996e6SHiren Panchasara #ifdef TCPPCAP 10386a996e6SHiren Panchasara #include <netinet/tcp_pcap.h> 10486a996e6SHiren Panchasara #endif 10509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 106bc65987aSKip Macy #include <netinet/tcp_offload.h> 10709fe6320SNavdeep Parhar #endif 108fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 109df8bae1dSRodney W. Grimes 110adc56f5aSEdward Tomasz Napierala #include <vm/vm.h> 111adc56f5aSEdward Tomasz Napierala #include <vm/vm_param.h> 112adc56f5aSEdward Tomasz Napierala #include <vm/pmap.h> 113adc56f5aSEdward Tomasz Napierala #include <vm/vm_extern.h> 114adc56f5aSEdward Tomasz Napierala #include <vm/vm_map.h> 115adc56f5aSEdward Tomasz Napierala #include <vm/vm_page.h> 116adc56f5aSEdward Tomasz Napierala 117df8bae1dSRodney W. Grimes /* 118df8bae1dSRodney W. Grimes * TCP protocol interface to socket abstraction. 119df8bae1dSRodney W. Grimes */ 120b287c6c7SBjoern A. Zeeb #ifdef INET 1214d77a549SAlfred Perlstein static int tcp_connect(struct tcpcb *, struct sockaddr *, 1224d77a549SAlfred Perlstein struct thread *td); 123b287c6c7SBjoern A. Zeeb #endif /* INET */ 124fb59c426SYoshinobu Inoue #ifdef INET6 1254d77a549SAlfred Perlstein static int tcp6_connect(struct tcpcb *, struct sockaddr *, 1264d77a549SAlfred Perlstein struct thread *td); 127fb59c426SYoshinobu Inoue #endif /* INET6 */ 128623dce13SRobert Watson static void tcp_disconnect(struct tcpcb *); 129623dce13SRobert Watson static void tcp_usrclosed(struct tcpcb *); 130b8af5dfaSRobert Watson static void tcp_fill_info(struct tcpcb *, struct tcp_info *); 1312c37256eSGarrett Wollman 132d3b6c96bSRandall Stewart static int tcp_pru_options_support(struct tcpcb *tp, int flags); 133d3b6c96bSRandall Stewart 1342c37256eSGarrett Wollman /* 13525102351SMike Karels * tcp_require_unique port requires a globally-unique source port for each 13625102351SMike Karels * outgoing connection. The default is to require the 4-tuple to be unique. 13725102351SMike Karels */ 13825102351SMike Karels VNET_DEFINE(int, tcp_require_unique_port) = 0; 13925102351SMike Karels SYSCTL_INT(_net_inet_tcp, OID_AUTO, require_unique_port, 14025102351SMike Karels CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_require_unique_port), 0, 14125102351SMike Karels "Require globally-unique ephemeral port for outgoing connections"); 14225102351SMike Karels #define V_tcp_require_unique_port VNET(tcp_require_unique_port) 14325102351SMike Karels 14425102351SMike Karels /* 1452c37256eSGarrett Wollman * TCP attaches to socket via pru_attach(), reserving space, 1462c37256eSGarrett Wollman * and an internet control block. 1472c37256eSGarrett Wollman */ 1482c37256eSGarrett Wollman static int 149b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td) 1502c37256eSGarrett Wollman { 151f76fcf6dSJeffrey Hsu struct inpcb *inp; 152623dce13SRobert Watson struct tcpcb *tp = NULL; 153623dce13SRobert Watson int error; 1542c37256eSGarrett Wollman 155623dce13SRobert Watson inp = sotoinpcb(so); 156623dce13SRobert Watson KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL")); 1572c37256eSGarrett Wollman 1580f6385e7SGleb Smirnoff error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace); 1592c37256eSGarrett Wollman if (error) 1602c37256eSGarrett Wollman goto out; 1612c37256eSGarrett Wollman 1620f6385e7SGleb Smirnoff so->so_rcv.sb_flags |= SB_AUTOSIZE; 1630f6385e7SGleb Smirnoff so->so_snd.sb_flags |= SB_AUTOSIZE; 1640f6385e7SGleb Smirnoff error = in_pcballoc(so, &V_tcbinfo); 1657669c586SGleb Smirnoff if (error) 1660f6385e7SGleb Smirnoff goto out; 1670f6385e7SGleb Smirnoff inp = sotoinpcb(so); 1680f6385e7SGleb Smirnoff tp = tcp_newtcpcb(inp); 1690f6385e7SGleb Smirnoff if (tp == NULL) { 1707669c586SGleb Smirnoff error = ENOBUFS; 1710f6385e7SGleb Smirnoff in_pcbdetach(inp); 1720f6385e7SGleb Smirnoff in_pcbfree(inp); 1730f6385e7SGleb Smirnoff goto out; 1740f6385e7SGleb Smirnoff } 1750f6385e7SGleb Smirnoff tp->t_state = TCPS_CLOSED; 1760f6385e7SGleb Smirnoff INP_WUNLOCK(inp); 1770f6385e7SGleb Smirnoff TCPSTATES_INC(TCPS_CLOSED); 1782c37256eSGarrett Wollman out: 1795d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ATTACH); 1800f6385e7SGleb Smirnoff return (error); 1812c37256eSGarrett Wollman } 1822c37256eSGarrett Wollman 1832c37256eSGarrett Wollman /* 1843fed74e9SGleb Smirnoff * tcp_usr_detach is called when the socket layer loses its final reference 185a152f8a3SRobert Watson * to the socket, be it a file descriptor reference, a reference from TCP, 186a152f8a3SRobert Watson * etc. At this point, there is only one case in which we will keep around 187a152f8a3SRobert Watson * inpcb state: time wait. 1882c37256eSGarrett Wollman */ 189bc725eafSRobert Watson static void 1903fed74e9SGleb Smirnoff tcp_usr_detach(struct socket *so) 1912c37256eSGarrett Wollman { 1923fed74e9SGleb Smirnoff struct inpcb *inp; 1932c37256eSGarrett Wollman struct tcpcb *tp; 1942c37256eSGarrett Wollman 1953fed74e9SGleb Smirnoff inp = sotoinpcb(so); 1963fed74e9SGleb Smirnoff KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 1973fed74e9SGleb Smirnoff INP_WLOCK(inp); 1983fed74e9SGleb Smirnoff KASSERT(so->so_pcb == inp && inp->inp_socket == so, 1993fed74e9SGleb Smirnoff ("%s: socket %p inp %p mismatch", __func__, so, inp)); 200953b5606SRobert Watson 201a152f8a3SRobert Watson tp = intotcpcb(inp); 202a152f8a3SRobert Watson 2031b91978fSGleb Smirnoff KASSERT(inp->inp_flags & INP_DROPPED || 2041b91978fSGleb Smirnoff tp->t_state < TCPS_SYN_SENT, 2051b91978fSGleb Smirnoff ("%s: inp %p not dropped or embryonic", __func__, inp)); 2069c3507f9SGleb Smirnoff 207623dce13SRobert Watson tcp_discardcb(tp); 208623dce13SRobert Watson in_pcbdetach(inp); 2090206cdb8SBjoern A. Zeeb in_pcbfree(inp); 210623dce13SRobert Watson } 211c78cbc7bSRobert Watson 212b287c6c7SBjoern A. Zeeb #ifdef INET 2132c37256eSGarrett Wollman /* 2142c37256eSGarrett Wollman * Give the socket an address. 2152c37256eSGarrett Wollman */ 2162c37256eSGarrett Wollman static int 217b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 2182c37256eSGarrett Wollman { 2192c37256eSGarrett Wollman int error = 0; 220f76fcf6dSJeffrey Hsu struct inpcb *inp; 221b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS 222623dce13SRobert Watson struct tcpcb *tp = NULL; 223b338b1fdSMateusz Guzik #endif 2242c37256eSGarrett Wollman struct sockaddr_in *sinp; 2252c37256eSGarrett Wollman 22652710de1SPawel Jakub Dawidek sinp = (struct sockaddr_in *)nam; 227f96603b5SMark Johnston if (nam->sa_family != AF_INET) { 228f96603b5SMark Johnston /* 229f96603b5SMark Johnston * Preserve compatibility with old programs. 230f96603b5SMark Johnston */ 231f96603b5SMark Johnston if (nam->sa_family != AF_UNSPEC || 2323f1f6b6eSMichael Tuexen nam->sa_len < offsetof(struct sockaddr_in, sin_zero) || 233f96603b5SMark Johnston sinp->sin_addr.s_addr != INADDR_ANY) 234f161d294SMark Johnston return (EAFNOSUPPORT); 235f96603b5SMark Johnston nam->sa_family = AF_INET; 236f96603b5SMark Johnston } 23752710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof(*sinp)) 23852710de1SPawel Jakub Dawidek return (EINVAL); 239f161d294SMark Johnston 2402c37256eSGarrett Wollman /* 2412c37256eSGarrett Wollman * Must check for multicast addresses and disallow binding 2422c37256eSGarrett Wollman * to them. 2432c37256eSGarrett Wollman */ 244f161d294SMark Johnston if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 24552710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 24652710de1SPawel Jakub Dawidek 247623dce13SRobert Watson inp = sotoinpcb(so); 248623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 2498501a69cSRobert Watson INP_WLOCK(inp); 25053af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 251623dce13SRobert Watson error = EINVAL; 2522c37256eSGarrett Wollman goto out; 253623dce13SRobert Watson } 254b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS 255623dce13SRobert Watson tp = intotcpcb(inp); 256b338b1fdSMateusz Guzik #endif 257fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 258623dce13SRobert Watson error = in_pcbbind(inp, nam, td->td_ucred); 259fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 260623dce13SRobert Watson out: 2615d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 2628501a69cSRobert Watson INP_WUNLOCK(inp); 263623dce13SRobert Watson 264623dce13SRobert Watson return (error); 2652c37256eSGarrett Wollman } 266b287c6c7SBjoern A. Zeeb #endif /* INET */ 2672c37256eSGarrett Wollman 268fb59c426SYoshinobu Inoue #ifdef INET6 269fb59c426SYoshinobu Inoue static int 270b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 271fb59c426SYoshinobu Inoue { 272fb59c426SYoshinobu Inoue int error = 0; 273f76fcf6dSJeffrey Hsu struct inpcb *inp; 274b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS 275623dce13SRobert Watson struct tcpcb *tp = NULL; 276b338b1fdSMateusz Guzik #endif 2770ecd976eSBjoern A. Zeeb struct sockaddr_in6 *sin6; 2784a91aa8fSMichael Tuexen u_char vflagsav; 279fb59c426SYoshinobu Inoue 2800ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 281f161d294SMark Johnston if (nam->sa_family != AF_INET6) 282f161d294SMark Johnston return (EAFNOSUPPORT); 2830ecd976eSBjoern A. Zeeb if (nam->sa_len != sizeof(*sin6)) 28452710de1SPawel Jakub Dawidek return (EINVAL); 285f161d294SMark Johnston 286fb59c426SYoshinobu Inoue /* 287fb59c426SYoshinobu Inoue * Must check for multicast addresses and disallow binding 288fb59c426SYoshinobu Inoue * to them. 289fb59c426SYoshinobu Inoue */ 290f161d294SMark Johnston if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 29152710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 29252710de1SPawel Jakub Dawidek 293623dce13SRobert Watson inp = sotoinpcb(so); 294623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 2958501a69cSRobert Watson INP_WLOCK(inp); 2964a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 29753af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 298623dce13SRobert Watson error = EINVAL; 299623dce13SRobert Watson goto out; 300623dce13SRobert Watson } 301b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS 302623dce13SRobert Watson tp = intotcpcb(inp); 303b338b1fdSMateusz Guzik #endif 304fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 305fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 306fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 307b287c6c7SBjoern A. Zeeb #ifdef INET 30866ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 3090ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 310fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 3110ecd976eSBjoern A. Zeeb else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 312fb59c426SYoshinobu Inoue struct sockaddr_in sin; 313fb59c426SYoshinobu Inoue 3140ecd976eSBjoern A. Zeeb in6_sin6_2_sin(&sin, sin6); 315888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 316888973f5SMichael Tuexen error = EAFNOSUPPORT; 317888973f5SMichael Tuexen INP_HASH_WUNLOCK(&V_tcbinfo); 318888973f5SMichael Tuexen goto out; 319888973f5SMichael Tuexen } 320fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 321fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 322b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)&sin, 323b0330ed9SPawel Jakub Dawidek td->td_ucred); 324fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 325fb59c426SYoshinobu Inoue goto out; 326fb59c426SYoshinobu Inoue } 327fb59c426SYoshinobu Inoue } 328b287c6c7SBjoern A. Zeeb #endif 329b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, nam, td->td_ucred); 330fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 331623dce13SRobert Watson out: 3324a91aa8fSMichael Tuexen if (error != 0) 3334a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 3345d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 3358501a69cSRobert Watson INP_WUNLOCK(inp); 336623dce13SRobert Watson return (error); 337fb59c426SYoshinobu Inoue } 338fb59c426SYoshinobu Inoue #endif /* INET6 */ 339fb59c426SYoshinobu Inoue 340b287c6c7SBjoern A. Zeeb #ifdef INET 3412c37256eSGarrett Wollman /* 3422c37256eSGarrett Wollman * Prepare to accept connections. 3432c37256eSGarrett Wollman */ 3442c37256eSGarrett Wollman static int 345d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td) 3462c37256eSGarrett Wollman { 3472c37256eSGarrett Wollman int error = 0; 348f76fcf6dSJeffrey Hsu struct inpcb *inp; 349623dce13SRobert Watson struct tcpcb *tp = NULL; 3502c37256eSGarrett Wollman 351623dce13SRobert Watson inp = sotoinpcb(so); 352623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 3538501a69cSRobert Watson INP_WLOCK(inp); 35453af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 355623dce13SRobert Watson error = EINVAL; 356623dce13SRobert Watson goto out; 357623dce13SRobert Watson } 358623dce13SRobert Watson tp = intotcpcb(inp); 3590daccb9cSRobert Watson SOCK_LOCK(so); 3600daccb9cSRobert Watson error = solisten_proto_check(so); 361bd4a39ccSMark Johnston if (error != 0) { 362bd4a39ccSMark Johnston SOCK_UNLOCK(so); 363bd4a39ccSMark Johnston goto out; 364bd4a39ccSMark Johnston } 365bd4a39ccSMark Johnston if (inp->inp_lport == 0) { 366fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 367bd4a39ccSMark Johnston error = in_pcbbind(inp, NULL, td->td_ucred); 368fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 369bd4a39ccSMark Johnston } 3700daccb9cSRobert Watson if (error == 0) { 37157f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 372d374e81eSRobert Watson solisten_proto(so, backlog); 37309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 37437cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 37509fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 37609fe6320SNavdeep Parhar #endif 377bd4a39ccSMark Johnston } else { 378bd4a39ccSMark Johnston solisten_proto_abort(so); 3790daccb9cSRobert Watson } 3800daccb9cSRobert Watson SOCK_UNLOCK(so); 381623dce13SRobert Watson 38268bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) 383281a0fd4SPatrick Kelsey tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 38418a75309SPatrick Kelsey 385623dce13SRobert Watson out: 3865d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 3878501a69cSRobert Watson INP_WUNLOCK(inp); 388623dce13SRobert Watson return (error); 3892c37256eSGarrett Wollman } 390b287c6c7SBjoern A. Zeeb #endif /* INET */ 3912c37256eSGarrett Wollman 392fb59c426SYoshinobu Inoue #ifdef INET6 393fb59c426SYoshinobu Inoue static int 394d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 395fb59c426SYoshinobu Inoue { 396fb59c426SYoshinobu Inoue int error = 0; 397f76fcf6dSJeffrey Hsu struct inpcb *inp; 398623dce13SRobert Watson struct tcpcb *tp = NULL; 3994a91aa8fSMichael Tuexen u_char vflagsav; 400fb59c426SYoshinobu Inoue 401623dce13SRobert Watson inp = sotoinpcb(so); 402623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL")); 4038501a69cSRobert Watson INP_WLOCK(inp); 40453af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 405623dce13SRobert Watson error = EINVAL; 406623dce13SRobert Watson goto out; 407623dce13SRobert Watson } 4084a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 409623dce13SRobert Watson tp = intotcpcb(inp); 4100daccb9cSRobert Watson SOCK_LOCK(so); 4110daccb9cSRobert Watson error = solisten_proto_check(so); 412bd4a39ccSMark Johnston if (error != 0) { 413bd4a39ccSMark Johnston SOCK_UNLOCK(so); 414bd4a39ccSMark Johnston goto out; 415bd4a39ccSMark Johnston } 416fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 417bd4a39ccSMark Johnston if (inp->inp_lport == 0) { 418fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 41966ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 420fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 421bd4a39ccSMark Johnston error = in6_pcbbind(inp, NULL, td->td_ucred); 422fb59c426SYoshinobu Inoue } 423fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 4240daccb9cSRobert Watson if (error == 0) { 42557f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 426d374e81eSRobert Watson solisten_proto(so, backlog); 42709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 42837cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 42909fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 43009fe6320SNavdeep Parhar #endif 431bd4a39ccSMark Johnston } else { 432bd4a39ccSMark Johnston solisten_proto_abort(so); 4330daccb9cSRobert Watson } 4340daccb9cSRobert Watson SOCK_UNLOCK(so); 435623dce13SRobert Watson 43668bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) 437281a0fd4SPatrick Kelsey tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 43818a75309SPatrick Kelsey 4394a91aa8fSMichael Tuexen if (error != 0) 4404a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 4414a91aa8fSMichael Tuexen 442623dce13SRobert Watson out: 4435d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 4448501a69cSRobert Watson INP_WUNLOCK(inp); 445623dce13SRobert Watson return (error); 446fb59c426SYoshinobu Inoue } 447fb59c426SYoshinobu Inoue #endif /* INET6 */ 448fb59c426SYoshinobu Inoue 449b287c6c7SBjoern A. Zeeb #ifdef INET 4502c37256eSGarrett Wollman /* 4512c37256eSGarrett Wollman * Initiate connection to peer. 4522c37256eSGarrett Wollman * Create a template for use in transmissions on this connection. 4532c37256eSGarrett Wollman * Enter SYN_SENT state, and mark socket as connecting. 4542c37256eSGarrett Wollman * Start keep-alive timer, and seed output sequence space. 4552c37256eSGarrett Wollman * Send initial segment on connection. 4562c37256eSGarrett Wollman */ 4572c37256eSGarrett Wollman static int 458b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 4592c37256eSGarrett Wollman { 460109eb549SGleb Smirnoff struct epoch_tracker et; 4612c37256eSGarrett Wollman int error = 0; 462f76fcf6dSJeffrey Hsu struct inpcb *inp; 463623dce13SRobert Watson struct tcpcb *tp = NULL; 4642c37256eSGarrett Wollman struct sockaddr_in *sinp; 4652c37256eSGarrett Wollman 46657bf258eSGarrett Wollman sinp = (struct sockaddr_in *)nam; 467f161d294SMark Johnston if (nam->sa_family != AF_INET) 468f161d294SMark Johnston return (EAFNOSUPPORT); 469e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sinp)) 470e29ef13fSDon Lewis return (EINVAL); 471f161d294SMark Johnston 47252710de1SPawel Jakub Dawidek /* 47352710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 47452710de1SPawel Jakub Dawidek */ 475f161d294SMark Johnston if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 47652710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 477f161d294SMark Johnston if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) 478f903a308SMichael Tuexen return (EACCES); 479b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0) 480b89e82ddSJamie Gritton return (error); 48175c13541SPoul-Henning Kamp 482623dce13SRobert Watson inp = sotoinpcb(so); 483623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 4848501a69cSRobert Watson INP_WLOCK(inp); 485eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 486eb96dc33SJulien Charbon error = ECONNREFUSED; 487623dce13SRobert Watson goto out; 488623dce13SRobert Watson } 489bd4a39ccSMark Johnston if (SOLISTENING(so)) { 490bd4a39ccSMark Johnston error = EOPNOTSUPP; 491bd4a39ccSMark Johnston goto out; 492bd4a39ccSMark Johnston } 493623dce13SRobert Watson tp = intotcpcb(inp); 494c1604fe4SGleb Smirnoff NET_EPOCH_ENTER(et); 495b40ce416SJulian Elischer if ((error = tcp_connect(tp, nam, td)) != 0) 496c1604fe4SGleb Smirnoff goto out_in_epoch; 49709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 49809fe6320SNavdeep Parhar if (registered_toedevs > 0 && 49937cc0ecbSNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 50009fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 501c1604fe4SGleb Smirnoff goto out_in_epoch; 50209fe6320SNavdeep Parhar #endif 50309fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 50440fa3e40SGleb Smirnoff error = tcp_output(tp); 5051d41a494SGleb Smirnoff KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()" 5061d41a494SGleb Smirnoff ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error)); 507c1604fe4SGleb Smirnoff out_in_epoch: 508109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 509623dce13SRobert Watson out: 510e79cb051SGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CONNECT); 5118501a69cSRobert Watson INP_WUNLOCK(inp); 512623dce13SRobert Watson return (error); 5132c37256eSGarrett Wollman } 514b287c6c7SBjoern A. Zeeb #endif /* INET */ 5152c37256eSGarrett Wollman 516fb59c426SYoshinobu Inoue #ifdef INET6 517fb59c426SYoshinobu Inoue static int 518b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 519fb59c426SYoshinobu Inoue { 520109eb549SGleb Smirnoff struct epoch_tracker et; 521fb59c426SYoshinobu Inoue int error = 0; 522f76fcf6dSJeffrey Hsu struct inpcb *inp; 523623dce13SRobert Watson struct tcpcb *tp = NULL; 5240ecd976eSBjoern A. Zeeb struct sockaddr_in6 *sin6; 5254a91aa8fSMichael Tuexen u_int8_t incflagsav; 5264a91aa8fSMichael Tuexen u_char vflagsav; 527623dce13SRobert Watson 5280ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 529f161d294SMark Johnston if (nam->sa_family != AF_INET6) 530f161d294SMark Johnston return (EAFNOSUPPORT); 5310ecd976eSBjoern A. Zeeb if (nam->sa_len != sizeof (*sin6)) 532e29ef13fSDon Lewis return (EINVAL); 533f161d294SMark Johnston 53452710de1SPawel Jakub Dawidek /* 53552710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 53652710de1SPawel Jakub Dawidek */ 537f161d294SMark Johnston if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 53852710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 539fb59c426SYoshinobu Inoue 540623dce13SRobert Watson inp = sotoinpcb(so); 541623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 5428501a69cSRobert Watson INP_WLOCK(inp); 5434a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 5444a91aa8fSMichael Tuexen incflagsav = inp->inp_inc.inc_flags; 545eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 546eb96dc33SJulien Charbon error = ECONNREFUSED; 547623dce13SRobert Watson goto out; 548623dce13SRobert Watson } 549bd4a39ccSMark Johnston if (SOLISTENING(so)) { 550bd4a39ccSMark Johnston error = EINVAL; 551bd4a39ccSMark Johnston goto out; 552bd4a39ccSMark Johnston } 553623dce13SRobert Watson tp = intotcpcb(inp); 554b287c6c7SBjoern A. Zeeb #ifdef INET 555fa046d87SRobert Watson /* 556fa046d87SRobert Watson * XXXRW: Some confusion: V4/V6 flags relate to binding, and 557fa046d87SRobert Watson * therefore probably require the hash lock, which isn't held here. 558fa046d87SRobert Watson * Is this a significant problem? 559fa046d87SRobert Watson */ 5600ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 561fb59c426SYoshinobu Inoue struct sockaddr_in sin; 562fb59c426SYoshinobu Inoue 563d46a5312SMaxim Konovalov if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 564d46a5312SMaxim Konovalov error = EINVAL; 565d46a5312SMaxim Konovalov goto out; 566d46a5312SMaxim Konovalov } 5675dba6adaSMichael Tuexen if ((inp->inp_vflag & INP_IPV4) == 0) { 5685dba6adaSMichael Tuexen error = EAFNOSUPPORT; 5695dba6adaSMichael Tuexen goto out; 5705dba6adaSMichael Tuexen } 57133841545SHajimu UMEMOTO 5720ecd976eSBjoern A. Zeeb in6_sin6_2_sin(&sin, sin6); 573888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 574888973f5SMichael Tuexen error = EAFNOSUPPORT; 575888973f5SMichael Tuexen goto out; 576888973f5SMichael Tuexen } 577f903a308SMichael Tuexen if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) { 578f903a308SMichael Tuexen error = EACCES; 5792cf21ae5SRandall Stewart goto out; 5802cf21ae5SRandall Stewart } 581b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, 582b89e82ddSJamie Gritton &sin.sin_addr)) != 0) 583413628a7SBjoern A. Zeeb goto out; 5844a91aa8fSMichael Tuexen inp->inp_vflag |= INP_IPV4; 5854a91aa8fSMichael Tuexen inp->inp_vflag &= ~INP_IPV6; 586c1604fe4SGleb Smirnoff NET_EPOCH_ENTER(et); 587b40ce416SJulian Elischer if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 588c1604fe4SGleb Smirnoff goto out_in_epoch; 58909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 59009fe6320SNavdeep Parhar if (registered_toedevs > 0 && 591adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 59209fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 593c1604fe4SGleb Smirnoff goto out_in_epoch; 59409fe6320SNavdeep Parhar #endif 59540fa3e40SGleb Smirnoff error = tcp_output(tp); 596c1604fe4SGleb Smirnoff goto out_in_epoch; 5975dba6adaSMichael Tuexen } else { 5985dba6adaSMichael Tuexen if ((inp->inp_vflag & INP_IPV6) == 0) { 5995dba6adaSMichael Tuexen error = EAFNOSUPPORT; 6005dba6adaSMichael Tuexen goto out; 6015dba6adaSMichael Tuexen } 602fb59c426SYoshinobu Inoue } 603b287c6c7SBjoern A. Zeeb #endif 6044a91aa8fSMichael Tuexen if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0) 6054a91aa8fSMichael Tuexen goto out; 606fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 607fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 608dcdb4371SBjoern A. Zeeb inp->inp_inc.inc_flags |= INC_ISIPV6; 6090773b44eSGleb Smirnoff NET_EPOCH_ENTER(et); 610b40ce416SJulian Elischer if ((error = tcp6_connect(tp, nam, td)) != 0) 6110773b44eSGleb Smirnoff goto out_in_epoch; 61209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 61309fe6320SNavdeep Parhar if (registered_toedevs > 0 && 614adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 61509fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 6160773b44eSGleb Smirnoff goto out_in_epoch; 61709fe6320SNavdeep Parhar #endif 61809fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 61940fa3e40SGleb Smirnoff error = tcp_output(tp); 620c1604fe4SGleb Smirnoff out_in_epoch: 621109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 622623dce13SRobert Watson out: 6231d41a494SGleb Smirnoff KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()" 6241d41a494SGleb Smirnoff ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error)); 6254a91aa8fSMichael Tuexen /* 6264a91aa8fSMichael Tuexen * If the implicit bind in the connect call fails, restore 6274a91aa8fSMichael Tuexen * the flags we modified. 6284a91aa8fSMichael Tuexen */ 6294a91aa8fSMichael Tuexen if (error != 0 && inp->inp_lport == 0) { 6304a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 6314a91aa8fSMichael Tuexen inp->inp_inc.inc_flags = incflagsav; 6324a91aa8fSMichael Tuexen } 6334a91aa8fSMichael Tuexen 6345d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CONNECT); 6358501a69cSRobert Watson INP_WUNLOCK(inp); 636623dce13SRobert Watson return (error); 637fb59c426SYoshinobu Inoue } 638fb59c426SYoshinobu Inoue #endif /* INET6 */ 639fb59c426SYoshinobu Inoue 6402c37256eSGarrett Wollman /* 6412c37256eSGarrett Wollman * Initiate disconnect from peer. 6422c37256eSGarrett Wollman * If connection never passed embryonic stage, just drop; 6432c37256eSGarrett Wollman * else if don't need to let data drain, then can just drop anyways, 6442c37256eSGarrett Wollman * else have to begin TCP shutdown process: mark socket disconnecting, 6452c37256eSGarrett Wollman * drain unread data, state switch to reflect user close, and 6462c37256eSGarrett Wollman * send segment (e.g. FIN) to peer. Socket will be really disconnected 6472c37256eSGarrett Wollman * when peer sends FIN and acks ours. 6482c37256eSGarrett Wollman * 6492c37256eSGarrett Wollman * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 6502c37256eSGarrett Wollman */ 6512c37256eSGarrett Wollman static int 6522c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so) 6532c37256eSGarrett Wollman { 654f76fcf6dSJeffrey Hsu struct inpcb *inp; 655623dce13SRobert Watson struct tcpcb *tp = NULL; 6566573d758SMatt Macy struct epoch_tracker et; 657623dce13SRobert Watson int error = 0; 6582c37256eSGarrett Wollman 65997a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 660623dce13SRobert Watson inp = sotoinpcb(so); 661623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 6628501a69cSRobert Watson INP_WLOCK(inp); 663489dcc92SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 66421367f63SSam Leffler error = ECONNRESET; 665623dce13SRobert Watson goto out; 666623dce13SRobert Watson } 667623dce13SRobert Watson tp = intotcpcb(inp); 668623dce13SRobert Watson tcp_disconnect(tp); 669623dce13SRobert Watson out: 6705d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_DISCONNECT); 6718501a69cSRobert Watson INP_WUNLOCK(inp); 67297a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 673623dce13SRobert Watson return (error); 6742c37256eSGarrett Wollman } 6752c37256eSGarrett Wollman 676b287c6c7SBjoern A. Zeeb #ifdef INET 6772c37256eSGarrett Wollman /* 6788296cddfSRobert Watson * Accept a connection. Essentially all the work is done at higher levels; 6798296cddfSRobert Watson * just return the address of the peer, storing through addr. 6802c37256eSGarrett Wollman */ 6812c37256eSGarrett Wollman static int 68257bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam) 6832c37256eSGarrett Wollman { 6842c37256eSGarrett Wollman int error = 0; 685f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 686b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS 6871db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 688b338b1fdSMateusz Guzik #endif 68926ef6ac4SDon Lewis struct in_addr addr; 69026ef6ac4SDon Lewis in_port_t port = 0; 6912c37256eSGarrett Wollman 6923d2d3ef4SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 6933d2d3ef4SRobert Watson return (ECONNABORTED); 694f76fcf6dSJeffrey Hsu 695f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 696623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 6978501a69cSRobert Watson INP_WLOCK(inp); 69853af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 6993d2d3ef4SRobert Watson error = ECONNABORTED; 700623dce13SRobert Watson goto out; 701623dce13SRobert Watson } 702b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS 7031db24ffbSJonathan Lemon tp = intotcpcb(inp); 704b338b1fdSMateusz Guzik #endif 705f76fcf6dSJeffrey Hsu 706f76fcf6dSJeffrey Hsu /* 70754d642bbSRobert Watson * We inline in_getpeeraddr and COMMON_END here, so that we can 70826ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 70926ef6ac4SDon Lewis * release the lock. 710f76fcf6dSJeffrey Hsu */ 71126ef6ac4SDon Lewis port = inp->inp_fport; 71226ef6ac4SDon Lewis addr = inp->inp_faddr; 713f76fcf6dSJeffrey Hsu 714623dce13SRobert Watson out: 7155d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 7168501a69cSRobert Watson INP_WUNLOCK(inp); 71726ef6ac4SDon Lewis if (error == 0) 71826ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 71926ef6ac4SDon Lewis return error; 7202c37256eSGarrett Wollman } 721b287c6c7SBjoern A. Zeeb #endif /* INET */ 7222c37256eSGarrett Wollman 723fb59c426SYoshinobu Inoue #ifdef INET6 724fb59c426SYoshinobu Inoue static int 725fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 726fb59c426SYoshinobu Inoue { 727f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 728fb59c426SYoshinobu Inoue int error = 0; 729b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS 7301db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 731b338b1fdSMateusz Guzik #endif 73226ef6ac4SDon Lewis struct in_addr addr; 73326ef6ac4SDon Lewis struct in6_addr addr6; 7346573d758SMatt Macy struct epoch_tracker et; 73526ef6ac4SDon Lewis in_port_t port = 0; 73626ef6ac4SDon Lewis int v4 = 0; 737fb59c426SYoshinobu Inoue 738b4470c16SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 739b4470c16SRobert Watson return (ECONNABORTED); 740f76fcf6dSJeffrey Hsu 741f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 742623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 74397a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 7448501a69cSRobert Watson INP_WLOCK(inp); 74553af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 74621367f63SSam Leffler error = ECONNABORTED; 747623dce13SRobert Watson goto out; 748623dce13SRobert Watson } 749b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS 7501db24ffbSJonathan Lemon tp = intotcpcb(inp); 751b338b1fdSMateusz Guzik #endif 752623dce13SRobert Watson 75326ef6ac4SDon Lewis /* 75426ef6ac4SDon Lewis * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 75526ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 75626ef6ac4SDon Lewis * release the lock. 75726ef6ac4SDon Lewis */ 75826ef6ac4SDon Lewis if (inp->inp_vflag & INP_IPV4) { 75926ef6ac4SDon Lewis v4 = 1; 76026ef6ac4SDon Lewis port = inp->inp_fport; 76126ef6ac4SDon Lewis addr = inp->inp_faddr; 76226ef6ac4SDon Lewis } else { 76326ef6ac4SDon Lewis port = inp->inp_fport; 76426ef6ac4SDon Lewis addr6 = inp->in6p_faddr; 76526ef6ac4SDon Lewis } 76626ef6ac4SDon Lewis 767623dce13SRobert Watson out: 7685d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 7698501a69cSRobert Watson INP_WUNLOCK(inp); 77097a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 77126ef6ac4SDon Lewis if (error == 0) { 77226ef6ac4SDon Lewis if (v4) 77326ef6ac4SDon Lewis *nam = in6_v4mapsin6_sockaddr(port, &addr); 77426ef6ac4SDon Lewis else 77526ef6ac4SDon Lewis *nam = in6_sockaddr(port, &addr6); 77626ef6ac4SDon Lewis } 77726ef6ac4SDon Lewis return error; 778fb59c426SYoshinobu Inoue } 779fb59c426SYoshinobu Inoue #endif /* INET6 */ 780f76fcf6dSJeffrey Hsu 781f76fcf6dSJeffrey Hsu /* 7822c37256eSGarrett Wollman * Mark the connection as being incapable of further output. 7832c37256eSGarrett Wollman */ 7842c37256eSGarrett Wollman static int 7852c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so) 7862c37256eSGarrett Wollman { 7872c37256eSGarrett Wollman int error = 0; 788f76fcf6dSJeffrey Hsu struct inpcb *inp; 789623dce13SRobert Watson struct tcpcb *tp = NULL; 7906573d758SMatt Macy struct epoch_tracker et; 7912c37256eSGarrett Wollman 792623dce13SRobert Watson inp = sotoinpcb(so); 793623dce13SRobert Watson KASSERT(inp != NULL, ("inp == NULL")); 7948501a69cSRobert Watson INP_WLOCK(inp); 79553af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 7960af4ce45SGleb Smirnoff INP_WUNLOCK(inp); 7970af4ce45SGleb Smirnoff return (ECONNRESET); 798623dce13SRobert Watson } 7990af4ce45SGleb Smirnoff tp = intotcpcb(inp); 8000af4ce45SGleb Smirnoff NET_EPOCH_ENTER(et); 8012c37256eSGarrett Wollman socantsendmore(so); 802623dce13SRobert Watson tcp_usrclosed(tp); 803ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 804f64dc2abSGleb Smirnoff error = tcp_output_nodrop(tp); 8055d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN); 806f64dc2abSGleb Smirnoff error = tcp_unlock_or_drop(tp, error); 80797a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 808623dce13SRobert Watson 809623dce13SRobert Watson return (error); 8102c37256eSGarrett Wollman } 8112c37256eSGarrett Wollman 8122c37256eSGarrett Wollman /* 8132c37256eSGarrett Wollman * After a receive, possibly send window update to peer. 8142c37256eSGarrett Wollman */ 8152c37256eSGarrett Wollman static int 8162c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags) 8172c37256eSGarrett Wollman { 818109eb549SGleb Smirnoff struct epoch_tracker et; 819f76fcf6dSJeffrey Hsu struct inpcb *inp; 820623dce13SRobert Watson struct tcpcb *tp = NULL; 821f64dc2abSGleb Smirnoff int outrv = 0, error = 0; 8222c37256eSGarrett Wollman 823623dce13SRobert Watson inp = sotoinpcb(so); 824623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 8258501a69cSRobert Watson INP_WLOCK(inp); 82653af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 82737a7f557SGleb Smirnoff INP_WUNLOCK(inp); 82837a7f557SGleb Smirnoff return (ECONNRESET); 829623dce13SRobert Watson } 83037a7f557SGleb Smirnoff tp = intotcpcb(inp); 83137a7f557SGleb Smirnoff NET_EPOCH_ENTER(et); 832281a0fd4SPatrick Kelsey /* 833281a0fd4SPatrick Kelsey * For passively-created TFO connections, don't attempt a window 834281a0fd4SPatrick Kelsey * update while still in SYN_RECEIVED as this may trigger an early 835281a0fd4SPatrick Kelsey * SYN|ACK. It is preferable to have the SYN|ACK be sent along with 836281a0fd4SPatrick Kelsey * application response data, or failing that, when the DELACK timer 837281a0fd4SPatrick Kelsey * expires. 838281a0fd4SPatrick Kelsey */ 83968bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 840281a0fd4SPatrick Kelsey (tp->t_state == TCPS_SYN_RECEIVED)) 841281a0fd4SPatrick Kelsey goto out; 84209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 84309fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) 84409fe6320SNavdeep Parhar tcp_offload_rcvd(tp); 845460cf046SNavdeep Parhar else 84609fe6320SNavdeep Parhar #endif 847f64dc2abSGleb Smirnoff outrv = tcp_output_nodrop(tp); 848623dce13SRobert Watson out: 8495d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVD); 850f64dc2abSGleb Smirnoff (void) tcp_unlock_or_drop(tp, outrv); 851f64dc2abSGleb Smirnoff NET_EPOCH_EXIT(et); 852623dce13SRobert Watson return (error); 8532c37256eSGarrett Wollman } 8542c37256eSGarrett Wollman 8552c37256eSGarrett Wollman /* 8562c37256eSGarrett Wollman * Do a send by putting data in output queue and updating urgent 8579c9906e9SPeter Wemm * marker if URG set. Possibly send more data. Unlike the other 8589c9906e9SPeter Wemm * pru_*() routines, the mbuf chains are our responsibility. We 8599c9906e9SPeter Wemm * must either enqueue them or free them. The other pru_* routines 8609c9906e9SPeter Wemm * generally are caller-frees. 8612c37256eSGarrett Wollman */ 8622c37256eSGarrett Wollman static int 86357bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 864b40ce416SJulian Elischer struct sockaddr *nam, struct mbuf *control, struct thread *td) 8652c37256eSGarrett Wollman { 86697a95ee1SGleb Smirnoff struct epoch_tracker et; 8672c37256eSGarrett Wollman int error = 0; 868f76fcf6dSJeffrey Hsu struct inpcb *inp; 869623dce13SRobert Watson struct tcpcb *tp = NULL; 870888973f5SMichael Tuexen #ifdef INET 87151e08d53SMichael Tuexen #ifdef INET6 87251e08d53SMichael Tuexen struct sockaddr_in sin; 87351e08d53SMichael Tuexen #endif 87451e08d53SMichael Tuexen struct sockaddr_in *sinp; 875888973f5SMichael Tuexen #endif 876fb59c426SYoshinobu Inoue #ifdef INET6 877fb59c426SYoshinobu Inoue int isipv6; 878fb59c426SYoshinobu Inoue #endif 8794a91aa8fSMichael Tuexen u_int8_t incflagsav; 8804a91aa8fSMichael Tuexen u_char vflagsav; 8814a91aa8fSMichael Tuexen bool restoreflags; 8822c37256eSGarrett Wollman 883d8acd268SMark Johnston if (control != NULL) { 884d8acd268SMark Johnston /* TCP doesn't do control messages (rights, creds, etc) */ 885d8acd268SMark Johnston if (control->m_len) { 886d8acd268SMark Johnston m_freem(control); 8874287aa56SGleb Smirnoff return (EINVAL); 888d8acd268SMark Johnston } 889d8acd268SMark Johnston m_freem(control); /* empty control, just free it */ 890d8acd268SMark Johnston } 8914287aa56SGleb Smirnoff 8924287aa56SGleb Smirnoff inp = sotoinpcb(so); 8934287aa56SGleb Smirnoff KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 8944287aa56SGleb Smirnoff INP_WLOCK(inp); 89553af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 8964287aa56SGleb Smirnoff if (m != NULL && (flags & PRUS_NOTREADY) == 0) 8974287aa56SGleb Smirnoff m_freem(m); 8984287aa56SGleb Smirnoff INP_WUNLOCK(inp); 8994287aa56SGleb Smirnoff return (ECONNRESET); 9004287aa56SGleb Smirnoff } 9014287aa56SGleb Smirnoff 9024287aa56SGleb Smirnoff vflagsav = inp->inp_vflag; 9034287aa56SGleb Smirnoff incflagsav = inp->inp_inc.inc_flags; 9044287aa56SGleb Smirnoff restoreflags = false; 9054287aa56SGleb Smirnoff tp = intotcpcb(inp); 9064287aa56SGleb Smirnoff 9074287aa56SGleb Smirnoff NET_EPOCH_ENTER(et); 9087d2608a5SMark Johnston if ((flags & PRUS_OOB) != 0 && 9097d2608a5SMark Johnston (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0) 910d3b6c96bSRandall Stewart goto out; 9117d2608a5SMark Johnston 912888973f5SMichael Tuexen if (nam != NULL && tp->t_state < TCPS_SYN_SENT) { 913bd4a39ccSMark Johnston if (tp->t_state == TCPS_LISTEN) { 914bd4a39ccSMark Johnston error = EINVAL; 915bd4a39ccSMark Johnston goto out; 916bd4a39ccSMark Johnston } 917888973f5SMichael Tuexen switch (nam->sa_family) { 918888973f5SMichael Tuexen #ifdef INET 919888973f5SMichael Tuexen case AF_INET: 920888973f5SMichael Tuexen sinp = (struct sockaddr_in *)nam; 921888973f5SMichael Tuexen if (sinp->sin_len != sizeof(struct sockaddr_in)) { 922888973f5SMichael Tuexen error = EINVAL; 923888973f5SMichael Tuexen goto out; 924888973f5SMichael Tuexen } 925888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV6) != 0) { 926888973f5SMichael Tuexen error = EAFNOSUPPORT; 927888973f5SMichael Tuexen goto out; 928888973f5SMichael Tuexen } 929888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 930888973f5SMichael Tuexen error = EAFNOSUPPORT; 931888973f5SMichael Tuexen goto out; 932888973f5SMichael Tuexen } 933f903a308SMichael Tuexen if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) { 934f903a308SMichael Tuexen error = EACCES; 9352cf21ae5SRandall Stewart goto out; 9362cf21ae5SRandall Stewart } 937888973f5SMichael Tuexen if ((error = prison_remote_ip4(td->td_ucred, 9387d2608a5SMark Johnston &sinp->sin_addr))) 939888973f5SMichael Tuexen goto out; 940888973f5SMichael Tuexen #ifdef INET6 941888973f5SMichael Tuexen isipv6 = 0; 942888973f5SMichael Tuexen #endif 943888973f5SMichael Tuexen break; 944888973f5SMichael Tuexen #endif /* INET */ 945888973f5SMichael Tuexen #ifdef INET6 946888973f5SMichael Tuexen case AF_INET6: 947888973f5SMichael Tuexen { 9480ecd976eSBjoern A. Zeeb struct sockaddr_in6 *sin6; 949888973f5SMichael Tuexen 9500ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 9510ecd976eSBjoern A. Zeeb if (sin6->sin6_len != sizeof(*sin6)) { 952888973f5SMichael Tuexen error = EINVAL; 953888973f5SMichael Tuexen goto out; 954888973f5SMichael Tuexen } 955e240ce42SMichael Tuexen if ((inp->inp_vflag & INP_IPV6PROTO) == 0) { 956e240ce42SMichael Tuexen error = EAFNOSUPPORT; 957e240ce42SMichael Tuexen goto out; 958e240ce42SMichael Tuexen } 9590ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 960888973f5SMichael Tuexen error = EAFNOSUPPORT; 961888973f5SMichael Tuexen goto out; 962888973f5SMichael Tuexen } 9630ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 964888973f5SMichael Tuexen #ifdef INET 965888973f5SMichael Tuexen if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 966888973f5SMichael Tuexen error = EINVAL; 967888973f5SMichael Tuexen goto out; 968888973f5SMichael Tuexen } 969888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV4) == 0) { 970888973f5SMichael Tuexen error = EAFNOSUPPORT; 971888973f5SMichael Tuexen goto out; 972888973f5SMichael Tuexen } 9734a91aa8fSMichael Tuexen restoreflags = true; 974888973f5SMichael Tuexen inp->inp_vflag &= ~INP_IPV6; 975888973f5SMichael Tuexen sinp = &sin; 9760ecd976eSBjoern A. Zeeb in6_sin6_2_sin(sinp, sin6); 977888973f5SMichael Tuexen if (IN_MULTICAST( 978888973f5SMichael Tuexen ntohl(sinp->sin_addr.s_addr))) { 979888973f5SMichael Tuexen error = EAFNOSUPPORT; 980888973f5SMichael Tuexen goto out; 981888973f5SMichael Tuexen } 982888973f5SMichael Tuexen if ((error = prison_remote_ip4(td->td_ucred, 9837d2608a5SMark Johnston &sinp->sin_addr))) 984888973f5SMichael Tuexen goto out; 985888973f5SMichael Tuexen isipv6 = 0; 986888973f5SMichael Tuexen #else /* !INET */ 987888973f5SMichael Tuexen error = EAFNOSUPPORT; 988888973f5SMichael Tuexen goto out; 989888973f5SMichael Tuexen #endif /* INET */ 990888973f5SMichael Tuexen } else { 991888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV6) == 0) { 992888973f5SMichael Tuexen error = EAFNOSUPPORT; 993888973f5SMichael Tuexen goto out; 994888973f5SMichael Tuexen } 9954a91aa8fSMichael Tuexen restoreflags = true; 996888973f5SMichael Tuexen inp->inp_vflag &= ~INP_IPV4; 997888973f5SMichael Tuexen inp->inp_inc.inc_flags |= INC_ISIPV6; 998888973f5SMichael Tuexen if ((error = prison_remote_ip6(td->td_ucred, 9997d2608a5SMark Johnston &sin6->sin6_addr))) 1000888973f5SMichael Tuexen goto out; 1001888973f5SMichael Tuexen isipv6 = 1; 1002888973f5SMichael Tuexen } 1003888973f5SMichael Tuexen break; 1004888973f5SMichael Tuexen } 1005888973f5SMichael Tuexen #endif /* INET6 */ 1006888973f5SMichael Tuexen default: 1007888973f5SMichael Tuexen error = EAFNOSUPPORT; 1008888973f5SMichael Tuexen goto out; 1009888973f5SMichael Tuexen } 1010888973f5SMichael Tuexen } 10112c37256eSGarrett Wollman if (!(flags & PRUS_OOB)) { 101208af8aacSRandall Stewart if (tp->t_acktime == 0) 101308af8aacSRandall Stewart tp->t_acktime = ticks; 1014651e4e6aSGleb Smirnoff sbappendstream(&so->so_snd, m, flags); 10157d2608a5SMark Johnston m = NULL; 10162c37256eSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 1017bd4a39ccSMark Johnston KASSERT(tp->t_state == TCPS_CLOSED, 1018bd4a39ccSMark Johnston ("%s: tp %p is listening", __func__, tp)); 1019bd4a39ccSMark Johnston 10202c37256eSGarrett Wollman /* 10212c37256eSGarrett Wollman * Do implied connect if not yet connected, 10222c37256eSGarrett Wollman * initialize window to default value, and 10230c39d38dSGleb Smirnoff * initialize maxseg using peer's cached MSS. 10242c37256eSGarrett Wollman */ 1025fb59c426SYoshinobu Inoue #ifdef INET6 1026fb59c426SYoshinobu Inoue if (isipv6) 1027b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 1028fb59c426SYoshinobu Inoue #endif /* INET6 */ 1029b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1030b287c6c7SBjoern A. Zeeb else 1031b287c6c7SBjoern A. Zeeb #endif 1032b287c6c7SBjoern A. Zeeb #ifdef INET 1033888973f5SMichael Tuexen error = tcp_connect(tp, 1034888973f5SMichael Tuexen (struct sockaddr *)sinp, td); 1035b287c6c7SBjoern A. Zeeb #endif 10364a91aa8fSMichael Tuexen /* 10374a91aa8fSMichael Tuexen * The bind operation in tcp_connect succeeded. We 10384a91aa8fSMichael Tuexen * no longer want to restore the flags if later 10394a91aa8fSMichael Tuexen * operations fail. 10404a91aa8fSMichael Tuexen */ 10414a91aa8fSMichael Tuexen if (error == 0 || inp->inp_lport != 0) 10424a91aa8fSMichael Tuexen restoreflags = false; 10434a91aa8fSMichael Tuexen 10447d2608a5SMark Johnston if (error) { 10457d2608a5SMark Johnston /* m is freed if PRUS_NOTREADY is unset. */ 10467d2608a5SMark Johnston sbflush(&so->so_snd); 10472c37256eSGarrett Wollman goto out; 10487d2608a5SMark Johnston } 1049c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) 1050c560df6fSPatrick Kelsey tcp_fastopen_connect(tp); 105118a75309SPatrick Kelsey else { 10522c37256eSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 10532c37256eSGarrett Wollman tcp_mss(tp, -1); 10542c37256eSGarrett Wollman } 1055c560df6fSPatrick Kelsey } 10562c37256eSGarrett Wollman if (flags & PRUS_EOF) { 10572c37256eSGarrett Wollman /* 10582c37256eSGarrett Wollman * Close the send side of the connection after 10592c37256eSGarrett Wollman * the data is sent. 10602c37256eSGarrett Wollman */ 10612c37256eSGarrett Wollman socantsendmore(so); 1062623dce13SRobert Watson tcp_usrclosed(tp); 10632c37256eSGarrett Wollman } 1064e854dd38SRandall Stewart if (TCPS_HAVEESTABLISHED(tp->t_state) && 1065e854dd38SRandall Stewart ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 1066e854dd38SRandall Stewart (tp->t_fbyte_out == 0) && 1067e854dd38SRandall Stewart (so->so_snd.sb_ccc > 0)) { 1068e854dd38SRandall Stewart tp->t_fbyte_out = ticks; 1069e854dd38SRandall Stewart if (tp->t_fbyte_out == 0) 1070e854dd38SRandall Stewart tp->t_fbyte_out = 1; 1071e854dd38SRandall Stewart if (tp->t_fbyte_out && tp->t_fbyte_in) 1072e854dd38SRandall Stewart tp->t_flags2 |= TF2_FBYTES_COMPLETE; 1073e854dd38SRandall Stewart } 10742cbcd3c1SGleb Smirnoff if (!(inp->inp_flags & INP_DROPPED) && 10752cbcd3c1SGleb Smirnoff !(flags & PRUS_NOTREADY)) { 1076b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 1077b0acefa8SBill Fenner tp->t_flags |= TF_MORETOCOME; 1078f64dc2abSGleb Smirnoff error = tcp_output_nodrop(tp); 1079b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 1080b0acefa8SBill Fenner tp->t_flags &= ~TF_MORETOCOME; 1081b0acefa8SBill Fenner } 10822c37256eSGarrett Wollman } else { 1083623dce13SRobert Watson /* 1084623dce13SRobert Watson * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 1085623dce13SRobert Watson */ 1086d2bc35abSRobert Watson SOCKBUF_LOCK(&so->so_snd); 10872c37256eSGarrett Wollman if (sbspace(&so->so_snd) < -512) { 1088d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 10892c37256eSGarrett Wollman error = ENOBUFS; 10902c37256eSGarrett Wollman goto out; 10912c37256eSGarrett Wollman } 10922c37256eSGarrett Wollman /* 10932c37256eSGarrett Wollman * According to RFC961 (Assigned Protocols), 10942c37256eSGarrett Wollman * the urgent pointer points to the last octet 10952c37256eSGarrett Wollman * of urgent data. We continue, however, 10962c37256eSGarrett Wollman * to consider it to indicate the first octet 10972c37256eSGarrett Wollman * of data past the urgent section. 10982c37256eSGarrett Wollman * Otherwise, snd_up should be one lower. 10992c37256eSGarrett Wollman */ 110008af8aacSRandall Stewart if (tp->t_acktime == 0) 110108af8aacSRandall Stewart tp->t_acktime = ticks; 1102651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_snd, m, flags); 1103d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 11047d2608a5SMark Johnston m = NULL; 1105ef53690bSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 1106ef53690bSGarrett Wollman /* 1107ef53690bSGarrett Wollman * Do implied connect if not yet connected, 1108ef53690bSGarrett Wollman * initialize window to default value, and 11090c39d38dSGleb Smirnoff * initialize maxseg using peer's cached MSS. 1110ef53690bSGarrett Wollman */ 111118a75309SPatrick Kelsey 1112c560df6fSPatrick Kelsey /* 1113c560df6fSPatrick Kelsey * Not going to contemplate SYN|URG 1114c560df6fSPatrick Kelsey */ 1115c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) 1116c560df6fSPatrick Kelsey tp->t_flags &= ~TF_FASTOPEN; 1117fb59c426SYoshinobu Inoue #ifdef INET6 1118fb59c426SYoshinobu Inoue if (isipv6) 1119b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 1120fb59c426SYoshinobu Inoue #endif /* INET6 */ 1121b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1122b287c6c7SBjoern A. Zeeb else 1123b287c6c7SBjoern A. Zeeb #endif 1124b287c6c7SBjoern A. Zeeb #ifdef INET 1125888973f5SMichael Tuexen error = tcp_connect(tp, 1126888973f5SMichael Tuexen (struct sockaddr *)sinp, td); 1127b287c6c7SBjoern A. Zeeb #endif 11284a91aa8fSMichael Tuexen /* 11294a91aa8fSMichael Tuexen * The bind operation in tcp_connect succeeded. We 11304a91aa8fSMichael Tuexen * no longer want to restore the flags if later 11314a91aa8fSMichael Tuexen * operations fail. 11324a91aa8fSMichael Tuexen */ 11334a91aa8fSMichael Tuexen if (error == 0 || inp->inp_lport != 0) 11344a91aa8fSMichael Tuexen restoreflags = false; 11354a91aa8fSMichael Tuexen 11367d2608a5SMark Johnston if (error != 0) { 11377d2608a5SMark Johnston /* m is freed if PRUS_NOTREADY is unset. */ 11387d2608a5SMark Johnston sbflush(&so->so_snd); 1139ef53690bSGarrett Wollman goto out; 11407d2608a5SMark Johnston } 1141ef53690bSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 1142ef53690bSGarrett Wollman tcp_mss(tp, -1); 1143623dce13SRobert Watson } 1144300fa232SGleb Smirnoff tp->snd_up = tp->snd_una + sbavail(&so->so_snd); 11457d2608a5SMark Johnston if ((flags & PRUS_NOTREADY) == 0) { 11462cdbfa66SPaul Saab tp->t_flags |= TF_FORCEDATA; 1147f64dc2abSGleb Smirnoff error = tcp_output_nodrop(tp); 11482cdbfa66SPaul Saab tp->t_flags &= ~TF_FORCEDATA; 11492c37256eSGarrett Wollman } 11502cbcd3c1SGleb Smirnoff } 11512529f56eSJonathan T. Looney TCP_LOG_EVENT(tp, NULL, 11522529f56eSJonathan T. Looney &inp->inp_socket->so_rcv, 11532529f56eSJonathan T. Looney &inp->inp_socket->so_snd, 11542529f56eSJonathan T. Looney TCP_LOG_USERSEND, error, 11552529f56eSJonathan T. Looney 0, NULL, false); 11567d2608a5SMark Johnston 1157d1401c90SRobert Watson out: 11584a91aa8fSMichael Tuexen /* 11597d2608a5SMark Johnston * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is 11607d2608a5SMark Johnston * responsible for freeing memory. 11617d2608a5SMark Johnston */ 11627d2608a5SMark Johnston if (m != NULL && (flags & PRUS_NOTREADY) == 0) 11637d2608a5SMark Johnston m_freem(m); 11647d2608a5SMark Johnston 11657d2608a5SMark Johnston /* 11664a91aa8fSMichael Tuexen * If the request was unsuccessful and we changed flags, 11674a91aa8fSMichael Tuexen * restore the original flags. 11684a91aa8fSMichael Tuexen */ 11694a91aa8fSMichael Tuexen if (error != 0 && restoreflags) { 11704a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 11714a91aa8fSMichael Tuexen inp->inp_inc.inc_flags = incflagsav; 11724a91aa8fSMichael Tuexen } 11735d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB : 11745d06879aSGeorge V. Neville-Neil ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 1175f64dc2abSGleb Smirnoff error = tcp_unlock_or_drop(tp, error); 117697a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 117773fddedaSPeter Grehan return (error); 11782c37256eSGarrett Wollman } 11792c37256eSGarrett Wollman 11802cbcd3c1SGleb Smirnoff static int 11812cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count) 11822cbcd3c1SGleb Smirnoff { 1183109eb549SGleb Smirnoff struct epoch_tracker et; 11842cbcd3c1SGleb Smirnoff struct inpcb *inp; 11852cbcd3c1SGleb Smirnoff struct tcpcb *tp; 11862cbcd3c1SGleb Smirnoff int error; 11872cbcd3c1SGleb Smirnoff 11882cbcd3c1SGleb Smirnoff inp = sotoinpcb(so); 11892cbcd3c1SGleb Smirnoff INP_WLOCK(inp); 119053af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 11912cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 119282334850SJohn Baldwin mb_free_notready(m, count); 11932cbcd3c1SGleb Smirnoff return (ECONNRESET); 11942cbcd3c1SGleb Smirnoff } 11952cbcd3c1SGleb Smirnoff tp = intotcpcb(inp); 11962cbcd3c1SGleb Smirnoff 11972cbcd3c1SGleb Smirnoff SOCKBUF_LOCK(&so->so_snd); 11982cbcd3c1SGleb Smirnoff error = sbready(&so->so_snd, m, count); 11992cbcd3c1SGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 1200f64dc2abSGleb Smirnoff if (error) { 12012cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 1202f64dc2abSGleb Smirnoff return (error); 1203f64dc2abSGleb Smirnoff } 1204f64dc2abSGleb Smirnoff NET_EPOCH_ENTER(et); 1205f64dc2abSGleb Smirnoff error = tcp_output_unlock(tp); 1206f64dc2abSGleb Smirnoff NET_EPOCH_EXIT(et); 12072cbcd3c1SGleb Smirnoff 12082cbcd3c1SGleb Smirnoff return (error); 12092cbcd3c1SGleb Smirnoff } 12102cbcd3c1SGleb Smirnoff 12112c37256eSGarrett Wollman /* 1212a152f8a3SRobert Watson * Abort the TCP. Drop the connection abruptly. 12132c37256eSGarrett Wollman */ 1214ac45e92fSRobert Watson static void 12152c37256eSGarrett Wollman tcp_usr_abort(struct socket *so) 12162c37256eSGarrett Wollman { 1217f76fcf6dSJeffrey Hsu struct inpcb *inp; 1218a152f8a3SRobert Watson struct tcpcb *tp = NULL; 12196573d758SMatt Macy struct epoch_tracker et; 1220c78cbc7bSRobert Watson 1221ac45e92fSRobert Watson inp = sotoinpcb(so); 1222c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 1223c78cbc7bSRobert Watson 122497a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 12258501a69cSRobert Watson INP_WLOCK(inp); 1226c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 1227c78cbc7bSRobert Watson ("tcp_usr_abort: inp_socket == NULL")); 1228c78cbc7bSRobert Watson 1229c78cbc7bSRobert Watson /* 1230a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, drop. 1231c78cbc7bSRobert Watson */ 123253af6903SGleb Smirnoff if (!(inp->inp_flags & INP_DROPPED)) { 1233c78cbc7bSRobert Watson tp = intotcpcb(inp); 12348fa799bdSJonathan T. Looney tp = tcp_drop(tp, ECONNABORTED); 12358fa799bdSJonathan T. Looney if (tp == NULL) 12368fa799bdSJonathan T. Looney goto dropped; 12375d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ABORT); 1238c78cbc7bSRobert Watson } 1239ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1240d8596171SGleb Smirnoff soref(so); 1241ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1242a152f8a3SRobert Watson } 12438501a69cSRobert Watson INP_WUNLOCK(inp); 12448fa799bdSJonathan T. Looney dropped: 124597a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 1246a152f8a3SRobert Watson } 1247a152f8a3SRobert Watson 1248a152f8a3SRobert Watson /* 1249a152f8a3SRobert Watson * TCP socket is closed. Start friendly disconnect. 1250a152f8a3SRobert Watson */ 1251a152f8a3SRobert Watson static void 1252a152f8a3SRobert Watson tcp_usr_close(struct socket *so) 1253a152f8a3SRobert Watson { 1254a152f8a3SRobert Watson struct inpcb *inp; 1255a152f8a3SRobert Watson struct tcpcb *tp = NULL; 12566573d758SMatt Macy struct epoch_tracker et; 1257a152f8a3SRobert Watson 1258a152f8a3SRobert Watson inp = sotoinpcb(so); 1259a152f8a3SRobert Watson KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL")); 1260a152f8a3SRobert Watson 126197a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 12628501a69cSRobert Watson INP_WLOCK(inp); 1263a152f8a3SRobert Watson KASSERT(inp->inp_socket != NULL, 1264a152f8a3SRobert Watson ("tcp_usr_close: inp_socket == NULL")); 1265a152f8a3SRobert Watson 1266a152f8a3SRobert Watson /* 1267a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, initiate 1268a152f8a3SRobert Watson * a disconnect. 1269a152f8a3SRobert Watson */ 127053af6903SGleb Smirnoff if (!(inp->inp_flags & INP_DROPPED)) { 1271a152f8a3SRobert Watson tp = intotcpcb(inp); 127274703901SGleb Smirnoff tp->t_flags |= TF_CLOSED; 1273a152f8a3SRobert Watson tcp_disconnect(tp); 12745d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CLOSE); 1275a152f8a3SRobert Watson } 1276ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1277d8596171SGleb Smirnoff soref(so); 1278ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1279a152f8a3SRobert Watson } 12808501a69cSRobert Watson INP_WUNLOCK(inp); 128197a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 12822c37256eSGarrett Wollman } 12832c37256eSGarrett Wollman 1284d3b6c96bSRandall Stewart static int 1285d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags) 1286d3b6c96bSRandall Stewart { 1287d3b6c96bSRandall Stewart /* 1288d3b6c96bSRandall Stewart * If the specific TCP stack has a pru_options 1289d3b6c96bSRandall Stewart * specified then it does not always support 1290d3b6c96bSRandall Stewart * all the PRU_XX options and we must ask it. 1291d3b6c96bSRandall Stewart * If the function is not specified then all 1292d3b6c96bSRandall Stewart * of the PRU_XX options are supported. 1293d3b6c96bSRandall Stewart */ 1294d3b6c96bSRandall Stewart int ret = 0; 1295d3b6c96bSRandall Stewart 1296d3b6c96bSRandall Stewart if (tp->t_fb->tfb_pru_options) { 1297d3b6c96bSRandall Stewart ret = (*tp->t_fb->tfb_pru_options)(tp, flags); 1298d3b6c96bSRandall Stewart } 1299d3b6c96bSRandall Stewart return (ret); 1300d3b6c96bSRandall Stewart } 1301d3b6c96bSRandall Stewart 13022c37256eSGarrett Wollman /* 13032c37256eSGarrett Wollman * Receive out-of-band data. 13042c37256eSGarrett Wollman */ 13052c37256eSGarrett Wollman static int 13062c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 13072c37256eSGarrett Wollman { 13082c37256eSGarrett Wollman int error = 0; 1309f76fcf6dSJeffrey Hsu struct inpcb *inp; 1310623dce13SRobert Watson struct tcpcb *tp = NULL; 13112c37256eSGarrett Wollman 1312623dce13SRobert Watson inp = sotoinpcb(so); 1313623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 13148501a69cSRobert Watson INP_WLOCK(inp); 131553af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 131621367f63SSam Leffler error = ECONNRESET; 1317623dce13SRobert Watson goto out; 1318623dce13SRobert Watson } 1319623dce13SRobert Watson tp = intotcpcb(inp); 1320d3b6c96bSRandall Stewart error = tcp_pru_options_support(tp, PRUS_OOB); 1321d3b6c96bSRandall Stewart if (error) { 1322d3b6c96bSRandall Stewart goto out; 1323d3b6c96bSRandall Stewart } 13242c37256eSGarrett Wollman if ((so->so_oobmark == 0 && 1325c0b99ffaSRobert Watson (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 13264cc20ab1SSeigo Tanimura so->so_options & SO_OOBINLINE || 13274cc20ab1SSeigo Tanimura tp->t_oobflags & TCPOOB_HADDATA) { 13282c37256eSGarrett Wollman error = EINVAL; 13292c37256eSGarrett Wollman goto out; 13302c37256eSGarrett Wollman } 13312c37256eSGarrett Wollman if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 13322c37256eSGarrett Wollman error = EWOULDBLOCK; 13332c37256eSGarrett Wollman goto out; 13342c37256eSGarrett Wollman } 13352c37256eSGarrett Wollman m->m_len = 1; 13362c37256eSGarrett Wollman *mtod(m, caddr_t) = tp->t_iobc; 13372c37256eSGarrett Wollman if ((flags & MSG_PEEK) == 0) 13382c37256eSGarrett Wollman tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1339623dce13SRobert Watson 1340623dce13SRobert Watson out: 13415d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVOOB); 13428501a69cSRobert Watson INP_WUNLOCK(inp); 1343623dce13SRobert Watson return (error); 13442c37256eSGarrett Wollman } 13452c37256eSGarrett Wollman 1346b287c6c7SBjoern A. Zeeb #ifdef INET 1347e7d02be1SGleb Smirnoff struct protosw tcp_protosw = { 1348e7d02be1SGleb Smirnoff .pr_type = SOCK_STREAM, 1349e7d02be1SGleb Smirnoff .pr_protocol = IPPROTO_TCP, 1350e7d02be1SGleb Smirnoff .pr_flags = PR_CONNREQUIRED | PR_IMPLOPCL | PR_WANTRCVD | 1351e7d02be1SGleb Smirnoff PR_CAPATTACH, 1352e7d02be1SGleb Smirnoff .pr_ctloutput = tcp_ctloutput, 1353e7d02be1SGleb Smirnoff .pr_abort = tcp_usr_abort, 1354e7d02be1SGleb Smirnoff .pr_accept = tcp_usr_accept, 1355e7d02be1SGleb Smirnoff .pr_attach = tcp_usr_attach, 1356e7d02be1SGleb Smirnoff .pr_bind = tcp_usr_bind, 1357e7d02be1SGleb Smirnoff .pr_connect = tcp_usr_connect, 1358e7d02be1SGleb Smirnoff .pr_control = in_control, 1359e7d02be1SGleb Smirnoff .pr_detach = tcp_usr_detach, 1360e7d02be1SGleb Smirnoff .pr_disconnect = tcp_usr_disconnect, 1361e7d02be1SGleb Smirnoff .pr_listen = tcp_usr_listen, 1362e7d02be1SGleb Smirnoff .pr_peeraddr = in_getpeeraddr, 1363e7d02be1SGleb Smirnoff .pr_rcvd = tcp_usr_rcvd, 1364e7d02be1SGleb Smirnoff .pr_rcvoob = tcp_usr_rcvoob, 1365e7d02be1SGleb Smirnoff .pr_send = tcp_usr_send, 1366e7d02be1SGleb Smirnoff .pr_ready = tcp_usr_ready, 1367e7d02be1SGleb Smirnoff .pr_shutdown = tcp_usr_shutdown, 1368e7d02be1SGleb Smirnoff .pr_sockaddr = in_getsockaddr, 1369e7d02be1SGleb Smirnoff .pr_sosetlabel = in_pcbsosetlabel, 1370e7d02be1SGleb Smirnoff .pr_close = tcp_usr_close, 13712c37256eSGarrett Wollman }; 1372b287c6c7SBjoern A. Zeeb #endif /* INET */ 1373df8bae1dSRodney W. Grimes 1374fb59c426SYoshinobu Inoue #ifdef INET6 1375e7d02be1SGleb Smirnoff struct protosw tcp6_protosw = { 1376e7d02be1SGleb Smirnoff .pr_type = SOCK_STREAM, 1377e7d02be1SGleb Smirnoff .pr_protocol = IPPROTO_TCP, 1378e7d02be1SGleb Smirnoff .pr_flags = PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD | 1379e7d02be1SGleb Smirnoff PR_CAPATTACH, 1380e7d02be1SGleb Smirnoff .pr_ctloutput = tcp_ctloutput, 1381e7d02be1SGleb Smirnoff .pr_abort = tcp_usr_abort, 1382e7d02be1SGleb Smirnoff .pr_accept = tcp6_usr_accept, 1383e7d02be1SGleb Smirnoff .pr_attach = tcp_usr_attach, 1384e7d02be1SGleb Smirnoff .pr_bind = tcp6_usr_bind, 1385e7d02be1SGleb Smirnoff .pr_connect = tcp6_usr_connect, 1386e7d02be1SGleb Smirnoff .pr_control = in6_control, 1387e7d02be1SGleb Smirnoff .pr_detach = tcp_usr_detach, 1388e7d02be1SGleb Smirnoff .pr_disconnect = tcp_usr_disconnect, 1389e7d02be1SGleb Smirnoff .pr_listen = tcp6_usr_listen, 1390e7d02be1SGleb Smirnoff .pr_peeraddr = in6_mapped_peeraddr, 1391e7d02be1SGleb Smirnoff .pr_rcvd = tcp_usr_rcvd, 1392e7d02be1SGleb Smirnoff .pr_rcvoob = tcp_usr_rcvoob, 1393e7d02be1SGleb Smirnoff .pr_send = tcp_usr_send, 1394e7d02be1SGleb Smirnoff .pr_ready = tcp_usr_ready, 1395e7d02be1SGleb Smirnoff .pr_shutdown = tcp_usr_shutdown, 1396e7d02be1SGleb Smirnoff .pr_sockaddr = in6_mapped_sockaddr, 1397e7d02be1SGleb Smirnoff .pr_sosetlabel = in_pcbsosetlabel, 1398e7d02be1SGleb Smirnoff .pr_close = tcp_usr_close, 1399fb59c426SYoshinobu Inoue }; 1400fb59c426SYoshinobu Inoue #endif /* INET6 */ 1401fb59c426SYoshinobu Inoue 1402b287c6c7SBjoern A. Zeeb #ifdef INET 1403a0292f23SGarrett Wollman /* 1404a0292f23SGarrett Wollman * Common subroutine to open a TCP connection to remote host specified 1405a0292f23SGarrett Wollman * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 14065200e00eSIan Dowse * port number if needed. Call in_pcbconnect_setup to do the routing and 14075200e00eSIan Dowse * to choose a local host address (interface). If there is an existing 14085200e00eSIan Dowse * incarnation of the same connection in TIME-WAIT state and if the remote 14095200e00eSIan Dowse * host was sending CC options and if the connection duration was < MSL, then 1410a0292f23SGarrett Wollman * truncate the previous TIME-WAIT state and proceed. 1411a0292f23SGarrett Wollman * Initialize connection parameters and enter SYN-SENT state. 1412a0292f23SGarrett Wollman */ 14130312fbe9SPoul-Henning Kamp static int 1414ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1415a0292f23SGarrett Wollman { 14169eb0e832SGleb Smirnoff struct inpcb *inp = tptoinpcb(tp), *oinp; 14179eb0e832SGleb Smirnoff struct socket *so = tptosocket(tp); 14185200e00eSIan Dowse struct in_addr laddr; 14195200e00eSIan Dowse u_short lport; 1420c3229e05SDavid Greenman int error; 1421a0292f23SGarrett Wollman 1422c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 14238501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1424fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 1425623dce13SRobert Watson 142625102351SMike Karels if (V_tcp_require_unique_port && inp->inp_lport == 0) { 14274616026fSErmal Luçi error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 14284616026fSErmal Luçi if (error) 1429fa046d87SRobert Watson goto out; 1430a0292f23SGarrett Wollman } 1431a0292f23SGarrett Wollman 1432a0292f23SGarrett Wollman /* 1433a0292f23SGarrett Wollman * Cannot simply call in_pcbconnect, because there might be an 1434a0292f23SGarrett Wollman * earlier incarnation of this same connection still in 1435a0292f23SGarrett Wollman * TIME_WAIT state, creating an ADDRINUSE error. 1436a0292f23SGarrett Wollman */ 14375200e00eSIan Dowse laddr = inp->inp_laddr; 14385200e00eSIan Dowse lport = inp->inp_lport; 14395200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 1440b0330ed9SPawel Jakub Dawidek &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 14415200e00eSIan Dowse if (error && oinp == NULL) 1442fa046d87SRobert Watson goto out; 1443fa046d87SRobert Watson if (oinp) { 1444fa046d87SRobert Watson error = EADDRINUSE; 1445fa046d87SRobert Watson goto out; 1446fa046d87SRobert Watson } 144725102351SMike Karels /* Handle initial bind if it hadn't been done in advance. */ 144825102351SMike Karels if (inp->inp_lport == 0) { 144925102351SMike Karels inp->inp_lport = lport; 145025102351SMike Karels if (in_pcbinshash(inp) != 0) { 145125102351SMike Karels inp->inp_lport = 0; 145225102351SMike Karels error = EAGAIN; 145325102351SMike Karels goto out; 145425102351SMike Karels } 145525102351SMike Karels } 14565200e00eSIan Dowse inp->inp_laddr = laddr; 145715bd2b43SDavid Greenman in_pcbrehash(inp); 1458fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1459a0292f23SGarrett Wollman 1460087b55eaSAndre Oppermann /* 1461087b55eaSAndre Oppermann * Compute window scaling to request: 1462087b55eaSAndre Oppermann * Scale to fit into sweet spot. See tcp_syncache.c. 1463087b55eaSAndre Oppermann * XXX: This should move to tcp_output(). 1464087b55eaSAndre Oppermann */ 1465a0292f23SGarrett Wollman while (tp->request_r_scale < TCP_MAX_WINSHIFT && 14669b3bc6bfSMike Silbersack (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1467a0292f23SGarrett Wollman tp->request_r_scale++; 1468a0292f23SGarrett Wollman 1469a0292f23SGarrett Wollman soisconnecting(so); 147078b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 147157f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 14728e02b4e0SMichael Tuexen tp->iss = tcp_new_isn(&inp->inp_inc); 14738e02b4e0SMichael Tuexen if (tp->t_flags & TF_REQ_TSTMP) 14748e02b4e0SMichael Tuexen tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc); 1475a0292f23SGarrett Wollman tcp_sendseqinit(tp); 1476a45d2726SAndras Olah 1477a0292f23SGarrett Wollman return 0; 1478fa046d87SRobert Watson 1479fa046d87SRobert Watson out: 1480fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1481fa046d87SRobert Watson return (error); 1482a0292f23SGarrett Wollman } 1483b287c6c7SBjoern A. Zeeb #endif /* INET */ 1484a0292f23SGarrett Wollman 1485fb59c426SYoshinobu Inoue #ifdef INET6 1486fb59c426SYoshinobu Inoue static int 1487ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1488fb59c426SYoshinobu Inoue { 14899eb0e832SGleb Smirnoff struct inpcb *inp = tptoinpcb(tp); 1490fb59c426SYoshinobu Inoue int error; 1491fb59c426SYoshinobu Inoue 14928501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1493fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 1494623dce13SRobert Watson 149525102351SMike Karels if (V_tcp_require_unique_port && inp->inp_lport == 0) { 14964616026fSErmal Luçi error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 14974616026fSErmal Luçi if (error) 1498fa046d87SRobert Watson goto out; 1499fb59c426SYoshinobu Inoue } 1500a7e201bbSAndrey V. Elsukov error = in6_pcbconnect(inp, nam, td->td_ucred); 1501a7e201bbSAndrey V. Elsukov if (error != 0) 1502b598155aSRobert Watson goto out; 1503fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1504fb59c426SYoshinobu Inoue 1505fb59c426SYoshinobu Inoue /* Compute window scaling to request. */ 1506fb59c426SYoshinobu Inoue while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1507970caf60SBjoern A. Zeeb (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1508fb59c426SYoshinobu Inoue tp->request_r_scale++; 1509fb59c426SYoshinobu Inoue 1510a7e201bbSAndrey V. Elsukov soisconnecting(inp->inp_socket); 151178b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 151257f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 15138e02b4e0SMichael Tuexen tp->iss = tcp_new_isn(&inp->inp_inc); 15148e02b4e0SMichael Tuexen if (tp->t_flags & TF_REQ_TSTMP) 15158e02b4e0SMichael Tuexen tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc); 1516fb59c426SYoshinobu Inoue tcp_sendseqinit(tp); 1517fb59c426SYoshinobu Inoue 1518fb59c426SYoshinobu Inoue return 0; 1519fa046d87SRobert Watson 1520fa046d87SRobert Watson out: 1521fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1522fa046d87SRobert Watson return error; 1523fb59c426SYoshinobu Inoue } 1524fb59c426SYoshinobu Inoue #endif /* INET6 */ 1525fb59c426SYoshinobu Inoue 1526cfe8b629SGarrett Wollman /* 1527b8af5dfaSRobert Watson * Export TCP internal state information via a struct tcp_info, based on the 1528b8af5dfaSRobert Watson * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1529b8af5dfaSRobert Watson * (TCP state machine, etc). We export all information using FreeBSD-native 1530b8af5dfaSRobert Watson * constants -- for example, the numeric values for tcpi_state will differ 1531b8af5dfaSRobert Watson * from Linux. 1532b8af5dfaSRobert Watson */ 1533b8af5dfaSRobert Watson static void 1534ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) 1535b8af5dfaSRobert Watson { 1536b8af5dfaSRobert Watson 15379eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(tptoinpcb(tp)); 1538b8af5dfaSRobert Watson bzero(ti, sizeof(*ti)); 1539b8af5dfaSRobert Watson 1540b8af5dfaSRobert Watson ti->tcpi_state = tp->t_state; 1541b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1542b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 15433529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 1544b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_SACK; 1545b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1546b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_WSCALE; 1547b8af5dfaSRobert Watson ti->tcpi_snd_wscale = tp->snd_scale; 1548b8af5dfaSRobert Watson ti->tcpi_rcv_wscale = tp->rcv_scale; 1549b8af5dfaSRobert Watson } 15503f169c54SRichard Scheffenegger if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) 15515a17b6adSMichael Tuexen ti->tcpi_options |= TCPI_OPT_ECN; 15521baaf834SBruce M Simpson 155343d94734SJohn Baldwin ti->tcpi_rto = tp->t_rxtcur * tick; 15543ac12506SJonathan T. Looney ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick; 15551baaf834SBruce M Simpson ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 15561baaf834SBruce M Simpson ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT; 15571baaf834SBruce M Simpson 1558b8af5dfaSRobert Watson ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1559b8af5dfaSRobert Watson ti->tcpi_snd_cwnd = tp->snd_cwnd; 1560b8af5dfaSRobert Watson 1561b8af5dfaSRobert Watson /* 1562b8af5dfaSRobert Watson * FreeBSD-specific extension fields for tcp_info. 1563b8af5dfaSRobert Watson */ 1564c8443a1dSRobert Watson ti->tcpi_rcv_space = tp->rcv_wnd; 1565535fbad6SKip Macy ti->tcpi_rcv_nxt = tp->rcv_nxt; 1566b8af5dfaSRobert Watson ti->tcpi_snd_wnd = tp->snd_wnd; 15671c18314dSAndre Oppermann ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */ 1568535fbad6SKip Macy ti->tcpi_snd_nxt = tp->snd_nxt; 156943d94734SJohn Baldwin ti->tcpi_snd_mss = tp->t_maxseg; 157043d94734SJohn Baldwin ti->tcpi_rcv_mss = tp->t_maxseg; 1571f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack; 1572f5d34df5SGeorge V. Neville-Neil ti->tcpi_rcv_ooopack = tp->t_rcvoopack; 1573f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_zerowin = tp->t_sndzerowin; 1574a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD 1575a6456410SNavdeep Parhar if (tp->t_flags & TF_TOE) { 1576a6456410SNavdeep Parhar ti->tcpi_options |= TCPI_OPT_TOE; 1577a6456410SNavdeep Parhar tcp_offload_tcp_info(tp, ti); 1578a6456410SNavdeep Parhar } 1579a6456410SNavdeep Parhar #endif 158022c81cc5SRichard Scheffenegger /* 158122c81cc5SRichard Scheffenegger * AccECN related counters. 158222c81cc5SRichard Scheffenegger */ 158322c81cc5SRichard Scheffenegger if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) == 158422c81cc5SRichard Scheffenegger (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) 158522c81cc5SRichard Scheffenegger /* 158622c81cc5SRichard Scheffenegger * Internal counter starts at 5 for AccECN 158722c81cc5SRichard Scheffenegger * but 0 for RFC3168 ECN. 158822c81cc5SRichard Scheffenegger */ 158922c81cc5SRichard Scheffenegger ti->tcpi_delivered_ce = tp->t_scep - 5; 159022c81cc5SRichard Scheffenegger else 159122c81cc5SRichard Scheffenegger ti->tcpi_delivered_ce = tp->t_scep; 159222c81cc5SRichard Scheffenegger ti->tcpi_received_ce = tp->t_rcep; 1593b8af5dfaSRobert Watson } 1594b8af5dfaSRobert Watson 1595b8af5dfaSRobert Watson /* 15961e8f5ffaSRobert Watson * tcp_ctloutput() must drop the inpcb lock before performing copyin on 15971e8f5ffaSRobert Watson * socket option arguments. When it re-acquires the lock after the copy, it 15981e8f5ffaSRobert Watson * has to revalidate that the connection is still valid for the socket 15991e8f5ffaSRobert Watson * option. 1600cfe8b629SGarrett Wollman */ 1601bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \ 16028501a69cSRobert Watson INP_WLOCK(inp); \ 160353af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { \ 16048501a69cSRobert Watson INP_WUNLOCK(inp); \ 1605bac5bedfSConrad Meyer cleanup; \ 16061e8f5ffaSRobert Watson return (ECONNRESET); \ 16071e8f5ffaSRobert Watson } \ 16081e8f5ffaSRobert Watson tp = intotcpcb(inp); \ 16091e8f5ffaSRobert Watson } while(0) 1610bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */) 16111e8f5ffaSRobert Watson 1612fd7daa72SMichael Tuexen int 1613fc4d53ccSGleb Smirnoff tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt) 1614df8bae1dSRodney W. Grimes { 1615fd7daa72SMichael Tuexen struct socket *so = inp->inp_socket; 1616fc4d53ccSGleb Smirnoff struct tcpcb *tp = intotcpcb(inp); 1617fc4d53ccSGleb Smirnoff int error = 0; 1618df8bae1dSRodney W. Grimes 1619fc4d53ccSGleb Smirnoff MPASS(sopt->sopt_dir == SOPT_SET); 16203b3c08c1SMichael Tuexen INP_WLOCK_ASSERT(inp); 162153af6903SGleb Smirnoff KASSERT((inp->inp_flags & INP_DROPPED) == 0, 1622fd7daa72SMichael Tuexen ("inp_flags == %x", inp->inp_flags)); 1623fd7daa72SMichael Tuexen KASSERT(so != NULL, ("inp_socket == NULL")); 1624fc4d53ccSGleb Smirnoff 1625cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_TCP) { 16263b3c08c1SMichael Tuexen INP_WUNLOCK(inp); 1627fb59c426SYoshinobu Inoue #ifdef INET6 1628de156263SGleb Smirnoff if (inp->inp_vflag & INP_IPV6PROTO) 1629fd7daa72SMichael Tuexen error = ip6_ctloutput(so, sopt); 1630de156263SGleb Smirnoff #endif 1631de156263SGleb Smirnoff #if defined(INET6) && defined(INET) 1632de156263SGleb Smirnoff else 1633de156263SGleb Smirnoff #endif 1634de156263SGleb Smirnoff #ifdef INET 1635fd7daa72SMichael Tuexen error = ip_ctloutput(so, sopt); 1636de156263SGleb Smirnoff #endif 16375dff1c38SMichael Tuexen /* 1638de156263SGleb Smirnoff * When an IP-level socket option affects TCP, pass control 1639de156263SGleb Smirnoff * down to stack tfb_tcp_ctloutput, otherwise return what 1640de156263SGleb Smirnoff * IP level returned. 16415dff1c38SMichael Tuexen */ 1642de156263SGleb Smirnoff switch (sopt->sopt_level) { 1643de156263SGleb Smirnoff #ifdef INET6 1644de156263SGleb Smirnoff case IPPROTO_IPV6: 1645de156263SGleb Smirnoff if ((inp->inp_vflag & INP_IPV6PROTO) == 0) 1646de156263SGleb Smirnoff return (error); 1647de156263SGleb Smirnoff switch (sopt->sopt_name) { 1648de156263SGleb Smirnoff case IPV6_TCLASS: 1649de156263SGleb Smirnoff /* Notify tcp stacks that care (e.g. RACK). */ 1650de156263SGleb Smirnoff break; 1651de156263SGleb Smirnoff case IPV6_USE_MIN_MTU: 1652f581a26eSGleb Smirnoff /* Update t_maxseg accordingly. */ 1653f581a26eSGleb Smirnoff break; 1654de156263SGleb Smirnoff default: 1655de156263SGleb Smirnoff return (error); 16565dff1c38SMichael Tuexen } 1657de156263SGleb Smirnoff break; 1658b287c6c7SBjoern A. Zeeb #endif 1659b287c6c7SBjoern A. Zeeb #ifdef INET 1660de156263SGleb Smirnoff case IPPROTO_IP: 1661de156263SGleb Smirnoff switch (sopt->sopt_name) { 1662de156263SGleb Smirnoff case IP_TOS: 16633b0ee680SRichard Scheffenegger inp->inp_ip_tos &= ~IPTOS_ECN_MASK; 16643b0ee680SRichard Scheffenegger break; 1665de156263SGleb Smirnoff case IP_TTL: 1666de156263SGleb Smirnoff /* Notify tcp stacks that care (e.g. RACK). */ 1667de156263SGleb Smirnoff break; 1668de156263SGleb Smirnoff default: 1669df8bae1dSRodney W. Grimes return (error); 1670de156263SGleb Smirnoff } 1671de156263SGleb Smirnoff break; 1672de156263SGleb Smirnoff #endif 1673de156263SGleb Smirnoff default: 1674de156263SGleb Smirnoff return (error); 1675de156263SGleb Smirnoff } 16763b3c08c1SMichael Tuexen INP_WLOCK(inp); 167753af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 16783b3c08c1SMichael Tuexen INP_WUNLOCK(inp); 16793b3c08c1SMichael Tuexen return (ECONNRESET); 16803b3c08c1SMichael Tuexen } 1681fc4d53ccSGleb Smirnoff } else if (sopt->sopt_name == TCP_FUNCTION_BLK) { 1682fc4d53ccSGleb Smirnoff /* 1683fc4d53ccSGleb Smirnoff * Protect the TCP option TCP_FUNCTION_BLK so 1684fc4d53ccSGleb Smirnoff * that a sub-function can *never* overwrite this. 1685fc4d53ccSGleb Smirnoff */ 1686fc4d53ccSGleb Smirnoff struct tcp_function_set fsn; 1687fc4d53ccSGleb Smirnoff struct tcp_function_block *blk; 1688fc4d53ccSGleb Smirnoff 16893b3c08c1SMichael Tuexen INP_WUNLOCK(inp); 1690fc4d53ccSGleb Smirnoff error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn); 1691fc4d53ccSGleb Smirnoff if (error) 1692fc4d53ccSGleb Smirnoff return (error); 1693fc4d53ccSGleb Smirnoff 169468cea2b1SJohn Baldwin INP_WLOCK(inp); 169553af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 16968501a69cSRobert Watson INP_WUNLOCK(inp); 16971e8f5ffaSRobert Watson return (ECONNRESET); 1698623dce13SRobert Watson } 169955bceb1eSRandall Stewart tp = intotcpcb(inp); 1700fc4d53ccSGleb Smirnoff 170155bceb1eSRandall Stewart blk = find_and_ref_tcp_functions(&fsn); 170255bceb1eSRandall Stewart if (blk == NULL) { 170355bceb1eSRandall Stewart INP_WUNLOCK(inp); 170455bceb1eSRandall Stewart return (ENOENT); 170555bceb1eSRandall Stewart } 1706587d67c0SRandall Stewart if (tp->t_fb == blk) { 1707587d67c0SRandall Stewart /* You already have this */ 1708587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1709587d67c0SRandall Stewart INP_WUNLOCK(inp); 1710587d67c0SRandall Stewart return (0); 1711587d67c0SRandall Stewart } 1712587d67c0SRandall Stewart if (tp->t_state != TCPS_CLOSED) { 1713587d67c0SRandall Stewart /* 1714587d67c0SRandall Stewart * The user has advanced the state 1715587d67c0SRandall Stewart * past the initial point, we may not 1716587d67c0SRandall Stewart * be able to switch. 1717587d67c0SRandall Stewart */ 1718587d67c0SRandall Stewart if (blk->tfb_tcp_handoff_ok != NULL) { 1719587d67c0SRandall Stewart /* 1720587d67c0SRandall Stewart * Does the stack provide a 1721587d67c0SRandall Stewart * query mechanism, if so it may 1722587d67c0SRandall Stewart * still be possible? 1723587d67c0SRandall Stewart */ 1724587d67c0SRandall Stewart error = (*blk->tfb_tcp_handoff_ok)(tp); 1725c6c0be27SMichael Tuexen } else 1726c6c0be27SMichael Tuexen error = EINVAL; 1727587d67c0SRandall Stewart if (error) { 1728587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1729587d67c0SRandall Stewart INP_WUNLOCK(inp); 1730587d67c0SRandall Stewart return(error); 1731587d67c0SRandall Stewart } 1732587d67c0SRandall Stewart } 173355bceb1eSRandall Stewart if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) { 173455bceb1eSRandall Stewart refcount_release(&blk->tfb_refcnt); 173555bceb1eSRandall Stewart INP_WUNLOCK(inp); 173655bceb1eSRandall Stewart return (ENOENT); 173755bceb1eSRandall Stewart } 173855bceb1eSRandall Stewart /* 173955bceb1eSRandall Stewart * Release the old refcnt, the 1740587d67c0SRandall Stewart * lookup acquired a ref on the 1741587d67c0SRandall Stewart * new one already. 174255bceb1eSRandall Stewart */ 1743587d67c0SRandall Stewart if (tp->t_fb->tfb_tcp_fb_fini) { 1744086a3556SAndrew Gallatin struct epoch_tracker et; 1745587d67c0SRandall Stewart /* 1746587d67c0SRandall Stewart * Tell the stack to cleanup with 0 i.e. 1747587d67c0SRandall Stewart * the tcb is not going away. 1748587d67c0SRandall Stewart */ 1749086a3556SAndrew Gallatin NET_EPOCH_ENTER(et); 1750587d67c0SRandall Stewart (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0); 1751086a3556SAndrew Gallatin NET_EPOCH_EXIT(et); 1752587d67c0SRandall Stewart } 17533ee9c3c4SRandall Stewart #ifdef TCPHPTS 17543ee9c3c4SRandall Stewart /* Assure that we are not on any hpts */ 17559eb0e832SGleb Smirnoff tcp_hpts_remove(tptoinpcb(tp)); 17563ee9c3c4SRandall Stewart #endif 17573ee9c3c4SRandall Stewart if (blk->tfb_tcp_fb_init) { 17583ee9c3c4SRandall Stewart error = (*blk->tfb_tcp_fb_init)(tp); 17593ee9c3c4SRandall Stewart if (error) { 17603ee9c3c4SRandall Stewart refcount_release(&blk->tfb_refcnt); 17613ee9c3c4SRandall Stewart if (tp->t_fb->tfb_tcp_fb_init) { 17623ee9c3c4SRandall Stewart if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) { 17633ee9c3c4SRandall Stewart /* Fall back failed, drop the connection */ 17643ee9c3c4SRandall Stewart INP_WUNLOCK(inp); 1765fd7daa72SMichael Tuexen soabort(so); 17663ee9c3c4SRandall Stewart return (error); 17673ee9c3c4SRandall Stewart } 17683ee9c3c4SRandall Stewart } 17693ee9c3c4SRandall Stewart goto err_out; 17703ee9c3c4SRandall Stewart } 17713ee9c3c4SRandall Stewart } 177255bceb1eSRandall Stewart refcount_release(&tp->t_fb->tfb_refcnt); 177355bceb1eSRandall Stewart tp->t_fb = blk; 177455bceb1eSRandall Stewart #ifdef TCP_OFFLOAD 177555bceb1eSRandall Stewart if (tp->t_flags & TF_TOE) { 177655bceb1eSRandall Stewart tcp_offload_ctloutput(tp, sopt->sopt_dir, 177755bceb1eSRandall Stewart sopt->sopt_name); 177855bceb1eSRandall Stewart } 177955bceb1eSRandall Stewart #endif 17803ee9c3c4SRandall Stewart err_out: 178155bceb1eSRandall Stewart INP_WUNLOCK(inp); 178255bceb1eSRandall Stewart return (error); 1783fc4d53ccSGleb Smirnoff } 1784fc4d53ccSGleb Smirnoff 17853b3c08c1SMichael Tuexen /* Pass in the INP locked, callee must unlock it. */ 17863b3c08c1SMichael Tuexen return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt)); 1787fc4d53ccSGleb Smirnoff } 1788fc4d53ccSGleb Smirnoff 1789fc4d53ccSGleb Smirnoff static int 1790fc4d53ccSGleb Smirnoff tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt) 1791fc4d53ccSGleb Smirnoff { 1792fd7daa72SMichael Tuexen struct socket *so = inp->inp_socket; 1793fd7daa72SMichael Tuexen struct tcpcb *tp = intotcpcb(inp); 1794fc4d53ccSGleb Smirnoff int error = 0; 1795fc4d53ccSGleb Smirnoff 1796fc4d53ccSGleb Smirnoff MPASS(sopt->sopt_dir == SOPT_GET); 17973b3c08c1SMichael Tuexen INP_WLOCK_ASSERT(inp); 179853af6903SGleb Smirnoff KASSERT((inp->inp_flags & INP_DROPPED) == 0, 1799fd7daa72SMichael Tuexen ("inp_flags == %x", inp->inp_flags)); 1800fd7daa72SMichael Tuexen KASSERT(so != NULL, ("inp_socket == NULL")); 1801fc4d53ccSGleb Smirnoff 1802fc4d53ccSGleb Smirnoff if (sopt->sopt_level != IPPROTO_TCP) { 18033b3c08c1SMichael Tuexen INP_WUNLOCK(inp); 1804fc4d53ccSGleb Smirnoff #ifdef INET6 1805fc4d53ccSGleb Smirnoff if (inp->inp_vflag & INP_IPV6PROTO) 1806fd7daa72SMichael Tuexen error = ip6_ctloutput(so, sopt); 1807fc4d53ccSGleb Smirnoff #endif /* INET6 */ 1808fc4d53ccSGleb Smirnoff #if defined(INET6) && defined(INET) 1809fc4d53ccSGleb Smirnoff else 1810fc4d53ccSGleb Smirnoff #endif 1811fc4d53ccSGleb Smirnoff #ifdef INET 1812fd7daa72SMichael Tuexen error = ip_ctloutput(so, sopt); 1813fc4d53ccSGleb Smirnoff #endif 1814fc4d53ccSGleb Smirnoff return (error); 1815fc4d53ccSGleb Smirnoff } 1816fc4d53ccSGleb Smirnoff if (((sopt->sopt_name == TCP_FUNCTION_BLK) || 1817e2833083SPeter Lei (sopt->sopt_name == TCP_FUNCTION_ALIAS))) { 1818fc4d53ccSGleb Smirnoff struct tcp_function_set fsn; 1819fc4d53ccSGleb Smirnoff 1820e2833083SPeter Lei if (sopt->sopt_name == TCP_FUNCTION_ALIAS) { 1821e2833083SPeter Lei memset(&fsn, 0, sizeof(fsn)); 1822e2833083SPeter Lei find_tcp_function_alias(tp->t_fb, &fsn); 1823e2833083SPeter Lei } else { 1824e2833083SPeter Lei strncpy(fsn.function_set_name, 1825e2833083SPeter Lei tp->t_fb->tfb_tcp_block_name, 1826c73b6f4dSEd Maste TCP_FUNCTION_NAME_LEN_MAX); 1827c73b6f4dSEd Maste fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0'; 1828e2833083SPeter Lei } 182955bceb1eSRandall Stewart fsn.pcbcnt = tp->t_fb->tfb_refcnt; 183055bceb1eSRandall Stewart INP_WUNLOCK(inp); 183155bceb1eSRandall Stewart error = sooptcopyout(sopt, &fsn, sizeof fsn); 183255bceb1eSRandall Stewart return (error); 183355bceb1eSRandall Stewart } 1834fc4d53ccSGleb Smirnoff 18353b3c08c1SMichael Tuexen /* Pass in the INP locked, callee must unlock it. */ 18363b3c08c1SMichael Tuexen return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt)); 1837fc4d53ccSGleb Smirnoff } 1838fc4d53ccSGleb Smirnoff 1839fc4d53ccSGleb Smirnoff int 1840fc4d53ccSGleb Smirnoff tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1841fc4d53ccSGleb Smirnoff { 1842fc4d53ccSGleb Smirnoff struct inpcb *inp; 1843fc4d53ccSGleb Smirnoff 1844fc4d53ccSGleb Smirnoff inp = sotoinpcb(so); 1845fc4d53ccSGleb Smirnoff KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 1846fc4d53ccSGleb Smirnoff 18473b3c08c1SMichael Tuexen INP_WLOCK(inp); 184853af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 18493b3c08c1SMichael Tuexen INP_WUNLOCK(inp); 18503b3c08c1SMichael Tuexen return (ECONNRESET); 18513b3c08c1SMichael Tuexen } 1852fc4d53ccSGleb Smirnoff if (sopt->sopt_dir == SOPT_SET) 1853fc4d53ccSGleb Smirnoff return (tcp_ctloutput_set(inp, sopt)); 1854fc4d53ccSGleb Smirnoff else if (sopt->sopt_dir == SOPT_GET) 1855fc4d53ccSGleb Smirnoff return (tcp_ctloutput_get(inp, sopt)); 1856fc4d53ccSGleb Smirnoff else 1857fc4d53ccSGleb Smirnoff panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir); 185855bceb1eSRandall Stewart } 185955bceb1eSRandall Stewart 18602529f56eSJonathan T. Looney /* 18612529f56eSJonathan T. Looney * If this assert becomes untrue, we need to change the size of the buf 18622529f56eSJonathan T. Looney * variable in tcp_default_ctloutput(). 18632529f56eSJonathan T. Looney */ 18642529f56eSJonathan T. Looney #ifdef CTASSERT 18652529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN); 18662529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN); 18672529f56eSJonathan T. Looney #endif 18682529f56eSJonathan T. Looney 1869ec1db6e1SJohn Baldwin #ifdef KERN_TLS 1870ec1db6e1SJohn Baldwin static int 1871ec1db6e1SJohn Baldwin copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls) 1872ec1db6e1SJohn Baldwin { 1873ec1db6e1SJohn Baldwin struct tls_enable_v0 tls_v0; 1874ec1db6e1SJohn Baldwin int error; 1875ec1db6e1SJohn Baldwin 1876ec1db6e1SJohn Baldwin if (sopt->sopt_valsize == sizeof(tls_v0)) { 1877ec1db6e1SJohn Baldwin error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0), 1878ec1db6e1SJohn Baldwin sizeof(tls_v0)); 1879ec1db6e1SJohn Baldwin if (error) 1880ec1db6e1SJohn Baldwin return (error); 1881ec1db6e1SJohn Baldwin memset(tls, 0, sizeof(*tls)); 1882ec1db6e1SJohn Baldwin tls->cipher_key = tls_v0.cipher_key; 1883ec1db6e1SJohn Baldwin tls->iv = tls_v0.iv; 1884ec1db6e1SJohn Baldwin tls->auth_key = tls_v0.auth_key; 1885ec1db6e1SJohn Baldwin tls->cipher_algorithm = tls_v0.cipher_algorithm; 1886ec1db6e1SJohn Baldwin tls->cipher_key_len = tls_v0.cipher_key_len; 1887ec1db6e1SJohn Baldwin tls->iv_len = tls_v0.iv_len; 1888ec1db6e1SJohn Baldwin tls->auth_algorithm = tls_v0.auth_algorithm; 1889ec1db6e1SJohn Baldwin tls->auth_key_len = tls_v0.auth_key_len; 1890ec1db6e1SJohn Baldwin tls->flags = tls_v0.flags; 1891ec1db6e1SJohn Baldwin tls->tls_vmajor = tls_v0.tls_vmajor; 1892ec1db6e1SJohn Baldwin tls->tls_vminor = tls_v0.tls_vminor; 1893ec1db6e1SJohn Baldwin return (0); 1894ec1db6e1SJohn Baldwin } 1895ec1db6e1SJohn Baldwin 1896ec1db6e1SJohn Baldwin return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls))); 1897ec1db6e1SJohn Baldwin } 1898ec1db6e1SJohn Baldwin #endif 1899ec1db6e1SJohn Baldwin 1900b8d60729SRandall Stewart extern struct cc_algo newreno_cc_algo; 1901b8d60729SRandall Stewart 1902b8d60729SRandall Stewart static int 1903ea9017fbSRandall Stewart tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt) 1904b8d60729SRandall Stewart { 1905b8d60729SRandall Stewart struct cc_algo *algo; 1906b8d60729SRandall Stewart void *ptr = NULL; 19073b3c08c1SMichael Tuexen struct tcpcb *tp; 1908b8d60729SRandall Stewart struct cc_var cc_mem; 1909b8d60729SRandall Stewart char buf[TCP_CA_NAME_MAX]; 1910b8d60729SRandall Stewart size_t mem_sz; 1911b8d60729SRandall Stewart int error; 1912b8d60729SRandall Stewart 1913b8d60729SRandall Stewart INP_WUNLOCK(inp); 1914b8d60729SRandall Stewart error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1); 1915b8d60729SRandall Stewart if (error) 1916b8d60729SRandall Stewart return(error); 1917b8d60729SRandall Stewart buf[sopt->sopt_valsize] = '\0'; 1918b8d60729SRandall Stewart CC_LIST_RLOCK(); 1919ea9017fbSRandall Stewart STAILQ_FOREACH(algo, &cc_list, entries) { 1920b8d60729SRandall Stewart if (strncmp(buf, algo->name, 1921b8d60729SRandall Stewart TCP_CA_NAME_MAX) == 0) { 1922b8d60729SRandall Stewart if (algo->flags & CC_MODULE_BEING_REMOVED) { 1923b8d60729SRandall Stewart /* We can't "see" modules being unloaded */ 1924b8d60729SRandall Stewart continue; 1925b8d60729SRandall Stewart } 1926b8d60729SRandall Stewart break; 1927b8d60729SRandall Stewart } 1928ea9017fbSRandall Stewart } 1929b8d60729SRandall Stewart if (algo == NULL) { 1930b8d60729SRandall Stewart CC_LIST_RUNLOCK(); 1931b8d60729SRandall Stewart return(ESRCH); 1932b8d60729SRandall Stewart } 1933ea9017fbSRandall Stewart /* 1934ea9017fbSRandall Stewart * With a reference the algorithm cannot be removed 1935ea9017fbSRandall Stewart * so we hold a reference through the change process. 1936ea9017fbSRandall Stewart */ 1937ea9017fbSRandall Stewart cc_refer(algo); 1938ea9017fbSRandall Stewart CC_LIST_RUNLOCK(); 1939b8d60729SRandall Stewart if (algo->cb_init != NULL) { 1940b8d60729SRandall Stewart /* We can now pre-get the memory for the CC */ 1941b8d60729SRandall Stewart mem_sz = (*algo->cc_data_sz)(); 1942b8d60729SRandall Stewart if (mem_sz == 0) { 1943b8d60729SRandall Stewart goto no_mem_needed; 1944b8d60729SRandall Stewart } 1945b8d60729SRandall Stewart ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK); 1946b8d60729SRandall Stewart } else { 1947b8d60729SRandall Stewart no_mem_needed: 1948b8d60729SRandall Stewart mem_sz = 0; 1949b8d60729SRandall Stewart ptr = NULL; 1950b8d60729SRandall Stewart } 1951b8d60729SRandall Stewart /* 1952b8d60729SRandall Stewart * Make sure its all clean and zero and also get 1953b8d60729SRandall Stewart * back the inplock. 1954b8d60729SRandall Stewart */ 1955b8d60729SRandall Stewart memset(&cc_mem, 0, sizeof(cc_mem)); 1956df07bfdaSMichael Tuexen INP_WLOCK(inp); 195753af6903SGleb Smirnoff if (inp->inp_flags & INP_DROPPED) { 1958df07bfdaSMichael Tuexen INP_WUNLOCK(inp); 1959ea9017fbSRandall Stewart if (ptr) 1960df07bfdaSMichael Tuexen free(ptr, M_CC_MEM); 1961ea9017fbSRandall Stewart /* Release our temp reference */ 1962ea9017fbSRandall Stewart CC_LIST_RLOCK(); 1963ea9017fbSRandall Stewart cc_release(algo); 1964ea9017fbSRandall Stewart CC_LIST_RUNLOCK(); 1965df07bfdaSMichael Tuexen return (ECONNRESET); 1966df07bfdaSMichael Tuexen } 1967df07bfdaSMichael Tuexen tp = intotcpcb(inp); 1968df07bfdaSMichael Tuexen if (ptr != NULL) 1969b8d60729SRandall Stewart memset(ptr, 0, mem_sz); 1970b8d60729SRandall Stewart cc_mem.ccvc.tcp = tp; 1971b8d60729SRandall Stewart /* 1972b8d60729SRandall Stewart * We once again hold a write lock over the tcb so it's 1973b8d60729SRandall Stewart * safe to do these things without ordering concerns. 1974b8d60729SRandall Stewart * Note here we init into stack memory. 1975b8d60729SRandall Stewart */ 1976b8d60729SRandall Stewart if (algo->cb_init != NULL) 1977b8d60729SRandall Stewart error = algo->cb_init(&cc_mem, ptr); 1978b8d60729SRandall Stewart else 1979b8d60729SRandall Stewart error = 0; 1980b8d60729SRandall Stewart /* 1981b8d60729SRandall Stewart * The CC algorithms, when given their memory 1982b8d60729SRandall Stewart * should not fail we could in theory have a 1983b8d60729SRandall Stewart * KASSERT here. 1984b8d60729SRandall Stewart */ 1985b8d60729SRandall Stewart if (error == 0) { 1986b8d60729SRandall Stewart /* 1987b8d60729SRandall Stewart * Touchdown, lets go ahead and move the 1988b8d60729SRandall Stewart * connection to the new CC module by 1989b8d60729SRandall Stewart * copying in the cc_mem after we call 1990b8d60729SRandall Stewart * the old ones cleanup (if any). 1991b8d60729SRandall Stewart */ 1992b8d60729SRandall Stewart if (CC_ALGO(tp)->cb_destroy != NULL) 1993e68b3792SGleb Smirnoff CC_ALGO(tp)->cb_destroy(&tp->t_ccv); 1994ea9017fbSRandall Stewart /* Detach the old CC from the tcpcb */ 1995ea9017fbSRandall Stewart cc_detach(tp); 1996ea9017fbSRandall Stewart /* Copy in our temp memory that was inited */ 1997e68b3792SGleb Smirnoff memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var)); 1998ea9017fbSRandall Stewart /* Now attach the new, which takes a reference */ 1999ea9017fbSRandall Stewart cc_attach(tp, algo); 2000b8d60729SRandall Stewart /* Ok now are we where we have gotten past any conn_init? */ 2001b8d60729SRandall Stewart if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) { 2002b8d60729SRandall Stewart /* Yep run the connection init for the new CC */ 2003e68b3792SGleb Smirnoff CC_ALGO(tp)->conn_init(&tp->t_ccv); 2004b8d60729SRandall Stewart } 2005b8d60729SRandall Stewart } else if (ptr) 2006b8d60729SRandall Stewart free(ptr, M_CC_MEM); 2007b8d60729SRandall Stewart INP_WUNLOCK(inp); 2008ea9017fbSRandall Stewart /* Now lets release our temp reference */ 2009ea9017fbSRandall Stewart CC_LIST_RLOCK(); 2010ea9017fbSRandall Stewart cc_release(algo); 2011ea9017fbSRandall Stewart CC_LIST_RUNLOCK(); 2012b8d60729SRandall Stewart return (error); 2013b8d60729SRandall Stewart } 2014b8d60729SRandall Stewart 201555bceb1eSRandall Stewart int 20163b3c08c1SMichael Tuexen tcp_default_ctloutput(struct inpcb *inp, struct sockopt *sopt) 201755bceb1eSRandall Stewart { 2018fd7daa72SMichael Tuexen struct tcpcb *tp = intotcpcb(inp); 201955bceb1eSRandall Stewart int error, opt, optval; 202055bceb1eSRandall Stewart u_int ui; 202155bceb1eSRandall Stewart struct tcp_info ti; 2022b2e60773SJohn Baldwin #ifdef KERN_TLS 2023b2e60773SJohn Baldwin struct tls_enable tls; 2024528c7649SMichael Tuexen struct socket *so = inp->inp_socket; 2025b2e60773SJohn Baldwin #endif 20262529f56eSJonathan T. Looney char *pbuf, buf[TCP_LOG_ID_LEN]; 2027adc56f5aSEdward Tomasz Napierala #ifdef STATS 2028adc56f5aSEdward Tomasz Napierala struct statsblob *sbp; 2029adc56f5aSEdward Tomasz Napierala #endif 2030af6fef3aSGleb Smirnoff size_t len; 2031df8bae1dSRodney W. Grimes 2032f581a26eSGleb Smirnoff INP_WLOCK_ASSERT(inp); 203353af6903SGleb Smirnoff KASSERT((inp->inp_flags & INP_DROPPED) == 0, 2034fd7daa72SMichael Tuexen ("inp_flags == %x", inp->inp_flags)); 2035528c7649SMichael Tuexen KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL")); 2036f581a26eSGleb Smirnoff 2037f581a26eSGleb Smirnoff switch (sopt->sopt_level) { 2038f581a26eSGleb Smirnoff #ifdef INET6 2039f581a26eSGleb Smirnoff case IPPROTO_IPV6: 2040f581a26eSGleb Smirnoff MPASS(inp->inp_vflag & INP_IPV6PROTO); 2041f581a26eSGleb Smirnoff switch (sopt->sopt_name) { 2042f581a26eSGleb Smirnoff case IPV6_USE_MIN_MTU: 2043f581a26eSGleb Smirnoff tcp6_use_min_mtu(tp); 2044f581a26eSGleb Smirnoff /* FALLTHROUGH */ 2045f581a26eSGleb Smirnoff } 2046f581a26eSGleb Smirnoff INP_WUNLOCK(inp); 2047f581a26eSGleb Smirnoff return (0); 2048f581a26eSGleb Smirnoff #endif 2049f581a26eSGleb Smirnoff #ifdef INET 2050f581a26eSGleb Smirnoff case IPPROTO_IP: 2051f581a26eSGleb Smirnoff INP_WUNLOCK(inp); 2052f581a26eSGleb Smirnoff return (0); 2053f581a26eSGleb Smirnoff #endif 2054f581a26eSGleb Smirnoff } 2055f581a26eSGleb Smirnoff 2056d519cedbSGleb Smirnoff /* 2057d519cedbSGleb Smirnoff * For TCP_CCALGOOPT forward the control to CC module, for both 2058d519cedbSGleb Smirnoff * SOPT_SET and SOPT_GET. 2059d519cedbSGleb Smirnoff */ 2060d519cedbSGleb Smirnoff switch (sopt->sopt_name) { 2061d519cedbSGleb Smirnoff case TCP_CCALGOOPT: 2062d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 2063c8b53cedSMichael Tuexen if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT) 2064c8b53cedSMichael Tuexen return (EINVAL); 2065af6fef3aSGleb Smirnoff pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO); 2066af6fef3aSGleb Smirnoff error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize, 2067d519cedbSGleb Smirnoff sopt->sopt_valsize); 2068d519cedbSGleb Smirnoff if (error) { 2069af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 2070d519cedbSGleb Smirnoff return (error); 2071d519cedbSGleb Smirnoff } 2072bac5bedfSConrad Meyer INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP)); 2073d519cedbSGleb Smirnoff if (CC_ALGO(tp)->ctl_output != NULL) 2074e68b3792SGleb Smirnoff error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf); 2075d519cedbSGleb Smirnoff else 2076d519cedbSGleb Smirnoff error = ENOENT; 2077d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 2078d519cedbSGleb Smirnoff if (error == 0 && sopt->sopt_dir == SOPT_GET) 2079af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize); 2080af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 2081d519cedbSGleb Smirnoff return (error); 2082d519cedbSGleb Smirnoff } 2083d519cedbSGleb Smirnoff 2084cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 2085cfe8b629SGarrett Wollman case SOPT_SET: 2086cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2087fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 208888f6b043SBruce M Simpson case TCP_MD5SIG: 20898501a69cSRobert Watson INP_WUNLOCK(inp); 209097453e5eSClaudio Jeker if (!TCPMD5_ENABLED()) 2091fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 2092fcf59617SAndrey V. Elsukov error = TCPMD5_PCBCTL(inp, sopt); 20931cfd4b53SBruce M Simpson if (error) 20941e8f5ffaSRobert Watson return (error); 209597453e5eSClaudio Jeker INP_WLOCK_RECHECK(inp); 209609fe6320SNavdeep Parhar goto unlock_and_done; 2097fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 209809fe6320SNavdeep Parhar 2099df8bae1dSRodney W. Grimes case TCP_NODELAY: 2100cfe8b629SGarrett Wollman case TCP_NOOPT: 21010471a8c7SRichard Scheffenegger case TCP_LRD: 21028501a69cSRobert Watson INP_WUNLOCK(inp); 2103cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 2104cfe8b629SGarrett Wollman sizeof optval); 2105cfe8b629SGarrett Wollman if (error) 21061e8f5ffaSRobert Watson return (error); 2107cfe8b629SGarrett Wollman 21088501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 2109cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2110cfe8b629SGarrett Wollman case TCP_NODELAY: 2111cfe8b629SGarrett Wollman opt = TF_NODELAY; 2112cfe8b629SGarrett Wollman break; 2113cfe8b629SGarrett Wollman case TCP_NOOPT: 2114cfe8b629SGarrett Wollman opt = TF_NOOPT; 2115cfe8b629SGarrett Wollman break; 21160471a8c7SRichard Scheffenegger case TCP_LRD: 21170471a8c7SRichard Scheffenegger opt = TF_LRD; 21180471a8c7SRichard Scheffenegger break; 2119cfe8b629SGarrett Wollman default: 2120cfe8b629SGarrett Wollman opt = 0; /* dead code to fool gcc */ 2121cfe8b629SGarrett Wollman break; 2122cfe8b629SGarrett Wollman } 2123cfe8b629SGarrett Wollman 2124cfe8b629SGarrett Wollman if (optval) 2125cfe8b629SGarrett Wollman tp->t_flags |= opt; 2126df8bae1dSRodney W. Grimes else 2127cfe8b629SGarrett Wollman tp->t_flags &= ~opt; 212809fe6320SNavdeep Parhar unlock_and_done: 212909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 213009fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 213109fe6320SNavdeep Parhar tcp_offload_ctloutput(tp, sopt->sopt_dir, 213209fe6320SNavdeep Parhar sopt->sopt_name); 213309fe6320SNavdeep Parhar } 213409fe6320SNavdeep Parhar #endif 21358501a69cSRobert Watson INP_WUNLOCK(inp); 2136df8bae1dSRodney W. Grimes break; 2137df8bae1dSRodney W. Grimes 2138007581c0SJonathan Lemon case TCP_NOPUSH: 21398501a69cSRobert Watson INP_WUNLOCK(inp); 2140007581c0SJonathan Lemon error = sooptcopyin(sopt, &optval, sizeof optval, 2141007581c0SJonathan Lemon sizeof optval); 2142007581c0SJonathan Lemon if (error) 21431e8f5ffaSRobert Watson return (error); 2144007581c0SJonathan Lemon 21458501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 2146007581c0SJonathan Lemon if (optval) 2147007581c0SJonathan Lemon tp->t_flags |= TF_NOPUSH; 2148d28b9e89SJohn Baldwin else if (tp->t_flags & TF_NOPUSH) { 2149007581c0SJonathan Lemon tp->t_flags &= ~TF_NOPUSH; 2150109eb549SGleb Smirnoff if (TCPS_HAVEESTABLISHED(tp->t_state)) { 2151109eb549SGleb Smirnoff struct epoch_tracker et; 2152109eb549SGleb Smirnoff 2153109eb549SGleb Smirnoff NET_EPOCH_ENTER(et); 2154f64dc2abSGleb Smirnoff error = tcp_output_nodrop(tp); 2155109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 2156109eb549SGleb Smirnoff } 2157007581c0SJonathan Lemon } 215809fe6320SNavdeep Parhar goto unlock_and_done; 2159007581c0SJonathan Lemon 21609e644c23SMichael Tuexen case TCP_REMOTE_UDP_ENCAPS_PORT: 21619e644c23SMichael Tuexen INP_WUNLOCK(inp); 21629e644c23SMichael Tuexen error = sooptcopyin(sopt, &optval, sizeof optval, 21639e644c23SMichael Tuexen sizeof optval); 21649e644c23SMichael Tuexen if (error) 21659e644c23SMichael Tuexen return (error); 21669e644c23SMichael Tuexen if ((optval < TCP_TUNNELING_PORT_MIN) || 21679e644c23SMichael Tuexen (optval > TCP_TUNNELING_PORT_MAX)) { 21689e644c23SMichael Tuexen /* Its got to be in range */ 21699e644c23SMichael Tuexen return (EINVAL); 21709e644c23SMichael Tuexen } 21719e644c23SMichael Tuexen if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) { 21729e644c23SMichael Tuexen /* You have to have enabled a UDP tunneling port first */ 21739e644c23SMichael Tuexen return (EINVAL); 21749e644c23SMichael Tuexen } 21759e644c23SMichael Tuexen INP_WLOCK_RECHECK(inp); 21769e644c23SMichael Tuexen if (tp->t_state != TCPS_CLOSED) { 21779e644c23SMichael Tuexen /* You can't change after you are connected */ 21789e644c23SMichael Tuexen error = EINVAL; 21799e644c23SMichael Tuexen } else { 21809e644c23SMichael Tuexen /* Ok we are all good set the port */ 21819e644c23SMichael Tuexen tp->t_port = htons(optval); 21829e644c23SMichael Tuexen } 21839e644c23SMichael Tuexen goto unlock_and_done; 21849e644c23SMichael Tuexen 2185df8bae1dSRodney W. Grimes case TCP_MAXSEG: 21868501a69cSRobert Watson INP_WUNLOCK(inp); 2187cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 2188cfe8b629SGarrett Wollman sizeof optval); 2189cfe8b629SGarrett Wollman if (error) 21901e8f5ffaSRobert Watson return (error); 2191df8bae1dSRodney W. Grimes 21928501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 219353369ac9SAndre Oppermann if (optval > 0 && optval <= tp->t_maxseg && 2194603724d3SBjoern A. Zeeb optval + 40 >= V_tcp_minmss) 2195cfe8b629SGarrett Wollman tp->t_maxseg = optval; 2196a0292f23SGarrett Wollman else 2197a0292f23SGarrett Wollman error = EINVAL; 219809fe6320SNavdeep Parhar goto unlock_and_done; 2199a0292f23SGarrett Wollman 2200b8af5dfaSRobert Watson case TCP_INFO: 22018501a69cSRobert Watson INP_WUNLOCK(inp); 2202b8af5dfaSRobert Watson error = EINVAL; 2203b8af5dfaSRobert Watson break; 2204b8af5dfaSRobert Watson 2205adc56f5aSEdward Tomasz Napierala case TCP_STATS: 2206adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2207adc56f5aSEdward Tomasz Napierala #ifdef STATS 2208adc56f5aSEdward Tomasz Napierala error = sooptcopyin(sopt, &optval, sizeof optval, 2209adc56f5aSEdward Tomasz Napierala sizeof optval); 2210adc56f5aSEdward Tomasz Napierala if (error) 2211adc56f5aSEdward Tomasz Napierala return (error); 2212adc56f5aSEdward Tomasz Napierala 2213adc56f5aSEdward Tomasz Napierala if (optval > 0) 2214adc56f5aSEdward Tomasz Napierala sbp = stats_blob_alloc( 2215adc56f5aSEdward Tomasz Napierala V_tcp_perconn_stats_dflt_tpl, 0); 2216adc56f5aSEdward Tomasz Napierala else 2217adc56f5aSEdward Tomasz Napierala sbp = NULL; 2218adc56f5aSEdward Tomasz Napierala 2219adc56f5aSEdward Tomasz Napierala INP_WLOCK_RECHECK(inp); 2220adc56f5aSEdward Tomasz Napierala if ((tp->t_stats != NULL && sbp == NULL) || 2221adc56f5aSEdward Tomasz Napierala (tp->t_stats == NULL && sbp != NULL)) { 2222adc56f5aSEdward Tomasz Napierala struct statsblob *t = tp->t_stats; 2223adc56f5aSEdward Tomasz Napierala tp->t_stats = sbp; 2224adc56f5aSEdward Tomasz Napierala sbp = t; 2225adc56f5aSEdward Tomasz Napierala } 2226adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2227adc56f5aSEdward Tomasz Napierala 2228adc56f5aSEdward Tomasz Napierala stats_blob_destroy(sbp); 2229adc56f5aSEdward Tomasz Napierala #else 2230adc56f5aSEdward Tomasz Napierala return (EOPNOTSUPP); 2231adc56f5aSEdward Tomasz Napierala #endif /* !STATS */ 2232adc56f5aSEdward Tomasz Napierala break; 2233adc56f5aSEdward Tomasz Napierala 2234dbc42409SLawrence Stewart case TCP_CONGESTION: 2235ea9017fbSRandall Stewart error = tcp_set_cc_mod(inp, sopt); 223673e263b1SGleb Smirnoff break; 2237dbc42409SLawrence Stewart 2238a034518aSAndrew Gallatin case TCP_REUSPORT_LB_NUMA: 2239a034518aSAndrew Gallatin INP_WUNLOCK(inp); 2240a034518aSAndrew Gallatin error = sooptcopyin(sopt, &optval, sizeof(optval), 2241a034518aSAndrew Gallatin sizeof(optval)); 2242a034518aSAndrew Gallatin INP_WLOCK_RECHECK(inp); 2243a034518aSAndrew Gallatin if (!error) 2244a034518aSAndrew Gallatin error = in_pcblbgroup_numa(inp, optval); 2245a034518aSAndrew Gallatin INP_WUNLOCK(inp); 2246a034518aSAndrew Gallatin break; 2247a034518aSAndrew Gallatin 2248b2e60773SJohn Baldwin #ifdef KERN_TLS 2249b2e60773SJohn Baldwin case TCP_TXTLS_ENABLE: 2250b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2251ec1db6e1SJohn Baldwin error = copyin_tls_enable(sopt, &tls); 2252b2e60773SJohn Baldwin if (error) 2253b2e60773SJohn Baldwin break; 2254fd7daa72SMichael Tuexen error = ktls_enable_tx(so, &tls); 2255b2e60773SJohn Baldwin break; 2256b2e60773SJohn Baldwin case TCP_TXTLS_MODE: 2257b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2258b2e60773SJohn Baldwin error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 2259b2e60773SJohn Baldwin if (error) 2260b2e60773SJohn Baldwin return (error); 2261b2e60773SJohn Baldwin 2262b2e60773SJohn Baldwin INP_WLOCK_RECHECK(inp); 2263fd7daa72SMichael Tuexen error = ktls_set_tx_mode(so, ui); 2264b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2265b2e60773SJohn Baldwin break; 2266f1f93475SJohn Baldwin case TCP_RXTLS_ENABLE: 2267f1f93475SJohn Baldwin INP_WUNLOCK(inp); 2268f1f93475SJohn Baldwin error = sooptcopyin(sopt, &tls, sizeof(tls), 2269f1f93475SJohn Baldwin sizeof(tls)); 2270f1f93475SJohn Baldwin if (error) 2271f1f93475SJohn Baldwin break; 2272fd7daa72SMichael Tuexen error = ktls_enable_rx(so, &tls); 2273f1f93475SJohn Baldwin break; 2274b2e60773SJohn Baldwin #endif 227508af8aacSRandall Stewart case TCP_MAXUNACKTIME: 22769077f387SGleb Smirnoff case TCP_KEEPIDLE: 22779077f387SGleb Smirnoff case TCP_KEEPINTVL: 22789077f387SGleb Smirnoff case TCP_KEEPINIT: 22799077f387SGleb Smirnoff INP_WUNLOCK(inp); 22809077f387SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 22819077f387SGleb Smirnoff if (error) 22829077f387SGleb Smirnoff return (error); 22839077f387SGleb Smirnoff 22849077f387SGleb Smirnoff if (ui > (UINT_MAX / hz)) { 22859077f387SGleb Smirnoff error = EINVAL; 22869077f387SGleb Smirnoff break; 22879077f387SGleb Smirnoff } 22889077f387SGleb Smirnoff ui *= hz; 22899077f387SGleb Smirnoff 22909077f387SGleb Smirnoff INP_WLOCK_RECHECK(inp); 22919077f387SGleb Smirnoff switch (sopt->sopt_name) { 229208af8aacSRandall Stewart case TCP_MAXUNACKTIME: 229308af8aacSRandall Stewart tp->t_maxunacktime = ui; 229408af8aacSRandall Stewart break; 229508af8aacSRandall Stewart 22969077f387SGleb Smirnoff case TCP_KEEPIDLE: 22979077f387SGleb Smirnoff tp->t_keepidle = ui; 22989077f387SGleb Smirnoff /* 22999077f387SGleb Smirnoff * XXX: better check current remaining 23009077f387SGleb Smirnoff * timeout and "merge" it with new value. 23019077f387SGleb Smirnoff */ 23029077f387SGleb Smirnoff if ((tp->t_state > TCPS_LISTEN) && 23039077f387SGleb Smirnoff (tp->t_state <= TCPS_CLOSING)) 23049077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 23059077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 23069077f387SGleb Smirnoff break; 23079077f387SGleb Smirnoff case TCP_KEEPINTVL: 23089077f387SGleb Smirnoff tp->t_keepintvl = ui; 23099077f387SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 23109077f387SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 23119077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 23129077f387SGleb Smirnoff TP_MAXIDLE(tp)); 23139077f387SGleb Smirnoff break; 23149077f387SGleb Smirnoff case TCP_KEEPINIT: 23159077f387SGleb Smirnoff tp->t_keepinit = ui; 23169077f387SGleb Smirnoff if (tp->t_state == TCPS_SYN_RECEIVED || 23179077f387SGleb Smirnoff tp->t_state == TCPS_SYN_SENT) 23189077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 23199077f387SGleb Smirnoff TP_KEEPINIT(tp)); 23209077f387SGleb Smirnoff break; 23219077f387SGleb Smirnoff } 232209fe6320SNavdeep Parhar goto unlock_and_done; 23239077f387SGleb Smirnoff 232485c05144SGleb Smirnoff case TCP_KEEPCNT: 232585c05144SGleb Smirnoff INP_WUNLOCK(inp); 232685c05144SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 232785c05144SGleb Smirnoff if (error) 232885c05144SGleb Smirnoff return (error); 232985c05144SGleb Smirnoff 233085c05144SGleb Smirnoff INP_WLOCK_RECHECK(inp); 233185c05144SGleb Smirnoff tp->t_keepcnt = ui; 233285c05144SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 233385c05144SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 233485c05144SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 233585c05144SGleb Smirnoff TP_MAXIDLE(tp)); 233685c05144SGleb Smirnoff goto unlock_and_done; 233785c05144SGleb Smirnoff 233886a996e6SHiren Panchasara #ifdef TCPPCAP 233986a996e6SHiren Panchasara case TCP_PCAP_OUT: 234086a996e6SHiren Panchasara case TCP_PCAP_IN: 234186a996e6SHiren Panchasara INP_WUNLOCK(inp); 234286a996e6SHiren Panchasara error = sooptcopyin(sopt, &optval, sizeof optval, 234386a996e6SHiren Panchasara sizeof optval); 234486a996e6SHiren Panchasara if (error) 234586a996e6SHiren Panchasara return (error); 234686a996e6SHiren Panchasara 234786a996e6SHiren Panchasara INP_WLOCK_RECHECK(inp); 234886a996e6SHiren Panchasara if (optval >= 0) 234986a996e6SHiren Panchasara tcp_pcap_set_sock_max(TCP_PCAP_OUT ? 235086a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts), 235186a996e6SHiren Panchasara optval); 235286a996e6SHiren Panchasara else 235386a996e6SHiren Panchasara error = EINVAL; 235486a996e6SHiren Panchasara goto unlock_and_done; 235586a996e6SHiren Panchasara #endif 235686a996e6SHiren Panchasara 2357c560df6fSPatrick Kelsey case TCP_FASTOPEN: { 2358c560df6fSPatrick Kelsey struct tcp_fastopen tfo_optval; 2359c560df6fSPatrick Kelsey 2360281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 2361c560df6fSPatrick Kelsey if (!V_tcp_fastopen_client_enable && 2362c560df6fSPatrick Kelsey !V_tcp_fastopen_server_enable) 2363281a0fd4SPatrick Kelsey return (EPERM); 2364281a0fd4SPatrick Kelsey 2365c560df6fSPatrick Kelsey error = sooptcopyin(sopt, &tfo_optval, 2366c560df6fSPatrick Kelsey sizeof(tfo_optval), sizeof(int)); 2367281a0fd4SPatrick Kelsey if (error) 2368281a0fd4SPatrick Kelsey return (error); 2369281a0fd4SPatrick Kelsey 2370281a0fd4SPatrick Kelsey INP_WLOCK_RECHECK(inp); 2371d442a657SMichael Tuexen if ((tp->t_state != TCPS_CLOSED) && 2372d442a657SMichael Tuexen (tp->t_state != TCPS_LISTEN)) { 2373d442a657SMichael Tuexen error = EINVAL; 2374d442a657SMichael Tuexen goto unlock_and_done; 2375d442a657SMichael Tuexen } 2376c560df6fSPatrick Kelsey if (tfo_optval.enable) { 2377c560df6fSPatrick Kelsey if (tp->t_state == TCPS_LISTEN) { 2378c560df6fSPatrick Kelsey if (!V_tcp_fastopen_server_enable) { 2379c560df6fSPatrick Kelsey error = EPERM; 2380c560df6fSPatrick Kelsey goto unlock_and_done; 2381c560df6fSPatrick Kelsey } 2382c560df6fSPatrick Kelsey 2383c560df6fSPatrick Kelsey if (tp->t_tfo_pending == NULL) 2384281a0fd4SPatrick Kelsey tp->t_tfo_pending = 2385281a0fd4SPatrick Kelsey tcp_fastopen_alloc_counter(); 2386c560df6fSPatrick Kelsey } else { 2387c560df6fSPatrick Kelsey /* 2388c560df6fSPatrick Kelsey * If a pre-shared key was provided, 2389c560df6fSPatrick Kelsey * stash it in the client cookie 2390c560df6fSPatrick Kelsey * field of the tcpcb for use during 2391c560df6fSPatrick Kelsey * connect. 2392c560df6fSPatrick Kelsey */ 2393c560df6fSPatrick Kelsey if (sopt->sopt_valsize == 2394c560df6fSPatrick Kelsey sizeof(tfo_optval)) { 2395c560df6fSPatrick Kelsey memcpy(tp->t_tfo_cookie.client, 2396c560df6fSPatrick Kelsey tfo_optval.psk, 2397c560df6fSPatrick Kelsey TCP_FASTOPEN_PSK_LEN); 2398c560df6fSPatrick Kelsey tp->t_tfo_client_cookie_len = 2399c560df6fSPatrick Kelsey TCP_FASTOPEN_PSK_LEN; 2400c560df6fSPatrick Kelsey } 2401c560df6fSPatrick Kelsey } 2402d442a657SMichael Tuexen tp->t_flags |= TF_FASTOPEN; 2403281a0fd4SPatrick Kelsey } else 2404281a0fd4SPatrick Kelsey tp->t_flags &= ~TF_FASTOPEN; 2405281a0fd4SPatrick Kelsey goto unlock_and_done; 2406c560df6fSPatrick Kelsey } 2407281a0fd4SPatrick Kelsey 2408e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX 24092529f56eSJonathan T. Looney case TCP_LOG: 24102529f56eSJonathan T. Looney INP_WUNLOCK(inp); 24112529f56eSJonathan T. Looney error = sooptcopyin(sopt, &optval, sizeof optval, 24122529f56eSJonathan T. Looney sizeof optval); 24132529f56eSJonathan T. Looney if (error) 24142529f56eSJonathan T. Looney return (error); 24152529f56eSJonathan T. Looney 24162529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 24172529f56eSJonathan T. Looney error = tcp_log_state_change(tp, optval); 24182529f56eSJonathan T. Looney goto unlock_and_done; 24192529f56eSJonathan T. Looney 24202529f56eSJonathan T. Looney case TCP_LOGBUF: 24212529f56eSJonathan T. Looney INP_WUNLOCK(inp); 24222529f56eSJonathan T. Looney error = EINVAL; 24232529f56eSJonathan T. Looney break; 24242529f56eSJonathan T. Looney 24252529f56eSJonathan T. Looney case TCP_LOGID: 24262529f56eSJonathan T. Looney INP_WUNLOCK(inp); 24272529f56eSJonathan T. Looney error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0); 24282529f56eSJonathan T. Looney if (error) 24292529f56eSJonathan T. Looney break; 24302529f56eSJonathan T. Looney buf[sopt->sopt_valsize] = '\0'; 24312529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 24322529f56eSJonathan T. Looney error = tcp_log_set_id(tp, buf); 24332529f56eSJonathan T. Looney /* tcp_log_set_id() unlocks the INP. */ 24342529f56eSJonathan T. Looney break; 24352529f56eSJonathan T. Looney 24362529f56eSJonathan T. Looney case TCP_LOGDUMP: 24372529f56eSJonathan T. Looney case TCP_LOGDUMPID: 24382529f56eSJonathan T. Looney INP_WUNLOCK(inp); 24392529f56eSJonathan T. Looney error = 24402529f56eSJonathan T. Looney sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0); 24412529f56eSJonathan T. Looney if (error) 24422529f56eSJonathan T. Looney break; 24432529f56eSJonathan T. Looney buf[sopt->sopt_valsize] = '\0'; 24442529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 24452529f56eSJonathan T. Looney if (sopt->sopt_name == TCP_LOGDUMP) { 24462529f56eSJonathan T. Looney error = tcp_log_dump_tp_logbuf(tp, buf, 24472529f56eSJonathan T. Looney M_WAITOK, true); 24482529f56eSJonathan T. Looney INP_WUNLOCK(inp); 24492529f56eSJonathan T. Looney } else { 24502529f56eSJonathan T. Looney tcp_log_dump_tp_bucket_logbufs(tp, buf); 24512529f56eSJonathan T. Looney /* 24522529f56eSJonathan T. Looney * tcp_log_dump_tp_bucket_logbufs() drops the 24532529f56eSJonathan T. Looney * INP lock. 24542529f56eSJonathan T. Looney */ 24552529f56eSJonathan T. Looney } 24562529f56eSJonathan T. Looney break; 2457e24e5683SJonathan T. Looney #endif 24582529f56eSJonathan T. Looney 2459df8bae1dSRodney W. Grimes default: 24608501a69cSRobert Watson INP_WUNLOCK(inp); 2461df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 2462df8bae1dSRodney W. Grimes break; 2463df8bae1dSRodney W. Grimes } 2464df8bae1dSRodney W. Grimes break; 2465df8bae1dSRodney W. Grimes 2466cfe8b629SGarrett Wollman case SOPT_GET: 24671e8f5ffaSRobert Watson tp = intotcpcb(inp); 2468cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2469fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 247088f6b043SBruce M Simpson case TCP_MD5SIG: 24718501a69cSRobert Watson INP_WUNLOCK(inp); 247297453e5eSClaudio Jeker if (!TCPMD5_ENABLED()) 2473fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 2474fcf59617SAndrey V. Elsukov error = TCPMD5_PCBCTL(inp, sopt); 24751cfd4b53SBruce M Simpson break; 2476265ed012SBruce M Simpson #endif 24771e8f5ffaSRobert Watson 2478df8bae1dSRodney W. Grimes case TCP_NODELAY: 2479cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NODELAY; 24808501a69cSRobert Watson INP_WUNLOCK(inp); 2481b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2482df8bae1dSRodney W. Grimes break; 2483df8bae1dSRodney W. Grimes case TCP_MAXSEG: 2484cfe8b629SGarrett Wollman optval = tp->t_maxseg; 24858501a69cSRobert Watson INP_WUNLOCK(inp); 2486b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2487df8bae1dSRodney W. Grimes break; 24889e644c23SMichael Tuexen case TCP_REMOTE_UDP_ENCAPS_PORT: 24899e644c23SMichael Tuexen optval = ntohs(tp->t_port); 24909e644c23SMichael Tuexen INP_WUNLOCK(inp); 24919e644c23SMichael Tuexen error = sooptcopyout(sopt, &optval, sizeof optval); 24929e644c23SMichael Tuexen break; 2493a0292f23SGarrett Wollman case TCP_NOOPT: 2494cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOOPT; 24958501a69cSRobert Watson INP_WUNLOCK(inp); 2496b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2497a0292f23SGarrett Wollman break; 2498a0292f23SGarrett Wollman case TCP_NOPUSH: 2499cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOPUSH; 25008501a69cSRobert Watson INP_WUNLOCK(inp); 2501b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2502b8af5dfaSRobert Watson break; 2503b8af5dfaSRobert Watson case TCP_INFO: 2504b8af5dfaSRobert Watson tcp_fill_info(tp, &ti); 25058501a69cSRobert Watson INP_WUNLOCK(inp); 2506b8af5dfaSRobert Watson error = sooptcopyout(sopt, &ti, sizeof ti); 2507a0292f23SGarrett Wollman break; 2508adc56f5aSEdward Tomasz Napierala case TCP_STATS: 2509adc56f5aSEdward Tomasz Napierala { 2510adc56f5aSEdward Tomasz Napierala #ifdef STATS 2511adc56f5aSEdward Tomasz Napierala int nheld; 2512adc56f5aSEdward Tomasz Napierala TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0; 2513adc56f5aSEdward Tomasz Napierala 2514adc56f5aSEdward Tomasz Napierala error = 0; 2515adc56f5aSEdward Tomasz Napierala socklen_t outsbsz = sopt->sopt_valsize; 2516adc56f5aSEdward Tomasz Napierala if (tp->t_stats == NULL) 2517adc56f5aSEdward Tomasz Napierala error = ENOENT; 2518adc56f5aSEdward Tomasz Napierala else if (outsbsz >= tp->t_stats->cursz) 2519adc56f5aSEdward Tomasz Napierala outsbsz = tp->t_stats->cursz; 2520adc56f5aSEdward Tomasz Napierala else if (outsbsz >= sizeof(struct statsblob)) 2521adc56f5aSEdward Tomasz Napierala outsbsz = sizeof(struct statsblob); 2522adc56f5aSEdward Tomasz Napierala else 2523adc56f5aSEdward Tomasz Napierala error = EINVAL; 2524adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2525adc56f5aSEdward Tomasz Napierala if (error) 2526adc56f5aSEdward Tomasz Napierala break; 2527adc56f5aSEdward Tomasz Napierala 2528adc56f5aSEdward Tomasz Napierala sbp = sopt->sopt_val; 2529adc56f5aSEdward Tomasz Napierala nheld = atop(round_page(((vm_offset_t)sbp) + 2530adc56f5aSEdward Tomasz Napierala (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp)); 2531adc56f5aSEdward Tomasz Napierala vm_page_t ma[nheld]; 2532adc56f5aSEdward Tomasz Napierala if (vm_fault_quick_hold_pages( 2533adc56f5aSEdward Tomasz Napierala &curproc->p_vmspace->vm_map, (vm_offset_t)sbp, 2534adc56f5aSEdward Tomasz Napierala outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma, 2535adc56f5aSEdward Tomasz Napierala nheld) < 0) { 2536adc56f5aSEdward Tomasz Napierala error = EFAULT; 2537adc56f5aSEdward Tomasz Napierala break; 2538adc56f5aSEdward Tomasz Napierala } 2539adc56f5aSEdward Tomasz Napierala 2540adc56f5aSEdward Tomasz Napierala if ((error = copyin_nofault(&(sbp->flags), &sbflags, 2541adc56f5aSEdward Tomasz Napierala SIZEOF_MEMBER(struct statsblob, flags)))) 2542adc56f5aSEdward Tomasz Napierala goto unhold; 2543adc56f5aSEdward Tomasz Napierala 2544adc56f5aSEdward Tomasz Napierala INP_WLOCK_RECHECK(inp); 2545adc56f5aSEdward Tomasz Napierala error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats, 2546adc56f5aSEdward Tomasz Napierala sbflags | SB_CLONE_USRDSTNOFAULT); 2547adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2548adc56f5aSEdward Tomasz Napierala sopt->sopt_valsize = outsbsz; 2549adc56f5aSEdward Tomasz Napierala unhold: 2550adc56f5aSEdward Tomasz Napierala vm_page_unhold_pages(ma, nheld); 2551adc56f5aSEdward Tomasz Napierala #else 2552adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2553adc56f5aSEdward Tomasz Napierala error = EOPNOTSUPP; 2554adc56f5aSEdward Tomasz Napierala #endif /* !STATS */ 2555adc56f5aSEdward Tomasz Napierala break; 2556adc56f5aSEdward Tomasz Napierala } 2557dbc42409SLawrence Stewart case TCP_CONGESTION: 2558af6fef3aSGleb Smirnoff len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); 2559dbc42409SLawrence Stewart INP_WUNLOCK(inp); 2560af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, buf, len + 1); 2561dbc42409SLawrence Stewart break; 256208af8aacSRandall Stewart case TCP_MAXUNACKTIME: 25632f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 25642f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 25652f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 25662f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 25672f3eb7f4SGleb Smirnoff switch (sopt->sopt_name) { 256808af8aacSRandall Stewart case TCP_MAXUNACKTIME: 256908af8aacSRandall Stewart ui = TP_MAXUNACKTIME(tp) / hz; 257008af8aacSRandall Stewart break; 25712f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 25725a17b6adSMichael Tuexen ui = TP_KEEPIDLE(tp) / hz; 25732f3eb7f4SGleb Smirnoff break; 25742f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 25755a17b6adSMichael Tuexen ui = TP_KEEPINTVL(tp) / hz; 25762f3eb7f4SGleb Smirnoff break; 25772f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 25785a17b6adSMichael Tuexen ui = TP_KEEPINIT(tp) / hz; 25792f3eb7f4SGleb Smirnoff break; 25802f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 25815a17b6adSMichael Tuexen ui = TP_KEEPCNT(tp); 25822f3eb7f4SGleb Smirnoff break; 25832f3eb7f4SGleb Smirnoff } 25842f3eb7f4SGleb Smirnoff INP_WUNLOCK(inp); 25852f3eb7f4SGleb Smirnoff error = sooptcopyout(sopt, &ui, sizeof(ui)); 25862f3eb7f4SGleb Smirnoff break; 258786a996e6SHiren Panchasara #ifdef TCPPCAP 258886a996e6SHiren Panchasara case TCP_PCAP_OUT: 258986a996e6SHiren Panchasara case TCP_PCAP_IN: 259086a996e6SHiren Panchasara optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ? 259186a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts)); 259286a996e6SHiren Panchasara INP_WUNLOCK(inp); 259386a996e6SHiren Panchasara error = sooptcopyout(sopt, &optval, sizeof optval); 259486a996e6SHiren Panchasara break; 259586a996e6SHiren Panchasara #endif 2596281a0fd4SPatrick Kelsey case TCP_FASTOPEN: 2597281a0fd4SPatrick Kelsey optval = tp->t_flags & TF_FASTOPEN; 2598281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 2599281a0fd4SPatrick Kelsey error = sooptcopyout(sopt, &optval, sizeof optval); 2600281a0fd4SPatrick Kelsey break; 2601e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX 26022529f56eSJonathan T. Looney case TCP_LOG: 26032529f56eSJonathan T. Looney optval = tp->t_logstate; 26042529f56eSJonathan T. Looney INP_WUNLOCK(inp); 26052529f56eSJonathan T. Looney error = sooptcopyout(sopt, &optval, sizeof(optval)); 26062529f56eSJonathan T. Looney break; 26072529f56eSJonathan T. Looney case TCP_LOGBUF: 26082529f56eSJonathan T. Looney /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */ 26092529f56eSJonathan T. Looney error = tcp_log_getlogbuf(sopt, tp); 26102529f56eSJonathan T. Looney break; 26112529f56eSJonathan T. Looney case TCP_LOGID: 26122529f56eSJonathan T. Looney len = tcp_log_get_id(tp, buf); 26132529f56eSJonathan T. Looney INP_WUNLOCK(inp); 26142529f56eSJonathan T. Looney error = sooptcopyout(sopt, buf, len + 1); 26152529f56eSJonathan T. Looney break; 26162529f56eSJonathan T. Looney case TCP_LOGDUMP: 26172529f56eSJonathan T. Looney case TCP_LOGDUMPID: 26182529f56eSJonathan T. Looney INP_WUNLOCK(inp); 26192529f56eSJonathan T. Looney error = EINVAL; 26202529f56eSJonathan T. Looney break; 2621e24e5683SJonathan T. Looney #endif 2622b2e60773SJohn Baldwin #ifdef KERN_TLS 2623b2e60773SJohn Baldwin case TCP_TXTLS_MODE: 2624fd7daa72SMichael Tuexen error = ktls_get_tx_mode(so, &optval); 2625b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2626bf256782SMark Johnston if (error == 0) 2627bf256782SMark Johnston error = sooptcopyout(sopt, &optval, 2628bf256782SMark Johnston sizeof(optval)); 2629b2e60773SJohn Baldwin break; 2630f1f93475SJohn Baldwin case TCP_RXTLS_MODE: 2631fd7daa72SMichael Tuexen error = ktls_get_rx_mode(so, &optval); 2632f1f93475SJohn Baldwin INP_WUNLOCK(inp); 2633bf256782SMark Johnston if (error == 0) 2634bf256782SMark Johnston error = sooptcopyout(sopt, &optval, 2635bf256782SMark Johnston sizeof(optval)); 2636f1f93475SJohn Baldwin break; 2637b2e60773SJohn Baldwin #endif 26380471a8c7SRichard Scheffenegger case TCP_LRD: 26390471a8c7SRichard Scheffenegger optval = tp->t_flags & TF_LRD; 26400471a8c7SRichard Scheffenegger INP_WUNLOCK(inp); 26410471a8c7SRichard Scheffenegger error = sooptcopyout(sopt, &optval, sizeof optval); 26420471a8c7SRichard Scheffenegger break; 2643df8bae1dSRodney W. Grimes default: 26448501a69cSRobert Watson INP_WUNLOCK(inp); 2645df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 2646df8bae1dSRodney W. Grimes break; 2647df8bae1dSRodney W. Grimes } 2648df8bae1dSRodney W. Grimes break; 2649df8bae1dSRodney W. Grimes } 2650df8bae1dSRodney W. Grimes return (error); 2651df8bae1dSRodney W. Grimes } 26528501a69cSRobert Watson #undef INP_WLOCK_RECHECK 2653bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP 2654df8bae1dSRodney W. Grimes 265526e30fbbSDavid Greenman /* 2656df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 2657df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 2658df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 2659df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 2660df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 2661df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 2662df8bae1dSRodney W. Grimes */ 2663623dce13SRobert Watson static void 2664ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp) 2665df8bae1dSRodney W. Grimes { 26669eb0e832SGleb Smirnoff struct inpcb *inp = tptoinpcb(tp); 26679eb0e832SGleb Smirnoff struct socket *so = tptosocket(tp); 2668e6e0b5ffSRobert Watson 266997a95ee1SGleb Smirnoff NET_EPOCH_ASSERT(); 26708501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2671df8bae1dSRodney W. Grimes 2672623dce13SRobert Watson /* 2673623dce13SRobert Watson * Neither tcp_close() nor tcp_drop() should return NULL, as the 2674623dce13SRobert Watson * socket is still open. 2675623dce13SRobert Watson */ 26768db239dcSMichael Tuexen if (tp->t_state < TCPS_ESTABLISHED && 26778db239dcSMichael Tuexen !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) { 2678df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2679623dce13SRobert Watson KASSERT(tp != NULL, 2680623dce13SRobert Watson ("tcp_disconnect: tcp_close() returned NULL")); 2681623dce13SRobert Watson } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 2682243917feSSeigo Tanimura tp = tcp_drop(tp, 0); 2683623dce13SRobert Watson KASSERT(tp != NULL, 2684623dce13SRobert Watson ("tcp_disconnect: tcp_drop() returned NULL")); 2685623dce13SRobert Watson } else { 2686df8bae1dSRodney W. Grimes soisdisconnecting(so); 2687df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 2688623dce13SRobert Watson tcp_usrclosed(tp); 2689ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 2690f64dc2abSGleb Smirnoff /* Ignore stack's drop request, we already at it. */ 2691f64dc2abSGleb Smirnoff (void)tcp_output_nodrop(tp); 2692df8bae1dSRodney W. Grimes } 2693df8bae1dSRodney W. Grimes } 2694df8bae1dSRodney W. Grimes 2695df8bae1dSRodney W. Grimes /* 2696df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 2697df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 2698df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 2699df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 2700df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 2701df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 2702df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 2703df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 2704df8bae1dSRodney W. Grimes */ 2705623dce13SRobert Watson static void 2706ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp) 2707df8bae1dSRodney W. Grimes { 2708df8bae1dSRodney W. Grimes 270997a95ee1SGleb Smirnoff NET_EPOCH_ASSERT(); 27109eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(tptoinpcb(tp)); 2711e6e0b5ffSRobert Watson 2712df8bae1dSRodney W. Grimes switch (tp->t_state) { 2713df8bae1dSRodney W. Grimes case TCPS_LISTEN: 271409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 271509fe6320SNavdeep Parhar tcp_offload_listen_stop(tp); 271609fe6320SNavdeep Parhar #endif 2717550e9d42SHiren Panchasara tcp_state_change(tp, TCPS_CLOSED); 2718bc65987aSKip Macy /* FALLTHROUGH */ 2719bc65987aSKip Macy case TCPS_CLOSED: 2720df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2721623dce13SRobert Watson /* 2722623dce13SRobert Watson * tcp_close() should never return NULL here as the socket is 2723623dce13SRobert Watson * still open. 2724623dce13SRobert Watson */ 2725623dce13SRobert Watson KASSERT(tp != NULL, 2726623dce13SRobert Watson ("tcp_usrclosed: tcp_close() returned NULL")); 2727df8bae1dSRodney W. Grimes break; 2728df8bae1dSRodney W. Grimes 2729a0292f23SGarrett Wollman case TCPS_SYN_SENT: 2730df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2731a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 2732a0292f23SGarrett Wollman break; 2733a0292f23SGarrett Wollman 2734df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 273557f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2736df8bae1dSRodney W. Grimes break; 2737df8bae1dSRodney W. Grimes 2738df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 273957f60867SMark Johnston tcp_state_change(tp, TCPS_LAST_ACK); 2740df8bae1dSRodney W. Grimes break; 2741df8bae1dSRodney W. Grimes } 274208af8aacSRandall Stewart if (tp->t_acktime == 0) 274308af8aacSRandall Stewart tp->t_acktime = ticks; 2744abc7d910SRobert Watson if (tp->t_state >= TCPS_FIN_WAIT_2) { 27459eb0e832SGleb Smirnoff soisdisconnected(tptosocket(tp)); 2746abc7d910SRobert Watson /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 27477c72af87SMohan Srinivasan if (tp->t_state == TCPS_FIN_WAIT_2) { 27487c72af87SMohan Srinivasan int timeout; 27497c72af87SMohan Srinivasan 27507c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 27519077f387SGleb Smirnoff tcp_finwait2_timeout : TP_MAXIDLE(tp); 2752b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 2753b6239c4aSAndras Olah } 2754df8bae1dSRodney W. Grimes } 27557c72af87SMohan Srinivasan } 2756497057eeSRobert Watson 2757497057eeSRobert Watson #ifdef DDB 2758497057eeSRobert Watson static void 2759497057eeSRobert Watson db_print_indent(int indent) 2760497057eeSRobert Watson { 2761497057eeSRobert Watson int i; 2762497057eeSRobert Watson 2763497057eeSRobert Watson for (i = 0; i < indent; i++) 2764497057eeSRobert Watson db_printf(" "); 2765497057eeSRobert Watson } 2766497057eeSRobert Watson 2767497057eeSRobert Watson static void 2768497057eeSRobert Watson db_print_tstate(int t_state) 2769497057eeSRobert Watson { 2770497057eeSRobert Watson 2771497057eeSRobert Watson switch (t_state) { 2772497057eeSRobert Watson case TCPS_CLOSED: 2773497057eeSRobert Watson db_printf("TCPS_CLOSED"); 2774497057eeSRobert Watson return; 2775497057eeSRobert Watson 2776497057eeSRobert Watson case TCPS_LISTEN: 2777497057eeSRobert Watson db_printf("TCPS_LISTEN"); 2778497057eeSRobert Watson return; 2779497057eeSRobert Watson 2780497057eeSRobert Watson case TCPS_SYN_SENT: 2781497057eeSRobert Watson db_printf("TCPS_SYN_SENT"); 2782497057eeSRobert Watson return; 2783497057eeSRobert Watson 2784497057eeSRobert Watson case TCPS_SYN_RECEIVED: 2785497057eeSRobert Watson db_printf("TCPS_SYN_RECEIVED"); 2786497057eeSRobert Watson return; 2787497057eeSRobert Watson 2788497057eeSRobert Watson case TCPS_ESTABLISHED: 2789497057eeSRobert Watson db_printf("TCPS_ESTABLISHED"); 2790497057eeSRobert Watson return; 2791497057eeSRobert Watson 2792497057eeSRobert Watson case TCPS_CLOSE_WAIT: 2793497057eeSRobert Watson db_printf("TCPS_CLOSE_WAIT"); 2794497057eeSRobert Watson return; 2795497057eeSRobert Watson 2796497057eeSRobert Watson case TCPS_FIN_WAIT_1: 2797497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_1"); 2798497057eeSRobert Watson return; 2799497057eeSRobert Watson 2800497057eeSRobert Watson case TCPS_CLOSING: 2801497057eeSRobert Watson db_printf("TCPS_CLOSING"); 2802497057eeSRobert Watson return; 2803497057eeSRobert Watson 2804497057eeSRobert Watson case TCPS_LAST_ACK: 2805497057eeSRobert Watson db_printf("TCPS_LAST_ACK"); 2806497057eeSRobert Watson return; 2807497057eeSRobert Watson 2808497057eeSRobert Watson case TCPS_FIN_WAIT_2: 2809497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_2"); 2810497057eeSRobert Watson return; 2811497057eeSRobert Watson 2812497057eeSRobert Watson case TCPS_TIME_WAIT: 2813497057eeSRobert Watson db_printf("TCPS_TIME_WAIT"); 2814497057eeSRobert Watson return; 2815497057eeSRobert Watson 2816497057eeSRobert Watson default: 2817497057eeSRobert Watson db_printf("unknown"); 2818497057eeSRobert Watson return; 2819497057eeSRobert Watson } 2820497057eeSRobert Watson } 2821497057eeSRobert Watson 2822497057eeSRobert Watson static void 2823497057eeSRobert Watson db_print_tflags(u_int t_flags) 2824497057eeSRobert Watson { 2825497057eeSRobert Watson int comma; 2826497057eeSRobert Watson 2827497057eeSRobert Watson comma = 0; 2828497057eeSRobert Watson if (t_flags & TF_ACKNOW) { 2829497057eeSRobert Watson db_printf("%sTF_ACKNOW", comma ? ", " : ""); 2830497057eeSRobert Watson comma = 1; 2831497057eeSRobert Watson } 2832497057eeSRobert Watson if (t_flags & TF_DELACK) { 2833497057eeSRobert Watson db_printf("%sTF_DELACK", comma ? ", " : ""); 2834497057eeSRobert Watson comma = 1; 2835497057eeSRobert Watson } 2836497057eeSRobert Watson if (t_flags & TF_NODELAY) { 2837497057eeSRobert Watson db_printf("%sTF_NODELAY", comma ? ", " : ""); 2838497057eeSRobert Watson comma = 1; 2839497057eeSRobert Watson } 2840497057eeSRobert Watson if (t_flags & TF_NOOPT) { 2841497057eeSRobert Watson db_printf("%sTF_NOOPT", comma ? ", " : ""); 2842497057eeSRobert Watson comma = 1; 2843497057eeSRobert Watson } 2844497057eeSRobert Watson if (t_flags & TF_SENTFIN) { 2845497057eeSRobert Watson db_printf("%sTF_SENTFIN", comma ? ", " : ""); 2846497057eeSRobert Watson comma = 1; 2847497057eeSRobert Watson } 2848497057eeSRobert Watson if (t_flags & TF_REQ_SCALE) { 2849497057eeSRobert Watson db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 2850497057eeSRobert Watson comma = 1; 2851497057eeSRobert Watson } 2852497057eeSRobert Watson if (t_flags & TF_RCVD_SCALE) { 2853497057eeSRobert Watson db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 2854497057eeSRobert Watson comma = 1; 2855497057eeSRobert Watson } 2856497057eeSRobert Watson if (t_flags & TF_REQ_TSTMP) { 2857497057eeSRobert Watson db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 2858497057eeSRobert Watson comma = 1; 2859497057eeSRobert Watson } 2860497057eeSRobert Watson if (t_flags & TF_RCVD_TSTMP) { 2861497057eeSRobert Watson db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 2862497057eeSRobert Watson comma = 1; 2863497057eeSRobert Watson } 2864497057eeSRobert Watson if (t_flags & TF_SACK_PERMIT) { 2865497057eeSRobert Watson db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 2866497057eeSRobert Watson comma = 1; 2867497057eeSRobert Watson } 2868497057eeSRobert Watson if (t_flags & TF_NEEDSYN) { 2869497057eeSRobert Watson db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 2870497057eeSRobert Watson comma = 1; 2871497057eeSRobert Watson } 2872497057eeSRobert Watson if (t_flags & TF_NEEDFIN) { 2873497057eeSRobert Watson db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 2874497057eeSRobert Watson comma = 1; 2875497057eeSRobert Watson } 2876497057eeSRobert Watson if (t_flags & TF_NOPUSH) { 2877497057eeSRobert Watson db_printf("%sTF_NOPUSH", comma ? ", " : ""); 2878497057eeSRobert Watson comma = 1; 2879497057eeSRobert Watson } 28803f169c54SRichard Scheffenegger if (t_flags & TF_PREVVALID) { 28813f169c54SRichard Scheffenegger db_printf("%sTF_PREVVALID", comma ? ", " : ""); 28823f169c54SRichard Scheffenegger comma = 1; 28833f169c54SRichard Scheffenegger } 2884497057eeSRobert Watson if (t_flags & TF_MORETOCOME) { 2885497057eeSRobert Watson db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 2886497057eeSRobert Watson comma = 1; 2887497057eeSRobert Watson } 2888493105c2SGleb Smirnoff if (t_flags & TF_SONOTCONN) { 2889493105c2SGleb Smirnoff db_printf("%sTF_SONOTCONN", comma ? ", " : ""); 2890497057eeSRobert Watson comma = 1; 2891497057eeSRobert Watson } 2892497057eeSRobert Watson if (t_flags & TF_LASTIDLE) { 2893497057eeSRobert Watson db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 2894497057eeSRobert Watson comma = 1; 2895497057eeSRobert Watson } 2896497057eeSRobert Watson if (t_flags & TF_RXWIN0SENT) { 2897497057eeSRobert Watson db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 2898497057eeSRobert Watson comma = 1; 2899497057eeSRobert Watson } 2900497057eeSRobert Watson if (t_flags & TF_FASTRECOVERY) { 2901497057eeSRobert Watson db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 2902497057eeSRobert Watson comma = 1; 2903497057eeSRobert Watson } 2904dbc42409SLawrence Stewart if (t_flags & TF_CONGRECOVERY) { 2905dbc42409SLawrence Stewart db_printf("%sTF_CONGRECOVERY", comma ? ", " : ""); 2906dbc42409SLawrence Stewart comma = 1; 2907dbc42409SLawrence Stewart } 2908497057eeSRobert Watson if (t_flags & TF_WASFRECOVERY) { 2909497057eeSRobert Watson db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 2910497057eeSRobert Watson comma = 1; 2911497057eeSRobert Watson } 29123f169c54SRichard Scheffenegger if (t_flags & TF_WASCRECOVERY) { 29133f169c54SRichard Scheffenegger db_printf("%sTF_WASCRECOVERY", comma ? ", " : ""); 29143f169c54SRichard Scheffenegger comma = 1; 29153f169c54SRichard Scheffenegger } 2916497057eeSRobert Watson if (t_flags & TF_SIGNATURE) { 2917497057eeSRobert Watson db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 2918497057eeSRobert Watson comma = 1; 2919497057eeSRobert Watson } 2920497057eeSRobert Watson if (t_flags & TF_FORCEDATA) { 2921497057eeSRobert Watson db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 2922497057eeSRobert Watson comma = 1; 2923497057eeSRobert Watson } 2924497057eeSRobert Watson if (t_flags & TF_TSO) { 2925497057eeSRobert Watson db_printf("%sTF_TSO", comma ? ", " : ""); 2926497057eeSRobert Watson comma = 1; 2927497057eeSRobert Watson } 2928281a0fd4SPatrick Kelsey if (t_flags & TF_FASTOPEN) { 2929281a0fd4SPatrick Kelsey db_printf("%sTF_FASTOPEN", comma ? ", " : ""); 2930281a0fd4SPatrick Kelsey comma = 1; 2931281a0fd4SPatrick Kelsey } 2932497057eeSRobert Watson } 2933497057eeSRobert Watson 2934497057eeSRobert Watson static void 29353cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2) 29363cf38784SMichael Tuexen { 29373cf38784SMichael Tuexen int comma; 29383cf38784SMichael Tuexen 29393cf38784SMichael Tuexen comma = 0; 29403f169c54SRichard Scheffenegger if (t_flags2 & TF2_PLPMTU_BLACKHOLE) { 29413f169c54SRichard Scheffenegger db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : ""); 29423f169c54SRichard Scheffenegger comma = 1; 29433f169c54SRichard Scheffenegger } 29443f169c54SRichard Scheffenegger if (t_flags2 & TF2_PLPMTU_PMTUD) { 29453f169c54SRichard Scheffenegger db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : ""); 29463f169c54SRichard Scheffenegger comma = 1; 29473f169c54SRichard Scheffenegger } 29483f169c54SRichard Scheffenegger if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) { 29493f169c54SRichard Scheffenegger db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : ""); 29503f169c54SRichard Scheffenegger comma = 1; 29513f169c54SRichard Scheffenegger } 29523f169c54SRichard Scheffenegger if (t_flags2 & TF2_LOG_AUTO) { 29533f169c54SRichard Scheffenegger db_printf("%sTF2_LOG_AUTO", comma ? ", " : ""); 29543f169c54SRichard Scheffenegger comma = 1; 29553f169c54SRichard Scheffenegger } 29563f169c54SRichard Scheffenegger if (t_flags2 & TF2_DROP_AF_DATA) { 29573f169c54SRichard Scheffenegger db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : ""); 29583f169c54SRichard Scheffenegger comma = 1; 29593f169c54SRichard Scheffenegger } 29603cf38784SMichael Tuexen if (t_flags2 & TF2_ECN_PERMIT) { 29613cf38784SMichael Tuexen db_printf("%sTF2_ECN_PERMIT", comma ? ", " : ""); 29623cf38784SMichael Tuexen comma = 1; 29633cf38784SMichael Tuexen } 29643f169c54SRichard Scheffenegger if (t_flags2 & TF2_ECN_SND_CWR) { 29653f169c54SRichard Scheffenegger db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : ""); 29663f169c54SRichard Scheffenegger comma = 1; 29673f169c54SRichard Scheffenegger } 29683f169c54SRichard Scheffenegger if (t_flags2 & TF2_ECN_SND_ECE) { 29693f169c54SRichard Scheffenegger db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : ""); 29703f169c54SRichard Scheffenegger comma = 1; 29713f169c54SRichard Scheffenegger } 29723f169c54SRichard Scheffenegger if (t_flags2 & TF2_ACE_PERMIT) { 29733f169c54SRichard Scheffenegger db_printf("%sTF2_ACE_PERMIT", comma ? ", " : ""); 29743f169c54SRichard Scheffenegger comma = 1; 29753f169c54SRichard Scheffenegger } 29763f169c54SRichard Scheffenegger if (t_flags2 & TF2_FBYTES_COMPLETE) { 29773f169c54SRichard Scheffenegger db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : ""); 29783f169c54SRichard Scheffenegger comma = 1; 29793f169c54SRichard Scheffenegger } 29803cf38784SMichael Tuexen } 29813cf38784SMichael Tuexen 29823cf38784SMichael Tuexen static void 2983497057eeSRobert Watson db_print_toobflags(char t_oobflags) 2984497057eeSRobert Watson { 2985497057eeSRobert Watson int comma; 2986497057eeSRobert Watson 2987497057eeSRobert Watson comma = 0; 2988497057eeSRobert Watson if (t_oobflags & TCPOOB_HAVEDATA) { 2989497057eeSRobert Watson db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 2990497057eeSRobert Watson comma = 1; 2991497057eeSRobert Watson } 2992497057eeSRobert Watson if (t_oobflags & TCPOOB_HADDATA) { 2993497057eeSRobert Watson db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 2994497057eeSRobert Watson comma = 1; 2995497057eeSRobert Watson } 2996497057eeSRobert Watson } 2997497057eeSRobert Watson 2998497057eeSRobert Watson static void 2999497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 3000497057eeSRobert Watson { 3001497057eeSRobert Watson 3002497057eeSRobert Watson db_print_indent(indent); 3003497057eeSRobert Watson db_printf("%s at %p\n", name, tp); 3004497057eeSRobert Watson 3005497057eeSRobert Watson indent += 2; 3006497057eeSRobert Watson 3007497057eeSRobert Watson db_print_indent(indent); 3008497057eeSRobert Watson db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 3009c28440dbSRandall Stewart TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 3010497057eeSRobert Watson 3011497057eeSRobert Watson db_print_indent(indent); 3012446ccdd0SGleb Smirnoff db_printf("t_callout: %p t_timers: %p\n", 3013446ccdd0SGleb Smirnoff &tp->t_callout, &tp->t_timers); 3014497057eeSRobert Watson 3015497057eeSRobert Watson db_print_indent(indent); 3016497057eeSRobert Watson db_printf("t_state: %d (", tp->t_state); 3017497057eeSRobert Watson db_print_tstate(tp->t_state); 3018497057eeSRobert Watson db_printf(")\n"); 3019497057eeSRobert Watson 3020497057eeSRobert Watson db_print_indent(indent); 3021497057eeSRobert Watson db_printf("t_flags: 0x%x (", tp->t_flags); 3022497057eeSRobert Watson db_print_tflags(tp->t_flags); 3023497057eeSRobert Watson db_printf(")\n"); 3024497057eeSRobert Watson 3025497057eeSRobert Watson db_print_indent(indent); 30263cf38784SMichael Tuexen db_printf("t_flags2: 0x%x (", tp->t_flags2); 30273cf38784SMichael Tuexen db_print_tflags2(tp->t_flags2); 30283cf38784SMichael Tuexen db_printf(")\n"); 30293cf38784SMichael Tuexen 30303cf38784SMichael Tuexen db_print_indent(indent); 3031497057eeSRobert Watson db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n", 3032497057eeSRobert Watson tp->snd_una, tp->snd_max, tp->snd_nxt); 3033497057eeSRobert Watson 3034497057eeSRobert Watson db_print_indent(indent); 3035497057eeSRobert Watson db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 3036497057eeSRobert Watson tp->snd_up, tp->snd_wl1, tp->snd_wl2); 3037497057eeSRobert Watson 3038497057eeSRobert Watson db_print_indent(indent); 3039497057eeSRobert Watson db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 3040497057eeSRobert Watson tp->iss, tp->irs, tp->rcv_nxt); 3041497057eeSRobert Watson 3042497057eeSRobert Watson db_print_indent(indent); 30433ac12506SJonathan T. Looney db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n", 3044497057eeSRobert Watson tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 3045497057eeSRobert Watson 3046497057eeSRobert Watson db_print_indent(indent); 30473ac12506SJonathan T. Looney db_printf("snd_wnd: %u snd_cwnd: %u\n", 30481c18314dSAndre Oppermann tp->snd_wnd, tp->snd_cwnd); 3049497057eeSRobert Watson 3050497057eeSRobert Watson db_print_indent(indent); 30513ac12506SJonathan T. Looney db_printf("snd_ssthresh: %u snd_recover: " 30521c18314dSAndre Oppermann "0x%08x\n", tp->snd_ssthresh, tp->snd_recover); 3053497057eeSRobert Watson 3054497057eeSRobert Watson db_print_indent(indent); 30550c39d38dSGleb Smirnoff db_printf("t_rcvtime: %u t_startime: %u\n", 30560c39d38dSGleb Smirnoff tp->t_rcvtime, tp->t_starttime); 3057497057eeSRobert Watson 3058497057eeSRobert Watson db_print_indent(indent); 30591c18314dSAndre Oppermann db_printf("t_rttime: %u t_rtsq: 0x%08x\n", 30601c18314dSAndre Oppermann tp->t_rtttime, tp->t_rtseq); 3061497057eeSRobert Watson 3062497057eeSRobert Watson db_print_indent(indent); 30631c18314dSAndre Oppermann db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n", 30641c18314dSAndre Oppermann tp->t_rxtcur, tp->t_maxseg, tp->t_srtt); 3065497057eeSRobert Watson 3066497057eeSRobert Watson db_print_indent(indent); 3067bd4f9866SMichael Tuexen db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u\n", 3068bd4f9866SMichael Tuexen tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin); 3069497057eeSRobert Watson 3070497057eeSRobert Watson db_print_indent(indent); 3071*18b83b62SRichard Scheffenegger db_printf("t_rttupdated: %u max_sndwnd: %u t_softerror: %d\n", 3072497057eeSRobert Watson tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 3073497057eeSRobert Watson 3074497057eeSRobert Watson db_print_indent(indent); 3075497057eeSRobert Watson db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 3076497057eeSRobert Watson db_print_toobflags(tp->t_oobflags); 3077497057eeSRobert Watson db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 3078497057eeSRobert Watson 3079497057eeSRobert Watson db_print_indent(indent); 3080497057eeSRobert Watson db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 3081497057eeSRobert Watson tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 3082497057eeSRobert Watson 3083497057eeSRobert Watson db_print_indent(indent); 30849f78a87aSJohn Baldwin db_printf("ts_recent: %u ts_recent_age: %u\n", 30851a553740SAndre Oppermann tp->ts_recent, tp->ts_recent_age); 3086497057eeSRobert Watson 3087497057eeSRobert Watson db_print_indent(indent); 3088497057eeSRobert Watson db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 30893ac12506SJonathan T. Looney "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 3090497057eeSRobert Watson 3091497057eeSRobert Watson db_print_indent(indent); 30923ac12506SJonathan T. Looney db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x " 30939f78a87aSJohn Baldwin "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 3094497057eeSRobert Watson tp->snd_recover_prev, tp->t_badrxtwin); 3095497057eeSRobert Watson 3096497057eeSRobert Watson db_print_indent(indent); 30973529149eSAndre Oppermann db_printf("snd_numholes: %d snd_holes first: %p\n", 30983529149eSAndre Oppermann tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 3099497057eeSRobert Watson 3100497057eeSRobert Watson db_print_indent(indent); 3101a3574665SMichael Tuexen db_printf("snd_fack: 0x%08x rcv_numsacks: %d\n", 3102a3574665SMichael Tuexen tp->snd_fack, tp->rcv_numsacks); 3103497057eeSRobert Watson 3104497057eeSRobert Watson /* Skip sackblks, sackhint. */ 3105497057eeSRobert Watson 3106497057eeSRobert Watson db_print_indent(indent); 3107497057eeSRobert Watson db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 3108497057eeSRobert Watson tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 3109497057eeSRobert Watson } 3110497057eeSRobert Watson 3111497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 3112497057eeSRobert Watson { 3113497057eeSRobert Watson struct tcpcb *tp; 3114497057eeSRobert Watson 3115497057eeSRobert Watson if (!have_addr) { 3116497057eeSRobert Watson db_printf("usage: show tcpcb <addr>\n"); 3117497057eeSRobert Watson return; 3118497057eeSRobert Watson } 3119497057eeSRobert Watson tp = (struct tcpcb *)addr; 3120497057eeSRobert Watson 3121497057eeSRobert Watson db_print_tcpcb(tp, "tcpcb", 0); 3122497057eeSRobert Watson } 3123497057eeSRobert Watson #endif 3124