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 72dbc42409SLawrence Stewart #include <netinet/cc.h> 73df8bae1dSRodney W. Grimes #include <netinet/in.h> 745d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h> 75df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 76b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h> 77b5e8ce9fSBruce Evans #include <netinet/in_var.h> 78df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 79fb59c426SYoshinobu Inoue #ifdef INET6 80b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h> 81b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h> 82fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 83a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h> 84fb59c426SYoshinobu Inoue #endif 85*281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 86*281a0fd4SPatrick Kelsey #include <netinet/tcp_fastopen.h> 87*281a0fd4SPatrick Kelsey #endif 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> 9386a996e6SHiren Panchasara #ifdef TCPPCAP 9486a996e6SHiren Panchasara #include <netinet/tcp_pcap.h> 9586a996e6SHiren Panchasara #endif 96610ee2f9SDavid Greenman #ifdef TCPDEBUG 97df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 98610ee2f9SDavid Greenman #endif 9909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 100bc65987aSKip Macy #include <netinet/tcp_offload.h> 10109fe6320SNavdeep Parhar #endif 102df8bae1dSRodney W. Grimes 103df8bae1dSRodney W. Grimes /* 104df8bae1dSRodney W. Grimes * TCP protocol interface to socket abstraction. 105df8bae1dSRodney W. Grimes */ 10656dc72c3SPawel Jakub Dawidek static int tcp_attach(struct socket *); 107b287c6c7SBjoern A. Zeeb #ifdef INET 1084d77a549SAlfred Perlstein static int tcp_connect(struct tcpcb *, struct sockaddr *, 1094d77a549SAlfred Perlstein struct thread *td); 110b287c6c7SBjoern A. Zeeb #endif /* INET */ 111fb59c426SYoshinobu Inoue #ifdef INET6 1124d77a549SAlfred Perlstein static int tcp6_connect(struct tcpcb *, struct sockaddr *, 1134d77a549SAlfred Perlstein struct thread *td); 114fb59c426SYoshinobu Inoue #endif /* INET6 */ 115623dce13SRobert Watson static void tcp_disconnect(struct tcpcb *); 116623dce13SRobert Watson static void tcp_usrclosed(struct tcpcb *); 117b8af5dfaSRobert Watson static void tcp_fill_info(struct tcpcb *, struct tcp_info *); 1182c37256eSGarrett Wollman 1192c37256eSGarrett Wollman #ifdef TCPDEBUG 1201db24ffbSJonathan Lemon #define TCPDEBUG0 int ostate = 0 1212c37256eSGarrett Wollman #define TCPDEBUG1() ostate = tp ? tp->t_state : 0 1224cc20ab1SSeigo Tanimura #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 1234cc20ab1SSeigo Tanimura tcp_trace(TA_USER, ostate, tp, 0, 0, req) 1242c37256eSGarrett Wollman #else 1252c37256eSGarrett Wollman #define TCPDEBUG0 1262c37256eSGarrett Wollman #define TCPDEBUG1() 1272c37256eSGarrett Wollman #define TCPDEBUG2(req) 1282c37256eSGarrett Wollman #endif 1292c37256eSGarrett Wollman 1302c37256eSGarrett Wollman /* 1312c37256eSGarrett Wollman * TCP attaches to socket via pru_attach(), reserving space, 1322c37256eSGarrett Wollman * and an internet control block. 1332c37256eSGarrett Wollman */ 1342c37256eSGarrett Wollman static int 135b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td) 1362c37256eSGarrett Wollman { 137f76fcf6dSJeffrey Hsu struct inpcb *inp; 138623dce13SRobert Watson struct tcpcb *tp = NULL; 139623dce13SRobert Watson int error; 1402c37256eSGarrett Wollman TCPDEBUG0; 1412c37256eSGarrett Wollman 142623dce13SRobert Watson inp = sotoinpcb(so); 143623dce13SRobert Watson KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL")); 1442c37256eSGarrett Wollman TCPDEBUG1(); 1452c37256eSGarrett Wollman 14656dc72c3SPawel Jakub Dawidek error = tcp_attach(so); 1472c37256eSGarrett Wollman if (error) 1482c37256eSGarrett Wollman goto out; 1492c37256eSGarrett Wollman 1502c37256eSGarrett Wollman if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1513879597fSAndrey A. Chernov so->so_linger = TCP_LINGERTIME; 152f76fcf6dSJeffrey Hsu 153f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 154f76fcf6dSJeffrey Hsu tp = intotcpcb(inp); 1552c37256eSGarrett Wollman out: 1562c37256eSGarrett Wollman TCPDEBUG2(PRU_ATTACH); 1575d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ATTACH); 1582c37256eSGarrett Wollman return error; 1592c37256eSGarrett Wollman } 1602c37256eSGarrett Wollman 1612c37256eSGarrett Wollman /* 162a152f8a3SRobert Watson * tcp_detach is called when the socket layer loses its final reference 163a152f8a3SRobert Watson * to the socket, be it a file descriptor reference, a reference from TCP, 164a152f8a3SRobert Watson * etc. At this point, there is only one case in which we will keep around 165a152f8a3SRobert Watson * inpcb state: time wait. 166c78cbc7bSRobert Watson * 167a152f8a3SRobert Watson * This function can probably be re-absorbed back into tcp_usr_detach() now 168a152f8a3SRobert Watson * that there is a single detach path. 1692c37256eSGarrett Wollman */ 170bc725eafSRobert Watson static void 171c78cbc7bSRobert Watson tcp_detach(struct socket *so, struct inpcb *inp) 1722c37256eSGarrett Wollman { 1732c37256eSGarrett Wollman struct tcpcb *tp; 1742c37256eSGarrett Wollman 175079672cbSJulien Charbon INP_INFO_LOCK_ASSERT(&V_tcbinfo); 1768501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 177623dce13SRobert Watson 178c78cbc7bSRobert Watson KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp")); 179c78cbc7bSRobert Watson KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so")); 180953b5606SRobert Watson 181a152f8a3SRobert Watson tp = intotcpcb(inp); 182a152f8a3SRobert Watson 183ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 184623dce13SRobert Watson /* 185a152f8a3SRobert Watson * There are two cases to handle: one in which the time wait 186a152f8a3SRobert Watson * state is being discarded (INP_DROPPED), and one in which 187a152f8a3SRobert Watson * this connection will remain in timewait. In the former, 188a152f8a3SRobert Watson * it is time to discard all state (except tcptw, which has 189a152f8a3SRobert Watson * already been discarded by the timewait close code, which 190a152f8a3SRobert Watson * should be further up the call stack somewhere). In the 191a152f8a3SRobert Watson * latter case, we detach from the socket, but leave the pcb 192a152f8a3SRobert Watson * present until timewait ends. 193623dce13SRobert Watson * 194a152f8a3SRobert Watson * XXXRW: Would it be cleaner to free the tcptw here? 195cea40c48SJulien Charbon * 196cea40c48SJulien Charbon * Astute question indeed, from twtcp perspective there are 197cea40c48SJulien Charbon * three cases to consider: 198cea40c48SJulien Charbon * 199cea40c48SJulien Charbon * #1 tcp_detach is called at tcptw creation time by 200cea40c48SJulien Charbon * tcp_twstart, then do not discard the newly created tcptw 201cea40c48SJulien Charbon * and leave inpcb present until timewait ends 202cea40c48SJulien Charbon * #2 tcp_detach is called at timewait end (or reuse) by 203cea40c48SJulien Charbon * tcp_twclose, then the tcptw has already been discarded 204ff9b006dSJulien Charbon * (or reused) and inpcb is freed here 205cea40c48SJulien Charbon * #3 tcp_detach is called() after timewait ends (or reuse) 206cea40c48SJulien Charbon * (e.g. by soclose), then tcptw has already been discarded 207ff9b006dSJulien Charbon * (or reused) and inpcb is freed here 208cea40c48SJulien Charbon * 209cea40c48SJulien Charbon * In all three cases the tcptw should not be freed here. 210623dce13SRobert Watson */ 211ad71fe3cSRobert Watson if (inp->inp_flags & INP_DROPPED) { 212a152f8a3SRobert Watson KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && " 213a152f8a3SRobert Watson "INP_DROPPED && tp != NULL")); 214623dce13SRobert Watson in_pcbdetach(inp); 2150206cdb8SBjoern A. Zeeb in_pcbfree(inp); 2160206cdb8SBjoern A. Zeeb } else { 217623dce13SRobert Watson in_pcbdetach(inp); 2188501a69cSRobert Watson INP_WUNLOCK(inp); 219623dce13SRobert Watson } 220623dce13SRobert Watson } else { 221e6e65783SRobert Watson /* 222a152f8a3SRobert Watson * If the connection is not in timewait, we consider two 223a152f8a3SRobert Watson * two conditions: one in which no further processing is 224a152f8a3SRobert Watson * necessary (dropped || embryonic), and one in which TCP is 225a152f8a3SRobert Watson * not yet done, but no longer requires the socket, so the 226a152f8a3SRobert Watson * pcb will persist for the time being. 227a152f8a3SRobert Watson * 228a152f8a3SRobert Watson * XXXRW: Does the second case still occur? 229e6e65783SRobert Watson */ 230ad71fe3cSRobert Watson if (inp->inp_flags & INP_DROPPED || 231623dce13SRobert Watson tp->t_state < TCPS_SYN_SENT) { 232623dce13SRobert Watson tcp_discardcb(tp); 233623dce13SRobert Watson in_pcbdetach(inp); 2340206cdb8SBjoern A. Zeeb in_pcbfree(inp); 235db3cee51SNavdeep Parhar } else { 236a152f8a3SRobert Watson in_pcbdetach(inp); 237db3cee51SNavdeep Parhar INP_WUNLOCK(inp); 238db3cee51SNavdeep Parhar } 239623dce13SRobert Watson } 240623dce13SRobert Watson } 241c78cbc7bSRobert Watson 242c78cbc7bSRobert Watson /* 243c78cbc7bSRobert Watson * pru_detach() detaches the TCP protocol from the socket. 244c78cbc7bSRobert Watson * If the protocol state is non-embryonic, then can't 245c78cbc7bSRobert Watson * do this directly: have to initiate a pru_disconnect(), 246c78cbc7bSRobert Watson * which may finish later; embryonic TCB's can just 247c78cbc7bSRobert Watson * be discarded here. 248c78cbc7bSRobert Watson */ 249c78cbc7bSRobert Watson static void 250c78cbc7bSRobert Watson tcp_usr_detach(struct socket *so) 251c78cbc7bSRobert Watson { 252c78cbc7bSRobert Watson struct inpcb *inp; 253079672cbSJulien Charbon int rlock = 0; 254c78cbc7bSRobert Watson 255c78cbc7bSRobert Watson inp = sotoinpcb(so); 256c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL")); 257079672cbSJulien Charbon if (!INP_INFO_WLOCKED(&V_tcbinfo)) { 258ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 259079672cbSJulien Charbon rlock = 1; 260079672cbSJulien Charbon } 2618501a69cSRobert Watson INP_WLOCK(inp); 262c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 263c78cbc7bSRobert Watson ("tcp_usr_detach: inp_socket == NULL")); 264c78cbc7bSRobert Watson tcp_detach(so, inp); 265079672cbSJulien Charbon if (rlock) 266ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 2672c37256eSGarrett Wollman } 2682c37256eSGarrett Wollman 269b287c6c7SBjoern A. Zeeb #ifdef INET 2702c37256eSGarrett Wollman /* 2712c37256eSGarrett Wollman * Give the socket an address. 2722c37256eSGarrett Wollman */ 2732c37256eSGarrett Wollman static int 274b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 2752c37256eSGarrett Wollman { 2762c37256eSGarrett Wollman int error = 0; 277f76fcf6dSJeffrey Hsu struct inpcb *inp; 278623dce13SRobert Watson struct tcpcb *tp = NULL; 2792c37256eSGarrett Wollman struct sockaddr_in *sinp; 2802c37256eSGarrett Wollman 28152710de1SPawel Jakub Dawidek sinp = (struct sockaddr_in *)nam; 28252710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof (*sinp)) 28352710de1SPawel Jakub Dawidek return (EINVAL); 2842c37256eSGarrett Wollman /* 2852c37256eSGarrett Wollman * Must check for multicast addresses and disallow binding 2862c37256eSGarrett Wollman * to them. 2872c37256eSGarrett Wollman */ 2882c37256eSGarrett Wollman if (sinp->sin_family == AF_INET && 28952710de1SPawel Jakub Dawidek IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 29052710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 29152710de1SPawel Jakub Dawidek 292623dce13SRobert Watson TCPDEBUG0; 293623dce13SRobert Watson inp = sotoinpcb(so); 294623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 2958501a69cSRobert Watson INP_WLOCK(inp); 296ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 297623dce13SRobert Watson error = EINVAL; 2982c37256eSGarrett Wollman goto out; 299623dce13SRobert Watson } 300623dce13SRobert Watson tp = intotcpcb(inp); 301623dce13SRobert Watson TCPDEBUG1(); 302fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 303623dce13SRobert Watson error = in_pcbbind(inp, nam, td->td_ucred); 304fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 305623dce13SRobert Watson out: 306623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 3075d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 3088501a69cSRobert Watson INP_WUNLOCK(inp); 309623dce13SRobert Watson 310623dce13SRobert Watson return (error); 3112c37256eSGarrett Wollman } 312b287c6c7SBjoern A. Zeeb #endif /* INET */ 3132c37256eSGarrett Wollman 314fb59c426SYoshinobu Inoue #ifdef INET6 315fb59c426SYoshinobu Inoue static int 316b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 317fb59c426SYoshinobu Inoue { 318fb59c426SYoshinobu Inoue int error = 0; 319f76fcf6dSJeffrey Hsu struct inpcb *inp; 320623dce13SRobert Watson struct tcpcb *tp = NULL; 321fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 322fb59c426SYoshinobu Inoue 32352710de1SPawel Jakub Dawidek sin6p = (struct sockaddr_in6 *)nam; 32452710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof (*sin6p)) 32552710de1SPawel Jakub Dawidek return (EINVAL); 326fb59c426SYoshinobu Inoue /* 327fb59c426SYoshinobu Inoue * Must check for multicast addresses and disallow binding 328fb59c426SYoshinobu Inoue * to them. 329fb59c426SYoshinobu Inoue */ 330fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 && 33152710de1SPawel Jakub Dawidek IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 33252710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 33352710de1SPawel Jakub Dawidek 334623dce13SRobert Watson TCPDEBUG0; 335623dce13SRobert Watson inp = sotoinpcb(so); 336623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 3378501a69cSRobert Watson INP_WLOCK(inp); 338ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 339623dce13SRobert Watson error = EINVAL; 340623dce13SRobert Watson goto out; 341623dce13SRobert Watson } 342623dce13SRobert Watson tp = intotcpcb(inp); 343623dce13SRobert Watson TCPDEBUG1(); 344fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 345fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 346fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 347b287c6c7SBjoern A. Zeeb #ifdef INET 34866ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 349fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) 350fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 351fb59c426SYoshinobu Inoue else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 352fb59c426SYoshinobu Inoue struct sockaddr_in sin; 353fb59c426SYoshinobu Inoue 354fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 355fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 356fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 357b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)&sin, 358b0330ed9SPawel Jakub Dawidek td->td_ucred); 359fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 360fb59c426SYoshinobu Inoue goto out; 361fb59c426SYoshinobu Inoue } 362fb59c426SYoshinobu Inoue } 363b287c6c7SBjoern A. Zeeb #endif 364b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, nam, td->td_ucred); 365fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 366623dce13SRobert Watson out: 367623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 3685d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 3698501a69cSRobert Watson INP_WUNLOCK(inp); 370623dce13SRobert Watson return (error); 371fb59c426SYoshinobu Inoue } 372fb59c426SYoshinobu Inoue #endif /* INET6 */ 373fb59c426SYoshinobu Inoue 374b287c6c7SBjoern A. Zeeb #ifdef INET 3752c37256eSGarrett Wollman /* 3762c37256eSGarrett Wollman * Prepare to accept connections. 3772c37256eSGarrett Wollman */ 3782c37256eSGarrett Wollman static int 379d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td) 3802c37256eSGarrett Wollman { 3812c37256eSGarrett Wollman int error = 0; 382f76fcf6dSJeffrey Hsu struct inpcb *inp; 383623dce13SRobert Watson struct tcpcb *tp = NULL; 3842c37256eSGarrett Wollman 385623dce13SRobert Watson TCPDEBUG0; 386623dce13SRobert Watson inp = sotoinpcb(so); 387623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 3888501a69cSRobert Watson INP_WLOCK(inp); 389ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 390623dce13SRobert Watson error = EINVAL; 391623dce13SRobert Watson goto out; 392623dce13SRobert Watson } 393623dce13SRobert Watson tp = intotcpcb(inp); 394623dce13SRobert Watson TCPDEBUG1(); 3950daccb9cSRobert Watson SOCK_LOCK(so); 3960daccb9cSRobert Watson error = solisten_proto_check(so); 397fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 3980daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) 399b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 400fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 4010daccb9cSRobert Watson if (error == 0) { 40257f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 403d374e81eSRobert Watson solisten_proto(so, backlog); 40409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 40537cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 40609fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 40709fe6320SNavdeep Parhar #endif 4080daccb9cSRobert Watson } 4090daccb9cSRobert Watson SOCK_UNLOCK(so); 410623dce13SRobert Watson 411*281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 412*281a0fd4SPatrick Kelsey if (tp->t_flags & TF_FASTOPEN) 413*281a0fd4SPatrick Kelsey tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 414*281a0fd4SPatrick Kelsey #endif 415623dce13SRobert Watson out: 416623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 4175d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 4188501a69cSRobert Watson INP_WUNLOCK(inp); 419623dce13SRobert Watson return (error); 4202c37256eSGarrett Wollman } 421b287c6c7SBjoern A. Zeeb #endif /* INET */ 4222c37256eSGarrett Wollman 423fb59c426SYoshinobu Inoue #ifdef INET6 424fb59c426SYoshinobu Inoue static int 425d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 426fb59c426SYoshinobu Inoue { 427fb59c426SYoshinobu Inoue int error = 0; 428f76fcf6dSJeffrey Hsu struct inpcb *inp; 429623dce13SRobert Watson struct tcpcb *tp = NULL; 430fb59c426SYoshinobu Inoue 431623dce13SRobert Watson TCPDEBUG0; 432623dce13SRobert Watson inp = sotoinpcb(so); 433623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL")); 4348501a69cSRobert Watson INP_WLOCK(inp); 435ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 436623dce13SRobert Watson error = EINVAL; 437623dce13SRobert Watson goto out; 438623dce13SRobert Watson } 439623dce13SRobert Watson tp = intotcpcb(inp); 440623dce13SRobert Watson TCPDEBUG1(); 4410daccb9cSRobert Watson SOCK_LOCK(so); 4420daccb9cSRobert Watson error = solisten_proto_check(so); 443fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 4440daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) { 445fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 44666ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 447fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 448b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 449fb59c426SYoshinobu Inoue } 450fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 4510daccb9cSRobert Watson if (error == 0) { 45257f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 453d374e81eSRobert Watson solisten_proto(so, backlog); 45409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 45537cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 45609fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 45709fe6320SNavdeep Parhar #endif 4580daccb9cSRobert Watson } 4590daccb9cSRobert Watson SOCK_UNLOCK(so); 460623dce13SRobert Watson 461*281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 462*281a0fd4SPatrick Kelsey if (tp->t_flags & TF_FASTOPEN) 463*281a0fd4SPatrick Kelsey tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 464*281a0fd4SPatrick Kelsey #endif 465623dce13SRobert Watson out: 466623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 4675d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 4688501a69cSRobert Watson INP_WUNLOCK(inp); 469623dce13SRobert Watson return (error); 470fb59c426SYoshinobu Inoue } 471fb59c426SYoshinobu Inoue #endif /* INET6 */ 472fb59c426SYoshinobu Inoue 473b287c6c7SBjoern A. Zeeb #ifdef INET 4742c37256eSGarrett Wollman /* 4752c37256eSGarrett Wollman * Initiate connection to peer. 4762c37256eSGarrett Wollman * Create a template for use in transmissions on this connection. 4772c37256eSGarrett Wollman * Enter SYN_SENT state, and mark socket as connecting. 4782c37256eSGarrett Wollman * Start keep-alive timer, and seed output sequence space. 4792c37256eSGarrett Wollman * Send initial segment on connection. 4802c37256eSGarrett Wollman */ 4812c37256eSGarrett Wollman static int 482b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 4832c37256eSGarrett Wollman { 4842c37256eSGarrett Wollman int error = 0; 485f76fcf6dSJeffrey Hsu struct inpcb *inp; 486623dce13SRobert Watson struct tcpcb *tp = NULL; 4872c37256eSGarrett Wollman struct sockaddr_in *sinp; 4882c37256eSGarrett Wollman 48957bf258eSGarrett Wollman sinp = (struct sockaddr_in *)nam; 490e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sinp)) 491e29ef13fSDon Lewis return (EINVAL); 49252710de1SPawel Jakub Dawidek /* 49352710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 49452710de1SPawel Jakub Dawidek */ 4952c37256eSGarrett Wollman if (sinp->sin_family == AF_INET 49652710de1SPawel Jakub Dawidek && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 49752710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 498b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0) 499b89e82ddSJamie Gritton return (error); 50075c13541SPoul-Henning Kamp 501623dce13SRobert Watson TCPDEBUG0; 502623dce13SRobert Watson inp = sotoinpcb(so); 503623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 5048501a69cSRobert Watson INP_WLOCK(inp); 505eb96dc33SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) { 506eb96dc33SJulien Charbon error = EADDRINUSE; 507eb96dc33SJulien Charbon goto out; 508eb96dc33SJulien Charbon } 509eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 510eb96dc33SJulien Charbon error = ECONNREFUSED; 511623dce13SRobert Watson goto out; 512623dce13SRobert Watson } 513623dce13SRobert Watson tp = intotcpcb(inp); 514623dce13SRobert Watson TCPDEBUG1(); 515b40ce416SJulian Elischer if ((error = tcp_connect(tp, nam, td)) != 0) 5162c37256eSGarrett Wollman goto out; 51709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 51809fe6320SNavdeep Parhar if (registered_toedevs > 0 && 51937cc0ecbSNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 52009fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 52109fe6320SNavdeep Parhar goto out; 52209fe6320SNavdeep Parhar #endif 52309fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 52455bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 525623dce13SRobert Watson out: 526623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 5278501a69cSRobert Watson INP_WUNLOCK(inp); 528623dce13SRobert Watson return (error); 5292c37256eSGarrett Wollman } 530b287c6c7SBjoern A. Zeeb #endif /* INET */ 5312c37256eSGarrett Wollman 532fb59c426SYoshinobu Inoue #ifdef INET6 533fb59c426SYoshinobu Inoue static int 534b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 535fb59c426SYoshinobu Inoue { 536fb59c426SYoshinobu Inoue int error = 0; 537f76fcf6dSJeffrey Hsu struct inpcb *inp; 538623dce13SRobert Watson struct tcpcb *tp = NULL; 539fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 540623dce13SRobert Watson 541623dce13SRobert Watson TCPDEBUG0; 542fb59c426SYoshinobu Inoue 543fb59c426SYoshinobu Inoue sin6p = (struct sockaddr_in6 *)nam; 544e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sin6p)) 545e29ef13fSDon Lewis return (EINVAL); 54652710de1SPawel Jakub Dawidek /* 54752710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 54852710de1SPawel Jakub Dawidek */ 549fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 55052710de1SPawel Jakub Dawidek && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 55152710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 552fb59c426SYoshinobu Inoue 553623dce13SRobert Watson inp = sotoinpcb(so); 554623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 5558501a69cSRobert Watson INP_WLOCK(inp); 556eb96dc33SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) { 557eb96dc33SJulien Charbon error = EADDRINUSE; 558eb96dc33SJulien Charbon goto out; 559eb96dc33SJulien Charbon } 560eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 561eb96dc33SJulien Charbon error = ECONNREFUSED; 562623dce13SRobert Watson goto out; 563623dce13SRobert Watson } 564623dce13SRobert Watson tp = intotcpcb(inp); 565623dce13SRobert Watson TCPDEBUG1(); 566b287c6c7SBjoern A. Zeeb #ifdef INET 567fa046d87SRobert Watson /* 568fa046d87SRobert Watson * XXXRW: Some confusion: V4/V6 flags relate to binding, and 569fa046d87SRobert Watson * therefore probably require the hash lock, which isn't held here. 570fa046d87SRobert Watson * Is this a significant problem? 571fa046d87SRobert Watson */ 57233841545SHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 573fb59c426SYoshinobu Inoue struct sockaddr_in sin; 574fb59c426SYoshinobu Inoue 575d46a5312SMaxim Konovalov if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 576d46a5312SMaxim Konovalov error = EINVAL; 577d46a5312SMaxim Konovalov goto out; 578d46a5312SMaxim Konovalov } 57933841545SHajimu UMEMOTO 580fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 581fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 582fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 583b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, 584b89e82ddSJamie Gritton &sin.sin_addr)) != 0) 585413628a7SBjoern A. Zeeb goto out; 586b40ce416SJulian Elischer if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 587fb59c426SYoshinobu Inoue goto out; 58809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 58909fe6320SNavdeep Parhar if (registered_toedevs > 0 && 590adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 59109fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 59209fe6320SNavdeep Parhar goto out; 59309fe6320SNavdeep Parhar #endif 59455bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 595fb59c426SYoshinobu Inoue goto out; 596fb59c426SYoshinobu Inoue } 597b287c6c7SBjoern A. Zeeb #endif 598fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 599fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 600dcdb4371SBjoern A. Zeeb inp->inp_inc.inc_flags |= INC_ISIPV6; 601b89e82ddSJamie Gritton if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0) 602413628a7SBjoern A. Zeeb goto out; 603b40ce416SJulian Elischer if ((error = tcp6_connect(tp, nam, td)) != 0) 604fb59c426SYoshinobu Inoue goto out; 60509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 60609fe6320SNavdeep Parhar if (registered_toedevs > 0 && 607adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 60809fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 60909fe6320SNavdeep Parhar goto out; 61009fe6320SNavdeep Parhar #endif 61109fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 61255bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 613623dce13SRobert Watson 614623dce13SRobert Watson out: 615623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 6165d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CONNECT); 6178501a69cSRobert Watson INP_WUNLOCK(inp); 618623dce13SRobert Watson return (error); 619fb59c426SYoshinobu Inoue } 620fb59c426SYoshinobu Inoue #endif /* INET6 */ 621fb59c426SYoshinobu Inoue 6222c37256eSGarrett Wollman /* 6232c37256eSGarrett Wollman * Initiate disconnect from peer. 6242c37256eSGarrett Wollman * If connection never passed embryonic stage, just drop; 6252c37256eSGarrett Wollman * else if don't need to let data drain, then can just drop anyways, 6262c37256eSGarrett Wollman * else have to begin TCP shutdown process: mark socket disconnecting, 6272c37256eSGarrett Wollman * drain unread data, state switch to reflect user close, and 6282c37256eSGarrett Wollman * send segment (e.g. FIN) to peer. Socket will be really disconnected 6292c37256eSGarrett Wollman * when peer sends FIN and acks ours. 6302c37256eSGarrett Wollman * 6312c37256eSGarrett Wollman * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 6322c37256eSGarrett Wollman */ 6332c37256eSGarrett Wollman static int 6342c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so) 6352c37256eSGarrett Wollman { 636f76fcf6dSJeffrey Hsu struct inpcb *inp; 637623dce13SRobert Watson struct tcpcb *tp = NULL; 638623dce13SRobert Watson int error = 0; 6392c37256eSGarrett Wollman 640623dce13SRobert Watson TCPDEBUG0; 641ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 642623dce13SRobert Watson inp = sotoinpcb(so); 643623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 6448501a69cSRobert Watson INP_WLOCK(inp); 645489dcc92SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) 646489dcc92SJulien Charbon goto out; 647489dcc92SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 64821367f63SSam Leffler error = ECONNRESET; 649623dce13SRobert Watson goto out; 650623dce13SRobert Watson } 651623dce13SRobert Watson tp = intotcpcb(inp); 652623dce13SRobert Watson TCPDEBUG1(); 653623dce13SRobert Watson tcp_disconnect(tp); 654623dce13SRobert Watson out: 655623dce13SRobert Watson TCPDEBUG2(PRU_DISCONNECT); 6565d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_DISCONNECT); 6578501a69cSRobert Watson INP_WUNLOCK(inp); 658ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 659623dce13SRobert Watson return (error); 6602c37256eSGarrett Wollman } 6612c37256eSGarrett Wollman 662b287c6c7SBjoern A. Zeeb #ifdef INET 6632c37256eSGarrett Wollman /* 6648296cddfSRobert Watson * Accept a connection. Essentially all the work is done at higher levels; 6658296cddfSRobert Watson * just return the address of the peer, storing through addr. 6662c37256eSGarrett Wollman */ 6672c37256eSGarrett Wollman static int 66857bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam) 6692c37256eSGarrett Wollman { 6702c37256eSGarrett Wollman int error = 0; 671f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 6721db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 67326ef6ac4SDon Lewis struct in_addr addr; 67426ef6ac4SDon Lewis in_port_t port = 0; 6751db24ffbSJonathan Lemon TCPDEBUG0; 6762c37256eSGarrett Wollman 6773d2d3ef4SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 6783d2d3ef4SRobert Watson return (ECONNABORTED); 679f76fcf6dSJeffrey Hsu 680f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 681623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 6828501a69cSRobert Watson INP_WLOCK(inp); 683ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 6843d2d3ef4SRobert Watson error = ECONNABORTED; 685623dce13SRobert Watson goto out; 686623dce13SRobert Watson } 6871db24ffbSJonathan Lemon tp = intotcpcb(inp); 6881db24ffbSJonathan Lemon TCPDEBUG1(); 689f76fcf6dSJeffrey Hsu 690f76fcf6dSJeffrey Hsu /* 69154d642bbSRobert Watson * We inline in_getpeeraddr and COMMON_END here, so that we can 69226ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 69326ef6ac4SDon Lewis * release the lock. 694f76fcf6dSJeffrey Hsu */ 69526ef6ac4SDon Lewis port = inp->inp_fport; 69626ef6ac4SDon Lewis addr = inp->inp_faddr; 697f76fcf6dSJeffrey Hsu 698623dce13SRobert Watson out: 699623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 7005d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 7018501a69cSRobert Watson INP_WUNLOCK(inp); 70226ef6ac4SDon Lewis if (error == 0) 70326ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 70426ef6ac4SDon Lewis return error; 7052c37256eSGarrett Wollman } 706b287c6c7SBjoern A. Zeeb #endif /* INET */ 7072c37256eSGarrett Wollman 708fb59c426SYoshinobu Inoue #ifdef INET6 709fb59c426SYoshinobu Inoue static int 710fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 711fb59c426SYoshinobu Inoue { 712f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 713fb59c426SYoshinobu Inoue int error = 0; 7141db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 71526ef6ac4SDon Lewis struct in_addr addr; 71626ef6ac4SDon Lewis struct in6_addr addr6; 71726ef6ac4SDon Lewis in_port_t port = 0; 71826ef6ac4SDon Lewis int v4 = 0; 7191db24ffbSJonathan Lemon TCPDEBUG0; 720fb59c426SYoshinobu Inoue 721b4470c16SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 722b4470c16SRobert Watson return (ECONNABORTED); 723f76fcf6dSJeffrey Hsu 724f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 725623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 726fa046d87SRobert Watson INP_INFO_RLOCK(&V_tcbinfo); 7278501a69cSRobert Watson INP_WLOCK(inp); 728ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 72921367f63SSam Leffler error = ECONNABORTED; 730623dce13SRobert Watson goto out; 731623dce13SRobert Watson } 7321db24ffbSJonathan Lemon tp = intotcpcb(inp); 7331db24ffbSJonathan Lemon TCPDEBUG1(); 734623dce13SRobert Watson 73526ef6ac4SDon Lewis /* 73626ef6ac4SDon Lewis * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 73726ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 73826ef6ac4SDon Lewis * release the lock. 73926ef6ac4SDon Lewis */ 74026ef6ac4SDon Lewis if (inp->inp_vflag & INP_IPV4) { 74126ef6ac4SDon Lewis v4 = 1; 74226ef6ac4SDon Lewis port = inp->inp_fport; 74326ef6ac4SDon Lewis addr = inp->inp_faddr; 74426ef6ac4SDon Lewis } else { 74526ef6ac4SDon Lewis port = inp->inp_fport; 74626ef6ac4SDon Lewis addr6 = inp->in6p_faddr; 74726ef6ac4SDon Lewis } 74826ef6ac4SDon Lewis 749623dce13SRobert Watson out: 750623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 7515d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 7528501a69cSRobert Watson INP_WUNLOCK(inp); 753fa046d87SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 75426ef6ac4SDon Lewis if (error == 0) { 75526ef6ac4SDon Lewis if (v4) 75626ef6ac4SDon Lewis *nam = in6_v4mapsin6_sockaddr(port, &addr); 75726ef6ac4SDon Lewis else 75826ef6ac4SDon Lewis *nam = in6_sockaddr(port, &addr6); 75926ef6ac4SDon Lewis } 76026ef6ac4SDon Lewis return error; 761fb59c426SYoshinobu Inoue } 762fb59c426SYoshinobu Inoue #endif /* INET6 */ 763f76fcf6dSJeffrey Hsu 764f76fcf6dSJeffrey Hsu /* 7652c37256eSGarrett Wollman * Mark the connection as being incapable of further output. 7662c37256eSGarrett Wollman */ 7672c37256eSGarrett Wollman static int 7682c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so) 7692c37256eSGarrett Wollman { 7702c37256eSGarrett Wollman int error = 0; 771f76fcf6dSJeffrey Hsu struct inpcb *inp; 772623dce13SRobert Watson struct tcpcb *tp = NULL; 7732c37256eSGarrett Wollman 774623dce13SRobert Watson TCPDEBUG0; 775ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 776623dce13SRobert Watson inp = sotoinpcb(so); 777623dce13SRobert Watson KASSERT(inp != NULL, ("inp == NULL")); 7788501a69cSRobert Watson INP_WLOCK(inp); 779ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 78021367f63SSam Leffler error = ECONNRESET; 781623dce13SRobert Watson goto out; 782623dce13SRobert Watson } 783623dce13SRobert Watson tp = intotcpcb(inp); 784623dce13SRobert Watson TCPDEBUG1(); 7852c37256eSGarrett Wollman socantsendmore(so); 786623dce13SRobert Watson tcp_usrclosed(tp); 787ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 78855bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 789623dce13SRobert Watson 790623dce13SRobert Watson out: 791623dce13SRobert Watson TCPDEBUG2(PRU_SHUTDOWN); 7925d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN); 7938501a69cSRobert Watson INP_WUNLOCK(inp); 794ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 795623dce13SRobert Watson 796623dce13SRobert Watson return (error); 7972c37256eSGarrett Wollman } 7982c37256eSGarrett Wollman 7992c37256eSGarrett Wollman /* 8002c37256eSGarrett Wollman * After a receive, possibly send window update to peer. 8012c37256eSGarrett Wollman */ 8022c37256eSGarrett Wollman static int 8032c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags) 8042c37256eSGarrett Wollman { 805f76fcf6dSJeffrey Hsu struct inpcb *inp; 806623dce13SRobert Watson struct tcpcb *tp = NULL; 807623dce13SRobert Watson int error = 0; 8082c37256eSGarrett Wollman 809623dce13SRobert Watson TCPDEBUG0; 810623dce13SRobert Watson inp = sotoinpcb(so); 811623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 8128501a69cSRobert Watson INP_WLOCK(inp); 813ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 81421367f63SSam Leffler error = ECONNRESET; 815623dce13SRobert Watson goto out; 816623dce13SRobert Watson } 817623dce13SRobert Watson tp = intotcpcb(inp); 818623dce13SRobert Watson TCPDEBUG1(); 819*281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 820*281a0fd4SPatrick Kelsey /* 821*281a0fd4SPatrick Kelsey * For passively-created TFO connections, don't attempt a window 822*281a0fd4SPatrick Kelsey * update while still in SYN_RECEIVED as this may trigger an early 823*281a0fd4SPatrick Kelsey * SYN|ACK. It is preferable to have the SYN|ACK be sent along with 824*281a0fd4SPatrick Kelsey * application response data, or failing that, when the DELACK timer 825*281a0fd4SPatrick Kelsey * expires. 826*281a0fd4SPatrick Kelsey */ 827*281a0fd4SPatrick Kelsey if ((tp->t_flags & TF_FASTOPEN) && 828*281a0fd4SPatrick Kelsey (tp->t_state == TCPS_SYN_RECEIVED)) 829*281a0fd4SPatrick Kelsey goto out; 830*281a0fd4SPatrick Kelsey #endif 83109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 83209fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) 83309fe6320SNavdeep Parhar tcp_offload_rcvd(tp); 834460cf046SNavdeep Parhar else 83509fe6320SNavdeep Parhar #endif 83655bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 837623dce13SRobert Watson 838623dce13SRobert Watson out: 839623dce13SRobert Watson TCPDEBUG2(PRU_RCVD); 8405d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVD); 8418501a69cSRobert Watson INP_WUNLOCK(inp); 842623dce13SRobert Watson return (error); 8432c37256eSGarrett Wollman } 8442c37256eSGarrett Wollman 8452c37256eSGarrett Wollman /* 8462c37256eSGarrett Wollman * Do a send by putting data in output queue and updating urgent 8479c9906e9SPeter Wemm * marker if URG set. Possibly send more data. Unlike the other 8489c9906e9SPeter Wemm * pru_*() routines, the mbuf chains are our responsibility. We 8499c9906e9SPeter Wemm * must either enqueue them or free them. The other pru_* routines 8509c9906e9SPeter Wemm * generally are caller-frees. 8512c37256eSGarrett Wollman */ 8522c37256eSGarrett Wollman static int 85357bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 854b40ce416SJulian Elischer struct sockaddr *nam, struct mbuf *control, struct thread *td) 8552c37256eSGarrett Wollman { 8562c37256eSGarrett Wollman int error = 0; 857f76fcf6dSJeffrey Hsu struct inpcb *inp; 858623dce13SRobert Watson struct tcpcb *tp = NULL; 859fb59c426SYoshinobu Inoue #ifdef INET6 860fb59c426SYoshinobu Inoue int isipv6; 861fb59c426SYoshinobu Inoue #endif 8629c9906e9SPeter Wemm TCPDEBUG0; 8632c37256eSGarrett Wollman 864f76fcf6dSJeffrey Hsu /* 865fa046d87SRobert Watson * We require the pcbinfo lock if we will close the socket as part of 866fa046d87SRobert Watson * this call. 867f76fcf6dSJeffrey Hsu */ 868fa046d87SRobert Watson if (flags & PRUS_EOF) 869ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 870f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 871623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 8728501a69cSRobert Watson INP_WLOCK(inp); 873ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 8747ff0b850SAndre Oppermann if (control) 8757ff0b850SAndre Oppermann m_freem(control); 8762cbcd3c1SGleb Smirnoff /* 8772cbcd3c1SGleb Smirnoff * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible 8782cbcd3c1SGleb Smirnoff * for freeing memory. 8792cbcd3c1SGleb Smirnoff */ 8802cbcd3c1SGleb Smirnoff if (m && (flags & PRUS_NOTREADY) == 0) 8817ff0b850SAndre Oppermann m_freem(m); 88221367f63SSam Leffler error = ECONNRESET; 8839c9906e9SPeter Wemm goto out; 8849c9906e9SPeter Wemm } 885fb59c426SYoshinobu Inoue #ifdef INET6 886fb59c426SYoshinobu Inoue isipv6 = nam && nam->sa_family == AF_INET6; 887fb59c426SYoshinobu Inoue #endif /* INET6 */ 8889c9906e9SPeter Wemm tp = intotcpcb(inp); 8899c9906e9SPeter Wemm TCPDEBUG1(); 8909c9906e9SPeter Wemm if (control) { 8919c9906e9SPeter Wemm /* TCP doesn't do control messages (rights, creds, etc) */ 8929c9906e9SPeter Wemm if (control->m_len) { 8939c9906e9SPeter Wemm m_freem(control); 8942c37256eSGarrett Wollman if (m) 8952c37256eSGarrett Wollman m_freem(m); 896744f87eaSDavid Greenman error = EINVAL; 897744f87eaSDavid Greenman goto out; 8982c37256eSGarrett Wollman } 8999c9906e9SPeter Wemm m_freem(control); /* empty control, just free it */ 9009c9906e9SPeter Wemm } 9012c37256eSGarrett Wollman if (!(flags & PRUS_OOB)) { 902651e4e6aSGleb Smirnoff sbappendstream(&so->so_snd, m, flags); 9032c37256eSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 9042c37256eSGarrett Wollman /* 9052c37256eSGarrett Wollman * Do implied connect if not yet connected, 9062c37256eSGarrett Wollman * initialize window to default value, and 9072c37256eSGarrett Wollman * initialize maxseg/maxopd using peer's cached 9082c37256eSGarrett Wollman * MSS. 9092c37256eSGarrett Wollman */ 910fb59c426SYoshinobu Inoue #ifdef INET6 911fb59c426SYoshinobu Inoue if (isipv6) 912b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 913fb59c426SYoshinobu Inoue #endif /* INET6 */ 914b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 915b287c6c7SBjoern A. Zeeb else 916b287c6c7SBjoern A. Zeeb #endif 917b287c6c7SBjoern A. Zeeb #ifdef INET 918b40ce416SJulian Elischer error = tcp_connect(tp, nam, td); 919b287c6c7SBjoern A. Zeeb #endif 9202c37256eSGarrett Wollman if (error) 9212c37256eSGarrett Wollman goto out; 9222c37256eSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 9232c37256eSGarrett Wollman tcp_mss(tp, -1); 9242c37256eSGarrett Wollman } 9252c37256eSGarrett Wollman if (flags & PRUS_EOF) { 9262c37256eSGarrett Wollman /* 9272c37256eSGarrett Wollman * Close the send side of the connection after 9282c37256eSGarrett Wollman * the data is sent. 9292c37256eSGarrett Wollman */ 930ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 9312c37256eSGarrett Wollman socantsendmore(so); 932623dce13SRobert Watson tcp_usrclosed(tp); 9332c37256eSGarrett Wollman } 9342cbcd3c1SGleb Smirnoff if (!(inp->inp_flags & INP_DROPPED) && 9352cbcd3c1SGleb Smirnoff !(flags & PRUS_NOTREADY)) { 936b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 937b0acefa8SBill Fenner tp->t_flags |= TF_MORETOCOME; 93855bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 939b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 940b0acefa8SBill Fenner tp->t_flags &= ~TF_MORETOCOME; 941b0acefa8SBill Fenner } 9422c37256eSGarrett Wollman } else { 943623dce13SRobert Watson /* 944623dce13SRobert Watson * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 945623dce13SRobert Watson */ 946d2bc35abSRobert Watson SOCKBUF_LOCK(&so->so_snd); 9472c37256eSGarrett Wollman if (sbspace(&so->so_snd) < -512) { 948d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 9492c37256eSGarrett Wollman m_freem(m); 9502c37256eSGarrett Wollman error = ENOBUFS; 9512c37256eSGarrett Wollman goto out; 9522c37256eSGarrett Wollman } 9532c37256eSGarrett Wollman /* 9542c37256eSGarrett Wollman * According to RFC961 (Assigned Protocols), 9552c37256eSGarrett Wollman * the urgent pointer points to the last octet 9562c37256eSGarrett Wollman * of urgent data. We continue, however, 9572c37256eSGarrett Wollman * to consider it to indicate the first octet 9582c37256eSGarrett Wollman * of data past the urgent section. 9592c37256eSGarrett Wollman * Otherwise, snd_up should be one lower. 9602c37256eSGarrett Wollman */ 961651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_snd, m, flags); 962d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 963ef53690bSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 964ef53690bSGarrett Wollman /* 965ef53690bSGarrett Wollman * Do implied connect if not yet connected, 966ef53690bSGarrett Wollman * initialize window to default value, and 967ef53690bSGarrett Wollman * initialize maxseg/maxopd using peer's cached 968ef53690bSGarrett Wollman * 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 } 13321baaf834SBruce M Simpson 133343d94734SJohn Baldwin ti->tcpi_rto = tp->t_rxtcur * tick; 133443d94734SJohn Baldwin ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick; 13351baaf834SBruce M Simpson ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 13361baaf834SBruce M Simpson ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT; 13371baaf834SBruce M Simpson 1338b8af5dfaSRobert Watson ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1339b8af5dfaSRobert Watson ti->tcpi_snd_cwnd = tp->snd_cwnd; 1340b8af5dfaSRobert Watson 1341b8af5dfaSRobert Watson /* 1342b8af5dfaSRobert Watson * FreeBSD-specific extension fields for tcp_info. 1343b8af5dfaSRobert Watson */ 1344c8443a1dSRobert Watson ti->tcpi_rcv_space = tp->rcv_wnd; 1345535fbad6SKip Macy ti->tcpi_rcv_nxt = tp->rcv_nxt; 1346b8af5dfaSRobert Watson ti->tcpi_snd_wnd = tp->snd_wnd; 13471c18314dSAndre Oppermann ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */ 1348535fbad6SKip Macy ti->tcpi_snd_nxt = tp->snd_nxt; 134943d94734SJohn Baldwin ti->tcpi_snd_mss = tp->t_maxseg; 135043d94734SJohn Baldwin ti->tcpi_rcv_mss = tp->t_maxseg; 1351535fbad6SKip Macy if (tp->t_flags & TF_TOE) 1352535fbad6SKip Macy ti->tcpi_options |= TCPI_OPT_TOE; 1353f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack; 1354f5d34df5SGeorge V. Neville-Neil ti->tcpi_rcv_ooopack = tp->t_rcvoopack; 1355f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_zerowin = tp->t_sndzerowin; 1356b8af5dfaSRobert Watson } 1357b8af5dfaSRobert Watson 1358b8af5dfaSRobert Watson /* 13591e8f5ffaSRobert Watson * tcp_ctloutput() must drop the inpcb lock before performing copyin on 13601e8f5ffaSRobert Watson * socket option arguments. When it re-acquires the lock after the copy, it 13611e8f5ffaSRobert Watson * has to revalidate that the connection is still valid for the socket 13621e8f5ffaSRobert Watson * option. 1363cfe8b629SGarrett Wollman */ 13648501a69cSRobert Watson #define INP_WLOCK_RECHECK(inp) do { \ 13658501a69cSRobert Watson INP_WLOCK(inp); \ 1366ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \ 13678501a69cSRobert Watson INP_WUNLOCK(inp); \ 13681e8f5ffaSRobert Watson return (ECONNRESET); \ 13691e8f5ffaSRobert Watson } \ 13701e8f5ffaSRobert Watson tp = intotcpcb(inp); \ 13711e8f5ffaSRobert Watson } while(0) 13721e8f5ffaSRobert Watson 1373df8bae1dSRodney W. Grimes int 1374ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1375df8bae1dSRodney W. Grimes { 137655bceb1eSRandall Stewart int error; 1377df8bae1dSRodney W. Grimes struct inpcb *inp; 1378cfe8b629SGarrett Wollman struct tcpcb *tp; 137955bceb1eSRandall Stewart struct tcp_function_block *blk; 138055bceb1eSRandall Stewart struct tcp_function_set fsn; 1381df8bae1dSRodney W. Grimes 1382cfe8b629SGarrett Wollman error = 0; 1383df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1384623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 13858501a69cSRobert Watson INP_WLOCK(inp); 1386cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_TCP) { 1387fb59c426SYoshinobu Inoue #ifdef INET6 13885cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 13898501a69cSRobert Watson INP_WUNLOCK(inp); 1390fb59c426SYoshinobu Inoue error = ip6_ctloutput(so, sopt); 1391b287c6c7SBjoern A. Zeeb } 1392fb59c426SYoshinobu Inoue #endif /* INET6 */ 1393b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1394b287c6c7SBjoern A. Zeeb else 1395b287c6c7SBjoern A. Zeeb #endif 1396b287c6c7SBjoern A. Zeeb #ifdef INET 1397b287c6c7SBjoern A. Zeeb { 13988501a69cSRobert Watson INP_WUNLOCK(inp); 1399cfe8b629SGarrett Wollman error = ip_ctloutput(so, sopt); 14001e8f5ffaSRobert Watson } 14011e8f5ffaSRobert Watson #endif 1402df8bae1dSRodney W. Grimes return (error); 1403df8bae1dSRodney W. Grimes } 1404ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 14058501a69cSRobert Watson INP_WUNLOCK(inp); 14061e8f5ffaSRobert Watson return (ECONNRESET); 1407623dce13SRobert Watson } 140855bceb1eSRandall Stewart tp = intotcpcb(inp); 140955bceb1eSRandall Stewart /* 141055bceb1eSRandall Stewart * Protect the TCP option TCP_FUNCTION_BLK so 141155bceb1eSRandall Stewart * that a sub-function can *never* overwrite this. 141255bceb1eSRandall Stewart */ 141355bceb1eSRandall Stewart if ((sopt->sopt_dir == SOPT_SET) && 141455bceb1eSRandall Stewart (sopt->sopt_name == TCP_FUNCTION_BLK)) { 141555bceb1eSRandall Stewart INP_WUNLOCK(inp); 141655bceb1eSRandall Stewart error = sooptcopyin(sopt, &fsn, sizeof fsn, 141755bceb1eSRandall Stewart sizeof fsn); 141855bceb1eSRandall Stewart if (error) 141955bceb1eSRandall Stewart return (error); 142055bceb1eSRandall Stewart INP_WLOCK_RECHECK(inp); 142155bceb1eSRandall Stewart if (tp->t_state != TCPS_CLOSED) { 142255bceb1eSRandall Stewart /* 142355bceb1eSRandall Stewart * The user has advanced the state 142455bceb1eSRandall Stewart * past the initial point, we can't 142555bceb1eSRandall Stewart * switch since we are down the road 142655bceb1eSRandall Stewart * and a new set of functions may 142755bceb1eSRandall Stewart * not be compatibile. 142855bceb1eSRandall Stewart */ 142955bceb1eSRandall Stewart INP_WUNLOCK(inp); 143055bceb1eSRandall Stewart return(EINVAL); 143155bceb1eSRandall Stewart } 143255bceb1eSRandall Stewart blk = find_and_ref_tcp_functions(&fsn); 143355bceb1eSRandall Stewart if (blk == NULL) { 143455bceb1eSRandall Stewart INP_WUNLOCK(inp); 143555bceb1eSRandall Stewart return (ENOENT); 143655bceb1eSRandall Stewart } 143755bceb1eSRandall Stewart if (tp->t_fb != blk) { 143855bceb1eSRandall Stewart if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) { 143955bceb1eSRandall Stewart refcount_release(&blk->tfb_refcnt); 144055bceb1eSRandall Stewart INP_WUNLOCK(inp); 144155bceb1eSRandall Stewart return (ENOENT); 144255bceb1eSRandall Stewart } 144355bceb1eSRandall Stewart /* 144455bceb1eSRandall Stewart * Release the old refcnt, the 144555bceb1eSRandall Stewart * lookup acquires a ref on the 144655bceb1eSRandall Stewart * new one. 144755bceb1eSRandall Stewart */ 144855bceb1eSRandall Stewart if (tp->t_fb->tfb_tcp_fb_fini) 144955bceb1eSRandall Stewart (*tp->t_fb->tfb_tcp_fb_fini)(tp); 145055bceb1eSRandall Stewart refcount_release(&tp->t_fb->tfb_refcnt); 145155bceb1eSRandall Stewart tp->t_fb = blk; 145255bceb1eSRandall Stewart if (tp->t_fb->tfb_tcp_fb_init) { 145355bceb1eSRandall Stewart (*tp->t_fb->tfb_tcp_fb_init)(tp); 145455bceb1eSRandall Stewart } 145555bceb1eSRandall Stewart } 145655bceb1eSRandall Stewart #ifdef TCP_OFFLOAD 145755bceb1eSRandall Stewart if (tp->t_flags & TF_TOE) { 145855bceb1eSRandall Stewart tcp_offload_ctloutput(tp, sopt->sopt_dir, 145955bceb1eSRandall Stewart sopt->sopt_name); 146055bceb1eSRandall Stewart } 146155bceb1eSRandall Stewart #endif 146255bceb1eSRandall Stewart INP_WUNLOCK(inp); 146355bceb1eSRandall Stewart return (error); 146455bceb1eSRandall Stewart } else if ((sopt->sopt_dir == SOPT_GET) && 146555bceb1eSRandall Stewart (sopt->sopt_name == TCP_FUNCTION_BLK)) { 146655bceb1eSRandall Stewart strcpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name); 146755bceb1eSRandall Stewart fsn.pcbcnt = tp->t_fb->tfb_refcnt; 146855bceb1eSRandall Stewart INP_WUNLOCK(inp); 146955bceb1eSRandall Stewart error = sooptcopyout(sopt, &fsn, sizeof fsn); 147055bceb1eSRandall Stewart return (error); 147155bceb1eSRandall Stewart } 147255bceb1eSRandall Stewart /* Pass in the INP locked, called must unlock it */ 147355bceb1eSRandall Stewart return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp)); 147455bceb1eSRandall Stewart } 147555bceb1eSRandall Stewart 147655bceb1eSRandall Stewart int 147755bceb1eSRandall Stewart tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp) 147855bceb1eSRandall Stewart { 147955bceb1eSRandall Stewart int error, opt, optval; 148055bceb1eSRandall Stewart u_int ui; 148155bceb1eSRandall Stewart struct tcp_info ti; 148255bceb1eSRandall Stewart struct cc_algo *algo; 148355bceb1eSRandall Stewart char buf[TCP_CA_NAME_MAX]; 1484df8bae1dSRodney W. Grimes 1485cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 1486cfe8b629SGarrett Wollman case SOPT_SET: 1487cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 14881cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 148988f6b043SBruce M Simpson case TCP_MD5SIG: 14908501a69cSRobert Watson INP_WUNLOCK(inp); 14911cfd4b53SBruce M Simpson error = sooptcopyin(sopt, &optval, sizeof optval, 14921cfd4b53SBruce M Simpson sizeof optval); 14931cfd4b53SBruce M Simpson if (error) 14941e8f5ffaSRobert Watson return (error); 14951cfd4b53SBruce M Simpson 14968501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 14971cfd4b53SBruce M Simpson if (optval > 0) 14981cfd4b53SBruce M Simpson tp->t_flags |= TF_SIGNATURE; 14991cfd4b53SBruce M Simpson else 15001cfd4b53SBruce M Simpson tp->t_flags &= ~TF_SIGNATURE; 150109fe6320SNavdeep Parhar goto unlock_and_done; 15021cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */ 150309fe6320SNavdeep Parhar 1504df8bae1dSRodney W. Grimes case TCP_NODELAY: 1505cfe8b629SGarrett Wollman case TCP_NOOPT: 15068501a69cSRobert Watson INP_WUNLOCK(inp); 1507cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1508cfe8b629SGarrett Wollman sizeof optval); 1509cfe8b629SGarrett Wollman if (error) 15101e8f5ffaSRobert Watson return (error); 1511cfe8b629SGarrett Wollman 15128501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 1513cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1514cfe8b629SGarrett Wollman case TCP_NODELAY: 1515cfe8b629SGarrett Wollman opt = TF_NODELAY; 1516cfe8b629SGarrett Wollman break; 1517cfe8b629SGarrett Wollman case TCP_NOOPT: 1518cfe8b629SGarrett Wollman opt = TF_NOOPT; 1519cfe8b629SGarrett Wollman break; 1520cfe8b629SGarrett Wollman default: 1521cfe8b629SGarrett Wollman opt = 0; /* dead code to fool gcc */ 1522cfe8b629SGarrett Wollman break; 1523cfe8b629SGarrett Wollman } 1524cfe8b629SGarrett Wollman 1525cfe8b629SGarrett Wollman if (optval) 1526cfe8b629SGarrett Wollman tp->t_flags |= opt; 1527df8bae1dSRodney W. Grimes else 1528cfe8b629SGarrett Wollman tp->t_flags &= ~opt; 152909fe6320SNavdeep Parhar unlock_and_done: 153009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 153109fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 153209fe6320SNavdeep Parhar tcp_offload_ctloutput(tp, sopt->sopt_dir, 153309fe6320SNavdeep Parhar sopt->sopt_name); 153409fe6320SNavdeep Parhar } 153509fe6320SNavdeep Parhar #endif 15368501a69cSRobert Watson INP_WUNLOCK(inp); 1537df8bae1dSRodney W. Grimes break; 1538df8bae1dSRodney W. Grimes 1539007581c0SJonathan Lemon case TCP_NOPUSH: 15408501a69cSRobert Watson INP_WUNLOCK(inp); 1541007581c0SJonathan Lemon error = sooptcopyin(sopt, &optval, sizeof optval, 1542007581c0SJonathan Lemon sizeof optval); 1543007581c0SJonathan Lemon if (error) 15441e8f5ffaSRobert Watson return (error); 1545007581c0SJonathan Lemon 15468501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 1547007581c0SJonathan Lemon if (optval) 1548007581c0SJonathan Lemon tp->t_flags |= TF_NOPUSH; 1549d28b9e89SJohn Baldwin else if (tp->t_flags & TF_NOPUSH) { 1550007581c0SJonathan Lemon tp->t_flags &= ~TF_NOPUSH; 1551d28b9e89SJohn Baldwin if (TCPS_HAVEESTABLISHED(tp->t_state)) 155255bceb1eSRandall Stewart error = tp->t_fb->tfb_tcp_output(tp); 1553007581c0SJonathan Lemon } 155409fe6320SNavdeep Parhar goto unlock_and_done; 1555007581c0SJonathan Lemon 1556df8bae1dSRodney W. Grimes case TCP_MAXSEG: 15578501a69cSRobert Watson INP_WUNLOCK(inp); 1558cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1559cfe8b629SGarrett Wollman sizeof optval); 1560cfe8b629SGarrett Wollman if (error) 15611e8f5ffaSRobert Watson return (error); 1562df8bae1dSRodney W. Grimes 15638501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 156453369ac9SAndre Oppermann if (optval > 0 && optval <= tp->t_maxseg && 1565603724d3SBjoern A. Zeeb optval + 40 >= V_tcp_minmss) 1566cfe8b629SGarrett Wollman tp->t_maxseg = optval; 1567a0292f23SGarrett Wollman else 1568a0292f23SGarrett Wollman error = EINVAL; 156909fe6320SNavdeep Parhar goto unlock_and_done; 1570a0292f23SGarrett Wollman 1571b8af5dfaSRobert Watson case TCP_INFO: 15728501a69cSRobert Watson INP_WUNLOCK(inp); 1573b8af5dfaSRobert Watson error = EINVAL; 1574b8af5dfaSRobert Watson break; 1575b8af5dfaSRobert Watson 1576dbc42409SLawrence Stewart case TCP_CONGESTION: 1577dbc42409SLawrence Stewart INP_WUNLOCK(inp); 1578dbc42409SLawrence Stewart bzero(buf, sizeof(buf)); 1579dbc42409SLawrence Stewart error = sooptcopyin(sopt, &buf, sizeof(buf), 1); 1580dbc42409SLawrence Stewart if (error) 1581dbc42409SLawrence Stewart break; 1582dbc42409SLawrence Stewart INP_WLOCK_RECHECK(inp); 1583dbc42409SLawrence Stewart /* 1584dbc42409SLawrence Stewart * Return EINVAL if we can't find the requested cc algo. 1585dbc42409SLawrence Stewart */ 1586dbc42409SLawrence Stewart error = EINVAL; 1587dbc42409SLawrence Stewart CC_LIST_RLOCK(); 1588dbc42409SLawrence Stewart STAILQ_FOREACH(algo, &cc_list, entries) { 1589dbc42409SLawrence Stewart if (strncmp(buf, algo->name, TCP_CA_NAME_MAX) 1590dbc42409SLawrence Stewart == 0) { 1591dbc42409SLawrence Stewart /* We've found the requested algo. */ 1592dbc42409SLawrence Stewart error = 0; 1593dbc42409SLawrence Stewart /* 1594dbc42409SLawrence Stewart * We hold a write lock over the tcb 1595dbc42409SLawrence Stewart * so it's safe to do these things 1596dbc42409SLawrence Stewart * without ordering concerns. 1597dbc42409SLawrence Stewart */ 1598dbc42409SLawrence Stewart if (CC_ALGO(tp)->cb_destroy != NULL) 1599dbc42409SLawrence Stewart CC_ALGO(tp)->cb_destroy(tp->ccv); 1600dbc42409SLawrence Stewart CC_ALGO(tp) = algo; 1601dbc42409SLawrence Stewart /* 1602dbc42409SLawrence Stewart * If something goes pear shaped 1603dbc42409SLawrence Stewart * initialising the new algo, 1604dbc42409SLawrence Stewart * fall back to newreno (which 1605dbc42409SLawrence Stewart * does not require initialisation). 1606dbc42409SLawrence Stewart */ 1607dbc42409SLawrence Stewart if (algo->cb_init != NULL) 1608dbc42409SLawrence Stewart if (algo->cb_init(tp->ccv) > 0) { 1609dbc42409SLawrence Stewart CC_ALGO(tp) = &newreno_cc_algo; 1610dbc42409SLawrence Stewart /* 1611dbc42409SLawrence Stewart * The only reason init 1612dbc42409SLawrence Stewart * should fail is 1613dbc42409SLawrence Stewart * because of malloc. 1614dbc42409SLawrence Stewart */ 1615dbc42409SLawrence Stewart error = ENOMEM; 1616dbc42409SLawrence Stewart } 1617dbc42409SLawrence Stewart break; /* Break the STAILQ_FOREACH. */ 1618dbc42409SLawrence Stewart } 1619dbc42409SLawrence Stewart } 1620dbc42409SLawrence Stewart CC_LIST_RUNLOCK(); 162109fe6320SNavdeep Parhar goto unlock_and_done; 1622dbc42409SLawrence Stewart 16239077f387SGleb Smirnoff case TCP_KEEPIDLE: 16249077f387SGleb Smirnoff case TCP_KEEPINTVL: 16259077f387SGleb Smirnoff case TCP_KEEPINIT: 16269077f387SGleb Smirnoff INP_WUNLOCK(inp); 16279077f387SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 16289077f387SGleb Smirnoff if (error) 16299077f387SGleb Smirnoff return (error); 16309077f387SGleb Smirnoff 16319077f387SGleb Smirnoff if (ui > (UINT_MAX / hz)) { 16329077f387SGleb Smirnoff error = EINVAL; 16339077f387SGleb Smirnoff break; 16349077f387SGleb Smirnoff } 16359077f387SGleb Smirnoff ui *= hz; 16369077f387SGleb Smirnoff 16379077f387SGleb Smirnoff INP_WLOCK_RECHECK(inp); 16389077f387SGleb Smirnoff switch (sopt->sopt_name) { 16399077f387SGleb Smirnoff case TCP_KEEPIDLE: 16409077f387SGleb Smirnoff tp->t_keepidle = ui; 16419077f387SGleb Smirnoff /* 16429077f387SGleb Smirnoff * XXX: better check current remaining 16439077f387SGleb Smirnoff * timeout and "merge" it with new value. 16449077f387SGleb Smirnoff */ 16459077f387SGleb Smirnoff if ((tp->t_state > TCPS_LISTEN) && 16469077f387SGleb Smirnoff (tp->t_state <= TCPS_CLOSING)) 16479077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 16489077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 16499077f387SGleb Smirnoff break; 16509077f387SGleb Smirnoff case TCP_KEEPINTVL: 16519077f387SGleb Smirnoff tp->t_keepintvl = ui; 16529077f387SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 16539077f387SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 16549077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 16559077f387SGleb Smirnoff TP_MAXIDLE(tp)); 16569077f387SGleb Smirnoff break; 16579077f387SGleb Smirnoff case TCP_KEEPINIT: 16589077f387SGleb Smirnoff tp->t_keepinit = ui; 16599077f387SGleb Smirnoff if (tp->t_state == TCPS_SYN_RECEIVED || 16609077f387SGleb Smirnoff tp->t_state == TCPS_SYN_SENT) 16619077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 16629077f387SGleb Smirnoff TP_KEEPINIT(tp)); 16639077f387SGleb Smirnoff break; 16649077f387SGleb Smirnoff } 166509fe6320SNavdeep Parhar goto unlock_and_done; 16669077f387SGleb Smirnoff 166785c05144SGleb Smirnoff case TCP_KEEPCNT: 166885c05144SGleb Smirnoff INP_WUNLOCK(inp); 166985c05144SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 167085c05144SGleb Smirnoff if (error) 167185c05144SGleb Smirnoff return (error); 167285c05144SGleb Smirnoff 167385c05144SGleb Smirnoff INP_WLOCK_RECHECK(inp); 167485c05144SGleb Smirnoff tp->t_keepcnt = ui; 167585c05144SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 167685c05144SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 167785c05144SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 167885c05144SGleb Smirnoff TP_MAXIDLE(tp)); 167985c05144SGleb Smirnoff goto unlock_and_done; 168085c05144SGleb Smirnoff 168186a996e6SHiren Panchasara #ifdef TCPPCAP 168286a996e6SHiren Panchasara case TCP_PCAP_OUT: 168386a996e6SHiren Panchasara case TCP_PCAP_IN: 168486a996e6SHiren Panchasara INP_WUNLOCK(inp); 168586a996e6SHiren Panchasara error = sooptcopyin(sopt, &optval, sizeof optval, 168686a996e6SHiren Panchasara sizeof optval); 168786a996e6SHiren Panchasara if (error) 168886a996e6SHiren Panchasara return (error); 168986a996e6SHiren Panchasara 169086a996e6SHiren Panchasara INP_WLOCK_RECHECK(inp); 169186a996e6SHiren Panchasara if (optval >= 0) 169286a996e6SHiren Panchasara tcp_pcap_set_sock_max(TCP_PCAP_OUT ? 169386a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts), 169486a996e6SHiren Panchasara optval); 169586a996e6SHiren Panchasara else 169686a996e6SHiren Panchasara error = EINVAL; 169786a996e6SHiren Panchasara goto unlock_and_done; 169886a996e6SHiren Panchasara #endif 169986a996e6SHiren Panchasara 1700*281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 1701*281a0fd4SPatrick Kelsey case TCP_FASTOPEN: 1702*281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 1703*281a0fd4SPatrick Kelsey if (!V_tcp_fastopen_enabled) 1704*281a0fd4SPatrick Kelsey return (EPERM); 1705*281a0fd4SPatrick Kelsey 1706*281a0fd4SPatrick Kelsey error = sooptcopyin(sopt, &optval, sizeof optval, 1707*281a0fd4SPatrick Kelsey sizeof optval); 1708*281a0fd4SPatrick Kelsey if (error) 1709*281a0fd4SPatrick Kelsey return (error); 1710*281a0fd4SPatrick Kelsey 1711*281a0fd4SPatrick Kelsey INP_WLOCK_RECHECK(inp); 1712*281a0fd4SPatrick Kelsey if (optval) { 1713*281a0fd4SPatrick Kelsey tp->t_flags |= TF_FASTOPEN; 1714*281a0fd4SPatrick Kelsey if ((tp->t_state == TCPS_LISTEN) && 1715*281a0fd4SPatrick Kelsey (tp->t_tfo_pending == NULL)) 1716*281a0fd4SPatrick Kelsey tp->t_tfo_pending = 1717*281a0fd4SPatrick Kelsey tcp_fastopen_alloc_counter(); 1718*281a0fd4SPatrick Kelsey } else 1719*281a0fd4SPatrick Kelsey tp->t_flags &= ~TF_FASTOPEN; 1720*281a0fd4SPatrick Kelsey goto unlock_and_done; 1721*281a0fd4SPatrick Kelsey #endif 1722*281a0fd4SPatrick Kelsey 1723df8bae1dSRodney W. Grimes default: 17248501a69cSRobert Watson INP_WUNLOCK(inp); 1725df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1726df8bae1dSRodney W. Grimes break; 1727df8bae1dSRodney W. Grimes } 1728df8bae1dSRodney W. Grimes break; 1729df8bae1dSRodney W. Grimes 1730cfe8b629SGarrett Wollman case SOPT_GET: 17311e8f5ffaSRobert Watson tp = intotcpcb(inp); 1732cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 17331cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 173488f6b043SBruce M Simpson case TCP_MD5SIG: 17351cfd4b53SBruce M Simpson optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0; 17368501a69cSRobert Watson INP_WUNLOCK(inp); 1737b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 17381cfd4b53SBruce M Simpson break; 1739265ed012SBruce M Simpson #endif 17401e8f5ffaSRobert Watson 1741df8bae1dSRodney W. Grimes case TCP_NODELAY: 1742cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NODELAY; 17438501a69cSRobert Watson INP_WUNLOCK(inp); 1744b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1745df8bae1dSRodney W. Grimes break; 1746df8bae1dSRodney W. Grimes case TCP_MAXSEG: 1747cfe8b629SGarrett Wollman optval = tp->t_maxseg; 17488501a69cSRobert Watson INP_WUNLOCK(inp); 1749b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1750df8bae1dSRodney W. Grimes break; 1751a0292f23SGarrett Wollman case TCP_NOOPT: 1752cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOOPT; 17538501a69cSRobert Watson INP_WUNLOCK(inp); 1754b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1755a0292f23SGarrett Wollman break; 1756a0292f23SGarrett Wollman case TCP_NOPUSH: 1757cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOPUSH; 17588501a69cSRobert Watson INP_WUNLOCK(inp); 1759b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1760b8af5dfaSRobert Watson break; 1761b8af5dfaSRobert Watson case TCP_INFO: 1762b8af5dfaSRobert Watson tcp_fill_info(tp, &ti); 17638501a69cSRobert Watson INP_WUNLOCK(inp); 1764b8af5dfaSRobert Watson error = sooptcopyout(sopt, &ti, sizeof ti); 1765a0292f23SGarrett Wollman break; 1766dbc42409SLawrence Stewart case TCP_CONGESTION: 1767dbc42409SLawrence Stewart bzero(buf, sizeof(buf)); 1768dbc42409SLawrence Stewart strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); 1769dbc42409SLawrence Stewart INP_WUNLOCK(inp); 1770dbc42409SLawrence Stewart error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX); 1771dbc42409SLawrence Stewart break; 17722f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 17732f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 17742f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 17752f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 17762f3eb7f4SGleb Smirnoff switch (sopt->sopt_name) { 17772f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 17782f3eb7f4SGleb Smirnoff ui = tp->t_keepidle / hz; 17792f3eb7f4SGleb Smirnoff break; 17802f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 17812f3eb7f4SGleb Smirnoff ui = tp->t_keepintvl / hz; 17822f3eb7f4SGleb Smirnoff break; 17832f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 17842f3eb7f4SGleb Smirnoff ui = tp->t_keepinit / hz; 17852f3eb7f4SGleb Smirnoff break; 17862f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 17872f3eb7f4SGleb Smirnoff ui = tp->t_keepcnt; 17882f3eb7f4SGleb Smirnoff break; 17892f3eb7f4SGleb Smirnoff } 17902f3eb7f4SGleb Smirnoff INP_WUNLOCK(inp); 17912f3eb7f4SGleb Smirnoff error = sooptcopyout(sopt, &ui, sizeof(ui)); 17922f3eb7f4SGleb Smirnoff break; 179386a996e6SHiren Panchasara #ifdef TCPPCAP 179486a996e6SHiren Panchasara case TCP_PCAP_OUT: 179586a996e6SHiren Panchasara case TCP_PCAP_IN: 179686a996e6SHiren Panchasara optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ? 179786a996e6SHiren Panchasara &(tp->t_outpkts) : &(tp->t_inpkts)); 179886a996e6SHiren Panchasara INP_WUNLOCK(inp); 179986a996e6SHiren Panchasara error = sooptcopyout(sopt, &optval, sizeof optval); 180086a996e6SHiren Panchasara break; 180186a996e6SHiren Panchasara #endif 1802*281a0fd4SPatrick Kelsey 1803*281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413 1804*281a0fd4SPatrick Kelsey case TCP_FASTOPEN: 1805*281a0fd4SPatrick Kelsey optval = tp->t_flags & TF_FASTOPEN; 1806*281a0fd4SPatrick Kelsey INP_WUNLOCK(inp); 1807*281a0fd4SPatrick Kelsey error = sooptcopyout(sopt, &optval, sizeof optval); 1808*281a0fd4SPatrick Kelsey break; 1809*281a0fd4SPatrick Kelsey #endif 1810df8bae1dSRodney W. Grimes default: 18118501a69cSRobert Watson INP_WUNLOCK(inp); 1812df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1813df8bae1dSRodney W. Grimes break; 1814df8bae1dSRodney W. Grimes } 1815df8bae1dSRodney W. Grimes break; 1816df8bae1dSRodney W. Grimes } 1817df8bae1dSRodney W. Grimes return (error); 1818df8bae1dSRodney W. Grimes } 18198501a69cSRobert Watson #undef INP_WLOCK_RECHECK 1820df8bae1dSRodney W. Grimes 182126e30fbbSDavid Greenman /* 1822df8bae1dSRodney W. Grimes * Attach TCP protocol to socket, allocating 1823df8bae1dSRodney W. Grimes * internet protocol control block, tcp control block, 1824df8bae1dSRodney W. Grimes * bufer space, and entering LISTEN state if to accept connections. 1825df8bae1dSRodney W. Grimes */ 18260312fbe9SPoul-Henning Kamp static int 1827ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so) 1828df8bae1dSRodney W. Grimes { 1829ad3f9ab3SAndre Oppermann struct tcpcb *tp; 1830df8bae1dSRodney W. Grimes struct inpcb *inp; 1831df8bae1dSRodney W. Grimes int error; 1832df8bae1dSRodney W. Grimes 1833df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1834e233e2acSAndre Oppermann error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace); 1835df8bae1dSRodney W. Grimes if (error) 1836df8bae1dSRodney W. Grimes return (error); 1837df8bae1dSRodney W. Grimes } 18386741ecf5SAndre Oppermann so->so_rcv.sb_flags |= SB_AUTOSIZE; 18396741ecf5SAndre Oppermann so->so_snd.sb_flags |= SB_AUTOSIZE; 1840ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 1841603724d3SBjoern A. Zeeb error = in_pcballoc(so, &V_tcbinfo); 1842f2de87feSRobert Watson if (error) { 1843ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1844df8bae1dSRodney W. Grimes return (error); 1845f2de87feSRobert Watson } 1846df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1847fb59c426SYoshinobu Inoue #ifdef INET6 18485cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 1849fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 1850fb59c426SYoshinobu Inoue inp->in6p_hops = -1; /* use kernel default */ 1851fb59c426SYoshinobu Inoue } 1852fb59c426SYoshinobu Inoue else 1853fb59c426SYoshinobu Inoue #endif 1854cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 1855df8bae1dSRodney W. Grimes tp = tcp_newtcpcb(inp); 1856623dce13SRobert Watson if (tp == NULL) { 1857df8bae1dSRodney W. Grimes in_pcbdetach(inp); 18580206cdb8SBjoern A. Zeeb in_pcbfree(inp); 1859ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1860df8bae1dSRodney W. Grimes return (ENOBUFS); 1861df8bae1dSRodney W. Grimes } 1862df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 18638501a69cSRobert Watson INP_WUNLOCK(inp); 1864ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1865df8bae1dSRodney W. Grimes return (0); 1866df8bae1dSRodney W. Grimes } 1867df8bae1dSRodney W. Grimes 1868df8bae1dSRodney W. Grimes /* 1869df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 1870df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 1871df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 1872df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 1873df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 1874df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 1875df8bae1dSRodney W. Grimes */ 1876623dce13SRobert Watson static void 1877ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp) 1878df8bae1dSRodney W. Grimes { 1879e6e0b5ffSRobert Watson struct inpcb *inp = tp->t_inpcb; 1880e6e0b5ffSRobert Watson struct socket *so = inp->inp_socket; 1881e6e0b5ffSRobert Watson 1882ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 18838501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1884df8bae1dSRodney W. Grimes 1885623dce13SRobert Watson /* 1886623dce13SRobert Watson * Neither tcp_close() nor tcp_drop() should return NULL, as the 1887623dce13SRobert Watson * socket is still open. 1888623dce13SRobert Watson */ 1889623dce13SRobert Watson if (tp->t_state < TCPS_ESTABLISHED) { 1890df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1891623dce13SRobert Watson KASSERT(tp != NULL, 1892623dce13SRobert Watson ("tcp_disconnect: tcp_close() returned NULL")); 1893623dce13SRobert Watson } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 1894243917feSSeigo Tanimura tp = tcp_drop(tp, 0); 1895623dce13SRobert Watson KASSERT(tp != NULL, 1896623dce13SRobert Watson ("tcp_disconnect: tcp_drop() returned NULL")); 1897623dce13SRobert Watson } else { 1898df8bae1dSRodney W. Grimes soisdisconnecting(so); 1899df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 1900623dce13SRobert Watson tcp_usrclosed(tp); 1901ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 190255bceb1eSRandall Stewart tp->t_fb->tfb_tcp_output(tp); 1903df8bae1dSRodney W. Grimes } 1904df8bae1dSRodney W. Grimes } 1905df8bae1dSRodney W. Grimes 1906df8bae1dSRodney W. Grimes /* 1907df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 1908df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 1909df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1910df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 1911df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 1912df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 1913df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 1914df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 1915df8bae1dSRodney W. Grimes */ 1916623dce13SRobert Watson static void 1917ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp) 1918df8bae1dSRodney W. Grimes { 1919df8bae1dSRodney W. Grimes 1920ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 19218501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1922e6e0b5ffSRobert Watson 1923df8bae1dSRodney W. Grimes switch (tp->t_state) { 1924df8bae1dSRodney W. Grimes case TCPS_LISTEN: 192509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 192609fe6320SNavdeep Parhar tcp_offload_listen_stop(tp); 192709fe6320SNavdeep Parhar #endif 1928550e9d42SHiren Panchasara tcp_state_change(tp, TCPS_CLOSED); 1929bc65987aSKip Macy /* FALLTHROUGH */ 1930bc65987aSKip Macy case TCPS_CLOSED: 1931df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1932623dce13SRobert Watson /* 1933623dce13SRobert Watson * tcp_close() should never return NULL here as the socket is 1934623dce13SRobert Watson * still open. 1935623dce13SRobert Watson */ 1936623dce13SRobert Watson KASSERT(tp != NULL, 1937623dce13SRobert Watson ("tcp_usrclosed: tcp_close() returned NULL")); 1938df8bae1dSRodney W. Grimes break; 1939df8bae1dSRodney W. Grimes 1940a0292f23SGarrett Wollman case TCPS_SYN_SENT: 1941df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1942a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 1943a0292f23SGarrett Wollman break; 1944a0292f23SGarrett Wollman 1945df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 194657f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 1947df8bae1dSRodney W. Grimes break; 1948df8bae1dSRodney W. Grimes 1949df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 195057f60867SMark Johnston tcp_state_change(tp, TCPS_LAST_ACK); 1951df8bae1dSRodney W. Grimes break; 1952df8bae1dSRodney W. Grimes } 1953abc7d910SRobert Watson if (tp->t_state >= TCPS_FIN_WAIT_2) { 1954df8bae1dSRodney W. Grimes soisdisconnected(tp->t_inpcb->inp_socket); 1955abc7d910SRobert Watson /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 19567c72af87SMohan Srinivasan if (tp->t_state == TCPS_FIN_WAIT_2) { 19577c72af87SMohan Srinivasan int timeout; 19587c72af87SMohan Srinivasan 19597c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 19609077f387SGleb Smirnoff tcp_finwait2_timeout : TP_MAXIDLE(tp); 1961b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 1962b6239c4aSAndras Olah } 1963df8bae1dSRodney W. Grimes } 19647c72af87SMohan Srinivasan } 1965497057eeSRobert Watson 1966497057eeSRobert Watson #ifdef DDB 1967497057eeSRobert Watson static void 1968497057eeSRobert Watson db_print_indent(int indent) 1969497057eeSRobert Watson { 1970497057eeSRobert Watson int i; 1971497057eeSRobert Watson 1972497057eeSRobert Watson for (i = 0; i < indent; i++) 1973497057eeSRobert Watson db_printf(" "); 1974497057eeSRobert Watson } 1975497057eeSRobert Watson 1976497057eeSRobert Watson static void 1977497057eeSRobert Watson db_print_tstate(int t_state) 1978497057eeSRobert Watson { 1979497057eeSRobert Watson 1980497057eeSRobert Watson switch (t_state) { 1981497057eeSRobert Watson case TCPS_CLOSED: 1982497057eeSRobert Watson db_printf("TCPS_CLOSED"); 1983497057eeSRobert Watson return; 1984497057eeSRobert Watson 1985497057eeSRobert Watson case TCPS_LISTEN: 1986497057eeSRobert Watson db_printf("TCPS_LISTEN"); 1987497057eeSRobert Watson return; 1988497057eeSRobert Watson 1989497057eeSRobert Watson case TCPS_SYN_SENT: 1990497057eeSRobert Watson db_printf("TCPS_SYN_SENT"); 1991497057eeSRobert Watson return; 1992497057eeSRobert Watson 1993497057eeSRobert Watson case TCPS_SYN_RECEIVED: 1994497057eeSRobert Watson db_printf("TCPS_SYN_RECEIVED"); 1995497057eeSRobert Watson return; 1996497057eeSRobert Watson 1997497057eeSRobert Watson case TCPS_ESTABLISHED: 1998497057eeSRobert Watson db_printf("TCPS_ESTABLISHED"); 1999497057eeSRobert Watson return; 2000497057eeSRobert Watson 2001497057eeSRobert Watson case TCPS_CLOSE_WAIT: 2002497057eeSRobert Watson db_printf("TCPS_CLOSE_WAIT"); 2003497057eeSRobert Watson return; 2004497057eeSRobert Watson 2005497057eeSRobert Watson case TCPS_FIN_WAIT_1: 2006497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_1"); 2007497057eeSRobert Watson return; 2008497057eeSRobert Watson 2009497057eeSRobert Watson case TCPS_CLOSING: 2010497057eeSRobert Watson db_printf("TCPS_CLOSING"); 2011497057eeSRobert Watson return; 2012497057eeSRobert Watson 2013497057eeSRobert Watson case TCPS_LAST_ACK: 2014497057eeSRobert Watson db_printf("TCPS_LAST_ACK"); 2015497057eeSRobert Watson return; 2016497057eeSRobert Watson 2017497057eeSRobert Watson case TCPS_FIN_WAIT_2: 2018497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_2"); 2019497057eeSRobert Watson return; 2020497057eeSRobert Watson 2021497057eeSRobert Watson case TCPS_TIME_WAIT: 2022497057eeSRobert Watson db_printf("TCPS_TIME_WAIT"); 2023497057eeSRobert Watson return; 2024497057eeSRobert Watson 2025497057eeSRobert Watson default: 2026497057eeSRobert Watson db_printf("unknown"); 2027497057eeSRobert Watson return; 2028497057eeSRobert Watson } 2029497057eeSRobert Watson } 2030497057eeSRobert Watson 2031497057eeSRobert Watson static void 2032497057eeSRobert Watson db_print_tflags(u_int t_flags) 2033497057eeSRobert Watson { 2034497057eeSRobert Watson int comma; 2035497057eeSRobert Watson 2036497057eeSRobert Watson comma = 0; 2037497057eeSRobert Watson if (t_flags & TF_ACKNOW) { 2038497057eeSRobert Watson db_printf("%sTF_ACKNOW", comma ? ", " : ""); 2039497057eeSRobert Watson comma = 1; 2040497057eeSRobert Watson } 2041497057eeSRobert Watson if (t_flags & TF_DELACK) { 2042497057eeSRobert Watson db_printf("%sTF_DELACK", comma ? ", " : ""); 2043497057eeSRobert Watson comma = 1; 2044497057eeSRobert Watson } 2045497057eeSRobert Watson if (t_flags & TF_NODELAY) { 2046497057eeSRobert Watson db_printf("%sTF_NODELAY", comma ? ", " : ""); 2047497057eeSRobert Watson comma = 1; 2048497057eeSRobert Watson } 2049497057eeSRobert Watson if (t_flags & TF_NOOPT) { 2050497057eeSRobert Watson db_printf("%sTF_NOOPT", comma ? ", " : ""); 2051497057eeSRobert Watson comma = 1; 2052497057eeSRobert Watson } 2053497057eeSRobert Watson if (t_flags & TF_SENTFIN) { 2054497057eeSRobert Watson db_printf("%sTF_SENTFIN", comma ? ", " : ""); 2055497057eeSRobert Watson comma = 1; 2056497057eeSRobert Watson } 2057497057eeSRobert Watson if (t_flags & TF_REQ_SCALE) { 2058497057eeSRobert Watson db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 2059497057eeSRobert Watson comma = 1; 2060497057eeSRobert Watson } 2061497057eeSRobert Watson if (t_flags & TF_RCVD_SCALE) { 2062497057eeSRobert Watson db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 2063497057eeSRobert Watson comma = 1; 2064497057eeSRobert Watson } 2065497057eeSRobert Watson if (t_flags & TF_REQ_TSTMP) { 2066497057eeSRobert Watson db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 2067497057eeSRobert Watson comma = 1; 2068497057eeSRobert Watson } 2069497057eeSRobert Watson if (t_flags & TF_RCVD_TSTMP) { 2070497057eeSRobert Watson db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 2071497057eeSRobert Watson comma = 1; 2072497057eeSRobert Watson } 2073497057eeSRobert Watson if (t_flags & TF_SACK_PERMIT) { 2074497057eeSRobert Watson db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 2075497057eeSRobert Watson comma = 1; 2076497057eeSRobert Watson } 2077497057eeSRobert Watson if (t_flags & TF_NEEDSYN) { 2078497057eeSRobert Watson db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 2079497057eeSRobert Watson comma = 1; 2080497057eeSRobert Watson } 2081497057eeSRobert Watson if (t_flags & TF_NEEDFIN) { 2082497057eeSRobert Watson db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 2083497057eeSRobert Watson comma = 1; 2084497057eeSRobert Watson } 2085497057eeSRobert Watson if (t_flags & TF_NOPUSH) { 2086497057eeSRobert Watson db_printf("%sTF_NOPUSH", comma ? ", " : ""); 2087497057eeSRobert Watson comma = 1; 2088497057eeSRobert Watson } 2089497057eeSRobert Watson if (t_flags & TF_MORETOCOME) { 2090497057eeSRobert Watson db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 2091497057eeSRobert Watson comma = 1; 2092497057eeSRobert Watson } 2093497057eeSRobert Watson if (t_flags & TF_LQ_OVERFLOW) { 2094497057eeSRobert Watson db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : ""); 2095497057eeSRobert Watson comma = 1; 2096497057eeSRobert Watson } 2097497057eeSRobert Watson if (t_flags & TF_LASTIDLE) { 2098497057eeSRobert Watson db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 2099497057eeSRobert Watson comma = 1; 2100497057eeSRobert Watson } 2101497057eeSRobert Watson if (t_flags & TF_RXWIN0SENT) { 2102497057eeSRobert Watson db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 2103497057eeSRobert Watson comma = 1; 2104497057eeSRobert Watson } 2105497057eeSRobert Watson if (t_flags & TF_FASTRECOVERY) { 2106497057eeSRobert Watson db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 2107497057eeSRobert Watson comma = 1; 2108497057eeSRobert Watson } 2109dbc42409SLawrence Stewart if (t_flags & TF_CONGRECOVERY) { 2110dbc42409SLawrence Stewart db_printf("%sTF_CONGRECOVERY", comma ? ", " : ""); 2111dbc42409SLawrence Stewart comma = 1; 2112dbc42409SLawrence Stewart } 2113497057eeSRobert Watson if (t_flags & TF_WASFRECOVERY) { 2114497057eeSRobert Watson db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 2115497057eeSRobert Watson comma = 1; 2116497057eeSRobert Watson } 2117497057eeSRobert Watson if (t_flags & TF_SIGNATURE) { 2118497057eeSRobert Watson db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 2119497057eeSRobert Watson comma = 1; 2120497057eeSRobert Watson } 2121497057eeSRobert Watson if (t_flags & TF_FORCEDATA) { 2122497057eeSRobert Watson db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 2123497057eeSRobert Watson comma = 1; 2124497057eeSRobert Watson } 2125497057eeSRobert Watson if (t_flags & TF_TSO) { 2126497057eeSRobert Watson db_printf("%sTF_TSO", comma ? ", " : ""); 2127497057eeSRobert Watson comma = 1; 2128497057eeSRobert Watson } 2129f2512ba1SRui Paulo if (t_flags & TF_ECN_PERMIT) { 2130f2512ba1SRui Paulo db_printf("%sTF_ECN_PERMIT", comma ? ", " : ""); 2131f2512ba1SRui Paulo comma = 1; 2132f2512ba1SRui Paulo } 2133*281a0fd4SPatrick Kelsey if (t_flags & TF_FASTOPEN) { 2134*281a0fd4SPatrick Kelsey db_printf("%sTF_FASTOPEN", comma ? ", " : ""); 2135*281a0fd4SPatrick Kelsey comma = 1; 2136*281a0fd4SPatrick Kelsey } 2137497057eeSRobert Watson } 2138497057eeSRobert Watson 2139497057eeSRobert Watson static void 2140497057eeSRobert Watson db_print_toobflags(char t_oobflags) 2141497057eeSRobert Watson { 2142497057eeSRobert Watson int comma; 2143497057eeSRobert Watson 2144497057eeSRobert Watson comma = 0; 2145497057eeSRobert Watson if (t_oobflags & TCPOOB_HAVEDATA) { 2146497057eeSRobert Watson db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 2147497057eeSRobert Watson comma = 1; 2148497057eeSRobert Watson } 2149497057eeSRobert Watson if (t_oobflags & TCPOOB_HADDATA) { 2150497057eeSRobert Watson db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 2151497057eeSRobert Watson comma = 1; 2152497057eeSRobert Watson } 2153497057eeSRobert Watson } 2154497057eeSRobert Watson 2155497057eeSRobert Watson static void 2156497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 2157497057eeSRobert Watson { 2158497057eeSRobert Watson 2159497057eeSRobert Watson db_print_indent(indent); 2160497057eeSRobert Watson db_printf("%s at %p\n", name, tp); 2161497057eeSRobert Watson 2162497057eeSRobert Watson indent += 2; 2163497057eeSRobert Watson 2164497057eeSRobert Watson db_print_indent(indent); 2165497057eeSRobert Watson db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 21664741bfcbSPatrick Kelsey LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 2167497057eeSRobert Watson 2168497057eeSRobert Watson db_print_indent(indent); 216985d94372SRobert Watson db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n", 2170e2f2059fSMike Silbersack &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep); 2171497057eeSRobert Watson 2172497057eeSRobert Watson db_print_indent(indent); 2173e2f2059fSMike Silbersack db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl, 2174e2f2059fSMike Silbersack &tp->t_timers->tt_delack, tp->t_inpcb); 2175497057eeSRobert Watson 2176497057eeSRobert Watson db_print_indent(indent); 2177497057eeSRobert Watson db_printf("t_state: %d (", tp->t_state); 2178497057eeSRobert Watson db_print_tstate(tp->t_state); 2179497057eeSRobert Watson db_printf(")\n"); 2180497057eeSRobert Watson 2181497057eeSRobert Watson db_print_indent(indent); 2182497057eeSRobert Watson db_printf("t_flags: 0x%x (", tp->t_flags); 2183497057eeSRobert Watson db_print_tflags(tp->t_flags); 2184497057eeSRobert Watson db_printf(")\n"); 2185497057eeSRobert Watson 2186497057eeSRobert Watson db_print_indent(indent); 2187497057eeSRobert Watson db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n", 2188497057eeSRobert Watson tp->snd_una, tp->snd_max, tp->snd_nxt); 2189497057eeSRobert Watson 2190497057eeSRobert Watson db_print_indent(indent); 2191497057eeSRobert Watson db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 2192497057eeSRobert Watson tp->snd_up, tp->snd_wl1, tp->snd_wl2); 2193497057eeSRobert Watson 2194497057eeSRobert Watson db_print_indent(indent); 2195497057eeSRobert Watson db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 2196497057eeSRobert Watson tp->iss, tp->irs, tp->rcv_nxt); 2197497057eeSRobert Watson 2198497057eeSRobert Watson db_print_indent(indent); 2199497057eeSRobert Watson db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n", 2200497057eeSRobert Watson tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 2201497057eeSRobert Watson 2202497057eeSRobert Watson db_print_indent(indent); 22031c18314dSAndre Oppermann db_printf("snd_wnd: %lu snd_cwnd: %lu\n", 22041c18314dSAndre Oppermann tp->snd_wnd, tp->snd_cwnd); 2205497057eeSRobert Watson 2206497057eeSRobert Watson db_print_indent(indent); 22071c18314dSAndre Oppermann db_printf("snd_ssthresh: %lu snd_recover: " 22081c18314dSAndre Oppermann "0x%08x\n", tp->snd_ssthresh, tp->snd_recover); 2209497057eeSRobert Watson 2210497057eeSRobert Watson db_print_indent(indent); 22119f78a87aSJohn Baldwin db_printf("t_maxopd: %u t_rcvtime: %u t_startime: %u\n", 2212497057eeSRobert Watson tp->t_maxopd, tp->t_rcvtime, tp->t_starttime); 2213497057eeSRobert Watson 2214497057eeSRobert Watson db_print_indent(indent); 22151c18314dSAndre Oppermann db_printf("t_rttime: %u t_rtsq: 0x%08x\n", 22161c18314dSAndre Oppermann tp->t_rtttime, tp->t_rtseq); 2217497057eeSRobert Watson 2218497057eeSRobert Watson db_print_indent(indent); 22191c18314dSAndre Oppermann db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n", 22201c18314dSAndre Oppermann tp->t_rxtcur, tp->t_maxseg, tp->t_srtt); 2221497057eeSRobert Watson 2222497057eeSRobert Watson db_print_indent(indent); 2223497057eeSRobert Watson db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u " 2224497057eeSRobert Watson "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin, 2225497057eeSRobert Watson tp->t_rttbest); 2226497057eeSRobert Watson 2227497057eeSRobert Watson db_print_indent(indent); 2228497057eeSRobert Watson db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n", 2229497057eeSRobert Watson tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 2230497057eeSRobert Watson 2231497057eeSRobert Watson db_print_indent(indent); 2232497057eeSRobert Watson db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 2233497057eeSRobert Watson db_print_toobflags(tp->t_oobflags); 2234497057eeSRobert Watson db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 2235497057eeSRobert Watson 2236497057eeSRobert Watson db_print_indent(indent); 2237497057eeSRobert Watson db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 2238497057eeSRobert Watson tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 2239497057eeSRobert Watson 2240497057eeSRobert Watson db_print_indent(indent); 22419f78a87aSJohn Baldwin db_printf("ts_recent: %u ts_recent_age: %u\n", 22421a553740SAndre Oppermann tp->ts_recent, tp->ts_recent_age); 2243497057eeSRobert Watson 2244497057eeSRobert Watson db_print_indent(indent); 2245497057eeSRobert Watson db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 2246497057eeSRobert Watson "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 2247497057eeSRobert Watson 2248497057eeSRobert Watson db_print_indent(indent); 2249497057eeSRobert Watson db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x " 22509f78a87aSJohn Baldwin "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 2251497057eeSRobert Watson tp->snd_recover_prev, tp->t_badrxtwin); 2252497057eeSRobert Watson 2253497057eeSRobert Watson db_print_indent(indent); 22543529149eSAndre Oppermann db_printf("snd_numholes: %d snd_holes first: %p\n", 22553529149eSAndre Oppermann tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 2256497057eeSRobert Watson 2257497057eeSRobert Watson db_print_indent(indent); 2258497057eeSRobert Watson db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: " 2259497057eeSRobert Watson "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata); 2260497057eeSRobert Watson 2261497057eeSRobert Watson /* Skip sackblks, sackhint. */ 2262497057eeSRobert Watson 2263497057eeSRobert Watson db_print_indent(indent); 2264497057eeSRobert Watson db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 2265497057eeSRobert Watson tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 2266497057eeSRobert Watson } 2267497057eeSRobert Watson 2268497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 2269497057eeSRobert Watson { 2270497057eeSRobert Watson struct tcpcb *tp; 2271497057eeSRobert Watson 2272497057eeSRobert Watson if (!have_addr) { 2273497057eeSRobert Watson db_printf("usage: show tcpcb <addr>\n"); 2274497057eeSRobert Watson return; 2275497057eeSRobert Watson } 2276497057eeSRobert Watson tp = (struct tcpcb *)addr; 2277497057eeSRobert Watson 2278497057eeSRobert Watson db_print_tcpcb(tp, "tcpcb", 0); 2279497057eeSRobert Watson } 2280497057eeSRobert Watson #endif 2281