From nobody Thu Apr 20 16:13:51 2023 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 4Q2N4w4hM2z46Fmm; Thu, 20 Apr 2023 16:13: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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Q2N4w0t5Fz3Lp6; Thu, 20 Apr 2023 16:13:52 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1682007232; 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=yVkx5RkqNBHEkml1BhBVdbJLZdjQ3yINxyW96Ap5N54=; b=OH2t0izzPd6fxxnNZi8Mr2UEwW8teYC4PMaFIOBXBof4CTv1vyQyVxPFMbFPteweLiFEP0 lzVIb0pAbLXJLDvDO56pfIGJ7XED5VQdLDDD0FrpNz9cCbmYqlDHfIaARR0hZR0AzLTln8 T1w0h9EMabbw9NxYG6OYgyQFm4yph4TWuvH3XlGu1+aG3SPmXz/QcZIncJ9g5jIBKWILt+ wtUkh8cXj+GI3a6y989HO/wy82oj/SKN5pVamVcPS6Kf2dca6mGR7uKKVe5+pqvi2PdlXa wfPNqngK6PTLhgRZGbz8Y3AqnhtsaYKHuVPvBkLtbcA0vQc8NRrIPVIBfttGRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1682007232; 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=yVkx5RkqNBHEkml1BhBVdbJLZdjQ3yINxyW96Ap5N54=; b=hTIdQvl0uPzhyA26ueeuphCRYENqRRvMhUaaDNe3M+A7wKRKk7xiQpurHKjwd9/ToRzjRx Nxuscmje0W01pRK/P0i3HyfVn/QtzTuJ3GbSshMFWH/BgnG7fb1oTzjavvXE/CWEIBoiBP vwCVjKUm/+wPRlyvFtdlPem0C0RiUDVLWuO2UoDz98+CVUJQpxplckVWNpqaX5aHSoTVaW 27ZwnJl3I9MOk3BsWjZxX7G7XPuhDqYNWO5jVvB8s0lcaqeOG9NH/Nmyyy6t2HfQGiEhbZ oSYz0CiqD+qMONtu4WVfTg+G1NwXYldRY9kjh6e6CmgFzT/uVMG9V0G2AujJXg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1682007232; a=rsa-sha256; cv=none; b=ThVRDvGT+bollW41qtJNmDn+PKNFYhzcr3b3yssCl4z88wMavdGGvJI7/lrzkCsW4hQ5ms ZpwcIMN4zyWrb5ZcLMvqW75L+OtEzTrFK4tV/S6EVgYdxUD/lU24ixXDh1mitPdkaJRuRT mM+szILPClNXj0MCz2RmogFi4wbSvQHmitKG+qBMEmMYEa5uGKSv9wG/OvzT1apzuEOKGD V5mIThg6/yYPXwMogOoxfQIQwh7PqAZE8d1GOzwhrVHNcApW12DX7/FUqxhBTjvAa0DgnV NpLhy9mO895LdINYRhjGPNU5IEacZX2BuMKB7u1+PxIycAFLK85lKrN0B/vh6g== 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 4Q2N4v726LzkgK; Thu, 20 Apr 2023 16:13:51 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 33KGDpF1045412; Thu, 20 Apr 2023 16:13:51 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 33KGDpiW045411; Thu, 20 Apr 2023 16:13:51 GMT (envelope-from git) Date: Thu, 20 Apr 2023 16:13:51 GMT Message-Id: <202304201613.33KGDpiW045411@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: 3e98dcb3d574 - main - inpcb: Move inpcb matching logic into separate functions 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: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: 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/main X-Git-Reftype: branch X-Git-Commit: 3e98dcb3d574af3364be350ee40105d515c6f45f Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=3e98dcb3d574af3364be350ee40105d515c6f45f commit 3e98dcb3d574af3364be350ee40105d515c6f45f Author: Mark Johnston AuthorDate: 2023-04-20 15:48:08 +0000 Commit: Mark Johnston CommitDate: 2023-04-20 16:13:06 +0000 inpcb: Move inpcb matching logic into separate functions These functions will get some additional callers in future revisions. No functional change intended. Discussed with: glebius Tested by: glebius Sponsored by: Modirum MDPay Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D38571 --- sys/netinet/in_pcb.c | 70 +++++++++++++++++++++++++++++++++++--------------- sys/netinet6/in6_pcb.c | 66 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 96 insertions(+), 40 deletions(-) diff --git a/sys/netinet/in_pcb.c b/sys/netinet/in_pcb.c index c6c5ca4d2281..e301c307a413 100644 --- a/sys/netinet/in_pcb.c +++ b/sys/netinet/in_pcb.c @@ -2180,31 +2180,63 @@ out: grp->il_inpcnt]); } +static bool +in_pcblookup_exact_match(const struct inpcb *inp, struct in_addr faddr, + u_short fport, struct in_addr laddr, u_short lport) +{ +#ifdef INET6 + /* XXX inp locking */ + if ((inp->inp_vflag & INP_IPV4) == 0) + return (false); +#endif + if (inp->inp_faddr.s_addr == faddr.s_addr && + inp->inp_laddr.s_addr == laddr.s_addr && + inp->inp_fport == fport && + inp->inp_lport == lport) + return (true); + return (false); +} + static struct inpcb * in_pcblookup_hash_exact(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_short fport, struct in_addr laddr, u_short lport) { struct inpcbhead *head; - struct inpcb *inp, *match; + struct inpcb *inp; INP_HASH_LOCK_ASSERT(pcbinfo); - match = NULL; head = &pcbinfo->ipi_hash_exact[INP_PCBHASH(&faddr, lport, fport, pcbinfo->ipi_hashmask)]; CK_LIST_FOREACH(inp, head, inp_hash_exact) { -#ifdef INET6 - /* XXX inp locking */ - if ((inp->inp_vflag & INP_IPV4) == 0) - continue; -#endif - if (inp->inp_faddr.s_addr == faddr.s_addr && - inp->inp_laddr.s_addr == laddr.s_addr && - inp->inp_fport == fport && - inp->inp_lport == lport) + if (in_pcblookup_exact_match(inp, faddr, fport, laddr, lport)) return (inp); } - return (match); + return (NULL); +} + +typedef enum { + INPLOOKUP_MATCH_NONE = 0, + INPLOOKUP_MATCH_WILD = 1, + INPLOOKUP_MATCH_LADDR = 2, +} inp_lookup_match_t; + +static inp_lookup_match_t +in_pcblookup_wild_match(const struct inpcb *inp, struct in_addr laddr, + u_short lport) +{ +#ifdef INET6 + /* XXX inp locking */ + if ((inp->inp_vflag & INP_IPV4) == 0) + return (INPLOOKUP_MATCH_NONE); +#endif + if (inp->inp_faddr.s_addr != INADDR_ANY || inp->inp_lport != lport) + return (INPLOOKUP_MATCH_NONE); + if (inp->inp_laddr.s_addr == INADDR_ANY) + return (INPLOOKUP_MATCH_WILD); + if (inp->inp_laddr.s_addr == laddr.s_addr) + return (INPLOOKUP_MATCH_LADDR); + return (INPLOOKUP_MATCH_NONE); } static struct inpcb * @@ -2233,15 +2265,11 @@ in_pcblookup_hash_wild_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, local_wild_mapped = NULL; #endif CK_LIST_FOREACH(inp, head, inp_hash_wild) { + inp_lookup_match_t match; bool injail; -#ifdef INET6 - /* XXX inp locking */ - if ((inp->inp_vflag & INP_IPV4) == 0) - continue; -#endif - if (inp->inp_faddr.s_addr != INADDR_ANY || - inp->inp_lport != lport) + match = in_pcblookup_wild_match(inp, laddr, lport); + if (match == INPLOOKUP_MATCH_NONE) continue; injail = prison_flag(inp->inp_cred, PR_IP4) != 0; @@ -2254,11 +2282,11 @@ in_pcblookup_hash_wild_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr, continue; } - if (inp->inp_laddr.s_addr == laddr.s_addr) { + if (match == INPLOOKUP_MATCH_LADDR) { if (injail) return (inp); local_exact = inp; - } else if (inp->inp_laddr.s_addr == INADDR_ANY) { + } else { #ifdef INET6 /* XXX inp locking, NULL check */ if (inp->inp_vflag & INP_IPV6PROTO) diff --git a/sys/netinet6/in6_pcb.c b/sys/netinet6/in6_pcb.c index 6a5fbe93b58a..508d264809e3 100644 --- a/sys/netinet6/in6_pcb.c +++ b/sys/netinet6/in6_pcb.c @@ -939,32 +939,62 @@ out: grp->il_inpcnt]); } +static bool +in6_pcblookup_exact_match(const struct inpcb *inp, const struct in6_addr *faddr, + u_short fport, const struct in6_addr *laddr, u_short lport) +{ + /* XXX inp locking */ + if ((inp->inp_vflag & INP_IPV6) == 0) + return (false); + if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) && + IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && + inp->inp_fport == fport && inp->inp_lport == lport) + return (true); + return (false); +} + static struct inpcb * in6_pcblookup_hash_exact(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_short fport, struct in6_addr *laddr, u_short lport) { struct inpcbhead *head; - struct inpcb *inp, *match; + struct inpcb *inp; INP_HASH_LOCK_ASSERT(pcbinfo); /* * First look for an exact match. */ - match = NULL; head = &pcbinfo->ipi_hash_exact[INP6_PCBHASH(faddr, lport, fport, pcbinfo->ipi_hashmask)]; CK_LIST_FOREACH(inp, head, inp_hash_exact) { - /* XXX inp locking */ - if ((inp->inp_vflag & INP_IPV6) == 0) - continue; - if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) && - IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && - inp->inp_fport == fport && - inp->inp_lport == lport) + if (in6_pcblookup_exact_match(inp, faddr, fport, laddr, lport)) return (inp); } - return (match); + return (NULL); +} + +typedef enum { + INPLOOKUP_MATCH_NONE = 0, + INPLOOKUP_MATCH_WILD = 1, + INPLOOKUP_MATCH_LADDR = 2, +} inp_lookup_match_t; + +static inp_lookup_match_t +in6_pcblookup_wild_match(const struct inpcb *inp, const struct in6_addr *laddr, + u_short lport) +{ + /* XXX inp locking */ + if ((inp->inp_vflag & INP_IPV6) == 0) + return (INPLOOKUP_MATCH_NONE); + if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) || + inp->inp_lport != lport) + return (INPLOOKUP_MATCH_NONE); + if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) + return (INPLOOKUP_MATCH_WILD); + if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) + return (INPLOOKUP_MATCH_LADDR); + return (INPLOOKUP_MATCH_NONE); } static struct inpcb * @@ -975,6 +1005,8 @@ in6_pcblookup_hash_wild_locked(struct inpcbinfo *pcbinfo, struct inpcbhead *head; struct inpcb *inp, *jail_wild, *local_exact, *local_wild; + INP_HASH_LOCK_ASSERT(pcbinfo); + /* * Order of socket selection - we always prefer jails. * 1. jailed, non-wild. @@ -986,17 +1018,13 @@ in6_pcblookup_hash_wild_locked(struct inpcbinfo *pcbinfo, pcbinfo->ipi_hashmask)]; local_wild = local_exact = jail_wild = NULL; CK_LIST_FOREACH(inp, head, inp_hash_wild) { + inp_lookup_match_t match; bool injail; - /* XXX inp locking */ - if ((inp->inp_vflag & INP_IPV6) == 0) + match = in6_pcblookup_wild_match(inp, laddr, lport); + if (match == INPLOOKUP_MATCH_NONE) continue; - if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) || - inp->inp_lport != lport) { - continue; - } - injail = prison_flag(inp->inp_cred, PR_IP6) != 0; if (injail) { if (prison_check_ip6_locked( @@ -1007,12 +1035,12 @@ in6_pcblookup_hash_wild_locked(struct inpcbinfo *pcbinfo, continue; } - if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) { + if (match == INPLOOKUP_MATCH_LADDR) { if (injail) return (inp); else local_exact = inp; - } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { + } else { if (injail) jail_wild = inp; else