1c398230bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1993 3623dce13SRobert Watson * The Regents of the University of California. 4623dce13SRobert Watson * Copyright (c) 2006 Robert N. M. Watson 5623dce13SRobert Watson * All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 9df8bae1dSRodney W. Grimes * are met: 10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 15df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 17df8bae1dSRodney W. Grimes * without specific prior written permission. 18df8bae1dSRodney W. Grimes * 19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29df8bae1dSRodney W. Grimes * SUCH DAMAGE. 30df8bae1dSRodney W. Grimes * 311fdbc7aeSGarrett Wollman * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94 32c3aac50fSPeter Wemm * $FreeBSD$ 33df8bae1dSRodney W. Grimes */ 34df8bae1dSRodney W. Grimes 351cfd4b53SBruce M Simpson #include "opt_inet.h" 36fb59c426SYoshinobu Inoue #include "opt_inet6.h" 370cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 380cc12cc5SJoerg Wunsch 39df8bae1dSRodney W. Grimes #include <sys/param.h> 40df8bae1dSRodney W. Grimes #include <sys/systm.h> 41f76fcf6dSJeffrey Hsu #include <sys/malloc.h> 42c7a82f90SGarrett Wollman #include <sys/kernel.h> 4398163b98SPoul-Henning Kamp #include <sys/sysctl.h> 44df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 45fb59c426SYoshinobu Inoue #ifdef INET6 46fb59c426SYoshinobu Inoue #include <sys/domain.h> 47fb59c426SYoshinobu Inoue #endif /* INET6 */ 48df8bae1dSRodney W. Grimes #include <sys/socket.h> 49df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 50df8bae1dSRodney W. Grimes #include <sys/protosw.h> 5191421ba2SRobert Watson #include <sys/proc.h> 5291421ba2SRobert Watson #include <sys/jail.h> 53df8bae1dSRodney W. Grimes 54df8bae1dSRodney W. Grimes #include <net/if.h> 55df8bae1dSRodney W. Grimes #include <net/route.h> 56df8bae1dSRodney W. Grimes 57df8bae1dSRodney W. Grimes #include <netinet/in.h> 58df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 59fb59c426SYoshinobu Inoue #ifdef INET6 60fb59c426SYoshinobu Inoue #include <netinet/ip6.h> 61fb59c426SYoshinobu Inoue #endif 62df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 63fb59c426SYoshinobu Inoue #ifdef INET6 64fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h> 65fb59c426SYoshinobu Inoue #endif 66b5e8ce9fSBruce Evans #include <netinet/in_var.h> 67df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 68fb59c426SYoshinobu Inoue #ifdef INET6 69fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 70a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h> 71fb59c426SYoshinobu Inoue #endif 72df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 73df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 74df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 75df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 76df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 77df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 78610ee2f9SDavid Greenman #ifdef TCPDEBUG 79df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 80610ee2f9SDavid Greenman #endif 81df8bae1dSRodney W. Grimes 82df8bae1dSRodney W. Grimes /* 83df8bae1dSRodney W. Grimes * TCP protocol interface to socket abstraction. 84df8bae1dSRodney W. Grimes */ 85117bcae7SGarrett Wollman extern char *tcpstates[]; /* XXX ??? */ 86df8bae1dSRodney W. Grimes 8756dc72c3SPawel Jakub Dawidek static int tcp_attach(struct socket *); 884d77a549SAlfred Perlstein static int tcp_connect(struct tcpcb *, struct sockaddr *, 894d77a549SAlfred Perlstein struct thread *td); 90fb59c426SYoshinobu Inoue #ifdef INET6 914d77a549SAlfred Perlstein static int tcp6_connect(struct tcpcb *, struct sockaddr *, 924d77a549SAlfred Perlstein struct thread *td); 93fb59c426SYoshinobu Inoue #endif /* INET6 */ 94623dce13SRobert Watson static void tcp_disconnect(struct tcpcb *); 95623dce13SRobert Watson static void tcp_usrclosed(struct tcpcb *); 96b8af5dfaSRobert Watson static void tcp_fill_info(struct tcpcb *, struct tcp_info *); 972c37256eSGarrett Wollman 982c37256eSGarrett Wollman #ifdef TCPDEBUG 991db24ffbSJonathan Lemon #define TCPDEBUG0 int ostate = 0 1002c37256eSGarrett Wollman #define TCPDEBUG1() ostate = tp ? tp->t_state : 0 1014cc20ab1SSeigo Tanimura #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 1024cc20ab1SSeigo Tanimura tcp_trace(TA_USER, ostate, tp, 0, 0, req) 1032c37256eSGarrett Wollman #else 1042c37256eSGarrett Wollman #define TCPDEBUG0 1052c37256eSGarrett Wollman #define TCPDEBUG1() 1062c37256eSGarrett Wollman #define TCPDEBUG2(req) 1072c37256eSGarrett Wollman #endif 1082c37256eSGarrett Wollman 1092c37256eSGarrett Wollman /* 1102c37256eSGarrett Wollman * TCP attaches to socket via pru_attach(), reserving space, 1112c37256eSGarrett Wollman * and an internet control block. 1122c37256eSGarrett Wollman */ 1132c37256eSGarrett Wollman static int 114b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td) 1152c37256eSGarrett Wollman { 116f76fcf6dSJeffrey Hsu struct inpcb *inp; 117623dce13SRobert Watson struct tcpcb *tp = NULL; 118623dce13SRobert Watson int error; 1192c37256eSGarrett Wollman TCPDEBUG0; 1202c37256eSGarrett Wollman 121623dce13SRobert Watson inp = sotoinpcb(so); 122623dce13SRobert Watson KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL")); 1232c37256eSGarrett Wollman TCPDEBUG1(); 1242c37256eSGarrett Wollman 12556dc72c3SPawel Jakub Dawidek error = tcp_attach(so); 1262c37256eSGarrett Wollman if (error) 1272c37256eSGarrett Wollman goto out; 1282c37256eSGarrett Wollman 1292c37256eSGarrett Wollman if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1303879597fSAndrey A. Chernov so->so_linger = TCP_LINGERTIME; 131f76fcf6dSJeffrey Hsu 132f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 133f76fcf6dSJeffrey Hsu tp = intotcpcb(inp); 1342c37256eSGarrett Wollman out: 1352c37256eSGarrett Wollman TCPDEBUG2(PRU_ATTACH); 1362c37256eSGarrett Wollman return error; 1372c37256eSGarrett Wollman } 1382c37256eSGarrett Wollman 1392c37256eSGarrett Wollman /* 140a152f8a3SRobert Watson * tcp_detach is called when the socket layer loses its final reference 141a152f8a3SRobert Watson * to the socket, be it a file descriptor reference, a reference from TCP, 142a152f8a3SRobert Watson * etc. At this point, there is only one case in which we will keep around 143a152f8a3SRobert Watson * inpcb state: time wait. 144c78cbc7bSRobert Watson * 145a152f8a3SRobert Watson * This function can probably be re-absorbed back into tcp_usr_detach() now 146a152f8a3SRobert Watson * that there is a single detach path. 1472c37256eSGarrett Wollman */ 148bc725eafSRobert Watson static void 149c78cbc7bSRobert Watson tcp_detach(struct socket *so, struct inpcb *inp) 1502c37256eSGarrett Wollman { 1512c37256eSGarrett Wollman struct tcpcb *tp; 152623dce13SRobert Watson #ifdef INET6 153623dce13SRobert Watson int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0; 154623dce13SRobert Watson #endif 1552c37256eSGarrett Wollman 156c78cbc7bSRobert Watson INP_INFO_WLOCK_ASSERT(&tcbinfo); 157c78cbc7bSRobert Watson INP_LOCK_ASSERT(inp); 158623dce13SRobert Watson 159c78cbc7bSRobert Watson KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp")); 160c78cbc7bSRobert Watson KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so")); 161953b5606SRobert Watson 162a152f8a3SRobert Watson tp = intotcpcb(inp); 163a152f8a3SRobert Watson 164623dce13SRobert Watson if (inp->inp_vflag & INP_TIMEWAIT) { 165623dce13SRobert Watson /* 166a152f8a3SRobert Watson * There are two cases to handle: one in which the time wait 167a152f8a3SRobert Watson * state is being discarded (INP_DROPPED), and one in which 168a152f8a3SRobert Watson * this connection will remain in timewait. In the former, 169a152f8a3SRobert Watson * it is time to discard all state (except tcptw, which has 170a152f8a3SRobert Watson * already been discarded by the timewait close code, which 171a152f8a3SRobert Watson * should be further up the call stack somewhere). In the 172a152f8a3SRobert Watson * latter case, we detach from the socket, but leave the pcb 173a152f8a3SRobert Watson * present until timewait ends. 174623dce13SRobert Watson * 175a152f8a3SRobert Watson * XXXRW: Would it be cleaner to free the tcptw here? 176623dce13SRobert Watson */ 177a152f8a3SRobert Watson if (inp->inp_vflag & INP_DROPPED) { 178a152f8a3SRobert Watson KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && " 179a152f8a3SRobert Watson "INP_DROPPED && tp != NULL")); 180623dce13SRobert Watson #ifdef INET6 181623dce13SRobert Watson if (isipv6) { 182623dce13SRobert Watson in6_pcbdetach(inp); 183623dce13SRobert Watson in6_pcbfree(inp); 184623dce13SRobert Watson } else { 185623dce13SRobert Watson #endif 186623dce13SRobert Watson in_pcbdetach(inp); 187623dce13SRobert Watson in_pcbfree(inp); 188623dce13SRobert Watson #ifdef INET6 189623dce13SRobert Watson } 190623dce13SRobert Watson #endif 191623dce13SRobert Watson } else { 192623dce13SRobert Watson #ifdef INET6 193623dce13SRobert Watson if (isipv6) 194623dce13SRobert Watson in6_pcbdetach(inp); 195623dce13SRobert Watson else 196623dce13SRobert Watson #endif 197623dce13SRobert Watson in_pcbdetach(inp); 198f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 199623dce13SRobert Watson } 200623dce13SRobert Watson } else { 201e6e65783SRobert Watson /* 202a152f8a3SRobert Watson * If the connection is not in timewait, we consider two 203a152f8a3SRobert Watson * two conditions: one in which no further processing is 204a152f8a3SRobert Watson * necessary (dropped || embryonic), and one in which TCP is 205a152f8a3SRobert Watson * not yet done, but no longer requires the socket, so the 206a152f8a3SRobert Watson * pcb will persist for the time being. 207a152f8a3SRobert Watson * 208a152f8a3SRobert Watson * XXXRW: Does the second case still occur? 209e6e65783SRobert Watson */ 210623dce13SRobert Watson if (inp->inp_vflag & INP_DROPPED || 211623dce13SRobert Watson tp->t_state < TCPS_SYN_SENT) { 212623dce13SRobert Watson tcp_discardcb(tp); 213623dce13SRobert Watson #ifdef INET6 214623dce13SRobert Watson if (isipv6) { 215a152f8a3SRobert Watson in6_pcbdetach(inp); 216a152f8a3SRobert Watson in6_pcbfree(inp); 217623dce13SRobert Watson } else { 218623dce13SRobert Watson #endif 219623dce13SRobert Watson in_pcbdetach(inp); 220623dce13SRobert Watson in_pcbfree(inp); 221623dce13SRobert Watson #ifdef INET6 222623dce13SRobert Watson } 223623dce13SRobert Watson #endif 224623dce13SRobert Watson } else { 225a152f8a3SRobert Watson #ifdef INET6 226a152f8a3SRobert Watson if (isipv6) 227a152f8a3SRobert Watson in6_pcbdetach(inp); 228a152f8a3SRobert Watson else 229a152f8a3SRobert Watson #endif 230a152f8a3SRobert Watson in_pcbdetach(inp); 231623dce13SRobert Watson } 232623dce13SRobert Watson } 233c78cbc7bSRobert 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; 246c78cbc7bSRobert Watson struct tcpcb *tp; 247c78cbc7bSRobert Watson TCPDEBUG0; 248c78cbc7bSRobert Watson 249c78cbc7bSRobert Watson inp = sotoinpcb(so); 250c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL")); 251c78cbc7bSRobert Watson INP_INFO_WLOCK(&tcbinfo); 252c78cbc7bSRobert Watson INP_LOCK(inp); 253c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 254c78cbc7bSRobert Watson ("tcp_usr_detach: inp_socket == NULL")); 255c78cbc7bSRobert Watson TCPDEBUG1(); 256c78cbc7bSRobert Watson 257c78cbc7bSRobert Watson tcp_detach(so, inp); 258623dce13SRobert Watson tp = NULL; 259623dce13SRobert Watson TCPDEBUG2(PRU_DETACH); 260f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2612c37256eSGarrett Wollman } 2622c37256eSGarrett Wollman 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_INFO_WLOCK(&tcbinfo); 287623dce13SRobert Watson inp = sotoinpcb(so); 288623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 289623dce13SRobert Watson INP_LOCK(inp); 290623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 291623dce13SRobert Watson error = EINVAL; 2922c37256eSGarrett Wollman goto out; 293623dce13SRobert Watson } 294623dce13SRobert Watson tp = intotcpcb(inp); 295623dce13SRobert Watson TCPDEBUG1(); 296623dce13SRobert Watson error = in_pcbbind(inp, nam, td->td_ucred); 297623dce13SRobert Watson out: 298623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 299623dce13SRobert Watson INP_UNLOCK(inp); 300623dce13SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 301623dce13SRobert Watson 302623dce13SRobert Watson return (error); 3032c37256eSGarrett Wollman } 3042c37256eSGarrett Wollman 305fb59c426SYoshinobu Inoue #ifdef INET6 306fb59c426SYoshinobu Inoue static int 307b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 308fb59c426SYoshinobu Inoue { 309fb59c426SYoshinobu Inoue int error = 0; 310f76fcf6dSJeffrey Hsu struct inpcb *inp; 311623dce13SRobert Watson struct tcpcb *tp = NULL; 312fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 313fb59c426SYoshinobu Inoue 31452710de1SPawel Jakub Dawidek sin6p = (struct sockaddr_in6 *)nam; 31552710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof (*sin6p)) 31652710de1SPawel Jakub Dawidek return (EINVAL); 317fb59c426SYoshinobu Inoue /* 318fb59c426SYoshinobu Inoue * Must check for multicast addresses and disallow binding 319fb59c426SYoshinobu Inoue * to them. 320fb59c426SYoshinobu Inoue */ 321fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 && 32252710de1SPawel Jakub Dawidek IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 32352710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 32452710de1SPawel Jakub Dawidek 325623dce13SRobert Watson TCPDEBUG0; 326623dce13SRobert Watson INP_INFO_WLOCK(&tcbinfo); 327623dce13SRobert Watson inp = sotoinpcb(so); 328623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 329623dce13SRobert Watson INP_LOCK(inp); 330623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 331623dce13SRobert Watson error = EINVAL; 332623dce13SRobert Watson goto out; 333623dce13SRobert Watson } 334623dce13SRobert Watson tp = intotcpcb(inp); 335623dce13SRobert Watson TCPDEBUG1(); 336fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 337fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 33866ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 339fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) 340fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 341fb59c426SYoshinobu Inoue else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 342fb59c426SYoshinobu Inoue struct sockaddr_in sin; 343fb59c426SYoshinobu Inoue 344fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 345fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 346fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 347b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)&sin, 348b0330ed9SPawel Jakub Dawidek td->td_ucred); 349fb59c426SYoshinobu Inoue goto out; 350fb59c426SYoshinobu Inoue } 351fb59c426SYoshinobu Inoue } 352b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, nam, td->td_ucred); 353623dce13SRobert Watson out: 354623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 355623dce13SRobert Watson INP_UNLOCK(inp); 356623dce13SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 357623dce13SRobert Watson return (error); 358fb59c426SYoshinobu Inoue } 359fb59c426SYoshinobu Inoue #endif /* INET6 */ 360fb59c426SYoshinobu Inoue 3612c37256eSGarrett Wollman /* 3622c37256eSGarrett Wollman * Prepare to accept connections. 3632c37256eSGarrett Wollman */ 3642c37256eSGarrett Wollman static int 365d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td) 3662c37256eSGarrett Wollman { 3672c37256eSGarrett Wollman int error = 0; 368f76fcf6dSJeffrey Hsu struct inpcb *inp; 369623dce13SRobert Watson struct tcpcb *tp = NULL; 3702c37256eSGarrett Wollman 371623dce13SRobert Watson TCPDEBUG0; 372623dce13SRobert Watson INP_INFO_WLOCK(&tcbinfo); 373623dce13SRobert Watson inp = sotoinpcb(so); 374623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 375623dce13SRobert Watson INP_LOCK(inp); 376623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 377623dce13SRobert Watson error = EINVAL; 378623dce13SRobert Watson goto out; 379623dce13SRobert Watson } 380623dce13SRobert Watson tp = intotcpcb(inp); 381623dce13SRobert Watson TCPDEBUG1(); 3820daccb9cSRobert Watson SOCK_LOCK(so); 3830daccb9cSRobert Watson error = solisten_proto_check(so); 3840daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) 385b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 3860daccb9cSRobert Watson if (error == 0) { 3872c37256eSGarrett Wollman tp->t_state = TCPS_LISTEN; 388d374e81eSRobert Watson solisten_proto(so, backlog); 3890daccb9cSRobert Watson } 3900daccb9cSRobert Watson SOCK_UNLOCK(so); 391623dce13SRobert Watson 392623dce13SRobert Watson out: 393623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 394623dce13SRobert Watson INP_UNLOCK(inp); 395623dce13SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 396623dce13SRobert Watson return (error); 3972c37256eSGarrett Wollman } 3982c37256eSGarrett Wollman 399fb59c426SYoshinobu Inoue #ifdef INET6 400fb59c426SYoshinobu Inoue static int 401d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 402fb59c426SYoshinobu Inoue { 403fb59c426SYoshinobu Inoue int error = 0; 404f76fcf6dSJeffrey Hsu struct inpcb *inp; 405623dce13SRobert Watson struct tcpcb *tp = NULL; 406fb59c426SYoshinobu Inoue 407623dce13SRobert Watson TCPDEBUG0; 408623dce13SRobert Watson INP_INFO_WLOCK(&tcbinfo); 409623dce13SRobert Watson inp = sotoinpcb(so); 410623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL")); 411623dce13SRobert Watson INP_LOCK(inp); 412623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 413623dce13SRobert Watson error = EINVAL; 414623dce13SRobert Watson goto out; 415623dce13SRobert Watson } 416623dce13SRobert Watson tp = intotcpcb(inp); 417623dce13SRobert Watson TCPDEBUG1(); 4180daccb9cSRobert Watson SOCK_LOCK(so); 4190daccb9cSRobert Watson error = solisten_proto_check(so); 4200daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) { 421fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 42266ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 423fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 424b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 425fb59c426SYoshinobu Inoue } 4260daccb9cSRobert Watson if (error == 0) { 427fb59c426SYoshinobu Inoue tp->t_state = TCPS_LISTEN; 428d374e81eSRobert Watson solisten_proto(so, backlog); 4290daccb9cSRobert Watson } 4300daccb9cSRobert Watson SOCK_UNLOCK(so); 431623dce13SRobert Watson 432623dce13SRobert Watson out: 433623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 434623dce13SRobert Watson INP_UNLOCK(inp); 435623dce13SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 436623dce13SRobert Watson return (error); 437fb59c426SYoshinobu Inoue } 438fb59c426SYoshinobu Inoue #endif /* INET6 */ 439fb59c426SYoshinobu Inoue 4402c37256eSGarrett Wollman /* 4412c37256eSGarrett Wollman * Initiate connection to peer. 4422c37256eSGarrett Wollman * Create a template for use in transmissions on this connection. 4432c37256eSGarrett Wollman * Enter SYN_SENT state, and mark socket as connecting. 4442c37256eSGarrett Wollman * Start keep-alive timer, and seed output sequence space. 4452c37256eSGarrett Wollman * Send initial segment on connection. 4462c37256eSGarrett Wollman */ 4472c37256eSGarrett Wollman static int 448b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 4492c37256eSGarrett Wollman { 4502c37256eSGarrett Wollman int error = 0; 451f76fcf6dSJeffrey Hsu struct inpcb *inp; 452623dce13SRobert Watson struct tcpcb *tp = NULL; 4532c37256eSGarrett Wollman struct sockaddr_in *sinp; 4542c37256eSGarrett Wollman 45557bf258eSGarrett Wollman sinp = (struct sockaddr_in *)nam; 456e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sinp)) 457e29ef13fSDon Lewis return (EINVAL); 45852710de1SPawel Jakub Dawidek /* 45952710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 46052710de1SPawel Jakub Dawidek */ 4612c37256eSGarrett Wollman if (sinp->sin_family == AF_INET 46252710de1SPawel Jakub Dawidek && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 46352710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 464812d8653SSam Leffler if (jailed(td->td_ucred)) 465a854ed98SJohn Baldwin prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr); 46675c13541SPoul-Henning Kamp 467623dce13SRobert Watson TCPDEBUG0; 468623dce13SRobert Watson INP_INFO_WLOCK(&tcbinfo); 469623dce13SRobert Watson inp = sotoinpcb(so); 470623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 471623dce13SRobert Watson INP_LOCK(inp); 472623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 473623dce13SRobert Watson error = EINVAL; 474623dce13SRobert Watson goto out; 475623dce13SRobert Watson } 476623dce13SRobert Watson tp = intotcpcb(inp); 477623dce13SRobert Watson TCPDEBUG1(); 478b40ce416SJulian Elischer if ((error = tcp_connect(tp, nam, td)) != 0) 4792c37256eSGarrett Wollman goto out; 4802c37256eSGarrett Wollman error = tcp_output(tp); 481623dce13SRobert Watson out: 482623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 483623dce13SRobert Watson INP_UNLOCK(inp); 484623dce13SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 485623dce13SRobert Watson return (error); 4862c37256eSGarrett Wollman } 4872c37256eSGarrett Wollman 488fb59c426SYoshinobu Inoue #ifdef INET6 489fb59c426SYoshinobu Inoue static int 490b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 491fb59c426SYoshinobu Inoue { 492fb59c426SYoshinobu Inoue int error = 0; 493f76fcf6dSJeffrey Hsu struct inpcb *inp; 494623dce13SRobert Watson struct tcpcb *tp = NULL; 495fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 496623dce13SRobert Watson 497623dce13SRobert Watson TCPDEBUG0; 498fb59c426SYoshinobu Inoue 499fb59c426SYoshinobu Inoue sin6p = (struct sockaddr_in6 *)nam; 500e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sin6p)) 501e29ef13fSDon Lewis return (EINVAL); 50252710de1SPawel Jakub Dawidek /* 50352710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 50452710de1SPawel Jakub Dawidek */ 505fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 50652710de1SPawel Jakub Dawidek && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 50752710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 508fb59c426SYoshinobu Inoue 509623dce13SRobert Watson INP_INFO_WLOCK(&tcbinfo); 510623dce13SRobert Watson inp = sotoinpcb(so); 511623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 512623dce13SRobert Watson INP_LOCK(inp); 513623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 514623dce13SRobert Watson error = EINVAL; 515623dce13SRobert Watson goto out; 516623dce13SRobert Watson } 517623dce13SRobert Watson tp = intotcpcb(inp); 518623dce13SRobert Watson TCPDEBUG1(); 51933841545SHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 520fb59c426SYoshinobu Inoue struct sockaddr_in sin; 521fb59c426SYoshinobu Inoue 522d46a5312SMaxim Konovalov if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 523d46a5312SMaxim Konovalov error = EINVAL; 524d46a5312SMaxim Konovalov goto out; 525d46a5312SMaxim Konovalov } 52633841545SHajimu UMEMOTO 527fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 528fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 529fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 530b40ce416SJulian Elischer if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 531fb59c426SYoshinobu Inoue goto out; 532fb59c426SYoshinobu Inoue error = tcp_output(tp); 533fb59c426SYoshinobu Inoue goto out; 534fb59c426SYoshinobu Inoue } 535fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 536fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 537b7d6d952SHajimu UMEMOTO inp->inp_inc.inc_isipv6 = 1; 538b40ce416SJulian Elischer if ((error = tcp6_connect(tp, nam, td)) != 0) 539fb59c426SYoshinobu Inoue goto out; 540fb59c426SYoshinobu Inoue error = tcp_output(tp); 541623dce13SRobert Watson 542623dce13SRobert Watson out: 543623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 544623dce13SRobert Watson INP_UNLOCK(inp); 545623dce13SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 546623dce13SRobert Watson return (error); 547fb59c426SYoshinobu Inoue } 548fb59c426SYoshinobu Inoue #endif /* INET6 */ 549fb59c426SYoshinobu Inoue 5502c37256eSGarrett Wollman /* 5512c37256eSGarrett Wollman * Initiate disconnect from peer. 5522c37256eSGarrett Wollman * If connection never passed embryonic stage, just drop; 5532c37256eSGarrett Wollman * else if don't need to let data drain, then can just drop anyways, 5542c37256eSGarrett Wollman * else have to begin TCP shutdown process: mark socket disconnecting, 5552c37256eSGarrett Wollman * drain unread data, state switch to reflect user close, and 5562c37256eSGarrett Wollman * send segment (e.g. FIN) to peer. Socket will be really disconnected 5572c37256eSGarrett Wollman * when peer sends FIN and acks ours. 5582c37256eSGarrett Wollman * 5592c37256eSGarrett Wollman * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 5602c37256eSGarrett Wollman */ 5612c37256eSGarrett Wollman static int 5622c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so) 5632c37256eSGarrett Wollman { 564f76fcf6dSJeffrey Hsu struct inpcb *inp; 565623dce13SRobert Watson struct tcpcb *tp = NULL; 566623dce13SRobert Watson int error = 0; 5672c37256eSGarrett Wollman 568623dce13SRobert Watson TCPDEBUG0; 569623dce13SRobert Watson INP_INFO_WLOCK(&tcbinfo); 570623dce13SRobert Watson inp = sotoinpcb(so); 571623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 572623dce13SRobert Watson INP_LOCK(inp); 573623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 574623dce13SRobert Watson error = EINVAL; 575623dce13SRobert Watson goto out; 576623dce13SRobert Watson } 577623dce13SRobert Watson tp = intotcpcb(inp); 578623dce13SRobert Watson TCPDEBUG1(); 579623dce13SRobert Watson tcp_disconnect(tp); 580623dce13SRobert Watson out: 581623dce13SRobert Watson TCPDEBUG2(PRU_DISCONNECT); 582623dce13SRobert Watson INP_UNLOCK(inp); 583623dce13SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 584623dce13SRobert Watson return (error); 5852c37256eSGarrett Wollman } 5862c37256eSGarrett Wollman 5872c37256eSGarrett Wollman /* 5882c37256eSGarrett Wollman * Accept a connection. Essentially all the work is 5892c37256eSGarrett Wollman * done at higher levels; just return the address 5902c37256eSGarrett Wollman * of the peer, storing through addr. 5912c37256eSGarrett Wollman */ 5922c37256eSGarrett Wollman static int 59357bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam) 5942c37256eSGarrett Wollman { 5952c37256eSGarrett Wollman int error = 0; 596f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 5971db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 59826ef6ac4SDon Lewis struct in_addr addr; 59926ef6ac4SDon Lewis in_port_t port = 0; 6001db24ffbSJonathan Lemon TCPDEBUG0; 6012c37256eSGarrett Wollman 6023d2d3ef4SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 6033d2d3ef4SRobert Watson return (ECONNABORTED); 604f76fcf6dSJeffrey Hsu 605f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 606623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 607f76fcf6dSJeffrey Hsu INP_LOCK(inp); 608623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 6093d2d3ef4SRobert Watson error = ECONNABORTED; 610623dce13SRobert Watson goto out; 611623dce13SRobert Watson } 6121db24ffbSJonathan Lemon tp = intotcpcb(inp); 6131db24ffbSJonathan Lemon TCPDEBUG1(); 614f76fcf6dSJeffrey Hsu 615f76fcf6dSJeffrey Hsu /* 61626ef6ac4SDon Lewis * We inline in_setpeeraddr and COMMON_END here, so that we can 61726ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 61826ef6ac4SDon Lewis * release the lock. 619f76fcf6dSJeffrey Hsu */ 62026ef6ac4SDon Lewis port = inp->inp_fport; 62126ef6ac4SDon Lewis addr = inp->inp_faddr; 622f76fcf6dSJeffrey Hsu 623623dce13SRobert Watson out: 624623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 62526ef6ac4SDon Lewis INP_UNLOCK(inp); 62626ef6ac4SDon Lewis if (error == 0) 62726ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 62826ef6ac4SDon Lewis return error; 6292c37256eSGarrett Wollman } 6302c37256eSGarrett Wollman 631fb59c426SYoshinobu Inoue #ifdef INET6 632fb59c426SYoshinobu Inoue static int 633fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 634fb59c426SYoshinobu Inoue { 635f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 636fb59c426SYoshinobu Inoue int error = 0; 6371db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 63826ef6ac4SDon Lewis struct in_addr addr; 63926ef6ac4SDon Lewis struct in6_addr addr6; 64026ef6ac4SDon Lewis in_port_t port = 0; 64126ef6ac4SDon Lewis int v4 = 0; 6421db24ffbSJonathan Lemon TCPDEBUG0; 643fb59c426SYoshinobu Inoue 644b4470c16SRobert Watson if (so->so_state & SS_ISDISCONNECTED) 645b4470c16SRobert Watson return (ECONNABORTED); 646f76fcf6dSJeffrey Hsu 647f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 648623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 649f76fcf6dSJeffrey Hsu INP_LOCK(inp); 650623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 651623dce13SRobert Watson error = EINVAL; 652623dce13SRobert Watson goto out; 653623dce13SRobert Watson } 6541db24ffbSJonathan Lemon tp = intotcpcb(inp); 6551db24ffbSJonathan Lemon TCPDEBUG1(); 656623dce13SRobert Watson 65726ef6ac4SDon Lewis /* 65826ef6ac4SDon Lewis * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 65926ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 66026ef6ac4SDon Lewis * release the lock. 66126ef6ac4SDon Lewis */ 66226ef6ac4SDon Lewis if (inp->inp_vflag & INP_IPV4) { 66326ef6ac4SDon Lewis v4 = 1; 66426ef6ac4SDon Lewis port = inp->inp_fport; 66526ef6ac4SDon Lewis addr = inp->inp_faddr; 66626ef6ac4SDon Lewis } else { 66726ef6ac4SDon Lewis port = inp->inp_fport; 66826ef6ac4SDon Lewis addr6 = inp->in6p_faddr; 66926ef6ac4SDon Lewis } 67026ef6ac4SDon Lewis 671623dce13SRobert Watson out: 672623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 67326ef6ac4SDon Lewis INP_UNLOCK(inp); 67426ef6ac4SDon Lewis if (error == 0) { 67526ef6ac4SDon Lewis if (v4) 67626ef6ac4SDon Lewis *nam = in6_v4mapsin6_sockaddr(port, &addr); 67726ef6ac4SDon Lewis else 67826ef6ac4SDon Lewis *nam = in6_sockaddr(port, &addr6); 67926ef6ac4SDon Lewis } 68026ef6ac4SDon Lewis return error; 681fb59c426SYoshinobu Inoue } 682fb59c426SYoshinobu Inoue #endif /* INET6 */ 683f76fcf6dSJeffrey Hsu 684f76fcf6dSJeffrey Hsu /* 685f76fcf6dSJeffrey Hsu * This is the wrapper function for in_setsockaddr. We just pass down 686f76fcf6dSJeffrey Hsu * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking 687f76fcf6dSJeffrey Hsu * here because in_setsockaddr will call malloc and can block. 688f76fcf6dSJeffrey Hsu */ 689f76fcf6dSJeffrey Hsu static int 690f76fcf6dSJeffrey Hsu tcp_sockaddr(struct socket *so, struct sockaddr **nam) 691f76fcf6dSJeffrey Hsu { 692f76fcf6dSJeffrey Hsu return (in_setsockaddr(so, nam, &tcbinfo)); 693f76fcf6dSJeffrey Hsu } 694f76fcf6dSJeffrey Hsu 695f76fcf6dSJeffrey Hsu /* 696f76fcf6dSJeffrey Hsu * This is the wrapper function for in_setpeeraddr. We just pass down 697f76fcf6dSJeffrey Hsu * the pcbinfo for in_setpeeraddr to lock. 698f76fcf6dSJeffrey Hsu */ 699f76fcf6dSJeffrey Hsu static int 700f76fcf6dSJeffrey Hsu tcp_peeraddr(struct socket *so, struct sockaddr **nam) 701f76fcf6dSJeffrey Hsu { 702f76fcf6dSJeffrey Hsu return (in_setpeeraddr(so, nam, &tcbinfo)); 703f76fcf6dSJeffrey Hsu } 704f76fcf6dSJeffrey Hsu 7052c37256eSGarrett Wollman /* 7062c37256eSGarrett Wollman * Mark the connection as being incapable of further output. 7072c37256eSGarrett Wollman */ 7082c37256eSGarrett Wollman static int 7092c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so) 7102c37256eSGarrett Wollman { 7112c37256eSGarrett Wollman int error = 0; 712f76fcf6dSJeffrey Hsu struct inpcb *inp; 713623dce13SRobert Watson struct tcpcb *tp = NULL; 7142c37256eSGarrett Wollman 715623dce13SRobert Watson TCPDEBUG0; 716623dce13SRobert Watson INP_INFO_WLOCK(&tcbinfo); 717623dce13SRobert Watson inp = sotoinpcb(so); 718623dce13SRobert Watson KASSERT(inp != NULL, ("inp == NULL")); 719623dce13SRobert Watson INP_LOCK(inp); 720623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 721623dce13SRobert Watson error = EINVAL; 722623dce13SRobert Watson goto out; 723623dce13SRobert Watson } 724623dce13SRobert Watson tp = intotcpcb(inp); 725623dce13SRobert Watson TCPDEBUG1(); 7262c37256eSGarrett Wollman socantsendmore(so); 727623dce13SRobert Watson tcp_usrclosed(tp); 7282c37256eSGarrett Wollman error = tcp_output(tp); 729623dce13SRobert Watson 730623dce13SRobert Watson out: 731623dce13SRobert Watson TCPDEBUG2(PRU_SHUTDOWN); 732623dce13SRobert Watson INP_UNLOCK(inp); 733623dce13SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 734623dce13SRobert Watson 735623dce13SRobert Watson return (error); 7362c37256eSGarrett Wollman } 7372c37256eSGarrett Wollman 7382c37256eSGarrett Wollman /* 7392c37256eSGarrett Wollman * After a receive, possibly send window update to peer. 7402c37256eSGarrett Wollman */ 7412c37256eSGarrett Wollman static int 7422c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags) 7432c37256eSGarrett Wollman { 744f76fcf6dSJeffrey Hsu struct inpcb *inp; 745623dce13SRobert Watson struct tcpcb *tp = NULL; 746623dce13SRobert Watson int error = 0; 7472c37256eSGarrett Wollman 748623dce13SRobert Watson TCPDEBUG0; 749623dce13SRobert Watson inp = sotoinpcb(so); 750623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 751623dce13SRobert Watson INP_LOCK(inp); 752623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 753623dce13SRobert Watson error = EINVAL; 754623dce13SRobert Watson goto out; 755623dce13SRobert Watson } 756623dce13SRobert Watson tp = intotcpcb(inp); 757623dce13SRobert Watson TCPDEBUG1(); 7582c37256eSGarrett Wollman tcp_output(tp); 759623dce13SRobert Watson 760623dce13SRobert Watson out: 761623dce13SRobert Watson TCPDEBUG2(PRU_RCVD); 762623dce13SRobert Watson INP_UNLOCK(inp); 763623dce13SRobert Watson return (error); 7642c37256eSGarrett Wollman } 7652c37256eSGarrett Wollman 7662c37256eSGarrett Wollman /* 7672c37256eSGarrett Wollman * Do a send by putting data in output queue and updating urgent 7689c9906e9SPeter Wemm * marker if URG set. Possibly send more data. Unlike the other 7699c9906e9SPeter Wemm * pru_*() routines, the mbuf chains are our responsibility. We 7709c9906e9SPeter Wemm * must either enqueue them or free them. The other pru_* routines 7719c9906e9SPeter Wemm * generally are caller-frees. 7722c37256eSGarrett Wollman */ 7732c37256eSGarrett Wollman static int 77457bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 775b40ce416SJulian Elischer struct sockaddr *nam, struct mbuf *control, struct thread *td) 7762c37256eSGarrett Wollman { 7772c37256eSGarrett Wollman int error = 0; 778f76fcf6dSJeffrey Hsu struct inpcb *inp; 779623dce13SRobert Watson struct tcpcb *tp = NULL; 780623dce13SRobert Watson int headlocked = 0; 781fb59c426SYoshinobu Inoue #ifdef INET6 782fb59c426SYoshinobu Inoue int isipv6; 783fb59c426SYoshinobu Inoue #endif 7849c9906e9SPeter Wemm TCPDEBUG0; 7852c37256eSGarrett Wollman 786f76fcf6dSJeffrey Hsu /* 787623dce13SRobert Watson * We require the pcbinfo lock in two cases: 788623dce13SRobert Watson * 789623dce13SRobert Watson * (1) An implied connect is taking place, which can result in 790623dce13SRobert Watson * binding IPs and ports and hence modification of the pcb hash 791623dce13SRobert Watson * chains. 792623dce13SRobert Watson * 793623dce13SRobert Watson * (2) PRUS_EOF is set, resulting in explicit close on the send. 794f76fcf6dSJeffrey Hsu */ 795623dce13SRobert Watson if ((nam != NULL) || (flags & PRUS_EOF)) { 796f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 797623dce13SRobert Watson headlocked = 1; 798623dce13SRobert Watson } 799f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 800623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 801623dce13SRobert Watson INP_LOCK(inp); 802623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 803623dce13SRobert Watson error = EINVAL; 8049c9906e9SPeter Wemm goto out; 8059c9906e9SPeter Wemm } 806fb59c426SYoshinobu Inoue #ifdef INET6 807fb59c426SYoshinobu Inoue isipv6 = nam && nam->sa_family == AF_INET6; 808fb59c426SYoshinobu Inoue #endif /* INET6 */ 8099c9906e9SPeter Wemm tp = intotcpcb(inp); 8109c9906e9SPeter Wemm TCPDEBUG1(); 8119c9906e9SPeter Wemm if (control) { 8129c9906e9SPeter Wemm /* TCP doesn't do control messages (rights, creds, etc) */ 8139c9906e9SPeter Wemm if (control->m_len) { 8149c9906e9SPeter Wemm m_freem(control); 8152c37256eSGarrett Wollman if (m) 8162c37256eSGarrett Wollman m_freem(m); 817744f87eaSDavid Greenman error = EINVAL; 818744f87eaSDavid Greenman goto out; 8192c37256eSGarrett Wollman } 8209c9906e9SPeter Wemm m_freem(control); /* empty control, just free it */ 8219c9906e9SPeter Wemm } 8222c37256eSGarrett Wollman if (!(flags & PRUS_OOB)) { 823395bb186SSam Leffler sbappendstream(&so->so_snd, m); 8242c37256eSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 8252c37256eSGarrett Wollman /* 8262c37256eSGarrett Wollman * Do implied connect if not yet connected, 8272c37256eSGarrett Wollman * initialize window to default value, and 8282c37256eSGarrett Wollman * initialize maxseg/maxopd using peer's cached 8292c37256eSGarrett Wollman * MSS. 8302c37256eSGarrett Wollman */ 831623dce13SRobert Watson INP_INFO_WLOCK_ASSERT(&tcbinfo); 832fb59c426SYoshinobu Inoue #ifdef INET6 833fb59c426SYoshinobu Inoue if (isipv6) 834b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 835fb59c426SYoshinobu Inoue else 836fb59c426SYoshinobu Inoue #endif /* INET6 */ 837b40ce416SJulian Elischer error = tcp_connect(tp, nam, td); 8382c37256eSGarrett Wollman if (error) 8392c37256eSGarrett Wollman goto out; 8402c37256eSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 8412c37256eSGarrett Wollman tcp_mss(tp, -1); 8422c37256eSGarrett Wollman } 8432c37256eSGarrett Wollman if (flags & PRUS_EOF) { 8442c37256eSGarrett Wollman /* 8452c37256eSGarrett Wollman * Close the send side of the connection after 8462c37256eSGarrett Wollman * the data is sent. 8472c37256eSGarrett Wollman */ 848623dce13SRobert Watson INP_INFO_WLOCK_ASSERT(&tcbinfo); 8492c37256eSGarrett Wollman socantsendmore(so); 850623dce13SRobert Watson tcp_usrclosed(tp); 8512c37256eSGarrett Wollman } 852623dce13SRobert Watson if (headlocked) { 853d1401c90SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 854623dce13SRobert Watson headlocked = 0; 855623dce13SRobert Watson } 856b0acefa8SBill Fenner if (tp != NULL) { 857b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 858b0acefa8SBill Fenner tp->t_flags |= TF_MORETOCOME; 8592c37256eSGarrett Wollman error = tcp_output(tp); 860b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 861b0acefa8SBill Fenner tp->t_flags &= ~TF_MORETOCOME; 862b0acefa8SBill Fenner } 8632c37256eSGarrett Wollman } else { 864623dce13SRobert Watson /* 865623dce13SRobert Watson * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 866623dce13SRobert Watson */ 867d2bc35abSRobert Watson SOCKBUF_LOCK(&so->so_snd); 8682c37256eSGarrett Wollman if (sbspace(&so->so_snd) < -512) { 869d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 8702c37256eSGarrett Wollman m_freem(m); 8712c37256eSGarrett Wollman error = ENOBUFS; 8722c37256eSGarrett Wollman goto out; 8732c37256eSGarrett Wollman } 8742c37256eSGarrett Wollman /* 8752c37256eSGarrett Wollman * According to RFC961 (Assigned Protocols), 8762c37256eSGarrett Wollman * the urgent pointer points to the last octet 8772c37256eSGarrett Wollman * of urgent data. We continue, however, 8782c37256eSGarrett Wollman * to consider it to indicate the first octet 8792c37256eSGarrett Wollman * of data past the urgent section. 8802c37256eSGarrett Wollman * Otherwise, snd_up should be one lower. 8812c37256eSGarrett Wollman */ 882d2bc35abSRobert Watson sbappendstream_locked(&so->so_snd, m); 883d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 884ef53690bSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 885ef53690bSGarrett Wollman /* 886ef53690bSGarrett Wollman * Do implied connect if not yet connected, 887ef53690bSGarrett Wollman * initialize window to default value, and 888ef53690bSGarrett Wollman * initialize maxseg/maxopd using peer's cached 889ef53690bSGarrett Wollman * MSS. 890ef53690bSGarrett Wollman */ 891623dce13SRobert Watson INP_INFO_WLOCK_ASSERT(&tcbinfo); 892fb59c426SYoshinobu Inoue #ifdef INET6 893fb59c426SYoshinobu Inoue if (isipv6) 894b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 895fb59c426SYoshinobu Inoue else 896fb59c426SYoshinobu Inoue #endif /* INET6 */ 897b40ce416SJulian Elischer error = tcp_connect(tp, nam, td); 898ef53690bSGarrett Wollman if (error) 899ef53690bSGarrett Wollman goto out; 900ef53690bSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 901ef53690bSGarrett Wollman tcp_mss(tp, -1); 902d1401c90SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 903623dce13SRobert Watson headlocked = 0; 904623dce13SRobert Watson } else if (nam) { 905623dce13SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 906623dce13SRobert Watson headlocked = 0; 907623dce13SRobert Watson } 9082c37256eSGarrett Wollman tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 9092cdbfa66SPaul Saab tp->t_flags |= TF_FORCEDATA; 9102c37256eSGarrett Wollman error = tcp_output(tp); 9112cdbfa66SPaul Saab tp->t_flags &= ~TF_FORCEDATA; 9122c37256eSGarrett Wollman } 913d1401c90SRobert Watson out: 914d1401c90SRobert Watson TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB : 9152c37256eSGarrett Wollman ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 916d1401c90SRobert Watson INP_UNLOCK(inp); 917623dce13SRobert Watson if (headlocked) 918d1401c90SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 91973fddedaSPeter Grehan return (error); 9202c37256eSGarrett Wollman } 9212c37256eSGarrett Wollman 9222c37256eSGarrett Wollman /* 923a152f8a3SRobert Watson * Abort the TCP. Drop the connection abruptly. 9242c37256eSGarrett Wollman */ 925ac45e92fSRobert Watson static void 9262c37256eSGarrett Wollman tcp_usr_abort(struct socket *so) 9272c37256eSGarrett Wollman { 928f76fcf6dSJeffrey Hsu struct inpcb *inp; 929a152f8a3SRobert Watson struct tcpcb *tp = NULL; 930623dce13SRobert Watson TCPDEBUG0; 931c78cbc7bSRobert Watson 932ac45e92fSRobert Watson inp = sotoinpcb(so); 933c78cbc7bSRobert Watson KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 934c78cbc7bSRobert Watson 935c78cbc7bSRobert Watson INP_INFO_WLOCK(&tcbinfo); 936ac45e92fSRobert Watson INP_LOCK(inp); 937c78cbc7bSRobert Watson KASSERT(inp->inp_socket != NULL, 938c78cbc7bSRobert Watson ("tcp_usr_abort: inp_socket == NULL")); 939c78cbc7bSRobert Watson 940c78cbc7bSRobert Watson /* 941a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, drop. 942c78cbc7bSRobert Watson */ 943c78cbc7bSRobert Watson if (!(inp->inp_vflag & INP_TIMEWAIT) && 944c78cbc7bSRobert Watson !(inp->inp_vflag & INP_DROPPED)) { 945c78cbc7bSRobert Watson tp = intotcpcb(inp); 946a152f8a3SRobert Watson TCPDEBUG1(); 947c78cbc7bSRobert Watson tcp_drop(tp, ECONNABORTED); 948a152f8a3SRobert Watson TCPDEBUG2(PRU_ABORT); 949c78cbc7bSRobert Watson } 950a152f8a3SRobert Watson if (!(inp->inp_vflag & INP_DROPPED)) { 951a152f8a3SRobert Watson SOCK_LOCK(so); 952a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 953a152f8a3SRobert Watson SOCK_UNLOCK(so); 954a152f8a3SRobert Watson inp->inp_vflag |= INP_SOCKREF; 955a152f8a3SRobert Watson } 956a152f8a3SRobert Watson INP_UNLOCK(inp); 957a152f8a3SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 958a152f8a3SRobert Watson } 959a152f8a3SRobert Watson 960a152f8a3SRobert Watson /* 961a152f8a3SRobert Watson * TCP socket is closed. Start friendly disconnect. 962a152f8a3SRobert Watson */ 963a152f8a3SRobert Watson static void 964a152f8a3SRobert Watson tcp_usr_close(struct socket *so) 965a152f8a3SRobert Watson { 966a152f8a3SRobert Watson struct inpcb *inp; 967a152f8a3SRobert Watson struct tcpcb *tp = NULL; 968a152f8a3SRobert Watson TCPDEBUG0; 969a152f8a3SRobert Watson 970a152f8a3SRobert Watson inp = sotoinpcb(so); 971a152f8a3SRobert Watson KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL")); 972a152f8a3SRobert Watson 973a152f8a3SRobert Watson INP_INFO_WLOCK(&tcbinfo); 974a152f8a3SRobert Watson INP_LOCK(inp); 975a152f8a3SRobert Watson KASSERT(inp->inp_socket != NULL, 976a152f8a3SRobert Watson ("tcp_usr_close: inp_socket == NULL")); 977a152f8a3SRobert Watson 978a152f8a3SRobert Watson /* 979a152f8a3SRobert Watson * If we still have full TCP state, and we're not dropped, initiate 980a152f8a3SRobert Watson * a disconnect. 981a152f8a3SRobert Watson */ 982a152f8a3SRobert Watson if (!(inp->inp_vflag & INP_TIMEWAIT) && 983a152f8a3SRobert Watson !(inp->inp_vflag & INP_DROPPED)) { 984a152f8a3SRobert Watson tp = intotcpcb(inp); 985a152f8a3SRobert Watson TCPDEBUG1(); 986a152f8a3SRobert Watson tcp_disconnect(tp); 987a152f8a3SRobert Watson TCPDEBUG2(PRU_CLOSE); 988a152f8a3SRobert Watson } 989a152f8a3SRobert Watson if (!(inp->inp_vflag & INP_DROPPED)) { 990a152f8a3SRobert Watson SOCK_LOCK(so); 991a152f8a3SRobert Watson so->so_state |= SS_PROTOREF; 992a152f8a3SRobert Watson SOCK_UNLOCK(so); 993a152f8a3SRobert Watson inp->inp_vflag |= INP_SOCKREF; 994a152f8a3SRobert Watson } 995a152f8a3SRobert Watson INP_UNLOCK(inp); 996ac45e92fSRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 9972c37256eSGarrett Wollman } 9982c37256eSGarrett Wollman 9992c37256eSGarrett Wollman /* 10002c37256eSGarrett Wollman * Receive out-of-band data. 10012c37256eSGarrett Wollman */ 10022c37256eSGarrett Wollman static int 10032c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 10042c37256eSGarrett Wollman { 10052c37256eSGarrett Wollman int error = 0; 1006f76fcf6dSJeffrey Hsu struct inpcb *inp; 1007623dce13SRobert Watson struct tcpcb *tp = NULL; 10082c37256eSGarrett Wollman 1009623dce13SRobert Watson TCPDEBUG0; 1010623dce13SRobert Watson inp = sotoinpcb(so); 1011623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 1012623dce13SRobert Watson INP_LOCK(inp); 1013623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 1014623dce13SRobert Watson error = EINVAL; 1015623dce13SRobert Watson goto out; 1016623dce13SRobert Watson } 1017623dce13SRobert Watson tp = intotcpcb(inp); 1018623dce13SRobert Watson TCPDEBUG1(); 10192c37256eSGarrett Wollman if ((so->so_oobmark == 0 && 1020c0b99ffaSRobert Watson (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 10214cc20ab1SSeigo Tanimura so->so_options & SO_OOBINLINE || 10224cc20ab1SSeigo Tanimura tp->t_oobflags & TCPOOB_HADDATA) { 10232c37256eSGarrett Wollman error = EINVAL; 10242c37256eSGarrett Wollman goto out; 10252c37256eSGarrett Wollman } 10262c37256eSGarrett Wollman if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 10272c37256eSGarrett Wollman error = EWOULDBLOCK; 10282c37256eSGarrett Wollman goto out; 10292c37256eSGarrett Wollman } 10302c37256eSGarrett Wollman m->m_len = 1; 10312c37256eSGarrett Wollman *mtod(m, caddr_t) = tp->t_iobc; 10322c37256eSGarrett Wollman if ((flags & MSG_PEEK) == 0) 10332c37256eSGarrett Wollman tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1034623dce13SRobert Watson 1035623dce13SRobert Watson out: 1036623dce13SRobert Watson TCPDEBUG2(PRU_RCVOOB); 1037623dce13SRobert Watson INP_UNLOCK(inp); 1038623dce13SRobert Watson return (error); 10392c37256eSGarrett Wollman } 10402c37256eSGarrett Wollman 10412c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = { 1042756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1043756d52a1SPoul-Henning Kamp .pru_accept = tcp_usr_accept, 1044756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1045756d52a1SPoul-Henning Kamp .pru_bind = tcp_usr_bind, 1046756d52a1SPoul-Henning Kamp .pru_connect = tcp_usr_connect, 1047756d52a1SPoul-Henning Kamp .pru_control = in_control, 1048756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1049756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1050756d52a1SPoul-Henning Kamp .pru_listen = tcp_usr_listen, 1051756d52a1SPoul-Henning Kamp .pru_peeraddr = tcp_peeraddr, 1052756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1053756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1054756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 1055756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 1056756d52a1SPoul-Henning Kamp .pru_sockaddr = tcp_sockaddr, 1057a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1058a152f8a3SRobert Watson .pru_close = tcp_usr_close, 10592c37256eSGarrett Wollman }; 1060df8bae1dSRodney W. Grimes 1061fb59c426SYoshinobu Inoue #ifdef INET6 1062fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = { 1063756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1064756d52a1SPoul-Henning Kamp .pru_accept = tcp6_usr_accept, 1065756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1066756d52a1SPoul-Henning Kamp .pru_bind = tcp6_usr_bind, 1067756d52a1SPoul-Henning Kamp .pru_connect = tcp6_usr_connect, 1068756d52a1SPoul-Henning Kamp .pru_control = in6_control, 1069756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1070756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1071756d52a1SPoul-Henning Kamp .pru_listen = tcp6_usr_listen, 1072756d52a1SPoul-Henning Kamp .pru_peeraddr = in6_mapped_peeraddr, 1073756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1074756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1075756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 1076756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 1077756d52a1SPoul-Henning Kamp .pru_sockaddr = in6_mapped_sockaddr, 1078a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1079a152f8a3SRobert Watson .pru_close = tcp_usr_close, 1080fb59c426SYoshinobu Inoue }; 1081fb59c426SYoshinobu Inoue #endif /* INET6 */ 1082fb59c426SYoshinobu Inoue 1083a0292f23SGarrett Wollman /* 1084a0292f23SGarrett Wollman * Common subroutine to open a TCP connection to remote host specified 1085a0292f23SGarrett Wollman * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 10865200e00eSIan Dowse * port number if needed. Call in_pcbconnect_setup to do the routing and 10875200e00eSIan Dowse * to choose a local host address (interface). If there is an existing 10885200e00eSIan Dowse * incarnation of the same connection in TIME-WAIT state and if the remote 10895200e00eSIan Dowse * host was sending CC options and if the connection duration was < MSL, then 1090a0292f23SGarrett Wollman * truncate the previous TIME-WAIT state and proceed. 1091a0292f23SGarrett Wollman * Initialize connection parameters and enter SYN-SENT state. 1092a0292f23SGarrett Wollman */ 10930312fbe9SPoul-Henning Kamp static int 1094b40ce416SJulian Elischer tcp_connect(tp, nam, td) 1095a0292f23SGarrett Wollman register struct tcpcb *tp; 109657bf258eSGarrett Wollman struct sockaddr *nam; 1097b40ce416SJulian Elischer struct thread *td; 1098a0292f23SGarrett Wollman { 1099a0292f23SGarrett Wollman struct inpcb *inp = tp->t_inpcb, *oinp; 1100a0292f23SGarrett Wollman struct socket *so = inp->inp_socket; 11015200e00eSIan Dowse struct in_addr laddr; 11025200e00eSIan Dowse u_short lport; 1103c3229e05SDavid Greenman int error; 1104a0292f23SGarrett Wollman 1105623dce13SRobert Watson INP_INFO_WLOCK_ASSERT(&tcbinfo); 1106623dce13SRobert Watson INP_LOCK_ASSERT(inp); 1107623dce13SRobert Watson 1108a0292f23SGarrett Wollman if (inp->inp_lport == 0) { 1109b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1110a0292f23SGarrett Wollman if (error) 1111a0292f23SGarrett Wollman return error; 1112a0292f23SGarrett Wollman } 1113a0292f23SGarrett Wollman 1114a0292f23SGarrett Wollman /* 1115a0292f23SGarrett Wollman * Cannot simply call in_pcbconnect, because there might be an 1116a0292f23SGarrett Wollman * earlier incarnation of this same connection still in 1117a0292f23SGarrett Wollman * TIME_WAIT state, creating an ADDRINUSE error. 1118a0292f23SGarrett Wollman */ 11195200e00eSIan Dowse laddr = inp->inp_laddr; 11205200e00eSIan Dowse lport = inp->inp_lport; 11215200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 1122b0330ed9SPawel Jakub Dawidek &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 11235200e00eSIan Dowse if (error && oinp == NULL) 1124d3628763SRodney W. Grimes return error; 1125c94c54e4SAndre Oppermann if (oinp) 1126a0292f23SGarrett Wollman return EADDRINUSE; 11275200e00eSIan Dowse inp->inp_laddr = laddr; 112815bd2b43SDavid Greenman in_pcbrehash(inp); 1129a0292f23SGarrett Wollman 1130a0292f23SGarrett Wollman /* Compute window scaling to request. */ 1131a0292f23SGarrett Wollman while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1132a0292f23SGarrett Wollman (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 1133a0292f23SGarrett Wollman tp->request_r_scale++; 1134a0292f23SGarrett Wollman 1135a0292f23SGarrett Wollman soisconnecting(so); 1136a0292f23SGarrett Wollman tcpstat.tcps_connattempt++; 1137a0292f23SGarrett Wollman tp->t_state = TCPS_SYN_SENT; 11389b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 1139b0e3ad75SMike Silbersack tp->iss = tcp_new_isn(tp); 11401fcc99b5SMatthew Dillon tp->t_bw_rtseq = tp->iss; 1141a0292f23SGarrett Wollman tcp_sendseqinit(tp); 1142a45d2726SAndras Olah 1143a0292f23SGarrett Wollman return 0; 1144a0292f23SGarrett Wollman } 1145a0292f23SGarrett Wollman 1146fb59c426SYoshinobu Inoue #ifdef INET6 1147fb59c426SYoshinobu Inoue static int 1148b40ce416SJulian Elischer tcp6_connect(tp, nam, td) 1149fb59c426SYoshinobu Inoue register struct tcpcb *tp; 1150fb59c426SYoshinobu Inoue struct sockaddr *nam; 1151b40ce416SJulian Elischer struct thread *td; 1152fb59c426SYoshinobu Inoue { 1153fb59c426SYoshinobu Inoue struct inpcb *inp = tp->t_inpcb, *oinp; 1154fb59c426SYoshinobu Inoue struct socket *so = inp->inp_socket; 1155fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 1156fb59c426SYoshinobu Inoue struct in6_addr *addr6; 1157fb59c426SYoshinobu Inoue int error; 1158fb59c426SYoshinobu Inoue 1159623dce13SRobert Watson INP_INFO_WLOCK_ASSERT(&tcbinfo); 1160623dce13SRobert Watson INP_LOCK_ASSERT(inp); 1161623dce13SRobert Watson 1162fb59c426SYoshinobu Inoue if (inp->inp_lport == 0) { 1163b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1164fb59c426SYoshinobu Inoue if (error) 1165fb59c426SYoshinobu Inoue return error; 1166fb59c426SYoshinobu Inoue } 1167fb59c426SYoshinobu Inoue 1168fb59c426SYoshinobu Inoue /* 1169fb59c426SYoshinobu Inoue * Cannot simply call in_pcbconnect, because there might be an 1170fb59c426SYoshinobu Inoue * earlier incarnation of this same connection still in 1171fb59c426SYoshinobu Inoue * TIME_WAIT state, creating an ADDRINUSE error. 1172a1f7e5f8SHajimu UMEMOTO * in6_pcbladdr() also handles scope zone IDs. 1173fb59c426SYoshinobu Inoue */ 1174fb59c426SYoshinobu Inoue error = in6_pcbladdr(inp, nam, &addr6); 1175fb59c426SYoshinobu Inoue if (error) 1176fb59c426SYoshinobu Inoue return error; 1177fb59c426SYoshinobu Inoue oinp = in6_pcblookup_hash(inp->inp_pcbinfo, 1178fb59c426SYoshinobu Inoue &sin6->sin6_addr, sin6->sin6_port, 1179fb59c426SYoshinobu Inoue IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 1180fb59c426SYoshinobu Inoue ? addr6 1181fb59c426SYoshinobu Inoue : &inp->in6p_laddr, 1182fb59c426SYoshinobu Inoue inp->inp_lport, 0, NULL); 1183c94c54e4SAndre Oppermann if (oinp) 1184fb59c426SYoshinobu Inoue return EADDRINUSE; 1185fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 1186fb59c426SYoshinobu Inoue inp->in6p_laddr = *addr6; 1187fb59c426SYoshinobu Inoue inp->in6p_faddr = sin6->sin6_addr; 1188fb59c426SYoshinobu Inoue inp->inp_fport = sin6->sin6_port; 11898a59da30SHajimu UMEMOTO /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 11908a59da30SHajimu UMEMOTO inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 11918a59da30SHajimu UMEMOTO if (inp->in6p_flags & IN6P_AUTOFLOWLABEL) 11928a59da30SHajimu UMEMOTO inp->in6p_flowinfo |= 11938a59da30SHajimu UMEMOTO (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 1194fb59c426SYoshinobu Inoue in_pcbrehash(inp); 1195fb59c426SYoshinobu Inoue 1196fb59c426SYoshinobu Inoue /* Compute window scaling to request. */ 1197fb59c426SYoshinobu Inoue while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1198fb59c426SYoshinobu Inoue (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 1199fb59c426SYoshinobu Inoue tp->request_r_scale++; 1200fb59c426SYoshinobu Inoue 1201fb59c426SYoshinobu Inoue soisconnecting(so); 1202fb59c426SYoshinobu Inoue tcpstat.tcps_connattempt++; 1203fb59c426SYoshinobu Inoue tp->t_state = TCPS_SYN_SENT; 1204fb59c426SYoshinobu Inoue callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 1205b0e3ad75SMike Silbersack tp->iss = tcp_new_isn(tp); 12061fcc99b5SMatthew Dillon tp->t_bw_rtseq = tp->iss; 1207fb59c426SYoshinobu Inoue tcp_sendseqinit(tp); 1208fb59c426SYoshinobu Inoue 1209fb59c426SYoshinobu Inoue return 0; 1210fb59c426SYoshinobu Inoue } 1211fb59c426SYoshinobu Inoue #endif /* INET6 */ 1212fb59c426SYoshinobu Inoue 1213cfe8b629SGarrett Wollman /* 1214b8af5dfaSRobert Watson * Export TCP internal state information via a struct tcp_info, based on the 1215b8af5dfaSRobert Watson * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1216b8af5dfaSRobert Watson * (TCP state machine, etc). We export all information using FreeBSD-native 1217b8af5dfaSRobert Watson * constants -- for example, the numeric values for tcpi_state will differ 1218b8af5dfaSRobert Watson * from Linux. 1219b8af5dfaSRobert Watson */ 1220b8af5dfaSRobert Watson static void 1221b8af5dfaSRobert Watson tcp_fill_info(tp, ti) 1222b8af5dfaSRobert Watson struct tcpcb *tp; 1223b8af5dfaSRobert Watson struct tcp_info *ti; 1224b8af5dfaSRobert Watson { 1225b8af5dfaSRobert Watson 1226b8af5dfaSRobert Watson INP_LOCK_ASSERT(tp->t_inpcb); 1227b8af5dfaSRobert Watson bzero(ti, sizeof(*ti)); 1228b8af5dfaSRobert Watson 1229b8af5dfaSRobert Watson ti->tcpi_state = tp->t_state; 1230b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1231b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 1232b8af5dfaSRobert Watson if (tp->sack_enable) 1233b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_SACK; 1234b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1235b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_WSCALE; 1236b8af5dfaSRobert Watson ti->tcpi_snd_wscale = tp->snd_scale; 1237b8af5dfaSRobert Watson ti->tcpi_rcv_wscale = tp->rcv_scale; 1238b8af5dfaSRobert Watson } 1239b8af5dfaSRobert Watson ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1240b8af5dfaSRobert Watson ti->tcpi_snd_cwnd = tp->snd_cwnd; 1241b8af5dfaSRobert Watson 1242b8af5dfaSRobert Watson /* 1243b8af5dfaSRobert Watson * FreeBSD-specific extension fields for tcp_info. 1244b8af5dfaSRobert Watson */ 1245c8443a1dSRobert Watson ti->tcpi_rcv_space = tp->rcv_wnd; 1246b8af5dfaSRobert Watson ti->tcpi_snd_wnd = tp->snd_wnd; 1247b8af5dfaSRobert Watson ti->tcpi_snd_bwnd = tp->snd_bwnd; 1248b8af5dfaSRobert Watson } 1249b8af5dfaSRobert Watson 1250b8af5dfaSRobert Watson /* 1251cfe8b629SGarrett Wollman * The new sockopt interface makes it possible for us to block in the 1252cfe8b629SGarrett Wollman * copyin/out step (if we take a page fault). Taking a page fault at 1253cfe8b629SGarrett Wollman * splnet() is probably a Bad Thing. (Since sockets and pcbs both now 1254cfe8b629SGarrett Wollman * use TSM, there probably isn't any need for this function to run at 1255cfe8b629SGarrett Wollman * splnet() any more. This needs more examination.) 1256b8af5dfaSRobert Watson * 1257b8af5dfaSRobert Watson * XXXRW: The locking here is wrong; we may take a page fault while holding 1258b8af5dfaSRobert Watson * the inpcb lock. 1259cfe8b629SGarrett Wollman */ 1260df8bae1dSRodney W. Grimes int 1261cfe8b629SGarrett Wollman tcp_ctloutput(so, sopt) 1262df8bae1dSRodney W. Grimes struct socket *so; 1263cfe8b629SGarrett Wollman struct sockopt *sopt; 1264df8bae1dSRodney W. Grimes { 12653f9d1ef9SRobert Watson int error, opt, optval; 1266df8bae1dSRodney W. Grimes struct inpcb *inp; 1267cfe8b629SGarrett Wollman struct tcpcb *tp; 1268b8af5dfaSRobert Watson struct tcp_info ti; 1269df8bae1dSRodney W. Grimes 1270cfe8b629SGarrett Wollman error = 0; 1271df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1272623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 1273f76fcf6dSJeffrey Hsu INP_LOCK(inp); 1274cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_TCP) { 12754e397bc5SRobert Watson INP_UNLOCK(inp); 1276fb59c426SYoshinobu Inoue #ifdef INET6 1277fb59c426SYoshinobu Inoue if (INP_CHECK_SOCKAF(so, AF_INET6)) 1278fb59c426SYoshinobu Inoue error = ip6_ctloutput(so, sopt); 1279fb59c426SYoshinobu Inoue else 1280fb59c426SYoshinobu Inoue #endif /* INET6 */ 1281cfe8b629SGarrett Wollman error = ip_ctloutput(so, sopt); 1282df8bae1dSRodney W. Grimes return (error); 1283df8bae1dSRodney W. Grimes } 1284623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 1285623dce13SRobert Watson error = ECONNRESET; 1286623dce13SRobert Watson goto out; 1287623dce13SRobert Watson } 1288df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 1289df8bae1dSRodney W. Grimes 1290cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 1291cfe8b629SGarrett Wollman case SOPT_SET: 1292cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 12931cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 129488f6b043SBruce M Simpson case TCP_MD5SIG: 12951cfd4b53SBruce M Simpson error = sooptcopyin(sopt, &optval, sizeof optval, 12961cfd4b53SBruce M Simpson sizeof optval); 12971cfd4b53SBruce M Simpson if (error) 12981cfd4b53SBruce M Simpson break; 12991cfd4b53SBruce M Simpson 13001cfd4b53SBruce M Simpson if (optval > 0) 13011cfd4b53SBruce M Simpson tp->t_flags |= TF_SIGNATURE; 13021cfd4b53SBruce M Simpson else 13031cfd4b53SBruce M Simpson tp->t_flags &= ~TF_SIGNATURE; 13041cfd4b53SBruce M Simpson break; 13051cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */ 1306df8bae1dSRodney W. Grimes case TCP_NODELAY: 1307cfe8b629SGarrett Wollman case TCP_NOOPT: 1308cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1309cfe8b629SGarrett Wollman sizeof optval); 1310cfe8b629SGarrett Wollman if (error) 1311cfe8b629SGarrett Wollman break; 1312cfe8b629SGarrett Wollman 1313cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1314cfe8b629SGarrett Wollman case TCP_NODELAY: 1315cfe8b629SGarrett Wollman opt = TF_NODELAY; 1316cfe8b629SGarrett Wollman break; 1317cfe8b629SGarrett Wollman case TCP_NOOPT: 1318cfe8b629SGarrett Wollman opt = TF_NOOPT; 1319cfe8b629SGarrett Wollman break; 1320cfe8b629SGarrett Wollman default: 1321cfe8b629SGarrett Wollman opt = 0; /* dead code to fool gcc */ 1322cfe8b629SGarrett Wollman break; 1323cfe8b629SGarrett Wollman } 1324cfe8b629SGarrett Wollman 1325cfe8b629SGarrett Wollman if (optval) 1326cfe8b629SGarrett Wollman tp->t_flags |= opt; 1327df8bae1dSRodney W. Grimes else 1328cfe8b629SGarrett Wollman tp->t_flags &= ~opt; 1329df8bae1dSRodney W. Grimes break; 1330df8bae1dSRodney W. Grimes 1331007581c0SJonathan Lemon case TCP_NOPUSH: 1332007581c0SJonathan Lemon error = sooptcopyin(sopt, &optval, sizeof optval, 1333007581c0SJonathan Lemon sizeof optval); 1334007581c0SJonathan Lemon if (error) 1335007581c0SJonathan Lemon break; 1336007581c0SJonathan Lemon 1337007581c0SJonathan Lemon if (optval) 1338007581c0SJonathan Lemon tp->t_flags |= TF_NOPUSH; 1339007581c0SJonathan Lemon else { 1340007581c0SJonathan Lemon tp->t_flags &= ~TF_NOPUSH; 1341007581c0SJonathan Lemon error = tcp_output(tp); 1342007581c0SJonathan Lemon } 1343007581c0SJonathan Lemon break; 1344007581c0SJonathan Lemon 1345df8bae1dSRodney W. Grimes case TCP_MAXSEG: 1346cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1347cfe8b629SGarrett Wollman sizeof optval); 1348cfe8b629SGarrett Wollman if (error) 1349df8bae1dSRodney W. Grimes break; 1350df8bae1dSRodney W. Grimes 135153369ac9SAndre Oppermann if (optval > 0 && optval <= tp->t_maxseg && 135253369ac9SAndre Oppermann optval + 40 >= tcp_minmss) 1353cfe8b629SGarrett Wollman tp->t_maxseg = optval; 1354a0292f23SGarrett Wollman else 1355a0292f23SGarrett Wollman error = EINVAL; 1356a0292f23SGarrett Wollman break; 1357a0292f23SGarrett Wollman 1358b8af5dfaSRobert Watson case TCP_INFO: 1359b8af5dfaSRobert Watson error = EINVAL; 1360b8af5dfaSRobert Watson break; 1361b8af5dfaSRobert Watson 1362df8bae1dSRodney W. Grimes default: 1363df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1364df8bae1dSRodney W. Grimes break; 1365df8bae1dSRodney W. Grimes } 1366df8bae1dSRodney W. Grimes break; 1367df8bae1dSRodney W. Grimes 1368cfe8b629SGarrett Wollman case SOPT_GET: 1369cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 13701cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 137188f6b043SBruce M Simpson case TCP_MD5SIG: 13721cfd4b53SBruce M Simpson optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0; 1373b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 13741cfd4b53SBruce M Simpson break; 1375265ed012SBruce M Simpson #endif 1376df8bae1dSRodney W. Grimes case TCP_NODELAY: 1377cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NODELAY; 1378b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1379df8bae1dSRodney W. Grimes break; 1380df8bae1dSRodney W. Grimes case TCP_MAXSEG: 1381cfe8b629SGarrett Wollman optval = tp->t_maxseg; 1382b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1383df8bae1dSRodney W. Grimes break; 1384a0292f23SGarrett Wollman case TCP_NOOPT: 1385cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOOPT; 1386b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1387a0292f23SGarrett Wollman break; 1388a0292f23SGarrett Wollman case TCP_NOPUSH: 1389cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOPUSH; 1390b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1391b8af5dfaSRobert Watson break; 1392b8af5dfaSRobert Watson case TCP_INFO: 1393b8af5dfaSRobert Watson tcp_fill_info(tp, &ti); 1394b8af5dfaSRobert Watson error = sooptcopyout(sopt, &ti, sizeof ti); 1395a0292f23SGarrett Wollman break; 1396df8bae1dSRodney W. Grimes default: 1397df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1398df8bae1dSRodney W. Grimes break; 1399df8bae1dSRodney W. Grimes } 1400df8bae1dSRodney W. Grimes break; 1401df8bae1dSRodney W. Grimes } 1402623dce13SRobert Watson out: 1403f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 1404df8bae1dSRodney W. Grimes return (error); 1405df8bae1dSRodney W. Grimes } 1406df8bae1dSRodney W. Grimes 140726e30fbbSDavid Greenman /* 140826e30fbbSDavid Greenman * tcp_sendspace and tcp_recvspace are the default send and receive window 140926e30fbbSDavid Greenman * sizes, respectively. These are obsolescent (this information should 141026e30fbbSDavid Greenman * be set by the route). 141126e30fbbSDavid Greenman */ 141281e561cdSDavid E. O'Brien u_long tcp_sendspace = 1024*32; 1413e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW, 14143d177f46SBill Fumerola &tcp_sendspace , 0, "Maximum outgoing TCP datagram size"); 141581e561cdSDavid E. O'Brien u_long tcp_recvspace = 1024*64; 1416e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 14173d177f46SBill Fumerola &tcp_recvspace , 0, "Maximum incoming TCP datagram size"); 1418df8bae1dSRodney W. Grimes 1419df8bae1dSRodney W. Grimes /* 1420df8bae1dSRodney W. Grimes * Attach TCP protocol to socket, allocating 1421df8bae1dSRodney W. Grimes * internet protocol control block, tcp control block, 1422df8bae1dSRodney W. Grimes * bufer space, and entering LISTEN state if to accept connections. 1423df8bae1dSRodney W. Grimes */ 14240312fbe9SPoul-Henning Kamp static int 142556dc72c3SPawel Jakub Dawidek tcp_attach(so) 1426df8bae1dSRodney W. Grimes struct socket *so; 1427df8bae1dSRodney W. Grimes { 1428df8bae1dSRodney W. Grimes register struct tcpcb *tp; 1429df8bae1dSRodney W. Grimes struct inpcb *inp; 1430df8bae1dSRodney W. Grimes int error; 1431fb59c426SYoshinobu Inoue #ifdef INET6 14324a6a94d8SArchie Cobbs int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0; 1433fb59c426SYoshinobu Inoue #endif 1434df8bae1dSRodney W. Grimes 1435df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1436df8bae1dSRodney W. Grimes error = soreserve(so, tcp_sendspace, tcp_recvspace); 1437df8bae1dSRodney W. Grimes if (error) 1438df8bae1dSRodney W. Grimes return (error); 1439df8bae1dSRodney W. Grimes } 1440f2de87feSRobert Watson INP_INFO_WLOCK(&tcbinfo); 1441d915b280SStephan Uphoff error = in_pcballoc(so, &tcbinfo); 1442f2de87feSRobert Watson if (error) { 1443f2de87feSRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 1444df8bae1dSRodney W. Grimes return (error); 1445f2de87feSRobert Watson } 1446df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1447fb59c426SYoshinobu Inoue #ifdef INET6 1448fb59c426SYoshinobu Inoue if (isipv6) { 1449fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 1450fb59c426SYoshinobu Inoue inp->in6p_hops = -1; /* use kernel default */ 1451fb59c426SYoshinobu Inoue } 1452fb59c426SYoshinobu Inoue else 1453fb59c426SYoshinobu Inoue #endif 1454cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 1455df8bae1dSRodney W. Grimes tp = tcp_newtcpcb(inp); 1456623dce13SRobert Watson if (tp == NULL) { 1457fb59c426SYoshinobu Inoue #ifdef INET6 1458623dce13SRobert Watson if (isipv6) { 1459fb59c426SYoshinobu Inoue in6_pcbdetach(inp); 1460623dce13SRobert Watson in6_pcbfree(inp); 1461623dce13SRobert Watson } else { 1462fb59c426SYoshinobu Inoue #endif 1463df8bae1dSRodney W. Grimes in_pcbdetach(inp); 1464623dce13SRobert Watson in_pcbfree(inp); 1465623dce13SRobert Watson #ifdef INET6 1466623dce13SRobert Watson } 1467623dce13SRobert Watson #endif 1468f2de87feSRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 1469df8bae1dSRodney W. Grimes return (ENOBUFS); 1470df8bae1dSRodney W. Grimes } 1471df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 1472f2de87feSRobert Watson INP_UNLOCK(inp); 1473f2de87feSRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 1474df8bae1dSRodney W. Grimes return (0); 1475df8bae1dSRodney W. Grimes } 1476df8bae1dSRodney W. Grimes 1477df8bae1dSRodney W. Grimes /* 1478df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 1479df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 1480df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 1481df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 1482df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 1483df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 1484df8bae1dSRodney W. Grimes */ 1485623dce13SRobert Watson static void 1486df8bae1dSRodney W. Grimes tcp_disconnect(tp) 1487df8bae1dSRodney W. Grimes register struct tcpcb *tp; 1488df8bae1dSRodney W. Grimes { 1489e6e0b5ffSRobert Watson struct inpcb *inp = tp->t_inpcb; 1490e6e0b5ffSRobert Watson struct socket *so = inp->inp_socket; 1491e6e0b5ffSRobert Watson 1492e6e0b5ffSRobert Watson INP_INFO_WLOCK_ASSERT(&tcbinfo); 1493e6e0b5ffSRobert Watson INP_LOCK_ASSERT(inp); 1494df8bae1dSRodney W. Grimes 1495623dce13SRobert Watson /* 1496623dce13SRobert Watson * Neither tcp_close() nor tcp_drop() should return NULL, as the 1497623dce13SRobert Watson * socket is still open. 1498623dce13SRobert Watson */ 1499623dce13SRobert Watson if (tp->t_state < TCPS_ESTABLISHED) { 1500df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1501623dce13SRobert Watson KASSERT(tp != NULL, 1502623dce13SRobert Watson ("tcp_disconnect: tcp_close() returned NULL")); 1503623dce13SRobert Watson } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 1504243917feSSeigo Tanimura tp = tcp_drop(tp, 0); 1505623dce13SRobert Watson KASSERT(tp != NULL, 1506623dce13SRobert Watson ("tcp_disconnect: tcp_drop() returned NULL")); 1507623dce13SRobert Watson } else { 1508df8bae1dSRodney W. Grimes soisdisconnecting(so); 1509df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 1510623dce13SRobert Watson tcp_usrclosed(tp); 1511953b5606SRobert Watson if (!(inp->inp_vflag & INP_DROPPED)) 1512623dce13SRobert Watson tcp_output(tp); 1513df8bae1dSRodney W. Grimes } 1514df8bae1dSRodney W. Grimes } 1515df8bae1dSRodney W. Grimes 1516df8bae1dSRodney W. Grimes /* 1517df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 1518df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 1519df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1520df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 1521df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 1522df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 1523df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 1524df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 1525df8bae1dSRodney W. Grimes */ 1526623dce13SRobert Watson static void 1527df8bae1dSRodney W. Grimes tcp_usrclosed(tp) 1528df8bae1dSRodney W. Grimes register struct tcpcb *tp; 1529df8bae1dSRodney W. Grimes { 1530df8bae1dSRodney W. Grimes 1531e6e0b5ffSRobert Watson INP_INFO_WLOCK_ASSERT(&tcbinfo); 1532e6e0b5ffSRobert Watson INP_LOCK_ASSERT(tp->t_inpcb); 1533e6e0b5ffSRobert Watson 1534df8bae1dSRodney W. Grimes switch (tp->t_state) { 1535df8bae1dSRodney W. Grimes 1536df8bae1dSRodney W. Grimes case TCPS_CLOSED: 1537df8bae1dSRodney W. Grimes case TCPS_LISTEN: 1538df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 1539df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1540623dce13SRobert Watson /* 1541623dce13SRobert Watson * tcp_close() should never return NULL here as the socket is 1542623dce13SRobert Watson * still open. 1543623dce13SRobert Watson */ 1544623dce13SRobert Watson KASSERT(tp != NULL, 1545623dce13SRobert Watson ("tcp_usrclosed: tcp_close() returned NULL")); 1546df8bae1dSRodney W. Grimes break; 1547df8bae1dSRodney W. Grimes 1548a0292f23SGarrett Wollman case TCPS_SYN_SENT: 1549df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1550a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 1551a0292f23SGarrett Wollman break; 1552a0292f23SGarrett Wollman 1553df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1554df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_1; 1555df8bae1dSRodney W. Grimes break; 1556df8bae1dSRodney W. Grimes 1557df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1558df8bae1dSRodney W. Grimes tp->t_state = TCPS_LAST_ACK; 1559df8bae1dSRodney W. Grimes break; 1560df8bae1dSRodney W. Grimes } 1561b6239c4aSAndras Olah if (tp && tp->t_state >= TCPS_FIN_WAIT_2) { 1562df8bae1dSRodney W. Grimes soisdisconnected(tp->t_inpcb->inp_socket); 1563b6239c4aSAndras Olah /* To prevent the connection hanging in FIN_WAIT_2 forever. */ 1564b6239c4aSAndras Olah if (tp->t_state == TCPS_FIN_WAIT_2) 15659b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, tcp_maxidle, 15669b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 1567b6239c4aSAndras Olah } 1568df8bae1dSRodney W. Grimes } 1569