1c398230bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1993 3623dce13SRobert Watson * The Regents of the University of California. 4497057eeSRobert Watson * Copyright (c) 2006-2007 Robert N. M. Watson 5fa046d87SRobert Watson * Copyright (c) 2010-2011 Juniper Networks, Inc. 6623dce13SRobert Watson * All rights reserved. 7df8bae1dSRodney W. Grimes * 8fa046d87SRobert Watson * Portions of this software were developed by Robert N. M. Watson under 9fa046d87SRobert Watson * contract to Juniper Networks, Inc. 10fa046d87SRobert Watson * 11df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 12df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 13df8bae1dSRodney W. Grimes * are met: 14df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 15df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 16df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 17df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 18df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 19df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 20df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 21df8bae1dSRodney W. Grimes * without specific prior written permission. 22df8bae1dSRodney W. Grimes * 23df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33df8bae1dSRodney W. Grimes * SUCH DAMAGE. 34df8bae1dSRodney W. Grimes * 351fdbc7aeSGarrett Wollman * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94 36df8bae1dSRodney W. Grimes */ 37df8bae1dSRodney W. Grimes 384b421e2dSMike Silbersack #include <sys/cdefs.h> 394b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 404b421e2dSMike Silbersack 41497057eeSRobert Watson #include "opt_ddb.h" 421cfd4b53SBruce M Simpson #include "opt_inet.h" 43fb59c426SYoshinobu Inoue #include "opt_inet6.h" 440cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 450cc12cc5SJoerg Wunsch 46df8bae1dSRodney W. Grimes #include <sys/param.h> 47df8bae1dSRodney W. Grimes #include <sys/systm.h> 489077f387SGleb Smirnoff #include <sys/limits.h> 49f76fcf6dSJeffrey Hsu #include <sys/malloc.h> 5055bceb1eSRandall Stewart #include <sys/refcount.h> 51c7a82f90SGarrett Wollman #include <sys/kernel.h> 5298163b98SPoul-Henning Kamp #include <sys/sysctl.h> 53df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 54fb59c426SYoshinobu Inoue #ifdef INET6 55fb59c426SYoshinobu Inoue #include <sys/domain.h> 56fb59c426SYoshinobu Inoue #endif /* INET6 */ 57df8bae1dSRodney W. Grimes #include <sys/socket.h> 58df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 59df8bae1dSRodney W. Grimes #include <sys/protosw.h> 6091421ba2SRobert Watson #include <sys/proc.h> 6191421ba2SRobert Watson #include <sys/jail.h> 62df8bae1dSRodney W. Grimes 63497057eeSRobert Watson #ifdef DDB 64497057eeSRobert Watson #include <ddb/ddb.h> 65497057eeSRobert Watson #endif 66497057eeSRobert Watson 67df8bae1dSRodney W. Grimes #include <net/if.h> 6876039bc8SGleb Smirnoff #include <net/if_var.h> 69df8bae1dSRodney W. Grimes #include <net/route.h> 70530c0060SRobert Watson #include <net/vnet.h> 71df8bae1dSRodney W. Grimes 72df8bae1dSRodney W. Grimes #include <netinet/in.h> 735d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h> 74df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 75b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h> 76b5e8ce9fSBruce Evans #include <netinet/in_var.h> 77df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 78fb59c426SYoshinobu Inoue #ifdef INET6 79b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h> 80b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h> 81fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 82a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h> 83fb59c426SYoshinobu Inoue #endif 84281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 85281a0fd4SPatrick Kelsey #include <netinet/tcp_fastopen.h> 86281a0fd4SPatrick Kelsey #endif 872de3e790SGleb Smirnoff #include <netinet/tcp.h> 88df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 89df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 90df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 91df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 92df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 934644fda3SGleb Smirnoff #include <netinet/cc/cc.h> 9486a996e6SHiren Panchasara #ifdef TCPPCAP 9586a996e6SHiren Panchasara #include <netinet/tcp_pcap.h> 9686a996e6SHiren Panchasara #endif 97610ee2f9SDavid Greenman #ifdef TCPDEBUG 98df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 99610ee2f9SDavid Greenman #endif 10009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 101bc65987aSKip Macy #include <netinet/tcp_offload.h> 10209fe6320SNavdeep Parhar #endif 103df8bae1dSRodney W. Grimes 104df8bae1dSRodney W. Grimes /* 105df8bae1dSRodney W. Grimes * TCP protocol interface to socket abstraction. 106df8bae1dSRodney W. Grimes */ 10756dc72c3SPawel Jakub Dawidek static int tcp_attach(struct socket *); 108b287c6c7SBjoern A. Zeeb #ifdef INET 1094d77a549SAlfred Perlstein static int tcp_connect(struct tcpcb *, struct sockaddr *, 1104d77a549SAlfred Perlstein struct thread *td); 111b287c6c7SBjoern A. Zeeb #endif /* INET */ 112fb59c426SYoshinobu Inoue #ifdef INET6 1134d77a549SAlfred Perlstein static int tcp6_connect(struct tcpcb *, struct sockaddr *, 1144d77a549SAlfred Perlstein struct thread *td); 115fb59c426SYoshinobu Inoue #endif /* INET6 */ 116623dce13SRobert Watson static void tcp_disconnect(struct tcpcb *); 117623dce13SRobert Watson static void tcp_usrclosed(struct tcpcb *); 118b8af5dfaSRobert Watson static void tcp_fill_info(struct tcpcb *, struct tcp_info *); 1192c37256eSGarrett Wollman 1202c37256eSGarrett Wollman #ifdef TCPDEBUG 1211db24ffbSJonathan Lemon #define TCPDEBUG0 int ostate = 0 1222c37256eSGarrett Wollman #define TCPDEBUG1() ostate = tp ? tp->t_state : 0 1234cc20ab1SSeigo Tanimura #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 1244cc20ab1SSeigo Tanimura tcp_trace(TA_USER, ostate, tp, 0, 0, req) 1252c37256eSGarrett Wollman #else 1262c37256eSGarrett Wollman #define TCPDEBUG0 1272c37256eSGarrett Wollman #define TCPDEBUG1() 1282c37256eSGarrett Wollman #define TCPDEBUG2(req) 1292c37256eSGarrett Wollman #endif 1302c37256eSGarrett Wollman 1312c37256eSGarrett Wollman /* 1322c37256eSGarrett Wollman * TCP attaches to socket via pru_attach(), reserving space, 1332c37256eSGarrett Wollman * and an internet control block. 1342c37256eSGarrett Wollman */ 1352c37256eSGarrett Wollman static int 136b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td) 1372c37256eSGarrett Wollman { 138f76fcf6dSJeffrey Hsu struct inpcb *inp; 139623dce13SRobert Watson struct tcpcb *tp = NULL; 140623dce13SRobert Watson int error; 1412c37256eSGarrett Wollman TCPDEBUG0; 1422c37256eSGarrett Wollman 143623dce13SRobert Watson inp = sotoinpcb(so); 144623dce13SRobert Watson KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL")); 1452c37256eSGarrett Wollman TCPDEBUG1(); 1462c37256eSGarrett Wollman 14756dc72c3SPawel Jakub Dawidek error = tcp_attach(so); 1482c37256eSGarrett Wollman if (error) 1492c37256eSGarrett Wollman goto out; 1502c37256eSGarrett Wollman 1512c37256eSGarrett Wollman if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1523879597fSAndrey A. Chernov so->so_linger = TCP_LINGERTIME; 153f76fcf6dSJeffrey Hsu 154f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 155f76fcf6dSJeffrey Hsu tp = intotcpcb(inp); 1562c37256eSGarrett Wollman out: 1572c37256eSGarrett Wollman TCPDEBUG2(PRU_ATTACH); 1585d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ATTACH); 1592c37256eSGarrett Wollman return error; 1602c37256eSGarrett Wollman } 1612c37256eSGarrett Wollman 1622c37256eSGarrett Wollman /* 163a152f8a3SRobert Watson * tcp_detach is called when the socket layer loses its final reference 164a152f8a3SRobert Watson * to the socket, be it a file descriptor reference, a reference from TCP, 165a152f8a3SRobert Watson * etc. At this point, there is only one case in which we will keep around 166a152f8a3SRobert Watson * inpcb state: time wait. 167c78cbc7bSRobert Watson * 168a152f8a3SRobert Watson * This function can probably be re-absorbed back into tcp_usr_detach() now 169a152f8a3SRobert Watson * that there is a single detach path. 1702c37256eSGarrett Wollman */ 171bc725eafSRobert Watson static void 172c78cbc7bSRobert Watson tcp_detach(struct socket *so, struct inpcb *inp) 1732c37256eSGarrett Wollman { 1742c37256eSGarrett Wollman struct tcpcb *tp; 1752c37256eSGarrett Wollman 176079672cbSJulien Charbon INP_INFO_LOCK_ASSERT(&V_tcbinfo); 1778501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 178623dce13SRobert Watson 179c78cbc7bSRobert Watson KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp")); 180c78cbc7bSRobert Watson KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so")); 181953b5606SRobert Watson 182a152f8a3SRobert Watson tp = intotcpcb(inp); 183a152f8a3SRobert Watson 184ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 185623dce13SRobert Watson /* 186a152f8a3SRobert Watson * There are two cases to handle: one in which the time wait 187a152f8a3SRobert Watson * state is being discarded (INP_DROPPED), and one in which 188a152f8a3SRobert Watson * this connection will remain in timewait. In the former, 189a152f8a3SRobert Watson * it is time to discard all state (except tcptw, which has 190a152f8a3SRobert Watson * already been discarded by the timewait close code, which 191a152f8a3SRobert Watson * should be further up the call stack somewhere). In the 192a152f8a3SRobert Watson * latter case, we detach from the socket, but leave the pcb 193a152f8a3SRobert Watson * present until timewait ends. 194623dce13SRobert Watson * 195a152f8a3SRobert Watson * XXXRW: Would it be cleaner to free the tcptw here? 196cea40c48SJulien Charbon * 197cea40c48SJulien Charbon * Astute question indeed, from twtcp perspective there are 198cea40c48SJulien Charbon * three cases to consider: 199cea40c48SJulien Charbon * 200cea40c48SJulien Charbon * #1 tcp_detach is called at tcptw creation time by 201cea40c48SJulien Charbon * tcp_twstart, then do not discard the newly created tcptw 202cea40c48SJulien Charbon * and leave inpcb present until timewait ends 203cea40c48SJulien Charbon * #2 tcp_detach is called at timewait end (or reuse) by 204cea40c48SJulien Charbon * tcp_twclose, then the tcptw has already been discarded 205ff9b006dSJulien Charbon * (or reused) and inpcb is freed here 206cea40c48SJulien Charbon * #3 tcp_detach is called() after timewait ends (or reuse) 207cea40c48SJulien Charbon * (e.g. by soclose), then tcptw has already been discarded 208ff9b006dSJulien Charbon * (or reused) and inpcb is freed here 209cea40c48SJulien Charbon * 210cea40c48SJulien Charbon * In all three cases the tcptw should not be freed here. 211623dce13SRobert Watson */ 212ad71fe3cSRobert Watson if (inp->inp_flags & INP_DROPPED) { 213a152f8a3SRobert Watson KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && " 214a152f8a3SRobert Watson "INP_DROPPED && tp != NULL")); 215623dce13SRobert Watson in_pcbdetach(inp); 2160206cdb8SBjoern A. Zeeb in_pcbfree(inp); 2170206cdb8SBjoern A. Zeeb } else { 218623dce13SRobert Watson in_pcbdetach(inp); 2198501a69cSRobert Watson INP_WUNLOCK(inp); 220623dce13SRobert Watson } 221623dce13SRobert Watson } else { 222e6e65783SRobert Watson /* 223a152f8a3SRobert Watson * If the connection is not in timewait, we consider two 224a152f8a3SRobert Watson * two conditions: one in which no further processing is 225a152f8a3SRobert Watson * necessary (dropped || embryonic), and one in which TCP is 226a152f8a3SRobert Watson * not yet done, but no longer requires the socket, so the 227a152f8a3SRobert Watson * pcb will persist for the time being. 228a152f8a3SRobert Watson * 229a152f8a3SRobert Watson * XXXRW: Does the second case still occur? 230e6e65783SRobert Watson */ 231ad71fe3cSRobert Watson if (inp->inp_flags & INP_DROPPED || 232623dce13SRobert Watson tp->t_state < TCPS_SYN_SENT) { 233623dce13SRobert Watson tcp_discardcb(tp); 234623dce13SRobert Watson in_pcbdetach(inp); 2350206cdb8SBjoern A. Zeeb in_pcbfree(inp); 236db3cee51SNavdeep Parhar } else { 237a152f8a3SRobert Watson in_pcbdetach(inp); 238db3cee51SNavdeep Parhar INP_WUNLOCK(inp); 239db3cee51SNavdeep Parhar } 240623dce13SRobert Watson } 241623dce13SRobert Watson } 242c78cbc7bSRobert Watson 243c78cbc7bSRobert Watson /* 244c78cbc7bSRobert Watson * pru_detach() detaches the TCP protocol from the socket. 245c78cbc7bSRobert Watson * If the protocol state is non-embryonic, then can't 246c78cbc7bSRobert Watson * do this directly: have to initiate a pru_disconnect(), 247c78cbc7bSRobert Watson * which may finish later; embryonic TCB's can just 248c78cbc7bSRobert Watson * be discarded here. 249c78cbc7bSRobert Watson */ 250c78cbc7bSRobert Watson static void 251c78cbc7bSRobert Watson tcp_usr_detach(struct socket *so) 252c78cbc7bSRobert Watson { 253c78cbc7bSRobert Watson struct inpcb *inp; 254079672cbSJulien Charbon int rlock = 0; 255c78cbc7bSRobert Watson 256c78cbc7bSRobert Watson inp = sotoinpcb(so); 257c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL")); 258079672cbSJulien Charbon if (!INP_INFO_WLOCKED(&V_tcbinfo)) { 259ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 260079672cbSJulien Charbon rlock = 1; 261079672cbSJulien Charbon } 2628501a69cSRobert Watson INP_WLOCK(inp); 263c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 264c78cbc7bSRobert Watson ("tcp_usr_detach: inp_socket == NULL")); 265c78cbc7bSRobert Watson tcp_detach(so, inp); 266079672cbSJulien Charbon if (rlock) 267ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 2682c37256eSGarrett Wollman } 2692c37256eSGarrett Wollman 270b287c6c7SBjoern A. Zeeb #ifdef INET 2712c37256eSGarrett Wollman /* 2722c37256eSGarrett Wollman * Give the socket an address. 2732c37256eSGarrett Wollman */ 2742c37256eSGarrett Wollman static int 275b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 2762c37256eSGarrett Wollman { 2772c37256eSGarrett Wollman int error = 0; 278f76fcf6dSJeffrey Hsu struct inpcb *inp; 279623dce13SRobert Watson struct tcpcb *tp = NULL; 2802c37256eSGarrett Wollman struct sockaddr_in *sinp; 2812c37256eSGarrett Wollman 28252710de1SPawel Jakub Dawidek sinp = (struct sockaddr_in *)nam; 28352710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof (*sinp)) 28452710de1SPawel Jakub Dawidek return (EINVAL); 2852c37256eSGarrett Wollman /* 2862c37256eSGarrett Wollman * Must check for multicast addresses and disallow binding 2872c37256eSGarrett Wollman * to them. 2882c37256eSGarrett Wollman */ 2892c37256eSGarrett Wollman if (sinp->sin_family == AF_INET && 29052710de1SPawel Jakub Dawidek IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 29152710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 29252710de1SPawel Jakub Dawidek 293623dce13SRobert Watson TCPDEBUG0; 294623dce13SRobert Watson inp = sotoinpcb(so); 295623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 2968501a69cSRobert Watson INP_WLOCK(inp); 297ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 298623dce13SRobert Watson error = EINVAL; 2992c37256eSGarrett Wollman goto out; 300623dce13SRobert Watson } 301623dce13SRobert Watson tp = intotcpcb(inp); 302623dce13SRobert Watson TCPDEBUG1(); 303fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 304623dce13SRobert Watson error = in_pcbbind(inp, nam, td->td_ucred); 305fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 306623dce13SRobert Watson out: 307623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 3085d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 3098501a69cSRobert Watson INP_WUNLOCK(inp); 310623dce13SRobert Watson 311623dce13SRobert Watson return (error); 3122c37256eSGarrett Wollman } 313b287c6c7SBjoern A. Zeeb #endif /* INET */ 3142c37256eSGarrett Wollman 315fb59c426SYoshinobu Inoue #ifdef INET6 316fb59c426SYoshinobu Inoue static int 317b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 318fb59c426SYoshinobu Inoue { 319fb59c426SYoshinobu Inoue int error = 0; 320f76fcf6dSJeffrey Hsu struct inpcb *inp; 321623dce13SRobert Watson struct tcpcb *tp = NULL; 322fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 323fb59c426SYoshinobu Inoue 32452710de1SPawel Jakub Dawidek sin6p = (struct sockaddr_in6 *)nam; 32552710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof (*sin6p)) 32652710de1SPawel Jakub Dawidek return (EINVAL); 327fb59c426SYoshinobu Inoue /* 328fb59c426SYoshinobu Inoue * Must check for multicast addresses and disallow binding 329fb59c426SYoshinobu Inoue * to them. 330fb59c426SYoshinobu Inoue */ 331fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 && 33252710de1SPawel Jakub Dawidek IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 33352710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 33452710de1SPawel Jakub Dawidek 335623dce13SRobert Watson TCPDEBUG0; 336623dce13SRobert Watson inp = sotoinpcb(so); 337623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 3388501a69cSRobert Watson INP_WLOCK(inp); 339ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 340623dce13SRobert Watson error = EINVAL; 341623dce13SRobert Watson goto out; 342623dce13SRobert Watson } 343623dce13SRobert Watson tp = intotcpcb(inp); 344623dce13SRobert Watson TCPDEBUG1(); 345fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 346fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 347fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 348b287c6c7SBjoern A. Zeeb #ifdef INET 34966ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 350fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) 351fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 352fb59c426SYoshinobu Inoue else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 353fb59c426SYoshinobu Inoue struct sockaddr_in sin; 354fb59c426SYoshinobu Inoue 355fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 356fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 357fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 358b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)&sin, 359b0330ed9SPawel Jakub Dawidek td->td_ucred); 360fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 361fb59c426SYoshinobu Inoue goto out; 362fb59c426SYoshinobu Inoue } 363fb59c426SYoshinobu Inoue } 364b287c6c7SBjoern A. Zeeb #endif 365b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, nam, td->td_ucred); 366fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 367623dce13SRobert Watson out: 368623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 3695d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 3708501a69cSRobert Watson INP_WUNLOCK(inp); 371623dce13SRobert Watson return (error); 372fb59c426SYoshinobu Inoue } 373fb59c426SYoshinobu Inoue #endif /* INET6 */ 374fb59c426SYoshinobu Inoue 375b287c6c7SBjoern A. Zeeb #ifdef INET 3762c37256eSGarrett Wollman /* 3772c37256eSGarrett Wollman * Prepare to accept connections. 3782c37256eSGarrett Wollman */ 3792c37256eSGarrett Wollman static int 380d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td) 3812c37256eSGarrett Wollman { 3822c37256eSGarrett Wollman int error = 0; 383f76fcf6dSJeffrey Hsu struct inpcb *inp; 384623dce13SRobert Watson struct tcpcb *tp = NULL; 3852c37256eSGarrett Wollman 386623dce13SRobert Watson TCPDEBUG0; 387623dce13SRobert Watson inp = sotoinpcb(so); 388623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 3898501a69cSRobert Watson INP_WLOCK(inp); 390ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 391623dce13SRobert Watson error = EINVAL; 392623dce13SRobert Watson goto out; 393623dce13SRobert Watson } 394623dce13SRobert Watson tp = intotcpcb(inp); 395623dce13SRobert Watson TCPDEBUG1(); 3960daccb9cSRobert Watson SOCK_LOCK(so); 3970daccb9cSRobert Watson error = solisten_proto_check(so); 398fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 3990daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) 400b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 401fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 4020daccb9cSRobert Watson if (error == 0) { 40357f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 404d374e81eSRobert Watson solisten_proto(so, backlog); 40509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 40637cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 40709fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 40809fe6320SNavdeep Parhar #endif 4090daccb9cSRobert Watson } 4100daccb9cSRobert Watson SOCK_UNLOCK(so); 411623dce13SRobert Watson 412281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 413281a0fd4SPatrick Kelsey if (tp->t_flags & TF_FASTOPEN) 414281a0fd4SPatrick Kelsey tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 415281a0fd4SPatrick Kelsey #endif 416623dce13SRobert Watson out: 417623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 4185d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 4198501a69cSRobert Watson INP_WUNLOCK(inp); 420623dce13SRobert Watson return (error); 4212c37256eSGarrett Wollman } 422b287c6c7SBjoern A. Zeeb #endif /* INET */ 4232c37256eSGarrett Wollman 424fb59c426SYoshinobu Inoue #ifdef INET6 425fb59c426SYoshinobu Inoue static int 426d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 427fb59c426SYoshinobu Inoue { 428fb59c426SYoshinobu Inoue int error = 0; 429f76fcf6dSJeffrey Hsu struct inpcb *inp; 430623dce13SRobert Watson struct tcpcb *tp = NULL; 431fb59c426SYoshinobu Inoue 432623dce13SRobert Watson TCPDEBUG0; 433623dce13SRobert Watson inp = sotoinpcb(so); 434623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_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) { 446fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 44766ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 448fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 449b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 450fb59c426SYoshinobu Inoue } 451fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 4520daccb9cSRobert Watson if (error == 0) { 45357f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 454d374e81eSRobert Watson solisten_proto(so, backlog); 45509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 45637cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 45709fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 45809fe6320SNavdeep Parhar #endif 4590daccb9cSRobert Watson } 4600daccb9cSRobert Watson SOCK_UNLOCK(so); 461623dce13SRobert Watson 462281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 463281a0fd4SPatrick Kelsey if (tp->t_flags & TF_FASTOPEN) 464281a0fd4SPatrick Kelsey tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 465281a0fd4SPatrick Kelsey #endif 466623dce13SRobert Watson out: 467623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 4685d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 4698501a69cSRobert Watson INP_WUNLOCK(inp); 470623dce13SRobert Watson return (error); 471fb59c426SYoshinobu Inoue } 472fb59c426SYoshinobu Inoue #endif /* INET6 */ 473fb59c426SYoshinobu Inoue 474b287c6c7SBjoern A. Zeeb #ifdef INET 4752c37256eSGarrett Wollman /* 4762c37256eSGarrett Wollman * Initiate connection to peer. 4772c37256eSGarrett Wollman * Create a template for use in transmissions on this connection. 4782c37256eSGarrett Wollman * Enter SYN_SENT state, and mark socket as connecting. 4792c37256eSGarrett Wollman * Start keep-alive timer, and seed output sequence space. 4802c37256eSGarrett Wollman * Send initial segment on connection. 4812c37256eSGarrett Wollman */ 4822c37256eSGarrett Wollman static int 483b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 4842c37256eSGarrett Wollman { 4852c37256eSGarrett Wollman int error = 0; 486f76fcf6dSJeffrey Hsu struct inpcb *inp; 487623dce13SRobert Watson struct tcpcb *tp = NULL; 4882c37256eSGarrett Wollman struct sockaddr_in *sinp; 4892c37256eSGarrett Wollman 49057bf258eSGarrett Wollman sinp = (struct sockaddr_in *)nam; 491e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sinp)) 492e29ef13fSDon Lewis return (EINVAL); 49352710de1SPawel Jakub Dawidek /* 49452710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 49552710de1SPawel Jakub Dawidek */ 4962c37256eSGarrett Wollman if (sinp->sin_family == AF_INET 49752710de1SPawel Jakub Dawidek && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 49852710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 499b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0) 500b89e82ddSJamie Gritton return (error); 50175c13541SPoul-Henning Kamp 502623dce13SRobert Watson TCPDEBUG0; 503623dce13SRobert Watson inp = sotoinpcb(so); 504623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 5058501a69cSRobert Watson INP_WLOCK(inp); 506eb96dc33SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) { 507eb96dc33SJulien Charbon error = EADDRINUSE; 508eb96dc33SJulien Charbon goto out; 509eb96dc33SJulien Charbon } 510eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 511eb96dc33SJulien Charbon error = ECONNREFUSED; 512623dce13SRobert Watson goto out; 513623dce13SRobert Watson } 514623dce13SRobert Watson tp = intotcpcb(inp); 515623dce13SRobert Watson TCPDEBUG1(); 516b40ce416SJulian Elischer if ((error = tcp_connect(tp, nam, td)) != 0) 5172c37256eSGarrett Wollman goto out; 51809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 51909fe6320SNavdeep Parhar if (registered_toedevs > 0 && 52037cc0ecbSNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 52109fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 52209fe6320SNavdeep Parhar goto out; 52309fe6320SNavdeep Parhar #endif 52409fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 52555bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 526623dce13SRobert Watson out: 527623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 528e79cb051SGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CONNECT); 5298501a69cSRobert Watson INP_WUNLOCK(inp); 530623dce13SRobert Watson return (error); 5312c37256eSGarrett Wollman } 532b287c6c7SBjoern A. Zeeb #endif /* INET */ 5332c37256eSGarrett Wollman 534fb59c426SYoshinobu Inoue #ifdef INET6 535fb59c426SYoshinobu Inoue static int 536b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 537fb59c426SYoshinobu Inoue { 538fb59c426SYoshinobu Inoue int error = 0; 539f76fcf6dSJeffrey Hsu struct inpcb *inp; 540623dce13SRobert Watson struct tcpcb *tp = NULL; 541fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 542623dce13SRobert Watson 543623dce13SRobert Watson TCPDEBUG0; 544fb59c426SYoshinobu Inoue 545fb59c426SYoshinobu Inoue sin6p = (struct sockaddr_in6 *)nam; 546e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sin6p)) 547e29ef13fSDon Lewis return (EINVAL); 54852710de1SPawel Jakub Dawidek /* 54952710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 55052710de1SPawel Jakub Dawidek */ 551fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 55252710de1SPawel Jakub Dawidek && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 55352710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 554fb59c426SYoshinobu Inoue 555623dce13SRobert Watson inp = sotoinpcb(so); 556623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 5578501a69cSRobert Watson INP_WLOCK(inp); 558eb96dc33SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) { 559eb96dc33SJulien Charbon error = EADDRINUSE; 560eb96dc33SJulien Charbon goto out; 561eb96dc33SJulien Charbon } 562eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 563eb96dc33SJulien Charbon error = ECONNREFUSED; 564623dce13SRobert Watson goto out; 565623dce13SRobert Watson } 566623dce13SRobert Watson tp = intotcpcb(inp); 567623dce13SRobert Watson TCPDEBUG1(); 568b287c6c7SBjoern A. Zeeb #ifdef INET 569fa046d87SRobert Watson /* 570fa046d87SRobert Watson * XXXRW: Some confusion: V4/V6 flags relate to binding, and 571fa046d87SRobert Watson * therefore probably require the hash lock, which isn't held here. 572fa046d87SRobert Watson * Is this a significant problem? 573fa046d87SRobert Watson */ 57433841545SHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 575fb59c426SYoshinobu Inoue struct sockaddr_in sin; 576fb59c426SYoshinobu Inoue 577d46a5312SMaxim Konovalov if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 578d46a5312SMaxim Konovalov error = EINVAL; 579d46a5312SMaxim Konovalov goto out; 580d46a5312SMaxim Konovalov } 58133841545SHajimu UMEMOTO 582fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 583fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 584fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 585b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, 586b89e82ddSJamie Gritton &sin.sin_addr)) != 0) 587413628a7SBjoern A. Zeeb goto out; 588b40ce416SJulian Elischer if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 589fb59c426SYoshinobu Inoue goto out; 59009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 59109fe6320SNavdeep Parhar if (registered_toedevs > 0 && 592adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 59309fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 59409fe6320SNavdeep Parhar goto out; 59509fe6320SNavdeep Parhar #endif 59655bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 597fb59c426SYoshinobu Inoue goto out; 598fb59c426SYoshinobu Inoue } 599b287c6c7SBjoern A. Zeeb #endif 600fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 601fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 602dcdb4371SBjoern A. Zeeb inp->inp_inc.inc_flags |= INC_ISIPV6; 603b89e82ddSJamie Gritton if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0) 604413628a7SBjoern A. Zeeb goto out; 605b40ce416SJulian Elischer if ((error = tcp6_connect(tp, nam, td)) != 0) 606fb59c426SYoshinobu Inoue goto out; 60709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 60809fe6320SNavdeep Parhar if (registered_toedevs > 0 && 609adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 61009fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 61109fe6320SNavdeep Parhar goto out; 61209fe6320SNavdeep Parhar #endif 61309fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 61455bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 615623dce13SRobert Watson 616623dce13SRobert Watson out: 617623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 6185d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CONNECT); 6198501a69cSRobert Watson INP_WUNLOCK(inp); 620623dce13SRobert Watson return (error); 621fb59c426SYoshinobu Inoue } 622fb59c426SYoshinobu Inoue #endif /* INET6 */ 623fb59c426SYoshinobu Inoue 6242c37256eSGarrett Wollman /* 6252c37256eSGarrett Wollman * Initiate disconnect from peer. 6262c37256eSGarrett Wollman * If connection never passed embryonic stage, just drop; 6272c37256eSGarrett Wollman * else if don't need to let data drain, then can just drop anyways, 6282c37256eSGarrett Wollman * else have to begin TCP shutdown process: mark socket disconnecting, 6292c37256eSGarrett Wollman * drain unread data, state switch to reflect user close, and 6302c37256eSGarrett Wollman * send segment (e.g. FIN) to peer. Socket will be really disconnected 6312c37256eSGarrett Wollman * when peer sends FIN and acks ours. 6322c37256eSGarrett Wollman * 6332c37256eSGarrett Wollman * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 6342c37256eSGarrett Wollman */ 6352c37256eSGarrett Wollman static int 6362c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so) 6372c37256eSGarrett Wollman { 638f76fcf6dSJeffrey Hsu struct inpcb *inp; 639623dce13SRobert Watson struct tcpcb *tp = NULL; 640623dce13SRobert Watson int error = 0; 6412c37256eSGarrett Wollman 642623dce13SRobert Watson TCPDEBUG0; 643ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 644623dce13SRobert Watson inp = sotoinpcb(so); 645623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 6468501a69cSRobert Watson INP_WLOCK(inp); 647489dcc92SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) 648489dcc92SJulien Charbon goto out; 649489dcc92SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 65021367f63SSam Leffler error = ECONNRESET; 651623dce13SRobert Watson goto out; 652623dce13SRobert Watson } 653623dce13SRobert Watson tp = intotcpcb(inp); 654623dce13SRobert Watson TCPDEBUG1(); 655623dce13SRobert Watson tcp_disconnect(tp); 656623dce13SRobert Watson out: 657623dce13SRobert Watson TCPDEBUG2(PRU_DISCONNECT); 6585d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_DISCONNECT); 6598501a69cSRobert Watson INP_WUNLOCK(inp); 660ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 661623dce13SRobert Watson return (error); 6622c37256eSGarrett Wollman } 6632c37256eSGarrett Wollman 664b287c6c7SBjoern A. Zeeb #ifdef INET 6652c37256eSGarrett Wollman /* 6668296cddfSRobert Watson * Accept a connection. Essentially all the work is done at higher levels; 6678296cddfSRobert Watson * just return the address of the peer, storing through addr. 6682c37256eSGarrett Wollman */ 6692c37256eSGarrett Wollman static int 67057bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam) 6712c37256eSGarrett Wollman { 6722c37256eSGarrett Wollman int error = 0; 673f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 6741db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 67526ef6ac4SDon Lewis struct in_addr addr; 67626ef6ac4SDon Lewis in_port_t port = 0; 6771db24ffbSJonathan Lemon TCPDEBUG0; 6782c37256eSGarrett Wollman 6793d2d3ef4SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 6803d2d3ef4SRobert Watson return (ECONNABORTED); 681f76fcf6dSJeffrey Hsu 682f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 683623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 6848501a69cSRobert Watson INP_WLOCK(inp); 685ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 6863d2d3ef4SRobert Watson error = ECONNABORTED; 687623dce13SRobert Watson goto out; 688623dce13SRobert Watson } 6891db24ffbSJonathan Lemon tp = intotcpcb(inp); 6901db24ffbSJonathan Lemon TCPDEBUG1(); 691f76fcf6dSJeffrey Hsu 692f76fcf6dSJeffrey Hsu /* 69354d642bbSRobert Watson * We inline in_getpeeraddr and COMMON_END here, so that we can 69426ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 69526ef6ac4SDon Lewis * release the lock. 696f76fcf6dSJeffrey Hsu */ 69726ef6ac4SDon Lewis port = inp->inp_fport; 69826ef6ac4SDon Lewis addr = inp->inp_faddr; 699f76fcf6dSJeffrey Hsu 700623dce13SRobert Watson out: 701623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 7025d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 7038501a69cSRobert Watson INP_WUNLOCK(inp); 70426ef6ac4SDon Lewis if (error == 0) 70526ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 70626ef6ac4SDon Lewis return error; 7072c37256eSGarrett Wollman } 708b287c6c7SBjoern A. Zeeb #endif /* INET */ 7092c37256eSGarrett Wollman 710fb59c426SYoshinobu Inoue #ifdef INET6 711fb59c426SYoshinobu Inoue static int 712fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 713fb59c426SYoshinobu Inoue { 714f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 715fb59c426SYoshinobu Inoue int error = 0; 7161db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 71726ef6ac4SDon Lewis struct in_addr addr; 71826ef6ac4SDon Lewis struct in6_addr addr6; 71926ef6ac4SDon Lewis in_port_t port = 0; 72026ef6ac4SDon Lewis int v4 = 0; 7211db24ffbSJonathan Lemon TCPDEBUG0; 722fb59c426SYoshinobu Inoue 723b4470c16SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 724b4470c16SRobert Watson return (ECONNABORTED); 725f76fcf6dSJeffrey Hsu 726f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 727623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 728fa046d87SRobert Watson INP_INFO_RLOCK(&V_tcbinfo); 7298501a69cSRobert Watson INP_WLOCK(inp); 730ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 73121367f63SSam Leffler error = ECONNABORTED; 732623dce13SRobert Watson goto out; 733623dce13SRobert Watson } 7341db24ffbSJonathan Lemon tp = intotcpcb(inp); 7351db24ffbSJonathan Lemon TCPDEBUG1(); 736623dce13SRobert Watson 73726ef6ac4SDon Lewis /* 73826ef6ac4SDon Lewis * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 73926ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 74026ef6ac4SDon Lewis * release the lock. 74126ef6ac4SDon Lewis */ 74226ef6ac4SDon Lewis if (inp->inp_vflag & INP_IPV4) { 74326ef6ac4SDon Lewis v4 = 1; 74426ef6ac4SDon Lewis port = inp->inp_fport; 74526ef6ac4SDon Lewis addr = inp->inp_faddr; 74626ef6ac4SDon Lewis } else { 74726ef6ac4SDon Lewis port = inp->inp_fport; 74826ef6ac4SDon Lewis addr6 = inp->in6p_faddr; 74926ef6ac4SDon Lewis } 75026ef6ac4SDon Lewis 751623dce13SRobert Watson out: 752623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 7535d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 7548501a69cSRobert Watson INP_WUNLOCK(inp); 755fa046d87SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 75626ef6ac4SDon Lewis if (error == 0) { 75726ef6ac4SDon Lewis if (v4) 75826ef6ac4SDon Lewis *nam = in6_v4mapsin6_sockaddr(port, &addr); 75926ef6ac4SDon Lewis else 76026ef6ac4SDon Lewis *nam = in6_sockaddr(port, &addr6); 76126ef6ac4SDon Lewis } 76226ef6ac4SDon Lewis return error; 763fb59c426SYoshinobu Inoue } 764fb59c426SYoshinobu Inoue #endif /* INET6 */ 765f76fcf6dSJeffrey Hsu 766f76fcf6dSJeffrey Hsu /* 7672c37256eSGarrett Wollman * Mark the connection as being incapable of further output. 7682c37256eSGarrett Wollman */ 7692c37256eSGarrett Wollman static int 7702c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so) 7712c37256eSGarrett Wollman { 7722c37256eSGarrett Wollman int error = 0; 773f76fcf6dSJeffrey Hsu struct inpcb *inp; 774623dce13SRobert Watson struct tcpcb *tp = NULL; 7752c37256eSGarrett Wollman 776623dce13SRobert Watson TCPDEBUG0; 777ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 778623dce13SRobert Watson inp = sotoinpcb(so); 779623dce13SRobert Watson KASSERT(inp != NULL, ("inp == NULL")); 7808501a69cSRobert Watson INP_WLOCK(inp); 781ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 78221367f63SSam Leffler error = ECONNRESET; 783623dce13SRobert Watson goto out; 784623dce13SRobert Watson } 785623dce13SRobert Watson tp = intotcpcb(inp); 786623dce13SRobert Watson TCPDEBUG1(); 7872c37256eSGarrett Wollman socantsendmore(so); 788623dce13SRobert Watson tcp_usrclosed(tp); 789ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 79055bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 791623dce13SRobert Watson 792623dce13SRobert Watson out: 793623dce13SRobert Watson TCPDEBUG2(PRU_SHUTDOWN); 7945d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN); 7958501a69cSRobert Watson INP_WUNLOCK(inp); 796ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 797623dce13SRobert Watson 798623dce13SRobert Watson return (error); 7992c37256eSGarrett Wollman } 8002c37256eSGarrett Wollman 8012c37256eSGarrett Wollman /* 8022c37256eSGarrett Wollman * After a receive, possibly send window update to peer. 8032c37256eSGarrett Wollman */ 8042c37256eSGarrett Wollman static int 8052c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags) 8062c37256eSGarrett Wollman { 807f76fcf6dSJeffrey Hsu struct inpcb *inp; 808623dce13SRobert Watson struct tcpcb *tp = NULL; 809623dce13SRobert Watson int error = 0; 8102c37256eSGarrett Wollman 811623dce13SRobert Watson TCPDEBUG0; 812623dce13SRobert Watson inp = sotoinpcb(so); 813623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 8148501a69cSRobert Watson INP_WLOCK(inp); 815ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 81621367f63SSam Leffler error = ECONNRESET; 817623dce13SRobert Watson goto out; 818623dce13SRobert Watson } 819623dce13SRobert Watson tp = intotcpcb(inp); 820623dce13SRobert Watson TCPDEBUG1(); 821281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 822281a0fd4SPatrick Kelsey /* 823281a0fd4SPatrick Kelsey * For passively-created TFO connections, don't attempt a window 824281a0fd4SPatrick Kelsey * update while still in SYN_RECEIVED as this may trigger an early 825281a0fd4SPatrick Kelsey * SYN|ACK. It is preferable to have the SYN|ACK be sent along with 826281a0fd4SPatrick Kelsey * application response data, or failing that, when the DELACK timer 827281a0fd4SPatrick Kelsey * expires. 828281a0fd4SPatrick Kelsey */ 829281a0fd4SPatrick Kelsey if ((tp->t_flags & TF_FASTOPEN) && 830281a0fd4SPatrick Kelsey (tp->t_state == TCPS_SYN_RECEIVED)) 831281a0fd4SPatrick Kelsey goto out; 832281a0fd4SPatrick Kelsey #endif 83309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 83409fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) 83509fe6320SNavdeep Parhar tcp_offload_rcvd(tp); 836460cf046SNavdeep Parhar else 83709fe6320SNavdeep Parhar #endif 83855bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 839623dce13SRobert Watson 840623dce13SRobert Watson out: 841623dce13SRobert Watson TCPDEBUG2(PRU_RCVD); 8425d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVD); 8438501a69cSRobert Watson INP_WUNLOCK(inp); 844623dce13SRobert Watson return (error); 8452c37256eSGarrett Wollman } 8462c37256eSGarrett Wollman 8472c37256eSGarrett Wollman /* 8482c37256eSGarrett Wollman * Do a send by putting data in output queue and updating urgent 8499c9906e9SPeter Wemm * marker if URG set. Possibly send more data. Unlike the other 8509c9906e9SPeter Wemm * pru_*() routines, the mbuf chains are our responsibility. We 8519c9906e9SPeter Wemm * must either enqueue them or free them. The other pru_* routines 8529c9906e9SPeter Wemm * generally are caller-frees. 8532c37256eSGarrett Wollman */ 8542c37256eSGarrett Wollman static int 85557bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 856b40ce416SJulian Elischer struct sockaddr *nam, struct mbuf *control, struct thread *td) 8572c37256eSGarrett Wollman { 8582c37256eSGarrett Wollman int error = 0; 859f76fcf6dSJeffrey Hsu struct inpcb *inp; 860623dce13SRobert Watson struct tcpcb *tp = NULL; 861fb59c426SYoshinobu Inoue #ifdef INET6 862fb59c426SYoshinobu Inoue int isipv6; 863fb59c426SYoshinobu Inoue #endif 8649c9906e9SPeter Wemm TCPDEBUG0; 8652c37256eSGarrett Wollman 866f76fcf6dSJeffrey Hsu /* 867fa046d87SRobert Watson * We require the pcbinfo lock if we will close the socket as part of 868fa046d87SRobert Watson * this call. 869f76fcf6dSJeffrey Hsu */ 870fa046d87SRobert Watson if (flags & PRUS_EOF) 871ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 872f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 873623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 8748501a69cSRobert Watson INP_WLOCK(inp); 875ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 8767ff0b850SAndre Oppermann if (control) 8777ff0b850SAndre Oppermann m_freem(control); 8782cbcd3c1SGleb Smirnoff /* 8792cbcd3c1SGleb Smirnoff * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible 8802cbcd3c1SGleb Smirnoff * for freeing memory. 8812cbcd3c1SGleb Smirnoff */ 8822cbcd3c1SGleb Smirnoff if (m && (flags & PRUS_NOTREADY) == 0) 8837ff0b850SAndre Oppermann m_freem(m); 88421367f63SSam Leffler error = ECONNRESET; 8859c9906e9SPeter Wemm goto out; 8869c9906e9SPeter Wemm } 887fb59c426SYoshinobu Inoue #ifdef INET6 888fb59c426SYoshinobu Inoue isipv6 = nam && nam->sa_family == AF_INET6; 889fb59c426SYoshinobu Inoue #endif /* INET6 */ 8909c9906e9SPeter Wemm tp = intotcpcb(inp); 8919c9906e9SPeter Wemm TCPDEBUG1(); 8929c9906e9SPeter Wemm if (control) { 8939c9906e9SPeter Wemm /* TCP doesn't do control messages (rights, creds, etc) */ 8949c9906e9SPeter Wemm if (control->m_len) { 8959c9906e9SPeter Wemm m_freem(control); 8962c37256eSGarrett Wollman if (m) 8972c37256eSGarrett Wollman m_freem(m); 898744f87eaSDavid Greenman error = EINVAL; 899744f87eaSDavid Greenman goto out; 9002c37256eSGarrett Wollman } 9019c9906e9SPeter Wemm m_freem(control); /* empty control, just free it */ 9029c9906e9SPeter Wemm } 9032c37256eSGarrett Wollman if (!(flags & PRUS_OOB)) { 904651e4e6aSGleb Smirnoff sbappendstream(&so->so_snd, m, flags); 9052c37256eSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 9062c37256eSGarrett Wollman /* 9072c37256eSGarrett Wollman * Do implied connect if not yet connected, 9082c37256eSGarrett Wollman * initialize window to default value, and 9090c39d38dSGleb Smirnoff * initialize maxseg using peer's cached MSS. 9102c37256eSGarrett Wollman */ 911fb59c426SYoshinobu Inoue #ifdef INET6 912fb59c426SYoshinobu Inoue if (isipv6) 913b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 914fb59c426SYoshinobu Inoue #endif /* INET6 */ 915b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 916b287c6c7SBjoern A. Zeeb else 917b287c6c7SBjoern A. Zeeb #endif 918b287c6c7SBjoern A. Zeeb #ifdef INET 919b40ce416SJulian Elischer error = tcp_connect(tp, nam, td); 920b287c6c7SBjoern A. Zeeb #endif 9212c37256eSGarrett Wollman if (error) 9222c37256eSGarrett Wollman goto out; 9232c37256eSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 9242c37256eSGarrett Wollman tcp_mss(tp, -1); 9252c37256eSGarrett Wollman } 9262c37256eSGarrett Wollman if (flags & PRUS_EOF) { 9272c37256eSGarrett Wollman /* 9282c37256eSGarrett Wollman * Close the send side of the connection after 9292c37256eSGarrett Wollman * the data is sent. 9302c37256eSGarrett Wollman */ 931ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 9322c37256eSGarrett Wollman socantsendmore(so); 933623dce13SRobert Watson tcp_usrclosed(tp); 9342c37256eSGarrett Wollman } 9352cbcd3c1SGleb Smirnoff if (!(inp->inp_flags & INP_DROPPED) && 9362cbcd3c1SGleb Smirnoff !(flags & PRUS_NOTREADY)) { 937b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 938b0acefa8SBill Fenner tp->t_flags |= TF_MORETOCOME; 93955bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 940b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 941b0acefa8SBill Fenner tp->t_flags &= ~TF_MORETOCOME; 942b0acefa8SBill Fenner } 9432c37256eSGarrett Wollman } else { 944623dce13SRobert Watson /* 945623dce13SRobert Watson * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 946623dce13SRobert Watson */ 947d2bc35abSRobert Watson SOCKBUF_LOCK(&so->so_snd); 9482c37256eSGarrett Wollman if (sbspace(&so->so_snd) < -512) { 949d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 9502c37256eSGarrett Wollman m_freem(m); 9512c37256eSGarrett Wollman error = ENOBUFS; 9522c37256eSGarrett Wollman goto out; 9532c37256eSGarrett Wollman } 9542c37256eSGarrett Wollman /* 9552c37256eSGarrett Wollman * According to RFC961 (Assigned Protocols), 9562c37256eSGarrett Wollman * the urgent pointer points to the last octet 9572c37256eSGarrett Wollman * of urgent data. We continue, however, 9582c37256eSGarrett Wollman * to consider it to indicate the first octet 9592c37256eSGarrett Wollman * of data past the urgent section. 9602c37256eSGarrett Wollman * Otherwise, snd_up should be one lower. 9612c37256eSGarrett Wollman */ 962651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_snd, m, flags); 963d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 964ef53690bSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 965ef53690bSGarrett Wollman /* 966ef53690bSGarrett Wollman * Do implied connect if not yet connected, 967ef53690bSGarrett Wollman * initialize window to default value, and 9680c39d38dSGleb Smirnoff * initialize maxseg using peer's cached MSS. 969ef53690bSGarrett Wollman */ 970fb59c426SYoshinobu Inoue #ifdef INET6 971fb59c426SYoshinobu Inoue if (isipv6) 972b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 973fb59c426SYoshinobu Inoue #endif /* INET6 */ 974b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 975b287c6c7SBjoern A. Zeeb else 976b287c6c7SBjoern A. Zeeb #endif 977b287c6c7SBjoern A. Zeeb #ifdef INET 978b40ce416SJulian Elischer error = tcp_connect(tp, nam, td); 979b287c6c7SBjoern A. Zeeb #endif 980ef53690bSGarrett Wollman if (error) 981ef53690bSGarrett Wollman goto out; 982ef53690bSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 983ef53690bSGarrett Wollman tcp_mss(tp, -1); 984623dce13SRobert Watson } 985300fa232SGleb Smirnoff tp->snd_up = tp->snd_una + sbavail(&so->so_snd); 9862cbcd3c1SGleb Smirnoff if (!(flags & PRUS_NOTREADY)) { 9872cdbfa66SPaul Saab tp->t_flags |= TF_FORCEDATA; 98855bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 9892cdbfa66SPaul Saab tp->t_flags &= ~TF_FORCEDATA; 9902c37256eSGarrett Wollman } 9912cbcd3c1SGleb Smirnoff } 992d1401c90SRobert Watson out: 993d1401c90SRobert Watson TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB : 9942c37256eSGarrett Wollman ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 9955d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB : 9965d06879aSGeorge V. Neville-Neil ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 9978501a69cSRobert Watson INP_WUNLOCK(inp); 998fa046d87SRobert Watson if (flags & PRUS_EOF) 999ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 100073fddedaSPeter Grehan return (error); 10012c37256eSGarrett Wollman } 10022c37256eSGarrett Wollman 10032cbcd3c1SGleb Smirnoff static int 10042cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count) 10052cbcd3c1SGleb Smirnoff { 10062cbcd3c1SGleb Smirnoff struct inpcb *inp; 10072cbcd3c1SGleb Smirnoff struct tcpcb *tp; 10082cbcd3c1SGleb Smirnoff int error; 10092cbcd3c1SGleb Smirnoff 10102cbcd3c1SGleb Smirnoff inp = sotoinpcb(so); 10112cbcd3c1SGleb Smirnoff INP_WLOCK(inp); 10122cbcd3c1SGleb Smirnoff if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 10132cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 10142cbcd3c1SGleb Smirnoff for (int i = 0; i < count; i++) 10152cbcd3c1SGleb Smirnoff m = m_free(m); 10162cbcd3c1SGleb Smirnoff return (ECONNRESET); 10172cbcd3c1SGleb Smirnoff } 10182cbcd3c1SGleb Smirnoff tp = intotcpcb(inp); 10192cbcd3c1SGleb Smirnoff 10202cbcd3c1SGleb Smirnoff SOCKBUF_LOCK(&so->so_snd); 10212cbcd3c1SGleb Smirnoff error = sbready(&so->so_snd, m, count); 10222cbcd3c1SGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 10232cbcd3c1SGleb Smirnoff if (error == 0) 102455bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 10252cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 10262cbcd3c1SGleb Smirnoff 10272cbcd3c1SGleb Smirnoff return (error); 10282cbcd3c1SGleb Smirnoff } 10292cbcd3c1SGleb Smirnoff 10302c37256eSGarrett Wollman /* 1031a152f8a3SRobert Watson * Abort the TCP. Drop the connection abruptly. 10322c37256eSGarrett Wollman */ 1033ac45e92fSRobert Watson static void 10342c37256eSGarrett Wollman tcp_usr_abort(struct socket *so) 10352c37256eSGarrett Wollman { 1036f76fcf6dSJeffrey Hsu struct inpcb *inp; 1037a152f8a3SRobert Watson struct tcpcb *tp = NULL; 1038623dce13SRobert Watson TCPDEBUG0; 1039c78cbc7bSRobert Watson 1040ac45e92fSRobert Watson inp = sotoinpcb(so); 1041c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 1042c78cbc7bSRobert Watson 1043ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 10448501a69cSRobert Watson INP_WLOCK(inp); 1045c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 1046c78cbc7bSRobert Watson ("tcp_usr_abort: inp_socket == NULL")); 1047c78cbc7bSRobert Watson 1048c78cbc7bSRobert Watson /* 1049a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, drop. 1050c78cbc7bSRobert Watson */ 1051ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_TIMEWAIT) && 1052ad71fe3cSRobert Watson !(inp->inp_flags & INP_DROPPED)) { 1053c78cbc7bSRobert Watson tp = intotcpcb(inp); 1054a152f8a3SRobert Watson TCPDEBUG1(); 1055c78cbc7bSRobert Watson tcp_drop(tp, ECONNABORTED); 1056a152f8a3SRobert Watson TCPDEBUG2(PRU_ABORT); 10575d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ABORT); 1058c78cbc7bSRobert Watson } 1059ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1060a152f8a3SRobert Watson SOCK_LOCK(so); 1061a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 1062a152f8a3SRobert Watson SOCK_UNLOCK(so); 1063ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1064a152f8a3SRobert Watson } 10658501a69cSRobert Watson INP_WUNLOCK(inp); 1066ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1067a152f8a3SRobert Watson } 1068a152f8a3SRobert Watson 1069a152f8a3SRobert Watson /* 1070a152f8a3SRobert Watson * TCP socket is closed. Start friendly disconnect. 1071a152f8a3SRobert Watson */ 1072a152f8a3SRobert Watson static void 1073a152f8a3SRobert Watson tcp_usr_close(struct socket *so) 1074a152f8a3SRobert Watson { 1075a152f8a3SRobert Watson struct inpcb *inp; 1076a152f8a3SRobert Watson struct tcpcb *tp = NULL; 1077a152f8a3SRobert Watson TCPDEBUG0; 1078a152f8a3SRobert Watson 1079a152f8a3SRobert Watson inp = sotoinpcb(so); 1080a152f8a3SRobert Watson KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL")); 1081a152f8a3SRobert Watson 1082ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 10838501a69cSRobert Watson INP_WLOCK(inp); 1084a152f8a3SRobert Watson KASSERT(inp->inp_socket != NULL, 1085a152f8a3SRobert Watson ("tcp_usr_close: inp_socket == NULL")); 1086a152f8a3SRobert Watson 1087a152f8a3SRobert Watson /* 1088a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, initiate 1089a152f8a3SRobert Watson * a disconnect. 1090a152f8a3SRobert Watson */ 1091ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_TIMEWAIT) && 1092ad71fe3cSRobert Watson !(inp->inp_flags & INP_DROPPED)) { 1093a152f8a3SRobert Watson tp = intotcpcb(inp); 1094a152f8a3SRobert Watson TCPDEBUG1(); 1095a152f8a3SRobert Watson tcp_disconnect(tp); 1096a152f8a3SRobert Watson TCPDEBUG2(PRU_CLOSE); 10975d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CLOSE); 1098a152f8a3SRobert Watson } 1099ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1100a152f8a3SRobert Watson SOCK_LOCK(so); 1101a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 1102a152f8a3SRobert Watson SOCK_UNLOCK(so); 1103ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1104a152f8a3SRobert Watson } 11058501a69cSRobert Watson INP_WUNLOCK(inp); 1106ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 11072c37256eSGarrett Wollman } 11082c37256eSGarrett Wollman 11092c37256eSGarrett Wollman /* 11102c37256eSGarrett Wollman * Receive out-of-band data. 11112c37256eSGarrett Wollman */ 11122c37256eSGarrett Wollman static int 11132c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 11142c37256eSGarrett Wollman { 11152c37256eSGarrett Wollman int error = 0; 1116f76fcf6dSJeffrey Hsu struct inpcb *inp; 1117623dce13SRobert Watson struct tcpcb *tp = NULL; 11182c37256eSGarrett Wollman 1119623dce13SRobert Watson TCPDEBUG0; 1120623dce13SRobert Watson inp = sotoinpcb(so); 1121623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 11228501a69cSRobert Watson INP_WLOCK(inp); 1123ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 112421367f63SSam Leffler error = ECONNRESET; 1125623dce13SRobert Watson goto out; 1126623dce13SRobert Watson } 1127623dce13SRobert Watson tp = intotcpcb(inp); 1128623dce13SRobert Watson TCPDEBUG1(); 11292c37256eSGarrett Wollman if ((so->so_oobmark == 0 && 1130c0b99ffaSRobert Watson (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 11314cc20ab1SSeigo Tanimura so->so_options & SO_OOBINLINE || 11324cc20ab1SSeigo Tanimura tp->t_oobflags & TCPOOB_HADDATA) { 11332c37256eSGarrett Wollman error = EINVAL; 11342c37256eSGarrett Wollman goto out; 11352c37256eSGarrett Wollman } 11362c37256eSGarrett Wollman if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 11372c37256eSGarrett Wollman error = EWOULDBLOCK; 11382c37256eSGarrett Wollman goto out; 11392c37256eSGarrett Wollman } 11402c37256eSGarrett Wollman m->m_len = 1; 11412c37256eSGarrett Wollman *mtod(m, caddr_t) = tp->t_iobc; 11422c37256eSGarrett Wollman if ((flags & MSG_PEEK) == 0) 11432c37256eSGarrett Wollman tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1144623dce13SRobert Watson 1145623dce13SRobert Watson out: 1146623dce13SRobert Watson TCPDEBUG2(PRU_RCVOOB); 11475d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVOOB); 11488501a69cSRobert Watson INP_WUNLOCK(inp); 1149623dce13SRobert Watson return (error); 11502c37256eSGarrett Wollman } 11512c37256eSGarrett Wollman 1152b287c6c7SBjoern A. Zeeb #ifdef INET 11532c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = { 1154756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1155756d52a1SPoul-Henning Kamp .pru_accept = tcp_usr_accept, 1156756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1157756d52a1SPoul-Henning Kamp .pru_bind = tcp_usr_bind, 1158756d52a1SPoul-Henning Kamp .pru_connect = tcp_usr_connect, 1159756d52a1SPoul-Henning Kamp .pru_control = in_control, 1160756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1161756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1162756d52a1SPoul-Henning Kamp .pru_listen = tcp_usr_listen, 116354d642bbSRobert Watson .pru_peeraddr = in_getpeeraddr, 1164756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1165756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1166756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 11672cbcd3c1SGleb Smirnoff .pru_ready = tcp_usr_ready, 1168756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 116954d642bbSRobert Watson .pru_sockaddr = in_getsockaddr, 1170a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1171a152f8a3SRobert Watson .pru_close = tcp_usr_close, 11722c37256eSGarrett Wollman }; 1173b287c6c7SBjoern A. Zeeb #endif /* INET */ 1174df8bae1dSRodney W. Grimes 1175fb59c426SYoshinobu Inoue #ifdef INET6 1176fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = { 1177756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1178756d52a1SPoul-Henning Kamp .pru_accept = tcp6_usr_accept, 1179756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1180756d52a1SPoul-Henning Kamp .pru_bind = tcp6_usr_bind, 1181756d52a1SPoul-Henning Kamp .pru_connect = tcp6_usr_connect, 1182756d52a1SPoul-Henning Kamp .pru_control = in6_control, 1183756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1184756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1185756d52a1SPoul-Henning Kamp .pru_listen = tcp6_usr_listen, 1186756d52a1SPoul-Henning Kamp .pru_peeraddr = in6_mapped_peeraddr, 1187756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1188756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1189756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 11902cbcd3c1SGleb Smirnoff .pru_ready = tcp_usr_ready, 1191756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 1192756d52a1SPoul-Henning Kamp .pru_sockaddr = in6_mapped_sockaddr, 1193a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1194a152f8a3SRobert Watson .pru_close = tcp_usr_close, 1195fb59c426SYoshinobu Inoue }; 1196fb59c426SYoshinobu Inoue #endif /* INET6 */ 1197fb59c426SYoshinobu Inoue 1198b287c6c7SBjoern A. Zeeb #ifdef INET 1199a0292f23SGarrett Wollman /* 1200a0292f23SGarrett Wollman * Common subroutine to open a TCP connection to remote host specified 1201a0292f23SGarrett Wollman * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 12025200e00eSIan Dowse * port number if needed. Call in_pcbconnect_setup to do the routing and 12035200e00eSIan Dowse * to choose a local host address (interface). If there is an existing 12045200e00eSIan Dowse * incarnation of the same connection in TIME-WAIT state and if the remote 12055200e00eSIan Dowse * host was sending CC options and if the connection duration was < MSL, then 1206a0292f23SGarrett Wollman * truncate the previous TIME-WAIT state and proceed. 1207a0292f23SGarrett Wollman * Initialize connection parameters and enter SYN-SENT state. 1208a0292f23SGarrett Wollman */ 12090312fbe9SPoul-Henning Kamp static int 1210ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1211a0292f23SGarrett Wollman { 1212a0292f23SGarrett Wollman struct inpcb *inp = tp->t_inpcb, *oinp; 1213a0292f23SGarrett Wollman struct socket *so = inp->inp_socket; 12145200e00eSIan Dowse struct in_addr laddr; 12155200e00eSIan Dowse u_short lport; 1216c3229e05SDavid Greenman int error; 1217a0292f23SGarrett Wollman 12188501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1219fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 1220623dce13SRobert Watson 1221a0292f23SGarrett Wollman if (inp->inp_lport == 0) { 1222b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1223a0292f23SGarrett Wollman if (error) 1224fa046d87SRobert Watson goto out; 1225a0292f23SGarrett Wollman } 1226a0292f23SGarrett Wollman 1227a0292f23SGarrett Wollman /* 1228a0292f23SGarrett Wollman * Cannot simply call in_pcbconnect, because there might be an 1229a0292f23SGarrett Wollman * earlier incarnation of this same connection still in 1230a0292f23SGarrett Wollman * TIME_WAIT state, creating an ADDRINUSE error. 1231a0292f23SGarrett Wollman */ 12325200e00eSIan Dowse laddr = inp->inp_laddr; 12335200e00eSIan Dowse lport = inp->inp_lport; 12345200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 1235b0330ed9SPawel Jakub Dawidek &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 12365200e00eSIan Dowse if (error && oinp == NULL) 1237fa046d87SRobert Watson goto out; 1238fa046d87SRobert Watson if (oinp) { 1239fa046d87SRobert Watson error = EADDRINUSE; 1240fa046d87SRobert Watson goto out; 1241fa046d87SRobert Watson } 12425200e00eSIan Dowse inp->inp_laddr = laddr; 124315bd2b43SDavid Greenman in_pcbrehash(inp); 1244fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1245a0292f23SGarrett Wollman 1246087b55eaSAndre Oppermann /* 1247087b55eaSAndre Oppermann * Compute window scaling to request: 1248087b55eaSAndre Oppermann * Scale to fit into sweet spot. See tcp_syncache.c. 1249087b55eaSAndre Oppermann * XXX: This should move to tcp_output(). 1250087b55eaSAndre Oppermann */ 1251a0292f23SGarrett Wollman while (tp->request_r_scale < TCP_MAX_WINSHIFT && 12529b3bc6bfSMike Silbersack (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1253a0292f23SGarrett Wollman tp->request_r_scale++; 1254a0292f23SGarrett Wollman 1255a0292f23SGarrett Wollman soisconnecting(so); 125678b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 125757f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 1258b0e3ad75SMike Silbersack tp->iss = tcp_new_isn(tp); 1259a0292f23SGarrett Wollman tcp_sendseqinit(tp); 1260a45d2726SAndras Olah 1261a0292f23SGarrett Wollman return 0; 1262fa046d87SRobert Watson 1263fa046d87SRobert Watson out: 1264fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1265fa046d87SRobert Watson return (error); 1266a0292f23SGarrett Wollman } 1267b287c6c7SBjoern A. Zeeb #endif /* INET */ 1268a0292f23SGarrett Wollman 1269fb59c426SYoshinobu Inoue #ifdef INET6 1270fb59c426SYoshinobu Inoue static int 1271ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1272fb59c426SYoshinobu Inoue { 1273a7e201bbSAndrey V. Elsukov struct inpcb *inp = tp->t_inpcb; 1274fb59c426SYoshinobu Inoue int error; 1275fb59c426SYoshinobu Inoue 12768501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1277fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 1278623dce13SRobert Watson 1279fb59c426SYoshinobu Inoue if (inp->inp_lport == 0) { 1280b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1281fb59c426SYoshinobu Inoue if (error) 1282fa046d87SRobert Watson goto out; 1283fb59c426SYoshinobu Inoue } 1284a7e201bbSAndrey V. Elsukov error = in6_pcbconnect(inp, nam, td->td_ucred); 1285a7e201bbSAndrey V. Elsukov if (error != 0) 1286b598155aSRobert Watson goto out; 1287fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1288fb59c426SYoshinobu Inoue 1289fb59c426SYoshinobu Inoue /* Compute window scaling to request. */ 1290fb59c426SYoshinobu Inoue while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1291970caf60SBjoern A. Zeeb (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1292fb59c426SYoshinobu Inoue tp->request_r_scale++; 1293fb59c426SYoshinobu Inoue 1294a7e201bbSAndrey V. Elsukov soisconnecting(inp->inp_socket); 129578b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 129657f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 1297b0e3ad75SMike Silbersack tp->iss = tcp_new_isn(tp); 1298fb59c426SYoshinobu Inoue tcp_sendseqinit(tp); 1299fb59c426SYoshinobu Inoue 1300fb59c426SYoshinobu Inoue return 0; 1301fa046d87SRobert Watson 1302fa046d87SRobert Watson out: 1303fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1304fa046d87SRobert Watson return error; 1305fb59c426SYoshinobu Inoue } 1306fb59c426SYoshinobu Inoue #endif /* INET6 */ 1307fb59c426SYoshinobu Inoue 1308cfe8b629SGarrett Wollman /* 1309b8af5dfaSRobert Watson * Export TCP internal state information via a struct tcp_info, based on the 1310b8af5dfaSRobert Watson * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1311b8af5dfaSRobert Watson * (TCP state machine, etc). We export all information using FreeBSD-native 1312b8af5dfaSRobert Watson * constants -- for example, the numeric values for tcpi_state will differ 1313b8af5dfaSRobert Watson * from Linux. 1314b8af5dfaSRobert Watson */ 1315b8af5dfaSRobert Watson static void 1316ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) 1317b8af5dfaSRobert Watson { 1318b8af5dfaSRobert Watson 13198501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1320b8af5dfaSRobert Watson bzero(ti, sizeof(*ti)); 1321b8af5dfaSRobert Watson 1322b8af5dfaSRobert Watson ti->tcpi_state = tp->t_state; 1323b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1324b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 13253529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 1326b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_SACK; 1327b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1328b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_WSCALE; 1329b8af5dfaSRobert Watson ti->tcpi_snd_wscale = tp->snd_scale; 1330b8af5dfaSRobert Watson ti->tcpi_rcv_wscale = tp->rcv_scale; 1331b8af5dfaSRobert Watson } 13325a17b6adSMichael Tuexen if (tp->t_flags & TF_ECN_PERMIT) 13335a17b6adSMichael Tuexen ti->tcpi_options |= TCPI_OPT_ECN; 13341baaf834SBruce M Simpson 133543d94734SJohn Baldwin ti->tcpi_rto = tp->t_rxtcur * tick; 1336*3ac12506SJonathan T. Looney ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick; 13371baaf834SBruce M Simpson ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 13381baaf834SBruce M Simpson ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT; 13391baaf834SBruce M Simpson 1340b8af5dfaSRobert Watson ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1341b8af5dfaSRobert Watson ti->tcpi_snd_cwnd = tp->snd_cwnd; 1342b8af5dfaSRobert Watson 1343b8af5dfaSRobert Watson /* 1344b8af5dfaSRobert Watson * FreeBSD-specific extension fields for tcp_info. 1345b8af5dfaSRobert Watson */ 1346c8443a1dSRobert Watson ti->tcpi_rcv_space = tp->rcv_wnd; 1347535fbad6SKip Macy ti->tcpi_rcv_nxt = tp->rcv_nxt; 1348b8af5dfaSRobert Watson ti->tcpi_snd_wnd = tp->snd_wnd; 13491c18314dSAndre Oppermann ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */ 1350535fbad6SKip Macy ti->tcpi_snd_nxt = tp->snd_nxt; 135143d94734SJohn Baldwin ti->tcpi_snd_mss = tp->t_maxseg; 135243d94734SJohn Baldwin ti->tcpi_rcv_mss = tp->t_maxseg; 1353535fbad6SKip Macy if (tp->t_flags & TF_TOE) 1354535fbad6SKip Macy ti->tcpi_options |= TCPI_OPT_TOE; 1355f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack; 1356f5d34df5SGeorge V. Neville-Neil ti->tcpi_rcv_ooopack = tp->t_rcvoopack; 1357f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_zerowin = tp->t_sndzerowin; 1358b8af5dfaSRobert Watson } 1359b8af5dfaSRobert Watson 1360b8af5dfaSRobert Watson /* 13611e8f5ffaSRobert Watson * tcp_ctloutput() must drop the inpcb lock before performing copyin on 13621e8f5ffaSRobert Watson * socket option arguments. When it re-acquires the lock after the copy, it 13631e8f5ffaSRobert Watson * has to revalidate that the connection is still valid for the socket 13641e8f5ffaSRobert Watson * option. 1365cfe8b629SGarrett Wollman */ 1366bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \ 13678501a69cSRobert Watson INP_WLOCK(inp); \ 1368ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \ 13698501a69cSRobert Watson INP_WUNLOCK(inp); \ 1370bac5bedfSConrad Meyer cleanup; \ 13711e8f5ffaSRobert Watson return (ECONNRESET); \ 13721e8f5ffaSRobert Watson } \ 13731e8f5ffaSRobert Watson tp = intotcpcb(inp); \ 13741e8f5ffaSRobert Watson } while(0) 1375bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */) 13761e8f5ffaSRobert Watson 1377df8bae1dSRodney W. Grimes int 1378ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1379df8bae1dSRodney W. Grimes { 138055bceb1eSRandall Stewart int error; 1381df8bae1dSRodney W. Grimes struct inpcb *inp; 1382cfe8b629SGarrett Wollman struct tcpcb *tp; 138355bceb1eSRandall Stewart struct tcp_function_block *blk; 138455bceb1eSRandall Stewart struct tcp_function_set fsn; 1385df8bae1dSRodney W. Grimes 1386cfe8b629SGarrett Wollman error = 0; 1387df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1388623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 13898501a69cSRobert Watson INP_WLOCK(inp); 1390cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_TCP) { 1391fb59c426SYoshinobu Inoue #ifdef INET6 13925cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 13938501a69cSRobert Watson INP_WUNLOCK(inp); 1394fb59c426SYoshinobu Inoue error = ip6_ctloutput(so, sopt); 1395b287c6c7SBjoern A. Zeeb } 1396fb59c426SYoshinobu Inoue #endif /* INET6 */ 1397b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1398b287c6c7SBjoern A. Zeeb else 1399b287c6c7SBjoern A. Zeeb #endif 1400b287c6c7SBjoern A. Zeeb #ifdef INET 1401b287c6c7SBjoern A. Zeeb { 14028501a69cSRobert Watson INP_WUNLOCK(inp); 1403cfe8b629SGarrett Wollman error = ip_ctloutput(so, sopt); 14041e8f5ffaSRobert Watson } 14051e8f5ffaSRobert Watson #endif 1406df8bae1dSRodney W. Grimes return (error); 1407df8bae1dSRodney W. Grimes } 1408ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 14098501a69cSRobert Watson INP_WUNLOCK(inp); 14101e8f5ffaSRobert Watson return (ECONNRESET); 1411623dce13SRobert Watson } 141255bceb1eSRandall Stewart tp = intotcpcb(inp); 141355bceb1eSRandall Stewart /* 141455bceb1eSRandall Stewart * Protect the TCP option TCP_FUNCTION_BLK so 141555bceb1eSRandall Stewart * that a sub-function can *never* overwrite this. 141655bceb1eSRandall Stewart */ 141755bceb1eSRandall Stewart if ((sopt->sopt_dir == SOPT_SET) && 141855bceb1eSRandall Stewart (sopt->sopt_name == TCP_FUNCTION_BLK)) { 141955bceb1eSRandall Stewart INP_WUNLOCK(inp); 142055bceb1eSRandall Stewart error = sooptcopyin(sopt, &fsn, sizeof fsn, 142155bceb1eSRandall Stewart sizeof fsn); 142255bceb1eSRandall Stewart if (error) 142355bceb1eSRandall Stewart return (error); 142455bceb1eSRandall Stewart INP_WLOCK_RECHECK(inp); 142555bceb1eSRandall Stewart blk = find_and_ref_tcp_functions(&fsn); 142655bceb1eSRandall Stewart if (blk == NULL) { 142755bceb1eSRandall Stewart INP_WUNLOCK(inp); 142855bceb1eSRandall Stewart return (ENOENT); 142955bceb1eSRandall Stewart } 1430587d67c0SRandall Stewart if (tp->t_fb == blk) { 1431587d67c0SRandall Stewart /* You already have this */ 1432587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1433587d67c0SRandall Stewart INP_WUNLOCK(inp); 1434587d67c0SRandall Stewart return (0); 1435587d67c0SRandall Stewart } 1436587d67c0SRandall Stewart if (tp->t_state != TCPS_CLOSED) { 1437587d67c0SRandall Stewart int error=EINVAL; 1438587d67c0SRandall Stewart /* 1439587d67c0SRandall Stewart * The user has advanced the state 1440587d67c0SRandall Stewart * past the initial point, we may not 1441587d67c0SRandall Stewart * be able to switch. 1442587d67c0SRandall Stewart */ 1443587d67c0SRandall Stewart if (blk->tfb_tcp_handoff_ok != NULL) { 1444587d67c0SRandall Stewart /* 1445587d67c0SRandall Stewart * Does the stack provide a 1446587d67c0SRandall Stewart * query mechanism, if so it may 1447587d67c0SRandall Stewart * still be possible? 1448587d67c0SRandall Stewart */ 1449587d67c0SRandall Stewart error = (*blk->tfb_tcp_handoff_ok)(tp); 1450587d67c0SRandall Stewart } 1451587d67c0SRandall Stewart if (error) { 1452587d67c0SRandall Stewart refcount_release(&blk->tfb_refcnt); 1453587d67c0SRandall Stewart INP_WUNLOCK(inp); 1454587d67c0SRandall Stewart return(error); 1455587d67c0SRandall Stewart } 1456587d67c0SRandall Stewart } 145755bceb1eSRandall Stewart if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) { 145855bceb1eSRandall Stewart refcount_release(&blk->tfb_refcnt); 145955bceb1eSRandall Stewart INP_WUNLOCK(inp); 146055bceb1eSRandall Stewart return (ENOENT); 146155bceb1eSRandall Stewart } 146255bceb1eSRandall Stewart /* 146355bceb1eSRandall Stewart * Release the old refcnt, the 1464587d67c0SRandall Stewart * lookup acquired a ref on the 1465587d67c0SRandall Stewart * new one already. 146655bceb1eSRandall Stewart */ 1467587d67c0SRandall Stewart if (tp->t_fb->tfb_tcp_fb_fini) { 1468587d67c0SRandall Stewart /* 1469587d67c0SRandall Stewart * Tell the stack to cleanup with 0 i.e. 1470587d67c0SRandall Stewart * the tcb is not going away. 1471587d67c0SRandall Stewart */ 1472587d67c0SRandall Stewart (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0); 1473587d67c0SRandall Stewart } 147455bceb1eSRandall Stewart refcount_release(&tp->t_fb->tfb_refcnt); 147555bceb1eSRandall Stewart tp->t_fb = blk; 147655bceb1eSRandall Stewart if (tp->t_fb->tfb_tcp_fb_init) { 147755bceb1eSRandall Stewart (*tp->t_fb->tfb_tcp_fb_init)(tp); 147855bceb1eSRandall Stewart } 147955bceb1eSRandall Stewart #ifdef TCP_OFFLOAD 148055bceb1eSRandall Stewart if (tp->t_flags & TF_TOE) { 148155bceb1eSRandall Stewart tcp_offload_ctloutput(tp, sopt->sopt_dir, 148255bceb1eSRandall Stewart sopt->sopt_name); 148355bceb1eSRandall Stewart } 148455bceb1eSRandall Stewart #endif 148555bceb1eSRandall Stewart INP_WUNLOCK(inp); 148655bceb1eSRandall Stewart return (error); 148755bceb1eSRandall Stewart } else if ((sopt->sopt_dir == SOPT_GET) && 148855bceb1eSRandall Stewart (sopt->sopt_name == TCP_FUNCTION_BLK)) { 148955bceb1eSRandall Stewart strcpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name); 149055bceb1eSRandall Stewart fsn.pcbcnt = tp->t_fb->tfb_refcnt; 149155bceb1eSRandall Stewart INP_WUNLOCK(inp); 149255bceb1eSRandall Stewart error = sooptcopyout(sopt, &fsn, sizeof fsn); 149355bceb1eSRandall Stewart return (error); 149455bceb1eSRandall Stewart } 149555bceb1eSRandall Stewart /* Pass in the INP locked, called must unlock it */ 149655bceb1eSRandall Stewart return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp)); 149755bceb1eSRandall Stewart } 149855bceb1eSRandall Stewart 149955bceb1eSRandall Stewart int 150055bceb1eSRandall Stewart tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp) 150155bceb1eSRandall Stewart { 150255bceb1eSRandall Stewart int error, opt, optval; 150355bceb1eSRandall Stewart u_int ui; 150455bceb1eSRandall Stewart struct tcp_info ti; 150555bceb1eSRandall Stewart struct cc_algo *algo; 1506af6fef3aSGleb Smirnoff char *pbuf, buf[TCP_CA_NAME_MAX]; 1507af6fef3aSGleb Smirnoff size_t len; 1508df8bae1dSRodney W. Grimes 1509d519cedbSGleb Smirnoff /* 1510d519cedbSGleb Smirnoff * For TCP_CCALGOOPT forward the control to CC module, for both 1511d519cedbSGleb Smirnoff * SOPT_SET and SOPT_GET. 1512d519cedbSGleb Smirnoff */ 1513d519cedbSGleb Smirnoff switch (sopt->sopt_name) { 1514d519cedbSGleb Smirnoff case TCP_CCALGOOPT: 1515d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 1516af6fef3aSGleb Smirnoff pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO); 1517af6fef3aSGleb Smirnoff error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize, 1518d519cedbSGleb Smirnoff sopt->sopt_valsize); 1519d519cedbSGleb Smirnoff if (error) { 1520af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 1521d519cedbSGleb Smirnoff return (error); 1522d519cedbSGleb Smirnoff } 1523bac5bedfSConrad Meyer INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP)); 1524d519cedbSGleb Smirnoff if (CC_ALGO(tp)->ctl_output != NULL) 1525af6fef3aSGleb Smirnoff error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf); 1526d519cedbSGleb Smirnoff else 1527d519cedbSGleb Smirnoff error = ENOENT; 1528d519cedbSGleb Smirnoff INP_WUNLOCK(inp); 1529d519cedbSGleb Smirnoff if (error == 0 && sopt->sopt_dir == SOPT_GET) 1530af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize); 1531af6fef3aSGleb Smirnoff free(pbuf, M_TEMP); 1532d519cedbSGleb Smirnoff return (error); 1533d519cedbSGleb Smirnoff } 1534d519cedbSGleb Smirnoff 1535cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 1536cfe8b629SGarrett Wollman case SOPT_SET: 1537cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 15381cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 153988f6b043SBruce M Simpson case TCP_MD5SIG: 15408501a69cSRobert Watson INP_WUNLOCK(inp); 15411cfd4b53SBruce M Simpson error = sooptcopyin(sopt, &optval, sizeof optval, 15421cfd4b53SBruce M Simpson sizeof optval); 15431cfd4b53SBruce M Simpson if (error) 15441e8f5ffaSRobert Watson return (error); 15451cfd4b53SBruce M Simpson 15468501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 15471cfd4b53SBruce M Simpson if (optval > 0) 15481cfd4b53SBruce M Simpson tp->t_flags |= TF_SIGNATURE; 15491cfd4b53SBruce M Simpson else 15501cfd4b53SBruce M Simpson tp->t_flags &= ~TF_SIGNATURE; 155109fe6320SNavdeep Parhar goto unlock_and_done; 15521cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */ 155309fe6320SNavdeep Parhar 1554df8bae1dSRodney W. Grimes case TCP_NODELAY: 1555cfe8b629SGarrett Wollman case TCP_NOOPT: 15568501a69cSRobert Watson INP_WUNLOCK(inp); 1557cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1558cfe8b629SGarrett Wollman sizeof optval); 1559cfe8b629SGarrett Wollman if (error) 15601e8f5ffaSRobert Watson return (error); 1561cfe8b629SGarrett Wollman 15628501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 1563cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1564cfe8b629SGarrett Wollman case TCP_NODELAY: 1565cfe8b629SGarrett Wollman opt = TF_NODELAY; 1566cfe8b629SGarrett Wollman break; 1567cfe8b629SGarrett Wollman case TCP_NOOPT: 1568cfe8b629SGarrett Wollman opt = TF_NOOPT; 1569cfe8b629SGarrett Wollman break; 1570cfe8b629SGarrett Wollman default: 1571cfe8b629SGarrett Wollman opt = 0; /* dead code to fool gcc */ 1572cfe8b629SGarrett Wollman break; 1573cfe8b629SGarrett Wollman } 1574cfe8b629SGarrett Wollman 1575cfe8b629SGarrett Wollman if (optval) 1576cfe8b629SGarrett Wollman tp->t_flags |= opt; 1577df8bae1dSRodney W. Grimes else 1578cfe8b629SGarrett Wollman tp->t_flags &= ~opt; 157909fe6320SNavdeep Parhar unlock_and_done: 158009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 158109fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 158209fe6320SNavdeep Parhar tcp_offload_ctloutput(tp, sopt->sopt_dir, 158309fe6320SNavdeep Parhar sopt->sopt_name); 158409fe6320SNavdeep Parhar } 158509fe6320SNavdeep Parhar #endif 15868501a69cSRobert Watson INP_WUNLOCK(inp); 1587df8bae1dSRodney W. Grimes break; 1588df8bae1dSRodney W. Grimes 1589007581c0SJonathan Lemon case TCP_NOPUSH: 15908501a69cSRobert Watson INP_WUNLOCK(inp); 1591007581c0SJonathan Lemon error = sooptcopyin(sopt, &optval, sizeof optval, 1592007581c0SJonathan Lemon sizeof optval); 1593007581c0SJonathan Lemon if (error) 15941e8f5ffaSRobert Watson return (error); 1595007581c0SJonathan Lemon 15968501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 1597007581c0SJonathan Lemon if (optval) 1598007581c0SJonathan Lemon tp->t_flags |= TF_NOPUSH; 1599d28b9e89SJohn Baldwin else if (tp->t_flags & TF_NOPUSH) { 1600007581c0SJonathan Lemon tp->t_flags &= ~TF_NOPUSH; 1601d28b9e89SJohn Baldwin if (TCPS_HAVEESTABLISHED(tp->t_state)) 160255bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 1603007581c0SJonathan Lemon } 160409fe6320SNavdeep Parhar goto unlock_and_done; 1605007581c0SJonathan Lemon 1606df8bae1dSRodney W. Grimes case TCP_MAXSEG: 16078501a69cSRobert Watson INP_WUNLOCK(inp); 1608cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1609cfe8b629SGarrett Wollman sizeof optval); 1610cfe8b629SGarrett Wollman if (error) 16111e8f5ffaSRobert Watson return (error); 1612df8bae1dSRodney W. Grimes 16138501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 161453369ac9SAndre Oppermann if (optval > 0 && optval <= tp->t_maxseg && 1615603724d3SBjoern A. Zeeb optval + 40 >= V_tcp_minmss) 1616cfe8b629SGarrett Wollman tp->t_maxseg = optval; 1617a0292f23SGarrett Wollman else 1618a0292f23SGarrett Wollman error = EINVAL; 161909fe6320SNavdeep Parhar goto unlock_and_done; 1620a0292f23SGarrett Wollman 1621b8af5dfaSRobert Watson case TCP_INFO: 16228501a69cSRobert Watson INP_WUNLOCK(inp); 1623b8af5dfaSRobert Watson error = EINVAL; 1624b8af5dfaSRobert Watson break; 1625b8af5dfaSRobert Watson 1626dbc42409SLawrence Stewart case TCP_CONGESTION: 1627dbc42409SLawrence Stewart INP_WUNLOCK(inp); 1628af6fef3aSGleb Smirnoff error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1); 1629af6fef3aSGleb Smirnoff if (error) 1630dbc42409SLawrence Stewart break; 1631af6fef3aSGleb Smirnoff buf[sopt->sopt_valsize] = '\0'; 1632af6fef3aSGleb Smirnoff INP_WLOCK_RECHECK(inp); 163373e263b1SGleb Smirnoff CC_LIST_RLOCK(); 163473e263b1SGleb Smirnoff STAILQ_FOREACH(algo, &cc_list, entries) 163573e263b1SGleb Smirnoff if (strncmp(buf, algo->name, 163673e263b1SGleb Smirnoff TCP_CA_NAME_MAX) == 0) 163773e263b1SGleb Smirnoff break; 163873e263b1SGleb Smirnoff CC_LIST_RUNLOCK(); 163973e263b1SGleb Smirnoff if (algo == NULL) { 1640af6fef3aSGleb Smirnoff INP_WUNLOCK(inp); 164173e263b1SGleb Smirnoff error = EINVAL; 164273e263b1SGleb Smirnoff break; 164373e263b1SGleb Smirnoff } 1644dbc42409SLawrence Stewart /* 164573e263b1SGleb Smirnoff * We hold a write lock over the tcb so it's safe to 164673e263b1SGleb Smirnoff * do these things without ordering concerns. 1647dbc42409SLawrence Stewart */ 1648dbc42409SLawrence Stewart if (CC_ALGO(tp)->cb_destroy != NULL) 1649dbc42409SLawrence Stewart CC_ALGO(tp)->cb_destroy(tp->ccv); 1650dbc42409SLawrence Stewart CC_ALGO(tp) = algo; 1651dbc42409SLawrence Stewart /* 165273e263b1SGleb Smirnoff * If something goes pear shaped initialising the new 165373e263b1SGleb Smirnoff * algo, fall back to newreno (which does not 165473e263b1SGleb Smirnoff * require initialisation). 1655dbc42409SLawrence Stewart */ 165673e263b1SGleb Smirnoff if (algo->cb_init != NULL && 165773e263b1SGleb Smirnoff algo->cb_init(tp->ccv) != 0) { 1658dbc42409SLawrence Stewart CC_ALGO(tp) = &newreno_cc_algo; 1659dbc42409SLawrence Stewart /* 166073e263b1SGleb Smirnoff * The only reason init should fail is 1661dbc42409SLawrence Stewart * because of malloc. 1662dbc42409SLawrence Stewart */ 1663dbc42409SLawrence Stewart error = ENOMEM; 1664dbc42409SLawrence Stewart } 166573e263b1SGleb Smirnoff INP_WUNLOCK(inp); 166673e263b1SGleb Smirnoff break; 1667dbc42409SLawrence Stewart 16689077f387SGleb Smirnoff case TCP_KEEPIDLE: 16699077f387SGleb Smirnoff case TCP_KEEPINTVL: 16709077f387SGleb Smirnoff case TCP_KEEPINIT: 16719077f387SGleb Smirnoff INP_WUNLOCK(inp); 16729077f387SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 16739077f387SGleb Smirnoff if (error) 16749077f387SGleb Smirnoff return (error); 16759077f387SGleb Smirnoff 16769077f387SGleb Smirnoff if (ui > (UINT_MAX / hz)) { 16779077f387SGleb Smirnoff error = EINVAL; 16789077f387SGleb Smirnoff break; 16799077f387SGleb Smirnoff } 16809077f387SGleb Smirnoff ui *= hz; 16819077f387SGleb Smirnoff 16829077f387SGleb Smirnoff INP_WLOCK_RECHECK(inp); 16839077f387SGleb Smirnoff switch (sopt->sopt_name) { 16849077f387SGleb Smirnoff case TCP_KEEPIDLE: 16859077f387SGleb Smirnoff tp->t_keepidle = ui; 16869077f387SGleb Smirnoff /* 16879077f387SGleb Smirnoff * XXX: better check current remaining 16889077f387SGleb Smirnoff * timeout and "merge" it with new value. 16899077f387SGleb Smirnoff */ 16909077f387SGleb Smirnoff if ((tp->t_state > TCPS_LISTEN) && 16919077f387SGleb Smirnoff (tp->t_state <= TCPS_CLOSING)) 16929077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 16939077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 16949077f387SGleb Smirnoff break; 16959077f387SGleb Smirnoff case TCP_KEEPINTVL: 16969077f387SGleb Smirnoff tp->t_keepintvl = ui; 16979077f387SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 16989077f387SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 16999077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 17009077f387SGleb Smirnoff TP_MAXIDLE(tp)); 17019077f387SGleb Smirnoff break; 17029077f387SGleb Smirnoff case TCP_KEEPINIT: 17039077f387SGleb Smirnoff tp->t_keepinit = ui; 17049077f387SGleb Smirnoff if (tp->t_state == TCPS_SYN_RECEIVED || 17059077f387SGleb Smirnoff tp->t_state == TCPS_SYN_SENT) 17069077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 17079077f387SGleb Smirnoff TP_KEEPINIT(tp)); 17089077f387SGleb Smirnoff break; 17099077f387SGleb Smirnoff } 171009fe6320SNavdeep Parhar goto unlock_and_done; 17119077f387SGleb Smirnoff 171285c05144SGleb Smirnoff case TCP_KEEPCNT: 171385c05144SGleb Smirnoff INP_WUNLOCK(inp); 171485c05144SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 171585c05144SGleb Smirnoff if (error) 171685c05144SGleb Smirnoff return (error); 171785c05144SGleb Smirnoff 171885c05144SGleb Smirnoff INP_WLOCK_RECHECK(inp); 171985c05144SGleb Smirnoff tp->t_keepcnt = ui; 172085c05144SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 172185c05144SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 172285c05144SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 172385c05144SGleb Smirnoff TP_MAXIDLE(tp)); 172485c05144SGleb Smirnoff goto unlock_and_done; 172585c05144SGleb Smirnoff 172686a996e6SHiren Panchasara #ifdef TCPPCAP 172786a996e6SHiren Panchasara case TCP_PCAP_OUT: 172886a996e6SHiren Panchasara case TCP_PCAP_IN: 172986a996e6SHiren Panchasara INP_WUNLOCK(inp); 173086a996e6SHiren Panchasara error = sooptcopyin(sopt, &optval, sizeof optval, 173186a996e6SHiren Panchasara sizeof optval); 173286a996e6SHiren Panchasara if (error) 173386a996e6SHiren Panchasara return (error); 173486a996e6SHiren Panchasara 173586a996e6SHiren Panchasara INP_WLOCK_RECHECK(inp); 173686a996e6SHiren Panchasara if (optval >= 0) 173786a996e6SHiren Panchasara tcp_pcap_set_sock_max(TCP_PCAP_OUT ? 173886a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts), 173986a996e6SHiren Panchasara optval); 174086a996e6SHiren Panchasara else 174186a996e6SHiren Panchasara error = EINVAL; 174286a996e6SHiren Panchasara goto unlock_and_done; 174386a996e6SHiren Panchasara #endif 174486a996e6SHiren Panchasara 1745281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 1746281a0fd4SPatrick Kelsey case TCP_FASTOPEN: 1747281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 1748281a0fd4SPatrick Kelsey if (!V_tcp_fastopen_enabled) 1749281a0fd4SPatrick Kelsey return (EPERM); 1750281a0fd4SPatrick Kelsey 1751281a0fd4SPatrick Kelsey error = sooptcopyin(sopt, &optval, sizeof optval, 1752281a0fd4SPatrick Kelsey sizeof optval); 1753281a0fd4SPatrick Kelsey if (error) 1754281a0fd4SPatrick Kelsey return (error); 1755281a0fd4SPatrick Kelsey 1756281a0fd4SPatrick Kelsey INP_WLOCK_RECHECK(inp); 1757281a0fd4SPatrick Kelsey if (optval) { 1758281a0fd4SPatrick Kelsey tp->t_flags |= TF_FASTOPEN; 1759281a0fd4SPatrick Kelsey if ((tp->t_state == TCPS_LISTEN) && 1760281a0fd4SPatrick Kelsey (tp->t_tfo_pending == NULL)) 1761281a0fd4SPatrick Kelsey tp->t_tfo_pending = 1762281a0fd4SPatrick Kelsey tcp_fastopen_alloc_counter(); 1763281a0fd4SPatrick Kelsey } else 1764281a0fd4SPatrick Kelsey tp->t_flags &= ~TF_FASTOPEN; 1765281a0fd4SPatrick Kelsey goto unlock_and_done; 1766281a0fd4SPatrick Kelsey #endif 1767281a0fd4SPatrick Kelsey 1768df8bae1dSRodney W. Grimes default: 17698501a69cSRobert Watson INP_WUNLOCK(inp); 1770df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1771df8bae1dSRodney W. Grimes break; 1772df8bae1dSRodney W. Grimes } 1773df8bae1dSRodney W. Grimes break; 1774df8bae1dSRodney W. Grimes 1775cfe8b629SGarrett Wollman case SOPT_GET: 17761e8f5ffaSRobert Watson tp = intotcpcb(inp); 1777cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 17781cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 177988f6b043SBruce M Simpson case TCP_MD5SIG: 17801cfd4b53SBruce M Simpson optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0; 17818501a69cSRobert Watson INP_WUNLOCK(inp); 1782b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 17831cfd4b53SBruce M Simpson break; 1784265ed012SBruce M Simpson #endif 17851e8f5ffaSRobert Watson 1786df8bae1dSRodney W. Grimes case TCP_NODELAY: 1787cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NODELAY; 17888501a69cSRobert Watson INP_WUNLOCK(inp); 1789b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1790df8bae1dSRodney W. Grimes break; 1791df8bae1dSRodney W. Grimes case TCP_MAXSEG: 1792cfe8b629SGarrett Wollman optval = tp->t_maxseg; 17938501a69cSRobert Watson INP_WUNLOCK(inp); 1794b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1795df8bae1dSRodney W. Grimes break; 1796a0292f23SGarrett Wollman case TCP_NOOPT: 1797cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOOPT; 17988501a69cSRobert Watson INP_WUNLOCK(inp); 1799b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1800a0292f23SGarrett Wollman break; 1801a0292f23SGarrett Wollman case TCP_NOPUSH: 1802cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOPUSH; 18038501a69cSRobert Watson INP_WUNLOCK(inp); 1804b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1805b8af5dfaSRobert Watson break; 1806b8af5dfaSRobert Watson case TCP_INFO: 1807b8af5dfaSRobert Watson tcp_fill_info(tp, &ti); 18088501a69cSRobert Watson INP_WUNLOCK(inp); 1809b8af5dfaSRobert Watson error = sooptcopyout(sopt, &ti, sizeof ti); 1810a0292f23SGarrett Wollman break; 1811dbc42409SLawrence Stewart case TCP_CONGESTION: 1812af6fef3aSGleb Smirnoff len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); 1813dbc42409SLawrence Stewart INP_WUNLOCK(inp); 1814af6fef3aSGleb Smirnoff error = sooptcopyout(sopt, buf, len + 1); 1815dbc42409SLawrence Stewart break; 18162f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 18172f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 18182f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 18192f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 18202f3eb7f4SGleb Smirnoff switch (sopt->sopt_name) { 18212f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 18225a17b6adSMichael Tuexen ui = TP_KEEPIDLE(tp) / hz; 18232f3eb7f4SGleb Smirnoff break; 18242f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 18255a17b6adSMichael Tuexen ui = TP_KEEPINTVL(tp) / hz; 18262f3eb7f4SGleb Smirnoff break; 18272f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 18285a17b6adSMichael Tuexen ui = TP_KEEPINIT(tp) / hz; 18292f3eb7f4SGleb Smirnoff break; 18302f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 18315a17b6adSMichael Tuexen ui = TP_KEEPCNT(tp); 18322f3eb7f4SGleb Smirnoff break; 18332f3eb7f4SGleb Smirnoff } 18342f3eb7f4SGleb Smirnoff INP_WUNLOCK(inp); 18352f3eb7f4SGleb Smirnoff error = sooptcopyout(sopt, &ui, sizeof(ui)); 18362f3eb7f4SGleb Smirnoff break; 183786a996e6SHiren Panchasara #ifdef TCPPCAP 183886a996e6SHiren Panchasara case TCP_PCAP_OUT: 183986a996e6SHiren Panchasara case TCP_PCAP_IN: 184086a996e6SHiren Panchasara optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ? 184186a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts)); 184286a996e6SHiren Panchasara INP_WUNLOCK(inp); 184386a996e6SHiren Panchasara error = sooptcopyout(sopt, &optval, sizeof optval); 184486a996e6SHiren Panchasara break; 184586a996e6SHiren Panchasara #endif 1846281a0fd4SPatrick Kelsey 1847281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 1848281a0fd4SPatrick Kelsey case TCP_FASTOPEN: 1849281a0fd4SPatrick Kelsey optval = tp->t_flags & TF_FASTOPEN; 1850281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 1851281a0fd4SPatrick Kelsey error = sooptcopyout(sopt, &optval, sizeof optval); 1852281a0fd4SPatrick Kelsey break; 1853281a0fd4SPatrick Kelsey #endif 1854df8bae1dSRodney W. Grimes default: 18558501a69cSRobert Watson INP_WUNLOCK(inp); 1856df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1857df8bae1dSRodney W. Grimes break; 1858df8bae1dSRodney W. Grimes } 1859df8bae1dSRodney W. Grimes break; 1860df8bae1dSRodney W. Grimes } 1861df8bae1dSRodney W. Grimes return (error); 1862df8bae1dSRodney W. Grimes } 18638501a69cSRobert Watson #undef INP_WLOCK_RECHECK 1864bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP 1865df8bae1dSRodney W. Grimes 186626e30fbbSDavid Greenman /* 1867df8bae1dSRodney W. Grimes * Attach TCP protocol to socket, allocating 1868df8bae1dSRodney W. Grimes * internet protocol control block, tcp control block, 1869df8bae1dSRodney W. Grimes * bufer space, and entering LISTEN state if to accept connections. 1870df8bae1dSRodney W. Grimes */ 18710312fbe9SPoul-Henning Kamp static int 1872ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so) 1873df8bae1dSRodney W. Grimes { 1874ad3f9ab3SAndre Oppermann struct tcpcb *tp; 1875df8bae1dSRodney W. Grimes struct inpcb *inp; 1876df8bae1dSRodney W. Grimes int error; 1877df8bae1dSRodney W. Grimes 1878df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1879e233e2acSAndre Oppermann error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace); 1880df8bae1dSRodney W. Grimes if (error) 1881df8bae1dSRodney W. Grimes return (error); 1882df8bae1dSRodney W. Grimes } 18836741ecf5SAndre Oppermann so->so_rcv.sb_flags |= SB_AUTOSIZE; 18846741ecf5SAndre Oppermann so->so_snd.sb_flags |= SB_AUTOSIZE; 1885ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 1886603724d3SBjoern A. Zeeb error = in_pcballoc(so, &V_tcbinfo); 1887f2de87feSRobert Watson if (error) { 1888ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1889df8bae1dSRodney W. Grimes return (error); 1890f2de87feSRobert Watson } 1891df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1892fb59c426SYoshinobu Inoue #ifdef INET6 18935cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 1894fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 1895fb59c426SYoshinobu Inoue inp->in6p_hops = -1; /* use kernel default */ 1896fb59c426SYoshinobu Inoue } 1897fb59c426SYoshinobu Inoue else 1898fb59c426SYoshinobu Inoue #endif 1899cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 1900df8bae1dSRodney W. Grimes tp = tcp_newtcpcb(inp); 1901623dce13SRobert Watson if (tp == NULL) { 1902df8bae1dSRodney W. Grimes in_pcbdetach(inp); 19030206cdb8SBjoern A. Zeeb in_pcbfree(inp); 1904ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1905df8bae1dSRodney W. Grimes return (ENOBUFS); 1906df8bae1dSRodney W. Grimes } 1907df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 19088501a69cSRobert Watson INP_WUNLOCK(inp); 1909ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1910bf840a17SGleb Smirnoff TCPSTATES_INC(TCPS_CLOSED); 1911df8bae1dSRodney W. Grimes return (0); 1912df8bae1dSRodney W. Grimes } 1913df8bae1dSRodney W. Grimes 1914df8bae1dSRodney W. Grimes /* 1915df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 1916df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 1917df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 1918df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 1919df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 1920df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 1921df8bae1dSRodney W. Grimes */ 1922623dce13SRobert Watson static void 1923ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp) 1924df8bae1dSRodney W. Grimes { 1925e6e0b5ffSRobert Watson struct inpcb *inp = tp->t_inpcb; 1926e6e0b5ffSRobert Watson struct socket *so = inp->inp_socket; 1927e6e0b5ffSRobert Watson 1928ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 19298501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1930df8bae1dSRodney W. Grimes 1931623dce13SRobert Watson /* 1932623dce13SRobert Watson * Neither tcp_close() nor tcp_drop() should return NULL, as the 1933623dce13SRobert Watson * socket is still open. 1934623dce13SRobert Watson */ 1935623dce13SRobert Watson if (tp->t_state < TCPS_ESTABLISHED) { 1936df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1937623dce13SRobert Watson KASSERT(tp != NULL, 1938623dce13SRobert Watson ("tcp_disconnect: tcp_close() returned NULL")); 1939623dce13SRobert Watson } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 1940243917feSSeigo Tanimura tp = tcp_drop(tp, 0); 1941623dce13SRobert Watson KASSERT(tp != NULL, 1942623dce13SRobert Watson ("tcp_disconnect: tcp_drop() returned NULL")); 1943623dce13SRobert Watson } else { 1944df8bae1dSRodney W. Grimes soisdisconnecting(so); 1945df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 1946623dce13SRobert Watson tcp_usrclosed(tp); 1947ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 194855bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 1949df8bae1dSRodney W. Grimes } 1950df8bae1dSRodney W. Grimes } 1951df8bae1dSRodney W. Grimes 1952df8bae1dSRodney W. Grimes /* 1953df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 1954df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 1955df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1956df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 1957df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 1958df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 1959df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 1960df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 1961df8bae1dSRodney W. Grimes */ 1962623dce13SRobert Watson static void 1963ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp) 1964df8bae1dSRodney W. Grimes { 1965df8bae1dSRodney W. Grimes 1966ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 19678501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1968e6e0b5ffSRobert Watson 1969df8bae1dSRodney W. Grimes switch (tp->t_state) { 1970df8bae1dSRodney W. Grimes case TCPS_LISTEN: 197109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 197209fe6320SNavdeep Parhar tcp_offload_listen_stop(tp); 197309fe6320SNavdeep Parhar #endif 1974550e9d42SHiren Panchasara tcp_state_change(tp, TCPS_CLOSED); 1975bc65987aSKip Macy /* FALLTHROUGH */ 1976bc65987aSKip Macy case TCPS_CLOSED: 1977df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1978623dce13SRobert Watson /* 1979623dce13SRobert Watson * tcp_close() should never return NULL here as the socket is 1980623dce13SRobert Watson * still open. 1981623dce13SRobert Watson */ 1982623dce13SRobert Watson KASSERT(tp != NULL, 1983623dce13SRobert Watson ("tcp_usrclosed: tcp_close() returned NULL")); 1984df8bae1dSRodney W. Grimes break; 1985df8bae1dSRodney W. Grimes 1986a0292f23SGarrett Wollman case TCPS_SYN_SENT: 1987df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1988a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 1989a0292f23SGarrett Wollman break; 1990a0292f23SGarrett Wollman 1991df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 199257f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 1993df8bae1dSRodney W. Grimes break; 1994df8bae1dSRodney W. Grimes 1995df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 199657f60867SMark Johnston tcp_state_change(tp, TCPS_LAST_ACK); 1997df8bae1dSRodney W. Grimes break; 1998df8bae1dSRodney W. Grimes } 1999abc7d910SRobert Watson if (tp->t_state >= TCPS_FIN_WAIT_2) { 2000df8bae1dSRodney W. Grimes soisdisconnected(tp->t_inpcb->inp_socket); 2001abc7d910SRobert Watson /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 20027c72af87SMohan Srinivasan if (tp->t_state == TCPS_FIN_WAIT_2) { 20037c72af87SMohan Srinivasan int timeout; 20047c72af87SMohan Srinivasan 20057c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 20069077f387SGleb Smirnoff tcp_finwait2_timeout : TP_MAXIDLE(tp); 2007b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 2008b6239c4aSAndras Olah } 2009df8bae1dSRodney W. Grimes } 20107c72af87SMohan Srinivasan } 2011497057eeSRobert Watson 2012497057eeSRobert Watson #ifdef DDB 2013497057eeSRobert Watson static void 2014497057eeSRobert Watson db_print_indent(int indent) 2015497057eeSRobert Watson { 2016497057eeSRobert Watson int i; 2017497057eeSRobert Watson 2018497057eeSRobert Watson for (i = 0; i < indent; i++) 2019497057eeSRobert Watson db_printf(" "); 2020497057eeSRobert Watson } 2021497057eeSRobert Watson 2022497057eeSRobert Watson static void 2023497057eeSRobert Watson db_print_tstate(int t_state) 2024497057eeSRobert Watson { 2025497057eeSRobert Watson 2026497057eeSRobert Watson switch (t_state) { 2027497057eeSRobert Watson case TCPS_CLOSED: 2028497057eeSRobert Watson db_printf("TCPS_CLOSED"); 2029497057eeSRobert Watson return; 2030497057eeSRobert Watson 2031497057eeSRobert Watson case TCPS_LISTEN: 2032497057eeSRobert Watson db_printf("TCPS_LISTEN"); 2033497057eeSRobert Watson return; 2034497057eeSRobert Watson 2035497057eeSRobert Watson case TCPS_SYN_SENT: 2036497057eeSRobert Watson db_printf("TCPS_SYN_SENT"); 2037497057eeSRobert Watson return; 2038497057eeSRobert Watson 2039497057eeSRobert Watson case TCPS_SYN_RECEIVED: 2040497057eeSRobert Watson db_printf("TCPS_SYN_RECEIVED"); 2041497057eeSRobert Watson return; 2042497057eeSRobert Watson 2043497057eeSRobert Watson case TCPS_ESTABLISHED: 2044497057eeSRobert Watson db_printf("TCPS_ESTABLISHED"); 2045497057eeSRobert Watson return; 2046497057eeSRobert Watson 2047497057eeSRobert Watson case TCPS_CLOSE_WAIT: 2048497057eeSRobert Watson db_printf("TCPS_CLOSE_WAIT"); 2049497057eeSRobert Watson return; 2050497057eeSRobert Watson 2051497057eeSRobert Watson case TCPS_FIN_WAIT_1: 2052497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_1"); 2053497057eeSRobert Watson return; 2054497057eeSRobert Watson 2055497057eeSRobert Watson case TCPS_CLOSING: 2056497057eeSRobert Watson db_printf("TCPS_CLOSING"); 2057497057eeSRobert Watson return; 2058497057eeSRobert Watson 2059497057eeSRobert Watson case TCPS_LAST_ACK: 2060497057eeSRobert Watson db_printf("TCPS_LAST_ACK"); 2061497057eeSRobert Watson return; 2062497057eeSRobert Watson 2063497057eeSRobert Watson case TCPS_FIN_WAIT_2: 2064497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_2"); 2065497057eeSRobert Watson return; 2066497057eeSRobert Watson 2067497057eeSRobert Watson case TCPS_TIME_WAIT: 2068497057eeSRobert Watson db_printf("TCPS_TIME_WAIT"); 2069497057eeSRobert Watson return; 2070497057eeSRobert Watson 2071497057eeSRobert Watson default: 2072497057eeSRobert Watson db_printf("unknown"); 2073497057eeSRobert Watson return; 2074497057eeSRobert Watson } 2075497057eeSRobert Watson } 2076497057eeSRobert Watson 2077497057eeSRobert Watson static void 2078497057eeSRobert Watson db_print_tflags(u_int t_flags) 2079497057eeSRobert Watson { 2080497057eeSRobert Watson int comma; 2081497057eeSRobert Watson 2082497057eeSRobert Watson comma = 0; 2083497057eeSRobert Watson if (t_flags & TF_ACKNOW) { 2084497057eeSRobert Watson db_printf("%sTF_ACKNOW", comma ? ", " : ""); 2085497057eeSRobert Watson comma = 1; 2086497057eeSRobert Watson } 2087497057eeSRobert Watson if (t_flags & TF_DELACK) { 2088497057eeSRobert Watson db_printf("%sTF_DELACK", comma ? ", " : ""); 2089497057eeSRobert Watson comma = 1; 2090497057eeSRobert Watson } 2091497057eeSRobert Watson if (t_flags & TF_NODELAY) { 2092497057eeSRobert Watson db_printf("%sTF_NODELAY", comma ? ", " : ""); 2093497057eeSRobert Watson comma = 1; 2094497057eeSRobert Watson } 2095497057eeSRobert Watson if (t_flags & TF_NOOPT) { 2096497057eeSRobert Watson db_printf("%sTF_NOOPT", comma ? ", " : ""); 2097497057eeSRobert Watson comma = 1; 2098497057eeSRobert Watson } 2099497057eeSRobert Watson if (t_flags & TF_SENTFIN) { 2100497057eeSRobert Watson db_printf("%sTF_SENTFIN", comma ? ", " : ""); 2101497057eeSRobert Watson comma = 1; 2102497057eeSRobert Watson } 2103497057eeSRobert Watson if (t_flags & TF_REQ_SCALE) { 2104497057eeSRobert Watson db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 2105497057eeSRobert Watson comma = 1; 2106497057eeSRobert Watson } 2107497057eeSRobert Watson if (t_flags & TF_RCVD_SCALE) { 2108497057eeSRobert Watson db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 2109497057eeSRobert Watson comma = 1; 2110497057eeSRobert Watson } 2111497057eeSRobert Watson if (t_flags & TF_REQ_TSTMP) { 2112497057eeSRobert Watson db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 2113497057eeSRobert Watson comma = 1; 2114497057eeSRobert Watson } 2115497057eeSRobert Watson if (t_flags & TF_RCVD_TSTMP) { 2116497057eeSRobert Watson db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 2117497057eeSRobert Watson comma = 1; 2118497057eeSRobert Watson } 2119497057eeSRobert Watson if (t_flags & TF_SACK_PERMIT) { 2120497057eeSRobert Watson db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 2121497057eeSRobert Watson comma = 1; 2122497057eeSRobert Watson } 2123497057eeSRobert Watson if (t_flags & TF_NEEDSYN) { 2124497057eeSRobert Watson db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 2125497057eeSRobert Watson comma = 1; 2126497057eeSRobert Watson } 2127497057eeSRobert Watson if (t_flags & TF_NEEDFIN) { 2128497057eeSRobert Watson db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 2129497057eeSRobert Watson comma = 1; 2130497057eeSRobert Watson } 2131497057eeSRobert Watson if (t_flags & TF_NOPUSH) { 2132497057eeSRobert Watson db_printf("%sTF_NOPUSH", comma ? ", " : ""); 2133497057eeSRobert Watson comma = 1; 2134497057eeSRobert Watson } 2135497057eeSRobert Watson if (t_flags & TF_MORETOCOME) { 2136497057eeSRobert Watson db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 2137497057eeSRobert Watson comma = 1; 2138497057eeSRobert Watson } 2139497057eeSRobert Watson if (t_flags & TF_LQ_OVERFLOW) { 2140497057eeSRobert Watson db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : ""); 2141497057eeSRobert Watson comma = 1; 2142497057eeSRobert Watson } 2143497057eeSRobert Watson if (t_flags & TF_LASTIDLE) { 2144497057eeSRobert Watson db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 2145497057eeSRobert Watson comma = 1; 2146497057eeSRobert Watson } 2147497057eeSRobert Watson if (t_flags & TF_RXWIN0SENT) { 2148497057eeSRobert Watson db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 2149497057eeSRobert Watson comma = 1; 2150497057eeSRobert Watson } 2151497057eeSRobert Watson if (t_flags & TF_FASTRECOVERY) { 2152497057eeSRobert Watson db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 2153497057eeSRobert Watson comma = 1; 2154497057eeSRobert Watson } 2155dbc42409SLawrence Stewart if (t_flags & TF_CONGRECOVERY) { 2156dbc42409SLawrence Stewart db_printf("%sTF_CONGRECOVERY", comma ? ", " : ""); 2157dbc42409SLawrence Stewart comma = 1; 2158dbc42409SLawrence Stewart } 2159497057eeSRobert Watson if (t_flags & TF_WASFRECOVERY) { 2160497057eeSRobert Watson db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 2161497057eeSRobert Watson comma = 1; 2162497057eeSRobert Watson } 2163497057eeSRobert Watson if (t_flags & TF_SIGNATURE) { 2164497057eeSRobert Watson db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 2165497057eeSRobert Watson comma = 1; 2166497057eeSRobert Watson } 2167497057eeSRobert Watson if (t_flags & TF_FORCEDATA) { 2168497057eeSRobert Watson db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 2169497057eeSRobert Watson comma = 1; 2170497057eeSRobert Watson } 2171497057eeSRobert Watson if (t_flags & TF_TSO) { 2172497057eeSRobert Watson db_printf("%sTF_TSO", comma ? ", " : ""); 2173497057eeSRobert Watson comma = 1; 2174497057eeSRobert Watson } 2175f2512ba1SRui Paulo if (t_flags & TF_ECN_PERMIT) { 2176f2512ba1SRui Paulo db_printf("%sTF_ECN_PERMIT", comma ? ", " : ""); 2177f2512ba1SRui Paulo comma = 1; 2178f2512ba1SRui Paulo } 2179281a0fd4SPatrick Kelsey if (t_flags & TF_FASTOPEN) { 2180281a0fd4SPatrick Kelsey db_printf("%sTF_FASTOPEN", comma ? ", " : ""); 2181281a0fd4SPatrick Kelsey comma = 1; 2182281a0fd4SPatrick Kelsey } 2183497057eeSRobert Watson } 2184497057eeSRobert Watson 2185497057eeSRobert Watson static void 2186497057eeSRobert Watson db_print_toobflags(char t_oobflags) 2187497057eeSRobert Watson { 2188497057eeSRobert Watson int comma; 2189497057eeSRobert Watson 2190497057eeSRobert Watson comma = 0; 2191497057eeSRobert Watson if (t_oobflags & TCPOOB_HAVEDATA) { 2192497057eeSRobert Watson db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 2193497057eeSRobert Watson comma = 1; 2194497057eeSRobert Watson } 2195497057eeSRobert Watson if (t_oobflags & TCPOOB_HADDATA) { 2196497057eeSRobert Watson db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 2197497057eeSRobert Watson comma = 1; 2198497057eeSRobert Watson } 2199497057eeSRobert Watson } 2200497057eeSRobert Watson 2201497057eeSRobert Watson static void 2202497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 2203497057eeSRobert Watson { 2204497057eeSRobert Watson 2205497057eeSRobert Watson db_print_indent(indent); 2206497057eeSRobert Watson db_printf("%s at %p\n", name, tp); 2207497057eeSRobert Watson 2208497057eeSRobert Watson indent += 2; 2209497057eeSRobert Watson 2210497057eeSRobert Watson db_print_indent(indent); 2211497057eeSRobert Watson db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 22124741bfcbSPatrick Kelsey LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 2213497057eeSRobert Watson 2214497057eeSRobert Watson db_print_indent(indent); 221585d94372SRobert Watson db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n", 2216e2f2059fSMike Silbersack &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep); 2217497057eeSRobert Watson 2218497057eeSRobert Watson db_print_indent(indent); 2219e2f2059fSMike Silbersack db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl, 2220e2f2059fSMike Silbersack &tp->t_timers->tt_delack, tp->t_inpcb); 2221497057eeSRobert Watson 2222497057eeSRobert Watson db_print_indent(indent); 2223497057eeSRobert Watson db_printf("t_state: %d (", tp->t_state); 2224497057eeSRobert Watson db_print_tstate(tp->t_state); 2225497057eeSRobert Watson db_printf(")\n"); 2226497057eeSRobert Watson 2227497057eeSRobert Watson db_print_indent(indent); 2228497057eeSRobert Watson db_printf("t_flags: 0x%x (", tp->t_flags); 2229497057eeSRobert Watson db_print_tflags(tp->t_flags); 2230497057eeSRobert Watson db_printf(")\n"); 2231497057eeSRobert Watson 2232497057eeSRobert Watson db_print_indent(indent); 2233497057eeSRobert Watson db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n", 2234497057eeSRobert Watson tp->snd_una, tp->snd_max, tp->snd_nxt); 2235497057eeSRobert Watson 2236497057eeSRobert Watson db_print_indent(indent); 2237497057eeSRobert Watson db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 2238497057eeSRobert Watson tp->snd_up, tp->snd_wl1, tp->snd_wl2); 2239497057eeSRobert Watson 2240497057eeSRobert Watson db_print_indent(indent); 2241497057eeSRobert Watson db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 2242497057eeSRobert Watson tp->iss, tp->irs, tp->rcv_nxt); 2243497057eeSRobert Watson 2244497057eeSRobert Watson db_print_indent(indent); 2245*3ac12506SJonathan T. Looney db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n", 2246497057eeSRobert Watson tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 2247497057eeSRobert Watson 2248497057eeSRobert Watson db_print_indent(indent); 2249*3ac12506SJonathan T. Looney db_printf("snd_wnd: %u snd_cwnd: %u\n", 22501c18314dSAndre Oppermann tp->snd_wnd, tp->snd_cwnd); 2251497057eeSRobert Watson 2252497057eeSRobert Watson db_print_indent(indent); 2253*3ac12506SJonathan T. Looney db_printf("snd_ssthresh: %u snd_recover: " 22541c18314dSAndre Oppermann "0x%08x\n", tp->snd_ssthresh, tp->snd_recover); 2255497057eeSRobert Watson 2256497057eeSRobert Watson db_print_indent(indent); 22570c39d38dSGleb Smirnoff db_printf("t_rcvtime: %u t_startime: %u\n", 22580c39d38dSGleb Smirnoff tp->t_rcvtime, tp->t_starttime); 2259497057eeSRobert Watson 2260497057eeSRobert Watson db_print_indent(indent); 22611c18314dSAndre Oppermann db_printf("t_rttime: %u t_rtsq: 0x%08x\n", 22621c18314dSAndre Oppermann tp->t_rtttime, tp->t_rtseq); 2263497057eeSRobert Watson 2264497057eeSRobert Watson db_print_indent(indent); 22651c18314dSAndre Oppermann db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n", 22661c18314dSAndre Oppermann tp->t_rxtcur, tp->t_maxseg, tp->t_srtt); 2267497057eeSRobert Watson 2268497057eeSRobert Watson db_print_indent(indent); 2269497057eeSRobert Watson db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u " 2270497057eeSRobert Watson "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin, 2271497057eeSRobert Watson tp->t_rttbest); 2272497057eeSRobert Watson 2273497057eeSRobert Watson db_print_indent(indent); 2274*3ac12506SJonathan T. Looney db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n", 2275497057eeSRobert Watson tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 2276497057eeSRobert Watson 2277497057eeSRobert Watson db_print_indent(indent); 2278497057eeSRobert Watson db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 2279497057eeSRobert Watson db_print_toobflags(tp->t_oobflags); 2280497057eeSRobert Watson db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 2281497057eeSRobert Watson 2282497057eeSRobert Watson db_print_indent(indent); 2283497057eeSRobert Watson db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 2284497057eeSRobert Watson tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 2285497057eeSRobert Watson 2286497057eeSRobert Watson db_print_indent(indent); 22879f78a87aSJohn Baldwin db_printf("ts_recent: %u ts_recent_age: %u\n", 22881a553740SAndre Oppermann tp->ts_recent, tp->ts_recent_age); 2289497057eeSRobert Watson 2290497057eeSRobert Watson db_print_indent(indent); 2291497057eeSRobert Watson db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 2292*3ac12506SJonathan T. Looney "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 2293497057eeSRobert Watson 2294497057eeSRobert Watson db_print_indent(indent); 2295*3ac12506SJonathan T. Looney db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x " 22969f78a87aSJohn Baldwin "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 2297497057eeSRobert Watson tp->snd_recover_prev, tp->t_badrxtwin); 2298497057eeSRobert Watson 2299497057eeSRobert Watson db_print_indent(indent); 23003529149eSAndre Oppermann db_printf("snd_numholes: %d snd_holes first: %p\n", 23013529149eSAndre Oppermann tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 2302497057eeSRobert Watson 2303497057eeSRobert Watson db_print_indent(indent); 2304497057eeSRobert Watson db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: " 2305497057eeSRobert Watson "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata); 2306497057eeSRobert Watson 2307497057eeSRobert Watson /* Skip sackblks, sackhint. */ 2308497057eeSRobert Watson 2309497057eeSRobert Watson db_print_indent(indent); 2310497057eeSRobert Watson db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 2311497057eeSRobert Watson tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 2312497057eeSRobert Watson } 2313497057eeSRobert Watson 2314497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 2315497057eeSRobert Watson { 2316497057eeSRobert Watson struct tcpcb *tp; 2317497057eeSRobert Watson 2318497057eeSRobert Watson if (!have_addr) { 2319497057eeSRobert Watson db_printf("usage: show tcpcb <addr>\n"); 2320497057eeSRobert Watson return; 2321497057eeSRobert Watson } 2322497057eeSRobert Watson tp = (struct tcpcb *)addr; 2323497057eeSRobert Watson 2324497057eeSRobert Watson db_print_tcpcb(tp, "tcpcb", 0); 2325497057eeSRobert Watson } 2326497057eeSRobert Watson #endif 2327