From nobody Thu Jun 06 13:46:06 2024 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 4Vw5Fq1TDYz5MWPb; Thu, 06 Jun 2024 13:46:07 +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 4Vw5Fp6nvFz3ymt; Thu, 6 Jun 2024 13:46:06 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1717681566; 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=45KPbi768ArqAukx0T6XkB+ss1509BnBCx3HtzxEFaI=; b=A53Z+Ezmyx3DBkhmcafTFJBTSTHa/WN1/olycHxNArFUGL+JoR5URAo4LbcLvYaselwO/A 5rmBeV268g5oPZ4HAJiweUqqMCxMxS5IsLLqq0KgNUW6VAKG8+9nZuTBA7ErZ4jdjAdfpv 863fErdt5KzscSYGVsKVyW7YxTSNWgW1awIBeEs1hbQL+qIk6Xjme8AC+U4OFm7OV2yI9b 84lBhslz7VuLnxj44twKt80OIBAjLSOJMmE56VLzoLeyyIRgupWY9SrRECMtdWCH//zUAv WSWTZwyQ+9HQy795UF8nOFaLfrgBUAjCbaOhMFWjoGZhwJH+SyHSQah3H3EXfg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1717681567; a=rsa-sha256; cv=none; b=mRH0AsWIVvrEDlt3c7QWjS+j5XXChzACJGF56X+kyv6KGzUm2uFokRfF3+iZeh8R2ky+O/ SWkAr2ZntBymUvYXekfq5VCctE9uMXeAYUKeUOwKgyxgb/+ixn4XK8SboiT+MR3PULLEn6 M5M8PNa6K/b7jMisbJZ8+5XIH5ncPw3rfCQ7qkAFm/EERM2ZmgOHci7SWzRe0vKZge/ATA Qkv1g8DHCQ6OQBp6GMpC7unaQ0zb20iuSZ9fA1ZyiVVGSjoGN5ZxIeCsQwMnGSAWHDgPOf KkRrylU0ys5EoWRSCYX6hZvuusbKMAEuaWS/tvi0Ae76Omr8H8JONT2XMt4thA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1717681567; 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=45KPbi768ArqAukx0T6XkB+ss1509BnBCx3HtzxEFaI=; b=paoVw0T8FSj6iIknK2k82X7L2pwFdOBXq/vU9SsccVJgmftG4S7b+vqMfVT596BkHthogv JQiDp2FnuRSugQbCEJZnkxSd8VRUHYtAt1moM0yWZkvy0WpfxPScufPbLPm5MBvBo8+baU 9NAta/B/CUujYlYquzP+QZ3NT81dod1Eh0sJmqhabPOSNTiQFYR82i5bg6rFhVF4kqj7TX wXukfpM8Xd2IHfkaCh3u0MRgNIKb8ZQGxTNN7zZ03EXmaboJzxvlyIKPGyKjRVxk0yaqly pbJPr8dPbXsR7+GcrXL1TrnR0t2esQSDSFr82F/Z+Ep9eU+JxQZw8c1WbwI0CQ== 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 4Vw5Fp5zLTzp5P; Thu, 6 Jun 2024 13:46:06 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 456Dk6r0010686; Thu, 6 Jun 2024 13:46:06 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 456Dk60k010683; Thu, 6 Jun 2024 13:46:06 GMT (envelope-from git) Date: Thu, 6 Jun 2024 13:46:06 GMT Message-Id: <202406061346.456Dk60k010683@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kristof Provost Subject: git: 8f04209d37ec - main - pf: simplify pf_addrcpy() and pf_match_addr() 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: kp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 8f04209d37ec14e28aaeb14a7a020dac9fb4983b Auto-Submitted: auto-generated The branch main has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=8f04209d37ec14e28aaeb14a7a020dac9fb4983b commit 8f04209d37ec14e28aaeb14a7a020dac9fb4983b Author: Kristof Provost AuthorDate: 2024-06-05 20:30:34 +0000 Commit: Kristof Provost CommitDate: 2024-06-06 13:45:31 +0000 pf: simplify pf_addrcpy() and pf_match_addr() Use the v4/v6 union members rather than the uint32_t ones. Export IN_ARE_MASKED_ADDR_EQUAL() in in_var.h and use it (and its IPv6 equivalent) for masked comparisons rather than hand-rolled code. Event: Kitchener-Waterloo Hackathon 202406 --- sys/netinet/in.c | 3 --- sys/netinet/in_var.h | 5 +++++ sys/netpfil/pf/pf.c | 19 ++++--------------- 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/sys/netinet/in.c b/sys/netinet/in.c index 940b197d9e95..cc2f37863ea1 100644 --- a/sys/netinet/in.c +++ b/sys/netinet/in.c @@ -1473,9 +1473,6 @@ in_lltable_new(struct in_addr addr4, u_int flags) return (&lle->base); } -#define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \ - ((((d).s_addr ^ (a).s_addr) & (m).s_addr)) == 0 ) - static int in_lltable_match_prefix(const struct sockaddr *saddr, const struct sockaddr *smask, u_int flags, struct llentry *lle) diff --git a/sys/netinet/in_var.h b/sys/netinet/in_var.h index 09d3cd050fc3..b4bdb2a65fc8 100644 --- a/sys/netinet/in_var.h +++ b/sys/netinet/in_var.h @@ -97,6 +97,11 @@ struct in_ifaddr { #define IN_LNAOF(in, ifa) \ ((ntohl((in).s_addr) & ~((struct in_ifaddr *)(ifa)->ia_subnetmask)) +#ifdef _KERNEL +#define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \ + ((((d).s_addr ^ (a).s_addr) & (m).s_addr)) == 0 ) +#endif + #define LLTABLE(ifp) \ ((struct in_ifinfo *)(ifp)->if_afdata[AF_INET])->ii_llt /* diff --git a/sys/netpfil/pf/pf.c b/sys/netpfil/pf/pf.c index 195b5d49db7a..064642d7da05 100644 --- a/sys/netpfil/pf/pf.c +++ b/sys/netpfil/pf/pf.c @@ -723,14 +723,11 @@ pf_addrcpy(struct pf_addr *dst, struct pf_addr *src, sa_family_t af) switch (af) { #ifdef INET case AF_INET: - dst->addr32[0] = src->addr32[0]; + memcpy(&dst->v4, &src->v4, sizeof(dst->v4)); break; #endif /* INET */ case AF_INET6: - dst->addr32[0] = src->addr32[0]; - dst->addr32[1] = src->addr32[1]; - dst->addr32[2] = src->addr32[2]; - dst->addr32[3] = src->addr32[3]; + memcpy(&dst->v6, &src->v6, sizeof(dst->v6)); break; } } @@ -3408,21 +3405,13 @@ pf_match_addr(u_int8_t n, struct pf_addr *a, struct pf_addr *m, switch (af) { #ifdef INET case AF_INET: - if ((a->addr32[0] & m->addr32[0]) == - (b->addr32[0] & m->addr32[0])) + if (IN_ARE_MASKED_ADDR_EQUAL(a->v4, b->v4, m->v4)) match++; break; #endif /* INET */ #ifdef INET6 case AF_INET6: - if (((a->addr32[0] & m->addr32[0]) == - (b->addr32[0] & m->addr32[0])) && - ((a->addr32[1] & m->addr32[1]) == - (b->addr32[1] & m->addr32[1])) && - ((a->addr32[2] & m->addr32[2]) == - (b->addr32[2] & m->addr32[2])) && - ((a->addr32[3] & m->addr32[3]) == - (b->addr32[3] & m->addr32[3]))) + if (IN6_ARE_MASKED_ADDR_EQUAL(&a->v6, &b->v6, &m->v6)) match++; break; #endif /* INET6 */