From nobody Thu Oct 10 12:37:18 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 4XPTmH0vrBz5Z05j; Thu, 10 Oct 2024 12:37:19 +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 4XPTmG63Fvz4H5t; Thu, 10 Oct 2024 12:37:18 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1728563838; 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=KEv+deKePZRgkH364TiJmc7W9Hu39pxnSIkEY3fHncI=; b=R8veL9ooMHpRPxSRrDE71yfWmCY/5e5WB6/cWMV0JpJHmIpG1geM5UkGJxcknipBsJPWlC TpWBPux7/MuTVBG4jJ/zXRwcNH5jz8sv7IPP+rFuTTsOw3ZDnn7SdeN6w7vZ95ql4eXS5F Rm9j7l1U/5AgNKjmSnIiS8xl5cHZdaU/cIX/A8rffD85raSwueelZxFS/knyT+/ehcywiv 667AlSul018W/BR0FvoAS2txQLe4YQiE/VWGyEkq1aPr2iyW491nhfGdpfzQgo2FzeeZxD seQsI+VSOPHLWKaxJ4dBNvmPvqJSpUJ/UiUD3Bw91GUzZtfJsPjxTXb8+7ZAWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1728563838; 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=KEv+deKePZRgkH364TiJmc7W9Hu39pxnSIkEY3fHncI=; b=u3OAwSYup92ghpVORVGHj2t2LWhotr+s4WLUb7UJkiYR2rBGOGJelPGqYXLIfk1D0cCW3q fB270F8sXthSTKEUz2CUhbmitMACqMx2c6GCvggkDA4foMiNcpZ0F9vGlZU5DUIec9MGmf baz2vXj9YMRMXD9sETJyluLIZFpY9lWi7apFqSbsiA6NVPbLVqrPv7ZoCoWa16HwY+4eKv JPpi7FjFAj0/snlkSNYZHiuo0r2fqyUpmjE+HFntIAQx50EPGx/wdpjGNoZl+2sCPWzEqW g7qxncW/elUzxmPWd6QUjAjWYFhQ9wVPXIgaBlcgnnO0WKnATmT7xBRFjI4HHg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1728563838; a=rsa-sha256; cv=none; b=CFD9I/FEjEaE75Qs+HENIXxNjqYTRqv2lhxZtErmFOo1xCMdjfwuYFrCkl6FfsWKbhJE3X vl/tYWOV/wEppm44pxreBf8aXES8CWwnPUb7pbsgEjU/J1QGTS9JObgABXwqR5Orb3hEW9 8uhNFGzrAla8CB2LsYTsJDv+N1EDgtSl6aEnx4PWqw3C5JaBP4qDiGJYj64r4ymT9udP/S v4wYJZROdsTQ8kOgMB9WX8r7IfIHZ7G11dCKPPRAu0RcJaDcBCT3EKBvPoGjhGaC/g/ugX Oj5vR5v1SPVU403qA6mNdMRRgIx8pDpHmjBIp+MWXu8ZgemqW1bwAmLdmPp6Xw== 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 4XPTmG5ZBmzxq3; Thu, 10 Oct 2024 12:37:18 +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 49ACbI9B006236; Thu, 10 Oct 2024 12:37:18 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 49ACbIR5006233; Thu, 10 Oct 2024 12:37:18 GMT (envelope-from git) Date: Thu, 10 Oct 2024 12:37:18 GMT Message-Id: <202410101237.49ACbIR5006233@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: 25e0f8f99f54 - main - pf: convert DIOCGETRULESETS to netlink 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: 25e0f8f99f544f5bb737b2c7709fe6ea572e37f0 Auto-Submitted: auto-generated The branch main has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=25e0f8f99f544f5bb737b2c7709fe6ea572e37f0 commit 25e0f8f99f544f5bb737b2c7709fe6ea572e37f0 Author: Kristof Provost AuthorDate: 2024-10-01 17:15:54 +0000 Commit: Kristof Provost CommitDate: 2024-10-10 12:10:40 +0000 pf: convert DIOCGETRULESETS to netlink Sponsored by: Rubicon Communications, LLC ("Netgate") Differential Revision: https://reviews.freebsd.org/D46930 --- lib/libpfctl/libpfctl.c | 48 ++++++++++++++++++++++++++++++++++++++++++- lib/libpfctl/libpfctl.h | 1 + sbin/pfctl/pfctl.c | 22 +++++++++----------- sys/net/pfvar.h | 1 + sys/netpfil/pf/pf_ioctl.c | 52 +++++++++++++++++++++++++++++------------------ sys/netpfil/pf/pf_nl.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++ sys/netpfil/pf/pf_nl.h | 7 +++++++ 7 files changed, 149 insertions(+), 33 deletions(-) diff --git a/lib/libpfctl/libpfctl.c b/lib/libpfctl/libpfctl.c index 36b83c480aa7..c9f315a0db67 100644 --- a/lib/libpfctl/libpfctl.c +++ b/lib/libpfctl/libpfctl.c @@ -2914,5 +2914,51 @@ pfctl_get_addr(struct pfctl_handle *h, uint32_t ticket, uint32_t r_num, continue; } - return (0); + return (e.error); } + +#define _OUT(_field) offsetof(struct pfioc_ruleset, _field) +static const struct snl_attr_parser ap_ruleset[] = { + { .type = PF_RS_NR, .off = _OUT(nr), .cb = snl_attr_get_uint32 }, +}; +static struct snl_field_parser fp_ruleset[] = {}; +SNL_DECLARE_PARSER(ruleset_parser, struct genlmsghdr, fp_ruleset, ap_ruleset); +#undef _OUT + +int +pfctl_get_rulesets(struct pfctl_handle *h, const char *path, uint32_t *nr) +{ + struct snl_writer nw; + struct snl_errmsg_data e = {}; + struct nlmsghdr *hdr; + struct pfioc_ruleset rs = {}; + uint32_t seq_id; + int family_id; + + family_id = snl_get_genl_family(&h->ss, PFNL_FAMILY_NAME); + if (family_id == 0) + return (ENOTSUP); + + snl_init_writer(&h->ss, &nw); + hdr = snl_create_genl_msg_request(&nw, family_id, PFNL_CMD_GET_RULESETS); + + snl_add_msg_attr_string(&nw, PF_RS_PATH, path); + + if ((hdr = snl_finalize_msg(&nw)) == NULL) + return (ENXIO); + + seq_id = hdr->nlmsg_seq; + + if (! snl_send_message(&h->ss, hdr)) + return (ENXIO); + + while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) { + if (! snl_parse_nlmsg(&h->ss, hdr, &ruleset_parser, &rs)) + continue; + } + + *nr = rs.nr; + + return (e.error); +} + diff --git a/lib/libpfctl/libpfctl.h b/lib/libpfctl/libpfctl.h index 6088e18554b3..6d8be24dd04f 100644 --- a/lib/libpfctl/libpfctl.h +++ b/lib/libpfctl/libpfctl.h @@ -504,5 +504,6 @@ int pfctl_get_addrs(struct pfctl_handle *h, uint32_t ticket, uint32_t r_num, uint8_t r_action, const char *anchor, uint32_t *nr); int pfctl_get_addr(struct pfctl_handle *h, uint32_t ticket, uint32_t r_num, uint8_t r_action, const char *anchor, uint32_t nr, struct pfioc_pooladdr *pa); +int pfctl_get_rulesets(struct pfctl_handle *h, const char *path, uint32_t *nr); #endif diff --git a/sbin/pfctl/pfctl.c b/sbin/pfctl/pfctl.c index 16c54981b348..ee49e7b12278 100644 --- a/sbin/pfctl/pfctl.c +++ b/sbin/pfctl/pfctl.c @@ -1253,14 +1253,13 @@ pfctl_show_rules(int dev, char *path, int opts, enum pfctl_show format, memset(&prs, 0, sizeof(prs)); memcpy(prs.path, npath, sizeof(prs.path)); - if (ioctl(dev, DIOCGETRULESETS, &prs)) { - if (errno == EINVAL) + if ((ret = pfctl_get_rulesets(pfh, npath, &mnr)) != 0) { + if (ret == EINVAL) fprintf(stderr, "Anchor '%s' " "not found.\n", anchorname); else - err(1, "DIOCGETRULESETS"); + errc(1, ret, "DIOCGETRULESETS"); } - mnr = prs.nr; pfctl_print_rule_counters(&rule, opts); for (nr = 0; nr < mnr; ++nr) { @@ -1457,14 +1456,13 @@ pfctl_show_nat(int dev, char *path, int opts, char *anchorname, int depth, u_int32_t mnr, nr; memset(&prs, 0, sizeof(prs)); memcpy(prs.path, npath, sizeof(prs.path)); - if (ioctl(dev, DIOCGETRULESETS, &prs)) { - if (errno == EINVAL) + if ((ret = pfctl_get_rulesets(pfh, npath, &mnr)) != 0) { + if (ret == EINVAL) fprintf(stderr, "NAT anchor '%s' " "not found.\n", anchorname); else - err(1, "DIOCGETRULESETS"); + errc(1, ret, "DIOCGETRULESETS"); } - mnr = prs.nr; pfctl_print_rule_counters(&rule, opts); for (nr = 0; nr < mnr; ++nr) { @@ -2815,18 +2813,18 @@ pfctl_show_anchors(int dev, int opts, char *anchorname) { struct pfioc_ruleset pr; u_int32_t mnr, nr; + int ret; memset(&pr, 0, sizeof(pr)); memcpy(pr.path, anchorname, sizeof(pr.path)); - if (ioctl(dev, DIOCGETRULESETS, &pr)) { - if (errno == EINVAL) + if ((ret = pfctl_get_rulesets(pfh, anchorname, &mnr)) != 0) { + if (ret == EINVAL) fprintf(stderr, "Anchor '%s' not found.\n", anchorname); else - err(1, "DIOCGETRULESETS"); + errc(1, ret, "DIOCGETRULESETS"); return (-1); } - mnr = pr.nr; for (nr = 0; nr < mnr; ++nr) { char sub[MAXPATHLEN]; diff --git a/sys/net/pfvar.h b/sys/net/pfvar.h index 66e3e53b8b37..e27c5e666de8 100644 --- a/sys/net/pfvar.h +++ b/sys/net/pfvar.h @@ -2581,6 +2581,7 @@ int pf_ioctl_begin_addrs(uint32_t *); int pf_ioctl_add_addr(struct pfioc_pooladdr *); int pf_ioctl_get_addrs(struct pfioc_pooladdr *); int pf_ioctl_get_addr(struct pfioc_pooladdr *); +int pf_ioctl_get_rulesets(struct pfioc_ruleset *); void pf_krule_free(struct pf_krule *); void pf_krule_clear_counters(struct pf_krule *); diff --git a/sys/netpfil/pf/pf_ioctl.c b/sys/netpfil/pf/pf_ioctl.c index 8081960933a2..e3fb3150db5f 100644 --- a/sys/netpfil/pf/pf_ioctl.c +++ b/sys/netpfil/pf/pf_ioctl.c @@ -2665,6 +2665,37 @@ pf_ioctl_get_addr(struct pfioc_pooladdr *pp) return (0); } +int +pf_ioctl_get_rulesets(struct pfioc_ruleset *pr) +{ + struct pf_kruleset *ruleset; + struct pf_kanchor *anchor; + + PF_RULES_RLOCK_TRACKER; + + pr->path[sizeof(pr->path) - 1] = 0; + + PF_RULES_RLOCK(); + if ((ruleset = pf_find_kruleset(pr->path)) == NULL) { + PF_RULES_RUNLOCK(); + return (ENOENT); + } + pr->nr = 0; + if (ruleset->anchor == NULL) { + /* XXX kludge for pf_main_ruleset */ + RB_FOREACH(anchor, pf_kanchor_global, &V_pf_anchors) + if (anchor->parent == NULL) + pr->nr++; + } else { + RB_FOREACH(anchor, pf_kanchor_node, + &ruleset->anchor->children) + pr->nr++; + } + PF_RULES_RUNLOCK(); + + return (0); +} + static int pfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td) { @@ -4474,29 +4505,10 @@ DIOCCHANGEADDR_error: case DIOCGETRULESETS: { struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr; - struct pf_kruleset *ruleset; - struct pf_kanchor *anchor; pr->path[sizeof(pr->path) - 1] = 0; - PF_RULES_RLOCK(); - if ((ruleset = pf_find_kruleset(pr->path)) == NULL) { - PF_RULES_RUNLOCK(); - error = ENOENT; - break; - } - pr->nr = 0; - if (ruleset->anchor == NULL) { - /* XXX kludge for pf_main_ruleset */ - RB_FOREACH(anchor, pf_kanchor_global, &V_pf_anchors) - if (anchor->parent == NULL) - pr->nr++; - } else { - RB_FOREACH(anchor, pf_kanchor_node, - &ruleset->anchor->children) - pr->nr++; - } - PF_RULES_RUNLOCK(); + error = pf_ioctl_get_rulesets(pr); break; } diff --git a/sys/netpfil/pf/pf_nl.c b/sys/netpfil/pf/pf_nl.c index 15fe341ce57e..e3c0369f550d 100644 --- a/sys/netpfil/pf/pf_nl.c +++ b/sys/netpfil/pf/pf_nl.c @@ -1635,6 +1635,49 @@ pf_handle_get_addr(struct nlmsghdr *hdr, struct nl_pstate *npt) return (0); } +#define _OUT(_field) offsetof(struct pfioc_ruleset, _field) +static const struct nlattr_parser nla_p_ruleset[] = { + { .type = PF_RS_PATH, .off = _OUT(path), .arg = (void *)MAXPATHLEN, .cb = nlattr_get_chara }, +}; +static const struct nlfield_parser nlf_p_ruleset[] = { +}; +NL_DECLARE_PARSER(ruleset_parser, struct genlmsghdr, nlf_p_ruleset, nla_p_ruleset); +#undef _OUT + +static int +pf_handle_get_rulesets(struct nlmsghdr *hdr, struct nl_pstate *npt) +{ + struct pfioc_ruleset attrs = { 0 }; + struct nl_writer *nw = npt->nw; + struct genlmsghdr *ghdr_new; + int error; + + error = nl_parse_nlmsg(hdr, &ruleset_parser, npt, &attrs); + if (error != 0) + return (error); + + error = pf_ioctl_get_rulesets(&attrs); + if (error != 0) + return (error); + + if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr))) + return (ENOMEM); + + ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr); + ghdr_new->cmd = PFNL_CMD_GET_RULESETS; + ghdr_new->version = 0; + ghdr_new->reserved = 0; + + nlattr_add_u32(nw, PF_RS_NR, attrs.nr); + + if (!nlmsg_end(nw)) { + nlmsg_abort(nw); + return (ENOMEM); + } + + return (0); +} + static const struct nlhdr_parser *all_parsers[] = { &state_parser, &addrule_parser, @@ -1647,6 +1690,7 @@ static const struct nlhdr_parser *all_parsers[] = { &set_limit_parser, &pool_addr_parser, &add_addr_parser, + &ruleset_parser, }; static int family_id; @@ -1806,6 +1850,13 @@ static const struct genl_cmd pf_cmds[] = { .cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL, .cmd_priv = PRIV_NETINET_PF, }, + { + .cmd_num = PFNL_CMD_GET_RULESETS, + .cmd_name = "GET_RULESETS", + .cmd_cb = pf_handle_get_rulesets, + .cmd_flags = GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL, + .cmd_priv = PRIV_NETINET_PF, + }, }; void diff --git a/sys/netpfil/pf/pf_nl.h b/sys/netpfil/pf/pf_nl.h index acb4b31320c0..5aa02d10214b 100644 --- a/sys/netpfil/pf/pf_nl.h +++ b/sys/netpfil/pf/pf_nl.h @@ -58,6 +58,7 @@ enum { PFNL_CMD_ADD_ADDR = 20, PFNL_CMD_GET_ADDRS = 21, PFNL_CMD_GET_ADDR = 22, + PFNL_CMD_GET_RULESETS = 23, __PFNL_CMD_MAX, }; #define PFNL_CMD_MAX (__PFNL_CMD_MAX -1) @@ -380,6 +381,12 @@ enum pf_add_addr_types_t { PF_AA_ADDR = 9, /* nested, pf_pooladdr */ }; +enum pf_get_rulesets_types_t { + PF_RS_UNSPEC, + PF_RS_PATH = 1, /* string */ + PF_RS_NR = 2, /* u32 */ +}; + #ifdef _KERNEL void pf_nl_register(void);