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 #include "opt_tcpdebug.h" 490cc12cc5SJoerg Wunsch 50df8bae1dSRodney W. Grimes #include <sys/param.h> 51df8bae1dSRodney W. Grimes #include <sys/systm.h> 52adc56f5aSEdward Tomasz Napierala #include <sys/arb.h> 539077f387SGleb Smirnoff #include <sys/limits.h> 54f76fcf6dSJeffrey Hsu #include <sys/malloc.h> 5555bceb1eSRandall Stewart #include <sys/refcount.h> 56c7a82f90SGarrett Wollman #include <sys/kernel.h> 57b2e60773SJohn Baldwin #include <sys/ktls.h> 58adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h> 5998163b98SPoul-Henning Kamp #include <sys/sysctl.h> 60df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 61fb59c426SYoshinobu Inoue #ifdef INET6 62fb59c426SYoshinobu Inoue #include <sys/domain.h> 63fb59c426SYoshinobu Inoue #endif /* INET6 */ 64df8bae1dSRodney W. Grimes #include <sys/socket.h> 65df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 66df8bae1dSRodney W. Grimes #include <sys/protosw.h> 6791421ba2SRobert Watson #include <sys/proc.h> 6891421ba2SRobert Watson #include <sys/jail.h> 69adc56f5aSEdward Tomasz Napierala #include <sys/stats.h> 70df8bae1dSRodney W. Grimes 71497057eeSRobert Watson #ifdef DDB 72497057eeSRobert Watson #include <ddb/ddb.h> 73497057eeSRobert Watson #endif 74497057eeSRobert Watson 75df8bae1dSRodney W. Grimes #include <net/if.h> 7676039bc8SGleb Smirnoff #include <net/if_var.h> 77df8bae1dSRodney W. Grimes #include <net/route.h> 78530c0060SRobert Watson #include <net/vnet.h> 79df8bae1dSRodney W. Grimes 80df8bae1dSRodney W. Grimes #include <netinet/in.h> 815d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h> 82df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 83b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h> 84b5e8ce9fSBruce Evans #include <netinet/in_var.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 105610ee2f9SDavid Greenman #ifdef TCPDEBUG 106df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 107610ee2f9SDavid Greenman #endif 10809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 109bc65987aSKip Macy #include <netinet/tcp_offload.h> 11009fe6320SNavdeep Parhar #endif 111fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 112df8bae1dSRodney W. Grimes 113adc56f5aSEdward Tomasz Napierala #include <vm/vm.h> 114adc56f5aSEdward Tomasz Napierala #include <vm/vm_param.h> 115adc56f5aSEdward Tomasz Napierala #include <vm/pmap.h> 116adc56f5aSEdward Tomasz Napierala #include <vm/vm_extern.h> 117adc56f5aSEdward Tomasz Napierala #include <vm/vm_map.h> 118adc56f5aSEdward Tomasz Napierala #include <vm/vm_page.h> 119adc56f5aSEdward Tomasz Napierala 120df8bae1dSRodney W. Grimes /* 121df8bae1dSRodney W. Grimes * TCP protocol interface to socket abstraction. 122df8bae1dSRodney W. Grimes */ 123b287c6c7SBjoern A. Zeeb #ifdef INET 1244d77a549SAlfred Perlstein static int tcp_connect(struct tcpcb *, struct sockaddr *, 1254d77a549SAlfred Perlstein struct thread *td); 126b287c6c7SBjoern A. Zeeb #endif /* INET */ 127fb59c426SYoshinobu Inoue #ifdef INET6 1284d77a549SAlfred Perlstein static int tcp6_connect(struct tcpcb *, struct sockaddr *, 1294d77a549SAlfred Perlstein struct thread *td); 130fb59c426SYoshinobu Inoue #endif /* INET6 */ 131623dce13SRobert Watson static void tcp_disconnect(struct tcpcb *); 132623dce13SRobert Watson static void tcp_usrclosed(struct tcpcb *); 133b8af5dfaSRobert Watson static void tcp_fill_info(struct tcpcb *, struct tcp_info *); 1342c37256eSGarrett Wollman 135d3b6c96bSRandall Stewart static int tcp_pru_options_support(struct tcpcb *tp, int flags); 136d3b6c96bSRandall Stewart 1372c37256eSGarrett Wollman #ifdef TCPDEBUG 1381db24ffbSJonathan Lemon #define TCPDEBUG0 int ostate = 0 1392c37256eSGarrett Wollman #define TCPDEBUG1() ostate = tp ? tp->t_state : 0 1404cc20ab1SSeigo Tanimura #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 1414cc20ab1SSeigo Tanimura tcp_trace(TA_USER, ostate, tp, 0, 0, req) 1422c37256eSGarrett Wollman #else 1432c37256eSGarrett Wollman #define TCPDEBUG0 1442c37256eSGarrett Wollman #define TCPDEBUG1() 1452c37256eSGarrett Wollman #define TCPDEBUG2(req) 1462c37256eSGarrett Wollman #endif 1472c37256eSGarrett Wollman 1482c37256eSGarrett Wollman /* 14925102351SMike Karels * tcp_require_unique port requires a globally-unique source port for each 15025102351SMike Karels * outgoing connection. The default is to require the 4-tuple to be unique. 15125102351SMike Karels */ 15225102351SMike Karels VNET_DEFINE(int, tcp_require_unique_port) = 0; 15325102351SMike Karels SYSCTL_INT(_net_inet_tcp, OID_AUTO, require_unique_port, 15425102351SMike Karels CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_require_unique_port), 0, 15525102351SMike Karels "Require globally-unique ephemeral port for outgoing connections"); 15625102351SMike Karels #define V_tcp_require_unique_port VNET(tcp_require_unique_port) 15725102351SMike Karels 15825102351SMike Karels /* 1592c37256eSGarrett Wollman * TCP attaches to socket via pru_attach(), reserving space, 1602c37256eSGarrett Wollman * and an internet control block. 1612c37256eSGarrett Wollman */ 1622c37256eSGarrett Wollman static int 163b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td) 1642c37256eSGarrett Wollman { 165f76fcf6dSJeffrey Hsu struct inpcb *inp; 166623dce13SRobert Watson struct tcpcb *tp = NULL; 167623dce13SRobert Watson int error; 1682c37256eSGarrett Wollman TCPDEBUG0; 1692c37256eSGarrett Wollman 170623dce13SRobert Watson inp = sotoinpcb(so); 171623dce13SRobert Watson KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL")); 1722c37256eSGarrett Wollman TCPDEBUG1(); 1732c37256eSGarrett Wollman 1740f6385e7SGleb Smirnoff if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1750f6385e7SGleb Smirnoff error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace); 1762c37256eSGarrett Wollman if (error) 1772c37256eSGarrett Wollman goto out; 1780f6385e7SGleb Smirnoff } 1792c37256eSGarrett Wollman 1800f6385e7SGleb Smirnoff so->so_rcv.sb_flags |= SB_AUTOSIZE; 1810f6385e7SGleb Smirnoff so->so_snd.sb_flags |= SB_AUTOSIZE; 1820f6385e7SGleb Smirnoff error = in_pcballoc(so, &V_tcbinfo); 1837669c586SGleb Smirnoff if (error) 1840f6385e7SGleb Smirnoff goto out; 1850f6385e7SGleb Smirnoff inp = sotoinpcb(so); 1860f6385e7SGleb Smirnoff #ifdef INET6 1870f6385e7SGleb Smirnoff if (inp->inp_vflag & INP_IPV6PROTO) { 1880f6385e7SGleb Smirnoff inp->inp_vflag |= INP_IPV6; 1890f6385e7SGleb Smirnoff if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 1900f6385e7SGleb Smirnoff inp->inp_vflag |= INP_IPV4; 1910f6385e7SGleb Smirnoff inp->in6p_hops = -1; /* use kernel default */ 1920f6385e7SGleb Smirnoff } 1930f6385e7SGleb Smirnoff else 1940f6385e7SGleb Smirnoff #endif 1950f6385e7SGleb Smirnoff inp->inp_vflag |= INP_IPV4; 1960f6385e7SGleb Smirnoff tp = tcp_newtcpcb(inp); 1970f6385e7SGleb Smirnoff if (tp == NULL) { 1987669c586SGleb Smirnoff error = ENOBUFS; 1990f6385e7SGleb Smirnoff in_pcbdetach(inp); 2000f6385e7SGleb Smirnoff in_pcbfree(inp); 2010f6385e7SGleb Smirnoff goto out; 2020f6385e7SGleb Smirnoff } 2030f6385e7SGleb Smirnoff tp->t_state = TCPS_CLOSED; 2040f6385e7SGleb Smirnoff INP_WUNLOCK(inp); 2050f6385e7SGleb Smirnoff TCPSTATES_INC(TCPS_CLOSED); 2062c37256eSGarrett Wollman out: 2072c37256eSGarrett Wollman TCPDEBUG2(PRU_ATTACH); 2085d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ATTACH); 2090f6385e7SGleb Smirnoff return (error); 2102c37256eSGarrett Wollman } 2112c37256eSGarrett Wollman 2122c37256eSGarrett Wollman /* 2133fed74e9SGleb Smirnoff * tcp_usr_detach is called when the socket layer loses its final reference 214a152f8a3SRobert Watson * to the socket, be it a file descriptor reference, a reference from TCP, 215a152f8a3SRobert Watson * etc. At this point, there is only one case in which we will keep around 216a152f8a3SRobert Watson * inpcb state: time wait. 2172c37256eSGarrett Wollman */ 218bc725eafSRobert Watson static void 2193fed74e9SGleb Smirnoff tcp_usr_detach(struct socket *so) 2202c37256eSGarrett Wollman { 2213fed74e9SGleb Smirnoff struct inpcb *inp; 2222c37256eSGarrett Wollman struct tcpcb *tp; 2232c37256eSGarrett Wollman 2243fed74e9SGleb Smirnoff inp = sotoinpcb(so); 2253fed74e9SGleb Smirnoff KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 2263fed74e9SGleb Smirnoff INP_WLOCK(inp); 2273fed74e9SGleb Smirnoff KASSERT(so->so_pcb == inp && inp->inp_socket == so, 2283fed74e9SGleb Smirnoff ("%s: socket %p inp %p mismatch", __func__, so, inp)); 229953b5606SRobert Watson 230a152f8a3SRobert Watson tp = intotcpcb(inp); 231a152f8a3SRobert Watson 232ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 233623dce13SRobert Watson /* 234a152f8a3SRobert Watson * There are two cases to handle: one in which the time wait 235a152f8a3SRobert Watson * state is being discarded (INP_DROPPED), and one in which 236a152f8a3SRobert Watson * this connection will remain in timewait. In the former, 237a152f8a3SRobert Watson * it is time to discard all state (except tcptw, which has 238a152f8a3SRobert Watson * already been discarded by the timewait close code, which 239a152f8a3SRobert Watson * should be further up the call stack somewhere). In the 240a152f8a3SRobert Watson * latter case, we detach from the socket, but leave the pcb 241a152f8a3SRobert Watson * present until timewait ends. 242623dce13SRobert Watson * 243a152f8a3SRobert Watson * XXXRW: Would it be cleaner to free the tcptw here? 244cea40c48SJulien Charbon * 245cea40c48SJulien Charbon * Astute question indeed, from twtcp perspective there are 246dd388cfdSGleb Smirnoff * four cases to consider: 247cea40c48SJulien Charbon * 2483fed74e9SGleb Smirnoff * #1 tcp_usr_detach is called at tcptw creation time by 249cea40c48SJulien Charbon * tcp_twstart, then do not discard the newly created tcptw 250cea40c48SJulien Charbon * and leave inpcb present until timewait ends 2513fed74e9SGleb Smirnoff * #2 tcp_usr_detach is called at tcptw creation time by 252dd388cfdSGleb Smirnoff * tcp_twstart, but connection is local and tw will be 253dd388cfdSGleb Smirnoff * discarded immediately 2543fed74e9SGleb Smirnoff * #3 tcp_usr_detach is called at timewait end (or reuse) by 255cea40c48SJulien Charbon * tcp_twclose, then the tcptw has already been discarded 256ff9b006dSJulien Charbon * (or reused) and inpcb is freed here 2573fed74e9SGleb Smirnoff * #4 tcp_usr_detach is called() after timewait ends (or reuse) 258cea40c48SJulien Charbon * (e.g. by soclose), then tcptw has already been discarded 259ff9b006dSJulien Charbon * (or reused) and inpcb is freed here 260cea40c48SJulien Charbon * 261cea40c48SJulien Charbon * In all three cases the tcptw should not be freed here. 262623dce13SRobert Watson */ 263ad71fe3cSRobert Watson if (inp->inp_flags & INP_DROPPED) { 264ef396441SGleb Smirnoff KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && " 265ef396441SGleb Smirnoff "INP_DROPPED && tp != NULL")); 266623dce13SRobert Watson in_pcbdetach(inp); 2670206cdb8SBjoern A. Zeeb in_pcbfree(inp); 2680206cdb8SBjoern A. Zeeb } else { 269623dce13SRobert Watson in_pcbdetach(inp); 2708501a69cSRobert Watson INP_WUNLOCK(inp); 271623dce13SRobert Watson } 272623dce13SRobert Watson } else { 273e6e65783SRobert Watson /* 274a152f8a3SRobert Watson * If the connection is not in timewait, we consider two 275a152f8a3SRobert Watson * two conditions: one in which no further processing is 276a152f8a3SRobert Watson * necessary (dropped || embryonic), and one in which TCP is 277a152f8a3SRobert Watson * not yet done, but no longer requires the socket, so the 278a152f8a3SRobert Watson * pcb will persist for the time being. 279a152f8a3SRobert Watson * 280a152f8a3SRobert Watson * XXXRW: Does the second case still occur? 281e6e65783SRobert Watson */ 282ad71fe3cSRobert Watson if (inp->inp_flags & INP_DROPPED || 283623dce13SRobert Watson tp->t_state < TCPS_SYN_SENT) { 284623dce13SRobert Watson tcp_discardcb(tp); 285623dce13SRobert Watson in_pcbdetach(inp); 2860206cdb8SBjoern A. Zeeb in_pcbfree(inp); 287db3cee51SNavdeep Parhar } else { 288a152f8a3SRobert Watson in_pcbdetach(inp); 289db3cee51SNavdeep Parhar INP_WUNLOCK(inp); 290db3cee51SNavdeep Parhar } 291623dce13SRobert Watson } 292623dce13SRobert Watson } 293c78cbc7bSRobert Watson 294b287c6c7SBjoern A. Zeeb #ifdef INET 2952c37256eSGarrett Wollman /* 2962c37256eSGarrett Wollman * Give the socket an address. 2972c37256eSGarrett Wollman */ 2982c37256eSGarrett Wollman static int 299b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 3002c37256eSGarrett Wollman { 3012c37256eSGarrett Wollman int error = 0; 302f76fcf6dSJeffrey Hsu struct inpcb *inp; 303623dce13SRobert Watson struct tcpcb *tp = NULL; 3042c37256eSGarrett Wollman struct sockaddr_in *sinp; 3052c37256eSGarrett Wollman 30652710de1SPawel Jakub Dawidek sinp = (struct sockaddr_in *)nam; 307f96603b5SMark Johnston if (nam->sa_family != AF_INET) { 308f96603b5SMark Johnston /* 309f96603b5SMark Johnston * Preserve compatibility with old programs. 310f96603b5SMark Johnston */ 311f96603b5SMark Johnston if (nam->sa_family != AF_UNSPEC || 3123f1f6b6eSMichael Tuexen nam->sa_len < offsetof(struct sockaddr_in, sin_zero) || 313f96603b5SMark Johnston sinp->sin_addr.s_addr != INADDR_ANY) 314f161d294SMark Johnston return (EAFNOSUPPORT); 315f96603b5SMark Johnston nam->sa_family = AF_INET; 316f96603b5SMark Johnston } 31752710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof(*sinp)) 31852710de1SPawel Jakub Dawidek return (EINVAL); 319f161d294SMark Johnston 3202c37256eSGarrett Wollman /* 3212c37256eSGarrett Wollman * Must check for multicast addresses and disallow binding 3222c37256eSGarrett Wollman * to them. 3232c37256eSGarrett Wollman */ 324f161d294SMark Johnston if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 32552710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 32652710de1SPawel Jakub Dawidek 327623dce13SRobert Watson TCPDEBUG0; 328623dce13SRobert Watson inp = sotoinpcb(so); 329623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 3308501a69cSRobert Watson INP_WLOCK(inp); 331ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 332623dce13SRobert Watson error = EINVAL; 3332c37256eSGarrett Wollman goto out; 334623dce13SRobert Watson } 335623dce13SRobert Watson tp = intotcpcb(inp); 336623dce13SRobert Watson TCPDEBUG1(); 337fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 338623dce13SRobert Watson error = in_pcbbind(inp, nam, td->td_ucred); 339fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 340623dce13SRobert Watson out: 341623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 3425d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 3438501a69cSRobert Watson INP_WUNLOCK(inp); 344623dce13SRobert Watson 345623dce13SRobert Watson return (error); 3462c37256eSGarrett Wollman } 347b287c6c7SBjoern A. Zeeb #endif /* INET */ 3482c37256eSGarrett Wollman 349fb59c426SYoshinobu Inoue #ifdef INET6 350fb59c426SYoshinobu Inoue static int 351b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 352fb59c426SYoshinobu Inoue { 353fb59c426SYoshinobu Inoue int error = 0; 354f76fcf6dSJeffrey Hsu struct inpcb *inp; 355623dce13SRobert Watson struct tcpcb *tp = NULL; 3560ecd976eSBjoern A. Zeeb struct sockaddr_in6 *sin6; 3574a91aa8fSMichael Tuexen u_char vflagsav; 358fb59c426SYoshinobu Inoue 3590ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 360f161d294SMark Johnston if (nam->sa_family != AF_INET6) 361f161d294SMark Johnston return (EAFNOSUPPORT); 3620ecd976eSBjoern A. Zeeb if (nam->sa_len != sizeof(*sin6)) 36352710de1SPawel Jakub Dawidek return (EINVAL); 364f161d294SMark Johnston 365fb59c426SYoshinobu Inoue /* 366fb59c426SYoshinobu Inoue * Must check for multicast addresses and disallow binding 367fb59c426SYoshinobu Inoue * to them. 368fb59c426SYoshinobu Inoue */ 369f161d294SMark Johnston if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 37052710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 37152710de1SPawel Jakub Dawidek 372623dce13SRobert Watson TCPDEBUG0; 373623dce13SRobert Watson inp = sotoinpcb(so); 374623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 3758501a69cSRobert Watson INP_WLOCK(inp); 3764a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 377ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 378623dce13SRobert Watson error = EINVAL; 379623dce13SRobert Watson goto out; 380623dce13SRobert Watson } 381623dce13SRobert Watson tp = intotcpcb(inp); 382623dce13SRobert Watson TCPDEBUG1(); 383fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 384fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 385fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 386b287c6c7SBjoern A. Zeeb #ifdef INET 38766ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 3880ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 389fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 3900ecd976eSBjoern A. Zeeb else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 391fb59c426SYoshinobu Inoue struct sockaddr_in sin; 392fb59c426SYoshinobu Inoue 3930ecd976eSBjoern A. Zeeb in6_sin6_2_sin(&sin, sin6); 394888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 395888973f5SMichael Tuexen error = EAFNOSUPPORT; 396888973f5SMichael Tuexen INP_HASH_WUNLOCK(&V_tcbinfo); 397888973f5SMichael Tuexen goto out; 398888973f5SMichael Tuexen } 399fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 400fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 401b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)&sin, 402b0330ed9SPawel Jakub Dawidek td->td_ucred); 403fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 404fb59c426SYoshinobu Inoue goto out; 405fb59c426SYoshinobu Inoue } 406fb59c426SYoshinobu Inoue } 407b287c6c7SBjoern A. Zeeb #endif 408b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, nam, td->td_ucred); 409fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 410623dce13SRobert Watson out: 4114a91aa8fSMichael Tuexen if (error != 0) 4124a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 413623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 4145d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 4158501a69cSRobert Watson INP_WUNLOCK(inp); 416623dce13SRobert Watson return (error); 417fb59c426SYoshinobu Inoue } 418fb59c426SYoshinobu Inoue #endif /* INET6 */ 419fb59c426SYoshinobu Inoue 420b287c6c7SBjoern A. Zeeb #ifdef INET 4212c37256eSGarrett Wollman /* 4222c37256eSGarrett Wollman * Prepare to accept connections. 4232c37256eSGarrett Wollman */ 4242c37256eSGarrett Wollman static int 425d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td) 4262c37256eSGarrett Wollman { 4272c37256eSGarrett Wollman int error = 0; 428f76fcf6dSJeffrey Hsu struct inpcb *inp; 429623dce13SRobert Watson struct tcpcb *tp = NULL; 4302c37256eSGarrett Wollman 431623dce13SRobert Watson TCPDEBUG0; 432623dce13SRobert Watson inp = sotoinpcb(so); 433623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 4348501a69cSRobert Watson INP_WLOCK(inp); 435ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 436623dce13SRobert Watson error = EINVAL; 437623dce13SRobert Watson goto out; 438623dce13SRobert Watson } 439623dce13SRobert Watson tp = intotcpcb(inp); 440623dce13SRobert Watson TCPDEBUG1(); 4410daccb9cSRobert Watson SOCK_LOCK(so); 4420daccb9cSRobert Watson error = solisten_proto_check(so); 443bd4a39ccSMark Johnston if (error != 0) { 444bd4a39ccSMark Johnston SOCK_UNLOCK(so); 445bd4a39ccSMark Johnston goto out; 446bd4a39ccSMark Johnston } 447bd4a39ccSMark Johnston if (inp->inp_lport == 0) { 448fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 449bd4a39ccSMark Johnston error = in_pcbbind(inp, NULL, td->td_ucred); 450fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 451bd4a39ccSMark Johnston } 4520daccb9cSRobert Watson if (error == 0) { 45357f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 454d374e81eSRobert Watson solisten_proto(so, backlog); 45509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 45637cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 45709fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 45809fe6320SNavdeep Parhar #endif 459bd4a39ccSMark Johnston } else { 460bd4a39ccSMark Johnston solisten_proto_abort(so); 4610daccb9cSRobert Watson } 4620daccb9cSRobert Watson SOCK_UNLOCK(so); 463623dce13SRobert Watson 46468bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) 465281a0fd4SPatrick Kelsey tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 46618a75309SPatrick Kelsey 467623dce13SRobert Watson out: 468623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 4695d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 4708501a69cSRobert Watson INP_WUNLOCK(inp); 471623dce13SRobert Watson return (error); 4722c37256eSGarrett Wollman } 473b287c6c7SBjoern A. Zeeb #endif /* INET */ 4742c37256eSGarrett Wollman 475fb59c426SYoshinobu Inoue #ifdef INET6 476fb59c426SYoshinobu Inoue static int 477d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 478fb59c426SYoshinobu Inoue { 479fb59c426SYoshinobu Inoue int error = 0; 480f76fcf6dSJeffrey Hsu struct inpcb *inp; 481623dce13SRobert Watson struct tcpcb *tp = NULL; 4824a91aa8fSMichael Tuexen u_char vflagsav; 483fb59c426SYoshinobu Inoue 484623dce13SRobert Watson TCPDEBUG0; 485623dce13SRobert Watson inp = sotoinpcb(so); 486623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL")); 4878501a69cSRobert Watson INP_WLOCK(inp); 488ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 489623dce13SRobert Watson error = EINVAL; 490623dce13SRobert Watson goto out; 491623dce13SRobert Watson } 4924a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 493623dce13SRobert Watson tp = intotcpcb(inp); 494623dce13SRobert Watson TCPDEBUG1(); 4950daccb9cSRobert Watson SOCK_LOCK(so); 4960daccb9cSRobert Watson error = solisten_proto_check(so); 497bd4a39ccSMark Johnston if (error != 0) { 498bd4a39ccSMark Johnston SOCK_UNLOCK(so); 499bd4a39ccSMark Johnston goto out; 500bd4a39ccSMark Johnston } 501fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 502bd4a39ccSMark Johnston if (inp->inp_lport == 0) { 503fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 50466ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 505fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 506bd4a39ccSMark Johnston error = in6_pcbbind(inp, NULL, td->td_ucred); 507fb59c426SYoshinobu Inoue } 508fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 5090daccb9cSRobert Watson if (error == 0) { 51057f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 511d374e81eSRobert Watson solisten_proto(so, backlog); 51209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 51337cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 51409fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 51509fe6320SNavdeep Parhar #endif 516bd4a39ccSMark Johnston } else { 517bd4a39ccSMark Johnston solisten_proto_abort(so); 5180daccb9cSRobert Watson } 5190daccb9cSRobert Watson SOCK_UNLOCK(so); 520623dce13SRobert Watson 52168bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) 522281a0fd4SPatrick Kelsey tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 52318a75309SPatrick Kelsey 5244a91aa8fSMichael Tuexen if (error != 0) 5254a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 5264a91aa8fSMichael Tuexen 527623dce13SRobert Watson out: 528623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 5295d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 5308501a69cSRobert Watson INP_WUNLOCK(inp); 531623dce13SRobert Watson return (error); 532fb59c426SYoshinobu Inoue } 533fb59c426SYoshinobu Inoue #endif /* INET6 */ 534fb59c426SYoshinobu Inoue 535b287c6c7SBjoern A. Zeeb #ifdef INET 5362c37256eSGarrett Wollman /* 5372c37256eSGarrett Wollman * Initiate connection to peer. 5382c37256eSGarrett Wollman * Create a template for use in transmissions on this connection. 5392c37256eSGarrett Wollman * Enter SYN_SENT state, and mark socket as connecting. 5402c37256eSGarrett Wollman * Start keep-alive timer, and seed output sequence space. 5412c37256eSGarrett Wollman * Send initial segment on connection. 5422c37256eSGarrett Wollman */ 5432c37256eSGarrett Wollman static int 544b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 5452c37256eSGarrett Wollman { 546109eb549SGleb Smirnoff struct epoch_tracker et; 5472c37256eSGarrett Wollman int error = 0; 548f76fcf6dSJeffrey Hsu struct inpcb *inp; 549623dce13SRobert Watson struct tcpcb *tp = NULL; 5502c37256eSGarrett Wollman struct sockaddr_in *sinp; 5512c37256eSGarrett Wollman 55257bf258eSGarrett Wollman sinp = (struct sockaddr_in *)nam; 553f161d294SMark Johnston if (nam->sa_family != AF_INET) 554f161d294SMark Johnston return (EAFNOSUPPORT); 555e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sinp)) 556e29ef13fSDon Lewis return (EINVAL); 557f161d294SMark Johnston 55852710de1SPawel Jakub Dawidek /* 55952710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 56052710de1SPawel Jakub Dawidek */ 561f161d294SMark Johnston if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 56252710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 563f161d294SMark Johnston if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) 564f903a308SMichael Tuexen return (EACCES); 565b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0) 566b89e82ddSJamie Gritton return (error); 56775c13541SPoul-Henning Kamp 568623dce13SRobert Watson TCPDEBUG0; 569623dce13SRobert Watson inp = sotoinpcb(so); 570623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 5718501a69cSRobert Watson INP_WLOCK(inp); 572eb96dc33SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) { 573eb96dc33SJulien Charbon error = EADDRINUSE; 574eb96dc33SJulien Charbon goto out; 575eb96dc33SJulien Charbon } 576eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 577eb96dc33SJulien Charbon error = ECONNREFUSED; 578623dce13SRobert Watson goto out; 579623dce13SRobert Watson } 580bd4a39ccSMark Johnston if (SOLISTENING(so)) { 581bd4a39ccSMark Johnston error = EOPNOTSUPP; 582bd4a39ccSMark Johnston goto out; 583bd4a39ccSMark Johnston } 584623dce13SRobert Watson tp = intotcpcb(inp); 585623dce13SRobert Watson TCPDEBUG1(); 586c1604fe4SGleb Smirnoff NET_EPOCH_ENTER(et); 587b40ce416SJulian Elischer if ((error = tcp_connect(tp, nam, td)) != 0) 588c1604fe4SGleb Smirnoff goto out_in_epoch; 58909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 59009fe6320SNavdeep Parhar if (registered_toedevs > 0 && 59137cc0ecbSNavdeep 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 59509fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 596*40fa3e40SGleb Smirnoff error = tcp_output(tp); 597c1604fe4SGleb Smirnoff out_in_epoch: 598109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 599623dce13SRobert Watson out: 600623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 601e79cb051SGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CONNECT); 6028501a69cSRobert Watson INP_WUNLOCK(inp); 603623dce13SRobert Watson return (error); 6042c37256eSGarrett Wollman } 605b287c6c7SBjoern A. Zeeb #endif /* INET */ 6062c37256eSGarrett Wollman 607fb59c426SYoshinobu Inoue #ifdef INET6 608fb59c426SYoshinobu Inoue static int 609b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 610fb59c426SYoshinobu Inoue { 611109eb549SGleb Smirnoff struct epoch_tracker et; 612fb59c426SYoshinobu Inoue int error = 0; 613f76fcf6dSJeffrey Hsu struct inpcb *inp; 614623dce13SRobert Watson struct tcpcb *tp = NULL; 6150ecd976eSBjoern A. Zeeb struct sockaddr_in6 *sin6; 6164a91aa8fSMichael Tuexen u_int8_t incflagsav; 6174a91aa8fSMichael Tuexen u_char vflagsav; 618623dce13SRobert Watson 619623dce13SRobert Watson TCPDEBUG0; 620fb59c426SYoshinobu Inoue 6210ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 622f161d294SMark Johnston if (nam->sa_family != AF_INET6) 623f161d294SMark Johnston return (EAFNOSUPPORT); 6240ecd976eSBjoern A. Zeeb if (nam->sa_len != sizeof (*sin6)) 625e29ef13fSDon Lewis return (EINVAL); 626f161d294SMark Johnston 62752710de1SPawel Jakub Dawidek /* 62852710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 62952710de1SPawel Jakub Dawidek */ 630f161d294SMark Johnston if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 63152710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 632fb59c426SYoshinobu Inoue 633623dce13SRobert Watson inp = sotoinpcb(so); 634623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 6358501a69cSRobert Watson INP_WLOCK(inp); 6364a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 6374a91aa8fSMichael Tuexen incflagsav = inp->inp_inc.inc_flags; 638eb96dc33SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) { 639eb96dc33SJulien Charbon error = EADDRINUSE; 640eb96dc33SJulien Charbon goto out; 641eb96dc33SJulien Charbon } 642eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 643eb96dc33SJulien Charbon error = ECONNREFUSED; 644623dce13SRobert Watson goto out; 645623dce13SRobert Watson } 646bd4a39ccSMark Johnston if (SOLISTENING(so)) { 647bd4a39ccSMark Johnston error = EINVAL; 648bd4a39ccSMark Johnston goto out; 649bd4a39ccSMark Johnston } 650623dce13SRobert Watson tp = intotcpcb(inp); 651623dce13SRobert Watson TCPDEBUG1(); 652b287c6c7SBjoern A. Zeeb #ifdef INET 653fa046d87SRobert Watson /* 654fa046d87SRobert Watson * XXXRW: Some confusion: V4/V6 flags relate to binding, and 655fa046d87SRobert Watson * therefore probably require the hash lock, which isn't held here. 656fa046d87SRobert Watson * Is this a significant problem? 657fa046d87SRobert Watson */ 6580ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 659fb59c426SYoshinobu Inoue struct sockaddr_in sin; 660fb59c426SYoshinobu Inoue 661d46a5312SMaxim Konovalov if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 662d46a5312SMaxim Konovalov error = EINVAL; 663d46a5312SMaxim Konovalov goto out; 664d46a5312SMaxim Konovalov } 6655dba6adaSMichael Tuexen if ((inp->inp_vflag & INP_IPV4) == 0) { 6665dba6adaSMichael Tuexen error = EAFNOSUPPORT; 6675dba6adaSMichael Tuexen goto out; 6685dba6adaSMichael Tuexen } 66933841545SHajimu UMEMOTO 6700ecd976eSBjoern A. Zeeb in6_sin6_2_sin(&sin, sin6); 671888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 672888973f5SMichael Tuexen error = EAFNOSUPPORT; 673888973f5SMichael Tuexen goto out; 674888973f5SMichael Tuexen } 675f903a308SMichael Tuexen if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) { 676f903a308SMichael Tuexen error = EACCES; 6772cf21ae5SRandall Stewart goto out; 6782cf21ae5SRandall Stewart } 679b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, 680b89e82ddSJamie Gritton &sin.sin_addr)) != 0) 681413628a7SBjoern A. Zeeb goto out; 6824a91aa8fSMichael Tuexen inp->inp_vflag |= INP_IPV4; 6834a91aa8fSMichael Tuexen inp->inp_vflag &= ~INP_IPV6; 684c1604fe4SGleb Smirnoff NET_EPOCH_ENTER(et); 685b40ce416SJulian Elischer if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 686c1604fe4SGleb Smirnoff goto out_in_epoch; 68709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 68809fe6320SNavdeep Parhar if (registered_toedevs > 0 && 689adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 69009fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 691c1604fe4SGleb Smirnoff goto out_in_epoch; 69209fe6320SNavdeep Parhar #endif 693*40fa3e40SGleb Smirnoff error = tcp_output(tp); 694c1604fe4SGleb Smirnoff goto out_in_epoch; 6955dba6adaSMichael Tuexen } else { 6965dba6adaSMichael Tuexen if ((inp->inp_vflag & INP_IPV6) == 0) { 6975dba6adaSMichael Tuexen error = EAFNOSUPPORT; 6985dba6adaSMichael Tuexen goto out; 6995dba6adaSMichael Tuexen } 700fb59c426SYoshinobu Inoue } 701b287c6c7SBjoern A. Zeeb #endif 7024a91aa8fSMichael Tuexen if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0) 7034a91aa8fSMichael Tuexen goto out; 704fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 705fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 706dcdb4371SBjoern A. Zeeb inp->inp_inc.inc_flags |= INC_ISIPV6; 707b40ce416SJulian Elischer if ((error = tcp6_connect(tp, nam, td)) != 0) 708fb59c426SYoshinobu Inoue goto out; 70909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 71009fe6320SNavdeep Parhar if (registered_toedevs > 0 && 711adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 71209fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 71309fe6320SNavdeep Parhar goto out; 71409fe6320SNavdeep Parhar #endif 71509fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 716109eb549SGleb Smirnoff NET_EPOCH_ENTER(et); 717*40fa3e40SGleb Smirnoff error = tcp_output(tp); 7187754e281SBjoern A. Zeeb #ifdef INET 719c1604fe4SGleb Smirnoff out_in_epoch: 7207754e281SBjoern A. Zeeb #endif 721109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 722623dce13SRobert Watson out: 7234a91aa8fSMichael Tuexen /* 7244a91aa8fSMichael Tuexen * If the implicit bind in the connect call fails, restore 7254a91aa8fSMichael Tuexen * the flags we modified. 7264a91aa8fSMichael Tuexen */ 7274a91aa8fSMichael Tuexen if (error != 0 && inp->inp_lport == 0) { 7284a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 7294a91aa8fSMichael Tuexen inp->inp_inc.inc_flags = incflagsav; 7304a91aa8fSMichael Tuexen } 7314a91aa8fSMichael Tuexen 732623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 7335d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CONNECT); 7348501a69cSRobert Watson INP_WUNLOCK(inp); 735623dce13SRobert Watson return (error); 736fb59c426SYoshinobu Inoue } 737fb59c426SYoshinobu Inoue #endif /* INET6 */ 738fb59c426SYoshinobu Inoue 7392c37256eSGarrett Wollman /* 7402c37256eSGarrett Wollman * Initiate disconnect from peer. 7412c37256eSGarrett Wollman * If connection never passed embryonic stage, just drop; 7422c37256eSGarrett Wollman * else if don't need to let data drain, then can just drop anyways, 7432c37256eSGarrett Wollman * else have to begin TCP shutdown process: mark socket disconnecting, 7442c37256eSGarrett Wollman * drain unread data, state switch to reflect user close, and 7452c37256eSGarrett Wollman * send segment (e.g. FIN) to peer. Socket will be really disconnected 7462c37256eSGarrett Wollman * when peer sends FIN and acks ours. 7472c37256eSGarrett Wollman * 7482c37256eSGarrett Wollman * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 7492c37256eSGarrett Wollman */ 7502c37256eSGarrett Wollman static int 7512c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so) 7522c37256eSGarrett Wollman { 753f76fcf6dSJeffrey Hsu struct inpcb *inp; 754623dce13SRobert Watson struct tcpcb *tp = NULL; 7556573d758SMatt Macy struct epoch_tracker et; 756623dce13SRobert Watson int error = 0; 7572c37256eSGarrett Wollman 758623dce13SRobert Watson TCPDEBUG0; 75997a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 760623dce13SRobert Watson inp = sotoinpcb(so); 761623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 7628501a69cSRobert Watson INP_WLOCK(inp); 763489dcc92SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) 764489dcc92SJulien Charbon goto out; 765489dcc92SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 76621367f63SSam Leffler error = ECONNRESET; 767623dce13SRobert Watson goto out; 768623dce13SRobert Watson } 769623dce13SRobert Watson tp = intotcpcb(inp); 770623dce13SRobert Watson TCPDEBUG1(); 771623dce13SRobert Watson tcp_disconnect(tp); 772623dce13SRobert Watson out: 773623dce13SRobert Watson TCPDEBUG2(PRU_DISCONNECT); 7745d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_DISCONNECT); 7758501a69cSRobert Watson INP_WUNLOCK(inp); 77697a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 777623dce13SRobert Watson return (error); 7782c37256eSGarrett Wollman } 7792c37256eSGarrett Wollman 780b287c6c7SBjoern A. Zeeb #ifdef INET 7812c37256eSGarrett Wollman /* 7828296cddfSRobert Watson * Accept a connection. Essentially all the work is done at higher levels; 7838296cddfSRobert Watson * just return the address of the peer, storing through addr. 7842c37256eSGarrett Wollman */ 7852c37256eSGarrett Wollman static int 78657bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam) 7872c37256eSGarrett Wollman { 7882c37256eSGarrett Wollman int error = 0; 789f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 7901db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 79126ef6ac4SDon Lewis struct in_addr addr; 79226ef6ac4SDon Lewis in_port_t port = 0; 7931db24ffbSJonathan Lemon TCPDEBUG0; 7942c37256eSGarrett Wollman 7953d2d3ef4SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 7963d2d3ef4SRobert Watson return (ECONNABORTED); 797f76fcf6dSJeffrey Hsu 798f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 799623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 8008501a69cSRobert Watson INP_WLOCK(inp); 801ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 8023d2d3ef4SRobert Watson error = ECONNABORTED; 803623dce13SRobert Watson goto out; 804623dce13SRobert Watson } 8051db24ffbSJonathan Lemon tp = intotcpcb(inp); 8061db24ffbSJonathan Lemon TCPDEBUG1(); 807f76fcf6dSJeffrey Hsu 808f76fcf6dSJeffrey Hsu /* 80954d642bbSRobert Watson * We inline in_getpeeraddr and COMMON_END here, so that we can 81026ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 81126ef6ac4SDon Lewis * release the lock. 812f76fcf6dSJeffrey Hsu */ 81326ef6ac4SDon Lewis port = inp->inp_fport; 81426ef6ac4SDon Lewis addr = inp->inp_faddr; 815f76fcf6dSJeffrey Hsu 816623dce13SRobert Watson out: 817623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 8185d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 8198501a69cSRobert Watson INP_WUNLOCK(inp); 82026ef6ac4SDon Lewis if (error == 0) 82126ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 82226ef6ac4SDon Lewis return error; 8232c37256eSGarrett Wollman } 824b287c6c7SBjoern A. Zeeb #endif /* INET */ 8252c37256eSGarrett Wollman 826fb59c426SYoshinobu Inoue #ifdef INET6 827fb59c426SYoshinobu Inoue static int 828fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 829fb59c426SYoshinobu Inoue { 830f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 831fb59c426SYoshinobu Inoue int error = 0; 8321db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 83326ef6ac4SDon Lewis struct in_addr addr; 83426ef6ac4SDon Lewis struct in6_addr addr6; 8356573d758SMatt Macy struct epoch_tracker et; 83626ef6ac4SDon Lewis in_port_t port = 0; 83726ef6ac4SDon Lewis int v4 = 0; 8381db24ffbSJonathan Lemon TCPDEBUG0; 839fb59c426SYoshinobu Inoue 840b4470c16SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 841b4470c16SRobert Watson return (ECONNABORTED); 842f76fcf6dSJeffrey Hsu 843f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 844623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 84597a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 8468501a69cSRobert Watson INP_WLOCK(inp); 847ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 84821367f63SSam Leffler error = ECONNABORTED; 849623dce13SRobert Watson goto out; 850623dce13SRobert Watson } 8511db24ffbSJonathan Lemon tp = intotcpcb(inp); 8521db24ffbSJonathan Lemon TCPDEBUG1(); 853623dce13SRobert Watson 85426ef6ac4SDon Lewis /* 85526ef6ac4SDon Lewis * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 85626ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 85726ef6ac4SDon Lewis * release the lock. 85826ef6ac4SDon Lewis */ 85926ef6ac4SDon Lewis if (inp->inp_vflag & INP_IPV4) { 86026ef6ac4SDon Lewis v4 = 1; 86126ef6ac4SDon Lewis port = inp->inp_fport; 86226ef6ac4SDon Lewis addr = inp->inp_faddr; 86326ef6ac4SDon Lewis } else { 86426ef6ac4SDon Lewis port = inp->inp_fport; 86526ef6ac4SDon Lewis addr6 = inp->in6p_faddr; 86626ef6ac4SDon Lewis } 86726ef6ac4SDon Lewis 868623dce13SRobert Watson out: 869623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 8705d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 8718501a69cSRobert Watson INP_WUNLOCK(inp); 87297a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 87326ef6ac4SDon Lewis if (error == 0) { 87426ef6ac4SDon Lewis if (v4) 87526ef6ac4SDon Lewis *nam = in6_v4mapsin6_sockaddr(port, &addr); 87626ef6ac4SDon Lewis else 87726ef6ac4SDon Lewis *nam = in6_sockaddr(port, &addr6); 87826ef6ac4SDon Lewis } 87926ef6ac4SDon Lewis return error; 880fb59c426SYoshinobu Inoue } 881fb59c426SYoshinobu Inoue #endif /* INET6 */ 882f76fcf6dSJeffrey Hsu 883f76fcf6dSJeffrey Hsu /* 8842c37256eSGarrett Wollman * Mark the connection as being incapable of further output. 8852c37256eSGarrett Wollman */ 8862c37256eSGarrett Wollman static int 8872c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so) 8882c37256eSGarrett Wollman { 8892c37256eSGarrett Wollman int error = 0; 890f76fcf6dSJeffrey Hsu struct inpcb *inp; 891623dce13SRobert Watson struct tcpcb *tp = NULL; 8926573d758SMatt Macy struct epoch_tracker et; 8932c37256eSGarrett Wollman 894623dce13SRobert Watson TCPDEBUG0; 89597a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 896623dce13SRobert Watson inp = sotoinpcb(so); 897623dce13SRobert Watson KASSERT(inp != NULL, ("inp == NULL")); 8988501a69cSRobert Watson INP_WLOCK(inp); 899ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 90021367f63SSam Leffler error = ECONNRESET; 901623dce13SRobert Watson goto out; 902623dce13SRobert Watson } 903623dce13SRobert Watson tp = intotcpcb(inp); 904623dce13SRobert Watson TCPDEBUG1(); 9052c37256eSGarrett Wollman socantsendmore(so); 906623dce13SRobert Watson tcp_usrclosed(tp); 907ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 908*40fa3e40SGleb Smirnoff error = tcp_output(tp); 909623dce13SRobert Watson 910623dce13SRobert Watson out: 911623dce13SRobert Watson TCPDEBUG2(PRU_SHUTDOWN); 9125d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN); 9138501a69cSRobert Watson INP_WUNLOCK(inp); 91497a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 915623dce13SRobert Watson 916623dce13SRobert Watson return (error); 9172c37256eSGarrett Wollman } 9182c37256eSGarrett Wollman 9192c37256eSGarrett Wollman /* 9202c37256eSGarrett Wollman * After a receive, possibly send window update to peer. 9212c37256eSGarrett Wollman */ 9222c37256eSGarrett Wollman static int 9232c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags) 9242c37256eSGarrett Wollman { 925109eb549SGleb Smirnoff struct epoch_tracker et; 926f76fcf6dSJeffrey Hsu struct inpcb *inp; 927623dce13SRobert Watson struct tcpcb *tp = NULL; 928623dce13SRobert Watson int error = 0; 9292c37256eSGarrett Wollman 930623dce13SRobert Watson TCPDEBUG0; 931623dce13SRobert Watson inp = sotoinpcb(so); 932623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 9338501a69cSRobert Watson INP_WLOCK(inp); 934ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 93521367f63SSam Leffler error = ECONNRESET; 936623dce13SRobert Watson goto out; 937623dce13SRobert Watson } 938623dce13SRobert Watson tp = intotcpcb(inp); 939623dce13SRobert Watson TCPDEBUG1(); 940281a0fd4SPatrick Kelsey /* 941281a0fd4SPatrick Kelsey * For passively-created TFO connections, don't attempt a window 942281a0fd4SPatrick Kelsey * update while still in SYN_RECEIVED as this may trigger an early 943281a0fd4SPatrick Kelsey * SYN|ACK. It is preferable to have the SYN|ACK be sent along with 944281a0fd4SPatrick Kelsey * application response data, or failing that, when the DELACK timer 945281a0fd4SPatrick Kelsey * expires. 946281a0fd4SPatrick Kelsey */ 94768bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 948281a0fd4SPatrick Kelsey (tp->t_state == TCPS_SYN_RECEIVED)) 949281a0fd4SPatrick Kelsey goto out; 95042ce7937SGleb Smirnoff NET_EPOCH_ENTER(et); 95109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 95209fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) 95309fe6320SNavdeep Parhar tcp_offload_rcvd(tp); 954460cf046SNavdeep Parhar else 95509fe6320SNavdeep Parhar #endif 956*40fa3e40SGleb Smirnoff tcp_output(tp); 957109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 958623dce13SRobert Watson out: 959623dce13SRobert Watson TCPDEBUG2(PRU_RCVD); 9605d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVD); 9618501a69cSRobert Watson INP_WUNLOCK(inp); 962623dce13SRobert Watson return (error); 9632c37256eSGarrett Wollman } 9642c37256eSGarrett Wollman 9652c37256eSGarrett Wollman /* 9662c37256eSGarrett Wollman * Do a send by putting data in output queue and updating urgent 9679c9906e9SPeter Wemm * marker if URG set. Possibly send more data. Unlike the other 9689c9906e9SPeter Wemm * pru_*() routines, the mbuf chains are our responsibility. We 9699c9906e9SPeter Wemm * must either enqueue them or free them. The other pru_* routines 9709c9906e9SPeter Wemm * generally are caller-frees. 9712c37256eSGarrett Wollman */ 9722c37256eSGarrett Wollman static int 97357bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 974b40ce416SJulian Elischer struct sockaddr *nam, struct mbuf *control, struct thread *td) 9752c37256eSGarrett Wollman { 97697a95ee1SGleb Smirnoff struct epoch_tracker et; 9772c37256eSGarrett Wollman int error = 0; 978f76fcf6dSJeffrey Hsu struct inpcb *inp; 979623dce13SRobert Watson struct tcpcb *tp = NULL; 980888973f5SMichael Tuexen #ifdef INET 98151e08d53SMichael Tuexen #ifdef INET6 98251e08d53SMichael Tuexen struct sockaddr_in sin; 98351e08d53SMichael Tuexen #endif 98451e08d53SMichael Tuexen struct sockaddr_in *sinp; 985888973f5SMichael Tuexen #endif 986fb59c426SYoshinobu Inoue #ifdef INET6 987fb59c426SYoshinobu Inoue int isipv6; 988fb59c426SYoshinobu Inoue #endif 9894a91aa8fSMichael Tuexen u_int8_t incflagsav; 9904a91aa8fSMichael Tuexen u_char vflagsav; 9914a91aa8fSMichael Tuexen bool restoreflags; 9929c9906e9SPeter Wemm TCPDEBUG0; 9932c37256eSGarrett Wollman 994f76fcf6dSJeffrey Hsu /* 99597a95ee1SGleb Smirnoff * We require the pcbinfo "read lock" if we will close the socket 99697a95ee1SGleb Smirnoff * as part of this call. 997f76fcf6dSJeffrey Hsu */ 99897a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 999f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 1000623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 10018501a69cSRobert Watson INP_WLOCK(inp); 10024a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 10034a91aa8fSMichael Tuexen incflagsav = inp->inp_inc.inc_flags; 10044a91aa8fSMichael Tuexen restoreflags = false; 1005ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 10067ff0b850SAndre Oppermann if (control) 10077ff0b850SAndre Oppermann m_freem(control); 100821367f63SSam Leffler error = ECONNRESET; 10099c9906e9SPeter Wemm goto out; 10109c9906e9SPeter Wemm } 1011d8acd268SMark Johnston if (control != NULL) { 1012d8acd268SMark Johnston /* TCP doesn't do control messages (rights, creds, etc) */ 1013d8acd268SMark Johnston if (control->m_len) { 1014d8acd268SMark Johnston m_freem(control); 1015d8acd268SMark Johnston error = EINVAL; 1016d8acd268SMark Johnston goto out; 1017d8acd268SMark Johnston } 1018d8acd268SMark Johnston m_freem(control); /* empty control, just free it */ 1019d8acd268SMark Johnston control = NULL; 1020d8acd268SMark Johnston } 10219c9906e9SPeter Wemm tp = intotcpcb(inp); 10227d2608a5SMark Johnston if ((flags & PRUS_OOB) != 0 && 10237d2608a5SMark Johnston (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0) 1024d3b6c96bSRandall Stewart goto out; 10257d2608a5SMark Johnston 10269c9906e9SPeter Wemm TCPDEBUG1(); 1027888973f5SMichael Tuexen if (nam != NULL && tp->t_state < TCPS_SYN_SENT) { 1028bd4a39ccSMark Johnston if (tp->t_state == TCPS_LISTEN) { 1029bd4a39ccSMark Johnston error = EINVAL; 1030bd4a39ccSMark Johnston goto out; 1031bd4a39ccSMark Johnston } 1032888973f5SMichael Tuexen switch (nam->sa_family) { 1033888973f5SMichael Tuexen #ifdef INET 1034888973f5SMichael Tuexen case AF_INET: 1035888973f5SMichael Tuexen sinp = (struct sockaddr_in *)nam; 1036888973f5SMichael Tuexen if (sinp->sin_len != sizeof(struct sockaddr_in)) { 1037888973f5SMichael Tuexen error = EINVAL; 1038888973f5SMichael Tuexen goto out; 1039888973f5SMichael Tuexen } 1040888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV6) != 0) { 1041888973f5SMichael Tuexen error = EAFNOSUPPORT; 1042888973f5SMichael Tuexen goto out; 1043888973f5SMichael Tuexen } 1044888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 1045888973f5SMichael Tuexen error = EAFNOSUPPORT; 1046888973f5SMichael Tuexen goto out; 1047888973f5SMichael Tuexen } 1048f903a308SMichael Tuexen if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) { 1049f903a308SMichael Tuexen error = EACCES; 10502cf21ae5SRandall Stewart goto out; 10512cf21ae5SRandall Stewart } 1052888973f5SMichael Tuexen if ((error = prison_remote_ip4(td->td_ucred, 10537d2608a5SMark Johnston &sinp->sin_addr))) 1054888973f5SMichael Tuexen goto out; 1055888973f5SMichael Tuexen #ifdef INET6 1056888973f5SMichael Tuexen isipv6 = 0; 1057888973f5SMichael Tuexen #endif 1058888973f5SMichael Tuexen break; 1059888973f5SMichael Tuexen #endif /* INET */ 1060888973f5SMichael Tuexen #ifdef INET6 1061888973f5SMichael Tuexen case AF_INET6: 1062888973f5SMichael Tuexen { 10630ecd976eSBjoern A. Zeeb struct sockaddr_in6 *sin6; 1064888973f5SMichael Tuexen 10650ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 10660ecd976eSBjoern A. Zeeb if (sin6->sin6_len != sizeof(*sin6)) { 1067888973f5SMichael Tuexen error = EINVAL; 1068888973f5SMichael Tuexen goto out; 1069888973f5SMichael Tuexen } 1070e240ce42SMichael Tuexen if ((inp->inp_vflag & INP_IPV6PROTO) == 0) { 1071e240ce42SMichael Tuexen error = EAFNOSUPPORT; 1072e240ce42SMichael Tuexen goto out; 1073e240ce42SMichael Tuexen } 10740ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 1075888973f5SMichael Tuexen error = EAFNOSUPPORT; 1076888973f5SMichael Tuexen goto out; 1077888973f5SMichael Tuexen } 10780ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 1079888973f5SMichael Tuexen #ifdef INET 1080888973f5SMichael Tuexen if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 1081888973f5SMichael Tuexen error = EINVAL; 1082888973f5SMichael Tuexen goto out; 1083888973f5SMichael Tuexen } 1084888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV4) == 0) { 1085888973f5SMichael Tuexen error = EAFNOSUPPORT; 1086888973f5SMichael Tuexen goto out; 1087888973f5SMichael Tuexen } 10884a91aa8fSMichael Tuexen restoreflags = true; 1089888973f5SMichael Tuexen inp->inp_vflag &= ~INP_IPV6; 1090888973f5SMichael Tuexen sinp = &sin; 10910ecd976eSBjoern A. Zeeb in6_sin6_2_sin(sinp, sin6); 1092888973f5SMichael Tuexen if (IN_MULTICAST( 1093888973f5SMichael Tuexen ntohl(sinp->sin_addr.s_addr))) { 1094888973f5SMichael Tuexen error = EAFNOSUPPORT; 1095888973f5SMichael Tuexen goto out; 1096888973f5SMichael Tuexen } 1097888973f5SMichael Tuexen if ((error = prison_remote_ip4(td->td_ucred, 10987d2608a5SMark Johnston &sinp->sin_addr))) 1099888973f5SMichael Tuexen goto out; 1100888973f5SMichael Tuexen isipv6 = 0; 1101888973f5SMichael Tuexen #else /* !INET */ 1102888973f5SMichael Tuexen error = EAFNOSUPPORT; 1103888973f5SMichael Tuexen goto out; 1104888973f5SMichael Tuexen #endif /* INET */ 1105888973f5SMichael Tuexen } else { 1106888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV6) == 0) { 1107888973f5SMichael Tuexen error = EAFNOSUPPORT; 1108888973f5SMichael Tuexen goto out; 1109888973f5SMichael Tuexen } 11104a91aa8fSMichael Tuexen restoreflags = true; 1111888973f5SMichael Tuexen inp->inp_vflag &= ~INP_IPV4; 1112888973f5SMichael Tuexen inp->inp_inc.inc_flags |= INC_ISIPV6; 1113888973f5SMichael Tuexen if ((error = prison_remote_ip6(td->td_ucred, 11147d2608a5SMark Johnston &sin6->sin6_addr))) 1115888973f5SMichael Tuexen goto out; 1116888973f5SMichael Tuexen isipv6 = 1; 1117888973f5SMichael Tuexen } 1118888973f5SMichael Tuexen break; 1119888973f5SMichael Tuexen } 1120888973f5SMichael Tuexen #endif /* INET6 */ 1121888973f5SMichael Tuexen default: 1122888973f5SMichael Tuexen error = EAFNOSUPPORT; 1123888973f5SMichael Tuexen goto out; 1124888973f5SMichael Tuexen } 1125888973f5SMichael Tuexen } 11262c37256eSGarrett Wollman if (!(flags & PRUS_OOB)) { 1127651e4e6aSGleb Smirnoff sbappendstream(&so->so_snd, m, flags); 11287d2608a5SMark Johnston m = NULL; 11292c37256eSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 1130bd4a39ccSMark Johnston KASSERT(tp->t_state == TCPS_CLOSED, 1131bd4a39ccSMark Johnston ("%s: tp %p is listening", __func__, tp)); 1132bd4a39ccSMark Johnston 11332c37256eSGarrett Wollman /* 11342c37256eSGarrett Wollman * Do implied connect if not yet connected, 11352c37256eSGarrett Wollman * initialize window to default value, and 11360c39d38dSGleb Smirnoff * initialize maxseg using peer's cached MSS. 11372c37256eSGarrett Wollman */ 1138fb59c426SYoshinobu Inoue #ifdef INET6 1139fb59c426SYoshinobu Inoue if (isipv6) 1140b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 1141fb59c426SYoshinobu Inoue #endif /* INET6 */ 1142b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1143b287c6c7SBjoern A. Zeeb else 1144b287c6c7SBjoern A. Zeeb #endif 1145b287c6c7SBjoern A. Zeeb #ifdef INET 1146888973f5SMichael Tuexen error = tcp_connect(tp, 1147888973f5SMichael Tuexen (struct sockaddr *)sinp, td); 1148b287c6c7SBjoern A. Zeeb #endif 11494a91aa8fSMichael Tuexen /* 11504a91aa8fSMichael Tuexen * The bind operation in tcp_connect succeeded. We 11514a91aa8fSMichael Tuexen * no longer want to restore the flags if later 11524a91aa8fSMichael Tuexen * operations fail. 11534a91aa8fSMichael Tuexen */ 11544a91aa8fSMichael Tuexen if (error == 0 || inp->inp_lport != 0) 11554a91aa8fSMichael Tuexen restoreflags = false; 11564a91aa8fSMichael Tuexen 11577d2608a5SMark Johnston if (error) { 11587d2608a5SMark Johnston /* m is freed if PRUS_NOTREADY is unset. */ 11597d2608a5SMark Johnston sbflush(&so->so_snd); 11602c37256eSGarrett Wollman goto out; 11617d2608a5SMark Johnston } 1162c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) 1163c560df6fSPatrick Kelsey tcp_fastopen_connect(tp); 116418a75309SPatrick Kelsey else { 11652c37256eSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 11662c37256eSGarrett Wollman tcp_mss(tp, -1); 11672c37256eSGarrett Wollman } 1168c560df6fSPatrick Kelsey } 11692c37256eSGarrett Wollman if (flags & PRUS_EOF) { 11702c37256eSGarrett Wollman /* 11712c37256eSGarrett Wollman * Close the send side of the connection after 11722c37256eSGarrett Wollman * the data is sent. 11732c37256eSGarrett Wollman */ 11742c37256eSGarrett Wollman socantsendmore(so); 1175623dce13SRobert Watson tcp_usrclosed(tp); 11762c37256eSGarrett Wollman } 1177e854dd38SRandall Stewart if (TCPS_HAVEESTABLISHED(tp->t_state) && 1178e854dd38SRandall Stewart ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 1179e854dd38SRandall Stewart (tp->t_fbyte_out == 0) && 1180e854dd38SRandall Stewart (so->so_snd.sb_ccc > 0)) { 1181e854dd38SRandall Stewart tp->t_fbyte_out = ticks; 1182e854dd38SRandall Stewart if (tp->t_fbyte_out == 0) 1183e854dd38SRandall Stewart tp->t_fbyte_out = 1; 1184e854dd38SRandall Stewart if (tp->t_fbyte_out && tp->t_fbyte_in) 1185e854dd38SRandall Stewart tp->t_flags2 |= TF2_FBYTES_COMPLETE; 1186e854dd38SRandall Stewart } 11872cbcd3c1SGleb Smirnoff if (!(inp->inp_flags & INP_DROPPED) && 11882cbcd3c1SGleb Smirnoff !(flags & PRUS_NOTREADY)) { 1189b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 1190b0acefa8SBill Fenner tp->t_flags |= TF_MORETOCOME; 1191*40fa3e40SGleb Smirnoff error = tcp_output(tp); 1192b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 1193b0acefa8SBill Fenner tp->t_flags &= ~TF_MORETOCOME; 1194b0acefa8SBill Fenner } 11952c37256eSGarrett Wollman } else { 1196623dce13SRobert Watson /* 1197623dce13SRobert Watson * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 1198623dce13SRobert Watson */ 1199d2bc35abSRobert Watson SOCKBUF_LOCK(&so->so_snd); 12002c37256eSGarrett Wollman if (sbspace(&so->so_snd) < -512) { 1201d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 12022c37256eSGarrett Wollman error = ENOBUFS; 12032c37256eSGarrett Wollman goto out; 12042c37256eSGarrett Wollman } 12052c37256eSGarrett Wollman /* 12062c37256eSGarrett Wollman * According to RFC961 (Assigned Protocols), 12072c37256eSGarrett Wollman * the urgent pointer points to the last octet 12082c37256eSGarrett Wollman * of urgent data. We continue, however, 12092c37256eSGarrett Wollman * to consider it to indicate the first octet 12102c37256eSGarrett Wollman * of data past the urgent section. 12112c37256eSGarrett Wollman * Otherwise, snd_up should be one lower. 12122c37256eSGarrett Wollman */ 1213651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_snd, m, flags); 1214d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 12157d2608a5SMark Johnston m = NULL; 1216ef53690bSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 1217ef53690bSGarrett Wollman /* 1218ef53690bSGarrett Wollman * Do implied connect if not yet connected, 1219ef53690bSGarrett Wollman * initialize window to default value, and 12200c39d38dSGleb Smirnoff * initialize maxseg using peer's cached MSS. 1221ef53690bSGarrett Wollman */ 122218a75309SPatrick Kelsey 1223c560df6fSPatrick Kelsey /* 1224c560df6fSPatrick Kelsey * Not going to contemplate SYN|URG 1225c560df6fSPatrick Kelsey */ 1226c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) 1227c560df6fSPatrick Kelsey tp->t_flags &= ~TF_FASTOPEN; 1228fb59c426SYoshinobu Inoue #ifdef INET6 1229fb59c426SYoshinobu Inoue if (isipv6) 1230b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 1231fb59c426SYoshinobu Inoue #endif /* INET6 */ 1232b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1233b287c6c7SBjoern A. Zeeb else 1234b287c6c7SBjoern A. Zeeb #endif 1235b287c6c7SBjoern A. Zeeb #ifdef INET 1236888973f5SMichael Tuexen error = tcp_connect(tp, 1237888973f5SMichael Tuexen (struct sockaddr *)sinp, td); 1238b287c6c7SBjoern A. Zeeb #endif 12394a91aa8fSMichael Tuexen /* 12404a91aa8fSMichael Tuexen * The bind operation in tcp_connect succeeded. We 12414a91aa8fSMichael Tuexen * no longer want to restore the flags if later 12424a91aa8fSMichael Tuexen * operations fail. 12434a91aa8fSMichael Tuexen */ 12444a91aa8fSMichael Tuexen if (error == 0 || inp->inp_lport != 0) 12454a91aa8fSMichael Tuexen restoreflags = false; 12464a91aa8fSMichael Tuexen 12477d2608a5SMark Johnston if (error != 0) { 12487d2608a5SMark Johnston /* m is freed if PRUS_NOTREADY is unset. */ 12497d2608a5SMark Johnston sbflush(&so->so_snd); 1250ef53690bSGarrett Wollman goto out; 12517d2608a5SMark Johnston } 1252ef53690bSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 1253ef53690bSGarrett Wollman tcp_mss(tp, -1); 1254623dce13SRobert Watson } 1255300fa232SGleb Smirnoff tp->snd_up = tp->snd_una + sbavail(&so->so_snd); 12567d2608a5SMark Johnston if ((flags & PRUS_NOTREADY) == 0) { 12572cdbfa66SPaul Saab tp->t_flags |= TF_FORCEDATA; 1258*40fa3e40SGleb Smirnoff error = tcp_output(tp); 12592cdbfa66SPaul Saab tp->t_flags &= ~TF_FORCEDATA; 12602c37256eSGarrett Wollman } 12612cbcd3c1SGleb Smirnoff } 12622529f56eSJonathan T. Looney TCP_LOG_EVENT(tp, NULL, 12632529f56eSJonathan T. Looney &inp->inp_socket->so_rcv, 12642529f56eSJonathan T. Looney &inp->inp_socket->so_snd, 12652529f56eSJonathan T. Looney TCP_LOG_USERSEND, error, 12662529f56eSJonathan T. Looney 0, NULL, false); 12677d2608a5SMark Johnston 1268d1401c90SRobert Watson out: 12694a91aa8fSMichael Tuexen /* 12707d2608a5SMark Johnston * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is 12717d2608a5SMark Johnston * responsible for freeing memory. 12727d2608a5SMark Johnston */ 12737d2608a5SMark Johnston if (m != NULL && (flags & PRUS_NOTREADY) == 0) 12747d2608a5SMark Johnston m_freem(m); 12757d2608a5SMark Johnston 12767d2608a5SMark Johnston /* 12774a91aa8fSMichael Tuexen * If the request was unsuccessful and we changed flags, 12784a91aa8fSMichael Tuexen * restore the original flags. 12794a91aa8fSMichael Tuexen */ 12804a91aa8fSMichael Tuexen if (error != 0 && restoreflags) { 12814a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 12824a91aa8fSMichael Tuexen inp->inp_inc.inc_flags = incflagsav; 12834a91aa8fSMichael Tuexen } 1284d1401c90SRobert Watson TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB : 12852c37256eSGarrett Wollman ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 12865d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB : 12875d06879aSGeorge V. Neville-Neil ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 12888501a69cSRobert Watson INP_WUNLOCK(inp); 128997a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 129073fddedaSPeter Grehan return (error); 12912c37256eSGarrett Wollman } 12922c37256eSGarrett Wollman 12932cbcd3c1SGleb Smirnoff static int 12942cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count) 12952cbcd3c1SGleb Smirnoff { 1296109eb549SGleb Smirnoff struct epoch_tracker et; 12972cbcd3c1SGleb Smirnoff struct inpcb *inp; 12982cbcd3c1SGleb Smirnoff struct tcpcb *tp; 12992cbcd3c1SGleb Smirnoff int error; 13002cbcd3c1SGleb Smirnoff 13012cbcd3c1SGleb Smirnoff inp = sotoinpcb(so); 13022cbcd3c1SGleb Smirnoff INP_WLOCK(inp); 13032cbcd3c1SGleb Smirnoff if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 13042cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 130582334850SJohn Baldwin mb_free_notready(m, count); 13062cbcd3c1SGleb Smirnoff return (ECONNRESET); 13072cbcd3c1SGleb Smirnoff } 13082cbcd3c1SGleb Smirnoff tp = intotcpcb(inp); 13092cbcd3c1SGleb Smirnoff 13102cbcd3c1SGleb Smirnoff SOCKBUF_LOCK(&so->so_snd); 13112cbcd3c1SGleb Smirnoff error = sbready(&so->so_snd, m, count); 13122cbcd3c1SGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 1313109eb549SGleb Smirnoff if (error == 0) { 1314109eb549SGleb Smirnoff NET_EPOCH_ENTER(et); 1315*40fa3e40SGleb Smirnoff error = tcp_output(tp); 1316109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 1317109eb549SGleb Smirnoff } 13182cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 13192cbcd3c1SGleb Smirnoff 13202cbcd3c1SGleb Smirnoff return (error); 13212cbcd3c1SGleb Smirnoff } 13222cbcd3c1SGleb Smirnoff 13232c37256eSGarrett Wollman /* 1324a152f8a3SRobert Watson * Abort the TCP. Drop the connection abruptly. 13252c37256eSGarrett Wollman */ 1326ac45e92fSRobert Watson static void 13272c37256eSGarrett Wollman tcp_usr_abort(struct socket *so) 13282c37256eSGarrett Wollman { 1329f76fcf6dSJeffrey Hsu struct inpcb *inp; 1330a152f8a3SRobert Watson struct tcpcb *tp = NULL; 13316573d758SMatt Macy struct epoch_tracker et; 1332623dce13SRobert Watson TCPDEBUG0; 1333c78cbc7bSRobert Watson 1334ac45e92fSRobert Watson inp = sotoinpcb(so); 1335c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 1336c78cbc7bSRobert Watson 133797a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 13388501a69cSRobert Watson INP_WLOCK(inp); 1339c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 1340c78cbc7bSRobert Watson ("tcp_usr_abort: inp_socket == NULL")); 1341c78cbc7bSRobert Watson 1342c78cbc7bSRobert Watson /* 1343a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, drop. 1344c78cbc7bSRobert Watson */ 1345ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_TIMEWAIT) && 1346ad71fe3cSRobert Watson !(inp->inp_flags & INP_DROPPED)) { 1347c78cbc7bSRobert Watson tp = intotcpcb(inp); 1348a152f8a3SRobert Watson TCPDEBUG1(); 13498fa799bdSJonathan T. Looney tp = tcp_drop(tp, ECONNABORTED); 13508fa799bdSJonathan T. Looney if (tp == NULL) 13518fa799bdSJonathan T. Looney goto dropped; 1352a152f8a3SRobert Watson TCPDEBUG2(PRU_ABORT); 13535d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ABORT); 1354c78cbc7bSRobert Watson } 1355ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1356a152f8a3SRobert Watson SOCK_LOCK(so); 1357a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 1358a152f8a3SRobert Watson SOCK_UNLOCK(so); 1359ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1360a152f8a3SRobert Watson } 13618501a69cSRobert Watson INP_WUNLOCK(inp); 13628fa799bdSJonathan T. Looney dropped: 136397a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 1364a152f8a3SRobert Watson } 1365a152f8a3SRobert Watson 1366a152f8a3SRobert Watson /* 1367a152f8a3SRobert Watson * TCP socket is closed. Start friendly disconnect. 1368a152f8a3SRobert Watson */ 1369a152f8a3SRobert Watson static void 1370a152f8a3SRobert Watson tcp_usr_close(struct socket *so) 1371a152f8a3SRobert Watson { 1372a152f8a3SRobert Watson struct inpcb *inp; 1373a152f8a3SRobert Watson struct tcpcb *tp = NULL; 13746573d758SMatt Macy struct epoch_tracker et; 1375a152f8a3SRobert Watson TCPDEBUG0; 1376a152f8a3SRobert Watson 1377a152f8a3SRobert Watson inp = sotoinpcb(so); 1378a152f8a3SRobert Watson KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL")); 1379a152f8a3SRobert Watson 138097a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 13818501a69cSRobert Watson INP_WLOCK(inp); 1382a152f8a3SRobert Watson KASSERT(inp->inp_socket != NULL, 1383a152f8a3SRobert Watson ("tcp_usr_close: inp_socket == NULL")); 1384a152f8a3SRobert Watson 1385a152f8a3SRobert Watson /* 1386a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, initiate 1387a152f8a3SRobert Watson * a disconnect. 1388a152f8a3SRobert Watson */ 1389ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_TIMEWAIT) && 1390ad71fe3cSRobert Watson !(inp->inp_flags & INP_DROPPED)) { 1391a152f8a3SRobert Watson tp = intotcpcb(inp); 1392a152f8a3SRobert Watson TCPDEBUG1(); 1393a152f8a3SRobert Watson tcp_disconnect(tp); 1394a152f8a3SRobert Watson TCPDEBUG2(PRU_CLOSE); 13955d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CLOSE); 1396a152f8a3SRobert Watson } 1397ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1398a152f8a3SRobert Watson SOCK_LOCK(so); 1399a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 1400a152f8a3SRobert Watson SOCK_UNLOCK(so); 1401ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1402a152f8a3SRobert Watson } 14038501a69cSRobert Watson INP_WUNLOCK(inp); 140497a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 14052c37256eSGarrett Wollman } 14062c37256eSGarrett Wollman 1407d3b6c96bSRandall Stewart static int 1408d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags) 1409d3b6c96bSRandall Stewart { 1410d3b6c96bSRandall Stewart /* 1411d3b6c96bSRandall Stewart * If the specific TCP stack has a pru_options 1412d3b6c96bSRandall Stewart * specified then it does not always support 1413d3b6c96bSRandall Stewart * all the PRU_XX options and we must ask it. 1414d3b6c96bSRandall Stewart * If the function is not specified then all 1415d3b6c96bSRandall Stewart * of the PRU_XX options are supported. 1416d3b6c96bSRandall Stewart */ 1417d3b6c96bSRandall Stewart int ret = 0; 1418d3b6c96bSRandall Stewart 1419d3b6c96bSRandall Stewart if (tp->t_fb->tfb_pru_options) { 1420d3b6c96bSRandall Stewart ret = (*tp->t_fb->tfb_pru_options)(tp, flags); 1421d3b6c96bSRandall Stewart } 1422d3b6c96bSRandall Stewart return (ret); 1423d3b6c96bSRandall Stewart } 1424d3b6c96bSRandall Stewart 14252c37256eSGarrett Wollman /* 14262c37256eSGarrett Wollman * Receive out-of-band data. 14272c37256eSGarrett Wollman */ 14282c37256eSGarrett Wollman static int 14292c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 14302c37256eSGarrett Wollman { 14312c37256eSGarrett Wollman int error = 0; 1432f76fcf6dSJeffrey Hsu struct inpcb *inp; 1433623dce13SRobert Watson struct tcpcb *tp = NULL; 14342c37256eSGarrett Wollman 1435623dce13SRobert Watson TCPDEBUG0; 1436623dce13SRobert Watson inp = sotoinpcb(so); 1437623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 14388501a69cSRobert Watson INP_WLOCK(inp); 1439ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 144021367f63SSam Leffler error = ECONNRESET; 1441623dce13SRobert Watson goto out; 1442623dce13SRobert Watson } 1443623dce13SRobert Watson tp = intotcpcb(inp); 1444d3b6c96bSRandall Stewart error = tcp_pru_options_support(tp, PRUS_OOB); 1445d3b6c96bSRandall Stewart if (error) { 1446d3b6c96bSRandall Stewart goto out; 1447d3b6c96bSRandall Stewart } 1448623dce13SRobert Watson TCPDEBUG1(); 14492c37256eSGarrett Wollman if ((so->so_oobmark == 0 && 1450c0b99ffaSRobert Watson (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 14514cc20ab1SSeigo Tanimura so->so_options & SO_OOBINLINE || 14524cc20ab1SSeigo Tanimura tp->t_oobflags & TCPOOB_HADDATA) { 14532c37256eSGarrett Wollman error = EINVAL; 14542c37256eSGarrett Wollman goto out; 14552c37256eSGarrett Wollman } 14562c37256eSGarrett Wollman if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 14572c37256eSGarrett Wollman error = EWOULDBLOCK; 14582c37256eSGarrett Wollman goto out; 14592c37256eSGarrett Wollman } 14602c37256eSGarrett Wollman m->m_len = 1; 14612c37256eSGarrett Wollman *mtod(m, caddr_t) = tp->t_iobc; 14622c37256eSGarrett Wollman if ((flags & MSG_PEEK) == 0) 14632c37256eSGarrett Wollman tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1464623dce13SRobert Watson 1465623dce13SRobert Watson out: 1466623dce13SRobert Watson TCPDEBUG2(PRU_RCVOOB); 14675d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVOOB); 14688501a69cSRobert Watson INP_WUNLOCK(inp); 1469623dce13SRobert Watson return (error); 14702c37256eSGarrett Wollman } 14712c37256eSGarrett Wollman 1472b287c6c7SBjoern A. Zeeb #ifdef INET 14732c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = { 1474756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1475756d52a1SPoul-Henning Kamp .pru_accept = tcp_usr_accept, 1476756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1477756d52a1SPoul-Henning Kamp .pru_bind = tcp_usr_bind, 1478756d52a1SPoul-Henning Kamp .pru_connect = tcp_usr_connect, 1479756d52a1SPoul-Henning Kamp .pru_control = in_control, 1480756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1481756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1482756d52a1SPoul-Henning Kamp .pru_listen = tcp_usr_listen, 148354d642bbSRobert Watson .pru_peeraddr = in_getpeeraddr, 1484756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1485756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1486756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 14872cbcd3c1SGleb Smirnoff .pru_ready = tcp_usr_ready, 1488756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 148954d642bbSRobert Watson .pru_sockaddr = in_getsockaddr, 1490a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1491a152f8a3SRobert Watson .pru_close = tcp_usr_close, 14922c37256eSGarrett Wollman }; 1493b287c6c7SBjoern A. Zeeb #endif /* INET */ 1494df8bae1dSRodney W. Grimes 1495fb59c426SYoshinobu Inoue #ifdef INET6 1496fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = { 1497756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1498756d52a1SPoul-Henning Kamp .pru_accept = tcp6_usr_accept, 1499756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1500756d52a1SPoul-Henning Kamp .pru_bind = tcp6_usr_bind, 1501756d52a1SPoul-Henning Kamp .pru_connect = tcp6_usr_connect, 1502756d52a1SPoul-Henning Kamp .pru_control = in6_control, 1503756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1504756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1505756d52a1SPoul-Henning Kamp .pru_listen = tcp6_usr_listen, 1506756d52a1SPoul-Henning Kamp .pru_peeraddr = in6_mapped_peeraddr, 1507756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1508756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1509756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 15102cbcd3c1SGleb Smirnoff .pru_ready = tcp_usr_ready, 1511756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 1512756d52a1SPoul-Henning Kamp .pru_sockaddr = in6_mapped_sockaddr, 1513a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1514a152f8a3SRobert Watson .pru_close = tcp_usr_close, 1515fb59c426SYoshinobu Inoue }; 1516fb59c426SYoshinobu Inoue #endif /* INET6 */ 1517fb59c426SYoshinobu Inoue 1518b287c6c7SBjoern A. Zeeb #ifdef INET 1519a0292f23SGarrett Wollman /* 1520a0292f23SGarrett Wollman * Common subroutine to open a TCP connection to remote host specified 1521a0292f23SGarrett Wollman * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 15225200e00eSIan Dowse * port number if needed. Call in_pcbconnect_setup to do the routing and 15235200e00eSIan Dowse * to choose a local host address (interface). If there is an existing 15245200e00eSIan Dowse * incarnation of the same connection in TIME-WAIT state and if the remote 15255200e00eSIan Dowse * host was sending CC options and if the connection duration was < MSL, then 1526a0292f23SGarrett Wollman * truncate the previous TIME-WAIT state and proceed. 1527a0292f23SGarrett Wollman * Initialize connection parameters and enter SYN-SENT state. 1528a0292f23SGarrett Wollman */ 15290312fbe9SPoul-Henning Kamp static int 1530ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1531a0292f23SGarrett Wollman { 1532a0292f23SGarrett Wollman struct inpcb *inp = tp->t_inpcb, *oinp; 1533a0292f23SGarrett Wollman struct socket *so = inp->inp_socket; 15345200e00eSIan Dowse struct in_addr laddr; 15355200e00eSIan Dowse u_short lport; 1536c3229e05SDavid Greenman int error; 1537a0292f23SGarrett Wollman 1538c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 15398501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1540fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 1541623dce13SRobert Watson 154225102351SMike Karels if (V_tcp_require_unique_port && inp->inp_lport == 0) { 15434616026fSErmal Luçi error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 15444616026fSErmal Luçi if (error) 1545fa046d87SRobert Watson goto out; 1546a0292f23SGarrett Wollman } 1547a0292f23SGarrett Wollman 1548a0292f23SGarrett Wollman /* 1549a0292f23SGarrett Wollman * Cannot simply call in_pcbconnect, because there might be an 1550a0292f23SGarrett Wollman * earlier incarnation of this same connection still in 1551a0292f23SGarrett Wollman * TIME_WAIT state, creating an ADDRINUSE error. 1552a0292f23SGarrett Wollman */ 15535200e00eSIan Dowse laddr = inp->inp_laddr; 15545200e00eSIan Dowse lport = inp->inp_lport; 15555200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 1556b0330ed9SPawel Jakub Dawidek &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 15575200e00eSIan Dowse if (error && oinp == NULL) 1558fa046d87SRobert Watson goto out; 1559fa046d87SRobert Watson if (oinp) { 1560fa046d87SRobert Watson error = EADDRINUSE; 1561fa046d87SRobert Watson goto out; 1562fa046d87SRobert Watson } 156325102351SMike Karels /* Handle initial bind if it hadn't been done in advance. */ 156425102351SMike Karels if (inp->inp_lport == 0) { 156525102351SMike Karels inp->inp_lport = lport; 156625102351SMike Karels if (in_pcbinshash(inp) != 0) { 156725102351SMike Karels inp->inp_lport = 0; 156825102351SMike Karels error = EAGAIN; 156925102351SMike Karels goto out; 157025102351SMike Karels } 157125102351SMike Karels } 15725200e00eSIan Dowse inp->inp_laddr = laddr; 157315bd2b43SDavid Greenman in_pcbrehash(inp); 1574fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1575a0292f23SGarrett Wollman 1576087b55eaSAndre Oppermann /* 1577087b55eaSAndre Oppermann * Compute window scaling to request: 1578087b55eaSAndre Oppermann * Scale to fit into sweet spot. See tcp_syncache.c. 1579087b55eaSAndre Oppermann * XXX: This should move to tcp_output(). 1580087b55eaSAndre Oppermann */ 1581a0292f23SGarrett Wollman while (tp->request_r_scale < TCP_MAX_WINSHIFT && 15829b3bc6bfSMike Silbersack (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1583a0292f23SGarrett Wollman tp->request_r_scale++; 1584a0292f23SGarrett Wollman 1585a0292f23SGarrett Wollman soisconnecting(so); 158678b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 158757f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 15888e02b4e0SMichael Tuexen tp->iss = tcp_new_isn(&inp->inp_inc); 15898e02b4e0SMichael Tuexen if (tp->t_flags & TF_REQ_TSTMP) 15908e02b4e0SMichael Tuexen tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc); 1591a0292f23SGarrett Wollman tcp_sendseqinit(tp); 1592a45d2726SAndras Olah 1593a0292f23SGarrett Wollman return 0; 1594fa046d87SRobert Watson 1595fa046d87SRobert Watson out: 1596fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1597fa046d87SRobert Watson return (error); 1598a0292f23SGarrett Wollman } 1599b287c6c7SBjoern A. Zeeb #endif /* INET */ 1600a0292f23SGarrett Wollman 1601fb59c426SYoshinobu Inoue #ifdef INET6 1602fb59c426SYoshinobu Inoue static int 1603ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1604fb59c426SYoshinobu Inoue { 1605a7e201bbSAndrey V. Elsukov struct inpcb *inp = tp->t_inpcb; 1606fb59c426SYoshinobu Inoue int error; 1607fb59c426SYoshinobu Inoue 16088501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1609fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 1610623dce13SRobert Watson 161125102351SMike Karels if (V_tcp_require_unique_port && inp->inp_lport == 0) { 16124616026fSErmal Luçi error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 16134616026fSErmal Luçi if (error) 1614fa046d87SRobert Watson goto out; 1615fb59c426SYoshinobu Inoue } 1616a7e201bbSAndrey V. Elsukov error = in6_pcbconnect(inp, nam, td->td_ucred); 1617a7e201bbSAndrey V. Elsukov if (error != 0) 1618b598155aSRobert Watson goto out; 1619fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1620fb59c426SYoshinobu Inoue 1621fb59c426SYoshinobu Inoue /* Compute window scaling to request. */ 1622fb59c426SYoshinobu Inoue while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1623970caf60SBjoern A. Zeeb (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1624fb59c426SYoshinobu Inoue tp->request_r_scale++; 1625fb59c426SYoshinobu Inoue 1626a7e201bbSAndrey V. Elsukov soisconnecting(inp->inp_socket); 162778b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 162857f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 16298e02b4e0SMichael Tuexen tp->iss = tcp_new_isn(&inp->inp_inc); 16308e02b4e0SMichael Tuexen if (tp->t_flags & TF_REQ_TSTMP) 16318e02b4e0SMichael Tuexen tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc); 1632fb59c426SYoshinobu Inoue tcp_sendseqinit(tp); 1633fb59c426SYoshinobu Inoue 1634fb59c426SYoshinobu Inoue return 0; 1635fa046d87SRobert Watson 1636fa046d87SRobert Watson out: 1637fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1638fa046d87SRobert Watson return error; 1639fb59c426SYoshinobu Inoue } 1640fb59c426SYoshinobu Inoue #endif /* INET6 */ 1641fb59c426SYoshinobu Inoue 1642cfe8b629SGarrett Wollman /* 1643b8af5dfaSRobert Watson * Export TCP internal state information via a struct tcp_info, based on the 1644b8af5dfaSRobert Watson * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1645b8af5dfaSRobert Watson * (TCP state machine, etc). We export all information using FreeBSD-native 1646b8af5dfaSRobert Watson * constants -- for example, the numeric values for tcpi_state will differ 1647b8af5dfaSRobert Watson * from Linux. 1648b8af5dfaSRobert Watson */ 1649b8af5dfaSRobert Watson static void 1650ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) 1651b8af5dfaSRobert Watson { 1652b8af5dfaSRobert Watson 16538501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1654b8af5dfaSRobert Watson bzero(ti, sizeof(*ti)); 1655b8af5dfaSRobert Watson 1656b8af5dfaSRobert Watson ti->tcpi_state = tp->t_state; 1657b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1658b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 16593529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 1660b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_SACK; 1661b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1662b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_WSCALE; 1663b8af5dfaSRobert Watson ti->tcpi_snd_wscale = tp->snd_scale; 1664b8af5dfaSRobert Watson ti->tcpi_rcv_wscale = tp->rcv_scale; 1665b8af5dfaSRobert Watson } 16663cf38784SMichael Tuexen if (tp->t_flags2 & TF2_ECN_PERMIT) 16675a17b6adSMichael Tuexen ti->tcpi_options |= TCPI_OPT_ECN; 16681baaf834SBruce M Simpson 166943d94734SJohn Baldwin ti->tcpi_rto = tp->t_rxtcur * tick; 16703ac12506SJonathan T. Looney ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick; 16711baaf834SBruce M Simpson ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 16721baaf834SBruce M Simpson ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT; 16731baaf834SBruce M Simpson 1674b8af5dfaSRobert Watson ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1675b8af5dfaSRobert Watson ti->tcpi_snd_cwnd = tp->snd_cwnd; 1676b8af5dfaSRobert Watson 1677b8af5dfaSRobert Watson /* 1678b8af5dfaSRobert Watson * FreeBSD-specific extension fields for tcp_info. 1679b8af5dfaSRobert Watson */ 1680c8443a1dSRobert Watson ti->tcpi_rcv_space = tp->rcv_wnd; 1681535fbad6SKip Macy ti->tcpi_rcv_nxt = tp->rcv_nxt; 1682b8af5dfaSRobert Watson ti->tcpi_snd_wnd = tp->snd_wnd; 16831c18314dSAndre Oppermann ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */ 1684535fbad6SKip Macy ti->tcpi_snd_nxt = tp->snd_nxt; 168543d94734SJohn Baldwin ti->tcpi_snd_mss = tp->t_maxseg; 168643d94734SJohn Baldwin ti->tcpi_rcv_mss = tp->t_maxseg; 1687f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack; 1688f5d34df5SGeorge V. Neville-Neil ti->tcpi_rcv_ooopack = tp->t_rcvoopack; 1689f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_zerowin = tp->t_sndzerowin; 1690a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD 1691a6456410SNavdeep Parhar if (tp->t_flags & TF_TOE) { 1692a6456410SNavdeep Parhar ti->tcpi_options |= TCPI_OPT_TOE; 1693a6456410SNavdeep Parhar tcp_offload_tcp_info(tp, ti); 1694a6456410SNavdeep Parhar } 1695a6456410SNavdeep Parhar #endif 1696b8af5dfaSRobert Watson } 1697b8af5dfaSRobert Watson 1698b8af5dfaSRobert Watson /* 16991e8f5ffaSRobert Watson * tcp_ctloutput() must drop the inpcb lock before performing copyin on 17001e8f5ffaSRobert Watson * socket option arguments. When it re-acquires the lock after the copy, it 17011e8f5ffaSRobert Watson * has to revalidate that the connection is still valid for the socket 17021e8f5ffaSRobert Watson * option. 1703cfe8b629SGarrett Wollman */ 1704bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \ 17058501a69cSRobert Watson INP_WLOCK(inp); \ 1706ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \ 17078501a69cSRobert Watson INP_WUNLOCK(inp); \ 1708bac5bedfSConrad Meyer cleanup; \ 17091e8f5ffaSRobert Watson return (ECONNRESET); \ 17101e8f5ffaSRobert Watson } \ 17111e8f5ffaSRobert Watson tp = intotcpcb(inp); \ 17121e8f5ffaSRobert Watson } while(0) 1713bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */) 17141e8f5ffaSRobert Watson 1715fc4d53ccSGleb Smirnoff static int 1716fc4d53ccSGleb Smirnoff tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt) 1717df8bae1dSRodney W. Grimes { 1718fc4d53ccSGleb Smirnoff struct tcpcb *tp = intotcpcb(inp); 1719fc4d53ccSGleb Smirnoff int error = 0; 1720df8bae1dSRodney W. Grimes 1721fc4d53ccSGleb Smirnoff MPASS(sopt->sopt_dir == SOPT_SET); 1722fc4d53ccSGleb Smirnoff 1723cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_TCP) { 1724fb59c426SYoshinobu Inoue #ifdef INET6 1725de156263SGleb Smirnoff if (inp->inp_vflag & INP_IPV6PROTO) 1726fc4d53ccSGleb Smirnoff error = ip6_ctloutput(inp->inp_socket, sopt); 1727de156263SGleb Smirnoff #endif 1728de156263SGleb Smirnoff #if defined(INET6) && defined(INET) 1729de156263SGleb Smirnoff else 1730de156263SGleb Smirnoff #endif 1731de156263SGleb Smirnoff #ifdef INET 1732de156263SGleb Smirnoff error = ip_ctloutput(inp->inp_socket, sopt); 1733de156263SGleb Smirnoff #endif 17345dff1c38SMichael Tuexen /* 1735de156263SGleb Smirnoff * When an IP-level socket option affects TCP, pass control 1736de156263SGleb Smirnoff * down to stack tfb_tcp_ctloutput, otherwise return what 1737de156263SGleb Smirnoff * IP level returned. 17385dff1c38SMichael Tuexen */ 1739de156263SGleb Smirnoff switch (sopt->sopt_level) { 1740de156263SGleb Smirnoff #ifdef INET6 1741de156263SGleb Smirnoff case IPPROTO_IPV6: 1742de156263SGleb Smirnoff if ((inp->inp_vflag & INP_IPV6PROTO) == 0) 1743de156263SGleb Smirnoff return (error); 1744de156263SGleb Smirnoff switch (sopt->sopt_name) { 1745de156263SGleb Smirnoff case IPV6_TCLASS: 1746de156263SGleb Smirnoff /* Notify tcp stacks that care (e.g. RACK). */ 1747de156263SGleb Smirnoff break; 1748de156263SGleb Smirnoff case IPV6_USE_MIN_MTU: 1749f581a26eSGleb Smirnoff /* Update t_maxseg accordingly. */ 1750f581a26eSGleb Smirnoff break; 1751de156263SGleb Smirnoff default: 1752de156263SGleb Smirnoff return (error); 17535dff1c38SMichael Tuexen } 1754de156263SGleb Smirnoff break; 1755b287c6c7SBjoern A. Zeeb #endif 1756b287c6c7SBjoern A. Zeeb #ifdef INET 1757de156263SGleb Smirnoff case IPPROTO_IP: 1758de156263SGleb Smirnoff switch (sopt->sopt_name) { 1759de156263SGleb Smirnoff case IP_TOS: 1760de156263SGleb Smirnoff case IP_TTL: 1761de156263SGleb Smirnoff /* Notify tcp stacks that care (e.g. RACK). */ 1762de156263SGleb Smirnoff break; 1763de156263SGleb Smirnoff default: 1764df8bae1dSRodney W. Grimes return (error); 1765de156263SGleb Smirnoff } 1766de156263SGleb Smirnoff break; 1767de156263SGleb Smirnoff #endif 1768de156263SGleb Smirnoff default: 1769de156263SGleb Smirnoff return (error); 1770de156263SGleb Smirnoff } 1771fc4d53ccSGleb Smirnoff } else if (sopt->sopt_name == TCP_FUNCTION_BLK) { 1772fc4d53ccSGleb Smirnoff /* 1773fc4d53ccSGleb Smirnoff * Protect the TCP option TCP_FUNCTION_BLK so 1774fc4d53ccSGleb Smirnoff * that a sub-function can *never* overwrite this. 1775fc4d53ccSGleb Smirnoff */ 1776fc4d53ccSGleb Smirnoff struct tcp_function_set fsn; 1777fc4d53ccSGleb Smirnoff struct tcp_function_block *blk; 1778fc4d53ccSGleb Smirnoff 1779fc4d53ccSGleb Smirnoff error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn); 1780fc4d53ccSGleb Smirnoff if (error) 1781fc4d53ccSGleb Smirnoff return (error); 1782fc4d53ccSGleb Smirnoff 178368cea2b1SJohn Baldwin INP_WLOCK(inp); 1784ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 17858501a69cSRobert Watson INP_WUNLOCK(inp); 17861e8f5ffaSRobert Watson return (ECONNRESET); 1787623dce13SRobert Watson } 178855bceb1eSRandall Stewart tp = intotcpcb(inp); 1789fc4d53ccSGleb Smirnoff 179055bceb1eSRandall Stewart blk = find_and_ref_tcp_functions(&fsn); 179155bceb1eSRandall Stewart if (blk == NULL) { 179255bceb1eSRandall Stewart INP_WUNLOCK(inp); 179355bceb1eSRandall Stewart return (ENOENT); 179455bceb1eSRandall Stewart } 1795587d67c0SRandall Stewart if (tp->t_fb == blk) { 1796587d67c0SRandall Stewart /* You already have this */ 1797587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1798587d67c0SRandall Stewart INP_WUNLOCK(inp); 1799587d67c0SRandall Stewart return (0); 1800587d67c0SRandall Stewart } 1801587d67c0SRandall Stewart if (tp->t_state != TCPS_CLOSED) { 1802587d67c0SRandall Stewart /* 1803587d67c0SRandall Stewart * The user has advanced the state 1804587d67c0SRandall Stewart * past the initial point, we may not 1805587d67c0SRandall Stewart * be able to switch. 1806587d67c0SRandall Stewart */ 1807587d67c0SRandall Stewart if (blk->tfb_tcp_handoff_ok != NULL) { 1808587d67c0SRandall Stewart /* 1809587d67c0SRandall Stewart * Does the stack provide a 1810587d67c0SRandall Stewart * query mechanism, if so it may 1811587d67c0SRandall Stewart * still be possible? 1812587d67c0SRandall Stewart */ 1813587d67c0SRandall Stewart error = (*blk->tfb_tcp_handoff_ok)(tp); 1814c6c0be27SMichael Tuexen } else 1815c6c0be27SMichael Tuexen error = EINVAL; 1816587d67c0SRandall Stewart if (error) { 1817587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1818587d67c0SRandall Stewart INP_WUNLOCK(inp); 1819587d67c0SRandall Stewart return(error); 1820587d67c0SRandall Stewart } 1821587d67c0SRandall Stewart } 182255bceb1eSRandall Stewart if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) { 182355bceb1eSRandall Stewart refcount_release(&blk->tfb_refcnt); 182455bceb1eSRandall Stewart INP_WUNLOCK(inp); 182555bceb1eSRandall Stewart return (ENOENT); 182655bceb1eSRandall Stewart } 182755bceb1eSRandall Stewart /* 182855bceb1eSRandall Stewart * Release the old refcnt, the 1829587d67c0SRandall Stewart * lookup acquired a ref on the 1830587d67c0SRandall Stewart * new one already. 183155bceb1eSRandall Stewart */ 1832587d67c0SRandall Stewart if (tp->t_fb->tfb_tcp_fb_fini) { 1833086a3556SAndrew Gallatin struct epoch_tracker et; 1834587d67c0SRandall Stewart /* 1835587d67c0SRandall Stewart * Tell the stack to cleanup with 0 i.e. 1836587d67c0SRandall Stewart * the tcb is not going away. 1837587d67c0SRandall Stewart */ 1838086a3556SAndrew Gallatin NET_EPOCH_ENTER(et); 1839587d67c0SRandall Stewart (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0); 1840086a3556SAndrew Gallatin NET_EPOCH_EXIT(et); 1841587d67c0SRandall Stewart } 18423ee9c3c4SRandall Stewart #ifdef TCPHPTS 18433ee9c3c4SRandall Stewart /* Assure that we are not on any hpts */ 18443ee9c3c4SRandall Stewart tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL); 18453ee9c3c4SRandall Stewart #endif 18463ee9c3c4SRandall Stewart if (blk->tfb_tcp_fb_init) { 18473ee9c3c4SRandall Stewart error = (*blk->tfb_tcp_fb_init)(tp); 18483ee9c3c4SRandall Stewart if (error) { 18493ee9c3c4SRandall Stewart refcount_release(&blk->tfb_refcnt); 18503ee9c3c4SRandall Stewart if (tp->t_fb->tfb_tcp_fb_init) { 18513ee9c3c4SRandall Stewart if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) { 18523ee9c3c4SRandall Stewart /* Fall back failed, drop the connection */ 18533ee9c3c4SRandall Stewart INP_WUNLOCK(inp); 1854fc4d53ccSGleb Smirnoff soabort(inp->inp_socket); 18553ee9c3c4SRandall Stewart return(error); 18563ee9c3c4SRandall Stewart } 18573ee9c3c4SRandall Stewart } 18583ee9c3c4SRandall Stewart goto err_out; 18593ee9c3c4SRandall Stewart } 18603ee9c3c4SRandall Stewart } 186155bceb1eSRandall Stewart refcount_release(&tp->t_fb->tfb_refcnt); 186255bceb1eSRandall Stewart tp->t_fb = blk; 186355bceb1eSRandall Stewart #ifdef TCP_OFFLOAD 186455bceb1eSRandall Stewart if (tp->t_flags & TF_TOE) { 186555bceb1eSRandall Stewart tcp_offload_ctloutput(tp, sopt->sopt_dir, 186655bceb1eSRandall Stewart sopt->sopt_name); 186755bceb1eSRandall Stewart } 186855bceb1eSRandall Stewart #endif 18693ee9c3c4SRandall Stewart err_out: 187055bceb1eSRandall Stewart INP_WUNLOCK(inp); 187155bceb1eSRandall Stewart return (error); 1872fc4d53ccSGleb Smirnoff } 1873fc4d53ccSGleb Smirnoff 1874fc4d53ccSGleb Smirnoff INP_WLOCK(inp); 1875fc4d53ccSGleb Smirnoff if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 1876fc4d53ccSGleb Smirnoff INP_WUNLOCK(inp); 1877fc4d53ccSGleb Smirnoff return (ECONNRESET); 1878fc4d53ccSGleb Smirnoff } 1879fc4d53ccSGleb Smirnoff tp = intotcpcb(inp); 1880fc4d53ccSGleb Smirnoff 1881fc4d53ccSGleb Smirnoff /* Pass in the INP locked, caller must unlock it. */ 1882fc4d53ccSGleb Smirnoff return (tp->t_fb->tfb_tcp_ctloutput(inp->inp_socket, sopt, inp, tp)); 1883fc4d53ccSGleb Smirnoff } 1884fc4d53ccSGleb Smirnoff 1885fc4d53ccSGleb Smirnoff static int 1886fc4d53ccSGleb Smirnoff tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt) 1887fc4d53ccSGleb Smirnoff { 1888fc4d53ccSGleb Smirnoff int error = 0; 1889fc4d53ccSGleb Smirnoff struct tcpcb *tp; 1890fc4d53ccSGleb Smirnoff 1891fc4d53ccSGleb Smirnoff MPASS(sopt->sopt_dir == SOPT_GET); 1892fc4d53ccSGleb Smirnoff 1893fc4d53ccSGleb Smirnoff if (sopt->sopt_level != IPPROTO_TCP) { 1894fc4d53ccSGleb Smirnoff #ifdef INET6 1895fc4d53ccSGleb Smirnoff if (inp->inp_vflag & INP_IPV6PROTO) 1896fc4d53ccSGleb Smirnoff error = ip6_ctloutput(inp->inp_socket, sopt); 1897fc4d53ccSGleb Smirnoff #endif /* INET6 */ 1898fc4d53ccSGleb Smirnoff #if defined(INET6) && defined(INET) 1899fc4d53ccSGleb Smirnoff else 1900fc4d53ccSGleb Smirnoff #endif 1901fc4d53ccSGleb Smirnoff #ifdef INET 1902fc4d53ccSGleb Smirnoff error = ip_ctloutput(inp->inp_socket, sopt); 1903fc4d53ccSGleb Smirnoff #endif 1904fc4d53ccSGleb Smirnoff return (error); 1905fc4d53ccSGleb Smirnoff } 1906fc4d53ccSGleb Smirnoff INP_WLOCK(inp); 1907fc4d53ccSGleb Smirnoff if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 1908fc4d53ccSGleb Smirnoff INP_WUNLOCK(inp); 1909fc4d53ccSGleb Smirnoff return (ECONNRESET); 1910fc4d53ccSGleb Smirnoff } 1911fc4d53ccSGleb Smirnoff tp = intotcpcb(inp); 1912fc4d53ccSGleb Smirnoff if (((sopt->sopt_name == TCP_FUNCTION_BLK) || 1913e2833083SPeter Lei (sopt->sopt_name == TCP_FUNCTION_ALIAS))) { 1914fc4d53ccSGleb Smirnoff struct tcp_function_set fsn; 1915fc4d53ccSGleb Smirnoff 1916e2833083SPeter Lei if (sopt->sopt_name == TCP_FUNCTION_ALIAS) { 1917e2833083SPeter Lei memset(&fsn, 0, sizeof(fsn)); 1918e2833083SPeter Lei find_tcp_function_alias(tp->t_fb, &fsn); 1919e2833083SPeter Lei } else { 1920e2833083SPeter Lei strncpy(fsn.function_set_name, 1921e2833083SPeter Lei tp->t_fb->tfb_tcp_block_name, 1922c73b6f4dSEd Maste TCP_FUNCTION_NAME_LEN_MAX); 1923c73b6f4dSEd Maste fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0'; 1924e2833083SPeter Lei } 192555bceb1eSRandall Stewart fsn.pcbcnt = tp->t_fb->tfb_refcnt; 192655bceb1eSRandall Stewart INP_WUNLOCK(inp); 192755bceb1eSRandall Stewart error = sooptcopyout(sopt, &fsn, sizeof fsn); 192855bceb1eSRandall Stewart return (error); 192955bceb1eSRandall Stewart } 1930fc4d53ccSGleb Smirnoff 1931fc4d53ccSGleb Smirnoff /* Pass in the INP locked, caller must unlock it. */ 1932fc4d53ccSGleb Smirnoff return (tp->t_fb->tfb_tcp_ctloutput(inp->inp_socket, sopt, inp, tp)); 1933fc4d53ccSGleb Smirnoff } 1934fc4d53ccSGleb Smirnoff 1935fc4d53ccSGleb Smirnoff int 1936fc4d53ccSGleb Smirnoff tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1937fc4d53ccSGleb Smirnoff { 1938fc4d53ccSGleb Smirnoff int error; 1939fc4d53ccSGleb Smirnoff struct inpcb *inp; 1940fc4d53ccSGleb Smirnoff 1941fc4d53ccSGleb Smirnoff error = 0; 1942fc4d53ccSGleb Smirnoff inp = sotoinpcb(so); 1943fc4d53ccSGleb Smirnoff KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 1944fc4d53ccSGleb Smirnoff 1945fc4d53ccSGleb Smirnoff if (sopt->sopt_dir == SOPT_SET) 1946fc4d53ccSGleb Smirnoff return (tcp_ctloutput_set(inp, sopt)); 1947fc4d53ccSGleb Smirnoff else if (sopt->sopt_dir == SOPT_GET) 1948fc4d53ccSGleb Smirnoff return (tcp_ctloutput_get(inp, sopt)); 1949fc4d53ccSGleb Smirnoff else 1950fc4d53ccSGleb Smirnoff panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir); 195155bceb1eSRandall Stewart } 195255bceb1eSRandall Stewart 19532529f56eSJonathan T. Looney /* 19542529f56eSJonathan T. Looney * If this assert becomes untrue, we need to change the size of the buf 19552529f56eSJonathan T. Looney * variable in tcp_default_ctloutput(). 19562529f56eSJonathan T. Looney */ 19572529f56eSJonathan T. Looney #ifdef CTASSERT 19582529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN); 19592529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN); 19602529f56eSJonathan T. Looney #endif 19612529f56eSJonathan T. Looney 1962ec1db6e1SJohn Baldwin #ifdef KERN_TLS 1963ec1db6e1SJohn Baldwin static int 1964ec1db6e1SJohn Baldwin copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls) 1965ec1db6e1SJohn Baldwin { 1966ec1db6e1SJohn Baldwin struct tls_enable_v0 tls_v0; 1967ec1db6e1SJohn Baldwin int error; 1968ec1db6e1SJohn Baldwin 1969ec1db6e1SJohn Baldwin if (sopt->sopt_valsize == sizeof(tls_v0)) { 1970ec1db6e1SJohn Baldwin error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0), 1971ec1db6e1SJohn Baldwin sizeof(tls_v0)); 1972ec1db6e1SJohn Baldwin if (error) 1973ec1db6e1SJohn Baldwin return (error); 1974ec1db6e1SJohn Baldwin memset(tls, 0, sizeof(*tls)); 1975ec1db6e1SJohn Baldwin tls->cipher_key = tls_v0.cipher_key; 1976ec1db6e1SJohn Baldwin tls->iv = tls_v0.iv; 1977ec1db6e1SJohn Baldwin tls->auth_key = tls_v0.auth_key; 1978ec1db6e1SJohn Baldwin tls->cipher_algorithm = tls_v0.cipher_algorithm; 1979ec1db6e1SJohn Baldwin tls->cipher_key_len = tls_v0.cipher_key_len; 1980ec1db6e1SJohn Baldwin tls->iv_len = tls_v0.iv_len; 1981ec1db6e1SJohn Baldwin tls->auth_algorithm = tls_v0.auth_algorithm; 1982ec1db6e1SJohn Baldwin tls->auth_key_len = tls_v0.auth_key_len; 1983ec1db6e1SJohn Baldwin tls->flags = tls_v0.flags; 1984ec1db6e1SJohn Baldwin tls->tls_vmajor = tls_v0.tls_vmajor; 1985ec1db6e1SJohn Baldwin tls->tls_vminor = tls_v0.tls_vminor; 1986ec1db6e1SJohn Baldwin return (0); 1987ec1db6e1SJohn Baldwin } 1988ec1db6e1SJohn Baldwin 1989ec1db6e1SJohn Baldwin return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls))); 1990ec1db6e1SJohn Baldwin } 1991ec1db6e1SJohn Baldwin #endif 1992ec1db6e1SJohn Baldwin 1993b8d60729SRandall Stewart extern struct cc_algo newreno_cc_algo; 1994b8d60729SRandall Stewart 1995b8d60729SRandall Stewart static int 1996b8d60729SRandall Stewart tcp_congestion(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp) 1997b8d60729SRandall Stewart { 1998b8d60729SRandall Stewart struct cc_algo *algo; 1999b8d60729SRandall Stewart void *ptr = NULL; 2000b8d60729SRandall Stewart struct cc_var cc_mem; 2001b8d60729SRandall Stewart char buf[TCP_CA_NAME_MAX]; 2002b8d60729SRandall Stewart size_t mem_sz; 2003b8d60729SRandall Stewart int error; 2004b8d60729SRandall Stewart 2005b8d60729SRandall Stewart INP_WUNLOCK(inp); 2006b8d60729SRandall Stewart error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1); 2007b8d60729SRandall Stewart if (error) 2008b8d60729SRandall Stewart return(error); 2009b8d60729SRandall Stewart buf[sopt->sopt_valsize] = '\0'; 2010b8d60729SRandall Stewart CC_LIST_RLOCK(); 2011b8d60729SRandall Stewart STAILQ_FOREACH(algo, &cc_list, entries) 2012b8d60729SRandall Stewart if (strncmp(buf, algo->name, 2013b8d60729SRandall Stewart TCP_CA_NAME_MAX) == 0) { 2014b8d60729SRandall Stewart if (algo->flags & CC_MODULE_BEING_REMOVED) { 2015b8d60729SRandall Stewart /* We can't "see" modules being unloaded */ 2016b8d60729SRandall Stewart continue; 2017b8d60729SRandall Stewart } 2018b8d60729SRandall Stewart break; 2019b8d60729SRandall Stewart } 2020b8d60729SRandall Stewart if (algo == NULL) { 2021b8d60729SRandall Stewart CC_LIST_RUNLOCK(); 2022b8d60729SRandall Stewart return(ESRCH); 2023b8d60729SRandall Stewart } 2024b8d60729SRandall Stewart do_over: 2025b8d60729SRandall Stewart if (algo->cb_init != NULL) { 2026b8d60729SRandall Stewart /* We can now pre-get the memory for the CC */ 2027b8d60729SRandall Stewart mem_sz = (*algo->cc_data_sz)(); 2028b8d60729SRandall Stewart if (mem_sz == 0) { 2029b8d60729SRandall Stewart goto no_mem_needed; 2030b8d60729SRandall Stewart } 2031b8d60729SRandall Stewart CC_LIST_RUNLOCK(); 2032b8d60729SRandall Stewart ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK); 2033b8d60729SRandall Stewart CC_LIST_RLOCK(); 2034b8d60729SRandall Stewart STAILQ_FOREACH(algo, &cc_list, entries) 2035b8d60729SRandall Stewart if (strncmp(buf, algo->name, 2036b8d60729SRandall Stewart TCP_CA_NAME_MAX) == 0) 2037b8d60729SRandall Stewart break; 2038b8d60729SRandall Stewart if (algo == NULL) { 2039b8d60729SRandall Stewart if (ptr) 2040b8d60729SRandall Stewart free(ptr, M_CC_MEM); 2041b8d60729SRandall Stewart CC_LIST_RUNLOCK(); 2042b8d60729SRandall Stewart return(ESRCH); 2043b8d60729SRandall Stewart } 2044b8d60729SRandall Stewart } else { 2045b8d60729SRandall Stewart no_mem_needed: 2046b8d60729SRandall Stewart mem_sz = 0; 2047b8d60729SRandall Stewart ptr = NULL; 2048b8d60729SRandall Stewart } 2049b8d60729SRandall Stewart /* 2050b8d60729SRandall Stewart * Make sure its all clean and zero and also get 2051b8d60729SRandall Stewart * back the inplock. 2052b8d60729SRandall Stewart */ 2053b8d60729SRandall Stewart memset(&cc_mem, 0, sizeof(cc_mem)); 2054b8d60729SRandall Stewart if (mem_sz != (*algo->cc_data_sz)()) { 2055b8d60729SRandall Stewart if (ptr) 2056b8d60729SRandall Stewart free(ptr, M_CC_MEM); 2057b8d60729SRandall Stewart goto do_over; 2058b8d60729SRandall Stewart } 2059df07bfdaSMichael Tuexen INP_WLOCK(inp); 2060df07bfdaSMichael Tuexen if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 2061df07bfdaSMichael Tuexen INP_WUNLOCK(inp); 2062df07bfdaSMichael Tuexen CC_LIST_RUNLOCK(); 2063df07bfdaSMichael Tuexen free(ptr, M_CC_MEM); 2064df07bfdaSMichael Tuexen return (ECONNRESET); 2065df07bfdaSMichael Tuexen } 2066df07bfdaSMichael Tuexen tp = intotcpcb(inp); 2067df07bfdaSMichael Tuexen if (ptr != NULL) 2068b8d60729SRandall Stewart memset(ptr, 0, mem_sz); 2069b8d60729SRandall Stewart CC_LIST_RUNLOCK(); 2070b8d60729SRandall Stewart cc_mem.ccvc.tcp = tp; 2071b8d60729SRandall Stewart /* 2072b8d60729SRandall Stewart * We once again hold a write lock over the tcb so it's 2073b8d60729SRandall Stewart * safe to do these things without ordering concerns. 2074b8d60729SRandall Stewart * Note here we init into stack memory. 2075b8d60729SRandall Stewart */ 2076b8d60729SRandall Stewart if (algo->cb_init != NULL) 2077b8d60729SRandall Stewart error = algo->cb_init(&cc_mem, ptr); 2078b8d60729SRandall Stewart else 2079b8d60729SRandall Stewart error = 0; 2080b8d60729SRandall Stewart /* 2081b8d60729SRandall Stewart * The CC algorithms, when given their memory 2082b8d60729SRandall Stewart * should not fail we could in theory have a 2083b8d60729SRandall Stewart * KASSERT here. 2084b8d60729SRandall Stewart */ 2085b8d60729SRandall Stewart if (error == 0) { 2086b8d60729SRandall Stewart /* 2087b8d60729SRandall Stewart * Touchdown, lets go ahead and move the 2088b8d60729SRandall Stewart * connection to the new CC module by 2089b8d60729SRandall Stewart * copying in the cc_mem after we call 2090b8d60729SRandall Stewart * the old ones cleanup (if any). 2091b8d60729SRandall Stewart */ 2092b8d60729SRandall Stewart if (CC_ALGO(tp)->cb_destroy != NULL) 2093b8d60729SRandall Stewart CC_ALGO(tp)->cb_destroy(tp->ccv); 2094b8d60729SRandall Stewart memcpy(tp->ccv, &cc_mem, sizeof(struct cc_var)); 2095b8d60729SRandall Stewart tp->cc_algo = algo; 2096b8d60729SRandall Stewart /* Ok now are we where we have gotten past any conn_init? */ 2097b8d60729SRandall Stewart if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) { 2098b8d60729SRandall Stewart /* Yep run the connection init for the new CC */ 2099b8d60729SRandall Stewart CC_ALGO(tp)->conn_init(tp->ccv); 2100b8d60729SRandall Stewart } 2101b8d60729SRandall Stewart } else if (ptr) 2102b8d60729SRandall Stewart free(ptr, M_CC_MEM); 2103b8d60729SRandall Stewart INP_WUNLOCK(inp); 2104b8d60729SRandall Stewart return (error); 2105b8d60729SRandall Stewart } 2106b8d60729SRandall Stewart 210755bceb1eSRandall Stewart int 210855bceb1eSRandall Stewart tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp) 210955bceb1eSRandall Stewart { 211055bceb1eSRandall Stewart int error, opt, optval; 211155bceb1eSRandall Stewart u_int ui; 211255bceb1eSRandall Stewart struct tcp_info ti; 2113b2e60773SJohn Baldwin #ifdef KERN_TLS 2114b2e60773SJohn Baldwin struct tls_enable tls; 2115b2e60773SJohn Baldwin #endif 21162529f56eSJonathan T. Looney char *pbuf, buf[TCP_LOG_ID_LEN]; 2117adc56f5aSEdward Tomasz Napierala #ifdef STATS 2118adc56f5aSEdward Tomasz Napierala struct statsblob *sbp; 2119adc56f5aSEdward Tomasz Napierala #endif 2120af6fef3aSGleb Smirnoff size_t len; 2121df8bae1dSRodney W. Grimes 2122f581a26eSGleb Smirnoff INP_WLOCK_ASSERT(inp); 2123f581a26eSGleb Smirnoff 2124f581a26eSGleb Smirnoff switch (sopt->sopt_level) { 2125f581a26eSGleb Smirnoff #ifdef INET6 2126f581a26eSGleb Smirnoff case IPPROTO_IPV6: 2127f581a26eSGleb Smirnoff MPASS(inp->inp_vflag & INP_IPV6PROTO); 2128f581a26eSGleb Smirnoff switch (sopt->sopt_name) { 2129f581a26eSGleb Smirnoff case IPV6_USE_MIN_MTU: 2130f581a26eSGleb Smirnoff tcp6_use_min_mtu(tp); 2131f581a26eSGleb Smirnoff /* FALLTHROUGH */ 2132f581a26eSGleb Smirnoff } 2133f581a26eSGleb Smirnoff INP_WUNLOCK(inp); 2134f581a26eSGleb Smirnoff return (0); 2135f581a26eSGleb Smirnoff #endif 2136f581a26eSGleb Smirnoff #ifdef INET 2137f581a26eSGleb Smirnoff case IPPROTO_IP: 2138f581a26eSGleb Smirnoff INP_WUNLOCK(inp); 2139f581a26eSGleb Smirnoff return (0); 2140f581a26eSGleb Smirnoff #endif 2141f581a26eSGleb Smirnoff } 2142f581a26eSGleb Smirnoff 2143d519cedbSGleb Smirnoff /* 2144d519cedbSGleb Smirnoff * For TCP_CCALGOOPT forward the control to CC module, for both 2145d519cedbSGleb Smirnoff * SOPT_SET and SOPT_GET. 2146d519cedbSGleb Smirnoff */ 2147d519cedbSGleb Smirnoff switch (sopt->sopt_name) { 2148d519cedbSGleb Smirnoff case TCP_CCALGOOPT: 2149d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 2150c8b53cedSMichael Tuexen if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT) 2151c8b53cedSMichael Tuexen return (EINVAL); 2152af6fef3aSGleb Smirnoff pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO); 2153af6fef3aSGleb Smirnoff error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize, 2154d519cedbSGleb Smirnoff sopt->sopt_valsize); 2155d519cedbSGleb Smirnoff if (error) { 2156af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 2157d519cedbSGleb Smirnoff return (error); 2158d519cedbSGleb Smirnoff } 2159bac5bedfSConrad Meyer INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP)); 2160d519cedbSGleb Smirnoff if (CC_ALGO(tp)->ctl_output != NULL) 2161af6fef3aSGleb Smirnoff error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf); 2162d519cedbSGleb Smirnoff else 2163d519cedbSGleb Smirnoff error = ENOENT; 2164d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 2165d519cedbSGleb Smirnoff if (error == 0 && sopt->sopt_dir == SOPT_GET) 2166af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize); 2167af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 2168d519cedbSGleb Smirnoff return (error); 2169d519cedbSGleb Smirnoff } 2170d519cedbSGleb Smirnoff 2171cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 2172cfe8b629SGarrett Wollman case SOPT_SET: 2173cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2174fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 217588f6b043SBruce M Simpson case TCP_MD5SIG: 2176fcf59617SAndrey V. Elsukov if (!TCPMD5_ENABLED()) { 21778501a69cSRobert Watson INP_WUNLOCK(inp); 2178fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 2179fcf59617SAndrey V. Elsukov } 2180fcf59617SAndrey V. Elsukov error = TCPMD5_PCBCTL(inp, sopt); 21811cfd4b53SBruce M Simpson if (error) 21821e8f5ffaSRobert Watson return (error); 218309fe6320SNavdeep Parhar goto unlock_and_done; 2184fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 218509fe6320SNavdeep Parhar 2186df8bae1dSRodney W. Grimes case TCP_NODELAY: 2187cfe8b629SGarrett Wollman case TCP_NOOPT: 21880471a8c7SRichard Scheffenegger case TCP_LRD: 21898501a69cSRobert Watson INP_WUNLOCK(inp); 2190cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 2191cfe8b629SGarrett Wollman sizeof optval); 2192cfe8b629SGarrett Wollman if (error) 21931e8f5ffaSRobert Watson return (error); 2194cfe8b629SGarrett Wollman 21958501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 2196cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2197cfe8b629SGarrett Wollman case TCP_NODELAY: 2198cfe8b629SGarrett Wollman opt = TF_NODELAY; 2199cfe8b629SGarrett Wollman break; 2200cfe8b629SGarrett Wollman case TCP_NOOPT: 2201cfe8b629SGarrett Wollman opt = TF_NOOPT; 2202cfe8b629SGarrett Wollman break; 22030471a8c7SRichard Scheffenegger case TCP_LRD: 22040471a8c7SRichard Scheffenegger opt = TF_LRD; 22050471a8c7SRichard Scheffenegger break; 2206cfe8b629SGarrett Wollman default: 2207cfe8b629SGarrett Wollman opt = 0; /* dead code to fool gcc */ 2208cfe8b629SGarrett Wollman break; 2209cfe8b629SGarrett Wollman } 2210cfe8b629SGarrett Wollman 2211cfe8b629SGarrett Wollman if (optval) 2212cfe8b629SGarrett Wollman tp->t_flags |= opt; 2213df8bae1dSRodney W. Grimes else 2214cfe8b629SGarrett Wollman tp->t_flags &= ~opt; 221509fe6320SNavdeep Parhar unlock_and_done: 221609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 221709fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 221809fe6320SNavdeep Parhar tcp_offload_ctloutput(tp, sopt->sopt_dir, 221909fe6320SNavdeep Parhar sopt->sopt_name); 222009fe6320SNavdeep Parhar } 222109fe6320SNavdeep Parhar #endif 22228501a69cSRobert Watson INP_WUNLOCK(inp); 2223df8bae1dSRodney W. Grimes break; 2224df8bae1dSRodney W. Grimes 2225007581c0SJonathan Lemon case TCP_NOPUSH: 22268501a69cSRobert Watson INP_WUNLOCK(inp); 2227007581c0SJonathan Lemon error = sooptcopyin(sopt, &optval, sizeof optval, 2228007581c0SJonathan Lemon sizeof optval); 2229007581c0SJonathan Lemon if (error) 22301e8f5ffaSRobert Watson return (error); 2231007581c0SJonathan Lemon 22328501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 2233007581c0SJonathan Lemon if (optval) 2234007581c0SJonathan Lemon tp->t_flags |= TF_NOPUSH; 2235d28b9e89SJohn Baldwin else if (tp->t_flags & TF_NOPUSH) { 2236007581c0SJonathan Lemon tp->t_flags &= ~TF_NOPUSH; 2237109eb549SGleb Smirnoff if (TCPS_HAVEESTABLISHED(tp->t_state)) { 2238109eb549SGleb Smirnoff struct epoch_tracker et; 2239109eb549SGleb Smirnoff 2240109eb549SGleb Smirnoff NET_EPOCH_ENTER(et); 2241*40fa3e40SGleb Smirnoff error = tcp_output(tp); 2242109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 2243109eb549SGleb Smirnoff } 2244007581c0SJonathan Lemon } 224509fe6320SNavdeep Parhar goto unlock_and_done; 2246007581c0SJonathan Lemon 22479e644c23SMichael Tuexen case TCP_REMOTE_UDP_ENCAPS_PORT: 22489e644c23SMichael Tuexen INP_WUNLOCK(inp); 22499e644c23SMichael Tuexen error = sooptcopyin(sopt, &optval, sizeof optval, 22509e644c23SMichael Tuexen sizeof optval); 22519e644c23SMichael Tuexen if (error) 22529e644c23SMichael Tuexen return (error); 22539e644c23SMichael Tuexen if ((optval < TCP_TUNNELING_PORT_MIN) || 22549e644c23SMichael Tuexen (optval > TCP_TUNNELING_PORT_MAX)) { 22559e644c23SMichael Tuexen /* Its got to be in range */ 22569e644c23SMichael Tuexen return (EINVAL); 22579e644c23SMichael Tuexen } 22589e644c23SMichael Tuexen if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) { 22599e644c23SMichael Tuexen /* You have to have enabled a UDP tunneling port first */ 22609e644c23SMichael Tuexen return (EINVAL); 22619e644c23SMichael Tuexen } 22629e644c23SMichael Tuexen INP_WLOCK_RECHECK(inp); 22639e644c23SMichael Tuexen if (tp->t_state != TCPS_CLOSED) { 22649e644c23SMichael Tuexen /* You can't change after you are connected */ 22659e644c23SMichael Tuexen error = EINVAL; 22669e644c23SMichael Tuexen } else { 22679e644c23SMichael Tuexen /* Ok we are all good set the port */ 22689e644c23SMichael Tuexen tp->t_port = htons(optval); 22699e644c23SMichael Tuexen } 22709e644c23SMichael Tuexen goto unlock_and_done; 22719e644c23SMichael Tuexen 2272df8bae1dSRodney W. Grimes case TCP_MAXSEG: 22738501a69cSRobert Watson INP_WUNLOCK(inp); 2274cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 2275cfe8b629SGarrett Wollman sizeof optval); 2276cfe8b629SGarrett Wollman if (error) 22771e8f5ffaSRobert Watson return (error); 2278df8bae1dSRodney W. Grimes 22798501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 228053369ac9SAndre Oppermann if (optval > 0 && optval <= tp->t_maxseg && 2281603724d3SBjoern A. Zeeb optval + 40 >= V_tcp_minmss) 2282cfe8b629SGarrett Wollman tp->t_maxseg = optval; 2283a0292f23SGarrett Wollman else 2284a0292f23SGarrett Wollman error = EINVAL; 228509fe6320SNavdeep Parhar goto unlock_and_done; 2286a0292f23SGarrett Wollman 2287b8af5dfaSRobert Watson case TCP_INFO: 22888501a69cSRobert Watson INP_WUNLOCK(inp); 2289b8af5dfaSRobert Watson error = EINVAL; 2290b8af5dfaSRobert Watson break; 2291b8af5dfaSRobert Watson 2292adc56f5aSEdward Tomasz Napierala case TCP_STATS: 2293adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2294adc56f5aSEdward Tomasz Napierala #ifdef STATS 2295adc56f5aSEdward Tomasz Napierala error = sooptcopyin(sopt, &optval, sizeof optval, 2296adc56f5aSEdward Tomasz Napierala sizeof optval); 2297adc56f5aSEdward Tomasz Napierala if (error) 2298adc56f5aSEdward Tomasz Napierala return (error); 2299adc56f5aSEdward Tomasz Napierala 2300adc56f5aSEdward Tomasz Napierala if (optval > 0) 2301adc56f5aSEdward Tomasz Napierala sbp = stats_blob_alloc( 2302adc56f5aSEdward Tomasz Napierala V_tcp_perconn_stats_dflt_tpl, 0); 2303adc56f5aSEdward Tomasz Napierala else 2304adc56f5aSEdward Tomasz Napierala sbp = NULL; 2305adc56f5aSEdward Tomasz Napierala 2306adc56f5aSEdward Tomasz Napierala INP_WLOCK_RECHECK(inp); 2307adc56f5aSEdward Tomasz Napierala if ((tp->t_stats != NULL && sbp == NULL) || 2308adc56f5aSEdward Tomasz Napierala (tp->t_stats == NULL && sbp != NULL)) { 2309adc56f5aSEdward Tomasz Napierala struct statsblob *t = tp->t_stats; 2310adc56f5aSEdward Tomasz Napierala tp->t_stats = sbp; 2311adc56f5aSEdward Tomasz Napierala sbp = t; 2312adc56f5aSEdward Tomasz Napierala } 2313adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2314adc56f5aSEdward Tomasz Napierala 2315adc56f5aSEdward Tomasz Napierala stats_blob_destroy(sbp); 2316adc56f5aSEdward Tomasz Napierala #else 2317adc56f5aSEdward Tomasz Napierala return (EOPNOTSUPP); 2318adc56f5aSEdward Tomasz Napierala #endif /* !STATS */ 2319adc56f5aSEdward Tomasz Napierala break; 2320adc56f5aSEdward Tomasz Napierala 2321dbc42409SLawrence Stewart case TCP_CONGESTION: 2322b8d60729SRandall Stewart error = tcp_congestion(so, sopt, inp, tp); 232373e263b1SGleb Smirnoff break; 2324dbc42409SLawrence Stewart 2325a034518aSAndrew Gallatin case TCP_REUSPORT_LB_NUMA: 2326a034518aSAndrew Gallatin INP_WUNLOCK(inp); 2327a034518aSAndrew Gallatin error = sooptcopyin(sopt, &optval, sizeof(optval), 2328a034518aSAndrew Gallatin sizeof(optval)); 2329a034518aSAndrew Gallatin INP_WLOCK_RECHECK(inp); 2330a034518aSAndrew Gallatin if (!error) 2331a034518aSAndrew Gallatin error = in_pcblbgroup_numa(inp, optval); 2332a034518aSAndrew Gallatin INP_WUNLOCK(inp); 2333a034518aSAndrew Gallatin break; 2334a034518aSAndrew Gallatin 2335b2e60773SJohn Baldwin #ifdef KERN_TLS 2336b2e60773SJohn Baldwin case TCP_TXTLS_ENABLE: 2337b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2338ec1db6e1SJohn Baldwin error = copyin_tls_enable(sopt, &tls); 2339b2e60773SJohn Baldwin if (error) 2340b2e60773SJohn Baldwin break; 2341b2e60773SJohn Baldwin error = ktls_enable_tx(so, &tls); 2342b2e60773SJohn Baldwin break; 2343b2e60773SJohn Baldwin case TCP_TXTLS_MODE: 2344b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2345b2e60773SJohn Baldwin error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 2346b2e60773SJohn Baldwin if (error) 2347b2e60773SJohn Baldwin return (error); 2348b2e60773SJohn Baldwin 2349b2e60773SJohn Baldwin INP_WLOCK_RECHECK(inp); 2350b2e60773SJohn Baldwin error = ktls_set_tx_mode(so, ui); 2351b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2352b2e60773SJohn Baldwin break; 2353f1f93475SJohn Baldwin case TCP_RXTLS_ENABLE: 2354f1f93475SJohn Baldwin INP_WUNLOCK(inp); 2355f1f93475SJohn Baldwin error = sooptcopyin(sopt, &tls, sizeof(tls), 2356f1f93475SJohn Baldwin sizeof(tls)); 2357f1f93475SJohn Baldwin if (error) 2358f1f93475SJohn Baldwin break; 2359f1f93475SJohn Baldwin error = ktls_enable_rx(so, &tls); 2360f1f93475SJohn Baldwin break; 2361b2e60773SJohn Baldwin #endif 2362b2e60773SJohn Baldwin 23639077f387SGleb Smirnoff case TCP_KEEPIDLE: 23649077f387SGleb Smirnoff case TCP_KEEPINTVL: 23659077f387SGleb Smirnoff case TCP_KEEPINIT: 23669077f387SGleb Smirnoff INP_WUNLOCK(inp); 23679077f387SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 23689077f387SGleb Smirnoff if (error) 23699077f387SGleb Smirnoff return (error); 23709077f387SGleb Smirnoff 23719077f387SGleb Smirnoff if (ui > (UINT_MAX / hz)) { 23729077f387SGleb Smirnoff error = EINVAL; 23739077f387SGleb Smirnoff break; 23749077f387SGleb Smirnoff } 23759077f387SGleb Smirnoff ui *= hz; 23769077f387SGleb Smirnoff 23779077f387SGleb Smirnoff INP_WLOCK_RECHECK(inp); 23789077f387SGleb Smirnoff switch (sopt->sopt_name) { 23799077f387SGleb Smirnoff case TCP_KEEPIDLE: 23809077f387SGleb Smirnoff tp->t_keepidle = ui; 23819077f387SGleb Smirnoff /* 23829077f387SGleb Smirnoff * XXX: better check current remaining 23839077f387SGleb Smirnoff * timeout and "merge" it with new value. 23849077f387SGleb Smirnoff */ 23859077f387SGleb Smirnoff if ((tp->t_state > TCPS_LISTEN) && 23869077f387SGleb Smirnoff (tp->t_state <= TCPS_CLOSING)) 23879077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 23889077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 23899077f387SGleb Smirnoff break; 23909077f387SGleb Smirnoff case TCP_KEEPINTVL: 23919077f387SGleb Smirnoff tp->t_keepintvl = ui; 23929077f387SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 23939077f387SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 23949077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 23959077f387SGleb Smirnoff TP_MAXIDLE(tp)); 23969077f387SGleb Smirnoff break; 23979077f387SGleb Smirnoff case TCP_KEEPINIT: 23989077f387SGleb Smirnoff tp->t_keepinit = ui; 23999077f387SGleb Smirnoff if (tp->t_state == TCPS_SYN_RECEIVED || 24009077f387SGleb Smirnoff tp->t_state == TCPS_SYN_SENT) 24019077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 24029077f387SGleb Smirnoff TP_KEEPINIT(tp)); 24039077f387SGleb Smirnoff break; 24049077f387SGleb Smirnoff } 240509fe6320SNavdeep Parhar goto unlock_and_done; 24069077f387SGleb Smirnoff 240785c05144SGleb Smirnoff case TCP_KEEPCNT: 240885c05144SGleb Smirnoff INP_WUNLOCK(inp); 240985c05144SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 241085c05144SGleb Smirnoff if (error) 241185c05144SGleb Smirnoff return (error); 241285c05144SGleb Smirnoff 241385c05144SGleb Smirnoff INP_WLOCK_RECHECK(inp); 241485c05144SGleb Smirnoff tp->t_keepcnt = ui; 241585c05144SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 241685c05144SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 241785c05144SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 241885c05144SGleb Smirnoff TP_MAXIDLE(tp)); 241985c05144SGleb Smirnoff goto unlock_and_done; 242085c05144SGleb Smirnoff 242186a996e6SHiren Panchasara #ifdef TCPPCAP 242286a996e6SHiren Panchasara case TCP_PCAP_OUT: 242386a996e6SHiren Panchasara case TCP_PCAP_IN: 242486a996e6SHiren Panchasara INP_WUNLOCK(inp); 242586a996e6SHiren Panchasara error = sooptcopyin(sopt, &optval, sizeof optval, 242686a996e6SHiren Panchasara sizeof optval); 242786a996e6SHiren Panchasara if (error) 242886a996e6SHiren Panchasara return (error); 242986a996e6SHiren Panchasara 243086a996e6SHiren Panchasara INP_WLOCK_RECHECK(inp); 243186a996e6SHiren Panchasara if (optval >= 0) 243286a996e6SHiren Panchasara tcp_pcap_set_sock_max(TCP_PCAP_OUT ? 243386a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts), 243486a996e6SHiren Panchasara optval); 243586a996e6SHiren Panchasara else 243686a996e6SHiren Panchasara error = EINVAL; 243786a996e6SHiren Panchasara goto unlock_and_done; 243886a996e6SHiren Panchasara #endif 243986a996e6SHiren Panchasara 2440c560df6fSPatrick Kelsey case TCP_FASTOPEN: { 2441c560df6fSPatrick Kelsey struct tcp_fastopen tfo_optval; 2442c560df6fSPatrick Kelsey 2443281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 2444c560df6fSPatrick Kelsey if (!V_tcp_fastopen_client_enable && 2445c560df6fSPatrick Kelsey !V_tcp_fastopen_server_enable) 2446281a0fd4SPatrick Kelsey return (EPERM); 2447281a0fd4SPatrick Kelsey 2448c560df6fSPatrick Kelsey error = sooptcopyin(sopt, &tfo_optval, 2449c560df6fSPatrick Kelsey sizeof(tfo_optval), sizeof(int)); 2450281a0fd4SPatrick Kelsey if (error) 2451281a0fd4SPatrick Kelsey return (error); 2452281a0fd4SPatrick Kelsey 2453281a0fd4SPatrick Kelsey INP_WLOCK_RECHECK(inp); 2454d442a657SMichael Tuexen if ((tp->t_state != TCPS_CLOSED) && 2455d442a657SMichael Tuexen (tp->t_state != TCPS_LISTEN)) { 2456d442a657SMichael Tuexen error = EINVAL; 2457d442a657SMichael Tuexen goto unlock_and_done; 2458d442a657SMichael Tuexen } 2459c560df6fSPatrick Kelsey if (tfo_optval.enable) { 2460c560df6fSPatrick Kelsey if (tp->t_state == TCPS_LISTEN) { 2461c560df6fSPatrick Kelsey if (!V_tcp_fastopen_server_enable) { 2462c560df6fSPatrick Kelsey error = EPERM; 2463c560df6fSPatrick Kelsey goto unlock_and_done; 2464c560df6fSPatrick Kelsey } 2465c560df6fSPatrick Kelsey 2466c560df6fSPatrick Kelsey if (tp->t_tfo_pending == NULL) 2467281a0fd4SPatrick Kelsey tp->t_tfo_pending = 2468281a0fd4SPatrick Kelsey tcp_fastopen_alloc_counter(); 2469c560df6fSPatrick Kelsey } else { 2470c560df6fSPatrick Kelsey /* 2471c560df6fSPatrick Kelsey * If a pre-shared key was provided, 2472c560df6fSPatrick Kelsey * stash it in the client cookie 2473c560df6fSPatrick Kelsey * field of the tcpcb for use during 2474c560df6fSPatrick Kelsey * connect. 2475c560df6fSPatrick Kelsey */ 2476c560df6fSPatrick Kelsey if (sopt->sopt_valsize == 2477c560df6fSPatrick Kelsey sizeof(tfo_optval)) { 2478c560df6fSPatrick Kelsey memcpy(tp->t_tfo_cookie.client, 2479c560df6fSPatrick Kelsey tfo_optval.psk, 2480c560df6fSPatrick Kelsey TCP_FASTOPEN_PSK_LEN); 2481c560df6fSPatrick Kelsey tp->t_tfo_client_cookie_len = 2482c560df6fSPatrick Kelsey TCP_FASTOPEN_PSK_LEN; 2483c560df6fSPatrick Kelsey } 2484c560df6fSPatrick Kelsey } 2485d442a657SMichael Tuexen tp->t_flags |= TF_FASTOPEN; 2486281a0fd4SPatrick Kelsey } else 2487281a0fd4SPatrick Kelsey tp->t_flags &= ~TF_FASTOPEN; 2488281a0fd4SPatrick Kelsey goto unlock_and_done; 2489c560df6fSPatrick Kelsey } 2490281a0fd4SPatrick Kelsey 2491e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX 24922529f56eSJonathan T. Looney case TCP_LOG: 24932529f56eSJonathan T. Looney INP_WUNLOCK(inp); 24942529f56eSJonathan T. Looney error = sooptcopyin(sopt, &optval, sizeof optval, 24952529f56eSJonathan T. Looney sizeof optval); 24962529f56eSJonathan T. Looney if (error) 24972529f56eSJonathan T. Looney return (error); 24982529f56eSJonathan T. Looney 24992529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 25002529f56eSJonathan T. Looney error = tcp_log_state_change(tp, optval); 25012529f56eSJonathan T. Looney goto unlock_and_done; 25022529f56eSJonathan T. Looney 25032529f56eSJonathan T. Looney case TCP_LOGBUF: 25042529f56eSJonathan T. Looney INP_WUNLOCK(inp); 25052529f56eSJonathan T. Looney error = EINVAL; 25062529f56eSJonathan T. Looney break; 25072529f56eSJonathan T. Looney 25082529f56eSJonathan T. Looney case TCP_LOGID: 25092529f56eSJonathan T. Looney INP_WUNLOCK(inp); 25102529f56eSJonathan T. Looney error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0); 25112529f56eSJonathan T. Looney if (error) 25122529f56eSJonathan T. Looney break; 25132529f56eSJonathan T. Looney buf[sopt->sopt_valsize] = '\0'; 25142529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 25152529f56eSJonathan T. Looney error = tcp_log_set_id(tp, buf); 25162529f56eSJonathan T. Looney /* tcp_log_set_id() unlocks the INP. */ 25172529f56eSJonathan T. Looney break; 25182529f56eSJonathan T. Looney 25192529f56eSJonathan T. Looney case TCP_LOGDUMP: 25202529f56eSJonathan T. Looney case TCP_LOGDUMPID: 25212529f56eSJonathan T. Looney INP_WUNLOCK(inp); 25222529f56eSJonathan T. Looney error = 25232529f56eSJonathan T. Looney sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0); 25242529f56eSJonathan T. Looney if (error) 25252529f56eSJonathan T. Looney break; 25262529f56eSJonathan T. Looney buf[sopt->sopt_valsize] = '\0'; 25272529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 25282529f56eSJonathan T. Looney if (sopt->sopt_name == TCP_LOGDUMP) { 25292529f56eSJonathan T. Looney error = tcp_log_dump_tp_logbuf(tp, buf, 25302529f56eSJonathan T. Looney M_WAITOK, true); 25312529f56eSJonathan T. Looney INP_WUNLOCK(inp); 25322529f56eSJonathan T. Looney } else { 25332529f56eSJonathan T. Looney tcp_log_dump_tp_bucket_logbufs(tp, buf); 25342529f56eSJonathan T. Looney /* 25352529f56eSJonathan T. Looney * tcp_log_dump_tp_bucket_logbufs() drops the 25362529f56eSJonathan T. Looney * INP lock. 25372529f56eSJonathan T. Looney */ 25382529f56eSJonathan T. Looney } 25392529f56eSJonathan T. Looney break; 2540e24e5683SJonathan T. Looney #endif 25412529f56eSJonathan T. Looney 2542df8bae1dSRodney W. Grimes default: 25438501a69cSRobert Watson INP_WUNLOCK(inp); 2544df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 2545df8bae1dSRodney W. Grimes break; 2546df8bae1dSRodney W. Grimes } 2547df8bae1dSRodney W. Grimes break; 2548df8bae1dSRodney W. Grimes 2549cfe8b629SGarrett Wollman case SOPT_GET: 25501e8f5ffaSRobert Watson tp = intotcpcb(inp); 2551cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2552fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 255388f6b043SBruce M Simpson case TCP_MD5SIG: 2554fcf59617SAndrey V. Elsukov if (!TCPMD5_ENABLED()) { 25558501a69cSRobert Watson INP_WUNLOCK(inp); 2556fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 2557fcf59617SAndrey V. Elsukov } 2558fcf59617SAndrey V. Elsukov error = TCPMD5_PCBCTL(inp, sopt); 25591cfd4b53SBruce M Simpson break; 2560265ed012SBruce M Simpson #endif 25611e8f5ffaSRobert Watson 2562df8bae1dSRodney W. Grimes case TCP_NODELAY: 2563cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NODELAY; 25648501a69cSRobert Watson INP_WUNLOCK(inp); 2565b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2566df8bae1dSRodney W. Grimes break; 2567df8bae1dSRodney W. Grimes case TCP_MAXSEG: 2568cfe8b629SGarrett Wollman optval = tp->t_maxseg; 25698501a69cSRobert Watson INP_WUNLOCK(inp); 2570b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2571df8bae1dSRodney W. Grimes break; 25729e644c23SMichael Tuexen case TCP_REMOTE_UDP_ENCAPS_PORT: 25739e644c23SMichael Tuexen optval = ntohs(tp->t_port); 25749e644c23SMichael Tuexen INP_WUNLOCK(inp); 25759e644c23SMichael Tuexen error = sooptcopyout(sopt, &optval, sizeof optval); 25769e644c23SMichael Tuexen break; 2577a0292f23SGarrett Wollman case TCP_NOOPT: 2578cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOOPT; 25798501a69cSRobert Watson INP_WUNLOCK(inp); 2580b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2581a0292f23SGarrett Wollman break; 2582a0292f23SGarrett Wollman case TCP_NOPUSH: 2583cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOPUSH; 25848501a69cSRobert Watson INP_WUNLOCK(inp); 2585b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2586b8af5dfaSRobert Watson break; 2587b8af5dfaSRobert Watson case TCP_INFO: 2588b8af5dfaSRobert Watson tcp_fill_info(tp, &ti); 25898501a69cSRobert Watson INP_WUNLOCK(inp); 2590b8af5dfaSRobert Watson error = sooptcopyout(sopt, &ti, sizeof ti); 2591a0292f23SGarrett Wollman break; 2592adc56f5aSEdward Tomasz Napierala case TCP_STATS: 2593adc56f5aSEdward Tomasz Napierala { 2594adc56f5aSEdward Tomasz Napierala #ifdef STATS 2595adc56f5aSEdward Tomasz Napierala int nheld; 2596adc56f5aSEdward Tomasz Napierala TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0; 2597adc56f5aSEdward Tomasz Napierala 2598adc56f5aSEdward Tomasz Napierala error = 0; 2599adc56f5aSEdward Tomasz Napierala socklen_t outsbsz = sopt->sopt_valsize; 2600adc56f5aSEdward Tomasz Napierala if (tp->t_stats == NULL) 2601adc56f5aSEdward Tomasz Napierala error = ENOENT; 2602adc56f5aSEdward Tomasz Napierala else if (outsbsz >= tp->t_stats->cursz) 2603adc56f5aSEdward Tomasz Napierala outsbsz = tp->t_stats->cursz; 2604adc56f5aSEdward Tomasz Napierala else if (outsbsz >= sizeof(struct statsblob)) 2605adc56f5aSEdward Tomasz Napierala outsbsz = sizeof(struct statsblob); 2606adc56f5aSEdward Tomasz Napierala else 2607adc56f5aSEdward Tomasz Napierala error = EINVAL; 2608adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2609adc56f5aSEdward Tomasz Napierala if (error) 2610adc56f5aSEdward Tomasz Napierala break; 2611adc56f5aSEdward Tomasz Napierala 2612adc56f5aSEdward Tomasz Napierala sbp = sopt->sopt_val; 2613adc56f5aSEdward Tomasz Napierala nheld = atop(round_page(((vm_offset_t)sbp) + 2614adc56f5aSEdward Tomasz Napierala (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp)); 2615adc56f5aSEdward Tomasz Napierala vm_page_t ma[nheld]; 2616adc56f5aSEdward Tomasz Napierala if (vm_fault_quick_hold_pages( 2617adc56f5aSEdward Tomasz Napierala &curproc->p_vmspace->vm_map, (vm_offset_t)sbp, 2618adc56f5aSEdward Tomasz Napierala outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma, 2619adc56f5aSEdward Tomasz Napierala nheld) < 0) { 2620adc56f5aSEdward Tomasz Napierala error = EFAULT; 2621adc56f5aSEdward Tomasz Napierala break; 2622adc56f5aSEdward Tomasz Napierala } 2623adc56f5aSEdward Tomasz Napierala 2624adc56f5aSEdward Tomasz Napierala if ((error = copyin_nofault(&(sbp->flags), &sbflags, 2625adc56f5aSEdward Tomasz Napierala SIZEOF_MEMBER(struct statsblob, flags)))) 2626adc56f5aSEdward Tomasz Napierala goto unhold; 2627adc56f5aSEdward Tomasz Napierala 2628adc56f5aSEdward Tomasz Napierala INP_WLOCK_RECHECK(inp); 2629adc56f5aSEdward Tomasz Napierala error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats, 2630adc56f5aSEdward Tomasz Napierala sbflags | SB_CLONE_USRDSTNOFAULT); 2631adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2632adc56f5aSEdward Tomasz Napierala sopt->sopt_valsize = outsbsz; 2633adc56f5aSEdward Tomasz Napierala unhold: 2634adc56f5aSEdward Tomasz Napierala vm_page_unhold_pages(ma, nheld); 2635adc56f5aSEdward Tomasz Napierala #else 2636adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2637adc56f5aSEdward Tomasz Napierala error = EOPNOTSUPP; 2638adc56f5aSEdward Tomasz Napierala #endif /* !STATS */ 2639adc56f5aSEdward Tomasz Napierala break; 2640adc56f5aSEdward Tomasz Napierala } 2641dbc42409SLawrence Stewart case TCP_CONGESTION: 2642af6fef3aSGleb Smirnoff len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); 2643dbc42409SLawrence Stewart INP_WUNLOCK(inp); 2644af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, buf, len + 1); 2645dbc42409SLawrence Stewart break; 26462f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 26472f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 26482f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 26492f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 26502f3eb7f4SGleb Smirnoff switch (sopt->sopt_name) { 26512f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 26525a17b6adSMichael Tuexen ui = TP_KEEPIDLE(tp) / hz; 26532f3eb7f4SGleb Smirnoff break; 26542f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 26555a17b6adSMichael Tuexen ui = TP_KEEPINTVL(tp) / hz; 26562f3eb7f4SGleb Smirnoff break; 26572f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 26585a17b6adSMichael Tuexen ui = TP_KEEPINIT(tp) / hz; 26592f3eb7f4SGleb Smirnoff break; 26602f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 26615a17b6adSMichael Tuexen ui = TP_KEEPCNT(tp); 26622f3eb7f4SGleb Smirnoff break; 26632f3eb7f4SGleb Smirnoff } 26642f3eb7f4SGleb Smirnoff INP_WUNLOCK(inp); 26652f3eb7f4SGleb Smirnoff error = sooptcopyout(sopt, &ui, sizeof(ui)); 26662f3eb7f4SGleb Smirnoff break; 266786a996e6SHiren Panchasara #ifdef TCPPCAP 266886a996e6SHiren Panchasara case TCP_PCAP_OUT: 266986a996e6SHiren Panchasara case TCP_PCAP_IN: 267086a996e6SHiren Panchasara optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ? 267186a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts)); 267286a996e6SHiren Panchasara INP_WUNLOCK(inp); 267386a996e6SHiren Panchasara error = sooptcopyout(sopt, &optval, sizeof optval); 267486a996e6SHiren Panchasara break; 267586a996e6SHiren Panchasara #endif 2676281a0fd4SPatrick Kelsey case TCP_FASTOPEN: 2677281a0fd4SPatrick Kelsey optval = tp->t_flags & TF_FASTOPEN; 2678281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 2679281a0fd4SPatrick Kelsey error = sooptcopyout(sopt, &optval, sizeof optval); 2680281a0fd4SPatrick Kelsey break; 2681e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX 26822529f56eSJonathan T. Looney case TCP_LOG: 26832529f56eSJonathan T. Looney optval = tp->t_logstate; 26842529f56eSJonathan T. Looney INP_WUNLOCK(inp); 26852529f56eSJonathan T. Looney error = sooptcopyout(sopt, &optval, sizeof(optval)); 26862529f56eSJonathan T. Looney break; 26872529f56eSJonathan T. Looney case TCP_LOGBUF: 26882529f56eSJonathan T. Looney /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */ 26892529f56eSJonathan T. Looney error = tcp_log_getlogbuf(sopt, tp); 26902529f56eSJonathan T. Looney break; 26912529f56eSJonathan T. Looney case TCP_LOGID: 26922529f56eSJonathan T. Looney len = tcp_log_get_id(tp, buf); 26932529f56eSJonathan T. Looney INP_WUNLOCK(inp); 26942529f56eSJonathan T. Looney error = sooptcopyout(sopt, buf, len + 1); 26952529f56eSJonathan T. Looney break; 26962529f56eSJonathan T. Looney case TCP_LOGDUMP: 26972529f56eSJonathan T. Looney case TCP_LOGDUMPID: 26982529f56eSJonathan T. Looney INP_WUNLOCK(inp); 26992529f56eSJonathan T. Looney error = EINVAL; 27002529f56eSJonathan T. Looney break; 2701e24e5683SJonathan T. Looney #endif 2702b2e60773SJohn Baldwin #ifdef KERN_TLS 2703b2e60773SJohn Baldwin case TCP_TXTLS_MODE: 2704bf256782SMark Johnston error = ktls_get_tx_mode(so, &optval); 2705b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2706bf256782SMark Johnston if (error == 0) 2707bf256782SMark Johnston error = sooptcopyout(sopt, &optval, 2708bf256782SMark Johnston sizeof(optval)); 2709b2e60773SJohn Baldwin break; 2710f1f93475SJohn Baldwin case TCP_RXTLS_MODE: 2711bf256782SMark Johnston error = ktls_get_rx_mode(so, &optval); 2712f1f93475SJohn Baldwin INP_WUNLOCK(inp); 2713bf256782SMark Johnston if (error == 0) 2714bf256782SMark Johnston error = sooptcopyout(sopt, &optval, 2715bf256782SMark Johnston sizeof(optval)); 2716f1f93475SJohn Baldwin break; 2717b2e60773SJohn Baldwin #endif 27180471a8c7SRichard Scheffenegger case TCP_LRD: 27190471a8c7SRichard Scheffenegger optval = tp->t_flags & TF_LRD; 27200471a8c7SRichard Scheffenegger INP_WUNLOCK(inp); 27210471a8c7SRichard Scheffenegger error = sooptcopyout(sopt, &optval, sizeof optval); 27220471a8c7SRichard Scheffenegger break; 2723df8bae1dSRodney W. Grimes default: 27248501a69cSRobert Watson INP_WUNLOCK(inp); 2725df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 2726df8bae1dSRodney W. Grimes break; 2727df8bae1dSRodney W. Grimes } 2728df8bae1dSRodney W. Grimes break; 2729df8bae1dSRodney W. Grimes } 2730df8bae1dSRodney W. Grimes return (error); 2731df8bae1dSRodney W. Grimes } 27328501a69cSRobert Watson #undef INP_WLOCK_RECHECK 2733bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP 2734df8bae1dSRodney W. Grimes 273526e30fbbSDavid Greenman /* 2736df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 2737df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 2738df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 2739df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 2740df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 2741df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 2742df8bae1dSRodney W. Grimes */ 2743623dce13SRobert Watson static void 2744ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp) 2745df8bae1dSRodney W. Grimes { 2746e6e0b5ffSRobert Watson struct inpcb *inp = tp->t_inpcb; 2747e6e0b5ffSRobert Watson struct socket *so = inp->inp_socket; 2748e6e0b5ffSRobert Watson 274997a95ee1SGleb Smirnoff NET_EPOCH_ASSERT(); 27508501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2751df8bae1dSRodney W. Grimes 2752623dce13SRobert Watson /* 2753623dce13SRobert Watson * Neither tcp_close() nor tcp_drop() should return NULL, as the 2754623dce13SRobert Watson * socket is still open. 2755623dce13SRobert Watson */ 27568db239dcSMichael Tuexen if (tp->t_state < TCPS_ESTABLISHED && 27578db239dcSMichael Tuexen !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) { 2758df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2759623dce13SRobert Watson KASSERT(tp != NULL, 2760623dce13SRobert Watson ("tcp_disconnect: tcp_close() returned NULL")); 2761623dce13SRobert Watson } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 2762243917feSSeigo Tanimura tp = tcp_drop(tp, 0); 2763623dce13SRobert Watson KASSERT(tp != NULL, 2764623dce13SRobert Watson ("tcp_disconnect: tcp_drop() returned NULL")); 2765623dce13SRobert Watson } else { 2766df8bae1dSRodney W. Grimes soisdisconnecting(so); 2767df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 2768623dce13SRobert Watson tcp_usrclosed(tp); 2769ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 2770*40fa3e40SGleb Smirnoff tcp_output(tp); 2771df8bae1dSRodney W. Grimes } 2772df8bae1dSRodney W. Grimes } 2773df8bae1dSRodney W. Grimes 2774df8bae1dSRodney W. Grimes /* 2775df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 2776df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 2777df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 2778df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 2779df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 2780df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 2781df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 2782df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 2783df8bae1dSRodney W. Grimes */ 2784623dce13SRobert Watson static void 2785ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp) 2786df8bae1dSRodney W. Grimes { 2787df8bae1dSRodney W. Grimes 278897a95ee1SGleb Smirnoff NET_EPOCH_ASSERT(); 27898501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 2790e6e0b5ffSRobert Watson 2791df8bae1dSRodney W. Grimes switch (tp->t_state) { 2792df8bae1dSRodney W. Grimes case TCPS_LISTEN: 279309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 279409fe6320SNavdeep Parhar tcp_offload_listen_stop(tp); 279509fe6320SNavdeep Parhar #endif 2796550e9d42SHiren Panchasara tcp_state_change(tp, TCPS_CLOSED); 2797bc65987aSKip Macy /* FALLTHROUGH */ 2798bc65987aSKip Macy case TCPS_CLOSED: 2799df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2800623dce13SRobert Watson /* 2801623dce13SRobert Watson * tcp_close() should never return NULL here as the socket is 2802623dce13SRobert Watson * still open. 2803623dce13SRobert Watson */ 2804623dce13SRobert Watson KASSERT(tp != NULL, 2805623dce13SRobert Watson ("tcp_usrclosed: tcp_close() returned NULL")); 2806df8bae1dSRodney W. Grimes break; 2807df8bae1dSRodney W. Grimes 2808a0292f23SGarrett Wollman case TCPS_SYN_SENT: 2809df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2810a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 2811a0292f23SGarrett Wollman break; 2812a0292f23SGarrett Wollman 2813df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 281457f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2815df8bae1dSRodney W. Grimes break; 2816df8bae1dSRodney W. Grimes 2817df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 281857f60867SMark Johnston tcp_state_change(tp, TCPS_LAST_ACK); 2819df8bae1dSRodney W. Grimes break; 2820df8bae1dSRodney W. Grimes } 2821abc7d910SRobert Watson if (tp->t_state >= TCPS_FIN_WAIT_2) { 2822df8bae1dSRodney W. Grimes soisdisconnected(tp->t_inpcb->inp_socket); 2823abc7d910SRobert Watson /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 28247c72af87SMohan Srinivasan if (tp->t_state == TCPS_FIN_WAIT_2) { 28257c72af87SMohan Srinivasan int timeout; 28267c72af87SMohan Srinivasan 28277c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 28289077f387SGleb Smirnoff tcp_finwait2_timeout : TP_MAXIDLE(tp); 2829b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 2830b6239c4aSAndras Olah } 2831df8bae1dSRodney W. Grimes } 28327c72af87SMohan Srinivasan } 2833497057eeSRobert Watson 2834497057eeSRobert Watson #ifdef DDB 2835497057eeSRobert Watson static void 2836497057eeSRobert Watson db_print_indent(int indent) 2837497057eeSRobert Watson { 2838497057eeSRobert Watson int i; 2839497057eeSRobert Watson 2840497057eeSRobert Watson for (i = 0; i < indent; i++) 2841497057eeSRobert Watson db_printf(" "); 2842497057eeSRobert Watson } 2843497057eeSRobert Watson 2844497057eeSRobert Watson static void 2845497057eeSRobert Watson db_print_tstate(int t_state) 2846497057eeSRobert Watson { 2847497057eeSRobert Watson 2848497057eeSRobert Watson switch (t_state) { 2849497057eeSRobert Watson case TCPS_CLOSED: 2850497057eeSRobert Watson db_printf("TCPS_CLOSED"); 2851497057eeSRobert Watson return; 2852497057eeSRobert Watson 2853497057eeSRobert Watson case TCPS_LISTEN: 2854497057eeSRobert Watson db_printf("TCPS_LISTEN"); 2855497057eeSRobert Watson return; 2856497057eeSRobert Watson 2857497057eeSRobert Watson case TCPS_SYN_SENT: 2858497057eeSRobert Watson db_printf("TCPS_SYN_SENT"); 2859497057eeSRobert Watson return; 2860497057eeSRobert Watson 2861497057eeSRobert Watson case TCPS_SYN_RECEIVED: 2862497057eeSRobert Watson db_printf("TCPS_SYN_RECEIVED"); 2863497057eeSRobert Watson return; 2864497057eeSRobert Watson 2865497057eeSRobert Watson case TCPS_ESTABLISHED: 2866497057eeSRobert Watson db_printf("TCPS_ESTABLISHED"); 2867497057eeSRobert Watson return; 2868497057eeSRobert Watson 2869497057eeSRobert Watson case TCPS_CLOSE_WAIT: 2870497057eeSRobert Watson db_printf("TCPS_CLOSE_WAIT"); 2871497057eeSRobert Watson return; 2872497057eeSRobert Watson 2873497057eeSRobert Watson case TCPS_FIN_WAIT_1: 2874497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_1"); 2875497057eeSRobert Watson return; 2876497057eeSRobert Watson 2877497057eeSRobert Watson case TCPS_CLOSING: 2878497057eeSRobert Watson db_printf("TCPS_CLOSING"); 2879497057eeSRobert Watson return; 2880497057eeSRobert Watson 2881497057eeSRobert Watson case TCPS_LAST_ACK: 2882497057eeSRobert Watson db_printf("TCPS_LAST_ACK"); 2883497057eeSRobert Watson return; 2884497057eeSRobert Watson 2885497057eeSRobert Watson case TCPS_FIN_WAIT_2: 2886497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_2"); 2887497057eeSRobert Watson return; 2888497057eeSRobert Watson 2889497057eeSRobert Watson case TCPS_TIME_WAIT: 2890497057eeSRobert Watson db_printf("TCPS_TIME_WAIT"); 2891497057eeSRobert Watson return; 2892497057eeSRobert Watson 2893497057eeSRobert Watson default: 2894497057eeSRobert Watson db_printf("unknown"); 2895497057eeSRobert Watson return; 2896497057eeSRobert Watson } 2897497057eeSRobert Watson } 2898497057eeSRobert Watson 2899497057eeSRobert Watson static void 2900497057eeSRobert Watson db_print_tflags(u_int t_flags) 2901497057eeSRobert Watson { 2902497057eeSRobert Watson int comma; 2903497057eeSRobert Watson 2904497057eeSRobert Watson comma = 0; 2905497057eeSRobert Watson if (t_flags & TF_ACKNOW) { 2906497057eeSRobert Watson db_printf("%sTF_ACKNOW", comma ? ", " : ""); 2907497057eeSRobert Watson comma = 1; 2908497057eeSRobert Watson } 2909497057eeSRobert Watson if (t_flags & TF_DELACK) { 2910497057eeSRobert Watson db_printf("%sTF_DELACK", comma ? ", " : ""); 2911497057eeSRobert Watson comma = 1; 2912497057eeSRobert Watson } 2913497057eeSRobert Watson if (t_flags & TF_NODELAY) { 2914497057eeSRobert Watson db_printf("%sTF_NODELAY", comma ? ", " : ""); 2915497057eeSRobert Watson comma = 1; 2916497057eeSRobert Watson } 2917497057eeSRobert Watson if (t_flags & TF_NOOPT) { 2918497057eeSRobert Watson db_printf("%sTF_NOOPT", comma ? ", " : ""); 2919497057eeSRobert Watson comma = 1; 2920497057eeSRobert Watson } 2921497057eeSRobert Watson if (t_flags & TF_SENTFIN) { 2922497057eeSRobert Watson db_printf("%sTF_SENTFIN", comma ? ", " : ""); 2923497057eeSRobert Watson comma = 1; 2924497057eeSRobert Watson } 2925497057eeSRobert Watson if (t_flags & TF_REQ_SCALE) { 2926497057eeSRobert Watson db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 2927497057eeSRobert Watson comma = 1; 2928497057eeSRobert Watson } 2929497057eeSRobert Watson if (t_flags & TF_RCVD_SCALE) { 2930497057eeSRobert Watson db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 2931497057eeSRobert Watson comma = 1; 2932497057eeSRobert Watson } 2933497057eeSRobert Watson if (t_flags & TF_REQ_TSTMP) { 2934497057eeSRobert Watson db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 2935497057eeSRobert Watson comma = 1; 2936497057eeSRobert Watson } 2937497057eeSRobert Watson if (t_flags & TF_RCVD_TSTMP) { 2938497057eeSRobert Watson db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 2939497057eeSRobert Watson comma = 1; 2940497057eeSRobert Watson } 2941497057eeSRobert Watson if (t_flags & TF_SACK_PERMIT) { 2942497057eeSRobert Watson db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 2943497057eeSRobert Watson comma = 1; 2944497057eeSRobert Watson } 2945497057eeSRobert Watson if (t_flags & TF_NEEDSYN) { 2946497057eeSRobert Watson db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 2947497057eeSRobert Watson comma = 1; 2948497057eeSRobert Watson } 2949497057eeSRobert Watson if (t_flags & TF_NEEDFIN) { 2950497057eeSRobert Watson db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 2951497057eeSRobert Watson comma = 1; 2952497057eeSRobert Watson } 2953497057eeSRobert Watson if (t_flags & TF_NOPUSH) { 2954497057eeSRobert Watson db_printf("%sTF_NOPUSH", comma ? ", " : ""); 2955497057eeSRobert Watson comma = 1; 2956497057eeSRobert Watson } 2957497057eeSRobert Watson if (t_flags & TF_MORETOCOME) { 2958497057eeSRobert Watson db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 2959497057eeSRobert Watson comma = 1; 2960497057eeSRobert Watson } 2961497057eeSRobert Watson if (t_flags & TF_LQ_OVERFLOW) { 2962497057eeSRobert Watson db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : ""); 2963497057eeSRobert Watson comma = 1; 2964497057eeSRobert Watson } 2965497057eeSRobert Watson if (t_flags & TF_LASTIDLE) { 2966497057eeSRobert Watson db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 2967497057eeSRobert Watson comma = 1; 2968497057eeSRobert Watson } 2969497057eeSRobert Watson if (t_flags & TF_RXWIN0SENT) { 2970497057eeSRobert Watson db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 2971497057eeSRobert Watson comma = 1; 2972497057eeSRobert Watson } 2973497057eeSRobert Watson if (t_flags & TF_FASTRECOVERY) { 2974497057eeSRobert Watson db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 2975497057eeSRobert Watson comma = 1; 2976497057eeSRobert Watson } 2977dbc42409SLawrence Stewart if (t_flags & TF_CONGRECOVERY) { 2978dbc42409SLawrence Stewart db_printf("%sTF_CONGRECOVERY", comma ? ", " : ""); 2979dbc42409SLawrence Stewart comma = 1; 2980dbc42409SLawrence Stewart } 2981497057eeSRobert Watson if (t_flags & TF_WASFRECOVERY) { 2982497057eeSRobert Watson db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 2983497057eeSRobert Watson comma = 1; 2984497057eeSRobert Watson } 2985497057eeSRobert Watson if (t_flags & TF_SIGNATURE) { 2986497057eeSRobert Watson db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 2987497057eeSRobert Watson comma = 1; 2988497057eeSRobert Watson } 2989497057eeSRobert Watson if (t_flags & TF_FORCEDATA) { 2990497057eeSRobert Watson db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 2991497057eeSRobert Watson comma = 1; 2992497057eeSRobert Watson } 2993497057eeSRobert Watson if (t_flags & TF_TSO) { 2994497057eeSRobert Watson db_printf("%sTF_TSO", comma ? ", " : ""); 2995497057eeSRobert Watson comma = 1; 2996497057eeSRobert Watson } 2997281a0fd4SPatrick Kelsey if (t_flags & TF_FASTOPEN) { 2998281a0fd4SPatrick Kelsey db_printf("%sTF_FASTOPEN", comma ? ", " : ""); 2999281a0fd4SPatrick Kelsey comma = 1; 3000281a0fd4SPatrick Kelsey } 3001497057eeSRobert Watson } 3002497057eeSRobert Watson 3003497057eeSRobert Watson static void 30043cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2) 30053cf38784SMichael Tuexen { 30063cf38784SMichael Tuexen int comma; 30073cf38784SMichael Tuexen 30083cf38784SMichael Tuexen comma = 0; 30093cf38784SMichael Tuexen if (t_flags2 & TF2_ECN_PERMIT) { 30103cf38784SMichael Tuexen db_printf("%sTF2_ECN_PERMIT", comma ? ", " : ""); 30113cf38784SMichael Tuexen comma = 1; 30123cf38784SMichael Tuexen } 30133cf38784SMichael Tuexen } 30143cf38784SMichael Tuexen 30153cf38784SMichael Tuexen static void 3016497057eeSRobert Watson db_print_toobflags(char t_oobflags) 3017497057eeSRobert Watson { 3018497057eeSRobert Watson int comma; 3019497057eeSRobert Watson 3020497057eeSRobert Watson comma = 0; 3021497057eeSRobert Watson if (t_oobflags & TCPOOB_HAVEDATA) { 3022497057eeSRobert Watson db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 3023497057eeSRobert Watson comma = 1; 3024497057eeSRobert Watson } 3025497057eeSRobert Watson if (t_oobflags & TCPOOB_HADDATA) { 3026497057eeSRobert Watson db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 3027497057eeSRobert Watson comma = 1; 3028497057eeSRobert Watson } 3029497057eeSRobert Watson } 3030497057eeSRobert Watson 3031497057eeSRobert Watson static void 3032497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 3033497057eeSRobert Watson { 3034497057eeSRobert Watson 3035497057eeSRobert Watson db_print_indent(indent); 3036497057eeSRobert Watson db_printf("%s at %p\n", name, tp); 3037497057eeSRobert Watson 3038497057eeSRobert Watson indent += 2; 3039497057eeSRobert Watson 3040497057eeSRobert Watson db_print_indent(indent); 3041497057eeSRobert Watson db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 3042c28440dbSRandall Stewart TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 3043497057eeSRobert Watson 3044497057eeSRobert Watson db_print_indent(indent); 304585d94372SRobert Watson db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n", 3046e2f2059fSMike Silbersack &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep); 3047497057eeSRobert Watson 3048497057eeSRobert Watson db_print_indent(indent); 3049e2f2059fSMike Silbersack db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl, 3050e2f2059fSMike Silbersack &tp->t_timers->tt_delack, tp->t_inpcb); 3051497057eeSRobert Watson 3052497057eeSRobert Watson db_print_indent(indent); 3053497057eeSRobert Watson db_printf("t_state: %d (", tp->t_state); 3054497057eeSRobert Watson db_print_tstate(tp->t_state); 3055497057eeSRobert Watson db_printf(")\n"); 3056497057eeSRobert Watson 3057497057eeSRobert Watson db_print_indent(indent); 3058497057eeSRobert Watson db_printf("t_flags: 0x%x (", tp->t_flags); 3059497057eeSRobert Watson db_print_tflags(tp->t_flags); 3060497057eeSRobert Watson db_printf(")\n"); 3061497057eeSRobert Watson 3062497057eeSRobert Watson db_print_indent(indent); 30633cf38784SMichael Tuexen db_printf("t_flags2: 0x%x (", tp->t_flags2); 30643cf38784SMichael Tuexen db_print_tflags2(tp->t_flags2); 30653cf38784SMichael Tuexen db_printf(")\n"); 30663cf38784SMichael Tuexen 30673cf38784SMichael Tuexen db_print_indent(indent); 3068497057eeSRobert Watson db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n", 3069497057eeSRobert Watson tp->snd_una, tp->snd_max, tp->snd_nxt); 3070497057eeSRobert Watson 3071497057eeSRobert Watson db_print_indent(indent); 3072497057eeSRobert Watson db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 3073497057eeSRobert Watson tp->snd_up, tp->snd_wl1, tp->snd_wl2); 3074497057eeSRobert Watson 3075497057eeSRobert Watson db_print_indent(indent); 3076497057eeSRobert Watson db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 3077497057eeSRobert Watson tp->iss, tp->irs, tp->rcv_nxt); 3078497057eeSRobert Watson 3079497057eeSRobert Watson db_print_indent(indent); 30803ac12506SJonathan T. Looney db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n", 3081497057eeSRobert Watson tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 3082497057eeSRobert Watson 3083497057eeSRobert Watson db_print_indent(indent); 30843ac12506SJonathan T. Looney db_printf("snd_wnd: %u snd_cwnd: %u\n", 30851c18314dSAndre Oppermann tp->snd_wnd, tp->snd_cwnd); 3086497057eeSRobert Watson 3087497057eeSRobert Watson db_print_indent(indent); 30883ac12506SJonathan T. Looney db_printf("snd_ssthresh: %u snd_recover: " 30891c18314dSAndre Oppermann "0x%08x\n", tp->snd_ssthresh, tp->snd_recover); 3090497057eeSRobert Watson 3091497057eeSRobert Watson db_print_indent(indent); 30920c39d38dSGleb Smirnoff db_printf("t_rcvtime: %u t_startime: %u\n", 30930c39d38dSGleb Smirnoff tp->t_rcvtime, tp->t_starttime); 3094497057eeSRobert Watson 3095497057eeSRobert Watson db_print_indent(indent); 30961c18314dSAndre Oppermann db_printf("t_rttime: %u t_rtsq: 0x%08x\n", 30971c18314dSAndre Oppermann tp->t_rtttime, tp->t_rtseq); 3098497057eeSRobert Watson 3099497057eeSRobert Watson db_print_indent(indent); 31001c18314dSAndre Oppermann db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n", 31011c18314dSAndre Oppermann tp->t_rxtcur, tp->t_maxseg, tp->t_srtt); 3102497057eeSRobert Watson 3103497057eeSRobert Watson db_print_indent(indent); 3104497057eeSRobert Watson db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u " 3105497057eeSRobert Watson "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin, 3106497057eeSRobert Watson tp->t_rttbest); 3107497057eeSRobert Watson 3108497057eeSRobert Watson db_print_indent(indent); 31093ac12506SJonathan T. Looney db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n", 3110497057eeSRobert Watson tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 3111497057eeSRobert Watson 3112497057eeSRobert Watson db_print_indent(indent); 3113497057eeSRobert Watson db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 3114497057eeSRobert Watson db_print_toobflags(tp->t_oobflags); 3115497057eeSRobert Watson db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 3116497057eeSRobert Watson 3117497057eeSRobert Watson db_print_indent(indent); 3118497057eeSRobert Watson db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 3119497057eeSRobert Watson tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 3120497057eeSRobert Watson 3121497057eeSRobert Watson db_print_indent(indent); 31229f78a87aSJohn Baldwin db_printf("ts_recent: %u ts_recent_age: %u\n", 31231a553740SAndre Oppermann tp->ts_recent, tp->ts_recent_age); 3124497057eeSRobert Watson 3125497057eeSRobert Watson db_print_indent(indent); 3126497057eeSRobert Watson db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 31273ac12506SJonathan T. Looney "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 3128497057eeSRobert Watson 3129497057eeSRobert Watson db_print_indent(indent); 31303ac12506SJonathan T. Looney db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x " 31319f78a87aSJohn Baldwin "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 3132497057eeSRobert Watson tp->snd_recover_prev, tp->t_badrxtwin); 3133497057eeSRobert Watson 3134497057eeSRobert Watson db_print_indent(indent); 31353529149eSAndre Oppermann db_printf("snd_numholes: %d snd_holes first: %p\n", 31363529149eSAndre Oppermann tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 3137497057eeSRobert Watson 3138497057eeSRobert Watson db_print_indent(indent); 3139a3574665SMichael Tuexen db_printf("snd_fack: 0x%08x rcv_numsacks: %d\n", 3140a3574665SMichael Tuexen tp->snd_fack, tp->rcv_numsacks); 3141497057eeSRobert Watson 3142497057eeSRobert Watson /* Skip sackblks, sackhint. */ 3143497057eeSRobert Watson 3144497057eeSRobert Watson db_print_indent(indent); 3145497057eeSRobert Watson db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 3146497057eeSRobert Watson tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 3147497057eeSRobert Watson } 3148497057eeSRobert Watson 3149497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 3150497057eeSRobert Watson { 3151497057eeSRobert Watson struct tcpcb *tp; 3152497057eeSRobert Watson 3153497057eeSRobert Watson if (!have_addr) { 3154497057eeSRobert Watson db_printf("usage: show tcpcb <addr>\n"); 3155497057eeSRobert Watson return; 3156497057eeSRobert Watson } 3157497057eeSRobert Watson tp = (struct tcpcb *)addr; 3158497057eeSRobert Watson 3159497057eeSRobert Watson db_print_tcpcb(tp, "tcpcb", 0); 3160497057eeSRobert Watson } 3161497057eeSRobert Watson #endif 3162