From nobody Thu Nov 14 16:12:52 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 4Xq4tr5hrcz5crFh; Thu, 14 Nov 2024 16:12:52 +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 4Xq4tr5Bh2z4pKB; Thu, 14 Nov 2024 16:12:52 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1731600772; 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=dobCKEU93mqNRXOSs9q6L7TLN4OCQKHnVxNPNOVAWyE=; b=IloBZJ03/xETAvtJlSSA7/vuDJBOwj9OGyWxa5hMBXZGxShjw8r7mmXXy0j9pzkw5V+fyw 1/d89w6Gy+uMqwaz6mWp1qWyoUN8rfPePlSq3GghgyfZBCVHIEmNNkAf9bYmd2Jemn2OLK llnuPl3mcAPAjsJ69c8zgQbw2kUmgzuNu9S/kkVIhAAkPB8G/mu0VF1XFmf/ePI6Om2khR vkSAnHPxzLFaZJ89UfJzVnGLvL4VQ8Ew+gg6l68hf6zUfKkMcgakzc6/X4daivTh6ystaY GdKZVV5+cr/1DXAYVm1f3xOtcyf2MKIiCUeyU5caHSolDIlmP/soA3ydDPg07w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1731600772; 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=dobCKEU93mqNRXOSs9q6L7TLN4OCQKHnVxNPNOVAWyE=; b=mfgb1BaMXQrwxarM8qC9ByhaBoJLJ2nPsBx0mSnr13yD0ZXxgX83CCCsWo53YPltPVupxR 4cqTQmDho+XxYMH0hmd+zYmMS3pjtfjHqbPO5xnwPIaw+qBVBzfWtlNu9hAQ7q120ORsZX YZi6ik3HmEgMv932ffn5v4v65luaBIzKVohI+y0jGTzl1dxdlBpxk1gNTZ9aza5piRiDZ3 HBD0uIJGDZVLHK8aLn0F51Vq9yw6RUHOAwwJrhz80VUibD1FtxkhqFM1HcxpTQdaLD1nSp I8WTxXZw5ZhSSyJEJdZsjfA9VNqsPZkr3HSVBwSWV+UrfidXloYMYvHMZXzYFg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1731600772; a=rsa-sha256; cv=none; b=gkWocV1EqjaWa0r0yuF8JA6gNnpwb2ZTWn7HCH7BmPyM7+GP5BaJ36C8l5ZKQ5uggMMcWl qAqkprPIIOc4WLzCJqt4OKF2uyDdeHGPJnJpChx6cmIXseHKk7uZyEcc6enqBIhhpsUOlL Nm+J1CcflnlkzJ1AKy/6sjT9NpNcGIjMvAOQG4MgfFgUBSK5qQZEqtLMp9EsJ/jouZYweT K1vrQ2U9HKT4rPsMzuRTzX5kCGlvePI9PN9SMc/ef03B0SBJ+rtCeTRP6/rDhRnhOjYwBz dn5uHb33lzoLqjPmHGNKQS9tYVwmls/qSVP6JN+6P8a2UseBht1+dfJqZL0hjQ== 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 4Xq4tr4VTNzls5; Thu, 14 Nov 2024 16:12:52 +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 4AEGCqKQ054377; Thu, 14 Nov 2024 16:12:52 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4AEGCqmC054374; Thu, 14 Nov 2024 16:12:52 GMT (envelope-from git) Date: Thu, 14 Nov 2024 16:12:52 GMT Message-Id: <202411141612.4AEGCqmC054374@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: 45a77bf23fa2 - main - inpcb: Make some cosmetic improvements to in_pcbbind() 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: 45a77bf23fa2f36bf2169f7ba2a33b31f4c35adb Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=45a77bf23fa2f36bf2169f7ba2a33b31f4c35adb commit 45a77bf23fa2f36bf2169f7ba2a33b31f4c35adb Author: Mark Johnston AuthorDate: 2024-11-14 16:05:27 +0000 Commit: Mark Johnston CommitDate: 2024-11-14 16:05:27 +0000 inpcb: Make some cosmetic improvements to in_pcbbind() - Use the local var "laddr" instead of sin->sin_addr in one block. - Use in_nullhost() instead of explicit comparisons with INADDR_ANY. - Combine multiple socket options checks into one. - Fix indentation. - Remove some unhelpful comments. This is in preparation for some simplification and bug-fixing. No functional change intended. Reviewed by: glebius MFC after: 2 weeks Sponsored by: Klara, Inc. Sponsored by: Stormshield Differential Revision: https://reviews.freebsd.org/D47451 --- sys/netinet/in_pcb.c | 44 ++++++++++++++++++++------------------------ sys/netinet6/in6_pcb.c | 19 ++++++++----------- 2 files changed, 28 insertions(+), 35 deletions(-) diff --git a/sys/netinet/in_pcb.c b/sys/netinet/in_pcb.c index 769724fd5063..116ee149fecb 100644 --- a/sys/netinet/in_pcb.c +++ b/sys/netinet/in_pcb.c @@ -916,8 +916,10 @@ in_pcbbind_setup(struct inpcb *inp, struct sockaddr_in *sin, in_addr_t *laddrp, return (EINVAL); lport = sin->sin_port; } + laddr = sin->sin_addr; + /* NB: lport is left as 0 if the port isn't being changed. */ - if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { + if (IN_MULTICAST(ntohl(laddr.s_addr))) { /* * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; * allow complete duplication of binding if @@ -934,7 +936,7 @@ in_pcbbind_setup(struct inpcb *inp, struct sockaddr_in *sin, in_addr_t *laddrp, if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0) reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB; - } else if (sin->sin_addr.s_addr != INADDR_ANY) { + } else if (!in_nullhost(laddr)) { sin->sin_port = 0; /* yech... */ bzero(&sin->sin_zero, sizeof(sin->sin_zero)); /* @@ -943,50 +945,44 @@ in_pcbbind_setup(struct inpcb *inp, struct sockaddr_in *sin, in_addr_t *laddrp, * to any endpoint address, local or not. */ if ((inp->inp_flags & INP_BINDANY) == 0 && - ifa_ifwithaddr_check((struct sockaddr *)sin) == 0) + ifa_ifwithaddr_check( + (const struct sockaddr *)sin) == 0) return (EADDRNOTAVAIL); } - laddr = sin->sin_addr; if (lport) { struct inpcb *t; - /* GROSS */ if (ntohs(lport) <= V_ipport_reservedhigh && ntohs(lport) >= V_ipport_reservedlow && priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT)) return (EACCES); - if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && + + if (!IN_MULTICAST(ntohl(laddr.s_addr)) && priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) { - t = in_pcblookup_local(pcbinfo, sin->sin_addr, - lport, INPLOOKUP_WILDCARD, cred); - /* - * XXX - * This entire block sorely needs a rewrite. - */ + t = in_pcblookup_local(pcbinfo, laddr, lport, + INPLOOKUP_WILDCARD, cred); if (t != NULL && (so->so_type != SOCK_STREAM || - ntohl(t->inp_faddr.s_addr) == INADDR_ANY) && - (ntohl(sin->sin_addr.s_addr) != INADDR_ANY || - ntohl(t->inp_laddr.s_addr) != INADDR_ANY || + 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, sin->sin_addr, - lport, lookupflags, cred); - if (t != NULL && (reuseport & t->inp_socket->so_options) == 0 && - (reuseport_lb & t->inp_socket->so_options) == 0) { + t = in_pcblookup_local(pcbinfo, laddr, lport, + lookupflags, cred); + if (t != NULL && ((reuseport | reuseport_lb) & + t->inp_socket->so_options) == 0) { #ifdef INET6 - if (ntohl(sin->sin_addr.s_addr) != - INADDR_ANY || - ntohl(t->inp_laddr.s_addr) != - INADDR_ANY || + 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 (EADDRINUSE); } } } diff --git a/sys/netinet6/in6_pcb.c b/sys/netinet6/in6_pcb.c index 49a430ea6d01..45fdd6541bf7 100644 --- a/sys/netinet6/in6_pcb.c +++ b/sys/netinet6/in6_pcb.c @@ -247,11 +247,11 @@ in6_pcbbind(struct inpcb *inp, struct sockaddr_in6 *sin6, struct ucred *cred) if (lport) { struct inpcb *t; - /* GROSS */ 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, @@ -279,8 +279,7 @@ in6_pcbbind(struct inpcb *inp, struct sockaddr_in6 *sin6, struct ucred *cred) INPLOOKUP_WILDCARD, cred); if (t != NULL && (so->so_type != SOCK_STREAM || - ntohl(t->inp_faddr.s_addr) == - INADDR_ANY) && + in_nullhost(t->inp_faddr)) && (inp->inp_cred->cr_uid != t->inp_cred->cr_uid)) return (EADDRINUSE); @@ -289,10 +288,9 @@ in6_pcbbind(struct inpcb *inp, struct sockaddr_in6 *sin6, struct ucred *cred) } t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr, lport, lookupflags, cred); - if (t && (reuseport & t->inp_socket->so_options) == 0 && - (reuseport_lb & t->inp_socket->so_options) == 0) { + 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)) { @@ -301,11 +299,10 @@ in6_pcbbind(struct inpcb *inp, struct sockaddr_in6 *sin6, struct ucred *cred) in6_sin6_2_sin(&sin, sin6); t = in_pcblookup_local(pcbinfo, sin.sin_addr, lport, lookupflags, cred); - if (t && - (reuseport & t->inp_socket->so_options) == 0 && - (reuseport_lb & t->inp_socket->so_options) == 0 && - (ntohl(t->inp_laddr.s_addr) != INADDR_ANY || - (t->inp_vflag & INP_IPV6PROTO) != 0)) { + 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); } }