In the past, virFirewallBackend was a private static in virfirewall.c
that was set at daemon init time, and used to globally (i.e. for all
drivers in the daemon) determine whether to directly execute iptables
commands, or to run them indirectly via the firewalld passthrough
API. This was removed in commit d566cc55, since we decided that using
the firewalld passthrough API is never appropriate.
Now the virFirewallBackend enum is being reintroduced, with a slightly
different meaning and usage pattern. It will be used to pick between
using nftables commands or iptables commands (in either case directly
handled by libvirt, *not* via firewalld). Additionally, rather than
being a static known only within virfirewall.c and applying to all
firewall commands for all drivers, each virFirewall object will have
its own backend setting, which will be set during virFirewallNew() by
the driver who wants to add a firewall rule.
This will allow the nwfilter and network drivers to each have their
own backend setting, even when they coexist in a single unified
daemon. At least as important as that, it will also allow an instance
of the network driver to remove iptables rules that had been added by
a previous instance, and then add nftables rules for the new instance
(in the case that an admin, or possibly an update, switches the driver
backend from iptables to nftable)
Initially, the enum will only have one usable value -
VIR_FIREWALL_BACKEND_IPTABLES, and that will be hardcoded into all
calls to virFirewallNew(). The other enum value (along with a method
of setting it for each driver) will be added later, when it can be
used (when the nftables backend is in the code).
Signed-off-by: Laine Stump <laine(a)redhat.com>
---
src/libvirt_private.syms | 3 +++
src/network/bridge_driver_linux.c | 4 ++--
src/nwfilter/nwfilter_ebiptables_driver.c | 16 ++++++++--------
src/util/virebtables.c | 4 ++--
src/util/virfirewall.c | 16 +++++++++++++++-
src/util/virfirewall.h | 12 +++++++++++-
src/util/viriptables.c | 2 +-
tests/virfirewalltest.c | 20 ++++++++++----------
8 files changed, 52 insertions(+), 25 deletions(-)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index cf68e4c942..a09e5ae871 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2373,7 +2373,10 @@ virFileCacheSetPriv;
# util/virfirewall.h
virFirewallAddRuleFull;
virFirewallApply;
+virFirewallBackendTypeFromString;
+virFirewallBackendTypeToString;
virFirewallFree;
+virFirewallGetBackend;
virFirewallNew;
virFirewallRemoveRule;
virFirewallRuleAddArg;
diff --git a/src/network/bridge_driver_linux.c b/src/network/bridge_driver_linux.c
index e03c17b259..c6aab9b236 100644
--- a/src/network/bridge_driver_linux.c
+++ b/src/network/bridge_driver_linux.c
@@ -806,7 +806,7 @@ int networkAddFirewallRules(virNetworkDef *def)
{
size_t i;
virNetworkIPDef *ipdef;
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
if (virOnce(&createdOnce, networkSetupPrivateChains) < 0)
return -1;
@@ -932,7 +932,7 @@ void networkRemoveFirewallRules(virNetworkDef *def)
{
size_t i;
virNetworkIPDef *ipdef;
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
virFirewallStartTransaction(fw, VIR_FIREWALL_TRANSACTION_IGNORE_ERRORS);
networkRemoveChecksumFirewallRules(fw, def);
diff --git a/src/nwfilter/nwfilter_ebiptables_driver.c
b/src/nwfilter/nwfilter_ebiptables_driver.c
index 99a74a60e5..9ed23c27a3 100644
--- a/src/nwfilter/nwfilter_ebiptables_driver.c
+++ b/src/nwfilter/nwfilter_ebiptables_driver.c
@@ -2815,7 +2815,7 @@ static int
ebtablesApplyBasicRules(const char *ifname,
const virMacAddr *macaddr)
{
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
char chain[MAX_CHAINNAME_LENGTH];
char chainPrefix = CHAINPREFIX_HOST_IN_TEMP;
char macaddr_str[VIR_MAC_STRING_BUFLEN];
@@ -2888,7 +2888,7 @@ ebtablesApplyDHCPOnlyRules(const char *ifname,
char macaddr_str[VIR_MAC_STRING_BUFLEN];
unsigned int idx = 0;
unsigned int num_dhcpsrvrs;
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
virMacAddrFormat(macaddr, macaddr_str);
@@ -2990,7 +2990,7 @@ ebtablesApplyDropAllRules(const char *ifname)
{
char chain_in [MAX_CHAINNAME_LENGTH],
chain_out[MAX_CHAINNAME_LENGTH];
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
if (ebiptablesAllTeardown(ifname) < 0)
return -1;
@@ -3037,7 +3037,7 @@ ebtablesRemoveBasicRules(const char *ifname)
static int
ebtablesCleanAll(const char *ifname)
{
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
virFirewallStartTransaction(fw, VIR_FIREWALL_TRANSACTION_IGNORE_ERRORS);
@@ -3291,7 +3291,7 @@ ebiptablesApplyNewRules(const char *ifname,
size_t nrules)
{
size_t i, j;
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
g_autoptr(GHashTable) chains_in_set = virHashNew(NULL);
g_autoptr(GHashTable) chains_out_set = virHashNew(NULL);
bool haveEbtables = false;
@@ -3513,7 +3513,7 @@ ebiptablesTearNewRulesFW(virFirewall *fw, const char *ifname)
static int
ebiptablesTearNewRules(const char *ifname)
{
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
virFirewallStartTransaction(fw, VIR_FIREWALL_TRANSACTION_IGNORE_ERRORS);
@@ -3525,7 +3525,7 @@ ebiptablesTearNewRules(const char *ifname)
static int
ebiptablesTearOldRules(const char *ifname)
{
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
virFirewallStartTransaction(fw, VIR_FIREWALL_TRANSACTION_IGNORE_ERRORS);
@@ -3560,7 +3560,7 @@ ebiptablesTearOldRules(const char *ifname)
static int
ebiptablesAllTeardown(const char *ifname)
{
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
virFirewallStartTransaction(fw, VIR_FIREWALL_TRANSACTION_IGNORE_ERRORS);
diff --git a/src/util/virebtables.c b/src/util/virebtables.c
index a1f5f7cf1e..f242186c52 100644
--- a/src/util/virebtables.c
+++ b/src/util/virebtables.c
@@ -78,7 +78,7 @@ ebtablesContextFree(ebtablesContext *ctx)
int
ebtablesAddForwardPolicyReject(ebtablesContext *ctx)
{
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
virFirewallStartTransaction(fw, VIR_FIREWALL_TRANSACTION_IGNORE_ERRORS);
virFirewallAddRule(fw, VIR_FIREWALL_LAYER_ETHERNET,
@@ -106,7 +106,7 @@ ebtablesForwardAllowIn(ebtablesContext *ctx,
const char *macaddr,
int action)
{
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
virFirewallStartTransaction(fw, 0);
virFirewallAddRule(fw, VIR_FIREWALL_LAYER_ETHERNET,
diff --git a/src/util/virfirewall.c b/src/util/virfirewall.c
index 6603fd6341..e1fda162c4 100644
--- a/src/util/virfirewall.c
+++ b/src/util/virfirewall.c
@@ -36,6 +36,11 @@
VIR_LOG_INIT("util.firewall");
+VIR_ENUM_IMPL(virFirewallBackend,
+ VIR_FIREWALL_BACKEND_LAST,
+ "UNSET", /* not yet set */
+ "iptables");
+
typedef struct _virFirewallGroup virFirewallGroup;
struct _virFirewallRule {
@@ -70,6 +75,7 @@ struct _virFirewall {
size_t ngroups;
virFirewallGroup **groups;
size_t currentGroup;
+ virFirewallBackend backend;
};
static virMutex ruleLock = VIR_MUTEX_INITIALIZER;
@@ -91,14 +97,22 @@ virFirewallGroupNew(void)
*
* Returns the new firewall ruleset
*/
-virFirewall *virFirewallNew(void)
+virFirewall *virFirewallNew(virFirewallBackend backend)
{
virFirewall *firewall = g_new0(virFirewall, 1);
+ firewall->backend = backend;
return firewall;
}
+virFirewallBackend
+virFirewallGetBackend(virFirewall *firewall)
+{
+ return firewall->backend;
+}
+
+
static void
virFirewallRuleFree(virFirewallRule *rule)
{
diff --git a/src/util/virfirewall.h b/src/util/virfirewall.h
index ed0bc8b6f7..020dd2bedb 100644
--- a/src/util/virfirewall.h
+++ b/src/util/virfirewall.h
@@ -21,6 +21,7 @@
#pragma once
#include "internal.h"
+#include "virenum.h"
typedef struct _virFirewall virFirewall;
@@ -42,9 +43,18 @@ typedef enum {
VIR_FIREWALL_ACTION_LAST
} virFirewallAction;
-virFirewall *virFirewallNew(void);
+typedef enum {
+ VIR_FIREWALL_BACKEND_UNSET,
+ VIR_FIREWALL_BACKEND_IPTABLES,
+
+ VIR_FIREWALL_BACKEND_LAST,
+} virFirewallBackend;
+
+VIR_ENUM_DECL(virFirewallBackend);
+virFirewall *virFirewallNew(virFirewallBackend backend);
void virFirewallFree(virFirewall *firewall);
+virFirewallBackend virFirewallGetBackend(virFirewall *firewall);
/**
* virFirewallAddRule:
diff --git a/src/util/viriptables.c b/src/util/viriptables.c
index 9c7f7790c4..96b69daf68 100644
--- a/src/util/viriptables.c
+++ b/src/util/viriptables.c
@@ -196,7 +196,7 @@ iptablesPrivateChainCreate(virFirewall *fw,
int
iptablesSetupPrivateChains(virFirewallLayer layer)
{
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
iptablesGlobalChain filter_chains[] = {
{"INPUT", VIR_NETFILTER_INPUT_CHAIN},
{"OUTPUT", VIR_NETFILTER_OUTPUT_CHAIN},
diff --git a/tests/virfirewalltest.c b/tests/virfirewalltest.c
index e676a434c8..48300bf242 100644
--- a/tests/virfirewalltest.c
+++ b/tests/virfirewalltest.c
@@ -62,7 +62,7 @@ static int
testFirewallSingleGroup(const void *opaque G_GNUC_UNUSED)
{
g_auto(virBuffer) cmdbuf = VIR_BUFFER_INITIALIZER;
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
const char *actual = NULL;
const char *expected =
IPTABLES " -w -A INPUT --source 192.168.122.1 --jump ACCEPT\n"
@@ -102,7 +102,7 @@ static int
testFirewallRemoveRule(const void *opaque G_GNUC_UNUSED)
{
g_auto(virBuffer) cmdbuf = VIR_BUFFER_INITIALIZER;
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
const char *actual = NULL;
const char *expected =
IPTABLES " -w -A INPUT --source 192.168.122.1 --jump ACCEPT\n"
@@ -148,7 +148,7 @@ static int
testFirewallManyGroups(const void *opaque G_GNUC_UNUSED)
{
g_auto(virBuffer) cmdbuf = VIR_BUFFER_INITIALIZER;
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
const char *actual = NULL;
const char *expected =
IPTABLES " -w -A INPUT --source 192.168.122.1 --jump ACCEPT\n"
@@ -222,7 +222,7 @@ static int
testFirewallIgnoreFailGroup(const void *opaque G_GNUC_UNUSED)
{
g_auto(virBuffer) cmdbuf = VIR_BUFFER_INITIALIZER;
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
const char *actual = NULL;
const char *expected =
IPTABLES " -w -A INPUT --source 192.168.122.1 --jump ACCEPT\n"
@@ -275,7 +275,7 @@ static int
testFirewallIgnoreFailRule(const void *opaque G_GNUC_UNUSED)
{
g_auto(virBuffer) cmdbuf = VIR_BUFFER_INITIALIZER;
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
const char *actual = NULL;
const char *expected =
IPTABLES " -w -A INPUT --source 192.168.122.1 --jump ACCEPT\n"
@@ -327,7 +327,7 @@ static int
testFirewallNoRollback(const void *opaque G_GNUC_UNUSED)
{
g_auto(virBuffer) cmdbuf = VIR_BUFFER_INITIALIZER;
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
const char *actual = NULL;
const char *expected =
IPTABLES " -w -A INPUT --source 192.168.122.1 --jump ACCEPT\n"
@@ -372,7 +372,7 @@ static int
testFirewallSingleRollback(const void *opaque G_GNUC_UNUSED)
{
g_auto(virBuffer) cmdbuf = VIR_BUFFER_INITIALIZER;
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
const char *actual = NULL;
const char *expected =
IPTABLES " -w -A INPUT --source 192.168.122.1 --jump ACCEPT\n"
@@ -437,7 +437,7 @@ static int
testFirewallManyRollback(const void *opaque G_GNUC_UNUSED)
{
g_auto(virBuffer) cmdbuf = VIR_BUFFER_INITIALIZER;
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
const char *actual = NULL;
const char *expected =
IPTABLES " -w -A INPUT --source 192.168.122.1 --jump ACCEPT\n"
@@ -505,7 +505,7 @@ static int
testFirewallChainedRollback(const void *opaque G_GNUC_UNUSED)
{
g_auto(virBuffer) cmdbuf = VIR_BUFFER_INITIALIZER;
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
const char *actual = NULL;
const char *expected =
IPTABLES " -w -A INPUT --source 192.168.122.1 --jump ACCEPT\n"
@@ -682,7 +682,7 @@ static int
testFirewallQuery(const void *opaque G_GNUC_UNUSED)
{
g_auto(virBuffer) cmdbuf = VIR_BUFFER_INITIALIZER;
- g_autoptr(virFirewall) fw = virFirewallNew();
+ g_autoptr(virFirewall) fw = virFirewallNew(VIR_FIREWALL_BACKEND_IPTABLES);
const char *actual = NULL;
const char *expected =
IPTABLES " -w -A INPUT --source 192.168.122.1 --jump ACCEPT\n"
--
2.39.2