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> 43f76fcf6dSJeffrey Hsu #include <sys/malloc.h> 44c7a82f90SGarrett Wollman #include <sys/kernel.h> 4598163b98SPoul-Henning Kamp #include <sys/sysctl.h> 46df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 47fb59c426SYoshinobu Inoue #ifdef INET6 48fb59c426SYoshinobu Inoue #include <sys/domain.h> 49fb59c426SYoshinobu Inoue #endif /* INET6 */ 50df8bae1dSRodney W. Grimes #include <sys/socket.h> 51df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 52df8bae1dSRodney W. Grimes #include <sys/protosw.h> 5391421ba2SRobert Watson #include <sys/proc.h> 5491421ba2SRobert Watson #include <sys/jail.h> 55df8bae1dSRodney W. Grimes 56df8bae1dSRodney W. Grimes #include <net/if.h> 57df8bae1dSRodney W. Grimes #include <net/route.h> 58df8bae1dSRodney W. Grimes 59df8bae1dSRodney W. Grimes #include <netinet/in.h> 60df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 61fb59c426SYoshinobu Inoue #ifdef INET6 62fb59c426SYoshinobu Inoue #include <netinet/ip6.h> 63fb59c426SYoshinobu Inoue #endif 64df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 65fb59c426SYoshinobu Inoue #ifdef INET6 66fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h> 67fb59c426SYoshinobu Inoue #endif 68b5e8ce9fSBruce Evans #include <netinet/in_var.h> 69df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 70fb59c426SYoshinobu Inoue #ifdef INET6 71fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 72fb59c426SYoshinobu Inoue #endif 73df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 74df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 75df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 76df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 77df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 78df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 79610ee2f9SDavid Greenman #ifdef TCPDEBUG 80df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 81610ee2f9SDavid Greenman #endif 82df8bae1dSRodney W. Grimes 836a800098SYoshinobu Inoue #ifdef IPSEC 846a800098SYoshinobu Inoue #include <netinet6/ipsec.h> 856a800098SYoshinobu Inoue #endif /*IPSEC*/ 866a800098SYoshinobu Inoue 87df8bae1dSRodney W. Grimes /* 88df8bae1dSRodney W. Grimes * TCP protocol interface to socket abstraction. 89df8bae1dSRodney W. Grimes */ 90117bcae7SGarrett Wollman extern char *tcpstates[]; /* XXX ??? */ 91df8bae1dSRodney W. Grimes 924d77a549SAlfred Perlstein static int tcp_attach(struct socket *, struct thread *td); 934d77a549SAlfred Perlstein static int tcp_connect(struct tcpcb *, struct sockaddr *, 944d77a549SAlfred Perlstein struct thread *td); 95fb59c426SYoshinobu Inoue #ifdef INET6 964d77a549SAlfred Perlstein static int tcp6_connect(struct tcpcb *, struct sockaddr *, 974d77a549SAlfred Perlstein struct thread *td); 98fb59c426SYoshinobu Inoue #endif /* INET6 */ 990312fbe9SPoul-Henning Kamp static struct tcpcb * 1004d77a549SAlfred Perlstein tcp_disconnect(struct tcpcb *); 1010312fbe9SPoul-Henning Kamp static struct tcpcb * 1024d77a549SAlfred Perlstein tcp_usrclosed(struct tcpcb *); 1032c37256eSGarrett Wollman 1042c37256eSGarrett Wollman #ifdef TCPDEBUG 1051db24ffbSJonathan Lemon #define TCPDEBUG0 int ostate = 0 1062c37256eSGarrett Wollman #define TCPDEBUG1() ostate = tp ? tp->t_state : 0 1074cc20ab1SSeigo Tanimura #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 1084cc20ab1SSeigo Tanimura tcp_trace(TA_USER, ostate, tp, 0, 0, req) 1092c37256eSGarrett Wollman #else 1102c37256eSGarrett Wollman #define TCPDEBUG0 1112c37256eSGarrett Wollman #define TCPDEBUG1() 1122c37256eSGarrett Wollman #define TCPDEBUG2(req) 1132c37256eSGarrett Wollman #endif 1142c37256eSGarrett Wollman 1152c37256eSGarrett Wollman /* 1162c37256eSGarrett Wollman * TCP attaches to socket via pru_attach(), reserving space, 1172c37256eSGarrett Wollman * and an internet control block. 1182c37256eSGarrett Wollman */ 1192c37256eSGarrett Wollman static int 120b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td) 1212c37256eSGarrett Wollman { 1222c37256eSGarrett Wollman int s = splnet(); 1232c37256eSGarrett Wollman int error; 124f76fcf6dSJeffrey Hsu struct inpcb *inp; 1252c37256eSGarrett Wollman struct tcpcb *tp = 0; 1262c37256eSGarrett Wollman TCPDEBUG0; 1272c37256eSGarrett Wollman 128f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 1292c37256eSGarrett Wollman TCPDEBUG1(); 130f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 1312c37256eSGarrett Wollman if (inp) { 1322c37256eSGarrett Wollman error = EISCONN; 1332c37256eSGarrett Wollman goto out; 1342c37256eSGarrett Wollman } 1352c37256eSGarrett Wollman 136b40ce416SJulian Elischer error = tcp_attach(so, td); 1372c37256eSGarrett Wollman if (error) 1382c37256eSGarrett Wollman goto out; 1392c37256eSGarrett Wollman 1402c37256eSGarrett Wollman if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1413879597fSAndrey A. Chernov so->so_linger = TCP_LINGERTIME; 142f76fcf6dSJeffrey Hsu 143f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 144f76fcf6dSJeffrey Hsu INP_LOCK(inp); 145f76fcf6dSJeffrey Hsu tp = intotcpcb(inp); 1462c37256eSGarrett Wollman out: 1472c37256eSGarrett Wollman TCPDEBUG2(PRU_ATTACH); 148f76fcf6dSJeffrey Hsu if (tp) 149f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 150f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 1512c37256eSGarrett Wollman splx(s); 1522c37256eSGarrett Wollman return error; 1532c37256eSGarrett Wollman } 1542c37256eSGarrett Wollman 1552c37256eSGarrett Wollman /* 1562c37256eSGarrett Wollman * pru_detach() detaches the TCP protocol from the socket. 1572c37256eSGarrett Wollman * If the protocol state is non-embryonic, then can't 1582c37256eSGarrett Wollman * do this directly: have to initiate a pru_disconnect(), 1592c37256eSGarrett Wollman * which may finish later; embryonic TCB's can just 1602c37256eSGarrett Wollman * be discarded here. 1612c37256eSGarrett Wollman */ 1622c37256eSGarrett Wollman static int 1632c37256eSGarrett Wollman tcp_usr_detach(struct socket *so) 1642c37256eSGarrett Wollman { 1652c37256eSGarrett Wollman int s = splnet(); 1662c37256eSGarrett Wollman int error = 0; 167f76fcf6dSJeffrey Hsu struct inpcb *inp; 1682c37256eSGarrett Wollman struct tcpcb *tp; 1692c37256eSGarrett Wollman TCPDEBUG0; 1702c37256eSGarrett Wollman 171f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 172f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 1732c37256eSGarrett Wollman if (inp == 0) { 174f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 1752c37256eSGarrett Wollman splx(s); 1762c37256eSGarrett Wollman return EINVAL; /* XXX */ 1772c37256eSGarrett Wollman } 178f76fcf6dSJeffrey Hsu INP_LOCK(inp); 1792c37256eSGarrett Wollman tp = intotcpcb(inp); 1802c37256eSGarrett Wollman TCPDEBUG1(); 1812c37256eSGarrett Wollman tp = tcp_disconnect(tp); 1822c37256eSGarrett Wollman 1832c37256eSGarrett Wollman TCPDEBUG2(PRU_DETACH); 184f76fcf6dSJeffrey Hsu if (tp) 185f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 186f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 1872c37256eSGarrett Wollman splx(s); 1882c37256eSGarrett Wollman return error; 1892c37256eSGarrett Wollman } 1902c37256eSGarrett Wollman 191f76fcf6dSJeffrey Hsu #define INI_NOLOCK 0 192f76fcf6dSJeffrey Hsu #define INI_READ 1 193f76fcf6dSJeffrey Hsu #define INI_WRITE 2 194f76fcf6dSJeffrey Hsu 195f76fcf6dSJeffrey Hsu #define COMMON_START() \ 196f76fcf6dSJeffrey Hsu TCPDEBUG0; \ 1972c37256eSGarrett Wollman do { \ 198f76fcf6dSJeffrey Hsu if (inirw == INI_READ) \ 199f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(&tcbinfo); \ 200f76fcf6dSJeffrey Hsu else if (inirw == INI_WRITE) \ 201f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); \ 202f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); \ 2032c37256eSGarrett Wollman if (inp == 0) { \ 204f76fcf6dSJeffrey Hsu if (inirw == INI_READ) \ 205f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&tcbinfo); \ 206f76fcf6dSJeffrey Hsu else if (inirw == INI_WRITE) \ 207f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); \ 2082c37256eSGarrett Wollman splx(s); \ 2092c37256eSGarrett Wollman return EINVAL; \ 2102c37256eSGarrett Wollman } \ 211f76fcf6dSJeffrey Hsu INP_LOCK(inp); \ 212f76fcf6dSJeffrey Hsu if (inirw == INI_READ) \ 213f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&tcbinfo); \ 2142c37256eSGarrett Wollman tp = intotcpcb(inp); \ 2152c37256eSGarrett Wollman TCPDEBUG1(); \ 2162c37256eSGarrett Wollman } while(0) 2172c37256eSGarrett Wollman 218f76fcf6dSJeffrey Hsu #define COMMON_END(req) \ 219f76fcf6dSJeffrey Hsu out: TCPDEBUG2(req); \ 220f76fcf6dSJeffrey Hsu do { \ 221f76fcf6dSJeffrey Hsu if (tp) \ 222f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); \ 223f76fcf6dSJeffrey Hsu if (inirw == INI_WRITE) \ 224f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); \ 225f76fcf6dSJeffrey Hsu splx(s); \ 226f76fcf6dSJeffrey Hsu return error; \ 227f76fcf6dSJeffrey Hsu goto out; \ 228f76fcf6dSJeffrey Hsu } while(0) 2292c37256eSGarrett Wollman 2302c37256eSGarrett Wollman /* 2312c37256eSGarrett Wollman * Give the socket an address. 2322c37256eSGarrett Wollman */ 2332c37256eSGarrett Wollman static int 234b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 2352c37256eSGarrett Wollman { 2362c37256eSGarrett Wollman int s = splnet(); 2372c37256eSGarrett Wollman int error = 0; 238f76fcf6dSJeffrey Hsu struct inpcb *inp; 2392c37256eSGarrett Wollman struct tcpcb *tp; 2402c37256eSGarrett Wollman struct sockaddr_in *sinp; 241f76fcf6dSJeffrey Hsu const int inirw = INI_READ; 2422c37256eSGarrett Wollman 2432c37256eSGarrett Wollman COMMON_START(); 2442c37256eSGarrett Wollman 2452c37256eSGarrett Wollman /* 2462c37256eSGarrett Wollman * Must check for multicast addresses and disallow binding 2472c37256eSGarrett Wollman * to them. 2482c37256eSGarrett Wollman */ 24957bf258eSGarrett Wollman sinp = (struct sockaddr_in *)nam; 2502c37256eSGarrett Wollman if (sinp->sin_family == AF_INET && 2512c37256eSGarrett Wollman IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 2522c37256eSGarrett Wollman error = EAFNOSUPPORT; 2532c37256eSGarrett Wollman goto out; 2542c37256eSGarrett Wollman } 255b40ce416SJulian Elischer error = in_pcbbind(inp, nam, td); 2562c37256eSGarrett Wollman if (error) 2572c37256eSGarrett Wollman goto out; 2582c37256eSGarrett Wollman COMMON_END(PRU_BIND); 2592c37256eSGarrett Wollman } 2602c37256eSGarrett Wollman 261fb59c426SYoshinobu Inoue #ifdef INET6 262fb59c426SYoshinobu Inoue static int 263b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 264fb59c426SYoshinobu Inoue { 265fb59c426SYoshinobu Inoue int s = splnet(); 266fb59c426SYoshinobu Inoue int error = 0; 267f76fcf6dSJeffrey Hsu struct inpcb *inp; 268fb59c426SYoshinobu Inoue struct tcpcb *tp; 269fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 270f76fcf6dSJeffrey Hsu const int inirw = INI_READ; 271fb59c426SYoshinobu Inoue 272fb59c426SYoshinobu Inoue COMMON_START(); 273fb59c426SYoshinobu Inoue 274fb59c426SYoshinobu Inoue /* 275fb59c426SYoshinobu Inoue * Must check for multicast addresses and disallow binding 276fb59c426SYoshinobu Inoue * to them. 277fb59c426SYoshinobu Inoue */ 278fb59c426SYoshinobu Inoue sin6p = (struct sockaddr_in6 *)nam; 279fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 && 280fb59c426SYoshinobu Inoue IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) { 281fb59c426SYoshinobu Inoue error = EAFNOSUPPORT; 282fb59c426SYoshinobu Inoue goto out; 283fb59c426SYoshinobu Inoue } 284fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 285fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 28633841545SHajimu UMEMOTO if (ip6_mapped_addr_on && (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 287fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) 288fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 289fb59c426SYoshinobu Inoue else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 290fb59c426SYoshinobu Inoue struct sockaddr_in sin; 291fb59c426SYoshinobu Inoue 292fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 293fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 294fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 295b40ce416SJulian Elischer error = in_pcbbind(inp, (struct sockaddr *)&sin, td); 296fb59c426SYoshinobu Inoue goto out; 297fb59c426SYoshinobu Inoue } 298fb59c426SYoshinobu Inoue } 299b40ce416SJulian Elischer error = in6_pcbbind(inp, nam, td); 300fb59c426SYoshinobu Inoue if (error) 301fb59c426SYoshinobu Inoue goto out; 302fb59c426SYoshinobu Inoue COMMON_END(PRU_BIND); 303fb59c426SYoshinobu Inoue } 304fb59c426SYoshinobu Inoue #endif /* INET6 */ 305fb59c426SYoshinobu Inoue 3062c37256eSGarrett Wollman /* 3072c37256eSGarrett Wollman * Prepare to accept connections. 3082c37256eSGarrett Wollman */ 3092c37256eSGarrett Wollman static int 310b40ce416SJulian Elischer tcp_usr_listen(struct socket *so, struct thread *td) 3112c37256eSGarrett Wollman { 3122c37256eSGarrett Wollman int s = splnet(); 3132c37256eSGarrett Wollman int error = 0; 314f76fcf6dSJeffrey Hsu struct inpcb *inp; 3152c37256eSGarrett Wollman struct tcpcb *tp; 316f76fcf6dSJeffrey Hsu const int inirw = INI_READ; 3172c37256eSGarrett Wollman 3182c37256eSGarrett Wollman COMMON_START(); 3192c37256eSGarrett Wollman if (inp->inp_lport == 0) 320b40ce416SJulian Elischer error = in_pcbbind(inp, (struct sockaddr *)0, td); 3212c37256eSGarrett Wollman if (error == 0) 3222c37256eSGarrett Wollman tp->t_state = TCPS_LISTEN; 3232c37256eSGarrett Wollman COMMON_END(PRU_LISTEN); 3242c37256eSGarrett Wollman } 3252c37256eSGarrett Wollman 326fb59c426SYoshinobu Inoue #ifdef INET6 327fb59c426SYoshinobu Inoue static int 328b40ce416SJulian Elischer tcp6_usr_listen(struct socket *so, struct thread *td) 329fb59c426SYoshinobu Inoue { 330fb59c426SYoshinobu Inoue int s = splnet(); 331fb59c426SYoshinobu Inoue int error = 0; 332f76fcf6dSJeffrey Hsu struct inpcb *inp; 333fb59c426SYoshinobu Inoue struct tcpcb *tp; 334f76fcf6dSJeffrey Hsu const int inirw = INI_READ; 335fb59c426SYoshinobu Inoue 336fb59c426SYoshinobu Inoue COMMON_START(); 337fb59c426SYoshinobu Inoue if (inp->inp_lport == 0) { 338fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 33933841545SHajimu UMEMOTO if (ip6_mapped_addr_on && 34033841545SHajimu UMEMOTO (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 341fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 342b40ce416SJulian Elischer error = in6_pcbbind(inp, (struct sockaddr *)0, td); 343fb59c426SYoshinobu Inoue } 344fb59c426SYoshinobu Inoue if (error == 0) 345fb59c426SYoshinobu Inoue tp->t_state = TCPS_LISTEN; 346fb59c426SYoshinobu Inoue COMMON_END(PRU_LISTEN); 347fb59c426SYoshinobu Inoue } 348fb59c426SYoshinobu Inoue #endif /* INET6 */ 349fb59c426SYoshinobu Inoue 3502c37256eSGarrett Wollman /* 3512c37256eSGarrett Wollman * Initiate connection to peer. 3522c37256eSGarrett Wollman * Create a template for use in transmissions on this connection. 3532c37256eSGarrett Wollman * Enter SYN_SENT state, and mark socket as connecting. 3542c37256eSGarrett Wollman * Start keep-alive timer, and seed output sequence space. 3552c37256eSGarrett Wollman * Send initial segment on connection. 3562c37256eSGarrett Wollman */ 3572c37256eSGarrett Wollman static int 358b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 3592c37256eSGarrett Wollman { 3602c37256eSGarrett Wollman int s = splnet(); 3612c37256eSGarrett Wollman int error = 0; 362f76fcf6dSJeffrey Hsu struct inpcb *inp; 3632c37256eSGarrett Wollman struct tcpcb *tp; 3642c37256eSGarrett Wollman struct sockaddr_in *sinp; 365f76fcf6dSJeffrey Hsu const int inirw = INI_WRITE; 3662c37256eSGarrett Wollman 3672c37256eSGarrett Wollman COMMON_START(); 3682c37256eSGarrett Wollman 3692c37256eSGarrett Wollman /* 3702c37256eSGarrett Wollman * Must disallow TCP ``connections'' to multicast addresses. 3712c37256eSGarrett Wollman */ 37257bf258eSGarrett Wollman sinp = (struct sockaddr_in *)nam; 3732c37256eSGarrett Wollman if (sinp->sin_family == AF_INET 3742c37256eSGarrett Wollman && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 3752c37256eSGarrett Wollman error = EAFNOSUPPORT; 3762c37256eSGarrett Wollman goto out; 3772c37256eSGarrett Wollman } 3782c37256eSGarrett Wollman 379a854ed98SJohn Baldwin if (td && jailed(td->td_ucred)) 380a854ed98SJohn Baldwin prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr); 38175c13541SPoul-Henning Kamp 382b40ce416SJulian Elischer if ((error = tcp_connect(tp, nam, td)) != 0) 3832c37256eSGarrett Wollman goto out; 3842c37256eSGarrett Wollman error = tcp_output(tp); 3852c37256eSGarrett Wollman COMMON_END(PRU_CONNECT); 3862c37256eSGarrett Wollman } 3872c37256eSGarrett Wollman 388fb59c426SYoshinobu Inoue #ifdef INET6 389fb59c426SYoshinobu Inoue static int 390b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 391fb59c426SYoshinobu Inoue { 392fb59c426SYoshinobu Inoue int s = splnet(); 393fb59c426SYoshinobu Inoue int error = 0; 394f76fcf6dSJeffrey Hsu struct inpcb *inp; 395fb59c426SYoshinobu Inoue struct tcpcb *tp; 396fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 397f76fcf6dSJeffrey Hsu const int inirw = INI_WRITE; 398fb59c426SYoshinobu Inoue 399fb59c426SYoshinobu Inoue COMMON_START(); 400fb59c426SYoshinobu Inoue 401fb59c426SYoshinobu Inoue /* 402fb59c426SYoshinobu Inoue * Must disallow TCP ``connections'' to multicast addresses. 403fb59c426SYoshinobu Inoue */ 404fb59c426SYoshinobu Inoue sin6p = (struct sockaddr_in6 *)nam; 405fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 406fb59c426SYoshinobu Inoue && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) { 407fb59c426SYoshinobu Inoue error = EAFNOSUPPORT; 408fb59c426SYoshinobu Inoue goto out; 409fb59c426SYoshinobu Inoue } 410fb59c426SYoshinobu Inoue 41133841545SHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 412fb59c426SYoshinobu Inoue struct sockaddr_in sin; 413fb59c426SYoshinobu Inoue 41433841545SHajimu UMEMOTO if (!ip6_mapped_addr_on || 41533841545SHajimu UMEMOTO (inp->inp_flags & IN6P_IPV6_V6ONLY)) 41633841545SHajimu UMEMOTO return(EINVAL); 41733841545SHajimu UMEMOTO 418fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 419fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 420fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 421b40ce416SJulian Elischer if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 422fb59c426SYoshinobu Inoue goto out; 423fb59c426SYoshinobu Inoue error = tcp_output(tp); 424fb59c426SYoshinobu Inoue goto out; 425fb59c426SYoshinobu Inoue } 426fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 427fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 428b7d6d952SHajimu UMEMOTO inp->inp_inc.inc_isipv6 = 1; 429b40ce416SJulian Elischer if ((error = tcp6_connect(tp, nam, td)) != 0) 430fb59c426SYoshinobu Inoue goto out; 431fb59c426SYoshinobu Inoue error = tcp_output(tp); 432fb59c426SYoshinobu Inoue COMMON_END(PRU_CONNECT); 433fb59c426SYoshinobu Inoue } 434fb59c426SYoshinobu Inoue #endif /* INET6 */ 435fb59c426SYoshinobu Inoue 4362c37256eSGarrett Wollman /* 4372c37256eSGarrett Wollman * Initiate disconnect from peer. 4382c37256eSGarrett Wollman * If connection never passed embryonic stage, just drop; 4392c37256eSGarrett Wollman * else if don't need to let data drain, then can just drop anyways, 4402c37256eSGarrett Wollman * else have to begin TCP shutdown process: mark socket disconnecting, 4412c37256eSGarrett Wollman * drain unread data, state switch to reflect user close, and 4422c37256eSGarrett Wollman * send segment (e.g. FIN) to peer. Socket will be really disconnected 4432c37256eSGarrett Wollman * when peer sends FIN and acks ours. 4442c37256eSGarrett Wollman * 4452c37256eSGarrett Wollman * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 4462c37256eSGarrett Wollman */ 4472c37256eSGarrett Wollman static int 4482c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so) 4492c37256eSGarrett Wollman { 4502c37256eSGarrett Wollman int s = splnet(); 4512c37256eSGarrett Wollman int error = 0; 452f76fcf6dSJeffrey Hsu struct inpcb *inp; 4532c37256eSGarrett Wollman struct tcpcb *tp; 454f76fcf6dSJeffrey Hsu const int inirw = INI_WRITE; 4552c37256eSGarrett Wollman 4562c37256eSGarrett Wollman COMMON_START(); 4572c37256eSGarrett Wollman tp = tcp_disconnect(tp); 4582c37256eSGarrett Wollman COMMON_END(PRU_DISCONNECT); 4592c37256eSGarrett Wollman } 4602c37256eSGarrett Wollman 4612c37256eSGarrett Wollman /* 4622c37256eSGarrett Wollman * Accept a connection. Essentially all the work is 4632c37256eSGarrett Wollman * done at higher levels; just return the address 4642c37256eSGarrett Wollman * of the peer, storing through addr. 4652c37256eSGarrett Wollman */ 4662c37256eSGarrett Wollman static int 46757bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam) 4682c37256eSGarrett Wollman { 469f76fcf6dSJeffrey Hsu int s; 4702c37256eSGarrett Wollman int error = 0; 471f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 4721db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 473f76fcf6dSJeffrey Hsu struct sockaddr_in *sin; 474f76fcf6dSJeffrey Hsu const int inirw = INI_READ; 4751db24ffbSJonathan Lemon TCPDEBUG0; 4762c37256eSGarrett Wollman 477c0647e0dSJonathan Lemon if (so->so_state & SS_ISDISCONNECTED) { 478c0647e0dSJonathan Lemon error = ECONNABORTED; 479c0647e0dSJonathan Lemon goto out; 480c0647e0dSJonathan Lemon } 481f76fcf6dSJeffrey Hsu 482f76fcf6dSJeffrey Hsu /* 483f76fcf6dSJeffrey Hsu * Do the malloc first in case it blocks. 484f76fcf6dSJeffrey Hsu */ 485f76fcf6dSJeffrey Hsu MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, 486f76fcf6dSJeffrey Hsu M_WAITOK | M_ZERO); 487f76fcf6dSJeffrey Hsu sin->sin_family = AF_INET; 488f76fcf6dSJeffrey Hsu sin->sin_len = sizeof(*sin); 489f76fcf6dSJeffrey Hsu 490f76fcf6dSJeffrey Hsu s = splnet(); 491f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(&tcbinfo); 492f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 493f76fcf6dSJeffrey Hsu if (!inp) { 494f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&tcbinfo); 4951db24ffbSJonathan Lemon splx(s); 496f76fcf6dSJeffrey Hsu free(sin, M_SONAME); 4971db24ffbSJonathan Lemon return (EINVAL); 4981db24ffbSJonathan Lemon } 499f76fcf6dSJeffrey Hsu INP_LOCK(inp); 500f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&tcbinfo); 5011db24ffbSJonathan Lemon tp = intotcpcb(inp); 5021db24ffbSJonathan Lemon TCPDEBUG1(); 503f76fcf6dSJeffrey Hsu 504f76fcf6dSJeffrey Hsu /* 505f76fcf6dSJeffrey Hsu * We inline in_setpeeraddr here, because we have already done 506f76fcf6dSJeffrey Hsu * the locking and the malloc. 507f76fcf6dSJeffrey Hsu */ 508f76fcf6dSJeffrey Hsu sin->sin_port = inp->inp_fport; 509f76fcf6dSJeffrey Hsu sin->sin_addr = inp->inp_faddr; 510f76fcf6dSJeffrey Hsu *nam = (struct sockaddr *)sin; 511f76fcf6dSJeffrey Hsu 5122c37256eSGarrett Wollman COMMON_END(PRU_ACCEPT); 5132c37256eSGarrett Wollman } 5142c37256eSGarrett Wollman 515fb59c426SYoshinobu Inoue #ifdef INET6 516fb59c426SYoshinobu Inoue static int 517fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 518fb59c426SYoshinobu Inoue { 519f76fcf6dSJeffrey Hsu int s; 520f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 521fb59c426SYoshinobu Inoue int error = 0; 5221db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 523f76fcf6dSJeffrey Hsu const int inirw = INI_READ; 5241db24ffbSJonathan Lemon TCPDEBUG0; 525fb59c426SYoshinobu Inoue 526c0647e0dSJonathan Lemon if (so->so_state & SS_ISDISCONNECTED) { 527c0647e0dSJonathan Lemon error = ECONNABORTED; 528c0647e0dSJonathan Lemon goto out; 529c0647e0dSJonathan Lemon } 530f76fcf6dSJeffrey Hsu 531f76fcf6dSJeffrey Hsu s = splnet(); 532f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(&tcbinfo); 533f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 5341db24ffbSJonathan Lemon if (inp == 0) { 535f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&tcbinfo); 5361db24ffbSJonathan Lemon splx(s); 5371db24ffbSJonathan Lemon return (EINVAL); 5381db24ffbSJonathan Lemon } 539f76fcf6dSJeffrey Hsu INP_LOCK(inp); 540f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&tcbinfo); 5411db24ffbSJonathan Lemon tp = intotcpcb(inp); 5421db24ffbSJonathan Lemon TCPDEBUG1(); 543fb59c426SYoshinobu Inoue in6_mapped_peeraddr(so, nam); 544fb59c426SYoshinobu Inoue COMMON_END(PRU_ACCEPT); 545fb59c426SYoshinobu Inoue } 546fb59c426SYoshinobu Inoue #endif /* INET6 */ 547f76fcf6dSJeffrey Hsu 548f76fcf6dSJeffrey Hsu /* 549f76fcf6dSJeffrey Hsu * This is the wrapper function for in_setsockaddr. We just pass down 550f76fcf6dSJeffrey Hsu * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking 551f76fcf6dSJeffrey Hsu * here because in_setsockaddr will call malloc and can block. 552f76fcf6dSJeffrey Hsu */ 553f76fcf6dSJeffrey Hsu static int 554f76fcf6dSJeffrey Hsu tcp_sockaddr(struct socket *so, struct sockaddr **nam) 555f76fcf6dSJeffrey Hsu { 556f76fcf6dSJeffrey Hsu return (in_setsockaddr(so, nam, &tcbinfo)); 557f76fcf6dSJeffrey Hsu } 558f76fcf6dSJeffrey Hsu 559f76fcf6dSJeffrey Hsu /* 560f76fcf6dSJeffrey Hsu * This is the wrapper function for in_setpeeraddr. We just pass down 561f76fcf6dSJeffrey Hsu * the pcbinfo for in_setpeeraddr to lock. 562f76fcf6dSJeffrey Hsu */ 563f76fcf6dSJeffrey Hsu static int 564f76fcf6dSJeffrey Hsu tcp_peeraddr(struct socket *so, struct sockaddr **nam) 565f76fcf6dSJeffrey Hsu { 566f76fcf6dSJeffrey Hsu return (in_setpeeraddr(so, nam, &tcbinfo)); 567f76fcf6dSJeffrey Hsu } 568f76fcf6dSJeffrey Hsu 5692c37256eSGarrett Wollman /* 5702c37256eSGarrett Wollman * Mark the connection as being incapable of further output. 5712c37256eSGarrett Wollman */ 5722c37256eSGarrett Wollman static int 5732c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so) 5742c37256eSGarrett Wollman { 5752c37256eSGarrett Wollman int s = splnet(); 5762c37256eSGarrett Wollman int error = 0; 577f76fcf6dSJeffrey Hsu struct inpcb *inp; 5782c37256eSGarrett Wollman struct tcpcb *tp; 579f76fcf6dSJeffrey Hsu const int inirw = INI_WRITE; 5802c37256eSGarrett Wollman 5812c37256eSGarrett Wollman COMMON_START(); 5822c37256eSGarrett Wollman socantsendmore(so); 5832c37256eSGarrett Wollman tp = tcp_usrclosed(tp); 5842c37256eSGarrett Wollman if (tp) 5852c37256eSGarrett Wollman error = tcp_output(tp); 5862c37256eSGarrett Wollman COMMON_END(PRU_SHUTDOWN); 5872c37256eSGarrett Wollman } 5882c37256eSGarrett Wollman 5892c37256eSGarrett Wollman /* 5902c37256eSGarrett Wollman * After a receive, possibly send window update to peer. 5912c37256eSGarrett Wollman */ 5922c37256eSGarrett Wollman static int 5932c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags) 5942c37256eSGarrett Wollman { 5952c37256eSGarrett Wollman int s = splnet(); 5962c37256eSGarrett Wollman int error = 0; 597f76fcf6dSJeffrey Hsu struct inpcb *inp; 5982c37256eSGarrett Wollman struct tcpcb *tp; 599f76fcf6dSJeffrey Hsu const int inirw = INI_READ; 6002c37256eSGarrett Wollman 6012c37256eSGarrett Wollman COMMON_START(); 6022c37256eSGarrett Wollman tcp_output(tp); 6032c37256eSGarrett Wollman COMMON_END(PRU_RCVD); 6042c37256eSGarrett Wollman } 6052c37256eSGarrett Wollman 6062c37256eSGarrett Wollman /* 6072c37256eSGarrett Wollman * Do a send by putting data in output queue and updating urgent 6089c9906e9SPeter Wemm * marker if URG set. Possibly send more data. Unlike the other 6099c9906e9SPeter Wemm * pru_*() routines, the mbuf chains are our responsibility. We 6109c9906e9SPeter Wemm * must either enqueue them or free them. The other pru_* routines 6119c9906e9SPeter Wemm * generally are caller-frees. 6122c37256eSGarrett Wollman */ 6132c37256eSGarrett Wollman static int 61457bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 615b40ce416SJulian Elischer struct sockaddr *nam, struct mbuf *control, struct thread *td) 6162c37256eSGarrett Wollman { 6172c37256eSGarrett Wollman int s = splnet(); 6182c37256eSGarrett Wollman int error = 0; 619f76fcf6dSJeffrey Hsu struct inpcb *inp; 6202c37256eSGarrett Wollman struct tcpcb *tp; 621f76fcf6dSJeffrey Hsu const int inirw = INI_WRITE; 622fb59c426SYoshinobu Inoue #ifdef INET6 623fb59c426SYoshinobu Inoue int isipv6; 624fb59c426SYoshinobu Inoue #endif 6259c9906e9SPeter Wemm TCPDEBUG0; 6262c37256eSGarrett Wollman 627f76fcf6dSJeffrey Hsu /* 628f76fcf6dSJeffrey Hsu * Need write lock here because this function might call 629f76fcf6dSJeffrey Hsu * tcp_connect or tcp_usrclosed. 630f76fcf6dSJeffrey Hsu * We really want to have to this function upgrade from read lock 631f76fcf6dSJeffrey Hsu * to write lock. XXX 632f76fcf6dSJeffrey Hsu */ 633f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 634f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 6359c9906e9SPeter Wemm if (inp == NULL) { 6369c9906e9SPeter Wemm /* 6379c9906e9SPeter Wemm * OOPS! we lost a race, the TCP session got reset after 6389c9906e9SPeter Wemm * we checked SS_CANTSENDMORE, eg: while doing uiomove or a 6399c9906e9SPeter Wemm * network interrupt in the non-splnet() section of sosend(). 6409c9906e9SPeter Wemm */ 6419c9906e9SPeter Wemm if (m) 6429c9906e9SPeter Wemm m_freem(m); 6439c9906e9SPeter Wemm if (control) 6449c9906e9SPeter Wemm m_freem(control); 6459c9906e9SPeter Wemm error = ECONNRESET; /* XXX EPIPE? */ 64645d3a132SPeter Wemm tp = NULL; 64745d3a132SPeter Wemm TCPDEBUG1(); 6489c9906e9SPeter Wemm goto out; 6499c9906e9SPeter Wemm } 650f76fcf6dSJeffrey Hsu INP_LOCK(inp); 651fb59c426SYoshinobu Inoue #ifdef INET6 652fb59c426SYoshinobu Inoue isipv6 = nam && nam->sa_family == AF_INET6; 653fb59c426SYoshinobu Inoue #endif /* INET6 */ 6549c9906e9SPeter Wemm tp = intotcpcb(inp); 6559c9906e9SPeter Wemm TCPDEBUG1(); 6569c9906e9SPeter Wemm if (control) { 6579c9906e9SPeter Wemm /* TCP doesn't do control messages (rights, creds, etc) */ 6589c9906e9SPeter Wemm if (control->m_len) { 6599c9906e9SPeter Wemm m_freem(control); 6602c37256eSGarrett Wollman if (m) 6612c37256eSGarrett Wollman m_freem(m); 662744f87eaSDavid Greenman error = EINVAL; 663744f87eaSDavid Greenman goto out; 6642c37256eSGarrett Wollman } 6659c9906e9SPeter Wemm m_freem(control); /* empty control, just free it */ 6669c9906e9SPeter Wemm } 6672c37256eSGarrett Wollman if (!(flags & PRUS_OOB)) { 6682c37256eSGarrett Wollman sbappend(&so->so_snd, m); 6692c37256eSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 6702c37256eSGarrett Wollman /* 6712c37256eSGarrett Wollman * Do implied connect if not yet connected, 6722c37256eSGarrett Wollman * initialize window to default value, and 6732c37256eSGarrett Wollman * initialize maxseg/maxopd using peer's cached 6742c37256eSGarrett Wollman * MSS. 6752c37256eSGarrett Wollman */ 676fb59c426SYoshinobu Inoue #ifdef INET6 677fb59c426SYoshinobu Inoue if (isipv6) 678b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 679fb59c426SYoshinobu Inoue else 680fb59c426SYoshinobu Inoue #endif /* INET6 */ 681b40ce416SJulian Elischer error = tcp_connect(tp, nam, td); 6822c37256eSGarrett Wollman if (error) 6832c37256eSGarrett Wollman goto out; 6842c37256eSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 6852c37256eSGarrett Wollman tcp_mss(tp, -1); 6862c37256eSGarrett Wollman } 6872c37256eSGarrett Wollman 6882c37256eSGarrett Wollman if (flags & PRUS_EOF) { 6892c37256eSGarrett Wollman /* 6902c37256eSGarrett Wollman * Close the send side of the connection after 6912c37256eSGarrett Wollman * the data is sent. 6922c37256eSGarrett Wollman */ 6932c37256eSGarrett Wollman socantsendmore(so); 6942c37256eSGarrett Wollman tp = tcp_usrclosed(tp); 6952c37256eSGarrett Wollman } 696b0acefa8SBill Fenner if (tp != NULL) { 697b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 698b0acefa8SBill Fenner tp->t_flags |= TF_MORETOCOME; 6992c37256eSGarrett Wollman error = tcp_output(tp); 700b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 701b0acefa8SBill Fenner tp->t_flags &= ~TF_MORETOCOME; 702b0acefa8SBill Fenner } 7032c37256eSGarrett Wollman } else { 7042c37256eSGarrett Wollman if (sbspace(&so->so_snd) < -512) { 7052c37256eSGarrett Wollman m_freem(m); 7062c37256eSGarrett Wollman error = ENOBUFS; 7072c37256eSGarrett Wollman goto out; 7082c37256eSGarrett Wollman } 7092c37256eSGarrett Wollman /* 7102c37256eSGarrett Wollman * According to RFC961 (Assigned Protocols), 7112c37256eSGarrett Wollman * the urgent pointer points to the last octet 7122c37256eSGarrett Wollman * of urgent data. We continue, however, 7132c37256eSGarrett Wollman * to consider it to indicate the first octet 7142c37256eSGarrett Wollman * of data past the urgent section. 7152c37256eSGarrett Wollman * Otherwise, snd_up should be one lower. 7162c37256eSGarrett Wollman */ 7172c37256eSGarrett Wollman sbappend(&so->so_snd, m); 718ef53690bSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 719ef53690bSGarrett Wollman /* 720ef53690bSGarrett Wollman * Do implied connect if not yet connected, 721ef53690bSGarrett Wollman * initialize window to default value, and 722ef53690bSGarrett Wollman * initialize maxseg/maxopd using peer's cached 723ef53690bSGarrett Wollman * MSS. 724ef53690bSGarrett Wollman */ 725fb59c426SYoshinobu Inoue #ifdef INET6 726fb59c426SYoshinobu Inoue if (isipv6) 727b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 728fb59c426SYoshinobu Inoue else 729fb59c426SYoshinobu Inoue #endif /* INET6 */ 730b40ce416SJulian Elischer error = tcp_connect(tp, nam, td); 731ef53690bSGarrett Wollman if (error) 732ef53690bSGarrett Wollman goto out; 733ef53690bSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 734ef53690bSGarrett Wollman tcp_mss(tp, -1); 735ef53690bSGarrett Wollman } 7362c37256eSGarrett Wollman tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 7372c37256eSGarrett Wollman tp->t_force = 1; 7382c37256eSGarrett Wollman error = tcp_output(tp); 7392c37256eSGarrett Wollman tp->t_force = 0; 7402c37256eSGarrett Wollman } 7412c37256eSGarrett Wollman COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB : 7422c37256eSGarrett Wollman ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 7432c37256eSGarrett Wollman } 7442c37256eSGarrett Wollman 7452c37256eSGarrett Wollman /* 7462c37256eSGarrett Wollman * Abort the TCP. 7472c37256eSGarrett Wollman */ 7482c37256eSGarrett Wollman static int 7492c37256eSGarrett Wollman tcp_usr_abort(struct socket *so) 7502c37256eSGarrett Wollman { 7512c37256eSGarrett Wollman int s = splnet(); 7522c37256eSGarrett Wollman int error = 0; 753f76fcf6dSJeffrey Hsu struct inpcb *inp; 7542c37256eSGarrett Wollman struct tcpcb *tp; 755f76fcf6dSJeffrey Hsu const int inirw = INI_WRITE; 7562c37256eSGarrett Wollman 7572c37256eSGarrett Wollman COMMON_START(); 7582c37256eSGarrett Wollman tp = tcp_drop(tp, ECONNABORTED); 7592c37256eSGarrett Wollman COMMON_END(PRU_ABORT); 7602c37256eSGarrett Wollman } 7612c37256eSGarrett Wollman 7622c37256eSGarrett Wollman /* 7632c37256eSGarrett Wollman * Receive out-of-band data. 7642c37256eSGarrett Wollman */ 7652c37256eSGarrett Wollman static int 7662c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 7672c37256eSGarrett Wollman { 7682c37256eSGarrett Wollman int s = splnet(); 7692c37256eSGarrett Wollman int error = 0; 770f76fcf6dSJeffrey Hsu struct inpcb *inp; 7712c37256eSGarrett Wollman struct tcpcb *tp; 772f76fcf6dSJeffrey Hsu const int inirw = INI_READ; 7732c37256eSGarrett Wollman 7742c37256eSGarrett Wollman COMMON_START(); 7752c37256eSGarrett Wollman if ((so->so_oobmark == 0 && 7762c37256eSGarrett Wollman (so->so_state & SS_RCVATMARK) == 0) || 7774cc20ab1SSeigo Tanimura so->so_options & SO_OOBINLINE || 7784cc20ab1SSeigo Tanimura tp->t_oobflags & TCPOOB_HADDATA) { 7792c37256eSGarrett Wollman error = EINVAL; 7802c37256eSGarrett Wollman goto out; 7812c37256eSGarrett Wollman } 7822c37256eSGarrett Wollman if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 7832c37256eSGarrett Wollman error = EWOULDBLOCK; 7842c37256eSGarrett Wollman goto out; 7852c37256eSGarrett Wollman } 7862c37256eSGarrett Wollman m->m_len = 1; 7872c37256eSGarrett Wollman *mtod(m, caddr_t) = tp->t_iobc; 7882c37256eSGarrett Wollman if ((flags & MSG_PEEK) == 0) 7892c37256eSGarrett Wollman tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 7902c37256eSGarrett Wollman COMMON_END(PRU_RCVOOB); 7912c37256eSGarrett Wollman } 7922c37256eSGarrett Wollman 7932c37256eSGarrett Wollman /* xxx - should be const */ 7942c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = { 7952c37256eSGarrett Wollman tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind, 796117bcae7SGarrett Wollman tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach, 797f76fcf6dSJeffrey Hsu tcp_usr_disconnect, tcp_usr_listen, tcp_peeraddr, tcp_usr_rcvd, 798117bcae7SGarrett Wollman tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown, 799f76fcf6dSJeffrey Hsu tcp_sockaddr, sosend, soreceive, sopoll 8002c37256eSGarrett Wollman }; 801df8bae1dSRodney W. Grimes 802fb59c426SYoshinobu Inoue #ifdef INET6 803fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = { 804fb59c426SYoshinobu Inoue tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind, 805fb59c426SYoshinobu Inoue tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach, 806fb59c426SYoshinobu Inoue tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd, 807fb59c426SYoshinobu Inoue tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown, 808fb59c426SYoshinobu Inoue in6_mapped_sockaddr, sosend, soreceive, sopoll 809fb59c426SYoshinobu Inoue }; 810fb59c426SYoshinobu Inoue #endif /* INET6 */ 811fb59c426SYoshinobu Inoue 812a0292f23SGarrett Wollman /* 813a0292f23SGarrett Wollman * Common subroutine to open a TCP connection to remote host specified 814a0292f23SGarrett Wollman * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 815a0292f23SGarrett Wollman * port number if needed. Call in_pcbladdr to do the routing and to choose 816a0292f23SGarrett Wollman * a local host address (interface). If there is an existing incarnation 817a0292f23SGarrett Wollman * of the same connection in TIME-WAIT state and if the remote host was 818a0292f23SGarrett Wollman * sending CC options and if the connection duration was < MSL, then 819a0292f23SGarrett Wollman * truncate the previous TIME-WAIT state and proceed. 820a0292f23SGarrett Wollman * Initialize connection parameters and enter SYN-SENT state. 821a0292f23SGarrett Wollman */ 8220312fbe9SPoul-Henning Kamp static int 823b40ce416SJulian Elischer tcp_connect(tp, nam, td) 824a0292f23SGarrett Wollman register struct tcpcb *tp; 82557bf258eSGarrett Wollman struct sockaddr *nam; 826b40ce416SJulian Elischer struct thread *td; 827a0292f23SGarrett Wollman { 828a0292f23SGarrett Wollman struct inpcb *inp = tp->t_inpcb, *oinp; 829a0292f23SGarrett Wollman struct socket *so = inp->inp_socket; 830a0292f23SGarrett Wollman struct tcpcb *otp; 83157bf258eSGarrett Wollman struct sockaddr_in *sin = (struct sockaddr_in *)nam; 832a0292f23SGarrett Wollman struct sockaddr_in *ifaddr; 833a45d2726SAndras Olah struct rmxp_tao *taop; 834a45d2726SAndras Olah struct rmxp_tao tao_noncached; 835c3229e05SDavid Greenman int error; 836a0292f23SGarrett Wollman 837a0292f23SGarrett Wollman if (inp->inp_lport == 0) { 838b40ce416SJulian Elischer error = in_pcbbind(inp, (struct sockaddr *)0, td); 839a0292f23SGarrett Wollman if (error) 840a0292f23SGarrett Wollman return error; 841a0292f23SGarrett Wollman } 842a0292f23SGarrett Wollman 843a0292f23SGarrett Wollman /* 844a0292f23SGarrett Wollman * Cannot simply call in_pcbconnect, because there might be an 845a0292f23SGarrett Wollman * earlier incarnation of this same connection still in 846a0292f23SGarrett Wollman * TIME_WAIT state, creating an ADDRINUSE error. 847a0292f23SGarrett Wollman */ 848a0292f23SGarrett Wollman error = in_pcbladdr(inp, nam, &ifaddr); 849d3628763SRodney W. Grimes if (error) 850d3628763SRodney W. Grimes return error; 851c3229e05SDavid Greenman oinp = in_pcblookup_hash(inp->inp_pcbinfo, 852a0292f23SGarrett Wollman sin->sin_addr, sin->sin_port, 853a0292f23SGarrett Wollman inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr 854a0292f23SGarrett Wollman : ifaddr->sin_addr, 855cfa1ca9dSYoshinobu Inoue inp->inp_lport, 0, NULL); 856a0292f23SGarrett Wollman if (oinp) { 857a0292f23SGarrett Wollman if (oinp != inp && (otp = intotcpcb(oinp)) != NULL && 858a0292f23SGarrett Wollman otp->t_state == TCPS_TIME_WAIT && 8599b8b58e0SJonathan Lemon (ticks - otp->t_starttime) < tcp_msl && 860a0292f23SGarrett Wollman (otp->t_flags & TF_RCVD_CC)) 861a0292f23SGarrett Wollman otp = tcp_close(otp); 862a0292f23SGarrett Wollman else 863a0292f23SGarrett Wollman return EADDRINUSE; 864a0292f23SGarrett Wollman } 865a0292f23SGarrett Wollman if (inp->inp_laddr.s_addr == INADDR_ANY) 866a0292f23SGarrett Wollman inp->inp_laddr = ifaddr->sin_addr; 867a0292f23SGarrett Wollman inp->inp_faddr = sin->sin_addr; 868a0292f23SGarrett Wollman inp->inp_fport = sin->sin_port; 86915bd2b43SDavid Greenman in_pcbrehash(inp); 870a0292f23SGarrett Wollman 871a0292f23SGarrett Wollman /* Compute window scaling to request. */ 872a0292f23SGarrett Wollman while (tp->request_r_scale < TCP_MAX_WINSHIFT && 873a0292f23SGarrett Wollman (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 874a0292f23SGarrett Wollman tp->request_r_scale++; 875a0292f23SGarrett Wollman 876a0292f23SGarrett Wollman soisconnecting(so); 877a0292f23SGarrett Wollman tcpstat.tcps_connattempt++; 878a0292f23SGarrett Wollman tp->t_state = TCPS_SYN_SENT; 8799b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 880b0e3ad75SMike Silbersack tp->iss = tcp_new_isn(tp); 881a0292f23SGarrett Wollman tcp_sendseqinit(tp); 882a45d2726SAndras Olah 883a45d2726SAndras Olah /* 884a45d2726SAndras Olah * Generate a CC value for this connection and 885a45d2726SAndras Olah * check whether CC or CCnew should be used. 886a45d2726SAndras Olah */ 887be2ac88cSJonathan Lemon if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) { 888a45d2726SAndras Olah taop = &tao_noncached; 889a45d2726SAndras Olah bzero(taop, sizeof(*taop)); 890a45d2726SAndras Olah } 891a45d2726SAndras Olah 892a0292f23SGarrett Wollman tp->cc_send = CC_INC(tcp_ccgen); 893a45d2726SAndras Olah if (taop->tao_ccsent != 0 && 894a45d2726SAndras Olah CC_GEQ(tp->cc_send, taop->tao_ccsent)) { 895a45d2726SAndras Olah taop->tao_ccsent = tp->cc_send; 896a45d2726SAndras Olah } else { 897a45d2726SAndras Olah taop->tao_ccsent = 0; 898a45d2726SAndras Olah tp->t_flags |= TF_SENDCCNEW; 899a45d2726SAndras Olah } 900a0292f23SGarrett Wollman 901a0292f23SGarrett Wollman return 0; 902a0292f23SGarrett Wollman } 903a0292f23SGarrett Wollman 904fb59c426SYoshinobu Inoue #ifdef INET6 905fb59c426SYoshinobu Inoue static int 906b40ce416SJulian Elischer tcp6_connect(tp, nam, td) 907fb59c426SYoshinobu Inoue register struct tcpcb *tp; 908fb59c426SYoshinobu Inoue struct sockaddr *nam; 909b40ce416SJulian Elischer struct thread *td; 910fb59c426SYoshinobu Inoue { 911fb59c426SYoshinobu Inoue struct inpcb *inp = tp->t_inpcb, *oinp; 912fb59c426SYoshinobu Inoue struct socket *so = inp->inp_socket; 913fb59c426SYoshinobu Inoue struct tcpcb *otp; 914fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 915fb59c426SYoshinobu Inoue struct in6_addr *addr6; 916fb59c426SYoshinobu Inoue struct rmxp_tao *taop; 917fb59c426SYoshinobu Inoue struct rmxp_tao tao_noncached; 918fb59c426SYoshinobu Inoue int error; 919fb59c426SYoshinobu Inoue 920fb59c426SYoshinobu Inoue if (inp->inp_lport == 0) { 921b40ce416SJulian Elischer error = in6_pcbbind(inp, (struct sockaddr *)0, td); 922fb59c426SYoshinobu Inoue if (error) 923fb59c426SYoshinobu Inoue return error; 924fb59c426SYoshinobu Inoue } 925fb59c426SYoshinobu Inoue 926fb59c426SYoshinobu Inoue /* 927fb59c426SYoshinobu Inoue * Cannot simply call in_pcbconnect, because there might be an 928fb59c426SYoshinobu Inoue * earlier incarnation of this same connection still in 929fb59c426SYoshinobu Inoue * TIME_WAIT state, creating an ADDRINUSE error. 930fb59c426SYoshinobu Inoue */ 931fb59c426SYoshinobu Inoue error = in6_pcbladdr(inp, nam, &addr6); 932fb59c426SYoshinobu Inoue if (error) 933fb59c426SYoshinobu Inoue return error; 934fb59c426SYoshinobu Inoue oinp = in6_pcblookup_hash(inp->inp_pcbinfo, 935fb59c426SYoshinobu Inoue &sin6->sin6_addr, sin6->sin6_port, 936fb59c426SYoshinobu Inoue IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 937fb59c426SYoshinobu Inoue ? addr6 938fb59c426SYoshinobu Inoue : &inp->in6p_laddr, 939fb59c426SYoshinobu Inoue inp->inp_lport, 0, NULL); 940fb59c426SYoshinobu Inoue if (oinp) { 941fb59c426SYoshinobu Inoue if (oinp != inp && (otp = intotcpcb(oinp)) != NULL && 942fb59c426SYoshinobu Inoue otp->t_state == TCPS_TIME_WAIT && 943fb59c426SYoshinobu Inoue (ticks - otp->t_starttime) < tcp_msl && 944fb59c426SYoshinobu Inoue (otp->t_flags & TF_RCVD_CC)) 945fb59c426SYoshinobu Inoue otp = tcp_close(otp); 946fb59c426SYoshinobu Inoue else 947fb59c426SYoshinobu Inoue return EADDRINUSE; 948fb59c426SYoshinobu Inoue } 949fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 950fb59c426SYoshinobu Inoue inp->in6p_laddr = *addr6; 951fb59c426SYoshinobu Inoue inp->in6p_faddr = sin6->sin6_addr; 952fb59c426SYoshinobu Inoue inp->inp_fport = sin6->sin6_port; 953fb59c426SYoshinobu Inoue if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL) 954fb59c426SYoshinobu Inoue inp->in6p_flowinfo = sin6->sin6_flowinfo; 955fb59c426SYoshinobu Inoue in_pcbrehash(inp); 956fb59c426SYoshinobu Inoue 957fb59c426SYoshinobu Inoue /* Compute window scaling to request. */ 958fb59c426SYoshinobu Inoue while (tp->request_r_scale < TCP_MAX_WINSHIFT && 959fb59c426SYoshinobu Inoue (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 960fb59c426SYoshinobu Inoue tp->request_r_scale++; 961fb59c426SYoshinobu Inoue 962fb59c426SYoshinobu Inoue soisconnecting(so); 963fb59c426SYoshinobu Inoue tcpstat.tcps_connattempt++; 964fb59c426SYoshinobu Inoue tp->t_state = TCPS_SYN_SENT; 965fb59c426SYoshinobu Inoue callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 966b0e3ad75SMike Silbersack tp->iss = tcp_new_isn(tp); 967fb59c426SYoshinobu Inoue tcp_sendseqinit(tp); 968fb59c426SYoshinobu Inoue 969fb59c426SYoshinobu Inoue /* 970fb59c426SYoshinobu Inoue * Generate a CC value for this connection and 971fb59c426SYoshinobu Inoue * check whether CC or CCnew should be used. 972fb59c426SYoshinobu Inoue */ 973be2ac88cSJonathan Lemon if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) { 974fb59c426SYoshinobu Inoue taop = &tao_noncached; 975fb59c426SYoshinobu Inoue bzero(taop, sizeof(*taop)); 976fb59c426SYoshinobu Inoue } 977fb59c426SYoshinobu Inoue 978fb59c426SYoshinobu Inoue tp->cc_send = CC_INC(tcp_ccgen); 979fb59c426SYoshinobu Inoue if (taop->tao_ccsent != 0 && 980fb59c426SYoshinobu Inoue CC_GEQ(tp->cc_send, taop->tao_ccsent)) { 981fb59c426SYoshinobu Inoue taop->tao_ccsent = tp->cc_send; 982fb59c426SYoshinobu Inoue } else { 983fb59c426SYoshinobu Inoue taop->tao_ccsent = 0; 984fb59c426SYoshinobu Inoue tp->t_flags |= TF_SENDCCNEW; 985fb59c426SYoshinobu Inoue } 986fb59c426SYoshinobu Inoue 987fb59c426SYoshinobu Inoue return 0; 988fb59c426SYoshinobu Inoue } 989fb59c426SYoshinobu Inoue #endif /* INET6 */ 990fb59c426SYoshinobu Inoue 991cfe8b629SGarrett Wollman /* 992cfe8b629SGarrett Wollman * The new sockopt interface makes it possible for us to block in the 993cfe8b629SGarrett Wollman * copyin/out step (if we take a page fault). Taking a page fault at 994cfe8b629SGarrett Wollman * splnet() is probably a Bad Thing. (Since sockets and pcbs both now 995cfe8b629SGarrett Wollman * use TSM, there probably isn't any need for this function to run at 996cfe8b629SGarrett Wollman * splnet() any more. This needs more examination.) 997cfe8b629SGarrett Wollman */ 998df8bae1dSRodney W. Grimes int 999cfe8b629SGarrett Wollman tcp_ctloutput(so, sopt) 1000df8bae1dSRodney W. Grimes struct socket *so; 1001cfe8b629SGarrett Wollman struct sockopt *sopt; 1002df8bae1dSRodney W. Grimes { 1003cfe8b629SGarrett Wollman int error, opt, optval, s; 1004df8bae1dSRodney W. Grimes struct inpcb *inp; 1005cfe8b629SGarrett Wollman struct tcpcb *tp; 1006df8bae1dSRodney W. Grimes 1007cfe8b629SGarrett Wollman error = 0; 1008cfe8b629SGarrett Wollman s = splnet(); /* XXX */ 1009f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(&tcbinfo); 1010df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1011df8bae1dSRodney W. Grimes if (inp == NULL) { 1012f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&tcbinfo); 1013df8bae1dSRodney W. Grimes splx(s); 1014df8bae1dSRodney W. Grimes return (ECONNRESET); 1015df8bae1dSRodney W. Grimes } 1016f76fcf6dSJeffrey Hsu INP_LOCK(inp); 1017f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&tcbinfo); 1018cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_TCP) { 1019fb59c426SYoshinobu Inoue #ifdef INET6 1020fb59c426SYoshinobu Inoue if (INP_CHECK_SOCKAF(so, AF_INET6)) 1021fb59c426SYoshinobu Inoue error = ip6_ctloutput(so, sopt); 1022fb59c426SYoshinobu Inoue else 1023fb59c426SYoshinobu Inoue #endif /* INET6 */ 1024cfe8b629SGarrett Wollman error = ip_ctloutput(so, sopt); 1025f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 1026df8bae1dSRodney W. Grimes splx(s); 1027df8bae1dSRodney W. Grimes return (error); 1028df8bae1dSRodney W. Grimes } 1029df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 1030df8bae1dSRodney W. Grimes 1031cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 1032cfe8b629SGarrett Wollman case SOPT_SET: 1033cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1034df8bae1dSRodney W. Grimes case TCP_NODELAY: 1035cfe8b629SGarrett Wollman case TCP_NOOPT: 1036cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1037cfe8b629SGarrett Wollman sizeof optval); 1038cfe8b629SGarrett Wollman if (error) 1039cfe8b629SGarrett Wollman break; 1040cfe8b629SGarrett Wollman 1041cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1042cfe8b629SGarrett Wollman case TCP_NODELAY: 1043cfe8b629SGarrett Wollman opt = TF_NODELAY; 1044cfe8b629SGarrett Wollman break; 1045cfe8b629SGarrett Wollman case TCP_NOOPT: 1046cfe8b629SGarrett Wollman opt = TF_NOOPT; 1047cfe8b629SGarrett Wollman break; 1048cfe8b629SGarrett Wollman default: 1049cfe8b629SGarrett Wollman opt = 0; /* dead code to fool gcc */ 1050cfe8b629SGarrett Wollman break; 1051cfe8b629SGarrett Wollman } 1052cfe8b629SGarrett Wollman 1053cfe8b629SGarrett Wollman if (optval) 1054cfe8b629SGarrett Wollman tp->t_flags |= opt; 1055df8bae1dSRodney W. Grimes else 1056cfe8b629SGarrett Wollman tp->t_flags &= ~opt; 1057df8bae1dSRodney W. Grimes break; 1058df8bae1dSRodney W. Grimes 1059007581c0SJonathan Lemon case TCP_NOPUSH: 1060007581c0SJonathan Lemon error = sooptcopyin(sopt, &optval, sizeof optval, 1061007581c0SJonathan Lemon sizeof optval); 1062007581c0SJonathan Lemon if (error) 1063007581c0SJonathan Lemon break; 1064007581c0SJonathan Lemon 1065007581c0SJonathan Lemon if (optval) 1066007581c0SJonathan Lemon tp->t_flags |= TF_NOPUSH; 1067007581c0SJonathan Lemon else { 1068007581c0SJonathan Lemon tp->t_flags &= ~TF_NOPUSH; 1069007581c0SJonathan Lemon error = tcp_output(tp); 1070007581c0SJonathan Lemon } 1071007581c0SJonathan Lemon break; 1072007581c0SJonathan Lemon 1073df8bae1dSRodney W. Grimes case TCP_MAXSEG: 1074cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1075cfe8b629SGarrett Wollman sizeof optval); 1076cfe8b629SGarrett Wollman if (error) 1077df8bae1dSRodney W. Grimes break; 1078df8bae1dSRodney W. Grimes 1079cfe8b629SGarrett Wollman if (optval > 0 && optval <= tp->t_maxseg) 1080cfe8b629SGarrett Wollman tp->t_maxseg = optval; 1081a0292f23SGarrett Wollman else 1082a0292f23SGarrett Wollman error = EINVAL; 1083a0292f23SGarrett Wollman break; 1084a0292f23SGarrett Wollman 1085df8bae1dSRodney W. Grimes default: 1086df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1087df8bae1dSRodney W. Grimes break; 1088df8bae1dSRodney W. Grimes } 1089df8bae1dSRodney W. Grimes break; 1090df8bae1dSRodney W. Grimes 1091cfe8b629SGarrett Wollman case SOPT_GET: 1092cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1093df8bae1dSRodney W. Grimes case TCP_NODELAY: 1094cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NODELAY; 1095df8bae1dSRodney W. Grimes break; 1096df8bae1dSRodney W. Grimes case TCP_MAXSEG: 1097cfe8b629SGarrett Wollman optval = tp->t_maxseg; 1098df8bae1dSRodney W. Grimes break; 1099a0292f23SGarrett Wollman case TCP_NOOPT: 1100cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOOPT; 1101a0292f23SGarrett Wollman break; 1102a0292f23SGarrett Wollman case TCP_NOPUSH: 1103cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOPUSH; 1104a0292f23SGarrett Wollman break; 1105df8bae1dSRodney W. Grimes default: 1106df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1107df8bae1dSRodney W. Grimes break; 1108df8bae1dSRodney W. Grimes } 1109cfe8b629SGarrett Wollman if (error == 0) 1110cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 1111df8bae1dSRodney W. Grimes break; 1112df8bae1dSRodney W. Grimes } 1113f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 1114df8bae1dSRodney W. Grimes splx(s); 1115df8bae1dSRodney W. Grimes return (error); 1116df8bae1dSRodney W. Grimes } 1117df8bae1dSRodney W. Grimes 111826e30fbbSDavid Greenman /* 111926e30fbbSDavid Greenman * tcp_sendspace and tcp_recvspace are the default send and receive window 112026e30fbbSDavid Greenman * sizes, respectively. These are obsolescent (this information should 112126e30fbbSDavid Greenman * be set by the route). 112226e30fbbSDavid Greenman */ 112381e561cdSDavid E. O'Brien u_long tcp_sendspace = 1024*32; 11243d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW, 11253d177f46SBill Fumerola &tcp_sendspace , 0, "Maximum outgoing TCP datagram size"); 112681e561cdSDavid E. O'Brien u_long tcp_recvspace = 1024*64; 11273d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 11283d177f46SBill Fumerola &tcp_recvspace , 0, "Maximum incoming TCP datagram size"); 1129df8bae1dSRodney W. Grimes 1130df8bae1dSRodney W. Grimes /* 1131df8bae1dSRodney W. Grimes * Attach TCP protocol to socket, allocating 1132df8bae1dSRodney W. Grimes * internet protocol control block, tcp control block, 1133df8bae1dSRodney W. Grimes * bufer space, and entering LISTEN state if to accept connections. 1134df8bae1dSRodney W. Grimes */ 11350312fbe9SPoul-Henning Kamp static int 1136b40ce416SJulian Elischer tcp_attach(so, td) 1137df8bae1dSRodney W. Grimes struct socket *so; 1138b40ce416SJulian Elischer struct thread *td; 1139df8bae1dSRodney W. Grimes { 1140df8bae1dSRodney W. Grimes register struct tcpcb *tp; 1141df8bae1dSRodney W. Grimes struct inpcb *inp; 1142df8bae1dSRodney W. Grimes int error; 1143fb59c426SYoshinobu Inoue #ifdef INET6 1144fb59c426SYoshinobu Inoue int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL; 1145fb59c426SYoshinobu Inoue #endif 1146df8bae1dSRodney W. Grimes 1147df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1148df8bae1dSRodney W. Grimes error = soreserve(so, tcp_sendspace, tcp_recvspace); 1149df8bae1dSRodney W. Grimes if (error) 1150df8bae1dSRodney W. Grimes return (error); 1151df8bae1dSRodney W. Grimes } 1152b40ce416SJulian Elischer error = in_pcballoc(so, &tcbinfo, td); 1153df8bae1dSRodney W. Grimes if (error) 1154df8bae1dSRodney W. Grimes return (error); 1155df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1156fb59c426SYoshinobu Inoue #ifdef INET6 1157fb59c426SYoshinobu Inoue if (isipv6) { 1158fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 1159fb59c426SYoshinobu Inoue inp->in6p_hops = -1; /* use kernel default */ 1160fb59c426SYoshinobu Inoue } 1161fb59c426SYoshinobu Inoue else 1162fb59c426SYoshinobu Inoue #endif 1163cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 1164df8bae1dSRodney W. Grimes tp = tcp_newtcpcb(inp); 1165df8bae1dSRodney W. Grimes if (tp == 0) { 11664cc20ab1SSeigo Tanimura int nofd = so->so_state & SS_NOFDREF; /* XXX */ 1167df8bae1dSRodney W. Grimes 1168df8bae1dSRodney W. Grimes so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 1169fb59c426SYoshinobu Inoue #ifdef INET6 1170fb59c426SYoshinobu Inoue if (isipv6) 1171fb59c426SYoshinobu Inoue in6_pcbdetach(inp); 1172fb59c426SYoshinobu Inoue else 1173fb59c426SYoshinobu Inoue #endif 1174df8bae1dSRodney W. Grimes in_pcbdetach(inp); 1175df8bae1dSRodney W. Grimes so->so_state |= nofd; 1176df8bae1dSRodney W. Grimes return (ENOBUFS); 1177df8bae1dSRodney W. Grimes } 1178df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 1179df8bae1dSRodney W. Grimes return (0); 1180df8bae1dSRodney W. Grimes } 1181df8bae1dSRodney W. Grimes 1182df8bae1dSRodney W. Grimes /* 1183df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 1184df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 1185df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 1186df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 1187df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 1188df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 1189df8bae1dSRodney W. Grimes */ 11900312fbe9SPoul-Henning Kamp static struct tcpcb * 1191df8bae1dSRodney W. Grimes tcp_disconnect(tp) 1192df8bae1dSRodney W. Grimes register struct tcpcb *tp; 1193df8bae1dSRodney W. Grimes { 1194df8bae1dSRodney W. Grimes struct socket *so = tp->t_inpcb->inp_socket; 1195df8bae1dSRodney W. Grimes 1196df8bae1dSRodney W. Grimes if (tp->t_state < TCPS_ESTABLISHED) 1197df8bae1dSRodney W. Grimes tp = tcp_close(tp); 11984cc20ab1SSeigo Tanimura else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1199243917feSSeigo Tanimura tp = tcp_drop(tp, 0); 12004cc20ab1SSeigo Tanimura else { 1201df8bae1dSRodney W. Grimes soisdisconnecting(so); 1202df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 1203df8bae1dSRodney W. Grimes tp = tcp_usrclosed(tp); 1204df8bae1dSRodney W. Grimes if (tp) 1205df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1206df8bae1dSRodney W. Grimes } 1207df8bae1dSRodney W. Grimes return (tp); 1208df8bae1dSRodney W. Grimes } 1209df8bae1dSRodney W. Grimes 1210df8bae1dSRodney W. Grimes /* 1211df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 1212df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 1213df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1214df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 1215df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 1216df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 1217df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 1218df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 1219df8bae1dSRodney W. Grimes */ 12200312fbe9SPoul-Henning Kamp static struct tcpcb * 1221df8bae1dSRodney W. Grimes tcp_usrclosed(tp) 1222df8bae1dSRodney W. Grimes register struct tcpcb *tp; 1223df8bae1dSRodney W. Grimes { 1224df8bae1dSRodney W. Grimes 1225df8bae1dSRodney W. Grimes switch (tp->t_state) { 1226df8bae1dSRodney W. Grimes 1227df8bae1dSRodney W. Grimes case TCPS_CLOSED: 1228df8bae1dSRodney W. Grimes case TCPS_LISTEN: 1229df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 1230df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1231df8bae1dSRodney W. Grimes break; 1232df8bae1dSRodney W. Grimes 1233a0292f23SGarrett Wollman case TCPS_SYN_SENT: 1234df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1235a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 1236a0292f23SGarrett Wollman break; 1237a0292f23SGarrett Wollman 1238df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1239df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_1; 1240df8bae1dSRodney W. Grimes break; 1241df8bae1dSRodney W. Grimes 1242df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1243df8bae1dSRodney W. Grimes tp->t_state = TCPS_LAST_ACK; 1244df8bae1dSRodney W. Grimes break; 1245df8bae1dSRodney W. Grimes } 1246b6239c4aSAndras Olah if (tp && tp->t_state >= TCPS_FIN_WAIT_2) { 1247df8bae1dSRodney W. Grimes soisdisconnected(tp->t_inpcb->inp_socket); 1248b6239c4aSAndras Olah /* To prevent the connection hanging in FIN_WAIT_2 forever. */ 1249b6239c4aSAndras Olah if (tp->t_state == TCPS_FIN_WAIT_2) 12509b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, tcp_maxidle, 12519b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 1252b6239c4aSAndras Olah } 1253df8bae1dSRodney W. Grimes return (tp); 1254df8bae1dSRodney W. Grimes } 1255a0292f23SGarrett Wollman 1256