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 if ((so->so_options & SO_LINGER) && so->so_linger == 0) 2083879597fSAndrey A. Chernov so->so_linger = TCP_LINGERTIME; 2092c37256eSGarrett Wollman out: 2102c37256eSGarrett Wollman TCPDEBUG2(PRU_ATTACH); 2115d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ATTACH); 2120f6385e7SGleb Smirnoff return (error); 2132c37256eSGarrett Wollman } 2142c37256eSGarrett Wollman 2152c37256eSGarrett Wollman /* 2163fed74e9SGleb Smirnoff * tcp_usr_detach is called when the socket layer loses its final reference 217a152f8a3SRobert Watson * to the socket, be it a file descriptor reference, a reference from TCP, 218a152f8a3SRobert Watson * etc. At this point, there is only one case in which we will keep around 219a152f8a3SRobert Watson * inpcb state: time wait. 2202c37256eSGarrett Wollman */ 221bc725eafSRobert Watson static void 2223fed74e9SGleb Smirnoff tcp_usr_detach(struct socket *so) 2232c37256eSGarrett Wollman { 2243fed74e9SGleb Smirnoff struct inpcb *inp; 2252c37256eSGarrett Wollman struct tcpcb *tp; 2262c37256eSGarrett Wollman 2273fed74e9SGleb Smirnoff inp = sotoinpcb(so); 2283fed74e9SGleb Smirnoff KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 2293fed74e9SGleb Smirnoff INP_WLOCK(inp); 2303fed74e9SGleb Smirnoff KASSERT(so->so_pcb == inp && inp->inp_socket == so, 2313fed74e9SGleb Smirnoff ("%s: socket %p inp %p mismatch", __func__, so, inp)); 232953b5606SRobert Watson 233a152f8a3SRobert Watson tp = intotcpcb(inp); 234a152f8a3SRobert Watson 235ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 236623dce13SRobert Watson /* 237a152f8a3SRobert Watson * There are two cases to handle: one in which the time wait 238a152f8a3SRobert Watson * state is being discarded (INP_DROPPED), and one in which 239a152f8a3SRobert Watson * this connection will remain in timewait. In the former, 240a152f8a3SRobert Watson * it is time to discard all state (except tcptw, which has 241a152f8a3SRobert Watson * already been discarded by the timewait close code, which 242a152f8a3SRobert Watson * should be further up the call stack somewhere). In the 243a152f8a3SRobert Watson * latter case, we detach from the socket, but leave the pcb 244a152f8a3SRobert Watson * present until timewait ends. 245623dce13SRobert Watson * 246a152f8a3SRobert Watson * XXXRW: Would it be cleaner to free the tcptw here? 247cea40c48SJulien Charbon * 248cea40c48SJulien Charbon * Astute question indeed, from twtcp perspective there are 249dd388cfdSGleb Smirnoff * four cases to consider: 250cea40c48SJulien Charbon * 2513fed74e9SGleb Smirnoff * #1 tcp_usr_detach is called at tcptw creation time by 252cea40c48SJulien Charbon * tcp_twstart, then do not discard the newly created tcptw 253cea40c48SJulien Charbon * and leave inpcb present until timewait ends 2543fed74e9SGleb Smirnoff * #2 tcp_usr_detach is called at tcptw creation time by 255dd388cfdSGleb Smirnoff * tcp_twstart, but connection is local and tw will be 256dd388cfdSGleb Smirnoff * discarded immediately 2573fed74e9SGleb Smirnoff * #3 tcp_usr_detach is called at timewait end (or reuse) by 258cea40c48SJulien Charbon * tcp_twclose, then the tcptw has already been discarded 259ff9b006dSJulien Charbon * (or reused) and inpcb is freed here 2603fed74e9SGleb Smirnoff * #4 tcp_usr_detach is called() after timewait ends (or reuse) 261cea40c48SJulien Charbon * (e.g. by soclose), then tcptw has already been discarded 262ff9b006dSJulien Charbon * (or reused) and inpcb is freed here 263cea40c48SJulien Charbon * 264cea40c48SJulien Charbon * In all three cases the tcptw should not be freed here. 265623dce13SRobert Watson */ 266ad71fe3cSRobert Watson if (inp->inp_flags & INP_DROPPED) { 267623dce13SRobert Watson in_pcbdetach(inp); 268f5cf1e5fSJulien Charbon if (__predict_true(tp == NULL)) { 2690206cdb8SBjoern A. Zeeb in_pcbfree(inp); 2700206cdb8SBjoern A. Zeeb } else { 271f5cf1e5fSJulien Charbon /* 272f5cf1e5fSJulien Charbon * This case should not happen as in TIMEWAIT 273f5cf1e5fSJulien Charbon * state the inp should not be destroyed before 274f5cf1e5fSJulien Charbon * its tcptw. If INVARIANTS is defined, panic. 275f5cf1e5fSJulien Charbon */ 276f5cf1e5fSJulien Charbon #ifdef INVARIANTS 277f5cf1e5fSJulien Charbon panic("%s: Panic before an inp double-free: " 278f5cf1e5fSJulien Charbon "INP_TIMEWAIT && INP_DROPPED && tp != NULL" 279f5cf1e5fSJulien Charbon , __func__); 280f5cf1e5fSJulien Charbon #else 281f5cf1e5fSJulien Charbon log(LOG_ERR, "%s: Avoid an inp double-free: " 282f5cf1e5fSJulien Charbon "INP_TIMEWAIT && INP_DROPPED && tp != NULL" 283f5cf1e5fSJulien Charbon , __func__); 284f5cf1e5fSJulien Charbon #endif 285f5cf1e5fSJulien Charbon INP_WUNLOCK(inp); 286f5cf1e5fSJulien Charbon } 287f5cf1e5fSJulien Charbon } else { 288623dce13SRobert Watson in_pcbdetach(inp); 2898501a69cSRobert Watson INP_WUNLOCK(inp); 290623dce13SRobert Watson } 291623dce13SRobert Watson } else { 292e6e65783SRobert Watson /* 293a152f8a3SRobert Watson * If the connection is not in timewait, we consider two 294a152f8a3SRobert Watson * two conditions: one in which no further processing is 295a152f8a3SRobert Watson * necessary (dropped || embryonic), and one in which TCP is 296a152f8a3SRobert Watson * not yet done, but no longer requires the socket, so the 297a152f8a3SRobert Watson * pcb will persist for the time being. 298a152f8a3SRobert Watson * 299a152f8a3SRobert Watson * XXXRW: Does the second case still occur? 300e6e65783SRobert Watson */ 301ad71fe3cSRobert Watson if (inp->inp_flags & INP_DROPPED || 302623dce13SRobert Watson tp->t_state < TCPS_SYN_SENT) { 303623dce13SRobert Watson tcp_discardcb(tp); 304623dce13SRobert Watson in_pcbdetach(inp); 3050206cdb8SBjoern A. Zeeb in_pcbfree(inp); 306db3cee51SNavdeep Parhar } else { 307a152f8a3SRobert Watson in_pcbdetach(inp); 308db3cee51SNavdeep Parhar INP_WUNLOCK(inp); 309db3cee51SNavdeep Parhar } 310623dce13SRobert Watson } 311623dce13SRobert Watson } 312c78cbc7bSRobert Watson 313b287c6c7SBjoern A. Zeeb #ifdef INET 3142c37256eSGarrett Wollman /* 3152c37256eSGarrett Wollman * Give the socket an address. 3162c37256eSGarrett Wollman */ 3172c37256eSGarrett Wollman static int 318b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 3192c37256eSGarrett Wollman { 3202c37256eSGarrett Wollman int error = 0; 321f76fcf6dSJeffrey Hsu struct inpcb *inp; 322623dce13SRobert Watson struct tcpcb *tp = NULL; 3232c37256eSGarrett Wollman struct sockaddr_in *sinp; 3242c37256eSGarrett Wollman 32552710de1SPawel Jakub Dawidek sinp = (struct sockaddr_in *)nam; 32652710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof (*sinp)) 32752710de1SPawel Jakub Dawidek return (EINVAL); 3282c37256eSGarrett Wollman /* 3292c37256eSGarrett Wollman * Must check for multicast addresses and disallow binding 3302c37256eSGarrett Wollman * to them. 3312c37256eSGarrett Wollman */ 3322c37256eSGarrett Wollman if (sinp->sin_family == AF_INET && 33352710de1SPawel Jakub Dawidek IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 33452710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 33552710de1SPawel Jakub Dawidek 336623dce13SRobert Watson TCPDEBUG0; 337623dce13SRobert Watson inp = sotoinpcb(so); 338623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 3398501a69cSRobert Watson INP_WLOCK(inp); 340ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 341623dce13SRobert Watson error = EINVAL; 3422c37256eSGarrett Wollman goto out; 343623dce13SRobert Watson } 344623dce13SRobert Watson tp = intotcpcb(inp); 345623dce13SRobert Watson TCPDEBUG1(); 346fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 347623dce13SRobert Watson error = in_pcbbind(inp, nam, td->td_ucred); 348fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 349623dce13SRobert Watson out: 350623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 3515d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 3528501a69cSRobert Watson INP_WUNLOCK(inp); 353623dce13SRobert Watson 354623dce13SRobert Watson return (error); 3552c37256eSGarrett Wollman } 356b287c6c7SBjoern A. Zeeb #endif /* INET */ 3572c37256eSGarrett Wollman 358fb59c426SYoshinobu Inoue #ifdef INET6 359fb59c426SYoshinobu Inoue static int 360b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 361fb59c426SYoshinobu Inoue { 362fb59c426SYoshinobu Inoue int error = 0; 363f76fcf6dSJeffrey Hsu struct inpcb *inp; 364623dce13SRobert Watson struct tcpcb *tp = NULL; 3650ecd976eSBjoern A. Zeeb struct sockaddr_in6 *sin6; 3664a91aa8fSMichael Tuexen u_char vflagsav; 367fb59c426SYoshinobu Inoue 3680ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 3690ecd976eSBjoern A. Zeeb if (nam->sa_len != sizeof (*sin6)) 37052710de1SPawel Jakub Dawidek return (EINVAL); 371fb59c426SYoshinobu Inoue /* 372fb59c426SYoshinobu Inoue * Must check for multicast addresses and disallow binding 373fb59c426SYoshinobu Inoue * to them. 374fb59c426SYoshinobu Inoue */ 3750ecd976eSBjoern A. Zeeb if (sin6->sin6_family == AF_INET6 && 3760ecd976eSBjoern A. Zeeb IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 37752710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 37852710de1SPawel Jakub Dawidek 379623dce13SRobert Watson TCPDEBUG0; 380623dce13SRobert Watson inp = sotoinpcb(so); 381623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 3828501a69cSRobert Watson INP_WLOCK(inp); 3834a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 384ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 385623dce13SRobert Watson error = EINVAL; 386623dce13SRobert Watson goto out; 387623dce13SRobert Watson } 388623dce13SRobert Watson tp = intotcpcb(inp); 389623dce13SRobert Watson TCPDEBUG1(); 390fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 391fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 392fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 393b287c6c7SBjoern A. Zeeb #ifdef INET 39466ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 3950ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 396fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 3970ecd976eSBjoern A. Zeeb else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 398fb59c426SYoshinobu Inoue struct sockaddr_in sin; 399fb59c426SYoshinobu Inoue 4000ecd976eSBjoern A. Zeeb in6_sin6_2_sin(&sin, sin6); 401888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 402888973f5SMichael Tuexen error = EAFNOSUPPORT; 403888973f5SMichael Tuexen INP_HASH_WUNLOCK(&V_tcbinfo); 404888973f5SMichael Tuexen goto out; 405888973f5SMichael Tuexen } 406fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 407fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 408b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)&sin, 409b0330ed9SPawel Jakub Dawidek td->td_ucred); 410fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 411fb59c426SYoshinobu Inoue goto out; 412fb59c426SYoshinobu Inoue } 413fb59c426SYoshinobu Inoue } 414b287c6c7SBjoern A. Zeeb #endif 415b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, nam, td->td_ucred); 416fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 417623dce13SRobert Watson out: 4184a91aa8fSMichael Tuexen if (error != 0) 4194a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 420623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 4215d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 4228501a69cSRobert Watson INP_WUNLOCK(inp); 423623dce13SRobert Watson return (error); 424fb59c426SYoshinobu Inoue } 425fb59c426SYoshinobu Inoue #endif /* INET6 */ 426fb59c426SYoshinobu Inoue 427b287c6c7SBjoern A. Zeeb #ifdef INET 4282c37256eSGarrett Wollman /* 4292c37256eSGarrett Wollman * Prepare to accept connections. 4302c37256eSGarrett Wollman */ 4312c37256eSGarrett Wollman static int 432d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td) 4332c37256eSGarrett Wollman { 4342c37256eSGarrett Wollman int error = 0; 435f76fcf6dSJeffrey Hsu struct inpcb *inp; 436623dce13SRobert Watson struct tcpcb *tp = NULL; 4372c37256eSGarrett Wollman 438623dce13SRobert Watson TCPDEBUG0; 439623dce13SRobert Watson inp = sotoinpcb(so); 440623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 4418501a69cSRobert Watson INP_WLOCK(inp); 442ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 443623dce13SRobert Watson error = EINVAL; 444623dce13SRobert Watson goto out; 445623dce13SRobert Watson } 446623dce13SRobert Watson tp = intotcpcb(inp); 447623dce13SRobert Watson TCPDEBUG1(); 4480daccb9cSRobert Watson SOCK_LOCK(so); 4490daccb9cSRobert Watson error = solisten_proto_check(so); 450fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 4510daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) 452b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 453fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 4540daccb9cSRobert Watson if (error == 0) { 45557f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 456d374e81eSRobert Watson solisten_proto(so, backlog); 45709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 45837cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 45909fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 46009fe6320SNavdeep Parhar #endif 4610daccb9cSRobert Watson } 4620daccb9cSRobert Watson SOCK_UNLOCK(so); 463623dce13SRobert Watson 46468bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) 465281a0fd4SPatrick Kelsey tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 46618a75309SPatrick Kelsey 467623dce13SRobert Watson out: 468623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 4695d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 4708501a69cSRobert Watson INP_WUNLOCK(inp); 471623dce13SRobert Watson return (error); 4722c37256eSGarrett Wollman } 473b287c6c7SBjoern A. Zeeb #endif /* INET */ 4742c37256eSGarrett Wollman 475fb59c426SYoshinobu Inoue #ifdef INET6 476fb59c426SYoshinobu Inoue static int 477d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 478fb59c426SYoshinobu Inoue { 479fb59c426SYoshinobu Inoue int error = 0; 480f76fcf6dSJeffrey Hsu struct inpcb *inp; 481623dce13SRobert Watson struct tcpcb *tp = NULL; 4824a91aa8fSMichael Tuexen u_char vflagsav; 483fb59c426SYoshinobu Inoue 484623dce13SRobert Watson TCPDEBUG0; 485623dce13SRobert Watson inp = sotoinpcb(so); 486623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL")); 4878501a69cSRobert Watson INP_WLOCK(inp); 488ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 489623dce13SRobert Watson error = EINVAL; 490623dce13SRobert Watson goto out; 491623dce13SRobert Watson } 4924a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 493623dce13SRobert Watson tp = intotcpcb(inp); 494623dce13SRobert Watson TCPDEBUG1(); 4950daccb9cSRobert Watson SOCK_LOCK(so); 4960daccb9cSRobert Watson error = solisten_proto_check(so); 497fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 4980daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) { 499fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 50066ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 501fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 502b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 503fb59c426SYoshinobu Inoue } 504fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 5050daccb9cSRobert Watson if (error == 0) { 50657f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 507d374e81eSRobert Watson solisten_proto(so, backlog); 50809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 50937cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 51009fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 51109fe6320SNavdeep Parhar #endif 5120daccb9cSRobert Watson } 5130daccb9cSRobert Watson SOCK_UNLOCK(so); 514623dce13SRobert Watson 51568bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) 516281a0fd4SPatrick Kelsey tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 51718a75309SPatrick Kelsey 5184a91aa8fSMichael Tuexen if (error != 0) 5194a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 5204a91aa8fSMichael Tuexen 521623dce13SRobert Watson out: 522623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 5235d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 5248501a69cSRobert Watson INP_WUNLOCK(inp); 525623dce13SRobert Watson return (error); 526fb59c426SYoshinobu Inoue } 527fb59c426SYoshinobu Inoue #endif /* INET6 */ 528fb59c426SYoshinobu Inoue 529b287c6c7SBjoern A. Zeeb #ifdef INET 5302c37256eSGarrett Wollman /* 5312c37256eSGarrett Wollman * Initiate connection to peer. 5322c37256eSGarrett Wollman * Create a template for use in transmissions on this connection. 5332c37256eSGarrett Wollman * Enter SYN_SENT state, and mark socket as connecting. 5342c37256eSGarrett Wollman * Start keep-alive timer, and seed output sequence space. 5352c37256eSGarrett Wollman * Send initial segment on connection. 5362c37256eSGarrett Wollman */ 5372c37256eSGarrett Wollman static int 538b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 5392c37256eSGarrett Wollman { 540109eb549SGleb Smirnoff struct epoch_tracker et; 5412c37256eSGarrett Wollman int error = 0; 542f76fcf6dSJeffrey Hsu struct inpcb *inp; 543623dce13SRobert Watson struct tcpcb *tp = NULL; 5442c37256eSGarrett Wollman struct sockaddr_in *sinp; 5452c37256eSGarrett Wollman 54657bf258eSGarrett Wollman sinp = (struct sockaddr_in *)nam; 547e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sinp)) 548e29ef13fSDon Lewis return (EINVAL); 54952710de1SPawel Jakub Dawidek /* 55052710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 55152710de1SPawel Jakub Dawidek */ 5522c37256eSGarrett Wollman if (sinp->sin_family == AF_INET 55352710de1SPawel Jakub Dawidek && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 55452710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 5552cf21ae5SRandall Stewart if ((sinp->sin_family == AF_INET) && 556*f903a308SMichael Tuexen (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST)) 557*f903a308SMichael Tuexen return (EACCES); 558b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0) 559b89e82ddSJamie Gritton return (error); 56075c13541SPoul-Henning Kamp 561623dce13SRobert Watson TCPDEBUG0; 562623dce13SRobert Watson inp = sotoinpcb(so); 563623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 5648501a69cSRobert Watson INP_WLOCK(inp); 565eb96dc33SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) { 566eb96dc33SJulien Charbon error = EADDRINUSE; 567eb96dc33SJulien Charbon goto out; 568eb96dc33SJulien Charbon } 569eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 570eb96dc33SJulien Charbon error = ECONNREFUSED; 571623dce13SRobert Watson goto out; 572623dce13SRobert Watson } 573623dce13SRobert Watson tp = intotcpcb(inp); 574623dce13SRobert Watson TCPDEBUG1(); 575c1604fe4SGleb Smirnoff NET_EPOCH_ENTER(et); 576b40ce416SJulian Elischer if ((error = tcp_connect(tp, nam, td)) != 0) 577c1604fe4SGleb Smirnoff goto out_in_epoch; 57809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 57909fe6320SNavdeep Parhar if (registered_toedevs > 0 && 58037cc0ecbSNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 58109fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 582c1604fe4SGleb Smirnoff goto out_in_epoch; 58309fe6320SNavdeep Parhar #endif 58409fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 58555bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 586c1604fe4SGleb Smirnoff out_in_epoch: 587109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 588623dce13SRobert Watson out: 589623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 590e79cb051SGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CONNECT); 5918501a69cSRobert Watson INP_WUNLOCK(inp); 592623dce13SRobert Watson return (error); 5932c37256eSGarrett Wollman } 594b287c6c7SBjoern A. Zeeb #endif /* INET */ 5952c37256eSGarrett Wollman 596fb59c426SYoshinobu Inoue #ifdef INET6 597fb59c426SYoshinobu Inoue static int 598b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 599fb59c426SYoshinobu Inoue { 600109eb549SGleb Smirnoff struct epoch_tracker et; 601fb59c426SYoshinobu Inoue int error = 0; 602f76fcf6dSJeffrey Hsu struct inpcb *inp; 603623dce13SRobert Watson struct tcpcb *tp = NULL; 6040ecd976eSBjoern A. Zeeb struct sockaddr_in6 *sin6; 6054a91aa8fSMichael Tuexen u_int8_t incflagsav; 6064a91aa8fSMichael Tuexen u_char vflagsav; 607623dce13SRobert Watson 608623dce13SRobert Watson TCPDEBUG0; 609fb59c426SYoshinobu Inoue 6100ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 6110ecd976eSBjoern A. Zeeb if (nam->sa_len != sizeof (*sin6)) 612e29ef13fSDon Lewis return (EINVAL); 61352710de1SPawel Jakub Dawidek /* 61452710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 61552710de1SPawel Jakub Dawidek */ 6160ecd976eSBjoern A. Zeeb if (sin6->sin6_family == AF_INET6 6170ecd976eSBjoern A. Zeeb && IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 61852710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 619fb59c426SYoshinobu Inoue 620623dce13SRobert Watson inp = sotoinpcb(so); 621623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 6228501a69cSRobert Watson INP_WLOCK(inp); 6234a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 6244a91aa8fSMichael Tuexen incflagsav = inp->inp_inc.inc_flags; 625eb96dc33SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) { 626eb96dc33SJulien Charbon error = EADDRINUSE; 627eb96dc33SJulien Charbon goto out; 628eb96dc33SJulien Charbon } 629eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 630eb96dc33SJulien Charbon error = ECONNREFUSED; 631623dce13SRobert Watson goto out; 632623dce13SRobert Watson } 633623dce13SRobert Watson tp = intotcpcb(inp); 634623dce13SRobert Watson TCPDEBUG1(); 635b287c6c7SBjoern A. Zeeb #ifdef INET 636fa046d87SRobert Watson /* 637fa046d87SRobert Watson * XXXRW: Some confusion: V4/V6 flags relate to binding, and 638fa046d87SRobert Watson * therefore probably require the hash lock, which isn't held here. 639fa046d87SRobert Watson * Is this a significant problem? 640fa046d87SRobert Watson */ 6410ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 642fb59c426SYoshinobu Inoue struct sockaddr_in sin; 643fb59c426SYoshinobu Inoue 644d46a5312SMaxim Konovalov if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 645d46a5312SMaxim Konovalov error = EINVAL; 646d46a5312SMaxim Konovalov goto out; 647d46a5312SMaxim Konovalov } 6485dba6adaSMichael Tuexen if ((inp->inp_vflag & INP_IPV4) == 0) { 6495dba6adaSMichael Tuexen error = EAFNOSUPPORT; 6505dba6adaSMichael Tuexen goto out; 6515dba6adaSMichael Tuexen } 65233841545SHajimu UMEMOTO 6530ecd976eSBjoern A. Zeeb in6_sin6_2_sin(&sin, sin6); 654888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 655888973f5SMichael Tuexen error = EAFNOSUPPORT; 656888973f5SMichael Tuexen goto out; 657888973f5SMichael Tuexen } 658*f903a308SMichael Tuexen if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) { 659*f903a308SMichael Tuexen error = EACCES; 6602cf21ae5SRandall Stewart goto out; 6612cf21ae5SRandall Stewart } 662b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, 663b89e82ddSJamie Gritton &sin.sin_addr)) != 0) 664413628a7SBjoern A. Zeeb goto out; 6654a91aa8fSMichael Tuexen inp->inp_vflag |= INP_IPV4; 6664a91aa8fSMichael Tuexen inp->inp_vflag &= ~INP_IPV6; 667c1604fe4SGleb Smirnoff NET_EPOCH_ENTER(et); 668b40ce416SJulian Elischer if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 669c1604fe4SGleb Smirnoff goto out_in_epoch; 67009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 67109fe6320SNavdeep Parhar if (registered_toedevs > 0 && 672adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 67309fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 674c1604fe4SGleb Smirnoff goto out_in_epoch; 67509fe6320SNavdeep Parhar #endif 67655bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 677c1604fe4SGleb Smirnoff goto out_in_epoch; 6785dba6adaSMichael Tuexen } else { 6795dba6adaSMichael Tuexen if ((inp->inp_vflag & INP_IPV6) == 0) { 6805dba6adaSMichael Tuexen error = EAFNOSUPPORT; 6815dba6adaSMichael Tuexen goto out; 6825dba6adaSMichael Tuexen } 683fb59c426SYoshinobu Inoue } 684b287c6c7SBjoern A. Zeeb #endif 6854a91aa8fSMichael Tuexen if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0) 6864a91aa8fSMichael Tuexen goto out; 687fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 688fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 689dcdb4371SBjoern A. Zeeb inp->inp_inc.inc_flags |= INC_ISIPV6; 690b40ce416SJulian Elischer if ((error = tcp6_connect(tp, nam, td)) != 0) 691fb59c426SYoshinobu Inoue goto out; 69209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 69309fe6320SNavdeep Parhar if (registered_toedevs > 0 && 694adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 69509fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 69609fe6320SNavdeep Parhar goto out; 69709fe6320SNavdeep Parhar #endif 69809fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 699109eb549SGleb Smirnoff NET_EPOCH_ENTER(et); 70055bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 7017754e281SBjoern A. Zeeb #ifdef INET 702c1604fe4SGleb Smirnoff out_in_epoch: 7037754e281SBjoern A. Zeeb #endif 704109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 705623dce13SRobert Watson out: 7064a91aa8fSMichael Tuexen /* 7074a91aa8fSMichael Tuexen * If the implicit bind in the connect call fails, restore 7084a91aa8fSMichael Tuexen * the flags we modified. 7094a91aa8fSMichael Tuexen */ 7104a91aa8fSMichael Tuexen if (error != 0 && inp->inp_lport == 0) { 7114a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 7124a91aa8fSMichael Tuexen inp->inp_inc.inc_flags = incflagsav; 7134a91aa8fSMichael Tuexen } 7144a91aa8fSMichael Tuexen 715623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 7165d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CONNECT); 7178501a69cSRobert Watson INP_WUNLOCK(inp); 718623dce13SRobert Watson return (error); 719fb59c426SYoshinobu Inoue } 720fb59c426SYoshinobu Inoue #endif /* INET6 */ 721fb59c426SYoshinobu Inoue 7222c37256eSGarrett Wollman /* 7232c37256eSGarrett Wollman * Initiate disconnect from peer. 7242c37256eSGarrett Wollman * If connection never passed embryonic stage, just drop; 7252c37256eSGarrett Wollman * else if don't need to let data drain, then can just drop anyways, 7262c37256eSGarrett Wollman * else have to begin TCP shutdown process: mark socket disconnecting, 7272c37256eSGarrett Wollman * drain unread data, state switch to reflect user close, and 7282c37256eSGarrett Wollman * send segment (e.g. FIN) to peer. Socket will be really disconnected 7292c37256eSGarrett Wollman * when peer sends FIN and acks ours. 7302c37256eSGarrett Wollman * 7312c37256eSGarrett Wollman * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 7322c37256eSGarrett Wollman */ 7332c37256eSGarrett Wollman static int 7342c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so) 7352c37256eSGarrett Wollman { 736f76fcf6dSJeffrey Hsu struct inpcb *inp; 737623dce13SRobert Watson struct tcpcb *tp = NULL; 7386573d758SMatt Macy struct epoch_tracker et; 739623dce13SRobert Watson int error = 0; 7402c37256eSGarrett Wollman 741623dce13SRobert Watson TCPDEBUG0; 74297a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 743623dce13SRobert Watson inp = sotoinpcb(so); 744623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 7458501a69cSRobert Watson INP_WLOCK(inp); 746489dcc92SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) 747489dcc92SJulien Charbon goto out; 748489dcc92SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 74921367f63SSam Leffler error = ECONNRESET; 750623dce13SRobert Watson goto out; 751623dce13SRobert Watson } 752623dce13SRobert Watson tp = intotcpcb(inp); 753623dce13SRobert Watson TCPDEBUG1(); 754623dce13SRobert Watson tcp_disconnect(tp); 755623dce13SRobert Watson out: 756623dce13SRobert Watson TCPDEBUG2(PRU_DISCONNECT); 7575d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_DISCONNECT); 7588501a69cSRobert Watson INP_WUNLOCK(inp); 75997a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 760623dce13SRobert Watson return (error); 7612c37256eSGarrett Wollman } 7622c37256eSGarrett Wollman 763b287c6c7SBjoern A. Zeeb #ifdef INET 7642c37256eSGarrett Wollman /* 7658296cddfSRobert Watson * Accept a connection. Essentially all the work is done at higher levels; 7668296cddfSRobert Watson * just return the address of the peer, storing through addr. 7672c37256eSGarrett Wollman */ 7682c37256eSGarrett Wollman static int 76957bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam) 7702c37256eSGarrett Wollman { 7712c37256eSGarrett Wollman int error = 0; 772f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 7731db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 77426ef6ac4SDon Lewis struct in_addr addr; 77526ef6ac4SDon Lewis in_port_t port = 0; 7761db24ffbSJonathan Lemon TCPDEBUG0; 7772c37256eSGarrett Wollman 7783d2d3ef4SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 7793d2d3ef4SRobert Watson return (ECONNABORTED); 780f76fcf6dSJeffrey Hsu 781f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 782623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 7838501a69cSRobert Watson INP_WLOCK(inp); 784ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 7853d2d3ef4SRobert Watson error = ECONNABORTED; 786623dce13SRobert Watson goto out; 787623dce13SRobert Watson } 7881db24ffbSJonathan Lemon tp = intotcpcb(inp); 7891db24ffbSJonathan Lemon TCPDEBUG1(); 790f76fcf6dSJeffrey Hsu 791f76fcf6dSJeffrey Hsu /* 79254d642bbSRobert Watson * We inline in_getpeeraddr and COMMON_END here, so that we can 79326ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 79426ef6ac4SDon Lewis * release the lock. 795f76fcf6dSJeffrey Hsu */ 79626ef6ac4SDon Lewis port = inp->inp_fport; 79726ef6ac4SDon Lewis addr = inp->inp_faddr; 798f76fcf6dSJeffrey Hsu 799623dce13SRobert Watson out: 800623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 8015d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 8028501a69cSRobert Watson INP_WUNLOCK(inp); 80326ef6ac4SDon Lewis if (error == 0) 80426ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 80526ef6ac4SDon Lewis return error; 8062c37256eSGarrett Wollman } 807b287c6c7SBjoern A. Zeeb #endif /* INET */ 8082c37256eSGarrett Wollman 809fb59c426SYoshinobu Inoue #ifdef INET6 810fb59c426SYoshinobu Inoue static int 811fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 812fb59c426SYoshinobu Inoue { 813f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 814fb59c426SYoshinobu Inoue int error = 0; 8151db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 81626ef6ac4SDon Lewis struct in_addr addr; 81726ef6ac4SDon Lewis struct in6_addr addr6; 8186573d758SMatt Macy struct epoch_tracker et; 81926ef6ac4SDon Lewis in_port_t port = 0; 82026ef6ac4SDon Lewis int v4 = 0; 8211db24ffbSJonathan Lemon TCPDEBUG0; 822fb59c426SYoshinobu Inoue 823b4470c16SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 824b4470c16SRobert Watson return (ECONNABORTED); 825f76fcf6dSJeffrey Hsu 826f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 827623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 82897a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 8298501a69cSRobert Watson INP_WLOCK(inp); 830ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 83121367f63SSam Leffler error = ECONNABORTED; 832623dce13SRobert Watson goto out; 833623dce13SRobert Watson } 8341db24ffbSJonathan Lemon tp = intotcpcb(inp); 8351db24ffbSJonathan Lemon TCPDEBUG1(); 836623dce13SRobert Watson 83726ef6ac4SDon Lewis /* 83826ef6ac4SDon Lewis * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 83926ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 84026ef6ac4SDon Lewis * release the lock. 84126ef6ac4SDon Lewis */ 84226ef6ac4SDon Lewis if (inp->inp_vflag & INP_IPV4) { 84326ef6ac4SDon Lewis v4 = 1; 84426ef6ac4SDon Lewis port = inp->inp_fport; 84526ef6ac4SDon Lewis addr = inp->inp_faddr; 84626ef6ac4SDon Lewis } else { 84726ef6ac4SDon Lewis port = inp->inp_fport; 84826ef6ac4SDon Lewis addr6 = inp->in6p_faddr; 84926ef6ac4SDon Lewis } 85026ef6ac4SDon Lewis 851623dce13SRobert Watson out: 852623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 8535d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 8548501a69cSRobert Watson INP_WUNLOCK(inp); 85597a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 85626ef6ac4SDon Lewis if (error == 0) { 85726ef6ac4SDon Lewis if (v4) 85826ef6ac4SDon Lewis *nam = in6_v4mapsin6_sockaddr(port, &addr); 85926ef6ac4SDon Lewis else 86026ef6ac4SDon Lewis *nam = in6_sockaddr(port, &addr6); 86126ef6ac4SDon Lewis } 86226ef6ac4SDon Lewis return error; 863fb59c426SYoshinobu Inoue } 864fb59c426SYoshinobu Inoue #endif /* INET6 */ 865f76fcf6dSJeffrey Hsu 866f76fcf6dSJeffrey Hsu /* 8672c37256eSGarrett Wollman * Mark the connection as being incapable of further output. 8682c37256eSGarrett Wollman */ 8692c37256eSGarrett Wollman static int 8702c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so) 8712c37256eSGarrett Wollman { 8722c37256eSGarrett Wollman int error = 0; 873f76fcf6dSJeffrey Hsu struct inpcb *inp; 874623dce13SRobert Watson struct tcpcb *tp = NULL; 8756573d758SMatt Macy struct epoch_tracker et; 8762c37256eSGarrett Wollman 877623dce13SRobert Watson TCPDEBUG0; 87897a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 879623dce13SRobert Watson inp = sotoinpcb(so); 880623dce13SRobert Watson KASSERT(inp != NULL, ("inp == NULL")); 8818501a69cSRobert Watson INP_WLOCK(inp); 882ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 88321367f63SSam Leffler error = ECONNRESET; 884623dce13SRobert Watson goto out; 885623dce13SRobert Watson } 886623dce13SRobert Watson tp = intotcpcb(inp); 887623dce13SRobert Watson TCPDEBUG1(); 8882c37256eSGarrett Wollman socantsendmore(so); 889623dce13SRobert Watson tcp_usrclosed(tp); 890ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 89155bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 892623dce13SRobert Watson 893623dce13SRobert Watson out: 894623dce13SRobert Watson TCPDEBUG2(PRU_SHUTDOWN); 8955d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN); 8968501a69cSRobert Watson INP_WUNLOCK(inp); 89797a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 898623dce13SRobert Watson 899623dce13SRobert Watson return (error); 9002c37256eSGarrett Wollman } 9012c37256eSGarrett Wollman 9022c37256eSGarrett Wollman /* 9032c37256eSGarrett Wollman * After a receive, possibly send window update to peer. 9042c37256eSGarrett Wollman */ 9052c37256eSGarrett Wollman static int 9062c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags) 9072c37256eSGarrett Wollman { 908109eb549SGleb Smirnoff struct epoch_tracker et; 909f76fcf6dSJeffrey Hsu struct inpcb *inp; 910623dce13SRobert Watson struct tcpcb *tp = NULL; 911623dce13SRobert Watson int error = 0; 9122c37256eSGarrett Wollman 913623dce13SRobert Watson TCPDEBUG0; 914623dce13SRobert Watson inp = sotoinpcb(so); 915623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 9168501a69cSRobert Watson INP_WLOCK(inp); 917ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 91821367f63SSam Leffler error = ECONNRESET; 919623dce13SRobert Watson goto out; 920623dce13SRobert Watson } 921623dce13SRobert Watson tp = intotcpcb(inp); 922623dce13SRobert Watson TCPDEBUG1(); 923281a0fd4SPatrick Kelsey /* 924281a0fd4SPatrick Kelsey * For passively-created TFO connections, don't attempt a window 925281a0fd4SPatrick Kelsey * update while still in SYN_RECEIVED as this may trigger an early 926281a0fd4SPatrick Kelsey * SYN|ACK. It is preferable to have the SYN|ACK be sent along with 927281a0fd4SPatrick Kelsey * application response data, or failing that, when the DELACK timer 928281a0fd4SPatrick Kelsey * expires. 929281a0fd4SPatrick Kelsey */ 93068bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 931281a0fd4SPatrick Kelsey (tp->t_state == TCPS_SYN_RECEIVED)) 932281a0fd4SPatrick Kelsey goto out; 93342ce7937SGleb Smirnoff NET_EPOCH_ENTER(et); 93409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 93509fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) 93609fe6320SNavdeep Parhar tcp_offload_rcvd(tp); 937460cf046SNavdeep Parhar else 93809fe6320SNavdeep Parhar #endif 93955bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 940109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 941623dce13SRobert Watson out: 942623dce13SRobert Watson TCPDEBUG2(PRU_RCVD); 9435d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVD); 9448501a69cSRobert Watson INP_WUNLOCK(inp); 945623dce13SRobert Watson return (error); 9462c37256eSGarrett Wollman } 9472c37256eSGarrett Wollman 9482c37256eSGarrett Wollman /* 9492c37256eSGarrett Wollman * Do a send by putting data in output queue and updating urgent 9509c9906e9SPeter Wemm * marker if URG set. Possibly send more data. Unlike the other 9519c9906e9SPeter Wemm * pru_*() routines, the mbuf chains are our responsibility. We 9529c9906e9SPeter Wemm * must either enqueue them or free them. The other pru_* routines 9539c9906e9SPeter Wemm * generally are caller-frees. 9542c37256eSGarrett Wollman */ 9552c37256eSGarrett Wollman static int 95657bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 957b40ce416SJulian Elischer struct sockaddr *nam, struct mbuf *control, struct thread *td) 9582c37256eSGarrett Wollman { 95997a95ee1SGleb Smirnoff struct epoch_tracker et; 9602c37256eSGarrett Wollman int error = 0; 961f76fcf6dSJeffrey Hsu struct inpcb *inp; 962623dce13SRobert Watson struct tcpcb *tp = NULL; 963888973f5SMichael Tuexen #ifdef INET 96451e08d53SMichael Tuexen #ifdef INET6 96551e08d53SMichael Tuexen struct sockaddr_in sin; 96651e08d53SMichael Tuexen #endif 96751e08d53SMichael Tuexen struct sockaddr_in *sinp; 968888973f5SMichael Tuexen #endif 969fb59c426SYoshinobu Inoue #ifdef INET6 970fb59c426SYoshinobu Inoue int isipv6; 971fb59c426SYoshinobu Inoue #endif 9724a91aa8fSMichael Tuexen u_int8_t incflagsav; 9734a91aa8fSMichael Tuexen u_char vflagsav; 9744a91aa8fSMichael Tuexen bool restoreflags; 9759c9906e9SPeter Wemm TCPDEBUG0; 9762c37256eSGarrett Wollman 977f76fcf6dSJeffrey Hsu /* 97897a95ee1SGleb Smirnoff * We require the pcbinfo "read lock" if we will close the socket 97997a95ee1SGleb Smirnoff * as part of this call. 980f76fcf6dSJeffrey Hsu */ 98197a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 982f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 983623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 9848501a69cSRobert Watson INP_WLOCK(inp); 9854a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 9864a91aa8fSMichael Tuexen incflagsav = inp->inp_inc.inc_flags; 9874a91aa8fSMichael Tuexen restoreflags = false; 988ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 9897ff0b850SAndre Oppermann if (control) 9907ff0b850SAndre Oppermann m_freem(control); 9912cbcd3c1SGleb Smirnoff /* 9922cbcd3c1SGleb Smirnoff * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible 9932cbcd3c1SGleb Smirnoff * for freeing memory. 9942cbcd3c1SGleb Smirnoff */ 9952cbcd3c1SGleb Smirnoff if (m && (flags & PRUS_NOTREADY) == 0) 9967ff0b850SAndre Oppermann m_freem(m); 99721367f63SSam Leffler error = ECONNRESET; 9989c9906e9SPeter Wemm goto out; 9999c9906e9SPeter Wemm } 10009c9906e9SPeter Wemm tp = intotcpcb(inp); 1001d3b6c96bSRandall Stewart if (flags & PRUS_OOB) { 1002d3b6c96bSRandall Stewart if ((error = tcp_pru_options_support(tp, PRUS_OOB)) != 0) { 1003d3b6c96bSRandall Stewart if (control) 1004d3b6c96bSRandall Stewart m_freem(control); 1005d3b6c96bSRandall Stewart if (m && (flags & PRUS_NOTREADY) == 0) 1006d3b6c96bSRandall Stewart m_freem(m); 1007d3b6c96bSRandall Stewart goto out; 1008d3b6c96bSRandall Stewart } 1009d3b6c96bSRandall Stewart } 10109c9906e9SPeter Wemm TCPDEBUG1(); 1011888973f5SMichael Tuexen if (nam != NULL && tp->t_state < TCPS_SYN_SENT) { 1012888973f5SMichael Tuexen switch (nam->sa_family) { 1013888973f5SMichael Tuexen #ifdef INET 1014888973f5SMichael Tuexen case AF_INET: 1015888973f5SMichael Tuexen sinp = (struct sockaddr_in *)nam; 1016888973f5SMichael Tuexen if (sinp->sin_len != sizeof(struct sockaddr_in)) { 1017888973f5SMichael Tuexen if (m) 1018888973f5SMichael Tuexen m_freem(m); 1019888973f5SMichael Tuexen error = EINVAL; 1020888973f5SMichael Tuexen goto out; 1021888973f5SMichael Tuexen } 1022888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV6) != 0) { 1023888973f5SMichael Tuexen if (m) 1024888973f5SMichael Tuexen m_freem(m); 1025888973f5SMichael Tuexen error = EAFNOSUPPORT; 1026888973f5SMichael Tuexen goto out; 1027888973f5SMichael Tuexen } 1028888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 1029888973f5SMichael Tuexen if (m) 1030888973f5SMichael Tuexen m_freem(m); 1031888973f5SMichael Tuexen error = EAFNOSUPPORT; 1032888973f5SMichael Tuexen goto out; 1033888973f5SMichael Tuexen } 1034*f903a308SMichael Tuexen if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) { 10352cf21ae5SRandall Stewart if (m) 10362cf21ae5SRandall Stewart m_freem(m); 1037*f903a308SMichael Tuexen error = EACCES; 10382cf21ae5SRandall Stewart goto out; 10392cf21ae5SRandall Stewart } 1040888973f5SMichael Tuexen if ((error = prison_remote_ip4(td->td_ucred, 1041888973f5SMichael Tuexen &sinp->sin_addr))) { 1042888973f5SMichael Tuexen if (m) 1043888973f5SMichael Tuexen m_freem(m); 1044888973f5SMichael Tuexen goto out; 1045888973f5SMichael Tuexen } 1046888973f5SMichael Tuexen #ifdef INET6 1047888973f5SMichael Tuexen isipv6 = 0; 1048888973f5SMichael Tuexen #endif 1049888973f5SMichael Tuexen break; 1050888973f5SMichael Tuexen #endif /* INET */ 1051888973f5SMichael Tuexen #ifdef INET6 1052888973f5SMichael Tuexen case AF_INET6: 1053888973f5SMichael Tuexen { 10540ecd976eSBjoern A. Zeeb struct sockaddr_in6 *sin6; 1055888973f5SMichael Tuexen 10560ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 10570ecd976eSBjoern A. Zeeb if (sin6->sin6_len != sizeof(*sin6)) { 1058888973f5SMichael Tuexen if (m) 1059888973f5SMichael Tuexen m_freem(m); 1060888973f5SMichael Tuexen error = EINVAL; 1061888973f5SMichael Tuexen goto out; 1062888973f5SMichael Tuexen } 1063e240ce42SMichael Tuexen if ((inp->inp_vflag & INP_IPV6PROTO) == 0) { 1064e240ce42SMichael Tuexen if (m != NULL) 1065e240ce42SMichael Tuexen m_freem(m); 1066e240ce42SMichael Tuexen error = EAFNOSUPPORT; 1067e240ce42SMichael Tuexen goto out; 1068e240ce42SMichael Tuexen } 10690ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 1070888973f5SMichael Tuexen if (m) 1071888973f5SMichael Tuexen m_freem(m); 1072888973f5SMichael Tuexen error = EAFNOSUPPORT; 1073888973f5SMichael Tuexen goto out; 1074888973f5SMichael Tuexen } 10750ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 1076888973f5SMichael Tuexen #ifdef INET 1077888973f5SMichael Tuexen if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 1078888973f5SMichael Tuexen error = EINVAL; 1079888973f5SMichael Tuexen if (m) 1080888973f5SMichael Tuexen m_freem(m); 1081888973f5SMichael Tuexen goto out; 1082888973f5SMichael Tuexen } 1083888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV4) == 0) { 1084888973f5SMichael Tuexen error = EAFNOSUPPORT; 1085888973f5SMichael Tuexen if (m) 1086888973f5SMichael Tuexen m_freem(m); 1087888973f5SMichael Tuexen goto out; 1088888973f5SMichael Tuexen } 10894a91aa8fSMichael Tuexen restoreflags = true; 1090888973f5SMichael Tuexen inp->inp_vflag &= ~INP_IPV6; 1091888973f5SMichael Tuexen sinp = &sin; 10920ecd976eSBjoern A. Zeeb in6_sin6_2_sin(sinp, sin6); 1093888973f5SMichael Tuexen if (IN_MULTICAST( 1094888973f5SMichael Tuexen ntohl(sinp->sin_addr.s_addr))) { 1095888973f5SMichael Tuexen error = EAFNOSUPPORT; 1096888973f5SMichael Tuexen if (m) 1097888973f5SMichael Tuexen m_freem(m); 1098888973f5SMichael Tuexen goto out; 1099888973f5SMichael Tuexen } 1100888973f5SMichael Tuexen if ((error = prison_remote_ip4(td->td_ucred, 1101888973f5SMichael Tuexen &sinp->sin_addr))) { 1102888973f5SMichael Tuexen if (m) 1103888973f5SMichael Tuexen m_freem(m); 1104888973f5SMichael Tuexen goto out; 1105888973f5SMichael Tuexen } 1106888973f5SMichael Tuexen isipv6 = 0; 1107888973f5SMichael Tuexen #else /* !INET */ 1108888973f5SMichael Tuexen error = EAFNOSUPPORT; 1109888973f5SMichael Tuexen if (m) 1110888973f5SMichael Tuexen m_freem(m); 1111888973f5SMichael Tuexen goto out; 1112888973f5SMichael Tuexen #endif /* INET */ 1113888973f5SMichael Tuexen } else { 1114888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV6) == 0) { 1115888973f5SMichael Tuexen if (m) 1116888973f5SMichael Tuexen m_freem(m); 1117888973f5SMichael Tuexen error = EAFNOSUPPORT; 1118888973f5SMichael Tuexen goto out; 1119888973f5SMichael Tuexen } 11204a91aa8fSMichael Tuexen restoreflags = true; 1121888973f5SMichael Tuexen inp->inp_vflag &= ~INP_IPV4; 1122888973f5SMichael Tuexen inp->inp_inc.inc_flags |= INC_ISIPV6; 1123888973f5SMichael Tuexen if ((error = prison_remote_ip6(td->td_ucred, 11240ecd976eSBjoern A. Zeeb &sin6->sin6_addr))) { 1125888973f5SMichael Tuexen if (m) 1126888973f5SMichael Tuexen m_freem(m); 1127888973f5SMichael Tuexen goto out; 1128888973f5SMichael Tuexen } 1129888973f5SMichael Tuexen isipv6 = 1; 1130888973f5SMichael Tuexen } 1131888973f5SMichael Tuexen break; 1132888973f5SMichael Tuexen } 1133888973f5SMichael Tuexen #endif /* INET6 */ 1134888973f5SMichael Tuexen default: 1135888973f5SMichael Tuexen if (m) 1136888973f5SMichael Tuexen m_freem(m); 1137888973f5SMichael Tuexen error = EAFNOSUPPORT; 1138888973f5SMichael Tuexen goto out; 1139888973f5SMichael Tuexen } 1140888973f5SMichael Tuexen } 11419c9906e9SPeter Wemm if (control) { 11429c9906e9SPeter Wemm /* TCP doesn't do control messages (rights, creds, etc) */ 11439c9906e9SPeter Wemm if (control->m_len) { 11449c9906e9SPeter Wemm m_freem(control); 11452c37256eSGarrett Wollman if (m) 11462c37256eSGarrett Wollman m_freem(m); 1147744f87eaSDavid Greenman error = EINVAL; 1148744f87eaSDavid Greenman goto out; 11492c37256eSGarrett Wollman } 11509c9906e9SPeter Wemm m_freem(control); /* empty control, just free it */ 11519c9906e9SPeter Wemm } 11522c37256eSGarrett Wollman if (!(flags & PRUS_OOB)) { 1153651e4e6aSGleb Smirnoff sbappendstream(&so->so_snd, m, flags); 11542c37256eSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 11552c37256eSGarrett Wollman /* 11562c37256eSGarrett Wollman * Do implied connect if not yet connected, 11572c37256eSGarrett Wollman * initialize window to default value, and 11580c39d38dSGleb Smirnoff * initialize maxseg using peer's cached MSS. 11592c37256eSGarrett Wollman */ 1160fb59c426SYoshinobu Inoue #ifdef INET6 1161fb59c426SYoshinobu Inoue if (isipv6) 1162b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 1163fb59c426SYoshinobu Inoue #endif /* INET6 */ 1164b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1165b287c6c7SBjoern A. Zeeb else 1166b287c6c7SBjoern A. Zeeb #endif 1167b287c6c7SBjoern A. Zeeb #ifdef INET 1168888973f5SMichael Tuexen error = tcp_connect(tp, 1169888973f5SMichael Tuexen (struct sockaddr *)sinp, td); 1170b287c6c7SBjoern A. Zeeb #endif 11714a91aa8fSMichael Tuexen /* 11724a91aa8fSMichael Tuexen * The bind operation in tcp_connect succeeded. We 11734a91aa8fSMichael Tuexen * no longer want to restore the flags if later 11744a91aa8fSMichael Tuexen * operations fail. 11754a91aa8fSMichael Tuexen */ 11764a91aa8fSMichael Tuexen if (error == 0 || inp->inp_lport != 0) 11774a91aa8fSMichael Tuexen restoreflags = false; 11784a91aa8fSMichael Tuexen 11792c37256eSGarrett Wollman if (error) 11802c37256eSGarrett Wollman goto out; 1181c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) 1182c560df6fSPatrick Kelsey tcp_fastopen_connect(tp); 118318a75309SPatrick Kelsey else { 11842c37256eSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 11852c37256eSGarrett Wollman tcp_mss(tp, -1); 11862c37256eSGarrett Wollman } 1187c560df6fSPatrick Kelsey } 11882c37256eSGarrett Wollman if (flags & PRUS_EOF) { 11892c37256eSGarrett Wollman /* 11902c37256eSGarrett Wollman * Close the send side of the connection after 11912c37256eSGarrett Wollman * the data is sent. 11922c37256eSGarrett Wollman */ 11932c37256eSGarrett Wollman socantsendmore(so); 1194623dce13SRobert Watson tcp_usrclosed(tp); 11952c37256eSGarrett Wollman } 1196e854dd38SRandall Stewart if (TCPS_HAVEESTABLISHED(tp->t_state) && 1197e854dd38SRandall Stewart ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 1198e854dd38SRandall Stewart (tp->t_fbyte_out == 0) && 1199e854dd38SRandall Stewart (so->so_snd.sb_ccc > 0)) { 1200e854dd38SRandall Stewart tp->t_fbyte_out = ticks; 1201e854dd38SRandall Stewart if (tp->t_fbyte_out == 0) 1202e854dd38SRandall Stewart tp->t_fbyte_out = 1; 1203e854dd38SRandall Stewart if (tp->t_fbyte_out && tp->t_fbyte_in) 1204e854dd38SRandall Stewart tp->t_flags2 |= TF2_FBYTES_COMPLETE; 1205e854dd38SRandall Stewart } 12062cbcd3c1SGleb Smirnoff if (!(inp->inp_flags & INP_DROPPED) && 12072cbcd3c1SGleb Smirnoff !(flags & PRUS_NOTREADY)) { 1208b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 1209b0acefa8SBill Fenner tp->t_flags |= TF_MORETOCOME; 121055bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 1211b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 1212b0acefa8SBill Fenner tp->t_flags &= ~TF_MORETOCOME; 1213b0acefa8SBill Fenner } 12142c37256eSGarrett Wollman } else { 1215623dce13SRobert Watson /* 1216623dce13SRobert Watson * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 1217623dce13SRobert Watson */ 1218d2bc35abSRobert Watson SOCKBUF_LOCK(&so->so_snd); 12192c37256eSGarrett Wollman if (sbspace(&so->so_snd) < -512) { 1220d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 12212c37256eSGarrett Wollman m_freem(m); 12222c37256eSGarrett Wollman error = ENOBUFS; 12232c37256eSGarrett Wollman goto out; 12242c37256eSGarrett Wollman } 12252c37256eSGarrett Wollman /* 12262c37256eSGarrett Wollman * According to RFC961 (Assigned Protocols), 12272c37256eSGarrett Wollman * the urgent pointer points to the last octet 12282c37256eSGarrett Wollman * of urgent data. We continue, however, 12292c37256eSGarrett Wollman * to consider it to indicate the first octet 12302c37256eSGarrett Wollman * of data past the urgent section. 12312c37256eSGarrett Wollman * Otherwise, snd_up should be one lower. 12322c37256eSGarrett Wollman */ 1233651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_snd, m, flags); 1234d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 1235ef53690bSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 1236ef53690bSGarrett Wollman /* 1237ef53690bSGarrett Wollman * Do implied connect if not yet connected, 1238ef53690bSGarrett Wollman * initialize window to default value, and 12390c39d38dSGleb Smirnoff * initialize maxseg using peer's cached MSS. 1240ef53690bSGarrett Wollman */ 124118a75309SPatrick Kelsey 1242c560df6fSPatrick Kelsey /* 1243c560df6fSPatrick Kelsey * Not going to contemplate SYN|URG 1244c560df6fSPatrick Kelsey */ 1245c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) 1246c560df6fSPatrick Kelsey tp->t_flags &= ~TF_FASTOPEN; 1247fb59c426SYoshinobu Inoue #ifdef INET6 1248fb59c426SYoshinobu Inoue if (isipv6) 1249b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 1250fb59c426SYoshinobu Inoue #endif /* INET6 */ 1251b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1252b287c6c7SBjoern A. Zeeb else 1253b287c6c7SBjoern A. Zeeb #endif 1254b287c6c7SBjoern A. Zeeb #ifdef INET 1255888973f5SMichael Tuexen error = tcp_connect(tp, 1256888973f5SMichael Tuexen (struct sockaddr *)sinp, td); 1257b287c6c7SBjoern A. Zeeb #endif 12584a91aa8fSMichael Tuexen /* 12594a91aa8fSMichael Tuexen * The bind operation in tcp_connect succeeded. We 12604a91aa8fSMichael Tuexen * no longer want to restore the flags if later 12614a91aa8fSMichael Tuexen * operations fail. 12624a91aa8fSMichael Tuexen */ 12634a91aa8fSMichael Tuexen if (error == 0 || inp->inp_lport != 0) 12644a91aa8fSMichael Tuexen restoreflags = false; 12654a91aa8fSMichael Tuexen 1266ef53690bSGarrett Wollman if (error) 1267ef53690bSGarrett Wollman goto out; 1268ef53690bSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 1269ef53690bSGarrett Wollman tcp_mss(tp, -1); 1270623dce13SRobert Watson } 1271300fa232SGleb Smirnoff tp->snd_up = tp->snd_una + sbavail(&so->so_snd); 12722cbcd3c1SGleb Smirnoff if (!(flags & PRUS_NOTREADY)) { 12732cdbfa66SPaul Saab tp->t_flags |= TF_FORCEDATA; 127455bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 12752cdbfa66SPaul Saab tp->t_flags &= ~TF_FORCEDATA; 12762c37256eSGarrett Wollman } 12772cbcd3c1SGleb Smirnoff } 12782529f56eSJonathan T. Looney TCP_LOG_EVENT(tp, NULL, 12792529f56eSJonathan T. Looney &inp->inp_socket->so_rcv, 12802529f56eSJonathan T. Looney &inp->inp_socket->so_snd, 12812529f56eSJonathan T. Looney TCP_LOG_USERSEND, error, 12822529f56eSJonathan T. Looney 0, NULL, false); 1283d1401c90SRobert Watson out: 12844a91aa8fSMichael Tuexen /* 12854a91aa8fSMichael Tuexen * If the request was unsuccessful and we changed flags, 12864a91aa8fSMichael Tuexen * restore the original flags. 12874a91aa8fSMichael Tuexen */ 12884a91aa8fSMichael Tuexen if (error != 0 && restoreflags) { 12894a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 12904a91aa8fSMichael Tuexen inp->inp_inc.inc_flags = incflagsav; 12914a91aa8fSMichael Tuexen } 1292d1401c90SRobert Watson TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB : 12932c37256eSGarrett Wollman ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 12945d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB : 12955d06879aSGeorge V. Neville-Neil ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 12968501a69cSRobert Watson INP_WUNLOCK(inp); 129797a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 129873fddedaSPeter Grehan return (error); 12992c37256eSGarrett Wollman } 13002c37256eSGarrett Wollman 13012cbcd3c1SGleb Smirnoff static int 13022cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count) 13032cbcd3c1SGleb Smirnoff { 1304109eb549SGleb Smirnoff struct epoch_tracker et; 13052cbcd3c1SGleb Smirnoff struct inpcb *inp; 13062cbcd3c1SGleb Smirnoff struct tcpcb *tp; 13072cbcd3c1SGleb Smirnoff int error; 13082cbcd3c1SGleb Smirnoff 13092cbcd3c1SGleb Smirnoff inp = sotoinpcb(so); 13102cbcd3c1SGleb Smirnoff INP_WLOCK(inp); 13112cbcd3c1SGleb Smirnoff if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 13122cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 131382334850SJohn Baldwin mb_free_notready(m, count); 13142cbcd3c1SGleb Smirnoff return (ECONNRESET); 13152cbcd3c1SGleb Smirnoff } 13162cbcd3c1SGleb Smirnoff tp = intotcpcb(inp); 13172cbcd3c1SGleb Smirnoff 13182cbcd3c1SGleb Smirnoff SOCKBUF_LOCK(&so->so_snd); 13192cbcd3c1SGleb Smirnoff error = sbready(&so->so_snd, m, count); 13202cbcd3c1SGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 1321109eb549SGleb Smirnoff if (error == 0) { 1322109eb549SGleb Smirnoff NET_EPOCH_ENTER(et); 132355bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 1324109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 1325109eb549SGleb Smirnoff } 13262cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 13272cbcd3c1SGleb Smirnoff 13282cbcd3c1SGleb Smirnoff return (error); 13292cbcd3c1SGleb Smirnoff } 13302cbcd3c1SGleb Smirnoff 13312c37256eSGarrett Wollman /* 1332a152f8a3SRobert Watson * Abort the TCP. Drop the connection abruptly. 13332c37256eSGarrett Wollman */ 1334ac45e92fSRobert Watson static void 13352c37256eSGarrett Wollman tcp_usr_abort(struct socket *so) 13362c37256eSGarrett Wollman { 1337f76fcf6dSJeffrey Hsu struct inpcb *inp; 1338a152f8a3SRobert Watson struct tcpcb *tp = NULL; 13396573d758SMatt Macy struct epoch_tracker et; 1340623dce13SRobert Watson TCPDEBUG0; 1341c78cbc7bSRobert Watson 1342ac45e92fSRobert Watson inp = sotoinpcb(so); 1343c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 1344c78cbc7bSRobert Watson 134597a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 13468501a69cSRobert Watson INP_WLOCK(inp); 1347c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 1348c78cbc7bSRobert Watson ("tcp_usr_abort: inp_socket == NULL")); 1349c78cbc7bSRobert Watson 1350c78cbc7bSRobert Watson /* 1351a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, drop. 1352c78cbc7bSRobert Watson */ 1353ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_TIMEWAIT) && 1354ad71fe3cSRobert Watson !(inp->inp_flags & INP_DROPPED)) { 1355c78cbc7bSRobert Watson tp = intotcpcb(inp); 1356a152f8a3SRobert Watson TCPDEBUG1(); 13578fa799bdSJonathan T. Looney tp = tcp_drop(tp, ECONNABORTED); 13588fa799bdSJonathan T. Looney if (tp == NULL) 13598fa799bdSJonathan T. Looney goto dropped; 1360a152f8a3SRobert Watson TCPDEBUG2(PRU_ABORT); 13615d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ABORT); 1362c78cbc7bSRobert Watson } 1363ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1364a152f8a3SRobert Watson SOCK_LOCK(so); 1365a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 1366a152f8a3SRobert Watson SOCK_UNLOCK(so); 1367ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1368a152f8a3SRobert Watson } 13698501a69cSRobert Watson INP_WUNLOCK(inp); 13708fa799bdSJonathan T. Looney dropped: 137197a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 1372a152f8a3SRobert Watson } 1373a152f8a3SRobert Watson 1374a152f8a3SRobert Watson /* 1375a152f8a3SRobert Watson * TCP socket is closed. Start friendly disconnect. 1376a152f8a3SRobert Watson */ 1377a152f8a3SRobert Watson static void 1378a152f8a3SRobert Watson tcp_usr_close(struct socket *so) 1379a152f8a3SRobert Watson { 1380a152f8a3SRobert Watson struct inpcb *inp; 1381a152f8a3SRobert Watson struct tcpcb *tp = NULL; 13826573d758SMatt Macy struct epoch_tracker et; 1383a152f8a3SRobert Watson TCPDEBUG0; 1384a152f8a3SRobert Watson 1385a152f8a3SRobert Watson inp = sotoinpcb(so); 1386a152f8a3SRobert Watson KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL")); 1387a152f8a3SRobert Watson 138897a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 13898501a69cSRobert Watson INP_WLOCK(inp); 1390a152f8a3SRobert Watson KASSERT(inp->inp_socket != NULL, 1391a152f8a3SRobert Watson ("tcp_usr_close: inp_socket == NULL")); 1392a152f8a3SRobert Watson 1393a152f8a3SRobert Watson /* 1394a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, initiate 1395a152f8a3SRobert Watson * a disconnect. 1396a152f8a3SRobert Watson */ 1397ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_TIMEWAIT) && 1398ad71fe3cSRobert Watson !(inp->inp_flags & INP_DROPPED)) { 1399a152f8a3SRobert Watson tp = intotcpcb(inp); 1400a152f8a3SRobert Watson TCPDEBUG1(); 1401a152f8a3SRobert Watson tcp_disconnect(tp); 1402a152f8a3SRobert Watson TCPDEBUG2(PRU_CLOSE); 14035d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CLOSE); 1404a152f8a3SRobert Watson } 1405ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1406a152f8a3SRobert Watson SOCK_LOCK(so); 1407a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 1408a152f8a3SRobert Watson SOCK_UNLOCK(so); 1409ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1410a152f8a3SRobert Watson } 14118501a69cSRobert Watson INP_WUNLOCK(inp); 141297a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 14132c37256eSGarrett Wollman } 14142c37256eSGarrett Wollman 1415d3b6c96bSRandall Stewart static int 1416d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags) 1417d3b6c96bSRandall Stewart { 1418d3b6c96bSRandall Stewart /* 1419d3b6c96bSRandall Stewart * If the specific TCP stack has a pru_options 1420d3b6c96bSRandall Stewart * specified then it does not always support 1421d3b6c96bSRandall Stewart * all the PRU_XX options and we must ask it. 1422d3b6c96bSRandall Stewart * If the function is not specified then all 1423d3b6c96bSRandall Stewart * of the PRU_XX options are supported. 1424d3b6c96bSRandall Stewart */ 1425d3b6c96bSRandall Stewart int ret = 0; 1426d3b6c96bSRandall Stewart 1427d3b6c96bSRandall Stewart if (tp->t_fb->tfb_pru_options) { 1428d3b6c96bSRandall Stewart ret = (*tp->t_fb->tfb_pru_options)(tp, flags); 1429d3b6c96bSRandall Stewart } 1430d3b6c96bSRandall Stewart return (ret); 1431d3b6c96bSRandall Stewart } 1432d3b6c96bSRandall Stewart 14332c37256eSGarrett Wollman /* 14342c37256eSGarrett Wollman * Receive out-of-band data. 14352c37256eSGarrett Wollman */ 14362c37256eSGarrett Wollman static int 14372c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 14382c37256eSGarrett Wollman { 14392c37256eSGarrett Wollman int error = 0; 1440f76fcf6dSJeffrey Hsu struct inpcb *inp; 1441623dce13SRobert Watson struct tcpcb *tp = NULL; 14422c37256eSGarrett Wollman 1443623dce13SRobert Watson TCPDEBUG0; 1444623dce13SRobert Watson inp = sotoinpcb(so); 1445623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 14468501a69cSRobert Watson INP_WLOCK(inp); 1447ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 144821367f63SSam Leffler error = ECONNRESET; 1449623dce13SRobert Watson goto out; 1450623dce13SRobert Watson } 1451623dce13SRobert Watson tp = intotcpcb(inp); 1452d3b6c96bSRandall Stewart error = tcp_pru_options_support(tp, PRUS_OOB); 1453d3b6c96bSRandall Stewart if (error) { 1454d3b6c96bSRandall Stewart goto out; 1455d3b6c96bSRandall Stewart } 1456623dce13SRobert Watson TCPDEBUG1(); 14572c37256eSGarrett Wollman if ((so->so_oobmark == 0 && 1458c0b99ffaSRobert Watson (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 14594cc20ab1SSeigo Tanimura so->so_options & SO_OOBINLINE || 14604cc20ab1SSeigo Tanimura tp->t_oobflags & TCPOOB_HADDATA) { 14612c37256eSGarrett Wollman error = EINVAL; 14622c37256eSGarrett Wollman goto out; 14632c37256eSGarrett Wollman } 14642c37256eSGarrett Wollman if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 14652c37256eSGarrett Wollman error = EWOULDBLOCK; 14662c37256eSGarrett Wollman goto out; 14672c37256eSGarrett Wollman } 14682c37256eSGarrett Wollman m->m_len = 1; 14692c37256eSGarrett Wollman *mtod(m, caddr_t) = tp->t_iobc; 14702c37256eSGarrett Wollman if ((flags & MSG_PEEK) == 0) 14712c37256eSGarrett Wollman tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1472623dce13SRobert Watson 1473623dce13SRobert Watson out: 1474623dce13SRobert Watson TCPDEBUG2(PRU_RCVOOB); 14755d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVOOB); 14768501a69cSRobert Watson INP_WUNLOCK(inp); 1477623dce13SRobert Watson return (error); 14782c37256eSGarrett Wollman } 14792c37256eSGarrett Wollman 1480b287c6c7SBjoern A. Zeeb #ifdef INET 14812c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = { 1482756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1483756d52a1SPoul-Henning Kamp .pru_accept = tcp_usr_accept, 1484756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1485756d52a1SPoul-Henning Kamp .pru_bind = tcp_usr_bind, 1486756d52a1SPoul-Henning Kamp .pru_connect = tcp_usr_connect, 1487756d52a1SPoul-Henning Kamp .pru_control = in_control, 1488756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1489756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1490756d52a1SPoul-Henning Kamp .pru_listen = tcp_usr_listen, 149154d642bbSRobert Watson .pru_peeraddr = in_getpeeraddr, 1492756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1493756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1494756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 14952cbcd3c1SGleb Smirnoff .pru_ready = tcp_usr_ready, 1496756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 149754d642bbSRobert Watson .pru_sockaddr = in_getsockaddr, 1498a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1499a152f8a3SRobert Watson .pru_close = tcp_usr_close, 15002c37256eSGarrett Wollman }; 1501b287c6c7SBjoern A. Zeeb #endif /* INET */ 1502df8bae1dSRodney W. Grimes 1503fb59c426SYoshinobu Inoue #ifdef INET6 1504fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = { 1505756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1506756d52a1SPoul-Henning Kamp .pru_accept = tcp6_usr_accept, 1507756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1508756d52a1SPoul-Henning Kamp .pru_bind = tcp6_usr_bind, 1509756d52a1SPoul-Henning Kamp .pru_connect = tcp6_usr_connect, 1510756d52a1SPoul-Henning Kamp .pru_control = in6_control, 1511756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1512756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1513756d52a1SPoul-Henning Kamp .pru_listen = tcp6_usr_listen, 1514756d52a1SPoul-Henning Kamp .pru_peeraddr = in6_mapped_peeraddr, 1515756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1516756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1517756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 15182cbcd3c1SGleb Smirnoff .pru_ready = tcp_usr_ready, 1519756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 1520756d52a1SPoul-Henning Kamp .pru_sockaddr = in6_mapped_sockaddr, 1521a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1522a152f8a3SRobert Watson .pru_close = tcp_usr_close, 1523fb59c426SYoshinobu Inoue }; 1524fb59c426SYoshinobu Inoue #endif /* INET6 */ 1525fb59c426SYoshinobu Inoue 1526b287c6c7SBjoern A. Zeeb #ifdef INET 1527a0292f23SGarrett Wollman /* 1528a0292f23SGarrett Wollman * Common subroutine to open a TCP connection to remote host specified 1529a0292f23SGarrett Wollman * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 15305200e00eSIan Dowse * port number if needed. Call in_pcbconnect_setup to do the routing and 15315200e00eSIan Dowse * to choose a local host address (interface). If there is an existing 15325200e00eSIan Dowse * incarnation of the same connection in TIME-WAIT state and if the remote 15335200e00eSIan Dowse * host was sending CC options and if the connection duration was < MSL, then 1534a0292f23SGarrett Wollman * truncate the previous TIME-WAIT state and proceed. 1535a0292f23SGarrett Wollman * Initialize connection parameters and enter SYN-SENT state. 1536a0292f23SGarrett Wollman */ 15370312fbe9SPoul-Henning Kamp static int 1538ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1539a0292f23SGarrett Wollman { 1540a0292f23SGarrett Wollman struct inpcb *inp = tp->t_inpcb, *oinp; 1541a0292f23SGarrett Wollman struct socket *so = inp->inp_socket; 15425200e00eSIan Dowse struct in_addr laddr; 15435200e00eSIan Dowse u_short lport; 1544c3229e05SDavid Greenman int error; 1545a0292f23SGarrett Wollman 1546c1604fe4SGleb Smirnoff NET_EPOCH_ASSERT(); 15478501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1548fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 1549623dce13SRobert Watson 155025102351SMike Karels if (V_tcp_require_unique_port && inp->inp_lport == 0) { 15514616026fSErmal Luçi error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 15524616026fSErmal Luçi if (error) 1553fa046d87SRobert Watson goto out; 1554a0292f23SGarrett Wollman } 1555a0292f23SGarrett Wollman 1556a0292f23SGarrett Wollman /* 1557a0292f23SGarrett Wollman * Cannot simply call in_pcbconnect, because there might be an 1558a0292f23SGarrett Wollman * earlier incarnation of this same connection still in 1559a0292f23SGarrett Wollman * TIME_WAIT state, creating an ADDRINUSE error. 1560a0292f23SGarrett Wollman */ 15615200e00eSIan Dowse laddr = inp->inp_laddr; 15625200e00eSIan Dowse lport = inp->inp_lport; 15635200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 1564b0330ed9SPawel Jakub Dawidek &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 15655200e00eSIan Dowse if (error && oinp == NULL) 1566fa046d87SRobert Watson goto out; 1567fa046d87SRobert Watson if (oinp) { 1568fa046d87SRobert Watson error = EADDRINUSE; 1569fa046d87SRobert Watson goto out; 1570fa046d87SRobert Watson } 157125102351SMike Karels /* Handle initial bind if it hadn't been done in advance. */ 157225102351SMike Karels if (inp->inp_lport == 0) { 157325102351SMike Karels inp->inp_lport = lport; 157425102351SMike Karels if (in_pcbinshash(inp) != 0) { 157525102351SMike Karels inp->inp_lport = 0; 157625102351SMike Karels error = EAGAIN; 157725102351SMike Karels goto out; 157825102351SMike Karels } 157925102351SMike Karels } 15805200e00eSIan Dowse inp->inp_laddr = laddr; 158115bd2b43SDavid Greenman in_pcbrehash(inp); 1582fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1583a0292f23SGarrett Wollman 1584087b55eaSAndre Oppermann /* 1585087b55eaSAndre Oppermann * Compute window scaling to request: 1586087b55eaSAndre Oppermann * Scale to fit into sweet spot. See tcp_syncache.c. 1587087b55eaSAndre Oppermann * XXX: This should move to tcp_output(). 1588087b55eaSAndre Oppermann */ 1589a0292f23SGarrett Wollman while (tp->request_r_scale < TCP_MAX_WINSHIFT && 15909b3bc6bfSMike Silbersack (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1591a0292f23SGarrett Wollman tp->request_r_scale++; 1592a0292f23SGarrett Wollman 1593a0292f23SGarrett Wollman soisconnecting(so); 159478b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 159557f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 15968e02b4e0SMichael Tuexen tp->iss = tcp_new_isn(&inp->inp_inc); 15978e02b4e0SMichael Tuexen if (tp->t_flags & TF_REQ_TSTMP) 15988e02b4e0SMichael Tuexen tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc); 1599a0292f23SGarrett Wollman tcp_sendseqinit(tp); 1600a45d2726SAndras Olah 1601a0292f23SGarrett Wollman return 0; 1602fa046d87SRobert Watson 1603fa046d87SRobert Watson out: 1604fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1605fa046d87SRobert Watson return (error); 1606a0292f23SGarrett Wollman } 1607b287c6c7SBjoern A. Zeeb #endif /* INET */ 1608a0292f23SGarrett Wollman 1609fb59c426SYoshinobu Inoue #ifdef INET6 1610fb59c426SYoshinobu Inoue static int 1611ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1612fb59c426SYoshinobu Inoue { 1613a7e201bbSAndrey V. Elsukov struct inpcb *inp = tp->t_inpcb; 1614fb59c426SYoshinobu Inoue int error; 1615fb59c426SYoshinobu Inoue 16168501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1617fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 1618623dce13SRobert Watson 161925102351SMike Karels if (V_tcp_require_unique_port && inp->inp_lport == 0) { 16204616026fSErmal Luçi error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 16214616026fSErmal Luçi if (error) 1622fa046d87SRobert Watson goto out; 1623fb59c426SYoshinobu Inoue } 1624a7e201bbSAndrey V. Elsukov error = in6_pcbconnect(inp, nam, td->td_ucred); 1625a7e201bbSAndrey V. Elsukov if (error != 0) 1626b598155aSRobert Watson goto out; 1627fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1628fb59c426SYoshinobu Inoue 1629fb59c426SYoshinobu Inoue /* Compute window scaling to request. */ 1630fb59c426SYoshinobu Inoue while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1631970caf60SBjoern A. Zeeb (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1632fb59c426SYoshinobu Inoue tp->request_r_scale++; 1633fb59c426SYoshinobu Inoue 1634a7e201bbSAndrey V. Elsukov soisconnecting(inp->inp_socket); 163578b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 163657f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 16378e02b4e0SMichael Tuexen tp->iss = tcp_new_isn(&inp->inp_inc); 16388e02b4e0SMichael Tuexen if (tp->t_flags & TF_REQ_TSTMP) 16398e02b4e0SMichael Tuexen tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc); 1640fb59c426SYoshinobu Inoue tcp_sendseqinit(tp); 1641fb59c426SYoshinobu Inoue 1642fb59c426SYoshinobu Inoue return 0; 1643fa046d87SRobert Watson 1644fa046d87SRobert Watson out: 1645fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1646fa046d87SRobert Watson return error; 1647fb59c426SYoshinobu Inoue } 1648fb59c426SYoshinobu Inoue #endif /* INET6 */ 1649fb59c426SYoshinobu Inoue 1650cfe8b629SGarrett Wollman /* 1651b8af5dfaSRobert Watson * Export TCP internal state information via a struct tcp_info, based on the 1652b8af5dfaSRobert Watson * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1653b8af5dfaSRobert Watson * (TCP state machine, etc). We export all information using FreeBSD-native 1654b8af5dfaSRobert Watson * constants -- for example, the numeric values for tcpi_state will differ 1655b8af5dfaSRobert Watson * from Linux. 1656b8af5dfaSRobert Watson */ 1657b8af5dfaSRobert Watson static void 1658ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) 1659b8af5dfaSRobert Watson { 1660b8af5dfaSRobert Watson 16618501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1662b8af5dfaSRobert Watson bzero(ti, sizeof(*ti)); 1663b8af5dfaSRobert Watson 1664b8af5dfaSRobert Watson ti->tcpi_state = tp->t_state; 1665b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1666b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 16673529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 1668b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_SACK; 1669b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1670b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_WSCALE; 1671b8af5dfaSRobert Watson ti->tcpi_snd_wscale = tp->snd_scale; 1672b8af5dfaSRobert Watson ti->tcpi_rcv_wscale = tp->rcv_scale; 1673b8af5dfaSRobert Watson } 16743cf38784SMichael Tuexen if (tp->t_flags2 & TF2_ECN_PERMIT) 16755a17b6adSMichael Tuexen ti->tcpi_options |= TCPI_OPT_ECN; 16761baaf834SBruce M Simpson 167743d94734SJohn Baldwin ti->tcpi_rto = tp->t_rxtcur * tick; 16783ac12506SJonathan T. Looney ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick; 16791baaf834SBruce M Simpson ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 16801baaf834SBruce M Simpson ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT; 16811baaf834SBruce M Simpson 1682b8af5dfaSRobert Watson ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1683b8af5dfaSRobert Watson ti->tcpi_snd_cwnd = tp->snd_cwnd; 1684b8af5dfaSRobert Watson 1685b8af5dfaSRobert Watson /* 1686b8af5dfaSRobert Watson * FreeBSD-specific extension fields for tcp_info. 1687b8af5dfaSRobert Watson */ 1688c8443a1dSRobert Watson ti->tcpi_rcv_space = tp->rcv_wnd; 1689535fbad6SKip Macy ti->tcpi_rcv_nxt = tp->rcv_nxt; 1690b8af5dfaSRobert Watson ti->tcpi_snd_wnd = tp->snd_wnd; 16911c18314dSAndre Oppermann ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */ 1692535fbad6SKip Macy ti->tcpi_snd_nxt = tp->snd_nxt; 169343d94734SJohn Baldwin ti->tcpi_snd_mss = tp->t_maxseg; 169443d94734SJohn Baldwin ti->tcpi_rcv_mss = tp->t_maxseg; 1695f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack; 1696f5d34df5SGeorge V. Neville-Neil ti->tcpi_rcv_ooopack = tp->t_rcvoopack; 1697f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_zerowin = tp->t_sndzerowin; 1698a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD 1699a6456410SNavdeep Parhar if (tp->t_flags & TF_TOE) { 1700a6456410SNavdeep Parhar ti->tcpi_options |= TCPI_OPT_TOE; 1701a6456410SNavdeep Parhar tcp_offload_tcp_info(tp, ti); 1702a6456410SNavdeep Parhar } 1703a6456410SNavdeep Parhar #endif 1704b8af5dfaSRobert Watson } 1705b8af5dfaSRobert Watson 1706b8af5dfaSRobert Watson /* 17071e8f5ffaSRobert Watson * tcp_ctloutput() must drop the inpcb lock before performing copyin on 17081e8f5ffaSRobert Watson * socket option arguments. When it re-acquires the lock after the copy, it 17091e8f5ffaSRobert Watson * has to revalidate that the connection is still valid for the socket 17101e8f5ffaSRobert Watson * option. 1711cfe8b629SGarrett Wollman */ 1712bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \ 17138501a69cSRobert Watson INP_WLOCK(inp); \ 1714ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \ 17158501a69cSRobert Watson INP_WUNLOCK(inp); \ 1716bac5bedfSConrad Meyer cleanup; \ 17171e8f5ffaSRobert Watson return (ECONNRESET); \ 17181e8f5ffaSRobert Watson } \ 17191e8f5ffaSRobert Watson tp = intotcpcb(inp); \ 17201e8f5ffaSRobert Watson } while(0) 1721bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */) 17221e8f5ffaSRobert Watson 1723df8bae1dSRodney W. Grimes int 1724ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1725df8bae1dSRodney W. Grimes { 172655bceb1eSRandall Stewart int error; 1727df8bae1dSRodney W. Grimes struct inpcb *inp; 1728cfe8b629SGarrett Wollman struct tcpcb *tp; 172955bceb1eSRandall Stewart struct tcp_function_block *blk; 173055bceb1eSRandall Stewart struct tcp_function_set fsn; 1731df8bae1dSRodney W. Grimes 1732cfe8b629SGarrett Wollman error = 0; 1733df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1734623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 1735cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_TCP) { 1736fb59c426SYoshinobu Inoue #ifdef INET6 17375cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 1738fb59c426SYoshinobu Inoue error = ip6_ctloutput(so, sopt); 17395dff1c38SMichael Tuexen /* 17405dff1c38SMichael Tuexen * In case of the IPV6_USE_MIN_MTU socket option, 17415dff1c38SMichael Tuexen * the INC_IPV6MINMTU flag to announce a corresponding 17425dff1c38SMichael Tuexen * MSS during the initial handshake. 17435dff1c38SMichael Tuexen * If the TCP connection is not in the front states, 17445dff1c38SMichael Tuexen * just reduce the MSS being used. 17455dff1c38SMichael Tuexen * This avoids the sending of TCP segments which will 17465dff1c38SMichael Tuexen * be fragmented at the IPv6 layer. 17475dff1c38SMichael Tuexen */ 17485dff1c38SMichael Tuexen if ((error == 0) && 17495dff1c38SMichael Tuexen (sopt->sopt_dir == SOPT_SET) && 17505dff1c38SMichael Tuexen (sopt->sopt_level == IPPROTO_IPV6) && 17515dff1c38SMichael Tuexen (sopt->sopt_name == IPV6_USE_MIN_MTU)) { 17525dff1c38SMichael Tuexen INP_WLOCK(inp); 17535dff1c38SMichael Tuexen if ((inp->inp_flags & 17545dff1c38SMichael Tuexen (INP_TIMEWAIT | INP_DROPPED))) { 17555dff1c38SMichael Tuexen INP_WUNLOCK(inp); 17565dff1c38SMichael Tuexen return (ECONNRESET); 17575dff1c38SMichael Tuexen } 17585dff1c38SMichael Tuexen inp->inp_inc.inc_flags |= INC_IPV6MINMTU; 17595dff1c38SMichael Tuexen tp = intotcpcb(inp); 17605dff1c38SMichael Tuexen if ((tp->t_state >= TCPS_SYN_SENT) && 17615dff1c38SMichael Tuexen (inp->inp_inc.inc_flags & INC_ISIPV6)) { 17625dff1c38SMichael Tuexen struct ip6_pktopts *opt; 17635dff1c38SMichael Tuexen 17645dff1c38SMichael Tuexen opt = inp->in6p_outputopts; 17655dff1c38SMichael Tuexen if ((opt != NULL) && 17665dff1c38SMichael Tuexen (opt->ip6po_minmtu == 17675dff1c38SMichael Tuexen IP6PO_MINMTU_ALL)) { 17685dff1c38SMichael Tuexen if (tp->t_maxseg > TCP6_MSS) { 17695dff1c38SMichael Tuexen tp->t_maxseg = TCP6_MSS; 17705dff1c38SMichael Tuexen } 17715dff1c38SMichael Tuexen } 17725dff1c38SMichael Tuexen } 17735dff1c38SMichael Tuexen INP_WUNLOCK(inp); 17745dff1c38SMichael Tuexen } 1775b287c6c7SBjoern A. Zeeb } 1776fb59c426SYoshinobu Inoue #endif /* INET6 */ 1777b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1778b287c6c7SBjoern A. Zeeb else 1779b287c6c7SBjoern A. Zeeb #endif 1780b287c6c7SBjoern A. Zeeb #ifdef INET 1781b287c6c7SBjoern A. Zeeb { 1782cfe8b629SGarrett Wollman error = ip_ctloutput(so, sopt); 17831e8f5ffaSRobert Watson } 17841e8f5ffaSRobert Watson #endif 1785df8bae1dSRodney W. Grimes return (error); 1786df8bae1dSRodney W. Grimes } 178768cea2b1SJohn Baldwin INP_WLOCK(inp); 1788ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 17898501a69cSRobert Watson INP_WUNLOCK(inp); 17901e8f5ffaSRobert Watson return (ECONNRESET); 1791623dce13SRobert Watson } 179255bceb1eSRandall Stewart tp = intotcpcb(inp); 179355bceb1eSRandall Stewart /* 179455bceb1eSRandall Stewart * Protect the TCP option TCP_FUNCTION_BLK so 179555bceb1eSRandall Stewart * that a sub-function can *never* overwrite this. 179655bceb1eSRandall Stewart */ 179755bceb1eSRandall Stewart if ((sopt->sopt_dir == SOPT_SET) && 179855bceb1eSRandall Stewart (sopt->sopt_name == TCP_FUNCTION_BLK)) { 179955bceb1eSRandall Stewart INP_WUNLOCK(inp); 180055bceb1eSRandall Stewart error = sooptcopyin(sopt, &fsn, sizeof fsn, 180155bceb1eSRandall Stewart sizeof fsn); 180255bceb1eSRandall Stewart if (error) 180355bceb1eSRandall Stewart return (error); 180455bceb1eSRandall Stewart INP_WLOCK_RECHECK(inp); 180555bceb1eSRandall Stewart blk = find_and_ref_tcp_functions(&fsn); 180655bceb1eSRandall Stewart if (blk == NULL) { 180755bceb1eSRandall Stewart INP_WUNLOCK(inp); 180855bceb1eSRandall Stewart return (ENOENT); 180955bceb1eSRandall Stewart } 1810587d67c0SRandall Stewart if (tp->t_fb == blk) { 1811587d67c0SRandall Stewart /* You already have this */ 1812587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1813587d67c0SRandall Stewart INP_WUNLOCK(inp); 1814587d67c0SRandall Stewart return (0); 1815587d67c0SRandall Stewart } 1816587d67c0SRandall Stewart if (tp->t_state != TCPS_CLOSED) { 1817587d67c0SRandall Stewart /* 1818587d67c0SRandall Stewart * The user has advanced the state 1819587d67c0SRandall Stewart * past the initial point, we may not 1820587d67c0SRandall Stewart * be able to switch. 1821587d67c0SRandall Stewart */ 1822587d67c0SRandall Stewart if (blk->tfb_tcp_handoff_ok != NULL) { 1823587d67c0SRandall Stewart /* 1824587d67c0SRandall Stewart * Does the stack provide a 1825587d67c0SRandall Stewart * query mechanism, if so it may 1826587d67c0SRandall Stewart * still be possible? 1827587d67c0SRandall Stewart */ 1828587d67c0SRandall Stewart error = (*blk->tfb_tcp_handoff_ok)(tp); 1829c6c0be27SMichael Tuexen } else 1830c6c0be27SMichael Tuexen error = EINVAL; 1831587d67c0SRandall Stewart if (error) { 1832587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1833587d67c0SRandall Stewart INP_WUNLOCK(inp); 1834587d67c0SRandall Stewart return(error); 1835587d67c0SRandall Stewart } 1836587d67c0SRandall Stewart } 183755bceb1eSRandall Stewart if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) { 183855bceb1eSRandall Stewart refcount_release(&blk->tfb_refcnt); 183955bceb1eSRandall Stewart INP_WUNLOCK(inp); 184055bceb1eSRandall Stewart return (ENOENT); 184155bceb1eSRandall Stewart } 184255bceb1eSRandall Stewart /* 184355bceb1eSRandall Stewart * Release the old refcnt, the 1844587d67c0SRandall Stewart * lookup acquired a ref on the 1845587d67c0SRandall Stewart * new one already. 184655bceb1eSRandall Stewart */ 1847587d67c0SRandall Stewart if (tp->t_fb->tfb_tcp_fb_fini) { 1848587d67c0SRandall Stewart /* 1849587d67c0SRandall Stewart * Tell the stack to cleanup with 0 i.e. 1850587d67c0SRandall Stewart * the tcb is not going away. 1851587d67c0SRandall Stewart */ 1852587d67c0SRandall Stewart (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0); 1853587d67c0SRandall Stewart } 18543ee9c3c4SRandall Stewart #ifdef TCPHPTS 18553ee9c3c4SRandall Stewart /* Assure that we are not on any hpts */ 18563ee9c3c4SRandall Stewart tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL); 18573ee9c3c4SRandall Stewart #endif 18583ee9c3c4SRandall Stewart if (blk->tfb_tcp_fb_init) { 18593ee9c3c4SRandall Stewart error = (*blk->tfb_tcp_fb_init)(tp); 18603ee9c3c4SRandall Stewart if (error) { 18613ee9c3c4SRandall Stewart refcount_release(&blk->tfb_refcnt); 18623ee9c3c4SRandall Stewart if (tp->t_fb->tfb_tcp_fb_init) { 18633ee9c3c4SRandall Stewart if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) { 18643ee9c3c4SRandall Stewart /* Fall back failed, drop the connection */ 18653ee9c3c4SRandall Stewart INP_WUNLOCK(inp); 18663ee9c3c4SRandall Stewart soabort(so); 18673ee9c3c4SRandall Stewart return(error); 18683ee9c3c4SRandall Stewart } 18693ee9c3c4SRandall Stewart } 18703ee9c3c4SRandall Stewart goto err_out; 18713ee9c3c4SRandall Stewart } 18723ee9c3c4SRandall Stewart } 187355bceb1eSRandall Stewart refcount_release(&tp->t_fb->tfb_refcnt); 187455bceb1eSRandall Stewart tp->t_fb = blk; 187555bceb1eSRandall Stewart #ifdef TCP_OFFLOAD 187655bceb1eSRandall Stewart if (tp->t_flags & TF_TOE) { 187755bceb1eSRandall Stewart tcp_offload_ctloutput(tp, sopt->sopt_dir, 187855bceb1eSRandall Stewart sopt->sopt_name); 187955bceb1eSRandall Stewart } 188055bceb1eSRandall Stewart #endif 18813ee9c3c4SRandall Stewart err_out: 188255bceb1eSRandall Stewart INP_WUNLOCK(inp); 188355bceb1eSRandall Stewart return (error); 188455bceb1eSRandall Stewart } else if ((sopt->sopt_dir == SOPT_GET) && 188555bceb1eSRandall Stewart (sopt->sopt_name == TCP_FUNCTION_BLK)) { 1886c73b6f4dSEd Maste strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name, 1887c73b6f4dSEd Maste TCP_FUNCTION_NAME_LEN_MAX); 1888c73b6f4dSEd Maste fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0'; 188955bceb1eSRandall Stewart fsn.pcbcnt = tp->t_fb->tfb_refcnt; 189055bceb1eSRandall Stewart INP_WUNLOCK(inp); 189155bceb1eSRandall Stewart error = sooptcopyout(sopt, &fsn, sizeof fsn); 189255bceb1eSRandall Stewart return (error); 189355bceb1eSRandall Stewart } 189455bceb1eSRandall Stewart /* Pass in the INP locked, called must unlock it */ 189555bceb1eSRandall Stewart return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp)); 189655bceb1eSRandall Stewart } 189755bceb1eSRandall Stewart 18982529f56eSJonathan T. Looney /* 18992529f56eSJonathan T. Looney * If this assert becomes untrue, we need to change the size of the buf 19002529f56eSJonathan T. Looney * variable in tcp_default_ctloutput(). 19012529f56eSJonathan T. Looney */ 19022529f56eSJonathan T. Looney #ifdef CTASSERT 19032529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN); 19042529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN); 19052529f56eSJonathan T. Looney #endif 19062529f56eSJonathan T. Looney 1907ec1db6e1SJohn Baldwin #ifdef KERN_TLS 1908ec1db6e1SJohn Baldwin static int 1909ec1db6e1SJohn Baldwin copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls) 1910ec1db6e1SJohn Baldwin { 1911ec1db6e1SJohn Baldwin struct tls_enable_v0 tls_v0; 1912ec1db6e1SJohn Baldwin int error; 1913ec1db6e1SJohn Baldwin 1914ec1db6e1SJohn Baldwin if (sopt->sopt_valsize == sizeof(tls_v0)) { 1915ec1db6e1SJohn Baldwin error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0), 1916ec1db6e1SJohn Baldwin sizeof(tls_v0)); 1917ec1db6e1SJohn Baldwin if (error) 1918ec1db6e1SJohn Baldwin return (error); 1919ec1db6e1SJohn Baldwin memset(tls, 0, sizeof(*tls)); 1920ec1db6e1SJohn Baldwin tls->cipher_key = tls_v0.cipher_key; 1921ec1db6e1SJohn Baldwin tls->iv = tls_v0.iv; 1922ec1db6e1SJohn Baldwin tls->auth_key = tls_v0.auth_key; 1923ec1db6e1SJohn Baldwin tls->cipher_algorithm = tls_v0.cipher_algorithm; 1924ec1db6e1SJohn Baldwin tls->cipher_key_len = tls_v0.cipher_key_len; 1925ec1db6e1SJohn Baldwin tls->iv_len = tls_v0.iv_len; 1926ec1db6e1SJohn Baldwin tls->auth_algorithm = tls_v0.auth_algorithm; 1927ec1db6e1SJohn Baldwin tls->auth_key_len = tls_v0.auth_key_len; 1928ec1db6e1SJohn Baldwin tls->flags = tls_v0.flags; 1929ec1db6e1SJohn Baldwin tls->tls_vmajor = tls_v0.tls_vmajor; 1930ec1db6e1SJohn Baldwin tls->tls_vminor = tls_v0.tls_vminor; 1931ec1db6e1SJohn Baldwin return (0); 1932ec1db6e1SJohn Baldwin } 1933ec1db6e1SJohn Baldwin 1934ec1db6e1SJohn Baldwin return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls))); 1935ec1db6e1SJohn Baldwin } 1936ec1db6e1SJohn Baldwin #endif 1937ec1db6e1SJohn Baldwin 193855bceb1eSRandall Stewart int 193955bceb1eSRandall Stewart tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp) 194055bceb1eSRandall Stewart { 194155bceb1eSRandall Stewart int error, opt, optval; 194255bceb1eSRandall Stewart u_int ui; 194355bceb1eSRandall Stewart struct tcp_info ti; 1944b2e60773SJohn Baldwin #ifdef KERN_TLS 1945b2e60773SJohn Baldwin struct tls_enable tls; 1946b2e60773SJohn Baldwin #endif 194755bceb1eSRandall Stewart struct cc_algo *algo; 19482529f56eSJonathan T. Looney char *pbuf, buf[TCP_LOG_ID_LEN]; 1949adc56f5aSEdward Tomasz Napierala #ifdef STATS 1950adc56f5aSEdward Tomasz Napierala struct statsblob *sbp; 1951adc56f5aSEdward Tomasz Napierala #endif 1952af6fef3aSGleb Smirnoff size_t len; 1953df8bae1dSRodney W. Grimes 1954d519cedbSGleb Smirnoff /* 1955d519cedbSGleb Smirnoff * For TCP_CCALGOOPT forward the control to CC module, for both 1956d519cedbSGleb Smirnoff * SOPT_SET and SOPT_GET. 1957d519cedbSGleb Smirnoff */ 1958d519cedbSGleb Smirnoff switch (sopt->sopt_name) { 1959d519cedbSGleb Smirnoff case TCP_CCALGOOPT: 1960d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 1961c8b53cedSMichael Tuexen if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT) 1962c8b53cedSMichael Tuexen return (EINVAL); 1963af6fef3aSGleb Smirnoff pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO); 1964af6fef3aSGleb Smirnoff error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize, 1965d519cedbSGleb Smirnoff sopt->sopt_valsize); 1966d519cedbSGleb Smirnoff if (error) { 1967af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 1968d519cedbSGleb Smirnoff return (error); 1969d519cedbSGleb Smirnoff } 1970bac5bedfSConrad Meyer INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP)); 1971d519cedbSGleb Smirnoff if (CC_ALGO(tp)->ctl_output != NULL) 1972af6fef3aSGleb Smirnoff error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf); 1973d519cedbSGleb Smirnoff else 1974d519cedbSGleb Smirnoff error = ENOENT; 1975d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 1976d519cedbSGleb Smirnoff if (error == 0 && sopt->sopt_dir == SOPT_GET) 1977af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize); 1978af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 1979d519cedbSGleb Smirnoff return (error); 1980d519cedbSGleb Smirnoff } 1981d519cedbSGleb Smirnoff 1982cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 1983cfe8b629SGarrett Wollman case SOPT_SET: 1984cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1985fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 198688f6b043SBruce M Simpson case TCP_MD5SIG: 1987fcf59617SAndrey V. Elsukov if (!TCPMD5_ENABLED()) { 19888501a69cSRobert Watson INP_WUNLOCK(inp); 1989fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 1990fcf59617SAndrey V. Elsukov } 1991fcf59617SAndrey V. Elsukov error = TCPMD5_PCBCTL(inp, sopt); 19921cfd4b53SBruce M Simpson if (error) 19931e8f5ffaSRobert Watson return (error); 199409fe6320SNavdeep Parhar goto unlock_and_done; 1995fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 199609fe6320SNavdeep Parhar 1997df8bae1dSRodney W. Grimes case TCP_NODELAY: 1998cfe8b629SGarrett Wollman case TCP_NOOPT: 19998501a69cSRobert Watson INP_WUNLOCK(inp); 2000cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 2001cfe8b629SGarrett Wollman sizeof optval); 2002cfe8b629SGarrett Wollman if (error) 20031e8f5ffaSRobert Watson return (error); 2004cfe8b629SGarrett Wollman 20058501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 2006cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2007cfe8b629SGarrett Wollman case TCP_NODELAY: 2008cfe8b629SGarrett Wollman opt = TF_NODELAY; 2009cfe8b629SGarrett Wollman break; 2010cfe8b629SGarrett Wollman case TCP_NOOPT: 2011cfe8b629SGarrett Wollman opt = TF_NOOPT; 2012cfe8b629SGarrett Wollman break; 2013cfe8b629SGarrett Wollman default: 2014cfe8b629SGarrett Wollman opt = 0; /* dead code to fool gcc */ 2015cfe8b629SGarrett Wollman break; 2016cfe8b629SGarrett Wollman } 2017cfe8b629SGarrett Wollman 2018cfe8b629SGarrett Wollman if (optval) 2019cfe8b629SGarrett Wollman tp->t_flags |= opt; 2020df8bae1dSRodney W. Grimes else 2021cfe8b629SGarrett Wollman tp->t_flags &= ~opt; 202209fe6320SNavdeep Parhar unlock_and_done: 202309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 202409fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 202509fe6320SNavdeep Parhar tcp_offload_ctloutput(tp, sopt->sopt_dir, 202609fe6320SNavdeep Parhar sopt->sopt_name); 202709fe6320SNavdeep Parhar } 202809fe6320SNavdeep Parhar #endif 20298501a69cSRobert Watson INP_WUNLOCK(inp); 2030df8bae1dSRodney W. Grimes break; 2031df8bae1dSRodney W. Grimes 2032007581c0SJonathan Lemon case TCP_NOPUSH: 20338501a69cSRobert Watson INP_WUNLOCK(inp); 2034007581c0SJonathan Lemon error = sooptcopyin(sopt, &optval, sizeof optval, 2035007581c0SJonathan Lemon sizeof optval); 2036007581c0SJonathan Lemon if (error) 20371e8f5ffaSRobert Watson return (error); 2038007581c0SJonathan Lemon 20398501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 2040007581c0SJonathan Lemon if (optval) 2041007581c0SJonathan Lemon tp->t_flags |= TF_NOPUSH; 2042d28b9e89SJohn Baldwin else if (tp->t_flags & TF_NOPUSH) { 2043007581c0SJonathan Lemon tp->t_flags &= ~TF_NOPUSH; 2044109eb549SGleb Smirnoff if (TCPS_HAVEESTABLISHED(tp->t_state)) { 2045109eb549SGleb Smirnoff struct epoch_tracker et; 2046109eb549SGleb Smirnoff 2047109eb549SGleb Smirnoff NET_EPOCH_ENTER(et); 204855bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 2049109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 2050109eb549SGleb Smirnoff } 2051007581c0SJonathan Lemon } 205209fe6320SNavdeep Parhar goto unlock_and_done; 2053007581c0SJonathan Lemon 2054df8bae1dSRodney W. Grimes case TCP_MAXSEG: 20558501a69cSRobert Watson INP_WUNLOCK(inp); 2056cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 2057cfe8b629SGarrett Wollman sizeof optval); 2058cfe8b629SGarrett Wollman if (error) 20591e8f5ffaSRobert Watson return (error); 2060df8bae1dSRodney W. Grimes 20618501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 206253369ac9SAndre Oppermann if (optval > 0 && optval <= tp->t_maxseg && 2063603724d3SBjoern A. Zeeb optval + 40 >= V_tcp_minmss) 2064cfe8b629SGarrett Wollman tp->t_maxseg = optval; 2065a0292f23SGarrett Wollman else 2066a0292f23SGarrett Wollman error = EINVAL; 206709fe6320SNavdeep Parhar goto unlock_and_done; 2068a0292f23SGarrett Wollman 2069b8af5dfaSRobert Watson case TCP_INFO: 20708501a69cSRobert Watson INP_WUNLOCK(inp); 2071b8af5dfaSRobert Watson error = EINVAL; 2072b8af5dfaSRobert Watson break; 2073b8af5dfaSRobert Watson 2074adc56f5aSEdward Tomasz Napierala case TCP_STATS: 2075adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2076adc56f5aSEdward Tomasz Napierala #ifdef STATS 2077adc56f5aSEdward Tomasz Napierala error = sooptcopyin(sopt, &optval, sizeof optval, 2078adc56f5aSEdward Tomasz Napierala sizeof optval); 2079adc56f5aSEdward Tomasz Napierala if (error) 2080adc56f5aSEdward Tomasz Napierala return (error); 2081adc56f5aSEdward Tomasz Napierala 2082adc56f5aSEdward Tomasz Napierala if (optval > 0) 2083adc56f5aSEdward Tomasz Napierala sbp = stats_blob_alloc( 2084adc56f5aSEdward Tomasz Napierala V_tcp_perconn_stats_dflt_tpl, 0); 2085adc56f5aSEdward Tomasz Napierala else 2086adc56f5aSEdward Tomasz Napierala sbp = NULL; 2087adc56f5aSEdward Tomasz Napierala 2088adc56f5aSEdward Tomasz Napierala INP_WLOCK_RECHECK(inp); 2089adc56f5aSEdward Tomasz Napierala if ((tp->t_stats != NULL && sbp == NULL) || 2090adc56f5aSEdward Tomasz Napierala (tp->t_stats == NULL && sbp != NULL)) { 2091adc56f5aSEdward Tomasz Napierala struct statsblob *t = tp->t_stats; 2092adc56f5aSEdward Tomasz Napierala tp->t_stats = sbp; 2093adc56f5aSEdward Tomasz Napierala sbp = t; 2094adc56f5aSEdward Tomasz Napierala } 2095adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2096adc56f5aSEdward Tomasz Napierala 2097adc56f5aSEdward Tomasz Napierala stats_blob_destroy(sbp); 2098adc56f5aSEdward Tomasz Napierala #else 2099adc56f5aSEdward Tomasz Napierala return (EOPNOTSUPP); 2100adc56f5aSEdward Tomasz Napierala #endif /* !STATS */ 2101adc56f5aSEdward Tomasz Napierala break; 2102adc56f5aSEdward Tomasz Napierala 2103dbc42409SLawrence Stewart case TCP_CONGESTION: 2104dbc42409SLawrence Stewart INP_WUNLOCK(inp); 2105af6fef3aSGleb Smirnoff error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1); 2106af6fef3aSGleb Smirnoff if (error) 2107dbc42409SLawrence Stewart break; 2108af6fef3aSGleb Smirnoff buf[sopt->sopt_valsize] = '\0'; 2109af6fef3aSGleb Smirnoff INP_WLOCK_RECHECK(inp); 211073e263b1SGleb Smirnoff CC_LIST_RLOCK(); 211173e263b1SGleb Smirnoff STAILQ_FOREACH(algo, &cc_list, entries) 211273e263b1SGleb Smirnoff if (strncmp(buf, algo->name, 211373e263b1SGleb Smirnoff TCP_CA_NAME_MAX) == 0) 211473e263b1SGleb Smirnoff break; 211573e263b1SGleb Smirnoff CC_LIST_RUNLOCK(); 211673e263b1SGleb Smirnoff if (algo == NULL) { 2117af6fef3aSGleb Smirnoff INP_WUNLOCK(inp); 211873e263b1SGleb Smirnoff error = EINVAL; 211973e263b1SGleb Smirnoff break; 212073e263b1SGleb Smirnoff } 2121dbc42409SLawrence Stewart /* 212273e263b1SGleb Smirnoff * We hold a write lock over the tcb so it's safe to 212373e263b1SGleb Smirnoff * do these things without ordering concerns. 2124dbc42409SLawrence Stewart */ 2125dbc42409SLawrence Stewart if (CC_ALGO(tp)->cb_destroy != NULL) 2126dbc42409SLawrence Stewart CC_ALGO(tp)->cb_destroy(tp->ccv); 212722699887SMatt Macy CC_DATA(tp) = NULL; 2128dbc42409SLawrence Stewart CC_ALGO(tp) = algo; 2129dbc42409SLawrence Stewart /* 213073e263b1SGleb Smirnoff * If something goes pear shaped initialising the new 213173e263b1SGleb Smirnoff * algo, fall back to newreno (which does not 213273e263b1SGleb Smirnoff * require initialisation). 2133dbc42409SLawrence Stewart */ 213473e263b1SGleb Smirnoff if (algo->cb_init != NULL && 213573e263b1SGleb Smirnoff algo->cb_init(tp->ccv) != 0) { 2136dbc42409SLawrence Stewart CC_ALGO(tp) = &newreno_cc_algo; 2137dbc42409SLawrence Stewart /* 213873e263b1SGleb Smirnoff * The only reason init should fail is 2139dbc42409SLawrence Stewart * because of malloc. 2140dbc42409SLawrence Stewart */ 2141dbc42409SLawrence Stewart error = ENOMEM; 2142dbc42409SLawrence Stewart } 214373e263b1SGleb Smirnoff INP_WUNLOCK(inp); 214473e263b1SGleb Smirnoff break; 2145dbc42409SLawrence Stewart 2146b2e60773SJohn Baldwin #ifdef KERN_TLS 2147b2e60773SJohn Baldwin case TCP_TXTLS_ENABLE: 2148b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2149ec1db6e1SJohn Baldwin error = copyin_tls_enable(sopt, &tls); 2150b2e60773SJohn Baldwin if (error) 2151b2e60773SJohn Baldwin break; 2152b2e60773SJohn Baldwin error = ktls_enable_tx(so, &tls); 2153b2e60773SJohn Baldwin break; 2154b2e60773SJohn Baldwin case TCP_TXTLS_MODE: 2155b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2156b2e60773SJohn Baldwin error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 2157b2e60773SJohn Baldwin if (error) 2158b2e60773SJohn Baldwin return (error); 2159b2e60773SJohn Baldwin 2160b2e60773SJohn Baldwin INP_WLOCK_RECHECK(inp); 2161b2e60773SJohn Baldwin error = ktls_set_tx_mode(so, ui); 2162b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2163b2e60773SJohn Baldwin break; 2164f1f93475SJohn Baldwin case TCP_RXTLS_ENABLE: 2165f1f93475SJohn Baldwin INP_WUNLOCK(inp); 2166f1f93475SJohn Baldwin error = sooptcopyin(sopt, &tls, sizeof(tls), 2167f1f93475SJohn Baldwin sizeof(tls)); 2168f1f93475SJohn Baldwin if (error) 2169f1f93475SJohn Baldwin break; 2170f1f93475SJohn Baldwin error = ktls_enable_rx(so, &tls); 2171f1f93475SJohn Baldwin break; 2172b2e60773SJohn Baldwin #endif 2173b2e60773SJohn Baldwin 21749077f387SGleb Smirnoff case TCP_KEEPIDLE: 21759077f387SGleb Smirnoff case TCP_KEEPINTVL: 21769077f387SGleb Smirnoff case TCP_KEEPINIT: 21779077f387SGleb Smirnoff INP_WUNLOCK(inp); 21789077f387SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 21799077f387SGleb Smirnoff if (error) 21809077f387SGleb Smirnoff return (error); 21819077f387SGleb Smirnoff 21829077f387SGleb Smirnoff if (ui > (UINT_MAX / hz)) { 21839077f387SGleb Smirnoff error = EINVAL; 21849077f387SGleb Smirnoff break; 21859077f387SGleb Smirnoff } 21869077f387SGleb Smirnoff ui *= hz; 21879077f387SGleb Smirnoff 21889077f387SGleb Smirnoff INP_WLOCK_RECHECK(inp); 21899077f387SGleb Smirnoff switch (sopt->sopt_name) { 21909077f387SGleb Smirnoff case TCP_KEEPIDLE: 21919077f387SGleb Smirnoff tp->t_keepidle = ui; 21929077f387SGleb Smirnoff /* 21939077f387SGleb Smirnoff * XXX: better check current remaining 21949077f387SGleb Smirnoff * timeout and "merge" it with new value. 21959077f387SGleb Smirnoff */ 21969077f387SGleb Smirnoff if ((tp->t_state > TCPS_LISTEN) && 21979077f387SGleb Smirnoff (tp->t_state <= TCPS_CLOSING)) 21989077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 21999077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 22009077f387SGleb Smirnoff break; 22019077f387SGleb Smirnoff case TCP_KEEPINTVL: 22029077f387SGleb Smirnoff tp->t_keepintvl = ui; 22039077f387SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 22049077f387SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 22059077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 22069077f387SGleb Smirnoff TP_MAXIDLE(tp)); 22079077f387SGleb Smirnoff break; 22089077f387SGleb Smirnoff case TCP_KEEPINIT: 22099077f387SGleb Smirnoff tp->t_keepinit = ui; 22109077f387SGleb Smirnoff if (tp->t_state == TCPS_SYN_RECEIVED || 22119077f387SGleb Smirnoff tp->t_state == TCPS_SYN_SENT) 22129077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 22139077f387SGleb Smirnoff TP_KEEPINIT(tp)); 22149077f387SGleb Smirnoff break; 22159077f387SGleb Smirnoff } 221609fe6320SNavdeep Parhar goto unlock_and_done; 22179077f387SGleb Smirnoff 221885c05144SGleb Smirnoff case TCP_KEEPCNT: 221985c05144SGleb Smirnoff INP_WUNLOCK(inp); 222085c05144SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 222185c05144SGleb Smirnoff if (error) 222285c05144SGleb Smirnoff return (error); 222385c05144SGleb Smirnoff 222485c05144SGleb Smirnoff INP_WLOCK_RECHECK(inp); 222585c05144SGleb Smirnoff tp->t_keepcnt = ui; 222685c05144SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 222785c05144SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 222885c05144SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 222985c05144SGleb Smirnoff TP_MAXIDLE(tp)); 223085c05144SGleb Smirnoff goto unlock_and_done; 223185c05144SGleb Smirnoff 223286a996e6SHiren Panchasara #ifdef TCPPCAP 223386a996e6SHiren Panchasara case TCP_PCAP_OUT: 223486a996e6SHiren Panchasara case TCP_PCAP_IN: 223586a996e6SHiren Panchasara INP_WUNLOCK(inp); 223686a996e6SHiren Panchasara error = sooptcopyin(sopt, &optval, sizeof optval, 223786a996e6SHiren Panchasara sizeof optval); 223886a996e6SHiren Panchasara if (error) 223986a996e6SHiren Panchasara return (error); 224086a996e6SHiren Panchasara 224186a996e6SHiren Panchasara INP_WLOCK_RECHECK(inp); 224286a996e6SHiren Panchasara if (optval >= 0) 224386a996e6SHiren Panchasara tcp_pcap_set_sock_max(TCP_PCAP_OUT ? 224486a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts), 224586a996e6SHiren Panchasara optval); 224686a996e6SHiren Panchasara else 224786a996e6SHiren Panchasara error = EINVAL; 224886a996e6SHiren Panchasara goto unlock_and_done; 224986a996e6SHiren Panchasara #endif 225086a996e6SHiren Panchasara 2251c560df6fSPatrick Kelsey case TCP_FASTOPEN: { 2252c560df6fSPatrick Kelsey struct tcp_fastopen tfo_optval; 2253c560df6fSPatrick Kelsey 2254281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 2255c560df6fSPatrick Kelsey if (!V_tcp_fastopen_client_enable && 2256c560df6fSPatrick Kelsey !V_tcp_fastopen_server_enable) 2257281a0fd4SPatrick Kelsey return (EPERM); 2258281a0fd4SPatrick Kelsey 2259c560df6fSPatrick Kelsey error = sooptcopyin(sopt, &tfo_optval, 2260c560df6fSPatrick Kelsey sizeof(tfo_optval), sizeof(int)); 2261281a0fd4SPatrick Kelsey if (error) 2262281a0fd4SPatrick Kelsey return (error); 2263281a0fd4SPatrick Kelsey 2264281a0fd4SPatrick Kelsey INP_WLOCK_RECHECK(inp); 2265d442a657SMichael Tuexen if ((tp->t_state != TCPS_CLOSED) && 2266d442a657SMichael Tuexen (tp->t_state != TCPS_LISTEN)) { 2267d442a657SMichael Tuexen error = EINVAL; 2268d442a657SMichael Tuexen goto unlock_and_done; 2269d442a657SMichael Tuexen } 2270c560df6fSPatrick Kelsey if (tfo_optval.enable) { 2271c560df6fSPatrick Kelsey if (tp->t_state == TCPS_LISTEN) { 2272c560df6fSPatrick Kelsey if (!V_tcp_fastopen_server_enable) { 2273c560df6fSPatrick Kelsey error = EPERM; 2274c560df6fSPatrick Kelsey goto unlock_and_done; 2275c560df6fSPatrick Kelsey } 2276c560df6fSPatrick Kelsey 2277c560df6fSPatrick Kelsey if (tp->t_tfo_pending == NULL) 2278281a0fd4SPatrick Kelsey tp->t_tfo_pending = 2279281a0fd4SPatrick Kelsey tcp_fastopen_alloc_counter(); 2280c560df6fSPatrick Kelsey } else { 2281c560df6fSPatrick Kelsey /* 2282c560df6fSPatrick Kelsey * If a pre-shared key was provided, 2283c560df6fSPatrick Kelsey * stash it in the client cookie 2284c560df6fSPatrick Kelsey * field of the tcpcb for use during 2285c560df6fSPatrick Kelsey * connect. 2286c560df6fSPatrick Kelsey */ 2287c560df6fSPatrick Kelsey if (sopt->sopt_valsize == 2288c560df6fSPatrick Kelsey sizeof(tfo_optval)) { 2289c560df6fSPatrick Kelsey memcpy(tp->t_tfo_cookie.client, 2290c560df6fSPatrick Kelsey tfo_optval.psk, 2291c560df6fSPatrick Kelsey TCP_FASTOPEN_PSK_LEN); 2292c560df6fSPatrick Kelsey tp->t_tfo_client_cookie_len = 2293c560df6fSPatrick Kelsey TCP_FASTOPEN_PSK_LEN; 2294c560df6fSPatrick Kelsey } 2295c560df6fSPatrick Kelsey } 2296d442a657SMichael Tuexen tp->t_flags |= TF_FASTOPEN; 2297281a0fd4SPatrick Kelsey } else 2298281a0fd4SPatrick Kelsey tp->t_flags &= ~TF_FASTOPEN; 2299281a0fd4SPatrick Kelsey goto unlock_and_done; 2300c560df6fSPatrick Kelsey } 2301281a0fd4SPatrick Kelsey 2302e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX 23032529f56eSJonathan T. Looney case TCP_LOG: 23042529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23052529f56eSJonathan T. Looney error = sooptcopyin(sopt, &optval, sizeof optval, 23062529f56eSJonathan T. Looney sizeof optval); 23072529f56eSJonathan T. Looney if (error) 23082529f56eSJonathan T. Looney return (error); 23092529f56eSJonathan T. Looney 23102529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 23112529f56eSJonathan T. Looney error = tcp_log_state_change(tp, optval); 23122529f56eSJonathan T. Looney goto unlock_and_done; 23132529f56eSJonathan T. Looney 23142529f56eSJonathan T. Looney case TCP_LOGBUF: 23152529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23162529f56eSJonathan T. Looney error = EINVAL; 23172529f56eSJonathan T. Looney break; 23182529f56eSJonathan T. Looney 23192529f56eSJonathan T. Looney case TCP_LOGID: 23202529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23212529f56eSJonathan T. Looney error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0); 23222529f56eSJonathan T. Looney if (error) 23232529f56eSJonathan T. Looney break; 23242529f56eSJonathan T. Looney buf[sopt->sopt_valsize] = '\0'; 23252529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 23262529f56eSJonathan T. Looney error = tcp_log_set_id(tp, buf); 23272529f56eSJonathan T. Looney /* tcp_log_set_id() unlocks the INP. */ 23282529f56eSJonathan T. Looney break; 23292529f56eSJonathan T. Looney 23302529f56eSJonathan T. Looney case TCP_LOGDUMP: 23312529f56eSJonathan T. Looney case TCP_LOGDUMPID: 23322529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23332529f56eSJonathan T. Looney error = 23342529f56eSJonathan T. Looney sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0); 23352529f56eSJonathan T. Looney if (error) 23362529f56eSJonathan T. Looney break; 23372529f56eSJonathan T. Looney buf[sopt->sopt_valsize] = '\0'; 23382529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 23392529f56eSJonathan T. Looney if (sopt->sopt_name == TCP_LOGDUMP) { 23402529f56eSJonathan T. Looney error = tcp_log_dump_tp_logbuf(tp, buf, 23412529f56eSJonathan T. Looney M_WAITOK, true); 23422529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23432529f56eSJonathan T. Looney } else { 23442529f56eSJonathan T. Looney tcp_log_dump_tp_bucket_logbufs(tp, buf); 23452529f56eSJonathan T. Looney /* 23462529f56eSJonathan T. Looney * tcp_log_dump_tp_bucket_logbufs() drops the 23472529f56eSJonathan T. Looney * INP lock. 23482529f56eSJonathan T. Looney */ 23492529f56eSJonathan T. Looney } 23502529f56eSJonathan T. Looney break; 2351e24e5683SJonathan T. Looney #endif 23522529f56eSJonathan T. Looney 2353df8bae1dSRodney W. Grimes default: 23548501a69cSRobert Watson INP_WUNLOCK(inp); 2355df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 2356df8bae1dSRodney W. Grimes break; 2357df8bae1dSRodney W. Grimes } 2358df8bae1dSRodney W. Grimes break; 2359df8bae1dSRodney W. Grimes 2360cfe8b629SGarrett Wollman case SOPT_GET: 23611e8f5ffaSRobert Watson tp = intotcpcb(inp); 2362cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2363fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 236488f6b043SBruce M Simpson case TCP_MD5SIG: 2365fcf59617SAndrey V. Elsukov if (!TCPMD5_ENABLED()) { 23668501a69cSRobert Watson INP_WUNLOCK(inp); 2367fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 2368fcf59617SAndrey V. Elsukov } 2369fcf59617SAndrey V. Elsukov error = TCPMD5_PCBCTL(inp, sopt); 23701cfd4b53SBruce M Simpson break; 2371265ed012SBruce M Simpson #endif 23721e8f5ffaSRobert Watson 2373df8bae1dSRodney W. Grimes case TCP_NODELAY: 2374cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NODELAY; 23758501a69cSRobert Watson INP_WUNLOCK(inp); 2376b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2377df8bae1dSRodney W. Grimes break; 2378df8bae1dSRodney W. Grimes case TCP_MAXSEG: 2379cfe8b629SGarrett Wollman optval = tp->t_maxseg; 23808501a69cSRobert Watson INP_WUNLOCK(inp); 2381b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2382df8bae1dSRodney W. Grimes break; 2383a0292f23SGarrett Wollman case TCP_NOOPT: 2384cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOOPT; 23858501a69cSRobert Watson INP_WUNLOCK(inp); 2386b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2387a0292f23SGarrett Wollman break; 2388a0292f23SGarrett Wollman case TCP_NOPUSH: 2389cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOPUSH; 23908501a69cSRobert Watson INP_WUNLOCK(inp); 2391b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2392b8af5dfaSRobert Watson break; 2393b8af5dfaSRobert Watson case TCP_INFO: 2394b8af5dfaSRobert Watson tcp_fill_info(tp, &ti); 23958501a69cSRobert Watson INP_WUNLOCK(inp); 2396b8af5dfaSRobert Watson error = sooptcopyout(sopt, &ti, sizeof ti); 2397a0292f23SGarrett Wollman break; 2398adc56f5aSEdward Tomasz Napierala case TCP_STATS: 2399adc56f5aSEdward Tomasz Napierala { 2400adc56f5aSEdward Tomasz Napierala #ifdef STATS 2401adc56f5aSEdward Tomasz Napierala int nheld; 2402adc56f5aSEdward Tomasz Napierala TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0; 2403adc56f5aSEdward Tomasz Napierala 2404adc56f5aSEdward Tomasz Napierala error = 0; 2405adc56f5aSEdward Tomasz Napierala socklen_t outsbsz = sopt->sopt_valsize; 2406adc56f5aSEdward Tomasz Napierala if (tp->t_stats == NULL) 2407adc56f5aSEdward Tomasz Napierala error = ENOENT; 2408adc56f5aSEdward Tomasz Napierala else if (outsbsz >= tp->t_stats->cursz) 2409adc56f5aSEdward Tomasz Napierala outsbsz = tp->t_stats->cursz; 2410adc56f5aSEdward Tomasz Napierala else if (outsbsz >= sizeof(struct statsblob)) 2411adc56f5aSEdward Tomasz Napierala outsbsz = sizeof(struct statsblob); 2412adc56f5aSEdward Tomasz Napierala else 2413adc56f5aSEdward Tomasz Napierala error = EINVAL; 2414adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2415adc56f5aSEdward Tomasz Napierala if (error) 2416adc56f5aSEdward Tomasz Napierala break; 2417adc56f5aSEdward Tomasz Napierala 2418adc56f5aSEdward Tomasz Napierala sbp = sopt->sopt_val; 2419adc56f5aSEdward Tomasz Napierala nheld = atop(round_page(((vm_offset_t)sbp) + 2420adc56f5aSEdward Tomasz Napierala (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp)); 2421adc56f5aSEdward Tomasz Napierala vm_page_t ma[nheld]; 2422adc56f5aSEdward Tomasz Napierala if (vm_fault_quick_hold_pages( 2423adc56f5aSEdward Tomasz Napierala &curproc->p_vmspace->vm_map, (vm_offset_t)sbp, 2424adc56f5aSEdward Tomasz Napierala outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma, 2425adc56f5aSEdward Tomasz Napierala nheld) < 0) { 2426adc56f5aSEdward Tomasz Napierala error = EFAULT; 2427adc56f5aSEdward Tomasz Napierala break; 2428adc56f5aSEdward Tomasz Napierala } 2429adc56f5aSEdward Tomasz Napierala 2430adc56f5aSEdward Tomasz Napierala if ((error = copyin_nofault(&(sbp->flags), &sbflags, 2431adc56f5aSEdward Tomasz Napierala SIZEOF_MEMBER(struct statsblob, flags)))) 2432adc56f5aSEdward Tomasz Napierala goto unhold; 2433adc56f5aSEdward Tomasz Napierala 2434adc56f5aSEdward Tomasz Napierala INP_WLOCK_RECHECK(inp); 2435adc56f5aSEdward Tomasz Napierala error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats, 2436adc56f5aSEdward Tomasz Napierala sbflags | SB_CLONE_USRDSTNOFAULT); 2437adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2438adc56f5aSEdward Tomasz Napierala sopt->sopt_valsize = outsbsz; 2439adc56f5aSEdward Tomasz Napierala unhold: 2440adc56f5aSEdward Tomasz Napierala vm_page_unhold_pages(ma, nheld); 2441adc56f5aSEdward Tomasz Napierala #else 2442adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2443adc56f5aSEdward Tomasz Napierala error = EOPNOTSUPP; 2444adc56f5aSEdward Tomasz Napierala #endif /* !STATS */ 2445adc56f5aSEdward Tomasz Napierala break; 2446adc56f5aSEdward Tomasz Napierala } 2447dbc42409SLawrence Stewart case TCP_CONGESTION: 2448af6fef3aSGleb Smirnoff len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); 2449dbc42409SLawrence Stewart INP_WUNLOCK(inp); 2450af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, buf, len + 1); 2451dbc42409SLawrence Stewart break; 24522f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 24532f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 24542f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 24552f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 24562f3eb7f4SGleb Smirnoff switch (sopt->sopt_name) { 24572f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 24585a17b6adSMichael Tuexen ui = TP_KEEPIDLE(tp) / hz; 24592f3eb7f4SGleb Smirnoff break; 24602f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 24615a17b6adSMichael Tuexen ui = TP_KEEPINTVL(tp) / hz; 24622f3eb7f4SGleb Smirnoff break; 24632f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 24645a17b6adSMichael Tuexen ui = TP_KEEPINIT(tp) / hz; 24652f3eb7f4SGleb Smirnoff break; 24662f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 24675a17b6adSMichael Tuexen ui = TP_KEEPCNT(tp); 24682f3eb7f4SGleb Smirnoff break; 24692f3eb7f4SGleb Smirnoff } 24702f3eb7f4SGleb Smirnoff INP_WUNLOCK(inp); 24712f3eb7f4SGleb Smirnoff error = sooptcopyout(sopt, &ui, sizeof(ui)); 24722f3eb7f4SGleb Smirnoff break; 247386a996e6SHiren Panchasara #ifdef TCPPCAP 247486a996e6SHiren Panchasara case TCP_PCAP_OUT: 247586a996e6SHiren Panchasara case TCP_PCAP_IN: 247686a996e6SHiren Panchasara optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ? 247786a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts)); 247886a996e6SHiren Panchasara INP_WUNLOCK(inp); 247986a996e6SHiren Panchasara error = sooptcopyout(sopt, &optval, sizeof optval); 248086a996e6SHiren Panchasara break; 248186a996e6SHiren Panchasara #endif 2482281a0fd4SPatrick Kelsey case TCP_FASTOPEN: 2483281a0fd4SPatrick Kelsey optval = tp->t_flags & TF_FASTOPEN; 2484281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 2485281a0fd4SPatrick Kelsey error = sooptcopyout(sopt, &optval, sizeof optval); 2486281a0fd4SPatrick Kelsey break; 2487e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX 24882529f56eSJonathan T. Looney case TCP_LOG: 24892529f56eSJonathan T. Looney optval = tp->t_logstate; 24902529f56eSJonathan T. Looney INP_WUNLOCK(inp); 24912529f56eSJonathan T. Looney error = sooptcopyout(sopt, &optval, sizeof(optval)); 24922529f56eSJonathan T. Looney break; 24932529f56eSJonathan T. Looney case TCP_LOGBUF: 24942529f56eSJonathan T. Looney /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */ 24952529f56eSJonathan T. Looney error = tcp_log_getlogbuf(sopt, tp); 24962529f56eSJonathan T. Looney break; 24972529f56eSJonathan T. Looney case TCP_LOGID: 24982529f56eSJonathan T. Looney len = tcp_log_get_id(tp, buf); 24992529f56eSJonathan T. Looney INP_WUNLOCK(inp); 25002529f56eSJonathan T. Looney error = sooptcopyout(sopt, buf, len + 1); 25012529f56eSJonathan T. Looney break; 25022529f56eSJonathan T. Looney case TCP_LOGDUMP: 25032529f56eSJonathan T. Looney case TCP_LOGDUMPID: 25042529f56eSJonathan T. Looney INP_WUNLOCK(inp); 25052529f56eSJonathan T. Looney error = EINVAL; 25062529f56eSJonathan T. Looney break; 2507e24e5683SJonathan T. Looney #endif 2508b2e60773SJohn Baldwin #ifdef KERN_TLS 2509b2e60773SJohn Baldwin case TCP_TXTLS_MODE: 2510b2e60773SJohn Baldwin optval = ktls_get_tx_mode(so); 2511b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2512b2e60773SJohn Baldwin error = sooptcopyout(sopt, &optval, sizeof(optval)); 2513b2e60773SJohn Baldwin break; 2514f1f93475SJohn Baldwin case TCP_RXTLS_MODE: 2515f1f93475SJohn Baldwin optval = ktls_get_rx_mode(so); 2516f1f93475SJohn Baldwin INP_WUNLOCK(inp); 2517f1f93475SJohn Baldwin error = sooptcopyout(sopt, &optval, sizeof(optval)); 2518f1f93475SJohn Baldwin break; 2519b2e60773SJohn Baldwin #endif 2520df8bae1dSRodney W. Grimes default: 25218501a69cSRobert Watson INP_WUNLOCK(inp); 2522df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 2523df8bae1dSRodney W. Grimes break; 2524df8bae1dSRodney W. Grimes } 2525df8bae1dSRodney W. Grimes break; 2526df8bae1dSRodney W. Grimes } 2527df8bae1dSRodney W. Grimes return (error); 2528df8bae1dSRodney W. Grimes } 25298501a69cSRobert Watson #undef INP_WLOCK_RECHECK 2530bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP 2531df8bae1dSRodney W. Grimes 253226e30fbbSDavid Greenman /* 2533df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 2534df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 2535df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 2536df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 2537df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 2538df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 2539df8bae1dSRodney W. Grimes */ 2540623dce13SRobert Watson static void 2541ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp) 2542df8bae1dSRodney W. Grimes { 2543e6e0b5ffSRobert Watson struct inpcb *inp = tp->t_inpcb; 2544e6e0b5ffSRobert Watson struct socket *so = inp->inp_socket; 2545e6e0b5ffSRobert Watson 254697a95ee1SGleb Smirnoff NET_EPOCH_ASSERT(); 25478501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2548df8bae1dSRodney W. Grimes 2549623dce13SRobert Watson /* 2550623dce13SRobert Watson * Neither tcp_close() nor tcp_drop() should return NULL, as the 2551623dce13SRobert Watson * socket is still open. 2552623dce13SRobert Watson */ 25538db239dcSMichael Tuexen if (tp->t_state < TCPS_ESTABLISHED && 25548db239dcSMichael Tuexen !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) { 2555df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2556623dce13SRobert Watson KASSERT(tp != NULL, 2557623dce13SRobert Watson ("tcp_disconnect: tcp_close() returned NULL")); 2558623dce13SRobert Watson } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 2559243917feSSeigo Tanimura tp = tcp_drop(tp, 0); 2560623dce13SRobert Watson KASSERT(tp != NULL, 2561623dce13SRobert Watson ("tcp_disconnect: tcp_drop() returned NULL")); 2562623dce13SRobert Watson } else { 2563df8bae1dSRodney W. Grimes soisdisconnecting(so); 2564df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 2565623dce13SRobert Watson tcp_usrclosed(tp); 2566ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 256755bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 2568df8bae1dSRodney W. Grimes } 2569df8bae1dSRodney W. Grimes } 2570df8bae1dSRodney W. Grimes 2571df8bae1dSRodney W. Grimes /* 2572df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 2573df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 2574df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 2575df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 2576df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 2577df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 2578df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 2579df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 2580df8bae1dSRodney W. Grimes */ 2581623dce13SRobert Watson static void 2582ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp) 2583df8bae1dSRodney W. Grimes { 2584df8bae1dSRodney W. Grimes 258597a95ee1SGleb Smirnoff NET_EPOCH_ASSERT(); 25868501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 2587e6e0b5ffSRobert Watson 2588df8bae1dSRodney W. Grimes switch (tp->t_state) { 2589df8bae1dSRodney W. Grimes case TCPS_LISTEN: 259009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 259109fe6320SNavdeep Parhar tcp_offload_listen_stop(tp); 259209fe6320SNavdeep Parhar #endif 2593550e9d42SHiren Panchasara tcp_state_change(tp, TCPS_CLOSED); 2594bc65987aSKip Macy /* FALLTHROUGH */ 2595bc65987aSKip Macy case TCPS_CLOSED: 2596df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2597623dce13SRobert Watson /* 2598623dce13SRobert Watson * tcp_close() should never return NULL here as the socket is 2599623dce13SRobert Watson * still open. 2600623dce13SRobert Watson */ 2601623dce13SRobert Watson KASSERT(tp != NULL, 2602623dce13SRobert Watson ("tcp_usrclosed: tcp_close() returned NULL")); 2603df8bae1dSRodney W. Grimes break; 2604df8bae1dSRodney W. Grimes 2605a0292f23SGarrett Wollman case TCPS_SYN_SENT: 2606df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2607a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 2608a0292f23SGarrett Wollman break; 2609a0292f23SGarrett Wollman 2610df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 261157f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2612df8bae1dSRodney W. Grimes break; 2613df8bae1dSRodney W. Grimes 2614df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 261557f60867SMark Johnston tcp_state_change(tp, TCPS_LAST_ACK); 2616df8bae1dSRodney W. Grimes break; 2617df8bae1dSRodney W. Grimes } 2618abc7d910SRobert Watson if (tp->t_state >= TCPS_FIN_WAIT_2) { 2619df8bae1dSRodney W. Grimes soisdisconnected(tp->t_inpcb->inp_socket); 2620abc7d910SRobert Watson /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 26217c72af87SMohan Srinivasan if (tp->t_state == TCPS_FIN_WAIT_2) { 26227c72af87SMohan Srinivasan int timeout; 26237c72af87SMohan Srinivasan 26247c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 26259077f387SGleb Smirnoff tcp_finwait2_timeout : TP_MAXIDLE(tp); 2626b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 2627b6239c4aSAndras Olah } 2628df8bae1dSRodney W. Grimes } 26297c72af87SMohan Srinivasan } 2630497057eeSRobert Watson 2631497057eeSRobert Watson #ifdef DDB 2632497057eeSRobert Watson static void 2633497057eeSRobert Watson db_print_indent(int indent) 2634497057eeSRobert Watson { 2635497057eeSRobert Watson int i; 2636497057eeSRobert Watson 2637497057eeSRobert Watson for (i = 0; i < indent; i++) 2638497057eeSRobert Watson db_printf(" "); 2639497057eeSRobert Watson } 2640497057eeSRobert Watson 2641497057eeSRobert Watson static void 2642497057eeSRobert Watson db_print_tstate(int t_state) 2643497057eeSRobert Watson { 2644497057eeSRobert Watson 2645497057eeSRobert Watson switch (t_state) { 2646497057eeSRobert Watson case TCPS_CLOSED: 2647497057eeSRobert Watson db_printf("TCPS_CLOSED"); 2648497057eeSRobert Watson return; 2649497057eeSRobert Watson 2650497057eeSRobert Watson case TCPS_LISTEN: 2651497057eeSRobert Watson db_printf("TCPS_LISTEN"); 2652497057eeSRobert Watson return; 2653497057eeSRobert Watson 2654497057eeSRobert Watson case TCPS_SYN_SENT: 2655497057eeSRobert Watson db_printf("TCPS_SYN_SENT"); 2656497057eeSRobert Watson return; 2657497057eeSRobert Watson 2658497057eeSRobert Watson case TCPS_SYN_RECEIVED: 2659497057eeSRobert Watson db_printf("TCPS_SYN_RECEIVED"); 2660497057eeSRobert Watson return; 2661497057eeSRobert Watson 2662497057eeSRobert Watson case TCPS_ESTABLISHED: 2663497057eeSRobert Watson db_printf("TCPS_ESTABLISHED"); 2664497057eeSRobert Watson return; 2665497057eeSRobert Watson 2666497057eeSRobert Watson case TCPS_CLOSE_WAIT: 2667497057eeSRobert Watson db_printf("TCPS_CLOSE_WAIT"); 2668497057eeSRobert Watson return; 2669497057eeSRobert Watson 2670497057eeSRobert Watson case TCPS_FIN_WAIT_1: 2671497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_1"); 2672497057eeSRobert Watson return; 2673497057eeSRobert Watson 2674497057eeSRobert Watson case TCPS_CLOSING: 2675497057eeSRobert Watson db_printf("TCPS_CLOSING"); 2676497057eeSRobert Watson return; 2677497057eeSRobert Watson 2678497057eeSRobert Watson case TCPS_LAST_ACK: 2679497057eeSRobert Watson db_printf("TCPS_LAST_ACK"); 2680497057eeSRobert Watson return; 2681497057eeSRobert Watson 2682497057eeSRobert Watson case TCPS_FIN_WAIT_2: 2683497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_2"); 2684497057eeSRobert Watson return; 2685497057eeSRobert Watson 2686497057eeSRobert Watson case TCPS_TIME_WAIT: 2687497057eeSRobert Watson db_printf("TCPS_TIME_WAIT"); 2688497057eeSRobert Watson return; 2689497057eeSRobert Watson 2690497057eeSRobert Watson default: 2691497057eeSRobert Watson db_printf("unknown"); 2692497057eeSRobert Watson return; 2693497057eeSRobert Watson } 2694497057eeSRobert Watson } 2695497057eeSRobert Watson 2696497057eeSRobert Watson static void 2697497057eeSRobert Watson db_print_tflags(u_int t_flags) 2698497057eeSRobert Watson { 2699497057eeSRobert Watson int comma; 2700497057eeSRobert Watson 2701497057eeSRobert Watson comma = 0; 2702497057eeSRobert Watson if (t_flags & TF_ACKNOW) { 2703497057eeSRobert Watson db_printf("%sTF_ACKNOW", comma ? ", " : ""); 2704497057eeSRobert Watson comma = 1; 2705497057eeSRobert Watson } 2706497057eeSRobert Watson if (t_flags & TF_DELACK) { 2707497057eeSRobert Watson db_printf("%sTF_DELACK", comma ? ", " : ""); 2708497057eeSRobert Watson comma = 1; 2709497057eeSRobert Watson } 2710497057eeSRobert Watson if (t_flags & TF_NODELAY) { 2711497057eeSRobert Watson db_printf("%sTF_NODELAY", comma ? ", " : ""); 2712497057eeSRobert Watson comma = 1; 2713497057eeSRobert Watson } 2714497057eeSRobert Watson if (t_flags & TF_NOOPT) { 2715497057eeSRobert Watson db_printf("%sTF_NOOPT", comma ? ", " : ""); 2716497057eeSRobert Watson comma = 1; 2717497057eeSRobert Watson } 2718497057eeSRobert Watson if (t_flags & TF_SENTFIN) { 2719497057eeSRobert Watson db_printf("%sTF_SENTFIN", comma ? ", " : ""); 2720497057eeSRobert Watson comma = 1; 2721497057eeSRobert Watson } 2722497057eeSRobert Watson if (t_flags & TF_REQ_SCALE) { 2723497057eeSRobert Watson db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 2724497057eeSRobert Watson comma = 1; 2725497057eeSRobert Watson } 2726497057eeSRobert Watson if (t_flags & TF_RCVD_SCALE) { 2727497057eeSRobert Watson db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 2728497057eeSRobert Watson comma = 1; 2729497057eeSRobert Watson } 2730497057eeSRobert Watson if (t_flags & TF_REQ_TSTMP) { 2731497057eeSRobert Watson db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 2732497057eeSRobert Watson comma = 1; 2733497057eeSRobert Watson } 2734497057eeSRobert Watson if (t_flags & TF_RCVD_TSTMP) { 2735497057eeSRobert Watson db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 2736497057eeSRobert Watson comma = 1; 2737497057eeSRobert Watson } 2738497057eeSRobert Watson if (t_flags & TF_SACK_PERMIT) { 2739497057eeSRobert Watson db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 2740497057eeSRobert Watson comma = 1; 2741497057eeSRobert Watson } 2742497057eeSRobert Watson if (t_flags & TF_NEEDSYN) { 2743497057eeSRobert Watson db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 2744497057eeSRobert Watson comma = 1; 2745497057eeSRobert Watson } 2746497057eeSRobert Watson if (t_flags & TF_NEEDFIN) { 2747497057eeSRobert Watson db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 2748497057eeSRobert Watson comma = 1; 2749497057eeSRobert Watson } 2750497057eeSRobert Watson if (t_flags & TF_NOPUSH) { 2751497057eeSRobert Watson db_printf("%sTF_NOPUSH", comma ? ", " : ""); 2752497057eeSRobert Watson comma = 1; 2753497057eeSRobert Watson } 2754497057eeSRobert Watson if (t_flags & TF_MORETOCOME) { 2755497057eeSRobert Watson db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 2756497057eeSRobert Watson comma = 1; 2757497057eeSRobert Watson } 2758497057eeSRobert Watson if (t_flags & TF_LQ_OVERFLOW) { 2759497057eeSRobert Watson db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : ""); 2760497057eeSRobert Watson comma = 1; 2761497057eeSRobert Watson } 2762497057eeSRobert Watson if (t_flags & TF_LASTIDLE) { 2763497057eeSRobert Watson db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 2764497057eeSRobert Watson comma = 1; 2765497057eeSRobert Watson } 2766497057eeSRobert Watson if (t_flags & TF_RXWIN0SENT) { 2767497057eeSRobert Watson db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 2768497057eeSRobert Watson comma = 1; 2769497057eeSRobert Watson } 2770497057eeSRobert Watson if (t_flags & TF_FASTRECOVERY) { 2771497057eeSRobert Watson db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 2772497057eeSRobert Watson comma = 1; 2773497057eeSRobert Watson } 2774dbc42409SLawrence Stewart if (t_flags & TF_CONGRECOVERY) { 2775dbc42409SLawrence Stewart db_printf("%sTF_CONGRECOVERY", comma ? ", " : ""); 2776dbc42409SLawrence Stewart comma = 1; 2777dbc42409SLawrence Stewart } 2778497057eeSRobert Watson if (t_flags & TF_WASFRECOVERY) { 2779497057eeSRobert Watson db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 2780497057eeSRobert Watson comma = 1; 2781497057eeSRobert Watson } 2782497057eeSRobert Watson if (t_flags & TF_SIGNATURE) { 2783497057eeSRobert Watson db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 2784497057eeSRobert Watson comma = 1; 2785497057eeSRobert Watson } 2786497057eeSRobert Watson if (t_flags & TF_FORCEDATA) { 2787497057eeSRobert Watson db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 2788497057eeSRobert Watson comma = 1; 2789497057eeSRobert Watson } 2790497057eeSRobert Watson if (t_flags & TF_TSO) { 2791497057eeSRobert Watson db_printf("%sTF_TSO", comma ? ", " : ""); 2792497057eeSRobert Watson comma = 1; 2793497057eeSRobert Watson } 2794281a0fd4SPatrick Kelsey if (t_flags & TF_FASTOPEN) { 2795281a0fd4SPatrick Kelsey db_printf("%sTF_FASTOPEN", comma ? ", " : ""); 2796281a0fd4SPatrick Kelsey comma = 1; 2797281a0fd4SPatrick Kelsey } 2798497057eeSRobert Watson } 2799497057eeSRobert Watson 2800497057eeSRobert Watson static void 28013cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2) 28023cf38784SMichael Tuexen { 28033cf38784SMichael Tuexen int comma; 28043cf38784SMichael Tuexen 28053cf38784SMichael Tuexen comma = 0; 28063cf38784SMichael Tuexen if (t_flags2 & TF2_ECN_PERMIT) { 28073cf38784SMichael Tuexen db_printf("%sTF2_ECN_PERMIT", comma ? ", " : ""); 28083cf38784SMichael Tuexen comma = 1; 28093cf38784SMichael Tuexen } 28103cf38784SMichael Tuexen } 28113cf38784SMichael Tuexen 28123cf38784SMichael Tuexen 28133cf38784SMichael Tuexen static void 2814497057eeSRobert Watson db_print_toobflags(char t_oobflags) 2815497057eeSRobert Watson { 2816497057eeSRobert Watson int comma; 2817497057eeSRobert Watson 2818497057eeSRobert Watson comma = 0; 2819497057eeSRobert Watson if (t_oobflags & TCPOOB_HAVEDATA) { 2820497057eeSRobert Watson db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 2821497057eeSRobert Watson comma = 1; 2822497057eeSRobert Watson } 2823497057eeSRobert Watson if (t_oobflags & TCPOOB_HADDATA) { 2824497057eeSRobert Watson db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 2825497057eeSRobert Watson comma = 1; 2826497057eeSRobert Watson } 2827497057eeSRobert Watson } 2828497057eeSRobert Watson 2829497057eeSRobert Watson static void 2830497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 2831497057eeSRobert Watson { 2832497057eeSRobert Watson 2833497057eeSRobert Watson db_print_indent(indent); 2834497057eeSRobert Watson db_printf("%s at %p\n", name, tp); 2835497057eeSRobert Watson 2836497057eeSRobert Watson indent += 2; 2837497057eeSRobert Watson 2838497057eeSRobert Watson db_print_indent(indent); 2839497057eeSRobert Watson db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 2840c28440dbSRandall Stewart TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 2841497057eeSRobert Watson 2842497057eeSRobert Watson db_print_indent(indent); 284385d94372SRobert Watson db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n", 2844e2f2059fSMike Silbersack &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep); 2845497057eeSRobert Watson 2846497057eeSRobert Watson db_print_indent(indent); 2847e2f2059fSMike Silbersack db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl, 2848e2f2059fSMike Silbersack &tp->t_timers->tt_delack, tp->t_inpcb); 2849497057eeSRobert Watson 2850497057eeSRobert Watson db_print_indent(indent); 2851497057eeSRobert Watson db_printf("t_state: %d (", tp->t_state); 2852497057eeSRobert Watson db_print_tstate(tp->t_state); 2853497057eeSRobert Watson db_printf(")\n"); 2854497057eeSRobert Watson 2855497057eeSRobert Watson db_print_indent(indent); 2856497057eeSRobert Watson db_printf("t_flags: 0x%x (", tp->t_flags); 2857497057eeSRobert Watson db_print_tflags(tp->t_flags); 2858497057eeSRobert Watson db_printf(")\n"); 2859497057eeSRobert Watson 2860497057eeSRobert Watson db_print_indent(indent); 28613cf38784SMichael Tuexen db_printf("t_flags2: 0x%x (", tp->t_flags2); 28623cf38784SMichael Tuexen db_print_tflags2(tp->t_flags2); 28633cf38784SMichael Tuexen db_printf(")\n"); 28643cf38784SMichael Tuexen 28653cf38784SMichael Tuexen db_print_indent(indent); 2866497057eeSRobert Watson db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n", 2867497057eeSRobert Watson tp->snd_una, tp->snd_max, tp->snd_nxt); 2868497057eeSRobert Watson 2869497057eeSRobert Watson db_print_indent(indent); 2870497057eeSRobert Watson db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 2871497057eeSRobert Watson tp->snd_up, tp->snd_wl1, tp->snd_wl2); 2872497057eeSRobert Watson 2873497057eeSRobert Watson db_print_indent(indent); 2874497057eeSRobert Watson db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 2875497057eeSRobert Watson tp->iss, tp->irs, tp->rcv_nxt); 2876497057eeSRobert Watson 2877497057eeSRobert Watson db_print_indent(indent); 28783ac12506SJonathan T. Looney db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n", 2879497057eeSRobert Watson tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 2880497057eeSRobert Watson 2881497057eeSRobert Watson db_print_indent(indent); 28823ac12506SJonathan T. Looney db_printf("snd_wnd: %u snd_cwnd: %u\n", 28831c18314dSAndre Oppermann tp->snd_wnd, tp->snd_cwnd); 2884497057eeSRobert Watson 2885497057eeSRobert Watson db_print_indent(indent); 28863ac12506SJonathan T. Looney db_printf("snd_ssthresh: %u snd_recover: " 28871c18314dSAndre Oppermann "0x%08x\n", tp->snd_ssthresh, tp->snd_recover); 2888497057eeSRobert Watson 2889497057eeSRobert Watson db_print_indent(indent); 28900c39d38dSGleb Smirnoff db_printf("t_rcvtime: %u t_startime: %u\n", 28910c39d38dSGleb Smirnoff tp->t_rcvtime, tp->t_starttime); 2892497057eeSRobert Watson 2893497057eeSRobert Watson db_print_indent(indent); 28941c18314dSAndre Oppermann db_printf("t_rttime: %u t_rtsq: 0x%08x\n", 28951c18314dSAndre Oppermann tp->t_rtttime, tp->t_rtseq); 2896497057eeSRobert Watson 2897497057eeSRobert Watson db_print_indent(indent); 28981c18314dSAndre Oppermann db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n", 28991c18314dSAndre Oppermann tp->t_rxtcur, tp->t_maxseg, tp->t_srtt); 2900497057eeSRobert Watson 2901497057eeSRobert Watson db_print_indent(indent); 2902497057eeSRobert Watson db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u " 2903497057eeSRobert Watson "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin, 2904497057eeSRobert Watson tp->t_rttbest); 2905497057eeSRobert Watson 2906497057eeSRobert Watson db_print_indent(indent); 29073ac12506SJonathan T. Looney db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n", 2908497057eeSRobert Watson tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 2909497057eeSRobert Watson 2910497057eeSRobert Watson db_print_indent(indent); 2911497057eeSRobert Watson db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 2912497057eeSRobert Watson db_print_toobflags(tp->t_oobflags); 2913497057eeSRobert Watson db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 2914497057eeSRobert Watson 2915497057eeSRobert Watson db_print_indent(indent); 2916497057eeSRobert Watson db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 2917497057eeSRobert Watson tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 2918497057eeSRobert Watson 2919497057eeSRobert Watson db_print_indent(indent); 29209f78a87aSJohn Baldwin db_printf("ts_recent: %u ts_recent_age: %u\n", 29211a553740SAndre Oppermann tp->ts_recent, tp->ts_recent_age); 2922497057eeSRobert Watson 2923497057eeSRobert Watson db_print_indent(indent); 2924497057eeSRobert Watson db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 29253ac12506SJonathan T. Looney "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 2926497057eeSRobert Watson 2927497057eeSRobert Watson db_print_indent(indent); 29283ac12506SJonathan T. Looney db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x " 29299f78a87aSJohn Baldwin "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 2930497057eeSRobert Watson tp->snd_recover_prev, tp->t_badrxtwin); 2931497057eeSRobert Watson 2932497057eeSRobert Watson db_print_indent(indent); 29333529149eSAndre Oppermann db_printf("snd_numholes: %d snd_holes first: %p\n", 29343529149eSAndre Oppermann tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 2935497057eeSRobert Watson 2936497057eeSRobert Watson db_print_indent(indent); 2937a3574665SMichael Tuexen db_printf("snd_fack: 0x%08x rcv_numsacks: %d\n", 2938a3574665SMichael Tuexen tp->snd_fack, tp->rcv_numsacks); 2939497057eeSRobert Watson 2940497057eeSRobert Watson /* Skip sackblks, sackhint. */ 2941497057eeSRobert Watson 2942497057eeSRobert Watson db_print_indent(indent); 2943497057eeSRobert Watson db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 2944497057eeSRobert Watson tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 2945497057eeSRobert Watson } 2946497057eeSRobert Watson 2947497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 2948497057eeSRobert Watson { 2949497057eeSRobert Watson struct tcpcb *tp; 2950497057eeSRobert Watson 2951497057eeSRobert Watson if (!have_addr) { 2952497057eeSRobert Watson db_printf("usage: show tcpcb <addr>\n"); 2953497057eeSRobert Watson return; 2954497057eeSRobert Watson } 2955497057eeSRobert Watson tp = (struct tcpcb *)addr; 2956497057eeSRobert Watson 2957497057eeSRobert Watson db_print_tcpcb(tp, "tcpcb", 0); 2958497057eeSRobert Watson } 2959497057eeSRobert Watson #endif 2960