Skip to contentSkip to conversation

netfilter: nft_fib: Fix for rpath check with VRF devices

PATCH Superseded Phil Sutter 2 participants 3 messages Sep 20, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH 1/1 netfilter: nft_fib: Fix for rpath check with VRF devices

Analogous to commit b575b24b8eee3 ("netfilter: Fix rpfilter dropping vrf packets by mistake") but for nftables fib expression:
Add special treatment of VRF devices so that typical reverse path filtering via 'fib saddr . iif oif' expression works as expected.

Fixes f6d0cbcf09c50 ("netfilter: nf_tables: add fib expression")
---
 net/ipv4/netfilter/nft_fib_ipv4.c | 3 +++
 net/ipv6/netfilter/nft_fib_ipv6.c | 7 ++++++-
 2 files changed, 9 insertions(+), 1 deletion(-)
net/ipv4/netfilter/nft_fib_ipv4.c +3 -0
index b75cac69bd7e6..7ade04ff972d7 100644
@@ -83,6 +83,9 @@ void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs,
 	else
 		oif = NULL;
 
+	if (priv->flags & NFTA_FIB_F_IIF)
+		fl4.flowi4_oif = l3mdev_master_ifindex_rcu(oif);
+
 	if (nft_hook(pkt) == NF_INET_PRE_ROUTING &&
 	    nft_fib_is_loopback(pkt->skb, nft_in(pkt))) {
 		nft_fib_store_result(dest, priv, nft_in(pkt));
net/ipv6/netfilter/nft_fib_ipv6.c +6 -1
index 8970d0b4faeb4..3f860e331580d 100644
@@ -170,6 +170,10 @@ void nft_fib6_eval(const struct nft_expr *expr, struct nft_regs *regs,
 	else if (priv->flags & NFTA_FIB_F_OIF)
 		oif = nft_out(pkt);
 
+	if ((priv->flags & NFTA_FIB_F_IIF) &&
+	    (netif_is_l3_master(oif) || netif_is_l3_slave(oif)))
+		fl6.flowi6_oif = oif->ifindex;
+
 	iph = skb_header_pointer(pkt->skb, noff, sizeof(_iph), &_iph);
 	if (!iph) {
 		regs->verdict.code = NFT_BREAK;
@@ -197,7 +201,8 @@ void nft_fib6_eval(const struct nft_expr *expr, struct nft_regs *regs,
 	if (rt->rt6i_flags & (RTF_REJECT | RTF_ANYCAST | RTF_LOCAL))
 		goto put_rt_err;
 
-	if (oif && oif != rt->rt6i_idev->dev)
+	if (oif && oif != rt->rt6i_idev->dev &&
+	    l3mdev_master_ifindex_rcu(rt->rt6i_idev->dev) != oif->ifindex)
 		goto put_rt_err;
 
 	nft_fib_store_result(dest, priv, rt->rt6i_idev->dev);
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 31 lines · Phil Sutter

Analogous to commit b575b24b8eee3 ("netfilter: Fix rpfilter dropping vrf packets by mistake") but for nftables fib expression:
Add special treatment of VRF devices so that typical reverse path filtering via 'fib saddr . iif oif' expression works as expected.

Fixes f6d0cbcf09c50 ("netfilter: nf_tables: add fib expression")
---
 net/ipv4/netfilter/nft_fib_ipv4.c | 3 +++
 net/ipv6/netfilter/nft_fib_ipv6.c | 7 ++++++-
 2 files changed, 9 insertions(+), 1 deletion(-)
net/ipv4/netfilter/nft_fib_ipv4.c +3 -0
index b75cac69bd7e6..7ade04ff972d7 100644
@@ -83,6 +83,9 @@ void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs,
 	else
 		oif = NULL;
 
+	if (priv->flags & NFTA_FIB_F_IIF)
+		fl4.flowi4_oif = l3mdev_master_ifindex_rcu(oif);
+
 	if (nft_hook(pkt) == NF_INET_PRE_ROUTING &&
 	    nft_fib_is_loopback(pkt->skb, nft_in(pkt))) {
 		nft_fib_store_result(dest, priv, nft_in(pkt));
net/ipv6/netfilter/nft_fib_ipv6.c +0 -0
index 8970d0b4faeb4..3f860e331580d 100644

net/ipv6/netfilter/nft_fib_ipv6.c void nft_fib6_eval(const struct nft_expr *expr, struct nft_regs *regs, · line 176

Quoted 7 lines
net/ipv6/netfilter/nft_fib_ipv6.c +4 -0
@@ -170,6 +170,10 @@ void nft_fib6_eval(const struct nft_expr *expr, struct nft_regs *regs,
 	else if (priv->flags & NFTA_FIB_F_OIF)
 		oif = nft_out(pkt);
 
+	if ((priv->flags & NFTA_FIB_F_IIF) &&
+	    (netif_is_l3_master(oif) || netif_is_l3_slave(oif)))
+		fl6.flowi6_oif = oif->ifindex;
+

I was about to apply this, but this initialisation comes before nft_fib6_flowi_init(), should this be *after*, or part of nft_fib6_flowi_init() function instead?

author reviewer
#2 lore raw
Quoted 35 lines · Florian Westphal
Quoted 34 lines · Phil Sutter

Analogous to commit b575b24b8eee3 ("netfilter: Fix rpfilter dropping vrf packets by mistake") but for nftables fib expression:
Add special treatment of VRF devices so that typical reverse path filtering via 'fib saddr . iif oif' expression works as expected.

Fixes f6d0cbcf09c50 ("netfilter: nf_tables: add fib expression")
---
 net/ipv4/netfilter/nft_fib_ipv4.c | 3 +++
 net/ipv6/netfilter/nft_fib_ipv6.c | 7 ++++++-
 2 files changed, 9 insertions(+), 1 deletion(-)
net/ipv4/netfilter/nft_fib_ipv4.c +3 -0
index b75cac69bd7e6..7ade04ff972d7 100644
@@ -83,6 +83,9 @@ void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs,
 	else
 		oif = NULL;
 
+	if (priv->flags & NFTA_FIB_F_IIF)
+		fl4.flowi4_oif = l3mdev_master_ifindex_rcu(oif);
+
 	if (nft_hook(pkt) == NF_INET_PRE_ROUTING &&
 	    nft_fib_is_loopback(pkt->skb, nft_in(pkt))) {
 		nft_fib_store_result(dest, priv, nft_in(pkt));
net/ipv6/netfilter/nft_fib_ipv6.c +0 -0
index 8970d0b4faeb4..3f860e331580d 100644
@@ -170,6 +170,10 @@ void nft_fib6_eval(const struct nft_expr *expr, struct nft_regs *regs,
 	else if (priv->flags & NFTA_FIB_F_OIF)
 		oif = nft_out(pkt);
 
Quoted 8 lines

net/ipv6/netfilter/nft_fib_ipv6.c void nft_fib6_eval(const struct nft_expr *expr, struct nft_regs *regs, · line 176

Quoted 4 lines
net/ipv6/netfilter/nft_fib_ipv6.c +4 -0
+	if ((priv->flags & NFTA_FIB_F_IIF) &&
+	    (netif_is_l3_master(oif) || netif_is_l3_slave(oif)))
+		fl6.flowi6_oif = oif->ifindex;
+

I was about to apply this, but this initialisation comes before nft_fib6_flowi_init(), should this be *after*, or part of nft_fib6_flowi_init() function instead?

Oh, I missed that one, sorry. I'll make it part of nft_fib6_flowi_init() to better align with ip6t_rpfilter.c.

Thanks, Phil