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> 50c7a82f90SGarrett Wollman #include <sys/kernel.h> 5198163b98SPoul-Henning Kamp #include <sys/sysctl.h> 52df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 53fb59c426SYoshinobu Inoue #ifdef INET6 54fb59c426SYoshinobu Inoue #include <sys/domain.h> 55fb59c426SYoshinobu Inoue #endif /* INET6 */ 56df8bae1dSRodney W. Grimes #include <sys/socket.h> 57df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 58df8bae1dSRodney W. Grimes #include <sys/protosw.h> 5991421ba2SRobert Watson #include <sys/proc.h> 6091421ba2SRobert Watson #include <sys/jail.h> 61df8bae1dSRodney W. Grimes 62497057eeSRobert Watson #ifdef DDB 63497057eeSRobert Watson #include <ddb/ddb.h> 64497057eeSRobert Watson #endif 65497057eeSRobert Watson 66df8bae1dSRodney W. Grimes #include <net/if.h> 6776039bc8SGleb Smirnoff #include <net/if_var.h> 68df8bae1dSRodney W. Grimes #include <net/route.h> 69530c0060SRobert Watson #include <net/vnet.h> 70df8bae1dSRodney W. Grimes 71dbc42409SLawrence Stewart #include <netinet/cc.h> 72df8bae1dSRodney W. Grimes #include <netinet/in.h> 735d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h> 74df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 75b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h> 76b5e8ce9fSBruce Evans #include <netinet/in_var.h> 77df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 78fb59c426SYoshinobu Inoue #ifdef INET6 79b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h> 80b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h> 81fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 82a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h> 83fb59c426SYoshinobu Inoue #endif 84df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 85df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 86df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 87df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 88df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 89610ee2f9SDavid Greenman #ifdef TCPDEBUG 90df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 91610ee2f9SDavid Greenman #endif 9209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 93bc65987aSKip Macy #include <netinet/tcp_offload.h> 9409fe6320SNavdeep Parhar #endif 95df8bae1dSRodney W. Grimes 96df8bae1dSRodney W. Grimes /* 97df8bae1dSRodney W. Grimes * TCP protocol interface to socket abstraction. 98df8bae1dSRodney W. Grimes */ 9956dc72c3SPawel Jakub Dawidek static int tcp_attach(struct socket *); 100b287c6c7SBjoern A. Zeeb #ifdef INET 1014d77a549SAlfred Perlstein static int tcp_connect(struct tcpcb *, struct sockaddr *, 1024d77a549SAlfred Perlstein struct thread *td); 103b287c6c7SBjoern A. Zeeb #endif /* INET */ 104fb59c426SYoshinobu Inoue #ifdef INET6 1054d77a549SAlfred Perlstein static int tcp6_connect(struct tcpcb *, struct sockaddr *, 1064d77a549SAlfred Perlstein struct thread *td); 107fb59c426SYoshinobu Inoue #endif /* INET6 */ 108623dce13SRobert Watson static void tcp_disconnect(struct tcpcb *); 109623dce13SRobert Watson static void tcp_usrclosed(struct tcpcb *); 110b8af5dfaSRobert Watson static void tcp_fill_info(struct tcpcb *, struct tcp_info *); 1112c37256eSGarrett Wollman 1122c37256eSGarrett Wollman #ifdef TCPDEBUG 1131db24ffbSJonathan Lemon #define TCPDEBUG0 int ostate = 0 1142c37256eSGarrett Wollman #define TCPDEBUG1() ostate = tp ? tp->t_state : 0 1154cc20ab1SSeigo Tanimura #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 1164cc20ab1SSeigo Tanimura tcp_trace(TA_USER, ostate, tp, 0, 0, req) 1172c37256eSGarrett Wollman #else 1182c37256eSGarrett Wollman #define TCPDEBUG0 1192c37256eSGarrett Wollman #define TCPDEBUG1() 1202c37256eSGarrett Wollman #define TCPDEBUG2(req) 1212c37256eSGarrett Wollman #endif 1222c37256eSGarrett Wollman 1232c37256eSGarrett Wollman /* 1242c37256eSGarrett Wollman * TCP attaches to socket via pru_attach(), reserving space, 1252c37256eSGarrett Wollman * and an internet control block. 1262c37256eSGarrett Wollman */ 1272c37256eSGarrett Wollman static int 128b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td) 1292c37256eSGarrett Wollman { 130f76fcf6dSJeffrey Hsu struct inpcb *inp; 131623dce13SRobert Watson struct tcpcb *tp = NULL; 132623dce13SRobert Watson int error; 1332c37256eSGarrett Wollman TCPDEBUG0; 1342c37256eSGarrett Wollman 135623dce13SRobert Watson inp = sotoinpcb(so); 136623dce13SRobert Watson KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL")); 1372c37256eSGarrett Wollman TCPDEBUG1(); 1382c37256eSGarrett Wollman 13956dc72c3SPawel Jakub Dawidek error = tcp_attach(so); 1402c37256eSGarrett Wollman if (error) 1412c37256eSGarrett Wollman goto out; 1422c37256eSGarrett Wollman 1432c37256eSGarrett Wollman if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1443879597fSAndrey A. Chernov so->so_linger = TCP_LINGERTIME; 145f76fcf6dSJeffrey Hsu 146f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 147f76fcf6dSJeffrey Hsu tp = intotcpcb(inp); 1482c37256eSGarrett Wollman out: 1492c37256eSGarrett Wollman TCPDEBUG2(PRU_ATTACH); 1505d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ATTACH); 1512c37256eSGarrett Wollman return error; 1522c37256eSGarrett Wollman } 1532c37256eSGarrett Wollman 1542c37256eSGarrett Wollman /* 155a152f8a3SRobert Watson * tcp_detach is called when the socket layer loses its final reference 156a152f8a3SRobert Watson * to the socket, be it a file descriptor reference, a reference from TCP, 157a152f8a3SRobert Watson * etc. At this point, there is only one case in which we will keep around 158a152f8a3SRobert Watson * inpcb state: time wait. 159c78cbc7bSRobert Watson * 160a152f8a3SRobert Watson * This function can probably be re-absorbed back into tcp_usr_detach() now 161a152f8a3SRobert Watson * that there is a single detach path. 1622c37256eSGarrett Wollman */ 163bc725eafSRobert Watson static void 164c78cbc7bSRobert Watson tcp_detach(struct socket *so, struct inpcb *inp) 1652c37256eSGarrett Wollman { 1662c37256eSGarrett Wollman struct tcpcb *tp; 1672c37256eSGarrett Wollman 168079672cbSJulien Charbon INP_INFO_LOCK_ASSERT(&V_tcbinfo); 1698501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 170623dce13SRobert Watson 171c78cbc7bSRobert Watson KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp")); 172c78cbc7bSRobert Watson KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so")); 173953b5606SRobert Watson 174a152f8a3SRobert Watson tp = intotcpcb(inp); 175a152f8a3SRobert Watson 176ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 177623dce13SRobert Watson /* 178a152f8a3SRobert Watson * There are two cases to handle: one in which the time wait 179a152f8a3SRobert Watson * state is being discarded (INP_DROPPED), and one in which 180a152f8a3SRobert Watson * this connection will remain in timewait. In the former, 181a152f8a3SRobert Watson * it is time to discard all state (except tcptw, which has 182a152f8a3SRobert Watson * already been discarded by the timewait close code, which 183a152f8a3SRobert Watson * should be further up the call stack somewhere). In the 184a152f8a3SRobert Watson * latter case, we detach from the socket, but leave the pcb 185a152f8a3SRobert Watson * present until timewait ends. 186623dce13SRobert Watson * 187a152f8a3SRobert Watson * XXXRW: Would it be cleaner to free the tcptw here? 188cea40c48SJulien Charbon * 189cea40c48SJulien Charbon * Astute question indeed, from twtcp perspective there are 190cea40c48SJulien Charbon * three cases to consider: 191cea40c48SJulien Charbon * 192cea40c48SJulien Charbon * #1 tcp_detach is called at tcptw creation time by 193cea40c48SJulien Charbon * tcp_twstart, then do not discard the newly created tcptw 194cea40c48SJulien Charbon * and leave inpcb present until timewait ends 195cea40c48SJulien Charbon * #2 tcp_detach is called at timewait end (or reuse) by 196cea40c48SJulien Charbon * tcp_twclose, then the tcptw has already been discarded 197ff9b006dSJulien Charbon * (or reused) and inpcb is freed here 198cea40c48SJulien Charbon * #3 tcp_detach is called() after timewait ends (or reuse) 199cea40c48SJulien Charbon * (e.g. by soclose), then tcptw has already been discarded 200ff9b006dSJulien Charbon * (or reused) and inpcb is freed here 201cea40c48SJulien Charbon * 202cea40c48SJulien Charbon * In all three cases the tcptw should not be freed here. 203623dce13SRobert Watson */ 204ad71fe3cSRobert Watson if (inp->inp_flags & INP_DROPPED) { 205a152f8a3SRobert Watson KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && " 206a152f8a3SRobert Watson "INP_DROPPED && tp != NULL")); 207623dce13SRobert Watson in_pcbdetach(inp); 2080206cdb8SBjoern A. Zeeb in_pcbfree(inp); 2090206cdb8SBjoern A. Zeeb } else { 210623dce13SRobert Watson in_pcbdetach(inp); 2118501a69cSRobert Watson INP_WUNLOCK(inp); 212623dce13SRobert Watson } 213623dce13SRobert Watson } else { 214e6e65783SRobert Watson /* 215a152f8a3SRobert Watson * If the connection is not in timewait, we consider two 216a152f8a3SRobert Watson * two conditions: one in which no further processing is 217a152f8a3SRobert Watson * necessary (dropped || embryonic), and one in which TCP is 218a152f8a3SRobert Watson * not yet done, but no longer requires the socket, so the 219a152f8a3SRobert Watson * pcb will persist for the time being. 220a152f8a3SRobert Watson * 221a152f8a3SRobert Watson * XXXRW: Does the second case still occur? 222e6e65783SRobert Watson */ 223ad71fe3cSRobert Watson if (inp->inp_flags & INP_DROPPED || 224623dce13SRobert Watson tp->t_state < TCPS_SYN_SENT) { 225623dce13SRobert Watson tcp_discardcb(tp); 226623dce13SRobert Watson in_pcbdetach(inp); 2270206cdb8SBjoern A. Zeeb in_pcbfree(inp); 228db3cee51SNavdeep Parhar } else { 229a152f8a3SRobert Watson in_pcbdetach(inp); 230db3cee51SNavdeep Parhar INP_WUNLOCK(inp); 231db3cee51SNavdeep Parhar } 232623dce13SRobert Watson } 233623dce13SRobert Watson } 234c78cbc7bSRobert Watson 235c78cbc7bSRobert Watson /* 236c78cbc7bSRobert Watson * pru_detach() detaches the TCP protocol from the socket. 237c78cbc7bSRobert Watson * If the protocol state is non-embryonic, then can't 238c78cbc7bSRobert Watson * do this directly: have to initiate a pru_disconnect(), 239c78cbc7bSRobert Watson * which may finish later; embryonic TCB's can just 240c78cbc7bSRobert Watson * be discarded here. 241c78cbc7bSRobert Watson */ 242c78cbc7bSRobert Watson static void 243c78cbc7bSRobert Watson tcp_usr_detach(struct socket *so) 244c78cbc7bSRobert Watson { 245c78cbc7bSRobert Watson struct inpcb *inp; 246079672cbSJulien Charbon int rlock = 0; 247c78cbc7bSRobert Watson 248c78cbc7bSRobert Watson inp = sotoinpcb(so); 249c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL")); 250079672cbSJulien Charbon if (!INP_INFO_WLOCKED(&V_tcbinfo)) { 251ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 252079672cbSJulien Charbon rlock = 1; 253079672cbSJulien Charbon } 2548501a69cSRobert Watson INP_WLOCK(inp); 255c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 256c78cbc7bSRobert Watson ("tcp_usr_detach: inp_socket == NULL")); 257c78cbc7bSRobert Watson tcp_detach(so, inp); 258079672cbSJulien Charbon if (rlock) 259ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 2602c37256eSGarrett Wollman } 2612c37256eSGarrett Wollman 262b287c6c7SBjoern A. Zeeb #ifdef INET 2632c37256eSGarrett Wollman /* 2642c37256eSGarrett Wollman * Give the socket an address. 2652c37256eSGarrett Wollman */ 2662c37256eSGarrett Wollman static int 267b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 2682c37256eSGarrett Wollman { 2692c37256eSGarrett Wollman int error = 0; 270f76fcf6dSJeffrey Hsu struct inpcb *inp; 271623dce13SRobert Watson struct tcpcb *tp = NULL; 2722c37256eSGarrett Wollman struct sockaddr_in *sinp; 2732c37256eSGarrett Wollman 27452710de1SPawel Jakub Dawidek sinp = (struct sockaddr_in *)nam; 27552710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof (*sinp)) 27652710de1SPawel Jakub Dawidek return (EINVAL); 2772c37256eSGarrett Wollman /* 2782c37256eSGarrett Wollman * Must check for multicast addresses and disallow binding 2792c37256eSGarrett Wollman * to them. 2802c37256eSGarrett Wollman */ 2812c37256eSGarrett Wollman if (sinp->sin_family == AF_INET && 28252710de1SPawel Jakub Dawidek IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 28352710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 28452710de1SPawel Jakub Dawidek 285623dce13SRobert Watson TCPDEBUG0; 286623dce13SRobert Watson inp = sotoinpcb(so); 287623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 2888501a69cSRobert Watson INP_WLOCK(inp); 289ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 290623dce13SRobert Watson error = EINVAL; 2912c37256eSGarrett Wollman goto out; 292623dce13SRobert Watson } 293623dce13SRobert Watson tp = intotcpcb(inp); 294623dce13SRobert Watson TCPDEBUG1(); 295fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 296623dce13SRobert Watson error = in_pcbbind(inp, nam, td->td_ucred); 297fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 298623dce13SRobert Watson out: 299623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 3005d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 3018501a69cSRobert Watson INP_WUNLOCK(inp); 302623dce13SRobert Watson 303623dce13SRobert Watson return (error); 3042c37256eSGarrett Wollman } 305b287c6c7SBjoern A. Zeeb #endif /* INET */ 3062c37256eSGarrett Wollman 307fb59c426SYoshinobu Inoue #ifdef INET6 308fb59c426SYoshinobu Inoue static int 309b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 310fb59c426SYoshinobu Inoue { 311fb59c426SYoshinobu Inoue int error = 0; 312f76fcf6dSJeffrey Hsu struct inpcb *inp; 313623dce13SRobert Watson struct tcpcb *tp = NULL; 314fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 315fb59c426SYoshinobu Inoue 31652710de1SPawel Jakub Dawidek sin6p = (struct sockaddr_in6 *)nam; 31752710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof (*sin6p)) 31852710de1SPawel Jakub Dawidek return (EINVAL); 319fb59c426SYoshinobu Inoue /* 320fb59c426SYoshinobu Inoue * Must check for multicast addresses and disallow binding 321fb59c426SYoshinobu Inoue * to them. 322fb59c426SYoshinobu Inoue */ 323fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 && 32452710de1SPawel Jakub Dawidek IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 32552710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 32652710de1SPawel Jakub Dawidek 327623dce13SRobert Watson TCPDEBUG0; 328623dce13SRobert Watson inp = sotoinpcb(so); 329623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 3308501a69cSRobert Watson INP_WLOCK(inp); 331ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 332623dce13SRobert Watson error = EINVAL; 333623dce13SRobert Watson goto out; 334623dce13SRobert Watson } 335623dce13SRobert Watson tp = intotcpcb(inp); 336623dce13SRobert Watson TCPDEBUG1(); 337fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 338fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 339fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 340b287c6c7SBjoern A. Zeeb #ifdef INET 34166ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 342fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) 343fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 344fb59c426SYoshinobu Inoue else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 345fb59c426SYoshinobu Inoue struct sockaddr_in sin; 346fb59c426SYoshinobu Inoue 347fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 348fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 349fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 350b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)&sin, 351b0330ed9SPawel Jakub Dawidek td->td_ucred); 352fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 353fb59c426SYoshinobu Inoue goto out; 354fb59c426SYoshinobu Inoue } 355fb59c426SYoshinobu Inoue } 356b287c6c7SBjoern A. Zeeb #endif 357b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, nam, td->td_ucred); 358fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 359623dce13SRobert Watson out: 360623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 3615d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_BIND); 3628501a69cSRobert Watson INP_WUNLOCK(inp); 363623dce13SRobert Watson return (error); 364fb59c426SYoshinobu Inoue } 365fb59c426SYoshinobu Inoue #endif /* INET6 */ 366fb59c426SYoshinobu Inoue 367b287c6c7SBjoern A. Zeeb #ifdef INET 3682c37256eSGarrett Wollman /* 3692c37256eSGarrett Wollman * Prepare to accept connections. 3702c37256eSGarrett Wollman */ 3712c37256eSGarrett Wollman static int 372d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td) 3732c37256eSGarrett Wollman { 3742c37256eSGarrett Wollman int error = 0; 375f76fcf6dSJeffrey Hsu struct inpcb *inp; 376623dce13SRobert Watson struct tcpcb *tp = NULL; 3772c37256eSGarrett Wollman 378623dce13SRobert Watson TCPDEBUG0; 379623dce13SRobert Watson inp = sotoinpcb(so); 380623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 3818501a69cSRobert Watson INP_WLOCK(inp); 382ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 383623dce13SRobert Watson error = EINVAL; 384623dce13SRobert Watson goto out; 385623dce13SRobert Watson } 386623dce13SRobert Watson tp = intotcpcb(inp); 387623dce13SRobert Watson TCPDEBUG1(); 3880daccb9cSRobert Watson SOCK_LOCK(so); 3890daccb9cSRobert Watson error = solisten_proto_check(so); 390fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 3910daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) 392b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 393fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 3940daccb9cSRobert Watson if (error == 0) { 39557f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 396d374e81eSRobert Watson solisten_proto(so, backlog); 39709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 39837cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 39909fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 40009fe6320SNavdeep Parhar #endif 4010daccb9cSRobert Watson } 4020daccb9cSRobert Watson SOCK_UNLOCK(so); 403623dce13SRobert Watson 404623dce13SRobert Watson out: 405623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 4065d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 4078501a69cSRobert Watson INP_WUNLOCK(inp); 408623dce13SRobert Watson return (error); 4092c37256eSGarrett Wollman } 410b287c6c7SBjoern A. Zeeb #endif /* INET */ 4112c37256eSGarrett Wollman 412fb59c426SYoshinobu Inoue #ifdef INET6 413fb59c426SYoshinobu Inoue static int 414d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 415fb59c426SYoshinobu Inoue { 416fb59c426SYoshinobu Inoue int error = 0; 417f76fcf6dSJeffrey Hsu struct inpcb *inp; 418623dce13SRobert Watson struct tcpcb *tp = NULL; 419fb59c426SYoshinobu Inoue 420623dce13SRobert Watson TCPDEBUG0; 421623dce13SRobert Watson inp = sotoinpcb(so); 422623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL")); 4238501a69cSRobert Watson INP_WLOCK(inp); 424ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 425623dce13SRobert Watson error = EINVAL; 426623dce13SRobert Watson goto out; 427623dce13SRobert Watson } 428623dce13SRobert Watson tp = intotcpcb(inp); 429623dce13SRobert Watson TCPDEBUG1(); 4300daccb9cSRobert Watson SOCK_LOCK(so); 4310daccb9cSRobert Watson error = solisten_proto_check(so); 432fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 4330daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) { 434fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 43566ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 436fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 437b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 438fb59c426SYoshinobu Inoue } 439fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 4400daccb9cSRobert Watson if (error == 0) { 44157f60867SMark Johnston tcp_state_change(tp, TCPS_LISTEN); 442d374e81eSRobert Watson solisten_proto(so, backlog); 44309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 44437cc0ecbSNavdeep Parhar if ((so->so_options & SO_NO_OFFLOAD) == 0) 44509fe6320SNavdeep Parhar tcp_offload_listen_start(tp); 44609fe6320SNavdeep Parhar #endif 4470daccb9cSRobert Watson } 4480daccb9cSRobert Watson SOCK_UNLOCK(so); 449623dce13SRobert Watson 450623dce13SRobert Watson out: 451623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 4525d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_LISTEN); 4538501a69cSRobert Watson INP_WUNLOCK(inp); 454623dce13SRobert Watson return (error); 455fb59c426SYoshinobu Inoue } 456fb59c426SYoshinobu Inoue #endif /* INET6 */ 457fb59c426SYoshinobu Inoue 458b287c6c7SBjoern A. Zeeb #ifdef INET 4592c37256eSGarrett Wollman /* 4602c37256eSGarrett Wollman * Initiate connection to peer. 4612c37256eSGarrett Wollman * Create a template for use in transmissions on this connection. 4622c37256eSGarrett Wollman * Enter SYN_SENT state, and mark socket as connecting. 4632c37256eSGarrett Wollman * Start keep-alive timer, and seed output sequence space. 4642c37256eSGarrett Wollman * Send initial segment on connection. 4652c37256eSGarrett Wollman */ 4662c37256eSGarrett Wollman static int 467b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 4682c37256eSGarrett Wollman { 4692c37256eSGarrett Wollman int error = 0; 470f76fcf6dSJeffrey Hsu struct inpcb *inp; 471623dce13SRobert Watson struct tcpcb *tp = NULL; 4722c37256eSGarrett Wollman struct sockaddr_in *sinp; 4732c37256eSGarrett Wollman 47457bf258eSGarrett Wollman sinp = (struct sockaddr_in *)nam; 475e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sinp)) 476e29ef13fSDon Lewis return (EINVAL); 47752710de1SPawel Jakub Dawidek /* 47852710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 47952710de1SPawel Jakub Dawidek */ 4802c37256eSGarrett Wollman if (sinp->sin_family == AF_INET 48152710de1SPawel Jakub Dawidek && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 48252710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 483b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0) 484b89e82ddSJamie Gritton return (error); 48575c13541SPoul-Henning Kamp 486623dce13SRobert Watson TCPDEBUG0; 487623dce13SRobert Watson inp = sotoinpcb(so); 488623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 4898501a69cSRobert Watson INP_WLOCK(inp); 490eb96dc33SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) { 491eb96dc33SJulien Charbon error = EADDRINUSE; 492eb96dc33SJulien Charbon goto out; 493eb96dc33SJulien Charbon } 494eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 495eb96dc33SJulien Charbon error = ECONNREFUSED; 496623dce13SRobert Watson goto out; 497623dce13SRobert Watson } 498623dce13SRobert Watson tp = intotcpcb(inp); 499623dce13SRobert Watson TCPDEBUG1(); 500b40ce416SJulian Elischer if ((error = tcp_connect(tp, nam, td)) != 0) 5012c37256eSGarrett Wollman goto out; 50209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 50309fe6320SNavdeep Parhar if (registered_toedevs > 0 && 50437cc0ecbSNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 50509fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 50609fe6320SNavdeep Parhar goto out; 50709fe6320SNavdeep Parhar #endif 50809fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 50909fe6320SNavdeep Parhar error = tcp_output(tp); 510623dce13SRobert Watson out: 511623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 5128501a69cSRobert Watson INP_WUNLOCK(inp); 513623dce13SRobert Watson return (error); 5142c37256eSGarrett Wollman } 515b287c6c7SBjoern A. Zeeb #endif /* INET */ 5162c37256eSGarrett Wollman 517fb59c426SYoshinobu Inoue #ifdef INET6 518fb59c426SYoshinobu Inoue static int 519b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 520fb59c426SYoshinobu Inoue { 521fb59c426SYoshinobu Inoue int error = 0; 522f76fcf6dSJeffrey Hsu struct inpcb *inp; 523623dce13SRobert Watson struct tcpcb *tp = NULL; 524fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 525623dce13SRobert Watson 526623dce13SRobert Watson TCPDEBUG0; 527fb59c426SYoshinobu Inoue 528fb59c426SYoshinobu Inoue sin6p = (struct sockaddr_in6 *)nam; 529e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sin6p)) 530e29ef13fSDon Lewis return (EINVAL); 53152710de1SPawel Jakub Dawidek /* 53252710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 53352710de1SPawel Jakub Dawidek */ 534fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 53552710de1SPawel Jakub Dawidek && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 53652710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 537fb59c426SYoshinobu Inoue 538623dce13SRobert Watson inp = sotoinpcb(so); 539623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 5408501a69cSRobert Watson INP_WLOCK(inp); 541eb96dc33SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) { 542eb96dc33SJulien Charbon error = EADDRINUSE; 543eb96dc33SJulien Charbon goto out; 544eb96dc33SJulien Charbon } 545eb96dc33SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 546eb96dc33SJulien Charbon error = ECONNREFUSED; 547623dce13SRobert Watson goto out; 548623dce13SRobert Watson } 549623dce13SRobert Watson tp = intotcpcb(inp); 550623dce13SRobert Watson TCPDEBUG1(); 551b287c6c7SBjoern A. Zeeb #ifdef INET 552fa046d87SRobert Watson /* 553fa046d87SRobert Watson * XXXRW: Some confusion: V4/V6 flags relate to binding, and 554fa046d87SRobert Watson * therefore probably require the hash lock, which isn't held here. 555fa046d87SRobert Watson * Is this a significant problem? 556fa046d87SRobert Watson */ 55733841545SHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 558fb59c426SYoshinobu Inoue struct sockaddr_in sin; 559fb59c426SYoshinobu Inoue 560d46a5312SMaxim Konovalov if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 561d46a5312SMaxim Konovalov error = EINVAL; 562d46a5312SMaxim Konovalov goto out; 563d46a5312SMaxim Konovalov } 56433841545SHajimu UMEMOTO 565fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 566fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 567fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 568b89e82ddSJamie Gritton if ((error = prison_remote_ip4(td->td_ucred, 569b89e82ddSJamie Gritton &sin.sin_addr)) != 0) 570413628a7SBjoern A. Zeeb goto out; 571b40ce416SJulian Elischer if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 572fb59c426SYoshinobu Inoue goto out; 57309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 57409fe6320SNavdeep Parhar if (registered_toedevs > 0 && 575adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 57609fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 57709fe6320SNavdeep Parhar goto out; 57809fe6320SNavdeep Parhar #endif 57909fe6320SNavdeep Parhar error = tcp_output(tp); 580fb59c426SYoshinobu Inoue goto out; 581fb59c426SYoshinobu Inoue } 582b287c6c7SBjoern A. Zeeb #endif 583fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 584fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 585dcdb4371SBjoern A. Zeeb inp->inp_inc.inc_flags |= INC_ISIPV6; 586b89e82ddSJamie Gritton if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0) 587413628a7SBjoern A. Zeeb goto out; 588b40ce416SJulian Elischer if ((error = tcp6_connect(tp, nam, td)) != 0) 589fb59c426SYoshinobu Inoue goto out; 59009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 59109fe6320SNavdeep Parhar if (registered_toedevs > 0 && 592adfaf8f6SNavdeep Parhar (so->so_options & SO_NO_OFFLOAD) == 0 && 59309fe6320SNavdeep Parhar (error = tcp_offload_connect(so, nam)) == 0) 59409fe6320SNavdeep Parhar goto out; 59509fe6320SNavdeep Parhar #endif 59609fe6320SNavdeep Parhar tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 59709fe6320SNavdeep Parhar error = tcp_output(tp); 598623dce13SRobert Watson 599623dce13SRobert Watson out: 600623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 6015d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CONNECT); 6028501a69cSRobert Watson INP_WUNLOCK(inp); 603623dce13SRobert Watson return (error); 604fb59c426SYoshinobu Inoue } 605fb59c426SYoshinobu Inoue #endif /* INET6 */ 606fb59c426SYoshinobu Inoue 6072c37256eSGarrett Wollman /* 6082c37256eSGarrett Wollman * Initiate disconnect from peer. 6092c37256eSGarrett Wollman * If connection never passed embryonic stage, just drop; 6102c37256eSGarrett Wollman * else if don't need to let data drain, then can just drop anyways, 6112c37256eSGarrett Wollman * else have to begin TCP shutdown process: mark socket disconnecting, 6122c37256eSGarrett Wollman * drain unread data, state switch to reflect user close, and 6132c37256eSGarrett Wollman * send segment (e.g. FIN) to peer. Socket will be really disconnected 6142c37256eSGarrett Wollman * when peer sends FIN and acks ours. 6152c37256eSGarrett Wollman * 6162c37256eSGarrett Wollman * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 6172c37256eSGarrett Wollman */ 6182c37256eSGarrett Wollman static int 6192c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so) 6202c37256eSGarrett Wollman { 621f76fcf6dSJeffrey Hsu struct inpcb *inp; 622623dce13SRobert Watson struct tcpcb *tp = NULL; 623623dce13SRobert Watson int error = 0; 6242c37256eSGarrett Wollman 625623dce13SRobert Watson TCPDEBUG0; 626ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 627623dce13SRobert Watson inp = sotoinpcb(so); 628623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 6298501a69cSRobert Watson INP_WLOCK(inp); 630489dcc92SJulien Charbon if (inp->inp_flags & INP_TIMEWAIT) 631489dcc92SJulien Charbon goto out; 632489dcc92SJulien Charbon if (inp->inp_flags & INP_DROPPED) { 63321367f63SSam Leffler error = ECONNRESET; 634623dce13SRobert Watson goto out; 635623dce13SRobert Watson } 636623dce13SRobert Watson tp = intotcpcb(inp); 637623dce13SRobert Watson TCPDEBUG1(); 638623dce13SRobert Watson tcp_disconnect(tp); 639623dce13SRobert Watson out: 640623dce13SRobert Watson TCPDEBUG2(PRU_DISCONNECT); 6415d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_DISCONNECT); 6428501a69cSRobert Watson INP_WUNLOCK(inp); 643ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 644623dce13SRobert Watson return (error); 6452c37256eSGarrett Wollman } 6462c37256eSGarrett Wollman 647b287c6c7SBjoern A. Zeeb #ifdef INET 6482c37256eSGarrett Wollman /* 6498296cddfSRobert Watson * Accept a connection. Essentially all the work is done at higher levels; 6508296cddfSRobert Watson * just return the address of the peer, storing through addr. 6512c37256eSGarrett Wollman */ 6522c37256eSGarrett Wollman static int 65357bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam) 6542c37256eSGarrett Wollman { 6552c37256eSGarrett Wollman int error = 0; 656f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 6571db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 65826ef6ac4SDon Lewis struct in_addr addr; 65926ef6ac4SDon Lewis in_port_t port = 0; 6601db24ffbSJonathan Lemon TCPDEBUG0; 6612c37256eSGarrett Wollman 6623d2d3ef4SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 6633d2d3ef4SRobert Watson return (ECONNABORTED); 664f76fcf6dSJeffrey Hsu 665f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 666623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 6678501a69cSRobert Watson INP_WLOCK(inp); 668ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 6693d2d3ef4SRobert Watson error = ECONNABORTED; 670623dce13SRobert Watson goto out; 671623dce13SRobert Watson } 6721db24ffbSJonathan Lemon tp = intotcpcb(inp); 6731db24ffbSJonathan Lemon TCPDEBUG1(); 674f76fcf6dSJeffrey Hsu 675f76fcf6dSJeffrey Hsu /* 67654d642bbSRobert Watson * We inline in_getpeeraddr and COMMON_END here, so that we can 67726ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 67826ef6ac4SDon Lewis * release the lock. 679f76fcf6dSJeffrey Hsu */ 68026ef6ac4SDon Lewis port = inp->inp_fport; 68126ef6ac4SDon Lewis addr = inp->inp_faddr; 682f76fcf6dSJeffrey Hsu 683623dce13SRobert Watson out: 684623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 6855d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 6868501a69cSRobert Watson INP_WUNLOCK(inp); 68726ef6ac4SDon Lewis if (error == 0) 68826ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 68926ef6ac4SDon Lewis return error; 6902c37256eSGarrett Wollman } 691b287c6c7SBjoern A. Zeeb #endif /* INET */ 6922c37256eSGarrett Wollman 693fb59c426SYoshinobu Inoue #ifdef INET6 694fb59c426SYoshinobu Inoue static int 695fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 696fb59c426SYoshinobu Inoue { 697f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 698fb59c426SYoshinobu Inoue int error = 0; 6991db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 70026ef6ac4SDon Lewis struct in_addr addr; 70126ef6ac4SDon Lewis struct in6_addr addr6; 70226ef6ac4SDon Lewis in_port_t port = 0; 70326ef6ac4SDon Lewis int v4 = 0; 7041db24ffbSJonathan Lemon TCPDEBUG0; 705fb59c426SYoshinobu Inoue 706b4470c16SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 707b4470c16SRobert Watson return (ECONNABORTED); 708f76fcf6dSJeffrey Hsu 709f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 710623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 711fa046d87SRobert Watson INP_INFO_RLOCK(&V_tcbinfo); 7128501a69cSRobert Watson INP_WLOCK(inp); 713ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 71421367f63SSam Leffler error = ECONNABORTED; 715623dce13SRobert Watson goto out; 716623dce13SRobert Watson } 7171db24ffbSJonathan Lemon tp = intotcpcb(inp); 7181db24ffbSJonathan Lemon TCPDEBUG1(); 719623dce13SRobert Watson 72026ef6ac4SDon Lewis /* 72126ef6ac4SDon Lewis * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 72226ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 72326ef6ac4SDon Lewis * release the lock. 72426ef6ac4SDon Lewis */ 72526ef6ac4SDon Lewis if (inp->inp_vflag & INP_IPV4) { 72626ef6ac4SDon Lewis v4 = 1; 72726ef6ac4SDon Lewis port = inp->inp_fport; 72826ef6ac4SDon Lewis addr = inp->inp_faddr; 72926ef6ac4SDon Lewis } else { 73026ef6ac4SDon Lewis port = inp->inp_fport; 73126ef6ac4SDon Lewis addr6 = inp->in6p_faddr; 73226ef6ac4SDon Lewis } 73326ef6ac4SDon Lewis 734623dce13SRobert Watson out: 735623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 7365d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 7378501a69cSRobert Watson INP_WUNLOCK(inp); 738fa046d87SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 73926ef6ac4SDon Lewis if (error == 0) { 74026ef6ac4SDon Lewis if (v4) 74126ef6ac4SDon Lewis *nam = in6_v4mapsin6_sockaddr(port, &addr); 74226ef6ac4SDon Lewis else 74326ef6ac4SDon Lewis *nam = in6_sockaddr(port, &addr6); 74426ef6ac4SDon Lewis } 74526ef6ac4SDon Lewis return error; 746fb59c426SYoshinobu Inoue } 747fb59c426SYoshinobu Inoue #endif /* INET6 */ 748f76fcf6dSJeffrey Hsu 749f76fcf6dSJeffrey Hsu /* 7502c37256eSGarrett Wollman * Mark the connection as being incapable of further output. 7512c37256eSGarrett Wollman */ 7522c37256eSGarrett Wollman static int 7532c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so) 7542c37256eSGarrett Wollman { 7552c37256eSGarrett Wollman int error = 0; 756f76fcf6dSJeffrey Hsu struct inpcb *inp; 757623dce13SRobert Watson struct tcpcb *tp = NULL; 7582c37256eSGarrett Wollman 759623dce13SRobert Watson TCPDEBUG0; 760ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 761623dce13SRobert Watson inp = sotoinpcb(so); 762623dce13SRobert Watson KASSERT(inp != NULL, ("inp == NULL")); 7638501a69cSRobert Watson INP_WLOCK(inp); 764ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 76521367f63SSam Leffler error = ECONNRESET; 766623dce13SRobert Watson goto out; 767623dce13SRobert Watson } 768623dce13SRobert Watson tp = intotcpcb(inp); 769623dce13SRobert Watson TCPDEBUG1(); 7702c37256eSGarrett Wollman socantsendmore(so); 771623dce13SRobert Watson tcp_usrclosed(tp); 772ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 77309fe6320SNavdeep Parhar error = tcp_output(tp); 774623dce13SRobert Watson 775623dce13SRobert Watson out: 776623dce13SRobert Watson TCPDEBUG2(PRU_SHUTDOWN); 7775d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN); 7788501a69cSRobert Watson INP_WUNLOCK(inp); 779ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 780623dce13SRobert Watson 781623dce13SRobert Watson return (error); 7822c37256eSGarrett Wollman } 7832c37256eSGarrett Wollman 7842c37256eSGarrett Wollman /* 7852c37256eSGarrett Wollman * After a receive, possibly send window update to peer. 7862c37256eSGarrett Wollman */ 7872c37256eSGarrett Wollman static int 7882c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags) 7892c37256eSGarrett Wollman { 790f76fcf6dSJeffrey Hsu struct inpcb *inp; 791623dce13SRobert Watson struct tcpcb *tp = NULL; 792623dce13SRobert Watson int error = 0; 7932c37256eSGarrett Wollman 794623dce13SRobert Watson TCPDEBUG0; 795623dce13SRobert Watson inp = sotoinpcb(so); 796623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 7978501a69cSRobert Watson INP_WLOCK(inp); 798ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 79921367f63SSam Leffler error = ECONNRESET; 800623dce13SRobert Watson goto out; 801623dce13SRobert Watson } 802623dce13SRobert Watson tp = intotcpcb(inp); 803623dce13SRobert Watson TCPDEBUG1(); 80409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 80509fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) 80609fe6320SNavdeep Parhar tcp_offload_rcvd(tp); 807460cf046SNavdeep Parhar else 80809fe6320SNavdeep Parhar #endif 80909fe6320SNavdeep Parhar tcp_output(tp); 810623dce13SRobert Watson 811623dce13SRobert Watson out: 812623dce13SRobert Watson TCPDEBUG2(PRU_RCVD); 8135d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVD); 8148501a69cSRobert Watson INP_WUNLOCK(inp); 815623dce13SRobert Watson return (error); 8162c37256eSGarrett Wollman } 8172c37256eSGarrett Wollman 8182c37256eSGarrett Wollman /* 8192c37256eSGarrett Wollman * Do a send by putting data in output queue and updating urgent 8209c9906e9SPeter Wemm * marker if URG set. Possibly send more data. Unlike the other 8219c9906e9SPeter Wemm * pru_*() routines, the mbuf chains are our responsibility. We 8229c9906e9SPeter Wemm * must either enqueue them or free them. The other pru_* routines 8239c9906e9SPeter Wemm * generally are caller-frees. 8242c37256eSGarrett Wollman */ 8252c37256eSGarrett Wollman static int 82657bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 827b40ce416SJulian Elischer struct sockaddr *nam, struct mbuf *control, struct thread *td) 8282c37256eSGarrett Wollman { 8292c37256eSGarrett Wollman int error = 0; 830f76fcf6dSJeffrey Hsu struct inpcb *inp; 831623dce13SRobert Watson struct tcpcb *tp = NULL; 832fb59c426SYoshinobu Inoue #ifdef INET6 833fb59c426SYoshinobu Inoue int isipv6; 834fb59c426SYoshinobu Inoue #endif 8359c9906e9SPeter Wemm TCPDEBUG0; 8362c37256eSGarrett Wollman 837f76fcf6dSJeffrey Hsu /* 838fa046d87SRobert Watson * We require the pcbinfo lock if we will close the socket as part of 839fa046d87SRobert Watson * this call. 840f76fcf6dSJeffrey Hsu */ 841fa046d87SRobert Watson if (flags & PRUS_EOF) 842ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 843f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 844623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 8458501a69cSRobert Watson INP_WLOCK(inp); 846ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 8477ff0b850SAndre Oppermann if (control) 8487ff0b850SAndre Oppermann m_freem(control); 8492cbcd3c1SGleb Smirnoff /* 8502cbcd3c1SGleb Smirnoff * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible 8512cbcd3c1SGleb Smirnoff * for freeing memory. 8522cbcd3c1SGleb Smirnoff */ 8532cbcd3c1SGleb Smirnoff if (m && (flags & PRUS_NOTREADY) == 0) 8547ff0b850SAndre Oppermann m_freem(m); 85521367f63SSam Leffler error = ECONNRESET; 8569c9906e9SPeter Wemm goto out; 8579c9906e9SPeter Wemm } 858fb59c426SYoshinobu Inoue #ifdef INET6 859fb59c426SYoshinobu Inoue isipv6 = nam && nam->sa_family == AF_INET6; 860fb59c426SYoshinobu Inoue #endif /* INET6 */ 8619c9906e9SPeter Wemm tp = intotcpcb(inp); 8629c9906e9SPeter Wemm TCPDEBUG1(); 8639c9906e9SPeter Wemm if (control) { 8649c9906e9SPeter Wemm /* TCP doesn't do control messages (rights, creds, etc) */ 8659c9906e9SPeter Wemm if (control->m_len) { 8669c9906e9SPeter Wemm m_freem(control); 8672c37256eSGarrett Wollman if (m) 8682c37256eSGarrett Wollman m_freem(m); 869744f87eaSDavid Greenman error = EINVAL; 870744f87eaSDavid Greenman goto out; 8712c37256eSGarrett Wollman } 8729c9906e9SPeter Wemm m_freem(control); /* empty control, just free it */ 8739c9906e9SPeter Wemm } 8742c37256eSGarrett Wollman if (!(flags & PRUS_OOB)) { 875651e4e6aSGleb Smirnoff sbappendstream(&so->so_snd, m, flags); 8762c37256eSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 8772c37256eSGarrett Wollman /* 8782c37256eSGarrett Wollman * Do implied connect if not yet connected, 8792c37256eSGarrett Wollman * initialize window to default value, and 8802c37256eSGarrett Wollman * initialize maxseg/maxopd using peer's cached 8812c37256eSGarrett Wollman * MSS. 8822c37256eSGarrett Wollman */ 883fb59c426SYoshinobu Inoue #ifdef INET6 884fb59c426SYoshinobu Inoue if (isipv6) 885b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 886fb59c426SYoshinobu Inoue #endif /* INET6 */ 887b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 888b287c6c7SBjoern A. Zeeb else 889b287c6c7SBjoern A. Zeeb #endif 890b287c6c7SBjoern A. Zeeb #ifdef INET 891b40ce416SJulian Elischer error = tcp_connect(tp, nam, td); 892b287c6c7SBjoern A. Zeeb #endif 8932c37256eSGarrett Wollman if (error) 8942c37256eSGarrett Wollman goto out; 8952c37256eSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 8962c37256eSGarrett Wollman tcp_mss(tp, -1); 8972c37256eSGarrett Wollman } 8982c37256eSGarrett Wollman if (flags & PRUS_EOF) { 8992c37256eSGarrett Wollman /* 9002c37256eSGarrett Wollman * Close the send side of the connection after 9012c37256eSGarrett Wollman * the data is sent. 9022c37256eSGarrett Wollman */ 903ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 9042c37256eSGarrett Wollman socantsendmore(so); 905623dce13SRobert Watson tcp_usrclosed(tp); 9062c37256eSGarrett Wollman } 9072cbcd3c1SGleb Smirnoff if (!(inp->inp_flags & INP_DROPPED) && 9082cbcd3c1SGleb Smirnoff !(flags & PRUS_NOTREADY)) { 909b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 910b0acefa8SBill Fenner tp->t_flags |= TF_MORETOCOME; 91109fe6320SNavdeep Parhar error = tcp_output(tp); 912b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 913b0acefa8SBill Fenner tp->t_flags &= ~TF_MORETOCOME; 914b0acefa8SBill Fenner } 9152c37256eSGarrett Wollman } else { 916623dce13SRobert Watson /* 917623dce13SRobert Watson * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 918623dce13SRobert Watson */ 919d2bc35abSRobert Watson SOCKBUF_LOCK(&so->so_snd); 9202c37256eSGarrett Wollman if (sbspace(&so->so_snd) < -512) { 921d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 9222c37256eSGarrett Wollman m_freem(m); 9232c37256eSGarrett Wollman error = ENOBUFS; 9242c37256eSGarrett Wollman goto out; 9252c37256eSGarrett Wollman } 9262c37256eSGarrett Wollman /* 9272c37256eSGarrett Wollman * According to RFC961 (Assigned Protocols), 9282c37256eSGarrett Wollman * the urgent pointer points to the last octet 9292c37256eSGarrett Wollman * of urgent data. We continue, however, 9302c37256eSGarrett Wollman * to consider it to indicate the first octet 9312c37256eSGarrett Wollman * of data past the urgent section. 9322c37256eSGarrett Wollman * Otherwise, snd_up should be one lower. 9332c37256eSGarrett Wollman */ 934651e4e6aSGleb Smirnoff sbappendstream_locked(&so->so_snd, m, flags); 935d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 936ef53690bSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 937ef53690bSGarrett Wollman /* 938ef53690bSGarrett Wollman * Do implied connect if not yet connected, 939ef53690bSGarrett Wollman * initialize window to default value, and 940ef53690bSGarrett Wollman * initialize maxseg/maxopd using peer's cached 941ef53690bSGarrett Wollman * MSS. 942ef53690bSGarrett Wollman */ 943fb59c426SYoshinobu Inoue #ifdef INET6 944fb59c426SYoshinobu Inoue if (isipv6) 945b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 946fb59c426SYoshinobu Inoue #endif /* INET6 */ 947b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 948b287c6c7SBjoern A. Zeeb else 949b287c6c7SBjoern A. Zeeb #endif 950b287c6c7SBjoern A. Zeeb #ifdef INET 951b40ce416SJulian Elischer error = tcp_connect(tp, nam, td); 952b287c6c7SBjoern A. Zeeb #endif 953ef53690bSGarrett Wollman if (error) 954ef53690bSGarrett Wollman goto out; 955ef53690bSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 956ef53690bSGarrett Wollman tcp_mss(tp, -1); 957623dce13SRobert Watson } 958300fa232SGleb Smirnoff tp->snd_up = tp->snd_una + sbavail(&so->so_snd); 9592cbcd3c1SGleb Smirnoff if (!(flags & PRUS_NOTREADY)) { 9602cdbfa66SPaul Saab tp->t_flags |= TF_FORCEDATA; 96109fe6320SNavdeep Parhar error = tcp_output(tp); 9622cdbfa66SPaul Saab tp->t_flags &= ~TF_FORCEDATA; 9632c37256eSGarrett Wollman } 9642cbcd3c1SGleb Smirnoff } 965d1401c90SRobert Watson out: 966d1401c90SRobert Watson TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB : 9672c37256eSGarrett Wollman ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 9685d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB : 9695d06879aSGeorge V. Neville-Neil ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 9708501a69cSRobert Watson INP_WUNLOCK(inp); 971fa046d87SRobert Watson if (flags & PRUS_EOF) 972ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 97373fddedaSPeter Grehan return (error); 9742c37256eSGarrett Wollman } 9752c37256eSGarrett Wollman 9762cbcd3c1SGleb Smirnoff static int 9772cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count) 9782cbcd3c1SGleb Smirnoff { 9792cbcd3c1SGleb Smirnoff struct inpcb *inp; 9802cbcd3c1SGleb Smirnoff struct tcpcb *tp; 9812cbcd3c1SGleb Smirnoff int error; 9822cbcd3c1SGleb Smirnoff 9832cbcd3c1SGleb Smirnoff inp = sotoinpcb(so); 9842cbcd3c1SGleb Smirnoff INP_WLOCK(inp); 9852cbcd3c1SGleb Smirnoff if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 9862cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 9872cbcd3c1SGleb Smirnoff for (int i = 0; i < count; i++) 9882cbcd3c1SGleb Smirnoff m = m_free(m); 9892cbcd3c1SGleb Smirnoff return (ECONNRESET); 9902cbcd3c1SGleb Smirnoff } 9912cbcd3c1SGleb Smirnoff tp = intotcpcb(inp); 9922cbcd3c1SGleb Smirnoff 9932cbcd3c1SGleb Smirnoff SOCKBUF_LOCK(&so->so_snd); 9942cbcd3c1SGleb Smirnoff error = sbready(&so->so_snd, m, count); 9952cbcd3c1SGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd); 9962cbcd3c1SGleb Smirnoff if (error == 0) 9972cbcd3c1SGleb Smirnoff error = tcp_output(tp); 9982cbcd3c1SGleb Smirnoff INP_WUNLOCK(inp); 9992cbcd3c1SGleb Smirnoff 10002cbcd3c1SGleb Smirnoff return (error); 10012cbcd3c1SGleb Smirnoff } 10022cbcd3c1SGleb Smirnoff 10032c37256eSGarrett Wollman /* 1004a152f8a3SRobert Watson * Abort the TCP. Drop the connection abruptly. 10052c37256eSGarrett Wollman */ 1006ac45e92fSRobert Watson static void 10072c37256eSGarrett Wollman tcp_usr_abort(struct socket *so) 10082c37256eSGarrett Wollman { 1009f76fcf6dSJeffrey Hsu struct inpcb *inp; 1010a152f8a3SRobert Watson struct tcpcb *tp = NULL; 1011623dce13SRobert Watson TCPDEBUG0; 1012c78cbc7bSRobert Watson 1013ac45e92fSRobert Watson inp = sotoinpcb(so); 1014c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 1015c78cbc7bSRobert Watson 1016ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 10178501a69cSRobert Watson INP_WLOCK(inp); 1018c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 1019c78cbc7bSRobert Watson ("tcp_usr_abort: inp_socket == NULL")); 1020c78cbc7bSRobert Watson 1021c78cbc7bSRobert Watson /* 1022a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, drop. 1023c78cbc7bSRobert Watson */ 1024ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_TIMEWAIT) && 1025ad71fe3cSRobert Watson !(inp->inp_flags & INP_DROPPED)) { 1026c78cbc7bSRobert Watson tp = intotcpcb(inp); 1027a152f8a3SRobert Watson TCPDEBUG1(); 1028c78cbc7bSRobert Watson tcp_drop(tp, ECONNABORTED); 1029a152f8a3SRobert Watson TCPDEBUG2(PRU_ABORT); 10305d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_ABORT); 1031c78cbc7bSRobert Watson } 1032ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1033a152f8a3SRobert Watson SOCK_LOCK(so); 1034a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 1035a152f8a3SRobert Watson SOCK_UNLOCK(so); 1036ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1037a152f8a3SRobert Watson } 10388501a69cSRobert Watson INP_WUNLOCK(inp); 1039ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1040a152f8a3SRobert Watson } 1041a152f8a3SRobert Watson 1042a152f8a3SRobert Watson /* 1043a152f8a3SRobert Watson * TCP socket is closed. Start friendly disconnect. 1044a152f8a3SRobert Watson */ 1045a152f8a3SRobert Watson static void 1046a152f8a3SRobert Watson tcp_usr_close(struct socket *so) 1047a152f8a3SRobert Watson { 1048a152f8a3SRobert Watson struct inpcb *inp; 1049a152f8a3SRobert Watson struct tcpcb *tp = NULL; 1050a152f8a3SRobert Watson TCPDEBUG0; 1051a152f8a3SRobert Watson 1052a152f8a3SRobert Watson inp = sotoinpcb(so); 1053a152f8a3SRobert Watson KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL")); 1054a152f8a3SRobert Watson 1055ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 10568501a69cSRobert Watson INP_WLOCK(inp); 1057a152f8a3SRobert Watson KASSERT(inp->inp_socket != NULL, 1058a152f8a3SRobert Watson ("tcp_usr_close: inp_socket == NULL")); 1059a152f8a3SRobert Watson 1060a152f8a3SRobert Watson /* 1061a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, initiate 1062a152f8a3SRobert Watson * a disconnect. 1063a152f8a3SRobert Watson */ 1064ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_TIMEWAIT) && 1065ad71fe3cSRobert Watson !(inp->inp_flags & INP_DROPPED)) { 1066a152f8a3SRobert Watson tp = intotcpcb(inp); 1067a152f8a3SRobert Watson TCPDEBUG1(); 1068a152f8a3SRobert Watson tcp_disconnect(tp); 1069a152f8a3SRobert Watson TCPDEBUG2(PRU_CLOSE); 10705d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_CLOSE); 1071a152f8a3SRobert Watson } 1072ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) { 1073a152f8a3SRobert Watson SOCK_LOCK(so); 1074a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 1075a152f8a3SRobert Watson SOCK_UNLOCK(so); 1076ad71fe3cSRobert Watson inp->inp_flags |= INP_SOCKREF; 1077a152f8a3SRobert Watson } 10788501a69cSRobert Watson INP_WUNLOCK(inp); 1079ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 10802c37256eSGarrett Wollman } 10812c37256eSGarrett Wollman 10822c37256eSGarrett Wollman /* 10832c37256eSGarrett Wollman * Receive out-of-band data. 10842c37256eSGarrett Wollman */ 10852c37256eSGarrett Wollman static int 10862c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 10872c37256eSGarrett Wollman { 10882c37256eSGarrett Wollman int error = 0; 1089f76fcf6dSJeffrey Hsu struct inpcb *inp; 1090623dce13SRobert Watson struct tcpcb *tp = NULL; 10912c37256eSGarrett Wollman 1092623dce13SRobert Watson TCPDEBUG0; 1093623dce13SRobert Watson inp = sotoinpcb(so); 1094623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 10958501a69cSRobert Watson INP_WLOCK(inp); 1096ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 109721367f63SSam Leffler error = ECONNRESET; 1098623dce13SRobert Watson goto out; 1099623dce13SRobert Watson } 1100623dce13SRobert Watson tp = intotcpcb(inp); 1101623dce13SRobert Watson TCPDEBUG1(); 11022c37256eSGarrett Wollman if ((so->so_oobmark == 0 && 1103c0b99ffaSRobert Watson (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 11044cc20ab1SSeigo Tanimura so->so_options & SO_OOBINLINE || 11054cc20ab1SSeigo Tanimura tp->t_oobflags & TCPOOB_HADDATA) { 11062c37256eSGarrett Wollman error = EINVAL; 11072c37256eSGarrett Wollman goto out; 11082c37256eSGarrett Wollman } 11092c37256eSGarrett Wollman if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 11102c37256eSGarrett Wollman error = EWOULDBLOCK; 11112c37256eSGarrett Wollman goto out; 11122c37256eSGarrett Wollman } 11132c37256eSGarrett Wollman m->m_len = 1; 11142c37256eSGarrett Wollman *mtod(m, caddr_t) = tp->t_iobc; 11152c37256eSGarrett Wollman if ((flags & MSG_PEEK) == 0) 11162c37256eSGarrett Wollman tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1117623dce13SRobert Watson 1118623dce13SRobert Watson out: 1119623dce13SRobert Watson TCPDEBUG2(PRU_RCVOOB); 11205d06879aSGeorge V. Neville-Neil TCP_PROBE2(debug__user, tp, PRU_RCVOOB); 11218501a69cSRobert Watson INP_WUNLOCK(inp); 1122623dce13SRobert Watson return (error); 11232c37256eSGarrett Wollman } 11242c37256eSGarrett Wollman 1125b287c6c7SBjoern A. Zeeb #ifdef INET 11262c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = { 1127756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1128756d52a1SPoul-Henning Kamp .pru_accept = tcp_usr_accept, 1129756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1130756d52a1SPoul-Henning Kamp .pru_bind = tcp_usr_bind, 1131756d52a1SPoul-Henning Kamp .pru_connect = tcp_usr_connect, 1132756d52a1SPoul-Henning Kamp .pru_control = in_control, 1133756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1134756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1135756d52a1SPoul-Henning Kamp .pru_listen = tcp_usr_listen, 113654d642bbSRobert Watson .pru_peeraddr = in_getpeeraddr, 1137756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1138756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1139756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 11402cbcd3c1SGleb Smirnoff .pru_ready = tcp_usr_ready, 1141756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 114254d642bbSRobert Watson .pru_sockaddr = in_getsockaddr, 1143a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1144a152f8a3SRobert Watson .pru_close = tcp_usr_close, 11452c37256eSGarrett Wollman }; 1146b287c6c7SBjoern A. Zeeb #endif /* INET */ 1147df8bae1dSRodney W. Grimes 1148fb59c426SYoshinobu Inoue #ifdef INET6 1149fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = { 1150756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1151756d52a1SPoul-Henning Kamp .pru_accept = tcp6_usr_accept, 1152756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1153756d52a1SPoul-Henning Kamp .pru_bind = tcp6_usr_bind, 1154756d52a1SPoul-Henning Kamp .pru_connect = tcp6_usr_connect, 1155756d52a1SPoul-Henning Kamp .pru_control = in6_control, 1156756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1157756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1158756d52a1SPoul-Henning Kamp .pru_listen = tcp6_usr_listen, 1159756d52a1SPoul-Henning Kamp .pru_peeraddr = in6_mapped_peeraddr, 1160756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1161756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1162756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 11632cbcd3c1SGleb Smirnoff .pru_ready = tcp_usr_ready, 1164756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 1165756d52a1SPoul-Henning Kamp .pru_sockaddr = in6_mapped_sockaddr, 1166a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1167a152f8a3SRobert Watson .pru_close = tcp_usr_close, 1168fb59c426SYoshinobu Inoue }; 1169fb59c426SYoshinobu Inoue #endif /* INET6 */ 1170fb59c426SYoshinobu Inoue 1171b287c6c7SBjoern A. Zeeb #ifdef INET 1172a0292f23SGarrett Wollman /* 1173a0292f23SGarrett Wollman * Common subroutine to open a TCP connection to remote host specified 1174a0292f23SGarrett Wollman * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 11755200e00eSIan Dowse * port number if needed. Call in_pcbconnect_setup to do the routing and 11765200e00eSIan Dowse * to choose a local host address (interface). If there is an existing 11775200e00eSIan Dowse * incarnation of the same connection in TIME-WAIT state and if the remote 11785200e00eSIan Dowse * host was sending CC options and if the connection duration was < MSL, then 1179a0292f23SGarrett Wollman * truncate the previous TIME-WAIT state and proceed. 1180a0292f23SGarrett Wollman * Initialize connection parameters and enter SYN-SENT state. 1181a0292f23SGarrett Wollman */ 11820312fbe9SPoul-Henning Kamp static int 1183ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1184a0292f23SGarrett Wollman { 1185a0292f23SGarrett Wollman struct inpcb *inp = tp->t_inpcb, *oinp; 1186a0292f23SGarrett Wollman struct socket *so = inp->inp_socket; 11875200e00eSIan Dowse struct in_addr laddr; 11885200e00eSIan Dowse u_short lport; 1189c3229e05SDavid Greenman int error; 1190a0292f23SGarrett Wollman 11918501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1192fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 1193623dce13SRobert Watson 1194a0292f23SGarrett Wollman if (inp->inp_lport == 0) { 1195b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1196a0292f23SGarrett Wollman if (error) 1197fa046d87SRobert Watson goto out; 1198a0292f23SGarrett Wollman } 1199a0292f23SGarrett Wollman 1200a0292f23SGarrett Wollman /* 1201a0292f23SGarrett Wollman * Cannot simply call in_pcbconnect, because there might be an 1202a0292f23SGarrett Wollman * earlier incarnation of this same connection still in 1203a0292f23SGarrett Wollman * TIME_WAIT state, creating an ADDRINUSE error. 1204a0292f23SGarrett Wollman */ 12055200e00eSIan Dowse laddr = inp->inp_laddr; 12065200e00eSIan Dowse lport = inp->inp_lport; 12075200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 1208b0330ed9SPawel Jakub Dawidek &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 12095200e00eSIan Dowse if (error && oinp == NULL) 1210fa046d87SRobert Watson goto out; 1211fa046d87SRobert Watson if (oinp) { 1212fa046d87SRobert Watson error = EADDRINUSE; 1213fa046d87SRobert Watson goto out; 1214fa046d87SRobert Watson } 12155200e00eSIan Dowse inp->inp_laddr = laddr; 121615bd2b43SDavid Greenman in_pcbrehash(inp); 1217fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1218a0292f23SGarrett Wollman 1219087b55eaSAndre Oppermann /* 1220087b55eaSAndre Oppermann * Compute window scaling to request: 1221087b55eaSAndre Oppermann * Scale to fit into sweet spot. See tcp_syncache.c. 1222087b55eaSAndre Oppermann * XXX: This should move to tcp_output(). 1223087b55eaSAndre Oppermann */ 1224a0292f23SGarrett Wollman while (tp->request_r_scale < TCP_MAX_WINSHIFT && 12259b3bc6bfSMike Silbersack (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1226a0292f23SGarrett Wollman tp->request_r_scale++; 1227a0292f23SGarrett Wollman 1228a0292f23SGarrett Wollman soisconnecting(so); 122978b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 123057f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 1231b0e3ad75SMike Silbersack tp->iss = tcp_new_isn(tp); 1232a0292f23SGarrett Wollman tcp_sendseqinit(tp); 1233a45d2726SAndras Olah 1234a0292f23SGarrett Wollman return 0; 1235fa046d87SRobert Watson 1236fa046d87SRobert Watson out: 1237fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1238fa046d87SRobert Watson return (error); 1239a0292f23SGarrett Wollman } 1240b287c6c7SBjoern A. Zeeb #endif /* INET */ 1241a0292f23SGarrett Wollman 1242fb59c426SYoshinobu Inoue #ifdef INET6 1243fb59c426SYoshinobu Inoue static int 1244ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1245fb59c426SYoshinobu Inoue { 1246a7e201bbSAndrey V. Elsukov struct inpcb *inp = tp->t_inpcb; 1247fb59c426SYoshinobu Inoue int error; 1248fb59c426SYoshinobu Inoue 12498501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1250fa046d87SRobert Watson INP_HASH_WLOCK(&V_tcbinfo); 1251623dce13SRobert Watson 1252fb59c426SYoshinobu Inoue if (inp->inp_lport == 0) { 1253b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1254fb59c426SYoshinobu Inoue if (error) 1255fa046d87SRobert Watson goto out; 1256fb59c426SYoshinobu Inoue } 1257a7e201bbSAndrey V. Elsukov error = in6_pcbconnect(inp, nam, td->td_ucred); 1258a7e201bbSAndrey V. Elsukov if (error != 0) 1259b598155aSRobert Watson goto out; 1260fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1261fb59c426SYoshinobu Inoue 1262fb59c426SYoshinobu Inoue /* Compute window scaling to request. */ 1263fb59c426SYoshinobu Inoue while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1264970caf60SBjoern A. Zeeb (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1265fb59c426SYoshinobu Inoue tp->request_r_scale++; 1266fb59c426SYoshinobu Inoue 1267a7e201bbSAndrey V. Elsukov soisconnecting(inp->inp_socket); 126878b50714SRobert Watson TCPSTAT_INC(tcps_connattempt); 126957f60867SMark Johnston tcp_state_change(tp, TCPS_SYN_SENT); 1270b0e3ad75SMike Silbersack tp->iss = tcp_new_isn(tp); 1271fb59c426SYoshinobu Inoue tcp_sendseqinit(tp); 1272fb59c426SYoshinobu Inoue 1273fb59c426SYoshinobu Inoue return 0; 1274fa046d87SRobert Watson 1275fa046d87SRobert Watson out: 1276fa046d87SRobert Watson INP_HASH_WUNLOCK(&V_tcbinfo); 1277fa046d87SRobert Watson return error; 1278fb59c426SYoshinobu Inoue } 1279fb59c426SYoshinobu Inoue #endif /* INET6 */ 1280fb59c426SYoshinobu Inoue 1281cfe8b629SGarrett Wollman /* 1282b8af5dfaSRobert Watson * Export TCP internal state information via a struct tcp_info, based on the 1283b8af5dfaSRobert Watson * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1284b8af5dfaSRobert Watson * (TCP state machine, etc). We export all information using FreeBSD-native 1285b8af5dfaSRobert Watson * constants -- for example, the numeric values for tcpi_state will differ 1286b8af5dfaSRobert Watson * from Linux. 1287b8af5dfaSRobert Watson */ 1288b8af5dfaSRobert Watson static void 1289ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) 1290b8af5dfaSRobert Watson { 1291b8af5dfaSRobert Watson 12928501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1293b8af5dfaSRobert Watson bzero(ti, sizeof(*ti)); 1294b8af5dfaSRobert Watson 1295b8af5dfaSRobert Watson ti->tcpi_state = tp->t_state; 1296b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1297b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 12983529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 1299b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_SACK; 1300b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1301b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_WSCALE; 1302b8af5dfaSRobert Watson ti->tcpi_snd_wscale = tp->snd_scale; 1303b8af5dfaSRobert Watson ti->tcpi_rcv_wscale = tp->rcv_scale; 1304b8af5dfaSRobert Watson } 13051baaf834SBruce M Simpson 130643d94734SJohn Baldwin ti->tcpi_rto = tp->t_rxtcur * tick; 130743d94734SJohn Baldwin ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick; 13081baaf834SBruce M Simpson ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 13091baaf834SBruce M Simpson ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT; 13101baaf834SBruce M Simpson 1311b8af5dfaSRobert Watson ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1312b8af5dfaSRobert Watson ti->tcpi_snd_cwnd = tp->snd_cwnd; 1313b8af5dfaSRobert Watson 1314b8af5dfaSRobert Watson /* 1315b8af5dfaSRobert Watson * FreeBSD-specific extension fields for tcp_info. 1316b8af5dfaSRobert Watson */ 1317c8443a1dSRobert Watson ti->tcpi_rcv_space = tp->rcv_wnd; 1318535fbad6SKip Macy ti->tcpi_rcv_nxt = tp->rcv_nxt; 1319b8af5dfaSRobert Watson ti->tcpi_snd_wnd = tp->snd_wnd; 13201c18314dSAndre Oppermann ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */ 1321535fbad6SKip Macy ti->tcpi_snd_nxt = tp->snd_nxt; 132243d94734SJohn Baldwin ti->tcpi_snd_mss = tp->t_maxseg; 132343d94734SJohn Baldwin ti->tcpi_rcv_mss = tp->t_maxseg; 1324535fbad6SKip Macy if (tp->t_flags & TF_TOE) 1325535fbad6SKip Macy ti->tcpi_options |= TCPI_OPT_TOE; 1326f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack; 1327f5d34df5SGeorge V. Neville-Neil ti->tcpi_rcv_ooopack = tp->t_rcvoopack; 1328f5d34df5SGeorge V. Neville-Neil ti->tcpi_snd_zerowin = tp->t_sndzerowin; 1329b8af5dfaSRobert Watson } 1330b8af5dfaSRobert Watson 1331b8af5dfaSRobert Watson /* 13321e8f5ffaSRobert Watson * tcp_ctloutput() must drop the inpcb lock before performing copyin on 13331e8f5ffaSRobert Watson * socket option arguments. When it re-acquires the lock after the copy, it 13341e8f5ffaSRobert Watson * has to revalidate that the connection is still valid for the socket 13351e8f5ffaSRobert Watson * option. 1336cfe8b629SGarrett Wollman */ 13378501a69cSRobert Watson #define INP_WLOCK_RECHECK(inp) do { \ 13388501a69cSRobert Watson INP_WLOCK(inp); \ 1339ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \ 13408501a69cSRobert Watson INP_WUNLOCK(inp); \ 13411e8f5ffaSRobert Watson return (ECONNRESET); \ 13421e8f5ffaSRobert Watson } \ 13431e8f5ffaSRobert Watson tp = intotcpcb(inp); \ 13441e8f5ffaSRobert Watson } while(0) 13451e8f5ffaSRobert Watson 1346df8bae1dSRodney W. Grimes int 1347ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1348df8bae1dSRodney W. Grimes { 13493f9d1ef9SRobert Watson int error, opt, optval; 13509077f387SGleb Smirnoff u_int ui; 1351df8bae1dSRodney W. Grimes struct inpcb *inp; 1352cfe8b629SGarrett Wollman struct tcpcb *tp; 1353b8af5dfaSRobert Watson struct tcp_info ti; 1354dbc42409SLawrence Stewart char buf[TCP_CA_NAME_MAX]; 1355dbc42409SLawrence Stewart struct cc_algo *algo; 1356df8bae1dSRodney W. Grimes 1357cfe8b629SGarrett Wollman error = 0; 1358df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1359623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 13608501a69cSRobert Watson INP_WLOCK(inp); 1361cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_TCP) { 1362fb59c426SYoshinobu Inoue #ifdef INET6 13635cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 13648501a69cSRobert Watson INP_WUNLOCK(inp); 1365fb59c426SYoshinobu Inoue error = ip6_ctloutput(so, sopt); 1366b287c6c7SBjoern A. Zeeb } 1367fb59c426SYoshinobu Inoue #endif /* INET6 */ 1368b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1369b287c6c7SBjoern A. Zeeb else 1370b287c6c7SBjoern A. Zeeb #endif 1371b287c6c7SBjoern A. Zeeb #ifdef INET 1372b287c6c7SBjoern A. Zeeb { 13738501a69cSRobert Watson INP_WUNLOCK(inp); 1374cfe8b629SGarrett Wollman error = ip_ctloutput(so, sopt); 13751e8f5ffaSRobert Watson } 13761e8f5ffaSRobert Watson #endif 1377df8bae1dSRodney W. Grimes return (error); 1378df8bae1dSRodney W. Grimes } 1379ad71fe3cSRobert Watson if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 13808501a69cSRobert Watson INP_WUNLOCK(inp); 13811e8f5ffaSRobert Watson return (ECONNRESET); 1382623dce13SRobert Watson } 1383df8bae1dSRodney W. Grimes 1384cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 1385cfe8b629SGarrett Wollman case SOPT_SET: 1386cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 13871cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 138888f6b043SBruce M Simpson case TCP_MD5SIG: 13898501a69cSRobert Watson INP_WUNLOCK(inp); 13901cfd4b53SBruce M Simpson error = sooptcopyin(sopt, &optval, sizeof optval, 13911cfd4b53SBruce M Simpson sizeof optval); 13921cfd4b53SBruce M Simpson if (error) 13931e8f5ffaSRobert Watson return (error); 13941cfd4b53SBruce M Simpson 13958501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 13961cfd4b53SBruce M Simpson if (optval > 0) 13971cfd4b53SBruce M Simpson tp->t_flags |= TF_SIGNATURE; 13981cfd4b53SBruce M Simpson else 13991cfd4b53SBruce M Simpson tp->t_flags &= ~TF_SIGNATURE; 140009fe6320SNavdeep Parhar goto unlock_and_done; 14011cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */ 140209fe6320SNavdeep Parhar 1403df8bae1dSRodney W. Grimes case TCP_NODELAY: 1404cfe8b629SGarrett Wollman case TCP_NOOPT: 14058501a69cSRobert Watson INP_WUNLOCK(inp); 1406cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1407cfe8b629SGarrett Wollman sizeof optval); 1408cfe8b629SGarrett Wollman if (error) 14091e8f5ffaSRobert Watson return (error); 1410cfe8b629SGarrett Wollman 14118501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 1412cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1413cfe8b629SGarrett Wollman case TCP_NODELAY: 1414cfe8b629SGarrett Wollman opt = TF_NODELAY; 1415cfe8b629SGarrett Wollman break; 1416cfe8b629SGarrett Wollman case TCP_NOOPT: 1417cfe8b629SGarrett Wollman opt = TF_NOOPT; 1418cfe8b629SGarrett Wollman break; 1419cfe8b629SGarrett Wollman default: 1420cfe8b629SGarrett Wollman opt = 0; /* dead code to fool gcc */ 1421cfe8b629SGarrett Wollman break; 1422cfe8b629SGarrett Wollman } 1423cfe8b629SGarrett Wollman 1424cfe8b629SGarrett Wollman if (optval) 1425cfe8b629SGarrett Wollman tp->t_flags |= opt; 1426df8bae1dSRodney W. Grimes else 1427cfe8b629SGarrett Wollman tp->t_flags &= ~opt; 142809fe6320SNavdeep Parhar unlock_and_done: 142909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 143009fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) { 143109fe6320SNavdeep Parhar tcp_offload_ctloutput(tp, sopt->sopt_dir, 143209fe6320SNavdeep Parhar sopt->sopt_name); 143309fe6320SNavdeep Parhar } 143409fe6320SNavdeep Parhar #endif 14358501a69cSRobert Watson INP_WUNLOCK(inp); 1436df8bae1dSRodney W. Grimes break; 1437df8bae1dSRodney W. Grimes 1438007581c0SJonathan Lemon case TCP_NOPUSH: 14398501a69cSRobert Watson INP_WUNLOCK(inp); 1440007581c0SJonathan Lemon error = sooptcopyin(sopt, &optval, sizeof optval, 1441007581c0SJonathan Lemon sizeof optval); 1442007581c0SJonathan Lemon if (error) 14431e8f5ffaSRobert Watson return (error); 1444007581c0SJonathan Lemon 14458501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 1446007581c0SJonathan Lemon if (optval) 1447007581c0SJonathan Lemon tp->t_flags |= TF_NOPUSH; 1448d28b9e89SJohn Baldwin else if (tp->t_flags & TF_NOPUSH) { 1449007581c0SJonathan Lemon tp->t_flags &= ~TF_NOPUSH; 1450d28b9e89SJohn Baldwin if (TCPS_HAVEESTABLISHED(tp->t_state)) 1451007581c0SJonathan Lemon error = tcp_output(tp); 1452007581c0SJonathan Lemon } 145309fe6320SNavdeep Parhar goto unlock_and_done; 1454007581c0SJonathan Lemon 1455df8bae1dSRodney W. Grimes case TCP_MAXSEG: 14568501a69cSRobert Watson INP_WUNLOCK(inp); 1457cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1458cfe8b629SGarrett Wollman sizeof optval); 1459cfe8b629SGarrett Wollman if (error) 14601e8f5ffaSRobert Watson return (error); 1461df8bae1dSRodney W. Grimes 14628501a69cSRobert Watson INP_WLOCK_RECHECK(inp); 146353369ac9SAndre Oppermann if (optval > 0 && optval <= tp->t_maxseg && 1464603724d3SBjoern A. Zeeb optval + 40 >= V_tcp_minmss) 1465cfe8b629SGarrett Wollman tp->t_maxseg = optval; 1466a0292f23SGarrett Wollman else 1467a0292f23SGarrett Wollman error = EINVAL; 146809fe6320SNavdeep Parhar goto unlock_and_done; 1469a0292f23SGarrett Wollman 1470b8af5dfaSRobert Watson case TCP_INFO: 14718501a69cSRobert Watson INP_WUNLOCK(inp); 1472b8af5dfaSRobert Watson error = EINVAL; 1473b8af5dfaSRobert Watson break; 1474b8af5dfaSRobert Watson 1475dbc42409SLawrence Stewart case TCP_CONGESTION: 1476dbc42409SLawrence Stewart INP_WUNLOCK(inp); 1477dbc42409SLawrence Stewart bzero(buf, sizeof(buf)); 1478dbc42409SLawrence Stewart error = sooptcopyin(sopt, &buf, sizeof(buf), 1); 1479dbc42409SLawrence Stewart if (error) 1480dbc42409SLawrence Stewart break; 1481dbc42409SLawrence Stewart INP_WLOCK_RECHECK(inp); 1482dbc42409SLawrence Stewart /* 1483dbc42409SLawrence Stewart * Return EINVAL if we can't find the requested cc algo. 1484dbc42409SLawrence Stewart */ 1485dbc42409SLawrence Stewart error = EINVAL; 1486dbc42409SLawrence Stewart CC_LIST_RLOCK(); 1487dbc42409SLawrence Stewart STAILQ_FOREACH(algo, &cc_list, entries) { 1488dbc42409SLawrence Stewart if (strncmp(buf, algo->name, TCP_CA_NAME_MAX) 1489dbc42409SLawrence Stewart == 0) { 1490dbc42409SLawrence Stewart /* We've found the requested algo. */ 1491dbc42409SLawrence Stewart error = 0; 1492dbc42409SLawrence Stewart /* 1493dbc42409SLawrence Stewart * We hold a write lock over the tcb 1494dbc42409SLawrence Stewart * so it's safe to do these things 1495dbc42409SLawrence Stewart * without ordering concerns. 1496dbc42409SLawrence Stewart */ 1497dbc42409SLawrence Stewart if (CC_ALGO(tp)->cb_destroy != NULL) 1498dbc42409SLawrence Stewart CC_ALGO(tp)->cb_destroy(tp->ccv); 1499dbc42409SLawrence Stewart CC_ALGO(tp) = algo; 1500dbc42409SLawrence Stewart /* 1501dbc42409SLawrence Stewart * If something goes pear shaped 1502dbc42409SLawrence Stewart * initialising the new algo, 1503dbc42409SLawrence Stewart * fall back to newreno (which 1504dbc42409SLawrence Stewart * does not require initialisation). 1505dbc42409SLawrence Stewart */ 1506dbc42409SLawrence Stewart if (algo->cb_init != NULL) 1507dbc42409SLawrence Stewart if (algo->cb_init(tp->ccv) > 0) { 1508dbc42409SLawrence Stewart CC_ALGO(tp) = &newreno_cc_algo; 1509dbc42409SLawrence Stewart /* 1510dbc42409SLawrence Stewart * The only reason init 1511dbc42409SLawrence Stewart * should fail is 1512dbc42409SLawrence Stewart * because of malloc. 1513dbc42409SLawrence Stewart */ 1514dbc42409SLawrence Stewart error = ENOMEM; 1515dbc42409SLawrence Stewart } 1516dbc42409SLawrence Stewart break; /* Break the STAILQ_FOREACH. */ 1517dbc42409SLawrence Stewart } 1518dbc42409SLawrence Stewart } 1519dbc42409SLawrence Stewart CC_LIST_RUNLOCK(); 152009fe6320SNavdeep Parhar goto unlock_and_done; 1521dbc42409SLawrence Stewart 15229077f387SGleb Smirnoff case TCP_KEEPIDLE: 15239077f387SGleb Smirnoff case TCP_KEEPINTVL: 15249077f387SGleb Smirnoff case TCP_KEEPINIT: 15259077f387SGleb Smirnoff INP_WUNLOCK(inp); 15269077f387SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 15279077f387SGleb Smirnoff if (error) 15289077f387SGleb Smirnoff return (error); 15299077f387SGleb Smirnoff 15309077f387SGleb Smirnoff if (ui > (UINT_MAX / hz)) { 15319077f387SGleb Smirnoff error = EINVAL; 15329077f387SGleb Smirnoff break; 15339077f387SGleb Smirnoff } 15349077f387SGleb Smirnoff ui *= hz; 15359077f387SGleb Smirnoff 15369077f387SGleb Smirnoff INP_WLOCK_RECHECK(inp); 15379077f387SGleb Smirnoff switch (sopt->sopt_name) { 15389077f387SGleb Smirnoff case TCP_KEEPIDLE: 15399077f387SGleb Smirnoff tp->t_keepidle = ui; 15409077f387SGleb Smirnoff /* 15419077f387SGleb Smirnoff * XXX: better check current remaining 15429077f387SGleb Smirnoff * timeout and "merge" it with new value. 15439077f387SGleb Smirnoff */ 15449077f387SGleb Smirnoff if ((tp->t_state > TCPS_LISTEN) && 15459077f387SGleb Smirnoff (tp->t_state <= TCPS_CLOSING)) 15469077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 15479077f387SGleb Smirnoff TP_KEEPIDLE(tp)); 15489077f387SGleb Smirnoff break; 15499077f387SGleb Smirnoff case TCP_KEEPINTVL: 15509077f387SGleb Smirnoff tp->t_keepintvl = ui; 15519077f387SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 15529077f387SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 15539077f387SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 15549077f387SGleb Smirnoff TP_MAXIDLE(tp)); 15559077f387SGleb Smirnoff break; 15569077f387SGleb Smirnoff case TCP_KEEPINIT: 15579077f387SGleb Smirnoff tp->t_keepinit = ui; 15589077f387SGleb Smirnoff if (tp->t_state == TCPS_SYN_RECEIVED || 15599077f387SGleb Smirnoff tp->t_state == TCPS_SYN_SENT) 15609077f387SGleb Smirnoff tcp_timer_activate(tp, TT_KEEP, 15619077f387SGleb Smirnoff TP_KEEPINIT(tp)); 15629077f387SGleb Smirnoff break; 15639077f387SGleb Smirnoff } 156409fe6320SNavdeep Parhar goto unlock_and_done; 15659077f387SGleb Smirnoff 156685c05144SGleb Smirnoff case TCP_KEEPCNT: 156785c05144SGleb Smirnoff INP_WUNLOCK(inp); 156885c05144SGleb Smirnoff error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 156985c05144SGleb Smirnoff if (error) 157085c05144SGleb Smirnoff return (error); 157185c05144SGleb Smirnoff 157285c05144SGleb Smirnoff INP_WLOCK_RECHECK(inp); 157385c05144SGleb Smirnoff tp->t_keepcnt = ui; 157485c05144SGleb Smirnoff if ((tp->t_state == TCPS_FIN_WAIT_2) && 157585c05144SGleb Smirnoff (TP_MAXIDLE(tp) > 0)) 157685c05144SGleb Smirnoff tcp_timer_activate(tp, TT_2MSL, 157785c05144SGleb Smirnoff TP_MAXIDLE(tp)); 157885c05144SGleb Smirnoff goto unlock_and_done; 157985c05144SGleb Smirnoff 1580df8bae1dSRodney W. Grimes default: 15818501a69cSRobert Watson INP_WUNLOCK(inp); 1582df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1583df8bae1dSRodney W. Grimes break; 1584df8bae1dSRodney W. Grimes } 1585df8bae1dSRodney W. Grimes break; 1586df8bae1dSRodney W. Grimes 1587cfe8b629SGarrett Wollman case SOPT_GET: 15881e8f5ffaSRobert Watson tp = intotcpcb(inp); 1589cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 15901cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 159188f6b043SBruce M Simpson case TCP_MD5SIG: 15921cfd4b53SBruce M Simpson optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0; 15938501a69cSRobert Watson INP_WUNLOCK(inp); 1594b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 15951cfd4b53SBruce M Simpson break; 1596265ed012SBruce M Simpson #endif 15971e8f5ffaSRobert Watson 1598df8bae1dSRodney W. Grimes case TCP_NODELAY: 1599cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NODELAY; 16008501a69cSRobert Watson INP_WUNLOCK(inp); 1601b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1602df8bae1dSRodney W. Grimes break; 1603df8bae1dSRodney W. Grimes case TCP_MAXSEG: 1604cfe8b629SGarrett Wollman optval = tp->t_maxseg; 16058501a69cSRobert Watson INP_WUNLOCK(inp); 1606b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1607df8bae1dSRodney W. Grimes break; 1608a0292f23SGarrett Wollman case TCP_NOOPT: 1609cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOOPT; 16108501a69cSRobert Watson INP_WUNLOCK(inp); 1611b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1612a0292f23SGarrett Wollman break; 1613a0292f23SGarrett Wollman case TCP_NOPUSH: 1614cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOPUSH; 16158501a69cSRobert Watson INP_WUNLOCK(inp); 1616b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1617b8af5dfaSRobert Watson break; 1618b8af5dfaSRobert Watson case TCP_INFO: 1619b8af5dfaSRobert Watson tcp_fill_info(tp, &ti); 16208501a69cSRobert Watson INP_WUNLOCK(inp); 1621b8af5dfaSRobert Watson error = sooptcopyout(sopt, &ti, sizeof ti); 1622a0292f23SGarrett Wollman break; 1623dbc42409SLawrence Stewart case TCP_CONGESTION: 1624dbc42409SLawrence Stewart bzero(buf, sizeof(buf)); 1625dbc42409SLawrence Stewart strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); 1626dbc42409SLawrence Stewart INP_WUNLOCK(inp); 1627dbc42409SLawrence Stewart error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX); 1628dbc42409SLawrence Stewart break; 16292f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 16302f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 16312f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 16322f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 16332f3eb7f4SGleb Smirnoff switch (sopt->sopt_name) { 16342f3eb7f4SGleb Smirnoff case TCP_KEEPIDLE: 16352f3eb7f4SGleb Smirnoff ui = tp->t_keepidle / hz; 16362f3eb7f4SGleb Smirnoff break; 16372f3eb7f4SGleb Smirnoff case TCP_KEEPINTVL: 16382f3eb7f4SGleb Smirnoff ui = tp->t_keepintvl / hz; 16392f3eb7f4SGleb Smirnoff break; 16402f3eb7f4SGleb Smirnoff case TCP_KEEPINIT: 16412f3eb7f4SGleb Smirnoff ui = tp->t_keepinit / hz; 16422f3eb7f4SGleb Smirnoff break; 16432f3eb7f4SGleb Smirnoff case TCP_KEEPCNT: 16442f3eb7f4SGleb Smirnoff ui = tp->t_keepcnt; 16452f3eb7f4SGleb Smirnoff break; 16462f3eb7f4SGleb Smirnoff } 16472f3eb7f4SGleb Smirnoff INP_WUNLOCK(inp); 16482f3eb7f4SGleb Smirnoff error = sooptcopyout(sopt, &ui, sizeof(ui)); 16492f3eb7f4SGleb Smirnoff break; 1650df8bae1dSRodney W. Grimes default: 16518501a69cSRobert Watson INP_WUNLOCK(inp); 1652df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1653df8bae1dSRodney W. Grimes break; 1654df8bae1dSRodney W. Grimes } 1655df8bae1dSRodney W. Grimes break; 1656df8bae1dSRodney W. Grimes } 1657df8bae1dSRodney W. Grimes return (error); 1658df8bae1dSRodney W. Grimes } 16598501a69cSRobert Watson #undef INP_WLOCK_RECHECK 1660df8bae1dSRodney W. Grimes 166126e30fbbSDavid Greenman /* 1662df8bae1dSRodney W. Grimes * Attach TCP protocol to socket, allocating 1663df8bae1dSRodney W. Grimes * internet protocol control block, tcp control block, 1664df8bae1dSRodney W. Grimes * bufer space, and entering LISTEN state if to accept connections. 1665df8bae1dSRodney W. Grimes */ 16660312fbe9SPoul-Henning Kamp static int 1667ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so) 1668df8bae1dSRodney W. Grimes { 1669ad3f9ab3SAndre Oppermann struct tcpcb *tp; 1670df8bae1dSRodney W. Grimes struct inpcb *inp; 1671df8bae1dSRodney W. Grimes int error; 1672df8bae1dSRodney W. Grimes 1673df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1674e233e2acSAndre Oppermann error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace); 1675df8bae1dSRodney W. Grimes if (error) 1676df8bae1dSRodney W. Grimes return (error); 1677df8bae1dSRodney W. Grimes } 16786741ecf5SAndre Oppermann so->so_rcv.sb_flags |= SB_AUTOSIZE; 16796741ecf5SAndre Oppermann so->so_snd.sb_flags |= SB_AUTOSIZE; 1680ff9b006dSJulien Charbon INP_INFO_RLOCK(&V_tcbinfo); 1681603724d3SBjoern A. Zeeb error = in_pcballoc(so, &V_tcbinfo); 1682f2de87feSRobert Watson if (error) { 1683ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1684df8bae1dSRodney W. Grimes return (error); 1685f2de87feSRobert Watson } 1686df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1687fb59c426SYoshinobu Inoue #ifdef INET6 16885cd54324SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6PROTO) { 1689fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 1690fb59c426SYoshinobu Inoue inp->in6p_hops = -1; /* use kernel default */ 1691fb59c426SYoshinobu Inoue } 1692fb59c426SYoshinobu Inoue else 1693fb59c426SYoshinobu Inoue #endif 1694cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 1695df8bae1dSRodney W. Grimes tp = tcp_newtcpcb(inp); 1696623dce13SRobert Watson if (tp == NULL) { 1697df8bae1dSRodney W. Grimes in_pcbdetach(inp); 16980206cdb8SBjoern A. Zeeb in_pcbfree(inp); 1699ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1700df8bae1dSRodney W. Grimes return (ENOBUFS); 1701df8bae1dSRodney W. Grimes } 1702df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 17038501a69cSRobert Watson INP_WUNLOCK(inp); 1704ff9b006dSJulien Charbon INP_INFO_RUNLOCK(&V_tcbinfo); 1705df8bae1dSRodney W. Grimes return (0); 1706df8bae1dSRodney W. Grimes } 1707df8bae1dSRodney W. Grimes 1708df8bae1dSRodney W. Grimes /* 1709df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 1710df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 1711df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 1712df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 1713df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 1714df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 1715df8bae1dSRodney W. Grimes */ 1716623dce13SRobert Watson static void 1717ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp) 1718df8bae1dSRodney W. Grimes { 1719e6e0b5ffSRobert Watson struct inpcb *inp = tp->t_inpcb; 1720e6e0b5ffSRobert Watson struct socket *so = inp->inp_socket; 1721e6e0b5ffSRobert Watson 1722ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 17238501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 1724df8bae1dSRodney W. Grimes 1725623dce13SRobert Watson /* 1726623dce13SRobert Watson * Neither tcp_close() nor tcp_drop() should return NULL, as the 1727623dce13SRobert Watson * socket is still open. 1728623dce13SRobert Watson */ 1729623dce13SRobert Watson if (tp->t_state < TCPS_ESTABLISHED) { 1730df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1731623dce13SRobert Watson KASSERT(tp != NULL, 1732623dce13SRobert Watson ("tcp_disconnect: tcp_close() returned NULL")); 1733623dce13SRobert Watson } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 1734243917feSSeigo Tanimura tp = tcp_drop(tp, 0); 1735623dce13SRobert Watson KASSERT(tp != NULL, 1736623dce13SRobert Watson ("tcp_disconnect: tcp_drop() returned NULL")); 1737623dce13SRobert Watson } else { 1738df8bae1dSRodney W. Grimes soisdisconnecting(so); 1739df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 1740623dce13SRobert Watson tcp_usrclosed(tp); 1741ad71fe3cSRobert Watson if (!(inp->inp_flags & INP_DROPPED)) 174209fe6320SNavdeep Parhar tcp_output(tp); 1743df8bae1dSRodney W. Grimes } 1744df8bae1dSRodney W. Grimes } 1745df8bae1dSRodney W. Grimes 1746df8bae1dSRodney W. Grimes /* 1747df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 1748df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 1749df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1750df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 1751df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 1752df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 1753df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 1754df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 1755df8bae1dSRodney W. Grimes */ 1756623dce13SRobert Watson static void 1757ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp) 1758df8bae1dSRodney W. Grimes { 1759df8bae1dSRodney W. Grimes 1760ff9b006dSJulien Charbon INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 17618501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1762e6e0b5ffSRobert Watson 1763df8bae1dSRodney W. Grimes switch (tp->t_state) { 1764df8bae1dSRodney W. Grimes case TCPS_LISTEN: 176509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 176609fe6320SNavdeep Parhar tcp_offload_listen_stop(tp); 176709fe6320SNavdeep Parhar #endif 1768*550e9d42SHiren Panchasara tcp_state_change(tp, TCPS_CLOSED); 1769bc65987aSKip Macy /* FALLTHROUGH */ 1770bc65987aSKip Macy case TCPS_CLOSED: 1771df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1772623dce13SRobert Watson /* 1773623dce13SRobert Watson * tcp_close() should never return NULL here as the socket is 1774623dce13SRobert Watson * still open. 1775623dce13SRobert Watson */ 1776623dce13SRobert Watson KASSERT(tp != NULL, 1777623dce13SRobert Watson ("tcp_usrclosed: tcp_close() returned NULL")); 1778df8bae1dSRodney W. Grimes break; 1779df8bae1dSRodney W. Grimes 1780a0292f23SGarrett Wollman case TCPS_SYN_SENT: 1781df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1782a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 1783a0292f23SGarrett Wollman break; 1784a0292f23SGarrett Wollman 1785df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 178657f60867SMark Johnston tcp_state_change(tp, TCPS_FIN_WAIT_1); 1787df8bae1dSRodney W. Grimes break; 1788df8bae1dSRodney W. Grimes 1789df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 179057f60867SMark Johnston tcp_state_change(tp, TCPS_LAST_ACK); 1791df8bae1dSRodney W. Grimes break; 1792df8bae1dSRodney W. Grimes } 1793abc7d910SRobert Watson if (tp->t_state >= TCPS_FIN_WAIT_2) { 1794df8bae1dSRodney W. Grimes soisdisconnected(tp->t_inpcb->inp_socket); 1795abc7d910SRobert Watson /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 17967c72af87SMohan Srinivasan if (tp->t_state == TCPS_FIN_WAIT_2) { 17977c72af87SMohan Srinivasan int timeout; 17987c72af87SMohan Srinivasan 17997c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 18009077f387SGleb Smirnoff tcp_finwait2_timeout : TP_MAXIDLE(tp); 1801b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 1802b6239c4aSAndras Olah } 1803df8bae1dSRodney W. Grimes } 18047c72af87SMohan Srinivasan } 1805497057eeSRobert Watson 1806497057eeSRobert Watson #ifdef DDB 1807497057eeSRobert Watson static void 1808497057eeSRobert Watson db_print_indent(int indent) 1809497057eeSRobert Watson { 1810497057eeSRobert Watson int i; 1811497057eeSRobert Watson 1812497057eeSRobert Watson for (i = 0; i < indent; i++) 1813497057eeSRobert Watson db_printf(" "); 1814497057eeSRobert Watson } 1815497057eeSRobert Watson 1816497057eeSRobert Watson static void 1817497057eeSRobert Watson db_print_tstate(int t_state) 1818497057eeSRobert Watson { 1819497057eeSRobert Watson 1820497057eeSRobert Watson switch (t_state) { 1821497057eeSRobert Watson case TCPS_CLOSED: 1822497057eeSRobert Watson db_printf("TCPS_CLOSED"); 1823497057eeSRobert Watson return; 1824497057eeSRobert Watson 1825497057eeSRobert Watson case TCPS_LISTEN: 1826497057eeSRobert Watson db_printf("TCPS_LISTEN"); 1827497057eeSRobert Watson return; 1828497057eeSRobert Watson 1829497057eeSRobert Watson case TCPS_SYN_SENT: 1830497057eeSRobert Watson db_printf("TCPS_SYN_SENT"); 1831497057eeSRobert Watson return; 1832497057eeSRobert Watson 1833497057eeSRobert Watson case TCPS_SYN_RECEIVED: 1834497057eeSRobert Watson db_printf("TCPS_SYN_RECEIVED"); 1835497057eeSRobert Watson return; 1836497057eeSRobert Watson 1837497057eeSRobert Watson case TCPS_ESTABLISHED: 1838497057eeSRobert Watson db_printf("TCPS_ESTABLISHED"); 1839497057eeSRobert Watson return; 1840497057eeSRobert Watson 1841497057eeSRobert Watson case TCPS_CLOSE_WAIT: 1842497057eeSRobert Watson db_printf("TCPS_CLOSE_WAIT"); 1843497057eeSRobert Watson return; 1844497057eeSRobert Watson 1845497057eeSRobert Watson case TCPS_FIN_WAIT_1: 1846497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_1"); 1847497057eeSRobert Watson return; 1848497057eeSRobert Watson 1849497057eeSRobert Watson case TCPS_CLOSING: 1850497057eeSRobert Watson db_printf("TCPS_CLOSING"); 1851497057eeSRobert Watson return; 1852497057eeSRobert Watson 1853497057eeSRobert Watson case TCPS_LAST_ACK: 1854497057eeSRobert Watson db_printf("TCPS_LAST_ACK"); 1855497057eeSRobert Watson return; 1856497057eeSRobert Watson 1857497057eeSRobert Watson case TCPS_FIN_WAIT_2: 1858497057eeSRobert Watson db_printf("TCPS_FIN_WAIT_2"); 1859497057eeSRobert Watson return; 1860497057eeSRobert Watson 1861497057eeSRobert Watson case TCPS_TIME_WAIT: 1862497057eeSRobert Watson db_printf("TCPS_TIME_WAIT"); 1863497057eeSRobert Watson return; 1864497057eeSRobert Watson 1865497057eeSRobert Watson default: 1866497057eeSRobert Watson db_printf("unknown"); 1867497057eeSRobert Watson return; 1868497057eeSRobert Watson } 1869497057eeSRobert Watson } 1870497057eeSRobert Watson 1871497057eeSRobert Watson static void 1872497057eeSRobert Watson db_print_tflags(u_int t_flags) 1873497057eeSRobert Watson { 1874497057eeSRobert Watson int comma; 1875497057eeSRobert Watson 1876497057eeSRobert Watson comma = 0; 1877497057eeSRobert Watson if (t_flags & TF_ACKNOW) { 1878497057eeSRobert Watson db_printf("%sTF_ACKNOW", comma ? ", " : ""); 1879497057eeSRobert Watson comma = 1; 1880497057eeSRobert Watson } 1881497057eeSRobert Watson if (t_flags & TF_DELACK) { 1882497057eeSRobert Watson db_printf("%sTF_DELACK", comma ? ", " : ""); 1883497057eeSRobert Watson comma = 1; 1884497057eeSRobert Watson } 1885497057eeSRobert Watson if (t_flags & TF_NODELAY) { 1886497057eeSRobert Watson db_printf("%sTF_NODELAY", comma ? ", " : ""); 1887497057eeSRobert Watson comma = 1; 1888497057eeSRobert Watson } 1889497057eeSRobert Watson if (t_flags & TF_NOOPT) { 1890497057eeSRobert Watson db_printf("%sTF_NOOPT", comma ? ", " : ""); 1891497057eeSRobert Watson comma = 1; 1892497057eeSRobert Watson } 1893497057eeSRobert Watson if (t_flags & TF_SENTFIN) { 1894497057eeSRobert Watson db_printf("%sTF_SENTFIN", comma ? ", " : ""); 1895497057eeSRobert Watson comma = 1; 1896497057eeSRobert Watson } 1897497057eeSRobert Watson if (t_flags & TF_REQ_SCALE) { 1898497057eeSRobert Watson db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 1899497057eeSRobert Watson comma = 1; 1900497057eeSRobert Watson } 1901497057eeSRobert Watson if (t_flags & TF_RCVD_SCALE) { 1902497057eeSRobert Watson db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 1903497057eeSRobert Watson comma = 1; 1904497057eeSRobert Watson } 1905497057eeSRobert Watson if (t_flags & TF_REQ_TSTMP) { 1906497057eeSRobert Watson db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 1907497057eeSRobert Watson comma = 1; 1908497057eeSRobert Watson } 1909497057eeSRobert Watson if (t_flags & TF_RCVD_TSTMP) { 1910497057eeSRobert Watson db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 1911497057eeSRobert Watson comma = 1; 1912497057eeSRobert Watson } 1913497057eeSRobert Watson if (t_flags & TF_SACK_PERMIT) { 1914497057eeSRobert Watson db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 1915497057eeSRobert Watson comma = 1; 1916497057eeSRobert Watson } 1917497057eeSRobert Watson if (t_flags & TF_NEEDSYN) { 1918497057eeSRobert Watson db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 1919497057eeSRobert Watson comma = 1; 1920497057eeSRobert Watson } 1921497057eeSRobert Watson if (t_flags & TF_NEEDFIN) { 1922497057eeSRobert Watson db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 1923497057eeSRobert Watson comma = 1; 1924497057eeSRobert Watson } 1925497057eeSRobert Watson if (t_flags & TF_NOPUSH) { 1926497057eeSRobert Watson db_printf("%sTF_NOPUSH", comma ? ", " : ""); 1927497057eeSRobert Watson comma = 1; 1928497057eeSRobert Watson } 1929497057eeSRobert Watson if (t_flags & TF_MORETOCOME) { 1930497057eeSRobert Watson db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 1931497057eeSRobert Watson comma = 1; 1932497057eeSRobert Watson } 1933497057eeSRobert Watson if (t_flags & TF_LQ_OVERFLOW) { 1934497057eeSRobert Watson db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : ""); 1935497057eeSRobert Watson comma = 1; 1936497057eeSRobert Watson } 1937497057eeSRobert Watson if (t_flags & TF_LASTIDLE) { 1938497057eeSRobert Watson db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 1939497057eeSRobert Watson comma = 1; 1940497057eeSRobert Watson } 1941497057eeSRobert Watson if (t_flags & TF_RXWIN0SENT) { 1942497057eeSRobert Watson db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 1943497057eeSRobert Watson comma = 1; 1944497057eeSRobert Watson } 1945497057eeSRobert Watson if (t_flags & TF_FASTRECOVERY) { 1946497057eeSRobert Watson db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 1947497057eeSRobert Watson comma = 1; 1948497057eeSRobert Watson } 1949dbc42409SLawrence Stewart if (t_flags & TF_CONGRECOVERY) { 1950dbc42409SLawrence Stewart db_printf("%sTF_CONGRECOVERY", comma ? ", " : ""); 1951dbc42409SLawrence Stewart comma = 1; 1952dbc42409SLawrence Stewart } 1953497057eeSRobert Watson if (t_flags & TF_WASFRECOVERY) { 1954497057eeSRobert Watson db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 1955497057eeSRobert Watson comma = 1; 1956497057eeSRobert Watson } 1957497057eeSRobert Watson if (t_flags & TF_SIGNATURE) { 1958497057eeSRobert Watson db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 1959497057eeSRobert Watson comma = 1; 1960497057eeSRobert Watson } 1961497057eeSRobert Watson if (t_flags & TF_FORCEDATA) { 1962497057eeSRobert Watson db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 1963497057eeSRobert Watson comma = 1; 1964497057eeSRobert Watson } 1965497057eeSRobert Watson if (t_flags & TF_TSO) { 1966497057eeSRobert Watson db_printf("%sTF_TSO", comma ? ", " : ""); 1967497057eeSRobert Watson comma = 1; 1968497057eeSRobert Watson } 1969f2512ba1SRui Paulo if (t_flags & TF_ECN_PERMIT) { 1970f2512ba1SRui Paulo db_printf("%sTF_ECN_PERMIT", comma ? ", " : ""); 1971f2512ba1SRui Paulo comma = 1; 1972f2512ba1SRui Paulo } 1973497057eeSRobert Watson } 1974497057eeSRobert Watson 1975497057eeSRobert Watson static void 1976497057eeSRobert Watson db_print_toobflags(char t_oobflags) 1977497057eeSRobert Watson { 1978497057eeSRobert Watson int comma; 1979497057eeSRobert Watson 1980497057eeSRobert Watson comma = 0; 1981497057eeSRobert Watson if (t_oobflags & TCPOOB_HAVEDATA) { 1982497057eeSRobert Watson db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 1983497057eeSRobert Watson comma = 1; 1984497057eeSRobert Watson } 1985497057eeSRobert Watson if (t_oobflags & TCPOOB_HADDATA) { 1986497057eeSRobert Watson db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 1987497057eeSRobert Watson comma = 1; 1988497057eeSRobert Watson } 1989497057eeSRobert Watson } 1990497057eeSRobert Watson 1991497057eeSRobert Watson static void 1992497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 1993497057eeSRobert Watson { 1994497057eeSRobert Watson 1995497057eeSRobert Watson db_print_indent(indent); 1996497057eeSRobert Watson db_printf("%s at %p\n", name, tp); 1997497057eeSRobert Watson 1998497057eeSRobert Watson indent += 2; 1999497057eeSRobert Watson 2000497057eeSRobert Watson db_print_indent(indent); 2001497057eeSRobert Watson db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 20024741bfcbSPatrick Kelsey LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 2003497057eeSRobert Watson 2004497057eeSRobert Watson db_print_indent(indent); 200585d94372SRobert Watson db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n", 2006e2f2059fSMike Silbersack &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep); 2007497057eeSRobert Watson 2008497057eeSRobert Watson db_print_indent(indent); 2009e2f2059fSMike Silbersack db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl, 2010e2f2059fSMike Silbersack &tp->t_timers->tt_delack, tp->t_inpcb); 2011497057eeSRobert Watson 2012497057eeSRobert Watson db_print_indent(indent); 2013497057eeSRobert Watson db_printf("t_state: %d (", tp->t_state); 2014497057eeSRobert Watson db_print_tstate(tp->t_state); 2015497057eeSRobert Watson db_printf(")\n"); 2016497057eeSRobert Watson 2017497057eeSRobert Watson db_print_indent(indent); 2018497057eeSRobert Watson db_printf("t_flags: 0x%x (", tp->t_flags); 2019497057eeSRobert Watson db_print_tflags(tp->t_flags); 2020497057eeSRobert Watson db_printf(")\n"); 2021497057eeSRobert Watson 2022497057eeSRobert Watson db_print_indent(indent); 2023497057eeSRobert Watson db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n", 2024497057eeSRobert Watson tp->snd_una, tp->snd_max, tp->snd_nxt); 2025497057eeSRobert Watson 2026497057eeSRobert Watson db_print_indent(indent); 2027497057eeSRobert Watson db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 2028497057eeSRobert Watson tp->snd_up, tp->snd_wl1, tp->snd_wl2); 2029497057eeSRobert Watson 2030497057eeSRobert Watson db_print_indent(indent); 2031497057eeSRobert Watson db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 2032497057eeSRobert Watson tp->iss, tp->irs, tp->rcv_nxt); 2033497057eeSRobert Watson 2034497057eeSRobert Watson db_print_indent(indent); 2035497057eeSRobert Watson db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n", 2036497057eeSRobert Watson tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 2037497057eeSRobert Watson 2038497057eeSRobert Watson db_print_indent(indent); 20391c18314dSAndre Oppermann db_printf("snd_wnd: %lu snd_cwnd: %lu\n", 20401c18314dSAndre Oppermann tp->snd_wnd, tp->snd_cwnd); 2041497057eeSRobert Watson 2042497057eeSRobert Watson db_print_indent(indent); 20431c18314dSAndre Oppermann db_printf("snd_ssthresh: %lu snd_recover: " 20441c18314dSAndre Oppermann "0x%08x\n", tp->snd_ssthresh, tp->snd_recover); 2045497057eeSRobert Watson 2046497057eeSRobert Watson db_print_indent(indent); 20479f78a87aSJohn Baldwin db_printf("t_maxopd: %u t_rcvtime: %u t_startime: %u\n", 2048497057eeSRobert Watson tp->t_maxopd, tp->t_rcvtime, tp->t_starttime); 2049497057eeSRobert Watson 2050497057eeSRobert Watson db_print_indent(indent); 20511c18314dSAndre Oppermann db_printf("t_rttime: %u t_rtsq: 0x%08x\n", 20521c18314dSAndre Oppermann tp->t_rtttime, tp->t_rtseq); 2053497057eeSRobert Watson 2054497057eeSRobert Watson db_print_indent(indent); 20551c18314dSAndre Oppermann db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n", 20561c18314dSAndre Oppermann tp->t_rxtcur, tp->t_maxseg, tp->t_srtt); 2057497057eeSRobert Watson 2058497057eeSRobert Watson db_print_indent(indent); 2059497057eeSRobert Watson db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u " 2060497057eeSRobert Watson "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin, 2061497057eeSRobert Watson tp->t_rttbest); 2062497057eeSRobert Watson 2063497057eeSRobert Watson db_print_indent(indent); 2064497057eeSRobert Watson db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n", 2065497057eeSRobert Watson tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 2066497057eeSRobert Watson 2067497057eeSRobert Watson db_print_indent(indent); 2068497057eeSRobert Watson db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 2069497057eeSRobert Watson db_print_toobflags(tp->t_oobflags); 2070497057eeSRobert Watson db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 2071497057eeSRobert Watson 2072497057eeSRobert Watson db_print_indent(indent); 2073497057eeSRobert Watson db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 2074497057eeSRobert Watson tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 2075497057eeSRobert Watson 2076497057eeSRobert Watson db_print_indent(indent); 20779f78a87aSJohn Baldwin db_printf("ts_recent: %u ts_recent_age: %u\n", 20781a553740SAndre Oppermann tp->ts_recent, tp->ts_recent_age); 2079497057eeSRobert Watson 2080497057eeSRobert Watson db_print_indent(indent); 2081497057eeSRobert Watson db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 2082497057eeSRobert Watson "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 2083497057eeSRobert Watson 2084497057eeSRobert Watson db_print_indent(indent); 2085497057eeSRobert Watson db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x " 20869f78a87aSJohn Baldwin "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 2087497057eeSRobert Watson tp->snd_recover_prev, tp->t_badrxtwin); 2088497057eeSRobert Watson 2089497057eeSRobert Watson db_print_indent(indent); 20903529149eSAndre Oppermann db_printf("snd_numholes: %d snd_holes first: %p\n", 20913529149eSAndre Oppermann tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 2092497057eeSRobert Watson 2093497057eeSRobert Watson db_print_indent(indent); 2094497057eeSRobert Watson db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: " 2095497057eeSRobert Watson "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata); 2096497057eeSRobert Watson 2097497057eeSRobert Watson /* Skip sackblks, sackhint. */ 2098497057eeSRobert Watson 2099497057eeSRobert Watson db_print_indent(indent); 2100497057eeSRobert Watson db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 2101497057eeSRobert Watson tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 2102497057eeSRobert Watson } 2103497057eeSRobert Watson 2104497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 2105497057eeSRobert Watson { 2106497057eeSRobert Watson struct tcpcb *tp; 2107497057eeSRobert Watson 2108497057eeSRobert Watson if (!have_addr) { 2109497057eeSRobert Watson db_printf("usage: show tcpcb <addr>\n"); 2110497057eeSRobert Watson return; 2111497057eeSRobert Watson } 2112497057eeSRobert Watson tp = (struct tcpcb *)addr; 2113497057eeSRobert Watson 2114497057eeSRobert Watson db_print_tcpcb(tp, "tcpcb", 0); 2115497057eeSRobert Watson } 2116497057eeSRobert Watson #endif 2117