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")); 123f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 1242c37256eSGarrett Wollman TCPDEBUG1(); 1252c37256eSGarrett Wollman 12656dc72c3SPawel Jakub Dawidek error = tcp_attach(so); 1272c37256eSGarrett Wollman if (error) 1282c37256eSGarrett Wollman goto out; 1292c37256eSGarrett Wollman 1302c37256eSGarrett Wollman if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1313879597fSAndrey A. Chernov so->so_linger = TCP_LINGERTIME; 132f76fcf6dSJeffrey Hsu 133f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 134f76fcf6dSJeffrey Hsu tp = intotcpcb(inp); 1352c37256eSGarrett Wollman out: 1362c37256eSGarrett Wollman TCPDEBUG2(PRU_ATTACH); 137f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 1382c37256eSGarrett Wollman return error; 1392c37256eSGarrett Wollman } 1402c37256eSGarrett Wollman 1412c37256eSGarrett Wollman /* 1422c37256eSGarrett Wollman * pru_detach() detaches the TCP protocol from the socket. 1432c37256eSGarrett Wollman * If the protocol state is non-embryonic, then can't 1442c37256eSGarrett Wollman * do this directly: have to initiate a pru_disconnect(), 1452c37256eSGarrett Wollman * which may finish later; embryonic TCB's can just 1462c37256eSGarrett Wollman * be discarded here. 1472c37256eSGarrett Wollman */ 148bc725eafSRobert Watson static void 1492c37256eSGarrett Wollman tcp_usr_detach(struct socket *so) 1502c37256eSGarrett Wollman { 151f76fcf6dSJeffrey Hsu struct inpcb *inp; 1522c37256eSGarrett Wollman struct tcpcb *tp; 153623dce13SRobert Watson #ifdef INET6 154623dce13SRobert Watson int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0; 155623dce13SRobert Watson #endif 1562c37256eSGarrett Wollman TCPDEBUG0; 1572c37256eSGarrett Wollman 158f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 159623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL")); 160623dce13SRobert Watson INP_INFO_WLOCK(&tcbinfo); 161f76fcf6dSJeffrey Hsu INP_LOCK(inp); 162623dce13SRobert Watson KASSERT(inp->inp_socket != NULL, 163623dce13SRobert Watson ("tcp_usr_detach: inp_socket == NULL")); 1642c37256eSGarrett Wollman 165623dce13SRobert Watson TCPDEBUG1(); 166623dce13SRobert Watson tp = intotcpcb(inp); 167623dce13SRobert Watson 168623dce13SRobert Watson if (inp->inp_vflag & INP_TIMEWAIT) { 169623dce13SRobert Watson if (inp->inp_vflag & INP_DROPPED) { 170623dce13SRobert Watson /* 171623dce13SRobert Watson * Connection was in time wait and has been dropped; 172623dce13SRobert Watson * the calling path is via tcp_twclose(), which will 173623dce13SRobert Watson * free the tcptw, so we can discard the remainder. 174623dce13SRobert Watson * 175623dce13SRobert Watson * XXXRW: Would it be cleaner to free the tcptw 176623dce13SRobert Watson * here? 177623dce13SRobert Watson */ 178623dce13SRobert Watson #ifdef INET6 179623dce13SRobert Watson if (isipv6) { 180623dce13SRobert Watson in6_pcbdetach(inp); 181623dce13SRobert Watson in6_pcbfree(inp); 182623dce13SRobert Watson } else { 183623dce13SRobert Watson #endif 184623dce13SRobert Watson in_pcbdetach(inp); 185623dce13SRobert Watson in_pcbfree(inp); 186623dce13SRobert Watson #ifdef INET6 187623dce13SRobert Watson } 188623dce13SRobert Watson #endif 189623dce13SRobert Watson } else { 190623dce13SRobert Watson /* 191623dce13SRobert Watson * Connection is in time wait and has not yet been 192623dce13SRobert Watson * dropped; allow the socket to be discarded, but 193623dce13SRobert Watson * need to keep inpcb until end of time wait. 194623dce13SRobert Watson */ 195623dce13SRobert Watson #ifdef INET6 196623dce13SRobert Watson if (isipv6) 197623dce13SRobert Watson in6_pcbdetach(inp); 198623dce13SRobert Watson else 199623dce13SRobert Watson #endif 200623dce13SRobert Watson in_pcbdetach(inp); 201f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 202623dce13SRobert Watson } 203623dce13SRobert Watson } else { 204623dce13SRobert Watson tp = intotcpcb(inp); 205623dce13SRobert Watson if (inp->inp_vflag & INP_DROPPED || 206623dce13SRobert Watson tp->t_state < TCPS_SYN_SENT) { 207623dce13SRobert Watson /* 208623dce13SRobert Watson * Connection has been dropped or is a listen socket, 209623dce13SRobert Watson * tear down all pcb state and allow socket to be 210623dce13SRobert Watson * freed. 211623dce13SRobert Watson */ 212623dce13SRobert Watson tcp_discardcb(tp); 213623dce13SRobert Watson #ifdef INET6 214623dce13SRobert Watson if (isipv6) { 215623dce13SRobert Watson in_pcbdetach(inp); 216623dce13SRobert Watson in_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 { 225623dce13SRobert Watson /* 226623dce13SRobert Watson * Connection state still required, as is socket, so 227623dce13SRobert Watson * mark socket for TCP to free later. 228623dce13SRobert Watson */ 229623dce13SRobert Watson SOCK_LOCK(so); 230623dce13SRobert Watson so->so_state |= SS_PROTOREF; 231623dce13SRobert Watson SOCK_UNLOCK(so); 232623dce13SRobert Watson inp->inp_vflag |= INP_SOCKREF; 233623dce13SRobert Watson INP_UNLOCK(inp); 234623dce13SRobert Watson } 235623dce13SRobert Watson } 236623dce13SRobert Watson tp = NULL; 237623dce13SRobert Watson TCPDEBUG2(PRU_DETACH); 238f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 2392c37256eSGarrett Wollman } 2402c37256eSGarrett Wollman 2412c37256eSGarrett Wollman /* 2422c37256eSGarrett Wollman * Give the socket an address. 2432c37256eSGarrett Wollman */ 2442c37256eSGarrett Wollman static int 245b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 2462c37256eSGarrett Wollman { 2472c37256eSGarrett Wollman int error = 0; 248f76fcf6dSJeffrey Hsu struct inpcb *inp; 249623dce13SRobert Watson struct tcpcb *tp = NULL; 2502c37256eSGarrett Wollman struct sockaddr_in *sinp; 2512c37256eSGarrett Wollman 25252710de1SPawel Jakub Dawidek sinp = (struct sockaddr_in *)nam; 25352710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof (*sinp)) 25452710de1SPawel Jakub Dawidek return (EINVAL); 2552c37256eSGarrett Wollman /* 2562c37256eSGarrett Wollman * Must check for multicast addresses and disallow binding 2572c37256eSGarrett Wollman * to them. 2582c37256eSGarrett Wollman */ 2592c37256eSGarrett Wollman if (sinp->sin_family == AF_INET && 26052710de1SPawel Jakub Dawidek IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 26152710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 26252710de1SPawel Jakub Dawidek 263623dce13SRobert Watson TCPDEBUG0; 264623dce13SRobert Watson INP_INFO_WLOCK(&tcbinfo); 265623dce13SRobert Watson inp = sotoinpcb(so); 266623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 267623dce13SRobert Watson INP_LOCK(inp); 268623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 269623dce13SRobert Watson error = EINVAL; 2702c37256eSGarrett Wollman goto out; 271623dce13SRobert Watson } 272623dce13SRobert Watson tp = intotcpcb(inp); 273623dce13SRobert Watson TCPDEBUG1(); 274623dce13SRobert Watson error = in_pcbbind(inp, nam, td->td_ucred); 275623dce13SRobert Watson out: 276623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 277623dce13SRobert Watson INP_UNLOCK(inp); 278623dce13SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 279623dce13SRobert Watson 280623dce13SRobert Watson return (error); 2812c37256eSGarrett Wollman } 2822c37256eSGarrett Wollman 283fb59c426SYoshinobu Inoue #ifdef INET6 284fb59c426SYoshinobu Inoue static int 285b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 286fb59c426SYoshinobu Inoue { 287fb59c426SYoshinobu Inoue int error = 0; 288f76fcf6dSJeffrey Hsu struct inpcb *inp; 289623dce13SRobert Watson struct tcpcb *tp = NULL; 290fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 291fb59c426SYoshinobu Inoue 29252710de1SPawel Jakub Dawidek sin6p = (struct sockaddr_in6 *)nam; 29352710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof (*sin6p)) 29452710de1SPawel Jakub Dawidek return (EINVAL); 295fb59c426SYoshinobu Inoue /* 296fb59c426SYoshinobu Inoue * Must check for multicast addresses and disallow binding 297fb59c426SYoshinobu Inoue * to them. 298fb59c426SYoshinobu Inoue */ 299fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 && 30052710de1SPawel Jakub Dawidek IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 30152710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 30252710de1SPawel Jakub Dawidek 303623dce13SRobert Watson TCPDEBUG0; 304623dce13SRobert Watson INP_INFO_WLOCK(&tcbinfo); 305623dce13SRobert Watson inp = sotoinpcb(so); 306623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 307623dce13SRobert Watson INP_LOCK(inp); 308623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 309623dce13SRobert Watson error = EINVAL; 310623dce13SRobert Watson goto out; 311623dce13SRobert Watson } 312623dce13SRobert Watson tp = intotcpcb(inp); 313623dce13SRobert Watson TCPDEBUG1(); 314fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 315fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 31666ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 317fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) 318fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 319fb59c426SYoshinobu Inoue else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 320fb59c426SYoshinobu Inoue struct sockaddr_in sin; 321fb59c426SYoshinobu Inoue 322fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 323fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 324fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 325b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)&sin, 326b0330ed9SPawel Jakub Dawidek td->td_ucred); 327fb59c426SYoshinobu Inoue goto out; 328fb59c426SYoshinobu Inoue } 329fb59c426SYoshinobu Inoue } 330b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, nam, td->td_ucred); 331623dce13SRobert Watson out: 332623dce13SRobert Watson TCPDEBUG2(PRU_BIND); 333623dce13SRobert Watson INP_UNLOCK(inp); 334623dce13SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 335623dce13SRobert Watson return (error); 336fb59c426SYoshinobu Inoue } 337fb59c426SYoshinobu Inoue #endif /* INET6 */ 338fb59c426SYoshinobu Inoue 3392c37256eSGarrett Wollman /* 3402c37256eSGarrett Wollman * Prepare to accept connections. 3412c37256eSGarrett Wollman */ 3422c37256eSGarrett Wollman static int 343d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td) 3442c37256eSGarrett Wollman { 3452c37256eSGarrett Wollman int error = 0; 346f76fcf6dSJeffrey Hsu struct inpcb *inp; 347623dce13SRobert Watson struct tcpcb *tp = NULL; 3482c37256eSGarrett Wollman 349623dce13SRobert Watson TCPDEBUG0; 350623dce13SRobert Watson INP_INFO_WLOCK(&tcbinfo); 351623dce13SRobert Watson inp = sotoinpcb(so); 352623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 353623dce13SRobert Watson INP_LOCK(inp); 354623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 355623dce13SRobert Watson error = EINVAL; 356623dce13SRobert Watson goto out; 357623dce13SRobert Watson } 358623dce13SRobert Watson tp = intotcpcb(inp); 359623dce13SRobert Watson TCPDEBUG1(); 3600daccb9cSRobert Watson SOCK_LOCK(so); 3610daccb9cSRobert Watson error = solisten_proto_check(so); 3620daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) 363b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 3640daccb9cSRobert Watson if (error == 0) { 3652c37256eSGarrett Wollman tp->t_state = TCPS_LISTEN; 366d374e81eSRobert Watson solisten_proto(so, backlog); 3670daccb9cSRobert Watson } 3680daccb9cSRobert Watson SOCK_UNLOCK(so); 369623dce13SRobert Watson 370623dce13SRobert Watson out: 371623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 372623dce13SRobert Watson INP_UNLOCK(inp); 373623dce13SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 374623dce13SRobert Watson return (error); 3752c37256eSGarrett Wollman } 3762c37256eSGarrett Wollman 377fb59c426SYoshinobu Inoue #ifdef INET6 378fb59c426SYoshinobu Inoue static int 379d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 380fb59c426SYoshinobu Inoue { 381fb59c426SYoshinobu Inoue int error = 0; 382f76fcf6dSJeffrey Hsu struct inpcb *inp; 383623dce13SRobert Watson struct tcpcb *tp = NULL; 384fb59c426SYoshinobu Inoue 385623dce13SRobert Watson TCPDEBUG0; 386623dce13SRobert Watson INP_INFO_WLOCK(&tcbinfo); 387623dce13SRobert Watson inp = sotoinpcb(so); 388623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL")); 389623dce13SRobert Watson INP_LOCK(inp); 390623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 391623dce13SRobert Watson error = EINVAL; 392623dce13SRobert Watson goto out; 393623dce13SRobert Watson } 394623dce13SRobert Watson tp = intotcpcb(inp); 395623dce13SRobert Watson TCPDEBUG1(); 3960daccb9cSRobert Watson SOCK_LOCK(so); 3970daccb9cSRobert Watson error = solisten_proto_check(so); 3980daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) { 399fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 40066ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 401fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 402b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 403fb59c426SYoshinobu Inoue } 4040daccb9cSRobert Watson if (error == 0) { 405fb59c426SYoshinobu Inoue tp->t_state = TCPS_LISTEN; 406d374e81eSRobert Watson solisten_proto(so, backlog); 4070daccb9cSRobert Watson } 4080daccb9cSRobert Watson SOCK_UNLOCK(so); 409623dce13SRobert Watson 410623dce13SRobert Watson out: 411623dce13SRobert Watson TCPDEBUG2(PRU_LISTEN); 412623dce13SRobert Watson INP_UNLOCK(inp); 413623dce13SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 414623dce13SRobert Watson return (error); 415fb59c426SYoshinobu Inoue } 416fb59c426SYoshinobu Inoue #endif /* INET6 */ 417fb59c426SYoshinobu Inoue 4182c37256eSGarrett Wollman /* 4192c37256eSGarrett Wollman * Initiate connection to peer. 4202c37256eSGarrett Wollman * Create a template for use in transmissions on this connection. 4212c37256eSGarrett Wollman * Enter SYN_SENT state, and mark socket as connecting. 4222c37256eSGarrett Wollman * Start keep-alive timer, and seed output sequence space. 4232c37256eSGarrett Wollman * Send initial segment on connection. 4242c37256eSGarrett Wollman */ 4252c37256eSGarrett Wollman static int 426b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 4272c37256eSGarrett Wollman { 4282c37256eSGarrett Wollman int error = 0; 429f76fcf6dSJeffrey Hsu struct inpcb *inp; 430623dce13SRobert Watson struct tcpcb *tp = NULL; 4312c37256eSGarrett Wollman struct sockaddr_in *sinp; 4322c37256eSGarrett Wollman 43357bf258eSGarrett Wollman sinp = (struct sockaddr_in *)nam; 434e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sinp)) 435e29ef13fSDon Lewis return (EINVAL); 43652710de1SPawel Jakub Dawidek /* 43752710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 43852710de1SPawel Jakub Dawidek */ 4392c37256eSGarrett Wollman if (sinp->sin_family == AF_INET 44052710de1SPawel Jakub Dawidek && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 44152710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 442812d8653SSam Leffler if (jailed(td->td_ucred)) 443a854ed98SJohn Baldwin prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr); 44475c13541SPoul-Henning Kamp 445623dce13SRobert Watson TCPDEBUG0; 446623dce13SRobert Watson INP_INFO_WLOCK(&tcbinfo); 447623dce13SRobert Watson inp = sotoinpcb(so); 448623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 449623dce13SRobert Watson INP_LOCK(inp); 450623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 451623dce13SRobert Watson error = EINVAL; 452623dce13SRobert Watson goto out; 453623dce13SRobert Watson } 454623dce13SRobert Watson tp = intotcpcb(inp); 455623dce13SRobert Watson TCPDEBUG1(); 456b40ce416SJulian Elischer if ((error = tcp_connect(tp, nam, td)) != 0) 4572c37256eSGarrett Wollman goto out; 4582c37256eSGarrett Wollman error = tcp_output(tp); 459623dce13SRobert Watson out: 460623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 461623dce13SRobert Watson INP_UNLOCK(inp); 462623dce13SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 463623dce13SRobert Watson return (error); 4642c37256eSGarrett Wollman } 4652c37256eSGarrett Wollman 466fb59c426SYoshinobu Inoue #ifdef INET6 467fb59c426SYoshinobu Inoue static int 468b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 469fb59c426SYoshinobu Inoue { 470fb59c426SYoshinobu Inoue int error = 0; 471f76fcf6dSJeffrey Hsu struct inpcb *inp; 472623dce13SRobert Watson struct tcpcb *tp = NULL; 473fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 474623dce13SRobert Watson 475623dce13SRobert Watson TCPDEBUG0; 476fb59c426SYoshinobu Inoue 477fb59c426SYoshinobu Inoue sin6p = (struct sockaddr_in6 *)nam; 478e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sin6p)) 479e29ef13fSDon Lewis return (EINVAL); 48052710de1SPawel Jakub Dawidek /* 48152710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 48252710de1SPawel Jakub Dawidek */ 483fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 48452710de1SPawel Jakub Dawidek && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 48552710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 486fb59c426SYoshinobu Inoue 487623dce13SRobert Watson INP_INFO_WLOCK(&tcbinfo); 488623dce13SRobert Watson inp = sotoinpcb(so); 489623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 490623dce13SRobert Watson INP_LOCK(inp); 491623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 492623dce13SRobert Watson error = EINVAL; 493623dce13SRobert Watson goto out; 494623dce13SRobert Watson } 495623dce13SRobert Watson tp = intotcpcb(inp); 496623dce13SRobert Watson TCPDEBUG1(); 49733841545SHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 498fb59c426SYoshinobu Inoue struct sockaddr_in sin; 499fb59c426SYoshinobu Inoue 500d46a5312SMaxim Konovalov if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 501d46a5312SMaxim Konovalov error = EINVAL; 502d46a5312SMaxim Konovalov goto out; 503d46a5312SMaxim Konovalov } 50433841545SHajimu UMEMOTO 505fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 506fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 507fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 508b40ce416SJulian Elischer if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 509fb59c426SYoshinobu Inoue goto out; 510fb59c426SYoshinobu Inoue error = tcp_output(tp); 511fb59c426SYoshinobu Inoue goto out; 512fb59c426SYoshinobu Inoue } 513fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 514fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 515b7d6d952SHajimu UMEMOTO inp->inp_inc.inc_isipv6 = 1; 516b40ce416SJulian Elischer if ((error = tcp6_connect(tp, nam, td)) != 0) 517fb59c426SYoshinobu Inoue goto out; 518fb59c426SYoshinobu Inoue error = tcp_output(tp); 519623dce13SRobert Watson 520623dce13SRobert Watson out: 521623dce13SRobert Watson TCPDEBUG2(PRU_CONNECT); 522623dce13SRobert Watson INP_UNLOCK(inp); 523623dce13SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 524623dce13SRobert Watson return (error); 525fb59c426SYoshinobu Inoue } 526fb59c426SYoshinobu Inoue #endif /* INET6 */ 527fb59c426SYoshinobu Inoue 5282c37256eSGarrett Wollman /* 5292c37256eSGarrett Wollman * Initiate disconnect from peer. 5302c37256eSGarrett Wollman * If connection never passed embryonic stage, just drop; 5312c37256eSGarrett Wollman * else if don't need to let data drain, then can just drop anyways, 5322c37256eSGarrett Wollman * else have to begin TCP shutdown process: mark socket disconnecting, 5332c37256eSGarrett Wollman * drain unread data, state switch to reflect user close, and 5342c37256eSGarrett Wollman * send segment (e.g. FIN) to peer. Socket will be really disconnected 5352c37256eSGarrett Wollman * when peer sends FIN and acks ours. 5362c37256eSGarrett Wollman * 5372c37256eSGarrett Wollman * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 5382c37256eSGarrett Wollman */ 5392c37256eSGarrett Wollman static int 5402c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so) 5412c37256eSGarrett Wollman { 542f76fcf6dSJeffrey Hsu struct inpcb *inp; 543623dce13SRobert Watson struct tcpcb *tp = NULL; 544623dce13SRobert Watson int error = 0; 5452c37256eSGarrett Wollman 546623dce13SRobert Watson TCPDEBUG0; 547623dce13SRobert Watson INP_INFO_WLOCK(&tcbinfo); 548623dce13SRobert Watson inp = sotoinpcb(so); 549623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 550623dce13SRobert Watson INP_LOCK(inp); 551623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 552623dce13SRobert Watson error = EINVAL; 553623dce13SRobert Watson goto out; 554623dce13SRobert Watson } 555623dce13SRobert Watson tp = intotcpcb(inp); 556623dce13SRobert Watson TCPDEBUG1(); 557623dce13SRobert Watson tcp_disconnect(tp); 558623dce13SRobert Watson out: 559623dce13SRobert Watson TCPDEBUG2(PRU_DISCONNECT); 560623dce13SRobert Watson INP_UNLOCK(inp); 561623dce13SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 562623dce13SRobert Watson return (error); 5632c37256eSGarrett Wollman } 5642c37256eSGarrett Wollman 5652c37256eSGarrett Wollman /* 5662c37256eSGarrett Wollman * Accept a connection. Essentially all the work is 5672c37256eSGarrett Wollman * done at higher levels; just return the address 5682c37256eSGarrett Wollman * of the peer, storing through addr. 5692c37256eSGarrett Wollman */ 5702c37256eSGarrett Wollman static int 57157bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam) 5722c37256eSGarrett Wollman { 5732c37256eSGarrett Wollman int error = 0; 574f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 5751db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 57626ef6ac4SDon Lewis struct in_addr addr; 57726ef6ac4SDon Lewis in_port_t port = 0; 5781db24ffbSJonathan Lemon TCPDEBUG0; 5792c37256eSGarrett Wollman 580c0647e0dSJonathan Lemon if (so->so_state & SS_ISDISCONNECTED) { 581c0647e0dSJonathan Lemon error = ECONNABORTED; 582c0647e0dSJonathan Lemon goto out; 583c0647e0dSJonathan Lemon } 584f76fcf6dSJeffrey Hsu 585f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 586623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 587f76fcf6dSJeffrey Hsu INP_LOCK(inp); 588623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 589623dce13SRobert Watson error = EINVAL; 590623dce13SRobert Watson goto out; 591623dce13SRobert Watson } 5921db24ffbSJonathan Lemon tp = intotcpcb(inp); 5931db24ffbSJonathan Lemon TCPDEBUG1(); 594f76fcf6dSJeffrey Hsu 595f76fcf6dSJeffrey Hsu /* 59626ef6ac4SDon Lewis * We inline in_setpeeraddr and COMMON_END here, so that we can 59726ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 59826ef6ac4SDon Lewis * release the lock. 599f76fcf6dSJeffrey Hsu */ 60026ef6ac4SDon Lewis port = inp->inp_fport; 60126ef6ac4SDon Lewis addr = inp->inp_faddr; 602f76fcf6dSJeffrey Hsu 603623dce13SRobert Watson out: 604623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 60526ef6ac4SDon Lewis INP_UNLOCK(inp); 60626ef6ac4SDon Lewis if (error == 0) 60726ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 60826ef6ac4SDon Lewis return error; 6092c37256eSGarrett Wollman } 6102c37256eSGarrett Wollman 611fb59c426SYoshinobu Inoue #ifdef INET6 612fb59c426SYoshinobu Inoue static int 613fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 614fb59c426SYoshinobu Inoue { 615f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 616fb59c426SYoshinobu Inoue int error = 0; 6171db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 61826ef6ac4SDon Lewis struct in_addr addr; 61926ef6ac4SDon Lewis struct in6_addr addr6; 62026ef6ac4SDon Lewis in_port_t port = 0; 62126ef6ac4SDon Lewis int v4 = 0; 6221db24ffbSJonathan Lemon TCPDEBUG0; 623fb59c426SYoshinobu Inoue 624c0647e0dSJonathan Lemon if (so->so_state & SS_ISDISCONNECTED) { 625c0647e0dSJonathan Lemon error = ECONNABORTED; 626c0647e0dSJonathan Lemon goto out; 627c0647e0dSJonathan Lemon } 628f76fcf6dSJeffrey Hsu 629f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 630623dce13SRobert Watson KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 631f76fcf6dSJeffrey Hsu INP_LOCK(inp); 632623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 633623dce13SRobert Watson error = EINVAL; 634623dce13SRobert Watson goto out; 635623dce13SRobert Watson } 6361db24ffbSJonathan Lemon tp = intotcpcb(inp); 6371db24ffbSJonathan Lemon TCPDEBUG1(); 638623dce13SRobert Watson 63926ef6ac4SDon Lewis /* 64026ef6ac4SDon Lewis * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 64126ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 64226ef6ac4SDon Lewis * release the lock. 64326ef6ac4SDon Lewis */ 64426ef6ac4SDon Lewis if (inp->inp_vflag & INP_IPV4) { 64526ef6ac4SDon Lewis v4 = 1; 64626ef6ac4SDon Lewis port = inp->inp_fport; 64726ef6ac4SDon Lewis addr = inp->inp_faddr; 64826ef6ac4SDon Lewis } else { 64926ef6ac4SDon Lewis port = inp->inp_fport; 65026ef6ac4SDon Lewis addr6 = inp->in6p_faddr; 65126ef6ac4SDon Lewis } 65226ef6ac4SDon Lewis 653623dce13SRobert Watson out: 654623dce13SRobert Watson TCPDEBUG2(PRU_ACCEPT); 65526ef6ac4SDon Lewis INP_UNLOCK(inp); 65626ef6ac4SDon Lewis if (error == 0) { 65726ef6ac4SDon Lewis if (v4) 65826ef6ac4SDon Lewis *nam = in6_v4mapsin6_sockaddr(port, &addr); 65926ef6ac4SDon Lewis else 66026ef6ac4SDon Lewis *nam = in6_sockaddr(port, &addr6); 66126ef6ac4SDon Lewis } 66226ef6ac4SDon Lewis return error; 663fb59c426SYoshinobu Inoue } 664fb59c426SYoshinobu Inoue #endif /* INET6 */ 665f76fcf6dSJeffrey Hsu 666f76fcf6dSJeffrey Hsu /* 667f76fcf6dSJeffrey Hsu * This is the wrapper function for in_setsockaddr. We just pass down 668f76fcf6dSJeffrey Hsu * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking 669f76fcf6dSJeffrey Hsu * here because in_setsockaddr will call malloc and can block. 670f76fcf6dSJeffrey Hsu */ 671f76fcf6dSJeffrey Hsu static int 672f76fcf6dSJeffrey Hsu tcp_sockaddr(struct socket *so, struct sockaddr **nam) 673f76fcf6dSJeffrey Hsu { 674f76fcf6dSJeffrey Hsu return (in_setsockaddr(so, nam, &tcbinfo)); 675f76fcf6dSJeffrey Hsu } 676f76fcf6dSJeffrey Hsu 677f76fcf6dSJeffrey Hsu /* 678f76fcf6dSJeffrey Hsu * This is the wrapper function for in_setpeeraddr. We just pass down 679f76fcf6dSJeffrey Hsu * the pcbinfo for in_setpeeraddr to lock. 680f76fcf6dSJeffrey Hsu */ 681f76fcf6dSJeffrey Hsu static int 682f76fcf6dSJeffrey Hsu tcp_peeraddr(struct socket *so, struct sockaddr **nam) 683f76fcf6dSJeffrey Hsu { 684f76fcf6dSJeffrey Hsu return (in_setpeeraddr(so, nam, &tcbinfo)); 685f76fcf6dSJeffrey Hsu } 686f76fcf6dSJeffrey Hsu 6872c37256eSGarrett Wollman /* 6882c37256eSGarrett Wollman * Mark the connection as being incapable of further output. 6892c37256eSGarrett Wollman */ 6902c37256eSGarrett Wollman static int 6912c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so) 6922c37256eSGarrett Wollman { 6932c37256eSGarrett Wollman int error = 0; 694f76fcf6dSJeffrey Hsu struct inpcb *inp; 695623dce13SRobert Watson struct tcpcb *tp = NULL; 6962c37256eSGarrett Wollman 697623dce13SRobert Watson TCPDEBUG0; 698623dce13SRobert Watson INP_INFO_WLOCK(&tcbinfo); 699623dce13SRobert Watson inp = sotoinpcb(so); 700623dce13SRobert Watson KASSERT(inp != NULL, ("inp == NULL")); 701623dce13SRobert Watson INP_LOCK(inp); 702623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 703623dce13SRobert Watson error = EINVAL; 704623dce13SRobert Watson goto out; 705623dce13SRobert Watson } 706623dce13SRobert Watson tp = intotcpcb(inp); 707623dce13SRobert Watson TCPDEBUG1(); 7082c37256eSGarrett Wollman socantsendmore(so); 709623dce13SRobert Watson tcp_usrclosed(tp); 7102c37256eSGarrett Wollman error = tcp_output(tp); 711623dce13SRobert Watson 712623dce13SRobert Watson out: 713623dce13SRobert Watson TCPDEBUG2(PRU_SHUTDOWN); 714623dce13SRobert Watson INP_UNLOCK(inp); 715623dce13SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 716623dce13SRobert Watson 717623dce13SRobert Watson return (error); 7182c37256eSGarrett Wollman } 7192c37256eSGarrett Wollman 7202c37256eSGarrett Wollman /* 7212c37256eSGarrett Wollman * After a receive, possibly send window update to peer. 7222c37256eSGarrett Wollman */ 7232c37256eSGarrett Wollman static int 7242c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags) 7252c37256eSGarrett Wollman { 726f76fcf6dSJeffrey Hsu struct inpcb *inp; 727623dce13SRobert Watson struct tcpcb *tp = NULL; 728623dce13SRobert Watson int error = 0; 7292c37256eSGarrett Wollman 730623dce13SRobert Watson TCPDEBUG0; 731623dce13SRobert Watson inp = sotoinpcb(so); 732623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 733623dce13SRobert Watson INP_LOCK(inp); 734623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 735623dce13SRobert Watson error = EINVAL; 736623dce13SRobert Watson goto out; 737623dce13SRobert Watson } 738623dce13SRobert Watson tp = intotcpcb(inp); 739623dce13SRobert Watson TCPDEBUG1(); 7402c37256eSGarrett Wollman tcp_output(tp); 741623dce13SRobert Watson 742623dce13SRobert Watson out: 743623dce13SRobert Watson TCPDEBUG2(PRU_RCVD); 744623dce13SRobert Watson INP_UNLOCK(inp); 745623dce13SRobert Watson return (error); 7462c37256eSGarrett Wollman } 7472c37256eSGarrett Wollman 7482c37256eSGarrett Wollman /* 7492c37256eSGarrett Wollman * Do a send by putting data in output queue and updating urgent 7509c9906e9SPeter Wemm * marker if URG set. Possibly send more data. Unlike the other 7519c9906e9SPeter Wemm * pru_*() routines, the mbuf chains are our responsibility. We 7529c9906e9SPeter Wemm * must either enqueue them or free them. The other pru_* routines 7539c9906e9SPeter Wemm * generally are caller-frees. 7542c37256eSGarrett Wollman */ 7552c37256eSGarrett Wollman static int 75657bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 757b40ce416SJulian Elischer struct sockaddr *nam, struct mbuf *control, struct thread *td) 7582c37256eSGarrett Wollman { 7592c37256eSGarrett Wollman int error = 0; 760f76fcf6dSJeffrey Hsu struct inpcb *inp; 761623dce13SRobert Watson struct tcpcb *tp = NULL; 762623dce13SRobert Watson int headlocked = 0; 763fb59c426SYoshinobu Inoue #ifdef INET6 764fb59c426SYoshinobu Inoue int isipv6; 765fb59c426SYoshinobu Inoue #endif 7669c9906e9SPeter Wemm TCPDEBUG0; 7672c37256eSGarrett Wollman 768f76fcf6dSJeffrey Hsu /* 769623dce13SRobert Watson * We require the pcbinfo lock in two cases: 770623dce13SRobert Watson * 771623dce13SRobert Watson * (1) An implied connect is taking place, which can result in 772623dce13SRobert Watson * binding IPs and ports and hence modification of the pcb hash 773623dce13SRobert Watson * chains. 774623dce13SRobert Watson * 775623dce13SRobert Watson * (2) PRUS_EOF is set, resulting in explicit close on the send. 776f76fcf6dSJeffrey Hsu */ 777623dce13SRobert Watson if ((nam != NULL) || (flags & PRUS_EOF)) { 778f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 779623dce13SRobert Watson headlocked = 1; 780623dce13SRobert Watson } 781f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 782623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 783623dce13SRobert Watson INP_LOCK(inp); 784623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 785623dce13SRobert Watson error = EINVAL; 7869c9906e9SPeter Wemm goto out; 7879c9906e9SPeter Wemm } 788fb59c426SYoshinobu Inoue #ifdef INET6 789fb59c426SYoshinobu Inoue isipv6 = nam && nam->sa_family == AF_INET6; 790fb59c426SYoshinobu Inoue #endif /* INET6 */ 7919c9906e9SPeter Wemm tp = intotcpcb(inp); 7929c9906e9SPeter Wemm TCPDEBUG1(); 7939c9906e9SPeter Wemm if (control) { 7949c9906e9SPeter Wemm /* TCP doesn't do control messages (rights, creds, etc) */ 7959c9906e9SPeter Wemm if (control->m_len) { 7969c9906e9SPeter Wemm m_freem(control); 7972c37256eSGarrett Wollman if (m) 7982c37256eSGarrett Wollman m_freem(m); 799744f87eaSDavid Greenman error = EINVAL; 800744f87eaSDavid Greenman goto out; 8012c37256eSGarrett Wollman } 8029c9906e9SPeter Wemm m_freem(control); /* empty control, just free it */ 8039c9906e9SPeter Wemm } 8042c37256eSGarrett Wollman if (!(flags & PRUS_OOB)) { 805395bb186SSam Leffler sbappendstream(&so->so_snd, m); 8062c37256eSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 8072c37256eSGarrett Wollman /* 8082c37256eSGarrett Wollman * Do implied connect if not yet connected, 8092c37256eSGarrett Wollman * initialize window to default value, and 8102c37256eSGarrett Wollman * initialize maxseg/maxopd using peer's cached 8112c37256eSGarrett Wollman * MSS. 8122c37256eSGarrett Wollman */ 813623dce13SRobert Watson INP_INFO_WLOCK_ASSERT(&tcbinfo); 814fb59c426SYoshinobu Inoue #ifdef INET6 815fb59c426SYoshinobu Inoue if (isipv6) 816b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 817fb59c426SYoshinobu Inoue else 818fb59c426SYoshinobu Inoue #endif /* INET6 */ 819b40ce416SJulian Elischer error = tcp_connect(tp, nam, td); 8202c37256eSGarrett Wollman if (error) 8212c37256eSGarrett Wollman goto out; 8222c37256eSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 8232c37256eSGarrett Wollman tcp_mss(tp, -1); 8242c37256eSGarrett Wollman } 8252c37256eSGarrett Wollman if (flags & PRUS_EOF) { 8262c37256eSGarrett Wollman /* 8272c37256eSGarrett Wollman * Close the send side of the connection after 8282c37256eSGarrett Wollman * the data is sent. 8292c37256eSGarrett Wollman */ 830623dce13SRobert Watson INP_INFO_WLOCK_ASSERT(&tcbinfo); 8312c37256eSGarrett Wollman socantsendmore(so); 832623dce13SRobert Watson tcp_usrclosed(tp); 8332c37256eSGarrett Wollman } 834623dce13SRobert Watson if (headlocked) { 835d1401c90SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 836623dce13SRobert Watson headlocked = 0; 837623dce13SRobert Watson } 838b0acefa8SBill Fenner if (tp != NULL) { 839b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 840b0acefa8SBill Fenner tp->t_flags |= TF_MORETOCOME; 8412c37256eSGarrett Wollman error = tcp_output(tp); 842b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 843b0acefa8SBill Fenner tp->t_flags &= ~TF_MORETOCOME; 844b0acefa8SBill Fenner } 8452c37256eSGarrett Wollman } else { 846623dce13SRobert Watson /* 847623dce13SRobert Watson * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 848623dce13SRobert Watson */ 849d2bc35abSRobert Watson SOCKBUF_LOCK(&so->so_snd); 8502c37256eSGarrett Wollman if (sbspace(&so->so_snd) < -512) { 851d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 8522c37256eSGarrett Wollman m_freem(m); 8532c37256eSGarrett Wollman error = ENOBUFS; 8542c37256eSGarrett Wollman goto out; 8552c37256eSGarrett Wollman } 8562c37256eSGarrett Wollman /* 8572c37256eSGarrett Wollman * According to RFC961 (Assigned Protocols), 8582c37256eSGarrett Wollman * the urgent pointer points to the last octet 8592c37256eSGarrett Wollman * of urgent data. We continue, however, 8602c37256eSGarrett Wollman * to consider it to indicate the first octet 8612c37256eSGarrett Wollman * of data past the urgent section. 8622c37256eSGarrett Wollman * Otherwise, snd_up should be one lower. 8632c37256eSGarrett Wollman */ 864d2bc35abSRobert Watson sbappendstream_locked(&so->so_snd, m); 865d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 866ef53690bSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 867ef53690bSGarrett Wollman /* 868ef53690bSGarrett Wollman * Do implied connect if not yet connected, 869ef53690bSGarrett Wollman * initialize window to default value, and 870ef53690bSGarrett Wollman * initialize maxseg/maxopd using peer's cached 871ef53690bSGarrett Wollman * MSS. 872ef53690bSGarrett Wollman */ 873623dce13SRobert Watson INP_INFO_WLOCK_ASSERT(&tcbinfo); 874fb59c426SYoshinobu Inoue #ifdef INET6 875fb59c426SYoshinobu Inoue if (isipv6) 876b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 877fb59c426SYoshinobu Inoue else 878fb59c426SYoshinobu Inoue #endif /* INET6 */ 879b40ce416SJulian Elischer error = tcp_connect(tp, nam, td); 880ef53690bSGarrett Wollman if (error) 881ef53690bSGarrett Wollman goto out; 882ef53690bSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 883ef53690bSGarrett Wollman tcp_mss(tp, -1); 884d1401c90SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 885623dce13SRobert Watson headlocked = 0; 886623dce13SRobert Watson } else if (nam) { 887623dce13SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 888623dce13SRobert Watson headlocked = 0; 889623dce13SRobert Watson } 8902c37256eSGarrett Wollman tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 8912cdbfa66SPaul Saab tp->t_flags |= TF_FORCEDATA; 8922c37256eSGarrett Wollman error = tcp_output(tp); 8932cdbfa66SPaul Saab tp->t_flags &= ~TF_FORCEDATA; 8942c37256eSGarrett Wollman } 895d1401c90SRobert Watson out: 896d1401c90SRobert Watson TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB : 8972c37256eSGarrett Wollman ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 898d1401c90SRobert Watson INP_UNLOCK(inp); 899623dce13SRobert Watson if (headlocked) 900d1401c90SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 90173fddedaSPeter Grehan return (error); 9022c37256eSGarrett Wollman } 9032c37256eSGarrett Wollman 9042c37256eSGarrett Wollman /* 9052c37256eSGarrett Wollman * Abort the TCP. 9062c37256eSGarrett Wollman */ 907ac45e92fSRobert Watson static void 9082c37256eSGarrett Wollman tcp_usr_abort(struct socket *so) 9092c37256eSGarrett Wollman { 910623dce13SRobert Watson #if 0 911f76fcf6dSJeffrey Hsu struct inpcb *inp; 9122c37256eSGarrett Wollman struct tcpcb *tp; 913623dce13SRobert Watson #endif 9142c37256eSGarrett Wollman 915623dce13SRobert Watson /* 916623dce13SRobert Watson * XXXRW: This is not really quite the same, as we want to tcp_drop() 917623dce13SRobert Watson * rather than tcp_disconnect(), I think, but for now I'll avoid 918623dce13SRobert Watson * replicating all the tear-down logic here. 919623dce13SRobert Watson */ 920623dce13SRobert Watson tcp_usr_detach(so); 921623dce13SRobert Watson 922623dce13SRobert Watson #if 0 923623dce13SRobert Watson TCPDEBUG0; 924ac45e92fSRobert Watson INP_INFO_WLOCK(&tcbinfo); 925ac45e92fSRobert Watson inp = sotoinpcb(so); 926ac45e92fSRobert Watson INP_LOCK(inp); 927623dce13SRobert Watson /* 928623dce13SRobert Watson * Do we need to handle timewait here? Aborted connections should 929623dce13SRobert Watson * never generate a FIN? 930623dce13SRobert Watson */ 931623dce13SRobert Watson KASSERT((inp->inp_vflag & INP_TIMEWAIT) == 0, 932623dce13SRobert Watson ("tcp_usr_abort: timewait")); 933ac45e92fSRobert Watson tp = intotcpcb(inp); 934ac45e92fSRobert Watson TCPDEBUG1(); 9352c37256eSGarrett Wollman tp = tcp_drop(tp, ECONNABORTED); 936ac45e92fSRobert Watson TCPDEBUG2(PRU_ABORT); 937623dce13SRobert Watson if (tp != NULL) 938ac45e92fSRobert Watson INP_UNLOCK(inp); 939ac45e92fSRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 940623dce13SRobert Watson #endif 9412c37256eSGarrett Wollman } 9422c37256eSGarrett Wollman 9432c37256eSGarrett Wollman /* 9442c37256eSGarrett Wollman * Receive out-of-band data. 9452c37256eSGarrett Wollman */ 9462c37256eSGarrett Wollman static int 9472c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 9482c37256eSGarrett Wollman { 9492c37256eSGarrett Wollman int error = 0; 950f76fcf6dSJeffrey Hsu struct inpcb *inp; 951623dce13SRobert Watson struct tcpcb *tp = NULL; 9522c37256eSGarrett Wollman 953623dce13SRobert Watson TCPDEBUG0; 954623dce13SRobert Watson inp = sotoinpcb(so); 955623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 956623dce13SRobert Watson INP_LOCK(inp); 957623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 958623dce13SRobert Watson error = EINVAL; 959623dce13SRobert Watson goto out; 960623dce13SRobert Watson } 961623dce13SRobert Watson tp = intotcpcb(inp); 962623dce13SRobert Watson TCPDEBUG1(); 9632c37256eSGarrett Wollman if ((so->so_oobmark == 0 && 964c0b99ffaSRobert Watson (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 9654cc20ab1SSeigo Tanimura so->so_options & SO_OOBINLINE || 9664cc20ab1SSeigo Tanimura tp->t_oobflags & TCPOOB_HADDATA) { 9672c37256eSGarrett Wollman error = EINVAL; 9682c37256eSGarrett Wollman goto out; 9692c37256eSGarrett Wollman } 9702c37256eSGarrett Wollman if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 9712c37256eSGarrett Wollman error = EWOULDBLOCK; 9722c37256eSGarrett Wollman goto out; 9732c37256eSGarrett Wollman } 9742c37256eSGarrett Wollman m->m_len = 1; 9752c37256eSGarrett Wollman *mtod(m, caddr_t) = tp->t_iobc; 9762c37256eSGarrett Wollman if ((flags & MSG_PEEK) == 0) 9772c37256eSGarrett Wollman tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 978623dce13SRobert Watson 979623dce13SRobert Watson out: 980623dce13SRobert Watson TCPDEBUG2(PRU_RCVOOB); 981623dce13SRobert Watson INP_UNLOCK(inp); 982623dce13SRobert Watson return (error); 9832c37256eSGarrett Wollman } 9842c37256eSGarrett Wollman 9852c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = { 986756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 987756d52a1SPoul-Henning Kamp .pru_accept = tcp_usr_accept, 988756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 989756d52a1SPoul-Henning Kamp .pru_bind = tcp_usr_bind, 990756d52a1SPoul-Henning Kamp .pru_connect = tcp_usr_connect, 991756d52a1SPoul-Henning Kamp .pru_control = in_control, 992756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 993756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 994756d52a1SPoul-Henning Kamp .pru_listen = tcp_usr_listen, 995756d52a1SPoul-Henning Kamp .pru_peeraddr = tcp_peeraddr, 996756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 997756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 998756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 999756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 1000756d52a1SPoul-Henning Kamp .pru_sockaddr = tcp_sockaddr, 1001756d52a1SPoul-Henning Kamp .pru_sosetlabel = in_pcbsosetlabel 10022c37256eSGarrett Wollman }; 1003df8bae1dSRodney W. Grimes 1004fb59c426SYoshinobu Inoue #ifdef INET6 1005fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = { 1006756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 1007756d52a1SPoul-Henning Kamp .pru_accept = tcp6_usr_accept, 1008756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 1009756d52a1SPoul-Henning Kamp .pru_bind = tcp6_usr_bind, 1010756d52a1SPoul-Henning Kamp .pru_connect = tcp6_usr_connect, 1011756d52a1SPoul-Henning Kamp .pru_control = in6_control, 1012756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 1013756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 1014756d52a1SPoul-Henning Kamp .pru_listen = tcp6_usr_listen, 1015756d52a1SPoul-Henning Kamp .pru_peeraddr = in6_mapped_peeraddr, 1016756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 1017756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 1018756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 1019756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 1020756d52a1SPoul-Henning Kamp .pru_sockaddr = in6_mapped_sockaddr, 1021756d52a1SPoul-Henning Kamp .pru_sosetlabel = in_pcbsosetlabel 1022fb59c426SYoshinobu Inoue }; 1023fb59c426SYoshinobu Inoue #endif /* INET6 */ 1024fb59c426SYoshinobu Inoue 1025a0292f23SGarrett Wollman /* 1026a0292f23SGarrett Wollman * Common subroutine to open a TCP connection to remote host specified 1027a0292f23SGarrett Wollman * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 10285200e00eSIan Dowse * port number if needed. Call in_pcbconnect_setup to do the routing and 10295200e00eSIan Dowse * to choose a local host address (interface). If there is an existing 10305200e00eSIan Dowse * incarnation of the same connection in TIME-WAIT state and if the remote 10315200e00eSIan Dowse * host was sending CC options and if the connection duration was < MSL, then 1032a0292f23SGarrett Wollman * truncate the previous TIME-WAIT state and proceed. 1033a0292f23SGarrett Wollman * Initialize connection parameters and enter SYN-SENT state. 1034a0292f23SGarrett Wollman */ 10350312fbe9SPoul-Henning Kamp static int 1036b40ce416SJulian Elischer tcp_connect(tp, nam, td) 1037a0292f23SGarrett Wollman register struct tcpcb *tp; 103857bf258eSGarrett Wollman struct sockaddr *nam; 1039b40ce416SJulian Elischer struct thread *td; 1040a0292f23SGarrett Wollman { 1041a0292f23SGarrett Wollman struct inpcb *inp = tp->t_inpcb, *oinp; 1042a0292f23SGarrett Wollman struct socket *so = inp->inp_socket; 10435200e00eSIan Dowse struct in_addr laddr; 10445200e00eSIan Dowse u_short lport; 1045c3229e05SDavid Greenman int error; 1046a0292f23SGarrett Wollman 1047623dce13SRobert Watson INP_INFO_WLOCK_ASSERT(&tcbinfo); 1048623dce13SRobert Watson INP_LOCK_ASSERT(inp); 1049623dce13SRobert Watson 1050a0292f23SGarrett Wollman if (inp->inp_lport == 0) { 1051b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1052a0292f23SGarrett Wollman if (error) 1053a0292f23SGarrett Wollman return error; 1054a0292f23SGarrett Wollman } 1055a0292f23SGarrett Wollman 1056a0292f23SGarrett Wollman /* 1057a0292f23SGarrett Wollman * Cannot simply call in_pcbconnect, because there might be an 1058a0292f23SGarrett Wollman * earlier incarnation of this same connection still in 1059a0292f23SGarrett Wollman * TIME_WAIT state, creating an ADDRINUSE error. 1060a0292f23SGarrett Wollman */ 10615200e00eSIan Dowse laddr = inp->inp_laddr; 10625200e00eSIan Dowse lport = inp->inp_lport; 10635200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 1064b0330ed9SPawel Jakub Dawidek &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 10655200e00eSIan Dowse if (error && oinp == NULL) 1066d3628763SRodney W. Grimes return error; 1067c94c54e4SAndre Oppermann if (oinp) 1068a0292f23SGarrett Wollman return EADDRINUSE; 10695200e00eSIan Dowse inp->inp_laddr = laddr; 107015bd2b43SDavid Greenman in_pcbrehash(inp); 1071a0292f23SGarrett Wollman 1072a0292f23SGarrett Wollman /* Compute window scaling to request. */ 1073a0292f23SGarrett Wollman while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1074a0292f23SGarrett Wollman (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 1075a0292f23SGarrett Wollman tp->request_r_scale++; 1076a0292f23SGarrett Wollman 1077a0292f23SGarrett Wollman soisconnecting(so); 1078a0292f23SGarrett Wollman tcpstat.tcps_connattempt++; 1079a0292f23SGarrett Wollman tp->t_state = TCPS_SYN_SENT; 10809b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 1081b0e3ad75SMike Silbersack tp->iss = tcp_new_isn(tp); 10821fcc99b5SMatthew Dillon tp->t_bw_rtseq = tp->iss; 1083a0292f23SGarrett Wollman tcp_sendseqinit(tp); 1084a45d2726SAndras Olah 1085a0292f23SGarrett Wollman return 0; 1086a0292f23SGarrett Wollman } 1087a0292f23SGarrett Wollman 1088fb59c426SYoshinobu Inoue #ifdef INET6 1089fb59c426SYoshinobu Inoue static int 1090b40ce416SJulian Elischer tcp6_connect(tp, nam, td) 1091fb59c426SYoshinobu Inoue register struct tcpcb *tp; 1092fb59c426SYoshinobu Inoue struct sockaddr *nam; 1093b40ce416SJulian Elischer struct thread *td; 1094fb59c426SYoshinobu Inoue { 1095fb59c426SYoshinobu Inoue struct inpcb *inp = tp->t_inpcb, *oinp; 1096fb59c426SYoshinobu Inoue struct socket *so = inp->inp_socket; 1097fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 1098fb59c426SYoshinobu Inoue struct in6_addr *addr6; 1099fb59c426SYoshinobu Inoue int error; 1100fb59c426SYoshinobu Inoue 1101623dce13SRobert Watson INP_INFO_WLOCK_ASSERT(&tcbinfo); 1102623dce13SRobert Watson INP_LOCK_ASSERT(inp); 1103623dce13SRobert Watson 1104fb59c426SYoshinobu Inoue if (inp->inp_lport == 0) { 1105b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1106fb59c426SYoshinobu Inoue if (error) 1107fb59c426SYoshinobu Inoue return error; 1108fb59c426SYoshinobu Inoue } 1109fb59c426SYoshinobu Inoue 1110fb59c426SYoshinobu Inoue /* 1111fb59c426SYoshinobu Inoue * Cannot simply call in_pcbconnect, because there might be an 1112fb59c426SYoshinobu Inoue * earlier incarnation of this same connection still in 1113fb59c426SYoshinobu Inoue * TIME_WAIT state, creating an ADDRINUSE error. 1114a1f7e5f8SHajimu UMEMOTO * in6_pcbladdr() also handles scope zone IDs. 1115fb59c426SYoshinobu Inoue */ 1116fb59c426SYoshinobu Inoue error = in6_pcbladdr(inp, nam, &addr6); 1117fb59c426SYoshinobu Inoue if (error) 1118fb59c426SYoshinobu Inoue return error; 1119fb59c426SYoshinobu Inoue oinp = in6_pcblookup_hash(inp->inp_pcbinfo, 1120fb59c426SYoshinobu Inoue &sin6->sin6_addr, sin6->sin6_port, 1121fb59c426SYoshinobu Inoue IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 1122fb59c426SYoshinobu Inoue ? addr6 1123fb59c426SYoshinobu Inoue : &inp->in6p_laddr, 1124fb59c426SYoshinobu Inoue inp->inp_lport, 0, NULL); 1125c94c54e4SAndre Oppermann if (oinp) 1126fb59c426SYoshinobu Inoue return EADDRINUSE; 1127fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 1128fb59c426SYoshinobu Inoue inp->in6p_laddr = *addr6; 1129fb59c426SYoshinobu Inoue inp->in6p_faddr = sin6->sin6_addr; 1130fb59c426SYoshinobu Inoue inp->inp_fport = sin6->sin6_port; 11318a59da30SHajimu UMEMOTO /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 11328a59da30SHajimu UMEMOTO inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 11338a59da30SHajimu UMEMOTO if (inp->in6p_flags & IN6P_AUTOFLOWLABEL) 11348a59da30SHajimu UMEMOTO inp->in6p_flowinfo |= 11358a59da30SHajimu UMEMOTO (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 1136fb59c426SYoshinobu Inoue in_pcbrehash(inp); 1137fb59c426SYoshinobu Inoue 1138fb59c426SYoshinobu Inoue /* Compute window scaling to request. */ 1139fb59c426SYoshinobu Inoue while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1140fb59c426SYoshinobu Inoue (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 1141fb59c426SYoshinobu Inoue tp->request_r_scale++; 1142fb59c426SYoshinobu Inoue 1143fb59c426SYoshinobu Inoue soisconnecting(so); 1144fb59c426SYoshinobu Inoue tcpstat.tcps_connattempt++; 1145fb59c426SYoshinobu Inoue tp->t_state = TCPS_SYN_SENT; 1146fb59c426SYoshinobu Inoue callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 1147b0e3ad75SMike Silbersack tp->iss = tcp_new_isn(tp); 11481fcc99b5SMatthew Dillon tp->t_bw_rtseq = tp->iss; 1149fb59c426SYoshinobu Inoue tcp_sendseqinit(tp); 1150fb59c426SYoshinobu Inoue 1151fb59c426SYoshinobu Inoue return 0; 1152fb59c426SYoshinobu Inoue } 1153fb59c426SYoshinobu Inoue #endif /* INET6 */ 1154fb59c426SYoshinobu Inoue 1155cfe8b629SGarrett Wollman /* 1156b8af5dfaSRobert Watson * Export TCP internal state information via a struct tcp_info, based on the 1157b8af5dfaSRobert Watson * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1158b8af5dfaSRobert Watson * (TCP state machine, etc). We export all information using FreeBSD-native 1159b8af5dfaSRobert Watson * constants -- for example, the numeric values for tcpi_state will differ 1160b8af5dfaSRobert Watson * from Linux. 1161b8af5dfaSRobert Watson */ 1162b8af5dfaSRobert Watson static void 1163b8af5dfaSRobert Watson tcp_fill_info(tp, ti) 1164b8af5dfaSRobert Watson struct tcpcb *tp; 1165b8af5dfaSRobert Watson struct tcp_info *ti; 1166b8af5dfaSRobert Watson { 1167b8af5dfaSRobert Watson 1168b8af5dfaSRobert Watson INP_LOCK_ASSERT(tp->t_inpcb); 1169b8af5dfaSRobert Watson bzero(ti, sizeof(*ti)); 1170b8af5dfaSRobert Watson 1171b8af5dfaSRobert Watson ti->tcpi_state = tp->t_state; 1172b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1173b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 1174b8af5dfaSRobert Watson if (tp->sack_enable) 1175b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_SACK; 1176b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1177b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_WSCALE; 1178b8af5dfaSRobert Watson ti->tcpi_snd_wscale = tp->snd_scale; 1179b8af5dfaSRobert Watson ti->tcpi_rcv_wscale = tp->rcv_scale; 1180b8af5dfaSRobert Watson } 1181b8af5dfaSRobert Watson ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1182b8af5dfaSRobert Watson ti->tcpi_snd_cwnd = tp->snd_cwnd; 1183b8af5dfaSRobert Watson 1184b8af5dfaSRobert Watson /* 1185b8af5dfaSRobert Watson * FreeBSD-specific extension fields for tcp_info. 1186b8af5dfaSRobert Watson */ 1187c8443a1dSRobert Watson ti->tcpi_rcv_space = tp->rcv_wnd; 1188b8af5dfaSRobert Watson ti->tcpi_snd_wnd = tp->snd_wnd; 1189b8af5dfaSRobert Watson ti->tcpi_snd_bwnd = tp->snd_bwnd; 1190b8af5dfaSRobert Watson } 1191b8af5dfaSRobert Watson 1192b8af5dfaSRobert Watson /* 1193cfe8b629SGarrett Wollman * The new sockopt interface makes it possible for us to block in the 1194cfe8b629SGarrett Wollman * copyin/out step (if we take a page fault). Taking a page fault at 1195cfe8b629SGarrett Wollman * splnet() is probably a Bad Thing. (Since sockets and pcbs both now 1196cfe8b629SGarrett Wollman * use TSM, there probably isn't any need for this function to run at 1197cfe8b629SGarrett Wollman * splnet() any more. This needs more examination.) 1198b8af5dfaSRobert Watson * 1199b8af5dfaSRobert Watson * XXXRW: The locking here is wrong; we may take a page fault while holding 1200b8af5dfaSRobert Watson * the inpcb lock. 1201cfe8b629SGarrett Wollman */ 1202df8bae1dSRodney W. Grimes int 1203cfe8b629SGarrett Wollman tcp_ctloutput(so, sopt) 1204df8bae1dSRodney W. Grimes struct socket *so; 1205cfe8b629SGarrett Wollman struct sockopt *sopt; 1206df8bae1dSRodney W. Grimes { 12073f9d1ef9SRobert Watson int error, opt, optval; 1208df8bae1dSRodney W. Grimes struct inpcb *inp; 1209cfe8b629SGarrett Wollman struct tcpcb *tp; 1210b8af5dfaSRobert Watson struct tcp_info ti; 1211df8bae1dSRodney W. Grimes 1212cfe8b629SGarrett Wollman error = 0; 1213df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1214623dce13SRobert Watson KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 1215f76fcf6dSJeffrey Hsu INP_LOCK(inp); 1216cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_TCP) { 12174e397bc5SRobert Watson INP_UNLOCK(inp); 1218fb59c426SYoshinobu Inoue #ifdef INET6 1219fb59c426SYoshinobu Inoue if (INP_CHECK_SOCKAF(so, AF_INET6)) 1220fb59c426SYoshinobu Inoue error = ip6_ctloutput(so, sopt); 1221fb59c426SYoshinobu Inoue else 1222fb59c426SYoshinobu Inoue #endif /* INET6 */ 1223cfe8b629SGarrett Wollman error = ip_ctloutput(so, sopt); 1224df8bae1dSRodney W. Grimes return (error); 1225df8bae1dSRodney W. Grimes } 1226623dce13SRobert Watson if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 1227623dce13SRobert Watson error = ECONNRESET; 1228623dce13SRobert Watson goto out; 1229623dce13SRobert Watson } 1230df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 1231df8bae1dSRodney W. Grimes 1232cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 1233cfe8b629SGarrett Wollman case SOPT_SET: 1234cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 12351cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 123688f6b043SBruce M Simpson case TCP_MD5SIG: 12371cfd4b53SBruce M Simpson error = sooptcopyin(sopt, &optval, sizeof optval, 12381cfd4b53SBruce M Simpson sizeof optval); 12391cfd4b53SBruce M Simpson if (error) 12401cfd4b53SBruce M Simpson break; 12411cfd4b53SBruce M Simpson 12421cfd4b53SBruce M Simpson if (optval > 0) 12431cfd4b53SBruce M Simpson tp->t_flags |= TF_SIGNATURE; 12441cfd4b53SBruce M Simpson else 12451cfd4b53SBruce M Simpson tp->t_flags &= ~TF_SIGNATURE; 12461cfd4b53SBruce M Simpson break; 12471cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */ 1248df8bae1dSRodney W. Grimes case TCP_NODELAY: 1249cfe8b629SGarrett Wollman case TCP_NOOPT: 1250cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1251cfe8b629SGarrett Wollman sizeof optval); 1252cfe8b629SGarrett Wollman if (error) 1253cfe8b629SGarrett Wollman break; 1254cfe8b629SGarrett Wollman 1255cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1256cfe8b629SGarrett Wollman case TCP_NODELAY: 1257cfe8b629SGarrett Wollman opt = TF_NODELAY; 1258cfe8b629SGarrett Wollman break; 1259cfe8b629SGarrett Wollman case TCP_NOOPT: 1260cfe8b629SGarrett Wollman opt = TF_NOOPT; 1261cfe8b629SGarrett Wollman break; 1262cfe8b629SGarrett Wollman default: 1263cfe8b629SGarrett Wollman opt = 0; /* dead code to fool gcc */ 1264cfe8b629SGarrett Wollman break; 1265cfe8b629SGarrett Wollman } 1266cfe8b629SGarrett Wollman 1267cfe8b629SGarrett Wollman if (optval) 1268cfe8b629SGarrett Wollman tp->t_flags |= opt; 1269df8bae1dSRodney W. Grimes else 1270cfe8b629SGarrett Wollman tp->t_flags &= ~opt; 1271df8bae1dSRodney W. Grimes break; 1272df8bae1dSRodney W. Grimes 1273007581c0SJonathan Lemon case TCP_NOPUSH: 1274007581c0SJonathan Lemon error = sooptcopyin(sopt, &optval, sizeof optval, 1275007581c0SJonathan Lemon sizeof optval); 1276007581c0SJonathan Lemon if (error) 1277007581c0SJonathan Lemon break; 1278007581c0SJonathan Lemon 1279007581c0SJonathan Lemon if (optval) 1280007581c0SJonathan Lemon tp->t_flags |= TF_NOPUSH; 1281007581c0SJonathan Lemon else { 1282007581c0SJonathan Lemon tp->t_flags &= ~TF_NOPUSH; 1283007581c0SJonathan Lemon error = tcp_output(tp); 1284007581c0SJonathan Lemon } 1285007581c0SJonathan Lemon break; 1286007581c0SJonathan Lemon 1287df8bae1dSRodney W. Grimes case TCP_MAXSEG: 1288cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1289cfe8b629SGarrett Wollman sizeof optval); 1290cfe8b629SGarrett Wollman if (error) 1291df8bae1dSRodney W. Grimes break; 1292df8bae1dSRodney W. Grimes 129353369ac9SAndre Oppermann if (optval > 0 && optval <= tp->t_maxseg && 129453369ac9SAndre Oppermann optval + 40 >= tcp_minmss) 1295cfe8b629SGarrett Wollman tp->t_maxseg = optval; 1296a0292f23SGarrett Wollman else 1297a0292f23SGarrett Wollman error = EINVAL; 1298a0292f23SGarrett Wollman break; 1299a0292f23SGarrett Wollman 1300b8af5dfaSRobert Watson case TCP_INFO: 1301b8af5dfaSRobert Watson error = EINVAL; 1302b8af5dfaSRobert Watson break; 1303b8af5dfaSRobert Watson 1304df8bae1dSRodney W. Grimes default: 1305df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1306df8bae1dSRodney W. Grimes break; 1307df8bae1dSRodney W. Grimes } 1308df8bae1dSRodney W. Grimes break; 1309df8bae1dSRodney W. Grimes 1310cfe8b629SGarrett Wollman case SOPT_GET: 1311cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 13121cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 131388f6b043SBruce M Simpson case TCP_MD5SIG: 13141cfd4b53SBruce M Simpson optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0; 1315b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 13161cfd4b53SBruce M Simpson break; 1317265ed012SBruce M Simpson #endif 1318df8bae1dSRodney W. Grimes case TCP_NODELAY: 1319cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NODELAY; 1320b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1321df8bae1dSRodney W. Grimes break; 1322df8bae1dSRodney W. Grimes case TCP_MAXSEG: 1323cfe8b629SGarrett Wollman optval = tp->t_maxseg; 1324b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1325df8bae1dSRodney W. Grimes break; 1326a0292f23SGarrett Wollman case TCP_NOOPT: 1327cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOOPT; 1328b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1329a0292f23SGarrett Wollman break; 1330a0292f23SGarrett Wollman case TCP_NOPUSH: 1331cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOPUSH; 1332b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1333b8af5dfaSRobert Watson break; 1334b8af5dfaSRobert Watson case TCP_INFO: 1335b8af5dfaSRobert Watson tcp_fill_info(tp, &ti); 1336b8af5dfaSRobert Watson error = sooptcopyout(sopt, &ti, sizeof ti); 1337a0292f23SGarrett Wollman break; 1338df8bae1dSRodney W. Grimes default: 1339df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1340df8bae1dSRodney W. Grimes break; 1341df8bae1dSRodney W. Grimes } 1342df8bae1dSRodney W. Grimes break; 1343df8bae1dSRodney W. Grimes } 1344623dce13SRobert Watson out: 1345f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 1346df8bae1dSRodney W. Grimes return (error); 1347df8bae1dSRodney W. Grimes } 1348df8bae1dSRodney W. Grimes 134926e30fbbSDavid Greenman /* 135026e30fbbSDavid Greenman * tcp_sendspace and tcp_recvspace are the default send and receive window 135126e30fbbSDavid Greenman * sizes, respectively. These are obsolescent (this information should 135226e30fbbSDavid Greenman * be set by the route). 135326e30fbbSDavid Greenman */ 135481e561cdSDavid E. O'Brien u_long tcp_sendspace = 1024*32; 1355e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW, 13563d177f46SBill Fumerola &tcp_sendspace , 0, "Maximum outgoing TCP datagram size"); 135781e561cdSDavid E. O'Brien u_long tcp_recvspace = 1024*64; 1358e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 13593d177f46SBill Fumerola &tcp_recvspace , 0, "Maximum incoming TCP datagram size"); 1360df8bae1dSRodney W. Grimes 1361df8bae1dSRodney W. Grimes /* 1362df8bae1dSRodney W. Grimes * Attach TCP protocol to socket, allocating 1363df8bae1dSRodney W. Grimes * internet protocol control block, tcp control block, 1364df8bae1dSRodney W. Grimes * bufer space, and entering LISTEN state if to accept connections. 1365df8bae1dSRodney W. Grimes */ 13660312fbe9SPoul-Henning Kamp static int 136756dc72c3SPawel Jakub Dawidek tcp_attach(so) 1368df8bae1dSRodney W. Grimes struct socket *so; 1369df8bae1dSRodney W. Grimes { 1370df8bae1dSRodney W. Grimes register struct tcpcb *tp; 1371df8bae1dSRodney W. Grimes struct inpcb *inp; 1372df8bae1dSRodney W. Grimes int error; 1373fb59c426SYoshinobu Inoue #ifdef INET6 13744a6a94d8SArchie Cobbs int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0; 1375fb59c426SYoshinobu Inoue #endif 1376df8bae1dSRodney W. Grimes 13777609aad7SRobert Watson INP_INFO_WLOCK_ASSERT(&tcbinfo); 13787609aad7SRobert Watson 1379df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1380df8bae1dSRodney W. Grimes error = soreserve(so, tcp_sendspace, tcp_recvspace); 1381df8bae1dSRodney W. Grimes if (error) 1382df8bae1dSRodney W. Grimes return (error); 1383df8bae1dSRodney W. Grimes } 13846823b823SPawel Jakub Dawidek error = in_pcballoc(so, &tcbinfo, "tcpinp"); 1385df8bae1dSRodney W. Grimes if (error) 1386df8bae1dSRodney W. Grimes return (error); 1387df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1388fb59c426SYoshinobu Inoue #ifdef INET6 1389fb59c426SYoshinobu Inoue if (isipv6) { 1390fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 1391fb59c426SYoshinobu Inoue inp->in6p_hops = -1; /* use kernel default */ 1392fb59c426SYoshinobu Inoue } 1393fb59c426SYoshinobu Inoue else 1394fb59c426SYoshinobu Inoue #endif 1395cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 1396df8bae1dSRodney W. Grimes tp = tcp_newtcpcb(inp); 1397623dce13SRobert Watson if (tp == NULL) { 139830393994SRobert Watson INP_LOCK(inp); 1399fb59c426SYoshinobu Inoue #ifdef INET6 1400623dce13SRobert Watson if (isipv6) { 1401fb59c426SYoshinobu Inoue in6_pcbdetach(inp); 1402623dce13SRobert Watson in6_pcbfree(inp); 1403623dce13SRobert Watson } else { 1404fb59c426SYoshinobu Inoue #endif 1405df8bae1dSRodney W. Grimes in_pcbdetach(inp); 1406623dce13SRobert Watson in_pcbfree(inp); 1407623dce13SRobert Watson #ifdef INET6 1408623dce13SRobert Watson } 1409623dce13SRobert Watson #endif 1410df8bae1dSRodney W. Grimes return (ENOBUFS); 1411df8bae1dSRodney W. Grimes } 1412df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 1413df8bae1dSRodney W. Grimes return (0); 1414df8bae1dSRodney W. Grimes } 1415df8bae1dSRodney W. Grimes 1416df8bae1dSRodney W. Grimes /* 1417df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 1418df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 1419df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 1420df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 1421df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 1422df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 1423df8bae1dSRodney W. Grimes */ 1424623dce13SRobert Watson static void 1425df8bae1dSRodney W. Grimes tcp_disconnect(tp) 1426df8bae1dSRodney W. Grimes register struct tcpcb *tp; 1427df8bae1dSRodney W. Grimes { 1428e6e0b5ffSRobert Watson struct inpcb *inp = tp->t_inpcb; 1429e6e0b5ffSRobert Watson struct socket *so = inp->inp_socket; 1430e6e0b5ffSRobert Watson 1431e6e0b5ffSRobert Watson INP_INFO_WLOCK_ASSERT(&tcbinfo); 1432e6e0b5ffSRobert Watson INP_LOCK_ASSERT(inp); 1433df8bae1dSRodney W. Grimes 1434623dce13SRobert Watson /* 1435623dce13SRobert Watson * Neither tcp_close() nor tcp_drop() should return NULL, as the 1436623dce13SRobert Watson * socket is still open. 1437623dce13SRobert Watson */ 1438623dce13SRobert Watson if (tp->t_state < TCPS_ESTABLISHED) { 1439df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1440623dce13SRobert Watson KASSERT(tp != NULL, 1441623dce13SRobert Watson ("tcp_disconnect: tcp_close() returned NULL")); 1442623dce13SRobert Watson } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 1443243917feSSeigo Tanimura tp = tcp_drop(tp, 0); 1444623dce13SRobert Watson KASSERT(tp != NULL, 1445623dce13SRobert Watson ("tcp_disconnect: tcp_drop() returned NULL")); 1446623dce13SRobert Watson } else { 1447df8bae1dSRodney W. Grimes soisdisconnecting(so); 1448df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 1449623dce13SRobert Watson tcp_usrclosed(tp); 1450623dce13SRobert Watson tcp_output(tp); 1451df8bae1dSRodney W. Grimes } 1452df8bae1dSRodney W. Grimes } 1453df8bae1dSRodney W. Grimes 1454df8bae1dSRodney W. Grimes /* 1455df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 1456df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 1457df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1458df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 1459df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 1460df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 1461df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 1462df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 1463df8bae1dSRodney W. Grimes */ 1464623dce13SRobert Watson static void 1465df8bae1dSRodney W. Grimes tcp_usrclosed(tp) 1466df8bae1dSRodney W. Grimes register struct tcpcb *tp; 1467df8bae1dSRodney W. Grimes { 1468df8bae1dSRodney W. Grimes 1469e6e0b5ffSRobert Watson INP_INFO_WLOCK_ASSERT(&tcbinfo); 1470e6e0b5ffSRobert Watson INP_LOCK_ASSERT(tp->t_inpcb); 1471e6e0b5ffSRobert Watson 1472df8bae1dSRodney W. Grimes switch (tp->t_state) { 1473df8bae1dSRodney W. Grimes 1474df8bae1dSRodney W. Grimes case TCPS_CLOSED: 1475df8bae1dSRodney W. Grimes case TCPS_LISTEN: 1476df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 1477df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1478623dce13SRobert Watson /* 1479623dce13SRobert Watson * tcp_close() should never return NULL here as the socket is 1480623dce13SRobert Watson * still open. 1481623dce13SRobert Watson */ 1482623dce13SRobert Watson KASSERT(tp != NULL, 1483623dce13SRobert Watson ("tcp_usrclosed: tcp_close() returned NULL")); 1484df8bae1dSRodney W. Grimes break; 1485df8bae1dSRodney W. Grimes 1486a0292f23SGarrett Wollman case TCPS_SYN_SENT: 1487df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1488a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 1489a0292f23SGarrett Wollman break; 1490a0292f23SGarrett Wollman 1491df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1492df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_1; 1493df8bae1dSRodney W. Grimes break; 1494df8bae1dSRodney W. Grimes 1495df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1496df8bae1dSRodney W. Grimes tp->t_state = TCPS_LAST_ACK; 1497df8bae1dSRodney W. Grimes break; 1498df8bae1dSRodney W. Grimes } 1499b6239c4aSAndras Olah if (tp && tp->t_state >= TCPS_FIN_WAIT_2) { 1500df8bae1dSRodney W. Grimes soisdisconnected(tp->t_inpcb->inp_socket); 1501b6239c4aSAndras Olah /* To prevent the connection hanging in FIN_WAIT_2 forever. */ 1502b6239c4aSAndras Olah if (tp->t_state == TCPS_FIN_WAIT_2) 15039b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, tcp_maxidle, 15049b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 1505b6239c4aSAndras Olah } 1506df8bae1dSRodney W. Grimes } 1507