X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/1c79356b52d46aa6b508fb032f5ae709b1f2897b..ca66cea69e6e866fd781ae2260d9474bdd48f2ca:/bsd/netinet/tcp_usrreq.c diff --git a/bsd/netinet/tcp_usrreq.c b/bsd/netinet/tcp_usrreq.c index 75458f08a..ed796e474 100644 --- a/bsd/netinet/tcp_usrreq.c +++ b/bsd/netinet/tcp_usrreq.c @@ -1,23 +1,29 @@ /* - * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. + * Copyright (c) 2000-2007 Apple Inc. All rights reserved. * - * @APPLE_LICENSE_HEADER_START@ + * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * - * The contents of this file constitute Original Code as defined in and - * are subject to the Apple Public Source License Version 1.1 (the - * "License"). You may not use this file except in compliance with the - * License. Please obtain a copy of the License at - * http://www.apple.com/publicsource and read it before using this file. + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. The rights granted to you under the License + * may not be used to create, or enable the creation or redistribution of, + * unlawful or unlicensed copies of an Apple operating system, or to + * circumvent, violate, or enable the circumvention or violation of, any + * terms of an Apple operating system software license agreement. * - * This Original Code and all software distributed under the License are - * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the - * License for the specific language governing rights and limitations - * under the License. + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. * - * @APPLE_LICENSE_HEADER_END@ + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* * Copyright (c) 1982, 1986, 1988, 1993 @@ -52,11 +58,9 @@ * SUCH DAMAGE. * * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94 + * $FreeBSD: src/sys/netinet/tcp_usrreq.c,v 1.51.2.9 2001/08/22 00:59:12 silby Exp $ */ -#if ISFB31 -#include "opt_tcpdebug.h" -#endif #include #include @@ -75,14 +79,16 @@ #include #include -#include +#if INET6 +#include +#endif #include +#if INET6 +#include +#endif #include #include #if INET6 -#include -#include -#include #include #endif #include @@ -104,36 +110,68 @@ */ extern char *tcpstates[]; /* XXX ??? */ -static int tcp_attach __P((struct socket *, struct proc *)); -static int tcp_connect __P((struct tcpcb *, struct sockaddr *, - struct proc *)); +static int tcp_attach(struct socket *, struct proc *); +static int tcp_connect(struct tcpcb *, struct sockaddr *, struct proc *); #if INET6 -static int tcp6_connect __P((struct tcpcb *, struct sockaddr *, - struct proc *)); +static int tcp6_connect(struct tcpcb *, struct sockaddr *, struct proc *); #endif /* INET6 */ -static struct tcpcb * tcp_disconnect __P((struct tcpcb *)); static struct tcpcb * - tcp_usrclosed __P((struct tcpcb *)); + tcp_disconnect(struct tcpcb *); +static struct tcpcb * + tcp_usrclosed(struct tcpcb *); + +__private_extern__ int tcp_win_scale = 3; +SYSCTL_INT(_net_inet_tcp, OID_AUTO, win_scale_factor, CTLFLAG_RW, + &tcp_win_scale, 0, "Window scaling factor"); + +static u_int32_t tcps_in_sw_cksum; +SYSCTL_UINT(_net_inet_tcp, OID_AUTO, in_sw_cksum, CTLFLAG_RD, + &tcps_in_sw_cksum, 0, + "Number of received packets checksummed in software"); + +static u_int64_t tcps_in_sw_cksum_bytes; +SYSCTL_QUAD(_net_inet_tcp, OID_AUTO, in_sw_cksum_bytes, CTLFLAG_RD, + &tcps_in_sw_cksum_bytes, + "Amount of received data checksummed in software"); + +static u_int32_t tcps_out_sw_cksum; +SYSCTL_UINT(_net_inet_tcp, OID_AUTO, out_sw_cksum, CTLFLAG_RD, + &tcps_out_sw_cksum, 0, + "Number of transmitted packets checksummed in software"); + +static u_int64_t tcps_out_sw_cksum_bytes; +SYSCTL_QUAD(_net_inet_tcp, OID_AUTO, out_sw_cksum_bytes, CTLFLAG_RD, + &tcps_out_sw_cksum_bytes, + "Amount of transmitted data checksummed in software"); #if TCPDEBUG -#define TCPDEBUG0 int ostate +#define TCPDEBUG0 int ostate = 0 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ - tcp_trace(TA_USER, ostate, tp, 0, req) + tcp_trace(TA_USER, ostate, tp, 0, 0, req) #else #define TCPDEBUG0 #define TCPDEBUG1() #define TCPDEBUG2(req) #endif +__private_extern__ unsigned int tcp_sockthreshold = 64; +SYSCTL_INT(_net_inet_tcp, OID_AUTO, sockthreshold, CTLFLAG_RW, + &tcp_sockthreshold , 0, "TCP Socket size increased if less than threshold"); + /* * TCP attaches to socket via pru_attach(), reserving space, * and an internet control block. + * + * Returns: 0 Success + * EISCONN + * tcp_attach:ENOBUFS + * tcp_attach:ENOMEM + * tcp_attach:??? [IPSEC specific] */ static int -tcp_usr_attach(struct socket *so, int proto, struct proc *p) +tcp_usr_attach(struct socket *so, __unused int proto, struct proc *p) { - int s = splnet(); int error; struct inpcb *inp = sotoinpcb(so); struct tcpcb *tp = 0; @@ -154,7 +192,6 @@ tcp_usr_attach(struct socket *so, int proto, struct proc *p) tp = sototcpcb(so); out: TCPDEBUG2(PRU_ATTACH); - splx(s); return error; } @@ -168,16 +205,17 @@ out: static int tcp_usr_detach(struct socket *so) { - int s = splnet(); int error = 0; struct inpcb *inp = sotoinpcb(so); struct tcpcb *tp; TCPDEBUG0; - if (inp == 0) { - splx(s); + if (inp == 0 || (inp->inp_state == INPCB_STATE_DEAD)) { return EINVAL; /* XXX */ } +#if 1 + lck_mtx_assert(((struct inpcb *)so->so_pcb)->inpcb_mtx, LCK_MTX_ASSERT_OWNED); +#endif tp = intotcpcb(inp); /* In case we got disconnected from the peer */ if (tp == 0) @@ -186,30 +224,38 @@ tcp_usr_detach(struct socket *so) tp = tcp_disconnect(tp); out: TCPDEBUG2(PRU_DETACH); - splx(s); return error; } #define COMMON_START() TCPDEBUG0; \ do { \ - if (inp == 0) { \ - splx(s); \ + if (inp == 0 || (inp->inp_state == INPCB_STATE_DEAD)) { \ return EINVAL; \ } \ tp = intotcpcb(inp); \ TCPDEBUG1(); \ } while(0) -#define COMMON_END(req) out: TCPDEBUG2(req); splx(s); return error; goto out +#define COMMON_END(req) out: TCPDEBUG2(req); return error; goto out /* * Give the socket an address. + * + * Returns: 0 Success + * EINVAL Invalid argument [COMMON_START] + * EAFNOSUPPORT Address family not supported + * in_pcbbind:EADDRNOTAVAIL Address not available. + * in_pcbbind:EINVAL Invalid argument + * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef] + * in_pcbbind:EACCES Permission denied + * in_pcbbind:EADDRINUSE Address in use + * in_pcbbind:EAGAIN Resource unavailable, try again + * in_pcbbind:EPERM Operation not permitted */ static int tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p) { - int s = splnet(); int error = 0; struct inpcb *inp = sotoinpcb(so); struct tcpcb *tp; @@ -217,6 +263,11 @@ tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p) COMMON_START(); + if (nam->sa_family != 0 && nam->sa_family != AF_INET) { + error = EAFNOSUPPORT; + goto out; + } + /* * Must check for multicast addresses and disallow binding * to them. @@ -238,7 +289,6 @@ tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p) static int tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p) { - int s = splnet(); int error = 0; struct inpcb *inp = sotoinpcb(so); struct tcpcb *tp; @@ -246,6 +296,11 @@ tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p) COMMON_START(); + if (nam->sa_family != 0 && nam->sa_family != AF_INET6) { + error = EAFNOSUPPORT; + goto out; + } + /* * Must check for multicast addresses and disallow binding * to them. @@ -258,8 +313,7 @@ tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p) } inp->inp_vflag &= ~INP_IPV4; inp->inp_vflag |= INP_IPV6; - if (ip6_mapped_addr_on && (inp->inp_flags & IN6P_BINDV6ONLY) == NULL) { - + if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) inp->inp_vflag |= INP_IPV4; else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { @@ -273,17 +327,28 @@ tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p) } } error = in6_pcbbind(inp, nam, p); + if (error) + goto out; COMMON_END(PRU_BIND); } #endif /* INET6 */ /* * Prepare to accept connections. + * + * Returns: 0 Success + * EINVAL [COMMON_START] + * in_pcbbind:EADDRNOTAVAIL Address not available. + * in_pcbbind:EINVAL Invalid argument + * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef] + * in_pcbbind:EACCES Permission denied + * in_pcbbind:EADDRINUSE Address in use + * in_pcbbind:EAGAIN Resource unavailable, try again + * in_pcbbind:EPERM Operation not permitted */ static int tcp_usr_listen(struct socket *so, struct proc *p) { - int s = splnet(); int error = 0; struct inpcb *inp = sotoinpcb(so); struct tcpcb *tp; @@ -300,7 +365,6 @@ tcp_usr_listen(struct socket *so, struct proc *p) static int tcp6_usr_listen(struct socket *so, struct proc *p) { - int s = splnet(); int error = 0; struct inpcb *inp = sotoinpcb(so); struct tcpcb *tp; @@ -308,8 +372,7 @@ tcp6_usr_listen(struct socket *so, struct proc *p) COMMON_START(); if (inp->inp_lport == 0) { inp->inp_vflag &= ~INP_IPV4; - if (ip6_mapped_addr_on && - (inp->inp_flags & IN6P_BINDV6ONLY) == NULL) + if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) inp->inp_vflag |= INP_IPV4; error = in6_pcbbind(inp, (struct sockaddr *)0, p); } @@ -329,14 +392,29 @@ tcp6_usr_listen(struct socket *so, struct proc *p) static int tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p) { - int s = splnet(); int error = 0; struct inpcb *inp = sotoinpcb(so); struct tcpcb *tp; struct sockaddr_in *sinp; - COMMON_START(); + TCPDEBUG0; + if (inp == 0) + return EINVAL; + else if (inp->inp_state == INPCB_STATE_DEAD) { + if (so->so_error) { + error = so->so_error; + so->so_error = 0; + return error; + } else + return EINVAL; + } + tp = intotcpcb(inp); + TCPDEBUG1(); + if (nam->sa_family != 0 && nam->sa_family != AF_INET) { + error = EAFNOSUPPORT; + goto out; + } /* * Must disallow TCP ``connections'' to multicast addresses. */ @@ -347,6 +425,7 @@ tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p) goto out; } + if ((error = tcp_connect(tp, nam, p)) != 0) goto out; error = tcp_output(tp); @@ -357,7 +436,6 @@ tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p) static int tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p) { - int s = splnet(); int error = 0; struct inpcb *inp = sotoinpcb(so); struct tcpcb *tp; @@ -365,6 +443,11 @@ tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p) COMMON_START(); + if (nam->sa_family != 0 && nam->sa_family != AF_INET6) { + error = EAFNOSUPPORT; + goto out; + } + /* * Must disallow TCP ``connections'' to multicast addresses. */ @@ -374,12 +457,13 @@ tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p) error = EAFNOSUPPORT; goto out; } - inp->inp_vflag &= ~INP_IPV4; - inp->inp_vflag |= INP_IPV6; - if (ip6_mapped_addr_on && - IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { + + if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { struct sockaddr_in sin; + if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) + return (EINVAL); + in6_sin6_2_sin(&sin, sin6p); inp->inp_vflag |= INP_IPV4; inp->inp_vflag &= ~INP_IPV6; @@ -388,13 +472,13 @@ tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p) error = tcp_output(tp); goto out; } + inp->inp_vflag &= ~INP_IPV4; + inp->inp_vflag |= INP_IPV6; if ((error = tcp6_connect(tp, nam, p)) != 0) goto out; error = tcp_output(tp); if (error) goto out; - if (ip6_mapped_addr_on) - inp->inp_vflag |= INP_IPV6; COMMON_END(PRU_CONNECT); } #endif /* INET6 */ @@ -413,11 +497,13 @@ tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p) static int tcp_usr_disconnect(struct socket *so) { - int s = splnet(); int error = 0; struct inpcb *inp = sotoinpcb(so); struct tcpcb *tp; - + +#if 1 + lck_mtx_assert(((struct inpcb *)so->so_pcb)->inpcb_mtx, LCK_MTX_ASSERT_OWNED); +#endif COMMON_START(); /* In case we got disconnected from the peer */ if (tp == 0) @@ -434,12 +520,20 @@ tcp_usr_disconnect(struct socket *so) static int tcp_usr_accept(struct socket *so, struct sockaddr **nam) { - int s = splnet(); int error = 0; struct inpcb *inp = sotoinpcb(so); - struct tcpcb *tp; + struct tcpcb *tp = NULL; + TCPDEBUG0; - COMMON_START(); + if (so->so_state & SS_ISDISCONNECTED) { + error = ECONNABORTED; + goto out; + } + if (inp == 0 || (inp->inp_state == INPCB_STATE_DEAD)) { + return (EINVAL); + } + tp = intotcpcb(inp); + TCPDEBUG1(); in_setpeeraddr(so, nam); COMMON_END(PRU_ACCEPT); } @@ -448,12 +542,20 @@ tcp_usr_accept(struct socket *so, struct sockaddr **nam) static int tcp6_usr_accept(struct socket *so, struct sockaddr **nam) { - int s = splnet(); int error = 0; struct inpcb *inp = sotoinpcb(so); - struct tcpcb *tp; + struct tcpcb *tp = NULL; + TCPDEBUG0; - COMMON_START(); + if (so->so_state & SS_ISDISCONNECTED) { + error = ECONNABORTED; + goto out; + } + if (inp == 0 || (inp->inp_state == INPCB_STATE_DEAD)) { + return (EINVAL); + } + tp = intotcpcb(inp); + TCPDEBUG1(); in6_mapped_peeraddr(so, nam); COMMON_END(PRU_ACCEPT); } @@ -461,11 +563,24 @@ tcp6_usr_accept(struct socket *so, struct sockaddr **nam) /* * Mark the connection as being incapable of further output. + * + * Returns: 0 Success + * EINVAL [COMMON_START] + * tcp_output:EADDRNOTAVAIL + * tcp_output:ENOBUFS + * tcp_output:EMSGSIZE + * tcp_output:EHOSTUNREACH + * tcp_output:ENETUNREACH + * tcp_output:ENETDOWN + * tcp_output:ENOMEM + * tcp_output:EACCES + * tcp_output:EMSGSIZE + * tcp_output:ENOBUFS + * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL] */ static int tcp_usr_shutdown(struct socket *so) { - int s = splnet(); int error = 0; struct inpcb *inp = sotoinpcb(so); struct tcpcb *tp; @@ -485,9 +600,8 @@ tcp_usr_shutdown(struct socket *so) * After a receive, possibly send window update to peer. */ static int -tcp_usr_rcvd(struct socket *so, int flags) +tcp_usr_rcvd(struct socket *so, __unused int flags) { - int s = splnet(); int error = 0; struct inpcb *inp = sotoinpcb(so); struct tcpcb *tp; @@ -502,35 +616,80 @@ tcp_usr_rcvd(struct socket *so, int flags) /* * Do a send by putting data in output queue and updating urgent - * marker if URG set. Possibly send more data. + * marker if URG set. Possibly send more data. Unlike the other + * pru_*() routines, the mbuf chains are our responsibility. We + * must either enqueue them or free them. The other pru_* routines + * generally are caller-frees. + * + * Returns: 0 Success + * ECONNRESET + * EINVAL + * ENOBUFS + * tcp_connect:EADDRINUSE Address in use + * tcp_connect:EADDRNOTAVAIL Address not available. + * tcp_connect:EINVAL Invalid argument + * tcp_connect:EAFNOSUPPORT Address family not supported [notdef] + * tcp_connect:EACCES Permission denied + * tcp_connect:EAGAIN Resource unavailable, try again + * tcp_connect:EPERM Operation not permitted + * tcp_output:EADDRNOTAVAIL + * tcp_output:ENOBUFS + * tcp_output:EMSGSIZE + * tcp_output:EHOSTUNREACH + * tcp_output:ENETUNREACH + * tcp_output:ENETDOWN + * tcp_output:ENOMEM + * tcp_output:EACCES + * tcp_output:EMSGSIZE + * tcp_output:ENOBUFS + * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL] + * tcp6_connect:??? [IPV6 only] */ static int -tcp_usr_send(struct socket *so, int flags, struct mbuf *m, +tcp_usr_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, struct mbuf *control, struct proc *p) { - int s = splnet(); int error = 0; struct inpcb *inp = sotoinpcb(so); struct tcpcb *tp; #if INET6 int isipv6; -#endif /* INET6 */ +#endif + TCPDEBUG0; - COMMON_START(); - if (control && control->m_len) { - m_freem(control); /* XXX shouldn't caller do this??? */ + if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) { + /* + * OOPS! we lost a race, the TCP session got reset after + * we checked SS_CANTSENDMORE, eg: while doing uiomove or a + * network interrupt in the non-splnet() section of sosend(). + */ if (m) m_freem(m); - error = EINVAL; + if (control) + m_freem(control); + error = ECONNRESET; /* XXX EPIPE? */ + tp = NULL; + TCPDEBUG1(); goto out; } - #if INET6 isipv6 = nam && nam->sa_family == AF_INET6; #endif /* INET6 */ - + tp = intotcpcb(inp); + TCPDEBUG1(); + if (control) { + /* TCP doesn't do control messages (rights, creds, etc) */ + if (control->m_len) { + m_freem(control); + if (m) + m_freem(m); + error = EINVAL; + goto out; + } + m_freem(control); /* empty control, just free it */ + } if(!(flags & PRUS_OOB)) { - sbappend(&so->so_snd, m); + sbappendstream(&so->so_snd, m); if (nam && tp->t_state < TCPS_SYN_SENT) { /* * Do implied connect if not yet connected, @@ -547,7 +706,7 @@ tcp_usr_send(struct socket *so, int flags, struct mbuf *m, if (error) goto out; tp->snd_wnd = TTCP_CLIENT_SND_WND; - tcp_mss(tp, -1, isipv6); + tcp_mss(tp, -1); } if (flags & PRUS_EOF) { @@ -579,7 +738,7 @@ tcp_usr_send(struct socket *so, int flags, struct mbuf *m, * of data past the urgent section. * Otherwise, snd_up should be one lower. */ - sbappend(&so->so_snd, m); + sbappendstream(&so->so_snd, m); if (nam && tp->t_state < TCPS_SYN_SENT) { /* * Do implied connect if not yet connected, @@ -596,7 +755,7 @@ tcp_usr_send(struct socket *so, int flags, struct mbuf *m, if (error) goto out; tp->snd_wnd = TTCP_CLIENT_SND_WND; - tcp_mss(tp, -1, isipv6); + tcp_mss(tp, -1); } tp->snd_up = tp->snd_una + so->so_snd.sb_cc; tp->t_force = 1; @@ -613,7 +772,6 @@ tcp_usr_send(struct socket *so, int flags, struct mbuf *m, static int tcp_usr_abort(struct socket *so) { - int s = splnet(); int error = 0; struct inpcb *inp = sotoinpcb(so); struct tcpcb *tp; @@ -623,16 +781,21 @@ tcp_usr_abort(struct socket *so) if (tp == 0) goto out; tp = tcp_drop(tp, ECONNABORTED); + so->so_usecount--; COMMON_END(PRU_ABORT); } /* * Receive out-of-band data. + * + * Returns: 0 Success + * EINVAL [COMMON_START] + * EINVAL + * EWOULDBLOCK */ static int tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) { - int s = splnet(); int error = 0; struct inpcb *inp = sotoinpcb(so); struct tcpcb *tp; @@ -662,7 +825,7 @@ struct pr_usrreqs tcp_usrreqs = { tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach, tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd, tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown, - in_setsockaddr, sosend, soreceive, sopoll + in_setsockaddr, sosend, soreceive, pru_sopoll_notsupp }; #if INET6 @@ -671,7 +834,7 @@ struct pr_usrreqs tcp6_usrreqs = { tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach, tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd, tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown, - in6_mapped_sockaddr, sosend, soreceive, sopoll + in6_mapped_sockaddr, sosend, soreceive, pru_sopoll_notsupp }; #endif /* INET6 */ @@ -684,6 +847,20 @@ struct pr_usrreqs tcp6_usrreqs = { * sending CC options and if the connection duration was < MSL, then * truncate the previous TIME-WAIT state and proceed. * Initialize connection parameters and enter SYN-SENT state. + * + * Returns: 0 Success + * EADDRINUSE + * EINVAL + * in_pcbbind:EADDRNOTAVAIL Address not available. + * in_pcbbind:EINVAL Invalid argument + * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef] + * in_pcbbind:EACCES Permission denied + * in_pcbbind:EADDRINUSE Address in use + * in_pcbbind:EAGAIN Resource unavailable, try again + * in_pcbbind:EPERM Operation not permitted + * in_pcbladdr:EINVAL Invalid argument + * in_pcbladdr:EAFNOSUPPORT Address family not supported + * in_pcbladdr:EADDRNOTAVAIL Address not available */ static int tcp_connect(tp, nam, p) @@ -714,42 +891,79 @@ tcp_connect(tp, nam, p) error = in_pcbladdr(inp, nam, &ifaddr); if (error) return error; + + tcp_unlock(inp->inp_socket, 0, 0); oinp = in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port, inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr : ifaddr->sin_addr, inp->inp_lport, 0, NULL); + + tcp_lock(inp->inp_socket, 0, 0); if (oinp) { + if (oinp != inp) /* 4143933: avoid deadlock if inp == oinp */ + tcp_lock(oinp->inp_socket, 1, 0); + if (in_pcb_checkstate(oinp, WNT_RELEASE, 1) == WNT_STOPUSING) { + if (oinp != inp) + tcp_unlock(oinp->inp_socket, 1, 0); + goto skip_oinp; + } + if (oinp != inp && (otp = intotcpcb(oinp)) != NULL && otp->t_state == TCPS_TIME_WAIT && - otp->t_duration < TCPTV_MSL && + otp->t_starttime < (u_long)tcp_msl && (otp->t_flags & TF_RCVD_CC)) otp = tcp_close(otp); - else + else { + printf("tcp_connect: inp=%p err=EADDRINUSE\n", inp); + if (oinp != inp) + tcp_unlock(oinp->inp_socket, 1, 0); return EADDRINUSE; + } + if (oinp != inp) + tcp_unlock(oinp->inp_socket, 1, 0); + } +skip_oinp: + if ((inp->inp_laddr.s_addr == INADDR_ANY ? ifaddr->sin_addr.s_addr : + inp->inp_laddr.s_addr) == sin->sin_addr.s_addr && + inp->inp_lport == sin->sin_port) + return EINVAL; + if (!lck_rw_try_lock_exclusive(inp->inp_pcbinfo->mtx)) { + /*lock inversion issue, mostly with udp multicast packets */ + socket_unlock(inp->inp_socket, 0); + lck_rw_lock_exclusive(inp->inp_pcbinfo->mtx); + socket_lock(inp->inp_socket, 0); } if (inp->inp_laddr.s_addr == INADDR_ANY) inp->inp_laddr = ifaddr->sin_addr; inp->inp_faddr = sin->sin_addr; inp->inp_fport = sin->sin_port; in_pcbrehash(inp); + lck_rw_done(inp->inp_pcbinfo->mtx); - tp->t_template = tcp_template(tp); - if (tp->t_template == 0) { - in_pcbdisconnect(inp); - return ENOBUFS; - } - - /* Compute window scaling to request. */ + /* Compute window scaling to requesti according to sb_hiwat + * or leave us some room to increase potentially increase the window size depending + * on the default win scale + */ while (tp->request_r_scale < TCP_MAX_WINSHIFT && - (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) + (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) tp->request_r_scale++; + /* + * Inflate window size only if no setsockopt was performed on the recv sockbuf and + * if we're not over our number of active pcbs. + */ + + if (((so->so_rcv.sb_flags & SB_USRSIZE) == 0) && (inp->inp_pcbinfo->ipi_count < tcp_sockthreshold)) { + tp->request_r_scale = max(tcp_win_scale, tp->request_r_scale); + so->so_rcv.sb_hiwat = min(TCP_MAXWIN << tp->request_r_scale, (sb_max / (MSIZE+MCLBYTES)) * MCLBYTES); + } + soisconnecting(so); tcpstat.tcps_connattempt++; tp->t_state = TCPS_SYN_SENT; tp->t_timer[TCPT_KEEP] = tcp_keepinit; - tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; + tp->iss = tcp_new_isn(tp); tcp_sendseqinit(tp); /* @@ -784,7 +998,7 @@ tcp6_connect(tp, nam, p) struct socket *so = inp->inp_socket; struct tcpcb *otp; struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; - struct in6_addr *addr6; + struct in6_addr addr6; struct rmxp_tao *taop; struct rmxp_tao tao_noncached; int error; @@ -803,38 +1017,37 @@ tcp6_connect(tp, nam, p) error = in6_pcbladdr(inp, nam, &addr6); if (error) return error; + tcp_unlock(inp->inp_socket, 0, 0); oinp = in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr, sin6->sin6_port, IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) - ? addr6 + ? &addr6 : &inp->in6p_laddr, inp->inp_lport, 0, NULL); + tcp_lock(inp->inp_socket, 0, 0); if (oinp) { if (oinp != inp && (otp = intotcpcb(oinp)) != NULL && otp->t_state == TCPS_TIME_WAIT && - otp->t_duration < TCPTV_MSL && + otp->t_starttime < (u_long)tcp_msl && (otp->t_flags & TF_RCVD_CC)) otp = tcp_close(otp); else return EADDRINUSE; } + if (!lck_rw_try_lock_exclusive(inp->inp_pcbinfo->mtx)) { + /*lock inversion issue, mostly with udp multicast packets */ + socket_unlock(inp->inp_socket, 0); + lck_rw_lock_exclusive(inp->inp_pcbinfo->mtx); + socket_lock(inp->inp_socket, 0); + } if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) - inp->in6p_laddr = *addr6; + inp->in6p_laddr = addr6; inp->in6p_faddr = sin6->sin6_addr; inp->inp_fport = sin6->sin6_port; - /* - * xxx kazu flowlabel is necessary for connect? - * but if this line is missing, the garbage value remains. - */ - inp->in6p_flowinfo = sin6->sin6_flowinfo; - + if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != 0) + inp->in6p_flowinfo = sin6->sin6_flowinfo; in_pcbrehash(inp); - - tp->t_template = tcp_template(tp); - if (tp->t_template == 0) { - in6_pcbdisconnect(inp); - return ENOBUFS; - } + lck_rw_done(inp->inp_pcbinfo->mtx); /* Compute window scaling to request. */ while (tp->request_r_scale < TCP_MAX_WINSHIFT && @@ -845,7 +1058,7 @@ tcp6_connect(tp, nam, p) tcpstat.tcps_connattempt++; tp->t_state = TCPS_SYN_SENT; tp->t_timer[TCPT_KEEP] = tcp_keepinit; - tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; + tp->iss = tcp_new_isn(tp); tcp_sendseqinit(tp); /* @@ -882,15 +1095,13 @@ tcp_ctloutput(so, sopt) struct socket *so; struct sockopt *sopt; { - int error, opt, optval, s; + int error, opt, optval; struct inpcb *inp; struct tcpcb *tp; error = 0; - s = splnet(); /* XXX */ inp = sotoinpcb(so); if (inp == NULL) { - splx(s); return (ECONNRESET); } if (sopt->sopt_level != IPPROTO_TCP) { @@ -900,12 +1111,10 @@ tcp_ctloutput(so, sopt) else #endif /* INET6 */ error = ip_ctloutput(so, sopt); - splx(s); return (error); } tp = intotcpcb(inp); if (tp == NULL) { - splx(s); return (ECONNRESET); } @@ -947,12 +1156,26 @@ tcp_ctloutput(so, sopt) if (error) break; - if (optval > 0 && optval <= tp->t_maxseg) + if (optval > 0 && optval <= tp->t_maxseg && + optval + 40 >= tcp_minmss) tp->t_maxseg = optval; else error = EINVAL; break; + case TCP_KEEPALIVE: + error = sooptcopyin(sopt, &optval, sizeof optval, + sizeof optval); + if (error) + break; + if (optval < 0) + error = EINVAL; + else { + tp->t_keepidle = optval * TCP_RETRANSHZ; + tp->t_timer[TCPT_KEEP] = TCP_KEEPIDLE(tp); /* reset the timer to new value */ + } + break; + default: error = ENOPROTOOPT; break; @@ -967,6 +1190,9 @@ tcp_ctloutput(so, sopt) case TCP_MAXSEG: optval = tp->t_maxseg; break; + case TCP_KEEPALIVE: + optval = tp->t_keepidle / TCP_RETRANSHZ; + break; case TCP_NOOPT: optval = tp->t_flags & TF_NOOPT; break; @@ -981,7 +1207,6 @@ tcp_ctloutput(so, sopt) error = sooptcopyout(sopt, &optval, sizeof optval); break; } - splx(s); return (error); } @@ -990,17 +1215,24 @@ tcp_ctloutput(so, sopt) * sizes, respectively. These are obsolescent (this information should * be set by the route). */ -u_long tcp_sendspace = 1024*16; -SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, - CTLFLAG_RW, &tcp_sendspace , 0, ""); -u_long tcp_recvspace = 1024*16; -SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, - CTLFLAG_RW, &tcp_recvspace , 0, ""); +u_long tcp_sendspace = 1448*256; +SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW, + &tcp_sendspace , 0, "Maximum outgoing TCP datagram size"); +u_long tcp_recvspace = 1448*384; +SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, + &tcp_recvspace , 0, "Maximum incoming TCP datagram size"); + /* * Attach TCP protocol to socket, allocating * internet protocol control block, tcp control block, * bufer space, and entering LISTEN state if to accept connections. + * + * Returns: 0 Success + * in_pcballoc:ENOBUFS + * in_pcballoc:ENOMEM + * in_pcballoc:??? [IPSEC specific] + * soreserve:ENOBUFS */ static int tcp_attach(so, p) @@ -1010,31 +1242,40 @@ tcp_attach(so, p) register struct tcpcb *tp; struct inpcb *inp; int error; + u_long sb_effective_max; #if INET6 - int isipv6 = INP_CHECK_SOCKAF(so, AF_INET) == NULL; -#endif /* INET6 */ + int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0; +#endif - if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { - error = soreserve(so, tcp_sendspace, tcp_recvspace); - if (error) - return (error); - } error = in_pcballoc(so, &tcbinfo, p); if (error) return (error); + inp = sotoinpcb(so); -#if IPSEC - error = ipsec_init_policy(so, &inp->inp_sp); - if (error) { -#if INET6 - if (isipv6) - in6_pcbdetach(inp); - else -#endif /* INET6 */ - in_pcbdetach(inp); - return (error); + + if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { + /* + * The goal is to let clients machines use large send/rcv default windows to compensate for link + * latency and make sure the receiver is not constraining the sender window. + * But we doon't want to have a few connections use all our mbuf space for servers. + * This is done by watching a threshold of tcpcbs in use and bumping the default send and rcvspace + * only if that threshold isn't reached. + * We're also advertising a much bigger window size (tuneable by sysctl) in correlation with * the max socket buffer size if + * we consider that we have enough ressources for it. This window will be adjusted depending on the + * global socket layer buffer use with the use of tcp_sbpace + */ + + if (inp->inp_pcbinfo->ipi_count < tcp_sockthreshold) { + sb_effective_max = (sb_max / (MSIZE+MCLBYTES)) * MCLBYTES; + error = soreserve(so, max(min((TCP_MAXWIN << tcp_win_scale)/4, sb_effective_max), tcp_sendspace), + max(min((TCP_MAXWIN << tcp_win_scale)/2, sb_effective_max), tcp_recvspace)); + } + else + error = soreserve(so, tcp_sendspace, tcp_recvspace); + if (error) + return (error); } -#endif /*IPSEC*/ + #if INET6 if (isipv6) { inp->inp_vflag |= INP_IPV6; @@ -1134,3 +1375,16 @@ tcp_usrclosed(tp) return (tp); } +void +tcp_in_cksum_stats(u_int32_t len) +{ + tcps_in_sw_cksum++; + tcps_in_sw_cksum_bytes += len; +} + +void +tcp_out_cksum_stats(u_int32_t len) +{ + tcps_out_sw_cksum++; + tcps_out_sw_cksum_bytes += len; +}