From nobody Thu Dec 05 15:20:48 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Y3yl52CcPz5gdLn; Thu, 05 Dec 2024 15:20:49 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Y3yl50NHQz56Xh; Thu, 5 Dec 2024 15:20:49 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1733412049; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=lHNWCKh2ODYhaT+Z8oXSdmlUUj5kr2I2AYJhJo84rmI=; b=GIcLTDylBV8GvYS1DvOTojuAEb03U4SUYIHd1fsR1BozimlhZnjHSC/VonVXZuo3DsJ/7A pyqjcIZw7hP/vZK3rq4+EeplhCdszLD+jTea9ZiZ0/eJ9h2KihT62smvaoSyrs4LaOFbue n4aDB/AFO0NzBpBMY1EK5FDPgZbyf7Syh/6Zv8mZnrTkfHlECl9/QlXq9aTi6xbP37TwiI W5TL9/HMWzgLebQxiXVdhkhUYUW3alU7B4WQs/QcsY38oL6j2qjCGUYesmNMnCj70iz2Hx 4ZeTwcohJ59d/UqVyg2/PusLSwZRgm49iU33rL7docPkHE4g2K9Ncw4BZHiYBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1733412049; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=lHNWCKh2ODYhaT+Z8oXSdmlUUj5kr2I2AYJhJo84rmI=; b=IM0iZYOD+HJqpL4y3l5kZMnTO6P9L44S5Kw5EPwIVWGTBWM7Y5IszPaNyy/Of3Tyyv0cHP seZd1Gu++Lg5ihFPzVFMS9kotXafaqroMWWbSZJYCfoDUIjnw3otm7vTQi00kwDj9eI3WJ GcJX91V2KGxK+L1FWIgsnErJBKCFZovUc9h1iVXKI2bGBQARIuiFFJeRt4Ok0JslZiADy4 aLnENGh+gG9lqfjzTm82hbS0s7gF7Za4CYJz0xuqRdKhApBbeOb6Hns3FsEtyXl5CLtZEa inHfTz5fDw7q3+78VPQdwj/dfJgMrwNxWTYEs/RDZt+0xzqg+iEYY79BmuDajQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1733412049; a=rsa-sha256; cv=none; b=h6WBhszcFuapjjrTU2vIh+cMXaknSJmEkuc3JW87Tsv0qzqdPlTLeouCl1VFgOANjZTTjH VaPO2WO98T4z/hRvRRN+mUWuPwInRn8JaoUxVZO0X6kBf0bYk3h/zA99keNqK1e6ax2e3h rGTWTv402u+H5XH4HM/dkFEIpzI9E+CocYPbOQ0UtNwZE1LpHYqKpVS3Kju5QHg6Bzzudq 7jhQ0uUEAxR0uFMDIGzmerhSD82NyuYf1B2E7OCsjNNNvOzp2WlnFLK/E+m09cJ8MUKQH1 MtBacu557dZXBS20RnwGO00RF35tAFjPawLbWZShRdSi7ezz9mwWsZN3buZQng== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Y3yl454sRzxg2; Thu, 5 Dec 2024 15:20:48 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 4B5FKmcI099694; Thu, 5 Dec 2024 15:20:48 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4B5FKmT0099691; Thu, 5 Dec 2024 15:20:48 GMT (envelope-from git) Date: Thu, 5 Dec 2024 15:20:48 GMT Message-Id: <202412051520.4B5FKmT0099691@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 01f8ce83242d - main - inpcb: Factor out parts of in6_pcbbind() and in_pcbbind_setup() List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 01f8ce83242d7a8e599cf6a78b6277161d79edd4 Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=01f8ce83242d7a8e599cf6a78b6277161d79edd4 commit 01f8ce83242d7a8e599cf6a78b6277161d79edd4 Author: Mark Johnston AuthorDate: 2024-12-05 15:00:13 +0000 Commit: Mark Johnston CommitDate: 2024-12-05 15:19:57 +0000 inpcb: Factor out parts of in6_pcbbind() and in_pcbbind_setup() A large portion of these functions just determines whether the inpcb can bind to the address/port. This portion has no side effects, so is a good candidate to move into its own helper function. This patch does so, making the callers less complicated and reducing indentation. While moving this code, also make some changes: - Load socket options (SO_REUSEADDR etc.) only once. There is nothing preventing another thread from toggling the socket options, so make this function easier to reason about by avoiding races. - When checking whether the bind address is an interface address, make a separate sockaddr rather than temporarily modifying the one passed to in_pcbbind(). Reviewed by: ae, glebius MFC after: 1 month Sponsored by: Klara, Inc. Sponsored by: Stormshield Differential Revision: https://reviews.freebsd.org/D47590 --- sys/netinet/in_pcb.c | 172 ++++++++++++++++++-------------- sys/netinet6/in6_pcb.c | 262 +++++++++++++++++++++++++++---------------------- 2 files changed, 242 insertions(+), 192 deletions(-) diff --git a/sys/netinet/in_pcb.c b/sys/netinet/in_pcb.c index 7c881817ac6e..94fb406bcd2b 100644 --- a/sys/netinet/in_pcb.c +++ b/sys/netinet/in_pcb.c @@ -861,6 +861,93 @@ in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp, #endif /* INET || INET6 */ #ifdef INET +/* + * Determine whether the inpcb can be bound to the specified address/port tuple. + */ +static int +in_pcbbind_avail(struct inpcb *inp, const struct in_addr laddr, + const u_short lport, int sooptions, int lookupflags, struct ucred *cred) +{ + int reuseport, reuseport_lb; + + INP_LOCK_ASSERT(inp); + INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); + + reuseport = (sooptions & SO_REUSEPORT); + reuseport_lb = (sooptions & SO_REUSEPORT_LB); + + if (IN_MULTICAST(ntohl(laddr.s_addr))) { + /* + * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; + * allow complete duplication of binding if + * SO_REUSEPORT is set, or if SO_REUSEADDR is set + * and a multicast address is bound on both + * new and duplicated sockets. + */ + if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT)) != 0) + reuseport = SO_REUSEADDR | SO_REUSEPORT; + /* + * XXX: How to deal with SO_REUSEPORT_LB here? + * Treat same as SO_REUSEPORT for now. + */ + if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT_LB)) != 0) + reuseport_lb = SO_REUSEADDR | SO_REUSEPORT_LB; + } else if (!in_nullhost(laddr)) { + struct sockaddr_in sin; + + memset(&sin, 0, sizeof(sin)); + sin.sin_family = AF_INET; + sin.sin_len = sizeof(sin); + sin.sin_addr = laddr; + + /* + * Is the address a local IP address? + * If INP_BINDANY is set, then the socket may be bound + * to any endpoint address, local or not. + */ + if ((inp->inp_flags & INP_BINDANY) == 0 && + ifa_ifwithaddr_check((const struct sockaddr *)&sin) == 0) + return (EADDRNOTAVAIL); + } + + if (lport != 0) { + struct inpcb *t; + + if (ntohs(lport) <= V_ipport_reservedhigh && + ntohs(lport) >= V_ipport_reservedlow && + priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT)) + return (EACCES); + + if (!IN_MULTICAST(ntohl(laddr.s_addr)) && + priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) { + t = in_pcblookup_local(inp->inp_pcbinfo, laddr, lport, + INPLOOKUP_WILDCARD, cred); + if (t != NULL && + (inp->inp_socket->so_type != SOCK_STREAM || + in_nullhost(t->inp_faddr)) && + (!in_nullhost(laddr) || + !in_nullhost(t->inp_laddr) || + (t->inp_socket->so_options & SO_REUSEPORT) || + (t->inp_socket->so_options & SO_REUSEPORT_LB) == 0) && + (inp->inp_cred->cr_uid != t->inp_cred->cr_uid)) + return (EADDRINUSE); + } + t = in_pcblookup_local(inp->inp_pcbinfo, laddr, lport, + lookupflags, cred); + if (t != NULL && ((reuseport | reuseport_lb) & + t->inp_socket->so_options) == 0) { +#ifdef INET6 + if (!in_nullhost(laddr) || + !in_nullhost(t->inp_laddr) || + (inp->inp_vflag & INP_IPV6PROTO) == 0 || + (t->inp_vflag & INP_IPV6PROTO) == 0) +#endif + return (EADDRINUSE); + } + } + return (0); +} + /* * Set up a bind operation on a PCB, performing port allocation * as required, but do not actually modify the PCB. Callers can @@ -878,15 +965,9 @@ in_pcbbind_setup(struct inpcb *inp, struct sockaddr_in *sin, in_addr_t *laddrp, struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; struct in_addr laddr; u_short lport = 0; - int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT); + int lookupflags, sooptions; int error; - /* - * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here - * so that we don't have to add to the (already messy) code below. - */ - int reuseport_lb = (so->so_options & SO_REUSEPORT_LB); - /* * No state changes, so read locks are sufficient here. */ @@ -896,7 +977,10 @@ in_pcbbind_setup(struct inpcb *inp, struct sockaddr_in *sin, in_addr_t *laddrp, laddr.s_addr = *laddrp; if (sin != NULL && laddr.s_addr != INADDR_ANY) return (EINVAL); - if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0) + + lookupflags = 0; + sooptions = atomic_load_int(&so->so_options); + if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT | SO_REUSEPORT_LB)) == 0) lookupflags = INPLOOKUP_WILDCARD; if (sin == NULL) { if ((error = prison_local_ip4(cred, &laddr)) != 0) @@ -918,73 +1002,11 @@ in_pcbbind_setup(struct inpcb *inp, struct sockaddr_in *sin, in_addr_t *laddrp, } laddr = sin->sin_addr; - /* NB: lport is left as 0 if the port isn't being changed. */ - if (IN_MULTICAST(ntohl(laddr.s_addr))) { - /* - * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; - * allow complete duplication of binding if - * SO_REUSEPORT is set, or if SO_REUSEADDR is set - * and a multicast address is bound on both - * new and duplicated sockets. - */ - if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0) - reuseport = SO_REUSEADDR|SO_REUSEPORT; - /* - * XXX: How to deal with SO_REUSEPORT_LB here? - * Treat same as SO_REUSEPORT for now. - */ - if ((so->so_options & - (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0) - reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB; - } else if (!in_nullhost(laddr)) { - sin->sin_port = 0; /* yech... */ - bzero(&sin->sin_zero, sizeof(sin->sin_zero)); - /* - * Is the address a local IP address? - * If INP_BINDANY is set, then the socket may be bound - * to any endpoint address, local or not. - */ - if ((inp->inp_flags & INP_BINDANY) == 0 && - ifa_ifwithaddr_check( - (const struct sockaddr *)sin) == 0) - return (EADDRNOTAVAIL); - } - if (lport) { - struct inpcb *t; - - if (ntohs(lport) <= V_ipport_reservedhigh && - ntohs(lport) >= V_ipport_reservedlow && - priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT)) - return (EACCES); - - if (!IN_MULTICAST(ntohl(laddr.s_addr)) && - priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) { - t = in_pcblookup_local(pcbinfo, laddr, lport, - INPLOOKUP_WILDCARD, cred); - if (t != NULL && - (so->so_type != SOCK_STREAM || - in_nullhost(t->inp_faddr)) && - (!in_nullhost(laddr) || - !in_nullhost(t->inp_laddr) || - (t->inp_socket->so_options & SO_REUSEPORT) || - (t->inp_socket->so_options & SO_REUSEPORT_LB) == 0) && - (inp->inp_cred->cr_uid != - t->inp_cred->cr_uid)) - return (EADDRINUSE); - } - t = in_pcblookup_local(pcbinfo, laddr, lport, - lookupflags, cred); - if (t != NULL && ((reuseport | reuseport_lb) & - t->inp_socket->so_options) == 0) { -#ifdef INET6 - if (!in_nullhost(laddr) || - !in_nullhost(t->inp_laddr) || - (inp->inp_vflag & INP_IPV6PROTO) == 0 || - (t->inp_vflag & INP_IPV6PROTO) == 0) -#endif - return (EADDRINUSE); - } - } + /* See if this address/port combo is available. */ + error = in_pcbbind_avail(inp, laddr, lport, sooptions, + lookupflags, cred); + if (error != 0) + return (error); } if (*lportp != 0) lport = *lportp; diff --git a/sys/netinet6/in6_pcb.c b/sys/netinet6/in6_pcb.c index 639ec68e19e2..6b5a9d3706be 100644 --- a/sys/netinet6/in6_pcb.c +++ b/sys/netinet6/in6_pcb.c @@ -160,27 +160,157 @@ in6_pcbsetport(struct in6_addr *laddr, struct inpcb *inp, struct ucred *cred) return (0); } +/* + * Determine whether the inpcb can be bound to the specified address/port tuple. + */ +static int +in6_pcbbind_avail(struct inpcb *inp, const struct sockaddr_in6 *sin6, + int sooptions, int lookupflags, struct ucred *cred) +{ + const struct in6_addr *laddr; + int reuseport, reuseport_lb; + u_short lport; + + INP_LOCK_ASSERT(inp); + INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo); + + laddr = &sin6->sin6_addr; + lport = sin6->sin6_port; + + reuseport = (sooptions & SO_REUSEPORT); + reuseport_lb = (sooptions & SO_REUSEPORT_LB); + + if (IN6_IS_ADDR_MULTICAST(laddr)) { + /* + * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; + * allow compepte duplication of binding if + * SO_REUSEPORT is set, or if SO_REUSEADDR is set + * and a multicast address is bound on both + * new and duplicated sockets. + */ + if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT)) != 0) + reuseport = SO_REUSEADDR | SO_REUSEPORT; + /* + * XXX: How to deal with SO_REUSEPORT_LB here? + * Treat same as SO_REUSEPORT for now. + */ + if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT_LB)) != 0) + reuseport_lb = SO_REUSEADDR | SO_REUSEPORT_LB; + } else if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) { + struct sockaddr_in6 sin6; + struct epoch_tracker et; + struct ifaddr *ifa; + + memset(&sin6, 0, sizeof(sin6)); + sin6.sin6_family = AF_INET6; + sin6.sin6_len = sizeof(sin6); + sin6.sin6_addr = *laddr; + + NET_EPOCH_ENTER(et); + if ((ifa = ifa_ifwithaddr((const struct sockaddr *)&sin6)) == + NULL && (inp->inp_flags & INP_BINDANY) == 0) { + NET_EPOCH_EXIT(et); + return (EADDRNOTAVAIL); + } + + /* + * XXX: bind to an anycast address might accidentally + * cause sending a packet with anycast source address. + * We should allow to bind to a deprecated address, since + * the application dares to use it. + */ + if (ifa != NULL && + ((struct in6_ifaddr *)ifa)->ia6_flags & + (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY | IN6_IFF_DETACHED)) { + NET_EPOCH_EXIT(et); + return (EADDRNOTAVAIL); + } + NET_EPOCH_EXIT(et); + } + + if (lport != 0) { + struct inpcb *t; + + if (ntohs(lport) <= V_ipport_reservedhigh && + ntohs(lport) >= V_ipport_reservedlow && + priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT)) + return (EACCES); + + if (!IN6_IS_ADDR_MULTICAST(laddr) && + priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != + 0) { + t = in6_pcblookup_local(inp->inp_pcbinfo, laddr, lport, + INPLOOKUP_WILDCARD, cred); + if (t != NULL && + (inp->inp_socket->so_type != SOCK_STREAM || + IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) && + (!IN6_IS_ADDR_UNSPECIFIED(laddr) || + !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) || + (t->inp_socket->so_options & SO_REUSEPORT) || + (t->inp_socket->so_options & SO_REUSEPORT_LB) == 0) && + (inp->inp_cred->cr_uid != t->inp_cred->cr_uid)) + return (EADDRINUSE); + +#ifdef INET + if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && + IN6_IS_ADDR_UNSPECIFIED(laddr)) { + struct sockaddr_in sin; + + in6_sin6_2_sin(&sin, sin6); + t = in_pcblookup_local(inp->inp_pcbinfo, + sin.sin_addr, lport, INPLOOKUP_WILDCARD, + cred); + if (t != NULL && + (inp->inp_socket->so_type != SOCK_STREAM || + in_nullhost(t->inp_faddr)) && + (inp->inp_cred->cr_uid != + t->inp_cred->cr_uid)) + return (EADDRINUSE); + } +#endif + } + t = in6_pcblookup_local(inp->inp_pcbinfo, laddr, lport, + lookupflags, cred); + if (t != NULL && ((reuseport | reuseport_lb) & + t->inp_socket->so_options) == 0) + return (EADDRINUSE); +#ifdef INET + if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && + IN6_IS_ADDR_UNSPECIFIED(laddr)) { + struct sockaddr_in sin; + + in6_sin6_2_sin(&sin, sin6); + t = in_pcblookup_local(inp->inp_pcbinfo, sin.sin_addr, + lport, lookupflags, cred); + if (t != NULL && ((reuseport | reuseport_lb) & + t->inp_socket->so_options) == 0 && + (!in_nullhost(t->inp_laddr) || + (t->inp_vflag & INP_IPV6PROTO) != 0)) { + return (EADDRINUSE); + } + } +#endif + } + return (0); +} + int in6_pcbbind(struct inpcb *inp, struct sockaddr_in6 *sin6, struct ucred *cred) { struct socket *so = inp->inp_socket; struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; u_short lport = 0; - int error, lookupflags = 0; - int reuseport = (so->so_options & SO_REUSEPORT); - - /* - * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here - * so that we don't have to add to the (already messy) code below. - */ - int reuseport_lb = (so->so_options & SO_REUSEPORT_LB); + int error, lookupflags, sooptions; INP_WLOCK_ASSERT(inp); INP_HASH_WLOCK_ASSERT(pcbinfo); if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) return (EINVAL); - if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0) + + lookupflags = 0; + sooptions = atomic_load_int(&so->so_options); + if ((sooptions & (SO_REUSEADDR | SO_REUSEPORT | SO_REUSEPORT_LB)) == 0) lookupflags = INPLOOKUP_WILDCARD; if (sin6 == NULL) { if ((error = prison_local_ip6(cred, &inp->in6p_laddr, @@ -199,115 +329,13 @@ in6_pcbbind(struct inpcb *inp, struct sockaddr_in6 *sin6, struct ucred *cred) ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0) return (error); - lport = sin6->sin6_port; - if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { - /* - * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; - * allow compepte duplication of binding if - * SO_REUSEPORT is set, or if SO_REUSEADDR is set - * and a multicast address is bound on both - * new and duplicated sockets. - */ - if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0) - reuseport = SO_REUSEADDR|SO_REUSEPORT; - /* - * XXX: How to deal with SO_REUSEPORT_LB here? - * Treat same as SO_REUSEPORT for now. - */ - if ((so->so_options & - (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0) - reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB; - } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { - struct epoch_tracker et; - struct ifaddr *ifa; - - sin6->sin6_port = 0; /* yech... */ - NET_EPOCH_ENTER(et); - if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) == - NULL && - (inp->inp_flags & INP_BINDANY) == 0) { - NET_EPOCH_EXIT(et); - return (EADDRNOTAVAIL); - } - - /* - * XXX: bind to an anycast address might accidentally - * cause sending a packet with anycast source address. - * We should allow to bind to a deprecated address, since - * the application dares to use it. - */ - if (ifa != NULL && - ((struct in6_ifaddr *)ifa)->ia6_flags & - (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) { - NET_EPOCH_EXIT(et); - return (EADDRNOTAVAIL); - } - NET_EPOCH_EXIT(et); - } - if (lport) { - struct inpcb *t; - - if (ntohs(lport) <= V_ipport_reservedhigh && - ntohs(lport) >= V_ipport_reservedlow && - priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT)) - return (EACCES); - - if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) && - priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) { - t = in6_pcblookup_local(pcbinfo, - &sin6->sin6_addr, lport, - INPLOOKUP_WILDCARD, cred); - if (t != NULL && - (so->so_type != SOCK_STREAM || - IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) && - (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || - !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) || - (t->inp_socket->so_options & SO_REUSEPORT) || - (t->inp_socket->so_options & SO_REUSEPORT_LB) == 0) && - (inp->inp_cred->cr_uid != - t->inp_cred->cr_uid)) - return (EADDRINUSE); - -#ifdef INET - if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && - IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { - struct sockaddr_in sin; - - in6_sin6_2_sin(&sin, sin6); - t = in_pcblookup_local(pcbinfo, - sin.sin_addr, lport, - INPLOOKUP_WILDCARD, cred); - if (t != NULL && - (so->so_type != SOCK_STREAM || - in_nullhost(t->inp_faddr)) && - (inp->inp_cred->cr_uid != - t->inp_cred->cr_uid)) - return (EADDRINUSE); - } -#endif - } - t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr, - lport, lookupflags, cred); - if (t != NULL && ((reuseport | reuseport_lb) & - t->inp_socket->so_options) == 0) - return (EADDRINUSE); -#ifdef INET - if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 && - IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { - struct sockaddr_in sin; + /* See if this address/port combo is available. */ + error = in6_pcbbind_avail(inp, sin6, sooptions, lookupflags, + cred); + if (error != 0) + return (error); - in6_sin6_2_sin(&sin, sin6); - t = in_pcblookup_local(pcbinfo, sin.sin_addr, - lport, lookupflags, cred); - if (t != NULL && ((reuseport | reuseport_lb) & - t->inp_socket->so_options) == 0 && - (!in_nullhost(t->inp_laddr) || - (t->inp_vflag & INP_IPV6PROTO) != 0)) { - return (EADDRINUSE); - } - } -#endif - } + lport = sin6->sin6_port; inp->in6p_laddr = sin6->sin6_addr; } if (lport == 0) {