1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 331fdbc7aeSGarrett Wollman * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94 34c3aac50fSPeter Wemm * $FreeBSD$ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 376a800098SYoshinobu Inoue #include "opt_ipsec.h" 38fb59c426SYoshinobu Inoue #include "opt_inet6.h" 390cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 400cc12cc5SJoerg Wunsch 41df8bae1dSRodney W. Grimes #include <sys/param.h> 42df8bae1dSRodney W. Grimes #include <sys/systm.h> 43c7a82f90SGarrett Wollman #include <sys/kernel.h> 4498163b98SPoul-Henning Kamp #include <sys/sysctl.h> 45df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 46fb59c426SYoshinobu Inoue #ifdef INET6 47fb59c426SYoshinobu Inoue #include <sys/domain.h> 48fb59c426SYoshinobu Inoue #endif /* INET6 */ 49df8bae1dSRodney W. Grimes #include <sys/socket.h> 50df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 51df8bae1dSRodney W. Grimes #include <sys/protosw.h> 52df8bae1dSRodney W. Grimes 53df8bae1dSRodney W. Grimes #include <net/if.h> 54df8bae1dSRodney W. Grimes #include <net/route.h> 55df8bae1dSRodney W. Grimes 56df8bae1dSRodney W. Grimes #include <netinet/in.h> 57df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 58fb59c426SYoshinobu Inoue #ifdef INET6 59fb59c426SYoshinobu Inoue #include <netinet/ip6.h> 60fb59c426SYoshinobu Inoue #endif 61df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 62fb59c426SYoshinobu Inoue #ifdef INET6 63fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h> 64fb59c426SYoshinobu Inoue #endif 65b5e8ce9fSBruce Evans #include <netinet/in_var.h> 66df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 67fb59c426SYoshinobu Inoue #ifdef INET6 68fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 69fb59c426SYoshinobu Inoue #endif 70df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 71df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 72df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 73df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 74df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 75df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 76610ee2f9SDavid Greenman #ifdef TCPDEBUG 77df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 78610ee2f9SDavid Greenman #endif 79df8bae1dSRodney W. Grimes 806a800098SYoshinobu Inoue #ifdef IPSEC 816a800098SYoshinobu Inoue #include <netinet6/ipsec.h> 826a800098SYoshinobu Inoue #endif /*IPSEC*/ 836a800098SYoshinobu Inoue 84df8bae1dSRodney W. Grimes /* 85df8bae1dSRodney W. Grimes * TCP protocol interface to socket abstraction. 86df8bae1dSRodney W. Grimes */ 87117bcae7SGarrett Wollman extern char *tcpstates[]; /* XXX ??? */ 88df8bae1dSRodney W. Grimes 89a29f300eSGarrett Wollman static int tcp_attach __P((struct socket *, struct proc *)); 9057bf258eSGarrett Wollman static int tcp_connect __P((struct tcpcb *, struct sockaddr *, 91a29f300eSGarrett Wollman struct proc *)); 92fb59c426SYoshinobu Inoue #ifdef INET6 93fb59c426SYoshinobu Inoue static int tcp6_connect __P((struct tcpcb *, struct sockaddr *, 94fb59c426SYoshinobu Inoue struct proc *)); 95fb59c426SYoshinobu Inoue #endif /* INET6 */ 960312fbe9SPoul-Henning Kamp static struct tcpcb * 970312fbe9SPoul-Henning Kamp tcp_disconnect __P((struct tcpcb *)); 980312fbe9SPoul-Henning Kamp static struct tcpcb * 990312fbe9SPoul-Henning Kamp tcp_usrclosed __P((struct tcpcb *)); 1002c37256eSGarrett Wollman 1012c37256eSGarrett Wollman #ifdef TCPDEBUG 1022c37256eSGarrett Wollman #define TCPDEBUG0 int ostate 1032c37256eSGarrett Wollman #define TCPDEBUG1() ostate = tp ? tp->t_state : 0 104af7a2999SDavid Greenman #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 105fb59c426SYoshinobu Inoue tcp_trace(TA_USER, ostate, tp, 0, 0, req) 1062c37256eSGarrett Wollman #else 1072c37256eSGarrett Wollman #define TCPDEBUG0 1082c37256eSGarrett Wollman #define TCPDEBUG1() 1092c37256eSGarrett Wollman #define TCPDEBUG2(req) 1102c37256eSGarrett Wollman #endif 1112c37256eSGarrett Wollman 1122c37256eSGarrett Wollman /* 1132c37256eSGarrett Wollman * TCP attaches to socket via pru_attach(), reserving space, 1142c37256eSGarrett Wollman * and an internet control block. 1152c37256eSGarrett Wollman */ 1162c37256eSGarrett Wollman static int 117a29f300eSGarrett Wollman tcp_usr_attach(struct socket *so, int proto, struct proc *p) 1182c37256eSGarrett Wollman { 1192c37256eSGarrett Wollman int s = splnet(); 1202c37256eSGarrett Wollman int error; 1212c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 1222c37256eSGarrett Wollman struct tcpcb *tp = 0; 1232c37256eSGarrett Wollman TCPDEBUG0; 1242c37256eSGarrett Wollman 1252c37256eSGarrett Wollman TCPDEBUG1(); 1262c37256eSGarrett Wollman if (inp) { 1272c37256eSGarrett Wollman error = EISCONN; 1282c37256eSGarrett Wollman goto out; 1292c37256eSGarrett Wollman } 1302c37256eSGarrett Wollman 131a29f300eSGarrett Wollman error = tcp_attach(so, p); 1322c37256eSGarrett Wollman if (error) 1332c37256eSGarrett Wollman goto out; 1342c37256eSGarrett Wollman 1352c37256eSGarrett Wollman if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1363879597fSAndrey A. Chernov so->so_linger = TCP_LINGERTIME; 1372c37256eSGarrett Wollman tp = sototcpcb(so); 1382c37256eSGarrett Wollman out: 1392c37256eSGarrett Wollman TCPDEBUG2(PRU_ATTACH); 1402c37256eSGarrett Wollman splx(s); 1412c37256eSGarrett Wollman return error; 1422c37256eSGarrett Wollman } 1432c37256eSGarrett Wollman 1442c37256eSGarrett Wollman /* 1452c37256eSGarrett Wollman * pru_detach() detaches the TCP protocol from the socket. 1462c37256eSGarrett Wollman * If the protocol state is non-embryonic, then can't 1472c37256eSGarrett Wollman * do this directly: have to initiate a pru_disconnect(), 1482c37256eSGarrett Wollman * which may finish later; embryonic TCB's can just 1492c37256eSGarrett Wollman * be discarded here. 1502c37256eSGarrett Wollman */ 1512c37256eSGarrett Wollman static int 1522c37256eSGarrett Wollman tcp_usr_detach(struct socket *so) 1532c37256eSGarrett Wollman { 1542c37256eSGarrett Wollman int s = splnet(); 1552c37256eSGarrett Wollman int error = 0; 1562c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 1572c37256eSGarrett Wollman struct tcpcb *tp; 1582c37256eSGarrett Wollman TCPDEBUG0; 1592c37256eSGarrett Wollman 1602c37256eSGarrett Wollman if (inp == 0) { 1612c37256eSGarrett Wollman splx(s); 1622c37256eSGarrett Wollman return EINVAL; /* XXX */ 1632c37256eSGarrett Wollman } 1642c37256eSGarrett Wollman tp = intotcpcb(inp); 1652c37256eSGarrett Wollman TCPDEBUG1(); 1662c37256eSGarrett Wollman tp = tcp_disconnect(tp); 1672c37256eSGarrett Wollman 1682c37256eSGarrett Wollman TCPDEBUG2(PRU_DETACH); 1692c37256eSGarrett Wollman splx(s); 1702c37256eSGarrett Wollman return error; 1712c37256eSGarrett Wollman } 1722c37256eSGarrett Wollman 1732c37256eSGarrett Wollman #define COMMON_START() TCPDEBUG0; \ 1742c37256eSGarrett Wollman do { \ 1752c37256eSGarrett Wollman if (inp == 0) { \ 1762c37256eSGarrett Wollman splx(s); \ 1772c37256eSGarrett Wollman return EINVAL; \ 1782c37256eSGarrett Wollman } \ 1792c37256eSGarrett Wollman tp = intotcpcb(inp); \ 1802c37256eSGarrett Wollman TCPDEBUG1(); \ 1812c37256eSGarrett Wollman } while(0) 1822c37256eSGarrett Wollman 1832c37256eSGarrett Wollman #define COMMON_END(req) out: TCPDEBUG2(req); splx(s); return error; goto out 1842c37256eSGarrett Wollman 1852c37256eSGarrett Wollman 1862c37256eSGarrett Wollman /* 1872c37256eSGarrett Wollman * Give the socket an address. 1882c37256eSGarrett Wollman */ 1892c37256eSGarrett Wollman static int 19057bf258eSGarrett Wollman tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p) 1912c37256eSGarrett Wollman { 1922c37256eSGarrett Wollman int s = splnet(); 1932c37256eSGarrett Wollman int error = 0; 1942c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 1952c37256eSGarrett Wollman struct tcpcb *tp; 1962c37256eSGarrett Wollman struct sockaddr_in *sinp; 1972c37256eSGarrett Wollman 1982c37256eSGarrett Wollman COMMON_START(); 1992c37256eSGarrett Wollman 2002c37256eSGarrett Wollman /* 2012c37256eSGarrett Wollman * Must check for multicast addresses and disallow binding 2022c37256eSGarrett Wollman * to them. 2032c37256eSGarrett Wollman */ 20457bf258eSGarrett Wollman sinp = (struct sockaddr_in *)nam; 2052c37256eSGarrett Wollman if (sinp->sin_family == AF_INET && 2062c37256eSGarrett Wollman IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 2072c37256eSGarrett Wollman error = EAFNOSUPPORT; 2082c37256eSGarrett Wollman goto out; 2092c37256eSGarrett Wollman } 210a29f300eSGarrett Wollman error = in_pcbbind(inp, nam, p); 2112c37256eSGarrett Wollman if (error) 2122c37256eSGarrett Wollman goto out; 2132c37256eSGarrett Wollman COMMON_END(PRU_BIND); 2142c37256eSGarrett Wollman 2152c37256eSGarrett Wollman } 2162c37256eSGarrett Wollman 217fb59c426SYoshinobu Inoue #ifdef INET6 218fb59c426SYoshinobu Inoue static int 219fb59c426SYoshinobu Inoue tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p) 220fb59c426SYoshinobu Inoue { 221fb59c426SYoshinobu Inoue int s = splnet(); 222fb59c426SYoshinobu Inoue int error = 0; 223fb59c426SYoshinobu Inoue struct inpcb *inp = sotoinpcb(so); 224fb59c426SYoshinobu Inoue struct tcpcb *tp; 225fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 226fb59c426SYoshinobu Inoue 227fb59c426SYoshinobu Inoue COMMON_START(); 228fb59c426SYoshinobu Inoue 229fb59c426SYoshinobu Inoue /* 230fb59c426SYoshinobu Inoue * Must check for multicast addresses and disallow binding 231fb59c426SYoshinobu Inoue * to them. 232fb59c426SYoshinobu Inoue */ 233fb59c426SYoshinobu Inoue sin6p = (struct sockaddr_in6 *)nam; 234fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 && 235fb59c426SYoshinobu Inoue IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) { 236fb59c426SYoshinobu Inoue error = EAFNOSUPPORT; 237fb59c426SYoshinobu Inoue goto out; 238fb59c426SYoshinobu Inoue } 239fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 240fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 241fb59c426SYoshinobu Inoue if (ip6_mapped_addr_on && (inp->inp_flags & IN6P_BINDV6ONLY) == NULL) { 242fb59c426SYoshinobu Inoue 243fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) 244fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 245fb59c426SYoshinobu Inoue else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 246fb59c426SYoshinobu Inoue struct sockaddr_in sin; 247fb59c426SYoshinobu Inoue 248fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 249fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 250fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 251fb59c426SYoshinobu Inoue error = in_pcbbind(inp, (struct sockaddr *)&sin, p); 252fb59c426SYoshinobu Inoue goto out; 253fb59c426SYoshinobu Inoue } 254fb59c426SYoshinobu Inoue } 255fb59c426SYoshinobu Inoue error = in6_pcbbind(inp, nam, p); 256fb59c426SYoshinobu Inoue if (error) 257fb59c426SYoshinobu Inoue goto out; 258fb59c426SYoshinobu Inoue COMMON_END(PRU_BIND); 259fb59c426SYoshinobu Inoue } 260fb59c426SYoshinobu Inoue #endif /* INET6 */ 261fb59c426SYoshinobu Inoue 2622c37256eSGarrett Wollman /* 2632c37256eSGarrett Wollman * Prepare to accept connections. 2642c37256eSGarrett Wollman */ 2652c37256eSGarrett Wollman static int 266a29f300eSGarrett Wollman tcp_usr_listen(struct socket *so, struct proc *p) 2672c37256eSGarrett Wollman { 2682c37256eSGarrett Wollman int s = splnet(); 2692c37256eSGarrett Wollman int error = 0; 2702c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 2712c37256eSGarrett Wollman struct tcpcb *tp; 2722c37256eSGarrett Wollman 2732c37256eSGarrett Wollman COMMON_START(); 2742c37256eSGarrett Wollman if (inp->inp_lport == 0) 27557bf258eSGarrett Wollman error = in_pcbbind(inp, (struct sockaddr *)0, p); 2762c37256eSGarrett Wollman if (error == 0) 2772c37256eSGarrett Wollman tp->t_state = TCPS_LISTEN; 2782c37256eSGarrett Wollman COMMON_END(PRU_LISTEN); 2792c37256eSGarrett Wollman } 2802c37256eSGarrett Wollman 281fb59c426SYoshinobu Inoue #ifdef INET6 282fb59c426SYoshinobu Inoue static int 283fb59c426SYoshinobu Inoue tcp6_usr_listen(struct socket *so, struct proc *p) 284fb59c426SYoshinobu Inoue { 285fb59c426SYoshinobu Inoue int s = splnet(); 286fb59c426SYoshinobu Inoue int error = 0; 287fb59c426SYoshinobu Inoue struct inpcb *inp = sotoinpcb(so); 288fb59c426SYoshinobu Inoue struct tcpcb *tp; 289fb59c426SYoshinobu Inoue 290fb59c426SYoshinobu Inoue COMMON_START(); 291fb59c426SYoshinobu Inoue if (inp->inp_lport == 0) { 292fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 293fb59c426SYoshinobu Inoue if (ip6_mapped_addr_on && 294fb59c426SYoshinobu Inoue (inp->inp_flags & IN6P_BINDV6ONLY) == NULL) 295fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 296fb59c426SYoshinobu Inoue error = in6_pcbbind(inp, (struct sockaddr *)0, p); 297fb59c426SYoshinobu Inoue } 298fb59c426SYoshinobu Inoue if (error == 0) 299fb59c426SYoshinobu Inoue tp->t_state = TCPS_LISTEN; 300fb59c426SYoshinobu Inoue COMMON_END(PRU_LISTEN); 301fb59c426SYoshinobu Inoue } 302fb59c426SYoshinobu Inoue #endif /* INET6 */ 303fb59c426SYoshinobu Inoue 3042c37256eSGarrett Wollman /* 3052c37256eSGarrett Wollman * Initiate connection to peer. 3062c37256eSGarrett Wollman * Create a template for use in transmissions on this connection. 3072c37256eSGarrett Wollman * Enter SYN_SENT state, and mark socket as connecting. 3082c37256eSGarrett Wollman * Start keep-alive timer, and seed output sequence space. 3092c37256eSGarrett Wollman * Send initial segment on connection. 3102c37256eSGarrett Wollman */ 3112c37256eSGarrett Wollman static int 31257bf258eSGarrett Wollman tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p) 3132c37256eSGarrett Wollman { 3142c37256eSGarrett Wollman int s = splnet(); 3152c37256eSGarrett Wollman int error = 0; 3162c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 3172c37256eSGarrett Wollman struct tcpcb *tp; 3182c37256eSGarrett Wollman struct sockaddr_in *sinp; 3192c37256eSGarrett Wollman 3202c37256eSGarrett Wollman COMMON_START(); 3212c37256eSGarrett Wollman 3222c37256eSGarrett Wollman /* 3232c37256eSGarrett Wollman * Must disallow TCP ``connections'' to multicast addresses. 3242c37256eSGarrett Wollman */ 32557bf258eSGarrett Wollman sinp = (struct sockaddr_in *)nam; 3262c37256eSGarrett Wollman if (sinp->sin_family == AF_INET 3272c37256eSGarrett Wollman && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 3282c37256eSGarrett Wollman error = EAFNOSUPPORT; 3292c37256eSGarrett Wollman goto out; 3302c37256eSGarrett Wollman } 3312c37256eSGarrett Wollman 33275c13541SPoul-Henning Kamp prison_remote_ip(p, 0, &sinp->sin_addr.s_addr); 33375c13541SPoul-Henning Kamp 334a29f300eSGarrett Wollman if ((error = tcp_connect(tp, nam, p)) != 0) 3352c37256eSGarrett Wollman goto out; 3362c37256eSGarrett Wollman error = tcp_output(tp); 3372c37256eSGarrett Wollman COMMON_END(PRU_CONNECT); 3382c37256eSGarrett Wollman } 3392c37256eSGarrett Wollman 340fb59c426SYoshinobu Inoue #ifdef INET6 341fb59c426SYoshinobu Inoue static int 342fb59c426SYoshinobu Inoue tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p) 343fb59c426SYoshinobu Inoue { 344fb59c426SYoshinobu Inoue int s = splnet(); 345fb59c426SYoshinobu Inoue int error = 0; 346fb59c426SYoshinobu Inoue struct inpcb *inp = sotoinpcb(so); 347fb59c426SYoshinobu Inoue struct tcpcb *tp; 348fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 349fb59c426SYoshinobu Inoue 350fb59c426SYoshinobu Inoue COMMON_START(); 351fb59c426SYoshinobu Inoue 352fb59c426SYoshinobu Inoue /* 353fb59c426SYoshinobu Inoue * Must disallow TCP ``connections'' to multicast addresses. 354fb59c426SYoshinobu Inoue */ 355fb59c426SYoshinobu Inoue sin6p = (struct sockaddr_in6 *)nam; 356fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 357fb59c426SYoshinobu Inoue && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) { 358fb59c426SYoshinobu Inoue error = EAFNOSUPPORT; 359fb59c426SYoshinobu Inoue goto out; 360fb59c426SYoshinobu Inoue } 361fb59c426SYoshinobu Inoue 362fb59c426SYoshinobu Inoue if (ip6_mapped_addr_on && 363fb59c426SYoshinobu Inoue IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 364fb59c426SYoshinobu Inoue struct sockaddr_in sin; 365fb59c426SYoshinobu Inoue 366fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 367fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 368fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 369fb59c426SYoshinobu Inoue if ((error = tcp_connect(tp, (struct sockaddr *)&sin, p)) != 0) 370fb59c426SYoshinobu Inoue goto out; 371fb59c426SYoshinobu Inoue error = tcp_output(tp); 372fb59c426SYoshinobu Inoue goto out; 373fb59c426SYoshinobu Inoue } 374fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 375fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 376fb59c426SYoshinobu Inoue if ((error = tcp6_connect(tp, nam, p)) != 0) 377fb59c426SYoshinobu Inoue goto out; 378fb59c426SYoshinobu Inoue error = tcp_output(tp); 379fb59c426SYoshinobu Inoue COMMON_END(PRU_CONNECT); 380fb59c426SYoshinobu Inoue } 381fb59c426SYoshinobu Inoue #endif /* INET6 */ 382fb59c426SYoshinobu Inoue 3832c37256eSGarrett Wollman /* 3842c37256eSGarrett Wollman * Initiate disconnect from peer. 3852c37256eSGarrett Wollman * If connection never passed embryonic stage, just drop; 3862c37256eSGarrett Wollman * else if don't need to let data drain, then can just drop anyways, 3872c37256eSGarrett Wollman * else have to begin TCP shutdown process: mark socket disconnecting, 3882c37256eSGarrett Wollman * drain unread data, state switch to reflect user close, and 3892c37256eSGarrett Wollman * send segment (e.g. FIN) to peer. Socket will be really disconnected 3902c37256eSGarrett Wollman * when peer sends FIN and acks ours. 3912c37256eSGarrett Wollman * 3922c37256eSGarrett Wollman * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 3932c37256eSGarrett Wollman */ 3942c37256eSGarrett Wollman static int 3952c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so) 3962c37256eSGarrett Wollman { 3972c37256eSGarrett Wollman int s = splnet(); 3982c37256eSGarrett Wollman int error = 0; 3992c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 4002c37256eSGarrett Wollman struct tcpcb *tp; 4012c37256eSGarrett Wollman 4022c37256eSGarrett Wollman COMMON_START(); 4032c37256eSGarrett Wollman tp = tcp_disconnect(tp); 4042c37256eSGarrett Wollman COMMON_END(PRU_DISCONNECT); 4052c37256eSGarrett Wollman } 4062c37256eSGarrett Wollman 4072c37256eSGarrett Wollman /* 4082c37256eSGarrett Wollman * Accept a connection. Essentially all the work is 4092c37256eSGarrett Wollman * done at higher levels; just return the address 4102c37256eSGarrett Wollman * of the peer, storing through addr. 4112c37256eSGarrett Wollman */ 4122c37256eSGarrett Wollman static int 41357bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam) 4142c37256eSGarrett Wollman { 4152c37256eSGarrett Wollman int s = splnet(); 4162c37256eSGarrett Wollman int error = 0; 4172c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 4182c37256eSGarrett Wollman struct tcpcb *tp; 4192c37256eSGarrett Wollman 4202c37256eSGarrett Wollman COMMON_START(); 421117bcae7SGarrett Wollman in_setpeeraddr(so, nam); 4222c37256eSGarrett Wollman COMMON_END(PRU_ACCEPT); 4232c37256eSGarrett Wollman } 4242c37256eSGarrett Wollman 425fb59c426SYoshinobu Inoue #ifdef INET6 426fb59c426SYoshinobu Inoue static int 427fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 428fb59c426SYoshinobu Inoue { 429fb59c426SYoshinobu Inoue int s = splnet(); 430fb59c426SYoshinobu Inoue int error = 0; 431fb59c426SYoshinobu Inoue struct inpcb *inp = sotoinpcb(so); 432fb59c426SYoshinobu Inoue struct tcpcb *tp; 433fb59c426SYoshinobu Inoue 434fb59c426SYoshinobu Inoue COMMON_START(); 435fb59c426SYoshinobu Inoue in6_mapped_peeraddr(so, nam); 436fb59c426SYoshinobu Inoue COMMON_END(PRU_ACCEPT); 437fb59c426SYoshinobu Inoue } 438fb59c426SYoshinobu Inoue #endif /* INET6 */ 4392c37256eSGarrett Wollman /* 4402c37256eSGarrett Wollman * Mark the connection as being incapable of further output. 4412c37256eSGarrett Wollman */ 4422c37256eSGarrett Wollman static int 4432c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so) 4442c37256eSGarrett Wollman { 4452c37256eSGarrett Wollman int s = splnet(); 4462c37256eSGarrett Wollman int error = 0; 4472c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 4482c37256eSGarrett Wollman struct tcpcb *tp; 4492c37256eSGarrett Wollman 4502c37256eSGarrett Wollman COMMON_START(); 4512c37256eSGarrett Wollman socantsendmore(so); 4522c37256eSGarrett Wollman tp = tcp_usrclosed(tp); 4532c37256eSGarrett Wollman if (tp) 4542c37256eSGarrett Wollman error = tcp_output(tp); 4552c37256eSGarrett Wollman COMMON_END(PRU_SHUTDOWN); 4562c37256eSGarrett Wollman } 4572c37256eSGarrett Wollman 4582c37256eSGarrett Wollman /* 4592c37256eSGarrett Wollman * After a receive, possibly send window update to peer. 4602c37256eSGarrett Wollman */ 4612c37256eSGarrett Wollman static int 4622c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags) 4632c37256eSGarrett Wollman { 4642c37256eSGarrett Wollman int s = splnet(); 4652c37256eSGarrett Wollman int error = 0; 4662c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 4672c37256eSGarrett Wollman struct tcpcb *tp; 4682c37256eSGarrett Wollman 4692c37256eSGarrett Wollman COMMON_START(); 4702c37256eSGarrett Wollman tcp_output(tp); 4712c37256eSGarrett Wollman COMMON_END(PRU_RCVD); 4722c37256eSGarrett Wollman } 4732c37256eSGarrett Wollman 4742c37256eSGarrett Wollman /* 4752c37256eSGarrett Wollman * Do a send by putting data in output queue and updating urgent 4769c9906e9SPeter Wemm * marker if URG set. Possibly send more data. Unlike the other 4779c9906e9SPeter Wemm * pru_*() routines, the mbuf chains are our responsibility. We 4789c9906e9SPeter Wemm * must either enqueue them or free them. The other pru_* routines 4799c9906e9SPeter Wemm * generally are caller-frees. 4802c37256eSGarrett Wollman */ 4812c37256eSGarrett Wollman static int 48257bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 48357bf258eSGarrett Wollman struct sockaddr *nam, struct mbuf *control, struct proc *p) 4842c37256eSGarrett Wollman { 4852c37256eSGarrett Wollman int s = splnet(); 4862c37256eSGarrett Wollman int error = 0; 4872c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 4882c37256eSGarrett Wollman struct tcpcb *tp; 489fb59c426SYoshinobu Inoue #ifdef INET6 490fb59c426SYoshinobu Inoue int isipv6; 491fb59c426SYoshinobu Inoue #endif 4929c9906e9SPeter Wemm TCPDEBUG0; 4932c37256eSGarrett Wollman 4949c9906e9SPeter Wemm if (inp == NULL) { 4959c9906e9SPeter Wemm /* 4969c9906e9SPeter Wemm * OOPS! we lost a race, the TCP session got reset after 4979c9906e9SPeter Wemm * we checked SS_CANTSENDMORE, eg: while doing uiomove or a 4989c9906e9SPeter Wemm * network interrupt in the non-splnet() section of sosend(). 4999c9906e9SPeter Wemm */ 5009c9906e9SPeter Wemm if (m) 5019c9906e9SPeter Wemm m_freem(m); 5029c9906e9SPeter Wemm if (control) 5039c9906e9SPeter Wemm m_freem(control); 5049c9906e9SPeter Wemm error = ECONNRESET; /* XXX EPIPE? */ 50545d3a132SPeter Wemm tp = NULL; 50645d3a132SPeter Wemm TCPDEBUG1(); 5079c9906e9SPeter Wemm goto out; 5089c9906e9SPeter Wemm } 509fb59c426SYoshinobu Inoue #ifdef INET6 510fb59c426SYoshinobu Inoue isipv6 = nam && nam->sa_family == AF_INET6; 511fb59c426SYoshinobu Inoue #endif /* INET6 */ 5129c9906e9SPeter Wemm tp = intotcpcb(inp); 5139c9906e9SPeter Wemm TCPDEBUG1(); 5149c9906e9SPeter Wemm if (control) { 5159c9906e9SPeter Wemm /* TCP doesn't do control messages (rights, creds, etc) */ 5169c9906e9SPeter Wemm if (control->m_len) { 5179c9906e9SPeter Wemm m_freem(control); 5182c37256eSGarrett Wollman if (m) 5192c37256eSGarrett Wollman m_freem(m); 520744f87eaSDavid Greenman error = EINVAL; 521744f87eaSDavid Greenman goto out; 5222c37256eSGarrett Wollman } 5239c9906e9SPeter Wemm m_freem(control); /* empty control, just free it */ 5249c9906e9SPeter Wemm } 5252c37256eSGarrett Wollman if(!(flags & PRUS_OOB)) { 5262c37256eSGarrett Wollman sbappend(&so->so_snd, m); 5272c37256eSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 5282c37256eSGarrett Wollman /* 5292c37256eSGarrett Wollman * Do implied connect if not yet connected, 5302c37256eSGarrett Wollman * initialize window to default value, and 5312c37256eSGarrett Wollman * initialize maxseg/maxopd using peer's cached 5322c37256eSGarrett Wollman * MSS. 5332c37256eSGarrett Wollman */ 534fb59c426SYoshinobu Inoue #ifdef INET6 535fb59c426SYoshinobu Inoue if (isipv6) 536fb59c426SYoshinobu Inoue error = tcp6_connect(tp, nam, p); 537fb59c426SYoshinobu Inoue else 538fb59c426SYoshinobu Inoue #endif /* INET6 */ 539a29f300eSGarrett Wollman error = tcp_connect(tp, nam, p); 5402c37256eSGarrett Wollman if (error) 5412c37256eSGarrett Wollman goto out; 5422c37256eSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 5432c37256eSGarrett Wollman tcp_mss(tp, -1); 5442c37256eSGarrett Wollman } 5452c37256eSGarrett Wollman 5462c37256eSGarrett Wollman if (flags & PRUS_EOF) { 5472c37256eSGarrett Wollman /* 5482c37256eSGarrett Wollman * Close the send side of the connection after 5492c37256eSGarrett Wollman * the data is sent. 5502c37256eSGarrett Wollman */ 5512c37256eSGarrett Wollman socantsendmore(so); 5522c37256eSGarrett Wollman tp = tcp_usrclosed(tp); 5532c37256eSGarrett Wollman } 554b0acefa8SBill Fenner if (tp != NULL) { 555b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 556b0acefa8SBill Fenner tp->t_flags |= TF_MORETOCOME; 5572c37256eSGarrett Wollman error = tcp_output(tp); 558b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 559b0acefa8SBill Fenner tp->t_flags &= ~TF_MORETOCOME; 560b0acefa8SBill Fenner } 5612c37256eSGarrett Wollman } else { 5622c37256eSGarrett Wollman if (sbspace(&so->so_snd) < -512) { 5632c37256eSGarrett Wollman m_freem(m); 5642c37256eSGarrett Wollman error = ENOBUFS; 5652c37256eSGarrett Wollman goto out; 5662c37256eSGarrett Wollman } 5672c37256eSGarrett Wollman /* 5682c37256eSGarrett Wollman * According to RFC961 (Assigned Protocols), 5692c37256eSGarrett Wollman * the urgent pointer points to the last octet 5702c37256eSGarrett Wollman * of urgent data. We continue, however, 5712c37256eSGarrett Wollman * to consider it to indicate the first octet 5722c37256eSGarrett Wollman * of data past the urgent section. 5732c37256eSGarrett Wollman * Otherwise, snd_up should be one lower. 5742c37256eSGarrett Wollman */ 5752c37256eSGarrett Wollman sbappend(&so->so_snd, m); 576ef53690bSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 577ef53690bSGarrett Wollman /* 578ef53690bSGarrett Wollman * Do implied connect if not yet connected, 579ef53690bSGarrett Wollman * initialize window to default value, and 580ef53690bSGarrett Wollman * initialize maxseg/maxopd using peer's cached 581ef53690bSGarrett Wollman * MSS. 582ef53690bSGarrett Wollman */ 583fb59c426SYoshinobu Inoue #ifdef INET6 584fb59c426SYoshinobu Inoue if (isipv6) 585fb59c426SYoshinobu Inoue error = tcp6_connect(tp, nam, p); 586fb59c426SYoshinobu Inoue else 587fb59c426SYoshinobu Inoue #endif /* INET6 */ 588a29f300eSGarrett Wollman error = tcp_connect(tp, nam, p); 589ef53690bSGarrett Wollman if (error) 590ef53690bSGarrett Wollman goto out; 591ef53690bSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 592ef53690bSGarrett Wollman tcp_mss(tp, -1); 593ef53690bSGarrett Wollman } 5942c37256eSGarrett Wollman tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 5952c37256eSGarrett Wollman tp->t_force = 1; 5962c37256eSGarrett Wollman error = tcp_output(tp); 5972c37256eSGarrett Wollman tp->t_force = 0; 5982c37256eSGarrett Wollman } 5992c37256eSGarrett Wollman COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB : 6002c37256eSGarrett Wollman ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 6012c37256eSGarrett Wollman } 6022c37256eSGarrett Wollman 6032c37256eSGarrett Wollman /* 6042c37256eSGarrett Wollman * Abort the TCP. 6052c37256eSGarrett Wollman */ 6062c37256eSGarrett Wollman static int 6072c37256eSGarrett Wollman tcp_usr_abort(struct socket *so) 6082c37256eSGarrett Wollman { 6092c37256eSGarrett Wollman int s = splnet(); 6102c37256eSGarrett Wollman int error = 0; 6112c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 6122c37256eSGarrett Wollman struct tcpcb *tp; 6132c37256eSGarrett Wollman 6142c37256eSGarrett Wollman COMMON_START(); 6152c37256eSGarrett Wollman tp = tcp_drop(tp, ECONNABORTED); 6162c37256eSGarrett Wollman COMMON_END(PRU_ABORT); 6172c37256eSGarrett Wollman } 6182c37256eSGarrett Wollman 6192c37256eSGarrett Wollman /* 6202c37256eSGarrett Wollman * Receive out-of-band data. 6212c37256eSGarrett Wollman */ 6222c37256eSGarrett Wollman static int 6232c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 6242c37256eSGarrett Wollman { 6252c37256eSGarrett Wollman int s = splnet(); 6262c37256eSGarrett Wollman int error = 0; 6272c37256eSGarrett Wollman struct inpcb *inp = sotoinpcb(so); 6282c37256eSGarrett Wollman struct tcpcb *tp; 6292c37256eSGarrett Wollman 6302c37256eSGarrett Wollman COMMON_START(); 6312c37256eSGarrett Wollman if ((so->so_oobmark == 0 && 6322c37256eSGarrett Wollman (so->so_state & SS_RCVATMARK) == 0) || 6332c37256eSGarrett Wollman so->so_options & SO_OOBINLINE || 6342c37256eSGarrett Wollman tp->t_oobflags & TCPOOB_HADDATA) { 6352c37256eSGarrett Wollman error = EINVAL; 6362c37256eSGarrett Wollman goto out; 6372c37256eSGarrett Wollman } 6382c37256eSGarrett Wollman if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 6392c37256eSGarrett Wollman error = EWOULDBLOCK; 6402c37256eSGarrett Wollman goto out; 6412c37256eSGarrett Wollman } 6422c37256eSGarrett Wollman m->m_len = 1; 6432c37256eSGarrett Wollman *mtod(m, caddr_t) = tp->t_iobc; 6442c37256eSGarrett Wollman if ((flags & MSG_PEEK) == 0) 6452c37256eSGarrett Wollman tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 6462c37256eSGarrett Wollman COMMON_END(PRU_RCVOOB); 6472c37256eSGarrett Wollman } 6482c37256eSGarrett Wollman 6492c37256eSGarrett Wollman /* xxx - should be const */ 6502c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = { 6512c37256eSGarrett Wollman tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind, 652117bcae7SGarrett Wollman tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach, 653117bcae7SGarrett Wollman tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd, 654117bcae7SGarrett Wollman tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown, 655f8f6cbbaSPeter Wemm in_setsockaddr, sosend, soreceive, sopoll 6562c37256eSGarrett Wollman }; 657df8bae1dSRodney W. Grimes 658fb59c426SYoshinobu Inoue #ifdef INET6 659fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = { 660fb59c426SYoshinobu Inoue tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind, 661fb59c426SYoshinobu Inoue tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach, 662fb59c426SYoshinobu Inoue tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd, 663fb59c426SYoshinobu Inoue tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown, 664fb59c426SYoshinobu Inoue in6_mapped_sockaddr, sosend, soreceive, sopoll 665fb59c426SYoshinobu Inoue }; 666fb59c426SYoshinobu Inoue #endif /* INET6 */ 667fb59c426SYoshinobu Inoue 668a0292f23SGarrett Wollman /* 669a0292f23SGarrett Wollman * Common subroutine to open a TCP connection to remote host specified 670a0292f23SGarrett Wollman * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 671a0292f23SGarrett Wollman * port number if needed. Call in_pcbladdr to do the routing and to choose 672a0292f23SGarrett Wollman * a local host address (interface). If there is an existing incarnation 673a0292f23SGarrett Wollman * of the same connection in TIME-WAIT state and if the remote host was 674a0292f23SGarrett Wollman * sending CC options and if the connection duration was < MSL, then 675a0292f23SGarrett Wollman * truncate the previous TIME-WAIT state and proceed. 676a0292f23SGarrett Wollman * Initialize connection parameters and enter SYN-SENT state. 677a0292f23SGarrett Wollman */ 6780312fbe9SPoul-Henning Kamp static int 679a29f300eSGarrett Wollman tcp_connect(tp, nam, p) 680a0292f23SGarrett Wollman register struct tcpcb *tp; 68157bf258eSGarrett Wollman struct sockaddr *nam; 682a29f300eSGarrett Wollman struct proc *p; 683a0292f23SGarrett Wollman { 684a0292f23SGarrett Wollman struct inpcb *inp = tp->t_inpcb, *oinp; 685a0292f23SGarrett Wollman struct socket *so = inp->inp_socket; 686a0292f23SGarrett Wollman struct tcpcb *otp; 68757bf258eSGarrett Wollman struct sockaddr_in *sin = (struct sockaddr_in *)nam; 688a0292f23SGarrett Wollman struct sockaddr_in *ifaddr; 689a45d2726SAndras Olah struct rmxp_tao *taop; 690a45d2726SAndras Olah struct rmxp_tao tao_noncached; 691c3229e05SDavid Greenman int error; 692a0292f23SGarrett Wollman 693a0292f23SGarrett Wollman if (inp->inp_lport == 0) { 69457bf258eSGarrett Wollman error = in_pcbbind(inp, (struct sockaddr *)0, p); 695a0292f23SGarrett Wollman if (error) 696a0292f23SGarrett Wollman return error; 697a0292f23SGarrett Wollman } 698a0292f23SGarrett Wollman 699a0292f23SGarrett Wollman /* 700a0292f23SGarrett Wollman * Cannot simply call in_pcbconnect, because there might be an 701a0292f23SGarrett Wollman * earlier incarnation of this same connection still in 702a0292f23SGarrett Wollman * TIME_WAIT state, creating an ADDRINUSE error. 703a0292f23SGarrett Wollman */ 704a0292f23SGarrett Wollman error = in_pcbladdr(inp, nam, &ifaddr); 705d3628763SRodney W. Grimes if (error) 706d3628763SRodney W. Grimes return error; 707c3229e05SDavid Greenman oinp = in_pcblookup_hash(inp->inp_pcbinfo, 708a0292f23SGarrett Wollman sin->sin_addr, sin->sin_port, 709a0292f23SGarrett Wollman inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr 710a0292f23SGarrett Wollman : ifaddr->sin_addr, 711cfa1ca9dSYoshinobu Inoue inp->inp_lport, 0, NULL); 712a0292f23SGarrett Wollman if (oinp) { 713a0292f23SGarrett Wollman if (oinp != inp && (otp = intotcpcb(oinp)) != NULL && 714a0292f23SGarrett Wollman otp->t_state == TCPS_TIME_WAIT && 7159b8b58e0SJonathan Lemon (ticks - otp->t_starttime) < tcp_msl && 716a0292f23SGarrett Wollman (otp->t_flags & TF_RCVD_CC)) 717a0292f23SGarrett Wollman otp = tcp_close(otp); 718a0292f23SGarrett Wollman else 719a0292f23SGarrett Wollman return EADDRINUSE; 720a0292f23SGarrett Wollman } 721a0292f23SGarrett Wollman if (inp->inp_laddr.s_addr == INADDR_ANY) 722a0292f23SGarrett Wollman inp->inp_laddr = ifaddr->sin_addr; 723a0292f23SGarrett Wollman inp->inp_faddr = sin->sin_addr; 724a0292f23SGarrett Wollman inp->inp_fport = sin->sin_port; 72515bd2b43SDavid Greenman in_pcbrehash(inp); 726a0292f23SGarrett Wollman 727a0292f23SGarrett Wollman tp->t_template = tcp_template(tp); 728a0292f23SGarrett Wollman if (tp->t_template == 0) { 729a0292f23SGarrett Wollman in_pcbdisconnect(inp); 730a0292f23SGarrett Wollman return ENOBUFS; 731a0292f23SGarrett Wollman } 732a0292f23SGarrett Wollman 733a0292f23SGarrett Wollman /* Compute window scaling to request. */ 734a0292f23SGarrett Wollman while (tp->request_r_scale < TCP_MAX_WINSHIFT && 735a0292f23SGarrett Wollman (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 736a0292f23SGarrett Wollman tp->request_r_scale++; 737a0292f23SGarrett Wollman 738a0292f23SGarrett Wollman soisconnecting(so); 739a0292f23SGarrett Wollman tcpstat.tcps_connattempt++; 740a0292f23SGarrett Wollman tp->t_state = TCPS_SYN_SENT; 7419b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 742a0292f23SGarrett Wollman tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 743a0292f23SGarrett Wollman tcp_sendseqinit(tp); 744a45d2726SAndras Olah 745a45d2726SAndras Olah /* 746a45d2726SAndras Olah * Generate a CC value for this connection and 747a45d2726SAndras Olah * check whether CC or CCnew should be used. 748a45d2726SAndras Olah */ 749a45d2726SAndras Olah if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) { 750a45d2726SAndras Olah taop = &tao_noncached; 751a45d2726SAndras Olah bzero(taop, sizeof(*taop)); 752a45d2726SAndras Olah } 753a45d2726SAndras Olah 754a0292f23SGarrett Wollman tp->cc_send = CC_INC(tcp_ccgen); 755a45d2726SAndras Olah if (taop->tao_ccsent != 0 && 756a45d2726SAndras Olah CC_GEQ(tp->cc_send, taop->tao_ccsent)) { 757a45d2726SAndras Olah taop->tao_ccsent = tp->cc_send; 758a45d2726SAndras Olah } else { 759a45d2726SAndras Olah taop->tao_ccsent = 0; 760a45d2726SAndras Olah tp->t_flags |= TF_SENDCCNEW; 761a45d2726SAndras Olah } 762a0292f23SGarrett Wollman 763a0292f23SGarrett Wollman return 0; 764a0292f23SGarrett Wollman } 765a0292f23SGarrett Wollman 766fb59c426SYoshinobu Inoue #ifdef INET6 767fb59c426SYoshinobu Inoue static int 768fb59c426SYoshinobu Inoue tcp6_connect(tp, nam, p) 769fb59c426SYoshinobu Inoue register struct tcpcb *tp; 770fb59c426SYoshinobu Inoue struct sockaddr *nam; 771fb59c426SYoshinobu Inoue struct proc *p; 772fb59c426SYoshinobu Inoue { 773fb59c426SYoshinobu Inoue struct inpcb *inp = tp->t_inpcb, *oinp; 774fb59c426SYoshinobu Inoue struct socket *so = inp->inp_socket; 775fb59c426SYoshinobu Inoue struct tcpcb *otp; 776fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 777fb59c426SYoshinobu Inoue struct in6_addr *addr6; 778fb59c426SYoshinobu Inoue struct rmxp_tao *taop; 779fb59c426SYoshinobu Inoue struct rmxp_tao tao_noncached; 780fb59c426SYoshinobu Inoue int error; 781fb59c426SYoshinobu Inoue 782fb59c426SYoshinobu Inoue if (inp->inp_lport == 0) { 783fb59c426SYoshinobu Inoue error = in6_pcbbind(inp, (struct sockaddr *)0, p); 784fb59c426SYoshinobu Inoue if (error) 785fb59c426SYoshinobu Inoue return error; 786fb59c426SYoshinobu Inoue } 787fb59c426SYoshinobu Inoue 788fb59c426SYoshinobu Inoue /* 789fb59c426SYoshinobu Inoue * Cannot simply call in_pcbconnect, because there might be an 790fb59c426SYoshinobu Inoue * earlier incarnation of this same connection still in 791fb59c426SYoshinobu Inoue * TIME_WAIT state, creating an ADDRINUSE error. 792fb59c426SYoshinobu Inoue */ 793fb59c426SYoshinobu Inoue error = in6_pcbladdr(inp, nam, &addr6); 794fb59c426SYoshinobu Inoue if (error) 795fb59c426SYoshinobu Inoue return error; 796fb59c426SYoshinobu Inoue oinp = in6_pcblookup_hash(inp->inp_pcbinfo, 797fb59c426SYoshinobu Inoue &sin6->sin6_addr, sin6->sin6_port, 798fb59c426SYoshinobu Inoue IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 799fb59c426SYoshinobu Inoue ? addr6 800fb59c426SYoshinobu Inoue : &inp->in6p_laddr, 801fb59c426SYoshinobu Inoue inp->inp_lport, 0, NULL); 802fb59c426SYoshinobu Inoue if (oinp) { 803fb59c426SYoshinobu Inoue if (oinp != inp && (otp = intotcpcb(oinp)) != NULL && 804fb59c426SYoshinobu Inoue otp->t_state == TCPS_TIME_WAIT && 805fb59c426SYoshinobu Inoue (ticks - otp->t_starttime) < tcp_msl && 806fb59c426SYoshinobu Inoue (otp->t_flags & TF_RCVD_CC)) 807fb59c426SYoshinobu Inoue otp = tcp_close(otp); 808fb59c426SYoshinobu Inoue else 809fb59c426SYoshinobu Inoue return EADDRINUSE; 810fb59c426SYoshinobu Inoue } 811fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 812fb59c426SYoshinobu Inoue inp->in6p_laddr = *addr6; 813fb59c426SYoshinobu Inoue inp->in6p_faddr = sin6->sin6_addr; 814fb59c426SYoshinobu Inoue inp->inp_fport = sin6->sin6_port; 815fb59c426SYoshinobu Inoue if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL) 816fb59c426SYoshinobu Inoue inp->in6p_flowinfo = sin6->sin6_flowinfo; 817fb59c426SYoshinobu Inoue in_pcbrehash(inp); 818fb59c426SYoshinobu Inoue 819fb59c426SYoshinobu Inoue tp->t_template = tcp_template(tp); 820fb59c426SYoshinobu Inoue if (tp->t_template == 0) { 821fb59c426SYoshinobu Inoue in6_pcbdisconnect(inp); 822fb59c426SYoshinobu Inoue return ENOBUFS; 823fb59c426SYoshinobu Inoue } 824fb59c426SYoshinobu Inoue 825fb59c426SYoshinobu Inoue /* Compute window scaling to request. */ 826fb59c426SYoshinobu Inoue while (tp->request_r_scale < TCP_MAX_WINSHIFT && 827fb59c426SYoshinobu Inoue (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 828fb59c426SYoshinobu Inoue tp->request_r_scale++; 829fb59c426SYoshinobu Inoue 830fb59c426SYoshinobu Inoue soisconnecting(so); 831fb59c426SYoshinobu Inoue tcpstat.tcps_connattempt++; 832fb59c426SYoshinobu Inoue tp->t_state = TCPS_SYN_SENT; 833fb59c426SYoshinobu Inoue callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 834fb59c426SYoshinobu Inoue tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 835fb59c426SYoshinobu Inoue tcp_sendseqinit(tp); 836fb59c426SYoshinobu Inoue 837fb59c426SYoshinobu Inoue /* 838fb59c426SYoshinobu Inoue * Generate a CC value for this connection and 839fb59c426SYoshinobu Inoue * check whether CC or CCnew should be used. 840fb59c426SYoshinobu Inoue */ 841fb59c426SYoshinobu Inoue if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) { 842fb59c426SYoshinobu Inoue taop = &tao_noncached; 843fb59c426SYoshinobu Inoue bzero(taop, sizeof(*taop)); 844fb59c426SYoshinobu Inoue } 845fb59c426SYoshinobu Inoue 846fb59c426SYoshinobu Inoue tp->cc_send = CC_INC(tcp_ccgen); 847fb59c426SYoshinobu Inoue if (taop->tao_ccsent != 0 && 848fb59c426SYoshinobu Inoue CC_GEQ(tp->cc_send, taop->tao_ccsent)) { 849fb59c426SYoshinobu Inoue taop->tao_ccsent = tp->cc_send; 850fb59c426SYoshinobu Inoue } else { 851fb59c426SYoshinobu Inoue taop->tao_ccsent = 0; 852fb59c426SYoshinobu Inoue tp->t_flags |= TF_SENDCCNEW; 853fb59c426SYoshinobu Inoue } 854fb59c426SYoshinobu Inoue 855fb59c426SYoshinobu Inoue return 0; 856fb59c426SYoshinobu Inoue } 857fb59c426SYoshinobu Inoue #endif /* INET6 */ 858fb59c426SYoshinobu Inoue 859cfe8b629SGarrett Wollman /* 860cfe8b629SGarrett Wollman * The new sockopt interface makes it possible for us to block in the 861cfe8b629SGarrett Wollman * copyin/out step (if we take a page fault). Taking a page fault at 862cfe8b629SGarrett Wollman * splnet() is probably a Bad Thing. (Since sockets and pcbs both now 863cfe8b629SGarrett Wollman * use TSM, there probably isn't any need for this function to run at 864cfe8b629SGarrett Wollman * splnet() any more. This needs more examination.) 865cfe8b629SGarrett Wollman */ 866df8bae1dSRodney W. Grimes int 867cfe8b629SGarrett Wollman tcp_ctloutput(so, sopt) 868df8bae1dSRodney W. Grimes struct socket *so; 869cfe8b629SGarrett Wollman struct sockopt *sopt; 870df8bae1dSRodney W. Grimes { 871cfe8b629SGarrett Wollman int error, opt, optval, s; 872df8bae1dSRodney W. Grimes struct inpcb *inp; 873cfe8b629SGarrett Wollman struct tcpcb *tp; 874df8bae1dSRodney W. Grimes 875cfe8b629SGarrett Wollman error = 0; 876cfe8b629SGarrett Wollman s = splnet(); /* XXX */ 877df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 878df8bae1dSRodney W. Grimes if (inp == NULL) { 879df8bae1dSRodney W. Grimes splx(s); 880df8bae1dSRodney W. Grimes return (ECONNRESET); 881df8bae1dSRodney W. Grimes } 882cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_TCP) { 883fb59c426SYoshinobu Inoue #ifdef INET6 884fb59c426SYoshinobu Inoue if (INP_CHECK_SOCKAF(so, AF_INET6)) 885fb59c426SYoshinobu Inoue error = ip6_ctloutput(so, sopt); 886fb59c426SYoshinobu Inoue else 887fb59c426SYoshinobu Inoue #endif /* INET6 */ 888cfe8b629SGarrett Wollman error = ip_ctloutput(so, sopt); 889df8bae1dSRodney W. Grimes splx(s); 890df8bae1dSRodney W. Grimes return (error); 891df8bae1dSRodney W. Grimes } 892df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 893df8bae1dSRodney W. Grimes 894cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 895cfe8b629SGarrett Wollman case SOPT_SET: 896cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 897df8bae1dSRodney W. Grimes case TCP_NODELAY: 898cfe8b629SGarrett Wollman case TCP_NOOPT: 899cfe8b629SGarrett Wollman case TCP_NOPUSH: 900cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 901cfe8b629SGarrett Wollman sizeof optval); 902cfe8b629SGarrett Wollman if (error) 903cfe8b629SGarrett Wollman break; 904cfe8b629SGarrett Wollman 905cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 906cfe8b629SGarrett Wollman case TCP_NODELAY: 907cfe8b629SGarrett Wollman opt = TF_NODELAY; 908cfe8b629SGarrett Wollman break; 909cfe8b629SGarrett Wollman case TCP_NOOPT: 910cfe8b629SGarrett Wollman opt = TF_NOOPT; 911cfe8b629SGarrett Wollman break; 912cfe8b629SGarrett Wollman case TCP_NOPUSH: 913cfe8b629SGarrett Wollman opt = TF_NOPUSH; 914cfe8b629SGarrett Wollman break; 915cfe8b629SGarrett Wollman default: 916cfe8b629SGarrett Wollman opt = 0; /* dead code to fool gcc */ 917cfe8b629SGarrett Wollman break; 918cfe8b629SGarrett Wollman } 919cfe8b629SGarrett Wollman 920cfe8b629SGarrett Wollman if (optval) 921cfe8b629SGarrett Wollman tp->t_flags |= opt; 922df8bae1dSRodney W. Grimes else 923cfe8b629SGarrett Wollman tp->t_flags &= ~opt; 924df8bae1dSRodney W. Grimes break; 925df8bae1dSRodney W. Grimes 926df8bae1dSRodney W. Grimes case TCP_MAXSEG: 927cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 928cfe8b629SGarrett Wollman sizeof optval); 929cfe8b629SGarrett Wollman if (error) 930df8bae1dSRodney W. Grimes break; 931df8bae1dSRodney W. Grimes 932cfe8b629SGarrett Wollman if (optval > 0 && optval <= tp->t_maxseg) 933cfe8b629SGarrett Wollman tp->t_maxseg = optval; 934a0292f23SGarrett Wollman else 935a0292f23SGarrett Wollman error = EINVAL; 936a0292f23SGarrett Wollman break; 937a0292f23SGarrett Wollman 938df8bae1dSRodney W. Grimes default: 939df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 940df8bae1dSRodney W. Grimes break; 941df8bae1dSRodney W. Grimes } 942df8bae1dSRodney W. Grimes break; 943df8bae1dSRodney W. Grimes 944cfe8b629SGarrett Wollman case SOPT_GET: 945cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 946df8bae1dSRodney W. Grimes case TCP_NODELAY: 947cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NODELAY; 948df8bae1dSRodney W. Grimes break; 949df8bae1dSRodney W. Grimes case TCP_MAXSEG: 950cfe8b629SGarrett Wollman optval = tp->t_maxseg; 951df8bae1dSRodney W. Grimes break; 952a0292f23SGarrett Wollman case TCP_NOOPT: 953cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOOPT; 954a0292f23SGarrett Wollman break; 955a0292f23SGarrett Wollman case TCP_NOPUSH: 956cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOPUSH; 957a0292f23SGarrett Wollman break; 958df8bae1dSRodney W. Grimes default: 959df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 960df8bae1dSRodney W. Grimes break; 961df8bae1dSRodney W. Grimes } 962cfe8b629SGarrett Wollman if (error == 0) 963cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 964df8bae1dSRodney W. Grimes break; 965df8bae1dSRodney W. Grimes } 966df8bae1dSRodney W. Grimes splx(s); 967df8bae1dSRodney W. Grimes return (error); 968df8bae1dSRodney W. Grimes } 969df8bae1dSRodney W. Grimes 97026e30fbbSDavid Greenman /* 97126e30fbbSDavid Greenman * tcp_sendspace and tcp_recvspace are the default send and receive window 97226e30fbbSDavid Greenman * sizes, respectively. These are obsolescent (this information should 97326e30fbbSDavid Greenman * be set by the route). 97426e30fbbSDavid Greenman */ 97526e30fbbSDavid Greenman u_long tcp_sendspace = 1024*16; 9763d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW, 9773d177f46SBill Fumerola &tcp_sendspace , 0, "Maximum outgoing TCP datagram size"); 97826e30fbbSDavid Greenman u_long tcp_recvspace = 1024*16; 9793d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 9803d177f46SBill Fumerola &tcp_recvspace , 0, "Maximum incoming TCP datagram size"); 981df8bae1dSRodney W. Grimes 982df8bae1dSRodney W. Grimes /* 983df8bae1dSRodney W. Grimes * Attach TCP protocol to socket, allocating 984df8bae1dSRodney W. Grimes * internet protocol control block, tcp control block, 985df8bae1dSRodney W. Grimes * bufer space, and entering LISTEN state if to accept connections. 986df8bae1dSRodney W. Grimes */ 9870312fbe9SPoul-Henning Kamp static int 988a29f300eSGarrett Wollman tcp_attach(so, p) 989df8bae1dSRodney W. Grimes struct socket *so; 990a29f300eSGarrett Wollman struct proc *p; 991df8bae1dSRodney W. Grimes { 992df8bae1dSRodney W. Grimes register struct tcpcb *tp; 993df8bae1dSRodney W. Grimes struct inpcb *inp; 994df8bae1dSRodney W. Grimes int error; 995fb59c426SYoshinobu Inoue #ifdef INET6 996fb59c426SYoshinobu Inoue int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL; 997fb59c426SYoshinobu Inoue #endif 998df8bae1dSRodney W. Grimes 999df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1000df8bae1dSRodney W. Grimes error = soreserve(so, tcp_sendspace, tcp_recvspace); 1001df8bae1dSRodney W. Grimes if (error) 1002df8bae1dSRodney W. Grimes return (error); 1003df8bae1dSRodney W. Grimes } 1004a29f300eSGarrett Wollman error = in_pcballoc(so, &tcbinfo, p); 1005df8bae1dSRodney W. Grimes if (error) 1006df8bae1dSRodney W. Grimes return (error); 1007df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 10086a800098SYoshinobu Inoue #ifdef IPSEC 10096a800098SYoshinobu Inoue error = ipsec_init_policy(so, &inp->inp_sp); 10106a800098SYoshinobu Inoue if (error) { 1011fb59c426SYoshinobu Inoue #ifdef INET6 1012fb59c426SYoshinobu Inoue if (isipv6) 1013fb59c426SYoshinobu Inoue in6_pcbdetach(inp); 1014fb59c426SYoshinobu Inoue else 1015fb59c426SYoshinobu Inoue #endif 10166a800098SYoshinobu Inoue in_pcbdetach(inp); 10176a800098SYoshinobu Inoue return (error); 10186a800098SYoshinobu Inoue } 10196a800098SYoshinobu Inoue #endif /*IPSEC*/ 1020fb59c426SYoshinobu Inoue #ifdef INET6 1021fb59c426SYoshinobu Inoue if (isipv6) { 1022fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 1023fb59c426SYoshinobu Inoue inp->in6p_hops = -1; /* use kernel default */ 1024fb59c426SYoshinobu Inoue } 1025fb59c426SYoshinobu Inoue else 1026fb59c426SYoshinobu Inoue #endif 1027cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 1028df8bae1dSRodney W. Grimes tp = tcp_newtcpcb(inp); 1029df8bae1dSRodney W. Grimes if (tp == 0) { 1030df8bae1dSRodney W. Grimes int nofd = so->so_state & SS_NOFDREF; /* XXX */ 1031df8bae1dSRodney W. Grimes 1032df8bae1dSRodney W. Grimes so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 1033fb59c426SYoshinobu Inoue #ifdef INET6 1034fb59c426SYoshinobu Inoue if (isipv6) 1035fb59c426SYoshinobu Inoue in6_pcbdetach(inp); 1036fb59c426SYoshinobu Inoue else 1037fb59c426SYoshinobu Inoue #endif 1038df8bae1dSRodney W. Grimes in_pcbdetach(inp); 1039df8bae1dSRodney W. Grimes so->so_state |= nofd; 1040df8bae1dSRodney W. Grimes return (ENOBUFS); 1041df8bae1dSRodney W. Grimes } 1042df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 1043df8bae1dSRodney W. Grimes return (0); 1044df8bae1dSRodney W. Grimes } 1045df8bae1dSRodney W. Grimes 1046df8bae1dSRodney W. Grimes /* 1047df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 1048df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 1049df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 1050df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 1051df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 1052df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 1053df8bae1dSRodney W. Grimes */ 10540312fbe9SPoul-Henning Kamp static struct tcpcb * 1055df8bae1dSRodney W. Grimes tcp_disconnect(tp) 1056df8bae1dSRodney W. Grimes register struct tcpcb *tp; 1057df8bae1dSRodney W. Grimes { 1058df8bae1dSRodney W. Grimes struct socket *so = tp->t_inpcb->inp_socket; 1059df8bae1dSRodney W. Grimes 1060df8bae1dSRodney W. Grimes if (tp->t_state < TCPS_ESTABLISHED) 1061df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1062df8bae1dSRodney W. Grimes else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1063df8bae1dSRodney W. Grimes tp = tcp_drop(tp, 0); 1064df8bae1dSRodney W. Grimes else { 1065df8bae1dSRodney W. Grimes soisdisconnecting(so); 1066df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 1067df8bae1dSRodney W. Grimes tp = tcp_usrclosed(tp); 1068df8bae1dSRodney W. Grimes if (tp) 1069df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1070df8bae1dSRodney W. Grimes } 1071df8bae1dSRodney W. Grimes return (tp); 1072df8bae1dSRodney W. Grimes } 1073df8bae1dSRodney W. Grimes 1074df8bae1dSRodney W. Grimes /* 1075df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 1076df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 1077df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1078df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 1079df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 1080df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 1081df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 1082df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 1083df8bae1dSRodney W. Grimes */ 10840312fbe9SPoul-Henning Kamp static struct tcpcb * 1085df8bae1dSRodney W. Grimes tcp_usrclosed(tp) 1086df8bae1dSRodney W. Grimes register struct tcpcb *tp; 1087df8bae1dSRodney W. Grimes { 1088df8bae1dSRodney W. Grimes 1089df8bae1dSRodney W. Grimes switch (tp->t_state) { 1090df8bae1dSRodney W. Grimes 1091df8bae1dSRodney W. Grimes case TCPS_CLOSED: 1092df8bae1dSRodney W. Grimes case TCPS_LISTEN: 1093df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 1094df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1095df8bae1dSRodney W. Grimes break; 1096df8bae1dSRodney W. Grimes 1097a0292f23SGarrett Wollman case TCPS_SYN_SENT: 1098df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1099a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 1100a0292f23SGarrett Wollman break; 1101a0292f23SGarrett Wollman 1102df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1103df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_1; 1104df8bae1dSRodney W. Grimes break; 1105df8bae1dSRodney W. Grimes 1106df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1107df8bae1dSRodney W. Grimes tp->t_state = TCPS_LAST_ACK; 1108df8bae1dSRodney W. Grimes break; 1109df8bae1dSRodney W. Grimes } 1110b6239c4aSAndras Olah if (tp && tp->t_state >= TCPS_FIN_WAIT_2) { 1111df8bae1dSRodney W. Grimes soisdisconnected(tp->t_inpcb->inp_socket); 1112b6239c4aSAndras Olah /* To prevent the connection hanging in FIN_WAIT_2 forever. */ 1113b6239c4aSAndras Olah if (tp->t_state == TCPS_FIN_WAIT_2) 11149b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, tcp_maxidle, 11159b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 1116b6239c4aSAndras Olah } 1117df8bae1dSRodney W. Grimes return (tp); 1118df8bae1dSRodney W. Grimes } 1119a0292f23SGarrett Wollman 1120