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> 69f5cf1e5fSJulien Charbon #include <sys/syslog.h> 70adc56f5aSEdward Tomasz Napierala #include <sys/stats.h> 71df8bae1dSRodney W. Grimes 72497057eeSRobert Watson #ifdef DDB 73497057eeSRobert Watson #include <ddb/ddb.h> 74497057eeSRobert Watson #endif 75497057eeSRobert Watson 76df8bae1dSRodney W. Grimes #include <net/if.h> 7776039bc8SGleb Smirnoff #include <net/if_var.h> 78df8bae1dSRodney W. Grimes #include <net/route.h> 79530c0060SRobert Watson #include <net/vnet.h> 80df8bae1dSRodney W. Grimes 81df8bae1dSRodney W. Grimes #include <netinet/in.h> 825d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h> 83df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 84b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h> 85b5e8ce9fSBruce Evans #include <netinet/in_var.h> 86df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 87fb59c426SYoshinobu Inoue #ifdef INET6 88b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h> 89b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h> 90fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 91a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h> 92fb59c426SYoshinobu Inoue #endif 932de3e790SGleb Smirnoff #include <netinet/tcp.h> 94df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 95df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 96df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 97df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 982529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h> 99df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 1004644fda3SGleb Smirnoff #include <netinet/cc/cc.h> 101c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h> 102fd389e7cSRandall Stewart #include <netinet/tcp_hpts.h> 10386a996e6SHiren Panchasara #ifdef TCPPCAP 10486a996e6SHiren Panchasara #include <netinet/tcp_pcap.h> 10586a996e6SHiren Panchasara #endif 106610ee2f9SDavid Greenman #ifdef TCPDEBUG 107df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 108610ee2f9SDavid Greenman #endif 10909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 110bc65987aSKip Macy #include <netinet/tcp_offload.h> 11109fe6320SNavdeep Parhar #endif 112fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 113df8bae1dSRodney W. Grimes 114adc56f5aSEdward Tomasz Napierala #include <vm/vm.h> 115adc56f5aSEdward Tomasz Napierala #include <vm/vm_param.h> 116adc56f5aSEdward Tomasz Napierala #include <vm/pmap.h> 117adc56f5aSEdward Tomasz Napierala #include <vm/vm_extern.h> 118adc56f5aSEdward Tomasz Napierala #include <vm/vm_map.h> 119adc56f5aSEdward Tomasz Napierala #include <vm/vm_page.h> 120adc56f5aSEdward Tomasz Napierala 121df8bae1dSRodney W. Grimes /* 122df8bae1dSRodney W. Grimes * TCP protocol interface to socket abstraction. 123df8bae1dSRodney W. Grimes */ 124b287c6c7SBjoern A. Zeeb #ifdef INET 1254d77a549SAlfred Perlstein static int tcp_connect(struct tcpcb *, struct sockaddr *, 1264d77a549SAlfred Perlstein struct thread *td); 127b287c6c7SBjoern A. Zeeb #endif /* INET */ 128fb59c426SYoshinobu Inoue #ifdef INET6 1294d77a549SAlfred Perlstein static int tcp6_connect(struct tcpcb *, struct sockaddr *, 1304d77a549SAlfred Perlstein struct thread *td); 131fb59c426SYoshinobu Inoue #endif /* INET6 */ 132623dce13SRobert Watson static void tcp_disconnect(struct tcpcb *); 133623dce13SRobert Watson static void tcp_usrclosed(struct tcpcb *); 134b8af5dfaSRobert Watson static void tcp_fill_info(struct tcpcb *, struct tcp_info *); 1352c37256eSGarrett Wollman 136d3b6c96bSRandall Stewart static int tcp_pru_options_support(struct tcpcb *tp, int flags); 137d3b6c96bSRandall Stewart 1382c37256eSGarrett Wollman #ifdef TCPDEBUG 1391db24ffbSJonathan Lemon #define TCPDEBUG0 int ostate = 0 1402c37256eSGarrett Wollman #define TCPDEBUG1() ostate = tp ? tp->t_state : 0 1414cc20ab1SSeigo Tanimura #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 1424cc20ab1SSeigo Tanimura tcp_trace(TA_USER, ostate, tp, 0, 0, req) 1432c37256eSGarrett Wollman #else 1442c37256eSGarrett Wollman #define TCPDEBUG0 1452c37256eSGarrett Wollman #define TCPDEBUG1() 1462c37256eSGarrett Wollman #define TCPDEBUG2(req) 1472c37256eSGarrett Wollman #endif 1482c37256eSGarrett Wollman 1492c37256eSGarrett Wollman /* 15025102351SMike Karels * tcp_require_unique port requires a globally-unique source port for each 15125102351SMike Karels * outgoing connection. The default is to require the 4-tuple to be unique. 15225102351SMike Karels */ 15325102351SMike Karels VNET_DEFINE(int, tcp_require_unique_port) = 0; 15425102351SMike Karels SYSCTL_INT(_net_inet_tcp, OID_AUTO, require_unique_port, 15525102351SMike Karels CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_require_unique_port), 0, 15625102351SMike Karels "Require globally-unique ephemeral port for outgoing connections"); 15725102351SMike Karels #define V_tcp_require_unique_port VNET(tcp_require_unique_port) 15825102351SMike Karels 15925102351SMike Karels /* 1602c37256eSGarrett Wollman * TCP attaches to socket via pru_attach(), reserving space, 1612c37256eSGarrett Wollman * and an internet control block. 1622c37256eSGarrett Wollman */ 1632c37256eSGarrett Wollman static int 164b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td) 1652c37256eSGarrett Wollman { 166f76fcf6dSJeffrey Hsu struct inpcb *inp; 167623dce13SRobert Watson struct tcpcb *tp = NULL; 168623dce13SRobert Watson int error; 1692c37256eSGarrett Wollman TCPDEBUG0; 1702c37256eSGarrett Wollman 171623dce13SRobert Watson inp = sotoinpcb(so); 172623dce13SRobert Watson KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL")); 1732c37256eSGarrett Wollman TCPDEBUG1(); 1742c37256eSGarrett Wollman 1750f6385e7SGleb Smirnoff if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1760f6385e7SGleb Smirnoff error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace); 1772c37256eSGarrett Wollman if (error) 1782c37256eSGarrett Wollman goto out; 1790f6385e7SGleb Smirnoff } 1802c37256eSGarrett Wollman 1810f6385e7SGleb Smirnoff so->so_rcv.sb_flags |= SB_AUTOSIZE; 1820f6385e7SGleb Smirnoff so->so_snd.sb_flags |= SB_AUTOSIZE; 1830f6385e7SGleb Smirnoff error = in_pcballoc(so, &V_tcbinfo); 1847669c586SGleb Smirnoff if (error) 1850f6385e7SGleb Smirnoff goto out; 1860f6385e7SGleb Smirnoff inp = sotoinpcb(so); 1870f6385e7SGleb Smirnoff #ifdef INET6 1880f6385e7SGleb Smirnoff if (inp->inp_vflag & INP_IPV6PROTO) { 1890f6385e7SGleb Smirnoff inp->inp_vflag |= INP_IPV6; 1900f6385e7SGleb Smirnoff if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 1910f6385e7SGleb Smirnoff inp->inp_vflag |= INP_IPV4; 1920f6385e7SGleb Smirnoff inp->in6p_hops = -1; /* use kernel default */ 1930f6385e7SGleb Smirnoff } 1940f6385e7SGleb Smirnoff else 1950f6385e7SGleb Smirnoff #endif 1960f6385e7SGleb Smirnoff inp->inp_vflag |= INP_IPV4; 1970f6385e7SGleb Smirnoff tp = tcp_newtcpcb(inp); 1980f6385e7SGleb Smirnoff if (tp == NULL) { 1997669c586SGleb Smirnoff error = ENOBUFS; 2000f6385e7SGleb Smirnoff in_pcbdetach(inp); 2010f6385e7SGleb Smirnoff in_pcbfree(inp); 2020f6385e7SGleb Smirnoff goto out; 2030f6385e7SGleb Smirnoff } 2040f6385e7SGleb Smirnoff tp->t_state = TCPS_CLOSED; 2050f6385e7SGleb Smirnoff INP_WUNLOCK(inp); 2060f6385e7SGleb Smirnoff TCPSTATES_INC(TCPS_CLOSED); 2072c37256eSGarrett Wollman out: 2082c37256eSGarrett Wollman TCPDEBUG2(PRU_ATTACH); 2095d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ATTACH); 2100f6385e7SGleb Smirnoff return (error); 2112c37256eSGarrett Wollman } 2122c37256eSGarrett Wollman 2132c37256eSGarrett Wollman /* 2143fed74e9SGleb Smirnoff * tcp_usr_detach is called when the socket layer loses its final reference 215a152f8a3SRobert Watson * to the socket, be it a file descriptor reference, a reference from TCP, 216a152f8a3SRobert Watson * etc. At this point, there is only one case in which we will keep around 217a152f8a3SRobert Watson * inpcb state: time wait. 2182c37256eSGarrett Wollman */ 219bc725eafSRobert Watson static void 2203fed74e9SGleb Smirnoff tcp_usr_detach(struct socket *so) 2212c37256eSGarrett Wollman { 2223fed74e9SGleb Smirnoff struct inpcb *inp; 2232c37256eSGarrett Wollman struct tcpcb *tp; 2242c37256eSGarrett Wollman 2253fed74e9SGleb Smirnoff inp = sotoinpcb(so); 2263fed74e9SGleb Smirnoff KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 2273fed74e9SGleb Smirnoff INP_WLOCK(inp); 2283fed74e9SGleb Smirnoff KASSERT(so->so_pcb == inp && inp->inp_socket == so, 2293fed74e9SGleb Smirnoff ("%s: socket %p inp %p mismatch", __func__, so, inp)); 230953b5606SRobert Watson 231a152f8a3SRobert Watson tp = intotcpcb(inp); 232a152f8a3SRobert Watson 233ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 234623dce13SRobert Watson /* 235a152f8a3SRobert Watson * There are two cases to handle: one in which the time wait 236a152f8a3SRobert Watson * state is being discarded (INP_DROPPED), and one in which 237a152f8a3SRobert Watson * this connection will remain in timewait. In the former, 238a152f8a3SRobert Watson * it is time to discard all state (except tcptw, which has 239a152f8a3SRobert Watson * already been discarded by the timewait close code, which 240a152f8a3SRobert Watson * should be further up the call stack somewhere). In the 241a152f8a3SRobert Watson * latter case, we detach from the socket, but leave the pcb 242a152f8a3SRobert Watson * present until timewait ends. 243623dce13SRobert Watson * 244a152f8a3SRobert Watson * XXXRW: Would it be cleaner to free the tcptw here? 245cea40c48SJulien Charbon * 246cea40c48SJulien Charbon * Astute question indeed, from twtcp perspective there are 247dd388cfdSGleb Smirnoff * four cases to consider: 248cea40c48SJulien Charbon * 2493fed74e9SGleb Smirnoff * #1 tcp_usr_detach is called at tcptw creation time by 250cea40c48SJulien Charbon * tcp_twstart, then do not discard the newly created tcptw 251cea40c48SJulien Charbon * and leave inpcb present until timewait ends 2523fed74e9SGleb Smirnoff * #2 tcp_usr_detach is called at tcptw creation time by 253dd388cfdSGleb Smirnoff * tcp_twstart, but connection is local and tw will be 254dd388cfdSGleb Smirnoff * discarded immediately 2553fed74e9SGleb Smirnoff * #3 tcp_usr_detach is called at timewait end (or reuse) by 256cea40c48SJulien Charbon * tcp_twclose, then the tcptw has already been discarded 257ff9b006dSJulien Charbon * (or reused) and inpcb is freed here 2583fed74e9SGleb Smirnoff * #4 tcp_usr_detach is called() after timewait ends (or reuse) 259cea40c48SJulien Charbon * (e.g. by soclose), then tcptw has already been discarded 260ff9b006dSJulien Charbon * (or reused) and inpcb is freed here 261cea40c48SJulien Charbon * 262cea40c48SJulien Charbon * In all three cases the tcptw should not be freed here. 263623dce13SRobert Watson */ 264ad71fe3cSRobert Watson if (inp->inp_flags & INP_DROPPED) { 265623dce13SRobert Watson in_pcbdetach(inp); 266f5cf1e5fSJulien Charbon if (__predict_true(tp == NULL)) { 2670206cdb8SBjoern A. Zeeb in_pcbfree(inp); 2680206cdb8SBjoern A. Zeeb } else { 269f5cf1e5fSJulien Charbon /* 270f5cf1e5fSJulien Charbon * This case should not happen as in TIMEWAIT 271f5cf1e5fSJulien Charbon * state the inp should not be destroyed before 272f5cf1e5fSJulien Charbon * its tcptw. If INVARIANTS is defined, panic. 273f5cf1e5fSJulien Charbon */ 274f5cf1e5fSJulien Charbon #ifdef INVARIANTS 275f5cf1e5fSJulien Charbon panic("%s: Panic before an inp double-free: " 276f5cf1e5fSJulien Charbon "INP_TIMEWAIT && INP_DROPPED && tp != NULL" 277f5cf1e5fSJulien Charbon , __func__); 278f5cf1e5fSJulien Charbon #else 279f5cf1e5fSJulien Charbon log(LOG_ERR, "%s: Avoid an inp double-free: " 280f5cf1e5fSJulien Charbon "INP_TIMEWAIT && INP_DROPPED && tp != NULL" 281f5cf1e5fSJulien Charbon , __func__); 282f5cf1e5fSJulien Charbon #endif 283f5cf1e5fSJulien Charbon INP_WUNLOCK(inp); 284f5cf1e5fSJulien Charbon } 285f5cf1e5fSJulien Charbon } else { 286623dce13SRobert Watson in_pcbdetach(inp); 2878501a69cSRobert Watson INP_WUNLOCK(inp); 288623dce13SRobert Watson } 289623dce13SRobert Watson } else { 290e6e65783SRobert Watson /* 291a152f8a3SRobert Watson * If the connection is not in timewait, we consider two 292a152f8a3SRobert Watson * two conditions: one in which no further processing is 293a152f8a3SRobert Watson * necessary (dropped || embryonic), and one in which TCP is 294a152f8a3SRobert Watson * not yet done, but no longer requires the socket, so the 295a152f8a3SRobert Watson * pcb will persist for the time being. 296a152f8a3SRobert Watson * 297a152f8a3SRobert Watson * XXXRW: Does the second case still occur? 298e6e65783SRobert Watson */ 299ad71fe3cSRobert Watson if (inp->inp_flags & INP_DROPPED || 300623dce13SRobert Watson tp->t_state < TCPS_SYN_SENT) { 301623dce13SRobert Watson tcp_discardcb(tp); 302623dce13SRobert Watson in_pcbdetach(inp); 3030206cdb8SBjoern A. Zeeb in_pcbfree(inp); 304db3cee51SNavdeep Parhar } else { 305a152f8a3SRobert Watson in_pcbdetach(inp); 306db3cee51SNavdeep Parhar INP_WUNLOCK(inp); 307db3cee51SNavdeep Parhar } 308623dce13SRobert Watson } 309623dce13SRobert Watson } 310c78cbc7bSRobert Watson 311b287c6c7SBjoern A. Zeeb #ifdef INET 3122c37256eSGarrett Wollman /* 3132c37256eSGarrett Wollman * Give the socket an address. 3142c37256eSGarrett Wollman */ 3152c37256eSGarrett Wollman static int 316b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 3172c37256eSGarrett Wollman { 3182c37256eSGarrett Wollman int error = 0; 319f76fcf6dSJeffrey Hsu struct inpcb *inp; 320623dce13SRobert Watson struct tcpcb *tp = NULL; 3212c37256eSGarrett Wollman struct sockaddr_in *sinp; 3222c37256eSGarrett Wollman 32352710de1SPawel Jakub Dawidek sinp = (struct sockaddr_in *)nam; 324f96603b5SMark Johnston if (nam->sa_family != AF_INET) { 325f96603b5SMark Johnston /* 326f96603b5SMark Johnston * Preserve compatibility with old programs. 327f96603b5SMark Johnston */ 328f96603b5SMark Johnston if (nam->sa_family != AF_UNSPEC || 3293f1f6b6eSMichael Tuexen nam->sa_len < offsetof(struct sockaddr_in, sin_zero) || 330f96603b5SMark Johnston sinp->sin_addr.s_addr != INADDR_ANY) 331f161d294SMark Johnston return (EAFNOSUPPORT); 332f96603b5SMark Johnston nam->sa_family = AF_INET; 333f96603b5SMark Johnston } 33452710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof(*sinp)) 33552710de1SPawel Jakub Dawidek return (EINVAL); 336f161d294SMark Johnston 3372c37256eSGarrett Wollman /* 3382c37256eSGarrett Wollman * Must check for multicast addresses and disallow binding 3392c37256eSGarrett Wollman * to them. 3402c37256eSGarrett Wollman */ 341f161d294SMark Johnston if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 34252710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 34352710de1SPawel Jakub Dawidek 344623dce13SRobert Watson TCPDEBUG0; 345623dce13SRobert Watson inp = sotoinpcb(so); 346623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 3478501a69cSRobert Watson INP_WLOCK(inp); 348ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 349623dce13SRobert Watson error = EINVAL; 3502c37256eSGarrett Wollman goto out; 351623dce13SRobert Watson } 352623dce13SRobert Watson tp = intotcpcb(inp); 353623dce13SRobert Watson TCPDEBUG1(); 354fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 355623dce13SRobert Watson error = in_pcbbind(inp, nam, td->td_ucred); 356fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 357623dce13SRobert Watson out: 358623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 3595d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 3608501a69cSRobert Watson INP_WUNLOCK(inp); 361623dce13SRobert Watson 362623dce13SRobert Watson return (error); 3632c37256eSGarrett Wollman } 364b287c6c7SBjoern A. Zeeb #endif /* INET */ 3652c37256eSGarrett Wollman 366fb59c426SYoshinobu Inoue #ifdef INET6 367fb59c426SYoshinobu Inoue static int 368b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 369fb59c426SYoshinobu Inoue { 370fb59c426SYoshinobu Inoue int error = 0; 371f76fcf6dSJeffrey Hsu struct inpcb *inp; 372623dce13SRobert Watson struct tcpcb *tp = NULL; 3730ecd976eSBjoern A. Zeeb struct sockaddr_in6 *sin6; 3744a91aa8fSMichael Tuexen u_char vflagsav; 375fb59c426SYoshinobu Inoue 3760ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 377f161d294SMark Johnston if (nam->sa_family != AF_INET6) 378f161d294SMark Johnston return (EAFNOSUPPORT); 3790ecd976eSBjoern A. Zeeb if (nam->sa_len != sizeof(*sin6)) 38052710de1SPawel Jakub Dawidek return (EINVAL); 381f161d294SMark Johnston 382fb59c426SYoshinobu Inoue /* 383fb59c426SYoshinobu Inoue * Must check for multicast addresses and disallow binding 384fb59c426SYoshinobu Inoue * to them. 385fb59c426SYoshinobu Inoue */ 386f161d294SMark Johnston if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 38752710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 38852710de1SPawel Jakub Dawidek 389623dce13SRobert Watson TCPDEBUG0; 390623dce13SRobert Watson inp = sotoinpcb(so); 391623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 3928501a69cSRobert Watson INP_WLOCK(inp); 3934a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 394ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 395623dce13SRobert Watson error = EINVAL; 396623dce13SRobert Watson goto out; 397623dce13SRobert Watson } 398623dce13SRobert Watson tp = intotcpcb(inp); 399623dce13SRobert Watson TCPDEBUG1(); 400fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 401fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 402fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 403b287c6c7SBjoern A. Zeeb #ifdef INET 40466ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 4050ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 406fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 4070ecd976eSBjoern A. Zeeb else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 408fb59c426SYoshinobu Inoue struct sockaddr_in sin; 409fb59c426SYoshinobu Inoue 4100ecd976eSBjoern A. Zeeb in6_sin6_2_sin(&sin, sin6); 411888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 412888973f5SMichael Tuexen error = EAFNOSUPPORT; 413888973f5SMichael Tuexen INP_HASH_WUNLOCK(&V_tcbinfo); 414888973f5SMichael Tuexen goto out; 415888973f5SMichael Tuexen } 416fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 417fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 418b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)&sin, 419b0330ed9SPawel Jakub Dawidek td->td_ucred); 420fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 421fb59c426SYoshinobu Inoue goto out; 422fb59c426SYoshinobu Inoue } 423fb59c426SYoshinobu Inoue } 424b287c6c7SBjoern A. Zeeb #endif 425b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, nam, td->td_ucred); 426fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 427623dce13SRobert Watson out: 4284a91aa8fSMichael Tuexen if (error != 0) 4294a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 430623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 4315d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 4328501a69cSRobert Watson INP_WUNLOCK(inp); 433623dce13SRobert Watson return (error); 434fb59c426SYoshinobu Inoue } 435fb59c426SYoshinobu Inoue #endif /* INET6 */ 436fb59c426SYoshinobu Inoue 437b287c6c7SBjoern A. Zeeb #ifdef INET 4382c37256eSGarrett Wollman /* 4392c37256eSGarrett Wollman * Prepare to accept connections. 4402c37256eSGarrett Wollman */ 4412c37256eSGarrett Wollman static int 442d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td) 4432c37256eSGarrett Wollman { 4442c37256eSGarrett Wollman int error = 0; 445f76fcf6dSJeffrey Hsu struct inpcb *inp; 446623dce13SRobert Watson struct tcpcb *tp = NULL; 4472c37256eSGarrett Wollman 448623dce13SRobert Watson TCPDEBUG0; 449623dce13SRobert Watson inp = sotoinpcb(so); 450623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 4518501a69cSRobert Watson INP_WLOCK(inp); 452ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 453623dce13SRobert Watson error = EINVAL; 454623dce13SRobert Watson goto out; 455623dce13SRobert Watson } 456623dce13SRobert Watson tp = intotcpcb(inp); 457623dce13SRobert Watson TCPDEBUG1(); 4580daccb9cSRobert Watson SOCK_LOCK(so); 4590daccb9cSRobert Watson error = solisten_proto_check(so); 460bd4a39ccSMark Johnston if (error != 0) { 461bd4a39ccSMark Johnston SOCK_UNLOCK(so); 462bd4a39ccSMark Johnston goto out; 463bd4a39ccSMark Johnston } 464bd4a39ccSMark Johnston if (inp->inp_lport == 0) { 465fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 466bd4a39ccSMark Johnston error = in_pcbbind(inp, NULL, td->td_ucred); 467fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 468bd4a39ccSMark Johnston } 4690daccb9cSRobert Watson if (error == 0) { 47057f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 471d374e81eSRobert Watson solisten_proto(so, backlog); 47209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 47337cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 47409fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 47509fe6320SNavdeep Parhar #endif 476bd4a39ccSMark Johnston } else { 477bd4a39ccSMark Johnston solisten_proto_abort(so); 4780daccb9cSRobert Watson } 4790daccb9cSRobert Watson SOCK_UNLOCK(so); 480623dce13SRobert Watson 48168bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) 482281a0fd4SPatrick Kelsey tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 48318a75309SPatrick Kelsey 484623dce13SRobert Watson out: 485623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 4865d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 4878501a69cSRobert Watson INP_WUNLOCK(inp); 488623dce13SRobert Watson return (error); 4892c37256eSGarrett Wollman } 490b287c6c7SBjoern A. Zeeb #endif /* INET */ 4912c37256eSGarrett Wollman 492fb59c426SYoshinobu Inoue #ifdef INET6 493fb59c426SYoshinobu Inoue static int 494d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 495fb59c426SYoshinobu Inoue { 496fb59c426SYoshinobu Inoue int error = 0; 497f76fcf6dSJeffrey Hsu struct inpcb *inp; 498623dce13SRobert Watson struct tcpcb *tp = NULL; 4994a91aa8fSMichael Tuexen u_char vflagsav; 500fb59c426SYoshinobu Inoue 501623dce13SRobert Watson TCPDEBUG0; 502623dce13SRobert Watson inp = sotoinpcb(so); 503623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL")); 5048501a69cSRobert Watson INP_WLOCK(inp); 505ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 506623dce13SRobert Watson error = EINVAL; 507623dce13SRobert Watson goto out; 508623dce13SRobert Watson } 5094a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 510623dce13SRobert Watson tp = intotcpcb(inp); 511623dce13SRobert Watson TCPDEBUG1(); 5120daccb9cSRobert Watson SOCK_LOCK(so); 5130daccb9cSRobert Watson error = solisten_proto_check(so); 514bd4a39ccSMark Johnston if (error != 0) { 515bd4a39ccSMark Johnston SOCK_UNLOCK(so); 516bd4a39ccSMark Johnston goto out; 517bd4a39ccSMark Johnston } 518fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 519bd4a39ccSMark Johnston if (inp->inp_lport == 0) { 520fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 52166ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 522fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 523bd4a39ccSMark Johnston error = in6_pcbbind(inp, NULL, td->td_ucred); 524fb59c426SYoshinobu Inoue } 525fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 5260daccb9cSRobert Watson if (error == 0) { 52757f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 528d374e81eSRobert Watson solisten_proto(so, backlog); 52909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 53037cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 53109fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 53209fe6320SNavdeep Parhar #endif 533bd4a39ccSMark Johnston } else { 534bd4a39ccSMark Johnston solisten_proto_abort(so); 5350daccb9cSRobert Watson } 5360daccb9cSRobert Watson SOCK_UNLOCK(so); 537623dce13SRobert Watson 53868bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) 539281a0fd4SPatrick Kelsey tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 54018a75309SPatrick Kelsey 5414a91aa8fSMichael Tuexen if (error != 0) 5424a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 5434a91aa8fSMichael Tuexen 544623dce13SRobert Watson out: 545623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 5465d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 5478501a69cSRobert Watson INP_WUNLOCK(inp); 548623dce13SRobert Watson return (error); 549fb59c426SYoshinobu Inoue } 550fb59c426SYoshinobu Inoue #endif /* INET6 */ 551fb59c426SYoshinobu Inoue 552b287c6c7SBjoern A. Zeeb #ifdef INET 5532c37256eSGarrett Wollman /* 5542c37256eSGarrett Wollman * Initiate connection to peer. 5552c37256eSGarrett Wollman * Create a template for use in transmissions on this connection. 5562c37256eSGarrett Wollman * Enter SYN_SENT state, and mark socket as connecting. 5572c37256eSGarrett Wollman * Start keep-alive timer, and seed output sequence space. 5582c37256eSGarrett Wollman * Send initial segment on connection. 5592c37256eSGarrett Wollman */ 5602c37256eSGarrett Wollman static int 561b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 5622c37256eSGarrett Wollman { 563109eb549SGleb Smirnoff struct epoch_tracker et; 5642c37256eSGarrett Wollman int error = 0; 565f76fcf6dSJeffrey Hsu struct inpcb *inp; 566623dce13SRobert Watson struct tcpcb *tp = NULL; 5672c37256eSGarrett Wollman struct sockaddr_in *sinp; 5682c37256eSGarrett Wollman 56957bf258eSGarrett Wollman sinp = (struct sockaddr_in *)nam; 570f161d294SMark Johnston if (nam->sa_family != AF_INET) 571f161d294SMark Johnston return (EAFNOSUPPORT); 572e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sinp)) 573e29ef13fSDon Lewis return (EINVAL); 574f161d294SMark Johnston 57552710de1SPawel Jakub Dawidek /* 57652710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 57752710de1SPawel Jakub Dawidek */ 578f161d294SMark Johnston if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 57952710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 580f161d294SMark Johnston if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) 581f903a308SMichael Tuexen return (EACCES); 582b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0) 583b89e82ddSJamie Gritton return (error); 58475c13541SPoul-Henning Kamp 585623dce13SRobert Watson TCPDEBUG0; 586623dce13SRobert Watson inp = sotoinpcb(so); 587623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 5888501a69cSRobert Watson INP_WLOCK(inp); 589eb96dc33SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) { 590eb96dc33SJulien Charbon error = EADDRINUSE; 591eb96dc33SJulien Charbon goto out; 592eb96dc33SJulien Charbon } 593eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 594eb96dc33SJulien Charbon error = ECONNREFUSED; 595623dce13SRobert Watson goto out; 596623dce13SRobert Watson } 597bd4a39ccSMark Johnston if (SOLISTENING(so)) { 598bd4a39ccSMark Johnston error = EOPNOTSUPP; 599bd4a39ccSMark Johnston goto out; 600bd4a39ccSMark Johnston } 601623dce13SRobert Watson tp = intotcpcb(inp); 602623dce13SRobert Watson TCPDEBUG1(); 603c1604fe4SGleb Smirnoff NET_EPOCH_ENTER(et); 604b40ce416SJulian Elischer if ((error = tcp_connect(tp, nam, td)) != 0) 605c1604fe4SGleb Smirnoff goto out_in_epoch; 60609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 60709fe6320SNavdeep Parhar if (registered_toedevs > 0 && 60837cc0ecbSNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 60909fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 610c1604fe4SGleb Smirnoff goto out_in_epoch; 61109fe6320SNavdeep Parhar #endif 61209fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 61355bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 614c1604fe4SGleb Smirnoff out_in_epoch: 615109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 616623dce13SRobert Watson out: 617623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 618e79cb051SGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CONNECT); 6198501a69cSRobert Watson INP_WUNLOCK(inp); 620623dce13SRobert Watson return (error); 6212c37256eSGarrett Wollman } 622b287c6c7SBjoern A. Zeeb #endif /* INET */ 6232c37256eSGarrett Wollman 624fb59c426SYoshinobu Inoue #ifdef INET6 625fb59c426SYoshinobu Inoue static int 626b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 627fb59c426SYoshinobu Inoue { 628109eb549SGleb Smirnoff struct epoch_tracker et; 629fb59c426SYoshinobu Inoue int error = 0; 630f76fcf6dSJeffrey Hsu struct inpcb *inp; 631623dce13SRobert Watson struct tcpcb *tp = NULL; 6320ecd976eSBjoern A. Zeeb struct sockaddr_in6 *sin6; 6334a91aa8fSMichael Tuexen u_int8_t incflagsav; 6344a91aa8fSMichael Tuexen u_char vflagsav; 635623dce13SRobert Watson 636623dce13SRobert Watson TCPDEBUG0; 637fb59c426SYoshinobu Inoue 6380ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 639f161d294SMark Johnston if (nam->sa_family != AF_INET6) 640f161d294SMark Johnston return (EAFNOSUPPORT); 6410ecd976eSBjoern A. Zeeb if (nam->sa_len != sizeof (*sin6)) 642e29ef13fSDon Lewis return (EINVAL); 643f161d294SMark Johnston 64452710de1SPawel Jakub Dawidek /* 64552710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 64652710de1SPawel Jakub Dawidek */ 647f161d294SMark Johnston if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 64852710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 649fb59c426SYoshinobu Inoue 650623dce13SRobert Watson inp = sotoinpcb(so); 651623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 6528501a69cSRobert Watson INP_WLOCK(inp); 6534a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 6544a91aa8fSMichael Tuexen incflagsav = inp->inp_inc.inc_flags; 655eb96dc33SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) { 656eb96dc33SJulien Charbon error = EADDRINUSE; 657eb96dc33SJulien Charbon goto out; 658eb96dc33SJulien Charbon } 659eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 660eb96dc33SJulien Charbon error = ECONNREFUSED; 661623dce13SRobert Watson goto out; 662623dce13SRobert Watson } 663bd4a39ccSMark Johnston if (SOLISTENING(so)) { 664bd4a39ccSMark Johnston error = EINVAL; 665bd4a39ccSMark Johnston goto out; 666bd4a39ccSMark Johnston } 667623dce13SRobert Watson tp = intotcpcb(inp); 668623dce13SRobert Watson TCPDEBUG1(); 669b287c6c7SBjoern A. Zeeb #ifdef INET 670fa046d87SRobert Watson /* 671fa046d87SRobert Watson * XXXRW: Some confusion: V4/V6 flags relate to binding, and 672fa046d87SRobert Watson * therefore probably require the hash lock, which isn't held here. 673fa046d87SRobert Watson * Is this a significant problem? 674fa046d87SRobert Watson */ 6750ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 676fb59c426SYoshinobu Inoue struct sockaddr_in sin; 677fb59c426SYoshinobu Inoue 678d46a5312SMaxim Konovalov if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 679d46a5312SMaxim Konovalov error = EINVAL; 680d46a5312SMaxim Konovalov goto out; 681d46a5312SMaxim Konovalov } 6825dba6adaSMichael Tuexen if ((inp->inp_vflag & INP_IPV4) == 0) { 6835dba6adaSMichael Tuexen error = EAFNOSUPPORT; 6845dba6adaSMichael Tuexen goto out; 6855dba6adaSMichael Tuexen } 68633841545SHajimu UMEMOTO 6870ecd976eSBjoern A. Zeeb in6_sin6_2_sin(&sin, sin6); 688888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 689888973f5SMichael Tuexen error = EAFNOSUPPORT; 690888973f5SMichael Tuexen goto out; 691888973f5SMichael Tuexen } 692f903a308SMichael Tuexen if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) { 693f903a308SMichael Tuexen error = EACCES; 6942cf21ae5SRandall Stewart goto out; 6952cf21ae5SRandall Stewart } 696b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, 697b89e82ddSJamie Gritton &sin.sin_addr)) != 0) 698413628a7SBjoern A. Zeeb goto out; 6994a91aa8fSMichael Tuexen inp->inp_vflag |= INP_IPV4; 7004a91aa8fSMichael Tuexen inp->inp_vflag &= ~INP_IPV6; 701c1604fe4SGleb Smirnoff NET_EPOCH_ENTER(et); 702b40ce416SJulian Elischer if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 703c1604fe4SGleb Smirnoff goto out_in_epoch; 70409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 70509fe6320SNavdeep Parhar if (registered_toedevs > 0 && 706adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 70709fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 708c1604fe4SGleb Smirnoff goto out_in_epoch; 70909fe6320SNavdeep Parhar #endif 71055bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 711c1604fe4SGleb Smirnoff goto out_in_epoch; 7125dba6adaSMichael Tuexen } else { 7135dba6adaSMichael Tuexen if ((inp->inp_vflag & INP_IPV6) == 0) { 7145dba6adaSMichael Tuexen error = EAFNOSUPPORT; 7155dba6adaSMichael Tuexen goto out; 7165dba6adaSMichael Tuexen } 717fb59c426SYoshinobu Inoue } 718b287c6c7SBjoern A. Zeeb #endif 7194a91aa8fSMichael Tuexen if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0) 7204a91aa8fSMichael Tuexen goto out; 721fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 722fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 723dcdb4371SBjoern A. Zeeb inp->inp_inc.inc_flags |= INC_ISIPV6; 724b40ce416SJulian Elischer if ((error = tcp6_connect(tp, nam, td)) != 0) 725fb59c426SYoshinobu Inoue goto out; 72609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 72709fe6320SNavdeep Parhar if (registered_toedevs > 0 && 728adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 72909fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 73009fe6320SNavdeep Parhar goto out; 73109fe6320SNavdeep Parhar #endif 73209fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 733109eb549SGleb Smirnoff NET_EPOCH_ENTER(et); 73455bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 7357754e281SBjoern A. Zeeb #ifdef INET 736c1604fe4SGleb Smirnoff out_in_epoch: 7377754e281SBjoern A. Zeeb #endif 738109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 739623dce13SRobert Watson out: 7404a91aa8fSMichael Tuexen /* 7414a91aa8fSMichael Tuexen * If the implicit bind in the connect call fails, restore 7424a91aa8fSMichael Tuexen * the flags we modified. 7434a91aa8fSMichael Tuexen */ 7444a91aa8fSMichael Tuexen if (error != 0 && inp->inp_lport == 0) { 7454a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 7464a91aa8fSMichael Tuexen inp->inp_inc.inc_flags = incflagsav; 7474a91aa8fSMichael Tuexen } 7484a91aa8fSMichael Tuexen 749623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 7505d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CONNECT); 7518501a69cSRobert Watson INP_WUNLOCK(inp); 752623dce13SRobert Watson return (error); 753fb59c426SYoshinobu Inoue } 754fb59c426SYoshinobu Inoue #endif /* INET6 */ 755fb59c426SYoshinobu Inoue 7562c37256eSGarrett Wollman /* 7572c37256eSGarrett Wollman * Initiate disconnect from peer. 7582c37256eSGarrett Wollman * If connection never passed embryonic stage, just drop; 7592c37256eSGarrett Wollman * else if don't need to let data drain, then can just drop anyways, 7602c37256eSGarrett Wollman * else have to begin TCP shutdown process: mark socket disconnecting, 7612c37256eSGarrett Wollman * drain unread data, state switch to reflect user close, and 7622c37256eSGarrett Wollman * send segment (e.g. FIN) to peer. Socket will be really disconnected 7632c37256eSGarrett Wollman * when peer sends FIN and acks ours. 7642c37256eSGarrett Wollman * 7652c37256eSGarrett Wollman * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 7662c37256eSGarrett Wollman */ 7672c37256eSGarrett Wollman static int 7682c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so) 7692c37256eSGarrett Wollman { 770f76fcf6dSJeffrey Hsu struct inpcb *inp; 771623dce13SRobert Watson struct tcpcb *tp = NULL; 7726573d758SMatt Macy struct epoch_tracker et; 773623dce13SRobert Watson int error = 0; 7742c37256eSGarrett Wollman 775623dce13SRobert Watson TCPDEBUG0; 77697a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 777623dce13SRobert Watson inp = sotoinpcb(so); 778623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 7798501a69cSRobert Watson INP_WLOCK(inp); 780489dcc92SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) 781489dcc92SJulien Charbon goto out; 782489dcc92SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 78321367f63SSam Leffler error = ECONNRESET; 784623dce13SRobert Watson goto out; 785623dce13SRobert Watson } 786623dce13SRobert Watson tp = intotcpcb(inp); 787623dce13SRobert Watson TCPDEBUG1(); 788623dce13SRobert Watson tcp_disconnect(tp); 789623dce13SRobert Watson out: 790623dce13SRobert Watson TCPDEBUG2(PRU_DISCONNECT); 7915d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_DISCONNECT); 7928501a69cSRobert Watson INP_WUNLOCK(inp); 79397a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 794623dce13SRobert Watson return (error); 7952c37256eSGarrett Wollman } 7962c37256eSGarrett Wollman 797b287c6c7SBjoern A. Zeeb #ifdef INET 7982c37256eSGarrett Wollman /* 7998296cddfSRobert Watson * Accept a connection. Essentially all the work is done at higher levels; 8008296cddfSRobert Watson * just return the address of the peer, storing through addr. 8012c37256eSGarrett Wollman */ 8022c37256eSGarrett Wollman static int 80357bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam) 8042c37256eSGarrett Wollman { 8052c37256eSGarrett Wollman int error = 0; 806f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 8071db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 80826ef6ac4SDon Lewis struct in_addr addr; 80926ef6ac4SDon Lewis in_port_t port = 0; 8101db24ffbSJonathan Lemon TCPDEBUG0; 8112c37256eSGarrett Wollman 8123d2d3ef4SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 8133d2d3ef4SRobert Watson return (ECONNABORTED); 814f76fcf6dSJeffrey Hsu 815f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 816623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 8178501a69cSRobert Watson INP_WLOCK(inp); 818ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 8193d2d3ef4SRobert Watson error = ECONNABORTED; 820623dce13SRobert Watson goto out; 821623dce13SRobert Watson } 8221db24ffbSJonathan Lemon tp = intotcpcb(inp); 8231db24ffbSJonathan Lemon TCPDEBUG1(); 824f76fcf6dSJeffrey Hsu 825f76fcf6dSJeffrey Hsu /* 82654d642bbSRobert Watson * We inline in_getpeeraddr and COMMON_END here, so that we can 82726ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 82826ef6ac4SDon Lewis * release the lock. 829f76fcf6dSJeffrey Hsu */ 83026ef6ac4SDon Lewis port = inp->inp_fport; 83126ef6ac4SDon Lewis addr = inp->inp_faddr; 832f76fcf6dSJeffrey Hsu 833623dce13SRobert Watson out: 834623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 8355d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 8368501a69cSRobert Watson INP_WUNLOCK(inp); 83726ef6ac4SDon Lewis if (error == 0) 83826ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 83926ef6ac4SDon Lewis return error; 8402c37256eSGarrett Wollman } 841b287c6c7SBjoern A. Zeeb #endif /* INET */ 8422c37256eSGarrett Wollman 843fb59c426SYoshinobu Inoue #ifdef INET6 844fb59c426SYoshinobu Inoue static int 845fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 846fb59c426SYoshinobu Inoue { 847f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 848fb59c426SYoshinobu Inoue int error = 0; 8491db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 85026ef6ac4SDon Lewis struct in_addr addr; 85126ef6ac4SDon Lewis struct in6_addr addr6; 8526573d758SMatt Macy struct epoch_tracker et; 85326ef6ac4SDon Lewis in_port_t port = 0; 85426ef6ac4SDon Lewis int v4 = 0; 8551db24ffbSJonathan Lemon TCPDEBUG0; 856fb59c426SYoshinobu Inoue 857b4470c16SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 858b4470c16SRobert Watson return (ECONNABORTED); 859f76fcf6dSJeffrey Hsu 860f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 861623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 86297a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 8638501a69cSRobert Watson INP_WLOCK(inp); 864ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 86521367f63SSam Leffler error = ECONNABORTED; 866623dce13SRobert Watson goto out; 867623dce13SRobert Watson } 8681db24ffbSJonathan Lemon tp = intotcpcb(inp); 8691db24ffbSJonathan Lemon TCPDEBUG1(); 870623dce13SRobert Watson 87126ef6ac4SDon Lewis /* 87226ef6ac4SDon Lewis * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 87326ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 87426ef6ac4SDon Lewis * release the lock. 87526ef6ac4SDon Lewis */ 87626ef6ac4SDon Lewis if (inp->inp_vflag & INP_IPV4) { 87726ef6ac4SDon Lewis v4 = 1; 87826ef6ac4SDon Lewis port = inp->inp_fport; 87926ef6ac4SDon Lewis addr = inp->inp_faddr; 88026ef6ac4SDon Lewis } else { 88126ef6ac4SDon Lewis port = inp->inp_fport; 88226ef6ac4SDon Lewis addr6 = inp->in6p_faddr; 88326ef6ac4SDon Lewis } 88426ef6ac4SDon Lewis 885623dce13SRobert Watson out: 886623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 8875d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 8888501a69cSRobert Watson INP_WUNLOCK(inp); 88997a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 89026ef6ac4SDon Lewis if (error == 0) { 89126ef6ac4SDon Lewis if (v4) 89226ef6ac4SDon Lewis *nam = in6_v4mapsin6_sockaddr(port, &addr); 89326ef6ac4SDon Lewis else 89426ef6ac4SDon Lewis *nam = in6_sockaddr(port, &addr6); 89526ef6ac4SDon Lewis } 89626ef6ac4SDon Lewis return error; 897fb59c426SYoshinobu Inoue } 898fb59c426SYoshinobu Inoue #endif /* INET6 */ 899f76fcf6dSJeffrey Hsu 900f76fcf6dSJeffrey Hsu /* 9012c37256eSGarrett Wollman * Mark the connection as being incapable of further output. 9022c37256eSGarrett Wollman */ 9032c37256eSGarrett Wollman static int 9042c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so) 9052c37256eSGarrett Wollman { 9062c37256eSGarrett Wollman int error = 0; 907f76fcf6dSJeffrey Hsu struct inpcb *inp; 908623dce13SRobert Watson struct tcpcb *tp = NULL; 9096573d758SMatt Macy struct epoch_tracker et; 9102c37256eSGarrett Wollman 911623dce13SRobert Watson TCPDEBUG0; 91297a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 913623dce13SRobert Watson inp = sotoinpcb(so); 914623dce13SRobert Watson KASSERT(inp != NULL, ("inp == NULL")); 9158501a69cSRobert Watson INP_WLOCK(inp); 916ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 91721367f63SSam Leffler error = ECONNRESET; 918623dce13SRobert Watson goto out; 919623dce13SRobert Watson } 920623dce13SRobert Watson tp = intotcpcb(inp); 921623dce13SRobert Watson TCPDEBUG1(); 9222c37256eSGarrett Wollman socantsendmore(so); 923623dce13SRobert Watson tcp_usrclosed(tp); 924ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 92555bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 926623dce13SRobert Watson 927623dce13SRobert Watson out: 928623dce13SRobert Watson TCPDEBUG2(PRU_SHUTDOWN); 9295d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN); 9308501a69cSRobert Watson INP_WUNLOCK(inp); 93197a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 932623dce13SRobert Watson 933623dce13SRobert Watson return (error); 9342c37256eSGarrett Wollman } 9352c37256eSGarrett Wollman 9362c37256eSGarrett Wollman /* 9372c37256eSGarrett Wollman * After a receive, possibly send window update to peer. 9382c37256eSGarrett Wollman */ 9392c37256eSGarrett Wollman static int 9402c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags) 9412c37256eSGarrett Wollman { 942109eb549SGleb Smirnoff struct epoch_tracker et; 943f76fcf6dSJeffrey Hsu struct inpcb *inp; 944623dce13SRobert Watson struct tcpcb *tp = NULL; 945623dce13SRobert Watson int error = 0; 9462c37256eSGarrett Wollman 947623dce13SRobert Watson TCPDEBUG0; 948623dce13SRobert Watson inp = sotoinpcb(so); 949623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 9508501a69cSRobert Watson INP_WLOCK(inp); 951ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 95221367f63SSam Leffler error = ECONNRESET; 953623dce13SRobert Watson goto out; 954623dce13SRobert Watson } 955623dce13SRobert Watson tp = intotcpcb(inp); 956623dce13SRobert Watson TCPDEBUG1(); 957281a0fd4SPatrick Kelsey /* 958281a0fd4SPatrick Kelsey * For passively-created TFO connections, don't attempt a window 959281a0fd4SPatrick Kelsey * update while still in SYN_RECEIVED as this may trigger an early 960281a0fd4SPatrick Kelsey * SYN|ACK. It is preferable to have the SYN|ACK be sent along with 961281a0fd4SPatrick Kelsey * application response data, or failing that, when the DELACK timer 962281a0fd4SPatrick Kelsey * expires. 963281a0fd4SPatrick Kelsey */ 96468bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 965281a0fd4SPatrick Kelsey (tp->t_state == TCPS_SYN_RECEIVED)) 966281a0fd4SPatrick Kelsey goto out; 96742ce7937SGleb Smirnoff NET_EPOCH_ENTER(et); 96809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 96909fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) 97009fe6320SNavdeep Parhar tcp_offload_rcvd(tp); 971460cf046SNavdeep Parhar else 97209fe6320SNavdeep Parhar #endif 97355bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 974109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 975623dce13SRobert Watson out: 976623dce13SRobert Watson TCPDEBUG2(PRU_RCVD); 9775d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVD); 9788501a69cSRobert Watson INP_WUNLOCK(inp); 979623dce13SRobert Watson return (error); 9802c37256eSGarrett Wollman } 9812c37256eSGarrett Wollman 9822c37256eSGarrett Wollman /* 9832c37256eSGarrett Wollman * Do a send by putting data in output queue and updating urgent 9849c9906e9SPeter Wemm * marker if URG set. Possibly send more data. Unlike the other 9859c9906e9SPeter Wemm * pru_*() routines, the mbuf chains are our responsibility. We 9869c9906e9SPeter Wemm * must either enqueue them or free them. The other pru_* routines 9879c9906e9SPeter Wemm * generally are caller-frees. 9882c37256eSGarrett Wollman */ 9892c37256eSGarrett Wollman static int 99057bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 991b40ce416SJulian Elischer struct sockaddr *nam, struct mbuf *control, struct thread *td) 9922c37256eSGarrett Wollman { 99397a95ee1SGleb Smirnoff struct epoch_tracker et; 9942c37256eSGarrett Wollman int error = 0; 995f76fcf6dSJeffrey Hsu struct inpcb *inp; 996623dce13SRobert Watson struct tcpcb *tp = NULL; 997888973f5SMichael Tuexen #ifdef INET 99851e08d53SMichael Tuexen #ifdef INET6 99951e08d53SMichael Tuexen struct sockaddr_in sin; 100051e08d53SMichael Tuexen #endif 100151e08d53SMichael Tuexen struct sockaddr_in *sinp; 1002888973f5SMichael Tuexen #endif 1003fb59c426SYoshinobu Inoue #ifdef INET6 1004fb59c426SYoshinobu Inoue int isipv6; 1005fb59c426SYoshinobu Inoue #endif 10064a91aa8fSMichael Tuexen u_int8_t incflagsav; 10074a91aa8fSMichael Tuexen u_char vflagsav; 10084a91aa8fSMichael Tuexen bool restoreflags; 10099c9906e9SPeter Wemm TCPDEBUG0; 10102c37256eSGarrett Wollman 1011f76fcf6dSJeffrey Hsu /* 101297a95ee1SGleb Smirnoff * We require the pcbinfo "read lock" if we will close the socket 101397a95ee1SGleb Smirnoff * as part of this call. 1014f76fcf6dSJeffrey Hsu */ 101597a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 1016f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 1017623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 10188501a69cSRobert Watson INP_WLOCK(inp); 10194a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 10204a91aa8fSMichael Tuexen incflagsav = inp->inp_inc.inc_flags; 10214a91aa8fSMichael Tuexen restoreflags = false; 1022ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 10237ff0b850SAndre Oppermann if (control) 10247ff0b850SAndre Oppermann m_freem(control); 102521367f63SSam Leffler error = ECONNRESET; 10269c9906e9SPeter Wemm goto out; 10279c9906e9SPeter Wemm } 1028d8acd268SMark Johnston if (control != NULL) { 1029d8acd268SMark Johnston /* TCP doesn't do control messages (rights, creds, etc) */ 1030d8acd268SMark Johnston if (control->m_len) { 1031d8acd268SMark Johnston m_freem(control); 1032d8acd268SMark Johnston error = EINVAL; 1033d8acd268SMark Johnston goto out; 1034d8acd268SMark Johnston } 1035d8acd268SMark Johnston m_freem(control); /* empty control, just free it */ 1036d8acd268SMark Johnston control = NULL; 1037d8acd268SMark Johnston } 10389c9906e9SPeter Wemm tp = intotcpcb(inp); 10397d2608a5SMark Johnston if ((flags & PRUS_OOB) != 0 && 10407d2608a5SMark Johnston (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0) 1041d3b6c96bSRandall Stewart goto out; 10427d2608a5SMark Johnston 10439c9906e9SPeter Wemm TCPDEBUG1(); 1044888973f5SMichael Tuexen if (nam != NULL && tp->t_state < TCPS_SYN_SENT) { 1045bd4a39ccSMark Johnston if (tp->t_state == TCPS_LISTEN) { 1046bd4a39ccSMark Johnston error = EINVAL; 1047bd4a39ccSMark Johnston goto out; 1048bd4a39ccSMark Johnston } 1049888973f5SMichael Tuexen switch (nam->sa_family) { 1050888973f5SMichael Tuexen #ifdef INET 1051888973f5SMichael Tuexen case AF_INET: 1052888973f5SMichael Tuexen sinp = (struct sockaddr_in *)nam; 1053888973f5SMichael Tuexen if (sinp->sin_len != sizeof(struct sockaddr_in)) { 1054888973f5SMichael Tuexen error = EINVAL; 1055888973f5SMichael Tuexen goto out; 1056888973f5SMichael Tuexen } 1057888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV6) != 0) { 1058888973f5SMichael Tuexen error = EAFNOSUPPORT; 1059888973f5SMichael Tuexen goto out; 1060888973f5SMichael Tuexen } 1061888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 1062888973f5SMichael Tuexen error = EAFNOSUPPORT; 1063888973f5SMichael Tuexen goto out; 1064888973f5SMichael Tuexen } 1065f903a308SMichael Tuexen if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) { 1066f903a308SMichael Tuexen error = EACCES; 10672cf21ae5SRandall Stewart goto out; 10682cf21ae5SRandall Stewart } 1069888973f5SMichael Tuexen if ((error = prison_remote_ip4(td->td_ucred, 10707d2608a5SMark Johnston &sinp->sin_addr))) 1071888973f5SMichael Tuexen goto out; 1072888973f5SMichael Tuexen #ifdef INET6 1073888973f5SMichael Tuexen isipv6 = 0; 1074888973f5SMichael Tuexen #endif 1075888973f5SMichael Tuexen break; 1076888973f5SMichael Tuexen #endif /* INET */ 1077888973f5SMichael Tuexen #ifdef INET6 1078888973f5SMichael Tuexen case AF_INET6: 1079888973f5SMichael Tuexen { 10800ecd976eSBjoern A. Zeeb struct sockaddr_in6 *sin6; 1081888973f5SMichael Tuexen 10820ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 10830ecd976eSBjoern A. Zeeb if (sin6->sin6_len != sizeof(*sin6)) { 1084888973f5SMichael Tuexen error = EINVAL; 1085888973f5SMichael Tuexen goto out; 1086888973f5SMichael Tuexen } 1087e240ce42SMichael Tuexen if ((inp->inp_vflag & INP_IPV6PROTO) == 0) { 1088e240ce42SMichael Tuexen error = EAFNOSUPPORT; 1089e240ce42SMichael Tuexen goto out; 1090e240ce42SMichael Tuexen } 10910ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 1092888973f5SMichael Tuexen error = EAFNOSUPPORT; 1093888973f5SMichael Tuexen goto out; 1094888973f5SMichael Tuexen } 10950ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 1096888973f5SMichael Tuexen #ifdef INET 1097888973f5SMichael Tuexen if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 1098888973f5SMichael Tuexen error = EINVAL; 1099888973f5SMichael Tuexen goto out; 1100888973f5SMichael Tuexen } 1101888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV4) == 0) { 1102888973f5SMichael Tuexen error = EAFNOSUPPORT; 1103888973f5SMichael Tuexen goto out; 1104888973f5SMichael Tuexen } 11054a91aa8fSMichael Tuexen restoreflags = true; 1106888973f5SMichael Tuexen inp->inp_vflag &= ~INP_IPV6; 1107888973f5SMichael Tuexen sinp = &sin; 11080ecd976eSBjoern A. Zeeb in6_sin6_2_sin(sinp, sin6); 1109888973f5SMichael Tuexen if (IN_MULTICAST( 1110888973f5SMichael Tuexen ntohl(sinp->sin_addr.s_addr))) { 1111888973f5SMichael Tuexen error = EAFNOSUPPORT; 1112888973f5SMichael Tuexen goto out; 1113888973f5SMichael Tuexen } 1114888973f5SMichael Tuexen if ((error = prison_remote_ip4(td->td_ucred, 11157d2608a5SMark Johnston &sinp->sin_addr))) 1116888973f5SMichael Tuexen goto out; 1117888973f5SMichael Tuexen isipv6 = 0; 1118888973f5SMichael Tuexen #else /* !INET */ 1119888973f5SMichael Tuexen error = EAFNOSUPPORT; 1120888973f5SMichael Tuexen goto out; 1121888973f5SMichael Tuexen #endif /* INET */ 1122888973f5SMichael Tuexen } else { 1123888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV6) == 0) { 1124888973f5SMichael Tuexen error = EAFNOSUPPORT; 1125888973f5SMichael Tuexen goto out; 1126888973f5SMichael Tuexen } 11274a91aa8fSMichael Tuexen restoreflags = true; 1128888973f5SMichael Tuexen inp->inp_vflag &= ~INP_IPV4; 1129888973f5SMichael Tuexen inp->inp_inc.inc_flags |= INC_ISIPV6; 1130888973f5SMichael Tuexen if ((error = prison_remote_ip6(td->td_ucred, 11317d2608a5SMark Johnston &sin6->sin6_addr))) 1132888973f5SMichael Tuexen goto out; 1133888973f5SMichael Tuexen isipv6 = 1; 1134888973f5SMichael Tuexen } 1135888973f5SMichael Tuexen break; 1136888973f5SMichael Tuexen } 1137888973f5SMichael Tuexen #endif /* INET6 */ 1138888973f5SMichael Tuexen default: 1139888973f5SMichael Tuexen error = EAFNOSUPPORT; 1140888973f5SMichael Tuexen goto out; 1141888973f5SMichael Tuexen } 1142888973f5SMichael Tuexen } 11432c37256eSGarrett Wollman if (!(flags & PRUS_OOB)) { 1144651e4e6aSGleb Smirnoff sbappendstream(&so->so_snd, m, flags); 11457d2608a5SMark Johnston m = NULL; 11462c37256eSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 1147bd4a39ccSMark Johnston KASSERT(tp->t_state == TCPS_CLOSED, 1148bd4a39ccSMark Johnston ("%s: tp %p is listening", __func__, tp)); 1149bd4a39ccSMark Johnston 11502c37256eSGarrett Wollman /* 11512c37256eSGarrett Wollman * Do implied connect if not yet connected, 11522c37256eSGarrett Wollman * initialize window to default value, and 11530c39d38dSGleb Smirnoff * initialize maxseg using peer's cached MSS. 11542c37256eSGarrett Wollman */ 1155fb59c426SYoshinobu Inoue #ifdef INET6 1156fb59c426SYoshinobu Inoue if (isipv6) 1157b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 1158fb59c426SYoshinobu Inoue #endif /* INET6 */ 1159b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1160b287c6c7SBjoern A. Zeeb else 1161b287c6c7SBjoern A. Zeeb #endif 1162b287c6c7SBjoern A. Zeeb #ifdef INET 1163888973f5SMichael Tuexen error = tcp_connect(tp, 1164888973f5SMichael Tuexen (struct sockaddr *)sinp, td); 1165b287c6c7SBjoern A. Zeeb #endif 11664a91aa8fSMichael Tuexen /* 11674a91aa8fSMichael Tuexen * The bind operation in tcp_connect succeeded. We 11684a91aa8fSMichael Tuexen * no longer want to restore the flags if later 11694a91aa8fSMichael Tuexen * operations fail. 11704a91aa8fSMichael Tuexen */ 11714a91aa8fSMichael Tuexen if (error == 0 || inp->inp_lport != 0) 11724a91aa8fSMichael Tuexen restoreflags = false; 11734a91aa8fSMichael Tuexen 11747d2608a5SMark Johnston if (error) { 11757d2608a5SMark Johnston /* m is freed if PRUS_NOTREADY is unset. */ 11767d2608a5SMark Johnston sbflush(&so->so_snd); 11772c37256eSGarrett Wollman goto out; 11787d2608a5SMark Johnston } 1179c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) 1180c560df6fSPatrick Kelsey tcp_fastopen_connect(tp); 118118a75309SPatrick Kelsey else { 11822c37256eSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 11832c37256eSGarrett Wollman tcp_mss(tp, -1); 11842c37256eSGarrett Wollman } 1185c560df6fSPatrick Kelsey } 11862c37256eSGarrett Wollman if (flags & PRUS_EOF) { 11872c37256eSGarrett Wollman /* 11882c37256eSGarrett Wollman * Close the send side of the connection after 11892c37256eSGarrett Wollman * the data is sent. 11902c37256eSGarrett Wollman */ 11912c37256eSGarrett Wollman socantsendmore(so); 1192623dce13SRobert Watson tcp_usrclosed(tp); 11932c37256eSGarrett Wollman } 1194e854dd38SRandall Stewart if (TCPS_HAVEESTABLISHED(tp->t_state) && 1195e854dd38SRandall Stewart ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 1196e854dd38SRandall Stewart (tp->t_fbyte_out == 0) && 1197e854dd38SRandall Stewart (so->so_snd.sb_ccc > 0)) { 1198e854dd38SRandall Stewart tp->t_fbyte_out = ticks; 1199e854dd38SRandall Stewart if (tp->t_fbyte_out == 0) 1200e854dd38SRandall Stewart tp->t_fbyte_out = 1; 1201e854dd38SRandall Stewart if (tp->t_fbyte_out && tp->t_fbyte_in) 1202e854dd38SRandall Stewart tp->t_flags2 |= TF2_FBYTES_COMPLETE; 1203e854dd38SRandall Stewart } 12042cbcd3c1SGleb Smirnoff if (!(inp->inp_flags & INP_DROPPED) && 12052cbcd3c1SGleb Smirnoff !(flags & PRUS_NOTREADY)) { 1206b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 1207b0acefa8SBill Fenner tp->t_flags |= TF_MORETOCOME; 120855bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 1209b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 1210b0acefa8SBill Fenner tp->t_flags &= ~TF_MORETOCOME; 1211b0acefa8SBill Fenner } 12122c37256eSGarrett Wollman } else { 1213623dce13SRobert Watson /* 1214623dce13SRobert Watson * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 1215623dce13SRobert Watson */ 1216d2bc35abSRobert Watson SOCKBUF_LOCK(&so->so_snd); 12172c37256eSGarrett Wollman if (sbspace(&so->so_snd) < -512) { 1218d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 12192c37256eSGarrett Wollman error = ENOBUFS; 12202c37256eSGarrett Wollman goto out; 12212c37256eSGarrett Wollman } 12222c37256eSGarrett Wollman /* 12232c37256eSGarrett Wollman * According to RFC961 (Assigned Protocols), 12242c37256eSGarrett Wollman * the urgent pointer points to the last octet 12252c37256eSGarrett Wollman * of urgent data. We continue, however, 12262c37256eSGarrett Wollman * to consider it to indicate the first octet 12272c37256eSGarrett Wollman * of data past the urgent section. 12282c37256eSGarrett Wollman * Otherwise, snd_up should be one lower. 12292c37256eSGarrett Wollman */ 1230651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_snd, m, flags); 1231d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 12327d2608a5SMark Johnston m = NULL; 1233ef53690bSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 1234ef53690bSGarrett Wollman /* 1235ef53690bSGarrett Wollman * Do implied connect if not yet connected, 1236ef53690bSGarrett Wollman * initialize window to default value, and 12370c39d38dSGleb Smirnoff * initialize maxseg using peer's cached MSS. 1238ef53690bSGarrett Wollman */ 123918a75309SPatrick Kelsey 1240c560df6fSPatrick Kelsey /* 1241c560df6fSPatrick Kelsey * Not going to contemplate SYN|URG 1242c560df6fSPatrick Kelsey */ 1243c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) 1244c560df6fSPatrick Kelsey tp->t_flags &= ~TF_FASTOPEN; 1245fb59c426SYoshinobu Inoue #ifdef INET6 1246fb59c426SYoshinobu Inoue if (isipv6) 1247b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 1248fb59c426SYoshinobu Inoue #endif /* INET6 */ 1249b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1250b287c6c7SBjoern A. Zeeb else 1251b287c6c7SBjoern A. Zeeb #endif 1252b287c6c7SBjoern A. Zeeb #ifdef INET 1253888973f5SMichael Tuexen error = tcp_connect(tp, 1254888973f5SMichael Tuexen (struct sockaddr *)sinp, td); 1255b287c6c7SBjoern A. Zeeb #endif 12564a91aa8fSMichael Tuexen /* 12574a91aa8fSMichael Tuexen * The bind operation in tcp_connect succeeded. We 12584a91aa8fSMichael Tuexen * no longer want to restore the flags if later 12594a91aa8fSMichael Tuexen * operations fail. 12604a91aa8fSMichael Tuexen */ 12614a91aa8fSMichael Tuexen if (error == 0 || inp->inp_lport != 0) 12624a91aa8fSMichael Tuexen restoreflags = false; 12634a91aa8fSMichael Tuexen 12647d2608a5SMark Johnston if (error != 0) { 12657d2608a5SMark Johnston /* m is freed if PRUS_NOTREADY is unset. */ 12667d2608a5SMark Johnston sbflush(&so->so_snd); 1267ef53690bSGarrett Wollman goto out; 12687d2608a5SMark Johnston } 1269ef53690bSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 1270ef53690bSGarrett Wollman tcp_mss(tp, -1); 1271623dce13SRobert Watson } 1272300fa232SGleb Smirnoff tp->snd_up = tp->snd_una + sbavail(&so->so_snd); 12737d2608a5SMark Johnston if ((flags & PRUS_NOTREADY) == 0) { 12742cdbfa66SPaul Saab tp->t_flags |= TF_FORCEDATA; 127555bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 12762cdbfa66SPaul Saab tp->t_flags &= ~TF_FORCEDATA; 12772c37256eSGarrett Wollman } 12782cbcd3c1SGleb Smirnoff } 12792529f56eSJonathan T. Looney TCP_LOG_EVENT(tp, NULL, 12802529f56eSJonathan T. Looney &inp->inp_socket->so_rcv, 12812529f56eSJonathan T. Looney &inp->inp_socket->so_snd, 12822529f56eSJonathan T. Looney TCP_LOG_USERSEND, error, 12832529f56eSJonathan T. Looney 0, NULL, false); 12847d2608a5SMark Johnston 1285d1401c90SRobert Watson out: 12864a91aa8fSMichael Tuexen /* 12877d2608a5SMark Johnston * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is 12887d2608a5SMark Johnston * responsible for freeing memory. 12897d2608a5SMark Johnston */ 12907d2608a5SMark Johnston if (m != NULL && (flags & PRUS_NOTREADY) == 0) 12917d2608a5SMark Johnston m_freem(m); 12927d2608a5SMark Johnston 12937d2608a5SMark Johnston /* 12944a91aa8fSMichael Tuexen * If the request was unsuccessful and we changed flags, 12954a91aa8fSMichael Tuexen * restore the original flags. 12964a91aa8fSMichael Tuexen */ 12974a91aa8fSMichael Tuexen if (error != 0 && restoreflags) { 12984a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 12994a91aa8fSMichael Tuexen inp->inp_inc.inc_flags = incflagsav; 13004a91aa8fSMichael Tuexen } 1301d1401c90SRobert Watson TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB : 13022c37256eSGarrett Wollman ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 13035d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB : 13045d06879aSGeorge V. Neville-Neil ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 13058501a69cSRobert Watson INP_WUNLOCK(inp); 130697a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 130773fddedaSPeter Grehan return (error); 13082c37256eSGarrett Wollman } 13092c37256eSGarrett Wollman 13102cbcd3c1SGleb Smirnoff static int 13112cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count) 13122cbcd3c1SGleb Smirnoff { 1313109eb549SGleb Smirnoff struct epoch_tracker et; 13142cbcd3c1SGleb Smirnoff struct inpcb *inp; 13152cbcd3c1SGleb Smirnoff struct tcpcb *tp; 13162cbcd3c1SGleb Smirnoff int error; 13172cbcd3c1SGleb Smirnoff 13182cbcd3c1SGleb Smirnoff inp = sotoinpcb(so); 13192cbcd3c1SGleb Smirnoff INP_WLOCK(inp); 13202cbcd3c1SGleb Smirnoff if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 13212cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 132282334850SJohn Baldwin mb_free_notready(m, count); 13232cbcd3c1SGleb Smirnoff return (ECONNRESET); 13242cbcd3c1SGleb Smirnoff } 13252cbcd3c1SGleb Smirnoff tp = intotcpcb(inp); 13262cbcd3c1SGleb Smirnoff 13272cbcd3c1SGleb Smirnoff SOCKBUF_LOCK(&so->so_snd); 13282cbcd3c1SGleb Smirnoff error = sbready(&so->so_snd, m, count); 13292cbcd3c1SGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 1330109eb549SGleb Smirnoff if (error == 0) { 1331109eb549SGleb Smirnoff NET_EPOCH_ENTER(et); 133255bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 1333109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 1334109eb549SGleb Smirnoff } 13352cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 13362cbcd3c1SGleb Smirnoff 13372cbcd3c1SGleb Smirnoff return (error); 13382cbcd3c1SGleb Smirnoff } 13392cbcd3c1SGleb Smirnoff 13402c37256eSGarrett Wollman /* 1341a152f8a3SRobert Watson * Abort the TCP. Drop the connection abruptly. 13422c37256eSGarrett Wollman */ 1343ac45e92fSRobert Watson static void 13442c37256eSGarrett Wollman tcp_usr_abort(struct socket *so) 13452c37256eSGarrett Wollman { 1346f76fcf6dSJeffrey Hsu struct inpcb *inp; 1347a152f8a3SRobert Watson struct tcpcb *tp = NULL; 13486573d758SMatt Macy struct epoch_tracker et; 1349623dce13SRobert Watson TCPDEBUG0; 1350c78cbc7bSRobert Watson 1351ac45e92fSRobert Watson inp = sotoinpcb(so); 1352c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 1353c78cbc7bSRobert Watson 135497a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 13558501a69cSRobert Watson INP_WLOCK(inp); 1356c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 1357c78cbc7bSRobert Watson ("tcp_usr_abort: inp_socket == NULL")); 1358c78cbc7bSRobert Watson 1359c78cbc7bSRobert Watson /* 1360a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, drop. 1361c78cbc7bSRobert Watson */ 1362ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_TIMEWAIT) && 1363ad71fe3cSRobert Watson !(inp->inp_flags & INP_DROPPED)) { 1364c78cbc7bSRobert Watson tp = intotcpcb(inp); 1365a152f8a3SRobert Watson TCPDEBUG1(); 13668fa799bdSJonathan T. Looney tp = tcp_drop(tp, ECONNABORTED); 13678fa799bdSJonathan T. Looney if (tp == NULL) 13688fa799bdSJonathan T. Looney goto dropped; 1369a152f8a3SRobert Watson TCPDEBUG2(PRU_ABORT); 13705d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ABORT); 1371c78cbc7bSRobert Watson } 1372ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1373a152f8a3SRobert Watson SOCK_LOCK(so); 1374a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 1375a152f8a3SRobert Watson SOCK_UNLOCK(so); 1376ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1377a152f8a3SRobert Watson } 13788501a69cSRobert Watson INP_WUNLOCK(inp); 13798fa799bdSJonathan T. Looney dropped: 138097a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 1381a152f8a3SRobert Watson } 1382a152f8a3SRobert Watson 1383a152f8a3SRobert Watson /* 1384a152f8a3SRobert Watson * TCP socket is closed. Start friendly disconnect. 1385a152f8a3SRobert Watson */ 1386a152f8a3SRobert Watson static void 1387a152f8a3SRobert Watson tcp_usr_close(struct socket *so) 1388a152f8a3SRobert Watson { 1389a152f8a3SRobert Watson struct inpcb *inp; 1390a152f8a3SRobert Watson struct tcpcb *tp = NULL; 13916573d758SMatt Macy struct epoch_tracker et; 1392a152f8a3SRobert Watson TCPDEBUG0; 1393a152f8a3SRobert Watson 1394a152f8a3SRobert Watson inp = sotoinpcb(so); 1395a152f8a3SRobert Watson KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL")); 1396a152f8a3SRobert Watson 139797a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 13988501a69cSRobert Watson INP_WLOCK(inp); 1399a152f8a3SRobert Watson KASSERT(inp->inp_socket != NULL, 1400a152f8a3SRobert Watson ("tcp_usr_close: inp_socket == NULL")); 1401a152f8a3SRobert Watson 1402a152f8a3SRobert Watson /* 1403a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, initiate 1404a152f8a3SRobert Watson * a disconnect. 1405a152f8a3SRobert Watson */ 1406ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_TIMEWAIT) && 1407ad71fe3cSRobert Watson !(inp->inp_flags & INP_DROPPED)) { 1408a152f8a3SRobert Watson tp = intotcpcb(inp); 1409a152f8a3SRobert Watson TCPDEBUG1(); 1410a152f8a3SRobert Watson tcp_disconnect(tp); 1411a152f8a3SRobert Watson TCPDEBUG2(PRU_CLOSE); 14125d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CLOSE); 1413a152f8a3SRobert Watson } 1414ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1415a152f8a3SRobert Watson SOCK_LOCK(so); 1416a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 1417a152f8a3SRobert Watson SOCK_UNLOCK(so); 1418ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1419a152f8a3SRobert Watson } 14208501a69cSRobert Watson INP_WUNLOCK(inp); 142197a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 14222c37256eSGarrett Wollman } 14232c37256eSGarrett Wollman 1424d3b6c96bSRandall Stewart static int 1425d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags) 1426d3b6c96bSRandall Stewart { 1427d3b6c96bSRandall Stewart /* 1428d3b6c96bSRandall Stewart * If the specific TCP stack has a pru_options 1429d3b6c96bSRandall Stewart * specified then it does not always support 1430d3b6c96bSRandall Stewart * all the PRU_XX options and we must ask it. 1431d3b6c96bSRandall Stewart * If the function is not specified then all 1432d3b6c96bSRandall Stewart * of the PRU_XX options are supported. 1433d3b6c96bSRandall Stewart */ 1434d3b6c96bSRandall Stewart int ret = 0; 1435d3b6c96bSRandall Stewart 1436d3b6c96bSRandall Stewart if (tp->t_fb->tfb_pru_options) { 1437d3b6c96bSRandall Stewart ret = (*tp->t_fb->tfb_pru_options)(tp, flags); 1438d3b6c96bSRandall Stewart } 1439d3b6c96bSRandall Stewart return (ret); 1440d3b6c96bSRandall Stewart } 1441d3b6c96bSRandall Stewart 14422c37256eSGarrett Wollman /* 14432c37256eSGarrett Wollman * Receive out-of-band data. 14442c37256eSGarrett Wollman */ 14452c37256eSGarrett Wollman static int 14462c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 14472c37256eSGarrett Wollman { 14482c37256eSGarrett Wollman int error = 0; 1449f76fcf6dSJeffrey Hsu struct inpcb *inp; 1450623dce13SRobert Watson struct tcpcb *tp = NULL; 14512c37256eSGarrett Wollman 1452623dce13SRobert Watson TCPDEBUG0; 1453623dce13SRobert Watson inp = sotoinpcb(so); 1454623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 14558501a69cSRobert Watson INP_WLOCK(inp); 1456ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 145721367f63SSam Leffler error = ECONNRESET; 1458623dce13SRobert Watson goto out; 1459623dce13SRobert Watson } 1460623dce13SRobert Watson tp = intotcpcb(inp); 1461d3b6c96bSRandall Stewart error = tcp_pru_options_support(tp, PRUS_OOB); 1462d3b6c96bSRandall Stewart if (error) { 1463d3b6c96bSRandall Stewart goto out; 1464d3b6c96bSRandall Stewart } 1465623dce13SRobert Watson TCPDEBUG1(); 14662c37256eSGarrett Wollman if ((so->so_oobmark == 0 && 1467c0b99ffaSRobert Watson (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 14684cc20ab1SSeigo Tanimura so->so_options & SO_OOBINLINE || 14694cc20ab1SSeigo Tanimura tp->t_oobflags & TCPOOB_HADDATA) { 14702c37256eSGarrett Wollman error = EINVAL; 14712c37256eSGarrett Wollman goto out; 14722c37256eSGarrett Wollman } 14732c37256eSGarrett Wollman if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 14742c37256eSGarrett Wollman error = EWOULDBLOCK; 14752c37256eSGarrett Wollman goto out; 14762c37256eSGarrett Wollman } 14772c37256eSGarrett Wollman m->m_len = 1; 14782c37256eSGarrett Wollman *mtod(m, caddr_t) = tp->t_iobc; 14792c37256eSGarrett Wollman if ((flags & MSG_PEEK) == 0) 14802c37256eSGarrett Wollman tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1481623dce13SRobert Watson 1482623dce13SRobert Watson out: 1483623dce13SRobert Watson TCPDEBUG2(PRU_RCVOOB); 14845d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVOOB); 14858501a69cSRobert Watson INP_WUNLOCK(inp); 1486623dce13SRobert Watson return (error); 14872c37256eSGarrett Wollman } 14882c37256eSGarrett Wollman 1489b287c6c7SBjoern A. Zeeb #ifdef INET 14902c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = { 1491756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1492756d52a1SPoul-Henning Kamp .pru_accept = tcp_usr_accept, 1493756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1494756d52a1SPoul-Henning Kamp .pru_bind = tcp_usr_bind, 1495756d52a1SPoul-Henning Kamp .pru_connect = tcp_usr_connect, 1496756d52a1SPoul-Henning Kamp .pru_control = in_control, 1497756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1498756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1499756d52a1SPoul-Henning Kamp .pru_listen = tcp_usr_listen, 150054d642bbSRobert Watson .pru_peeraddr = in_getpeeraddr, 1501756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1502756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1503756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 15042cbcd3c1SGleb Smirnoff .pru_ready = tcp_usr_ready, 1505756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 150654d642bbSRobert Watson .pru_sockaddr = in_getsockaddr, 1507a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1508a152f8a3SRobert Watson .pru_close = tcp_usr_close, 15092c37256eSGarrett Wollman }; 1510b287c6c7SBjoern A. Zeeb #endif /* INET */ 1511df8bae1dSRodney W. Grimes 1512fb59c426SYoshinobu Inoue #ifdef INET6 1513fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = { 1514756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1515756d52a1SPoul-Henning Kamp .pru_accept = tcp6_usr_accept, 1516756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1517756d52a1SPoul-Henning Kamp .pru_bind = tcp6_usr_bind, 1518756d52a1SPoul-Henning Kamp .pru_connect = tcp6_usr_connect, 1519756d52a1SPoul-Henning Kamp .pru_control = in6_control, 1520756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1521756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1522756d52a1SPoul-Henning Kamp .pru_listen = tcp6_usr_listen, 1523756d52a1SPoul-Henning Kamp .pru_peeraddr = in6_mapped_peeraddr, 1524756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1525756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1526756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 15272cbcd3c1SGleb Smirnoff .pru_ready = tcp_usr_ready, 1528756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 1529756d52a1SPoul-Henning Kamp .pru_sockaddr = in6_mapped_sockaddr, 1530a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1531a152f8a3SRobert Watson .pru_close = tcp_usr_close, 1532fb59c426SYoshinobu Inoue }; 1533fb59c426SYoshinobu Inoue #endif /* INET6 */ 1534fb59c426SYoshinobu Inoue 1535b287c6c7SBjoern A. Zeeb #ifdef INET 1536a0292f23SGarrett Wollman /* 1537a0292f23SGarrett Wollman * Common subroutine to open a TCP connection to remote host specified 1538a0292f23SGarrett Wollman * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 15395200e00eSIan Dowse * port number if needed. Call in_pcbconnect_setup to do the routing and 15405200e00eSIan Dowse * to choose a local host address (interface). If there is an existing 15415200e00eSIan Dowse * incarnation of the same connection in TIME-WAIT state and if the remote 15425200e00eSIan Dowse * host was sending CC options and if the connection duration was < MSL, then 1543a0292f23SGarrett Wollman * truncate the previous TIME-WAIT state and proceed. 1544a0292f23SGarrett Wollman * Initialize connection parameters and enter SYN-SENT state. 1545a0292f23SGarrett Wollman */ 15460312fbe9SPoul-Henning Kamp static int 1547ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1548a0292f23SGarrett Wollman { 1549a0292f23SGarrett Wollman struct inpcb *inp = tp->t_inpcb, *oinp; 1550a0292f23SGarrett Wollman struct socket *so = inp->inp_socket; 15515200e00eSIan Dowse struct in_addr laddr; 15525200e00eSIan Dowse u_short lport; 1553c3229e05SDavid Greenman int error; 1554a0292f23SGarrett Wollman 1555c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 15568501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1557fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 1558623dce13SRobert Watson 155925102351SMike Karels if (V_tcp_require_unique_port && inp->inp_lport == 0) { 15604616026fSErmal Luçi error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 15614616026fSErmal Luçi if (error) 1562fa046d87SRobert Watson goto out; 1563a0292f23SGarrett Wollman } 1564a0292f23SGarrett Wollman 1565a0292f23SGarrett Wollman /* 1566a0292f23SGarrett Wollman * Cannot simply call in_pcbconnect, because there might be an 1567a0292f23SGarrett Wollman * earlier incarnation of this same connection still in 1568a0292f23SGarrett Wollman * TIME_WAIT state, creating an ADDRINUSE error. 1569a0292f23SGarrett Wollman */ 15705200e00eSIan Dowse laddr = inp->inp_laddr; 15715200e00eSIan Dowse lport = inp->inp_lport; 15725200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 1573b0330ed9SPawel Jakub Dawidek &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 15745200e00eSIan Dowse if (error && oinp == NULL) 1575fa046d87SRobert Watson goto out; 1576fa046d87SRobert Watson if (oinp) { 1577fa046d87SRobert Watson error = EADDRINUSE; 1578fa046d87SRobert Watson goto out; 1579fa046d87SRobert Watson } 158025102351SMike Karels /* Handle initial bind if it hadn't been done in advance. */ 158125102351SMike Karels if (inp->inp_lport == 0) { 158225102351SMike Karels inp->inp_lport = lport; 158325102351SMike Karels if (in_pcbinshash(inp) != 0) { 158425102351SMike Karels inp->inp_lport = 0; 158525102351SMike Karels error = EAGAIN; 158625102351SMike Karels goto out; 158725102351SMike Karels } 158825102351SMike Karels } 15895200e00eSIan Dowse inp->inp_laddr = laddr; 159015bd2b43SDavid Greenman in_pcbrehash(inp); 1591fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1592a0292f23SGarrett Wollman 1593087b55eaSAndre Oppermann /* 1594087b55eaSAndre Oppermann * Compute window scaling to request: 1595087b55eaSAndre Oppermann * Scale to fit into sweet spot. See tcp_syncache.c. 1596087b55eaSAndre Oppermann * XXX: This should move to tcp_output(). 1597087b55eaSAndre Oppermann */ 1598a0292f23SGarrett Wollman while (tp->request_r_scale < TCP_MAX_WINSHIFT && 15999b3bc6bfSMike Silbersack (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1600a0292f23SGarrett Wollman tp->request_r_scale++; 1601a0292f23SGarrett Wollman 1602a0292f23SGarrett Wollman soisconnecting(so); 160378b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 160457f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 16058e02b4e0SMichael Tuexen tp->iss = tcp_new_isn(&inp->inp_inc); 16068e02b4e0SMichael Tuexen if (tp->t_flags & TF_REQ_TSTMP) 16078e02b4e0SMichael Tuexen tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc); 1608a0292f23SGarrett Wollman tcp_sendseqinit(tp); 1609a45d2726SAndras Olah 1610a0292f23SGarrett Wollman return 0; 1611fa046d87SRobert Watson 1612fa046d87SRobert Watson out: 1613fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1614fa046d87SRobert Watson return (error); 1615a0292f23SGarrett Wollman } 1616b287c6c7SBjoern A. Zeeb #endif /* INET */ 1617a0292f23SGarrett Wollman 1618fb59c426SYoshinobu Inoue #ifdef INET6 1619fb59c426SYoshinobu Inoue static int 1620ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1621fb59c426SYoshinobu Inoue { 1622a7e201bbSAndrey V. Elsukov struct inpcb *inp = tp->t_inpcb; 1623fb59c426SYoshinobu Inoue int error; 1624fb59c426SYoshinobu Inoue 16258501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1626fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 1627623dce13SRobert Watson 162825102351SMike Karels if (V_tcp_require_unique_port && inp->inp_lport == 0) { 16294616026fSErmal Luçi error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 16304616026fSErmal Luçi if (error) 1631fa046d87SRobert Watson goto out; 1632fb59c426SYoshinobu Inoue } 1633a7e201bbSAndrey V. Elsukov error = in6_pcbconnect(inp, nam, td->td_ucred); 1634a7e201bbSAndrey V. Elsukov if (error != 0) 1635b598155aSRobert Watson goto out; 1636fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1637fb59c426SYoshinobu Inoue 1638fb59c426SYoshinobu Inoue /* Compute window scaling to request. */ 1639fb59c426SYoshinobu Inoue while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1640970caf60SBjoern A. Zeeb (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1641fb59c426SYoshinobu Inoue tp->request_r_scale++; 1642fb59c426SYoshinobu Inoue 1643a7e201bbSAndrey V. Elsukov soisconnecting(inp->inp_socket); 164478b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 164557f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 16468e02b4e0SMichael Tuexen tp->iss = tcp_new_isn(&inp->inp_inc); 16478e02b4e0SMichael Tuexen if (tp->t_flags & TF_REQ_TSTMP) 16488e02b4e0SMichael Tuexen tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc); 1649fb59c426SYoshinobu Inoue tcp_sendseqinit(tp); 1650fb59c426SYoshinobu Inoue 1651fb59c426SYoshinobu Inoue return 0; 1652fa046d87SRobert Watson 1653fa046d87SRobert Watson out: 1654fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1655fa046d87SRobert Watson return error; 1656fb59c426SYoshinobu Inoue } 1657fb59c426SYoshinobu Inoue #endif /* INET6 */ 1658fb59c426SYoshinobu Inoue 1659cfe8b629SGarrett Wollman /* 1660b8af5dfaSRobert Watson * Export TCP internal state information via a struct tcp_info, based on the 1661b8af5dfaSRobert Watson * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1662b8af5dfaSRobert Watson * (TCP state machine, etc). We export all information using FreeBSD-native 1663b8af5dfaSRobert Watson * constants -- for example, the numeric values for tcpi_state will differ 1664b8af5dfaSRobert Watson * from Linux. 1665b8af5dfaSRobert Watson */ 1666b8af5dfaSRobert Watson static void 1667ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) 1668b8af5dfaSRobert Watson { 1669b8af5dfaSRobert Watson 16708501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1671b8af5dfaSRobert Watson bzero(ti, sizeof(*ti)); 1672b8af5dfaSRobert Watson 1673b8af5dfaSRobert Watson ti->tcpi_state = tp->t_state; 1674b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1675b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 16763529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 1677b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_SACK; 1678b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1679b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_WSCALE; 1680b8af5dfaSRobert Watson ti->tcpi_snd_wscale = tp->snd_scale; 1681b8af5dfaSRobert Watson ti->tcpi_rcv_wscale = tp->rcv_scale; 1682b8af5dfaSRobert Watson } 16833cf38784SMichael Tuexen if (tp->t_flags2 & TF2_ECN_PERMIT) 16845a17b6adSMichael Tuexen ti->tcpi_options |= TCPI_OPT_ECN; 16851baaf834SBruce M Simpson 168643d94734SJohn Baldwin ti->tcpi_rto = tp->t_rxtcur * tick; 16873ac12506SJonathan T. Looney ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick; 16881baaf834SBruce M Simpson ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 16891baaf834SBruce M Simpson ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT; 16901baaf834SBruce M Simpson 1691b8af5dfaSRobert Watson ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1692b8af5dfaSRobert Watson ti->tcpi_snd_cwnd = tp->snd_cwnd; 1693b8af5dfaSRobert Watson 1694b8af5dfaSRobert Watson /* 1695b8af5dfaSRobert Watson * FreeBSD-specific extension fields for tcp_info. 1696b8af5dfaSRobert Watson */ 1697c8443a1dSRobert Watson ti->tcpi_rcv_space = tp->rcv_wnd; 1698535fbad6SKip Macy ti->tcpi_rcv_nxt = tp->rcv_nxt; 1699b8af5dfaSRobert Watson ti->tcpi_snd_wnd = tp->snd_wnd; 17001c18314dSAndre Oppermann ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */ 1701535fbad6SKip Macy ti->tcpi_snd_nxt = tp->snd_nxt; 170243d94734SJohn Baldwin ti->tcpi_snd_mss = tp->t_maxseg; 170343d94734SJohn Baldwin ti->tcpi_rcv_mss = tp->t_maxseg; 1704f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack; 1705f5d34df5SGeorge V. Neville-Neil ti->tcpi_rcv_ooopack = tp->t_rcvoopack; 1706f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_zerowin = tp->t_sndzerowin; 1707a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD 1708a6456410SNavdeep Parhar if (tp->t_flags & TF_TOE) { 1709a6456410SNavdeep Parhar ti->tcpi_options |= TCPI_OPT_TOE; 1710a6456410SNavdeep Parhar tcp_offload_tcp_info(tp, ti); 1711a6456410SNavdeep Parhar } 1712a6456410SNavdeep Parhar #endif 1713b8af5dfaSRobert Watson } 1714b8af5dfaSRobert Watson 1715b8af5dfaSRobert Watson /* 17161e8f5ffaSRobert Watson * tcp_ctloutput() must drop the inpcb lock before performing copyin on 17171e8f5ffaSRobert Watson * socket option arguments. When it re-acquires the lock after the copy, it 17181e8f5ffaSRobert Watson * has to revalidate that the connection is still valid for the socket 17191e8f5ffaSRobert Watson * option. 1720cfe8b629SGarrett Wollman */ 1721bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \ 17228501a69cSRobert Watson INP_WLOCK(inp); \ 1723ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \ 17248501a69cSRobert Watson INP_WUNLOCK(inp); \ 1725bac5bedfSConrad Meyer cleanup; \ 17261e8f5ffaSRobert Watson return (ECONNRESET); \ 17271e8f5ffaSRobert Watson } \ 17281e8f5ffaSRobert Watson tp = intotcpcb(inp); \ 17291e8f5ffaSRobert Watson } while(0) 1730bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */) 17311e8f5ffaSRobert Watson 1732fc4d53ccSGleb Smirnoff static int 1733fc4d53ccSGleb Smirnoff tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt) 1734df8bae1dSRodney W. Grimes { 1735fc4d53ccSGleb Smirnoff struct tcpcb *tp = intotcpcb(inp); 1736fc4d53ccSGleb Smirnoff int error = 0; 1737df8bae1dSRodney W. Grimes 1738fc4d53ccSGleb Smirnoff MPASS(sopt->sopt_dir == SOPT_SET); 1739fc4d53ccSGleb Smirnoff 1740cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_TCP) { 1741fb59c426SYoshinobu Inoue #ifdef INET6 1742*de156263SGleb Smirnoff if (inp->inp_vflag & INP_IPV6PROTO) 1743fc4d53ccSGleb Smirnoff error = ip6_ctloutput(inp->inp_socket, sopt); 1744*de156263SGleb Smirnoff #endif 1745*de156263SGleb Smirnoff #if defined(INET6) && defined(INET) 1746*de156263SGleb Smirnoff else 1747*de156263SGleb Smirnoff #endif 1748*de156263SGleb Smirnoff #ifdef INET 1749*de156263SGleb Smirnoff error = ip_ctloutput(inp->inp_socket, sopt); 1750*de156263SGleb Smirnoff #endif 17515dff1c38SMichael Tuexen /* 1752*de156263SGleb Smirnoff * When an IP-level socket option affects TCP, pass control 1753*de156263SGleb Smirnoff * down to stack tfb_tcp_ctloutput, otherwise return what 1754*de156263SGleb Smirnoff * IP level returned. 17555dff1c38SMichael Tuexen */ 1756*de156263SGleb Smirnoff switch (sopt->sopt_level) { 1757*de156263SGleb Smirnoff #ifdef INET6 1758*de156263SGleb Smirnoff case IPPROTO_IPV6: 1759*de156263SGleb Smirnoff if ((inp->inp_vflag & INP_IPV6PROTO) == 0) 1760*de156263SGleb Smirnoff return (error); 1761*de156263SGleb Smirnoff switch (sopt->sopt_name) { 1762*de156263SGleb Smirnoff case IPV6_TCLASS: 1763*de156263SGleb Smirnoff /* Notify tcp stacks that care (e.g. RACK). */ 1764*de156263SGleb Smirnoff break; 1765*de156263SGleb Smirnoff case IPV6_USE_MIN_MTU: 1766*de156263SGleb Smirnoff /* 1767*de156263SGleb Smirnoff * XXXGL: this handling should belong to 1768*de156263SGleb Smirnoff * stack specific tfb_tcp_ctloutput, we 1769*de156263SGleb Smirnoff * should just break here. 1770*de156263SGleb Smirnoff * 1771*de156263SGleb Smirnoff * In case of the IPV6_USE_MIN_MTU socket 1772*de156263SGleb Smirnoff * option, the INC_IPV6MINMTU flag to announce 1773*de156263SGleb Smirnoff * a corresponding MSS during the initial 1774*de156263SGleb Smirnoff * handshake. If the TCP connection is not in 1775*de156263SGleb Smirnoff * the front states, just reduce the MSS being 1776*de156263SGleb Smirnoff * used. This avoids the sending of TCP 1777*de156263SGleb Smirnoff * segments which will be fragmented at the 1778*de156263SGleb Smirnoff * IPv6 layer. 1779*de156263SGleb Smirnoff */ 17805dff1c38SMichael Tuexen INP_WLOCK(inp); 17815dff1c38SMichael Tuexen if ((inp->inp_flags & 17825dff1c38SMichael Tuexen (INP_TIMEWAIT | INP_DROPPED))) { 17835dff1c38SMichael Tuexen INP_WUNLOCK(inp); 17845dff1c38SMichael Tuexen return (ECONNRESET); 17855dff1c38SMichael Tuexen } 17865dff1c38SMichael Tuexen inp->inp_inc.inc_flags |= INC_IPV6MINMTU; 17875dff1c38SMichael Tuexen tp = intotcpcb(inp); 17885dff1c38SMichael Tuexen if ((tp->t_state >= TCPS_SYN_SENT) && 17895dff1c38SMichael Tuexen (inp->inp_inc.inc_flags & INC_ISIPV6)) { 17905dff1c38SMichael Tuexen struct ip6_pktopts *opt; 17915dff1c38SMichael Tuexen 17925dff1c38SMichael Tuexen opt = inp->in6p_outputopts; 17935dff1c38SMichael Tuexen if ((opt != NULL) && 17945dff1c38SMichael Tuexen (opt->ip6po_minmtu == 17955dff1c38SMichael Tuexen IP6PO_MINMTU_ALL)) { 17965dff1c38SMichael Tuexen if (tp->t_maxseg > TCP6_MSS) { 17975dff1c38SMichael Tuexen tp->t_maxseg = TCP6_MSS; 17985dff1c38SMichael Tuexen } 17995dff1c38SMichael Tuexen } 18005dff1c38SMichael Tuexen } 18015dff1c38SMichael Tuexen INP_WUNLOCK(inp); 1802*de156263SGleb Smirnoff /* FALLTHROUGH */ 1803*de156263SGleb Smirnoff default: 1804*de156263SGleb Smirnoff return (error); 18055dff1c38SMichael Tuexen } 1806*de156263SGleb Smirnoff break; 1807b287c6c7SBjoern A. Zeeb #endif 1808b287c6c7SBjoern A. Zeeb #ifdef INET 1809*de156263SGleb Smirnoff case IPPROTO_IP: 1810*de156263SGleb Smirnoff switch (sopt->sopt_name) { 1811*de156263SGleb Smirnoff case IP_TOS: 1812*de156263SGleb Smirnoff case IP_TTL: 1813*de156263SGleb Smirnoff /* Notify tcp stacks that care (e.g. RACK). */ 1814*de156263SGleb Smirnoff break; 1815*de156263SGleb Smirnoff default: 1816df8bae1dSRodney W. Grimes return (error); 1817*de156263SGleb Smirnoff } 1818*de156263SGleb Smirnoff break; 1819*de156263SGleb Smirnoff #endif 1820*de156263SGleb Smirnoff default: 1821*de156263SGleb Smirnoff return (error); 1822*de156263SGleb Smirnoff } 1823fc4d53ccSGleb Smirnoff } else if (sopt->sopt_name == TCP_FUNCTION_BLK) { 1824fc4d53ccSGleb Smirnoff /* 1825fc4d53ccSGleb Smirnoff * Protect the TCP option TCP_FUNCTION_BLK so 1826fc4d53ccSGleb Smirnoff * that a sub-function can *never* overwrite this. 1827fc4d53ccSGleb Smirnoff */ 1828fc4d53ccSGleb Smirnoff struct tcp_function_set fsn; 1829fc4d53ccSGleb Smirnoff struct tcp_function_block *blk; 1830fc4d53ccSGleb Smirnoff 1831fc4d53ccSGleb Smirnoff error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn); 1832fc4d53ccSGleb Smirnoff if (error) 1833fc4d53ccSGleb Smirnoff return (error); 1834fc4d53ccSGleb Smirnoff 183568cea2b1SJohn Baldwin INP_WLOCK(inp); 1836ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 18378501a69cSRobert Watson INP_WUNLOCK(inp); 18381e8f5ffaSRobert Watson return (ECONNRESET); 1839623dce13SRobert Watson } 184055bceb1eSRandall Stewart tp = intotcpcb(inp); 1841fc4d53ccSGleb Smirnoff 184255bceb1eSRandall Stewart blk = find_and_ref_tcp_functions(&fsn); 184355bceb1eSRandall Stewart if (blk == NULL) { 184455bceb1eSRandall Stewart INP_WUNLOCK(inp); 184555bceb1eSRandall Stewart return (ENOENT); 184655bceb1eSRandall Stewart } 1847587d67c0SRandall Stewart if (tp->t_fb == blk) { 1848587d67c0SRandall Stewart /* You already have this */ 1849587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1850587d67c0SRandall Stewart INP_WUNLOCK(inp); 1851587d67c0SRandall Stewart return (0); 1852587d67c0SRandall Stewart } 1853587d67c0SRandall Stewart if (tp->t_state != TCPS_CLOSED) { 1854587d67c0SRandall Stewart /* 1855587d67c0SRandall Stewart * The user has advanced the state 1856587d67c0SRandall Stewart * past the initial point, we may not 1857587d67c0SRandall Stewart * be able to switch. 1858587d67c0SRandall Stewart */ 1859587d67c0SRandall Stewart if (blk->tfb_tcp_handoff_ok != NULL) { 1860587d67c0SRandall Stewart /* 1861587d67c0SRandall Stewart * Does the stack provide a 1862587d67c0SRandall Stewart * query mechanism, if so it may 1863587d67c0SRandall Stewart * still be possible? 1864587d67c0SRandall Stewart */ 1865587d67c0SRandall Stewart error = (*blk->tfb_tcp_handoff_ok)(tp); 1866c6c0be27SMichael Tuexen } else 1867c6c0be27SMichael Tuexen error = EINVAL; 1868587d67c0SRandall Stewart if (error) { 1869587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1870587d67c0SRandall Stewart INP_WUNLOCK(inp); 1871587d67c0SRandall Stewart return(error); 1872587d67c0SRandall Stewart } 1873587d67c0SRandall Stewart } 187455bceb1eSRandall Stewart if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) { 187555bceb1eSRandall Stewart refcount_release(&blk->tfb_refcnt); 187655bceb1eSRandall Stewart INP_WUNLOCK(inp); 187755bceb1eSRandall Stewart return (ENOENT); 187855bceb1eSRandall Stewart } 187955bceb1eSRandall Stewart /* 188055bceb1eSRandall Stewart * Release the old refcnt, the 1881587d67c0SRandall Stewart * lookup acquired a ref on the 1882587d67c0SRandall Stewart * new one already. 188355bceb1eSRandall Stewart */ 1884587d67c0SRandall Stewart if (tp->t_fb->tfb_tcp_fb_fini) { 1885086a3556SAndrew Gallatin struct epoch_tracker et; 1886587d67c0SRandall Stewart /* 1887587d67c0SRandall Stewart * Tell the stack to cleanup with 0 i.e. 1888587d67c0SRandall Stewart * the tcb is not going away. 1889587d67c0SRandall Stewart */ 1890086a3556SAndrew Gallatin NET_EPOCH_ENTER(et); 1891587d67c0SRandall Stewart (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0); 1892086a3556SAndrew Gallatin NET_EPOCH_EXIT(et); 1893587d67c0SRandall Stewart } 18943ee9c3c4SRandall Stewart #ifdef TCPHPTS 18953ee9c3c4SRandall Stewart /* Assure that we are not on any hpts */ 18963ee9c3c4SRandall Stewart tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL); 18973ee9c3c4SRandall Stewart #endif 18983ee9c3c4SRandall Stewart if (blk->tfb_tcp_fb_init) { 18993ee9c3c4SRandall Stewart error = (*blk->tfb_tcp_fb_init)(tp); 19003ee9c3c4SRandall Stewart if (error) { 19013ee9c3c4SRandall Stewart refcount_release(&blk->tfb_refcnt); 19023ee9c3c4SRandall Stewart if (tp->t_fb->tfb_tcp_fb_init) { 19033ee9c3c4SRandall Stewart if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) { 19043ee9c3c4SRandall Stewart /* Fall back failed, drop the connection */ 19053ee9c3c4SRandall Stewart INP_WUNLOCK(inp); 1906fc4d53ccSGleb Smirnoff soabort(inp->inp_socket); 19073ee9c3c4SRandall Stewart return(error); 19083ee9c3c4SRandall Stewart } 19093ee9c3c4SRandall Stewart } 19103ee9c3c4SRandall Stewart goto err_out; 19113ee9c3c4SRandall Stewart } 19123ee9c3c4SRandall Stewart } 191355bceb1eSRandall Stewart refcount_release(&tp->t_fb->tfb_refcnt); 191455bceb1eSRandall Stewart tp->t_fb = blk; 191555bceb1eSRandall Stewart #ifdef TCP_OFFLOAD 191655bceb1eSRandall Stewart if (tp->t_flags & TF_TOE) { 191755bceb1eSRandall Stewart tcp_offload_ctloutput(tp, sopt->sopt_dir, 191855bceb1eSRandall Stewart sopt->sopt_name); 191955bceb1eSRandall Stewart } 192055bceb1eSRandall Stewart #endif 19213ee9c3c4SRandall Stewart err_out: 192255bceb1eSRandall Stewart INP_WUNLOCK(inp); 192355bceb1eSRandall Stewart return (error); 1924fc4d53ccSGleb Smirnoff } 1925fc4d53ccSGleb Smirnoff 1926fc4d53ccSGleb Smirnoff INP_WLOCK(inp); 1927fc4d53ccSGleb Smirnoff if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 1928fc4d53ccSGleb Smirnoff INP_WUNLOCK(inp); 1929fc4d53ccSGleb Smirnoff return (ECONNRESET); 1930fc4d53ccSGleb Smirnoff } 1931fc4d53ccSGleb Smirnoff tp = intotcpcb(inp); 1932fc4d53ccSGleb Smirnoff 1933fc4d53ccSGleb Smirnoff /* Pass in the INP locked, caller must unlock it. */ 1934fc4d53ccSGleb Smirnoff return (tp->t_fb->tfb_tcp_ctloutput(inp->inp_socket, sopt, inp, tp)); 1935fc4d53ccSGleb Smirnoff } 1936fc4d53ccSGleb Smirnoff 1937fc4d53ccSGleb Smirnoff static int 1938fc4d53ccSGleb Smirnoff tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt) 1939fc4d53ccSGleb Smirnoff { 1940fc4d53ccSGleb Smirnoff int error = 0; 1941fc4d53ccSGleb Smirnoff struct tcpcb *tp; 1942fc4d53ccSGleb Smirnoff 1943fc4d53ccSGleb Smirnoff MPASS(sopt->sopt_dir == SOPT_GET); 1944fc4d53ccSGleb Smirnoff 1945fc4d53ccSGleb Smirnoff if (sopt->sopt_level != IPPROTO_TCP) { 1946fc4d53ccSGleb Smirnoff #ifdef INET6 1947fc4d53ccSGleb Smirnoff if (inp->inp_vflag & INP_IPV6PROTO) 1948fc4d53ccSGleb Smirnoff error = ip6_ctloutput(inp->inp_socket, sopt); 1949fc4d53ccSGleb Smirnoff #endif /* INET6 */ 1950fc4d53ccSGleb Smirnoff #if defined(INET6) && defined(INET) 1951fc4d53ccSGleb Smirnoff else 1952fc4d53ccSGleb Smirnoff #endif 1953fc4d53ccSGleb Smirnoff #ifdef INET 1954fc4d53ccSGleb Smirnoff error = ip_ctloutput(inp->inp_socket, sopt); 1955fc4d53ccSGleb Smirnoff #endif 1956fc4d53ccSGleb Smirnoff return (error); 1957fc4d53ccSGleb Smirnoff } 1958fc4d53ccSGleb Smirnoff INP_WLOCK(inp); 1959fc4d53ccSGleb Smirnoff if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 1960fc4d53ccSGleb Smirnoff INP_WUNLOCK(inp); 1961fc4d53ccSGleb Smirnoff return (ECONNRESET); 1962fc4d53ccSGleb Smirnoff } 1963fc4d53ccSGleb Smirnoff tp = intotcpcb(inp); 1964fc4d53ccSGleb Smirnoff if (((sopt->sopt_name == TCP_FUNCTION_BLK) || 1965e2833083SPeter Lei (sopt->sopt_name == TCP_FUNCTION_ALIAS))) { 1966fc4d53ccSGleb Smirnoff struct tcp_function_set fsn; 1967fc4d53ccSGleb Smirnoff 1968e2833083SPeter Lei if (sopt->sopt_name == TCP_FUNCTION_ALIAS) { 1969e2833083SPeter Lei memset(&fsn, 0, sizeof(fsn)); 1970e2833083SPeter Lei find_tcp_function_alias(tp->t_fb, &fsn); 1971e2833083SPeter Lei } else { 1972e2833083SPeter Lei strncpy(fsn.function_set_name, 1973e2833083SPeter Lei tp->t_fb->tfb_tcp_block_name, 1974c73b6f4dSEd Maste TCP_FUNCTION_NAME_LEN_MAX); 1975c73b6f4dSEd Maste fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0'; 1976e2833083SPeter Lei } 197755bceb1eSRandall Stewart fsn.pcbcnt = tp->t_fb->tfb_refcnt; 197855bceb1eSRandall Stewart INP_WUNLOCK(inp); 197955bceb1eSRandall Stewart error = sooptcopyout(sopt, &fsn, sizeof fsn); 198055bceb1eSRandall Stewart return (error); 198155bceb1eSRandall Stewart } 1982fc4d53ccSGleb Smirnoff 1983fc4d53ccSGleb Smirnoff /* Pass in the INP locked, caller must unlock it. */ 1984fc4d53ccSGleb Smirnoff return (tp->t_fb->tfb_tcp_ctloutput(inp->inp_socket, sopt, inp, tp)); 1985fc4d53ccSGleb Smirnoff } 1986fc4d53ccSGleb Smirnoff 1987fc4d53ccSGleb Smirnoff int 1988fc4d53ccSGleb Smirnoff tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1989fc4d53ccSGleb Smirnoff { 1990fc4d53ccSGleb Smirnoff int error; 1991fc4d53ccSGleb Smirnoff struct inpcb *inp; 1992fc4d53ccSGleb Smirnoff 1993fc4d53ccSGleb Smirnoff error = 0; 1994fc4d53ccSGleb Smirnoff inp = sotoinpcb(so); 1995fc4d53ccSGleb Smirnoff KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 1996fc4d53ccSGleb Smirnoff 1997fc4d53ccSGleb Smirnoff if (sopt->sopt_dir == SOPT_SET) 1998fc4d53ccSGleb Smirnoff return (tcp_ctloutput_set(inp, sopt)); 1999fc4d53ccSGleb Smirnoff else if (sopt->sopt_dir == SOPT_GET) 2000fc4d53ccSGleb Smirnoff return (tcp_ctloutput_get(inp, sopt)); 2001fc4d53ccSGleb Smirnoff else 2002fc4d53ccSGleb Smirnoff panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir); 200355bceb1eSRandall Stewart } 200455bceb1eSRandall Stewart 20052529f56eSJonathan T. Looney /* 20062529f56eSJonathan T. Looney * If this assert becomes untrue, we need to change the size of the buf 20072529f56eSJonathan T. Looney * variable in tcp_default_ctloutput(). 20082529f56eSJonathan T. Looney */ 20092529f56eSJonathan T. Looney #ifdef CTASSERT 20102529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN); 20112529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN); 20122529f56eSJonathan T. Looney #endif 20132529f56eSJonathan T. Looney 2014ec1db6e1SJohn Baldwin #ifdef KERN_TLS 2015ec1db6e1SJohn Baldwin static int 2016ec1db6e1SJohn Baldwin copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls) 2017ec1db6e1SJohn Baldwin { 2018ec1db6e1SJohn Baldwin struct tls_enable_v0 tls_v0; 2019ec1db6e1SJohn Baldwin int error; 2020ec1db6e1SJohn Baldwin 2021ec1db6e1SJohn Baldwin if (sopt->sopt_valsize == sizeof(tls_v0)) { 2022ec1db6e1SJohn Baldwin error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0), 2023ec1db6e1SJohn Baldwin sizeof(tls_v0)); 2024ec1db6e1SJohn Baldwin if (error) 2025ec1db6e1SJohn Baldwin return (error); 2026ec1db6e1SJohn Baldwin memset(tls, 0, sizeof(*tls)); 2027ec1db6e1SJohn Baldwin tls->cipher_key = tls_v0.cipher_key; 2028ec1db6e1SJohn Baldwin tls->iv = tls_v0.iv; 2029ec1db6e1SJohn Baldwin tls->auth_key = tls_v0.auth_key; 2030ec1db6e1SJohn Baldwin tls->cipher_algorithm = tls_v0.cipher_algorithm; 2031ec1db6e1SJohn Baldwin tls->cipher_key_len = tls_v0.cipher_key_len; 2032ec1db6e1SJohn Baldwin tls->iv_len = tls_v0.iv_len; 2033ec1db6e1SJohn Baldwin tls->auth_algorithm = tls_v0.auth_algorithm; 2034ec1db6e1SJohn Baldwin tls->auth_key_len = tls_v0.auth_key_len; 2035ec1db6e1SJohn Baldwin tls->flags = tls_v0.flags; 2036ec1db6e1SJohn Baldwin tls->tls_vmajor = tls_v0.tls_vmajor; 2037ec1db6e1SJohn Baldwin tls->tls_vminor = tls_v0.tls_vminor; 2038ec1db6e1SJohn Baldwin return (0); 2039ec1db6e1SJohn Baldwin } 2040ec1db6e1SJohn Baldwin 2041ec1db6e1SJohn Baldwin return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls))); 2042ec1db6e1SJohn Baldwin } 2043ec1db6e1SJohn Baldwin #endif 2044ec1db6e1SJohn Baldwin 204555bceb1eSRandall Stewart int 204655bceb1eSRandall Stewart tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp) 204755bceb1eSRandall Stewart { 204855bceb1eSRandall Stewart int error, opt, optval; 204955bceb1eSRandall Stewart u_int ui; 205055bceb1eSRandall Stewart struct tcp_info ti; 2051b2e60773SJohn Baldwin #ifdef KERN_TLS 2052b2e60773SJohn Baldwin struct tls_enable tls; 2053b2e60773SJohn Baldwin #endif 205455bceb1eSRandall Stewart struct cc_algo *algo; 20552529f56eSJonathan T. Looney char *pbuf, buf[TCP_LOG_ID_LEN]; 2056adc56f5aSEdward Tomasz Napierala #ifdef STATS 2057adc56f5aSEdward Tomasz Napierala struct statsblob *sbp; 2058adc56f5aSEdward Tomasz Napierala #endif 2059af6fef3aSGleb Smirnoff size_t len; 2060df8bae1dSRodney W. Grimes 2061d519cedbSGleb Smirnoff /* 2062d519cedbSGleb Smirnoff * For TCP_CCALGOOPT forward the control to CC module, for both 2063d519cedbSGleb Smirnoff * SOPT_SET and SOPT_GET. 2064d519cedbSGleb Smirnoff */ 2065d519cedbSGleb Smirnoff switch (sopt->sopt_name) { 2066d519cedbSGleb Smirnoff case TCP_CCALGOOPT: 2067d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 2068c8b53cedSMichael Tuexen if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT) 2069c8b53cedSMichael Tuexen return (EINVAL); 2070af6fef3aSGleb Smirnoff pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO); 2071af6fef3aSGleb Smirnoff error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize, 2072d519cedbSGleb Smirnoff sopt->sopt_valsize); 2073d519cedbSGleb Smirnoff if (error) { 2074af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 2075d519cedbSGleb Smirnoff return (error); 2076d519cedbSGleb Smirnoff } 2077bac5bedfSConrad Meyer INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP)); 2078d519cedbSGleb Smirnoff if (CC_ALGO(tp)->ctl_output != NULL) 2079af6fef3aSGleb Smirnoff error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf); 2080d519cedbSGleb Smirnoff else 2081d519cedbSGleb Smirnoff error = ENOENT; 2082d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 2083d519cedbSGleb Smirnoff if (error == 0 && sopt->sopt_dir == SOPT_GET) 2084af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize); 2085af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 2086d519cedbSGleb Smirnoff return (error); 2087d519cedbSGleb Smirnoff } 2088d519cedbSGleb Smirnoff 2089cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 2090cfe8b629SGarrett Wollman case SOPT_SET: 2091cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2092fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 209388f6b043SBruce M Simpson case TCP_MD5SIG: 2094fcf59617SAndrey V. Elsukov if (!TCPMD5_ENABLED()) { 20958501a69cSRobert Watson INP_WUNLOCK(inp); 2096fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 2097fcf59617SAndrey V. Elsukov } 2098fcf59617SAndrey V. Elsukov error = TCPMD5_PCBCTL(inp, sopt); 20991cfd4b53SBruce M Simpson if (error) 21001e8f5ffaSRobert Watson return (error); 210109fe6320SNavdeep Parhar goto unlock_and_done; 2102fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 210309fe6320SNavdeep Parhar 2104df8bae1dSRodney W. Grimes case TCP_NODELAY: 2105cfe8b629SGarrett Wollman case TCP_NOOPT: 21060471a8c7SRichard Scheffenegger case TCP_LRD: 21078501a69cSRobert Watson INP_WUNLOCK(inp); 2108cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 2109cfe8b629SGarrett Wollman sizeof optval); 2110cfe8b629SGarrett Wollman if (error) 21111e8f5ffaSRobert Watson return (error); 2112cfe8b629SGarrett Wollman 21138501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 2114cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2115cfe8b629SGarrett Wollman case TCP_NODELAY: 2116cfe8b629SGarrett Wollman opt = TF_NODELAY; 2117cfe8b629SGarrett Wollman break; 2118cfe8b629SGarrett Wollman case TCP_NOOPT: 2119cfe8b629SGarrett Wollman opt = TF_NOOPT; 2120cfe8b629SGarrett Wollman break; 21210471a8c7SRichard Scheffenegger case TCP_LRD: 21220471a8c7SRichard Scheffenegger opt = TF_LRD; 21230471a8c7SRichard Scheffenegger break; 2124cfe8b629SGarrett Wollman default: 2125cfe8b629SGarrett Wollman opt = 0; /* dead code to fool gcc */ 2126cfe8b629SGarrett Wollman break; 2127cfe8b629SGarrett Wollman } 2128cfe8b629SGarrett Wollman 2129cfe8b629SGarrett Wollman if (optval) 2130cfe8b629SGarrett Wollman tp->t_flags |= opt; 2131df8bae1dSRodney W. Grimes else 2132cfe8b629SGarrett Wollman tp->t_flags &= ~opt; 213309fe6320SNavdeep Parhar unlock_and_done: 213409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 213509fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 213609fe6320SNavdeep Parhar tcp_offload_ctloutput(tp, sopt->sopt_dir, 213709fe6320SNavdeep Parhar sopt->sopt_name); 213809fe6320SNavdeep Parhar } 213909fe6320SNavdeep Parhar #endif 21408501a69cSRobert Watson INP_WUNLOCK(inp); 2141df8bae1dSRodney W. Grimes break; 2142df8bae1dSRodney W. Grimes 2143007581c0SJonathan Lemon case TCP_NOPUSH: 21448501a69cSRobert Watson INP_WUNLOCK(inp); 2145007581c0SJonathan Lemon error = sooptcopyin(sopt, &optval, sizeof optval, 2146007581c0SJonathan Lemon sizeof optval); 2147007581c0SJonathan Lemon if (error) 21481e8f5ffaSRobert Watson return (error); 2149007581c0SJonathan Lemon 21508501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 2151007581c0SJonathan Lemon if (optval) 2152007581c0SJonathan Lemon tp->t_flags |= TF_NOPUSH; 2153d28b9e89SJohn Baldwin else if (tp->t_flags & TF_NOPUSH) { 2154007581c0SJonathan Lemon tp->t_flags &= ~TF_NOPUSH; 2155109eb549SGleb Smirnoff if (TCPS_HAVEESTABLISHED(tp->t_state)) { 2156109eb549SGleb Smirnoff struct epoch_tracker et; 2157109eb549SGleb Smirnoff 2158109eb549SGleb Smirnoff NET_EPOCH_ENTER(et); 215955bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 2160109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 2161109eb549SGleb Smirnoff } 2162007581c0SJonathan Lemon } 216309fe6320SNavdeep Parhar goto unlock_and_done; 2164007581c0SJonathan Lemon 21659e644c23SMichael Tuexen case TCP_REMOTE_UDP_ENCAPS_PORT: 21669e644c23SMichael Tuexen INP_WUNLOCK(inp); 21679e644c23SMichael Tuexen error = sooptcopyin(sopt, &optval, sizeof optval, 21689e644c23SMichael Tuexen sizeof optval); 21699e644c23SMichael Tuexen if (error) 21709e644c23SMichael Tuexen return (error); 21719e644c23SMichael Tuexen if ((optval < TCP_TUNNELING_PORT_MIN) || 21729e644c23SMichael Tuexen (optval > TCP_TUNNELING_PORT_MAX)) { 21739e644c23SMichael Tuexen /* Its got to be in range */ 21749e644c23SMichael Tuexen return (EINVAL); 21759e644c23SMichael Tuexen } 21769e644c23SMichael Tuexen if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) { 21779e644c23SMichael Tuexen /* You have to have enabled a UDP tunneling port first */ 21789e644c23SMichael Tuexen return (EINVAL); 21799e644c23SMichael Tuexen } 21809e644c23SMichael Tuexen INP_WLOCK_RECHECK(inp); 21819e644c23SMichael Tuexen if (tp->t_state != TCPS_CLOSED) { 21829e644c23SMichael Tuexen /* You can't change after you are connected */ 21839e644c23SMichael Tuexen error = EINVAL; 21849e644c23SMichael Tuexen } else { 21859e644c23SMichael Tuexen /* Ok we are all good set the port */ 21869e644c23SMichael Tuexen tp->t_port = htons(optval); 21879e644c23SMichael Tuexen } 21889e644c23SMichael Tuexen goto unlock_and_done; 21899e644c23SMichael Tuexen 2190df8bae1dSRodney W. Grimes case TCP_MAXSEG: 21918501a69cSRobert Watson INP_WUNLOCK(inp); 2192cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 2193cfe8b629SGarrett Wollman sizeof optval); 2194cfe8b629SGarrett Wollman if (error) 21951e8f5ffaSRobert Watson return (error); 2196df8bae1dSRodney W. Grimes 21978501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 219853369ac9SAndre Oppermann if (optval > 0 && optval <= tp->t_maxseg && 2199603724d3SBjoern A. Zeeb optval + 40 >= V_tcp_minmss) 2200cfe8b629SGarrett Wollman tp->t_maxseg = optval; 2201a0292f23SGarrett Wollman else 2202a0292f23SGarrett Wollman error = EINVAL; 220309fe6320SNavdeep Parhar goto unlock_and_done; 2204a0292f23SGarrett Wollman 2205b8af5dfaSRobert Watson case TCP_INFO: 22068501a69cSRobert Watson INP_WUNLOCK(inp); 2207b8af5dfaSRobert Watson error = EINVAL; 2208b8af5dfaSRobert Watson break; 2209b8af5dfaSRobert Watson 2210adc56f5aSEdward Tomasz Napierala case TCP_STATS: 2211adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2212adc56f5aSEdward Tomasz Napierala #ifdef STATS 2213adc56f5aSEdward Tomasz Napierala error = sooptcopyin(sopt, &optval, sizeof optval, 2214adc56f5aSEdward Tomasz Napierala sizeof optval); 2215adc56f5aSEdward Tomasz Napierala if (error) 2216adc56f5aSEdward Tomasz Napierala return (error); 2217adc56f5aSEdward Tomasz Napierala 2218adc56f5aSEdward Tomasz Napierala if (optval > 0) 2219adc56f5aSEdward Tomasz Napierala sbp = stats_blob_alloc( 2220adc56f5aSEdward Tomasz Napierala V_tcp_perconn_stats_dflt_tpl, 0); 2221adc56f5aSEdward Tomasz Napierala else 2222adc56f5aSEdward Tomasz Napierala sbp = NULL; 2223adc56f5aSEdward Tomasz Napierala 2224adc56f5aSEdward Tomasz Napierala INP_WLOCK_RECHECK(inp); 2225adc56f5aSEdward Tomasz Napierala if ((tp->t_stats != NULL && sbp == NULL) || 2226adc56f5aSEdward Tomasz Napierala (tp->t_stats == NULL && sbp != NULL)) { 2227adc56f5aSEdward Tomasz Napierala struct statsblob *t = tp->t_stats; 2228adc56f5aSEdward Tomasz Napierala tp->t_stats = sbp; 2229adc56f5aSEdward Tomasz Napierala sbp = t; 2230adc56f5aSEdward Tomasz Napierala } 2231adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2232adc56f5aSEdward Tomasz Napierala 2233adc56f5aSEdward Tomasz Napierala stats_blob_destroy(sbp); 2234adc56f5aSEdward Tomasz Napierala #else 2235adc56f5aSEdward Tomasz Napierala return (EOPNOTSUPP); 2236adc56f5aSEdward Tomasz Napierala #endif /* !STATS */ 2237adc56f5aSEdward Tomasz Napierala break; 2238adc56f5aSEdward Tomasz Napierala 2239dbc42409SLawrence Stewart case TCP_CONGESTION: 2240dbc42409SLawrence Stewart INP_WUNLOCK(inp); 2241af6fef3aSGleb Smirnoff error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1); 2242af6fef3aSGleb Smirnoff if (error) 2243dbc42409SLawrence Stewart break; 2244af6fef3aSGleb Smirnoff buf[sopt->sopt_valsize] = '\0'; 2245af6fef3aSGleb Smirnoff INP_WLOCK_RECHECK(inp); 224673e263b1SGleb Smirnoff CC_LIST_RLOCK(); 224773e263b1SGleb Smirnoff STAILQ_FOREACH(algo, &cc_list, entries) 224873e263b1SGleb Smirnoff if (strncmp(buf, algo->name, 224973e263b1SGleb Smirnoff TCP_CA_NAME_MAX) == 0) 225073e263b1SGleb Smirnoff break; 225173e263b1SGleb Smirnoff CC_LIST_RUNLOCK(); 225273e263b1SGleb Smirnoff if (algo == NULL) { 2253af6fef3aSGleb Smirnoff INP_WUNLOCK(inp); 225473e263b1SGleb Smirnoff error = EINVAL; 225573e263b1SGleb Smirnoff break; 225673e263b1SGleb Smirnoff } 2257dbc42409SLawrence Stewart /* 225873e263b1SGleb Smirnoff * We hold a write lock over the tcb so it's safe to 225973e263b1SGleb Smirnoff * do these things without ordering concerns. 2260dbc42409SLawrence Stewart */ 2261dbc42409SLawrence Stewart if (CC_ALGO(tp)->cb_destroy != NULL) 2262dbc42409SLawrence Stewart CC_ALGO(tp)->cb_destroy(tp->ccv); 226322699887SMatt Macy CC_DATA(tp) = NULL; 2264dbc42409SLawrence Stewart CC_ALGO(tp) = algo; 2265dbc42409SLawrence Stewart /* 226673e263b1SGleb Smirnoff * If something goes pear shaped initialising the new 226773e263b1SGleb Smirnoff * algo, fall back to newreno (which does not 226873e263b1SGleb Smirnoff * require initialisation). 2269dbc42409SLawrence Stewart */ 227073e263b1SGleb Smirnoff if (algo->cb_init != NULL && 227173e263b1SGleb Smirnoff algo->cb_init(tp->ccv) != 0) { 2272dbc42409SLawrence Stewart CC_ALGO(tp) = &newreno_cc_algo; 2273dbc42409SLawrence Stewart /* 227473e263b1SGleb Smirnoff * The only reason init should fail is 2275dbc42409SLawrence Stewart * because of malloc. 2276dbc42409SLawrence Stewart */ 2277dbc42409SLawrence Stewart error = ENOMEM; 2278dbc42409SLawrence Stewart } 227973e263b1SGleb Smirnoff INP_WUNLOCK(inp); 228073e263b1SGleb Smirnoff break; 2281dbc42409SLawrence Stewart 2282a034518aSAndrew Gallatin case TCP_REUSPORT_LB_NUMA: 2283a034518aSAndrew Gallatin INP_WUNLOCK(inp); 2284a034518aSAndrew Gallatin error = sooptcopyin(sopt, &optval, sizeof(optval), 2285a034518aSAndrew Gallatin sizeof(optval)); 2286a034518aSAndrew Gallatin INP_WLOCK_RECHECK(inp); 2287a034518aSAndrew Gallatin if (!error) 2288a034518aSAndrew Gallatin error = in_pcblbgroup_numa(inp, optval); 2289a034518aSAndrew Gallatin INP_WUNLOCK(inp); 2290a034518aSAndrew Gallatin break; 2291a034518aSAndrew Gallatin 2292b2e60773SJohn Baldwin #ifdef KERN_TLS 2293b2e60773SJohn Baldwin case TCP_TXTLS_ENABLE: 2294b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2295ec1db6e1SJohn Baldwin error = copyin_tls_enable(sopt, &tls); 2296b2e60773SJohn Baldwin if (error) 2297b2e60773SJohn Baldwin break; 2298b2e60773SJohn Baldwin error = ktls_enable_tx(so, &tls); 2299b2e60773SJohn Baldwin break; 2300b2e60773SJohn Baldwin case TCP_TXTLS_MODE: 2301b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2302b2e60773SJohn Baldwin error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 2303b2e60773SJohn Baldwin if (error) 2304b2e60773SJohn Baldwin return (error); 2305b2e60773SJohn Baldwin 2306b2e60773SJohn Baldwin INP_WLOCK_RECHECK(inp); 2307b2e60773SJohn Baldwin error = ktls_set_tx_mode(so, ui); 2308b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2309b2e60773SJohn Baldwin break; 2310f1f93475SJohn Baldwin case TCP_RXTLS_ENABLE: 2311f1f93475SJohn Baldwin INP_WUNLOCK(inp); 2312f1f93475SJohn Baldwin error = sooptcopyin(sopt, &tls, sizeof(tls), 2313f1f93475SJohn Baldwin sizeof(tls)); 2314f1f93475SJohn Baldwin if (error) 2315f1f93475SJohn Baldwin break; 2316f1f93475SJohn Baldwin error = ktls_enable_rx(so, &tls); 2317f1f93475SJohn Baldwin break; 2318b2e60773SJohn Baldwin #endif 2319b2e60773SJohn Baldwin 23209077f387SGleb Smirnoff case TCP_KEEPIDLE: 23219077f387SGleb Smirnoff case TCP_KEEPINTVL: 23229077f387SGleb Smirnoff case TCP_KEEPINIT: 23239077f387SGleb Smirnoff INP_WUNLOCK(inp); 23249077f387SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 23259077f387SGleb Smirnoff if (error) 23269077f387SGleb Smirnoff return (error); 23279077f387SGleb Smirnoff 23289077f387SGleb Smirnoff if (ui > (UINT_MAX / hz)) { 23299077f387SGleb Smirnoff error = EINVAL; 23309077f387SGleb Smirnoff break; 23319077f387SGleb Smirnoff } 23329077f387SGleb Smirnoff ui *= hz; 23339077f387SGleb Smirnoff 23349077f387SGleb Smirnoff INP_WLOCK_RECHECK(inp); 23359077f387SGleb Smirnoff switch (sopt->sopt_name) { 23369077f387SGleb Smirnoff case TCP_KEEPIDLE: 23379077f387SGleb Smirnoff tp->t_keepidle = ui; 23389077f387SGleb Smirnoff /* 23399077f387SGleb Smirnoff * XXX: better check current remaining 23409077f387SGleb Smirnoff * timeout and "merge" it with new value. 23419077f387SGleb Smirnoff */ 23429077f387SGleb Smirnoff if ((tp->t_state > TCPS_LISTEN) && 23439077f387SGleb Smirnoff (tp->t_state <= TCPS_CLOSING)) 23449077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 23459077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 23469077f387SGleb Smirnoff break; 23479077f387SGleb Smirnoff case TCP_KEEPINTVL: 23489077f387SGleb Smirnoff tp->t_keepintvl = ui; 23499077f387SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 23509077f387SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 23519077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 23529077f387SGleb Smirnoff TP_MAXIDLE(tp)); 23539077f387SGleb Smirnoff break; 23549077f387SGleb Smirnoff case TCP_KEEPINIT: 23559077f387SGleb Smirnoff tp->t_keepinit = ui; 23569077f387SGleb Smirnoff if (tp->t_state == TCPS_SYN_RECEIVED || 23579077f387SGleb Smirnoff tp->t_state == TCPS_SYN_SENT) 23589077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 23599077f387SGleb Smirnoff TP_KEEPINIT(tp)); 23609077f387SGleb Smirnoff break; 23619077f387SGleb Smirnoff } 236209fe6320SNavdeep Parhar goto unlock_and_done; 23639077f387SGleb Smirnoff 236485c05144SGleb Smirnoff case TCP_KEEPCNT: 236585c05144SGleb Smirnoff INP_WUNLOCK(inp); 236685c05144SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 236785c05144SGleb Smirnoff if (error) 236885c05144SGleb Smirnoff return (error); 236985c05144SGleb Smirnoff 237085c05144SGleb Smirnoff INP_WLOCK_RECHECK(inp); 237185c05144SGleb Smirnoff tp->t_keepcnt = ui; 237285c05144SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 237385c05144SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 237485c05144SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 237585c05144SGleb Smirnoff TP_MAXIDLE(tp)); 237685c05144SGleb Smirnoff goto unlock_and_done; 237785c05144SGleb Smirnoff 237886a996e6SHiren Panchasara #ifdef TCPPCAP 237986a996e6SHiren Panchasara case TCP_PCAP_OUT: 238086a996e6SHiren Panchasara case TCP_PCAP_IN: 238186a996e6SHiren Panchasara INP_WUNLOCK(inp); 238286a996e6SHiren Panchasara error = sooptcopyin(sopt, &optval, sizeof optval, 238386a996e6SHiren Panchasara sizeof optval); 238486a996e6SHiren Panchasara if (error) 238586a996e6SHiren Panchasara return (error); 238686a996e6SHiren Panchasara 238786a996e6SHiren Panchasara INP_WLOCK_RECHECK(inp); 238886a996e6SHiren Panchasara if (optval >= 0) 238986a996e6SHiren Panchasara tcp_pcap_set_sock_max(TCP_PCAP_OUT ? 239086a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts), 239186a996e6SHiren Panchasara optval); 239286a996e6SHiren Panchasara else 239386a996e6SHiren Panchasara error = EINVAL; 239486a996e6SHiren Panchasara goto unlock_and_done; 239586a996e6SHiren Panchasara #endif 239686a996e6SHiren Panchasara 2397c560df6fSPatrick Kelsey case TCP_FASTOPEN: { 2398c560df6fSPatrick Kelsey struct tcp_fastopen tfo_optval; 2399c560df6fSPatrick Kelsey 2400281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 2401c560df6fSPatrick Kelsey if (!V_tcp_fastopen_client_enable && 2402c560df6fSPatrick Kelsey !V_tcp_fastopen_server_enable) 2403281a0fd4SPatrick Kelsey return (EPERM); 2404281a0fd4SPatrick Kelsey 2405c560df6fSPatrick Kelsey error = sooptcopyin(sopt, &tfo_optval, 2406c560df6fSPatrick Kelsey sizeof(tfo_optval), sizeof(int)); 2407281a0fd4SPatrick Kelsey if (error) 2408281a0fd4SPatrick Kelsey return (error); 2409281a0fd4SPatrick Kelsey 2410281a0fd4SPatrick Kelsey INP_WLOCK_RECHECK(inp); 2411d442a657SMichael Tuexen if ((tp->t_state != TCPS_CLOSED) && 2412d442a657SMichael Tuexen (tp->t_state != TCPS_LISTEN)) { 2413d442a657SMichael Tuexen error = EINVAL; 2414d442a657SMichael Tuexen goto unlock_and_done; 2415d442a657SMichael Tuexen } 2416c560df6fSPatrick Kelsey if (tfo_optval.enable) { 2417c560df6fSPatrick Kelsey if (tp->t_state == TCPS_LISTEN) { 2418c560df6fSPatrick Kelsey if (!V_tcp_fastopen_server_enable) { 2419c560df6fSPatrick Kelsey error = EPERM; 2420c560df6fSPatrick Kelsey goto unlock_and_done; 2421c560df6fSPatrick Kelsey } 2422c560df6fSPatrick Kelsey 2423c560df6fSPatrick Kelsey if (tp->t_tfo_pending == NULL) 2424281a0fd4SPatrick Kelsey tp->t_tfo_pending = 2425281a0fd4SPatrick Kelsey tcp_fastopen_alloc_counter(); 2426c560df6fSPatrick Kelsey } else { 2427c560df6fSPatrick Kelsey /* 2428c560df6fSPatrick Kelsey * If a pre-shared key was provided, 2429c560df6fSPatrick Kelsey * stash it in the client cookie 2430c560df6fSPatrick Kelsey * field of the tcpcb for use during 2431c560df6fSPatrick Kelsey * connect. 2432c560df6fSPatrick Kelsey */ 2433c560df6fSPatrick Kelsey if (sopt->sopt_valsize == 2434c560df6fSPatrick Kelsey sizeof(tfo_optval)) { 2435c560df6fSPatrick Kelsey memcpy(tp->t_tfo_cookie.client, 2436c560df6fSPatrick Kelsey tfo_optval.psk, 2437c560df6fSPatrick Kelsey TCP_FASTOPEN_PSK_LEN); 2438c560df6fSPatrick Kelsey tp->t_tfo_client_cookie_len = 2439c560df6fSPatrick Kelsey TCP_FASTOPEN_PSK_LEN; 2440c560df6fSPatrick Kelsey } 2441c560df6fSPatrick Kelsey } 2442d442a657SMichael Tuexen tp->t_flags |= TF_FASTOPEN; 2443281a0fd4SPatrick Kelsey } else 2444281a0fd4SPatrick Kelsey tp->t_flags &= ~TF_FASTOPEN; 2445281a0fd4SPatrick Kelsey goto unlock_and_done; 2446c560df6fSPatrick Kelsey } 2447281a0fd4SPatrick Kelsey 2448e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX 24492529f56eSJonathan T. Looney case TCP_LOG: 24502529f56eSJonathan T. Looney INP_WUNLOCK(inp); 24512529f56eSJonathan T. Looney error = sooptcopyin(sopt, &optval, sizeof optval, 24522529f56eSJonathan T. Looney sizeof optval); 24532529f56eSJonathan T. Looney if (error) 24542529f56eSJonathan T. Looney return (error); 24552529f56eSJonathan T. Looney 24562529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 24572529f56eSJonathan T. Looney error = tcp_log_state_change(tp, optval); 24582529f56eSJonathan T. Looney goto unlock_and_done; 24592529f56eSJonathan T. Looney 24602529f56eSJonathan T. Looney case TCP_LOGBUF: 24612529f56eSJonathan T. Looney INP_WUNLOCK(inp); 24622529f56eSJonathan T. Looney error = EINVAL; 24632529f56eSJonathan T. Looney break; 24642529f56eSJonathan T. Looney 24652529f56eSJonathan T. Looney case TCP_LOGID: 24662529f56eSJonathan T. Looney INP_WUNLOCK(inp); 24672529f56eSJonathan T. Looney error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0); 24682529f56eSJonathan T. Looney if (error) 24692529f56eSJonathan T. Looney break; 24702529f56eSJonathan T. Looney buf[sopt->sopt_valsize] = '\0'; 24712529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 24722529f56eSJonathan T. Looney error = tcp_log_set_id(tp, buf); 24732529f56eSJonathan T. Looney /* tcp_log_set_id() unlocks the INP. */ 24742529f56eSJonathan T. Looney break; 24752529f56eSJonathan T. Looney 24762529f56eSJonathan T. Looney case TCP_LOGDUMP: 24772529f56eSJonathan T. Looney case TCP_LOGDUMPID: 24782529f56eSJonathan T. Looney INP_WUNLOCK(inp); 24792529f56eSJonathan T. Looney error = 24802529f56eSJonathan T. Looney sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0); 24812529f56eSJonathan T. Looney if (error) 24822529f56eSJonathan T. Looney break; 24832529f56eSJonathan T. Looney buf[sopt->sopt_valsize] = '\0'; 24842529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 24852529f56eSJonathan T. Looney if (sopt->sopt_name == TCP_LOGDUMP) { 24862529f56eSJonathan T. Looney error = tcp_log_dump_tp_logbuf(tp, buf, 24872529f56eSJonathan T. Looney M_WAITOK, true); 24882529f56eSJonathan T. Looney INP_WUNLOCK(inp); 24892529f56eSJonathan T. Looney } else { 24902529f56eSJonathan T. Looney tcp_log_dump_tp_bucket_logbufs(tp, buf); 24912529f56eSJonathan T. Looney /* 24922529f56eSJonathan T. Looney * tcp_log_dump_tp_bucket_logbufs() drops the 24932529f56eSJonathan T. Looney * INP lock. 24942529f56eSJonathan T. Looney */ 24952529f56eSJonathan T. Looney } 24962529f56eSJonathan T. Looney break; 2497e24e5683SJonathan T. Looney #endif 24982529f56eSJonathan T. Looney 2499df8bae1dSRodney W. Grimes default: 25008501a69cSRobert Watson INP_WUNLOCK(inp); 2501df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 2502df8bae1dSRodney W. Grimes break; 2503df8bae1dSRodney W. Grimes } 2504df8bae1dSRodney W. Grimes break; 2505df8bae1dSRodney W. Grimes 2506cfe8b629SGarrett Wollman case SOPT_GET: 25071e8f5ffaSRobert Watson tp = intotcpcb(inp); 2508cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2509fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 251088f6b043SBruce M Simpson case TCP_MD5SIG: 2511fcf59617SAndrey V. Elsukov if (!TCPMD5_ENABLED()) { 25128501a69cSRobert Watson INP_WUNLOCK(inp); 2513fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 2514fcf59617SAndrey V. Elsukov } 2515fcf59617SAndrey V. Elsukov error = TCPMD5_PCBCTL(inp, sopt); 25161cfd4b53SBruce M Simpson break; 2517265ed012SBruce M Simpson #endif 25181e8f5ffaSRobert Watson 2519df8bae1dSRodney W. Grimes case TCP_NODELAY: 2520cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NODELAY; 25218501a69cSRobert Watson INP_WUNLOCK(inp); 2522b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2523df8bae1dSRodney W. Grimes break; 2524df8bae1dSRodney W. Grimes case TCP_MAXSEG: 2525cfe8b629SGarrett Wollman optval = tp->t_maxseg; 25268501a69cSRobert Watson INP_WUNLOCK(inp); 2527b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2528df8bae1dSRodney W. Grimes break; 25299e644c23SMichael Tuexen case TCP_REMOTE_UDP_ENCAPS_PORT: 25309e644c23SMichael Tuexen optval = ntohs(tp->t_port); 25319e644c23SMichael Tuexen INP_WUNLOCK(inp); 25329e644c23SMichael Tuexen error = sooptcopyout(sopt, &optval, sizeof optval); 25339e644c23SMichael Tuexen break; 2534a0292f23SGarrett Wollman case TCP_NOOPT: 2535cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOOPT; 25368501a69cSRobert Watson INP_WUNLOCK(inp); 2537b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2538a0292f23SGarrett Wollman break; 2539a0292f23SGarrett Wollman case TCP_NOPUSH: 2540cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOPUSH; 25418501a69cSRobert Watson INP_WUNLOCK(inp); 2542b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2543b8af5dfaSRobert Watson break; 2544b8af5dfaSRobert Watson case TCP_INFO: 2545b8af5dfaSRobert Watson tcp_fill_info(tp, &ti); 25468501a69cSRobert Watson INP_WUNLOCK(inp); 2547b8af5dfaSRobert Watson error = sooptcopyout(sopt, &ti, sizeof ti); 2548a0292f23SGarrett Wollman break; 2549adc56f5aSEdward Tomasz Napierala case TCP_STATS: 2550adc56f5aSEdward Tomasz Napierala { 2551adc56f5aSEdward Tomasz Napierala #ifdef STATS 2552adc56f5aSEdward Tomasz Napierala int nheld; 2553adc56f5aSEdward Tomasz Napierala TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0; 2554adc56f5aSEdward Tomasz Napierala 2555adc56f5aSEdward Tomasz Napierala error = 0; 2556adc56f5aSEdward Tomasz Napierala socklen_t outsbsz = sopt->sopt_valsize; 2557adc56f5aSEdward Tomasz Napierala if (tp->t_stats == NULL) 2558adc56f5aSEdward Tomasz Napierala error = ENOENT; 2559adc56f5aSEdward Tomasz Napierala else if (outsbsz >= tp->t_stats->cursz) 2560adc56f5aSEdward Tomasz Napierala outsbsz = tp->t_stats->cursz; 2561adc56f5aSEdward Tomasz Napierala else if (outsbsz >= sizeof(struct statsblob)) 2562adc56f5aSEdward Tomasz Napierala outsbsz = sizeof(struct statsblob); 2563adc56f5aSEdward Tomasz Napierala else 2564adc56f5aSEdward Tomasz Napierala error = EINVAL; 2565adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2566adc56f5aSEdward Tomasz Napierala if (error) 2567adc56f5aSEdward Tomasz Napierala break; 2568adc56f5aSEdward Tomasz Napierala 2569adc56f5aSEdward Tomasz Napierala sbp = sopt->sopt_val; 2570adc56f5aSEdward Tomasz Napierala nheld = atop(round_page(((vm_offset_t)sbp) + 2571adc56f5aSEdward Tomasz Napierala (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp)); 2572adc56f5aSEdward Tomasz Napierala vm_page_t ma[nheld]; 2573adc56f5aSEdward Tomasz Napierala if (vm_fault_quick_hold_pages( 2574adc56f5aSEdward Tomasz Napierala &curproc->p_vmspace->vm_map, (vm_offset_t)sbp, 2575adc56f5aSEdward Tomasz Napierala outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma, 2576adc56f5aSEdward Tomasz Napierala nheld) < 0) { 2577adc56f5aSEdward Tomasz Napierala error = EFAULT; 2578adc56f5aSEdward Tomasz Napierala break; 2579adc56f5aSEdward Tomasz Napierala } 2580adc56f5aSEdward Tomasz Napierala 2581adc56f5aSEdward Tomasz Napierala if ((error = copyin_nofault(&(sbp->flags), &sbflags, 2582adc56f5aSEdward Tomasz Napierala SIZEOF_MEMBER(struct statsblob, flags)))) 2583adc56f5aSEdward Tomasz Napierala goto unhold; 2584adc56f5aSEdward Tomasz Napierala 2585adc56f5aSEdward Tomasz Napierala INP_WLOCK_RECHECK(inp); 2586adc56f5aSEdward Tomasz Napierala error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats, 2587adc56f5aSEdward Tomasz Napierala sbflags | SB_CLONE_USRDSTNOFAULT); 2588adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2589adc56f5aSEdward Tomasz Napierala sopt->sopt_valsize = outsbsz; 2590adc56f5aSEdward Tomasz Napierala unhold: 2591adc56f5aSEdward Tomasz Napierala vm_page_unhold_pages(ma, nheld); 2592adc56f5aSEdward Tomasz Napierala #else 2593adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2594adc56f5aSEdward Tomasz Napierala error = EOPNOTSUPP; 2595adc56f5aSEdward Tomasz Napierala #endif /* !STATS */ 2596adc56f5aSEdward Tomasz Napierala break; 2597adc56f5aSEdward Tomasz Napierala } 2598dbc42409SLawrence Stewart case TCP_CONGESTION: 2599af6fef3aSGleb Smirnoff len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); 2600dbc42409SLawrence Stewart INP_WUNLOCK(inp); 2601af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, buf, len + 1); 2602dbc42409SLawrence Stewart break; 26032f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 26042f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 26052f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 26062f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 26072f3eb7f4SGleb Smirnoff switch (sopt->sopt_name) { 26082f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 26095a17b6adSMichael Tuexen ui = TP_KEEPIDLE(tp) / hz; 26102f3eb7f4SGleb Smirnoff break; 26112f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 26125a17b6adSMichael Tuexen ui = TP_KEEPINTVL(tp) / hz; 26132f3eb7f4SGleb Smirnoff break; 26142f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 26155a17b6adSMichael Tuexen ui = TP_KEEPINIT(tp) / hz; 26162f3eb7f4SGleb Smirnoff break; 26172f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 26185a17b6adSMichael Tuexen ui = TP_KEEPCNT(tp); 26192f3eb7f4SGleb Smirnoff break; 26202f3eb7f4SGleb Smirnoff } 26212f3eb7f4SGleb Smirnoff INP_WUNLOCK(inp); 26222f3eb7f4SGleb Smirnoff error = sooptcopyout(sopt, &ui, sizeof(ui)); 26232f3eb7f4SGleb Smirnoff break; 262486a996e6SHiren Panchasara #ifdef TCPPCAP 262586a996e6SHiren Panchasara case TCP_PCAP_OUT: 262686a996e6SHiren Panchasara case TCP_PCAP_IN: 262786a996e6SHiren Panchasara optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ? 262886a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts)); 262986a996e6SHiren Panchasara INP_WUNLOCK(inp); 263086a996e6SHiren Panchasara error = sooptcopyout(sopt, &optval, sizeof optval); 263186a996e6SHiren Panchasara break; 263286a996e6SHiren Panchasara #endif 2633281a0fd4SPatrick Kelsey case TCP_FASTOPEN: 2634281a0fd4SPatrick Kelsey optval = tp->t_flags & TF_FASTOPEN; 2635281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 2636281a0fd4SPatrick Kelsey error = sooptcopyout(sopt, &optval, sizeof optval); 2637281a0fd4SPatrick Kelsey break; 2638e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX 26392529f56eSJonathan T. Looney case TCP_LOG: 26402529f56eSJonathan T. Looney optval = tp->t_logstate; 26412529f56eSJonathan T. Looney INP_WUNLOCK(inp); 26422529f56eSJonathan T. Looney error = sooptcopyout(sopt, &optval, sizeof(optval)); 26432529f56eSJonathan T. Looney break; 26442529f56eSJonathan T. Looney case TCP_LOGBUF: 26452529f56eSJonathan T. Looney /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */ 26462529f56eSJonathan T. Looney error = tcp_log_getlogbuf(sopt, tp); 26472529f56eSJonathan T. Looney break; 26482529f56eSJonathan T. Looney case TCP_LOGID: 26492529f56eSJonathan T. Looney len = tcp_log_get_id(tp, buf); 26502529f56eSJonathan T. Looney INP_WUNLOCK(inp); 26512529f56eSJonathan T. Looney error = sooptcopyout(sopt, buf, len + 1); 26522529f56eSJonathan T. Looney break; 26532529f56eSJonathan T. Looney case TCP_LOGDUMP: 26542529f56eSJonathan T. Looney case TCP_LOGDUMPID: 26552529f56eSJonathan T. Looney INP_WUNLOCK(inp); 26562529f56eSJonathan T. Looney error = EINVAL; 26572529f56eSJonathan T. Looney break; 2658e24e5683SJonathan T. Looney #endif 2659b2e60773SJohn Baldwin #ifdef KERN_TLS 2660b2e60773SJohn Baldwin case TCP_TXTLS_MODE: 2661bf256782SMark Johnston error = ktls_get_tx_mode(so, &optval); 2662b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2663bf256782SMark Johnston if (error == 0) 2664bf256782SMark Johnston error = sooptcopyout(sopt, &optval, 2665bf256782SMark Johnston sizeof(optval)); 2666b2e60773SJohn Baldwin break; 2667f1f93475SJohn Baldwin case TCP_RXTLS_MODE: 2668bf256782SMark Johnston error = ktls_get_rx_mode(so, &optval); 2669f1f93475SJohn Baldwin INP_WUNLOCK(inp); 2670bf256782SMark Johnston if (error == 0) 2671bf256782SMark Johnston error = sooptcopyout(sopt, &optval, 2672bf256782SMark Johnston sizeof(optval)); 2673f1f93475SJohn Baldwin break; 2674b2e60773SJohn Baldwin #endif 26750471a8c7SRichard Scheffenegger case TCP_LRD: 26760471a8c7SRichard Scheffenegger optval = tp->t_flags & TF_LRD; 26770471a8c7SRichard Scheffenegger INP_WUNLOCK(inp); 26780471a8c7SRichard Scheffenegger error = sooptcopyout(sopt, &optval, sizeof optval); 26790471a8c7SRichard Scheffenegger break; 2680df8bae1dSRodney W. Grimes default: 26818501a69cSRobert Watson INP_WUNLOCK(inp); 2682df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 2683df8bae1dSRodney W. Grimes break; 2684df8bae1dSRodney W. Grimes } 2685df8bae1dSRodney W. Grimes break; 2686df8bae1dSRodney W. Grimes } 2687df8bae1dSRodney W. Grimes return (error); 2688df8bae1dSRodney W. Grimes } 26898501a69cSRobert Watson #undef INP_WLOCK_RECHECK 2690bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP 2691df8bae1dSRodney W. Grimes 269226e30fbbSDavid Greenman /* 2693df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 2694df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 2695df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 2696df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 2697df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 2698df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 2699df8bae1dSRodney W. Grimes */ 2700623dce13SRobert Watson static void 2701ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp) 2702df8bae1dSRodney W. Grimes { 2703e6e0b5ffSRobert Watson struct inpcb *inp = tp->t_inpcb; 2704e6e0b5ffSRobert Watson struct socket *so = inp->inp_socket; 2705e6e0b5ffSRobert Watson 270697a95ee1SGleb Smirnoff NET_EPOCH_ASSERT(); 27078501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2708df8bae1dSRodney W. Grimes 2709623dce13SRobert Watson /* 2710623dce13SRobert Watson * Neither tcp_close() nor tcp_drop() should return NULL, as the 2711623dce13SRobert Watson * socket is still open. 2712623dce13SRobert Watson */ 27138db239dcSMichael Tuexen if (tp->t_state < TCPS_ESTABLISHED && 27148db239dcSMichael Tuexen !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) { 2715df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2716623dce13SRobert Watson KASSERT(tp != NULL, 2717623dce13SRobert Watson ("tcp_disconnect: tcp_close() returned NULL")); 2718623dce13SRobert Watson } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 2719243917feSSeigo Tanimura tp = tcp_drop(tp, 0); 2720623dce13SRobert Watson KASSERT(tp != NULL, 2721623dce13SRobert Watson ("tcp_disconnect: tcp_drop() returned NULL")); 2722623dce13SRobert Watson } else { 2723df8bae1dSRodney W. Grimes soisdisconnecting(so); 2724df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 2725623dce13SRobert Watson tcp_usrclosed(tp); 2726ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 272755bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 2728df8bae1dSRodney W. Grimes } 2729df8bae1dSRodney W. Grimes } 2730df8bae1dSRodney W. Grimes 2731df8bae1dSRodney W. Grimes /* 2732df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 2733df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 2734df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 2735df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 2736df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 2737df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 2738df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 2739df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 2740df8bae1dSRodney W. Grimes */ 2741623dce13SRobert Watson static void 2742ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp) 2743df8bae1dSRodney W. Grimes { 2744df8bae1dSRodney W. Grimes 274597a95ee1SGleb Smirnoff NET_EPOCH_ASSERT(); 27468501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 2747e6e0b5ffSRobert Watson 2748df8bae1dSRodney W. Grimes switch (tp->t_state) { 2749df8bae1dSRodney W. Grimes case TCPS_LISTEN: 275009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 275109fe6320SNavdeep Parhar tcp_offload_listen_stop(tp); 275209fe6320SNavdeep Parhar #endif 2753550e9d42SHiren Panchasara tcp_state_change(tp, TCPS_CLOSED); 2754bc65987aSKip Macy /* FALLTHROUGH */ 2755bc65987aSKip Macy case TCPS_CLOSED: 2756df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2757623dce13SRobert Watson /* 2758623dce13SRobert Watson * tcp_close() should never return NULL here as the socket is 2759623dce13SRobert Watson * still open. 2760623dce13SRobert Watson */ 2761623dce13SRobert Watson KASSERT(tp != NULL, 2762623dce13SRobert Watson ("tcp_usrclosed: tcp_close() returned NULL")); 2763df8bae1dSRodney W. Grimes break; 2764df8bae1dSRodney W. Grimes 2765a0292f23SGarrett Wollman case TCPS_SYN_SENT: 2766df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2767a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 2768a0292f23SGarrett Wollman break; 2769a0292f23SGarrett Wollman 2770df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 277157f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2772df8bae1dSRodney W. Grimes break; 2773df8bae1dSRodney W. Grimes 2774df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 277557f60867SMark Johnston tcp_state_change(tp, TCPS_LAST_ACK); 2776df8bae1dSRodney W. Grimes break; 2777df8bae1dSRodney W. Grimes } 2778abc7d910SRobert Watson if (tp->t_state >= TCPS_FIN_WAIT_2) { 2779df8bae1dSRodney W. Grimes soisdisconnected(tp->t_inpcb->inp_socket); 2780abc7d910SRobert Watson /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 27817c72af87SMohan Srinivasan if (tp->t_state == TCPS_FIN_WAIT_2) { 27827c72af87SMohan Srinivasan int timeout; 27837c72af87SMohan Srinivasan 27847c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 27859077f387SGleb Smirnoff tcp_finwait2_timeout : TP_MAXIDLE(tp); 2786b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 2787b6239c4aSAndras Olah } 2788df8bae1dSRodney W. Grimes } 27897c72af87SMohan Srinivasan } 2790497057eeSRobert Watson 2791497057eeSRobert Watson #ifdef DDB 2792497057eeSRobert Watson static void 2793497057eeSRobert Watson db_print_indent(int indent) 2794497057eeSRobert Watson { 2795497057eeSRobert Watson int i; 2796497057eeSRobert Watson 2797497057eeSRobert Watson for (i = 0; i < indent; i++) 2798497057eeSRobert Watson db_printf(" "); 2799497057eeSRobert Watson } 2800497057eeSRobert Watson 2801497057eeSRobert Watson static void 2802497057eeSRobert Watson db_print_tstate(int t_state) 2803497057eeSRobert Watson { 2804497057eeSRobert Watson 2805497057eeSRobert Watson switch (t_state) { 2806497057eeSRobert Watson case TCPS_CLOSED: 2807497057eeSRobert Watson db_printf("TCPS_CLOSED"); 2808497057eeSRobert Watson return; 2809497057eeSRobert Watson 2810497057eeSRobert Watson case TCPS_LISTEN: 2811497057eeSRobert Watson db_printf("TCPS_LISTEN"); 2812497057eeSRobert Watson return; 2813497057eeSRobert Watson 2814497057eeSRobert Watson case TCPS_SYN_SENT: 2815497057eeSRobert Watson db_printf("TCPS_SYN_SENT"); 2816497057eeSRobert Watson return; 2817497057eeSRobert Watson 2818497057eeSRobert Watson case TCPS_SYN_RECEIVED: 2819497057eeSRobert Watson db_printf("TCPS_SYN_RECEIVED"); 2820497057eeSRobert Watson return; 2821497057eeSRobert Watson 2822497057eeSRobert Watson case TCPS_ESTABLISHED: 2823497057eeSRobert Watson db_printf("TCPS_ESTABLISHED"); 2824497057eeSRobert Watson return; 2825497057eeSRobert Watson 2826497057eeSRobert Watson case TCPS_CLOSE_WAIT: 2827497057eeSRobert Watson db_printf("TCPS_CLOSE_WAIT"); 2828497057eeSRobert Watson return; 2829497057eeSRobert Watson 2830497057eeSRobert Watson case TCPS_FIN_WAIT_1: 2831497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_1"); 2832497057eeSRobert Watson return; 2833497057eeSRobert Watson 2834497057eeSRobert Watson case TCPS_CLOSING: 2835497057eeSRobert Watson db_printf("TCPS_CLOSING"); 2836497057eeSRobert Watson return; 2837497057eeSRobert Watson 2838497057eeSRobert Watson case TCPS_LAST_ACK: 2839497057eeSRobert Watson db_printf("TCPS_LAST_ACK"); 2840497057eeSRobert Watson return; 2841497057eeSRobert Watson 2842497057eeSRobert Watson case TCPS_FIN_WAIT_2: 2843497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_2"); 2844497057eeSRobert Watson return; 2845497057eeSRobert Watson 2846497057eeSRobert Watson case TCPS_TIME_WAIT: 2847497057eeSRobert Watson db_printf("TCPS_TIME_WAIT"); 2848497057eeSRobert Watson return; 2849497057eeSRobert Watson 2850497057eeSRobert Watson default: 2851497057eeSRobert Watson db_printf("unknown"); 2852497057eeSRobert Watson return; 2853497057eeSRobert Watson } 2854497057eeSRobert Watson } 2855497057eeSRobert Watson 2856497057eeSRobert Watson static void 2857497057eeSRobert Watson db_print_tflags(u_int t_flags) 2858497057eeSRobert Watson { 2859497057eeSRobert Watson int comma; 2860497057eeSRobert Watson 2861497057eeSRobert Watson comma = 0; 2862497057eeSRobert Watson if (t_flags & TF_ACKNOW) { 2863497057eeSRobert Watson db_printf("%sTF_ACKNOW", comma ? ", " : ""); 2864497057eeSRobert Watson comma = 1; 2865497057eeSRobert Watson } 2866497057eeSRobert Watson if (t_flags & TF_DELACK) { 2867497057eeSRobert Watson db_printf("%sTF_DELACK", comma ? ", " : ""); 2868497057eeSRobert Watson comma = 1; 2869497057eeSRobert Watson } 2870497057eeSRobert Watson if (t_flags & TF_NODELAY) { 2871497057eeSRobert Watson db_printf("%sTF_NODELAY", comma ? ", " : ""); 2872497057eeSRobert Watson comma = 1; 2873497057eeSRobert Watson } 2874497057eeSRobert Watson if (t_flags & TF_NOOPT) { 2875497057eeSRobert Watson db_printf("%sTF_NOOPT", comma ? ", " : ""); 2876497057eeSRobert Watson comma = 1; 2877497057eeSRobert Watson } 2878497057eeSRobert Watson if (t_flags & TF_SENTFIN) { 2879497057eeSRobert Watson db_printf("%sTF_SENTFIN", comma ? ", " : ""); 2880497057eeSRobert Watson comma = 1; 2881497057eeSRobert Watson } 2882497057eeSRobert Watson if (t_flags & TF_REQ_SCALE) { 2883497057eeSRobert Watson db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 2884497057eeSRobert Watson comma = 1; 2885497057eeSRobert Watson } 2886497057eeSRobert Watson if (t_flags & TF_RCVD_SCALE) { 2887497057eeSRobert Watson db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 2888497057eeSRobert Watson comma = 1; 2889497057eeSRobert Watson } 2890497057eeSRobert Watson if (t_flags & TF_REQ_TSTMP) { 2891497057eeSRobert Watson db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 2892497057eeSRobert Watson comma = 1; 2893497057eeSRobert Watson } 2894497057eeSRobert Watson if (t_flags & TF_RCVD_TSTMP) { 2895497057eeSRobert Watson db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 2896497057eeSRobert Watson comma = 1; 2897497057eeSRobert Watson } 2898497057eeSRobert Watson if (t_flags & TF_SACK_PERMIT) { 2899497057eeSRobert Watson db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 2900497057eeSRobert Watson comma = 1; 2901497057eeSRobert Watson } 2902497057eeSRobert Watson if (t_flags & TF_NEEDSYN) { 2903497057eeSRobert Watson db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 2904497057eeSRobert Watson comma = 1; 2905497057eeSRobert Watson } 2906497057eeSRobert Watson if (t_flags & TF_NEEDFIN) { 2907497057eeSRobert Watson db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 2908497057eeSRobert Watson comma = 1; 2909497057eeSRobert Watson } 2910497057eeSRobert Watson if (t_flags & TF_NOPUSH) { 2911497057eeSRobert Watson db_printf("%sTF_NOPUSH", comma ? ", " : ""); 2912497057eeSRobert Watson comma = 1; 2913497057eeSRobert Watson } 2914497057eeSRobert Watson if (t_flags & TF_MORETOCOME) { 2915497057eeSRobert Watson db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 2916497057eeSRobert Watson comma = 1; 2917497057eeSRobert Watson } 2918497057eeSRobert Watson if (t_flags & TF_LQ_OVERFLOW) { 2919497057eeSRobert Watson db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : ""); 2920497057eeSRobert Watson comma = 1; 2921497057eeSRobert Watson } 2922497057eeSRobert Watson if (t_flags & TF_LASTIDLE) { 2923497057eeSRobert Watson db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 2924497057eeSRobert Watson comma = 1; 2925497057eeSRobert Watson } 2926497057eeSRobert Watson if (t_flags & TF_RXWIN0SENT) { 2927497057eeSRobert Watson db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 2928497057eeSRobert Watson comma = 1; 2929497057eeSRobert Watson } 2930497057eeSRobert Watson if (t_flags & TF_FASTRECOVERY) { 2931497057eeSRobert Watson db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 2932497057eeSRobert Watson comma = 1; 2933497057eeSRobert Watson } 2934dbc42409SLawrence Stewart if (t_flags & TF_CONGRECOVERY) { 2935dbc42409SLawrence Stewart db_printf("%sTF_CONGRECOVERY", comma ? ", " : ""); 2936dbc42409SLawrence Stewart comma = 1; 2937dbc42409SLawrence Stewart } 2938497057eeSRobert Watson if (t_flags & TF_WASFRECOVERY) { 2939497057eeSRobert Watson db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 2940497057eeSRobert Watson comma = 1; 2941497057eeSRobert Watson } 2942497057eeSRobert Watson if (t_flags & TF_SIGNATURE) { 2943497057eeSRobert Watson db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 2944497057eeSRobert Watson comma = 1; 2945497057eeSRobert Watson } 2946497057eeSRobert Watson if (t_flags & TF_FORCEDATA) { 2947497057eeSRobert Watson db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 2948497057eeSRobert Watson comma = 1; 2949497057eeSRobert Watson } 2950497057eeSRobert Watson if (t_flags & TF_TSO) { 2951497057eeSRobert Watson db_printf("%sTF_TSO", comma ? ", " : ""); 2952497057eeSRobert Watson comma = 1; 2953497057eeSRobert Watson } 2954281a0fd4SPatrick Kelsey if (t_flags & TF_FASTOPEN) { 2955281a0fd4SPatrick Kelsey db_printf("%sTF_FASTOPEN", comma ? ", " : ""); 2956281a0fd4SPatrick Kelsey comma = 1; 2957281a0fd4SPatrick Kelsey } 2958497057eeSRobert Watson } 2959497057eeSRobert Watson 2960497057eeSRobert Watson static void 29613cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2) 29623cf38784SMichael Tuexen { 29633cf38784SMichael Tuexen int comma; 29643cf38784SMichael Tuexen 29653cf38784SMichael Tuexen comma = 0; 29663cf38784SMichael Tuexen if (t_flags2 & TF2_ECN_PERMIT) { 29673cf38784SMichael Tuexen db_printf("%sTF2_ECN_PERMIT", comma ? ", " : ""); 29683cf38784SMichael Tuexen comma = 1; 29693cf38784SMichael Tuexen } 29703cf38784SMichael Tuexen } 29713cf38784SMichael Tuexen 29723cf38784SMichael Tuexen static void 2973497057eeSRobert Watson db_print_toobflags(char t_oobflags) 2974497057eeSRobert Watson { 2975497057eeSRobert Watson int comma; 2976497057eeSRobert Watson 2977497057eeSRobert Watson comma = 0; 2978497057eeSRobert Watson if (t_oobflags & TCPOOB_HAVEDATA) { 2979497057eeSRobert Watson db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 2980497057eeSRobert Watson comma = 1; 2981497057eeSRobert Watson } 2982497057eeSRobert Watson if (t_oobflags & TCPOOB_HADDATA) { 2983497057eeSRobert Watson db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 2984497057eeSRobert Watson comma = 1; 2985497057eeSRobert Watson } 2986497057eeSRobert Watson } 2987497057eeSRobert Watson 2988497057eeSRobert Watson static void 2989497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 2990497057eeSRobert Watson { 2991497057eeSRobert Watson 2992497057eeSRobert Watson db_print_indent(indent); 2993497057eeSRobert Watson db_printf("%s at %p\n", name, tp); 2994497057eeSRobert Watson 2995497057eeSRobert Watson indent += 2; 2996497057eeSRobert Watson 2997497057eeSRobert Watson db_print_indent(indent); 2998497057eeSRobert Watson db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 2999c28440dbSRandall Stewart TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 3000497057eeSRobert Watson 3001497057eeSRobert Watson db_print_indent(indent); 300285d94372SRobert Watson db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n", 3003e2f2059fSMike Silbersack &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep); 3004497057eeSRobert Watson 3005497057eeSRobert Watson db_print_indent(indent); 3006e2f2059fSMike Silbersack db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl, 3007e2f2059fSMike Silbersack &tp->t_timers->tt_delack, tp->t_inpcb); 3008497057eeSRobert Watson 3009497057eeSRobert Watson db_print_indent(indent); 3010497057eeSRobert Watson db_printf("t_state: %d (", tp->t_state); 3011497057eeSRobert Watson db_print_tstate(tp->t_state); 3012497057eeSRobert Watson db_printf(")\n"); 3013497057eeSRobert Watson 3014497057eeSRobert Watson db_print_indent(indent); 3015497057eeSRobert Watson db_printf("t_flags: 0x%x (", tp->t_flags); 3016497057eeSRobert Watson db_print_tflags(tp->t_flags); 3017497057eeSRobert Watson db_printf(")\n"); 3018497057eeSRobert Watson 3019497057eeSRobert Watson db_print_indent(indent); 30203cf38784SMichael Tuexen db_printf("t_flags2: 0x%x (", tp->t_flags2); 30213cf38784SMichael Tuexen db_print_tflags2(tp->t_flags2); 30223cf38784SMichael Tuexen db_printf(")\n"); 30233cf38784SMichael Tuexen 30243cf38784SMichael Tuexen db_print_indent(indent); 3025497057eeSRobert Watson db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n", 3026497057eeSRobert Watson tp->snd_una, tp->snd_max, tp->snd_nxt); 3027497057eeSRobert Watson 3028497057eeSRobert Watson db_print_indent(indent); 3029497057eeSRobert Watson db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 3030497057eeSRobert Watson tp->snd_up, tp->snd_wl1, tp->snd_wl2); 3031497057eeSRobert Watson 3032497057eeSRobert Watson db_print_indent(indent); 3033497057eeSRobert Watson db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 3034497057eeSRobert Watson tp->iss, tp->irs, tp->rcv_nxt); 3035497057eeSRobert Watson 3036497057eeSRobert Watson db_print_indent(indent); 30373ac12506SJonathan T. Looney db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n", 3038497057eeSRobert Watson tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 3039497057eeSRobert Watson 3040497057eeSRobert Watson db_print_indent(indent); 30413ac12506SJonathan T. Looney db_printf("snd_wnd: %u snd_cwnd: %u\n", 30421c18314dSAndre Oppermann tp->snd_wnd, tp->snd_cwnd); 3043497057eeSRobert Watson 3044497057eeSRobert Watson db_print_indent(indent); 30453ac12506SJonathan T. Looney db_printf("snd_ssthresh: %u snd_recover: " 30461c18314dSAndre Oppermann "0x%08x\n", tp->snd_ssthresh, tp->snd_recover); 3047497057eeSRobert Watson 3048497057eeSRobert Watson db_print_indent(indent); 30490c39d38dSGleb Smirnoff db_printf("t_rcvtime: %u t_startime: %u\n", 30500c39d38dSGleb Smirnoff tp->t_rcvtime, tp->t_starttime); 3051497057eeSRobert Watson 3052497057eeSRobert Watson db_print_indent(indent); 30531c18314dSAndre Oppermann db_printf("t_rttime: %u t_rtsq: 0x%08x\n", 30541c18314dSAndre Oppermann tp->t_rtttime, tp->t_rtseq); 3055497057eeSRobert Watson 3056497057eeSRobert Watson db_print_indent(indent); 30571c18314dSAndre Oppermann db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n", 30581c18314dSAndre Oppermann tp->t_rxtcur, tp->t_maxseg, tp->t_srtt); 3059497057eeSRobert Watson 3060497057eeSRobert Watson db_print_indent(indent); 3061497057eeSRobert Watson db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u " 3062497057eeSRobert Watson "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin, 3063497057eeSRobert Watson tp->t_rttbest); 3064497057eeSRobert Watson 3065497057eeSRobert Watson db_print_indent(indent); 30663ac12506SJonathan T. Looney db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n", 3067497057eeSRobert Watson tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 3068497057eeSRobert Watson 3069497057eeSRobert Watson db_print_indent(indent); 3070497057eeSRobert Watson db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 3071497057eeSRobert Watson db_print_toobflags(tp->t_oobflags); 3072497057eeSRobert Watson db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 3073497057eeSRobert Watson 3074497057eeSRobert Watson db_print_indent(indent); 3075497057eeSRobert Watson db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 3076497057eeSRobert Watson tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 3077497057eeSRobert Watson 3078497057eeSRobert Watson db_print_indent(indent); 30799f78a87aSJohn Baldwin db_printf("ts_recent: %u ts_recent_age: %u\n", 30801a553740SAndre Oppermann tp->ts_recent, tp->ts_recent_age); 3081497057eeSRobert Watson 3082497057eeSRobert Watson db_print_indent(indent); 3083497057eeSRobert Watson db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 30843ac12506SJonathan T. Looney "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 3085497057eeSRobert Watson 3086497057eeSRobert Watson db_print_indent(indent); 30873ac12506SJonathan T. Looney db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x " 30889f78a87aSJohn Baldwin "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 3089497057eeSRobert Watson tp->snd_recover_prev, tp->t_badrxtwin); 3090497057eeSRobert Watson 3091497057eeSRobert Watson db_print_indent(indent); 30923529149eSAndre Oppermann db_printf("snd_numholes: %d snd_holes first: %p\n", 30933529149eSAndre Oppermann tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 3094497057eeSRobert Watson 3095497057eeSRobert Watson db_print_indent(indent); 3096a3574665SMichael Tuexen db_printf("snd_fack: 0x%08x rcv_numsacks: %d\n", 3097a3574665SMichael Tuexen tp->snd_fack, tp->rcv_numsacks); 3098497057eeSRobert Watson 3099497057eeSRobert Watson /* Skip sackblks, sackhint. */ 3100497057eeSRobert Watson 3101497057eeSRobert Watson db_print_indent(indent); 3102497057eeSRobert Watson db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 3103497057eeSRobert Watson tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 3104497057eeSRobert Watson } 3105497057eeSRobert Watson 3106497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 3107497057eeSRobert Watson { 3108497057eeSRobert Watson struct tcpcb *tp; 3109497057eeSRobert Watson 3110497057eeSRobert Watson if (!have_addr) { 3111497057eeSRobert Watson db_printf("usage: show tcpcb <addr>\n"); 3112497057eeSRobert Watson return; 3113497057eeSRobert Watson } 3114497057eeSRobert Watson tp = (struct tcpcb *)addr; 3115497057eeSRobert Watson 3116497057eeSRobert Watson db_print_tcpcb(tp, "tcpcb", 0); 3117497057eeSRobert Watson } 3118497057eeSRobert Watson #endif 3119