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 */ 12456dc72c3SPawel Jakub Dawidek static int tcp_attach(struct socket *); 125b287c6c7SBjoern A. Zeeb #ifdef INET 1264d77a549SAlfred Perlstein static int tcp_connect(struct tcpcb *, struct sockaddr *, 1274d77a549SAlfred Perlstein struct thread *td); 128b287c6c7SBjoern A. Zeeb #endif /* INET */ 129fb59c426SYoshinobu Inoue #ifdef INET6 1304d77a549SAlfred Perlstein static int tcp6_connect(struct tcpcb *, struct sockaddr *, 1314d77a549SAlfred Perlstein struct thread *td); 132fb59c426SYoshinobu Inoue #endif /* INET6 */ 133623dce13SRobert Watson static void tcp_disconnect(struct tcpcb *); 134623dce13SRobert Watson static void tcp_usrclosed(struct tcpcb *); 135b8af5dfaSRobert Watson static void tcp_fill_info(struct tcpcb *, struct tcp_info *); 1362c37256eSGarrett Wollman 1372c37256eSGarrett Wollman #ifdef TCPDEBUG 1381db24ffbSJonathan Lemon #define TCPDEBUG0 int ostate = 0 1392c37256eSGarrett Wollman #define TCPDEBUG1() ostate = tp ? tp->t_state : 0 1404cc20ab1SSeigo Tanimura #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 1414cc20ab1SSeigo Tanimura tcp_trace(TA_USER, ostate, tp, 0, 0, req) 1422c37256eSGarrett Wollman #else 1432c37256eSGarrett Wollman #define TCPDEBUG0 1442c37256eSGarrett Wollman #define TCPDEBUG1() 1452c37256eSGarrett Wollman #define TCPDEBUG2(req) 1462c37256eSGarrett Wollman #endif 1472c37256eSGarrett Wollman 1482c37256eSGarrett Wollman /* 1492c37256eSGarrett Wollman * TCP attaches to socket via pru_attach(), reserving space, 1502c37256eSGarrett Wollman * and an internet control block. 1512c37256eSGarrett Wollman */ 1522c37256eSGarrett Wollman static int 153b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td) 1542c37256eSGarrett Wollman { 155f76fcf6dSJeffrey Hsu struct inpcb *inp; 156623dce13SRobert Watson struct tcpcb *tp = NULL; 157623dce13SRobert Watson int error; 1582c37256eSGarrett Wollman TCPDEBUG0; 1592c37256eSGarrett Wollman 160623dce13SRobert Watson inp = sotoinpcb(so); 161623dce13SRobert Watson KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL")); 1622c37256eSGarrett Wollman TCPDEBUG1(); 1632c37256eSGarrett Wollman 16456dc72c3SPawel Jakub Dawidek error = tcp_attach(so); 1652c37256eSGarrett Wollman if (error) 1662c37256eSGarrett Wollman goto out; 1672c37256eSGarrett Wollman 1682c37256eSGarrett Wollman if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1693879597fSAndrey A. Chernov so->so_linger = TCP_LINGERTIME; 170f76fcf6dSJeffrey Hsu 171f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 172f76fcf6dSJeffrey Hsu tp = intotcpcb(inp); 1732c37256eSGarrett Wollman out: 1742c37256eSGarrett Wollman TCPDEBUG2(PRU_ATTACH); 1755d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ATTACH); 1762c37256eSGarrett Wollman return error; 1772c37256eSGarrett Wollman } 1782c37256eSGarrett Wollman 1792c37256eSGarrett Wollman /* 180a152f8a3SRobert Watson * tcp_detach is called when the socket layer loses its final reference 181a152f8a3SRobert Watson * to the socket, be it a file descriptor reference, a reference from TCP, 182a152f8a3SRobert Watson * etc. At this point, there is only one case in which we will keep around 183a152f8a3SRobert Watson * inpcb state: time wait. 184c78cbc7bSRobert Watson * 185a152f8a3SRobert Watson * This function can probably be re-absorbed back into tcp_usr_detach() now 186a152f8a3SRobert Watson * that there is a single detach path. 1872c37256eSGarrett Wollman */ 188bc725eafSRobert Watson static void 189c78cbc7bSRobert Watson tcp_detach(struct socket *so, struct inpcb *inp) 1902c37256eSGarrett Wollman { 1912c37256eSGarrett Wollman struct tcpcb *tp; 1922c37256eSGarrett Wollman 193079672cbSJulien Charbon INP_INFO_LOCK_ASSERT(&V_tcbinfo); 1948501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 195623dce13SRobert Watson 196c78cbc7bSRobert Watson KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp")); 197c78cbc7bSRobert Watson KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so")); 198953b5606SRobert Watson 199a152f8a3SRobert Watson tp = intotcpcb(inp); 200a152f8a3SRobert Watson 201ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 202623dce13SRobert Watson /* 203a152f8a3SRobert Watson * There are two cases to handle: one in which the time wait 204a152f8a3SRobert Watson * state is being discarded (INP_DROPPED), and one in which 205a152f8a3SRobert Watson * this connection will remain in timewait. In the former, 206a152f8a3SRobert Watson * it is time to discard all state (except tcptw, which has 207a152f8a3SRobert Watson * already been discarded by the timewait close code, which 208a152f8a3SRobert Watson * should be further up the call stack somewhere). In the 209a152f8a3SRobert Watson * latter case, we detach from the socket, but leave the pcb 210a152f8a3SRobert Watson * present until timewait ends. 211623dce13SRobert Watson * 212a152f8a3SRobert Watson * XXXRW: Would it be cleaner to free the tcptw here? 213cea40c48SJulien Charbon * 214cea40c48SJulien Charbon * Astute question indeed, from twtcp perspective there are 215dd388cfdSGleb Smirnoff * four cases to consider: 216cea40c48SJulien Charbon * 217cea40c48SJulien Charbon * #1 tcp_detach is called at tcptw creation time by 218cea40c48SJulien Charbon * tcp_twstart, then do not discard the newly created tcptw 219cea40c48SJulien Charbon * and leave inpcb present until timewait ends 220dd388cfdSGleb Smirnoff * #2 tcp_detach is called at tcptw creation time by 221dd388cfdSGleb Smirnoff * tcp_twstart, but connection is local and tw will be 222dd388cfdSGleb Smirnoff * discarded immediately 223dd388cfdSGleb Smirnoff * #3 tcp_detach is called at timewait end (or reuse) by 224cea40c48SJulien Charbon * tcp_twclose, then the tcptw has already been discarded 225ff9b006dSJulien Charbon * (or reused) and inpcb is freed here 226dd388cfdSGleb Smirnoff * #4 tcp_detach is called() after timewait ends (or reuse) 227cea40c48SJulien Charbon * (e.g. by soclose), then tcptw has already been discarded 228ff9b006dSJulien Charbon * (or reused) and inpcb is freed here 229cea40c48SJulien Charbon * 230cea40c48SJulien Charbon * In all three cases the tcptw should not be freed here. 231623dce13SRobert Watson */ 232ad71fe3cSRobert Watson if (inp->inp_flags & INP_DROPPED) { 233623dce13SRobert Watson in_pcbdetach(inp); 234f5cf1e5fSJulien Charbon if (__predict_true(tp == NULL)) { 2350206cdb8SBjoern A. Zeeb in_pcbfree(inp); 2360206cdb8SBjoern A. Zeeb } else { 237f5cf1e5fSJulien Charbon /* 238f5cf1e5fSJulien Charbon * This case should not happen as in TIMEWAIT 239f5cf1e5fSJulien Charbon * state the inp should not be destroyed before 240f5cf1e5fSJulien Charbon * its tcptw. If INVARIANTS is defined, panic. 241f5cf1e5fSJulien Charbon */ 242f5cf1e5fSJulien Charbon #ifdef INVARIANTS 243f5cf1e5fSJulien Charbon panic("%s: Panic before an inp double-free: " 244f5cf1e5fSJulien Charbon "INP_TIMEWAIT && INP_DROPPED && tp != NULL" 245f5cf1e5fSJulien Charbon , __func__); 246f5cf1e5fSJulien Charbon #else 247f5cf1e5fSJulien Charbon log(LOG_ERR, "%s: Avoid an inp double-free: " 248f5cf1e5fSJulien Charbon "INP_TIMEWAIT && INP_DROPPED && tp != NULL" 249f5cf1e5fSJulien Charbon , __func__); 250f5cf1e5fSJulien Charbon #endif 251f5cf1e5fSJulien Charbon INP_WUNLOCK(inp); 252f5cf1e5fSJulien Charbon } 253f5cf1e5fSJulien Charbon } else { 254623dce13SRobert Watson in_pcbdetach(inp); 2558501a69cSRobert Watson INP_WUNLOCK(inp); 256623dce13SRobert Watson } 257623dce13SRobert Watson } else { 258e6e65783SRobert Watson /* 259a152f8a3SRobert Watson * If the connection is not in timewait, we consider two 260a152f8a3SRobert Watson * two conditions: one in which no further processing is 261a152f8a3SRobert Watson * necessary (dropped || embryonic), and one in which TCP is 262a152f8a3SRobert Watson * not yet done, but no longer requires the socket, so the 263a152f8a3SRobert Watson * pcb will persist for the time being. 264a152f8a3SRobert Watson * 265a152f8a3SRobert Watson * XXXRW: Does the second case still occur? 266e6e65783SRobert Watson */ 267ad71fe3cSRobert Watson if (inp->inp_flags & INP_DROPPED || 268623dce13SRobert Watson tp->t_state < TCPS_SYN_SENT) { 269623dce13SRobert Watson tcp_discardcb(tp); 270623dce13SRobert Watson in_pcbdetach(inp); 2710206cdb8SBjoern A. Zeeb in_pcbfree(inp); 272db3cee51SNavdeep Parhar } else { 273a152f8a3SRobert Watson in_pcbdetach(inp); 274db3cee51SNavdeep Parhar INP_WUNLOCK(inp); 275db3cee51SNavdeep Parhar } 276623dce13SRobert Watson } 277623dce13SRobert Watson } 278c78cbc7bSRobert Watson 279c78cbc7bSRobert Watson /* 280c78cbc7bSRobert Watson * pru_detach() detaches the TCP protocol from the socket. 281c78cbc7bSRobert Watson * If the protocol state is non-embryonic, then can't 282c78cbc7bSRobert Watson * do this directly: have to initiate a pru_disconnect(), 283c78cbc7bSRobert Watson * which may finish later; embryonic TCB's can just 284c78cbc7bSRobert Watson * be discarded here. 285c78cbc7bSRobert Watson */ 286c78cbc7bSRobert Watson static void 287c78cbc7bSRobert Watson tcp_usr_detach(struct socket *so) 288c78cbc7bSRobert Watson { 289c78cbc7bSRobert Watson struct inpcb *inp; 290079672cbSJulien Charbon int rlock = 0; 2916573d758SMatt Macy struct epoch_tracker et; 292c78cbc7bSRobert Watson 293c78cbc7bSRobert Watson inp = sotoinpcb(so); 294c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL")); 295079672cbSJulien Charbon if (!INP_INFO_WLOCKED(&V_tcbinfo)) { 29697a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 297079672cbSJulien Charbon rlock = 1; 298079672cbSJulien Charbon } 2998501a69cSRobert Watson INP_WLOCK(inp); 300c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 301c78cbc7bSRobert Watson ("tcp_usr_detach: inp_socket == NULL")); 302c78cbc7bSRobert Watson tcp_detach(so, inp); 303079672cbSJulien Charbon if (rlock) 30497a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 3052c37256eSGarrett Wollman } 3062c37256eSGarrett Wollman 307b287c6c7SBjoern A. Zeeb #ifdef INET 3082c37256eSGarrett Wollman /* 3092c37256eSGarrett Wollman * Give the socket an address. 3102c37256eSGarrett Wollman */ 3112c37256eSGarrett Wollman static int 312b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 3132c37256eSGarrett Wollman { 3142c37256eSGarrett Wollman int error = 0; 315f76fcf6dSJeffrey Hsu struct inpcb *inp; 316623dce13SRobert Watson struct tcpcb *tp = NULL; 3172c37256eSGarrett Wollman struct sockaddr_in *sinp; 3182c37256eSGarrett Wollman 31952710de1SPawel Jakub Dawidek sinp = (struct sockaddr_in *)nam; 32052710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof (*sinp)) 32152710de1SPawel Jakub Dawidek return (EINVAL); 3222c37256eSGarrett Wollman /* 3232c37256eSGarrett Wollman * Must check for multicast addresses and disallow binding 3242c37256eSGarrett Wollman * to them. 3252c37256eSGarrett Wollman */ 3262c37256eSGarrett Wollman if (sinp->sin_family == AF_INET && 32752710de1SPawel Jakub Dawidek IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 32852710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 32952710de1SPawel Jakub Dawidek 330623dce13SRobert Watson TCPDEBUG0; 331623dce13SRobert Watson inp = sotoinpcb(so); 332623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 3338501a69cSRobert Watson INP_WLOCK(inp); 334ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 335623dce13SRobert Watson error = EINVAL; 3362c37256eSGarrett Wollman goto out; 337623dce13SRobert Watson } 338623dce13SRobert Watson tp = intotcpcb(inp); 339623dce13SRobert Watson TCPDEBUG1(); 340fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 341623dce13SRobert Watson error = in_pcbbind(inp, nam, td->td_ucred); 342fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 343623dce13SRobert Watson out: 344623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 3455d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 3468501a69cSRobert Watson INP_WUNLOCK(inp); 347623dce13SRobert Watson 348623dce13SRobert Watson return (error); 3492c37256eSGarrett Wollman } 350b287c6c7SBjoern A. Zeeb #endif /* INET */ 3512c37256eSGarrett Wollman 352fb59c426SYoshinobu Inoue #ifdef INET6 353fb59c426SYoshinobu Inoue static int 354b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 355fb59c426SYoshinobu Inoue { 356fb59c426SYoshinobu Inoue int error = 0; 357f76fcf6dSJeffrey Hsu struct inpcb *inp; 358623dce13SRobert Watson struct tcpcb *tp = NULL; 3590ecd976eSBjoern A. Zeeb struct sockaddr_in6 *sin6; 3604a91aa8fSMichael Tuexen u_char vflagsav; 361fb59c426SYoshinobu Inoue 3620ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 3630ecd976eSBjoern A. Zeeb if (nam->sa_len != sizeof (*sin6)) 36452710de1SPawel Jakub Dawidek return (EINVAL); 365fb59c426SYoshinobu Inoue /* 366fb59c426SYoshinobu Inoue * Must check for multicast addresses and disallow binding 367fb59c426SYoshinobu Inoue * to them. 368fb59c426SYoshinobu Inoue */ 3690ecd976eSBjoern A. Zeeb if (sin6->sin6_family == AF_INET6 && 3700ecd976eSBjoern A. Zeeb IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 37152710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 37252710de1SPawel Jakub Dawidek 373623dce13SRobert Watson TCPDEBUG0; 374623dce13SRobert Watson inp = sotoinpcb(so); 375623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 3768501a69cSRobert Watson INP_WLOCK(inp); 3774a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 378ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 379623dce13SRobert Watson error = EINVAL; 380623dce13SRobert Watson goto out; 381623dce13SRobert Watson } 382623dce13SRobert Watson tp = intotcpcb(inp); 383623dce13SRobert Watson TCPDEBUG1(); 384fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 385fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 386fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 387b287c6c7SBjoern A. Zeeb #ifdef INET 38866ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 3890ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 390fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 3910ecd976eSBjoern A. Zeeb else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 392fb59c426SYoshinobu Inoue struct sockaddr_in sin; 393fb59c426SYoshinobu Inoue 3940ecd976eSBjoern A. Zeeb in6_sin6_2_sin(&sin, sin6); 395888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 396888973f5SMichael Tuexen error = EAFNOSUPPORT; 397888973f5SMichael Tuexen INP_HASH_WUNLOCK(&V_tcbinfo); 398888973f5SMichael Tuexen goto out; 399888973f5SMichael Tuexen } 400fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 401fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 402b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)&sin, 403b0330ed9SPawel Jakub Dawidek td->td_ucred); 404fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 405fb59c426SYoshinobu Inoue goto out; 406fb59c426SYoshinobu Inoue } 407fb59c426SYoshinobu Inoue } 408b287c6c7SBjoern A. Zeeb #endif 409b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, nam, td->td_ucred); 410fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 411623dce13SRobert Watson out: 4124a91aa8fSMichael Tuexen if (error != 0) 4134a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 414623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 4155d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 4168501a69cSRobert Watson INP_WUNLOCK(inp); 417623dce13SRobert Watson return (error); 418fb59c426SYoshinobu Inoue } 419fb59c426SYoshinobu Inoue #endif /* INET6 */ 420fb59c426SYoshinobu Inoue 421b287c6c7SBjoern A. Zeeb #ifdef INET 4222c37256eSGarrett Wollman /* 4232c37256eSGarrett Wollman * Prepare to accept connections. 4242c37256eSGarrett Wollman */ 4252c37256eSGarrett Wollman static int 426d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td) 4272c37256eSGarrett Wollman { 4282c37256eSGarrett Wollman int error = 0; 429f76fcf6dSJeffrey Hsu struct inpcb *inp; 430623dce13SRobert Watson struct tcpcb *tp = NULL; 4312c37256eSGarrett Wollman 432623dce13SRobert Watson TCPDEBUG0; 433623dce13SRobert Watson inp = sotoinpcb(so); 434623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 4358501a69cSRobert Watson INP_WLOCK(inp); 436ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 437623dce13SRobert Watson error = EINVAL; 438623dce13SRobert Watson goto out; 439623dce13SRobert Watson } 440623dce13SRobert Watson tp = intotcpcb(inp); 441623dce13SRobert Watson TCPDEBUG1(); 4420daccb9cSRobert Watson SOCK_LOCK(so); 4430daccb9cSRobert Watson error = solisten_proto_check(so); 444fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 4450daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) 446b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 447fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 4480daccb9cSRobert Watson if (error == 0) { 44957f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 450d374e81eSRobert Watson solisten_proto(so, backlog); 45109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 45237cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 45309fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 45409fe6320SNavdeep Parhar #endif 4550daccb9cSRobert Watson } 4560daccb9cSRobert Watson SOCK_UNLOCK(so); 457623dce13SRobert Watson 45868bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) 459281a0fd4SPatrick Kelsey tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 46018a75309SPatrick Kelsey 461623dce13SRobert Watson out: 462623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 4635d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 4648501a69cSRobert Watson INP_WUNLOCK(inp); 465623dce13SRobert Watson return (error); 4662c37256eSGarrett Wollman } 467b287c6c7SBjoern A. Zeeb #endif /* INET */ 4682c37256eSGarrett Wollman 469fb59c426SYoshinobu Inoue #ifdef INET6 470fb59c426SYoshinobu Inoue static int 471d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 472fb59c426SYoshinobu Inoue { 473fb59c426SYoshinobu Inoue int error = 0; 474f76fcf6dSJeffrey Hsu struct inpcb *inp; 475623dce13SRobert Watson struct tcpcb *tp = NULL; 4764a91aa8fSMichael Tuexen u_char vflagsav; 477fb59c426SYoshinobu Inoue 478623dce13SRobert Watson TCPDEBUG0; 479623dce13SRobert Watson inp = sotoinpcb(so); 480623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL")); 4818501a69cSRobert Watson INP_WLOCK(inp); 482ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 483623dce13SRobert Watson error = EINVAL; 484623dce13SRobert Watson goto out; 485623dce13SRobert Watson } 4864a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 487623dce13SRobert Watson tp = intotcpcb(inp); 488623dce13SRobert Watson TCPDEBUG1(); 4890daccb9cSRobert Watson SOCK_LOCK(so); 4900daccb9cSRobert Watson error = solisten_proto_check(so); 491fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 4920daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) { 493fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 49466ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 495fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 496b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 497fb59c426SYoshinobu Inoue } 498fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 4990daccb9cSRobert Watson if (error == 0) { 50057f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 501d374e81eSRobert Watson solisten_proto(so, backlog); 50209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 50337cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 50409fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 50509fe6320SNavdeep Parhar #endif 5060daccb9cSRobert Watson } 5070daccb9cSRobert Watson SOCK_UNLOCK(so); 508623dce13SRobert Watson 50968bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags)) 510281a0fd4SPatrick Kelsey tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 51118a75309SPatrick Kelsey 5124a91aa8fSMichael Tuexen if (error != 0) 5134a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 5144a91aa8fSMichael Tuexen 515623dce13SRobert Watson out: 516623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 5175d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 5188501a69cSRobert Watson INP_WUNLOCK(inp); 519623dce13SRobert Watson return (error); 520fb59c426SYoshinobu Inoue } 521fb59c426SYoshinobu Inoue #endif /* INET6 */ 522fb59c426SYoshinobu Inoue 523b287c6c7SBjoern A. Zeeb #ifdef INET 5242c37256eSGarrett Wollman /* 5252c37256eSGarrett Wollman * Initiate connection to peer. 5262c37256eSGarrett Wollman * Create a template for use in transmissions on this connection. 5272c37256eSGarrett Wollman * Enter SYN_SENT state, and mark socket as connecting. 5282c37256eSGarrett Wollman * Start keep-alive timer, and seed output sequence space. 5292c37256eSGarrett Wollman * Send initial segment on connection. 5302c37256eSGarrett Wollman */ 5312c37256eSGarrett Wollman static int 532b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 5332c37256eSGarrett Wollman { 534*109eb549SGleb Smirnoff struct epoch_tracker et; 5352c37256eSGarrett Wollman int error = 0; 536f76fcf6dSJeffrey Hsu struct inpcb *inp; 537623dce13SRobert Watson struct tcpcb *tp = NULL; 5382c37256eSGarrett Wollman struct sockaddr_in *sinp; 5392c37256eSGarrett Wollman 54057bf258eSGarrett Wollman sinp = (struct sockaddr_in *)nam; 541e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sinp)) 542e29ef13fSDon Lewis return (EINVAL); 54352710de1SPawel Jakub Dawidek /* 54452710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 54552710de1SPawel Jakub Dawidek */ 5462c37256eSGarrett Wollman if (sinp->sin_family == AF_INET 54752710de1SPawel Jakub Dawidek && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 54852710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 549b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0) 550b89e82ddSJamie Gritton return (error); 55175c13541SPoul-Henning Kamp 552623dce13SRobert Watson TCPDEBUG0; 553623dce13SRobert Watson inp = sotoinpcb(so); 554623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 5558501a69cSRobert Watson INP_WLOCK(inp); 556eb96dc33SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) { 557eb96dc33SJulien Charbon error = EADDRINUSE; 558eb96dc33SJulien Charbon goto out; 559eb96dc33SJulien Charbon } 560eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 561eb96dc33SJulien Charbon error = ECONNREFUSED; 562623dce13SRobert Watson goto out; 563623dce13SRobert Watson } 564623dce13SRobert Watson tp = intotcpcb(inp); 565623dce13SRobert Watson TCPDEBUG1(); 566b40ce416SJulian Elischer if ((error = tcp_connect(tp, nam, td)) != 0) 5672c37256eSGarrett Wollman goto out; 56809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 56909fe6320SNavdeep Parhar if (registered_toedevs > 0 && 57037cc0ecbSNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 57109fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 57209fe6320SNavdeep Parhar goto out; 57309fe6320SNavdeep Parhar #endif 57409fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 575*109eb549SGleb Smirnoff NET_EPOCH_ENTER(et); 57655bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 577*109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 578623dce13SRobert Watson out: 579623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 580e79cb051SGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CONNECT); 5818501a69cSRobert Watson INP_WUNLOCK(inp); 582623dce13SRobert Watson return (error); 5832c37256eSGarrett Wollman } 584b287c6c7SBjoern A. Zeeb #endif /* INET */ 5852c37256eSGarrett Wollman 586fb59c426SYoshinobu Inoue #ifdef INET6 587fb59c426SYoshinobu Inoue static int 588b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 589fb59c426SYoshinobu Inoue { 590*109eb549SGleb Smirnoff struct epoch_tracker et; 591fb59c426SYoshinobu Inoue int error = 0; 592f76fcf6dSJeffrey Hsu struct inpcb *inp; 593623dce13SRobert Watson struct tcpcb *tp = NULL; 5940ecd976eSBjoern A. Zeeb struct sockaddr_in6 *sin6; 5954a91aa8fSMichael Tuexen u_int8_t incflagsav; 5964a91aa8fSMichael Tuexen u_char vflagsav; 597623dce13SRobert Watson 598623dce13SRobert Watson TCPDEBUG0; 599fb59c426SYoshinobu Inoue 6000ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 6010ecd976eSBjoern A. Zeeb if (nam->sa_len != sizeof (*sin6)) 602e29ef13fSDon Lewis return (EINVAL); 60352710de1SPawel Jakub Dawidek /* 60452710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 60552710de1SPawel Jakub Dawidek */ 6060ecd976eSBjoern A. Zeeb if (sin6->sin6_family == AF_INET6 6070ecd976eSBjoern A. Zeeb && IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 60852710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 609fb59c426SYoshinobu Inoue 610623dce13SRobert Watson inp = sotoinpcb(so); 611623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 6128501a69cSRobert Watson INP_WLOCK(inp); 6134a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 6144a91aa8fSMichael Tuexen incflagsav = inp->inp_inc.inc_flags; 615eb96dc33SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) { 616eb96dc33SJulien Charbon error = EADDRINUSE; 617eb96dc33SJulien Charbon goto out; 618eb96dc33SJulien Charbon } 619eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 620eb96dc33SJulien Charbon error = ECONNREFUSED; 621623dce13SRobert Watson goto out; 622623dce13SRobert Watson } 623623dce13SRobert Watson tp = intotcpcb(inp); 624623dce13SRobert Watson TCPDEBUG1(); 625b287c6c7SBjoern A. Zeeb #ifdef INET 626fa046d87SRobert Watson /* 627fa046d87SRobert Watson * XXXRW: Some confusion: V4/V6 flags relate to binding, and 628fa046d87SRobert Watson * therefore probably require the hash lock, which isn't held here. 629fa046d87SRobert Watson * Is this a significant problem? 630fa046d87SRobert Watson */ 6310ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 632fb59c426SYoshinobu Inoue struct sockaddr_in sin; 633fb59c426SYoshinobu Inoue 634d46a5312SMaxim Konovalov if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 635d46a5312SMaxim Konovalov error = EINVAL; 636d46a5312SMaxim Konovalov goto out; 637d46a5312SMaxim Konovalov } 6385dba6adaSMichael Tuexen if ((inp->inp_vflag & INP_IPV4) == 0) { 6395dba6adaSMichael Tuexen error = EAFNOSUPPORT; 6405dba6adaSMichael Tuexen goto out; 6415dba6adaSMichael Tuexen } 64233841545SHajimu UMEMOTO 6430ecd976eSBjoern A. Zeeb in6_sin6_2_sin(&sin, sin6); 644888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 645888973f5SMichael Tuexen error = EAFNOSUPPORT; 646888973f5SMichael Tuexen goto out; 647888973f5SMichael Tuexen } 648b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, 649b89e82ddSJamie Gritton &sin.sin_addr)) != 0) 650413628a7SBjoern A. Zeeb goto out; 6514a91aa8fSMichael Tuexen inp->inp_vflag |= INP_IPV4; 6524a91aa8fSMichael Tuexen inp->inp_vflag &= ~INP_IPV6; 653b40ce416SJulian Elischer if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 654fb59c426SYoshinobu Inoue goto out; 65509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 65609fe6320SNavdeep Parhar if (registered_toedevs > 0 && 657adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 65809fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 65909fe6320SNavdeep Parhar goto out; 66009fe6320SNavdeep Parhar #endif 661*109eb549SGleb Smirnoff NET_EPOCH_ENTER(et); 66255bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 663*109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 664fb59c426SYoshinobu Inoue goto out; 6655dba6adaSMichael Tuexen } else { 6665dba6adaSMichael Tuexen if ((inp->inp_vflag & INP_IPV6) == 0) { 6675dba6adaSMichael Tuexen error = EAFNOSUPPORT; 6685dba6adaSMichael Tuexen goto out; 6695dba6adaSMichael Tuexen } 670fb59c426SYoshinobu Inoue } 671b287c6c7SBjoern A. Zeeb #endif 6724a91aa8fSMichael Tuexen if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0) 6734a91aa8fSMichael Tuexen goto out; 674fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 675fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 676dcdb4371SBjoern A. Zeeb inp->inp_inc.inc_flags |= INC_ISIPV6; 677b40ce416SJulian Elischer if ((error = tcp6_connect(tp, nam, td)) != 0) 678fb59c426SYoshinobu Inoue goto out; 67909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 68009fe6320SNavdeep Parhar if (registered_toedevs > 0 && 681adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 68209fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 68309fe6320SNavdeep Parhar goto out; 68409fe6320SNavdeep Parhar #endif 68509fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 686*109eb549SGleb Smirnoff NET_EPOCH_ENTER(et); 68755bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 688*109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 689623dce13SRobert Watson out: 6904a91aa8fSMichael Tuexen /* 6914a91aa8fSMichael Tuexen * If the implicit bind in the connect call fails, restore 6924a91aa8fSMichael Tuexen * the flags we modified. 6934a91aa8fSMichael Tuexen */ 6944a91aa8fSMichael Tuexen if (error != 0 && inp->inp_lport == 0) { 6954a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 6964a91aa8fSMichael Tuexen inp->inp_inc.inc_flags = incflagsav; 6974a91aa8fSMichael Tuexen } 6984a91aa8fSMichael Tuexen 699623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 7005d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CONNECT); 7018501a69cSRobert Watson INP_WUNLOCK(inp); 702623dce13SRobert Watson return (error); 703fb59c426SYoshinobu Inoue } 704fb59c426SYoshinobu Inoue #endif /* INET6 */ 705fb59c426SYoshinobu Inoue 7062c37256eSGarrett Wollman /* 7072c37256eSGarrett Wollman * Initiate disconnect from peer. 7082c37256eSGarrett Wollman * If connection never passed embryonic stage, just drop; 7092c37256eSGarrett Wollman * else if don't need to let data drain, then can just drop anyways, 7102c37256eSGarrett Wollman * else have to begin TCP shutdown process: mark socket disconnecting, 7112c37256eSGarrett Wollman * drain unread data, state switch to reflect user close, and 7122c37256eSGarrett Wollman * send segment (e.g. FIN) to peer. Socket will be really disconnected 7132c37256eSGarrett Wollman * when peer sends FIN and acks ours. 7142c37256eSGarrett Wollman * 7152c37256eSGarrett Wollman * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 7162c37256eSGarrett Wollman */ 7172c37256eSGarrett Wollman static int 7182c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so) 7192c37256eSGarrett Wollman { 720f76fcf6dSJeffrey Hsu struct inpcb *inp; 721623dce13SRobert Watson struct tcpcb *tp = NULL; 7226573d758SMatt Macy struct epoch_tracker et; 723623dce13SRobert Watson int error = 0; 7242c37256eSGarrett Wollman 725623dce13SRobert Watson TCPDEBUG0; 72697a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 727623dce13SRobert Watson inp = sotoinpcb(so); 728623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 7298501a69cSRobert Watson INP_WLOCK(inp); 730489dcc92SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) 731489dcc92SJulien Charbon goto out; 732489dcc92SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 73321367f63SSam Leffler error = ECONNRESET; 734623dce13SRobert Watson goto out; 735623dce13SRobert Watson } 736623dce13SRobert Watson tp = intotcpcb(inp); 737623dce13SRobert Watson TCPDEBUG1(); 738623dce13SRobert Watson tcp_disconnect(tp); 739623dce13SRobert Watson out: 740623dce13SRobert Watson TCPDEBUG2(PRU_DISCONNECT); 7415d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_DISCONNECT); 7428501a69cSRobert Watson INP_WUNLOCK(inp); 74397a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 744623dce13SRobert Watson return (error); 7452c37256eSGarrett Wollman } 7462c37256eSGarrett Wollman 747b287c6c7SBjoern A. Zeeb #ifdef INET 7482c37256eSGarrett Wollman /* 7498296cddfSRobert Watson * Accept a connection. Essentially all the work is done at higher levels; 7508296cddfSRobert Watson * just return the address of the peer, storing through addr. 7512c37256eSGarrett Wollman */ 7522c37256eSGarrett Wollman static int 75357bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam) 7542c37256eSGarrett Wollman { 7552c37256eSGarrett Wollman int error = 0; 756f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 7571db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 75826ef6ac4SDon Lewis struct in_addr addr; 75926ef6ac4SDon Lewis in_port_t port = 0; 7601db24ffbSJonathan Lemon TCPDEBUG0; 7612c37256eSGarrett Wollman 7623d2d3ef4SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 7633d2d3ef4SRobert Watson return (ECONNABORTED); 764f76fcf6dSJeffrey Hsu 765f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 766623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 7678501a69cSRobert Watson INP_WLOCK(inp); 768ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 7693d2d3ef4SRobert Watson error = ECONNABORTED; 770623dce13SRobert Watson goto out; 771623dce13SRobert Watson } 7721db24ffbSJonathan Lemon tp = intotcpcb(inp); 7731db24ffbSJonathan Lemon TCPDEBUG1(); 774f76fcf6dSJeffrey Hsu 775f76fcf6dSJeffrey Hsu /* 77654d642bbSRobert Watson * We inline in_getpeeraddr and COMMON_END here, so that we can 77726ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 77826ef6ac4SDon Lewis * release the lock. 779f76fcf6dSJeffrey Hsu */ 78026ef6ac4SDon Lewis port = inp->inp_fport; 78126ef6ac4SDon Lewis addr = inp->inp_faddr; 782f76fcf6dSJeffrey Hsu 783623dce13SRobert Watson out: 784623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 7855d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 7868501a69cSRobert Watson INP_WUNLOCK(inp); 78726ef6ac4SDon Lewis if (error == 0) 78826ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 78926ef6ac4SDon Lewis return error; 7902c37256eSGarrett Wollman } 791b287c6c7SBjoern A. Zeeb #endif /* INET */ 7922c37256eSGarrett Wollman 793fb59c426SYoshinobu Inoue #ifdef INET6 794fb59c426SYoshinobu Inoue static int 795fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 796fb59c426SYoshinobu Inoue { 797f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 798fb59c426SYoshinobu Inoue int error = 0; 7991db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 80026ef6ac4SDon Lewis struct in_addr addr; 80126ef6ac4SDon Lewis struct in6_addr addr6; 8026573d758SMatt Macy struct epoch_tracker et; 80326ef6ac4SDon Lewis in_port_t port = 0; 80426ef6ac4SDon Lewis int v4 = 0; 8051db24ffbSJonathan Lemon TCPDEBUG0; 806fb59c426SYoshinobu Inoue 807b4470c16SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 808b4470c16SRobert Watson return (ECONNABORTED); 809f76fcf6dSJeffrey Hsu 810f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 811623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 81297a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 8138501a69cSRobert Watson INP_WLOCK(inp); 814ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 81521367f63SSam Leffler error = ECONNABORTED; 816623dce13SRobert Watson goto out; 817623dce13SRobert Watson } 8181db24ffbSJonathan Lemon tp = intotcpcb(inp); 8191db24ffbSJonathan Lemon TCPDEBUG1(); 820623dce13SRobert Watson 82126ef6ac4SDon Lewis /* 82226ef6ac4SDon Lewis * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 82326ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 82426ef6ac4SDon Lewis * release the lock. 82526ef6ac4SDon Lewis */ 82626ef6ac4SDon Lewis if (inp->inp_vflag & INP_IPV4) { 82726ef6ac4SDon Lewis v4 = 1; 82826ef6ac4SDon Lewis port = inp->inp_fport; 82926ef6ac4SDon Lewis addr = inp->inp_faddr; 83026ef6ac4SDon Lewis } else { 83126ef6ac4SDon Lewis port = inp->inp_fport; 83226ef6ac4SDon Lewis addr6 = inp->in6p_faddr; 83326ef6ac4SDon Lewis } 83426ef6ac4SDon Lewis 835623dce13SRobert Watson out: 836623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 8375d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 8388501a69cSRobert Watson INP_WUNLOCK(inp); 83997a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 84026ef6ac4SDon Lewis if (error == 0) { 84126ef6ac4SDon Lewis if (v4) 84226ef6ac4SDon Lewis *nam = in6_v4mapsin6_sockaddr(port, &addr); 84326ef6ac4SDon Lewis else 84426ef6ac4SDon Lewis *nam = in6_sockaddr(port, &addr6); 84526ef6ac4SDon Lewis } 84626ef6ac4SDon Lewis return error; 847fb59c426SYoshinobu Inoue } 848fb59c426SYoshinobu Inoue #endif /* INET6 */ 849f76fcf6dSJeffrey Hsu 850f76fcf6dSJeffrey Hsu /* 8512c37256eSGarrett Wollman * Mark the connection as being incapable of further output. 8522c37256eSGarrett Wollman */ 8532c37256eSGarrett Wollman static int 8542c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so) 8552c37256eSGarrett Wollman { 8562c37256eSGarrett Wollman int error = 0; 857f76fcf6dSJeffrey Hsu struct inpcb *inp; 858623dce13SRobert Watson struct tcpcb *tp = NULL; 8596573d758SMatt Macy struct epoch_tracker et; 8602c37256eSGarrett Wollman 861623dce13SRobert Watson TCPDEBUG0; 86297a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 863623dce13SRobert Watson inp = sotoinpcb(so); 864623dce13SRobert Watson KASSERT(inp != NULL, ("inp == NULL")); 8658501a69cSRobert Watson INP_WLOCK(inp); 866ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 86721367f63SSam Leffler error = ECONNRESET; 868623dce13SRobert Watson goto out; 869623dce13SRobert Watson } 870623dce13SRobert Watson tp = intotcpcb(inp); 871623dce13SRobert Watson TCPDEBUG1(); 8722c37256eSGarrett Wollman socantsendmore(so); 873623dce13SRobert Watson tcp_usrclosed(tp); 874ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 87555bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 876623dce13SRobert Watson 877623dce13SRobert Watson out: 878623dce13SRobert Watson TCPDEBUG2(PRU_SHUTDOWN); 8795d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN); 8808501a69cSRobert Watson INP_WUNLOCK(inp); 88197a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 882623dce13SRobert Watson 883623dce13SRobert Watson return (error); 8842c37256eSGarrett Wollman } 8852c37256eSGarrett Wollman 8862c37256eSGarrett Wollman /* 8872c37256eSGarrett Wollman * After a receive, possibly send window update to peer. 8882c37256eSGarrett Wollman */ 8892c37256eSGarrett Wollman static int 8902c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags) 8912c37256eSGarrett Wollman { 892*109eb549SGleb Smirnoff struct epoch_tracker et; 893f76fcf6dSJeffrey Hsu struct inpcb *inp; 894623dce13SRobert Watson struct tcpcb *tp = NULL; 895623dce13SRobert Watson int error = 0; 8962c37256eSGarrett Wollman 897623dce13SRobert Watson TCPDEBUG0; 898623dce13SRobert Watson inp = sotoinpcb(so); 899623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 9008501a69cSRobert Watson INP_WLOCK(inp); 901ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 90221367f63SSam Leffler error = ECONNRESET; 903623dce13SRobert Watson goto out; 904623dce13SRobert Watson } 905623dce13SRobert Watson tp = intotcpcb(inp); 906623dce13SRobert Watson TCPDEBUG1(); 907281a0fd4SPatrick Kelsey /* 908281a0fd4SPatrick Kelsey * For passively-created TFO connections, don't attempt a window 909281a0fd4SPatrick Kelsey * update while still in SYN_RECEIVED as this may trigger an early 910281a0fd4SPatrick Kelsey * SYN|ACK. It is preferable to have the SYN|ACK be sent along with 911281a0fd4SPatrick Kelsey * application response data, or failing that, when the DELACK timer 912281a0fd4SPatrick Kelsey * expires. 913281a0fd4SPatrick Kelsey */ 91468bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 915281a0fd4SPatrick Kelsey (tp->t_state == TCPS_SYN_RECEIVED)) 916281a0fd4SPatrick Kelsey goto out; 91709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 91809fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) 91909fe6320SNavdeep Parhar tcp_offload_rcvd(tp); 920460cf046SNavdeep Parhar else 92109fe6320SNavdeep Parhar #endif 922*109eb549SGleb Smirnoff NET_EPOCH_ENTER(et); 92355bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 924*109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 925623dce13SRobert Watson out: 926623dce13SRobert Watson TCPDEBUG2(PRU_RCVD); 9275d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVD); 9288501a69cSRobert Watson INP_WUNLOCK(inp); 929623dce13SRobert Watson return (error); 9302c37256eSGarrett Wollman } 9312c37256eSGarrett Wollman 9322c37256eSGarrett Wollman /* 9332c37256eSGarrett Wollman * Do a send by putting data in output queue and updating urgent 9349c9906e9SPeter Wemm * marker if URG set. Possibly send more data. Unlike the other 9359c9906e9SPeter Wemm * pru_*() routines, the mbuf chains are our responsibility. We 9369c9906e9SPeter Wemm * must either enqueue them or free them. The other pru_* routines 9379c9906e9SPeter Wemm * generally are caller-frees. 9382c37256eSGarrett Wollman */ 9392c37256eSGarrett Wollman static int 94057bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 941b40ce416SJulian Elischer struct sockaddr *nam, struct mbuf *control, struct thread *td) 9422c37256eSGarrett Wollman { 94397a95ee1SGleb Smirnoff struct epoch_tracker et; 9442c37256eSGarrett Wollman int error = 0; 945f76fcf6dSJeffrey Hsu struct inpcb *inp; 946623dce13SRobert Watson struct tcpcb *tp = NULL; 947888973f5SMichael Tuexen #ifdef INET 94851e08d53SMichael Tuexen #ifdef INET6 94951e08d53SMichael Tuexen struct sockaddr_in sin; 95051e08d53SMichael Tuexen #endif 95151e08d53SMichael Tuexen struct sockaddr_in *sinp; 952888973f5SMichael Tuexen #endif 953fb59c426SYoshinobu Inoue #ifdef INET6 954fb59c426SYoshinobu Inoue int isipv6; 955fb59c426SYoshinobu Inoue #endif 9564a91aa8fSMichael Tuexen u_int8_t incflagsav; 9574a91aa8fSMichael Tuexen u_char vflagsav; 9584a91aa8fSMichael Tuexen bool restoreflags; 9599c9906e9SPeter Wemm TCPDEBUG0; 9602c37256eSGarrett Wollman 961f76fcf6dSJeffrey Hsu /* 96297a95ee1SGleb Smirnoff * We require the pcbinfo "read lock" if we will close the socket 96397a95ee1SGleb Smirnoff * as part of this call. 964f76fcf6dSJeffrey Hsu */ 96597a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 966f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 967623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 9688501a69cSRobert Watson INP_WLOCK(inp); 9694a91aa8fSMichael Tuexen vflagsav = inp->inp_vflag; 9704a91aa8fSMichael Tuexen incflagsav = inp->inp_inc.inc_flags; 9714a91aa8fSMichael Tuexen restoreflags = false; 972ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 9737ff0b850SAndre Oppermann if (control) 9747ff0b850SAndre Oppermann m_freem(control); 9752cbcd3c1SGleb Smirnoff /* 9762cbcd3c1SGleb Smirnoff * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible 9772cbcd3c1SGleb Smirnoff * for freeing memory. 9782cbcd3c1SGleb Smirnoff */ 9792cbcd3c1SGleb Smirnoff if (m && (flags & PRUS_NOTREADY) == 0) 9807ff0b850SAndre Oppermann m_freem(m); 98121367f63SSam Leffler error = ECONNRESET; 9829c9906e9SPeter Wemm goto out; 9839c9906e9SPeter Wemm } 9849c9906e9SPeter Wemm tp = intotcpcb(inp); 9859c9906e9SPeter Wemm TCPDEBUG1(); 986888973f5SMichael Tuexen if (nam != NULL && tp->t_state < TCPS_SYN_SENT) { 987888973f5SMichael Tuexen switch (nam->sa_family) { 988888973f5SMichael Tuexen #ifdef INET 989888973f5SMichael Tuexen case AF_INET: 990888973f5SMichael Tuexen sinp = (struct sockaddr_in *)nam; 991888973f5SMichael Tuexen if (sinp->sin_len != sizeof(struct sockaddr_in)) { 992888973f5SMichael Tuexen if (m) 993888973f5SMichael Tuexen m_freem(m); 994888973f5SMichael Tuexen error = EINVAL; 995888973f5SMichael Tuexen goto out; 996888973f5SMichael Tuexen } 997888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV6) != 0) { 998888973f5SMichael Tuexen if (m) 999888973f5SMichael Tuexen m_freem(m); 1000888973f5SMichael Tuexen error = EAFNOSUPPORT; 1001888973f5SMichael Tuexen goto out; 1002888973f5SMichael Tuexen } 1003888973f5SMichael Tuexen if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 1004888973f5SMichael Tuexen if (m) 1005888973f5SMichael Tuexen m_freem(m); 1006888973f5SMichael Tuexen error = EAFNOSUPPORT; 1007888973f5SMichael Tuexen goto out; 1008888973f5SMichael Tuexen } 1009888973f5SMichael Tuexen if ((error = prison_remote_ip4(td->td_ucred, 1010888973f5SMichael Tuexen &sinp->sin_addr))) { 1011888973f5SMichael Tuexen if (m) 1012888973f5SMichael Tuexen m_freem(m); 1013888973f5SMichael Tuexen goto out; 1014888973f5SMichael Tuexen } 1015888973f5SMichael Tuexen #ifdef INET6 1016888973f5SMichael Tuexen isipv6 = 0; 1017888973f5SMichael Tuexen #endif 1018888973f5SMichael Tuexen break; 1019888973f5SMichael Tuexen #endif /* INET */ 1020888973f5SMichael Tuexen #ifdef INET6 1021888973f5SMichael Tuexen case AF_INET6: 1022888973f5SMichael Tuexen { 10230ecd976eSBjoern A. Zeeb struct sockaddr_in6 *sin6; 1024888973f5SMichael Tuexen 10250ecd976eSBjoern A. Zeeb sin6 = (struct sockaddr_in6 *)nam; 10260ecd976eSBjoern A. Zeeb if (sin6->sin6_len != sizeof(*sin6)) { 1027888973f5SMichael Tuexen if (m) 1028888973f5SMichael Tuexen m_freem(m); 1029888973f5SMichael Tuexen error = EINVAL; 1030888973f5SMichael Tuexen goto out; 1031888973f5SMichael Tuexen } 10320ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 1033888973f5SMichael Tuexen if (m) 1034888973f5SMichael Tuexen m_freem(m); 1035888973f5SMichael Tuexen error = EAFNOSUPPORT; 1036888973f5SMichael Tuexen goto out; 1037888973f5SMichael Tuexen } 10380ecd976eSBjoern A. Zeeb if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 1039888973f5SMichael Tuexen #ifdef INET 1040888973f5SMichael Tuexen if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 1041888973f5SMichael Tuexen error = EINVAL; 1042888973f5SMichael Tuexen if (m) 1043888973f5SMichael Tuexen m_freem(m); 1044888973f5SMichael Tuexen goto out; 1045888973f5SMichael Tuexen } 1046888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV4) == 0) { 1047888973f5SMichael Tuexen error = EAFNOSUPPORT; 1048888973f5SMichael Tuexen if (m) 1049888973f5SMichael Tuexen m_freem(m); 1050888973f5SMichael Tuexen goto out; 1051888973f5SMichael Tuexen } 10524a91aa8fSMichael Tuexen restoreflags = true; 1053888973f5SMichael Tuexen inp->inp_vflag &= ~INP_IPV6; 1054888973f5SMichael Tuexen sinp = &sin; 10550ecd976eSBjoern A. Zeeb in6_sin6_2_sin(sinp, sin6); 1056888973f5SMichael Tuexen if (IN_MULTICAST( 1057888973f5SMichael Tuexen ntohl(sinp->sin_addr.s_addr))) { 1058888973f5SMichael Tuexen error = EAFNOSUPPORT; 1059888973f5SMichael Tuexen if (m) 1060888973f5SMichael Tuexen m_freem(m); 1061888973f5SMichael Tuexen goto out; 1062888973f5SMichael Tuexen } 1063888973f5SMichael Tuexen if ((error = prison_remote_ip4(td->td_ucred, 1064888973f5SMichael Tuexen &sinp->sin_addr))) { 1065888973f5SMichael Tuexen if (m) 1066888973f5SMichael Tuexen m_freem(m); 1067888973f5SMichael Tuexen goto out; 1068888973f5SMichael Tuexen } 1069888973f5SMichael Tuexen isipv6 = 0; 1070888973f5SMichael Tuexen #else /* !INET */ 1071888973f5SMichael Tuexen error = EAFNOSUPPORT; 1072888973f5SMichael Tuexen if (m) 1073888973f5SMichael Tuexen m_freem(m); 1074888973f5SMichael Tuexen goto out; 1075888973f5SMichael Tuexen #endif /* INET */ 1076888973f5SMichael Tuexen } else { 1077888973f5SMichael Tuexen if ((inp->inp_vflag & INP_IPV6) == 0) { 1078888973f5SMichael Tuexen if (m) 1079888973f5SMichael Tuexen m_freem(m); 1080888973f5SMichael Tuexen error = EAFNOSUPPORT; 1081888973f5SMichael Tuexen goto out; 1082888973f5SMichael Tuexen } 10834a91aa8fSMichael Tuexen restoreflags = true; 1084888973f5SMichael Tuexen inp->inp_vflag &= ~INP_IPV4; 1085888973f5SMichael Tuexen inp->inp_inc.inc_flags |= INC_ISIPV6; 1086888973f5SMichael Tuexen if ((error = prison_remote_ip6(td->td_ucred, 10870ecd976eSBjoern A. Zeeb &sin6->sin6_addr))) { 1088888973f5SMichael Tuexen if (m) 1089888973f5SMichael Tuexen m_freem(m); 1090888973f5SMichael Tuexen goto out; 1091888973f5SMichael Tuexen } 1092888973f5SMichael Tuexen isipv6 = 1; 1093888973f5SMichael Tuexen } 1094888973f5SMichael Tuexen break; 1095888973f5SMichael Tuexen } 1096888973f5SMichael Tuexen #endif /* INET6 */ 1097888973f5SMichael Tuexen default: 1098888973f5SMichael Tuexen if (m) 1099888973f5SMichael Tuexen m_freem(m); 1100888973f5SMichael Tuexen error = EAFNOSUPPORT; 1101888973f5SMichael Tuexen goto out; 1102888973f5SMichael Tuexen } 1103888973f5SMichael Tuexen } 11049c9906e9SPeter Wemm if (control) { 11059c9906e9SPeter Wemm /* TCP doesn't do control messages (rights, creds, etc) */ 11069c9906e9SPeter Wemm if (control->m_len) { 11079c9906e9SPeter Wemm m_freem(control); 11082c37256eSGarrett Wollman if (m) 11092c37256eSGarrett Wollman m_freem(m); 1110744f87eaSDavid Greenman error = EINVAL; 1111744f87eaSDavid Greenman goto out; 11122c37256eSGarrett Wollman } 11139c9906e9SPeter Wemm m_freem(control); /* empty control, just free it */ 11149c9906e9SPeter Wemm } 11152c37256eSGarrett Wollman if (!(flags & PRUS_OOB)) { 1116651e4e6aSGleb Smirnoff sbappendstream(&so->so_snd, m, flags); 11172c37256eSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 11182c37256eSGarrett Wollman /* 11192c37256eSGarrett Wollman * Do implied connect if not yet connected, 11202c37256eSGarrett Wollman * initialize window to default value, and 11210c39d38dSGleb Smirnoff * initialize maxseg using peer's cached MSS. 11222c37256eSGarrett Wollman */ 1123fb59c426SYoshinobu Inoue #ifdef INET6 1124fb59c426SYoshinobu Inoue if (isipv6) 1125b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 1126fb59c426SYoshinobu Inoue #endif /* INET6 */ 1127b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1128b287c6c7SBjoern A. Zeeb else 1129b287c6c7SBjoern A. Zeeb #endif 1130b287c6c7SBjoern A. Zeeb #ifdef INET 1131888973f5SMichael Tuexen error = tcp_connect(tp, 1132888973f5SMichael Tuexen (struct sockaddr *)sinp, td); 1133b287c6c7SBjoern A. Zeeb #endif 11344a91aa8fSMichael Tuexen /* 11354a91aa8fSMichael Tuexen * The bind operation in tcp_connect succeeded. We 11364a91aa8fSMichael Tuexen * no longer want to restore the flags if later 11374a91aa8fSMichael Tuexen * operations fail. 11384a91aa8fSMichael Tuexen */ 11394a91aa8fSMichael Tuexen if (error == 0 || inp->inp_lport != 0) 11404a91aa8fSMichael Tuexen restoreflags = false; 11414a91aa8fSMichael Tuexen 11422c37256eSGarrett Wollman if (error) 11432c37256eSGarrett Wollman goto out; 1144c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) 1145c560df6fSPatrick Kelsey tcp_fastopen_connect(tp); 114618a75309SPatrick Kelsey else { 11472c37256eSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 11482c37256eSGarrett Wollman tcp_mss(tp, -1); 11492c37256eSGarrett Wollman } 1150c560df6fSPatrick Kelsey } 11512c37256eSGarrett Wollman if (flags & PRUS_EOF) { 11522c37256eSGarrett Wollman /* 11532c37256eSGarrett Wollman * Close the send side of the connection after 11542c37256eSGarrett Wollman * the data is sent. 11552c37256eSGarrett Wollman */ 115697a95ee1SGleb Smirnoff NET_EPOCH_ASSERT(); 11572c37256eSGarrett Wollman socantsendmore(so); 1158623dce13SRobert Watson tcp_usrclosed(tp); 11592c37256eSGarrett Wollman } 11602cbcd3c1SGleb Smirnoff if (!(inp->inp_flags & INP_DROPPED) && 11612cbcd3c1SGleb Smirnoff !(flags & PRUS_NOTREADY)) { 1162b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 1163b0acefa8SBill Fenner tp->t_flags |= TF_MORETOCOME; 116455bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 1165b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 1166b0acefa8SBill Fenner tp->t_flags &= ~TF_MORETOCOME; 1167b0acefa8SBill Fenner } 11682c37256eSGarrett Wollman } else { 1169623dce13SRobert Watson /* 1170623dce13SRobert Watson * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 1171623dce13SRobert Watson */ 1172d2bc35abSRobert Watson SOCKBUF_LOCK(&so->so_snd); 11732c37256eSGarrett Wollman if (sbspace(&so->so_snd) < -512) { 1174d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 11752c37256eSGarrett Wollman m_freem(m); 11762c37256eSGarrett Wollman error = ENOBUFS; 11772c37256eSGarrett Wollman goto out; 11782c37256eSGarrett Wollman } 11792c37256eSGarrett Wollman /* 11802c37256eSGarrett Wollman * According to RFC961 (Assigned Protocols), 11812c37256eSGarrett Wollman * the urgent pointer points to the last octet 11822c37256eSGarrett Wollman * of urgent data. We continue, however, 11832c37256eSGarrett Wollman * to consider it to indicate the first octet 11842c37256eSGarrett Wollman * of data past the urgent section. 11852c37256eSGarrett Wollman * Otherwise, snd_up should be one lower. 11862c37256eSGarrett Wollman */ 1187651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_snd, m, flags); 1188d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 1189ef53690bSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 1190ef53690bSGarrett Wollman /* 1191ef53690bSGarrett Wollman * Do implied connect if not yet connected, 1192ef53690bSGarrett Wollman * initialize window to default value, and 11930c39d38dSGleb Smirnoff * initialize maxseg using peer's cached MSS. 1194ef53690bSGarrett Wollman */ 119518a75309SPatrick Kelsey 1196c560df6fSPatrick Kelsey /* 1197c560df6fSPatrick Kelsey * Not going to contemplate SYN|URG 1198c560df6fSPatrick Kelsey */ 1199c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags)) 1200c560df6fSPatrick Kelsey tp->t_flags &= ~TF_FASTOPEN; 1201fb59c426SYoshinobu Inoue #ifdef INET6 1202fb59c426SYoshinobu Inoue if (isipv6) 1203b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 1204fb59c426SYoshinobu Inoue #endif /* INET6 */ 1205b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1206b287c6c7SBjoern A. Zeeb else 1207b287c6c7SBjoern A. Zeeb #endif 1208b287c6c7SBjoern A. Zeeb #ifdef INET 1209888973f5SMichael Tuexen error = tcp_connect(tp, 1210888973f5SMichael Tuexen (struct sockaddr *)sinp, td); 1211b287c6c7SBjoern A. Zeeb #endif 12124a91aa8fSMichael Tuexen /* 12134a91aa8fSMichael Tuexen * The bind operation in tcp_connect succeeded. We 12144a91aa8fSMichael Tuexen * no longer want to restore the flags if later 12154a91aa8fSMichael Tuexen * operations fail. 12164a91aa8fSMichael Tuexen */ 12174a91aa8fSMichael Tuexen if (error == 0 || inp->inp_lport != 0) 12184a91aa8fSMichael Tuexen restoreflags = false; 12194a91aa8fSMichael Tuexen 1220ef53690bSGarrett Wollman if (error) 1221ef53690bSGarrett Wollman goto out; 1222ef53690bSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 1223ef53690bSGarrett Wollman tcp_mss(tp, -1); 1224623dce13SRobert Watson } 1225300fa232SGleb Smirnoff tp->snd_up = tp->snd_una + sbavail(&so->so_snd); 12262cbcd3c1SGleb Smirnoff if (!(flags & PRUS_NOTREADY)) { 12272cdbfa66SPaul Saab tp->t_flags |= TF_FORCEDATA; 122855bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 12292cdbfa66SPaul Saab tp->t_flags &= ~TF_FORCEDATA; 12302c37256eSGarrett Wollman } 12312cbcd3c1SGleb Smirnoff } 12322529f56eSJonathan T. Looney TCP_LOG_EVENT(tp, NULL, 12332529f56eSJonathan T. Looney &inp->inp_socket->so_rcv, 12342529f56eSJonathan T. Looney &inp->inp_socket->so_snd, 12352529f56eSJonathan T. Looney TCP_LOG_USERSEND, error, 12362529f56eSJonathan T. Looney 0, NULL, false); 1237d1401c90SRobert Watson out: 12384a91aa8fSMichael Tuexen /* 12394a91aa8fSMichael Tuexen * If the request was unsuccessful and we changed flags, 12404a91aa8fSMichael Tuexen * restore the original flags. 12414a91aa8fSMichael Tuexen */ 12424a91aa8fSMichael Tuexen if (error != 0 && restoreflags) { 12434a91aa8fSMichael Tuexen inp->inp_vflag = vflagsav; 12444a91aa8fSMichael Tuexen inp->inp_inc.inc_flags = incflagsav; 12454a91aa8fSMichael Tuexen } 1246d1401c90SRobert Watson TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB : 12472c37256eSGarrett Wollman ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 12485d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB : 12495d06879aSGeorge V. Neville-Neil ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 12508501a69cSRobert Watson INP_WUNLOCK(inp); 125197a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 125273fddedaSPeter Grehan return (error); 12532c37256eSGarrett Wollman } 12542c37256eSGarrett Wollman 12552cbcd3c1SGleb Smirnoff static int 12562cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count) 12572cbcd3c1SGleb Smirnoff { 1258*109eb549SGleb Smirnoff struct epoch_tracker et; 12592cbcd3c1SGleb Smirnoff struct inpcb *inp; 12602cbcd3c1SGleb Smirnoff struct tcpcb *tp; 12612cbcd3c1SGleb Smirnoff int error; 12622cbcd3c1SGleb Smirnoff 12632cbcd3c1SGleb Smirnoff inp = sotoinpcb(so); 12642cbcd3c1SGleb Smirnoff INP_WLOCK(inp); 12652cbcd3c1SGleb Smirnoff if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 12662cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 126782334850SJohn Baldwin mb_free_notready(m, count); 12682cbcd3c1SGleb Smirnoff return (ECONNRESET); 12692cbcd3c1SGleb Smirnoff } 12702cbcd3c1SGleb Smirnoff tp = intotcpcb(inp); 12712cbcd3c1SGleb Smirnoff 12722cbcd3c1SGleb Smirnoff SOCKBUF_LOCK(&so->so_snd); 12732cbcd3c1SGleb Smirnoff error = sbready(&so->so_snd, m, count); 12742cbcd3c1SGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 1275*109eb549SGleb Smirnoff if (error == 0) { 1276*109eb549SGleb Smirnoff NET_EPOCH_ENTER(et); 127755bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 1278*109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 1279*109eb549SGleb Smirnoff } 12802cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 12812cbcd3c1SGleb Smirnoff 12822cbcd3c1SGleb Smirnoff return (error); 12832cbcd3c1SGleb Smirnoff } 12842cbcd3c1SGleb Smirnoff 12852c37256eSGarrett Wollman /* 1286a152f8a3SRobert Watson * Abort the TCP. Drop the connection abruptly. 12872c37256eSGarrett Wollman */ 1288ac45e92fSRobert Watson static void 12892c37256eSGarrett Wollman tcp_usr_abort(struct socket *so) 12902c37256eSGarrett Wollman { 1291f76fcf6dSJeffrey Hsu struct inpcb *inp; 1292a152f8a3SRobert Watson struct tcpcb *tp = NULL; 12936573d758SMatt Macy struct epoch_tracker et; 1294623dce13SRobert Watson TCPDEBUG0; 1295c78cbc7bSRobert Watson 1296ac45e92fSRobert Watson inp = sotoinpcb(so); 1297c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 1298c78cbc7bSRobert Watson 129997a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 13008501a69cSRobert Watson INP_WLOCK(inp); 1301c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 1302c78cbc7bSRobert Watson ("tcp_usr_abort: inp_socket == NULL")); 1303c78cbc7bSRobert Watson 1304c78cbc7bSRobert Watson /* 1305a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, drop. 1306c78cbc7bSRobert Watson */ 1307ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_TIMEWAIT) && 1308ad71fe3cSRobert Watson !(inp->inp_flags & INP_DROPPED)) { 1309c78cbc7bSRobert Watson tp = intotcpcb(inp); 1310a152f8a3SRobert Watson TCPDEBUG1(); 13118fa799bdSJonathan T. Looney tp = tcp_drop(tp, ECONNABORTED); 13128fa799bdSJonathan T. Looney if (tp == NULL) 13138fa799bdSJonathan T. Looney goto dropped; 1314a152f8a3SRobert Watson TCPDEBUG2(PRU_ABORT); 13155d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ABORT); 1316c78cbc7bSRobert Watson } 1317ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1318a152f8a3SRobert Watson SOCK_LOCK(so); 1319a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 1320a152f8a3SRobert Watson SOCK_UNLOCK(so); 1321ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1322a152f8a3SRobert Watson } 13238501a69cSRobert Watson INP_WUNLOCK(inp); 13248fa799bdSJonathan T. Looney dropped: 132597a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 1326a152f8a3SRobert Watson } 1327a152f8a3SRobert Watson 1328a152f8a3SRobert Watson /* 1329a152f8a3SRobert Watson * TCP socket is closed. Start friendly disconnect. 1330a152f8a3SRobert Watson */ 1331a152f8a3SRobert Watson static void 1332a152f8a3SRobert Watson tcp_usr_close(struct socket *so) 1333a152f8a3SRobert Watson { 1334a152f8a3SRobert Watson struct inpcb *inp; 1335a152f8a3SRobert Watson struct tcpcb *tp = NULL; 13366573d758SMatt Macy struct epoch_tracker et; 1337a152f8a3SRobert Watson TCPDEBUG0; 1338a152f8a3SRobert Watson 1339a152f8a3SRobert Watson inp = sotoinpcb(so); 1340a152f8a3SRobert Watson KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL")); 1341a152f8a3SRobert Watson 134297a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 13438501a69cSRobert Watson INP_WLOCK(inp); 1344a152f8a3SRobert Watson KASSERT(inp->inp_socket != NULL, 1345a152f8a3SRobert Watson ("tcp_usr_close: inp_socket == NULL")); 1346a152f8a3SRobert Watson 1347a152f8a3SRobert Watson /* 1348a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, initiate 1349a152f8a3SRobert Watson * a disconnect. 1350a152f8a3SRobert Watson */ 1351ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_TIMEWAIT) && 1352ad71fe3cSRobert Watson !(inp->inp_flags & INP_DROPPED)) { 1353a152f8a3SRobert Watson tp = intotcpcb(inp); 1354a152f8a3SRobert Watson TCPDEBUG1(); 1355a152f8a3SRobert Watson tcp_disconnect(tp); 1356a152f8a3SRobert Watson TCPDEBUG2(PRU_CLOSE); 13575d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CLOSE); 1358a152f8a3SRobert Watson } 1359ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1360a152f8a3SRobert Watson SOCK_LOCK(so); 1361a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 1362a152f8a3SRobert Watson SOCK_UNLOCK(so); 1363ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1364a152f8a3SRobert Watson } 13658501a69cSRobert Watson INP_WUNLOCK(inp); 136697a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 13672c37256eSGarrett Wollman } 13682c37256eSGarrett Wollman 13692c37256eSGarrett Wollman /* 13702c37256eSGarrett Wollman * Receive out-of-band data. 13712c37256eSGarrett Wollman */ 13722c37256eSGarrett Wollman static int 13732c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 13742c37256eSGarrett Wollman { 13752c37256eSGarrett Wollman int error = 0; 1376f76fcf6dSJeffrey Hsu struct inpcb *inp; 1377623dce13SRobert Watson struct tcpcb *tp = NULL; 13782c37256eSGarrett Wollman 1379623dce13SRobert Watson TCPDEBUG0; 1380623dce13SRobert Watson inp = sotoinpcb(so); 1381623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 13828501a69cSRobert Watson INP_WLOCK(inp); 1383ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 138421367f63SSam Leffler error = ECONNRESET; 1385623dce13SRobert Watson goto out; 1386623dce13SRobert Watson } 1387623dce13SRobert Watson tp = intotcpcb(inp); 1388623dce13SRobert Watson TCPDEBUG1(); 13892c37256eSGarrett Wollman if ((so->so_oobmark == 0 && 1390c0b99ffaSRobert Watson (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 13914cc20ab1SSeigo Tanimura so->so_options & SO_OOBINLINE || 13924cc20ab1SSeigo Tanimura tp->t_oobflags & TCPOOB_HADDATA) { 13932c37256eSGarrett Wollman error = EINVAL; 13942c37256eSGarrett Wollman goto out; 13952c37256eSGarrett Wollman } 13962c37256eSGarrett Wollman if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 13972c37256eSGarrett Wollman error = EWOULDBLOCK; 13982c37256eSGarrett Wollman goto out; 13992c37256eSGarrett Wollman } 14002c37256eSGarrett Wollman m->m_len = 1; 14012c37256eSGarrett Wollman *mtod(m, caddr_t) = tp->t_iobc; 14022c37256eSGarrett Wollman if ((flags & MSG_PEEK) == 0) 14032c37256eSGarrett Wollman tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1404623dce13SRobert Watson 1405623dce13SRobert Watson out: 1406623dce13SRobert Watson TCPDEBUG2(PRU_RCVOOB); 14075d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVOOB); 14088501a69cSRobert Watson INP_WUNLOCK(inp); 1409623dce13SRobert Watson return (error); 14102c37256eSGarrett Wollman } 14112c37256eSGarrett Wollman 1412b287c6c7SBjoern A. Zeeb #ifdef INET 14132c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = { 1414756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1415756d52a1SPoul-Henning Kamp .pru_accept = tcp_usr_accept, 1416756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1417756d52a1SPoul-Henning Kamp .pru_bind = tcp_usr_bind, 1418756d52a1SPoul-Henning Kamp .pru_connect = tcp_usr_connect, 1419756d52a1SPoul-Henning Kamp .pru_control = in_control, 1420756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1421756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1422756d52a1SPoul-Henning Kamp .pru_listen = tcp_usr_listen, 142354d642bbSRobert Watson .pru_peeraddr = in_getpeeraddr, 1424756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1425756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1426756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 14272cbcd3c1SGleb Smirnoff .pru_ready = tcp_usr_ready, 1428756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 142954d642bbSRobert Watson .pru_sockaddr = in_getsockaddr, 1430a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1431a152f8a3SRobert Watson .pru_close = tcp_usr_close, 14322c37256eSGarrett Wollman }; 1433b287c6c7SBjoern A. Zeeb #endif /* INET */ 1434df8bae1dSRodney W. Grimes 1435fb59c426SYoshinobu Inoue #ifdef INET6 1436fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = { 1437756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1438756d52a1SPoul-Henning Kamp .pru_accept = tcp6_usr_accept, 1439756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1440756d52a1SPoul-Henning Kamp .pru_bind = tcp6_usr_bind, 1441756d52a1SPoul-Henning Kamp .pru_connect = tcp6_usr_connect, 1442756d52a1SPoul-Henning Kamp .pru_control = in6_control, 1443756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1444756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1445756d52a1SPoul-Henning Kamp .pru_listen = tcp6_usr_listen, 1446756d52a1SPoul-Henning Kamp .pru_peeraddr = in6_mapped_peeraddr, 1447756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1448756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1449756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 14502cbcd3c1SGleb Smirnoff .pru_ready = tcp_usr_ready, 1451756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 1452756d52a1SPoul-Henning Kamp .pru_sockaddr = in6_mapped_sockaddr, 1453a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1454a152f8a3SRobert Watson .pru_close = tcp_usr_close, 1455fb59c426SYoshinobu Inoue }; 1456fb59c426SYoshinobu Inoue #endif /* INET6 */ 1457fb59c426SYoshinobu Inoue 1458b287c6c7SBjoern A. Zeeb #ifdef INET 1459a0292f23SGarrett Wollman /* 1460a0292f23SGarrett Wollman * Common subroutine to open a TCP connection to remote host specified 1461a0292f23SGarrett Wollman * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 14625200e00eSIan Dowse * port number if needed. Call in_pcbconnect_setup to do the routing and 14635200e00eSIan Dowse * to choose a local host address (interface). If there is an existing 14645200e00eSIan Dowse * incarnation of the same connection in TIME-WAIT state and if the remote 14655200e00eSIan Dowse * host was sending CC options and if the connection duration was < MSL, then 1466a0292f23SGarrett Wollman * truncate the previous TIME-WAIT state and proceed. 1467a0292f23SGarrett Wollman * Initialize connection parameters and enter SYN-SENT state. 1468a0292f23SGarrett Wollman */ 14690312fbe9SPoul-Henning Kamp static int 1470ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1471a0292f23SGarrett Wollman { 1472a0292f23SGarrett Wollman struct inpcb *inp = tp->t_inpcb, *oinp; 1473a0292f23SGarrett Wollman struct socket *so = inp->inp_socket; 14745200e00eSIan Dowse struct in_addr laddr; 14755200e00eSIan Dowse u_short lport; 1476c3229e05SDavid Greenman int error; 1477a0292f23SGarrett Wollman 14788501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1479fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 1480623dce13SRobert Watson 1481a0292f23SGarrett Wollman if (inp->inp_lport == 0) { 14824616026fSErmal Luçi error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 14834616026fSErmal Luçi if (error) 1484fa046d87SRobert Watson goto out; 1485a0292f23SGarrett Wollman } 1486a0292f23SGarrett Wollman 1487a0292f23SGarrett Wollman /* 1488a0292f23SGarrett Wollman * Cannot simply call in_pcbconnect, because there might be an 1489a0292f23SGarrett Wollman * earlier incarnation of this same connection still in 1490a0292f23SGarrett Wollman * TIME_WAIT state, creating an ADDRINUSE error. 1491a0292f23SGarrett Wollman */ 14925200e00eSIan Dowse laddr = inp->inp_laddr; 14935200e00eSIan Dowse lport = inp->inp_lport; 14945200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 1495b0330ed9SPawel Jakub Dawidek &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 14965200e00eSIan Dowse if (error && oinp == NULL) 1497fa046d87SRobert Watson goto out; 1498fa046d87SRobert Watson if (oinp) { 1499fa046d87SRobert Watson error = EADDRINUSE; 1500fa046d87SRobert Watson goto out; 1501fa046d87SRobert Watson } 15025200e00eSIan Dowse inp->inp_laddr = laddr; 150315bd2b43SDavid Greenman in_pcbrehash(inp); 1504fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1505a0292f23SGarrett Wollman 1506087b55eaSAndre Oppermann /* 1507087b55eaSAndre Oppermann * Compute window scaling to request: 1508087b55eaSAndre Oppermann * Scale to fit into sweet spot. See tcp_syncache.c. 1509087b55eaSAndre Oppermann * XXX: This should move to tcp_output(). 1510087b55eaSAndre Oppermann */ 1511a0292f23SGarrett Wollman while (tp->request_r_scale < TCP_MAX_WINSHIFT && 15129b3bc6bfSMike Silbersack (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1513a0292f23SGarrett Wollman tp->request_r_scale++; 1514a0292f23SGarrett Wollman 1515a0292f23SGarrett Wollman soisconnecting(so); 151678b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 151757f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 15188e02b4e0SMichael Tuexen tp->iss = tcp_new_isn(&inp->inp_inc); 15198e02b4e0SMichael Tuexen if (tp->t_flags & TF_REQ_TSTMP) 15208e02b4e0SMichael Tuexen tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc); 1521a0292f23SGarrett Wollman tcp_sendseqinit(tp); 1522a45d2726SAndras Olah 1523a0292f23SGarrett Wollman return 0; 1524fa046d87SRobert Watson 1525fa046d87SRobert Watson out: 1526fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1527fa046d87SRobert Watson return (error); 1528a0292f23SGarrett Wollman } 1529b287c6c7SBjoern A. Zeeb #endif /* INET */ 1530a0292f23SGarrett Wollman 1531fb59c426SYoshinobu Inoue #ifdef INET6 1532fb59c426SYoshinobu Inoue static int 1533ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1534fb59c426SYoshinobu Inoue { 1535a7e201bbSAndrey V. Elsukov struct inpcb *inp = tp->t_inpcb; 1536fb59c426SYoshinobu Inoue int error; 1537fb59c426SYoshinobu Inoue 15388501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1539fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 1540623dce13SRobert Watson 1541fb59c426SYoshinobu Inoue if (inp->inp_lport == 0) { 15424616026fSErmal Luçi error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 15434616026fSErmal Luçi if (error) 1544fa046d87SRobert Watson goto out; 1545fb59c426SYoshinobu Inoue } 1546a7e201bbSAndrey V. Elsukov error = in6_pcbconnect(inp, nam, td->td_ucred); 1547a7e201bbSAndrey V. Elsukov if (error != 0) 1548b598155aSRobert Watson goto out; 1549fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1550fb59c426SYoshinobu Inoue 1551fb59c426SYoshinobu Inoue /* Compute window scaling to request. */ 1552fb59c426SYoshinobu Inoue while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1553970caf60SBjoern A. Zeeb (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1554fb59c426SYoshinobu Inoue tp->request_r_scale++; 1555fb59c426SYoshinobu Inoue 1556a7e201bbSAndrey V. Elsukov soisconnecting(inp->inp_socket); 155778b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 155857f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 15598e02b4e0SMichael Tuexen tp->iss = tcp_new_isn(&inp->inp_inc); 15608e02b4e0SMichael Tuexen if (tp->t_flags & TF_REQ_TSTMP) 15618e02b4e0SMichael Tuexen tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc); 1562fb59c426SYoshinobu Inoue tcp_sendseqinit(tp); 1563fb59c426SYoshinobu Inoue 1564fb59c426SYoshinobu Inoue return 0; 1565fa046d87SRobert Watson 1566fa046d87SRobert Watson out: 1567fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1568fa046d87SRobert Watson return error; 1569fb59c426SYoshinobu Inoue } 1570fb59c426SYoshinobu Inoue #endif /* INET6 */ 1571fb59c426SYoshinobu Inoue 1572cfe8b629SGarrett Wollman /* 1573b8af5dfaSRobert Watson * Export TCP internal state information via a struct tcp_info, based on the 1574b8af5dfaSRobert Watson * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1575b8af5dfaSRobert Watson * (TCP state machine, etc). We export all information using FreeBSD-native 1576b8af5dfaSRobert Watson * constants -- for example, the numeric values for tcpi_state will differ 1577b8af5dfaSRobert Watson * from Linux. 1578b8af5dfaSRobert Watson */ 1579b8af5dfaSRobert Watson static void 1580ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) 1581b8af5dfaSRobert Watson { 1582b8af5dfaSRobert Watson 15838501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1584b8af5dfaSRobert Watson bzero(ti, sizeof(*ti)); 1585b8af5dfaSRobert Watson 1586b8af5dfaSRobert Watson ti->tcpi_state = tp->t_state; 1587b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1588b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 15893529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 1590b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_SACK; 1591b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1592b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_WSCALE; 1593b8af5dfaSRobert Watson ti->tcpi_snd_wscale = tp->snd_scale; 1594b8af5dfaSRobert Watson ti->tcpi_rcv_wscale = tp->rcv_scale; 1595b8af5dfaSRobert Watson } 15963cf38784SMichael Tuexen if (tp->t_flags2 & TF2_ECN_PERMIT) 15975a17b6adSMichael Tuexen ti->tcpi_options |= TCPI_OPT_ECN; 15981baaf834SBruce M Simpson 159943d94734SJohn Baldwin ti->tcpi_rto = tp->t_rxtcur * tick; 16003ac12506SJonathan T. Looney ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick; 16011baaf834SBruce M Simpson ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 16021baaf834SBruce M Simpson ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT; 16031baaf834SBruce M Simpson 1604b8af5dfaSRobert Watson ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1605b8af5dfaSRobert Watson ti->tcpi_snd_cwnd = tp->snd_cwnd; 1606b8af5dfaSRobert Watson 1607b8af5dfaSRobert Watson /* 1608b8af5dfaSRobert Watson * FreeBSD-specific extension fields for tcp_info. 1609b8af5dfaSRobert Watson */ 1610c8443a1dSRobert Watson ti->tcpi_rcv_space = tp->rcv_wnd; 1611535fbad6SKip Macy ti->tcpi_rcv_nxt = tp->rcv_nxt; 1612b8af5dfaSRobert Watson ti->tcpi_snd_wnd = tp->snd_wnd; 16131c18314dSAndre Oppermann ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */ 1614535fbad6SKip Macy ti->tcpi_snd_nxt = tp->snd_nxt; 161543d94734SJohn Baldwin ti->tcpi_snd_mss = tp->t_maxseg; 161643d94734SJohn Baldwin ti->tcpi_rcv_mss = tp->t_maxseg; 1617f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack; 1618f5d34df5SGeorge V. Neville-Neil ti->tcpi_rcv_ooopack = tp->t_rcvoopack; 1619f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_zerowin = tp->t_sndzerowin; 1620a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD 1621a6456410SNavdeep Parhar if (tp->t_flags & TF_TOE) { 1622a6456410SNavdeep Parhar ti->tcpi_options |= TCPI_OPT_TOE; 1623a6456410SNavdeep Parhar tcp_offload_tcp_info(tp, ti); 1624a6456410SNavdeep Parhar } 1625a6456410SNavdeep Parhar #endif 1626b8af5dfaSRobert Watson } 1627b8af5dfaSRobert Watson 1628b8af5dfaSRobert Watson /* 16291e8f5ffaSRobert Watson * tcp_ctloutput() must drop the inpcb lock before performing copyin on 16301e8f5ffaSRobert Watson * socket option arguments. When it re-acquires the lock after the copy, it 16311e8f5ffaSRobert Watson * has to revalidate that the connection is still valid for the socket 16321e8f5ffaSRobert Watson * option. 1633cfe8b629SGarrett Wollman */ 1634bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \ 16358501a69cSRobert Watson INP_WLOCK(inp); \ 1636ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \ 16378501a69cSRobert Watson INP_WUNLOCK(inp); \ 1638bac5bedfSConrad Meyer cleanup; \ 16391e8f5ffaSRobert Watson return (ECONNRESET); \ 16401e8f5ffaSRobert Watson } \ 16411e8f5ffaSRobert Watson tp = intotcpcb(inp); \ 16421e8f5ffaSRobert Watson } while(0) 1643bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */) 16441e8f5ffaSRobert Watson 1645df8bae1dSRodney W. Grimes int 1646ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1647df8bae1dSRodney W. Grimes { 164855bceb1eSRandall Stewart int error; 1649df8bae1dSRodney W. Grimes struct inpcb *inp; 1650cfe8b629SGarrett Wollman struct tcpcb *tp; 165155bceb1eSRandall Stewart struct tcp_function_block *blk; 165255bceb1eSRandall Stewart struct tcp_function_set fsn; 1653df8bae1dSRodney W. Grimes 1654cfe8b629SGarrett Wollman error = 0; 1655df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1656623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 1657cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_TCP) { 1658fb59c426SYoshinobu Inoue #ifdef INET6 16595cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 1660fb59c426SYoshinobu Inoue error = ip6_ctloutput(so, sopt); 16615dff1c38SMichael Tuexen /* 16625dff1c38SMichael Tuexen * In case of the IPV6_USE_MIN_MTU socket option, 16635dff1c38SMichael Tuexen * the INC_IPV6MINMTU flag to announce a corresponding 16645dff1c38SMichael Tuexen * MSS during the initial handshake. 16655dff1c38SMichael Tuexen * If the TCP connection is not in the front states, 16665dff1c38SMichael Tuexen * just reduce the MSS being used. 16675dff1c38SMichael Tuexen * This avoids the sending of TCP segments which will 16685dff1c38SMichael Tuexen * be fragmented at the IPv6 layer. 16695dff1c38SMichael Tuexen */ 16705dff1c38SMichael Tuexen if ((error == 0) && 16715dff1c38SMichael Tuexen (sopt->sopt_dir == SOPT_SET) && 16725dff1c38SMichael Tuexen (sopt->sopt_level == IPPROTO_IPV6) && 16735dff1c38SMichael Tuexen (sopt->sopt_name == IPV6_USE_MIN_MTU)) { 16745dff1c38SMichael Tuexen INP_WLOCK(inp); 16755dff1c38SMichael Tuexen if ((inp->inp_flags & 16765dff1c38SMichael Tuexen (INP_TIMEWAIT | INP_DROPPED))) { 16775dff1c38SMichael Tuexen INP_WUNLOCK(inp); 16785dff1c38SMichael Tuexen return (ECONNRESET); 16795dff1c38SMichael Tuexen } 16805dff1c38SMichael Tuexen inp->inp_inc.inc_flags |= INC_IPV6MINMTU; 16815dff1c38SMichael Tuexen tp = intotcpcb(inp); 16825dff1c38SMichael Tuexen if ((tp->t_state >= TCPS_SYN_SENT) && 16835dff1c38SMichael Tuexen (inp->inp_inc.inc_flags & INC_ISIPV6)) { 16845dff1c38SMichael Tuexen struct ip6_pktopts *opt; 16855dff1c38SMichael Tuexen 16865dff1c38SMichael Tuexen opt = inp->in6p_outputopts; 16875dff1c38SMichael Tuexen if ((opt != NULL) && 16885dff1c38SMichael Tuexen (opt->ip6po_minmtu == 16895dff1c38SMichael Tuexen IP6PO_MINMTU_ALL)) { 16905dff1c38SMichael Tuexen if (tp->t_maxseg > TCP6_MSS) { 16915dff1c38SMichael Tuexen tp->t_maxseg = TCP6_MSS; 16925dff1c38SMichael Tuexen } 16935dff1c38SMichael Tuexen } 16945dff1c38SMichael Tuexen } 16955dff1c38SMichael Tuexen INP_WUNLOCK(inp); 16965dff1c38SMichael Tuexen } 1697b287c6c7SBjoern A. Zeeb } 1698fb59c426SYoshinobu Inoue #endif /* INET6 */ 1699b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1700b287c6c7SBjoern A. Zeeb else 1701b287c6c7SBjoern A. Zeeb #endif 1702b287c6c7SBjoern A. Zeeb #ifdef INET 1703b287c6c7SBjoern A. Zeeb { 1704cfe8b629SGarrett Wollman error = ip_ctloutput(so, sopt); 17051e8f5ffaSRobert Watson } 17061e8f5ffaSRobert Watson #endif 1707df8bae1dSRodney W. Grimes return (error); 1708df8bae1dSRodney W. Grimes } 170968cea2b1SJohn Baldwin INP_WLOCK(inp); 1710ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 17118501a69cSRobert Watson INP_WUNLOCK(inp); 17121e8f5ffaSRobert Watson return (ECONNRESET); 1713623dce13SRobert Watson } 171455bceb1eSRandall Stewart tp = intotcpcb(inp); 171555bceb1eSRandall Stewart /* 171655bceb1eSRandall Stewart * Protect the TCP option TCP_FUNCTION_BLK so 171755bceb1eSRandall Stewart * that a sub-function can *never* overwrite this. 171855bceb1eSRandall Stewart */ 171955bceb1eSRandall Stewart if ((sopt->sopt_dir == SOPT_SET) && 172055bceb1eSRandall Stewart (sopt->sopt_name == TCP_FUNCTION_BLK)) { 172155bceb1eSRandall Stewart INP_WUNLOCK(inp); 172255bceb1eSRandall Stewart error = sooptcopyin(sopt, &fsn, sizeof fsn, 172355bceb1eSRandall Stewart sizeof fsn); 172455bceb1eSRandall Stewart if (error) 172555bceb1eSRandall Stewart return (error); 172655bceb1eSRandall Stewart INP_WLOCK_RECHECK(inp); 172755bceb1eSRandall Stewart blk = find_and_ref_tcp_functions(&fsn); 172855bceb1eSRandall Stewart if (blk == NULL) { 172955bceb1eSRandall Stewart INP_WUNLOCK(inp); 173055bceb1eSRandall Stewart return (ENOENT); 173155bceb1eSRandall Stewart } 1732587d67c0SRandall Stewart if (tp->t_fb == blk) { 1733587d67c0SRandall Stewart /* You already have this */ 1734587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1735587d67c0SRandall Stewart INP_WUNLOCK(inp); 1736587d67c0SRandall Stewart return (0); 1737587d67c0SRandall Stewart } 1738587d67c0SRandall Stewart if (tp->t_state != TCPS_CLOSED) { 1739587d67c0SRandall Stewart /* 1740587d67c0SRandall Stewart * The user has advanced the state 1741587d67c0SRandall Stewart * past the initial point, we may not 1742587d67c0SRandall Stewart * be able to switch. 1743587d67c0SRandall Stewart */ 1744587d67c0SRandall Stewart if (blk->tfb_tcp_handoff_ok != NULL) { 1745587d67c0SRandall Stewart /* 1746587d67c0SRandall Stewart * Does the stack provide a 1747587d67c0SRandall Stewart * query mechanism, if so it may 1748587d67c0SRandall Stewart * still be possible? 1749587d67c0SRandall Stewart */ 1750587d67c0SRandall Stewart error = (*blk->tfb_tcp_handoff_ok)(tp); 1751c6c0be27SMichael Tuexen } else 1752c6c0be27SMichael Tuexen error = EINVAL; 1753587d67c0SRandall Stewart if (error) { 1754587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1755587d67c0SRandall Stewart INP_WUNLOCK(inp); 1756587d67c0SRandall Stewart return(error); 1757587d67c0SRandall Stewart } 1758587d67c0SRandall Stewart } 175955bceb1eSRandall Stewart if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) { 176055bceb1eSRandall Stewart refcount_release(&blk->tfb_refcnt); 176155bceb1eSRandall Stewart INP_WUNLOCK(inp); 176255bceb1eSRandall Stewart return (ENOENT); 176355bceb1eSRandall Stewart } 176455bceb1eSRandall Stewart /* 176555bceb1eSRandall Stewart * Release the old refcnt, the 1766587d67c0SRandall Stewart * lookup acquired a ref on the 1767587d67c0SRandall Stewart * new one already. 176855bceb1eSRandall Stewart */ 1769587d67c0SRandall Stewart if (tp->t_fb->tfb_tcp_fb_fini) { 1770587d67c0SRandall Stewart /* 1771587d67c0SRandall Stewart * Tell the stack to cleanup with 0 i.e. 1772587d67c0SRandall Stewart * the tcb is not going away. 1773587d67c0SRandall Stewart */ 1774587d67c0SRandall Stewart (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0); 1775587d67c0SRandall Stewart } 17763ee9c3c4SRandall Stewart #ifdef TCPHPTS 17773ee9c3c4SRandall Stewart /* Assure that we are not on any hpts */ 17783ee9c3c4SRandall Stewart tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL); 17793ee9c3c4SRandall Stewart #endif 17803ee9c3c4SRandall Stewart if (blk->tfb_tcp_fb_init) { 17813ee9c3c4SRandall Stewart error = (*blk->tfb_tcp_fb_init)(tp); 17823ee9c3c4SRandall Stewart if (error) { 17833ee9c3c4SRandall Stewart refcount_release(&blk->tfb_refcnt); 17843ee9c3c4SRandall Stewart if (tp->t_fb->tfb_tcp_fb_init) { 17853ee9c3c4SRandall Stewart if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) { 17863ee9c3c4SRandall Stewart /* Fall back failed, drop the connection */ 17873ee9c3c4SRandall Stewart INP_WUNLOCK(inp); 17883ee9c3c4SRandall Stewart soabort(so); 17893ee9c3c4SRandall Stewart return(error); 17903ee9c3c4SRandall Stewart } 17913ee9c3c4SRandall Stewart } 17923ee9c3c4SRandall Stewart goto err_out; 17933ee9c3c4SRandall Stewart } 17943ee9c3c4SRandall Stewart } 179555bceb1eSRandall Stewart refcount_release(&tp->t_fb->tfb_refcnt); 179655bceb1eSRandall Stewart tp->t_fb = blk; 179755bceb1eSRandall Stewart #ifdef TCP_OFFLOAD 179855bceb1eSRandall Stewart if (tp->t_flags & TF_TOE) { 179955bceb1eSRandall Stewart tcp_offload_ctloutput(tp, sopt->sopt_dir, 180055bceb1eSRandall Stewart sopt->sopt_name); 180155bceb1eSRandall Stewart } 180255bceb1eSRandall Stewart #endif 18033ee9c3c4SRandall Stewart err_out: 180455bceb1eSRandall Stewart INP_WUNLOCK(inp); 180555bceb1eSRandall Stewart return (error); 180655bceb1eSRandall Stewart } else if ((sopt->sopt_dir == SOPT_GET) && 180755bceb1eSRandall Stewart (sopt->sopt_name == TCP_FUNCTION_BLK)) { 1808c73b6f4dSEd Maste strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name, 1809c73b6f4dSEd Maste TCP_FUNCTION_NAME_LEN_MAX); 1810c73b6f4dSEd Maste fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0'; 181155bceb1eSRandall Stewart fsn.pcbcnt = tp->t_fb->tfb_refcnt; 181255bceb1eSRandall Stewart INP_WUNLOCK(inp); 181355bceb1eSRandall Stewart error = sooptcopyout(sopt, &fsn, sizeof fsn); 181455bceb1eSRandall Stewart return (error); 181555bceb1eSRandall Stewart } 181655bceb1eSRandall Stewart /* Pass in the INP locked, called must unlock it */ 181755bceb1eSRandall Stewart return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp)); 181855bceb1eSRandall Stewart } 181955bceb1eSRandall Stewart 18202529f56eSJonathan T. Looney /* 18212529f56eSJonathan T. Looney * If this assert becomes untrue, we need to change the size of the buf 18222529f56eSJonathan T. Looney * variable in tcp_default_ctloutput(). 18232529f56eSJonathan T. Looney */ 18242529f56eSJonathan T. Looney #ifdef CTASSERT 18252529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN); 18262529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN); 18272529f56eSJonathan T. Looney #endif 18282529f56eSJonathan T. Looney 182955bceb1eSRandall Stewart int 183055bceb1eSRandall Stewart tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp) 183155bceb1eSRandall Stewart { 183255bceb1eSRandall Stewart int error, opt, optval; 183355bceb1eSRandall Stewart u_int ui; 183455bceb1eSRandall Stewart struct tcp_info ti; 1835b2e60773SJohn Baldwin #ifdef KERN_TLS 1836b2e60773SJohn Baldwin struct tls_enable tls; 1837b2e60773SJohn Baldwin #endif 183855bceb1eSRandall Stewart struct cc_algo *algo; 18392529f56eSJonathan T. Looney char *pbuf, buf[TCP_LOG_ID_LEN]; 1840adc56f5aSEdward Tomasz Napierala #ifdef STATS 1841adc56f5aSEdward Tomasz Napierala struct statsblob *sbp; 1842adc56f5aSEdward Tomasz Napierala #endif 1843af6fef3aSGleb Smirnoff size_t len; 1844df8bae1dSRodney W. Grimes 1845d519cedbSGleb Smirnoff /* 1846d519cedbSGleb Smirnoff * For TCP_CCALGOOPT forward the control to CC module, for both 1847d519cedbSGleb Smirnoff * SOPT_SET and SOPT_GET. 1848d519cedbSGleb Smirnoff */ 1849d519cedbSGleb Smirnoff switch (sopt->sopt_name) { 1850d519cedbSGleb Smirnoff case TCP_CCALGOOPT: 1851d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 1852c8b53cedSMichael Tuexen if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT) 1853c8b53cedSMichael Tuexen return (EINVAL); 1854af6fef3aSGleb Smirnoff pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO); 1855af6fef3aSGleb Smirnoff error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize, 1856d519cedbSGleb Smirnoff sopt->sopt_valsize); 1857d519cedbSGleb Smirnoff if (error) { 1858af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 1859d519cedbSGleb Smirnoff return (error); 1860d519cedbSGleb Smirnoff } 1861bac5bedfSConrad Meyer INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP)); 1862d519cedbSGleb Smirnoff if (CC_ALGO(tp)->ctl_output != NULL) 1863af6fef3aSGleb Smirnoff error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf); 1864d519cedbSGleb Smirnoff else 1865d519cedbSGleb Smirnoff error = ENOENT; 1866d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 1867d519cedbSGleb Smirnoff if (error == 0 && sopt->sopt_dir == SOPT_GET) 1868af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize); 1869af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 1870d519cedbSGleb Smirnoff return (error); 1871d519cedbSGleb Smirnoff } 1872d519cedbSGleb Smirnoff 1873cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 1874cfe8b629SGarrett Wollman case SOPT_SET: 1875cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1876fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 187788f6b043SBruce M Simpson case TCP_MD5SIG: 1878fcf59617SAndrey V. Elsukov if (!TCPMD5_ENABLED()) { 18798501a69cSRobert Watson INP_WUNLOCK(inp); 1880fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 1881fcf59617SAndrey V. Elsukov } 1882fcf59617SAndrey V. Elsukov error = TCPMD5_PCBCTL(inp, sopt); 18831cfd4b53SBruce M Simpson if (error) 18841e8f5ffaSRobert Watson return (error); 188509fe6320SNavdeep Parhar goto unlock_and_done; 1886fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 188709fe6320SNavdeep Parhar 1888df8bae1dSRodney W. Grimes case TCP_NODELAY: 1889cfe8b629SGarrett Wollman case TCP_NOOPT: 18908501a69cSRobert Watson INP_WUNLOCK(inp); 1891cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1892cfe8b629SGarrett Wollman sizeof optval); 1893cfe8b629SGarrett Wollman if (error) 18941e8f5ffaSRobert Watson return (error); 1895cfe8b629SGarrett Wollman 18968501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 1897cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1898cfe8b629SGarrett Wollman case TCP_NODELAY: 1899cfe8b629SGarrett Wollman opt = TF_NODELAY; 1900cfe8b629SGarrett Wollman break; 1901cfe8b629SGarrett Wollman case TCP_NOOPT: 1902cfe8b629SGarrett Wollman opt = TF_NOOPT; 1903cfe8b629SGarrett Wollman break; 1904cfe8b629SGarrett Wollman default: 1905cfe8b629SGarrett Wollman opt = 0; /* dead code to fool gcc */ 1906cfe8b629SGarrett Wollman break; 1907cfe8b629SGarrett Wollman } 1908cfe8b629SGarrett Wollman 1909cfe8b629SGarrett Wollman if (optval) 1910cfe8b629SGarrett Wollman tp->t_flags |= opt; 1911df8bae1dSRodney W. Grimes else 1912cfe8b629SGarrett Wollman tp->t_flags &= ~opt; 191309fe6320SNavdeep Parhar unlock_and_done: 191409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 191509fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 191609fe6320SNavdeep Parhar tcp_offload_ctloutput(tp, sopt->sopt_dir, 191709fe6320SNavdeep Parhar sopt->sopt_name); 191809fe6320SNavdeep Parhar } 191909fe6320SNavdeep Parhar #endif 19208501a69cSRobert Watson INP_WUNLOCK(inp); 1921df8bae1dSRodney W. Grimes break; 1922df8bae1dSRodney W. Grimes 1923007581c0SJonathan Lemon case TCP_NOPUSH: 19248501a69cSRobert Watson INP_WUNLOCK(inp); 1925007581c0SJonathan Lemon error = sooptcopyin(sopt, &optval, sizeof optval, 1926007581c0SJonathan Lemon sizeof optval); 1927007581c0SJonathan Lemon if (error) 19281e8f5ffaSRobert Watson return (error); 1929007581c0SJonathan Lemon 19308501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 1931007581c0SJonathan Lemon if (optval) 1932007581c0SJonathan Lemon tp->t_flags |= TF_NOPUSH; 1933d28b9e89SJohn Baldwin else if (tp->t_flags & TF_NOPUSH) { 1934007581c0SJonathan Lemon tp->t_flags &= ~TF_NOPUSH; 1935*109eb549SGleb Smirnoff if (TCPS_HAVEESTABLISHED(tp->t_state)) { 1936*109eb549SGleb Smirnoff struct epoch_tracker et; 1937*109eb549SGleb Smirnoff 1938*109eb549SGleb Smirnoff NET_EPOCH_ENTER(et); 193955bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 1940*109eb549SGleb Smirnoff NET_EPOCH_EXIT(et); 1941*109eb549SGleb Smirnoff } 1942007581c0SJonathan Lemon } 194309fe6320SNavdeep Parhar goto unlock_and_done; 1944007581c0SJonathan Lemon 1945df8bae1dSRodney W. Grimes case TCP_MAXSEG: 19468501a69cSRobert Watson INP_WUNLOCK(inp); 1947cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1948cfe8b629SGarrett Wollman sizeof optval); 1949cfe8b629SGarrett Wollman if (error) 19501e8f5ffaSRobert Watson return (error); 1951df8bae1dSRodney W. Grimes 19528501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 195353369ac9SAndre Oppermann if (optval > 0 && optval <= tp->t_maxseg && 1954603724d3SBjoern A. Zeeb optval + 40 >= V_tcp_minmss) 1955cfe8b629SGarrett Wollman tp->t_maxseg = optval; 1956a0292f23SGarrett Wollman else 1957a0292f23SGarrett Wollman error = EINVAL; 195809fe6320SNavdeep Parhar goto unlock_and_done; 1959a0292f23SGarrett Wollman 1960b8af5dfaSRobert Watson case TCP_INFO: 19618501a69cSRobert Watson INP_WUNLOCK(inp); 1962b8af5dfaSRobert Watson error = EINVAL; 1963b8af5dfaSRobert Watson break; 1964b8af5dfaSRobert Watson 1965adc56f5aSEdward Tomasz Napierala case TCP_STATS: 1966adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 1967adc56f5aSEdward Tomasz Napierala #ifdef STATS 1968adc56f5aSEdward Tomasz Napierala error = sooptcopyin(sopt, &optval, sizeof optval, 1969adc56f5aSEdward Tomasz Napierala sizeof optval); 1970adc56f5aSEdward Tomasz Napierala if (error) 1971adc56f5aSEdward Tomasz Napierala return (error); 1972adc56f5aSEdward Tomasz Napierala 1973adc56f5aSEdward Tomasz Napierala if (optval > 0) 1974adc56f5aSEdward Tomasz Napierala sbp = stats_blob_alloc( 1975adc56f5aSEdward Tomasz Napierala V_tcp_perconn_stats_dflt_tpl, 0); 1976adc56f5aSEdward Tomasz Napierala else 1977adc56f5aSEdward Tomasz Napierala sbp = NULL; 1978adc56f5aSEdward Tomasz Napierala 1979adc56f5aSEdward Tomasz Napierala INP_WLOCK_RECHECK(inp); 1980adc56f5aSEdward Tomasz Napierala if ((tp->t_stats != NULL && sbp == NULL) || 1981adc56f5aSEdward Tomasz Napierala (tp->t_stats == NULL && sbp != NULL)) { 1982adc56f5aSEdward Tomasz Napierala struct statsblob *t = tp->t_stats; 1983adc56f5aSEdward Tomasz Napierala tp->t_stats = sbp; 1984adc56f5aSEdward Tomasz Napierala sbp = t; 1985adc56f5aSEdward Tomasz Napierala } 1986adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 1987adc56f5aSEdward Tomasz Napierala 1988adc56f5aSEdward Tomasz Napierala stats_blob_destroy(sbp); 1989adc56f5aSEdward Tomasz Napierala #else 1990adc56f5aSEdward Tomasz Napierala return (EOPNOTSUPP); 1991adc56f5aSEdward Tomasz Napierala #endif /* !STATS */ 1992adc56f5aSEdward Tomasz Napierala break; 1993adc56f5aSEdward Tomasz Napierala 1994dbc42409SLawrence Stewart case TCP_CONGESTION: 1995dbc42409SLawrence Stewart INP_WUNLOCK(inp); 1996af6fef3aSGleb Smirnoff error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1); 1997af6fef3aSGleb Smirnoff if (error) 1998dbc42409SLawrence Stewart break; 1999af6fef3aSGleb Smirnoff buf[sopt->sopt_valsize] = '\0'; 2000af6fef3aSGleb Smirnoff INP_WLOCK_RECHECK(inp); 200173e263b1SGleb Smirnoff CC_LIST_RLOCK(); 200273e263b1SGleb Smirnoff STAILQ_FOREACH(algo, &cc_list, entries) 200373e263b1SGleb Smirnoff if (strncmp(buf, algo->name, 200473e263b1SGleb Smirnoff TCP_CA_NAME_MAX) == 0) 200573e263b1SGleb Smirnoff break; 200673e263b1SGleb Smirnoff CC_LIST_RUNLOCK(); 200773e263b1SGleb Smirnoff if (algo == NULL) { 2008af6fef3aSGleb Smirnoff INP_WUNLOCK(inp); 200973e263b1SGleb Smirnoff error = EINVAL; 201073e263b1SGleb Smirnoff break; 201173e263b1SGleb Smirnoff } 2012dbc42409SLawrence Stewart /* 201373e263b1SGleb Smirnoff * We hold a write lock over the tcb so it's safe to 201473e263b1SGleb Smirnoff * do these things without ordering concerns. 2015dbc42409SLawrence Stewart */ 2016dbc42409SLawrence Stewart if (CC_ALGO(tp)->cb_destroy != NULL) 2017dbc42409SLawrence Stewart CC_ALGO(tp)->cb_destroy(tp->ccv); 201822699887SMatt Macy CC_DATA(tp) = NULL; 2019dbc42409SLawrence Stewart CC_ALGO(tp) = algo; 2020dbc42409SLawrence Stewart /* 202173e263b1SGleb Smirnoff * If something goes pear shaped initialising the new 202273e263b1SGleb Smirnoff * algo, fall back to newreno (which does not 202373e263b1SGleb Smirnoff * require initialisation). 2024dbc42409SLawrence Stewart */ 202573e263b1SGleb Smirnoff if (algo->cb_init != NULL && 202673e263b1SGleb Smirnoff algo->cb_init(tp->ccv) != 0) { 2027dbc42409SLawrence Stewart CC_ALGO(tp) = &newreno_cc_algo; 2028dbc42409SLawrence Stewart /* 202973e263b1SGleb Smirnoff * The only reason init should fail is 2030dbc42409SLawrence Stewart * because of malloc. 2031dbc42409SLawrence Stewart */ 2032dbc42409SLawrence Stewart error = ENOMEM; 2033dbc42409SLawrence Stewart } 203473e263b1SGleb Smirnoff INP_WUNLOCK(inp); 203573e263b1SGleb Smirnoff break; 2036dbc42409SLawrence Stewart 2037b2e60773SJohn Baldwin #ifdef KERN_TLS 2038b2e60773SJohn Baldwin case TCP_TXTLS_ENABLE: 2039b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2040b2e60773SJohn Baldwin error = sooptcopyin(sopt, &tls, sizeof(tls), 2041b2e60773SJohn Baldwin sizeof(tls)); 2042b2e60773SJohn Baldwin if (error) 2043b2e60773SJohn Baldwin break; 2044b2e60773SJohn Baldwin error = ktls_enable_tx(so, &tls); 2045b2e60773SJohn Baldwin break; 2046b2e60773SJohn Baldwin case TCP_TXTLS_MODE: 2047b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2048b2e60773SJohn Baldwin error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 2049b2e60773SJohn Baldwin if (error) 2050b2e60773SJohn Baldwin return (error); 2051b2e60773SJohn Baldwin 2052b2e60773SJohn Baldwin INP_WLOCK_RECHECK(inp); 2053b2e60773SJohn Baldwin error = ktls_set_tx_mode(so, ui); 2054b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2055b2e60773SJohn Baldwin break; 2056b2e60773SJohn Baldwin #endif 2057b2e60773SJohn Baldwin 20589077f387SGleb Smirnoff case TCP_KEEPIDLE: 20599077f387SGleb Smirnoff case TCP_KEEPINTVL: 20609077f387SGleb Smirnoff case TCP_KEEPINIT: 20619077f387SGleb Smirnoff INP_WUNLOCK(inp); 20629077f387SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 20639077f387SGleb Smirnoff if (error) 20649077f387SGleb Smirnoff return (error); 20659077f387SGleb Smirnoff 20669077f387SGleb Smirnoff if (ui > (UINT_MAX / hz)) { 20679077f387SGleb Smirnoff error = EINVAL; 20689077f387SGleb Smirnoff break; 20699077f387SGleb Smirnoff } 20709077f387SGleb Smirnoff ui *= hz; 20719077f387SGleb Smirnoff 20729077f387SGleb Smirnoff INP_WLOCK_RECHECK(inp); 20739077f387SGleb Smirnoff switch (sopt->sopt_name) { 20749077f387SGleb Smirnoff case TCP_KEEPIDLE: 20759077f387SGleb Smirnoff tp->t_keepidle = ui; 20769077f387SGleb Smirnoff /* 20779077f387SGleb Smirnoff * XXX: better check current remaining 20789077f387SGleb Smirnoff * timeout and "merge" it with new value. 20799077f387SGleb Smirnoff */ 20809077f387SGleb Smirnoff if ((tp->t_state > TCPS_LISTEN) && 20819077f387SGleb Smirnoff (tp->t_state <= TCPS_CLOSING)) 20829077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 20839077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 20849077f387SGleb Smirnoff break; 20859077f387SGleb Smirnoff case TCP_KEEPINTVL: 20869077f387SGleb Smirnoff tp->t_keepintvl = ui; 20879077f387SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 20889077f387SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 20899077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 20909077f387SGleb Smirnoff TP_MAXIDLE(tp)); 20919077f387SGleb Smirnoff break; 20929077f387SGleb Smirnoff case TCP_KEEPINIT: 20939077f387SGleb Smirnoff tp->t_keepinit = ui; 20949077f387SGleb Smirnoff if (tp->t_state == TCPS_SYN_RECEIVED || 20959077f387SGleb Smirnoff tp->t_state == TCPS_SYN_SENT) 20969077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 20979077f387SGleb Smirnoff TP_KEEPINIT(tp)); 20989077f387SGleb Smirnoff break; 20999077f387SGleb Smirnoff } 210009fe6320SNavdeep Parhar goto unlock_and_done; 21019077f387SGleb Smirnoff 210285c05144SGleb Smirnoff case TCP_KEEPCNT: 210385c05144SGleb Smirnoff INP_WUNLOCK(inp); 210485c05144SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 210585c05144SGleb Smirnoff if (error) 210685c05144SGleb Smirnoff return (error); 210785c05144SGleb Smirnoff 210885c05144SGleb Smirnoff INP_WLOCK_RECHECK(inp); 210985c05144SGleb Smirnoff tp->t_keepcnt = ui; 211085c05144SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 211185c05144SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 211285c05144SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 211385c05144SGleb Smirnoff TP_MAXIDLE(tp)); 211485c05144SGleb Smirnoff goto unlock_and_done; 211585c05144SGleb Smirnoff 211686a996e6SHiren Panchasara #ifdef TCPPCAP 211786a996e6SHiren Panchasara case TCP_PCAP_OUT: 211886a996e6SHiren Panchasara case TCP_PCAP_IN: 211986a996e6SHiren Panchasara INP_WUNLOCK(inp); 212086a996e6SHiren Panchasara error = sooptcopyin(sopt, &optval, sizeof optval, 212186a996e6SHiren Panchasara sizeof optval); 212286a996e6SHiren Panchasara if (error) 212386a996e6SHiren Panchasara return (error); 212486a996e6SHiren Panchasara 212586a996e6SHiren Panchasara INP_WLOCK_RECHECK(inp); 212686a996e6SHiren Panchasara if (optval >= 0) 212786a996e6SHiren Panchasara tcp_pcap_set_sock_max(TCP_PCAP_OUT ? 212886a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts), 212986a996e6SHiren Panchasara optval); 213086a996e6SHiren Panchasara else 213186a996e6SHiren Panchasara error = EINVAL; 213286a996e6SHiren Panchasara goto unlock_and_done; 213386a996e6SHiren Panchasara #endif 213486a996e6SHiren Panchasara 2135c560df6fSPatrick Kelsey case TCP_FASTOPEN: { 2136c560df6fSPatrick Kelsey struct tcp_fastopen tfo_optval; 2137c560df6fSPatrick Kelsey 2138281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 2139c560df6fSPatrick Kelsey if (!V_tcp_fastopen_client_enable && 2140c560df6fSPatrick Kelsey !V_tcp_fastopen_server_enable) 2141281a0fd4SPatrick Kelsey return (EPERM); 2142281a0fd4SPatrick Kelsey 2143c560df6fSPatrick Kelsey error = sooptcopyin(sopt, &tfo_optval, 2144c560df6fSPatrick Kelsey sizeof(tfo_optval), sizeof(int)); 2145281a0fd4SPatrick Kelsey if (error) 2146281a0fd4SPatrick Kelsey return (error); 2147281a0fd4SPatrick Kelsey 2148281a0fd4SPatrick Kelsey INP_WLOCK_RECHECK(inp); 2149c560df6fSPatrick Kelsey if (tfo_optval.enable) { 2150c560df6fSPatrick Kelsey if (tp->t_state == TCPS_LISTEN) { 2151c560df6fSPatrick Kelsey if (!V_tcp_fastopen_server_enable) { 2152c560df6fSPatrick Kelsey error = EPERM; 2153c560df6fSPatrick Kelsey goto unlock_and_done; 2154c560df6fSPatrick Kelsey } 2155c560df6fSPatrick Kelsey 2156281a0fd4SPatrick Kelsey tp->t_flags |= TF_FASTOPEN; 2157c560df6fSPatrick Kelsey if (tp->t_tfo_pending == NULL) 2158281a0fd4SPatrick Kelsey tp->t_tfo_pending = 2159281a0fd4SPatrick Kelsey tcp_fastopen_alloc_counter(); 2160c560df6fSPatrick Kelsey } else { 2161c560df6fSPatrick Kelsey /* 2162c560df6fSPatrick Kelsey * If a pre-shared key was provided, 2163c560df6fSPatrick Kelsey * stash it in the client cookie 2164c560df6fSPatrick Kelsey * field of the tcpcb for use during 2165c560df6fSPatrick Kelsey * connect. 2166c560df6fSPatrick Kelsey */ 2167c560df6fSPatrick Kelsey if (sopt->sopt_valsize == 2168c560df6fSPatrick Kelsey sizeof(tfo_optval)) { 2169c560df6fSPatrick Kelsey memcpy(tp->t_tfo_cookie.client, 2170c560df6fSPatrick Kelsey tfo_optval.psk, 2171c560df6fSPatrick Kelsey TCP_FASTOPEN_PSK_LEN); 2172c560df6fSPatrick Kelsey tp->t_tfo_client_cookie_len = 2173c560df6fSPatrick Kelsey TCP_FASTOPEN_PSK_LEN; 2174c560df6fSPatrick Kelsey } 2175c560df6fSPatrick Kelsey tp->t_flags |= TF_FASTOPEN; 2176c560df6fSPatrick Kelsey } 2177281a0fd4SPatrick Kelsey } else 2178281a0fd4SPatrick Kelsey tp->t_flags &= ~TF_FASTOPEN; 2179281a0fd4SPatrick Kelsey goto unlock_and_done; 2180c560df6fSPatrick Kelsey } 2181281a0fd4SPatrick Kelsey 2182e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX 21832529f56eSJonathan T. Looney case TCP_LOG: 21842529f56eSJonathan T. Looney INP_WUNLOCK(inp); 21852529f56eSJonathan T. Looney error = sooptcopyin(sopt, &optval, sizeof optval, 21862529f56eSJonathan T. Looney sizeof optval); 21872529f56eSJonathan T. Looney if (error) 21882529f56eSJonathan T. Looney return (error); 21892529f56eSJonathan T. Looney 21902529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 21912529f56eSJonathan T. Looney error = tcp_log_state_change(tp, optval); 21922529f56eSJonathan T. Looney goto unlock_and_done; 21932529f56eSJonathan T. Looney 21942529f56eSJonathan T. Looney case TCP_LOGBUF: 21952529f56eSJonathan T. Looney INP_WUNLOCK(inp); 21962529f56eSJonathan T. Looney error = EINVAL; 21972529f56eSJonathan T. Looney break; 21982529f56eSJonathan T. Looney 21992529f56eSJonathan T. Looney case TCP_LOGID: 22002529f56eSJonathan T. Looney INP_WUNLOCK(inp); 22012529f56eSJonathan T. Looney error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0); 22022529f56eSJonathan T. Looney if (error) 22032529f56eSJonathan T. Looney break; 22042529f56eSJonathan T. Looney buf[sopt->sopt_valsize] = '\0'; 22052529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 22062529f56eSJonathan T. Looney error = tcp_log_set_id(tp, buf); 22072529f56eSJonathan T. Looney /* tcp_log_set_id() unlocks the INP. */ 22082529f56eSJonathan T. Looney break; 22092529f56eSJonathan T. Looney 22102529f56eSJonathan T. Looney case TCP_LOGDUMP: 22112529f56eSJonathan T. Looney case TCP_LOGDUMPID: 22122529f56eSJonathan T. Looney INP_WUNLOCK(inp); 22132529f56eSJonathan T. Looney error = 22142529f56eSJonathan T. Looney sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0); 22152529f56eSJonathan T. Looney if (error) 22162529f56eSJonathan T. Looney break; 22172529f56eSJonathan T. Looney buf[sopt->sopt_valsize] = '\0'; 22182529f56eSJonathan T. Looney INP_WLOCK_RECHECK(inp); 22192529f56eSJonathan T. Looney if (sopt->sopt_name == TCP_LOGDUMP) { 22202529f56eSJonathan T. Looney error = tcp_log_dump_tp_logbuf(tp, buf, 22212529f56eSJonathan T. Looney M_WAITOK, true); 22222529f56eSJonathan T. Looney INP_WUNLOCK(inp); 22232529f56eSJonathan T. Looney } else { 22242529f56eSJonathan T. Looney tcp_log_dump_tp_bucket_logbufs(tp, buf); 22252529f56eSJonathan T. Looney /* 22262529f56eSJonathan T. Looney * tcp_log_dump_tp_bucket_logbufs() drops the 22272529f56eSJonathan T. Looney * INP lock. 22282529f56eSJonathan T. Looney */ 22292529f56eSJonathan T. Looney } 22302529f56eSJonathan T. Looney break; 2231e24e5683SJonathan T. Looney #endif 22322529f56eSJonathan T. Looney 2233df8bae1dSRodney W. Grimes default: 22348501a69cSRobert Watson INP_WUNLOCK(inp); 2235df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 2236df8bae1dSRodney W. Grimes break; 2237df8bae1dSRodney W. Grimes } 2238df8bae1dSRodney W. Grimes break; 2239df8bae1dSRodney W. Grimes 2240cfe8b629SGarrett Wollman case SOPT_GET: 22411e8f5ffaSRobert Watson tp = intotcpcb(inp); 2242cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 2243fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 224488f6b043SBruce M Simpson case TCP_MD5SIG: 2245fcf59617SAndrey V. Elsukov if (!TCPMD5_ENABLED()) { 22468501a69cSRobert Watson INP_WUNLOCK(inp); 2247fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 2248fcf59617SAndrey V. Elsukov } 2249fcf59617SAndrey V. Elsukov error = TCPMD5_PCBCTL(inp, sopt); 22501cfd4b53SBruce M Simpson break; 2251265ed012SBruce M Simpson #endif 22521e8f5ffaSRobert Watson 2253df8bae1dSRodney W. Grimes case TCP_NODELAY: 2254cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NODELAY; 22558501a69cSRobert Watson INP_WUNLOCK(inp); 2256b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2257df8bae1dSRodney W. Grimes break; 2258df8bae1dSRodney W. Grimes case TCP_MAXSEG: 2259cfe8b629SGarrett Wollman optval = tp->t_maxseg; 22608501a69cSRobert Watson INP_WUNLOCK(inp); 2261b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2262df8bae1dSRodney W. Grimes break; 2263a0292f23SGarrett Wollman case TCP_NOOPT: 2264cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOOPT; 22658501a69cSRobert Watson INP_WUNLOCK(inp); 2266b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2267a0292f23SGarrett Wollman break; 2268a0292f23SGarrett Wollman case TCP_NOPUSH: 2269cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOPUSH; 22708501a69cSRobert Watson INP_WUNLOCK(inp); 2271b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 2272b8af5dfaSRobert Watson break; 2273b8af5dfaSRobert Watson case TCP_INFO: 2274b8af5dfaSRobert Watson tcp_fill_info(tp, &ti); 22758501a69cSRobert Watson INP_WUNLOCK(inp); 2276b8af5dfaSRobert Watson error = sooptcopyout(sopt, &ti, sizeof ti); 2277a0292f23SGarrett Wollman break; 2278adc56f5aSEdward Tomasz Napierala case TCP_STATS: 2279adc56f5aSEdward Tomasz Napierala { 2280adc56f5aSEdward Tomasz Napierala #ifdef STATS 2281adc56f5aSEdward Tomasz Napierala int nheld; 2282adc56f5aSEdward Tomasz Napierala TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0; 2283adc56f5aSEdward Tomasz Napierala 2284adc56f5aSEdward Tomasz Napierala error = 0; 2285adc56f5aSEdward Tomasz Napierala socklen_t outsbsz = sopt->sopt_valsize; 2286adc56f5aSEdward Tomasz Napierala if (tp->t_stats == NULL) 2287adc56f5aSEdward Tomasz Napierala error = ENOENT; 2288adc56f5aSEdward Tomasz Napierala else if (outsbsz >= tp->t_stats->cursz) 2289adc56f5aSEdward Tomasz Napierala outsbsz = tp->t_stats->cursz; 2290adc56f5aSEdward Tomasz Napierala else if (outsbsz >= sizeof(struct statsblob)) 2291adc56f5aSEdward Tomasz Napierala outsbsz = sizeof(struct statsblob); 2292adc56f5aSEdward Tomasz Napierala else 2293adc56f5aSEdward Tomasz Napierala error = EINVAL; 2294adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2295adc56f5aSEdward Tomasz Napierala if (error) 2296adc56f5aSEdward Tomasz Napierala break; 2297adc56f5aSEdward Tomasz Napierala 2298adc56f5aSEdward Tomasz Napierala sbp = sopt->sopt_val; 2299adc56f5aSEdward Tomasz Napierala nheld = atop(round_page(((vm_offset_t)sbp) + 2300adc56f5aSEdward Tomasz Napierala (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp)); 2301adc56f5aSEdward Tomasz Napierala vm_page_t ma[nheld]; 2302adc56f5aSEdward Tomasz Napierala if (vm_fault_quick_hold_pages( 2303adc56f5aSEdward Tomasz Napierala &curproc->p_vmspace->vm_map, (vm_offset_t)sbp, 2304adc56f5aSEdward Tomasz Napierala outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma, 2305adc56f5aSEdward Tomasz Napierala nheld) < 0) { 2306adc56f5aSEdward Tomasz Napierala error = EFAULT; 2307adc56f5aSEdward Tomasz Napierala break; 2308adc56f5aSEdward Tomasz Napierala } 2309adc56f5aSEdward Tomasz Napierala 2310adc56f5aSEdward Tomasz Napierala if ((error = copyin_nofault(&(sbp->flags), &sbflags, 2311adc56f5aSEdward Tomasz Napierala SIZEOF_MEMBER(struct statsblob, flags)))) 2312adc56f5aSEdward Tomasz Napierala goto unhold; 2313adc56f5aSEdward Tomasz Napierala 2314adc56f5aSEdward Tomasz Napierala INP_WLOCK_RECHECK(inp); 2315adc56f5aSEdward Tomasz Napierala error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats, 2316adc56f5aSEdward Tomasz Napierala sbflags | SB_CLONE_USRDSTNOFAULT); 2317adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2318adc56f5aSEdward Tomasz Napierala sopt->sopt_valsize = outsbsz; 2319adc56f5aSEdward Tomasz Napierala unhold: 2320adc56f5aSEdward Tomasz Napierala vm_page_unhold_pages(ma, nheld); 2321adc56f5aSEdward Tomasz Napierala #else 2322adc56f5aSEdward Tomasz Napierala INP_WUNLOCK(inp); 2323adc56f5aSEdward Tomasz Napierala error = EOPNOTSUPP; 2324adc56f5aSEdward Tomasz Napierala #endif /* !STATS */ 2325adc56f5aSEdward Tomasz Napierala break; 2326adc56f5aSEdward Tomasz Napierala } 2327dbc42409SLawrence Stewart case TCP_CONGESTION: 2328af6fef3aSGleb Smirnoff len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); 2329dbc42409SLawrence Stewart INP_WUNLOCK(inp); 2330af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, buf, len + 1); 2331dbc42409SLawrence Stewart break; 23322f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 23332f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 23342f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 23352f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 23362f3eb7f4SGleb Smirnoff switch (sopt->sopt_name) { 23372f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 23385a17b6adSMichael Tuexen ui = TP_KEEPIDLE(tp) / hz; 23392f3eb7f4SGleb Smirnoff break; 23402f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 23415a17b6adSMichael Tuexen ui = TP_KEEPINTVL(tp) / hz; 23422f3eb7f4SGleb Smirnoff break; 23432f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 23445a17b6adSMichael Tuexen ui = TP_KEEPINIT(tp) / hz; 23452f3eb7f4SGleb Smirnoff break; 23462f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 23475a17b6adSMichael Tuexen ui = TP_KEEPCNT(tp); 23482f3eb7f4SGleb Smirnoff break; 23492f3eb7f4SGleb Smirnoff } 23502f3eb7f4SGleb Smirnoff INP_WUNLOCK(inp); 23512f3eb7f4SGleb Smirnoff error = sooptcopyout(sopt, &ui, sizeof(ui)); 23522f3eb7f4SGleb Smirnoff break; 235386a996e6SHiren Panchasara #ifdef TCPPCAP 235486a996e6SHiren Panchasara case TCP_PCAP_OUT: 235586a996e6SHiren Panchasara case TCP_PCAP_IN: 235686a996e6SHiren Panchasara optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ? 235786a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts)); 235886a996e6SHiren Panchasara INP_WUNLOCK(inp); 235986a996e6SHiren Panchasara error = sooptcopyout(sopt, &optval, sizeof optval); 236086a996e6SHiren Panchasara break; 236186a996e6SHiren Panchasara #endif 2362281a0fd4SPatrick Kelsey case TCP_FASTOPEN: 2363281a0fd4SPatrick Kelsey optval = tp->t_flags & TF_FASTOPEN; 2364281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 2365281a0fd4SPatrick Kelsey error = sooptcopyout(sopt, &optval, sizeof optval); 2366281a0fd4SPatrick Kelsey break; 2367e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX 23682529f56eSJonathan T. Looney case TCP_LOG: 23692529f56eSJonathan T. Looney optval = tp->t_logstate; 23702529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23712529f56eSJonathan T. Looney error = sooptcopyout(sopt, &optval, sizeof(optval)); 23722529f56eSJonathan T. Looney break; 23732529f56eSJonathan T. Looney case TCP_LOGBUF: 23742529f56eSJonathan T. Looney /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */ 23752529f56eSJonathan T. Looney error = tcp_log_getlogbuf(sopt, tp); 23762529f56eSJonathan T. Looney break; 23772529f56eSJonathan T. Looney case TCP_LOGID: 23782529f56eSJonathan T. Looney len = tcp_log_get_id(tp, buf); 23792529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23802529f56eSJonathan T. Looney error = sooptcopyout(sopt, buf, len + 1); 23812529f56eSJonathan T. Looney break; 23822529f56eSJonathan T. Looney case TCP_LOGDUMP: 23832529f56eSJonathan T. Looney case TCP_LOGDUMPID: 23842529f56eSJonathan T. Looney INP_WUNLOCK(inp); 23852529f56eSJonathan T. Looney error = EINVAL; 23862529f56eSJonathan T. Looney break; 2387e24e5683SJonathan T. Looney #endif 2388b2e60773SJohn Baldwin #ifdef KERN_TLS 2389b2e60773SJohn Baldwin case TCP_TXTLS_MODE: 2390b2e60773SJohn Baldwin optval = ktls_get_tx_mode(so); 2391b2e60773SJohn Baldwin INP_WUNLOCK(inp); 2392b2e60773SJohn Baldwin error = sooptcopyout(sopt, &optval, sizeof(optval)); 2393b2e60773SJohn Baldwin break; 2394b2e60773SJohn Baldwin #endif 2395df8bae1dSRodney W. Grimes default: 23968501a69cSRobert Watson INP_WUNLOCK(inp); 2397df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 2398df8bae1dSRodney W. Grimes break; 2399df8bae1dSRodney W. Grimes } 2400df8bae1dSRodney W. Grimes break; 2401df8bae1dSRodney W. Grimes } 2402df8bae1dSRodney W. Grimes return (error); 2403df8bae1dSRodney W. Grimes } 24048501a69cSRobert Watson #undef INP_WLOCK_RECHECK 2405bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP 2406df8bae1dSRodney W. Grimes 240726e30fbbSDavid Greenman /* 2408df8bae1dSRodney W. Grimes * Attach TCP protocol to socket, allocating 2409df8bae1dSRodney W. Grimes * internet protocol control block, tcp control block, 2410df8bae1dSRodney W. Grimes * bufer space, and entering LISTEN state if to accept connections. 2411df8bae1dSRodney W. Grimes */ 24120312fbe9SPoul-Henning Kamp static int 2413ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so) 2414df8bae1dSRodney W. Grimes { 2415ad3f9ab3SAndre Oppermann struct tcpcb *tp; 2416df8bae1dSRodney W. Grimes struct inpcb *inp; 24176573d758SMatt Macy struct epoch_tracker et; 2418df8bae1dSRodney W. Grimes int error; 2419df8bae1dSRodney W. Grimes 2420df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 2421e233e2acSAndre Oppermann error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace); 2422df8bae1dSRodney W. Grimes if (error) 2423df8bae1dSRodney W. Grimes return (error); 2424df8bae1dSRodney W. Grimes } 24256741ecf5SAndre Oppermann so->so_rcv.sb_flags |= SB_AUTOSIZE; 24266741ecf5SAndre Oppermann so->so_snd.sb_flags |= SB_AUTOSIZE; 242797a95ee1SGleb Smirnoff NET_EPOCH_ENTER(et); 2428603724d3SBjoern A. Zeeb error = in_pcballoc(so, &V_tcbinfo); 2429f2de87feSRobert Watson if (error) { 243097a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 2431df8bae1dSRodney W. Grimes return (error); 2432f2de87feSRobert Watson } 2433df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 2434fb59c426SYoshinobu Inoue #ifdef INET6 24355cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 2436fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 243763ec505aSMichael Tuexen if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 243863ec505aSMichael Tuexen inp->inp_vflag |= INP_IPV4; 2439fb59c426SYoshinobu Inoue inp->in6p_hops = -1; /* use kernel default */ 2440fb59c426SYoshinobu Inoue } 2441fb59c426SYoshinobu Inoue else 2442fb59c426SYoshinobu Inoue #endif 2443cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 2444df8bae1dSRodney W. Grimes tp = tcp_newtcpcb(inp); 2445623dce13SRobert Watson if (tp == NULL) { 2446df8bae1dSRodney W. Grimes in_pcbdetach(inp); 24470206cdb8SBjoern A. Zeeb in_pcbfree(inp); 244897a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 2449df8bae1dSRodney W. Grimes return (ENOBUFS); 2450df8bae1dSRodney W. Grimes } 2451df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 24528501a69cSRobert Watson INP_WUNLOCK(inp); 245397a95ee1SGleb Smirnoff NET_EPOCH_EXIT(et); 2454bf840a17SGleb Smirnoff TCPSTATES_INC(TCPS_CLOSED); 2455df8bae1dSRodney W. Grimes return (0); 2456df8bae1dSRodney W. Grimes } 2457df8bae1dSRodney W. Grimes 2458df8bae1dSRodney W. Grimes /* 2459df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 2460df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 2461df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 2462df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 2463df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 2464df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 2465df8bae1dSRodney W. Grimes */ 2466623dce13SRobert Watson static void 2467ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp) 2468df8bae1dSRodney W. Grimes { 2469e6e0b5ffSRobert Watson struct inpcb *inp = tp->t_inpcb; 2470e6e0b5ffSRobert Watson struct socket *so = inp->inp_socket; 2471e6e0b5ffSRobert Watson 247297a95ee1SGleb Smirnoff NET_EPOCH_ASSERT(); 24738501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 2474df8bae1dSRodney W. Grimes 2475623dce13SRobert Watson /* 2476623dce13SRobert Watson * Neither tcp_close() nor tcp_drop() should return NULL, as the 2477623dce13SRobert Watson * socket is still open. 2478623dce13SRobert Watson */ 24798db239dcSMichael Tuexen if (tp->t_state < TCPS_ESTABLISHED && 24808db239dcSMichael Tuexen !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) { 2481df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2482623dce13SRobert Watson KASSERT(tp != NULL, 2483623dce13SRobert Watson ("tcp_disconnect: tcp_close() returned NULL")); 2484623dce13SRobert Watson } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 2485243917feSSeigo Tanimura tp = tcp_drop(tp, 0); 2486623dce13SRobert Watson KASSERT(tp != NULL, 2487623dce13SRobert Watson ("tcp_disconnect: tcp_drop() returned NULL")); 2488623dce13SRobert Watson } else { 2489df8bae1dSRodney W. Grimes soisdisconnecting(so); 2490df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 2491623dce13SRobert Watson tcp_usrclosed(tp); 2492ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 249355bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 2494df8bae1dSRodney W. Grimes } 2495df8bae1dSRodney W. Grimes } 2496df8bae1dSRodney W. Grimes 2497df8bae1dSRodney W. Grimes /* 2498df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 2499df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 2500df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 2501df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 2502df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 2503df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 2504df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 2505df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 2506df8bae1dSRodney W. Grimes */ 2507623dce13SRobert Watson static void 2508ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp) 2509df8bae1dSRodney W. Grimes { 2510df8bae1dSRodney W. Grimes 251197a95ee1SGleb Smirnoff NET_EPOCH_ASSERT(); 25128501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 2513e6e0b5ffSRobert Watson 2514df8bae1dSRodney W. Grimes switch (tp->t_state) { 2515df8bae1dSRodney W. Grimes case TCPS_LISTEN: 251609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 251709fe6320SNavdeep Parhar tcp_offload_listen_stop(tp); 251809fe6320SNavdeep Parhar #endif 2519550e9d42SHiren Panchasara tcp_state_change(tp, TCPS_CLOSED); 2520bc65987aSKip Macy /* FALLTHROUGH */ 2521bc65987aSKip Macy case TCPS_CLOSED: 2522df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2523623dce13SRobert Watson /* 2524623dce13SRobert Watson * tcp_close() should never return NULL here as the socket is 2525623dce13SRobert Watson * still open. 2526623dce13SRobert Watson */ 2527623dce13SRobert Watson KASSERT(tp != NULL, 2528623dce13SRobert Watson ("tcp_usrclosed: tcp_close() returned NULL")); 2529df8bae1dSRodney W. Grimes break; 2530df8bae1dSRodney W. Grimes 2531a0292f23SGarrett Wollman case TCPS_SYN_SENT: 2532df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2533a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 2534a0292f23SGarrett Wollman break; 2535a0292f23SGarrett Wollman 2536df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 253757f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 2538df8bae1dSRodney W. Grimes break; 2539df8bae1dSRodney W. Grimes 2540df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 254157f60867SMark Johnston tcp_state_change(tp, TCPS_LAST_ACK); 2542df8bae1dSRodney W. Grimes break; 2543df8bae1dSRodney W. Grimes } 2544abc7d910SRobert Watson if (tp->t_state >= TCPS_FIN_WAIT_2) { 2545df8bae1dSRodney W. Grimes soisdisconnected(tp->t_inpcb->inp_socket); 2546abc7d910SRobert Watson /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 25477c72af87SMohan Srinivasan if (tp->t_state == TCPS_FIN_WAIT_2) { 25487c72af87SMohan Srinivasan int timeout; 25497c72af87SMohan Srinivasan 25507c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 25519077f387SGleb Smirnoff tcp_finwait2_timeout : TP_MAXIDLE(tp); 2552b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 2553b6239c4aSAndras Olah } 2554df8bae1dSRodney W. Grimes } 25557c72af87SMohan Srinivasan } 2556497057eeSRobert Watson 2557497057eeSRobert Watson #ifdef DDB 2558497057eeSRobert Watson static void 2559497057eeSRobert Watson db_print_indent(int indent) 2560497057eeSRobert Watson { 2561497057eeSRobert Watson int i; 2562497057eeSRobert Watson 2563497057eeSRobert Watson for (i = 0; i < indent; i++) 2564497057eeSRobert Watson db_printf(" "); 2565497057eeSRobert Watson } 2566497057eeSRobert Watson 2567497057eeSRobert Watson static void 2568497057eeSRobert Watson db_print_tstate(int t_state) 2569497057eeSRobert Watson { 2570497057eeSRobert Watson 2571497057eeSRobert Watson switch (t_state) { 2572497057eeSRobert Watson case TCPS_CLOSED: 2573497057eeSRobert Watson db_printf("TCPS_CLOSED"); 2574497057eeSRobert Watson return; 2575497057eeSRobert Watson 2576497057eeSRobert Watson case TCPS_LISTEN: 2577497057eeSRobert Watson db_printf("TCPS_LISTEN"); 2578497057eeSRobert Watson return; 2579497057eeSRobert Watson 2580497057eeSRobert Watson case TCPS_SYN_SENT: 2581497057eeSRobert Watson db_printf("TCPS_SYN_SENT"); 2582497057eeSRobert Watson return; 2583497057eeSRobert Watson 2584497057eeSRobert Watson case TCPS_SYN_RECEIVED: 2585497057eeSRobert Watson db_printf("TCPS_SYN_RECEIVED"); 2586497057eeSRobert Watson return; 2587497057eeSRobert Watson 2588497057eeSRobert Watson case TCPS_ESTABLISHED: 2589497057eeSRobert Watson db_printf("TCPS_ESTABLISHED"); 2590497057eeSRobert Watson return; 2591497057eeSRobert Watson 2592497057eeSRobert Watson case TCPS_CLOSE_WAIT: 2593497057eeSRobert Watson db_printf("TCPS_CLOSE_WAIT"); 2594497057eeSRobert Watson return; 2595497057eeSRobert Watson 2596497057eeSRobert Watson case TCPS_FIN_WAIT_1: 2597497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_1"); 2598497057eeSRobert Watson return; 2599497057eeSRobert Watson 2600497057eeSRobert Watson case TCPS_CLOSING: 2601497057eeSRobert Watson db_printf("TCPS_CLOSING"); 2602497057eeSRobert Watson return; 2603497057eeSRobert Watson 2604497057eeSRobert Watson case TCPS_LAST_ACK: 2605497057eeSRobert Watson db_printf("TCPS_LAST_ACK"); 2606497057eeSRobert Watson return; 2607497057eeSRobert Watson 2608497057eeSRobert Watson case TCPS_FIN_WAIT_2: 2609497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_2"); 2610497057eeSRobert Watson return; 2611497057eeSRobert Watson 2612497057eeSRobert Watson case TCPS_TIME_WAIT: 2613497057eeSRobert Watson db_printf("TCPS_TIME_WAIT"); 2614497057eeSRobert Watson return; 2615497057eeSRobert Watson 2616497057eeSRobert Watson default: 2617497057eeSRobert Watson db_printf("unknown"); 2618497057eeSRobert Watson return; 2619497057eeSRobert Watson } 2620497057eeSRobert Watson } 2621497057eeSRobert Watson 2622497057eeSRobert Watson static void 2623497057eeSRobert Watson db_print_tflags(u_int t_flags) 2624497057eeSRobert Watson { 2625497057eeSRobert Watson int comma; 2626497057eeSRobert Watson 2627497057eeSRobert Watson comma = 0; 2628497057eeSRobert Watson if (t_flags & TF_ACKNOW) { 2629497057eeSRobert Watson db_printf("%sTF_ACKNOW", comma ? ", " : ""); 2630497057eeSRobert Watson comma = 1; 2631497057eeSRobert Watson } 2632497057eeSRobert Watson if (t_flags & TF_DELACK) { 2633497057eeSRobert Watson db_printf("%sTF_DELACK", comma ? ", " : ""); 2634497057eeSRobert Watson comma = 1; 2635497057eeSRobert Watson } 2636497057eeSRobert Watson if (t_flags & TF_NODELAY) { 2637497057eeSRobert Watson db_printf("%sTF_NODELAY", comma ? ", " : ""); 2638497057eeSRobert Watson comma = 1; 2639497057eeSRobert Watson } 2640497057eeSRobert Watson if (t_flags & TF_NOOPT) { 2641497057eeSRobert Watson db_printf("%sTF_NOOPT", comma ? ", " : ""); 2642497057eeSRobert Watson comma = 1; 2643497057eeSRobert Watson } 2644497057eeSRobert Watson if (t_flags & TF_SENTFIN) { 2645497057eeSRobert Watson db_printf("%sTF_SENTFIN", comma ? ", " : ""); 2646497057eeSRobert Watson comma = 1; 2647497057eeSRobert Watson } 2648497057eeSRobert Watson if (t_flags & TF_REQ_SCALE) { 2649497057eeSRobert Watson db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 2650497057eeSRobert Watson comma = 1; 2651497057eeSRobert Watson } 2652497057eeSRobert Watson if (t_flags & TF_RCVD_SCALE) { 2653497057eeSRobert Watson db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 2654497057eeSRobert Watson comma = 1; 2655497057eeSRobert Watson } 2656497057eeSRobert Watson if (t_flags & TF_REQ_TSTMP) { 2657497057eeSRobert Watson db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 2658497057eeSRobert Watson comma = 1; 2659497057eeSRobert Watson } 2660497057eeSRobert Watson if (t_flags & TF_RCVD_TSTMP) { 2661497057eeSRobert Watson db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 2662497057eeSRobert Watson comma = 1; 2663497057eeSRobert Watson } 2664497057eeSRobert Watson if (t_flags & TF_SACK_PERMIT) { 2665497057eeSRobert Watson db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 2666497057eeSRobert Watson comma = 1; 2667497057eeSRobert Watson } 2668497057eeSRobert Watson if (t_flags & TF_NEEDSYN) { 2669497057eeSRobert Watson db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 2670497057eeSRobert Watson comma = 1; 2671497057eeSRobert Watson } 2672497057eeSRobert Watson if (t_flags & TF_NEEDFIN) { 2673497057eeSRobert Watson db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 2674497057eeSRobert Watson comma = 1; 2675497057eeSRobert Watson } 2676497057eeSRobert Watson if (t_flags & TF_NOPUSH) { 2677497057eeSRobert Watson db_printf("%sTF_NOPUSH", comma ? ", " : ""); 2678497057eeSRobert Watson comma = 1; 2679497057eeSRobert Watson } 2680497057eeSRobert Watson if (t_flags & TF_MORETOCOME) { 2681497057eeSRobert Watson db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 2682497057eeSRobert Watson comma = 1; 2683497057eeSRobert Watson } 2684497057eeSRobert Watson if (t_flags & TF_LQ_OVERFLOW) { 2685497057eeSRobert Watson db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : ""); 2686497057eeSRobert Watson comma = 1; 2687497057eeSRobert Watson } 2688497057eeSRobert Watson if (t_flags & TF_LASTIDLE) { 2689497057eeSRobert Watson db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 2690497057eeSRobert Watson comma = 1; 2691497057eeSRobert Watson } 2692497057eeSRobert Watson if (t_flags & TF_RXWIN0SENT) { 2693497057eeSRobert Watson db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 2694497057eeSRobert Watson comma = 1; 2695497057eeSRobert Watson } 2696497057eeSRobert Watson if (t_flags & TF_FASTRECOVERY) { 2697497057eeSRobert Watson db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 2698497057eeSRobert Watson comma = 1; 2699497057eeSRobert Watson } 2700dbc42409SLawrence Stewart if (t_flags & TF_CONGRECOVERY) { 2701dbc42409SLawrence Stewart db_printf("%sTF_CONGRECOVERY", comma ? ", " : ""); 2702dbc42409SLawrence Stewart comma = 1; 2703dbc42409SLawrence Stewart } 2704497057eeSRobert Watson if (t_flags & TF_WASFRECOVERY) { 2705497057eeSRobert Watson db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 2706497057eeSRobert Watson comma = 1; 2707497057eeSRobert Watson } 2708497057eeSRobert Watson if (t_flags & TF_SIGNATURE) { 2709497057eeSRobert Watson db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 2710497057eeSRobert Watson comma = 1; 2711497057eeSRobert Watson } 2712497057eeSRobert Watson if (t_flags & TF_FORCEDATA) { 2713497057eeSRobert Watson db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 2714497057eeSRobert Watson comma = 1; 2715497057eeSRobert Watson } 2716497057eeSRobert Watson if (t_flags & TF_TSO) { 2717497057eeSRobert Watson db_printf("%sTF_TSO", comma ? ", " : ""); 2718497057eeSRobert Watson comma = 1; 2719497057eeSRobert Watson } 2720281a0fd4SPatrick Kelsey if (t_flags & TF_FASTOPEN) { 2721281a0fd4SPatrick Kelsey db_printf("%sTF_FASTOPEN", comma ? ", " : ""); 2722281a0fd4SPatrick Kelsey comma = 1; 2723281a0fd4SPatrick Kelsey } 2724497057eeSRobert Watson } 2725497057eeSRobert Watson 2726497057eeSRobert Watson static void 27273cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2) 27283cf38784SMichael Tuexen { 27293cf38784SMichael Tuexen int comma; 27303cf38784SMichael Tuexen 27313cf38784SMichael Tuexen comma = 0; 27323cf38784SMichael Tuexen if (t_flags2 & TF2_ECN_PERMIT) { 27333cf38784SMichael Tuexen db_printf("%sTF2_ECN_PERMIT", comma ? ", " : ""); 27343cf38784SMichael Tuexen comma = 1; 27353cf38784SMichael Tuexen } 27363cf38784SMichael Tuexen } 27373cf38784SMichael Tuexen 27383cf38784SMichael Tuexen 27393cf38784SMichael Tuexen static void 2740497057eeSRobert Watson db_print_toobflags(char t_oobflags) 2741497057eeSRobert Watson { 2742497057eeSRobert Watson int comma; 2743497057eeSRobert Watson 2744497057eeSRobert Watson comma = 0; 2745497057eeSRobert Watson if (t_oobflags & TCPOOB_HAVEDATA) { 2746497057eeSRobert Watson db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 2747497057eeSRobert Watson comma = 1; 2748497057eeSRobert Watson } 2749497057eeSRobert Watson if (t_oobflags & TCPOOB_HADDATA) { 2750497057eeSRobert Watson db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 2751497057eeSRobert Watson comma = 1; 2752497057eeSRobert Watson } 2753497057eeSRobert Watson } 2754497057eeSRobert Watson 2755497057eeSRobert Watson static void 2756497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 2757497057eeSRobert Watson { 2758497057eeSRobert Watson 2759497057eeSRobert Watson db_print_indent(indent); 2760497057eeSRobert Watson db_printf("%s at %p\n", name, tp); 2761497057eeSRobert Watson 2762497057eeSRobert Watson indent += 2; 2763497057eeSRobert Watson 2764497057eeSRobert Watson db_print_indent(indent); 2765497057eeSRobert Watson db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 2766c28440dbSRandall Stewart TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 2767497057eeSRobert Watson 2768497057eeSRobert Watson db_print_indent(indent); 276985d94372SRobert Watson db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n", 2770e2f2059fSMike Silbersack &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep); 2771497057eeSRobert Watson 2772497057eeSRobert Watson db_print_indent(indent); 2773e2f2059fSMike Silbersack db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl, 2774e2f2059fSMike Silbersack &tp->t_timers->tt_delack, tp->t_inpcb); 2775497057eeSRobert Watson 2776497057eeSRobert Watson db_print_indent(indent); 2777497057eeSRobert Watson db_printf("t_state: %d (", tp->t_state); 2778497057eeSRobert Watson db_print_tstate(tp->t_state); 2779497057eeSRobert Watson db_printf(")\n"); 2780497057eeSRobert Watson 2781497057eeSRobert Watson db_print_indent(indent); 2782497057eeSRobert Watson db_printf("t_flags: 0x%x (", tp->t_flags); 2783497057eeSRobert Watson db_print_tflags(tp->t_flags); 2784497057eeSRobert Watson db_printf(")\n"); 2785497057eeSRobert Watson 2786497057eeSRobert Watson db_print_indent(indent); 27873cf38784SMichael Tuexen db_printf("t_flags2: 0x%x (", tp->t_flags2); 27883cf38784SMichael Tuexen db_print_tflags2(tp->t_flags2); 27893cf38784SMichael Tuexen db_printf(")\n"); 27903cf38784SMichael Tuexen 27913cf38784SMichael Tuexen db_print_indent(indent); 2792497057eeSRobert Watson db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n", 2793497057eeSRobert Watson tp->snd_una, tp->snd_max, tp->snd_nxt); 2794497057eeSRobert Watson 2795497057eeSRobert Watson db_print_indent(indent); 2796497057eeSRobert Watson db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 2797497057eeSRobert Watson tp->snd_up, tp->snd_wl1, tp->snd_wl2); 2798497057eeSRobert Watson 2799497057eeSRobert Watson db_print_indent(indent); 2800497057eeSRobert Watson db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 2801497057eeSRobert Watson tp->iss, tp->irs, tp->rcv_nxt); 2802497057eeSRobert Watson 2803497057eeSRobert Watson db_print_indent(indent); 28043ac12506SJonathan T. Looney db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n", 2805497057eeSRobert Watson tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 2806497057eeSRobert Watson 2807497057eeSRobert Watson db_print_indent(indent); 28083ac12506SJonathan T. Looney db_printf("snd_wnd: %u snd_cwnd: %u\n", 28091c18314dSAndre Oppermann tp->snd_wnd, tp->snd_cwnd); 2810497057eeSRobert Watson 2811497057eeSRobert Watson db_print_indent(indent); 28123ac12506SJonathan T. Looney db_printf("snd_ssthresh: %u snd_recover: " 28131c18314dSAndre Oppermann "0x%08x\n", tp->snd_ssthresh, tp->snd_recover); 2814497057eeSRobert Watson 2815497057eeSRobert Watson db_print_indent(indent); 28160c39d38dSGleb Smirnoff db_printf("t_rcvtime: %u t_startime: %u\n", 28170c39d38dSGleb Smirnoff tp->t_rcvtime, tp->t_starttime); 2818497057eeSRobert Watson 2819497057eeSRobert Watson db_print_indent(indent); 28201c18314dSAndre Oppermann db_printf("t_rttime: %u t_rtsq: 0x%08x\n", 28211c18314dSAndre Oppermann tp->t_rtttime, tp->t_rtseq); 2822497057eeSRobert Watson 2823497057eeSRobert Watson db_print_indent(indent); 28241c18314dSAndre Oppermann db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n", 28251c18314dSAndre Oppermann tp->t_rxtcur, tp->t_maxseg, tp->t_srtt); 2826497057eeSRobert Watson 2827497057eeSRobert Watson db_print_indent(indent); 2828497057eeSRobert Watson db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u " 2829497057eeSRobert Watson "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin, 2830497057eeSRobert Watson tp->t_rttbest); 2831497057eeSRobert Watson 2832497057eeSRobert Watson db_print_indent(indent); 28333ac12506SJonathan T. Looney db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n", 2834497057eeSRobert Watson tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 2835497057eeSRobert Watson 2836497057eeSRobert Watson db_print_indent(indent); 2837497057eeSRobert Watson db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 2838497057eeSRobert Watson db_print_toobflags(tp->t_oobflags); 2839497057eeSRobert Watson db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 2840497057eeSRobert Watson 2841497057eeSRobert Watson db_print_indent(indent); 2842497057eeSRobert Watson db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 2843497057eeSRobert Watson tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 2844497057eeSRobert Watson 2845497057eeSRobert Watson db_print_indent(indent); 28469f78a87aSJohn Baldwin db_printf("ts_recent: %u ts_recent_age: %u\n", 28471a553740SAndre Oppermann tp->ts_recent, tp->ts_recent_age); 2848497057eeSRobert Watson 2849497057eeSRobert Watson db_print_indent(indent); 2850497057eeSRobert Watson db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 28513ac12506SJonathan T. Looney "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 2852497057eeSRobert Watson 2853497057eeSRobert Watson db_print_indent(indent); 28543ac12506SJonathan T. Looney db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x " 28559f78a87aSJohn Baldwin "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 2856497057eeSRobert Watson tp->snd_recover_prev, tp->t_badrxtwin); 2857497057eeSRobert Watson 2858497057eeSRobert Watson db_print_indent(indent); 28593529149eSAndre Oppermann db_printf("snd_numholes: %d snd_holes first: %p\n", 28603529149eSAndre Oppermann tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 2861497057eeSRobert Watson 2862497057eeSRobert Watson db_print_indent(indent); 2863497057eeSRobert Watson db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: " 2864497057eeSRobert Watson "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata); 2865497057eeSRobert Watson 2866497057eeSRobert Watson /* Skip sackblks, sackhint. */ 2867497057eeSRobert Watson 2868497057eeSRobert Watson db_print_indent(indent); 2869497057eeSRobert Watson db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 2870497057eeSRobert Watson tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 2871497057eeSRobert Watson } 2872497057eeSRobert Watson 2873497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 2874497057eeSRobert Watson { 2875497057eeSRobert Watson struct tcpcb *tp; 2876497057eeSRobert Watson 2877497057eeSRobert Watson if (!have_addr) { 2878497057eeSRobert Watson db_printf("usage: show tcpcb <addr>\n"); 2879497057eeSRobert Watson return; 2880497057eeSRobert Watson } 2881497057eeSRobert Watson tp = (struct tcpcb *)addr; 2882497057eeSRobert Watson 2883497057eeSRobert Watson db_print_tcpcb(tp, "tcpcb", 0); 2884497057eeSRobert Watson } 2885497057eeSRobert Watson #endif 2886