From nobody Tue Mar 15 15:20:11 2022 X-Original-To: dev-commits-src-branches@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 2A8ED1A193D9; Tue, 15 Mar 2022 15:20:12 +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 4KHxt355bxz4b2p; Tue, 15 Mar 2022 15:20:11 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1647357611; 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=ewyBHPWJZmuV7TIKlbLItqCrqTWaVC2i6X+JrYsi5UI=; b=uAd1hdyYp1MSx3HBTCMg/FtgKzkwRNhIcR7RzH5Hrzc1KCBoIYiojpXYnggOblshJLSMIs 91rrERTY/5VyxjbFJKsFbKXUhuGDFj2B5/vL8SCVSGstGzBm4QB45UNgRM7zH4jktOCsT0 sq78lQhR+rXKBEFawsx6TXHmPi9HcoL4qKRVK/J/Ni6M1PdZ4C34LSFr3AzRRvlxpyzEZF rSYpSha4y112ReY7xQgc5/hWH5JzGjfMC4SmQTZ43kcGtFZoi8PftbpUf8uCaYhLopETew /mpvQ3j6PBtnnklPovulIEPiYEVKZ3U0A8FVdIrTb06p/5StbxSEBl6mKLy12Q== 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 7B85D24053; Tue, 15 Mar 2022 15:20:11 +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 22FFKBP7033855; Tue, 15 Mar 2022 15:20:11 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 22FFKBKH033848; Tue, 15 Mar 2022 15:20:11 GMT (envelope-from git) Date: Tue, 15 Mar 2022 15:20:11 GMT Message-Id: <202203151520.22FFKBKH033848@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kristof Provost Subject: git: a445feae98f2 - stable/12 - libpfctl: support flushing rules/nat/eth List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@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/stable/12 X-Git-Reftype: branch X-Git-Commit: a445feae98f27325c6845ccebb35f062fce1889d Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1647357611; 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=ewyBHPWJZmuV7TIKlbLItqCrqTWaVC2i6X+JrYsi5UI=; b=C1yszHCSd2v5hcWTYEm0qUrxJ4X9hDWWocP4Jw67/8RoitX6/HsdRjHGd2/vQqG5ZaLgDi OBcQXAHecdl1hK6oVCskxiW8hYYYCjfwWFWcuOJGh+bNeB0b+EmaaLixhPnF8pKrlDxHtO 1NQyOq4TDJxJbkeDiEp0REK1v+8eydbuoIgH5Zaj/uTTdHH0AY5PwHsoe/cmPrhyu+sOsG HLAnsC1S33PL5u6/IATm2pgIAMTHXg+JkNOS/e6HDrKnFLOAYDbC2M3/BHDkeGx54lVlb/ 7sI86rqA3ZBx03sFknVptznR/Bescph7ElhQRGKDn5ejrIWldHENjwO2IgfVKw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1647357611; a=rsa-sha256; cv=none; b=KK5Q28ruj3pdxfgeWSNFKNYpPgnEctpPK9XVGxA/2SbPCQIYb+bW64cJygLrpUs/n5P92j uWP0GXmfnMDZofuv076Gm9hL4fcRXU/as+JQB49NNDE3XwYh/EuSwBg0jxq+S4jRYI2VJG rREhnIUkD3MXu/zuHO60Wald0gBW6u4aX50oFN92MuqJlxr35W8EMY4YvWWmoOGqwsbL0A cOz6P1ihF1ra9x5KHHBiqwY+jsqu/qC2D/uL7jymsr/s1cCCEEgauWBjiExtuKGXJsf0yi ECsWcOA5oE7RTJVOXhYr3I/NfzupXfR0XwQcTBAj39zA5L6JpcuWK+rc28KUxA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/12 has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=a445feae98f27325c6845ccebb35f062fce1889d commit a445feae98f27325c6845ccebb35f062fce1889d Author: Kristof Provost AuthorDate: 2022-03-04 14:50:43 +0000 Commit: Kristof Provost CommitDate: 2022-03-15 03:23:28 +0000 libpfctl: support flushing rules/nat/eth Move the code to flush regular rules, nat rules and Ethernet rules into libpfctl for easier re-use. MFC after: 1 week Sponsored by: Rubicon Communications, LLC ("Netgate") Differential Revision: https://reviews.freebsd.org/D34442 (cherry picked from commit f0c334e4deca08b56b4226539c844a7018e9182d) --- lib/libpfctl/libpfctl.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/libpfctl/libpfctl.h | 2 ++ sbin/pfctl/pfctl.c | 37 +++++++++++----------------- 3 files changed, 81 insertions(+), 23 deletions(-) diff --git a/lib/libpfctl/libpfctl.c b/lib/libpfctl/libpfctl.c index 61fce7d743f6..4c110131dcbe 100644 --- a/lib/libpfctl/libpfctl.c +++ b/lib/libpfctl/libpfctl.c @@ -946,6 +946,71 @@ pfctl_kill_states(int dev, const struct pfctl_kill *kill, unsigned int *killed) return (_pfctl_clear_states(dev, kill, killed, DIOCKILLSTATESNV)); } +int +pfctl_clear_rules(int dev, const char *anchorname) +{ + struct pfioc_trans trans; + struct pfioc_trans_e transe[2]; + int ret; + + bzero(&trans, sizeof(trans)); + bzero(&transe, sizeof(transe)); + + transe[0].rs_num = PF_RULESET_SCRUB; + if (strlcpy(transe[0].anchor, anchorname, sizeof(transe[0].anchor)) + >= sizeof(transe[0].anchor)) + return (E2BIG); + + transe[1].rs_num = PF_RULESET_FILTER; + if (strlcpy(transe[1].anchor, anchorname, sizeof(transe[1].anchor)) + >= sizeof(transe[1].anchor)) + return (E2BIG); + + trans.size = 2; + trans.esize = sizeof(transe[0]); + trans.array = transe; + + ret = ioctl(dev, DIOCXBEGIN, &trans); + if (ret != 0) + return (ret); + return ioctl(dev, DIOCXCOMMIT, &trans); +} + +int +pfctl_clear_nat(int dev, const char *anchorname) +{ + struct pfioc_trans trans; + struct pfioc_trans_e transe[3]; + int ret; + + bzero(&trans, sizeof(trans)); + bzero(&transe, sizeof(transe)); + + transe[0].rs_num = PF_RULESET_NAT; + if (strlcpy(transe[0].anchor, anchorname, sizeof(transe[0].anchor)) + >= sizeof(transe[0].anchor)) + return (E2BIG); + + transe[1].rs_num = PF_RULESET_BINAT; + if (strlcpy(transe[1].anchor, anchorname, sizeof(transe[1].anchor)) + >= sizeof(transe[0].anchor)) + return (E2BIG); + + transe[2].rs_num = PF_RULESET_RDR; + if (strlcpy(transe[2].anchor, anchorname, sizeof(transe[2].anchor)) + >= sizeof(transe[2].anchor)) + return (E2BIG); + + trans.size = 3; + trans.esize = sizeof(transe[0]); + trans.array = transe; + + ret = ioctl(dev, DIOCXBEGIN, &trans); + if (ret != 0) + return (ret); + return ioctl(dev, DIOCXCOMMIT, &trans); +} + static int pfctl_get_limit(int dev, const int index, uint *limit) { diff --git a/lib/libpfctl/libpfctl.h b/lib/libpfctl/libpfctl.h index a9bd14527126..3b4e61be0165 100644 --- a/lib/libpfctl/libpfctl.h +++ b/lib/libpfctl/libpfctl.h @@ -304,6 +304,8 @@ int pfctl_clear_states(int dev, const struct pfctl_kill *kill, unsigned int *killed); int pfctl_kill_states(int dev, const struct pfctl_kill *kill, unsigned int *killed); +int pfctl_clear_rules(int dev, const char *anchorname); +int pfctl_clear_nat(int dev, const char *anchorname); int pfctl_set_syncookies(int dev, const struct pfctl_syncookies *s); int pfctl_get_syncookies(int dev, struct pfctl_syncookies *s); diff --git a/sbin/pfctl/pfctl.c b/sbin/pfctl/pfctl.c index 342f355a0116..7dfcd7cc494a 100644 --- a/sbin/pfctl/pfctl.c +++ b/sbin/pfctl/pfctl.c @@ -75,8 +75,8 @@ int pfctl_get_skip_ifaces(void); int pfctl_check_skip_ifaces(char *); int pfctl_adjust_skip_ifaces(struct pfctl *); int pfctl_clear_interface_flags(int, int); -int pfctl_clear_rules(int, int, char *); -int pfctl_clear_nat(int, int, char *); +int pfctl_flush_rules(int, int, char *); +int pfctl_flush_nat(int, int, char *); int pfctl_clear_altq(int, int); int pfctl_clear_src_nodes(int, int); int pfctl_clear_iface_states(int, const char *, int); @@ -449,16 +449,12 @@ pfctl_clear_interface_flags(int dev, int opts) } int -pfctl_clear_rules(int dev, int opts, char *anchorname) +pfctl_flush_rules(int dev, int opts, char *anchorname) { - struct pfr_buffer t; + int ret; - memset(&t, 0, sizeof(t)); - t.pfrb_type = PFRB_TRANS; - if (pfctl_add_trans(&t, PF_RULESET_SCRUB, anchorname) || - pfctl_add_trans(&t, PF_RULESET_FILTER, anchorname) || - pfctl_trans(dev, &t, DIOCXBEGIN, 0) || - pfctl_trans(dev, &t, DIOCXCOMMIT, 0)) + ret = pfctl_clear_rules(dev, anchorname); + if (ret != 0) err(1, "pfctl_clear_rules"); if ((opts & PF_OPT_QUIET) == 0) fprintf(stderr, "rules cleared\n"); @@ -466,17 +462,12 @@ pfctl_clear_rules(int dev, int opts, char *anchorname) } int -pfctl_clear_nat(int dev, int opts, char *anchorname) +pfctl_flush_nat(int dev, int opts, char *anchorname) { - struct pfr_buffer t; + int ret; - memset(&t, 0, sizeof(t)); - t.pfrb_type = PFRB_TRANS; - if (pfctl_add_trans(&t, PF_RULESET_NAT, anchorname) || - pfctl_add_trans(&t, PF_RULESET_BINAT, anchorname) || - pfctl_add_trans(&t, PF_RULESET_RDR, anchorname) || - pfctl_trans(dev, &t, DIOCXBEGIN, 0) || - pfctl_trans(dev, &t, DIOCXCOMMIT, 0)) + ret = pfctl_clear_nat(dev, anchorname); + if (ret != 0) err(1, "pfctl_clear_nat"); if ((opts & PF_OPT_QUIET) == 0) fprintf(stderr, "nat cleared\n"); @@ -2628,10 +2619,10 @@ main(int argc, char *argv[]) switch (*clearopt) { case 'r': - pfctl_clear_rules(dev, opts, anchorname); + pfctl_flush_rules(dev, opts, anchorname); break; case 'n': - pfctl_clear_nat(dev, opts, anchorname); + pfctl_flush_nat(dev, opts, anchorname); break; case 'q': pfctl_clear_altq(dev, opts); @@ -2646,8 +2637,8 @@ main(int argc, char *argv[]) pfctl_clear_stats(dev, opts); break; case 'a': - pfctl_clear_rules(dev, opts, anchorname); - pfctl_clear_nat(dev, opts, anchorname); + pfctl_flush_rules(dev, opts, anchorname); + pfctl_flush_nat(dev, opts, anchorname); pfctl_clear_tables(anchorname, opts); if (!*anchorname) { pfctl_clear_altq(dev, opts);