1c398230bSWarner Losh /*- 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 * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 291fdbc7aeSGarrett Wollman * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94 30c3aac50fSPeter Wemm * $FreeBSD$ 31df8bae1dSRodney W. Grimes */ 32df8bae1dSRodney W. Grimes 336a800098SYoshinobu Inoue #include "opt_ipsec.h" 341cfd4b53SBruce M Simpson #include "opt_inet.h" 35fb59c426SYoshinobu Inoue #include "opt_inet6.h" 360cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 370cc12cc5SJoerg Wunsch 38df8bae1dSRodney W. Grimes #include <sys/param.h> 39df8bae1dSRodney W. Grimes #include <sys/systm.h> 40f76fcf6dSJeffrey Hsu #include <sys/malloc.h> 41c7a82f90SGarrett Wollman #include <sys/kernel.h> 4298163b98SPoul-Henning Kamp #include <sys/sysctl.h> 43df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 44fb59c426SYoshinobu Inoue #ifdef INET6 45fb59c426SYoshinobu Inoue #include <sys/domain.h> 46fb59c426SYoshinobu Inoue #endif /* INET6 */ 47df8bae1dSRodney W. Grimes #include <sys/socket.h> 48df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 49df8bae1dSRodney W. Grimes #include <sys/protosw.h> 5091421ba2SRobert Watson #include <sys/proc.h> 5191421ba2SRobert Watson #include <sys/jail.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 8956dc72c3SPawel Jakub Dawidek static int tcp_attach(struct socket *); 904d77a549SAlfred Perlstein static int tcp_connect(struct tcpcb *, struct sockaddr *, 914d77a549SAlfred Perlstein struct thread *td); 92fb59c426SYoshinobu Inoue #ifdef INET6 934d77a549SAlfred Perlstein static int tcp6_connect(struct tcpcb *, struct sockaddr *, 944d77a549SAlfred Perlstein struct thread *td); 95fb59c426SYoshinobu Inoue #endif /* INET6 */ 960312fbe9SPoul-Henning Kamp static struct tcpcb * 974d77a549SAlfred Perlstein tcp_disconnect(struct tcpcb *); 980312fbe9SPoul-Henning Kamp static struct tcpcb * 994d77a549SAlfred Perlstein tcp_usrclosed(struct tcpcb *); 100b8af5dfaSRobert Watson static void tcp_fill_info(struct tcpcb *, struct tcp_info *); 1012c37256eSGarrett Wollman 1022c37256eSGarrett Wollman #ifdef TCPDEBUG 1031db24ffbSJonathan Lemon #define TCPDEBUG0 int ostate = 0 1042c37256eSGarrett Wollman #define TCPDEBUG1() ostate = tp ? tp->t_state : 0 1054cc20ab1SSeigo Tanimura #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 1064cc20ab1SSeigo Tanimura tcp_trace(TA_USER, ostate, tp, 0, 0, req) 1072c37256eSGarrett Wollman #else 1082c37256eSGarrett Wollman #define TCPDEBUG0 1092c37256eSGarrett Wollman #define TCPDEBUG1() 1102c37256eSGarrett Wollman #define TCPDEBUG2(req) 1112c37256eSGarrett Wollman #endif 1122c37256eSGarrett Wollman 1132c37256eSGarrett Wollman /* 1142c37256eSGarrett Wollman * TCP attaches to socket via pru_attach(), reserving space, 1152c37256eSGarrett Wollman * and an internet control block. 1162c37256eSGarrett Wollman */ 1172c37256eSGarrett Wollman static int 118b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td) 1192c37256eSGarrett Wollman { 1202c37256eSGarrett Wollman int error; 121f76fcf6dSJeffrey Hsu struct inpcb *inp; 1222c37256eSGarrett Wollman struct tcpcb *tp = 0; 1232c37256eSGarrett Wollman TCPDEBUG0; 1242c37256eSGarrett Wollman 125f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 1262c37256eSGarrett Wollman TCPDEBUG1(); 127f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 1282c37256eSGarrett Wollman if (inp) { 1292c37256eSGarrett Wollman error = EISCONN; 1302c37256eSGarrett Wollman goto out; 1312c37256eSGarrett Wollman } 1322c37256eSGarrett Wollman 13356dc72c3SPawel Jakub Dawidek error = tcp_attach(so); 1342c37256eSGarrett Wollman if (error) 1352c37256eSGarrett Wollman goto out; 1362c37256eSGarrett Wollman 1372c37256eSGarrett Wollman if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1383879597fSAndrey A. Chernov so->so_linger = TCP_LINGERTIME; 139f76fcf6dSJeffrey Hsu 140f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 141f76fcf6dSJeffrey Hsu tp = intotcpcb(inp); 1422c37256eSGarrett Wollman out: 1432c37256eSGarrett Wollman TCPDEBUG2(PRU_ATTACH); 144f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 1452c37256eSGarrett Wollman return error; 1462c37256eSGarrett Wollman } 1472c37256eSGarrett Wollman 1482c37256eSGarrett Wollman /* 1492c37256eSGarrett Wollman * pru_detach() detaches the TCP protocol from the socket. 1502c37256eSGarrett Wollman * If the protocol state is non-embryonic, then can't 1512c37256eSGarrett Wollman * do this directly: have to initiate a pru_disconnect(), 1522c37256eSGarrett Wollman * which may finish later; embryonic TCB's can just 1532c37256eSGarrett Wollman * be discarded here. 1542c37256eSGarrett Wollman */ 1552c37256eSGarrett Wollman static int 1562c37256eSGarrett Wollman tcp_usr_detach(struct socket *so) 1572c37256eSGarrett Wollman { 1582c37256eSGarrett Wollman int error = 0; 159f76fcf6dSJeffrey Hsu struct inpcb *inp; 1602c37256eSGarrett Wollman struct tcpcb *tp; 1612c37256eSGarrett Wollman TCPDEBUG0; 1622c37256eSGarrett Wollman 163f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 164f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 1650aa8ce50SJohn-Mark Gurney if (inp == NULL) { 166f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 1670aa8ce50SJohn-Mark Gurney return error; 1682c37256eSGarrett Wollman } 169f76fcf6dSJeffrey Hsu INP_LOCK(inp); 1702c37256eSGarrett Wollman tp = intotcpcb(inp); 1712c37256eSGarrett Wollman TCPDEBUG1(); 1722c37256eSGarrett Wollman tp = tcp_disconnect(tp); 1732c37256eSGarrett Wollman 1742c37256eSGarrett Wollman TCPDEBUG2(PRU_DETACH); 175f76fcf6dSJeffrey Hsu if (tp) 176f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 177f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); 1782c37256eSGarrett Wollman return error; 1792c37256eSGarrett Wollman } 1802c37256eSGarrett Wollman 181f76fcf6dSJeffrey Hsu #define INI_NOLOCK 0 182f76fcf6dSJeffrey Hsu #define INI_READ 1 183f76fcf6dSJeffrey Hsu #define INI_WRITE 2 184f76fcf6dSJeffrey Hsu 185f76fcf6dSJeffrey Hsu #define COMMON_START() \ 186f76fcf6dSJeffrey Hsu TCPDEBUG0; \ 1872c37256eSGarrett Wollman do { \ 188f76fcf6dSJeffrey Hsu if (inirw == INI_READ) \ 189f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(&tcbinfo); \ 190f76fcf6dSJeffrey Hsu else if (inirw == INI_WRITE) \ 191f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); \ 192f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); \ 1932c37256eSGarrett Wollman if (inp == 0) { \ 194f76fcf6dSJeffrey Hsu if (inirw == INI_READ) \ 195f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&tcbinfo); \ 196f76fcf6dSJeffrey Hsu else if (inirw == INI_WRITE) \ 197f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); \ 1982c37256eSGarrett Wollman return EINVAL; \ 1992c37256eSGarrett Wollman } \ 200f76fcf6dSJeffrey Hsu INP_LOCK(inp); \ 201f76fcf6dSJeffrey Hsu if (inirw == INI_READ) \ 202f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&tcbinfo); \ 2032c37256eSGarrett Wollman tp = intotcpcb(inp); \ 2042c37256eSGarrett Wollman TCPDEBUG1(); \ 2052c37256eSGarrett Wollman } while(0) 2062c37256eSGarrett Wollman 207f76fcf6dSJeffrey Hsu #define COMMON_END(req) \ 208f76fcf6dSJeffrey Hsu out: TCPDEBUG2(req); \ 209f76fcf6dSJeffrey Hsu do { \ 210f76fcf6dSJeffrey Hsu if (tp) \ 211f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); \ 212f76fcf6dSJeffrey Hsu if (inirw == INI_WRITE) \ 213f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&tcbinfo); \ 214f76fcf6dSJeffrey Hsu return error; \ 215f76fcf6dSJeffrey Hsu goto out; \ 216f76fcf6dSJeffrey Hsu } while(0) 2172c37256eSGarrett Wollman 2182c37256eSGarrett Wollman /* 2192c37256eSGarrett Wollman * Give the socket an address. 2202c37256eSGarrett Wollman */ 2212c37256eSGarrett Wollman static int 222b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 2232c37256eSGarrett Wollman { 2242c37256eSGarrett Wollman int error = 0; 225f76fcf6dSJeffrey Hsu struct inpcb *inp; 2262c37256eSGarrett Wollman struct tcpcb *tp; 2272c37256eSGarrett Wollman struct sockaddr_in *sinp; 228edf02ff1SJeffrey Hsu const int inirw = INI_WRITE; 2292c37256eSGarrett Wollman 23052710de1SPawel Jakub Dawidek sinp = (struct sockaddr_in *)nam; 23152710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof (*sinp)) 23252710de1SPawel Jakub Dawidek return (EINVAL); 2332c37256eSGarrett Wollman /* 2342c37256eSGarrett Wollman * Must check for multicast addresses and disallow binding 2352c37256eSGarrett Wollman * to them. 2362c37256eSGarrett Wollman */ 2372c37256eSGarrett Wollman if (sinp->sin_family == AF_INET && 23852710de1SPawel Jakub Dawidek IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 23952710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 24052710de1SPawel Jakub Dawidek 24152710de1SPawel Jakub Dawidek COMMON_START(); 242b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, nam, td->td_ucred); 2432c37256eSGarrett Wollman if (error) 2442c37256eSGarrett Wollman goto out; 2452c37256eSGarrett Wollman COMMON_END(PRU_BIND); 2462c37256eSGarrett Wollman } 2472c37256eSGarrett Wollman 248fb59c426SYoshinobu Inoue #ifdef INET6 249fb59c426SYoshinobu Inoue static int 250b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 251fb59c426SYoshinobu Inoue { 252fb59c426SYoshinobu Inoue int error = 0; 253f76fcf6dSJeffrey Hsu struct inpcb *inp; 254fb59c426SYoshinobu Inoue struct tcpcb *tp; 255fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 256edf02ff1SJeffrey Hsu const int inirw = INI_WRITE; 257fb59c426SYoshinobu Inoue 25852710de1SPawel Jakub Dawidek sin6p = (struct sockaddr_in6 *)nam; 25952710de1SPawel Jakub Dawidek if (nam->sa_len != sizeof (*sin6p)) 26052710de1SPawel Jakub Dawidek return (EINVAL); 261fb59c426SYoshinobu Inoue /* 262fb59c426SYoshinobu Inoue * Must check for multicast addresses and disallow binding 263fb59c426SYoshinobu Inoue * to them. 264fb59c426SYoshinobu Inoue */ 265fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 && 26652710de1SPawel Jakub Dawidek IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 26752710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 26852710de1SPawel Jakub Dawidek 26952710de1SPawel Jakub Dawidek COMMON_START(); 270fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 271fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 27266ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 273fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) 274fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 275fb59c426SYoshinobu Inoue else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 276fb59c426SYoshinobu Inoue struct sockaddr_in sin; 277fb59c426SYoshinobu Inoue 278fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 279fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 280fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 281b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)&sin, 282b0330ed9SPawel Jakub Dawidek td->td_ucred); 283fb59c426SYoshinobu Inoue goto out; 284fb59c426SYoshinobu Inoue } 285fb59c426SYoshinobu Inoue } 286b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, nam, td->td_ucred); 287fb59c426SYoshinobu Inoue if (error) 288fb59c426SYoshinobu Inoue goto out; 289fb59c426SYoshinobu Inoue COMMON_END(PRU_BIND); 290fb59c426SYoshinobu Inoue } 291fb59c426SYoshinobu Inoue #endif /* INET6 */ 292fb59c426SYoshinobu Inoue 2932c37256eSGarrett Wollman /* 2942c37256eSGarrett Wollman * Prepare to accept connections. 2952c37256eSGarrett Wollman */ 2962c37256eSGarrett Wollman static int 297b40ce416SJulian Elischer tcp_usr_listen(struct socket *so, struct thread *td) 2982c37256eSGarrett Wollman { 2992c37256eSGarrett Wollman int error = 0; 300f76fcf6dSJeffrey Hsu struct inpcb *inp; 3012c37256eSGarrett Wollman struct tcpcb *tp; 302edf02ff1SJeffrey Hsu const int inirw = INI_WRITE; 3032c37256eSGarrett Wollman 3042c37256eSGarrett Wollman COMMON_START(); 3050daccb9cSRobert Watson SOCK_LOCK(so); 3060daccb9cSRobert Watson error = solisten_proto_check(so); 3070daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) 308b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 3090daccb9cSRobert Watson if (error == 0) { 3102c37256eSGarrett Wollman tp->t_state = TCPS_LISTEN; 3110daccb9cSRobert Watson solisten_proto(so); 3120daccb9cSRobert Watson } 3130daccb9cSRobert Watson SOCK_UNLOCK(so); 3142c37256eSGarrett Wollman COMMON_END(PRU_LISTEN); 3152c37256eSGarrett Wollman } 3162c37256eSGarrett Wollman 317fb59c426SYoshinobu Inoue #ifdef INET6 318fb59c426SYoshinobu Inoue static int 319b40ce416SJulian Elischer tcp6_usr_listen(struct socket *so, struct thread *td) 320fb59c426SYoshinobu Inoue { 321fb59c426SYoshinobu Inoue int error = 0; 322f76fcf6dSJeffrey Hsu struct inpcb *inp; 323fb59c426SYoshinobu Inoue struct tcpcb *tp; 324edf02ff1SJeffrey Hsu const int inirw = INI_WRITE; 325fb59c426SYoshinobu Inoue 326fb59c426SYoshinobu Inoue COMMON_START(); 3270daccb9cSRobert Watson SOCK_LOCK(so); 3280daccb9cSRobert Watson error = solisten_proto_check(so); 3290daccb9cSRobert Watson if (error == 0 && inp->inp_lport == 0) { 330fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 33166ef17c4SHajimu UMEMOTO if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 332fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 333b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 334fb59c426SYoshinobu Inoue } 3350daccb9cSRobert Watson if (error == 0) { 336fb59c426SYoshinobu Inoue tp->t_state = TCPS_LISTEN; 3370daccb9cSRobert Watson solisten_proto(so); 3380daccb9cSRobert Watson } 3390daccb9cSRobert Watson SOCK_UNLOCK(so); 340fb59c426SYoshinobu Inoue COMMON_END(PRU_LISTEN); 341fb59c426SYoshinobu Inoue } 342fb59c426SYoshinobu Inoue #endif /* INET6 */ 343fb59c426SYoshinobu Inoue 3442c37256eSGarrett Wollman /* 3452c37256eSGarrett Wollman * Initiate connection to peer. 3462c37256eSGarrett Wollman * Create a template for use in transmissions on this connection. 3472c37256eSGarrett Wollman * Enter SYN_SENT state, and mark socket as connecting. 3482c37256eSGarrett Wollman * Start keep-alive timer, and seed output sequence space. 3492c37256eSGarrett Wollman * Send initial segment on connection. 3502c37256eSGarrett Wollman */ 3512c37256eSGarrett Wollman static int 352b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 3532c37256eSGarrett Wollman { 3542c37256eSGarrett Wollman int error = 0; 355f76fcf6dSJeffrey Hsu struct inpcb *inp; 3562c37256eSGarrett Wollman struct tcpcb *tp; 3572c37256eSGarrett Wollman struct sockaddr_in *sinp; 358f76fcf6dSJeffrey Hsu const int inirw = INI_WRITE; 3592c37256eSGarrett Wollman 36057bf258eSGarrett Wollman sinp = (struct sockaddr_in *)nam; 361e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sinp)) 362e29ef13fSDon Lewis return (EINVAL); 36352710de1SPawel Jakub Dawidek /* 36452710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 36552710de1SPawel Jakub Dawidek */ 3662c37256eSGarrett Wollman if (sinp->sin_family == AF_INET 36752710de1SPawel Jakub Dawidek && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 36852710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 369812d8653SSam Leffler if (jailed(td->td_ucred)) 370a854ed98SJohn Baldwin prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr); 37175c13541SPoul-Henning Kamp 37252710de1SPawel Jakub Dawidek COMMON_START(); 373b40ce416SJulian Elischer if ((error = tcp_connect(tp, nam, td)) != 0) 3742c37256eSGarrett Wollman goto out; 3752c37256eSGarrett Wollman error = tcp_output(tp); 3762c37256eSGarrett Wollman COMMON_END(PRU_CONNECT); 3772c37256eSGarrett Wollman } 3782c37256eSGarrett Wollman 379fb59c426SYoshinobu Inoue #ifdef INET6 380fb59c426SYoshinobu Inoue static int 381b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 382fb59c426SYoshinobu Inoue { 383fb59c426SYoshinobu Inoue int error = 0; 384f76fcf6dSJeffrey Hsu struct inpcb *inp; 385fb59c426SYoshinobu Inoue struct tcpcb *tp; 386fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6p; 387f76fcf6dSJeffrey Hsu const int inirw = INI_WRITE; 388fb59c426SYoshinobu Inoue 389fb59c426SYoshinobu Inoue sin6p = (struct sockaddr_in6 *)nam; 390e29ef13fSDon Lewis if (nam->sa_len != sizeof (*sin6p)) 391e29ef13fSDon Lewis return (EINVAL); 39252710de1SPawel Jakub Dawidek /* 39352710de1SPawel Jakub Dawidek * Must disallow TCP ``connections'' to multicast addresses. 39452710de1SPawel Jakub Dawidek */ 395fb59c426SYoshinobu Inoue if (sin6p->sin6_family == AF_INET6 39652710de1SPawel Jakub Dawidek && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 39752710de1SPawel Jakub Dawidek return (EAFNOSUPPORT); 398fb59c426SYoshinobu Inoue 39952710de1SPawel Jakub Dawidek COMMON_START(); 40033841545SHajimu UMEMOTO if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 401fb59c426SYoshinobu Inoue struct sockaddr_in sin; 402fb59c426SYoshinobu Inoue 403d46a5312SMaxim Konovalov if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 404d46a5312SMaxim Konovalov error = EINVAL; 405d46a5312SMaxim Konovalov goto out; 406d46a5312SMaxim Konovalov } 40733841545SHajimu UMEMOTO 408fb59c426SYoshinobu Inoue in6_sin6_2_sin(&sin, sin6p); 409fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 410fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV6; 411b40ce416SJulian Elischer if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 412fb59c426SYoshinobu Inoue goto out; 413fb59c426SYoshinobu Inoue error = tcp_output(tp); 414fb59c426SYoshinobu Inoue goto out; 415fb59c426SYoshinobu Inoue } 416fb59c426SYoshinobu Inoue inp->inp_vflag &= ~INP_IPV4; 417fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 418b7d6d952SHajimu UMEMOTO inp->inp_inc.inc_isipv6 = 1; 419b40ce416SJulian Elischer if ((error = tcp6_connect(tp, nam, td)) != 0) 420fb59c426SYoshinobu Inoue goto out; 421fb59c426SYoshinobu Inoue error = tcp_output(tp); 422fb59c426SYoshinobu Inoue COMMON_END(PRU_CONNECT); 423fb59c426SYoshinobu Inoue } 424fb59c426SYoshinobu Inoue #endif /* INET6 */ 425fb59c426SYoshinobu Inoue 4262c37256eSGarrett Wollman /* 4272c37256eSGarrett Wollman * Initiate disconnect from peer. 4282c37256eSGarrett Wollman * If connection never passed embryonic stage, just drop; 4292c37256eSGarrett Wollman * else if don't need to let data drain, then can just drop anyways, 4302c37256eSGarrett Wollman * else have to begin TCP shutdown process: mark socket disconnecting, 4312c37256eSGarrett Wollman * drain unread data, state switch to reflect user close, and 4322c37256eSGarrett Wollman * send segment (e.g. FIN) to peer. Socket will be really disconnected 4332c37256eSGarrett Wollman * when peer sends FIN and acks ours. 4342c37256eSGarrett Wollman * 4352c37256eSGarrett Wollman * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 4362c37256eSGarrett Wollman */ 4372c37256eSGarrett Wollman static int 4382c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so) 4392c37256eSGarrett Wollman { 4402c37256eSGarrett Wollman int error = 0; 441f76fcf6dSJeffrey Hsu struct inpcb *inp; 4422c37256eSGarrett Wollman struct tcpcb *tp; 443f76fcf6dSJeffrey Hsu const int inirw = INI_WRITE; 4442c37256eSGarrett Wollman 4452c37256eSGarrett Wollman COMMON_START(); 4462c37256eSGarrett Wollman tp = tcp_disconnect(tp); 4472c37256eSGarrett Wollman COMMON_END(PRU_DISCONNECT); 4482c37256eSGarrett Wollman } 4492c37256eSGarrett Wollman 4502c37256eSGarrett Wollman /* 4512c37256eSGarrett Wollman * Accept a connection. Essentially all the work is 4522c37256eSGarrett Wollman * done at higher levels; just return the address 4532c37256eSGarrett Wollman * of the peer, storing through addr. 4542c37256eSGarrett Wollman */ 4552c37256eSGarrett Wollman static int 45657bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam) 4572c37256eSGarrett Wollman { 4582c37256eSGarrett Wollman int error = 0; 459f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 4601db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 46126ef6ac4SDon Lewis struct in_addr addr; 46226ef6ac4SDon Lewis in_port_t port = 0; 4631db24ffbSJonathan Lemon TCPDEBUG0; 4642c37256eSGarrett Wollman 465c0647e0dSJonathan Lemon if (so->so_state & SS_ISDISCONNECTED) { 466c0647e0dSJonathan Lemon error = ECONNABORTED; 467c0647e0dSJonathan Lemon goto out; 468c0647e0dSJonathan Lemon } 469f76fcf6dSJeffrey Hsu 470f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(&tcbinfo); 471f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 472f76fcf6dSJeffrey Hsu if (!inp) { 473f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&tcbinfo); 4741db24ffbSJonathan Lemon return (EINVAL); 4751db24ffbSJonathan Lemon } 476f76fcf6dSJeffrey Hsu INP_LOCK(inp); 477f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&tcbinfo); 4781db24ffbSJonathan Lemon tp = intotcpcb(inp); 4791db24ffbSJonathan Lemon TCPDEBUG1(); 480f76fcf6dSJeffrey Hsu 481f76fcf6dSJeffrey Hsu /* 48226ef6ac4SDon Lewis * We inline in_setpeeraddr and COMMON_END here, so that we can 48326ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 48426ef6ac4SDon Lewis * release the lock. 485f76fcf6dSJeffrey Hsu */ 48626ef6ac4SDon Lewis port = inp->inp_fport; 48726ef6ac4SDon Lewis addr = inp->inp_faddr; 488f76fcf6dSJeffrey Hsu 48926ef6ac4SDon Lewis out: TCPDEBUG2(PRU_ACCEPT); 49026ef6ac4SDon Lewis if (tp) 49126ef6ac4SDon Lewis INP_UNLOCK(inp); 49226ef6ac4SDon Lewis if (error == 0) 49326ef6ac4SDon Lewis *nam = in_sockaddr(port, &addr); 49426ef6ac4SDon Lewis return error; 4952c37256eSGarrett Wollman } 4962c37256eSGarrett Wollman 497fb59c426SYoshinobu Inoue #ifdef INET6 498fb59c426SYoshinobu Inoue static int 499fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 500fb59c426SYoshinobu Inoue { 501f76fcf6dSJeffrey Hsu struct inpcb *inp = NULL; 502fb59c426SYoshinobu Inoue int error = 0; 5031db24ffbSJonathan Lemon struct tcpcb *tp = NULL; 50426ef6ac4SDon Lewis struct in_addr addr; 50526ef6ac4SDon Lewis struct in6_addr addr6; 50626ef6ac4SDon Lewis in_port_t port = 0; 50726ef6ac4SDon Lewis int v4 = 0; 5081db24ffbSJonathan Lemon TCPDEBUG0; 509fb59c426SYoshinobu Inoue 510c0647e0dSJonathan Lemon if (so->so_state & SS_ISDISCONNECTED) { 511c0647e0dSJonathan Lemon error = ECONNABORTED; 512c0647e0dSJonathan Lemon goto out; 513c0647e0dSJonathan Lemon } 514f76fcf6dSJeffrey Hsu 515f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(&tcbinfo); 516f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 5171db24ffbSJonathan Lemon if (inp == 0) { 518f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&tcbinfo); 5191db24ffbSJonathan Lemon return (EINVAL); 5201db24ffbSJonathan Lemon } 521f76fcf6dSJeffrey Hsu INP_LOCK(inp); 522f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&tcbinfo); 5231db24ffbSJonathan Lemon tp = intotcpcb(inp); 5241db24ffbSJonathan Lemon TCPDEBUG1(); 52526ef6ac4SDon Lewis /* 52626ef6ac4SDon Lewis * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 52726ef6ac4SDon Lewis * copy the data of interest and defer the malloc until after we 52826ef6ac4SDon Lewis * release the lock. 52926ef6ac4SDon Lewis */ 53026ef6ac4SDon Lewis if (inp->inp_vflag & INP_IPV4) { 53126ef6ac4SDon Lewis v4 = 1; 53226ef6ac4SDon Lewis port = inp->inp_fport; 53326ef6ac4SDon Lewis addr = inp->inp_faddr; 53426ef6ac4SDon Lewis } else { 53526ef6ac4SDon Lewis port = inp->inp_fport; 53626ef6ac4SDon Lewis addr6 = inp->in6p_faddr; 53726ef6ac4SDon Lewis } 53826ef6ac4SDon Lewis 53926ef6ac4SDon Lewis out: TCPDEBUG2(PRU_ACCEPT); 54026ef6ac4SDon Lewis if (tp) 54126ef6ac4SDon Lewis INP_UNLOCK(inp); 54226ef6ac4SDon Lewis if (error == 0) { 54326ef6ac4SDon Lewis if (v4) 54426ef6ac4SDon Lewis *nam = in6_v4mapsin6_sockaddr(port, &addr); 54526ef6ac4SDon Lewis else 54626ef6ac4SDon Lewis *nam = in6_sockaddr(port, &addr6); 54726ef6ac4SDon Lewis } 54826ef6ac4SDon Lewis return error; 549fb59c426SYoshinobu Inoue } 550fb59c426SYoshinobu Inoue #endif /* INET6 */ 551f76fcf6dSJeffrey Hsu 552f76fcf6dSJeffrey Hsu /* 553f76fcf6dSJeffrey Hsu * This is the wrapper function for in_setsockaddr. We just pass down 554f76fcf6dSJeffrey Hsu * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking 555f76fcf6dSJeffrey Hsu * here because in_setsockaddr will call malloc and can block. 556f76fcf6dSJeffrey Hsu */ 557f76fcf6dSJeffrey Hsu static int 558f76fcf6dSJeffrey Hsu tcp_sockaddr(struct socket *so, struct sockaddr **nam) 559f76fcf6dSJeffrey Hsu { 560f76fcf6dSJeffrey Hsu return (in_setsockaddr(so, nam, &tcbinfo)); 561f76fcf6dSJeffrey Hsu } 562f76fcf6dSJeffrey Hsu 563f76fcf6dSJeffrey Hsu /* 564f76fcf6dSJeffrey Hsu * This is the wrapper function for in_setpeeraddr. We just pass down 565f76fcf6dSJeffrey Hsu * the pcbinfo for in_setpeeraddr to lock. 566f76fcf6dSJeffrey Hsu */ 567f76fcf6dSJeffrey Hsu static int 568f76fcf6dSJeffrey Hsu tcp_peeraddr(struct socket *so, struct sockaddr **nam) 569f76fcf6dSJeffrey Hsu { 570f76fcf6dSJeffrey Hsu return (in_setpeeraddr(so, nam, &tcbinfo)); 571f76fcf6dSJeffrey Hsu } 572f76fcf6dSJeffrey Hsu 5732c37256eSGarrett Wollman /* 5742c37256eSGarrett Wollman * Mark the connection as being incapable of further output. 5752c37256eSGarrett Wollman */ 5762c37256eSGarrett Wollman static int 5772c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so) 5782c37256eSGarrett Wollman { 5792c37256eSGarrett Wollman int error = 0; 580f76fcf6dSJeffrey Hsu struct inpcb *inp; 5812c37256eSGarrett Wollman struct tcpcb *tp; 582f76fcf6dSJeffrey Hsu const int inirw = INI_WRITE; 5832c37256eSGarrett Wollman 5842c37256eSGarrett Wollman COMMON_START(); 5852c37256eSGarrett Wollman socantsendmore(so); 5862c37256eSGarrett Wollman tp = tcp_usrclosed(tp); 5872c37256eSGarrett Wollman if (tp) 5882c37256eSGarrett Wollman error = tcp_output(tp); 5892c37256eSGarrett Wollman COMMON_END(PRU_SHUTDOWN); 5902c37256eSGarrett Wollman } 5912c37256eSGarrett Wollman 5922c37256eSGarrett Wollman /* 5932c37256eSGarrett Wollman * After a receive, possibly send window update to peer. 5942c37256eSGarrett Wollman */ 5952c37256eSGarrett Wollman static int 5962c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags) 5972c37256eSGarrett Wollman { 5982c37256eSGarrett Wollman int error = 0; 599f76fcf6dSJeffrey Hsu struct inpcb *inp; 6002c37256eSGarrett Wollman struct tcpcb *tp; 601f76fcf6dSJeffrey Hsu const int inirw = INI_READ; 6022c37256eSGarrett Wollman 6032c37256eSGarrett Wollman COMMON_START(); 6042c37256eSGarrett Wollman tcp_output(tp); 6052c37256eSGarrett Wollman COMMON_END(PRU_RCVD); 6062c37256eSGarrett Wollman } 6072c37256eSGarrett Wollman 6082c37256eSGarrett Wollman /* 6092c37256eSGarrett Wollman * Do a send by putting data in output queue and updating urgent 6109c9906e9SPeter Wemm * marker if URG set. Possibly send more data. Unlike the other 6119c9906e9SPeter Wemm * pru_*() routines, the mbuf chains are our responsibility. We 6129c9906e9SPeter Wemm * must either enqueue them or free them. The other pru_* routines 6139c9906e9SPeter Wemm * generally are caller-frees. 6142c37256eSGarrett Wollman */ 6152c37256eSGarrett Wollman static int 61657bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 617b40ce416SJulian Elischer struct sockaddr *nam, struct mbuf *control, struct thread *td) 6182c37256eSGarrett Wollman { 6192c37256eSGarrett Wollman int error = 0; 620f76fcf6dSJeffrey Hsu struct inpcb *inp; 6212c37256eSGarrett Wollman struct tcpcb *tp; 622d1401c90SRobert Watson int unlocked = 0; 623fb59c426SYoshinobu Inoue #ifdef INET6 624fb59c426SYoshinobu Inoue int isipv6; 625fb59c426SYoshinobu Inoue #endif 6269c9906e9SPeter Wemm TCPDEBUG0; 6272c37256eSGarrett Wollman 628f76fcf6dSJeffrey Hsu /* 629f76fcf6dSJeffrey Hsu * Need write lock here because this function might call 630f76fcf6dSJeffrey Hsu * tcp_connect or tcp_usrclosed. 631f76fcf6dSJeffrey Hsu * We really want to have to this function upgrade from read lock 632f76fcf6dSJeffrey Hsu * to write lock. XXX 633f76fcf6dSJeffrey Hsu */ 634f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&tcbinfo); 635f76fcf6dSJeffrey Hsu inp = sotoinpcb(so); 6369c9906e9SPeter Wemm if (inp == NULL) { 6379c9906e9SPeter Wemm /* 6389c9906e9SPeter Wemm * OOPS! we lost a race, the TCP session got reset after 639c0b99ffaSRobert Watson * we checked SBS_CANTSENDMORE, eg: while doing uiomove or a 6409c9906e9SPeter Wemm * network interrupt in the non-splnet() section of sosend(). 6419c9906e9SPeter Wemm */ 6429c9906e9SPeter Wemm if (m) 6439c9906e9SPeter Wemm m_freem(m); 6449c9906e9SPeter Wemm if (control) 6459c9906e9SPeter Wemm m_freem(control); 6469c9906e9SPeter Wemm error = ECONNRESET; /* XXX EPIPE? */ 64745d3a132SPeter Wemm tp = NULL; 64845d3a132SPeter Wemm TCPDEBUG1(); 6499c9906e9SPeter Wemm goto out; 6509c9906e9SPeter Wemm } 651f76fcf6dSJeffrey Hsu INP_LOCK(inp); 652fb59c426SYoshinobu Inoue #ifdef INET6 653fb59c426SYoshinobu Inoue isipv6 = nam && nam->sa_family == AF_INET6; 654fb59c426SYoshinobu Inoue #endif /* INET6 */ 6559c9906e9SPeter Wemm tp = intotcpcb(inp); 6569c9906e9SPeter Wemm TCPDEBUG1(); 6579c9906e9SPeter Wemm if (control) { 6589c9906e9SPeter Wemm /* TCP doesn't do control messages (rights, creds, etc) */ 6599c9906e9SPeter Wemm if (control->m_len) { 6609c9906e9SPeter Wemm m_freem(control); 6612c37256eSGarrett Wollman if (m) 6622c37256eSGarrett Wollman m_freem(m); 663744f87eaSDavid Greenman error = EINVAL; 664744f87eaSDavid Greenman goto out; 6652c37256eSGarrett Wollman } 6669c9906e9SPeter Wemm m_freem(control); /* empty control, just free it */ 6679c9906e9SPeter Wemm } 6682c37256eSGarrett Wollman if (!(flags & PRUS_OOB)) { 669395bb186SSam Leffler sbappendstream(&so->so_snd, m); 6702c37256eSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 6712c37256eSGarrett Wollman /* 6722c37256eSGarrett Wollman * Do implied connect if not yet connected, 6732c37256eSGarrett Wollman * initialize window to default value, and 6742c37256eSGarrett Wollman * initialize maxseg/maxopd using peer's cached 6752c37256eSGarrett Wollman * MSS. 6762c37256eSGarrett Wollman */ 677fb59c426SYoshinobu Inoue #ifdef INET6 678fb59c426SYoshinobu Inoue if (isipv6) 679b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 680fb59c426SYoshinobu Inoue else 681fb59c426SYoshinobu Inoue #endif /* INET6 */ 682b40ce416SJulian Elischer error = tcp_connect(tp, nam, td); 6832c37256eSGarrett Wollman if (error) 6842c37256eSGarrett Wollman goto out; 6852c37256eSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 6862c37256eSGarrett Wollman tcp_mss(tp, -1); 6872c37256eSGarrett Wollman } 6882c37256eSGarrett Wollman 6892c37256eSGarrett Wollman if (flags & PRUS_EOF) { 6902c37256eSGarrett Wollman /* 6912c37256eSGarrett Wollman * Close the send side of the connection after 6922c37256eSGarrett Wollman * the data is sent. 6932c37256eSGarrett Wollman */ 6942c37256eSGarrett Wollman socantsendmore(so); 6952c37256eSGarrett Wollman tp = tcp_usrclosed(tp); 6962c37256eSGarrett Wollman } 697d1401c90SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 698d1401c90SRobert Watson unlocked = 1; 699b0acefa8SBill Fenner if (tp != NULL) { 700b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 701b0acefa8SBill Fenner tp->t_flags |= TF_MORETOCOME; 7022c37256eSGarrett Wollman error = tcp_output(tp); 703b0acefa8SBill Fenner if (flags & PRUS_MORETOCOME) 704b0acefa8SBill Fenner tp->t_flags &= ~TF_MORETOCOME; 705b0acefa8SBill Fenner } 7062c37256eSGarrett Wollman } else { 707d2bc35abSRobert Watson SOCKBUF_LOCK(&so->so_snd); 7082c37256eSGarrett Wollman if (sbspace(&so->so_snd) < -512) { 709d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 7102c37256eSGarrett Wollman m_freem(m); 7112c37256eSGarrett Wollman error = ENOBUFS; 7122c37256eSGarrett Wollman goto out; 7132c37256eSGarrett Wollman } 7142c37256eSGarrett Wollman /* 7152c37256eSGarrett Wollman * According to RFC961 (Assigned Protocols), 7162c37256eSGarrett Wollman * the urgent pointer points to the last octet 7172c37256eSGarrett Wollman * of urgent data. We continue, however, 7182c37256eSGarrett Wollman * to consider it to indicate the first octet 7192c37256eSGarrett Wollman * of data past the urgent section. 7202c37256eSGarrett Wollman * Otherwise, snd_up should be one lower. 7212c37256eSGarrett Wollman */ 722d2bc35abSRobert Watson sbappendstream_locked(&so->so_snd, m); 723d2bc35abSRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 724ef53690bSGarrett Wollman if (nam && tp->t_state < TCPS_SYN_SENT) { 725ef53690bSGarrett Wollman /* 726ef53690bSGarrett Wollman * Do implied connect if not yet connected, 727ef53690bSGarrett Wollman * initialize window to default value, and 728ef53690bSGarrett Wollman * initialize maxseg/maxopd using peer's cached 729ef53690bSGarrett Wollman * MSS. 730ef53690bSGarrett Wollman */ 731fb59c426SYoshinobu Inoue #ifdef INET6 732fb59c426SYoshinobu Inoue if (isipv6) 733b40ce416SJulian Elischer error = tcp6_connect(tp, nam, td); 734fb59c426SYoshinobu Inoue else 735fb59c426SYoshinobu Inoue #endif /* INET6 */ 736b40ce416SJulian Elischer error = tcp_connect(tp, nam, td); 737ef53690bSGarrett Wollman if (error) 738ef53690bSGarrett Wollman goto out; 739ef53690bSGarrett Wollman tp->snd_wnd = TTCP_CLIENT_SND_WND; 740ef53690bSGarrett Wollman tcp_mss(tp, -1); 741ef53690bSGarrett Wollman } 742d1401c90SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 743d1401c90SRobert Watson unlocked = 1; 7442c37256eSGarrett Wollman tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 7452cdbfa66SPaul Saab tp->t_flags |= TF_FORCEDATA; 7462c37256eSGarrett Wollman error = tcp_output(tp); 7472cdbfa66SPaul Saab tp->t_flags &= ~TF_FORCEDATA; 7482c37256eSGarrett Wollman } 749d1401c90SRobert Watson out: 750d1401c90SRobert Watson TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB : 7512c37256eSGarrett Wollman ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 752d1401c90SRobert Watson if (tp) 753d1401c90SRobert Watson INP_UNLOCK(inp); 754d1401c90SRobert Watson if (!unlocked) 755d1401c90SRobert Watson INP_INFO_WUNLOCK(&tcbinfo); 75673fddedaSPeter Grehan return (error); 7572c37256eSGarrett Wollman } 7582c37256eSGarrett Wollman 7592c37256eSGarrett Wollman /* 7602c37256eSGarrett Wollman * Abort the TCP. 7612c37256eSGarrett Wollman */ 7622c37256eSGarrett Wollman static int 7632c37256eSGarrett Wollman tcp_usr_abort(struct socket *so) 7642c37256eSGarrett Wollman { 7652c37256eSGarrett Wollman int error = 0; 766f76fcf6dSJeffrey Hsu struct inpcb *inp; 7672c37256eSGarrett Wollman struct tcpcb *tp; 768f76fcf6dSJeffrey Hsu const int inirw = INI_WRITE; 7692c37256eSGarrett Wollman 7702c37256eSGarrett Wollman COMMON_START(); 7712c37256eSGarrett Wollman tp = tcp_drop(tp, ECONNABORTED); 7722c37256eSGarrett Wollman COMMON_END(PRU_ABORT); 7732c37256eSGarrett Wollman } 7742c37256eSGarrett Wollman 7752c37256eSGarrett Wollman /* 7762c37256eSGarrett Wollman * Receive out-of-band data. 7772c37256eSGarrett Wollman */ 7782c37256eSGarrett Wollman static int 7792c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 7802c37256eSGarrett Wollman { 7812c37256eSGarrett Wollman int error = 0; 782f76fcf6dSJeffrey Hsu struct inpcb *inp; 7832c37256eSGarrett Wollman struct tcpcb *tp; 784f76fcf6dSJeffrey Hsu const int inirw = INI_READ; 7852c37256eSGarrett Wollman 7862c37256eSGarrett Wollman COMMON_START(); 7872c37256eSGarrett Wollman if ((so->so_oobmark == 0 && 788c0b99ffaSRobert Watson (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 7894cc20ab1SSeigo Tanimura so->so_options & SO_OOBINLINE || 7904cc20ab1SSeigo Tanimura tp->t_oobflags & TCPOOB_HADDATA) { 7912c37256eSGarrett Wollman error = EINVAL; 7922c37256eSGarrett Wollman goto out; 7932c37256eSGarrett Wollman } 7942c37256eSGarrett Wollman if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 7952c37256eSGarrett Wollman error = EWOULDBLOCK; 7962c37256eSGarrett Wollman goto out; 7972c37256eSGarrett Wollman } 7982c37256eSGarrett Wollman m->m_len = 1; 7992c37256eSGarrett Wollman *mtod(m, caddr_t) = tp->t_iobc; 8002c37256eSGarrett Wollman if ((flags & MSG_PEEK) == 0) 8012c37256eSGarrett Wollman tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 8022c37256eSGarrett Wollman COMMON_END(PRU_RCVOOB); 8032c37256eSGarrett Wollman } 8042c37256eSGarrett Wollman 8052c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = { 806756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 807756d52a1SPoul-Henning Kamp .pru_accept = tcp_usr_accept, 808756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 809756d52a1SPoul-Henning Kamp .pru_bind = tcp_usr_bind, 810756d52a1SPoul-Henning Kamp .pru_connect = tcp_usr_connect, 811756d52a1SPoul-Henning Kamp .pru_control = in_control, 812756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 813756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 814756d52a1SPoul-Henning Kamp .pru_listen = tcp_usr_listen, 815756d52a1SPoul-Henning Kamp .pru_peeraddr = tcp_peeraddr, 816756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 817756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 818756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 819756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 820756d52a1SPoul-Henning Kamp .pru_sockaddr = tcp_sockaddr, 821756d52a1SPoul-Henning Kamp .pru_sosetlabel = in_pcbsosetlabel 8222c37256eSGarrett Wollman }; 823df8bae1dSRodney W. Grimes 824fb59c426SYoshinobu Inoue #ifdef INET6 825fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = { 826756d52a1SPoul-Henning Kamp .pru_abort = tcp_usr_abort, 827756d52a1SPoul-Henning Kamp .pru_accept = tcp6_usr_accept, 828756d52a1SPoul-Henning Kamp .pru_attach = tcp_usr_attach, 829756d52a1SPoul-Henning Kamp .pru_bind = tcp6_usr_bind, 830756d52a1SPoul-Henning Kamp .pru_connect = tcp6_usr_connect, 831756d52a1SPoul-Henning Kamp .pru_control = in6_control, 832756d52a1SPoul-Henning Kamp .pru_detach = tcp_usr_detach, 833756d52a1SPoul-Henning Kamp .pru_disconnect = tcp_usr_disconnect, 834756d52a1SPoul-Henning Kamp .pru_listen = tcp6_usr_listen, 835756d52a1SPoul-Henning Kamp .pru_peeraddr = in6_mapped_peeraddr, 836756d52a1SPoul-Henning Kamp .pru_rcvd = tcp_usr_rcvd, 837756d52a1SPoul-Henning Kamp .pru_rcvoob = tcp_usr_rcvoob, 838756d52a1SPoul-Henning Kamp .pru_send = tcp_usr_send, 839756d52a1SPoul-Henning Kamp .pru_shutdown = tcp_usr_shutdown, 840756d52a1SPoul-Henning Kamp .pru_sockaddr = in6_mapped_sockaddr, 841756d52a1SPoul-Henning Kamp .pru_sosetlabel = in_pcbsosetlabel 842fb59c426SYoshinobu Inoue }; 843fb59c426SYoshinobu Inoue #endif /* INET6 */ 844fb59c426SYoshinobu Inoue 845a0292f23SGarrett Wollman /* 846a0292f23SGarrett Wollman * Common subroutine to open a TCP connection to remote host specified 847a0292f23SGarrett Wollman * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 8485200e00eSIan Dowse * port number if needed. Call in_pcbconnect_setup to do the routing and 8495200e00eSIan Dowse * to choose a local host address (interface). If there is an existing 8505200e00eSIan Dowse * incarnation of the same connection in TIME-WAIT state and if the remote 8515200e00eSIan Dowse * host was sending CC options and if the connection duration was < MSL, then 852a0292f23SGarrett Wollman * truncate the previous TIME-WAIT state and proceed. 853a0292f23SGarrett Wollman * Initialize connection parameters and enter SYN-SENT state. 854a0292f23SGarrett Wollman */ 8550312fbe9SPoul-Henning Kamp static int 856b40ce416SJulian Elischer tcp_connect(tp, nam, td) 857a0292f23SGarrett Wollman register struct tcpcb *tp; 85857bf258eSGarrett Wollman struct sockaddr *nam; 859b40ce416SJulian Elischer struct thread *td; 860a0292f23SGarrett Wollman { 861a0292f23SGarrett Wollman struct inpcb *inp = tp->t_inpcb, *oinp; 862a0292f23SGarrett Wollman struct socket *so = inp->inp_socket; 8635200e00eSIan Dowse struct in_addr laddr; 8645200e00eSIan Dowse u_short lport; 865c3229e05SDavid Greenman int error; 866a0292f23SGarrett Wollman 867a0292f23SGarrett Wollman if (inp->inp_lport == 0) { 868b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 869a0292f23SGarrett Wollman if (error) 870a0292f23SGarrett Wollman return error; 871a0292f23SGarrett Wollman } 872a0292f23SGarrett Wollman 873a0292f23SGarrett Wollman /* 874a0292f23SGarrett Wollman * Cannot simply call in_pcbconnect, because there might be an 875a0292f23SGarrett Wollman * earlier incarnation of this same connection still in 876a0292f23SGarrett Wollman * TIME_WAIT state, creating an ADDRINUSE error. 877a0292f23SGarrett Wollman */ 8785200e00eSIan Dowse laddr = inp->inp_laddr; 8795200e00eSIan Dowse lport = inp->inp_lport; 8805200e00eSIan Dowse error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 881b0330ed9SPawel Jakub Dawidek &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 8825200e00eSIan Dowse if (error && oinp == NULL) 883d3628763SRodney W. Grimes return error; 884c94c54e4SAndre Oppermann if (oinp) 885a0292f23SGarrett Wollman return EADDRINUSE; 8865200e00eSIan Dowse inp->inp_laddr = laddr; 88715bd2b43SDavid Greenman in_pcbrehash(inp); 888a0292f23SGarrett Wollman 889a0292f23SGarrett Wollman /* Compute window scaling to request. */ 890a0292f23SGarrett Wollman while (tp->request_r_scale < TCP_MAX_WINSHIFT && 891a0292f23SGarrett Wollman (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 892a0292f23SGarrett Wollman tp->request_r_scale++; 893a0292f23SGarrett Wollman 894a0292f23SGarrett Wollman soisconnecting(so); 895a0292f23SGarrett Wollman tcpstat.tcps_connattempt++; 896a0292f23SGarrett Wollman tp->t_state = TCPS_SYN_SENT; 8979b8b58e0SJonathan Lemon callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 898b0e3ad75SMike Silbersack tp->iss = tcp_new_isn(tp); 8991fcc99b5SMatthew Dillon tp->t_bw_rtseq = tp->iss; 900a0292f23SGarrett Wollman tcp_sendseqinit(tp); 901a45d2726SAndras Olah 902a0292f23SGarrett Wollman return 0; 903a0292f23SGarrett Wollman } 904a0292f23SGarrett Wollman 905fb59c426SYoshinobu Inoue #ifdef INET6 906fb59c426SYoshinobu Inoue static int 907b40ce416SJulian Elischer tcp6_connect(tp, nam, td) 908fb59c426SYoshinobu Inoue register struct tcpcb *tp; 909fb59c426SYoshinobu Inoue struct sockaddr *nam; 910b40ce416SJulian Elischer struct thread *td; 911fb59c426SYoshinobu Inoue { 912fb59c426SYoshinobu Inoue struct inpcb *inp = tp->t_inpcb, *oinp; 913fb59c426SYoshinobu Inoue struct socket *so = inp->inp_socket; 914fb59c426SYoshinobu Inoue struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 915fb59c426SYoshinobu Inoue struct in6_addr *addr6; 916fb59c426SYoshinobu Inoue int error; 917fb59c426SYoshinobu Inoue 918fb59c426SYoshinobu Inoue if (inp->inp_lport == 0) { 919b0330ed9SPawel Jakub Dawidek error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 920fb59c426SYoshinobu Inoue if (error) 921fb59c426SYoshinobu Inoue return error; 922fb59c426SYoshinobu Inoue } 923fb59c426SYoshinobu Inoue 924fb59c426SYoshinobu Inoue /* 925fb59c426SYoshinobu Inoue * Cannot simply call in_pcbconnect, because there might be an 926fb59c426SYoshinobu Inoue * earlier incarnation of this same connection still in 927fb59c426SYoshinobu Inoue * TIME_WAIT state, creating an ADDRINUSE error. 928fb59c426SYoshinobu Inoue */ 929fb59c426SYoshinobu Inoue error = in6_pcbladdr(inp, nam, &addr6); 930fb59c426SYoshinobu Inoue if (error) 931fb59c426SYoshinobu Inoue return error; 932fb59c426SYoshinobu Inoue oinp = in6_pcblookup_hash(inp->inp_pcbinfo, 933fb59c426SYoshinobu Inoue &sin6->sin6_addr, sin6->sin6_port, 934fb59c426SYoshinobu Inoue IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 935fb59c426SYoshinobu Inoue ? addr6 936fb59c426SYoshinobu Inoue : &inp->in6p_laddr, 937fb59c426SYoshinobu Inoue inp->inp_lport, 0, NULL); 938c94c54e4SAndre Oppermann if (oinp) 939fb59c426SYoshinobu Inoue return EADDRINUSE; 940fb59c426SYoshinobu Inoue if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 941fb59c426SYoshinobu Inoue inp->in6p_laddr = *addr6; 942fb59c426SYoshinobu Inoue inp->in6p_faddr = sin6->sin6_addr; 943fb59c426SYoshinobu Inoue inp->inp_fport = sin6->sin6_port; 9448a59da30SHajimu UMEMOTO /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 9458a59da30SHajimu UMEMOTO inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 9468a59da30SHajimu UMEMOTO if (inp->in6p_flags & IN6P_AUTOFLOWLABEL) 9478a59da30SHajimu UMEMOTO inp->in6p_flowinfo |= 9488a59da30SHajimu UMEMOTO (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 949fb59c426SYoshinobu Inoue in_pcbrehash(inp); 950fb59c426SYoshinobu Inoue 951fb59c426SYoshinobu Inoue /* Compute window scaling to request. */ 952fb59c426SYoshinobu Inoue while (tp->request_r_scale < TCP_MAX_WINSHIFT && 953fb59c426SYoshinobu Inoue (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 954fb59c426SYoshinobu Inoue tp->request_r_scale++; 955fb59c426SYoshinobu Inoue 956fb59c426SYoshinobu Inoue soisconnecting(so); 957fb59c426SYoshinobu Inoue tcpstat.tcps_connattempt++; 958fb59c426SYoshinobu Inoue tp->t_state = TCPS_SYN_SENT; 959fb59c426SYoshinobu Inoue callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 960b0e3ad75SMike Silbersack tp->iss = tcp_new_isn(tp); 9611fcc99b5SMatthew Dillon tp->t_bw_rtseq = tp->iss; 962fb59c426SYoshinobu Inoue tcp_sendseqinit(tp); 963fb59c426SYoshinobu Inoue 964fb59c426SYoshinobu Inoue return 0; 965fb59c426SYoshinobu Inoue } 966fb59c426SYoshinobu Inoue #endif /* INET6 */ 967fb59c426SYoshinobu Inoue 968cfe8b629SGarrett Wollman /* 969b8af5dfaSRobert Watson * Export TCP internal state information via a struct tcp_info, based on the 970b8af5dfaSRobert Watson * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 971b8af5dfaSRobert Watson * (TCP state machine, etc). We export all information using FreeBSD-native 972b8af5dfaSRobert Watson * constants -- for example, the numeric values for tcpi_state will differ 973b8af5dfaSRobert Watson * from Linux. 974b8af5dfaSRobert Watson */ 975b8af5dfaSRobert Watson static void 976b8af5dfaSRobert Watson tcp_fill_info(tp, ti) 977b8af5dfaSRobert Watson struct tcpcb *tp; 978b8af5dfaSRobert Watson struct tcp_info *ti; 979b8af5dfaSRobert Watson { 980b8af5dfaSRobert Watson 981b8af5dfaSRobert Watson INP_LOCK_ASSERT(tp->t_inpcb); 982b8af5dfaSRobert Watson bzero(ti, sizeof(*ti)); 983b8af5dfaSRobert Watson 984b8af5dfaSRobert Watson ti->tcpi_state = tp->t_state; 985b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 986b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 987b8af5dfaSRobert Watson if (tp->sack_enable) 988b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_SACK; 989b8af5dfaSRobert Watson if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 990b8af5dfaSRobert Watson ti->tcpi_options |= TCPI_OPT_WSCALE; 991b8af5dfaSRobert Watson ti->tcpi_snd_wscale = tp->snd_scale; 992b8af5dfaSRobert Watson ti->tcpi_rcv_wscale = tp->rcv_scale; 993b8af5dfaSRobert Watson } 994b8af5dfaSRobert Watson ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 995b8af5dfaSRobert Watson ti->tcpi_snd_cwnd = tp->snd_cwnd; 996b8af5dfaSRobert Watson 997b8af5dfaSRobert Watson /* 998b8af5dfaSRobert Watson * FreeBSD-specific extension fields for tcp_info. 999b8af5dfaSRobert Watson */ 1000c8443a1dSRobert Watson ti->tcpi_rcv_space = tp->rcv_wnd; 1001b8af5dfaSRobert Watson ti->tcpi_snd_wnd = tp->snd_wnd; 1002b8af5dfaSRobert Watson ti->tcpi_snd_bwnd = tp->snd_bwnd; 1003b8af5dfaSRobert Watson } 1004b8af5dfaSRobert Watson 1005b8af5dfaSRobert Watson /* 1006cfe8b629SGarrett Wollman * The new sockopt interface makes it possible for us to block in the 1007cfe8b629SGarrett Wollman * copyin/out step (if we take a page fault). Taking a page fault at 1008cfe8b629SGarrett Wollman * splnet() is probably a Bad Thing. (Since sockets and pcbs both now 1009cfe8b629SGarrett Wollman * use TSM, there probably isn't any need for this function to run at 1010cfe8b629SGarrett Wollman * splnet() any more. This needs more examination.) 1011b8af5dfaSRobert Watson * 1012b8af5dfaSRobert Watson * XXXRW: The locking here is wrong; we may take a page fault while holding 1013b8af5dfaSRobert Watson * the inpcb lock. 1014cfe8b629SGarrett Wollman */ 1015df8bae1dSRodney W. Grimes int 1016cfe8b629SGarrett Wollman tcp_ctloutput(so, sopt) 1017df8bae1dSRodney W. Grimes struct socket *so; 1018cfe8b629SGarrett Wollman struct sockopt *sopt; 1019df8bae1dSRodney W. Grimes { 10203f9d1ef9SRobert Watson int error, opt, optval; 1021df8bae1dSRodney W. Grimes struct inpcb *inp; 1022cfe8b629SGarrett Wollman struct tcpcb *tp; 1023b8af5dfaSRobert Watson struct tcp_info ti; 1024df8bae1dSRodney W. Grimes 1025cfe8b629SGarrett Wollman error = 0; 1026f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(&tcbinfo); 1027df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1028df8bae1dSRodney W. Grimes if (inp == NULL) { 1029f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&tcbinfo); 1030df8bae1dSRodney W. Grimes return (ECONNRESET); 1031df8bae1dSRodney W. Grimes } 1032f76fcf6dSJeffrey Hsu INP_LOCK(inp); 1033f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&tcbinfo); 1034cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_TCP) { 10354e397bc5SRobert Watson INP_UNLOCK(inp); 1036fb59c426SYoshinobu Inoue #ifdef INET6 1037fb59c426SYoshinobu Inoue if (INP_CHECK_SOCKAF(so, AF_INET6)) 1038fb59c426SYoshinobu Inoue error = ip6_ctloutput(so, sopt); 1039fb59c426SYoshinobu Inoue else 1040fb59c426SYoshinobu Inoue #endif /* INET6 */ 1041cfe8b629SGarrett Wollman error = ip_ctloutput(so, sopt); 1042df8bae1dSRodney W. Grimes return (error); 1043df8bae1dSRodney W. Grimes } 1044df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 1045df8bae1dSRodney W. Grimes 1046cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 1047cfe8b629SGarrett Wollman case SOPT_SET: 1048cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 10491cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 105088f6b043SBruce M Simpson case TCP_MD5SIG: 10511cfd4b53SBruce M Simpson error = sooptcopyin(sopt, &optval, sizeof optval, 10521cfd4b53SBruce M Simpson sizeof optval); 10531cfd4b53SBruce M Simpson if (error) 10541cfd4b53SBruce M Simpson break; 10551cfd4b53SBruce M Simpson 10561cfd4b53SBruce M Simpson if (optval > 0) 10571cfd4b53SBruce M Simpson tp->t_flags |= TF_SIGNATURE; 10581cfd4b53SBruce M Simpson else 10591cfd4b53SBruce M Simpson tp->t_flags &= ~TF_SIGNATURE; 10601cfd4b53SBruce M Simpson break; 10611cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */ 1062df8bae1dSRodney W. Grimes case TCP_NODELAY: 1063cfe8b629SGarrett Wollman case TCP_NOOPT: 1064cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1065cfe8b629SGarrett Wollman sizeof optval); 1066cfe8b629SGarrett Wollman if (error) 1067cfe8b629SGarrett Wollman break; 1068cfe8b629SGarrett Wollman 1069cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 1070cfe8b629SGarrett Wollman case TCP_NODELAY: 1071cfe8b629SGarrett Wollman opt = TF_NODELAY; 1072cfe8b629SGarrett Wollman break; 1073cfe8b629SGarrett Wollman case TCP_NOOPT: 1074cfe8b629SGarrett Wollman opt = TF_NOOPT; 1075cfe8b629SGarrett Wollman break; 1076cfe8b629SGarrett Wollman default: 1077cfe8b629SGarrett Wollman opt = 0; /* dead code to fool gcc */ 1078cfe8b629SGarrett Wollman break; 1079cfe8b629SGarrett Wollman } 1080cfe8b629SGarrett Wollman 1081cfe8b629SGarrett Wollman if (optval) 1082cfe8b629SGarrett Wollman tp->t_flags |= opt; 1083df8bae1dSRodney W. Grimes else 1084cfe8b629SGarrett Wollman tp->t_flags &= ~opt; 1085df8bae1dSRodney W. Grimes break; 1086df8bae1dSRodney W. Grimes 1087007581c0SJonathan Lemon case TCP_NOPUSH: 1088007581c0SJonathan Lemon error = sooptcopyin(sopt, &optval, sizeof optval, 1089007581c0SJonathan Lemon sizeof optval); 1090007581c0SJonathan Lemon if (error) 1091007581c0SJonathan Lemon break; 1092007581c0SJonathan Lemon 1093007581c0SJonathan Lemon if (optval) 1094007581c0SJonathan Lemon tp->t_flags |= TF_NOPUSH; 1095007581c0SJonathan Lemon else { 1096007581c0SJonathan Lemon tp->t_flags &= ~TF_NOPUSH; 1097007581c0SJonathan Lemon error = tcp_output(tp); 1098007581c0SJonathan Lemon } 1099007581c0SJonathan Lemon break; 1100007581c0SJonathan Lemon 1101df8bae1dSRodney W. Grimes case TCP_MAXSEG: 1102cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 1103cfe8b629SGarrett Wollman sizeof optval); 1104cfe8b629SGarrett Wollman if (error) 1105df8bae1dSRodney W. Grimes break; 1106df8bae1dSRodney W. Grimes 110753369ac9SAndre Oppermann if (optval > 0 && optval <= tp->t_maxseg && 110853369ac9SAndre Oppermann optval + 40 >= tcp_minmss) 1109cfe8b629SGarrett Wollman tp->t_maxseg = optval; 1110a0292f23SGarrett Wollman else 1111a0292f23SGarrett Wollman error = EINVAL; 1112a0292f23SGarrett Wollman break; 1113a0292f23SGarrett Wollman 1114b8af5dfaSRobert Watson case TCP_INFO: 1115b8af5dfaSRobert Watson error = EINVAL; 1116b8af5dfaSRobert Watson break; 1117b8af5dfaSRobert Watson 1118df8bae1dSRodney W. Grimes default: 1119df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1120df8bae1dSRodney W. Grimes break; 1121df8bae1dSRodney W. Grimes } 1122df8bae1dSRodney W. Grimes break; 1123df8bae1dSRodney W. Grimes 1124cfe8b629SGarrett Wollman case SOPT_GET: 1125cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 11261cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 112788f6b043SBruce M Simpson case TCP_MD5SIG: 11281cfd4b53SBruce M Simpson optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0; 1129b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 11301cfd4b53SBruce M Simpson break; 1131265ed012SBruce M Simpson #endif 1132df8bae1dSRodney W. Grimes case TCP_NODELAY: 1133cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NODELAY; 1134b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1135df8bae1dSRodney W. Grimes break; 1136df8bae1dSRodney W. Grimes case TCP_MAXSEG: 1137cfe8b629SGarrett Wollman optval = tp->t_maxseg; 1138b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1139df8bae1dSRodney W. Grimes break; 1140a0292f23SGarrett Wollman case TCP_NOOPT: 1141cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOOPT; 1142b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1143a0292f23SGarrett Wollman break; 1144a0292f23SGarrett Wollman case TCP_NOPUSH: 1145cfe8b629SGarrett Wollman optval = tp->t_flags & TF_NOPUSH; 1146b8af5dfaSRobert Watson error = sooptcopyout(sopt, &optval, sizeof optval); 1147b8af5dfaSRobert Watson break; 1148b8af5dfaSRobert Watson case TCP_INFO: 1149b8af5dfaSRobert Watson tcp_fill_info(tp, &ti); 1150b8af5dfaSRobert Watson error = sooptcopyout(sopt, &ti, sizeof ti); 1151a0292f23SGarrett Wollman break; 1152df8bae1dSRodney W. Grimes default: 1153df8bae1dSRodney W. Grimes error = ENOPROTOOPT; 1154df8bae1dSRodney W. Grimes break; 1155df8bae1dSRodney W. Grimes } 1156df8bae1dSRodney W. Grimes break; 1157df8bae1dSRodney W. Grimes } 1158f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 1159df8bae1dSRodney W. Grimes return (error); 1160df8bae1dSRodney W. Grimes } 1161df8bae1dSRodney W. Grimes 116226e30fbbSDavid Greenman /* 116326e30fbbSDavid Greenman * tcp_sendspace and tcp_recvspace are the default send and receive window 116426e30fbbSDavid Greenman * sizes, respectively. These are obsolescent (this information should 116526e30fbbSDavid Greenman * be set by the route). 116626e30fbbSDavid Greenman */ 116781e561cdSDavid E. O'Brien u_long tcp_sendspace = 1024*32; 11683d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW, 11693d177f46SBill Fumerola &tcp_sendspace , 0, "Maximum outgoing TCP datagram size"); 117081e561cdSDavid E. O'Brien u_long tcp_recvspace = 1024*64; 11713d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 11723d177f46SBill Fumerola &tcp_recvspace , 0, "Maximum incoming TCP datagram size"); 1173df8bae1dSRodney W. Grimes 1174df8bae1dSRodney W. Grimes /* 1175df8bae1dSRodney W. Grimes * Attach TCP protocol to socket, allocating 1176df8bae1dSRodney W. Grimes * internet protocol control block, tcp control block, 1177df8bae1dSRodney W. Grimes * bufer space, and entering LISTEN state if to accept connections. 1178df8bae1dSRodney W. Grimes */ 11790312fbe9SPoul-Henning Kamp static int 118056dc72c3SPawel Jakub Dawidek tcp_attach(so) 1181df8bae1dSRodney W. Grimes struct socket *so; 1182df8bae1dSRodney W. Grimes { 1183df8bae1dSRodney W. Grimes register struct tcpcb *tp; 1184df8bae1dSRodney W. Grimes struct inpcb *inp; 1185df8bae1dSRodney W. Grimes int error; 1186fb59c426SYoshinobu Inoue #ifdef INET6 11874a6a94d8SArchie Cobbs int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0; 1188fb59c426SYoshinobu Inoue #endif 1189df8bae1dSRodney W. Grimes 1190df8bae1dSRodney W. Grimes if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1191df8bae1dSRodney W. Grimes error = soreserve(so, tcp_sendspace, tcp_recvspace); 1192df8bae1dSRodney W. Grimes if (error) 1193df8bae1dSRodney W. Grimes return (error); 1194df8bae1dSRodney W. Grimes } 11956823b823SPawel Jakub Dawidek error = in_pcballoc(so, &tcbinfo, "tcpinp"); 1196df8bae1dSRodney W. Grimes if (error) 1197df8bae1dSRodney W. Grimes return (error); 1198df8bae1dSRodney W. Grimes inp = sotoinpcb(so); 1199fb59c426SYoshinobu Inoue #ifdef INET6 1200fb59c426SYoshinobu Inoue if (isipv6) { 1201fb59c426SYoshinobu Inoue inp->inp_vflag |= INP_IPV6; 1202fb59c426SYoshinobu Inoue inp->in6p_hops = -1; /* use kernel default */ 1203fb59c426SYoshinobu Inoue } 1204fb59c426SYoshinobu Inoue else 1205fb59c426SYoshinobu Inoue #endif 1206cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 1207df8bae1dSRodney W. Grimes tp = tcp_newtcpcb(inp); 1208df8bae1dSRodney W. Grimes if (tp == 0) { 12094cc20ab1SSeigo Tanimura int nofd = so->so_state & SS_NOFDREF; /* XXX */ 1210df8bae1dSRodney W. Grimes 1211df8bae1dSRodney W. Grimes so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 1212fb59c426SYoshinobu Inoue #ifdef INET6 1213fb59c426SYoshinobu Inoue if (isipv6) 1214fb59c426SYoshinobu Inoue in6_pcbdetach(inp); 1215fb59c426SYoshinobu Inoue else 1216fb59c426SYoshinobu Inoue #endif 1217df8bae1dSRodney W. Grimes in_pcbdetach(inp); 1218df8bae1dSRodney W. Grimes so->so_state |= nofd; 1219df8bae1dSRodney W. Grimes return (ENOBUFS); 1220df8bae1dSRodney W. Grimes } 1221df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 1222df8bae1dSRodney W. Grimes return (0); 1223df8bae1dSRodney W. Grimes } 1224df8bae1dSRodney W. Grimes 1225df8bae1dSRodney W. Grimes /* 1226df8bae1dSRodney W. Grimes * Initiate (or continue) disconnect. 1227df8bae1dSRodney W. Grimes * If embryonic state, just send reset (once). 1228df8bae1dSRodney W. Grimes * If in ``let data drain'' option and linger null, just drop. 1229df8bae1dSRodney W. Grimes * Otherwise (hard), mark socket disconnecting and drop 1230df8bae1dSRodney W. Grimes * current input data; switch states based on user close, and 1231df8bae1dSRodney W. Grimes * send segment to peer (with FIN). 1232df8bae1dSRodney W. Grimes */ 12330312fbe9SPoul-Henning Kamp static struct tcpcb * 1234df8bae1dSRodney W. Grimes tcp_disconnect(tp) 1235df8bae1dSRodney W. Grimes register struct tcpcb *tp; 1236df8bae1dSRodney W. Grimes { 1237df8bae1dSRodney W. Grimes struct socket *so = tp->t_inpcb->inp_socket; 1238df8bae1dSRodney W. Grimes 1239df8bae1dSRodney W. Grimes if (tp->t_state < TCPS_ESTABLISHED) 1240df8bae1dSRodney W. Grimes tp = tcp_close(tp); 12414cc20ab1SSeigo Tanimura else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1242243917feSSeigo Tanimura tp = tcp_drop(tp, 0); 12434cc20ab1SSeigo Tanimura else { 1244df8bae1dSRodney W. Grimes soisdisconnecting(so); 1245df8bae1dSRodney W. Grimes sbflush(&so->so_rcv); 1246df8bae1dSRodney W. Grimes tp = tcp_usrclosed(tp); 1247df8bae1dSRodney W. Grimes if (tp) 1248df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1249df8bae1dSRodney W. Grimes } 1250df8bae1dSRodney W. Grimes return (tp); 1251df8bae1dSRodney W. Grimes } 1252df8bae1dSRodney W. Grimes 1253df8bae1dSRodney W. Grimes /* 1254df8bae1dSRodney W. Grimes * User issued close, and wish to trail through shutdown states: 1255df8bae1dSRodney W. Grimes * if never received SYN, just forget it. If got a SYN from peer, 1256df8bae1dSRodney W. Grimes * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1257df8bae1dSRodney W. Grimes * If already got a FIN from peer, then almost done; go to LAST_ACK 1258df8bae1dSRodney W. Grimes * state. In all other cases, have already sent FIN to peer (e.g. 1259df8bae1dSRodney W. Grimes * after PRU_SHUTDOWN), and just have to play tedious game waiting 1260df8bae1dSRodney W. Grimes * for peer to send FIN or not respond to keep-alives, etc. 1261df8bae1dSRodney W. Grimes * We can let the user exit from the close as soon as the FIN is acked. 1262df8bae1dSRodney W. Grimes */ 12630312fbe9SPoul-Henning Kamp static struct tcpcb * 1264df8bae1dSRodney W. Grimes tcp_usrclosed(tp) 1265df8bae1dSRodney W. Grimes register struct tcpcb *tp; 1266df8bae1dSRodney W. Grimes { 1267df8bae1dSRodney W. Grimes 1268df8bae1dSRodney W. Grimes switch (tp->t_state) { 1269df8bae1dSRodney W. Grimes 1270df8bae1dSRodney W. Grimes case TCPS_CLOSED: 1271df8bae1dSRodney W. Grimes case TCPS_LISTEN: 1272df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSED; 1273df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1274df8bae1dSRodney W. Grimes break; 1275df8bae1dSRodney W. Grimes 1276a0292f23SGarrett Wollman case TCPS_SYN_SENT: 1277df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1278a0292f23SGarrett Wollman tp->t_flags |= TF_NEEDFIN; 1279a0292f23SGarrett Wollman break; 1280a0292f23SGarrett Wollman 1281df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1282df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_1; 1283df8bae1dSRodney W. Grimes break; 1284df8bae1dSRodney W. Grimes 1285df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1286df8bae1dSRodney W. Grimes tp->t_state = TCPS_LAST_ACK; 1287df8bae1dSRodney W. Grimes break; 1288df8bae1dSRodney W. Grimes } 1289b6239c4aSAndras Olah if (tp && tp->t_state >= TCPS_FIN_WAIT_2) { 1290df8bae1dSRodney W. Grimes soisdisconnected(tp->t_inpcb->inp_socket); 1291b6239c4aSAndras Olah /* To prevent the connection hanging in FIN_WAIT_2 forever. */ 1292b6239c4aSAndras Olah if (tp->t_state == TCPS_FIN_WAIT_2) 12939b8b58e0SJonathan Lemon callout_reset(tp->tt_2msl, tcp_maxidle, 12949b8b58e0SJonathan Lemon tcp_timer_2msl, tp); 1295b6239c4aSAndras Olah } 1296df8bae1dSRodney W. Grimes return (tp); 1297df8bae1dSRodney W. Grimes } 1298