[PATCH 0/1] nwfilter: add default accept to nftables subchains
This patch aims to let the nwfilter nftables driver follow the nwfilter ebtables driver In ebtables a subchain was created with a default accept When converted to nftables, this resulted in an "accept" at the end of each subchain: chain O-v100562794-mac { ether daddr 52:54:00:a3:13:87 counter packets 9 bytes 1145 return ether daddr 33:33:00:00:00:00/16 counter packets 48 bytes 3712 return ether daddr ff:ff:ff:ff:ff:ff counter packets 22 bytes 924 return counter packets 3 bytes 384 drop
counter packets 0 bytes 0 accept }
In order to mimick this behavior we add an "accept" for each subchain by calling nftablesCreateSubChainAccept after having created all of the fw rules. Dion Bosschieter (1): nwfilter: add default accept to nftables subchains src/nwfilter/nwfilter_nftables_driver.c | 60 +++++++++++++++++-- .../stp-linux.nftables.args | 14 +++++ 2 files changed, 69 insertions(+), 5 deletions(-) -- 2.53.0
In ebtables a subchain was created with a default accept When converted to nftables, this resulted in an "accept" at the end of each subchain: chain O-v100562794-mac { ether daddr 52:54:00:a3:13:87 counter packets 9 bytes 1145 return ether daddr 33:33:00:00:00:00/16 counter packets 48 bytes 3712 return ether daddr ff:ff:ff:ff:ff:ff counter packets 22 bytes 924 return counter packets 3 bytes 384 drop
counter packets 0 bytes 0 accept }
In order to mimick this behavior we add an "accept" for each subchain by calling nftablesCreateSubChainAccept after having created all of the fw rules Signed-off-by: Dion Bosschieter <dionbosschieter@gmail.com> --- src/nwfilter/nwfilter_nftables_driver.c | 60 +++++++++++++++++-- .../stp-linux.nftables.args | 14 +++++ 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/src/nwfilter/nwfilter_nftables_driver.c b/src/nwfilter/nwfilter_nftables_driver.c index a72c51472f..79bd987325 100644 --- a/src/nwfilter/nwfilter_nftables_driver.c +++ b/src/nwfilter/nwfilter_nftables_driver.c @@ -80,9 +80,9 @@ static bool trace_enabled; #define SAME_IP_SET_NAME "same-ip-set" -#define ROOT_CHAINSETTINGS(chain, defaultPolicy) \ +#define ROOT_CHAINSETTINGS(chain) \ "{ type filter hook "chain" priority %d;" \ - " policy "defaultPolicy"; %s }" + " policy "DEFAULT_POLICY"; %s }" VIR_LOG_INIT("nwfilter.nwfilter_nftables_driver"); @@ -183,12 +183,12 @@ static void nftablesCreateTable(virFirewall *fw, fwrule = virFirewallAddCmd(fw, layer, "add", "chain", "bridge", tableName, IN_CHAIN, NULL); virFirewallCmdAddArgFormat(fw, fwrule, - ROOT_CHAINSETTINGS(IN_CHAIN, DEFAULT_POLICY), + ROOT_CHAINSETTINGS(IN_CHAIN), tablePriority, traceSetting); fwrule = virFirewallAddCmd(fw, layer, "add", "chain", "bridge", tableName, OUT_CHAIN, NULL); virFirewallCmdAddArgFormat(fw, fwrule, - ROOT_CHAINSETTINGS(OUT_CHAIN, DEFAULT_POLICY), + ROOT_CHAINSETTINGS(OUT_CHAIN), tablePriority, traceSetting); /* add the one jump rule based on the vmap */ @@ -1825,7 +1825,7 @@ nftablesRuleInstCommand(virFirewall *fw, * nftablesCreateSubChain: * @fw: the firewall ruleset instance * @layer: the firewall layer - * @ifname : The name of the interface to apply the chain to + * @nftablesTableName : The name of the table to create the chain on * @chainPrefix: The prefix to put on the beginning of the name of the chain * @protoidx: Protocol id for conditional jump * @rootChain: The chain to define the jump on @@ -1871,6 +1871,41 @@ nftablesCreateSubChain(virFirewall *fw, virFirewallCmdAddArgList(fw, fwrule, "jump", chain, NULL); } +/* + * nftablesCreateSubChainAccept: + * @fw: the firewall ruleset instance + * @layer: the firewall layer + * @nftablesTableName : The name of the table to create the chain on + * @chainPrefix: The prefix to put on the beginning of the name of the chain + * @chainPostfix: The postfix to put at the end of the name of the chain + * + * To comply with what nwfilter ipebtables was doing for ethernet subchains + * we need to end with an accept on a subchain, to follow the nwfilter ipebtables logic + * on ebtables, subchains have an ACCEPT, which means, that if the end of the chain + * is reached, the packet will be accepted, not returned + * Ebtables chains look as follows if translated to nftables: + * chain O-v100562794-ipv6 { + * ether type ip6 icmpv6 type 133 counter packets 0 bytes 0 drop + * ether type ip6 ether type ip6 meta l4proto ipv6-icmp counter packets 54 bytes 4368 accept + * ether type ip6 ip6 daddr 2a01:7c8:fffe:3::/64 counter packets 0 bytes 0 accept + * counter packets 3 bytes 489 accept + * } + * + * The accept at the bottom has not been provided by the user, it got added by translating ebtables to nftables + */ +static void nftablesCreateSubChainAccept(virFirewall *fw, + virFirewallLayer layer, + const char *nftablesTableName, + const char *chainPrefix, + const char *chainPostfix) +{ + char chain[MAX_NF_CHAINNAME_LENGTH]; + g_snprintf(chain, sizeof(chain), "%s-%s", chainPrefix, chainPostfix); + + virFirewallAddCmd(fw, layer, "add", "rule", "bridge", + nftablesTableName, chain, "accept", NULL); +} + static void nftablesCreateRootChainJump(virFirewall *fw, virFirewallLayer layer, @@ -2165,6 +2200,21 @@ nftablesHandleCreateChains(virFirewall *fw, goto cleanup; } + /* create subchain accepts */ + /* to comply with what nwfilter ipebtables was doing for ethernet subchains + * we need to finally accept on a subchain, to follow the nwfilter ipebtables logic + * on ebtables, subchains have an ACCEPT, which means, that if the end of the chain + * is reached, the packet will be accepted, not returned + */ + for (i = 0; i < nchains; i++) { + if (chains[i]->ethernetInChain) + nftablesCreateSubChainAccept(fw, layer, NF_ETHERNET_TABLE, + chains[i]->prefix, "in"); + if (chains[i]->ethernetOutChain) + nftablesCreateSubChainAccept(fw, layer, NF_ETHERNET_TABLE, + chains[i]->prefix, "out"); + } + /* creation of temp jumps is done as libvirt doesn't execute * atomic nft changes (yet) */ nftablesCreateRootChainJump(fw, layer, cbdata->ifname, IN_IFMATCH, diff --git a/tests/nwfilterxml2firewalldata/stp-linux.nftables.args b/tests/nwfilterxml2firewalldata/stp-linux.nftables.args index 4dabd3cbc2..85ee588a16 100644 --- a/tests/nwfilterxml2firewalldata/stp-linux.nftables.args +++ b/tests/nwfilterxml2firewalldata/stp-linux.nftables.args @@ -111,6 +111,20 @@ nft \ add \ rule \ bridge \ +libvirt_nwfilter_ethernet \ +n-vnet0-stp-xyz-in \ +accept +nft \ +add \ +rule \ +bridge \ +libvirt_nwfilter_ethernet \ +n-vnet0-stp-xyz-out \ +accept +nft \ +add \ +rule \ +bridge \ libvirt_nwfilter_inet \ postrouting \ oif \ -- 2.53.0
On Tue, Oct 27, 2026 at 02:02:59PM +0100, Dion Bosschieter wrote:
In ebtables a subchain was created with a default accept
When converted to nftables, this resulted in an "accept" at the end of each subchain:
chain O-v100562794-mac { ether daddr 52:54:00:a3:13:87 counter packets 9 bytes 1145 return ether daddr 33:33:00:00:00:00/16 counter packets 48 bytes 3712 return ether daddr ff:ff:ff:ff:ff:ff counter packets 22 bytes 924 return counter packets 3 bytes 384 drop
counter packets 0 bytes 0 accept }
This example probably isn't the best as it shows the addition is pointless - nothing will get past the 'drop' statement. When I updated the libvirt-tck data files to work with this new patch, I didn't find any rule where this addition makes a functional difference, as the 'accept' rule always immediately followed another 'accept' or 'drop' rule. Do you have an example XML fragment where this makes a difference to the result ?
In order to mimick this behavior we add an "accept" for each subchain by calling nftablesCreateSubChainAccept after having created all of the fw rules
Signed-off-by: Dion Bosschieter <dionbosschieter@gmail.com> --- src/nwfilter/nwfilter_nftables_driver.c | 60 +++++++++++++++++-- .../stp-linux.nftables.args | 14 +++++ 2 files changed, 69 insertions(+), 5 deletions(-)
diff --git a/src/nwfilter/nwfilter_nftables_driver.c b/src/nwfilter/nwfilter_nftables_driver.c index a72c51472f..79bd987325 100644 --- a/src/nwfilter/nwfilter_nftables_driver.c +++ b/src/nwfilter/nwfilter_nftables_driver.c @@ -80,9 +80,9 @@ static bool trace_enabled;
#define SAME_IP_SET_NAME "same-ip-set"
-#define ROOT_CHAINSETTINGS(chain, defaultPolicy) \ +#define ROOT_CHAINSETTINGS(chain) \ "{ type filter hook "chain" priority %d;" \ - " policy "defaultPolicy"; %s }" + " policy "DEFAULT_POLICY"; %s }"
VIR_LOG_INIT("nwfilter.nwfilter_nftables_driver");
@@ -183,12 +183,12 @@ static void nftablesCreateTable(virFirewall *fw, fwrule = virFirewallAddCmd(fw, layer, "add", "chain", "bridge", tableName, IN_CHAIN, NULL); virFirewallCmdAddArgFormat(fw, fwrule, - ROOT_CHAINSETTINGS(IN_CHAIN, DEFAULT_POLICY), + ROOT_CHAINSETTINGS(IN_CHAIN), tablePriority, traceSetting); fwrule = virFirewallAddCmd(fw, layer, "add", "chain", "bridge", tableName, OUT_CHAIN, NULL); virFirewallCmdAddArgFormat(fw, fwrule, - ROOT_CHAINSETTINGS(OUT_CHAIN, DEFAULT_POLICY), + ROOT_CHAINSETTINGS(OUT_CHAIN), tablePriority, traceSetting);
/* add the one jump rule based on the vmap */ @@ -1825,7 +1825,7 @@ nftablesRuleInstCommand(virFirewall *fw, * nftablesCreateSubChain: * @fw: the firewall ruleset instance * @layer: the firewall layer - * @ifname : The name of the interface to apply the chain to + * @nftablesTableName : The name of the table to create the chain on * @chainPrefix: The prefix to put on the beginning of the name of the chain * @protoidx: Protocol id for conditional jump * @rootChain: The chain to define the jump on @@ -1871,6 +1871,41 @@ nftablesCreateSubChain(virFirewall *fw, virFirewallCmdAddArgList(fw, fwrule, "jump", chain, NULL); }
+/* + * nftablesCreateSubChainAccept: + * @fw: the firewall ruleset instance + * @layer: the firewall layer + * @nftablesTableName : The name of the table to create the chain on + * @chainPrefix: The prefix to put on the beginning of the name of the chain + * @chainPostfix: The postfix to put at the end of the name of the chain + * + * To comply with what nwfilter ipebtables was doing for ethernet subchains + * we need to end with an accept on a subchain, to follow the nwfilter ipebtables logic + * on ebtables, subchains have an ACCEPT, which means, that if the end of the chain + * is reached, the packet will be accepted, not returned + * Ebtables chains look as follows if translated to nftables: + * chain O-v100562794-ipv6 { + * ether type ip6 icmpv6 type 133 counter packets 0 bytes 0 drop + * ether type ip6 ether type ip6 meta l4proto ipv6-icmp counter packets 54 bytes 4368 accept + * ether type ip6 ip6 daddr 2a01:7c8:fffe:3::/64 counter packets 0 bytes 0 accept + * counter packets 3 bytes 489 accept + * } + * + * The accept at the bottom has not been provided by the user, it got added by translating ebtables to nftables + */ +static void nftablesCreateSubChainAccept(virFirewall *fw, + virFirewallLayer layer, + const char *nftablesTableName, + const char *chainPrefix, + const char *chainPostfix) +{ + char chain[MAX_NF_CHAINNAME_LENGTH]; + g_snprintf(chain, sizeof(chain), "%s-%s", chainPrefix, chainPostfix); + + virFirewallAddCmd(fw, layer, "add", "rule", "bridge", + nftablesTableName, chain, "accept", NULL); +} + static void nftablesCreateRootChainJump(virFirewall *fw, virFirewallLayer layer, @@ -2165,6 +2200,21 @@ nftablesHandleCreateChains(virFirewall *fw, goto cleanup; }
+ /* create subchain accepts */ + /* to comply with what nwfilter ipebtables was doing for ethernet subchains + * we need to finally accept on a subchain, to follow the nwfilter ipebtables logic + * on ebtables, subchains have an ACCEPT, which means, that if the end of the chain + * is reached, the packet will be accepted, not returned + */ + for (i = 0; i < nchains; i++) { + if (chains[i]->ethernetInChain) + nftablesCreateSubChainAccept(fw, layer, NF_ETHERNET_TABLE, + chains[i]->prefix, "in"); + if (chains[i]->ethernetOutChain) + nftablesCreateSubChainAccept(fw, layer, NF_ETHERNET_TABLE, + chains[i]->prefix, "out"); + } + /* creation of temp jumps is done as libvirt doesn't execute * atomic nft changes (yet) */ nftablesCreateRootChainJump(fw, layer, cbdata->ifname, IN_IFMATCH, diff --git a/tests/nwfilterxml2firewalldata/stp-linux.nftables.args b/tests/nwfilterxml2firewalldata/stp-linux.nftables.args index 4dabd3cbc2..85ee588a16 100644 --- a/tests/nwfilterxml2firewalldata/stp-linux.nftables.args +++ b/tests/nwfilterxml2firewalldata/stp-linux.nftables.args @@ -111,6 +111,20 @@ nft \ add \ rule \ bridge \ +libvirt_nwfilter_ethernet \ +n-vnet0-stp-xyz-in \ +accept +nft \ +add \ +rule \ +bridge \ +libvirt_nwfilter_ethernet \ +n-vnet0-stp-xyz-out \ +accept +nft \ +add \ +rule \ +bridge \ libvirt_nwfilter_inet \ postrouting \ oif \ -- 2.53.0
With regards, Daniel -- |: https://berrange.com ~~ https://hachyderm.io/@berrange :| |: https://libvirt.org ~~ https://entangle-photo.org :| |: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
On 11/12/26 18:26, Daniel P. Berrangé wrote:
On Tue, Oct 27, 2026 at 02:02:59PM +0100, Dion Bosschieter wrote:
In ebtables a subchain was created with a default accept
When converted to nftables, this resulted in an "accept" at the end of each subchain:
chain O-v100562794-mac { ether daddr 52:54:00:a3:13:87 counter packets 9 bytes 1145 return ether daddr 33:33:00:00:00:00/16 counter packets 48 bytes 3712 return ether daddr ff:ff:ff:ff:ff:ff counter packets 22 bytes 924 return counter packets 3 bytes 384 drop
counter packets 0 bytes 0 accept }
This example probably isn't the best as it shows the addition is pointless - nothing will get past the 'drop' statement.
When I updated the libvirt-tck data files to work with this new patch, I didn't find any rule where this addition makes a functional difference, as the 'accept' rule always immediately followed another 'accept' or 'drop' rule.
Do you have an example XML fragment where this makes a difference to the result ?
With the following should filter definition: <filter name='custom-icmp6-filter' chain='ipv6' priority='-700'> <uuid>e97f7946-9292-4131-a1dd-f83055cf67ea</uuid> <!-- ICMPv6: disallow incoming router solicitations --> <rule action='drop' direction='in' priority='100'> <ipv6 protocol='icmpv6' type='133'/> </rule> <!-- ICMPv6: accept all other icmp --> <rule action='accept' direction='inout' priority='101'> <ipv6 protocol='icmpv6'/> </rule> </filter> <filter name='custom-ipv6' chain='ipv6'> <uuid>09c21bb9-33fe-4006-ba3b-67c28d616883</uuid> <!-- accept inbound dhcpv6 traffic --> <rule action='accept' direction='in' priority='111' statematch='false'> <tcp-ipv6 dstipaddr='2345::1234:ff:dcba:abcd' dstipmask='128' dstportstart='2345'/> </rule> </filter> <filter name='firewall-domain-foo' chain='root'> <uuid>6622d457-9fd3-4504-9356-273c1fd4a4bd</uuid> <filterref filter='custom-icmp6-filter'/> <filterref filter='custom-ipv6'/> <filterref filter='no-other-l2-traffic'/> <filterref filter='qemu-announce-self'/> </filter> On the ebiptables driver [2345::1234:ff:dcba:abcd]:2345 tcp is reachable, But on the nftables implementation it is not. no-other-l2-traffic blocks traffic at the end of the root chain on L2 tcp-ipv6 is a L3 (inet) rule, it will see and filter the traffic later, but the packet is already dropped in nftables in the ethernet table. The reason that it works on the ebiptables driver is that there was an implicit accept at the end of the ethernet subchains, in this case in custom-icmp6-filter. So in theory it is more logical how it works on the nwfilter nftables driver. Because it does a return in a subchain whenever there is no match. In practice however this means that it is harder to migrate from ebiptables to nftables because the firewalling behavior is different. With regards, Dion
participants (3)
-
Daniel P. Berrangé -
Dion -
Dion Bosschieter