From nobody Sat Jan 4 14:08:17 2025 X-Original-To: dev-commits-src-all@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 4YQMjY341Jz5kJg6; Sat, 04 Jan 2025 14:08:17 +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 "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4YQMjY2R27z4Tsd; Sat, 4 Jan 2025 14:08:17 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1735999697; 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=nahuYwyEFXB4itsdpcM0hoDpRwSsbbphfW3nYJm+gjo=; b=eSkLGmECT+2YguCNWj/0WQ91+MjR5pZfzcomvJmJ/PqUt+Z5wK/Hf/uGY0T5GmDO6EUz/7 bcGa9HEXWRzaBfGzhuswgEyzYAXH5ntmcX6mEYYG/SPHqVx+FN+ZAvk+RG17wT1W/osWaY wzR7HNjkc53Pl8u28vKgHft3dKoM7sSkntgkouT2SUMRVFd9G1z5V+Vay0TEr592SV0nP0 xJlVNa0ITDkfnz/fbWOiJruPJpSfXrKkeWx3OvdFXuPEVgBGD2ePM5tPk5Dwws3neK5f3C Z2pwB9H912jKBjT6Ph98SYyQy7rpFNosS0XRGXrtVlLtsLM+NM1GMNRjMFDMIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1735999697; 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=nahuYwyEFXB4itsdpcM0hoDpRwSsbbphfW3nYJm+gjo=; b=uFL//A64diCgFVIh3zjSzePTt/ZdvYa87GT1ri/SK4cl93onycZNQpTMlZJoVq3hAAUv6d 7QnjgGqWt8lz5xjYF76hxMs8AjU90IM35jIAaCpNEPQVBZsLP//pCqgN87YABI1afHb5z4 aPxesbtSUVp4oohvZ0UZ9+y/HtNlzm/1aW3H82ivO0jzx1RJoQfeFjPyf2yjWBQyKW/9u+ zPr6n8MY9Lxs6pq8dKWjdHu4kn1o9aJuGd9UPvs7mLiykF2DG2La821bzJcxKDYCmyoNdw azy92JVCohDO43WLPlzgdqAQJiLMgMzqH1DGM5rXfeUhhDq96CBBCFpWPVXEOw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1735999697; a=rsa-sha256; cv=none; b=NKGj2xYljWgUNoYhRANBSsR77n1qxzy2nojz/PKimnR5/89vL+IBfGJwcFgMHeEYjt3PxQ PbwRAd3gI0FO1w1jLi+wIceDFlSwTt19niR9YYWcSR8y9eJJj+KFkB9/F+3H15KglG7LbI BCOS0PoBAbUTHjQgC3UuTDPgc7qwOlBEObS0YadWeXAdRzI8QHXjmDkqxOLVjKNJ2UJEZB nScB/hh3T9/sf2pDJDUx9zk0DifpvQ9ENymchRfGnygY1MZdVShVr5JoPEjMpP18qcU0Nv T7uNPH6d3/5ctucvg+hNIaffmUFzmj1u1weVz9f+0a6nhSK/yO8rrCwGShy2lw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none 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 4YQMjY1qLmzvpG; Sat, 04 Jan 2025 14:08:17 +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 504E8HGd070544; Sat, 4 Jan 2025 14:08:17 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 504E8Hh4070541; Sat, 4 Jan 2025 14:08:17 GMT (envelope-from git) Date: Sat, 4 Jan 2025 14:08:17 GMT Message-Id: <202501041408.504E8Hh4070541@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: 4e793a6be4f9 - stable/14 - inpcb: Factor out parts of in6_pcbbind() and in_pcbbind_setup() List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@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/stable/14 X-Git-Reftype: branch X-Git-Commit: 4e793a6be4f908376ba9d2770f220c7a952f19e7 Auto-Submitted: auto-generated The branch stable/14 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=4e793a6be4f908376ba9d2770f220c7a952f19e7 commit 4e793a6be4f908376ba9d2770f220c7a952f19e7 Author: Mark Johnston AuthorDate: 2024-12-05 15:00:13 +0000 Commit: Mark Johnston CommitDate: 2025-01-04 13:56:51 +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 (cherry picked from commit 01f8ce83242d7a8e599cf6a78b6277161d79edd4) --- 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 ccd76d1c449e..88826a866bdc 100644 --- a/sys/netinet/in_pcb.c +++ b/sys/netinet/in_pcb.c @@ -863,6 +863,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 @@ -880,15 +967,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. */ @@ -898,7 +979,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) @@ -920,73 +1004,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 5741d88d8e76..7a0eb68c233e 100644 --- a/sys/netinet6/in6_pcb.c +++ b/sys/netinet6/in6_pcb.c @@ -162,27 +162,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, @@ -201,115 +331,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) {